cJSON.c 62.9 KB
Newer Older
K
Kevin Branigan 已提交
1
/*
M
Max Bruckner 已提交
2
  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
K
Kevin Branigan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

/* cJSON */
/* JSON parser in C. */

26
#ifdef __GNUC__
27
#pragma GCC visibility push(default)
28 29
#endif

K
Kevin Branigan 已提交
30 31 32 33 34 35 36
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
37
#include <locale.h>
38 39

#ifdef __GNUC__
40
#pragma GCC visibility pop
41
#endif
42

K
Kevin Branigan 已提交
43 44
#include "cJSON.h"

M
Max Bruckner 已提交
45
/* define our own boolean type */
46 47
#define true ((cJSON_bool)1)
#define false ((cJSON_bool)0)
M
Max Bruckner 已提交
48

49 50 51 52 53
typedef struct {
    const unsigned char *json;
    size_t position;
} error;
static error global_error = { NULL, 0 };
K
Kevin Branigan 已提交
54

55
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
M
Max Bruckner 已提交
56
{
57
    return (const char*) (global_error.json + global_error.position);
M
Max Bruckner 已提交
58
}
K
Kevin Branigan 已提交
59

60
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
M
Max Bruckner 已提交
61
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 4) || (CJSON_VERSION_PATCH != 6)
62 63 64
    #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif

65
CJSON_PUBLIC(const char*) cJSON_Version(void)
66 67 68 69 70 71 72
{
    static char version[15];
    sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);

    return version;
}

73 74
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
K
Kevin Branigan 已提交
75
{
76
    if ((string1 == NULL) || (string2 == NULL))
M
Max Bruckner 已提交
77
    {
78
        return 1;
M
Max Bruckner 已提交
79
    }
80

81
    if (string1 == string2)
M
Max Bruckner 已提交
82
    {
83
        return 0;
M
Max Bruckner 已提交
84
    }
85 86

    for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
M
Max Bruckner 已提交
87
    {
88
        if (*string1 == '\0')
M
Max Bruckner 已提交
89 90 91 92 93
        {
            return 0;
        }
    }

94
    return tolower(*string1) - tolower(*string2);
K
Kevin Branigan 已提交
95 96
}

97 98 99 100 101 102 103 104
typedef struct internal_hooks
{
    void *(*allocate)(size_t size);
    void (*deallocate)(void *pointer);
    void *(*reallocate)(void *pointer, size_t size);
} internal_hooks;

static internal_hooks global_hooks = { malloc, free, realloc };
K
Kevin Branigan 已提交
105

M
Max Bruckner 已提交
106
static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
107
{
M
Max Bruckner 已提交
108
    size_t length = 0;
109
    unsigned char *copy = NULL;
K
Kevin Branigan 已提交
110

M
Max Bruckner 已提交
111
    if (string == NULL)
112 113 114 115
    {
        return NULL;
    }

M
Max Bruckner 已提交
116 117
    length = strlen((const char*)string) + sizeof("");
    if (!(copy = (unsigned char*)hooks->allocate(length)))
M
Max Bruckner 已提交
118
    {
119
        return NULL;
M
Max Bruckner 已提交
120
    }
M
Max Bruckner 已提交
121
    memcpy(copy, string, length);
M
Max Bruckner 已提交
122 123

    return copy;
K
Kevin Branigan 已提交
124 125
}

126
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
K
Kevin Branigan 已提交
127
{
M
Max Bruckner 已提交
128
    if (hooks == NULL)
M
Max Bruckner 已提交
129 130
    {
        /* Reset hooks */
131 132 133
        global_hooks.allocate = malloc;
        global_hooks.deallocate = free;
        global_hooks.reallocate = realloc;
K
Kevin Branigan 已提交
134 135 136
        return;
    }

137
    global_hooks.allocate = malloc;
M
Max Bruckner 已提交
138 139
    if (hooks->malloc_fn != NULL)
    {
140
        global_hooks.allocate = hooks->malloc_fn;
M
Max Bruckner 已提交
141 142
    }

143
    global_hooks.deallocate = free;
M
Max Bruckner 已提交
144 145
    if (hooks->free_fn != NULL)
    {
146
        global_hooks.deallocate = hooks->free_fn;
M
Max Bruckner 已提交
147 148 149
    }

    /* use realloc only if both free and malloc are used */
150 151
    global_hooks.reallocate = NULL;
    if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
M
Max Bruckner 已提交
152
    {
153
        global_hooks.reallocate = realloc;
M
Max Bruckner 已提交
154
    }
K
Kevin Branigan 已提交
155 156 157
}

/* Internal constructor. */
158
static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
K
Kevin Branigan 已提交
159
{
160
    cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
M
Max Bruckner 已提交
161 162
    if (node)
    {
163
        memset(node, '\0', sizeof(cJSON));
M
Max Bruckner 已提交
164 165 166
    }

    return node;
K
Kevin Branigan 已提交
167 168 169
}

/* Delete a cJSON structure. */
M
Max Bruckner 已提交
170
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
K
Kevin Branigan 已提交
171
{
M
Max Bruckner 已提交
172
    cJSON *next = NULL;
M
Max Bruckner 已提交
173
    while (item != NULL)
M
Max Bruckner 已提交
174
    {
M
Max Bruckner 已提交
175 176
        next = item->next;
        if (!(item->type & cJSON_IsReference) && (item->child != NULL))
M
Max Bruckner 已提交
177
        {
M
Max Bruckner 已提交
178
            cJSON_Delete(item->child);
M
Max Bruckner 已提交
179
        }
M
Max Bruckner 已提交
180
        if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
M
Max Bruckner 已提交
181
        {
M
Max Bruckner 已提交
182
            global_hooks.deallocate(item->valuestring);
M
Max Bruckner 已提交
183
        }
M
Max Bruckner 已提交
184
        if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
M
Max Bruckner 已提交
185
        {
M
Max Bruckner 已提交
186
            global_hooks.deallocate(item->string);
M
Max Bruckner 已提交
187
        }
M
Max Bruckner 已提交
188 189
        global_hooks.deallocate(item);
        item = next;
M
Max Bruckner 已提交
190
    }
K
Kevin Branigan 已提交
191 192
}

193 194 195 196 197 198 199
/* get the decimal point character of the current locale */
static unsigned char get_decimal_point(void)
{
    struct lconv *lconv = localeconv();
    return (unsigned char) lconv->decimal_point[0];
}

M
Max Bruckner 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
typedef struct
{
    const unsigned char *content;
    size_t length;
    size_t offset;
} parse_buffer;

/* check if the given size is left to read in a given parse buffer (starting with 1) */
#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
#define cannot_read(buffer, size) (!can_read(buffer, size))
/* check if the buffer can be accessed at the given index (starting with 0) */
#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
/* get a pointer to the buffer at the position */
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)

K
Kevin Branigan 已提交
216
/* Parse the input text to generate a number, and populate the result into item. */
217
static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
218
{
219
    double number = 0;
220
    unsigned char *after_end = NULL;
221 222 223
    unsigned char number_c_string[64];
    unsigned char decimal_point = get_decimal_point();
    size_t i = 0;
M
Max Bruckner 已提交
224

M
Max Bruckner 已提交
225
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
226
    {
227
        return false;
228 229
    }

230
    /* copy the number into a temporary buffer and replace '.' with the decimal point
M
Max Bruckner 已提交
231 232 233
     * of the current locale (for strtod)
     * This also takes care of '\0' not necessarily being available for marking the end of the input */
    for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
234
    {
M
Max Bruckner 已提交
235
        switch (buffer_at_offset(input_buffer)[i])
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '+':
            case '-':
            case 'e':
            case 'E':
M
Max Bruckner 已提交
251
                number_c_string[i] = buffer_at_offset(input_buffer)[i];
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
                break;

            case '.':
                number_c_string[i] = decimal_point;
                break;

            default:
                goto loop_end;
        }
    }
loop_end:
    number_c_string[i] = '\0';

    number = strtod((const char*)number_c_string, (char**)&after_end);
    if (number_c_string == after_end)
M
Max Bruckner 已提交
267
    {
268
        return false; /* parse_error */
M
Max Bruckner 已提交
269 270
    }

271
    item->valuedouble = number;
M
Max Bruckner 已提交
272

273
    /* use saturation in case of overflow */
274
    if (number >= INT_MAX)
275 276 277
    {
        item->valueint = INT_MAX;
    }
278
    else if (number <= INT_MIN)
279 280 281 282 283
    {
        item->valueint = INT_MIN;
    }
    else
    {
284
        item->valueint = (int)number;
285
    }
286

M
Max Bruckner 已提交
287 288
    item->type = cJSON_Number;

M
Max Bruckner 已提交
289
    input_buffer->offset += (size_t)(after_end - number_c_string);
290
    return true;
K
Kevin Branigan 已提交
291 292
}

293
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
294
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
295 296 297 298 299 300 301 302 303 304 305
{
    if (number >= INT_MAX)
    {
        object->valueint = INT_MAX;
    }
    else if (number <= INT_MIN)
    {
        object->valueint = INT_MIN;
    }
    else
    {
306
        object->valueint = (int)number;
307 308 309 310 311
    }

    return object->valuedouble = number;
}

