cJSON.c 68.2 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 27 28 29 30
/* disable warnings about old C89 functions in MSVC */
#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE
#endif

31
#ifdef __GNUC__
32
#pragma GCC visibility push(default)
33
#endif
34 35 36 37 38
#if defined(_MSC_VER)
#pragma warning (push)
/* disable warning about single line comments in system headers */
#pragma warning (disable : 4001)
#endif
39

K
Kevin Branigan 已提交
40 41 42 43 44 45 46
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
47 48

#ifdef ENABLE_LOCALES
49
#include <locale.h>
50
#endif
51

52 53 54
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
55
#ifdef __GNUC__
56
#pragma GCC visibility pop
57
#endif
58

K
Kevin Branigan 已提交
59 60
#include "cJSON.h"

M
Max Bruckner 已提交
61
/* define our own boolean type */
62 63
#define true ((cJSON_bool)1)
#define false ((cJSON_bool)0)
M
Max Bruckner 已提交
64

65 66 67 68 69
typedef struct {
    const unsigned char *json;
    size_t position;
} error;
static error global_error = { NULL, 0 };
K
Kevin Branigan 已提交
70

71
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
M
Max Bruckner 已提交
72
{
73
    return (const char*) (global_error.json + global_error.position);
M
Max Bruckner 已提交
74
}
K
Kevin Branigan 已提交
75

M
Max Bruckner 已提交
76 77 78 79 80 81 82 83
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) {
    if (!cJSON_IsString(item)) {
        return NULL;
    }

    return item->valuestring;
}

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

89
CJSON_PUBLIC(const char*) cJSON_Version(void)
90 91 92 93 94 95 96
{
    static char version[15];
    sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);

    return version;
}

97 98
/* 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 已提交
99
{
100
    if ((string1 == NULL) || (string2 == NULL))
M
Max Bruckner 已提交
101
    {
102
        return 1;
M
Max Bruckner 已提交
103
    }
104

105
    if (string1 == string2)
M
Max Bruckner 已提交
106
    {
107
        return 0;
M
Max Bruckner 已提交
108
    }
109 110

    for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
M
Max Bruckner 已提交
111
    {
112
        if (*string1 == '\0')
M
Max Bruckner 已提交
113 114 115 116 117
        {
            return 0;
        }
    }

118
    return tolower(*string1) - tolower(*string2);
K
Kevin Branigan 已提交
119 120
}

121 122 123 124 125 126 127
typedef struct internal_hooks
{
    void *(*allocate)(size_t size);
    void (*deallocate)(void *pointer);
    void *(*reallocate)(void *pointer, size_t size);
} internal_hooks;

M
Max Bruckner 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#if defined(_MSC_VER)
/* work around MSVC error C2322: '...' address of dillimport '...' is not static */
static void *internal_malloc(size_t size)
{
    return malloc(size);
}
static void internal_free(void *pointer)
{
    free(pointer);
}
static void *internal_realloc(void *pointer, size_t size)
{
    return realloc(pointer, size);
}
#else
#define internal_malloc malloc
#define internal_free free
#define internal_realloc realloc
#endif

static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
K
Kevin Branigan 已提交
149

M
Max Bruckner 已提交
150
static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
151
{
M
Max Bruckner 已提交
152
    size_t length = 0;
153
    unsigned char *copy = NULL;
K
Kevin Branigan 已提交
154

M
Max Bruckner 已提交
155
    if (string == NULL)
156 157 158 159
    {
        return NULL;
    }

M
Max Bruckner 已提交
160
    length = strlen((const char*)string) + sizeof("");
161 162
    copy = (unsigned char*)hooks->allocate(length);
    if (copy == NULL)
M
Max Bruckner 已提交
163
    {
164
        return NULL;
M
Max Bruckner 已提交
165
    }
M
Max Bruckner 已提交
166
    memcpy(copy, string, length);
M
Max Bruckner 已提交
167 168

    return copy;
K
Kevin Branigan 已提交
169 170
}

171
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
K
Kevin Branigan 已提交
172
{
M
Max Bruckner 已提交
173
    if (hooks == NULL)
M
Max Bruckner 已提交
174 175
    {
        /* Reset hooks */
176 177 178
        global_hooks.allocate = malloc;
        global_hooks.deallocate = free;
        global_hooks.reallocate = realloc;
K
Kevin Branigan 已提交
179 180 181
        return;
    }

182
    global_hooks.allocate = malloc;
M
Max Bruckner 已提交
183 184
    if (hooks->malloc_fn != NULL)
    {
185
        global_hooks.allocate = hooks->malloc_fn;
M
Max Bruckner 已提交
186 187
    }

188
    global_hooks.deallocate = free;
M
Max Bruckner 已提交
189 190
    if (hooks->free_fn != NULL)
    {
191
        global_hooks.deallocate = hooks->free_fn;
M
Max Bruckner 已提交
192 193 194
    }

    /* use realloc only if both free and malloc are used */
195 196
    global_hooks.reallocate = NULL;
    if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
M
Max Bruckner 已提交
197
    {
198
        global_hooks.reallocate = realloc;
M
Max Bruckner 已提交
199
    }
K
Kevin Branigan 已提交
200 201 202
}

/* Internal constructor. */
203
static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
K
Kevin Branigan 已提交
204
{
205
    cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
M
Max Bruckner 已提交
206 207
    if (node)
    {
208
        memset(node, '\0', sizeof(cJSON));
M
Max Bruckner 已提交
209 210 211
    }

    return node;
K
Kevin Branigan 已提交
212 213 214
}

/* Delete a cJSON structure. */
M
Max Bruckner 已提交
215
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
K
Kevin Branigan 已提交
216
{
M
Max Bruckner 已提交
217
    cJSON *next = NULL;
M
Max Bruckner 已提交
218
    while (item != NULL)
M
Max Bruckner 已提交
219
    {
M
Max Bruckner 已提交
220 221
        next = item->next;
        if (!(item->type & cJSON_IsReference) && (item->child != NULL))
M
Max Bruckner 已提交
222
        {
M
Max Bruckner 已提交
223
            cJSON_Delete(item->child);
M
Max Bruckner 已提交
224
        }
M
Max Bruckner 已提交
225
        if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
M
Max Bruckner 已提交
226
        {
M
Max Bruckner 已提交
227
            global_hooks.deallocate(item->valuestring);
M
Max Bruckner 已提交
228
        }
M
Max Bruckner 已提交
229
        if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
M
Max Bruckner 已提交
230
        {
M
Max Bruckner 已提交
231
            global_hooks.deallocate(item->string);
M
Max Bruckner 已提交
232
        }
M
Max Bruckner 已提交
233 234
        global_hooks.deallocate(item);
        item = next;
M
Max Bruckner 已提交
235
    }
K
Kevin Branigan 已提交
236 237
}

238 239 240
/* get the decimal point character of the current locale */
static unsigned char get_decimal_point(void)
{
241
#ifdef ENABLE_LOCALES
242 243
    struct lconv *lconv = localeconv();
    return (unsigned char) lconv->decimal_point[0];
244
#else
245
    return '.';
246
#endif
247 248
}

M
Max Bruckner 已提交
249 250 251 252 253
typedef struct
{
    const unsigned char *content;
    size_t length;
    size_t offset;
254
    size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
255
    internal_hooks hooks;
M
Max Bruckner 已提交
256 257 258 259 260 261 262 263 264 265
} 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))
/* 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 已提交
266
/* Parse the input text to generate a number, and populate the result into item. */
267
static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
268
{
269
    double number = 0;
270
    unsigned char *after_end = NULL;
271 272 273
    unsigned char number_c_string[64];
    unsigned char decimal_point = get_decimal_point();
    size_t i = 0;
M
Max Bruckner 已提交
274

M
Max Bruckner 已提交
275
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
276
    {
277
        return false;
278 279
    }

280
    /* copy the number into a temporary buffer and replace '.' with the decimal point
M
Max Bruckner 已提交
281 282 283
     * 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++)
284
    {
M
Max Bruckner 已提交
285
        switch (buffer_at_offset(input_buffer)[i])
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        {
            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 已提交
301
                number_c_string[i] = buffer_at_offset(input_buffer)[i];
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
                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 已提交
317
    {
318
        return false; /* parse_error */
M
Max Bruckner 已提交
319 320
    }

321
    item->valuedouble = number;
M
Max Bruckner 已提交
322

323
    /* use saturation in case of overflow */
324
    if (number >= INT_MAX)
325 326 327
    {
        item->valueint = INT_MAX;
    }
328
    else if (number <= INT_MIN)
329 330 331 332 333
    {
        item->valueint = INT_MIN;
    }
    else
    {
334
        item->valueint = (int)number;
335
    }
336

M
Max Bruckner 已提交
337 338
    item->type = cJSON_Number;

M
Max Bruckner 已提交
339
    input_buffer->offset += (size_t)(after_end - number_c_string);
340
    return true;
K
Kevin Branigan 已提交
341 342
}

343
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
344
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
345 346 347 348 349 350 351 352 353 354 355
{
    if (number >= INT_MAX)
    {
        object->valueint = INT_MAX;
    }
    else if (number <= INT_MIN)
    {
        object->valueint = INT_MIN;
    }
    else
    {
356
        object->valueint = (int)number;
357 358 359 360 361
    }

    return object->valuedouble = number;
}

M
Max Bruckner 已提交
362 363
typedef struct
{
364
    unsigned char *buffer;
365 366
    size_t length;
    size_t offset;
M
Max Bruckner 已提交
367
    size_t depth; /* current nesting depth (for formatted printing) */
368
    cJSON_bool noalloc;
M
Max Bruckner 已提交
369
    cJSON_bool format; /* is this print a formatted print */
370
    internal_hooks hooks;
M
Max Bruckner 已提交
371
} printbuffer;
372

