TianoCore EDK2 master
Loading...
Searching...
No Matches
load.c
1/*
2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3 *
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6
7 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
8 Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
9
10 SPDX-License-Identifier: BSD-2-Clause-Patent AND MIT
11 */
12
13#ifndef _GNU_SOURCE
14#define _GNU_SOURCE
15#endif
16
17#include "jansson_private.h"
18
19#include <assert.h>
20#include <errno.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27#endif
28
29#include "jansson.h"
30#include "strbuffer.h"
31#include "utf.h"
32
33#define STREAM_STATE_OK 0
34#define STREAM_STATE_EOF -1
35#define STREAM_STATE_ERROR -2
36
37#define TOKEN_INVALID -1
38#define TOKEN_EOF 0
39#define TOKEN_STRING 256
40#define TOKEN_INTEGER 257
41#define TOKEN_REAL 258
42#define TOKEN_TRUE 259
43#define TOKEN_FALSE 260
44#define TOKEN_NULL 261
45
46/* Locale independent versions of isxxx() functions */
47#define l_isupper(c) ('A' <= (c) && (c) <= 'Z')
48#define l_islower(c) ('a' <= (c) && (c) <= 'z')
49#define l_isalpha(c) (l_isupper(c) || l_islower(c))
50#define l_isdigit(c) ('0' <= (c) && (c) <= '9')
51#define l_isxdigit(c) \
52 (l_isdigit(c) || ('A' <= (c) && (c) <= 'F') || ('a' <= (c) && (c) <= 'f'))
53
54/* Read one byte from stream, convert to unsigned char, then int, and
55 return. return EOF on end of file. This corresponds to the
56 behaviour of fgetc(). */
57typedef int (*get_func)(
58 void *data
59 );
60
61typedef struct {
62 get_func get;
63 void *data;
64 char buffer[5];
65 size_t buffer_pos;
66 int state;
67 int line;
68 int column, last_column;
69 size_t position;
70} stream_t;
71
72typedef struct {
73 stream_t stream;
74 strbuffer_t saved_text;
75 size_t flags;
76 size_t depth;
77 int token;
78 union {
79 struct {
80 char *val;
81 size_t len;
82 } string;
83 json_int_t integer;
84 double real;
85 } value;
86} lex_t;
87
88#define stream_to_lex(stream) container_of(stream, lex_t, stream)
89
90/*** error reporting ***/
91
92static void
93error_set (
94 json_error_t *error,
95 const lex_t *lex,
96 enum json_error_code code,
97 const char *msg,
98 ...
99 )
100{
101 va_list ap;
102 char msg_text[JSON_ERROR_TEXT_LENGTH];
103 char msg_with_context[JSON_ERROR_TEXT_LENGTH];
104
105 int line = -1, col = -1;
106 size_t pos = 0;
107 const char *result = msg_text;
108
109 if (!error) {
110 return;
111 }
112
113 va_start (ap, msg);
114 vsnprintf (msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
115 msg_text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
116 va_end (ap);
117
118 if (lex) {
119 const char *saved_text = strbuffer_value (&lex->saved_text);
120
121 line = lex->stream.line;
122 col = lex->stream.column;
123 pos = lex->stream.position;
124
125 if (saved_text && saved_text[0]) {
126 if (lex->saved_text.length <= 20) {
127 snprintf (
128 msg_with_context,
129 JSON_ERROR_TEXT_LENGTH,
130 "%s near '%s'",
131 msg_text,
132 saved_text
133 );
134 msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
135 result = msg_with_context;
136 }
137 } else {
138 if (code == json_error_invalid_syntax) {
139 /* More specific error code for premature end of file. */
140 code = json_error_premature_end_of_input;
141 }
142
143 if (lex->stream.state == STREAM_STATE_ERROR) {
144 /* No context for UTF-8 decoding errors */
145 result = msg_text;
146 } else {
147 snprintf (
148 msg_with_context,
149 JSON_ERROR_TEXT_LENGTH,
150 "%s near end of file",
151 msg_text
152 );
153 msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
154 result = msg_with_context;
155 }
156 }
157 }
158
159 jsonp_error_set (error, line, col, pos, code, "%s", result);
160}
161
162/*** lexical analyzer ***/
163
164static void
165stream_init (
166 stream_t *stream,
167 get_func get,
168 void *data
169 )
170{
171 stream->get = get;
172 stream->data = data;
173 stream->buffer[0] = '\0';
174 stream->buffer_pos = 0;
175
176 stream->state = STREAM_STATE_OK;
177 stream->line = 1;
178 stream->column = 0;
179 stream->position = 0;
180}
181
182static int
183stream_get (
184 stream_t *stream,
185 json_error_t *error
186 )
187{
188 int c;
189
190 if (stream->state != STREAM_STATE_OK) {
191 return stream->state;
192 }
193
194 if (!stream->buffer[stream->buffer_pos]) {
195 c = stream->get (stream->data);
196 if (c == EOF) {
197 stream->state = STREAM_STATE_EOF;
198 return STREAM_STATE_EOF;
199 }
200
201 stream->buffer[0] = c;
202 stream->buffer_pos = 0;
203
204 if ((0x80 <= c) && (c <= 0xFF)) {
205 /* multi-byte UTF-8 sequence */
206 size_t i, count;
207
208 count = utf8_check_first (c);
209 if (!count) {
210 goto out;
211 }
212
213 assert (count >= 2);
214
215 for (i = 1; i < count; i++) {
216 stream->buffer[i] = stream->get (stream->data);
217 }
218
219 if (!utf8_check_full (stream->buffer, count, NULL)) {
220 goto out;
221 }
222
223 stream->buffer[count] = '\0';
224 } else {
225 stream->buffer[1] = '\0';
226 }
227 }
228
229 c = stream->buffer[stream->buffer_pos++];
230
231 stream->position++;
232 if (c == '\n') {
233 stream->line++;
234 stream->last_column = stream->column;
235 stream->column = 0;
236 } else if (utf8_check_first (c)) {
237 /* track the Unicode character column, so increment only if
238 this is the first character of a UTF-8 sequence */
239 stream->column++;
240 }
241
242 return c;
243
244out:
245 stream->state = STREAM_STATE_ERROR;
246 error_set (
247 error,
248 stream_to_lex (stream),
249 json_error_invalid_utf8,
250 "unable to decode byte 0x%x",
251 c
252 );
253 return STREAM_STATE_ERROR;
254}
255
256static void
257stream_unget (
258 stream_t *stream,
259 int c
260 )
261{
262 if ((c == STREAM_STATE_EOF) || (c == STREAM_STATE_ERROR)) {
263 return;
264 }
265
266 stream->position--;
267 if (c == '\n') {
268 stream->line--;
269 stream->column = stream->last_column;
270 } else if (utf8_check_first (c)) {
271 stream->column--;
272 }
273
274 assert (stream->buffer_pos > 0);
275 stream->buffer_pos--;
276 assert (stream->buffer[stream->buffer_pos] == c);
277}
278
279static int
280lex_get (
281 lex_t *lex,
282 json_error_t *error
283 )
284{
285 return stream_get (&lex->stream, error);
286}
287
288static void
289lex_save (
290 lex_t *lex,
291 int c
292 )
293{
294 strbuffer_append_byte (&lex->saved_text, c);
295}
296
297static int
298lex_get_save (
299 lex_t *lex,
300 json_error_t *error
301 )
302{
303 int c = stream_get (&lex->stream, error);
304
305 if ((c != STREAM_STATE_EOF) && (c != STREAM_STATE_ERROR)) {
306 lex_save (lex, c);
307 }
308
309 return c;
310}
311
312static void
313lex_unget (
314 lex_t *lex,
315 int c
316 )
317{
318 stream_unget (&lex->stream, c);
319}
320
321static void
322lex_unget_unsave (
323 lex_t *lex,
324 int c
325 )
326{
327 if ((c != STREAM_STATE_EOF) && (c != STREAM_STATE_ERROR)) {
328 /* Since we treat warnings as errors, when assertions are turned
329 * off the "d" variable would be set but never used. Which is
330 * treated as an error by GCC.
331 */
332 #ifndef NDEBUG
333 char d;
334 #endif
335 stream_unget (&lex->stream, c);
336 #ifndef NDEBUG
337 d =
338 #endif
339 strbuffer_pop (&lex->saved_text);
340 assert (c == d);
341 }
342}
343
344static void
345lex_save_cached (
346 lex_t *lex
347 )
348{
349 while (lex->stream.buffer[lex->stream.buffer_pos] != '\0') {
350 lex_save (lex, lex->stream.buffer[lex->stream.buffer_pos]);
351 lex->stream.buffer_pos++;
352 lex->stream.position++;
353 }
354}
355
356static void
357lex_free_string (
358 lex_t *lex
359 )
360{
361 jsonp_free (lex->value.string.val);
362 lex->value.string.val = NULL;
363 lex->value.string.len = 0;
364}
365
366/* assumes that str points to 'u' plus at least 4 valid hex digits */
367static int32_t
368decode_unicode_escape (
369 const char *str
370 )
371{
372 int i;
373 int32_t value = 0;
374
375 assert (str[0] == 'u');
376
377 for (i = 1; i <= 4; i++) {
378 char c = str[i];
379 value <<= 4;
380 if (l_isdigit (c)) {
381 value += c - '0';
382 } else if (l_islower (c)) {
383 value += c - 'a' + 10;
384 } else if (l_isupper (c)) {
385 value += c - 'A' + 10;
386 } else {
387 return -1;
388 }
389 }
390
391 return value;
392}
393
394static void
395lex_scan_string (
396 lex_t *lex,
397 json_error_t *error
398 )
399{
400 int c;
401 const char *p;
402 char *t;
403 int i;
404
405 lex->value.string.val = NULL;
406 lex->token = TOKEN_INVALID;
407
408 c = lex_get_save (lex, error);
409
410 while (c != '"') {
411 if (c == STREAM_STATE_ERROR) {
412 goto out;
413 } else if (c == STREAM_STATE_EOF) {
414 error_set (
415 error,
416 lex,
417 json_error_premature_end_of_input,
418 "premature end of input"
419 );
420 goto out;
421 } else if ((0 <= c) && (c <= 0x1F)) {
422 /* control character */
423 lex_unget_unsave (lex, c);
424 if (c == '\n') {
425 error_set (error, lex, json_error_invalid_syntax, "unexpected newline");
426 } else {
427 error_set (
428 error,
429 lex,
430 json_error_invalid_syntax,
431 "control character 0x%x",
432 c
433 );
434 }
435
436 goto out;
437 } else if (c == '\\') {
438 c = lex_get_save (lex, error);
439 if (c == 'u') {
440 c = lex_get_save (lex, error);
441 for (i = 0; i < 4; i++) {
442 if (!l_isxdigit (c)) {
443 error_set (
444 error,
445 lex,
446 json_error_invalid_syntax,
447 "invalid escape"
448 );
449 goto out;
450 }
451
452 c = lex_get_save (lex, error);
453 }
454 } else if ((c == '"') || (c == '\\') || (c == '/') || (c == 'b') || (c == 'f') ||
455 (c == 'n') || (c == 'r') || (c == 't'))
456 {
457 c = lex_get_save (lex, error);
458 } else {
459 error_set (error, lex, json_error_invalid_syntax, "invalid escape");
460 goto out;
461 }
462 } else {
463 c = lex_get_save (lex, error);
464 }
465 }
466
467 /* the actual value is at most of the same length as the source
468 string, because:
469 - shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte
470 - a single \uXXXX escape (length 6) is converted to at most 3 bytes
471 - two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
472 are converted to 4 bytes
473 */
474 t = jsonp_malloc (lex->saved_text.length + 1);
475 if (!t) {
476 /* this is not very nice, since TOKEN_INVALID is returned */
477 goto out;
478 }
479
480 lex->value.string.val = t;
481
482 /* + 1 to skip the " */
483 p = strbuffer_value (&lex->saved_text) + 1;
484
485 while (*p != '"') {
486 if (*p == '\\') {
487 p++;
488 if (*p == 'u') {
489 size_t length;
490 int32_t value;
491
492 value = decode_unicode_escape (p);
493 if (value < 0) {
494 error_set (
495 error,
496 lex,
497 json_error_invalid_syntax,
498 "invalid Unicode escape '%.6s'",
499 p - 1
500 );
501 goto out;
502 }
503
504 p += 5;
505
506 if ((0xD800 <= value) && (value <= 0xDBFF)) {
507 /* surrogate pair */
508 if ((*p == '\\') && (*(p + 1) == 'u')) {
509 int32_t value2 = decode_unicode_escape (++p);
510 if (value2 < 0) {
511 error_set (
512 error,
513 lex,
514 json_error_invalid_syntax,
515 "invalid Unicode escape '%.6s'",
516 p - 1
517 );
518 goto out;
519 }
520
521 p += 5;
522
523 if ((0xDC00 <= value2) && (value2 <= 0xDFFF)) {
524 /* valid second surrogate */
525 value =
526 ((value - 0xD800) << 10) + (value2 - 0xDC00) + 0x10000;
527 } else {
528 /* invalid second surrogate */
529 error_set (
530 error,
531 lex,
532 json_error_invalid_syntax,
533 "invalid Unicode '\\u%04X\\u%04X'",
534 value,
535 value2
536 );
537 goto out;
538 }
539 } else {
540 /* no second surrogate */
541 error_set (
542 error,
543 lex,
544 json_error_invalid_syntax,
545 "invalid Unicode '\\u%04X'",
546 value
547 );
548 goto out;
549 }
550 } else if ((0xDC00 <= value) && (value <= 0xDFFF)) {
551 error_set (
552 error,
553 lex,
554 json_error_invalid_syntax,
555 "invalid Unicode '\\u%04X'",
556 value
557 );
558 goto out;
559 }
560
561 if (utf8_encode (value, t, &length)) {
562 assert (0);
563 }
564
565 t += length;
566 } else {
567 switch (*p) {
568 case '"':
569 case '\\':
570 case '/':
571 *t = *p;
572 break;
573 case 'b':
574 *t = '\b';
575 break;
576 case 'f':
577 *t = '\f';
578 break;
579 case 'n':
580 *t = '\n';
581 break;
582 case 'r':
583 *t = '\r';
584 break;
585 case 't':
586 *t = '\t';
587 break;
588 default:
589 assert (0);
590 }
591
592 t++;
593 p++;
594 }
595 } else {
596 *(t++) = *(p++);
597 }
598 }
599
600 *t = '\0';
601 lex->value.string.len = t - lex->value.string.val;
602 lex->token = TOKEN_STRING;
603 return;
604
605out:
606 lex_free_string (lex);
607}
608
609#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
610 #if JSON_INTEGER_IS_LONG_LONG
611 #ifdef _MSC_VER /* Microsoft Visual Studio */
612#define json_strtoint _strtoi64
613 #else
614#define json_strtoint strtoll
615 #endif
616 #else
617#define json_strtoint strtol
618 #endif
619#endif
620
621static int
622lex_scan_number (
623 lex_t *lex,
624 int c,
625 json_error_t *error
626 )
627{
628 const char *saved_text;
629 char *end;
630 double doubleval;
631
632 lex->token = TOKEN_INVALID;
633
634 if (c == '-') {
635 c = lex_get_save (lex, error);
636 }
637
638 if (c == '0') {
639 c = lex_get_save (lex, error);
640 if (l_isdigit (c)) {
641 lex_unget_unsave (lex, c);
642 goto out;
643 }
644 } else if (l_isdigit (c)) {
645 do {
646 c = lex_get_save (lex, error);
647 } while (l_isdigit (c));
648 } else {
649 lex_unget_unsave (lex, c);
650 goto out;
651 }
652
653 if (!(lex->flags & JSON_DECODE_INT_AS_REAL) && (c != '.') && (c != 'E') && (c != 'e')) {
654 json_int_t intval;
655
656 lex_unget_unsave (lex, c);
657
658 saved_text = strbuffer_value (&lex->saved_text);
659
660 errno = 0;
661 intval = json_strtoint (saved_text, &end, 10);
662 if (errno == ERANGE) {
663 if (intval < 0) {
664 error_set (
665 error,
666 lex,
667 json_error_numeric_overflow,
668 "too big negative integer"
669 );
670 } else {
671 error_set (error, lex, json_error_numeric_overflow, "too big integer");
672 }
673
674 goto out;
675 }
676
677 assert (end == saved_text + lex->saved_text.length);
678
679 lex->token = TOKEN_INTEGER;
680 lex->value.integer = intval;
681 return 0;
682 }
683
684 if (c == '.') {
685 c = lex_get (lex, error);
686 if (!l_isdigit (c)) {
687 lex_unget (lex, c);
688 goto out;
689 }
690
691 lex_save (lex, c);
692
693 do {
694 c = lex_get_save (lex, error);
695 } while (l_isdigit (c));
696 }
697
698 if ((c == 'E') || (c == 'e')) {
699 c = lex_get_save (lex, error);
700 if ((c == '+') || (c == '-')) {
701 c = lex_get_save (lex, error);
702 }
703
704 if (!l_isdigit (c)) {
705 lex_unget_unsave (lex, c);
706 goto out;
707 }
708
709 do {
710 c = lex_get_save (lex, error);
711 } while (l_isdigit (c));
712 }
713
714 lex_unget_unsave (lex, c);
715
716 if (jsonp_strtod (&lex->saved_text, &doubleval)) {
717 error_set (error, lex, json_error_numeric_overflow, "real number overflow");
718 goto out;
719 }
720
721 lex->token = TOKEN_INTEGER;
722 lex->value.integer = doubleval;
723 return 0;
724
725out:
726 return -1;
727}
728
729static int
730lex_scan (
731 lex_t *lex,
732 json_error_t *error
733 )
734{
735 int c;
736
737 strbuffer_clear (&lex->saved_text);
738
739 if (lex->token == TOKEN_STRING) {
740 lex_free_string (lex);
741 }
742
743 do {
744 c = lex_get (lex, error);
745 } while (c == ' ' || c == '\t' || c == '\n' || c == '\r');
746
747 if (c == STREAM_STATE_EOF) {
748 lex->token = TOKEN_EOF;
749 goto out;
750 }
751
752 if (c == STREAM_STATE_ERROR) {
753 lex->token = TOKEN_INVALID;
754 goto out;
755 }
756
757 lex_save (lex, c);
758
759 if ((c == '{') || (c == '}') || (c == '[') || (c == ']') || (c == ':') || (c == ',')) {
760 lex->token = c;
761 } else if (c == '"') {
762 lex_scan_string (lex, error);
763 } else if (l_isdigit (c) || (c == '-')) {
764 if (lex_scan_number (lex, c, error)) {
765 goto out;
766 }
767 } else if (l_isalpha (c)) {
768 /* eat up the whole identifier for clearer error messages */
769 const char *saved_text;
770
771 do {
772 c = lex_get_save (lex, error);
773 } while (l_isalpha (c));
774
775 lex_unget_unsave (lex, c);
776
777 saved_text = strbuffer_value (&lex->saved_text);
778
779 if (strcmp (saved_text, "true") == 0) {
780 lex->token = TOKEN_TRUE;
781 } else if (strcmp (saved_text, "false") == 0) {
782 lex->token = TOKEN_FALSE;
783 } else if (strcmp (saved_text, "null") == 0) {
784 lex->token = TOKEN_NULL;
785 } else {
786 lex->token = TOKEN_INVALID;
787 }
788 } else {
789 /* save the rest of the input UTF-8 sequence to get an error
790 message of valid UTF-8 */
791 lex_save_cached (lex);
792 lex->token = TOKEN_INVALID;
793 }
794
795out:
796 return lex->token;
797}
798
799static char *
800lex_steal_string (
801 lex_t *lex,
802 size_t *out_len
803 )
804{
805 char *result = NULL;
806
807 if (lex->token == TOKEN_STRING) {
808 result = lex->value.string.val;
809 *out_len = lex->value.string.len;
810 lex->value.string.val = NULL;
811 lex->value.string.len = 0;
812 }
813
814 return result;
815}
816
817static int
818lex_init (
819 lex_t *lex,
820 get_func get,
821 size_t flags,
822 void *data
823 )
824{
825 stream_init (&lex->stream, get, data);
826 if (strbuffer_init (&lex->saved_text)) {
827 return -1;
828 }
829
830 lex->flags = flags;
831 lex->token = TOKEN_INVALID;
832 return 0;
833}
834
835static void
836lex_close (
837 lex_t *lex
838 )
839{
840 if (lex->token == TOKEN_STRING) {
841 lex_free_string (lex);
842 }
843
844 strbuffer_close (&lex->saved_text);
845}
846
847/*** parser ***/
848
849static json_t *
850parse_value (
851 lex_t *lex,
852 size_t flags,
853 json_error_t *error
854 );
855
856static json_t *
857parse_object (
858 lex_t *lex,
859 size_t flags,
860 json_error_t *error
861 )
862{
863 json_t *object = json_object ();
864
865 if (!object) {
866 return NULL;
867 }
868
869 lex_scan (lex, error);
870 if (lex->token == '}') {
871 return object;
872 }
873
874 while (1) {
875 char *key;
876 size_t len;
877 json_t *value;
878
879 if (lex->token != TOKEN_STRING) {
880 error_set (error, lex, json_error_invalid_syntax, "string or '}' expected");
881 goto error;
882 }
883
884 key = lex_steal_string (lex, &len);
885 if (!key) {
886 return NULL;
887 }
888
889 if (memchr (key, '\0', len)) {
890 jsonp_free (key);
891 error_set (
892 error,
893 lex,
894 json_error_null_byte_in_key,
895 "NUL byte in object key not supported"
896 );
897 goto error;
898 }
899
900 if (flags & JSON_REJECT_DUPLICATES) {
901 if (json_object_get (object, key)) {
902 jsonp_free (key);
903 error_set (error, lex, json_error_duplicate_key, "duplicate object key");
904 goto error;
905 }
906 }
907
908 lex_scan (lex, error);
909 if (lex->token != ':') {
910 jsonp_free (key);
911 error_set (error, lex, json_error_invalid_syntax, "':' expected");
912 goto error;
913 }
914
915 lex_scan (lex, error);
916 value = parse_value (lex, flags, error);
917 if (!value) {
918 jsonp_free (key);
919 goto error;
920 }
921
922 if (json_object_set_new_nocheck (object, key, value)) {
923 jsonp_free (key);
924 goto error;
925 }
926
927 jsonp_free (key);
928
929 lex_scan (lex, error);
930 if (lex->token != ',') {
931 break;
932 }
933
934 lex_scan (lex, error);
935 }
936
937 if (lex->token != '}') {
938 error_set (error, lex, json_error_invalid_syntax, "'}' expected");
939 goto error;
940 }
941
942 return object;
943
944error:
945 json_decref (object);
946 return NULL;
947}
948
949static json_t *
950parse_array (
951 lex_t *lex,
952 size_t flags,
953 json_error_t *error
954 )
955{
956 json_t *array = json_array ();
957
958 if (!array) {
959 return NULL;
960 }
961
962 lex_scan (lex, error);
963 if (lex->token == ']') {
964 return array;
965 }
966
967 while (lex->token) {
968 json_t *elem = parse_value (lex, flags, error);
969 if (!elem) {
970 goto error;
971 }
972
973 if (json_array_append_new (array, elem)) {
974 goto error;
975 }
976
977 lex_scan (lex, error);
978 if (lex->token != ',') {
979 break;
980 }
981
982 lex_scan (lex, error);
983 }
984
985 if (lex->token != ']') {
986 error_set (error, lex, json_error_invalid_syntax, "']' expected");
987 goto error;
988 }
989
990 return array;
991
992error:
993 json_decref (array);
994 return NULL;
995}
996
997static json_t *
998parse_value (
999 lex_t *lex,
1000 size_t flags,
1001 json_error_t *error
1002 )
1003{
1004 json_t *json;
1005
1006 lex->depth++;
1007 if (lex->depth > JSON_PARSER_MAX_DEPTH) {
1008 error_set (error, lex, json_error_stack_overflow, "maximum parsing depth reached");
1009 return NULL;
1010 }
1011
1012 switch (lex->token) {
1013 case TOKEN_STRING:
1014 {
1015 const char *value = lex->value.string.val;
1016 size_t len = lex->value.string.len;
1017
1018 if (!(flags & JSON_ALLOW_NUL)) {
1019 if (memchr (value, '\0', len)) {
1020 error_set (
1021 error,
1022 lex,
1023 json_error_null_character,
1024 "\\u0000 is not allowed without JSON_ALLOW_NUL"
1025 );
1026 return NULL;
1027 }
1028 }
1029
1030 json = jsonp_stringn_nocheck_own (value, len);
1031 lex->value.string.val = NULL;
1032 lex->value.string.len = 0;
1033 break;
1034 }
1035
1036 case TOKEN_INTEGER:
1037 {
1038 json = json_integer (lex->value.integer);
1039 break;
1040 }
1041
1042 case TOKEN_REAL:
1043 {
1044 json = json_real (lex->value.real);
1045 break;
1046 }
1047
1048 case TOKEN_TRUE:
1049 json = json_true ();
1050 break;
1051
1052 case TOKEN_FALSE:
1053 json = json_false ();
1054 break;
1055
1056 case TOKEN_NULL:
1057 json = json_null ();
1058 break;
1059
1060 case '{':
1061 json = parse_object (lex, flags, error);
1062 break;
1063
1064 case '[':
1065 json = parse_array (lex, flags, error);
1066 break;
1067
1068 case TOKEN_INVALID:
1069 error_set (error, lex, json_error_invalid_syntax, "invalid token");
1070 return NULL;
1071
1072 default:
1073 error_set (error, lex, json_error_invalid_syntax, "unexpected token");
1074 return NULL;
1075 }
1076
1077 if (!json) {
1078 return NULL;
1079 }
1080
1081 lex->depth--;
1082 return json;
1083}
1084
1085static json_t *
1086parse_json (
1087 lex_t *lex,
1088 size_t flags,
1089 json_error_t *error
1090 )
1091{
1092 json_t *result;
1093
1094 lex->depth = 0;
1095
1096 lex_scan (lex, error);
1097 if (!(flags & JSON_DECODE_ANY)) {
1098 if ((lex->token != '[') && (lex->token != '{')) {
1099 error_set (error, lex, json_error_invalid_syntax, "'[' or '{' expected");
1100 return NULL;
1101 }
1102 }
1103
1104 result = parse_value (lex, flags, error);
1105 if (!result) {
1106 return NULL;
1107 }
1108
1109 if (!(flags & JSON_DISABLE_EOF_CHECK)) {
1110 lex_scan (lex, error);
1111 if (lex->token != TOKEN_EOF) {
1112 error_set (
1113 error,
1114 lex,
1115 json_error_end_of_input_expected,
1116 "end of file expected"
1117 );
1118 json_decref (result);
1119 return NULL;
1120 }
1121 }
1122
1123 if (error) {
1124 /* Save the position even though there was no error */
1125 error->position = (int)lex->stream.position;
1126 }
1127
1128 return result;
1129}
1130
1131typedef struct {
1132 const char *data;
1133 size_t pos;
1135
1136static int
1137string_get (
1138 void *data
1139 )
1140{
1141 char c;
1142 string_data_t *stream = (string_data_t *)data;
1143
1144 c = stream->data[stream->pos];
1145 if (c == '\0') {
1146 return EOF;
1147 } else {
1148 stream->pos++;
1149 return (unsigned char)c;
1150 }
1151}
1152
1153json_t *
1154json_loads (
1155 const char *string,
1156 size_t flags,
1157 json_error_t *error
1158 )
1159{
1160 lex_t lex;
1161 json_t *result;
1162 string_data_t stream_data;
1163
1164 jsonp_error_init (error, "<string>");
1165
1166 if (string == NULL) {
1167 error_set (error, NULL, json_error_invalid_argument, "wrong arguments");
1168 return NULL;
1169 }
1170
1171 stream_data.data = string;
1172 stream_data.pos = 0;
1173
1174 if (lex_init (&lex, string_get, flags, (void *)&stream_data)) {
1175 return NULL;
1176 }
1177
1178 result = parse_json (&lex, flags, error);
1179
1180 lex_close (&lex);
1181 return result;
1182}
1183
1184typedef struct {
1185 const char *data;
1186 size_t len;
1187 size_t pos;
1189
1190static int
1191buffer_get (
1192 void *data
1193 )
1194{
1195 char c;
1196 buffer_data_t *stream = data;
1197
1198 if (stream->pos >= stream->len) {
1199 return EOF;
1200 }
1201
1202 c = stream->data[stream->pos];
1203 stream->pos++;
1204 return (unsigned char)c;
1205}
1206
1207json_t *
1208json_loadb (
1209 const char *buffer,
1210 size_t buflen,
1211 size_t flags,
1212 json_error_t *error
1213 )
1214{
1215 lex_t lex;
1216 json_t *result;
1217 buffer_data_t stream_data;
1218
1219 jsonp_error_init (error, "<buffer>");
1220
1221 if (buffer == NULL) {
1222 error_set (error, NULL, json_error_invalid_argument, "wrong arguments");
1223 return NULL;
1224 }
1225
1226 stream_data.data = buffer;
1227 stream_data.pos = 0;
1228 stream_data.len = buflen;
1229
1230 if (lex_init (&lex, buffer_get, flags, (void *)&stream_data)) {
1231 return NULL;
1232 }
1233
1234 result = parse_json (&lex, flags, error);
1235
1236 lex_close (&lex);
1237 return result;
1238}
1239
1240json_t *
1241json_loadf (
1242 FILE *input,
1243 size_t flags,
1244 json_error_t *error
1245 )
1246{
1247 lex_t lex;
1248 const char *source;
1249 json_t *result;
1250
1251 #ifdef HAVE_UNISTD_H
1252 if (input == stdin) {
1253 source = "<stdin>";
1254 } else
1255 #endif
1256 source = "<stream>";
1257
1258 jsonp_error_init (error, source);
1259
1260 if (input == NULL) {
1261 error_set (error, NULL, json_error_invalid_argument, "wrong arguments");
1262 return NULL;
1263 }
1264
1265 if (lex_init (&lex, (get_func)fgetc, flags, input)) {
1266 return NULL;
1267 }
1268
1269 result = parse_json (&lex, flags, error);
1270
1271 lex_close (&lex);
1272 return result;
1273}
1274
1275static int
1276fd_get_func (
1277 int *fd
1278 )
1279{
1280 #ifdef HAVE_UNISTD_H
1281 uint8_t c;
1282 if (read (*fd, &c, 1) == 1) {
1283 return c;
1284 }
1285
1286 #endif
1287 return EOF;
1288}
1289
1290json_t *
1291json_loadfd (
1292 int input,
1293 size_t flags,
1294 json_error_t *error
1295 )
1296{
1297 lex_t lex;
1298 const char *source;
1299 json_t *result;
1300
1301 #ifdef HAVE_UNISTD_H
1302 if (input == STDIN_FILENO) {
1303 source = "<stdin>";
1304 } else
1305 #endif
1306 source = "<stream>";
1307
1308 jsonp_error_init (error, source);
1309
1310 if (input < 0) {
1311 error_set (error, NULL, json_error_invalid_argument, "wrong arguments");
1312 return NULL;
1313 }
1314
1315 if (lex_init (&lex, (get_func)fd_get_func, flags, &input)) {
1316 return NULL;
1317 }
1318
1319 result = parse_json (&lex, flags, error);
1320
1321 lex_close (&lex);
1322 return result;
1323}
1324
1325json_t *
1326json_load_file (
1327 const char *path,
1328 size_t flags,
1329 json_error_t *error
1330 )
1331{
1332 json_t *result;
1333 FILE *fp;
1334
1335 jsonp_error_init (error, path);
1336
1337 if (path == NULL) {
1338 error_set (error, NULL, json_error_invalid_argument, "wrong arguments");
1339 return NULL;
1340 }
1341
1342 fp = fopen (path, "rb");
1343 if (!fp) {
1344 error_set (
1345 error,
1346 NULL,
1347 json_error_cannot_open_file,
1348 "unable to open %s: %s",
1349 path,
1350 strerror (errno)
1351 );
1352 return NULL;
1353 }
1354
1355 result = json_loadf (fp, flags, error);
1356
1357 fclose (fp);
1358 return result;
1359}
1360
1361#define MAX_BUF_LEN 1024
1362
1363typedef struct {
1364 char data[MAX_BUF_LEN];
1365 size_t len;
1366 size_t pos;
1367 json_load_callback_t callback;
1368 void *arg;
1370
1371static int
1372callback_get (
1373 void *data
1374 )
1375{
1376 char c;
1377 callback_data_t *stream = data;
1378
1379 if (stream->pos >= stream->len) {
1380 stream->pos = 0;
1381 stream->len = stream->callback (stream->data, MAX_BUF_LEN, stream->arg);
1382 if ((stream->len == 0) || (stream->len == (size_t)-1)) {
1383 return EOF;
1384 }
1385 }
1386
1387 c = stream->data[stream->pos];
1388 stream->pos++;
1389 return (unsigned char)c;
1390}
1391
1392json_t *
1393json_load_callback (
1394 json_load_callback_t callback,
1395 void *arg,
1396 size_t flags,
1397 json_error_t *error
1398 )
1399{
1400 lex_t lex;
1401 json_t *result;
1402
1403 callback_data_t stream_data;
1404
1405 memset (&stream_data, 0, sizeof (stream_data));
1406 stream_data.callback = callback;
1407 stream_data.arg = arg;
1408
1409 jsonp_error_init (error, "<callback>");
1410
1411 if (callback == NULL) {
1412 error_set (error, NULL, json_error_invalid_argument, "wrong arguments");
1413 return NULL;
1414 }
1415
1416 if (lex_init (&lex, (get_func)callback_get, flags, &stream_data)) {
1417 return NULL;
1418 }
1419
1420 result = parse_json (&lex, flags, error);
1421
1422 lex_close (&lex);
1423 return result;
1424}
#define NULL
Definition: Base.h:319
int fgetc(FILE *_File)
Definition: load.c:72
Definition: load.c:61