M
Max Bruckner 已提交
312 313
typedef struct
{
314
    unsigned char *buffer;
315 316
    size_t length;
    size_t offset;
317
    cJSON_bool noalloc;
M
Max Bruckner 已提交
318
} printbuffer;
319

M
Max Bruckner 已提交
320
/* realloc printbuffer if necessary to have at least "needed" bytes more */
321
static unsigned char* ensure(printbuffer * const p, size_t needed, const internal_hooks * const hooks)
322
{
323
    unsigned char *newbuffer = NULL;
324 325
    size_t newsize = 0;

326
    if ((p == NULL) || (p->buffer == NULL))
327 328 329 330
    {
        return NULL;
    }

M
Max Bruckner 已提交
331 332 333 334 335 336
    if ((p->length > 0) && (p->offset >= p->length))
    {
        /* make sure that offset is valid */
        return NULL;
    }

337
    if (needed > INT_MAX)
M
Max Bruckner 已提交
338
    {
339
        /* sizes bigger than INT_MAX are currently not supported */
340
        return NULL;
M
Max Bruckner 已提交
341
    }
342

343
    needed += p->offset + 1;
M
Max Bruckner 已提交
344 345 346 347 348
    if (needed <= p->length)
    {
        return p->buffer + p->offset;
    }

349 350 351 352
    if (p->noalloc) {
        return NULL;
    }

353
    /* calculate new buffer size */
M
Max Bruckner 已提交
354
    if (needed > (INT_MAX / 2))
355 356 357 358 359 360 361 362 363 364 365
    {
        /* overflow of int, use INT_MAX if possible */
        if (needed <= INT_MAX)
        {
            newsize = INT_MAX;
        }
        else
        {
            return NULL;
        }
    }
366 367 368 369
    else
    {
        newsize = needed * 2;
    }
370

371
    if (hooks->reallocate != NULL)
M
Max Bruckner 已提交
372
    {
M
Max Bruckner 已提交
373
        /* reallocate with realloc if available */
374
        newbuffer = (unsigned char*)hooks->reallocate(p->buffer, newsize);
M
Max Bruckner 已提交
375
    }
M
Max Bruckner 已提交
376
    else
M
Max Bruckner 已提交
377
    {
M
Max Bruckner 已提交
378
        /* otherwise reallocate manually */
379
        newbuffer = (unsigned char*)hooks->allocate(newsize);
M
Max Bruckner 已提交
380 381
        if (!newbuffer)
        {
382
            hooks->deallocate(p->buffer);
M
Max Bruckner 已提交
383 384 385 386 387 388 389
            p->length = 0;
            p->buffer = NULL;

            return NULL;
        }
        if (newbuffer)
        {
390
            memcpy(newbuffer, p->buffer, p->offset + 1);
M
Max Bruckner 已提交
391
        }
392
        hooks->deallocate(p->buffer);
M
Max Bruckner 已提交
393 394 395 396 397
    }
    p->length = newsize;
    p->buffer = newbuffer;

    return newbuffer + p->offset;
398 399
}

400 401
/* calculate the new length of the string in a printbuffer and update the offset */
static void update_offset(printbuffer * const buffer)
K
Kevin Branigan 已提交
402
{
403 404
    const unsigned char *buffer_pointer = NULL;
    if ((buffer == NULL) || (buffer->buffer == NULL))
M
Max Bruckner 已提交
405
    {
406
        return;
M
Max Bruckner 已提交
407
    }
408
    buffer_pointer = buffer->buffer + buffer->offset;
M
Max Bruckner 已提交
409

410
    buffer->offset += strlen((const char*)buffer_pointer);
411 412 413
}

/* Render the number nicely from the given item into a string. */
414
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks)
415
{
M
Max Bruckner 已提交
416
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
417
    double d = item->valuedouble;
418
    int length = 0;
419
    size_t i = 0;
420
    unsigned char number_buffer[26]; /* temporary buffer to print the number into */
421
    unsigned char decimal_point = get_decimal_point();
422
    double test;
M
Max Bruckner 已提交
423

M
Max Bruckner 已提交
424
    if (output_buffer == NULL)
M
Max Bruckner 已提交
425
    {
426
        return false;
M
Max Bruckner 已提交
427
    }
M
Max Bruckner 已提交
428

429 430 431
    /* This checks for NaN and Infinity */
    if ((d * 0) != 0)
    {
432
        length = sprintf((char*)number_buffer, "null");
433 434 435
    }
    else
    {
436 437 438 439 440 441 442 443 444
        /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
        length = sprintf((char*)number_buffer, "%1.15g", d);

        /* Check whether the original double can be recovered */
        if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d))
        {
            /* If not, print with 17 decimal places of precision */
            length = sprintf((char*)number_buffer, "%1.17g", d);
        }
M
Max Bruckner 已提交
445
    }
446

447 448
    /* sprintf failed or buffer overrun occured */
    if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
449
    {
450
        return false;
451 452
    }

453 454 455 456 457
    /* reserve appropriate space in the output */
    output_pointer = ensure(output_buffer, (size_t)length, hooks);
    if (output_pointer == NULL)
    {
        return false;
458 459
    }

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    /* copy the printed number to the output and replace locale
     * dependent decimal point with '.' */
    for (i = 0; i < ((size_t)length); i++)
    {
        if (number_buffer[i] == decimal_point)
        {
            output_pointer[i] = '.';
            continue;
        }

        output_pointer[i] = number_buffer[i];
    }
    output_pointer[i] = '\0';

    output_buffer->offset += (size_t)length;

476
    return true;
K
Kevin Branigan 已提交
477 478
}

M
Max Bruckner 已提交
479
/* parse 4 digit hexadecimal number */
480
static unsigned parse_hex4(const unsigned char * const input)
481
{
M
Max Bruckner 已提交
482
    unsigned int h = 0;
483
    size_t i = 0;
M
Max Bruckner 已提交
484

485
    for (i = 0; i < 4; i++)
M
Max Bruckner 已提交
486
    {
487
        /* parse digit */
488
        if ((input[i] >= '0') && (input[i] <= '9'))
489
        {
490
            h += (unsigned int) input[i] - '0';
491
        }
492
        else if ((input[i] >= 'A') && (input[i] <= 'F'))
493
        {
494
            h += (unsigned int) 10 + input[i] - 'A';
495
        }
496
        else if ((input[i] >= 'a') && (input[i] <= 'f'))
497
        {
498
            h += (unsigned int) 10 + input[i] - 'a';
499 500 501 502 503
        }
        else /* invalid */
        {
            return 0;
        }
M
Max Bruckner 已提交
504

505 506 507 508 509
        if (i < 3)
        {
            /* shift left to make place for the next nibble */
            h = h << 4;
        }
M
Max Bruckner 已提交
510 511 512
    }

    return h;
513 514
}

515 516
/* converts a UTF-16 literal to UTF-8
 * A literal can be one or two sequences of the form \uXXXX */
517
static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
M
Max Bruckner 已提交
518
{
519 520 521
    long unsigned int codepoint = 0;
    unsigned int first_code = 0;
    const unsigned char *first_sequence = input_pointer;
522
    unsigned char utf8_length = 0;
523
    unsigned char utf8_position = 0;
524
    unsigned char sequence_length = 0;
525
    unsigned char first_byte_mark = 0;
526 527 528 529 530 531

    if ((input_end - first_sequence) < 6)
    {
        /* input ends unexpectedly */
        goto fail;
    }
M
Max Bruckner 已提交
532

533 534 535
    /* get the first utf16 sequence */
    first_code = parse_hex4(first_sequence + 2);

536
    /* check that the code is valid */
537
    if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
M
Max Bruckner 已提交
538
    {
539
        goto fail;
M
Max Bruckner 已提交
540
    }
M
Max Bruckner 已提交
541

542 543
    /* UTF16 surrogate pair */
    if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
M
Max Bruckner 已提交
544
    {
545 546 547 548 549
        const unsigned char *second_sequence = first_sequence + 6;
        unsigned int second_code = 0;
        sequence_length = 12; /* \uXXXX\uXXXX */

        if ((input_end - second_sequence) < 6)
M
Max Bruckner 已提交
550
        {
551 552
            /* input ends unexpectedly */
            goto fail;
M
Max Bruckner 已提交
553
        }
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

        if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
        {
            /* missing second half of the surrogate pair */
            goto fail;
        }

        /* get the second utf16 sequence */
        second_code = parse_hex4(second_sequence + 2);
        /* check that the code is valid */
        if ((second_code < 0xDC00) || (second_code > 0xDFFF))
        {
            /* invalid second half of the surrogate pair */
            goto fail;
        }


        /* calculate the unicode codepoint from the surrogate pair */
        codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
    }
    else
    {
        sequence_length = 6; /* \uXXXX */
        codepoint = first_code;
M
Max Bruckner 已提交
578
    }
M
Max Bruckner 已提交
579

580 581 582 583 584 585 586 587 588 589 590 591
    /* encode as UTF-8
     * takes at maximum 4 bytes to encode:
     * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
    if (codepoint < 0x80)
    {
        /* normal ascii, encoding 0xxxxxxx */
        utf8_length = 1;
    }
    else if (codepoint < 0x800)
    {
        /* two bytes, encoding 110xxxxx 10xxxxxx */
        utf8_length = 2;
592
        first_byte_mark = 0xC0; /* 11000000 */
593 594 595 596 597
    }
    else if (codepoint < 0x10000)
    {
        /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
        utf8_length = 3;
598
        first_byte_mark = 0xE0; /* 11100000 */
599 600 601 602 603
    }
    else if (codepoint <= 0x10FFFF)
    {
        /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
        utf8_length = 4;
604
        first_byte_mark = 0xF0; /* 11110000 */
605 606
    }
    else