M
Max Bruckner 已提交
373
/* realloc printbuffer if necessary to have at least "needed" bytes more */
374
static unsigned char* ensure(printbuffer * const p, size_t needed)
375
{
376
    unsigned char *newbuffer = NULL;
377 378
    size_t newsize = 0;

379
    if ((p == NULL) || (p->buffer == NULL))
380 381 382 383
    {
        return NULL;
    }

M
Max Bruckner 已提交
384 385 386 387 388 389
    if ((p->length > 0) && (p->offset >= p->length))
    {
        /* make sure that offset is valid */
        return NULL;
    }

390
    if (needed > INT_MAX)
M
Max Bruckner 已提交
391
    {
392
        /* sizes bigger than INT_MAX are currently not supported */
393
        return NULL;
M
Max Bruckner 已提交
394
    }
395

396
    needed += p->offset + 1;
M
Max Bruckner 已提交
397 398 399 400 401
    if (needed <= p->length)
    {
        return p->buffer + p->offset;
    }

402 403 404 405
    if (p->noalloc) {
        return NULL;
    }

406
    /* calculate new buffer size */
M
Max Bruckner 已提交
407
    if (needed > (INT_MAX / 2))
408 409 410 411 412 413 414 415 416 417 418
    {
        /* overflow of int, use INT_MAX if possible */
        if (needed <= INT_MAX)
        {
            newsize = INT_MAX;
        }
        else
        {
            return NULL;
        }
    }
419 420 421 422
    else
    {
        newsize = needed * 2;
    }
423

424
    if (p->hooks.reallocate != NULL)
M
Max Bruckner 已提交
425
    {
M
Max Bruckner 已提交
426
        /* reallocate with realloc if available */
427
        newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
428 429 430 431 432 433 434 435
        if (newbuffer == NULL)
        {
            p->hooks.deallocate(p->buffer);
            p->length = 0;
            p->buffer = NULL;

            return NULL;
        }
M
Max Bruckner 已提交
436
    }
M
Max Bruckner 已提交
437
    else
M
Max Bruckner 已提交
438
    {
M
Max Bruckner 已提交
439
        /* otherwise reallocate manually */
440
        newbuffer = (unsigned char*)p->hooks.allocate(newsize);
M
Max Bruckner 已提交
441 442
        if (!newbuffer)
        {
443
            p->hooks.deallocate(p->buffer);
M
Max Bruckner 已提交
444 445 446 447 448 449 450
            p->length = 0;
            p->buffer = NULL;

            return NULL;
        }
        if (newbuffer)
        {
451
            memcpy(newbuffer, p->buffer, p->offset + 1);
M
Max Bruckner 已提交
452
        }
453
        p->hooks.deallocate(p->buffer);
M
Max Bruckner 已提交
454 455 456 457 458
    }
    p->length = newsize;
    p->buffer = newbuffer;

    return newbuffer + p->offset;
459 460
}

461 462
/* calculate the new length of the string in a printbuffer and update the offset */
static void update_offset(printbuffer * const buffer)
K
Kevin Branigan 已提交
463
{
464 465
    const unsigned char *buffer_pointer = NULL;
    if ((buffer == NULL) || (buffer->buffer == NULL))
M
Max Bruckner 已提交
466
    {
467
        return;
M
Max Bruckner 已提交
468
    }
469
    buffer_pointer = buffer->buffer + buffer->offset;
M
Max Bruckner 已提交
470

471
    buffer->offset += strlen((const char*)buffer_pointer);
472 473 474
}

