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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_pointer.h"
  15. #include "strdup_compat.h"
  16. #include "vasprintf_compat.h"
  17. /* Avoid ctype.h and locale overhead */
  18. #define is_plain_digit(c) ((c) >= '0' && (c) <= '9')
  19. /**
  20. * JavaScript Object Notation (JSON) Pointer
  21. * RFC 6901 - https://tools.ietf.org/html/rfc6901
  22. */
  23. static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
  24. {
  25. size_t slen = strlen(s);
  26. size_t skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
  27. char *p = s;
  28. while ((p = strstr(p, occur)))
  29. {
  30. *p = repl_char;
  31. p++;
  32. slen -= skip;
  33. memmove(p, (p + skip), slen - (p - s) + 1); /* includes null char too */
  34. }
  35. }
  36. static int is_valid_index(struct json_object *jo, const char *path, size_t *idx)
  37. {
  38. size_t i, len = strlen(path);
  39. long int idx_val = -1;
  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. goto check_oob;
  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. idx_val = strtol(path, NULL, 10);
  69. if (idx_val < 0)
  70. {
  71. errno = EINVAL;
  72. return 0;
  73. }
  74. *idx = idx_val;
  75. check_oob:
  76. len = json_object_array_length(jo);
  77. if (*idx >= len)
  78. {
  79. errno = ENOENT;
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. static int json_pointer_get_single_path(struct json_object *obj, char *path,
  85. struct json_object **value)
  86. {
  87. if (json_object_is_type(obj, json_type_array))
  88. {
  89. size_t idx;
  90. if (!is_valid_index(obj, path, &idx))
  91. return -1;
  92. obj = json_object_array_get_idx(obj, idx);
  93. if (obj)
  94. {
  95. if (value)
  96. *value = obj;
  97. return 0;
  98. }
  99. /* Entry not found */
  100. errno = ENOENT;
  101. return -1;
  102. }
  103. /* RFC states that we first must eval all ~1 then all ~0 */
  104. string_replace_all_occurrences_with_char(path, "~1", '/');
  105. string_replace_all_occurrences_with_char(path, "~0", '~');
  106. if (!json_object_object_get_ex(obj, path, value))
  107. {
  108. errno = ENOENT;
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. static int json_pointer_set_single_path(struct json_object *parent, const char *path,
  114. struct json_object *value)
  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(parent, path, &idx))
  123. return -1;
  124. return json_object_array_put_idx(parent, idx, value);
  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_get_recursive(struct json_object *obj, char *path,
  138. struct json_object **value)
  139. {
  140. char *endp;
  141. int rc;
  142. /* All paths (on each recursion level must have a leading '/' */
  143. if (path[0] != '/')
  144. {
  145. errno = EINVAL;
  146. return -1;
  147. }
  148. path++;
  149. endp = strchr(path, '/');
  150. if (endp)
  151. *endp = '\0';
  152. /* If we err-ed here, return here */
  153. if ((rc = json_pointer_get_single_path(obj, path, &obj)))
  154. return rc;
  155. if (endp)
  156. {
  157. /* Put the slash back, so that the sanity check passes on next recursion level */
  158. *endp = '/';
  159. return json_pointer_get_recursive(obj, endp, value);
  160. }
  161. /* We should be at the end of the recursion here */
  162. if (value)
  163. *value = obj;
  164. return 0;
  165. }
  166. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
  167. {
  168. char *path_copy = NULL;
  169. int rc;
  170. if (!obj || !path)
  171. {
  172. errno = EINVAL;
  173. return -1;
  174. }
  175. if (path[0] == '\0')
  176. {
  177. if (res)
  178. *res = obj;
  179. return 0;
  180. }
  181. /* pass a working copy to the recursive call */
  182. if (!(path_copy = strdup(path)))
  183. {
  184. errno = ENOMEM;
  185. return -1;
  186. }
  187. rc = json_pointer_get_recursive(obj, path_copy, res);
  188. free(path_copy);
  189. return rc;
  190. }
  191. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
  192. {
  193. char *path_copy = NULL;
  194. int rc = 0;
  195. va_list args;
  196. if (!obj || !path_fmt)
  197. {
  198. errno = EINVAL;
  199. return -1;
  200. }
  201. va_start(args, path_fmt);
  202. rc = vasprintf(&path_copy, path_fmt, args);
  203. va_end(args);
  204. if (rc < 0)
  205. return rc;
  206. if (path_copy[0] == '\0')
  207. {
  208. if (res)
  209. *res = obj;
  210. goto out;
  211. }
  212. rc = json_pointer_get_recursive(obj, path_copy, res);
  213. out:
  214. free(path_copy);
  215. return rc;
  216. }
  217. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
  218. {
  219. const char *endp;
  220. char *path_copy = NULL;
  221. struct json_object *set = NULL;
  222. int rc;
  223. if (!obj || !path)
  224. {
  225. errno = EINVAL;
  226. return -1;
  227. }
  228. if (path[0] == '\0')
  229. {
  230. json_object_put(*obj);
  231. *obj = value;
  232. return 0;
  233. }
  234. if (path[0] != '/')
  235. {
  236. errno = EINVAL;
  237. return -1;
  238. }
  239. /* If there's only 1 level to set, stop here */
  240. if ((endp = strrchr(path, '/')) == path)
  241. {
  242. path++;
  243. return json_pointer_set_single_path(*obj, path, value);
  244. }
  245. /* pass a working copy to the recursive call */
  246. if (!(path_copy = strdup(path)))
  247. {
  248. errno = ENOMEM;
  249. return -1;
  250. }
  251. path_copy[endp - path] = '\0';
  252. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  253. free(path_copy);
  254. if (rc)
  255. return rc;
  256. endp++;
  257. return json_pointer_set_single_path(set, endp, value);
  258. }
  259. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
  260. ...)
  261. {
  262. char *endp;
  263. char *path_copy = NULL;
  264. struct json_object *set = NULL;
  265. va_list args;
  266. int rc = 0;
  267. if (!obj || !path_fmt)
  268. {
  269. errno = EINVAL;
  270. return -1;
  271. }
  272. /* pass a working copy to the recursive call */
  273. va_start(args, path_fmt);
  274. rc = vasprintf(&path_copy, path_fmt, args);
  275. va_end(args);
  276. if (rc < 0)
  277. return rc;
  278. if (path_copy[0] == '\0')
  279. {
  280. json_object_put(*obj);
  281. *obj = value;
  282. goto out;
  283. }
  284. if (path_copy[0] != '/')
  285. {
  286. errno = EINVAL;
  287. rc = -1;
  288. goto out;
  289. }
  290. /* If there's only 1 level to set, stop here */
  291. if ((endp = strrchr(path_copy, '/')) == path_copy)
  292. {
  293. set = *obj;
  294. goto set_single_path;
  295. }
  296. *endp = '\0';
  297. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  298. if (rc)
  299. goto out;
  300. set_single_path:
  301. endp++;
  302. rc = json_pointer_set_single_path(set, endp, value);
  303. out:
  304. free(path_copy);
  305. return rc;
  306. }