* [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper @ 2023-09-21 10:24 Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 2/4] lib: jsmn: add helper for allocating tokens Ahmad Fatoum ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: Ahmad Fatoum @ 2023-09-21 10:24 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum To make code a bit more terse, add a simple helper to get a token's size and start using it in some of the existing jsmn helpers. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- include/jsmn.h | 5 +++++ lib/jsmn.c | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/jsmn.h b/include/jsmn.h index 8f6db8d534f4..2dd1d5ffc08b 100644 --- a/include/jsmn.h +++ b/include/jsmn.h @@ -84,6 +84,11 @@ 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); +static inline int jsmn_token_size(const jsmntok_t *token) +{ + return token->end - token->start; +} + /** Returns `true` if `token` is a string and equal to `str`. */ JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *token); diff --git a/lib/jsmn.c b/lib/jsmn.c index 7bdcc90f2f4b..9eeed790d93d 100644 --- a/lib/jsmn.c +++ b/lib/jsmn.c @@ -372,7 +372,7 @@ JSMN_API void jsmn_init(jsmn_parser *parser) { JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token) { - size_t token_size = token->end - token->start; + size_t token_size = jsmn_token_size(token); return strlen(val) == token_size && strncmp(json + token->start, val, token_size) == 0; } @@ -446,7 +446,7 @@ JSMN_API char *jsmn_strcpy(const char *path[], const char *json, if (!node || node->type != JSMN_STRING) return NULL; - value_size = node->end - node->start; + value_size = jsmn_token_size(node); value = malloc(value_size + 1); if (value) { strncpy(value, json + node->start, value_size); -- 2.39.2 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/4] lib: jsmn: add helper for allocating tokens 2023-09-21 10:24 [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper Ahmad Fatoum @ 2023-09-21 10:24 ` Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 3/4] lib: jsmn: add case-insensitive comparison Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 4/4] lib: rename json_strcpy to json_strdup Ahmad Fatoum 2 siblings, 0 replies; 7+ messages in thread From: Ahmad Fatoum @ 2023-09-21 10:24 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum 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> --- include/jsmn.h | 6 ++++++ lib/jsmn.c | 36 ++++++++++++++++++++++++++++++++++++ test/self/json.c | 33 ++++----------------------------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/include/jsmn.h b/include/jsmn.h index 2dd1d5ffc08b..62197c2593b4 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 9eeed790d93d..3d2ada7b7fdc 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 54323cf4350c..d3088f1b7542 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")) { -- 2.39.2 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/4] lib: jsmn: add case-insensitive comparison 2023-09-21 10:24 [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 2/4] lib: jsmn: add helper for allocating tokens Ahmad Fatoum @ 2023-09-21 10:24 ` Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 4/4] lib: rename json_strcpy to json_strdup Ahmad Fatoum 2 siblings, 0 replies; 7+ messages in thread From: Ahmad Fatoum @ 2023-09-21 10:24 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum Users may want to do a case-insensitive comparison of tokens. Add simple helpers for that. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- include/jsmn.h | 3 +++ lib/jsmn.c | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/jsmn.h b/include/jsmn.h index 62197c2593b4..64b3b535ab6e 100644 --- a/include/jsmn.h +++ b/include/jsmn.h @@ -101,6 +101,9 @@ JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *to /** Returns `true` if `token` is to `str`. */ JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token); +/** Returns `true` if `token` is equal to `str`, ignoring case. */ +JSMN_API bool jsmn_strcase_eq(const char *str, const char *json, const jsmntok_t *token); + /** Returns the token after the value at `tokens[0]`. */ JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens); diff --git a/lib/jsmn.c b/lib/jsmn.c index 3d2ada7b7fdc..9e624f7518ad 100644 --- a/lib/jsmn.c +++ b/lib/jsmn.c @@ -418,6 +418,18 @@ JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *to return token->type == JSMN_STRING && jsmn_eq(str, json, token); } +static bool jsmn_case_eq(const char *val, const char *json, const jsmntok_t *token) +{ + size_t token_size = jsmn_token_size(token); + return strlen(val) == token_size + && strncasecmp(json + token->start, val, token_size) == 0; +} + +JSMN_API bool jsmn_strcase_eq(const char *str, const char *json, const jsmntok_t *token) +{ + return token->type == JSMN_STRING && jsmn_case_eq(str, json, token); +} + JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens) { int max_index = tokens[0].end; -- 2.39.2 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 4/4] lib: rename json_strcpy to json_strdup 2023-09-21 10:24 [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 2/4] lib: jsmn: add helper for allocating tokens Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 3/4] lib: jsmn: add case-insensitive comparison Ahmad Fatoum @ 2023-09-21 10:24 ` Ahmad Fatoum 2023-09-21 13:03 ` Sascha Hauer 2 siblings, 1 reply; 7+ messages in thread From: Ahmad Fatoum @ 2023-09-21 10:24 UTC (permalink / raw) To: barebox; +Cc: Ahmad Fatoum json_strcpy is an unfortunate name for a function that allocates memory. Rename it to json_strdup instead. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- include/jsmn.h | 2 +- lib/jsmn.c | 2 +- test/self/json.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/jsmn.h b/include/jsmn.h index 64b3b535ab6e..156ae2086439 100644 --- a/include/jsmn.h +++ b/include/jsmn.h @@ -153,7 +153,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, * value does not exist or is not a string. The caller takes ownership of the * pointer returned. */ -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, const jsmntok_t *tokens); +JSMN_API char *jsmn_strdup(const char *path[], const char *json, const jsmntok_t *tokens); #ifdef __cplusplus } diff --git a/lib/jsmn.c b/lib/jsmn.c index 9e624f7518ad..10a77886a8bd 100644 --- a/lib/jsmn.c +++ b/lib/jsmn.c @@ -483,7 +483,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, return tokens; } -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, +JSMN_API char *jsmn_strdup(const char *path[], const char *json, const jsmntok_t *tokens) { const jsmntok_t *node; diff --git a/test/self/json.c b/test/self/json.c index d3088f1b7542..489671768923 100644 --- a/test/self/json.c +++ b/test/self/json.c @@ -104,7 +104,7 @@ static void test_json(void) token = jsmn_skip_value(token); __json_expect(json, token, JP("boolean"), JSMN_PRIMITIVE, "true"); - string = jsmn_strcpy(JP("string"), json, tokens); + string = jsmn_strdup(JP("string"), json, tokens); if (WARN_ON(!string)) goto out; -- 2.39.2 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] lib: rename json_strcpy to json_strdup 2023-09-21 10:24 ` [PATCH 4/4] lib: rename json_strcpy to json_strdup Ahmad Fatoum @ 2023-09-21 13:03 ` Sascha Hauer 2023-09-21 13:04 ` Ahmad Fatoum 0 siblings, 1 reply; 7+ messages in thread From: Sascha Hauer @ 2023-09-21 13:03 UTC (permalink / raw) To: Ahmad Fatoum; +Cc: barebox On Thu, Sep 21, 2023 at 12:24:26PM +0200, Ahmad Fatoum wrote: > json_strcpy is an unfortunate name for a function that allocates > memory. Rename it to json_strdup instead. s/json_strcpy/jsmn_strcpy/ s/json_strdup/jsmn_strdup/ Sascha > > Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> > --- > include/jsmn.h | 2 +- > lib/jsmn.c | 2 +- > test/self/json.c | 2 +- > 3 files changed, 3 insertions(+), 3 deletions(-) > > diff --git a/include/jsmn.h b/include/jsmn.h > index 64b3b535ab6e..156ae2086439 100644 > --- a/include/jsmn.h > +++ b/include/jsmn.h > @@ -153,7 +153,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, > * value does not exist or is not a string. The caller takes ownership of the > * pointer returned. > */ > -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, const jsmntok_t *tokens); > +JSMN_API char *jsmn_strdup(const char *path[], const char *json, const jsmntok_t *tokens); > > #ifdef __cplusplus > } > diff --git a/lib/jsmn.c b/lib/jsmn.c > index 9e624f7518ad..10a77886a8bd 100644 > --- a/lib/jsmn.c > +++ b/lib/jsmn.c > @@ -483,7 +483,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, > return tokens; > } > > -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, > +JSMN_API char *jsmn_strdup(const char *path[], const char *json, > const jsmntok_t *tokens) > { > const jsmntok_t *node; > diff --git a/test/self/json.c b/test/self/json.c > index d3088f1b7542..489671768923 100644 > --- a/test/self/json.c > +++ b/test/self/json.c > @@ -104,7 +104,7 @@ static void test_json(void) > token = jsmn_skip_value(token); > __json_expect(json, token, JP("boolean"), JSMN_PRIMITIVE, "true"); > > - string = jsmn_strcpy(JP("string"), json, tokens); > + string = jsmn_strdup(JP("string"), json, tokens); > if (WARN_ON(!string)) > goto out; > > -- > 2.39.2 > > > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] lib: rename json_strcpy to json_strdup 2023-09-21 13:03 ` Sascha Hauer @ 2023-09-21 13:04 ` Ahmad Fatoum 2023-09-21 13:08 ` Sascha Hauer 0 siblings, 1 reply; 7+ messages in thread From: Ahmad Fatoum @ 2023-09-21 13:04 UTC (permalink / raw) To: Sascha Hauer; +Cc: barebox On 21.09.23 15:03, Sascha Hauer wrote: > On Thu, Sep 21, 2023 at 12:24:26PM +0200, Ahmad Fatoum wrote: >> json_strcpy is an unfortunate name for a function that allocates >> memory. Rename it to json_strdup instead. > > s/json_strcpy/jsmn_strcpy/ > s/json_strdup/jsmn_strdup/ Right. Should I resend? > > Sascha > >> >> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> >> --- >> include/jsmn.h | 2 +- >> lib/jsmn.c | 2 +- >> test/self/json.c | 2 +- >> 3 files changed, 3 insertions(+), 3 deletions(-) >> >> diff --git a/include/jsmn.h b/include/jsmn.h >> index 64b3b535ab6e..156ae2086439 100644 >> --- a/include/jsmn.h >> +++ b/include/jsmn.h >> @@ -153,7 +153,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, >> * value does not exist or is not a string. The caller takes ownership of the >> * pointer returned. >> */ >> -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, const jsmntok_t *tokens); >> +JSMN_API char *jsmn_strdup(const char *path[], const char *json, const jsmntok_t *tokens); >> >> #ifdef __cplusplus >> } >> diff --git a/lib/jsmn.c b/lib/jsmn.c >> index 9e624f7518ad..10a77886a8bd 100644 >> --- a/lib/jsmn.c >> +++ b/lib/jsmn.c >> @@ -483,7 +483,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, >> return tokens; >> } >> >> -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, >> +JSMN_API char *jsmn_strdup(const char *path[], const char *json, >> const jsmntok_t *tokens) >> { >> const jsmntok_t *node; >> diff --git a/test/self/json.c b/test/self/json.c >> index d3088f1b7542..489671768923 100644 >> --- a/test/self/json.c >> +++ b/test/self/json.c >> @@ -104,7 +104,7 @@ static void test_json(void) >> token = jsmn_skip_value(token); >> __json_expect(json, token, JP("boolean"), JSMN_PRIMITIVE, "true"); >> >> - string = jsmn_strcpy(JP("string"), json, tokens); >> + string = jsmn_strdup(JP("string"), json, tokens); >> if (WARN_ON(!string)) >> goto out; >> >> -- >> 2.39.2 >> >> >> > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 4/4] lib: rename json_strcpy to json_strdup 2023-09-21 13:04 ` Ahmad Fatoum @ 2023-09-21 13:08 ` Sascha Hauer 0 siblings, 0 replies; 7+ messages in thread From: Sascha Hauer @ 2023-09-21 13:08 UTC (permalink / raw) To: Ahmad Fatoum; +Cc: barebox On Thu, Sep 21, 2023 at 03:04:44PM +0200, Ahmad Fatoum wrote: > On 21.09.23 15:03, Sascha Hauer wrote: > > On Thu, Sep 21, 2023 at 12:24:26PM +0200, Ahmad Fatoum wrote: > >> json_strcpy is an unfortunate name for a function that allocates > >> memory. Rename it to json_strdup instead. > > > > s/json_strcpy/jsmn_strcpy/ > > s/json_strdup/jsmn_strdup/ > > Right. Should I resend? No, that's all for this series. Fixed while applying. Sascha > > > > > Sascha > > > >> > >> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> > >> --- > >> include/jsmn.h | 2 +- > >> lib/jsmn.c | 2 +- > >> test/self/json.c | 2 +- > >> 3 files changed, 3 insertions(+), 3 deletions(-) > >> > >> diff --git a/include/jsmn.h b/include/jsmn.h > >> index 64b3b535ab6e..156ae2086439 100644 > >> --- a/include/jsmn.h > >> +++ b/include/jsmn.h > >> @@ -153,7 +153,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, > >> * value does not exist or is not a string. The caller takes ownership of the > >> * pointer returned. > >> */ > >> -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, const jsmntok_t *tokens); > >> +JSMN_API char *jsmn_strdup(const char *path[], const char *json, const jsmntok_t *tokens); > >> > >> #ifdef __cplusplus > >> } > >> diff --git a/lib/jsmn.c b/lib/jsmn.c > >> index 9e624f7518ad..10a77886a8bd 100644 > >> --- a/lib/jsmn.c > >> +++ b/lib/jsmn.c > >> @@ -483,7 +483,7 @@ JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json, > >> return tokens; > >> } > >> > >> -JSMN_API char *jsmn_strcpy(const char *path[], const char *json, > >> +JSMN_API char *jsmn_strdup(const char *path[], const char *json, > >> const jsmntok_t *tokens) > >> { > >> const jsmntok_t *node; > >> diff --git a/test/self/json.c b/test/self/json.c > >> index d3088f1b7542..489671768923 100644 > >> --- a/test/self/json.c > >> +++ b/test/self/json.c > >> @@ -104,7 +104,7 @@ static void test_json(void) > >> token = jsmn_skip_value(token); > >> __json_expect(json, token, JP("boolean"), JSMN_PRIMITIVE, "true"); > >> > >> - string = jsmn_strcpy(JP("string"), json, tokens); > >> + string = jsmn_strdup(JP("string"), json, tokens); > >> if (WARN_ON(!string)) > >> goto out; > >> > >> -- > >> 2.39.2 > >> > >> > >> > > > > -- > Pengutronix e.K. | | > Steuerwalder Str. 21 | http://www.pengutronix.de/ | > 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | > Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | > > > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-09-21 13:10 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-09-21 10:24 [PATCH 1/4] lib: jsmn: add and use new jsmn_token_size helper Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 2/4] lib: jsmn: add helper for allocating tokens Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 3/4] lib: jsmn: add case-insensitive comparison Ahmad Fatoum 2023-09-21 10:24 ` [PATCH 4/4] lib: rename json_strcpy to json_strdup Ahmad Fatoum 2023-09-21 13:03 ` Sascha Hauer 2023-09-21 13:04 ` Ahmad Fatoum 2023-09-21 13:08 ` Sascha Hauer
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox