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

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <ctype.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "json_pointer.h"
  16. #include "strdup_compat.h"
  17. #include "vasprintf_compat.h"
  18. /**
  19. * JavaScript Object Notation (JSON) Pointer
  20. * RFC 6901 - https://tools.ietf.org/html/rfc6901
  21. */
  22. static void string_replace_all_occurrences_with_char(char *s, const char *occur, char repl_char)
  23. {
  24. int slen = strlen(s);
  25. int skip = strlen(occur) - 1; /* length of the occurrence, minus the char we're replacing */
  26. char *p = s;
  27. while ((p = strstr(p, occur)))
  28. {
  29. *p = repl_char;
  30. p++;
  31. slen -= skip;
  32. memmove(p, (p + skip), slen - (p - s) + 1); /* includes null char too */
  33. }
  34. }
  35. static int is_valid_index(struct json_object *jo, const char *path, int32_t *idx)
  36. {
  37. int i, len = strlen(path);
  38. /* this code-path optimizes a bit, for when we reference the 0-9 index range
  39. * in a JSON array and because leading zeros not allowed
  40. */
  41. if (len == 1)
  42. {
  43. if (isdigit((unsigned char)path[0]))
  44. {
  45. *idx = (path[0] - '0');
  46. goto check_oob;
  47. }
  48. errno = EINVAL;
  49. return 0;
  50. }
  51. /* leading zeros not allowed per RFC */
  52. if (path[0] == '0')
  53. {
  54. errno = EINVAL;
  55. return 0;
  56. }
  57. /* RFC states base-10 decimals */
  58. for (i = 0; i < len; i++)
  59. {
  60. if (!isdigit((unsigned char)path[i]))
  61. {
  62. errno = EINVAL;
  63. return 0;
  64. }
  65. }
  66. *idx = strtol(path, NULL, 10);
  67. if (*idx < 0)
  68. {
  69. errno = EINVAL;
  70. return 0;
  71. }
  72. check_oob:
  73. len = json_object_array_length(jo);
  74. if (*idx >= len)
  75. {
  76. errno = ENOENT;
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. static int json_pointer_get_single_path(struct json_object *obj, char *path,
  82. struct json_object **value)
  83. {
  84. if (json_object_is_type(obj, json_type_array))
  85. {
  86. int32_t idx;
  87. if (!is_valid_index(obj, path, &idx))
  88. return -1;
  89. obj = json_object_array_get_idx(obj, idx);
  90. if (obj)
  91. {
  92. if (value)
  93. *value = obj;
  94. return 0;
  95. }
  96. /* Entry not found */
  97. errno = ENOENT;
  98. return -1;
  99. }
  100. /* RFC states that we first must eval all ~1 then all ~0 */
  101. string_replace_all_occurrences_with_char(path, "~1", '/');
  102. string_replace_all_occurrences_with_char(path, "~0", '~');
  103. if (!json_object_object_get_ex(obj, path, value))
  104. {
  105. errno = ENOENT;
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. static int json_pointer_set_single_path(struct json_object *parent, const char *path,
  111. struct json_object *value)
  112. {
  113. if (json_object_is_type(parent, json_type_array))
  114. {
  115. int32_t idx;
  116. /* RFC (Chapter 4) states that '-' may be used to add new elements to an array */
  117. if (path[0] == '-' && path[1] == '\0')
  118. return json_object_array_add(parent, value);
  119. if (!is_valid_index(parent, path, &idx))
  120. return -1;
  121. return json_object_array_put_idx(parent, idx, value);
  122. }
  123. /* path replacements should have been done in json_pointer_get_single_path(),
  124. * and we should still be good here
  125. */
  126. if (json_object_is_type(parent, json_type_object))
  127. return json_object_object_add(parent, path, value);
  128. /* Getting here means that we tried to "dereference" a primitive JSON type
  129. * (like string, int, bool).i.e. add a sub-object to it
  130. */
  131. errno = ENOENT;
  132. return -1;
  133. }
  134. static int json_pointer_get_recursive(struct json_object *obj, char *path,
  135. struct json_object **value)
  136. {
  137. char *endp;
  138. int rc;
  139. /* All paths (on each recursion level must have a leading '/' */
  140. if (path[0] != '/')
  141. {
  142. errno = EINVAL;
  143. return -1;
  144. }
  145. path++;
  146. endp = strchr(path, '/');
  147. if (endp)
  148. *endp = '\0';
  149. /* If we err-ed here, return here */
  150. if ((rc = json_pointer_get_single_path(obj, path, &obj)))
  151. return rc;
  152. if (endp)
  153. {
  154. /* Put the slash back, so that the sanity check passes on next recursion level */
  155. *endp = '/';
  156. return json_pointer_get_recursive(obj, endp, value);
  157. }
  158. /* We should be at the end of the recursion here */
  159. if (value)
  160. *value = obj;
  161. return 0;
  162. }
  163. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res)
  164. {
  165. char *path_copy = NULL;
  166. int rc;
  167. if (!obj || !path)
  168. {
  169. errno = EINVAL;
  170. return -1;
  171. }
  172. if (path[0] == '\0')
  173. {
  174. if (res)
  175. *res = obj;
  176. return 0;
  177. }
  178. /* pass a working copy to the recursive call */
  179. if (!(path_copy = strdup(path)))
  180. {
  181. errno = ENOMEM;
  182. return -1;
  183. }
  184. rc = json_pointer_get_recursive(obj, path_copy, res);
  185. free(path_copy);
  186. return rc;
  187. }
  188. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...)
  189. {
  190. char *path_copy = NULL;
  191. int rc = 0;
  192. va_list args;
  193. if (!obj || !path_fmt)
  194. {
  195. errno = EINVAL;
  196. return -1;
  197. }
  198. va_start(args, path_fmt);
  199. rc = vasprintf(&path_copy, path_fmt, args);
  200. va_end(args);
  201. if (rc < 0)
  202. return rc;
  203. if (path_copy[0] == '\0')
  204. {
  205. if (res)
  206. *res = obj;
  207. goto out;
  208. }
  209. rc = json_pointer_get_recursive(obj, path_copy, res);
  210. out:
  211. free(path_copy);
  212. return rc;
  213. }
  214. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value)
  215. {
  216. const char *endp;
  217. char *path_copy = NULL;
  218. struct json_object *set = NULL;
  219. int rc;
  220. if (!obj || !path)
  221. {
  222. errno = EINVAL;
  223. return -1;
  224. }
  225. if (path[0] == '\0')
  226. {
  227. json_object_put(*obj);
  228. *obj = value;
  229. return 0;
  230. }
  231. if (path[0] != '/')
  232. {
  233. errno = EINVAL;
  234. return -1;
  235. }
  236. /* If there's only 1 level to set, stop here */
  237. if ((endp = strrchr(path, '/')) == path)
  238. {
  239. path++;
  240. return json_pointer_set_single_path(*obj, path, value);
  241. }
  242. /* pass a working copy to the recursive call */
  243. if (!(path_copy = strdup(path)))
  244. {
  245. errno = ENOMEM;
  246. return -1;
  247. }
  248. path_copy[endp - path] = '\0';
  249. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  250. free(path_copy);
  251. if (rc)
  252. return rc;
  253. endp++;
  254. return json_pointer_set_single_path(set, endp, value);
  255. }
  256. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt,
  257. ...)
  258. {
  259. char *endp;
  260. char *path_copy = NULL;
  261. struct json_object *set = NULL;
  262. va_list args;
  263. int rc = 0;
  264. if (!obj || !path_fmt)
  265. {
  266. errno = EINVAL;
  267. return -1;
  268. }
  269. /* pass a working copy to the recursive call */
  270. va_start(args, path_fmt);
  271. rc = vasprintf(&path_copy, path_fmt, args);
  272. va_end(args);
  273. if (rc < 0)
  274. return rc;
  275. if (path_copy[0] == '\0')
  276. {
  277. json_object_put(*obj);
  278. *obj = value;
  279. goto out;
  280. }
  281. if (path_copy[0] != '/')
  282. {
  283. errno = EINVAL;
  284. rc = -1;
  285. goto out;
  286. }
  287. /* If there's only 1 level to set, stop here */
  288. if ((endp = strrchr(path_copy, '/')) == path_copy)
  289. {
  290. set = *obj;
  291. goto set_single_path;
  292. }
  293. *endp = '\0';
  294. rc = json_pointer_get_recursive(*obj, path_copy, &set);
  295. if (rc)
  296. goto out;
  297. set_single_path:
  298. endp++;
  299. rc = json_pointer_set_single_path(set, endp, value);
  300. out:
  301. free(path_copy);
  302. return rc;
  303. }