17ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c. 27ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * The main function is pb_decode. You also need an input stream, and the 37ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * field descriptions created by nanopb_generator.py. 47ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 57ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 67ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef _PB_DECODE_H_ 77ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#define _PB_DECODE_H_ 87ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 97ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "pb.h" 107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef __cplusplus 127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenextern "C" { 137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Structure for defining custom input streams. You will need to provide 167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * a callback function to read the bytes from your storage, which can be 177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * for example a file or a network socket. 187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * The callback must conform to these rules: 207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 1) Return false on IO errors. This will cause decoding to abort. 227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 2) You can use state to store your own data (e.g. buffer pointer), 237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * and rely on pb_read to verify that no-body reads past bytes_left. 247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 3) Your callback may be used with substreams, in which case bytes_left 257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * is different than from the main stream. Don't use bytes_left to compute 267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * any pointers. 277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstruct _pb_istream_t 297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen{ 307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef PB_BUFFER_ONLY 317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen /* Callback pointer is not used in buffer-only configuration. 327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Having an int pointer here allows binary compatibility but 337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * gives an error if someone tries to assign callback function. 347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *callback; 367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#else 377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count); 387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *state; /* Free field for use by callback implementation */ 417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t bytes_left; 427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef PB_NO_ERRMSG 447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const char *errmsg; 457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen}; 477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/*************************** 497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Main decoding functions * 507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ***************************/ 517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Decode a single protocol buffers message from input stream into a C structure. 537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Returns true on success, false on any failure. 547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * The actual struct pointed to by dest must match the description in fields. 557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Callback fields of the destination structure must be initialized by caller. 567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * All other fields will be initialized by this function. 577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Example usage: 597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * MyMessage msg = {}; 607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * uint8_t buffer[64]; 617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * pb_istream_t stream; 627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * // ... read some data into buffer ... 647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * stream = pb_istream_from_buffer(buffer, count); 667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * pb_decode(&stream, MyMessage_fields, &msg); 677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Same as pb_decode, except does not initialize the destination structure 717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * to default values. This is slightly faster if you need no default values 727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * and just do memset(struct, 0, sizeof(struct)) yourself. 737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * This can also be used for 'merging' two messages, i.e. update only the 757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * fields that exist in the new message. 767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Note: If this function returns with an error, it will not release any 787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * dynamically allocated fields. You will need to call pb_release() yourself. 797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Same as pb_decode, except expects the stream to start with the message size 837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * encoded as varint. Corresponds to parseDelimitedFrom() in Google's 847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * protobuf API. 857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef PB_ENABLE_MALLOC 897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Release any allocated pointer fields. If you use dynamic allocation, you should 907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * call this for any successfully decoded message when you are done with it. If 917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * pb_decode() returns with an error, the message is already released. 927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid pb_release(const pb_field_t fields[], void *dest_struct); 947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/************************************** 987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Functions for manipulating streams * 997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen **************************************/ 1007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Create an input stream for reading from a memory buffer. 1027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * 1037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Alternatively, you can use a custom stream that reads directly from e.g. 1047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * a file or a network socket. 1057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 1067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenpb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize); 1077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Function to read from a pb_istream_t. You can use this if you need to 1097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * read some custom header data, or to read data in field callbacks. 1107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen */ 1117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count); 1127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/************************************************ 1157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * Helper functions for writing field callbacks * 1167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ************************************************/ 1177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Decode the tag for the next field in the stream. Gives the wire type and 1197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * field tag. At end of the message, returns false and sets eof to true. */ 1207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); 1217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Skip the field payload data, given the wire type. */ 1237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); 1247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Decode an integer in the varint format. This works for bool, enum, int32, 1267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * int64, uint32 and uint64 field types. */ 1277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); 1287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Decode an integer in the zig-zagged svarint format. This works for sint32 1307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * and sint64. */ 1317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); 1327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to 1347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * a 4-byte wide C variable. */ 1357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_fixed32(pb_istream_t *stream, void *dest); 1367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to 1387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen * a 8-byte wide C variable. */ 1397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_decode_fixed64(pb_istream_t *stream, void *dest); 1407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen/* Make a limited-length substream for reading a PB_WT_STRING field. */ 1427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenbool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); 1437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); 1447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef __cplusplus 1467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} /* extern "C" */ 1477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 1487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 150