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

  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
#pragma GCC visibility push(default)
K
Kevin Branigan 已提交
27 28 29 30 31 32 33
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
34
#include <locale.h>
35 36
#pragma GCC visibility pop

K
Kevin Branigan 已提交
37 38
#include "cJSON.h"

M
Max Bruckner 已提交
39
/* define our own boolean type */
40 41
#define true ((cJSON_bool)1)
#define false ((cJSON_bool)0)
M
Max Bruckner 已提交
42

43
static const unsigned char *global_ep = NULL;
K
Kevin Branigan 已提交
44

45
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
M
Max Bruckner 已提交
46
{
47
    return (const char*) global_ep;
M
Max Bruckner 已提交
48
}
K
Kevin Branigan 已提交
49

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

55
CJSON_PUBLIC(const char*) cJSON_Version(void)
56 57 58 59 60 61 62
{
    static char version[15];
    sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);

    return version;
}

M
Max Bruckner 已提交
63
/* case insensitive strcmp */
64
static int cJSON_strcasecmp(const unsigned char *s1, const unsigned char *s2)
K
Kevin Branigan 已提交
65
{
M
Max Bruckner 已提交
66 67 68 69 70 71 72 73
    if (!s1)
    {
        return (s1 == s2) ? 0 : 1; /* both NULL? */
    }
    if (!s2)
    {
        return 1;
    }
M
Max Bruckner 已提交
74
    for(; tolower(*s1) == tolower(*s2); (void)++s1, ++s2)
M
Max Bruckner 已提交
75
    {
76
        if (*s1 == '\0')
M
Max Bruckner 已提交
77 78 79 80 81
        {
            return 0;
        }
    }

82
    return tolower(*s1) - tolower(*s2);
K
Kevin Branigan 已提交
83 84
}

85 86 87 88 89 90 91 92
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 已提交
93

94
static unsigned char* cJSON_strdup(const unsigned char* str, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
95
{
M
Max Bruckner 已提交
96
    size_t len = 0;
97
    unsigned char *copy = NULL;
K
Kevin Branigan 已提交
98

99 100 101 102 103
    if (str == NULL)
    {
        return NULL;
    }

104
    len = strlen((const char*)str) + sizeof("");
105
    if (!(copy = (unsigned char*)hooks->allocate(len)))
M
Max Bruckner 已提交
106
    {
107
        return NULL;
M
Max Bruckner 已提交
108 109 110 111
    }
    memcpy(copy, str, len);

    return copy;
K
Kevin Branigan 已提交
112 113
}

114
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
K
Kevin Branigan 已提交
115
{
M
Max Bruckner 已提交
116
    if (hooks == NULL)
M
Max Bruckner 已提交
117 118
    {
        /* Reset hooks */
119 120 121
        global_hooks.allocate = malloc;
        global_hooks.deallocate = free;
        global_hooks.reallocate = realloc;
K
Kevin Branigan 已提交
122 123 124
        return;
    }

125
    global_hooks.allocate = malloc;
M
Max Bruckner 已提交
126 127
    if (hooks->malloc_fn != NULL)
    {
128
        global_hooks.allocate = hooks->malloc_fn;
M
Max Bruckner 已提交
129 130
    }

131
    global_hooks.deallocate = free;
M
Max Bruckner 已提交
132 133
    if (hooks->free_fn != NULL)
    {
134
        global_hooks.deallocate = hooks->free_fn;
M
Max Bruckner 已提交
135 136 137
    }

    /* use realloc only if both free and malloc are used */
138 139
    global_hooks.reallocate = NULL;
    if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
M
Max Bruckner 已提交
140
    {
141
        global_hooks.reallocate = realloc;
M
Max Bruckner 已提交
142
    }
K
Kevin Branigan 已提交
143 144 145
}

/* Internal constructor. */
146
static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
K
Kevin Branigan 已提交
147
{
148
    cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
M
Max Bruckner 已提交
149 150
    if (node)
    {
151
        memset(node, '\0', sizeof(cJSON));
M
Max Bruckner 已提交
152 153 154
    }

    return node;
K
Kevin Branigan 已提交
155 156 157
}

/* Delete a cJSON structure. */
158
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c)
K
Kevin Branigan 已提交
159
{
M
Max Bruckner 已提交
160
    cJSON *next = NULL;
M
Max Bruckner 已提交
161 162 163 164 165 166 167 168 169
    while (c)
    {
        next = c->next;
        if (!(c->type & cJSON_IsReference) && c->child)
        {
            cJSON_Delete(c->child);
        }
        if (!(c->type & cJSON_IsReference) && c->valuestring)
        {
170
            global_hooks.deallocate(c->valuestring);
M
Max Bruckner 已提交
171 172 173
        }
        if (!(c->type & cJSON_StringIsConst) && c->string)
        {
174
            global_hooks.deallocate(c->string);
M
Max Bruckner 已提交
175
        }
176
        global_hooks.deallocate(c);
M
Max Bruckner 已提交
177 178
        c = next;
    }
K
Kevin Branigan 已提交
179 180
}

