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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return version;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M
Max Bruckner 已提交
200 201 202 203 204
typedef struct
{
    const unsigned char *content;
    size_t length;
    size_t offset;
205
    size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
206
    internal_hooks hooks;
M
Max Bruckner 已提交
207 208 209 210 211 212 213 214 215 216 217
} parse_buffer;

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

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

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

232
    /* copy the number into a temporary buffer and replace '.' with the decimal point
M
Max Bruckner 已提交
233 234 235
     * 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++)
236
    {
M
Max Bruckner 已提交
237
        switch (buffer_at_offset(input_buffer)[i])
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        {
            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 已提交
253
                number_c_string[i] = buffer_at_offset(input_buffer)[i];
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
                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 已提交
269
    {
270
        return false; /* parse_error */
M
Max Bruckner 已提交
271 272
    }

273
    item->valuedouble = number;
M
Max Bruckner 已提交
274

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

M
Max Bruckner 已提交
289 290
    item->type = cJSON_Number;

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

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

    return object->valuedouble = number;
}

M
Max Bruckner 已提交
314 315
typedef struct
{
316
    unsigned char *buffer;
317 318
    size_t length;
    size_t offset;
M
Max Bruckner 已提交
319
    size_t depth; /* current nesting depth (for formatted printing) */
320
    cJSON_bool noalloc;
M
Max Bruckner 已提交
321
    cJSON_bool format; /* is this print a formatted print */
322
    internal_hooks hooks;
M
Max Bruckner 已提交
323
} printbuffer;
324

M
Max Bruckner 已提交
325
/* realloc printbuffer if necessary to have at least "needed" bytes more */
326
static unsigned char* ensure(printbuffer * const p, size_t needed)
327
{
328
    unsigned char *newbuffer = NULL;
329 330
    size_t newsize = 0;

331
    if ((p == NULL) || (p->buffer == NULL))
332 333 334 335
    {
        return NULL;
    }

M
Max Bruckner 已提交
336 337 338 339 340 341
    if ((p->length > 0) && (p->offset >= p->length))
    {
        /* make sure that offset is valid */
        return NULL;
    }

342
    if (needed > INT_MAX)
M
Max Bruckner 已提交
343
    {
344
        /* sizes bigger than INT_MAX are currently not supported */
345
        return NULL;
M
Max Bruckner 已提交
346
    }
347

348
    needed += p->offset + 1;
M
Max Bruckner 已提交
349 350 351 352 353
    if (needed <= p->length)
    {
        return p->buffer + p->offset;
    }

354 355 356 357
    if (p->noalloc) {
        return NULL;
    }

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

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

            return NULL;
        }
        if (newbuffer)
        {
395
            memcpy(newbuffer, p->buffer, p->offset + 1);
M
Max Bruckner 已提交
396
        }
397
        p->hooks.deallocate(p->buffer);
M
Max Bruckner 已提交
398 399 400 401 402
    }
    p->length = newsize;
    p->buffer = newbuffer;

    return newbuffer + p->offset;
403 404
}

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

415
    buffer->offset += strlen((const char*)buffer_pointer);
416 417 418
}

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

M
Max Bruckner 已提交
429
    if (output_buffer == NULL)
M
Max Bruckner 已提交
430
    {
431
        return false;
M
Max Bruckner 已提交
432
    }
M
Max Bruckner 已提交
433

434 435 436
    /* This checks for NaN and Infinity */
    if ((d * 0) != 0)
    {
437
        length = sprintf((char*)number_buffer, "null");
438 439 440
    }
    else
    {
441 442 443 444 445 446 447 448 449
        /* 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 已提交
450
    }
451

452 453
    /* sprintf failed or buffer overrun occured */
    if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
454
    {
455
        return false;
456 457
    }

458
    /* reserve appropriate space in the output */
459
    output_pointer = ensure(output_buffer, (size_t)length);
460 461 462
    if (output_pointer == NULL)
    {
        return false;
463 464
    }

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    /* 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;

481
    return true;
K
Kevin Branigan 已提交
482 483
}

M
Max Bruckner 已提交
484
/* parse 4 digit hexadecimal number */
485
static unsigned parse_hex4(const unsigned char * const input)
486
{
M
Max Bruckner 已提交
487
    unsigned int h = 0;
488
    size_t i = 0;
M
Max Bruckner 已提交
489

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

510 511 512 513 514
        if (i < 3)
        {
            /* shift left to make place for the next nibble */
            h = h << 4;
        }
M
Max Bruckner 已提交
515 516 517
    }

    return h;
518 519
}

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

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

538 539 540
    /* get the first utf16 sequence */
    first_code = parse_hex4(first_sequence + 2);

541
    /* check that the code is valid */
542
    if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
M
Max Bruckner 已提交
543
    {
544
        goto fail;
M
Max Bruckner 已提交
545
    }
M
Max Bruckner 已提交
546

547 548
    /* UTF16 surrogate pair */
    if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
M
Max Bruckner 已提交
549
    {
550 551 552 553 554
        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 已提交
555
        {
556 557
            /* input ends unexpectedly */
            goto fail;
M
Max Bruckner 已提交
558
        }
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

        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 已提交
583
    }
M
Max Bruckner 已提交
584

585 586 587 588 589 590 591 592 593 594 595 596
    /* 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;
597
        first_byte_mark = 0xC0; /* 11000000 */
598 599 600 601 602
    }
    else if (codepoint < 0x10000)
    {
        /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
        utf8_length = 3;
603
        first_byte_mark = 0xE0; /* 11100000 */
604 605 606 607 608
    }
    else if (codepoint <= 0x10FFFF)
    {
        /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
        utf8_length = 4;
609
        first_byte_mark = 0xF0; /* 11110000 */
610 611
    }
    else
M
Max Bruckner 已提交
612
    {
613
        /* invalid unicode codepoint */
614
        goto fail;
M
Max Bruckner 已提交
615 616
    }

617
    /* encode as utf8 */
618 619 620 621 622
    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;
623
    }
624 625 626 627 628 629 630 631
    /* 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);
632
    }
633

634 635 636 637 638 639 640 641 642
    *output_pointer += utf8_length;

    return sequence_length;

fail:
    return 0;
}

/* Parse the input text into an unescaped cinput, and populate item. */
643
static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
644
{
M
Max Bruckner 已提交
645 646
    const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
    const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
647 648 649 650
    unsigned char *output_pointer = NULL;
    unsigned char *output = NULL;

    /* not a string */
M
Max Bruckner 已提交
651
    if (buffer_at_offset(input_buffer)[0] != '\"')
652 653 654 655 656 657 658 659
    {
        goto fail;
    }

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

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

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

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

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

M
Max Bruckner 已提交
739
                default:
740
                    goto fail;
M
Max Bruckner 已提交
741
            }
742
            input_pointer += sequence_length;