/* Render the number nicely from the given item into a string. */
475
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
476
{
M
Max Bruckner 已提交
477
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
478
    double d = item->valuedouble;
479
    int length = 0;
480
    size_t i = 0;
481
    unsigned char number_buffer[26]; /* temporary buffer to print the number into */
482
    unsigned char decimal_point = get_decimal_point();
483
    double test;
M
Max Bruckner 已提交
484

M
Max Bruckner 已提交
485
    if (output_buffer == NULL)
M
Max Bruckner 已提交
486
    {
487
        return false;
M
Max Bruckner 已提交
488
    }
M
Max Bruckner 已提交
489

490 491 492
    /* This checks for NaN and Infinity */
    if ((d * 0) != 0)
    {
493
        length = sprintf((char*)number_buffer, "null");
494 495 496
    }
    else
    {
497 498 499 500 501 502 503 504 505
        /* 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 已提交
506
    }
507

508 509
    /* sprintf failed or buffer overrun occured */
    if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
510
    {
511
        return false;
512 513
    }

514
    /* reserve appropriate space in the output */
515
    output_pointer = ensure(output_buffer, (size_t)length);
516 517 518
    if (output_pointer == NULL)
    {
        return false;
519 520
    }

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    /* 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;

537
    return true;
K
Kevin Branigan 已提交
538 539
}

M
Max Bruckner 已提交
540
/* parse 4 digit hexadecimal number */
541
static unsigned parse_hex4(const unsigned char * const input)
542
{
M
Max Bruckner 已提交
543
    unsigned int h = 0;
544
    size_t i = 0;
M
Max Bruckner 已提交
545

546
    for (i = 0; i < 4; i++)
M
Max Bruckner 已提交
547
    {
548
        /* parse digit */
549
        if ((input[i] >= '0') && (input[i] <= '9'))
550
        {
551
            h += (unsigned int) input[i] - '0';
552
        }
553
        else if ((input[i] >= 'A') && (input[i] <= 'F'))
554
        {
555
            h += (unsigned int) 10 + input[i] - 'A';
556
        }
557
        else if ((input[i] >= 'a') && (input[i] <= 'f'))
558
        {
559
            h += (unsigned int) 10 + input[i] - 'a';
560 561 562 563 564
        }
        else /* invalid */
        {
            return 0;
        }
M
Max Bruckner 已提交
565

566 567 568 569 570
        if (i < 3)
        {
            /* shift left to make place for the next nibble */
            h = h << 4;
        }
M
Max Bruckner 已提交
571 572 573
    }

    return h;
574 575
}

576 577
/* converts a UTF-16 literal to UTF-8
 * A literal can be one or two sequences of the form \uXXXX */
578
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 已提交
579
{
580 581 582
    long unsigned int codepoint = 0;
    unsigned int first_code = 0;
    const unsigned char *first_sequence = input_pointer;
583
    unsigned char utf8_length = 0;
584
    unsigned char utf8_position = 0;
585
    unsigned char sequence_length = 0;
586
    unsigned char first_byte_mark = 0;
587 588 589 590 591 592

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

594 595 596
    /* get the first utf16 sequence */
    first_code = parse_hex4(first_sequence + 2);

597
    /* check that the code is valid */
598
    if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
M
Max Bruckner 已提交
599
    {
600
        goto fail;
M
Max Bruckner 已提交
601
    }
M
Max Bruckner 已提交
602

603 604
    /* UTF16 surrogate pair */
    if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
M
Max Bruckner 已提交
605
    {
606 607 608 609 610
        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 已提交
611
        {
612 613
            /* input ends unexpectedly */
            goto fail;
M
Max Bruckner 已提交
614
        }
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

        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 已提交
639
    }
M
Max Bruckner 已提交
640

641 642 643 644 645 646 647 648 649 650 651 652
    /* 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;
653
        first_byte_mark = 0xC0; /* 11000000 */
654 655 656 657 658
    }
    else if (codepoint < 0x10000)
    {
        /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
        utf8_length = 3;
659
        first_byte_mark = 0xE0; /* 11100000 */
660 661 662 663 664
    }
    else if (codepoint <= 0x10FFFF)
    {
        /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
        utf8_length = 4;
665
        first_byte_mark = 0xF0; /* 11110000 */
666 667
    }
    else
M
Max Bruckner 已提交
668
    {
669
        /* invalid unicode codepoint */
670
        goto fail;
M
Max Bruckner 已提交
671 672
    }

673
    /* encode as utf8 */
674 675 676 677 678
    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;
679
    }
680 681 682 683 684 685 686 687
    /* 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);
688
    }
689

690 691 692 693 694 695 696 697 698
    *output_pointer += utf8_length;

    return sequence_length;

fail:
    return 0;
}

/* Parse the input text into an unescaped cinput, and populate item. */
699
static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
700
{
M
Max Bruckner 已提交
701 702
    const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
    const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
703 704 705 706
    unsigned char *output_pointer = NULL;
    unsigned char *output = NULL;

    /* not a string */
M
Max Bruckner 已提交
707
    if (buffer_at_offset(input_buffer)[0] != '\"')
708 709 710 711 712 713 714 715
    {
        goto fail;
    }

    {
        /* calculate approximate size of the output (overestimate) */
        size_t allocation_length = 0;
        size_t skipped_bytes = 0;
716
        while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
717 718 719 720
        {
            /* is escape sequence */
            if (input_end[0] == '\\')
            {
M
Max Bruckner 已提交
721
                if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
722 723 724 725 726 727 728 729 730
                {
                    /* prevent buffer overflow when last input character is a backslash */
                    goto fail;
                }
                skipped_bytes++;
                input_end++;
            }
            input_end++;
        }
731
        if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
732 733 734 735 736
        {
            goto fail; /* string ended unexpectedly */
        }

        /* This is at most how much we need for the output */
M
Max Bruckner 已提交
737
        allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
738
        output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
739 740 741 742 743 744 745
        if (output == NULL)
        {
            goto fail; /* allocation failure */
        }
    }

    output_pointer = output;
M
Max Bruckner 已提交
746
    /* loop through the string literal */
747
    while (input_pointer < input_end)
M
Max Bruckner 已提交
748
    {
749
        if (*input_pointer != '\\')
M
Max Bruckner 已提交
750
        {
751
            *output_pointer++ = *input_pointer++;
M
Max Bruckner 已提交
752 753 754 755
        }
        /* escape sequence */
        else
        {
756
            unsigned char sequence_length = 2;
M
Max Bruckner 已提交
757 758 759 760 761
            if ((input_end - input_pointer) < 1)
            {
                goto fail;
            }

762
            switch (input_pointer[1])
M
Max Bruckner 已提交
763 764
            {
                case 'b':
765
                    *output_pointer++ = '\b';
M
Max Bruckner 已提交
766 767
                    break;
                case 'f':
768
                    *output_pointer++ = '\f';
M
Max Bruckner 已提交
769 770
                    break;
                case 'n':
771
                    *output_pointer++ = '\n';
M
Max Bruckner 已提交
772 773
                    break;
                case 'r':
774
                    *output_pointer++ = '\r';
M
Max Bruckner 已提交
775 776
                    break;
                case 't':
777
                    *output_pointer++ = '\t';
M
Max Bruckner 已提交
778
                    break;
779 780 781
                case '\"':
                case '\\':
                case '/':
782
                    *output_pointer++ = input_pointer[1];
783
                    break;
784 785

                /* UTF-16 literal */
M
Max Bruckner 已提交
786
                case 'u':
787
                    sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
788
                    if (sequence_length == 0)
M
Max Bruckner 已提交
789
                    {
790
                        /* failed to convert UTF16-literal to UTF-8 */
791
                        goto fail;
M
Max Bruckner 已提交
792 793
                    }
                    break;
794

M
Max Bruckner 已提交
795
                default:
796
                    goto fail;
M
Max Bruckner 已提交
797
            }
798
            input_pointer += sequence_length;
M
Max Bruckner 已提交
799 800
        }
    }
801 802 803

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

805
    item->type = cJSON_String;
806
    item->valuestring = (char*)output;
807

M
Max Bruckner 已提交
808 809 810
    input_buffer->offset = (size_t) (input_end - input_buffer->content);
    input_buffer->offset++;

811
    return true;
812 813

fail:
814
    if (output != NULL)
815
    {
816
        input_buffer->hooks.deallocate(output);
817 818
    }

819 820 821 822 823
    if (input_pointer != NULL)
    {
        input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
    }

824
    return false;
K
Kevin Branigan 已提交
825 826 827
}

/* Render the cstring provided to an escaped version that can be printed. */
828
static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
K
Kevin Branigan 已提交
829
{
830 831 832
    const unsigned char *input_pointer = NULL;
    unsigned char *output = NULL;
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
833 834 835
    size_t output_length = 0;
    /* numbers of additional characters needed for escaping */
    size_t escape_characters = 0;
M
Max Bruckner 已提交
836

837
    if (output_buffer == NULL)
M
Max Bruckner 已提交
838
    {
839
        return false;
M
Max Bruckner 已提交
840 841 842
    }

    /* empty string */
843
    if (input == NULL)
M
Max Bruckner 已提交
844
    {
845
        output = ensure(output_buffer, sizeof("\"\""));
846
        if (output == NULL)
M
Max Bruckner 已提交
847
        {
848
            return false;
M
Max Bruckner 已提交
849
        }
850
        strcpy((char*)output, "\"\"");
M
Max Bruckner 已提交
851

852
        return true;
M
Max Bruckner 已提交
853 854 855
    }

    /* set "flag" to 1 if something needs to be escaped */
856
    for (input_pointer = input; *input_pointer; input_pointer++)
M
Max Bruckner 已提交
857
    {
M
Max Bruckner 已提交
858
        switch (*input_pointer)
M
Max Bruckner 已提交
859
        {
M
Max Bruckner 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
            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 已提交
877 878
        }
    }
M
Max Bruckner 已提交
879
    output_length = (size_t)(input_pointer - input) + escape_characters;
M
Max Bruckner 已提交
880

881
    output = ensure(output_buffer, output_length + sizeof("\"\""));
882
    if (output == NULL)
M
Max Bruckner 已提交
883
    {
884
        return false;
M
Max Bruckner 已提交
885 886
    }

M
Max Bruckner 已提交
887 888
    /* no characters have to be escaped */
    if (escape_characters == 0)
M
Max Bruckner 已提交
889
    {
M
Max Bruckner 已提交
890 891 892 893 894
        output[0] = '\"';
        memcpy(output + 1, input, output_length);
        output[output_length + 1] = '\"';
        output[output_length + 2] = '\0';

895
        return true;
M
Max Bruckner 已提交
896 897
    }

M
Max Bruckner 已提交
898 899
    output[0] = '\"';
    output_pointer = output + 1;
M
Max Bruckner 已提交
900
    /* copy the string */
M
Max Bruckner 已提交
901
    for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
M
Max Bruckner 已提交
902
    {
903
        if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
M
Max Bruckner 已提交
904 905
        {
            /* normal character, copy */
M
Max Bruckner 已提交
906
            *output_pointer = *input_pointer;
M
Max Bruckner 已提交
907 908 909 910
        }
        else
        {
            /* character needs to be escaped */
911
            *output_pointer++ = '\\';
M
Max Bruckner 已提交
912
            switch (*input_pointer)
M
Max Bruckner 已提交
913 914
            {
                case '\\':
M
Max Bruckner 已提交
915
                    *output_pointer = '\\';
M
Max Bruckner 已提交
916 917
                    break;
                case '\"':
M
Max Bruckner 已提交
918
                    *output_pointer = '\"';
M
Max Bruckner 已提交
919 920
                    break;
                case '\b':
M
Max Bruckner 已提交
921
                    *output_pointer = 'b';
M
Max Bruckner 已提交
922 923
                    break;
                case '\f':
M
Max Bruckner 已提交
924
                    *output_pointer = 'f';
M
Max Bruckner 已提交
925 926
                    break;
                case '\n':
M
Max Bruckner 已提交
927
                    *output_pointer = 'n';
M
Max Bruckner 已提交
928 929
                    break;
                case '\r':
M
Max Bruckner 已提交
930
                    *output_pointer = 'r';
M
Max Bruckner 已提交
931 932
                    break;
                case '\t':
M
Max Bruckner 已提交
933
                    *output_pointer = 't';
M
Max Bruckner 已提交
934 935 936
                    break;
                default:
                    /* escape and print as unicode codepoint */
M
Max Bruckner 已提交
937 938
                    sprintf((char*)output_pointer, "u%04x", *input_pointer);
                    output_pointer += 4;
M
Max Bruckner 已提交
939 940 941 942
                    break;
            }
        }
    }
M
Max Bruckner 已提交
943 944
    output[output_length + 1] = '\"';
    output[output_length + 2] = '\0';
M
Max Bruckner 已提交
945

946
    return true;
K
Kevin Branigan 已提交
947
}
M
Max Bruckner 已提交
948

M
Max Bruckner 已提交
949
/* Invoke print_string_ptr (which is useful) on an item. */
950
static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
M
Max Bruckner 已提交
951
{
952
    return print_string_ptr((unsigned char*)item->valuestring, p);
M
Max Bruckner 已提交
953
}
K
Kevin Branigan 已提交
954 955

/* Predeclare these prototypes. */
956 957 958 959 960 961
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
K
Kevin Branigan 已提交
962 963

/* Utility to jump whitespace and cr/lf */
M
Max Bruckner 已提交
964
static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
M
Max Bruckner 已提交
965
{
M
Max Bruckner 已提交
966 967 968 969 970
    if ((buffer == NULL) || (buffer->content == NULL))
    {
        return NULL;
    }

M
Max Bruckner 已提交
971 972 973 974 975 976
    while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
    {
       buffer->offset++;
    }

    if (buffer->offset == buffer->length)
M
Max Bruckner 已提交
977
    {
M
Max Bruckner 已提交
978
        buffer->offset--;
M
Max Bruckner 已提交
979 980
    }

M
Max Bruckner 已提交
981
    return buffer;
M
Max Bruckner 已提交
982
}
K
Kevin Branigan 已提交
983

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
{
    if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
    {
        return NULL;
    }

    if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
    {
        buffer->offset += 3;
    }

    return buffer;
}

K
Kevin Branigan 已提交
1000
/* Parse an object - create a new root, and populate. */
1001
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
K
Kevin Branigan 已提交
1002
{
1003
    parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
1004
    cJSON *item = NULL;
1005 1006 1007 1008

    /* reset error position */
    global_error.json = NULL;
    global_error.position = 0;
1009 1010

    if (value == NULL)
M
Max Bruckner 已提交
1011
    {
1012
        goto fail;
M
Max Bruckner 已提交
1013 1014
    }

M
Max Bruckner 已提交
1015 1016 1017
    buffer.content = (const unsigned char*)value;
    buffer.length = strlen((const char*)value) + sizeof("");
    buffer.offset = 0;
1018
    buffer.hooks = global_hooks;
M
Max Bruckner 已提交
1019

1020 1021 1022 1023
    item = cJSON_New_Item(&global_hooks);
    if (item == NULL) /* memory fail */
    {
        goto fail;
M
Max Bruckner 已提交
1024 1025
    }

1026
    if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
M
Max Bruckner 已提交
1027 1028
    {
        /* parse failure. ep is set. */
1029
        goto fail;
M
Max Bruckner 已提交
1030 1031 1032 1033 1034
    }

    /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
    if (require_null_terminated)
    {
1035 1036
        buffer_skip_whitespace(&buffer);
        if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
M
Max Bruckner 已提交
1037
        {
1038
            goto fail;
M
Max Bruckner 已提交
1039 1040 1041 1042
        }
    }
    if (return_parse_end)
    {
1043
        *return_parse_end = (const char*)buffer_at_offset(&buffer);
M
Max Bruckner 已提交
1044 1045
    }

1046
    return item;
1047 1048 1049 1050 1051 1052 1053

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

1054 1055
    if (value != NULL)
    {
1056 1057 1058 1059
        error local_error;
        local_error.json = (const unsigned char*)value;
        local_error.position = 0;

1060 1061
        if (buffer.offset < buffer.length)
        {
1062
            local_error.position = buffer.offset;
1063 1064 1065
        }
        else if (buffer.length > 0)
        {
1066 1067 1068 1069 1070 1071 1072
            local_error.position = buffer.length - 1;
        }

        if (return_parse_end != NULL)
        {
            *return_parse_end = (const char*)local_error.json + local_error.position;
        }
Y
yangfl 已提交
1073

1074
        global_error = local_error;
M
Max Bruckner 已提交
1075 1076
    }

1077
    return NULL;
K
Kevin Branigan 已提交
1078
}
M
Max Bruckner 已提交
1079

1080
/* Default options for cJSON_Parse */
1081
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
M
Max Bruckner 已提交
1082 1083 1084
{
    return cJSON_ParseWithOpts(value, 0, 0);
}
K
Kevin Branigan 已提交
1085

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

1088
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1089 1090 1091 1092 1093 1094 1095
{
    printbuffer buffer[1];
    unsigned char *printed = NULL;

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

    /* create buffer */
1096
    buffer->buffer = (unsigned char*) hooks->allocate(256);
M
Max Bruckner 已提交
1097
    buffer->format = format;
1098
    buffer->hooks = *hooks;
M
Max Bruckner 已提交
1099 1100 1101 1102 1103 1104
    if (buffer->buffer == NULL)
    {
        goto fail;
    }

    /* print the value */
1105
    if (!print_value(item, buffer))
M
Max Bruckner 已提交
1106 1107 1108
    {
        goto fail;
    }
1109
    update_offset(buffer);
M
Max Bruckner 已提交
1110

1111 1112
    /* check if reallocate is available */
    if (hooks->reallocate != NULL)
M
Max Bruckner 已提交
1113
    {
1114 1115 1116 1117 1118
        printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->length);
        buffer->buffer = NULL;
        if (printed == NULL) {
            goto fail;
        }
M
Max Bruckner 已提交
1119
    }
1120 1121 1122 1123 1124 1125 1126
    else /* otherwise copy the JSON over to a new buffer */
    {
        printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
        if (printed == NULL)
        {
            goto fail;
        }
1127
        memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1128
        printed[buffer->offset] = '\0'; /* just to be sure */
M
Max Bruckner 已提交
1129

1130 1131 1132
        /* free the buffer */
        hooks->deallocate(buffer->buffer);
    }
M
Max Bruckner 已提交
1133 1134 1135 1136 1137 1138

    return printed;

fail:
    if (buffer->buffer != NULL)
    {
1139
        hooks->deallocate(buffer->buffer);
M
Max Bruckner 已提交
1140 1141 1142 1143
    }

    if (printed != NULL)
    {
1144
        hooks->deallocate(printed);
M
Max Bruckner 已提交
1145 1146 1147 1148 1149
    }

    return NULL;
}