181 182 183 184 185 186 187
/* 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];
}

K
Kevin Branigan 已提交
188
/* Parse the input text to generate a number, and populate the result into item. */
189
static const unsigned char *parse_number(cJSON * const item, const unsigned char * const input)
K
Kevin Branigan 已提交
190
{
191
    double number = 0;
192
    unsigned char *after_end = NULL;
193 194 195
    unsigned char number_c_string[64];
    unsigned char decimal_point = get_decimal_point();
    size_t i = 0;
M
Max Bruckner 已提交
196

197
    if (input == NULL)
198 199 200 201
    {
        return NULL;
    }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    /* copy the number into a temporary buffer and replace '.' with the decimal point
     * of the current locale (for strtod) */
    for (i = 0; (i < (sizeof(number_c_string) - 1)) && (input[i] != '\0'); i++)
    {
        switch (input[i])
        {
            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':
                number_c_string[i] = input[i];
                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 已提交
238
    {
239
        return NULL; /* parse_error */
M
Max Bruckner 已提交
240 241
    }

242
    item->valuedouble = number;
M
Max Bruckner 已提交
243

244
    /* use saturation in case of overflow */
245
    if (number >= INT_MAX)
246 247 248
    {
        item->valueint = INT_MAX;
    }
249
    else if (number <= INT_MIN)
250 251 252 253 254
    {
        item->valueint = INT_MIN;
    }
    else
    {
255
        item->valueint = (int)number;
256
    }
257

M
Max Bruckner 已提交
258 259
    item->type = cJSON_Number;

260
    return input + (after_end - number_c_string);
K
Kevin Branigan 已提交
261 262
}

263
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
264
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
265 266 267 268 269 270 271 272 273 274 275
{
    if (number >= INT_MAX)
    {
        object->valueint = INT_MAX;
    }
    else if (number <= INT_MIN)
    {
        object->valueint = INT_MIN;
    }
    else
    {
276
        object->valueint = (int)number;
277 278 279 280 281
    }

    return object->valuedouble = number;
}

M
Max Bruckner 已提交
282 283
typedef struct
{
284
    unsigned char *buffer;
285 286
    size_t length;
    size_t offset;
287
    cJSON_bool noalloc;
M
Max Bruckner 已提交
288
} printbuffer;
289

M
Max Bruckner 已提交
290
/* realloc printbuffer if necessary to have at least "needed" bytes more */
291
static unsigned char* ensure(printbuffer * const p, size_t needed, const internal_hooks * const hooks)
292
{
293
    unsigned char *newbuffer = NULL;
294 295
    size_t newsize = 0;

296
    if ((p == NULL) || (p->buffer == NULL))
297 298 299 300
    {
        return NULL;
    }

M
Max Bruckner 已提交
301 302 303 304 305 306
    if ((p->length > 0) && (p->offset >= p->length))
    {
        /* make sure that offset is valid */
        return NULL;
    }

307
    if (needed > INT_MAX)
M
Max Bruckner 已提交
308
    {
309
        /* sizes bigger than INT_MAX are currently not supported */
310
        return NULL;
M
Max Bruckner 已提交
311
    }
312

313
    needed += p->offset + 1;
M
Max Bruckner 已提交
314 315 316 317 318
    if (needed <= p->length)
    {
        return p->buffer + p->offset;
    }

319 320 321 322
    if (p->noalloc) {
        return NULL;
    }

323
    /* calculate new buffer size */
M
Max Bruckner 已提交
324
    if (needed > (INT_MAX / 2))
325 326 327 328 329 330 331 332 333 334 335
    {
        /* overflow of int, use INT_MAX if possible */
        if (needed <= INT_MAX)
        {
            newsize = INT_MAX;
        }
        else
        {
            return NULL;
        }
    }
336 337 338 339
    else
    {
        newsize = needed * 2;
    }
340

341
    if (hooks->reallocate != NULL)
M
Max Bruckner 已提交
342
    {
M
Max Bruckner 已提交
343
        /* reallocate with realloc if available */
344
        newbuffer = (unsigned char*)hooks->reallocate(p->buffer, newsize);
M
Max Bruckner 已提交
345
    }
M
Max Bruckner 已提交
346
    else
M
Max Bruckner 已提交
347
    {
M
Max Bruckner 已提交
348
        /* otherwise reallocate manually */
349
        newbuffer = (unsigned char*)hooks->allocate(newsize);
M
Max Bruckner 已提交
350 351
        if (!newbuffer)
        {
352
            hooks->deallocate(p->buffer);
M
Max Bruckner 已提交
353 354 355 356 357 358 359
            p->length = 0;
            p->buffer = NULL;

            return NULL;
        }
        if (newbuffer)
        {
360
            memcpy(newbuffer, p->buffer, p->offset + 1);
M
Max Bruckner 已提交
361
        }
362
        hooks->deallocate(p->buffer);
M
Max Bruckner 已提交
363 364 365 366 367
    }
    p->length = newsize;
    p->buffer = newbuffer;

    return newbuffer + p->offset;
368 369
}

370 371
/* calculate the new length of the string in a printbuffer and update the offset */
static void update_offset(printbuffer * const buffer)
K
Kevin Branigan 已提交
372
{
373 374
    const unsigned char *buffer_pointer = NULL;
    if ((buffer == NULL) || (buffer->buffer == NULL))
M
Max Bruckner 已提交
375
    {
376
        return;
M
Max Bruckner 已提交
377
    }
378
    buffer_pointer = buffer->buffer + buffer->offset;
M
Max Bruckner 已提交
379

380
    buffer->offset += strlen((const char*)buffer_pointer);
381 382
}

383
/* Removes trailing zeroes from the end of a printed number */
384
static int trim_trailing_zeroes(const unsigned char * const number, int length, const unsigned char decimal_point)
K
Kevin Branigan 已提交
385
{
386
    if ((number == NULL) || (length <= 0))
387
    {
388
        return -1;
389 390
    }

391
    while ((length > 0) && (number[length - 1] == '0'))
M
Max Bruckner 已提交
392
    {
393
        length--;
M
Max Bruckner 已提交
394
    }
395
    if ((length > 0) && (number[length - 1] == decimal_point))
396
    {
397 398
        /* remove trailing decimal_point */
        length--;
399 400
    }

401
    return length;
402 403 404
}

/* Render the number nicely from the given item into a string. */
405
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks)
406
{
M
Max Bruckner 已提交
407
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
408
    double d = item->valuedouble;
409
    int length = 0;
410
    size_t i = 0;
411
    cJSON_bool trim_zeroes = true; /* should zeroes at the end be removed? */
412 413
    unsigned char number_buffer[64]; /* temporary buffer to print the number into */
    unsigned char decimal_point = get_decimal_point();
M
Max Bruckner 已提交
414

M
Max Bruckner 已提交
415
    if (output_buffer == NULL)
M
Max Bruckner 已提交
416
    {
417
        return false;
M
Max Bruckner 已提交
418
    }
M
Max Bruckner 已提交
419

420 421 422
    /* This checks for NaN and Infinity */
    if ((d * 0) != 0)
    {
423
        length = sprintf((char*)number_buffer, "null");
424 425 426 427
    }
    else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60))
    {
        /* integer */
428
        length = sprintf((char*)number_buffer, "%.0f", d);
429 430 431 432
        trim_zeroes = false; /* don't remove zeroes for "big integers" */
    }
    else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
    {
433
        length = sprintf((char*)number_buffer, "%e", d);
434 435 436 437
        trim_zeroes = false; /* don't remove zeroes in engineering notation */
    }
    else
    {
438
        length = sprintf((char*)number_buffer, "%f", d);
M
Max Bruckner 已提交
439
    }
440

441 442
    /* sprintf failed or buffer overrun occured */
    if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
443
    {
444
        return false;
445 446
    }

447 448
    if (trim_zeroes)
    {
449 450 451 452 453 454 455 456 457 458 459 460
        length = trim_trailing_zeroes(number_buffer, length, decimal_point);
        if (length <= 0)
        {
            return false;
        }
    }

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

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

479
    return true;
K
Kevin Branigan 已提交
480 481
}

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

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

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

    return h;
516 517
}

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

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

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

540 541
    /* check that the code is valid */
    if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)) || (first_code == 0))
M
Max Bruckner 已提交
542
    {
543
        *error_pointer = first_sequence;
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 558
            /* input ends unexpectedly */
            *error_pointer = first_sequence;
            goto fail;
M
Max Bruckner 已提交
559
        }
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

        if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
        {
            /* missing second half of the surrogate pair */
            *error_pointer = first_sequence;
            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 */
            *error_pointer = first_sequence;
            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 已提交
586
    }
M
Max Bruckner 已提交
587

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

621
    /* encode as utf8 */
622 623 624 625 626
    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;
627
    }
