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

  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.
*/

23
#pragma GCC visibility push(default)
24
#include <ctype.h>
25 26
#include <string.h>
#include <stdlib.h>
27
#include <stdio.h>
M
Max Bruckner 已提交
28
#include <limits.h>
29
#pragma GCC visibility pop
M
Max Bruckner 已提交
30

31 32
#include "cJSON_Utils.h"

M
Max Bruckner 已提交
33
static unsigned char* cJSONUtils_strdup(const unsigned char* const string)
34
{
M
Max Bruckner 已提交
35
    size_t length = 0;
36
    unsigned char *copy = NULL;
37

M
Max Bruckner 已提交
38 39 40
    length = strlen((const char*)string) + sizeof("");
    copy = (unsigned char*) cJSON_malloc(length);
    if (copy == NULL)
41
    {
42
        return NULL;
43
    }
M
Max Bruckner 已提交
44
    memcpy(copy, string, length);
45 46 47 48

    return copy;
}

M
Max Bruckner 已提交
49 50
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
static int cJSONUtils_strcasecmp(const unsigned char *string1, const unsigned char *string2)
51
{
M
Max Bruckner 已提交
52
    if ((string1 == NULL) || (string2 == NULL))
M
Max Bruckner 已提交
53
    {
M
Max Bruckner 已提交
54
        return 1;
M
Max Bruckner 已提交
55
    }
M
Max Bruckner 已提交
56 57

    if (string1 == string2)
M
Max Bruckner 已提交
58
    {
M
Max Bruckner 已提交
59
        return 0;
M
Max Bruckner 已提交
60
    }
M
Max Bruckner 已提交
61 62

    for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
M
Max Bruckner 已提交
63
    {
M
Max Bruckner 已提交
64
        if (*string1 == '\0')
M
Max Bruckner 已提交
65 66 67 68 69
        {
            return 0;
        }
    }

M
Max Bruckner 已提交
70
    return tolower(*string1) - tolower(*string2);
71 72
}

M
Max Bruckner 已提交
73 74
/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */
static int cJSONUtils_Pstrcasecmp(const unsigned char *name, const unsigned char *pointer)
75
{
M
Max Bruckner 已提交
76
    if ((name == NULL) || (pointer == NULL))
77
    {
M
Max Bruckner 已提交
78
        return 1;
79
    }
M
Max Bruckner 已提交
80 81

    for (; (*name != '\0') && (*pointer != '\0') && (*pointer != '/'); (void)name++, pointer++) /* compare until next '/' */
82
    {
M
Max Bruckner 已提交
83
        if (*pointer == '~')
84 85
        {
            /* check for escaped '~' (~0) and '/' (~1) */
M
Max Bruckner 已提交
86
            if (((pointer[1] != '0') || (*name != '~')) && ((pointer[1] != '1') || (*name != '/')))
87
            {
M
Max Bruckner 已提交
88
                /* invalid escape sequence or wrong character in *name */
89 90 91 92
                return 1;
            }
            else
            {
M
Max Bruckner 已提交
93
                pointer++;
94 95
            }
        }
M
Max Bruckner 已提交
96
        else if (tolower(*name) != tolower(*pointer))
97 98 99 100
        {
            return 1;
        }
    }
M
Max Bruckner 已提交
101
    if (((*pointer != 0) && (*pointer != '/')) != (*name != 0))
102 103 104 105 106 107
    {
        /* one string has ended, the other not */
        return 1;
    }

    return 0;
108 109
}

110 111
/* calculate the length of a string if encoded as JSON pointer with ~0 and ~1 escape sequences */
static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *string)
112
{
113 114
    size_t length;
    for (length = 0; *string != '\0'; (void)string++, length++)
115
    {
116 117
        /* character needs to be escaped? */
        if ((*string == '~') || (*string == '/'))
118
        {
119
            length++;
120 121 122
        }
    }

123
    return length;
124
}
125

