···11+Copyright (c) 2011 Petteri Aimonen <jpa at nanopb.mail.kapsi.fi>
22+33+This software is provided 'as-is', without any express or
44+implied warranty. In no event will the authors be held liable
55+for any damages arising from the use of this software.
66+77+Permission is granted to anyone to use this software for any
88+purpose, including commercial applications, and to alter it and
99+redistribute it freely, subject to the following restrictions:
1010+1111+1. The origin of this software must not be misrepresented; you
1212+ must not claim that you wrote the original software. If you use
1313+ this software in a product, an acknowledgment in the product
1414+ documentation would be appreciated but is not required.
1515+1616+2. Altered source versions must be plainly marked as such, and
1717+ must not be misrepresented as being the original software.
1818+1919+3. This notice may not be removed or altered from any source
2020+ distribution.
+911
src/external/nanopb/pb.h
···11+/* Common parts of the nanopb library. Most of these are quite low-level
22+ * stuff. For the high-level interface, see pb_encode.h and pb_decode.h.
33+ */
44+55+#ifndef PB_H_INCLUDED
66+#define PB_H_INCLUDED
77+88+/*****************************************************************
99+ * Nanopb compilation time options. You can change these here by *
1010+ * uncommenting the lines, or on the compiler command line. *
1111+ *****************************************************************/
1212+1313+/* Enable support for dynamically allocated fields */
1414+/* #define PB_ENABLE_MALLOC 1 */
1515+1616+/* Define this if your CPU / compiler combination does not support
1717+ * unaligned memory access to packed structures. Note that packed
1818+ * structures are only used when requested in .proto options. */
1919+/* #define PB_NO_PACKED_STRUCTS 1 */
2020+2121+/* Increase the number of required fields that are tracked.
2222+ * A compiler warning will tell if you need this. */
2323+/* #define PB_MAX_REQUIRED_FIELDS 256 */
2424+2525+/* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
2626+/* #define PB_FIELD_32BIT 1 */
2727+2828+/* Disable support for error messages in order to save some code space. */
2929+/* #define PB_NO_ERRMSG 1 */
3030+3131+/* Disable support for custom streams (support only memory buffers). */
3232+/* #define PB_BUFFER_ONLY 1 */
3333+3434+/* Disable support for 64-bit datatypes, for compilers without int64_t
3535+ or to save some code space. */
3636+/* #define PB_WITHOUT_64BIT 1 */
3737+3838+/* Don't encode scalar arrays as packed. This is only to be used when
3939+ * the decoder on the receiving side cannot process packed scalar arrays.
4040+ * Such example is older protobuf.js. */
4141+/* #define PB_ENCODE_ARRAYS_UNPACKED 1 */
4242+4343+/* Enable conversion of doubles to floats for platforms that do not
4444+ * support 64-bit doubles. Most commonly AVR. */
4545+/* #define PB_CONVERT_DOUBLE_FLOAT 1 */
4646+4747+/* Check whether incoming strings are valid UTF-8 sequences. Slows down
4848+ * the string processing slightly and slightly increases code size. */
4949+/* #define PB_VALIDATE_UTF8 1 */
5050+5151+/* This can be defined if the platform is little-endian and has 8-bit bytes.
5252+ * Normally it is automatically detected based on __BYTE_ORDER__ macro. */
5353+/* #define PB_LITTLE_ENDIAN_8BIT 1 */
5454+5555+/* Configure static assert mechanism. Instead of changing these, set your
5656+ * compiler to C11 standard mode if possible. */
5757+/* #define PB_C99_STATIC_ASSERT 1 */
5858+/* #define PB_NO_STATIC_ASSERT 1 */
5959+6060+/******************************************************************
6161+ * You usually don't need to change anything below this line. *
6262+ * Feel free to look around and use the defined macros, though. *
6363+ ******************************************************************/
6464+6565+6666+/* Version of the nanopb library. Just in case you want to check it in
6767+ * your own program. */
6868+#define NANOPB_VERSION "nanopb-0.4.7-dev"
6969+7070+/* Include all the system headers needed by nanopb. You will need the
7171+ * definitions of the following:
7272+ * - strlen, memcpy, memset functions
7373+ * - [u]int_least8_t, uint_fast8_t, [u]int_least16_t, [u]int32_t, [u]int64_t
7474+ * - size_t
7575+ * - bool
7676+ *
7777+ * If you don't have the standard header files, you can instead provide
7878+ * a custom header that defines or includes all this. In that case,
7979+ * define PB_SYSTEM_HEADER to the path of this file.
8080+ */
8181+#ifdef PB_SYSTEM_HEADER
8282+#include PB_SYSTEM_HEADER
8383+#else
8484+#include <stdint.h>
8585+#include <stddef.h>
8686+#include <stdbool.h>
8787+#include <string.h>
8888+#include <limits.h>
8989+9090+#ifdef PB_ENABLE_MALLOC
9191+#include <stdlib.h>
9292+#endif
9393+#endif
9494+9595+#ifdef __cplusplus
9696+extern "C" {
9797+#endif
9898+9999+/* Macro for defining packed structures (compiler dependent).
100100+ * This just reduces memory requirements, but is not required.
101101+ */
102102+#if defined(PB_NO_PACKED_STRUCTS)
103103+ /* Disable struct packing */
104104+# define PB_PACKED_STRUCT_START
105105+# define PB_PACKED_STRUCT_END
106106+# define pb_packed
107107+#elif defined(__GNUC__) || defined(__clang__)
108108+ /* For GCC and clang */
109109+# define PB_PACKED_STRUCT_START
110110+# define PB_PACKED_STRUCT_END
111111+# define pb_packed __attribute__((packed))
112112+#elif defined(__ICCARM__) || defined(__CC_ARM)
113113+ /* For IAR ARM and Keil MDK-ARM compilers */
114114+# define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
115115+# define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
116116+# define pb_packed
117117+#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
118118+ /* For Microsoft Visual C++ */
119119+# define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
120120+# define PB_PACKED_STRUCT_END __pragma(pack(pop))
121121+# define pb_packed
122122+#else
123123+ /* Unknown compiler */
124124+# define PB_PACKED_STRUCT_START
125125+# define PB_PACKED_STRUCT_END
126126+# define pb_packed
127127+#endif
128128+129129+/* Detect endianness */
130130+#ifndef PB_LITTLE_ENDIAN_8BIT
131131+#if ((defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
132132+ (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
133133+ defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
134134+ defined(__THUMBEL__) || defined(__AARCH64EL__) || defined(_MIPSEL) || \
135135+ defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM)) \
136136+ && CHAR_BIT == 8
137137+#define PB_LITTLE_ENDIAN_8BIT 1
138138+#endif
139139+#endif
140140+141141+/* Handly macro for suppressing unreferenced-parameter compiler warnings. */
142142+#ifndef PB_UNUSED
143143+#define PB_UNUSED(x) (void)(x)
144144+#endif
145145+146146+/* Harvard-architecture processors may need special attributes for storing
147147+ * field information in program memory. */
148148+#ifndef PB_PROGMEM
149149+#ifdef __AVR__
150150+#include <avr/pgmspace.h>
151151+#define PB_PROGMEM PROGMEM
152152+#define PB_PROGMEM_READU32(x) pgm_read_dword(&x)
153153+#else
154154+#define PB_PROGMEM
155155+#define PB_PROGMEM_READU32(x) (x)
156156+#endif
157157+#endif
158158+159159+/* Compile-time assertion, used for checking compatible compilation options.
160160+ * If this does not work properly on your compiler, use
161161+ * #define PB_NO_STATIC_ASSERT to disable it.
162162+ *
163163+ * But before doing that, check carefully the error message / place where it
164164+ * comes from to see if the error has a real cause. Unfortunately the error
165165+ * message is not always very clear to read, but you can see the reason better
166166+ * in the place where the PB_STATIC_ASSERT macro was called.
167167+ */
168168+#ifndef PB_NO_STATIC_ASSERT
169169+# ifndef PB_STATIC_ASSERT
170170+# if defined(__ICCARM__)
171171+ /* IAR has static_assert keyword but no _Static_assert */
172172+# define PB_STATIC_ASSERT(COND,MSG) static_assert(COND,#MSG);
173173+# elif defined(PB_C99_STATIC_ASSERT)
174174+ /* Classic negative-size-array static assert mechanism */
175175+# define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
176176+# define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
177177+# define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##_##LINE##_##COUNTER
178178+# elif defined(__cplusplus)
179179+ /* C++11 standard static_assert mechanism */
180180+# define PB_STATIC_ASSERT(COND,MSG) static_assert(COND,#MSG);
181181+# else
182182+ /* C11 standard _Static_assert mechanism */
183183+# define PB_STATIC_ASSERT(COND,MSG) _Static_assert(COND,#MSG);
184184+# endif
185185+# endif
186186+#else
187187+ /* Static asserts disabled by PB_NO_STATIC_ASSERT */
188188+# define PB_STATIC_ASSERT(COND,MSG)
189189+#endif
190190+191191+/* Test that PB_STATIC_ASSERT works
192192+ * If you get errors here, you may need to do one of these:
193193+ * - Enable C11 standard support in your compiler
194194+ * - Define PB_C99_STATIC_ASSERT to enable C99 standard support
195195+ * - Define PB_NO_STATIC_ASSERT to disable static asserts altogether
196196+ */
197197+PB_STATIC_ASSERT(1, STATIC_ASSERT_IS_NOT_WORKING)
198198+199199+/* Number of required fields to keep track of. */
200200+#ifndef PB_MAX_REQUIRED_FIELDS
201201+#define PB_MAX_REQUIRED_FIELDS 64
202202+#endif
203203+204204+#if PB_MAX_REQUIRED_FIELDS < 64
205205+#error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
206206+#endif
207207+208208+#ifdef PB_WITHOUT_64BIT
209209+#ifdef PB_CONVERT_DOUBLE_FLOAT
210210+/* Cannot use doubles without 64-bit types */
211211+#undef PB_CONVERT_DOUBLE_FLOAT
212212+#endif
213213+#endif
214214+215215+/* List of possible field types. These are used in the autogenerated code.
216216+ * Least-significant 4 bits tell the scalar type
217217+ * Most-significant 4 bits specify repeated/required/packed etc.
218218+ */
219219+220220+typedef uint_least8_t pb_type_t;
221221+222222+/**** Field data types ****/
223223+224224+/* Numeric types */
225225+#define PB_LTYPE_BOOL 0x00U /* bool */
226226+#define PB_LTYPE_VARINT 0x01U /* int32, int64, enum, bool */
227227+#define PB_LTYPE_UVARINT 0x02U /* uint32, uint64 */
228228+#define PB_LTYPE_SVARINT 0x03U /* sint32, sint64 */
229229+#define PB_LTYPE_FIXED32 0x04U /* fixed32, sfixed32, float */
230230+#define PB_LTYPE_FIXED64 0x05U /* fixed64, sfixed64, double */
231231+232232+/* Marker for last packable field type. */
233233+#define PB_LTYPE_LAST_PACKABLE 0x05U
234234+235235+/* Byte array with pre-allocated buffer.
236236+ * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
237237+#define PB_LTYPE_BYTES 0x06U
238238+239239+/* String with pre-allocated buffer.
240240+ * data_size is the maximum length. */
241241+#define PB_LTYPE_STRING 0x07U
242242+243243+/* Submessage
244244+ * submsg_fields is pointer to field descriptions */
245245+#define PB_LTYPE_SUBMESSAGE 0x08U
246246+247247+/* Submessage with pre-decoding callback
248248+ * The pre-decoding callback is stored as pb_callback_t right before pSize.
249249+ * submsg_fields is pointer to field descriptions */
250250+#define PB_LTYPE_SUBMSG_W_CB 0x09U
251251+252252+/* Extension pseudo-field
253253+ * The field contains a pointer to pb_extension_t */
254254+#define PB_LTYPE_EXTENSION 0x0AU
255255+256256+/* Byte array with inline, pre-allocated byffer.
257257+ * data_size is the length of the inline, allocated buffer.
258258+ * This differs from PB_LTYPE_BYTES by defining the element as
259259+ * pb_byte_t[data_size] rather than pb_bytes_array_t. */
260260+#define PB_LTYPE_FIXED_LENGTH_BYTES 0x0BU
261261+262262+/* Number of declared LTYPES */
263263+#define PB_LTYPES_COUNT 0x0CU
264264+#define PB_LTYPE_MASK 0x0FU
265265+266266+/**** Field repetition rules ****/
267267+268268+#define PB_HTYPE_REQUIRED 0x00U
269269+#define PB_HTYPE_OPTIONAL 0x10U
270270+#define PB_HTYPE_SINGULAR 0x10U
271271+#define PB_HTYPE_REPEATED 0x20U
272272+#define PB_HTYPE_FIXARRAY 0x20U
273273+#define PB_HTYPE_ONEOF 0x30U
274274+#define PB_HTYPE_MASK 0x30U
275275+276276+/**** Field allocation types ****/
277277+278278+#define PB_ATYPE_STATIC 0x00U
279279+#define PB_ATYPE_POINTER 0x80U
280280+#define PB_ATYPE_CALLBACK 0x40U
281281+#define PB_ATYPE_MASK 0xC0U
282282+283283+#define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
284284+#define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
285285+#define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
286286+#define PB_LTYPE_IS_SUBMSG(x) (PB_LTYPE(x) == PB_LTYPE_SUBMESSAGE || \
287287+ PB_LTYPE(x) == PB_LTYPE_SUBMSG_W_CB)
288288+289289+/* Data type used for storing sizes of struct fields
290290+ * and array counts.
291291+ */
292292+#if defined(PB_FIELD_32BIT)
293293+ typedef uint32_t pb_size_t;
294294+ typedef int32_t pb_ssize_t;
295295+#else
296296+ typedef uint_least16_t pb_size_t;
297297+ typedef int_least16_t pb_ssize_t;
298298+#endif
299299+#define PB_SIZE_MAX ((pb_size_t)-1)
300300+301301+/* Data type for storing encoded data and other byte streams.
302302+ * This typedef exists to support platforms where uint8_t does not exist.
303303+ * You can regard it as equivalent on uint8_t on other platforms.
304304+ */
305305+typedef uint_least8_t pb_byte_t;
306306+307307+/* Forward declaration of struct types */
308308+typedef struct pb_istream_s pb_istream_t;
309309+typedef struct pb_ostream_s pb_ostream_t;
310310+typedef struct pb_field_iter_s pb_field_iter_t;
311311+312312+/* This structure is used in auto-generated constants
313313+ * to specify struct fields.
314314+ */
315315+typedef struct pb_msgdesc_s pb_msgdesc_t;
316316+struct pb_msgdesc_s {
317317+ const uint32_t *field_info;
318318+ const pb_msgdesc_t * const * submsg_info;
319319+ const pb_byte_t *default_value;
320320+321321+ bool (*field_callback)(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field);
322322+323323+ pb_size_t field_count;
324324+ pb_size_t required_field_count;
325325+ pb_size_t largest_tag;
326326+};
327327+328328+/* Iterator for message descriptor */
329329+struct pb_field_iter_s {
330330+ const pb_msgdesc_t *descriptor; /* Pointer to message descriptor constant */
331331+ void *message; /* Pointer to start of the structure */
332332+333333+ pb_size_t index; /* Index of the field */
334334+ pb_size_t field_info_index; /* Index to descriptor->field_info array */
335335+ pb_size_t required_field_index; /* Index that counts only the required fields */
336336+ pb_size_t submessage_index; /* Index that counts only submessages */
337337+338338+ pb_size_t tag; /* Tag of current field */
339339+ pb_size_t data_size; /* sizeof() of a single item */
340340+ pb_size_t array_size; /* Number of array entries */
341341+ pb_type_t type; /* Type of current field */
342342+343343+ void *pField; /* Pointer to current field in struct */
344344+ void *pData; /* Pointer to current data contents. Different than pField for arrays and pointers. */
345345+ void *pSize; /* Pointer to count/has field */
346346+347347+ const pb_msgdesc_t *submsg_desc; /* For submessage fields, pointer to field descriptor for the submessage. */
348348+};
349349+350350+/* For compatibility with legacy code */
351351+typedef pb_field_iter_t pb_field_t;
352352+353353+/* Make sure that the standard integer types are of the expected sizes.
354354+ * Otherwise fixed32/fixed64 fields can break.
355355+ *
356356+ * If you get errors here, it probably means that your stdint.h is not
357357+ * correct for your platform.
358358+ */
359359+#ifndef PB_WITHOUT_64BIT
360360+PB_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), INT64_T_WRONG_SIZE)
361361+PB_STATIC_ASSERT(sizeof(uint64_t) == 2 * sizeof(uint32_t), UINT64_T_WRONG_SIZE)
362362+#endif
363363+364364+/* This structure is used for 'bytes' arrays.
365365+ * It has the number of bytes in the beginning, and after that an array.
366366+ * Note that actual structs used will have a different length of bytes array.
367367+ */
368368+#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }
369369+#define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes))
370370+371371+struct pb_bytes_array_s {
372372+ pb_size_t size;
373373+ pb_byte_t bytes[1];
374374+};
375375+typedef struct pb_bytes_array_s pb_bytes_array_t;
376376+377377+/* This structure is used for giving the callback function.
378378+ * It is stored in the message structure and filled in by the method that
379379+ * calls pb_decode.
380380+ *
381381+ * The decoding callback will be given a limited-length stream
382382+ * If the wire type was string, the length is the length of the string.
383383+ * If the wire type was a varint/fixed32/fixed64, the length is the length
384384+ * of the actual value.
385385+ * The function may be called multiple times (especially for repeated types,
386386+ * but also otherwise if the message happens to contain the field multiple
387387+ * times.)
388388+ *
389389+ * The encoding callback will receive the actual output stream.
390390+ * It should write all the data in one call, including the field tag and
391391+ * wire type. It can write multiple fields.
392392+ *
393393+ * The callback can be null if you want to skip a field.
394394+ */
395395+typedef struct pb_callback_s pb_callback_t;
396396+struct pb_callback_s {
397397+ /* Callback functions receive a pointer to the arg field.
398398+ * You can access the value of the field as *arg, and modify it if needed.
399399+ */
400400+ union {
401401+ bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
402402+ bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
403403+ } funcs;
404404+405405+ /* Free arg for use by callback */
406406+ void *arg;
407407+};
408408+409409+extern bool pb_default_field_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field);
410410+411411+/* Wire types. Library user needs these only in encoder callbacks. */
412412+typedef enum {
413413+ PB_WT_VARINT = 0,
414414+ PB_WT_64BIT = 1,
415415+ PB_WT_STRING = 2,
416416+ PB_WT_32BIT = 5,
417417+ PB_WT_PACKED = 255 /* PB_WT_PACKED is internal marker for packed arrays. */
418418+} pb_wire_type_t;
419419+420420+/* Structure for defining the handling of unknown/extension fields.
421421+ * Usually the pb_extension_type_t structure is automatically generated,
422422+ * while the pb_extension_t structure is created by the user. However,
423423+ * if you want to catch all unknown fields, you can also create a custom
424424+ * pb_extension_type_t with your own callback.
425425+ */
426426+typedef struct pb_extension_type_s pb_extension_type_t;
427427+typedef struct pb_extension_s pb_extension_t;
428428+struct pb_extension_type_s {
429429+ /* Called for each unknown field in the message.
430430+ * If you handle the field, read off all of its data and return true.
431431+ * If you do not handle the field, do not read anything and return true.
432432+ * If you run into an error, return false.
433433+ * Set to NULL for default handler.
434434+ */
435435+ bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
436436+ uint32_t tag, pb_wire_type_t wire_type);
437437+438438+ /* Called once after all regular fields have been encoded.
439439+ * If you have something to write, do so and return true.
440440+ * If you do not have anything to write, just return true.
441441+ * If you run into an error, return false.
442442+ * Set to NULL for default handler.
443443+ */
444444+ bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
445445+446446+ /* Free field for use by the callback. */
447447+ const void *arg;
448448+};
449449+450450+struct pb_extension_s {
451451+ /* Type describing the extension field. Usually you'll initialize
452452+ * this to a pointer to the automatically generated structure. */
453453+ const pb_extension_type_t *type;
454454+455455+ /* Destination for the decoded data. This must match the datatype
456456+ * of the extension field. */
457457+ void *dest;
458458+459459+ /* Pointer to the next extension handler, or NULL.
460460+ * If this extension does not match a field, the next handler is
461461+ * automatically called. */
462462+ pb_extension_t *next;
463463+464464+ /* The decoder sets this to true if the extension was found.
465465+ * Ignored for encoding. */
466466+ bool found;
467467+};
468468+469469+#define pb_extension_init_zero {NULL,NULL,NULL,false}
470470+471471+/* Memory allocation functions to use. You can define pb_realloc and
472472+ * pb_free to custom functions if you want. */
473473+#ifdef PB_ENABLE_MALLOC
474474+# ifndef pb_realloc
475475+# define pb_realloc(ptr, size) realloc(ptr, size)
476476+# endif
477477+# ifndef pb_free
478478+# define pb_free(ptr) free(ptr)
479479+# endif
480480+#endif
481481+482482+/* This is used to inform about need to regenerate .pb.h/.pb.c files. */
483483+#define PB_PROTO_HEADER_VERSION 40
484484+485485+/* These macros are used to declare pb_field_t's in the constant array. */
486486+/* Size of a structure member, in bytes. */
487487+#define pb_membersize(st, m) (sizeof ((st*)0)->m)
488488+/* Number of entries in an array. */
489489+#define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
490490+/* Delta from start of one member to the start of another member. */
491491+#define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
492492+493493+/* Force expansion of macro value */
494494+#define PB_EXPAND(x) x
495495+496496+/* Binding of a message field set into a specific structure */
497497+#define PB_BIND(msgname, structname, width) \
498498+ const uint32_t structname ## _field_info[] PB_PROGMEM = \
499499+ { \
500500+ msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ ## width, structname) \
501501+ 0 \
502502+ }; \
503503+ const pb_msgdesc_t* const structname ## _submsg_info[] = \
504504+ { \
505505+ msgname ## _FIELDLIST(PB_GEN_SUBMSG_INFO, structname) \
506506+ NULL \
507507+ }; \
508508+ const pb_msgdesc_t structname ## _msg = \
509509+ { \
510510+ structname ## _field_info, \
511511+ structname ## _submsg_info, \
512512+ msgname ## _DEFAULT, \
513513+ msgname ## _CALLBACK, \
514514+ 0 msgname ## _FIELDLIST(PB_GEN_FIELD_COUNT, structname), \
515515+ 0 msgname ## _FIELDLIST(PB_GEN_REQ_FIELD_COUNT, structname), \
516516+ 0 msgname ## _FIELDLIST(PB_GEN_LARGEST_TAG, structname), \
517517+ }; \
518518+ msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ASSERT_ ## width, structname)
519519+520520+#define PB_GEN_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) +1
521521+#define PB_GEN_REQ_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) \
522522+ + (PB_HTYPE_ ## htype == PB_HTYPE_REQUIRED)
523523+#define PB_GEN_LARGEST_TAG(structname, atype, htype, ltype, fieldname, tag) \
524524+ * 0 + tag
525525+526526+/* X-macro for generating the entries in struct_field_info[] array. */
527527+#define PB_GEN_FIELD_INFO_1(structname, atype, htype, ltype, fieldname, tag) \
528528+ PB_FIELDINFO_1(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
529529+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
530530+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
531531+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
532532+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
533533+534534+#define PB_GEN_FIELD_INFO_2(structname, atype, htype, ltype, fieldname, tag) \
535535+ PB_FIELDINFO_2(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
536536+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
537537+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
538538+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
539539+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
540540+541541+#define PB_GEN_FIELD_INFO_4(structname, atype, htype, ltype, fieldname, tag) \
542542+ PB_FIELDINFO_4(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
543543+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
544544+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
545545+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
546546+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
547547+548548+#define PB_GEN_FIELD_INFO_8(structname, atype, htype, ltype, fieldname, tag) \
549549+ PB_FIELDINFO_8(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
550550+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
551551+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
552552+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
553553+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
554554+555555+#define PB_GEN_FIELD_INFO_AUTO(structname, atype, htype, ltype, fieldname, tag) \
556556+ PB_FIELDINFO_AUTO2(PB_FIELDINFO_WIDTH_AUTO(_PB_ATYPE_ ## atype, _PB_HTYPE_ ## htype, _PB_LTYPE_ ## ltype), \
557557+ tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
558558+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
559559+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
560560+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
561561+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
562562+563563+#define PB_FIELDINFO_AUTO2(width, tag, type, data_offset, data_size, size_offset, array_size) \
564564+ PB_FIELDINFO_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size)
565565+566566+#define PB_FIELDINFO_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size) \
567567+ PB_FIELDINFO_ ## width(tag, type, data_offset, data_size, size_offset, array_size)
568568+569569+/* X-macro for generating asserts that entries fit in struct_field_info[] array.
570570+ * The structure of macros here must match the structure above in PB_GEN_FIELD_INFO_x(),
571571+ * but it is not easily reused because of how macro substitutions work. */
572572+#define PB_GEN_FIELD_INFO_ASSERT_1(structname, atype, htype, ltype, fieldname, tag) \
573573+ PB_FIELDINFO_ASSERT_1(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
574574+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
575575+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
576576+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
577577+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
578578+579579+#define PB_GEN_FIELD_INFO_ASSERT_2(structname, atype, htype, ltype, fieldname, tag) \
580580+ PB_FIELDINFO_ASSERT_2(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
581581+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
582582+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
583583+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
584584+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
585585+586586+#define PB_GEN_FIELD_INFO_ASSERT_4(structname, atype, htype, ltype, fieldname, tag) \
587587+ PB_FIELDINFO_ASSERT_4(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
588588+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
589589+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
590590+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
591591+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
592592+593593+#define PB_GEN_FIELD_INFO_ASSERT_8(structname, atype, htype, ltype, fieldname, tag) \
594594+ PB_FIELDINFO_ASSERT_8(tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
595595+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
596596+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
597597+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
598598+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
599599+600600+#define PB_GEN_FIELD_INFO_ASSERT_AUTO(structname, atype, htype, ltype, fieldname, tag) \
601601+ PB_FIELDINFO_ASSERT_AUTO2(PB_FIELDINFO_WIDTH_AUTO(_PB_ATYPE_ ## atype, _PB_HTYPE_ ## htype, _PB_LTYPE_ ## ltype), \
602602+ tag, PB_ATYPE_ ## atype | PB_HTYPE_ ## htype | PB_LTYPE_MAP_ ## ltype, \
603603+ PB_DATA_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
604604+ PB_DATA_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
605605+ PB_SIZE_OFFSET_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname), \
606606+ PB_ARRAY_SIZE_ ## atype(_PB_HTYPE_ ## htype, structname, fieldname))
607607+608608+#define PB_FIELDINFO_ASSERT_AUTO2(width, tag, type, data_offset, data_size, size_offset, array_size) \
609609+ PB_FIELDINFO_ASSERT_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size)
610610+611611+#define PB_FIELDINFO_ASSERT_AUTO3(width, tag, type, data_offset, data_size, size_offset, array_size) \
612612+ PB_FIELDINFO_ASSERT_ ## width(tag, type, data_offset, data_size, size_offset, array_size)
613613+614614+#define PB_DATA_OFFSET_STATIC(htype, structname, fieldname) PB_DO ## htype(structname, fieldname)
615615+#define PB_DATA_OFFSET_POINTER(htype, structname, fieldname) PB_DO ## htype(structname, fieldname)
616616+#define PB_DATA_OFFSET_CALLBACK(htype, structname, fieldname) PB_DO ## htype(structname, fieldname)
617617+#define PB_DO_PB_HTYPE_REQUIRED(structname, fieldname) offsetof(structname, fieldname)
618618+#define PB_DO_PB_HTYPE_SINGULAR(structname, fieldname) offsetof(structname, fieldname)
619619+#define PB_DO_PB_HTYPE_ONEOF(structname, fieldname) offsetof(structname, PB_ONEOF_NAME(FULL, fieldname))
620620+#define PB_DO_PB_HTYPE_OPTIONAL(structname, fieldname) offsetof(structname, fieldname)
621621+#define PB_DO_PB_HTYPE_REPEATED(structname, fieldname) offsetof(structname, fieldname)
622622+#define PB_DO_PB_HTYPE_FIXARRAY(structname, fieldname) offsetof(structname, fieldname)
623623+624624+#define PB_SIZE_OFFSET_STATIC(htype, structname, fieldname) PB_SO ## htype(structname, fieldname)
625625+#define PB_SIZE_OFFSET_POINTER(htype, structname, fieldname) PB_SO_PTR ## htype(structname, fieldname)
626626+#define PB_SIZE_OFFSET_CALLBACK(htype, structname, fieldname) PB_SO_CB ## htype(structname, fieldname)
627627+#define PB_SO_PB_HTYPE_REQUIRED(structname, fieldname) 0
628628+#define PB_SO_PB_HTYPE_SINGULAR(structname, fieldname) 0
629629+#define PB_SO_PB_HTYPE_ONEOF(structname, fieldname) PB_SO_PB_HTYPE_ONEOF2(structname, PB_ONEOF_NAME(FULL, fieldname), PB_ONEOF_NAME(UNION, fieldname))
630630+#define PB_SO_PB_HTYPE_ONEOF2(structname, fullname, unionname) PB_SO_PB_HTYPE_ONEOF3(structname, fullname, unionname)
631631+#define PB_SO_PB_HTYPE_ONEOF3(structname, fullname, unionname) pb_delta(structname, fullname, which_ ## unionname)
632632+#define PB_SO_PB_HTYPE_OPTIONAL(structname, fieldname) pb_delta(structname, fieldname, has_ ## fieldname)
633633+#define PB_SO_PB_HTYPE_REPEATED(structname, fieldname) pb_delta(structname, fieldname, fieldname ## _count)
634634+#define PB_SO_PB_HTYPE_FIXARRAY(structname, fieldname) 0
635635+#define PB_SO_PTR_PB_HTYPE_REQUIRED(structname, fieldname) 0
636636+#define PB_SO_PTR_PB_HTYPE_SINGULAR(structname, fieldname) 0
637637+#define PB_SO_PTR_PB_HTYPE_ONEOF(structname, fieldname) PB_SO_PB_HTYPE_ONEOF(structname, fieldname)
638638+#define PB_SO_PTR_PB_HTYPE_OPTIONAL(structname, fieldname) 0
639639+#define PB_SO_PTR_PB_HTYPE_REPEATED(structname, fieldname) PB_SO_PB_HTYPE_REPEATED(structname, fieldname)
640640+#define PB_SO_PTR_PB_HTYPE_FIXARRAY(structname, fieldname) 0
641641+#define PB_SO_CB_PB_HTYPE_REQUIRED(structname, fieldname) 0
642642+#define PB_SO_CB_PB_HTYPE_SINGULAR(structname, fieldname) 0
643643+#define PB_SO_CB_PB_HTYPE_ONEOF(structname, fieldname) PB_SO_PB_HTYPE_ONEOF(structname, fieldname)
644644+#define PB_SO_CB_PB_HTYPE_OPTIONAL(structname, fieldname) 0
645645+#define PB_SO_CB_PB_HTYPE_REPEATED(structname, fieldname) 0
646646+#define PB_SO_CB_PB_HTYPE_FIXARRAY(structname, fieldname) 0
647647+648648+#define PB_ARRAY_SIZE_STATIC(htype, structname, fieldname) PB_AS ## htype(structname, fieldname)
649649+#define PB_ARRAY_SIZE_POINTER(htype, structname, fieldname) PB_AS_PTR ## htype(structname, fieldname)
650650+#define PB_ARRAY_SIZE_CALLBACK(htype, structname, fieldname) 1
651651+#define PB_AS_PB_HTYPE_REQUIRED(structname, fieldname) 1
652652+#define PB_AS_PB_HTYPE_SINGULAR(structname, fieldname) 1
653653+#define PB_AS_PB_HTYPE_OPTIONAL(structname, fieldname) 1
654654+#define PB_AS_PB_HTYPE_ONEOF(structname, fieldname) 1
655655+#define PB_AS_PB_HTYPE_REPEATED(structname, fieldname) pb_arraysize(structname, fieldname)
656656+#define PB_AS_PB_HTYPE_FIXARRAY(structname, fieldname) pb_arraysize(structname, fieldname)
657657+#define PB_AS_PTR_PB_HTYPE_REQUIRED(structname, fieldname) 1
658658+#define PB_AS_PTR_PB_HTYPE_SINGULAR(structname, fieldname) 1
659659+#define PB_AS_PTR_PB_HTYPE_OPTIONAL(structname, fieldname) 1
660660+#define PB_AS_PTR_PB_HTYPE_ONEOF(structname, fieldname) 1
661661+#define PB_AS_PTR_PB_HTYPE_REPEATED(structname, fieldname) 1
662662+#define PB_AS_PTR_PB_HTYPE_FIXARRAY(structname, fieldname) pb_arraysize(structname, fieldname[0])
663663+664664+#define PB_DATA_SIZE_STATIC(htype, structname, fieldname) PB_DS ## htype(structname, fieldname)
665665+#define PB_DATA_SIZE_POINTER(htype, structname, fieldname) PB_DS_PTR ## htype(structname, fieldname)
666666+#define PB_DATA_SIZE_CALLBACK(htype, structname, fieldname) PB_DS_CB ## htype(structname, fieldname)
667667+#define PB_DS_PB_HTYPE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname)
668668+#define PB_DS_PB_HTYPE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname)
669669+#define PB_DS_PB_HTYPE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname)
670670+#define PB_DS_PB_HTYPE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname))
671671+#define PB_DS_PB_HTYPE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname[0])
672672+#define PB_DS_PB_HTYPE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname[0])
673673+#define PB_DS_PTR_PB_HTYPE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname[0])
674674+#define PB_DS_PTR_PB_HTYPE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname[0])
675675+#define PB_DS_PTR_PB_HTYPE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname[0])
676676+#define PB_DS_PTR_PB_HTYPE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname)[0])
677677+#define PB_DS_PTR_PB_HTYPE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname[0])
678678+#define PB_DS_PTR_PB_HTYPE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname[0][0])
679679+#define PB_DS_CB_PB_HTYPE_REQUIRED(structname, fieldname) pb_membersize(structname, fieldname)
680680+#define PB_DS_CB_PB_HTYPE_SINGULAR(structname, fieldname) pb_membersize(structname, fieldname)
681681+#define PB_DS_CB_PB_HTYPE_OPTIONAL(structname, fieldname) pb_membersize(structname, fieldname)
682682+#define PB_DS_CB_PB_HTYPE_ONEOF(structname, fieldname) pb_membersize(structname, PB_ONEOF_NAME(FULL, fieldname))
683683+#define PB_DS_CB_PB_HTYPE_REPEATED(structname, fieldname) pb_membersize(structname, fieldname)
684684+#define PB_DS_CB_PB_HTYPE_FIXARRAY(structname, fieldname) pb_membersize(structname, fieldname)
685685+686686+#define PB_ONEOF_NAME(type, tuple) PB_EXPAND(PB_ONEOF_NAME_ ## type tuple)
687687+#define PB_ONEOF_NAME_UNION(unionname,membername,fullname) unionname
688688+#define PB_ONEOF_NAME_MEMBER(unionname,membername,fullname) membername
689689+#define PB_ONEOF_NAME_FULL(unionname,membername,fullname) fullname
690690+691691+#define PB_GEN_SUBMSG_INFO(structname, atype, htype, ltype, fieldname, tag) \
692692+ PB_SUBMSG_INFO_ ## htype(_PB_LTYPE_ ## ltype, structname, fieldname)
693693+694694+#define PB_SUBMSG_INFO_REQUIRED(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
695695+#define PB_SUBMSG_INFO_SINGULAR(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
696696+#define PB_SUBMSG_INFO_OPTIONAL(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
697697+#define PB_SUBMSG_INFO_ONEOF(ltype, structname, fieldname) PB_SUBMSG_INFO_ONEOF2(ltype, structname, PB_ONEOF_NAME(UNION, fieldname), PB_ONEOF_NAME(MEMBER, fieldname))
698698+#define PB_SUBMSG_INFO_ONEOF2(ltype, structname, unionname, membername) PB_SUBMSG_INFO_ONEOF3(ltype, structname, unionname, membername)
699699+#define PB_SUBMSG_INFO_ONEOF3(ltype, structname, unionname, membername) PB_SI ## ltype(structname ## _ ## unionname ## _ ## membername ## _MSGTYPE)
700700+#define PB_SUBMSG_INFO_REPEATED(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
701701+#define PB_SUBMSG_INFO_FIXARRAY(ltype, structname, fieldname) PB_SI ## ltype(structname ## _ ## fieldname ## _MSGTYPE)
702702+#define PB_SI_PB_LTYPE_BOOL(t)
703703+#define PB_SI_PB_LTYPE_BYTES(t)
704704+#define PB_SI_PB_LTYPE_DOUBLE(t)
705705+#define PB_SI_PB_LTYPE_ENUM(t)
706706+#define PB_SI_PB_LTYPE_UENUM(t)
707707+#define PB_SI_PB_LTYPE_FIXED32(t)
708708+#define PB_SI_PB_LTYPE_FIXED64(t)
709709+#define PB_SI_PB_LTYPE_FLOAT(t)
710710+#define PB_SI_PB_LTYPE_INT32(t)
711711+#define PB_SI_PB_LTYPE_INT64(t)
712712+#define PB_SI_PB_LTYPE_MESSAGE(t) PB_SUBMSG_DESCRIPTOR(t)
713713+#define PB_SI_PB_LTYPE_MSG_W_CB(t) PB_SUBMSG_DESCRIPTOR(t)
714714+#define PB_SI_PB_LTYPE_SFIXED32(t)
715715+#define PB_SI_PB_LTYPE_SFIXED64(t)
716716+#define PB_SI_PB_LTYPE_SINT32(t)
717717+#define PB_SI_PB_LTYPE_SINT64(t)
718718+#define PB_SI_PB_LTYPE_STRING(t)
719719+#define PB_SI_PB_LTYPE_UINT32(t)
720720+#define PB_SI_PB_LTYPE_UINT64(t)
721721+#define PB_SI_PB_LTYPE_EXTENSION(t)
722722+#define PB_SI_PB_LTYPE_FIXED_LENGTH_BYTES(t)
723723+#define PB_SUBMSG_DESCRIPTOR(t) &(t ## _msg),
724724+725725+/* The field descriptors use a variable width format, with width of either
726726+ * 1, 2, 4 or 8 of 32-bit words. The two lowest bytes of the first byte always
727727+ * encode the descriptor size, 6 lowest bits of field tag number, and 8 bits
728728+ * of the field type.
729729+ *
730730+ * Descriptor size is encoded as 0 = 1 word, 1 = 2 words, 2 = 4 words, 3 = 8 words.
731731+ *
732732+ * Formats, listed starting with the least significant bit of the first word.
733733+ * 1 word: [2-bit len] [6-bit tag] [8-bit type] [8-bit data_offset] [4-bit size_offset] [4-bit data_size]
734734+ *
735735+ * 2 words: [2-bit len] [6-bit tag] [8-bit type] [12-bit array_size] [4-bit size_offset]
736736+ * [16-bit data_offset] [12-bit data_size] [4-bit tag>>6]
737737+ *
738738+ * 4 words: [2-bit len] [6-bit tag] [8-bit type] [16-bit array_size]
739739+ * [8-bit size_offset] [24-bit tag>>6]
740740+ * [32-bit data_offset]
741741+ * [32-bit data_size]
742742+ *
743743+ * 8 words: [2-bit len] [6-bit tag] [8-bit type] [16-bit reserved]
744744+ * [8-bit size_offset] [24-bit tag>>6]
745745+ * [32-bit data_offset]
746746+ * [32-bit data_size]
747747+ * [32-bit array_size]
748748+ * [32-bit reserved]
749749+ * [32-bit reserved]
750750+ * [32-bit reserved]
751751+ */
752752+753753+#define PB_FIELDINFO_1(tag, type, data_offset, data_size, size_offset, array_size) \
754754+ (0 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(data_offset) & 0xFF) << 16) | \
755755+ (((uint32_t)(size_offset) & 0x0F) << 24) | (((uint32_t)(data_size) & 0x0F) << 28)),
756756+757757+#define PB_FIELDINFO_2(tag, type, data_offset, data_size, size_offset, array_size) \
758758+ (1 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFF) << 16) | (((uint32_t)(size_offset) & 0x0F) << 28)), \
759759+ (((uint32_t)(data_offset) & 0xFFFF) | (((uint32_t)(data_size) & 0xFFF) << 16) | (((uint32_t)(tag) & 0x3c0) << 22)),
760760+761761+#define PB_FIELDINFO_4(tag, type, data_offset, data_size, size_offset, array_size) \
762762+ (2 | (((tag) << 2) & 0xFF) | ((type) << 8) | (((uint32_t)(array_size) & 0xFFFF) << 16)), \
763763+ ((uint32_t)(int_least8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
764764+ (data_offset), (data_size),
765765+766766+#define PB_FIELDINFO_8(tag, type, data_offset, data_size, size_offset, array_size) \
767767+ (3 | (((tag) << 2) & 0xFF) | ((type) << 8)), \
768768+ ((uint32_t)(int_least8_t)(size_offset) | (((uint32_t)(tag) << 2) & 0xFFFFFF00)), \
769769+ (data_offset), (data_size), (array_size), 0, 0, 0,
770770+771771+/* These assertions verify that the field information fits in the allocated space.
772772+ * The generator tries to automatically determine the correct width that can fit all
773773+ * data associated with a message. These asserts will fail only if there has been a
774774+ * problem in the automatic logic - this may be worth reporting as a bug. As a workaround,
775775+ * you can increase the descriptor width by defining PB_FIELDINFO_WIDTH or by setting
776776+ * descriptorsize option in .options file.
777777+ */
778778+#define PB_FITS(value,bits) ((uint32_t)(value) < ((uint32_t)1<<bits))
779779+#define PB_FIELDINFO_ASSERT_1(tag, type, data_offset, data_size, size_offset, array_size) \
780780+ PB_STATIC_ASSERT(PB_FITS(tag,6) && PB_FITS(data_offset,8) && PB_FITS(size_offset,4) && PB_FITS(data_size,4) && PB_FITS(array_size,1), FIELDINFO_DOES_NOT_FIT_width1_field ## tag)
781781+782782+#define PB_FIELDINFO_ASSERT_2(tag, type, data_offset, data_size, size_offset, array_size) \
783783+ PB_STATIC_ASSERT(PB_FITS(tag,10) && PB_FITS(data_offset,16) && PB_FITS(size_offset,4) && PB_FITS(data_size,12) && PB_FITS(array_size,12), FIELDINFO_DOES_NOT_FIT_width2_field ## tag)
784784+785785+#ifndef PB_FIELD_32BIT
786786+/* Maximum field sizes are still 16-bit if pb_size_t is 16-bit */
787787+#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
788788+ PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int_least8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
789789+790790+#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
791791+ PB_STATIC_ASSERT(PB_FITS(tag,16) && PB_FITS(data_offset,16) && PB_FITS((int_least8_t)size_offset,8) && PB_FITS(data_size,16) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
792792+#else
793793+/* Up to 32-bit fields supported.
794794+ * Note that the checks are against 31 bits to avoid compiler warnings about shift wider than type in the test.
795795+ * I expect that there is no reasonable use for >2GB messages with nanopb anyway.
796796+ */
797797+#define PB_FIELDINFO_ASSERT_4(tag, type, data_offset, data_size, size_offset, array_size) \
798798+ PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS(size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,16), FIELDINFO_DOES_NOT_FIT_width4_field ## tag)
799799+800800+#define PB_FIELDINFO_ASSERT_8(tag, type, data_offset, data_size, size_offset, array_size) \
801801+ PB_STATIC_ASSERT(PB_FITS(tag,30) && PB_FITS(data_offset,31) && PB_FITS(size_offset,8) && PB_FITS(data_size,31) && PB_FITS(array_size,31), FIELDINFO_DOES_NOT_FIT_width8_field ## tag)
802802+#endif
803803+804804+805805+/* Automatic picking of FIELDINFO width:
806806+ * Uses width 1 when possible, otherwise resorts to width 2.
807807+ * This is used when PB_BIND() is called with "AUTO" as the argument.
808808+ * The generator will give explicit size argument when it knows that a message
809809+ * structure grows beyond 1-word format limits.
810810+ */
811811+#define PB_FIELDINFO_WIDTH_AUTO(atype, htype, ltype) PB_FI_WIDTH ## atype(htype, ltype)
812812+#define PB_FI_WIDTH_PB_ATYPE_STATIC(htype, ltype) PB_FI_WIDTH ## htype(ltype)
813813+#define PB_FI_WIDTH_PB_ATYPE_POINTER(htype, ltype) PB_FI_WIDTH ## htype(ltype)
814814+#define PB_FI_WIDTH_PB_ATYPE_CALLBACK(htype, ltype) 2
815815+#define PB_FI_WIDTH_PB_HTYPE_REQUIRED(ltype) PB_FI_WIDTH ## ltype
816816+#define PB_FI_WIDTH_PB_HTYPE_SINGULAR(ltype) PB_FI_WIDTH ## ltype
817817+#define PB_FI_WIDTH_PB_HTYPE_OPTIONAL(ltype) PB_FI_WIDTH ## ltype
818818+#define PB_FI_WIDTH_PB_HTYPE_ONEOF(ltype) PB_FI_WIDTH ## ltype
819819+#define PB_FI_WIDTH_PB_HTYPE_REPEATED(ltype) 2
820820+#define PB_FI_WIDTH_PB_HTYPE_FIXARRAY(ltype) 2
821821+#define PB_FI_WIDTH_PB_LTYPE_BOOL 1
822822+#define PB_FI_WIDTH_PB_LTYPE_BYTES 2
823823+#define PB_FI_WIDTH_PB_LTYPE_DOUBLE 1
824824+#define PB_FI_WIDTH_PB_LTYPE_ENUM 1
825825+#define PB_FI_WIDTH_PB_LTYPE_UENUM 1
826826+#define PB_FI_WIDTH_PB_LTYPE_FIXED32 1
827827+#define PB_FI_WIDTH_PB_LTYPE_FIXED64 1
828828+#define PB_FI_WIDTH_PB_LTYPE_FLOAT 1
829829+#define PB_FI_WIDTH_PB_LTYPE_INT32 1
830830+#define PB_FI_WIDTH_PB_LTYPE_INT64 1
831831+#define PB_FI_WIDTH_PB_LTYPE_MESSAGE 2
832832+#define PB_FI_WIDTH_PB_LTYPE_MSG_W_CB 2
833833+#define PB_FI_WIDTH_PB_LTYPE_SFIXED32 1
834834+#define PB_FI_WIDTH_PB_LTYPE_SFIXED64 1
835835+#define PB_FI_WIDTH_PB_LTYPE_SINT32 1
836836+#define PB_FI_WIDTH_PB_LTYPE_SINT64 1
837837+#define PB_FI_WIDTH_PB_LTYPE_STRING 2
838838+#define PB_FI_WIDTH_PB_LTYPE_UINT32 1
839839+#define PB_FI_WIDTH_PB_LTYPE_UINT64 1
840840+#define PB_FI_WIDTH_PB_LTYPE_EXTENSION 1
841841+#define PB_FI_WIDTH_PB_LTYPE_FIXED_LENGTH_BYTES 2
842842+843843+/* The mapping from protobuf types to LTYPEs is done using these macros. */
844844+#define PB_LTYPE_MAP_BOOL PB_LTYPE_BOOL
845845+#define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
846846+#define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
847847+#define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
848848+#define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT
849849+#define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
850850+#define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
851851+#define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
852852+#define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
853853+#define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
854854+#define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
855855+#define PB_LTYPE_MAP_MSG_W_CB PB_LTYPE_SUBMSG_W_CB
856856+#define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
857857+#define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
858858+#define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
859859+#define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
860860+#define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
861861+#define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
862862+#define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
863863+#define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
864864+#define PB_LTYPE_MAP_FIXED_LENGTH_BYTES PB_LTYPE_FIXED_LENGTH_BYTES
865865+866866+/* These macros are used for giving out error messages.
867867+ * They are mostly a debugging aid; the main error information
868868+ * is the true/false return value from functions.
869869+ * Some code space can be saved by disabling the error
870870+ * messages if not used.
871871+ *
872872+ * PB_SET_ERROR() sets the error message if none has been set yet.
873873+ * msg must be a constant string literal.
874874+ * PB_GET_ERROR() always returns a pointer to a string.
875875+ * PB_RETURN_ERROR() sets the error and returns false from current
876876+ * function.
877877+ */
878878+#ifdef PB_NO_ERRMSG
879879+#define PB_SET_ERROR(stream, msg) PB_UNUSED(stream)
880880+#define PB_GET_ERROR(stream) "(errmsg disabled)"
881881+#else
882882+#define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg))
883883+#define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
884884+#endif
885885+886886+#define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false
887887+888888+#ifdef __cplusplus
889889+} /* extern "C" */
890890+#endif
891891+892892+#ifdef __cplusplus
893893+#if __cplusplus >= 201103L
894894+#define PB_CONSTEXPR constexpr
895895+#else // __cplusplus >= 201103L
896896+#define PB_CONSTEXPR
897897+#endif // __cplusplus >= 201103L
898898+899899+#if __cplusplus >= 201703L
900900+#define PB_INLINE_CONSTEXPR inline constexpr
901901+#else // __cplusplus >= 201703L
902902+#define PB_INLINE_CONSTEXPR PB_CONSTEXPR
903903+#endif // __cplusplus >= 201703L
904904+905905+namespace nanopb {
906906+// Each type will be partially specialized by the generator.
907907+template <typename GenMessageT> struct MessageDescriptor;
908908+} // namespace nanopb
909909+#endif /* __cplusplus */
910910+911911+#endif
+388
src/external/nanopb/pb_common.c
···11+/* pb_common.c: Common support functions for pb_encode.c and pb_decode.c.
22+ *
33+ * 2014 Petteri Aimonen <jpa@kapsi.fi>
44+ */
55+66+#include "pb_common.h"
77+88+static bool load_descriptor_values(pb_field_iter_t *iter)
99+{
1010+ uint32_t word0;
1111+ uint32_t data_offset;
1212+ int_least8_t size_offset;
1313+1414+ if (iter->index >= iter->descriptor->field_count)
1515+ return false;
1616+1717+ word0 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index]);
1818+ iter->type = (pb_type_t)((word0 >> 8) & 0xFF);
1919+2020+ switch(word0 & 3)
2121+ {
2222+ case 0: {
2323+ /* 1-word format */
2424+ iter->array_size = 1;
2525+ iter->tag = (pb_size_t)((word0 >> 2) & 0x3F);
2626+ size_offset = (int_least8_t)((word0 >> 24) & 0x0F);
2727+ data_offset = (word0 >> 16) & 0xFF;
2828+ iter->data_size = (pb_size_t)((word0 >> 28) & 0x0F);
2929+ break;
3030+ }
3131+3232+ case 1: {
3333+ /* 2-word format */
3434+ uint32_t word1 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 1]);
3535+3636+ iter->array_size = (pb_size_t)((word0 >> 16) & 0x0FFF);
3737+ iter->tag = (pb_size_t)(((word0 >> 2) & 0x3F) | ((word1 >> 28) << 6));
3838+ size_offset = (int_least8_t)((word0 >> 28) & 0x0F);
3939+ data_offset = word1 & 0xFFFF;
4040+ iter->data_size = (pb_size_t)((word1 >> 16) & 0x0FFF);
4141+ break;
4242+ }
4343+4444+ case 2: {
4545+ /* 4-word format */
4646+ uint32_t word1 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 1]);
4747+ uint32_t word2 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 2]);
4848+ uint32_t word3 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 3]);
4949+5050+ iter->array_size = (pb_size_t)(word0 >> 16);
5151+ iter->tag = (pb_size_t)(((word0 >> 2) & 0x3F) | ((word1 >> 8) << 6));
5252+ size_offset = (int_least8_t)(word1 & 0xFF);
5353+ data_offset = word2;
5454+ iter->data_size = (pb_size_t)word3;
5555+ break;
5656+ }
5757+5858+ default: {
5959+ /* 8-word format */
6060+ uint32_t word1 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 1]);
6161+ uint32_t word2 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 2]);
6262+ uint32_t word3 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 3]);
6363+ uint32_t word4 = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index + 4]);
6464+6565+ iter->array_size = (pb_size_t)word4;
6666+ iter->tag = (pb_size_t)(((word0 >> 2) & 0x3F) | ((word1 >> 8) << 6));
6767+ size_offset = (int_least8_t)(word1 & 0xFF);
6868+ data_offset = word2;
6969+ iter->data_size = (pb_size_t)word3;
7070+ break;
7171+ }
7272+ }
7373+7474+ if (!iter->message)
7575+ {
7676+ /* Avoid doing arithmetic on null pointers, it is undefined */
7777+ iter->pField = NULL;
7878+ iter->pSize = NULL;
7979+ }
8080+ else
8181+ {
8282+ iter->pField = (char*)iter->message + data_offset;
8383+8484+ if (size_offset)
8585+ {
8686+ iter->pSize = (char*)iter->pField - size_offset;
8787+ }
8888+ else if (PB_HTYPE(iter->type) == PB_HTYPE_REPEATED &&
8989+ (PB_ATYPE(iter->type) == PB_ATYPE_STATIC ||
9090+ PB_ATYPE(iter->type) == PB_ATYPE_POINTER))
9191+ {
9292+ /* Fixed count array */
9393+ iter->pSize = &iter->array_size;
9494+ }
9595+ else
9696+ {
9797+ iter->pSize = NULL;
9898+ }
9999+100100+ if (PB_ATYPE(iter->type) == PB_ATYPE_POINTER && iter->pField != NULL)
101101+ {
102102+ iter->pData = *(void**)iter->pField;
103103+ }
104104+ else
105105+ {
106106+ iter->pData = iter->pField;
107107+ }
108108+ }
109109+110110+ if (PB_LTYPE_IS_SUBMSG(iter->type))
111111+ {
112112+ iter->submsg_desc = iter->descriptor->submsg_info[iter->submessage_index];
113113+ }
114114+ else
115115+ {
116116+ iter->submsg_desc = NULL;
117117+ }
118118+119119+ return true;
120120+}
121121+122122+static void advance_iterator(pb_field_iter_t *iter)
123123+{
124124+ iter->index++;
125125+126126+ if (iter->index >= iter->descriptor->field_count)
127127+ {
128128+ /* Restart */
129129+ iter->index = 0;
130130+ iter->field_info_index = 0;
131131+ iter->submessage_index = 0;
132132+ iter->required_field_index = 0;
133133+ }
134134+ else
135135+ {
136136+ /* Increment indexes based on previous field type.
137137+ * All field info formats have the following fields:
138138+ * - lowest 2 bits tell the amount of words in the descriptor (2^n words)
139139+ * - bits 2..7 give the lowest bits of tag number.
140140+ * - bits 8..15 give the field type.
141141+ */
142142+ uint32_t prev_descriptor = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index]);
143143+ pb_type_t prev_type = (prev_descriptor >> 8) & 0xFF;
144144+ pb_size_t descriptor_len = (pb_size_t)(1 << (prev_descriptor & 3));
145145+146146+ /* Add to fields.
147147+ * The cast to pb_size_t is needed to avoid -Wconversion warning.
148148+ * Because the data is is constants from generator, there is no danger of overflow.
149149+ */
150150+ iter->field_info_index = (pb_size_t)(iter->field_info_index + descriptor_len);
151151+ iter->required_field_index = (pb_size_t)(iter->required_field_index + (PB_HTYPE(prev_type) == PB_HTYPE_REQUIRED));
152152+ iter->submessage_index = (pb_size_t)(iter->submessage_index + PB_LTYPE_IS_SUBMSG(prev_type));
153153+ }
154154+}
155155+156156+bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_msgdesc_t *desc, void *message)
157157+{
158158+ memset(iter, 0, sizeof(*iter));
159159+160160+ iter->descriptor = desc;
161161+ iter->message = message;
162162+163163+ return load_descriptor_values(iter);
164164+}
165165+166166+bool pb_field_iter_begin_extension(pb_field_iter_t *iter, pb_extension_t *extension)
167167+{
168168+ const pb_msgdesc_t *msg = (const pb_msgdesc_t*)extension->type->arg;
169169+ bool status;
170170+171171+ uint32_t word0 = PB_PROGMEM_READU32(msg->field_info[0]);
172172+ if (PB_ATYPE(word0 >> 8) == PB_ATYPE_POINTER)
173173+ {
174174+ /* For pointer extensions, the pointer is stored directly
175175+ * in the extension structure. This avoids having an extra
176176+ * indirection. */
177177+ status = pb_field_iter_begin(iter, msg, &extension->dest);
178178+ }
179179+ else
180180+ {
181181+ status = pb_field_iter_begin(iter, msg, extension->dest);
182182+ }
183183+184184+ iter->pSize = &extension->found;
185185+ return status;
186186+}
187187+188188+bool pb_field_iter_next(pb_field_iter_t *iter)
189189+{
190190+ advance_iterator(iter);
191191+ (void)load_descriptor_values(iter);
192192+ return iter->index != 0;
193193+}
194194+195195+bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag)
196196+{
197197+ if (iter->tag == tag)
198198+ {
199199+ return true; /* Nothing to do, correct field already. */
200200+ }
201201+ else if (tag > iter->descriptor->largest_tag)
202202+ {
203203+ return false;
204204+ }
205205+ else
206206+ {
207207+ pb_size_t start = iter->index;
208208+ uint32_t fieldinfo;
209209+210210+ if (tag < iter->tag)
211211+ {
212212+ /* Fields are in tag number order, so we know that tag is between
213213+ * 0 and our start position. Setting index to end forces
214214+ * advance_iterator() call below to restart from beginning. */
215215+ iter->index = iter->descriptor->field_count;
216216+ }
217217+218218+ do
219219+ {
220220+ /* Advance iterator but don't load values yet */
221221+ advance_iterator(iter);
222222+223223+ /* Do fast check for tag number match */
224224+ fieldinfo = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index]);
225225+226226+ if (((fieldinfo >> 2) & 0x3F) == (tag & 0x3F))
227227+ {
228228+ /* Good candidate, check further */
229229+ (void)load_descriptor_values(iter);
230230+231231+ if (iter->tag == tag &&
232232+ PB_LTYPE(iter->type) != PB_LTYPE_EXTENSION)
233233+ {
234234+ /* Found it */
235235+ return true;
236236+ }
237237+ }
238238+ } while (iter->index != start);
239239+240240+ /* Searched all the way back to start, and found nothing. */
241241+ (void)load_descriptor_values(iter);
242242+ return false;
243243+ }
244244+}
245245+246246+bool pb_field_iter_find_extension(pb_field_iter_t *iter)
247247+{
248248+ if (PB_LTYPE(iter->type) == PB_LTYPE_EXTENSION)
249249+ {
250250+ return true;
251251+ }
252252+ else
253253+ {
254254+ pb_size_t start = iter->index;
255255+ uint32_t fieldinfo;
256256+257257+ do
258258+ {
259259+ /* Advance iterator but don't load values yet */
260260+ advance_iterator(iter);
261261+262262+ /* Do fast check for field type */
263263+ fieldinfo = PB_PROGMEM_READU32(iter->descriptor->field_info[iter->field_info_index]);
264264+265265+ if (PB_LTYPE((fieldinfo >> 8) & 0xFF) == PB_LTYPE_EXTENSION)
266266+ {
267267+ return load_descriptor_values(iter);
268268+ }
269269+ } while (iter->index != start);
270270+271271+ /* Searched all the way back to start, and found nothing. */
272272+ (void)load_descriptor_values(iter);
273273+ return false;
274274+ }
275275+}
276276+277277+static void *pb_const_cast(const void *p)
278278+{
279279+ /* Note: this casts away const, in order to use the common field iterator
280280+ * logic for both encoding and decoding. The cast is done using union
281281+ * to avoid spurious compiler warnings. */
282282+ union {
283283+ void *p1;
284284+ const void *p2;
285285+ } t;
286286+ t.p2 = p;
287287+ return t.p1;
288288+}
289289+290290+bool pb_field_iter_begin_const(pb_field_iter_t *iter, const pb_msgdesc_t *desc, const void *message)
291291+{
292292+ return pb_field_iter_begin(iter, desc, pb_const_cast(message));
293293+}
294294+295295+bool pb_field_iter_begin_extension_const(pb_field_iter_t *iter, const pb_extension_t *extension)
296296+{
297297+ return pb_field_iter_begin_extension(iter, (pb_extension_t*)pb_const_cast(extension));
298298+}
299299+300300+bool pb_default_field_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field)
301301+{
302302+ if (field->data_size == sizeof(pb_callback_t))
303303+ {
304304+ pb_callback_t *pCallback = (pb_callback_t*)field->pData;
305305+306306+ if (pCallback != NULL)
307307+ {
308308+ if (istream != NULL && pCallback->funcs.decode != NULL)
309309+ {
310310+ return pCallback->funcs.decode(istream, field, &pCallback->arg);
311311+ }
312312+313313+ if (ostream != NULL && pCallback->funcs.encode != NULL)
314314+ {
315315+ return pCallback->funcs.encode(ostream, field, &pCallback->arg);
316316+ }
317317+ }
318318+ }
319319+320320+ return true; /* Success, but didn't do anything */
321321+322322+}
323323+324324+#ifdef PB_VALIDATE_UTF8
325325+326326+/* This function checks whether a string is valid UTF-8 text.
327327+ *
328328+ * Algorithm is adapted from https://www.cl.cam.ac.uk/~mgk25/ucs/utf8_check.c
329329+ * Original copyright: Markus Kuhn <http://www.cl.cam.ac.uk/~mgk25/> 2005-03-30
330330+ * Licensed under "Short code license", which allows use under MIT license or
331331+ * any compatible with it.
332332+ */
333333+334334+bool pb_validate_utf8(const char *str)
335335+{
336336+ const pb_byte_t *s = (const pb_byte_t*)str;
337337+ while (*s)
338338+ {
339339+ if (*s < 0x80)
340340+ {
341341+ /* 0xxxxxxx */
342342+ s++;
343343+ }
344344+ else if ((s[0] & 0xe0) == 0xc0)
345345+ {
346346+ /* 110XXXXx 10xxxxxx */
347347+ if ((s[1] & 0xc0) != 0x80 ||
348348+ (s[0] & 0xfe) == 0xc0) /* overlong? */
349349+ return false;
350350+ else
351351+ s += 2;
352352+ }
353353+ else if ((s[0] & 0xf0) == 0xe0)
354354+ {
355355+ /* 1110XXXX 10Xxxxxx 10xxxxxx */
356356+ if ((s[1] & 0xc0) != 0x80 ||
357357+ (s[2] & 0xc0) != 0x80 ||
358358+ (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || /* overlong? */
359359+ (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) || /* surrogate? */
360360+ (s[0] == 0xef && s[1] == 0xbf &&
361361+ (s[2] & 0xfe) == 0xbe)) /* U+FFFE or U+FFFF? */
362362+ return false;
363363+ else
364364+ s += 3;
365365+ }
366366+ else if ((s[0] & 0xf8) == 0xf0)
367367+ {
368368+ /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
369369+ if ((s[1] & 0xc0) != 0x80 ||
370370+ (s[2] & 0xc0) != 0x80 ||
371371+ (s[3] & 0xc0) != 0x80 ||
372372+ (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || /* overlong? */
373373+ (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4) /* > U+10FFFF? */
374374+ return false;
375375+ else
376376+ s += 4;
377377+ }
378378+ else
379379+ {
380380+ return false;
381381+ }
382382+ }
383383+384384+ return true;
385385+}
386386+387387+#endif
388388+
+49
src/external/nanopb/pb_common.h
···11+/* pb_common.h: Common support functions for pb_encode.c and pb_decode.c.
22+ * These functions are rarely needed by applications directly.
33+ */
44+55+#ifndef PB_COMMON_H_INCLUDED
66+#define PB_COMMON_H_INCLUDED
77+88+#include "pb.h"
99+1010+#ifdef __cplusplus
1111+extern "C" {
1212+#endif
1313+1414+/* Initialize the field iterator structure to beginning.
1515+ * Returns false if the message type is empty. */
1616+bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_msgdesc_t *desc, void *message);
1717+1818+/* Get a field iterator for extension field. */
1919+bool pb_field_iter_begin_extension(pb_field_iter_t *iter, pb_extension_t *extension);
2020+2121+/* Same as pb_field_iter_begin(), but for const message pointer.
2222+ * Note that the pointers in pb_field_iter_t will be non-const but shouldn't
2323+ * be written to when using these functions. */
2424+bool pb_field_iter_begin_const(pb_field_iter_t *iter, const pb_msgdesc_t *desc, const void *message);
2525+bool pb_field_iter_begin_extension_const(pb_field_iter_t *iter, const pb_extension_t *extension);
2626+2727+/* Advance the iterator to the next field.
2828+ * Returns false when the iterator wraps back to the first field. */
2929+bool pb_field_iter_next(pb_field_iter_t *iter);
3030+3131+/* Advance the iterator until it points at a field with the given tag.
3232+ * Returns false if no such field exists. */
3333+bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag);
3434+3535+/* Find a field with type PB_LTYPE_EXTENSION, or return false if not found.
3636+ * There can be only one extension range field per message. */
3737+bool pb_field_iter_find_extension(pb_field_iter_t *iter);
3838+3939+#ifdef PB_VALIDATE_UTF8
4040+/* Validate UTF-8 text string */
4141+bool pb_validate_utf8(const char *s);
4242+#endif
4343+4444+#ifdef __cplusplus
4545+} /* extern "C" */
4646+#endif
4747+4848+#endif
4949+
+1716
src/external/nanopb/pb_decode.c
···11+/* pb_decode.c -- decode a protobuf using minimal resources
22+ *
33+ * 2011 Petteri Aimonen <jpa@kapsi.fi>
44+ */
55+66+/* Use the GCC warn_unused_result attribute to check that all return values
77+ * are propagated correctly. On other compilers and gcc before 3.4.0 just
88+ * ignore the annotation.
99+ */
1010+#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
1111+ #define checkreturn
1212+#else
1313+ #define checkreturn __attribute__((warn_unused_result))
1414+#endif
1515+1616+#include "pb.h"
1717+#include "pb_decode.h"
1818+#include "pb_common.h"
1919+2020+/**************************************
2121+ * Declarations internal to this file *
2222+ **************************************/
2323+2424+static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
2525+static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof);
2626+static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
2727+static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
2828+static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
2929+static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
3030+static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
3131+static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
3232+static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
3333+static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension);
3434+static bool pb_field_set_to_default(pb_field_iter_t *field);
3535+static bool pb_message_set_to_defaults(pb_field_iter_t *iter);
3636+static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field);
3737+static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field);
3838+static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
3939+static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field);
4040+static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field);
4141+static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
4242+static bool checkreturn pb_skip_varint(pb_istream_t *stream);
4343+static bool checkreturn pb_skip_string(pb_istream_t *stream);
4444+4545+#ifdef PB_ENABLE_MALLOC
4646+static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
4747+static void initialize_pointer_field(void *pItem, pb_field_iter_t *field);
4848+static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field);
4949+static void pb_release_single_field(pb_field_iter_t *field);
5050+#endif
5151+5252+#ifdef PB_WITHOUT_64BIT
5353+#define pb_int64_t int32_t
5454+#define pb_uint64_t uint32_t
5555+#else
5656+#define pb_int64_t int64_t
5757+#define pb_uint64_t uint64_t
5858+#endif
5959+6060+typedef struct {
6161+ uint32_t bitfield[(PB_MAX_REQUIRED_FIELDS + 31) / 32];
6262+} pb_fields_seen_t;
6363+6464+/*******************************
6565+ * pb_istream_t implementation *
6666+ *******************************/
6767+6868+static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
6969+{
7070+ const pb_byte_t *source = (const pb_byte_t*)stream->state;
7171+ stream->state = (pb_byte_t*)stream->state + count;
7272+7373+ if (buf != NULL)
7474+ {
7575+ memcpy(buf, source, count * sizeof(pb_byte_t));
7676+ }
7777+7878+ return true;
7979+}
8080+8181+bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
8282+{
8383+ if (count == 0)
8484+ return true;
8585+8686+#ifndef PB_BUFFER_ONLY
8787+ if (buf == NULL && stream->callback != buf_read)
8888+ {
8989+ /* Skip input bytes */
9090+ pb_byte_t tmp[16];
9191+ while (count > 16)
9292+ {
9393+ if (!pb_read(stream, tmp, 16))
9494+ return false;
9595+9696+ count -= 16;
9797+ }
9898+9999+ return pb_read(stream, tmp, count);
100100+ }
101101+#endif
102102+103103+ if (stream->bytes_left < count)
104104+ PB_RETURN_ERROR(stream, "end-of-stream");
105105+106106+#ifndef PB_BUFFER_ONLY
107107+ if (!stream->callback(stream, buf, count))
108108+ PB_RETURN_ERROR(stream, "io error");
109109+#else
110110+ if (!buf_read(stream, buf, count))
111111+ return false;
112112+#endif
113113+114114+ stream->bytes_left -= count;
115115+ return true;
116116+}
117117+118118+/* Read a single byte from input stream. buf may not be NULL.
119119+ * This is an optimization for the varint decoding. */
120120+static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
121121+{
122122+ if (stream->bytes_left == 0)
123123+ PB_RETURN_ERROR(stream, "end-of-stream");
124124+125125+#ifndef PB_BUFFER_ONLY
126126+ if (!stream->callback(stream, buf, 1))
127127+ PB_RETURN_ERROR(stream, "io error");
128128+#else
129129+ *buf = *(const pb_byte_t*)stream->state;
130130+ stream->state = (pb_byte_t*)stream->state + 1;
131131+#endif
132132+133133+ stream->bytes_left--;
134134+135135+ return true;
136136+}
137137+138138+pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
139139+{
140140+ pb_istream_t stream;
141141+ /* Cast away the const from buf without a compiler error. We are
142142+ * careful to use it only in a const manner in the callbacks.
143143+ */
144144+ union {
145145+ void *state;
146146+ const void *c_state;
147147+ } state;
148148+#ifdef PB_BUFFER_ONLY
149149+ stream.callback = NULL;
150150+#else
151151+ stream.callback = &buf_read;
152152+#endif
153153+ state.c_state = buf;
154154+ stream.state = state.state;
155155+ stream.bytes_left = msglen;
156156+#ifndef PB_NO_ERRMSG
157157+ stream.errmsg = NULL;
158158+#endif
159159+ return stream;
160160+}
161161+162162+/********************
163163+ * Helper functions *
164164+ ********************/
165165+166166+static bool checkreturn pb_decode_varint32_eof(pb_istream_t *stream, uint32_t *dest, bool *eof)
167167+{
168168+ pb_byte_t byte;
169169+ uint32_t result;
170170+171171+ if (!pb_readbyte(stream, &byte))
172172+ {
173173+ if (stream->bytes_left == 0)
174174+ {
175175+ if (eof)
176176+ {
177177+ *eof = true;
178178+ }
179179+ }
180180+181181+ return false;
182182+ }
183183+184184+ if ((byte & 0x80) == 0)
185185+ {
186186+ /* Quick case, 1 byte value */
187187+ result = byte;
188188+ }
189189+ else
190190+ {
191191+ /* Multibyte case */
192192+ uint_fast8_t bitpos = 7;
193193+ result = byte & 0x7F;
194194+195195+ do
196196+ {
197197+ if (!pb_readbyte(stream, &byte))
198198+ return false;
199199+200200+ if (bitpos >= 32)
201201+ {
202202+ /* Note: The varint could have trailing 0x80 bytes, or 0xFF for negative. */
203203+ pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
204204+ bool valid_extension = ((byte & 0x7F) == 0x00 ||
205205+ ((result >> 31) != 0 && byte == sign_extension));
206206+207207+ if (bitpos >= 64 || !valid_extension)
208208+ {
209209+ PB_RETURN_ERROR(stream, "varint overflow");
210210+ }
211211+ }
212212+ else if (bitpos == 28)
213213+ {
214214+ if ((byte & 0x70) != 0 && (byte & 0x78) != 0x78)
215215+ {
216216+ PB_RETURN_ERROR(stream, "varint overflow");
217217+ }
218218+ result |= (uint32_t)(byte & 0x0F) << bitpos;
219219+ }
220220+ else
221221+ {
222222+ result |= (uint32_t)(byte & 0x7F) << bitpos;
223223+ }
224224+ bitpos = (uint_fast8_t)(bitpos + 7);
225225+ } while (byte & 0x80);
226226+ }
227227+228228+ *dest = result;
229229+ return true;
230230+}
231231+232232+bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
233233+{
234234+ return pb_decode_varint32_eof(stream, dest, NULL);
235235+}
236236+237237+#ifndef PB_WITHOUT_64BIT
238238+bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
239239+{
240240+ pb_byte_t byte;
241241+ uint_fast8_t bitpos = 0;
242242+ uint64_t result = 0;
243243+244244+ do
245245+ {
246246+ if (!pb_readbyte(stream, &byte))
247247+ return false;
248248+249249+ if (bitpos >= 63 && (byte & 0xFE) != 0)
250250+ PB_RETURN_ERROR(stream, "varint overflow");
251251+252252+ result |= (uint64_t)(byte & 0x7F) << bitpos;
253253+ bitpos = (uint_fast8_t)(bitpos + 7);
254254+ } while (byte & 0x80);
255255+256256+ *dest = result;
257257+ return true;
258258+}
259259+#endif
260260+261261+bool checkreturn pb_skip_varint(pb_istream_t *stream)
262262+{
263263+ pb_byte_t byte;
264264+ do
265265+ {
266266+ if (!pb_read(stream, &byte, 1))
267267+ return false;
268268+ } while (byte & 0x80);
269269+ return true;
270270+}
271271+272272+bool checkreturn pb_skip_string(pb_istream_t *stream)
273273+{
274274+ uint32_t length;
275275+ if (!pb_decode_varint32(stream, &length))
276276+ return false;
277277+278278+ if ((size_t)length != length)
279279+ {
280280+ PB_RETURN_ERROR(stream, "size too large");
281281+ }
282282+283283+ return pb_read(stream, NULL, (size_t)length);
284284+}
285285+286286+bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
287287+{
288288+ uint32_t temp;
289289+ *eof = false;
290290+ *wire_type = (pb_wire_type_t) 0;
291291+ *tag = 0;
292292+293293+ if (!pb_decode_varint32_eof(stream, &temp, eof))
294294+ {
295295+ return false;
296296+ }
297297+298298+ *tag = temp >> 3;
299299+ *wire_type = (pb_wire_type_t)(temp & 7);
300300+ return true;
301301+}
302302+303303+bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
304304+{
305305+ switch (wire_type)
306306+ {
307307+ case PB_WT_VARINT: return pb_skip_varint(stream);
308308+ case PB_WT_64BIT: return pb_read(stream, NULL, 8);
309309+ case PB_WT_STRING: return pb_skip_string(stream);
310310+ case PB_WT_32BIT: return pb_read(stream, NULL, 4);
311311+ default: PB_RETURN_ERROR(stream, "invalid wire_type");
312312+ }
313313+}
314314+315315+/* Read a raw value to buffer, for the purpose of passing it to callback as
316316+ * a substream. Size is maximum size on call, and actual size on return.
317317+ */
318318+static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
319319+{
320320+ size_t max_size = *size;
321321+ switch (wire_type)
322322+ {
323323+ case PB_WT_VARINT:
324324+ *size = 0;
325325+ do
326326+ {
327327+ (*size)++;
328328+ if (*size > max_size)
329329+ PB_RETURN_ERROR(stream, "varint overflow");
330330+331331+ if (!pb_read(stream, buf, 1))
332332+ return false;
333333+ } while (*buf++ & 0x80);
334334+ return true;
335335+336336+ case PB_WT_64BIT:
337337+ *size = 8;
338338+ return pb_read(stream, buf, 8);
339339+340340+ case PB_WT_32BIT:
341341+ *size = 4;
342342+ return pb_read(stream, buf, 4);
343343+344344+ case PB_WT_STRING:
345345+ /* Calling read_raw_value with a PB_WT_STRING is an error.
346346+ * Explicitly handle this case and fallthrough to default to avoid
347347+ * compiler warnings.
348348+ */
349349+350350+ default: PB_RETURN_ERROR(stream, "invalid wire_type");
351351+ }
352352+}
353353+354354+/* Decode string length from stream and return a substream with limited length.
355355+ * Remember to close the substream using pb_close_string_substream().
356356+ */
357357+bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
358358+{
359359+ uint32_t size;
360360+ if (!pb_decode_varint32(stream, &size))
361361+ return false;
362362+363363+ *substream = *stream;
364364+ if (substream->bytes_left < size)
365365+ PB_RETURN_ERROR(stream, "parent stream too short");
366366+367367+ substream->bytes_left = (size_t)size;
368368+ stream->bytes_left -= (size_t)size;
369369+ return true;
370370+}
371371+372372+bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
373373+{
374374+ if (substream->bytes_left) {
375375+ if (!pb_read(substream, NULL, substream->bytes_left))
376376+ return false;
377377+ }
378378+379379+ stream->state = substream->state;
380380+381381+#ifndef PB_NO_ERRMSG
382382+ stream->errmsg = substream->errmsg;
383383+#endif
384384+ return true;
385385+}
386386+387387+/*************************
388388+ * Decode a single field *
389389+ *************************/
390390+391391+static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
392392+{
393393+ switch (PB_LTYPE(field->type))
394394+ {
395395+ case PB_LTYPE_BOOL:
396396+ if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
397397+ PB_RETURN_ERROR(stream, "wrong wire type");
398398+399399+ return pb_dec_bool(stream, field);
400400+401401+ case PB_LTYPE_VARINT:
402402+ case PB_LTYPE_UVARINT:
403403+ case PB_LTYPE_SVARINT:
404404+ if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
405405+ PB_RETURN_ERROR(stream, "wrong wire type");
406406+407407+ return pb_dec_varint(stream, field);
408408+409409+ case PB_LTYPE_FIXED32:
410410+ if (wire_type != PB_WT_32BIT && wire_type != PB_WT_PACKED)
411411+ PB_RETURN_ERROR(stream, "wrong wire type");
412412+413413+ return pb_decode_fixed32(stream, field->pData);
414414+415415+ case PB_LTYPE_FIXED64:
416416+ if (wire_type != PB_WT_64BIT && wire_type != PB_WT_PACKED)
417417+ PB_RETURN_ERROR(stream, "wrong wire type");
418418+419419+#ifdef PB_CONVERT_DOUBLE_FLOAT
420420+ if (field->data_size == sizeof(float))
421421+ {
422422+ return pb_decode_double_as_float(stream, (float*)field->pData);
423423+ }
424424+#endif
425425+426426+#ifdef PB_WITHOUT_64BIT
427427+ PB_RETURN_ERROR(stream, "invalid data_size");
428428+#else
429429+ return pb_decode_fixed64(stream, field->pData);
430430+#endif
431431+432432+ case PB_LTYPE_BYTES:
433433+ if (wire_type != PB_WT_STRING)
434434+ PB_RETURN_ERROR(stream, "wrong wire type");
435435+436436+ return pb_dec_bytes(stream, field);
437437+438438+ case PB_LTYPE_STRING:
439439+ if (wire_type != PB_WT_STRING)
440440+ PB_RETURN_ERROR(stream, "wrong wire type");
441441+442442+ return pb_dec_string(stream, field);
443443+444444+ case PB_LTYPE_SUBMESSAGE:
445445+ case PB_LTYPE_SUBMSG_W_CB:
446446+ if (wire_type != PB_WT_STRING)
447447+ PB_RETURN_ERROR(stream, "wrong wire type");
448448+449449+ return pb_dec_submessage(stream, field);
450450+451451+ case PB_LTYPE_FIXED_LENGTH_BYTES:
452452+ if (wire_type != PB_WT_STRING)
453453+ PB_RETURN_ERROR(stream, "wrong wire type");
454454+455455+ return pb_dec_fixed_length_bytes(stream, field);
456456+457457+ default:
458458+ PB_RETURN_ERROR(stream, "invalid field type");
459459+ }
460460+}
461461+462462+static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
463463+{
464464+ switch (PB_HTYPE(field->type))
465465+ {
466466+ case PB_HTYPE_REQUIRED:
467467+ return decode_basic_field(stream, wire_type, field);
468468+469469+ case PB_HTYPE_OPTIONAL:
470470+ if (field->pSize != NULL)
471471+ *(bool*)field->pSize = true;
472472+ return decode_basic_field(stream, wire_type, field);
473473+474474+ case PB_HTYPE_REPEATED:
475475+ if (wire_type == PB_WT_STRING
476476+ && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
477477+ {
478478+ /* Packed array */
479479+ bool status = true;
480480+ pb_istream_t substream;
481481+ pb_size_t *size = (pb_size_t*)field->pSize;
482482+ field->pData = (char*)field->pField + field->data_size * (*size);
483483+484484+ if (!pb_make_string_substream(stream, &substream))
485485+ return false;
486486+487487+ while (substream.bytes_left > 0 && *size < field->array_size)
488488+ {
489489+ if (!decode_basic_field(&substream, PB_WT_PACKED, field))
490490+ {
491491+ status = false;
492492+ break;
493493+ }
494494+ (*size)++;
495495+ field->pData = (char*)field->pData + field->data_size;
496496+ }
497497+498498+ if (substream.bytes_left != 0)
499499+ PB_RETURN_ERROR(stream, "array overflow");
500500+ if (!pb_close_string_substream(stream, &substream))
501501+ return false;
502502+503503+ return status;
504504+ }
505505+ else
506506+ {
507507+ /* Repeated field */
508508+ pb_size_t *size = (pb_size_t*)field->pSize;
509509+ field->pData = (char*)field->pField + field->data_size * (*size);
510510+511511+ if ((*size)++ >= field->array_size)
512512+ PB_RETURN_ERROR(stream, "array overflow");
513513+514514+ return decode_basic_field(stream, wire_type, field);
515515+ }
516516+517517+ case PB_HTYPE_ONEOF:
518518+ if (PB_LTYPE_IS_SUBMSG(field->type) &&
519519+ *(pb_size_t*)field->pSize != field->tag)
520520+ {
521521+ /* We memset to zero so that any callbacks are set to NULL.
522522+ * This is because the callbacks might otherwise have values
523523+ * from some other union field.
524524+ * If callbacks are needed inside oneof field, use .proto
525525+ * option submsg_callback to have a separate callback function
526526+ * that can set the fields before submessage is decoded.
527527+ * pb_dec_submessage() will set any default values. */
528528+ memset(field->pData, 0, (size_t)field->data_size);
529529+530530+ /* Set default values for the submessage fields. */
531531+ if (field->submsg_desc->default_value != NULL ||
532532+ field->submsg_desc->field_callback != NULL ||
533533+ field->submsg_desc->submsg_info[0] != NULL)
534534+ {
535535+ pb_field_iter_t submsg_iter;
536536+ if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
537537+ {
538538+ if (!pb_message_set_to_defaults(&submsg_iter))
539539+ PB_RETURN_ERROR(stream, "failed to set defaults");
540540+ }
541541+ }
542542+ }
543543+ *(pb_size_t*)field->pSize = field->tag;
544544+545545+ return decode_basic_field(stream, wire_type, field);
546546+547547+ default:
548548+ PB_RETURN_ERROR(stream, "invalid field type");
549549+ }
550550+}
551551+552552+#ifdef PB_ENABLE_MALLOC
553553+/* Allocate storage for the field and store the pointer at iter->pData.
554554+ * array_size is the number of entries to reserve in an array.
555555+ * Zero size is not allowed, use pb_free() for releasing.
556556+ */
557557+static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
558558+{
559559+ void *ptr = *(void**)pData;
560560+561561+ if (data_size == 0 || array_size == 0)
562562+ PB_RETURN_ERROR(stream, "invalid size");
563563+564564+#ifdef __AVR__
565565+ /* Workaround for AVR libc bug 53284: http://savannah.nongnu.org/bugs/?53284
566566+ * Realloc to size of 1 byte can cause corruption of the malloc structures.
567567+ */
568568+ if (data_size == 1 && array_size == 1)
569569+ {
570570+ data_size = 2;
571571+ }
572572+#endif
573573+574574+ /* Check for multiplication overflows.
575575+ * This code avoids the costly division if the sizes are small enough.
576576+ * Multiplication is safe as long as only half of bits are set
577577+ * in either multiplicand.
578578+ */
579579+ {
580580+ const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
581581+ if (data_size >= check_limit || array_size >= check_limit)
582582+ {
583583+ const size_t size_max = (size_t)-1;
584584+ if (size_max / array_size < data_size)
585585+ {
586586+ PB_RETURN_ERROR(stream, "size too large");
587587+ }
588588+ }
589589+ }
590590+591591+ /* Allocate new or expand previous allocation */
592592+ /* Note: on failure the old pointer will remain in the structure,
593593+ * the message must be freed by caller also on error return. */
594594+ ptr = pb_realloc(ptr, array_size * data_size);
595595+ if (ptr == NULL)
596596+ PB_RETURN_ERROR(stream, "realloc failed");
597597+598598+ *(void**)pData = ptr;
599599+ return true;
600600+}
601601+602602+/* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
603603+static void initialize_pointer_field(void *pItem, pb_field_iter_t *field)
604604+{
605605+ if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
606606+ PB_LTYPE(field->type) == PB_LTYPE_BYTES)
607607+ {
608608+ *(void**)pItem = NULL;
609609+ }
610610+ else if (PB_LTYPE_IS_SUBMSG(field->type))
611611+ {
612612+ /* We memset to zero so that any callbacks are set to NULL.
613613+ * Default values will be set by pb_dec_submessage(). */
614614+ memset(pItem, 0, field->data_size);
615615+ }
616616+}
617617+#endif
618618+619619+static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
620620+{
621621+#ifndef PB_ENABLE_MALLOC
622622+ PB_UNUSED(wire_type);
623623+ PB_UNUSED(field);
624624+ PB_RETURN_ERROR(stream, "no malloc support");
625625+#else
626626+ switch (PB_HTYPE(field->type))
627627+ {
628628+ case PB_HTYPE_REQUIRED:
629629+ case PB_HTYPE_OPTIONAL:
630630+ case PB_HTYPE_ONEOF:
631631+ if (PB_LTYPE_IS_SUBMSG(field->type) && *(void**)field->pField != NULL)
632632+ {
633633+ /* Duplicate field, have to release the old allocation first. */
634634+ /* FIXME: Does this work correctly for oneofs? */
635635+ pb_release_single_field(field);
636636+ }
637637+638638+ if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
639639+ {
640640+ *(pb_size_t*)field->pSize = field->tag;
641641+ }
642642+643643+ if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
644644+ PB_LTYPE(field->type) == PB_LTYPE_BYTES)
645645+ {
646646+ /* pb_dec_string and pb_dec_bytes handle allocation themselves */
647647+ field->pData = field->pField;
648648+ return decode_basic_field(stream, wire_type, field);
649649+ }
650650+ else
651651+ {
652652+ if (!allocate_field(stream, field->pField, field->data_size, 1))
653653+ return false;
654654+655655+ field->pData = *(void**)field->pField;
656656+ initialize_pointer_field(field->pData, field);
657657+ return decode_basic_field(stream, wire_type, field);
658658+ }
659659+660660+ case PB_HTYPE_REPEATED:
661661+ if (wire_type == PB_WT_STRING
662662+ && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
663663+ {
664664+ /* Packed array, multiple items come in at once. */
665665+ bool status = true;
666666+ pb_size_t *size = (pb_size_t*)field->pSize;
667667+ size_t allocated_size = *size;
668668+ pb_istream_t substream;
669669+670670+ if (!pb_make_string_substream(stream, &substream))
671671+ return false;
672672+673673+ while (substream.bytes_left)
674674+ {
675675+ if (*size == PB_SIZE_MAX)
676676+ {
677677+#ifndef PB_NO_ERRMSG
678678+ stream->errmsg = "too many array entries";
679679+#endif
680680+ status = false;
681681+ break;
682682+ }
683683+684684+ if ((size_t)*size + 1 > allocated_size)
685685+ {
686686+ /* Allocate more storage. This tries to guess the
687687+ * number of remaining entries. Round the division
688688+ * upwards. */
689689+ size_t remain = (substream.bytes_left - 1) / field->data_size + 1;
690690+ if (remain < PB_SIZE_MAX - allocated_size)
691691+ allocated_size += remain;
692692+ else
693693+ allocated_size += 1;
694694+695695+ if (!allocate_field(&substream, field->pField, field->data_size, allocated_size))
696696+ {
697697+ status = false;
698698+ break;
699699+ }
700700+ }
701701+702702+ /* Decode the array entry */
703703+ field->pData = *(char**)field->pField + field->data_size * (*size);
704704+ if (field->pData == NULL)
705705+ {
706706+ /* Shouldn't happen, but satisfies static analyzers */
707707+ status = false;
708708+ break;
709709+ }
710710+ initialize_pointer_field(field->pData, field);
711711+ if (!decode_basic_field(&substream, PB_WT_PACKED, field))
712712+ {
713713+ status = false;
714714+ break;
715715+ }
716716+717717+ (*size)++;
718718+ }
719719+ if (!pb_close_string_substream(stream, &substream))
720720+ return false;
721721+722722+ return status;
723723+ }
724724+ else
725725+ {
726726+ /* Normal repeated field, i.e. only one item at a time. */
727727+ pb_size_t *size = (pb_size_t*)field->pSize;
728728+729729+ if (*size == PB_SIZE_MAX)
730730+ PB_RETURN_ERROR(stream, "too many array entries");
731731+732732+ if (!allocate_field(stream, field->pField, field->data_size, (size_t)(*size + 1)))
733733+ return false;
734734+735735+ field->pData = *(char**)field->pField + field->data_size * (*size);
736736+ (*size)++;
737737+ initialize_pointer_field(field->pData, field);
738738+ return decode_basic_field(stream, wire_type, field);
739739+ }
740740+741741+ default:
742742+ PB_RETURN_ERROR(stream, "invalid field type");
743743+ }
744744+#endif
745745+}
746746+747747+static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
748748+{
749749+ if (!field->descriptor->field_callback)
750750+ return pb_skip_field(stream, wire_type);
751751+752752+ if (wire_type == PB_WT_STRING)
753753+ {
754754+ pb_istream_t substream;
755755+ size_t prev_bytes_left;
756756+757757+ if (!pb_make_string_substream(stream, &substream))
758758+ return false;
759759+760760+ do
761761+ {
762762+ prev_bytes_left = substream.bytes_left;
763763+ if (!field->descriptor->field_callback(&substream, NULL, field))
764764+ {
765765+ PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg : "callback failed");
766766+ return false;
767767+ }
768768+ } while (substream.bytes_left > 0 && substream.bytes_left < prev_bytes_left);
769769+770770+ if (!pb_close_string_substream(stream, &substream))
771771+ return false;
772772+773773+ return true;
774774+ }
775775+ else
776776+ {
777777+ /* Copy the single scalar value to stack.
778778+ * This is required so that we can limit the stream length,
779779+ * which in turn allows to use same callback for packed and
780780+ * not-packed fields. */
781781+ pb_istream_t substream;
782782+ pb_byte_t buffer[10];
783783+ size_t size = sizeof(buffer);
784784+785785+ if (!read_raw_value(stream, wire_type, buffer, &size))
786786+ return false;
787787+ substream = pb_istream_from_buffer(buffer, size);
788788+789789+ return field->descriptor->field_callback(&substream, NULL, field);
790790+ }
791791+}
792792+793793+static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
794794+{
795795+#ifdef PB_ENABLE_MALLOC
796796+ /* When decoding an oneof field, check if there is old data that must be
797797+ * released first. */
798798+ if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
799799+ {
800800+ if (!pb_release_union_field(stream, field))
801801+ return false;
802802+ }
803803+#endif
804804+805805+ switch (PB_ATYPE(field->type))
806806+ {
807807+ case PB_ATYPE_STATIC:
808808+ return decode_static_field(stream, wire_type, field);
809809+810810+ case PB_ATYPE_POINTER:
811811+ return decode_pointer_field(stream, wire_type, field);
812812+813813+ case PB_ATYPE_CALLBACK:
814814+ return decode_callback_field(stream, wire_type, field);
815815+816816+ default:
817817+ PB_RETURN_ERROR(stream, "invalid field type");
818818+ }
819819+}
820820+821821+/* Default handler for extension fields. Expects to have a pb_msgdesc_t
822822+ * pointer in the extension->type->arg field, pointing to a message with
823823+ * only one field in it. */
824824+static bool checkreturn default_extension_decoder(pb_istream_t *stream,
825825+ pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
826826+{
827827+ pb_field_iter_t iter;
828828+829829+ if (!pb_field_iter_begin_extension(&iter, extension))
830830+ PB_RETURN_ERROR(stream, "invalid extension");
831831+832832+ if (iter.tag != tag || !iter.message)
833833+ return true;
834834+835835+ extension->found = true;
836836+ return decode_field(stream, wire_type, &iter);
837837+}
838838+839839+/* Try to decode an unknown field as an extension field. Tries each extension
840840+ * decoder in turn, until one of them handles the field or loop ends. */
841841+static bool checkreturn decode_extension(pb_istream_t *stream,
842842+ uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension)
843843+{
844844+ size_t pos = stream->bytes_left;
845845+846846+ while (extension != NULL && pos == stream->bytes_left)
847847+ {
848848+ bool status;
849849+ if (extension->type->decode)
850850+ status = extension->type->decode(stream, extension, tag, wire_type);
851851+ else
852852+ status = default_extension_decoder(stream, extension, tag, wire_type);
853853+854854+ if (!status)
855855+ return false;
856856+857857+ extension = extension->next;
858858+ }
859859+860860+ return true;
861861+}
862862+863863+/* Initialize message fields to default values, recursively */
864864+static bool pb_field_set_to_default(pb_field_iter_t *field)
865865+{
866866+ pb_type_t type;
867867+ type = field->type;
868868+869869+ if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
870870+ {
871871+ pb_extension_t *ext = *(pb_extension_t* const *)field->pData;
872872+ while (ext != NULL)
873873+ {
874874+ pb_field_iter_t ext_iter;
875875+ if (pb_field_iter_begin_extension(&ext_iter, ext))
876876+ {
877877+ ext->found = false;
878878+ if (!pb_message_set_to_defaults(&ext_iter))
879879+ return false;
880880+ }
881881+ ext = ext->next;
882882+ }
883883+ }
884884+ else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
885885+ {
886886+ bool init_data = true;
887887+ if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
888888+ {
889889+ /* Set has_field to false. Still initialize the optional field
890890+ * itself also. */
891891+ *(bool*)field->pSize = false;
892892+ }
893893+ else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
894894+ PB_HTYPE(type) == PB_HTYPE_ONEOF)
895895+ {
896896+ /* REPEATED: Set array count to 0, no need to initialize contents.
897897+ ONEOF: Set which_field to 0. */
898898+ *(pb_size_t*)field->pSize = 0;
899899+ init_data = false;
900900+ }
901901+902902+ if (init_data)
903903+ {
904904+ if (PB_LTYPE_IS_SUBMSG(field->type) &&
905905+ (field->submsg_desc->default_value != NULL ||
906906+ field->submsg_desc->field_callback != NULL ||
907907+ field->submsg_desc->submsg_info[0] != NULL))
908908+ {
909909+ /* Initialize submessage to defaults.
910910+ * Only needed if it has default values
911911+ * or callback/submessage fields. */
912912+ pb_field_iter_t submsg_iter;
913913+ if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
914914+ {
915915+ if (!pb_message_set_to_defaults(&submsg_iter))
916916+ return false;
917917+ }
918918+ }
919919+ else
920920+ {
921921+ /* Initialize to zeros */
922922+ memset(field->pData, 0, (size_t)field->data_size);
923923+ }
924924+ }
925925+ }
926926+ else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
927927+ {
928928+ /* Initialize the pointer to NULL. */
929929+ *(void**)field->pField = NULL;
930930+931931+ /* Initialize array count to 0. */
932932+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
933933+ PB_HTYPE(type) == PB_HTYPE_ONEOF)
934934+ {
935935+ *(pb_size_t*)field->pSize = 0;
936936+ }
937937+ }
938938+ else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
939939+ {
940940+ /* Don't overwrite callback */
941941+ }
942942+943943+ return true;
944944+}
945945+946946+static bool pb_message_set_to_defaults(pb_field_iter_t *iter)
947947+{
948948+ pb_istream_t defstream = PB_ISTREAM_EMPTY;
949949+ uint32_t tag = 0;
950950+ pb_wire_type_t wire_type = PB_WT_VARINT;
951951+ bool eof;
952952+953953+ if (iter->descriptor->default_value)
954954+ {
955955+ defstream = pb_istream_from_buffer(iter->descriptor->default_value, (size_t)-1);
956956+ if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
957957+ return false;
958958+ }
959959+960960+ do
961961+ {
962962+ if (!pb_field_set_to_default(iter))
963963+ return false;
964964+965965+ if (tag != 0 && iter->tag == tag)
966966+ {
967967+ /* We have a default value for this field in the defstream */
968968+ if (!decode_field(&defstream, wire_type, iter))
969969+ return false;
970970+ if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
971971+ return false;
972972+973973+ if (iter->pSize)
974974+ *(bool*)iter->pSize = false;
975975+ }
976976+ } while (pb_field_iter_next(iter));
977977+978978+ return true;
979979+}
980980+981981+/*********************
982982+ * Decode all fields *
983983+ *********************/
984984+985985+static bool checkreturn pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
986986+{
987987+ uint32_t extension_range_start = 0;
988988+ pb_extension_t *extensions = NULL;
989989+990990+ /* 'fixed_count_field' and 'fixed_count_size' track position of a repeated fixed
991991+ * count field. This can only handle _one_ repeated fixed count field that
992992+ * is unpacked and unordered among other (non repeated fixed count) fields.
993993+ */
994994+ pb_size_t fixed_count_field = PB_SIZE_MAX;
995995+ pb_size_t fixed_count_size = 0;
996996+ pb_size_t fixed_count_total_size = 0;
997997+998998+ pb_fields_seen_t fields_seen = {{0, 0}};
999999+ const uint32_t allbits = ~(uint32_t)0;
10001000+ pb_field_iter_t iter;
10011001+10021002+ if (pb_field_iter_begin(&iter, fields, dest_struct))
10031003+ {
10041004+ if ((flags & PB_DECODE_NOINIT) == 0)
10051005+ {
10061006+ if (!pb_message_set_to_defaults(&iter))
10071007+ PB_RETURN_ERROR(stream, "failed to set defaults");
10081008+ }
10091009+ }
10101010+10111011+ while (stream->bytes_left)
10121012+ {
10131013+ uint32_t tag;
10141014+ pb_wire_type_t wire_type;
10151015+ bool eof;
10161016+10171017+ if (!pb_decode_tag(stream, &wire_type, &tag, &eof))
10181018+ {
10191019+ if (eof)
10201020+ break;
10211021+ else
10221022+ return false;
10231023+ }
10241024+10251025+ if (tag == 0)
10261026+ {
10271027+ if (flags & PB_DECODE_NULLTERMINATED)
10281028+ {
10291029+ break;
10301030+ }
10311031+ else
10321032+ {
10331033+ PB_RETURN_ERROR(stream, "zero tag");
10341034+ }
10351035+ }
10361036+10371037+ if (!pb_field_iter_find(&iter, tag) || PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
10381038+ {
10391039+ /* No match found, check if it matches an extension. */
10401040+ if (extension_range_start == 0)
10411041+ {
10421042+ if (pb_field_iter_find_extension(&iter))
10431043+ {
10441044+ extensions = *(pb_extension_t* const *)iter.pData;
10451045+ extension_range_start = iter.tag;
10461046+ }
10471047+10481048+ if (!extensions)
10491049+ {
10501050+ extension_range_start = (uint32_t)-1;
10511051+ }
10521052+ }
10531053+10541054+ if (tag >= extension_range_start)
10551055+ {
10561056+ size_t pos = stream->bytes_left;
10571057+10581058+ if (!decode_extension(stream, tag, wire_type, extensions))
10591059+ return false;
10601060+10611061+ if (pos != stream->bytes_left)
10621062+ {
10631063+ /* The field was handled */
10641064+ continue;
10651065+ }
10661066+ }
10671067+10681068+ /* No match found, skip data */
10691069+ if (!pb_skip_field(stream, wire_type))
10701070+ return false;
10711071+ continue;
10721072+ }
10731073+10741074+ /* If a repeated fixed count field was found, get size from
10751075+ * 'fixed_count_field' as there is no counter contained in the struct.
10761076+ */
10771077+ if (PB_HTYPE(iter.type) == PB_HTYPE_REPEATED && iter.pSize == &iter.array_size)
10781078+ {
10791079+ if (fixed_count_field != iter.index) {
10801080+ /* If the new fixed count field does not match the previous one,
10811081+ * check that the previous one is NULL or that it finished
10821082+ * receiving all the expected data.
10831083+ */
10841084+ if (fixed_count_field != PB_SIZE_MAX &&
10851085+ fixed_count_size != fixed_count_total_size)
10861086+ {
10871087+ PB_RETURN_ERROR(stream, "wrong size for fixed count field");
10881088+ }
10891089+10901090+ fixed_count_field = iter.index;
10911091+ fixed_count_size = 0;
10921092+ fixed_count_total_size = iter.array_size;
10931093+ }
10941094+10951095+ iter.pSize = &fixed_count_size;
10961096+ }
10971097+10981098+ if (PB_HTYPE(iter.type) == PB_HTYPE_REQUIRED
10991099+ && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
11001100+ {
11011101+ uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
11021102+ fields_seen.bitfield[iter.required_field_index >> 5] |= tmp;
11031103+ }
11041104+11051105+ if (!decode_field(stream, wire_type, &iter))
11061106+ return false;
11071107+ }
11081108+11091109+ /* Check that all elements of the last decoded fixed count field were present. */
11101110+ if (fixed_count_field != PB_SIZE_MAX &&
11111111+ fixed_count_size != fixed_count_total_size)
11121112+ {
11131113+ PB_RETURN_ERROR(stream, "wrong size for fixed count field");
11141114+ }
11151115+11161116+ /* Check that all required fields were present. */
11171117+ {
11181118+ pb_size_t req_field_count = iter.descriptor->required_field_count;
11191119+11201120+ if (req_field_count > 0)
11211121+ {
11221122+ pb_size_t i;
11231123+11241124+ if (req_field_count > PB_MAX_REQUIRED_FIELDS)
11251125+ req_field_count = PB_MAX_REQUIRED_FIELDS;
11261126+11271127+ /* Check the whole words */
11281128+ for (i = 0; i < (req_field_count >> 5); i++)
11291129+ {
11301130+ if (fields_seen.bitfield[i] != allbits)
11311131+ PB_RETURN_ERROR(stream, "missing required field");
11321132+ }
11331133+11341134+ /* Check the remaining bits (if any) */
11351135+ if ((req_field_count & 31) != 0)
11361136+ {
11371137+ if (fields_seen.bitfield[req_field_count >> 5] !=
11381138+ (allbits >> (uint_least8_t)(32 - (req_field_count & 31))))
11391139+ {
11401140+ PB_RETURN_ERROR(stream, "missing required field");
11411141+ }
11421142+ }
11431143+ }
11441144+ }
11451145+11461146+ return true;
11471147+}
11481148+11491149+bool checkreturn pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
11501150+{
11511151+ bool status;
11521152+11531153+ if ((flags & PB_DECODE_DELIMITED) == 0)
11541154+ {
11551155+ status = pb_decode_inner(stream, fields, dest_struct, flags);
11561156+ }
11571157+ else
11581158+ {
11591159+ pb_istream_t substream;
11601160+ if (!pb_make_string_substream(stream, &substream))
11611161+ return false;
11621162+11631163+ status = pb_decode_inner(&substream, fields, dest_struct, flags);
11641164+11651165+ if (!pb_close_string_substream(stream, &substream))
11661166+ return false;
11671167+ }
11681168+11691169+#ifdef PB_ENABLE_MALLOC
11701170+ if (!status)
11711171+ pb_release(fields, dest_struct);
11721172+#endif
11731173+11741174+ return status;
11751175+}
11761176+11771177+bool checkreturn pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
11781178+{
11791179+ bool status;
11801180+11811181+ status = pb_decode_inner(stream, fields, dest_struct, 0);
11821182+11831183+#ifdef PB_ENABLE_MALLOC
11841184+ if (!status)
11851185+ pb_release(fields, dest_struct);
11861186+#endif
11871187+11881188+ return status;
11891189+}
11901190+11911191+#ifdef PB_ENABLE_MALLOC
11921192+/* Given an oneof field, if there has already been a field inside this oneof,
11931193+ * release it before overwriting with a different one. */
11941194+static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field)
11951195+{
11961196+ pb_field_iter_t old_field = *field;
11971197+ pb_size_t old_tag = *(pb_size_t*)field->pSize; /* Previous which_ value */
11981198+ pb_size_t new_tag = field->tag; /* New which_ value */
11991199+12001200+ if (old_tag == 0)
12011201+ return true; /* Ok, no old data in union */
12021202+12031203+ if (old_tag == new_tag)
12041204+ return true; /* Ok, old data is of same type => merge */
12051205+12061206+ /* Release old data. The find can fail if the message struct contains
12071207+ * invalid data. */
12081208+ if (!pb_field_iter_find(&old_field, old_tag))
12091209+ PB_RETURN_ERROR(stream, "invalid union tag");
12101210+12111211+ pb_release_single_field(&old_field);
12121212+12131213+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
12141214+ {
12151215+ /* Initialize the pointer to NULL to make sure it is valid
12161216+ * even in case of error return. */
12171217+ *(void**)field->pField = NULL;
12181218+ field->pData = NULL;
12191219+ }
12201220+12211221+ return true;
12221222+}
12231223+12241224+static void pb_release_single_field(pb_field_iter_t *field)
12251225+{
12261226+ pb_type_t type;
12271227+ type = field->type;
12281228+12291229+ if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
12301230+ {
12311231+ if (*(pb_size_t*)field->pSize != field->tag)
12321232+ return; /* This is not the current field in the union */
12331233+ }
12341234+12351235+ /* Release anything contained inside an extension or submsg.
12361236+ * This has to be done even if the submsg itself is statically
12371237+ * allocated. */
12381238+ if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
12391239+ {
12401240+ /* Release fields from all extensions in the linked list */
12411241+ pb_extension_t *ext = *(pb_extension_t**)field->pData;
12421242+ while (ext != NULL)
12431243+ {
12441244+ pb_field_iter_t ext_iter;
12451245+ if (pb_field_iter_begin_extension(&ext_iter, ext))
12461246+ {
12471247+ pb_release_single_field(&ext_iter);
12481248+ }
12491249+ ext = ext->next;
12501250+ }
12511251+ }
12521252+ else if (PB_LTYPE_IS_SUBMSG(type) && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
12531253+ {
12541254+ /* Release fields in submessage or submsg array */
12551255+ pb_size_t count = 1;
12561256+12571257+ if (PB_ATYPE(type) == PB_ATYPE_POINTER)
12581258+ {
12591259+ field->pData = *(void**)field->pField;
12601260+ }
12611261+ else
12621262+ {
12631263+ field->pData = field->pField;
12641264+ }
12651265+12661266+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
12671267+ {
12681268+ count = *(pb_size_t*)field->pSize;
12691269+12701270+ if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > field->array_size)
12711271+ {
12721272+ /* Protect against corrupted _count fields */
12731273+ count = field->array_size;
12741274+ }
12751275+ }
12761276+12771277+ if (field->pData)
12781278+ {
12791279+ for (; count > 0; count--)
12801280+ {
12811281+ pb_release(field->submsg_desc, field->pData);
12821282+ field->pData = (char*)field->pData + field->data_size;
12831283+ }
12841284+ }
12851285+ }
12861286+12871287+ if (PB_ATYPE(type) == PB_ATYPE_POINTER)
12881288+ {
12891289+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
12901290+ (PB_LTYPE(type) == PB_LTYPE_STRING ||
12911291+ PB_LTYPE(type) == PB_LTYPE_BYTES))
12921292+ {
12931293+ /* Release entries in repeated string or bytes array */
12941294+ void **pItem = *(void***)field->pField;
12951295+ pb_size_t count = *(pb_size_t*)field->pSize;
12961296+ for (; count > 0; count--)
12971297+ {
12981298+ pb_free(*pItem);
12991299+ *pItem++ = NULL;
13001300+ }
13011301+ }
13021302+13031303+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
13041304+ {
13051305+ /* We are going to release the array, so set the size to 0 */
13061306+ *(pb_size_t*)field->pSize = 0;
13071307+ }
13081308+13091309+ /* Release main pointer */
13101310+ pb_free(*(void**)field->pField);
13111311+ *(void**)field->pField = NULL;
13121312+ }
13131313+}
13141314+13151315+void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
13161316+{
13171317+ pb_field_iter_t iter;
13181318+13191319+ if (!dest_struct)
13201320+ return; /* Ignore NULL pointers, similar to free() */
13211321+13221322+ if (!pb_field_iter_begin(&iter, fields, dest_struct))
13231323+ return; /* Empty message type */
13241324+13251325+ do
13261326+ {
13271327+ pb_release_single_field(&iter);
13281328+ } while (pb_field_iter_next(&iter));
13291329+}
13301330+#endif
13311331+13321332+/* Field decoders */
13331333+13341334+bool pb_decode_bool(pb_istream_t *stream, bool *dest)
13351335+{
13361336+ uint32_t value;
13371337+ if (!pb_decode_varint32(stream, &value))
13381338+ return false;
13391339+13401340+ *(bool*)dest = (value != 0);
13411341+ return true;
13421342+}
13431343+13441344+bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
13451345+{
13461346+ pb_uint64_t value;
13471347+ if (!pb_decode_varint(stream, &value))
13481348+ return false;
13491349+13501350+ if (value & 1)
13511351+ *dest = (pb_int64_t)(~(value >> 1));
13521352+ else
13531353+ *dest = (pb_int64_t)(value >> 1);
13541354+13551355+ return true;
13561356+}
13571357+13581358+bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
13591359+{
13601360+ union {
13611361+ uint32_t fixed32;
13621362+ pb_byte_t bytes[4];
13631363+ } u;
13641364+13651365+ if (!pb_read(stream, u.bytes, 4))
13661366+ return false;
13671367+13681368+#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
13691369+ /* fast path - if we know that we're on little endian, assign directly */
13701370+ *(uint32_t*)dest = u.fixed32;
13711371+#else
13721372+ *(uint32_t*)dest = ((uint32_t)u.bytes[0] << 0) |
13731373+ ((uint32_t)u.bytes[1] << 8) |
13741374+ ((uint32_t)u.bytes[2] << 16) |
13751375+ ((uint32_t)u.bytes[3] << 24);
13761376+#endif
13771377+ return true;
13781378+}
13791379+13801380+#ifndef PB_WITHOUT_64BIT
13811381+bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
13821382+{
13831383+ union {
13841384+ uint64_t fixed64;
13851385+ pb_byte_t bytes[8];
13861386+ } u;
13871387+13881388+ if (!pb_read(stream, u.bytes, 8))
13891389+ return false;
13901390+13911391+#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
13921392+ /* fast path - if we know that we're on little endian, assign directly */
13931393+ *(uint64_t*)dest = u.fixed64;
13941394+#else
13951395+ *(uint64_t*)dest = ((uint64_t)u.bytes[0] << 0) |
13961396+ ((uint64_t)u.bytes[1] << 8) |
13971397+ ((uint64_t)u.bytes[2] << 16) |
13981398+ ((uint64_t)u.bytes[3] << 24) |
13991399+ ((uint64_t)u.bytes[4] << 32) |
14001400+ ((uint64_t)u.bytes[5] << 40) |
14011401+ ((uint64_t)u.bytes[6] << 48) |
14021402+ ((uint64_t)u.bytes[7] << 56);
14031403+#endif
14041404+ return true;
14051405+}
14061406+#endif
14071407+14081408+static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field)
14091409+{
14101410+ return pb_decode_bool(stream, (bool*)field->pData);
14111411+}
14121412+14131413+static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field)
14141414+{
14151415+ if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
14161416+ {
14171417+ pb_uint64_t value, clamped;
14181418+ if (!pb_decode_varint(stream, &value))
14191419+ return false;
14201420+14211421+ /* Cast to the proper field size, while checking for overflows */
14221422+ if (field->data_size == sizeof(pb_uint64_t))
14231423+ clamped = *(pb_uint64_t*)field->pData = value;
14241424+ else if (field->data_size == sizeof(uint32_t))
14251425+ clamped = *(uint32_t*)field->pData = (uint32_t)value;
14261426+ else if (field->data_size == sizeof(uint_least16_t))
14271427+ clamped = *(uint_least16_t*)field->pData = (uint_least16_t)value;
14281428+ else if (field->data_size == sizeof(uint_least8_t))
14291429+ clamped = *(uint_least8_t*)field->pData = (uint_least8_t)value;
14301430+ else
14311431+ PB_RETURN_ERROR(stream, "invalid data_size");
14321432+14331433+ if (clamped != value)
14341434+ PB_RETURN_ERROR(stream, "integer too large");
14351435+14361436+ return true;
14371437+ }
14381438+ else
14391439+ {
14401440+ pb_uint64_t value;
14411441+ pb_int64_t svalue;
14421442+ pb_int64_t clamped;
14431443+14441444+ if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
14451445+ {
14461446+ if (!pb_decode_svarint(stream, &svalue))
14471447+ return false;
14481448+ }
14491449+ else
14501450+ {
14511451+ if (!pb_decode_varint(stream, &value))
14521452+ return false;
14531453+14541454+ /* See issue 97: Google's C++ protobuf allows negative varint values to
14551455+ * be cast as int32_t, instead of the int64_t that should be used when
14561456+ * encoding. Nanopb versions before 0.2.5 had a bug in encoding. In order to
14571457+ * not break decoding of such messages, we cast <=32 bit fields to
14581458+ * int32_t first to get the sign correct.
14591459+ */
14601460+ if (field->data_size == sizeof(pb_int64_t))
14611461+ svalue = (pb_int64_t)value;
14621462+ else
14631463+ svalue = (int32_t)value;
14641464+ }
14651465+14661466+ /* Cast to the proper field size, while checking for overflows */
14671467+ if (field->data_size == sizeof(pb_int64_t))
14681468+ clamped = *(pb_int64_t*)field->pData = svalue;
14691469+ else if (field->data_size == sizeof(int32_t))
14701470+ clamped = *(int32_t*)field->pData = (int32_t)svalue;
14711471+ else if (field->data_size == sizeof(int_least16_t))
14721472+ clamped = *(int_least16_t*)field->pData = (int_least16_t)svalue;
14731473+ else if (field->data_size == sizeof(int_least8_t))
14741474+ clamped = *(int_least8_t*)field->pData = (int_least8_t)svalue;
14751475+ else
14761476+ PB_RETURN_ERROR(stream, "invalid data_size");
14771477+14781478+ if (clamped != svalue)
14791479+ PB_RETURN_ERROR(stream, "integer too large");
14801480+14811481+ return true;
14821482+ }
14831483+}
14841484+14851485+static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
14861486+{
14871487+ uint32_t size;
14881488+ size_t alloc_size;
14891489+ pb_bytes_array_t *dest;
14901490+14911491+ if (!pb_decode_varint32(stream, &size))
14921492+ return false;
14931493+14941494+ if (size > PB_SIZE_MAX)
14951495+ PB_RETURN_ERROR(stream, "bytes overflow");
14961496+14971497+ alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
14981498+ if (size > alloc_size)
14991499+ PB_RETURN_ERROR(stream, "size too large");
15001500+15011501+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
15021502+ {
15031503+#ifndef PB_ENABLE_MALLOC
15041504+ PB_RETURN_ERROR(stream, "no malloc support");
15051505+#else
15061506+ if (stream->bytes_left < size)
15071507+ PB_RETURN_ERROR(stream, "end-of-stream");
15081508+15091509+ if (!allocate_field(stream, field->pData, alloc_size, 1))
15101510+ return false;
15111511+ dest = *(pb_bytes_array_t**)field->pData;
15121512+#endif
15131513+ }
15141514+ else
15151515+ {
15161516+ if (alloc_size > field->data_size)
15171517+ PB_RETURN_ERROR(stream, "bytes overflow");
15181518+ dest = (pb_bytes_array_t*)field->pData;
15191519+ }
15201520+15211521+ dest->size = (pb_size_t)size;
15221522+ return pb_read(stream, dest->bytes, (size_t)size);
15231523+}
15241524+15251525+static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field)
15261526+{
15271527+ uint32_t size;
15281528+ size_t alloc_size;
15291529+ pb_byte_t *dest = (pb_byte_t*)field->pData;
15301530+15311531+ if (!pb_decode_varint32(stream, &size))
15321532+ return false;
15331533+15341534+ if (size == (uint32_t)-1)
15351535+ PB_RETURN_ERROR(stream, "size too large");
15361536+15371537+ /* Space for null terminator */
15381538+ alloc_size = (size_t)(size + 1);
15391539+15401540+ if (alloc_size < size)
15411541+ PB_RETURN_ERROR(stream, "size too large");
15421542+15431543+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
15441544+ {
15451545+#ifndef PB_ENABLE_MALLOC
15461546+ PB_RETURN_ERROR(stream, "no malloc support");
15471547+#else
15481548+ if (stream->bytes_left < size)
15491549+ PB_RETURN_ERROR(stream, "end-of-stream");
15501550+15511551+ if (!allocate_field(stream, field->pData, alloc_size, 1))
15521552+ return false;
15531553+ dest = *(pb_byte_t**)field->pData;
15541554+#endif
15551555+ }
15561556+ else
15571557+ {
15581558+ if (alloc_size > field->data_size)
15591559+ PB_RETURN_ERROR(stream, "string overflow");
15601560+ }
15611561+15621562+ dest[size] = 0;
15631563+15641564+ if (!pb_read(stream, dest, (size_t)size))
15651565+ return false;
15661566+15671567+#ifdef PB_VALIDATE_UTF8
15681568+ if (!pb_validate_utf8((const char*)dest))
15691569+ PB_RETURN_ERROR(stream, "invalid utf8");
15701570+#endif
15711571+15721572+ return true;
15731573+}
15741574+15751575+static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field)
15761576+{
15771577+ bool status = true;
15781578+ bool submsg_consumed = false;
15791579+ pb_istream_t substream;
15801580+15811581+ if (!pb_make_string_substream(stream, &substream))
15821582+ return false;
15831583+15841584+ if (field->submsg_desc == NULL)
15851585+ PB_RETURN_ERROR(stream, "invalid field descriptor");
15861586+15871587+ /* Submessages can have a separate message-level callback that is called
15881588+ * before decoding the message. Typically it is used to set callback fields
15891589+ * inside oneofs. */
15901590+ if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
15911591+ {
15921592+ /* Message callback is stored right before pSize. */
15931593+ pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
15941594+ if (callback->funcs.decode)
15951595+ {
15961596+ status = callback->funcs.decode(&substream, field, &callback->arg);
15971597+15981598+ if (substream.bytes_left == 0)
15991599+ {
16001600+ submsg_consumed = true;
16011601+ }
16021602+ }
16031603+ }
16041604+16051605+ /* Now decode the submessage contents */
16061606+ if (status && !submsg_consumed)
16071607+ {
16081608+ unsigned int flags = 0;
16091609+16101610+ /* Static required/optional fields are already initialized by top-level
16111611+ * pb_decode(), no need to initialize them again. */
16121612+ if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
16131613+ PB_HTYPE(field->type) != PB_HTYPE_REPEATED)
16141614+ {
16151615+ flags = PB_DECODE_NOINIT;
16161616+ }
16171617+16181618+ status = pb_decode_inner(&substream, field->submsg_desc, field->pData, flags);
16191619+ }
16201620+16211621+ if (!pb_close_string_substream(stream, &substream))
16221622+ return false;
16231623+16241624+ return status;
16251625+}
16261626+16271627+static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
16281628+{
16291629+ uint32_t size;
16301630+16311631+ if (!pb_decode_varint32(stream, &size))
16321632+ return false;
16331633+16341634+ if (size > PB_SIZE_MAX)
16351635+ PB_RETURN_ERROR(stream, "bytes overflow");
16361636+16371637+ if (size == 0)
16381638+ {
16391639+ /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
16401640+ memset(field->pData, 0, (size_t)field->data_size);
16411641+ return true;
16421642+ }
16431643+16441644+ if (size != field->data_size)
16451645+ PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
16461646+16471647+ return pb_read(stream, (pb_byte_t*)field->pData, (size_t)field->data_size);
16481648+}
16491649+16501650+#ifdef PB_CONVERT_DOUBLE_FLOAT
16511651+bool pb_decode_double_as_float(pb_istream_t *stream, float *dest)
16521652+{
16531653+ uint_least8_t sign;
16541654+ int exponent;
16551655+ uint32_t mantissa;
16561656+ uint64_t value;
16571657+ union { float f; uint32_t i; } out;
16581658+16591659+ if (!pb_decode_fixed64(stream, &value))
16601660+ return false;
16611661+16621662+ /* Decompose input value */
16631663+ sign = (uint_least8_t)((value >> 63) & 1);
16641664+ exponent = (int)((value >> 52) & 0x7FF) - 1023;
16651665+ mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */
16661666+16671667+ /* Figure if value is in range representable by floats. */
16681668+ if (exponent == 1024)
16691669+ {
16701670+ /* Special value */
16711671+ exponent = 128;
16721672+ mantissa >>= 1;
16731673+ }
16741674+ else
16751675+ {
16761676+ if (exponent > 127)
16771677+ {
16781678+ /* Too large, convert to infinity */
16791679+ exponent = 128;
16801680+ mantissa = 0;
16811681+ }
16821682+ else if (exponent < -150)
16831683+ {
16841684+ /* Too small, convert to zero */
16851685+ exponent = -127;
16861686+ mantissa = 0;
16871687+ }
16881688+ else if (exponent < -126)
16891689+ {
16901690+ /* Denormalized */
16911691+ mantissa |= 0x1000000;
16921692+ mantissa >>= (-126 - exponent);
16931693+ exponent = -127;
16941694+ }
16951695+16961696+ /* Round off mantissa */
16971697+ mantissa = (mantissa + 1) >> 1;
16981698+16991699+ /* Check if mantissa went over 2.0 */
17001700+ if (mantissa & 0x800000)
17011701+ {
17021702+ exponent += 1;
17031703+ mantissa &= 0x7FFFFF;
17041704+ mantissa >>= 1;
17051705+ }
17061706+ }
17071707+17081708+ /* Combine fields */
17091709+ out.i = mantissa;
17101710+ out.i |= (uint32_t)(exponent + 127) << 23;
17111711+ out.i |= (uint32_t)sign << 31;
17121712+17131713+ *dest = out.f;
17141714+ return true;
17151715+}
17161716+#endif
+199
src/external/nanopb/pb_decode.h
···11+/* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c.
22+ * The main function is pb_decode. You also need an input stream, and the
33+ * field descriptions created by nanopb_generator.py.
44+ */
55+66+#ifndef PB_DECODE_H_INCLUDED
77+#define PB_DECODE_H_INCLUDED
88+99+#include "pb.h"
1010+1111+#ifdef __cplusplus
1212+extern "C" {
1313+#endif
1414+1515+/* Structure for defining custom input streams. You will need to provide
1616+ * a callback function to read the bytes from your storage, which can be
1717+ * for example a file or a network socket.
1818+ *
1919+ * The callback must conform to these rules:
2020+ *
2121+ * 1) Return false on IO errors. This will cause decoding to abort.
2222+ * 2) You can use state to store your own data (e.g. buffer pointer),
2323+ * and rely on pb_read to verify that no-body reads past bytes_left.
2424+ * 3) Your callback may be used with substreams, in which case bytes_left
2525+ * is different than from the main stream. Don't use bytes_left to compute
2626+ * any pointers.
2727+ */
2828+struct pb_istream_s
2929+{
3030+#ifdef PB_BUFFER_ONLY
3131+ /* Callback pointer is not used in buffer-only configuration.
3232+ * Having an int pointer here allows binary compatibility but
3333+ * gives an error if someone tries to assign callback function.
3434+ */
3535+ int *callback;
3636+#else
3737+ bool (*callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count);
3838+#endif
3939+4040+ void *state; /* Free field for use by callback implementation */
4141+ size_t bytes_left;
4242+4343+#ifndef PB_NO_ERRMSG
4444+ const char *errmsg;
4545+#endif
4646+};
4747+4848+#ifndef PB_NO_ERRMSG
4949+#define PB_ISTREAM_EMPTY {0,0,0,0}
5050+#else
5151+#define PB_ISTREAM_EMPTY {0,0,0}
5252+#endif
5353+5454+/***************************
5555+ * Main decoding functions *
5656+ ***************************/
5757+5858+/* Decode a single protocol buffers message from input stream into a C structure.
5959+ * Returns true on success, false on any failure.
6060+ * The actual struct pointed to by dest must match the description in fields.
6161+ * Callback fields of the destination structure must be initialized by caller.
6262+ * All other fields will be initialized by this function.
6363+ *
6464+ * Example usage:
6565+ * MyMessage msg = {};
6666+ * uint8_t buffer[64];
6767+ * pb_istream_t stream;
6868+ *
6969+ * // ... read some data into buffer ...
7070+ *
7171+ * stream = pb_istream_from_buffer(buffer, count);
7272+ * pb_decode(&stream, MyMessage_fields, &msg);
7373+ */
7474+bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct);
7575+7676+/* Extended version of pb_decode, with several options to control
7777+ * the decoding process:
7878+ *
7979+ * PB_DECODE_NOINIT: Do not initialize the fields to default values.
8080+ * This is slightly faster if you do not need the default
8181+ * values and instead initialize the structure to 0 using
8282+ * e.g. memset(). This can also be used for merging two
8383+ * messages, i.e. combine already existing data with new
8484+ * values.
8585+ *
8686+ * PB_DECODE_DELIMITED: Input message starts with the message size as varint.
8787+ * Corresponds to parseDelimitedFrom() in Google's
8888+ * protobuf API.
8989+ *
9090+ * PB_DECODE_NULLTERMINATED: Stop reading when field tag is read as 0. This allows
9191+ * reading null terminated messages.
9292+ * NOTE: Until nanopb-0.4.0, pb_decode() also allows
9393+ * null-termination. This behaviour is not supported in
9494+ * most other protobuf implementations, so PB_DECODE_DELIMITED
9595+ * is a better option for compatibility.
9696+ *
9797+ * Multiple flags can be combined with bitwise or (| operator)
9898+ */
9999+#define PB_DECODE_NOINIT 0x01U
100100+#define PB_DECODE_DELIMITED 0x02U
101101+#define PB_DECODE_NULLTERMINATED 0x04U
102102+bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags);
103103+104104+/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
105105+#define pb_decode_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NOINIT)
106106+#define pb_decode_delimited(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED)
107107+#define pb_decode_delimited_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED | PB_DECODE_NOINIT)
108108+#define pb_decode_nullterminated(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NULLTERMINATED)
109109+110110+#ifdef PB_ENABLE_MALLOC
111111+/* Release any allocated pointer fields. If you use dynamic allocation, you should
112112+ * call this for any successfully decoded message when you are done with it. If
113113+ * pb_decode() returns with an error, the message is already released.
114114+ */
115115+void pb_release(const pb_msgdesc_t *fields, void *dest_struct);
116116+#else
117117+/* Allocation is not supported, so release is no-op */
118118+#define pb_release(fields, dest_struct) PB_UNUSED(fields); PB_UNUSED(dest_struct);
119119+#endif
120120+121121+122122+/**************************************
123123+ * Functions for manipulating streams *
124124+ **************************************/
125125+126126+/* Create an input stream for reading from a memory buffer.
127127+ *
128128+ * msglen should be the actual length of the message, not the full size of
129129+ * allocated buffer.
130130+ *
131131+ * Alternatively, you can use a custom stream that reads directly from e.g.
132132+ * a file or a network socket.
133133+ */
134134+pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen);
135135+136136+/* Function to read from a pb_istream_t. You can use this if you need to
137137+ * read some custom header data, or to read data in field callbacks.
138138+ */
139139+bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
140140+141141+142142+/************************************************
143143+ * Helper functions for writing field callbacks *
144144+ ************************************************/
145145+146146+/* Decode the tag for the next field in the stream. Gives the wire type and
147147+ * field tag. At end of the message, returns false and sets eof to true. */
148148+bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
149149+150150+/* Skip the field payload data, given the wire type. */
151151+bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
152152+153153+/* Decode an integer in the varint format. This works for enum, int32,
154154+ * int64, uint32 and uint64 field types. */
155155+#ifndef PB_WITHOUT_64BIT
156156+bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
157157+#else
158158+#define pb_decode_varint pb_decode_varint32
159159+#endif
160160+161161+/* Decode an integer in the varint format. This works for enum, int32,
162162+ * and uint32 field types. */
163163+bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
164164+165165+/* Decode a bool value in varint format. */
166166+bool pb_decode_bool(pb_istream_t *stream, bool *dest);
167167+168168+/* Decode an integer in the zig-zagged svarint format. This works for sint32
169169+ * and sint64. */
170170+#ifndef PB_WITHOUT_64BIT
171171+bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
172172+#else
173173+bool pb_decode_svarint(pb_istream_t *stream, int32_t *dest);
174174+#endif
175175+176176+/* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
177177+ * a 4-byte wide C variable. */
178178+bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
179179+180180+#ifndef PB_WITHOUT_64BIT
181181+/* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
182182+ * a 8-byte wide C variable. */
183183+bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
184184+#endif
185185+186186+#ifdef PB_CONVERT_DOUBLE_FLOAT
187187+/* Decode a double value into float variable. */
188188+bool pb_decode_double_as_float(pb_istream_t *stream, float *dest);
189189+#endif
190190+191191+/* Make a limited-length substream for reading a PB_WT_STRING field. */
192192+bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
193193+bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
194194+195195+#ifdef __cplusplus
196196+} /* extern "C" */
197197+#endif
198198+199199+#endif
+1000
src/external/nanopb/pb_encode.c
···11+/* pb_encode.c -- encode a protobuf using minimal resources
22+ *
33+ * 2011 Petteri Aimonen <jpa@kapsi.fi>
44+ */
55+66+#include "pb.h"
77+#include "pb_encode.h"
88+#include "pb_common.h"
99+1010+/* Use the GCC warn_unused_result attribute to check that all return values
1111+ * are propagated correctly. On other compilers and gcc before 3.4.0 just
1212+ * ignore the annotation.
1313+ */
1414+#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
1515+ #define checkreturn
1616+#else
1717+ #define checkreturn __attribute__((warn_unused_result))
1818+#endif
1919+2020+/**************************************
2121+ * Declarations internal to this file *
2222+ **************************************/
2323+static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
2424+static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field);
2525+static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field);
2626+static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field);
2727+static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field);
2828+static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field);
2929+static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field);
3030+static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension);
3131+static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high);
3232+static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field);
3333+static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field);
3434+static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field);
3535+static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
3636+static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field);
3737+static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field);
3838+static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field);
3939+4040+#ifdef PB_WITHOUT_64BIT
4141+#define pb_int64_t int32_t
4242+#define pb_uint64_t uint32_t
4343+#else
4444+#define pb_int64_t int64_t
4545+#define pb_uint64_t uint64_t
4646+#endif
4747+4848+/*******************************
4949+ * pb_ostream_t implementation *
5050+ *******************************/
5151+5252+static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
5353+{
5454+ pb_byte_t *dest = (pb_byte_t*)stream->state;
5555+ stream->state = dest + count;
5656+5757+ memcpy(dest, buf, count * sizeof(pb_byte_t));
5858+5959+ return true;
6060+}
6161+6262+pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
6363+{
6464+ pb_ostream_t stream;
6565+#ifdef PB_BUFFER_ONLY
6666+ /* In PB_BUFFER_ONLY configuration the callback pointer is just int*.
6767+ * NULL pointer marks a sizing field, so put a non-NULL value to mark a buffer stream.
6868+ */
6969+ static const int marker = 0;
7070+ stream.callback = ▮
7171+#else
7272+ stream.callback = &buf_write;
7373+#endif
7474+ stream.state = buf;
7575+ stream.max_size = bufsize;
7676+ stream.bytes_written = 0;
7777+#ifndef PB_NO_ERRMSG
7878+ stream.errmsg = NULL;
7979+#endif
8080+ return stream;
8181+}
8282+8383+bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
8484+{
8585+ if (count > 0 && stream->callback != NULL)
8686+ {
8787+ if (stream->bytes_written + count < stream->bytes_written ||
8888+ stream->bytes_written + count > stream->max_size)
8989+ {
9090+ PB_RETURN_ERROR(stream, "stream full");
9191+ }
9292+9393+#ifdef PB_BUFFER_ONLY
9494+ if (!buf_write(stream, buf, count))
9595+ PB_RETURN_ERROR(stream, "io error");
9696+#else
9797+ if (!stream->callback(stream, buf, count))
9898+ PB_RETURN_ERROR(stream, "io error");
9999+#endif
100100+ }
101101+102102+ stream->bytes_written += count;
103103+ return true;
104104+}
105105+106106+/*************************
107107+ * Encode a single field *
108108+ *************************/
109109+110110+/* Read a bool value without causing undefined behavior even if the value
111111+ * is invalid. See issue #434 and
112112+ * https://stackoverflow.com/questions/27661768/weird-results-for-conditional
113113+ */
114114+static bool safe_read_bool(const void *pSize)
115115+{
116116+ const char *p = (const char *)pSize;
117117+ size_t i;
118118+ for (i = 0; i < sizeof(bool); i++)
119119+ {
120120+ if (p[i] != 0)
121121+ return true;
122122+ }
123123+ return false;
124124+}
125125+126126+/* Encode a static array. Handles the size calculations and possible packing. */
127127+static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field)
128128+{
129129+ pb_size_t i;
130130+ pb_size_t count;
131131+#ifndef PB_ENCODE_ARRAYS_UNPACKED
132132+ size_t size;
133133+#endif
134134+135135+ count = *(pb_size_t*)field->pSize;
136136+137137+ if (count == 0)
138138+ return true;
139139+140140+ if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
141141+ PB_RETURN_ERROR(stream, "array max size exceeded");
142142+143143+#ifndef PB_ENCODE_ARRAYS_UNPACKED
144144+ /* We always pack arrays if the datatype allows it. */
145145+ if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
146146+ {
147147+ if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
148148+ return false;
149149+150150+ /* Determine the total size of packed array. */
151151+ if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
152152+ {
153153+ size = 4 * (size_t)count;
154154+ }
155155+ else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
156156+ {
157157+ size = 8 * (size_t)count;
158158+ }
159159+ else
160160+ {
161161+ pb_ostream_t sizestream = PB_OSTREAM_SIZING;
162162+ void *pData_orig = field->pData;
163163+ for (i = 0; i < count; i++)
164164+ {
165165+ if (!pb_enc_varint(&sizestream, field))
166166+ PB_RETURN_ERROR(stream, PB_GET_ERROR(&sizestream));
167167+ field->pData = (char*)field->pData + field->data_size;
168168+ }
169169+ field->pData = pData_orig;
170170+ size = sizestream.bytes_written;
171171+ }
172172+173173+ if (!pb_encode_varint(stream, (pb_uint64_t)size))
174174+ return false;
175175+176176+ if (stream->callback == NULL)
177177+ return pb_write(stream, NULL, size); /* Just sizing.. */
178178+179179+ /* Write the data */
180180+ for (i = 0; i < count; i++)
181181+ {
182182+ if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32 || PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
183183+ {
184184+ if (!pb_enc_fixed(stream, field))
185185+ return false;
186186+ }
187187+ else
188188+ {
189189+ if (!pb_enc_varint(stream, field))
190190+ return false;
191191+ }
192192+193193+ field->pData = (char*)field->pData + field->data_size;
194194+ }
195195+ }
196196+ else /* Unpacked fields */
197197+#endif
198198+ {
199199+ for (i = 0; i < count; i++)
200200+ {
201201+ /* Normally the data is stored directly in the array entries, but
202202+ * for pointer-type string and bytes fields, the array entries are
203203+ * actually pointers themselves also. So we have to dereference once
204204+ * more to get to the actual data. */
205205+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
206206+ (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
207207+ PB_LTYPE(field->type) == PB_LTYPE_BYTES))
208208+ {
209209+ bool status;
210210+ void *pData_orig = field->pData;
211211+ field->pData = *(void* const*)field->pData;
212212+213213+ if (!field->pData)
214214+ {
215215+ /* Null pointer in array is treated as empty string / bytes */
216216+ status = pb_encode_tag_for_field(stream, field) &&
217217+ pb_encode_varint(stream, 0);
218218+ }
219219+ else
220220+ {
221221+ status = encode_basic_field(stream, field);
222222+ }
223223+224224+ field->pData = pData_orig;
225225+226226+ if (!status)
227227+ return false;
228228+ }
229229+ else
230230+ {
231231+ if (!encode_basic_field(stream, field))
232232+ return false;
233233+ }
234234+ field->pData = (char*)field->pData + field->data_size;
235235+ }
236236+ }
237237+238238+ return true;
239239+}
240240+241241+/* In proto3, all fields are optional and are only encoded if their value is "non-zero".
242242+ * This function implements the check for the zero value. */
243243+static bool checkreturn pb_check_proto3_default_value(const pb_field_iter_t *field)
244244+{
245245+ pb_type_t type = field->type;
246246+247247+ if (PB_ATYPE(type) == PB_ATYPE_STATIC)
248248+ {
249249+ if (PB_HTYPE(type) == PB_HTYPE_REQUIRED)
250250+ {
251251+ /* Required proto2 fields inside proto3 submessage, pretty rare case */
252252+ return false;
253253+ }
254254+ else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
255255+ {
256256+ /* Repeated fields inside proto3 submessage: present if count != 0 */
257257+ return *(const pb_size_t*)field->pSize == 0;
258258+ }
259259+ else if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
260260+ {
261261+ /* Oneof fields */
262262+ return *(const pb_size_t*)field->pSize == 0;
263263+ }
264264+ else if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
265265+ {
266266+ /* Proto2 optional fields inside proto3 message, or proto3
267267+ * submessage fields. */
268268+ return safe_read_bool(field->pSize) == false;
269269+ }
270270+ else if (field->descriptor->default_value)
271271+ {
272272+ /* Proto3 messages do not have default values, but proto2 messages
273273+ * can contain optional fields without has_fields (generator option 'proto3').
274274+ * In this case they must always be encoded, to make sure that the
275275+ * non-zero default value is overwritten.
276276+ */
277277+ return false;
278278+ }
279279+280280+ /* Rest is proto3 singular fields */
281281+ if (PB_LTYPE(type) <= PB_LTYPE_LAST_PACKABLE)
282282+ {
283283+ /* Simple integer / float fields */
284284+ pb_size_t i;
285285+ const char *p = (const char*)field->pData;
286286+ for (i = 0; i < field->data_size; i++)
287287+ {
288288+ if (p[i] != 0)
289289+ {
290290+ return false;
291291+ }
292292+ }
293293+294294+ return true;
295295+ }
296296+ else if (PB_LTYPE(type) == PB_LTYPE_BYTES)
297297+ {
298298+ const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)field->pData;
299299+ return bytes->size == 0;
300300+ }
301301+ else if (PB_LTYPE(type) == PB_LTYPE_STRING)
302302+ {
303303+ return *(const char*)field->pData == '\0';
304304+ }
305305+ else if (PB_LTYPE(type) == PB_LTYPE_FIXED_LENGTH_BYTES)
306306+ {
307307+ /* Fixed length bytes is only empty if its length is fixed
308308+ * as 0. Which would be pretty strange, but we can check
309309+ * it anyway. */
310310+ return field->data_size == 0;
311311+ }
312312+ else if (PB_LTYPE_IS_SUBMSG(type))
313313+ {
314314+ /* Check all fields in the submessage to find if any of them
315315+ * are non-zero. The comparison cannot be done byte-per-byte
316316+ * because the C struct may contain padding bytes that must
317317+ * be skipped. Note that usually proto3 submessages have
318318+ * a separate has_field that is checked earlier in this if.
319319+ */
320320+ pb_field_iter_t iter;
321321+ if (pb_field_iter_begin(&iter, field->submsg_desc, field->pData))
322322+ {
323323+ do
324324+ {
325325+ if (!pb_check_proto3_default_value(&iter))
326326+ {
327327+ return false;
328328+ }
329329+ } while (pb_field_iter_next(&iter));
330330+ }
331331+ return true;
332332+ }
333333+ }
334334+ else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
335335+ {
336336+ return field->pData == NULL;
337337+ }
338338+ else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
339339+ {
340340+ if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
341341+ {
342342+ const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
343343+ return extension == NULL;
344344+ }
345345+ else if (field->descriptor->field_callback == pb_default_field_callback)
346346+ {
347347+ pb_callback_t *pCallback = (pb_callback_t*)field->pData;
348348+ return pCallback->funcs.encode == NULL;
349349+ }
350350+ else
351351+ {
352352+ return field->descriptor->field_callback == NULL;
353353+ }
354354+ }
355355+356356+ return false; /* Not typically reached, safe default for weird special cases. */
357357+}
358358+359359+/* Encode a field with static or pointer allocation, i.e. one whose data
360360+ * is available to the encoder directly. */
361361+static bool checkreturn encode_basic_field(pb_ostream_t *stream, const pb_field_iter_t *field)
362362+{
363363+ if (!field->pData)
364364+ {
365365+ /* Missing pointer field */
366366+ return true;
367367+ }
368368+369369+ if (!pb_encode_tag_for_field(stream, field))
370370+ return false;
371371+372372+ switch (PB_LTYPE(field->type))
373373+ {
374374+ case PB_LTYPE_BOOL:
375375+ return pb_enc_bool(stream, field);
376376+377377+ case PB_LTYPE_VARINT:
378378+ case PB_LTYPE_UVARINT:
379379+ case PB_LTYPE_SVARINT:
380380+ return pb_enc_varint(stream, field);
381381+382382+ case PB_LTYPE_FIXED32:
383383+ case PB_LTYPE_FIXED64:
384384+ return pb_enc_fixed(stream, field);
385385+386386+ case PB_LTYPE_BYTES:
387387+ return pb_enc_bytes(stream, field);
388388+389389+ case PB_LTYPE_STRING:
390390+ return pb_enc_string(stream, field);
391391+392392+ case PB_LTYPE_SUBMESSAGE:
393393+ case PB_LTYPE_SUBMSG_W_CB:
394394+ return pb_enc_submessage(stream, field);
395395+396396+ case PB_LTYPE_FIXED_LENGTH_BYTES:
397397+ return pb_enc_fixed_length_bytes(stream, field);
398398+399399+ default:
400400+ PB_RETURN_ERROR(stream, "invalid field type");
401401+ }
402402+}
403403+404404+/* Encode a field with callback semantics. This means that a user function is
405405+ * called to provide and encode the actual data. */
406406+static bool checkreturn encode_callback_field(pb_ostream_t *stream, const pb_field_iter_t *field)
407407+{
408408+ if (field->descriptor->field_callback != NULL)
409409+ {
410410+ if (!field->descriptor->field_callback(NULL, stream, field))
411411+ PB_RETURN_ERROR(stream, "callback error");
412412+ }
413413+ return true;
414414+}
415415+416416+/* Encode a single field of any callback, pointer or static type. */
417417+static bool checkreturn encode_field(pb_ostream_t *stream, pb_field_iter_t *field)
418418+{
419419+ /* Check field presence */
420420+ if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
421421+ {
422422+ if (*(const pb_size_t*)field->pSize != field->tag)
423423+ {
424424+ /* Different type oneof field */
425425+ return true;
426426+ }
427427+ }
428428+ else if (PB_HTYPE(field->type) == PB_HTYPE_OPTIONAL)
429429+ {
430430+ if (field->pSize)
431431+ {
432432+ if (safe_read_bool(field->pSize) == false)
433433+ {
434434+ /* Missing optional field */
435435+ return true;
436436+ }
437437+ }
438438+ else if (PB_ATYPE(field->type) == PB_ATYPE_STATIC)
439439+ {
440440+ /* Proto3 singular field */
441441+ if (pb_check_proto3_default_value(field))
442442+ return true;
443443+ }
444444+ }
445445+446446+ if (!field->pData)
447447+ {
448448+ if (PB_HTYPE(field->type) == PB_HTYPE_REQUIRED)
449449+ PB_RETURN_ERROR(stream, "missing required field");
450450+451451+ /* Pointer field set to NULL */
452452+ return true;
453453+ }
454454+455455+ /* Then encode field contents */
456456+ if (PB_ATYPE(field->type) == PB_ATYPE_CALLBACK)
457457+ {
458458+ return encode_callback_field(stream, field);
459459+ }
460460+ else if (PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
461461+ {
462462+ return encode_array(stream, field);
463463+ }
464464+ else
465465+ {
466466+ return encode_basic_field(stream, field);
467467+ }
468468+}
469469+470470+/* Default handler for extension fields. Expects to have a pb_msgdesc_t
471471+ * pointer in the extension->type->arg field, pointing to a message with
472472+ * only one field in it. */
473473+static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension)
474474+{
475475+ pb_field_iter_t iter;
476476+477477+ if (!pb_field_iter_begin_extension_const(&iter, extension))
478478+ PB_RETURN_ERROR(stream, "invalid extension");
479479+480480+ return encode_field(stream, &iter);
481481+}
482482+483483+484484+/* Walk through all the registered extensions and give them a chance
485485+ * to encode themselves. */
486486+static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_iter_t *field)
487487+{
488488+ const pb_extension_t *extension = *(const pb_extension_t* const *)field->pData;
489489+490490+ while (extension)
491491+ {
492492+ bool status;
493493+ if (extension->type->encode)
494494+ status = extension->type->encode(stream, extension);
495495+ else
496496+ status = default_extension_encoder(stream, extension);
497497+498498+ if (!status)
499499+ return false;
500500+501501+ extension = extension->next;
502502+ }
503503+504504+ return true;
505505+}
506506+507507+/*********************
508508+ * Encode all fields *
509509+ *********************/
510510+511511+bool checkreturn pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
512512+{
513513+ pb_field_iter_t iter;
514514+ if (!pb_field_iter_begin_const(&iter, fields, src_struct))
515515+ return true; /* Empty message type */
516516+517517+ do {
518518+ if (PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
519519+ {
520520+ /* Special case for the extension field placeholder */
521521+ if (!encode_extension_field(stream, &iter))
522522+ return false;
523523+ }
524524+ else
525525+ {
526526+ /* Regular field */
527527+ if (!encode_field(stream, &iter))
528528+ return false;
529529+ }
530530+ } while (pb_field_iter_next(&iter));
531531+532532+ return true;
533533+}
534534+535535+bool checkreturn pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags)
536536+{
537537+ if ((flags & PB_ENCODE_DELIMITED) != 0)
538538+ {
539539+ return pb_encode_submessage(stream, fields, src_struct);
540540+ }
541541+ else if ((flags & PB_ENCODE_NULLTERMINATED) != 0)
542542+ {
543543+ const pb_byte_t zero = 0;
544544+545545+ if (!pb_encode(stream, fields, src_struct))
546546+ return false;
547547+548548+ return pb_write(stream, &zero, 1);
549549+ }
550550+ else
551551+ {
552552+ return pb_encode(stream, fields, src_struct);
553553+ }
554554+}
555555+556556+bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct)
557557+{
558558+ pb_ostream_t stream = PB_OSTREAM_SIZING;
559559+560560+ if (!pb_encode(&stream, fields, src_struct))
561561+ return false;
562562+563563+ *size = stream.bytes_written;
564564+ return true;
565565+}
566566+567567+/********************
568568+ * Helper functions *
569569+ ********************/
570570+571571+/* This function avoids 64-bit shifts as they are quite slow on many platforms. */
572572+static bool checkreturn pb_encode_varint_32(pb_ostream_t *stream, uint32_t low, uint32_t high)
573573+{
574574+ size_t i = 0;
575575+ pb_byte_t buffer[10];
576576+ pb_byte_t byte = (pb_byte_t)(low & 0x7F);
577577+ low >>= 7;
578578+579579+ while (i < 4 && (low != 0 || high != 0))
580580+ {
581581+ byte |= 0x80;
582582+ buffer[i++] = byte;
583583+ byte = (pb_byte_t)(low & 0x7F);
584584+ low >>= 7;
585585+ }
586586+587587+ if (high)
588588+ {
589589+ byte = (pb_byte_t)(byte | ((high & 0x07) << 4));
590590+ high >>= 3;
591591+592592+ while (high)
593593+ {
594594+ byte |= 0x80;
595595+ buffer[i++] = byte;
596596+ byte = (pb_byte_t)(high & 0x7F);
597597+ high >>= 7;
598598+ }
599599+ }
600600+601601+ buffer[i++] = byte;
602602+603603+ return pb_write(stream, buffer, i);
604604+}
605605+606606+bool checkreturn pb_encode_varint(pb_ostream_t *stream, pb_uint64_t value)
607607+{
608608+ if (value <= 0x7F)
609609+ {
610610+ /* Fast path: single byte */
611611+ pb_byte_t byte = (pb_byte_t)value;
612612+ return pb_write(stream, &byte, 1);
613613+ }
614614+ else
615615+ {
616616+#ifdef PB_WITHOUT_64BIT
617617+ return pb_encode_varint_32(stream, value, 0);
618618+#else
619619+ return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)(value >> 32));
620620+#endif
621621+ }
622622+}
623623+624624+bool checkreturn pb_encode_svarint(pb_ostream_t *stream, pb_int64_t value)
625625+{
626626+ pb_uint64_t zigzagged;
627627+ pb_uint64_t mask = ((pb_uint64_t)-1) >> 1; /* Satisfy clang -fsanitize=integer */
628628+ if (value < 0)
629629+ zigzagged = ~(((pb_uint64_t)value & mask) << 1);
630630+ else
631631+ zigzagged = (pb_uint64_t)value << 1;
632632+633633+ return pb_encode_varint(stream, zigzagged);
634634+}
635635+636636+bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value)
637637+{
638638+#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
639639+ /* Fast path if we know that we're on little endian */
640640+ return pb_write(stream, (const pb_byte_t*)value, 4);
641641+#else
642642+ uint32_t val = *(const uint32_t*)value;
643643+ pb_byte_t bytes[4];
644644+ bytes[0] = (pb_byte_t)(val & 0xFF);
645645+ bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
646646+ bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
647647+ bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
648648+ return pb_write(stream, bytes, 4);
649649+#endif
650650+}
651651+652652+#ifndef PB_WITHOUT_64BIT
653653+bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value)
654654+{
655655+#if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
656656+ /* Fast path if we know that we're on little endian */
657657+ return pb_write(stream, (const pb_byte_t*)value, 8);
658658+#else
659659+ uint64_t val = *(const uint64_t*)value;
660660+ pb_byte_t bytes[8];
661661+ bytes[0] = (pb_byte_t)(val & 0xFF);
662662+ bytes[1] = (pb_byte_t)((val >> 8) & 0xFF);
663663+ bytes[2] = (pb_byte_t)((val >> 16) & 0xFF);
664664+ bytes[3] = (pb_byte_t)((val >> 24) & 0xFF);
665665+ bytes[4] = (pb_byte_t)((val >> 32) & 0xFF);
666666+ bytes[5] = (pb_byte_t)((val >> 40) & 0xFF);
667667+ bytes[6] = (pb_byte_t)((val >> 48) & 0xFF);
668668+ bytes[7] = (pb_byte_t)((val >> 56) & 0xFF);
669669+ return pb_write(stream, bytes, 8);
670670+#endif
671671+}
672672+#endif
673673+674674+bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
675675+{
676676+ pb_uint64_t tag = ((pb_uint64_t)field_number << 3) | wiretype;
677677+ return pb_encode_varint(stream, tag);
678678+}
679679+680680+bool pb_encode_tag_for_field ( pb_ostream_t* stream, const pb_field_iter_t* field )
681681+{
682682+ pb_wire_type_t wiretype;
683683+ switch (PB_LTYPE(field->type))
684684+ {
685685+ case PB_LTYPE_BOOL:
686686+ case PB_LTYPE_VARINT:
687687+ case PB_LTYPE_UVARINT:
688688+ case PB_LTYPE_SVARINT:
689689+ wiretype = PB_WT_VARINT;
690690+ break;
691691+692692+ case PB_LTYPE_FIXED32:
693693+ wiretype = PB_WT_32BIT;
694694+ break;
695695+696696+ case PB_LTYPE_FIXED64:
697697+ wiretype = PB_WT_64BIT;
698698+ break;
699699+700700+ case PB_LTYPE_BYTES:
701701+ case PB_LTYPE_STRING:
702702+ case PB_LTYPE_SUBMESSAGE:
703703+ case PB_LTYPE_SUBMSG_W_CB:
704704+ case PB_LTYPE_FIXED_LENGTH_BYTES:
705705+ wiretype = PB_WT_STRING;
706706+ break;
707707+708708+ default:
709709+ PB_RETURN_ERROR(stream, "invalid field type");
710710+ }
711711+712712+ return pb_encode_tag(stream, wiretype, field->tag);
713713+}
714714+715715+bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
716716+{
717717+ if (!pb_encode_varint(stream, (pb_uint64_t)size))
718718+ return false;
719719+720720+ return pb_write(stream, buffer, size);
721721+}
722722+723723+bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
724724+{
725725+ /* First calculate the message size using a non-writing substream. */
726726+ pb_ostream_t substream = PB_OSTREAM_SIZING;
727727+ size_t size;
728728+ bool status;
729729+730730+ if (!pb_encode(&substream, fields, src_struct))
731731+ {
732732+#ifndef PB_NO_ERRMSG
733733+ stream->errmsg = substream.errmsg;
734734+#endif
735735+ return false;
736736+ }
737737+738738+ size = substream.bytes_written;
739739+740740+ if (!pb_encode_varint(stream, (pb_uint64_t)size))
741741+ return false;
742742+743743+ if (stream->callback == NULL)
744744+ return pb_write(stream, NULL, size); /* Just sizing */
745745+746746+ if (stream->bytes_written + size > stream->max_size)
747747+ PB_RETURN_ERROR(stream, "stream full");
748748+749749+ /* Use a substream to verify that a callback doesn't write more than
750750+ * what it did the first time. */
751751+ substream.callback = stream->callback;
752752+ substream.state = stream->state;
753753+ substream.max_size = size;
754754+ substream.bytes_written = 0;
755755+#ifndef PB_NO_ERRMSG
756756+ substream.errmsg = NULL;
757757+#endif
758758+759759+ status = pb_encode(&substream, fields, src_struct);
760760+761761+ stream->bytes_written += substream.bytes_written;
762762+ stream->state = substream.state;
763763+#ifndef PB_NO_ERRMSG
764764+ stream->errmsg = substream.errmsg;
765765+#endif
766766+767767+ if (substream.bytes_written != size)
768768+ PB_RETURN_ERROR(stream, "submsg size changed");
769769+770770+ return status;
771771+}
772772+773773+/* Field encoders */
774774+775775+static bool checkreturn pb_enc_bool(pb_ostream_t *stream, const pb_field_iter_t *field)
776776+{
777777+ uint32_t value = safe_read_bool(field->pData) ? 1 : 0;
778778+ PB_UNUSED(field);
779779+ return pb_encode_varint(stream, value);
780780+}
781781+782782+static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_iter_t *field)
783783+{
784784+ if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
785785+ {
786786+ /* Perform unsigned integer extension */
787787+ pb_uint64_t value = 0;
788788+789789+ if (field->data_size == sizeof(uint_least8_t))
790790+ value = *(const uint_least8_t*)field->pData;
791791+ else if (field->data_size == sizeof(uint_least16_t))
792792+ value = *(const uint_least16_t*)field->pData;
793793+ else if (field->data_size == sizeof(uint32_t))
794794+ value = *(const uint32_t*)field->pData;
795795+ else if (field->data_size == sizeof(pb_uint64_t))
796796+ value = *(const pb_uint64_t*)field->pData;
797797+ else
798798+ PB_RETURN_ERROR(stream, "invalid data_size");
799799+800800+ return pb_encode_varint(stream, value);
801801+ }
802802+ else
803803+ {
804804+ /* Perform signed integer extension */
805805+ pb_int64_t value = 0;
806806+807807+ if (field->data_size == sizeof(int_least8_t))
808808+ value = *(const int_least8_t*)field->pData;
809809+ else if (field->data_size == sizeof(int_least16_t))
810810+ value = *(const int_least16_t*)field->pData;
811811+ else if (field->data_size == sizeof(int32_t))
812812+ value = *(const int32_t*)field->pData;
813813+ else if (field->data_size == sizeof(pb_int64_t))
814814+ value = *(const pb_int64_t*)field->pData;
815815+ else
816816+ PB_RETURN_ERROR(stream, "invalid data_size");
817817+818818+ if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
819819+ return pb_encode_svarint(stream, value);
820820+#ifdef PB_WITHOUT_64BIT
821821+ else if (value < 0)
822822+ return pb_encode_varint_32(stream, (uint32_t)value, (uint32_t)-1);
823823+#endif
824824+ else
825825+ return pb_encode_varint(stream, (pb_uint64_t)value);
826826+827827+ }
828828+}
829829+830830+static bool checkreturn pb_enc_fixed(pb_ostream_t *stream, const pb_field_iter_t *field)
831831+{
832832+#ifdef PB_CONVERT_DOUBLE_FLOAT
833833+ if (field->data_size == sizeof(float) && PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
834834+ {
835835+ return pb_encode_float_as_double(stream, *(float*)field->pData);
836836+ }
837837+#endif
838838+839839+ if (field->data_size == sizeof(uint32_t))
840840+ {
841841+ return pb_encode_fixed32(stream, field->pData);
842842+ }
843843+#ifndef PB_WITHOUT_64BIT
844844+ else if (field->data_size == sizeof(uint64_t))
845845+ {
846846+ return pb_encode_fixed64(stream, field->pData);
847847+ }
848848+#endif
849849+ else
850850+ {
851851+ PB_RETURN_ERROR(stream, "invalid data_size");
852852+ }
853853+}
854854+855855+static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
856856+{
857857+ const pb_bytes_array_t *bytes = NULL;
858858+859859+ bytes = (const pb_bytes_array_t*)field->pData;
860860+861861+ if (bytes == NULL)
862862+ {
863863+ /* Treat null pointer as an empty bytes field */
864864+ return pb_encode_string(stream, NULL, 0);
865865+ }
866866+867867+ if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
868868+ bytes->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
869869+ {
870870+ PB_RETURN_ERROR(stream, "bytes size exceeded");
871871+ }
872872+873873+ return pb_encode_string(stream, bytes->bytes, (size_t)bytes->size);
874874+}
875875+876876+static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_iter_t *field)
877877+{
878878+ size_t size = 0;
879879+ size_t max_size = (size_t)field->data_size;
880880+ const char *str = (const char*)field->pData;
881881+882882+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
883883+ {
884884+ max_size = (size_t)-1;
885885+ }
886886+ else
887887+ {
888888+ /* pb_dec_string() assumes string fields end with a null
889889+ * terminator when the type isn't PB_ATYPE_POINTER, so we
890890+ * shouldn't allow more than max-1 bytes to be written to
891891+ * allow space for the null terminator.
892892+ */
893893+ if (max_size == 0)
894894+ PB_RETURN_ERROR(stream, "zero-length string");
895895+896896+ max_size -= 1;
897897+ }
898898+899899+900900+ if (str == NULL)
901901+ {
902902+ size = 0; /* Treat null pointer as an empty string */
903903+ }
904904+ else
905905+ {
906906+ const char *p = str;
907907+908908+ /* strnlen() is not always available, so just use a loop */
909909+ while (size < max_size && *p != '\0')
910910+ {
911911+ size++;
912912+ p++;
913913+ }
914914+915915+ if (*p != '\0')
916916+ {
917917+ PB_RETURN_ERROR(stream, "unterminated string");
918918+ }
919919+ }
920920+921921+#ifdef PB_VALIDATE_UTF8
922922+ if (!pb_validate_utf8(str))
923923+ PB_RETURN_ERROR(stream, "invalid utf8");
924924+#endif
925925+926926+ return pb_encode_string(stream, (const pb_byte_t*)str, size);
927927+}
928928+929929+static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_iter_t *field)
930930+{
931931+ if (field->submsg_desc == NULL)
932932+ PB_RETURN_ERROR(stream, "invalid field descriptor");
933933+934934+ if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
935935+ {
936936+ /* Message callback is stored right before pSize. */
937937+ pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
938938+ if (callback->funcs.encode)
939939+ {
940940+ if (!callback->funcs.encode(stream, field, &callback->arg))
941941+ return false;
942942+ }
943943+ }
944944+945945+ return pb_encode_submessage(stream, field->submsg_desc, field->pData);
946946+}
947947+948948+static bool checkreturn pb_enc_fixed_length_bytes(pb_ostream_t *stream, const pb_field_iter_t *field)
949949+{
950950+ return pb_encode_string(stream, (const pb_byte_t*)field->pData, (size_t)field->data_size);
951951+}
952952+953953+#ifdef PB_CONVERT_DOUBLE_FLOAT
954954+bool pb_encode_float_as_double(pb_ostream_t *stream, float value)
955955+{
956956+ union { float f; uint32_t i; } in;
957957+ uint_least8_t sign;
958958+ int exponent;
959959+ uint64_t mantissa;
960960+961961+ in.f = value;
962962+963963+ /* Decompose input value */
964964+ sign = (uint_least8_t)((in.i >> 31) & 1);
965965+ exponent = (int)((in.i >> 23) & 0xFF) - 127;
966966+ mantissa = in.i & 0x7FFFFF;
967967+968968+ if (exponent == 128)
969969+ {
970970+ /* Special value (NaN etc.) */
971971+ exponent = 1024;
972972+ }
973973+ else if (exponent == -127)
974974+ {
975975+ if (!mantissa)
976976+ {
977977+ /* Zero */
978978+ exponent = -1023;
979979+ }
980980+ else
981981+ {
982982+ /* Denormalized */
983983+ mantissa <<= 1;
984984+ while (!(mantissa & 0x800000))
985985+ {
986986+ mantissa <<= 1;
987987+ exponent--;
988988+ }
989989+ mantissa &= 0x7FFFFF;
990990+ }
991991+ }
992992+993993+ /* Combine fields */
994994+ mantissa <<= 29;
995995+ mantissa |= (uint64_t)(exponent + 1023) << 52;
996996+ mantissa |= (uint64_t)sign << 63;
997997+998998+ return pb_encode_fixed64(stream, &mantissa);
999999+}
10001000+#endif
+185
src/external/nanopb/pb_encode.h
···11+/* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
22+ * The main function is pb_encode. You also need an output stream, and the
33+ * field descriptions created by nanopb_generator.py.
44+ */
55+66+#ifndef PB_ENCODE_H_INCLUDED
77+#define PB_ENCODE_H_INCLUDED
88+99+#include "pb.h"
1010+1111+#ifdef __cplusplus
1212+extern "C" {
1313+#endif
1414+1515+/* Structure for defining custom output streams. You will need to provide
1616+ * a callback function to write the bytes to your storage, which can be
1717+ * for example a file or a network socket.
1818+ *
1919+ * The callback must conform to these rules:
2020+ *
2121+ * 1) Return false on IO errors. This will cause encoding to abort.
2222+ * 2) You can use state to store your own data (e.g. buffer pointer).
2323+ * 3) pb_write will update bytes_written after your callback runs.
2424+ * 4) Substreams will modify max_size and bytes_written. Don't use them
2525+ * to calculate any pointers.
2626+ */
2727+struct pb_ostream_s
2828+{
2929+#ifdef PB_BUFFER_ONLY
3030+ /* Callback pointer is not used in buffer-only configuration.
3131+ * Having an int pointer here allows binary compatibility but
3232+ * gives an error if someone tries to assign callback function.
3333+ * Also, NULL pointer marks a 'sizing stream' that does not
3434+ * write anything.
3535+ */
3636+ const int *callback;
3737+#else
3838+ bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
3939+#endif
4040+ void *state; /* Free field for use by callback implementation. */
4141+ size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
4242+ size_t bytes_written; /* Number of bytes written so far. */
4343+4444+#ifndef PB_NO_ERRMSG
4545+ const char *errmsg;
4646+#endif
4747+};
4848+4949+/***************************
5050+ * Main encoding functions *
5151+ ***************************/
5252+5353+/* Encode a single protocol buffers message from C structure into a stream.
5454+ * Returns true on success, false on any failure.
5555+ * The actual struct pointed to by src_struct must match the description in fields.
5656+ * All required fields in the struct are assumed to have been filled in.
5757+ *
5858+ * Example usage:
5959+ * MyMessage msg = {};
6060+ * uint8_t buffer[64];
6161+ * pb_ostream_t stream;
6262+ *
6363+ * msg.field1 = 42;
6464+ * stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
6565+ * pb_encode(&stream, MyMessage_fields, &msg);
6666+ */
6767+bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct);
6868+6969+/* Extended version of pb_encode, with several options to control the
7070+ * encoding process:
7171+ *
7272+ * PB_ENCODE_DELIMITED: Prepend the length of message as a varint.
7373+ * Corresponds to writeDelimitedTo() in Google's
7474+ * protobuf API.
7575+ *
7676+ * PB_ENCODE_NULLTERMINATED: Append a null byte to the message for termination.
7777+ * NOTE: This behaviour is not supported in most other
7878+ * protobuf implementations, so PB_ENCODE_DELIMITED
7979+ * is a better option for compatibility.
8080+ */
8181+#define PB_ENCODE_DELIMITED 0x02U
8282+#define PB_ENCODE_NULLTERMINATED 0x04U
8383+bool pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags);
8484+8585+/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
8686+#define pb_encode_delimited(s,f,d) pb_encode_ex(s,f,d, PB_ENCODE_DELIMITED)
8787+#define pb_encode_nullterminated(s,f,d) pb_encode_ex(s,f,d, PB_ENCODE_NULLTERMINATED)
8888+8989+/* Encode the message to get the size of the encoded data, but do not store
9090+ * the data. */
9191+bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct);
9292+9393+/**************************************
9494+ * Functions for manipulating streams *
9595+ **************************************/
9696+9797+/* Create an output stream for writing into a memory buffer.
9898+ * The number of bytes written can be found in stream.bytes_written after
9999+ * encoding the message.
100100+ *
101101+ * Alternatively, you can use a custom stream that writes directly to e.g.
102102+ * a file or a network socket.
103103+ */
104104+pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize);
105105+106106+/* Pseudo-stream for measuring the size of a message without actually storing
107107+ * the encoded data.
108108+ *
109109+ * Example usage:
110110+ * MyMessage msg = {};
111111+ * pb_ostream_t stream = PB_OSTREAM_SIZING;
112112+ * pb_encode(&stream, MyMessage_fields, &msg);
113113+ * printf("Message size is %d\n", stream.bytes_written);
114114+ */
115115+#ifndef PB_NO_ERRMSG
116116+#define PB_OSTREAM_SIZING {0,0,0,0,0}
117117+#else
118118+#define PB_OSTREAM_SIZING {0,0,0,0}
119119+#endif
120120+121121+/* Function to write into a pb_ostream_t stream. You can use this if you need
122122+ * to append or prepend some custom headers to the message.
123123+ */
124124+bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count);
125125+126126+127127+/************************************************
128128+ * Helper functions for writing field callbacks *
129129+ ************************************************/
130130+131131+/* Encode field header based on type and field number defined in the field
132132+ * structure. Call this from the callback before writing out field contents. */
133133+bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field);
134134+135135+/* Encode field header by manually specifying wire type. You need to use this
136136+ * if you want to write out packed arrays from a callback field. */
137137+bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
138138+139139+/* Encode an integer in the varint format.
140140+ * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
141141+#ifndef PB_WITHOUT_64BIT
142142+bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
143143+#else
144144+bool pb_encode_varint(pb_ostream_t *stream, uint32_t value);
145145+#endif
146146+147147+/* Encode an integer in the zig-zagged svarint format.
148148+ * This works for sint32 and sint64. */
149149+#ifndef PB_WITHOUT_64BIT
150150+bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
151151+#else
152152+bool pb_encode_svarint(pb_ostream_t *stream, int32_t value);
153153+#endif
154154+155155+/* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
156156+bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size);
157157+158158+/* Encode a fixed32, sfixed32 or float value.
159159+ * You need to pass a pointer to a 4-byte wide C variable. */
160160+bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
161161+162162+#ifndef PB_WITHOUT_64BIT
163163+/* Encode a fixed64, sfixed64 or double value.
164164+ * You need to pass a pointer to a 8-byte wide C variable. */
165165+bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
166166+#endif
167167+168168+#ifdef PB_CONVERT_DOUBLE_FLOAT
169169+/* Encode a float value so that it appears like a double in the encoded
170170+ * message. */
171171+bool pb_encode_float_as_double(pb_ostream_t *stream, float value);
172172+#endif
173173+174174+/* Encode a submessage field.
175175+ * You need to pass the pb_field_t array and pointer to struct, just like
176176+ * with pb_encode(). This internally encodes the submessage twice, first to
177177+ * calculate message size and then to actually write it out.
178178+ */
179179+bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct);
180180+181181+#ifdef __cplusplus
182182+} /* extern "C" */
183183+#endif
184184+185185+#endif