summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2023-09-21 12:24:24 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2023-09-22 11:43:16 +0200
commite25184ba656d0b7e2a5b04aeae0e65a838beaff5 (patch)
tree41ea697e54cb75d87036369a34d367b68eb3418b
parent0c8bd55b08a47493f6cde8a5ad9ca291fc262e6c (diff)
downloadbarebox-e25184ba656d0b7e2a5b04aeae0e65a838beaff5.tar.gz
barebox-e25184ba656d0b7e2a5b04aeae0e65a838beaff5.tar.xz
lib: jsmn: add helper for allocating tokens
jsmn_parse can be called with a NULL buffer to determine the needed allocation size beforehand. Most users will go on to dynamically allocate that buffer, so add a helper that does just that and start using it to simplify the json selftest. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20230921102426.1109289-2-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--include/jsmn.h6
-rw-r--r--lib/jsmn.c36
-rw-r--r--test/self/json.c33
3 files changed, 46 insertions, 29 deletions
diff --git a/include/jsmn.h b/include/jsmn.h
index 2dd1d5ffc0..62197c2593 100644
--- a/include/jsmn.h
+++ b/include/jsmn.h
@@ -84,6 +84,12 @@ JSMN_API void jsmn_init(jsmn_parser *parser);
JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
jsmntok_t *tokens, const unsigned int num_tokens);
+/**
+ * Like jsmn_parse, but allocates tokens dynamically.
+ */
+JSMN_API jsmntok_t *jsmn_parse_alloc(const char *js, const size_t len,
+ unsigned int *num_tokens);
+
static inline int jsmn_token_size(const jsmntok_t *token)
{
return token->end - token->start;
diff --git a/lib/jsmn.c b/lib/jsmn.c
index 9eeed790d9..3d2ada7b7f 100644
--- a/lib/jsmn.c
+++ b/lib/jsmn.c
@@ -370,6 +370,42 @@ JSMN_API void jsmn_init(jsmn_parser *parser) {
parser->toksuper = -1;
}
+/**
+ * Parse JSON string and fill tokens into self-allocated buffer.
+ */
+JSMN_API jsmntok_t *jsmn_parse_alloc(const char *js, const size_t len,
+ unsigned int *num_tokens)
+{
+
+ ssize_t token_count;
+ jsmn_parser parser;
+ jsmntok_t *tokens;
+ int ret;
+
+ jsmn_init(&parser);
+
+ /* Figure out how many tokens we need. */
+ ret = jsmn_parse(&parser, js, len, NULL, 0);
+ if (ret < 0)
+ return NULL;
+
+ token_count = ret;
+
+ tokens = kmalloc_array(token_count, sizeof(jsmntok_t), GFP_KERNEL);
+ if (!tokens)
+ return NULL;
+
+ jsmn_init(&parser);
+ ret = jsmn_parse(&parser, js, len, tokens, token_count);
+ if (ret < 0) {
+ free(tokens);
+ return NULL;
+ }
+
+ if (num_tokens)
+ *num_tokens = ret;
+ return tokens;
+}
JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token)
{
size_t token_size = jsmn_token_size(token);
diff --git a/test/self/json.c b/test/self/json.c
index 54323cf435..d3088f1b75 100644
--- a/test/self/json.c
+++ b/test/self/json.c
@@ -75,45 +75,20 @@ static const char json[] =
static void test_json(void)
{
- ssize_t token_count;
const jsmntok_t *token;
jsmntok_t *tokens;
- jsmn_parser parser;
char *string;
- int ret;
total_tests++;
- jsmn_init(&parser);
-
/* Figure out how many tokens we need. */
- ret = jsmn_parse(&parser, json, sizeof(json), NULL, 0);
- if (ret < 0) {
- printf("failed to determine number of tokens: %d\n", ret);
+ tokens = jsmn_parse_alloc(json, sizeof(json), NULL);
+ if (!tokens) {
+ printf("failed to parse JSON\n");
failed_tests++;
return;
}
- token_count = ret;
-
- /* `token_count` is strictly less than `length` which is strictly less
- * than CONFIG_SYS_EEPROM_SIZE (or 8K), so we should never overflow an
- * int here.
- */
- tokens = kmalloc_array(token_count, sizeof(jsmntok_t), GFP_KERNEL);
- if (WARN_ON(!tokens))
- return;
-
- total_tests++;
-
- jsmn_init(&parser);
- ret = jsmn_parse(&parser, json, sizeof(json), tokens, token_count);
- if (ret < 0) {
- printf("failed to parse JSON with tokens: %d\n", ret);
- failed_tests++;
- goto out;
- }
-
json_expect(json, tokens, JP("null"), JSMN_PRIMITIVE, "null");
json_expect(json, tokens, JP("number"), JSMN_PRIMITIVE, "0x42");
json_expect(json, tokens, JP("object"), JSMN_OBJECT, NULL);
@@ -131,7 +106,7 @@ static void test_json(void)
string = jsmn_strcpy(JP("string"), json, tokens);
if (WARN_ON(!string))
- return;
+ goto out;
total_tests++;
if (strcmp(string, "Hello World\n")) {