M
Max Bruckner 已提交
743 744
        }
    }
745 746 747

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

749
    item->type = cJSON_String;
750
    item->valuestring = (char*)output;
751

M
Max Bruckner 已提交
752 753 754
    input_buffer->offset = (size_t) (input_end - input_buffer->content);
    input_buffer->offset++;

755
    return true;
756 757

fail:
758
    if (output != NULL)
759
    {
760
        input_buffer->hooks.deallocate(output);
761 762
    }

763 764 765 766 767
    if (input_pointer != NULL)
    {
        input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
    }

768
    return false;
K
Kevin Branigan 已提交
769 770 771
}

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

781
    if (output_buffer == NULL)
M
Max Bruckner 已提交
782
    {
783
        return false;
M
Max Bruckner 已提交
784 785 786
    }

    /* empty string */
787
    if (input == NULL)
M
Max Bruckner 已提交
788
    {
789
        output = ensure(output_buffer, sizeof("\"\""));
790
        if (output == NULL)
M
Max Bruckner 已提交
791
        {
792
            return false;
M
Max Bruckner 已提交
793
        }
794
        strcpy((char*)output, "\"\"");
M
Max Bruckner 已提交
795

796
        return true;
M
Max Bruckner 已提交
797 798 799
    }

    /* set "flag" to 1 if something needs to be escaped */
800
    for (input_pointer = input; *input_pointer; input_pointer++)
M
Max Bruckner 已提交
801
    {
M
Max Bruckner 已提交
802
        switch (*input_pointer)
M
Max Bruckner 已提交
803
        {
M
Max Bruckner 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
            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 已提交
821 822
        }
    }
M
Max Bruckner 已提交
823
    output_length = (size_t)(input_pointer - input) + escape_characters;
M
Max Bruckner 已提交
824

825
    output = ensure(output_buffer, output_length + sizeof("\"\""));
826
    if (output == NULL)
M
Max Bruckner 已提交
827
    {
828
        return false;
M
Max Bruckner 已提交
829 830
    }

M
Max Bruckner 已提交
831 832
    /* no characters have to be escaped */
    if (escape_characters == 0)
M
Max Bruckner 已提交
833
    {
M
Max Bruckner 已提交
834 835 836 837 838
        output[0] = '\"';
        memcpy(output + 1, input, output_length);
        output[output_length + 1] = '\"';
        output[output_length + 2] = '\0';

839
        return true;
M
Max Bruckner 已提交
840 841
    }

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

890
    return true;
K
Kevin Branigan 已提交
891
}
M
Max Bruckner 已提交
892

M
Max Bruckner 已提交
893
/* Invoke print_string_ptr (which is useful) on an item. */
894
static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
M
Max Bruckner 已提交
895
{
896
    return print_string_ptr((unsigned char*)item->valuestring, p);
M
Max Bruckner 已提交
897
}
K
Kevin Branigan 已提交
898 899

/* Predeclare these prototypes. */
900 901 902 903 904 905
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 已提交
906 907

/* Utility to jump whitespace and cr/lf */
M
Max Bruckner 已提交
908
static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
M
Max Bruckner 已提交
909
{
M
Max Bruckner 已提交
910 911 912 913 914
    if ((buffer == NULL) || (buffer->content == NULL))
    {
        return NULL;
    }

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

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

M
Max Bruckner 已提交
925
    return buffer;
M
Max Bruckner 已提交
926
}
K
Kevin Branigan 已提交
927 928

/* Parse an object - create a new root, and populate. */
929
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
K
Kevin Branigan 已提交
930
{
931
    parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
932
    cJSON *item = NULL;
933 934 935 936

    /* reset error position */
    global_error.json = NULL;
    global_error.position = 0;
937 938

    if (value == NULL)
M
Max Bruckner 已提交
939
    {
940
        goto fail;
M
Max Bruckner 已提交
941 942
    }

M
Max Bruckner 已提交
943 944 945
    buffer.content = (const unsigned char*)value;
    buffer.length = strlen((const char*)value) + sizeof("");
    buffer.offset = 0;
946
    buffer.hooks = global_hooks;
M
Max Bruckner 已提交
947

948 949 950 951
    item = cJSON_New_Item(&global_hooks);
    if (item == NULL) /* memory fail */
    {
        goto fail;
M
Max Bruckner 已提交
952 953
    }

954
    if (!parse_value(item, buffer_skip_whitespace(&buffer)))
M
Max Bruckner 已提交
955 956
    {
        /* parse failure. ep is set. */
957
        goto fail;
M
Max Bruckner 已提交
958 959 960 961 962
    }

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

974
    return item;
975 976 977 978 979 980 981

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

982 983
    if (value != NULL)
    {
984 985 986 987
        error local_error;
        local_error.json = (const unsigned char*)value;
        local_error.position = 0;

988 989
        if (buffer.offset < buffer.length)
        {
990
            local_error.position = buffer.offset;
991 992 993
        }
        else if (buffer.length > 0)
        {
994 995 996 997 998 999 1000 1001 1002 1003
            local_error.position = buffer.length - 1;
        }

        if (return_parse_end != NULL)
        {
            *return_parse_end = (const char*)local_error.json + local_error.position;
        }
        else
        {
            global_error = local_error;
1004
        }
M
Max Bruckner 已提交
1005 1006
    }

1007
    return NULL;
K
Kevin Branigan 已提交
1008
}
M
Max Bruckner 已提交
1009

1010
/* Default options for cJSON_Parse */
1011
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
M
Max Bruckner 已提交
1012 1013 1014
{
    return cJSON_ParseWithOpts(value, 0, 0);
}
K
Kevin Branigan 已提交
1015

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

1018
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1019 1020 1021 1022 1023 1024 1025
{
    printbuffer buffer[1];
    unsigned char *printed = NULL;

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

    /* create buffer */
1026
    buffer->buffer = (unsigned char*) hooks->allocate(256);
M
Max Bruckner 已提交
1027
    buffer->format = format;
1028
    buffer->hooks = *hooks;
M
Max Bruckner 已提交
1029 1030 1031 1032 1033 1034
    if (buffer->buffer == NULL)
    {
        goto fail;
    }

    /* print the value */
1035
    if (!print_value(item, buffer))
M
Max Bruckner 已提交
1036 1037 1038
    {
        goto fail;
    }
1039
    update_offset(buffer);
M
Max Bruckner 已提交
1040

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

1060 1061 1062
        /* free the buffer */
        hooks->deallocate(buffer->buffer);
    }
M
Max Bruckner 已提交
1063 1064 1065 1066 1067 1068

    return printed;

fail:
    if (buffer->buffer != NULL)
    {
1069
        hooks->deallocate(buffer->buffer);
M
Max Bruckner 已提交
1070 1071 1072 1073
    }

    if (printed != NULL)
    {
1074
        hooks->deallocate(printed);
M
Max Bruckner 已提交
1075 1076 1077 1078 1079
    }

    return NULL;
}

