cJSON/cJSON_Utils.c

1450 lines
39 KiB
C
Raw Normal View History

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.
*/
/* disable warnings about old C89 functions in MSVC */
#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifdef __GNUCC__
#pragma GCC visibility push(default)
#endif
#if defined(_MSC_VER)
#pragma warning (push)
/* disable warning about single line comments in system headers */
#pragma warning (disable : 4001)
#endif
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
#ifdef __GNUCC__
#pragma GCC visibility pop
#endif
#include "cJSON_Utils.h"
2017-04-30 15:19:01 +03:00
/* define our own boolean type */
#define true ((cJSON_bool)1)
#define false ((cJSON_bool)0)
2017-04-30 12:06:43 +03:00
static unsigned char* cJSONUtils_strdup(const unsigned char* const string)
{
2017-04-30 12:06:43 +03:00
size_t length = 0;
unsigned char *copy = NULL;
2017-04-30 12:06:43 +03:00
length = strlen((const char*)string) + sizeof("");
copy = (unsigned char*) cJSON_malloc(length);
if (copy == NULL)
{
return NULL;
}
2017-04-30 12:06:43 +03:00
memcpy(copy, string, length);
return copy;
}
/* string comparison which doesn't consider NULL pointers equal */
static int compare_strings(const unsigned char *string1, const unsigned char *string2, const cJSON_bool case_sensitive)
{
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
if (case_sensitive)
{
return strcmp((const char*)string1, (const char*)string2);
}
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);
}
2017-04-30 13:53:00 +03:00
/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */
static cJSON_bool compare_pointers(const unsigned char *name, const unsigned char *pointer, const cJSON_bool case_sensitive)
{
2017-04-30 13:53:00 +03:00
if ((name == NULL) || (pointer == NULL))
2016-10-13 19:40:02 +03:00
{
return false;
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 */
return false;
2016-10-13 19:40:02 +03:00
}
else
{
2017-04-30 13:53:00 +03:00
pointer++;
2016-10-13 19:40:02 +03:00
}
}
else if ((!case_sensitive && (tolower(*name) != tolower(*pointer))) || (case_sensitive && (*name != *pointer)))
2016-10-13 19:40:02 +03:00
{
return false;
2016-10-13 19:40:02 +03:00
}
}
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 false;;
2016-10-13 19:40:02 +03:00
}
return true;
}
/* calculate the length of a string if encoded as JSON pointer with ~0 and ~1 escape sequences */
static size_t pointer_encoded_length(const unsigned char *string)
{
size_t length;
for (length = 0; *string != '\0'; (void)string++, length++)
{
/* character needs to be escaped? */
if ((*string == '~') || (*string == '/'))
{
length++;
}
}
return length;
}
/* copy a string while escaping '~' and '/' with ~0 and ~1 JSON pointer escape codes */
static void encode_string_as_pointer(unsigned char *destination, const unsigned char *source)
{
for (; source[0] != '\0'; (void)source++, destination++)
{
if (source[0] == '/')
{
destination[1] = '1';
destination++;
}
else if (source[0] == '~')
{
destination[0] = '~';
destination[1] = '1';
destination++;
}
else
{
destination[0] = source[0];
}
}
destination[0] = '\0';
}
CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target)
{
size_t child_index = 0;
cJSON *current_child = 0;
if ((object == NULL) || (target == NULL))
{
return NULL;
}
if (object == target)
{
/* found */
return (char*)cJSONUtils_strdup((const unsigned char*)"");
}
/* 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++)
{
unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target);
/* found the target? */
if (target_pointer != NULL)
{
if (cJSON_IsArray(object))
{
/* reserve enough memory for a 64 bit integer + '/' and '\0' */
unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/"));
/* 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 (child_index > ULONG_MAX)
{
cJSON_free(target_pointer);
return NULL;
}
sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* /<array_index><path> */
cJSON_free(target_pointer);
return (char*)full_pointer;
}
if (cJSON_IsObject(object))
{
unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + pointer_encoded_length((unsigned char*)current_child->string) + 2);
full_pointer[0] = '/';
encode_string_as_pointer(full_pointer + 1, (unsigned char*)current_child->string);
strcat((char*)full_pointer, (char*)target_pointer);
cJSON_free(target_pointer);
return (char*)full_pointer;
}
/* reached leaf of the tree, found nothing */
cJSON_free(target_pointer);
return NULL;
}
}
/* not found */
return NULL;
}
/* 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;
}
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;
}
for (position = 0; (pointer[position] >= '0') && (pointer[0] <= '9'); position++)
{
parsed_index = (10 * parsed_index) + (size_t)(pointer[position] - '0');
}
if ((pointer[position] != '\0') && (pointer[position] != '/'))
{
return 0;
}
*index = parsed_index;
return 1;
}
static cJSON *get_item_from_pointer(cJSON * const object, const char * pointer, const cJSON_bool case_sensitive)
{
2017-04-30 13:57:49 +03:00
cJSON *current_element = object;
if (pointer == NULL)
{
return NULL;
}
/* follow path of the pointer */
2017-04-30 13:57:49 +03:00
while ((pointer[0] == '/') && (current_element != NULL))
{
2017-04-30 13:57:49 +03:00
pointer++;
if (cJSON_IsArray(current_element))
{
size_t index = 0;
if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index))
{
return NULL;
}
2017-04-30 13:57:49 +03:00
current_element = get_array_item(current_element, index);
}
2017-04-30 13:57:49 +03:00
else if (cJSON_IsObject(current_element))
{
2017-04-30 13:57:49 +03:00
current_element = current_element->child;
/* GetObjectItem. */
while ((current_element != NULL) && !compare_pointers((unsigned char*)current_element->string, (const unsigned char*)pointer, case_sensitive))
{
2017-04-30 13:57:49 +03:00
current_element = current_element->next;
}
}
else
{
return NULL;
}
/* skip to the next path token or end of string */
while ((pointer[0] != '\0') && (pointer[0] != '/'))
{
pointer++;
}
}
2017-04-30 13:57:49 +03:00
return current_element;
}
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer)
{
return get_item_from_pointer(object, pointer, false);
}
2017-04-30 20:58:00 +03:00
CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer)
{
return get_item_from_pointer(object, pointer, true);
}
/* JSON Patch implementation. */
static void decode_pointer_inplace(unsigned char *string)
{
unsigned char *decoded_string = string;
if (string == NULL) {
return;
}
for (; *string; (void)decoded_string++, string++)
{
if (string[0] == '~')
{
if (string[1] == '0')
{
decoded_string[0] = '~';
}
else if (string[1] == '1')
{
decoded_string[1] = '/';
}
else
{
/* invalid escape sequence */
return;
}
string++;
}
}
decoded_string[0] = '\0';
}
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-04-30 15:36:21 +03:00
/* detach an item at the given path */
static cJSON *detach_path(cJSON *object, const unsigned char *path, const cJSON_bool case_sensitive)
{
2017-04-30 14:01:04 +03:00
unsigned char *parent_pointer = NULL;
unsigned char *child_pointer = NULL;
cJSON *parent = NULL;
2017-04-30 14:01:04 +03:00
cJSON *detached_item = NULL;
2016-10-16 21:17:31 +03:00
/* copy path and split it in parent and child */
2017-04-30 14:01:04 +03:00
parent_pointer = cJSONUtils_strdup(path);
if (parent_pointer == NULL) {
goto cleanup;
}
2017-04-30 14:01:04 +03:00
child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */
if (child_pointer == NULL)
2016-10-16 21:17:31 +03:00
{
2017-04-30 14:01:04 +03:00
goto cleanup;
2016-10-16 21:17:31 +03:00
}
/* split strings */
2017-04-30 14:01:04 +03:00
child_pointer[0] = '\0';
child_pointer++;
parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
decode_pointer_inplace(child_pointer);
2017-04-30 14:01:04 +03:00
if (cJSON_IsArray(parent))
2016-10-16 21:17:31 +03:00
{
2017-04-12 12:21:21 +03:00
size_t index = 0;
2017-04-30 14:01:04 +03:00
if (!decode_array_index_from_pointer(child_pointer, &index))
2017-04-12 12:21:21 +03:00
{
2017-04-30 14:01:04 +03:00
goto cleanup;
2017-04-12 12:21:21 +03:00
}
2017-04-30 14:01:04 +03:00
detached_item = detach_item_from_array(parent, index);
2016-10-16 21:17:31 +03:00
}
else if (cJSON_IsObject(parent))
2016-10-16 21:17:31 +03:00
{
2017-04-30 14:01:04 +03:00
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);
2016-10-16 21:17:31 +03:00
}
2017-04-30 14:01:04 +03:00
return detached_item;
}
2017-04-30 21:09:54 +03:00
/* sort lists using mergesort */
static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive)
{
cJSON *first = list;
cJSON *second = list;
cJSON *current_item = list;
cJSON *result = list;
cJSON *result_tail = NULL;
if ((list == NULL) || (list->next == NULL))
{
/* One entry is sorted already. */
return result;
}
while ((current_item != NULL) && (current_item->next != NULL) && (compare_strings((unsigned char*)current_item->string, (unsigned char*)current_item->next->string, case_sensitive) < 0))
{
/* Test for list sorted. */
current_item = current_item->next;
}
if ((current_item == NULL) || (current_item->next == NULL))
{
/* Leave sorted lists unmodified. */
return result;
}
/* reset pointer to the beginning */
current_item = list;
while (current_item != NULL)
{
/* Walk two pointers to find the middle. */
second = second->next;
current_item = current_item->next;
/* advances current_item two steps at a time */
if (current_item != NULL)
{
current_item = current_item->next;
}
}
if ((second != NULL) && (second->prev != NULL))
{
/* Split the lists */
second->prev->next = NULL;
}
/* Recursively sort the sub-lists. */
first = sort_list(first, case_sensitive);
second = sort_list(second, case_sensitive);
result = NULL;
/* Merge the sub-lists */
while ((first != NULL) && (second != NULL))
{
cJSON *smaller = NULL;
if (compare_strings((unsigned char*)first->string, (unsigned char*)second->string, case_sensitive) < 0)
2017-04-30 21:09:54 +03:00
{
smaller = first;
}
else
{
smaller = second;
}
if (result == NULL)
{
/* start merged list with the smaller element */
result_tail = smaller;
result = smaller;
}
else
{
/* add smaller element to the list */
result_tail->next = smaller;
smaller->prev = result_tail;
result_tail = smaller;
}
if (first == smaller)
{
first = first->next;
}
else
{
second = second->next;
}
}
if (first != NULL)
{
/* Append rest of first list. */
if (result == NULL)
{
return first;
}
result_tail->next = first;
first->prev = result_tail;
}
if (second != NULL)
{
/* Append rest of second list */
if (result == NULL)
{
return second;
}
result_tail->next = second;
second->prev = result_tail;
}
return result;
}
static void sort_object(cJSON * const object, const cJSON_bool case_sensitive)
{
2017-06-28 16:59:53 +03:00
if (object == NULL)
{
return;
}
2017-04-30 21:09:54 +03:00
object->child = sort_list(object->child, case_sensitive);
}
static cJSON_bool compare_json(cJSON *a, cJSON *b, const cJSON_bool case_sensitive)
{
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
2016-10-16 21:23:37 +03:00
{
/* mismatched type. */
2017-04-30 17:34:39 +03:00
return false;
2016-10-16 21:23:37 +03:00
}
switch (a->type & 0xFF)
2016-10-16 21:23:37 +03:00
{
case cJSON_Number:
/* numeric mismatch. */
2017-04-30 14:06:09 +03:00
if ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble))
{
2017-04-30 17:34:39 +03:00
return false;
2017-04-30 14:06:09 +03:00
}
else
{
2017-04-30 17:34:39 +03:00
return true;
2017-04-30 14:06:09 +03:00
}
2016-10-16 21:23:37 +03:00
case cJSON_String:
/* string mismatch. */
2017-04-30 20:30:26 +03:00
if (strcmp(a->valuestring, b->valuestring) != 0)
2017-04-30 14:06:09 +03:00
{
2017-04-30 17:34:39 +03:00
return false;
2017-04-30 14:06:09 +03:00
}
else
{
2017-04-30 17:34:39 +03:00
return true;
2017-04-30 14:06:09 +03:00
}
2016-10-16 21:23:37 +03:00
case cJSON_Array:
2017-04-30 14:06:09 +03:00
for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
2016-10-16 21:23:37 +03:00
{
cJSON_bool identical = compare_json(a, b, case_sensitive);
2017-04-30 17:34:39 +03:00
if (!identical)
2016-10-16 21:23:37 +03:00
{
2017-04-30 17:34:39 +03:00
return false;
2016-10-16 21:23:37 +03:00
}
}
2017-04-30 14:06:09 +03:00
2016-10-16 21:23:37 +03:00
/* array size mismatch? (one of both children is not NULL) */
2017-04-30 14:06:09 +03:00
if ((a != NULL) || (b != NULL))
{
2017-04-30 17:34:39 +03:00
return false;
2017-04-30 14:06:09 +03:00
}
else
{
2017-04-30 17:34:39 +03:00
return true;
2017-04-30 14:06:09 +03:00
}
2016-10-16 21:23:37 +03:00
case cJSON_Object:
2017-04-30 21:09:54 +03:00
sort_object(a, case_sensitive);
sort_object(b, case_sensitive);
2017-04-30 14:06:09 +03:00
for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
2016-10-16 21:23:37 +03:00
{
2017-04-30 17:34:39 +03:00
cJSON_bool identical = false;
2016-10-16 21:23:37 +03:00
/* compare object keys */
if (compare_strings((unsigned char*)a->string, (unsigned char*)b->string, case_sensitive))
2016-10-16 21:23:37 +03:00
{
/* missing member */
2017-04-30 17:34:39 +03:00
return false;
2016-10-16 21:23:37 +03:00
}
identical = compare_json(a, b, case_sensitive);
2017-04-30 17:34:39 +03:00
if (!identical)
2016-10-16 21:23:37 +03:00
{
2017-04-30 17:34:39 +03:00
return false;
2016-10-16 21:23:37 +03:00
}
}
2017-04-30 14:06:09 +03:00
2016-10-16 21:23:37 +03:00
/* object length mismatch (one of both children is not null) */
2017-04-30 14:06:09 +03:00
if ((a != NULL) || (b != NULL))
{
2017-04-30 17:34:39 +03:00
return false;
2017-04-30 14:06:09 +03:00
}
else
{
2017-04-30 17:34:39 +03:00
return true;
2017-04-30 14:06:09 +03:00
}
2016-10-16 21:23:37 +03:00
default:
break;
}
2017-04-30 14:06:09 +03:00
2016-10-16 21:23:37 +03:00
/* null, true or false */
2017-04-30 17:34:39 +03:00
return true;
}
/* 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;
}
static cJSON *get_object_item(const cJSON * const object, const char* name, const cJSON_bool case_sensitive)
{
if (case_sensitive)
{
return cJSON_GetObjectItemCaseSensitive(object, name);
}
return cJSON_GetObjectItem(object, name);
}
2017-04-12 12:36:14 +03:00
enum patch_operation { INVALID, ADD, REMOVE, REPLACE, MOVE, COPY, TEST };
static enum patch_operation decode_patch_operation(const cJSON * const patch, const cJSON_bool case_sensitive)
{
cJSON *operation = get_object_item(patch, "op", case_sensitive);
2017-04-30 14:11:14 +03:00
if (!cJSON_IsString(operation))
{
return INVALID;
}
2017-04-30 14:11:14 +03:00
if (strcmp(operation->valuestring, "add") == 0)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return ADD;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (strcmp(operation->valuestring, "remove") == 0)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return REMOVE;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (strcmp(operation->valuestring, "replace") == 0)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return REPLACE;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (strcmp(operation->valuestring, "move") == 0)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return MOVE;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (strcmp(operation->valuestring, "copy") == 0)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return COPY;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (strcmp(operation->valuestring, "test") == 0)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return TEST;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
return INVALID;
}
/* overwrite and existing item with another one and free resources on the way */
static void overwrite_item(cJSON * const root, const cJSON replacement)
{
if (root == NULL)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
return;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (root->string != NULL)
{
cJSON_free(root->string);
}
if (root->valuestring != NULL)
{
cJSON_free(root->valuestring);
}
if (root->child != NULL)
{
cJSON_Delete(root->child);
}
memcpy(root, &replacement, sizeof(cJSON));
}
static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_sensitive)
2017-04-30 14:11:14 +03:00
{
cJSON *path = NULL;
cJSON *value = NULL;
cJSON *parent = NULL;
enum patch_operation opcode = INVALID;
unsigned char *parent_pointer = NULL;
unsigned char *child_pointer = NULL;
int status = 0;
path = get_object_item(patch, "path", case_sensitive);
2017-04-30 14:11:14 +03:00
if (!cJSON_IsString(path))
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
/* malformed patch. */
status = 2;
goto cleanup;
}
opcode = decode_patch_operation(patch, case_sensitive);
2017-04-30 14:11:14 +03:00
if (opcode == INVALID)
{
status = 3;
goto cleanup;
}
else if (opcode == TEST)
{
/* compare value: {...} with the given path */
status = !compare_json(get_item_from_pointer(object, path->valuestring, case_sensitive), get_object_item(patch, "value", case_sensitive), case_sensitive);
2017-04-30 14:11:14 +03:00
goto cleanup;
2016-10-17 07:32:47 +03:00
}
/* special case for replacing the root */
if (path->valuestring[0] == '\0')
{
if (opcode == REMOVE)
{
2017-04-30 14:11:14 +03:00
static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};
2017-04-30 14:11:14 +03:00
overwrite_item(object, invalid);
2017-04-30 14:11:14 +03:00
status = 0;
goto cleanup;
}
if ((opcode == REPLACE) || (opcode == ADD))
{
value = get_object_item(patch, "value", case_sensitive);
if (value == NULL)
{
/* missing "value" for add/replace. */
2017-04-30 14:11:14 +03:00
status = 7;
goto cleanup;
}
value = cJSON_Duplicate(value, 1);
if (value == NULL)
{
/* out of memory for add/replace. */
2017-04-30 14:11:14 +03:00
status = 8;
goto cleanup;
}
2017-04-30 14:11:14 +03:00
overwrite_item(object, *value);
/* delete the duplicated value */
cJSON_free(value);
2017-04-30 14:11:14 +03:00
value = NULL;
2017-04-30 14:11:14 +03:00
/* the string "value" isn't needed */
if (object->string != NULL)
{
cJSON_free(object->string);
object->string = NULL;
}
status = 0;
goto cleanup;
}
}
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. */
cJSON *old_item = detach_path(object, (unsigned char*)path->valuestring, case_sensitive);
if (old_item == NULL)
{
2017-04-30 14:11:14 +03:00
status = 13;
goto cleanup;
}
cJSON_Delete(old_item);
2017-04-12 12:36:14 +03:00
if (opcode == REMOVE)
2016-10-17 07:32:47 +03:00
{
/* For Remove, this job is done. */
2017-04-30 14:11:14 +03:00
status = 0;
goto cleanup;
2016-10-17 07:32:47 +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 = get_object_item(patch, "from", case_sensitive);
2017-04-30 14:11:14 +03:00
if (from == NULL)
2016-10-17 07:32:47 +03:00
{
/* missing "from" for copy/move. */
2017-04-30 14:11:14 +03:00
status = 4;
goto cleanup;
2016-10-17 07:32:47 +03:00
}
2017-04-12 12:36:14 +03:00
if (opcode == MOVE)
2016-10-17 07:32:47 +03:00
{
value = detach_path(object, (unsigned char*)from->valuestring, case_sensitive);
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 = get_item_from_pointer(object, from->valuestring, case_sensitive);
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:11:14 +03:00
if (value == NULL)
2016-10-17 07:32:47 +03:00
{
/* missing "from" for copy/move. */
2017-04-30 14:11:14 +03:00
status = 5;
goto cleanup;
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 = cJSON_Duplicate(value, 1);
}
2017-04-30 14:11:14 +03:00
if (value == NULL)
2016-10-17 07:32:47 +03:00
{
/* out of memory for copy/move. */
2017-04-30 14:11:14 +03:00
status = 6;
goto cleanup;
2016-10-17 07:32:47 +03:00
}
}
else /* Add/Replace uses "value". */
{
value = get_object_item(patch, "value", case_sensitive);
2017-04-30 14:11:14 +03:00
if (value == NULL)
2016-10-17 07:32:47 +03:00
{
/* missing "value" for add/replace. */
2017-04-30 14:11:14 +03:00
status = 7;
goto cleanup;
2016-10-17 07:32:47 +03:00
}
value = cJSON_Duplicate(value, 1);
2017-04-30 14:11:14 +03:00
if (value == NULL)
2016-10-17 07:32:47 +03:00
{
/* out of memory for add/replace. */
2017-04-30 14:11:14 +03:00
status = 8;
goto cleanup;
2016-10-17 07:32:47 +03:00
}
}
2016-10-17 07:32:47 +03:00
/* Now, just add "value" to "path". */
2016-10-17 07:32:47 +03:00
/* split pointer in parent and child */
2017-04-30 14:11:14 +03:00
parent_pointer = cJSONUtils_strdup((unsigned char*)path->valuestring);
child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/');
if (child_pointer != NULL)
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
child_pointer[0] = '\0';
child_pointer++;
2016-10-17 07:32:47 +03:00
}
parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
decode_pointer_inplace(child_pointer);
2016-10-17 07:32:47 +03:00
/* add, remove, replace, move, copy, test. */
2017-04-30 14:11:14 +03:00
if ((parent == NULL) || (child_pointer == NULL))
2016-10-17 07:32:47 +03:00
{
/* Couldn't find object to add to. */
2017-04-30 14:11:14 +03:00
status = 9;
goto cleanup;
2016-10-17 07:32:47 +03:00
}
else if (cJSON_IsArray(parent))
2016-10-17 07:32:47 +03:00
{
2017-04-30 14:11:14 +03:00
if (strcmp((char*)child_pointer, "-") == 0)
2016-10-17 07:32:47 +03:00
{
cJSON_AddItemToArray(parent, value);
2017-04-30 14:11:14 +03:00
value = NULL;
2016-10-17 07:32:47 +03:00
}
else
{
size_t index = 0;
2017-04-30 14:11:14 +03:00
if (!decode_array_index_from_pointer(child_pointer, &index))
{
2017-04-30 14:11:14 +03:00
status = 11;
goto cleanup;
}
if (!insert_item_in_array(parent, index, value))
{
2017-04-30 14:11:14 +03:00
status = 10;
goto cleanup;
}
2017-04-30 14:11:14 +03:00
value = NULL;
2016-10-17 07:32:47 +03:00
}
}
else if (cJSON_IsObject(parent))
2016-10-17 07:32:47 +03:00
{
if (case_sensitive)
{
cJSON_DeleteItemFromObjectCaseSensitive(parent, (char*)child_pointer);
}
else
{
cJSON_DeleteItemFromObject(parent, (char*)child_pointer);
}
2017-04-30 14:11:14 +03:00
cJSON_AddItemToObject(parent, (char*)child_pointer, value);
value = NULL;
2016-10-17 07:32:47 +03:00
}
else /* parent is not an object */
{
/* Couldn't find object to add to. */
status = 9;
goto cleanup;
}
2017-04-30 14:11:14 +03:00
cleanup:
if (value != NULL)
2016-10-17 07:32:47 +03:00
{
cJSON_Delete(value);
}
2017-04-30 14:11:14 +03:00
if (parent_pointer != NULL)
{
cJSON_free(parent_pointer);
}
2016-10-17 07:32:47 +03:00
2017-04-30 14:11:14 +03:00
return status;
2016-10-17 07:32:47 +03:00
}
2017-04-30 14:12:32 +03:00
CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches)
{
2017-04-30 14:12:32 +03:00
const cJSON *current_patch = NULL;
int status = 0;
if (!cJSON_IsArray(patches))
2016-10-17 07:34:33 +03:00
{
/* malformed patches. */
return 1;
}
2017-04-30 14:12:32 +03:00
if (patches != NULL)
2016-10-17 07:34:33 +03:00
{
2017-04-30 14:12:32 +03:00
current_patch = patches->child;
2016-10-17 07:34:33 +03:00
}
2017-04-30 14:12:32 +03:00
while (current_patch != NULL)
2016-10-17 07:34:33 +03:00
{
status = apply_patch(object, current_patch, false);
2017-04-30 14:12:32 +03:00
if (status != 0)
2016-10-17 07:34:33 +03:00
{
2017-04-30 14:12:32 +03:00
return status;
2016-10-17 07:34:33 +03:00
}
2017-04-30 14:12:32 +03:00
current_patch = current_patch->next;
2016-10-17 07:34:33 +03:00
}
return 0;
}
CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches)
{
const cJSON *current_patch = NULL;
int status = 0;
if (!cJSON_IsArray(patches))
{
/* malformed patches. */
return 1;
}
if (patches != NULL)
{
current_patch = patches->child;
}
while (current_patch != NULL)
{
status = apply_patch(object, current_patch, true);
if (status != 0)
{
return status;
}
current_patch = current_patch->next;
}
return 0;
}
static void compose_patch(cJSON * const patches, const unsigned char * const operation, const unsigned char * const path, const unsigned char *suffix, const cJSON * const value)
{
2017-06-28 17:00:14 +03:00
cJSON *patch = NULL;
if ((patches == NULL) || (operation == NULL) || (path == NULL))
{
return;
}
patch = cJSON_CreateObject();
2017-04-30 14:12:57 +03:00
if (patch == NULL)
2016-10-17 13:00:46 +03:00
{
2017-04-30 14:12:57 +03:00
return;
2016-10-17 13:00:46 +03:00
}
2017-04-30 14:12:57 +03:00
cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)operation));
if (suffix == NULL)
2016-10-17 13:00:46 +03:00
{
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
2016-10-17 13:00:46 +03:00
}
2017-04-30 14:12:57 +03:00
else
{
size_t suffix_length = pointer_encoded_length(suffix);
2017-04-30 14:12:57 +03:00
size_t path_length = strlen((const char*)path);
unsigned char *full_path = (unsigned char*)cJSON_malloc(path_length + suffix_length + sizeof("/"));
sprintf((char*)full_path, "%s/", (const char*)path);
encode_string_as_pointer(full_path + path_length + 1, suffix);
2017-04-30 14:12:57 +03:00
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)full_path));
2017-05-06 07:38:10 +03:00
cJSON_free(full_path);
2017-04-30 14:12:57 +03:00
}
if (value != NULL)
2016-10-17 13:00:46 +03:00
{
2017-04-30 14:12:57 +03:00
cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(value, 1));
2016-10-17 13:00:46 +03:00
}
cJSON_AddItemToArray(patches, patch);
}
2017-04-30 14:13:17 +03:00
CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value)
2016-10-17 13:01:52 +03:00
{
compose_patch(array, (const unsigned char*)operation, (const unsigned char*)path, NULL, value);
2016-10-17 13:01:52 +03:00
}
static void create_patches(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
if ((from == NULL) || (to == NULL))
{
return;
}
if ((from->type & 0xFF) != (to->type & 0xFF))
{
compose_patch(patches, (const unsigned char*)"replace", path, 0, to);
return;
}
2017-04-30 14:14:29 +03:00
switch (from->type & 0xFF)
{
case cJSON_Number:
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
{
compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
}
return;
case cJSON_String:
2017-04-30 20:42:16 +03:00
if (strcmp(from->valuestring, to->valuestring) != 0)
{
compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
}
return;
case cJSON_Array:
{
2017-04-30 14:14:29 +03:00
size_t index = 0;
cJSON *from_child = from->child;
cJSON *to_child = to->child;
unsigned char *new_path = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 20 + sizeof("/")); /* Allow space for 64bit int. log10(2^64) = 20 */
/* generate patches for all array elements that exist in both "from" and "to" */
for (index = 0; (from_child != NULL) && (to_child != NULL); (void)(from_child = from_child->next), (void)(to_child = to_child->next), index++)
{
/* 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 14:14:29 +03:00
if (index > ULONG_MAX)
{
2017-05-06 07:38:10 +03:00
cJSON_free(new_path);
return;
}
2017-04-30 14:14:29 +03:00
sprintf((char*)new_path, "%s/%lu", path, (unsigned long)index); /* path of the current array element */
create_patches(patches, new_path, from_child, to_child, case_sensitive);
}
2017-04-30 14:14:29 +03:00
/* remove leftover elements from 'from' that are not in 'to' */
2017-04-30 14:14:29 +03:00
for (; (from_child != NULL); (void)(from_child = from_child->next))
{
/* 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 14:14:29 +03:00
if (index > ULONG_MAX)
{
2017-05-06 07:38:10 +03:00
cJSON_free(new_path);
return;
}
2017-04-30 14:14:29 +03:00
sprintf((char*)new_path, "%lu", (unsigned long)index);
compose_patch(patches, (const unsigned char*)"remove", path, new_path, NULL);
}
/* add new elements in 'to' that were not in 'from' */
2017-04-30 14:14:29 +03:00
for (; (to_child != NULL); (void)(to_child = to_child->next), index++)
{
compose_patch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child);
}
2017-05-06 07:38:10 +03:00
cJSON_free(new_path);
return;
}
case cJSON_Object:
{
2017-04-30 14:14:29 +03:00
cJSON *from_child = NULL;
cJSON *to_child = NULL;
2017-04-30 21:09:54 +03:00
sort_object(from, case_sensitive);
sort_object(to, case_sensitive);
2017-04-30 14:14:29 +03:00
from_child = from->child;
to_child = to->child;
/* for all object values in the object with more of them */
2017-04-30 14:14:29 +03:00
while ((from_child != NULL) || (to_child != NULL))
{
2017-04-30 14:14:29 +03:00
int diff;
if (from_child == NULL)
{
diff = 1;
}
else if (to_child == NULL)
{
diff = -1;
}
else
{
diff = compare_strings((unsigned char*)from_child->string, (unsigned char*)to_child->string, case_sensitive);
2017-04-30 14:14:29 +03:00
}
if (diff == 0)
{
/* both object keys are the same */
2017-04-30 14:14:29 +03:00
size_t path_length = strlen((const char*)path);
size_t from_child_name_length = pointer_encoded_length((unsigned char*)from_child->string);
2017-04-30 14:14:29 +03:00
unsigned char *new_path = (unsigned char*)cJSON_malloc(path_length + from_child_name_length + sizeof("/"));
sprintf((char*)new_path, "%s/", path);
encode_string_as_pointer(new_path + path_length + 1, (unsigned char*)from_child->string);
2017-04-30 14:14:29 +03:00
/* create a patch for the element */
create_patches(patches, new_path, from_child, to_child, case_sensitive);
2017-05-06 07:38:10 +03:00
cJSON_free(new_path);
2017-04-30 14:14:29 +03:00
from_child = from_child->next;
to_child = to_child->next;
}
else if (diff < 0)
{
/* object element doesn't exist in 'to' --> remove it */
compose_patch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL);
2017-04-30 14:14:29 +03:00
from_child = from_child->next;
}
else
{
/* object element doesn't exist in 'from' --> add it */
compose_patch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child);
2017-04-30 14:14:29 +03:00
to_child = to_child->next;
}
}
return;
}
default:
break;
}
}
2017-04-30 14:15:18 +03:00
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to)
{
cJSON *patches = NULL;
if ((from == NULL) || (to == NULL))
{
return NULL;
}
patches = cJSON_CreateArray();
create_patches(patches, (const unsigned char*)"", from, to, false);
return patches;
}
CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to)
{
cJSON *patches = NULL;
if ((from == NULL) || (to == NULL))
{
return NULL;
}
patches = cJSON_CreateArray();
create_patches(patches, (const unsigned char*)"", from, to, true);
return patches;
}
2017-04-30 21:09:54 +03:00
CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object)
{
2017-04-30 21:09:54 +03:00
sort_object(object, false);
}
2017-04-30 21:09:54 +03:00
CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object)
2016-10-28 19:37:21 +03:00
{
2017-04-30 21:09:54 +03:00
sort_object(object, true);
2016-10-28 19:37:21 +03:00
}
2017-05-01 23:41:19 +03:00
static cJSON *merge_patch(cJSON *target, const cJSON * const patch, const cJSON_bool case_sensitive)
{
2017-04-30 14:17:39 +03:00
cJSON *patch_child = NULL;
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);
}
if (!cJSON_IsObject(target))
2016-10-29 08:40:41 +03:00
{
cJSON_Delete(target);
target = cJSON_CreateObject();
}
2017-04-30 14:17:39 +03:00
patch_child = patch->child;
while (patch_child != NULL)
2016-10-29 08:40:41 +03:00
{
2017-04-30 14:17:39 +03:00
if (cJSON_IsNull(patch_child))
2016-10-29 08:40:41 +03:00
{
/* NULL is the indicator to remove a value, see RFC7396 */
2017-05-01 23:41:19 +03:00
if (case_sensitive)
{
cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string);
}
else
{
cJSON_DeleteItemFromObject(target, patch_child->string);
}
2016-10-29 08:40:41 +03:00
}
else
{
2017-05-01 23:41:19 +03:00
cJSON *replace_me = NULL;
cJSON *replacement = NULL;
if (case_sensitive)
{
replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string);
}
else
{
replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
}
replacement = merge_patch(replace_me, patch_child, case_sensitive);
if (replacement == NULL)
{
return NULL;
}
cJSON_AddItemToObject(target, patch_child->string, replacement);
2016-10-29 08:40:41 +03:00
}
2017-04-30 14:17:39 +03:00
patch_child = patch_child->next;
2016-10-29 08:40:41 +03:00
}
return target;
}
2017-05-01 23:41:19 +03:00
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch)
{
return merge_patch(target, patch, false);
}
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch)
{
return merge_patch(target, patch, true);
}
static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
2017-04-30 14:18:25 +03:00
cJSON *from_child = NULL;
cJSON *to_child = NULL;
cJSON *patch = NULL;
2017-04-30 14:18:25 +03:00
if (to == NULL)
{
/* patch to delete everything */
return cJSON_CreateNull();
}
if (!cJSON_IsObject(to) || !cJSON_IsObject(from))
{
return cJSON_Duplicate(to, 1);
}
2017-04-30 21:09:54 +03:00
sort_object(from, case_sensitive);
sort_object(to, case_sensitive);
2017-04-30 14:18:25 +03:00
from_child = from->child;
to_child = to->child;
patch = cJSON_CreateObject();
2017-04-30 14:18:25 +03:00
while (from_child || to_child)
{
2017-04-30 14:18:25 +03:00
int diff;
if (from_child != NULL)
{
if (to_child != NULL)
{
2017-04-30 20:48:46 +03:00
diff = strcmp(from_child->string, to_child->string);
2017-04-30 14:18:25 +03:00
}
else
{
diff = -1;
}
}
else
{
diff = 1;
}
if (diff < 0)
{
/* from has a value that to doesn't have -> remove */
2017-04-30 14:18:25 +03:00
cJSON_AddItemToObject(patch, from_child->string, cJSON_CreateNull());
from_child = from_child->next;
}
2017-04-30 14:18:25 +03:00
else if (diff > 0)
{
/* to has a value that from doesn't have -> add to patch */
2017-04-30 14:18:25 +03:00
cJSON_AddItemToObject(patch, to_child->string, cJSON_Duplicate(to_child, 1));
to_child = to_child->next;
}
else
{
/* object key exists in both objects */
if (!compare_json(from_child, to_child, case_sensitive))
{
/* not identical --> generate a patch */
2017-04-30 14:18:25 +03:00
cJSON_AddItemToObject(patch, to_child->string, cJSONUtils_GenerateMergePatch(from_child, to_child));
}
2017-04-30 14:18:25 +03:00
/* next key in the object */
2017-04-30 14:18:25 +03:00
from_child = from_child->next;
to_child = to_child->next;
}
}
2017-04-30 14:18:25 +03:00
if (patch->child == NULL)
{
2017-04-30 14:18:25 +03:00
/* no patch generated */
cJSON_Delete(patch);
return NULL;
}
return patch;
2016-10-13 19:26:53 +03:00
}
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to)
{
return generate_merge_patch(from, to, false);
}
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to)
{
return generate_merge_patch(from, to, true);
}