Libical API Documentation 4.0 STABLE VERSION [Visit the v3.0 documentation]
Loading...
Searching...
No Matches
icalparser.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: icalparser.c
3 CREATOR: eric 04 August 1999
4
5 SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com>
6 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7
8 The Initial Developer of the Original Code is Eric Busboom
9 ======================================================================*/
10
15
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "icalparser.h"
21#include "icalerror_p.h"
22#include "icalerror.h"
23#include "icallimits.h"
24#include "icalmemory.h"
25#include "icalvalue.h"
26#include "icalparameter.h"
27#include "icalproperty_p.h"
28#include "icalpvl_p.h"
29
30#include <ctype.h>
31#include <stddef.h> /* for ptrdiff_t */
32#include <stdlib.h>
33#include <string.h>
34
36#define TMP_BUF_SIZE 80
38
39static enum icalparser_ctrl icalparser_ctrl_g = ICALPARSER_CTRL_KEEP;
40
41struct icalparser_impl {
42 int buffer_full; /* flag indicates that temp is smaller that
43 data being read into it */
44 int continuation_line; /* last line read was a continuation line */
45 size_t tmp_buf_size;
46 char temp[TMP_BUF_SIZE];
47 icalcomponent *root_component;
48 int version;
49 int level;
50 int lineno;
51 size_t insert_errors; /* counts all parse errors replaced with an X-LIC insert_error() */
52 size_t parse_errors; /* counts all parse errors */
53 icalparser_state state;
54 icalpvl_list components;
55
56 void *line_gen_data;
57};
58
59/*
60 * New version of strstrip() that does not move the pointer.
61 */
62static void strstriplt(char *buf)
63{
64 size_t len, a;
65
66 if (buf == NULL) {
67 return;
68 }
69 if (buf[0] == 0) {
70 return;
71 }
72 len = strlen(buf);
73 /* the casts to unsigned char below are to work around isspace asserts
74 on Windows due to non-ascii characters becoming negative */
75 while ((buf[0] != 0) && (isspace((unsigned char)buf[len - 1]))) {
76 buf[--len] = 0;
77 }
78 if (buf[0] == 0) {
79 return;
80 }
81 a = 0;
82 while (isspace((unsigned char)buf[a])) {
83 a++;
84 }
85 if (a > 0) {
86 memmove(buf, &buf[a], len - a + 1);
87 }
88}
89
90icalparser *icalparser_new(void)
91{
92 struct icalparser_impl *impl = 0;
93
94 if ((impl = (struct icalparser_impl *)icalmemory_new_buffer(sizeof(struct icalparser_impl))) == 0) {
96 return 0;
97 }
98
99 impl->root_component = 0;
100 impl->components = icalpvl_newlist();
101 impl->level = 0;
102 impl->state = ICALPARSER_SUCCESS;
103 impl->tmp_buf_size = TMP_BUF_SIZE;
104 impl->buffer_full = 0;
105 impl->continuation_line = 0;
106 impl->lineno = 0;
107 impl->insert_errors = 0;
108 impl->parse_errors = 0;
109 memset(impl->temp, 0, TMP_BUF_SIZE);
110
111 return (icalparser *)impl;
112}
113
114void icalparser_free(icalparser *parser)
115{
116 icalcomponent *c;
117
118 if (parser->root_component) {
119 icalcomponent_free(parser->root_component);
120 }
121
122 while ((c = icalpvl_pop(parser->components)) != 0) {
124 }
125
126 icalpvl_free(parser->components);
127
129}
130
131void icalparser_set_gen_data(icalparser *parser, void *data)
132{
133 parser->line_gen_data = data;
134}
135
136static char *parser_get_next_char(char c, char *str, int qm)
137{
138 int quote_mode = 0;
139 char *p = str;
140 char next_char = *p;
141 char prev_char = 0;
142
143 size_t charCount = 0;
144 const size_t max_search_chars = icallimit_get(ICAL_LIMIT_PARSE_SEARCH);
145 while (next_char != '\0' && charCount++ < max_search_chars) {
146 if ((prev_char != '\0') && (prev_char != '\\')) {
147 if (qm == 1 && next_char == '"') {
148 /* Encountered a quote, toggle quote mode */
149 quote_mode = !quote_mode;
150 } else if (quote_mode == 0 && next_char == c) {
151 /* Found a matching character out of quote mode, return it */
152 return p;
153 }
154 }
155
156 /* Save the previous character so we can check if it's a backslash in the next iteration */
157 prev_char = next_char;
158 next_char = *(++p);
159 }
160
161 return 0;
162}
163
165static char *make_segment(const char *start, const char *end)
166{
167#if defined(__GNUC__) && !defined(__clang__)
168#pragma GCC diagnostic push
169#pragma GCC diagnostic ignored "-Wstringop-truncation"
170#pragma GCC diagnostic ignored "-Wstringop-overflow"
171#endif
172 char *buf, *tmp;
173 size_t size = (size_t)(ptrdiff_t)(end - start);
174
175 buf = icalmemory_new_buffer(size + 1);
176 strncpy(buf, start, size);
177 *(buf + size) = 0;
178
179 tmp = (buf + size);
180 while ((tmp >= buf) && ((*tmp == '\0') || iswspace((wint_t)*tmp))) {
181 *tmp = 0;
182 tmp--;
183 }
184
185 return buf;
186#if defined(__GNUC__) && !defined(__clang__)
187#pragma GCC diagnostic pop
188#endif
189}
190
191static char *parser_get_prop_name(char *line, char **end)
192{
193 char *p;
194 char *v;
195 char *str;
196
197 p = parser_get_next_char(';', line, 1);
198 v = parser_get_next_char(':', line, 1);
199 if (p == 0 && v == 0) {
200 return 0;
201 }
202
203 /* There is no ';' or, it is after the ';' that marks the beginning of
204 the value */
205 if (v != 0 && (p == 0 || p > v)) {
206 str = make_segment(line, v);
207 *end = v + 1;
208 } else {
209 str = make_segment(line, p);
210 *end = p + 1;
211 }
212
213 return str;
214}
215
216static bool parser_get_param_name_stack(char *line, char *name, size_t name_length,
217 char *value, size_t value_length)
218{
219 char *next;
220 size_t requested_name_length, requested_value_length;
221
222 /* The name is everything up to the equals sign */
223 next = parser_get_next_char('=', line, 1);
224
225 if (next == 0) {
226 return false;
227 }
228
229 requested_name_length = (size_t)(ptrdiff_t)(next - line);
230
231 /* There's not enough room in the name input, we need to fall back
232 to parser_get_param_name_heap and use heap-allocated strings */
233 if (requested_name_length >= name_length - 1) {
234 return false;
235 }
236
237 strncpy(name, line, requested_name_length);
238 name[requested_name_length] = 0;
239
241 int is_multivalued = 0;
242 icalparameter_kind_value_kind(kind, &is_multivalued);
243
244 /* Figure out what range of line contains the value (everything after the equals sign) */
245 next++;
246
247 if (next[0] == '"' && !is_multivalued) {
248 /* Dequote the value if it is a single quoted-string */
249 next++;
250
251 const char *end_quote = (*next == '"') ? next : parser_get_next_char('"', next, 0);
252 if (end_quote == 0) {
253 return false;
254 }
255
256 requested_value_length = (size_t)(ptrdiff_t)(end_quote - next);
257 } else {
258 requested_value_length = strlen(next);
259 }
260
261 /* There's not enough room in the value input, we need to fall back
262 to parser_get_param_name_heap and use heap-allocated strings */
263 if (requested_value_length >= value_length - 1) {
264 return false;
265 }
266
267 memcpy(value, next, requested_value_length);
268 value[requested_value_length] = 0;
269
270 if (!is_multivalued) {
272 }
273
274 return true;
275}
276
277static char *parser_get_param_name_heap(char *line, char **end)
278{
279 /* This is similar to parser_get_param_name_stack except it returns heap
280 objects in the return value and the end parameter. This is used in case
281 the name or value is longer than the stack-allocated string.
282 */
283 char *next;
284 char *str;
285
286 next = parser_get_next_char('=', line, 1);
287
288 if (next == 0) {
289 return 0;
290 }
291
292 str = make_segment(line, next);
293
295 int is_multivalued = 0;
296 icalparameter_kind_value_kind(kind, &is_multivalued);
297
298 *end = next + 1;
299 if (**end == '"' && !is_multivalued) {
300 *end = *end + 1;
301 next = (**end == '"') ? *end : parser_get_next_char('"', *end, 0);
302 if (next == 0) {
304 *end = NULL;
305 return 0;
306 } else {
307 *end = make_segment(*end, next);
308 }
309 } else {
310 *end = make_segment(*end, *end + strlen(*end));
311 }
312
313 if (!is_multivalued) {
315 }
316
317 return str;
318}
319
320static char *icalparser_get_value(char *line, char **end, icalvalue_kind kind)
321{
322 char *str;
323 size_t length = strlen(line);
324
325 _unused(kind);
326
327 if (length == 0) {
328 return 0;
329 }
330
331 *end = line + length;
332 str = make_segment(line, *end);
333
334 return str;
335}
336
342
343static char *parser_get_next_value(char *line, char **end, icalvalue_kind kind)
344{
345 char *next = 0;
346 char *p;
347 char *str;
348 size_t length = strlen(line);
349 int quoted = 0;
350
351 if (line[0] == '\"' && line[length - 1] == '\"') {
352 /* This line is quoted, don't split into multiple values */
353 quoted = 1;
354 }
355
356 p = line;
357 while (!quoted) {
358 next = parser_get_next_char(',', p, 1);
359
360 /* Unfortunately, RFC2445 allowed that for the RECUR value, COMMA
361 could both separate digits in a list, and it could separate
362 multiple recurrence specifications. This is not a friendly
363 part of the spec and was deprecated in RFC5545. The following
364 weirdness tries to distinguish the two uses. It is probably a HACK */
365
366 if (kind == ICAL_RECUR_VALUE) {
367 if (next != 0 && (*end + length) > next + 5 && strncmp(next, "FREQ", 4) == 0) {
368 /* The COMMA was followed by 'FREQ', is it a real separator */
369 /* Fall through */
370 } else if (next != 0) {
371 /* Not real, get the next COMMA */
372 p = next + 1;
373 next = 0;
374 continue;
375 }
376 }
377 /* ignore all commas for query and x values. select dtstart, dtend etc ... */
378 else if (kind == ICAL_QUERY_VALUE || kind == ICAL_X_VALUE) {
379 if (next != 0) {
380 p = next + 1;
381 continue;
382 } else {
383 break;
384 }
385 }
386
387 /* If the comma is preceded by a '\', then it is a literal and
388 not a value separator */
389
390 if ((next != 0 && *(next - 1) == '\\') || (next != 0 && *(next - 3) == '\\'))
391 /*second clause for '/' is on prev line. HACK may be out of bounds */
392 {
393 p = next + 1;
394 } else {
395 break;
396 }
397 }
398
399 if (next == 0) {
400 next = (char *)(size_t)line + length;
401 *end = next;
402 } else {
403 *end = next + 1;
404 }
405
406 if (next == line) {
407 return 0;
408 }
409
410 str = make_segment(line, next);
411 return str;
412}
413
414static char *parser_get_next_parameter(char *line, char **end)
415{
416 char *next;
417 const char *v;
418
419 v = parser_get_next_char(':', line, 1);
420 next = parser_get_next_char(';', line, 1);
421
422 /* There is no ';' or, it is after the ':' that marks the beginning of
423 the value */
424
425 if (next == 0 || next > v) {
426 next = parser_get_next_char(':', line, 1);
427 }
428
429 if (next != 0) {
430 char *str = make_segment(line, next);
431 *end = next + 1;
432 return str;
433 } else {
434 *end = line;
435 return 0;
436 }
437}
438
439char *icalparser_get_line(icalparser *parser,
440 icalparser_line_gen_func line_gen_func)
441{
442 char *line;
443 char *line_p;
444 size_t buf_size = parser->tmp_buf_size;
445
446 line = icalmemory_new_buffer(buf_size);
447 if (!line) {
448 return NULL;
449 }
450 line_p = line;
451 line[0] = '\0';
452
453 /* Read lines by calling line_gen_func and putting the data into
454 parser->temp. If the line is a continuation line ( begins with a
455 space after a newline ) then append the data onto line and read
456 again. Otherwise, exit the loop. */
457
458 while (1) {
459 /* The first part of the loop deals with the temp buffer,
460 which was read on he last pass through the loop. The
461 routine is split like this because it has to read lone line
462 ahead to determine if a line is a continuation line. */
463
464 /* The tmp buffer is not clear, so transfer the data in it to the
465 output. This may be left over from a previous call */
466 if (parser->temp[0] != '\0') {
467 /* If the last position in the temp buffer is occupied,
468 mark the buffer as full. The means we will do another
469 read later, because the line is not finished */
470 if (parser->temp[parser->tmp_buf_size - 1] == 0 &&
471 parser->temp[parser->tmp_buf_size - 2] != '\n' &&
472 parser->temp[parser->tmp_buf_size - 2] != 0) {
473 parser->buffer_full = 1;
474 } else {
475 parser->buffer_full = 0;
476 }
477
478 /* Copy the temp to the output and clear the temp buffer. */
479 if (parser->continuation_line == 1) {
480 /* back up the pointer to erase the continuation characters */
481 parser->continuation_line = 0;
482 line_p--;
483
484 if (*(line_p - 1) == '\r') {
485 line_p--;
486 }
487
488 /* copy one space up to eliminate the leading space */
489 icalmemory_append_string(&line, &line_p, &buf_size, parser->temp + 1);
490
491 } else {
492 icalmemory_append_string(&line, &line_p, &buf_size, parser->temp);
493 }
494
495 parser->temp[0] = '\0';
496 }
497
498 parser->temp[parser->tmp_buf_size - 1] = 1; /* Mark end of buffer */
499
500 /****** Here is where the routine gets string data ******************/
501 if ((*line_gen_func)(parser->temp, parser->tmp_buf_size, parser->line_gen_data) == 0) { /* Get more data */
502
503 /* If the first position is clear, it means we didn't get
504 any more data from the last call to line_ge_func */
505 if (parser->temp[0] == '\0') {
506 if (line[0] != '\0') {
507 /* There is data in the output, so fall through and process it */
508 break;
509 } else {
510 /* No data in output; return and signal that there
511 is no more input */
513 return 0;
514 }
515 }
516 }
517
518 /* If the output line ends in a '\n' and the temp buffer
519 begins with a ' ' or tab, then the buffer holds a continuation
520 line, so keep reading. RFC 5545, section 3.1 */
521
522 if (line_p > line + 1 && *(line_p - 1) == '\n' && (parser->temp[0] == ' ' || parser->temp[0] == '\t')) {
523 parser->continuation_line = 1;
524
525 } else if (parser->buffer_full == 1) {
526 /* The buffer was filled on the last read, so read again */
527
528 } else {
529 /* Looks like the end of this content line, so break */
530 break;
531 }
532 }
533
534 /* Erase the final newline and/or carriage return */
535 if (line_p > line + 1 && *(line_p - 1) == '\n') {
536 *(line_p - 1) = '\0';
537 if (*(line_p - 2) == '\r') {
538 *(line_p - 2) = '\0';
539 }
540
541 } else {
542 *line_p = '\0';
543 }
544
545 while ((*line_p == '\0' || iswspace((wint_t)*line_p)) && line_p > line) {
546 *line_p = '\0';
547 line_p--;
548 }
549
550 return line;
551}
552
553static void insert_error(icalparser *parser, icalcomponent *comp, const char *text,
554 const char *message, icalparameter_xlicerrortype type)
555{
556 char temp[1024];
557
558 if (parser->insert_errors > icallimit_get(ICAL_LIMIT_PARSE_FAILURE_ERROR_MESSAGES)) {
559 return;
560 }
561
562 if (text == 0) {
563 snprintf(temp, 1024, "%s:", message);
564 } else {
565 snprintf(temp, 1024, "%s: %s", message, text);
566 }
567
568 /* coverity[resource_leak] */
569 icalproperty *errProp = icalproperty_vanew_xlicerror(temp, icalparameter_new_xlicerrortype(type), (void *)0);
570 icalcomponent_add_property(comp, errProp);
571
572 parser->insert_errors++;
573}
574
575static bool line_is_blank(const char *line)
576{
577 int i = 0;
578
579 for (i = 0; *(line + i) != 0; i++) {
580 char c = *(line + i);
581
582 if (c != ' ' && c != '\n' && c != '\t') {
583 return false;
584 }
585 }
586
587 return true;
588}
589
590icalcomponent *icalparser_parse(icalparser *parser,
591 icalparser_line_gen_func line_gen_func)
592{
593 char *line;
594 icalcomponent *c;
595 icalcomponent *root = 0;
597 bool cont = false;
598
599 icalerror_check_arg_rz((parser != 0), "parser");
600
602
603 /* Maximum number of bad parsed lines allowed */
604 const size_t max_parse_failures = icallimit_get(ICAL_LIMIT_PARSE_FAILURES);
605 do {
606 line = icalparser_get_line(parser, line_gen_func);
607
608 if ((c = icalparser_add_line(parser, line)) != 0) {
609 if (icalcomponent_get_parent(c) != 0) {
610 /* This is bad news... assert? */
611 }
612
613 icalassert(parser->root_component == 0);
614 icalassert(icalpvl_count(parser->components) == 0);
615
616 if (root == 0) {
617 /* Just one component */
618 root = c;
619 } else if (icalcomponent_isa(root) != ICAL_XROOT_COMPONENT) {
620 /*Got a second component, so move the two components under
621 an XROOT container */
622 icalcomponent *tempc = icalcomponent_new(ICAL_XROOT_COMPONENT);
623
624 icalcomponent_add_component(tempc, root);
626 root = tempc;
627 } else if (icalcomponent_isa(root) == ICAL_XROOT_COMPONENT) {
628 /* Already have an XROOT container, so add the component
629 to it */
631
632 } else {
633 /* Badness */
634 icalassert(0);
635 }
636 }
637 cont = false;
638 if (line != 0) {
640 cont = true;
641 }
642 } while (cont && parser->parse_errors < max_parse_failures); // limit the number of un-parsable data lines
643
645
646 return root;
647}
648
649icalcomponent *icalparser_add_line(icalparser *parser, char *line)
650{
651 char *str;
652 char *end;
653 size_t pcount = 0;
654 size_t vcount = 0;
655 icalproperty *prop;
656 icalproperty_kind prop_kind;
657 icalvalue *value;
658 icalvalue_kind value_kind = ICAL_NO_VALUE;
659
660 icalerror_check_arg_rz((parser != 0), "parser");
661
662 if (line == 0) {
663 parser->parse_errors++;
664 parser->state = ICALPARSER_ERROR;
665 return 0;
666 }
667
668 if (line_is_blank(line)) {
669 return 0;
670 }
671
672 if (icalparser_ctrl_g != ICALPARSER_CTRL_KEEP) {
673 static const unsigned char is_icalctrl[256] = {
674 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1,
675 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
676 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
677 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
678 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
679 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
680 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
681 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
682 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
683 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
684 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
685 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
686 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
687 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
688 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
689 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
690
691 char *c, *d;
692 for (c = d = line; *c; c++) {
693 if (!is_icalctrl[(unsigned char)*c]) {
694 *d++ = *c;
695 } else if (icalparser_ctrl_g == ICALPARSER_CTRL_OMIT) {
696 // omit CTRL character
697 } else {
698 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
699 if (tail) {
700 insert_error(
701 parser, tail, line,
702 "Content line contains invalid CONTROL characters",
704 }
705 parser->parse_errors++;
706 parser->state = ICALPARSER_ERROR;
707 return 0;
708 }
709 }
710 *d = '\0';
711 }
712
713 /* Begin by getting the property name at the start of the line. The
714 property name may end up being "BEGIN" or "END" in which case it
715 is not really a property, but the marker for the start or end of
716 a component */
717
718 end = 0;
719 str = parser_get_prop_name(line, &end);
720
721 if (str == 0 || *str == '\0') {
722 /* Could not get a property name */
723 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
724
725 if (tail) {
726 insert_error(
727 parser, tail, line,
728 "Got a data line, but could not find a property name or component begin tag",
730 }
731 parser->parse_errors++;
732 parser->state = ICALPARSER_ERROR;
734 str = NULL;
735 return 0;
736 }
737
738 /**********************************************************************
739 * Handle begin and end of components
740 **********************************************************************/
741 /* If the property name is BEGIN or END, we are actually
742 starting or ending a new component */
743
744 if (strcasecmp(str, "BEGIN") == 0) {
745 icalcomponent *c = NULL;
746 icalcomponent_kind comp_kind;
747
748 parser->level++;
750 if (parser->level > 100) { // TODO 4.1 - add a limit for component nesting levels
751 icalerror_warn("Too many nesting levels");
753 parser->parse_errors = icallimit_get(ICAL_LIMIT_PARSE_FAILURES); // pathological
754 }
755 str = parser_get_next_value(end, &end, value_kind);
756
757 comp_kind = icalcomponent_string_to_kind(str);
758
759 if (comp_kind == ICAL_X_COMPONENT) {
760 c = icalcomponent_new_x(str);
761 } else if (comp_kind == ICAL_IANA_COMPONENT) {
762 ical_unknown_token_handling tokHandlingSetting =
764 if (tokHandlingSetting == ICAL_ASSUME_IANA_TOKEN) {
765 c = icalcomponent_new_iana(str);
766 }
767 /* ICAL_DISCARD_TOKEN / ICAL_TREAT_AS_ERROR: treat as error */
768 } else {
769 c = icalcomponent_new(comp_kind);
770 }
771
772 if (c == 0) {
774 insert_error(parser, c, str, "Parse error in component name",
776 parser->parse_errors++;
777 }
778
779 icalpvl_push(parser->components, c);
780
781 parser->state = ICALPARSER_BEGIN_COMP;
782
784 str = NULL;
785 return 0;
786
787 } else if (strcasecmp(str, "END") == 0) {
788 icalcomponent *tail;
789
790 parser->level--;
792 str = parser_get_next_value(end, &end, value_kind);
793
794 /* Pop last component off of list and add it to the second-to-last */
795 parser->root_component = icalpvl_pop(parser->components);
796
797 tail = icalpvl_data(icalpvl_tail(parser->components));
798
799 if (tail != 0) {
800 icalcomponent_add_component(tail, parser->root_component);
801 }
802
804 str = NULL;
805
806 if (parser->level < 0) {
807 // Encountered an END before any BEGIN, this must be invalid data
808 icalerror_warn("Encountered END before BEGIN");
809 parser->parse_errors++;
810 parser->state = ICALPARSER_ERROR;
811 parser->level = 0;
812 return 0;
813 } else if (parser->level == 0) {
814 /* Return the component if we are back to the 0th level */
815 icalcomponent *rtrn;
816
817 if (icalpvl_count(parser->components) != 0) {
818 /* There are still components on the stack -- this means
819 that one of them did not have a proper "END" */
820 icalpvl_push(parser->components, parser->root_component);
821 (void)icalparser_clean(parser); /* may reset parser->root_component */
822 }
823
824 icalassert(icalpvl_count(parser->components) == 0);
825
826 parser->state = ICALPARSER_SUCCESS;
827 rtrn = parser->root_component;
828 parser->root_component = 0;
829 return rtrn;
830
831 } else {
832 parser->state = ICALPARSER_END_COMP;
833 return 0;
834 }
835 }
836
837 /* There is no point in continuing if we have not seen a
838 component yet */
839
840 if (icalpvl_data(icalpvl_tail(parser->components)) == 0) {
841 parser->parse_errors++;
842 parser->state = ICALPARSER_ERROR;
844 str = NULL;
845 return 0;
846 }
847
848 /**********************************************************************
849 * Handle property names
850 **********************************************************************/
851
852 /* At this point, the property name really is a property name,
853 (Not a component name) so make a new property and add it to
854 the component */
855
856 prop_kind = icalproperty_string_to_kind(str);
857
858 if (prop_kind == ICAL_IANA_PROPERTY &&
860 prop_kind = ICAL_NO_PROPERTY;
861 }
862
863 prop = icalproperty_new(prop_kind);
864
865 if (prop != 0) {
866 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
867
868 if (prop_kind == ICAL_X_PROPERTY) {
869 icalproperty_set_x_name(prop, str);
870 } else if (prop_kind == ICAL_IANA_PROPERTY) {
872 }
873
874 icalcomponent_add_property(tail, prop);
875
876 /* Set the value kind for the default for this type of
877 property. This may be re-set by a VALUE parameter */
879
880 } else {
881 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
882
883 insert_error(parser, tail, str, "Parse error in property name",
885
886 parser->parse_errors++;
887 parser->state = ICALPARSER_ERROR;
889 str = NULL;
890 return 0;
891 }
892
894 str = NULL;
895
896 /**********************************************************************
897 * Handle parameter values
898 **********************************************************************/
899
900 /* Now, add any parameters to the last property */
901 const size_t max_parse_failures = icallimit_get(ICAL_LIMIT_PARSE_FAILURES);
902 const size_t maximum_allowed_parameters = icallimit_get(ICAL_LIMIT_PARAMETERS);
903 while ((pcount < maximum_allowed_parameters) && (parser->parse_errors < max_parse_failures)) {
904 if (*(end - 1) == ':') {
905 /* if the last separator was a ":" and the value is a
906 URL, icalparser_get_next_parameter will find the
907 ':' in the URL, so better break now. */
908 break;
909 }
910
912 str = parser_get_next_parameter(end, &end);
913 strstriplt(str);
914 if (str != 0) {
915 char *name_heap = 0;
916 char *pvalue_heap = 0;
917 char name_stack[TMP_BUF_SIZE] = {};
918 char pvalue_stack[TMP_BUF_SIZE] = {};
919 const char *name = name_stack;
920 const char *pvalue = pvalue_stack;
921
922 icalparameter *param = 0;
924 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
925
926 if (!parser_get_param_name_stack(str, name_stack, sizeof(name_stack),
927 pvalue_stack, sizeof(pvalue_stack))) {
928 name_heap = parser_get_param_name_heap(str, &pvalue_heap);
929
930 name = name_heap;
931 pvalue = pvalue_heap;
932
933 if (name_heap == 0) {
934 /* 'tail' defined above */
935 insert_error(parser, tail, str, "Can't parse parameter name",
937 parser->parse_errors++;
939 str = NULL;
940 break;
941 }
942 }
943
944 kind = icalparameter_string_to_kind(name);
945
946 if (kind == ICAL_X_PARAMETER) {
948 if (param != 0) {
949 icalparameter_set_xname(param, name);
950 icalparameter_set_xvalue(param, pvalue);
951 }
952 } else if (kind == ICAL_IANA_PARAMETER) {
953 ical_unknown_token_handling tokHandlingSetting =
955 if (tokHandlingSetting == ICAL_DISCARD_TOKEN) {
956 icalmemory_free_buffer(name_heap);
957 name_heap = 0;
958
959 icalmemory_free_buffer(pvalue_heap);
960 pvalue_heap = 0;
961
962 continue;
963 }
964
966
967 if (param != 0) {
968 icalparameter_set_xname(param, name);
969 icalparameter_set_xvalue(param, pvalue);
970 }
971 } else if (kind != ICAL_NO_PARAMETER) {
972 param = icalparameter_new_from_value_string(kind, pvalue);
973 } else {
974 /* Error. Failed to parse the parameter */
975 /* 'tail' defined above */
976
977 /* Change for mozilla */
978 /* have the option of being flexible towards unsupported parameters */
980 insert_error(parser, tail, str, "Can't parse parameter name",
982 parser->parse_errors++;
983 parser->state = ICALPARSER_ERROR;
984
985 icalmemory_free_buffer(pvalue_heap);
986 icalmemory_free_buffer(name_heap);
988 str = NULL;
989 return 0;
990 } else {
991 icalmemory_free_buffer(name_heap);
992 name_heap = 0;
993 icalmemory_free_buffer(pvalue_heap);
994 pvalue_heap = 0;
996 str = NULL;
997 continue;
998 }
999 }
1000
1001 icalmemory_free_buffer(pvalue_heap);
1002 pvalue_heap = 0;
1003
1004 icalmemory_free_buffer(name_heap);
1005 name_heap = 0;
1006
1007 if (param == 0) {
1008 /* 'tail' defined above */
1009 insert_error(parser, tail, str, "Can't parse parameter value",
1011
1012 tail = 0;
1013 parser->parse_errors++;
1014 parser->state = ICALPARSER_ERROR;
1015
1017 str = NULL;
1018
1019 continue;
1020 }
1021
1022 /* If it is a VALUE parameter, set the kind of value */
1024 value_kind =
1027
1028 if (!icalproperty_value_kind_is_valid(prop_kind, value_kind)) {
1029 /* Ooops, invalid VALUE parameter, so reset the value_kind */
1030
1031 const char *err_str = "Invalid VALUE type for property";
1032 const char *prop_str = icalproperty_kind_to_string(prop_kind);
1033 size_t tmp_buf_len = strlen(err_str) + strlen(prop_str) + 2;
1034 char *tmp_buf = icalmemory_tmp_buffer(tmp_buf_len);
1035 snprintf(tmp_buf, tmp_buf_len, "%s %s", err_str, prop_str);
1036
1037 insert_error(parser, tail, str, tmp_buf,
1039
1040 value_kind = icalproperty_kind_to_value_kind(prop_kind);
1041
1042 icalparameter_free(param);
1043 tail = 0;
1044 parser->parse_errors++;
1045 parser->state = ICALPARSER_ERROR;
1046
1048 str = NULL;
1049 pcount++;
1050 continue;
1051 }
1052 }
1053
1054 /* Everything is OK, so add the parameter */
1055 icalproperty_add_parameter(prop, param);
1057 str = NULL;
1058 pcount++;
1059
1060 } else {
1061 /* str is NULL */
1062 break;
1063 }
1064
1065 } /* while(pcount < maximum_allowed_parameters) */
1066
1067 /**********************************************************************
1068 * Handle values
1069 **********************************************************************/
1070
1071 /* Look for values. If there are ',' characters in the values,
1072 then there are multiple values, so clone the current
1073 parameter and add one part of the value to each clone */
1074 vcount = 0;
1075 const size_t maximum_property_values = icallimit_get(ICAL_LIMIT_PROPERTY_VALUES);
1076 while ((vcount < maximum_property_values) && (parser->parse_errors < max_parse_failures)) {
1077 /* Only some properties can have multiple values. This list was taken
1078 from rfc5545. Also added the x-properties, because the spec actually
1079 says that commas should be escaped. For x-properties, other apps may
1080 depend on that behaviour
1081 */
1083 str = NULL;
1084
1085 if (icalproperty_value_kind_is_multivalued(prop_kind, &value_kind)) {
1086 str = parser_get_next_value(end, &end, value_kind);
1087 } else {
1088 str = icalparser_get_value(end, &end, value_kind);
1089 }
1090 strstriplt(str);
1091
1092 if (str != 0) {
1093 if (vcount > 0) {
1094 /* Actually, only clone after the second value */
1095 icalproperty *clone = icalproperty_clone(prop);
1096 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
1097
1098 icalcomponent_add_property(tail, clone);
1099 prop = clone;
1100 }
1101
1102 value = icalvalue_new_from_string(value_kind, str);
1103
1104 /* Don't add properties without value */
1105 if (value == 0) {
1106 char temp[200]; /* HACK */
1107
1108 icalproperty_kind isa_prop_kind = icalproperty_isa(prop);
1109 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
1110
1111 snprintf(temp, sizeof(temp),
1112 "Can't parse as %s value in %s property. Removing entire property",
1113 icalvalue_kind_to_string(value_kind),
1114 icalproperty_kind_to_string(isa_prop_kind));
1115
1116 insert_error(parser, tail, str, temp, ICAL_XLICERRORTYPE_VALUEPARSEERROR);
1117
1118 /* Remove the troublesome property */
1120 icalproperty_free(prop);
1121 parser->parse_errors++;
1122 parser->state = ICALPARSER_ERROR;
1123
1125 str = NULL;
1126 return 0;
1127
1128 } else {
1129 vcount++;
1130 icalproperty_set_value(prop, value);
1131 }
1133 str = NULL;
1134
1135 } else {
1137 /* Don't replace empty properties with an error.
1138 Set an empty length string (not null) as the value instead */
1139 if (vcount == 0) {
1141 }
1142 break;
1143 } else {
1144 if (vcount == 0) {
1145 char temp[200]; /* HACK */
1146
1147 icalproperty_kind isa_prop_kind = icalproperty_isa(prop);
1148 icalcomponent *tail = icalpvl_data(icalpvl_tail(parser->components));
1149
1150 snprintf(temp, sizeof(temp), "No value for %s property. Removing entire property",
1151 icalproperty_kind_to_string(isa_prop_kind));
1152
1153 insert_error(parser, tail, str, temp, ICAL_XLICERRORTYPE_VALUEPARSEERROR);
1154
1155 /* Remove the troublesome property */
1157 icalproperty_free(prop);
1158 parser->parse_errors++;
1159 parser->state = ICALPARSER_ERROR;
1160 return 0;
1161 } else {
1162 break;
1163 }
1164 }
1165 }
1166 }
1167 if (parser->parse_errors == max_parse_failures) {
1168 parser->state = ICALPARSER_ERROR;
1169 return 0;
1170 }
1171
1172 /****************************************************************
1173 * End of component parsing.
1174 *****************************************************************/
1175
1176 if (icalpvl_data(icalpvl_tail(parser->components)) == 0 && parser->level == 0) {
1177 /* HACK. Does this clause ever get executed? */
1178 parser->state = ICALPARSER_SUCCESS;
1179 icalassert(0);
1180 return parser->root_component;
1181 } else {
1182 parser->state = ICALPARSER_IN_PROGRESS;
1183 return 0;
1184 }
1185}
1186
1188{
1189 return parser->state;
1190}
1191
1192icalcomponent *icalparser_clean(icalparser *parser)
1193{
1194 icalcomponent *tail;
1195
1196 icalerror_check_arg_rz((parser != 0), "parser");
1197
1198 /* We won't get a clean exit if some components did not have an
1199 "END" tag. Clear off any component that may be left in the list */
1200
1201 while ((tail = icalpvl_data(icalpvl_tail(parser->components))) != 0) {
1202 insert_error(parser, tail, " ",
1203 "Missing END tag for this component. Closing component at end of input.",
1205 parser->parse_errors++;
1206 parser->root_component = icalpvl_pop(parser->components);
1207 tail = icalpvl_data(icalpvl_tail(parser->components));
1208
1209 if (tail != 0 && parser->root_component != NULL) {
1210 if (icalcomponent_get_parent(parser->root_component) != 0) {
1211 icalerror_warn(
1212 "icalparser_clean is trying to attach a component for the second time");
1213 } else {
1214 icalcomponent_add_component(tail, parser->root_component);
1215 }
1216 }
1217 }
1218 icalcomponent *result = parser->root_component;
1219 parser->root_component = 0;
1220 parser->level = 0;
1221 parser->state = ICALPARSER_SUCCESS;
1222 parser->buffer_full = 0;
1223 parser->continuation_line = 0;
1224 parser->lineno = 0;
1225 parser->parse_errors = 0;
1226 parser->insert_errors = 0;
1227 memset(parser->temp, 0, sizeof(parser->temp));
1228 return result;
1229}
1230
1231struct slg_data {
1232 const char *pos;
1233 const char *str;
1234};
1235
1236char *icalparser_string_line_generator(char *out, size_t buf_size, void *d)
1237{
1238 int replace_cr = 0;
1239 const char *n;
1240 size_t size;
1241 struct slg_data *data = (struct slg_data *)d;
1242
1243 if (data->pos == 0) {
1244 data->pos = data->str;
1245 if (data->pos && strlen(data->pos) > 2) {
1246 /* Skip the UTF-8 marker at the beginning of the string */
1247 if (((unsigned char)data->pos[0]) == 0xEF &&
1248 ((unsigned char)data->pos[1]) == 0xBB &&
1249 ((unsigned char)data->pos[2]) == 0xBF) {
1250 data->pos += 3;
1251 }
1252 }
1253 }
1254
1255 /* If the pointer is at the end of the string, we are done */
1256 if (!data->pos || *data->pos == 0) {
1257 return 0;
1258 }
1259
1260 n = strchr(data->pos, '\n');
1261
1262 if (n == 0) {
1263 n = strchr(data->pos, '\r'); /* support malformed input with only CR and no LF
1264 (e.g. from Kerio Connect Server) */
1265 if (n == 0) {
1266 size = strlen(data->pos);
1267 } else {
1268 n++; /* include CR in output - will be replaced by LF later on */
1269 replace_cr = 1;
1270 size = (size_t)(ptrdiff_t)(n - data->pos);
1271 }
1272 } else {
1273 n++; /* include newline in output */
1274 size = (size_t)(ptrdiff_t)(n - data->pos);
1275 }
1276
1277 if (size > buf_size - 1) {
1278 size = buf_size - 1;
1279 }
1280
1281#if defined(__GNUC__) && !defined(__clang__)
1282#pragma GCC diagnostic push
1283#pragma GCC diagnostic ignored "-Wstringop-truncation"
1284#pragma GCC diagnostic ignored "-Wstringop-overflow"
1285#endif
1286 strncpy(out, data->pos, size);
1287#if defined(__GNUC__) && !defined(__clang__)
1288#pragma GCC diagnostic pop
1289#endif
1290
1291 if (replace_cr) {
1292 *(out + size - 1) = '\n';
1293 }
1294 *(out + size) = '\0';
1295
1296 data->pos += size;
1297
1298 return out;
1299}
1300
1301icalcomponent *icalparser_parse_string(const char *str)
1302{
1303 icalcomponent *c;
1304 struct slg_data d;
1305 icalparser *p;
1306
1308
1309 d.pos = 0;
1310 d.str = str;
1311
1312 p = icalparser_new();
1313 if (!p) {
1314 return NULL;
1315 }
1316
1318
1320
1322
1324
1325 icalparser_free(p);
1326
1327 return c;
1328}
1329
1331{
1332 return icalparser_ctrl_g;
1333}
1334
1336{
1337 icalparser_ctrl_g = ctrl;
1338}
icalcomponent_kind icalcomponent_string_to_kind(const char *string)
void icalcomponent_remove_property(icalcomponent *component, icalproperty *property)
icalcomponent * icalcomponent_new(icalcomponent_kind kind)
icalcomponent * icalcomponent_new_iana(const char *iana_name)
icalcomponent * icalcomponent_new_x(const char *x_name)
void icalcomponent_add_property(icalcomponent *component, icalproperty *property)
icalcomponent_kind icalcomponent_isa(const icalcomponent *component)
void icalcomponent_free(icalcomponent *c)
void icalcomponent_add_component(icalcomponent *parent, icalcomponent *child)
icalparameter_kind
@ ICAL_NO_PARAMETER
@ ICAL_IANA_PARAMETER
@ ICAL_X_PARAMETER
@ ICAL_VALUE_PARAMETER
icalparameter_value icalparameter_get_value(const icalparameter *value)
struct icalparameter_impl icalparameter
icalparameter_xlicerrortype
@ ICAL_XLICERRORTYPE_PROPERTYPARSEERROR
@ ICAL_XLICERRORTYPE_PARAMETERNAMEPARSEERROR
@ ICAL_XLICERRORTYPE_PARAMETERVALUEPARSEERROR
@ ICAL_XLICERRORTYPE_VALUEPARSEERROR
@ ICAL_XLICERRORTYPE_COMPONENTPARSEERROR
icalparameter * icalparameter_new_xlicerrortype(icalparameter_xlicerrortype v)
icalproperty * icalproperty_vanew_xlicerror(const char *v,...)
icalproperty_kind
@ ICAL_IANA_PROPERTY
@ ICAL_X_PROPERTY
@ ICAL_NO_PROPERTY
icalvalue_kind
@ ICAL_RECUR_VALUE
@ ICAL_QUERY_VALUE
@ ICAL_X_VALUE
@ ICAL_NO_VALUE
struct icalvalue_impl icalvalue
icalcomponent_kind
Definition icalenums.h:29
@ ICAL_X_COMPONENT
Definition icalenums.h:48
@ ICAL_XROOT_COMPONENT
Definition icalenums.h:32
@ ICAL_IANA_COMPONENT
Definition icalenums.h:67
@ ICAL_XLICINVALID_COMPONENT
Definition icalenums.h:54
bool icalerror_get_errors_are_fatal(void)
Determine if errors are fatal.
Definition icalerror.c:36
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition icalerror.c:90
Error handling for libical.
icalerrorstate
Determine if an error is fatal or non-fatal.
Definition icalerror.h:89
@ ICAL_ERROR_NONFATAL
Definition icalerror.h:94
icalerrorstate icalerror_get_error_state(icalerrorenum error)
Gets the error state (severity) for a given error.
@ ICAL_NEWFAILED_ERROR
Definition icalerror.h:50
@ ICAL_MALFORMEDDATA_ERROR
Definition icalerror.h:59
void icalerror_set_error_state(icalerrorenum error, icalerrorstate state)
Sets the icalerrorstate for a given icalerrorenum error.
size_t icallimit_get(icallimits_kind kind)
Definition icallimits.c:29
Defines the interface for getting/setting internal library limits.
@ ICAL_LIMIT_PARSE_FAILURE_ERROR_MESSAGES
Definition icallimits.h:28
@ ICAL_LIMIT_PROPERTY_VALUES
Definition icallimits.h:36
@ ICAL_LIMIT_PARAMETERS
Definition icallimits.h:32
@ ICAL_LIMIT_PARSE_FAILURES
Definition icallimits.h:24
@ ICAL_LIMIT_PARSE_SEARCH
Definition icallimits.h:26
void icalmemory_free_buffer(void *buf)
Releases a buffer.
Definition icalmemory.c:355
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_tmp_buffer(size_t size)
Creates a new temporary buffer on the ring and returns it.
Definition icalmemory.c:182
Common memory management routines.
void icalparameter_free(icalparameter *param)
Frees an icalparameter object.
icalparameter * icalparameter_new(icalparameter_kind kind)
Creates new icalparameter object.
void icalparameter_set_xname(icalparameter *param, const char *v)
Sets the X-name of param to v.
icalparameter_kind icalparameter_isa(const icalparameter *parameter)
void icalparameter_decode_value(char *value)
void icalparameter_set_xvalue(icalparameter *param, const char *v)
Sets the X-value of param to v.
Defines the data structure representing iCalendar parameters.
icalparameter_kind icalparameter_string_to_kind(const char *string)
Returns the icalparameter_kind for a given string.
icalvalue_kind icalparameter_kind_value_kind(const icalparameter_kind kind, int *is_multivalued)
icalparameter * icalparameter_new_from_value_string(icalparameter_kind kind, const char *value)
Creates new icalparameter of a given kind with a given value.
icalcomponent * icalparser_parse(icalparser *parser, icalparser_line_gen_func line_gen_func)
Message oriented parsing.
Definition icalparser.c:590
icalcomponent * icalparser_add_line(icalparser *parser, char *line)
Adds a single line to be parsed by the icalparser.
Definition icalparser.c:649
void icalparser_free(icalparser *parser)
Frees an icalparser object.
Definition icalparser.c:114
void icalparser_set_ctrl(enum icalparser_ctrl ctrl)
Set the parser setting how to handle CONTROL characters.
enum icalparser_ctrl icalparser_get_ctrl(void)
Get the current parser setting how to handle CONTROL characters.
icalcomponent * icalparser_parse_string(const char *str)
Parses a string and returns the parsed icalcomponent.
char * icalparser_string_line_generator(char *out, size_t buf_size, void *d)
icalparser * icalparser_new(void)
Creates a new icalparser.
Definition icalparser.c:90
icalcomponent * icalparser_clean(icalparser *parser)
Cleans out an icalparser and returns whatever it has parsed so far.
void icalparser_set_gen_data(icalparser *parser, void *data)
Sets the data that icalparser_parse will give to the line_gen_func as the parameter 'd'.
Definition icalparser.c:131
char * icalparser_get_line(icalparser *parser, icalparser_line_gen_func line_gen_func)
Given a line generator function, returns a single iCal content line.
Definition icalparser.c:439
icalparser_state icalparser_get_state(const icalparser *parser)
Returns current state of the icalparser.
Line-oriented parsing.
icalparser_ctrl
Defines how to handle invalid CONTROL characters in content lines.
Definition icalparser.h:315
@ ICALPARSER_CTRL_OMIT
Definition icalparser.h:319
@ ICALPARSER_CTRL_KEEP
Definition icalparser.h:317
icalparser_state
Represents the current state of the parser.
Definition icalparser.h:40
@ ICALPARSER_BEGIN_COMP
Definition icalparser.h:48
@ ICALPARSER_SUCCESS
Definition icalparser.h:45
@ ICALPARSER_END_COMP
Definition icalparser.h:51
@ ICALPARSER_IN_PROGRESS
Definition icalparser.h:54
@ ICALPARSER_ERROR
Definition icalparser.h:42
void icalproperty_free(icalproperty *p)
void icalproperty_set_value(icalproperty *p, icalvalue *value)
icalproperty_kind icalproperty_isa(const icalproperty *p)
bool icalproperty_get_allow_empty_properties(void)
void icalproperty_add_parameter(icalproperty *p, icalparameter *parameter)
icalproperty * icalproperty_clone(const icalproperty *old)
icalproperty * icalproperty_new(icalproperty_kind kind)
void icalproperty_set_iana_name(icalproperty *prop, const char *name)
void icalproperty_set_x_name(icalproperty *prop, const char *name)
icalproperty_kind icalproperty_string_to_kind(const char *string)
icalvalue_kind icalparameter_value_to_value_kind(icalparameter_value value)
icalvalue_kind icalproperty_kind_to_value_kind(icalproperty_kind kind)
const char * icalproperty_kind_to_string(icalproperty_kind kind)
ical_unknown_token_handling ical_get_unknown_token_handling_setting(void)
Definition icaltypes.c:188
ical_unknown_token_handling
Definition icaltypes.h:155
@ ICAL_DISCARD_TOKEN
Definition icaltypes.h:159
@ ICAL_ASSUME_IANA_TOKEN
Definition icaltypes.h:157
icalvalue * icalvalue_new_from_string(icalvalue_kind kind, const char *str)
Definition icalvalue.c:835
icalvalue * icalvalue_new(icalvalue_kind kind)
Definition icalvalue.c:61
Defines the data structure representing iCalendar parameter values.
const char * icalvalue_kind_to_string(const icalvalue_kind kind)
#define TMP_BUF_SIZE
Definition vcardvalue.c:30