1 /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
2  * The main function is pb_encode. You also need an output stream, and the
3  * field descriptions created by nanopb_generator.py.
4  */
5 
6 #ifndef PB_ENCODE_H_INCLUDED
7 #define PB_ENCODE_H_INCLUDED
8 
9 #include "pb.h"
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /* Structure for defining custom output streams. You will need to provide
16  * a callback function to write the bytes to your storage, which can be
17  * for example a file or a network socket.
18  *
19  * The callback must conform to these rules:
20  *
21  * 1) Return false on IO errors. This will cause encoding to abort.
22  * 2) You can use state to store your own data (e.g. buffer pointer).
23  * 3) pb_write will update bytes_written after your callback runs.
24  * 4) Substreams will modify max_size and bytes_written. Don't use them
25  *    to calculate any pointers.
26  */
27 struct pb_ostream_s
28 {
29 #ifdef PB_BUFFER_ONLY
30     /* Callback pointer is not used in buffer-only configuration.
31      * Having an int pointer here allows binary compatibility but
32      * gives an error if someone tries to assign callback function.
33      * Also, NULL pointer marks a 'sizing stream' that does not
34      * write anything.
35      */
36     const int *callback;
37 #else
38     bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
39 #endif
40 
41     /* state is a free field for use of the callback function defined above.
42      * Note that when pb_ostream_from_buffer() is used, it reserves this field
43      * for its own use.
44      */
45     void *state;
46 
47     /* Limit number of output bytes written. Can be set to SIZE_MAX. */
48     size_t max_size;
49 
50     /* Number of bytes written so far. */
51     size_t bytes_written;
52 
53 #ifndef PB_NO_ERRMSG
54     /* Pointer to constant (ROM) string when decoding function returns error */
55     const char *errmsg;
56 #endif
57 };
58 
59 /***************************
60  * Main encoding functions *
61  ***************************/
62 
63 /* Encode a single protocol buffers message from C structure into a stream.
64  * Returns true on success, false on any failure.
65  * The actual struct pointed to by src_struct must match the description in fields.
66  * All required fields in the struct are assumed to have been filled in.
67  *
68  * Example usage:
69  *    MyMessage msg = {};
70  *    uint8_t buffer[64];
71  *    pb_ostream_t stream;
72  *
73  *    msg.field1 = 42;
74  *    stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
75  *    pb_encode(&stream, MyMessage_fields, &msg);
76  */
77 bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct);
78 
79 /* Extended version of pb_encode, with several options to control the
80  * encoding process:
81  *
82  * PB_ENCODE_DELIMITED:      Prepend the length of message as a varint.
83  *                           Corresponds to writeDelimitedTo() in Google's
84  *                           protobuf API.
85  *
86  * PB_ENCODE_NULLTERMINATED: Append a null byte to the message for termination.
87  *                           NOTE: This behaviour is not supported in most other
88  *                           protobuf implementations, so PB_ENCODE_DELIMITED
89  *                           is a better option for compatibility.
90  */
91 #define PB_ENCODE_DELIMITED       0x02U
92 #define PB_ENCODE_NULLTERMINATED  0x04U
93 bool pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags);
94 
95 /* Defines for backwards compatibility with code written before nanopb-0.4.0 */
96 #define pb_encode_delimited(s,f,d) pb_encode_ex(s,f,d, PB_ENCODE_DELIMITED)
97 #define pb_encode_nullterminated(s,f,d) pb_encode_ex(s,f,d, PB_ENCODE_NULLTERMINATED)
98 
99 /* Encode the message to get the size of the encoded data, but do not store
100  * the data. */
101 bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct);
102 
103 /**************************************
104  * Functions for manipulating streams *
105  **************************************/
106 
107 /* Create an output stream for writing into a memory buffer.
108  * The number of bytes written can be found in stream.bytes_written after
109  * encoding the message.
110  *
111  * Alternatively, you can use a custom stream that writes directly to e.g.
112  * a file or a network socket.
113  */
114 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize);
115 
116 /* Pseudo-stream for measuring the size of a message without actually storing
117  * the encoded data.
118  *
119  * Example usage:
120  *    MyMessage msg = {};
121  *    pb_ostream_t stream = PB_OSTREAM_SIZING;
122  *    pb_encode(&stream, MyMessage_fields, &msg);
123  *    printf("Message size is %d\n", stream.bytes_written);
124  */
125 #ifndef PB_NO_ERRMSG
126 #define PB_OSTREAM_SIZING {0,0,0,0,0}
127 #else
128 #define PB_OSTREAM_SIZING {0,0,0,0}
129 #endif
130 
131 /* Function to write into a pb_ostream_t stream. You can use this if you need
132  * to append or prepend some custom headers to the message.
133  */
134 bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
135 
136 
137 /************************************************
138  * Helper functions for writing field callbacks *
139  ************************************************/
140 
141 /* Encode field header based on type and field number defined in the field
142  * structure. Call this from the callback before writing out field contents. */
143 bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field);
144 
145 /* Encode field header by manually specifying wire type. You need to use this
146  * if you want to write out packed arrays from a callback field. */
147 bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
148 
149 /* Encode an integer in the varint format.
150  * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
151 #ifndef PB_WITHOUT_64BIT
152 bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
153 #else
154 bool pb_encode_varint(pb_ostream_t *stream, uint32_t value);
155 #endif
156 
157 /* Encode an integer in the zig-zagged svarint format.
158  * This works for sint32 and sint64. */
159 #ifndef PB_WITHOUT_64BIT
160 bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
161 #else
162 bool pb_encode_svarint(pb_ostream_t *stream, int32_t value);
163 #endif
164 
165 /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
166 bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size);
167 
168 /* Encode a fixed32, sfixed32 or float value.
169  * You need to pass a pointer to a 4-byte wide C variable. */
170 bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
171 
172 #ifndef PB_WITHOUT_64BIT
173 /* Encode a fixed64, sfixed64 or double value.
174  * You need to pass a pointer to a 8-byte wide C variable. */
175 bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
176 #endif
177 
178 #ifdef PB_CONVERT_DOUBLE_FLOAT
179 /* Encode a float value so that it appears like a double in the encoded
180  * message. */
181 bool pb_encode_float_as_double(pb_ostream_t *stream, float value);
182 #endif
183 
184 /* Encode a submessage field.
185  * You need to pass the pb_field_t array and pointer to struct, just like
186  * with pb_encode(). This internally encodes the submessage twice, first to
187  * calculate message size and then to actually write it out.
188  */
189 bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct);
190 
191 #ifdef __cplusplus
192 } /* extern "C" */
193 #endif
194 
195 #endif
196