blobmsg_json.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <inttypes.h>
  17. #include "blobmsg.h"
  18. #include "blobmsg_json.h"
  19. #ifdef JSONC
  20. #include <json.h>
  21. #else
  22. #include <json/json.h>
  23. #endif
  24. bool blobmsg_add_object(struct blob_buf *b, json_object *obj)
  25. {
  26. json_object_object_foreach(obj, key, val) {
  27. if (!blobmsg_add_json_element(b, key, val))
  28. return false;
  29. }
  30. return true;
  31. }
  32. static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
  33. {
  34. int i, len;
  35. for (i = 0, len = array_list_length(a); i < len; i++) {
  36. if (!blobmsg_add_json_element(b, NULL, array_list_get_idx(a, i)))
  37. return false;
  38. }
  39. return true;
  40. }
  41. bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
  42. {
  43. bool ret = true;
  44. void *c;
  45. switch (json_object_get_type(obj)) {
  46. case json_type_object:
  47. c = blobmsg_open_table(b, name);
  48. ret = blobmsg_add_object(b, obj);
  49. blobmsg_close_table(b, c);
  50. break;
  51. case json_type_array:
  52. c = blobmsg_open_array(b, name);
  53. ret = blobmsg_add_array(b, json_object_get_array(obj));
  54. blobmsg_close_array(b, c);
  55. break;
  56. case json_type_string:
  57. blobmsg_add_string(b, name, json_object_get_string(obj));
  58. break;
  59. case json_type_boolean:
  60. blobmsg_add_u8(b, name, json_object_get_boolean(obj));
  61. break;
  62. case json_type_int: {
  63. int64_t i64 = json_object_get_int64(obj);
  64. if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
  65. blobmsg_add_u32(b, name, (uint32_t)i64);
  66. } else {
  67. blobmsg_add_u64(b, name, (uint64_t)i64);
  68. }
  69. break;
  70. }
  71. case json_type_double:
  72. blobmsg_add_double(b, name, json_object_get_double(obj));
  73. break;
  74. case json_type_null:
  75. blobmsg_add_field(b, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
  76. break;
  77. default:
  78. return false;
  79. }
  80. return ret;
  81. }
  82. static bool __blobmsg_add_json(struct blob_buf *b, json_object *obj)
  83. {
  84. bool ret = false;
  85. if (!obj)
  86. return false;
  87. if (json_object_get_type(obj) != json_type_object)
  88. goto out;
  89. ret = blobmsg_add_object(b, obj);
  90. out:
  91. json_object_put(obj);
  92. return ret;
  93. }
  94. bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
  95. {
  96. return __blobmsg_add_json(b, json_object_from_file(file));
  97. }
  98. bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)
  99. {
  100. return __blobmsg_add_json(b, json_tokener_parse(str));
  101. }
  102. struct strbuf {
  103. int len;
  104. int pos;
  105. char *buf;
  106. blobmsg_json_format_t custom_format;
  107. void *priv;
  108. bool indent;
  109. int indent_level;
  110. };
  111. static bool blobmsg_puts(struct strbuf *s, const char *c, int len)
  112. {
  113. size_t new_len;
  114. char *new_buf;
  115. if (len <= 0)
  116. return true;
  117. if (s->pos + len >= s->len) {
  118. new_len = s->len + 16 + len;
  119. new_buf = realloc(s->buf, new_len);
  120. if (!new_buf)
  121. return false;
  122. s->len = new_len;
  123. s->buf = new_buf;
  124. }
  125. memcpy(s->buf + s->pos, c, len);
  126. s->pos += len;
  127. return true;
  128. }
  129. static void add_separator(struct strbuf *s)
  130. {
  131. const char indent_chars[] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  132. size_t len;
  133. if (!s->indent)
  134. return;
  135. len = s->indent_level + 1;
  136. if (len > sizeof(indent_chars) - 1)
  137. len = sizeof(indent_chars) - 1;
  138. blobmsg_puts(s, indent_chars, len);
  139. }
  140. static void blobmsg_format_string(struct strbuf *s, const char *str)
  141. {
  142. const unsigned char *p, *last, *end;
  143. char buf[8] = "\\u00";
  144. end = (unsigned char *) str + strlen(str);
  145. blobmsg_puts(s, "\"", 1);
  146. for (p = (unsigned char *) str, last = p; *p; p++) {
  147. char escape = '\0';
  148. int len;
  149. switch(*p) {
  150. case '\b':
  151. escape = 'b';
  152. break;
  153. case '\n':
  154. escape = 'n';
  155. break;
  156. case '\t':
  157. escape = 't';
  158. break;
  159. case '\r':
  160. escape = 'r';
  161. break;
  162. case '"':
  163. case '\\':
  164. escape = *p;
  165. break;
  166. default:
  167. if (*p < ' ')
  168. escape = 'u';
  169. break;
  170. }
  171. if (!escape)
  172. continue;
  173. if (p > last)
  174. blobmsg_puts(s, (char *) last, p - last);
  175. last = p + 1;
  176. buf[1] = escape;
  177. if (escape == 'u') {
  178. snprintf(buf + 4, sizeof(buf) - 4, "%02x", (unsigned char) *p);
  179. len = 6;
  180. } else {
  181. len = 2;
  182. }
  183. blobmsg_puts(s, buf, len);
  184. }
  185. blobmsg_puts(s, (char *) last, end - last);
  186. blobmsg_puts(s, "\"", 1);
  187. }
  188. static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array);
  189. static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool without_name, bool head)
  190. {
  191. const char *data_str;
  192. char buf[317];
  193. void *data;
  194. int len;
  195. if (!blobmsg_check_attr(attr, false))
  196. return;
  197. if (!without_name && blobmsg_name(attr)[0]) {
  198. blobmsg_format_string(s, blobmsg_name(attr));
  199. blobmsg_puts(s, ": ", s->indent ? 2 : 1);
  200. }
  201. data = blobmsg_data(attr);
  202. len = blobmsg_data_len(attr);
  203. if (!head && s->custom_format) {
  204. data_str = s->custom_format(s->priv, attr);
  205. if (data_str)
  206. goto out;
  207. }
  208. data_str = buf;
  209. switch(blob_id(attr)) {
  210. case BLOBMSG_TYPE_UNSPEC:
  211. snprintf(buf, sizeof(buf), "null");
  212. break;
  213. case BLOBMSG_TYPE_BOOL:
  214. snprintf(buf, sizeof(buf), "%s", *(uint8_t *)data ? "true" : "false");
  215. break;
  216. case BLOBMSG_TYPE_INT16:
  217. snprintf(buf, sizeof(buf), "%" PRId16, (int16_t) be16_to_cpu(*(uint16_t *)data));
  218. break;
  219. case BLOBMSG_TYPE_INT32:
  220. snprintf(buf, sizeof(buf), "%" PRId32, (int32_t) be32_to_cpu(*(uint32_t *)data));
  221. break;
  222. case BLOBMSG_TYPE_INT64:
  223. snprintf(buf, sizeof(buf), "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data));
  224. break;
  225. case BLOBMSG_TYPE_DOUBLE:
  226. snprintf(buf, sizeof(buf), "%lf", blobmsg_get_double(attr));
  227. break;
  228. case BLOBMSG_TYPE_STRING:
  229. blobmsg_format_string(s, data);
  230. return;
  231. case BLOBMSG_TYPE_ARRAY:
  232. blobmsg_format_json_list(s, data, len, true);
  233. return;
  234. case BLOBMSG_TYPE_TABLE:
  235. blobmsg_format_json_list(s, data, len, false);
  236. return;
  237. }
  238. out:
  239. blobmsg_puts(s, data_str, strlen(data_str));
  240. }
  241. static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, int len, bool array)
  242. {
  243. struct blob_attr *pos;
  244. bool first = true;
  245. size_t rem = len;
  246. blobmsg_puts(s, (array ? "[" : "{" ), 1);
  247. s->indent_level++;
  248. add_separator(s);
  249. __blob_for_each_attr(pos, attr, rem) {
  250. if (!first) {
  251. blobmsg_puts(s, ",", 1);
  252. add_separator(s);
  253. }
  254. blobmsg_format_element(s, pos, array, false);
  255. first = false;
  256. }
  257. s->indent_level--;
  258. add_separator(s);
  259. blobmsg_puts(s, (array ? "]" : "}"), 1);
  260. }
  261. static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
  262. {
  263. s->len = blob_len(attr);
  264. s->buf = malloc(s->len);
  265. s->pos = 0;
  266. s->custom_format = cb;
  267. s->priv = priv;
  268. s->indent = false;
  269. if (indent >= 0) {
  270. s->indent = true;
  271. s->indent_level = indent;
  272. }
  273. }
  274. char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
  275. {
  276. struct strbuf s = {0};
  277. bool array;
  278. char *ret;
  279. setup_strbuf(&s, attr, cb, priv, indent);
  280. if (!s.buf)
  281. return NULL;
  282. array = blob_is_extended(attr) &&
  283. blobmsg_type(attr) == BLOBMSG_TYPE_ARRAY;
  284. if (list)
  285. blobmsg_format_json_list(&s, blobmsg_data(attr), blobmsg_data_len(attr), array);
  286. else
  287. blobmsg_format_element(&s, attr, false, false);
  288. if (!s.len) {
  289. free(s.buf);
  290. return NULL;
  291. }
  292. ret = realloc(s.buf, s.pos + 1);
  293. if (!ret) {
  294. free(s.buf);
  295. return NULL;
  296. }
  297. ret[s.pos] = 0;
  298. return ret;
  299. }
  300. char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
  301. {
  302. struct strbuf s = {0};
  303. char *ret;
  304. setup_strbuf(&s, attr, cb, priv, indent);
  305. if (!s.buf)
  306. return NULL;
  307. blobmsg_format_element(&s, attr, true, false);
  308. if (!s.len) {
  309. free(s.buf);
  310. return NULL;
  311. }
  312. ret = realloc(s.buf, s.pos + 1);
  313. if (!ret) {
  314. free(s.buf);
  315. return NULL;
  316. }
  317. ret[s.pos] = 0;
  318. return ret;
  319. }