CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target) { size_t c = 0; cJSON *obj = 0; if (object == target) { /* found */ return (char*)cJSONUtils_strdup((const unsigned char*)""); } /* recursively search all children of the object */ for (obj = object->child; obj; (void)(obj = obj->next), c++) { unsigned char *found = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(obj, target); if (found) { if (cJSON_IsArray(object)) { /* reserve enough memory for a 64 bit integer + '/' and '\0' */ unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + 23); /* 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(found); return NULL; } sprintf((char*)ret, "/%lu%s", (unsigned long)c, found); /* /<array_index><path> */ free(found); return (char*)ret; } else if (cJSON_IsObject(object)) { unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + cJSONUtils_PointerEncodedstrlen((unsigned char*)obj->string) + 2); *ret = '/'; cJSONUtils_PointerEncodedstrcpy(ret + 1, (unsigned char*)obj->string); strcat((char*)ret, (char*)found); free(found); return (char*)ret; } /* reached leaf of the tree, found nothing */ free(found); return NULL; } } /* not found */ return NULL; }
char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target) { int type = object->type; int c = 0; cJSON *obj = 0; if (object == target) { /* found */ return cJSONUtils_strdup(""); } /* recursively search all children of the object */ for (obj = object->child; obj; obj = obj->next, c++) { char *found = cJSONUtils_FindPointerFromObjectTo(obj, target); if (found) { if (type == cJSON_Array) { /* reserve enough memory for a 64 bit integer + '/' and '\0' */ char *ret = (char*)malloc(strlen(found) + 23); sprintf(ret, "/%d%s", c, found); /* /<array_index><path> */ free(found); return ret; } else if (type == cJSON_Object) { char *ret = (char*)malloc(strlen(found) + cJSONUtils_PointerEncodedstrlen(obj->string) + 2); *ret = '/'; cJSONUtils_PointerEncodedstrcpy(ret + 1, obj->string); strcat(ret, found); free(found); return ret; } /* reached leaf of the tree, found nothing */ free(found); return 0; } } /* not found */ return 0; }
static void cJSONUtils_GeneratePatch(cJSON *patches, const char *op, const char *path, const char *suffix, cJSON *val) { cJSON *patch = cJSON_CreateObject(); cJSON_AddItemToObject(patch, "op", cJSON_CreateString(op)); if (suffix) { char *newpath = (char*)malloc(strlen(path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2); cJSONUtils_PointerEncodedstrcpy(newpath + sprintf(newpath, "%s/", path), suffix); cJSON_AddItemToObject(patch, "path", cJSON_CreateString(newpath)); free(newpath); } else { cJSON_AddItemToObject(patch, "path", cJSON_CreateString(path)); } if (val) { cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1)); } cJSON_AddItemToArray(patches, patch); }
static void cJSONUtils_CompareToPatch(cJSON *patches, const char *path, cJSON *from, cJSON *to) { if (from->type != to->type) { cJSONUtils_GeneratePatch(patches, "replace", path, 0, to); return; } switch (from->type) { case cJSON_Number: if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble)) { cJSONUtils_GeneratePatch(patches, "replace", path, 0, to); } return; case cJSON_String: if (strcmp(from->valuestring, to->valuestring) != 0) { cJSONUtils_GeneratePatch(patches, "replace", path, 0, to); } return; case cJSON_Array: { int c; char *newpath = (char*)malloc(strlen(path) + 23); /* Allow space for 64bit int. */ /* 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++) { sprintf(newpath, "%s/%d", path, c); /* path of the current array element */ cJSONUtils_CompareToPatch(patches, newpath, from, to); } /* remove leftover elements from 'from' that are not in 'to' */ for (; from; from = from->next, c++) { sprintf(newpath, "%d", c); cJSONUtils_GeneratePatch(patches, "remove", path, newpath, 0); } /* add new elements in 'to' that were not in 'from' */ for (; to; to = to->next, c++) { cJSONUtils_GeneratePatch(patches, "add", path, "-", to); } free(newpath); return; } case cJSON_Object: { cJSON *a; cJSON *b; 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) { int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp(a->string, b->string)); if (!diff) { /* both object keys are the same */ char *newpath = (char*)malloc(strlen(path) + cJSONUtils_PointerEncodedstrlen(a->string) + 2); cJSONUtils_PointerEncodedstrcpy(newpath + sprintf(newpath, "%s/", path), a->string); /* 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 */ cJSONUtils_GeneratePatch(patches, "remove", path, a->string, 0); a = a->next; } else { /* object element doesn't exist in 'from' --> add it */ cJSONUtils_GeneratePatch(patches, "add", path, b->string, b); b = b->next; } } return; } default: break; } }
static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, cJSON *from, cJSON *to) { if ((from == NULL) || (to == NULL)) { return; } if ((from->type & 0xFF) != (to->type & 0xFF)) { cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to); return; } switch ((from->type & 0xFF)) { case cJSON_Number: if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble)) { cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to); } return; case cJSON_String: if (strcmp(from->valuestring, to->valuestring) != 0) { cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to); } return; case cJSON_Array: { size_t c = 0; unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */ /* generate patches for all array elements that exist in "from" and "to" */ for ((void)(c = 0), (void)(from = from->child), to = to->child; from && to; (void)(from = from->next), (void)(to = to->next), c++) { /* 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; } sprintf((char*)newpath, "%s/%lu", path, (unsigned long)c); /* path of the current array element */ cJSONUtils_CompareToPatch(patches, newpath, from, to); } /* remove leftover elements from 'from' that are not in 'to' */ for (; from; (void)(from = from->next), c++) { /* 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; } sprintf((char*)newpath, "%lu", (unsigned long)c); cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, newpath, 0); } /* add new elements in 'to' that were not in 'from' */ for (; to; (void)(to = to->next), c++) { cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to); } free(newpath); return; } case cJSON_Object: { cJSON *a = NULL; cJSON *b = NULL; 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) { int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string)); if (!diff) { /* both object keys are the same */ 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); /* 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 */ cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)a->string, 0); a = a->next; } else { /* object element doesn't exist in 'from' --> add it */ cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)b->string, b); b = b->next; } } return; } default: break; } }