Nanopb files
Dependents: nanopb_V2 Message_generator LEX_Threaded_Programming_V3
pb_decode.h@0:c7beea49fc91, 2013-02-19 (annotated)
- Committer:
- intrinseca
- Date:
- Tue Feb 19 20:03:29 2013 +0000
- Revision:
- 0:c7beea49fc91
- Child:
- 2:d2c61a9be078
Initial Uploaded Version 0.1.9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
intrinseca | 0:c7beea49fc91 | 1 | #ifndef _PB_DECODE_H_ |
intrinseca | 0:c7beea49fc91 | 2 | #define _PB_DECODE_H_ |
intrinseca | 0:c7beea49fc91 | 3 | |
intrinseca | 0:c7beea49fc91 | 4 | /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c. |
intrinseca | 0:c7beea49fc91 | 5 | * The main function is pb_decode. You will also need to create an input |
intrinseca | 0:c7beea49fc91 | 6 | * stream, which is easiest to do with pb_istream_from_buffer(). |
intrinseca | 0:c7beea49fc91 | 7 | * |
intrinseca | 0:c7beea49fc91 | 8 | * You also need structures and their corresponding pb_field_t descriptions. |
intrinseca | 0:c7beea49fc91 | 9 | * These are usually generated from .proto-files with a script. |
intrinseca | 0:c7beea49fc91 | 10 | */ |
intrinseca | 0:c7beea49fc91 | 11 | |
intrinseca | 0:c7beea49fc91 | 12 | #include <stdbool.h> |
intrinseca | 0:c7beea49fc91 | 13 | #include "pb.h" |
intrinseca | 0:c7beea49fc91 | 14 | |
intrinseca | 0:c7beea49fc91 | 15 | #ifdef __cplusplus |
intrinseca | 0:c7beea49fc91 | 16 | extern "C" { |
intrinseca | 0:c7beea49fc91 | 17 | #endif |
intrinseca | 0:c7beea49fc91 | 18 | |
intrinseca | 0:c7beea49fc91 | 19 | /* Lightweight input stream. |
intrinseca | 0:c7beea49fc91 | 20 | * You can provide a callback function for reading or use |
intrinseca | 0:c7beea49fc91 | 21 | * pb_istream_from_buffer. |
intrinseca | 0:c7beea49fc91 | 22 | * |
intrinseca | 0:c7beea49fc91 | 23 | * Rules for callback: |
intrinseca | 0:c7beea49fc91 | 24 | * 1) Return false on IO errors. This will cause decoding to abort. |
intrinseca | 0:c7beea49fc91 | 25 | * |
intrinseca | 0:c7beea49fc91 | 26 | * 2) You can use state to store your own data (e.g. buffer pointer), |
intrinseca | 0:c7beea49fc91 | 27 | * and rely on pb_read to verify that no-body reads past bytes_left. |
intrinseca | 0:c7beea49fc91 | 28 | * |
intrinseca | 0:c7beea49fc91 | 29 | * 3) Your callback may be used with substreams, in which case bytes_left |
intrinseca | 0:c7beea49fc91 | 30 | * is different than from the main stream. Don't use bytes_left to compute |
intrinseca | 0:c7beea49fc91 | 31 | * any pointers. |
intrinseca | 0:c7beea49fc91 | 32 | */ |
intrinseca | 0:c7beea49fc91 | 33 | struct _pb_istream_t |
intrinseca | 0:c7beea49fc91 | 34 | { |
intrinseca | 0:c7beea49fc91 | 35 | #ifdef PB_BUFFER_ONLY |
intrinseca | 0:c7beea49fc91 | 36 | /* Callback pointer is not used in buffer-only configuration. |
intrinseca | 0:c7beea49fc91 | 37 | * Having an int pointer here allows binary compatibility but |
intrinseca | 0:c7beea49fc91 | 38 | * gives an error if someone tries to assign callback function. |
intrinseca | 0:c7beea49fc91 | 39 | */ |
intrinseca | 0:c7beea49fc91 | 40 | int *callback; |
intrinseca | 0:c7beea49fc91 | 41 | #else |
intrinseca | 0:c7beea49fc91 | 42 | bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count); |
intrinseca | 0:c7beea49fc91 | 43 | #endif |
intrinseca | 0:c7beea49fc91 | 44 | |
intrinseca | 0:c7beea49fc91 | 45 | void *state; /* Free field for use by callback implementation */ |
intrinseca | 0:c7beea49fc91 | 46 | size_t bytes_left; |
intrinseca | 0:c7beea49fc91 | 47 | |
intrinseca | 0:c7beea49fc91 | 48 | #ifndef PB_NO_ERRMSG |
intrinseca | 0:c7beea49fc91 | 49 | const char *errmsg; |
intrinseca | 0:c7beea49fc91 | 50 | #endif |
intrinseca | 0:c7beea49fc91 | 51 | }; |
intrinseca | 0:c7beea49fc91 | 52 | |
intrinseca | 0:c7beea49fc91 | 53 | pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize); |
intrinseca | 0:c7beea49fc91 | 54 | bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count); |
intrinseca | 0:c7beea49fc91 | 55 | |
intrinseca | 0:c7beea49fc91 | 56 | /* Decode from stream to destination struct. |
intrinseca | 0:c7beea49fc91 | 57 | * Returns true on success, false on any failure. |
intrinseca | 0:c7beea49fc91 | 58 | * The actual struct pointed to by dest must match the description in fields. |
intrinseca | 0:c7beea49fc91 | 59 | */ |
intrinseca | 0:c7beea49fc91 | 60 | bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
intrinseca | 0:c7beea49fc91 | 61 | |
intrinseca | 0:c7beea49fc91 | 62 | /* Same as pb_decode, except does not initialize the destination structure |
intrinseca | 0:c7beea49fc91 | 63 | * to default values. This is slightly faster if you need no default values |
intrinseca | 0:c7beea49fc91 | 64 | * and just do memset(struct, 0, sizeof(struct)) yourself. |
intrinseca | 0:c7beea49fc91 | 65 | */ |
intrinseca | 0:c7beea49fc91 | 66 | bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
intrinseca | 0:c7beea49fc91 | 67 | |
intrinseca | 0:c7beea49fc91 | 68 | /* --- Helper functions --- |
intrinseca | 0:c7beea49fc91 | 69 | * You may want to use these from your caller or callbacks. |
intrinseca | 0:c7beea49fc91 | 70 | */ |
intrinseca | 0:c7beea49fc91 | 71 | |
intrinseca | 0:c7beea49fc91 | 72 | /* Decode the tag for the next field in the stream. Gives the wire type and |
intrinseca | 0:c7beea49fc91 | 73 | * field tag. At end of the message, returns false and sets eof to true. */ |
intrinseca | 0:c7beea49fc91 | 74 | bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); |
intrinseca | 0:c7beea49fc91 | 75 | |
intrinseca | 0:c7beea49fc91 | 76 | /* Skip the field payload data, given the wire type. */ |
intrinseca | 0:c7beea49fc91 | 77 | bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); |
intrinseca | 0:c7beea49fc91 | 78 | |
intrinseca | 0:c7beea49fc91 | 79 | /* Decode an integer in the varint format. This works for bool, enum, int32, |
intrinseca | 0:c7beea49fc91 | 80 | * int64, uint32 and uint64 field types. */ |
intrinseca | 0:c7beea49fc91 | 81 | bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); |
intrinseca | 0:c7beea49fc91 | 82 | |
intrinseca | 0:c7beea49fc91 | 83 | /* Decode an integer in the zig-zagged svarint format. This works for sint32 |
intrinseca | 0:c7beea49fc91 | 84 | * and sint64. */ |
intrinseca | 0:c7beea49fc91 | 85 | bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); |
intrinseca | 0:c7beea49fc91 | 86 | |
intrinseca | 0:c7beea49fc91 | 87 | /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to |
intrinseca | 0:c7beea49fc91 | 88 | * a 4-byte wide C variable. */ |
intrinseca | 0:c7beea49fc91 | 89 | bool pb_decode_fixed32(pb_istream_t *stream, void *dest); |
intrinseca | 0:c7beea49fc91 | 90 | |
intrinseca | 0:c7beea49fc91 | 91 | /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to |
intrinseca | 0:c7beea49fc91 | 92 | * a 8-byte wide C variable. */ |
intrinseca | 0:c7beea49fc91 | 93 | bool pb_decode_fixed64(pb_istream_t *stream, void *dest); |
intrinseca | 0:c7beea49fc91 | 94 | |
intrinseca | 0:c7beea49fc91 | 95 | /* Make a limited-length substream for reading a PB_WT_STRING field. */ |
intrinseca | 0:c7beea49fc91 | 96 | bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
intrinseca | 0:c7beea49fc91 | 97 | void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
intrinseca | 0:c7beea49fc91 | 98 | |
intrinseca | 0:c7beea49fc91 | 99 | /* --- Internal functions --- |
intrinseca | 0:c7beea49fc91 | 100 | * These functions are not terribly useful for the average library user, but |
intrinseca | 0:c7beea49fc91 | 101 | * are exported to make the unit testing and extending nanopb easier. |
intrinseca | 0:c7beea49fc91 | 102 | */ |
intrinseca | 0:c7beea49fc91 | 103 | |
intrinseca | 0:c7beea49fc91 | 104 | #ifdef NANOPB_INTERNALS |
intrinseca | 0:c7beea49fc91 | 105 | bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 106 | bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 107 | bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 108 | bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 109 | |
intrinseca | 0:c7beea49fc91 | 110 | bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 111 | bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 112 | bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest); |
intrinseca | 0:c7beea49fc91 | 113 | |
intrinseca | 0:c7beea49fc91 | 114 | bool pb_skip_varint(pb_istream_t *stream); |
intrinseca | 0:c7beea49fc91 | 115 | bool pb_skip_string(pb_istream_t *stream); |
intrinseca | 0:c7beea49fc91 | 116 | #endif |
intrinseca | 0:c7beea49fc91 | 117 | |
intrinseca | 0:c7beea49fc91 | 118 | #ifdef __cplusplus |
intrinseca | 0:c7beea49fc91 | 119 | } /* extern "C" */ |
intrinseca | 0:c7beea49fc91 | 120 | #endif |
intrinseca | 0:c7beea49fc91 | 121 | |
intrinseca | 0:c7beea49fc91 | 122 | #endif |