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-05-06 16:27:07 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          26 : const char *json_tokener_error_desc(enum json_tokener_error jerr)
     105             : {
     106          26 :   int jerr_int = (int)jerr;
     107          26 :   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          26 :   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        7217 : struct json_tokener *json_tokener_new_ex(int depth)
     126             : {
     127             :   struct json_tokener *tok;
     128             : 
     129        7217 :   tok = (struct json_tokener *)calloc(1, sizeof(struct json_tokener));
     130        7217 :   if (!tok)
     131           0 :     return NULL;
     132        7217 :   tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec));
     133        7217 :   if (!tok->stack)
     134             :   {
     135           0 :     free(tok);
     136           0 :     return NULL;
     137             :   }
     138        7217 :   tok->pb = printbuf_new();
     139        7217 :   tok->max_depth = depth;
     140        7217 :   json_tokener_reset(tok);
     141        7217 :   return tok;
     142             : }
     143             : 
     144        7215 : struct json_tokener *json_tokener_new(void)
     145             : {
     146        7215 :   return json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
     147             : }
     148             : 
     149        7217 : void json_tokener_free(struct json_tokener *tok)
     150             : {
     151        7217 :   json_tokener_reset(tok);
     152        7217 :   if (tok->pb)
     153        7217 :     printbuf_free(tok->pb);
     154        7217 :   free(tok->stack);
     155        7217 :   free(tok);
     156        7217 : }
     157             : 
     158     4053430 : static void json_tokener_reset_level(struct json_tokener *tok, int depth)
     159             : {
     160     4053430 :   tok->stack[depth].state = json_tokener_state_eatws;
     161     4053430 :   tok->stack[depth].saved_state = json_tokener_state_start;
     162     4053430 :   json_object_put(tok->stack[depth].current);
     163     4053430 :   tok->stack[depth].current = NULL;
     164     4053430 :   free(tok->stack[depth].obj_field_name);
     165     4053430 :   tok->stack[depth].obj_field_name = NULL;
     166     4053430 : }
     167             : 
     168       14434 : void json_tokener_reset(struct json_tokener *tok)
     169             : {
     170             :   int i;
     171       14434 :   if (!tok)
     172           0 :     return;
     173             : 
     174       28868 :   for (i = tok->depth; i >= 0; i--)
     175       14434 :     json_tokener_reset_level(tok, i);
     176       14434 :   tok->depth = 0;
     177       14434 :   tok->err = json_tokener_success;
     178             : }
     179             : 
     180          50 : struct json_object *json_tokener_parse(const char *str)
     181             : {
     182             :   enum json_tokener_error jerr_ignored;
     183             :   struct json_object *obj;
     184          50 :   obj = json_tokener_parse_verbose(str, &jerr_ignored);
     185          50 :   return obj;
     186             : }
     187             : 
     188          50 : 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          50 :   tok = json_tokener_new();
     194          50 :   if (!tok)
     195           0 :     return NULL;
     196          50 :   obj = json_tokener_parse_ex(tok, str, -1);
     197          50 :   *error = tok->err;
     198          50 :   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          50 :   json_tokener_free(tok);
     216          50 :   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        7220 : struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
     263             : {
     264        7220 :   struct json_object *obj = NULL;
     265        7220 :   char c = '\1';
     266        7220 :   unsigned int nBytes = 0;
     267        7220 :   unsigned int *nBytesp = &nBytes;
     268             : 
     269             : #ifdef HAVE_USELOCALE
     270        7220 :   locale_t oldlocale = uselocale(NULL);
     271             :   locale_t newloc;
     272             : #elif defined(HAVE_SETLOCALE)
     273             :   char *oldlocale = NULL;
     274             : #endif
     275             : 
     276        7220 :   tok->char_offset = 0;
     277        7220 :   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        7220 :   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        7220 :     locale_t duploc = duplocale(oldlocale);
     294        7220 :     newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
     295        7220 :     if (newloc == NULL)
     296             :     {
     297           0 :       freelocale(duploc);
     298           0 :       return NULL;
     299             :     }
     300        7220 :     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     9711090 :   while (PEEK_CHAR(c, tok)) // Note: c might be '\0' !
     313             :   {
     314             : 
     315     9704770 :   redo_char:
     316    27127700 :     switch (state)
     317             :     {
     318             : 
     319    10628400 :     case json_tokener_state_eatws:
     320             :       /* Advance until we change state */
     321    39082000 :       while (isspace((unsigned char)c))
     322             :       {
     323    28454300 :         if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
     324         711 :           goto out;
     325             :       }
     326    10627700 :       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    10627700 :         state = saved_state;
     335    10627700 :         goto redo_char;
     336             :       }
     337           0 :       break;
     338             : 
     339     2023120 :     case json_tokener_state_start:
     340     2023120 :       switch (c)
     341             :       {
     342      309524 :       case '{':
     343      309524 :         state = json_tokener_state_eatws;
     344      309524 :         saved_state = json_tokener_state_object_field_start;
     345      309524 :         current = json_object_new_object();
     346      309524 :         if (current == NULL)
     347           0 :           goto out;
     348      309524 :         break;
     349      146993 :       case '[':
     350      146993 :         state = json_tokener_state_eatws;
     351      146993 :         saved_state = json_tokener_state_array;
     352      146993 :         current = json_object_new_array();
     353      146993 :         if (current == NULL)
     354           0 :           goto out;
     355      146993 :         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        3012 :       case 'N':
     363             :       case 'n':
     364        3012 :         state = json_tokener_state_null; // or NaN
     365        3012 :         printbuf_reset(tok->pb);
     366        3012 :         tok->st_pos = 0;
     367        3012 :         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     1192820 :         state = json_tokener_state_string;
     378     1192820 :         printbuf_reset(tok->pb);
     379     1192820 :         tok->quote_char = c;
     380     1192820 :         break;
     381       36661 :       case 'T':
     382             :       case 't':
     383             :       case 'F':
     384             :       case 'f':
     385       36661 :         state = json_tokener_state_boolean;
     386       36661 :         printbuf_reset(tok->pb);
     387       36661 :         tok->st_pos = 0;
     388       36661 :         goto redo_char;
     389      334092 :       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      334092 :         state = json_tokener_state_number;
     401      334092 :         printbuf_reset(tok->pb);
     402      334092 :         tok->is_double = 0;
     403      334092 :         goto redo_char;
     404           7 :       default: tok->err = json_tokener_error_parse_unexpected; goto out;
     405             :       }
     406     1649340 :       break;
     407             : 
     408     2016080 :     case json_tokener_state_finish:
     409     2016080 :       if (tok->depth == 0)
     410         170 :         goto out;
     411     2015910 :       obj = json_object_get(current);
     412     2015910 :       json_tokener_reset_level(tok, tok->depth);
     413     2015910 :       tok->depth--;
     414     2015910 :       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       15054 :     case json_tokener_state_null: /* aka starts with 'n' */
     466             :     {
     467             :       int size;
     468             :       int size_nan;
     469       15054 :       printbuf_memappend_fast(tok->pb, &c, 1);
     470       15054 :       size = json_min(tok->st_pos + 1, json_null_str_len);
     471       15054 :       size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
     472       15054 :       if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     473       15054 :            strncasecmp(json_null_str, tok->pb->buf, size) == 0) ||
     474           9 :           (strncmp(json_null_str, tok->pb->buf, size) == 0))
     475             :       {
     476       15045 :         if (tok->st_pos == json_null_str_len)
     477             :         {
     478        3006 :           current = NULL;
     479        3006 :           saved_state = json_tokener_state_finish;
     480        3006 :           state = json_tokener_state_eatws;
     481        3006 :           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       12045 :       tok->st_pos++;
     504             :     }
     505       12045 :     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     1618630 :     case json_tokener_state_string:
     575             :     {
     576             :       /* Advance until we change state */
     577     1618630 :       const char *case_start = str;
     578             :       while (1)
     579             :       {
     580    53619500 :         if (c == tok->quote_char)
     581             :         {
     582     1192820 :           printbuf_memappend_fast(tok->pb, case_start,
     583             :                                   (int)(str - case_start));
     584     2385650 :           current =
     585     1192820 :               json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
     586     1192820 :           if (current == NULL)
     587           0 :             goto out;
     588     1192820 :           saved_state = json_tokener_state_finish;
     589     1192820 :           state = json_tokener_state_eatws;
     590     1192820 :           break;
     591             :         }
     592    52426700 :         else if (c == '\\')
     593             :         {
     594      425801 :           printbuf_memappend_fast(tok->pb, case_start,
     595             :                                   (int)(str - case_start));
     596      425801 :           saved_state = json_tokener_state_string;
     597      425801 :           state = json_tokener_state_string_escape;
     598      425801 :           break;
     599             :         }
     600    52000900 :         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     1618630 :     break;
     609             : 
     610      426418 :     case json_tokener_state_string_escape:
     611      426418 :       switch (c)
     612             :       {
     613      282512 :       case '"':
     614             :       case '\\':
     615             :       case '/':
     616      282512 :         printbuf_memappend_fast(tok->pb, &c, 1);
     617      282512 :         state = saved_state;
     618      282512 :         break;
     619      143756 :       case 'b':
     620             :       case 'n':
     621             :       case 'r':
     622             :       case 't':
     623             :       case 'f':
     624      143756 :         if (c == 'b')
     625           0 :           printbuf_memappend_fast(tok->pb, "\b", 1);
     626      143756 :         else if (c == 'n')
     627      143756 :           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      143756 :         state = saved_state;
     635      143756 :         break;
     636         150 :       case 'u':
     637         150 :         tok->ucs_char = 0;
     638         150 :         tok->st_pos = 0;
     639         150 :         state = json_tokener_state_escape_unicode;
     640         150 :         break;
     641           0 :       default: tok->err = json_tokener_error_parse_string; goto out;
     642             :       }
     643      426418 :       break;
     644             : 
     645             :       // ===================================================
     646             : 
     647         600 :     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         600 :         if (!c || !strchr(json_hex_chars, c))
     653             :         {
     654           0 :           tok->err = json_tokener_error_parse_string;
     655           0 :           goto out;
     656             :         }
     657        1200 :         tok->ucs_char |=
     658         600 :             ((unsigned int)jt_hexdigit(c) << ((3 - tok->st_pos) * 4));
     659         600 :         tok->st_pos++;
     660         600 :         if (tok->st_pos >= 4)
     661         150 :           break;
     662             : 
     663         450 :         (void)ADVANCE_CHAR(str, tok);
     664         450 :         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         150 :       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         150 :       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         150 :       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          15 :       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          15 :       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          15 :       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          15 :       else if (tok->ucs_char < 0x10000)
     739             :       {
     740             :         unsigned char unescaped_utf[3];
     741          15 :         unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
     742          15 :         unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
     743          15 :         unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
     744          15 :         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         150 :       state = saved_state; // i.e. _state_string or _state_object_field
     761             :     }
     762         150 :     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      183349 :     case json_tokener_state_boolean:
     805             :     {
     806             :       int size1, size2;
     807      183349 :       printbuf_memappend_fast(tok->pb, &c, 1);
     808      183349 :       size1 = json_min(tok->st_pos + 1, json_true_str_len);
     809      183349 :       size2 = json_min(tok->st_pos + 1, json_false_str_len);
     810      183349 :       if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     811      183349 :            strncasecmp(json_true_str, tok->pb->buf, size1) == 0) ||
     812         349 :           (strncmp(json_true_str, tok->pb->buf, size1) == 0))
     813             :       {
     814      183000 :         if (tok->st_pos == json_true_str_len)
     815             :         {
     816       36600 :           current = json_object_new_boolean(1);
     817       36600 :           if (current == NULL)
     818           0 :             goto out;
     819       36600 :           saved_state = json_tokener_state_finish;
     820       36600 :           state = json_tokener_state_eatws;
     821       36600 :           goto redo_char;
     822             :         }
     823             :       }
     824         349 :       else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
     825         349 :                 strncasecmp(json_false_str, tok->pb->buf, size2) == 0) ||
     826           4 :                (strncmp(json_false_str, tok->pb->buf, size2) == 0))
     827             :       {
     828         345 :         if (tok->st_pos == json_false_str_len)
     829             :         {
     830          56 :           current = json_object_new_boolean(0);
     831          56 :           if (current == NULL)
     832           0 :             goto out;
     833          56 :           saved_state = json_tokener_state_finish;
     834          56 :           state = json_tokener_state_eatws;
     835          56 :           goto redo_char;
     836             :         }
     837             :       }
     838             :       else
     839             :       {
     840           4 :         tok->err = json_tokener_error_parse_boolean;
     841           4 :         goto out;
     842             :       }
     843      146689 :       tok->st_pos++;
     844             :     }
     845      146689 :     break;
     846             : 
     847      334092 :     case json_tokener_state_number:
     848             :     {
     849             :       /* Advance until we change state */
     850      334092 :       const char *case_start = str;
     851      334092 :       int case_len = 0;
     852      334092 :       int is_exponent = 0;
     853      334092 :       int neg_sign_ok = 1;
     854      334092 :       int pos_sign_ok = 0;
     855      334092 :       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     1543230 :       while (c && ((c >= '0' && c <= '9') ||
     880      413772 :                    (!is_exponent && (c == 'e' || c == 'E')) ||
     881      413731 :                    (neg_sign_ok && c == '-') || (pos_sign_ok && c == '+') ||
     882      395079 :                    (!tok->is_double && c == '.')))
     883             :       {
     884     1209140 :         pos_sign_ok = neg_sign_ok = 0;
     885     1209140 :         ++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     1209140 :         switch (c)
     894             :         {
     895       60987 :         case '.':
     896       60987 :           tok->is_double = 1;
     897       60987 :           pos_sign_ok = 1;
     898       60987 :           neg_sign_ok = 1;
     899       60987 :           break;
     900          41 :         case 'e': /* FALLTHRU */
     901             :         case 'E':
     902          41 :           is_exponent = 1;
     903          41 :           tok->is_double = 1;
     904             :           /* the exponent part can begin with a negative sign */
     905          41 :           pos_sign_ok = neg_sign_ok = 1;
     906          41 :           break;
     907     1148110 :         default: break;
     908             :         }
     909             : 
     910     1209140 :         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      334092 :       if (tok->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' &&
     925       36656 :           c != 'I' && c != 'i' && !isspace((unsigned char)c))
     926             :       {
     927           0 :         tok->err = json_tokener_error_parse_number;
     928           0 :         goto out;
     929             :       }
     930      334092 :       if (case_len > 0)
     931      334092 :         printbuf_memappend_fast(tok->pb, case_start, case_len);
     932             : 
     933             :       // Check for -Infinity
     934      334092 :       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      334089 :       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       60993 :         while (printbuf_length(tok->pb) > 1)
     945             :         {
     946       60993 :           char last_char = tok->pb->buf[printbuf_length(tok->pb) - 1];
     947       60993 :           if (last_char != 'e' && last_char != 'E' &&
     948       60993 :               last_char != '-' && last_char != '+')
     949             :           {
     950       60993 :             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      346346 :         if (!tok->is_double && tok->pb->buf[0] == '-' &&
     962       12257 :             json_parse_int64(tok->pb->buf, &num64) == 0)
     963             :         {
     964       12257 :           current = json_object_new_int64(num64);
     965       12257 :           if (current == NULL)
     966           0 :             goto out;
     967             :         }
     968      582671 :         else if (!tok->is_double && tok->pb->buf[0] != '-' &&
     969      260839 :                  json_parse_uint64(tok->pb->buf, &numuint64) == 0)
     970             :         {
     971      260839 :           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      260839 :           if (numuint64 <= INT64_MAX)
     978             :           {
     979      260838 :             num64 = (uint64_t)numuint64;
     980      260838 :             current = json_object_new_int64(num64);
     981      260838 :             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      121986 :         else if (tok->is_double &&
     992       60993 :                  json_tokener_parse_double(
     993       60993 :                      tok->pb->buf, printbuf_length(tok->pb), &numd) == 0)
     994             :         {
     995       60993 :           current = json_object_new_double_s(numd, tok->pb->buf);
     996       60993 :           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      334089 :         saved_state = json_tokener_state_finish;
    1005      334089 :         state = json_tokener_state_eatws;
    1006      334089 :         goto redo_char;
    1007             :       }
    1008             :       break;
    1009             : 
    1010      739084 :     case json_tokener_state_array_after_sep:
    1011             :     case json_tokener_state_array:
    1012      739084 :       if (c == ']')
    1013             :       {
    1014             :         // Minimize memory usage; assume parsed objs are unlikely to be changed
    1015         597 :         json_object_array_shrink(current, 0);
    1016             : 
    1017         597 :         if (state == json_tokener_state_array_after_sep &&
    1018          21 :             (tok->flags & JSON_TOKENER_STRICT))
    1019             :         {
    1020           0 :           tok->err = json_tokener_error_parse_unexpected;
    1021           0 :           goto out;
    1022             :         }
    1023         597 :         saved_state = json_tokener_state_finish;
    1024         597 :         state = json_tokener_state_eatws;
    1025             :       }
    1026             :       else
    1027             :       {
    1028      738487 :         if (tok->depth >= tok->max_depth - 1)
    1029             :         {
    1030           0 :           tok->err = json_tokener_error_depth;
    1031           0 :           goto out;
    1032             :         }
    1033      738487 :         state = json_tokener_state_array_add;
    1034      738487 :         tok->depth++;
    1035      738487 :         json_tokener_reset_level(tok, tok->depth);
    1036      738487 :         goto redo_char;
    1037             :       }
    1038         597 :       break;
    1039             : 
    1040      738487 :     case json_tokener_state_array_add:
    1041      738487 :       if (json_object_array_add(current, obj) != 0)
    1042           0 :         goto out;
    1043      738487 :       saved_state = json_tokener_state_array_sep;
    1044      738487 :       state = json_tokener_state_eatws;
    1045      738487 :       goto redo_char;
    1046             : 
    1047      738487 :     case json_tokener_state_array_sep:
    1048      738487 :       if (c == ']')
    1049             :       {
    1050             :         // Minimize memory usage; assume parsed objs are unlikely to be changed
    1051      146396 :         json_object_array_shrink(current, 0);
    1052             : 
    1053      146396 :         saved_state = json_tokener_state_finish;
    1054      146396 :         state = json_tokener_state_eatws;
    1055             :       }
    1056      592091 :       else if (c == ',')
    1057             :       {
    1058      592091 :         saved_state = json_tokener_state_array_after_sep;
    1059      592091 :         state = json_tokener_state_eatws;
    1060             :       }
    1061             :       else
    1062             :       {
    1063           0 :         tok->err = json_tokener_error_parse_array;
    1064           0 :         goto out;
    1065             :       }
    1066      738487 :       break;
    1067             : 
    1068     1278650 :     case json_tokener_state_object_field_start:
    1069             :     case json_tokener_state_object_field_start_after_sep:
    1070     1278650 :       if (c == '}')
    1071             :       {
    1072        1224 :         if (state == json_tokener_state_object_field_start_after_sep &&
    1073         132 :             (tok->flags & JSON_TOKENER_STRICT))
    1074             :         {
    1075           0 :           tok->err = json_tokener_error_parse_unexpected;
    1076           0 :           goto out;
    1077             :         }
    1078        1224 :         saved_state = json_tokener_state_finish;
    1079        1224 :         state = json_tokener_state_eatws;
    1080             :       }
    1081     1277420 :       else if (c == '"' || c == '\'')
    1082             :       {
    1083     1277420 :         tok->quote_char = c;
    1084     1277420 :         printbuf_reset(tok->pb);
    1085     1277420 :         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     1278640 :       break;
    1093             : 
    1094     1278040 :     case json_tokener_state_object_field:
    1095             :     {
    1096             :       /* Advance until we change state */
    1097     1278040 :       const char *case_start = str;
    1098             :       while (1)
    1099             :       {
    1100     7598690 :         if (c == tok->quote_char)
    1101             :         {
    1102     1277420 :           printbuf_memappend_fast(tok->pb, case_start,
    1103             :                                   (int)(str - case_start));
    1104     1277420 :           obj_field_name = strdup(tok->pb->buf);
    1105     1277420 :           saved_state = json_tokener_state_object_field_end;
    1106     1277420 :           state = json_tokener_state_eatws;
    1107     1277420 :           break;
    1108             :         }
    1109     6321270 :         else if (c == '\\')
    1110             :         {
    1111         617 :           printbuf_memappend_fast(tok->pb, case_start,
    1112             :                                   (int)(str - case_start));
    1113         617 :           saved_state = json_tokener_state_object_field;
    1114         617 :           state = json_tokener_state_string_escape;
    1115         617 :           break;
    1116             :         }
    1117     6320660 :         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     1278040 :     break;
    1126             : 
    1127     1277420 :     case json_tokener_state_object_field_end:
    1128     1277420 :       if (c == ':')
    1129             :       {
    1130     1277420 :         saved_state = json_tokener_state_object_value;
    1131     1277420 :         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     1277420 :       break;
    1139             : 
    1140     1277420 :     case json_tokener_state_object_value:
    1141     1277420 :       if (tok->depth >= tok->max_depth - 1)
    1142             :       {
    1143           0 :         tok->err = json_tokener_error_depth;
    1144           0 :         goto out;
    1145             :       }
    1146     1277420 :       state = json_tokener_state_object_value_add;
    1147     1277420 :       tok->depth++;
    1148     1277420 :       json_tokener_reset_level(tok, tok->depth);
    1149     1277420 :       goto redo_char;
    1150             : 
    1151     1277420 :     case json_tokener_state_object_value_add:
    1152     1277420 :       json_object_object_add(current, obj_field_name, obj);
    1153     1277420 :       free(obj_field_name);
    1154     1277420 :       obj_field_name = NULL;
    1155     1277420 :       saved_state = json_tokener_state_object_sep;
    1156     1277420 :       state = json_tokener_state_eatws;
    1157     1277420 :       goto redo_char;
    1158             : 
    1159     1277420 :     case json_tokener_state_object_sep:
    1160             :       /* { */
    1161     1277420 :       if (c == '}')
    1162             :       {
    1163      308291 :         saved_state = json_tokener_state_finish;
    1164      308291 :         state = json_tokener_state_eatws;
    1165             :       }
    1166      969128 :       else if (c == ',')
    1167             :       {
    1168      969127 :         saved_state = json_tokener_state_object_field_start_after_sep;
    1169      969127 :         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     1277420 :       break;
    1177             :     }
    1178     9703870 :     (void)ADVANCE_CHAR(str, tok);
    1179     9703870 :     if (!c) // This is the char *before* advancing
    1180           0 :       break;
    1181             :   } /* while(PEEK_CHAR) */
    1182             : 
    1183        6317 : out:
    1184        7220 :   if ((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && (nBytes != 0))
    1185             :   {
    1186           0 :     tok->err = json_tokener_error_parse_utf8_string;
    1187             :   }
    1188        7220 :   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        7220 :   if (!c)
    1196             :   {
    1197             :     /* We hit an eof char (0) */
    1198         168 :     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        7220 :   uselocale(oldlocale);
    1204        7220 :   freelocale(newloc);
    1205             : #elif defined(HAVE_SETLOCALE)
    1206             :   setlocale(LC_NUMERIC, oldlocale);
    1207             :   free(oldlocale);
    1208             : #endif
    1209             : 
    1210        7220 :   if (tok->err == json_tokener_success)
    1211             :   {
    1212        7187 :     json_object *ret = json_object_get(current);
    1213             :     int ii;
    1214             : 
    1215             :     /* Partially reset, so we parse additional objects on subsequent calls. */
    1216       14374 :     for (ii = tok->depth; ii >= 0; ii--)
    1217        7187 :       json_tokener_reset_level(tok, ii);
    1218        7187 :     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          33 :   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       60993 : static int json_tokener_parse_double(const char *buf, int len, double *retval)
    1264             : {
    1265             :   char *end;
    1266       60993 :   *retval = CPLStrtod(buf, &end);
    1267       60993 :   if (buf + len == end)
    1268       60993 :     return 0; // It worked
    1269           0 :   return 1;
    1270             : }

Generated by: LCOV version 1.14