json.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2019 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/dl.h>
  19. #include <grub/mm.h>
  20. #define JSMN_STATIC
  21. #include "jsmn.h"
  22. #include "json.h"
  23. GRUB_MOD_LICENSE ("GPLv3");
  24. grub_err_t
  25. grub_json_parse (grub_json_t **out, char *string, grub_size_t string_len)
  26. {
  27. grub_json_t *json = NULL;
  28. jsmn_parser parser;
  29. grub_err_t ret = GRUB_ERR_NONE;
  30. int jsmn_ret;
  31. if (!string)
  32. return GRUB_ERR_BAD_ARGUMENT;
  33. json = grub_zalloc (sizeof (*json));
  34. if (!json)
  35. return GRUB_ERR_OUT_OF_MEMORY;
  36. json->string = string;
  37. /*
  38. * Parse the string twice: first to determine how many tokens
  39. * we need to allocate, second to fill allocated tokens.
  40. */
  41. jsmn_init (&parser);
  42. jsmn_ret = jsmn_parse (&parser, string, string_len, NULL, 0);
  43. if (jsmn_ret <= 0)
  44. {
  45. ret = GRUB_ERR_BAD_ARGUMENT;
  46. goto err;
  47. }
  48. json->tokens = grub_calloc (jsmn_ret, sizeof (jsmntok_t));
  49. if (!json->tokens)
  50. {
  51. ret = GRUB_ERR_OUT_OF_MEMORY;
  52. goto err;
  53. }
  54. jsmn_init (&parser);
  55. jsmn_ret = jsmn_parse (&parser, string, string_len, json->tokens, jsmn_ret);
  56. if (jsmn_ret <= 0)
  57. {
  58. ret = GRUB_ERR_BAD_ARGUMENT;
  59. goto err;
  60. }
  61. *out = json;
  62. err:
  63. if (ret)
  64. grub_json_free (json);
  65. return ret;
  66. }
  67. void
  68. grub_json_free (grub_json_t *json)
  69. {
  70. if (json)
  71. {
  72. grub_free (json->tokens);
  73. grub_free (json);
  74. }
  75. }
  76. grub_err_t
  77. grub_json_getsize (grub_size_t *out, const grub_json_t *json)
  78. {
  79. int size;
  80. size = json->tokens[json->idx].size;
  81. if (size < 0)
  82. return GRUB_ERR_OUT_OF_RANGE;
  83. *out = (grub_size_t) size;
  84. return GRUB_ERR_NONE;
  85. }
  86. grub_err_t
  87. grub_json_gettype (grub_json_type_t *out, const grub_json_t *json)
  88. {
  89. switch (json->tokens[json->idx].type)
  90. {
  91. case JSMN_OBJECT:
  92. *out = GRUB_JSON_OBJECT;
  93. break;
  94. case JSMN_ARRAY:
  95. *out = GRUB_JSON_ARRAY;
  96. break;
  97. case JSMN_STRING:
  98. *out = GRUB_JSON_STRING;
  99. break;
  100. case JSMN_PRIMITIVE:
  101. *out = GRUB_JSON_PRIMITIVE;
  102. break;
  103. default:
  104. return GRUB_ERR_BAD_ARGUMENT;
  105. }
  106. return GRUB_ERR_NONE;
  107. }
  108. grub_err_t
  109. grub_json_getchild (grub_json_t *out, const grub_json_t *parent, grub_size_t n)
  110. {
  111. grub_size_t offset = 1, size;
  112. jsmntok_t *p;
  113. if (grub_json_getsize (&size, parent) || n >= size)
  114. return GRUB_ERR_OUT_OF_RANGE;
  115. /*
  116. * Skip the first n children. For each of the children, we need
  117. * to skip their own potential children (e.g. if it's an
  118. * array), as well. We thus add the children's size to n on
  119. * each iteration.
  120. */
  121. p = &parent->tokens[parent->idx];
  122. while (n--)
  123. n += p[offset++].size;
  124. out->string = parent->string;
  125. out->tokens = parent->tokens;
  126. out->idx = parent->idx + offset;
  127. return GRUB_ERR_NONE;
  128. }
  129. grub_err_t
  130. grub_json_getvalue (grub_json_t *out, const grub_json_t *parent, const char *key)
  131. {
  132. grub_json_type_t type;
  133. grub_size_t i, size;
  134. if (grub_json_gettype (&type, parent) || type != GRUB_JSON_OBJECT)
  135. return GRUB_ERR_BAD_ARGUMENT;
  136. if (grub_json_getsize (&size, parent))
  137. return GRUB_ERR_BAD_ARGUMENT;
  138. for (i = 0; i < size; i++)
  139. {
  140. grub_json_t child;
  141. const char *s;
  142. if (grub_json_getchild (&child, parent, i) ||
  143. grub_json_getstring (&s, &child, NULL) ||
  144. grub_strcmp (s, key) != 0)
  145. continue;
  146. return grub_json_getchild (out, &child, 0);
  147. }
  148. return GRUB_ERR_FILE_NOT_FOUND;
  149. }
  150. static grub_err_t
  151. get_value (grub_json_type_t *out_type, const char **out_string, const grub_json_t *parent, const char *key)
  152. {
  153. const grub_json_t *p = parent;
  154. grub_json_t child;
  155. grub_err_t ret;
  156. jsmntok_t *tok;
  157. if (key)
  158. {
  159. ret = grub_json_getvalue (&child, parent, key);
  160. if (ret)
  161. return ret;
  162. p = &child;
  163. }
  164. tok = &p->tokens[p->idx];
  165. p->string[tok->end] = '\0';
  166. *out_string = p->string + tok->start;
  167. return grub_json_gettype (out_type, p);
  168. }
  169. grub_err_t
  170. grub_json_getstring (const char **out, const grub_json_t *parent, const char *key)
  171. {
  172. grub_json_type_t type;
  173. const char *value;
  174. grub_err_t ret;
  175. ret = get_value (&type, &value, parent, key);
  176. if (ret)
  177. return ret;
  178. if (type != GRUB_JSON_STRING)
  179. return GRUB_ERR_BAD_ARGUMENT;
  180. *out = value;
  181. return GRUB_ERR_NONE;
  182. }
  183. grub_err_t
  184. grub_json_getuint64 (grub_uint64_t *out, const grub_json_t *parent, const char *key)
  185. {
  186. grub_json_type_t type;
  187. const char *value;
  188. const char *end;
  189. grub_err_t ret;
  190. ret = get_value (&type, &value, parent, key);
  191. if (ret)
  192. return ret;
  193. if (type != GRUB_JSON_STRING && type != GRUB_JSON_PRIMITIVE)
  194. return GRUB_ERR_BAD_ARGUMENT;
  195. grub_errno = GRUB_ERR_NONE;
  196. *out = grub_strtoul (value, &end, 10);
  197. if (grub_errno != GRUB_ERR_NONE || *end)
  198. return GRUB_ERR_BAD_NUMBER;
  199. return GRUB_ERR_NONE;
  200. }
  201. grub_err_t
  202. grub_json_getint64 (grub_int64_t *out, const grub_json_t *parent, const char *key)
  203. {
  204. grub_json_type_t type;
  205. const char *value;
  206. const char *end;
  207. grub_err_t ret;
  208. ret = get_value (&type, &value, parent, key);
  209. if (ret)
  210. return ret;
  211. if (type != GRUB_JSON_STRING && type != GRUB_JSON_PRIMITIVE)
  212. return GRUB_ERR_BAD_ARGUMENT;
  213. grub_errno = GRUB_ERR_NONE;
  214. *out = grub_strtol (value, &end, 10);
  215. if (grub_errno != GRUB_ERR_NONE || *end)
  216. return GRUB_ERR_BAD_NUMBER;
  217. return GRUB_ERR_NONE;
  218. }
  219. grub_err_t
  220. grub_json_unescape (char **out, grub_size_t *outlen, const char *in, grub_size_t inlen)
  221. {
  222. grub_err_t ret = GRUB_ERR_NONE;
  223. grub_size_t inpos, resultpos;
  224. char *result;
  225. if (out == NULL || outlen == NULL)
  226. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("output parameters are not set"));
  227. result = grub_calloc (1, inlen + 1);
  228. if (result == NULL)
  229. return GRUB_ERR_OUT_OF_MEMORY;
  230. for (inpos = resultpos = 0; inpos < inlen; inpos++)
  231. {
  232. if (in[inpos] == '\\')
  233. {
  234. inpos++;
  235. if (inpos >= inlen)
  236. {
  237. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("expected escaped character"));
  238. goto err;
  239. }
  240. switch (in[inpos])
  241. {
  242. case '"':
  243. result[resultpos++] = '"';
  244. break;
  245. case '/':
  246. result[resultpos++] = '/';
  247. break;
  248. case '\\':
  249. result[resultpos++] = '\\';
  250. break;
  251. case 'b':
  252. result[resultpos++] = '\b';
  253. break;
  254. case 'f':
  255. result[resultpos++] = '\f';
  256. break;
  257. case 'r':
  258. result[resultpos++] = '\r';
  259. break;
  260. case 'n':
  261. result[resultpos++] = '\n';
  262. break;
  263. case 't':
  264. result[resultpos++] = '\t';
  265. break;
  266. case 'u':
  267. {
  268. char values[4] = {0};
  269. unsigned i;
  270. inpos++;
  271. if (inpos + ARRAY_SIZE(values) > inlen)
  272. {
  273. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unicode sequence too short"));
  274. goto err;
  275. }
  276. for (i = 0; i < ARRAY_SIZE(values); i++)
  277. {
  278. char c = in[inpos++];
  279. if (c >= '0' && c <= '9')
  280. values[i] = c - '0';
  281. else if (c >= 'A' && c <= 'F')
  282. values[i] = c - 'A' + 10;
  283. else if (c >= 'a' && c <= 'f')
  284. values[i] = c - 'a' + 10;
  285. else
  286. {
  287. ret = grub_error (GRUB_ERR_BAD_ARGUMENT,
  288. N_("unicode sequence with invalid character '%c'"), c);
  289. goto err;
  290. }
  291. }
  292. if (values[0] != 0 || values[1] != 0)
  293. result[resultpos++] = values[0] << 4 | values[1];
  294. result[resultpos++] = values[2] << 4 | values[3];
  295. /* Offset the increment that's coming in via the loop increment. */
  296. inpos--;
  297. break;
  298. }
  299. default:
  300. ret = grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unrecognized escaped character '%c'"), in[inpos]);
  301. goto err;
  302. }
  303. }
  304. else
  305. result[resultpos++] = in[inpos];
  306. }
  307. *out = result;
  308. *outlen = resultpos;
  309. err:
  310. if (ret != GRUB_ERR_NONE)
  311. grub_free (result);
  312. return ret;
  313. }