"Fossies" - the Fresh Open Source Software Archive

Member "jansson-2.14/src/load.c" (19 Nov 2020, 28743 Bytes) of package /linux/www/jansson-2.14.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "load.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.13.1_vs_2.14.

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