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.8 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 "strdup_compat.h"
  17. #include "vasprintf_compat.h"
  18. /* Avoid ctype.h and locale overhead */
  19. #define is_plain_digit(c) ((c) >= '0' && (c) <= '9')
  20. /**
  21. * JavaScript Object Notation (JSON) Pointer
  22. * RFC 6901 - https://tools.ietf.org/html/rfc6901
  23. */
  24. static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
  25. {
  26. size_t slen = strlen(s);
  27. size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
  28. char *p = s;
  29. while ((p = strstr(p, occur)))
  30. {
  31. *p = repl_char;
  32. p++;
  33. slen -= skip;
  34. memmove(p, (p + skip), slen - (p - s) + 1); /* includes null char too */
  35. }
  36. }
  37. static int is_valid_index(const char *path, size_t *idx)
  38. {
  39. size_t i, len = strlen(path);
  40. /* this code-path optimizes a bit, for when we reference the 0-9 index range
  41. * in a JSON array and because leading zeros not allowed
  42. */
  43. if (len == 1)
  44. {
  45. if (is_plain_digit(path[0]))
  46. {
  47. *idx = (path[0] - '0');
  48. return 1;
  49. }
  50. errno = EINVAL;
  51. return 0;
  52. }
  53. /* leading zeros not allowed per RFC */
  54. if (path[0] == '0')
  55. {
  56. errno = EINVAL;
  57. return 0;
  58. }
  59. /* RFC states base-10 decimals */
  60. for (i = 0; i < len; i++)
  61. {
  62. if (!is_plain_digit(path[i]))
  63. {
  64. errno = EINVAL;
  65. return 0;
  66. }
  67. }
  68. // We know it's all digits, so the only error case here is overflow,
  69. // but ULLONG_MAX will be longer than any array length so that's ok.
  70. *idx = strtoull(path, NULL, 10);
  71. return 1;
  72. }
  73. static int json_pointer_get_single_path(struct json_object *obj, char *path,
  74. struct json_object **value, size_t *idx)
  75. {
  76. if (json_object_is_type(obj, json_type_array))
  77. {
  78. if (!is_valid_index(path, idx))
  79. return -1;
  80. if (*idx >= json_object_array_length(obj))
  81. {
  82. errno = ENOENT;
  83. return -1;
  84. }
  85. obj = json_object_array_get_idx(obj, *idx);
  86. if (obj)
  87. {
  88. if (value)
  89. *value = obj;
  90. return 0;
  91. }
  92. /* Entry not found */
  93. errno = ENOENT;
  94. return -1;
  95. }
  96. /* RFC states that we first must eval all ~1 then all ~0 */
  97. string_replace_all_occurrences_with_char(path, "~1", '/');
  98. string_replace_all_occurrences_with_char(path, "~0", '~');
  99. if (!json_object_object_get_ex(obj, path, value))
  100. {
  101. errno = ENOENT;
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. static int json_object_array_put_idx_cb(struct json_object *parent, size_t idx,
  107. struct json_object *value, void *priv)
  108. {
  109. return json_object_array_put_idx(parent, idx, value);
  110. }
  111. static int json_pointer_set_single_path(struct json_object *parent, const char *path,
  112. struct json_object *value,
  113. json_pointer_array_set_cb array_set_cb, void *priv)
  114. {
  115. if (json_object_is_type(parent, json_type_array))
  116. {
  117. size_t idx;
  118. /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
  119. if (path[0] == '-' && path[1] == '\0')
  120. return json_object_array_add(parent, value);
  121. if (!is_valid_index(path, &idx))
  122. return -1;
  123. return array_set_cb(parent, idx, value, priv);
  124. }
  125. /* path replacements should have been done in json_pointer_get_single_path(),
  126. * and we should still be good here
  127. */
  128. if (json_object_is_type(parent, json_type_object))
  129. return json_object_object_add(parent, path, value);
  130. /* Getting here means that we tried to "dereference" a primitive JSON type
  131. * (like string, int, bool).i.e. add a sub-object to it
  132. */
  133. errno = ENOENT;
  134. return -1;
  135. }
  136. static int json_pointer_result_get_recursive(struct json_object *obj, char *path,
  137. struct json_pointer_get_result *res)
  138. {
  139. struct json_object *parent_obj = obj;
  140. size_t idx;
  141. char *endp;
  142. int rc;
  143. /* All paths (on each recursion level must have a leading '/' */
  144. if (path[0] != '/')
  145. {
  146. errno = EINVAL;
  147. return -1;
  148. }
  149. path++;
  150. endp = strchr(path, '/');
  151. if (endp)
  152. *endp = '\0';
  153. /* If we err-ed here, return here */
  154. if ((rc = json_pointer_get_single_path(obj, path, &obj, &idx)))
  155. return rc;
  156. if (endp)
  157. {
  158. /* Put the slash back, so that the sanity check passes on next recursion level */
  159. *endp = '/';
  160. return json_pointer_result_get_recursive(obj, endp, res);
  161. }
  162. /* We should be at the end of the recursion here */
  163. if (res) {
  164. res->parent = parent_obj;
  165. res->obj = obj;
  166. if (json_object_is_type(res->parent, json_type_array))
  167. res->id.index = idx;
  168. else
  169. res->id.key = path;
  170. }
  171. return 0;
  172. }
  173. static int json_pointer_object_get_recursive(struct json_object *obj, char *path,
  174. struct json_object **value)
  175. {
  176. struct json_pointer_get_result res;
  177. int rc;
  178. rc = json_pointer_result_get_recursive(obj, path, &res);
  179. if (rc)
  180. return rc;
  181. if (value)
  182. *value = res.obj;
  183. return 0;
  184. }
  185. int json_pointer_get_internal(struct json_object *obj, const char *path,
  186. struct json_pointer_get_result *res)
  187. {
  188. char *path_copy = NULL;
  189. int rc;
  190. if (!obj || !path)
  191. {
  192. errno = EINVAL;
  193. return -1;
  194. }
  195. if (path[0] == '\0')
  196. {
  197. if (res) {
  198. res->parent = NULL;
  199. res->obj = obj;
  200. }
  201. res->id.key = NULL;
  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 && res->id.key && !json_object_is_type(res->parent, json_type_array))
  213. res->id.key = path + (res->id.key - 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. }