K
Kevin Branigan 已提交
1080
/* Render a cJSON item/entity/structure to text. */
1081
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
M
Max Bruckner 已提交
1082
{
1083
    return (char*)print(item, true, &global_hooks);
M
Max Bruckner 已提交
1084 1085
}

1086
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
1087
{
1088
    return (char*)print(item, false, &global_hooks);
1089
}
1090

1091
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
1092
{
1093
    printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
M
Max Bruckner 已提交
1094 1095 1096

    if (prebuffer < 0)
    {
M
Max Bruckner 已提交
1097
        return NULL;
M
Max Bruckner 已提交
1098 1099
    }

1100
    p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
1101 1102
    if (!p.buffer)
    {
1103
        return NULL;
1104
    }
M
Max Bruckner 已提交
1105 1106

    p.length = (size_t)prebuffer;
M
Max Bruckner 已提交
1107
    p.offset = 0;
1108
    p.noalloc = false;
M
Max Bruckner 已提交
1109
    p.format = fmt;
1110
    p.hooks = global_hooks;
M
Max Bruckner 已提交
1111

1112
    if (!print_value(item, &p))
1113 1114 1115 1116 1117
    {
        return NULL;
    }

    return (char*)p.buffer;
1118 1119
}

1120
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
1121
{
1122
    printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
M
Max Bruckner 已提交
1123 1124 1125 1126 1127 1128

    if (len < 0)
    {
        return false;
    }

1129
    p.buffer = (unsigned char*)buf;
M
Max Bruckner 已提交
1130
    p.length = (size_t)len;
1131
    p.offset = 0;
1132
    p.noalloc = true;
M
Max Bruckner 已提交
1133
    p.format = fmt;
1134
    p.hooks = global_hooks;
M
Max Bruckner 已提交
1135

1136
    return print_value(item, &p);
1137
}
K
Kevin Branigan 已提交
1138 1139

/* Parser core - when encountering text, process appropriately. */
1140
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
1141
{
M
Max Bruckner 已提交
1142
    if ((input_buffer == NULL) || (input_buffer->content == NULL))
M
Max Bruckner 已提交
1143
    {
1144
        return false; /* no input */
M
Max Bruckner 已提交
1145 1146 1147
    }

    /* parse the different types of values */
1148
    /* null */
M
Max Bruckner 已提交
1149
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
M
Max Bruckner 已提交
1150 1151
    {
        item->type = cJSON_NULL;
M
Max Bruckner 已提交
1152
        input_buffer->offset += 4;
1153
        return true;
M
Max Bruckner 已提交
1154
    }
1155
    /* false */
M
Max Bruckner 已提交
1156
    if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
M
Max Bruckner 已提交
1157 1158
    {
        item->type = cJSON_False;
M
Max Bruckner 已提交
1159
        input_buffer->offset += 5;
1160
        return true;
M
Max Bruckner 已提交
1161
    }
1162
    /* true */
M
Max Bruckner 已提交
1163
    if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
M
Max Bruckner 已提交
1164 1165 1166
    {
        item->type = cJSON_True;
        item->valueint = 1;
M
Max Bruckner 已提交
1167
        input_buffer->offset += 4;
1168
        return true;
M
Max Bruckner 已提交
1169
    }
1170
    /* string */
M
Max Bruckner 已提交
1171
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
M
Max Bruckner 已提交
1172
    {
1173
        return parse_string(item, input_buffer);
M
Max Bruckner 已提交
1174
    }
1175
    /* number */
M
Max Bruckner 已提交
1176
    if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
M
Max Bruckner 已提交
1177
    {
M
Max Bruckner 已提交
1178
        return parse_number(item, input_buffer);
M
Max Bruckner 已提交
1179
    }
1180
    /* array */
M
Max Bruckner 已提交
1181
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
M
Max Bruckner 已提交
1182
    {
1183
        return parse_array(item, input_buffer);
M
Max Bruckner 已提交
1184
    }
1185
    /* object */
M
Max Bruckner 已提交
1186
    if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
M
Max Bruckner 已提交
1187
    {
1188
        return parse_object(item, input_buffer);
M
Max Bruckner 已提交
1189 1190
    }

M
Max Bruckner 已提交
1191

1192
    return false;
K
Kevin Branigan 已提交
1193 1194 1195
}

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

M
Max Bruckner 已提交
1200
    if ((item == NULL) || (output_buffer == NULL))
M
Max Bruckner 已提交
1201
    {
1202
        return false;
M
Max Bruckner 已提交
1203
    }
M
Max Bruckner 已提交
1204 1205

    switch ((item->type) & 0xFF)
M
Max Bruckner 已提交
1206
    {
M
Max Bruckner 已提交
1207
        case cJSON_NULL:
1208
            output = ensure(output_buffer, 5);
1209
            if (output == NULL)
M
Max Bruckner 已提交
1210
            {
1211
                return false;
M
Max Bruckner 已提交
1212
            }
1213 1214 1215
            strcpy((char*)output, "null");
            return true;

M
Max Bruckner 已提交
1216
        case cJSON_False:
1217
            output = ensure(output_buffer, 6);
1218
            if (output == NULL)
M
Max Bruckner 已提交
1219
            {
1220
                return false;
M
Max Bruckner 已提交
1221
            }
1222 1223 1224
            strcpy((char*)output, "false");
            return true;

M
Max Bruckner 已提交
1225
        case cJSON_True:
1226
            output = ensure(output_buffer, 5);
1227
            if (output == NULL)
M
Max Bruckner 已提交
1228
            {
1229
                return false;
M
Max Bruckner 已提交
1230
            }
1231 1232 1233
            strcpy((char*)output, "true");
            return true;

M
Max Bruckner 已提交
1234
        case cJSON_Number:
1235
            return print_number(item, output_buffer);
1236

M
Max Bruckner 已提交
1237
        case cJSON_Raw:
M
Max Bruckner 已提交
1238
        {
M
Max Bruckner 已提交
1239 1240
            size_t raw_length = 0;
            if (item->valuestring == NULL)
1241
            {
M
Max Bruckner 已提交
1242
                if (!output_buffer->noalloc)
1243
                {
1244
                    output_buffer->hooks.deallocate(output_buffer->buffer);
1245
                }
1246
                return false;
M
Max Bruckner 已提交
1247
            }
1248

1249
            raw_length = strlen(item->valuestring) + sizeof("");
1250
            output = ensure(output_buffer, raw_length);
1251
            if (output == NULL)
M
Max Bruckner 已提交
1252
            {
1253
                return false;
1254
            }
1255 1256
            memcpy(output, item->valuestring, raw_length);
            return true;
M
Max Bruckner 已提交
1257
        }
1258

M
Max Bruckner 已提交
1259
        case cJSON_String:
1260
            return print_string(item, output_buffer);
1261

M
Max Bruckner 已提交
1262
        case cJSON_Array:
1263
            return print_array(item, output_buffer);
1264

M
Max Bruckner 已提交
1265
        case cJSON_Object:
1266
            return print_object(item, output_buffer);
1267

M
Max Bruckner 已提交
1268
        default:
1269
            return false;
M
Max Bruckner 已提交
1270
    }
