Debian Codegraph @

src/json/json.c

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2007 by Rui Maciel *
3 * [email protected] *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "json.h"
22
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <memory.h>
29 #include <sys/types.h>
30
31
32 enum LEX_VALUE
33 { LEX_MORE = 0,
34 LEX_INVALID_CHARACTER,
35 LEX_TRUE,
36 LEX_FALSE,
37 LEX_NULL,
38 LEX_BEGIN_OBJECT,
39 LEX_END_OBJECT,
40 LEX_BEGIN_ARRAY,
41 LEX_END_ARRAY,
42 LEX_NAME_SEPARATOR,
43 LEX_VALUE_SEPARATOR,
44 LEX_STRING,
45 LEX_NUMBER,
46 LEX_ERROR,
47 LEX_MEMORY
48 };
49
50
51 /* rc_string part */
52
53 #define RSTRING_INCSTEP 5
54 #define RSTRING_DEFAULT 8
55
56
57 enum rui_string_error_codes
58 { RS_MEMORY, RS_OK = 1, RS_UNKNOWN };
59
60 typedef enum rui_string_error_codes rstring_code;
61
62
63 rcstring *
64 rcs_create (size_t length)
65 {
66 rcstring *rcs;
67 rcs = malloc (sizeof (rcstring)); /* allocates memory for a struct rcstring */
68 if (rcs == NULL)
69 return NULL;
70
71 rcs->max = length;
72 rcs->length = 0;
73
74 rcs->text = malloc ((rcs->max + 1) * sizeof (char));
75 if (rcs->text == NULL)
76 {
77 free (rcs);
78 return NULL;
79 }
80 rcs->text[0] = '\0';
81
82 return rcs;
83 }
84
85
86 void
87 rcs_free (rcstring ** rcs)
88 {
89 assert (rcs != NULL);
90 if (*rcs != NULL)
91 {
92 if ((*rcs)->text != NULL)
93 {
94 free ((*rcs)->text);
95 (*rcs)->text = NULL;
96 }
97 free (*rcs);
98 *rcs = NULL;
99 }
100
101 }
102
103
104 rstring_code
105 rcs_resize (rcstring * rcs, size_t length)
106 {
107 char *temp;
108 assert (rcs != NULL);
109
110 temp = realloc (rcs->text, sizeof (char) * (length + 1)); /* length plus '\0' */
111 if (temp == NULL)
112 {
113 free (rcs);
114 return RS_MEMORY;
115 }
116 rcs->text = temp;
117 rcs->max = length;
118 rcs->text[rcs->max] = '\0';
119 return RS_OK;
120 }
121
122
123 rstring_code
124 rcs_catcs (rcstring * pre, const char *pos, const size_t length)
125 {
126 assert (pre != NULL);
127 assert (pos != NULL);
128
129 if (pre->max < pre->length + length)
130 {
131 if (rcs_resize (pre, pre->length + length + RSTRING_INCSTEP) != RS_OK)
132 return RS_MEMORY;
133 }
134 strncpy (pre->text + pre->length, pos, length);
135 pre->text[pre->length + length] = '\0';
136 pre->length += length;
137 return RS_OK;
138 }
139
140
141 rstring_code
142 rcs_catc (rcstring * pre, const char c)
143 {
144 assert (pre != NULL);
145
146 if (pre->max <= pre->length)
147 {
148 if (rcs_resize (pre, pre->max + RSTRING_INCSTEP) != RS_OK)
149 return RS_MEMORY;
150 }
151 pre->text[pre->length] = c;
152 pre->length++;
153 pre->text[pre->length] = '\0';
154 return RS_OK;
155 }
156
157
158 char *
159 rcs_unwrap (rcstring * rcs)
160 {
161 char *out;
162 assert (rcs != NULL);
163
164 if (rcs->text == NULL)
165 out = NULL;
166 else
167 {
168 out = realloc (rcs->text, sizeof (char) * (strlen (rcs->text) + 1));
169 }
170
171 free (rcs);
172 return out;
173 }
174
175
176
177 size_t
178 rcs_length (rcstring * rcs)
179 {
180 /*TODO account for UTF8 */
181 assert (rcs != NULL);
182 return rcs->length;
183 }
184
185
186 /* end of rc_string part */
187
188
189 enum json_error
190 json_stream_parse (FILE * file, json_t ** document)
191 {
192 char buffer[1024]; /* hard-coded value */
193 unsigned int error = JSON_INCOMPLETE_DOCUMENT;
194
195 struct json_parsing_info state;
196
197 assert (file != NULL); /* must be an open stream */
198 assert (document != NULL); /* must be a valid pointer reference */
199 assert (*document == NULL); /* only accepts a null json_t pointer, to avoid memory leaks */
200
201 json_jpi_init (&state); /* initializes the json_parsing_info object */
202
203 while ((error == JSON_WAITING_FOR_EOF) || (error == JSON_INCOMPLETE_DOCUMENT))
204 {
205 if (fgets (buffer, 1024, file) != NULL)
206 {
207 switch (error = json_parse_fragment (&state, buffer))
208 {
209 case JSON_OK:
210 case JSON_WAITING_FOR_EOF:
211 case JSON_INCOMPLETE_DOCUMENT:
212 break;
213
214 default:
215 json_free_value (&state.cursor);
216 return error;
217 break;
218 }
219 }
220 else
221 {
222 if (error == JSON_WAITING_FOR_EOF)
223 error = JSON_OK;
224 else
225 {
226 /*TODO refine this error code */
227 error = JSON_UNKNOWN_PROBLEM;
228 }
229 }
230 }
231
232 if (error == JSON_OK)
233 {
234 *document = state.cursor;
235 }
236
237 return error;
238 }
239
240
241 json_t *
242 json_new_value (const enum json_value_type type)
243 {
244 json_t *new_object;
245 /* allocate memory to the new object */
246 new_object = malloc (sizeof (json_t));
247 if (new_object == NULL)
248 return NULL;
249
250 /* initialize members */
251 new_object->text = NULL;
252 new_object->parent = NULL;
253 new_object->child = NULL;
254 new_object->child_end = NULL;
255 new_object->previous = NULL;
256 new_object->next = NULL;
257 new_object->type = type;
258 return new_object;
259 }
260
261
262 json_t *
263 json_new_string (const char *text)
264 {
265 json_t *new_object;
266 size_t length;
267
268 if (text == NULL)
269 {
270 text = "";
271 }
272
273 /* allocate memory for the new object */
274 new_object = malloc (sizeof (json_t));
275 if (new_object == NULL)
276 return NULL;
277
278 /* initialize members */
279 length = strlen (text) + 1;
280 new_object->text = malloc (length * sizeof (char));
281 if (new_object->text == NULL)
282 {
283 free (new_object);
284 return NULL;
285 }
286 strncpy (new_object->text, text, length);
287 new_object->parent = NULL;
288 new_object->child = NULL;
289 new_object->child_end = NULL;
290 new_object->previous = NULL;
291 new_object->next = NULL;
292 new_object->type = JSON_STRING;
293 return new_object;
294 }
295
296
297 json_t *
298 json_new_number (const char *text)
299 {
300 json_t *new_object;
301 size_t length;
302
303 assert (text != NULL);
304
305 /* allocate memory for the new object */
306 new_object = malloc (sizeof (json_t));
307 if (new_object == NULL)
308 return NULL;
309
310 /* initialize members */
311 length = strlen (text) + 1;
312 new_object->text = malloc (length * sizeof (char));
313 if (new_object->text == NULL)
314 {
315 free (new_object);
316 return NULL;
317 }
318 strncpy (new_object->text, text, length);
319 new_object->parent = NULL;
320 new_object->child = NULL;
321 new_object->child_end = NULL;
322 new_object->previous = NULL;
323 new_object->next = NULL;
324 new_object->type = JSON_NUMBER;
325 return new_object;
326 }
327
328
329 json_t *
330 json_new_object (void)
331 {
332 return json_new_value (JSON_OBJECT);
333 }
334
335
336 json_t *
337 json_new_array (void)
338 {
339 return json_new_value (JSON_ARRAY);
340 }
341
342
343 json_t *
344 json_new_null (void)
345 {
346 return json_new_value (JSON_NULL);
347 }
348
349
350 json_t *
351 json_new_true (void)
352 {
353 return json_new_value (JSON_TRUE);
354 }
355
356
357 json_t *
358 json_new_false (void)
359 {
360 return json_new_value (JSON_FALSE);
361 }
362
363
364 json_t *
365 json_new_bool (int value)
366 {
367 return !!value ? json_new_true() : json_new_false();
368 }
369
370
371 static void
372 intern_json_free_value (json_t ** value)
373 {
374 assert (value != NULL);
375 assert ((*value) != NULL);
376 assert ((*value)->child == NULL);
377
378 /* fixing sibling linked list connections */
379 if ((*value)->previous && (*value)->next)
380 {
381 (*value)->previous->next = (*value)->next;
382 (*value)->next->previous = (*value)->previous;
383 }
384 else
385 {
386 if ((*value)->previous)
387 {
388 (*value)->previous->next = NULL;
389 }
390 if ((*value)->next)
391 {
392 (*value)->next->previous = NULL;
393 }
394 }
395
396 /*fixing parent node connections */
397 if ((*value)->parent)
398 {
399 /* fix the tree connection to the first node in the children's list */
400 if ((*value)->parent->child == (*value))
401 {
402 if ((*value)->next)
403 {
404 (*value)->parent->child = (*value)->next; /* the parent node always points to the first node in the children linked list */
405 }
406 else
407 {
408 (*value)->parent->child = NULL;
409 }
410 }
411
412 /* fix the tree connection to the last node in the children's list */
413 if ((*value)->parent->child_end == (*value))
414 {
415 if ((*value)->previous)
416 {
417 (*value)->parent->child_end = (*value)->previous; /* the parent node always points to the last node in the children linked list */
418 }
419 else
420 {
421 (*value)->parent->child_end = NULL;
422 }
423 }
424 }
425
426 /*finally, freeing the memory allocated for this value */
427 if ((*value)->text != NULL)
428 {
429 free ((*value)->text);
430 }
431 free (*value); /* the json value */
432 (*value) = NULL;
433 }
434
435
436 void
437 json_free_value (json_t ** value)
438 {
439 json_t *cursor = *value;
440
441 if (value == NULL || *value == NULL)
442 {
443 return;
444 }
445
446 while (*value)
447 {
448 json_t *parent;
449
450 if (cursor->child)
451 {
452 cursor = cursor->child;
453 continue;
454 }
455
456 if (cursor == *value)
457 {
458 *value = NULL;
459 }
460
461 parent = cursor->parent;
462 intern_json_free_value (&cursor);
463 cursor = parent;
464 }
465 }
466
467
468 enum json_error
469 json_insert_child (json_t * parent, json_t * child)
470 {
471 /*TODO change the child list from FIFO to LIFO, in order to get rid of the child_end pointer */
472 assert (parent != NULL); /* the parent must exist */
473 assert (child != NULL); /* the child must exist */
474 assert (parent != child); /* parent and child must not be the same. if they are, it will enter an infinite loop */
475
476 /* enforce tree structure correctness */
477 switch (parent->type)
478 {
479 case JSON_STRING:
480 /* a string accepts every JSON type as a child value */
481 /* therefore, the sanity check must be performed on the child node */
482 switch (child->type)
483 {
484 case JSON_STRING:
485 case JSON_NUMBER:
486 case JSON_TRUE:
487 case JSON_FALSE:
488 case JSON_NULL:
489 if (child->child != NULL)
490 return JSON_BAD_TREE_STRUCTURE;
491 break;
492
493 case JSON_OBJECT:
494 case JSON_ARRAY:
495 break;
496
497 default:
498 return JSON_BAD_TREE_STRUCTURE; /* this part should never be reached */
499 break;
500 }
501 break;
502
503 case JSON_OBJECT: /* JSON objects may only accept JSON string objects which already have child nodes of their own */
504 if (child->type != JSON_STRING)
505 return JSON_BAD_TREE_STRUCTURE;
506 break;
507
508 case JSON_ARRAY:
509 switch (child->type)
510 {
511 case JSON_STRING:
512 case JSON_TRUE:
513 case JSON_FALSE:
514 case JSON_NULL:
515 case JSON_NUMBER:
516 if (child->child)
517 return JSON_BAD_TREE_STRUCTURE;
518 break;
519
520 case JSON_OBJECT:
521 case JSON_ARRAY:
522 break;
523
524 default:
525 return JSON_BAD_TREE_STRUCTURE;
526 }
527 break;
528
529 default:
530 return JSON_BAD_TREE_STRUCTURE;
531 }
532
533 child->parent = parent;
534 if (parent->child)
535 {
536 child->previous = parent->child_end;
537 parent->child_end->next = child;
538 parent->child_end = child;
539 }
540 else
541 {
542 parent->child = child;
543 parent->child_end = child;
544 }
545
546 return JSON_OK;
547 }
548
549
550 enum json_error
551 json_insert_pair_into_object (json_t * parent, const char *text_label, json_t * value)
552 {
553 enum json_error error;
554 json_t *label;
555
556 /* verify if the parameters are valid */
557 assert (parent != NULL);
558 assert (text_label != NULL);
559 assert (value != NULL);
560 assert (parent != value);
561
562 /* enforce type coherence */
563 assert (parent->type == JSON_OBJECT);
564
565
566 /* create label json_value */
567 label = json_new_string (text_label);
568 if (label == NULL)
569 return JSON_MEMORY;
570
571 /*insert value and check for error */
572 error = json_insert_child (label, value);
573 if (error != JSON_OK)
574 return error;
575 /*insert value and check for error */
576 error = json_insert_child (parent, label);
577 if (error != JSON_OK)
578 return error;
579
580 return JSON_OK;
581 }
582
583
584 enum json_error
585 json_tree_to_string (json_t * root, char **text)
586 {
587 json_t *cursor;
588 rcstring *output;
589
590 assert (root != NULL);
591 assert (text != NULL);
592
593 cursor = root;
594 /* set up the output and temporary rwstrings */
595 output = rcs_create (RSTRING_DEFAULT);
596
597 /* start the convoluted fun */
598 state1: /* open value */
599 {
600 if ((cursor->previous) && (cursor != root)) /*if cursor is children and not root than it is a followup sibling */
601 {
602 /* append comma */
603 if (rcs_catc (output, ',') != RS_OK)
604 {
605 return JSON_MEMORY;
606 }
607 }
608 switch (cursor->type)
609 {
610 case JSON_STRING:
611 /* append the "text"\0, which means 1 + wcslen(cursor->text) + 1 + 1 */
612 /* set the new output size */
613 if (rcs_catc (output, '\"') != RS_OK)
614 {
615 return JSON_MEMORY;
616 }
617 if (rcs_catcs (output, cursor->text, strlen (cursor->text)) != RS_OK)
618 {
619 return JSON_MEMORY;
620 }
621 if (rcs_catc (output, '\"') != RS_OK)
622 {
623 return JSON_MEMORY;
624 }
625
626 if (cursor->parent != NULL)
627 {
628 if (cursor->parent->type == JSON_OBJECT) /* cursor is label in label:value pair */
629 {
630 /* error checking: if parent is object and cursor is string then cursor must have a single child */
631 if (cursor->child != NULL)
632 {
633 if (rcs_catc (output, ':') != RS_OK)
634 {
635 return JSON_MEMORY;
636 }
637 }
638 else
639 {
640 /* malformed document tree: label without value in label:value pair */
641 rcs_free (&output);
642 text = NULL;
643 return JSON_BAD_TREE_STRUCTURE;
644 }
645 }
646 }
647 else /* does not have a parent */
648 {
649 if (cursor->child != NULL) /* is root label in label:value pair */
650 {
651 if (rcs_catc (output, ':') != RS_OK)
652 {
653 return JSON_MEMORY;
654 }
655 }
656 else
657 {
658 /* malformed document tree: label without value in label:value pair */
659 rcs_free (&output);
660 text = NULL;
661 return JSON_BAD_TREE_STRUCTURE;
662 }
663 }
664 break;
665
666 case JSON_NUMBER:
667 /* must not have any children */
668 /* set the new size */
669 if (rcs_catcs (output, cursor->text, strlen (cursor->text)) != RS_OK)
670 {
671 return JSON_MEMORY;
672 }
673 goto state2; /* close value */
674 break;
675
676 case JSON_OBJECT:
677 if (rcs_catc (output, '{') != RS_OK)
678 {
679 return JSON_MEMORY;
680 }
681
682 if (cursor->child)
683 {
684 cursor = cursor->child;
685 goto state1; /* open value */
686 }
687 else
688 {
689 goto state2; /* close value */
690 }
691 break;
692
693 case JSON_ARRAY:
694 if (rcs_catc (output, '[') != RS_OK)
695 {
696 return JSON_MEMORY;
697 }
698
699 if (cursor->child != NULL)
700 {
701 cursor = cursor->child;
702 goto state1;
703 }
704 else
705 {
706 goto state2; /* close value */
707 }
708 break;
709
710 case JSON_TRUE:
711 /* must not have any children */
712 if (rcs_catcs (output, "true", 4) != RS_OK)
713 {
714 return JSON_MEMORY;
715 }
716 goto state2; /* close value */
717 break;
718
719 case JSON_FALSE:
720 /* must not have any children */
721 if (rcs_catcs (output, "false", 5) != RS_OK)
722 {
723 return JSON_MEMORY;
724 }
725 goto state2; /* close value */
726 break;
727
728 case JSON_NULL:
729 /* must not have any children */
730 if (rcs_catcs (output, "null", 4) != RS_OK)
731 {
732 return JSON_MEMORY;
733 }
734 goto state2; /* close value */
735 break;
736
737 default:
738 goto error;
739 }
740 if (cursor->child)
741 {
742 cursor = cursor->child;
743 goto state1; /* open value */
744 }
745 else
746 {
747 /* does not have any children */
748 goto state2; /* close value */
749 }
750 }
751
752 state2: /* close value */
753 {
754 switch (cursor->type)
755 {
756 case JSON_OBJECT:
757 if (rcs_catc (output, '}') != RS_OK)
758 {
759 return JSON_MEMORY;
760 }
761 break;
762
763 case JSON_ARRAY:
764 if (rcs_catc (output, ']') != RS_OK)
765 {
766 return JSON_MEMORY;
767 }
768 break;
769
770 case JSON_STRING:
771 break;
772 case JSON_NUMBER:
773 break;
774 case JSON_TRUE:
775 break;
776 case JSON_FALSE:
777 break;
778 case JSON_NULL:
779 break;
780 default:
781 goto error;
782 }
783 if ((cursor->parent == NULL) || (cursor == root))
784 {
785 goto end;
786 }
787 else if (cursor->next)
788 {
789 cursor = cursor->next;
790 goto state1; /* open value */
791 }
792 else
793 {
794 cursor = cursor->parent;
795 goto state2; /* close value */
796 }
797 }
798
799 error:
800 {
801 rcs_free (&output);
802 return JSON_UNKNOWN_PROBLEM;
803 }
804
805 end:
806 {
807 *text = rcs_unwrap (output);
808 return JSON_OK;
809 }
810 }
811
812
813 enum json_error
814 json_stream_output (FILE * file, json_t * root)
815 {
816 json_t *cursor;
817
818 assert (root != NULL);
819 assert (file != NULL); /* the file stream must be opened */
820
821 cursor = root;
822 /* set up the output and temporary rwstrings */
823
824 /* start the convoluted fun */
825 state1: /* open value */
826 {
827 if ((cursor->previous) && (cursor != root)) /*if cursor is children and not root than it is a followup sibling */
828 {
829 /* append comma */
830 fprintf (file, ",");
831 }
832 switch (cursor->type)
833 {
834 case JSON_STRING:
835 /* append the "text"\0, which means 1 + wcslen(cursor->text) + 1 + 1 */
836 /* set the new output size */
837 fprintf (file, "\"%s\"", cursor->text);
838
839 if (cursor->parent != NULL)
840 {
841 if (cursor->parent->type == JSON_OBJECT) /* cursor is label in label:value pair */
842 {
843 /* error checking: if parent is object and cursor is string then cursor must have a single child */
844 if (cursor->child != NULL)
845 {
846 if (fprintf (file, ":") != RS_OK)
847 {
848 return JSON_MEMORY;
849 }
850 }
851 else
852 {
853 /* malformed document tree: label without value in label:value pair */
854 return JSON_BAD_TREE_STRUCTURE;
855 }
856 }
857 }
858 else /* does not have a parent */
859 {
860 if (cursor->child != NULL) /* is root label in label:value pair */
861 {
862 fprintf (file, ":");
863 }
864 else
865 {
866 /* malformed document tree: label without value in label:value pair */
867 return JSON_BAD_TREE_STRUCTURE;
868 }
869 }
870 break;
871
872 case JSON_NUMBER:
873 /* must not have any children */
874 /* set the new size */
875 fprintf (file, "%s", cursor->text);
876 goto state2; /* close value */
877 break;
878
879 case JSON_OBJECT:
880 fprintf (file, "{");
881
882 if (cursor->child)
883 {
884 cursor = cursor->child;
885 goto state1; /* open value */
886 }
887 else
888 {
889 goto state2; /* close value */
890 }
891 break;
892
893 case JSON_ARRAY:
894 fprintf (file, "[");
895
896 if (cursor->child != NULL)
897 {
898 cursor = cursor->child;
899 goto state1;
900 }
901 else
902 {
903 goto state2; /* close value */
904 }
905 break;
906
907 case JSON_TRUE:
908 /* must not have any children */
909 fprintf (file, "true");
910 goto state2; /* close value */
911 break;
912
913 case JSON_FALSE:
914 /* must not have any children */
915 fprintf (file, "false");
916 goto state2; /* close value */
917 break;
918
919 case JSON_NULL:
920 /* must not have any children */
921 fprintf (file, "null");
922 goto state2; /* close value */
923 break;
924
925 default:
926 goto error;
927 }
928 if (cursor->child)
929 {
930 cursor = cursor->child;
931 goto state1; /* open value */
932 }
933 else
934 {
935 /* does not have any children */
936 goto state2; /* close value */
937 }
938 }
939
940 state2: /* close value */
941 {
942 switch (cursor->type)
943 {
944 case JSON_OBJECT:
945 fprintf (file, "}");
946 break;
947
948 case JSON_ARRAY:
949 fprintf (file, "]");
950 break;
951
952 case JSON_STRING:
953 break;
954 case JSON_NUMBER:
955 break;
956 case JSON_TRUE:
957 break;
958 case JSON_FALSE:
959 break;
960 case JSON_NULL:
961 break;
962 default:
963 goto error;
964 }
965 if ((cursor->parent == NULL) || (cursor == root))
966 {
967 goto end;
968 }
969 else if (cursor->next)
970 {
971 cursor = cursor->next;
972 goto state1; /* open value */
973 }
974 else
975 {
976 cursor = cursor->parent;
977 goto state2; /* close value */
978 }
979 }
980
981 error:
982 {
983 return JSON_UNKNOWN_PROBLEM;
984 }
985
986 end:
987 {
988 fprintf (file, "\n");
989 return JSON_OK;
990 }
991 }
992
993
994 void
995 json_strip_white_spaces (char *text)
996 {
997 size_t in, out, length;
998 int state;
999
1000 assert (text != NULL);
1001
1002 in = 0;
1003 out = 0;
1004 length = strlen (text);
1005 state = 0; /* possible states: 0 -> document, 1 -> inside a string */
1006
1007 while (in < length)
1008 {
1009 switch (text[in])
1010 {
1011 case '\x20': /* space */
1012 case '\x09': /* horizontal tab */
1013 case '\x0A': /* line feed or new line */
1014 case '\x0D': /* Carriage return */
1015 if (state == 1)
1016 {
1017 text[out++] = text[in];
1018 }
1019 break;
1020
1021 case '\"':
1022 switch (state)
1023 {
1024 case 0: /* not inside a JSON string */
1025 state = 1;
1026 break;
1027
1028 case 1: /* inside a JSON string */
1029 if (text[in - 1] != '\\')
1030 {
1031 state = 0;
1032 }
1033 break;
1034
1035 default:
1036 assert (0);
1037 }
1038 text[out++] = text[in];
1039 break;
1040
1041 default:
1042 text[out++] = text[in];
1043 }
1044 ++in;
1045 }
1046 text[out] = '\0';
1047 }
1048
1049
1050 char *
1051 json_format_string (const char *text)
1052 {
1053 size_t pos = 0, text_length;
1054 unsigned int indentation = 0; /* the current indentation level */
1055 unsigned int i; /* loop iterator variable */
1056 char loop;
1057
1058 rcstring *output;
1059 text_length = strlen (text);
1060
1061 output = rcs_create (text_length);
1062 bool in_small_array = false;
1063 while (pos < text_length)
1064 {
1065 switch (text[pos])
1066 {
1067 case '\x20':
1068 case '\x09':
1069 case '\x0A':
1070 case '\x0D': /* JSON insignificant white spaces */
1071 pos++;
1072 break;
1073
1074 case '[':
1075 {
1076 int num_elements = 0;
1077 // Read ahead to see if this is a big array
1078 for (size_t pos_i = pos; pos_i < text_length && num_elements <= 2; pos_i++)
1079 {
1080 if (text[pos_i] == ']')
1081 {
1082 num_elements++;
1083 break;
1084 }
1085 if (text[pos_i] == ',')
1086 {
1087 num_elements++;
1088 }
1089 if (text[pos_i] == '{')
1090 {
1091 num_elements = 999;
1092 break;
1093 }
1094 }
1095 in_small_array = num_elements <= 2;
1096 }
1097 rcs_catc(output, text[pos]);
1098 pos++;
1099 break;
1100
1101 case ']':
1102 in_small_array = false;
1103 rcs_catc(output, text[pos]);
1104 pos++;
1105 break;
1106
1107 case '{':
1108 in_small_array = false;
1109 indentation++;
1110 rcs_catcs (output, "{\n", 2);
1111 for (i = 0; i < indentation; i++)
1112 {
1113 rcs_catc (output, '\t');
1114 }
1115 pos++;
1116 break;
1117
1118 case '}':
1119 indentation--;
1120 rcs_catc (output, '\n');
1121 for (i = 0; i < indentation; i++)
1122 {
1123 rcs_catc (output, '\t');
1124 }
1125 rcs_catc (output, '}');
1126 pos++;
1127 break;
1128
1129 case ':':
1130 rcs_catcs (output, ": ", 2);
1131 pos++;
1132 break;
1133
1134 case ',':
1135 if (in_small_array)
1136 {
1137 rcs_catcs(output, ", ", 2);
1138 }
1139 else
1140 {
1141 rcs_catcs(output, ",\n", 2);
1142 for (i = 0; i < indentation; i++)
1143 {
1144 rcs_catc(output, '\t');
1145 }
1146 }
1147 pos++;
1148 break;
1149
1150 case '\"': /* open string */
1151 rcs_catc (output, text[pos]);
1152 pos++;
1153 loop = 1; /* inner string loop trigger is enabled */
1154 while (loop)
1155 {
1156 if (text[pos] == '\\') /* escaped sequence */
1157 {
1158 rcs_catc (output, '\\');
1159 pos++;
1160 if (text[pos] == '\"') /* don't consider a \" escaped sequence as an end of string */
1161 {
1162 rcs_catc (output, '\"');
1163 pos++;
1164 continue;
1165 }
1166 }
1167 else if (text[pos] == '\"') /* reached end of string */
1168 {
1169 loop = 0;
1170 }
1171
1172 rcs_catc (output, text[pos]);
1173
1174 pos++;
1175 if (pos >= text_length)
1176 {
1177 loop = 0;
1178 }
1179 }
1180 break;
1181
1182 default:
1183 rcs_catc (output, text[pos]);
1184 pos++;
1185 break;
1186 }
1187 }
1188
1189 return rcs_unwrap (output);
1190 }
1191
1192
1193 char *
1194 json_escape (const char *text)
1195 {
1196 rcstring *output;
1197 size_t i, length;
1198 char buffer[7];
1199 /* check if pre-conditions are met */
1200 assert (text != NULL);
1201
1202 /* defining the temporary variables */
1203 length = strlen (text);
1204 output = rcs_create (length);
1205 if (output == NULL)
1206 return NULL;
1207 for (i = 0; i < length; i++)
1208 {
1209 if (text[i] == '\\')
1210 {
1211 rcs_catcs (output, "\\\\", 2);
1212 }
1213 else if (text[i] == '\"')
1214 {
1215 rcs_catcs (output, "\\\"", 2);
1216 }
1217 else if (text[i] == '/')
1218 {
1219 rcs_catcs (output, "\\/", 2);
1220 }
1221 else if (text[i] == '\b')
1222 {
1223 rcs_catcs (output, "\\b", 2);
1224 }
1225 else if (text[i] == '\f')
1226 {
1227 rcs_catcs (output, "\\f", 2);
1228 }
1229 else if (text[i] == '\n')
1230 {
1231 rcs_catcs (output, "\\n", 2);
1232 }
1233 else if (text[i] == '\r')
1234 {
1235 rcs_catcs (output, "\\r", 2);
1236 }
1237 else if (text[i] == '\t')
1238 {
1239 rcs_catcs (output, "\\t", 2);
1240 }
1241 else if (text[i] < 0) /* non-BMP character */
1242 {
1243 rcs_catc (output, text[i]);
1244 }
1245 else if (text[i] < 0x20)
1246 {
1247 sprintf (buffer, "\\u%4.4x", text[i]);
1248 rcs_catcs (output, buffer, 6);
1249 }
1250 else
1251 {
1252 rcs_catc (output, text[i]);
1253 }
1254 }
1255 return rcs_unwrap (output);
1256 }
1257
1258
1259 char *
1260 json_unescape (const char *text)
1261 {
1262 char *result = malloc (strlen (text) + 1);
1263 size_t r; /* read cursor */
1264 size_t w; /* write cursor */
1265
1266 assert (text);
1267
1268 for (r = w = 0; text[r]; r++)
1269 {
1270 switch (text[r])
1271 {
1272 case '\\':
1273 switch (text[++r])
1274 {
1275 case '\"':
1276 case '\\':
1277 case '/':
1278 /* literal translation */
1279 result[w++] = text[r];
1280 break;
1281 case 'b':
1282 result[w++] = '\b';
1283 break;
1284 case 'f':
1285 result[w++] = '\f';
1286 break;
1287 case 'n':
1288 result[w++] = '\n';
1289 break;
1290 case 'r':
1291 result[w++] = '\r';
1292 break;
1293 case 't':
1294 result[w++] = '\t';
1295 break;
1296 case 'u':
1297 {
1298 char buf[5];
1299 int64_t unicode;
1300
1301 buf[0] = text[++r];
1302 buf[1] = text[++r];
1303 buf[2] = text[++r];
1304 buf[3] = text[++r];
1305 buf[4] = '\0';
1306
1307 unicode = strtol (buf, NULL, 16);
1308
1309 if (unicode < 0x80)
1310 {
1311 /* ASCII: map to UTF-8 literally */
1312 result[w++] = (char) unicode;
1313 }
1314 else if (unicode < 0x800)
1315 {
1316 /* two-byte-encoding */
1317 char one = 0xC0; /* 110 00000 */
1318 char two = 0x80; /* 10 000000 */
1319
1320 two += (unicode & 0x3F);
1321 unicode >>= 6;
1322 one += (unicode & 0x1F);
1323
1324 result[w++] = one;
1325 result[w++] = two;
1326 }
1327 else if (unicode < 0x10000)
1328 {
1329 if (unicode < 0xD800 || 0xDBFF < unicode)
1330 {
1331 /* three-byte-encoding */
1332 char one = 0xE0; /* 1110 0000 */
1333 char two = 0x80; /* 10 000000 */
1334 char three = 0x80; /* 10 000000 */
1335
1336 three += (unicode & 0x3F);
1337 unicode >>= 6;
1338 two += (unicode & 0x3F);
1339 unicode >>= 6;
1340 one += (unicode & 0xF);
1341
1342 result[w++] = one;
1343 result[w++] = two;
1344 result[w++] = three;
1345 }
1346 else
1347 {
1348 /* unicode is a UTF-16 high surrogate, continue with the low surrogate */
1349 uint64_t high_surrogate = unicode; /* 110110 00;00000000 */
1350 uint64_t low_surrogate;
1351 char one = 0xF0; /* 11110 000 */
1352 char two = 0x80; /* 10 000000 */
1353 char three = 0x80; /* 10 000000 */
1354 char four = 0x80; /* 10 000000 */
1355
1356 if (text[++r] != '\\')
1357 {
1358 break;
1359 }
1360 if (text[++r] != 'u')
1361 {
1362 break;
1363 }
1364
1365 buf[0] = text[++r];
1366 buf[1] = text[++r];
1367 buf[2] = text[++r];
1368 buf[3] = text[++r];
1369
1370 low_surrogate = strtol (buf, NULL, 16); /* 110111 00;00000000 */
1371
1372 /* strip surrogate markers */
1373 high_surrogate -= 0xD800; /* 11011000;00000000 */
1374 low_surrogate -= 0xDC00; /* 11011100;00000000 */
1375
1376 unicode = (high_surrogate << 10) + (low_surrogate) + 0x10000;
1377
1378 /* now encode into four-byte UTF-8 (as we are larger than 0x10000) */
1379 four += (unicode & 0x3F);
1380 unicode >>= 6;
1381 three += (unicode & 0x3F);
1382 unicode >>= 6;
1383 two += (unicode & 0x3F);
1384 unicode >>= 6;
1385 one += (unicode & 0x7);
1386
1387 result[w++] = one;
1388 result[w++] = two;
1389 result[w++] = three;
1390 result[w++] = four;
1391 }
1392 }
1393 else
1394 {
1395 fprintf (stderr, "JSON: unsupported unicode value: 0x%lX\n", (long unsigned int)unicode);
1396 }
1397 }
1398 break;
1399 default:
1400 assert (0);
1401 break;
1402 }
1403 break;
1404 default:
1405 result[w++] = text[r];
1406 break;
1407 }
1408 }
1409 result[w] = '\0';
1410
1411 return result;
1412 }
1413
1414
1415 void
1416 json_jpi_init (struct json_parsing_info *jpi)
1417 {
1418 assert (jpi != NULL);
1419 jpi->state = 0;
1420 jpi->lex_state = 0;
1421 jpi->lex_text = NULL;
1422 jpi->p = NULL;
1423 jpi->cursor = NULL;
1424 jpi->line = 1;
1425 jpi->string_length_limit_reached = 0;
1426 }
1427
1428
1429 int
1430 lexer (const char *buffer, const char **p, unsigned int *state, rcstring ** text, size_t *line)
1431 {
1432 assert (buffer != NULL);
1433 assert (p != NULL);
1434 assert (state != NULL);
1435 assert (text != NULL);
1436 assert (line != NULL);
1437 if (*p == NULL)
1438 *p = buffer;
1439
1440 while (**p != '\0')
1441 {
1442 switch (*state)
1443 {
1444
1445 case 0: /* Root document */
1446 {
1447 switch (*(*p)++)
1448 {
1449 case '\x20': /* space */
1450 case '\x09': /* horizontal tab */
1451 break;
1452
1453 case '\x0A': /* line feed or new line */
1454 case '\x0D': /* Carriage return */
1455 *line += 1; // increment line number
1456 break;
1457
1458 case '{':
1459 return LEX_BEGIN_OBJECT;
1460 case '}':
1461 return LEX_END_OBJECT;
1462 case '[':
1463 return LEX_BEGIN_ARRAY;
1464 case ']':
1465 return LEX_END_ARRAY;
1466 case ':':
1467 return LEX_NAME_SEPARATOR;
1468 case ',':
1469 return LEX_VALUE_SEPARATOR;
1470
1471 case '\"':
1472 *text = rcs_create (RSTRING_DEFAULT);
1473 if (*text == NULL)
1474 return LEX_MEMORY;
1475 *state = 1; /* inside a JSON string */
1476 break;
1477
1478 case 't':
1479 *state = 7; /* true: 1 */
1480 break;
1481
1482 case 'f':
1483 *state = 10; /* false: 1 */
1484 break;
1485
1486 case 'n':
1487 *state = 14; /* false: 1 */
1488 break;
1489
1490 case '-':
1491 *text = rcs_create (RSTRING_DEFAULT);
1492 if (*text == NULL)
1493 return LEX_MEMORY;
1494 if (rcs_catc (*text, '-') != RS_OK)
1495 return LEX_MEMORY;
1496 *state = 17; /* number: '0' */
1497 break;
1498
1499 case '0':
1500 *text = rcs_create (RSTRING_DEFAULT);
1501 if (*text == NULL)
1502 return LEX_MEMORY;
1503 if (rcs_catc (*text, '0') != RS_OK)
1504 return LEX_MEMORY;
1505 *state = 18; /* number: '0' */
1506 break;
1507
1508 case '1':
1509 case '2':
1510 case '3':
1511 case '4':
1512 case '5':
1513 case '6':
1514 case '7':
1515 case '8':
1516 case '9':
1517 *text = rcs_create (RSTRING_DEFAULT);
1518 if (*text == NULL)
1519 return LEX_MEMORY;
1520 if (rcs_catc (*text, *(*p - 1)) != RS_OK)
1521 return LEX_MEMORY;
1522 *state = 19; /* number: decimal followup */
1523 break;
1524
1525
1526 default:
1527 return LEX_INVALID_CHARACTER;
1528 }
1529 }
1530 break;
1531
1532 case 1: /* inside a JSON string */
1533 {
1534 assert (*text != NULL);
1535 switch (**p)
1536 {
1537 case 1:
1538 case 2:
1539 case 3:
1540 case 4:
1541 case 5:
1542 case 6:
1543 case 7:
1544 case 8:
1545 case 9:
1546 case 10: /* line feed */
1547 case 11:
1548 case 12:
1549 case 13: /* carriage return */
1550 case 14:
1551 case 15:
1552 case 16:
1553 case 17:
1554 case 18:
1555 case 19:
1556 case 20:
1557 case 21:
1558 case 22:
1559 case 23:
1560 case 24:
1561 case 25:
1562 case 26:
1563 case 27:
1564 case 28:
1565 case 29:
1566 case 30:
1567 case 31:
1568 /* ASCII control characters can only be present in a JSON string if they are escaped. If not then the document is invalid */
1569 return LEX_INVALID_CHARACTER;
1570 break;
1571
1572 case '\"': /* close JSON string */
1573 /* it is expected that, in the routine that calls this function, text is set to NULL */
1574 *state = 0;
1575 ++*p;
1576 return LEX_STRING;
1577 break;
1578
1579 case '\\':
1580 if (rcs_catc (*text, '\\') != RS_OK)
1581 return LEX_MEMORY;
1582 *state = 2; /* inside a JSON string: start escape sequence */
1583 break;
1584
1585 default:
1586 if (rcs_catc (*text, **p) != RS_OK)
1587 return LEX_MEMORY;
1588 }
1589 ++*p;
1590 }
1591 break;
1592
1593 case 2: /* inside a JSON string: start escape sequence */
1594 {
1595 assert (*text != NULL);
1596 switch (**p)
1597 {
1598 case '\\':
1599 case '\"':
1600 case '/':
1601 case 'b':
1602 case 'f':
1603 case 'n':
1604 case 'r':
1605 case 't':
1606 if (rcs_catc (*text, **p) != RS_OK)
1607 return LEX_MEMORY;
1608 *state = 1; /* inside a JSON string */
1609 break;
1610
1611 case 'u':
1612 if (rcs_catc (*text, **p) != RS_OK)
1613 return LEX_MEMORY;
1614 *state = 3; /* inside a JSON string: escape unicode */
1615 break;
1616
1617 default:
1618 return LEX_INVALID_CHARACTER;
1619 }
1620 ++*p;
1621 }
1622 break;
1623
1624 case 3: /*inside a JSON string: escape unicode */
1625 {
1626 assert (*text != NULL);
1627 if ((**p >= 'a') && (**p <= 'f'))
1628 {
1629 if (rcs_catc (*text, **p) != RS_OK)
1630 return LEX_MEMORY;
1631 *state = 4; /* inside a JSON string: escape unicode */
1632 }
1633 else if ((**p >= 'A') && (**p <= 'F'))
1634 {
1635 if (rcs_catc (*text, **p) != RS_OK)
1636 return LEX_MEMORY;
1637 *state = 4; /* inside a JSON string: escape unicode */
1638 }
1639 else if ((**p >= '0') && (**p <= '9'))
1640 {
1641 if (rcs_catc (*text, **p) != RS_OK)
1642 return LEX_MEMORY;
1643 *state = 4; /* inside a JSON string: escape unicode */
1644 }
1645 else
1646 return LEX_INVALID_CHARACTER;
1647 ++*p;
1648 }
1649 break;
1650
1651 case 4: /* inside a JSON string: escape unicode */
1652 {
1653 assert (*text != NULL);
1654 if ((**p >= 'a') && (**p <= 'f'))
1655 {
1656 if (rcs_catc (*text, **p) != RS_OK)
1657 return LEX_MEMORY;
1658 *state = 5; /* inside a JSON string: escape unicode */
1659 }
1660 else if ((**p >= 'A') && (**p <= 'F'))
1661 {
1662 if (rcs_catc (*text, **p) != RS_OK)
1663 return LEX_MEMORY;
1664 *state = 5; /* inside a JSON string: escape unicode */
1665 }
1666 else if ((**p >= '0') && (**p <= '9'))
1667 {
1668 if (rcs_catc (*text, **p) != RS_OK)
1669 return LEX_MEMORY;
1670 *state = 5; /* inside a JSON string: escape unicode */
1671 }
1672 else
1673 return LEX_INVALID_CHARACTER;
1674 ++*p;
1675 }
1676 break;
1677
1678 case 5: /* inside a JSON string: escape unicode */
1679 {
1680 assert (*text != NULL);
1681 if ((**p >= 'a') && (**p <= 'f'))
1682 {
1683 if (rcs_catc (*text, **p) != RS_OK)
1684 return LEX_MEMORY;
1685 *state = 6; /* inside a JSON string: escape unicode */
1686 }
1687 else if ((**p >= 'A') && (**p <= 'F'))
1688 {
1689 if (rcs_catc (*text, **p) != RS_OK)
1690 return LEX_MEMORY;
1691 *state = 6; /* inside a JSON string: escape unicode */
1692 }
1693 else if ((**p >= '0') && (**p <= '9'))
1694 {
1695 if (rcs_catc (*text, **p) != RS_OK)
1696 return LEX_MEMORY;
1697 *state = 6; /* inside a JSON string: escape unicode */
1698 }
1699 else
1700 return LEX_INVALID_CHARACTER;
1701 ++*p;
1702 }
1703 break;
1704
1705 case 6: /* inside a JSON string: escape unicode */
1706 {
1707 assert (*text != NULL);
1708 if ((**p >= 'a') && (**p <= 'f'))
1709 {
1710 if (rcs_catc (*text, **p) != RS_OK)
1711 return LEX_MEMORY;
1712 *state = 1; /* inside a JSON string: escape unicode */
1713 }
1714 else if ((**p >= 'A') && (**p <= 'F'))
1715 {
1716 if (rcs_catc (*text, **p) != RS_OK)
1717 return LEX_MEMORY;
1718 *state = 1; /* inside a JSON string: escape unicode */
1719 }
1720 else if ((**p >= '0') && (**p <= '9'))
1721 {
1722 if (rcs_catc (*text, **p) != RS_OK)
1723 return LEX_MEMORY;
1724 *state = 1; /* inside a JSON string: escape unicode */
1725 }
1726 else
1727 return LEX_INVALID_CHARACTER;
1728 ++*p;
1729 }
1730 break;
1731
1732 case 7: /* true: 1 */
1733 {
1734 switch (*(*p)++)
1735 {
1736 case 'r':
1737 *state = 8;
1738 break;
1739 default:
1740 return LEX_INVALID_CHARACTER;
1741 break;
1742 }
1743 }
1744 break;
1745
1746 case 8: /* true: 2 */
1747 {
1748 switch (*(*p)++)
1749 {
1750 case 'u':
1751 *state = 9;
1752 break;
1753 default:
1754 return LEX_INVALID_CHARACTER;
1755 break;
1756 }
1757 }
1758 break;
1759
1760 case 9: /* true: 3 */
1761 {
1762 switch (*(*p)++)
1763 {
1764 case 'e':
1765 *state = 0;
1766 return LEX_TRUE;
1767 break;
1768 default:
1769 return LEX_INVALID_CHARACTER;
1770 break;
1771 }
1772 }
1773 break;
1774
1775 case 10: /* false: 1 */
1776 {
1777 switch (*(*p)++)
1778 {
1779 case 'a':
1780 *state = 11;
1781 break;
1782 default:
1783 return LEX_INVALID_CHARACTER;
1784 break;
1785 }
1786 }
1787 break;
1788
1789 case 11: /* false: 2 */
1790 {
1791 switch (*(*p)++)
1792 {
1793 case 'l':
1794 *state = 12;
1795 break;
1796 default:
1797 return LEX_INVALID_CHARACTER;
1798 break;
1799 }
1800 }
1801 break;
1802
1803 case 12: /* false: 3 */
1804 {
1805 switch (*(*p)++)
1806 {
1807 case 's':
1808 *state = 13;
1809 break;
1810 default:
1811 return LEX_INVALID_CHARACTER;
1812 break;
1813 }
1814 }
1815 break;
1816
1817 case 13: /* false: 4 */
1818 {
1819 switch (*(*p)++)
1820 {
1821 case 'e':
1822 *state = 0;
1823 return LEX_FALSE;
1824 break;
1825 default:
1826 return LEX_INVALID_CHARACTER;
1827 break;
1828 }
1829 }
1830 break;
1831
1832 case 14: /* null: 1 */
1833 {
1834 switch (*(*p)++)
1835 {
1836 case 'u':
1837 *state = 15;
1838 break;
1839 default:
1840 return LEX_INVALID_CHARACTER;
1841 break;
1842 }
1843 }
1844 break;
1845
1846 case 15: /* null: 2 */
1847 {
1848 switch (*(*p)++)
1849 {
1850 case 'l':
1851 *state = 16;
1852 break;
1853 default:
1854 return LEX_INVALID_CHARACTER;
1855 break;
1856 }
1857 }
1858 break;
1859
1860 case 16: /* null: 3 */
1861 {
1862 switch (*(*p)++)
1863 {
1864 case 'l':
1865 *state = 0;
1866 return LEX_NULL;
1867 break;
1868 default:
1869 return LEX_INVALID_CHARACTER;
1870 break;
1871 }
1872 }
1873 break;
1874
1875 case 17: /* number: minus sign */
1876 {
1877 assert (*text != NULL);
1878 switch (**p)
1879 {
1880 case '0':
1881 if (rcs_catc (*text, **p) != RS_OK)
1882 return LEX_MEMORY;
1883 ++*p;
1884 *state = 18; /* number: '0' */
1885 break;
1886
1887 case '1':
1888 case '2':
1889 case '3':
1890 case '4':
1891 case '5':
1892 case '6':
1893 case '7':
1894 case '8':
1895 case '9':
1896 if (rcs_catc (*text, **p) != RS_OK)
1897 return LEX_MEMORY;
1898 ++*p;
1899 *state = 19; /* number: decimal followup */
1900 break;
1901
1902 default:
1903 return LEX_INVALID_CHARACTER;
1904 break;
1905 }
1906 }
1907 break;
1908
1909 case 18: /* number: '0' */
1910 {
1911 assert (*text != NULL);
1912 switch (**p)
1913 {
1914 case '\x20': /* space */
1915 case '\x09': /* horizontal tab */
1916 case '\x0A': /* line feed or new line */
1917 case '\x0D': /* Carriage return */
1918 ++*p;
1919 case ']':
1920 case '}':
1921 case ',':
1922 *state = 0;
1923 return LEX_NUMBER;
1924 break;
1925
1926 case '.':
1927 if (rcs_catc (*text, **p) != RS_OK)
1928 return LEX_MEMORY;
1929 ++*p;
1930 *state = 20; /* number: frac start */
1931 break;
1932
1933 case 'e':
1934 case 'E':
1935 if (rcs_catc (*text, **p) != RS_OK)
1936 return LEX_MEMORY;
1937 ++*p;
1938 *state = 22; /* number: exp start */
1939 break;
1940
1941 default:
1942 return LEX_INVALID_CHARACTER;
1943 break;
1944 }
1945 }
1946 break;
1947
1948 case 19: /* number: int followup */
1949 {
1950 assert (*text != NULL);
1951 switch (**p)
1952 {
1953 case '\x20': /* space */
1954 case '\x09': /* horizontal tab */
1955 case '\x0A': /* line feed or new line */
1956 case '\x0D': /* Carriage return */
1957 ++*p;
1958 case ']':
1959 case '}':
1960 case ',':
1961 *state = 0;
1962 return LEX_NUMBER;
1963 break;
1964
1965 case '.':
1966 if (rcs_catc (*text, **p) != RS_OK)
1967 return LEX_MEMORY;
1968 ++*p;
1969 *state = 20; /* number: frac start */
1970 break;
1971
1972 case 'e':
1973 case 'E':
1974 if (rcs_catc (*text, **p) != RS_OK)
1975 return LEX_MEMORY;
1976 ++*p;
1977 *state = 22; /* number: exp start */
1978 break;
1979
1980 case '0':
1981 case '1':
1982 case '2':
1983 case '3':
1984 case '4':
1985 case '5':
1986 case '6':
1987 case '7':
1988 case '8':
1989 case '9':
1990 if (rcs_catc (*text, **p) != RS_OK)
1991 return LEX_MEMORY;
1992 ++*p;
1993 break;
1994
1995 default:
1996 return LEX_INVALID_CHARACTER;
1997 break;
1998 }
1999 }
2000 break;
2001
2002 case 20: /* number: frac start */
2003 {
2004 assert (*text != NULL);
2005 switch (**p)
2006 {
2007 case '0':
2008 case '1':
2009 case '2':
2010 case '3':
2011 case '4':
2012 case '5':
2013 case '6':
2014 case '7':
2015 case '8':
2016 case '9':
2017 if (rcs_catc (*text, **p) != RS_OK)
2018 return LEX_MEMORY;
2019 ++*p;
2020 *state = 21; /* number: frac continue */
2021 break;
2022
2023 default:
2024 return LEX_INVALID_CHARACTER;
2025 break;
2026 }
2027 }
2028 break;
2029
2030 case 21: /* number: frac continue */
2031 {
2032 assert (*text != NULL);
2033 switch (**p)
2034 {
2035 case '\x20': /* space */
2036 case '\x09': /* horizontal tab */
2037 case '\x0A': /* line feed or new line */
2038 case '\x0D': /* Carriage return */
2039 ++*p;
2040 case ']':
2041 case '}':
2042 case ',':
2043 *state = 0;
2044 return LEX_NUMBER;
2045 break;
2046
2047 case 'e':
2048 case 'E':
2049 if (rcs_catc (*text, **p) != RS_OK)
2050 return LEX_MEMORY;
2051 ++*p;
2052 *state = 22; /* number: exp start */
2053 break;
2054
2055 case '0':
2056 case '1':
2057 case '2':
2058 case '3':
2059 case '4':
2060 case '5':
2061 case '6':
2062 case '7':
2063 case '8':
2064 case '9':
2065 if (rcs_catc (*text, **p) != RS_OK)
2066 return LEX_MEMORY;
2067 ++*p;
2068 break;
2069
2070 default:
2071 return LEX_INVALID_CHARACTER;
2072 break;
2073 }
2074 }
2075 break;
2076
2077 case 22: /* number: exp start */
2078 {
2079 assert (*text != NULL);
2080 switch (**p)
2081 {
2082 case '-':
2083 case '+':
2084 if (rcs_catc (*text, **p) != RS_OK)
2085 return LEX_MEMORY;
2086 ++*p;
2087 *state = 23; /* number: exp continue */
2088 break;
2089
2090 case '0':
2091 case '1':
2092 case '2':
2093 case '3':
2094 case '4':
2095 case '5':
2096 case '6':
2097 case '7':
2098 case '8':
2099 case '9':
2100 if (rcs_catc (*text, **p) != RS_OK)
2101 return LEX_MEMORY;
2102 ++*p;
2103 *state = 24; /* number: exp end */
2104 break;
2105
2106 default:
2107 return LEX_INVALID_CHARACTER;
2108 break;
2109 }
2110 }
2111 break;
2112
2113 case 23: /* number: exp continue */
2114 {
2115 assert (*text != NULL);
2116 switch (**p)
2117 {
2118 case '0':
2119 case '1':
2120 case '2':
2121 case '3':
2122 case '4':
2123 case '5':
2124 case '6':
2125 case '7':
2126 case '8':
2127 case '9':
2128 if (rcs_catc (*text, **p) != RS_OK)
2129 return LEX_MEMORY;
2130 ++*p;
2131 *state = 24; /* number: exp end */
2132 break;
2133
2134 default:
2135 return LEX_INVALID_CHARACTER;
2136 break;
2137 }
2138 }
2139 break;
2140
2141 case 24: /* number: exp end */
2142 {
2143 assert (*text != NULL);
2144 switch (**p)
2145 {
2146 case '\x20': /* space */
2147 case '\x09': /* horizontal tab */
2148 case '\x0A': /* line feed or new line */
2149 case '\x0D': /* Carriage return */
2150 ++*p;
2151 case ']':
2152 case '}':
2153 case ',':
2154 *state = 0;
2155 return LEX_NUMBER;
2156 break;
2157
2158 case '0':
2159 case '1':
2160 case '2':
2161 case '3':
2162 case '4':
2163 case '5':
2164 case '6':
2165 case '7':
2166 case '8':
2167 case '9':
2168 if (rcs_catc (*text, **p) != RS_OK)
2169 return LEX_MEMORY;
2170 ++*p;
2171 break;
2172
2173 default:
2174 return LEX_INVALID_CHARACTER;
2175 break;
2176 }
2177 }
2178 break;
2179
2180 default:
2181 fprintf (stderr, "JSON: *state missing: %d\n", *state);
2182 return LEX_INVALID_CHARACTER;
2183 }
2184
2185 }
2186
2187 *p = NULL;
2188 return LEX_MORE;
2189 }
2190
2191
2192 enum json_error
2193 json_parse_fragment (struct json_parsing_info *info, const char *buffer)
2194 {
2195 json_t *temp = NULL;
2196
2197 assert (info != NULL);
2198 assert (buffer != NULL);
2199
2200 info->p = buffer;
2201 while (*info->p != '\0')
2202 {
2203 switch (info->state)
2204 {
2205 case 0: /* starting point */
2206 {
2207 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2208 {
2209 case LEX_BEGIN_OBJECT:
2210 info->state = 1; /* begin object */
2211 break;
2212
2213 case LEX_INVALID_CHARACTER:
2214 return JSON_MALFORMED_DOCUMENT;
2215 break;
2216
2217 default:
2218 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2219 return JSON_MALFORMED_DOCUMENT;
2220 break;
2221 }
2222 }
2223 break;
2224
2225 case 1: /* open object */
2226 {
2227 if (info->cursor == NULL)
2228 {
2229 if ((info->cursor = json_new_object ()) == NULL)
2230 {
2231 return JSON_MEMORY;
2232 }
2233 }
2234 else
2235 {
2236 /* perform tree sanity check */
2237 assert ((info->cursor->type == JSON_STRING) || (info->cursor->type == JSON_ARRAY));
2238
2239 if ((temp = json_new_object ()) == NULL)
2240 {
2241 return JSON_MEMORY;
2242 }
2243 if (json_insert_child (info->cursor, temp) != JSON_OK)
2244 {
2245 return JSON_UNKNOWN_PROBLEM;
2246 }
2247 info->cursor = temp;
2248 temp = NULL;
2249 }
2250 info->state = 2; /* just entered an object */
2251 }
2252 break;
2253
2254 case 2: /* opened object */
2255 {
2256 /*perform tree sanity checks */
2257 assert (info->cursor != NULL);
2258 assert (info->cursor->type == JSON_OBJECT);
2259
2260 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2261 {
2262 case LEX_STRING:
2263 if ((temp = json_new_value (JSON_STRING)) == NULL)
2264 return JSON_MEMORY;
2265 temp->text = rcs_unwrap (info->lex_text), info->lex_text = NULL;
2266 if (json_insert_child (info->cursor, temp) != JSON_OK)
2267 {
2268 /*TODO return value according to the value returned from json_insert_child() */
2269 return JSON_UNKNOWN_PROBLEM;
2270 }
2271 info->cursor = temp;
2272 temp = NULL;
2273 info->state = 5; /* label, pre label:value separator */
2274 break;
2275
2276 case LEX_END_OBJECT:
2277 if (info->cursor->parent == NULL)
2278 {
2279 info->state = 99; /* finished document. only accept whitespaces until EOF */
2280 }
2281 else
2282 {
2283 info->cursor = info->cursor->parent;
2284 switch (info->cursor->type)
2285 {
2286 case JSON_STRING:
2287 /* perform tree sanity checks */
2288 assert (info->cursor->parent != NULL);
2289
2290 info->cursor = info->cursor->parent;
2291 if (info->cursor->type != JSON_OBJECT)
2292 {
2293 return JSON_BAD_TREE_STRUCTURE;
2294 }
2295 else
2296 {
2297 info->state = 3; /* finished adding a field to an object */
2298 }
2299 break;
2300
2301 case JSON_ARRAY:
2302 info->state = 9;
2303 break;
2304
2305 default:
2306 return JSON_BAD_TREE_STRUCTURE;
2307 }
2308 }
2309 break;
2310
2311 case LEX_MORE:
2312 return JSON_INCOMPLETE_DOCUMENT;
2313 break;
2314
2315 default:
2316 /* this should never run */
2317 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2318 return JSON_MALFORMED_DOCUMENT;
2319 break;
2320 }
2321 }
2322 break;
2323
2324 case 3: /* finished adding a field to an object */
2325 {
2326 /*perform tree sanity checks */
2327 assert (info->cursor != NULL);
2328 assert (info->cursor->type == JSON_OBJECT);
2329
2330 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2331 {
2332 case LEX_VALUE_SEPARATOR:
2333 info->state = 4; /* sibling, post-object */
2334 break;
2335
2336 case LEX_END_OBJECT:
2337 if (info->cursor->parent == NULL)
2338 {
2339 info->state = 99; /* parse until EOF */
2340 }
2341 else
2342 {
2343 info->cursor = info->cursor->parent;
2344 switch (info->cursor->type)
2345 {
2346 case JSON_STRING:
2347 /* perform tree sanity checks */
2348 assert (info->cursor->parent != NULL);
2349
2350 info->cursor = info->cursor->parent;
2351 if (info->cursor->type != JSON_OBJECT)
2352 {
2353 return JSON_BAD_TREE_STRUCTURE;
2354 }
2355 else
2356 {
2357 info->state = 3; /* finished adding a field to an object */
2358 }
2359 break;
2360
2361 case JSON_ARRAY:
2362 info->state = 9;
2363 break;
2364
2365 default:
2366 return JSON_BAD_TREE_STRUCTURE;
2367 }
2368 }
2369 break;
2370
2371 case LEX_MORE:
2372 return JSON_INCOMPLETE_DOCUMENT;
2373 break;
2374
2375 default:
2376 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2377 return JSON_MALFORMED_DOCUMENT;
2378 break;
2379 }
2380 }
2381 break;
2382
2383 case 4: /* sibling, post-object */
2384 {
2385 assert (info->cursor != NULL);
2386 assert (info->cursor->type == JSON_OBJECT);
2387
2388 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2389 {
2390 case LEX_STRING:
2391 if ((temp = json_new_value (JSON_STRING)) == NULL)
2392 return JSON_MEMORY;
2393 temp->text = rcs_unwrap (info->lex_text), info->lex_text = NULL;
2394 if (json_insert_child (info->cursor, temp) != JSON_OK)
2395 {
2396 return JSON_UNKNOWN_PROBLEM;
2397 }
2398 info->cursor = temp;
2399 temp = NULL;
2400 info->state = 5;
2401 break;
2402
2403 case LEX_MORE:
2404 return JSON_INCOMPLETE_DOCUMENT;
2405 break;
2406
2407 case LEX_INVALID_CHARACTER:
2408 return JSON_ILLEGAL_CHARACTER;
2409 break;
2410
2411 default: /* this should never run */
2412 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2413 return JSON_MALFORMED_DOCUMENT;
2414 break;
2415 }
2416 }
2417 break;
2418
2419 case 5: /* label, pre name separator */
2420 {
2421 /* perform tree sanity checks */
2422 assert (info->cursor != NULL);
2423 assert (info->cursor->type == JSON_STRING);
2424
2425 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2426 {
2427 case LEX_NAME_SEPARATOR:
2428 info->state = 6; /* label, pos label:value separator */
2429 break;
2430
2431 case LEX_MORE:
2432 return JSON_INCOMPLETE_DOCUMENT;
2433 break;
2434
2435 default:
2436 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2437 return JSON_MALFORMED_DOCUMENT;
2438 break;
2439 }
2440 }
2441 break;
2442
2443 case 6: /* label, pos name separator */
2444 {
2445 unsigned int value; /* to avoid redundant code */
2446 /* perform tree sanity checks */
2447 assert (info->cursor != NULL);
2448 assert (info->cursor->type == JSON_STRING);
2449
2450 switch (value = lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2451 {
2452 case LEX_STRING:
2453 if ((temp = json_new_value (JSON_STRING)) == NULL)
2454 return JSON_MEMORY;
2455 temp->text = rcs_unwrap (info->lex_text), info->lex_text = NULL;
2456 if (json_insert_child (info->cursor, temp) != JSON_OK)
2457 {
2458 /*TODO specify the exact error message */
2459 return JSON_UNKNOWN_PROBLEM;
2460 }
2461 if (info->cursor->parent == NULL)
2462 {
2463 info->state = 99; /* finished document. only accepts whitespaces until EOF */
2464 }
2465 else
2466 {
2467 info->cursor = info->cursor->parent;
2468 }
2469 temp = NULL;
2470 info->state = 3; /* finished adding a field to an object */
2471 break;
2472
2473 case LEX_NUMBER:
2474 if ((temp = json_new_value (JSON_NUMBER)) == NULL)
2475 return JSON_MEMORY;
2476 temp->text = rcs_unwrap (info->lex_text), info->lex_text = NULL;
2477 if (json_insert_child (info->cursor, temp) != JSON_OK)
2478 {
2479 /*TODO specify the exact error message */
2480 return JSON_UNKNOWN_PROBLEM;
2481 }
2482 if (info->cursor->parent == NULL)
2483 {
2484 info->state = 99; /* finished document. only accepts whitespaces until EOF */
2485 }
2486 else
2487 {
2488 info->cursor = info->cursor->parent;
2489 }
2490 temp = NULL;
2491 info->state = 3; /* finished adding a field to an object */
2492 break;
2493
2494 case LEX_TRUE:
2495 if ((temp = json_new_value (JSON_TRUE)) == NULL)
2496 return JSON_MEMORY;
2497 if (json_insert_child (info->cursor, temp) != JSON_OK)
2498 {
2499 /*TODO specify the exact error message */
2500 return JSON_UNKNOWN_PROBLEM;
2501 }
2502 if (info->cursor->parent == NULL)
2503 {
2504 info->state = 99; /* finished document. only accepts whitespaces until EOF */
2505 }
2506 else
2507 {
2508 info->cursor = info->cursor->parent;
2509 }
2510 temp = NULL;
2511 info->state = 3; /* finished adding a field to an object */
2512 break;
2513
2514 case LEX_FALSE:
2515 if ((temp = json_new_value (JSON_FALSE)) == NULL)
2516 return JSON_MEMORY;
2517 if (json_insert_child (info->cursor, temp) != JSON_OK)
2518 {
2519 /*TODO specify the exact error message */
2520 return JSON_UNKNOWN_PROBLEM;
2521 }
2522 if (info->cursor->parent == NULL)
2523 {
2524 info->state = 99; /* finished document. only accepts whitespaces until EOF */
2525 }
2526 else
2527 {
2528 info->cursor = info->cursor->parent;
2529 }
2530 temp = NULL;
2531 info->state = 3; /* finished adding a field to an object */
2532 break;
2533
2534 case LEX_NULL:
2535 if ((temp = json_new_value (JSON_NULL)) == NULL)
2536 return JSON_MEMORY;
2537 if (json_insert_child (info->cursor, temp) != JSON_OK)
2538 {
2539 /*TODO specify the exact error message */
2540 return JSON_UNKNOWN_PROBLEM;
2541 }
2542 if (info->cursor->parent == NULL)
2543 {
2544 info->state = 99; /* finished document. only accepts whitespaces until EOF */
2545 }
2546 else
2547 {
2548 info->cursor = info->cursor->parent;
2549 }
2550 temp = NULL;
2551 info->state = 3; /* finished adding a field to an object */
2552 break;
2553
2554 case LEX_BEGIN_OBJECT:
2555 info->state = 1;
2556 break;
2557
2558 case LEX_BEGIN_ARRAY:
2559 info->state = 7;
2560 break;
2561
2562 case LEX_MORE:
2563 return JSON_INCOMPLETE_DOCUMENT;
2564 break;
2565
2566 case LEX_MEMORY:
2567 return JSON_MEMORY;
2568 break;
2569
2570 case LEX_INVALID_CHARACTER:
2571 return JSON_ILLEGAL_CHARACTER;
2572 break;
2573
2574 default:
2575 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2576 return JSON_MALFORMED_DOCUMENT;
2577 break;
2578 }
2579 }
2580 break;
2581
2582 case 7: /* open array */
2583 {
2584 if (info->cursor == NULL)
2585 {
2586 if ((info->cursor = json_new_array ()) == NULL)
2587 {
2588 return JSON_MEMORY;
2589 }
2590 }
2591 else
2592 {
2593 /* perform tree sanity checks */
2594 assert ((info->cursor->type == JSON_ARRAY) || (info->cursor->type == JSON_STRING));
2595
2596 if ((temp = json_new_array ()) == NULL)
2597 {
2598 return JSON_MEMORY;
2599 }
2600 if (json_insert_child (info->cursor, temp) != JSON_OK)
2601 {
2602 return JSON_UNKNOWN_PROBLEM;
2603 }
2604 info->cursor = temp;
2605 temp = NULL;
2606 }
2607 info->state = 8; /* just entered an array */
2608 }
2609 break;
2610
2611 case 8: /* just entered an array */
2612 {
2613 /* perform tree sanity checks */
2614 assert (info->cursor != NULL);
2615 assert (info->cursor->type == JSON_ARRAY);
2616
2617 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2618 {
2619 case LEX_STRING:
2620 if ((temp = json_new_value (JSON_STRING)) == NULL)
2621 return JSON_MEMORY;
2622 temp->text = rcs_unwrap (info->lex_text), info->lex_text = NULL;
2623 if (json_insert_child (info->cursor, temp) != JSON_OK)
2624 {
2625 return JSON_UNKNOWN_PROBLEM;
2626 }
2627 temp = NULL;
2628 info->state = 9; /* label, pre label:value separator */
2629 break;
2630
2631 case LEX_NUMBER:
2632 if ((temp = json_new_value (JSON_NUMBER)) == NULL)
2633 return JSON_MEMORY;
2634 temp->text = rcs_unwrap (info->lex_text), info->lex_text = NULL;
2635 if (json_insert_child (info->cursor, temp) != JSON_OK)
2636 {
2637 return JSON_UNKNOWN_PROBLEM;
2638 }
2639 temp = NULL;
2640 info->state = 9; /* label, pre label:value separator */
2641 break;
2642
2643 case LEX_TRUE:
2644 if ((temp = json_new_value (JSON_TRUE)) == NULL)
2645 return JSON_MEMORY;
2646 if (json_insert_child (info->cursor, temp) != JSON_OK)
2647 {
2648 return JSON_UNKNOWN_PROBLEM;
2649 }
2650 info->state = 9; /* label, pre label:value separator */
2651 break;
2652
2653 case LEX_FALSE:
2654 if ((temp = json_new_value (JSON_FALSE)) == NULL)
2655 return JSON_MEMORY;
2656 if (json_insert_child (info->cursor, temp) != JSON_OK)
2657 {
2658 return JSON_UNKNOWN_PROBLEM;
2659 }
2660 info->state = 9; /* label, pre label:value separator */
2661 break;
2662
2663 case LEX_NULL:
2664 if ((temp = json_new_value (JSON_NULL)) == NULL)
2665 return JSON_MEMORY;
2666 if (json_insert_child (info->cursor, temp) != JSON_OK)
2667 {
2668 return JSON_UNKNOWN_PROBLEM;
2669 }
2670 info->state = 9; /* label, pre label:value separator */
2671 break;
2672
2673 case LEX_BEGIN_ARRAY:
2674 info->state = 7; /* open array */
2675 break;
2676
2677 case LEX_END_ARRAY:
2678 if (info->cursor->parent == NULL)
2679 {
2680 /*TODO implement this */
2681 info->state = 99; /* finished document. only accept whitespaces until EOF */
2682 }
2683 else
2684 {
2685 info->cursor = info->cursor->parent;
2686 switch (info->cursor->type)
2687 {
2688 case JSON_STRING:
2689 if (info->cursor->parent == NULL)
2690 return JSON_BAD_TREE_STRUCTURE;
2691 else
2692 {
2693 info->cursor = info->cursor->parent;
2694 if (info->cursor->type != JSON_OBJECT)
2695 {
2696 return JSON_BAD_TREE_STRUCTURE;
2697 }
2698
2699 info->state = 3; /* followup to adding child to array */
2700 }
2701 break;
2702
2703 case JSON_ARRAY:
2704 info->state = 9; /* followup to adding child to array */
2705 break;
2706
2707 default:
2708 return JSON_BAD_TREE_STRUCTURE;
2709 }
2710 }
2711 break;
2712
2713 case LEX_BEGIN_OBJECT:
2714 info->state = 1; /* open object */
2715 break;
2716
2717 case LEX_MORE:
2718 return JSON_INCOMPLETE_DOCUMENT;
2719 break;
2720
2721 case LEX_INVALID_CHARACTER:
2722 return JSON_ILLEGAL_CHARACTER;
2723 break;
2724
2725 default:
2726 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2727 return JSON_MALFORMED_DOCUMENT;
2728 break;
2729 }
2730 }
2731 break;
2732
2733 case 9: /* followup to adding child to array */
2734 {
2735 /*TODO perform tree sanity checks */
2736 assert (info->cursor != NULL);
2737 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2738 {
2739 case LEX_VALUE_SEPARATOR:
2740 info->state = 8;
2741 break;
2742
2743 case LEX_END_ARRAY:
2744 if (info->cursor->parent == NULL)
2745 {
2746 info->state = 99; /* finished document. only accept whitespaces until EOF */
2747 }
2748 else
2749 {
2750 info->cursor = info->cursor->parent;
2751 switch (info->cursor->type)
2752 {
2753 case JSON_STRING:
2754 if (info->cursor->parent == NULL)
2755 {
2756 info->state = 99; /* finished document. only accept whitespaces until EOF */
2757 }
2758 else
2759 {
2760 info->cursor = info->cursor->parent;
2761 if (info->cursor->type != JSON_OBJECT)
2762 {
2763 return JSON_BAD_TREE_STRUCTURE;
2764 }
2765 else
2766 {
2767 info->state = 3; /* followup to adding child to array */
2768 }
2769 }
2770 break;
2771
2772 case JSON_ARRAY:
2773 info->state = 9; /* followup to adding child to array */
2774 break;
2775
2776 default:
2777 return JSON_BAD_TREE_STRUCTURE;
2778 }
2779 }
2780 break;
2781
2782 case LEX_MORE:
2783 return JSON_INCOMPLETE_DOCUMENT;
2784 break;
2785
2786 default:
2787 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2788 return JSON_MALFORMED_DOCUMENT;
2789 break;
2790 }
2791 }
2792 break;
2793
2794 case 99: /* finished document. only accept whitespaces until EOF */
2795 {
2796 /* perform tree sanity check */
2797 assert (info->cursor->parent == NULL);
2798 switch (lexer (buffer, &info->p, &info->lex_state, &info->lex_text, &info->line))
2799 {
2800 case LEX_MORE:
2801 return JSON_WAITING_FOR_EOF;
2802 break;
2803
2804 case LEX_MEMORY:
2805 return JSON_MEMORY;
2806 break;
2807
2808 default:
2809 return JSON_MALFORMED_DOCUMENT;
2810 break;
2811 }
2812 }
2813 break;
2814
2815 default:
2816 fprintf (stderr, "JSON: state %d: defaulted at line %ld\n", info->state, (long)info->line);
2817 return JSON_UNKNOWN_PROBLEM;
2818 }
2819 }
2820 info->p = NULL;
2821 if (info->state == 99)
2822 return JSON_WAITING_FOR_EOF;
2823 else
2824 return JSON_INCOMPLETE_DOCUMENT;
2825 }
2826
2827
2828
2829 enum json_error
2830 json_parse_document (json_t ** root, const char *text)
2831 {
2832 enum json_error error;
2833 struct json_parsing_info *jpi;
2834
2835 assert (root != NULL);
2836 assert (*root == NULL);
2837 assert (text != NULL);
2838
2839 /* initialize the parsing structure */
2840 jpi = malloc (sizeof (struct json_parsing_info));
2841 if (jpi == NULL)
2842 {
2843 return JSON_MEMORY;
2844 }
2845 json_jpi_init (jpi);
2846
2847 error = json_parse_fragment (jpi, text);
2848 if ((error == JSON_WAITING_FOR_EOF) || (error == JSON_OK))
2849 {
2850 *root = jpi->cursor;
2851 free (jpi);
2852 return JSON_OK;
2853 }
2854 else
2855 {
2856 free (jpi);
2857 return error;
2858 }
2859 }
2860
2861
2862 enum json_error
2863 json_saxy_parse (struct json_saxy_parser_status *jsps, struct json_saxy_functions *jsf, char c)
2864 {
2865 /* make sure everything is in it's place */
2866 assert (jsps != NULL);
2867 assert (jsf != NULL);
2868
2869 /* goto where we left off */
2870 switch (jsps->state)
2871 {
2872 case 0: /* general state. everything goes. */
2873 goto state0;
2874 break;
2875 case 1: /* parse string */
2876 goto state1;
2877 break;
2878 case 2: /* parse string: escaped character */
2879 goto state2;
2880 break;
2881 case 3: /* parse string: escaped unicode 1 */
2882 goto state3;
2883 break;
2884 case 4: /* parse string: escaped unicode 2 */
2885 goto state4;
2886 break;
2887 case 5: /* parse string: escaped unicode 3 */
2888 goto state5;
2889 break;
2890 case 6: /* parse string: escaped unicode 4 */
2891 goto state6;
2892 break;
2893 case 7: /* parse true: tr */
2894 goto state7;
2895 break;
2896 case 8: /* parse true: tru */
2897 goto state8;
2898 break;
2899 case 9: /* parse true: true */
2900 goto state9;
2901 break;
2902 case 10: /* parse false: fa */
2903 goto state10;
2904 break;
2905 case 11: /* parse false: fal */
2906 goto state11;
2907 break;
2908 case 12: /* parse false: fals */
2909 goto state12;
2910 break;
2911 case 13: /* parse false: false */
2912 goto state13;
2913 break;
2914 case 14: /* parse null: nu */
2915 goto state14;
2916 break;
2917 case 15: /* parse null: nul */
2918 goto state15;
2919 break;
2920 case 16: /* parse null: null */
2921 goto state16;
2922 break;
2923 case 17: /* parse number: 0 */
2924 goto state17;
2925 break;
2926 case 18: /* parse number: start fraccional part */
2927 goto state18;
2928 break;
2929 case 19: /* parse number: fraccional part */
2930 goto state19;
2931 break;
2932 case 20: /* parse number: start exponent part */
2933 goto state20;
2934 break;
2935 case 21: /* parse number: exponent part */
2936 goto state21;
2937 break;
2938 case 22: /* parse number: exponent sign part */
2939 goto state22;
2940 break;
2941 case 23: /* parse number: start negative */
2942 goto state23;
2943 break;
2944 case 24: /* parse number: decimal part */
2945 goto state24;
2946 break;
2947 case 25: /* open object */
2948 goto state25;
2949 break;
2950 case 26: /* close object/array */
2951 goto state26;
2952 break;
2953 case 27: /* sibling followup */
2954 goto state27;
2955 break;
2956
2957 default: /* oops... this should never be reached */
2958 return JSON_UNKNOWN_PROBLEM;
2959 }
2960
2961 state0: /* starting point */
2962 {
2963 switch (c)
2964 {
2965 case '\x20':
2966 case '\x09':
2967 case '\x0A':
2968 case '\x0D': /* JSON insignificant white spaces */
2969 break;
2970
2971 case '\"': /* starting a string */
2972 jsps->string_length_limit_reached = 0;
2973 jsps->state = 1;
2974 break;
2975
2976 case '{':
2977 if (jsf->open_object != NULL)
2978 jsf->open_object ();
2979 jsps->state = 25; /*open object */
2980 break;
2981
2982 case '}':
2983 if (jsf->close_object != NULL)
2984 jsf->close_object ();
2985 jsps->state = 26; /* close object/array */
2986 break;
2987
2988 case '[':
2989 if (jsf->open_array != NULL)
2990 jsf->open_array ();
2991 /* jsps->state = 0; // redundant*/
2992 break;
2993
2994 case ']':
2995 if (jsf->close_array != NULL)
2996 jsf->close_array ();
2997 jsps->state = 26; /* close object/array */
2998 break;
2999
3000 case 't':
3001 jsps->state = 7; /* parse true: tr */
3002 break;
3003
3004 case 'f':
3005 jsps->state = 10; /* parse false: fa */
3006 break;
3007
3008 case 'n':
3009 jsps->state = 14; /* parse null: nu */
3010 break;
3011
3012 case ':':
3013 if (jsf->label_value_separator != NULL)
3014 jsf->label_value_separator ();
3015 /* jsps->state = 0; // redundant*/
3016 break;
3017
3018 case ',':
3019 if (jsf->sibling_separator != NULL)
3020 jsf->sibling_separator ();
3021 jsps->state = 27; /* sibling followup */
3022 break;
3023
3024 case '0':
3025 jsps->string_length_limit_reached = 0;
3026 jsps->state = 17; /* parse number: 0 */
3027 if ((jsps->temp = rcs_create (5)) == NULL)
3028 {
3029 return JSON_MEMORY;
3030 }
3031 if (rcs_catc ((jsps->temp), '0') != RS_OK)
3032 {
3033 return JSON_MEMORY;
3034 }
3035 break;
3036
3037 case '1':
3038 case '2':
3039 case '3':
3040 case '4':
3041 case '5':
3042 case '6':
3043 case '7':
3044 case '8':
3045 case '9':
3046 jsps->string_length_limit_reached = 0;
3047 jsps->state = 24; /* parse number: decimal */
3048 if ((jsps->temp = rcs_create (5)) == NULL)
3049 {
3050 return JSON_MEMORY;
3051 }
3052 if (rcs_catc ((jsps->temp), c) != RS_OK)
3053 {
3054 return JSON_MEMORY;
3055 }
3056 break;
3057
3058 case '-':
3059 jsps->string_length_limit_reached = 0;
3060 jsps->state = 23; /* number: */
3061 jsps->temp = NULL;
3062 if ((jsps->temp = rcs_create (5)) == NULL)
3063 {
3064 return JSON_MEMORY;
3065 }
3066 if (rcs_catc ((jsps->temp), '-') != RS_OK)
3067 {
3068 return JSON_MEMORY;
3069 }
3070
3071 break;
3072
3073 default:
3074 return JSON_ILLEGAL_CHARACTER;
3075 break;
3076 }
3077 return JSON_OK;
3078 }
3079
3080 state1: /* parse string */
3081 {
3082 switch (c)
3083 {
3084 case '\\':
3085 if (!jsps->string_length_limit_reached)
3086 {
3087 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH - 1) /* check if there is space for a two character escape sequence */
3088 {
3089 if (rcs_catc ((jsps->temp), '\\') != RS_OK)
3090 {
3091 return JSON_MEMORY;
3092 }
3093 }
3094 else
3095 {
3096 jsps->string_length_limit_reached = 1;
3097 }
3098 }
3099 jsps->state = 2; /* parse string: escaped character */
3100 break;
3101
3102 case '\"': /* end of string */
3103 if ((jsps->temp) != NULL)
3104 {
3105 jsps->state = 0; /* starting point */
3106 if (jsf->new_string != NULL)
3107 jsf->new_string (((jsps->temp))->text); /*copied or integral? */
3108 rcs_free (&jsps->temp);
3109 }
3110 else
3111 return JSON_UNKNOWN_PROBLEM;
3112 break;
3113
3114 default:
3115 if (!jsps->string_length_limit_reached)
3116 {
3117 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH) /* check if there is space for a two character escape sequence */
3118 {
3119 if (rcs_catc ((jsps->temp), c) != RS_OK)
3120 {
3121 return JSON_MEMORY;
3122 }
3123 }
3124 else
3125 {
3126 jsps->string_length_limit_reached = 1;
3127 }
3128 }
3129 break;
3130 }
3131 return JSON_OK;
3132 }
3133
3134 state2: /* parse string: escaped character */
3135 {
3136 switch (c)
3137 {
3138 case '\"':
3139 case '\\':
3140 case '/':
3141 case 'b':
3142 case 'f':
3143 case 'n':
3144 case 'r':
3145 case 't':
3146 if (!jsps->string_length_limit_reached)
3147 {
3148 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH)
3149 {
3150 if (rcs_catc ((jsps->temp), c) != RS_OK)
3151 {
3152 return JSON_MEMORY;
3153 }
3154 }
3155 else
3156 {
3157 jsps->string_length_limit_reached = 1;
3158 }
3159 }
3160 break;
3161
3162 case 'u':
3163 if (!jsps->string_length_limit_reached)
3164 {
3165 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH - 4)
3166 {
3167 if (rcs_catc ((jsps->temp), 'u') != RS_OK)
3168 {
3169 return JSON_MEMORY;
3170 }
3171 }
3172 else
3173 {
3174 jsps->string_length_limit_reached = 1;
3175 }
3176 }
3177 jsps->state = 3; /* parse string: escaped unicode 1; */
3178 break;
3179
3180 default:
3181 return JSON_ILLEGAL_CHARACTER;
3182 break;
3183 }
3184 return JSON_OK;
3185 }
3186
3187 state3: /* parse string: escaped unicode 1 */
3188 {
3189 switch (c)
3190 {
3191 case '0':
3192 case '1':
3193 case '2':
3194 case '3':
3195 case '4':
3196 case '5':
3197 case '6':
3198 case '7':
3199 case '8':
3200 case '9':
3201 case 'a':
3202 case 'b':
3203 case 'c':
3204 case 'd':
3205 case 'e':
3206 case 'f':
3207 case 'A':
3208 case 'B':
3209 case 'C':
3210 case 'D':
3211 case 'E':
3212 case 'F':
3213 if (!jsps->string_length_limit_reached)
3214 {
3215 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH - 3)
3216 {
3217 if (rcs_catc ((jsps->temp), 'u') != RS_OK)
3218 {
3219 return JSON_MEMORY;
3220 }
3221 }
3222 else
3223 {
3224 jsps->string_length_limit_reached = 1;
3225 }
3226 }
3227 jsps->state = 4; /* parse string. escaped unicode 2 */
3228 break;
3229
3230 default:
3231 return JSON_ILLEGAL_CHARACTER;
3232 }
3233 return JSON_OK;
3234 }
3235
3236 state4: /* parse string: escaped unicode 2 */
3237 {
3238 switch (c)
3239 {
3240 case '0':
3241 case '1':
3242 case '2':
3243 case '3':
3244 case '4':
3245 case '5':
3246 case '6':
3247 case '7':
3248 case '8':
3249 case '9':
3250 case 'a':
3251 case 'b':
3252 case 'c':
3253 case 'd':
3254 case 'e':
3255 case 'f':
3256 case 'A':
3257 case 'B':
3258 case 'C':
3259 case 'D':
3260 case 'E':
3261 case 'F':
3262 if (!jsps->string_length_limit_reached)
3263 {
3264 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH - 2)
3265 {
3266 if (rcs_catc ((jsps->temp), c) != RS_OK)
3267 {
3268 return JSON_MEMORY;
3269 }
3270 }
3271 else
3272 {
3273 jsps->string_length_limit_reached = 1;
3274 }
3275 }
3276 jsps->state = 5; /* parse string. escaped unicode 3 */
3277 break;
3278
3279 default:
3280 return JSON_ILLEGAL_CHARACTER;
3281 }
3282 return JSON_OK;
3283 }
3284
3285 state5: /* parse string: escaped unicode 3 */
3286 {
3287 switch (c)
3288 {
3289 case '0':
3290 case '1':
3291 case '2':
3292 case '3':
3293 case '4':
3294 case '5':
3295 case '6':
3296 case '7':
3297 case '8':
3298 case '9':
3299 case 'a':
3300 case 'b':
3301 case 'c':
3302 case 'd':
3303 case 'e':
3304 case 'f':
3305 case 'A':
3306 case 'B':
3307 case 'C':
3308 case 'D':
3309 case 'E':
3310 case 'F':
3311 if (!jsps->string_length_limit_reached)
3312 {
3313 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH - 1)
3314 {
3315 if (rcs_catc ((jsps->temp), c) != RS_OK)
3316 {
3317 return JSON_MEMORY;
3318 }
3319 }
3320 else
3321 {
3322 jsps->string_length_limit_reached = 1;
3323 }
3324 }
3325 jsps->state = 6; /* parse string. escaped unicode 4 */
3326 break;
3327
3328 default:
3329 return JSON_ILLEGAL_CHARACTER;
3330 }
3331 return JSON_OK;
3332 }
3333
3334 state6: /* parse string: escaped unicode 4 */
3335 {
3336 switch (c)
3337 {
3338 case '0':
3339 case '1':
3340 case '2':
3341 case '3':
3342 case '4':
3343 case '5':
3344 case '6':
3345 case '7':
3346 case '8':
3347 case '9':
3348 case 'a':
3349 case 'b':
3350 case 'c':
3351 case 'd':
3352 case 'e':
3353 case 'f':
3354 case 'A':
3355 case 'B':
3356 case 'C':
3357 case 'D':
3358 case 'E':
3359 case 'F':
3360 if (!jsps->string_length_limit_reached)
3361 {
3362 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH)
3363 {
3364 if (rcs_catc ((jsps->temp), c) != RS_OK)
3365 {
3366 return JSON_MEMORY;
3367 }
3368 }
3369 else
3370 {
3371 jsps->string_length_limit_reached = 1;
3372 }
3373 }
3374 jsps->state = 1; /* parse string */
3375 break;
3376
3377 default:
3378 return JSON_ILLEGAL_CHARACTER;
3379 }
3380 return JSON_OK;
3381 }
3382
3383 state7: /* parse true: tr */
3384 {
3385 if (c != 'r')
3386 {
3387 return JSON_ILLEGAL_CHARACTER;
3388 }
3389
3390 jsps->state = 8; /* parse true: tru */
3391 return JSON_OK;
3392 }
3393
3394 state8: /* parse true: tru */
3395 {
3396 if (c != 'u')
3397 {
3398 return JSON_ILLEGAL_CHARACTER;
3399 }
3400
3401 jsps->state = 9; /* parse true: true */
3402 return JSON_OK;
3403 }
3404
3405 state9: /* parse true: true */
3406 {
3407 if (c != 'e')
3408 {
3409 return JSON_ILLEGAL_CHARACTER;
3410 }
3411
3412 jsps->state = 0; /* back to general state. */
3413 if (jsf->new_true != NULL)
3414 jsf->new_true ();
3415 return JSON_OK;
3416 }
3417
3418 state10: /* parse false: fa */
3419 {
3420 if (c != 'a')
3421 {
3422 return JSON_ILLEGAL_CHARACTER;
3423 }
3424
3425 jsps->state = 11; /* parse true: fal */
3426 return JSON_OK;
3427 }
3428
3429 state11: /* parse false: fal */
3430 {
3431 if (c != 'l')
3432 {
3433 return JSON_ILLEGAL_CHARACTER;
3434 }
3435
3436 jsps->state = 12; /* parse true: fals */
3437 return JSON_OK;
3438 }
3439
3440 state12: /* parse false: fals */
3441 {
3442 if (c != 's')
3443 {
3444 return JSON_ILLEGAL_CHARACTER;
3445 }
3446
3447 jsps->state = 13; /* parse true: false */
3448 return JSON_OK;
3449 }
3450
3451 state13: /* parse false: false */
3452 {
3453 if (c != 'e')
3454 {
3455 return JSON_ILLEGAL_CHARACTER;
3456 }
3457
3458 jsps->state = 0; /* general state. everything goes. */
3459 if (jsf->new_false != NULL)
3460 jsf->new_false ();
3461 return JSON_OK;
3462 }
3463
3464 state14: /* parse null: nu */
3465 {
3466 if (c != 'u')
3467 {
3468 return JSON_ILLEGAL_CHARACTER;
3469 }
3470
3471 jsps->state = 15; /* parse null: nul */
3472 return JSON_OK;
3473 }
3474
3475 state15: /* parse null: nul */
3476 {
3477 if (c != 'l')
3478 {
3479 return JSON_ILLEGAL_CHARACTER;
3480 }
3481
3482 jsps->state = 16; /* parse null: null */
3483 return JSON_OK;
3484 }
3485
3486 state16: /* parse null: null */
3487 {
3488 if (c != 'l')
3489 {
3490 return JSON_ILLEGAL_CHARACTER;
3491 }
3492
3493 jsps->state = 0; /* general state. everything goes. */
3494 if (jsf->new_null != NULL)
3495 jsf->new_null ();
3496 return JSON_OK;
3497 }
3498
3499 state17: /* parse number: 0 */
3500 {
3501 switch (c)
3502 {
3503 case '.':
3504 if ((jsps->temp = rcs_create (5)) == NULL)
3505 {
3506 return JSON_MEMORY;
3507 }
3508 if (rcs_catc ((jsps->temp), '.') != RS_OK)
3509 {
3510 return JSON_MEMORY;
3511 }
3512 jsps->state = 18; /* parse number: fraccional part */
3513 break;
3514
3515 case '\x20':
3516 case '\x09':
3517 case '\x0A':
3518 case '\x0D': /* JSON insignificant white spaces */
3519 if ((jsps->temp) == NULL)
3520 return JSON_MEMORY;
3521 if (jsf->new_number != NULL)
3522 {
3523 jsf->new_number ((jsps->temp)->text);
3524 }
3525 rcs_free (&jsps->temp);
3526
3527 jsps->state = 0;
3528 break;
3529
3530 case '}':
3531 if ((jsps->temp) == NULL)
3532 return JSON_MEMORY;
3533 if (jsf->new_number != NULL)
3534 {
3535 jsf->new_number ((jsps->temp)->text);
3536 }
3537 rcs_free (&jsps->temp);
3538
3539 if (jsf->open_object != NULL)
3540 jsf->close_object ();
3541 jsps->state = 26; /* close object/array */
3542 break;
3543
3544 case ']':
3545
3546 if ((jsps->temp) == NULL)
3547 return JSON_MEMORY;
3548 if (jsf->new_number != NULL)
3549 {
3550 jsf->new_number ((jsps->temp)->text);
3551 }
3552 rcs_free (&jsps->temp);
3553
3554 if (jsf->open_object != NULL)
3555 jsf->close_array ();
3556 jsps->state = 26; /* close object/array */
3557 break;
3558
3559 case ',':
3560
3561 if ((jsps->temp) == NULL)
3562 return JSON_MEMORY;
3563 if (jsf->new_number != NULL)
3564 {
3565 jsf->new_number ((jsps->temp)->text);
3566 }
3567 rcs_free (&jsps->temp);
3568
3569 if (jsf->open_object != NULL)
3570 jsf->label_value_separator ();
3571 jsps->state = 27; /* sibling followup */
3572 break;
3573
3574 default:
3575 return JSON_ILLEGAL_CHARACTER;
3576 break;
3577 }
3578
3579 return JSON_OK;
3580 }
3581
3582 state18: /* parse number: start fraccional part */
3583 {
3584 switch (c)
3585 {
3586 case '0':
3587 case '1':
3588 case '2':
3589 case '3':
3590 case '4':
3591 case '5':
3592 case '6':
3593 case '7':
3594 case '8':
3595 case '9':
3596 if (!jsps->string_length_limit_reached)
3597 {
3598 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
3599 {
3600 if (rcs_catc ((jsps->temp), c) != RS_OK)
3601 {
3602 return JSON_MEMORY;
3603 }
3604 }
3605 else
3606 {
3607 jsps->string_length_limit_reached = 1;
3608 }
3609 }
3610 jsps->state = 19; /* parse number: fractional part */
3611 break;
3612
3613 default:
3614 return JSON_ILLEGAL_CHARACTER;
3615 break;
3616 }
3617 return JSON_OK;
3618 }
3619
3620 state19: /* parse number: fraccional part */
3621 {
3622 switch (c)
3623 {
3624 case '0':
3625 case '1':
3626 case '2':
3627 case '3':
3628 case '4':
3629 case '5':
3630 case '6':
3631 case '7':
3632 case '8':
3633 case '9':
3634 if (!jsps->string_length_limit_reached)
3635 {
3636 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
3637 {
3638 if (rcs_catc ((jsps->temp), c) != RS_OK)
3639 {
3640 return JSON_MEMORY;
3641 }
3642 }
3643 else
3644 {
3645 jsps->string_length_limit_reached = 1;
3646 }
3647 }
3648 /* jsps->state = 19; // parse number: fractional part*/
3649 break;
3650
3651 case 'e':
3652 case 'E':
3653 if (rcs_catc ((jsps->temp), c) != RS_OK)
3654 {
3655 return JSON_MEMORY;
3656 }
3657
3658 jsps->state = 20; /* parse number: start exponent part */
3659 break;
3660
3661
3662 case '\x20':
3663 case '\x09':
3664 case '\x0A':
3665 case '\x0D': /* JSON insignificant white spaces */
3666
3667 if ((jsps->temp) == NULL)
3668 return JSON_MEMORY;
3669 if (jsf->new_number != NULL)
3670 {
3671 jsf->new_number ((jsps->temp)->text);
3672 }
3673 rcs_free (&jsps->temp);
3674
3675 jsps->state = 0;
3676 break;
3677
3678 case '}':
3679
3680 if ((jsps->temp) == NULL)
3681 return JSON_MEMORY;
3682 if (jsf->new_number != NULL)
3683 {
3684 jsf->new_number ((jsps->temp)->text);
3685 }
3686 rcs_free (&jsps->temp);
3687
3688 if (jsf->open_object != NULL)
3689 jsf->close_object ();
3690 jsps->state = 26; /* close object/array */
3691 break;
3692
3693 case ']':
3694 if (jsf->new_number != NULL)
3695 {
3696 if ((jsps->temp) == NULL)
3697 return JSON_MEMORY;
3698 jsf->new_number ((jsps->temp)->text);
3699 rcs_free (&jsps->temp);
3700 }
3701 else
3702 {
3703 rcs_free (&jsps->temp);
3704 jsps->temp = NULL;
3705 }
3706 if (jsf->open_object != NULL)
3707 jsf->close_array ();
3708 jsps->state = 26; /* close object/array */
3709 break;
3710
3711 case ',':
3712
3713 if ((jsps->temp) == NULL)
3714 return JSON_MEMORY;
3715 if (jsf->new_number != NULL)
3716 {
3717 jsf->new_number ((jsps->temp)->text);
3718 }
3719 rcs_free (&jsps->temp);
3720
3721 if (jsf->label_value_separator != NULL)
3722 jsf->label_value_separator ();
3723 jsps->state = 27; /* sibling followup */
3724 break;
3725
3726
3727 default:
3728 return JSON_ILLEGAL_CHARACTER;
3729 break;
3730 }
3731 return JSON_OK;
3732 }
3733
3734 state20: /* parse number: start exponent part */
3735 {
3736 switch (c)
3737 {
3738 case '+':
3739 case '-':
3740 jsps->string_length_limit_reached = 0;
3741 if (rcs_catc ((jsps->temp), c) != RS_OK)
3742 {
3743 return JSON_MEMORY;
3744 }
3745
3746 jsps->state = 22; /* parse number: exponent sign part */
3747 break;
3748
3749 case '0':
3750 case '1':
3751 case '2':
3752 case '3':
3753 case '4':
3754 case '5':
3755 case '6':
3756 case '7':
3757 case '8':
3758 case '9':
3759 if (!jsps->string_length_limit_reached)
3760 {
3761 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH)
3762 {
3763 if (rcs_catc ((jsps->temp), c) != RS_OK)
3764 {
3765 return JSON_MEMORY;
3766 }
3767
3768 }
3769 else
3770 {
3771 jsps->string_length_limit_reached = 1;
3772 }
3773 }
3774 jsps->state = 21; /* parse number: exponent part */
3775 break;
3776
3777 default:
3778 return JSON_ILLEGAL_CHARACTER;
3779 break;
3780 }
3781 return JSON_OK;
3782 }
3783
3784 state21: /* parse number: exponent part */
3785 {
3786 switch (c)
3787 {
3788 case '0':
3789 case '1':
3790 case '2':
3791 case '3':
3792 case '4':
3793 case '5':
3794 case '6':
3795 case '7':
3796 case '8':
3797 case '9':
3798 if (!jsps->string_length_limit_reached)
3799 {
3800 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH)
3801 {
3802 if (rcs_catc ((jsps->temp), c) != RS_OK)
3803 {
3804 return JSON_MEMORY;
3805 }
3806 }
3807 else
3808 {
3809 jsps->string_length_limit_reached = 1;
3810 }
3811 }
3812 /* jsps->state = 21; // parse number: exponent part*/
3813 break;
3814
3815 case '\x20':
3816 case '\x09':
3817 case '\x0A':
3818 case '\x0D': /* JSON insignificant white spaces */
3819
3820 if ((jsps->temp) == NULL)
3821 return JSON_MEMORY;
3822 if (jsf->new_number != NULL)
3823 {
3824 jsf->new_number ((jsps->temp)->text);
3825 }
3826 rcs_free (&jsps->temp);
3827
3828 jsps->state = 0;
3829 break;
3830
3831 case '}':
3832 if ((jsps->temp) == NULL)
3833 return JSON_MEMORY;
3834 if (jsf->new_number != NULL)
3835 {
3836 jsf->new_number ((jsps->temp)->text);
3837 }
3838 rcs_free (&jsps->temp);
3839
3840 if (jsf->open_object != NULL)
3841 jsf->close_object ();
3842 jsps->state = 26; /* close object */
3843 break;
3844
3845 case ']':
3846 if (jsf->new_number != NULL)
3847 {
3848 if ((jsps->temp) == NULL)
3849 return JSON_MEMORY;
3850 jsf->new_number ((jsps->temp)->text);
3851 free (jsps->temp);
3852 jsps->temp = NULL;
3853 }
3854 else
3855 {
3856 free ((jsps->temp));
3857 jsps->temp = NULL;
3858 }
3859 if (jsf->open_object != NULL)
3860 jsf->close_array ();
3861 jsps->state = 26; /* close object/array */
3862 break;
3863
3864 case ',':
3865 if (jsf->new_number != NULL)
3866 {
3867 if ((jsps->temp) == NULL)
3868 return JSON_MEMORY;
3869 jsf->new_number ((jsps->temp)->text);
3870 free ((jsps->temp));
3871 jsps->temp = NULL;
3872 }
3873 else
3874 {
3875 free (jsps->temp);
3876 jsps->temp = NULL;
3877 }
3878 if (jsf->label_value_separator != NULL)
3879 jsf->label_value_separator ();
3880 jsps->state = 27; /* sibling followup */
3881 break;
3882
3883 default:
3884 return JSON_ILLEGAL_CHARACTER;
3885 break;
3886 }
3887 return JSON_OK;
3888 }
3889
3890 state22: /* parse number: start exponent part */
3891 {
3892 switch (c)
3893 {
3894 case '0':
3895 case '1':
3896 case '2':
3897 case '3':
3898 case '4':
3899 case '5':
3900 case '6':
3901 case '7':
3902 case '8':
3903 case '9':
3904 if (!jsps->string_length_limit_reached)
3905 {
3906 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH)
3907 {
3908 rcs_catc ((jsps->temp), c);
3909 }
3910 else
3911 {
3912 jsps->string_length_limit_reached = 1;
3913 }
3914 }
3915 jsps->state = 21; /* parse number: exponent part */
3916 break;
3917
3918 default:
3919 return JSON_ILLEGAL_CHARACTER;
3920 break;
3921 }
3922 return JSON_OK;
3923 }
3924
3925 state23: /* parse number: start negative */
3926 {
3927 switch (c)
3928 {
3929 case '0':
3930 rcs_catc ((jsps->temp), c);
3931 jsps->state = 17; /* parse number: 0 */
3932 break;
3933
3934 case '1':
3935 case '2':
3936 case '3':
3937 case '4':
3938 case '5':
3939 case '6':
3940 case '7':
3941 case '8':
3942 case '9':
3943 if (!jsps->string_length_limit_reached)
3944 {
3945 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
3946 {
3947 if ((jsps->temp = rcs_create (5)) == NULL)
3948 {
3949 return JSON_MEMORY;
3950 }
3951 if (rcs_catc ((jsps->temp), c) != RS_OK)
3952 {
3953 return JSON_MEMORY;
3954 }
3955 else
3956 {
3957 jsps->string_length_limit_reached = 1;
3958 }
3959 }
3960 }
3961 jsps->state = 24; /* parse number: start decimal part */
3962 break;
3963
3964 default:
3965 return JSON_ILLEGAL_CHARACTER;
3966 break;
3967 }
3968 return JSON_OK;
3969 }
3970
3971 state24: /* parse number: decimal part */
3972 {
3973 switch (c)
3974 {
3975 case '0':
3976 case '1':
3977 case '2':
3978 case '3':
3979 case '4':
3980 case '5':
3981 case '6':
3982 case '7':
3983 case '8':
3984 case '9':
3985 if (!jsps->string_length_limit_reached)
3986 {
3987 if (rcs_length ((jsps->temp)) < JSON_MAX_STRING_LENGTH / 2)
3988 {
3989 if ((jsps->temp = rcs_create (5)) == NULL)
3990 {
3991 return JSON_MEMORY;
3992 }
3993 if (rcs_catc ((jsps->temp), c) != RS_OK)
3994 {
3995 return JSON_MEMORY;
3996 }
3997 }
3998 else
3999 {
4000 jsps->string_length_limit_reached = 1;
4001 }
4002 }
4003 /* jsps->state = 24; // parse number: decimal part*/
4004 break;
4005
4006 case '.':
4007 if ((jsps->temp = rcs_create (5)) == NULL)
4008 {
4009 return JSON_MEMORY;
4010 }
4011 if (rcs_catc ((jsps->temp), '.') != RS_OK)
4012 {
4013 return JSON_MEMORY;
4014 }
4015
4016 jsps->state = 18; /* parse number: start exponent part */
4017 break;
4018
4019 case 'e':
4020 case 'E':
4021 if ((jsps->temp = rcs_create (5)) == NULL)
4022 {
4023 return JSON_MEMORY;
4024 }
4025 if (rcs_catc ((jsps->temp), c) != RS_OK)
4026 {
4027 return JSON_MEMORY;
4028 }
4029
4030 jsps->string_length_limit_reached = 0; /* reset to accept the exponential part */
4031 jsps->state = 20; /* parse number: start exponent part */
4032 break;
4033
4034 case '\x20':
4035 case '\x09':
4036 case '\x0A':
4037 case '\x0D': /* JSON insignificant white spaces */
4038 if ((jsps->temp) == NULL)
4039 return JSON_MEMORY;
4040 if (jsf->new_number != NULL)
4041 {
4042 jsf->new_number ((jsps->temp)->text);
4043 }
4044 rcs_free (&jsps->temp);
4045
4046 jsps->state = 0;
4047 break;
4048
4049 case '}':
4050 if ((jsps->temp) == NULL)
4051 return JSON_MEMORY;
4052 if (jsf->new_number != NULL)
4053 {
4054 jsf->new_number ((jsps->temp)->text);
4055 }
4056 rcs_free (&jsps->temp);
4057
4058 if (jsf->open_object != NULL)
4059 jsf->close_object ();
4060 jsps->state = 26; /* close object/array */
4061 break;
4062
4063 case ']':
4064 if ((jsps->temp) == NULL)
4065 return JSON_MEMORY;
4066 if (jsf->new_number != NULL)
4067 {
4068 jsf->new_number ((jsps->temp)->text);
4069 }
4070 rcs_free (&jsps->temp);
4071
4072 if (jsf->open_object != NULL)
4073 jsf->close_array ();
4074 jsps->state = 26; /* close object/array */
4075 break;
4076
4077 case ',':
4078 if ((jsps->temp) == NULL)
4079 return JSON_MEMORY;
4080 if (jsf->new_number != NULL)
4081 {
4082 jsf->new_number ((jsps->temp)->text);
4083 }
4084 rcs_free (&jsps->temp);
4085
4086 if (jsf->label_value_separator != NULL)
4087 jsf->label_value_separator ();
4088 jsps->state = 27; /* sibling followup */
4089 break;
4090
4091 default:
4092 return JSON_ILLEGAL_CHARACTER;
4093 break;
4094 }
4095 return JSON_OK;
4096 }
4097
4098 state25: /* open object */
4099 {
4100 switch (c)
4101 {
4102 case '\x20':
4103 case '\x09':
4104 case '\x0A':
4105 case '\x0D': /* JSON insignificant white spaces */
4106 break;
4107
4108 case '\"':
4109 jsps->temp = NULL;
4110 jsps->state = 1;
4111 break;
4112
4113 case '}':
4114 if (jsf->close_object != NULL)
4115 jsf->close_object ();
4116 jsps->state = 26; /* close object */
4117 break;
4118
4119 default:
4120 return JSON_ILLEGAL_CHARACTER;
4121 break;
4122 }
4123 return JSON_OK;
4124 }
4125
4126 state26: /* close object/array */
4127 {
4128 switch (c)
4129 {
4130 case '\x20':
4131 case '\x09':
4132 case '\x0A':
4133 case '\x0D': /* JSON insignificant white spaces */
4134 break;
4135
4136 case '}':
4137 if (jsf->close_object != NULL)
4138 jsf->close_object ();
4139 /* jsp->state = 26; // close object*/
4140 break;
4141
4142 case ']':
4143 if (jsf->close_array != NULL)
4144 jsf->close_array ();
4145 /* jsps->state = 26; // close object/array*/
4146 break;
4147
4148 case ',':
4149 if (jsf->sibling_separator != NULL)
4150 jsf->sibling_separator ();
4151 jsps->state = 27; /* sibling followup */
4152 break;
4153
4154 default:
4155 return JSON_ILLEGAL_CHARACTER;
4156 break;
4157 }
4158 return JSON_OK;
4159 }
4160
4161 state27: /* sibling followup */
4162 {
4163 switch (c)
4164 {
4165 case '\x20':
4166 case '\x09':
4167 case '\x0A':
4168 case '\x0D': /* JSON insignificant white spaces */
4169 break;
4170
4171 case '\"':
4172 jsps->state = 1;
4173 jsps->temp = NULL;
4174 break;
4175
4176 case '{':
4177 if (jsf->open_object != NULL)
4178 jsf->open_object ();
4179 jsps->state = 25; /*open object */
4180 break;
4181
4182 case '[':
4183 if (jsf->open_array != NULL)
4184 jsf->open_array ();
4185 /* jsps->state = 0; // redundant*/
4186 break;
4187
4188 case 't':
4189 jsps->state = 7; /* parse true: tr */
4190 break;
4191
4192 case 'f':
4193 jsps->state = 10; /* parse false: fa */
4194 break;
4195
4196 case 'n':
4197 jsps->state = 14; /* parse null: nu */
4198 break;
4199
4200 case '0':
4201 jsps->state = 17; /* parse number: 0 */
4202 if ((jsps->temp = rcs_create (5)) == NULL)
4203 {
4204 return JSON_MEMORY;
4205 }
4206 if (rcs_catc ((jsps->temp), '0') != RS_OK)
4207 {
4208 return JSON_MEMORY;
4209 }
4210 break;
4211
4212 case '1':
4213 case '2':
4214 case '3':
4215 case '4':
4216 case '5':
4217 case '6':
4218 case '7':
4219 case '8':
4220 case '9':
4221 jsps->state = 24; /* parse number: decimal */
4222 if ((jsps->temp = rcs_create (5)) == NULL)
4223 {
4224 return JSON_MEMORY;
4225 }
4226 if (rcs_catc ((jsps->temp), c) != RS_OK)
4227 {
4228 return JSON_MEMORY;
4229 }
4230 break;
4231
4232 case '-':
4233 jsps->state = 23; /* number: */
4234 if ((jsps->temp = rcs_create (RSTRING_DEFAULT)) == NULL)
4235 {
4236 return JSON_MEMORY;
4237 }
4238 if (rcs_catc ((jsps->temp), '-') != RS_OK)
4239 {
4240 return JSON_MEMORY;
4241 }
4242 break;
4243
4244 default:
4245 return JSON_ILLEGAL_CHARACTER;
4246 break;
4247 }
4248 return JSON_OK;
4249 }
4250
4251 //return JSON_UNKNOWN_PROBLEM;
4252 }
4253
4254
4255 json_t *
4256 json_find_first_label (const json_t * object, const char *text_label)
4257 {
4258 json_t *cursor;
4259
4260 assert (object != NULL);
4261 assert (text_label != NULL);
4262 assert (object->type == JSON_OBJECT);
4263
4264 for (cursor = object->child; cursor != NULL; cursor = cursor->next)
4265 {
4266 if (strcmp (cursor->text, text_label) == 0)
4267 break;
4268 }
4269 return cursor;
4270 }