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

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