Libical API Documentation 4.0 STABLE VERSION [Visit the v3.0 documentation]
Loading...
Searching...
No Matches
vcardvalue.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: vcardvalue.c
3 CREATOR: Ken Murchison 24 Aug 2022
4
5 SPDX-FileCopyrightText: 2022, Fastmail Pty. Ltd. (https://fastmail.com)
6 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7 ======================================================================*/
8
13
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18#include "vcardvalue.h"
19#include "vcardvalueimpl.h"
20#include "vcardcomponent.h"
21#include "vcardproperty.h"
22#include "vcardtextlist.h"
23#include "icalerror_p.h"
24#include "icalmemory.h"
25
26#include <ctype.h>
27#include <locale.h>
28#include <stdlib.h>
29
30#define TMP_BUF_SIZE 1024
31
32LIBICAL_VCARD_EXPORT struct vcardvalue_impl *vcardvalue_new_impl(vcardvalue_kind kind)
33{
34 struct vcardvalue_impl *v;
35
36 if (!vcardvalue_kind_is_valid(kind)) {
37 return NULL;
38 }
39
40 if ((v = (struct vcardvalue_impl *)icalmemory_new_buffer(sizeof(struct vcardvalue_impl))) == 0) {
42 return 0;
43 }
44
45 v->id = ICAL_STRUCTURE_TYPE_VALUE;
46 v->kind = kind;
47 v->size = 0;
48 v->parent = 0;
49 v->x_value = 0;
50 memset(&v->data, 0, sizeof(v->data));
51
52 return v;
53}
54
59
61{
62 struct vcardvalue_impl *clone;
63
64 clone = vcardvalue_new_impl(old->kind);
65
66 if (clone == 0) {
67 return 0;
68 }
69
70 clone->id = old->id;
71 clone->kind = old->kind;
72 clone->size = old->size;
73
74 switch (clone->kind) {
76 case VCARD_URI_VALUE:
78 if (old->data.v_string != 0) {
79 clone->data.v_string = icalmemory_strdup(old->data.v_string);
80
81 if (clone->data.v_string == 0) {
82 clone->parent = 0;
83 vcardvalue_free(clone);
84 return 0;
85 }
86 }
87 break;
88 }
89 case VCARD_X_VALUE: {
90 if (old->x_value != 0) {
91 clone->x_value = icalmemory_strdup(old->x_value);
92
93 if (clone->x_value == 0) {
94 clone->parent = 0;
95 vcardvalue_free(clone);
96 return 0;
97 }
98 }
99
100 break;
101 }
102
104 if (old->data.v_structured != 0) {
105 clone->data.v_structured =
106 vcardstructured_clone(old->data.v_structured);
107
108 if (clone->data.v_structured == 0) {
109 clone->parent = 0;
110 vcardvalue_free(clone);
111 return 0;
112 }
113 }
114 break;
115
117 if (old->data.v_textlist != 0) {
118 clone->data.v_textlist = vcardstrarray_clone(old->data.v_textlist);
119
120 if (clone->data.v_textlist == 0) {
121 clone->parent = 0;
122 vcardvalue_free(clone);
123 return 0;
124 }
125 }
126 break;
127
128 default: {
129 /* all of the other types are stored as values, not
130 pointers, so we can just copy the whole structure.
131 Every kind that vcardvalue_free() releases memory for
132 must have its own case above. */
133
134 clone->data = old->data;
135 }
136 }
137
138 return clone;
139}
140
141char *vcardvalue_strdup_and_dequote_text(const char **str, const char *sep)
142{
143 const char *p;
144 char *out = (char *)icalmemory_new_buffer(sizeof(char) * strlen(*str) + 1);
145 char *pout;
146
147 if (out == 0) {
148 return 0;
149 }
150
151 pout = out;
152
153 /* Stop the loop when encountering a terminator/separator in the source string
154 or if a null has been written to the destination. This prevents
155 reading past the end of the source string if the last character
156 is a backslash. */
157 for (p = *str; *p != 0; p++) {
158 if (*p == '\\') {
159 p++;
160 switch (*p) {
161 case 0: {
162 p--; // step back to NUL to stop iteration
163 *pout = '\0';
164 break;
165 }
166 case 'n':
167 case 'N': {
168 *pout = '\n';
169 break;
170 }
171 case 't':
172 case 'T': {
173 *pout = '\t';
174 break;
175 }
176 case 'r':
177 case 'R': {
178 *pout = '\r';
179 break;
180 }
181 case 'b':
182 case 'B': {
183 *pout = '\b';
184 break;
185 }
186 case 'f':
187 case 'F': {
188 *pout = '\f';
189 break;
190 }
191 case ';':
192 case ',':
193 case '"':
194 case '\\': {
195 *pout = *p;
196 break;
197 }
198 default: {
199 *pout = ' ';
200 }
201 }
202 } else if (sep && strchr(sep, *p)) {
203 break;
204 } else {
205 *pout = *p;
206 }
207
208 pout++;
209 }
210
211 *str = p;
212
213 *pout = '\0';
214
215 return out;
216}
217
218/*
219 * Returns a quoted copy of a string
220 * @todo This is not RFC5545 compliant.
221 * RFC 6350 allows:
222 * TEXT-CHAR = "\\" / "\," / "\n" / WSP / NON-ASCII / %x21-2B / %x2D-5B / %x5D-7E
223 * but we use the more restrictive (for ADR and N):
224 * component = "\\" / "\," / "\;" / "\n" / WSP / NON-ASCII /
225 %x21-2B / %x2D-3A / %x3C-5B / %x5D-7E
226 * As such, \b, \f, \r are not allowed, not even escaped
227 */
228static char *vcardmemory_strdup_and_quote(char **str, char **str_p, size_t *buf_sz,
229 const char *unquoted_str, bool is_param)
230{
231 const char *p;
232
233 if (!*str) {
234 *buf_sz = strlen(unquoted_str) + 1;
235
236 *str_p = *str = (char *)icalmemory_new_buffer(*buf_sz);
237 }
238
239 /* cppcheck-suppress uninitvar */
240 if (*str_p == 0) {
241 return 0;
242 }
243
244 for (p = unquoted_str; *p != 0; p++) {
245 switch (*p) {
246 case '\b':
247 case '\f':
248 case '\r':
249 /* ignore */
250 break;
251
252 case '\\':
253 case ',':
254 case ';':
255 /* escape */
256 icalmemory_append_char(str, str_p, buf_sz, '\\');
257 icalmemory_append_char(str, str_p, buf_sz, *p);
258 break;
259
260 case '\n':
261 /* If encoding a parameter value, embed literally
262 (parameter encoding is done elsewhere), otherwise escape */
263 icalmemory_append_string(str, str_p, buf_sz, is_param ? "\n" : "\\n"); //NOLINT
264 break;
265
266 default:
267 icalmemory_append_char(str, str_p, buf_sz, *p);
268 break;
269 }
270 }
271
272 /* Assume the last character is not a '\0' and add one. We could
273 check *str_p != 0, but that would be an uninitialized memory
274 read. */
275
276 icalmemory_append_char(str, str_p, buf_sz, '\0');
277 return *str;
278}
279
280/*
281 * FIXME
282 *
283 * This is a bad API, as it forces callers to specify their own X type.
284 * This function should take care of this by itself.
285 */
286static vcardvalue *vcardvalue_new_enum(vcardvalue_kind kind, int x_type, const char *str)
287{
288 int e = vcardproperty_kind_and_string_to_enum((int)kind, str);
289 struct vcardvalue_impl *value;
290
292 value = vcardvalue_new_impl(kind);
293 value->data.v_enum = e;
294 } else {
295 /* Make it an X value */
296 value = vcardvalue_new_impl(kind);
297 value->data.v_enum = x_type;
298 vcardvalue_set_x(value, str);
299 }
300
301 return value;
302}
303
310static bool simple_str_to_doublestr(const char *from, char *result, int result_len, char **to)
311{
312 const char *start = NULL;
313 char *end = NULL, *cur = (char *)from;
314
315 struct lconv *loc_data = localeconv();
316 int i = 0, len;
317 double dtest;
318
319 /*sanity checks */
320 if (!from || !result) {
321 return true;
322 }
323
324 /*skip the white spaces at the beginning */
325 while (*cur && isspace((int)*cur)) {
326 cur++;
327 }
328
329 start = cur;
330 /* copy the part that looks like a double into result.
331 * during the copy, we give ourselves a chance to convert the '.'
332 * into the decimal separator of the current locale.
333 */
334 while (*cur && (isdigit((int)*cur) || *cur == '.' || *cur == '+' || *cur == '-')) {
335 ++cur;
336 }
337 end = cur;
338 len = (int)(ptrdiff_t)(end - start);
339 if (len + 1 >= result_len) {
340 /* huh hoh, number is too big. truncate it */
341 len = result_len - 1;
342 }
343
344 /* copy the float number string into result, and take
345 * care to have the (optional) decimal separator be the one
346 * of the current locale.
347 */
348 for (i = 0; i < len; ++i) {
349 if (start[i] == '.' &&
350 loc_data && loc_data->decimal_point && loc_data->decimal_point[0] && loc_data->decimal_point[0] != '.') {
351 /*replace '.' by the digit separator of the current locale */
352 result[i] = loc_data->decimal_point[0];
353 } else {
354 result[i] = start[i];
355 }
356 }
357 if (to) {
358 *to = end;
359 }
360
361 //NOLINTBEGIN(bugprone-unchecked-string-to-number-conversion)
362 /* now try to convert to a floating point number, to check for validity only */
363 if (sscanf(result, "%lf", &dtest) != 1) {
364 return true;
365 }
366 //NOLINTEND(bugprone-unchecked-string-to-number-conversion)
367 return false;
368}
369
370static vcardvalue *vcardvalue_new_from_string_with_error(vcardvalue_kind kind,
371 const char *str,
372 vcardproperty **error)
373{
374 struct vcardvalue_impl *value = 0;
375
376 icalerror_check_arg_rz(str != 0, "str");
377
378 if (error != 0) {
379 *error = 0;
380 }
381
382 switch (kind) {
383 case VCARD_BOOLEAN_VALUE: {
384 if (!strcmp(str, "TRUE")) {
385 value = vcardvalue_new_boolean(1);
386 } else if (!strcmp(str, "FALSE")) {
387 value = vcardvalue_new_boolean(0);
388 } else if (error != 0) {
389 char temp[TMP_BUF_SIZE];
390 vcardparameter *errParam;
391
392 snprintf(temp, sizeof(temp),
393 "Could not parse %s as a %s property",
394 str, vcardvalue_kind_to_string(kind));
396 *error = vcardproperty_vanew_xlicerror(temp, errParam, (void *)0);
397 }
398 break;
399 }
400
402 value = vcardvalue_new_enum(kind, (int)VCARD_VERSION_X, str);
403 break;
404
405 case VCARD_KIND_VALUE:
406 value = vcardvalue_new_enum(kind, (int)VCARD_KIND_X, str);
407 break;
408
410 value = vcardvalue_new_enum(kind, (int)VCARD_GRAMGENDER_X, str);
411 break;
412
413 case VCARD_INTEGER_VALUE: {
414 char *s_end;
415 const int v = strtol(str, &s_end, 10);
416 if (str != s_end) {
417 value = vcardvalue_new_integer(v);
418 }
419 break;
420 }
421
422 case VCARD_FLOAT_VALUE: {
423 char *s_end;
424 const float v = strtof(str, &s_end);
425 if (str != s_end) {
426 value = vcardvalue_new_float(v);
427 }
428 break;
429 }
430
432#pragma GCC diagnostic push
433#pragma GCC diagnostic ignored "-Wformat-nonliteral"
434 // NOLINTBEGIN(bugprone-unchecked-string-to-number-conversion)
435 /* "+" / "-" hh [ [":"] mm ] */
436 char sign[2] = "";
437 unsigned hour, min = 0;
438 int nchar = 0, len = (int)strlen(str);
439
440 if (len > 3) {
441 const char *fmt;
442
443 if (str[3] == ':') {
444 fmt = "%1[+-]%02u:%02u%n";
445 } else {
446 fmt = "%1[+-]%02u%02u%n";
447 }
448
449 if (3 != sscanf(str, fmt, sign, &hour, &min, &nchar)) {
450 nchar = 0;
451 }
452 } else if (2 != sscanf(str, "%1[+-]%02u%n", sign, &hour, &nchar)) {
453 nchar = 0;
454 }
455// NOLINTEND(bugprone-unchecked-string-to-number-conversion)
456#pragma GCC diagnostic pop
457
458 if (len && (len == nchar)) {
459 int utcoffset = (int)(hour * 3600 + min * 60);
460
461 if (*sign == '-') {
462 utcoffset = -utcoffset;
463 }
464 value = vcardvalue_new_utcoffset(utcoffset);
465 } else if (error != 0) {
466 char temp[TMP_BUF_SIZE];
467 vcardparameter *errParam;
468
469 snprintf(temp, sizeof(temp),
470 "Could not parse %s as a %s property",
471 str, vcardvalue_kind_to_string(kind));
473 *error = vcardproperty_vanew_xlicerror(temp, errParam, (void *)0);
474 }
475 break;
476 }
477
478 case VCARD_TEXT_VALUE: {
479 char *dequoted_str = vcardvalue_strdup_and_dequote_text(&str, NULL);
480
481 value = vcardvalue_new_text(dequoted_str);
482 icalmemory_free_buffer(dequoted_str);
483 break;
484 }
485
488 break;
489 }
490
493 value = vcardvalue_new_structured(st);
495 break;
496 }
497
498 case VCARD_GEO_VALUE: {
499 char *cur = NULL;
500 struct vcardgeotype geo = {0};
501 memset(geo.coords.lat, 0, VCARD_GEO_LEN);
502 memset(geo.coords.lon, 0, VCARD_GEO_LEN);
503
504 if (simple_str_to_doublestr(str, geo.coords.lat, VCARD_GEO_LEN, &cur)) {
505 goto geo_parsing_error;
506 }
507 /* skip white spaces */
508 while (cur && isspace((int)*cur)) {
509 ++cur;
510 }
511
512 /*there is a ';' between the latitude and longitude parts */
513 if (!cur || *cur != ';') {
514 goto geo_parsing_error;
515 }
516
517 ++cur;
518
519 /* skip white spaces */
520 while (cur && isspace((int)*cur)) {
521 ++cur;
522 }
523
524 if (simple_str_to_doublestr(cur, geo.coords.lon, VCARD_GEO_LEN, &cur)) {
525 goto geo_parsing_error;
526 }
527 value = vcardvalue_new_geo(geo);
528 break;
529
530 geo_parsing_error:
531 if (error != 0) {
532 char temp[TMP_BUF_SIZE];
533 snprintf(temp, sizeof(temp),
534 "Could not parse %s as a %s value",
535 str, vcardvalue_kind_to_string(kind));
536 vcardparameter *errParam =
538 *error = vcardproperty_vanew_xlicerror(temp, errParam, (void *)0);
539 }
540 } break;
541
542 case VCARD_URI_VALUE:
543 value = vcardvalue_new_uri(str);
544 break;
545
546 case VCARD_DATE_VALUE:
547 case VCARD_TIME_VALUE:
551 struct vcardtimetype tt;
552
553 tt = vcardtime_from_string(str, kind == VCARD_TIME_VALUE);
555 value = vcardvalue_new_impl(kind);
556 value->data.v_time = tt;
557
559 }
560 break;
561 }
562
564 value = vcardvalue_new_languagetag(str);
565 break;
566
567 case VCARD_X_VALUE: {
568 value = vcardvalue_new_x(str);
569 } break;
570
571 default: {
572 char temp[TMP_BUF_SIZE];
573 if (error != 0) {
574 snprintf(temp, TMP_BUF_SIZE, "Unknown type for \'%s\'", str);
576 *error = vcardproperty_vanew_xlicerror(temp, errParam, (void *)0);
577 }
578
579 snprintf(temp, TMP_BUF_SIZE,
580 "vcardvalue_new_from_string got an unknown value type (%s) for \'%s\'",
581 vcardvalue_kind_to_string(kind), str);
582 icalerror_warn(temp);
583 value = 0;
584 }
585 }
586
587 if (error != 0 && *error == 0 && value == 0) {
588 char temp[TMP_BUF_SIZE];
589 vcardparameter *errParam;
590
591 snprintf(temp, TMP_BUF_SIZE, "Failed to parse value: \'%s\'", str);
592
593 /* coverity[resource_leak] */
595 *error = vcardproperty_vanew_xlicerror(temp, errParam, (void *)0);
596 }
597
598 return value;
599}
600
602{
603 return vcardvalue_new_from_string_with_error(kind, str, (vcardproperty **)0);
604}
605
607{
608 icalerror_check_arg_rv((v != 0), "value");
609
610 if (v->parent != 0) {
611 return;
612 }
613
614 if (v->x_value != 0) {
615 icalmemory_free_buffer(v->x_value);
616 }
617
618 switch (v->kind) {
619 case VCARD_TEXT_VALUE:
620 case VCARD_URI_VALUE:
622 if (v->data.v_string != 0) {
623 icalmemory_free_buffer((void *)v->data.v_string);
624 v->data.v_string = 0;
625 }
626 break;
627 }
628
630 if (v->data.v_textlist != 0) {
631 vcardstrarray_free(v->data.v_textlist);
632 }
633 v->data.v_textlist = 0;
634 break;
635 }
636
638 vcardstructured_unref(v->data.v_structured);
639 v->data.v_structured = 0;
640 break;
641 }
642
643 case VCARD_GEO_VALUE: {
644 v->data.v_geo.coords.lat[0] = '\0';
645 v->data.v_geo.coords.lon[0] = '\0';
646 break;
647 }
648
649 default: {
650 /* Nothing to do */
651 }
652 }
653
654 v->kind = VCARD_NO_VALUE;
655 v->size = 0;
656 v->parent = 0;
657 memset(&v->data, 0, sizeof(v->data));
658 v->id = ICAL_STRUCTURE_TYPE_VALUE_EMPTY;
660}
661
663{
664 if (value == 0) {
665 return false;
666 }
667
668 return true;
669}
670
671static char *vcardvalue_boolean_as_vcard_string_r(const vcardvalue *value)
672{
673 int data;
674 char *str;
675
676 icalerror_check_arg_rz((value != 0), "value");
677 str = (char *)icalmemory_new_buffer(6);
678
679 data = vcardvalue_get_integer(value);
680
681 strncpy(str, data ? "TRUE" : "FALSE", 6);
682
683 return str;
684}
685
686#define MAX_INT_DIGITS 12 /* Enough for 2^32 + sign */
687
688static char *vcardvalue_int_as_vcard_string_r(const vcardvalue *value)
689{
690 int data;
691 char *str;
692
693 icalerror_check_arg_rz((value != 0), "value");
695
696 data = vcardvalue_get_integer(value);
697
698 snprintf(str, MAX_INT_DIGITS, "%d", data);
699
700 return str;
701}
702
703static char *vcardvalue_utcoffset_as_vcard_string_r(const vcardvalue *value,
704 vcardproperty_version version)
705{
706 int data, h, m, s;
707 char sign;
708 char *str;
709 size_t size = 10;
710 const char *fmt;
711
712 icalerror_check_arg_rz((value != 0), "value");
713
714 str = (char *)icalmemory_new_buffer(size);
715 data = vcardvalue_get_utcoffset(value);
716
717 if (abs(data) == data) {
718 sign = '+';
719 } else {
720 sign = '-';
721 }
722
723 h = data / 3600;
724 m = (data - (h * 3600)) / 60;
725 s = (data - (h * 3600) - (m * 60));
726
727 h = MIN(abs(h), 23);
728 m = MIN(abs(m), 59);
729 s = MIN(abs(s), 59);
730#pragma GCC diagnostic push
731#pragma GCC diagnostic ignored "-Wformat-nonliteral"
732 if (s != 0) {
733 if (version == VCARD_VERSION_40) {
734 fmt = "%c%02d%02d%02d";
735 } else {
736 fmt = "%c%02d:%02d:%02d";
737 }
738 } else if (version == VCARD_VERSION_40) {
739 if (m != 0) {
740 fmt = "%c%02d%02d";
741 } else {
742 fmt = "%c%02d";
743 }
744 } else {
745 fmt = "%c%02d:%02d";
746 }
747
748 snprintf(str, size, fmt, sign, h, m, s);
749#pragma GCC diagnostic pop
750 return str;
751}
752
753static char *vcardvalue_text_as_vcard_string_r(const vcardvalue *value)
754{
755 char *str = NULL;
756 char *str_p = NULL;
757 size_t buf_sz;
758
759 return vcardmemory_strdup_and_quote(&str, &str_p, &buf_sz,
760 value->data.v_string, false);
761}
762
763static char *vcardvalue_string_as_vcard_string_r(const vcardvalue *value)
764{
765 const char *data;
766 char *str = 0;
767
768 icalerror_check_arg_rz((value != 0), "value");
769 data = value->data.v_string;
770
771 const size_t len_data = strlen(data) + 1;
772 str = (char *)icalmemory_new_buffer(len_data);
773
774 strncpy(str, data, len_data);
775 str[len_data - 1] = '\0';
776
777 return str;
778}
779
780static void _vcardstrarray_as_vcard_string_r(char **str, char **str_p, size_t *buf_sz,
781 vcardstrarray *array, const char sep,
782 bool is_param)
783{
784 if (!vcardstrarray_size(array)) {
785 (void)vcardmemory_strdup_and_quote(str, str_p, buf_sz, "", is_param);
786 return;
787 }
788
789 size_t i;
790
791 for (i = 0; i < vcardstrarray_size(array); i++) {
792 if (i) {
793 *str_p -= 1; // backup to \0
794 icalmemory_append_char(str, str_p, buf_sz, sep);
795 }
796
797 (void)vcardmemory_strdup_and_quote(str, str_p, buf_sz,
798 vcardstrarray_element_at(array, i), is_param);
799 }
800}
801
802char *vcardstrarray_as_vcard_string_r(const vcardstrarray *array, const char sep)
803{
804 char *buf = NULL;
805 char *buf_ptr = NULL;
806 size_t buf_size;
807
808 _vcardstrarray_as_vcard_string_r(&buf, &buf_ptr, &buf_size,
809 (vcardstrarray *)array, sep, false);
810
811 return buf;
812}
813
815{
816 char *buf;
817 char *buf_ptr;
818 size_t buf_size = 1;
819 size_t i;
820
821 buf_ptr = buf = (char *)icalmemory_new_buffer(buf_size);
822
823 for (i = 0; i < vcardstructured_num_fields(st); i++) {
825
826 if (i) {
827 if (buf_ptr > buf) {
828 buf_ptr -= 1; // backup to \0
829 }
830 icalmemory_append_char(&buf, &buf_ptr, &buf_size, ';');
831 }
832
833 if (array) {
834 _vcardstrarray_as_vcard_string_r(&buf, &buf_ptr, &buf_size,
835 array, ',', is_param);
836 } else {
837 icalmemory_append_char(&buf, &buf_ptr, &buf_size, '\0');
838 }
839 }
840
841 return buf;
842}
843
844static char *vcardvalue_textlist_as_vcard_string_r(const vcardvalue *value,
845 const char sep)
846{
847 icalerror_check_arg_rz((value != 0), "value");
848
849 return vcardstrarray_as_vcard_string_r(value->data.v_textlist, sep);
850}
851
852static char *vcardvalue_structured_as_vcard_string_r(const vcardvalue *value)
853{
854 icalerror_check_arg_rz((value != 0), "value");
855
856 return vcardstructured_as_vcard_string_r(value->data.v_structured, false);
857}
858
859static char *vcardvalue_float_as_vcard_string_r(const vcardvalue *value)
860{
861 float data;
862 char *str;
863 char *old_locale;
864
865 icalerror_check_arg_rz((value != 0), "value");
866 data = vcardvalue_get_float(value);
867
868 /* bypass current locale in order to make
869 sure snprintf uses a '.' as a separator
870 set locate to 'C' and keep old locale */
871 old_locale = icalmemory_strdup(setlocale(LC_NUMERIC, NULL));
872 (void)setlocale(LC_NUMERIC, "C");
873
874 str = (char *)icalmemory_new_buffer(40);
875
876 snprintf(str, 40, "%f", data);
877
878 /* restore saved locale */
879 (void)setlocale(LC_NUMERIC, old_locale);
880 icalmemory_free_buffer(old_locale);
881
882 return str;
883}
884
885static char *vcardvalue_geo_as_vcard_string_r(const vcardvalue *value)
886{
887 icalerror_check_arg_rz((value != 0), "value");
888 size_t max_len = 2 * VCARD_GEO_LEN;
889 char *str = (char *)icalmemory_new_buffer(max_len);
890 snprintf(str, max_len, "%s;%s",
891 value->data.v_geo.coords.lat, value->data.v_geo.coords.lon);
892 str[max_len - 1] = '\0';
893 return str;
894}
895
896const char *vcardvalue_as_vcard_string(const vcardvalue *value)
897{
898 char *buf;
899
900 buf = vcardvalue_as_vcard_string_r(value);
902 return buf;
903}
904
906{
908 bool is_structured;
909 unsigned flags = 0;
910
911 if (value == 0) {
912 return 0;
913 }
914
915 if (value->parent) {
916 vcardcomponent *comp = vcardproperty_get_parent(value->parent);
917 if (comp) {
918 version = vcardcomponent_get_version(comp);
919 }
920 }
921
922 if (version == VCARD_VERSION_NONE) {
923 version = VCARD_VERSION_30;
924 }
925
926 is_structured = vcardproperty_is_structured(vcardproperty_isa(value->parent));
927
928 switch (value->kind) {
930 return vcardvalue_boolean_as_vcard_string_r(value);
931
933 return vcardvalue_int_as_vcard_string_r(value);
934
936 return vcardvalue_utcoffset_as_vcard_string_r(value, version);
937
938 case VCARD_TEXT_VALUE:
939 return vcardvalue_text_as_vcard_string_r(value);
940
942 return vcardvalue_textlist_as_vcard_string_r(value,
943 is_structured ? ';' : ','); //NOLINT
944
946 return vcardvalue_structured_as_vcard_string_r(value);
947
948 case VCARD_GEO_VALUE:
949 return vcardvalue_geo_as_vcard_string_r(value);
950
951 case VCARD_URI_VALUE:
953 return vcardvalue_string_as_vcard_string_r(value);
954
955 case VCARD_TIME_VALUE:
956 flags |= VCARDTIME_BARE_TIME;
957 _fallthrough();
958
959 case VCARD_DATE_VALUE:
960 _fallthrough();
961
963 _fallthrough();
964
966 _fallthrough();
967
969 if (version == VCARD_VERSION_40) {
970 flags |= VCARDTIME_AS_V4;
971 }
972
973 return vcardtime_as_vcard_string_r(value->data.v_time, flags);
974
976 return vcardvalue_float_as_vcard_string_r(value);
977
978 case VCARD_KIND_VALUE:
981 if (value->x_value != 0) {
982 return icalmemory_strdup(value->x_value);
983 }
984
985 return vcardproperty_enum_to_string_r(value->data.v_enum);
986
987 case VCARD_X_VALUE:
988 if (value->x_value != 0) {
989 return icalmemory_strdup(value->x_value);
990 }
991 _fallthrough();
992
993 case VCARD_NO_VALUE:
994 _fallthrough();
995
996 default: {
997 return 0;
998 }
999 }
1000}
1001
1003{
1004 if (value == 0) {
1005 return VCARD_NO_VALUE;
1006 }
1007
1008 return value->kind;
1009}
1010
1011bool vcardvalue_isa_value(void *value)
1012{
1013 const struct vcardvalue_impl *impl = (struct vcardvalue_impl *)value;
1014
1015 icalerror_check_arg_rz((value != 0), "value");
1016
1017 return (impl->id == ICAL_STRUCTURE_TYPE_VALUE);
1018}
1019
1023
1025{
1026 if (!value) {
1027 return;
1028 }
1029 switch (value->kind) {
1030 case VCARD_DATE_VALUE:
1031 case VCARD_TIME_VALUE:
1035 if (vcardtime_is_timestamp(value->data.v_time)) {
1036 value->kind = VCARD_TIMESTAMP_VALUE;
1037 } else if (vcardtime_is_time(value->data.v_time)) {
1038 value->kind = VCARD_TIME_VALUE;
1039 } else if (vcardtime_is_date(value->data.v_time)) {
1040 value->kind = VCARD_DATE_VALUE;
1041 } else {
1042 value->kind = VCARD_DATETIME_VALUE;
1043 }
1044 break;
1045
1046 default:
1047 break;
1048 }
1049}
1050
1051void vcardvalue_set_parent(vcardvalue *value, vcardproperty *property)
1052{
1053 icalerror_check_arg_rv((value != 0), "value");
1054
1055 value->parent = property;
1056}
1057
1058vcardproperty *vcardvalue_get_parent(const vcardvalue *value)
1059{
1060 return value->parent;
1061}
1062
1063/* The remaining interfaces are 'new', 'set' and 'get' for each of the value
1064 types */
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition icalerror.c:90
@ ICAL_NEWFAILED_ERROR
Definition icalerror.h:50
void icalmemory_free_buffer(void *buf)
Releases a buffer.
Definition icalmemory.c:355
char * icalmemory_strdup(const char *s)
Creates a duplicate of a string.
Definition icalmemory.c:242
void icalmemory_append_string(char **buf, char **pos, size_t *buf_size, const char *string)
Appends a string to a buffer.
Definition icalmemory.c:365
void * icalmemory_new_buffer(size_t size)
Creates new buffer with the specified size.
Definition icalmemory.c:315
void icalmemory_append_char(char **buf, char **pos, size_t *buf_size, char ch)
Appends a character to a buffer.
Definition icalmemory.c:406
void icalmemory_add_tmp_buffer(void *buf)
Adds an externally allocated buffer to the ring.
Definition icalmemory.c:158
Common memory management routines.
struct vcardgeotype::@162201204342263205067330224176321027032342016274 coords
enum vcardproperty_version vcardcomponent_get_version(vcardcomponent *comp)
Defines the data structure representing vCard components.
vcardcomponent * vcardproperty_get_parent(const vcardproperty *property)
Returns the parent vcard for the specified property.
struct vcardparameter_impl vcardparameter
vcardparameter * vcardparameter_new_xlicerrortype(vcardparameter_xlicerrortype v)
@ VCARD_XLICERRORTYPE_VALUEPARSEERROR
vcardproperty * vcardproperty_vanew_xlicerror(const char *v,...)
vcardvalue * vcardvalue_new_textlist(vcardstrarray *v)
struct vcardvalue_impl vcardvalue
vcardvalue * vcardvalue_new_geo(vcardgeotype v)
int vcardvalue_get_utcoffset(const vcardvalue *value)
vcardvalue * vcardvalue_new_integer(int v)
@ VCARD_KIND_X
vcardvalue * vcardvalue_new_x(const char *v)
vcardvalue * vcardvalue_new_languagetag(const char *v)
int vcardvalue_get_integer(const vcardvalue *value)
void vcardvalue_set_x(vcardvalue *value, const char *v)
vcardvalue * vcardvalue_new_float(float v)
vcardvalue * vcardvalue_new_uri(const char *v)
float vcardvalue_get_float(const vcardvalue *value)
vcardproperty_version
@ VCARD_VERSION_30
@ VCARD_VERSION_40
@ VCARD_VERSION_NONE
@ VCARD_VERSION_X
vcardvalue * vcardvalue_new_structured(vcardstructuredtype *v)
vcardvalue_kind
@ VCARD_INTEGER_VALUE
@ VCARD_TIMESTAMP_VALUE
@ VCARD_URI_VALUE
@ VCARD_VERSION_VALUE
@ VCARD_TEXTLIST_VALUE
@ VCARD_BOOLEAN_VALUE
@ VCARD_KIND_VALUE
@ VCARD_X_VALUE
@ VCARD_DATETIME_VALUE
@ VCARD_GRAMGENDER_VALUE
@ VCARD_GEO_VALUE
@ VCARD_UTCOFFSET_VALUE
@ VCARD_DATE_VALUE
@ VCARD_FLOAT_VALUE
@ VCARD_DATEANDORTIME_VALUE
@ VCARD_STRUCTURED_VALUE
@ VCARD_LANGUAGETAG_VALUE
@ VCARD_NO_VALUE
@ VCARD_TEXT_VALUE
@ VCARD_TIME_VALUE
vcardvalue * vcardvalue_new_text(const char *v)
vcardvalue * vcardvalue_new_utcoffset(int v)
vcardvalue * vcardvalue_new_boolean(int v)
@ VCARD_GRAMGENDER_X
vcardproperty_kind vcardproperty_isa(const vcardproperty *p)
Defines the data structure representing vCard properties.
vcardproperty * vcardvalue_get_parent(const vcardvalue *value)
vcardproperty_kind vcardproperty_value_kind_to_kind(vcardvalue_kind kind)
char * vcardproperty_enum_to_string_r(int e)
void vcardvalue_set_parent(vcardvalue *value, vcardproperty *property)
int vcardproperty_kind_and_string_to_enum(const int kind, const char *str)
bool vcardproperty_enum_belongs_to_property(vcardproperty_kind kind, int e)
bool vcardproperty_is_structured(vcardproperty_kind pkind)
#define vcardstrarray_clone(array)
#define vcardstrarray_size(array)
#define vcardstrarray_free(array)
#define vcardstrarray_element_at(array, position)
icalstrarray vcardstrarray
vcardstrarray * vcardstructured_field_at(const vcardstructuredtype *st, size_t position)
Returns the field at the given position in a vcardstructuredtype.
vcardstructuredtype * vcardstructured_new_from_string(const char *str)
Creates a new instance of vcardstructuredtype from a string.
vcardstructuredtype * vcardstructured_clone(const vcardstructuredtype *st)
Clones a vcardstructuredtype.
void vcardstructured_unref(vcardstructuredtype *st)
Decrements the reference count of the vcardstructuredtype.
size_t vcardstructured_num_fields(const vcardstructuredtype *st)
Returns the number of fields in a vcardstructuredtype.
struct vcardstructuredtype_impl vcardstructuredtype
Represents a decoded, structured text value.
char * vcardstructured_as_vcard_string_r(const vcardstructuredtype *st, bool is_param)
Formats a vcardstructuredtype as a vCard property or parameter value.
Definition vcardvalue.c:814
vcardstrarray * vcardtextlist_new_from_string(const char *str, char sep)
Defines functions for creating vCard text lists.
bool vcardtime_is_timestamp(const vcardtimetype t)
Definition vcardtime.c:79
bool vcardtime_is_date(const vcardtimetype t)
Definition vcardtime.c:64
vcardtimetype vcardtime_from_string(const char *str, int is_bare_time)
Definition vcardtime.c:490
bool vcardtime_is_null_datetime(const vcardtimetype t)
Definition vcardtime.c:69
bool vcardtime_is_time(const vcardtimetype t)
Definition vcardtime.c:59
char * vcardtime_as_vcard_string_r(const vcardtimetype t, unsigned flags)
Definition vcardtime.c:266
#define VCARDTIME_BARE_TIME
Definition vcardtime.h:48
#define VCARDTIME_AS_V4
Definition vcardtime.h:49
#define VCARD_GEO_LEN
Definition vcardtypes.h:19
vcardvalue * vcardvalue_new_from_string(vcardvalue_kind kind, const char *str)
Definition vcardvalue.c:601
const char * vcardvalue_as_vcard_string(const vcardvalue *value)
Definition vcardvalue.c:896
bool vcardvalue_isa_value(void *value)
#define MAX_INT_DIGITS
Definition vcardvalue.c:686
#define TMP_BUF_SIZE
Definition vcardvalue.c:30
char * vcardvalue_strdup_and_dequote_text(const char **str, const char *sep)
Definition vcardvalue.c:141
struct vcardvalue_impl * vcardvalue_new_impl(vcardvalue_kind kind)
Definition vcardvalue.c:32
vcardvalue_kind vcardvalue_isa(const vcardvalue *value)
char * vcardvalue_as_vcard_string_r(const vcardvalue *value)
Definition vcardvalue.c:905
vcardvalue * vcardvalue_clone(const vcardvalue *old)
Definition vcardvalue.c:60
void vcardvalue_reset_kind(vcardvalue *value)
bool vcardvalue_is_valid(const vcardvalue *value)
Definition vcardvalue.c:662
char * vcardstrarray_as_vcard_string_r(const vcardstrarray *array, const char sep)
Definition vcardvalue.c:802
vcardvalue * vcardvalue_new(vcardvalue_kind kind)
Definition vcardvalue.c:55
void vcardvalue_free(vcardvalue *v)
Definition vcardvalue.c:606
Defines the data structure representing vCard values.
const char * vcardvalue_kind_to_string(const vcardvalue_kind kind)
bool vcardvalue_kind_is_valid(const vcardvalue_kind kind)