M
Max Bruckner 已提交
607
    {
608
        /* invalid unicode codepoint */
609
        goto fail;
M
Max Bruckner 已提交
610 611
    }

612
    /* encode as utf8 */
613 614 615 616 617
    for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
    {
        /* 10xxxxxx */
        (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
        codepoint >>= 6;
618
    }
619 620 621 622 623 624 625 626
    /* encode first byte */
    if (utf8_length > 1)
    {
        (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
    }
    else
    {
        (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
627
    }
628

629 630 631 632 633 634 635 636 637
    *output_pointer += utf8_length;

    return sequence_length;

fail:
    return 0;
}

/* Parse the input text into an unescaped cinput, and populate item. */
638
static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks)
639
{
M
Max Bruckner 已提交
640 641
    const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
    const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
642 643 644 645
    unsigned char *output_pointer = NULL;
    unsigned char *output = NULL;

    /* not a string */
M
Max Bruckner 已提交
646
    if (buffer_at_offset(input_buffer)[0] != '\"')
647 648 649 650 651 652 653 654
    {
        goto fail;
    }

    {
        /* calculate approximate size of the output (overestimate) */
        size_t allocation_length = 0;
        size_t skipped_bytes = 0;
M
Max Bruckner 已提交
655
        while ((*input_end != '\"') && ((size_t)(input_end - input_buffer->content) < input_buffer->length))
656 657 658 659
        {
            /* is escape sequence */
            if (input_end[0] == '\\')
            {
M
Max Bruckner 已提交
660
                if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
661 662 663 664 665 666 667 668 669
                {
                    /* prevent buffer overflow when last input character is a backslash */
                    goto fail;
                }
                skipped_bytes++;
                input_end++;
            }
            input_end++;
        }
M
Max Bruckner 已提交
670
        if (*input_end != '\"')
671 672 673 674 675
        {
            goto fail; /* string ended unexpectedly */
        }

        /* This is at most how much we need for the output */
M
Max Bruckner 已提交
676
        allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
677
        output = (unsigned char*)hooks->allocate(allocation_length + sizeof(""));
678 679 680 681 682 683 684
        if (output == NULL)
        {
            goto fail; /* allocation failure */
        }
    }

    output_pointer = output;
M
Max Bruckner 已提交
685
    /* loop through the string literal */
686
    while (input_pointer < input_end)
M
Max Bruckner 已提交
687
    {
688
        if (*input_pointer != '\\')
M
Max Bruckner 已提交
689
        {
690
            *output_pointer++ = *input_pointer++;
M
Max Bruckner 已提交
691 692 693 694
        }
        /* escape sequence */
        else
        {
695
            unsigned char sequence_length = 2;
M
Max Bruckner 已提交
696 697 698 699 700
            if ((input_end - input_pointer) < 1)
            {
                goto fail;
            }

701
            switch (input_pointer[1])
M
Max Bruckner 已提交
702 703
            {
                case 'b':
704
                    *output_pointer++ = '\b';
M
Max Bruckner 已提交
705 706
                    break;
                case 'f':
707
                    *output_pointer++ = '\f';
M
Max Bruckner 已提交
708 709
                    break;
                case 'n':
710
                    *output_pointer++ = '\n';
M
Max Bruckner 已提交
711 712
                    break;
                case 'r':
713
                    *output_pointer++ = '\r';
M
Max Bruckner 已提交
714 715
                    break;
                case 't':
716
                    *output_pointer++ = '\t';
M
Max Bruckner 已提交
717
                    break;
718 719 720
                case '\"':
                case '\\':
                case '/':
721
                    *output_pointer++ = input_pointer[1];
722
                    break;
723 724

                /* UTF-16 literal */
M
Max Bruckner 已提交
725
                case 'u':
726
                    sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
727
                    if (sequence_length == 0)
M
Max Bruckner 已提交
728
                    {
729
                        /* failed to convert UTF16-literal to UTF-8 */
730
                        goto fail;
M
Max Bruckner 已提交
731 732
                    }
                    break;
733

M
Max Bruckner 已提交
734
                default:
735
                    goto fail;
M
Max Bruckner 已提交
736
            }
737
            input_pointer += sequence_length;
M
Max Bruckner 已提交
738 739
        }
    }
740 741 742

    /* zero terminate the output */
    *output_pointer = '\0';
M
Max Bruckner 已提交
743

744
    item->type = cJSON_String;
745
    item->valuestring = (char*)output;
746

M
Max Bruckner 已提交
747 748 749
    input_buffer->offset = (size_t) (input_end - input_buffer->content);
    input_buffer->offset++;

750
    return true;
751 752

fail:
753
    if (output != NULL)
754
    {
755
        hooks->deallocate(output);
756 757
    }

758 759 760 761 762
    if (input_pointer != NULL)
    {
        input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
    }

763
    return false;
K
Kevin Branigan 已提交
764 765 766
}

/* Render the cstring provided to an escaped version that can be printed. */
767
static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
768
{
769 770 771
    const unsigned char *input_pointer = NULL;
    unsigned char *output = NULL;
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
772 773 774
    size_t output_length = 0;
    /* numbers of additional characters needed for escaping */
    size_t escape_characters = 0;
M
Max Bruckner 已提交
775

776
    if (output_buffer == NULL)
M
Max Bruckner 已提交
777
    {
778
        return false;
M
Max Bruckner 已提交
779 780 781
    }

    /* empty string */
782
    if (input == NULL)
M
Max Bruckner 已提交
783
    {
784
        output = ensure(output_buffer, sizeof("\"\""), hooks);
785
        if (output == NULL)
M
Max Bruckner 已提交
786
        {
787
            return false;
M
Max Bruckner 已提交
788
        }
789
        strcpy((char*)output, "\"\"");
M
Max Bruckner 已提交
790

791
        return true;
M
Max Bruckner 已提交
792 793 794
    }

    /* set "flag" to 1 if something needs to be escaped */
795
    for (input_pointer = input; *input_pointer; input_pointer++)
M
Max Bruckner 已提交
796
    {
M
Max Bruckner 已提交
797
        switch (*input_pointer)
M
Max Bruckner 已提交
798
        {
M
Max Bruckner 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
            case '\"':
            case '\\':
            case '\b':
            case '\f':
            case '\n':
            case '\r':
            case '\t':
                /* one character escape sequence */
                escape_characters++;
                break;
            default:
                if (*input_pointer < 32)
                {
                    /* UTF-16 escape sequence uXXXX */
                    escape_characters += 5;
                }
                break;
M
Max Bruckner 已提交
816 817
        }
    }
M
Max Bruckner 已提交
818
    output_length = (size_t)(input_pointer - input) + escape_characters;
M
Max Bruckner 已提交
819

820
    output = ensure(output_buffer, output_length + sizeof("\"\""), hooks);
821
    if (output == NULL)
M
Max Bruckner 已提交
822
    {
823
        return false;
M
Max Bruckner 已提交
824 825
    }

M
Max Bruckner 已提交
826 827
    /* no characters have to be escaped */
    if (escape_characters == 0)
M
Max Bruckner 已提交
828
    {
M
Max Bruckner 已提交
829 830 831 832 833
        output[0] = '\"';
        memcpy(output + 1, input, output_length);
        output[output_length + 1] = '\"';
        output[output_length + 2] = '\0';

834
        return true;
M
Max Bruckner 已提交
835 836
    }

M
Max Bruckner 已提交
837 838
    output[0] = '\"';
    output_pointer = output + 1;
M
Max Bruckner 已提交
839
    /* copy the string */
M
Max Bruckner 已提交
840
    for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
M
Max Bruckner 已提交
841
    {
842
        if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
M
Max Bruckner 已提交
843 844
        {
            /* normal character, copy */
M
Max Bruckner 已提交
845
            *output_pointer = *input_pointer;
M
Max Bruckner 已提交
846 847 848 849
        }
        else
        {
            /* character needs to be escaped */
850
            *output_pointer++ = '\\';
M
Max Bruckner 已提交
851
            switch (*input_pointer)
M
Max Bruckner 已提交
852 853
            {
                case '\\':
M
Max Bruckner 已提交
854
                    *output_pointer = '\\';
M
Max Bruckner 已提交
855 856
                    break;
                case '\"':
M
Max Bruckner 已提交
857
                    *output_pointer = '\"';
M
Max Bruckner 已提交
858 859
                    break;
                case '\b':
M
Max Bruckner 已提交
860
                    *output_pointer = 'b';
M
Max Bruckner 已提交
861 862
                    break;
                case '\f':
M
Max Bruckner 已提交
863
                    *output_pointer = 'f';
M
Max Bruckner 已提交
864 865
                    break;
                case '\n':
M
Max Bruckner 已提交
866
                    *output_pointer = 'n';
M
Max Bruckner 已提交
867 868
                    break;
                case '\r':
M
Max Bruckner 已提交
869
                    *output_pointer = 'r';
M
Max Bruckner 已提交
870 871
                    break;
                case '\t':
M
Max Bruckner 已提交
872
                    *output_pointer = 't';
M
Max Bruckner 已提交
873 874 875
                    break;
                default:
                    /* escape and print as unicode codepoint */
M
Max Bruckner 已提交
876 877
                    sprintf((char*)output_pointer, "u%04x", *input_pointer);
                    output_pointer += 4;
M
Max Bruckner 已提交
878 879 880 881
                    break;
            }
        }
    }
M
Max Bruckner 已提交
882 883
    output[output_length + 1] = '\"';
    output[output_length + 2] = '\0';
M
Max Bruckner 已提交
884

885
    return true;
K
Kevin Branigan 已提交
886
}
M
Max Bruckner 已提交
887

M
Max Bruckner 已提交
888
/* Invoke print_string_ptr (which is useful) on an item. */
889
static cJSON_bool print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks)
M
Max Bruckner 已提交
890
{
891
    return print_string_ptr((unsigned char*)item->valuestring, p, hooks);
M
Max Bruckner 已提交
892
}
K
Kevin Branigan 已提交
893 894