K
Kevin Branigan 已提交
1150
/* Render a cJSON item/entity/structure to text. */
1151
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
M
Max Bruckner 已提交
1152
{
1153
    return (char*)print(item, true, &global_hooks);
M
Max Bruckner 已提交
1154 1155
}

1156
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
1157
{
1158
    return (char*)print(item, false, &global_hooks);
1159
}
1160

1161
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
1162
{
1163
    printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
M
Max Bruckner 已提交
1164 1165 1166

    if (prebuffer < 0)
    {
M
Max Bruckner 已提交
1167
        return NULL;
M
Max Bruckner 已提交
1168 1169
    }

1170
    p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
1171 1172
    if (!p.buffer)
    {
1173
        return NULL;
1174
    }
M
Max Bruckner 已提交
1175 1176

    p.length = (size_t)prebuffer;
M
Max Bruckner 已提交
1177
    p.offset = 0;
1178
    p.noalloc = false;
M
Max Bruckner 已提交
1179
    p.format = fmt;
1180
    p.hooks = global_hooks;
M
Max Bruckner 已提交
1181

1182
    if (!print_value(item, &p))
1183
    {
1184
        global_hooks.deallocate(p.buffer);
1185 1186 1187 1188
        return NULL;
    }

    return (char*)p.buffer;
1189 1190
}

1191
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
1192
{
1193
    printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
M
Max Bruckner 已提交
1194

1195
    if ((len < 0) || (buf == NULL))
M
Max Bruckner 已提交
1196 1197 1198 1199
    {
        return false;
    }

1200
    p.buffer = (unsigned char*)buf;
M
Max Bruckner 已提交
1201
    p.length = (size_t)len;
1202
    p.offset = 0;
1203
    p.noalloc = true;
M
Max Bruckner 已提交
1204
    p.format = fmt;
1205
    p.hooks = global_hooks;
M
Max Bruckner 已提交
1206

1207
    return print_value(item, &p);
1208
}
K
Kevin Branigan 已提交
1209 1210

/* Parser core - when encountering text, process appropriately. */
1211
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
1212
{
M
Max Bruckner 已提交
1213
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
M
Max Bruckner 已提交
1214
    {
1215
        return false; /* no input */
M
Max Bruckner 已提交
1216 1217 1218
    }

    /* parse the different types of values */
1219
    /* null */
M
Max Bruckner 已提交
1220
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
M
Max Bruckner 已提交
1221 1222
    {
        item->type = cJSON_NULL;
M
Max Bruckner 已提交
1223
        input_buffer->offset += 4;
1224
        return true;
M
Max Bruckner 已提交
1225
    }
1226
    /* false */
M
Max Bruckner 已提交
1227
    if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
M
Max Bruckner 已提交
1228 1229
    {
        item->type = cJSON_False;
M
Max Bruckner 已提交
1230
        input_buffer->offset += 5;
1231
        return true;
M
Max Bruckner 已提交
1232
    }
1233
    /* true */
M
Max Bruckner 已提交
1234
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
M
Max Bruckner 已提交
1235 1236 1237
    {
        item->type = cJSON_True;
        item->valueint = 1;
M
Max Bruckner 已提交
1238
        input_buffer->offset += 4;
1239
        return true;
M
Max Bruckner 已提交
1240
    }
1241
    /* string */
M
Max Bruckner 已提交
1242
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
M
Max Bruckner 已提交
1243
    {
1244
        return parse_string(item, input_buffer);
M
Max Bruckner 已提交
1245
    }
1246
    /* number */
M
Max Bruckner 已提交
1247
    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 已提交
1248
    {
M
Max Bruckner 已提交
1249
        return parse_number(item, input_buffer);
M
Max Bruckner 已提交
1250
    }
1251
    /* array */
M
Max Bruckner 已提交
1252
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
M
Max Bruckner 已提交
1253
    {
1254
        return parse_array(item, input_buffer);
M
Max Bruckner 已提交
1255
    }
1256
    /* object */
M
Max Bruckner 已提交
1257
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
M
Max Bruckner 已提交
1258
    {
1259
        return parse_object(item, input_buffer);
M
Max Bruckner 已提交
1260 1261
    }

1262
    return false;
K
Kevin Branigan 已提交
1263 1264 1265
}

/* Render a value to text. */
1266
static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
K
Kevin Branigan 已提交
1267
{
M
Max Bruckner 已提交
1268
    unsigned char *output = NULL;
M
Max Bruckner 已提交
1269

M
Max Bruckner 已提交
1270
    if ((item == NULL) || (output_buffer == NULL))
M
Max Bruckner 已提交
1271
    {
1272
        return false;
M
Max Bruckner 已提交
1273
    }
M
Max Bruckner 已提交
1274 1275

    switch ((item->type) & 0xFF)
M
Max Bruckner 已提交
1276
    {
M
Max Bruckner 已提交
1277
        case cJSON_NULL:
1278
            output = ensure(output_buffer, 5);
1279
            if (output == NULL)
M
Max Bruckner 已提交
1280
            {
1281
                return false;
M
Max Bruckner 已提交
1282
            }
1283 1284 1285
            strcpy((char*)output, "null");
            return true;

M
Max Bruckner 已提交
1286
        case cJSON_False:
1287
            output = ensure(output_buffer, 6);
1288
            if (output == NULL)
M
Max Bruckner 已提交
1289
            {
1290
                return false;
M
Max Bruckner 已提交
1291
            }
1292 1293 1294
            strcpy((char*)output, "false");
            return true;

M
Max Bruckner 已提交
1295
        case cJSON_True:
1296
            output = ensure(output_buffer, 5);
1297
            if (output == NULL)
M
Max Bruckner 已提交
1298
            {
1299
                return false;
M
Max Bruckner 已提交
1300
            }
1301 1302 1303
            strcpy((char*)output, "true");
            return true;

M
Max Bruckner 已提交
1304
        case cJSON_Number:
1305
            return print_number(item, output_buffer);
1306

M
Max Bruckner 已提交
1307
        case cJSON_Raw:
M
Max Bruckner 已提交
1308
        {
M
Max Bruckner 已提交
1309 1310
            size_t raw_length = 0;
            if (item->valuestring == NULL)
1311
            {
M
Max Bruckner 已提交
1312
                if (!output_buffer->noalloc)
1313
                {
1314
                    output_buffer->hooks.deallocate(output_buffer->buffer);
1315
                }
1316
                return false;
M
Max Bruckner 已提交
1317
            }
1318

1319
            raw_length = strlen(item->valuestring) + sizeof("");
1320
            output = ensure(output_buffer, raw_length);
1321
            if (output == NULL)
M
Max Bruckner 已提交
1322
            {
1323
                return false;
1324
            }
1325 1326
            memcpy(output, item->valuestring, raw_length);
            return true;
M
Max Bruckner 已提交
1327
        }
1328

M
Max Bruckner 已提交
1329
        case cJSON_String:
1330
            return print_string(item, output_buffer);
1331

M
Max Bruckner 已提交
1332
        case cJSON_Array:
1333
            return print_array(item, output_buffer);
1334

M
Max Bruckner 已提交
1335
        case cJSON_Object:
1336
            return print_object(item, output_buffer);
1337

M
Max Bruckner 已提交
1338
        default:
1339
            return false;
M
Max Bruckner 已提交
1340
    }
K
Kevin Branigan 已提交
1341 1342 1343
}