K
Kevin Branigan 已提交
1271 1272 1273
}

/* Build an array from input text. */
1274
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
1275
{
1276
    cJSON *head = NULL; /* head of the linked list */
1277 1278
    cJSON *current_item = NULL;

1279 1280 1281 1282 1283 1284
    if (input_buffer->depth >= CJSON_NESTING_LIMIT)
    {
        return false; /* to deeply nested */
    }
    input_buffer->depth++;

M
Max Bruckner 已提交
1285
    if (buffer_at_offset(input_buffer)[0] != '[')
M
Max Bruckner 已提交
1286
    {
1287
        /* not an array */
1288
        goto fail;
M
Max Bruckner 已提交
1289
    }
K
Kevin Branigan 已提交
1290

M
Max Bruckner 已提交
1291 1292 1293
    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 已提交
1294
    {
1295
        /* empty array */
1296
        goto success;
M
Max Bruckner 已提交
1297
    }
K
Kevin Branigan 已提交
1298

M
Max Bruckner 已提交
1299 1300 1301 1302 1303 1304 1305
    /* check if we skipped to the end of the buffer */
    if (cannot_access_at_index(input_buffer, 0))
    {
        input_buffer->offset--;
        goto fail;
    }

1306
    /* step back to character in front of the first element */
M
Max Bruckner 已提交
1307
    input_buffer->offset--;
M
Max Bruckner 已提交
1308
    /* loop through the comma separated array elements */
1309
    do
M
Max Bruckner 已提交
1310
    {
1311
        /* allocate next item */
1312
        cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1313
        if (new_item == NULL)
M
Max Bruckner 已提交
1314
        {
1315
            goto fail; /* allocation failure */
M
Max Bruckner 已提交
1316
        }
1317 1318 1319

        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1320
        {
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
            /* 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 已提交
1333 1334
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1335
        if (!parse_value(current_item, input_buffer))
1336 1337
        {
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1338
        }
M
Max Bruckner 已提交
1339
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1340
    }
M
Max Bruckner 已提交
1341
    while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
M
Max Bruckner 已提交
1342

M
Max Bruckner 已提交
1343
    if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
M
Max Bruckner 已提交
1344
    {
1345
        goto fail; /* expected end of array */
M
Max Bruckner 已提交
1346 1347
    }

1348
success:
1349 1350
    input_buffer->depth--;

1351
    item->type = cJSON_Array;
1352
    item->child = head;
1353

M
Max Bruckner 已提交
1354 1355
    input_buffer->offset++;

1356
    return true;
K
Kevin Branigan 已提交
1357

1358
fail:
1359
    if (head != NULL)
1360
    {
1361
        cJSON_Delete(head);
1362 1363
    }

1364
    return false;
K
Kevin Branigan 已提交
1365 1366 1367
}

/* Render an array to text */
1368
static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
K
Kevin Branigan 已提交
1369
{
M
Max Bruckner 已提交
1370
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1371
    size_t length = 0;
M
Max Bruckner 已提交
1372
    cJSON *current_element = item->child;
K
Kevin Branigan 已提交
1373

M
Max Bruckner 已提交
1374
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1375
    {
1376
        return false;
M
Max Bruckner 已提交
1377 1378
    }

M
Max Bruckner 已提交
1379 1380
    /* Compose the output array. */
    /* opening square bracket */
1381
    output_pointer = ensure(output_buffer, 1);
M
Max Bruckner 已提交
1382
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1383
    {
1384
        return false;
M
Max Bruckner 已提交
1385 1386
    }

M
Max Bruckner 已提交
1387 1388
    *output_pointer = '[';
    output_buffer->offset++;
M
Max Bruckner 已提交
1389
    output_buffer->depth++;
M
Max Bruckner 已提交
1390

M
Max Bruckner 已提交
1391
    while (current_element != NULL)
M
Max Bruckner 已提交
1392
    {
1393
        if (!print_value(current_element, output_buffer))
M
Max Bruckner 已提交
1394
        {
1395
            return false;
M
Max Bruckner 已提交
1396
        }
1397
        update_offset(output_buffer);
M
Max Bruckner 已提交
1398
        if (current_element->next)
M
Max Bruckner 已提交
1399
        {
M
Max Bruckner 已提交
1400
            length = (size_t) (output_buffer->format ? 2 : 1);
1401
            output_pointer = ensure(output_buffer, length + 1);
M
Max Bruckner 已提交
1402
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1403
            {
1404
                return false;
M
Max Bruckner 已提交
1405
            }
M
Max Bruckner 已提交
1406
            *output_pointer++ = ',';
M
Max Bruckner 已提交
1407
            if(output_buffer->format)
M
Max Bruckner 已提交
1408
            {
M
Max Bruckner 已提交
1409
                *output_pointer++ = ' ';
M
Max Bruckner 已提交
1410
            }
M
Max Bruckner 已提交
1411 1412
            *output_pointer = '\0';
            output_buffer->offset += length;
M
Max Bruckner 已提交
1413
        }
M
Max Bruckner 已提交
1414
        current_element = current_element->next;
M
Max Bruckner 已提交
1415 1416
    }

1417
    output_pointer = ensure(output_buffer, 2);
M
Max Bruckner 已提交
1418
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1419
    {
1420
        return false;
M
Max Bruckner 已提交
1421
    }
M
Max Bruckner 已提交
1422 1423
    *output_pointer++ = ']';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1424
    output_buffer->depth--;
M
Max Bruckner 已提交
1425

1426
    return true;
K
Kevin Branigan 已提交
1427 1428 1429
}

/* Build an object from the text. */
1430
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
K
Kevin Branigan 已提交
1431
{
1432
    cJSON *head = NULL; /* linked list head */
1433 1434
    cJSON *current_item = NULL;

1435 1436 1437 1438 1439 1440
    if (input_buffer->depth >= CJSON_NESTING_LIMIT)
    {
        return false; /* to deeply nested */
    }
    input_buffer->depth++;

M
Max Bruckner 已提交
1441
    if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
M
Max Bruckner 已提交
1442
    {
1443
        goto fail; /* not an object */
M
Max Bruckner 已提交
1444 1445
    }

M
Max Bruckner 已提交
1446 1447 1448
    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 已提交
1449
    {
1450
        goto success; /* empty object */
M
Max Bruckner 已提交
1451 1452
    }

M
Max Bruckner 已提交
1453 1454 1455 1456 1457 1458 1459
    /* check if we skipped to the end of the buffer */
    if (cannot_access_at_index(input_buffer, 0))
    {
        input_buffer->offset--;
        goto fail;
    }

1460
    /* step back to character in front of the first element */
M
Max Bruckner 已提交
1461
    input_buffer->offset--;
1462 1463
    /* loop through the comma separated array elements */
    do
M
Max Bruckner 已提交
1464
    {
1465
        /* allocate next item */
1466
        cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1467 1468 1469 1470
        if (new_item == NULL)
        {
            goto fail; /* allocation failure */
        }
M
Max Bruckner 已提交
1471

1472 1473
        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1474
        {
1475 1476 1477 1478 1479 1480 1481 1482 1483
            /* 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 已提交
1484 1485
        }

1486
        /* parse the name of the child */
M
Max Bruckner 已提交
1487 1488
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1489
        if (!parse_string(current_item, input_buffer))
M
Max Bruckner 已提交
1490
        {
1491
            goto fail; /* faile to parse name */
M
Max Bruckner 已提交
1492
        }
M
Max Bruckner 已提交
1493
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1494

1495 1496 1497
        /* swap valuestring and string, because we parsed the name */
        current_item->string = current_item->valuestring;
        current_item->valuestring = NULL;
M
Max Bruckner 已提交
1498

M
Max Bruckner 已提交
1499
        if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
M
Max Bruckner 已提交
1500
        {
1501
            goto fail; /* invalid object */
M
Max Bruckner 已提交
1502
        }
1503 1504

        /* parse the value */
M
Max Bruckner 已提交
1505 1506
        input_buffer->offset++;
        buffer_skip_whitespace(input_buffer);
1507
        if (!parse_value(current_item, input_buffer))
M
Max Bruckner 已提交
1508
        {
1509
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1510
        }
M
Max Bruckner 已提交
1511
        buffer_skip_whitespace(input_buffer);
M
Max Bruckner 已提交
1512
    }
M
Max Bruckner 已提交
1513
    while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1514

M
Max Bruckner 已提交
1515
    if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
M
Max Bruckner 已提交
1516
    {
1517
        goto fail; /* expected end of object */
M
Max Bruckner 已提交
1518 1519
    }

1520
success:
1521 1522
    input_buffer->depth--;

1523
    item->type = cJSON_Object;
1524
    item->child = head;
1525

M
Max Bruckner 已提交
1526
    input_buffer->offset++;
1527
    return true;
1528 1529

fail:
1530
    if (head != NULL)
1531
    {
1532
        cJSON_Delete(head);
1533 1534
    }

1535
    return false;
K
Kevin Branigan 已提交
1536 1537 1538
}

/* Render an object to text. */
1539
static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
1540
{
M
Max Bruckner 已提交
1541
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1542
    size_t length = 0;
M
Max Bruckner 已提交
1543
    cJSON *current_item = item->child;
M
Max Bruckner 已提交
1544

M
Max Bruckner 已提交
1545
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1546
    {
1547
        return false;
M
Max Bruckner 已提交
1548 1549
    }

M
Max Bruckner 已提交
1550
    /* Compose the output: */
M
Max Bruckner 已提交
1551
    length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
1552
    output_pointer = ensure(output_buffer, length + 1);
M
Max Bruckner 已提交
1553
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1554
    {
1555
        return false;
M
Max Bruckner 已提交
1556 1557
    }

M
Max Bruckner 已提交
1558
    *output_pointer++ = '{';
M
Max Bruckner 已提交
1559
    output_buffer->depth++;
M
Max Bruckner 已提交
1560
    if (output_buffer->format)
M
Max Bruckner 已提交
1561
    {
M
Max Bruckner 已提交
1562
        *output_pointer++ = '\n';
M
Max Bruckner 已提交
1563
    }
M
Max Bruckner 已提交
1564
    output_buffer->offset += length;
M
Max Bruckner 已提交
1565

M
Max Bruckner 已提交
1566
    while (current_item)
M
Max Bruckner 已提交
1567
    {
M
Max Bruckner 已提交
1568
        if (output_buffer->format)
M
Max Bruckner 已提交
1569
        {
M
Max Bruckner 已提交
1570
            size_t i;
1571
            output_pointer = ensure(output_buffer, output_buffer->depth);
M
Max Bruckner 已提交
1572
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1573
            {
1574
                return false;
M
Max Bruckner 已提交
1575
            }
M
Max Bruckner 已提交
1576
            for (i = 0; i < output_buffer->depth; i++)
M
Max Bruckner 已提交
1577
            {
M
Max Bruckner 已提交
1578
                *output_pointer++ = '\t';
M
Max Bruckner 已提交
1579
            }
M
Max Bruckner 已提交
1580
            output_buffer->offset += output_buffer->depth;
M
Max Bruckner 已提交
1581 1582
        }

M
Max Bruckner 已提交
1583
        /* print key */
1584
        if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
M
Max Bruckner 已提交
1585
        {
1586
            return false;
M
Max Bruckner 已提交
1587
        }
M
Max Bruckner 已提交
1588
        update_offset(output_buffer);
M
Max Bruckner 已提交
1589

M
Max Bruckner 已提交
1590
        length = (size_t) (output_buffer->format ? 2 : 1);
1591
        output_pointer = ensure(output_buffer, length);
M
Max Bruckner 已提交
1592
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1593
        {
1594
            return false;
M
Max Bruckner 已提交
1595
        }
M
Max Bruckner 已提交
1596
        *output_pointer++ = ':';
M
Max Bruckner 已提交
1597
        if (output_buffer->format)
M
Max Bruckner 已提交
1598
        {
M
Max Bruckner 已提交
1599
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1600
        }
M
Max Bruckner 已提交
1601
        output_buffer->offset += length;
M
Max Bruckner 已提交
1602

M
Max Bruckner 已提交
1603
        /* print value */
1604
        if (!print_value(current_item, output_buffer))
M
Max Bruckner 已提交
1605
        {
1606
            return false;
M
Max Bruckner 已提交
1607
        }
M
Max Bruckner 已提交
1608
        update_offset(output_buffer);
M
Max Bruckner 已提交
1609

M
Max Bruckner 已提交
1610
        /* print comma if not last */
M
Max Bruckner 已提交
1611
        length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0));
1612
        output_pointer = ensure(output_buffer, length + 1);
M
Max Bruckner 已提交
1613
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1614
        {
1615
            return false;
M
Max Bruckner 已提交
1616
        }
M
Max Bruckner 已提交
1617
        if (current_item->next)
M
Max Bruckner 已提交
1618
        {
M
Max Bruckner 已提交
1619
            *output_pointer++ = ',';
M
Max Bruckner 已提交
1620 1621
        }

M
Max Bruckner 已提交
1622
        if (output_buffer->format)
M
Max Bruckner 已提交
1623
        {
M
Max Bruckner 已提交
1624
            *output_pointer++ = '\n';
M
Max Bruckner 已提交
1625
        }
M
Max Bruckner 已提交
1626 1627
        *output_pointer = '\0';
        output_buffer->offset += length;
M
Max Bruckner 已提交
1628

M
Max Bruckner 已提交
1629
        current_item = current_item->next;
M
Max Bruckner 已提交
1630
    }
M
Max Bruckner 已提交
1631

1632
    output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
M
Max Bruckner 已提交
1633
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1634
    {
1635
        return false;
M
Max Bruckner 已提交
1636
    }
M
Max Bruckner 已提交
1637
    if (output_buffer->format)
M
Max Bruckner 已提交
1638
    {
M
Max Bruckner 已提交
1639
        size_t i;
M
Max Bruckner 已提交
1640
        for (i = 0; i < (output_buffer->depth - 1); i++)
M
Max Bruckner 已提交
1641
        {
M
Max Bruckner 已提交
1642
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1643 1644
        }
    }
M
Max Bruckner 已提交
1645 1646
    *output_pointer++ = '}';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1647
    output_buffer->depth--;
M
Max Bruckner 已提交
1648

1649
    return true;
K
Kevin Branigan 已提交
1650 1651 1652
}