/* Predeclare these prototypes. */
895
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks);
896
static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
897
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks);
898
static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
899
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks);
900
static cJSON_bool print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
K
Kevin Branigan 已提交
901 902

/* Utility to jump whitespace and cr/lf */
M
Max Bruckner 已提交
903 904 905 906 907 908 909
static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
{
    if ((buffer == NULL) || (buffer->content == NULL))
    {
        return NULL;
    }

M
Max Bruckner 已提交
910 911 912 913 914 915 916 917 918
    while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
    {
       buffer->offset++;
    }

    if (buffer->offset == buffer->length)
    {
        buffer->offset--;
    }
M
Max Bruckner 已提交
919 920 921 922

    return buffer;
}

K
Kevin Branigan 已提交
923
/* Parse an object - create a new root, and populate. */
924
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
K
Kevin Branigan 已提交
925
{
926
    parse_buffer buffer = { 0, 0, 0 };
927
    cJSON *item = NULL;
928 929 930 931

    /* reset error position */
    global_error.json = NULL;
    global_error.position = 0;
932 933 934 935

    if (value == NULL)
    {
        goto fail;
M
Max Bruckner 已提交
936 937
    }

M
Max Bruckner 已提交
938 939 940 941
    buffer.content = (const unsigned char*)value;
    buffer.length = strlen((const char*)value) + sizeof("");
    buffer.offset = 0;

942 943 944 945 946 947 948
    item = cJSON_New_Item(&global_hooks);
    if (item == NULL) /* memory fail */
    {
        goto fail;
    }

    if (!parse_value(item, buffer_skip_whitespace(&buffer), &global_hooks))
M
Max Bruckner 已提交
949 950
    {
        /* parse failure. ep is set. */
951
        goto fail;
M
Max Bruckner 已提交
952 953 954 955 956
    }

    /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
    if (require_null_terminated)
    {
957 958
        buffer_skip_whitespace(&buffer);
        if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
M
Max Bruckner 已提交
959
        {
960
            goto fail;
M
Max Bruckner 已提交
961 962 963 964
        }
    }
    if (return_parse_end)
    {
965
        *return_parse_end = (const char*)buffer_at_offset(&buffer);
M
Max Bruckner 已提交
966 967
    }

968
    return item;
969 970 971 972 973 974 975

fail:
    if (item != NULL)
    {
        cJSON_Delete(item);
    }

976 977
    if (value != NULL)
    {
978 979 980 981
        error local_error;
        local_error.json = (const unsigned char*)value;
        local_error.position = 0;

982 983
        if (buffer.offset < buffer.length)
        {
984
            local_error.position = buffer.offset;
985 986 987
        }
        else if (buffer.length > 0)
        {
988 989 990 991 992 993 994 995 996 997
            local_error.position = buffer.length - 1;
        }

        if (return_parse_end != NULL)
        {
            *return_parse_end = (const char*)local_error.json + local_error.position;
        }
        else
        {
            global_error = local_error;
998 999 1000
        }
    }

1001
    return NULL;
K
Kevin Branigan 已提交
1002
}
M
Max Bruckner 已提交
1003

1004
/* Default options for cJSON_Parse */
1005
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
M
Max Bruckner 已提交
1006 1007 1008
{
    return cJSON_ParseWithOpts(value, 0, 0);
}
K
Kevin Branigan 已提交
1009

1010
#define cjson_min(a, b) ((a < b) ? a : b)
M
Max Bruckner 已提交
1011

1012
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1013 1014 1015 1016 1017 1018 1019
{
    printbuffer buffer[1];
    unsigned char *printed = NULL;

    memset(buffer, 0, sizeof(buffer));

    /* create buffer */
1020
    buffer->buffer = (unsigned char*) hooks->allocate(256);
M
Max Bruckner 已提交
1021 1022 1023 1024 1025 1026
    if (buffer->buffer == NULL)
    {
        goto fail;
    }

    /* print the value */
1027
    if (!print_value(item, 0, format, buffer, hooks))
M
Max Bruckner 已提交
1028 1029 1030
    {
        goto fail;
    }
1031
    update_offset(buffer);
M
Max Bruckner 已提交
1032

1033 1034
    /* check if reallocate is available */
    if (hooks->reallocate != NULL)
M
Max Bruckner 已提交
1035
    {
1036 1037 1038 1039 1040
        printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->length);
        buffer->buffer = NULL;
        if (printed == NULL) {
            goto fail;
        }
M
Max Bruckner 已提交
1041
    }
1042 1043 1044 1045 1046 1047 1048
    else /* otherwise copy the JSON over to a new buffer */
    {
        printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
        if (printed == NULL)
        {
            goto fail;
        }
1049
        memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1050
        printed[buffer->offset] = '\0'; /* just to be sure */
M
Max Bruckner 已提交
1051

1052 1053 1054
        /* free the buffer */
        hooks->deallocate(buffer->buffer);
    }
M
Max Bruckner 已提交
1055 1056 1057 1058 1059 1060

    return printed;

fail:
    if (buffer->buffer != NULL)
    {
1061
        hooks->deallocate(buffer->buffer);
M
Max Bruckner 已提交
1062 1063 1064 1065
    }

    if (printed != NULL)
    {
1066
        hooks->deallocate(printed);
M
Max Bruckner 已提交
1067 1068 1069 1070 1071
    }

    return NULL;
}

K
Kevin Branigan 已提交
1072
/* Render a cJSON item/entity/structure to text. */
1073
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
M
Max Bruckner 已提交
1074
{
1075
    return (char*)print(item, true, &global_hooks);
M
Max Bruckner 已提交
1076 1077
}

1078
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
1079
{
1080
    return (char*)print(item, false, &global_hooks);
1081
}
1082

1083
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
1084
{
M
Max Bruckner 已提交
1085
    printbuffer p;
M
Max Bruckner 已提交
1086 1087 1088

    if (prebuffer < 0)
    {
M
Max Bruckner 已提交
1089
        return NULL;
M
Max Bruckner 已提交
1090 1091
    }

1092
    p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
1093 1094
    if (!p.buffer)
    {
1095
        return NULL;
1096
    }
M
Max Bruckner 已提交
1097 1098

    p.length = (size_t)prebuffer;
M
Max Bruckner 已提交
1099
    p.offset = 0;
1100
    p.noalloc = false;
M
Max Bruckner 已提交
1101

1102 1103 1104 1105 1106 1107
    if (!print_value(item, 0, fmt, &p, &global_hooks))
    {
        return NULL;
    }

    return (char*)p.buffer;
1108 1109
}

1110
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
1111 1112
{
    printbuffer p;
M
Max Bruckner 已提交
1113 1114 1115 1116 1117 1118

    if (len < 0)
    {
        return false;
    }

1119
    p.buffer = (unsigned char*)buf;
M
Max Bruckner 已提交
1120
    p.length = (size_t)len;
1121
    p.offset = 0;
1122
    p.noalloc = true;
1123
    return print_value(item, 0, fmt, &p, &global_hooks);
1124
}
K
Kevin Branigan 已提交
1125 1126

/* Parser core - when encountering text, process appropriately. */
1127
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1128
{
M
Max Bruckner 已提交
1129
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
M
Max Bruckner 已提交
1130
    {
1131
        return false; /* no input */
M
Max Bruckner 已提交
1132 1133 1134
    }

    /* parse the different types of values */
1135
    /* null */
M
Max Bruckner 已提交
1136
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
M
Max Bruckner 已提交
1137 1138
    {
        item->type = cJSON_NULL;
M
Max Bruckner 已提交
1139
        input_buffer->offset += 4;
1140
        return true;
M
Max Bruckner 已提交
1141
    }
1142
    /* false */
M
Max Bruckner 已提交
1143
    if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
M
Max Bruckner 已提交
1144 1145
    {
        item->type = cJSON_False;
M
Max Bruckner 已提交
1146
        input_buffer->offset += 5;
1147
        return true;
M
Max Bruckner 已提交
1148
    }
1149
    /* true */
M
Max Bruckner 已提交
1150
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
M
Max Bruckner 已提交
1151 1152 1153
    {
        item->type = cJSON_True;
        item->valueint = 1;
M
Max Bruckner 已提交
1154
        input_buffer->offset += 4;
1155
        return true;
M
Max Bruckner 已提交
1156
    }
1157
    /* string */
M
Max Bruckner 已提交
1158
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
M
Max Bruckner 已提交
1159
    {
1160
        return parse_string(item, input_buffer, hooks);
M
Max Bruckner 已提交
1161
    }
1162
    /* number */
M
Max Bruckner 已提交
1163
    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'))))
M
Max Bruckner 已提交
1164
    {
M
Max Bruckner 已提交
1165
        return parse_number(item, input_buffer);
M
Max Bruckner 已提交
1166
    }
1167
    /* array */
M
Max Bruckner 已提交
1168
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
M
Max Bruckner 已提交
1169
    {
1170
        return parse_array(item, input_buffer, hooks);
M
Max Bruckner 已提交
1171
    }
1172
    /* object */
M
Max Bruckner 已提交
1173
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
M
Max Bruckner 已提交
1174
    {
1175
        return parse_object(item, input_buffer, hooks);
M
Max Bruckner 已提交
1176 1177
    }

M
Max Bruckner 已提交
1178

1179
    return false;
K
Kevin Branigan 已提交
1180 1181 1182
}

