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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /* this code-path optimizes a bit, for when we reference the 0-9 index range
  40. * in a JSON array and because leading zeros not allowed
  41. */
  42. if (len == 1)
  43. {
  44. if (is_plain_digit(path[0]))
  45. {
  46. *idx = (path[0] - '0');
  47. goto check_oob;
  48. }
  49. errno = EINVAL;
  50. return 0;
  51. }
  52. /* leading zeros not allowed per RFC */
  53. if (path[0] == '0')
  54. {
  55. errno = EINVAL;
  56. return 0;
  57. }
  58. /* RFC states base-10 decimals */
  59. for (i = 0; i < len; i++)
  60. {
  61. if (!is_plain_digit(path[i]))
  62. {
  63. errno = EINVAL;
  64. return 0;
  65. }
  66. }
  67. long int idx_val = strtol(path, NULL, 10);
  68. if (idx_val < 0)
  69. {
  70. errno = EINVAL;
  71. return 0;
  72. }
  73. *idx = idx_val;
  74. check_oob:
  75. len = json_object_array_length(jo);
  76. if (*idx >= len)
  77. {
  78. errno = ENOENT;
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. static int json_pointer_get_single_path(struct json_object *obj, char *path,
  84. struct json_object **value)
  85. {
  86. if (json_object_is_type(obj, json_type_array))
  87. {
  88. size_t idx;
  89. if (!is_valid_index(obj, path, &idx))
  90. return -1;
  91. obj = json_object_array_get_idx(obj, idx);
  92. if (obj)
  93. {
  94. if (value)
  95. *value = obj;
  96. return 0;
  97. }
  98. /* Entry not found */
  99. errno = ENOENT;
  100. return -1;
  101. }
  102. /* RFC states that we first must eval all ~1 then all ~0 */
  103. string_replace_all_occurrences_with_char(path, "~1", '/');
  104. string_replace_all_occurrences_with_char(path, "~0", '~');
  105. if (!json_object_object_get_ex(obj, path, value))
  106. {
  107. errno = ENOENT;
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. static int json_pointer_set_single_path(struct json_object *parent, const char *path,
  113. struct json_object *value)
  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(parent, path, &idx))
  122. return -1;
  123. return json_object_array_put_idx(parent, idx, value);
  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_get_recursive(struct json_object *obj, char *path,
  137. struct json_object **value)
  138. {
  139. char *endp;
  140. int rc;
  141. /* All paths (on each recursion level must have a leading '/' */
  142. if (path[0] != '/')
  143. {
  144. errno = EINVAL;
  145. return -1;
  146. }
  147. path++;
  148. endp = strchr(path, '/');
  149. if (endp)
  150. *endp = '\0';
  151. /* If we err-ed here, return here */
  152. if ((rc = json_pointer_get_single_path(obj, path, &obj)))
  153. return rc;
  154. if (endp)
  155. {
  156. /* Put the slash back, so that the sanity check passes on next recursion level */
  157. *endp = '/';
  158. return json_pointer_get_recursive(obj, endp, value);
  159. }
  160. /* We should be at the end of the recursion here */
  161. if (value)
  162. *value = obj;
  163. return 0;
  164. }
  165. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
  166. {
  167. char *path_copy = NULL;
  168. int rc;
  169. if (!obj || !path)
  170. {
  171. errno = EINVAL;
  172. return -1;
  173. }
  174. if (path[0] == '\0')
  175. {
  176. if (res)
  177. *res = obj;
  178. return 0;
  179. }
  180. /* pass a working copy to the recursive call */
  181. if (!(path_copy = strdup(path)))
  182. {
  183. errno = ENOMEM;
  184. return -1;
  185. }
  186. rc = json_pointer_get_recursive(obj, path_copy, res);
  187. free(path_copy);
  188. return rc;
  189. }
  190. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
  191. {
  192. char *path_copy = NULL;
  193. int rc = 0;
  194. va_list args;
  195. if (!obj || !path_fmt)
  196. {
  197. errno = EINVAL;
  198. return -1;
  199. }
  200. va_start(args, path_fmt);
  201. rc = vasprintf(&path_copy, path_fmt, args);
  202. va_end(args);
  203. if (rc < 0)
  204. return rc;
  205. if (path_copy[0] == '\0')
  206. {
  207. if (res)
  208. *res = obj;
  209. goto out;
  210. }
  211. rc = json_pointer_get_recursive(obj, path_copy, res);
  212. out:
  213. free(path_copy);
  214. return rc;
  215. }
  216. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
  217. {
  218. const char *endp;
  219. char *path_copy = NULL;
  220. struct json_object *set = NULL;
  221. int rc;
  222. if (!obj || !path)
  223. {
  224. errno = EINVAL;
  225. return -1;
  226. }
  227. if (path[0] == '\0')
  228. {
  229. json_object_put(*obj);
  230. *obj = value;
  231. return 0;
  232. }
  233. if (path[0] != '/')
  234. {
  235. errno = EINVAL;
  236. return -1;
  237. }
  238. /* If there's only 1 level to set, stop here */
  239. if ((endp = strrchr(path, '/')) == path)
  240. {
  241. path++;
  242. return json_pointer_set_single_path(*obj, path, value);
  243. }
  244. /* pass a working copy to the recursive call */
  245. if (!(path_copy = strdup(path)))
  246. {
  247. errno = ENOMEM;
  248. return -1;
  249. }
  250. path_copy[endp - path] = '\0';
  251. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  252. free(path_copy);
  253. if (rc)
  254. return rc;
  255. endp++;
  256. return json_pointer_set_single_path(set, endp, value);
  257. }
  258. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
  259. ...)
  260. {
  261. char *endp;
  262. char *path_copy = NULL;
  263. struct json_object *set = NULL;
  264. va_list args;
  265. int rc = 0;
  266. if (!obj || !path_fmt)
  267. {
  268. errno = EINVAL;
  269. return -1;
  270. }
  271. /* pass a working copy to the recursive call */
  272. va_start(args, path_fmt);
  273. rc = vasprintf(&path_copy, path_fmt, args);
  274. va_end(args);
  275. if (rc < 0)
  276. return rc;
  277. if (path_copy[0] == '\0')
  278. {
  279. json_object_put(*obj);
  280. *obj = value;
  281. goto out;
  282. }
  283. if (path_copy[0] != '/')
  284. {
  285. errno = EINVAL;
  286. rc = -1;
  287. goto out;
  288. }
  289. /* If there's only 1 level to set, stop here */
  290. if ((endp = strrchr(path_copy, '/')) == path_copy)
  291. {
  292. set = *obj;
  293. goto set_single_path;
  294. }
  295. *endp = '\0';
  296. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  297. if (rc)
  298. goto out;
  299. set_single_path:
  300. endp++;
  301. rc = json_pointer_set_single_path(set, endp, value);
  302. out:
  303. free(path_copy);
  304. return rc;
  305. }