126 127
/* copy a string while escaping '~' and '/' with ~0 and ~1 JSON pointer escape codes */
static void cJSONUtils_PointerEncodedstrcpy(unsigned char *destination, const unsigned char *source)
128
{
129
    for (; source[0] != '\0'; (void)source++, destination++)
130
    {
131
        if (source[0] == '/')
132
        {
133 134
            destination[1] = '1';
            destination++;
135
        }
136
        else if (source[0] == '~')
137
        {
138 139 140
            destination[0] = '~';
            destination[1] = '1';
            destination++;
141 142 143
        }
        else
        {
144
            destination[0] = source[0];
145 146 147
        }
    }

148
    destination[0] = '\0';
149 150
}

151
CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target)
152
{
153 154
    size_t child_index = 0;
    cJSON *current_child = 0;
155

156 157 158
    if (object == target)
    {
        /* found */
159
        return (char*)cJSONUtils_strdup((const unsigned char*)"");
160
    }
161

162 163
    /* recursively search all children of the object or array */
    for (current_child = object->child; current_child != NULL; (void)(current_child = current_child->next), child_index++)
164
    {
165 166 167
        unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target);
        /* found the target? */
        if (target_pointer != NULL)
168
        {
169
            if (cJSON_IsArray(object))
170 171
            {
                /* reserve enough memory for a 64 bit integer + '/' and '\0' */
172
                unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/"));
173 174 175
                /* check if conversion to unsigned long is valid
                 * This should be eliminated at compile time by dead code elimination
                 * if size_t is an alias of unsigned long, or if it is bigger */
176
                if (child_index > ULONG_MAX)
177
                {
178
                    cJSON_free(target_pointer);
179 180
                    return NULL;
                }
181 182
                sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* /<array_index><path> */
                cJSON_free(target_pointer);
183

184
                return (char*)full_pointer;
185
            }
186 187

            if (cJSON_IsObject(object))
188
            {
189 190 191 192 193
                unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + cJSONUtils_PointerEncodedstrlen((unsigned char*)current_child->string) + 2);
                full_pointer[0] = '/';
                cJSONUtils_PointerEncodedstrcpy(full_pointer + 1, (unsigned char*)current_child->string);
                strcat((char*)full_pointer, (char*)target_pointer);
                cJSON_free(target_pointer);
194

195
                return (char*)full_pointer;
196 197 198
            }

            /* reached leaf of the tree, found nothing */
199
            cJSON_free(target_pointer);
200
            return NULL;
201 202 203 204
        }
    }

    /* not found */
205
    return NULL;
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219 220
/* non broken version of cJSON_GetArrayItem */
static cJSON *get_array_item(const cJSON *array, size_t item)
{
    cJSON *child = array ? array->child : NULL;
    while ((child != NULL) && (item > 0))
    {
        item--;
        child = child->next;
    }

    return child;
}

221 222 223 224 225 226 227 228 229 230 231
static cJSON_bool decode_array_index_from_pointer(const unsigned char * const pointer, size_t * const index)
{
    size_t parsed_index = 0;
    size_t position = 0;

    if ((pointer[0] == '0') && ((pointer[1] != '\0') && (pointer[1] != '/')))
    {
        /* leading zeroes are not permitted */
        return 0;
    }

232
    for (position = 0; (pointer[position] >= '0') && (pointer[0] <= '9'); position++)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    {
        parsed_index = (10 * parsed_index) + (size_t)(pointer[position] - '0');

    }

    if ((pointer[position] != '\0') && (pointer[position] != '/'))
    {
        return 0;
    }

    *index = parsed_index;

    return 1;
}

M
Max Bruckner 已提交
248
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer)
249
{
M
Max Bruckner 已提交
250
    cJSON *current_element = object;
251
    /* follow path of the pointer */
M
Max Bruckner 已提交
252
    while ((pointer[0] == '/') && (current_element != NULL))
253
    {
M
Max Bruckner 已提交
254 255
        pointer++;
        if (cJSON_IsArray(current_element))
256
        {
257
            size_t index = 0;
258
            if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index))
259
            {
260
                return NULL;
261
            }
262

M
Max Bruckner 已提交
263
            current_element = get_array_item(current_element, index);
264
        }
M
Max Bruckner 已提交
265
        else if (cJSON_IsObject(current_element))