628 629 630 631 632 633 634 635
    /* 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);
636
    }
637

638 639 640 641 642 643 644 645 646
    *output_pointer += utf8_length;

    return sequence_length;

fail:
    return 0;
}

/* Parse the input text into an unescaped cinput, and populate item. */
647
static const unsigned char *parse_string(cJSON * const item, const unsigned char * const input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
{
    const unsigned char *input_pointer = input + 1;
    const unsigned char *input_end = input + 1;
    unsigned char *output_pointer = NULL;
    unsigned char *output = NULL;

    /* not a string */
    if (*input != '\"')
    {
        *error_pointer = input;
        goto fail;
    }

    {
        /* calculate approximate size of the output (overestimate) */
        size_t allocation_length = 0;
        size_t skipped_bytes = 0;
        while ((*input_end != '\"') && (*input_end != '\0'))
        {
            /* is escape sequence */
            if (input_end[0] == '\\')
            {
                if (input_end[1] == '\0')
                {
                    /* prevent buffer overflow when last input character is a backslash */
                    goto fail;
                }
                skipped_bytes++;
                input_end++;
            }
            input_end++;
        }
        if (*input_end == '\0')
        {
            goto fail; /* string ended unexpectedly */
        }

        /* This is at most how much we need for the output */
        allocation_length = (size_t) (input_end - input) - skipped_bytes;
687
        output = (unsigned char*)hooks->allocate(allocation_length + sizeof(""));
688 689 690 691 692 693 694
        if (output == NULL)
        {
            goto fail; /* allocation failure */
        }
    }

    output_pointer = output;
M
Max Bruckner 已提交
695
    /* loop through the string literal */
696
    while (input_pointer < input_end)
M
Max Bruckner 已提交
697
    {
698
        if (*input_pointer != '\\')
M
Max Bruckner 已提交
699
        {
700
            *output_pointer++ = *input_pointer++;
M
Max Bruckner 已提交
701 702 703 704
        }
        /* escape sequence */
        else
        {
705
            unsigned char sequence_length = 2;
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 732
                    sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer, error_pointer);
                    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
                    *error_pointer = input_pointer;
741
                    goto fail;
M
Max Bruckner 已提交
742
            }
743
            input_pointer += sequence_length;
M
Max Bruckner 已提交
744 745
        }
    }
746 747 748

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

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

753
    return input_end + 1;
754 755

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

    return NULL;
K
Kevin Branigan 已提交
762 763 764
}

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

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

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

789
        return true;
M
Max Bruckner 已提交
790 791 792
    }

    /* set "flag" to 1 if something needs to be escaped */
793
    for (input_pointer = input; *input_pointer; input_pointer++)
M
Max Bruckner 已提交
794
    {
M
Max Bruckner 已提交
795
        if (strchr("\"\\\b\f\n\r\t", *input_pointer))
M
Max Bruckner 已提交
796
        {
M
Max Bruckner 已提交
797 798
            /* one character escape sequence */
            escape_characters++;
M
Max Bruckner 已提交
799
        }
M
Max Bruckner 已提交
800
        else if (*input_pointer < 32)
M
Max Bruckner 已提交
801
        {
M
Max Bruckner 已提交
802 803
            /* UTF-16 escape sequence uXXXX */
            escape_characters += 5;
M
Max Bruckner 已提交
804 805
        }
    }
M
Max Bruckner 已提交
806
    output_length = (size_t)(input_pointer - input) + escape_characters;
M
Max Bruckner 已提交
807

808
    output = ensure(output_buffer, output_length + sizeof("\"\""), hooks);
809
    if (output == NULL)
M
Max Bruckner 已提交
810
    {
811
        return false;
M
Max Bruckner 已提交
812 813
    }

M
Max Bruckner 已提交
814 815
    /* no characters have to be escaped */
    if (escape_characters == 0)
M
Max Bruckner 已提交
816
    {
M
Max Bruckner 已提交
817 818 819 820 821
        output[0] = '\"';
        memcpy(output + 1, input, output_length);
        output[output_length + 1] = '\"';
        output[output_length + 2] = '\0';

822
        return true;
M
Max Bruckner 已提交
823 824
    }

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

873
    return true;
K
Kevin Branigan 已提交
874
}
M
Max Bruckner 已提交
875

M
Max Bruckner 已提交
876
/* Invoke print_string_ptr (which is useful) on an item. */
877
static cJSON_bool print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks)
M
Max Bruckner 已提交
878
{
879
    return print_string_ptr((unsigned char*)item->valuestring, p, hooks);
M
Max Bruckner 已提交
880
}
K
Kevin Branigan 已提交
881 882

/* Predeclare these prototypes. */
883
static const unsigned char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const ep, const internal_hooks * const hooks);
884
static cJSON_bool print_value(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
885
static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks);
886
static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
887
static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks);
888
static cJSON_bool print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
K
Kevin Branigan 已提交
889 890

/* Utility to jump whitespace and cr/lf */
M
Max Bruckner 已提交
891
static const unsigned char *skip_whitespace(const unsigned char *in)
M
Max Bruckner 已提交
892
{
893
    while (in && *in && (*in <= 32))
M
Max Bruckner 已提交
894 895 896 897 898 899
    {
        in++;
    }

    return in;
}
K
Kevin Branigan 已提交
900 901

/* Parse an object - create a new root, and populate. */
902
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
K
Kevin Branigan 已提交
903
{
904
    const unsigned char *end = NULL;
M
Max Bruckner 已提交
905
    /* use global error pointer if no specific one was given */
906 907 908 909
    const unsigned char **error_pointer = (return_parse_end != NULL) ? (const unsigned char**)return_parse_end : &global_ep;
    cJSON *item = cJSON_New_Item(&global_hooks);
    *error_pointer = NULL;
    if (item == NULL) /* memory fail */
M
Max Bruckner 已提交
910
    {
911
        return NULL;
M
Max Bruckner 已提交
912 913
    }

914 915
    end = parse_value(item, skip_whitespace((const unsigned char*)value), error_pointer, &global_hooks);
    if (end == NULL)
M
Max Bruckner 已提交
916 917
    {
        /* parse failure. ep is set. */
918
        cJSON_Delete(item);
919
        return NULL;
M
Max Bruckner 已提交
920 921 922 923 924
    }

    /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
    if (require_null_terminated)
    {
M
Max Bruckner 已提交
925
        end = skip_whitespace(end);
926
        if (*end != '\0')
M
Max Bruckner 已提交
927
        {
928 929
            cJSON_Delete(item);
            *error_pointer = end;
930
            return NULL;
M
Max Bruckner 已提交
931 932 933 934
        }
    }
    if (return_parse_end)
    {
935
        *return_parse_end = (const char*)end;
M
Max Bruckner 已提交
936 937
    }

938
    return item;
K
Kevin Branigan 已提交
939
}
M
Max Bruckner 已提交
940

941
/* Default options for cJSON_Parse */
942
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
M
Max Bruckner 已提交
943 944 945
{
    return cJSON_ParseWithOpts(value, 0, 0);
}
K
Kevin Branigan 已提交
946

M
Max Bruckner 已提交
947 948
#define min(a, b) ((a < b) ? a : b)

949
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
M
Max Bruckner 已提交
950 951 952 953 954 955 956
{
    printbuffer buffer[1];
    unsigned char *printed = NULL;

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

    /* create buffer */
957
    buffer->buffer = (unsigned char*) hooks->allocate(256);
M
Max Bruckner 已提交
958 959 960 961 962 963
    if (buffer->buffer == NULL)
    {
        goto fail;
    }

    /* print the value */
964
    if (!print_value(item, 0, format, buffer, hooks))
M
Max Bruckner 已提交
965 966 967
    {
        goto fail;
    }
968
    update_offset(buffer);
M
Max Bruckner 已提交
969 970

    /* copy the buffer over to a new one */
971
    printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
M
Max Bruckner 已提交
972 973 974 975 976 977 978 979
    if (printed == NULL)
    {
        goto fail;
    }
    strncpy((char*)printed, (char*)buffer->buffer, min(buffer->length, buffer->offset + 1));
    printed[buffer->offset] = '\0'; /* just to be sure */

    /* free the buffer */
980
    hooks->deallocate(buffer->buffer);
M
Max Bruckner 已提交
981 982 983 984 985 986

    return printed;

fail:
    if (buffer->buffer != NULL)
    {
987
        hooks->deallocate(buffer->buffer);
M
Max Bruckner 已提交
988 989 990 991
    }

    if (printed != NULL)
    {
992
        hooks->deallocate(printed);
M
Max Bruckner 已提交
993 994 995 996 997
    }

    return NULL;
}

