jsmn.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2010 Serge Zaitsev
  5. *
  6. * [License text continues...]
  7. */
  8. #include <jsmn/jsmn.h>
  9. /**
  10. * Allocates a fresh unused token from the token pool.
  11. */
  12. static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens,
  13. const size_t num_tokens)
  14. {
  15. jsmntok_t *tok;
  16. if (parser->toknext >= num_tokens)
  17. {
  18. return NULL;
  19. }
  20. tok = &tokens[parser->toknext++];
  21. tok->start = tok->end = -1;
  22. tok->size = 0;
  23. #ifdef JSMN_PARENT_LINKS
  24. tok->parent = -1;
  25. #endif
  26. return tok;
  27. }
  28. /**
  29. * Fills token type and boundaries.
  30. */
  31. static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type,
  32. const int start, const int end)
  33. {
  34. token->type = type;
  35. token->start = start;
  36. token->end = end;
  37. token->size = 0;
  38. }
  39. /**
  40. * Fills next available token with JSON primitive.
  41. */
  42. static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
  43. const size_t len, jsmntok_t *tokens,
  44. const size_t num_tokens)
  45. {
  46. jsmntok_t *token;
  47. int start;
  48. start = parser->pos;
  49. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  50. {
  51. switch (js[parser->pos])
  52. {
  53. #ifndef JSMN_STRICT
  54. /* In strict mode primitive must be followed by "," or "}" or "]" */
  55. case ':':
  56. #endif
  57. case '\t':
  58. case '\r':
  59. case '\n':
  60. case ' ':
  61. case ',':
  62. case ']':
  63. case '}':
  64. goto found;
  65. default:
  66. /* to quiet a warning from gcc*/
  67. break;
  68. }
  69. if (js[parser->pos] < 32 || js[parser->pos] >= 127)
  70. {
  71. parser->pos = start;
  72. return JSMN_ERROR_INVAL;
  73. }
  74. }
  75. #ifdef JSMN_STRICT
  76. /* In strict mode primitive must be followed by a comma/object/array */
  77. parser->pos = start;
  78. return JSMN_ERROR_PART;
  79. #endif
  80. found:
  81. if (tokens == NULL)
  82. {
  83. parser->pos--;
  84. return 0;
  85. }
  86. token = jsmn_alloc_token(parser, tokens, num_tokens);
  87. if (token == NULL)
  88. {
  89. parser->pos = start;
  90. return JSMN_ERROR_NOMEM;
  91. }
  92. jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
  93. #ifdef JSMN_PARENT_LINKS
  94. token->parent = parser->toksuper;
  95. #endif
  96. parser->pos--;
  97. return 0;
  98. }
  99. /**
  100. * Fills next token with JSON string.
  101. */
  102. static int jsmn_parse_string(jsmn_parser *parser, const char *js,
  103. const size_t len, jsmntok_t *tokens,
  104. const size_t num_tokens)
  105. {
  106. jsmntok_t *token;
  107. int start = parser->pos;
  108. /* Skip starting quote */
  109. parser->pos++;
  110. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  111. {
  112. char c = js[parser->pos];
  113. /* Quote: end of string */
  114. if (c == '\"')
  115. {
  116. if (tokens == NULL)
  117. {
  118. return 0;
  119. }
  120. token = jsmn_alloc_token(parser, tokens, num_tokens);
  121. if (token == NULL)
  122. {
  123. parser->pos = start;
  124. return JSMN_ERROR_NOMEM;
  125. }
  126. jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
  127. #ifdef JSMN_PARENT_LINKS
  128. token->parent = parser->toksuper;
  129. #endif
  130. return 0;
  131. }
  132. /* Backslash: Quoted symbol expected */
  133. if (c == '\\' && parser->pos + 1 < len)
  134. {
  135. int i;
  136. parser->pos++;
  137. switch (js[parser->pos])
  138. {
  139. /* Allowed escaped symbols */
  140. case '\"':
  141. case '/':
  142. case '\\':
  143. case 'b':
  144. case 'f':
  145. case 'r':
  146. case 'n':
  147. case 't':
  148. break;
  149. /* Allows escaped symbol \uXXXX */
  150. case 'u':
  151. parser->pos++;
  152. for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++)
  153. {
  154. /* If it isn't a hex character we have an error */
  155. if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
  156. (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
  157. (js[parser->pos] >= 97 && js[parser->pos] <= 102)))
  158. { /* a-f */
  159. parser->pos = start;
  160. return JSMN_ERROR_INVAL;
  161. }
  162. parser->pos++;
  163. }
  164. parser->pos--;
  165. break;
  166. /* Unexpected symbol */
  167. default:
  168. parser->pos = start;
  169. return JSMN_ERROR_INVAL;
  170. }
  171. }
  172. }
  173. parser->pos = start;
  174. return JSMN_ERROR_PART;
  175. }
  176. /**
  177. * Create JSON parser over an array of tokens
  178. */
  179. void jsmn_init(jsmn_parser *parser)
  180. {
  181. parser->pos = 0;
  182. parser->toknext = 0;
  183. parser->toksuper = -1;
  184. }
  185. /**
  186. * Parse JSON string and fill tokens.
  187. */
  188. int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
  189. jsmntok_t *tokens, const unsigned int num_tokens)
  190. {
  191. int r;
  192. int i;
  193. jsmntok_t *token;
  194. int count = parser->toknext;
  195. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  196. {
  197. char c;
  198. jsmntype_t type;
  199. c = js[parser->pos];
  200. switch (c)
  201. {
  202. case '{':
  203. case '[':
  204. count++;
  205. if (tokens == NULL)
  206. {
  207. break;
  208. }
  209. token = jsmn_alloc_token(parser, tokens, num_tokens);
  210. if (token == NULL)
  211. {
  212. return JSMN_ERROR_NOMEM;
  213. }
  214. if (parser->toksuper != -1)
  215. {
  216. jsmntok_t *t = &tokens[parser->toksuper];
  217. #ifdef JSMN_STRICT
  218. /* In strict mode an object or array can't become a key */
  219. if (t->type == JSMN_OBJECT)
  220. {
  221. return JSMN_ERROR_INVAL;
  222. }
  223. #endif
  224. t->size++;
  225. #ifdef JSMN_PARENT_LINKS
  226. token->parent = parser->toksuper;
  227. #endif
  228. }
  229. token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
  230. token->start = parser->pos;
  231. parser->toksuper = parser->toknext - 1;
  232. break;
  233. case '}':
  234. case ']':
  235. if (tokens == NULL)
  236. {
  237. break;
  238. }
  239. type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
  240. #ifdef JSMN_PARENT_LINKS
  241. if (parser->toknext < 1)
  242. {
  243. return JSMN_ERROR_INVAL;
  244. }
  245. token = &tokens[parser->toknext - 1];
  246. for (;;)
  247. {
  248. if (token->start != -1 && token->end == -1)
  249. {
  250. if (token->type != type)
  251. {
  252. return JSMN_ERROR_INVAL;
  253. }
  254. token->end = parser->pos + 1;
  255. parser->toksuper = token->parent;
  256. break;
  257. }
  258. if (token->parent == -1)
  259. {
  260. if (token->type != type || parser->toksuper == -1)
  261. {
  262. return JSMN_ERROR_INVAL;
  263. }
  264. break;
  265. }
  266. token = &tokens[token->parent];
  267. }
  268. #else
  269. for (i = parser->toknext - 1; i >= 0; i--)
  270. {
  271. token = &tokens[i];
  272. if (token->start != -1 && token->end == -1)
  273. {
  274. if (token->type != type)
  275. {
  276. return JSMN_ERROR_INVAL;
  277. }
  278. parser->toksuper = -1;
  279. token->end = parser->pos + 1;
  280. break;
  281. }
  282. }
  283. /* Error if unmatched closing bracket */
  284. if (i == -1)
  285. {
  286. return JSMN_ERROR_INVAL;
  287. }
  288. for (; i >= 0; i--)
  289. {
  290. token = &tokens[i];
  291. if (token->start != -1 && token->end == -1)
  292. {
  293. parser->toksuper = i;
  294. break;
  295. }
  296. }
  297. #endif
  298. break;
  299. case '\"':
  300. r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
  301. if (r < 0)
  302. {
  303. return r;
  304. }
  305. count++;
  306. if (parser->toksuper != -1 && tokens != NULL)
  307. {
  308. tokens[parser->toksuper].size++;
  309. }
  310. break;
  311. case '\t':
  312. case '\r':
  313. case '\n':
  314. case ' ':
  315. break;
  316. case ':':
  317. parser->toksuper = parser->toknext - 1;
  318. break;
  319. case ',':
  320. if (tokens != NULL && parser->toksuper != -1 &&
  321. tokens[parser->toksuper].type != JSMN_ARRAY &&
  322. tokens[parser->toksuper].type != JSMN_OBJECT)
  323. {
  324. #ifdef JSMN_PARENT_LINKS
  325. parser->toksuper = tokens[parser->toksuper].parent;
  326. #else
  327. for (i = parser->toknext - 1; i >= 0; i--)
  328. {
  329. if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT)
  330. {
  331. if (tokens[i].start != -1 && tokens[i].end == -1)
  332. {
  333. parser->toksuper = i;
  334. break;
  335. }
  336. }
  337. }
  338. #endif
  339. }
  340. break;
  341. #ifdef JSMN_STRICT
  342. /* In strict mode primitives are: numbers and booleans */
  343. case '-':
  344. case '0':
  345. case '1':
  346. case '2':
  347. case '3':
  348. case '4':
  349. case '5':
  350. case '6':
  351. case '7':
  352. case '8':
  353. case '9':
  354. case 't':
  355. case 'f':
  356. case 'n':
  357. /* And they must not be keys of the object */
  358. if (tokens != NULL && parser->toksuper != -1)
  359. {
  360. const jsmntok_t *t = &tokens[parser->toksuper];
  361. if (t->type == JSMN_OBJECT ||
  362. (t->type == JSMN_STRING && t->size != 0))
  363. {
  364. return JSMN_ERROR_INVAL;
  365. }
  366. }
  367. #else
  368. /* In non-strict mode every unquoted value is a primitive */
  369. default:
  370. #endif
  371. r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
  372. if (r < 0)
  373. {
  374. return r;
  375. }
  376. count++;
  377. if (parser->toksuper != -1 && tokens != NULL)
  378. {
  379. tokens[parser->toksuper].size++;
  380. }
  381. break;
  382. #ifdef JSMN_STRICT
  383. /* Unexpected char in strict mode */
  384. default:
  385. return JSMN_ERROR_INVAL;
  386. #endif
  387. }
  388. }
  389. if (tokens != NULL)
  390. {
  391. for (i = parser->toknext - 1; i >= 0; i--)
  392. {
  393. /* Unmatched opened object or array */
  394. if (tokens[i].start != -1 && tokens[i].end == -1)
  395. {
  396. return JSMN_ERROR_PART;
  397. }
  398. }
  399. }
  400. return count;
  401. }
  402. // Helper function to create a JSON object
  403. char *get_json(const char *key, const char *value)
  404. {
  405. int length = strlen(key) + strlen(value) + 8; // Calculate required length
  406. char *result = (char *)malloc(length * sizeof(char)); // Allocate memory
  407. if (result == NULL)
  408. {
  409. return NULL; // Handle memory allocation failure
  410. }
  411. snprintf(result, length, "{\"%s\":\"%s\"}", key, value);
  412. return result; // Caller is responsible for freeing this memory
  413. }
  414. // Helper function to compare JSON keys
  415. int jsoneq(const char *json, jsmntok_t *tok, const char *s)
  416. {
  417. if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
  418. strncmp(json + tok->start, s, tok->end - tok->start) == 0)
  419. {
  420. return 0;
  421. }
  422. return -1;
  423. }
  424. // Return the value of the key in the JSON data
  425. char *get_json_value(char *key, const char *json_data)
  426. {
  427. // Parse the JSON feed
  428. if (json_data != NULL)
  429. {
  430. jsmn_parser parser;
  431. jsmn_init(&parser);
  432. uint32_t max_tokens = json_token_count(json_data);
  433. if (!jsmn_memory_check(max_tokens))
  434. {
  435. FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens.");
  436. return NULL;
  437. }
  438. // Allocate tokens array on the heap
  439. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  440. if (tokens == NULL)
  441. {
  442. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  443. return NULL;
  444. }
  445. int ret = jsmn_parse(&parser, json_data, strlen(json_data), tokens, max_tokens);
  446. if (ret < 0)
  447. {
  448. // Handle parsing errors
  449. FURI_LOG_E("JSMM.H", "Failed to parse JSON: %d", ret);
  450. free(tokens);
  451. return NULL;
  452. }
  453. // Ensure that the root element is an object
  454. if (ret < 1 || tokens[0].type != JSMN_OBJECT)
  455. {
  456. FURI_LOG_E("JSMM.H", "Root element is not an object.");
  457. free(tokens);
  458. return NULL;
  459. }
  460. // Loop through the tokens to find the key
  461. for (int i = 1; i < ret; i++)
  462. {
  463. if (jsoneq(json_data, &tokens[i], key) == 0)
  464. {
  465. // We found the key. Now, return the associated value.
  466. int length = tokens[i + 1].end - tokens[i + 1].start;
  467. char *value = malloc(length + 1);
  468. if (value == NULL)
  469. {
  470. FURI_LOG_E("JSMM.H", "Failed to allocate memory for value.");
  471. free(tokens);
  472. return NULL;
  473. }
  474. strncpy(value, json_data + tokens[i + 1].start, length);
  475. value[length] = '\0'; // Null-terminate the string
  476. free(tokens); // Free the token array
  477. return value; // Return the extracted value
  478. }
  479. }
  480. // Free the token array if key was not found
  481. free(tokens);
  482. }
  483. else
  484. {
  485. FURI_LOG_E("JSMM.H", "JSON data is NULL");
  486. }
  487. char warning[128];
  488. snprintf(warning, sizeof(warning), "Failed to find the key \"%s\" in the JSON.", key);
  489. FURI_LOG_E("JSMM.H", warning);
  490. return NULL; // Return NULL if something goes wrong
  491. }
  492. // Helper function to skip a token and all its descendants.
  493. // Returns the index of the next token after skipping this one.
  494. // On error or out of bounds, returns -1.
  495. static int skip_token(const jsmntok_t *tokens, int start, int total)
  496. {
  497. if (start < 0 || start >= total)
  498. return -1;
  499. int i = start;
  500. if (tokens[i].type == JSMN_OBJECT)
  501. {
  502. // For an object: size is number of key-value pairs
  503. int pairs = tokens[i].size;
  504. i++; // move to first key-value pair
  505. for (int p = 0; p < pairs; p++)
  506. {
  507. // skip key (primitive/string)
  508. i++;
  509. if (i >= total)
  510. return -1;
  511. // skip value (which could be object/array and must be skipped recursively)
  512. i = skip_token(tokens, i, total);
  513. if (i == -1)
  514. return -1;
  515. }
  516. return i; // i is now just past the object
  517. }
  518. else if (tokens[i].type == JSMN_ARRAY)
  519. {
  520. // For an array: size is number of elements
  521. int elems = tokens[i].size;
  522. i++; // move to first element
  523. for (int e = 0; e < elems; e++)
  524. {
  525. i = skip_token(tokens, i, total);
  526. if (i == -1)
  527. return -1;
  528. }
  529. return i; // i is now just past the array
  530. }
  531. else
  532. {
  533. // Primitive or string token, just skip it
  534. return i + 1;
  535. }
  536. }
  537. // Revised get_json_array_value
  538. char *get_json_array_value(char *key, uint32_t index, const char *json_data)
  539. {
  540. // Always extract the full array each time from the original json_data
  541. char *array_str = get_json_value(key, json_data);
  542. if (array_str == NULL)
  543. {
  544. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  545. return NULL;
  546. }
  547. uint32_t max_tokens = json_token_count(array_str);
  548. if (!jsmn_memory_check(max_tokens))
  549. {
  550. FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens.");
  551. free(array_str);
  552. return NULL;
  553. }
  554. jsmn_parser parser;
  555. jsmn_init(&parser);
  556. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  557. if (tokens == NULL)
  558. {
  559. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  560. free(array_str);
  561. return NULL;
  562. }
  563. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  564. if (ret < 0)
  565. {
  566. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  567. free(tokens);
  568. free(array_str);
  569. return NULL;
  570. }
  571. if (ret < 1 || tokens[0].type != JSMN_ARRAY)
  572. {
  573. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  574. free(tokens);
  575. free(array_str);
  576. return NULL;
  577. }
  578. if (index >= (uint32_t)tokens[0].size)
  579. {
  580. // FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size);
  581. free(tokens);
  582. free(array_str);
  583. return NULL;
  584. }
  585. // Find the index-th element: start from token[1], which is the first element
  586. int elem_token = 1;
  587. for (uint32_t i = 0; i < index; i++)
  588. {
  589. elem_token = skip_token(tokens, elem_token, ret);
  590. if (elem_token == -1 || elem_token >= ret)
  591. {
  592. FURI_LOG_E("JSMM.H", "Error skipping tokens to reach element %lu.", i);
  593. free(tokens);
  594. free(array_str);
  595. return NULL;
  596. }
  597. }
  598. // Now elem_token should point to the token of the requested element
  599. jsmntok_t element = tokens[elem_token];
  600. int length = element.end - element.start;
  601. char *value = malloc(length + 1);
  602. if (!value)
  603. {
  604. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  605. free(tokens);
  606. free(array_str);
  607. return NULL;
  608. }
  609. strncpy(value, array_str + element.start, length);
  610. value[length] = '\0';
  611. free(tokens);
  612. free(array_str);
  613. return value;
  614. }
  615. // Revised get_json_array_values function with correct token skipping
  616. char **get_json_array_values(char *key, char *json_data, int *num_values)
  617. {
  618. // Retrieve the array string for the given key
  619. char *array_str = get_json_value(key, json_data);
  620. if (array_str == NULL)
  621. {
  622. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  623. return NULL;
  624. }
  625. uint32_t max_tokens = json_token_count(array_str);
  626. if (!jsmn_memory_check(max_tokens))
  627. {
  628. FURI_LOG_E("JSMM.H", "Insufficient memory for JSON tokens.");
  629. free(array_str);
  630. return NULL;
  631. }
  632. // Initialize the JSON parser
  633. jsmn_parser parser;
  634. jsmn_init(&parser);
  635. // Allocate memory for JSON tokens
  636. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens); // Allocate on the heap
  637. if (tokens == NULL)
  638. {
  639. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  640. free(array_str);
  641. return NULL;
  642. }
  643. // Parse the JSON array
  644. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  645. if (ret < 0)
  646. {
  647. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  648. free(tokens);
  649. free(array_str);
  650. return NULL;
  651. }
  652. // Ensure the root element is an array
  653. if (tokens[0].type != JSMN_ARRAY)
  654. {
  655. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  656. free(tokens);
  657. free(array_str);
  658. return NULL;
  659. }
  660. // Allocate memory for the array of values (maximum possible)
  661. int array_size = tokens[0].size;
  662. char **values = malloc(array_size * sizeof(char *));
  663. if (values == NULL)
  664. {
  665. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array of values.");
  666. free(tokens);
  667. free(array_str);
  668. return NULL;
  669. }
  670. int actual_num_values = 0;
  671. // Traverse the array and extract all object values
  672. int current_token = 1; // Start after the array token
  673. for (int i = 0; i < array_size; i++)
  674. {
  675. if (current_token >= ret)
  676. {
  677. FURI_LOG_E("JSMM.H", "Unexpected end of tokens while traversing array.");
  678. break;
  679. }
  680. jsmntok_t element = tokens[current_token];
  681. if (element.type != JSMN_OBJECT)
  682. {
  683. FURI_LOG_E("JSMM.H", "Array element %d is not an object, skipping.", i);
  684. // Skip this element
  685. current_token += 1;
  686. continue;
  687. }
  688. int length = element.end - element.start;
  689. // Allocate a new string for the value and copy the data
  690. char *value = malloc(length + 1);
  691. if (value == NULL)
  692. {
  693. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  694. for (int j = 0; j < actual_num_values; j++)
  695. {
  696. free(values[j]);
  697. }
  698. free(values);
  699. free(tokens);
  700. free(array_str);
  701. return NULL;
  702. }
  703. strncpy(value, array_str + element.start, length);
  704. value[length] = '\0'; // Null-terminate the string
  705. values[actual_num_values] = value;
  706. actual_num_values++;
  707. // Skip all tokens related to this object to avoid misparsing
  708. current_token += 1 + (2 * element.size); // Each key-value pair consumes two tokens
  709. }
  710. *num_values = actual_num_values;
  711. // Reallocate the values array to actual_num_values if necessary
  712. if (actual_num_values < array_size)
  713. {
  714. char **reduced_values = realloc(values, actual_num_values * sizeof(char *));
  715. if (reduced_values != NULL)
  716. {
  717. values = reduced_values;
  718. }
  719. // Free the remaining values
  720. for (int i = actual_num_values; i < array_size; i++)
  721. {
  722. free(values[i]);
  723. }
  724. }
  725. // Clean up
  726. free(tokens);
  727. free(array_str);
  728. return values;
  729. }
  730. int json_token_count(const char *json)
  731. {
  732. if (json == NULL)
  733. {
  734. return JSMN_ERROR_INVAL;
  735. }
  736. jsmn_parser parser;
  737. jsmn_init(&parser);
  738. // Pass NULL for tokens and 0 for num_tokens to get the token count only
  739. int ret = jsmn_parse(&parser, json, strlen(json), NULL, 0);
  740. return ret; // If ret >= 0, it represents the number of tokens needed.
  741. }