Merge branch 'dev-0.2'
[apps/agl-service-can-low-level.git] / pb_encode.h
1 #ifndef _PB_ENCODE_H_
2 #define _PB_ENCODE_H_
3
4 /* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
5  * The main function is pb_encode. You also need an output stream, structures
6  * and their field descriptions (just like with pb_decode).
7  */
8
9 #include <stdbool.h>
10 #include "pb.h"
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 /* Lightweight output stream.
17  * You can provide callback for writing or use pb_ostream_from_buffer.
18  * 
19  * Alternatively, callback can be NULL in which case the stream will just
20  * count the number of bytes that would have been written. In this case
21  * max_size is not checked.
22  *
23  * Rules for callback:
24  * 1) Return false on IO errors. This will cause encoding to abort.
25  * 
26  * 2) You can use state to store your own data (e.g. buffer pointer).
27  * 
28  * 3) pb_write will update bytes_written after your callback runs.
29  * 
30  * 4) Substreams will modify max_size and bytes_written. Don't use them to
31  * calculate any pointers.
32  */
33 struct _pb_ostream_t
34 {
35 #ifdef PB_BUFFER_ONLY
36     /* Callback pointer is not used in buffer-only configuration.
37      * Having an int pointer here allows binary compatibility but
38      * gives an error if someone tries to assign callback function.
39      * Also, NULL pointer marks a 'sizing stream' that does not
40      * write anything.
41      */
42     int *callback;
43 #else
44     bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count);
45 #endif
46     void *state; /* Free field for use by callback implementation */
47     size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
48     size_t bytes_written;
49 };
50
51 pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize);
52 bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count);
53
54 /* Encode struct to given output stream.
55  * Returns true on success, false on any failure.
56  * The actual struct pointed to by src_struct must match the description in fields.
57  * All required fields in the struct are assumed to have been filled in.
58  */
59 bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
60
61 /* --- Helper functions ---
62  * You may want to use these from your caller or callbacks.
63  */
64
65 /* Encode field header based on LTYPE and field number defined in the field structure.
66  * Call this from the callback before writing out field contents. */
67 bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field);
68
69 /* Encode field header by manually specifing wire type. You need to use this if
70  * you want to write out packed arrays from a callback field. */
71 bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number);
72
73 /* Encode an integer in the varint format.
74  * This works for bool, enum, int32, int64, uint32 and uint64 field types. */
75 bool pb_encode_varint(pb_ostream_t *stream, uint64_t value);
76
77 /* Encode an integer in the zig-zagged svarint format.
78  * This works for sint32 and sint64. */
79 bool pb_encode_svarint(pb_ostream_t *stream, int64_t value);
80
81 /* Encode a string or bytes type field. For strings, pass strlen(s) as size. */
82 bool pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, size_t size);
83
84 /* Encode a fixed32, sfixed32 or float value.
85  * You need to pass a pointer to a 4-byte wide C variable. */
86 bool pb_encode_fixed32(pb_ostream_t *stream, const void *value);
87
88 /* Encode a fixed64, sfixed64 or double value.
89  * You need to pass a pointer to a 8-byte wide C variable. */
90 bool pb_encode_fixed64(pb_ostream_t *stream, const void *value);
91
92 /* Encode a submessage field.
93  * You need to pass the pb_field_t array and pointer to struct, just like with pb_encode().
94  * This internally encodes the submessage twice, first to calculate message size and then to actually write it out.
95  */
96 bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct);
97
98 /* --- Internal functions ---
99  * These functions are not terribly useful for the average library user, but
100  * are exported to make the unit testing and extending nanopb easier.
101  */
102
103 #ifdef NANOPB_INTERNALS
104 bool pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
105 bool pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src);
106 bool pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src);
107 bool pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src);
108 bool pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src);
109 bool pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src);
110 #endif
111
112 /* This function is not recommended for new programs. Use pb_encode_submessage()
113  * instead, it has the same functionality with a less confusing interface. */
114 bool pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src);
115
116 #ifdef __cplusplus
117 } /* extern "C" */
118 #endif
119
120 #endif