/* Get Array size/item / object item. */
1653
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
M
Max Bruckner 已提交
1654 1655
{
    cJSON *c = array->child;
1656
    size_t i = 0;
M
Max Bruckner 已提交
1657 1658 1659 1660 1661
    while(c)
    {
        i++;
        c = c->next;
    }
1662 1663 1664

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

M
Max Bruckner 已提交
1665
    return (int)i;
M
Max Bruckner 已提交
1666 1667
}

M
Max Bruckner 已提交
1668
static cJSON* get_array_item(const cJSON *array, size_t index)
M
Max Bruckner 已提交
1669
{
M
Max Bruckner 已提交
1670 1671 1672
    cJSON *current_child = NULL;

    if (array == NULL)
M
Max Bruckner 已提交
1673
    {
M
Max Bruckner 已提交
1674
        return NULL;
M
Max Bruckner 已提交
1675 1676
    }

M
Max Bruckner 已提交
1677 1678 1679 1680 1681 1682 1683 1684
    current_child = array->child;
    while ((current_child != NULL) && (index > 0))
    {
        index--;
        current_child = current_child->next;
    }

    return current_child;
M
Max Bruckner 已提交
1685 1686
}

M
Max Bruckner 已提交
1687
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
M
Max Bruckner 已提交
1688
{
M
Max Bruckner 已提交
1689
    if (index < 0)
M
Max Bruckner 已提交
1690
    {
M
Max Bruckner 已提交
1691
        return NULL;
M
Max Bruckner 已提交
1692
    }
M
Max Bruckner 已提交
1693

M
Max Bruckner 已提交
1694
    return get_array_item(array, (size_t)index);
M
Max Bruckner 已提交
1695 1696
}

