diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2019-01-25 21:37:15 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2019-01-25 21:37:15 +0300 |
commit | e31fa8eb03774522ec1b3a46369b3669e944f59d (patch) | |
tree | 6c001326406a7ac0368c5c9b6cc657a411da3fe5 /pb_decode.h | |
parent | b2d04dfceaac1dc35fcde2706e56d090222d2761 (diff) |
Add pb_decode_ex(), pb_encode_ex() functions.
This should simplify specifying combinations, so that we don't
need every variant of pb_decode_noinit_delimited() etc.
Also disables zero-terminated decoding in pb_decode (issue #278).
Diffstat (limited to 'pb_decode.h')
-rw-r--r-- | pb_decode.h | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/pb_decode.h b/pb_decode.h index af7e678..e55e3c3 100644 --- a/pb_decode.h +++ b/pb_decode.h @@ -73,35 +73,39 @@ struct pb_istream_s */ bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct); -/* Same as pb_decode, except does not initialize the destination structure - * to default values. This is slightly faster if you need no default values - * and just do memset(struct, 0, sizeof(struct)) yourself. +/* Extended version of pb_decode, with several options to control + * the decoding process: * - * This can also be used for 'merging' two messages, i.e. update only the - * fields that exist in the new message. + * PB_DECODE_NOINIT: Do not initialize the fields to default values. + * This is slightly faster if you do not need the default + * values and instead initialize the structure to 0 using + * e.g. memset(). This can also be used for merging two + * messages, i.e. combine already existing data with new + * values. * - * Note: If this function returns with an error, it will not release any - * dynamically allocated fields. You will need to call pb_release() yourself. - */ -bool pb_decode_noinit(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct); - -/* Same as pb_decode, except expects the stream to start with the message size - * encoded as varint. Corresponds to parseDelimitedFrom() in Google's - * protobuf API. - */ -bool pb_decode_delimited(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct); - -/* Same as pb_decode_delimited, except that it does not initialize the destination structure. - * See pb_decode_noinit - */ -bool pb_decode_delimited_noinit(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct); - -/* Same as pb_decode, except allows the message to be terminated with a null byte. - * NOTE: Until nanopb-0.4.0, pb_decode() also allows null-termination. This behaviour - * is not supported in most other protobuf implementations, so pb_decode_delimited() - * is a better option for compatibility. + * PB_DECODE_DELIMITED: Input message starts with the message size as varint. + * Corresponds to parseDelimitedFrom() in Google's + * protobuf API. + * + * PB_DECODE_NULLTERMINATED: Stop reading when field tag is read as 0. This allows + * reading null terminated messages. + * NOTE: Until nanopb-0.4.0, pb_decode() also allows + * null-termination. This behaviour is not supported in + * most other protobuf implementations, so PB_DECODE_DELIMITED + * is a better option for compatibility. + * + * Multiple flags can be combined with bitwise or (| operator) */ -bool pb_decode_nullterminated(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct); +#define PB_DECODE_NOINIT 0x01 +#define PB_DECODE_DELIMITED 0x02 +#define PB_DECODE_NULLTERMINATED 0x04 +bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags); + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define pb_decode_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NOINIT) +#define pb_decode_delimited(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED) +#define pb_decode_delimited_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED | PB_DECODE_NOINIT) +#define pb_decode_nullterminated(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NULLTERMINATED) #ifdef PB_ENABLE_MALLOC /* Release any allocated pointer fields. If you use dynamic allocation, you should |