You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_patch.c 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2021 Alexandru Ardelean.
  3. * Copyright (c) 2023 Eric Hawicz
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the MIT license. See COPYING for details.
  7. */
  8. #include "config.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "json_patch.h"
  13. #include "json_object_private.h"
  14. #include "json_pointer_private.h"
  15. #include <limits.h>
  16. #ifndef SIZE_T_MAX
  17. #if SIZEOF_SIZE_T == SIZEOF_INT
  18. #define SIZE_T_MAX UINT_MAX
  19. #elif SIZEOF_SIZE_T == SIZEOF_LONG
  20. #define SIZE_T_MAX ULONG_MAX
  21. #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
  22. #define SIZE_T_MAX ULLONG_MAX
  23. #else
  24. #error Unable to determine size of size_t
  25. #endif
  26. #endif
  27. #define _set_err(_errval, _errmsg) do { \
  28. patch_error->errno_code = (_errval); \
  29. patch_error->errmsg = (_errmsg); \
  30. errno = 0; /* To avoid confusion */ \
  31. } while (0)
  32. #define _set_err_from_ptrget(_errval, _fieldname) do { \
  33. patch_error->errno_code = (_errval); \
  34. patch_error->errmsg = (_errval) == ENOENT ? \
  35. "Did not find element referenced by " _fieldname " field" : \
  36. "Invalid " _fieldname " field"; \
  37. errno = 0; /* To avoid confusion */ \
  38. } while(0)
  39. /**
  40. * JavaScript Object Notation (JSON) Patch
  41. * RFC 6902 - https://tools.ietf.org/html/rfc6902
  42. */
  43. static int json_patch_apply_test(struct json_object **res,
  44. struct json_object *patch_elem,
  45. const char *path, struct json_patch_error *patch_error)
  46. {
  47. struct json_object *value1, *value2;
  48. if (!json_object_object_get_ex(patch_elem, "value", &value1)) {
  49. _set_err(EINVAL, "Patch object does not contain a 'value' field");
  50. return -1;
  51. }
  52. if (json_pointer_get(*res, path, &value2))
  53. {
  54. _set_err_from_ptrget(errno, "path");
  55. return -1;
  56. }
  57. if (!json_object_equal(value1, value2)) {
  58. _set_err(ENOENT, "Value of element referenced by 'path' field did not match 'value' field");
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int __json_patch_apply_remove(struct json_pointer_get_result *jpres)
  64. {
  65. if (json_object_is_type(jpres->parent, json_type_array)) {
  66. return json_object_array_del_idx(jpres->parent, jpres->index_in_parent, 1);
  67. } else if (jpres->parent && jpres->key_in_parent) {
  68. json_object_object_del(jpres->parent, jpres->key_in_parent);
  69. return 0;
  70. } else {
  71. return json_object_put(jpres->obj);
  72. }
  73. }
  74. static int json_patch_apply_remove(struct json_object **res, const char *path, struct json_patch_error *patch_error)
  75. {
  76. struct json_pointer_get_result jpres;
  77. int rc;
  78. if (json_pointer_get_internal(*res, path, &jpres))
  79. {
  80. _set_err_from_ptrget(errno, "path");
  81. return -1;
  82. }
  83. rc = __json_patch_apply_remove(&jpres);
  84. if (rc < 0)
  85. _set_err(EINVAL, "Unable to remove path referenced by 'path' field");
  86. return rc;
  87. }
  88. // callback for json_pointer_set_with_array_cb()
  89. static int json_object_array_insert_idx_cb(struct json_object *parent, size_t idx,
  90. struct json_object *value, void *priv)
  91. {
  92. int rc;
  93. int *add = priv;
  94. if (idx > json_object_array_length(parent))
  95. {
  96. // Note: will propagate back out through json_pointer_set_with_array_cb()
  97. errno = EINVAL;
  98. return -1;
  99. }
  100. if (*add)
  101. rc = json_object_array_insert_idx(parent, idx, value);
  102. else
  103. rc = json_object_array_put_idx(parent, idx, value);
  104. if (rc < 0)
  105. errno = EINVAL;
  106. return rc;
  107. }
  108. static int json_patch_apply_add_replace(struct json_object **res,
  109. struct json_object *patch_elem,
  110. const char *path, int add, struct json_patch_error *patch_error)
  111. {
  112. struct json_object *value;
  113. int rc;
  114. if (!json_object_object_get_ex(patch_elem, "value", &value)) {
  115. _set_err(EINVAL, "Patch object does not contain a 'value' field");
  116. return -1;
  117. }
  118. /* if this is a replace op, then we need to make sure it exists before replacing */
  119. if (!add && json_pointer_get(*res, path, NULL)) {
  120. _set_err_from_ptrget(errno, "path");
  121. return -1;
  122. }
  123. rc = json_pointer_set_with_array_cb(res, path, json_object_get(value),
  124. json_object_array_insert_idx_cb, &add);
  125. if (rc)
  126. {
  127. _set_err(errno, "Failed to set value at path referenced by 'path' field");
  128. json_object_put(value);
  129. }
  130. return rc;
  131. }
  132. // callback for json_pointer_set_with_array_cb()
  133. static int json_object_array_move_cb(struct json_object *parent, size_t idx,
  134. struct json_object *value, void *priv)
  135. {
  136. int rc;
  137. struct json_pointer_get_result *from = priv;
  138. size_t len = json_object_array_length(parent);
  139. /**
  140. * If it's the same array parent, it means that we removed
  141. * and element from it, so the length is temporarily reduced
  142. * by 1, which means that if we try to move an element to
  143. * the last position, we need to check the current length + 1
  144. */
  145. if (parent == from->parent)
  146. len++;
  147. if (idx > len)
  148. {
  149. // Note: will propagate back out through json_pointer_set_with_array_cb()
  150. errno = EINVAL;
  151. return -1;
  152. }
  153. rc = json_object_array_insert_idx(parent, idx, value);
  154. if (rc < 0)
  155. errno = EINVAL;
  156. return rc;
  157. }
  158. static int json_patch_apply_move_copy(struct json_object **res,
  159. struct json_object *patch_elem,
  160. const char *path, int move, struct json_patch_error *patch_error)
  161. {
  162. json_pointer_array_set_cb array_set_cb;
  163. struct json_pointer_get_result from;
  164. struct json_object *jfrom;
  165. const char *from_s;
  166. size_t from_s_len;
  167. int rc;
  168. if (!json_object_object_get_ex(patch_elem, "from", &jfrom)) {
  169. _set_err(EINVAL, "Patch does not contain a 'from' field");
  170. return -1;
  171. }
  172. from_s = json_object_get_string(jfrom);
  173. from_s_len = strlen(from_s);
  174. if (strncmp(from_s, path, from_s_len) == 0) {
  175. /**
  176. * If lengths match, it's a noop, if they don't,
  177. * then we're trying to move a parent under a child
  178. * which is not allowed as per RFC 6902 section 4.4
  179. * The "from" location MUST NOT be a proper prefix of the "path"
  180. * location; i.e., a location cannot be moved into one of its children.
  181. */
  182. if (from_s_len == strlen(path))
  183. return 0;
  184. _set_err(EINVAL, "Invalid attempt to move parent under a child");
  185. return -1;
  186. }
  187. rc = json_pointer_get_internal(*res, from_s, &from);
  188. if (rc)
  189. {
  190. _set_err_from_ptrget(errno, "from");
  191. return rc;
  192. }
  193. json_object_get(from.obj);
  194. if (!move) {
  195. array_set_cb = json_object_array_insert_idx_cb;
  196. } else {
  197. rc = __json_patch_apply_remove(&from);
  198. if (rc < 0) {
  199. json_object_put(from.obj);
  200. return rc;
  201. }
  202. array_set_cb = json_object_array_move_cb;
  203. }
  204. rc = json_pointer_set_with_array_cb(res, path, from.obj, array_set_cb, &from);
  205. if (rc)
  206. {
  207. _set_err(errno, "Failed to set value at path referenced by 'path' field");
  208. json_object_put(from.obj);
  209. }
  210. return rc;
  211. }
  212. int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
  213. struct json_object **base, struct json_patch_error *patch_error)
  214. {
  215. size_t ii;
  216. int rc = 0;
  217. struct json_patch_error placeholder;
  218. if (!patch_error)
  219. patch_error = &placeholder;
  220. patch_error->patch_failure_idx = SIZE_T_MAX;
  221. patch_error->errno_code = 0;
  222. if (base == NULL||
  223. (*base == NULL && copy_from == NULL) ||
  224. (*base != NULL && copy_from != NULL))
  225. {
  226. _set_err(EFAULT, "Exactly one of *base or copy_from must be non-NULL");
  227. return -1;
  228. }
  229. if (!json_object_is_type(patch, json_type_array)) {
  230. _set_err(EFAULT, "Patch object is not of type json_type_array");
  231. return -1;
  232. }
  233. if (copy_from != NULL)
  234. {
  235. if (json_object_deep_copy(copy_from, base, NULL) < 0)
  236. {
  237. _set_err(ENOMEM, "Unable to copy copy_from using json_object_deep_copy()");
  238. return -1;
  239. }
  240. }
  241. /* Go through all operations ; apply them on res */
  242. for (ii = 0; ii < json_object_array_length(patch); ii++) {
  243. struct json_object *jop, *jpath;
  244. struct json_object *patch_elem = json_object_array_get_idx(patch, ii);
  245. const char *op, *path;
  246. patch_error->patch_failure_idx = ii;
  247. if (!json_object_object_get_ex(patch_elem, "op", &jop)) {
  248. _set_err(EINVAL, "Patch object does not contain 'op' field");
  249. return -1;
  250. }
  251. op = json_object_get_string(jop);
  252. if (!json_object_object_get_ex(patch_elem, "path", &jpath)) {
  253. _set_err(EINVAL, "Patch object does not contain 'path' field");
  254. return -1;
  255. }
  256. path = json_object_get_string(jpath); // Note: empty string is ok!
  257. if (!strcmp(op, "test"))
  258. rc = json_patch_apply_test(base, patch_elem, path, patch_error);
  259. else if (!strcmp(op, "remove"))
  260. rc = json_patch_apply_remove(base, path, patch_error);
  261. else if (!strcmp(op, "add"))
  262. rc = json_patch_apply_add_replace(base, patch_elem, path, 1, patch_error);
  263. else if (!strcmp(op, "replace"))
  264. rc = json_patch_apply_add_replace(base, patch_elem, path, 0, patch_error);
  265. else if (!strcmp(op, "move"))
  266. rc = json_patch_apply_move_copy(base, patch_elem, path, 1, patch_error);
  267. else if (!strcmp(op, "copy"))
  268. rc = json_patch_apply_move_copy(base, patch_elem, path, 0, patch_error);
  269. else {
  270. _set_err(EINVAL, "Patch object has invalid 'op' field");
  271. return -1;
  272. }
  273. if (rc < 0)
  274. break;
  275. }
  276. return rc;
  277. }