/* Render a value to text. */
1183
static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format,  printbuffer * const output_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1184
{
M
Max Bruckner 已提交
1185
    unsigned char *output = NULL;
M
Max Bruckner 已提交
1186

M
Max Bruckner 已提交
1187
    if ((item == NULL) || (output_buffer == NULL))
M
Max Bruckner 已提交
1188
    {
1189
        return false;
M
Max Bruckner 已提交
1190
    }
M
Max Bruckner 已提交
1191 1192

    switch ((item->type) & 0xFF)
M
Max Bruckner 已提交
1193
    {
M
Max Bruckner 已提交
1194
        case cJSON_NULL:
1195
            output = ensure(output_buffer, 5, hooks);
1196
            if (output == NULL)
M
Max Bruckner 已提交
1197
            {
1198
                return false;
M
Max Bruckner 已提交
1199
            }
1200 1201 1202
            strcpy((char*)output, "null");
            return true;

M
Max Bruckner 已提交
1203
        case cJSON_False:
1204
            output = ensure(output_buffer, 6, hooks);
1205
            if (output == NULL)
M
Max Bruckner 已提交
1206
            {
1207
                return false;
M
Max Bruckner 已提交
1208
            }
1209 1210 1211
            strcpy((char*)output, "false");
            return true;

M
Max Bruckner 已提交
1212
        case cJSON_True:
1213
            output = ensure(output_buffer, 5, hooks);
1214
            if (output == NULL)
M
Max Bruckner 已提交
1215
            {
1216
                return false;
M
Max Bruckner 已提交
1217
            }
1218 1219 1220
            strcpy((char*)output, "true");
            return true;

M
Max Bruckner 已提交
1221
        case cJSON_Number:
1222
            return print_number(item, output_buffer, hooks);
1223

M
Max Bruckner 已提交
1224
        case cJSON_Raw:
M
Max Bruckner 已提交
1225
        {
M
Max Bruckner 已提交
1226 1227
            size_t raw_length = 0;
            if (item->valuestring == NULL)
1228
            {
M
Max Bruckner 已提交
1229
                if (!output_buffer->noalloc)
1230
                {
1231
                    hooks->deallocate(output_buffer->buffer);
1232
                }
1233
                return false;
M
Max Bruckner 已提交
1234
            }
1235

1236
            raw_length = strlen(item->valuestring) + sizeof("");
1237
            output = ensure(output_buffer, raw_length, hooks);
1238
            if (output == NULL)
M
Max Bruckner 已提交
1239
            {
1240
                return false;
1241
            }
1242 1243
            memcpy(output, item->valuestring, raw_length);
            return true;
M
Max Bruckner 已提交
1244
        }
1245

M
Max Bruckner 已提交
1246
        case cJSON_String:
1247
            return print_string(item, output_buffer, hooks);
1248

M
Max Bruckner 已提交
1249
        case cJSON_Array:
1250
            return print_array(item, depth, format, output_buffer, hooks);
1251

M
Max Bruckner 已提交
1252
        case cJSON_Object:
1253
            return print_object(item, depth, format, output_buffer, hooks);
1254

M
Max Bruckner 已提交
1255
        default:
1256
            return false;
M
Max Bruckner 已提交
1257
    }
K
Kevin Branigan 已提交
1258 1259 1260
}

/* Build an array from input text. */
1261
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1262
{
1263
    cJSON *head = NULL; /* head of the linked list */
1264 1265
    cJSON *current_item = NULL;

M
Max Bruckner 已提交
1266
    if (buffer_at_offset(input_buffer)[0] != '[')
M
Max Bruckner 已提交
1267
    {
1268
        /* not an array */
1269
        goto fail;
M
Max Bruckner 已提交
1270
    }
K
Kevin Branigan 已提交
1271

M
Max Bruckner 已提交
1272 1273 1274
    input_buffer->offset++;
    buffer_skip_whitespace(input_buffer);
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
M
Max Bruckner 已提交
1275
    {
1276
        /* empty array */
1277
        goto success;
M
Max Bruckner 已提交
1278
    }
K
Kevin Branigan 已提交
1279

M
Max Bruckner 已提交
1280 1281 1282 1283 1284 1285 1286
    /* check if we skipped to the end of the buffer */
    if (cannot_access_at_index(input_buffer, 0))
    {
        input_buffer->offset--;
        goto fail;
    }

1287
    /* step back to character in front of the first element */
M
Max Bruckner 已提交
1288
    input_buffer->offset--;
M
Max Bruckner 已提交
1289
    /* loop through the comma separated array elements */
1290
    do
M
Max Bruckner 已提交
1291
    {
1292
        /* allocate next item */
1293
        cJSON *new_item = cJSON_New_Item(hooks);
1294
        if (new_item == NULL)
M
Max Bruckner 已提交
1295
        {
1296
            goto fail; /* allocation failure */
M
Max Bruckner 已提交
1297
        }
1298 1299 1300

        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1301
        {
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
            /* start the linked list */
            current_item = head = new_item;
        }
        else
        {
            /* add to the end and advance */
            current_item->next = new_item;
            new_item->prev = current_item;
            current_item = new_item;
        }

        /* parse next value */
M
Max Bruckner 已提交
1314 1315
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1316
        if (!parse_value(current_item, input_buffer, hooks))
1317 1318
        {
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1319
        }
M
Max Bruckner 已提交
1320
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1321
    }
M
Max Bruckner 已提交
1322
    while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
M
Max Bruckner 已提交
1323

M
Max Bruckner 已提交
1324
    if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
M
Max Bruckner 已提交
1325
    {
1326
        goto fail; /* expected end of array */
M
Max Bruckner 已提交
1327 1328
    }

1329 1330
success:
    item->type = cJSON_Array;
1331
    item->child = head;
1332

M
Max Bruckner 已提交
1333 1334
    input_buffer->offset++;

1335
    return true;
K
Kevin Branigan 已提交
1336

1337
fail:
1338
    if (head != NULL)
1339
    {
1340
        cJSON_Delete(head);
1341 1342
    }

1343
    return false;
K
Kevin Branigan 已提交
1344 1345 1346
}

/* Render an array to text */
1347
static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1348
{
M
Max Bruckner 已提交
1349
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1350
    size_t length = 0;
M
Max Bruckner 已提交
1351
    cJSON *current_element = item->child;
K
Kevin Branigan 已提交
1352

M
Max Bruckner 已提交
1353
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1354
    {
1355
        return false;
M
Max Bruckner 已提交
1356 1357
    }

M
Max Bruckner 已提交
1358 1359
    /* Compose the output array. */
    /* opening square bracket */
1360
    output_pointer = ensure(output_buffer, 1, hooks);
M
Max Bruckner 已提交
1361
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1362
    {
1363
        return false;
M
Max Bruckner 已提交
1364 1365
    }

M
Max Bruckner 已提交
1366 1367
    *output_pointer = '[';
    output_buffer->offset++;
M
Max Bruckner 已提交
1368

M
Max Bruckner 已提交
1369
    while (current_element != NULL)
M
Max Bruckner 已提交
1370
    {
1371
        if (!print_value(current_element, depth + 1, format, output_buffer, hooks))
M
Max Bruckner 已提交
1372
        {
1373
            return false;
M
Max Bruckner 已提交
1374
        }
1375
        update_offset(output_buffer);
M
Max Bruckner 已提交
1376
        if (current_element->next)
M
Max Bruckner 已提交
1377
        {
1378
            length = (size_t) (format ? 2 : 1);
1379
            output_pointer = ensure(output_buffer, length + 1, hooks);
M
Max Bruckner 已提交
1380
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1381
            {
1382
                return false;
M
Max Bruckner 已提交
1383
            }
M
Max Bruckner 已提交
1384 1385
            *output_pointer++ = ',';
            if(format)
M
Max Bruckner 已提交
1386
            {
M
Max Bruckner 已提交
1387
                *output_pointer++ = ' ';
M
Max Bruckner 已提交
1388
            }
M
Max Bruckner 已提交
1389 1390
            *output_pointer = '\0';
            output_buffer->offset += length;
M
Max Bruckner 已提交
1391
        }
M
Max Bruckner 已提交
1392
        current_element = current_element->next;
M
Max Bruckner 已提交
1393 1394
    }

1395
    output_pointer = ensure(output_buffer, 2, hooks);
M
Max Bruckner 已提交
1396
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1397
    {
1398
        return false;
M
Max Bruckner 已提交
1399
    }
M
Max Bruckner 已提交
1400 1401
    *output_pointer++ = ']';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1402

1403
    return true;
K
Kevin Branigan 已提交
1404 1405 1406
}