266
        {
M
Max Bruckner 已提交
267
            current_element = current_element->child;
268
            /* GetObjectItem. */
M
Max Bruckner 已提交
269
            while ((current_element != NULL) && cJSONUtils_Pstrcasecmp((unsigned char*)current_element->string, (const unsigned char*)pointer))
270
            {
M
Max Bruckner 已提交
271
                current_element = current_element->next;
272 273
            }
            /* skip to the next path token or end of string */
M
Max Bruckner 已提交
274
            while ((pointer[0] != '\0') && (pointer[0] != '/'))
275 276 277 278 279 280
            {
                pointer++;
            }
        }
        else
        {
281
            return NULL;
282 283 284
        }
    }

M
Max Bruckner 已提交
285
    return current_element;
286 287
}

288
/* JSON Patch implementation. */
289
static void cJSONUtils_InplaceDecodePointerString(unsigned char *string)
290
{
291
    unsigned char *decoded_string = string;
292 293 294 295 296

    if (string == NULL) {
        return;
    }

297
    for (; *string; (void)decoded_string++, string++)
298
    {
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        if (string[0] == '~')
        {
            if (string[1] == '0')
            {
                decoded_string[0] = '~';
            }
            else if (string[1] == '1')
            {
                decoded_string[1] = '/';
            }
            else
            {
                /* invalid escape sequence */
                return;
            }

            string++;
        }
317 318
    }

319
    decoded_string[0] = '\0';
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/* non-broken cJSON_DetachItemFromArray */
static cJSON *detach_item_from_array(cJSON *array, size_t which)
{
    cJSON *c = array->child;
    while (c && (which > 0))
    {
        c = c->next;
        which--;
    }
    if (!c)
    {
        /* item doesn't exist */
        return NULL;
    }
    if (c->prev)
    {
        /* 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 */
    c->prev = c->next = NULL;

    return c;
}

355
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
356
{
M
Max Bruckner 已提交
357 358
    unsigned char *parent_pointer = NULL;
    unsigned char *child_pointer = NULL;
359
    cJSON *parent = NULL;
M
Max Bruckner 已提交
360
    cJSON *detached_item = NULL;
361 362

    /* copy path and split it in parent and child */
M
Max Bruckner 已提交
363 364 365
    parent_pointer = cJSONUtils_strdup(path);
    if (parent_pointer == NULL) {
        goto cleanup;
366 367
    }

M
Max Bruckner 已提交
368 369
    child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */
    if (child_pointer == NULL)
370
    {
M
Max Bruckner 已提交
371
        goto cleanup;
372
    }
373
    /* split strings */
M
Max Bruckner 已提交
374 375
    child_pointer[0] = '\0';
    child_pointer++;
376

M
Max Bruckner 已提交
377 378
    parent = cJSONUtils_GetPointer(object, (char*)parent_pointer);
    cJSONUtils_InplaceDecodePointerString(child_pointer);
379

M
Max Bruckner 已提交
380
    if (cJSON_IsArray(parent))
381
    {
382
        size_t index = 0;
M
Max Bruckner 已提交
383
        if (!decode_array_index_from_pointer(child_pointer, &index))
384
        {
M
Max Bruckner 已提交
385
            goto cleanup;
386
        }
M
Max Bruckner 已提交
387
        detached_item = detach_item_from_array(parent, index);
388
    }
389
    else if (cJSON_IsObject(parent))
390
    {
M
Max Bruckner 已提交
391 392 393 394 395 396 397 398 399 400 401 402
        detached_item = cJSON_DetachItemFromObject(parent, (char*)child_pointer);
    }
    else
    {
        /* Couldn't find object to remove child from. */
        goto cleanup;
    }

cleanup:
    if (parent_pointer != NULL)
    {
        cJSON_free(parent_pointer);
403
    }
404

M
Max Bruckner 已提交
405
    return detached_item;
406 407
}

M
Max Bruckner 已提交
408
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
409
{
410
    if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
M
Max Bruckner 已提交
411 412 413 414
    {
        /* mismatched type. */
        return -1;
    }
415
    switch (a->type & 0xFF)
M
Max Bruckner 已提交
416 417 418 419 420 421 422 423
    {
        case cJSON_Number:
            /* numeric mismatch. */
            return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
        case cJSON_String:
            /* string mismatch. */
            return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
        case cJSON_Array:
M
Max Bruckner 已提交
424
            for ((void)(a = a->child), b = b->child; a && b; (void)(a = a->next), b = b->next)
M
Max Bruckner 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
            {
                int err = cJSONUtils_Compare(a, b);
                if (err)
                {
                    return err;
                }
            }
            /* array size mismatch? (one of both children is not NULL) */
            return (a || b) ? -4 : 0;
        case cJSON_Object:
            cJSONUtils_SortObject(a);
            cJSONUtils_SortObject(b);
            a = a->child;
            b = b->child;
            while (a && b)
            {
M
Max Bruckner 已提交
441
                int err = 0;
M
Max Bruckner 已提交
442
                /* compare object keys */
443
                if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
M
Max Bruckner 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457
                {
                    /* missing member */
                    return -6;
                }
                err = cJSONUtils_Compare(a, b);
                if (err)
                {
                    return err;
                }
                a = a->next;
                b = b->next;
            }
            /* object length mismatch (one of both children is not null) */
            return (a || b) ? -5 : 0;
458

M
Max Bruckner 已提交
459 460 461 462 463
        default:
            break;
    }
    /* null, true or false */
    return 0;
464 465
}

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
/* non broken version of cJSON_InsertItemInArray */
static cJSON_bool insert_item_in_array(cJSON *array, size_t which, cJSON *newitem)
{
    cJSON *child = array->child;
    while (child && (which > 0))
    {
        child = child->next;
        which--;
    }
    if (which > 0)
    {
        /* item is after the end of the array */
        return 0;
    }
    if (child == NULL)
    {
        cJSON_AddItemToArray(array, newitem);
        return 1;
    }

    /* insert into the linked list */
    newitem->next = child;
    newitem->prev = child->prev;
    child->prev = newitem;

    /* was it at the beginning */
    if (child == array->child)
    {
        array->child = newitem;
    }
    else
    {
        newitem->prev->next = newitem;
    }

    return 1;
}

M
Max Bruckner 已提交
504 505
enum patch_operation { INVALID, ADD, REMOVE, REPLACE, MOVE, COPY, TEST };

M
Max Bruckner 已提交
506
static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
507
{
508 509 510 511
    cJSON *op = NULL;
    cJSON *path = NULL;
    cJSON *value = NULL;
    cJSON *parent = NULL;
M
Max Bruckner 已提交
512
    enum patch_operation opcode = INVALID;
513 514
    unsigned char *parentptr = NULL;
    unsigned char *childptr = NULL;
515

M
Max Bruckner 已提交
516 517
    op = cJSON_GetObjectItem(patch, "op");
    path = cJSON_GetObjectItem(patch, "path");
518
    if (!cJSON_IsString(op) || !cJSON_IsString(path))
M
Max Bruckner 已提交
519 520 521 522
    {
        /* malformed patch. */
        return 2;
    }
523

M
Max Bruckner 已提交
524 525 526
    /* decode operation */
    if (!strcmp(op->valuestring, "add"))
    {
M
Max Bruckner 已提交
527
        opcode = ADD;
M
Max Bruckner 已提交
528 529 530
    }
    else if (!strcmp(op->valuestring, "remove"))
    {
M
Max Bruckner 已提交
531
        opcode = REMOVE;
M
Max Bruckner 已提交
532 533 534
    }
    else if (!strcmp(op->valuestring, "replace"))
    {
M
Max Bruckner 已提交
535
        opcode = REPLACE;
M
Max Bruckner 已提交
536 537 538
    }
    else if (!strcmp(op->valuestring, "move"))
    {
M
Max Bruckner 已提交
539
        opcode = MOVE;
M
Max Bruckner 已提交
540 541 542
    }
    else if (!strcmp(op->valuestring, "copy"))
    {
M
Max Bruckner 已提交
543
        opcode = COPY;
M
Max Bruckner 已提交
544 545 546 547 548 549 550 551 552 553 554
    }
    else if (!strcmp(op->valuestring, "test"))
    {
        /* compare value: {...} with the given path */
        return cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
    }
    else
    {
        /* unknown opcode. */
        return 3;
    }
555

556 557 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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    /* special case for replacing the root */
    if (path->valuestring[0] == '\0')
    {
        if (opcode == REMOVE)
        {
            /* remove possible children */
            if (object->child != NULL)
            {
                cJSON_Delete(object->child);
            }

            /* remove other allocated resources */
            if (object->string != NULL)
            {
                cJSON_free(object->string);
            }
            if (object->valuestring != NULL)
            {
                cJSON_free(object->valuestring);
            }

            /* make it invalid */
            memset(object, '\0', sizeof(cJSON));

            return 0;
        }

        if ((opcode == REPLACE) || (opcode == ADD))
        {
            /* remove possible children */
            if (object->child != NULL)
            {
                cJSON_Delete(object->child);
            }

            /* remove other allocated resources */
            if (object->string != NULL)
            {
                cJSON_free(object->string);
            }
            if (object->valuestring != NULL)
            {
                cJSON_free(object->valuestring);
            }

            value = cJSON_GetObjectItem(patch, "value");
            if (value == NULL)
            {
                /* missing "value" for add/replace. */
                return 7;
            }

            value = cJSON_Duplicate(value, 1);
            if (value == NULL)
            {
                /* out of memory for add/replace. */
                return 8;
            }
            /* the string "value" isn't needed */
            if (value->string != NULL)
            {
                cJSON_free(value->string);
                value->string = NULL;
            }

            /* copy over the value object */
            memcpy(object, value, sizeof(cJSON));

            /* delete the duplicated value */
            cJSON_free(value);

            return 0;
        }
    }

M
Max Bruckner 已提交
631
    if ((opcode == REMOVE) || (opcode == REPLACE))
M
Max Bruckner 已提交
632 633
    {
        /* Get rid of old. */
634 635 636 637 638 639
        cJSON *old_item = cJSONUtils_PatchDetach(object, (unsigned char*)path->valuestring);
        if (old_item == NULL)
        {
            return 13;
        }
        cJSON_Delete(old_item);
M
Max Bruckner 已提交
640
        if (opcode == REMOVE)
M
Max Bruckner 已提交
641
        {
642
            /* For Remove, this job is done. */
M
Max Bruckner 已提交
643 644 645
            return 0;
        }
    }
646

M
Max Bruckner 已提交
647
    /* Copy/Move uses "from". */
M
Max Bruckner 已提交
648
    if ((opcode == MOVE) || (opcode == COPY))
M
Max Bruckner 已提交
649 650 651 652 653 654 655
    {
        cJSON *from = cJSON_GetObjectItem(patch, "from");
        if (!from)
        {
            /* missing "from" for copy/move. */
            return 4;
        }
656

M
Max Bruckner 已提交
657
        if (opcode == MOVE)
M
Max Bruckner 已提交
658
        {
659
            value = cJSONUtils_PatchDetach(object, (unsigned char*)from->valuestring);
M
Max Bruckner 已提交
660
        }
M
Max Bruckner 已提交
661
        if (opcode == COPY)
M
Max Bruckner 已提交
662 663 664 665 666 667 668 669
        {
            value = cJSONUtils_GetPointer(object, from->valuestring);
        }
        if (!value)
        {
            /* missing "from" for copy/move. */
            return 5;
        }
M
Max Bruckner 已提交
670
        if (opcode == COPY)
M
Max Bruckner 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
        {
            value = cJSON_Duplicate(value, 1);
        }
        if (!value)
        {
            /* out of memory for copy/move. */
            return 6;
        }
    }
    else /* Add/Replace uses "value". */
    {
        value = cJSON_GetObjectItem(patch, "value");
        if (!value)
        {
            /* missing "value" for add/replace. */
            return 7;
        }
        value = cJSON_Duplicate(value, 1);
        if (!value)
        {
            /* out of memory for add/replace. */
            return 8;
        }
    }
695

M
Max Bruckner 已提交
696
    /* Now, just add "value" to "path". */
697

M
Max Bruckner 已提交
698
    /* split pointer in parent and child */
699 700
    parentptr = cJSONUtils_strdup((unsigned char*)path->valuestring);
    childptr = (unsigned char*)strrchr((char*)parentptr, '/');
M
Max Bruckner 已提交
701 702 703 704
    if (childptr)
    {
        *childptr++ = '\0';
    }
705
    parent = cJSONUtils_GetPointer(object, (char*)parentptr);
M
Max Bruckner 已提交
706
    cJSONUtils_InplaceDecodePointerString(childptr);
707

M
Max Bruckner 已提交
708
    /* add, remove, replace, move, copy, test. */
709
    if ((parent == NULL) || (childptr == NULL))
M
Max Bruckner 已提交
710 711 712 713 714 715
    {
        /* Couldn't find object to add to. */
        free(parentptr);
        cJSON_Delete(value);
        return 9;
    }
716
    else if (cJSON_IsArray(parent))
M
Max Bruckner 已提交
717
    {
718
        if (!strcmp((char*)childptr, "-"))
M
Max Bruckner 已提交
719 720 721 722 723
        {
            cJSON_AddItemToArray(parent, value);
        }
        else
        {
724 725
            size_t index = 0;
            if (!decode_array_index_from_pointer(childptr, &index))
726 727 728 729 730 731
            {
                free(parentptr);
                cJSON_Delete(value);
                return 11;
            }

732
            if (!insert_item_in_array(parent, index, value))
733 734 735 736 737
            {
                free(parentptr);
                cJSON_Delete(value);
                return 10;
            }
M
Max Bruckner 已提交
738 739
        }
    }
740
    else if (cJSON_IsObject(parent))
M
Max Bruckner 已提交
741
    {
742 743
        cJSON_DeleteItemFromObject(parent, (char*)childptr);
        cJSON_AddItemToObject(parent, (char*)childptr, value);
M
Max Bruckner 已提交
744 745 746 747 748 749 750 751 752
    }
    else
    {
        cJSON_Delete(value);
    }
    free(parentptr);

    return 0;
}
753

754
CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
755
{
M
Max Bruckner 已提交
756
    int err = 0;
757

758
    if (!cJSON_IsArray(patches))
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    {
        /* malformed patches. */
        return 1;
    }
    if (patches)
    {
        patches = patches->child;
    }
    while (patches)
    {
        if ((err = cJSONUtils_ApplyPatch(object, patches)))
        {
            return err;
        }
        patches = patches->next;
    }

    return 0;
777
}
778

779
static void cJSONUtils_GeneratePatch(cJSON *patches, const unsigned char *op, const unsigned char *path, const unsigned char *suffix, cJSON *val)
780
{
781
    cJSON *patch = cJSON_CreateObject();
782
    cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)op));