1697
static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
1698 1699 1700
{
    cJSON *current_element = NULL;

1701
    if ((object == NULL) || (name == NULL))
1702 1703 1704 1705 1706
    {
        return NULL;
    }

    current_element = object->child;
1707
    if (case_sensitive)
1708
    {
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
        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;
        }
1720 1721 1722 1723 1724
    }

    return current_element;
}

1725
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
1726 1727 1728 1729 1730 1731 1732 1733 1734
{
    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);
}

1735
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
M
Max Bruckner 已提交
1736 1737 1738
{
    return cJSON_GetObjectItem(object, string) ? 1 : 0;
}
K
Kevin Branigan 已提交
1739 1740

/* Utility for array list handling. */
M
Max Bruckner 已提交
1741 1742 1743 1744 1745 1746
static void suffix_object(cJSON *prev, cJSON *item)
{
    prev->next = item;
    item->prev = prev;
}

K
Kevin Branigan 已提交
1747
/* Utility for handling references. */
1748
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1749
{
1750
    cJSON *ref = cJSON_New_Item(hooks);
M
Max Bruckner 已提交
1751 1752
    if (!ref)
    {
1753
        return NULL;
M
Max Bruckner 已提交
1754 1755
    }
    memcpy(ref, item, sizeof(cJSON));
1756
    ref->string = NULL;
M
Max Bruckner 已提交
1757
    ref->type |= cJSON_IsReference;
1758
    ref->next = ref->prev = NULL;
M
Max Bruckner 已提交
1759 1760
    return ref;
}
K
Kevin Branigan 已提交
1761 1762

/* Add item to array/object. */
1763
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
M
Max Bruckner 已提交
1764
{
1765 1766 1767
    cJSON *child = NULL;

    if ((item == NULL) || (array == NULL))
M
Max Bruckner 已提交
1768 1769 1770
    {
        return;
    }
1771 1772 1773 1774

    child = array->child;

    if (child == NULL)
M
Max Bruckner 已提交
1775 1776 1777 1778 1779 1780 1781
    {
        /* list is empty, start new one */
        array->child = item;
    }
    else
    {
        /* append to the end */
1782
        while (child->next)
M
Max Bruckner 已提交
1783
        {
1784
            child = child->next;
M
Max Bruckner 已提交
1785
        }
1786
        suffix_object(child, item);
M
Max Bruckner 已提交
1787 1788 1789
    }
}

1790
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
M
Max Bruckner 已提交
1791
{
1792
    /* call cJSON_AddItemToObjectCS for code reuse */
1793
    cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
1794 1795
    /* remove cJSON_StringIsConst flag */
    item->type &= ~cJSON_StringIsConst;
M
Max Bruckner 已提交
1796 1797
}

1798 1799 1800
#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic push
#endif
1801
#ifdef __GNUC__
1802
#pragma GCC diagnostic ignored "-Wcast-qual"
1803 1804
#endif

1805
/* Add an item to an object with constant string as key */
1806
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
1807 1808 1809 1810 1811 1812 1813
{
    if (!item)
    {
        return;
    }
    if (!(item->type & cJSON_StringIsConst) && item->string)
    {
1814
        global_hooks.deallocate(item->string);
1815 1816 1817 1818 1819
    }
    item->string = (char*)string;
    item->type |= cJSON_StringIsConst;
    cJSON_AddItemToArray(object, item);
}
1820 1821 1822
#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic pop
#endif
1823

1824
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
1825
{
1826
    cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
1827 1828
}

1829
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
1830
{
1831
    cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
1832 1833
}