/* Build an object from the text. */
1407
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1408
{
1409
    cJSON *head = NULL; /* linked list head */
1410 1411
    cJSON *current_item = NULL;

M
Max Bruckner 已提交
1412
    if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
M
Max Bruckner 已提交
1413
    {
1414
        goto fail; /* not an object */
M
Max Bruckner 已提交
1415 1416
    }

M
Max Bruckner 已提交
1417 1418 1419
    input_buffer->offset++;
    buffer_skip_whitespace(input_buffer);
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
M
Max Bruckner 已提交
1420
    {
1421
        goto success; /* empty object */
M
Max Bruckner 已提交
1422 1423
    }

M
Max Bruckner 已提交
1424 1425 1426 1427 1428 1429 1430
    /* check if we skipped to the end of the buffer */
    if (cannot_access_at_index(input_buffer, 0))
    {
        input_buffer->offset--;
        goto fail;
    }

1431
    /* step back to character in front of the first element */
M
Max Bruckner 已提交
1432
    input_buffer->offset--;
1433 1434
    /* loop through the comma separated array elements */
    do
M
Max Bruckner 已提交
1435
    {
1436
        /* allocate next item */
1437
        cJSON *new_item = cJSON_New_Item(hooks);
1438 1439 1440 1441
        if (new_item == NULL)
        {
            goto fail; /* allocation failure */
        }
M
Max Bruckner 已提交
1442

1443 1444
        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1445
        {
1446 1447 1448 1449 1450 1451 1452 1453 1454
            /* start the linked list */
            current_item = head = new_item;
        }
        else
        {
            /* add to the end and advance */
            current_item->next = new_item;
            new_item->prev = current_item;
            current_item = new_item;
M
Max Bruckner 已提交
1455 1456
        }

1457
        /* parse the name of the child */
M
Max Bruckner 已提交
1458 1459
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1460
        if (!parse_string(current_item, input_buffer, hooks))
M
Max Bruckner 已提交
1461
        {
1462
            goto fail; /* faile to parse name */
M
Max Bruckner 已提交
1463
        }
M
Max Bruckner 已提交
1464
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1465

1466 1467 1468
        /* swap valuestring and string, because we parsed the name */
        current_item->string = current_item->valuestring;
        current_item->valuestring = NULL;
M
Max Bruckner 已提交
1469

M
Max Bruckner 已提交
1470
        if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
M
Max Bruckner 已提交
1471
        {
1472
            goto fail; /* invalid object */
M
Max Bruckner 已提交
1473
        }
1474 1475

        /* parse the value */
M
Max Bruckner 已提交
1476 1477
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1478
        if (!parse_value(current_item, input_buffer, hooks))
M
Max Bruckner 已提交
1479
        {
1480
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1481
        }
M
Max Bruckner 已提交
1482
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1483
    }
M
Max Bruckner 已提交
1484
    while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1485

M
Max Bruckner 已提交
1486
    if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
M
Max Bruckner 已提交
1487
    {
1488
        goto fail; /* expected end of object */
M
Max Bruckner 已提交
1489 1490
    }

1491 1492
success:
    item->type = cJSON_Object;
1493
    item->child = head;
1494

M
Max Bruckner 已提交
1495
    input_buffer->offset++;
1496
    return true;
1497 1498

fail:
1499
    if (head != NULL)
1500
    {
1501
        cJSON_Delete(head);
1502 1503
    }

1504
    return false;
K
Kevin Branigan 已提交
1505 1506 1507
}

/* Render an object to text. */
1508
static cJSON_bool print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
1509
{
M
Max Bruckner 已提交
1510
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1511
    size_t length = 0;
M
Max Bruckner 已提交
1512
    cJSON *current_item = item->child;
M
Max Bruckner 已提交
1513

M
Max Bruckner 已提交
1514
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1515
    {
1516
        return false;
M
Max Bruckner 已提交
1517 1518
    }

M
Max Bruckner 已提交
1519
    /* Compose the output: */
1520
    length = (size_t) (format ? 2 : 1); /* fmt: {\n */
1521
    output_pointer = ensure(output_buffer, length + 1, hooks);
M
Max Bruckner 已提交
1522
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1523
    {
1524
        return false;
M
Max Bruckner 已提交
1525 1526
    }

M
Max Bruckner 已提交
1527 1528
    *output_pointer++ = '{';
    if (format)
M
Max Bruckner 已提交
1529
    {
M
Max Bruckner 已提交
1530
        *output_pointer++ = '\n';
M
Max Bruckner 已提交
1531
    }
M
Max Bruckner 已提交
1532
    output_buffer->offset += length;
M
Max Bruckner 已提交
1533

M
Max Bruckner 已提交
1534
    while (current_item)
M
Max Bruckner 已提交
1535
    {
M
Max Bruckner 已提交
1536
        if (format)
M
Max Bruckner 已提交
1537
        {
M
Max Bruckner 已提交
1538
            size_t i;
1539
            output_pointer = ensure(output_buffer, depth + 1, hooks);
M
Max Bruckner 已提交
1540
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1541
            {
1542
                return false;
M
Max Bruckner 已提交
1543
            }
M
Max Bruckner 已提交
1544
            for (i = 0; i < depth + 1; i++)
M
Max Bruckner 已提交
1545
            {
M
Max Bruckner 已提交
1546
                *output_pointer++ = '\t';
M
Max Bruckner 已提交
1547
            }
M
Max Bruckner 已提交
1548
            output_buffer->offset += depth + 1;
M
Max Bruckner 已提交
1549 1550
        }

M
Max Bruckner 已提交
1551
        /* print key */
1552
        if (!print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks))
M
Max Bruckner 已提交
1553
        {
1554
            return false;
M
Max Bruckner 已提交
1555
        }
M
Max Bruckner 已提交
1556
        update_offset(output_buffer);
M
Max Bruckner 已提交
1557

1558
        length = (size_t) (format ? 2 : 1);
1559
        output_pointer = ensure(output_buffer, length, hooks);
M
Max Bruckner 已提交
1560
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1561
        {
1562
            return false;
M
Max Bruckner 已提交
1563
        }
M
Max Bruckner 已提交
1564 1565
        *output_pointer++ = ':';
        if (format)
M
Max Bruckner 已提交
1566
        {
M
Max Bruckner 已提交
1567
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1568
        }
M
Max Bruckner 已提交
1569
        output_buffer->offset += length;
M
Max Bruckner 已提交
1570

M
Max Bruckner 已提交
1571
        /* print value */
1572
        if (!print_value(current_item, depth + 1, format, output_buffer, hooks))
M
Max Bruckner 已提交
1573
        {
1574
            return false;
M
Max Bruckner 已提交
1575
        }
M
Max Bruckner 已提交
1576
        update_offset(output_buffer);
M
Max Bruckner 已提交
1577

M
Max Bruckner 已提交
1578
        /* print comma if not last */
1579
        length = (size_t) ((format ? 1 : 0) + (current_item->next ? 1 : 0));
1580
        output_pointer = ensure(output_buffer, length + 1, hooks);
M
Max Bruckner 已提交
1581
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1582
        {
1583
            return false;
M
Max Bruckner 已提交
1584
        }
M
Max Bruckner 已提交
1585
        if (current_item->next)
M
Max Bruckner 已提交
1586
        {
M
Max Bruckner 已提交
1587
            *output_pointer++ = ',';
M
Max Bruckner 已提交
1588 1589
        }

M
Max Bruckner 已提交
1590
        if (format)
M
Max Bruckner 已提交
1591
        {
M
Max Bruckner 已提交
1592
            *output_pointer++ = '\n';
M
Max Bruckner 已提交
1593
        }
M
Max Bruckner 已提交
1594 1595
        *output_pointer = '\0';
        output_buffer->offset += length;
M
Max Bruckner 已提交
1596

M
Max Bruckner 已提交
1597
        current_item = current_item->next;
M
Max Bruckner 已提交
1598
    }
M
Max Bruckner 已提交
1599

1600
    output_pointer = ensure(output_buffer, format ? (depth + 2) : 2, hooks);
M
Max Bruckner 已提交
1601
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1602
    {
1603
        return false;
M
Max Bruckner 已提交
1604
    }
M
Max Bruckner 已提交
1605
    if (format)
M
Max Bruckner 已提交
1606
    {
M
Max Bruckner 已提交
1607 1608
        size_t i;
        for (i = 0; i < (depth); i++)
M
Max Bruckner 已提交
1609
        {
M
Max Bruckner 已提交
1610
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1611 1612
        }
    }
M
Max Bruckner 已提交
1613 1614
    *output_pointer++ = '}';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1615

1616
    return true;
K
Kevin Branigan 已提交
1617 1618 1619
}

/* Get Array size/item / object item. */
1620
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
M
Max Bruckner 已提交
1621 1622
{
    cJSON *c = array->child;
1623
    size_t i = 0;
M
Max Bruckner 已提交
1624 1625 1626 1627 1628
    while(c)
    {
        i++;
        c = c->next;
    }
1629 1630 1631

    /* FIXME: Can overflow here. Cannot be fixed without breaking the API */

M
Max Bruckner 已提交
1632
    return (int)i;
M
Max Bruckner 已提交
1633 1634
}

