1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#ifndef _PB_H_
#define _PB_H_
#include <stdint.h>
#include <stddef.h> // size_t
/* Lightweight input stream.
* If buf is NULL, read but don't store bytes. */
typedef struct _pb_istream_t pb_istream_t;
struct _pb_istream_t
{
bool (*callback)(pb_istream_t *stream, char *buf, size_t count);
void *state; // Free field for use by callback implementation
size_t bytes_left;
};
static inline bool pb_read(pb_istream_t *stream, char *buf, size_t count)
{
bool status = stream->callback(stream, buf, count);
stream->bytes_left -= count;
return status;
}
/* Lightweight output stream. */
typedef struct _pb_ostream_t pb_ostream_t;
struct _pb_ostream_t
{
bool (*callback)(pb_ostream_t *stream, const char *buf, size_t count);
void *state; // Free field for use by callback implementation
size_t bytes_written;
};
static inline bool pb_write(pb_ostream_t *stream, const char *buf, size_t count)
{
bool status = stream->callback(stream, buf, count);
stream->bytes_written += count;
return status;
}
/* List of possible decode/encode action types */
typedef enum {
// Special case. Sets boolean field to true, continues parsing the data.
PB_ACT_HAS,
// Standard integer types
PB_ACT_UINT32,
PB_ACT_SINT32,
PB_ACT_INT32,
PB_ACT_FIXED32,
PB_ACT_SFIXED32,
PB_ACT_UINT64,
PB_ACT_SINT64,
PB_ACT_INT64,
PB_ACT_FIXED64,
PB_ACT_SFIXED64,
PB_ACT_BOOL,
// Standard float types
PB_ACT_FLOAT,
PB_ACT_DOUBLE,
// Constant-sized array
PB_ACT_BYTES,
// Constant-sized array, with null termination
PB_ACT_STRING,
// Callback function pointer in field value
PB_ACT_SUBMESSAGE,
PB_LAST_ACT
} pb_action_t;
// This structure is used in constants to specify struct fields.
typedef struct {
int field_number;
uint16_t offset;
pb_action_t action;
uint8_t fieldsize;
} pb_field_t;
#define PB_LAST_FIELD {0,0,0,0}
/* --- Types to use inside generated structures. --- */
// Byte array and size word.
// Note: because of variable length array, this type cannot be directly used.
// Autogenerated code declares the same type of fields but with explicit length.
typedef struct {
size_t size;
char bytes[];
} pb_bytearray_t;
// This structure is used for giving the callback function.
typedef struct _pb_callback_t pb_callback_t;
struct _pb_callback_t {
union {
bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void *arg);
} funcs;
// Free arg for use by callback
void *arg;
};
#endif
|