json_pointer.c 6.6 KB

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