cJSON_Utils.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <limits.h>
  6. #include "cJSON_Utils.h"
  7. static unsigned char* cJSONUtils_strdup(const unsigned char* str)
  8. {
  9. size_t len = 0;
  10. unsigned char *copy = NULL;
  11. len = strlen((const char*)str) + 1;
  12. if (!(copy = (unsigned char*)malloc(len)))
  13. {
  14. return NULL;
  15. }
  16. memcpy(copy, str, len);
  17. return copy;
  18. }
  19. static int cJSONUtils_strcasecmp(const unsigned char *s1, const unsigned char *s2)
  20. {
  21. if (!s1)
  22. {
  23. return (s1 == s2) ? 0 : 1; /* both NULL? */
  24. }
  25. if (!s2)
  26. {
  27. return 1;
  28. }
  29. for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
  30. {
  31. if(*s1 == 0)
  32. {
  33. return 0;
  34. }
  35. }
  36. return tolower(*s1) - tolower(*s2);
  37. }
  38. /* JSON Pointer implementation: */
  39. static int cJSONUtils_Pstrcasecmp(const unsigned char *a, const unsigned char *e)
  40. {
  41. if (!a || !e)
  42. {
  43. return (a == e) ? 0 : 1; /* both NULL? */
  44. }
  45. for (; *a && *e && (*e != '/'); a++, e++) /* compare until next '/' */
  46. {
  47. if (*e == '~')
  48. {
  49. /* check for escaped '~' (~0) and '/' (~1) */
  50. if (!((e[1] == '0') && (*a == '~')) && !((e[1] == '1') && (*a == '/')))
  51. {
  52. /* invalid escape sequence or wrong character in *a */
  53. return 1;
  54. }
  55. else
  56. {
  57. e++;
  58. }
  59. }
  60. else if (tolower(*a) != tolower(*e))
  61. {
  62. return 1;
  63. }
  64. }
  65. if (((*e != 0) && (*e != '/')) != (*a != 0))
  66. {
  67. /* one string has ended, the other not */
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *s)
  73. {
  74. size_t l = 0;
  75. for (; *s; s++, l++)
  76. {
  77. if ((*s == '~') || (*s == '/'))
  78. {
  79. l++;
  80. }
  81. }
  82. return l;
  83. }
  84. static void cJSONUtils_PointerEncodedstrcpy(unsigned char *d, const unsigned char *s)
  85. {
  86. for (; *s; s++)
  87. {
  88. if (*s == '/')
  89. {
  90. *d++ = '~';
  91. *d++ = '1';
  92. }
  93. else if (*s == '~')
  94. {
  95. *d++ = '~';
  96. *d++ = '0';
  97. }
  98. else
  99. {
  100. *d++ = *s;
  101. }
  102. }
  103. *d = '\0';
  104. }
  105. char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
  106. {
  107. int type = object->type;
  108. size_t c = 0;
  109. cJSON *obj = 0;
  110. if (object == target)
  111. {
  112. /* found */
  113. return (char*)cJSONUtils_strdup((const unsigned char*)"");
  114. }
  115. /* recursively search all children of the object */
  116. for (obj = object->child; obj; obj = obj->next, c++)
  117. {
  118. unsigned char *found = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(obj, target);
  119. if (found)
  120. {
  121. if ((type & 0xFF) == cJSON_Array)
  122. {
  123. /* reserve enough memory for a 64 bit integer + '/' and '\0' */
  124. unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + 23);
  125. sprintf((char*)ret, "/%lu%s", c, found); /* /<array_index><path> */
  126. free(found);
  127. return (char*)ret;
  128. }
  129. else if ((type & 0xFF) == cJSON_Object)
  130. {
  131. unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + cJSONUtils_PointerEncodedstrlen((unsigned char*)obj->string) + 2);
  132. *ret = '/';
  133. cJSONUtils_PointerEncodedstrcpy(ret + 1, (unsigned char*)obj->string);
  134. strcat((char*)ret, (char*)found);
  135. free(found);
  136. return (char*)ret;
  137. }
  138. /* reached leaf of the tree, found nothing */
  139. free(found);
  140. return NULL;
  141. }
  142. }
  143. /* not found */
  144. return NULL;
  145. }
  146. cJSON *cJSONUtils_GetPointer(cJSON *object, const char *pointer)
  147. {
  148. /* follow path of the pointer */
  149. while ((*pointer++ == '/') && object)
  150. {
  151. if ((object->type & 0xFF) == cJSON_Array)
  152. {
  153. size_t which = 0;
  154. /* parse array index */
  155. while ((*pointer >= '0') && (*pointer <= '9'))
  156. {
  157. which = (10 * which) + (size_t)(*pointer++ - '0');
  158. }
  159. if (*pointer && (*pointer != '/'))
  160. {
  161. /* not end of string or new path token */
  162. return NULL;
  163. }
  164. if (which > INT_MAX)
  165. {
  166. return NULL;
  167. }
  168. object = cJSON_GetArrayItem(object, (int)which);
  169. }
  170. else if ((object->type & 0xFF) == cJSON_Object)
  171. {
  172. object = object->child;
  173. /* GetObjectItem. */
  174. while (object && cJSONUtils_Pstrcasecmp((unsigned char*)object->string, (const unsigned char*)pointer))
  175. {
  176. object = object->next;
  177. }
  178. /* skip to the next path token or end of string */
  179. while (*pointer && (*pointer != '/'))
  180. {
  181. pointer++;
  182. }
  183. }
  184. else
  185. {
  186. return NULL;
  187. }
  188. }
  189. return object;
  190. }
  191. /* JSON Patch implementation. */
  192. static void cJSONUtils_InplaceDecodePointerString(unsigned char *string)
  193. {
  194. unsigned char *s2 = string;
  195. if (string == NULL) {
  196. return;
  197. }
  198. for (; *string; s2++, string++)
  199. {
  200. *s2 = (*string != '~')
  201. ? (*string)
  202. : ((*(++string) == '0')
  203. ? '~'
  204. : '/');
  205. }
  206. *s2 = '\0';
  207. }
  208. static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
  209. {
  210. unsigned char *parentptr = NULL;
  211. unsigned char *childptr = NULL;
  212. cJSON *parent = NULL;
  213. cJSON *ret = NULL;
  214. /* copy path and split it in parent and child */
  215. parentptr = cJSONUtils_strdup(path);
  216. if (parentptr == NULL) {
  217. return NULL;
  218. }
  219. childptr = (unsigned char*)strrchr((char*)parentptr, '/'); /* last '/' */
  220. if (childptr == NULL)
  221. {
  222. free(parentptr);
  223. return NULL;
  224. }
  225. /* split strings */
  226. *childptr++ = '\0';
  227. parent = cJSONUtils_GetPointer(object, (char*)parentptr);
  228. cJSONUtils_InplaceDecodePointerString(childptr);
  229. if (!parent)
  230. {
  231. /* Couldn't find object to remove child from. */
  232. ret = NULL;
  233. }
  234. else if ((parent->type & 0xFF) == cJSON_Array)
  235. {
  236. ret = cJSON_DetachItemFromArray(parent, atoi((char*)childptr));
  237. }
  238. else if ((parent->type & 0xFF) == cJSON_Object)
  239. {
  240. ret = cJSON_DetachItemFromObject(parent, (char*)childptr);
  241. }
  242. free(parentptr);
  243. /* return the detachted item */
  244. return ret;
  245. }
  246. static int cJSONUtils_Compare(cJSON *a, cJSON *b)
  247. {
  248. if ((a->type & 0xFF) != (b->type & 0xFF))
  249. {
  250. /* mismatched type. */
  251. return -1;
  252. }
  253. switch (a->type & 0xFF)
  254. {
  255. case cJSON_Number:
  256. /* numeric mismatch. */
  257. return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
  258. case cJSON_String:
  259. /* string mismatch. */
  260. return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
  261. case cJSON_Array:
  262. for (a = a->child, b = b->child; a && b; a = a->next, b = b->next)
  263. {
  264. int err = cJSONUtils_Compare(a, b);
  265. if (err)
  266. {
  267. return err;
  268. }
  269. }
  270. /* array size mismatch? (one of both children is not NULL) */
  271. return (a || b) ? -4 : 0;
  272. case cJSON_Object:
  273. cJSONUtils_SortObject(a);
  274. cJSONUtils_SortObject(b);
  275. a = a->child;
  276. b = b->child;
  277. while (a && b)
  278. {
  279. int err = 0;
  280. /* compare object keys */
  281. if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
  282. {
  283. /* missing member */
  284. return -6;
  285. }
  286. err = cJSONUtils_Compare(a, b);
  287. if (err)
  288. {
  289. return err;
  290. }
  291. a = a->next;
  292. b = b->next;
  293. }
  294. /* object length mismatch (one of both children is not null) */
  295. return (a || b) ? -5 : 0;
  296. default:
  297. break;
  298. }
  299. /* null, true or false */
  300. return 0;
  301. }
  302. static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
  303. {
  304. cJSON *op = NULL;
  305. cJSON *path = NULL;
  306. cJSON *value = NULL;
  307. cJSON *parent = NULL;
  308. int opcode = 0;
  309. unsigned char *parentptr = NULL;
  310. unsigned char *childptr = NULL;
  311. op = cJSON_GetObjectItem(patch, "op");
  312. path = cJSON_GetObjectItem(patch, "path");
  313. if (!op || !path)
  314. {
  315. /* malformed patch. */
  316. return 2;
  317. }
  318. /* decode operation */
  319. if (!strcmp(op->valuestring, "add"))
  320. {
  321. opcode = 0;
  322. }
  323. else if (!strcmp(op->valuestring, "remove"))
  324. {
  325. opcode = 1;
  326. }
  327. else if (!strcmp(op->valuestring, "replace"))
  328. {
  329. opcode = 2;
  330. }
  331. else if (!strcmp(op->valuestring, "move"))
  332. {
  333. opcode = 3;
  334. }
  335. else if (!strcmp(op->valuestring, "copy"))
  336. {
  337. opcode = 4;
  338. }
  339. else if (!strcmp(op->valuestring, "test"))
  340. {
  341. /* compare value: {...} with the given path */
  342. return cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
  343. }
  344. else
  345. {
  346. /* unknown opcode. */
  347. return 3;
  348. }
  349. /* Remove/Replace */
  350. if ((opcode == 1) || (opcode == 2))
  351. {
  352. /* Get rid of old. */
  353. cJSON_Delete(cJSONUtils_PatchDetach(object, (unsigned char*)path->valuestring));
  354. if (opcode == 1)
  355. {
  356. /* For Remove, this is job done. */
  357. return 0;
  358. }
  359. }
  360. /* Copy/Move uses "from". */
  361. if ((opcode == 3) || (opcode == 4))
  362. {
  363. cJSON *from = cJSON_GetObjectItem(patch, "from");
  364. if (!from)
  365. {
  366. /* missing "from" for copy/move. */
  367. return 4;
  368. }
  369. if (opcode == 3)
  370. {
  371. /* move */
  372. value = cJSONUtils_PatchDetach(object, (unsigned char*)from->valuestring);
  373. }
  374. if (opcode == 4)
  375. {
  376. /* copy */
  377. value = cJSONUtils_GetPointer(object, from->valuestring);
  378. }
  379. if (!value)
  380. {
  381. /* missing "from" for copy/move. */
  382. return 5;
  383. }
  384. if (opcode == 4)
  385. {
  386. value = cJSON_Duplicate(value, 1);
  387. }
  388. if (!value)
  389. {
  390. /* out of memory for copy/move. */
  391. return 6;
  392. }
  393. }
  394. else /* Add/Replace uses "value". */
  395. {
  396. value = cJSON_GetObjectItem(patch, "value");
  397. if (!value)
  398. {
  399. /* missing "value" for add/replace. */
  400. return 7;
  401. }
  402. value = cJSON_Duplicate(value, 1);
  403. if (!value)
  404. {
  405. /* out of memory for add/replace. */
  406. return 8;
  407. }
  408. }
  409. /* Now, just add "value" to "path". */
  410. /* split pointer in parent and child */
  411. parentptr = cJSONUtils_strdup((unsigned char*)path->valuestring);
  412. childptr = (unsigned char*)strrchr((char*)parentptr, '/');
  413. if (childptr)
  414. {
  415. *childptr++ = '\0';
  416. }
  417. parent = cJSONUtils_GetPointer(object, (char*)parentptr);
  418. cJSONUtils_InplaceDecodePointerString(childptr);
  419. /* add, remove, replace, move, copy, test. */
  420. if (!parent)
  421. {
  422. /* Couldn't find object to add to. */
  423. free(parentptr);
  424. cJSON_Delete(value);
  425. return 9;
  426. }
  427. else if ((parent->type & 0xFF) == cJSON_Array)
  428. {
  429. if (!strcmp((char*)childptr, "-"))
  430. {
  431. cJSON_AddItemToArray(parent, value);
  432. }
  433. else
  434. {
  435. cJSON_InsertItemInArray(parent, atoi((char*)childptr), value);
  436. }
  437. }
  438. else if ((parent->type & 0xFF) == cJSON_Object)
  439. {
  440. cJSON_DeleteItemFromObject(parent, (char*)childptr);
  441. cJSON_AddItemToObject(parent, (char*)childptr, value);
  442. }
  443. else
  444. {
  445. cJSON_Delete(value);
  446. }
  447. free(parentptr);
  448. return 0;
  449. }
  450. int cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
  451. {
  452. int err = 0;
  453. if (patches == NULL)
  454. {
  455. return 1;
  456. }
  457. if ((patches->type & 0xFF) != cJSON_Array)
  458. {
  459. /* malformed patches. */
  460. return 1;
  461. }
  462. if (patches)
  463. {
  464. patches = patches->child;
  465. }
  466. while (patches)
  467. {
  468. if ((err = cJSONUtils_ApplyPatch(object, patches)))
  469. {
  470. return err;
  471. }
  472. patches = patches->next;
  473. }
  474. return 0;
  475. }
  476. static void cJSONUtils_GeneratePatch(cJSON *patches, const unsigned char *op, const unsigned char *path, const unsigned char *suffix, cJSON *val)
  477. {
  478. cJSON *patch = cJSON_CreateObject();
  479. cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)op));
  480. if (suffix)
  481. {
  482. unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
  483. cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", (const char*)path), suffix);
  484. cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)newpath));
  485. free(newpath);
  486. }
  487. else
  488. {
  489. cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
  490. }
  491. if (val)
  492. {
  493. cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
  494. }
  495. cJSON_AddItemToArray(patches, patch);
  496. }
  497. void cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
  498. {
  499. cJSONUtils_GeneratePatch(array, (const unsigned char*)op, (const unsigned char*)path, 0, val);
  500. }
  501. static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, cJSON *from, cJSON *to)
  502. {
  503. if ((from == NULL) || (to == NULL))
  504. {
  505. return;
  506. }
  507. if ((from->type & 0xFF) != (to->type & 0xFF))
  508. {
  509. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
  510. return;
  511. }
  512. switch ((from->type & 0xFF))
  513. {
  514. case cJSON_Number:
  515. if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
  516. {
  517. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
  518. }
  519. return;
  520. case cJSON_String:
  521. if (strcmp(from->valuestring, to->valuestring) != 0)
  522. {
  523. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
  524. }
  525. return;
  526. case cJSON_Array:
  527. {
  528. size_t c = 0;
  529. unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */
  530. /* generate patches for all array elements that exist in "from" and "to" */
  531. for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++)
  532. {
  533. sprintf((char*)newpath, "%s/%lu", path, c); /* path of the current array element */
  534. cJSONUtils_CompareToPatch(patches, newpath, from, to);
  535. }
  536. /* remove leftover elements from 'from' that are not in 'to' */
  537. for (; from; from = from->next, c++)
  538. {
  539. sprintf((char*)newpath, "%lu", c);
  540. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, newpath, 0);
  541. }
  542. /* add new elements in 'to' that were not in 'from' */
  543. for (; to; to = to->next, c++)
  544. {
  545. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to);
  546. }
  547. free(newpath);
  548. return;
  549. }
  550. case cJSON_Object:
  551. {
  552. cJSON *a = NULL;
  553. cJSON *b = NULL;
  554. cJSONUtils_SortObject(from);
  555. cJSONUtils_SortObject(to);
  556. a = from->child;
  557. b = to->child;
  558. /* for all object values in the object with more of them */
  559. while (a || b)
  560. {
  561. int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string));
  562. if (!diff)
  563. {
  564. /* both object keys are the same */
  565. unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen((unsigned char*)a->string) + 2);
  566. cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", path), (unsigned char*)a->string);
  567. /* create a patch for the element */
  568. cJSONUtils_CompareToPatch(patches, newpath, a, b);
  569. free(newpath);
  570. a = a->next;
  571. b = b->next;
  572. }
  573. else if (diff < 0)
  574. {
  575. /* object element doesn't exist in 'to' --> remove it */
  576. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)a->string, 0);
  577. a = a->next;
  578. }
  579. else
  580. {
  581. /* object element doesn't exist in 'from' --> add it */
  582. cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)b->string, b);
  583. b = b->next;
  584. }
  585. }
  586. return;
  587. }
  588. default:
  589. break;
  590. }
  591. }
  592. cJSON* cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
  593. {
  594. cJSON *patches = cJSON_CreateArray();
  595. cJSONUtils_CompareToPatch(patches, (const unsigned char*)"", from, to);
  596. return patches;
  597. }
  598. /* sort lists using mergesort */
  599. static cJSON *cJSONUtils_SortList(cJSON *list)
  600. {
  601. cJSON *first = list;
  602. cJSON *second = list;
  603. cJSON *ptr = list;
  604. if (!list || !list->next)
  605. {
  606. /* One entry is sorted already. */
  607. return list;
  608. }
  609. while (ptr && ptr->next && (cJSONUtils_strcasecmp((unsigned char*)ptr->string, (unsigned char*)ptr->next->string) < 0))
  610. {
  611. /* Test for list sorted. */
  612. ptr = ptr->next;
  613. }
  614. if (!ptr || !ptr->next)
  615. {
  616. /* Leave sorted lists unmodified. */
  617. return list;
  618. }
  619. /* reset ptr to the beginning */
  620. ptr = list;
  621. while (ptr)
  622. {
  623. /* Walk two pointers to find the middle. */
  624. second = second->next;
  625. ptr = ptr->next;
  626. /* advances ptr two steps at a time */
  627. if (ptr)
  628. {
  629. ptr = ptr->next;
  630. }
  631. }
  632. if (second && second->prev)
  633. {
  634. /* Split the lists */
  635. second->prev->next = NULL;
  636. }
  637. /* Recursively sort the sub-lists. */
  638. first = cJSONUtils_SortList(first);
  639. second = cJSONUtils_SortList(second);
  640. list = ptr = NULL;
  641. while (first && second) /* Merge the sub-lists */
  642. {
  643. if (cJSONUtils_strcasecmp((unsigned char*)first->string, (unsigned char*)second->string) < 0)
  644. {
  645. if (!list)
  646. {
  647. /* start merged list with the first element of the first list */
  648. list = ptr = first;
  649. }
  650. else
  651. {
  652. /* add first element of first list to merged list */
  653. ptr->next = first;
  654. first->prev = ptr;
  655. ptr = first;
  656. }
  657. first = first->next;
  658. }
  659. else
  660. {
  661. if (!list)
  662. {
  663. /* start merged list with the first element of the second list */
  664. list = ptr = second;
  665. }
  666. else
  667. {
  668. /* add first element of second list to merged list */
  669. ptr->next = second;
  670. second->prev = ptr;
  671. ptr = second;
  672. }
  673. second = second->next;
  674. }
  675. }
  676. if (first)
  677. {
  678. /* Append rest of first list. */
  679. if (!list)
  680. {
  681. return first;
  682. }
  683. ptr->next = first;
  684. first->prev = ptr;
  685. }
  686. if (second)
  687. {
  688. /* Append rest of second list */
  689. if (!list)
  690. {
  691. return second;
  692. }
  693. ptr->next = second;
  694. second->prev = ptr;
  695. }
  696. return list;
  697. }
  698. void cJSONUtils_SortObject(cJSON *object)
  699. {
  700. object->child = cJSONUtils_SortList(object->child);
  701. }
  702. cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
  703. {
  704. if (!patch || ((patch->type & 0xFF) != cJSON_Object))
  705. {
  706. /* scalar value, array or NULL, just duplicate */
  707. cJSON_Delete(target);
  708. return cJSON_Duplicate(patch, 1);
  709. }
  710. if (!target || ((target->type & 0xFF) != cJSON_Object))
  711. {
  712. cJSON_Delete(target);
  713. target = cJSON_CreateObject();
  714. }
  715. patch = patch->child;
  716. while (patch)
  717. {
  718. if ((patch->type & 0xFF) == cJSON_NULL)
  719. {
  720. /* NULL is the indicator to remove a value, see RFC7396 */
  721. cJSON_DeleteItemFromObject(target, patch->string);
  722. }
  723. else
  724. {
  725. cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
  726. cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
  727. }
  728. patch = patch->next;
  729. }
  730. return target;
  731. }
  732. cJSON *cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
  733. {
  734. cJSON *patch = NULL;
  735. if (!to)
  736. {
  737. /* patch to delete everything */
  738. return cJSON_CreateNull();
  739. }
  740. if (((to->type & 0xFF) != cJSON_Object) || !from || ((from->type & 0xFF) != cJSON_Object))
  741. {
  742. return cJSON_Duplicate(to, 1);
  743. }
  744. cJSONUtils_SortObject(from);
  745. cJSONUtils_SortObject(to);
  746. from = from->child;
  747. to = to->child;
  748. patch = cJSON_CreateObject();
  749. while (from || to)
  750. {
  751. int compare = from ? (to ? strcmp(from->string, to->string) : -1) : 1;
  752. if (compare < 0)
  753. {
  754. /* from has a value that to doesn't have -> remove */
  755. cJSON_AddItemToObject(patch, from->string, cJSON_CreateNull());
  756. from = from->next;
  757. }
  758. else if (compare > 0)
  759. {
  760. /* to has a value that from doesn't have -> add to patch */
  761. cJSON_AddItemToObject(patch, to->string, cJSON_Duplicate(to, 1));
  762. to = to->next;
  763. }
  764. else
  765. {
  766. /* object key exists in both objects */
  767. if (cJSONUtils_Compare(from, to))
  768. {
  769. /* not identical --> generate a patch */
  770. cJSON_AddItemToObject(patch, to->string, cJSONUtils_GenerateMergePatch(from, to));
  771. }
  772. /* next key in the object */
  773. from = from->next;
  774. to = to->next;
  775. }
  776. }
  777. if (!patch->child)
  778. {
  779. cJSON_Delete(patch);
  780. return NULL;
  781. }
  782. return patch;
  783. }