783 784
    if (suffix)
    {
785
        unsigned char *newpath = (unsigned char*)cJSON_malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
786 787
        cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", (const char*)path), suffix);
        cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)newpath));
788 789 790 791
        free(newpath);
    }
    else
    {
792
        cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
793 794 795 796 797 798
    }
    if (val)
    {
        cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
    }
    cJSON_AddItemToArray(patches, patch);
799 800
}

801
CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
M
Max Bruckner 已提交
802
{
803
    cJSONUtils_GeneratePatch(array, (const unsigned char*)op, (const unsigned char*)path, 0, val);
M
Max Bruckner 已提交
804
}
805

806
static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, cJSON *from, cJSON *to)
807
{
808 809 810 811 812
    if ((from == NULL) || (to == NULL))
    {
        return;
    }

813
    if ((from->type & 0xFF) != (to->type & 0xFF))
814
    {
815
        cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
816 817 818
        return;
    }

819
    switch ((from->type & 0xFF))
820 821 822 823
    {
        case cJSON_Number:
            if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
            {
824
                cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
825 826 827 828 829 830
            }
            return;

        case cJSON_String:
            if (strcmp(from->valuestring, to->valuestring) != 0)
            {
831
                cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
832 833
            }
            return;
834

835 836
        case cJSON_Array:
        {
837
            size_t c = 0;
838
            unsigned char *newpath = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */
839
            /* generate patches for all array elements that exist in "from" and "to" */
M
Max Bruckner 已提交
840
            for ((void)(c = 0), (void)(from = from->child), to = to->child; from && to; (void)(from = from->next), (void)(to = to->next), c++)
841
            {
842 843 844 845 846 847 848 849
                /* check if conversion to unsigned long is valid
                 * This should be eliminated at compile time by dead code elimination
                 * if size_t is an alias of unsigned long, or if it is bigger */
                if (c > ULONG_MAX)
                {
                    free(newpath);
                    return;
                }
850
                sprintf((char*)newpath, "%s/%lu", path, (unsigned long)c); /* path of the current array element */
851 852 853
                cJSONUtils_CompareToPatch(patches, newpath, from, to);
            }
            /* remove leftover elements from 'from' that are not in 'to' */
854
            for (; from; (void)(from = from->next))
855
            {
856 857 858 859 860 861 862 863
                /* check if conversion to unsigned long is valid
                 * This should be eliminated at compile time by dead code elimination
                 * if size_t is an alias of unsigned long, or if it is bigger */
                if (c > ULONG_MAX)
                {
                    free(newpath);
                    return;
                }
864
                sprintf((char*)newpath, "%lu", (unsigned long)c);
865
                cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, newpath, 0);
866 867
            }
            /* add new elements in 'to' that were not in 'from' */
M
Max Bruckner 已提交
868
            for (; to; (void)(to = to->next), c++)
869
            {
870
                cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to);
871 872 873 874 875 876 877
            }
            free(newpath);
            return;
        }

        case cJSON_Object:
        {
M
Max Bruckner 已提交
878 879
            cJSON *a = NULL;
            cJSON *b = NULL;
880 881 882 883 884 885 886 887
            cJSONUtils_SortObject(from);
            cJSONUtils_SortObject(to);

            a = from->child;
            b = to->child;
            /* for all object values in the object with more of them */
            while (a || b)
            {
888
                int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string));
