LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/geojson/libjson - json_tokener.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 417 596 70.0 %
Date: 2024-11-21 22:18:42 Functions: 11 14 78.6 %

          Line data    Source code
       1             : /*
       2             :  * $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $
       3             :  *
       4             :  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
       5             :  * Michael Clark <michael@metaparadigm.com>
       6             :  *
       7             :  * This library is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the MIT license. See COPYING for details.
       9             :  *
      10             :  *
      11             :  * Copyright (c) 2008-2009 Yahoo! Inc.  All rights reserved.
      12             :  * The copyrights to the contents of this file are licensed under the MIT License
      13             :  * (http://www.opensource.org/licenses/mit-license.php)
      14             :  */
      15             : 
      16             : #include "config.h"
      17             : 
      18             : #include "math_compat.h"
      19             : #include <assert.h>
      20             : #include <ctype.h>
      21             : #include <limits.h>
      22             : #include <math.h>
      23             : #include <stddef.h>
      24             : #include <stdio.h>
      25             : #include <stdlib.h>
      26             : #include <string.h>
      27             : 
      28             : #include "debug.h"
      29             : #include "json_inttypes.h"
      30             : #include "json_object.h"
      31             : #include "json_object_private.h"
      32             : #include "json_tokener.h"
      33             : #include "json_util.h"
      34             : #include "printbuf.h"
      35             : #include "strdup_compat.h"
      36             : 
      37             : #ifdef HAVE_LOCALE_H
      38             : #include <locale.h>
      39             : #endif /* HAVE_LOCALE_H */
      40             : #ifdef HAVE_XLOCALE_H
      41             : #include <xlocale.h>
      42             : #endif
      43             : #ifdef HAVE_STRINGS_H
      44             : #include <strings.h>
      45             : #endif /* HAVE_STRINGS_H */
      46             : 
      47             : #include "cpl_string.h"
      48             : 
      49             : #define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x)&7) + 9)
      50             : 
      51             : #if !HAVE_STRNCASECMP && defined(_MSC_VER)
      52             : /* MSC has the version as _strnicmp */
      53             : #define strncasecmp _strnicmp
      54             : #elif !HAVE_STRNCASECMP
      55             : #error You do not have strncasecmp on your system.
      56             : #endif /* HAVE_STRNCASECMP */
      57             : 
      58             : /* Use C99 NAN by default; if not available, nan("") should work too. */
      59             : #ifndef NAN
      60             : #define NAN nan("")
      61             : #endif /* !NAN */
      62             : 
      63             : static const char json_null_str[] = "null";
      64             : static const int json_null_str_len = sizeof(json_null_str) - 1;
      65             : static const char json_inf_str[] = "Infinity";
      66             : static const char json_inf_str_lower[] = "infinity";
      67             : static const unsigned int json_inf_str_len = sizeof(json_inf_str) - 1;
      68             : static const char json_nan_str[] = "NaN";
      69             : static const int json_nan_str_len = sizeof(json_nan_str) - 1;
      70             : static const char json_true_str[] = "true";
      71             : static const int json_true_str_len = sizeof(json_true_str) - 1;
      72             : static const char json_false_str[] = "false";
      73             : static const int json_false_str_len = sizeof(json_false_str) - 1;
      74             : 
      75             : /* clang-format off */
      76             : static const char *json_tokener_errors[] = {
      77             :   "success",
      78             :   "continue",
      79             :   "nesting too deep",
      80             :   "unexpected end of data",
      81             :   "unexpected character",
      82             :   "null expected",
      83             :   "boolean expected",
      84             :   "number expected",
      85             :   "array value separator ',' expected",
      86             :   "quoted object property name expected",
      87             :   "object property name separator ':' expected",
      88             :   "object value separator ',' expected",
      89             :   "invalid string sequence",
      90             :   "expected comment",
      91             :   "invalid utf-8 string",
      92             :   "buffer size overflow"
      93             : };
      94             : /* clang-format on */
      95             : 
      96             : /**
      97             :  * validete the utf-8 string in strict model.
      98             :  * if not utf-8 format, return err.
      99             :  */
     100             : static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes);
     101             : 
     102             : static int json_tokener_parse_double(const char *buf, int len, double *retval);
     103             : 
     104          24 : const char *json_tokener_error_desc(enum json_tokener_error jerr)
     105             : {
     106          24 :   int jerr_int = (int)jerr;
     107          24 :   if (jerr_int < 0 ||
     108             :       jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
     109           0 :     return "Unknown error, "
     110             :            "invalid json_tokener_error value passed to json_tokener_error_desc()";
     111          24 :   return json_tokener_errors[jerr];
     112             : }
     113             : 
     114           8 : enum json_tokener_error json_tokener_get_error(struct json_tokener *tok)
     115             : {
     116           8 :   return tok->err;
     117             : }
     118             : 
     119             : /* Stuff for decoding unicode sequences */
     120             : #define IS_HIGH_SURROGATE(uc) (((uc)&0xFC00) == 0xD800)
     121             : #define IS_LOW_SURROGATE(uc) (((uc)&0xFC00) == 0xDC00)
     122             : #define DECODE_SURROGATE_PAIR(hi, lo) ((((hi)&0x3FF) << 10) + ((lo)&0x3FF) + 0x10000)
     123             : static unsigned char utf8_replacement_char[3] = {0xEF, 0xBF, 0xBD};
     124             : 
     125        7777 : struct json_tokener *json_tokener_new_ex(int depth)
     126             : {
     127             :   struct json_tokener *tok;
     128             : 
     129        7777 :   tok = (struct json_tokener *)calloc(1, sizeof(struct json_tokener));
     130        7777 :   if (!tok)
     131           0 :     return NULL;
     132        7777 :   tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec));
     133        7777 :   if (!tok->stack)
     134             :   {
     135           0 :     free(tok);
     136           0 :     return NULL;
     137             :   }
     138        7777 :   tok->pb = printbuf_new();
     139        7777 :   tok->max_depth = depth;
     140        7777 :   json_tokener_reset(tok);
     141        7777 :   return tok;
     142             : }
     143             : 
     144        7775 : struct json_tokener *json_tokener_new(void)
     145             : {
     146        7775 :   return json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
     147             : }
     148             : 
     149        7777 : void json_tokener_free(struct json_tokener *tok)
     150             : {
     151        7777 :   json_tokener_reset(tok);
     152        7777 :   if (tok->pb)
     153        7777 :     printbuf_free(tok->pb);
     154        7777 :   free(tok->stack);
     155        7777 :   free(tok);
     156        7777 : }
     157             : 
     158     4377090 : static void json_tokener_reset_level(struct json_tokener *tok, int depth)
     159             : {
     160     4377090 :   tok->stack[depth].state = json_tokener_state_eatws;
     161     4377090 :   tok->stack[depth].saved_state = json_tokener_state_start;
     162     4377090 :   json_object_put(tok->stack[depth].current);
     163     4377090 :   tok->stack[depth].current = NULL;
     164     4377090 :   free(tok->stack[depth].obj_field_name);
     165     4377090 :   tok->stack[depth].obj_field_name = NULL;
     166     4377090 : }
     167             : 
     168       15554 : void json_tokener_reset(struct json_tokener *tok)
     169             : {
     170             :   int i;
     171       15554 :   if (!tok)
     172           0 :     return;
     173             : 
     174       31108 :   for (i = tok->depth; i >= 0; i--)
     175       15554 :     json_tokener_reset_level(tok, i);
     176       15554 :   tok->depth = 0;
     177       15554 :   tok->err = json_tokener_success;
     178             : }
     179             : 
     180          51 : struct json_object *json_tokener_parse(const char *str)
     181             : {
     182             :   enum json_tokener_error jerr_ignored;
     183             :   struct json_object *obj;
     184          51 :   obj = json_tokener_parse_verbose(str, &jerr_ignored);
     185          51 :   return obj;
     186             : }
     187             : 
     188          51 : struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokener_error *error)
     189             : {
     190             :   struct json_tokener *tok;
     191             :   struct json_object *obj;
     192             : 
     193          51 :   tok = json_tokener_new();
     194          51 :   if (!tok)
     195           0 :     return NULL;
     196          51 :   obj = json_tokener_parse_ex(tok, str, -1);
     197          51 :   *error = tok->err;
     198          51 :   if (tok->err != json_tokener_success
     199             : #if 0
     200             :     /* This would be a more sensible default, and cause parsing
     201             :      * things like "null123" to fail when the caller can't know
     202             :      * where the parsing left off, but starting to fail would
     203             :      * be a notable behavior change.  Save for a 1.0 release.
     204             :      */
     205             :       || json_tokener_get_parse_end(tok) != strlen(str)
     206             : #endif
     207             :   )
     208             : 
     209             :   {
     210           0 :     if (obj != NULL)
     211           0 :       json_object_put(obj);
     212           0 :     obj = NULL;
     213             :   }
     214             : 
     215          51 :   json_tokener_free(tok);
     216          51 :   return obj;
     217             : }
     218             : 
     219             : #define state tok->stack[tok->depth].state
     220             : #define saved_state tok->stack[tok->depth].saved_state
     221             : #define current tok->stack[tok->depth].current
     222             : #define obj_field_name tok->stack[tok->depth].obj_field_name
     223             : 
     224             : /* Optimization:
     225             :  * json_tokener_parse_ex() consumed a lot of CPU in its main loop,
     226             :  * iterating character-by character.  A large performance boost is
     227             :  * achieved by using tighter loops to locally handle units such as
     228             :  * comments and strings.  Loops that handle an entire token within
     229             :  * their scope also gather entire strings and pass them to
     230             :  * printbuf_memappend() in a single call, rather than calling
     231             :  * printbuf_memappend() one char at a time.
     232             :  *
     233             :  * PEEK_CHAR() and ADVANCE_CHAR() macros are used for code that is
     234             :  * common to both the main loop and the tighter loops.
     235             :  */
     236             : 
     237             : /* PEEK_CHAR(dest, tok) macro:
     238             :  *   Peeks at the current char and stores it in dest.
     239             :  *   Returns 1 on success, sets tok->err and returns 0 if no more chars.
     240             :  *   Implicit inputs:  str, len, nBytesp vars
     241             :  */
     242             : #define PEEK_CHAR(dest, tok)                                                 \
     243             :   (((tok)->char_offset == len)                                         \
     244             :        ? (((tok)->depth == 0 && state == json_tokener_state_eatws &&   \
     245             :            saved_state == json_tokener_state_finish)                   \
     246             :               ? (((tok)->err = json_tokener_success), 0)               \
     247             :               : (((tok)->err = json_tokener_continue), 0))             \
     248             :        : (((tok->flags & JSON_TOKENER_VALIDATE_UTF8) &&                \
     249             :            (!json_tokener_validate_utf8(*str, nBytesp)))               \
     250             :               ? ((tok->err = json_tokener_error_parse_utf8_string), 0) \
     251             :               : (((dest) = *str), 1)))
     252             : 
     253             : /* ADVANCE_CHAR() macro:
     254             :  *   Increments str & tok->char_offset.
     255             :  *   For convenience of existing conditionals, returns the old value of c (0 on eof)
     256             :  *   Implicit inputs:  c var
     257             :  */
     258             : #define ADVANCE_CHAR(str, tok) (++(str), ((tok)->char_offset)++, c)
     259             : 
     260             : /* End optimization macro defs */
     261             : 
     262        7780 : struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
     263             : {
     264        7780 :   struct json_object *obj = NULL;
     265        7780 :   char c = '\1';
     266        7780 :   unsigned int nBytes = 0;
     267        7780 :   unsigned int *nBytesp = &nBytes;
     268             : 
     269             : #ifdef HAVE_USELOCALE
     270        7780 :   locale_t oldlocale = uselocale(NULL);
     271             :   locale_t newloc;
     272             : #elif defined(HAVE_SETLOCALE)
     273             :   char *oldlocale = NULL;
     274             : #endif
     275             : 
     276        7780 :   tok->char_offset = 0;
     277        7780 :   tok->err = json_tokener_success;
     278             : 
     279             :   /* this interface is presently not 64-bit clean due to the int len argument
     280             :    * and the internal printbuf interface that takes 32-bit int len arguments
     281             :    * so the function limits the maximum string size to INT32_MAX (2GB).
     282             :    * If the function is called with len == -1 then strlen is called to check
     283             :    * the string length is less than INT32_MAX (2GB)
     284             :    */
     285        7780 :   if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX))
     286             :   {
     287           0 :     tok->err = json_tokener_error_size;
     288           0 :     return NULL;
     289             :   }
     290             : 
     291             : #ifdef HAVE_USELOCALE
     292             :   {
     293        7780 :     locale_t duploc = duplocale(oldlocale);
     294        7780 :     newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
     295        7780 :     if (newloc == NULL)
     296             :     {
     297           0 :       freelocale(duploc);
     298           0 :       return NULL;
     299             :     }
     300        7780 :     uselocale(newloc);
     301             :   }
     302             : #elif defined(HAVE_SETLOCALE)
     303             :   {
     304             :     char *tmplocale;
     305             :     tmplocale = setlocale(LC_NUMERIC, NULL);
     306             :     if (tmplocale)
     307             :       oldlocale = strdup(tmplocale);
     308             :     setlocale(LC_NUMERIC, "C");
     309             :   }
     310             : #endif
     311             : 
     312    10115100 :   while (PEEK_CHAR(c, tok)) // Note: c might be '\0' !
     313             :   {
     314             : 
     315    10108200 :   redo_char:
     316    28931500 :     switch (state)
     317             :     {
     318             : 
     319    11363900 :     case json_tokener_state_eatws:
     320             :       /* Advance until we change state */
     321    46601400 :       while (isspace((unsigned char)c))
     322             :       {
     323    35238200 :         if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
     324         745 :           goto out;
     325             :       }
     326    11363100 :       if (c == '/' && !(tok->flags & JSON_TOKENER_STRICT))
     327             :       {
     328           0 :         printbuf_reset(tok->pb);
     329           0 :         printbuf_memappend_fast(tok->pb, &c, 1);
     330           0 :         state = json_tokener_state_comment_start;
     331             :       }
     332             :       else
     333             :       {
     334    11363100 :         state = saved_state;
     335    11363100 :         goto redo_char;
     336             :       }
     337           0 :       break;
     338             : 
     339     2184670 :     case json_tokener_state_start:
     340     2184670 :       switch (c)
     341             :       {
     342      321359 :       case '{':
     343      321359 :         state = json_tokener_state_eatws;
     344      321359 :         saved_state = json_tokener_state_object_field_start;
     345      321359 :         current = json_object_new_object();
     346      321359 :         if (current == NULL)
     347           0 :           goto out;
     348      321359 :         break;
     349      187966 :       case '[':
     350      187966 :         state = json_tokener_state_eatws;
     351      187966 :         saved_state = json_tokener_state_array;
     352      187966 :         current = json_object_new_array();
     353      187966 :         if (current == NULL)
     354           0 :           goto out;
     355      187966 :         break;
     356           7 :       case 'I':
     357             :       case 'i':
     358           7 :         state = json_tokener_state_inf;
     359           7 :         printbuf_reset(tok->pb);
     360           7 :         tok->st_pos = 0;
     361           7 :         goto redo_char;
     362        3373 :       case 'N':
     363             :       case 'n':
     364        3373 :         state = json_tokener_state_null; // or NaN
     365        3373 :         printbuf_reset(tok->pb);
     366        3373 :         tok->st_pos = 0;
     367        3373 :         goto redo_char;
     368           4 :       case '\'':
     369           4 :         if (tok->flags & JSON_TOKENER_STRICT)
     370             :         {
     371             :           /* in STRICT mode only double-quote are allowed */
     372           0 :           tok->err = json_tokener_error_parse_unexpected;
     373           0 :           goto out;
     374             :         }
     375             :         /* FALLTHRU */
     376             :       case '"':
     377     1210590 :         state = json_tokener_state_string;
     378     1210590 :         printbuf_reset(tok->pb);
     379     1210590 :         tok->quote_char = c;
     380     1210590 :         break;
     381       36889 :       case 'T':
     382             :       case 't':
     383             :       case 'F':
     384             :       case 'f':
     385       36889 :         state = json_tokener_state_boolean;
     386       36889 :         printbuf_reset(tok->pb);
     387       36889 :         tok->st_pos = 0;
     388       36889 :         goto redo_char;
     389      424478 :       case '0':
     390             :       case '1':
     391             :       case '2':
     392             :       case '3':
     393             :       case '4':
     394             :       case '5':
     395             :       case '6':
     396             :       case '7':
     397             :       case '8':
     398             :       case '9':
     399             :       case '-':
     400      424478 :         state = json_tokener_state_number;
     401      424478 :         printbuf_reset(tok->pb);
     402      424478 :         tok->is_double = 0;
     403      424478 :         goto redo_char;
     404           5 :       default: tok->err = json_tokener_error_parse_unexpected; goto out;
     405             :       }
     406     1719920 :       break;
     407             : 
     408     2177060 :     case json_tokener_state_finish:
     409     2177060 :       if (tok->depth == 0)
     410         171 :         goto out;
     411     2176890 :       obj = json_object_get(current);
     412     2176890 :       json_tokener_reset_level(tok, tok->depth);
     413     2176890 :       tok->depth--;
     414     2176890 :       goto redo_char;
     415             : 
     416          10 :     case json_tokener_state_inf: /* aka starts with 'i' (or 'I', or "-i", or "-I") */
     417             :     {
     418             :       /* If we were guaranteed to have len set, then we could (usually) handle
     419             :        * the entire "Infinity" check in a single strncmp (strncasecmp), but
     420             :        * since len might be -1 (i.e. "read until \0"), we need to check it
     421             :        * a character at a time.
     422             :        * Trying to handle it both ways would make this code considerably more
     423             :        * complicated with likely little performance benefit.
     424             :        */
     425          10 :       int is_negative = 0;
     426          10 :       const char *_json_inf_str = json_inf_str;
     427          10 :       if (!(tok->flags & JSON_TOKENER_STRICT))
     428          10 :         _json_inf_str = json_inf_str_lower;
     429             : 
     430             :       /* Note: tok->st_pos must be 0 when state is set to json_tokener_state_inf */
     431          66 :       while (tok->st_pos < (int)json_inf_str_len)
     432             :       {
     433          60 :         char inf_char = *str;
     434          60 :         if (!(tok->flags & JSON_TOKENER_STRICT))
     435          60 :           inf_char = tolower((unsigned char)*str);
     436          60 :         if (inf_char != _json_inf_str[tok->st_pos])
     437             :         {
     438           4 :           tok->err = json_tokener_error_parse_unexpected;
     439           4 :           goto out;
     440             :         }
     441          56 :         tok->st_pos++;
     442          56 :         (void)ADVANCE_CHAR(str, tok);
     443          56 :         if (!PEEK_CHAR(c, tok))
     444             :         {
     445             :           /* out of input chars, for now at least */
     446           0 :           goto out;
     447             :         }
     448             :       }
     449             :       /* We checked the full length of "Infinity", so create the object.
     450             :        * When handling -Infinity, the number parsing code will have dropped
     451             :        * the "-" into tok->pb for us, so check it now.
     452             :        */
     453           6 :       if (printbuf_length(tok->pb) > 0 && *(tok->pb->buf) == '-')
     454             :       {
     455           3 :         is_negative = 1;
     456             :       }
     457           6 :       current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
     458           6 :       if (current == NULL)
     459           0 :         goto out;
     460           6 :       saved_state = json_tokener_state_finish;
     461           6 :       state = json_tokener_state_eatws;
     462           6 :       goto redo_char;
     463             :     }
     464             :     break;
     465       16859 :     case json_tokener_state_null: /* aka starts with 'n' */
     466             :     {
     467             :       int size;
     468             :       int size_nan;
     469       16859 :       printbuf_memappend_fast(tok->pb, &c, 1);
     470       16859 :       size = json_min(tok->st_pos + 1, json_null_str_len);
     471       16859 :       size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
     472       16859 :       if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     473       16859 :            strncasecmp(json_null_str, tok->pb->buf, size) == 0) ||
     474           9 :           (strncmp(json_null_str, tok->pb->buf, size) == 0))
     475             :       {
     476       16850 :         if (tok->st_pos == json_null_str_len)
     477             :         {
     478        3367 :           current = NULL;
     479        3367 :           saved_state = json_tokener_state_finish;
     480        3367 :           state = json_tokener_state_eatws;
     481        3367 :           goto redo_char;
     482             :         }
     483             :       }
     484           9 :       else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     485           9 :                 strncasecmp(json_nan_str, tok->pb->buf, size_nan) == 0) ||
     486           0 :                (strncmp(json_nan_str, tok->pb->buf, size_nan) == 0))
     487             :       {
     488           9 :         if (tok->st_pos == json_nan_str_len)
     489             :         {
     490           3 :           current = json_object_new_double(NAN);
     491           3 :           if (current == NULL)
     492           0 :             goto out;
     493           3 :           saved_state = json_tokener_state_finish;
     494           3 :           state = json_tokener_state_eatws;
     495           3 :           goto redo_char;
     496             :         }
     497             :       }
     498             :       else
     499             :       {
     500           0 :         tok->err = json_tokener_error_parse_null;
     501           0 :         goto out;
     502             :       }
     503       13489 :       tok->st_pos++;
     504             :     }
     505       13489 :     break;
     506             : 
     507           0 :     case json_tokener_state_comment_start:
     508           0 :       if (c == '*')
     509             :       {
     510           0 :         state = json_tokener_state_comment;
     511             :       }
     512           0 :       else if (c == '/')
     513             :       {
     514           0 :         state = json_tokener_state_comment_eol;
     515             :       }
     516             :       else
     517             :       {
     518           0 :         tok->err = json_tokener_error_parse_comment;
     519           0 :         goto out;
     520             :       }
     521           0 :       printbuf_memappend_fast(tok->pb, &c, 1);
     522           0 :       break;
     523             : 
     524           0 :     case json_tokener_state_comment:
     525             :     {
     526             :       /* Advance until we change state */
     527           0 :       const char *case_start = str;
     528           0 :       while (c != '*')
     529             :       {
     530           0 :         if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
     531             :         {
     532           0 :           printbuf_memappend_fast(tok->pb, case_start,
     533             :                                   (int)(str - case_start));
     534           0 :           goto out;
     535             :         }
     536             :       }
     537           0 :       printbuf_memappend_fast(tok->pb, case_start, 1 + (int)(str - case_start));
     538           0 :       state = json_tokener_state_comment_end;
     539             :     }
     540           0 :     break;
     541             : 
     542           0 :     case json_tokener_state_comment_eol:
     543             :     {
     544             :       /* Advance until we change state */
     545           0 :       const char *case_start = str;
     546           0 :       while (c != '\n')
     547             :       {
     548           0 :         if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
     549             :         {
     550           0 :           printbuf_memappend_fast(tok->pb, case_start,
     551             :                                   (int)(str - case_start));
     552           0 :           goto out;
     553             :         }
     554             :       }
     555           0 :       printbuf_memappend_fast(tok->pb, case_start, (int)(str - case_start));
     556             :       MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
     557           0 :       state = json_tokener_state_eatws;
     558             :     }
     559           0 :     break;
     560             : 
     561           0 :     case json_tokener_state_comment_end:
     562           0 :       printbuf_memappend_fast(tok->pb, &c, 1);
     563           0 :       if (c == '/')
     564             :       {
     565             :         MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
     566           0 :         state = json_tokener_state_eatws;
     567             :       }
     568             :       else
     569             :       {
     570           0 :         state = json_tokener_state_comment;
     571             :       }
     572           0 :       break;
     573             : 
     574     1643960 :     case json_tokener_state_string:
     575             :     {
     576             :       /* Advance until we change state */
     577     1643960 :       const char *case_start = str;
     578             :       while (1)
     579             :       {
     580    55089400 :         if (c == tok->quote_char)
     581             :         {
     582     1210590 :           printbuf_memappend_fast(tok->pb, case_start,
     583             :                                   (int)(str - case_start));
     584     2421180 :           current =
     585     1210590 :               json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
     586     1210590 :           if (current == NULL)
     587           0 :             goto out;
     588     1210590 :           saved_state = json_tokener_state_finish;
     589     1210590 :           state = json_tokener_state_eatws;
     590     1210590 :           break;
     591             :         }
     592    53878800 :         else if (c == '\\')
     593             :         {
     594      433368 :           printbuf_memappend_fast(tok->pb, case_start,
     595             :                                   (int)(str - case_start));
     596      433368 :           saved_state = json_tokener_state_string;
     597      433368 :           state = json_tokener_state_string_escape;
     598      433368 :           break;
     599             :         }
     600    53445400 :         if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
     601             :         {
     602           0 :           printbuf_memappend_fast(tok->pb, case_start,
     603             :                                   (int)(str - case_start));
     604           0 :           goto out;
     605             :         }
     606             :       }
     607             :     }
     608     1643960 :     break;
     609             : 
     610      434015 :     case json_tokener_state_string_escape:
     611      434015 :       switch (c)
     612             :       {
     613      289858 :       case '"':
     614             :       case '\\':
     615             :       case '/':
     616      289858 :         printbuf_memappend_fast(tok->pb, &c, 1);
     617      289858 :         state = saved_state;
     618      289858 :         break;
     619      143982 :       case 'b':
     620             :       case 'n':
     621             :       case 'r':
     622             :       case 't':
     623             :       case 'f':
     624      143982 :         if (c == 'b')
     625           0 :           printbuf_memappend_fast(tok->pb, "\b", 1);
     626      143982 :         else if (c == 'n')
     627      143982 :           printbuf_memappend_fast(tok->pb, "\n", 1);
     628           0 :         else if (c == 'r')
     629           0 :           printbuf_memappend_fast(tok->pb, "\r", 1);
     630           0 :         else if (c == 't')
     631           0 :           printbuf_memappend_fast(tok->pb, "\t", 1);
     632           0 :         else if (c == 'f')
     633           0 :           printbuf_memappend_fast(tok->pb, "\f", 1);
     634      143982 :         state = saved_state;
     635      143982 :         break;
     636         175 :       case 'u':
     637         175 :         tok->ucs_char = 0;
     638         175 :         tok->st_pos = 0;
     639         175 :         state = json_tokener_state_escape_unicode;
     640         175 :         break;
     641           0 :       default: tok->err = json_tokener_error_parse_string; goto out;
     642             :       }
     643      434015 :       break;
     644             : 
     645             :       // ===================================================
     646             : 
     647         700 :     case json_tokener_state_escape_unicode:
     648             :     {
     649             :       /* Handle a 4-byte \uNNNN sequence, or two sequences if a surrogate pair */
     650             :       while (1)
     651             :       {
     652         700 :         if (!c || !strchr(json_hex_chars, c))
     653             :         {
     654           0 :           tok->err = json_tokener_error_parse_string;
     655           0 :           goto out;
     656             :         }
     657        1400 :         tok->ucs_char |=
     658         700 :             ((unsigned int)jt_hexdigit(c) << ((3 - tok->st_pos) * 4));
     659         700 :         tok->st_pos++;
     660         700 :         if (tok->st_pos >= 4)
     661         175 :           break;
     662             : 
     663         525 :         (void)ADVANCE_CHAR(str, tok);
     664         525 :         if (!PEEK_CHAR(c, tok))
     665             :         {
     666             :           /*
     667             :            * We're out of characters in the current call to
     668             :            * json_tokener_parse(), but a subsequent call might
     669             :            * provide us with more, so leave our current state
     670             :            * as-is (including tok->high_surrogate) and return.
     671             :            */
     672           0 :           goto out;
     673             :         }
     674             :       }
     675         175 :       tok->st_pos = 0;
     676             : 
     677             :       /* Now, we have a full \uNNNN sequence in tok->ucs_char */
     678             : 
     679             :       /* If the *previous* sequence was a high surrogate ... */
     680         175 :       if (tok->high_surrogate)
     681             :       {
     682           0 :         if (IS_LOW_SURROGATE(tok->ucs_char))
     683             :         {
     684             :           /* Recalculate the ucs_char, then fall thru to process normally */
     685           0 :           tok->ucs_char = DECODE_SURROGATE_PAIR(tok->high_surrogate,
     686             :                                                 tok->ucs_char);
     687             :         }
     688             :         else
     689             :         {
     690             :           /* High surrogate was not followed by a low surrogate
     691             :            * Replace the high and process the rest normally
     692             :            */
     693           0 :           printbuf_memappend_fast(tok->pb,
     694             :                                   (char *)utf8_replacement_char, 3);
     695             :         }
     696           0 :         tok->high_surrogate = 0;
     697             :       }
     698             : 
     699         175 :       if (tok->ucs_char < 0x80)
     700             :       {
     701             :         unsigned char unescaped_utf[1];
     702         135 :         unescaped_utf[0] = tok->ucs_char;
     703         135 :         printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 1);
     704             :       }
     705          40 :       else if (tok->ucs_char < 0x800)
     706             :       {
     707             :         unsigned char unescaped_utf[2];
     708           0 :         unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6);
     709           0 :         unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f);
     710           0 :         printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 2);
     711             :       }
     712          40 :       else if (IS_HIGH_SURROGATE(tok->ucs_char))
     713             :       {
     714             :         /*
     715             :          * The next two characters should be \u, HOWEVER,
     716             :          * we can't simply peek ahead here, because the
     717             :          * characters we need might not be passed to us
     718             :          * until a subsequent call to json_tokener_parse.
     719             :          * Instead, transition through a couple of states.
     720             :          * (now):
     721             :          *   _escape_unicode => _unicode_need_escape
     722             :          * (see a '\\' char):
     723             :          *   _unicode_need_escape => _unicode_need_u
     724             :          * (see a 'u' char):
     725             :          *   _unicode_need_u => _escape_unicode
     726             :          *      ...and we'll end up back around here.
     727             :          */
     728           0 :         tok->high_surrogate = tok->ucs_char;
     729           0 :         tok->ucs_char = 0;
     730           0 :         state = json_tokener_state_escape_unicode_need_escape;
     731           0 :         break;
     732             :       }
     733          40 :       else if (IS_LOW_SURROGATE(tok->ucs_char))
     734             :       {
     735             :         /* Got a low surrogate not preceded by a high */
     736           0 :         printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
     737             :       }
     738          40 :       else if (tok->ucs_char < 0x10000)
     739             :       {
     740             :         unsigned char unescaped_utf[3];
     741          40 :         unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
     742          40 :         unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
     743          40 :         unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
     744          40 :         printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 3);
     745             :       }
     746           0 :       else if (tok->ucs_char < 0x110000)
     747             :       {
     748             :         unsigned char unescaped_utf[4];
     749           0 :         unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07);
     750           0 :         unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f);
     751           0 :         unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
     752           0 :         unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f);
     753           0 :         printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 4);
     754             :       }
     755             :       else
     756             :       {
     757             :         /* Don't know what we got--insert the replacement char */
     758           0 :         printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
     759             :       }
     760         175 :       state = saved_state; // i.e. _state_string or _state_object_field
     761             :     }
     762         175 :     break;
     763             : 
     764           0 :     case json_tokener_state_escape_unicode_need_escape:
     765             :       // We get here after processing a high_surrogate
     766             :       // require a '\\' char
     767           0 :       if (!c || c != '\\')
     768             :       {
     769             :         /* Got a high surrogate without another sequence following
     770             :          * it.  Put a replacement char in for the high surrogate
     771             :          * and pop back up to _state_string or _state_object_field.
     772             :          */
     773           0 :         printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
     774           0 :         tok->high_surrogate = 0;
     775           0 :         tok->ucs_char = 0;
     776           0 :         tok->st_pos = 0;
     777           0 :         state = saved_state;
     778           0 :         goto redo_char;
     779             :       }
     780           0 :       state = json_tokener_state_escape_unicode_need_u;
     781           0 :       break;
     782             : 
     783           0 :     case json_tokener_state_escape_unicode_need_u:
     784             :       /* We already had a \ char, check that it's \u */
     785           0 :       if (!c || c != 'u')
     786             :       {
     787             :         /* Got a high surrogate with some non-unicode escape
     788             :          * sequence following it.
     789             :          * Put a replacement char in for the high surrogate
     790             :          * and handle the escape sequence normally.
     791             :          */
     792           0 :         printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
     793           0 :         tok->high_surrogate = 0;
     794           0 :         tok->ucs_char = 0;
     795           0 :         tok->st_pos = 0;
     796           0 :         state = json_tokener_state_string_escape;
     797           0 :         goto redo_char;
     798             :       }
     799           0 :       state = json_tokener_state_escape_unicode;
     800           0 :       break;
     801             : 
     802             :       // ===================================================
     803             : 
     804      184612 :     case json_tokener_state_boolean:
     805             :     {
     806             :       int size1, size2;
     807      184612 :       printbuf_memappend_fast(tok->pb, &c, 1);
     808      184612 :       size1 = json_min(tok->st_pos + 1, json_true_str_len);
     809      184612 :       size2 = json_min(tok->st_pos + 1, json_false_str_len);
     810      184612 :       if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     811      184612 :            strncasecmp(json_true_str, tok->pb->buf, size1) == 0) ||
     812        1087 :           (strncmp(json_true_str, tok->pb->buf, size1) == 0))
     813             :       {
     814      183525 :         if (tok->st_pos == json_true_str_len)
     815             :         {
     816       36705 :           current = json_object_new_boolean(1);
     817       36705 :           if (current == NULL)
     818           0 :             goto out;
     819       36705 :           saved_state = json_tokener_state_finish;
     820       36705 :           state = json_tokener_state_eatws;
     821       36705 :           goto redo_char;
     822             :         }
     823             :       }
     824        1087 :       else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     825        1087 :                 strncasecmp(json_false_str, tok->pb->buf, size2) == 0) ||
     826           4 :                (strncmp(json_false_str, tok->pb->buf, size2) == 0))
     827             :       {
     828        1083 :         if (tok->st_pos == json_false_str_len)
     829             :         {
     830         179 :           current = json_object_new_boolean(0);
     831         179 :           if (current == NULL)
     832           0 :             goto out;
     833         179 :           saved_state = json_tokener_state_finish;
     834         179 :           state = json_tokener_state_eatws;
     835         179 :           goto redo_char;
     836             :         }
     837             :       }
     838             :       else
     839             :       {
     840           4 :         tok->err = json_tokener_error_parse_boolean;
     841           4 :         goto out;
     842             :       }
     843      147724 :       tok->st_pos++;
     844             :     }
     845      147724 :     break;
     846             : 
     847      424478 :     case json_tokener_state_number:
     848             :     {
     849             :       /* Advance until we change state */
     850      424478 :       const char *case_start = str;
     851      424478 :       int case_len = 0;
     852      424478 :       int is_exponent = 0;
     853      424478 :       int neg_sign_ok = 1;
     854      424478 :       int pos_sign_ok = 0;
     855      424478 :       if (printbuf_length(tok->pb) > 0)
     856             :       {
     857             :         /* We don't save all state from the previous incremental parse
     858             :            so we need to re-generate it based on the saved string so far.
     859             :          */
     860           0 :         char *e_loc = strchr(tok->pb->buf, 'e');
     861           0 :         if (!e_loc)
     862           0 :           e_loc = strchr(tok->pb->buf, 'E');
     863           0 :         if (e_loc)
     864             :         {
     865           0 :           char *last_saved_char =
     866           0 :               &tok->pb->buf[printbuf_length(tok->pb) - 1];
     867           0 :           is_exponent = 1;
     868           0 :           pos_sign_ok = neg_sign_ok = 1;
     869             :           /* If the "e" isn't at the end, we can't start with a '-' */
     870           0 :           if (e_loc != last_saved_char)
     871             :           {
     872           0 :             neg_sign_ok = 0;
     873           0 :             pos_sign_ok = 0;
     874             :           }
     875             :           // else leave it set to 1, i.e. start of the new input
     876             :         }
     877             :       }
     878             : 
     879     2818200 :       while (c && ((c >= '0' && c <= '9') ||
     880      579275 :                    (!is_exponent && (c == 'e' || c == 'E')) ||
     881      579238 :                    (neg_sign_ok && c == '-') || (pos_sign_ok && c == '+') ||
     882      558024 :                    (!tok->is_double && c == '.')))
     883             :       {
     884     2393720 :         pos_sign_ok = neg_sign_ok = 0;
     885     2393720 :         ++case_len;
     886             : 
     887             :         /* non-digit characters checks */
     888             :         /* note: since the main loop condition to get here was
     889             :          * an input starting with 0-9 or '-', we are
     890             :          * protected from input starting with '.' or
     891             :          * e/E.
     892             :          */
     893     2393720 :         switch (c)
     894             :         {
     895      133546 :         case '.':
     896      133546 :           tok->is_double = 1;
     897      133546 :           pos_sign_ok = 1;
     898      133546 :           neg_sign_ok = 1;
     899      133546 :           break;
     900          37 :         case 'e': /* FALLTHRU */
     901             :         case 'E':
     902          37 :           is_exponent = 1;
     903          37 :           tok->is_double = 1;
     904             :           /* the exponent part can begin with a negative sign */
     905          37 :           pos_sign_ok = neg_sign_ok = 1;
     906          37 :           break;
     907     2260140 :         default: break;
     908             :         }
     909             : 
     910     2393720 :         if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
     911             :         {
     912           0 :           printbuf_memappend_fast(tok->pb, case_start, case_len);
     913           0 :           goto out;
     914             :         }
     915             :       }
     916             :       /*
     917             :         Now we know c isn't a valid number char, but check whether
     918             :         it might have been intended to be, and return a potentially
     919             :         more understandable error right away.
     920             :         However, if we're at the top-level, use the number as-is
     921             :           because c can be part of a new object to parse on the
     922             :         next call to json_tokener_parse().
     923             :        */
     924      424478 :       if (tok->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' &&
     925       39757 :           c != 'I' && c != 'i' && !isspace((unsigned char)c))
     926             :       {
     927           0 :         tok->err = json_tokener_error_parse_number;
     928           0 :         goto out;
     929             :       }
     930      424478 :       if (case_len > 0)
     931      424478 :         printbuf_memappend_fast(tok->pb, case_start, case_len);
     932             : 
     933             :       // Check for -Infinity
     934      424478 :       if (tok->pb->buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I'))
     935             :       {
     936           3 :         state = json_tokener_state_inf;
     937           3 :         tok->st_pos = 0;
     938           3 :         goto redo_char;
     939             :       }
     940      424475 :       if (tok->is_double && !(tok->flags & JSON_TOKENER_STRICT))
     941             :       {
     942             :         /* Trim some chars off the end, to allow things
     943             :            like "123e+" to parse ok. */
     944      133551 :         while (printbuf_length(tok->pb) > 1)
     945             :         {
     946      133551 :           char last_char = tok->pb->buf[printbuf_length(tok->pb) - 1];
     947      133551 :           if (last_char != 'e' && last_char != 'E' &&
     948      133551 :               last_char != '-' && last_char != '+')
     949             :           {
     950      133551 :             break;
     951             :           }
     952           0 :           tok->pb->buf[printbuf_length(tok->pb) - 1] = '\0';
     953           0 :           printbuf_length(tok->pb)--;
     954             :         }
     955             :       }
     956             :     }
     957             :       {
     958             :         int64_t num64;
     959             :         uint64_t numuint64;
     960             :         double numd;
     961      437441 :         if (!tok->is_double && tok->pb->buf[0] == '-' &&
     962       12966 :             json_parse_int64(tok->pb->buf, &num64) == 0)
     963             :         {
     964       12966 :           current = json_object_new_int64(num64);
     965       12966 :           if (current == NULL)
     966           0 :             goto out;
     967             :         }
     968      689467 :         else if (!tok->is_double && tok->pb->buf[0] != '-' &&
     969      277958 :                  json_parse_uint64(tok->pb->buf, &numuint64) == 0)
     970             :         {
     971      277958 :           if (numuint64 && tok->pb->buf[0] == '0' &&
     972           0 :               (tok->flags & JSON_TOKENER_STRICT))
     973             :           {
     974           0 :             tok->err = json_tokener_error_parse_number;
     975           0 :             goto out;
     976             :           }
     977      277958 :           if (numuint64 <= INT64_MAX)
     978             :           {
     979      277957 :             num64 = (uint64_t)numuint64;
     980      277957 :             current = json_object_new_int64(num64);
     981      277957 :             if (current == NULL)
     982           0 :               goto out;
     983             :           }
     984             :           else
     985             :           {
     986           1 :             current = json_object_new_uint64(numuint64);
     987           1 :             if (current == NULL)
     988           0 :               goto out;
     989             :           }
     990             :         }
     991      267102 :         else if (tok->is_double &&
     992      133551 :                  json_tokener_parse_double(
     993      133551 :                      tok->pb->buf, printbuf_length(tok->pb), &numd) == 0)
     994             :         {
     995      133551 :           current = json_object_new_double_s(numd, tok->pb->buf);
     996      133551 :           if (current == NULL)
     997           0 :             goto out;
     998             :         }
     999             :         else
    1000             :         {
    1001           0 :           tok->err = json_tokener_error_parse_number;
    1002           0 :           goto out;
    1003             :         }
    1004      424475 :         saved_state = json_tokener_state_finish;
    1005      424475 :         state = json_tokener_state_eatws;
    1006      424475 :         goto redo_char;
    1007             :       }
    1008             :       break;
    1009             : 
    1010      854647 :     case json_tokener_state_array_after_sep:
    1011             :     case json_tokener_state_array:
    1012      854647 :       if (c == ']')
    1013             :       {
    1014             :         // Minimize memory usage; assume parsed objs are unlikely to be changed
    1015         624 :         json_object_array_shrink(current, 0);
    1016             : 
    1017         624 :         if (state == json_tokener_state_array_after_sep &&
    1018          23 :             (tok->flags & JSON_TOKENER_STRICT))
    1019             :         {
    1020           0 :           tok->err = json_tokener_error_parse_unexpected;
    1021           0 :           goto out;
    1022             :         }
    1023         624 :         saved_state = json_tokener_state_finish;
    1024         624 :         state = json_tokener_state_eatws;
    1025             :       }
    1026             :       else
    1027             :       {
    1028      854023 :         if (tok->depth >= tok->max_depth - 1)
    1029             :         {
    1030           0 :           tok->err = json_tokener_error_depth;
    1031           0 :           goto out;
    1032             :         }
    1033      854023 :         state = json_tokener_state_array_add;
    1034      854023 :         tok->depth++;
    1035      854023 :         json_tokener_reset_level(tok, tok->depth);
    1036      854023 :         goto redo_char;
    1037             :       }
    1038         624 :       break;
    1039             : 
    1040      854023 :     case json_tokener_state_array_add:
    1041      854023 :       if (json_object_array_add(current, obj) != 0)
    1042           0 :         goto out;
    1043      854023 :       saved_state = json_tokener_state_array_sep;
    1044      854023 :       state = json_tokener_state_eatws;
    1045      854023 :       goto redo_char;
    1046             : 
    1047      854023 :     case json_tokener_state_array_sep:
    1048      854023 :       if (c == ']')
    1049             :       {
    1050             :         // Minimize memory usage; assume parsed objs are unlikely to be changed
    1051      187342 :         json_object_array_shrink(current, 0);
    1052             : 
    1053      187342 :         saved_state = json_tokener_state_finish;
    1054      187342 :         state = json_tokener_state_eatws;
    1055             :       }
    1056      666681 :       else if (c == ',')
    1057             :       {
    1058      666681 :         saved_state = json_tokener_state_array_after_sep;
    1059      666681 :         state = json_tokener_state_eatws;
    1060             :       }
    1061             :       else
    1062             :       {
    1063           0 :         tok->err = json_tokener_error_parse_array;
    1064           0 :         goto out;
    1065             :       }
    1066      854023 :       break;
    1067             : 
    1068     1324120 :     case json_tokener_state_object_field_start:
    1069             :     case json_tokener_state_object_field_start_after_sep:
    1070     1324120 :       if (c == '}')
    1071             :       {
    1072        1251 :         if (state == json_tokener_state_object_field_start_after_sep &&
    1073         148 :             (tok->flags & JSON_TOKENER_STRICT))
    1074             :         {
    1075           0 :           tok->err = json_tokener_error_parse_unexpected;
    1076           0 :           goto out;
    1077             :         }
    1078        1251 :         saved_state = json_tokener_state_finish;
    1079        1251 :         state = json_tokener_state_eatws;
    1080             :       }
    1081     1322870 :       else if (c == '"' || c == '\'')
    1082             :       {
    1083     1322870 :         tok->quote_char = c;
    1084     1322870 :         printbuf_reset(tok->pb);
    1085     1322870 :         state = json_tokener_state_object_field;
    1086             :       }
    1087             :       else
    1088             :       {
    1089           5 :         tok->err = json_tokener_error_parse_object_key_name;
    1090           5 :         goto out;
    1091             :       }
    1092     1324120 :       break;
    1093             : 
    1094     1323520 :     case json_tokener_state_object_field:
    1095             :     {
    1096             :       /* Advance until we change state */
    1097     1323520 :       const char *case_start = str;
    1098             :       while (1)
    1099             :       {
    1100     7993420 :         if (c == tok->quote_char)
    1101             :         {
    1102     1322870 :           printbuf_memappend_fast(tok->pb, case_start,
    1103             :                                   (int)(str - case_start));
    1104     1322870 :           obj_field_name = strdup(tok->pb->buf);
    1105     1322870 :           saved_state = json_tokener_state_object_field_end;
    1106     1322870 :           state = json_tokener_state_eatws;
    1107     1322870 :           break;
    1108             :         }
    1109     6670550 :         else if (c == '\\')
    1110             :         {
    1111         647 :           printbuf_memappend_fast(tok->pb, case_start,
    1112             :                                   (int)(str - case_start));
    1113         647 :           saved_state = json_tokener_state_object_field;
    1114         647 :           state = json_tokener_state_string_escape;
    1115         647 :           break;
    1116             :         }
    1117     6669900 :         if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
    1118             :         {
    1119           1 :           printbuf_memappend_fast(tok->pb, case_start,
    1120             :                                   (int)(str - case_start));
    1121           1 :           goto out;
    1122             :         }
    1123             :       }
    1124             :     }
    1125     1323520 :     break;
    1126             : 
    1127     1322870 :     case json_tokener_state_object_field_end:
    1128     1322870 :       if (c == ':')
    1129             :       {
    1130     1322870 :         saved_state = json_tokener_state_object_value;
    1131     1322870 :         state = json_tokener_state_eatws;
    1132             :       }
    1133             :       else
    1134             :       {
    1135           0 :         tok->err = json_tokener_error_parse_object_key_sep;
    1136           0 :         goto out;
    1137             :       }
    1138     1322870 :       break;
    1139             : 
    1140     1322870 :     case json_tokener_state_object_value:
    1141     1322870 :       if (tok->depth >= tok->max_depth - 1)
    1142             :       {
    1143           0 :         tok->err = json_tokener_error_depth;
    1144           0 :         goto out;
    1145             :       }
    1146     1322870 :       state = json_tokener_state_object_value_add;
    1147     1322870 :       tok->depth++;
    1148     1322870 :       json_tokener_reset_level(tok, tok->depth);
    1149     1322870 :       goto redo_char;
    1150             : 
    1151     1322870 :     case json_tokener_state_object_value_add:
    1152     1322870 :       json_object_object_add(current, obj_field_name, obj);
    1153     1322870 :       free(obj_field_name);
    1154     1322870 :       obj_field_name = NULL;
    1155     1322870 :       saved_state = json_tokener_state_object_sep;
    1156     1322870 :       state = json_tokener_state_eatws;
    1157     1322870 :       goto redo_char;
    1158             : 
    1159     1322870 :     case json_tokener_state_object_sep:
    1160             :       /* { */
    1161     1322870 :       if (c == '}')
    1162             :       {
    1163      320099 :         saved_state = json_tokener_state_finish;
    1164      320099 :         state = json_tokener_state_eatws;
    1165             :       }
    1166     1002770 :       else if (c == ',')
    1167             :       {
    1168     1002770 :         saved_state = json_tokener_state_object_field_start_after_sep;
    1169     1002770 :         state = json_tokener_state_eatws;
    1170             :       }
    1171             :       else
    1172             :       {
    1173           1 :         tok->err = json_tokener_error_parse_object_value_sep;
    1174           1 :         goto out;
    1175             :       }
    1176     1322870 :       break;
    1177             :     }
    1178    10107300 :     (void)ADVANCE_CHAR(str, tok);
    1179    10107300 :     if (!c) // This is the char *before* advancing
    1180           0 :       break;
    1181             :   } /* while(PEEK_CHAR) */
    1182             : 
    1183        6844 : out:
    1184        7780 :   if ((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && (nBytes != 0))
    1185             :   {
    1186           0 :     tok->err = json_tokener_error_parse_utf8_string;
    1187             :   }
    1188        7780 :   if (c && (state == json_tokener_state_finish) && (tok->depth == 0) &&
    1189           3 :       (tok->flags & (JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS)) ==
    1190             :           JSON_TOKENER_STRICT)
    1191             :   {
    1192             :     /* unexpected char after JSON data */
    1193           0 :     tok->err = json_tokener_error_parse_unexpected;
    1194             :   }
    1195        7780 :   if (!c)
    1196             :   {
    1197             :     /* We hit an eof char (0) */
    1198         169 :     if (state != json_tokener_state_finish && saved_state != json_tokener_state_finish)
    1199           1 :       tok->err = json_tokener_error_parse_eof;
    1200             :   }
    1201             : 
    1202             : #ifdef HAVE_USELOCALE
    1203        7780 :   uselocale(oldlocale);
    1204        7780 :   freelocale(newloc);
    1205             : #elif defined(HAVE_SETLOCALE)
    1206             :   setlocale(LC_NUMERIC, oldlocale);
    1207             :   free(oldlocale);
    1208             : #endif
    1209             : 
    1210        7780 :   if (tok->err == json_tokener_success)
    1211             :   {
    1212        7749 :     json_object *ret = json_object_get(current);
    1213             :     int ii;
    1214             : 
    1215             :     /* Partially reset, so we parse additional objects on subsequent calls. */
    1216       15498 :     for (ii = tok->depth; ii >= 0; ii--)
    1217        7749 :       json_tokener_reset_level(tok, ii);
    1218        7749 :     return ret;
    1219             :   }
    1220             : 
    1221             :   MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n", json_tokener_errors[tok->err],
    1222             :            tok->char_offset);
    1223          31 :   return NULL;
    1224             : }
    1225             : 
    1226           0 : static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes)
    1227             : {
    1228           0 :   unsigned char chr = c;
    1229           0 :   if (*nBytes == 0)
    1230             :   {
    1231           0 :     if (chr >= 0x80)
    1232             :     {
    1233           0 :       if ((chr & 0xe0) == 0xc0)
    1234           0 :         *nBytes = 1;
    1235           0 :       else if ((chr & 0xf0) == 0xe0)
    1236           0 :         *nBytes = 2;
    1237           0 :       else if ((chr & 0xf8) == 0xf0)
    1238           0 :         *nBytes = 3;
    1239             :       else
    1240           0 :         return 0;
    1241             :     }
    1242             :   }
    1243             :   else
    1244             :   {
    1245           0 :     if ((chr & 0xC0) != 0x80)
    1246           0 :       return 0;
    1247           0 :     (*nBytes)--;
    1248             :   }
    1249           0 :   return 1;
    1250             : }
    1251             : 
    1252           0 : void json_tokener_set_flags(struct json_tokener *tok, int flags)
    1253             : {
    1254           0 :   tok->flags = flags;
    1255           0 : }
    1256             : 
    1257           0 : size_t json_tokener_get_parse_end(struct json_tokener *tok)
    1258             : {
    1259           0 :   assert(tok->char_offset >= 0); /* Drop this line when char_offset becomes a size_t */
    1260           0 :   return (size_t)tok->char_offset;
    1261             : }
    1262             : 
    1263      133551 : static int json_tokener_parse_double(const char *buf, int len, double *retval)
    1264             : {
    1265             :   char *end;
    1266      133551 :   *retval = CPLStrtod(buf, &end);
    1267      133551 :   if (buf + len == end)
    1268      133551 :     return 0; // It worked
    1269           0 :   return 1;
    1270             : }

Generated by: LCOV version 1.14