1635
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item)
M
Max Bruckner 已提交
1636
{
1637
    cJSON *c = array ? array->child : NULL;
M
Max Bruckner 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646
    while (c && item > 0)
    {
        item--;
        c = c->next;
    }

    return c;
}

1647
static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
1648 1649 1650
{
    cJSON *current_element = NULL;

1651
    if ((object == NULL) || (name == NULL))
1652 1653 1654 1655 1656
    {
        return NULL;
    }

    current_element = object->child;
1657
    if (case_sensitive)
1658
    {
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
        while ((current_element != NULL) && (strcmp(name, current_element->string) != 0))
        {
            current_element = current_element->next;
        }
    }
    else
    {
        while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
        {
            current_element = current_element->next;
        }
1670 1671 1672 1673 1674
    }

    return current_element;
}

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string)
{
    return get_object_item(object, string, false);
}

CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
{
    return get_object_item(object, string, true);
}

1685
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
M
Max Bruckner 已提交
1686 1687 1688
{
    return cJSON_GetObjectItem(object, string) ? 1 : 0;
}
K
Kevin Branigan 已提交
1689 1690

/* Utility for array list handling. */
M
Max Bruckner 已提交
1691 1692 1693 1694 1695 1696
static void suffix_object(cJSON *prev, cJSON *item)
{
    prev->next = item;
    item->prev = prev;
}

K
Kevin Branigan 已提交
1697
/* Utility for handling references. */
1698
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1699
{
1700
    cJSON *ref = cJSON_New_Item(hooks);
M
Max Bruckner 已提交
1701 1702
    if (!ref)
    {
1703
        return NULL;
M
Max Bruckner 已提交
1704 1705
    }
    memcpy(ref, item, sizeof(cJSON));
1706
    ref->string = NULL;
M
Max Bruckner 已提交
1707
    ref->type |= cJSON_IsReference;
1708
    ref->next = ref->prev = NULL;
M
Max Bruckner 已提交
1709 1710
    return ref;
}
K
Kevin Branigan 已提交
1711 1712

/* Add item to array/object. */
1713
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
M
Max Bruckner 已提交
1714
{
1715 1716 1717
    cJSON *child = NULL;

    if ((item == NULL) || (array == NULL))
M
Max Bruckner 已提交
1718 1719 1720
    {
        return;
    }
1721 1722 1723 1724

    child = array->child;

    if (child == NULL)
M
Max Bruckner 已提交
1725 1726 1727 1728 1729 1730 1731
    {
        /* list is empty, start new one */
        array->child = item;
    }
    else
    {
        /* append to the end */
1732
        while (child->next)
M
Max Bruckner 已提交
1733
        {
1734
            child = child->next;
M
Max Bruckner 已提交
1735
        }
1736
        suffix_object(child, item);
M
Max Bruckner 已提交
1737 1738 1739
    }
}

1740
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
M
Max Bruckner 已提交
1741
{
1742
    /* call cJSON_AddItemToObjectCS for code reuse */
1743
    cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
1744 1745
    /* remove cJSON_StringIsConst flag */
    item->type &= ~cJSON_StringIsConst;
M
Max Bruckner 已提交
1746 1747
}

1748 1749 1750
#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic push
#endif
1751
#ifdef __GNUC__
1752
#pragma GCC diagnostic ignored "-Wcast-qual"
1753 1754
#endif

1755
/* Add an item to an object with constant string as key */
1756
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
1757 1758 1759 1760 1761 1762 1763
{
    if (!item)
    {
        return;
    }
    if (!(item->type & cJSON_StringIsConst) && item->string)
    {
1764
        global_hooks.deallocate(item->string);
1765 1766 1767 1768 1769
    }
    item->string = (char*)string;
    item->type |= cJSON_StringIsConst;
    cJSON_AddItemToArray(object, item);
}
1770 1771 1772
#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic pop
#endif
1773

1774
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
1775
{
1776
    cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
1777 1778
}

1779
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
1780
{
1781
    cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
1782 1783
}

M
Max Bruckner 已提交
1784
static cJSON *DetachItemFromArray(cJSON *array, size_t which)
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        /* item doesn't exist */
1795
        return NULL;
1796
    }
1797
    if (c->prev)
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
    {
        /* not the first element */
        c->prev->next = c->next;
    }
    if (c->next)
    {
        c->next->prev = c->prev;
    }
    if (c==array->child)
    {
        array->child = c->next;
    }
    /* make sure the detached item doesn't point anywhere anymore */
1811
    c->prev = c->next = NULL;
1812 1813 1814

    return c;
}
1815
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
M
Max Bruckner 已提交
1816 1817 1818 1819 1820 1821 1822 1823
{
    if (which < 0)
    {
        return NULL;
    }

    return DetachItemFromArray(array, (size_t)which);
}
K
Kevin Branigan 已提交
1824

1825
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
1826 1827 1828 1829
{
    cJSON_Delete(cJSON_DetachItemFromArray(array, which));
}

1830
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
1831
{
1832
    size_t i = 0;
1833
    cJSON *c = object->child;
1834
    while (c && (case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string) != 0))
1835 1836 1837 1838 1839 1840
    {
        i++;
        c = c->next;
    }
    if (c)
    {
M
Max Bruckner 已提交
1841
        return DetachItemFromArray(object, i);
1842 1843
    }

1844
    return NULL;
1845 1846
}

1847
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
1848 1849 1850
{
    cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}
K
Kevin Branigan 已提交
1851 1852

/* Replace array/object items with new ones. */
1853
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        cJSON_AddItemToArray(array, newitem);
        return;
    }
    newitem->next = c;
    newitem->prev = c->prev;
    c->prev = newitem;
    if (c == array->child)
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }
}

M
Max Bruckner 已提交
1879
static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem)
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        return;
    }
    newitem->next = c->next;
    newitem->prev = c->prev;
    if (newitem->next)
    {
        newitem->next->prev = newitem;
    }
    if (c == array->child)
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }
1905
    c->next = c->prev = NULL;
1906 1907
    cJSON_Delete(c);
}
1908
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
M
Max Bruckner 已提交
1909 1910 1911 1912 1913 1914 1915 1916
{
    if (which < 0)
    {
        return;
    }

    ReplaceItemInArray(array, (size_t)which, newitem);
}
1917

1918
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
1919
{
1920
    size_t i = 0;
1921
    cJSON *c = object->child;
1922
    while(c && (case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string) != 0))
1923 1924 1925 1926 1927 1928
    {
        i++;
        c = c->next;
    }
    if(c)
    {
1929 1930 1931
        /* free the old string if not const */
        if (!(newitem->type & cJSON_StringIsConst) && newitem->string)
        {
1932
             global_hooks.deallocate(newitem->string);
1933 1934
        }

1935
        newitem->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
M
Max Bruckner 已提交
1936
        ReplaceItemInArray(object, i, newitem);
1937 1938
    }
}
K
Kevin Branigan 已提交
1939 1940

/* Create basic types: */
1941
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
M
Max Bruckner 已提交
1942
{
1943
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1944 1945 1946 1947 1948 1949 1950 1951
    if(item)
    {
        item->type = cJSON_NULL;
    }

    return item;
}

1952
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
M
Max Bruckner 已提交
1953
{
1954
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1955 1956 1957 1958 1959 1960 1961 1962
    if(item)
    {
        item->type = cJSON_True;
    }

    return item;
}

1963
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
M
Max Bruckner 已提交
1964
{
1965
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1966 1967 1968 1969 1970 1971 1972 1973
    if(item)
    {
        item->type = cJSON_False;
    }

    return item;
}

1974
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b)
M
Max Bruckner 已提交
1975
{
1976
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1977 1978 1979 1980 1981 1982 1983 1984
    if(item)
    {
        item->type = b ? cJSON_True : cJSON_False;
    }

    return item;
}

1985
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
M
Max Bruckner 已提交
1986
{
1987
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1988 1989 1990 1991
    if(item)
    {
        item->type = cJSON_Number;
        item->valuedouble = num;
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005

        /* use saturation in case of overflow */
        if (num >= INT_MAX)
        {
            item->valueint = INT_MAX;
        }
        else if (num <= INT_MIN)
        {
            item->valueint = INT_MIN;
        }
        else
        {
            item->valueint = (int)num;
        }
M
Max Bruckner 已提交
2006 2007 2008 2009 2010
    }

    return item;
}

2011
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
M
Max Bruckner 已提交
2012
{
2013
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2014 2015 2016
    if(item)
    {
        item->type = cJSON_String;
2017
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
M
Max Bruckner 已提交
2018 2019 2020
        if(!item->valuestring)
        {
            cJSON_Delete(item);
2021
            return NULL;
M
Max Bruckner 已提交
2022 2023 2024 2025 2026 2027
        }
    }

    return item;
}

2028
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
J
Jiri Zouhar 已提交
2029
{
2030
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2031 2032 2033
    if(item)
    {
        item->type = cJSON_Raw;
2034
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
M
Max Bruckner 已提交
2035 2036 2037 2038 2039 2040 2041 2042
        if(!item->valuestring)
        {
            cJSON_Delete(item);
            return NULL;
        }
    }

    return item;
J
Jiri Zouhar 已提交
2043 2044
}