/* Build an array from input text. */
1344
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
1345
{
1346
    cJSON *head = NULL; /* head of the linked list */
1347 1348
    cJSON *current_item = NULL;

1349 1350 1351 1352 1353 1354
    if (input_buffer->depth >= CJSON_NESTING_LIMIT)
    {
        return false; /* to deeply nested */
    }
    input_buffer->depth++;

M
Max Bruckner 已提交
1355
    if (buffer_at_offset(input_buffer)[0] != '[')
M
Max Bruckner 已提交
1356
    {
1357
        /* not an array */
1358
        goto fail;
M
Max Bruckner 已提交
1359
    }
K
Kevin Branigan 已提交
1360

M
Max Bruckner 已提交
1361 1362 1363
    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 已提交
1364
    {
1365
        /* empty array */
1366
        goto success;
M
Max Bruckner 已提交
1367
    }
K
Kevin Branigan 已提交
1368

M
Max Bruckner 已提交
1369 1370 1371 1372 1373 1374 1375
    /* check if we skipped to the end of the buffer */
    if (cannot_access_at_index(input_buffer, 0))
    {
        input_buffer->offset--;
        goto fail;
    }

1376
    /* step back to character in front of the first element */
M
Max Bruckner 已提交
1377
    input_buffer->offset--;
M
Max Bruckner 已提交
1378
    /* loop through the comma separated array elements */
1379
    do
M
Max Bruckner 已提交
1380
    {
1381
        /* allocate next item */
1382
        cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1383
        if (new_item == NULL)
M
Max Bruckner 已提交
1384
        {
1385
            goto fail; /* allocation failure */
M
Max Bruckner 已提交
1386
        }
1387 1388 1389

        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1390
        {
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
            /* 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 已提交
1403 1404
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1405
        if (!parse_value(current_item, input_buffer))
1406 1407
        {
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1408
        }
M
Max Bruckner 已提交
1409
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1410
    }
M
Max Bruckner 已提交
1411
    while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
M
Max Bruckner 已提交
1412

M
Max Bruckner 已提交
1413
    if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
M
Max Bruckner 已提交
1414
    {
1415
        goto fail; /* expected end of array */
M
Max Bruckner 已提交
1416 1417
    }

1418
success:
1419 1420
    input_buffer->depth--;

1421
    item->type = cJSON_Array;
1422
    item->child = head;
1423

M
Max Bruckner 已提交
1424 1425
    input_buffer->offset++;

1426
    return true;
K
Kevin Branigan 已提交
1427

1428
fail:
1429
    if (head != NULL)
1430
    {
1431
        cJSON_Delete(head);
1432 1433
    }

1434
    return false;
K
Kevin Branigan 已提交
1435 1436 1437
}

/* Render an array to text */
1438
static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
K
Kevin Branigan 已提交
1439
{
M
Max Bruckner 已提交
1440
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1441
    size_t length = 0;
M
Max Bruckner 已提交
1442
    cJSON *current_element = item->child;
K
Kevin Branigan 已提交
1443

M
Max Bruckner 已提交
1444
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1445
    {
1446
        return false;
M
Max Bruckner 已提交
1447 1448
    }

M
Max Bruckner 已提交
1449 1450
    /* Compose the output array. */
    /* opening square bracket */
1451
    output_pointer = ensure(output_buffer, 1);
M
Max Bruckner 已提交
1452
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1453
    {
1454
        return false;
M
Max Bruckner 已提交
1455 1456
    }

M
Max Bruckner 已提交
1457 1458
    *output_pointer = '[';
    output_buffer->offset++;
M
Max Bruckner 已提交
1459
    output_buffer->depth++;
M
Max Bruckner 已提交
1460

M
Max Bruckner 已提交
1461
    while (current_element != NULL)
M
Max Bruckner 已提交
1462
    {
1463
        if (!print_value(current_element, output_buffer))
M
Max Bruckner 已提交
1464
        {
1465
            return false;
M
Max Bruckner 已提交
1466
        }
1467
        update_offset(output_buffer);
M
Max Bruckner 已提交
1468
        if (current_element->next)
M
Max Bruckner 已提交
1469
        {
M
Max Bruckner 已提交
1470
            length = (size_t) (output_buffer->format ? 2 : 1);
1471
            output_pointer = ensure(output_buffer, length + 1);
M
Max Bruckner 已提交
1472
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1473
            {
1474
                return false;
M
Max Bruckner 已提交
1475
            }
M
Max Bruckner 已提交
1476
            *output_pointer++ = ',';
M
Max Bruckner 已提交
1477
            if(output_buffer->format)
M
Max Bruckner 已提交
1478
            {
M
Max Bruckner 已提交
1479
                *output_pointer++ = ' ';
M
Max Bruckner 已提交
1480
            }
M
Max Bruckner 已提交
1481 1482
            *output_pointer = '\0';
            output_buffer->offset += length;
M
Max Bruckner 已提交
1483
        }
M
Max Bruckner 已提交
1484
        current_element = current_element->next;
M
Max Bruckner 已提交
1485 1486
    }

1487
    output_pointer = ensure(output_buffer, 2);
M
Max Bruckner 已提交
1488
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1489
    {
1490
        return false;
M
Max Bruckner 已提交
1491
    }
M
Max Bruckner 已提交
1492 1493
    *output_pointer++ = ']';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1494
    output_buffer->depth--;
M
Max Bruckner 已提交
1495

1496
    return true;
K
Kevin Branigan 已提交
1497 1498 1499
}

/* Build an object from the text. */
1500
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
1501
{
1502
    cJSON *head = NULL; /* linked list head */
1503 1504
    cJSON *current_item = NULL;

1505 1506 1507 1508 1509 1510
    if (input_buffer->depth >= CJSON_NESTING_LIMIT)
    {
        return false; /* to deeply nested */
    }
    input_buffer->depth++;

M
Max Bruckner 已提交
1511
    if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
M
Max Bruckner 已提交
1512
    {
1513
        goto fail; /* not an object */
M
Max Bruckner 已提交
1514 1515
    }

M
Max Bruckner 已提交
1516 1517 1518
    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 已提交
1519
    {
1520
        goto success; /* empty object */
M
Max Bruckner 已提交
1521 1522
    }

M
Max Bruckner 已提交
1523 1524 1525 1526 1527 1528 1529
    /* check if we skipped to the end of the buffer */
    if (cannot_access_at_index(input_buffer, 0))
    {
        input_buffer->offset--;
        goto fail;
    }

1530
    /* step back to character in front of the first element */
M
Max Bruckner 已提交
1531
    input_buffer->offset--;
1532 1533
    /* loop through the comma separated array elements */
    do
M
Max Bruckner 已提交
1534
    {
1535
        /* allocate next item */
1536
        cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1537 1538 1539 1540
        if (new_item == NULL)
        {
            goto fail; /* allocation failure */
        }
M
Max Bruckner 已提交
1541

1542 1543
        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1544
        {
1545 1546 1547 1548 1549 1550 1551 1552 1553
            /* 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 已提交
1554 1555
        }

1556
        /* parse the name of the child */
M
Max Bruckner 已提交
1557 1558
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1559
        if (!parse_string(current_item, input_buffer))
M
Max Bruckner 已提交
1560
        {
1561
            goto fail; /* faile to parse name */
M
Max Bruckner 已提交
1562
        }
M
Max Bruckner 已提交
1563
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1564

1565 1566 1567
        /* swap valuestring and string, because we parsed the name */
        current_item->string = current_item->valuestring;
        current_item->valuestring = NULL;
M
Max Bruckner 已提交
1568

M
Max Bruckner 已提交
1569
        if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
M
Max Bruckner 已提交
1570
        {
1571
            goto fail; /* invalid object */
M
Max Bruckner 已提交
1572
        }
1573 1574

        /* parse the value */
M
Max Bruckner 已提交
1575 1576
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1577
        if (!parse_value(current_item, input_buffer))
M
Max Bruckner 已提交
1578
        {
1579
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1580
        }
M
Max Bruckner 已提交
1581
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1582
    }
M
Max Bruckner 已提交
1583
    while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1584

M
Max Bruckner 已提交
1585
    if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
M
Max Bruckner 已提交
1586
    {
1587
        goto fail; /* expected end of object */
M
Max Bruckner 已提交
1588 1589
    }

1590
success:
1591 1592
    input_buffer->depth--;

1593
    item->type = cJSON_Object;
1594
    item->child = head;
1595

M
Max Bruckner 已提交
1596
    input_buffer->offset++;
1597
    return true;
1598 1599

fail:
1600
    if (head != NULL)
1601
    {
1602
        cJSON_Delete(head);
1603 1604
    }

1605
    return false;
K
Kevin Branigan 已提交
1606 1607 1608
}

/* Render an object to text. */
1609
static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
1610
{
M
Max Bruckner 已提交
1611
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1612
    size_t length = 0;
M
Max Bruckner 已提交
1613
    cJSON *current_item = item->child;
M
Max Bruckner 已提交
1614

M
Max Bruckner 已提交
1615
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1616
    {
1617
        return false;
M
Max Bruckner 已提交
1618 1619
    }

M
Max Bruckner 已提交
1620
    /* Compose the output: */
M
Max Bruckner 已提交
1621
    length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
1622
    output_pointer = ensure(output_buffer, length + 1);
M
Max Bruckner 已提交
1623
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1624
    {
1625
        return false;
M
Max Bruckner 已提交
1626 1627
    }

M
Max Bruckner 已提交
1628
    *output_pointer++ = '{';
M
Max Bruckner 已提交
1629
    output_buffer->depth++;
M
Max Bruckner 已提交
1630
    if (output_buffer->format)
M
Max Bruckner 已提交
1631
    {
M
Max Bruckner 已提交
1632
        *output_pointer++ = '\n';
M
Max Bruckner 已提交
1633
    }
M
Max Bruckner 已提交
1634
    output_buffer->offset += length;
M
Max Bruckner 已提交
1635

M
Max Bruckner 已提交
1636
    while (current_item)
M
Max Bruckner 已提交
1637
    {
M
Max Bruckner 已提交
1638
        if (output_buffer->format)
M
Max Bruckner 已提交
1639
        {
M
Max Bruckner 已提交
1640
            size_t i;
1641
            output_pointer = ensure(output_buffer, output_buffer->depth);
M
Max Bruckner 已提交
1642
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1643
            {
1644
                return false;
M
Max Bruckner 已提交
1645
            }
M
Max Bruckner 已提交
1646
            for (i = 0; i < output_buffer->depth; i++)
M
Max Bruckner 已提交
1647
            {
M
Max Bruckner 已提交
1648
                *output_pointer++ = '\t';
M
Max Bruckner 已提交
1649
            }
M
Max Bruckner 已提交
1650
            output_buffer->offset += output_buffer->depth;
M
Max Bruckner 已提交
1651 1652
        }

M
Max Bruckner 已提交
1653
        /* print key */
1654
        if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
M
Max Bruckner 已提交
1655
        {
1656
            return false;
M
Max Bruckner 已提交
1657
        }
M
Max Bruckner 已提交
1658
        update_offset(output_buffer);
M
Max Bruckner 已提交
1659

M
Max Bruckner 已提交
1660
        length = (size_t) (output_buffer->format ? 2 : 1);
1661
        output_pointer = ensure(output_buffer, length);
M
Max Bruckner 已提交
1662
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1663
        {
1664
            return false;
M
Max Bruckner 已提交
1665
        }
M
Max Bruckner 已提交
1666
        *output_pointer++ = ':';
M
Max Bruckner 已提交
1667
        if (output_buffer->format)
M
Max Bruckner 已提交
1668
        {
M
Max Bruckner 已提交
1669
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1670
        }
M
Max Bruckner 已提交
1671
        output_buffer->offset += length;
M
Max Bruckner 已提交
1672

M
Max Bruckner 已提交
1673
        /* print value */
1674
        if (!print_value(current_item, output_buffer))
M
Max Bruckner 已提交
1675
        {
1676
            return false;
M
Max Bruckner 已提交
1677
        }
M
Max Bruckner 已提交
1678
        update_offset(output_buffer);
M
Max Bruckner 已提交
1679

M
Max Bruckner 已提交
1680
        /* print comma if not last */
M
Max Bruckner 已提交
1681
        length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0));
1682
        output_pointer = ensure(output_buffer, length + 1);
M
Max Bruckner 已提交
1683
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1684
        {
1685
            return false;
M
Max Bruckner 已提交
1686
        }
M
Max Bruckner 已提交
1687
        if (current_item->next)
M
Max Bruckner 已提交
1688
        {
M
Max Bruckner 已提交
1689
            *output_pointer++ = ',';
M
Max Bruckner 已提交
1690 1691
        }

M
Max Bruckner 已提交
1692
        if (output_buffer->format)
M
Max Bruckner 已提交
1693
        {
M
Max Bruckner 已提交
1694
            *output_pointer++ = '\n';
M
Max Bruckner 已提交
1695
        }
M
Max Bruckner 已提交
1696 1697
        *output_pointer = '\0';
        output_buffer->offset += length;
M
Max Bruckner 已提交
1698

M
Max Bruckner 已提交
1699
        current_item = current_item->next;
M
Max Bruckner 已提交
1700
    }
M
Max Bruckner 已提交
1701

1702
    output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
M
Max Bruckner 已提交
1703
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1704
    {
1705
        return false;
M
Max Bruckner 已提交
1706
    }
M
Max Bruckner 已提交
1707
    if (output_buffer->format)
M
Max Bruckner 已提交
1708
    {
M
Max Bruckner 已提交
1709
        size_t i;
M
Max Bruckner 已提交
1710
        for (i = 0; i < (output_buffer->depth - 1); i++)
M
Max Bruckner 已提交
1711
        {
M
Max Bruckner 已提交
1712
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1713 1714
        }
    }
M
Max Bruckner 已提交
1715 1716
    *output_pointer++ = '}';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1717
    output_buffer->depth--;
M
Max Bruckner 已提交
1718

1719
    return true;
K
Kevin Branigan 已提交
1720 1721 1722
}