M
Max Bruckner 已提交
1834
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
1835
{
M
Max Bruckner 已提交
1836
    if ((parent == NULL) || (item == NULL))
1837
    {
1838
        return NULL;
1839
    }
M
Max Bruckner 已提交
1840 1841

    if (item->prev != NULL)
1842 1843
    {
        /* not the first element */
M
Max Bruckner 已提交
1844
        item->prev->next = item->next;
1845
    }
M
Max Bruckner 已提交
1846
    if (item->next != NULL)
1847
    {
M
Max Bruckner 已提交
1848 1849
        /* not the last element */
        item->next->prev = item->prev;
1850
    }
M
Max Bruckner 已提交
1851 1852

    if (item == parent->child)
1853
    {
M
Max Bruckner 已提交
1854 1855
        /* first element */
        parent->child = item->next;
1856 1857
    }
    /* make sure the detached item doesn't point anywhere anymore */
M
Max Bruckner 已提交
1858 1859
    item->prev = NULL;
    item->next = NULL;
1860

M
Max Bruckner 已提交
1861
    return item;
1862
}
M
Max Bruckner 已提交
1863

1864
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
M
Max Bruckner 已提交
1865 1866 1867 1868 1869 1870
{
    if (which < 0)
    {
        return NULL;
    }

M
Max Bruckner 已提交
1871
    return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
M
Max Bruckner 已提交
1872
}
K
Kevin Branigan 已提交
1873

1874
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
1875 1876 1877 1878
{
    cJSON_Delete(cJSON_DetachItemFromArray(array, which));
}

1879
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
1880
{
1881
    cJSON *to_detach = cJSON_GetObjectItem(object, string);
1882

1883 1884 1885 1886 1887 1888
    return cJSON_DetachItemViaPointer(object, to_detach);
}

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

1890
    return cJSON_DetachItemViaPointer(object, to_detach);
1891 1892
}

1893
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
1894 1895 1896
{
    cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}
K
Kevin Branigan 已提交
1897

1898 1899 1900 1901 1902
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
{
    cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
}

K
Kevin Branigan 已提交
1903
/* Replace array/object items with new ones. */
1904
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
1905
{
M
Max Bruckner 已提交
1906 1907 1908
    cJSON *after_inserted = NULL;

    if (which < 0)
1909
    {
M
Max Bruckner 已提交
1910
        return;
1911
    }
M
Max Bruckner 已提交
1912 1913 1914

    after_inserted = get_array_item(array, (size_t)which);
    if (after_inserted == NULL)
1915 1916 1917 1918
    {
        cJSON_AddItemToArray(array, newitem);
        return;
    }
M
Max Bruckner 已提交
1919 1920 1921 1922 1923

    newitem->next = after_inserted;
    newitem->prev = after_inserted->prev;
    after_inserted->prev = newitem;
    if (after_inserted == array->child)
1924 1925 1926 1927 1928 1929 1930 1931 1932
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }
}

M
Max Bruckner 已提交
1933
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
1934
{
M
Max Bruckner 已提交
1935
    if ((parent == NULL) || (replacement == NULL))
1936
    {
M
Max Bruckner 已提交
1937
        return false;
1938
    }
M
Max Bruckner 已提交
1939

M
Max Bruckner 已提交
1940
    if (replacement == item)
1941
    {
M
Max Bruckner 已提交
1942
        return true;
1943
    }
M
Max Bruckner 已提交
1944

M
Max Bruckner 已提交
1945 1946 1947 1948
    replacement->next = item->next;
    replacement->prev = item->prev;

    if (replacement->next != NULL)
1949
    {
M
Max Bruckner 已提交
1950
        replacement->next->prev = replacement;
1951
    }
M
Max Bruckner 已提交
1952
    if (replacement->prev != NULL)
1953
    {
M
Max Bruckner 已提交
1954
        replacement->prev->next = replacement;
1955
    }
M
Max Bruckner 已提交
1956
    if (parent->child == item)
1957
    {
M
Max Bruckner 已提交
1958
        parent->child = replacement;
1959
    }
M
Max Bruckner 已提交
1960

M
Max Bruckner 已提交
1961 1962 1963
    item->next = NULL;
    item->prev = NULL;
    cJSON_Delete(item);
M
Max Bruckner 已提交
1964

M
Max Bruckner 已提交
1965
    return true;
1966
}
M
Max Bruckner 已提交
1967

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

M
Max Bruckner 已提交
1975
    cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
M
Max Bruckner 已提交
1976
}
1977

1978
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
1979
{
M
Max Bruckner 已提交
1980
    cJSON_ReplaceItemViaPointer(object, cJSON_GetObjectItem(object, string), newitem);
1981
}
1982

1983 1984 1985
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
{
    cJSON_ReplaceItemViaPointer(object, cJSON_GetObjectItemCaseSensitive(object, string), newitem);
1986
}
K
Kevin Branigan 已提交
1987 1988

/* Create basic types: */
1989
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
M
Max Bruckner 已提交
1990
{
1991
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1992 1993 1994 1995 1996 1997 1998 1999
    if(item)
    {
        item->type = cJSON_NULL;
    }

    return item;
}

2000
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
M
Max Bruckner 已提交
2001
{
2002
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2003 2004 2005 2006 2007 2008 2009 2010
    if(item)
    {
        item->type = cJSON_True;
    }

    return item;
}

2011
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
M
Max Bruckner 已提交
2012
{
2013
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2014 2015 2016 2017 2018 2019 2020 2021
    if(item)
    {
        item->type = cJSON_False;
    }

    return item;
}

2022
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b)
M
Max Bruckner 已提交
2023
{
2024
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2025 2026 2027 2028 2029 2030 2031 2032
    if(item)
    {
        item->type = b ? cJSON_True : cJSON_False;
    }

    return item;
}

