1# Nanopb: API reference 2 3## Compilation options 4 5Compilation options affect the functionality included in the nanopb core C code. 6The options can be specified in one of two ways: 7 81. Using the -D switch on the C compiler command line. 92. Using a `#define` at the top of pb.h. 10 11> **NOTE:** You must have the same compilation options for the nanopb library and all code that 12includes nanopb headers. 13 14* `PB_ENABLE_MALLOC`: Enable dynamic allocation support in the decoder. 15* `PB_MAX_REQUIRED_FIELDS`: Maximum number of proto2 `required` fields to check for presence. Default value is 64. Compiler warning will tell if you need this. 16* `PB_FIELD_32BIT`: Add support for field tag numbers over 65535, fields larger than 64 kiB and arrays larger than 65535 entries. Compiler warning will tell if you need this. 17* `PB_NO_ERRMSG`: Disable error message support to save code size. Only error information is the `true`/`false` return value. 18* `PB_BUFFER_ONLY`: Disable support for custom streams. Only supports encoding and decoding with memory buffers. Speeds up execution and slightly decreases code size. 19* `PB_SYSTEM_HEADER`: Replace the standards header files with a single system-specific header file. Value must include quotes, for example `#define PB_SYSTEM_HEADER "foo.h"`. See [extra/pb_syshdr.h](https://github.com/nanopb/nanopb/blob/master/extra/pb_syshdr.h) for an example. 20* `PB_WITHOUT_64BIT`: Disable support of 64-bit integer fields, for old compilers or for a slight speedup on 8-bit platforms. 21* `PB_ENCODE_ARRAYS_UNPACKED`: Encode scalar arrays in the unpacked format, which takes up more space. Only to be used when the decoder on the receiving side cannot process packed arrays, such as [protobuf.js versions before 2020](https://github.com/protocolbuffers/protobuf/issues/1701). 22* `PB_CONVERT_DOUBLE_FLOAT`: Convert doubles to floats for platforms that do not support 64-bit `double` datatype. Mainly `AVR` processors. 23* `PB_VALIDATE_UTF8`: Check whether incoming strings are valid UTF-8 sequences. Adds a small performance and code size penalty. 24* `PB_C99_STATIC_ASSERT`: Use C99 style negative array trick for static assertions. For compilers that do not support C11 standard. 25* `PB_NO_STATIC_ASSERT`: Disable static assertions at compile time. Only for compilers with limited support of C standards. 26 27The `PB_MAX_REQUIRED_FIELDS` and `PB_FIELD_32BIT` settings allow 28raising some datatype limits to suit larger messages. Their need is 29recognized automatically by C-preprocessor `#if`-directives in the 30generated `.pb.c` files. The default setting is to use the smallest 31datatypes (least resources used). 32 33## Generator options 34 35Generator options affect how the `.proto` files get converted to `.pb.c` and `.pb.h.` files. 36 37Most options are related to specific message or field in `.proto` file. 38The full set of available options is defined in [nanopb.proto](https://github.com/nanopb/nanopb/blob/master/generator/proto/nanopb.proto). Here is a list of the most common options, but see the file for a full list: 39 40* `max_size`: Allocated maximum size for `bytes` and `string` fields. For strings, this includes the terminating zero. 41* `max_length`: Maximum length for `string` fields. Setting this is equivalent to setting `max_size` to a value of length + 1. 42* `max_count`: Allocated maximum number of entries in arrays (`repeated` fields). 43* `type`: Select how memory is allocated for the generated field. Default value is `FT_DEFAULT`, which defaults to `FT_STATIC` when possible and `FT_CALLBACK` if not possible. You can use `FT_CALLBACK`, `FT_POINTER`, `FT_STATIC` or `FT_IGNORE` to select a callback field, a dynamically allocate dfield, a statically allocated field or to completely ignore the field. 44* `long_names`: Prefix the enum name to the enum value in definitions, i.e. `EnumName_EnumValue`. Enabled by default. 45* `packed_struct`: Make the generated structures packed, which saves some RAM space but slows down execution. This can only be used if the CPU supports unaligned access to variables. 46* `skip_message`: Skip a whole message from generation. Can be used to remove message types that are not needed in an application. 47* `no_unions`: Generate `oneof` fields as multiple optional fields instead of a C `union {}`. 48* `anonymous_oneof`: Generate `oneof` fields as an anonymous union. 49* `msgid`: Specifies a unique id for this message type. Can be used by user code as an identifier. 50* `fixed_length`: Generate `bytes` fields with a constant length defined by `max_size`. A separate `.size` field will then not be generated. 51* `fixed_count`: Generate arrays with constant length defined by `max_count`. 52* `package`: Package name that applies only for nanopb generator. Defaults to name defined by `package` keyword in .proto file, which applies for all languages. 53* `int_size`: Override the integer type of a field. For example, specify `int_size = IS_8` to convert `int32` from protocol definition into `int8_t` in the structure. 54 55These options can be defined for the .proto files before they are 56converted using the nanopb-generator.py. There are three ways to define 57the options: 58 591. Using a separate .options file. This allows using wildcards for 60 applying same options to multiple fields. 612. Defining the options on the command line of nanopb_generator.py. 62 This only makes sense for settings that apply to a whole file. 633. Defining the options in the .proto file using the nanopb extensions. 64 This keeps the options close to the fields they apply to, but can be 65 problematic if the same .proto file is shared with many projects. 66 67The effect of the options is the same no matter how they are given. The 68most common purpose is to define maximum size for string fields in order 69to statically allocate them. 70 71### Defining the options in a .options file 72 73The preferred way to define options is to have a separate file 74'myproto.options' in the same directory as the 'myproto.proto'. : 75 76 # myproto.proto 77 message MyMessage { 78 required string name = 1; 79 repeated int32 ids = 4; 80 } 81 82 # myproto.options 83 MyMessage.name max_size:40 84 MyMessage.ids max_count:5 85 86The generator will automatically search for this file and read the 87options from it. The file format is as follows: 88 89- Lines starting with `#` or `//` are regarded as comments. 90- Blank lines are ignored. 91- All other lines should start with a field name pattern, followed by 92 one or more options. For example: `MyMessage.myfield max_size:5 max_count:10`. 93- The field name pattern is matched against a string of form 94 `Message.field`. For nested messages, the string is 95 `Message.SubMessage.field`. A whole file can be matched by its 96 filename `dir/file.proto`. 97- The field name pattern may use the notation recognized by Python 98 fnmatch(): 99 - `*` matches any part of string, like `Message.*` for all 100 fields 101 - `?` matches any single character 102 - `[seq]` matches any of characters `s`, `e` and `q` 103 - `[!seq]` matches any other character 104- The options are written as `option_name:option_value` and 105 several options can be defined on same line, separated by 106 whitespace. 107- Options defined later in the file override the ones specified 108 earlier, so it makes sense to define wildcard options first in the 109 file and more specific ones later. 110 111To debug problems in applying the options, you can use the `-v` option 112for the nanopb generator. With protoc, plugin options are specified with 113`--nanopb_opt`: 114 115 nanopb_generator -v message.proto # When invoked directly 116 protoc ... --nanopb_opt=-v --nanopb_out=. message.proto # When invoked through protoc 117 118Protoc doesn't currently pass include path into plugins. Therefore if 119your `.proto` is in a subdirectory, nanopb may have trouble finding the 120associated `.options` file. A workaround is to specify include path 121separately to the nanopb plugin, like: 122 123 protoc -Isubdir --nanopb_opt=-Isubdir --nanopb_out=. message.proto 124 125If preferred, the name of the options file can be set using generator 126argument `-f`. 127 128### Defining the options in the .proto file 129 130The .proto file format allows defining custom options for the fields. 131The nanopb library comes with *nanopb.proto* which does exactly that, 132allowing you do define the options directly in the .proto file: 133 134~~~~ protobuf 135import "nanopb.proto"; 136 137message MyMessage { 138 required string name = 1 [(nanopb).max_size = 40]; 139 repeated int32 ids = 4 [(nanopb).max_count = 5]; 140} 141~~~~ 142 143A small complication is that you have to set the include path of protoc 144so that nanopb.proto can be found. Therefore, to compile a .proto file 145which uses options, use a protoc command similar to: 146 147 protoc -Inanopb/generator/proto -I. --nanopb_out=. message.proto 148 149The options can be defined in file, message and field scopes: 150 151~~~~ protobuf 152option (nanopb_fileopt).max_size = 20; // File scope 153message Message 154{ 155 option (nanopb_msgopt).max_size = 30; // Message scope 156 required string fieldsize = 1 [(nanopb).max_size = 40]; // Field scope 157} 158~~~~ 159 160### Defining the options on command line 161 162The nanopb_generator.py has a simple command line option `-s OPTION:VALUE`. 163The setting applies to the whole file that is being processed. 164 165There are also a few command line options that cannot be applied using the 166other mechanisms, as they affect the whole generation: 167 168* `--c-style`: Modify symbol names to better match C naming conventions. 169* `--no-timestamp`: Do not add timestamp to generated files. 170* `--strip-path`: Remove relative path from generated `#include` directives. 171* `--cpp-descriptors`: Generate extra convenience definitions for use from C++ 172 173## pb.h 174 175### pb_byte_t 176 177Type used for storing byte-sized data, such as raw binary input and 178bytes-type fields. 179 180 typedef uint_least8_t pb_byte_t; 181 182For most platforms this is equivalent to `uint8_t`. Some platforms 183however do not support 8-bit variables, and on those platforms 16 or 32 184bits need to be used for each byte. 185 186### pb_size_t 187 188Type used for storing tag numbers and sizes of message fields. By 189default the type is 16-bit: 190 191 typedef uint_least16_t pb_size_t; 192 193If tag numbers or fields larger than 65535 are needed, `PB_FIELD_32BIT` 194option can be used to change the type to 32-bit value. 195 196### pb_type_t 197 198Type used to store the type of each field, to control the 199encoder/decoder behaviour. 200 201 typedef uint_least8_t pb_type_t; 202 203The low-order nibble of the enumeration values defines the function that 204can be used for encoding and decoding the field data: 205 206| LTYPE identifier |Value |Storage format 207| ---------------------------------|-------|------------------------------------------------ 208| `PB_LTYPE_BOOL` |0x00 |Boolean. 209| `PB_LTYPE_VARINT` |0x01 |Integer. 210| `PB_LTYPE_UVARINT` |0x02 |Unsigned integer. 211| `PB_LTYPE_SVARINT` |0x03 |Integer, zigzag encoded. 212| `PB_LTYPE_FIXED32` |0x04 |32-bit integer or floating point. 213| `PB_LTYPE_FIXED64` |0x05 |64-bit integer or floating point. 214| `PB_LTYPE_BYTES` |0x06 |Structure with `size_t` field and byte array. 215| `PB_LTYPE_STRING` |0x07 |Null-terminated string. 216| `PB_LTYPE_SUBMESSAGE` |0x08 |Submessage structure. 217| `PB_LTYPE_SUBMSG_W_CB` |0x09 |Submessage with pre-decoding callback. 218| `PB_LTYPE_EXTENSION` |0x0A |Pointer to `pb_extension_t`. 219| `PB_LTYPE_FIXED_LENGTH_BYTES` |0x0B |Inline `pb_byte_t` array of fixed size. 220 221The bits 4-5 define whether the field is required, optional or repeated. 222There are separate definitions for semantically different modes, even 223though some of them share values and are distinguished based on values 224of other fields: 225 226 |HTYPE identifier |Value |Field handling 227 |---------------------|-------|-------------------------------------------------------------------------------------------- 228 |`PB_HTYPE_REQUIRED` |0x00 |Verify that field exists in decoded message. 229 |`PB_HTYPE_OPTIONAL` |0x10 |Use separate `has_<field>` boolean to specify whether the field is present. 230 |`PB_HTYPE_SINGULAR` |0x10 |Proto3 field, which is present when its value is non-zero. 231 |`PB_HTYPE_REPEATED` |0x20 |A repeated field with preallocated array. Separate `<field>_count` for number of items. 232 |`PB_HTYPE_FIXARRAY` |0x20 |A repeated field that has constant length. 233 |`PB_HTYPE_ONEOF` |0x30 |Oneof-field, only one of each group can be present. 234 235The bits 6-7 define the how the storage for the field is allocated: 236 237|ATYPE identifier |Value |Allocation method 238|---------------------|-------|-------------------------------------------------------------------------------------------- 239|`PB_ATYPE_STATIC` |0x00 |Statically allocated storage in the structure. 240|`PB_ATYPE_POINTER` |0x80 |Dynamically allocated storage. Struct field contains a pointer to the storage. 241|`PB_ATYPE_CALLBACK` |0x40 |A field with dynamic storage size. Struct field contains a pointer to a callback function. 242 243### pb_msgdesc_t 244 245Autogenerated structure that contains information about a message and 246pointers to the field descriptors. Use functions defined in 247`pb_common.h` to process the field information. 248 249 typedef struct pb_msgdesc_s pb_msgdesc_t; 250 struct pb_msgdesc_s { 251 pb_size_t field_count; 252 const uint32_t *field_info; 253 const pb_msgdesc_t * const * submsg_info; 254 const pb_byte_t *default_value; 255 256 bool (*field_callback)(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field); 257 }; 258 259| | | 260|-----------------|--------------------------------------------------------| 261|`field_count` | Total number of fields in the message. 262|`field_info` | Pointer to compact representation of the field information. 263|`submsg_info` | Pointer to array of pointers to descriptors for submessages. 264|`default_value` | Default values for this message as an encoded protobuf message. 265|`field_callback` | Function used to handle all callback fields in this message. By default `pb_default_field_callback()` which loads per-field callbacks from a `pb_callback_t` structure. 266 267### pb_field_iter_t 268 269Describes a single structure field with memory position in relation to 270others. The field information is stored in a compact format and loaded 271into `pb_field_iter_t` by the functions defined in `pb_common.h`. 272 273 typedef struct pb_field_iter_s pb_field_iter_t; 274 struct pb_field_iter_s { 275 const pb_msgdesc_t *descriptor; 276 void *message; 277 278 pb_size_t index; 279 pb_size_t field_info_index; 280 pb_size_t required_field_index; 281 pb_size_t submessage_index; 282 283 pb_size_t tag; 284 pb_size_t data_size; 285 pb_size_t array_size; 286 pb_type_t type; 287 288 void *pField; 289 void *pData; 290 void *pSize; 291 292 const pb_msgdesc_t *submsg_desc; 293 }; 294 295| | | 296|----------------------|--------------------------------------------------------| 297| descriptor | Pointer to `pb_msgdesc_t` for the message that contains this field. 298| message | Pointer to the start of the message structure. 299| index | Index of the field inside the message 300| field_info_index | Index to the internal `field_info` array 301| required_field_index | Index that counts only the required fields 302| submessage_index | Index that counts only submessages 303| tag | Tag number defined in `.proto` file for this field. 304| data_size | `sizeof()` of the field in the structure. For repeated fields this is for a single array entry. 305| array_size | Maximum number of items in a statically allocated array. 306| type | Type ([pb_type_t](#pb_type_t)) of the field. 307| pField | Pointer to the field storage in the structure. 308| pData | Pointer to data contents. For arrays and pointers this can be different than `pField`. 309| pSize | Pointer to count or has field, or NULL if this field doesn't have such. 310| submsg_desc | For submessage fields, points to the descriptor for the submessage. 311 312By default [pb_size_t](#pb_size_t) is 16-bit, limiting the sizes and 313tags to 65535. The limit can be raised by defining `PB_FIELD_32BIT`. 314 315### pb_bytes_array_t 316 317An byte array with a field for storing the length: 318 319 typedef struct { 320 pb_size_t size; 321 pb_byte_t bytes[1]; 322 } pb_bytes_array_t; 323 324In an actual array, the length of `bytes` may be different. The macros 325`PB_BYTES_ARRAY_T()` and `PB_BYTES_ARRAY_T_ALLOCSIZE()` 326are used to allocate variable length storage for bytes fields. 327 328### pb_callback_t 329 330Part of a message structure, for fields with type PB_HTYPE_CALLBACK: 331 332 typedef struct _pb_callback_t pb_callback_t; 333 struct _pb_callback_t { 334 union { 335 bool (*decode)(pb_istream_t *stream, const pb_field_iter_t *field, void **arg); 336 bool (*encode)(pb_ostream_t *stream, const pb_field_iter_t *field, void * const *arg); 337 } funcs; 338 339 void *arg; 340 }; 341 342A pointer to the *arg* is passed to the callback when calling. It can be 343used to store any information that the callback might need. Note that 344this is a double pointer. If you set `field.arg` to point to 345`&data` in your main code, in the callback you can access it like this: 346 347 myfunction(*arg); /* Gives pointer to data as argument */ 348 myfunction(*(data_t*)*arg); /* Gives value of data as argument */ 349 *arg = newdata; /* Alters value of field.arg in structure */ 350 351When calling [pb_encode](#pb_encode), `funcs.encode` is used, and 352similarly when calling [pb_decode](#pb_decode), `funcs.decode` is used. 353The function pointers are stored in the same memory location but are of 354incompatible types. You can set the function pointer to NULL to skip the 355field. 356 357### pb_wire_type_t 358 359Protocol Buffers wire types. These are used with 360[pb_encode_tag](#pb_encode_tag). : 361 362 typedef enum { 363 PB_WT_VARINT = 0, 364 PB_WT_64BIT = 1, 365 PB_WT_STRING = 2, 366 PB_WT_32BIT = 5 367 } pb_wire_type_t; 368 369### pb_extension_type_t 370 371Defines the handler functions and auxiliary data for a field that 372extends another message. Usually autogenerated by 373`nanopb_generator.py`. 374 375 typedef struct { 376 bool (*decode)(pb_istream_t *stream, pb_extension_t *extension, 377 uint32_t tag, pb_wire_type_t wire_type); 378 bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension); 379 const void *arg; 380 } pb_extension_type_t; 381 382In the normal case, the function pointers are `NULL` and the decoder and 383encoder use their internal implementations. The internal implementations 384assume that `arg` points to a [pb_field_iter_t](#pb_field_iter_t) 385that describes the field in question. 386 387To implement custom processing of unknown fields, you can provide 388pointers to your own functions. Their functionality is mostly the same 389as for normal callback fields, except that they get called for any 390unknown field when decoding. 391 392### pb_extension_t 393 394Ties together the extension field type and the storage for the field 395value. For message structs that have extensions, the generator will 396add a `pb_extension_t*` field. It should point to a linked list of 397extensions. 398 399 typedef struct { 400 const pb_extension_type_t *type; 401 void *dest; 402 pb_extension_t *next; 403 bool found; 404 } pb_extension_t; 405 406| | | 407|----------------------|--------------------------------------------------------| 408| type | Pointer to the structure that defines the callback functions. 409| dest | Pointer to the variable that stores the field value (as used by the default extension callback functions.) 410| next | Pointer to the next extension handler, or `NULL` for last handler. 411| found | Decoder sets this to true if the extension was found. 412 413### PB_GET_ERROR 414 415Get the current error message from a stream, or a placeholder string if 416there is no error message: 417 418 #define PB_GET_ERROR(stream) (string expression) 419 420This should be used for printing errors, for example: 421 422 if (!pb_decode(...)) 423 { 424 printf("Decode failed: %s\n", PB_GET_ERROR(stream)); 425 } 426 427The macro only returns pointers to constant strings (in code memory), so 428that there is no need to release the returned pointer. 429 430### PB_RETURN_ERROR 431 432Set the error message and return false: 433 434 #define PB_RETURN_ERROR(stream,msg) (sets error and returns false) 435 436This should be used to handle error conditions inside nanopb functions 437and user callback functions: 438 439 if (error_condition) 440 { 441 PB_RETURN_ERROR(stream, "something went wrong"); 442 } 443 444The *msg* parameter must be a constant string. 445 446### PB_BIND 447 448This macro generates the [pb_msgdesc_t](#pb_msgdesc_t) and associated 449arrays, based on a list of fields in [X-macro](https://en.wikipedia.org/wiki/X_Macro) format. : 450 451 #define PB_BIND(msgname, structname, width) ... 452 453| | | 454|----------------------|--------------------------------------------------------| 455| msgname | Name of the message type. Expects `msgname_FIELDLIST` macro to exist. 456| structname | Name of the C structure to bind to. 457| width | Number of words per field descriptor, or `AUTO` to use minimum size possible. 458 459This macro is automatically invoked inside the autogenerated `.pb.c` 460files. User code can also call it to bind message types with custom 461structures or class types. 462 463## pb_encode.h 464 465### pb_ostream_from_buffer 466 467Constructs an output stream for writing into a memory buffer. It uses an internal callback that 468stores the pointer in stream `state` field. : 469 470 pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize); 471 472| | | 473|----------------------|--------------------------------------------------------| 474| buf | Memory buffer to write into. 475| bufsize | Maximum number of bytes to write. 476| returns | An output stream. 477 478After writing, you can check `stream.bytes_written` to find out how 479much valid data there is in the buffer. This should be passed as the 480message length on decoding side. 481 482### pb_write 483 484Writes data to an output stream. Always use this function, instead of 485trying to call stream callback manually. : 486 487 bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); 488 489| | | 490|----------------------|--------------------------------------------------------| 491| stream | Output stream to write to. 492| buf | Pointer to buffer with the data to be written. 493| count | Number of bytes to write. 494| returns | True on success, false if maximum length is exceeded or an IO error happens. 495 496> **NOTE:** If an error happens, *bytes_written* is not incremented. Depending on 497the callback used, calling pb_write again after it has failed once may 498cause undefined behavior. Nanopb itself never does this, instead it 499returns the error to user application. The builtin 500`pb_ostream_from_buffer` is safe to call again after failed write. 501 502### pb_encode 503 504Encodes the contents of a structure as a protocol buffers message and 505writes it to output stream. : 506 507 bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct); 508 509| | | 510|----------------------|--------------------------------------------------------| 511| stream | Output stream to write to. 512| fields | Message descriptor, usually autogenerated. 513| src_struct | Pointer to the message structure. Must match `fields` descriptor. 514| returns | True on success, false on any error condition. Error message is set to `stream->errmsg`. 515 516Normally pb_encode simply walks through the fields description array 517and serializes each field in turn. However, submessages must be 518serialized twice: first to calculate their size and then to actually 519write them to output. This causes some constraints for callback fields, 520which must return the same data on every call. 521 522### pb_encode_ex 523 524Encodes the message, with extended behavior set by flags: 525 526 bool pb_encode_ex(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct, unsigned int flags); 527 528| | | 529|----------------------|--------------------------------------------------------| 530| stream | Output stream to write to. 531| fields | Message descriptor, usually autogenerated. 532| src_struct | Pointer to the message structure. Must match `fields` descriptor. 533| flags | Extended options, see below. 534| returns | True on success, false on any error condition. Error message is set to `stream->errmsg`. 535 536The options that can be defined are: 537 538* `PB_ENCODE_DELIMITED`: Indicate the length of the message by prefixing with a varint-encoded length. Compatible with `parseDelimitedFrom` in Google's protobuf library. 539* `PB_ENCODE_NULLTERMINATED`: Indicate the length of the message by appending a zero tag value after it. Supported by nanopb decoder, but not by most other protobuf libraries. 540 541### pb_get_encoded_size 542 543Calculates the length of the encoded message. 544 545 bool pb_get_encoded_size(size_t *size, const pb_msgdesc_t *fields, const void *src_struct); 546 547| | | 548|----------------------|--------------------------------------------------------| 549| size | Calculated size of the encoded message. 550| fields | Message descriptor, usually autogenerated. 551| src_struct | Pointer to the data that will be serialized. 552| returns | True on success, false on detectable errors in field description or if a field encoder returns false. 553 554### Callback field encoders 555The functions with names `pb_encode_<datatype>` are used when dealing with 556callback fields. The typical reason for using callbacks is to have an 557array of unlimited size. In that case, [pb_encode](#pb_encode) will 558call your callback function, which in turn will call `pb_encode_<datatype>` 559functions repeatedly to write out values. 560 561The tag of a field must be encoded first with 562[pb_encode_tag_for_field](#pb_encode_tag_for_field). After that, you 563can call exactly one of the content-writing functions to encode the 564payload of the field. For repeated fields, you can repeat this process 565multiple times. 566 567Writing packed arrays is a little bit more involved: you need to use 568`pb_encode_tag` and specify `PB_WT_STRING` as the wire 569type. Then you need to know exactly how much data you are going to 570write, and use [pb_encode_varint](#pb_encode_varint) to write out the 571number of bytes before writing the actual data. Substreams can be used 572to determine the number of bytes beforehand; see 573[pb_encode_submessage](#pb_encode_submessage) source code for an 574example. 575 576See [Google Protobuf Encoding Format Documentation](https://developers.google.com/protocol-buffers/docs/encoding) 577for background information on the Protobuf wire format. 578 579#### pb_encode_tag 580 581Starts a field in the Protocol Buffers binary format: encodes the field 582number and the wire type of the data. 583 584 bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number); 585 586| | | 587|----------------------|--------------------------------------------------------| 588| stream | Output stream to write to. 1-5 bytes will be written. 589| wiretype | `PB_WT_VARINT`, `PB_WT_64BIT`, `PB_WT_STRING` or `PB_WT_32BIT` 590| field_number | Identifier for the field, defined in the .proto file. You can get it from `field->tag`. 591| returns | True on success, false on IO error. 592 593#### pb_encode_tag_for_field 594 595Same as [pb_encode_tag](#pb_encode_tag), except takes the parameters 596from a `pb_field_iter_t` structure. 597 598 bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field); 599 600| | | 601|----------------------|--------------------------------------------------------| 602| stream | Output stream to write to. 1-5 bytes will be written. 603| field | Field iterator for this field. 604| returns | True on success, false on IO error or unknown field type. 605 606This function only considers the `PB_LTYPE` of the field. You can use it from 607your field callbacks, because the source generator writes correct `LTYPE` 608also for callback type fields. 609 610Wire type mapping is as follows: 611 612| LTYPEs | Wire type 613|--------------------------------------------------|----------------- 614| BOOL, VARINT, UVARINT, SVARINT | PB_WT_VARINT 615| FIXED64 | PB_WT_64BIT 616| STRING, BYTES, SUBMESSAGE, FIXED_LENGTH_BYTES | PB_WT_STRING 617| FIXED32 | PB_WT_32BIT 618 619#### pb_encode_varint 620 621Encodes a signed or unsigned integer in the 622[varint](http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints) 623format. Works for fields of type `bool`, `enum`, `int32`, `int64`, `uint32` and `uint64`: 624 625 bool pb_encode_varint(pb_ostream_t *stream, uint64_t value); 626 627| | | 628|----------------------|--------------------------------------------------------| 629| stream | Output stream to write to. 1-10 bytes will be written. 630| value | Value to encode, cast to `uint64_t`. 631| returns | True on success, false on IO error. 632 633> **NOTE:** Value will be converted to `uint64_t` in the argument. 634> To encode signed values, the argument should be cast to `int64_t` first for correct sign extension. 635 636#### pb_encode_svarint 637 638Encodes a signed integer in the [zig-zagged](https://developers.google.com/protocol-buffers/docs/encoding#signed_integers) format. 639Works for fields of type `sint32` and `sint64`: 640 641 bool pb_encode_svarint(pb_ostream_t *stream, int64_t value); 642 643(parameters are the same as for [pb_encode_varint](#pb_encode_varint) 644 645#### pb_encode_string 646 647Writes the length of a string as varint and then contents of the string. 648Works for fields of type `bytes` and `string`: 649 650 bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size); 651 652| | | 653|----------------------|--------------------------------------------------------| 654| stream | Output stream to write to. 655| buffer | Pointer to string data. 656| size | Number of bytes in the string. Pass `strlen(s)` for strings. 657| returns | True on success, false on IO error. 658 659#### pb_encode_fixed32 660 661Writes 4 bytes to stream and swaps bytes on big-endian architectures. 662Works for fields of type `fixed32`, `sfixed32` and `float`: 663 664 bool pb_encode_fixed32(pb_ostream_t *stream, const void *value); 665 666| | | 667|----------------------|--------------------------------------------------------| 668| stream | Output stream to write to. 4 bytes will be written. 669| value | Pointer to a 4-bytes large C variable, for example `uint32_t foo;`. 670| returns | True on success, false on IO error. 671 672#### pb_encode_fixed64 673 674Writes 8 bytes to stream and swaps bytes on big-endian architecture. 675Works for fields of type `fixed64`, `sfixed64` and `double`: 676 677 bool pb_encode_fixed64(pb_ostream_t *stream, const void *value); 678 679| | | 680|----------------------|--------------------------------------------------------| 681| stream | Output stream to write to. 8 bytes will be written. 682| value | Pointer to a 8-bytes large C variable, for example `uint64_t foo;`. 683| returns | True on success, false on IO error. 684 685#### pb_encode_float_as_double 686 687Encodes a 32-bit `float` value so that it appears like a 64-bit `double` in the encoded message. 688This is sometimes needed when platforms like AVR that do not support 64-bit `double` need to communicate using a 689message type that contains `double` fields. 690 691 bool pb_encode_float_as_double(pb_ostream_t *stream, float value); 692 693| | | 694|----------------------|--------------------------------------------------------| 695| stream | Output stream to write to. 8 bytes will be written. 696| value | Float value to encode. 697| returns | True on success, false on IO error. 698 699#### pb_encode_submessage 700 701Encodes a submessage field, including the size header for it. Works for 702fields of any message type. 703 704 bool pb_encode_submessage(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct); 705 706| | | 707|----------------------|--------------------------------------------------------| 708| stream | Output stream to write to. 709| fields | Pointer to the autogenerated message descriptor for the submessage type, e.g. `MyMessage_fields`. 710| src | Pointer to the structure where submessage data is. 711| returns | True on success, false on IO errors, pb_encode errors or if submessage size changes between calls. 712 713In Protocol Buffers format, the submessage size must be written before 714the submessage contents. Therefore, this function has to encode the 715submessage twice in order to know the size beforehand. 716 717If the submessage contains callback fields, the callback function might 718misbehave and write out a different amount of data on the second call. 719This situation is recognized and `false` is returned, but garbage will 720be written to the output before the problem is detected. 721 722## pb_decode.h 723 724### pb_istream_from_buffer 725 726Helper function for creating an input stream that reads data from a 727memory buffer. 728 729 pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize); 730 731| | | 732|----------------------|--------------------------------------------------------| 733| buf | Pointer to byte array to read from. 734| bufsize | Size of the byte array. Typically length of the message to be decoded. 735| returns | An input stream ready to use. 736 737### pb_read 738 739Read data from input stream. Always use this function, don't try to 740call the stream callback directly. 741 742 bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); 743 744| | | 745|----------------------|--------------------------------------------------------| 746| stream | Input stream to read from. 747| buf | Buffer to store the data to, or `NULL` to just read data without storing it anywhere. 748| count | Number of bytes to read. 749| returns | True on success, false if `stream->bytes_left` is less than `count` or if an IO error occurs. 750 751End of file is signalled by `stream->bytes_left` being zero after pb_read returns false. 752 753### pb_decode 754 755Read and decode all fields of a structure. Reads until EOF on input 756stream. 757 758 bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct); 759 760| | | 761|----------------------|--------------------------------------------------------| 762| stream | Input stream to read from. 763| fields | Message descriptor, usually autogenerated. 764| dest_struct | Pointer to message structure where data will be stored. 765| returns | True on success, false on any error condition. Error message will be in `stream->errmsg`. 766 767In Protocol Buffers binary format, end-of-file is only allowed between fields. 768If it happens anywhere else, pb_decode will return `false`. If 769pb_decode returns `false`, you cannot trust any of the data in the 770structure. 771 772For optional fields, this function applies the default value and sets 773`has_<field>` to false if the field is not present. 774 775If `PB_ENABLE_MALLOC` is defined, this function may allocate storage 776for any pointer type fields. In this case, you have to call 777[pb_release](#pb_release) to release the memory after you are done with 778the message. On error return `pb_decode` will release the memory itself. 779 780### pb_decode_ex 781 782Same as [pb_decode](#pb_decode), but allows extended options. 783 784 bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags); 785 786| | | 787|----------------------|--------------------------------------------------------| 788| stream | Input stream to read from. 789| fields | Message descriptor, usually autogenerated. 790| dest_struct | Pointer to message structure where data will be stored. 791| flags | Extended options, see below 792| returns | True on success, false on any error condition. Error message will be in `stream->errmsg`. 793 794The following options can be defined and combined with bitwise `|` operator: 795 796* `PB_DECODE_NOINIT`: Do not initialize structure before decoding. This can be used to combine multiple messages, or if you have already initialized the message structure yourself. 797 798* `PB_DECODE_DELIMITED`: Expect a length prefix in varint format before message. The counterpart of `PB_ENCODE_DELIMITED`. 799 800* `PB_DECODE_NULLTERMINATED`: Expect the message to be terminated with zero tag. The counterpart of `PB_ENCODE_NULLTERMINATED`. 801 802If `PB_ENABLE_MALLOC` is defined, this function may allocate storage 803for any pointer type fields. In this case, you have to call 804[pb_release](#pb_release) to release the memory after you are done with 805the message. On error return `pb_decode_ex` will release the memory 806itself. 807 808### pb_release 809 810Releases any dynamically allocated fields: 811 812 void pb_release(const pb_msgdesc_t *fields, void *dest_struct); 813 814| | | 815|----------------------|--------------------------------------------------------| 816| fields | Message descriptor, usually autogenerated. 817| dest_struct | Pointer to structure where data is stored. If `NULL`, function does nothing. 818 819This function is only available if `PB_ENABLE_MALLOC` is defined. It 820will release any pointer type fields in the structure and set the 821pointers to `NULL`. 822 823This function is safe to call multiple times, calling it again does nothing. 824 825### pb_decode_tag 826 827Decode the tag that comes before field in the protobuf encoding: 828 829 bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); 830 831| | | 832|----------------------|--------------------------------------------------------| 833| stream | Input stream to read from. 834| wire_type | Pointer to variable where to store the wire type of the field. 835| tag | Pointer to variable where to store the tag of the field. 836| eof | Pointer to variable where to store end-of-file status. 837| returns | True on success, false on error or EOF. 838 839When the message (stream) ends, this function will return `false` and set 840`eof` to true. On other errors, `eof` will be set to false. 841 842### pb_skip_field 843 844Remove the data for a field from the stream, without actually decoding it: 845 846 bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); 847 848| | | 849|----------------------|--------------------------------------------------------| 850| stream | Input stream to read from. 851| wire_type | Type of field to skip. 852| returns | True on success, false on IO error. 853 854This function determines the amount of bytes to read based on the wire type. 855For `PB_WT_STRING`, it will read the length prefix of a string or submessage 856to determine its length. 857 858### Callback field decoders 859The functions with names `pb_decode_<datatype>` are used when dealing with callback fields. 860The typical reason for using callbacks is to have an array of unlimited size. 861In that case, [pb_decode](#pb_decode) will call your callback function repeatedly, 862which can then store the values into e.g. filesystem in the order received in. 863 864For decoding numeric (including enumerated and boolean) values, use 865[pb_decode_varint](#pb_decode_varint), [pb_decode_svarint](#pb_decode_svarint), 866[pb_decode_fixed32](#pb_decode_fixed32) and [pb_decode_fixed64](#pb_decode_fixed64). 867They take a pointer to a 32- or 64-bit C variable, which you may then cast to smaller datatype for storage. 868 869For decoding strings and bytes fields, the length has already been decoded and the callback function is given a length-limited substream. 870You can therefore check the total length in `stream->bytes_left` and read the data using [pb_read](#pb_read). 871 872Finally, for decoding submessages in a callback, use [pb_decode](#pb_decode) and pass it the `SubMessage_fields` descriptor array. 873 874#### pb_decode_varint 875 876Read and decode a [varint](http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints) 877encoded integer. 878 879 bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); 880 881| | | 882|----------------------|--------------------------------------------------------| 883| stream | Input stream to read from. 1-10 bytes will be read. 884| dest | Storage for the decoded integer. Value is undefined on error. 885| returns | True on success, false if value exceeds uint64_t range or an IO error happens. 886 887#### pb_decode_varint32 888 889Same as `pb_decode_varint`, but limits the value to 32 bits: 890 891 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest); 892 893Parameters are the same as `pb_decode_varint`. This function can be used 894for decoding lengths and other commonly occurring elements that you know 895shouldn't be larger than 32 bit. It will return an error if the value 896exceeds the `uint32_t` datatype. 897 898#### pb_decode_svarint 899 900Similar to [pb_decode_varint](#pb_decode_varint), except that it 901performs zigzag-decoding on the value. This corresponds to the Protocol 902Buffers `sint32` and `sint64` datatypes. : 903 904 bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); 905 906(parameters are the same as [pb_decode_varint](#pb_decode_varint)) 907 908#### pb_decode_fixed32 909 910Decode a `fixed32`, `sfixed32` or `float` value. 911 912 bool pb_decode_fixed32(pb_istream_t *stream, void *dest); 913 914| | | 915|----------------------|--------------------------------------------------------| 916| stream | Input stream to read from. 4 bytes will be read. 917| dest | Pointer to destination `int32_t`, `uint32_t` or `float`. 918| returns | True on success, false on IO errors. 919 920This function reads 4 bytes from the input stream. On big endian 921architectures, it then reverses the order of the bytes. Finally, it 922writes the bytes to `dest`. 923 924#### pb_decode_fixed64 925 926Decode a `fixed64`, `sfixed64` or `double` value. : 927 928 bool pb_decode_fixed64(pb_istream_t *stream, void *dest); 929 930| | | 931|----------------------|--------------------------------------------------------| 932| stream | Input stream to read from. 8 bytes will be read. 933| dest | Pointer to destination `int64_t`, `uint64_t` or `double`. 934| returns | True on success, false on IO errors. 935 936Same as [pb_decode_fixed32](#pb_decode_fixed32), except this reads 8 937bytes. 938 939#### pb_decode_double_as_float 940 941Decodes a 64-bit `double` value into a 32-bit `float` 942variable. Counterpart of [pb_encode_float_as_double](#pb_encode_float_as_double). : 943 944 bool pb_decode_double_as_float(pb_istream_t *stream, float *dest); 945 946| | | 947|----------------------|--------------------------------------------------------| 948| stream | Input stream to read from. 8 bytes will be read. 949| dest | Pointer to destination *float*. 950| returns | True on success, false on IO errors. 951 952#### pb_make_string_substream 953 954Decode the length for a field with wire type `PB_WT_STRING` and create 955a substream for reading the data. 956 957 bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); 958 959| | | 960|----------------------|--------------------------------------------------------| 961| stream | Original input stream to read the length and data from. 962| substream | Storage for a new substream that has limited length. Filled in by the function. 963| returns | True on success, false if reading the length fails. 964 965This function uses `pb_decode_varint` to read an integer from the stream. 966This is interpreted as a number of bytes, and the substream is set up so that its `bytes_left` is initially the same as the 967length, and its callback function and state the same as the parent stream. 968 969#### pb_close_string_substream 970 971Close the substream created with 972[pb_make_string_substream](#pb_make_string_substream). 973 974 void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); 975 976| | | 977|----------------------|--------------------------------------------------------| 978| stream | Original input stream to read data from. 979| substream | Substream to close 980 981This function copies back the state from the substream to the parent stream, 982and throws away any unread data from the substream. 983It must be called after done with the substream. 984 985## pb_common.h 986 987### pb_field_iter_begin 988 989Begins iterating over the fields in a message type: 990 991 bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_msgdesc_t *desc, void *message); 992 993| | | 994|----------------------|--------------------------------------------------------| 995| iter | Pointer to destination [pb_field_iter_t](#pb_field_iter_t) variable. 996| desc | Autogenerated message descriptor. 997| message | Pointer to message structure. 998| returns | True on success, false if the message type has no fields. 999 1000### pb_field_iter_next 1001 1002Advance to the next field in the message: 1003 1004 bool pb_field_iter_next(pb_field_iter_t *iter); 1005 1006| | | 1007|----------------------|--------------------------------------------------------| 1008| iter | Pointer to `pb_field_iter_t` previously initialized by [pb_field_iter_begin](#pb_field_iter_begin). 1009| returns | True on success, false after last field in the message. 1010 1011When the last field in the message has been processed, this function 1012will return false and initialize `iter` back to the first field in the 1013message. 1014 1015### pb_field_iter_find 1016 1017Find a field specified by tag number in the message: 1018 1019 bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag); 1020 1021| | | 1022|----------------------|--------------------------------------------------------| 1023| iter | Pointer to `pb_field_iter_t` previously initialized by [pb_field_iter_begin](#pb_field_iter_begin). 1024| tag | Tag number to search for. 1025| returns | True if field was found, false otherwise. 1026 1027This function is functionally identical to calling `pb_field_iter_next()` until `iter.tag` equals the searched value. 1028Internally this function avoids fully processing the descriptor for intermediate fields. 1029 1030### pb_validate_utf8 1031 1032Validates an UTF8 encoded string: 1033 1034 bool pb_validate_utf8(const char *s); 1035 1036| | | 1037|----------------------|--------------------------------------------------------| 1038| s | Pointer to beginning of a string. 1039| returns | True, if string is valid UTF-8, false otherwise. 1040 1041The protobuf standard requires that `string` fields only contain valid 1042UTF-8 encoded text, while `bytes` fields can contain arbitrary data. 1043When the compilation option `PB_VALIDATE_UTF8` is defined, nanopb will 1044automatically validate strings on both encoding and decoding. 1045 1046User code can call this function to validate strings in e.g. custom 1047callbacks. 1048