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

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