/* Get Array size/item / object item. */
1723
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
M
Max Bruckner 已提交
1724
{
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
    cJSON *child = NULL;
    size_t size = 0;

    if (array == NULL)
    {
        return 0;
    }

    child = array->child;

    while(child != NULL)
M
Max Bruckner 已提交
1736
    {
1737 1738
        size++;
        child = child->next;
M
Max Bruckner 已提交
1739
    }
1740 1741 1742

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

1743
    return (int)size;
M
Max Bruckner 已提交
1744 1745
}

M
Max Bruckner 已提交
1746
static cJSON* get_array_item(const cJSON *array, size_t index)
M
Max Bruckner 已提交
1747
{
M
Max Bruckner 已提交
1748 1749 1750
    cJSON *current_child = NULL;

    if (array == NULL)
M
Max Bruckner 已提交
1751
    {
M
Max Bruckner 已提交
1752
        return NULL;
M
Max Bruckner 已提交
1753 1754
    }

M
Max Bruckner 已提交
1755 1756 1757 1758 1759 1760 1761 1762
    current_child = array->child;
    while ((current_child != NULL) && (index > 0))
    {
        index--;
        current_child = current_child->next;
    }

    return current_child;
M
Max Bruckner 已提交
1763 1764
}

M
Max Bruckner 已提交
1765
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
M
Max Bruckner 已提交
1766
{
M
Max Bruckner 已提交
1767
    if (index < 0)
M
Max Bruckner 已提交
1768
    {
M
Max Bruckner 已提交
1769
        return NULL;
M
Max Bruckner 已提交
1770
    }
M
Max Bruckner 已提交
1771

M
Max Bruckner 已提交
1772
    return get_array_item(array, (size_t)index);
M
Max Bruckner 已提交
1773 1774
}

1775
static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
1776 1777 1778
{
    cJSON *current_element = NULL;

1779
    if ((object == NULL) || (name == NULL))
1780 1781 1782 1783 1784
    {
        return NULL;
    }

    current_element = object->child;
1785
    if (case_sensitive)
1786
    {
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
        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;
        }
1798 1799 1800 1801 1802
    }

    return current_element;
}

1803
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
1804 1805 1806 1807 1808 1809 1810 1811 1812
{
    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);
}

1813
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
M
Max Bruckner 已提交
1814 1815 1816
{
    return cJSON_GetObjectItem(object, string) ? 1 : 0;
}
K
Kevin Branigan 已提交
1817 1818

/* Utility for array list handling. */
M
Max Bruckner 已提交
1819 1820 1821 1822 1823 1824
static void suffix_object(cJSON *prev, cJSON *item)
{
    prev->next = item;
    item->prev = prev;
}

K
Kevin Branigan 已提交
1825
/* Utility for handling references. */
1826
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1827
{
1828 1829
    cJSON *reference = NULL;
    if (item == NULL)
M
Max Bruckner 已提交
1830
    {
1831
        return NULL;
M
Max Bruckner 已提交
1832
    }
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844

    reference = cJSON_New_Item(hooks);
    if (reference == NULL)
    {
        return NULL;
    }

    memcpy(reference, item, sizeof(cJSON));
    reference->string = NULL;
    reference->type |= cJSON_IsReference;
    reference->next = reference->prev = NULL;
    return reference;
M
Max Bruckner 已提交
1845
}
K
Kevin Branigan 已提交
1846 1847

