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

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