K
Kevin Branigan 已提交
998
/* Render a cJSON item/entity/structure to text. */
999
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
M
Max Bruckner 已提交
1000
{
1001
    return (char*)print(item, true, &global_hooks);
M
Max Bruckner 已提交
1002 1003
}

1004
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
1005
{
1006
    return (char*)print(item, false, &global_hooks);
1007
}
1008

1009
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
1010
{
M
Max Bruckner 已提交
1011
    printbuffer p;
M
Max Bruckner 已提交
1012 1013 1014

    if (prebuffer < 0)
    {
M
Max Bruckner 已提交
1015
        return NULL;
M
Max Bruckner 已提交
1016 1017
    }

1018
    p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
1019 1020
    if (!p.buffer)
    {
1021
        return NULL;
1022
    }
M
Max Bruckner 已提交
1023 1024

    p.length = (size_t)prebuffer;
M
Max Bruckner 已提交
1025
    p.offset = 0;
1026
    p.noalloc = false;
M
Max Bruckner 已提交
1027

1028 1029 1030 1031 1032 1033
    if (!print_value(item, 0, fmt, &p, &global_hooks))
    {
        return NULL;
    }

    return (char*)p.buffer;
1034 1035
}

1036
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
1037 1038
{
    printbuffer p;
M
Max Bruckner 已提交
1039 1040 1041 1042 1043 1044

    if (len < 0)
    {
        return false;
    }

1045
    p.buffer = (unsigned char*)buf;
M
Max Bruckner 已提交
1046
    p.length = (size_t)len;
1047
    p.offset = 0;
1048
    p.noalloc = true;
1049
    return print_value(item, 0, fmt, &p, &global_hooks);
1050
}
K
Kevin Branigan 已提交
1051 1052

/* Parser core - when encountering text, process appropriately. */
1053
static const unsigned  char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1054
{
1055
    if (input == NULL)
M
Max Bruckner 已提交
1056
    {
1057
        return NULL; /* no input */
M
Max Bruckner 已提交
1058 1059 1060
    }

    /* parse the different types of values */
1061 1062
    /* null */
    if (!strncmp((const char*)input, "null", 4))
M
Max Bruckner 已提交
1063 1064
    {
        item->type = cJSON_NULL;
1065
        return input + 4;
M
Max Bruckner 已提交
1066
    }
1067 1068
    /* false */
    if (!strncmp((const char*)input, "false", 5))
M
Max Bruckner 已提交
1069 1070
    {
        item->type = cJSON_False;
1071
        return input + 5;
M
Max Bruckner 已提交
1072
    }
1073 1074
    /* true */
    if (!strncmp((const char*)input, "true", 4))
M
Max Bruckner 已提交
1075 1076 1077
    {
        item->type = cJSON_True;
        item->valueint = 1;
1078
        return input + 4;
M
Max Bruckner 已提交
1079
    }
1080 1081
    /* string */
    if (*input == '\"')
M
Max Bruckner 已提交
1082
    {
1083
        return parse_string(item, input, error_pointer, hooks);
M
Max Bruckner 已提交
1084
    }
1085 1086
    /* number */
    if ((*input == '-') || ((*input >= '0') && (*input <= '9')))
M
Max Bruckner 已提交
1087
    {
1088
        return parse_number(item, input);
M
Max Bruckner 已提交
1089
    }
1090 1091
    /* array */
    if (*input == '[')
M
Max Bruckner 已提交
1092
    {
1093
        return parse_array(item, input, error_pointer, hooks);
M
Max Bruckner 已提交
1094
    }
1095 1096
    /* object */
    if (*input == '{')
M
Max Bruckner 已提交
1097
    {
1098
        return parse_object(item, input, error_pointer, hooks);
M
Max Bruckner 已提交
1099 1100
    }

M
Max Bruckner 已提交
1101
    /* failure. */
1102
    *error_pointer = input;
1103
    return NULL;
K
Kevin Branigan 已提交
1104 1105 1106
}

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

M
Max Bruckner 已提交
1111
    if ((item == NULL) || (output_buffer == NULL))
M
Max Bruckner 已提交
1112
    {
1113
        return false;
M
Max Bruckner 已提交
1114
    }
M
Max Bruckner 已提交
1115 1116

    switch ((item->type) & 0xFF)
M
Max Bruckner 已提交
1117
    {
M
Max Bruckner 已提交
1118
        case cJSON_NULL:
1119
            output = ensure(output_buffer, 5, hooks);
1120
            if (output == NULL)
M
Max Bruckner 已提交
1121
            {
1122
                return false;
M
Max Bruckner 已提交
1123
            }
1124 1125 1126
            strcpy((char*)output, "null");
            return true;

M
Max Bruckner 已提交
1127
        case cJSON_False:
1128
            output = ensure(output_buffer, 6, hooks);
1129
            if (output == NULL)
M
Max Bruckner 已提交
1130
            {
1131
                return false;
M
Max Bruckner 已提交
1132
            }
1133 1134 1135
            strcpy((char*)output, "false");
            return true;

M
Max Bruckner 已提交
1136
        case cJSON_True:
1137
            output = ensure(output_buffer, 5, hooks);
1138
            if (output == NULL)
M
Max Bruckner 已提交
1139
            {
1140
                return false;
M
Max Bruckner 已提交
1141
            }
1142 1143 1144
            strcpy((char*)output, "true");
            return true;

M
Max Bruckner 已提交
1145
        case cJSON_Number:
1146
            return print_number(item, output_buffer, hooks);
1147

M
Max Bruckner 已提交
1148
        case cJSON_Raw:
M
Max Bruckner 已提交
1149
        {
M
Max Bruckner 已提交
1150 1151
            size_t raw_length = 0;
            if (item->valuestring == NULL)
1152
            {
M
Max Bruckner 已提交
1153
                if (!output_buffer->noalloc)
1154
                {
1155
                    hooks->deallocate(output_buffer->buffer);
1156
                }
1157
                return false;
M
Max Bruckner 已提交
1158
            }
1159

1160
            raw_length = strlen(item->valuestring) + sizeof("");
1161
            output = ensure(output_buffer, raw_length, hooks);
1162
            if (output == NULL)
M
Max Bruckner 已提交
1163
            {
1164
                return false;
1165
            }
1166 1167
            memcpy(output, item->valuestring, raw_length);
            return true;
M
Max Bruckner 已提交
1168
        }
1169

M
Max Bruckner 已提交
1170
        case cJSON_String:
1171
            return print_string(item, output_buffer, hooks);
1172

M
Max Bruckner 已提交
1173
        case cJSON_Array:
1174
            return print_array(item, depth, format, output_buffer, hooks);
1175

M
Max Bruckner 已提交
1176
        case cJSON_Object:
1177
            return print_object(item, depth, format, output_buffer, hooks);
1178

M
Max Bruckner 已提交
1179
        default:
1180
            return false;
M
Max Bruckner 已提交
1181
    }
K
Kevin Branigan 已提交
1182 1183 1184
}