/* Add item to array/object. */
1848
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
M
Max Bruckner 已提交
1849
{
1850 1851 1852
    cJSON *child = NULL;

    if ((item == NULL) || (array == NULL))
M
Max Bruckner 已提交
1853 1854 1855
    {
        return;
    }
1856 1857 1858 1859

    child = array->child;

    if (child == NULL)
M
Max Bruckner 已提交
1860 1861 1862 1863 1864 1865 1866
    {
        /* list is empty, start new one */
        array->child = item;
    }
    else
    {
        /* append to the end */
1867
        while (child->next)
M
Max Bruckner 已提交
1868
        {
1869
            child = child->next;
M
Max Bruckner 已提交
1870
        }
1871
        suffix_object(child, item);
M
Max Bruckner 已提交
1872 1873 1874
    }
}

1875
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
M
Max Bruckner 已提交
1876
{
1877 1878 1879 1880 1881
    if (item == NULL)
    {
        return;
    }

1882
    /* call cJSON_AddItemToObjectCS for code reuse */
1883
    cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
1884 1885
    /* remove cJSON_StringIsConst flag */
    item->type &= ~cJSON_StringIsConst;
M
Max Bruckner 已提交
1886 1887
}

1888
#if defined(__clang__) || (defined(__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
1889 1890
    #pragma GCC diagnostic push
#endif
1891
#ifdef __GNUC__
1892
#pragma GCC diagnostic ignored "-Wcast-qual"
1893
#endif
1894
/* helper function to cast away const */
1895
static void* cast_away_const(const void* string)
1896
{
1897
    return (void*)string;
1898 1899 1900 1901
}
#if defined(__clang__) || (defined(__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic pop
#endif
1902

1903
/* Add an item to an object with constant string as key */
1904
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
1905
{
1906
    if ((item == NULL) || (string == NULL))
1907 1908 1909 1910 1911
    {
        return;
    }
    if (!(item->type & cJSON_StringIsConst) && item->string)
    {
1912
        global_hooks.deallocate(item->string);
1913
    }
1914
    item->string = (char*)cast_away_const(string);
1915 1916 1917 1918
    item->type |= cJSON_StringIsConst;
    cJSON_AddItemToArray(object, item);
}

1919
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
1920
{
1921 1922 1923 1924 1925
    if (array == NULL)
    {
        return;
    }

1926
    cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
1927 1928
}

1929
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
1930
{
1931 1932 1933 1934 1935
    if ((object == NULL) || (string == NULL))
    {
        return;
    }

1936
    cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
1937 1938
}

M
Max Bruckner 已提交
1939
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
1940
{
M
Max Bruckner 已提交
1941
    if ((parent == NULL) || (item == NULL))
1942
    {
1943
        return NULL;
1944
    }
M
Max Bruckner 已提交
1945 1946

    if (item->prev != NULL)
1947 1948
    {
        /* not the first element */
M
Max Bruckner 已提交
1949
        item->prev->next = item->next;
1950
    }
M
Max Bruckner 已提交
1951
    if (item->next != NULL)
1952
    {
M
Max Bruckner 已提交
1953 1954
        /* not the last element */
        item->next->prev = item->prev;
1955
    }
M
Max Bruckner 已提交
1956 1957

    if (item == parent->child)
1958
    {
M
Max Bruckner 已提交
1959 1960
        /* first element */
        parent->child = item->next;
1961 1962
    }
    /* make sure the detached item doesn't point anywhere anymore */
M
Max Bruckner 已提交
1963 1964
    item->prev = NULL;
    item->next = NULL;
1965

M
Max Bruckner 已提交
1966
    return item;
1967
}
M
Max Bruckner 已提交
1968

1969
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
M
Max Bruckner 已提交
1970 1971 1972 1973 1974 1975
{
    if (which < 0)
    {
        return NULL;
    }

M
Max Bruckner 已提交
1976
    return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
M
Max Bruckner 已提交
1977
}
K
Kevin Branigan 已提交
1978

1979
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
1980 1981 1982 1983
{
    cJSON_Delete(cJSON_DetachItemFromArray(array, which));
}

1984
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
1985
{
1986
    cJSON *to_detach = cJSON_GetObjectItem(object, string);
1987

1988 1989 1990 1991 1992 1993
    return cJSON_DetachItemViaPointer(object, to_detach);
}

CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string)
{
    cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
1994

1995
    return cJSON_DetachItemViaPointer(object, to_detach);
1996 1997
}

1998
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
1999 2000 2001
{
    cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}
K
Kevin Branigan 已提交
2002

2003 2004 2005 2006 2007
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
{
    cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
}

K
Kevin Branigan 已提交
2008
/* Replace array/object items with new ones. */
2009
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
2010
{
M
Max Bruckner 已提交
2011 2012 2013
    cJSON *after_inserted = NULL;

    if (which < 0)
2014
    {
M
Max Bruckner 已提交
2015
        return;
2016
    }
M
Max Bruckner 已提交
2017 2018 2019

    after_inserted = get_array_item(array, (size_t)which);
    if (after_inserted == NULL)
2020 2021 2022 2023
    {
        cJSON_AddItemToArray(array, newitem);
        return;
    }
M
Max Bruckner 已提交
2024 2025 2026 2027 2028

    newitem->next = after_inserted;
    newitem->prev = after_inserted->prev;
    after_inserted->prev = newitem;
    if (after_inserted == array->child)
2029 2030 2031 2032 2033 2034 2035 2036 2037
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }
}

M
Max Bruckner 已提交
2038
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
2039
{
2040
    if ((parent == NULL) || (replacement == NULL) || (item == NULL))
2041
    {
M
Max Bruckner 已提交
2042
        return false;
2043
    }
M
Max Bruckner 已提交
2044

M
Max Bruckner 已提交
2045
    if (replacement == item)
2046
    {
M
Max Bruckner 已提交
2047
        return true;
2048
    }
M
Max Bruckner 已提交
2049

M
Max Bruckner 已提交
2050 2051 2052 2053
    replacement->next = item->next;
    replacement->prev = item->prev;

    if (replacement->next != NULL)
2054
    {
M
Max Bruckner 已提交
2055
        replacement->next->prev = replacement;
2056
    }
M
Max Bruckner 已提交
2057
    if (replacement->prev != NULL)
2058
    {
M
Max Bruckner 已提交
2059
        replacement->prev->next = replacement;
2060
    }
M
Max Bruckner 已提交
2061
    if (parent->child == item)
2062
    {
M
Max Bruckner 已提交
2063
        parent->child = replacement;
2064
    }
M
Max Bruckner 已提交
2065

M
Max Bruckner 已提交
2066 2067 2068
    item->next = NULL;
    item->prev = NULL;
    cJSON_Delete(item);
M
Max Bruckner 已提交
2069

M
Max Bruckner 已提交
2070
    return true;
2071
}
M
Max Bruckner 已提交
2072

2073
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
M
Max Bruckner 已提交
2074 2075 2076 2077 2078 2079
{
    if (which < 0)
    {
        return;
    }

M
Max Bruckner 已提交
2080
    cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
M
Max Bruckner 已提交
2081
}
2082

2083 2084
static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
{
2085
    if ((replacement == NULL) || (string == NULL))
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
    {
        return false;
    }

    /* replace the name in the replacement */
    if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
    {
        cJSON_free(replacement->string);
    }
    replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
    replacement->type &= ~cJSON_StringIsConst;

    cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);

    return true;
}

2103
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
2104
{
2105
    replace_item_in_object(object, string, newitem, false);
2106
}
2107

2108 2109
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
{
2110
    replace_item_in_object(object, string, newitem, true);
2111
}
K
Kevin Branigan 已提交
2112 2113

/* Create basic types: */
2114
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
M
Max Bruckner 已提交
2115
{
2116
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2117 2118 2119 2120 2121 2122 2123 2124
    if(item)
    {
        item->type = cJSON_NULL;
    }

    return item;
}

2125
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
M
Max Bruckner 已提交
2126
{
2127
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2128 2129 2130 2131 2132 2133 2134 2135
    if(item)
    {
        item->type = cJSON_True;
    }

    return item;
}

2136
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
M
Max Bruckner 已提交
2137
{
2138
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2139 2140 2141 2142 2143 2144 2145 2146
    if(item)
    {
        item->type = cJSON_False;
    }

    return item;
}

2147
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b)
M
Max Bruckner 已提交
2148
{
2149
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2150 2151 2152 2153 2154 2155 2156 2157
    if(item)
    {
        item->type = b ? cJSON_True : cJSON_False;
    }

    return item;
}

