2
0

pb_decode.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c.
  2. * The main function is pb_decode. You also need an input stream, and the
  3. * field descriptions created by nanopb_generator.py.
  4. */
  5. #ifndef PB_DECODE_H_INCLUDED
  6. #define PB_DECODE_H_INCLUDED
  7. #include "pb.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* Structure for defining custom input streams. You will need to provide
  12. * a callback function to read the bytes from your storage, which can be
  13. * for example a file or a network socket.
  14. *
  15. * The callback must conform to these rules:
  16. *
  17. * 1) Return false on IO errors. This will cause decoding to abort.
  18. * 2) You can use state to store your own data (e.g. buffer pointer),
  19. * and rely on pb_read to verify that no-body reads past bytes_left.
  20. * 3) Your callback may be used with substreams, in which case bytes_left
  21. * is different than from the main stream. Don't use bytes_left to compute
  22. * any pointers.
  23. */
  24. struct pb_istream_s
  25. {
  26. #ifdef PB_BUFFER_ONLY
  27. /* Callback pointer is not used in buffer-only configuration.
  28. * Having an int pointer here allows binary compatibility but
  29. * gives an error if someone tries to assign callback function.
  30. */
  31. int *callback;
  32. #else
  33. bool (*callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count);
  34. #endif
  35. void *state; /* Free field for use by callback implementation */
  36. size_t bytes_left;
  37. #ifndef PB_NO_ERRMSG
  38. const char *errmsg;
  39. #endif
  40. };
  41. #ifndef PB_NO_ERRMSG
  42. #define PB_ISTREAM_EMPTY {0,0,0,0}
  43. #else
  44. #define PB_ISTREAM_EMPTY {0,0,0}
  45. #endif
  46. /***************************
  47. * Main decoding functions *
  48. ***************************/
  49. /* Decode a single protocol buffers message from input stream into a C structure.
  50. * Returns true on success, false on any failure.
  51. * The actual struct pointed to by dest must match the description in fields.
  52. * Callback fields of the destination structure must be initialized by caller.
  53. * All other fields will be initialized by this function.
  54. *
  55. * Example usage:
  56. * MyMessage msg = {};
  57. * uint8_t buffer[64];
  58. * pb_istream_t stream;
  59. *
  60. * // ... read some data into buffer ...
  61. *
  62. * stream = pb_istream_from_buffer(buffer, count);
  63. * pb_decode(&stream, MyMessage_fields, &msg);
  64. */
  65. bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct);
  66. /* Extended version of pb_decode, with several options to control
  67. * the decoding process:
  68. *
  69. * PB_DECODE_NOINIT: Do not initialize the fields to default values.
  70. * This is slightly faster if you do not need the default
  71. * values and instead initialize the structure to 0 using
  72. * e.g. memset(). This can also be used for merging two
  73. * messages, i.e. combine already existing data with new
  74. * values.
  75. *
  76. * PB_DECODE_DELIMITED: Input message starts with the message size as varint.
  77. * Corresponds to parseDelimitedFrom() in Google's
  78. * protobuf API.
  79. *
  80. * PB_DECODE_NULLTERMINATED: Stop reading when field tag is read as 0. This allows
  81. * reading null terminated messages.
  82. * NOTE: Until nanopb-0.4.0, pb_decode() also allows
  83. * null-termination. This behaviour is not supported in
  84. * most other protobuf implementations, so PB_DECODE_DELIMITED
  85. * is a better option for compatibility.
  86. *
  87. * Multiple flags can be combined with bitwise or (| operator)
  88. */
  89. #define PB_DECODE_NOINIT 0x01U
  90. #define PB_DECODE_DELIMITED 0x02U
  91. #define PB_DECODE_NULLTERMINATED 0x04U
  92. bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags);
  93. /* Defines for backwards compatibility with code written before nanopb-0.4.0 */
  94. #define pb_decode_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NOINIT)
  95. #define pb_decode_delimited(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED)
  96. #define pb_decode_delimited_noinit(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_DELIMITED | PB_DECODE_NOINIT)
  97. #define pb_decode_nullterminated(s,f,d) pb_decode_ex(s,f,d, PB_DECODE_NULLTERMINATED)
  98. #ifdef PB_ENABLE_MALLOC
  99. /* Release any allocated pointer fields. If you use dynamic allocation, you should
  100. * call this for any successfully decoded message when you are done with it. If
  101. * pb_decode() returns with an error, the message is already released.
  102. */
  103. void pb_release(const pb_msgdesc_t *fields, void *dest_struct);
  104. #else
  105. /* Allocation is not supported, so release is no-op */
  106. #define pb_release(fields, dest_struct) PB_UNUSED(fields); PB_UNUSED(dest_struct);
  107. #endif
  108. /**************************************
  109. * Functions for manipulating streams *
  110. **************************************/
  111. /* Create an input stream for reading from a memory buffer.
  112. *
  113. * msglen should be the actual length of the message, not the full size of
  114. * allocated buffer.
  115. *
  116. * Alternatively, you can use a custom stream that reads directly from e.g.
  117. * a file or a network socket.
  118. */
  119. pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen);
  120. /* Function to read from a pb_istream_t. You can use this if you need to
  121. * read some custom header data, or to read data in field callbacks.
  122. */
  123. bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
  124. /************************************************
  125. * Helper functions for writing field callbacks *
  126. ************************************************/
  127. /* Decode the tag for the next field in the stream. Gives the wire type and
  128. * field tag. At end of the message, returns false and sets eof to true. */
  129. bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
  130. /* Skip the field payload data, given the wire type. */
  131. bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
  132. /* Decode an integer in the varint format. This works for enum, int32,
  133. * int64, uint32 and uint64 field types. */
  134. #ifndef PB_WITHOUT_64BIT
  135. bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
  136. #else
  137. #define pb_decode_varint pb_decode_varint32
  138. #endif
  139. /* Decode an integer in the varint format. This works for enum, int32,
  140. * and uint32 field types. */
  141. bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
  142. /* Decode a bool value in varint format. */
  143. bool pb_decode_bool(pb_istream_t *stream, bool *dest);
  144. /* Decode an integer in the zig-zagged svarint format. This works for sint32
  145. * and sint64. */
  146. #ifndef PB_WITHOUT_64BIT
  147. bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
  148. #else
  149. bool pb_decode_svarint(pb_istream_t *stream, int32_t *dest);
  150. #endif
  151. /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
  152. * a 4-byte wide C variable. */
  153. bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
  154. #ifndef PB_WITHOUT_64BIT
  155. /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
  156. * a 8-byte wide C variable. */
  157. bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
  158. #endif
  159. #ifdef PB_CONVERT_DOUBLE_FLOAT
  160. /* Decode a double value into float variable. */
  161. bool pb_decode_double_as_float(pb_istream_t *stream, float *dest);
  162. #endif
  163. /* Make a limited-length substream for reading a PB_WT_STRING field. */
  164. bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
  165. bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
  166. #ifdef __cplusplus
  167. } /* extern "C" */
  168. #endif
  169. #endif