From 385b42e740fd8e040057ba682e402a208f211db7 Mon Sep 17 00:00:00 2001 From: Gabriel Gritsch Date: Wed, 12 Feb 2014 17:53:45 +0100 Subject: [PATCH] - Added possibility to specify the lenght of the input buffer (if not null-terminated). - Added "extern C" to use it with CPP. --- jsmn.c | 10 ++++++++-- jsmn.h | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/jsmn.c b/jsmn.c index 2956230..0bf92ff 100644 --- a/jsmn.c +++ b/jsmn.c @@ -1,4 +1,5 @@ #include +#include #include "jsmn.h" @@ -143,13 +144,18 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, /** * Parse JSON string and fill tokens. */ -jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, +jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length, jsmntok_t *tokens, unsigned int num_tokens) { jsmnerr_t r; int i; jsmntok_t *token; + + if (js_length <= 0) + { + js_length = INT_MAX; + } - for (; js[parser->pos] != '\0'; parser->pos++) { + for (; parser->pos < js_length && js[parser->pos] != '\0'; parser->pos++) { char c; jsmntype_t type; diff --git a/jsmn.h b/jsmn.h index 5aa6e3c..ecb4752 100644 --- a/jsmn.h +++ b/jsmn.h @@ -1,6 +1,10 @@ #ifndef __JSMN_H_ #define __JSMN_H_ +#ifdef __cplusplus +extern "C" { +#endif + /** * JSON type identifier. Basic types are: * o Object @@ -61,7 +65,11 @@ void jsmn_init(jsmn_parser *parser); * Run JSON parser. It parses a JSON data string into and array of tokens, each describing * a single JSON object. */ -jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, +jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length, jsmntok_t *tokens, unsigned int num_tokens); +#ifdef __cplusplus +} +#endif + #endif /* __JSMN_H_ */