summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLloyd Trevor Hilaiel <lloyd@hilaiel.com>2009-12-13 22:09:58 -0700
committerLloyd Trevor Hilaiel <lloyd@hilaiel.com>2009-12-13 22:09:58 -0700
commitf02de6765e5b8415fda1b080aeff2875a81ef6b1 (patch)
tree9c4d58000c3bd1ad269f4f125198a13931ce7d9c
parent16af3a8ea56e5144cabd62dba6ac9f75b8d2061f (diff)
downloadyajl-f02de6765e5b8415fda1b080aeff2875a81ef6b1.tar.gz
change yajl_get_error_offset() to yajl_get_bytes_consumed(), which will allow the client to determine how much input was consumed in non-error cases (and in turn, handle junk at end of input parse errors... that is, insure the entire input buffer is consumed)
-rw-r--r--src/api/yajl_parse.h8
-rw-r--r--src/yajl.c9
-rw-r--r--src/yajl_parser.c10
-rw-r--r--src/yajl_parser.h7
4 files changed, 22 insertions, 12 deletions
diff --git a/src/api/yajl_parse.h b/src/api/yajl_parse.h
index 3741318..e3c2f13 100644
--- a/src/api/yajl_parse.h
+++ b/src/api/yajl_parse.h
@@ -170,12 +170,18 @@ extern "C" {
unsigned int jsonTextLength);
/**
+ * get the amount of data consumed from the last chunk passed to YAJL.
+ *
+ * In the case of a successful parse this can help you understand if
+ * the entire buffer was consumed (which will allow you to handle
+ * "junk at end of input".
+ *
* In the event an error is encountered during parsing, this function
* affords the client a way to get the offset into the most recent
* chunk where the error occured. 0 will be returned if no error
* was encountered.
*/
- unsigned int YAJL_API yajl_get_error_offset(yajl_handle hand);
+ unsigned int YAJL_API yajl_get_bytes_consumed(yajl_handle hand);
/** free an error returned from yajl_get_error */
void YAJL_API yajl_free_error(yajl_handle hand, unsigned char * str);
diff --git a/src/yajl.c b/src/yajl.c
index b94ae53..0684f9e 100644
--- a/src/yajl.c
+++ b/src/yajl.c
@@ -95,7 +95,7 @@ yajl_alloc(const yajl_callbacks * callbacks,
hand->callbacks = callbacks;
hand->ctx = ctx;
hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8);
- hand->errorOffset = 0;
+ hand->bytesConsumed = 0;
hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
yajl_bs_init(hand->stateStack, &(hand->alloc));
@@ -117,9 +117,8 @@ yajl_status
yajl_parse(yajl_handle hand, const unsigned char * jsonText,
unsigned int jsonTextLen)
{
- unsigned int offset = 0;
yajl_status status;
- status = yajl_do_parse(hand, &offset, jsonText, jsonTextLen);
+ status = yajl_do_parse(hand, 0, jsonText, jsonTextLen);
return status;
}
@@ -143,10 +142,10 @@ yajl_get_error(yajl_handle hand, int verbose,
}
unsigned int
-yajl_get_error_offset(yajl_handle hand)
+yajl_get_bytes_consumed(yajl_handle hand)
{
if (!hand) return 0;
- else return hand->errorOffset;
+ else return hand->bytesConsumed;
}
diff --git a/src/yajl_parser.c b/src/yajl_parser.c
index 9f1824b..122b823 100644
--- a/src/yajl_parser.c
+++ b/src/yajl_parser.c
@@ -48,7 +48,7 @@ unsigned char *
yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
unsigned int jsonTextLen, int verbose)
{
- unsigned int offset = hand->errorOffset;
+ unsigned int offset = hand->bytesConsumed;
unsigned char * str;
const char * errorType = NULL;
const char * errorText = NULL;
@@ -131,18 +131,21 @@ yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
yajl_bs_set(hand->stateStack, yajl_state_parse_error); \
hand->parseError = \
"client cancelled parse via callback return value"; \
- hand->errorOffset = *offset; \
return yajl_status_client_canceled; \
}
yajl_status
-yajl_do_parse(yajl_handle hand, unsigned int * offset,
+yajl_do_parse(yajl_handle hand, unsigned int offsetStart,
const unsigned char * jsonText, unsigned int jsonTextLen)
{
yajl_tok tok;
const unsigned char * buf;
unsigned int bufLen;
+ unsigned int * offset = &(hand->bytesConsumed);
+
+ *offset = offsetStart;
+
around_again:
switch (yajl_bs_current(hand->stateStack)) {
@@ -150,7 +153,6 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
return yajl_status_ok;
case yajl_state_lexical_error:
case yajl_state_parse_error:
- hand->errorOffset = *offset;
return yajl_status_error;
case yajl_state_start:
case yajl_state_map_need_val:
diff --git a/src/yajl_parser.h b/src/yajl_parser.h
index 92b4a40..d0bcece 100644
--- a/src/yajl_parser.h
+++ b/src/yajl_parser.h
@@ -58,7 +58,10 @@ struct yajl_handle_t {
void * ctx;
yajl_lexer lexer;
const char * parseError;
- unsigned int errorOffset;
+ /* the number of bytes consumed from the last client buffer,
+ * in the case of an error this will be an error offset, in the
+ * case of an error this can be used as the error offset */
+ unsigned int bytesConsumed;
/* temporary storage for decoded strings */
yajl_buf decodeBuf;
/* a stack of states. access with yajl_state_XXX routines */
@@ -68,7 +71,7 @@ struct yajl_handle_t {
};
yajl_status
-yajl_do_parse(yajl_handle handle, unsigned int * offset,
+yajl_do_parse(yajl_handle handle, unsigned int offset,
const unsigned char * jsonText, unsigned int jsonTextLen);
unsigned char *