/* Build an array from input text. */
1185
static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1186
{
1187
    cJSON *head = NULL; /* head of the linked list */
1188 1189
    cJSON *current_item = NULL;

1190
    if (*input != '[')
M
Max Bruckner 已提交
1191
    {
1192
        /* not an array */
1193
        *error_pointer = input;
1194
        goto fail;
M
Max Bruckner 已提交
1195
    }
K
Kevin Branigan 已提交
1196

M
Max Bruckner 已提交
1197
    input = skip_whitespace(input + 1);
1198
    if (*input == ']')
M
Max Bruckner 已提交
1199
    {
1200
        /* empty array */
1201
        goto success;
M
Max Bruckner 已提交
1202
    }
K
Kevin Branigan 已提交
1203

1204
    /* step back to character in front of the first element */
1205
    input--;
M
Max Bruckner 已提交
1206
    /* loop through the comma separated array elements */
1207
    do
M
Max Bruckner 已提交
1208
    {
1209
        /* allocate next item */
1210
        cJSON *new_item = cJSON_New_Item(hooks);
1211
        if (new_item == NULL)
M
Max Bruckner 已提交
1212
        {
1213
            goto fail; /* allocation failure */
M
Max Bruckner 已提交
1214
        }
1215 1216 1217

        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1218
        {
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
            /* 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 已提交
1231
        input = skip_whitespace(input + 1);
1232
        input = parse_value(current_item, input, error_pointer, hooks);
M
Max Bruckner 已提交
1233
        input = skip_whitespace(input);
1234
        if (input == NULL)
1235 1236
        {
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1237 1238
        }
    }
1239
    while (*input == ',');
M
Max Bruckner 已提交
1240

1241
    if (*input != ']')
M
Max Bruckner 已提交
1242
    {
1243 1244
        *error_pointer = input;
        goto fail; /* expected end of array */
M
Max Bruckner 已提交
1245 1246
    }

1247 1248
success:
    item->type = cJSON_Array;
1249
    item->child = head;
1250

1251
    return input + 1;
K
Kevin Branigan 已提交
1252

1253
fail:
1254
    if (head != NULL)
1255
    {
1256
        cJSON_Delete(head);
1257 1258
    }

1259
    return NULL;
K
Kevin Branigan 已提交
1260 1261 1262
}

/* Render an array to text */
1263
static cJSON_bool print_array(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1264
{
M
Max Bruckner 已提交
1265
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1266
    size_t length = 0;
M
Max Bruckner 已提交
1267
    cJSON *current_element = item->child;
K
Kevin Branigan 已提交
1268

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

M
Max Bruckner 已提交
1274 1275
    /* Compose the output array. */
    /* opening square bracket */
1276
    output_pointer = ensure(output_buffer, 1, hooks);
M
Max Bruckner 已提交
1277
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1278
    {
1279
        return false;
M
Max Bruckner 已提交
1280 1281
    }

M
Max Bruckner 已提交
1282 1283
    *output_pointer = '[';
    output_buffer->offset++;
M
Max Bruckner 已提交
1284

M
Max Bruckner 已提交
1285
    while (current_element != NULL)
M
Max Bruckner 已提交
1286
    {
1287
        if (!print_value(current_element, depth + 1, format, output_buffer, hooks))
M
Max Bruckner 已提交
1288
        {
1289
            return false;
M
Max Bruckner 已提交
1290
        }
1291
        update_offset(output_buffer);
M
Max Bruckner 已提交
1292
        if (current_element->next)
M
Max Bruckner 已提交
1293
        {
1294
            length = (size_t) (format ? 2 : 1);
1295
            output_pointer = ensure(output_buffer, length + 1, hooks);
M
Max Bruckner 已提交
1296
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1297
            {
1298
                return false;
M
Max Bruckner 已提交
1299
            }
M
Max Bruckner 已提交
1300 1301
            *output_pointer++ = ',';
            if(format)
M
Max Bruckner 已提交
1302
            {
M
Max Bruckner 已提交
1303
                *output_pointer++ = ' ';
M
Max Bruckner 已提交
1304
            }
M
Max Bruckner 已提交
1305 1306
            *output_pointer = '\0';
            output_buffer->offset += length;
M
Max Bruckner 已提交
1307
        }
M
Max Bruckner 已提交
1308
        current_element = current_element->next;
M
Max Bruckner 已提交
1309 1310
    }

1311
    output_pointer = ensure(output_buffer, 2, hooks);
M
Max Bruckner 已提交
1312
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1313
    {
1314
        return false;
M
Max Bruckner 已提交
1315
    }
M
Max Bruckner 已提交
1316 1317
    *output_pointer++ = ']';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1318

1319
    return true;
K
Kevin Branigan 已提交
1320 1321 1322
}

/* Build an object from the text. */
1323
static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
K
Kevin Branigan 已提交
1324
{
1325
    cJSON *head = NULL; /* linked list head */
1326 1327
    cJSON *current_item = NULL;

1328
    if (*input != '{')
M
Max Bruckner 已提交
1329
    {
1330 1331
        *error_pointer = input;
        goto fail; /* not an object */
M
Max Bruckner 已提交
1332 1333
    }

M
Max Bruckner 已提交
1334
    input = skip_whitespace(input + 1);
1335
    if (*input == '}')
M
Max Bruckner 已提交
1336
    {
1337
        goto success; /* empty object */
M
Max Bruckner 已提交
1338 1339
    }

1340
    /* step back to character in front of the first element */
1341
    input--;
1342 1343
    /* loop through the comma separated array elements */
    do
M
Max Bruckner 已提交
1344
    {
1345
        /* allocate next item */
1346
        cJSON *new_item = cJSON_New_Item(hooks);
1347 1348 1349 1350
        if (new_item == NULL)
        {
            goto fail; /* allocation failure */
        }
M
Max Bruckner 已提交
1351

1352 1353
        /* attach next item to list */
        if (head == NULL)
M
Max Bruckner 已提交
1354
        {
1355 1356 1357 1358 1359 1360 1361 1362 1363
            /* 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 已提交
1364 1365
        }

1366
        /* parse the name of the child */
M
Max Bruckner 已提交
1367
        input = skip_whitespace(input + 1);
1368
        input = parse_string(current_item, input, error_pointer, hooks);
M
Max Bruckner 已提交
1369
        input = skip_whitespace(input);
1370
        if (input == NULL)
M
Max Bruckner 已提交
1371
        {
1372
            goto fail; /* faile to parse name */
M
Max Bruckner 已提交
1373 1374
        }

1375 1376 1377
        /* swap valuestring and string, because we parsed the name */
        current_item->string = current_item->valuestring;
        current_item->valuestring = NULL;
M
Max Bruckner 已提交
1378

1379
        if (*input != ':')
M
Max Bruckner 已提交
1380
        {
1381 1382
            *error_pointer = input;
            goto fail; /* invalid object */
M
Max Bruckner 已提交
1383
        }
1384 1385

        /* parse the value */
M
Max Bruckner 已提交
1386
        input = skip_whitespace(input + 1);
1387
        input = parse_value(current_item, input, error_pointer, hooks);
M
Max Bruckner 已提交
1388
        input = skip_whitespace(input);
1389
        if (input == NULL)
M
Max Bruckner 已提交
1390
        {
1391
            goto fail; /* failed to parse value */
M
Max Bruckner 已提交
1392 1393
        }
    }
1394
    while (*input == ',');
1395

1396
    if (*input != '}')
M
Max Bruckner 已提交
1397
    {
1398 1399
        *error_pointer = input;
        goto fail; /* expected end of object */
M
Max Bruckner 已提交
1400 1401
    }

1402 1403
success:
    item->type = cJSON_Object;
1404
    item->child = head;
1405

1406
    return input + 1;
1407 1408

fail:
1409
    if (head != NULL)
1410
    {
1411
        cJSON_Delete(head);
1412 1413
    }

1414
    return NULL;
K
Kevin Branigan 已提交
1415 1416 1417
}

/* Render an object to text. */
1418
static cJSON_bool print_object(const cJSON * const item, const size_t depth, const cJSON_bool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
1419
{
M
Max Bruckner 已提交
1420
    unsigned char *output_pointer = NULL;
M
Max Bruckner 已提交
1421
    size_t length = 0;
M
Max Bruckner 已提交
1422
    cJSON *current_item = item->child;
M
Max Bruckner 已提交
1423

M
Max Bruckner 已提交
1424
    if (output_buffer == NULL)
M
Max Bruckner 已提交
1425
    {
1426
        return false;
M
Max Bruckner 已提交
1427 1428
    }

M
Max Bruckner 已提交
1429
    /* Compose the output: */
1430
    length = (size_t) (format ? 2 : 1); /* fmt: {\n */
1431
    output_pointer = ensure(output_buffer, length + 1, hooks);
M
Max Bruckner 已提交
1432
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1433
    {
1434
        return false;
M
Max Bruckner 已提交
1435 1436
    }

M
Max Bruckner 已提交
1437 1438
    *output_pointer++ = '{';
    if (format)
M
Max Bruckner 已提交
1439
    {
M
Max Bruckner 已提交
1440
        *output_pointer++ = '\n';
M
Max Bruckner 已提交
1441
    }
M
Max Bruckner 已提交
1442
    output_buffer->offset += length;
M
Max Bruckner 已提交
1443

M
Max Bruckner 已提交
1444
    while (current_item)
M
Max Bruckner 已提交
1445
    {
M
Max Bruckner 已提交
1446
        if (format)
M
Max Bruckner 已提交
1447
        {
M
Max Bruckner 已提交
1448
            size_t i;
1449
            output_pointer = ensure(output_buffer, depth + 1, hooks);
M
Max Bruckner 已提交
1450
            if (output_pointer == NULL)
M
Max Bruckner 已提交
1451
            {
1452
                return false;
M
Max Bruckner 已提交
1453
            }
M
Max Bruckner 已提交
1454
            for (i = 0; i < depth + 1; i++)
M
Max Bruckner 已提交
1455
            {
M
Max Bruckner 已提交
1456
                *output_pointer++ = '\t';
M
Max Bruckner 已提交
1457
            }
M
Max Bruckner 已提交
1458
            output_buffer->offset += depth + 1;
M
Max Bruckner 已提交
1459 1460
        }

M
Max Bruckner 已提交
1461
        /* print key */
1462
        if (!print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks))
M
Max Bruckner 已提交
1463
        {
1464
            return false;
M
Max Bruckner 已提交
1465
        }
M
Max Bruckner 已提交
1466
        update_offset(output_buffer);
M
Max Bruckner 已提交
1467

1468
        length = (size_t) (format ? 2 : 1);
1469
        output_pointer = ensure(output_buffer, length, hooks);
M
Max Bruckner 已提交
1470
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1471
        {
1472
            return false;
M
Max Bruckner 已提交
1473
        }
M
Max Bruckner 已提交
1474 1475
        *output_pointer++ = ':';
        if (format)
M
Max Bruckner 已提交
1476
        {
M
Max Bruckner 已提交
1477
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1478
        }
M
Max Bruckner 已提交
1479
        output_buffer->offset += length;
M
Max Bruckner 已提交
1480

M
Max Bruckner 已提交
1481
        /* print value */
1482
        if (!print_value(current_item, depth + 1, format, output_buffer, hooks))
M
Max Bruckner 已提交
1483
        {
1484
            return false;
M
Max Bruckner 已提交
1485
        }
M
Max Bruckner 已提交
1486
        update_offset(output_buffer);
M
Max Bruckner 已提交
1487

M
Max Bruckner 已提交
1488
        /* print comma if not last */
1489
        length = (size_t) ((format ? 1 : 0) + (current_item->next ? 1 : 0));
1490
        output_pointer = ensure(output_buffer, length + 1, hooks);
M
Max Bruckner 已提交
1491
        if (output_pointer == NULL)
M
Max Bruckner 已提交
1492
        {
1493
            return false;
M
Max Bruckner 已提交
1494
        }
M
Max Bruckner 已提交
1495
        if (current_item->next)
M
Max Bruckner 已提交
1496
        {
M
Max Bruckner 已提交
1497
            *output_pointer++ = ',';
M
Max Bruckner 已提交
1498 1499
        }

M
Max Bruckner 已提交
1500
        if (format)
M
Max Bruckner 已提交
1501
        {
M
Max Bruckner 已提交
1502
            *output_pointer++ = '\n';
M
Max Bruckner 已提交
1503
        }
M
Max Bruckner 已提交
1504 1505
        *output_pointer = '\0';
        output_buffer->offset += length;
M
Max Bruckner 已提交
1506

M
Max Bruckner 已提交
1507
        current_item = current_item->next;
M
Max Bruckner 已提交
1508
    }
M
Max Bruckner 已提交
1509

1510
    output_pointer = ensure(output_buffer, format ? (depth + 2) : 2, hooks);
M
Max Bruckner 已提交
1511
    if (output_pointer == NULL)
M
Max Bruckner 已提交
1512
    {
1513
        return false;
M
Max Bruckner 已提交
1514
    }
M
Max Bruckner 已提交
1515
    if (format)
M
Max Bruckner 已提交
1516
    {
M
Max Bruckner 已提交
1517 1518
        size_t i;
        for (i = 0; i < (depth); i++)
M
Max Bruckner 已提交
1519
        {
M
Max Bruckner 已提交
1520
            *output_pointer++ = '\t';
M
Max Bruckner 已提交
1521 1522
        }
    }
M
Max Bruckner 已提交
1523 1524
    *output_pointer++ = '}';
    *output_pointer = '\0';
M
Max Bruckner 已提交
1525

1526
    return true;
K
Kevin Branigan 已提交
1527 1528 1529
}

/* Get Array size/item / object item. */
1530
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
M
Max Bruckner 已提交
1531 1532
{
    cJSON *c = array->child;
1533
    size_t i = 0;
M
Max Bruckner 已提交
1534 1535 1536 1537 1538
    while(c)
    {
        i++;
        c = c->next;
    }
1539 1540 1541

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

M
Max Bruckner 已提交
1542
    return (int)i;
M
Max Bruckner 已提交
1543 1544
}

1545
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item)
M
Max Bruckner 已提交
1546
{
1547
    cJSON *c = array ? array->child : NULL;
M
Max Bruckner 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556
    while (c && item > 0)
    {
        item--;
        c = c->next;
    }

    return c;
}

1557
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string)
M
Max Bruckner 已提交
1558
{
1559
    cJSON *c = object ? object->child : NULL;
1560
    while (c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
M
Max Bruckner 已提交
1561 1562 1563 1564 1565 1566
    {
        c = c->next;
    }
    return c;
}

1567
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
{
    cJSON *current_element = NULL;

    if ((object == NULL) || (string == NULL))
    {
        return NULL;
    }

    current_element = object->child;
    while ((current_element != NULL) && (strcmp(string, current_element->string) != 0))
    {
        current_element = current_element->next;
    }

    return current_element;
}

1585
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
M
Max Bruckner 已提交
1586 1587 1588
{
    return cJSON_GetObjectItem(object, string) ? 1 : 0;
}
K
Kevin Branigan 已提交
1589 1590

/* Utility for array list handling. */
M
Max Bruckner 已提交
1591 1592 1593 1594 1595 1596
static void suffix_object(cJSON *prev, cJSON *item)
{
    prev->next = item;
    item->prev = prev;
}

K
Kevin Branigan 已提交
1597
/* Utility for handling references. */
1598
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
M
Max Bruckner 已提交
1599
{
1600
    cJSON *ref = cJSON_New_Item(hooks);
M
Max Bruckner 已提交
1601 1602
    if (!ref)
    {
1603
        return NULL;
M
Max Bruckner 已提交
1604 1605
    }
    memcpy(ref, item, sizeof(cJSON));
1606
    ref->string = NULL;
M
Max Bruckner 已提交
1607
    ref->type |= cJSON_IsReference;
1608
    ref->next = ref->prev = NULL;
M
Max Bruckner 已提交
1609 1610
    return ref;
}
K
Kevin Branigan 已提交
1611 1612

/* Add item to array/object. */
1613
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
M
Max Bruckner 已提交
1614
{
1615 1616 1617
    cJSON *child = NULL;

    if ((item == NULL) || (array == NULL))
M
Max Bruckner 已提交
1618 1619 1620
    {
        return;
    }
1621 1622 1623 1624

    child = array->child;

    if (child == NULL)
M
Max Bruckner 已提交
1625 1626 1627 1628 1629 1630 1631
    {
        /* list is empty, start new one */
        array->child = item;
    }
    else
    {
        /* append to the end */
1632
        while (child->next)
M
Max Bruckner 已提交
1633
        {
1634
            child = child->next;
M
Max Bruckner 已提交
1635
        }
1636
        suffix_object(child, item);
M
Max Bruckner 已提交
1637 1638 1639
    }
}

1640
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
M
Max Bruckner 已提交
1641
{
1642
    /* call cJSON_AddItemToObjectCS for code reuse */
1643
    cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
1644 1645
    /* remove cJSON_StringIsConst flag */
    item->type &= ~cJSON_StringIsConst;
M
Max Bruckner 已提交
1646 1647
}

1648 1649 1650 1651
#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic push
#endif
#pragma GCC diagnostic ignored "-Wcast-qual"
1652
/* Add an item to an object with constant string as key */
1653
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
1654 1655 1656 1657 1658 1659 1660
{
    if (!item)
    {
        return;
    }
    if (!(item->type & cJSON_StringIsConst) && item->string)
    {
1661
        global_hooks.deallocate(item->string);
1662 1663 1664 1665 1666
    }
    item->string = (char*)string;
    item->type |= cJSON_StringIsConst;
    cJSON_AddItemToArray(object, item);
}
1667 1668 1669
#if defined (__clang__) || ((__GNUC__)  && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
    #pragma GCC diagnostic pop
#endif
1670

1671
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
1672
{
1673
    cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
1674 1675
}

1676
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
1677
{
1678
    cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
1679 1680
}

M
Max Bruckner 已提交
1681
static cJSON *DetachItemFromArray(cJSON *array, size_t which)
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        /* item doesn't exist */
1692
        return NULL;
1693
    }
1694
    if (c->prev)
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
    {
        /* not the first element */
        c->prev->next = c->next;
    }
    if (c->next)
    {
        c->next->prev = c->prev;
    }
    if (c==array->child)
    {
        array->child = c->next;
    }
    /* make sure the detached item doesn't point anywhere anymore */
1708
    c->prev = c->next = NULL;
1709 1710 1711

    return c;
}
1712
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
M
Max Bruckner 已提交
1713 1714 1715 1716 1717 1718 1719 1720
{
    if (which < 0)
    {
        return NULL;
    }

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

1722
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
1723 1724 1725 1726
{
    cJSON_Delete(cJSON_DetachItemFromArray(array, which));
}

1727
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
1728
{
1729
    size_t i = 0;
1730
    cJSON *c = object->child;
1731
    while (c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
1732 1733 1734 1735 1736 1737
    {
        i++;
        c = c->next;
    }
    if (c)
    {
M
Max Bruckner 已提交
1738
        return DetachItemFromArray(object, i);
1739 1740
    }

1741
    return NULL;
1742 1743
}

1744
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
1745 1746 1747
{
    cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}
K
Kevin Branigan 已提交
1748 1749

/* Replace array/object items with new ones. */
1750
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        cJSON_AddItemToArray(array, newitem);
        return;
    }
    newitem->next = c;
    newitem->prev = c->prev;
    c->prev = newitem;
    if (c == array->child)
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }
}

