You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.5 KiB

6 years ago
  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. /***************************
  42. * Main decoding functions *
  43. ***************************/
  44. /* Decode a single protocol buffers message from input stream into a C structure.
  45. * Returns true on success, false on any failure.
  46. * The actual struct pointed to by dest must match the description in fields.
  47. * Callback fields of the destination structure must be initialized by caller.
  48. * All other fields will be initialized by this function.
  49. *
  50. * Example usage:
  51. * MyMessage msg = {};
  52. * uint8_t buffer[64];
  53. * pb_istream_t stream;
  54. *
  55. * // ... read some data into buffer ...
  56. *
  57. * stream = pb_istream_from_buffer(buffer, count);
  58. * pb_decode(&stream, MyMessage_fields, &msg);
  59. */
  60. bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
  61. /* Same as pb_decode, except does not initialize the destination structure
  62. * to default values. This is slightly faster if you need no default values
  63. * and just do memset(struct, 0, sizeof(struct)) yourself.
  64. *
  65. * This can also be used for 'merging' two messages, i.e. update only the
  66. * fields that exist in the new message.
  67. *
  68. * Note: If this function returns with an error, it will not release any
  69. * dynamically allocated fields. You will need to call pb_release() yourself.
  70. */
  71. bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
  72. /* Same as pb_decode, except expects the stream to start with the message size
  73. * encoded as varint. Corresponds to parseDelimitedFrom() in Google's
  74. * protobuf API.
  75. */
  76. bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct);
  77. #ifdef PB_ENABLE_MALLOC
  78. /* Release any allocated pointer fields. If you use dynamic allocation, you should
  79. * call this for any successfully decoded message when you are done with it. If
  80. * pb_decode() returns with an error, the message is already released.
  81. */
  82. void pb_release(const pb_field_t fields[], void *dest_struct);
  83. #endif
  84. /**************************************
  85. * Functions for manipulating streams *
  86. **************************************/
  87. /* Create an input stream for reading from a memory buffer.
  88. *
  89. * Alternatively, you can use a custom stream that reads directly from e.g.
  90. * a file or a network socket.
  91. */
  92. pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize);
  93. /* Function to read from a pb_istream_t. You can use this if you need to
  94. * read some custom header data, or to read data in field callbacks.
  95. */
  96. bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
  97. /************************************************
  98. * Helper functions for writing field callbacks *
  99. ************************************************/
  100. /* Decode the tag for the next field in the stream. Gives the wire type and
  101. * field tag. At end of the message, returns false and sets eof to true. */
  102. bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
  103. /* Skip the field payload data, given the wire type. */
  104. bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
  105. /* Decode an integer in the varint format. This works for bool, enum, int32,
  106. * int64, uint32 and uint64 field types. */
  107. bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);
  108. /* Decode an integer in the varint format. This works for bool, enum, int32,
  109. * and uint32 field types. */
  110. bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest);
  111. /* Decode an integer in the zig-zagged svarint format. This works for sint32
  112. * and sint64. */
  113. bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest);
  114. /* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
  115. * a 4-byte wide C variable. */
  116. bool pb_decode_fixed32(pb_istream_t *stream, void *dest);
  117. /* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
  118. * a 8-byte wide C variable. */
  119. bool pb_decode_fixed64(pb_istream_t *stream, void *dest);
  120. /* Make a limited-length substream for reading a PB_WT_STRING field. */
  121. bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream);
  122. bool pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream);
  123. #ifdef __cplusplus
  124. } /* extern "C" */
  125. #endif
  126. #endif