2017-03-22 14:46:42 +03:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-03-03 02:45:50 +03:00
|
|
|
#pragma GCC visibility push(default)
|
2015-02-10 15:14:59 +03:00
|
|
|
#include <ctype.h>
|
2015-02-10 17:17:59 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-11 04:29:40 +03:00
|
|
|
#include <stdio.h>
|
2017-02-03 20:34:37 +03:00
|
|
|
#include <limits.h>
|
2017-03-03 02:45:50 +03:00
|
|
|
#pragma GCC visibility pop
|
2017-02-03 20:34:37 +03:00
|
|
|
|
2015-02-09 21:29:05 +03:00
|
|
|
#include "cJSON_Utils.h"
|
|
|
|
|
2017-04-30 12:06:43 +03:00
|
|
|
static unsigned char* cJSONUtils_strdup(const unsigned char* const string)
|
2016-11-04 20:15:57 +03:00
|
|
|
{
|
2017-04-30 12:06:43 +03:00
|
|
|
size_t length = 0;
|
2017-02-03 17:59:00 +03:00
|
|
|
unsigned char *copy = NULL;
|
2016-11-04 20:15:57 +03:00
|
|
|
|
2017-04-30 12:06:43 +03:00
|
|
|
length = strlen((const char*)string) + sizeof("");
|
|
|
|
copy = (unsigned char*) cJSON_malloc(length);
|
|
|
|
if (copy == NULL)
|
2016-11-04 20:15:57 +03:00
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-11-04 20:15:57 +03:00
|
|
|
}
|
2017-04-30 12:06:43 +03:00
|
|
|
memcpy(copy, string, length);
|
2016-11-04 20:15:57 +03:00
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:52:29 +03:00
|
|
|
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
|
|
|
|
static int cJSONUtils_strcasecmp(const unsigned char *string1, const unsigned char *string2)
|
2015-02-13 22:53:27 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
if ((string1 == NULL) || (string2 == NULL))
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
return 1;
|
2016-10-13 19:26:53 +03:00
|
|
|
}
|
2017-04-30 13:52:29 +03:00
|
|
|
|
|
|
|
if (string1 == string2)
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
return 0;
|
2016-10-13 19:26:53 +03:00
|
|
|
}
|
2017-04-30 13:52:29 +03:00
|
|
|
|
|
|
|
for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
2017-04-30 13:52:29 +03:00
|
|
|
if (*string1 == '\0')
|
2016-10-13 19:26:53 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:52:29 +03:00
|
|
|
return tolower(*string1) - tolower(*string2);
|
2015-02-13 22:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 13:53:00 +03:00
|
|
|
/* 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)
|
2015-02-10 15:14:59 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
if ((name == NULL) || (pointer == NULL))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
return 1;
|
2016-10-13 19:40:02 +03:00
|
|
|
}
|
2017-04-30 13:53:00 +03:00
|
|
|
|
|
|
|
for (; (*name != '\0') && (*pointer != '\0') && (*pointer != '/'); (void)name++, pointer++) /* compare until next '/' */
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
if (*pointer == '~')
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
|
|
|
/* check for escaped '~' (~0) and '/' (~1) */
|
2017-04-30 13:53:00 +03:00
|
|
|
if (((pointer[1] != '0') || (*name != '~')) && ((pointer[1] != '1') || (*name != '/')))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
/* invalid escape sequence or wrong character in *name */
|
2016-10-13 19:40:02 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 13:53:00 +03:00
|
|
|
pointer++;
|
2016-10-13 19:40:02 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-30 13:53:00 +03:00
|
|
|
else if (tolower(*name) != tolower(*pointer))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 13:53:00 +03:00
|
|
|
if (((*pointer != 0) && (*pointer != '/')) != (*name != 0))
|
2016-10-13 19:40:02 +03:00
|
|
|
{
|
|
|
|
/* one string has ended, the other not */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-02-10 15:14:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 13:54:00 +03:00
|
|
|
/* 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)
|
2016-10-13 19:42:34 +03:00
|
|
|
{
|
2017-04-30 13:54:00 +03:00
|
|
|
size_t length;
|
|
|
|
for (length = 0; *string != '\0'; (void)string++, length++)
|
2016-10-13 19:42:34 +03:00
|
|
|
{
|
2017-04-30 13:54:00 +03:00
|
|
|
/* character needs to be escaped? */
|
|
|
|
if ((*string == '~') || (*string == '/'))
|
2016-10-13 19:42:34 +03:00
|
|
|
{
|
2017-04-30 13:54:00 +03:00
|
|
|
length++;
|
2016-10-13 19:42:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:54:00 +03:00
|
|
|
return length;
|
2016-10-13 19:42:34 +03:00
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 13:54:44 +03:00
|
|
|
/* 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)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
for (; source[0] != '\0'; (void)source++, destination++)
|
2016-10-13 19:47:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
if (source[0] == '/')
|
2016-10-13 19:47:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[1] = '1';
|
|
|
|
destination++;
|
2016-10-13 19:47:40 +03:00
|
|
|
}
|
2017-04-30 13:54:44 +03:00
|
|
|
else if (source[0] == '~')
|
2016-10-13 19:47:40 +03:00
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[0] = '~';
|
|
|
|
destination[1] = '1';
|
|
|
|
destination++;
|
2016-10-13 19:47:40 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[0] = source[0];
|
2016-10-13 19:47:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:54:44 +03:00
|
|
|
destination[0] = '\0';
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
size_t child_index = 0;
|
|
|
|
cJSON *current_child = 0;
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-13 20:05:58 +03:00
|
|
|
if (object == target)
|
|
|
|
{
|
|
|
|
/* found */
|
2017-02-03 17:59:00 +03:00
|
|
|
return (char*)cJSONUtils_strdup((const unsigned char*)"");
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
/* 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++)
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target);
|
|
|
|
/* found the target? */
|
|
|
|
if (target_pointer != NULL)
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
2017-02-26 23:26:34 +03:00
|
|
|
if (cJSON_IsArray(object))
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
|
|
|
/* reserve enough memory for a 64 bit integer + '/' and '\0' */
|
2017-04-30 13:56:02 +03:00
|
|
|
unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/"));
|
2017-02-15 15:09:32 +03:00
|
|
|
/* 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 */
|
2017-04-30 13:56:02 +03:00
|
|
|
if (child_index > ULONG_MAX)
|
2017-02-15 15:09:32 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
cJSON_free(target_pointer);
|
2017-02-15 15:09:32 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-30 13:56:02 +03:00
|
|
|
sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* /<array_index><path> */
|
|
|
|
cJSON_free(target_pointer);
|
2016-10-13 20:05:58 +03:00
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
return (char*)full_pointer;
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
2017-04-30 13:56:02 +03:00
|
|
|
|
|
|
|
if (cJSON_IsObject(object))
|
2016-10-13 20:05:58 +03:00
|
|
|
{
|
2017-04-30 13:56:02 +03:00
|
|
|
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);
|
2016-10-13 20:05:58 +03:00
|
|
|
|
2017-04-30 13:56:02 +03:00
|
|
|
return (char*)full_pointer;
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reached leaf of the tree, found nothing */
|
2017-04-30 13:56:02 +03:00
|
|
|
cJSON_free(target_pointer);
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-13 20:05:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:21:48 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2017-04-12 13:06:27 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:56:45 +03:00
|
|
|
for (position = 0; (pointer[position] >= '0') && (pointer[0] <= '9'); position++)
|
2017-04-12 13:06:27 +03:00
|
|
|
{
|
|
|
|
parsed_index = (10 * parsed_index) + (size_t)(pointer[position] - '0');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pointer[position] != '\0') && (pointer[position] != '/'))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*index = parsed_index;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:57:49 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer)
|
2015-02-09 21:29:05 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
cJSON *current_element = object;
|
2016-10-13 20:18:15 +03:00
|
|
|
/* follow path of the pointer */
|
2017-04-30 13:57:49 +03:00
|
|
|
while ((pointer[0] == '/') && (current_element != NULL))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
pointer++;
|
|
|
|
if (cJSON_IsArray(current_element))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-12 12:21:48 +03:00
|
|
|
size_t index = 0;
|
2017-04-12 13:06:27 +03:00
|
|
|
if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
2017-04-12 13:06:27 +03:00
|
|
|
|
2017-04-30 13:57:49 +03:00
|
|
|
current_element = get_array_item(current_element, index);
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
2017-04-30 13:57:49 +03:00
|
|
|
else if (cJSON_IsObject(current_element))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
current_element = current_element->child;
|
2016-10-13 20:18:15 +03:00
|
|
|
/* GetObjectItem. */
|
2017-04-30 13:57:49 +03:00
|
|
|
while ((current_element != NULL) && cJSONUtils_Pstrcasecmp((unsigned char*)current_element->string, (const unsigned char*)pointer))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
2017-04-30 13:57:49 +03:00
|
|
|
current_element = current_element->next;
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
|
|
|
/* skip to the next path token or end of string */
|
2017-04-30 13:57:49 +03:00
|
|
|
while ((pointer[0] != '\0') && (pointer[0] != '/'))
|
2016-10-13 20:18:15 +03:00
|
|
|
{
|
|
|
|
pointer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-13 20:18:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 13:57:49 +03:00
|
|
|
return current_element;
|
2015-02-09 21:29:05 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:19:26 +03:00
|
|
|
/* JSON Patch implementation. */
|
2017-02-03 17:59:00 +03:00
|
|
|
static void cJSONUtils_InplaceDecodePointerString(unsigned char *string)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2017-04-30 14:00:16 +03:00
|
|
|
unsigned char *decoded_string = string;
|
2017-01-30 21:29:52 +03:00
|
|
|
|
|
|
|
if (string == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-30 14:00:16 +03:00
|
|
|
for (; *string; (void)decoded_string++, string++)
|
2016-10-15 08:20:47 +03:00
|
|
|
{
|
2017-04-30 14:00:16 +03:00
|
|
|
if (string[0] == '~')
|
|
|
|
{
|
|
|
|
if (string[1] == '0')
|
|
|
|
{
|
|
|
|
decoded_string[0] = '~';
|
|
|
|
}
|
|
|
|
else if (string[1] == '1')
|
|
|
|
{
|
|
|
|
decoded_string[1] = '/';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* invalid escape sequence */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string++;
|
|
|
|
}
|
2016-10-15 08:20:47 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 14:00:16 +03:00
|
|
|
decoded_string[0] = '\0';
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:21:21 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
unsigned char *parentptr = NULL;
|
|
|
|
unsigned char *childptr = NULL;
|
2016-11-13 13:45:40 +03:00
|
|
|
cJSON *parent = NULL;
|
|
|
|
cJSON *ret = NULL;
|
2016-10-16 21:17:31 +03:00
|
|
|
|
|
|
|
/* copy path and split it in parent and child */
|
2016-11-04 20:15:57 +03:00
|
|
|
parentptr = cJSONUtils_strdup(path);
|
2017-01-30 21:30:16 +03:00
|
|
|
if (parentptr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
childptr = (unsigned char*)strrchr((char*)parentptr, '/'); /* last '/' */
|
2017-01-30 21:30:16 +03:00
|
|
|
if (childptr == NULL)
|
2016-10-16 21:17:31 +03:00
|
|
|
{
|
2017-04-12 22:28:31 +03:00
|
|
|
cJSON_free(parentptr);
|
2017-01-30 21:30:16 +03:00
|
|
|
return NULL;
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2017-01-30 21:30:16 +03:00
|
|
|
/* split strings */
|
|
|
|
*childptr++ = '\0';
|
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
parent = cJSONUtils_GetPointer(object, (char*)parentptr);
|
2016-10-16 21:17:31 +03:00
|
|
|
cJSONUtils_InplaceDecodePointerString(childptr);
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-16 21:17:31 +03:00
|
|
|
if (!parent)
|
|
|
|
{
|
|
|
|
/* Couldn't find object to remove child from. */
|
2016-11-13 13:45:40 +03:00
|
|
|
ret = NULL;
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsArray(parent))
|
2016-10-16 21:17:31 +03:00
|
|
|
{
|
2017-04-12 12:21:21 +03:00
|
|
|
size_t index = 0;
|
|
|
|
if (!decode_array_index_from_pointer(childptr, &index))
|
|
|
|
{
|
2017-04-12 22:28:31 +03:00
|
|
|
cJSON_free(parentptr);
|
2017-04-12 12:21:21 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = detach_item_from_array(parent, index);
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsObject(parent))
|
2016-10-16 21:17:31 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
ret = cJSON_DetachItemFromObject(parent, (char*)childptr);
|
2016-10-16 21:17:31 +03:00
|
|
|
}
|
2017-04-12 22:28:31 +03:00
|
|
|
cJSON_free(parentptr);
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-16 21:17:31 +03:00
|
|
|
/* return the detachted item */
|
|
|
|
return ret;
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-02-08 05:00:44 +03:00
|
|
|
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
/* mismatched type. */
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-18 18:48:40 +03:00
|
|
|
switch (a->type & 0xFF)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
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:
|
2017-03-01 10:36:34 +03:00
|
|
|
for ((void)(a = a->child), b = b->child; a && b; (void)(a = a->next), b = b->next)
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2016-11-13 14:05:44 +03:00
|
|
|
int err = 0;
|
2016-10-16 21:23:37 +03:00
|
|
|
/* compare object keys */
|
2017-02-03 17:59:00 +03:00
|
|
|
if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
|
2016-10-16 21:23:37 +03:00
|
|
|
{
|
|
|
|
/* 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;
|
2015-02-13 22:53:27 +03:00
|
|
|
|
2016-10-16 21:23:37 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* null, true or false */
|
|
|
|
return 0;
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:40:43 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:36:14 +03:00
|
|
|
enum patch_operation { INVALID, ADD, REMOVE, REPLACE, MOVE, COPY, TEST };
|
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
cJSON *op = NULL;
|
|
|
|
cJSON *path = NULL;
|
|
|
|
cJSON *value = NULL;
|
|
|
|
cJSON *parent = NULL;
|
2017-04-12 12:36:14 +03:00
|
|
|
enum patch_operation opcode = INVALID;
|
2017-02-03 17:59:00 +03:00
|
|
|
unsigned char *parentptr = NULL;
|
|
|
|
unsigned char *childptr = NULL;
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
op = cJSON_GetObjectItem(patch, "op");
|
|
|
|
path = cJSON_GetObjectItem(patch, "path");
|
2017-04-12 20:14:07 +03:00
|
|
|
if (!cJSON_IsString(op) || !cJSON_IsString(path))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* malformed patch. */
|
|
|
|
return 2;
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* decode operation */
|
|
|
|
if (!strcmp(op->valuestring, "add"))
|
|
|
|
{
|
2017-04-12 12:36:14 +03:00
|
|
|
opcode = ADD;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "remove"))
|
|
|
|
{
|
2017-04-12 12:36:14 +03:00
|
|
|
opcode = REMOVE;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "replace"))
|
|
|
|
{
|
2017-04-12 12:36:14 +03:00
|
|
|
opcode = REPLACE;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "move"))
|
|
|
|
{
|
2017-04-12 12:36:14 +03:00
|
|
|
opcode = MOVE;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "copy"))
|
|
|
|
{
|
2017-04-12 12:36:14 +03:00
|
|
|
opcode = COPY;
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-12 20:14:07 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:36:14 +03:00
|
|
|
if ((opcode == REMOVE) || (opcode == REPLACE))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* Get rid of old. */
|
2017-04-12 11:44:29 +03:00
|
|
|
cJSON *old_item = cJSONUtils_PatchDetach(object, (unsigned char*)path->valuestring);
|
|
|
|
if (old_item == NULL)
|
|
|
|
{
|
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
cJSON_Delete(old_item);
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == REMOVE)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-04-12 11:44:29 +03:00
|
|
|
/* For Remove, this job is done. */
|
2016-10-17 07:32:47 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* Copy/Move uses "from". */
|
2017-04-12 12:36:14 +03:00
|
|
|
if ((opcode == MOVE) || (opcode == COPY))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
cJSON *from = cJSON_GetObjectItem(patch, "from");
|
|
|
|
if (!from)
|
|
|
|
{
|
|
|
|
/* missing "from" for copy/move. */
|
|
|
|
return 4;
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == MOVE)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
value = cJSONUtils_PatchDetach(object, (unsigned char*)from->valuestring);
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == COPY)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
value = cJSONUtils_GetPointer(object, from->valuestring);
|
|
|
|
}
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
/* missing "from" for copy/move. */
|
|
|
|
return 5;
|
|
|
|
}
|
2017-04-12 12:36:14 +03:00
|
|
|
if (opcode == COPY)
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* Now, just add "value" to "path". */
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* split pointer in parent and child */
|
2017-02-03 17:59:00 +03:00
|
|
|
parentptr = cJSONUtils_strdup((unsigned char*)path->valuestring);
|
|
|
|
childptr = (unsigned char*)strrchr((char*)parentptr, '/');
|
2016-10-17 07:32:47 +03:00
|
|
|
if (childptr)
|
|
|
|
{
|
|
|
|
*childptr++ = '\0';
|
|
|
|
}
|
2017-02-03 17:59:00 +03:00
|
|
|
parent = cJSONUtils_GetPointer(object, (char*)parentptr);
|
2016-10-17 07:32:47 +03:00
|
|
|
cJSONUtils_InplaceDecodePointerString(childptr);
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2016-10-17 07:32:47 +03:00
|
|
|
/* add, remove, replace, move, copy, test. */
|
2017-04-05 18:36:25 +03:00
|
|
|
if ((parent == NULL) || (childptr == NULL))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
/* Couldn't find object to add to. */
|
|
|
|
free(parentptr);
|
|
|
|
cJSON_Delete(value);
|
|
|
|
return 9;
|
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsArray(parent))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
if (!strcmp((char*)childptr, "-"))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
|
|
|
cJSON_AddItemToArray(parent, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-12 12:07:21 +03:00
|
|
|
size_t index = 0;
|
|
|
|
if (!decode_array_index_from_pointer(childptr, &index))
|
2017-04-11 19:07:19 +03:00
|
|
|
{
|
|
|
|
free(parentptr);
|
|
|
|
cJSON_Delete(value);
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:07:21 +03:00
|
|
|
if (!insert_item_in_array(parent, index, value))
|
2017-04-11 18:40:43 +03:00
|
|
|
{
|
|
|
|
free(parentptr);
|
|
|
|
cJSON_Delete(value);
|
|
|
|
return 10;
|
|
|
|
}
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
else if (cJSON_IsObject(parent))
|
2016-10-17 07:32:47 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSON_DeleteItemFromObject(parent, (char*)childptr);
|
|
|
|
cJSON_AddItemToObject(parent, (char*)childptr, value);
|
2016-10-17 07:32:47 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cJSON_Delete(value);
|
|
|
|
}
|
|
|
|
free(parentptr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-10 17:17:59 +03:00
|
|
|
|
2017-02-27 22:02:50 +03:00
|
|
|
CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
|
2015-02-10 17:17:59 +03:00
|
|
|
{
|
2016-11-13 14:05:44 +03:00
|
|
|
int err = 0;
|
2017-02-07 21:07:18 +03:00
|
|
|
|
2017-04-11 17:38:51 +03:00
|
|
|
if (!cJSON_IsArray(patches))
|
2016-10-17 07:34:33 +03:00
|
|
|
{
|
|
|
|
/* malformed patches. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (patches)
|
|
|
|
{
|
|
|
|
patches = patches->child;
|
|
|
|
}
|
|
|
|
while (patches)
|
|
|
|
{
|
|
|
|
if ((err = cJSONUtils_ApplyPatch(object, patches)))
|
|
|
|
{
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
patches = patches->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-02-10 17:17:59 +03:00
|
|
|
}
|
2015-02-09 21:29:05 +03:00
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
static void cJSONUtils_GeneratePatch(cJSON *patches, const unsigned char *op, const unsigned char *path, const unsigned char *suffix, cJSON *val)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2016-10-17 13:00:46 +03:00
|
|
|
cJSON *patch = cJSON_CreateObject();
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)op));
|
2016-10-17 13:00:46 +03:00
|
|
|
if (suffix)
|
|
|
|
{
|
2017-04-12 22:28:31 +03:00
|
|
|
unsigned char *newpath = (unsigned char*)cJSON_malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", (const char*)path), suffix);
|
|
|
|
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)newpath));
|
2016-10-17 13:00:46 +03:00
|
|
|
free(newpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
|
2016-10-17 13:00:46 +03:00
|
|
|
}
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
|
|
|
|
}
|
|
|
|
cJSON_AddItemToArray(patches, patch);
|
2015-02-11 04:29:40 +03:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:02:50 +03:00
|
|
|
CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
|
2016-10-17 13:01:52 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(array, (const unsigned char*)op, (const unsigned char*)path, 0, val);
|
2016-10-17 13:01:52 +03:00
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, cJSON *from, cJSON *to)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2017-02-07 21:07:18 +03:00
|
|
|
if ((from == NULL) || (to == NULL))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:48:40 +03:00
|
|
|
if ((from->type & 0xFF) != (to->type & 0xFF))
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 13:35:49 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:48:40 +03:00
|
|
|
switch ((from->type & 0xFF))
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
|
|
|
case cJSON_Number:
|
|
|
|
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
|
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case cJSON_String:
|
|
|
|
if (strcmp(from->valuestring, to->valuestring) != 0)
|
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
return;
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2016-10-17 13:35:49 +03:00
|
|
|
case cJSON_Array:
|
|
|
|
{
|
2017-02-03 18:34:19 +03:00
|
|
|
size_t c = 0;
|
2017-04-12 22:28:31 +03:00
|
|
|
unsigned char *newpath = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */
|
2016-10-17 13:35:49 +03:00
|
|
|
/* generate patches for all array elements that exist in "from" and "to" */
|
2017-03-01 10:36:34 +03:00
|
|
|
for ((void)(c = 0), (void)(from = from->child), to = to->child; from && to; (void)(from = from->next), (void)(to = to->next), c++)
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-15 15:09:32 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
2017-02-09 17:09:06 +03:00
|
|
|
sprintf((char*)newpath, "%s/%lu", path, (unsigned long)c); /* path of the current array element */
|
2016-10-17 13:35:49 +03:00
|
|
|
cJSONUtils_CompareToPatch(patches, newpath, from, to);
|
|
|
|
}
|
|
|
|
/* remove leftover elements from 'from' that are not in 'to' */
|
2017-04-27 22:31:37 +03:00
|
|
|
for (; from; (void)(from = from->next))
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-15 15:09:32 +03:00
|
|
|
/* 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;
|
|
|
|
}
|
2017-02-09 17:09:06 +03:00
|
|
|
sprintf((char*)newpath, "%lu", (unsigned long)c);
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, newpath, 0);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
/* add new elements in 'to' that were not in 'from' */
|
2017-03-01 10:36:34 +03:00
|
|
|
for (; to; (void)(to = to->next), c++)
|
2016-10-17 13:35:49 +03:00
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to);
|
2016-10-17 13:35:49 +03:00
|
|
|
}
|
|
|
|
free(newpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case cJSON_Object:
|
|
|
|
{
|
2016-11-13 14:05:44 +03:00
|
|
|
cJSON *a = NULL;
|
|
|
|
cJSON *b = NULL;
|
2016-10-17 13:35:49 +03:00
|
|
|
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)
|
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string));
|
2016-10-17 13:35:49 +03:00
|
|
|
if (!diff)
|
|
|
|
{
|
|
|
|
/* both object keys are the same */
|
2017-04-12 22:28:31 +03:00
|
|
|
unsigned char *newpath = (unsigned char*)cJSON_malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen((unsigned char*)a->string) + 2);
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", path), (unsigned char*)a->string);
|
2016-10-17 13:35:49 +03:00
|
|
|
/* 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 */
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)a->string, 0);
|
2016-10-17 13:35:49 +03:00
|
|
|
a = a->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* object element doesn't exist in 'from' --> add it */
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)b->string, b);
|
2016-10-17 13:35:49 +03:00
|
|
|
b = b->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-02-27 22:02:50 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
|
2015-02-11 04:29:40 +03:00
|
|
|
{
|
2016-10-17 13:41:50 +03:00
|
|
|
cJSON *patches = cJSON_CreateArray();
|
2017-02-03 17:59:00 +03:00
|
|
|
cJSONUtils_CompareToPatch(patches, (const unsigned char*)"", from, to);
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2016-10-17 13:41:50 +03:00
|
|
|
return patches;
|
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-28 19:27:15 +03:00
|
|
|
/* sort lists using mergesort */
|
2015-02-13 22:19:26 +03:00
|
|
|
static cJSON *cJSONUtils_SortList(cJSON *list)
|
|
|
|
{
|
2016-10-28 19:27:15 +03:00
|
|
|
cJSON *first = list;
|
|
|
|
cJSON *second = list;
|
|
|
|
cJSON *ptr = list;
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-28 19:27:15 +03:00
|
|
|
if (!list || !list->next)
|
|
|
|
{
|
|
|
|
/* One entry is sorted already. */
|
|
|
|
return list;
|
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2017-02-03 17:59:00 +03:00
|
|
|
while (ptr && ptr->next && (cJSONUtils_strcasecmp((unsigned char*)ptr->string, (unsigned char*)ptr->next->string) < 0))
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
/* Test for list sorted. */
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
if (!ptr || !ptr->next)
|
|
|
|
{
|
|
|
|
/* Leave sorted lists unmodified. */
|
|
|
|
return list;
|
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-28 19:27:15 +03:00
|
|
|
/* 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 */
|
2016-11-13 13:45:40 +03:00
|
|
|
second->prev->next = NULL;
|
2016-10-28 19:27:15 +03:00
|
|
|
}
|
2015-02-13 22:19:26 +03:00
|
|
|
|
2016-10-28 19:27:15 +03:00
|
|
|
/* Recursively sort the sub-lists. */
|
|
|
|
first = cJSONUtils_SortList(first);
|
|
|
|
second = cJSONUtils_SortList(second);
|
2016-11-13 13:45:40 +03:00
|
|
|
list = ptr = NULL;
|
2016-10-28 19:27:15 +03:00
|
|
|
|
|
|
|
while (first && second) /* Merge the sub-lists */
|
|
|
|
{
|
2017-02-03 17:59:00 +03:00
|
|
|
if (cJSONUtils_strcasecmp((unsigned char*)first->string, (unsigned char*)second->string) < 0)
|
2016-10-28 19:27:15 +03:00
|
|
|
{
|
|
|
|
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;
|
2015-02-13 22:19:26 +03:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:02:50 +03:00
|
|
|
CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON *object)
|
2016-10-28 19:37:21 +03:00
|
|
|
{
|
|
|
|
object->child = cJSONUtils_SortList(object->child);
|
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-02-27 22:02:50 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
|
2016-05-29 20:16:19 +03:00
|
|
|
{
|
2017-02-26 23:26:34 +03:00
|
|
|
if (!cJSON_IsObject(patch))
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
|
|
|
/* scalar value, array or NULL, just duplicate */
|
|
|
|
cJSON_Delete(target);
|
|
|
|
return cJSON_Duplicate(patch, 1);
|
|
|
|
}
|
2015-02-11 04:29:40 +03:00
|
|
|
|
2017-02-26 23:26:34 +03:00
|
|
|
if (!cJSON_IsObject(target))
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
|
|
|
cJSON_Delete(target);
|
|
|
|
target = cJSON_CreateObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
patch = patch->child;
|
|
|
|
while (patch)
|
|
|
|
{
|
2017-02-26 23:26:34 +03:00
|
|
|
if (cJSON_IsNull(patch))
|
2016-10-29 08:40:41 +03:00
|
|
|
{
|
|
|
|
/* 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;
|
2016-05-29 19:39:54 +03:00
|
|
|
}
|
2016-05-29 20:16:19 +03:00
|
|
|
|
2017-02-27 22:02:50 +03:00
|
|
|
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
|
2016-05-29 20:16:19 +03:00
|
|
|
{
|
2016-11-13 13:45:40 +03:00
|
|
|
cJSON *patch = NULL;
|
2016-10-29 18:15:49 +03:00
|
|
|
if (!to)
|
|
|
|
{
|
|
|
|
/* patch to delete everything */
|
|
|
|
return cJSON_CreateNull();
|
|
|
|
}
|
2017-02-26 23:26:34 +03:00
|
|
|
if (!cJSON_IsObject(to) || !cJSON_IsObject(from))
|
2016-10-29 18:15:49 +03:00
|
|
|
{
|
|
|
|
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);
|
2016-11-13 13:45:40 +03:00
|
|
|
return NULL;
|
2016-10-29 18:15:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return patch;
|
2016-10-13 19:26:53 +03:00
|
|
|
}
|