M
Max Bruckner 已提交
1776
static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem)
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        return;
    }
    newitem->next = c->next;
    newitem->prev = c->prev;
    if (newitem->next)
    {
        newitem->next->prev = newitem;
    }
    if (c == array->child)
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }
1802
    c->next = c->prev = NULL;
1803 1804
    cJSON_Delete(c);
}
1805
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
M
Max Bruckner 已提交
1806 1807 1808 1809 1810 1811 1812 1813
{
    if (which < 0)
    {
        return;
    }

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

1815
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
1816
{
1817
    size_t i = 0;
1818
    cJSON *c = object->child;
1819
    while(c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
1820 1821 1822 1823 1824 1825
    {
        i++;
        c = c->next;
    }
    if(c)
    {
1826 1827 1828
        /* free the old string if not const */
        if (!(newitem->type & cJSON_StringIsConst) && newitem->string)
        {
1829
             global_hooks.deallocate(newitem->string);
1830 1831
        }

1832
        newitem->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
M
Max Bruckner 已提交
1833
        ReplaceItemInArray(object, i, newitem);
1834 1835
    }
}
K
Kevin Branigan 已提交
1836 1837

/* Create basic types: */
1838
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
M
Max Bruckner 已提交
1839
{
1840
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1841 1842 1843 1844 1845 1846 1847 1848
    if(item)
    {
        item->type = cJSON_NULL;
    }

    return item;
}

1849
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
M
Max Bruckner 已提交
1850
{
1851
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1852 1853 1854 1855 1856 1857 1858 1859
    if(item)
    {
        item->type = cJSON_True;
    }

    return item;
}

1860
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
M
Max Bruckner 已提交
1861
{
1862
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1863 1864 1865 1866 1867 1868 1869 1870
    if(item)
    {
        item->type = cJSON_False;
    }

    return item;
}

1871
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b)
M
Max Bruckner 已提交
1872
{
1873
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1874 1875 1876 1877 1878 1879 1880 1881
    if(item)
    {
        item->type = b ? cJSON_True : cJSON_False;
    }

    return item;
}

1882
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
M
Max Bruckner 已提交
1883
{
1884
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1885 1886 1887 1888
    if(item)
    {
        item->type = cJSON_Number;
        item->valuedouble = num;
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902

        /* 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 已提交
1903 1904 1905 1906 1907
    }

    return item;
}

1908
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
M
Max Bruckner 已提交
1909
{
1910
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1911 1912 1913
    if(item)
    {
        item->type = cJSON_String;
1914
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
M
Max Bruckner 已提交
1915 1916 1917
        if(!item->valuestring)
        {
            cJSON_Delete(item);
1918
            return NULL;
M
Max Bruckner 已提交
1919 1920 1921 1922 1923 1924
        }
    }

    return item;
}

1925
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
J
Jiri Zouhar 已提交
1926
{
1927
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1928 1929 1930
    if(item)
    {
        item->type = cJSON_Raw;
1931
        item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
M
Max Bruckner 已提交
1932 1933 1934 1935 1936 1937 1938 1939
        if(!item->valuestring)
        {
            cJSON_Delete(item);
            return NULL;
        }
    }

    return item;
J
Jiri Zouhar 已提交
1940 1941
}

1942
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
M
Max Bruckner 已提交
1943
{
1944
    cJSON *item = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
1945 1946 1947 1948 1949 1950 1951 1952
    if(item)
    {
        item->type=cJSON_Array;
    }

    return item;
}

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

    return item;
}
K
Kevin Branigan 已提交
1963 1964

/* Create Arrays: */
1965
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
M
Max Bruckner 已提交
1966
{
1967
    size_t i = 0;
1968 1969
    cJSON *n = NULL;
    cJSON *p = NULL;
1970 1971 1972 1973 1974 1975 1976 1977 1978
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();
    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
1979 1980 1981 1982 1983
    {
        n = cJSON_CreateNumber(numbers[i]);
        if (!n)
        {
            cJSON_Delete(a);
1984
            return NULL;
M
Max Bruckner 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2000
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
M
Max Bruckner 已提交
2001
{
2002
    size_t i = 0;
2003 2004
    cJSON *n = NULL;
    cJSON *p = NULL;
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0; a && (i < (size_t)count); i++)
M
Max Bruckner 已提交
2015
    {
2016
        n = cJSON_CreateNumber((double)numbers[i]);
M
Max Bruckner 已提交
2017 2018 2019
        if(!n)
        {
            cJSON_Delete(a);
2020
            return NULL;
M
Max Bruckner 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2036
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
2037
{
2038
    size_t i = 0;
2039 2040
    cJSON *n = NULL;
    cJSON *p = NULL;
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for(i = 0;a && (i < (size_t)count); i++)
2051 2052 2053 2054 2055
    {
        n = cJSON_CreateNumber(numbers[i]);
        if(!n)
        {
            cJSON_Delete(a);
2056
            return NULL;
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p, n);
        }
        p = n;
    }

    return a;
}

2072
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
2073
{
2074
    size_t i = 0;
2075 2076
    cJSON *n = NULL;
    cJSON *p = NULL;
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
    cJSON *a = NULL;

    if (count < 0)
    {
        return NULL;
    }

    a = cJSON_CreateArray();

    for (i = 0; a && (i < (size_t)count); i++)
2087 2088 2089 2090 2091
    {
        n = cJSON_CreateString(strings[i]);
        if(!n)
        {
            cJSON_Delete(a);
2092
            return NULL;
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
        }
        if(!i)
        {
            a->child = n;
        }
        else
        {
            suffix_object(p,n);
        }
        p = n;
    }

    return a;
}
2107 2108

/* Duplication */
2109
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2110
{
M
Max Bruckner 已提交
2111
    cJSON *newitem = NULL;
2112 2113
    cJSON *child = NULL;
    cJSON *next = NULL;
M
Max Bruckner 已提交
2114
    cJSON *newchild = NULL;
M
Max Bruckner 已提交
2115 2116 2117 2118

    /* Bail on bad ptr */
    if (!item)
    {
2119
        goto fail;
M
Max Bruckner 已提交
2120 2121
    }
    /* Create new item */
2122
    newitem = cJSON_New_Item(&global_hooks);
M
Max Bruckner 已提交
2123 2124
    if (!newitem)
    {
2125
        goto fail;
M
Max Bruckner 已提交
2126 2127 2128 2129 2130 2131 2132
    }
    /* Copy over all vars */
    newitem->type = item->type & (~cJSON_IsReference);
    newitem->valueint = item->valueint;
    newitem->valuedouble = item->valuedouble;
    if (item->valuestring)
    {
2133
        newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
M
Max Bruckner 已提交
2134 2135
        if (!newitem->valuestring)
        {
2136
            goto fail;
M
Max Bruckner 已提交
2137 2138 2139 2140
        }
    }
    if (item->string)
    {
2141
        newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
M
Max Bruckner 已提交
2142 2143
        if (!newitem->string)
        {
2144
            goto fail;
M
Max Bruckner 已提交
2145 2146 2147 2148 2149 2150 2151 2152
        }
    }
    /* If non-recursive, then we're done! */
    if (!recurse)
    {
        return newitem;
    }
    /* Walk the ->next chain for the child. */
2153 2154
    child = item->child;
    while (child != NULL)
M
Max Bruckner 已提交
2155
    {
2156
        newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
M
Max Bruckner 已提交
2157 2158
        if (!newchild)
        {
2159
            goto fail;
M
Max Bruckner 已提交
2160
        }
2161
        if (next != NULL)
M
Max Bruckner 已提交
2162 2163
        {
            /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2164 2165 2166
            next->next = newchild;
            newchild->prev = next;
            next = newchild;
M
Max Bruckner 已提交
2167 2168 2169 2170
        }
        else
        {
            /* Set newitem->child and move to it */
2171 2172
            newitem->child = newchild;
            next = newchild;
M
Max Bruckner 已提交
2173
        }
2174
        child = child->next;
M
Max Bruckner 已提交
2175 2176 2177
    }

    return newitem;
2178 2179 2180 2181 2182 2183 2184 2185

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

    return NULL;
2186
}
2187

2188
CJSON_PUBLIC(void) cJSON_Minify(char *json)
2189
{
2190
    unsigned char *into = (unsigned char*)json;
M
Max Bruckner 已提交
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
    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 已提交
2230
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2231 2232 2233 2234
            while (*json && (*json != '\"'))
            {
                if (*json == '\\')
                {
M
Max Bruckner 已提交
2235
                    *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2236
                }
M
Max Bruckner 已提交
2237
                *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2238
            }
M
Max Bruckner 已提交
2239
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2240 2241 2242 2243
        }
        else
        {
            /* All other characters. */
M
Max Bruckner 已提交
2244
            *into++ = (unsigned char)*json++;
M
Max Bruckner 已提交
2245 2246 2247 2248 2249
        }
    }

    /* and null-terminate. */
    *into = '\0';
2250
}
2251

2252
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
2253 2254 2255 2256 2257 2258 2259 2260 2261
{
    if (item == NULL)
    {
        return false;
    }

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

2262
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
2263 2264 2265 2266 2267 2268 2269 2270 2271
{
    if (item == NULL)
    {
        return false;
    }

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

2272
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
{
    if (item == NULL)
    {
        return false;
    }

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


2283
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
2284 2285 2286 2287 2288 2289 2290 2291
{
    if (item == NULL)
    {
        return false;
    }

    return (item->type & (cJSON_True | cJSON_False)) != 0;
}
2292
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
2293 2294 2295 2296 2297 2298 2299 2300 2301
{
    if (item == NULL)
    {
        return false;
    }

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

2302
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
2303 2304 2305 2306 2307 2308 2309 2310 2311
{
    if (item == NULL)
    {
        return false;
    }

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

2312
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
2313 2314 2315 2316 2317 2318 2319 2320 2321
{
    if (item == NULL)
    {
        return false;
    }

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

2322
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
2323 2324 2325 2326 2327 2328 2329 2330 2331
{
    if (item == NULL)
    {
        return false;
    }

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

2332
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
2333 2334 2335 2336 2337 2338 2339 2340 2341
{
    if (item == NULL)
    {
        return false;
    }

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

2342
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
2343 2344 2345 2346 2347 2348 2349 2350
{
    if (item == NULL)
    {
        return false;
    }

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