Debian Codegraph @

texk/web2c/mfluadir/otfcc/dep/extern/json.c

raw
1 /* vim: set et ts=3 sw=3 sts=3 ft=c:
2 *
3 * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
4 * https://github.com/udp/json-parser
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "json.h"
31
32 #ifdef _MSC_VER
33 #ifndef _CRT_SECURE_NO_WARNINGS
34 #define _CRT_SECURE_NO_WARNINGS
35 #endif
36 #endif
37
38 const struct _json_value json_value_none;
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <math.h>
44
45 typedef unsigned int json_uchar;
46
47 static unsigned char hex_value (json_char c)
48 {
49 if (isdigit(c))
50 return c - '0';
51
52 switch (c) {
53 case 'a': case 'A': return 0x0A;
54 case 'b': case 'B': return 0x0B;
55 case 'c': case 'C': return 0x0C;
56 case 'd': case 'D': return 0x0D;
57 case 'e': case 'E': return 0x0E;
58 case 'f': case 'F': return 0x0F;
59 default: return 0xFF;
60 }
61 }
62
63 typedef struct
64 {
65 unsigned long used_memory;
66
67 unsigned int uint_max;
68 unsigned long ulong_max;
69
70 json_settings settings;
71 int first_pass;
72
73 const json_char * ptr;
74 unsigned int cur_line, cur_col;
75
76 } json_state;
77
78 static void * default_alloc (size_t size, int zero, void * user_data)
79 {
80 return zero ? calloc (1, size) : malloc (size);
81 }
82
83 static void default_free (void * ptr, void * user_data)
84 {
85 free (ptr);
86 }
87
88 static void * json_alloc (json_state * state, unsigned long size, int zero)
89 {
90 if ((state->ulong_max - state->used_memory) < size)
91 return 0;
92
93 if (state->settings.max_memory
94 && (state->used_memory += size) > state->settings.max_memory)
95 {
96 return 0;
97 }
98
99 return state->settings.mem_alloc (size, zero, state->settings.user_data);
100 }
101
102 static int new_value (json_state * state,
103 json_value ** top, json_value ** root, json_value ** alloc,
104 json_type type)
105 {
106 json_value * value;
107 int values_size;
108
109 if (!state->first_pass)
110 {
111 value = *top = *alloc;
112 *alloc = (*alloc)->_reserved.next_alloc;
113
114 if (!*root)
115 *root = value;
116
117 switch (value->type)
118 {
119 case json_array:
120
121 if (value->u.array.length == 0)
122 break;
123
124 if (! (value->u.array.values = (json_value **) json_alloc
125 (state, value->u.array.length * sizeof (json_value *), 0)) )
126 {
127 return 0;
128 }
129
130 value->u.array.length = 0;
131 break;
132
133 case json_object:
134
135 if (value->u.object.length == 0)
136 break;
137
138 values_size = sizeof (*value->u.object.values) * value->u.object.length;
139
140 if (! (value->u.object.values = (json_object_entry *) json_alloc
141 (state, values_size + ((uintptr_t) value->u.object.values), 0)) )
142 {
143 return 0;
144 }
145
146 value->_reserved.object_mem = (*(char **) &value->u.object.values) + values_size;
147
148 value->u.object.length = 0;
149 break;
150
151 case json_string:
152
153 if (! (value->u.string.ptr = (json_char *) json_alloc
154 (state, (value->u.string.length + 1) * sizeof (json_char), 0)) )
155 {
156 return 0;
157 }
158
159 value->u.string.length = 0;
160 break;
161
162 default:
163 break;
164 };
165
166 return 1;
167 }
168
169 if (! (value = (json_value *) json_alloc
170 (state, sizeof (json_value) + state->settings.value_extra, 1)))
171 {
172 return 0;
173 }
174
175 if (!*root)
176 *root = value;
177
178 value->type = type;
179 value->parent = *top;
180
181 #ifdef JSON_TRACK_SOURCE
182 value->line = state->cur_line;
183 value->col = state->cur_col;
184 #endif
185
186 if (*alloc)
187 (*alloc)->_reserved.next_alloc = value;
188
189 *alloc = *top = value;
190
191 return 1;
192 }
193
194 #define whitespace \
195 case '\n': ++ state.cur_line; state.cur_col = 0; \
196 case ' ': case '\t': case '\r'
197
198 #define string_add(b) \
199 do { if (!state.first_pass) string [string_length] = b; ++ string_length; } while (0);
200
201 #define line_and_col \
202 state.cur_line, state.cur_col
203
204 static const long
205 flag_next = 1 << 0,
206 flag_reproc = 1 << 1,
207 flag_need_comma = 1 << 2,
208 flag_seek_value = 1 << 3,
209 flag_escaped = 1 << 4,
210 flag_string = 1 << 5,
211 flag_need_colon = 1 << 6,
212 flag_done = 1 << 7,
213 flag_num_negative = 1 << 8,
214 flag_num_zero = 1 << 9,
215 flag_num_e = 1 << 10,
216 flag_num_e_got_sign = 1 << 11,
217 flag_num_e_negative = 1 << 12,
218 flag_line_comment = 1 << 13,
219 flag_block_comment = 1 << 14;
220
221 json_value * json_parse_ex (json_settings * settings,
222 const json_char * json,
223 size_t length,
224 char * error_buf)
225 {
226 json_char error [json_error_max];
227 const json_char * end;
228 json_value * top, * root, * alloc = 0;
229 json_state state;
230 state.used_memory = 0;
231 state.uint_max = 0;
232 state.ulong_max = 0;
233 state.settings.max_memory = 0;
234 state.settings.mem_alloc = NULL;
235 state.settings.mem_free = NULL;
236 state.settings.settings = 0;
237 state.settings.user_data = NULL;
238 state.settings.value_extra = 0;
239 state.first_pass = 0;
240 state.ptr = NULL;
241 state.cur_col = 0;
242 state.cur_line = 0;
243
244 long flags;
245 long num_digits = 0, num_e = 0;
246 json_int_t num_fraction = 0;
247
248 /* Skip UTF-8 BOM
249 */
250 if (length >= 3 && ((unsigned char) json [0]) == 0xEF
251 && ((unsigned char) json [1]) == 0xBB
252 && ((unsigned char) json [2]) == 0xBF)
253 {
254 json += 3;
255 length -= 3;
256 }
257
258 error[0] = '\0';
259 end = (json + length);
260
261 memcpy (&state.settings, settings, sizeof (json_settings));
262
263 if (!state.settings.mem_alloc)
264 state.settings.mem_alloc = default_alloc;
265
266 if (!state.settings.mem_free)
267 state.settings.mem_free = default_free;
268
269 memset (&state.uint_max, 0xFF, sizeof (state.uint_max));
270 memset (&state.ulong_max, 0xFF, sizeof (state.ulong_max));
271
272 state.uint_max -= 8; /* limit of how much can be added before next check */
273 state.ulong_max -= 8;
274
275 for (state.first_pass = 1; state.first_pass >= 0; -- state.first_pass)
276 {
277 json_uchar uchar;
278 unsigned char uc_b1, uc_b2, uc_b3, uc_b4;
279 json_char * string = 0;
280 unsigned int string_length = 0;
281
282 top = root = 0;
283 flags = flag_seek_value;
284
285 state.cur_line = 1;
286
287 for (state.ptr = json ;; ++ state.ptr)
288 {
289 json_char b = (state.ptr == end ? 0 : *state.ptr);
290
291 if (flags & flag_string)
292 {
293 if (!b)
294 { sprintf (error, "Unexpected EOF in string (at %d:%d)", line_and_col);
295 goto e_failed;
296 }
297
298 if (string_length > state.uint_max)
299 goto e_overflow;
300
301 if (flags & flag_escaped)
302 {
303 flags &= ~ flag_escaped;
304
305 switch (b)
306 {
307 case 'b': string_add ('\b'); break;
308 case 'f': string_add ('\f'); break;
309 case 'n': string_add ('\n'); break;
310 case 'r': string_add ('\r'); break;
311 case 't': string_add ('\t'); break;
312 case 'u':
313
314 if (end - state.ptr < 4 ||
315 (uc_b1 = hex_value (*++ state.ptr)) == 0xFF ||
316 (uc_b2 = hex_value (*++ state.ptr)) == 0xFF ||
317 (uc_b3 = hex_value (*++ state.ptr)) == 0xFF ||
318 (uc_b4 = hex_value (*++ state.ptr)) == 0xFF)
319 {
320 sprintf (error, "Invalid character value `%c` (at %d:%d)", b, line_and_col);
321 goto e_failed;
322 }
323
324 uc_b1 = (uc_b1 << 4) | uc_b2;
325 uc_b2 = (uc_b3 << 4) | uc_b4;
326 uchar = (uc_b1 << 8) | uc_b2;
327
328 if ((uchar & 0xF800) == 0xD800) {
329 json_uchar uchar2;
330
331 if (end - state.ptr < 6 || (*++ state.ptr) != '\\' || (*++ state.ptr) != 'u' ||
332 (uc_b1 = hex_value (*++ state.ptr)) == 0xFF ||
333 (uc_b2 = hex_value (*++ state.ptr)) == 0xFF ||
334 (uc_b3 = hex_value (*++ state.ptr)) == 0xFF ||
335 (uc_b4 = hex_value (*++ state.ptr)) == 0xFF)
336 {
337 sprintf (error, "Invalid character value `%c` (at %d:%d)", b, line_and_col);
338 goto e_failed;
339 }
340
341 uc_b1 = (uc_b1 << 4) | uc_b2;
342 uc_b2 = (uc_b3 << 4) | uc_b4;
343 uchar2 = (uc_b1 << 8) | uc_b2;
344
345 uchar = 0x010000 + ((uchar & 0x3FF) << 10) | (uchar2 & 0x3FF);
346 }
347
348 if (sizeof (json_char) >= sizeof (json_uchar) || (uchar <= 0x7F))
349 {
350 string_add ((json_char) uchar);
351 break;
352 }
353
354 if (uchar <= 0x7FF)
355 {
356 if (state.first_pass)
357 string_length += 2;
358 else
359 { string [string_length ++] = 0xC0 | (uchar >> 6);
360 string [string_length ++] = 0x80 | (uchar & 0x3F);
361 }
362
363 break;
364 }
365
366 if (uchar <= 0xFFFF) {
367 if (state.first_pass)
368 string_length += 3;
369 else
370 { string [string_length ++] = 0xE0 | (uchar >> 12);
371 string [string_length ++] = 0x80 | ((uchar >> 6) & 0x3F);
372 string [string_length ++] = 0x80 | (uchar & 0x3F);
373 }
374
375 break;
376 }
377
378 if (state.first_pass)
379 string_length += 4;
380 else
381 { string [string_length ++] = 0xF0 | (uchar >> 18);
382 string [string_length ++] = 0x80 | ((uchar >> 12) & 0x3F);
383 string [string_length ++] = 0x80 | ((uchar >> 6) & 0x3F);
384 string [string_length ++] = 0x80 | (uchar & 0x3F);
385 }
386
387 break;
388
389 default:
390 string_add (b);
391 };
392
393 continue;
394 }
395
396 if (b == '\\')
397 {
398 flags |= flag_escaped;
399 continue;
400 }
401
402 if (b == '"')
403 {
404 if (!state.first_pass)
405 string [string_length] = 0;
406
407 flags &= ~ flag_string;
408 string = 0;
409
410 switch (top->type)
411 {
412 case json_string:
413
414 top->u.string.length = string_length;
415 flags |= flag_next;
416
417 break;
418
419 case json_object:
420
421 if (state.first_pass)
422 (*(json_char **) &top->u.object.values) += string_length + 1;
423 else
424 {
425 top->u.object.values [top->u.object.length].name
426 = (json_char *) top->_reserved.object_mem;
427
428 top->u.object.values [top->u.object.length].name_length
429 = string_length;
430
431 (*(json_char **) &top->_reserved.object_mem) += string_length + 1;
432 }
433
434 flags |= flag_seek_value | flag_need_colon;
435 continue;
436
437 default:
438 break;
439 };
440 }
441 else
442 {
443 string_add (b);
444 continue;
445 }
446 }
447
448 if (state.settings.settings & json_enable_comments)
449 {
450 if (flags & (flag_line_comment | flag_block_comment))
451 {
452 if (flags & flag_line_comment)
453 {
454 if (b == '\r' || b == '\n' || !b)
455 {
456 flags &= ~ flag_line_comment;
457 -- state.ptr; /* so null can be reproc'd */
458 }
459
460 continue;
461 }
462
463 if (flags & flag_block_comment)
464 {
465 if (!b)
466 { sprintf (error, "%d:%d: Unexpected EOF in block comment", line_and_col);
467 goto e_failed;
468 }
469
470 if (b == '*' && state.ptr < (end - 1) && state.ptr [1] == '/')
471 {
472 flags &= ~ flag_block_comment;
473 ++ state.ptr; /* skip closing sequence */
474 }
475
476 continue;
477 }
478 }
479 else if (b == '/')
480 {
481 if (! (flags & (flag_seek_value | flag_done)) && top->type != json_object)
482 { sprintf (error, "%d:%d: Comment not allowed here", line_and_col);
483 goto e_failed;
484 }
485
486 if (++ state.ptr == end)
487 { sprintf (error, "%d:%d: EOF unexpected", line_and_col);
488 goto e_failed;
489 }
490
491 switch (b = *state.ptr)
492 {
493 case '/':
494 flags |= flag_line_comment;
495 continue;
496
497 case '*':
498 flags |= flag_block_comment;
499 continue;
500
501 default:
502 sprintf (error, "%d:%d: Unexpected `%c` in comment opening sequence", line_and_col, b);
503 goto e_failed;
504 };
505 }
506 }
507
508 if (flags & flag_done)
509 {
510 if (!b)
511 break;
512
513 switch (b)
514 {
515 whitespace:
516 continue;
517
518 default:
519
520 sprintf (error, "%d:%d: Trailing garbage: `%c`",
521 state.cur_line, state.cur_col, b);
522
523 goto e_failed;
524 };
525 }
526
527 if (flags & flag_seek_value)
528 {
529 switch (b)
530 {
531 whitespace:
532 continue;
533
534 case ']':
535
536 if (top && top->type == json_array)
537 flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next;
538 else
539 { sprintf (error, "%d:%d: Unexpected ]", line_and_col);
540 goto e_failed;
541 }
542
543 break;
544
545 default:
546
547 if (flags & flag_need_comma)
548 {
549 if (b == ',')
550 { flags &= ~ flag_need_comma;
551 continue;
552 }
553 else
554 {
555 sprintf (error, "%d:%d: Expected , before %c",
556 state.cur_line, state.cur_col, b);
557
558 goto e_failed;
559 }
560 }
561
562 if (flags & flag_need_colon)
563 {
564 if (b == ':')
565 { flags &= ~ flag_need_colon;
566 continue;
567 }
568 else
569 {
570 sprintf (error, "%d:%d: Expected : before %c",
571 state.cur_line, state.cur_col, b);
572
573 goto e_failed;
574 }
575 }
576
577 flags &= ~ flag_seek_value;
578
579 switch (b)
580 {
581 case '{':
582
583 if (!new_value (&state, &top, &root, &alloc, json_object))
584 goto e_alloc_failure;
585
586 continue;
587
588 case '[':
589
590 if (!new_value (&state, &top, &root, &alloc, json_array))
591 goto e_alloc_failure;
592
593 flags |= flag_seek_value;
594 continue;
595
596 case '"':
597
598 if (!new_value (&state, &top, &root, &alloc, json_string))
599 goto e_alloc_failure;
600
601 flags |= flag_string;
602
603 string = top->u.string.ptr;
604 string_length = 0;
605
606 continue;
607
608 case 't':
609
610 if ((end - state.ptr) < 3 || *(++ state.ptr) != 'r' ||
611 *(++ state.ptr) != 'u' || *(++ state.ptr) != 'e')
612 {
613 goto e_unknown_value;
614 }
615
616 if (!new_value (&state, &top, &root, &alloc, json_boolean))
617 goto e_alloc_failure;
618
619 top->u.boolean = 1;
620
621 flags |= flag_next;
622 break;
623
624 case 'f':
625
626 if ((end - state.ptr) < 4 || *(++ state.ptr) != 'a' ||
627 *(++ state.ptr) != 'l' || *(++ state.ptr) != 's' ||
628 *(++ state.ptr) != 'e')
629 {
630 goto e_unknown_value;
631 }
632
633 if (!new_value (&state, &top, &root, &alloc, json_boolean))
634 goto e_alloc_failure;
635
636 flags |= flag_next;
637 break;
638
639 case 'n':
640
641 if ((end - state.ptr) < 3 || *(++ state.ptr) != 'u' ||
642 *(++ state.ptr) != 'l' || *(++ state.ptr) != 'l')
643 {
644 goto e_unknown_value;
645 }
646
647 if (!new_value (&state, &top, &root, &alloc, json_null))
648 goto e_alloc_failure;
649
650 flags |= flag_next;
651 break;
652
653 default:
654
655 if (isdigit (b) || b == '-')
656 {
657 if (!new_value (&state, &top, &root, &alloc, json_integer))
658 goto e_alloc_failure;
659
660 if (!state.first_pass)
661 {
662 while (isdigit (b) || b == '+' || b == '-'
663 || b == 'e' || b == 'E' || b == '.')
664 {
665 if ( (++ state.ptr) == end)
666 {
667 b = 0;
668 break;
669 }
670
671 b = *state.ptr;
672 }
673
674 flags |= flag_next | flag_reproc;
675 break;
676 }
677
678 flags &= ~ (flag_num_negative | flag_num_e |
679 flag_num_e_got_sign | flag_num_e_negative |
680 flag_num_zero);
681
682 num_digits = 0;
683 num_fraction = 0;
684 num_e = 0;
685
686 if (b != '-')
687 {
688 flags |= flag_reproc;
689 break;
690 }
691
692 flags |= flag_num_negative;
693 continue;
694 }
695 else
696 { sprintf (error, "%d:%d: Unexpected %c when seeking value", line_and_col, b);
697 goto e_failed;
698 }
699 };
700 };
701 }
702 else
703 {
704 switch (top->type)
705 {
706 case json_object:
707
708 switch (b)
709 {
710 whitespace:
711 continue;
712
713 case '"':
714
715 if (flags & flag_need_comma)
716 { sprintf (error, "%d:%d: Expected , before \"", line_and_col);
717 goto e_failed;
718 }
719
720 flags |= flag_string;
721
722 string = (json_char *) top->_reserved.object_mem;
723 string_length = 0;
724
725 break;
726
727 case '}':
728
729 flags = (flags & ~ flag_need_comma) | flag_next;
730 break;
731
732 case ',':
733
734 if (flags & flag_need_comma)
735 {
736 flags &= ~ flag_need_comma;
737 break;
738 }
739
740 default:
741 sprintf (error, "%d:%d: Unexpected `%c` in object", line_and_col, b);
742 goto e_failed;
743 };
744
745 break;
746
747 case json_integer:
748 case json_double:
749
750 if (isdigit (b))
751 {
752 ++ num_digits;
753
754 if (top->type == json_integer || flags & flag_num_e)
755 {
756 if (! (flags & flag_num_e))
757 {
758 if (flags & flag_num_zero)
759 { sprintf (error, "%d:%d: Unexpected `0` before `%c`", line_and_col, b);
760 goto e_failed;
761 }
762
763 if (num_digits == 1 && b == '0')
764 flags |= flag_num_zero;
765 }
766 else
767 {
768 flags |= flag_num_e_got_sign;
769 num_e = (num_e * 10) + (b - '0');
770 continue;
771 }
772
773 top->u.integer = (top->u.integer * 10) + (b - '0');
774 continue;
775 }
776
777 num_fraction = (num_fraction * 10) + (b - '0');
778 continue;
779 }
780
781 if (b == '+' || b == '-')
782 {
783 if ( (flags & flag_num_e) && !(flags & flag_num_e_got_sign))
784 {
785 flags |= flag_num_e_got_sign;
786
787 if (b == '-')
788 flags |= flag_num_e_negative;
789
790 continue;
791 }
792 }
793 else if (b == '.' && top->type == json_integer)
794 {
795 if (!num_digits)
796 { sprintf (error, "%d:%d: Expected digit before `.`", line_and_col);
797 goto e_failed;
798 }
799
800 top->type = json_double;
801 top->u.dbl = (double) top->u.integer;
802
803 num_digits = 0;
804 continue;
805 }
806
807 if (! (flags & flag_num_e))
808 {
809 if (top->type == json_double)
810 {
811 if (!num_digits)
812 { sprintf (error, "%d:%d: Expected digit after `.`", line_and_col);
813 goto e_failed;
814 }
815
816 top->u.dbl += ((double) num_fraction) / (pow (10.0, (double) num_digits));
817 }
818
819 if (b == 'e' || b == 'E')
820 {
821 flags |= flag_num_e;
822
823 if (top->type == json_integer)
824 {
825 top->type = json_double;
826 top->u.dbl = (double) top->u.integer;
827 }
828
829 num_digits = 0;
830 flags &= ~ flag_num_zero;
831
832 continue;
833 }
834 }
835 else
836 {
837 if (!num_digits)
838 { sprintf (error, "%d:%d: Expected digit after `e`", line_and_col);
839 goto e_failed;
840 }
841
842 top->u.dbl *= pow (10.0, (double)
843 (flags & flag_num_e_negative ? - num_e : num_e));
844 }
845
846 if (flags & flag_num_negative)
847 {
848 if (top->type == json_integer)
849 top->u.integer = - top->u.integer;
850 else
851 top->u.dbl = - top->u.dbl;
852 }
853
854 flags |= flag_next | flag_reproc;
855 break;
856
857 default:
858 break;
859 };
860 }
861
862 if (flags & flag_reproc)
863 {
864 flags &= ~ flag_reproc;
865 -- state.ptr;
866 }
867
868 if (flags & flag_next)
869 {
870 flags = (flags & ~ flag_next) | flag_need_comma;
871
872 if (!top->parent)
873 {
874 /* root value done */
875
876 flags |= flag_done;
877 continue;
878 }
879
880 if (top->parent->type == json_array)
881 flags |= flag_seek_value;
882
883 if (!state.first_pass)
884 {
885 json_value * parent = top->parent;
886
887 switch (parent->type)
888 {
889 case json_object:
890
891 parent->u.object.values
892 [parent->u.object.length].value = top;
893
894 break;
895
896 case json_array:
897
898 parent->u.array.values
899 [parent->u.array.length] = top;
900
901 break;
902
903 default:
904 break;
905 };
906 }
907
908 if ( (++ top->parent->u.array.length) > state.uint_max)
909 goto e_overflow;
910
911 top = top->parent;
912
913 continue;
914 }
915 }
916
917 alloc = root;
918 }
919
920 return root;
921
922 e_unknown_value:
923
924 sprintf (error, "%d:%d: Unknown value", line_and_col);
925 goto e_failed;
926
927 e_alloc_failure:
928
929 strcpy (error, "Memory allocation failure");
930 goto e_failed;
931
932 e_overflow:
933
934 sprintf (error, "%d:%d: Too long (caught overflow)", line_and_col);
935 goto e_failed;
936
937 e_failed:
938
939 if (error_buf)
940 {
941 if (*error)
942 strcpy (error_buf, error);
943 else
944 strcpy (error_buf, "Unknown error");
945 }
946
947 if (state.first_pass)
948 alloc = root;
949
950 while (alloc)
951 {
952 top = alloc->_reserved.next_alloc;
953 state.settings.mem_free (alloc, state.settings.user_data);
954 alloc = top;
955 }
956
957 if (!state.first_pass)
958 json_value_free_ex (&state.settings, root);
959
960 return 0;
961 }
962
963 json_value * json_parse (const json_char * json, size_t length)
964 {
965 json_settings settings;
966 settings.max_memory = 0;
967 settings.settings = 0;
968 settings.mem_alloc = NULL;
969 settings.mem_free = NULL;
970 settings.user_data = NULL;
971 settings.value_extra = 0;
972 return json_parse_ex (&settings, json, length, 0);
973 }
974
975 void json_value_free_ex (json_settings * settings, json_value * value)
976 {
977 json_value * cur_value;
978
979 if (!value)
980 return;
981
982 value->parent = 0;
983
984 while (value)
985 {
986 switch (value->type)
987 {
988 case json_array:
989
990 if (!value->u.array.length)
991 {
992 settings->mem_free (value->u.array.values, settings->user_data);
993 break;
994 }
995
996 value = value->u.array.values [-- value->u.array.length];
997 continue;
998
999 case json_object:
1000
1001 if (!value->u.object.length)
1002 {
1003 settings->mem_free (value->u.object.values, settings->user_data);
1004 break;
1005 }
1006
1007 value = value->u.object.values [-- value->u.object.length].value;
1008 continue;
1009
1010 case json_string:
1011 #ifdef CARYLL_USE_PRE_SERIALIZED
1012 case json_pre_serialized:
1013 #endif
1014 settings->mem_free (value->u.string.ptr, settings->user_data);
1015 break;
1016
1017 default:
1018 break;
1019 };
1020
1021 cur_value = value;
1022 value = value->parent;
1023 settings->mem_free (cur_value, settings->user_data);
1024 }
1025 }
1026
1027 void json_value_free (json_value * value)
1028 {
1029 json_settings settings;
1030 settings.max_memory = 0;
1031 settings.settings = 0;
1032 settings.mem_alloc = NULL;
1033 settings.mem_free = NULL;
1034 settings.user_data = NULL;
1035 settings.value_extra = 0;
1036 settings.mem_free = default_free;
1037 json_value_free_ex (&settings, value);
1038 }
1039