889 890 891
                if (!diff)
                {
                    /* both object keys are the same */
892
                    unsigned char *newpath = (unsigned char*)cJSON_malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen((unsigned char*)a->string) + 2);
893
                    cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", path), (unsigned char*)a->string);
894 895 896 897 898 899 900 901 902
                    /* create a patch for the element */
                    cJSONUtils_CompareToPatch(patches, newpath, a, b);
                    free(newpath);
                    a = a->next;
                    b = b->next;
                }
                else if (diff < 0)
                {
                    /* object element doesn't exist in 'to' --> remove it */
903
                    cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)a->string, 0);
904 905 906 907 908
                    a = a->next;
                }
                else
                {
                    /* object element doesn't exist in 'from' --> add it */
909
                    cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)b->string, b);
910 911 912 913 914 915 916 917 918 919
                    b = b->next;
                }
            }
            return;
        }

        default:
            break;
    }
}
920

921
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
922
{
923
    cJSON *patches = cJSON_CreateArray();
924
    cJSONUtils_CompareToPatch(patches, (const unsigned char*)"", from, to);
925

926 927
    return patches;
}
928

M
Max Bruckner 已提交
929
/* sort lists using mergesort */
930 931
static cJSON *cJSONUtils_SortList(cJSON *list)
{
M
Max Bruckner 已提交
932 933 934
    cJSON *first = list;
    cJSON *second = list;
    cJSON *ptr = list;
935

M
Max Bruckner 已提交
936 937 938 939 940
    if (!list || !list->next)
    {
        /* One entry is sorted already. */
        return list;
    }
941

942
    while (ptr && ptr->next && (cJSONUtils_strcasecmp((unsigned char*)ptr->string, (unsigned char*)ptr->next->string) < 0))
M
Max Bruckner 已提交
943 944 945 946 947 948 949 950 951
    {
        /* Test for list sorted. */
        ptr = ptr->next;
    }
    if (!ptr || !ptr->next)
    {
        /* Leave sorted lists unmodified. */
        return list;
    }
952

M
Max Bruckner 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
    /* reset ptr to the beginning */
    ptr = list;
    while (ptr)
    {
        /* Walk two pointers to find the middle. */
        second = second->next;
        ptr = ptr->next;
        /* advances ptr two steps at a time */
        if (ptr)
        {
            ptr = ptr->next;
        }
    }
    if (second && second->prev)
    {
        /* Split the lists */
969
        second->prev->next = NULL;
M
Max Bruckner 已提交
970
    }
971

M
Max Bruckner 已提交
972 973 974
    /* Recursively sort the sub-lists. */
    first = cJSONUtils_SortList(first);
    second = cJSONUtils_SortList(second);
975
    list = ptr = NULL;
M
Max Bruckner 已提交
976 977 978

    while (first && second) /* Merge the sub-lists */
    {
979
        if (cJSONUtils_strcasecmp((unsigned char*)first->string, (unsigned char*)second->string) < 0)
M
Max Bruckner 已提交
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        {
            if (!list)
            {
                /* start merged list with the first element of the first list */
                list = ptr = first;
            }
            else
            {
                /* add first element of first list to merged list */
                ptr->next = first;
                first->prev = ptr;
                ptr = first;
            }
            first = first->next;
        }
        else
        {
            if (!list)
            {
                /* start merged list with the first element of the second list */
                list = ptr = second;
            }
            else
            {
                /* add first element of second list to merged list */
                ptr->next = second;
                second->prev = ptr;
                ptr = second;
            }
            second = second->next;
        }
    }
    if (first)
    {
        /* Append rest of first list. */
        if (!list)
        {
            return first;
        }
        ptr->next = first;
        first->prev = ptr;
    }
    if (second)
    {
        /* Append rest of second list */
        if (!list)
        {
            return second;
        }
        ptr->next = second;
        second->prev = ptr;
    }

    return list;
1034 1035
}

