summaryrefslogtreecommitdiff
path: root/src/upb.h
blob: af026f542d45d084b87e413bd1f9753659382e6c (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * upb - a minimalist implementation of protocol buffers.

 * Copyright (c) 2009 Joshua Haberman.  See LICENSE for details.
 *
 * This file contains shared definitions that are widely used across upb.
 */

#ifndef UPB_H_
#define UPB_H_

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>  /* for size_t. */
#include "descriptor_const.h"

#ifdef __cplusplus
extern "C" {
#endif

/* inline if possible, emit standalone code if required. */
#ifndef INLINE
#define INLINE static inline
#endif

#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))

/* The maximum that any submessages can be nested.  Matches proto2's limit. */
#define UPB_MAX_NESTING 64

/* The maximum number of fields that any one .proto type can have. */
#define UPB_MAX_FIELDS (1<<16)

/* Nested type names are separated by periods. */
#define UPB_SYMBOL_SEPARATOR '.'
#define UPB_SYMBOL_MAX_LENGTH 256

#define UPB_INDEX(base, i, m) (void*)((char*)(base) + ((i)*(m)))

/* Fundamental types and type constants. **************************************/

/* A list of types as they are encoded on-the-wire. */
enum upb_wire_type {
  UPB_WIRE_TYPE_VARINT      = 0,
  UPB_WIRE_TYPE_64BIT       = 1,
  UPB_WIRE_TYPE_DELIMITED   = 2,
  UPB_WIRE_TYPE_START_GROUP = 3,
  UPB_WIRE_TYPE_END_GROUP   = 4,
  UPB_WIRE_TYPE_32BIT       = 5
};
typedef uint8_t upb_wire_type_t;

/* Value type as defined in a .proto file.  eg. string, int32, etc.
 *
 * The values of this are defined by google_protobuf_FieldDescriptorProto_Type
 * (from descriptor.proto).  Note that descriptor.proto reserves "0" for
 * errors, and we use it to represent exceptional circumstances. */
typedef uint8_t upb_field_type_t;

INLINE bool upb_issubmsgtype(upb_field_type_t type) {
  return type == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP  ||
         type == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE;
}

INLINE bool upb_isstringtype(upb_field_type_t type) {
  return type == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING  ||
         type == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES;
}

/* Information about a given value type (upb_field_type_t). */
struct upb_type_info {
  uint8_t align;
  uint8_t size;
  upb_wire_type_t expected_wire_type;
  char *ctype;
};

/* Contains information for all .proto types.  Indexed by upb_field_type_t. */
extern struct upb_type_info upb_type_info[];

/* The number of a field, eg. "optional string foo = 3". */
typedef int32_t upb_field_number_t;

/* Label (optional, repeated, required) as defined in a .proto file.  The values
 * of this are defined by google.protobuf.FieldDescriptorProto.Label (from
 * descriptor.proto). */
typedef uint8_t  upb_label_t;

/* A value as it is encoded on-the-wire, except delimited, which is handled
 * separately. */
union upb_wire_value {
  uint64_t varint;
  uint64_t _64bit;
  uint32_t _32bit;
};

/* A tag occurs before each value on-the-wire. */
struct upb_tag {
  upb_field_number_t field_number;
  upb_wire_type_t wire_type;
};

/* Polymorphic values of .proto types *****************************************/

struct upb_string;
struct upb_array;
struct upb_msg;

/* A single .proto value.  The owner must have an out-of-band way of knowing
 * the type, so that it knows which union member to use. */
union upb_value {
  double   _double;
  float    _float;
  int32_t  int32;
  int64_t  int64;
  uint32_t uint32;
  uint64_t uint64;
  bool     _bool;
  struct upb_string *str;
  struct upb_array *arr;
  struct upb_msg *msg;
};

/* A pointer to a .proto value.  The owner must have an out-of-band way of
 * knowing the type, so it knows which union member to use. */
union upb_value_ptr {
  double   *_double;
  float    *_float;
  int32_t  *int32;
  int64_t  *int64;
  uint32_t *uint32;
  uint64_t *uint64;
  bool     *_bool;
  struct upb_string **str;
  struct upb_array **arr;
  struct upb_msg **msg;
  void     *_void;
};

/* Unfortunately there is no way to define this so that it can be used as a
 * generic expression, a la:
 *   foo(UPB_VALUE_ADDROF(bar));
 * ...you have to use it as the initializer of a upb_value_ptr:
 *   union upb_value_ptr p = UPB_VALUE_ADDROF(bar);
 *   foo(p);
 */
#define UPB_VALUE_ADDROF(val) {(void*)&val._double}

/* Converts upb_value_ptr -> upb_value by "dereferencing" the pointer.  We need
 * to know the field type to perform this operation, because we need to know
 * how much memory to copy. */
INLINE union upb_value upb_value_read(union upb_value_ptr ptr,
                                      upb_field_type_t ft) {
  union upb_value val;
#define CASE(t, member_name) \
  case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## t: \
    val.member_name = *ptr.member_name; \
    break;
  switch(ft) {
    CASE(DOUBLE,   _double)
    CASE(FLOAT,    _float)
    CASE(INT32,    int32)
    CASE(INT64,    int64)
    CASE(UINT32,   uint32)
    CASE(UINT64,   uint64)
    CASE(SINT32,   int32)
    CASE(SINT64,   int64)
    CASE(FIXED32,  uint32)
    CASE(FIXED64,  uint64)
    CASE(SFIXED32, int32)
    CASE(SFIXED64, int64)
    CASE(BOOL,     _bool)
    CASE(ENUM,     int32)
    CASE(STRING,   str)
    CASE(BYTES,    str)
    CASE(MESSAGE,  msg)
    CASE(GROUP,    msg)
    default: break;
  }
#undef CASE
  return val;
}

/* Converts upb_value_ptr -> upb_value by "dereferencing" the pointer.  We need
 * to know the field type to perform this operation, because we need to know
 * how much memory to copy. */
INLINE void upb_value_write(union upb_value_ptr ptr, union upb_value val,
                            upb_field_type_t ft) {
#define CASE(t, member_name) \
  case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## t: \
    *ptr.member_name = val.member_name; \
    break;
  switch(ft) {
    CASE(DOUBLE,   _double)
    CASE(FLOAT,    _float)
    CASE(INT32,    int32)
    CASE(INT64,    int64)
    CASE(UINT32,   uint32)
    CASE(UINT64,   uint64)
    CASE(SINT32,   int32)
    CASE(SINT64,   int64)
    CASE(FIXED32,  uint32)
    CASE(FIXED64,  uint64)
    CASE(SFIXED32, int32)
    CASE(SFIXED64, int64)
    CASE(BOOL,     _bool)
    CASE(ENUM,     int32)
    CASE(STRING,   str)
    CASE(BYTES,    str)
    CASE(MESSAGE,  msg)
    CASE(GROUP,    msg)
    default: break;
  }
#undef CASE
}

union upb_symbol_ref {
  struct upb_msgdef *msg;
  struct upb_enum *_enum;
  struct upb_svc *svc;
};

/* Status codes used as a return value.  Codes >0 are not fatal and can be
 * resumed. */
typedef enum upb_status {
  UPB_STATUS_OK = 0,

  // The input byte stream ended in the middle of a record.
  UPB_STATUS_NEED_MORE_DATA = 1,

  // The user value callback opted to stop parsing.
  UPB_STATUS_USER_CANCELLED = 2,

  // A varint did not terminate before hitting 64 bits.
  UPB_ERROR_UNTERMINATED_VARINT = -1,

  // A submessage or packed array ended in the middle of data.
  UPB_ERROR_BAD_SUBMESSAGE_END = -2,

  // Input was nested more than UPB_MAX_NESTING deep.
  UPB_ERROR_STACK_OVERFLOW = -3,

  // The input data caused the pb's offset (a size_t) to overflow.
  UPB_ERROR_OVERFLOW = -4,

  // An "end group" tag was encountered in an inappropriate place.
  UPB_ERROR_SPURIOUS_END_GROUP = -5,

  UPB_ERROR_ILLEGAL = -6
} upb_status_t;

#define UPB_CHECK(func) do { \
  upb_status_t status = func; \
  if(status != UPB_STATUS_OK) return status; \
  } while (0)

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif  /* UPB_H_ */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback