diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2012-06-16 14:07:37 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2012-06-16 14:07:37 +0300 |
commit | e18352d50678005c9dbb3ac76913555f5317c81c (patch) | |
tree | 5aabaaaff018d39e548ca5e4777005030af0e782 /pb_decode.c | |
parent | 7e1059628c1d67f96c00781cf2f57c915feadde1 (diff) |
Added new functions to public interface in pb_decode.h.
pb_decode_tag and pb_skip_field allow manually iterating the fields
in a message.
Diffstat (limited to 'pb_decode.c')
-rw-r--r-- | pb_decode.c | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/pb_decode.c b/pb_decode.c index eace906..50a11c4 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -117,12 +117,33 @@ bool checkreturn pb_skip_string(pb_istream_t *stream) return pb_read(stream, NULL, length); } -/* Currently the wire type related stuff is kept hidden from - * callbacks. They shouldn't need it. It's better for performance - * to just assume the correct type and fail safely on corrupt message. - */ +bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, int *tag, bool *eof) +{ + uint32_t temp; + *eof = false; + *wire_type = 0; + *tag = 0; + + if (!pb_decode_varint32(stream, &temp)) + { + if (stream->bytes_left == 0) + *eof = true; + + return false; + } + + if (temp == 0) + { + *eof = true; /* Special feature: allow 0-terminated messages. */ + return false; + } + + *tag = temp >> 3; + *wire_type = (pb_wire_type_t)(temp & 7); + return true; +} -static bool checkreturn skip(pb_istream_t *stream, pb_wire_type_t wire_type) +bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type) { switch (wire_type) { @@ -292,7 +313,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t pb_callback_t *pCallback = (pb_callback_t*)iter->pData; if (pCallback->funcs.decode == NULL) - return skip(stream, wire_type); + return pb_skip_field(stream, wire_type); if (wire_type == PB_WT_STRING) { @@ -392,27 +413,22 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void while (stream->bytes_left) { - uint32_t temp; int tag; pb_wire_type_t wire_type; - if (!pb_decode_varint32(stream, &temp)) + bool eof; + + if (!pb_decode_tag(stream, &wire_type, &tag, &eof)) { - if (stream->bytes_left == 0) - break; /* It was EOF */ + if (eof) + break; else - return false; /* It was error */ + return false; } - if (temp == 0) - break; /* Special feature: allow 0-terminated messages. */ - - tag = temp >> 3; - wire_type = (pb_wire_type_t)(temp & 7); - if (!pb_field_find(&iter, tag)) { /* No match found, skip data */ - if (!skip(stream, wire_type)) + if (!pb_skip_field(stream, wire_type)) return false; continue; } |