pbwire 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/pbwire_ext/core_arena.c +322 -0
- data/ext/pbwire_ext/core_ctx.c +334 -0
- data/ext/pbwire_ext/core_decode.c +1735 -0
- data/ext/pbwire_ext/core_descriptor_set.c +1482 -0
- data/ext/pbwire_ext/core_encode.c +1606 -0
- data/ext/pbwire_ext/core_json.c +4792 -0
- data/ext/pbwire_ext/core_message.c +1100 -0
- data/ext/pbwire_ext/core_schema.c +1459 -0
- data/ext/pbwire_ext/core_stream.c +450 -0
- data/ext/pbwire_ext/core_text.c +2583 -0
- data/ext/pbwire_ext/core_wire.c +735 -0
- data/ext/pbwire_ext/core_wkt.c +255 -0
- data/ext/pbwire_ext/extconf.rb +8 -7
- data/ext/pbwire_ext/internal.h +980 -0
- data/ext/pbwire_ext/pbwire.h +746 -0
- metadata +15 -15
- data/.diffend-unextracted-0 +0 -34
|
@@ -0,0 +1,1482 @@
|
|
|
1
|
+
/* FileDescriptorSet loader: a standard descriptor set produced by protoc / buf
|
|
2
|
+
* becomes a pbw_schema, so callers get a runtime schema without generating code
|
|
3
|
+
* and without pbwire depending on protobuf.
|
|
4
|
+
*
|
|
5
|
+
* Only the fields that carry runtime semantics are read: names, numbers, types,
|
|
6
|
+
* labels, oneofs, the `packed` option, the file syntax and services (names
|
|
7
|
+
* plus method input/output types, for routing; the core does no RPC).
|
|
8
|
+
* Everything else (extensions, reserved ranges, source code info, other
|
|
9
|
+
* options) is skipped like any unknown field.
|
|
10
|
+
*
|
|
11
|
+
* Names are stored fully qualified without the leading dot: a message `Inner`
|
|
12
|
+
* nested in `Outer` in package `a.b` is "a.b.Outer.Inner". Field type names are
|
|
13
|
+
* normalized the same way.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#include "internal.h"
|
|
17
|
+
|
|
18
|
+
#include <stdarg.h>
|
|
19
|
+
#include <stdio.h>
|
|
20
|
+
|
|
21
|
+
/* buffer size for a fully qualified name; longer names are rejected */
|
|
22
|
+
#define DS_MAX_NAME 512
|
|
23
|
+
/* proto2 defaults are short literals; longer ones are rejected */
|
|
24
|
+
#define DS_MAX_DEFAULT 4096
|
|
25
|
+
|
|
26
|
+
/* Editions FeatureSet resolution: file > message/enum > field, explicit
|
|
27
|
+
* FieldOptions.packed still wins over every repeated_field_encoding.
|
|
28
|
+
* Values are the FeatureSet enum numbers (0 = unset/unknown):
|
|
29
|
+
* presence 1 EXPLICIT, 2 IMPLICIT, 3 LEGACY_REQUIRED; enum_open 1 OPEN,
|
|
30
|
+
* 2 CLOSED; repack 1 PACKED, 2 EXPANDED; msg_enc 1 LENGTH_PREFIXED,
|
|
31
|
+
* 2 DELIMITED; utf8 2 VERIFY, 3 NONE (the reference's FeatureSet enum, where
|
|
32
|
+
* VERIFY is the proto3/editions default and NONE the proto2 one).
|
|
33
|
+
* Loader-internal: presence/repack/msg_enc/utf8 resolve to single values at
|
|
34
|
+
* load, enum_open stays per-field (inherit = 0). */
|
|
35
|
+
typedef struct {
|
|
36
|
+
int presence;
|
|
37
|
+
int enum_open;
|
|
38
|
+
int repack;
|
|
39
|
+
int msg_enc;
|
|
40
|
+
int utf8;
|
|
41
|
+
} ds_features;
|
|
42
|
+
|
|
43
|
+
/* One parsed FieldDescriptorProto: regular fields and extensions share the
|
|
44
|
+
* wire shape, only the presence of `extendee` tells them apart. */
|
|
45
|
+
typedef struct {
|
|
46
|
+
char name[DS_MAX_NAME];
|
|
47
|
+
char extendee[DS_MAX_NAME];
|
|
48
|
+
char type_name[DS_MAX_NAME];
|
|
49
|
+
char default_value[DS_MAX_DEFAULT];
|
|
50
|
+
uint32_t number;
|
|
51
|
+
uint32_t type;
|
|
52
|
+
uint32_t label;
|
|
53
|
+
uint32_t oneof;
|
|
54
|
+
int has_number;
|
|
55
|
+
int has_type;
|
|
56
|
+
int has_oneof;
|
|
57
|
+
int has_default;
|
|
58
|
+
int proto3_optional; /* FieldDescriptorProto.proto3_optional: explicit presence */
|
|
59
|
+
int packed; /* -1 when the options do not say */
|
|
60
|
+
ds_features feat; /* per-field FeatureSet override, zeros when absent */
|
|
61
|
+
} ds_field_info;
|
|
62
|
+
|
|
63
|
+
/* An extension waiting for its extendee: attach after every file is read, so
|
|
64
|
+
* order between files does not matter. `scope` is the declaring scope
|
|
65
|
+
* (containing message, or the file package) for the extension's full name. */
|
|
66
|
+
typedef struct {
|
|
67
|
+
char extendee[DS_MAX_NAME];
|
|
68
|
+
char scope[DS_MAX_NAME];
|
|
69
|
+
ds_field_info info;
|
|
70
|
+
/* file/message FeatureSet defaults of the declaring file, resolved at
|
|
71
|
+
* queue time (the extendee may live in another file). */
|
|
72
|
+
ds_features file;
|
|
73
|
+
} ds_pending_ext;
|
|
74
|
+
|
|
75
|
+
typedef struct {
|
|
76
|
+
uint32_t field;
|
|
77
|
+
uint32_t wire;
|
|
78
|
+
uint64_t varint; /* PBW_WIRE_VARINT */
|
|
79
|
+
const uint8_t *data;
|
|
80
|
+
size_t len;
|
|
81
|
+
} ds_field;
|
|
82
|
+
|
|
83
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
84
|
+
__attribute__((format(printf, 2, 3)))
|
|
85
|
+
#endif
|
|
86
|
+
static void
|
|
87
|
+
ds_error(pbw_schema *schema, const char *fmt, ...)
|
|
88
|
+
{
|
|
89
|
+
va_list ap;
|
|
90
|
+
|
|
91
|
+
va_start(ap, fmt);
|
|
92
|
+
vsnprintf(schema->error, sizeof(schema->error), fmt, ap);
|
|
93
|
+
va_end(ap);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* Reads the next field. 1 = field, 0 = end, -1 = malformed. Legacy groups never
|
|
97
|
+
* appear in descriptor.proto, so their wire types are rejected. */
|
|
98
|
+
static int
|
|
99
|
+
ds_next(const uint8_t **pp, const uint8_t *end, ds_field *f)
|
|
100
|
+
{
|
|
101
|
+
const uint8_t *p;
|
|
102
|
+
uint64_t value;
|
|
103
|
+
size_t used;
|
|
104
|
+
uint32_t tag;
|
|
105
|
+
|
|
106
|
+
if (pp == NULL || end == NULL || f == NULL || *pp == NULL) {
|
|
107
|
+
return -1;
|
|
108
|
+
}
|
|
109
|
+
p = *pp;
|
|
110
|
+
if (p >= end) {
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
if (!pbw_tag_read(p, end, &tag, &used)) {
|
|
114
|
+
return -1;
|
|
115
|
+
}
|
|
116
|
+
p += used;
|
|
117
|
+
f->field = tag >> 3;
|
|
118
|
+
f->wire = tag & 7u;
|
|
119
|
+
f->varint = 0;
|
|
120
|
+
f->data = NULL;
|
|
121
|
+
f->len = 0;
|
|
122
|
+
if (f->field == 0) {
|
|
123
|
+
return -1;
|
|
124
|
+
}
|
|
125
|
+
switch (f->wire) {
|
|
126
|
+
case PBW_WIRE_VARINT:
|
|
127
|
+
if (!pbw_varint_read(p, end, &value, &used)) {
|
|
128
|
+
return -1;
|
|
129
|
+
}
|
|
130
|
+
*pp = p + used;
|
|
131
|
+
f->varint = value;
|
|
132
|
+
return 1;
|
|
133
|
+
case PBW_WIRE_LEN: {
|
|
134
|
+
size_t len;
|
|
135
|
+
|
|
136
|
+
if (!pbw_len_read(p, end, &len, &used)) {
|
|
137
|
+
return -1;
|
|
138
|
+
}
|
|
139
|
+
p += used;
|
|
140
|
+
if (len > pbw_remaining(end, p)) {
|
|
141
|
+
return -1;
|
|
142
|
+
}
|
|
143
|
+
f->data = p;
|
|
144
|
+
f->len = len;
|
|
145
|
+
*pp = p + len;
|
|
146
|
+
return 1;
|
|
147
|
+
}
|
|
148
|
+
case PBW_WIRE_64BIT:
|
|
149
|
+
case PBW_WIRE_32BIT: {
|
|
150
|
+
size_t size = f->wire == PBW_WIRE_64BIT ? 8u : 4u;
|
|
151
|
+
if (pbw_remaining(end, p) < size) {
|
|
152
|
+
return -1;
|
|
153
|
+
}
|
|
154
|
+
f->data = p;
|
|
155
|
+
f->len = size;
|
|
156
|
+
*pp = p + size;
|
|
157
|
+
return 1;
|
|
158
|
+
}
|
|
159
|
+
default:
|
|
160
|
+
return -1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/* Copies a length-delimited string out of the wire. An empty string is not an
|
|
165
|
+
* error: `package` and `type_name` are legitimately absent. */
|
|
166
|
+
static pbw_status
|
|
167
|
+
ds_str(char *buf, size_t cap, const ds_field *f)
|
|
168
|
+
{
|
|
169
|
+
if (buf == NULL || cap == 0 || f == NULL) {
|
|
170
|
+
return PBW_ERR_INVALID;
|
|
171
|
+
}
|
|
172
|
+
if (f->wire != PBW_WIRE_LEN) {
|
|
173
|
+
return PBW_ERR_INVALID;
|
|
174
|
+
}
|
|
175
|
+
if (f->data == NULL && f->len > 0) {
|
|
176
|
+
return PBW_ERR_INVALID;
|
|
177
|
+
}
|
|
178
|
+
if (f->len == 0) {
|
|
179
|
+
buf[0] = '\0';
|
|
180
|
+
return PBW_OK;
|
|
181
|
+
}
|
|
182
|
+
if (f->len >= cap) {
|
|
183
|
+
return PBW_ERR_LIMIT;
|
|
184
|
+
}
|
|
185
|
+
memcpy(buf, f->data, f->len);
|
|
186
|
+
buf[f->len] = '\0';
|
|
187
|
+
return PBW_OK;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/* Field 1 of a DescriptorProto / EnumDescriptorProto. */
|
|
191
|
+
static pbw_status
|
|
192
|
+
ds_local_name(char *buf, size_t cap, const uint8_t *p, const uint8_t *end)
|
|
193
|
+
{
|
|
194
|
+
ds_field f;
|
|
195
|
+
|
|
196
|
+
for (;;) {
|
|
197
|
+
int rc = ds_next(&p, end, &f);
|
|
198
|
+
if (rc < 0) {
|
|
199
|
+
return PBW_ERR_INVALID;
|
|
200
|
+
}
|
|
201
|
+
if (rc == 0) {
|
|
202
|
+
return PBW_ERR_INVALID; /* no name */
|
|
203
|
+
}
|
|
204
|
+
if (f.field == 1) {
|
|
205
|
+
return ds_str(buf, cap, &f);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static pbw_status
|
|
211
|
+
ds_fqname(pbw_schema *schema, char *out, size_t cap, const char *parent,
|
|
212
|
+
const uint8_t *p, const uint8_t *end)
|
|
213
|
+
{
|
|
214
|
+
char local[DS_MAX_NAME];
|
|
215
|
+
pbw_status status;
|
|
216
|
+
int written;
|
|
217
|
+
|
|
218
|
+
if ((status = ds_local_name(local, sizeof(local), p, end)) != PBW_OK) {
|
|
219
|
+
ds_error(schema, "descriptor without a name");
|
|
220
|
+
return status;
|
|
221
|
+
}
|
|
222
|
+
written = parent[0] == '\0' ? snprintf(out, cap, "%s", local)
|
|
223
|
+
: snprintf(out, cap, "%s.%s", parent, local);
|
|
224
|
+
if (written < 0 || (size_t)written >= cap) {
|
|
225
|
+
ds_error(schema, "name too long: %.64s...", local);
|
|
226
|
+
return PBW_ERR_LIMIT;
|
|
227
|
+
}
|
|
228
|
+
return PBW_OK;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/* Parses a FeatureSet message (fields 1,2,3,5); utf8_validation and
|
|
232
|
+
* json_format carry no codec semantics here. */
|
|
233
|
+
static pbw_status
|
|
234
|
+
ds_parse_featureset(const uint8_t *p, const uint8_t *end, ds_features *out)
|
|
235
|
+
{
|
|
236
|
+
ds_field f;
|
|
237
|
+
|
|
238
|
+
for (;;) {
|
|
239
|
+
int rc = ds_next(&p, end, &f);
|
|
240
|
+
uint64_t v;
|
|
241
|
+
|
|
242
|
+
if (rc < 0) {
|
|
243
|
+
return PBW_ERR_INVALID;
|
|
244
|
+
}
|
|
245
|
+
if (rc == 0) {
|
|
246
|
+
return PBW_OK;
|
|
247
|
+
}
|
|
248
|
+
if (f.wire != PBW_WIRE_VARINT) {
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
v = f.varint;
|
|
252
|
+
switch (f.field) {
|
|
253
|
+
case 1: /* field_presence */
|
|
254
|
+
if (v >= 1 && v <= 3) {
|
|
255
|
+
out->presence = (int)v;
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
case 2: /* enum_type */
|
|
259
|
+
if (v >= 1 && v <= 2) {
|
|
260
|
+
out->enum_open = (int)v;
|
|
261
|
+
}
|
|
262
|
+
break;
|
|
263
|
+
case 3: /* repeated_field_encoding */
|
|
264
|
+
if (v >= 1 && v <= 2) {
|
|
265
|
+
out->repack = (int)v;
|
|
266
|
+
}
|
|
267
|
+
break;
|
|
268
|
+
case 4: /* utf8_validation: 2 VERIFY, 3 NONE */
|
|
269
|
+
if (v >= 2 && v <= 3) {
|
|
270
|
+
out->utf8 = (int)v;
|
|
271
|
+
}
|
|
272
|
+
break;
|
|
273
|
+
case 5: /* message_encoding */
|
|
274
|
+
if (v >= 1 && v <= 2) {
|
|
275
|
+
out->msg_enc = (int)v;
|
|
276
|
+
}
|
|
277
|
+
break;
|
|
278
|
+
default:
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/* FieldOptions.packed (field 2), when the schema spells it out, plus the
|
|
285
|
+
* per-field FeatureSet override (field 21). */
|
|
286
|
+
static pbw_status
|
|
287
|
+
ds_field_options(const uint8_t *p, const uint8_t *end, int *packed,
|
|
288
|
+
ds_features *feat)
|
|
289
|
+
{
|
|
290
|
+
ds_field f;
|
|
291
|
+
|
|
292
|
+
for (;;) {
|
|
293
|
+
int rc = ds_next(&p, end, &f);
|
|
294
|
+
if (rc < 0) {
|
|
295
|
+
return PBW_ERR_INVALID;
|
|
296
|
+
}
|
|
297
|
+
if (rc == 0) {
|
|
298
|
+
return PBW_OK;
|
|
299
|
+
}
|
|
300
|
+
if (f.field == 2 && f.wire == PBW_WIRE_VARINT) {
|
|
301
|
+
*packed = f.varint != 0;
|
|
302
|
+
} else if (f.field == 21 && f.wire == PBW_WIRE_LEN) {
|
|
303
|
+
pbw_status status =
|
|
304
|
+
ds_parse_featureset(f.data, f.data + f.len, feat);
|
|
305
|
+
if (status != PBW_OK) {
|
|
306
|
+
return status;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/* Parses one FieldDescriptorProto. Field 2 (`extendee`) is only set on
|
|
313
|
+
* extensions; field 7 is the proto2 default; field 10 (`json_name`) carries no
|
|
314
|
+
* runtime semantics yet. */
|
|
315
|
+
static pbw_status
|
|
316
|
+
ds_parse_field(ds_field_info *out, const uint8_t *p, const uint8_t *end)
|
|
317
|
+
{
|
|
318
|
+
ds_field f;
|
|
319
|
+
pbw_status status;
|
|
320
|
+
|
|
321
|
+
memset(out, 0, sizeof(*out));
|
|
322
|
+
out->label = PBW_LABEL_OPTIONAL;
|
|
323
|
+
out->packed = -1;
|
|
324
|
+
for (;;) {
|
|
325
|
+
int rc = ds_next(&p, end, &f);
|
|
326
|
+
if (rc < 0) {
|
|
327
|
+
return PBW_ERR_INVALID;
|
|
328
|
+
}
|
|
329
|
+
if (rc == 0) {
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
switch (f.field) {
|
|
333
|
+
case 1: /* name */
|
|
334
|
+
if ((status = ds_str(out->name, sizeof(out->name), &f)) != PBW_OK) {
|
|
335
|
+
return status;
|
|
336
|
+
}
|
|
337
|
+
break;
|
|
338
|
+
case 2: /* extendee, ".pkg.Type" (extensions only) */
|
|
339
|
+
if ((status = ds_str(out->extendee, sizeof(out->extendee), &f)) !=
|
|
340
|
+
PBW_OK) {
|
|
341
|
+
return status;
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
case 3: /* number */
|
|
345
|
+
if (f.wire != PBW_WIRE_VARINT || f.varint > UINT32_MAX) {
|
|
346
|
+
/* A 33-bit number would truncate into a valid one. */
|
|
347
|
+
return PBW_ERR_INVALID;
|
|
348
|
+
}
|
|
349
|
+
out->number = (uint32_t)f.varint;
|
|
350
|
+
out->has_number = 1;
|
|
351
|
+
break;
|
|
352
|
+
case 4: /* label */
|
|
353
|
+
if (f.wire != PBW_WIRE_VARINT || f.varint > UINT32_MAX) {
|
|
354
|
+
return PBW_ERR_INVALID;
|
|
355
|
+
}
|
|
356
|
+
out->label = (uint32_t)f.varint;
|
|
357
|
+
break;
|
|
358
|
+
case 5: /* type */
|
|
359
|
+
if (f.wire != PBW_WIRE_VARINT || f.varint > UINT32_MAX) {
|
|
360
|
+
return PBW_ERR_INVALID;
|
|
361
|
+
}
|
|
362
|
+
out->type = (uint32_t)f.varint;
|
|
363
|
+
out->has_type = 1;
|
|
364
|
+
break;
|
|
365
|
+
case 6: /* type_name, ".pkg.Type" */
|
|
366
|
+
if ((status = ds_str(out->type_name, sizeof(out->type_name), &f)) !=
|
|
367
|
+
PBW_OK) {
|
|
368
|
+
return status;
|
|
369
|
+
}
|
|
370
|
+
break;
|
|
371
|
+
case 7: /* default_value, the raw .proto literal */
|
|
372
|
+
if ((status = ds_str(out->default_value,
|
|
373
|
+
sizeof(out->default_value), &f)) != PBW_OK) {
|
|
374
|
+
return status;
|
|
375
|
+
}
|
|
376
|
+
out->has_default = 1;
|
|
377
|
+
break;
|
|
378
|
+
case 8: /* options */
|
|
379
|
+
if (f.wire != PBW_WIRE_LEN) {
|
|
380
|
+
break; /* wrong wire type: ignore, like the sibling cases */
|
|
381
|
+
}
|
|
382
|
+
if ((status = ds_field_options(f.data, f.data + f.len,
|
|
383
|
+
&out->packed, &out->feat)) !=
|
|
384
|
+
PBW_OK) {
|
|
385
|
+
return status;
|
|
386
|
+
}
|
|
387
|
+
break;
|
|
388
|
+
case 17: /* proto3_optional: explicit presence, no synthetic oneof */
|
|
389
|
+
if (f.wire != PBW_WIRE_VARINT) {
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
out->proto3_optional = f.varint != 0;
|
|
393
|
+
break;
|
|
394
|
+
case 9: /* oneof_index is 0-based on the wire, 1-based here */
|
|
395
|
+
if (f.wire != PBW_WIRE_VARINT || f.varint >= UINT32_MAX) {
|
|
396
|
+
return PBW_ERR_INVALID;
|
|
397
|
+
}
|
|
398
|
+
out->oneof = (uint32_t)f.varint + 1u;
|
|
399
|
+
out->has_oneof = 1;
|
|
400
|
+
break;
|
|
401
|
+
default:
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return PBW_OK;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/* Stores a parsed default literal on the field; OOM is the only failure. */
|
|
409
|
+
static pbw_status
|
|
410
|
+
ds_set_default(pbw_schema *schema, pbw_field *field, const char *def)
|
|
411
|
+
{
|
|
412
|
+
size_t len;
|
|
413
|
+
char *copy;
|
|
414
|
+
|
|
415
|
+
if (def == NULL) {
|
|
416
|
+
return PBW_OK;
|
|
417
|
+
}
|
|
418
|
+
len = strlen(def) + 1;
|
|
419
|
+
copy = (char *)malloc(len);
|
|
420
|
+
if (copy == NULL) {
|
|
421
|
+
ds_error(schema, "out of memory");
|
|
422
|
+
return PBW_ERR_NOMEM;
|
|
423
|
+
}
|
|
424
|
+
memcpy(copy, def, len);
|
|
425
|
+
field->default_value = copy;
|
|
426
|
+
return PBW_OK;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
static pbw_status
|
|
430
|
+
ds_add_field(pbw_schema *schema, pbw_descriptor *msg,
|
|
431
|
+
const ds_field_info *info, int is_extension,
|
|
432
|
+
const ds_features *file)
|
|
433
|
+
{
|
|
434
|
+
pbw_field *field;
|
|
435
|
+
pbw_status status;
|
|
436
|
+
int repack = file->repack;
|
|
437
|
+
|
|
438
|
+
if (info->name[0] == '\0' || !info->has_number || !info->has_type ||
|
|
439
|
+
info->number < 1 || info->number > PBW_MAX_FIELD_NUMBER ||
|
|
440
|
+
info->label < PBW_LABEL_OPTIONAL || info->label > PBW_LABEL_REPEATED ||
|
|
441
|
+
info->type < PBW_TYPE_DOUBLE || info->type > PBW_TYPE_SINT64) {
|
|
442
|
+
ds_error(schema, "invalid field %s.%s", pbw_descriptor_name(msg),
|
|
443
|
+
info->name);
|
|
444
|
+
return PBW_ERR_INVALID;
|
|
445
|
+
}
|
|
446
|
+
if (is_extension && info->has_oneof) {
|
|
447
|
+
ds_error(schema, "extension %s.%s cannot be in a oneof",
|
|
448
|
+
pbw_descriptor_name(msg), info->name);
|
|
449
|
+
return PBW_ERR_INVALID;
|
|
450
|
+
}
|
|
451
|
+
if (info->has_oneof && info->oneof == 0) {
|
|
452
|
+
/* Wire oneof_index is 0-based and stored 1-based, so 0 only comes
|
|
453
|
+
* from a 2^32-1 wrap. */
|
|
454
|
+
ds_error(schema, "invalid oneof index for %s.%s",
|
|
455
|
+
pbw_descriptor_name(msg), info->name);
|
|
456
|
+
return PBW_ERR_INVALID;
|
|
457
|
+
}
|
|
458
|
+
/* Packing default: explicit `packed` wins, then the field FeatureSet,
|
|
459
|
+
* then the file/message default (PACKED for proto3/editions files,
|
|
460
|
+
* EXPANDED for proto2). Unchanged when no editions features exist. */
|
|
461
|
+
if (info->feat.repack != 0) {
|
|
462
|
+
repack = info->feat.repack == 1;
|
|
463
|
+
}
|
|
464
|
+
if (info->packed >= 0) {
|
|
465
|
+
repack = info->packed;
|
|
466
|
+
}
|
|
467
|
+
if (is_extension) {
|
|
468
|
+
field = pbw_message_add_extension(msg, info->name, info->number,
|
|
469
|
+
(pbw_type)info->type,
|
|
470
|
+
(pbw_label)info->label);
|
|
471
|
+
} else {
|
|
472
|
+
field = pbw_message_add_field(msg, info->name, info->number,
|
|
473
|
+
(pbw_type)info->type,
|
|
474
|
+
(pbw_label)info->label);
|
|
475
|
+
}
|
|
476
|
+
if (field == NULL) {
|
|
477
|
+
/* add rejects duplicates and out-of-range numbers */
|
|
478
|
+
ds_error(schema, "duplicate or invalid field %s.%s (%u)",
|
|
479
|
+
pbw_descriptor_name(msg), info->name, info->number);
|
|
480
|
+
return PBW_ERR_INVALID;
|
|
481
|
+
}
|
|
482
|
+
if (info->type == PBW_TYPE_MESSAGE || info->type == PBW_TYPE_ENUM ||
|
|
483
|
+
info->type == PBW_TYPE_GROUP) {
|
|
484
|
+
pbw_field_set_type_name(field, info->type_name[0] == '.'
|
|
485
|
+
? info->type_name + 1
|
|
486
|
+
: info->type_name);
|
|
487
|
+
}
|
|
488
|
+
if (info->has_default) {
|
|
489
|
+
if ((status = ds_set_default(schema, field, info->default_value)) !=
|
|
490
|
+
PBW_OK) {
|
|
491
|
+
return status;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (repack && info->label == PBW_LABEL_REPEATED &&
|
|
495
|
+
pbw_is_packable((pbw_type)info->type)) {
|
|
496
|
+
pbw_field_set_packed(field, 1);
|
|
497
|
+
}
|
|
498
|
+
/* Editions resolution: field FeatureSet wins, file/message default
|
|
499
|
+
* fills the rest. Non-editions files resolve to zeros (dialect-wide
|
|
500
|
+
* behavior, as before). */
|
|
501
|
+
field->presence =
|
|
502
|
+
(uint8_t)(info->feat.presence ? info->feat.presence : file->presence);
|
|
503
|
+
if (info->proto3_optional) {
|
|
504
|
+
/* proto3 `optional` spells explicit presence without an editions
|
|
505
|
+
* FeatureSet; it is what keeps a set default value in the JSON/text
|
|
506
|
+
* output. */
|
|
507
|
+
field->presence = 1;
|
|
508
|
+
}
|
|
509
|
+
field->enum_open =
|
|
510
|
+
(uint8_t)info->feat.enum_open; /* closed-ness lives on the enum type */
|
|
511
|
+
field->msg_enc =
|
|
512
|
+
(uint8_t)(info->feat.msg_enc ? info->feat.msg_enc : file->msg_enc);
|
|
513
|
+
/* utf8_validation, the reference's rule: field FeatureSet, then the file's,
|
|
514
|
+
* then the dialect default (VERIFY for proto3/editions, NONE for proto2).
|
|
515
|
+
* Only `string` fields carry it; `bytes` never validates. */
|
|
516
|
+
{
|
|
517
|
+
int utf8 = info->feat.utf8 ? info->feat.utf8
|
|
518
|
+
: (file->utf8 ? file->utf8
|
|
519
|
+
: (msg->proto3 ? 2 : 3));
|
|
520
|
+
|
|
521
|
+
field->utf8_check =
|
|
522
|
+
(uint8_t)(utf8 == 2 && info->type == PBW_TYPE_STRING);
|
|
523
|
+
}
|
|
524
|
+
if (info->has_oneof) {
|
|
525
|
+
pbw_field_set_oneof(field, info->oneof);
|
|
526
|
+
}
|
|
527
|
+
return PBW_OK;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/* Queues an extension for attachment after every file is read. */
|
|
531
|
+
static pbw_status
|
|
532
|
+
ds_queue_extension(pbw_schema *schema, ds_pending_ext **exts, size_t *count,
|
|
533
|
+
size_t *cap, const ds_field_info *info, const char *scope,
|
|
534
|
+
const ds_features *file)
|
|
535
|
+
{
|
|
536
|
+
ds_pending_ext *grown;
|
|
537
|
+
|
|
538
|
+
if (*count == *cap) {
|
|
539
|
+
size_t ncap = *cap ? *cap * 2 : 8;
|
|
540
|
+
/* Extension counts are bounded by the input length, which reaches
|
|
541
|
+
* the 32-bit wrap with tens of MB of hostile input. */
|
|
542
|
+
if (ncap < *cap || ncap > SIZE_MAX / sizeof(*grown)) {
|
|
543
|
+
ds_error(schema, "out of memory");
|
|
544
|
+
return PBW_ERR_NOMEM;
|
|
545
|
+
}
|
|
546
|
+
grown = (ds_pending_ext *)realloc(*exts, ncap * sizeof(*grown));
|
|
547
|
+
if (grown == NULL) {
|
|
548
|
+
ds_error(schema, "out of memory");
|
|
549
|
+
return PBW_ERR_NOMEM;
|
|
550
|
+
}
|
|
551
|
+
*exts = grown;
|
|
552
|
+
*cap = ncap;
|
|
553
|
+
}
|
|
554
|
+
if (info->extendee[0] == '\0') {
|
|
555
|
+
ds_error(schema, "extension %s has no extendee", info->name);
|
|
556
|
+
return PBW_ERR_INVALID;
|
|
557
|
+
}
|
|
558
|
+
(*exts)[*count].info = *info;
|
|
559
|
+
(*exts)[*count].file = *file;
|
|
560
|
+
{
|
|
561
|
+
const char *src =
|
|
562
|
+
info->extendee[0] == '.' ? info->extendee + 1 : info->extendee;
|
|
563
|
+
size_t len = strlen(src);
|
|
564
|
+
if (len >= sizeof((*exts)[*count].extendee)) {
|
|
565
|
+
ds_error(schema, "extendee name too long: %.64s...", src);
|
|
566
|
+
return PBW_ERR_LIMIT;
|
|
567
|
+
}
|
|
568
|
+
memcpy((*exts)[*count].extendee, src, len + 1);
|
|
569
|
+
}
|
|
570
|
+
{
|
|
571
|
+
size_t len = strlen(scope);
|
|
572
|
+
if (len >= sizeof((*exts)[*count].scope)) {
|
|
573
|
+
ds_error(schema, "scope name too long: %.64s...", scope);
|
|
574
|
+
return PBW_ERR_LIMIT;
|
|
575
|
+
}
|
|
576
|
+
memcpy((*exts)[*count].scope, scope, len + 1);
|
|
577
|
+
}
|
|
578
|
+
(*count)++;
|
|
579
|
+
return PBW_OK;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/* Attaches every queued extension to the message it extends. The dialect
|
|
583
|
+
* follows the extendee: an extension of a proto2 message is proto2 itself. */
|
|
584
|
+
static pbw_status
|
|
585
|
+
ds_attach_extensions(pbw_schema *schema, ds_pending_ext *exts, size_t count)
|
|
586
|
+
{
|
|
587
|
+
size_t i;
|
|
588
|
+
|
|
589
|
+
for (i = 0; i < count; i++) {
|
|
590
|
+
pbw_descriptor *target = pbw_schema_find(schema, exts[i].extendee);
|
|
591
|
+
pbw_status status;
|
|
592
|
+
char full[DS_MAX_NAME * 2];
|
|
593
|
+
const pbw_field *field;
|
|
594
|
+
char *copy;
|
|
595
|
+
int written;
|
|
596
|
+
|
|
597
|
+
if (target == NULL || target->is_enum) {
|
|
598
|
+
ds_error(schema, "unknown extendee %s for extension %s",
|
|
599
|
+
exts[i].extendee, exts[i].info.name);
|
|
600
|
+
return PBW_ERR_NOT_FOUND;
|
|
601
|
+
}
|
|
602
|
+
status = ds_add_field(schema, target, &exts[i].info, 1,
|
|
603
|
+
&exts[i].file);
|
|
604
|
+
if (status != PBW_OK) {
|
|
605
|
+
return status;
|
|
606
|
+
}
|
|
607
|
+
/* The full name is scope + short name, like the reference. */
|
|
608
|
+
written = exts[i].scope[0] == '\0'
|
|
609
|
+
? snprintf(full, sizeof(full), "%s", exts[i].info.name)
|
|
610
|
+
: snprintf(full, sizeof(full), "%s.%s", exts[i].scope,
|
|
611
|
+
exts[i].info.name);
|
|
612
|
+
if (written < 0 || (size_t)written >= sizeof(full)) {
|
|
613
|
+
ds_error(schema, "extension name too long: %.64s...",
|
|
614
|
+
exts[i].info.name);
|
|
615
|
+
return PBW_ERR_LIMIT;
|
|
616
|
+
}
|
|
617
|
+
copy = (char *)malloc((size_t)written + 1);
|
|
618
|
+
if (copy == NULL) {
|
|
619
|
+
ds_error(schema, "out of memory");
|
|
620
|
+
return PBW_ERR_NOMEM;
|
|
621
|
+
}
|
|
622
|
+
memcpy(copy, full, (size_t)written + 1);
|
|
623
|
+
/* the schema is not linked yet, so scan the unsorted fields */
|
|
624
|
+
field = NULL;
|
|
625
|
+
{
|
|
626
|
+
size_t k;
|
|
627
|
+
for (k = 0; k < target->field_count; k++) {
|
|
628
|
+
if (target->fields[k].number == exts[i].info.number) {
|
|
629
|
+
field = &target->fields[k];
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (field == NULL) {
|
|
635
|
+
free(copy);
|
|
636
|
+
ds_error(schema, "extension %s vanished", exts[i].info.name);
|
|
637
|
+
return PBW_ERR_INVALID;
|
|
638
|
+
}
|
|
639
|
+
((pbw_field *)field)->full_name = copy;
|
|
640
|
+
}
|
|
641
|
+
return PBW_OK;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
static pbw_status
|
|
645
|
+
ds_enum_proto(pbw_schema *schema, const char *parent, const uint8_t *p,
|
|
646
|
+
const uint8_t *end, int file_closed, int file_proto3)
|
|
647
|
+
{
|
|
648
|
+
char fqname[DS_MAX_NAME];
|
|
649
|
+
char value_name[DS_MAX_NAME] = "";
|
|
650
|
+
pbw_descriptor *desc;
|
|
651
|
+
pbw_status status;
|
|
652
|
+
/* Editions enum_type: explicit EnumOptions.features wins, the file
|
|
653
|
+
* default fills the rest. Options may follow the values on the wire,
|
|
654
|
+
* so resolve first: openness only matters at link time. */
|
|
655
|
+
int closed = file_closed;
|
|
656
|
+
ds_field f;
|
|
657
|
+
|
|
658
|
+
if ((status = ds_fqname(schema, fqname, sizeof(fqname), parent, p, end)) !=
|
|
659
|
+
PBW_OK) {
|
|
660
|
+
return status;
|
|
661
|
+
}
|
|
662
|
+
{
|
|
663
|
+
const uint8_t *o = p;
|
|
664
|
+
ds_field of;
|
|
665
|
+
|
|
666
|
+
for (;;) {
|
|
667
|
+
int orc = ds_next(&o, end, &of);
|
|
668
|
+
|
|
669
|
+
if (orc < 0) {
|
|
670
|
+
return PBW_ERR_INVALID;
|
|
671
|
+
}
|
|
672
|
+
if (orc == 0) {
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
if (of.field != 3 || of.wire != PBW_WIRE_LEN) { /* options */
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
{
|
|
679
|
+
const uint8_t *q = of.data;
|
|
680
|
+
const uint8_t *qend = of.data + of.len;
|
|
681
|
+
ds_field qf;
|
|
682
|
+
|
|
683
|
+
for (;;) {
|
|
684
|
+
int qrc = ds_next(&q, qend, &qf);
|
|
685
|
+
ds_features feat;
|
|
686
|
+
|
|
687
|
+
if (qrc < 0) {
|
|
688
|
+
return PBW_ERR_INVALID;
|
|
689
|
+
}
|
|
690
|
+
if (qrc == 0) {
|
|
691
|
+
break;
|
|
692
|
+
}
|
|
693
|
+
if (qf.field != 7 || qf.wire != PBW_WIRE_LEN) {
|
|
694
|
+
continue;
|
|
695
|
+
}
|
|
696
|
+
memset(&feat, 0, sizeof(feat));
|
|
697
|
+
status = ds_parse_featureset(qf.data, qf.data + qf.len,
|
|
698
|
+
&feat);
|
|
699
|
+
if (status != PBW_OK) {
|
|
700
|
+
return status;
|
|
701
|
+
}
|
|
702
|
+
if (feat.enum_open != 0) {
|
|
703
|
+
closed = feat.enum_open == 2;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
desc = pbw_schema_add_enum(schema, fqname);
|
|
710
|
+
if (desc == NULL) {
|
|
711
|
+
ds_error(schema, "duplicate or invalid enum %s", fqname);
|
|
712
|
+
return PBW_ERR_INVALID;
|
|
713
|
+
}
|
|
714
|
+
/* Openness rides the dialect flag: a closed enum rejects unknown
|
|
715
|
+
* numbers exactly like a proto2 one. */
|
|
716
|
+
desc->proto3 = closed ? 0 : file_proto3;
|
|
717
|
+
for (;;) {
|
|
718
|
+
const uint8_t *vp;
|
|
719
|
+
const uint8_t *vend;
|
|
720
|
+
int32_t number = 0;
|
|
721
|
+
int rc = ds_next(&p, end, &f);
|
|
722
|
+
|
|
723
|
+
if (rc < 0) {
|
|
724
|
+
return PBW_ERR_INVALID;
|
|
725
|
+
}
|
|
726
|
+
if (rc == 0) {
|
|
727
|
+
return PBW_OK;
|
|
728
|
+
}
|
|
729
|
+
if (f.field != 2 || f.wire != PBW_WIRE_LEN) { /* value */
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
vp = f.data;
|
|
733
|
+
vend = f.data + f.len;
|
|
734
|
+
value_name[0] = '\0';
|
|
735
|
+
for (;;) {
|
|
736
|
+
int vrc = ds_next(&vp, vend, &f);
|
|
737
|
+
if (vrc < 0) {
|
|
738
|
+
return PBW_ERR_INVALID;
|
|
739
|
+
}
|
|
740
|
+
if (vrc == 0) {
|
|
741
|
+
break;
|
|
742
|
+
}
|
|
743
|
+
if (f.field == 1) {
|
|
744
|
+
if ((status = ds_str(value_name, sizeof(value_name), &f)) !=
|
|
745
|
+
PBW_OK) {
|
|
746
|
+
return status;
|
|
747
|
+
}
|
|
748
|
+
} else if (f.field == 2 && f.wire == PBW_WIRE_VARINT) {
|
|
749
|
+
/* int32 travels as up-to-64-bit varint (negatives use all
|
|
750
|
+
* ten bytes); anything outside the 32-bit range would
|
|
751
|
+
* truncate into a different value. */
|
|
752
|
+
if (f.varint > 0x7fffffffu &&
|
|
753
|
+
f.varint < 0xffffffff80000000ull) {
|
|
754
|
+
return PBW_ERR_INVALID;
|
|
755
|
+
}
|
|
756
|
+
number = (int32_t)(uint32_t)f.varint;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if (value_name[0] == '\0') {
|
|
760
|
+
ds_error(schema, "enum value without a name in %s", fqname);
|
|
761
|
+
return PBW_ERR_INVALID;
|
|
762
|
+
}
|
|
763
|
+
status = pbw_enum_add_value(desc, value_name, number);
|
|
764
|
+
if (status != PBW_OK) {
|
|
765
|
+
ds_error(schema, "out of memory");
|
|
766
|
+
return status;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
static pbw_status
|
|
772
|
+
ds_message_proto(pbw_schema *schema, const ds_features *file,
|
|
773
|
+
const char *parent, const uint8_t *p, const uint8_t *end,
|
|
774
|
+
uint32_t depth, uint32_t dlim, ds_pending_ext **exts,
|
|
775
|
+
size_t *ext_count, size_t *ext_cap)
|
|
776
|
+
{
|
|
777
|
+
char fqname[DS_MAX_NAME];
|
|
778
|
+
pbw_descriptor *msg;
|
|
779
|
+
pbw_status status;
|
|
780
|
+
/* Message-level FeatureSet overrides the file default for the fields
|
|
781
|
+
* declared here; nested messages inherit it in turn. */
|
|
782
|
+
ds_features feat = *file;
|
|
783
|
+
ds_field f;
|
|
784
|
+
uint32_t oneof_decls = 0;
|
|
785
|
+
size_t i;
|
|
786
|
+
|
|
787
|
+
if (depth >= dlim) {
|
|
788
|
+
return PBW_ERR_LIMIT;
|
|
789
|
+
}
|
|
790
|
+
if ((status = ds_fqname(schema, fqname, sizeof(fqname), parent, p, end)) !=
|
|
791
|
+
PBW_OK) {
|
|
792
|
+
return status;
|
|
793
|
+
}
|
|
794
|
+
msg = pbw_schema_add_message(schema, fqname);
|
|
795
|
+
if (msg == NULL) {
|
|
796
|
+
ds_error(schema, "duplicate or invalid message %s", fqname);
|
|
797
|
+
return PBW_ERR_INVALID;
|
|
798
|
+
}
|
|
799
|
+
/* Pre-scan for options: fields may precede them on the wire, but the
|
|
800
|
+
* message-level FeatureSet must apply to every field declared here. */
|
|
801
|
+
{
|
|
802
|
+
const uint8_t *o = p;
|
|
803
|
+
ds_field of;
|
|
804
|
+
|
|
805
|
+
for (;;) {
|
|
806
|
+
int orc = ds_next(&o, end, &of);
|
|
807
|
+
|
|
808
|
+
if (orc < 0) {
|
|
809
|
+
return PBW_ERR_INVALID;
|
|
810
|
+
}
|
|
811
|
+
if (orc == 0) {
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
if (of.field != 7 || of.wire != PBW_WIRE_LEN) {
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
{
|
|
818
|
+
const uint8_t *q = of.data;
|
|
819
|
+
const uint8_t *qend = of.data + of.len;
|
|
820
|
+
ds_field qf;
|
|
821
|
+
|
|
822
|
+
for (;;) {
|
|
823
|
+
int qrc = ds_next(&q, qend, &qf);
|
|
824
|
+
|
|
825
|
+
if (qrc < 0) {
|
|
826
|
+
return PBW_ERR_INVALID;
|
|
827
|
+
}
|
|
828
|
+
if (qrc == 0) {
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
if (qf.field == 12 && qf.wire == PBW_WIRE_LEN) {
|
|
832
|
+
status = ds_parse_featureset(qf.data,
|
|
833
|
+
qf.data + qf.len,
|
|
834
|
+
&feat);
|
|
835
|
+
if (status != PBW_OK) {
|
|
836
|
+
return status;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
for (;;) {
|
|
844
|
+
int rc = ds_next(&p, end, &f);
|
|
845
|
+
|
|
846
|
+
if (rc < 0) {
|
|
847
|
+
return PBW_ERR_INVALID;
|
|
848
|
+
}
|
|
849
|
+
if (rc == 0) {
|
|
850
|
+
break; /* the loop's post-checks run after it */
|
|
851
|
+
}
|
|
852
|
+
if (f.wire != PBW_WIRE_LEN) {
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
switch (f.field) {
|
|
856
|
+
case 2: /* field */
|
|
857
|
+
case 6: /* extension */
|
|
858
|
+
{
|
|
859
|
+
/* Heap, not stack: the struct carries a 4 KiB default buffer
|
|
860
|
+
* and this function recurses per nesting level, so a stack
|
|
861
|
+
* slot would multiply a hostile descriptor's depth into a
|
|
862
|
+
* stack overflow. Parsing is load-time only, one malloc per
|
|
863
|
+
* field is immaterial. */
|
|
864
|
+
ds_field_info *info = (ds_field_info *)malloc(sizeof(*info));
|
|
865
|
+
|
|
866
|
+
if (info == NULL) {
|
|
867
|
+
ds_error(schema, "out of memory");
|
|
868
|
+
return PBW_ERR_NOMEM;
|
|
869
|
+
}
|
|
870
|
+
status = ds_parse_field(info, f.data, f.data + f.len);
|
|
871
|
+
if (status == PBW_OK) {
|
|
872
|
+
if (f.field == 6 || info->extendee[0] != '\0') {
|
|
873
|
+
status = ds_queue_extension(schema, exts, ext_count,
|
|
874
|
+
ext_cap, info, fqname,
|
|
875
|
+
&feat);
|
|
876
|
+
} else {
|
|
877
|
+
status =
|
|
878
|
+
ds_add_field(schema, msg, info, 0, &feat);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
free(info);
|
|
882
|
+
if (status != PBW_OK) {
|
|
883
|
+
return status;
|
|
884
|
+
}
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
case 10: /* reserved_name */
|
|
888
|
+
{
|
|
889
|
+
char name[DS_MAX_NAME];
|
|
890
|
+
char **grown;
|
|
891
|
+
|
|
892
|
+
status = ds_str(name, sizeof(name), &f);
|
|
893
|
+
if (status != PBW_OK) {
|
|
894
|
+
return status;
|
|
895
|
+
}
|
|
896
|
+
/* reserved names are skipped silently by the text parser */
|
|
897
|
+
if (msg->reserved_count == SIZE_MAX ||
|
|
898
|
+
(msg->reserved_count + 1) > SIZE_MAX / sizeof(*grown)) {
|
|
899
|
+
ds_error(schema, "out of memory");
|
|
900
|
+
return PBW_ERR_NOMEM;
|
|
901
|
+
}
|
|
902
|
+
grown = (char **)realloc(msg->reserved, (msg->reserved_count + 1) *
|
|
903
|
+
sizeof(*grown));
|
|
904
|
+
if (grown == NULL) {
|
|
905
|
+
ds_error(schema, "out of memory");
|
|
906
|
+
return PBW_ERR_NOMEM;
|
|
907
|
+
}
|
|
908
|
+
msg->reserved = grown;
|
|
909
|
+
grown[msg->reserved_count] = NULL;
|
|
910
|
+
{
|
|
911
|
+
size_t len = strlen(name);
|
|
912
|
+
char *copy;
|
|
913
|
+
if (len == SIZE_MAX) {
|
|
914
|
+
ds_error(schema, "out of memory");
|
|
915
|
+
return PBW_ERR_NOMEM;
|
|
916
|
+
}
|
|
917
|
+
copy = (char *)malloc(len + 1);
|
|
918
|
+
if (copy == NULL) {
|
|
919
|
+
ds_error(schema, "out of memory");
|
|
920
|
+
return PBW_ERR_NOMEM;
|
|
921
|
+
}
|
|
922
|
+
memcpy(copy, name, len);
|
|
923
|
+
copy[len] = '\0';
|
|
924
|
+
grown[msg->reserved_count] = copy;
|
|
925
|
+
}
|
|
926
|
+
msg->reserved_count++;
|
|
927
|
+
status = PBW_OK;
|
|
928
|
+
break;
|
|
929
|
+
}
|
|
930
|
+
case 7: /* options: map_entry plus the message-level FeatureSet */
|
|
931
|
+
{
|
|
932
|
+
const uint8_t *o = f.data;
|
|
933
|
+
const uint8_t *oend = f.data + f.len;
|
|
934
|
+
ds_field of;
|
|
935
|
+
|
|
936
|
+
for (;;) {
|
|
937
|
+
int orc = ds_next(&o, oend, &of);
|
|
938
|
+
|
|
939
|
+
if (orc < 0) {
|
|
940
|
+
return PBW_ERR_INVALID;
|
|
941
|
+
}
|
|
942
|
+
if (orc == 0) {
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
if (of.field == 7 && of.wire == PBW_WIRE_VARINT &&
|
|
946
|
+
of.varint != 0) {
|
|
947
|
+
msg->map_entry = 1;
|
|
948
|
+
} else if (of.field == 12 && of.wire == PBW_WIRE_LEN) {
|
|
949
|
+
status = ds_parse_featureset(of.data, of.data + of.len,
|
|
950
|
+
&feat);
|
|
951
|
+
if (status != PBW_OK) {
|
|
952
|
+
return status;
|
|
953
|
+
}
|
|
954
|
+
} else if (of.field == 1 && of.wire == PBW_WIRE_VARINT &&
|
|
955
|
+
of.varint != 0) {
|
|
956
|
+
/* MessageOptions.message_set_wire_format */
|
|
957
|
+
msg->is_messageset = 1;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
status = PBW_OK;
|
|
961
|
+
break;
|
|
962
|
+
}
|
|
963
|
+
case 8: /* oneof_decl: only the count matters, oneof_index is 1-based */
|
|
964
|
+
oneof_decls++;
|
|
965
|
+
status = PBW_OK;
|
|
966
|
+
break;
|
|
967
|
+
case 3: /* nested_type: inherits the enclosing message's features */
|
|
968
|
+
status = ds_message_proto(schema, &feat, fqname, f.data,
|
|
969
|
+
f.data + f.len, depth + 1, dlim, exts,
|
|
970
|
+
ext_count, ext_cap);
|
|
971
|
+
break;
|
|
972
|
+
case 4: /* enum_type */
|
|
973
|
+
status = ds_enum_proto(schema, fqname, f.data, f.data + f.len,
|
|
974
|
+
feat.enum_open == 2, schema->proto3);
|
|
975
|
+
break;
|
|
976
|
+
default:
|
|
977
|
+
status = PBW_OK;
|
|
978
|
+
break;
|
|
979
|
+
}
|
|
980
|
+
if (status != PBW_OK) {
|
|
981
|
+
return status;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
/* oneof_index is only bounded by the field count in link_message; the
|
|
985
|
+
* real bound is the number of oneof_decl entries this message declared.
|
|
986
|
+
* A field pointing at an undeclared oneof silently drops the other
|
|
987
|
+
* fields sharing it, so reject the shape protoc would never emit. */
|
|
988
|
+
for (i = 0; i < msg->field_count; i++) {
|
|
989
|
+
const pbw_field *field = &msg->fields[i];
|
|
990
|
+
|
|
991
|
+
if (field->oneof_index > oneof_decls) {
|
|
992
|
+
ds_error(schema, "oneof index %u out of range in %s.%s",
|
|
993
|
+
field->oneof_index, pbw_descriptor_name(msg), field->name);
|
|
994
|
+
return PBW_ERR_INVALID;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
/* A map entry protoc synthesizes has exactly key #1 and value #2, and the
|
|
998
|
+
* JSON/text map printers read those two fields by number. Reject any
|
|
999
|
+
* other shape here instead of failing the whole print much later. */
|
|
1000
|
+
if (msg->map_entry) {
|
|
1001
|
+
const pbw_field *key = NULL;
|
|
1002
|
+
const pbw_field *value = NULL;
|
|
1003
|
+
|
|
1004
|
+
for (i = 0; i < msg->field_count; i++) {
|
|
1005
|
+
if (msg->fields[i].number == 1) {
|
|
1006
|
+
key = &msg->fields[i];
|
|
1007
|
+
} else if (msg->fields[i].number == 2) {
|
|
1008
|
+
value = &msg->fields[i];
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
if (msg->field_count != 2 || key == NULL || value == NULL ||
|
|
1012
|
+
key->label != PBW_LABEL_OPTIONAL ||
|
|
1013
|
+
value->label != PBW_LABEL_OPTIONAL ||
|
|
1014
|
+
key->type == PBW_TYPE_MESSAGE || key->type == PBW_TYPE_GROUP ||
|
|
1015
|
+
key->type == PBW_TYPE_ENUM || key->type == PBW_TYPE_BYTES ||
|
|
1016
|
+
key->type == PBW_TYPE_FLOAT || key->type == PBW_TYPE_DOUBLE) {
|
|
1017
|
+
ds_error(schema, "invalid map entry %s",
|
|
1018
|
+
pbw_descriptor_name(msg));
|
|
1019
|
+
return PBW_ERR_INVALID;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return PBW_OK;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
/* One parsed MethodDescriptorProto: names only, no transport. */
|
|
1026
|
+
typedef struct {
|
|
1027
|
+
char name[DS_MAX_NAME];
|
|
1028
|
+
char input[DS_MAX_NAME];
|
|
1029
|
+
char output[DS_MAX_NAME];
|
|
1030
|
+
int client_streaming;
|
|
1031
|
+
int server_streaming;
|
|
1032
|
+
} ds_method_info;
|
|
1033
|
+
|
|
1034
|
+
static char *
|
|
1035
|
+
ds_dup(const char *s)
|
|
1036
|
+
{
|
|
1037
|
+
size_t len;
|
|
1038
|
+
char *copy;
|
|
1039
|
+
|
|
1040
|
+
if (s == NULL) {
|
|
1041
|
+
return NULL;
|
|
1042
|
+
}
|
|
1043
|
+
len = strlen(s) + 1;
|
|
1044
|
+
copy = (char *)malloc(len);
|
|
1045
|
+
if (copy == NULL) {
|
|
1046
|
+
return NULL;
|
|
1047
|
+
}
|
|
1048
|
+
memcpy(copy, s, len);
|
|
1049
|
+
return copy;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/* Without the leading dot: ".pkg.Msg" -> "pkg.Msg", like field types. */
|
|
1053
|
+
static const char *
|
|
1054
|
+
ds_unroot(const char *s)
|
|
1055
|
+
{
|
|
1056
|
+
return s[0] == '.' ? s + 1 : s;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
static pbw_status
|
|
1060
|
+
ds_method_proto(ds_method_info *out, const uint8_t *p, const uint8_t *end)
|
|
1061
|
+
{
|
|
1062
|
+
ds_field f;
|
|
1063
|
+
|
|
1064
|
+
memset(out, 0, sizeof(*out));
|
|
1065
|
+
for (;;) {
|
|
1066
|
+
int rc = ds_next(&p, end, &f);
|
|
1067
|
+
pbw_status status;
|
|
1068
|
+
|
|
1069
|
+
if (rc < 0) {
|
|
1070
|
+
return PBW_ERR_INVALID;
|
|
1071
|
+
}
|
|
1072
|
+
if (rc == 0) {
|
|
1073
|
+
break;
|
|
1074
|
+
}
|
|
1075
|
+
switch (f.field) {
|
|
1076
|
+
case 1: /* name */
|
|
1077
|
+
if ((status = ds_str(out->name, sizeof(out->name), &f)) !=
|
|
1078
|
+
PBW_OK) {
|
|
1079
|
+
return status;
|
|
1080
|
+
}
|
|
1081
|
+
break;
|
|
1082
|
+
case 2: /* input_type */
|
|
1083
|
+
case 3: /* output_type */
|
|
1084
|
+
{
|
|
1085
|
+
char tmp[DS_MAX_NAME];
|
|
1086
|
+
const char *bare;
|
|
1087
|
+
size_t len;
|
|
1088
|
+
|
|
1089
|
+
if ((status = ds_str(tmp, sizeof(tmp), &f)) != PBW_OK) {
|
|
1090
|
+
return status;
|
|
1091
|
+
}
|
|
1092
|
+
bare = ds_unroot(tmp);
|
|
1093
|
+
len = strlen(bare) + 1;
|
|
1094
|
+
if (len > sizeof(out->input)) {
|
|
1095
|
+
return PBW_ERR_LIMIT;
|
|
1096
|
+
}
|
|
1097
|
+
memcpy(f.field == 2 ? out->input : out->output, bare, len);
|
|
1098
|
+
break;
|
|
1099
|
+
}
|
|
1100
|
+
case 5: /* client_streaming */
|
|
1101
|
+
if (f.wire != PBW_WIRE_VARINT) {
|
|
1102
|
+
return PBW_ERR_INVALID;
|
|
1103
|
+
}
|
|
1104
|
+
out->client_streaming = f.varint != 0;
|
|
1105
|
+
break;
|
|
1106
|
+
case 6: /* server_streaming */
|
|
1107
|
+
if (f.wire != PBW_WIRE_VARINT) {
|
|
1108
|
+
return PBW_ERR_INVALID;
|
|
1109
|
+
}
|
|
1110
|
+
out->server_streaming = f.varint != 0;
|
|
1111
|
+
break;
|
|
1112
|
+
default:
|
|
1113
|
+
break;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
return PBW_OK;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/* Releases a service the loader built but cannot keep (load failure). */
|
|
1120
|
+
static void
|
|
1121
|
+
ds_free_service(pbw_service *svc)
|
|
1122
|
+
{
|
|
1123
|
+
size_t m;
|
|
1124
|
+
|
|
1125
|
+
if (svc == NULL) {
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
/* method_count only counts stored methods, so this frees exactly
|
|
1129
|
+
* what the parse loop committed. */
|
|
1130
|
+
for (m = 0; m < svc->method_count; m++) {
|
|
1131
|
+
free(svc->methods[m].name);
|
|
1132
|
+
free(svc->methods[m].input_type);
|
|
1133
|
+
free(svc->methods[m].output_type);
|
|
1134
|
+
}
|
|
1135
|
+
free(svc->methods);
|
|
1136
|
+
free(svc->name);
|
|
1137
|
+
free(svc);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/* One ServiceDescriptorProto: the service joins the schema, its methods join
|
|
1141
|
+
* the service in wire order. Unknown method entries fail the load: a
|
|
1142
|
+
* routing table that silently drops an rpc is worse than no table. */
|
|
1143
|
+
static pbw_status
|
|
1144
|
+
ds_service_proto(pbw_schema *schema, const char *package, const uint8_t *p,
|
|
1145
|
+
const uint8_t *end)
|
|
1146
|
+
{
|
|
1147
|
+
char fqname[DS_MAX_NAME];
|
|
1148
|
+
pbw_service *svc;
|
|
1149
|
+
pbw_status status;
|
|
1150
|
+
ds_field f;
|
|
1151
|
+
|
|
1152
|
+
if ((status = ds_fqname(schema, fqname, sizeof(fqname), package, p,
|
|
1153
|
+
end)) != PBW_OK) {
|
|
1154
|
+
return status;
|
|
1155
|
+
}
|
|
1156
|
+
svc = (pbw_service *)calloc(1, sizeof(*svc));
|
|
1157
|
+
if (svc == NULL) {
|
|
1158
|
+
ds_error(schema, "out of memory");
|
|
1159
|
+
return PBW_ERR_NOMEM;
|
|
1160
|
+
}
|
|
1161
|
+
svc->name = ds_dup(fqname);
|
|
1162
|
+
if (svc->name == NULL) {
|
|
1163
|
+
free(svc);
|
|
1164
|
+
ds_error(schema, "out of memory");
|
|
1165
|
+
return PBW_ERR_NOMEM;
|
|
1166
|
+
}
|
|
1167
|
+
svc->schema = schema;
|
|
1168
|
+
for (;;) {
|
|
1169
|
+
int rc = ds_next(&p, end, &f);
|
|
1170
|
+
|
|
1171
|
+
if (rc < 0) {
|
|
1172
|
+
status = PBW_ERR_INVALID;
|
|
1173
|
+
break;
|
|
1174
|
+
}
|
|
1175
|
+
if (rc == 0) {
|
|
1176
|
+
status = PBW_OK;
|
|
1177
|
+
break;
|
|
1178
|
+
}
|
|
1179
|
+
if (f.field != 2 || f.wire != PBW_WIRE_LEN) { /* method */
|
|
1180
|
+
continue;
|
|
1181
|
+
}
|
|
1182
|
+
{
|
|
1183
|
+
ds_method_info info;
|
|
1184
|
+
pbw_method *grown;
|
|
1185
|
+
pbw_method *m;
|
|
1186
|
+
|
|
1187
|
+
status =
|
|
1188
|
+
ds_method_proto(&info, f.data, f.data + f.len);
|
|
1189
|
+
if (status != PBW_OK) {
|
|
1190
|
+
break;
|
|
1191
|
+
}
|
|
1192
|
+
if (info.name[0] == '\0' || info.input[0] == '\0' ||
|
|
1193
|
+
info.output[0] == '\0') {
|
|
1194
|
+
status = PBW_ERR_INVALID;
|
|
1195
|
+
break;
|
|
1196
|
+
}
|
|
1197
|
+
if (svc->method_count == SIZE_MAX ||
|
|
1198
|
+
(svc->method_count + 1) > SIZE_MAX / sizeof(*grown)) {
|
|
1199
|
+
ds_error(schema, "out of memory");
|
|
1200
|
+
status = PBW_ERR_NOMEM;
|
|
1201
|
+
break;
|
|
1202
|
+
}
|
|
1203
|
+
grown = (pbw_method *)realloc(svc->methods,
|
|
1204
|
+
(svc->method_count + 1) *
|
|
1205
|
+
sizeof(*grown));
|
|
1206
|
+
if (grown == NULL) {
|
|
1207
|
+
ds_error(schema, "out of memory");
|
|
1208
|
+
status = PBW_ERR_NOMEM;
|
|
1209
|
+
break;
|
|
1210
|
+
}
|
|
1211
|
+
svc->methods = grown;
|
|
1212
|
+
m = &grown[svc->method_count];
|
|
1213
|
+
memset(m, 0, sizeof(*m));
|
|
1214
|
+
m->name = ds_dup(info.name);
|
|
1215
|
+
m->input_type = ds_dup(info.input);
|
|
1216
|
+
m->output_type = ds_dup(info.output);
|
|
1217
|
+
if (m->name == NULL || m->input_type == NULL ||
|
|
1218
|
+
m->output_type == NULL) {
|
|
1219
|
+
free(m->name);
|
|
1220
|
+
free(m->input_type);
|
|
1221
|
+
free(m->output_type);
|
|
1222
|
+
ds_error(schema, "out of memory");
|
|
1223
|
+
status = PBW_ERR_NOMEM;
|
|
1224
|
+
break;
|
|
1225
|
+
}
|
|
1226
|
+
m->client_streaming = info.client_streaming;
|
|
1227
|
+
m->server_streaming = info.server_streaming;
|
|
1228
|
+
svc->method_count++;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
if (status == PBW_OK) {
|
|
1232
|
+
pbw_service **grown;
|
|
1233
|
+
|
|
1234
|
+
if (schema->service_count == SIZE_MAX ||
|
|
1235
|
+
(schema->service_count + 1) > SIZE_MAX / sizeof(*grown)) {
|
|
1236
|
+
ds_error(schema, "out of memory");
|
|
1237
|
+
status = PBW_ERR_NOMEM;
|
|
1238
|
+
} else {
|
|
1239
|
+
grown = (pbw_service **)realloc(schema->services,
|
|
1240
|
+
(schema->service_count + 1) *
|
|
1241
|
+
sizeof(*grown));
|
|
1242
|
+
if (grown == NULL) {
|
|
1243
|
+
ds_error(schema, "out of memory");
|
|
1244
|
+
status = PBW_ERR_NOMEM;
|
|
1245
|
+
} else {
|
|
1246
|
+
schema->services = grown;
|
|
1247
|
+
grown[schema->service_count++] = svc;
|
|
1248
|
+
return PBW_OK;
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
ds_free_service(svc);
|
|
1253
|
+
return status;
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
static pbw_status
|
|
1257
|
+
ds_file_proto(pbw_schema *schema, const uint8_t *p, const uint8_t *end,
|
|
1258
|
+
uint32_t dlim, ds_pending_ext **exts, size_t *ext_count,
|
|
1259
|
+
size_t *ext_cap)
|
|
1260
|
+
{
|
|
1261
|
+
char package[DS_MAX_NAME] = "";
|
|
1262
|
+
char syntax[DS_MAX_NAME] = "";
|
|
1263
|
+
pbw_status status;
|
|
1264
|
+
ds_field f;
|
|
1265
|
+
ds_features feat;
|
|
1266
|
+
int proto3 = 0;
|
|
1267
|
+
int edition = 0;
|
|
1268
|
+
int has_edition = 0;
|
|
1269
|
+
const uint8_t *walk;
|
|
1270
|
+
|
|
1271
|
+
/* pass 1: syntax and package may sit after message_type on the wire */
|
|
1272
|
+
walk = p;
|
|
1273
|
+
for (;;) {
|
|
1274
|
+
int rc = ds_next(&walk, end, &f);
|
|
1275
|
+
if (rc < 0) {
|
|
1276
|
+
return PBW_ERR_INVALID;
|
|
1277
|
+
}
|
|
1278
|
+
if (rc == 0) {
|
|
1279
|
+
break;
|
|
1280
|
+
}
|
|
1281
|
+
if (f.field == 2) {
|
|
1282
|
+
if ((status = ds_str(package, sizeof(package), &f)) != PBW_OK) {
|
|
1283
|
+
return status;
|
|
1284
|
+
}
|
|
1285
|
+
} else if (f.field == 12) {
|
|
1286
|
+
if ((status = ds_str(syntax, sizeof(syntax), &f)) != PBW_OK) {
|
|
1287
|
+
return status;
|
|
1288
|
+
}
|
|
1289
|
+
} else if (f.field == 14 && f.wire == PBW_WIRE_VARINT) {
|
|
1290
|
+
has_edition = 1;
|
|
1291
|
+
edition = (int)(f.varint > INT32_MAX ? INT32_MAX : f.varint);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
/* An editions file with the proto2 edition keeps proto2 semantics;
|
|
1295
|
+
* every other edition behaves like proto3 unless told otherwise by
|
|
1296
|
+
* its FeatureSet. */
|
|
1297
|
+
proto3 = has_edition ? edition != 998 : strcmp(syntax, "proto3") == 0;
|
|
1298
|
+
/* Descriptors created below inherit the dialect of the file they came
|
|
1299
|
+
* from, so one set may hold proto2 and proto3 files at once. */
|
|
1300
|
+
schema->proto3 = proto3;
|
|
1301
|
+
schema->edition = has_edition ? edition : 0;
|
|
1302
|
+
/* File FeatureSet over the syntax defaults. FileOptions.features is
|
|
1303
|
+
* field 50; options may sit anywhere, so re-walk for them. */
|
|
1304
|
+
memset(&feat, 0, sizeof(feat));
|
|
1305
|
+
feat.presence = has_edition ? 1 : 0; /* EXPLICIT unless overridden */
|
|
1306
|
+
feat.enum_open = has_edition || proto3 ? 1 : 0; /* OPEN unless proto2 */
|
|
1307
|
+
feat.repack = proto3 || has_edition; /* PACKED for proto3/editions */
|
|
1308
|
+
feat.msg_enc = has_edition ? 1 : 0; /* LENGTH_PREFIXED for editions */
|
|
1309
|
+
walk = p;
|
|
1310
|
+
for (;;) {
|
|
1311
|
+
int rc = ds_next(&walk, end, &f);
|
|
1312
|
+
if (rc < 0) {
|
|
1313
|
+
return PBW_ERR_INVALID;
|
|
1314
|
+
}
|
|
1315
|
+
if (rc == 0) {
|
|
1316
|
+
break;
|
|
1317
|
+
}
|
|
1318
|
+
if (f.field != 8 || f.wire != PBW_WIRE_LEN) { /* options */
|
|
1319
|
+
continue;
|
|
1320
|
+
}
|
|
1321
|
+
{
|
|
1322
|
+
const uint8_t *o = f.data;
|
|
1323
|
+
const uint8_t *oend = f.data + f.len;
|
|
1324
|
+
ds_field of;
|
|
1325
|
+
|
|
1326
|
+
for (;;) {
|
|
1327
|
+
int orc = ds_next(&o, oend, &of);
|
|
1328
|
+
|
|
1329
|
+
if (orc < 0) {
|
|
1330
|
+
return PBW_ERR_INVALID;
|
|
1331
|
+
}
|
|
1332
|
+
if (orc == 0) {
|
|
1333
|
+
break;
|
|
1334
|
+
}
|
|
1335
|
+
if (of.field != 50 || of.wire != PBW_WIRE_LEN) {
|
|
1336
|
+
continue;
|
|
1337
|
+
}
|
|
1338
|
+
status = ds_parse_featureset(of.data, of.data + of.len,
|
|
1339
|
+
&feat);
|
|
1340
|
+
if (status != PBW_OK) {
|
|
1341
|
+
return status;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
/* pass 2: declarations */
|
|
1348
|
+
walk = p;
|
|
1349
|
+
for (;;) {
|
|
1350
|
+
int rc = ds_next(&walk, end, &f);
|
|
1351
|
+
if (rc < 0) {
|
|
1352
|
+
return PBW_ERR_INVALID;
|
|
1353
|
+
}
|
|
1354
|
+
if (rc == 0) {
|
|
1355
|
+
return PBW_OK;
|
|
1356
|
+
}
|
|
1357
|
+
if (f.wire != PBW_WIRE_LEN) {
|
|
1358
|
+
continue;
|
|
1359
|
+
}
|
|
1360
|
+
if (f.field == 4) { /* message_type */
|
|
1361
|
+
status = ds_message_proto(schema, &feat, package, f.data,
|
|
1362
|
+
f.data + f.len, 0, dlim, exts,
|
|
1363
|
+
ext_count, ext_cap);
|
|
1364
|
+
} else if (f.field == 5) { /* enum_type */
|
|
1365
|
+
status = ds_enum_proto(schema, package, f.data, f.data + f.len,
|
|
1366
|
+
feat.enum_open == 2, proto3);
|
|
1367
|
+
} else if (f.field == 6) { /* service */
|
|
1368
|
+
status = ds_service_proto(schema, package, f.data,
|
|
1369
|
+
f.data + f.len);
|
|
1370
|
+
} else if (f.field == 7) { /* extension */
|
|
1371
|
+
/* Heap, not stack: see the field/extension case above. */
|
|
1372
|
+
ds_field_info *info = (ds_field_info *)malloc(sizeof(*info));
|
|
1373
|
+
|
|
1374
|
+
if (info == NULL) {
|
|
1375
|
+
ds_error(schema, "out of memory");
|
|
1376
|
+
return PBW_ERR_NOMEM;
|
|
1377
|
+
}
|
|
1378
|
+
status = ds_parse_field(info, f.data, f.data + f.len);
|
|
1379
|
+
if (status == PBW_OK) {
|
|
1380
|
+
status = ds_queue_extension(schema, exts, ext_count, ext_cap,
|
|
1381
|
+
info, package, &feat);
|
|
1382
|
+
}
|
|
1383
|
+
free(info);
|
|
1384
|
+
} else {
|
|
1385
|
+
status = PBW_OK;
|
|
1386
|
+
}
|
|
1387
|
+
if (status != PBW_OK) {
|
|
1388
|
+
return status;
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
static pbw_status
|
|
1394
|
+
ds_load_impl(const void *data, size_t len, uint32_t dlim, size_t max_bytes,
|
|
1395
|
+
pbw_schema **out)
|
|
1396
|
+
{
|
|
1397
|
+
const uint8_t *p;
|
|
1398
|
+
const uint8_t *end;
|
|
1399
|
+
pbw_schema *schema;
|
|
1400
|
+
pbw_status status = PBW_OK;
|
|
1401
|
+
ds_pending_ext *exts = NULL;
|
|
1402
|
+
size_t ext_count = 0;
|
|
1403
|
+
size_t ext_cap = 0;
|
|
1404
|
+
ds_field f;
|
|
1405
|
+
|
|
1406
|
+
if (out == NULL) {
|
|
1407
|
+
return PBW_ERR_INVALID;
|
|
1408
|
+
}
|
|
1409
|
+
*out = NULL;
|
|
1410
|
+
if (data == NULL) {
|
|
1411
|
+
return PBW_ERR_INVALID;
|
|
1412
|
+
}
|
|
1413
|
+
schema = pbw_schema_new();
|
|
1414
|
+
if (schema == NULL) {
|
|
1415
|
+
return PBW_ERR_NOMEM;
|
|
1416
|
+
}
|
|
1417
|
+
if (max_bytes > 0 && len > max_bytes) {
|
|
1418
|
+
ds_error(schema, "FileDescriptorSet exceeds max_bytes");
|
|
1419
|
+
*out = schema;
|
|
1420
|
+
return PBW_ERR_LIMIT;
|
|
1421
|
+
}
|
|
1422
|
+
p = pbw_normalize_input(data, len);
|
|
1423
|
+
end = p + len;
|
|
1424
|
+
for (;;) {
|
|
1425
|
+
int rc = ds_next(&p, end, &f);
|
|
1426
|
+
if (rc < 0) {
|
|
1427
|
+
ds_error(schema, "malformed FileDescriptorSet");
|
|
1428
|
+
status = PBW_ERR_INVALID;
|
|
1429
|
+
break;
|
|
1430
|
+
}
|
|
1431
|
+
if (rc == 0) {
|
|
1432
|
+
break;
|
|
1433
|
+
}
|
|
1434
|
+
if (f.field == 1 && f.wire == PBW_WIRE_LEN) { /* file */
|
|
1435
|
+
status = ds_file_proto(schema, f.data, f.data + f.len, dlim,
|
|
1436
|
+
&exts, &ext_count, &ext_cap);
|
|
1437
|
+
if (status != PBW_OK) {
|
|
1438
|
+
break;
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
if (status == PBW_OK) {
|
|
1443
|
+
status = ds_attach_extensions(schema, exts, ext_count);
|
|
1444
|
+
}
|
|
1445
|
+
free(exts);
|
|
1446
|
+
if (status == PBW_OK) {
|
|
1447
|
+
status = pbw_schema_link(schema);
|
|
1448
|
+
}
|
|
1449
|
+
if (status != PBW_OK && schema->error[0] == '\0') {
|
|
1450
|
+
/* Every failure must be explainable through pbw_schema_error(), also
|
|
1451
|
+
* the ones deep in the per-file parsers and the OOM paths of the
|
|
1452
|
+
* link step. */
|
|
1453
|
+
ds_error(schema,
|
|
1454
|
+
status == PBW_ERR_NOMEM ? "out of memory"
|
|
1455
|
+
: "malformed FileDescriptorSet");
|
|
1456
|
+
}
|
|
1457
|
+
*out = schema; /* on failure the reason is in pbw_schema_error() */
|
|
1458
|
+
return status;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
pbw_status
|
|
1462
|
+
pbw_descriptor_set_load(const void *data, size_t len, pbw_schema **out)
|
|
1463
|
+
{
|
|
1464
|
+
return ds_load_impl(data, len, PBW_MAX_DEPTH, 0, out);
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
pbw_status
|
|
1468
|
+
pbw_descriptor_set_load_limits(const void *data, size_t len,
|
|
1469
|
+
const pbw_limits *limits, pbw_schema **out)
|
|
1470
|
+
{
|
|
1471
|
+
uint32_t dlim = PBW_MAX_DEPTH;
|
|
1472
|
+
size_t max_bytes = 0;
|
|
1473
|
+
|
|
1474
|
+
if (limits != NULL) {
|
|
1475
|
+
/* Above PBW_MAX_DEPTH clamps, like the wire decoder. */
|
|
1476
|
+
if (limits->max_depth > 0 && limits->max_depth < dlim) {
|
|
1477
|
+
dlim = limits->max_depth;
|
|
1478
|
+
}
|
|
1479
|
+
max_bytes = limits->max_bytes;
|
|
1480
|
+
}
|
|
1481
|
+
return ds_load_impl(data, len, dlim, max_bytes, out);
|
|
1482
|
+
}
|