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.4 kB

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