2158
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
M
Max Bruckner 已提交
2159
{
2160
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2161 2162 2163 2164
    if(item)
    {
        item->type = cJSON_Number;
        item->valuedouble = num;
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178

        /* 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 已提交
2179 2180 2181 2182 2183
    }

    return item;
}

2184
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
M
Max Bruckner 已提交
2185
{
2186
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2187 2188 2189
    if(item)
    {
        item->type = cJSON_String;
2190
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
M
Max Bruckner 已提交
2191 2192 2193
        if(!item->valuestring)
        {
            cJSON_Delete(item);
2194
            return NULL;
M
Max Bruckner 已提交
2195 2196 2197 2198 2199 2200
        }
    }

    return item;
}

M
Max Bruckner 已提交
2201 2202 2203 2204 2205 2206
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
{
    cJSON *item = cJSON_New_Item(&global_hooks);
    if (item != NULL)
    {
        item->type = cJSON_String | cJSON_IsReference;
2207
        item->valuestring = (char*)cast_away_const(string);
M
Max Bruckner 已提交
2208 2209 2210 2211 2212
    }

    return item;
}

2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
{
    cJSON *item = cJSON_New_Item(&global_hooks);
    if (item != NULL) {
        item->type = cJSON_Object | cJSON_IsReference;
        item->child = (cJSON*)cast_away_const(child);
    }

    return item;
}

CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
    cJSON *item = cJSON_New_Item(&global_hooks);
    if (item != NULL) {
        item->type = cJSON_Array | cJSON_IsReference;
        item->child = (cJSON*)cast_away_const(child);
    }

    return item;
}

2234
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
J
Jiri Zouhar 已提交
2235
{
2236
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2237 2238 2239
    if(item)
    {
        item->type = cJSON_Raw;
2240
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
M
Max Bruckner 已提交
2241 2242 2243 2244 2245 2246 2247 2248
        if(!item->valuestring)
        {
            cJSON_Delete(item);
            return NULL;
        }
    }

    return item;
J
Jiri Zouhar 已提交
2249 2250
}

2251
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
M
Max Bruckner 已提交
2252
{
2253
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2254 2255 2256 2257 2258 2259 2260 2261
    if(item)
    {
        item->type=cJSON_Array;
    }

    return item;
}

2262
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
M
Max Bruckner 已提交
2263
{
2264
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2265 2266 2267 2268 2269 2270 2271
    if (item)
    {
        item->type = cJSON_Object;
    }

    return item;
}
K
Kevin Branigan 已提交
2272 2273

/* Create Arrays: */
2274
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
M
Max Bruckner 已提交
2275
{
2276
    size_t i = 0;
2277 2278
    cJSON *n = NULL;
    cJSON *p = NULL;
2279 2280
    cJSON *a = NULL;

2281
    if ((count < 0) || (numbers == NULL))
2282 2283 2284 2285 2286 2287
    {
        return NULL;
    }

    a = cJSON_CreateArray();
    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2288 2289 2290 2291 2292
    {
        n = cJSON_CreateNumber(numbers[i]);
        if (!n)
        {
            cJSON_Delete(a);
2293
            return NULL;
M
Max Bruckner 已提交
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2309
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
M
Max Bruckner 已提交
2310
{
2311
    size_t i = 0;
2312 2313
    cJSON *n = NULL;
    cJSON *p = NULL;
2314 2315
    cJSON *a = NULL;

2316
    if ((count < 0) || (numbers == NULL))
2317 2318 2319 2320 2321 2322 2323
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2324
    {
2325
        n = cJSON_CreateNumber((double)numbers[i]);
M
Max Bruckner 已提交
2326 2327 2328
        if(!n)
        {
            cJSON_Delete(a);
2329
            return NULL;
M
Max Bruckner 已提交
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2345
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
2346
{
2347
    size_t i = 0;
2348 2349
    cJSON *n = NULL;
    cJSON *p = NULL;
2350 2351
    cJSON *a = NULL;

2352
    if ((count < 0) || (numbers == NULL))
2353 2354 2355 2356 2357 2358 2359
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0;a && (i < (size_t)count); i++)
2360 2361 2362 2363 2364
    {
        n = cJSON_CreateNumber(numbers[i]);
        if(!n)
        {
            cJSON_Delete(a);
2365
            return NULL;
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2381
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
2382
{
2383
    size_t i = 0;
2384 2385
    cJSON *n = NULL;
    cJSON *p = NULL;
2386 2387
    cJSON *a = NULL;

2388
    if ((count < 0) || (strings == NULL))
2389 2390 2391 2392 2393 2394 2395
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for (i = 0; a && (i < (size_t)count); i++)
2396 2397 2398 2399 2400
    {
        n = cJSON_CreateString(strings[i]);
        if(!n)
        {
            cJSON_Delete(a);
2401
            return NULL;
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p,n);
        }
        p = n;
    }

    return a;
}
2416 2417

/* Duplication */
2418
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2419
{
M
Max Bruckner 已提交
2420
    cJSON *newitem = NULL;
2421 2422
    cJSON *child = NULL;
    cJSON *next = NULL;
M
Max Bruckner 已提交
2423
    cJSON *newchild = NULL;
M
Max Bruckner 已提交
2424 2425 2426 2427

    /* Bail on bad ptr */
    if (!item)
    {
2428
        goto fail;
M
Max Bruckner 已提交
2429 2430
    }
    /* Create new item */
2431
    newitem = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2432 2433
    if (!newitem)
    {
2434
        goto fail;
M
Max Bruckner 已提交
2435 2436 2437 2438 2439 2440 2441
    }
    /* Copy over all vars */
    newitem->type = item->type & (~cJSON_IsReference);
    newitem->valueint = item->valueint;
    newitem->valuedouble = item->valuedouble;
    if (item->valuestring)
    {
2442
        newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
M
Max Bruckner 已提交
2443 2444
        if (!newitem->valuestring)
        {
2445
            goto fail;
M
Max Bruckner 已提交
2446 2447 2448 2449
        }
    }
    if (item->string)
    {
2450
        newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
M
Max Bruckner 已提交
2451 2452
        if (!newitem->string)
        {
2453
            goto fail;
M
Max Bruckner 已提交
2454 2455 2456 2457 2458 2459 2460 2461
        }
    }
    /* If non-recursive, then we're done! */
    if (!recurse)
    {
        return newitem;
    }
    /* Walk the ->next chain for the child. */
2462 2463
    child = item->child;
    while (child != NULL)
M
Max Bruckner 已提交
2464
    {
2465
        newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
M
Max Bruckner 已提交
2466 2467
        if (!newchild)
        {
2468
            goto fail;
M
Max Bruckner 已提交
2469
        }
2470
        if (next != NULL)
M
Max Bruckner 已提交
2471 2472
        {
            /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2473 2474 2475
            next->next = newchild;
            newchild->prev = next;
            next = newchild;
M
Max Bruckner 已提交
2476 2477 2478 2479
        }
        else
        {
            /* Set newitem->child and move to it */
2480 2481
            newitem->child = newchild;
            next = newchild;
M
Max Bruckner 已提交
2482
        }
2483
        child = child->next;
M
Max Bruckner 已提交
2484 2485 2486
    }

    return newitem;
2487 2488 2489 2490 2491 2492 2493 2494

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

    return NULL;
2495
}
2496

2497
CJSON_PUBLIC(void) cJSON_Minify(char *json)
2498
{
2499
    unsigned char *into = (unsigned char*)json;
2500 2501 2502 2503 2504 2505

    if (json == NULL)
    {
        return;
    }

M
Max Bruckner 已提交
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
    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 已提交
2545
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2546 2547 2548 2549
            while (*json && (*json != '\"'))
            {
                if (*json == '\\')
                {
M
Max Bruckner 已提交
2550
                    *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2551
                }
M
Max Bruckner 已提交
2552
                *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2553
            }
M
Max Bruckner 已提交
2554
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2555 2556 2557 2558
        }
        else
        {
            /* All other characters. */
M
Max Bruckner 已提交
2559
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2560 2561 2562 2563 2564
        }
    }

    /* and null-terminate. */
    *into = '\0';
2565
}
2566

2567
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
2568 2569 2570 2571 2572 2573 2574 2575 2576
{
    if (item == NULL)
    {
        return false;
    }

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

2577
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
2578 2579 2580 2581 2582 2583 2584 2585 2586
{
    if (item == NULL)
    {
        return false;
    }

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

2587
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
{
    if (item == NULL)
    {
        return false;
    }

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


2598
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
2599 2600 2601 2602 2603 2604 2605 2606
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & (cJSON_True | cJSON_False)) != 0;
}
2607
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
2608 2609 2610 2611 2612 2613 2614 2615 2616
{
    if (item == NULL)
    {
        return false;
    }

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

2617
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
2618 2619 2620 2621 2622 2623 2624 2625 2626
{
    if (item == NULL)
    {
        return false;
    }

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

2627
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
2628 2629 2630 2631 2632 2633 2634 2635 2636
{
    if (item == NULL)
    {
        return false;
    }

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

2637
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
2638 2639 2640 2641 2642 2643 2644 2645 2646
{
    if (item == NULL)
    {
        return false;
    }

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

2647
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
2648 2649 2650 2651 2652 2653 2654 2655 2656
{
    if (item == NULL)
    {
        return false;
    }

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

2657
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
2658 2659 2660 2661 2662 2663 2664 2665
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Raw;
}
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726

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:
        {
M
Max Bruckner 已提交
2727 2728 2729 2730
            cJSON *a_element = a->child;
            cJSON *b_element = b->child;

            for (; (a_element != NULL) && (b_element != NULL);)
2731 2732 2733 2734 2735
            {
                if (!cJSON_Compare(a_element, b_element, case_sensitive))
                {
                    return false;
                }
M
Max Bruckner 已提交
2736 2737 2738

                a_element = a_element->next;
                b_element = b_element->next;
2739 2740
            }

2741 2742 2743 2744 2745
            /* one of the arrays is longer than the other */
            if (a_element != b_element) {
                return false;
            }

2746 2747 2748 2749 2750 2751
            return true;
        }

        case cJSON_Object:
        {
            cJSON *a_element = NULL;
2752
            cJSON *b_element = NULL;
2753 2754 2755
            cJSON_ArrayForEach(a_element, a)
            {
                /* TODO This has O(n^2) runtime, which is horrible! */
2756
                b_element = get_object_item(b, a_element->string, case_sensitive);
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
                if (b_element == NULL)
                {
                    return false;
                }

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

2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
            /* doing this twice, once on a and b to prevent true comparison if a subset of b
             * TODO: Do this the proper way, this is just a fix for now */
            cJSON_ArrayForEach(b_element, b)
            {
                a_element = get_object_item(a, b_element->string, case_sensitive);
                if (a_element == NULL)
                {
                    return false;
                }

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

2784 2785 2786 2787 2788 2789 2790
            return true;
        }

        default:
            return false;
    }
}
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800

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

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