2045
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
M
Max Bruckner 已提交
2046
{
2047
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2048 2049 2050 2051 2052 2053 2054 2055
    if(item)
    {
        item->type=cJSON_Array;
    }

    return item;
}

2056
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
M
Max Bruckner 已提交
2057
{
2058
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2059 2060 2061 2062 2063 2064 2065
    if (item)
    {
        item->type = cJSON_Object;
    }

    return item;
}
K
Kevin Branigan 已提交
2066 2067

/* Create Arrays: */
2068
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
M
Max Bruckner 已提交
2069
{
2070
    size_t i = 0;
2071 2072
    cJSON *n = NULL;
    cJSON *p = NULL;
2073 2074 2075 2076 2077 2078 2079 2080 2081
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();
    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2082 2083 2084 2085 2086
    {
        n = cJSON_CreateNumber(numbers[i]);
        if (!n)
        {
            cJSON_Delete(a);
2087
            return NULL;
M
Max Bruckner 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2103
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
M
Max Bruckner 已提交
2104
{
2105
    size_t i = 0;
2106 2107
    cJSON *n = NULL;
    cJSON *p = NULL;
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2118
    {
2119
        n = cJSON_CreateNumber((double)numbers[i]);
M
Max Bruckner 已提交
2120 2121 2122
        if(!n)
        {
            cJSON_Delete(a);
2123
            return NULL;
M
Max Bruckner 已提交
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2139
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
2140
{
2141
    size_t i = 0;
2142 2143
    cJSON *n = NULL;
    cJSON *p = NULL;
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0;a && (i < (size_t)count); i++)
2154 2155 2156 2157 2158
    {
        n = cJSON_CreateNumber(numbers[i]);
        if(!n)
        {
            cJSON_Delete(a);
2159
            return NULL;
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2175
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
2176
{
2177
    size_t i = 0;
2178 2179
    cJSON *n = NULL;
    cJSON *p = NULL;
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for (i = 0; a && (i < (size_t)count); i++)
2190 2191 2192 2193 2194
    {
        n = cJSON_CreateString(strings[i]);
        if(!n)
        {
            cJSON_Delete(a);
2195
            return NULL;
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p,n);
        }
        p = n;
    }

    return a;
}
2210 2211

/* Duplication */
2212
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2213
{
M
Max Bruckner 已提交
2214
    cJSON *newitem = NULL;
2215 2216
    cJSON *child = NULL;
    cJSON *next = NULL;
M
Max Bruckner 已提交
2217
    cJSON *newchild = NULL;
M
Max Bruckner 已提交
2218 2219 2220 2221

    /* Bail on bad ptr */
    if (!item)
    {
2222
        goto fail;
M
Max Bruckner 已提交
2223 2224
    }
    /* Create new item */
2225
    newitem = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2226 2227
    if (!newitem)
    {
2228
        goto fail;
M
Max Bruckner 已提交
2229 2230 2231 2232 2233 2234 2235
    }
    /* Copy over all vars */
    newitem->type = item->type & (~cJSON_IsReference);
    newitem->valueint = item->valueint;
    newitem->valuedouble = item->valuedouble;
    if (item->valuestring)
    {
2236
        newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
M
Max Bruckner 已提交
2237 2238
        if (!newitem->valuestring)
        {
2239
            goto fail;
M
Max Bruckner 已提交
2240 2241 2242 2243
        }
    }
    if (item->string)
    {
2244
        newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
M
Max Bruckner 已提交
2245 2246
        if (!newitem->string)
        {
2247
            goto fail;
M
Max Bruckner 已提交
2248 2249 2250 2251 2252 2253 2254 2255
        }
    }
    /* If non-recursive, then we're done! */
    if (!recurse)
    {
        return newitem;
    }
    /* Walk the ->next chain for the child. */
2256 2257
    child = item->child;
    while (child != NULL)
M
Max Bruckner 已提交
2258
    {
2259
        newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
M
Max Bruckner 已提交
2260 2261
        if (!newchild)
        {
2262
            goto fail;
M
Max Bruckner 已提交
2263
        }
2264
        if (next != NULL)
M
Max Bruckner 已提交
2265 2266
        {
            /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2267 2268 2269
            next->next = newchild;
            newchild->prev = next;
            next = newchild;
M
Max Bruckner 已提交
2270 2271 2272 2273
        }
        else
        {
            /* Set newitem->child and move to it */
2274 2275
            newitem->child = newchild;
            next = newchild;
M
Max Bruckner 已提交
2276
        }
2277
        child = child->next;
M
Max Bruckner 已提交
2278 2279 2280
    }

    return newitem;
2281 2282 2283 2284 2285 2286 2287 2288

fail:
    if (newitem != NULL)
    {
        cJSON_Delete(newitem);
    }

    return NULL;
2289
}
2290

2291
CJSON_PUBLIC(void) cJSON_Minify(char *json)
2292
{
2293
    unsigned char *into = (unsigned char*)json;
M
Max Bruckner 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
    while (*json)
    {
        if (*json == ' ')
        {
            json++;
        }
        else if (*json == '\t')
        {
            /* Whitespace characters. */
            json++;
        }
        else if (*json == '\r')
        {
            json++;
        }
        else if (*json=='\n')
        {
            json++;
        }
        else if ((*json == '/') && (json[1] == '/'))
        {
            /* double-slash comments, to end of line. */
            while (*json && (*json != '\n'))
            {
                json++;
            }
        }
        else if ((*json == '/') && (json[1] == '*'))
        {
            /* multiline comments. */
            while (*json && !((*json == '*') && (json[1] == '/')))
            {
                json++;
            }
            json += 2;
        }
        else if (*json == '\"')
        {
            /* string literals, which are \" sensitive. */
M
Max Bruckner 已提交
2333
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2334 2335 2336 2337
            while (*json && (*json != '\"'))
            {
                if (*json == '\\')
                {
M
Max Bruckner 已提交
2338
                    *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2339
                }
M
Max Bruckner 已提交
2340
                *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2341
            }
M
Max Bruckner 已提交
2342
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2343 2344 2345 2346
        }
        else
        {
            /* All other characters. */
M
Max Bruckner 已提交
2347
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2348 2349 2350 2351 2352
        }
    }

    /* and null-terminate. */
    *into = '\0';
2353
}
2354

2355
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
2356 2357 2358 2359 2360 2361 2362 2363 2364
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Invalid;
}

2365
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
2366 2367 2368 2369 2370 2371 2372 2373 2374
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_False;
}

2375
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xff) == cJSON_True;
}


2386
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
2387 2388 2389 2390 2391 2392 2393 2394
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & (cJSON_True | cJSON_False)) != 0;
}
2395
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
2396 2397 2398 2399 2400 2401 2402 2403 2404
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_NULL;
}

2405
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
2406 2407 2408 2409 2410 2411 2412 2413 2414
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Number;
}

2415
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
2416 2417 2418 2419 2420 2421 2422 2423 2424
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_String;
}

2425
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
2426 2427 2428 2429 2430 2431 2432 2433 2434
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Array;
}

2435
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
2436 2437 2438 2439 2440 2441 2442 2443 2444
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Object;
}

2445
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
2446 2447 2448 2449 2450 2451 2452 2453
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Raw;
}
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554

CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
{
    if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a))
    {
        return false;
    }

    /* check if type is valid */
    switch (a->type & 0xFF)
    {
        case cJSON_False:
        case cJSON_True:
        case cJSON_NULL:
        case cJSON_Number:
        case cJSON_String:
        case cJSON_Raw:
        case cJSON_Array:
        case cJSON_Object:
            break;

        default:
            return false;
    }

    /* identical objects are equal */
    if (a == b)
    {
        return true;
    }

    switch (a->type & 0xFF)
    {
        /* in these cases and equal type is enough */
        case cJSON_False:
        case cJSON_True:
        case cJSON_NULL:
            return true;

        case cJSON_Number:
            if (a->valuedouble == b->valuedouble)
            {
                return true;
            }
            return false;

        case cJSON_String:
        case cJSON_Raw:
            if ((a->valuestring == NULL) || (b->valuestring == NULL))
            {
                return false;
            }
            if (strcmp(a->valuestring, b->valuestring) == 0)
            {
                return true;
            }

            return false;

        case cJSON_Array:
        {
            cJSON *a_element = NULL;
            cJSON *b_element = NULL;
            for (a_element = a->child, b_element = b->child;
                    (a_element != NULL) && (b_element != NULL);
                    a_element = a_element->next, b_element = b_element->next)
            {
                if (!cJSON_Compare(a_element, b_element, case_sensitive))
                {
                    return false;
                }
            }

            return true;
        }

        case cJSON_Object:
        {
            cJSON *a_element = NULL;
            cJSON_ArrayForEach(a_element, a)
            {
                /* TODO This has O(n^2) runtime, which is horrible! */
                cJSON *b_element = get_object_item(b, a_element->string, case_sensitive);
                if (b_element == NULL)
                {
                    return false;
                }

                if (!cJSON_Compare(a_element, b_element, case_sensitive))
                {
                    return false;
                }
            }

            return true;
        }

        default:
            return false;
    }
}
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564

CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
{
    return global_hooks.allocate(size);
}

CJSON_PUBLIC(void) cJSON_free(void *object)
{
    global_hooks.deallocate(object);
}