Clean up the comments in pb_encode.h and pb_decode.h
[apps/low-level-can-service.git] / pb_decode.h
1 /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c.
2  * The main function is pb_decode. You also need an input stream, and the
3  * field descriptions created by nanopb_generator.py.
4  */
5
6 #ifndef _PB_DECODE_H_
7 #define _PB_DECODE_H_
8
9 #include "pb.h"
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 /***************************
16  * Main decoding functions *
17  ***************************/
18  
19 /* Decode a single protocol buffers message from input stream into a C structure.
20  * Returns true on success, false on any failure.
21  * The actual struct pointed to by dest must match the description in fields.
22  * Callback fields of the destination structure must be initialized by caller.
23  * All other fields will be initialized by this function.
24  *
25  * Example usage:
26  *    MyMessage msg = {};
27  *    uint8_t buffer[64];
28  *    pb_istream_t stream;
29  *    
30  *    // ... read some data into buffer ...
31  *
32  *    stream = pb_istream_from_buffer(buffer, count);
33  *    pb_decode(&stream, MyMessage_fields, &msg);
34  */
35 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
36
37 /* Same as pb_decode, except does not initialize the destination structure
38  * to default values. This is slightly faster if you need no default values
39  * and just do memset(struct, 0, sizeof(struct)) yourself.
40  *
41  * This can also be used for 'merging' two messages, i.e. update only the
42  * fields that exist in the new message.
43  */
44 bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
45
46
47 /**************************************
48  * Functions for manipulating streams *
49  **************************************/
50
51 /* Create an input stream for reading from a memory buffer.
52  *
53  * Alternatively, you can use a custom stream that reads directly from e.g.
54  * a file or a network socket.
55  */
56 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize);
57
58 /* Function to read from a pb_istream_t. You can use this if you need to
59  * read some custom header data, or to read data in field callbacks.
60  */
61 bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count);
62
63 /* Structure for defining custom input streams. You will need to provide
64  * a callback function to read the bytes from your storage, which can be
65  * for example a file or a network socket.
66  * 
67  * The callback must conform to these rules:
68  *
69  * 1) Return false on IO errors. This will cause decoding to abort.
70  * 2) You can use state to store your own data (e.g. buffer pointer),
71  *    and rely on pb_read to verify that no-body reads past bytes_left.
72  * 3) Your callback may be used with substreams, in which case bytes_left
73  *    is different than from the main stream. Don't use bytes_left to compute
74  *    any pointers.
75  */
76 struct _pb_istream_t
77 {
78 #ifdef PB_BUFFER_ONLY
79     /* Callback pointer is not used in buffer-only configuration.
80      * Having an int pointer here allows binary compatibility but
81      * gives an error if someone tries to assign callback function.
82      */
83     int *callback;
84 #else
85     bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count);
86 #endif
87
88     void *state; /* Free field for use by callback implementation */
89     size_t bytes_left;
90     
91 #ifndef PB_NO_ERRMSG
92     const char *errmsg;
93 #endif
94 };
95
96
97 /************************************************
98  * Helper functions for writing field callbacks *
99  ************************************************/
100
101 /* Decode the tag for the next field in the stream. Gives the wire type and
102  * field tag. At end of the message, returns false and sets eof to true. */
103 bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
104
105 /* Skip the field payload data, given the wire type. */
106 bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
107
108 /* Decode an integer in the varint format. This works for bool, enum, int32,
109  * int64, uint32 and uint64 field types. */
110 bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
111
112 /* Decode an integer in the zig-zagged svarint format. This works for sint32
113  * and sint64. */
114 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
115
116 /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
117  * a 4-byte wide C variable. */
118 bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
119
120 /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
121  * a 8-byte wide C variable. */
122 bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
123
124 /* Make a limited-length substream for reading a PB_WT_STRING field. */
125 bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
126 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
127
128
129 /*******************************
130  * Internal / legacy functions *
131  *******************************/
132
133 #ifdef NANOPB_INTERNALS
134 bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest);
135 bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest);
136 bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
137 bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest);
138
139 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
140 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
141 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest);
142
143 bool pb_skip_varint(pb_istream_t *stream);
144 bool pb_skip_string(pb_istream_t *stream);
145 #endif
146
147 #ifdef __cplusplus
148 } /* extern "C" */
149 #endif
150
151 #endif