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

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. int slen = strlen(s);
  26. int 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, int32_t *idx)
  37. {
  38. int 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. *idx = strtol(path, NULL, 10);
  68. if (*idx < 0)
  69. {
  70. errno = EINVAL;
  71. return 0;
  72. }
  73. check_oob:
  74. len = json_object_array_length(jo);
  75. if (*idx >= len)
  76. {
  77. errno = ENOENT;
  78. return 0;
  79. }
  80. return 1;
  81. }
  82. static int json_pointer_get_single_path(struct json_object *obj, char *path,
  83. struct json_object **value)
  84. {
  85. if (json_object_is_type(obj, json_type_array))
  86. {
  87. int32_t idx;
  88. if (!is_valid_index(obj, path, &idx))
  89. return -1;
  90. obj = json_object_array_get_idx(obj, idx);
  91. if (obj)
  92. {
  93. if (value)
  94. *value = obj;
  95. return 0;
  96. }
  97. /* Entry not found */
  98. errno = ENOENT;
  99. return -1;
  100. }
  101. /* RFC states that we first must eval all ~1 then all ~0 */
  102. string_replace_all_occurrences_with_char(path, "~1", '/');
  103. string_replace_all_occurrences_with_char(path, "~0", '~');
  104. if (!json_object_object_get_ex(obj, path, value))
  105. {
  106. errno = ENOENT;
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int json_pointer_set_single_path(struct json_object *parent, const char *path,
  112. struct json_object *value)
  113. {
  114. if (json_object_is_type(parent, json_type_array))
  115. {
  116. int32_t idx;
  117. /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
  118. if (path[0] == '-' && path[1] == '\0')
  119. return json_object_array_add(parent, value);
  120. if (!is_valid_index(parent, path, &idx))
  121. return -1;
  122. return json_object_array_put_idx(parent, idx, value);
  123. }
  124. /* path replacements should have been done in json_pointer_get_single_path(),
  125. * and we should still be good here
  126. */
  127. if (json_object_is_type(parent, json_type_object))
  128. return json_object_object_add(parent, path, value);
  129. /* Getting here means that we tried to "dereference" a primitive JSON type
  130. * (like string, int, bool).i.e. add a sub-object to it
  131. */
  132. errno = ENOENT;
  133. return -1;
  134. }
  135. static int json_pointer_get_recursive(struct json_object *obj, char *path,
  136. struct json_object **value)
  137. {
  138. char *endp;
  139. int rc;
  140. /* All paths (on each recursion level must have a leading '/' */
  141. if (path[0] != '/')
  142. {
  143. errno = EINVAL;
  144. return -1;
  145. }
  146. path++;
  147. endp = strchr(path, '/');
  148. if (endp)
  149. *endp = '\0';
  150. /* If we err-ed here, return here */
  151. if ((rc = json_pointer_get_single_path(obj, path, &obj)))
  152. return rc;
  153. if (endp)
  154. {
  155. /* Put the slash back, so that the sanity check passes on next recursion level */
  156. *endp = '/';
  157. return json_pointer_get_recursive(obj, endp, value);
  158. }
  159. /* We should be at the end of the recursion here */
  160. if (value)
  161. *value = obj;
  162. return 0;
  163. }
  164. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
  165. {
  166. char *path_copy = NULL;
  167. int rc;
  168. if (!obj || !path)
  169. {
  170. errno = EINVAL;
  171. return -1;
  172. }
  173. if (path[0] == '\0')
  174. {
  175. if (res)
  176. *res = obj;
  177. return 0;
  178. }
  179. /* pass a working copy to the recursive call */
  180. if (!(path_copy = strdup(path)))
  181. {
  182. errno = ENOMEM;
  183. return -1;
  184. }
  185. rc = json_pointer_get_recursive(obj, path_copy, res);
  186. free(path_copy);
  187. return rc;
  188. }
  189. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
  190. {
  191. char *path_copy = NULL;
  192. int rc = 0;
  193. va_list args;
  194. if (!obj || !path_fmt)
  195. {
  196. errno = EINVAL;
  197. return -1;
  198. }
  199. va_start(args, path_fmt);
  200. rc = vasprintf(&path_copy, path_fmt, args);
  201. va_end(args);
  202. if (rc < 0)
  203. return rc;
  204. if (path_copy[0] == '\0')
  205. {
  206. if (res)
  207. *res = obj;
  208. goto out;
  209. }
  210. rc = json_pointer_get_recursive(obj, path_copy, res);
  211. out:
  212. free(path_copy);
  213. return rc;
  214. }
  215. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
  216. {
  217. const char *endp;
  218. char *path_copy = NULL;
  219. struct json_object *set = NULL;
  220. int rc;
  221. if (!obj || !path)
  222. {
  223. errno = EINVAL;
  224. return -1;
  225. }
  226. if (path[0] == '\0')
  227. {
  228. json_object_put(*obj);
  229. *obj = value;
  230. return 0;
  231. }
  232. if (path[0] != '/')
  233. {
  234. errno = EINVAL;
  235. return -1;
  236. }
  237. /* If there's only 1 level to set, stop here */
  238. if ((endp = strrchr(path, '/')) == path)
  239. {
  240. path++;
  241. return json_pointer_set_single_path(*obj, path, value);
  242. }
  243. /* pass a working copy to the recursive call */
  244. if (!(path_copy = strdup(path)))
  245. {
  246. errno = ENOMEM;
  247. return -1;
  248. }
  249. path_copy[endp - path] = '\0';
  250. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  251. free(path_copy);
  252. if (rc)
  253. return rc;
  254. endp++;
  255. return json_pointer_set_single_path(set, endp, value);
  256. }
  257. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
  258. ...)
  259. {
  260. char *endp;
  261. char *path_copy = NULL;
  262. struct json_object *set = NULL;
  263. va_list args;
  264. int rc = 0;
  265. if (!obj || !path_fmt)
  266. {
  267. errno = EINVAL;
  268. return -1;
  269. }
  270. /* pass a working copy to the recursive call */
  271. va_start(args, path_fmt);
  272. rc = vasprintf(&path_copy, path_fmt, args);
  273. va_end(args);
  274. if (rc < 0)
  275. return rc;
  276. if (path_copy[0] == '\0')
  277. {
  278. json_object_put(*obj);
  279. *obj = value;
  280. goto out;
  281. }
  282. if (path_copy[0] != '/')
  283. {
  284. errno = EINVAL;
  285. rc = -1;
  286. goto out;
  287. }
  288. /* If there's only 1 level to set, stop here */
  289. if ((endp = strrchr(path_copy, '/')) == path_copy)
  290. {
  291. set = *obj;
  292. goto set_single_path;
  293. }
  294. *endp = '\0';
  295. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  296. if (rc)
  297. goto out;
  298. set_single_path:
  299. endp++;
  300. rc = json_pointer_set_single_path(set, endp, value);
  301. out:
  302. free(path_copy);
  303. return rc;
  304. }