2015-02-10 12:14:59 +00:00
|
|
|
#include <ctype.h>
|
2015-02-10 14:17:59 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-11 01:29:40 +00:00
|
|
|
#include <stdio.h>
|
2017-02-03 17:34:37 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2015-02-09 18:29:05 +00:00
|
|
|
#include "cJSON_Utils.h"
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
static unsigned char* cJSONUtils_strdup(const unsigned char* str)
|
2016-11-04 17:15:57 +00:00
|
|
|
{
|
2016-11-13 11:05:44 +00:00
|
|
|
size_t len = 0;
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *copy = NULL;
|
2016-11-04 17:15:57 +00:00
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
len = strlen((const char*)str) + 1;
|
|
|
|
if (!(copy = (unsigned char*)malloc(len)))
|
2016-11-04 17:15:57 +00:00
|
|
|
{
|
2016-11-13 10:45:40 +00:00
|
|
|
return NULL;
|
2016-11-04 17:15:57 +00:00
|
|
|
}
|
|
|
|
memcpy(copy, str, len);
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
static int cJSONUtils_strcasecmp(const unsigned char *s1, const unsigned char *s2)
|
2015-02-13 19:53:27 +00:00
|
|
|
{
|
2016-10-13 16:26:53 +00:00
|
|
|
if (!s1)
|
|
|
|
{
|
|
|
|
return (s1 == s2) ? 0 : 1; /* both NULL? */
|
|
|
|
}
|
|
|
|
if (!s2)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
|
|
|
|
{
|
|
|
|
if(*s1 == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
return tolower(*s1) - tolower(*s2);
|
2015-02-13 19:53:27 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 19:19:26 +00:00
|
|
|
/* JSON Pointer implementation: */
|
2017-02-03 14:59:00 +00:00
|
|
|
static int cJSONUtils_Pstrcasecmp(const unsigned char *a, const unsigned char *e)
|
2015-02-10 12:14:59 +00:00
|
|
|
{
|
2016-10-13 16:40:02 +00:00
|
|
|
if (!a || !e)
|
|
|
|
{
|
|
|
|
return (a == e) ? 0 : 1; /* both NULL? */
|
|
|
|
}
|
|
|
|
for (; *a && *e && (*e != '/'); a++, e++) /* compare until next '/' */
|
|
|
|
{
|
|
|
|
if (*e == '~')
|
|
|
|
{
|
|
|
|
/* check for escaped '~' (~0) and '/' (~1) */
|
|
|
|
if (!((e[1] == '0') && (*a == '~')) && !((e[1] == '1') && (*a == '/')))
|
|
|
|
{
|
|
|
|
/* invalid escape sequence or wrong character in *a */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
e++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tolower(*a) != tolower(*e))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (((*e != 0) && (*e != '/')) != (*a != 0))
|
|
|
|
{
|
|
|
|
/* one string has ended, the other not */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-02-10 12:14:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 15:34:19 +00:00
|
|
|
static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *s)
|
2016-10-13 16:42:34 +00:00
|
|
|
{
|
2017-02-03 15:34:19 +00:00
|
|
|
size_t l = 0;
|
2016-10-13 16:42:34 +00:00
|
|
|
for (; *s; s++, l++)
|
|
|
|
{
|
|
|
|
if ((*s == '~') || (*s == '/'))
|
|
|
|
{
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
static void cJSONUtils_PointerEncodedstrcpy(unsigned char *d, const unsigned char *s)
|
2015-02-11 01:29:40 +00:00
|
|
|
{
|
2016-10-13 16:47:40 +00:00
|
|
|
for (; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s == '/')
|
|
|
|
{
|
|
|
|
*d++ = '~';
|
|
|
|
*d++ = '1';
|
|
|
|
}
|
|
|
|
else if (*s == '~')
|
|
|
|
{
|
|
|
|
*d++ = '~';
|
|
|
|
*d++ = '0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*d++ = *s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = '\0';
|
2015-02-11 01:29:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:05:58 +00:00
|
|
|
char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
|
2015-02-11 01:29:40 +00:00
|
|
|
{
|
2016-10-13 17:05:58 +00:00
|
|
|
int type = object->type;
|
2017-02-03 15:34:19 +00:00
|
|
|
size_t c = 0;
|
2016-10-13 17:05:58 +00:00
|
|
|
cJSON *obj = 0;
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2016-10-13 17:05:58 +00:00
|
|
|
if (object == target)
|
|
|
|
{
|
|
|
|
/* found */
|
2017-02-03 14:59:00 +00:00
|
|
|
return (char*)cJSONUtils_strdup((const unsigned char*)"");
|
2016-10-13 17:05:58 +00:00
|
|
|
}
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2016-10-13 17:05:58 +00:00
|
|
|
/* recursively search all children of the object */
|
|
|
|
for (obj = object->child; obj; obj = obj->next, c++)
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *found = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(obj, target);
|
2016-10-13 17:05:58 +00:00
|
|
|
if (found)
|
|
|
|
{
|
2016-11-18 15:48:40 +00:00
|
|
|
if ((type & 0xFF) == cJSON_Array)
|
2016-10-13 17:05:58 +00:00
|
|
|
{
|
|
|
|
/* reserve enough memory for a 64 bit integer + '/' and '\0' */
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + 23);
|
2017-02-03 15:34:19 +00:00
|
|
|
sprintf((char*)ret, "/%lu%s", c, found); /* /<array_index><path> */
|
2016-10-13 17:05:58 +00:00
|
|
|
free(found);
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
return (char*)ret;
|
2016-10-13 17:05:58 +00:00
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
else if ((type & 0xFF) == cJSON_Object)
|
2016-10-13 17:05:58 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + cJSONUtils_PointerEncodedstrlen((unsigned char*)obj->string) + 2);
|
2016-10-13 17:05:58 +00:00
|
|
|
*ret = '/';
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_PointerEncodedstrcpy(ret + 1, (unsigned char*)obj->string);
|
|
|
|
strcat((char*)ret, (char*)found);
|
2016-10-13 17:05:58 +00:00
|
|
|
free(found);
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
return (char*)ret;
|
2016-10-13 17:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* reached leaf of the tree, found nothing */
|
|
|
|
free(found);
|
2016-11-13 10:45:40 +00:00
|
|
|
return NULL;
|
2016-10-13 17:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
2016-11-13 10:45:40 +00:00
|
|
|
return NULL;
|
2015-02-11 01:29:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:18:15 +00:00
|
|
|
cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer)
|
2015-02-09 18:29:05 +00:00
|
|
|
{
|
2016-10-13 17:18:15 +00:00
|
|
|
/* follow path of the pointer */
|
|
|
|
while ((*pointer++ == '/') && object)
|
|
|
|
{
|
2016-11-18 15:48:40 +00:00
|
|
|
if ((object->type & 0xFF) == cJSON_Array)
|
2016-10-13 17:18:15 +00:00
|
|
|
{
|
2017-02-03 15:34:19 +00:00
|
|
|
size_t which = 0;
|
2016-10-13 17:18:15 +00:00
|
|
|
/* parse array index */
|
|
|
|
while ((*pointer >= '0') && (*pointer <= '9'))
|
|
|
|
{
|
2017-02-03 17:34:37 +00:00
|
|
|
which = (10 * which) + (size_t)(*pointer++ - '0');
|
2016-10-13 17:18:15 +00:00
|
|
|
}
|
|
|
|
if (*pointer && (*pointer != '/'))
|
|
|
|
{
|
|
|
|
/* not end of string or new path token */
|
2016-11-13 10:45:40 +00:00
|
|
|
return NULL;
|
2016-10-13 17:18:15 +00:00
|
|
|
}
|
2017-02-03 17:34:37 +00:00
|
|
|
if (which > INT_MAX)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
object = cJSON_GetArrayItem(object, (int)which);
|
2016-10-13 17:18:15 +00:00
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
else if ((object->type & 0xFF) == cJSON_Object)
|
2016-10-13 17:18:15 +00:00
|
|
|
{
|
|
|
|
object = object->child;
|
|
|
|
/* GetObjectItem. */
|
2017-02-03 14:59:00 +00:00
|
|
|
while (object && cJSONUtils_Pstrcasecmp((unsigned char*)object->string, (const unsigned char*)pointer))
|
2016-10-13 17:18:15 +00:00
|
|
|
{
|
|
|
|
object = object->next;
|
|
|
|
}
|
|
|
|
/* skip to the next path token or end of string */
|
|
|
|
while (*pointer && (*pointer != '/'))
|
|
|
|
{
|
|
|
|
pointer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-13 10:45:40 +00:00
|
|
|
return NULL;
|
2016-10-13 17:18:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
2015-02-09 18:29:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 19:19:26 +00:00
|
|
|
/* JSON Patch implementation. */
|
2017-02-03 14:59:00 +00:00
|
|
|
static void cJSONUtils_InplaceDecodePointerString(unsigned char *string)
|
2015-02-10 14:17:59 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *s2 = string;
|
2017-01-30 18:29:52 +00:00
|
|
|
|
|
|
|
if (string == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-15 05:20:47 +00:00
|
|
|
for (; *string; s2++, string++)
|
|
|
|
{
|
|
|
|
*s2 = (*string != '~')
|
|
|
|
? (*string)
|
|
|
|
: ((*(++string) == '0')
|
|
|
|
? '~'
|
|
|
|
: '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
*s2 = '\0';
|
2015-02-10 14:17:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
|
2015-02-10 14:17:59 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *parentptr = NULL;
|
|
|
|
unsigned char *childptr = NULL;
|
2016-11-13 10:45:40 +00:00
|
|
|
cJSON *parent = NULL;
|
|
|
|
cJSON *ret = NULL;
|
2016-10-16 18:17:31 +00:00
|
|
|
|
|
|
|
/* copy path and split it in parent and child */
|
2016-11-04 17:15:57 +00:00
|
|
|
parentptr = cJSONUtils_strdup(path);
|
2017-01-30 18:30:16 +00:00
|
|
|
if (parentptr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
childptr = (unsigned char*)strrchr((char*)parentptr, '/'); /* last '/' */
|
2017-01-30 18:30:16 +00:00
|
|
|
if (childptr == NULL)
|
2016-10-16 18:17:31 +00:00
|
|
|
{
|
2017-01-30 18:30:16 +00:00
|
|
|
free(parentptr);
|
|
|
|
return NULL;
|
2016-10-16 18:17:31 +00:00
|
|
|
}
|
2017-01-30 18:30:16 +00:00
|
|
|
/* split strings */
|
|
|
|
*childptr++ = '\0';
|
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
parent = cJSONUtils_GetPointer(object, (char*)parentptr);
|
2016-10-16 18:17:31 +00:00
|
|
|
cJSONUtils_InplaceDecodePointerString(childptr);
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-16 18:17:31 +00:00
|
|
|
if (!parent)
|
|
|
|
{
|
|
|
|
/* Couldn't find object to remove child from. */
|
2016-11-13 10:45:40 +00:00
|
|
|
ret = NULL;
|
2016-10-16 18:17:31 +00:00
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
else if ((parent->type & 0xFF) == cJSON_Array)
|
2016-10-16 18:17:31 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
ret = cJSON_DetachItemFromArray(parent, atoi((char*)childptr));
|
2016-10-16 18:17:31 +00:00
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
else if ((parent->type & 0xFF) == cJSON_Object)
|
2016-10-16 18:17:31 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
ret = cJSON_DetachItemFromObject(parent, (char*)childptr);
|
2016-10-16 18:17:31 +00:00
|
|
|
}
|
|
|
|
free(parentptr);
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-16 18:17:31 +00:00
|
|
|
/* return the detachted item */
|
|
|
|
return ret;
|
2015-02-10 14:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 18:23:37 +00:00
|
|
|
static int cJSONUtils_Compare(cJSON *a, cJSON *b)
|
2015-02-11 01:29:40 +00:00
|
|
|
{
|
2016-11-18 15:48:40 +00:00
|
|
|
if ((a->type & 0xFF) != (b->type & 0xFF))
|
2016-10-16 18:23:37 +00:00
|
|
|
{
|
|
|
|
/* mismatched type. */
|
|
|
|
return -1;
|
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
switch (a->type & 0xFF)
|
2016-10-16 18:23:37 +00: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:
|
|
|
|
for (a = a->child, b = b->child; a && b; a = a->next, b = b->next)
|
|
|
|
{
|
|
|
|
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 11:05:44 +00:00
|
|
|
int err = 0;
|
2016-10-16 18:23:37 +00:00
|
|
|
/* compare object keys */
|
2017-02-03 14:59:00 +00:00
|
|
|
if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
|
2016-10-16 18:23:37 +00: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 19:53:27 +00:00
|
|
|
|
2016-10-16 18:23:37 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* null, true or false */
|
|
|
|
return 0;
|
2015-02-11 01:29:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
|
2015-02-10 14:17:59 +00:00
|
|
|
{
|
2016-11-13 10:45:40 +00:00
|
|
|
cJSON *op = NULL;
|
|
|
|
cJSON *path = NULL;
|
|
|
|
cJSON *value = NULL;
|
|
|
|
cJSON *parent = NULL;
|
2016-10-17 04:32:47 +00:00
|
|
|
int opcode = 0;
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *parentptr = NULL;
|
|
|
|
unsigned char *childptr = NULL;
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
op = cJSON_GetObjectItem(patch, "op");
|
|
|
|
path = cJSON_GetObjectItem(patch, "path");
|
|
|
|
if (!op || !path)
|
|
|
|
{
|
|
|
|
/* malformed patch. */
|
|
|
|
return 2;
|
|
|
|
}
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
/* decode operation */
|
|
|
|
if (!strcmp(op->valuestring, "add"))
|
|
|
|
{
|
|
|
|
opcode = 0;
|
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "remove"))
|
|
|
|
{
|
|
|
|
opcode = 1;
|
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "replace"))
|
|
|
|
{
|
|
|
|
opcode = 2;
|
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "move"))
|
|
|
|
{
|
|
|
|
opcode = 3;
|
|
|
|
}
|
|
|
|
else if (!strcmp(op->valuestring, "copy"))
|
|
|
|
{
|
|
|
|
opcode = 4;
|
|
|
|
}
|
|
|
|
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 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
/* Remove/Replace */
|
|
|
|
if ((opcode == 1) || (opcode == 2))
|
|
|
|
{
|
|
|
|
/* Get rid of old. */
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSON_Delete(cJSONUtils_PatchDetach(object, (unsigned char*)path->valuestring));
|
2016-10-17 04:32:47 +00:00
|
|
|
if (opcode == 1)
|
|
|
|
{
|
|
|
|
/* For Remove, this is job done. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
/* Copy/Move uses "from". */
|
|
|
|
if ((opcode == 3) || (opcode == 4))
|
|
|
|
{
|
|
|
|
cJSON *from = cJSON_GetObjectItem(patch, "from");
|
|
|
|
if (!from)
|
|
|
|
{
|
|
|
|
/* missing "from" for copy/move. */
|
|
|
|
return 4;
|
|
|
|
}
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
if (opcode == 3)
|
|
|
|
{
|
|
|
|
/* move */
|
2017-02-03 14:59:00 +00:00
|
|
|
value = cJSONUtils_PatchDetach(object, (unsigned char*)from->valuestring);
|
2016-10-17 04:32:47 +00:00
|
|
|
}
|
|
|
|
if (opcode == 4)
|
|
|
|
{
|
|
|
|
/* copy */
|
|
|
|
value = cJSONUtils_GetPointer(object, from->valuestring);
|
|
|
|
}
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
/* missing "from" for copy/move. */
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
if (opcode == 4)
|
|
|
|
{
|
|
|
|
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 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
/* Now, just add "value" to "path". */
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
/* split pointer in parent and child */
|
2017-02-03 14:59:00 +00:00
|
|
|
parentptr = cJSONUtils_strdup((unsigned char*)path->valuestring);
|
|
|
|
childptr = (unsigned char*)strrchr((char*)parentptr, '/');
|
2016-10-17 04:32:47 +00:00
|
|
|
if (childptr)
|
|
|
|
{
|
|
|
|
*childptr++ = '\0';
|
|
|
|
}
|
2017-02-03 14:59:00 +00:00
|
|
|
parent = cJSONUtils_GetPointer(object, (char*)parentptr);
|
2016-10-17 04:32:47 +00:00
|
|
|
cJSONUtils_InplaceDecodePointerString(childptr);
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-17 04:32:47 +00:00
|
|
|
/* add, remove, replace, move, copy, test. */
|
|
|
|
if (!parent)
|
|
|
|
{
|
|
|
|
/* Couldn't find object to add to. */
|
|
|
|
free(parentptr);
|
|
|
|
cJSON_Delete(value);
|
|
|
|
return 9;
|
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
else if ((parent->type & 0xFF) == cJSON_Array)
|
2016-10-17 04:32:47 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
if (!strcmp((char*)childptr, "-"))
|
2016-10-17 04:32:47 +00:00
|
|
|
{
|
|
|
|
cJSON_AddItemToArray(parent, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSON_InsertItemInArray(parent, atoi((char*)childptr), value);
|
2016-10-17 04:32:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
else if ((parent->type & 0xFF) == cJSON_Object)
|
2016-10-17 04:32:47 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSON_DeleteItemFromObject(parent, (char*)childptr);
|
|
|
|
cJSON_AddItemToObject(parent, (char*)childptr, value);
|
2016-10-17 04:32:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cJSON_Delete(value);
|
|
|
|
}
|
|
|
|
free(parentptr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-10 14:17:59 +00:00
|
|
|
|
2016-10-17 04:34:33 +00:00
|
|
|
int cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
|
2015-02-10 14:17:59 +00:00
|
|
|
{
|
2016-11-13 11:05:44 +00:00
|
|
|
int err = 0;
|
2017-02-07 18:07:18 +00:00
|
|
|
|
|
|
|
if (patches == NULL)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:48:40 +00:00
|
|
|
if ((patches->type & 0xFF) != cJSON_Array)
|
2016-10-17 04:34:33 +00: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 14:17:59 +00:00
|
|
|
}
|
2015-02-09 18:29:05 +00:00
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
static void cJSONUtils_GeneratePatch(cJSON *patches, const unsigned char *op, const unsigned char *path, const unsigned char *suffix, cJSON *val)
|
2015-02-11 01:29:40 +00:00
|
|
|
{
|
2016-10-17 10:00:46 +00:00
|
|
|
cJSON *patch = cJSON_CreateObject();
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)op));
|
2016-10-17 10:00:46 +00:00
|
|
|
if (suffix)
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
|
|
|
|
cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", (const char*)path), suffix);
|
|
|
|
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)newpath));
|
2016-10-17 10:00:46 +00:00
|
|
|
free(newpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
|
2016-10-17 10:00:46 +00:00
|
|
|
}
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
|
|
|
|
}
|
|
|
|
cJSON_AddItemToArray(patches, patch);
|
2015-02-11 01:29:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 10:01:52 +00:00
|
|
|
void cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(array, (const unsigned char*)op, (const unsigned char*)path, 0, val);
|
2016-10-17 10:01:52 +00:00
|
|
|
}
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, cJSON *from, cJSON *to)
|
2015-02-11 01:29:40 +00:00
|
|
|
{
|
2017-02-07 18:07:18 +00:00
|
|
|
if ((from == NULL) || (to == NULL))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:48:40 +00:00
|
|
|
if ((from->type & 0xFF) != (to->type & 0xFF))
|
2016-10-17 10:35:49 +00:00
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 10:35:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:48:40 +00:00
|
|
|
switch ((from->type & 0xFF))
|
2016-10-17 10:35:49 +00:00
|
|
|
{
|
|
|
|
case cJSON_Number:
|
|
|
|
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 10:35:49 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case cJSON_String:
|
|
|
|
if (strcmp(from->valuestring, to->valuestring) != 0)
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
|
2016-10-17 10:35:49 +00:00
|
|
|
}
|
|
|
|
return;
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2016-10-17 10:35:49 +00:00
|
|
|
case cJSON_Array:
|
|
|
|
{
|
2017-02-03 15:34:19 +00:00
|
|
|
size_t c = 0;
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */
|
2016-10-17 10:35:49 +00:00
|
|
|
/* generate patches for all array elements that exist in "from" and "to" */
|
|
|
|
for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++)
|
|
|
|
{
|
2017-02-03 15:34:19 +00:00
|
|
|
sprintf((char*)newpath, "%s/%lu", path, c); /* path of the current array element */
|
2016-10-17 10:35:49 +00:00
|
|
|
cJSONUtils_CompareToPatch(patches, newpath, from, to);
|
|
|
|
}
|
|
|
|
/* remove leftover elements from 'from' that are not in 'to' */
|
|
|
|
for (; from; from = from->next, c++)
|
|
|
|
{
|
2017-02-03 15:34:19 +00:00
|
|
|
sprintf((char*)newpath, "%lu", c);
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, newpath, 0);
|
2016-10-17 10:35:49 +00:00
|
|
|
}
|
|
|
|
/* add new elements in 'to' that were not in 'from' */
|
|
|
|
for (; to; to = to->next, c++)
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to);
|
2016-10-17 10:35:49 +00:00
|
|
|
}
|
|
|
|
free(newpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case cJSON_Object:
|
|
|
|
{
|
2016-11-13 11:05:44 +00:00
|
|
|
cJSON *a = NULL;
|
|
|
|
cJSON *b = NULL;
|
2016-10-17 10:35:49 +00: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 14:59:00 +00:00
|
|
|
int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string));
|
2016-10-17 10:35:49 +00:00
|
|
|
if (!diff)
|
|
|
|
{
|
|
|
|
/* both object keys are the same */
|
2017-02-03 14:59:00 +00:00
|
|
|
unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen((unsigned char*)a->string) + 2);
|
|
|
|
cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", path), (unsigned char*)a->string);
|
2016-10-17 10:35:49 +00: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 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)a->string, 0);
|
2016-10-17 10:35:49 +00:00
|
|
|
a = a->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* object element doesn't exist in 'from' --> add it */
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)b->string, b);
|
2016-10-17 10:35:49 +00:00
|
|
|
b = b->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2016-10-17 10:41:50 +00:00
|
|
|
cJSON* cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
|
2015-02-11 01:29:40 +00:00
|
|
|
{
|
2016-10-17 10:41:50 +00:00
|
|
|
cJSON *patches = cJSON_CreateArray();
|
2017-02-03 14:59:00 +00:00
|
|
|
cJSONUtils_CompareToPatch(patches, (const unsigned char*)"", from, to);
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2016-10-17 10:41:50 +00:00
|
|
|
return patches;
|
|
|
|
}
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2016-10-28 16:27:15 +00:00
|
|
|
/* sort lists using mergesort */
|
2015-02-13 19:19:26 +00:00
|
|
|
static cJSON *cJSONUtils_SortList(cJSON *list)
|
|
|
|
{
|
2016-10-28 16:27:15 +00:00
|
|
|
cJSON *first = list;
|
|
|
|
cJSON *second = list;
|
|
|
|
cJSON *ptr = list;
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2016-10-28 16:27:15 +00:00
|
|
|
if (!list || !list->next)
|
|
|
|
{
|
|
|
|
/* One entry is sorted already. */
|
|
|
|
return list;
|
|
|
|
}
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2017-02-03 14:59:00 +00:00
|
|
|
while (ptr && ptr->next && (cJSONUtils_strcasecmp((unsigned char*)ptr->string, (unsigned char*)ptr->next->string) < 0))
|
2016-10-28 16:27:15 +00:00
|
|
|
{
|
|
|
|
/* Test for list sorted. */
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
if (!ptr || !ptr->next)
|
|
|
|
{
|
|
|
|
/* Leave sorted lists unmodified. */
|
|
|
|
return list;
|
|
|
|
}
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2016-10-28 16:27:15 +00: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 10:45:40 +00:00
|
|
|
second->prev->next = NULL;
|
2016-10-28 16:27:15 +00:00
|
|
|
}
|
2015-02-13 19:19:26 +00:00
|
|
|
|
2016-10-28 16:27:15 +00:00
|
|
|
/* Recursively sort the sub-lists. */
|
|
|
|
first = cJSONUtils_SortList(first);
|
|
|
|
second = cJSONUtils_SortList(second);
|
2016-11-13 10:45:40 +00:00
|
|
|
list = ptr = NULL;
|
2016-10-28 16:27:15 +00:00
|
|
|
|
|
|
|
while (first && second) /* Merge the sub-lists */
|
|
|
|
{
|
2017-02-03 14:59:00 +00:00
|
|
|
if (cJSONUtils_strcasecmp((unsigned char*)first->string, (unsigned char*)second->string) < 0)
|
2016-10-28 16:27:15 +00: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 19:19:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 16:37:21 +00:00
|
|
|
void cJSONUtils_SortObject(cJSON *object)
|
|
|
|
{
|
|
|
|
object->child = cJSONUtils_SortList(object->child);
|
|
|
|
}
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2016-05-29 17:16:19 +00:00
|
|
|
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
|
|
|
|
{
|
2016-11-18 15:48:40 +00:00
|
|
|
if (!patch || ((patch->type & 0xFF) != cJSON_Object))
|
2016-10-29 05:40:41 +00:00
|
|
|
{
|
|
|
|
/* scalar value, array or NULL, just duplicate */
|
|
|
|
cJSON_Delete(target);
|
|
|
|
return cJSON_Duplicate(patch, 1);
|
|
|
|
}
|
2015-02-11 01:29:40 +00:00
|
|
|
|
2016-11-18 15:48:40 +00:00
|
|
|
if (!target || ((target->type & 0xFF) != cJSON_Object))
|
2016-10-29 05:40:41 +00:00
|
|
|
{
|
|
|
|
cJSON_Delete(target);
|
|
|
|
target = cJSON_CreateObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
patch = patch->child;
|
|
|
|
while (patch)
|
|
|
|
{
|
2016-11-18 15:48:40 +00:00
|
|
|
if ((patch->type & 0xFF) == cJSON_NULL)
|
2016-10-29 05:40:41 +00: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 16:39:54 +00:00
|
|
|
}
|
2016-05-29 17:16:19 +00:00
|
|
|
|
2016-10-29 15:15:49 +00:00
|
|
|
cJSON *cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
|
2016-05-29 17:16:19 +00:00
|
|
|
{
|
2016-11-13 10:45:40 +00:00
|
|
|
cJSON *patch = NULL;
|
2016-10-29 15:15:49 +00:00
|
|
|
if (!to)
|
|
|
|
{
|
|
|
|
/* patch to delete everything */
|
|
|
|
return cJSON_CreateNull();
|
|
|
|
}
|
2016-11-18 15:48:40 +00:00
|
|
|
if (((to->type & 0xFF) != cJSON_Object) || !from || ((from->type & 0xFF) != cJSON_Object))
|
2016-10-29 15:15:49 +00: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 10:45:40 +00:00
|
|
|
return NULL;
|
2016-10-29 15:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return patch;
|
2016-10-13 16:26:53 +00:00
|
|
|
}
|