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_pointer.c 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (c) 2016 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 "strerror_override.h"
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "json_object_private.h"
  15. #include "json_pointer.h"
  16. #include "json_pointer_private.h"
  17. #include "strdup_compat.h"
  18. #include "vasprintf_compat.h"
  19. /* Avoid ctype.h and locale overhead */
  20. #define is_plain_digit(c) ((c) >= '0' && (c) <= '9')
  21. /**
  22. * JavaScript Object Notation (JSON) Pointer
  23. * RFC 6901 - https://tools.ietf.org/html/rfc6901
  24. */
  25. static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
  26. {
  27. size_t slen = strlen(s);
  28. size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
  29. char *p = s;
  30. while ((p = strstr(p, occur)))
  31. {
  32. *p = repl_char;
  33. p++;
  34. slen -= skip;
  35. memmove(p, (p + skip), slen - (p - s) + 1); /* includes null char too */
  36. }
  37. }
  38. static int is_valid_index(const char *path, size_t *idx)
  39. {
  40. size_t i, len = strlen(path);
  41. /* this code-path optimizes a bit, for when we reference the 0-9 index range
  42. * in a JSON array and because leading zeros not allowed
  43. */
  44. if (len == 1)
  45. {
  46. if (is_plain_digit(path[0]))
  47. {
  48. *idx = (path[0] - '0');
  49. return 1;
  50. }
  51. errno = EINVAL;
  52. return 0;
  53. }
  54. /* leading zeros not allowed per RFC */
  55. if (path[0] == '0')
  56. {
  57. errno = EINVAL;
  58. return 0;
  59. }
  60. /* RFC states base-10 decimals */
  61. for (i = 0; i < len; i++)
  62. {
  63. if (!is_plain_digit(path[i]))
  64. {
  65. errno = EINVAL;
  66. return 0;
  67. }
  68. }
  69. // We know it's all digits, so the only error case here is overflow,
  70. // but ULLONG_MAX will be longer than any array length so that's ok.
  71. *idx = strtoull(path, NULL, 10);
  72. return 1;
  73. }
  74. static int json_pointer_get_single_path(struct json_object *obj, char *path,
  75. struct json_object **value, size_t *idx)
  76. {
  77. if (json_object_is_type(obj, json_type_array))
  78. {
  79. if (!is_valid_index(path, idx))
  80. return -1;
  81. if (*idx >= json_object_array_length(obj))
  82. {
  83. errno = ENOENT;
  84. return -1;
  85. }
  86. obj = json_object_array_get_idx(obj, *idx);
  87. if (obj)
  88. {
  89. if (value)
  90. *value = obj;
  91. return 0;
  92. }
  93. /* Entry not found */
  94. errno = ENOENT;
  95. return -1;
  96. }
  97. /* RFC states that we first must eval all ~1 then all ~0 */
  98. string_replace_all_occurrences_with_char(path, "~1", '/');
  99. string_replace_all_occurrences_with_char(path, "~0", '~');
  100. if (!json_object_object_get_ex(obj, path, value))
  101. {
  102. errno = ENOENT;
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. static int json_object_array_put_idx_cb(struct json_object *parent, size_t idx,
  108. struct json_object *value, void *priv)
  109. {
  110. return json_object_array_put_idx(parent, idx, value);
  111. }
  112. static int json_pointer_set_single_path(struct json_object *parent, const char *path,
  113. struct json_object *value,
  114. json_pointer_array_set_cb array_set_cb, void *priv)
  115. {
  116. if (json_object_is_type(parent, json_type_array))
  117. {
  118. size_t idx;
  119. /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
  120. if (path[0] == '-' && path[1] == '\0')
  121. return json_object_array_add(parent, value);
  122. if (!is_valid_index(path, &idx))
  123. return -1;
  124. return array_set_cb(parent, idx, value, priv);
  125. }
  126. /* path replacements should have been done in json_pointer_get_single_path(),
  127. * and we should still be good here
  128. */
  129. if (json_object_is_type(parent, json_type_object))
  130. return json_object_object_add(parent, path, value);
  131. /* Getting here means that we tried to "dereference" a primitive JSON type
  132. * (like string, int, bool).i.e. add a sub-object to it
  133. */
  134. errno = ENOENT;
  135. return -1;
  136. }
  137. static int json_pointer_result_get_recursive(struct json_object *obj, char *path,
  138. struct json_pointer_get_result *res)
  139. {
  140. struct json_object *parent_obj = obj;
  141. size_t idx = 0;
  142. char *endp;
  143. int rc;
  144. /* All paths (on each recursion level must have a leading '/' */
  145. if (path[0] != '/')
  146. {
  147. errno = EINVAL;
  148. return -1;
  149. }
  150. path++;
  151. endp = strchr(path, '/');
  152. if (endp)
  153. *endp = '\0';
  154. /* If we err-ed here, return here */
  155. if ((rc = json_pointer_get_single_path(obj, path, &obj, &idx)))
  156. return rc;
  157. if (endp)
  158. {
  159. /* Put the slash back, so that the sanity check passes on next recursion level */
  160. *endp = '/';
  161. return json_pointer_result_get_recursive(obj, endp, res);
  162. }
  163. /* We should be at the end of the recursion here */
  164. if (res) {
  165. res->parent = parent_obj;
  166. res->obj = obj;
  167. if (json_object_is_type(res->parent, json_type_array))
  168. res->index_in_parent = idx;
  169. else
  170. res->key_in_parent = path;
  171. }
  172. return 0;
  173. }
  174. static int json_pointer_object_get_recursive(struct json_object *obj, char *path,
  175. struct json_object **value)
  176. {
  177. struct json_pointer_get_result res;
  178. int rc;
  179. rc = json_pointer_result_get_recursive(obj, path, &res);
  180. if (rc)
  181. return rc;
  182. if (value)
  183. *value = res.obj;
  184. return 0;
  185. }
  186. int json_pointer_get_internal(struct json_object *obj, const char *path,
  187. struct json_pointer_get_result *res)
  188. {
  189. char *path_copy = NULL;
  190. int rc;
  191. if (!obj || !path)
  192. {
  193. errno = EINVAL;
  194. return -1;
  195. }
  196. if (path[0] == '\0')
  197. {
  198. res->parent = NULL;
  199. res->obj = obj;
  200. res->key_in_parent = NULL;
  201. res->index_in_parent = UINT32_MAX;
  202. return 0;
  203. }
  204. /* pass a working copy to the recursive call */
  205. if (!(path_copy = strdup(path)))
  206. {
  207. errno = ENOMEM;
  208. return -1;
  209. }
  210. rc = json_pointer_result_get_recursive(obj, path_copy, res);
  211. /* re-map the path string to the const-path string */
  212. if (rc == 0 && json_object_is_type(res->parent, json_type_object) && res->key_in_parent)
  213. res->key_in_parent = path + (res->key_in_parent - path_copy);
  214. free(path_copy);
  215. return rc;
  216. }
  217. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
  218. {
  219. struct json_pointer_get_result jpres;
  220. int rc;
  221. rc = json_pointer_get_internal(obj, path, &jpres);
  222. if (rc)
  223. return rc;
  224. if (res)
  225. *res = jpres.obj;
  226. return 0;
  227. }
  228. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
  229. {
  230. char *path_copy = NULL;
  231. int rc = 0;
  232. va_list args;
  233. if (!obj || !path_fmt)
  234. {
  235. errno = EINVAL;
  236. return -1;
  237. }
  238. va_start(args, path_fmt);
  239. rc = vasprintf(&path_copy, path_fmt, args);
  240. va_end(args);
  241. if (rc < 0)
  242. return rc;
  243. if (path_copy[0] == '\0')
  244. {
  245. if (res)
  246. *res = obj;
  247. goto out;
  248. }
  249. rc = json_pointer_object_get_recursive(obj, path_copy, res);
  250. out:
  251. free(path_copy);
  252. return rc;
  253. }
  254. int json_pointer_set_with_array_cb(struct json_object **obj, const char *path,
  255. struct json_object *value,
  256. json_pointer_array_set_cb array_set_cb, void *priv)
  257. {
  258. const char *endp;
  259. char *path_copy = NULL;
  260. struct json_object *set = NULL;
  261. int rc;
  262. if (!obj || !path)
  263. {
  264. errno = EINVAL;
  265. return -1;
  266. }
  267. if (path[0] == '\0')
  268. {
  269. json_object_put(*obj);
  270. *obj = value;
  271. return 0;
  272. }
  273. if (path[0] != '/')
  274. {
  275. errno = EINVAL;
  276. return -1;
  277. }
  278. /* If there's only 1 level to set, stop here */
  279. if ((endp = strrchr(path, '/')) == path)
  280. {
  281. path++;
  282. return json_pointer_set_single_path(*obj, path, value, array_set_cb, priv);
  283. }
  284. /* pass a working copy to the recursive call */
  285. if (!(path_copy = strdup(path)))
  286. {
  287. errno = ENOMEM;
  288. return -1;
  289. }
  290. path_copy[endp - path] = '\0';
  291. rc = json_pointer_object_get_recursive(*obj, path_copy, &set);
  292. free(path_copy);
  293. if (rc)
  294. return rc;
  295. endp++;
  296. return json_pointer_set_single_path(set, endp, value, array_set_cb, priv);
  297. }
  298. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
  299. {
  300. return json_pointer_set_with_array_cb(obj, path, value, json_object_array_put_idx_cb, NULL);
  301. }
  302. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
  303. ...)
  304. {
  305. char *endp;
  306. char *path_copy = NULL;
  307. struct json_object *set = NULL;
  308. va_list args;
  309. int rc = 0;
  310. if (!obj || !path_fmt)
  311. {
  312. errno = EINVAL;
  313. return -1;
  314. }
  315. /* pass a working copy to the recursive call */
  316. va_start(args, path_fmt);
  317. rc = vasprintf(&path_copy, path_fmt, args);
  318. va_end(args);
  319. if (rc < 0)
  320. return rc;
  321. if (path_copy[0] == '\0')
  322. {
  323. json_object_put(*obj);
  324. *obj = value;
  325. goto out;
  326. }
  327. if (path_copy[0] != '/')
  328. {
  329. errno = EINVAL;
  330. rc = -1;
  331. goto out;
  332. }
  333. /* If there's only 1 level to set, stop here */
  334. if ((endp = strrchr(path_copy, '/')) == path_copy)
  335. {
  336. set = *obj;
  337. goto set_single_path;
  338. }
  339. *endp = '\0';
  340. rc = json_pointer_object_get_recursive(*obj, path_copy, &set);
  341. if (rc)
  342. goto out;
  343. set_single_path:
  344. endp++;
  345. rc = json_pointer_set_single_path(set, endp, value,
  346. json_object_array_put_idx_cb, NULL);
  347. out:
  348. free(path_copy);
  349. return rc;
  350. }