cJSON.c
changeset 58 1390348facc7
equal deleted inserted replaced
57:4974e5368a29 58:1390348facc7
       
     1 /*
       
     2   Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
       
     3 
       
     4   Permission is hereby granted, free of charge, to any person obtaining a copy
       
     5   of this software and associated documentation files (the "Software"), to deal
       
     6   in the Software without restriction, including without limitation the rights
       
     7   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       
     8   copies of the Software, and to permit persons to whom the Software is
       
     9   furnished to do so, subject to the following conditions:
       
    10 
       
    11   The above copyright notice and this permission notice shall be included in
       
    12   all copies or substantial portions of the Software.
       
    13 
       
    14   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    15   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    16   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
       
    17   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    18   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    19   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
       
    20   THE SOFTWARE.
       
    21 */
       
    22 
       
    23 /* cJSON */
       
    24 /* JSON parser in C. */
       
    25 
       
    26 #ifdef __GNUC__
       
    27 #pragma GCC visibility push(default)
       
    28 #endif
       
    29 
       
    30 #include <string.h>
       
    31 #include <stdio.h>
       
    32 #include <math.h>
       
    33 #include <stdlib.h>
       
    34 #include <float.h>
       
    35 #include <limits.h>
       
    36 #include <ctype.h>
       
    37 #include <locale.h>
       
    38 
       
    39 #ifdef __GNUC__
       
    40 #pragma GCC visibility pop
       
    41 #endif
       
    42 
       
    43 #include "cJSON.h"
       
    44 
       
    45 /* define our own boolean type */
       
    46 #define true ((cJSON_bool)1)
       
    47 #define false ((cJSON_bool)0)
       
    48 
       
    49 typedef struct {
       
    50     const unsigned char *json;
       
    51     size_t position;
       
    52 } error;
       
    53 static error global_error = { NULL, 0 };
       
    54 
       
    55 CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
       
    56 {
       
    57     return (const char*) (global_error.json + global_error.position);
       
    58 }
       
    59 
       
    60 /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
       
    61 #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 5) || (CJSON_VERSION_PATCH != 5)
       
    62     #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
       
    63 #endif
       
    64 
       
    65 CJSON_PUBLIC(const char*) cJSON_Version(void)
       
    66 {
       
    67     static char version[15];
       
    68     sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
       
    69 
       
    70     return version;
       
    71 }
       
    72 
       
    73 /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
       
    74 static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
       
    75 {
       
    76     if ((string1 == NULL) || (string2 == NULL))
       
    77     {
       
    78         return 1;
       
    79     }
       
    80 
       
    81     if (string1 == string2)
       
    82     {
       
    83         return 0;
       
    84     }
       
    85 
       
    86     for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
       
    87     {
       
    88         if (*string1 == '\0')
       
    89         {
       
    90             return 0;
       
    91         }
       
    92     }
       
    93 
       
    94     return tolower(*string1) - tolower(*string2);
       
    95 }
       
    96 
       
    97 typedef struct internal_hooks
       
    98 {
       
    99     void *(*allocate)(size_t size);
       
   100     void (*deallocate)(void *pointer);
       
   101     void *(*reallocate)(void *pointer, size_t size);
       
   102 } internal_hooks;
       
   103 
       
   104 static internal_hooks global_hooks = { malloc, free, realloc };
       
   105 
       
   106 static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
       
   107 {
       
   108     size_t length = 0;
       
   109     unsigned char *copy = NULL;
       
   110 
       
   111     if (string == NULL)
       
   112     {
       
   113         return NULL;
       
   114     }
       
   115 
       
   116     length = strlen((const char*)string) + sizeof("");
       
   117     if (!(copy = (unsigned char*)hooks->allocate(length)))
       
   118     {
       
   119         return NULL;
       
   120     }
       
   121     memcpy(copy, string, length);
       
   122 
       
   123     return copy;
       
   124 }
       
   125 
       
   126 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
       
   127 {
       
   128     if (hooks == NULL)
       
   129     {
       
   130         /* Reset hooks */
       
   131         global_hooks.allocate = malloc;
       
   132         global_hooks.deallocate = free;
       
   133         global_hooks.reallocate = realloc;
       
   134         return;
       
   135     }
       
   136 
       
   137     global_hooks.allocate = malloc;
       
   138     if (hooks->malloc_fn != NULL)
       
   139     {
       
   140         global_hooks.allocate = hooks->malloc_fn;
       
   141     }
       
   142 
       
   143     global_hooks.deallocate = free;
       
   144     if (hooks->free_fn != NULL)
       
   145     {
       
   146         global_hooks.deallocate = hooks->free_fn;
       
   147     }
       
   148 
       
   149     /* use realloc only if both free and malloc are used */
       
   150     global_hooks.reallocate = NULL;
       
   151     if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
       
   152     {
       
   153         global_hooks.reallocate = realloc;
       
   154     }
       
   155 }
       
   156 
       
   157 /* Internal constructor. */
       
   158 static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
       
   159 {
       
   160     cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
       
   161     if (node)
       
   162     {
       
   163         memset(node, '\0', sizeof(cJSON));
       
   164     }
       
   165 
       
   166     return node;
       
   167 }
       
   168 
       
   169 /* Delete a cJSON structure. */
       
   170 CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
       
   171 {
       
   172     cJSON *next = NULL;
       
   173     while (item != NULL)
       
   174     {
       
   175         next = item->next;
       
   176         if (!(item->type & cJSON_IsReference) && (item->child != NULL))
       
   177         {
       
   178             cJSON_Delete(item->child);
       
   179         }
       
   180         if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
       
   181         {
       
   182             global_hooks.deallocate(item->valuestring);
       
   183         }
       
   184         if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
       
   185         {
       
   186             global_hooks.deallocate(item->string);
       
   187         }
       
   188         global_hooks.deallocate(item);
       
   189         item = next;
       
   190     }
       
   191 }
       
   192 
       
   193 /* get the decimal point character of the current locale */
       
   194 static unsigned char get_decimal_point(void)
       
   195 {
       
   196     struct lconv *lconv = localeconv();
       
   197     return (unsigned char) lconv->decimal_point[0];
       
   198 }
       
   199 
       
   200 typedef struct
       
   201 {
       
   202     const unsigned char *content;
       
   203     size_t length;
       
   204     size_t offset;
       
   205     size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
       
   206     internal_hooks hooks;
       
   207 } parse_buffer;
       
   208 
       
   209 /* check if the given size is left to read in a given parse buffer (starting with 1) */
       
   210 #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
       
   211 #define cannot_read(buffer, size) (!can_read(buffer, size))
       
   212 /* check if the buffer can be accessed at the given index (starting with 0) */
       
   213 #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
       
   214 #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
       
   215 /* get a pointer to the buffer at the position */
       
   216 #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
       
   217 
       
   218 /* Parse the input text to generate a number, and populate the result into item. */
       
   219 static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
       
   220 {
       
   221     double number = 0;
       
   222     unsigned char *after_end = NULL;
       
   223     unsigned char number_c_string[64];
       
   224     unsigned char decimal_point = get_decimal_point();
       
   225     size_t i = 0;
       
   226 
       
   227     if ((input_buffer == NULL) || (input_buffer->content == NULL))
       
   228     {
       
   229         return false;
       
   230     }
       
   231 
       
   232     /* copy the number into a temporary buffer and replace '.' with the decimal point
       
   233      * of the current locale (for strtod)
       
   234      * This also takes care of '\0' not necessarily being available for marking the end of the input */
       
   235     for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
       
   236     {
       
   237         switch (buffer_at_offset(input_buffer)[i])
       
   238         {
       
   239             case '0':
       
   240             case '1':
       
   241             case '2':
       
   242             case '3':
       
   243             case '4':
       
   244             case '5':
       
   245             case '6':
       
   246             case '7':
       
   247             case '8':
       
   248             case '9':
       
   249             case '+':
       
   250             case '-':
       
   251             case 'e':
       
   252             case 'E':
       
   253                 number_c_string[i] = buffer_at_offset(input_buffer)[i];
       
   254                 break;
       
   255 
       
   256             case '.':
       
   257                 number_c_string[i] = decimal_point;
       
   258                 break;
       
   259 
       
   260             default:
       
   261                 goto loop_end;
       
   262         }
       
   263     }
       
   264 loop_end:
       
   265     number_c_string[i] = '\0';
       
   266 
       
   267     number = strtod((const char*)number_c_string, (char**)&after_end);
       
   268     if (number_c_string == after_end)
       
   269     {
       
   270         return false; /* parse_error */
       
   271     }
       
   272 
       
   273     item->valuedouble = number;
       
   274 
       
   275     /* use saturation in case of overflow */
       
   276     if (number >= INT_MAX)
       
   277     {
       
   278         item->valueint = INT_MAX;
       
   279     }
       
   280     else if (number <= INT_MIN)
       
   281     {
       
   282         item->valueint = INT_MIN;
       
   283     }
       
   284     else
       
   285     {
       
   286         item->valueint = (int)number;
       
   287     }
       
   288 
       
   289     item->type = cJSON_Number;
       
   290 
       
   291     input_buffer->offset += (size_t)(after_end - number_c_string);
       
   292     return true;
       
   293 }
       
   294 
       
   295 /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
       
   296 CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
       
   297 {
       
   298     if (number >= INT_MAX)
       
   299     {
       
   300         object->valueint = INT_MAX;
       
   301     }
       
   302     else if (number <= INT_MIN)
       
   303     {
       
   304         object->valueint = INT_MIN;
       
   305     }
       
   306     else
       
   307     {
       
   308         object->valueint = (int)number;
       
   309     }
       
   310 
       
   311     return object->valuedouble = number;
       
   312 }
       
   313 
       
   314 typedef struct
       
   315 {
       
   316     unsigned char *buffer;
       
   317     size_t length;
       
   318     size_t offset;
       
   319     size_t depth; /* current nesting depth (for formatted printing) */
       
   320     cJSON_bool noalloc;
       
   321     cJSON_bool format; /* is this print a formatted print */
       
   322     internal_hooks hooks;
       
   323 } printbuffer;
       
   324 
       
   325 /* realloc printbuffer if necessary to have at least "needed" bytes more */
       
   326 static unsigned char* ensure(printbuffer * const p, size_t needed)
       
   327 {
       
   328     unsigned char *newbuffer = NULL;
       
   329     size_t newsize = 0;
       
   330 
       
   331     if ((p == NULL) || (p->buffer == NULL))
       
   332     {
       
   333         return NULL;
       
   334     }
       
   335 
       
   336     if ((p->length > 0) && (p->offset >= p->length))
       
   337     {
       
   338         /* make sure that offset is valid */
       
   339         return NULL;
       
   340     }
       
   341 
       
   342     if (needed > INT_MAX)
       
   343     {
       
   344         /* sizes bigger than INT_MAX are currently not supported */
       
   345         return NULL;
       
   346     }
       
   347 
       
   348     needed += p->offset + 1;
       
   349     if (needed <= p->length)
       
   350     {
       
   351         return p->buffer + p->offset;
       
   352     }
       
   353 
       
   354     if (p->noalloc) {
       
   355         return NULL;
       
   356     }
       
   357 
       
   358     /* calculate new buffer size */
       
   359     if (needed > (INT_MAX / 2))
       
   360     {
       
   361         /* overflow of int, use INT_MAX if possible */
       
   362         if (needed <= INT_MAX)
       
   363         {
       
   364             newsize = INT_MAX;
       
   365         }
       
   366         else
       
   367         {
       
   368             return NULL;
       
   369         }
       
   370     }
       
   371     else
       
   372     {
       
   373         newsize = needed * 2;
       
   374     }
       
   375 
       
   376     if (p->hooks.reallocate != NULL)
       
   377     {
       
   378         /* reallocate with realloc if available */
       
   379         newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
       
   380     }
       
   381     else
       
   382     {
       
   383         /* otherwise reallocate manually */
       
   384         newbuffer = (unsigned char*)p->hooks.allocate(newsize);
       
   385         if (!newbuffer)
       
   386         {
       
   387             p->hooks.deallocate(p->buffer);
       
   388             p->length = 0;
       
   389             p->buffer = NULL;
       
   390 
       
   391             return NULL;
       
   392         }
       
   393         if (newbuffer)
       
   394         {
       
   395             memcpy(newbuffer, p->buffer, p->offset + 1);
       
   396         }
       
   397         p->hooks.deallocate(p->buffer);
       
   398     }
       
   399     p->length = newsize;
       
   400     p->buffer = newbuffer;
       
   401 
       
   402     return newbuffer + p->offset;
       
   403 }
       
   404 
       
   405 /* calculate the new length of the string in a printbuffer and update the offset */
       
   406 static void update_offset(printbuffer * const buffer)
       
   407 {
       
   408     const unsigned char *buffer_pointer = NULL;
       
   409     if ((buffer == NULL) || (buffer->buffer == NULL))
       
   410     {
       
   411         return;
       
   412     }
       
   413     buffer_pointer = buffer->buffer + buffer->offset;
       
   414 
       
   415     buffer->offset += strlen((const char*)buffer_pointer);
       
   416 }
       
   417 
       
   418 /* Render the number nicely from the given item into a string. */
       
   419 static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
       
   420 {
       
   421     unsigned char *output_pointer = NULL;
       
   422     double d = item->valuedouble;
       
   423     int length = 0;
       
   424     size_t i = 0;
       
   425     unsigned char number_buffer[26]; /* temporary buffer to print the number into */
       
   426     unsigned char decimal_point = get_decimal_point();
       
   427     double test;
       
   428 
       
   429     if (output_buffer == NULL)
       
   430     {
       
   431         return false;
       
   432     }
       
   433 
       
   434     /* This checks for NaN and Infinity */
       
   435     if ((d * 0) != 0)
       
   436     {
       
   437         length = sprintf((char*)number_buffer, "null");
       
   438     }
       
   439     else
       
   440     {
       
   441         /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
       
   442         length = sprintf((char*)number_buffer, "%1.15g", d);
       
   443 
       
   444         /* Check whether the original double can be recovered */
       
   445         if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d))
       
   446         {
       
   447             /* If not, print with 17 decimal places of precision */
       
   448             length = sprintf((char*)number_buffer, "%1.17g", d);
       
   449         }
       
   450     }
       
   451 
       
   452     /* sprintf failed or buffer overrun occured */
       
   453     if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
       
   454     {
       
   455         return false;
       
   456     }
       
   457 
       
   458     /* reserve appropriate space in the output */
       
   459     output_pointer = ensure(output_buffer, (size_t)length);
       
   460     if (output_pointer == NULL)
       
   461     {
       
   462         return false;
       
   463     }
       
   464 
       
   465     /* copy the printed number to the output and replace locale
       
   466      * dependent decimal point with '.' */
       
   467     for (i = 0; i < ((size_t)length); i++)
       
   468     {
       
   469         if (number_buffer[i] == decimal_point)
       
   470         {
       
   471             output_pointer[i] = '.';
       
   472             continue;
       
   473         }
       
   474 
       
   475         output_pointer[i] = number_buffer[i];
       
   476     }
       
   477     output_pointer[i] = '\0';
       
   478 
       
   479     output_buffer->offset += (size_t)length;
       
   480 
       
   481     return true;
       
   482 }
       
   483 
       
   484 /* parse 4 digit hexadecimal number */
       
   485 static unsigned parse_hex4(const unsigned char * const input)
       
   486 {
       
   487     unsigned int h = 0;
       
   488     size_t i = 0;
       
   489 
       
   490     for (i = 0; i < 4; i++)
       
   491     {
       
   492         /* parse digit */
       
   493         if ((input[i] >= '0') && (input[i] <= '9'))
       
   494         {
       
   495             h += (unsigned int) input[i] - '0';
       
   496         }
       
   497         else if ((input[i] >= 'A') && (input[i] <= 'F'))
       
   498         {
       
   499             h += (unsigned int) 10 + input[i] - 'A';
       
   500         }
       
   501         else if ((input[i] >= 'a') && (input[i] <= 'f'))
       
   502         {
       
   503             h += (unsigned int) 10 + input[i] - 'a';
       
   504         }
       
   505         else /* invalid */
       
   506         {
       
   507             return 0;
       
   508         }
       
   509 
       
   510         if (i < 3)
       
   511         {
       
   512             /* shift left to make place for the next nibble */
       
   513             h = h << 4;
       
   514         }
       
   515     }
       
   516 
       
   517     return h;
       
   518 }
       
   519 
       
   520 /* converts a UTF-16 literal to UTF-8
       
   521  * A literal can be one or two sequences of the form \uXXXX */
       
   522 static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
       
   523 {
       
   524     long unsigned int codepoint = 0;
       
   525     unsigned int first_code = 0;
       
   526     const unsigned char *first_sequence = input_pointer;
       
   527     unsigned char utf8_length = 0;
       
   528     unsigned char utf8_position = 0;
       
   529     unsigned char sequence_length = 0;
       
   530     unsigned char first_byte_mark = 0;
       
   531 
       
   532     if ((input_end - first_sequence) < 6)
       
   533     {
       
   534         /* input ends unexpectedly */
       
   535         goto fail;
       
   536     }
       
   537 
       
   538     /* get the first utf16 sequence */
       
   539     first_code = parse_hex4(first_sequence + 2);
       
   540 
       
   541     /* check that the code is valid */
       
   542     if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
       
   543     {
       
   544         goto fail;
       
   545     }
       
   546 
       
   547     /* UTF16 surrogate pair */
       
   548     if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
       
   549     {
       
   550         const unsigned char *second_sequence = first_sequence + 6;
       
   551         unsigned int second_code = 0;
       
   552         sequence_length = 12; /* \uXXXX\uXXXX */
       
   553 
       
   554         if ((input_end - second_sequence) < 6)
       
   555         {
       
   556             /* input ends unexpectedly */
       
   557             goto fail;
       
   558         }
       
   559 
       
   560         if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
       
   561         {
       
   562             /* missing second half of the surrogate pair */
       
   563             goto fail;
       
   564         }
       
   565 
       
   566         /* get the second utf16 sequence */
       
   567         second_code = parse_hex4(second_sequence + 2);
       
   568         /* check that the code is valid */
       
   569         if ((second_code < 0xDC00) || (second_code > 0xDFFF))
       
   570         {
       
   571             /* invalid second half of the surrogate pair */
       
   572             goto fail;
       
   573         }
       
   574 
       
   575 
       
   576         /* calculate the unicode codepoint from the surrogate pair */
       
   577         codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
       
   578     }
       
   579     else
       
   580     {
       
   581         sequence_length = 6; /* \uXXXX */
       
   582         codepoint = first_code;
       
   583     }
       
   584 
       
   585     /* encode as UTF-8
       
   586      * takes at maximum 4 bytes to encode:
       
   587      * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
       
   588     if (codepoint < 0x80)
       
   589     {
       
   590         /* normal ascii, encoding 0xxxxxxx */
       
   591         utf8_length = 1;
       
   592     }
       
   593     else if (codepoint < 0x800)
       
   594     {
       
   595         /* two bytes, encoding 110xxxxx 10xxxxxx */
       
   596         utf8_length = 2;
       
   597         first_byte_mark = 0xC0; /* 11000000 */
       
   598     }
       
   599     else if (codepoint < 0x10000)
       
   600     {
       
   601         /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
       
   602         utf8_length = 3;
       
   603         first_byte_mark = 0xE0; /* 11100000 */
       
   604     }
       
   605     else if (codepoint <= 0x10FFFF)
       
   606     {
       
   607         /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
       
   608         utf8_length = 4;
       
   609         first_byte_mark = 0xF0; /* 11110000 */
       
   610     }
       
   611     else
       
   612     {
       
   613         /* invalid unicode codepoint */
       
   614         goto fail;
       
   615     }
       
   616 
       
   617     /* encode as utf8 */
       
   618     for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
       
   619     {
       
   620         /* 10xxxxxx */
       
   621         (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
       
   622         codepoint >>= 6;
       
   623     }
       
   624     /* encode first byte */
       
   625     if (utf8_length > 1)
       
   626     {
       
   627         (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
       
   628     }
       
   629     else
       
   630     {
       
   631         (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
       
   632     }
       
   633 
       
   634     *output_pointer += utf8_length;
       
   635 
       
   636     return sequence_length;
       
   637 
       
   638 fail:
       
   639     return 0;
       
   640 }
       
   641 
       
   642 /* Parse the input text into an unescaped cinput, and populate item. */
       
   643 static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
       
   644 {
       
   645     const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
       
   646     const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
       
   647     unsigned char *output_pointer = NULL;
       
   648     unsigned char *output = NULL;
       
   649 
       
   650     /* not a string */
       
   651     if (buffer_at_offset(input_buffer)[0] != '\"')
       
   652     {
       
   653         goto fail;
       
   654     }
       
   655 
       
   656     {
       
   657         /* calculate approximate size of the output (overestimate) */
       
   658         size_t allocation_length = 0;
       
   659         size_t skipped_bytes = 0;
       
   660         while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
       
   661         {
       
   662             /* is escape sequence */
       
   663             if (input_end[0] == '\\')
       
   664             {
       
   665                 if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
       
   666                 {
       
   667                     /* prevent buffer overflow when last input character is a backslash */
       
   668                     goto fail;
       
   669                 }
       
   670                 skipped_bytes++;
       
   671                 input_end++;
       
   672             }
       
   673             input_end++;
       
   674         }
       
   675         if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
       
   676         {
       
   677             goto fail; /* string ended unexpectedly */
       
   678         }
       
   679 
       
   680         /* This is at most how much we need for the output */
       
   681         allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
       
   682         output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
       
   683         if (output == NULL)
       
   684         {
       
   685             goto fail; /* allocation failure */
       
   686         }
       
   687     }
       
   688 
       
   689     output_pointer = output;
       
   690     /* loop through the string literal */
       
   691     while (input_pointer < input_end)
       
   692     {
       
   693         if (*input_pointer != '\\')
       
   694         {
       
   695             *output_pointer++ = *input_pointer++;
       
   696         }
       
   697         /* escape sequence */
       
   698         else
       
   699         {
       
   700             unsigned char sequence_length = 2;
       
   701             if ((input_end - input_pointer) < 1)
       
   702             {
       
   703                 goto fail;
       
   704             }
       
   705 
       
   706             switch (input_pointer[1])
       
   707             {
       
   708                 case 'b':
       
   709                     *output_pointer++ = '\b';
       
   710                     break;
       
   711                 case 'f':
       
   712                     *output_pointer++ = '\f';
       
   713                     break;
       
   714                 case 'n':
       
   715                     *output_pointer++ = '\n';
       
   716                     break;
       
   717                 case 'r':
       
   718                     *output_pointer++ = '\r';
       
   719                     break;
       
   720                 case 't':
       
   721                     *output_pointer++ = '\t';
       
   722                     break;
       
   723                 case '\"':
       
   724                 case '\\':
       
   725                 case '/':
       
   726                     *output_pointer++ = input_pointer[1];
       
   727                     break;
       
   728 
       
   729                 /* UTF-16 literal */
       
   730                 case 'u':
       
   731                     sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
       
   732                     if (sequence_length == 0)
       
   733                     {
       
   734                         /* failed to convert UTF16-literal to UTF-8 */
       
   735                         goto fail;
       
   736                     }
       
   737                     break;
       
   738 
       
   739                 default:
       
   740                     goto fail;
       
   741             }
       
   742             input_pointer += sequence_length;
       
   743         }
       
   744     }
       
   745 
       
   746     /* zero terminate the output */
       
   747     *output_pointer = '\0';
       
   748 
       
   749     item->type = cJSON_String;
       
   750     item->valuestring = (char*)output;
       
   751 
       
   752     input_buffer->offset = (size_t) (input_end - input_buffer->content);
       
   753     input_buffer->offset++;
       
   754 
       
   755     return true;
       
   756 
       
   757 fail:
       
   758     if (output != NULL)
       
   759     {
       
   760         input_buffer->hooks.deallocate(output);
       
   761     }
       
   762 
       
   763     if (input_pointer != NULL)
       
   764     {
       
   765         input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
       
   766     }
       
   767 
       
   768     return false;
       
   769 }
       
   770 
       
   771 /* Render the cstring provided to an escaped version that can be printed. */
       
   772 static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
       
   773 {
       
   774     const unsigned char *input_pointer = NULL;
       
   775     unsigned char *output = NULL;
       
   776     unsigned char *output_pointer = NULL;
       
   777     size_t output_length = 0;
       
   778     /* numbers of additional characters needed for escaping */
       
   779     size_t escape_characters = 0;
       
   780 
       
   781     if (output_buffer == NULL)
       
   782     {
       
   783         return false;
       
   784     }
       
   785 
       
   786     /* empty string */
       
   787     if (input == NULL)
       
   788     {
       
   789         output = ensure(output_buffer, sizeof("\"\""));
       
   790         if (output == NULL)
       
   791         {
       
   792             return false;
       
   793         }
       
   794         strcpy((char*)output, "\"\"");
       
   795 
       
   796         return true;
       
   797     }
       
   798 
       
   799     /* set "flag" to 1 if something needs to be escaped */
       
   800     for (input_pointer = input; *input_pointer; input_pointer++)
       
   801     {
       
   802         switch (*input_pointer)
       
   803         {
       
   804             case '\"':
       
   805             case '\\':
       
   806             case '\b':
       
   807             case '\f':
       
   808             case '\n':
       
   809             case '\r':
       
   810             case '\t':
       
   811                 /* one character escape sequence */
       
   812                 escape_characters++;
       
   813                 break;
       
   814             default:
       
   815                 if (*input_pointer < 32)
       
   816                 {
       
   817                     /* UTF-16 escape sequence uXXXX */
       
   818                     escape_characters += 5;
       
   819                 }
       
   820                 break;
       
   821         }
       
   822     }
       
   823     output_length = (size_t)(input_pointer - input) + escape_characters;
       
   824 
       
   825     output = ensure(output_buffer, output_length + sizeof("\"\""));
       
   826     if (output == NULL)
       
   827     {
       
   828         return false;
       
   829     }
       
   830 
       
   831     /* no characters have to be escaped */
       
   832     if (escape_characters == 0)
       
   833     {
       
   834         output[0] = '\"';
       
   835         memcpy(output + 1, input, output_length);
       
   836         output[output_length + 1] = '\"';
       
   837         output[output_length + 2] = '\0';
       
   838 
       
   839         return true;
       
   840     }
       
   841 
       
   842     output[0] = '\"';
       
   843     output_pointer = output + 1;
       
   844     /* copy the string */
       
   845     for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
       
   846     {
       
   847         if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
       
   848         {
       
   849             /* normal character, copy */
       
   850             *output_pointer = *input_pointer;
       
   851         }
       
   852         else
       
   853         {
       
   854             /* character needs to be escaped */
       
   855             *output_pointer++ = '\\';
       
   856             switch (*input_pointer)
       
   857             {
       
   858                 case '\\':
       
   859                     *output_pointer = '\\';
       
   860                     break;
       
   861                 case '\"':
       
   862                     *output_pointer = '\"';
       
   863                     break;
       
   864                 case '\b':
       
   865                     *output_pointer = 'b';
       
   866                     break;
       
   867                 case '\f':
       
   868                     *output_pointer = 'f';
       
   869                     break;
       
   870                 case '\n':
       
   871                     *output_pointer = 'n';
       
   872                     break;
       
   873                 case '\r':
       
   874                     *output_pointer = 'r';
       
   875                     break;
       
   876                 case '\t':
       
   877                     *output_pointer = 't';
       
   878                     break;
       
   879                 default:
       
   880                     /* escape and print as unicode codepoint */
       
   881                     sprintf((char*)output_pointer, "u%04x", *input_pointer);
       
   882                     output_pointer += 4;
       
   883                     break;
       
   884             }
       
   885         }
       
   886     }
       
   887     output[output_length + 1] = '\"';
       
   888     output[output_length + 2] = '\0';
       
   889 
       
   890     return true;
       
   891 }
       
   892 
       
   893 /* Invoke print_string_ptr (which is useful) on an item. */
       
   894 static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
       
   895 {
       
   896     return print_string_ptr((unsigned char*)item->valuestring, p);
       
   897 }
       
   898 
       
   899 /* Predeclare these prototypes. */
       
   900 static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
       
   901 static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
       
   902 static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
       
   903 static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
       
   904 static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
       
   905 static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
       
   906 
       
   907 /* Utility to jump whitespace and cr/lf */
       
   908 static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
       
   909 {
       
   910     if ((buffer == NULL) || (buffer->content == NULL))
       
   911     {
       
   912         return NULL;
       
   913     }
       
   914 
       
   915     while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
       
   916     {
       
   917        buffer->offset++;
       
   918     }
       
   919 
       
   920     if (buffer->offset == buffer->length)
       
   921     {
       
   922         buffer->offset--;
       
   923     }
       
   924 
       
   925     return buffer;
       
   926 }
       
   927 
       
   928 /* Parse an object - create a new root, and populate. */
       
   929 CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
       
   930 {
       
   931     parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
       
   932     cJSON *item = NULL;
       
   933 
       
   934     /* reset error position */
       
   935     global_error.json = NULL;
       
   936     global_error.position = 0;
       
   937 
       
   938     if (value == NULL)
       
   939     {
       
   940         goto fail;
       
   941     }
       
   942 
       
   943     buffer.content = (const unsigned char*)value;
       
   944     buffer.length = strlen((const char*)value) + sizeof("");
       
   945     buffer.offset = 0;
       
   946     buffer.hooks = global_hooks;
       
   947 
       
   948     item = cJSON_New_Item(&global_hooks);
       
   949     if (item == NULL) /* memory fail */
       
   950     {
       
   951         goto fail;
       
   952     }
       
   953 
       
   954     if (!parse_value(item, buffer_skip_whitespace(&buffer)))
       
   955     {
       
   956         /* parse failure. ep is set. */
       
   957         goto fail;
       
   958     }
       
   959 
       
   960     /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
       
   961     if (require_null_terminated)
       
   962     {
       
   963         buffer_skip_whitespace(&buffer);
       
   964         if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
       
   965         {
       
   966             goto fail;
       
   967         }
       
   968     }
       
   969     if (return_parse_end)
       
   970     {
       
   971         *return_parse_end = (const char*)buffer_at_offset(&buffer);
       
   972     }
       
   973 
       
   974     return item;
       
   975 
       
   976 fail:
       
   977     if (item != NULL)
       
   978     {
       
   979         cJSON_Delete(item);
       
   980     }
       
   981 
       
   982     if (value != NULL)
       
   983     {
       
   984         error local_error;
       
   985         local_error.json = (const unsigned char*)value;
       
   986         local_error.position = 0;
       
   987 
       
   988         if (buffer.offset < buffer.length)
       
   989         {
       
   990             local_error.position = buffer.offset;
       
   991         }
       
   992         else if (buffer.length > 0)
       
   993         {
       
   994             local_error.position = buffer.length - 1;
       
   995         }
       
   996 
       
   997         if (return_parse_end != NULL)
       
   998         {
       
   999             *return_parse_end = (const char*)local_error.json + local_error.position;
       
  1000         }
       
  1001         else
       
  1002         {
       
  1003             global_error = local_error;
       
  1004         }
       
  1005     }
       
  1006 
       
  1007     return NULL;
       
  1008 }
       
  1009 
       
  1010 /* Default options for cJSON_Parse */
       
  1011 CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
       
  1012 {
       
  1013     return cJSON_ParseWithOpts(value, 0, 0);
       
  1014 }
       
  1015 
       
  1016 #define cjson_min(a, b) ((a < b) ? a : b)
       
  1017 
       
  1018 static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
       
  1019 {
       
  1020     printbuffer buffer[1];
       
  1021     unsigned char *printed = NULL;
       
  1022 
       
  1023     memset(buffer, 0, sizeof(buffer));
       
  1024 
       
  1025     /* create buffer */
       
  1026     buffer->buffer = (unsigned char*) hooks->allocate(256);
       
  1027     buffer->format = format;
       
  1028     buffer->hooks = *hooks;
       
  1029     if (buffer->buffer == NULL)
       
  1030     {
       
  1031         goto fail;
       
  1032     }
       
  1033 
       
  1034     /* print the value */
       
  1035     if (!print_value(item, buffer))
       
  1036     {
       
  1037         goto fail;
       
  1038     }
       
  1039     update_offset(buffer);
       
  1040 
       
  1041     /* check if reallocate is available */
       
  1042     if (hooks->reallocate != NULL)
       
  1043     {
       
  1044         printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->length);
       
  1045         buffer->buffer = NULL;
       
  1046         if (printed == NULL) {
       
  1047             goto fail;
       
  1048         }
       
  1049     }
       
  1050     else /* otherwise copy the JSON over to a new buffer */
       
  1051     {
       
  1052         printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
       
  1053         if (printed == NULL)
       
  1054         {
       
  1055             goto fail;
       
  1056         }
       
  1057         memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
       
  1058         printed[buffer->offset] = '\0'; /* just to be sure */
       
  1059 
       
  1060         /* free the buffer */
       
  1061         hooks->deallocate(buffer->buffer);
       
  1062     }
       
  1063 
       
  1064     return printed;
       
  1065 
       
  1066 fail:
       
  1067     if (buffer->buffer != NULL)
       
  1068     {
       
  1069         hooks->deallocate(buffer->buffer);
       
  1070     }
       
  1071 
       
  1072     if (printed != NULL)
       
  1073     {
       
  1074         hooks->deallocate(printed);
       
  1075     }
       
  1076 
       
  1077     return NULL;
       
  1078 }
       
  1079 
       
  1080 /* Render a cJSON item/entity/structure to text. */
       
  1081 CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
       
  1082 {
       
  1083     return (char*)print(item, true, &global_hooks);
       
  1084 }
       
  1085 
       
  1086 CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
       
  1087 {
       
  1088     return (char*)print(item, false, &global_hooks);
       
  1089 }
       
  1090 
       
  1091 CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
       
  1092 {
       
  1093     printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
       
  1094 
       
  1095     if (prebuffer < 0)
       
  1096     {
       
  1097         return NULL;
       
  1098     }
       
  1099 
       
  1100     p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
       
  1101     if (!p.buffer)
       
  1102     {
       
  1103         return NULL;
       
  1104     }
       
  1105 
       
  1106     p.length = (size_t)prebuffer;
       
  1107     p.offset = 0;
       
  1108     p.noalloc = false;
       
  1109     p.format = fmt;
       
  1110     p.hooks = global_hooks;
       
  1111 
       
  1112     if (!print_value(item, &p))
       
  1113     {
       
  1114         return NULL;
       
  1115     }
       
  1116 
       
  1117     return (char*)p.buffer;
       
  1118 }
       
  1119 
       
  1120 CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
       
  1121 {
       
  1122     printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
       
  1123 
       
  1124     if (len < 0)
       
  1125     {
       
  1126         return false;
       
  1127     }
       
  1128 
       
  1129     p.buffer = (unsigned char*)buf;
       
  1130     p.length = (size_t)len;
       
  1131     p.offset = 0;
       
  1132     p.noalloc = true;
       
  1133     p.format = fmt;
       
  1134     p.hooks = global_hooks;
       
  1135 
       
  1136     return print_value(item, &p);
       
  1137 }
       
  1138 
       
  1139 /* Parser core - when encountering text, process appropriately. */
       
  1140 static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
       
  1141 {
       
  1142     if ((input_buffer == NULL) || (input_buffer->content == NULL))
       
  1143     {
       
  1144         return false; /* no input */
       
  1145     }
       
  1146 
       
  1147     /* parse the different types of values */
       
  1148     /* null */
       
  1149     if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
       
  1150     {
       
  1151         item->type = cJSON_NULL;
       
  1152         input_buffer->offset += 4;
       
  1153         return true;
       
  1154     }
       
  1155     /* false */
       
  1156     if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
       
  1157     {
       
  1158         item->type = cJSON_False;
       
  1159         input_buffer->offset += 5;
       
  1160         return true;
       
  1161     }
       
  1162     /* true */
       
  1163     if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
       
  1164     {
       
  1165         item->type = cJSON_True;
       
  1166         item->valueint = 1;
       
  1167         input_buffer->offset += 4;
       
  1168         return true;
       
  1169     }
       
  1170     /* string */
       
  1171     if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
       
  1172     {
       
  1173         return parse_string(item, input_buffer);
       
  1174     }
       
  1175     /* number */
       
  1176     if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
       
  1177     {
       
  1178         return parse_number(item, input_buffer);
       
  1179     }
       
  1180     /* array */
       
  1181     if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
       
  1182     {
       
  1183         return parse_array(item, input_buffer);
       
  1184     }
       
  1185     /* object */
       
  1186     if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
       
  1187     {
       
  1188         return parse_object(item, input_buffer);
       
  1189     }
       
  1190 
       
  1191 
       
  1192     return false;
       
  1193 }
       
  1194 
       
  1195 /* Render a value to text. */
       
  1196 static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
       
  1197 {
       
  1198     unsigned char *output = NULL;
       
  1199 
       
  1200     if ((item == NULL) || (output_buffer == NULL))
       
  1201     {
       
  1202         return false;
       
  1203     }
       
  1204 
       
  1205     switch ((item->type) & 0xFF)
       
  1206     {
       
  1207         case cJSON_NULL:
       
  1208             output = ensure(output_buffer, 5);
       
  1209             if (output == NULL)
       
  1210             {
       
  1211                 return false;
       
  1212             }
       
  1213             strcpy((char*)output, "null");
       
  1214             return true;
       
  1215 
       
  1216         case cJSON_False:
       
  1217             output = ensure(output_buffer, 6);
       
  1218             if (output == NULL)
       
  1219             {
       
  1220                 return false;
       
  1221             }
       
  1222             strcpy((char*)output, "false");
       
  1223             return true;
       
  1224 
       
  1225         case cJSON_True:
       
  1226             output = ensure(output_buffer, 5);
       
  1227             if (output == NULL)
       
  1228             {
       
  1229                 return false;
       
  1230             }
       
  1231             strcpy((char*)output, "true");
       
  1232             return true;
       
  1233 
       
  1234         case cJSON_Number:
       
  1235             return print_number(item, output_buffer);
       
  1236 
       
  1237         case cJSON_Raw:
       
  1238         {
       
  1239             size_t raw_length = 0;
       
  1240             if (item->valuestring == NULL)
       
  1241             {
       
  1242                 if (!output_buffer->noalloc)
       
  1243                 {
       
  1244                     output_buffer->hooks.deallocate(output_buffer->buffer);
       
  1245                 }
       
  1246                 return false;
       
  1247             }
       
  1248 
       
  1249             raw_length = strlen(item->valuestring) + sizeof("");
       
  1250             output = ensure(output_buffer, raw_length);
       
  1251             if (output == NULL)
       
  1252             {
       
  1253                 return false;
       
  1254             }
       
  1255             memcpy(output, item->valuestring, raw_length);
       
  1256             return true;
       
  1257         }
       
  1258 
       
  1259         case cJSON_String:
       
  1260             return print_string(item, output_buffer);
       
  1261 
       
  1262         case cJSON_Array:
       
  1263             return print_array(item, output_buffer);
       
  1264 
       
  1265         case cJSON_Object:
       
  1266             return print_object(item, output_buffer);
       
  1267 
       
  1268         default:
       
  1269             return false;
       
  1270     }
       
  1271 }
       
  1272 
       
  1273 /* Build an array from input text. */
       
  1274 static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
       
  1275 {
       
  1276     cJSON *head = NULL; /* head of the linked list */
       
  1277     cJSON *current_item = NULL;
       
  1278 
       
  1279     if (input_buffer->depth >= CJSON_NESTING_LIMIT)
       
  1280     {
       
  1281         return false; /* to deeply nested */
       
  1282     }
       
  1283     input_buffer->depth++;
       
  1284 
       
  1285     if (buffer_at_offset(input_buffer)[0] != '[')
       
  1286     {
       
  1287         /* not an array */
       
  1288         goto fail;
       
  1289     }
       
  1290 
       
  1291     input_buffer->offset++;
       
  1292     buffer_skip_whitespace(input_buffer);
       
  1293     if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
       
  1294     {
       
  1295         /* empty array */
       
  1296         goto success;
       
  1297     }
       
  1298 
       
  1299     /* check if we skipped to the end of the buffer */
       
  1300     if (cannot_access_at_index(input_buffer, 0))
       
  1301     {
       
  1302         input_buffer->offset--;
       
  1303         goto fail;
       
  1304     }
       
  1305 
       
  1306     /* step back to character in front of the first element */
       
  1307     input_buffer->offset--;
       
  1308     /* loop through the comma separated array elements */
       
  1309     do
       
  1310     {
       
  1311         /* allocate next item */
       
  1312         cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
       
  1313         if (new_item == NULL)
       
  1314         {
       
  1315             goto fail; /* allocation failure */
       
  1316         }
       
  1317 
       
  1318         /* attach next item to list */
       
  1319         if (head == NULL)
       
  1320         {
       
  1321             /* start the linked list */
       
  1322             current_item = head = new_item;
       
  1323         }
       
  1324         else
       
  1325         {
       
  1326             /* add to the end and advance */
       
  1327             current_item->next = new_item;
       
  1328             new_item->prev = current_item;
       
  1329             current_item = new_item;
       
  1330         }
       
  1331 
       
  1332         /* parse next value */
       
  1333         input_buffer->offset++;
       
  1334         buffer_skip_whitespace(input_buffer);
       
  1335         if (!parse_value(current_item, input_buffer))
       
  1336         {
       
  1337             goto fail; /* failed to parse value */
       
  1338         }
       
  1339         buffer_skip_whitespace(input_buffer);
       
  1340     }
       
  1341     while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
       
  1342 
       
  1343     if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
       
  1344     {
       
  1345         goto fail; /* expected end of array */
       
  1346     }
       
  1347 
       
  1348 success:
       
  1349     input_buffer->depth--;
       
  1350 
       
  1351     item->type = cJSON_Array;
       
  1352     item->child = head;
       
  1353 
       
  1354     input_buffer->offset++;
       
  1355 
       
  1356     return true;
       
  1357 
       
  1358 fail:
       
  1359     if (head != NULL)
       
  1360     {
       
  1361         cJSON_Delete(head);
       
  1362     }
       
  1363 
       
  1364     return false;
       
  1365 }
       
  1366 
       
  1367 /* Render an array to text */
       
  1368 static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
       
  1369 {
       
  1370     unsigned char *output_pointer = NULL;
       
  1371     size_t length = 0;
       
  1372     cJSON *current_element = item->child;
       
  1373 
       
  1374     if (output_buffer == NULL)
       
  1375     {
       
  1376         return false;
       
  1377     }
       
  1378 
       
  1379     /* Compose the output array. */
       
  1380     /* opening square bracket */
       
  1381     output_pointer = ensure(output_buffer, 1);
       
  1382     if (output_pointer == NULL)
       
  1383     {
       
  1384         return false;
       
  1385     }
       
  1386 
       
  1387     *output_pointer = '[';
       
  1388     output_buffer->offset++;
       
  1389     output_buffer->depth++;
       
  1390 
       
  1391     while (current_element != NULL)
       
  1392     {
       
  1393         if (!print_value(current_element, output_buffer))
       
  1394         {
       
  1395             return false;
       
  1396         }
       
  1397         update_offset(output_buffer);
       
  1398         if (current_element->next)
       
  1399         {
       
  1400             length = (size_t) (output_buffer->format ? 2 : 1);
       
  1401             output_pointer = ensure(output_buffer, length + 1);
       
  1402             if (output_pointer == NULL)
       
  1403             {
       
  1404                 return false;
       
  1405             }
       
  1406             *output_pointer++ = ',';
       
  1407             if(output_buffer->format)
       
  1408             {
       
  1409                 *output_pointer++ = ' ';
       
  1410             }
       
  1411             *output_pointer = '\0';
       
  1412             output_buffer->offset += length;
       
  1413         }
       
  1414         current_element = current_element->next;
       
  1415     }
       
  1416 
       
  1417     output_pointer = ensure(output_buffer, 2);
       
  1418     if (output_pointer == NULL)
       
  1419     {
       
  1420         return false;
       
  1421     }
       
  1422     *output_pointer++ = ']';
       
  1423     *output_pointer = '\0';
       
  1424     output_buffer->depth--;
       
  1425 
       
  1426     return true;
       
  1427 }
       
  1428 
       
  1429 /* Build an object from the text. */
       
  1430 static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
       
  1431 {
       
  1432     cJSON *head = NULL; /* linked list head */
       
  1433     cJSON *current_item = NULL;
       
  1434 
       
  1435     if (input_buffer->depth >= CJSON_NESTING_LIMIT)
       
  1436     {
       
  1437         return false; /* to deeply nested */
       
  1438     }
       
  1439     input_buffer->depth++;
       
  1440 
       
  1441     if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
       
  1442     {
       
  1443         goto fail; /* not an object */
       
  1444     }
       
  1445 
       
  1446     input_buffer->offset++;
       
  1447     buffer_skip_whitespace(input_buffer);
       
  1448     if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
       
  1449     {
       
  1450         goto success; /* empty object */
       
  1451     }
       
  1452 
       
  1453     /* check if we skipped to the end of the buffer */
       
  1454     if (cannot_access_at_index(input_buffer, 0))
       
  1455     {
       
  1456         input_buffer->offset--;
       
  1457         goto fail;
       
  1458     }
       
  1459 
       
  1460     /* step back to character in front of the first element */
       
  1461     input_buffer->offset--;
       
  1462     /* loop through the comma separated array elements */
       
  1463     do
       
  1464     {
       
  1465         /* allocate next item */
       
  1466         cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
       
  1467         if (new_item == NULL)
       
  1468         {
       
  1469             goto fail; /* allocation failure */
       
  1470         }
       
  1471 
       
  1472         /* attach next item to list */
       
  1473         if (head == NULL)
       
  1474         {
       
  1475             /* start the linked list */
       
  1476             current_item = head = new_item;
       
  1477         }
       
  1478         else
       
  1479         {
       
  1480             /* add to the end and advance */
       
  1481             current_item->next = new_item;
       
  1482             new_item->prev = current_item;
       
  1483             current_item = new_item;
       
  1484         }
       
  1485 
       
  1486         /* parse the name of the child */
       
  1487         input_buffer->offset++;
       
  1488         buffer_skip_whitespace(input_buffer);
       
  1489         if (!parse_string(current_item, input_buffer))
       
  1490         {
       
  1491             goto fail; /* faile to parse name */
       
  1492         }
       
  1493         buffer_skip_whitespace(input_buffer);
       
  1494 
       
  1495         /* swap valuestring and string, because we parsed the name */
       
  1496         current_item->string = current_item->valuestring;
       
  1497         current_item->valuestring = NULL;
       
  1498 
       
  1499         if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
       
  1500         {
       
  1501             goto fail; /* invalid object */
       
  1502         }
       
  1503 
       
  1504         /* parse the value */
       
  1505         input_buffer->offset++;
       
  1506         buffer_skip_whitespace(input_buffer);
       
  1507         if (!parse_value(current_item, input_buffer))
       
  1508         {
       
  1509             goto fail; /* failed to parse value */
       
  1510         }
       
  1511         buffer_skip_whitespace(input_buffer);
       
  1512     }
       
  1513     while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
       
  1514 
       
  1515     if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
       
  1516     {
       
  1517         goto fail; /* expected end of object */
       
  1518     }
       
  1519 
       
  1520 success:
       
  1521     input_buffer->depth--;
       
  1522 
       
  1523     item->type = cJSON_Object;
       
  1524     item->child = head;
       
  1525 
       
  1526     input_buffer->offset++;
       
  1527     return true;
       
  1528 
       
  1529 fail:
       
  1530     if (head != NULL)
       
  1531     {
       
  1532         cJSON_Delete(head);
       
  1533     }
       
  1534 
       
  1535     return false;
       
  1536 }
       
  1537 
       
  1538 /* Render an object to text. */
       
  1539 static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
       
  1540 {
       
  1541     unsigned char *output_pointer = NULL;
       
  1542     size_t length = 0;
       
  1543     cJSON *current_item = item->child;
       
  1544 
       
  1545     if (output_buffer == NULL)
       
  1546     {
       
  1547         return false;
       
  1548     }
       
  1549 
       
  1550     /* Compose the output: */
       
  1551     length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
       
  1552     output_pointer = ensure(output_buffer, length + 1);
       
  1553     if (output_pointer == NULL)
       
  1554     {
       
  1555         return false;
       
  1556     }
       
  1557 
       
  1558     *output_pointer++ = '{';
       
  1559     output_buffer->depth++;
       
  1560     if (output_buffer->format)
       
  1561     {
       
  1562         *output_pointer++ = '\n';
       
  1563     }
       
  1564     output_buffer->offset += length;
       
  1565 
       
  1566     while (current_item)
       
  1567     {
       
  1568         if (output_buffer->format)
       
  1569         {
       
  1570             size_t i;
       
  1571             output_pointer = ensure(output_buffer, output_buffer->depth);
       
  1572             if (output_pointer == NULL)
       
  1573             {
       
  1574                 return false;
       
  1575             }
       
  1576             for (i = 0; i < output_buffer->depth; i++)
       
  1577             {
       
  1578                 *output_pointer++ = '\t';
       
  1579             }
       
  1580             output_buffer->offset += output_buffer->depth;
       
  1581         }
       
  1582 
       
  1583         /* print key */
       
  1584         if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
       
  1585         {
       
  1586             return false;
       
  1587         }
       
  1588         update_offset(output_buffer);
       
  1589 
       
  1590         length = (size_t) (output_buffer->format ? 2 : 1);
       
  1591         output_pointer = ensure(output_buffer, length);
       
  1592         if (output_pointer == NULL)
       
  1593         {
       
  1594             return false;
       
  1595         }
       
  1596         *output_pointer++ = ':';
       
  1597         if (output_buffer->format)
       
  1598         {
       
  1599             *output_pointer++ = '\t';
       
  1600         }
       
  1601         output_buffer->offset += length;
       
  1602 
       
  1603         /* print value */
       
  1604         if (!print_value(current_item, output_buffer))
       
  1605         {
       
  1606             return false;
       
  1607         }
       
  1608         update_offset(output_buffer);
       
  1609 
       
  1610         /* print comma if not last */
       
  1611         length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0));
       
  1612         output_pointer = ensure(output_buffer, length + 1);
       
  1613         if (output_pointer == NULL)
       
  1614         {
       
  1615             return false;
       
  1616         }
       
  1617         if (current_item->next)
       
  1618         {
       
  1619             *output_pointer++ = ',';
       
  1620         }
       
  1621 
       
  1622         if (output_buffer->format)
       
  1623         {
       
  1624             *output_pointer++ = '\n';
       
  1625         }
       
  1626         *output_pointer = '\0';
       
  1627         output_buffer->offset += length;
       
  1628 
       
  1629         current_item = current_item->next;
       
  1630     }
       
  1631 
       
  1632     output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
       
  1633     if (output_pointer == NULL)
       
  1634     {
       
  1635         return false;
       
  1636     }
       
  1637     if (output_buffer->format)
       
  1638     {
       
  1639         size_t i;
       
  1640         for (i = 0; i < (output_buffer->depth - 1); i++)
       
  1641         {
       
  1642             *output_pointer++ = '\t';
       
  1643         }
       
  1644     }
       
  1645     *output_pointer++ = '}';
       
  1646     *output_pointer = '\0';
       
  1647     output_buffer->depth--;
       
  1648 
       
  1649     return true;
       
  1650 }
       
  1651 
       
  1652 /* Get Array size/item / object item. */
       
  1653 CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
       
  1654 {
       
  1655     cJSON *c = array->child;
       
  1656     size_t i = 0;
       
  1657     while(c)
       
  1658     {
       
  1659         i++;
       
  1660         c = c->next;
       
  1661     }
       
  1662 
       
  1663     /* FIXME: Can overflow here. Cannot be fixed without breaking the API */
       
  1664 
       
  1665     return (int)i;
       
  1666 }
       
  1667 
       
  1668 static cJSON* get_array_item(const cJSON *array, size_t index)
       
  1669 {
       
  1670     cJSON *current_child = NULL;
       
  1671 
       
  1672     if (array == NULL)
       
  1673     {
       
  1674         return NULL;
       
  1675     }
       
  1676 
       
  1677     current_child = array->child;
       
  1678     while ((current_child != NULL) && (index > 0))
       
  1679     {
       
  1680         index--;
       
  1681         current_child = current_child->next;
       
  1682     }
       
  1683 
       
  1684     return current_child;
       
  1685 }
       
  1686 
       
  1687 CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
       
  1688 {
       
  1689     if (index < 0)
       
  1690     {
       
  1691         return NULL;
       
  1692     }
       
  1693 
       
  1694     return get_array_item(array, (size_t)index);
       
  1695 }
       
  1696 
       
  1697 static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
       
  1698 {
       
  1699     cJSON *current_element = NULL;
       
  1700 
       
  1701     if ((object == NULL) || (name == NULL))
       
  1702     {
       
  1703         return NULL;
       
  1704     }
       
  1705 
       
  1706     current_element = object->child;
       
  1707     if (case_sensitive)
       
  1708     {
       
  1709         while ((current_element != NULL) && (strcmp(name, current_element->string) != 0))
       
  1710         {
       
  1711             current_element = current_element->next;
       
  1712         }
       
  1713     }
       
  1714     else
       
  1715     {
       
  1716         while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
       
  1717         {
       
  1718             current_element = current_element->next;
       
  1719         }
       
  1720     }
       
  1721 
       
  1722     return current_element;
       
  1723 }
       
  1724 
       
  1725 CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
       
  1726 {
       
  1727     return get_object_item(object, string, false);
       
  1728 }
       
  1729 
       
  1730 CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
       
  1731 {
       
  1732     return get_object_item(object, string, true);
       
  1733 }
       
  1734 
       
  1735 CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
       
  1736 {
       
  1737     return cJSON_GetObjectItem(object, string) ? 1 : 0;
       
  1738 }
       
  1739 
       
  1740 /* Utility for array list handling. */
       
  1741 static void suffix_object(cJSON *prev, cJSON *item)
       
  1742 {
       
  1743     prev->next = item;
       
  1744     item->prev = prev;
       
  1745 }
       
  1746 
       
  1747 /* Utility for handling references. */
       
  1748 static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
       
  1749 {
       
  1750     cJSON *ref = cJSON_New_Item(hooks);
       
  1751     if (!ref)
       
  1752     {
       
  1753         return NULL;
       
  1754     }
       
  1755     memcpy(ref, item, sizeof(cJSON));
       
  1756     ref->string = NULL;
       
  1757     ref->type |= cJSON_IsReference;
       
  1758     ref->next = ref->prev = NULL;
       
  1759     return ref;
       
  1760 }
       
  1761 
       
  1762 /* Add item to array/object. */
       
  1763 CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
       
  1764 {
       
  1765     cJSON *child = NULL;
       
  1766 
       
  1767     if ((item == NULL) || (array == NULL))
       
  1768     {
       
  1769         return;
       
  1770     }
       
  1771 
       
  1772     child = array->child;
       
  1773 
       
  1774     if (child == NULL)
       
  1775     {
       
  1776         /* list is empty, start new one */
       
  1777         array->child = item;
       
  1778     }
       
  1779     else
       
  1780     {
       
  1781         /* append to the end */
       
  1782         while (child->next)
       
  1783         {
       
  1784             child = child->next;
       
  1785         }
       
  1786         suffix_object(child, item);
       
  1787     }
       
  1788 }
       
  1789 
       
  1790 CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
       
  1791 {
       
  1792     /* call cJSON_AddItemToObjectCS for code reuse */
       
  1793     cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
       
  1794     /* remove cJSON_StringIsConst flag */
       
  1795     item->type &= ~cJSON_StringIsConst;
       
  1796 }
       
  1797 
       
  1798 #if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
       
  1799     #pragma GCC diagnostic push
       
  1800 #endif
       
  1801 #ifdef __GNUC__
       
  1802 #pragma GCC diagnostic ignored "-Wcast-qual"
       
  1803 #endif
       
  1804 
       
  1805 /* Add an item to an object with constant string as key */
       
  1806 CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
       
  1807 {
       
  1808     if (!item)
       
  1809     {
       
  1810         return;
       
  1811     }
       
  1812     if (!(item->type & cJSON_StringIsConst) && item->string)
       
  1813     {
       
  1814         global_hooks.deallocate(item->string);
       
  1815     }
       
  1816     item->string = (char*)string;
       
  1817     item->type |= cJSON_StringIsConst;
       
  1818     cJSON_AddItemToArray(object, item);
       
  1819 }
       
  1820 #if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
       
  1821     #pragma GCC diagnostic pop
       
  1822 #endif
       
  1823 
       
  1824 CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
       
  1825 {
       
  1826     cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
       
  1827 }
       
  1828 
       
  1829 CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
       
  1830 {
       
  1831     cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
       
  1832 }
       
  1833 
       
  1834 CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
       
  1835 {
       
  1836     if ((parent == NULL) || (item == NULL))
       
  1837     {
       
  1838         return NULL;
       
  1839     }
       
  1840 
       
  1841     if (item->prev != NULL)
       
  1842     {
       
  1843         /* not the first element */
       
  1844         item->prev->next = item->next;
       
  1845     }
       
  1846     if (item->next != NULL)
       
  1847     {
       
  1848         /* not the last element */
       
  1849         item->next->prev = item->prev;
       
  1850     }
       
  1851 
       
  1852     if (item == parent->child)
       
  1853     {
       
  1854         /* first element */
       
  1855         parent->child = item->next;
       
  1856     }
       
  1857     /* make sure the detached item doesn't point anywhere anymore */
       
  1858     item->prev = NULL;
       
  1859     item->next = NULL;
       
  1860 
       
  1861     return item;
       
  1862 }
       
  1863 
       
  1864 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
       
  1865 {
       
  1866     if (which < 0)
       
  1867     {
       
  1868         return NULL;
       
  1869     }
       
  1870 
       
  1871     return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
       
  1872 }
       
  1873 
       
  1874 CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
       
  1875 {
       
  1876     cJSON_Delete(cJSON_DetachItemFromArray(array, which));
       
  1877 }
       
  1878 
       
  1879 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
       
  1880 {
       
  1881     cJSON *to_detach = cJSON_GetObjectItem(object, string);
       
  1882 
       
  1883     return cJSON_DetachItemViaPointer(object, to_detach);
       
  1884 }
       
  1885 
       
  1886 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string)
       
  1887 {
       
  1888     cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
       
  1889 
       
  1890     return cJSON_DetachItemViaPointer(object, to_detach);
       
  1891 }
       
  1892 
       
  1893 CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
       
  1894 {
       
  1895     cJSON_Delete(cJSON_DetachItemFromObject(object, string));
       
  1896 }
       
  1897 
       
  1898 CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
       
  1899 {
       
  1900     cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
       
  1901 }
       
  1902 
       
  1903 /* Replace array/object items with new ones. */
       
  1904 CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
       
  1905 {
       
  1906     cJSON *after_inserted = NULL;
       
  1907 
       
  1908     if (which < 0)
       
  1909     {
       
  1910         return;
       
  1911     }
       
  1912 
       
  1913     after_inserted = get_array_item(array, (size_t)which);
       
  1914     if (after_inserted == NULL)
       
  1915     {
       
  1916         cJSON_AddItemToArray(array, newitem);
       
  1917         return;
       
  1918     }
       
  1919 
       
  1920     newitem->next = after_inserted;
       
  1921     newitem->prev = after_inserted->prev;
       
  1922     after_inserted->prev = newitem;
       
  1923     if (after_inserted == array->child)
       
  1924     {
       
  1925         array->child = newitem;
       
  1926     }
       
  1927     else
       
  1928     {
       
  1929         newitem->prev->next = newitem;
       
  1930     }
       
  1931 }
       
  1932 
       
  1933 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
       
  1934 {
       
  1935     if ((parent == NULL) || (replacement == NULL))
       
  1936     {
       
  1937         return false;
       
  1938     }
       
  1939 
       
  1940     if (replacement == item)
       
  1941     {
       
  1942         return true;
       
  1943     }
       
  1944 
       
  1945     replacement->next = item->next;
       
  1946     replacement->prev = item->prev;
       
  1947 
       
  1948     if (replacement->next != NULL)
       
  1949     {
       
  1950         replacement->next->prev = replacement;
       
  1951     }
       
  1952     if (replacement->prev != NULL)
       
  1953     {
       
  1954         replacement->prev->next = replacement;
       
  1955     }
       
  1956     if (parent->child == item)
       
  1957     {
       
  1958         parent->child = replacement;
       
  1959     }
       
  1960 
       
  1961     item->next = NULL;
       
  1962     item->prev = NULL;
       
  1963     cJSON_Delete(item);
       
  1964 
       
  1965     return true;
       
  1966 }
       
  1967 
       
  1968 CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
       
  1969 {
       
  1970     if (which < 0)
       
  1971     {
       
  1972         return;
       
  1973     }
       
  1974 
       
  1975     cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
       
  1976 }
       
  1977 
       
  1978 static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
       
  1979 {
       
  1980     if (replacement == NULL)
       
  1981     {
       
  1982         return false;
       
  1983     }
       
  1984 
       
  1985     /* replace the name in the replacement */
       
  1986     if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
       
  1987     {
       
  1988         cJSON_free(replacement->string);
       
  1989     }
       
  1990     replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
       
  1991     replacement->type &= ~cJSON_StringIsConst;
       
  1992 
       
  1993     cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
       
  1994 
       
  1995     return true;
       
  1996 }
       
  1997 
       
  1998 CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
       
  1999 {
       
  2000     replace_item_in_object(object, string, newitem, false);
       
  2001 }
       
  2002 
       
  2003 CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
       
  2004 {
       
  2005     replace_item_in_object(object, string, newitem, true);
       
  2006 }
       
  2007 
       
  2008 /* Create basic types: */
       
  2009 CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
       
  2010 {
       
  2011     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2012     if(item)
       
  2013     {
       
  2014         item->type = cJSON_NULL;
       
  2015     }
       
  2016 
       
  2017     return item;
       
  2018 }
       
  2019 
       
  2020 CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
       
  2021 {
       
  2022     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2023     if(item)
       
  2024     {
       
  2025         item->type = cJSON_True;
       
  2026     }
       
  2027 
       
  2028     return item;
       
  2029 }
       
  2030 
       
  2031 CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
       
  2032 {
       
  2033     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2034     if(item)
       
  2035     {
       
  2036         item->type = cJSON_False;
       
  2037     }
       
  2038 
       
  2039     return item;
       
  2040 }
       
  2041 
       
  2042 CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b)
       
  2043 {
       
  2044     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2045     if(item)
       
  2046     {
       
  2047         item->type = b ? cJSON_True : cJSON_False;
       
  2048     }
       
  2049 
       
  2050     return item;
       
  2051 }
       
  2052 
       
  2053 CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
       
  2054 {
       
  2055     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2056     if(item)
       
  2057     {
       
  2058         item->type = cJSON_Number;
       
  2059         item->valuedouble = num;
       
  2060 
       
  2061         /* use saturation in case of overflow */
       
  2062         if (num >= INT_MAX)
       
  2063         {
       
  2064             item->valueint = INT_MAX;
       
  2065         }
       
  2066         else if (num <= INT_MIN)
       
  2067         {
       
  2068             item->valueint = INT_MIN;
       
  2069         }
       
  2070         else
       
  2071         {
       
  2072             item->valueint = (int)num;
       
  2073         }
       
  2074     }
       
  2075 
       
  2076     return item;
       
  2077 }
       
  2078 
       
  2079 CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
       
  2080 {
       
  2081     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2082     if(item)
       
  2083     {
       
  2084         item->type = cJSON_String;
       
  2085         item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
       
  2086         if(!item->valuestring)
       
  2087         {
       
  2088             cJSON_Delete(item);
       
  2089             return NULL;
       
  2090         }
       
  2091     }
       
  2092 
       
  2093     return item;
       
  2094 }
       
  2095 
       
  2096 CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
       
  2097 {
       
  2098     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2099     if(item)
       
  2100     {
       
  2101         item->type = cJSON_Raw;
       
  2102         item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
       
  2103         if(!item->valuestring)
       
  2104         {
       
  2105             cJSON_Delete(item);
       
  2106             return NULL;
       
  2107         }
       
  2108     }
       
  2109 
       
  2110     return item;
       
  2111 }
       
  2112 
       
  2113 CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
       
  2114 {
       
  2115     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2116     if(item)
       
  2117     {
       
  2118         item->type=cJSON_Array;
       
  2119     }
       
  2120 
       
  2121     return item;
       
  2122 }
       
  2123 
       
  2124 CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
       
  2125 {
       
  2126     cJSON *item = cJSON_New_Item(&global_hooks);
       
  2127     if (item)
       
  2128     {
       
  2129         item->type = cJSON_Object;
       
  2130     }
       
  2131 
       
  2132     return item;
       
  2133 }
       
  2134 
       
  2135 /* Create Arrays: */
       
  2136 CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
       
  2137 {
       
  2138     size_t i = 0;
       
  2139     cJSON *n = NULL;
       
  2140     cJSON *p = NULL;
       
  2141     cJSON *a = NULL;
       
  2142 
       
  2143     if (count < 0)
       
  2144     {
       
  2145         return NULL;
       
  2146     }
       
  2147 
       
  2148     a = cJSON_CreateArray();
       
  2149     for(i = 0; a && (i < (size_t)count); i++)
       
  2150     {
       
  2151         n = cJSON_CreateNumber(numbers[i]);
       
  2152         if (!n)
       
  2153         {
       
  2154             cJSON_Delete(a);
       
  2155             return NULL;
       
  2156         }
       
  2157         if(!i)
       
  2158         {
       
  2159             a->child = n;
       
  2160         }
       
  2161         else
       
  2162         {
       
  2163             suffix_object(p, n);
       
  2164         }
       
  2165         p = n;
       
  2166     }
       
  2167 
       
  2168     return a;
       
  2169 }
       
  2170 
       
  2171 CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
       
  2172 {
       
  2173     size_t i = 0;
       
  2174     cJSON *n = NULL;
       
  2175     cJSON *p = NULL;
       
  2176     cJSON *a = NULL;
       
  2177 
       
  2178     if (count < 0)
       
  2179     {
       
  2180         return NULL;
       
  2181     }
       
  2182 
       
  2183     a = cJSON_CreateArray();
       
  2184 
       
  2185     for(i = 0; a && (i < (size_t)count); i++)
       
  2186     {
       
  2187         n = cJSON_CreateNumber((double)numbers[i]);
       
  2188         if(!n)
       
  2189         {
       
  2190             cJSON_Delete(a);
       
  2191             return NULL;
       
  2192         }
       
  2193         if(!i)
       
  2194         {
       
  2195             a->child = n;
       
  2196         }
       
  2197         else
       
  2198         {
       
  2199             suffix_object(p, n);
       
  2200         }
       
  2201         p = n;
       
  2202     }
       
  2203 
       
  2204     return a;
       
  2205 }
       
  2206 
       
  2207 CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
       
  2208 {
       
  2209     size_t i = 0;
       
  2210     cJSON *n = NULL;
       
  2211     cJSON *p = NULL;
       
  2212     cJSON *a = NULL;
       
  2213 
       
  2214     if (count < 0)
       
  2215     {
       
  2216         return NULL;
       
  2217     }
       
  2218 
       
  2219     a = cJSON_CreateArray();
       
  2220 
       
  2221     for(i = 0;a && (i < (size_t)count); i++)
       
  2222     {
       
  2223         n = cJSON_CreateNumber(numbers[i]);
       
  2224         if(!n)
       
  2225         {
       
  2226             cJSON_Delete(a);
       
  2227             return NULL;
       
  2228         }
       
  2229         if(!i)
       
  2230         {
       
  2231             a->child = n;
       
  2232         }
       
  2233         else
       
  2234         {
       
  2235             suffix_object(p, n);
       
  2236         }
       
  2237         p = n;
       
  2238     }
       
  2239 
       
  2240     return a;
       
  2241 }
       
  2242 
       
  2243 CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
       
  2244 {
       
  2245     size_t i = 0;
       
  2246     cJSON *n = NULL;
       
  2247     cJSON *p = NULL;
       
  2248     cJSON *a = NULL;
       
  2249 
       
  2250     if (count < 0)
       
  2251     {
       
  2252         return NULL;
       
  2253     }
       
  2254 
       
  2255     a = cJSON_CreateArray();
       
  2256 
       
  2257     for (i = 0; a && (i < (size_t)count); i++)
       
  2258     {
       
  2259         n = cJSON_CreateString(strings[i]);
       
  2260         if(!n)
       
  2261         {
       
  2262             cJSON_Delete(a);
       
  2263             return NULL;
       
  2264         }
       
  2265         if(!i)
       
  2266         {
       
  2267             a->child = n;
       
  2268         }
       
  2269         else
       
  2270         {
       
  2271             suffix_object(p,n);
       
  2272         }
       
  2273         p = n;
       
  2274     }
       
  2275 
       
  2276     return a;
       
  2277 }
       
  2278 
       
  2279 /* Duplication */
       
  2280 CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
       
  2281 {
       
  2282     cJSON *newitem = NULL;
       
  2283     cJSON *child = NULL;
       
  2284     cJSON *next = NULL;
       
  2285     cJSON *newchild = NULL;
       
  2286 
       
  2287     /* Bail on bad ptr */
       
  2288     if (!item)
       
  2289     {
       
  2290         goto fail;
       
  2291     }
       
  2292     /* Create new item */
       
  2293     newitem = cJSON_New_Item(&global_hooks);
       
  2294     if (!newitem)
       
  2295     {
       
  2296         goto fail;
       
  2297     }
       
  2298     /* Copy over all vars */
       
  2299     newitem->type = item->type & (~cJSON_IsReference);
       
  2300     newitem->valueint = item->valueint;
       
  2301     newitem->valuedouble = item->valuedouble;
       
  2302     if (item->valuestring)
       
  2303     {
       
  2304         newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
       
  2305         if (!newitem->valuestring)
       
  2306         {
       
  2307             goto fail;
       
  2308         }
       
  2309     }
       
  2310     if (item->string)
       
  2311     {
       
  2312         newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
       
  2313         if (!newitem->string)
       
  2314         {
       
  2315             goto fail;
       
  2316         }
       
  2317     }
       
  2318     /* If non-recursive, then we're done! */
       
  2319     if (!recurse)
       
  2320     {
       
  2321         return newitem;
       
  2322     }
       
  2323     /* Walk the ->next chain for the child. */
       
  2324     child = item->child;
       
  2325     while (child != NULL)
       
  2326     {
       
  2327         newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
       
  2328         if (!newchild)
       
  2329         {
       
  2330             goto fail;
       
  2331         }
       
  2332         if (next != NULL)
       
  2333         {
       
  2334             /* If newitem->child already set, then crosswire ->prev and ->next and move on */
       
  2335             next->next = newchild;
       
  2336             newchild->prev = next;
       
  2337             next = newchild;
       
  2338         }
       
  2339         else
       
  2340         {
       
  2341             /* Set newitem->child and move to it */
       
  2342             newitem->child = newchild;
       
  2343             next = newchild;
       
  2344         }
       
  2345         child = child->next;
       
  2346     }
       
  2347 
       
  2348     return newitem;
       
  2349 
       
  2350 fail:
       
  2351     if (newitem != NULL)
       
  2352     {
       
  2353         cJSON_Delete(newitem);
       
  2354     }
       
  2355 
       
  2356     return NULL;
       
  2357 }
       
  2358 
       
  2359 CJSON_PUBLIC(void) cJSON_Minify(char *json)
       
  2360 {
       
  2361     unsigned char *into = (unsigned char*)json;
       
  2362     while (*json)
       
  2363     {
       
  2364         if (*json == ' ')
       
  2365         {
       
  2366             json++;
       
  2367         }
       
  2368         else if (*json == '\t')
       
  2369         {
       
  2370             /* Whitespace characters. */
       
  2371             json++;
       
  2372         }
       
  2373         else if (*json == '\r')
       
  2374         {
       
  2375             json++;
       
  2376         }
       
  2377         else if (*json=='\n')
       
  2378         {
       
  2379             json++;
       
  2380         }
       
  2381         else if ((*json == '/') && (json[1] == '/'))
       
  2382         {
       
  2383             /* double-slash comments, to end of line. */
       
  2384             while (*json && (*json != '\n'))
       
  2385             {
       
  2386                 json++;
       
  2387             }
       
  2388         }
       
  2389         else if ((*json == '/') && (json[1] == '*'))
       
  2390         {
       
  2391             /* multiline comments. */
       
  2392             while (*json && !((*json == '*') && (json[1] == '/')))
       
  2393             {
       
  2394                 json++;
       
  2395             }
       
  2396             json += 2;
       
  2397         }
       
  2398         else if (*json == '\"')
       
  2399         {
       
  2400             /* string literals, which are \" sensitive. */
       
  2401             *into++ = (unsigned char)*json++;
       
  2402             while (*json && (*json != '\"'))
       
  2403             {
       
  2404                 if (*json == '\\')
       
  2405                 {
       
  2406                     *into++ = (unsigned char)*json++;
       
  2407                 }
       
  2408                 *into++ = (unsigned char)*json++;
       
  2409             }
       
  2410             *into++ = (unsigned char)*json++;
       
  2411         }
       
  2412         else
       
  2413         {
       
  2414             /* All other characters. */
       
  2415             *into++ = (unsigned char)*json++;
       
  2416         }
       
  2417     }
       
  2418 
       
  2419     /* and null-terminate. */
       
  2420     *into = '\0';
       
  2421 }
       
  2422 
       
  2423 CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
       
  2424 {
       
  2425     if (item == NULL)
       
  2426     {
       
  2427         return false;
       
  2428     }
       
  2429 
       
  2430     return (item->type & 0xFF) == cJSON_Invalid;
       
  2431 }
       
  2432 
       
  2433 CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
       
  2434 {
       
  2435     if (item == NULL)
       
  2436     {
       
  2437         return false;
       
  2438     }
       
  2439 
       
  2440     return (item->type & 0xFF) == cJSON_False;
       
  2441 }
       
  2442 
       
  2443 CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
       
  2444 {
       
  2445     if (item == NULL)
       
  2446     {
       
  2447         return false;
       
  2448     }
       
  2449 
       
  2450     return (item->type & 0xff) == cJSON_True;
       
  2451 }
       
  2452 
       
  2453 
       
  2454 CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
       
  2455 {
       
  2456     if (item == NULL)
       
  2457     {
       
  2458         return false;
       
  2459     }
       
  2460 
       
  2461     return (item->type & (cJSON_True | cJSON_False)) != 0;
       
  2462 }
       
  2463 CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
       
  2464 {
       
  2465     if (item == NULL)
       
  2466     {
       
  2467         return false;
       
  2468     }
       
  2469 
       
  2470     return (item->type & 0xFF) == cJSON_NULL;
       
  2471 }
       
  2472 
       
  2473 CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
       
  2474 {
       
  2475     if (item == NULL)
       
  2476     {
       
  2477         return false;
       
  2478     }
       
  2479 
       
  2480     return (item->type & 0xFF) == cJSON_Number;
       
  2481 }
       
  2482 
       
  2483 CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
       
  2484 {
       
  2485     if (item == NULL)
       
  2486     {
       
  2487         return false;
       
  2488     }
       
  2489 
       
  2490     return (item->type & 0xFF) == cJSON_String;
       
  2491 }
       
  2492 
       
  2493 CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
       
  2494 {
       
  2495     if (item == NULL)
       
  2496     {
       
  2497         return false;
       
  2498     }
       
  2499 
       
  2500     return (item->type & 0xFF) == cJSON_Array;
       
  2501 }
       
  2502 
       
  2503 CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
       
  2504 {
       
  2505     if (item == NULL)
       
  2506     {
       
  2507         return false;
       
  2508     }
       
  2509 
       
  2510     return (item->type & 0xFF) == cJSON_Object;
       
  2511 }
       
  2512 
       
  2513 CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
       
  2514 {
       
  2515     if (item == NULL)
       
  2516     {
       
  2517         return false;
       
  2518     }
       
  2519 
       
  2520     return (item->type & 0xFF) == cJSON_Raw;
       
  2521 }
       
  2522 
       
  2523 CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
       
  2524 {
       
  2525     if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a))
       
  2526     {
       
  2527         return false;
       
  2528     }
       
  2529 
       
  2530     /* check if type is valid */
       
  2531     switch (a->type & 0xFF)
       
  2532     {
       
  2533         case cJSON_False:
       
  2534         case cJSON_True:
       
  2535         case cJSON_NULL:
       
  2536         case cJSON_Number:
       
  2537         case cJSON_String:
       
  2538         case cJSON_Raw:
       
  2539         case cJSON_Array:
       
  2540         case cJSON_Object:
       
  2541             break;
       
  2542 
       
  2543         default:
       
  2544             return false;
       
  2545     }
       
  2546 
       
  2547     /* identical objects are equal */
       
  2548     if (a == b)
       
  2549     {
       
  2550         return true;
       
  2551     }
       
  2552 
       
  2553     switch (a->type & 0xFF)
       
  2554     {
       
  2555         /* in these cases and equal type is enough */
       
  2556         case cJSON_False:
       
  2557         case cJSON_True:
       
  2558         case cJSON_NULL:
       
  2559             return true;
       
  2560 
       
  2561         case cJSON_Number:
       
  2562             if (a->valuedouble == b->valuedouble)
       
  2563             {
       
  2564                 return true;
       
  2565             }
       
  2566             return false;
       
  2567 
       
  2568         case cJSON_String:
       
  2569         case cJSON_Raw:
       
  2570             if ((a->valuestring == NULL) || (b->valuestring == NULL))
       
  2571             {
       
  2572                 return false;
       
  2573             }
       
  2574             if (strcmp(a->valuestring, b->valuestring) == 0)
       
  2575             {
       
  2576                 return true;
       
  2577             }
       
  2578 
       
  2579             return false;
       
  2580 
       
  2581         case cJSON_Array:
       
  2582         {
       
  2583             cJSON *a_element = a->child;
       
  2584             cJSON *b_element = b->child;
       
  2585 
       
  2586             for (; (a_element != NULL) && (b_element != NULL);)
       
  2587             {
       
  2588                 if (!cJSON_Compare(a_element, b_element, case_sensitive))
       
  2589                 {
       
  2590                     return false;
       
  2591                 }
       
  2592 
       
  2593                 a_element = a_element->next;
       
  2594                 b_element = b_element->next;
       
  2595             }
       
  2596 
       
  2597             /* one of the arrays is longer than the other */
       
  2598             if (a_element != b_element) {
       
  2599                 return false;
       
  2600             }
       
  2601 
       
  2602             return true;
       
  2603         }
       
  2604 
       
  2605         case cJSON_Object:
       
  2606         {
       
  2607             cJSON *a_element = NULL;
       
  2608             cJSON *b_element = NULL;
       
  2609             cJSON_ArrayForEach(a_element, a)
       
  2610             {
       
  2611                 /* TODO This has O(n^2) runtime, which is horrible! */
       
  2612                 b_element = get_object_item(b, a_element->string, case_sensitive);
       
  2613                 if (b_element == NULL)
       
  2614                 {
       
  2615                     return false;
       
  2616                 }
       
  2617 
       
  2618                 if (!cJSON_Compare(a_element, b_element, case_sensitive))
       
  2619                 {
       
  2620                     return false;
       
  2621                 }
       
  2622             }
       
  2623 
       
  2624             /* doing this twice, once on a and b to prevent true comparison if a subset of b
       
  2625              * TODO: Do this the proper way, this is just a fix for now */
       
  2626             cJSON_ArrayForEach(b_element, b)
       
  2627             {
       
  2628                 a_element = get_object_item(a, b_element->string, case_sensitive);
       
  2629                 if (a_element == NULL)
       
  2630                 {
       
  2631                     return false;
       
  2632                 }
       
  2633 
       
  2634                 if (!cJSON_Compare(b_element, a_element, case_sensitive))
       
  2635                 {
       
  2636                     return false;
       
  2637                 }
       
  2638             }
       
  2639 
       
  2640             return true;
       
  2641         }
       
  2642 
       
  2643         default:
       
  2644             return false;
       
  2645     }
       
  2646 }
       
  2647 
       
  2648 CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
       
  2649 {
       
  2650     return global_hooks.allocate(size);
       
  2651 }
       
  2652 
       
  2653 CJSON_PUBLIC(void) cJSON_free(void *object)
       
  2654 {
       
  2655     global_hooks.deallocate(object);
       
  2656 }