"Fossies" - the Fresh Open Source Software Archive

Member "opensips-3.0.1/lib/cJSON.c" (16 Apr 2019, 57708 Bytes) of package /linux/misc/opensips-3.0.1.tar.gz:


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

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