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 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2021 Alexandru Ardelean.
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See COPYING for details.
  6. *
  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. /**
  15. * JavaScript Object Notation (JSON) Patch
  16. * RFC 6902 - https://tools.ietf.org/html/rfc6902
  17. */
  18. static int json_patch_apply_test(struct json_object **res,
  19. struct json_object *patch_elem,
  20. const char *path)
  21. {
  22. struct json_object *value1, *value2;
  23. if (!json_object_object_get_ex(patch_elem, "value", &value1)) {
  24. errno = EINVAL;
  25. return -1;
  26. }
  27. /* errno should be set by json_pointer_get() */
  28. if (json_pointer_get(*res, path, &value2))
  29. return -1;
  30. if (!json_object_equal(value1, value2)) {
  31. json_object_put(*res);
  32. *res = NULL;
  33. errno = ENOENT;
  34. return -1;
  35. }
  36. return 0;
  37. }
  38. static int __json_patch_apply_remove(struct json_pointer_get_result *jpres)
  39. {
  40. if (json_object_is_type(jpres->parent, json_type_array)) {
  41. return json_object_array_del_idx(jpres->parent, jpres->id.index, 1);
  42. } else if (jpres->parent && jpres->id.key) {
  43. json_object_object_del(jpres->parent, jpres->id.key);
  44. return 0;
  45. } else {
  46. return json_object_put(jpres->obj);
  47. }
  48. }
  49. static int json_patch_apply_remove(struct json_object **res, const char *path)
  50. {
  51. struct json_pointer_get_result jpres;
  52. if (json_pointer_get_internal(*res, path, &jpres))
  53. return -1;
  54. return __json_patch_apply_remove(&jpres);
  55. }
  56. static int json_object_array_insert_idx_cb(struct json_object *parent, size_t idx,
  57. struct json_object *value, void *priv)
  58. {
  59. int *add = priv;
  60. if (idx > json_object_array_length(parent))
  61. {
  62. errno = EINVAL;
  63. return -1;
  64. }
  65. if (*add)
  66. return json_object_array_insert_idx(parent, idx, value);
  67. else
  68. return json_object_array_put_idx(parent, idx, value);
  69. }
  70. static int json_patch_apply_add_replace(struct json_object **res,
  71. struct json_object *patch_elem,
  72. const char *path, int add)
  73. {
  74. struct json_object *value;
  75. int rc;
  76. if (!json_object_object_get_ex(patch_elem, "value", &value)) {
  77. errno = EINVAL;
  78. return -1;
  79. }
  80. /* if this is a replace op, then we need to make sure it exists before replacing */
  81. if (!add && json_pointer_get(*res, path, NULL)) {
  82. errno = ENOENT;
  83. return -1;
  84. }
  85. rc = json_pointer_set_with_array_cb(res, path, json_object_get(value),
  86. json_object_array_insert_idx_cb, &add);
  87. if (rc)
  88. json_object_put(value);
  89. return rc;
  90. }
  91. static int json_object_array_move_cb(struct json_object *parent, size_t idx,
  92. struct json_object *value, void *priv)
  93. {
  94. struct json_pointer_get_result *from = priv;
  95. size_t len = json_object_array_length(parent);
  96. /**
  97. * If it's the same array parent, it means that we removed
  98. * and element from it, so the length is temporarily reduced
  99. * by 1, which means that if we try to move an element to
  100. * the last position, we need to check the current length + 1
  101. */
  102. if (parent == from->parent)
  103. len++;
  104. if (idx > len)
  105. {
  106. errno = EINVAL;
  107. return -1;
  108. }
  109. return json_object_array_insert_idx(parent, idx, value);
  110. }
  111. static int json_patch_apply_move_copy(struct json_object **res,
  112. struct json_object *patch_elem,
  113. const char *path, int move)
  114. {
  115. json_pointer_array_set_cb array_set_cb;
  116. struct json_pointer_get_result from;
  117. struct json_object *jfrom;
  118. const char *from_s;
  119. size_t from_s_len;
  120. int rc;
  121. if (!json_object_object_get_ex(patch_elem, "from", &jfrom)) {
  122. errno = EINVAL;
  123. return -1;
  124. }
  125. from_s = json_object_get_string(jfrom);
  126. from_s_len = strlen(from_s);
  127. if (strncmp(from_s, path, from_s_len) == 0) {
  128. /**
  129. * If lengths match, it's a noop, if they don't,
  130. * then we're trying to move a parent under a child
  131. * which is not allowed as per RFC 6902 section 4.4
  132. * The "from" location MUST NOT be a proper prefix of the "path"
  133. * location; i.e., a location cannot be moved into one of its children.
  134. */
  135. if (from_s_len == strlen(path))
  136. return 0;
  137. errno = EINVAL;
  138. return -1;
  139. }
  140. rc = json_pointer_get_internal(*res, from_s, &from);
  141. if (rc)
  142. return rc;
  143. json_object_get(from.obj);
  144. if (!move) {
  145. array_set_cb = json_object_array_insert_idx_cb;
  146. } else {
  147. rc = __json_patch_apply_remove(&from);
  148. if (rc < 0) {
  149. json_object_put(from.obj);
  150. return rc;
  151. }
  152. array_set_cb = json_object_array_move_cb;
  153. }
  154. rc = json_pointer_set_with_array_cb(res, path, from.obj, array_set_cb, &from);
  155. if (rc)
  156. json_object_put(from.obj);
  157. return rc;
  158. }
  159. int json_patch_apply(struct json_object *base, struct json_object *patch,
  160. struct json_object **res)
  161. {
  162. size_t i;
  163. int rc = 0;
  164. if (!base || !json_object_is_type(patch, json_type_array)) {
  165. errno = EINVAL;
  166. return -1;
  167. }
  168. /* errno should be set inside json_object_deep_copy() */
  169. if (json_object_deep_copy(base, res, NULL) < 0)
  170. return -1;
  171. /* Go through all operations ; apply them on res */
  172. for (i = 0; i < json_object_array_length(patch); i++) {
  173. struct json_object *jop, *jpath;
  174. struct json_object *patch_elem = json_object_array_get_idx(patch, i);
  175. const char *op, *path;
  176. if (!json_object_object_get_ex(patch_elem, "op", &jop)) {
  177. errno = EINVAL;
  178. rc = -1;
  179. break;
  180. }
  181. op = json_object_get_string(jop);
  182. json_object_object_get_ex(patch_elem, "path", &jpath);
  183. path = json_object_get_string(jpath);
  184. if (!strcmp(op, "test"))
  185. rc = json_patch_apply_test(res, patch_elem, path);
  186. else if (!strcmp(op, "remove"))
  187. rc = json_patch_apply_remove(res, path);
  188. else if (!strcmp(op, "add"))
  189. rc = json_patch_apply_add_replace(res, patch_elem, path, 1);
  190. else if (!strcmp(op, "replace"))
  191. rc = json_patch_apply_add_replace(res, patch_elem, path, 0);
  192. else if (!strcmp(op, "move"))
  193. rc = json_patch_apply_move_copy(res, patch_elem, path, 1);
  194. else if (!strcmp(op, "copy"))
  195. rc = json_patch_apply_move_copy(res, patch_elem, path, 0);
  196. else {
  197. errno = EINVAL;
  198. rc = -1;
  199. break;
  200. }
  201. if (rc < 0)
  202. break;
  203. }
  204. if (rc < 0) {
  205. json_object_put(*res);
  206. *res = NULL;
  207. }
  208. return rc;
  209. }