jsmn.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 *jsmn(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. // Allocate tokens array on the heap
  434. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  435. if (tokens == NULL)
  436. {
  437. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  438. return NULL;
  439. }
  440. int ret = jsmn_parse(&parser, json_data, strlen(json_data), tokens, max_tokens);
  441. if (ret < 0)
  442. {
  443. // Handle parsing errors
  444. FURI_LOG_E("JSMM.H", "Failed to parse JSON: %d", ret);
  445. free(tokens);
  446. return NULL;
  447. }
  448. // Ensure that the root element is an object
  449. if (ret < 1 || tokens[0].type != JSMN_OBJECT)
  450. {
  451. FURI_LOG_E("JSMM.H", "Root element is not an object.");
  452. free(tokens);
  453. return NULL;
  454. }
  455. // Loop through the tokens to find the key
  456. for (int i = 1; i < ret; i++)
  457. {
  458. if (jsoneq(json_data, &tokens[i], key) == 0)
  459. {
  460. // We found the key. Now, return the associated value.
  461. int length = tokens[i + 1].end - tokens[i + 1].start;
  462. char *value = malloc(length + 1);
  463. if (value == NULL)
  464. {
  465. FURI_LOG_E("JSMM.H", "Failed to allocate memory for value.");
  466. free(tokens);
  467. return NULL;
  468. }
  469. strncpy(value, json_data + tokens[i + 1].start, length);
  470. value[length] = '\0'; // Null-terminate the string
  471. free(tokens); // Free the token array
  472. return value; // Return the extracted value
  473. }
  474. }
  475. // Free the token array if key was not found
  476. free(tokens);
  477. }
  478. else
  479. {
  480. FURI_LOG_E("JSMM.H", "JSON data is NULL");
  481. }
  482. FURI_LOG_E("JSMM.H", "Failed to find the key in the JSON.");
  483. return NULL; // Return NULL if something goes wrong
  484. }
  485. // Helper function to skip a token and all its descendants.
  486. // Returns the index of the next token after skipping this one.
  487. // On error or out of bounds, returns -1.
  488. static int skip_token(const jsmntok_t *tokens, int start, int total)
  489. {
  490. if (start < 0 || start >= total)
  491. return -1;
  492. int i = start;
  493. if (tokens[i].type == JSMN_OBJECT)
  494. {
  495. // For an object: size is number of key-value pairs
  496. int pairs = tokens[i].size;
  497. i++; // move to first key-value pair
  498. for (int p = 0; p < pairs; p++)
  499. {
  500. // skip key (primitive/string)
  501. i++;
  502. if (i >= total)
  503. return -1;
  504. // skip value (which could be object/array and must be skipped recursively)
  505. i = skip_token(tokens, i, total);
  506. if (i == -1)
  507. return -1;
  508. }
  509. return i; // i is now just past the object
  510. }
  511. else if (tokens[i].type == JSMN_ARRAY)
  512. {
  513. // For an array: size is number of elements
  514. int elems = tokens[i].size;
  515. i++; // move to first element
  516. for (int e = 0; e < elems; e++)
  517. {
  518. i = skip_token(tokens, i, total);
  519. if (i == -1)
  520. return -1;
  521. }
  522. return i; // i is now just past the array
  523. }
  524. else
  525. {
  526. // Primitive or string token, just skip it
  527. return i + 1;
  528. }
  529. }
  530. // Revised get_json_array_value
  531. char *get_json_array_value(char *key, uint32_t index, const char *json_data)
  532. {
  533. // Always extract the full array each time from the original json_data
  534. char *array_str = get_json_value(key, json_data);
  535. if (array_str == NULL)
  536. {
  537. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  538. return NULL;
  539. }
  540. uint32_t max_tokens = json_token_count(array_str);
  541. jsmn_parser parser;
  542. jsmn_init(&parser);
  543. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  544. if (tokens == NULL)
  545. {
  546. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  547. free(array_str);
  548. return NULL;
  549. }
  550. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  551. if (ret < 0)
  552. {
  553. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  554. free(tokens);
  555. free(array_str);
  556. return NULL;
  557. }
  558. if (ret < 1 || tokens[0].type != JSMN_ARRAY)
  559. {
  560. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  561. free(tokens);
  562. free(array_str);
  563. return NULL;
  564. }
  565. if (index >= (uint32_t)tokens[0].size)
  566. {
  567. FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size);
  568. free(tokens);
  569. free(array_str);
  570. return NULL;
  571. }
  572. // Find the index-th element: start from token[1], which is the first element
  573. int elem_token = 1;
  574. for (uint32_t i = 0; i < index; i++)
  575. {
  576. elem_token = skip_token(tokens, elem_token, ret);
  577. if (elem_token == -1 || elem_token >= ret)
  578. {
  579. FURI_LOG_E("JSMM.H", "Error skipping tokens to reach element %lu.", i);
  580. free(tokens);
  581. free(array_str);
  582. return NULL;
  583. }
  584. }
  585. // Now elem_token should point to the token of the requested element
  586. jsmntok_t element = tokens[elem_token];
  587. int length = element.end - element.start;
  588. char *value = malloc(length + 1);
  589. if (!value)
  590. {
  591. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  592. free(tokens);
  593. free(array_str);
  594. return NULL;
  595. }
  596. strncpy(value, array_str + element.start, length);
  597. value[length] = '\0';
  598. free(tokens);
  599. free(array_str);
  600. return value;
  601. }
  602. // Revised get_json_array_values function with correct token skipping
  603. char **get_json_array_values(char *key, char *json_data, int *num_values)
  604. {
  605. // Retrieve the array string for the given key
  606. char *array_str = get_json_value(key, json_data);
  607. if (array_str == NULL)
  608. {
  609. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  610. return NULL;
  611. }
  612. uint32_t max_tokens = json_token_count(array_str);
  613. // Initialize the JSON parser
  614. jsmn_parser parser;
  615. jsmn_init(&parser);
  616. // Allocate memory for JSON tokens
  617. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens); // Allocate on the heap
  618. if (tokens == NULL)
  619. {
  620. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  621. free(array_str);
  622. return NULL;
  623. }
  624. // Parse the JSON array
  625. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  626. if (ret < 0)
  627. {
  628. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  629. free(tokens);
  630. free(array_str);
  631. return NULL;
  632. }
  633. // Ensure the root element is an array
  634. if (tokens[0].type != JSMN_ARRAY)
  635. {
  636. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  637. free(tokens);
  638. free(array_str);
  639. return NULL;
  640. }
  641. // Allocate memory for the array of values (maximum possible)
  642. int array_size = tokens[0].size;
  643. char **values = malloc(array_size * sizeof(char *));
  644. if (values == NULL)
  645. {
  646. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array of values.");
  647. free(tokens);
  648. free(array_str);
  649. return NULL;
  650. }
  651. int actual_num_values = 0;
  652. // Traverse the array and extract all object values
  653. int current_token = 1; // Start after the array token
  654. for (int i = 0; i < array_size; i++)
  655. {
  656. if (current_token >= ret)
  657. {
  658. FURI_LOG_E("JSMM.H", "Unexpected end of tokens while traversing array.");
  659. break;
  660. }
  661. jsmntok_t element = tokens[current_token];
  662. if (element.type != JSMN_OBJECT)
  663. {
  664. FURI_LOG_E("JSMM.H", "Array element %d is not an object, skipping.", i);
  665. // Skip this element
  666. current_token += 1;
  667. continue;
  668. }
  669. int length = element.end - element.start;
  670. // Allocate a new string for the value and copy the data
  671. char *value = malloc(length + 1);
  672. if (value == NULL)
  673. {
  674. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  675. for (int j = 0; j < actual_num_values; j++)
  676. {
  677. free(values[j]);
  678. }
  679. free(values);
  680. free(tokens);
  681. free(array_str);
  682. return NULL;
  683. }
  684. strncpy(value, array_str + element.start, length);
  685. value[length] = '\0'; // Null-terminate the string
  686. values[actual_num_values] = value;
  687. actual_num_values++;
  688. // Skip all tokens related to this object to avoid misparsing
  689. current_token += 1 + (2 * element.size); // Each key-value pair consumes two tokens
  690. }
  691. *num_values = actual_num_values;
  692. // Reallocate the values array to actual_num_values if necessary
  693. if (actual_num_values < array_size)
  694. {
  695. char **reduced_values = realloc(values, actual_num_values * sizeof(char *));
  696. if (reduced_values != NULL)
  697. {
  698. values = reduced_values;
  699. }
  700. // Free the remaining values
  701. for (int i = actual_num_values; i < array_size; i++)
  702. {
  703. free(values[i]);
  704. }
  705. }
  706. // Clean up
  707. free(tokens);
  708. free(array_str);
  709. return values;
  710. }
  711. int json_token_count(const char *json)
  712. {
  713. if (json == NULL)
  714. {
  715. return JSMN_ERROR_INVAL;
  716. }
  717. jsmn_parser parser;
  718. jsmn_init(&parser);
  719. // Pass NULL for tokens and 0 for num_tokens to get the token count only
  720. int ret = jsmn_parse(&parser, json, strlen(json), NULL, 0);
  721. return ret; // If ret >= 0, it represents the number of tokens needed.
  722. }