2033
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
M
Max Bruckner 已提交
2034
{
2035
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2036 2037 2038 2039
    if(item)
    {
        item->type = cJSON_Number;
        item->valuedouble = num;
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053

        /* 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 已提交
2054 2055 2056 2057 2058
    }

    return item;
}

2059
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
M
Max Bruckner 已提交
2060
{
2061
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2062 2063 2064
    if(item)
    {
        item->type = cJSON_String;
2065
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
M
Max Bruckner 已提交
2066 2067 2068
        if(!item->valuestring)
        {
            cJSON_Delete(item);
2069
            return NULL;
M
Max Bruckner 已提交
2070 2071 2072 2073 2074 2075
        }
    }

    return item;
}

2076
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
J
Jiri Zouhar 已提交
2077
{
2078
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2079 2080 2081
    if(item)
    {
        item->type = cJSON_Raw;
2082
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
M
Max Bruckner 已提交
2083 2084 2085 2086 2087 2088 2089 2090
        if(!item->valuestring)
        {
            cJSON_Delete(item);
            return NULL;
        }
    }

    return item;
J
Jiri Zouhar 已提交
2091 2092
}

2093
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
M
Max Bruckner 已提交
2094
{
2095
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2096 2097 2098 2099 2100 2101 2102 2103
    if(item)
    {
        item->type=cJSON_Array;
    }

    return item;
}

2104
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
M
Max Bruckner 已提交
2105
{
2106
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2107 2108 2109 2110 2111 2112 2113
    if (item)
    {
        item->type = cJSON_Object;
    }

    return item;
}
K
Kevin Branigan 已提交
2114 2115

/* Create Arrays: */
2116
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
M
Max Bruckner 已提交
2117
{
2118
    size_t i = 0;
2119 2120
    cJSON *n = NULL;
    cJSON *p = NULL;
2121 2122 2123 2124 2125 2126 2127 2128 2129
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();
    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2130 2131 2132 2133 2134
    {
        n = cJSON_CreateNumber(numbers[i]);
        if (!n)
        {
            cJSON_Delete(a);
2135
            return NULL;
M
Max Bruckner 已提交
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2151
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
M
Max Bruckner 已提交
2152
{
2153
    size_t i = 0;
2154 2155
    cJSON *n = NULL;
    cJSON *p = NULL;
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2166
    {
2167
        n = cJSON_CreateNumber((double)numbers[i]);
M
Max Bruckner 已提交
2168 2169 2170
        if(!n)
        {
            cJSON_Delete(a);
2171
            return NULL;
M
Max Bruckner 已提交
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2187
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
2188
{
2189
    size_t i = 0;
2190 2191
    cJSON *n = NULL;
    cJSON *p = NULL;
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0;a && (i < (size_t)count); i++)
2202 2203 2204 2205 2206
    {
        n = cJSON_CreateNumber(numbers[i]);
        if(!n)
        {
            cJSON_Delete(a);
2207
            return NULL;
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2223
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
2224
{
2225
    size_t i = 0;
2226 2227
    cJSON *n = NULL;
    cJSON *p = NULL;
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for (i = 0; a && (i < (size_t)count); i++)
2238 2239 2240 2241 2242
    {
        n = cJSON_CreateString(strings[i]);
        if(!n)
        {
            cJSON_Delete(a);
2243
            return NULL;
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p,n);
        }
        p = n;
    }

    return a;
}
2258 2259

/* Duplication */
2260
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2261
{
M
Max Bruckner 已提交
2262
    cJSON *newitem = NULL;
2263 2264
    cJSON *child = NULL;
    cJSON *next = NULL;
M
Max Bruckner 已提交
2265
    cJSON *newchild = NULL;
M
Max Bruckner 已提交
2266 2267 2268 2269

    /* Bail on bad ptr */
    if (!item)
    {
2270
        goto fail;
M
Max Bruckner 已提交
2271 2272
    }
    /* Create new item */
2273
    newitem = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2274 2275
    if (!newitem)
    {
2276
        goto fail;
M
Max Bruckner 已提交
2277 2278 2279 2280 2281 2282 2283
    }
    /* Copy over all vars */
    newitem->type = item->type & (~cJSON_IsReference);
    newitem->valueint = item->valueint;
    newitem->valuedouble = item->valuedouble;
    if (item->valuestring)
    {
2284
        newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
M
Max Bruckner 已提交
2285 2286
        if (!newitem->valuestring)
        {
2287
            goto fail;
M
Max Bruckner 已提交
2288 2289 2290 2291
        }
    }
    if (item->string)
    {
2292
        newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
M
Max Bruckner 已提交
2293 2294
        if (!newitem->string)
        {
2295
            goto fail;
M
Max Bruckner 已提交
2296 2297 2298 2299 2300 2301 2302 2303
        }
    }
    /* If non-recursive, then we're done! */
    if (!recurse)
    {
        return newitem;
    }
    /* Walk the ->next chain for the child. */
2304 2305
    child = item->child;
    while (child != NULL)
M
Max Bruckner 已提交
2306
    {
2307
        newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
M
Max Bruckner 已提交
2308 2309
        if (!newchild)
        {
2310
            goto fail;
M
Max Bruckner 已提交
2311
        }
2312
        if (next != NULL)
M
Max Bruckner 已提交
2313 2314
        {
            /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2315 2316 2317
            next->next = newchild;
            newchild->prev = next;
            next = newchild;
M
Max Bruckner 已提交
2318 2319 2320 2321
        }
        else
        {
            /* Set newitem->child and move to it */
2322 2323
            newitem->child = newchild;
            next = newchild;
M
Max Bruckner 已提交
2324
        }
2325
        child = child->next;
M
Max Bruckner 已提交
2326 2327 2328
    }

    return newitem;
2329 2330 2331 2332 2333 2334 2335 2336

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

    return NULL;
2337
}
2338

2339
CJSON_PUBLIC(void) cJSON_Minify(char *json)
2340
{
2341
    unsigned char *into = (unsigned char*)json;
M
Max Bruckner 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
    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 已提交
2381
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2382 2383 2384 2385
            while (*json && (*json != '\"'))
            {
                if (*json == '\\')
                {
M
Max Bruckner 已提交
2386
                    *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2387
                }
M
Max Bruckner 已提交
2388
                *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2389
            }
M
Max Bruckner 已提交
2390
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2391 2392 2393 2394
        }
        else
        {
            /* All other characters. */
M
Max Bruckner 已提交
2395
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2396 2397 2398 2399 2400
        }
    }

    /* and null-terminate. */
    *into = '\0';
2401
}
2402

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

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

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

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

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

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


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

    return (item->type & (cJSON_True | cJSON_False)) != 0;
}
2443
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
2444 2445 2446 2447 2448 2449 2450 2451 2452
{
    if (item == NULL)
    {
        return false;
    }

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

2453
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
2454 2455 2456 2457 2458 2459 2460 2461 2462
{
    if (item == NULL)
    {
        return false;
    }

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

2463
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
2464 2465 2466 2467 2468 2469 2470 2471 2472
{
    if (item == NULL)
    {
        return false;
    }

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

2473
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
2474 2475 2476 2477 2478 2479 2480 2481 2482
{
    if (item == NULL)
    {
        return false;
    }

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

2483
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
2484 2485 2486 2487 2488 2489 2490 2491 2492
{
    if (item == NULL)
    {
        return false;
    }

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

2493
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
2494 2495 2496 2497 2498 2499 2500 2501
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & 0xFF) == cJSON_Raw;
}
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562

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 已提交
2563 2564 2565 2566
            cJSON *a_element = a->child;
            cJSON *b_element = b->child;

            for (; (a_element != NULL) && (b_element != NULL);)
2567 2568 2569 2570 2571
            {
                if (!cJSON_Compare(a_element, b_element, case_sensitive))
                {
                    return false;
                }
M
Max Bruckner 已提交
2572 2573 2574

                a_element = a_element->next;
                b_element = b_element->next;
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
            }

            return true;
        }

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

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

            return true;
        }

        default:
            return false;
    }
}
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614

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

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