1036
CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON *object)
M
Max Bruckner 已提交
1037 1038 1039
{
    object->child = cJSONUtils_SortList(object->child);
}
1040

1041
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
1042
{
1043
    if (!cJSON_IsObject(patch))
M
Max Bruckner 已提交
1044 1045 1046 1047 1048
    {
        /* scalar value, array or NULL, just duplicate */
        cJSON_Delete(target);
        return cJSON_Duplicate(patch, 1);
    }
1049

1050
    if (!cJSON_IsObject(target))
M
Max Bruckner 已提交
1051 1052 1053 1054 1055 1056 1057 1058
    {
        cJSON_Delete(target);
        target = cJSON_CreateObject();
    }

    patch = patch->child;
    while (patch)
    {
1059
        if (cJSON_IsNull(patch))
M
Max Bruckner 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
        {
            /* NULL is the indicator to remove a value, see RFC7396 */
            cJSON_DeleteItemFromObject(target, patch->string);
        }
        else
        {
            cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
            cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
        }
        patch = patch->next;
    }
    return target;
1072
}
1073

1074
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
1075
{
1076
    cJSON *patch = NULL;
1077 1078 1079 1080 1081
    if (!to)
    {
        /* patch to delete everything */
        return cJSON_CreateNull();
    }
1082
    if (!cJSON_IsObject(to) || !cJSON_IsObject(from))
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    {
        return cJSON_Duplicate(to, 1);
    }

    cJSONUtils_SortObject(from);
    cJSONUtils_SortObject(to);

    from = from->child;
    to = to->child;
    patch = cJSON_CreateObject();
    while (from || to)
    {
        int compare = from ? (to ? strcmp(from->string, to->string) : -1) : 1;
        if (compare < 0)
        {
            /* from has a value that to doesn't have -> remove */
            cJSON_AddItemToObject(patch, from->string, cJSON_CreateNull());
            from = from->next;
        }
        else if (compare > 0)
        {
            /* to has a value that from doesn't have -> add to patch */
            cJSON_AddItemToObject(patch, to->string, cJSON_Duplicate(to, 1));
            to = to->next;
        }
        else
        {
            /* object key exists in both objects */
            if (cJSONUtils_Compare(from, to))
            {
                /* not identical --> generate a patch */
                cJSON_AddItemToObject(patch, to->string, cJSONUtils_GenerateMergePatch(from, to));
            }
            /* next key in the object */
            from = from->next;
            to = to->next;
        }
    }
    if (!patch->child)
    {
        cJSON_Delete(patch);
1124
        return NULL;
1125 1126 1127
    }

    return patch;
M
Max Bruckner 已提交
1128
}