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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_pointer_set_single_path(struct json_object *parent, const char *path,
  107. struct json_object *value)
  108. {
  109. if (json_object_is_type(parent, json_type_array))
  110. {
  111. size_t idx;
  112. /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
  113. if (path[0] == '-' && path[1] == '\0')
  114. return json_object_array_add(parent, value);
  115. if (!is_valid_index(path, &idx))
  116. return -1;
  117. return json_object_array_put_idx(parent, idx, value);
  118. }
  119. /* path replacements should have been done in json_pointer_get_single_path(),
  120. * and we should still be good here
  121. */
  122. if (json_object_is_type(parent, json_type_object))
  123. return json_object_object_add(parent, path, value);
  124. /* Getting here means that we tried to "dereference" a primitive JSON type
  125. * (like string, int, bool).i.e. add a sub-object to it
  126. */
  127. errno = ENOENT;
  128. return -1;
  129. }
  130. static int json_pointer_result_get_recursive(struct json_object *obj, char *path,
  131. struct json_pointer_get_result *res)
  132. {
  133. struct json_object *parent_obj = obj;
  134. size_t idx;
  135. char *endp;
  136. int rc;
  137. /* All paths (on each recursion level must have a leading '/' */
  138. if (path[0] != '/')
  139. {
  140. errno = EINVAL;
  141. return -1;
  142. }
  143. path++;
  144. endp = strchr(path, '/');
  145. if (endp)
  146. *endp = '\0';
  147. /* If we err-ed here, return here */
  148. if ((rc = json_pointer_get_single_path(obj, path, &obj, &idx)))
  149. return rc;
  150. if (endp)
  151. {
  152. /* Put the slash back, so that the sanity check passes on next recursion level */
  153. *endp = '/';
  154. return json_pointer_result_get_recursive(obj, endp, res);
  155. }
  156. /* We should be at the end of the recursion here */
  157. if (res) {
  158. res->parent = parent_obj;
  159. res->obj = obj;
  160. if (json_object_is_type(res->parent, json_type_array))
  161. res->id.index = idx;
  162. else
  163. res->id.key = path;
  164. }
  165. return 0;
  166. }
  167. static int json_pointer_object_get_recursive(struct json_object *obj, char *path,
  168. struct json_object **value)
  169. {
  170. struct json_pointer_get_result res;
  171. int rc;
  172. rc = json_pointer_result_get_recursive(obj, path, &res);
  173. if (rc)
  174. return rc;
  175. if (value)
  176. *value = res.obj;
  177. return 0;
  178. }
  179. int json_pointer_get_internal(struct json_object *obj, const char *path,
  180. struct json_pointer_get_result *res)
  181. {
  182. char *path_copy = NULL;
  183. int rc;
  184. if (!obj || !path)
  185. {
  186. errno = EINVAL;
  187. return -1;
  188. }
  189. if (path[0] == '\0')
  190. {
  191. if (res) {
  192. res->parent = NULL;
  193. res->obj = obj;
  194. }
  195. res->id.key = NULL;
  196. return 0;
  197. }
  198. /* pass a working copy to the recursive call */
  199. if (!(path_copy = strdup(path)))
  200. {
  201. errno = ENOMEM;
  202. return -1;
  203. }
  204. rc = json_pointer_result_get_recursive(obj, path_copy, res);
  205. /* re-map the path string to the const-path string */
  206. if (rc == 0 && res->id.key && !json_object_is_type(res->parent, json_type_array))
  207. res->id.key = path + (res->id.key - path_copy);
  208. free(path_copy);
  209. return rc;
  210. }
  211. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
  212. {
  213. struct json_pointer_get_result jpres;
  214. int rc;
  215. rc = json_pointer_get_internal(obj, path, &jpres);
  216. if (rc)
  217. return rc;
  218. if (res)
  219. *res = jpres.obj;
  220. return 0;
  221. }
  222. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
  223. {
  224. char *path_copy = NULL;
  225. int rc = 0;
  226. va_list args;
  227. if (!obj || !path_fmt)
  228. {
  229. errno = EINVAL;
  230. return -1;
  231. }
  232. va_start(args, path_fmt);
  233. rc = vasprintf(&path_copy, path_fmt, args);
  234. va_end(args);
  235. if (rc < 0)
  236. return rc;
  237. if (path_copy[0] == '\0')
  238. {
  239. if (res)
  240. *res = obj;
  241. goto out;
  242. }
  243. rc = json_pointer_object_get_recursive(obj, path_copy, res);
  244. out:
  245. free(path_copy);
  246. return rc;
  247. }
  248. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
  249. {
  250. const char *endp;
  251. char *path_copy = NULL;
  252. struct json_object *set = NULL;
  253. int rc;
  254. if (!obj || !path)
  255. {
  256. errno = EINVAL;
  257. return -1;
  258. }
  259. if (path[0] == '\0')
  260. {
  261. json_object_put(*obj);
  262. *obj = value;
  263. return 0;
  264. }
  265. if (path[0] != '/')
  266. {
  267. errno = EINVAL;
  268. return -1;
  269. }
  270. /* If there's only 1 level to set, stop here */
  271. if ((endp = strrchr(path, '/')) == path)
  272. {
  273. path++;
  274. return json_pointer_set_single_path(*obj, path, value);
  275. }
  276. /* pass a working copy to the recursive call */
  277. if (!(path_copy = strdup(path)))
  278. {
  279. errno = ENOMEM;
  280. return -1;
  281. }
  282. path_copy[endp - path] = '\0';
  283. rc = json_pointer_object_get_recursive(*obj, path_copy, &set);
  284. free(path_copy);
  285. if (rc)
  286. return rc;
  287. endp++;
  288. return json_pointer_set_single_path(set, endp, value);
  289. }
  290. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
  291. ...)
  292. {
  293. char *endp;
  294. char *path_copy = NULL;
  295. struct json_object *set = NULL;
  296. va_list args;
  297. int rc = 0;
  298. if (!obj || !path_fmt)
  299. {
  300. errno = EINVAL;
  301. return -1;
  302. }
  303. /* pass a working copy to the recursive call */
  304. va_start(args, path_fmt);
  305. rc = vasprintf(&path_copy, path_fmt, args);
  306. va_end(args);
  307. if (rc < 0)
  308. return rc;
  309. if (path_copy[0] == '\0')
  310. {
  311. json_object_put(*obj);
  312. *obj = value;
  313. goto out;
  314. }
  315. if (path_copy[0] != '/')
  316. {
  317. errno = EINVAL;
  318. rc = -1;
  319. goto out;
  320. }
  321. /* If there's only 1 level to set, stop here */
  322. if ((endp = strrchr(path_copy, '/')) == path_copy)
  323. {
  324. set = *obj;
  325. goto set_single_path;
  326. }
  327. *endp = '\0';
  328. rc = json_pointer_object_get_recursive(*obj, path_copy, &set);
  329. if (rc)
  330. goto out;
  331. set_single_path:
  332. endp++;
  333. rc = json_pointer_set_single_path(set, endp, value);
  334. out:
  335. free(path_copy);
  336. return rc;
  337. }