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,4792 @@
|
|
|
1
|
+
/* Protobuf JSON mapping: printing and parsing.
|
|
2
|
+
*
|
|
3
|
+
* The printer emits the canonical mapping the reference implementation
|
|
4
|
+
* produces: field names in camelCase (`[full.name]` for extensions),
|
|
5
|
+
* 64-bit integers quoted, bytes as standard base64, enums by name, floats in
|
|
6
|
+
* the shortest round-tripping form, well-known types in their JSON form
|
|
7
|
+
* (`{"@type": ...}` expansions, RFC 3339 timestamps, `"3.2s"` durations,
|
|
8
|
+
* `Struct` as a nested object, `FieldMask` as one camelCase string, wrappers
|
|
9
|
+
* as bare values). Unknown fields are not printed. The parser accepts what
|
|
10
|
+
* the reference parser accepts in its default (legacy) mode: proto and
|
|
11
|
+
* camelCase names, quoted or bare 64-bit integers, floats with integral or
|
|
12
|
+
* exponential text, quoted bools, single-quoted and bare key strings,
|
|
13
|
+
* trailing commas, `null` as unset, nested-array flattening (outside
|
|
14
|
+
* `Value`/`ListValue`), the object form of
|
|
15
|
+
* well-known types (`{"seconds": ...}`), base64 with either alphabet and
|
|
16
|
+
* optional padding, unknown closed-enum numbers diverted to unknown fields
|
|
17
|
+
* (like the binary decoder), and duplicate keys allowed except inside oneofs
|
|
18
|
+
* and maps. Anything else unknown fails unless told to ignore it.
|
|
19
|
+
*
|
|
20
|
+
* Like the other formats, nesting is capped at PBW_MAX_DEPTH, string fields
|
|
21
|
+
* reject invalid UTF-8, and a missing proto2 `required` field fails the parse
|
|
22
|
+
* with PBW_ERR_REQUIRED.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#include "internal.h"
|
|
26
|
+
|
|
27
|
+
#include <ctype.h>
|
|
28
|
+
#include <limits.h>
|
|
29
|
+
#include <math.h>
|
|
30
|
+
#include <stdio.h>
|
|
31
|
+
#include <string.h>
|
|
32
|
+
|
|
33
|
+
/* ---------------------------------------------------------- json names */
|
|
34
|
+
|
|
35
|
+
/* protoc's ToJsonName: drop every '_', uppercase the letter after it. */
|
|
36
|
+
static size_t
|
|
37
|
+
j_json_name(const char *name, char *out, size_t cap)
|
|
38
|
+
{
|
|
39
|
+
size_t n = 0;
|
|
40
|
+
int upper = 0;
|
|
41
|
+
|
|
42
|
+
for (; *name != '\0'; name++) {
|
|
43
|
+
char c;
|
|
44
|
+
|
|
45
|
+
if (*name == '_') {
|
|
46
|
+
upper = 1;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
c = upper ? (char)toupper((unsigned char)*name) : *name;
|
|
50
|
+
upper = 0;
|
|
51
|
+
if (n + 1 < cap) {
|
|
52
|
+
out[n] = c;
|
|
53
|
+
}
|
|
54
|
+
n++;
|
|
55
|
+
}
|
|
56
|
+
if (cap > 0) {
|
|
57
|
+
out[n < cap ? n : cap - 1] = '\0';
|
|
58
|
+
}
|
|
59
|
+
return n;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* protoc's ToCamelCase with a lowercased first letter. */
|
|
63
|
+
static size_t
|
|
64
|
+
j_camel_name(const char *name, char *out, size_t cap)
|
|
65
|
+
{
|
|
66
|
+
size_t n = 0;
|
|
67
|
+
int upper = 1;
|
|
68
|
+
|
|
69
|
+
for (; *name != '\0'; name++) {
|
|
70
|
+
char c;
|
|
71
|
+
|
|
72
|
+
if (*name == '_') {
|
|
73
|
+
upper = 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
c = upper ? (char)toupper((unsigned char)*name) : *name;
|
|
77
|
+
upper = 0;
|
|
78
|
+
if (n == 0) {
|
|
79
|
+
c = (char)tolower((unsigned char)c);
|
|
80
|
+
}
|
|
81
|
+
if (n + 1 < cap) {
|
|
82
|
+
out[n] = c;
|
|
83
|
+
}
|
|
84
|
+
n++;
|
|
85
|
+
}
|
|
86
|
+
if (cap > 0) {
|
|
87
|
+
out[n < cap ? n : cap - 1] = '\0';
|
|
88
|
+
}
|
|
89
|
+
return n;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* Field names print as their jsonName: protoc's ToCamelCase with a
|
|
93
|
+
* lowercased first letter is j_camel_name() below, used for parsing;
|
|
94
|
+
* printing keeps the mapping j_json_name() computes. */
|
|
95
|
+
static void
|
|
96
|
+
j_print_name(const pbw_field *field, char *out, size_t cap)
|
|
97
|
+
{
|
|
98
|
+
j_json_name(field->name, out, cap);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* The reference parser matches camelCase names, then proto names, then json
|
|
102
|
+
* names, in that order. */
|
|
103
|
+
static int
|
|
104
|
+
j_field_by_name(const pbw_descriptor *desc, const char *key, size_t len,
|
|
105
|
+
const pbw_field **out)
|
|
106
|
+
{
|
|
107
|
+
/* Room for the longest name pbw_descriptor_set_load() accepts (512
|
|
108
|
+
* bytes); a shorter buffer would truncate and could alias two names. */
|
|
109
|
+
char tmp[520];
|
|
110
|
+
size_t i;
|
|
111
|
+
|
|
112
|
+
for (i = 0; i < desc->field_count; i++) {
|
|
113
|
+
const pbw_field *f = &desc->fields[i];
|
|
114
|
+
size_t n = strlen(f->name);
|
|
115
|
+
|
|
116
|
+
/* Camelizing drops underscores, so the JSON spelling can be shorter
|
|
117
|
+
* than the proto name; only a name without one is length-checked,
|
|
118
|
+
* which keeps the hot path at one compare. */
|
|
119
|
+
if (n != len && strchr(f->name, '_') == NULL) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
j_camel_name(f->name, tmp, sizeof(tmp));
|
|
123
|
+
if (strlen(tmp) == len && memcmp(tmp, key, len) == 0) {
|
|
124
|
+
*out = f;
|
|
125
|
+
return 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (i = 0; i < desc->field_count; i++) {
|
|
129
|
+
const pbw_field *f = &desc->fields[i];
|
|
130
|
+
|
|
131
|
+
if (strlen(f->name) == len && memcmp(f->name, key, len) == 0) {
|
|
132
|
+
*out = f;
|
|
133
|
+
return 1;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
for (i = 0; i < desc->field_count; i++) {
|
|
137
|
+
const pbw_field *f = &desc->fields[i];
|
|
138
|
+
|
|
139
|
+
j_json_name(f->name, tmp, sizeof(tmp));
|
|
140
|
+
if (strlen(tmp) == len && memcmp(tmp, key, len) == 0) {
|
|
141
|
+
*out = f;
|
|
142
|
+
return 1;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* -------------------------------------------------------- well-known */
|
|
149
|
+
|
|
150
|
+
/* The JSON mapping special-cases these by full message name. */
|
|
151
|
+
typedef enum {
|
|
152
|
+
J_NOT_WKT = 0,
|
|
153
|
+
J_ANY,
|
|
154
|
+
J_TIMESTAMP,
|
|
155
|
+
J_DURATION,
|
|
156
|
+
J_STRUCT,
|
|
157
|
+
J_VALUE,
|
|
158
|
+
J_LIST,
|
|
159
|
+
J_FIELDMASK,
|
|
160
|
+
J_WRAPPER,
|
|
161
|
+
J_EMPTY,
|
|
162
|
+
J_NULLVALUE /* not a message, but routed through here for nulls */
|
|
163
|
+
} j_wkt;
|
|
164
|
+
|
|
165
|
+
static j_wkt
|
|
166
|
+
j_classify(const pbw_descriptor *desc)
|
|
167
|
+
{
|
|
168
|
+
static const struct {
|
|
169
|
+
const char *name;
|
|
170
|
+
j_wkt kind;
|
|
171
|
+
} table[] = {
|
|
172
|
+
{"google.protobuf.Any", J_ANY},
|
|
173
|
+
{"google.protobuf.Timestamp", J_TIMESTAMP},
|
|
174
|
+
{"google.protobuf.Duration", J_DURATION},
|
|
175
|
+
{"google.protobuf.Struct", J_STRUCT},
|
|
176
|
+
{"google.protobuf.Value", J_VALUE},
|
|
177
|
+
{"google.protobuf.ListValue", J_LIST},
|
|
178
|
+
{"google.protobuf.FieldMask", J_FIELDMASK},
|
|
179
|
+
{"google.protobuf.DoubleValue", J_WRAPPER},
|
|
180
|
+
{"google.protobuf.FloatValue", J_WRAPPER},
|
|
181
|
+
{"google.protobuf.Int64Value", J_WRAPPER},
|
|
182
|
+
{"google.protobuf.UInt64Value", J_WRAPPER},
|
|
183
|
+
{"google.protobuf.Int32Value", J_WRAPPER},
|
|
184
|
+
{"google.protobuf.UInt32Value", J_WRAPPER},
|
|
185
|
+
{"google.protobuf.BoolValue", J_WRAPPER},
|
|
186
|
+
{"google.protobuf.StringValue", J_WRAPPER},
|
|
187
|
+
{"google.protobuf.BytesValue", J_WRAPPER},
|
|
188
|
+
{"google.protobuf.Empty", J_EMPTY},
|
|
189
|
+
{"google.protobuf.NullValue", J_NULLVALUE},
|
|
190
|
+
};
|
|
191
|
+
size_t i;
|
|
192
|
+
|
|
193
|
+
if (desc == NULL) {
|
|
194
|
+
return J_NOT_WKT;
|
|
195
|
+
}
|
|
196
|
+
for (i = 0; i < sizeof(table) / sizeof(table[0]); i++) {
|
|
197
|
+
if (strcmp(desc->name, table[i].name) == 0) {
|
|
198
|
+
return table[i].kind;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return J_NOT_WKT;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* ---------------------------------------------------------------- output */
|
|
205
|
+
|
|
206
|
+
typedef struct {
|
|
207
|
+
pbw_buf buf;
|
|
208
|
+
pbw_status status;
|
|
209
|
+
uint32_t depth; /* message nesting, capped at PBW_MAX_DEPTH */
|
|
210
|
+
} j_out;
|
|
211
|
+
|
|
212
|
+
static void
|
|
213
|
+
j_put(j_out *j, const void *data, size_t len)
|
|
214
|
+
{
|
|
215
|
+
if (j->status != PBW_OK || len == 0) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (!pbw_buf_reserve(&j->buf, len)) {
|
|
219
|
+
j->status = PBW_ERR_NOMEM;
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
memcpy(j->buf.data + j->buf.len, data, len);
|
|
223
|
+
j->buf.len += len;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
static void
|
|
227
|
+
j_str(j_out *j, const char *s)
|
|
228
|
+
{
|
|
229
|
+
j_put(j, s, strlen(s));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
static void
|
|
233
|
+
j_ch(j_out *j, char c)
|
|
234
|
+
{
|
|
235
|
+
j_put(j, &c, 1);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static void
|
|
239
|
+
j_u64(j_out *j, uint64_t value)
|
|
240
|
+
{
|
|
241
|
+
char tmp[24];
|
|
242
|
+
int n = snprintf(tmp, sizeof(tmp), "%llu", (unsigned long long)value);
|
|
243
|
+
|
|
244
|
+
if (n > 0) {
|
|
245
|
+
j_put(j, tmp, (size_t)n);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static void
|
|
250
|
+
j_i64(j_out *j, int64_t value)
|
|
251
|
+
{
|
|
252
|
+
char tmp[24];
|
|
253
|
+
int n = snprintf(tmp, sizeof(tmp), "%lld", (long long)value);
|
|
254
|
+
|
|
255
|
+
if (n > 0) {
|
|
256
|
+
j_put(j, tmp, (size_t)n);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/* Shortest decimal that still parses back to the same bits. */
|
|
261
|
+
static void
|
|
262
|
+
j_double(j_out *j, double value, int is_float)
|
|
263
|
+
{
|
|
264
|
+
int prec;
|
|
265
|
+
int prec_max = 17;
|
|
266
|
+
int prec_min = is_float ? 9 : 15;
|
|
267
|
+
|
|
268
|
+
for (prec = prec_min; prec <= prec_max; prec++) {
|
|
269
|
+
char tmp[32];
|
|
270
|
+
int n = pbw_format_g_c(tmp, sizeof(tmp), prec, value);
|
|
271
|
+
double back;
|
|
272
|
+
|
|
273
|
+
if (n <= 0 || (size_t)n >= sizeof(tmp)) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
back = pbw_strtod_c(tmp, NULL);
|
|
277
|
+
if ((is_float ? (double)(float)back == value : back == value)) {
|
|
278
|
+
j_put(j, tmp, (size_t)n);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
j_str(j, "0");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/* Writes the string quoted, with the reference printer's escapes. Invalid
|
|
286
|
+
* UTF-8 fails: the reference printer reports it instead of emitting bytes. */
|
|
287
|
+
static void
|
|
288
|
+
j_quoted(j_out *j, const uint8_t *data, size_t len)
|
|
289
|
+
{
|
|
290
|
+
static const char *hex = "0123456789abcdef";
|
|
291
|
+
const uint8_t *p;
|
|
292
|
+
const uint8_t *end;
|
|
293
|
+
|
|
294
|
+
if (j == NULL || j->status != PBW_OK) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (data == NULL) {
|
|
298
|
+
if (len > 0) {
|
|
299
|
+
j->status = PBW_ERR_INVALID;
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
data = pbw_empty_input;
|
|
303
|
+
}
|
|
304
|
+
p = data;
|
|
305
|
+
end = data + len;
|
|
306
|
+
|
|
307
|
+
j_ch(j, '"');
|
|
308
|
+
while (p < end) {
|
|
309
|
+
uint8_t c = *p;
|
|
310
|
+
|
|
311
|
+
switch (c) {
|
|
312
|
+
case '"':
|
|
313
|
+
j_str(j, "\\\"");
|
|
314
|
+
p++;
|
|
315
|
+
continue;
|
|
316
|
+
case '\\':
|
|
317
|
+
j_str(j, "\\\\");
|
|
318
|
+
p++;
|
|
319
|
+
continue;
|
|
320
|
+
case '\b':
|
|
321
|
+
j_str(j, "\\b");
|
|
322
|
+
p++;
|
|
323
|
+
continue;
|
|
324
|
+
case '\f':
|
|
325
|
+
j_str(j, "\\f");
|
|
326
|
+
p++;
|
|
327
|
+
continue;
|
|
328
|
+
case '\n':
|
|
329
|
+
j_str(j, "\\n");
|
|
330
|
+
p++;
|
|
331
|
+
continue;
|
|
332
|
+
case '\r':
|
|
333
|
+
j_str(j, "\\r");
|
|
334
|
+
p++;
|
|
335
|
+
continue;
|
|
336
|
+
case '\t':
|
|
337
|
+
j_str(j, "\\t");
|
|
338
|
+
p++;
|
|
339
|
+
continue;
|
|
340
|
+
default:
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
if (c == '<') {
|
|
344
|
+
j_str(j, "\\u003c");
|
|
345
|
+
p++;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (c == '>') {
|
|
349
|
+
j_str(j, "\\u003e");
|
|
350
|
+
p++;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
if (c < 0x20 || c == 0x7f) {
|
|
354
|
+
char esc[6];
|
|
355
|
+
|
|
356
|
+
esc[0] = '\\';
|
|
357
|
+
esc[1] = 'u';
|
|
358
|
+
esc[2] = '0';
|
|
359
|
+
esc[3] = '0';
|
|
360
|
+
esc[4] = hex[c >> 4];
|
|
361
|
+
esc[5] = hex[c & 15];
|
|
362
|
+
j_put(j, esc, 6);
|
|
363
|
+
p++;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (c < 0x80) {
|
|
367
|
+
j_ch(j, (char)c);
|
|
368
|
+
p++;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
/* Pass UTF-8 through after validating the whole scalar. */
|
|
372
|
+
{
|
|
373
|
+
size_t left = pbw_remaining((const uint8_t *)end, (const uint8_t *)p);
|
|
374
|
+
uint32_t rune = 0;
|
|
375
|
+
size_t width = 0;
|
|
376
|
+
|
|
377
|
+
if ((c & 0xe0) == 0xc0) {
|
|
378
|
+
rune = c & 0x1f;
|
|
379
|
+
width = 2;
|
|
380
|
+
} else if ((c & 0xf0) == 0xe0) {
|
|
381
|
+
rune = c & 0x0f;
|
|
382
|
+
width = 3;
|
|
383
|
+
} else if ((c & 0xf8) == 0xf0) {
|
|
384
|
+
rune = c & 0x07;
|
|
385
|
+
width = 4;
|
|
386
|
+
} else {
|
|
387
|
+
j->status = PBW_ERR_INVALID;
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (width > left) {
|
|
391
|
+
j->status = PBW_ERR_INVALID;
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
{
|
|
395
|
+
size_t k;
|
|
396
|
+
|
|
397
|
+
for (k = 1; k < width; k++) {
|
|
398
|
+
if ((p[k] & 0xc0) != 0x80) {
|
|
399
|
+
j->status = PBW_ERR_INVALID;
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
rune = (rune << 6) | (p[k] & 0x3f);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if ((width == 2 && rune < 0x80) ||
|
|
406
|
+
(width == 3 && rune < 0x800) ||
|
|
407
|
+
(width == 4 && rune < 0x10000) ||
|
|
408
|
+
rune > 0x10ffff || (rune >= 0xd800 && rune <= 0xdfff)) {
|
|
409
|
+
j->status = PBW_ERR_INVALID;
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
j_put(j, p, width);
|
|
413
|
+
p += width;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
j_ch(j, '"');
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
static void
|
|
420
|
+
j_base64(j_out *j, const uint8_t *data, size_t len)
|
|
421
|
+
{
|
|
422
|
+
static const char *alphabet =
|
|
423
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
424
|
+
const uint8_t *p;
|
|
425
|
+
const uint8_t *end;
|
|
426
|
+
|
|
427
|
+
if (j == NULL || j->status != PBW_OK) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (data == NULL) {
|
|
431
|
+
if (len > 0) {
|
|
432
|
+
j->status = PBW_ERR_INVALID;
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
data = pbw_empty_input;
|
|
436
|
+
}
|
|
437
|
+
p = data;
|
|
438
|
+
end = data + len;
|
|
439
|
+
|
|
440
|
+
j_ch(j, '"');
|
|
441
|
+
while (pbw_remaining(end, p) >= 3) {
|
|
442
|
+
char out[4];
|
|
443
|
+
|
|
444
|
+
out[0] = alphabet[p[0] >> 2];
|
|
445
|
+
out[1] = alphabet[((p[0] & 3) << 4) | (p[1] >> 4)];
|
|
446
|
+
out[2] = alphabet[((p[1] & 0xf) << 2) | (p[2] >> 6)];
|
|
447
|
+
out[3] = alphabet[p[2] & 0x3f];
|
|
448
|
+
j_put(j, out, 4);
|
|
449
|
+
p += 3;
|
|
450
|
+
}
|
|
451
|
+
if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) == 2) {
|
|
452
|
+
char out[4];
|
|
453
|
+
|
|
454
|
+
out[0] = alphabet[p[0] >> 2];
|
|
455
|
+
out[1] = alphabet[((p[0] & 3) << 4) | (p[1] >> 4)];
|
|
456
|
+
out[2] = alphabet[(p[1] & 0xf) << 2];
|
|
457
|
+
out[3] = '=';
|
|
458
|
+
j_put(j, out, 4);
|
|
459
|
+
} else if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) == 1) {
|
|
460
|
+
char out[4];
|
|
461
|
+
|
|
462
|
+
out[0] = alphabet[p[0] >> 2];
|
|
463
|
+
out[1] = alphabet[(p[0] & 3) << 4];
|
|
464
|
+
out[2] = '=';
|
|
465
|
+
out[3] = '=';
|
|
466
|
+
j_put(j, out, 4);
|
|
467
|
+
}
|
|
468
|
+
j_ch(j, '"');
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/* Whether a singular scalar prints: proto3 drops defaults, proto2 prints
|
|
472
|
+
* anything present, oneof members print when theirs is the set case. */
|
|
473
|
+
static int
|
|
474
|
+
j_should_print(const pbw_message *msg, size_t index)
|
|
475
|
+
{
|
|
476
|
+
const pbw_field *field = &msg->desc->fields[index];
|
|
477
|
+
const pbw_value *value = &msg->values[index];
|
|
478
|
+
|
|
479
|
+
if (!pbw_msg_present(msg, index)) {
|
|
480
|
+
return 0;
|
|
481
|
+
}
|
|
482
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
483
|
+
return value->kind == PBW_VALUE_LIST && value->as.list.len > 0;
|
|
484
|
+
}
|
|
485
|
+
if (field->type == PBW_TYPE_MESSAGE || field->type == PBW_TYPE_GROUP) {
|
|
486
|
+
return 1;
|
|
487
|
+
}
|
|
488
|
+
if (field->oneof_index != 0) {
|
|
489
|
+
return msg->oneof_case[field->oneof_index] == field->number;
|
|
490
|
+
}
|
|
491
|
+
if (pbw_field_implicit_presence(field, msg->desc)) {
|
|
492
|
+
return !pbw_value_is_default(value);
|
|
493
|
+
}
|
|
494
|
+
return 1;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/* An empty google.protobuf.Value prints as nothing: the reference printer
|
|
498
|
+
* silently discards it (and errors only at the top level, which never
|
|
499
|
+
* happens here since the top message is a real type). */
|
|
500
|
+
static int
|
|
501
|
+
j_value_is_empty(const pbw_message *msg)
|
|
502
|
+
{
|
|
503
|
+
size_t i;
|
|
504
|
+
|
|
505
|
+
if (!pbw_msg_fresh(msg)) {
|
|
506
|
+
/* A retired message has fewer slots than its live descriptor, so the
|
|
507
|
+
* scan below would read past them; and answering "empty" would drop
|
|
508
|
+
* the element instead of reporting it. Callers take 0 as "not empty"
|
|
509
|
+
* and hand it to j_print_message(), which reports PBW_ERR_INVALID. */
|
|
510
|
+
return 0;
|
|
511
|
+
}
|
|
512
|
+
for (i = 0; i < msg->desc->field_count; i++) {
|
|
513
|
+
if (pbw_msg_present(msg, i)) {
|
|
514
|
+
return 0;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return 1;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
static void j_print_message(j_out *j, const pbw_message *msg);
|
|
521
|
+
|
|
522
|
+
static void
|
|
523
|
+
j_print_scalar(j_out *j, const pbw_field *field, const pbw_value *value)
|
|
524
|
+
{
|
|
525
|
+
uint64_t bits = value->kind == PBW_VALUE_UINT ? value->as.u64 : 0;
|
|
526
|
+
|
|
527
|
+
switch (field->type) {
|
|
528
|
+
case PBW_TYPE_DOUBLE:
|
|
529
|
+
case PBW_TYPE_FLOAT: {
|
|
530
|
+
double v;
|
|
531
|
+
|
|
532
|
+
if (field->type == PBW_TYPE_FLOAT) {
|
|
533
|
+
/* Floats live in the low 32 bits of the raw bit pattern: mask
|
|
534
|
+
* first, so the memcpy below never reads the high half (which
|
|
535
|
+
* is meaningless, and byte-reversed on a big-endian host). */
|
|
536
|
+
uint32_t bits32 = (uint32_t)bits;
|
|
537
|
+
float f;
|
|
538
|
+
|
|
539
|
+
memcpy(&f, &bits32, sizeof(f));
|
|
540
|
+
v = (double)f;
|
|
541
|
+
} else {
|
|
542
|
+
memcpy(&v, &bits, sizeof(v));
|
|
543
|
+
}
|
|
544
|
+
if (isnan(v)) {
|
|
545
|
+
j_str(j, "\"NaN\"");
|
|
546
|
+
} else if (isinf(v)) {
|
|
547
|
+
j_str(j, v < 0 ? "\"-Infinity\"" : "\"Infinity\"");
|
|
548
|
+
} else {
|
|
549
|
+
j_double(j, v, field->type == PBW_TYPE_FLOAT);
|
|
550
|
+
}
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
case PBW_TYPE_INT64:
|
|
554
|
+
case PBW_TYPE_SINT64:
|
|
555
|
+
case PBW_TYPE_SFIXED64:
|
|
556
|
+
j_ch(j, '"');
|
|
557
|
+
j_i64(j, (int64_t)bits);
|
|
558
|
+
j_ch(j, '"');
|
|
559
|
+
break;
|
|
560
|
+
case PBW_TYPE_UINT64:
|
|
561
|
+
case PBW_TYPE_FIXED64:
|
|
562
|
+
j_ch(j, '"');
|
|
563
|
+
j_u64(j, bits);
|
|
564
|
+
j_ch(j, '"');
|
|
565
|
+
break;
|
|
566
|
+
case PBW_TYPE_INT32:
|
|
567
|
+
case PBW_TYPE_SINT32:
|
|
568
|
+
case PBW_TYPE_SFIXED32:
|
|
569
|
+
j_i64(j, (int64_t)(int32_t)bits);
|
|
570
|
+
break;
|
|
571
|
+
case PBW_TYPE_UINT32:
|
|
572
|
+
case PBW_TYPE_FIXED32:
|
|
573
|
+
j_u64(j, (uint64_t)(uint32_t)bits);
|
|
574
|
+
break;
|
|
575
|
+
case PBW_TYPE_BOOL:
|
|
576
|
+
j_str(j, bits != 0 ? "true" : "false");
|
|
577
|
+
break;
|
|
578
|
+
case PBW_TYPE_STRING: {
|
|
579
|
+
size_t len = 0;
|
|
580
|
+
const uint8_t *data = NULL;
|
|
581
|
+
|
|
582
|
+
if (value->kind == PBW_VALUE_BYTES) {
|
|
583
|
+
data = value->as.bytes.data;
|
|
584
|
+
len = value->as.bytes.len;
|
|
585
|
+
}
|
|
586
|
+
/* Pass data through: j_quoted() rejects a NULL payload with a
|
|
587
|
+
* length, and substituting "" here turned that into a read past the
|
|
588
|
+
* literal. */
|
|
589
|
+
j_quoted(j, data, len);
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
case PBW_TYPE_BYTES: {
|
|
593
|
+
size_t len = 0;
|
|
594
|
+
const uint8_t *data = NULL;
|
|
595
|
+
|
|
596
|
+
if (value->kind == PBW_VALUE_BYTES) {
|
|
597
|
+
data = value->as.bytes.data;
|
|
598
|
+
len = value->as.bytes.len;
|
|
599
|
+
}
|
|
600
|
+
j_base64(j, data, len); /* same rule as the string branch above */
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
case PBW_TYPE_ENUM: {
|
|
604
|
+
const char *name = NULL;
|
|
605
|
+
|
|
606
|
+
if (field->sub != NULL && j_classify(field->sub) == J_NULLVALUE) {
|
|
607
|
+
/* google.protobuf.NullValue prints as JSON null, never as its
|
|
608
|
+
* name: that null is the canonical spelling, also inside a oneof
|
|
609
|
+
* and as a repeated element. */
|
|
610
|
+
j_str(j, "null");
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
if (field->sub != NULL &&
|
|
614
|
+
pbw_enum_find_name(field->sub, (int32_t)bits, &name)) {
|
|
615
|
+
j_quoted(j, (const uint8_t *)name, strlen(name));
|
|
616
|
+
} else {
|
|
617
|
+
/* Open enums keep unknown numbers; they print as numbers. */
|
|
618
|
+
j_i64(j, (int64_t)(int32_t)bits);
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
case PBW_TYPE_MESSAGE:
|
|
623
|
+
case PBW_TYPE_GROUP:
|
|
624
|
+
if (value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL) {
|
|
625
|
+
j_print_message(j, value->as.msg);
|
|
626
|
+
} else {
|
|
627
|
+
j_str(j, "{}");
|
|
628
|
+
}
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/* Map keys print quoted: integers decimal, bools as "true"/"false". */
|
|
634
|
+
static void
|
|
635
|
+
j_print_map_key(j_out *j, const pbw_field *key_field, const pbw_value *key)
|
|
636
|
+
{
|
|
637
|
+
uint64_t bits = key->kind == PBW_VALUE_UINT ? key->as.u64 : 0;
|
|
638
|
+
|
|
639
|
+
j_ch(j, '"');
|
|
640
|
+
switch (key_field->type) {
|
|
641
|
+
case PBW_TYPE_INT64:
|
|
642
|
+
case PBW_TYPE_SINT64:
|
|
643
|
+
case PBW_TYPE_SFIXED64:
|
|
644
|
+
case PBW_TYPE_INT32:
|
|
645
|
+
case PBW_TYPE_SINT32:
|
|
646
|
+
case PBW_TYPE_SFIXED32:
|
|
647
|
+
j_i64(j, (int64_t)bits);
|
|
648
|
+
break;
|
|
649
|
+
case PBW_TYPE_UINT64:
|
|
650
|
+
case PBW_TYPE_FIXED64:
|
|
651
|
+
case PBW_TYPE_UINT32:
|
|
652
|
+
case PBW_TYPE_FIXED32:
|
|
653
|
+
j_u64(j, bits);
|
|
654
|
+
break;
|
|
655
|
+
case PBW_TYPE_BOOL:
|
|
656
|
+
j_str(j, bits != 0 ? "true" : "false");
|
|
657
|
+
break;
|
|
658
|
+
case PBW_TYPE_STRING: {
|
|
659
|
+
/* j_quoted adds its own quotes; emit the payload raw here. */
|
|
660
|
+
size_t len = 0;
|
|
661
|
+
const uint8_t *data = NULL;
|
|
662
|
+
|
|
663
|
+
if (key->kind == PBW_VALUE_BYTES) {
|
|
664
|
+
data = key->as.bytes.data;
|
|
665
|
+
len = key->as.bytes.len;
|
|
666
|
+
}
|
|
667
|
+
/* Reuse the escaper without double quotes: temporarily write into
|
|
668
|
+
* the buffer through j_quoted then strip the quotes. */
|
|
669
|
+
{
|
|
670
|
+
size_t before = j->buf.len;
|
|
671
|
+
|
|
672
|
+
j_quoted(j, data, len);
|
|
673
|
+
if (j->status == PBW_OK && j->buf.len >= before + 2) {
|
|
674
|
+
memmove(j->buf.data + before, j->buf.data + before + 1,
|
|
675
|
+
j->buf.len - before - 2);
|
|
676
|
+
j->buf.len -= 2;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
case PBW_TYPE_ENUM: {
|
|
682
|
+
const char *name = NULL;
|
|
683
|
+
|
|
684
|
+
if (key_field->sub != NULL &&
|
|
685
|
+
pbw_enum_find_name(key_field->sub, (int32_t)bits, &name)) {
|
|
686
|
+
size_t before = j->buf.len;
|
|
687
|
+
|
|
688
|
+
j_quoted(j, (const uint8_t *)name, strlen(name));
|
|
689
|
+
if (j->status == PBW_OK && j->buf.len >= before + 2) {
|
|
690
|
+
memmove(j->buf.data + before, j->buf.data + before + 1,
|
|
691
|
+
j->buf.len - before - 2);
|
|
692
|
+
j->buf.len -= 2;
|
|
693
|
+
}
|
|
694
|
+
} else {
|
|
695
|
+
j_i64(j, (int64_t)(int32_t)bits);
|
|
696
|
+
}
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
default:
|
|
700
|
+
j->status = PBW_ERR_INVALID;
|
|
701
|
+
break;
|
|
702
|
+
}
|
|
703
|
+
j_ch(j, '"');
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/* Flat map key for duplicate elimination: missing keys normalize to
|
|
707
|
+
* (UINT, 0), exactly like the `&zero` default in the print loop below. */
|
|
708
|
+
typedef struct {
|
|
709
|
+
uint32_t kind;
|
|
710
|
+
uint64_t u64;
|
|
711
|
+
const uint8_t *data;
|
|
712
|
+
size_t len;
|
|
713
|
+
int has_key; /* 0: entry omitted the key; never counts as a later dup */
|
|
714
|
+
} j_flatkey;
|
|
715
|
+
|
|
716
|
+
static int
|
|
717
|
+
j_flatkey_cmp(const j_flatkey *a, const j_flatkey *b)
|
|
718
|
+
{
|
|
719
|
+
if (a->kind != b->kind) {
|
|
720
|
+
return a->kind < b->kind ? -1 : 1;
|
|
721
|
+
}
|
|
722
|
+
if (a->kind == PBW_VALUE_BYTES) {
|
|
723
|
+
size_t n = a->len < b->len ? a->len : b->len;
|
|
724
|
+
int r = n > 0 ? memcmp(a->data, b->data, n) : 0;
|
|
725
|
+
|
|
726
|
+
if (r != 0) {
|
|
727
|
+
return r < 0 ? -1 : 1;
|
|
728
|
+
}
|
|
729
|
+
if (a->len != b->len) {
|
|
730
|
+
return a->len < b->len ? -1 : 1;
|
|
731
|
+
}
|
|
732
|
+
return 0;
|
|
733
|
+
}
|
|
734
|
+
if (a->u64 != b->u64) {
|
|
735
|
+
return a->u64 < b->u64 ? -1 : 1;
|
|
736
|
+
}
|
|
737
|
+
return 0;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/* Stable merge sort of indices by flat key (recursion is log2(n), like the
|
|
741
|
+
* deterministic map sort on the encode side). Stability keeps wire order
|
|
742
|
+
* inside an equal run, so the run's last countable member is the winner. */
|
|
743
|
+
static void
|
|
744
|
+
j_idx_sort(const j_flatkey *keys, size_t *idx, size_t *tmp, size_t n)
|
|
745
|
+
{
|
|
746
|
+
size_t mid;
|
|
747
|
+
size_t i;
|
|
748
|
+
size_t j;
|
|
749
|
+
size_t k;
|
|
750
|
+
|
|
751
|
+
if (n < 2) {
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
mid = n / 2;
|
|
755
|
+
j_idx_sort(keys, idx, tmp, mid);
|
|
756
|
+
j_idx_sort(keys, idx + mid, tmp + mid, n - mid);
|
|
757
|
+
i = 0;
|
|
758
|
+
j = mid;
|
|
759
|
+
k = 0;
|
|
760
|
+
while (i < mid && j < n) {
|
|
761
|
+
if (j_flatkey_cmp(&keys[idx[j]], &keys[idx[i]]) < 0) {
|
|
762
|
+
tmp[k++] = idx[j++];
|
|
763
|
+
} else {
|
|
764
|
+
tmp[k++] = idx[i++];
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
while (i < mid) {
|
|
768
|
+
tmp[k++] = idx[i++];
|
|
769
|
+
}
|
|
770
|
+
while (j < n) {
|
|
771
|
+
tmp[k++] = idx[j++];
|
|
772
|
+
}
|
|
773
|
+
memcpy(idx, tmp, n * sizeof(*idx));
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/* A list with items but no item array is corrupt: fail instead of reading
|
|
777
|
+
* NULL[0]. No producer builds one, but the text printer and the encoder
|
|
778
|
+
* already guard it, so the JSON printer does too. */
|
|
779
|
+
static int
|
|
780
|
+
j_list_items_ok(j_out *j, const pbw_value *list)
|
|
781
|
+
{
|
|
782
|
+
if (list->as.list.len > 0 && list->as.list.items == NULL) {
|
|
783
|
+
j->status = PBW_ERR_INVALID;
|
|
784
|
+
return 0;
|
|
785
|
+
}
|
|
786
|
+
return 1;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/* Marks entries shadowed by a later equal key in caller-owned, zeroed
|
|
790
|
+
* skip[]: 1 = keep the legacy loop (small maps and malformed input),
|
|
791
|
+
* -1 = out of memory, 0 = skip[] filled. */
|
|
792
|
+
static int
|
|
793
|
+
j_map_winners(const pbw_value *list, uint8_t *skip)
|
|
794
|
+
{
|
|
795
|
+
size_t n = list->as.list.len;
|
|
796
|
+
j_flatkey *keys;
|
|
797
|
+
size_t *idx;
|
|
798
|
+
size_t *tmp;
|
|
799
|
+
uint8_t *block;
|
|
800
|
+
size_t i;
|
|
801
|
+
|
|
802
|
+
if (n <= 32 || n > SIZE_MAX / (sizeof(*keys) + 2 * sizeof(*idx))) {
|
|
803
|
+
return 1;
|
|
804
|
+
}
|
|
805
|
+
block =
|
|
806
|
+
(uint8_t *)malloc(n * (sizeof(*keys) + 2 * sizeof(*idx)));
|
|
807
|
+
if (block == NULL) {
|
|
808
|
+
return -1;
|
|
809
|
+
}
|
|
810
|
+
keys = (j_flatkey *)(void *)block;
|
|
811
|
+
idx = (size_t *)(void *)(keys + n);
|
|
812
|
+
tmp = idx + n;
|
|
813
|
+
for (i = 0; i < n; i++) {
|
|
814
|
+
const pbw_value *item = list->as.list.items[i];
|
|
815
|
+
const pbw_value *key;
|
|
816
|
+
|
|
817
|
+
/* Malformed entries keep the exact legacy path (which reports them). */
|
|
818
|
+
if (item == NULL || item->kind != PBW_VALUE_MESSAGE ||
|
|
819
|
+
item->as.msg == NULL) {
|
|
820
|
+
free(block);
|
|
821
|
+
return 1;
|
|
822
|
+
}
|
|
823
|
+
key = pbw_message_get(item->as.msg, 1);
|
|
824
|
+
if (key != NULL && key->kind == PBW_VALUE_BYTES &&
|
|
825
|
+
!pbw_bytes_readable(key)) {
|
|
826
|
+
/* A payload that cannot exist: the legacy path below reports it
|
|
827
|
+
* with a status, before any sort compares key bytes. */
|
|
828
|
+
free(block);
|
|
829
|
+
return 1;
|
|
830
|
+
}
|
|
831
|
+
keys[i].has_key = key != NULL;
|
|
832
|
+
if (key == NULL) {
|
|
833
|
+
static const pbw_value zero = {PBW_VALUE_UINT, 0, {0}};
|
|
834
|
+
|
|
835
|
+
key = &zero;
|
|
836
|
+
}
|
|
837
|
+
keys[i].kind = (uint32_t)key->kind;
|
|
838
|
+
if (key->kind == PBW_VALUE_BYTES) {
|
|
839
|
+
keys[i].data = key->as.bytes.data;
|
|
840
|
+
keys[i].len = key->as.bytes.len;
|
|
841
|
+
keys[i].u64 = 0;
|
|
842
|
+
} else {
|
|
843
|
+
keys[i].data = NULL;
|
|
844
|
+
keys[i].len = 0;
|
|
845
|
+
keys[i].u64 = key->as.u64;
|
|
846
|
+
}
|
|
847
|
+
idx[i] = i;
|
|
848
|
+
}
|
|
849
|
+
j_idx_sort(keys, idx, tmp, n);
|
|
850
|
+
/* One pass over equal runs: everything before the run's last countable
|
|
851
|
+
* member is shadowed (a missing key never counts as the later dup). */
|
|
852
|
+
i = 0;
|
|
853
|
+
while (i < n) {
|
|
854
|
+
size_t e = i + 1;
|
|
855
|
+
size_t last = i;
|
|
856
|
+
size_t m;
|
|
857
|
+
|
|
858
|
+
while (e < n && j_flatkey_cmp(&keys[idx[e]], &keys[idx[i]]) == 0) {
|
|
859
|
+
e++;
|
|
860
|
+
}
|
|
861
|
+
for (m = i; m < e; m++) {
|
|
862
|
+
if (keys[idx[m]].has_key) {
|
|
863
|
+
last = m;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
if (keys[idx[last]].has_key) {
|
|
867
|
+
for (m = i; m < last; m++) {
|
|
868
|
+
skip[idx[m]] = 1;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
i = e;
|
|
872
|
+
}
|
|
873
|
+
free(block);
|
|
874
|
+
return 0;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
static void
|
|
878
|
+
j_print_map(j_out *j, const pbw_field *field, const pbw_value *list)
|
|
879
|
+
{
|
|
880
|
+
const pbw_descriptor *entry = field->sub;
|
|
881
|
+
const pbw_field *key_field = NULL;
|
|
882
|
+
const pbw_field *value_field = NULL;
|
|
883
|
+
size_t i;
|
|
884
|
+
|
|
885
|
+
if (entry != NULL) {
|
|
886
|
+
key_field = pbw_descriptor_find_field(entry, 1);
|
|
887
|
+
value_field = pbw_descriptor_find_field(entry, 2);
|
|
888
|
+
}
|
|
889
|
+
if (key_field == NULL || value_field == NULL) {
|
|
890
|
+
j->status = PBW_ERR_INVALID;
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
if (!j_list_items_ok(j, list)) {
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
/* Binary input may repeat a key (last wins); JSON objects cannot.
|
|
897
|
+
* Past a few dozen entries the per-item later-scan turns quadratic,
|
|
898
|
+
* so large maps precompute the shadowed entries once by sorting. */
|
|
899
|
+
uint8_t *skip = NULL;
|
|
900
|
+
if (list->as.list.len > 32) {
|
|
901
|
+
int winners;
|
|
902
|
+
|
|
903
|
+
skip = (uint8_t *)calloc(list->as.list.len, 1);
|
|
904
|
+
if (skip == NULL) {
|
|
905
|
+
j->status = PBW_ERR_NOMEM;
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
winners = j_map_winners(list, skip);
|
|
909
|
+
if (winners < 0) {
|
|
910
|
+
free(skip);
|
|
911
|
+
j->status = PBW_ERR_NOMEM;
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
if (winners > 0) {
|
|
915
|
+
/* Small or malformed input: the loop below scans as before. */
|
|
916
|
+
free(skip);
|
|
917
|
+
skip = NULL;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
int first = 1;
|
|
921
|
+
|
|
922
|
+
j_ch(j, '{');
|
|
923
|
+
for (i = 0; i < list->as.list.len; i++) {
|
|
924
|
+
const pbw_value *item = list->as.list.items[i];
|
|
925
|
+
const pbw_message *m;
|
|
926
|
+
const pbw_value *key;
|
|
927
|
+
const pbw_value *val;
|
|
928
|
+
size_t k;
|
|
929
|
+
int last = 1;
|
|
930
|
+
|
|
931
|
+
if (item == NULL || item->kind != PBW_VALUE_MESSAGE ||
|
|
932
|
+
item->as.msg == NULL || !pbw_msg_fresh(item->as.msg)) {
|
|
933
|
+
/* A retired entry (add_field/link after it was built) has no key
|
|
934
|
+
* or value to read: printing its defaults would silently drop it. */
|
|
935
|
+
j->status = PBW_ERR_INVALID;
|
|
936
|
+
free(skip);
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
/* Binary input may omit the key or the value; both default. */
|
|
940
|
+
m = item->as.msg;
|
|
941
|
+
key = pbw_message_get(m, 1);
|
|
942
|
+
val = pbw_message_get(m, 2);
|
|
943
|
+
if (key == NULL) {
|
|
944
|
+
static const pbw_value zero = {PBW_VALUE_UINT, 0, {0}};
|
|
945
|
+
|
|
946
|
+
key = &zero;
|
|
947
|
+
}
|
|
948
|
+
if (key->kind == PBW_VALUE_BYTES && !pbw_bytes_readable(key)) {
|
|
949
|
+
/* A key payload that cannot exist fails the print here, before
|
|
950
|
+
* the duplicate scan below compares it. */
|
|
951
|
+
j->status = PBW_ERR_INVALID;
|
|
952
|
+
free(skip);
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
if (val == NULL) {
|
|
956
|
+
static const pbw_value zero = {PBW_VALUE_UINT, 0, {0}};
|
|
957
|
+
static const pbw_value empty = {PBW_VALUE_BYTES, 0, {0}};
|
|
958
|
+
|
|
959
|
+
if (value_field->type != PBW_TYPE_MESSAGE &&
|
|
960
|
+
value_field->type != PBW_TYPE_GROUP) {
|
|
961
|
+
if (value_field->type == PBW_TYPE_STRING ||
|
|
962
|
+
value_field->type == PBW_TYPE_BYTES) {
|
|
963
|
+
val = ∅
|
|
964
|
+
} else {
|
|
965
|
+
val = &zero;
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
/* message values left NULL print as {} below */
|
|
969
|
+
}
|
|
970
|
+
if (skip != NULL) {
|
|
971
|
+
last = !skip[i];
|
|
972
|
+
} else {
|
|
973
|
+
for (k = i + 1; k < list->as.list.len; k++) {
|
|
974
|
+
const pbw_value *later = list->as.list.items[k];
|
|
975
|
+
const pbw_value *later_key;
|
|
976
|
+
|
|
977
|
+
if (later == NULL || later->kind != PBW_VALUE_MESSAGE ||
|
|
978
|
+
later->as.msg == NULL) {
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
later_key = pbw_message_get(later->as.msg, 1);
|
|
982
|
+
if (later_key == NULL || later_key->kind != key->kind) {
|
|
983
|
+
continue;
|
|
984
|
+
}
|
|
985
|
+
if (key->kind == PBW_VALUE_BYTES) {
|
|
986
|
+
if (later_key->as.bytes.len == key->as.bytes.len &&
|
|
987
|
+
pbw_bytes_readable(later_key) &&
|
|
988
|
+
memcmp(later_key->as.bytes.data, key->as.bytes.data,
|
|
989
|
+
key->as.bytes.len) == 0) {
|
|
990
|
+
last = 0;
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
} else if (later_key->as.u64 == key->as.u64) {
|
|
994
|
+
last = 0;
|
|
995
|
+
break;
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
if (!last) {
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
if (val != NULL && value_field->type == PBW_TYPE_MESSAGE &&
|
|
1003
|
+
val->kind == PBW_VALUE_MESSAGE && val->as.msg != NULL &&
|
|
1004
|
+
j_classify(val->as.msg->desc) == J_VALUE &&
|
|
1005
|
+
j_value_is_empty(val->as.msg)) {
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
if (!first) {
|
|
1009
|
+
j_ch(j, ',');
|
|
1010
|
+
}
|
|
1011
|
+
first = 0;
|
|
1012
|
+
j_print_map_key(j, key_field, key);
|
|
1013
|
+
j_ch(j, ':');
|
|
1014
|
+
if (val == NULL) {
|
|
1015
|
+
j_str(j, "{}");
|
|
1016
|
+
} else {
|
|
1017
|
+
j_print_scalar(j, value_field, val);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
free(skip);
|
|
1021
|
+
j_ch(j, '}');
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
static void
|
|
1025
|
+
j_print_repeated(j_out *j, const pbw_field *field, const pbw_value *list)
|
|
1026
|
+
{
|
|
1027
|
+
size_t i;
|
|
1028
|
+
int first = 1;
|
|
1029
|
+
|
|
1030
|
+
if (field->type == PBW_TYPE_MESSAGE && field->sub != NULL &&
|
|
1031
|
+
field->sub->map_entry) {
|
|
1032
|
+
j_print_map(j, field, list);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
if (!j_list_items_ok(j, list)) {
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
j_ch(j, '[');
|
|
1039
|
+
for (i = 0; i < list->as.list.len; i++) {
|
|
1040
|
+
const pbw_value *item = list->as.list.items[i];
|
|
1041
|
+
|
|
1042
|
+
if (item == NULL) {
|
|
1043
|
+
j->status = PBW_ERR_INVALID;
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
if (field->type == PBW_TYPE_MESSAGE && field->sub != NULL) {
|
|
1047
|
+
if (item->kind == PBW_VALUE_MESSAGE && item->as.msg != NULL &&
|
|
1048
|
+
j_classify(item->as.msg->desc) == J_VALUE &&
|
|
1049
|
+
j_value_is_empty(item->as.msg)) {
|
|
1050
|
+
continue;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
if (!first) {
|
|
1054
|
+
j_ch(j, ',');
|
|
1055
|
+
}
|
|
1056
|
+
first = 0;
|
|
1057
|
+
j_print_scalar(j, field, item);
|
|
1058
|
+
}
|
|
1059
|
+
j_ch(j, ']');
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/* "1970-01-01T00:00:00Z" with 0/3/6/9 fractional digits. */
|
|
1063
|
+
static void
|
|
1064
|
+
j_print_timestamp(j_out *j, const pbw_message *msg)
|
|
1065
|
+
{
|
|
1066
|
+
const pbw_value *secs_v = pbw_message_get(msg, 1);
|
|
1067
|
+
const pbw_value *nanos_v = pbw_message_get(msg, 2);
|
|
1068
|
+
int64_t secs = secs_v != NULL && secs_v->kind == PBW_VALUE_UINT
|
|
1069
|
+
? (int64_t)secs_v->as.u64
|
|
1070
|
+
: 0;
|
|
1071
|
+
int32_t nanos = nanos_v != NULL && nanos_v->kind == PBW_VALUE_UINT
|
|
1072
|
+
? (int32_t)nanos_v->as.u64
|
|
1073
|
+
: 0;
|
|
1074
|
+
/* Julian Day -> Y/M/D, the same algorithm as the reference printer. */
|
|
1075
|
+
int32_t l;
|
|
1076
|
+
int32_t n;
|
|
1077
|
+
int32_t k;
|
|
1078
|
+
int32_t jj;
|
|
1079
|
+
int32_t i;
|
|
1080
|
+
int32_t year;
|
|
1081
|
+
int32_t month;
|
|
1082
|
+
int32_t day;
|
|
1083
|
+
int32_t hour;
|
|
1084
|
+
int32_t minute;
|
|
1085
|
+
int32_t second;
|
|
1086
|
+
uint32_t frac = 0;
|
|
1087
|
+
int digits = 0;
|
|
1088
|
+
char tmp[40];
|
|
1089
|
+
int len;
|
|
1090
|
+
|
|
1091
|
+
if (secs < -62135596800ll || secs > 253402300799ll || nanos < 0 ||
|
|
1092
|
+
nanos > 999999999) {
|
|
1093
|
+
j->status = PBW_ERR_INVALID;
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
secs += 62135596800ll;
|
|
1097
|
+
l = (int32_t)(secs / 86400) - 719162 + 68569 + 2440588;
|
|
1098
|
+
n = 4 * l / 146097;
|
|
1099
|
+
l = l - (146097 * n + 3) / 4;
|
|
1100
|
+
i = 4000 * (l + 1) / 1461001;
|
|
1101
|
+
l = l - 1461 * i / 4 + 31;
|
|
1102
|
+
jj = 80 * l / 2447;
|
|
1103
|
+
k = l - 2447 * jj / 80;
|
|
1104
|
+
l = jj / 11;
|
|
1105
|
+
jj = jj + 2 - 12 * l;
|
|
1106
|
+
i = 100 * (n - 49) + i + l;
|
|
1107
|
+
year = i;
|
|
1108
|
+
month = jj;
|
|
1109
|
+
day = k;
|
|
1110
|
+
hour = (int32_t)((secs / 3600) % 24);
|
|
1111
|
+
minute = (int32_t)((secs / 60) % 60);
|
|
1112
|
+
second = (int32_t)(secs % 60);
|
|
1113
|
+
if (nanos != 0) {
|
|
1114
|
+
digits = 9;
|
|
1115
|
+
frac = (uint32_t)nanos;
|
|
1116
|
+
while (frac % 1000 == 0) {
|
|
1117
|
+
frac /= 1000;
|
|
1118
|
+
digits -= 3;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
if (digits == 0) {
|
|
1122
|
+
len = snprintf(tmp, sizeof(tmp),
|
|
1123
|
+
"\"%04d-%02d-%02dT%02d:%02d:%02dZ\"", year, month, day,
|
|
1124
|
+
hour, minute, second);
|
|
1125
|
+
} else {
|
|
1126
|
+
len = snprintf(tmp, sizeof(tmp),
|
|
1127
|
+
"\"%04d-%02d-%02dT%02d:%02d:%02d.%.*uZ\"", year, month,
|
|
1128
|
+
day, hour, minute, second, digits, frac);
|
|
1129
|
+
}
|
|
1130
|
+
if (len <= 0 || (size_t)len >= sizeof(tmp)) {
|
|
1131
|
+
j->status = PBW_ERR_INVALID;
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
j_put(j, tmp, (size_t)len);
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
static void
|
|
1138
|
+
j_print_duration(j_out *j, const pbw_message *msg)
|
|
1139
|
+
{
|
|
1140
|
+
const pbw_value *secs_v = pbw_message_get(msg, 1);
|
|
1141
|
+
const pbw_value *nanos_v = pbw_message_get(msg, 2);
|
|
1142
|
+
int64_t secs = secs_v != NULL && secs_v->kind == PBW_VALUE_UINT
|
|
1143
|
+
? (int64_t)secs_v->as.u64
|
|
1144
|
+
: 0;
|
|
1145
|
+
int32_t nanos = nanos_v != NULL && nanos_v->kind == PBW_VALUE_UINT
|
|
1146
|
+
? (int32_t)nanos_v->as.u64
|
|
1147
|
+
: 0;
|
|
1148
|
+
const int64_t max = (int64_t)3652500 * 86400;
|
|
1149
|
+
char tmp[48];
|
|
1150
|
+
int len;
|
|
1151
|
+
|
|
1152
|
+
if (secs > max || secs < -max || nanos > 999999999 || nanos < -999999999 ||
|
|
1153
|
+
((secs != 0) && (nanos != 0) && ((secs < 0) != (nanos < 0)))) {
|
|
1154
|
+
j->status = PBW_ERR_INVALID;
|
|
1155
|
+
return;
|
|
1156
|
+
}
|
|
1157
|
+
if (nanos == 0) {
|
|
1158
|
+
len = snprintf(tmp, sizeof(tmp), "\"%lld%s\"", (long long)secs, "s");
|
|
1159
|
+
} else {
|
|
1160
|
+
uint32_t frac = (uint32_t)(nanos < 0 ? -nanos : nanos);
|
|
1161
|
+
int digits = 9;
|
|
1162
|
+
const char *sign = (secs < 0 || nanos < 0) ? "-" : "";
|
|
1163
|
+
int64_t a = secs < 0 ? -secs : secs;
|
|
1164
|
+
|
|
1165
|
+
while (frac % 1000 == 0) {
|
|
1166
|
+
frac /= 1000;
|
|
1167
|
+
digits -= 3;
|
|
1168
|
+
}
|
|
1169
|
+
len = snprintf(tmp, sizeof(tmp), "\"%s%lld.%.*us\"", sign,
|
|
1170
|
+
(long long)a, digits, frac);
|
|
1171
|
+
}
|
|
1172
|
+
if (len <= 0 || (size_t)len >= sizeof(tmp)) {
|
|
1173
|
+
j->status = PBW_ERR_INVALID;
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
j_put(j, tmp, (size_t)len);
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/* Paths print camelCase (`foo_bar` -> `fooBar`), joined with commas. */
|
|
1180
|
+
static void
|
|
1181
|
+
j_print_fieldmask(j_out *j, const pbw_message *msg)
|
|
1182
|
+
{
|
|
1183
|
+
const pbw_value *paths = pbw_message_get(msg, 1);
|
|
1184
|
+
size_t i;
|
|
1185
|
+
|
|
1186
|
+
j_ch(j, '"');
|
|
1187
|
+
if (paths != NULL && paths->kind == PBW_VALUE_LIST) {
|
|
1188
|
+
if (!j_list_items_ok(j, paths)) {
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
for (i = 0; i < paths->as.list.len; i++) {
|
|
1192
|
+
const pbw_value *item = paths->as.list.items[i];
|
|
1193
|
+
const uint8_t *p;
|
|
1194
|
+
size_t len;
|
|
1195
|
+
size_t k;
|
|
1196
|
+
int saw_under = 0;
|
|
1197
|
+
|
|
1198
|
+
if (item == NULL || item->kind != PBW_VALUE_BYTES ||
|
|
1199
|
+
!pbw_bytes_readable(item)) {
|
|
1200
|
+
j->status = PBW_ERR_INVALID;
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
if (i > 0) {
|
|
1204
|
+
j_ch(j, ',');
|
|
1205
|
+
}
|
|
1206
|
+
p = item->as.bytes.data;
|
|
1207
|
+
len = item->as.bytes.len;
|
|
1208
|
+
for (k = 0; k < len; k++) {
|
|
1209
|
+
unsigned char c = p[k];
|
|
1210
|
+
|
|
1211
|
+
if (c >= 'a' && c <= 'z' && saw_under) {
|
|
1212
|
+
j_ch(j, (char)(c - 'a' + 'A'));
|
|
1213
|
+
} else if ((c >= 'a' && c <= 'z') ||
|
|
1214
|
+
(c >= '0' && c <= '9') || c == '.') {
|
|
1215
|
+
j_ch(j, (char)c);
|
|
1216
|
+
} else if (c == '_') {
|
|
1217
|
+
/* legacy mode is always on: keep swallowing */
|
|
1218
|
+
saw_under = 1;
|
|
1219
|
+
continue;
|
|
1220
|
+
} else {
|
|
1221
|
+
if (saw_under) {
|
|
1222
|
+
j_ch(j, '_');
|
|
1223
|
+
}
|
|
1224
|
+
j_ch(j, (char)c);
|
|
1225
|
+
}
|
|
1226
|
+
saw_under = 0;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
j_ch(j, '"');
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
static void
|
|
1234
|
+
j_print_struct_value(j_out *j, const pbw_message *msg);
|
|
1235
|
+
static void
|
|
1236
|
+
j_print_list_value(j_out *j, const pbw_message *msg);
|
|
1237
|
+
static void
|
|
1238
|
+
j_print_value_body(j_out *j, const pbw_message *msg);
|
|
1239
|
+
|
|
1240
|
+
/* Value -> Struct -> Value and Value -> ListValue -> Value recurse through
|
|
1241
|
+
* message fields the depth-guarded printer path does not walk field by field,
|
|
1242
|
+
* so a hand-built deep chain is capped here instead of overflowing the stack. */
|
|
1243
|
+
static void
|
|
1244
|
+
j_print_value(j_out *j, const pbw_message *msg)
|
|
1245
|
+
{
|
|
1246
|
+
if (j == NULL || j->status != PBW_OK) {
|
|
1247
|
+
return;
|
|
1248
|
+
}
|
|
1249
|
+
if (msg == NULL || msg->desc == NULL) {
|
|
1250
|
+
j->status = PBW_ERR_INVALID;
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
if (j->depth >= PBW_MAX_DEPTH) {
|
|
1254
|
+
j->status = PBW_ERR_LIMIT;
|
|
1255
|
+
return;
|
|
1256
|
+
}
|
|
1257
|
+
j->depth++;
|
|
1258
|
+
j_print_value_body(j, msg);
|
|
1259
|
+
j->depth--;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/* google.protobuf.Value: exactly one of its six fields is set. An empty
|
|
1263
|
+
* Value never reaches here (the caller skips it); NaN/Inf fail. */
|
|
1264
|
+
static void
|
|
1265
|
+
j_print_value_body(j_out *j, const pbw_message *msg)
|
|
1266
|
+
{
|
|
1267
|
+
const pbw_value *v;
|
|
1268
|
+
|
|
1269
|
+
v = pbw_message_get(msg, 1);
|
|
1270
|
+
if (v != NULL) {
|
|
1271
|
+
j_str(j, "null");
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1274
|
+
v = pbw_message_get(msg, 2);
|
|
1275
|
+
if (v != NULL && v->kind == PBW_VALUE_UINT) {
|
|
1276
|
+
double d;
|
|
1277
|
+
|
|
1278
|
+
memcpy(&d, &v->as.u64, sizeof(d));
|
|
1279
|
+
if (isnan(d) || isinf(d)) {
|
|
1280
|
+
j->status = PBW_ERR_INVALID;
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
j_double(j, d, 0);
|
|
1284
|
+
return;
|
|
1285
|
+
}
|
|
1286
|
+
v = pbw_message_get(msg, 3);
|
|
1287
|
+
if (v != NULL && v->kind == PBW_VALUE_BYTES) {
|
|
1288
|
+
j_quoted(j, v->as.bytes.data, v->as.bytes.len);
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
v = pbw_message_get(msg, 4);
|
|
1292
|
+
if (v != NULL) {
|
|
1293
|
+
j_str(j, v->kind == PBW_VALUE_UINT && v->as.u64 != 0 ? "true"
|
|
1294
|
+
: "false");
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
v = pbw_message_get(msg, 5);
|
|
1298
|
+
if (v != NULL && v->kind == PBW_VALUE_MESSAGE && v->as.msg != NULL) {
|
|
1299
|
+
j_print_struct_value(j, v->as.msg);
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
v = pbw_message_get(msg, 6);
|
|
1303
|
+
if (v != NULL && v->kind == PBW_VALUE_MESSAGE && v->as.msg != NULL) {
|
|
1304
|
+
j_print_list_value(j, v->as.msg);
|
|
1305
|
+
return;
|
|
1306
|
+
}
|
|
1307
|
+
j->status = PBW_ERR_INVALID;
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
static void
|
|
1311
|
+
j_print_struct_value(j_out *j, const pbw_message *msg)
|
|
1312
|
+
{
|
|
1313
|
+
const pbw_value *fields;
|
|
1314
|
+
|
|
1315
|
+
if (j == NULL || j->status != PBW_OK || msg == NULL ||
|
|
1316
|
+
msg->desc == NULL) {
|
|
1317
|
+
if (j != NULL && j->status == PBW_OK) {
|
|
1318
|
+
j->status = PBW_ERR_INVALID;
|
|
1319
|
+
}
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
if (!pbw_msg_fresh(msg)) {
|
|
1323
|
+
/* Struct/ListValue print their body here instead of going through
|
|
1324
|
+
* j_print_message(), so they need the same stale rule: a retired
|
|
1325
|
+
* inner message must fail, not print as an empty {} / []. */
|
|
1326
|
+
j->status = PBW_ERR_INVALID;
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
fields = pbw_message_get(msg, 1);
|
|
1330
|
+
size_t i;
|
|
1331
|
+
size_t k;
|
|
1332
|
+
int first = 1;
|
|
1333
|
+
|
|
1334
|
+
j_ch(j, '{');
|
|
1335
|
+
if (fields != NULL && fields->kind == PBW_VALUE_LIST) {
|
|
1336
|
+
if (!j_list_items_ok(j, fields)) {
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
for (i = 0; i < fields->as.list.len; i++) {
|
|
1340
|
+
const pbw_value *item = fields->as.list.items[i];
|
|
1341
|
+
const pbw_message *m;
|
|
1342
|
+
const pbw_value *key;
|
|
1343
|
+
const pbw_value *val;
|
|
1344
|
+
int last = 1;
|
|
1345
|
+
|
|
1346
|
+
if (item == NULL || item->kind != PBW_VALUE_MESSAGE ||
|
|
1347
|
+
item->as.msg == NULL) {
|
|
1348
|
+
j->status = PBW_ERR_INVALID;
|
|
1349
|
+
return;
|
|
1350
|
+
}
|
|
1351
|
+
m = item->as.msg;
|
|
1352
|
+
key = pbw_message_get(m, 1);
|
|
1353
|
+
val = pbw_message_get(m, 2);
|
|
1354
|
+
if (key == NULL || key->kind != PBW_VALUE_BYTES ||
|
|
1355
|
+
val == NULL || !pbw_bytes_readable(key)) {
|
|
1356
|
+
j->status = PBW_ERR_INVALID;
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
/* Binary input may repeat a key: only the last one prints,
|
|
1360
|
+
* like j_print_map does for map fields. */
|
|
1361
|
+
for (k = i + 1; k < fields->as.list.len; k++) {
|
|
1362
|
+
const pbw_value *later = fields->as.list.items[k];
|
|
1363
|
+
const pbw_value *later_key;
|
|
1364
|
+
|
|
1365
|
+
if (later == NULL || later->kind != PBW_VALUE_MESSAGE ||
|
|
1366
|
+
later->as.msg == NULL) {
|
|
1367
|
+
continue;
|
|
1368
|
+
}
|
|
1369
|
+
later_key = pbw_message_get(later->as.msg, 1);
|
|
1370
|
+
if (later_key != NULL &&
|
|
1371
|
+
later_key->kind == PBW_VALUE_BYTES &&
|
|
1372
|
+
later_key->as.bytes.len == key->as.bytes.len &&
|
|
1373
|
+
pbw_bytes_readable(later_key) &&
|
|
1374
|
+
memcmp(later_key->as.bytes.data, key->as.bytes.data,
|
|
1375
|
+
key->as.bytes.len) == 0) {
|
|
1376
|
+
last = 0;
|
|
1377
|
+
break;
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
if (!last) {
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
if (val->kind == PBW_VALUE_MESSAGE && val->as.msg != NULL &&
|
|
1384
|
+
j_classify(val->as.msg->desc) == J_VALUE &&
|
|
1385
|
+
j_value_is_empty(val->as.msg)) {
|
|
1386
|
+
continue;
|
|
1387
|
+
}
|
|
1388
|
+
if (!first) {
|
|
1389
|
+
j_ch(j, ',');
|
|
1390
|
+
}
|
|
1391
|
+
first = 0;
|
|
1392
|
+
/* Struct keys are arbitrary strings, not field names. */
|
|
1393
|
+
j_quoted(j, key->as.bytes.data, key->as.bytes.len);
|
|
1394
|
+
j_ch(j, ':');
|
|
1395
|
+
if (val->kind == PBW_VALUE_MESSAGE && val->as.msg != NULL) {
|
|
1396
|
+
j_print_value(j, val->as.msg);
|
|
1397
|
+
} else {
|
|
1398
|
+
j->status = PBW_ERR_INVALID;
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
j_ch(j, '}');
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
static void
|
|
1407
|
+
j_print_list_value(j_out *j, const pbw_message *msg)
|
|
1408
|
+
{
|
|
1409
|
+
const pbw_value *values;
|
|
1410
|
+
|
|
1411
|
+
if (j == NULL || j->status != PBW_OK || msg == NULL ||
|
|
1412
|
+
msg->desc == NULL) {
|
|
1413
|
+
if (j != NULL && j->status == PBW_OK) {
|
|
1414
|
+
j->status = PBW_ERR_INVALID;
|
|
1415
|
+
}
|
|
1416
|
+
return;
|
|
1417
|
+
}
|
|
1418
|
+
if (!pbw_msg_fresh(msg)) {
|
|
1419
|
+
/* Struct/ListValue print their body here instead of going through
|
|
1420
|
+
* j_print_message(), so they need the same stale rule: a retired
|
|
1421
|
+
* inner message must fail, not print as an empty {} / []. */
|
|
1422
|
+
j->status = PBW_ERR_INVALID;
|
|
1423
|
+
return;
|
|
1424
|
+
}
|
|
1425
|
+
values = pbw_message_get(msg, 1);
|
|
1426
|
+
|
|
1427
|
+
j_ch(j, '[');
|
|
1428
|
+
if (values != NULL && values->kind == PBW_VALUE_LIST) {
|
|
1429
|
+
if (!j_list_items_ok(j, values)) {
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
size_t i;
|
|
1433
|
+
int first = 1;
|
|
1434
|
+
|
|
1435
|
+
for (i = 0; i < values->as.list.len; i++) {
|
|
1436
|
+
const pbw_value *item = values->as.list.items[i];
|
|
1437
|
+
|
|
1438
|
+
if (item == NULL || item->kind != PBW_VALUE_MESSAGE ||
|
|
1439
|
+
item->as.msg == NULL) {
|
|
1440
|
+
j->status = PBW_ERR_INVALID;
|
|
1441
|
+
return;
|
|
1442
|
+
}
|
|
1443
|
+
if (j_classify(item->as.msg->desc) == J_VALUE &&
|
|
1444
|
+
j_value_is_empty(item->as.msg)) {
|
|
1445
|
+
continue;
|
|
1446
|
+
}
|
|
1447
|
+
if (!first) {
|
|
1448
|
+
j_ch(j, ',');
|
|
1449
|
+
}
|
|
1450
|
+
first = 0;
|
|
1451
|
+
j_print_value(j, item->as.msg);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
j_ch(j, ']');
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
/* google.protobuf.Any: {"@type": url} plus the expanded payload, or just
|
|
1458
|
+
* the type for well-known payloads under a "value" key. */
|
|
1459
|
+
static void
|
|
1460
|
+
j_print_any(j_out *j, const pbw_message *msg)
|
|
1461
|
+
{
|
|
1462
|
+
const pbw_value *url_v = pbw_message_get(msg, 1);
|
|
1463
|
+
const pbw_value *bytes_v = pbw_message_get(msg, 2);
|
|
1464
|
+
const uint8_t *url = NULL;
|
|
1465
|
+
size_t url_len = 0;
|
|
1466
|
+
const uint8_t *payload = NULL;
|
|
1467
|
+
size_t payload_len = 0;
|
|
1468
|
+
const char *slash;
|
|
1469
|
+
const pbw_descriptor *resolved;
|
|
1470
|
+
pbw_message *decoded = NULL;
|
|
1471
|
+
|
|
1472
|
+
if (url_v == NULL) {
|
|
1473
|
+
if (bytes_v != NULL) {
|
|
1474
|
+
j->status = PBW_ERR_INVALID;
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
j_str(j, "{}");
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
if (url_v->kind == PBW_VALUE_BYTES) {
|
|
1481
|
+
if (!pbw_bytes_readable(url_v)) {
|
|
1482
|
+
/* The type name is scanned below, before any quoting. */
|
|
1483
|
+
j->status = PBW_ERR_INVALID;
|
|
1484
|
+
return;
|
|
1485
|
+
}
|
|
1486
|
+
url = url_v->as.bytes.data;
|
|
1487
|
+
url_len = url_v->as.bytes.len;
|
|
1488
|
+
}
|
|
1489
|
+
if (bytes_v != NULL && bytes_v->kind == PBW_VALUE_BYTES) {
|
|
1490
|
+
payload = bytes_v->as.bytes.data;
|
|
1491
|
+
payload_len = bytes_v->as.bytes.len;
|
|
1492
|
+
}
|
|
1493
|
+
/* The type name is whatever follows the last '/'. */
|
|
1494
|
+
slash = NULL;
|
|
1495
|
+
{
|
|
1496
|
+
size_t i;
|
|
1497
|
+
|
|
1498
|
+
for (i = 0; i < url_len; i++) {
|
|
1499
|
+
if (url[i] == '/') {
|
|
1500
|
+
slash = (const char *)url + i;
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
{
|
|
1505
|
+
/* Room for the longest message name the loader accepts (512). */
|
|
1506
|
+
char name[520];
|
|
1507
|
+
size_t off = slash == NULL ? 0 : (size_t)(slash - (const char *)url) + 1;
|
|
1508
|
+
size_t n = url_len - off;
|
|
1509
|
+
|
|
1510
|
+
if (n >= sizeof(name)) {
|
|
1511
|
+
j->status = PBW_ERR_INVALID;
|
|
1512
|
+
return;
|
|
1513
|
+
}
|
|
1514
|
+
memcpy(name, url + off, n);
|
|
1515
|
+
name[n] = '\0';
|
|
1516
|
+
resolved = msg->desc->schema == NULL
|
|
1517
|
+
? NULL
|
|
1518
|
+
: pbw_schema_find(msg->desc->schema, name);
|
|
1519
|
+
}
|
|
1520
|
+
if (resolved == NULL || resolved->is_enum) {
|
|
1521
|
+
j->status = PBW_ERR_INVALID;
|
|
1522
|
+
return;
|
|
1523
|
+
}
|
|
1524
|
+
if (pbw_decode(resolved, payload, payload_len, &decoded) != PBW_OK) {
|
|
1525
|
+
/* The payload is peer data, not this process's memory: report it as
|
|
1526
|
+
* invalid instead of claiming the host ran out of memory. */
|
|
1527
|
+
j->status = PBW_ERR_INVALID;
|
|
1528
|
+
return;
|
|
1529
|
+
}
|
|
1530
|
+
j_str(j, "{\"@type\":");
|
|
1531
|
+
j_quoted(j, url, url_len);
|
|
1532
|
+
/* Empty is not special inside Any: it expands inline (to nothing). */
|
|
1533
|
+
{
|
|
1534
|
+
j_wkt wkt = j_classify(resolved);
|
|
1535
|
+
|
|
1536
|
+
if (wkt != J_NOT_WKT && wkt != J_EMPTY) {
|
|
1537
|
+
j_str(j, ",\"value\":");
|
|
1538
|
+
j_print_message(j, decoded);
|
|
1539
|
+
} else if (!j_value_is_empty(decoded)) {
|
|
1540
|
+
/* Regular payloads expand inline: {"a":1} becomes ,"a":1. */
|
|
1541
|
+
size_t mark = j->buf.len;
|
|
1542
|
+
|
|
1543
|
+
j_print_message(j, decoded);
|
|
1544
|
+
if (j->status == PBW_OK && j->buf.len >= mark + 2 &&
|
|
1545
|
+
j->buf.data[mark] == '{' &&
|
|
1546
|
+
j->buf.data[j->buf.len - 1] == '}') {
|
|
1547
|
+
j->buf.data[mark] = ',';
|
|
1548
|
+
j->buf.len--;
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
pbw_message_free(decoded);
|
|
1553
|
+
j_ch(j, '}');
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
/* One field of an object body, with the separating comma. */
|
|
1557
|
+
static void
|
|
1558
|
+
j_print_one(j_out *j, const pbw_message *msg, const pbw_field *field,
|
|
1559
|
+
int *first)
|
|
1560
|
+
{
|
|
1561
|
+
/* "[scope.name]" of an extension: the loader caps scope and name at 512
|
|
1562
|
+
* bytes each, plus the brackets. */
|
|
1563
|
+
char key[2 * 512 + 4];
|
|
1564
|
+
const pbw_value *value = &msg->values[(size_t)(field - msg->desc->fields)];
|
|
1565
|
+
|
|
1566
|
+
if (!*first) {
|
|
1567
|
+
j_ch(j, ',');
|
|
1568
|
+
}
|
|
1569
|
+
*first = 0;
|
|
1570
|
+
if (field->is_extension) {
|
|
1571
|
+
const char *full =
|
|
1572
|
+
field->full_name != NULL ? field->full_name : field->name;
|
|
1573
|
+
|
|
1574
|
+
if (strlen(full) + 3 > sizeof(key)) {
|
|
1575
|
+
j->status = PBW_ERR_INVALID;
|
|
1576
|
+
return;
|
|
1577
|
+
}
|
|
1578
|
+
key[0] = '[';
|
|
1579
|
+
memcpy(key + 1, full, strlen(full));
|
|
1580
|
+
key[strlen(full) + 1] = ']';
|
|
1581
|
+
key[strlen(full) + 2] = '\0';
|
|
1582
|
+
j_quoted(j, (const uint8_t *)key, strlen(key));
|
|
1583
|
+
} else {
|
|
1584
|
+
j_print_name(field, key, sizeof(key));
|
|
1585
|
+
j_quoted(j, (const uint8_t *)key, strlen(key));
|
|
1586
|
+
}
|
|
1587
|
+
j_ch(j, ':');
|
|
1588
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
1589
|
+
if (value->kind == PBW_VALUE_LIST) {
|
|
1590
|
+
j_print_repeated(j, field, value);
|
|
1591
|
+
} else {
|
|
1592
|
+
j->status = PBW_ERR_INVALID;
|
|
1593
|
+
}
|
|
1594
|
+
} else {
|
|
1595
|
+
j_print_scalar(j, field, value);
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
static void
|
|
1600
|
+
j_print_fields(j_out *j, const pbw_message *msg)
|
|
1601
|
+
{
|
|
1602
|
+
/* Fields print in field-number order, extensions interleaved. */
|
|
1603
|
+
const pbw_field **order = NULL;
|
|
1604
|
+
size_t count = 0;
|
|
1605
|
+
size_t capacity = 0;
|
|
1606
|
+
size_t i;
|
|
1607
|
+
size_t k;
|
|
1608
|
+
int first = 1;
|
|
1609
|
+
|
|
1610
|
+
if (msg->desc->schema != NULL && msg->desc->schema->linked) {
|
|
1611
|
+
/* link_message() already sorted the fields by number: print them in
|
|
1612
|
+
* place, with no per-message order array and no malloc. A printed tree
|
|
1613
|
+
* has one message per element, so this is the common case. */
|
|
1614
|
+
for (i = 0; i < msg->desc->field_count; i++) {
|
|
1615
|
+
if (!j_should_print(msg, i)) {
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
j_print_one(j, msg, &msg->desc->fields[i], &first);
|
|
1619
|
+
if (j->status != PBW_OK) {
|
|
1620
|
+
return;
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
return;
|
|
1624
|
+
}
|
|
1625
|
+
for (i = 0; i < msg->desc->field_count; i++) {
|
|
1626
|
+
if (j_should_print(msg, i)) {
|
|
1627
|
+
count++;
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
capacity = count;
|
|
1631
|
+
if (capacity > 0) {
|
|
1632
|
+
order = (const pbw_field **)malloc(capacity * sizeof(*order));
|
|
1633
|
+
if (order == NULL) {
|
|
1634
|
+
j->status = PBW_ERR_NOMEM;
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
count = 0;
|
|
1639
|
+
for (i = 0; i < msg->desc->field_count; i++) {
|
|
1640
|
+
if (!j_should_print(msg, i)) {
|
|
1641
|
+
continue;
|
|
1642
|
+
}
|
|
1643
|
+
if (count >= capacity) {
|
|
1644
|
+
break; /* the two passes disagree: never write past the array */
|
|
1645
|
+
}
|
|
1646
|
+
order[count] = &msg->desc->fields[i];
|
|
1647
|
+
/* insertion sort by number; a hand-built schema is not sorted yet */
|
|
1648
|
+
k = count;
|
|
1649
|
+
while (k > 0 && order[k - 1]->number > order[k]->number) {
|
|
1650
|
+
const pbw_field *tmp = order[k - 1];
|
|
1651
|
+
|
|
1652
|
+
order[k - 1] = order[k];
|
|
1653
|
+
order[k] = tmp;
|
|
1654
|
+
k--;
|
|
1655
|
+
}
|
|
1656
|
+
count++;
|
|
1657
|
+
}
|
|
1658
|
+
for (i = 0; i < count; i++) {
|
|
1659
|
+
j_print_one(j, msg, order[i], &first);
|
|
1660
|
+
if (j->status != PBW_OK) {
|
|
1661
|
+
break;
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
free(order);
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
static void
|
|
1668
|
+
j_print_message_body(j_out *j, const pbw_message *msg)
|
|
1669
|
+
{
|
|
1670
|
+
if (j == NULL || j->status != PBW_OK || msg == NULL ||
|
|
1671
|
+
msg->desc == NULL) {
|
|
1672
|
+
if (j != NULL && j->status == PBW_OK) {
|
|
1673
|
+
j->status = PBW_ERR_INVALID;
|
|
1674
|
+
}
|
|
1675
|
+
return;
|
|
1676
|
+
}
|
|
1677
|
+
if (msg->desc->field_count > 0 &&
|
|
1678
|
+
(msg->values == NULL || msg->present == NULL)) {
|
|
1679
|
+
j->status = PBW_ERR_INVALID;
|
|
1680
|
+
return;
|
|
1681
|
+
}
|
|
1682
|
+
switch (j_classify(msg->desc)) {
|
|
1683
|
+
case J_ANY:
|
|
1684
|
+
j_print_any(j, msg);
|
|
1685
|
+
return;
|
|
1686
|
+
case J_TIMESTAMP:
|
|
1687
|
+
j_print_timestamp(j, msg);
|
|
1688
|
+
return;
|
|
1689
|
+
case J_DURATION:
|
|
1690
|
+
j_print_duration(j, msg);
|
|
1691
|
+
return;
|
|
1692
|
+
case J_FIELDMASK:
|
|
1693
|
+
j_print_fieldmask(j, msg);
|
|
1694
|
+
return;
|
|
1695
|
+
case J_STRUCT:
|
|
1696
|
+
j_print_struct_value(j, msg);
|
|
1697
|
+
return;
|
|
1698
|
+
case J_LIST:
|
|
1699
|
+
j_print_list_value(j, msg);
|
|
1700
|
+
return;
|
|
1701
|
+
case J_VALUE:
|
|
1702
|
+
j_print_value(j, msg);
|
|
1703
|
+
return;
|
|
1704
|
+
case J_WRAPPER: {
|
|
1705
|
+
const pbw_field *inner = pbw_descriptor_find_field(msg->desc, 1);
|
|
1706
|
+
const pbw_value *v = pbw_message_get(msg, 1);
|
|
1707
|
+
|
|
1708
|
+
if (inner == NULL) {
|
|
1709
|
+
j->status = PBW_ERR_INVALID;
|
|
1710
|
+
return;
|
|
1711
|
+
}
|
|
1712
|
+
if (v == NULL) {
|
|
1713
|
+
/* A null wrapper element prints its default. */
|
|
1714
|
+
static const pbw_value zero = {PBW_VALUE_UINT, 0, {0}};
|
|
1715
|
+
|
|
1716
|
+
v = &zero;
|
|
1717
|
+
}
|
|
1718
|
+
j_print_scalar(j, inner, v);
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1721
|
+
case J_EMPTY:
|
|
1722
|
+
j_str(j, "{}");
|
|
1723
|
+
return;
|
|
1724
|
+
default:
|
|
1725
|
+
break;
|
|
1726
|
+
}
|
|
1727
|
+
j_ch(j, '{');
|
|
1728
|
+
j_print_fields(j, msg);
|
|
1729
|
+
j_ch(j, '}');
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
/* Nesting is capped like the decoder's: a hand-built tree deeper than
|
|
1733
|
+
* PBW_MAX_DEPTH reports PBW_ERR_LIMIT instead of overflowing the stack. */
|
|
1734
|
+
static void
|
|
1735
|
+
j_print_message(j_out *j, const pbw_message *msg)
|
|
1736
|
+
{
|
|
1737
|
+
if (j == NULL || j->status != PBW_OK) {
|
|
1738
|
+
return;
|
|
1739
|
+
}
|
|
1740
|
+
if (!pbw_msg_fresh(msg)) {
|
|
1741
|
+
/* A nested message retired by a later add_field (or link) has a
|
|
1742
|
+
* shorter slot array than its live descriptor: walking it would
|
|
1743
|
+
* read past the slots. The root is already checked on entry. */
|
|
1744
|
+
j->status = PBW_ERR_INVALID;
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
if (j->depth >= PBW_MAX_DEPTH) {
|
|
1748
|
+
j->status = PBW_ERR_LIMIT;
|
|
1749
|
+
return;
|
|
1750
|
+
}
|
|
1751
|
+
j->depth++;
|
|
1752
|
+
j_print_message_body(j, msg);
|
|
1753
|
+
j->depth--;
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
pbw_status
|
|
1757
|
+
pbw_json_print(const pbw_message *msg, char **out, size_t *len)
|
|
1758
|
+
{
|
|
1759
|
+
j_out j;
|
|
1760
|
+
|
|
1761
|
+
if (!pbw_msg_fresh(msg) || out == NULL || len == NULL) {
|
|
1762
|
+
return PBW_ERR_INVALID;
|
|
1763
|
+
}
|
|
1764
|
+
/* Zeroed on every failure path, like pbw_encode() and the text printer:
|
|
1765
|
+
* a caller that reuses its buffer never keeps a stale pointer. */
|
|
1766
|
+
*out = NULL;
|
|
1767
|
+
*len = 0;
|
|
1768
|
+
pbw_buf_init(&j.buf);
|
|
1769
|
+
j.status = PBW_OK;
|
|
1770
|
+
j.depth = 0;
|
|
1771
|
+
if (!pbw_buf_reserve(&j.buf, 64)) {
|
|
1772
|
+
return PBW_ERR_NOMEM;
|
|
1773
|
+
}
|
|
1774
|
+
j.buf.len = 0;
|
|
1775
|
+
j_print_message(&j, msg);
|
|
1776
|
+
if (j.status != PBW_OK) {
|
|
1777
|
+
pbw_buf_free(&j.buf);
|
|
1778
|
+
return j.status;
|
|
1779
|
+
}
|
|
1780
|
+
if (!pbw_buf_reserve(&j.buf, 1)) {
|
|
1781
|
+
pbw_buf_free(&j.buf);
|
|
1782
|
+
return PBW_ERR_NOMEM;
|
|
1783
|
+
}
|
|
1784
|
+
j.buf.data[j.buf.len] = '\0';
|
|
1785
|
+
*out = (char *)j.buf.data;
|
|
1786
|
+
*len = j.buf.len;
|
|
1787
|
+
return PBW_OK;
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
/* --------------------------------------------------------------- parser */
|
|
1791
|
+
|
|
1792
|
+
typedef struct {
|
|
1793
|
+
const char *p;
|
|
1794
|
+
const char *end;
|
|
1795
|
+
} j_cur;
|
|
1796
|
+
|
|
1797
|
+
typedef struct {
|
|
1798
|
+
j_cur cur;
|
|
1799
|
+
int ignore_unknown;
|
|
1800
|
+
uint32_t depth;
|
|
1801
|
+
uint32_t dlim; /* nesting cap for this call (pbw_limits.max_depth) */
|
|
1802
|
+
} j_in;
|
|
1803
|
+
|
|
1804
|
+
typedef enum {
|
|
1805
|
+
JK_END,
|
|
1806
|
+
JK_OBJ,
|
|
1807
|
+
JK_ARR,
|
|
1808
|
+
JK_STR,
|
|
1809
|
+
JK_NUM,
|
|
1810
|
+
JK_TRUE,
|
|
1811
|
+
JK_FALSE,
|
|
1812
|
+
JK_NULL,
|
|
1813
|
+
JK_BAD
|
|
1814
|
+
} j_kind;
|
|
1815
|
+
|
|
1816
|
+
static void
|
|
1817
|
+
j_skip(j_cur *c)
|
|
1818
|
+
{
|
|
1819
|
+
while (c->p < c->end && (*c->p == ' ' || *c->p == '\t' || *c->p == '\n' ||
|
|
1820
|
+
*c->p == '\r')) {
|
|
1821
|
+
c->p++;
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
static j_kind
|
|
1826
|
+
j_peek_kind(j_in *j)
|
|
1827
|
+
{
|
|
1828
|
+
j_skip(&j->cur);
|
|
1829
|
+
if (j->cur.p >= j->cur.end) {
|
|
1830
|
+
return JK_END;
|
|
1831
|
+
}
|
|
1832
|
+
switch (*j->cur.p) {
|
|
1833
|
+
case '{':
|
|
1834
|
+
return JK_OBJ;
|
|
1835
|
+
case '[':
|
|
1836
|
+
return JK_ARR;
|
|
1837
|
+
case '"':
|
|
1838
|
+
case '\'':
|
|
1839
|
+
return JK_STR;
|
|
1840
|
+
case 't':
|
|
1841
|
+
return JK_TRUE;
|
|
1842
|
+
case 'f':
|
|
1843
|
+
return JK_FALSE;
|
|
1844
|
+
case 'n':
|
|
1845
|
+
return JK_NULL;
|
|
1846
|
+
case '-':
|
|
1847
|
+
case '0':
|
|
1848
|
+
case '1':
|
|
1849
|
+
case '2':
|
|
1850
|
+
case '3':
|
|
1851
|
+
case '4':
|
|
1852
|
+
case '5':
|
|
1853
|
+
case '6':
|
|
1854
|
+
case '7':
|
|
1855
|
+
case '8':
|
|
1856
|
+
case '9':
|
|
1857
|
+
return JK_NUM;
|
|
1858
|
+
default:
|
|
1859
|
+
return JK_BAD;
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
static pbw_status
|
|
1864
|
+
j_expect_lit(j_in *j, const char *lit)
|
|
1865
|
+
{
|
|
1866
|
+
j_skip(&j->cur);
|
|
1867
|
+
for (; *lit != '\0'; lit++) {
|
|
1868
|
+
if (j->cur.p >= j->cur.end || *j->cur.p != *lit) {
|
|
1869
|
+
return PBW_ERR_INVALID;
|
|
1870
|
+
}
|
|
1871
|
+
j->cur.p++;
|
|
1872
|
+
}
|
|
1873
|
+
return PBW_OK;
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
static pbw_status
|
|
1877
|
+
j_expect_ch(j_in *j, char c)
|
|
1878
|
+
{
|
|
1879
|
+
j_skip(&j->cur);
|
|
1880
|
+
if (j->cur.p >= j->cur.end || *j->cur.p != c) {
|
|
1881
|
+
return PBW_ERR_INVALID;
|
|
1882
|
+
}
|
|
1883
|
+
j->cur.p++;
|
|
1884
|
+
return PBW_OK;
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
/* Parses a JSON string (single quotes accepted, legacy mode), unescaping
|
|
1888
|
+
* into `out`. */
|
|
1889
|
+
static pbw_status
|
|
1890
|
+
j_parse_string(j_in *j, pbw_buf *out)
|
|
1891
|
+
{
|
|
1892
|
+
char quote;
|
|
1893
|
+
uint8_t seq[4];
|
|
1894
|
+
|
|
1895
|
+
j_skip(&j->cur);
|
|
1896
|
+
if (j->cur.p >= j->cur.end ||
|
|
1897
|
+
(*j->cur.p != '"' && *j->cur.p != '\'')) {
|
|
1898
|
+
return PBW_ERR_INVALID;
|
|
1899
|
+
}
|
|
1900
|
+
quote = *j->cur.p++;
|
|
1901
|
+
for (;;) {
|
|
1902
|
+
unsigned char c;
|
|
1903
|
+
|
|
1904
|
+
if (j->cur.p >= j->cur.end) {
|
|
1905
|
+
return PBW_ERR_INVALID;
|
|
1906
|
+
}
|
|
1907
|
+
c = (unsigned char)*j->cur.p++;
|
|
1908
|
+
if (c == (unsigned char)quote) {
|
|
1909
|
+
if (!pbw_utf8_valid(out->data, out->len)) {
|
|
1910
|
+
return PBW_ERR_INVALID;
|
|
1911
|
+
}
|
|
1912
|
+
return PBW_OK;
|
|
1913
|
+
}
|
|
1914
|
+
if (c == '\\') {
|
|
1915
|
+
unsigned char e;
|
|
1916
|
+
uint32_t rune;
|
|
1917
|
+
int width;
|
|
1918
|
+
int k;
|
|
1919
|
+
|
|
1920
|
+
if (j->cur.p >= j->cur.end) {
|
|
1921
|
+
return PBW_ERR_INVALID;
|
|
1922
|
+
}
|
|
1923
|
+
e = (unsigned char)*j->cur.p++;
|
|
1924
|
+
switch (e) {
|
|
1925
|
+
case '"':
|
|
1926
|
+
c = '"';
|
|
1927
|
+
break;
|
|
1928
|
+
case '\\':
|
|
1929
|
+
c = '\\';
|
|
1930
|
+
break;
|
|
1931
|
+
case '/':
|
|
1932
|
+
c = '/';
|
|
1933
|
+
break;
|
|
1934
|
+
case 'b':
|
|
1935
|
+
c = '\b';
|
|
1936
|
+
break;
|
|
1937
|
+
case 'f':
|
|
1938
|
+
c = '\f';
|
|
1939
|
+
break;
|
|
1940
|
+
case 'n':
|
|
1941
|
+
c = '\n';
|
|
1942
|
+
break;
|
|
1943
|
+
case 'r':
|
|
1944
|
+
c = '\r';
|
|
1945
|
+
break;
|
|
1946
|
+
case 't':
|
|
1947
|
+
c = '\t';
|
|
1948
|
+
break;
|
|
1949
|
+
case '\'':
|
|
1950
|
+
c = '\'';
|
|
1951
|
+
break;
|
|
1952
|
+
case 'u':
|
|
1953
|
+
case 'U': {
|
|
1954
|
+
uint32_t hi;
|
|
1955
|
+
int i;
|
|
1956
|
+
|
|
1957
|
+
/* `\\U` reads four hex digits just like `\\u` (legacy). */
|
|
1958
|
+
hi = 0;
|
|
1959
|
+
for (i = 0; i < 4; i++) {
|
|
1960
|
+
unsigned char h;
|
|
1961
|
+
|
|
1962
|
+
if (j->cur.p >= j->cur.end) {
|
|
1963
|
+
return PBW_ERR_INVALID;
|
|
1964
|
+
}
|
|
1965
|
+
h = (unsigned char)*j->cur.p++;
|
|
1966
|
+
hi <<= 4;
|
|
1967
|
+
if (h >= '0' && h <= '9') {
|
|
1968
|
+
hi |= (uint32_t)(h - '0');
|
|
1969
|
+
} else if (h >= 'a' && h <= 'f') {
|
|
1970
|
+
hi |= (uint32_t)(h - 'a' + 10);
|
|
1971
|
+
} else if (h >= 'A' && h <= 'F') {
|
|
1972
|
+
hi |= (uint32_t)(h - 'A' + 10);
|
|
1973
|
+
} else {
|
|
1974
|
+
return PBW_ERR_INVALID;
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
rune = hi;
|
|
1978
|
+
if (rune >= 0xd800 && rune <= 0xdbff) {
|
|
1979
|
+
uint32_t lo = 0;
|
|
1980
|
+
|
|
1981
|
+
if (pbw_remaining((const uint8_t *)j->cur.end, (const uint8_t *)j->cur.p) < 2 ||
|
|
1982
|
+
j->cur.p[0] != '\\' || j->cur.p[1] != 'u') {
|
|
1983
|
+
return PBW_ERR_INVALID;
|
|
1984
|
+
}
|
|
1985
|
+
j->cur.p += 2;
|
|
1986
|
+
for (i = 0; i < 4; i++) {
|
|
1987
|
+
unsigned char h;
|
|
1988
|
+
|
|
1989
|
+
if (j->cur.p >= j->cur.end) {
|
|
1990
|
+
return PBW_ERR_INVALID;
|
|
1991
|
+
}
|
|
1992
|
+
h = (unsigned char)*j->cur.p++;
|
|
1993
|
+
lo <<= 4;
|
|
1994
|
+
if (h >= '0' && h <= '9') {
|
|
1995
|
+
lo |= (uint32_t)(h - '0');
|
|
1996
|
+
} else if (h >= 'a' && h <= 'f') {
|
|
1997
|
+
lo |= (uint32_t)(h - 'a' + 10);
|
|
1998
|
+
} else if (h >= 'A' && h <= 'F') {
|
|
1999
|
+
lo |= (uint32_t)(h - 'A' + 10);
|
|
2000
|
+
} else {
|
|
2001
|
+
return PBW_ERR_INVALID;
|
|
2002
|
+
}
|
|
2003
|
+
}
|
|
2004
|
+
if (lo < 0xdc00 || lo > 0xdfff) {
|
|
2005
|
+
return PBW_ERR_INVALID;
|
|
2006
|
+
}
|
|
2007
|
+
rune = ((hi & 0x3ff) << 10) | (lo & 0x3ff);
|
|
2008
|
+
rune += 0x10000;
|
|
2009
|
+
} else if (rune >= 0xdc00 && rune <= 0xdfff) {
|
|
2010
|
+
return PBW_ERR_INVALID;
|
|
2011
|
+
}
|
|
2012
|
+
if (rune > 0x10ffff) {
|
|
2013
|
+
return PBW_ERR_INVALID;
|
|
2014
|
+
}
|
|
2015
|
+
if (rune <= 0x7f) {
|
|
2016
|
+
seq[0] = (uint8_t)rune;
|
|
2017
|
+
width = 1;
|
|
2018
|
+
} else if (rune <= 0x7ff) {
|
|
2019
|
+
seq[0] = (uint8_t)(((rune >> 6) & 0x1f) | 0xc0);
|
|
2020
|
+
seq[1] = (uint8_t)((rune & 0x3f) | 0x80);
|
|
2021
|
+
width = 2;
|
|
2022
|
+
} else if (rune <= 0xffff) {
|
|
2023
|
+
seq[0] = (uint8_t)(((rune >> 12) & 0x0f) | 0xe0);
|
|
2024
|
+
seq[1] = (uint8_t)(((rune >> 6) & 0x3f) | 0x80);
|
|
2025
|
+
seq[2] = (uint8_t)((rune & 0x3f) | 0x80);
|
|
2026
|
+
width = 3;
|
|
2027
|
+
} else {
|
|
2028
|
+
seq[0] = (uint8_t)(((rune >> 18) & 0x07) | 0xf0);
|
|
2029
|
+
seq[1] = (uint8_t)(((rune >> 12) & 0x3f) | 0x80);
|
|
2030
|
+
seq[2] = (uint8_t)(((rune >> 6) & 0x3f) | 0x80);
|
|
2031
|
+
seq[3] = (uint8_t)((rune & 0x3f) | 0x80);
|
|
2032
|
+
width = 4;
|
|
2033
|
+
}
|
|
2034
|
+
if (!pbw_buf_reserve(out, (size_t)width)) {
|
|
2035
|
+
return PBW_ERR_NOMEM;
|
|
2036
|
+
}
|
|
2037
|
+
for (k = 0; k < width; k++) {
|
|
2038
|
+
out->data[out->len++] = seq[k];
|
|
2039
|
+
}
|
|
2040
|
+
continue;
|
|
2041
|
+
}
|
|
2042
|
+
default:
|
|
2043
|
+
return PBW_ERR_INVALID;
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
if (!pbw_buf_reserve(out, 1)) {
|
|
2047
|
+
return PBW_ERR_NOMEM;
|
|
2048
|
+
}
|
|
2049
|
+
out->data[out->len++] = c;
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
|
|
2053
|
+
/* Scans a JSON number token without consuming what follows it. */
|
|
2054
|
+
static pbw_status
|
|
2055
|
+
j_parse_raw(j_in *j, const char **start, size_t *len)
|
|
2056
|
+
{
|
|
2057
|
+
const char *p;
|
|
2058
|
+
const char *end = j->cur.end;
|
|
2059
|
+
enum {
|
|
2060
|
+
ST_INT,
|
|
2061
|
+
ST_FRAC,
|
|
2062
|
+
ST_EXP
|
|
2063
|
+
} state = ST_INT;
|
|
2064
|
+
char prev = 0;
|
|
2065
|
+
|
|
2066
|
+
j_skip(&j->cur);
|
|
2067
|
+
p = j->cur.p;
|
|
2068
|
+
for (; p < end; p++) {
|
|
2069
|
+
char c = *p;
|
|
2070
|
+
int is_digit = c >= '0' && c <= '9';
|
|
2071
|
+
int last_was_int = prev >= '0' && prev <= '9';
|
|
2072
|
+
|
|
2073
|
+
if (is_digit) {
|
|
2074
|
+
/* keep going */
|
|
2075
|
+
} else if (state == ST_INT && c == '-') {
|
|
2076
|
+
if (last_was_int) {
|
|
2077
|
+
break;
|
|
2078
|
+
}
|
|
2079
|
+
} else if (state == ST_INT && last_was_int && c == '.') {
|
|
2080
|
+
state = ST_FRAC;
|
|
2081
|
+
} else if (state != ST_EXP && last_was_int &&
|
|
2082
|
+
(c == 'e' || c == 'E')) {
|
|
2083
|
+
state = ST_EXP;
|
|
2084
|
+
} else if ((prev == 'e' || prev == 'E') && (c == '-' || c == '+')) {
|
|
2085
|
+
/* keep going */
|
|
2086
|
+
} else {
|
|
2087
|
+
break;
|
|
2088
|
+
}
|
|
2089
|
+
prev = c;
|
|
2090
|
+
}
|
|
2091
|
+
*start = j->cur.p;
|
|
2092
|
+
*len = (size_t)(p - j->cur.p);
|
|
2093
|
+
if (*len == 0 || (*len == 1 && **start == '-')) {
|
|
2094
|
+
return PBW_ERR_INVALID;
|
|
2095
|
+
}
|
|
2096
|
+
{
|
|
2097
|
+
const char *t = *start;
|
|
2098
|
+
size_t n = *len;
|
|
2099
|
+
|
|
2100
|
+
if (*t == '-') {
|
|
2101
|
+
t++;
|
|
2102
|
+
n--;
|
|
2103
|
+
}
|
|
2104
|
+
if (n > 1 && t[0] == '0' && t[1] >= '0' && t[1] <= '9') {
|
|
2105
|
+
return PBW_ERR_INVALID;
|
|
2106
|
+
}
|
|
2107
|
+
if ((*start)[*len - 1] == '.') {
|
|
2108
|
+
return PBW_ERR_INVALID;
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
j->cur.p = p;
|
|
2112
|
+
/* A number must end where a value may end. */
|
|
2113
|
+
j_skip(&j->cur);
|
|
2114
|
+
if (j->cur.p < j->cur.end && *j->cur.p != ',' && *j->cur.p != ']' &&
|
|
2115
|
+
*j->cur.p != '}') {
|
|
2116
|
+
return PBW_ERR_INVALID;
|
|
2117
|
+
}
|
|
2118
|
+
return PBW_OK;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
/* Strict [+-]?digits with 64-bit range checking (for quoted values and
|
|
2122
|
+
* number tokens; surrounding whitespace is rejected by the caller). */
|
|
2123
|
+
static pbw_status
|
|
2124
|
+
j_atoi64(const char *text, size_t len, int64_t *out)
|
|
2125
|
+
{
|
|
2126
|
+
uint64_t acc = 0;
|
|
2127
|
+
int neg = 0;
|
|
2128
|
+
size_t i = 0;
|
|
2129
|
+
|
|
2130
|
+
if (len > 0 && (text[0] == '-' || text[0] == '+')) {
|
|
2131
|
+
neg = text[0] == '-';
|
|
2132
|
+
i = 1;
|
|
2133
|
+
}
|
|
2134
|
+
if (i >= len) {
|
|
2135
|
+
return PBW_ERR_INVALID;
|
|
2136
|
+
}
|
|
2137
|
+
for (; i < len; i++) {
|
|
2138
|
+
unsigned char c = (unsigned char)text[i];
|
|
2139
|
+
uint64_t limit;
|
|
2140
|
+
|
|
2141
|
+
if (c < '0' || c > '9') {
|
|
2142
|
+
return PBW_ERR_INVALID;
|
|
2143
|
+
}
|
|
2144
|
+
limit = neg ? (uint64_t)INT64_MIN : (uint64_t)INT64_MAX;
|
|
2145
|
+
if (acc > (limit - (uint64_t)(c - '0')) / 10) {
|
|
2146
|
+
return PBW_ERR_INVALID;
|
|
2147
|
+
}
|
|
2148
|
+
acc = acc * 10 + (uint64_t)(c - '0');
|
|
2149
|
+
}
|
|
2150
|
+
if (neg) {
|
|
2151
|
+
/* INT64_MIN negates to itself in two's complement. */
|
|
2152
|
+
*out = (int64_t)(~(acc - 1));
|
|
2153
|
+
} else {
|
|
2154
|
+
*out = (int64_t)acc;
|
|
2155
|
+
}
|
|
2156
|
+
return PBW_OK;
|
|
2157
|
+
}
|
|
2158
|
+
|
|
2159
|
+
static pbw_status
|
|
2160
|
+
j_atou64(const char *text, size_t len, uint64_t *out)
|
|
2161
|
+
{
|
|
2162
|
+
uint64_t acc = 0;
|
|
2163
|
+
size_t i = 0;
|
|
2164
|
+
|
|
2165
|
+
if (len > 0 && text[0] == '+') {
|
|
2166
|
+
i = 1;
|
|
2167
|
+
}
|
|
2168
|
+
if (i >= len) {
|
|
2169
|
+
return PBW_ERR_INVALID;
|
|
2170
|
+
}
|
|
2171
|
+
for (; i < len; i++) {
|
|
2172
|
+
unsigned char c = (unsigned char)text[i];
|
|
2173
|
+
|
|
2174
|
+
if (c < '0' || c > '9') {
|
|
2175
|
+
return PBW_ERR_INVALID;
|
|
2176
|
+
}
|
|
2177
|
+
if (acc > (UINT64_MAX - (uint64_t)(c - '0')) / 10) {
|
|
2178
|
+
return PBW_ERR_INVALID;
|
|
2179
|
+
}
|
|
2180
|
+
acc = acc * 10 + (uint64_t)(c - '0');
|
|
2181
|
+
}
|
|
2182
|
+
*out = acc;
|
|
2183
|
+
return PBW_OK;
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
/* Whitespace-tolerant variant for map keys and quoted enum numbers, where
|
|
2187
|
+
* the reference uses SimpleAtoi (which skips surrounding whitespace). */
|
|
2188
|
+
static void
|
|
2189
|
+
j_trim(const char **text, size_t *len)
|
|
2190
|
+
{
|
|
2191
|
+
while (*len > 0 && (**text == ' ' || **text == '\t' ||
|
|
2192
|
+
**text == '\n' || **text == '\r')) {
|
|
2193
|
+
(*text)++;
|
|
2194
|
+
(*len)--;
|
|
2195
|
+
}
|
|
2196
|
+
while (*len > 0 && ((*text)[*len - 1] == ' ' ||
|
|
2197
|
+
(*text)[*len - 1] == '\t' ||
|
|
2198
|
+
(*text)[*len - 1] == '\n' ||
|
|
2199
|
+
(*text)[*len - 1] == '\r')) {
|
|
2200
|
+
(*len)--;
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
/* Locale-free strtod with full-consumption check, like SimpleAtod. */
|
|
2205
|
+
static pbw_status
|
|
2206
|
+
j_atod(const char *text, size_t len, double *out)
|
|
2207
|
+
{
|
|
2208
|
+
char stack[64];
|
|
2209
|
+
char *tmp = stack;
|
|
2210
|
+
char *stop;
|
|
2211
|
+
double v;
|
|
2212
|
+
|
|
2213
|
+
if (text == NULL || out == NULL) {
|
|
2214
|
+
return PBW_ERR_INVALID;
|
|
2215
|
+
}
|
|
2216
|
+
/* strtod skips leading whitespace itself, but not trailing garbage. A
|
|
2217
|
+
* number longer than the stack buffer is unusual, not invalid: the
|
|
2218
|
+
* reference parser has no length limit either. */
|
|
2219
|
+
if (len >= sizeof(stack)) {
|
|
2220
|
+
if (len == SIZE_MAX) {
|
|
2221
|
+
return PBW_ERR_NOMEM;
|
|
2222
|
+
}
|
|
2223
|
+
tmp = (char *)malloc(len + 1);
|
|
2224
|
+
if (tmp == NULL) {
|
|
2225
|
+
return PBW_ERR_NOMEM;
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
memcpy(tmp, text, len);
|
|
2229
|
+
tmp[len] = '\0';
|
|
2230
|
+
if (tmp == NULL) {
|
|
2231
|
+
return PBW_ERR_INVALID;
|
|
2232
|
+
}
|
|
2233
|
+
v = pbw_strtod_c(tmp, &stop);
|
|
2234
|
+
if (stop == tmp) {
|
|
2235
|
+
goto invalid;
|
|
2236
|
+
}
|
|
2237
|
+
while (*stop == ' ' || *stop == '\t' || *stop == '\n' ||
|
|
2238
|
+
*stop == '\r') {
|
|
2239
|
+
stop++;
|
|
2240
|
+
}
|
|
2241
|
+
if (*stop != '\0') {
|
|
2242
|
+
goto invalid;
|
|
2243
|
+
}
|
|
2244
|
+
if (tmp != stack) {
|
|
2245
|
+
free(tmp);
|
|
2246
|
+
}
|
|
2247
|
+
*out = v;
|
|
2248
|
+
return PBW_OK;
|
|
2249
|
+
invalid:
|
|
2250
|
+
if (tmp != stack) {
|
|
2251
|
+
free(tmp);
|
|
2252
|
+
}
|
|
2253
|
+
return PBW_ERR_INVALID;
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
/* absl::SimpleAtob's word set, case-insensitive. */
|
|
2257
|
+
static pbw_status
|
|
2258
|
+
j_atob(const char *text, size_t len, int *out)
|
|
2259
|
+
{
|
|
2260
|
+
static const struct {
|
|
2261
|
+
const char *word;
|
|
2262
|
+
int value;
|
|
2263
|
+
} table[] = {{"true", 1}, {"1", 1}, {"t", 1}, {"y", 1}, {"yes", 1}, {"false", 0}, {"0", 0}, {"f", 0}, {"n", 0}, {"no", 0}};
|
|
2264
|
+
char lower[8];
|
|
2265
|
+
size_t i;
|
|
2266
|
+
|
|
2267
|
+
if (len >= sizeof(lower)) {
|
|
2268
|
+
return PBW_ERR_INVALID;
|
|
2269
|
+
}
|
|
2270
|
+
for (i = 0; i < len; i++) {
|
|
2271
|
+
unsigned char c = (unsigned char)text[i];
|
|
2272
|
+
|
|
2273
|
+
lower[i] = (char)tolower(c);
|
|
2274
|
+
}
|
|
2275
|
+
lower[len] = '\0';
|
|
2276
|
+
for (i = 0; i < sizeof(table) / sizeof(table[0]); i++) {
|
|
2277
|
+
if (strcmp(lower, table[i].word) == 0) {
|
|
2278
|
+
*out = table[i].value;
|
|
2279
|
+
return PBW_OK;
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
return PBW_ERR_INVALID;
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2285
|
+
static pbw_status
|
|
2286
|
+
j_parse_key(j_in *j, pbw_buf *out)
|
|
2287
|
+
{
|
|
2288
|
+
if (j_peek_kind(j) == JK_STR) {
|
|
2289
|
+
if (out == NULL) {
|
|
2290
|
+
pbw_buf scratch;
|
|
2291
|
+
pbw_status st;
|
|
2292
|
+
|
|
2293
|
+
pbw_buf_init(&scratch);
|
|
2294
|
+
st = j_parse_string(j, &scratch);
|
|
2295
|
+
pbw_buf_free(&scratch);
|
|
2296
|
+
return st;
|
|
2297
|
+
}
|
|
2298
|
+
return j_parse_string(j, out);
|
|
2299
|
+
}
|
|
2300
|
+
{
|
|
2301
|
+
const char *p;
|
|
2302
|
+
size_t n = 0;
|
|
2303
|
+
|
|
2304
|
+
j_skip(&j->cur);
|
|
2305
|
+
p = j->cur.p;
|
|
2306
|
+
for (; p < j->cur.end; p++) {
|
|
2307
|
+
char c = *p;
|
|
2308
|
+
|
|
2309
|
+
if ((c < '0' || c > '9') && (c < 'a' || c > 'z') &&
|
|
2310
|
+
(c < 'A' || c > 'Z') && c != '_') {
|
|
2311
|
+
break;
|
|
2312
|
+
}
|
|
2313
|
+
n++;
|
|
2314
|
+
}
|
|
2315
|
+
if (n == 0) {
|
|
2316
|
+
return PBW_ERR_INVALID;
|
|
2317
|
+
}
|
|
2318
|
+
if (j->cur.p >= j->cur.end || (j->cur.p[0] >= '0' && j->cur.p[0] <= '9')) {
|
|
2319
|
+
return PBW_ERR_INVALID;
|
|
2320
|
+
}
|
|
2321
|
+
if ((n == 4 && (memcmp(j->cur.p, "true", 4) == 0 ||
|
|
2322
|
+
memcmp(j->cur.p, "null", 4) == 0)) ||
|
|
2323
|
+
(n == 5 && memcmp(j->cur.p, "false", 5) == 0)) {
|
|
2324
|
+
/* A bare JSON literal is not a key. Comparing each word at its
|
|
2325
|
+
* own length keeps a real field called "nulls" or "trueish"
|
|
2326
|
+
* addressable in the legacy bare-key form. */
|
|
2327
|
+
return PBW_ERR_INVALID;
|
|
2328
|
+
}
|
|
2329
|
+
if (out != NULL) {
|
|
2330
|
+
if (!pbw_buf_reserve(out, n)) {
|
|
2331
|
+
return PBW_ERR_NOMEM;
|
|
2332
|
+
}
|
|
2333
|
+
memcpy(out->data + out->len, j->cur.p, n);
|
|
2334
|
+
out->len += n;
|
|
2335
|
+
}
|
|
2336
|
+
j->cur.p = p;
|
|
2337
|
+
return PBW_OK;
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
/* Parses a key (quoted or bare) into `out`; NULL discards it. */
|
|
2342
|
+
static pbw_status j_parse_key(j_in *j, pbw_buf *out); /* defined below */
|
|
2343
|
+
static pbw_status j_parse_named_field(j_in *j, pbw_message *msg,
|
|
2344
|
+
const uint8_t *key, size_t len);
|
|
2345
|
+
static pbw_status j_parse_list_of(j_in *j, pbw_message *msg);
|
|
2346
|
+
|
|
2347
|
+
/* Skips one JSON value (for unknown fields and Any scans). */
|
|
2348
|
+
static pbw_status
|
|
2349
|
+
j_skip_value(j_in *j)
|
|
2350
|
+
{
|
|
2351
|
+
pbw_buf scratch;
|
|
2352
|
+
const char *start;
|
|
2353
|
+
size_t len;
|
|
2354
|
+
|
|
2355
|
+
switch (j_peek_kind(j)) {
|
|
2356
|
+
case JK_OBJ: {
|
|
2357
|
+
pbw_status st;
|
|
2358
|
+
|
|
2359
|
+
if (j_expect_ch(j, '{') != PBW_OK) {
|
|
2360
|
+
return PBW_ERR_INVALID;
|
|
2361
|
+
}
|
|
2362
|
+
if (j->depth >= j->dlim) {
|
|
2363
|
+
return PBW_ERR_LIMIT;
|
|
2364
|
+
}
|
|
2365
|
+
j->depth++;
|
|
2366
|
+
j_skip(&j->cur);
|
|
2367
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
2368
|
+
j->cur.p++;
|
|
2369
|
+
j->depth--;
|
|
2370
|
+
return PBW_OK;
|
|
2371
|
+
}
|
|
2372
|
+
for (;;) {
|
|
2373
|
+
/* Bare keys are accepted in legacy mode. */
|
|
2374
|
+
st = j_parse_key(j, NULL);
|
|
2375
|
+
if (st != PBW_OK) {
|
|
2376
|
+
j->depth--;
|
|
2377
|
+
return st;
|
|
2378
|
+
}
|
|
2379
|
+
if (j_expect_ch(j, ':') != PBW_OK) {
|
|
2380
|
+
j->depth--;
|
|
2381
|
+
return PBW_ERR_INVALID;
|
|
2382
|
+
}
|
|
2383
|
+
st = j_skip_value(j);
|
|
2384
|
+
if (st != PBW_OK) {
|
|
2385
|
+
j->depth--;
|
|
2386
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
2387
|
+
}
|
|
2388
|
+
j_skip(&j->cur);
|
|
2389
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
2390
|
+
j->cur.p++;
|
|
2391
|
+
continue;
|
|
2392
|
+
}
|
|
2393
|
+
if (j_expect_ch(j, '}') != PBW_OK) {
|
|
2394
|
+
j->depth--;
|
|
2395
|
+
return PBW_ERR_INVALID;
|
|
2396
|
+
}
|
|
2397
|
+
j->depth--;
|
|
2398
|
+
return PBW_OK;
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
case JK_ARR: {
|
|
2402
|
+
pbw_status st = PBW_OK;
|
|
2403
|
+
|
|
2404
|
+
if (j_expect_ch(j, '[') != PBW_OK) {
|
|
2405
|
+
return PBW_ERR_INVALID;
|
|
2406
|
+
}
|
|
2407
|
+
if (j->depth >= j->dlim) {
|
|
2408
|
+
return PBW_ERR_LIMIT;
|
|
2409
|
+
}
|
|
2410
|
+
j->depth++;
|
|
2411
|
+
j_skip(&j->cur);
|
|
2412
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
2413
|
+
j->cur.p++;
|
|
2414
|
+
j->depth--;
|
|
2415
|
+
return PBW_OK;
|
|
2416
|
+
}
|
|
2417
|
+
for (;;) {
|
|
2418
|
+
st = j_skip_value(j);
|
|
2419
|
+
if (st != PBW_OK) {
|
|
2420
|
+
j->depth--;
|
|
2421
|
+
return st;
|
|
2422
|
+
}
|
|
2423
|
+
j_skip(&j->cur);
|
|
2424
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
2425
|
+
j->cur.p++;
|
|
2426
|
+
continue;
|
|
2427
|
+
}
|
|
2428
|
+
if (j_expect_ch(j, ']') != PBW_OK) {
|
|
2429
|
+
j->depth--;
|
|
2430
|
+
return PBW_ERR_INVALID;
|
|
2431
|
+
}
|
|
2432
|
+
j->depth--;
|
|
2433
|
+
return PBW_OK;
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
case JK_STR: {
|
|
2437
|
+
pbw_status st;
|
|
2438
|
+
|
|
2439
|
+
pbw_buf_init(&scratch);
|
|
2440
|
+
st = j_parse_string(j, &scratch);
|
|
2441
|
+
pbw_buf_free(&scratch);
|
|
2442
|
+
return st;
|
|
2443
|
+
}
|
|
2444
|
+
case JK_NUM:
|
|
2445
|
+
return j_parse_raw(j, &start, &len);
|
|
2446
|
+
case JK_TRUE:
|
|
2447
|
+
return j_expect_lit(j, "true");
|
|
2448
|
+
case JK_FALSE:
|
|
2449
|
+
return j_expect_lit(j, "false");
|
|
2450
|
+
case JK_NULL:
|
|
2451
|
+
return j_expect_lit(j, "null");
|
|
2452
|
+
default:
|
|
2453
|
+
return PBW_ERR_INVALID;
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
/* -------------------------------------------------- value storage */
|
|
2458
|
+
|
|
2459
|
+
static pbw_status j_parse_message(j_in *j, pbw_message *msg);
|
|
2460
|
+
static pbw_status j_parse_object_body(j_in *j, pbw_message *msg);
|
|
2461
|
+
static pbw_status j_parse_field_value(j_in *j, pbw_message *msg,
|
|
2462
|
+
const pbw_field *field, int in_array);
|
|
2463
|
+
|
|
2464
|
+
/* Stores a uint; repeated fields append. */
|
|
2465
|
+
static pbw_status
|
|
2466
|
+
j_store_uint(j_in *j, pbw_message *msg, const pbw_field *field, uint64_t bits)
|
|
2467
|
+
{
|
|
2468
|
+
pbw_value *item;
|
|
2469
|
+
|
|
2470
|
+
(void)j;
|
|
2471
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
2472
|
+
item = pbw_value_new_uint_a(msg->arena, bits);
|
|
2473
|
+
if (item == NULL) {
|
|
2474
|
+
return PBW_ERR_NOMEM;
|
|
2475
|
+
}
|
|
2476
|
+
{
|
|
2477
|
+
pbw_value *list = pbw_message_list_for(msg, field);
|
|
2478
|
+
|
|
2479
|
+
if (list == NULL ||
|
|
2480
|
+
!pbw_value_list_push(list, item, msg->arena)) {
|
|
2481
|
+
pbw_value_free(item);
|
|
2482
|
+
return PBW_ERR_NOMEM;
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
return PBW_OK;
|
|
2486
|
+
}
|
|
2487
|
+
{
|
|
2488
|
+
pbw_value value;
|
|
2489
|
+
|
|
2490
|
+
value.kind = PBW_VALUE_UINT;
|
|
2491
|
+
value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
|
|
2492
|
+
value.as.u64 = bits;
|
|
2493
|
+
pbw_message_put(msg, (size_t)(field - msg->desc->fields), &value);
|
|
2494
|
+
return PBW_OK;
|
|
2495
|
+
}
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
/* Stores bytes; repeated fields append. */
|
|
2499
|
+
static pbw_status
|
|
2500
|
+
j_store_bytes(j_in *j, pbw_message *msg, const pbw_field *field,
|
|
2501
|
+
const uint8_t *data, size_t len)
|
|
2502
|
+
{
|
|
2503
|
+
(void)j;
|
|
2504
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
2505
|
+
pbw_value *item = pbw_value_new_bytes_a(msg->arena, data, len);
|
|
2506
|
+
pbw_value *list;
|
|
2507
|
+
|
|
2508
|
+
if (item == NULL) {
|
|
2509
|
+
return PBW_ERR_NOMEM;
|
|
2510
|
+
}
|
|
2511
|
+
list = pbw_message_list_for(msg, field);
|
|
2512
|
+
if (list == NULL || !pbw_value_list_push(list, item, msg->arena)) {
|
|
2513
|
+
pbw_value_free(item);
|
|
2514
|
+
return PBW_ERR_NOMEM;
|
|
2515
|
+
}
|
|
2516
|
+
return PBW_OK;
|
|
2517
|
+
}
|
|
2518
|
+
{
|
|
2519
|
+
pbw_value value;
|
|
2520
|
+
|
|
2521
|
+
if (!pbw_value_init_bytes(&value, msg->arena, data, len)) {
|
|
2522
|
+
return PBW_ERR_NOMEM;
|
|
2523
|
+
}
|
|
2524
|
+
pbw_message_put(msg, (size_t)(field - msg->desc->fields), &value);
|
|
2525
|
+
return PBW_OK;
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
/* Stores a submessage; repeated fields append, singular fields replace. */
|
|
2530
|
+
static pbw_status
|
|
2531
|
+
j_store_message(j_in *j, pbw_message *msg, const pbw_field *field,
|
|
2532
|
+
pbw_message *sub)
|
|
2533
|
+
{
|
|
2534
|
+
(void)j;
|
|
2535
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
2536
|
+
pbw_value *item = pbw_value_new_message(sub);
|
|
2537
|
+
pbw_value *list;
|
|
2538
|
+
|
|
2539
|
+
if (item == NULL) {
|
|
2540
|
+
pbw_message_free(sub);
|
|
2541
|
+
return PBW_ERR_NOMEM;
|
|
2542
|
+
}
|
|
2543
|
+
list = pbw_message_list_for(msg, field);
|
|
2544
|
+
if (list == NULL || !pbw_value_list_push(list, item, msg->arena)) {
|
|
2545
|
+
pbw_value_free(item);
|
|
2546
|
+
return PBW_ERR_NOMEM;
|
|
2547
|
+
}
|
|
2548
|
+
return PBW_OK;
|
|
2549
|
+
}
|
|
2550
|
+
{
|
|
2551
|
+
pbw_value value;
|
|
2552
|
+
|
|
2553
|
+
value.kind = PBW_VALUE_MESSAGE;
|
|
2554
|
+
value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
|
|
2555
|
+
value.as.msg = sub;
|
|
2556
|
+
pbw_message_put(msg, (size_t)(field - msg->desc->fields), &value);
|
|
2557
|
+
return PBW_OK;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
/* An unknown number for a closed enum moves to the unknown fields, exactly
|
|
2562
|
+
* like the binary decoder does. */
|
|
2563
|
+
static pbw_status
|
|
2564
|
+
j_store_unknown_enum(j_in *j, pbw_message *msg, const pbw_field *field,
|
|
2565
|
+
int32_t number)
|
|
2566
|
+
{
|
|
2567
|
+
uint8_t tmp[16];
|
|
2568
|
+
size_t n;
|
|
2569
|
+
|
|
2570
|
+
(void)j;
|
|
2571
|
+
/* int32 negatives encode sign-extended, like on the wire */
|
|
2572
|
+
n = (size_t)pbw_varint_write(tmp, ((uint64_t)field->number << 3));
|
|
2573
|
+
n += (size_t)pbw_varint_write(tmp + n, (uint64_t)(int64_t)number);
|
|
2574
|
+
if ((pbw_status)pbw_message_add_unknown(msg, tmp, n) != PBW_OK) {
|
|
2575
|
+
return PBW_ERR_NOMEM;
|
|
2576
|
+
}
|
|
2577
|
+
return PBW_OK;
|
|
2578
|
+
}
|
|
2579
|
+
|
|
2580
|
+
/* -------------------------------------------------- scalar values */
|
|
2581
|
+
|
|
2582
|
+
/* Integer rules shared by tokens and quoted strings: the token is strict,
|
|
2583
|
+
* quoted strings reject surrounding whitespace (map keys and quoted enum
|
|
2584
|
+
* numbers allow it, like SimpleAtoi). */
|
|
2585
|
+
static pbw_status
|
|
2586
|
+
j_parse_int_value(j_in *j, const char *text, size_t len, int quoted,
|
|
2587
|
+
int is_signed, int bits32, uint64_t *out)
|
|
2588
|
+
{
|
|
2589
|
+
int64_t s = 0;
|
|
2590
|
+
uint64_t u = 0;
|
|
2591
|
+
|
|
2592
|
+
(void)j;
|
|
2593
|
+
if (quoted) {
|
|
2594
|
+
size_t i;
|
|
2595
|
+
|
|
2596
|
+
for (i = 0; i < len; i++) {
|
|
2597
|
+
if (text[i] == ' ' || text[i] == '\t' || text[i] == '\n' ||
|
|
2598
|
+
text[i] == '\r') {
|
|
2599
|
+
return PBW_ERR_INVALID;
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
if (is_signed) {
|
|
2604
|
+
if (j_atoi64(text, len, &s) != PBW_OK) {
|
|
2605
|
+
double d;
|
|
2606
|
+
|
|
2607
|
+
/* 1e3 and 1.0 spell integers; 1.5 does not. */
|
|
2608
|
+
if (j_atod(text, len, &d) != PBW_OK || !isfinite(d) ||
|
|
2609
|
+
d < -9007199254740992.0 || d > 9007199254740992.0) {
|
|
2610
|
+
return PBW_ERR_INVALID;
|
|
2611
|
+
}
|
|
2612
|
+
s = (int64_t)d;
|
|
2613
|
+
if ((double)s != d) {
|
|
2614
|
+
return PBW_ERR_INVALID;
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
if (bits32 && (s < INT32_MIN || s > INT32_MAX)) {
|
|
2618
|
+
return PBW_ERR_INVALID;
|
|
2619
|
+
}
|
|
2620
|
+
*out = (uint64_t)s;
|
|
2621
|
+
return PBW_OK;
|
|
2622
|
+
}
|
|
2623
|
+
if (j_atou64(text, len, &u) != PBW_OK) {
|
|
2624
|
+
double d;
|
|
2625
|
+
|
|
2626
|
+
if (j_atod(text, len, &d) != PBW_OK || !isfinite(d) || d < 0 ||
|
|
2627
|
+
d > 18014398509481984.0) {
|
|
2628
|
+
return PBW_ERR_INVALID;
|
|
2629
|
+
}
|
|
2630
|
+
/* The bounds above keep the conversion defined. */
|
|
2631
|
+
u = (uint64_t)d;
|
|
2632
|
+
if ((double)u != d) {
|
|
2633
|
+
return PBW_ERR_INVALID;
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
if (bits32 && u > UINT32_MAX) {
|
|
2637
|
+
return PBW_ERR_INVALID;
|
|
2638
|
+
}
|
|
2639
|
+
*out = u;
|
|
2640
|
+
return PBW_OK;
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
static pbw_status
|
|
2644
|
+
j_parse_double_value(j_in *j, const char *text, size_t len, int is_float,
|
|
2645
|
+
double *out)
|
|
2646
|
+
{
|
|
2647
|
+
static const struct {
|
|
2648
|
+
const char *word;
|
|
2649
|
+
double v;
|
|
2650
|
+
} specials[] = {{"NaN", NAN},
|
|
2651
|
+
{"Infinity", INFINITY},
|
|
2652
|
+
{"-Infinity", -INFINITY}};
|
|
2653
|
+
double d;
|
|
2654
|
+
size_t i;
|
|
2655
|
+
|
|
2656
|
+
(void)j;
|
|
2657
|
+
for (i = 0; i < sizeof(specials) / sizeof(specials[0]); i++) {
|
|
2658
|
+
if (strlen(specials[i].word) == len &&
|
|
2659
|
+
memcmp(text, specials[i].word, len) == 0) {
|
|
2660
|
+
*out = specials[i].v;
|
|
2661
|
+
return PBW_OK;
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
/* Quoted floats allow surrounding whitespace (SimpleAtod does). */
|
|
2665
|
+
if (j_atod(text, len, &d) != PBW_OK) {
|
|
2666
|
+
return PBW_ERR_INVALID;
|
|
2667
|
+
}
|
|
2668
|
+
if (is_float && isfinite(d) && !isfinite((float)d)) {
|
|
2669
|
+
return PBW_ERR_INVALID;
|
|
2670
|
+
}
|
|
2671
|
+
*out = d;
|
|
2672
|
+
return PBW_OK;
|
|
2673
|
+
}
|
|
2674
|
+
|
|
2675
|
+
static int
|
|
2676
|
+
j_base64_value(char c)
|
|
2677
|
+
{
|
|
2678
|
+
if (c >= 'A' && c <= 'Z') {
|
|
2679
|
+
return c - 'A';
|
|
2680
|
+
}
|
|
2681
|
+
if (c >= 'a' && c <= 'z') {
|
|
2682
|
+
return c - 'a' + 26;
|
|
2683
|
+
}
|
|
2684
|
+
if (c >= '0' && c <= '9') {
|
|
2685
|
+
return c - '0' + 52;
|
|
2686
|
+
}
|
|
2687
|
+
if (c == '+' || c == '-') {
|
|
2688
|
+
return 62;
|
|
2689
|
+
}
|
|
2690
|
+
if (c == '/' || c == '_') {
|
|
2691
|
+
return 63;
|
|
2692
|
+
}
|
|
2693
|
+
return -1;
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
/* Base64 with either alphabet and optional padding, like the reference. */
|
|
2697
|
+
static pbw_status
|
|
2698
|
+
j_parse_base64(const char *text, size_t len, pbw_buf *out)
|
|
2699
|
+
{
|
|
2700
|
+
size_t n;
|
|
2701
|
+
size_t i = 0;
|
|
2702
|
+
size_t need;
|
|
2703
|
+
|
|
2704
|
+
if (out == NULL) {
|
|
2705
|
+
return PBW_ERR_INVALID;
|
|
2706
|
+
}
|
|
2707
|
+
if (text == NULL && len > 0) {
|
|
2708
|
+
return PBW_ERR_INVALID;
|
|
2709
|
+
}
|
|
2710
|
+
if (text == NULL) {
|
|
2711
|
+
text = (const char *)pbw_empty_input;
|
|
2712
|
+
}
|
|
2713
|
+
n = len;
|
|
2714
|
+
|
|
2715
|
+
/* Padding, if any, is 1-2 '=' at the very end. */
|
|
2716
|
+
while (n > 0 && text[n - 1] == '=') {
|
|
2717
|
+
n--;
|
|
2718
|
+
}
|
|
2719
|
+
if (len - n > 2) {
|
|
2720
|
+
return PBW_ERR_INVALID;
|
|
2721
|
+
}
|
|
2722
|
+
if (n == 0) {
|
|
2723
|
+
/* Empty is fine; padding alone is not. */
|
|
2724
|
+
return len == 0 ? PBW_OK : PBW_ERR_INVALID;
|
|
2725
|
+
}
|
|
2726
|
+
if (len == n ? (n % 4 == 1) : (len % 4 != 0)) {
|
|
2727
|
+
/* Unpadded tails of 2-3 chars are fine, but padded input must
|
|
2728
|
+
* fill complete quanta, like the reference. */
|
|
2729
|
+
return PBW_ERR_INVALID;
|
|
2730
|
+
}
|
|
2731
|
+
/* (n/4)*3+3 cannot wrap: n <= SIZE_MAX, so (n/4)*3 <= n on both
|
|
2732
|
+
* 32- and 64-bit targets. */
|
|
2733
|
+
need = (n / 4) * 3 + 3;
|
|
2734
|
+
if (need < (n / 4) * 3) {
|
|
2735
|
+
return PBW_ERR_NOMEM;
|
|
2736
|
+
}
|
|
2737
|
+
if (!pbw_buf_reserve(out, need)) {
|
|
2738
|
+
return PBW_ERR_NOMEM;
|
|
2739
|
+
}
|
|
2740
|
+
while (i < (n & ~(size_t)3)) {
|
|
2741
|
+
int a = j_base64_value(text[i]);
|
|
2742
|
+
int b = j_base64_value(text[i + 1]);
|
|
2743
|
+
int c = j_base64_value(text[i + 2]);
|
|
2744
|
+
int d = j_base64_value(text[i + 3]);
|
|
2745
|
+
|
|
2746
|
+
if (a < 0 || b < 0 || c < 0 || d < 0) {
|
|
2747
|
+
return PBW_ERR_INVALID;
|
|
2748
|
+
}
|
|
2749
|
+
out->data[out->len++] = (uint8_t)((a << 2) | (b >> 4));
|
|
2750
|
+
out->data[out->len++] = (uint8_t)(((b & 15) << 4) | (c >> 2));
|
|
2751
|
+
out->data[out->len++] = (uint8_t)(((c & 3) << 6) | d);
|
|
2752
|
+
i += 4;
|
|
2753
|
+
}
|
|
2754
|
+
if (n - i == 2) {
|
|
2755
|
+
int a = j_base64_value(text[i]);
|
|
2756
|
+
int b = j_base64_value(text[i + 1]);
|
|
2757
|
+
|
|
2758
|
+
if (a < 0 || b < 0) {
|
|
2759
|
+
return PBW_ERR_INVALID;
|
|
2760
|
+
}
|
|
2761
|
+
out->data[out->len++] = (uint8_t)((a << 2) | (b >> 4));
|
|
2762
|
+
} else if (n - i == 3) {
|
|
2763
|
+
int a = j_base64_value(text[i]);
|
|
2764
|
+
int b = j_base64_value(text[i + 1]);
|
|
2765
|
+
int c = j_base64_value(text[i + 2]);
|
|
2766
|
+
|
|
2767
|
+
if (a < 0 || b < 0 || c < 0) {
|
|
2768
|
+
return PBW_ERR_INVALID;
|
|
2769
|
+
}
|
|
2770
|
+
out->data[out->len++] = (uint8_t)((a << 2) | (b >> 4));
|
|
2771
|
+
out->data[out->len++] = (uint8_t)(((b & 15) << 4) | (c >> 2));
|
|
2772
|
+
}
|
|
2773
|
+
return PBW_OK;
|
|
2774
|
+
}
|
|
2775
|
+
|
|
2776
|
+
/* Parses one scalar (non-message) value of `field`. */
|
|
2777
|
+
static pbw_status
|
|
2778
|
+
j_parse_scalar_value(j_in *j, pbw_message *msg, const pbw_field *field)
|
|
2779
|
+
{
|
|
2780
|
+
j_kind kind = j_peek_kind(j);
|
|
2781
|
+
uint64_t bits = 0;
|
|
2782
|
+
|
|
2783
|
+
switch (field->type) {
|
|
2784
|
+
case PBW_TYPE_DOUBLE:
|
|
2785
|
+
case PBW_TYPE_FLOAT: {
|
|
2786
|
+
double v = 0;
|
|
2787
|
+
pbw_status st;
|
|
2788
|
+
|
|
2789
|
+
if (kind == JK_NUM) {
|
|
2790
|
+
const char *text;
|
|
2791
|
+
size_t len;
|
|
2792
|
+
|
|
2793
|
+
if (j_parse_raw(j, &text, &len) != PBW_OK) {
|
|
2794
|
+
return PBW_ERR_INVALID;
|
|
2795
|
+
}
|
|
2796
|
+
st = j_parse_double_value(
|
|
2797
|
+
j, text, len, field->type == PBW_TYPE_FLOAT, &v);
|
|
2798
|
+
/* Raw numbers must be finite (the lexer bars inf/nan spellings,
|
|
2799
|
+
* but not overflow like 1e400). */
|
|
2800
|
+
if (st == PBW_OK && !isfinite(v)) {
|
|
2801
|
+
st = PBW_ERR_INVALID;
|
|
2802
|
+
}
|
|
2803
|
+
} else if (kind == JK_STR) {
|
|
2804
|
+
pbw_buf raw;
|
|
2805
|
+
|
|
2806
|
+
pbw_buf_init(&raw);
|
|
2807
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
2808
|
+
pbw_buf_free(&raw);
|
|
2809
|
+
return PBW_ERR_INVALID;
|
|
2810
|
+
}
|
|
2811
|
+
st = j_parse_double_value(j, (const char *)raw.data, raw.len,
|
|
2812
|
+
field->type == PBW_TYPE_FLOAT, &v);
|
|
2813
|
+
pbw_buf_free(&raw);
|
|
2814
|
+
} else {
|
|
2815
|
+
return PBW_ERR_INVALID;
|
|
2816
|
+
}
|
|
2817
|
+
if (st != PBW_OK) {
|
|
2818
|
+
return st;
|
|
2819
|
+
}
|
|
2820
|
+
if (field->type == PBW_TYPE_FLOAT) {
|
|
2821
|
+
/* Copy through a uint32 so the float lands in the low half of
|
|
2822
|
+
* `bits` on every host (a 4-byte memcpy into a uint64 fills the
|
|
2823
|
+
* high half on a big-endian one). */
|
|
2824
|
+
uint32_t bits32;
|
|
2825
|
+
float f = (float)v;
|
|
2826
|
+
|
|
2827
|
+
memcpy(&bits32, &f, sizeof(bits32));
|
|
2828
|
+
bits = bits32;
|
|
2829
|
+
} else {
|
|
2830
|
+
memcpy(&bits, &v, 8);
|
|
2831
|
+
}
|
|
2832
|
+
return j_store_uint(j, msg, field, bits);
|
|
2833
|
+
}
|
|
2834
|
+
case PBW_TYPE_INT64:
|
|
2835
|
+
case PBW_TYPE_SINT64:
|
|
2836
|
+
case PBW_TYPE_SFIXED64:
|
|
2837
|
+
case PBW_TYPE_UINT64:
|
|
2838
|
+
case PBW_TYPE_FIXED64:
|
|
2839
|
+
case PBW_TYPE_INT32:
|
|
2840
|
+
case PBW_TYPE_SINT32:
|
|
2841
|
+
case PBW_TYPE_SFIXED32:
|
|
2842
|
+
case PBW_TYPE_UINT32:
|
|
2843
|
+
case PBW_TYPE_FIXED32: {
|
|
2844
|
+
int is_signed = field->type != PBW_TYPE_UINT64 &&
|
|
2845
|
+
field->type != PBW_TYPE_FIXED64 &&
|
|
2846
|
+
field->type != PBW_TYPE_UINT32 &&
|
|
2847
|
+
field->type != PBW_TYPE_FIXED32;
|
|
2848
|
+
int bits32 = field->type == PBW_TYPE_INT32 ||
|
|
2849
|
+
field->type == PBW_TYPE_SINT32 ||
|
|
2850
|
+
field->type == PBW_TYPE_SFIXED32 ||
|
|
2851
|
+
field->type == PBW_TYPE_UINT32 ||
|
|
2852
|
+
field->type == PBW_TYPE_FIXED32;
|
|
2853
|
+
pbw_status st;
|
|
2854
|
+
|
|
2855
|
+
if (kind == JK_NUM) {
|
|
2856
|
+
const char *text;
|
|
2857
|
+
size_t len;
|
|
2858
|
+
|
|
2859
|
+
if (j_parse_raw(j, &text, &len) != PBW_OK) {
|
|
2860
|
+
return PBW_ERR_INVALID;
|
|
2861
|
+
}
|
|
2862
|
+
st = j_parse_int_value(j, text, len, 0, is_signed, bits32,
|
|
2863
|
+
&bits);
|
|
2864
|
+
} else if (kind == JK_STR) {
|
|
2865
|
+
pbw_buf raw;
|
|
2866
|
+
|
|
2867
|
+
pbw_buf_init(&raw);
|
|
2868
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
2869
|
+
pbw_buf_free(&raw);
|
|
2870
|
+
return PBW_ERR_INVALID;
|
|
2871
|
+
}
|
|
2872
|
+
st = j_parse_int_value(j, (const char *)raw.data, raw.len, 1,
|
|
2873
|
+
is_signed, bits32, &bits);
|
|
2874
|
+
pbw_buf_free(&raw);
|
|
2875
|
+
} else {
|
|
2876
|
+
return PBW_ERR_INVALID;
|
|
2877
|
+
}
|
|
2878
|
+
if (st != PBW_OK) {
|
|
2879
|
+
return st;
|
|
2880
|
+
}
|
|
2881
|
+
/* SINT stores decoded, like the binary decoder. */
|
|
2882
|
+
return j_store_uint(j, msg, field, bits);
|
|
2883
|
+
}
|
|
2884
|
+
case PBW_TYPE_BOOL: {
|
|
2885
|
+
int flag = 0;
|
|
2886
|
+
|
|
2887
|
+
if (kind == JK_TRUE) {
|
|
2888
|
+
if (j_expect_lit(j, "true") != PBW_OK) {
|
|
2889
|
+
return PBW_ERR_INVALID;
|
|
2890
|
+
}
|
|
2891
|
+
flag = 1;
|
|
2892
|
+
} else if (kind == JK_FALSE) {
|
|
2893
|
+
if (j_expect_lit(j, "false") != PBW_OK) {
|
|
2894
|
+
return PBW_ERR_INVALID;
|
|
2895
|
+
}
|
|
2896
|
+
} else if (kind == JK_STR) {
|
|
2897
|
+
pbw_buf raw;
|
|
2898
|
+
pbw_status st;
|
|
2899
|
+
|
|
2900
|
+
pbw_buf_init(&raw);
|
|
2901
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
2902
|
+
pbw_buf_free(&raw);
|
|
2903
|
+
return PBW_ERR_INVALID;
|
|
2904
|
+
}
|
|
2905
|
+
st = j_atob((const char *)raw.data, raw.len, &flag);
|
|
2906
|
+
pbw_buf_free(&raw);
|
|
2907
|
+
if (st != PBW_OK) {
|
|
2908
|
+
return st;
|
|
2909
|
+
}
|
|
2910
|
+
} else {
|
|
2911
|
+
return PBW_ERR_INVALID;
|
|
2912
|
+
}
|
|
2913
|
+
return j_store_uint(j, msg, field, flag ? 1 : 0);
|
|
2914
|
+
}
|
|
2915
|
+
case PBW_TYPE_STRING:
|
|
2916
|
+
case PBW_TYPE_BYTES: {
|
|
2917
|
+
pbw_buf raw;
|
|
2918
|
+
pbw_status st;
|
|
2919
|
+
|
|
2920
|
+
if (kind != JK_STR) {
|
|
2921
|
+
return PBW_ERR_INVALID;
|
|
2922
|
+
}
|
|
2923
|
+
pbw_buf_init(&raw);
|
|
2924
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
2925
|
+
pbw_buf_free(&raw);
|
|
2926
|
+
return PBW_ERR_INVALID;
|
|
2927
|
+
}
|
|
2928
|
+
if (field->type == PBW_TYPE_STRING) {
|
|
2929
|
+
st = j_store_bytes(j, msg, field, raw.data, raw.len);
|
|
2930
|
+
} else {
|
|
2931
|
+
pbw_buf decoded;
|
|
2932
|
+
|
|
2933
|
+
pbw_buf_init(&decoded);
|
|
2934
|
+
st = j_parse_base64((const char *)raw.data, raw.len, &decoded);
|
|
2935
|
+
if (st == PBW_OK) {
|
|
2936
|
+
st = j_store_bytes(j, msg, field, decoded.data,
|
|
2937
|
+
decoded.len);
|
|
2938
|
+
}
|
|
2939
|
+
pbw_buf_free(&decoded);
|
|
2940
|
+
}
|
|
2941
|
+
pbw_buf_free(&raw);
|
|
2942
|
+
return st;
|
|
2943
|
+
}
|
|
2944
|
+
case PBW_TYPE_ENUM:
|
|
2945
|
+
case PBW_TYPE_MESSAGE:
|
|
2946
|
+
case PBW_TYPE_GROUP:
|
|
2947
|
+
break;
|
|
2948
|
+
}
|
|
2949
|
+
return PBW_ERR_INVALID;
|
|
2950
|
+
}
|
|
2951
|
+
|
|
2952
|
+
/* Parses an enum value; `*stored` is 0 when an unknown name was skipped
|
|
2953
|
+
* under ignore_unknown (the caller stores nothing then). */
|
|
2954
|
+
static pbw_status
|
|
2955
|
+
j_parse_enum_value(j_in *j, pbw_message *msg, const pbw_field *field,
|
|
2956
|
+
int *stored)
|
|
2957
|
+
{
|
|
2958
|
+
j_kind kind = j_peek_kind(j);
|
|
2959
|
+
int32_t number = 0;
|
|
2960
|
+
|
|
2961
|
+
*stored = 1;
|
|
2962
|
+
if (kind == JK_STR) {
|
|
2963
|
+
pbw_buf raw;
|
|
2964
|
+
const char *text;
|
|
2965
|
+
size_t len;
|
|
2966
|
+
|
|
2967
|
+
pbw_buf_init(&raw);
|
|
2968
|
+
if (j_parse_string(j, &raw) != PBW_OK ||
|
|
2969
|
+
!pbw_buf_reserve(&raw, 1)) {
|
|
2970
|
+
pbw_buf_free(&raw);
|
|
2971
|
+
return PBW_ERR_INVALID;
|
|
2972
|
+
}
|
|
2973
|
+
raw.data[raw.len] = '\0';
|
|
2974
|
+
text = (const char *)raw.data;
|
|
2975
|
+
len = raw.len;
|
|
2976
|
+
if (field->sub != NULL &&
|
|
2977
|
+
pbw_enum_find_value(field->sub, text, &number)) {
|
|
2978
|
+
pbw_buf_free(&raw);
|
|
2979
|
+
} else {
|
|
2980
|
+
/* A quoted number spells the value; anything else is unknown. */
|
|
2981
|
+
const char *t = text;
|
|
2982
|
+
size_t n = len;
|
|
2983
|
+
int64_t v = 0;
|
|
2984
|
+
|
|
2985
|
+
j_trim(&t, &n);
|
|
2986
|
+
if (j_atoi64(t, n, &v) != PBW_OK || v < INT32_MIN ||
|
|
2987
|
+
v > INT32_MAX) {
|
|
2988
|
+
pbw_buf_free(&raw);
|
|
2989
|
+
if (j->ignore_unknown) {
|
|
2990
|
+
*stored = 0;
|
|
2991
|
+
return PBW_OK;
|
|
2992
|
+
}
|
|
2993
|
+
return PBW_ERR_INVALID;
|
|
2994
|
+
}
|
|
2995
|
+
number = (int32_t)v;
|
|
2996
|
+
pbw_buf_free(&raw);
|
|
2997
|
+
}
|
|
2998
|
+
} else if (kind == JK_NUM) {
|
|
2999
|
+
const char *text;
|
|
3000
|
+
size_t len;
|
|
3001
|
+
uint64_t bits = 0;
|
|
3002
|
+
|
|
3003
|
+
if (j_parse_raw(j, &text, &len) != PBW_OK) {
|
|
3004
|
+
return PBW_ERR_INVALID;
|
|
3005
|
+
}
|
|
3006
|
+
if (j_parse_int_value(j, text, len, 0, 1, 1, &bits) != PBW_OK) {
|
|
3007
|
+
return PBW_ERR_INVALID;
|
|
3008
|
+
}
|
|
3009
|
+
number = (int32_t)bits;
|
|
3010
|
+
} else if (kind == JK_NULL && field->sub != NULL &&
|
|
3011
|
+
j_classify(field->sub) == J_NULLVALUE) {
|
|
3012
|
+
/* `null` is the canonical JSON spelling of google.protobuf.NullValue,
|
|
3013
|
+
* the only enum that takes a non-string token: it means NULL_VALUE
|
|
3014
|
+
* (number 0), so a oneof member counts as selected. */
|
|
3015
|
+
if (j_expect_lit(j, "null") != PBW_OK) {
|
|
3016
|
+
return PBW_ERR_INVALID;
|
|
3017
|
+
}
|
|
3018
|
+
} else {
|
|
3019
|
+
return PBW_ERR_INVALID;
|
|
3020
|
+
}
|
|
3021
|
+
if (field->sub != NULL &&
|
|
3022
|
+
pbw_enum_find_name(field->sub, number, NULL)) {
|
|
3023
|
+
return j_store_uint(j, msg, field, (uint64_t)(int64_t)number);
|
|
3024
|
+
}
|
|
3025
|
+
/* Unknown number: open enums keep it, closed enums divert it. */
|
|
3026
|
+
if (field->sub == NULL || !pbw_enum_is_closed(field)) {
|
|
3027
|
+
return j_store_uint(j, msg, field, (uint64_t)(int64_t)number);
|
|
3028
|
+
}
|
|
3029
|
+
return j_store_unknown_enum(j, msg, field, number);
|
|
3030
|
+
}
|
|
3031
|
+
|
|
3032
|
+
/* ------------------------------------------------- message parsing */
|
|
3033
|
+
|
|
3034
|
+
/* Timestamp/Duration are recognized by name and stored by field number, so a
|
|
3035
|
+
* descriptor that carries those names without the real shape (a crafted or
|
|
3036
|
+
* hand-built schema) must fail instead of storing a value into a slot of
|
|
3037
|
+
* another kind. seconds is int64, nanos int32 in the real WKTs. */
|
|
3038
|
+
static const pbw_field *
|
|
3039
|
+
j_time_field(const pbw_descriptor *desc, uint32_t number, pbw_type type)
|
|
3040
|
+
{
|
|
3041
|
+
const pbw_field *field;
|
|
3042
|
+
|
|
3043
|
+
if (desc == NULL) {
|
|
3044
|
+
return NULL;
|
|
3045
|
+
}
|
|
3046
|
+
field = pbw_descriptor_find_field(desc, number);
|
|
3047
|
+
if (field == NULL || field->label == PBW_LABEL_REPEATED ||
|
|
3048
|
+
field->type != type) {
|
|
3049
|
+
return NULL;
|
|
3050
|
+
}
|
|
3051
|
+
return field;
|
|
3052
|
+
}
|
|
3053
|
+
|
|
3054
|
+
/* Well-known types with a string form. */
|
|
3055
|
+
static pbw_status
|
|
3056
|
+
j_parse_timestamp(j_in *j, pbw_message *msg)
|
|
3057
|
+
{
|
|
3058
|
+
pbw_buf raw;
|
|
3059
|
+
const pbw_field *secs_field;
|
|
3060
|
+
const pbw_field *nanos_field;
|
|
3061
|
+
const char *data;
|
|
3062
|
+
size_t left;
|
|
3063
|
+
uint32_t year = 0;
|
|
3064
|
+
uint32_t mon = 0;
|
|
3065
|
+
uint32_t day = 0;
|
|
3066
|
+
uint32_t hour = 0;
|
|
3067
|
+
uint32_t min = 0;
|
|
3068
|
+
uint32_t sec = 0;
|
|
3069
|
+
uint32_t frac = 0;
|
|
3070
|
+
size_t i;
|
|
3071
|
+
int64_t secs;
|
|
3072
|
+
uint32_t m_adj;
|
|
3073
|
+
uint32_t carry;
|
|
3074
|
+
uint32_t y_adj;
|
|
3075
|
+
uint32_t month_days;
|
|
3076
|
+
uint32_t leap_days;
|
|
3077
|
+
int64_t epoch_days;
|
|
3078
|
+
uint32_t parts[6];
|
|
3079
|
+
const char *suffix[6] = {"-", "-", "T", ":", ":", ""};
|
|
3080
|
+
|
|
3081
|
+
secs_field = j_time_field(msg->desc, 1, PBW_TYPE_INT64);
|
|
3082
|
+
nanos_field = j_time_field(msg->desc, 2, PBW_TYPE_INT32);
|
|
3083
|
+
if (secs_field == NULL || nanos_field == NULL) {
|
|
3084
|
+
return PBW_ERR_INVALID;
|
|
3085
|
+
}
|
|
3086
|
+
if (j_peek_kind(j) == JK_NULL) {
|
|
3087
|
+
return j_expect_lit(j, "null");
|
|
3088
|
+
}
|
|
3089
|
+
pbw_buf_init(&raw);
|
|
3090
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
3091
|
+
pbw_buf_free(&raw);
|
|
3092
|
+
return PBW_ERR_INVALID;
|
|
3093
|
+
}
|
|
3094
|
+
/* strptime-free: fixed offsets, exactly like the reference. */
|
|
3095
|
+
data = (const char *)raw.data;
|
|
3096
|
+
left = raw.len;
|
|
3097
|
+
if (left < 20) {
|
|
3098
|
+
pbw_buf_free(&raw);
|
|
3099
|
+
return PBW_ERR_INVALID;
|
|
3100
|
+
}
|
|
3101
|
+
for (i = 0; i < 6; i++) {
|
|
3102
|
+
int max = (i == 0) ? 4 : 2;
|
|
3103
|
+
int limit = max;
|
|
3104
|
+
uint32_t val = 0;
|
|
3105
|
+
|
|
3106
|
+
/* The limit check runs before the digit test (like TakeTimeDigits),
|
|
3107
|
+
* so a 5-digit year or 3-digit month fails even at the suffix. */
|
|
3108
|
+
while (left > 0) {
|
|
3109
|
+
unsigned char c = (unsigned char)*data;
|
|
3110
|
+
|
|
3111
|
+
if (limit-- < 0) {
|
|
3112
|
+
pbw_buf_free(&raw);
|
|
3113
|
+
return PBW_ERR_INVALID;
|
|
3114
|
+
}
|
|
3115
|
+
if (c < '0' || c > '9') {
|
|
3116
|
+
break;
|
|
3117
|
+
}
|
|
3118
|
+
val = val * 10 + (uint32_t)(c - '0');
|
|
3119
|
+
data++;
|
|
3120
|
+
left--;
|
|
3121
|
+
}
|
|
3122
|
+
if (suffix[i][0] != '\0') {
|
|
3123
|
+
if (left == 0 || *data != suffix[i][0]) {
|
|
3124
|
+
pbw_buf_free(&raw);
|
|
3125
|
+
return PBW_ERR_INVALID;
|
|
3126
|
+
}
|
|
3127
|
+
data++;
|
|
3128
|
+
left--;
|
|
3129
|
+
}
|
|
3130
|
+
parts[i] = val;
|
|
3131
|
+
}
|
|
3132
|
+
year = parts[0];
|
|
3133
|
+
mon = parts[1];
|
|
3134
|
+
day = parts[2];
|
|
3135
|
+
hour = parts[3];
|
|
3136
|
+
min = parts[4];
|
|
3137
|
+
sec = parts[5];
|
|
3138
|
+
/* The reference rejects anything a real calendar would: month 13, hour
|
|
3139
|
+
* 25, minute 61, February 30 and a leap day in a common year. Without
|
|
3140
|
+
* this the epoch-day arithmetic silently rolls the value over. */
|
|
3141
|
+
if (year == 0 || mon == 0 || day == 0 || mon > 12 || hour > 23 ||
|
|
3142
|
+
min > 59 || sec > 59) {
|
|
3143
|
+
pbw_buf_free(&raw);
|
|
3144
|
+
return PBW_ERR_INVALID;
|
|
3145
|
+
}
|
|
3146
|
+
{
|
|
3147
|
+
static const uint32_t days_in_month[13] = {0, 31, 28, 31, 30, 31, 30,
|
|
3148
|
+
31, 31, 30, 31, 30, 31};
|
|
3149
|
+
uint32_t days = days_in_month[mon];
|
|
3150
|
+
int leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
|
|
3151
|
+
|
|
3152
|
+
if (mon == 2 && leap) {
|
|
3153
|
+
days = 29;
|
|
3154
|
+
}
|
|
3155
|
+
if (day > days) {
|
|
3156
|
+
pbw_buf_free(&raw);
|
|
3157
|
+
return PBW_ERR_INVALID;
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
/* Fractional seconds: '.' plus 1-9 digits. */
|
|
3161
|
+
if (left > 0 && *data == '.') {
|
|
3162
|
+
size_t frac_digits = 0;
|
|
3163
|
+
|
|
3164
|
+
data++;
|
|
3165
|
+
left--;
|
|
3166
|
+
while (left > 0 && *data >= '0' && *data <= '9') {
|
|
3167
|
+
if (frac_digits >= 9) {
|
|
3168
|
+
pbw_buf_free(&raw);
|
|
3169
|
+
return PBW_ERR_INVALID;
|
|
3170
|
+
}
|
|
3171
|
+
frac = frac * 10 + (uint32_t)(*data - '0');
|
|
3172
|
+
frac_digits++;
|
|
3173
|
+
data++;
|
|
3174
|
+
left--;
|
|
3175
|
+
}
|
|
3176
|
+
if (frac_digits == 0) {
|
|
3177
|
+
pbw_buf_free(&raw);
|
|
3178
|
+
return PBW_ERR_INVALID;
|
|
3179
|
+
}
|
|
3180
|
+
for (i = frac_digits; i < 9; i++) {
|
|
3181
|
+
frac *= 10;
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
if (left == 0) {
|
|
3185
|
+
pbw_buf_free(&raw);
|
|
3186
|
+
return PBW_ERR_INVALID;
|
|
3187
|
+
}
|
|
3188
|
+
/* March-based month index, exactly like the reference: January and
|
|
3189
|
+
* February belong to the previous year. Spelled without the unsigned
|
|
3190
|
+
* wrap that `mon - 3` relies on, so the arithmetic is plain. */
|
|
3191
|
+
if (mon >= 3) {
|
|
3192
|
+
m_adj = mon - 3;
|
|
3193
|
+
carry = 0;
|
|
3194
|
+
} else {
|
|
3195
|
+
m_adj = mon + 9; /* January -> 10, February -> 11 */
|
|
3196
|
+
carry = 1;
|
|
3197
|
+
}
|
|
3198
|
+
y_adj = year + 4800 - carry;
|
|
3199
|
+
month_days = (m_adj * 62719 + 769) / 2048;
|
|
3200
|
+
leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
|
|
3201
|
+
/* int64 math: a date before 1970 lands below the 2472632 offset, and an
|
|
3202
|
+
* unsigned subtraction there would wrap and lean on the int32 conversion
|
|
3203
|
+
* to come back out negative. */
|
|
3204
|
+
epoch_days = (int64_t)y_adj * 365 + leap_days + month_days + (day - 1) -
|
|
3205
|
+
2472632;
|
|
3206
|
+
secs = epoch_days * 86400 + (int64_t)hour * 3600 + (int64_t)min * 60 +
|
|
3207
|
+
(int64_t)sec;
|
|
3208
|
+
if (*data == 'Z') {
|
|
3209
|
+
if (left != 1) {
|
|
3210
|
+
pbw_buf_free(&raw);
|
|
3211
|
+
return PBW_ERR_INVALID;
|
|
3212
|
+
}
|
|
3213
|
+
} else if (*data == '+' || *data == '-') {
|
|
3214
|
+
int neg = *data == '-';
|
|
3215
|
+
uint32_t off_hour = 0;
|
|
3216
|
+
uint32_t off_min = 0;
|
|
3217
|
+
int64_t offset;
|
|
3218
|
+
int limit;
|
|
3219
|
+
const char *off_start;
|
|
3220
|
+
|
|
3221
|
+
/* Exactly 6 chars (hh:mm); each part takes up to 3 digits. */
|
|
3222
|
+
if (left != 6) {
|
|
3223
|
+
pbw_buf_free(&raw);
|
|
3224
|
+
return PBW_ERR_INVALID;
|
|
3225
|
+
}
|
|
3226
|
+
data++;
|
|
3227
|
+
left--;
|
|
3228
|
+
off_start = data;
|
|
3229
|
+
limit = 2;
|
|
3230
|
+
while (left > 0) {
|
|
3231
|
+
if (limit-- < 0) {
|
|
3232
|
+
pbw_buf_free(&raw);
|
|
3233
|
+
return PBW_ERR_INVALID;
|
|
3234
|
+
}
|
|
3235
|
+
if (*data < '0' || *data > '9') {
|
|
3236
|
+
break;
|
|
3237
|
+
}
|
|
3238
|
+
off_hour = off_hour * 10 + (uint32_t)(*data - '0');
|
|
3239
|
+
data++;
|
|
3240
|
+
left--;
|
|
3241
|
+
}
|
|
3242
|
+
if (data == off_start || left == 0 || *data != ':') {
|
|
3243
|
+
pbw_buf_free(&raw);
|
|
3244
|
+
return PBW_ERR_INVALID;
|
|
3245
|
+
}
|
|
3246
|
+
data++;
|
|
3247
|
+
left--;
|
|
3248
|
+
limit = 2;
|
|
3249
|
+
while (left > 0) {
|
|
3250
|
+
if (limit-- < 0) {
|
|
3251
|
+
pbw_buf_free(&raw);
|
|
3252
|
+
return PBW_ERR_INVALID;
|
|
3253
|
+
}
|
|
3254
|
+
if (*data < '0' || *data > '9') {
|
|
3255
|
+
break;
|
|
3256
|
+
}
|
|
3257
|
+
off_min = off_min * 10 + (uint32_t)(*data - '0');
|
|
3258
|
+
data++;
|
|
3259
|
+
left--;
|
|
3260
|
+
}
|
|
3261
|
+
if (left != 0) {
|
|
3262
|
+
pbw_buf_free(&raw);
|
|
3263
|
+
return PBW_ERR_INVALID;
|
|
3264
|
+
}
|
|
3265
|
+
offset = ((int64_t)off_hour * 60 + (int64_t)off_min) * 60;
|
|
3266
|
+
secs += neg ? offset : -offset;
|
|
3267
|
+
} else {
|
|
3268
|
+
pbw_buf_free(&raw);
|
|
3269
|
+
return PBW_ERR_INVALID;
|
|
3270
|
+
}
|
|
3271
|
+
/* A valid date with a valid offset can still fall outside the Timestamp
|
|
3272
|
+
* range (0001-01-01 to 9999-12-31): the printer rejects such values,
|
|
3273
|
+
* so the parser must too. */
|
|
3274
|
+
if (secs < -62135596800ll || secs > 253402300799ll) {
|
|
3275
|
+
pbw_buf_free(&raw);
|
|
3276
|
+
return PBW_ERR_INVALID;
|
|
3277
|
+
}
|
|
3278
|
+
pbw_buf_free(&raw);
|
|
3279
|
+
if (j_store_uint(j, msg, secs_field, (uint64_t)secs) != PBW_OK ||
|
|
3280
|
+
j_store_uint(j, msg, nanos_field, frac) != PBW_OK) {
|
|
3281
|
+
return PBW_ERR_NOMEM;
|
|
3282
|
+
}
|
|
3283
|
+
return PBW_OK;
|
|
3284
|
+
}
|
|
3285
|
+
|
|
3286
|
+
static pbw_status
|
|
3287
|
+
j_parse_duration(j_in *j, pbw_message *msg)
|
|
3288
|
+
{
|
|
3289
|
+
pbw_buf raw;
|
|
3290
|
+
const char *text;
|
|
3291
|
+
size_t len;
|
|
3292
|
+
size_t int_end = 0;
|
|
3293
|
+
int64_t secs = 0;
|
|
3294
|
+
int32_t nanos = 0;
|
|
3295
|
+
int neg = 0;
|
|
3296
|
+
const char *rest;
|
|
3297
|
+
size_t rest_len;
|
|
3298
|
+
size_t i;
|
|
3299
|
+
const pbw_field *secs_field =
|
|
3300
|
+
j_time_field(msg->desc, 1, PBW_TYPE_INT64);
|
|
3301
|
+
const pbw_field *nanos_field =
|
|
3302
|
+
j_time_field(msg->desc, 2, PBW_TYPE_INT32);
|
|
3303
|
+
|
|
3304
|
+
if (secs_field == NULL || nanos_field == NULL) {
|
|
3305
|
+
return PBW_ERR_INVALID;
|
|
3306
|
+
}
|
|
3307
|
+
if (j_peek_kind(j) == JK_NULL) {
|
|
3308
|
+
return j_expect_lit(j, "null");
|
|
3309
|
+
}
|
|
3310
|
+
pbw_buf_init(&raw);
|
|
3311
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
3312
|
+
pbw_buf_free(&raw);
|
|
3313
|
+
return PBW_ERR_INVALID;
|
|
3314
|
+
}
|
|
3315
|
+
/* NUL-terminate for the integer scan. */
|
|
3316
|
+
if (!pbw_buf_reserve(&raw, 1)) {
|
|
3317
|
+
pbw_buf_free(&raw);
|
|
3318
|
+
return PBW_ERR_NOMEM;
|
|
3319
|
+
}
|
|
3320
|
+
raw.data[raw.len] = '\0';
|
|
3321
|
+
text = (const char *)raw.data;
|
|
3322
|
+
len = raw.len;
|
|
3323
|
+
while (int_end < len &&
|
|
3324
|
+
((text[int_end] >= '0' && text[int_end] <= '9') ||
|
|
3325
|
+
text[int_end] == '-')) {
|
|
3326
|
+
int_end++;
|
|
3327
|
+
}
|
|
3328
|
+
if (int_end == 0 || j_atoi64(text, int_end, &secs) != PBW_OK) {
|
|
3329
|
+
pbw_buf_free(&raw);
|
|
3330
|
+
return PBW_ERR_INVALID;
|
|
3331
|
+
}
|
|
3332
|
+
if (secs > (int64_t)3652500 * 86400 ||
|
|
3333
|
+
secs < -((int64_t)3652500 * 86400)) {
|
|
3334
|
+
pbw_buf_free(&raw);
|
|
3335
|
+
return PBW_ERR_INVALID;
|
|
3336
|
+
}
|
|
3337
|
+
rest = text + int_end;
|
|
3338
|
+
rest_len = len - int_end;
|
|
3339
|
+
nanos = 0;
|
|
3340
|
+
if (rest_len > 0 && rest[0] == '.') {
|
|
3341
|
+
size_t frac_digits = 0;
|
|
3342
|
+
uint32_t frac = 0;
|
|
3343
|
+
|
|
3344
|
+
rest++;
|
|
3345
|
+
rest_len--;
|
|
3346
|
+
while (rest_len > 0 && *rest >= '0' && *rest <= '9') {
|
|
3347
|
+
if (frac_digits >= 9) {
|
|
3348
|
+
pbw_buf_free(&raw);
|
|
3349
|
+
return PBW_ERR_INVALID;
|
|
3350
|
+
}
|
|
3351
|
+
frac = frac * 10 + (uint32_t)(*rest - '0');
|
|
3352
|
+
frac_digits++;
|
|
3353
|
+
rest++;
|
|
3354
|
+
rest_len--;
|
|
3355
|
+
}
|
|
3356
|
+
if (frac_digits == 0) {
|
|
3357
|
+
pbw_buf_free(&raw);
|
|
3358
|
+
return PBW_ERR_INVALID;
|
|
3359
|
+
}
|
|
3360
|
+
for (i = frac_digits; i < 9; i++) {
|
|
3361
|
+
frac *= 10;
|
|
3362
|
+
}
|
|
3363
|
+
nanos = (int32_t)frac;
|
|
3364
|
+
if (frac > 999999999) {
|
|
3365
|
+
pbw_buf_free(&raw);
|
|
3366
|
+
return PBW_ERR_INVALID;
|
|
3367
|
+
}
|
|
3368
|
+
}
|
|
3369
|
+
neg = secs < 0 || (int_end > 0 && text[0] == '-');
|
|
3370
|
+
if (neg) {
|
|
3371
|
+
nanos = -nanos;
|
|
3372
|
+
}
|
|
3373
|
+
if (rest_len != 1 || rest[0] != 's') {
|
|
3374
|
+
pbw_buf_free(&raw);
|
|
3375
|
+
return PBW_ERR_INVALID;
|
|
3376
|
+
}
|
|
3377
|
+
pbw_buf_free(&raw);
|
|
3378
|
+
if (j_store_uint(j, msg, secs_field, (uint64_t)secs) != PBW_OK ||
|
|
3379
|
+
j_store_uint(j, msg, nanos_field, (uint64_t)(int64_t)nanos) !=
|
|
3380
|
+
PBW_OK) {
|
|
3381
|
+
return PBW_ERR_NOMEM;
|
|
3382
|
+
}
|
|
3383
|
+
return PBW_OK;
|
|
3384
|
+
}
|
|
3385
|
+
|
|
3386
|
+
static pbw_status
|
|
3387
|
+
j_parse_fieldmask(j_in *j, pbw_message *msg)
|
|
3388
|
+
{
|
|
3389
|
+
pbw_buf raw;
|
|
3390
|
+
const pbw_field *paths;
|
|
3391
|
+
const char *p;
|
|
3392
|
+
const char *end;
|
|
3393
|
+
|
|
3394
|
+
if (j_peek_kind(j) == JK_NULL) {
|
|
3395
|
+
return j_expect_lit(j, "null");
|
|
3396
|
+
}
|
|
3397
|
+
pbw_buf_init(&raw);
|
|
3398
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
3399
|
+
pbw_buf_free(&raw);
|
|
3400
|
+
return PBW_ERR_INVALID;
|
|
3401
|
+
}
|
|
3402
|
+
paths = pbw_descriptor_find_field(msg->desc, 1);
|
|
3403
|
+
if (paths == NULL) {
|
|
3404
|
+
pbw_buf_free(&raw);
|
|
3405
|
+
return PBW_ERR_INVALID;
|
|
3406
|
+
}
|
|
3407
|
+
/* An empty string is an empty mask, not one empty path. */
|
|
3408
|
+
if (raw.len == 0) {
|
|
3409
|
+
pbw_buf_free(&raw);
|
|
3410
|
+
return PBW_OK;
|
|
3411
|
+
}
|
|
3412
|
+
p = (const char *)raw.data;
|
|
3413
|
+
end = p + raw.len;
|
|
3414
|
+
for (;;) {
|
|
3415
|
+
const char *comma = p;
|
|
3416
|
+
pbw_buf snake;
|
|
3417
|
+
|
|
3418
|
+
while (comma < end && *comma != ',') {
|
|
3419
|
+
comma++;
|
|
3420
|
+
}
|
|
3421
|
+
/* One path: uppercase becomes _lower, the rest passes through. */
|
|
3422
|
+
pbw_buf_init(&snake);
|
|
3423
|
+
for (; p < comma; p++) {
|
|
3424
|
+
char c = *p;
|
|
3425
|
+
|
|
3426
|
+
if (!pbw_buf_reserve(&snake, 2)) {
|
|
3427
|
+
pbw_buf_free(&snake);
|
|
3428
|
+
pbw_buf_free(&raw);
|
|
3429
|
+
return PBW_ERR_NOMEM;
|
|
3430
|
+
}
|
|
3431
|
+
if (c >= 'A' && c <= 'Z') {
|
|
3432
|
+
snake.data[snake.len++] = '_';
|
|
3433
|
+
snake.data[snake.len++] = (char)(c - 'A' + 'a');
|
|
3434
|
+
} else {
|
|
3435
|
+
snake.data[snake.len++] = (uint8_t)c;
|
|
3436
|
+
}
|
|
3437
|
+
}
|
|
3438
|
+
if (j_store_bytes(j, msg, paths, snake.data, snake.len) != PBW_OK) {
|
|
3439
|
+
pbw_buf_free(&snake);
|
|
3440
|
+
pbw_buf_free(&raw);
|
|
3441
|
+
return PBW_ERR_NOMEM;
|
|
3442
|
+
}
|
|
3443
|
+
pbw_buf_free(&snake);
|
|
3444
|
+
if (comma >= end) {
|
|
3445
|
+
break;
|
|
3446
|
+
}
|
|
3447
|
+
p = comma + 1;
|
|
3448
|
+
}
|
|
3449
|
+
pbw_buf_free(&raw);
|
|
3450
|
+
return PBW_OK;
|
|
3451
|
+
}
|
|
3452
|
+
|
|
3453
|
+
/* google.protobuf.Value from the upcoming JSON kind. */
|
|
3454
|
+
static pbw_status
|
|
3455
|
+
j_parse_value_msg(j_in *j, pbw_message *msg)
|
|
3456
|
+
{
|
|
3457
|
+
const pbw_field *f1 = pbw_descriptor_find_field(msg->desc, 1);
|
|
3458
|
+
const pbw_field *f2 = pbw_descriptor_find_field(msg->desc, 2);
|
|
3459
|
+
const pbw_field *f3 = pbw_descriptor_find_field(msg->desc, 3);
|
|
3460
|
+
const pbw_field *f4 = pbw_descriptor_find_field(msg->desc, 4);
|
|
3461
|
+
const pbw_field *f5 = pbw_descriptor_find_field(msg->desc, 5);
|
|
3462
|
+
const pbw_field *f6 = pbw_descriptor_find_field(msg->desc, 6);
|
|
3463
|
+
j_kind kind = j_peek_kind(j);
|
|
3464
|
+
|
|
3465
|
+
if (f1 == NULL || f2 == NULL || f3 == NULL || f4 == NULL || f5 == NULL ||
|
|
3466
|
+
f6 == NULL) {
|
|
3467
|
+
return PBW_ERR_INVALID;
|
|
3468
|
+
}
|
|
3469
|
+
switch (kind) {
|
|
3470
|
+
case JK_NULL:
|
|
3471
|
+
if (j_expect_lit(j, "null") != PBW_OK) {
|
|
3472
|
+
return PBW_ERR_INVALID;
|
|
3473
|
+
}
|
|
3474
|
+
return j_store_uint(j, msg, f1, 0);
|
|
3475
|
+
case JK_NUM: {
|
|
3476
|
+
const char *text;
|
|
3477
|
+
size_t len;
|
|
3478
|
+
double d;
|
|
3479
|
+
|
|
3480
|
+
if (j_parse_raw(j, &text, &len) != PBW_OK ||
|
|
3481
|
+
j_atod(text, len, &d) != PBW_OK) {
|
|
3482
|
+
return PBW_ERR_INVALID;
|
|
3483
|
+
}
|
|
3484
|
+
{
|
|
3485
|
+
pbw_value value;
|
|
3486
|
+
|
|
3487
|
+
value.kind = PBW_VALUE_UINT;
|
|
3488
|
+
value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
|
|
3489
|
+
memcpy(&value.as.u64, &d, 8);
|
|
3490
|
+
pbw_message_put(msg, (size_t)(f2 - msg->desc->fields), &value);
|
|
3491
|
+
}
|
|
3492
|
+
return PBW_OK;
|
|
3493
|
+
}
|
|
3494
|
+
case JK_STR: {
|
|
3495
|
+
pbw_buf raw;
|
|
3496
|
+
pbw_status st;
|
|
3497
|
+
|
|
3498
|
+
pbw_buf_init(&raw);
|
|
3499
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
3500
|
+
pbw_buf_free(&raw);
|
|
3501
|
+
return PBW_ERR_INVALID;
|
|
3502
|
+
}
|
|
3503
|
+
st = j_store_bytes(j, msg, f3, raw.data, raw.len);
|
|
3504
|
+
pbw_buf_free(&raw);
|
|
3505
|
+
return st;
|
|
3506
|
+
}
|
|
3507
|
+
case JK_TRUE:
|
|
3508
|
+
if (j_expect_lit(j, "true") != PBW_OK) {
|
|
3509
|
+
return PBW_ERR_INVALID;
|
|
3510
|
+
}
|
|
3511
|
+
return j_store_uint(j, msg, f4, 1);
|
|
3512
|
+
case JK_FALSE:
|
|
3513
|
+
if (j_expect_lit(j, "false") != PBW_OK) {
|
|
3514
|
+
return PBW_ERR_INVALID;
|
|
3515
|
+
}
|
|
3516
|
+
return j_store_uint(j, msg, f4, 0);
|
|
3517
|
+
case JK_OBJ: {
|
|
3518
|
+
pbw_message *sub = pbw_message_new(f5->sub);
|
|
3519
|
+
pbw_status st;
|
|
3520
|
+
|
|
3521
|
+
if (sub == NULL) {
|
|
3522
|
+
return PBW_ERR_NOMEM;
|
|
3523
|
+
}
|
|
3524
|
+
/* A depth-limit breach stays LIMIT so callers (and users) can
|
|
3525
|
+
* tell unbounded input from malformed input. */
|
|
3526
|
+
st = j_parse_message(j, sub);
|
|
3527
|
+
if (st != PBW_OK) {
|
|
3528
|
+
pbw_message_free(sub);
|
|
3529
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
3530
|
+
}
|
|
3531
|
+
return j_store_message(j, msg, f5, sub);
|
|
3532
|
+
}
|
|
3533
|
+
case JK_ARR: {
|
|
3534
|
+
pbw_message *sub = pbw_message_new(f6->sub);
|
|
3535
|
+
pbw_status st;
|
|
3536
|
+
|
|
3537
|
+
if (sub == NULL) {
|
|
3538
|
+
return PBW_ERR_NOMEM;
|
|
3539
|
+
}
|
|
3540
|
+
st = j_parse_message(j, sub);
|
|
3541
|
+
if (st != PBW_OK) {
|
|
3542
|
+
pbw_message_free(sub);
|
|
3543
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
3544
|
+
}
|
|
3545
|
+
return j_store_message(j, msg, f6, sub);
|
|
3546
|
+
}
|
|
3547
|
+
default:
|
|
3548
|
+
return PBW_ERR_INVALID;
|
|
3549
|
+
}
|
|
3550
|
+
}
|
|
3551
|
+
|
|
3552
|
+
/* Duplicate-key detection for JSON maps and Struct. One hash table over the
|
|
3553
|
+
* stored key values: scanning the whole entry list per key is quadratic in
|
|
3554
|
+
* the object size, and an object size is peer controlled. Runs after the object
|
|
3555
|
+
* has parsed, so an error simply drops the message. */
|
|
3556
|
+
static uint32_t
|
|
3557
|
+
j_key_hash(const pbw_value *k)
|
|
3558
|
+
{
|
|
3559
|
+
uint32_t h = 2166136261u;
|
|
3560
|
+
size_t i;
|
|
3561
|
+
|
|
3562
|
+
if (k->kind == PBW_VALUE_BYTES) {
|
|
3563
|
+
for (i = 0; i < k->as.bytes.len; i++) {
|
|
3564
|
+
h = (h ^ k->as.bytes.data[i]) * 16777619u;
|
|
3565
|
+
}
|
|
3566
|
+
return h;
|
|
3567
|
+
}
|
|
3568
|
+
{
|
|
3569
|
+
uint64_t v = k->as.u64;
|
|
3570
|
+
|
|
3571
|
+
for (i = 0; i < 8; i++) {
|
|
3572
|
+
h = (h ^ (uint32_t)(v & 0xffu)) * 16777619u;
|
|
3573
|
+
v >>= 8;
|
|
3574
|
+
}
|
|
3575
|
+
}
|
|
3576
|
+
return h;
|
|
3577
|
+
}
|
|
3578
|
+
|
|
3579
|
+
static int
|
|
3580
|
+
j_key_eq(const pbw_value *a, const pbw_value *b)
|
|
3581
|
+
{
|
|
3582
|
+
if (a->kind != b->kind) {
|
|
3583
|
+
return 0;
|
|
3584
|
+
}
|
|
3585
|
+
if (a->kind == PBW_VALUE_BYTES) {
|
|
3586
|
+
return a->as.bytes.len == b->as.bytes.len &&
|
|
3587
|
+
(a->as.bytes.len == 0 ||
|
|
3588
|
+
memcmp(a->as.bytes.data, b->as.bytes.data, a->as.bytes.len) ==
|
|
3589
|
+
0);
|
|
3590
|
+
}
|
|
3591
|
+
return a->as.u64 == b->as.u64;
|
|
3592
|
+
}
|
|
3593
|
+
|
|
3594
|
+
/* The key value of one stored entry, or NULL when it has none. */
|
|
3595
|
+
static const pbw_value *
|
|
3596
|
+
j_entry_key(const pbw_value *entry)
|
|
3597
|
+
{
|
|
3598
|
+
if (entry == NULL || entry->kind != PBW_VALUE_MESSAGE ||
|
|
3599
|
+
entry->as.msg == NULL) {
|
|
3600
|
+
return NULL;
|
|
3601
|
+
}
|
|
3602
|
+
return pbw_message_get(entry->as.msg, 1);
|
|
3603
|
+
}
|
|
3604
|
+
|
|
3605
|
+
static pbw_status
|
|
3606
|
+
j_check_duplicate_keys(const pbw_value *list)
|
|
3607
|
+
{
|
|
3608
|
+
size_t n, cap = 16, i;
|
|
3609
|
+
size_t *slots;
|
|
3610
|
+
|
|
3611
|
+
if (list == NULL || list->kind != PBW_VALUE_LIST || list->as.list.len < 2) {
|
|
3612
|
+
return PBW_OK;
|
|
3613
|
+
}
|
|
3614
|
+
if (list->as.list.items == NULL) {
|
|
3615
|
+
return PBW_ERR_INVALID;
|
|
3616
|
+
}
|
|
3617
|
+
n = list->as.list.len;
|
|
3618
|
+
while (cap < n * 2) {
|
|
3619
|
+
if (cap > SIZE_MAX / 2) {
|
|
3620
|
+
return PBW_ERR_NOMEM;
|
|
3621
|
+
}
|
|
3622
|
+
cap *= 2;
|
|
3623
|
+
}
|
|
3624
|
+
if (cap > SIZE_MAX / sizeof(*slots)) {
|
|
3625
|
+
return PBW_ERR_NOMEM;
|
|
3626
|
+
}
|
|
3627
|
+
slots = (size_t *)calloc(cap, sizeof(*slots));
|
|
3628
|
+
if (slots == NULL) {
|
|
3629
|
+
return PBW_ERR_NOMEM;
|
|
3630
|
+
}
|
|
3631
|
+
for (i = 0; i < n; i++) {
|
|
3632
|
+
const pbw_value *k = j_entry_key(list->as.list.items[i]);
|
|
3633
|
+
size_t slot;
|
|
3634
|
+
|
|
3635
|
+
if (k == NULL ||
|
|
3636
|
+
(k->kind != PBW_VALUE_BYTES && k->kind != PBW_VALUE_UINT)) {
|
|
3637
|
+
continue;
|
|
3638
|
+
}
|
|
3639
|
+
slot = (size_t)j_key_hash(k) & (cap - 1);
|
|
3640
|
+
for (;;) {
|
|
3641
|
+
if (slots[slot] == 0) {
|
|
3642
|
+
slots[slot] = i + 1;
|
|
3643
|
+
break;
|
|
3644
|
+
}
|
|
3645
|
+
{
|
|
3646
|
+
const pbw_value *k2 =
|
|
3647
|
+
j_entry_key(list->as.list.items[slots[slot] - 1]);
|
|
3648
|
+
|
|
3649
|
+
if (k2 != NULL && j_key_eq(k2, k)) {
|
|
3650
|
+
free(slots);
|
|
3651
|
+
return PBW_ERR_INVALID;
|
|
3652
|
+
}
|
|
3653
|
+
}
|
|
3654
|
+
slot = (slot + 1) & (cap - 1);
|
|
3655
|
+
}
|
|
3656
|
+
}
|
|
3657
|
+
free(slots);
|
|
3658
|
+
return PBW_OK;
|
|
3659
|
+
}
|
|
3660
|
+
|
|
3661
|
+
/* google.protobuf.Struct: a map<string, Value> in disguise. */
|
|
3662
|
+
static pbw_status
|
|
3663
|
+
j_parse_struct(j_in *j, pbw_message *msg)
|
|
3664
|
+
{
|
|
3665
|
+
const pbw_field *entry_field = pbw_descriptor_find_field(msg->desc, 1);
|
|
3666
|
+
const pbw_field *key_field;
|
|
3667
|
+
const pbw_field *value_field;
|
|
3668
|
+
const pbw_descriptor *entry;
|
|
3669
|
+
|
|
3670
|
+
if (entry_field == NULL || entry_field->sub == NULL) {
|
|
3671
|
+
return PBW_ERR_INVALID;
|
|
3672
|
+
}
|
|
3673
|
+
entry = entry_field->sub;
|
|
3674
|
+
key_field = pbw_descriptor_find_field(entry, 1);
|
|
3675
|
+
value_field = pbw_descriptor_find_field(entry, 2);
|
|
3676
|
+
if (key_field == NULL || value_field == NULL) {
|
|
3677
|
+
return PBW_ERR_INVALID;
|
|
3678
|
+
}
|
|
3679
|
+
if (j_peek_kind(j) == JK_NULL) {
|
|
3680
|
+
return j_expect_lit(j, "null");
|
|
3681
|
+
}
|
|
3682
|
+
if (j_expect_ch(j, '{') != PBW_OK) {
|
|
3683
|
+
return PBW_ERR_INVALID;
|
|
3684
|
+
}
|
|
3685
|
+
if (j->depth >= j->dlim) {
|
|
3686
|
+
return PBW_ERR_LIMIT;
|
|
3687
|
+
}
|
|
3688
|
+
j->depth++;
|
|
3689
|
+
j_skip(&j->cur);
|
|
3690
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
3691
|
+
j->cur.p++;
|
|
3692
|
+
j->depth--;
|
|
3693
|
+
return PBW_OK;
|
|
3694
|
+
}
|
|
3695
|
+
for (;;) {
|
|
3696
|
+
pbw_buf key;
|
|
3697
|
+
pbw_message *item;
|
|
3698
|
+
pbw_message *val;
|
|
3699
|
+
pbw_status st;
|
|
3700
|
+
|
|
3701
|
+
pbw_buf_init(&key);
|
|
3702
|
+
if (j_parse_key(j, &key) != PBW_OK) {
|
|
3703
|
+
pbw_buf_free(&key);
|
|
3704
|
+
j->depth--;
|
|
3705
|
+
return PBW_ERR_INVALID;
|
|
3706
|
+
}
|
|
3707
|
+
if (j_expect_ch(j, ':') != PBW_OK) {
|
|
3708
|
+
pbw_buf_free(&key);
|
|
3709
|
+
j->depth--;
|
|
3710
|
+
return PBW_ERR_INVALID;
|
|
3711
|
+
}
|
|
3712
|
+
item = pbw_message_new(entry);
|
|
3713
|
+
if (item == NULL) {
|
|
3714
|
+
pbw_buf_free(&key);
|
|
3715
|
+
j->depth--;
|
|
3716
|
+
return PBW_ERR_NOMEM;
|
|
3717
|
+
}
|
|
3718
|
+
st = j_store_bytes(j, item, key_field, key.data, key.len);
|
|
3719
|
+
pbw_buf_free(&key);
|
|
3720
|
+
if (st != PBW_OK) {
|
|
3721
|
+
pbw_message_free(item);
|
|
3722
|
+
j->depth--;
|
|
3723
|
+
return st;
|
|
3724
|
+
}
|
|
3725
|
+
val = pbw_message_new(value_field->sub);
|
|
3726
|
+
if (val == NULL) {
|
|
3727
|
+
pbw_message_free(item);
|
|
3728
|
+
j->depth--;
|
|
3729
|
+
return PBW_ERR_NOMEM;
|
|
3730
|
+
}
|
|
3731
|
+
/* The value parses as a Value message, like a map value would. */
|
|
3732
|
+
st = j_parse_message(j, val);
|
|
3733
|
+
if (st != PBW_OK) {
|
|
3734
|
+
pbw_message_free(val);
|
|
3735
|
+
pbw_message_free(item);
|
|
3736
|
+
j->depth--;
|
|
3737
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
3738
|
+
}
|
|
3739
|
+
st = j_store_message(j, item, value_field, val);
|
|
3740
|
+
if (st != PBW_OK) {
|
|
3741
|
+
pbw_message_free(item);
|
|
3742
|
+
j->depth--;
|
|
3743
|
+
return st;
|
|
3744
|
+
}
|
|
3745
|
+
st = j_store_message(j, msg, entry_field, item);
|
|
3746
|
+
if (st != PBW_OK) {
|
|
3747
|
+
j->depth--;
|
|
3748
|
+
return st;
|
|
3749
|
+
}
|
|
3750
|
+
j_skip(&j->cur);
|
|
3751
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
3752
|
+
j->cur.p++;
|
|
3753
|
+
/* Trailing commas are accepted in legacy mode. */
|
|
3754
|
+
j_skip(&j->cur);
|
|
3755
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
3756
|
+
pbw_status dup;
|
|
3757
|
+
|
|
3758
|
+
j->cur.p++;
|
|
3759
|
+
j->depth--;
|
|
3760
|
+
dup = j_check_duplicate_keys(
|
|
3761
|
+
pbw_message_get(msg, entry_field->number));
|
|
3762
|
+
return dup;
|
|
3763
|
+
}
|
|
3764
|
+
continue;
|
|
3765
|
+
}
|
|
3766
|
+
if (j_expect_ch(j, '}') != PBW_OK) {
|
|
3767
|
+
j->depth--;
|
|
3768
|
+
return PBW_ERR_INVALID;
|
|
3769
|
+
}
|
|
3770
|
+
{
|
|
3771
|
+
pbw_status dup = j_check_duplicate_keys(
|
|
3772
|
+
pbw_message_get(msg, entry_field->number));
|
|
3773
|
+
|
|
3774
|
+
j->depth--;
|
|
3775
|
+
return dup;
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
|
|
3780
|
+
/* Emits the reference's null element for repeated fields: a default value
|
|
3781
|
+
* (an empty message for message fields). */
|
|
3782
|
+
static pbw_status
|
|
3783
|
+
j_emit_null(j_in *j, pbw_message *msg, const pbw_field *field)
|
|
3784
|
+
{
|
|
3785
|
+
switch (field->type) {
|
|
3786
|
+
case PBW_TYPE_DOUBLE:
|
|
3787
|
+
case PBW_TYPE_FLOAT:
|
|
3788
|
+
case PBW_TYPE_INT64:
|
|
3789
|
+
case PBW_TYPE_SINT64:
|
|
3790
|
+
case PBW_TYPE_SFIXED64:
|
|
3791
|
+
case PBW_TYPE_UINT64:
|
|
3792
|
+
case PBW_TYPE_FIXED64:
|
|
3793
|
+
case PBW_TYPE_INT32:
|
|
3794
|
+
case PBW_TYPE_SINT32:
|
|
3795
|
+
case PBW_TYPE_SFIXED32:
|
|
3796
|
+
case PBW_TYPE_UINT32:
|
|
3797
|
+
case PBW_TYPE_FIXED32:
|
|
3798
|
+
case PBW_TYPE_BOOL:
|
|
3799
|
+
case PBW_TYPE_ENUM:
|
|
3800
|
+
return j_store_uint(j, msg, field, 0);
|
|
3801
|
+
case PBW_TYPE_STRING:
|
|
3802
|
+
case PBW_TYPE_BYTES:
|
|
3803
|
+
return j_store_bytes(j, msg, field, NULL, 0);
|
|
3804
|
+
case PBW_TYPE_MESSAGE:
|
|
3805
|
+
case PBW_TYPE_GROUP: {
|
|
3806
|
+
pbw_message *sub;
|
|
3807
|
+
|
|
3808
|
+
if (field->sub == NULL) {
|
|
3809
|
+
return PBW_ERR_INVALID;
|
|
3810
|
+
}
|
|
3811
|
+
sub = pbw_message_new(field->sub);
|
|
3812
|
+
if (sub == NULL) {
|
|
3813
|
+
return PBW_ERR_NOMEM;
|
|
3814
|
+
}
|
|
3815
|
+
return j_store_message(j, msg, field, sub);
|
|
3816
|
+
}
|
|
3817
|
+
}
|
|
3818
|
+
return PBW_ERR_INVALID;
|
|
3819
|
+
}
|
|
3820
|
+
|
|
3821
|
+
/* Parses one array element of a repeated field. */
|
|
3822
|
+
static pbw_status
|
|
3823
|
+
j_parse_element(j_in *j, pbw_message *msg, const pbw_field *field)
|
|
3824
|
+
{
|
|
3825
|
+
j_kind kind = j_peek_kind(j);
|
|
3826
|
+
j_wkt wkt = field->sub != NULL ? j_classify(field->sub) : J_NOT_WKT;
|
|
3827
|
+
|
|
3828
|
+
if (kind == JK_NULL) {
|
|
3829
|
+
if ((field->type == PBW_TYPE_MESSAGE && wkt == J_VALUE) ||
|
|
3830
|
+
(field->type == PBW_TYPE_ENUM && field->sub != NULL &&
|
|
3831
|
+
j_classify(field->sub) == J_NULLVALUE)) {
|
|
3832
|
+
return j_parse_field_value(j, msg, field, 1);
|
|
3833
|
+
}
|
|
3834
|
+
if (j_expect_lit(j, "null") != PBW_OK) {
|
|
3835
|
+
return PBW_ERR_INVALID;
|
|
3836
|
+
}
|
|
3837
|
+
return j_emit_null(j, msg, field);
|
|
3838
|
+
}
|
|
3839
|
+
/* Nested arrays flatten into the current one (legacy mode), except
|
|
3840
|
+
* inside Value and ListValue. */
|
|
3841
|
+
if (kind == JK_ARR && wkt != J_VALUE && wkt != J_LIST) {
|
|
3842
|
+
if (j_expect_ch(j, '[') != PBW_OK) {
|
|
3843
|
+
return PBW_ERR_INVALID;
|
|
3844
|
+
}
|
|
3845
|
+
if (j->depth >= j->dlim) {
|
|
3846
|
+
return PBW_ERR_LIMIT;
|
|
3847
|
+
}
|
|
3848
|
+
j->depth++;
|
|
3849
|
+
j_skip(&j->cur);
|
|
3850
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
3851
|
+
j->cur.p++;
|
|
3852
|
+
j->depth--;
|
|
3853
|
+
return PBW_OK;
|
|
3854
|
+
}
|
|
3855
|
+
for (;;) {
|
|
3856
|
+
pbw_status st = j_parse_element(j, msg, field);
|
|
3857
|
+
|
|
3858
|
+
if (st != PBW_OK) {
|
|
3859
|
+
j->depth--;
|
|
3860
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
3861
|
+
}
|
|
3862
|
+
j_skip(&j->cur);
|
|
3863
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
3864
|
+
j->cur.p++;
|
|
3865
|
+
j_skip(&j->cur);
|
|
3866
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
3867
|
+
j->cur.p++;
|
|
3868
|
+
j->depth--;
|
|
3869
|
+
return PBW_OK;
|
|
3870
|
+
}
|
|
3871
|
+
continue;
|
|
3872
|
+
}
|
|
3873
|
+
if (j_expect_ch(j, ']') != PBW_OK) {
|
|
3874
|
+
j->depth--;
|
|
3875
|
+
return PBW_ERR_INVALID;
|
|
3876
|
+
}
|
|
3877
|
+
j->depth--;
|
|
3878
|
+
return PBW_OK;
|
|
3879
|
+
}
|
|
3880
|
+
}
|
|
3881
|
+
return j_parse_field_value(j, msg, field, 1);
|
|
3882
|
+
}
|
|
3883
|
+
|
|
3884
|
+
static pbw_status
|
|
3885
|
+
j_parse_array(j_in *j, pbw_message *msg, const pbw_field *field)
|
|
3886
|
+
{
|
|
3887
|
+
if (j_expect_ch(j, '[') != PBW_OK) {
|
|
3888
|
+
return PBW_ERR_INVALID;
|
|
3889
|
+
}
|
|
3890
|
+
if (j->depth >= j->dlim) {
|
|
3891
|
+
return PBW_ERR_LIMIT;
|
|
3892
|
+
}
|
|
3893
|
+
j->depth++;
|
|
3894
|
+
j_skip(&j->cur);
|
|
3895
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
3896
|
+
j->cur.p++;
|
|
3897
|
+
j->depth--;
|
|
3898
|
+
return PBW_OK;
|
|
3899
|
+
}
|
|
3900
|
+
for (;;) {
|
|
3901
|
+
pbw_status st = j_parse_element(j, msg, field);
|
|
3902
|
+
|
|
3903
|
+
if (st != PBW_OK) {
|
|
3904
|
+
j->depth--;
|
|
3905
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
3906
|
+
}
|
|
3907
|
+
j_skip(&j->cur);
|
|
3908
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
3909
|
+
j->cur.p++;
|
|
3910
|
+
/* Trailing commas are accepted in legacy mode. */
|
|
3911
|
+
j_skip(&j->cur);
|
|
3912
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
3913
|
+
j->cur.p++;
|
|
3914
|
+
j->depth--;
|
|
3915
|
+
return PBW_OK;
|
|
3916
|
+
}
|
|
3917
|
+
continue;
|
|
3918
|
+
}
|
|
3919
|
+
if (j_expect_ch(j, ']') != PBW_OK) {
|
|
3920
|
+
j->depth--;
|
|
3921
|
+
return PBW_ERR_INVALID;
|
|
3922
|
+
}
|
|
3923
|
+
j->depth--;
|
|
3924
|
+
return PBW_OK;
|
|
3925
|
+
}
|
|
3926
|
+
}
|
|
3927
|
+
|
|
3928
|
+
/* Map keys: strict integers, exact bools, raw strings. */
|
|
3929
|
+
static pbw_status
|
|
3930
|
+
j_parse_map_key(const char *text, size_t len, const pbw_field *key_field,
|
|
3931
|
+
pbw_message *entry)
|
|
3932
|
+
{
|
|
3933
|
+
switch (key_field->type) {
|
|
3934
|
+
case PBW_TYPE_INT64:
|
|
3935
|
+
case PBW_TYPE_SINT64:
|
|
3936
|
+
case PBW_TYPE_SFIXED64:
|
|
3937
|
+
case PBW_TYPE_INT32:
|
|
3938
|
+
case PBW_TYPE_SINT32:
|
|
3939
|
+
case PBW_TYPE_SFIXED32: {
|
|
3940
|
+
const char *t = text;
|
|
3941
|
+
size_t n = len;
|
|
3942
|
+
int64_t v = 0;
|
|
3943
|
+
|
|
3944
|
+
j_trim(&t, &n);
|
|
3945
|
+
if (j_atoi64(t, n, &v) != PBW_OK) {
|
|
3946
|
+
return PBW_ERR_INVALID;
|
|
3947
|
+
}
|
|
3948
|
+
if ((key_field->type == PBW_TYPE_INT32 ||
|
|
3949
|
+
key_field->type == PBW_TYPE_SINT32 ||
|
|
3950
|
+
key_field->type == PBW_TYPE_SFIXED32) &&
|
|
3951
|
+
(v < INT32_MIN || v > INT32_MAX)) {
|
|
3952
|
+
return PBW_ERR_INVALID;
|
|
3953
|
+
}
|
|
3954
|
+
return j_store_uint(NULL, entry, key_field, (uint64_t)v);
|
|
3955
|
+
}
|
|
3956
|
+
case PBW_TYPE_UINT64:
|
|
3957
|
+
case PBW_TYPE_FIXED64:
|
|
3958
|
+
case PBW_TYPE_UINT32:
|
|
3959
|
+
case PBW_TYPE_FIXED32: {
|
|
3960
|
+
const char *t = text;
|
|
3961
|
+
size_t n = len;
|
|
3962
|
+
uint64_t v = 0;
|
|
3963
|
+
|
|
3964
|
+
j_trim(&t, &n);
|
|
3965
|
+
if (j_atou64(t, n, &v) != PBW_OK) {
|
|
3966
|
+
return PBW_ERR_INVALID;
|
|
3967
|
+
}
|
|
3968
|
+
if ((key_field->type == PBW_TYPE_UINT32 ||
|
|
3969
|
+
key_field->type == PBW_TYPE_FIXED32) &&
|
|
3970
|
+
v > UINT32_MAX) {
|
|
3971
|
+
return PBW_ERR_INVALID;
|
|
3972
|
+
}
|
|
3973
|
+
return j_store_uint(NULL, entry, key_field, v);
|
|
3974
|
+
}
|
|
3975
|
+
case PBW_TYPE_BOOL:
|
|
3976
|
+
/* Map keys want exactly "true"/"false", not the Atob set. */
|
|
3977
|
+
if (len == 4 && memcmp(text, "true", 4) == 0) {
|
|
3978
|
+
return j_store_uint(NULL, entry, key_field, 1);
|
|
3979
|
+
}
|
|
3980
|
+
if (len == 5 && memcmp(text, "false", 5) == 0) {
|
|
3981
|
+
return j_store_uint(NULL, entry, key_field, 0);
|
|
3982
|
+
}
|
|
3983
|
+
return PBW_ERR_INVALID;
|
|
3984
|
+
case PBW_TYPE_STRING:
|
|
3985
|
+
return j_store_bytes(NULL, entry, key_field, (const uint8_t *)text,
|
|
3986
|
+
len);
|
|
3987
|
+
default:
|
|
3988
|
+
return PBW_ERR_INVALID;
|
|
3989
|
+
}
|
|
3990
|
+
}
|
|
3991
|
+
|
|
3992
|
+
static pbw_status
|
|
3993
|
+
j_parse_map(j_in *j, pbw_message *msg, const pbw_field *field)
|
|
3994
|
+
{
|
|
3995
|
+
const pbw_descriptor *entry;
|
|
3996
|
+
const pbw_field *key_field;
|
|
3997
|
+
const pbw_field *value_field;
|
|
3998
|
+
|
|
3999
|
+
if (field->sub == NULL) {
|
|
4000
|
+
return PBW_ERR_INVALID;
|
|
4001
|
+
}
|
|
4002
|
+
entry = field->sub;
|
|
4003
|
+
key_field = pbw_descriptor_find_field(entry, 1);
|
|
4004
|
+
value_field = pbw_descriptor_find_field(entry, 2);
|
|
4005
|
+
if (key_field == NULL || value_field == NULL) {
|
|
4006
|
+
return PBW_ERR_INVALID;
|
|
4007
|
+
}
|
|
4008
|
+
if (j_expect_ch(j, '{') != PBW_OK) {
|
|
4009
|
+
return PBW_ERR_INVALID;
|
|
4010
|
+
}
|
|
4011
|
+
if (j->depth >= j->dlim) {
|
|
4012
|
+
return PBW_ERR_LIMIT;
|
|
4013
|
+
}
|
|
4014
|
+
j->depth++;
|
|
4015
|
+
j_skip(&j->cur);
|
|
4016
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
4017
|
+
j->cur.p++;
|
|
4018
|
+
j->depth--;
|
|
4019
|
+
return PBW_OK;
|
|
4020
|
+
}
|
|
4021
|
+
for (;;) {
|
|
4022
|
+
pbw_buf key;
|
|
4023
|
+
pbw_message *item;
|
|
4024
|
+
pbw_status st;
|
|
4025
|
+
|
|
4026
|
+
pbw_buf_init(&key);
|
|
4027
|
+
if (j_parse_key(j, &key) != PBW_OK) {
|
|
4028
|
+
pbw_buf_free(&key);
|
|
4029
|
+
j->depth--;
|
|
4030
|
+
return PBW_ERR_INVALID;
|
|
4031
|
+
}
|
|
4032
|
+
if (j_expect_ch(j, ':') != PBW_OK) {
|
|
4033
|
+
pbw_buf_free(&key);
|
|
4034
|
+
j->depth--;
|
|
4035
|
+
return PBW_ERR_INVALID;
|
|
4036
|
+
}
|
|
4037
|
+
item = pbw_message_new(entry);
|
|
4038
|
+
if (item == NULL) {
|
|
4039
|
+
pbw_buf_free(&key);
|
|
4040
|
+
j->depth--;
|
|
4041
|
+
return PBW_ERR_INVALID;
|
|
4042
|
+
}
|
|
4043
|
+
/* Keys compare by their stored form; integers normalize first, so
|
|
4044
|
+
* "1" and "01" collide exactly like two identical strings. */
|
|
4045
|
+
if (j_parse_map_key((const char *)key.data, key.len, key_field,
|
|
4046
|
+
item) != PBW_OK) {
|
|
4047
|
+
pbw_message_free(item);
|
|
4048
|
+
pbw_buf_free(&key);
|
|
4049
|
+
j->depth--;
|
|
4050
|
+
return PBW_ERR_INVALID;
|
|
4051
|
+
}
|
|
4052
|
+
pbw_buf_free(&key);
|
|
4053
|
+
if (value_field->type == PBW_TYPE_ENUM) {
|
|
4054
|
+
int stored = 0;
|
|
4055
|
+
|
|
4056
|
+
/* Unknown enum values are dropped with ignore_unknown. */
|
|
4057
|
+
if (j_parse_enum_value(j, item, value_field, &stored) !=
|
|
4058
|
+
PBW_OK) {
|
|
4059
|
+
pbw_message_free(item);
|
|
4060
|
+
j->depth--;
|
|
4061
|
+
return PBW_ERR_INVALID;
|
|
4062
|
+
}
|
|
4063
|
+
if (!stored) {
|
|
4064
|
+
pbw_message_free(item);
|
|
4065
|
+
} else {
|
|
4066
|
+
st = j_store_message(j, msg, field, item);
|
|
4067
|
+
if (st != PBW_OK) {
|
|
4068
|
+
j->depth--;
|
|
4069
|
+
return st;
|
|
4070
|
+
}
|
|
4071
|
+
}
|
|
4072
|
+
} else if (value_field->type == PBW_TYPE_MESSAGE ||
|
|
4073
|
+
value_field->type == PBW_TYPE_GROUP) {
|
|
4074
|
+
pbw_message *sub = pbw_message_new(value_field->sub);
|
|
4075
|
+
|
|
4076
|
+
if (sub == NULL) {
|
|
4077
|
+
pbw_message_free(item);
|
|
4078
|
+
j->depth--;
|
|
4079
|
+
return PBW_ERR_NOMEM;
|
|
4080
|
+
}
|
|
4081
|
+
{
|
|
4082
|
+
pbw_status pst = j_parse_message(j, sub);
|
|
4083
|
+
|
|
4084
|
+
if (pst != PBW_OK) {
|
|
4085
|
+
pbw_message_free(sub);
|
|
4086
|
+
pbw_message_free(item);
|
|
4087
|
+
j->depth--;
|
|
4088
|
+
return pst == PBW_ERR_LIMIT ? pst : PBW_ERR_INVALID;
|
|
4089
|
+
}
|
|
4090
|
+
}
|
|
4091
|
+
st = j_store_message(j, item, value_field, sub);
|
|
4092
|
+
if (st != PBW_OK) {
|
|
4093
|
+
pbw_message_free(item);
|
|
4094
|
+
j->depth--;
|
|
4095
|
+
return st;
|
|
4096
|
+
}
|
|
4097
|
+
st = j_store_message(j, msg, field, item);
|
|
4098
|
+
if (st != PBW_OK) {
|
|
4099
|
+
j->depth--;
|
|
4100
|
+
return st;
|
|
4101
|
+
}
|
|
4102
|
+
} else {
|
|
4103
|
+
if (j_parse_scalar_value(j, item, value_field) != PBW_OK) {
|
|
4104
|
+
pbw_message_free(item);
|
|
4105
|
+
j->depth--;
|
|
4106
|
+
return PBW_ERR_INVALID;
|
|
4107
|
+
}
|
|
4108
|
+
st = j_store_message(j, msg, field, item);
|
|
4109
|
+
if (st != PBW_OK) {
|
|
4110
|
+
j->depth--;
|
|
4111
|
+
return st;
|
|
4112
|
+
}
|
|
4113
|
+
}
|
|
4114
|
+
j_skip(&j->cur);
|
|
4115
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
4116
|
+
j->cur.p++;
|
|
4117
|
+
/* Trailing commas are accepted in legacy mode. */
|
|
4118
|
+
j_skip(&j->cur);
|
|
4119
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
4120
|
+
pbw_status dup;
|
|
4121
|
+
|
|
4122
|
+
j->cur.p++;
|
|
4123
|
+
j->depth--;
|
|
4124
|
+
dup = j_check_duplicate_keys(
|
|
4125
|
+
pbw_message_get(msg, field->number));
|
|
4126
|
+
return dup;
|
|
4127
|
+
}
|
|
4128
|
+
continue;
|
|
4129
|
+
}
|
|
4130
|
+
if (j_expect_ch(j, '}') != PBW_OK) {
|
|
4131
|
+
j->depth--;
|
|
4132
|
+
return PBW_ERR_INVALID;
|
|
4133
|
+
}
|
|
4134
|
+
{
|
|
4135
|
+
pbw_status dup =
|
|
4136
|
+
j_check_duplicate_keys(pbw_message_get(msg, field->number));
|
|
4137
|
+
j->depth--;
|
|
4138
|
+
return dup;
|
|
4139
|
+
}
|
|
4140
|
+
}
|
|
4141
|
+
}
|
|
4142
|
+
|
|
4143
|
+
/* google.protobuf.Any: buffer the object, find @type, then parse. */
|
|
4144
|
+
static pbw_status
|
|
4145
|
+
j_parse_any(j_in *j, pbw_message *msg)
|
|
4146
|
+
{
|
|
4147
|
+
const pbw_field *url_field = pbw_descriptor_find_field(msg->desc, 1);
|
|
4148
|
+
const pbw_field *value_field = pbw_descriptor_find_field(msg->desc, 2);
|
|
4149
|
+
const char *start;
|
|
4150
|
+
const char *end;
|
|
4151
|
+
int depth0;
|
|
4152
|
+
const uint8_t *url = NULL;
|
|
4153
|
+
size_t url_len = 0;
|
|
4154
|
+
const pbw_descriptor *resolved = NULL;
|
|
4155
|
+
pbw_message *sub = NULL;
|
|
4156
|
+
pbw_buf buf;
|
|
4157
|
+
uint8_t *bytes = NULL;
|
|
4158
|
+
size_t bytes_len = 0;
|
|
4159
|
+
pbw_status st;
|
|
4160
|
+
|
|
4161
|
+
/* The URL is stored verbatim (a type_url has no length limit, so it can
|
|
4162
|
+
* be longer than any field name), so the shape of its two fields is
|
|
4163
|
+
* checked here: a crafted Any must fail, not store a value of the wrong
|
|
4164
|
+
* kind in a slot. */
|
|
4165
|
+
if (url_field == NULL || value_field == NULL ||
|
|
4166
|
+
url_field->type != PBW_TYPE_STRING ||
|
|
4167
|
+
url_field->label == PBW_LABEL_REPEATED ||
|
|
4168
|
+
value_field->type != PBW_TYPE_BYTES ||
|
|
4169
|
+
value_field->label == PBW_LABEL_REPEATED) {
|
|
4170
|
+
return PBW_ERR_INVALID;
|
|
4171
|
+
}
|
|
4172
|
+
/* Scan the object once to find @type (values are skipped). */
|
|
4173
|
+
if (j_expect_ch(j, '{') != PBW_OK) {
|
|
4174
|
+
return PBW_ERR_INVALID;
|
|
4175
|
+
}
|
|
4176
|
+
depth0 = j->depth;
|
|
4177
|
+
start = j->cur.p;
|
|
4178
|
+
if (j->depth >= j->dlim) {
|
|
4179
|
+
return PBW_ERR_LIMIT;
|
|
4180
|
+
}
|
|
4181
|
+
j->depth++;
|
|
4182
|
+
j_skip(&j->cur);
|
|
4183
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
4184
|
+
j->cur.p++;
|
|
4185
|
+
j->depth--;
|
|
4186
|
+
/* Empty objects parse as an empty Any in legacy mode. */
|
|
4187
|
+
return PBW_OK;
|
|
4188
|
+
}
|
|
4189
|
+
for (;;) {
|
|
4190
|
+
pbw_buf key;
|
|
4191
|
+
int is_type;
|
|
4192
|
+
|
|
4193
|
+
pbw_buf_init(&key);
|
|
4194
|
+
if (j_parse_key(j, &key) != PBW_OK) {
|
|
4195
|
+
pbw_buf_free(&key);
|
|
4196
|
+
j->depth = depth0;
|
|
4197
|
+
return PBW_ERR_INVALID;
|
|
4198
|
+
}
|
|
4199
|
+
if (j_expect_ch(j, ':') != PBW_OK) {
|
|
4200
|
+
pbw_buf_free(&key);
|
|
4201
|
+
j->depth = depth0;
|
|
4202
|
+
return PBW_ERR_INVALID;
|
|
4203
|
+
}
|
|
4204
|
+
is_type = key.len == 5 && memcmp(key.data, "@type", 5) == 0;
|
|
4205
|
+
if (is_type) {
|
|
4206
|
+
pbw_buf raw;
|
|
4207
|
+
pbw_status ust;
|
|
4208
|
+
|
|
4209
|
+
if (pbw_message_get(msg, url_field->number) != NULL) {
|
|
4210
|
+
/* like the reference, a second expansion is an error */
|
|
4211
|
+
pbw_buf_free(&key);
|
|
4212
|
+
j->depth = depth0;
|
|
4213
|
+
return PBW_ERR_INVALID;
|
|
4214
|
+
}
|
|
4215
|
+
pbw_buf_init(&raw);
|
|
4216
|
+
if (j_parse_string(j, &raw) != PBW_OK) {
|
|
4217
|
+
pbw_buf_free(&raw);
|
|
4218
|
+
pbw_buf_free(&key);
|
|
4219
|
+
j->depth = depth0;
|
|
4220
|
+
return PBW_ERR_INVALID;
|
|
4221
|
+
}
|
|
4222
|
+
/* The stored type_url is the full URL; resolution strips it. */
|
|
4223
|
+
ust = j_store_bytes(j, msg, url_field, raw.data, raw.len);
|
|
4224
|
+
pbw_buf_free(&raw);
|
|
4225
|
+
if (ust != PBW_OK) {
|
|
4226
|
+
pbw_buf_free(&key);
|
|
4227
|
+
j->depth = depth0;
|
|
4228
|
+
return ust;
|
|
4229
|
+
}
|
|
4230
|
+
} else {
|
|
4231
|
+
pbw_status sst = j_skip_value(j);
|
|
4232
|
+
|
|
4233
|
+
if (sst != PBW_OK) {
|
|
4234
|
+
pbw_buf_free(&key);
|
|
4235
|
+
j->depth = depth0;
|
|
4236
|
+
return sst == PBW_ERR_LIMIT ? sst : PBW_ERR_INVALID;
|
|
4237
|
+
}
|
|
4238
|
+
}
|
|
4239
|
+
pbw_buf_free(&key);
|
|
4240
|
+
j_skip(&j->cur);
|
|
4241
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
4242
|
+
j->cur.p++;
|
|
4243
|
+
j_skip(&j->cur);
|
|
4244
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
4245
|
+
j->cur.p++;
|
|
4246
|
+
break;
|
|
4247
|
+
}
|
|
4248
|
+
continue;
|
|
4249
|
+
}
|
|
4250
|
+
if (j_expect_ch(j, '}') != PBW_OK) {
|
|
4251
|
+
j->depth = depth0;
|
|
4252
|
+
return PBW_ERR_INVALID;
|
|
4253
|
+
}
|
|
4254
|
+
break;
|
|
4255
|
+
}
|
|
4256
|
+
end = j->cur.p;
|
|
4257
|
+
j->depth = depth0;
|
|
4258
|
+
url = pbw_value_get_bytes(pbw_message_get(msg, url_field->number),
|
|
4259
|
+
&url_len);
|
|
4260
|
+
if (url == NULL || url_len == 0) {
|
|
4261
|
+
/* A non-empty object without @type fails. */
|
|
4262
|
+
return PBW_ERR_INVALID;
|
|
4263
|
+
}
|
|
4264
|
+
/* The type name is whatever follows the last '/'; the stored type_url
|
|
4265
|
+
* keeps the full URL. */
|
|
4266
|
+
{
|
|
4267
|
+
const uint8_t *slash = NULL;
|
|
4268
|
+
/* Room for the longest message name the loader accepts (512). */
|
|
4269
|
+
char name[520];
|
|
4270
|
+
size_t off;
|
|
4271
|
+
size_t i;
|
|
4272
|
+
|
|
4273
|
+
for (i = 0; i < url_len; i++) {
|
|
4274
|
+
if (url[i] == '/') {
|
|
4275
|
+
slash = url + i;
|
|
4276
|
+
}
|
|
4277
|
+
}
|
|
4278
|
+
off = slash == NULL ? 0 : (size_t)(slash - url) + 1;
|
|
4279
|
+
if (url_len - off >= sizeof(name)) {
|
|
4280
|
+
return PBW_ERR_INVALID;
|
|
4281
|
+
}
|
|
4282
|
+
memcpy(name, url + off, url_len - off);
|
|
4283
|
+
name[url_len - off] = '\0';
|
|
4284
|
+
resolved = msg->desc->schema == NULL
|
|
4285
|
+
? NULL
|
|
4286
|
+
: pbw_schema_find(msg->desc->schema, name);
|
|
4287
|
+
}
|
|
4288
|
+
if (resolved == NULL || resolved->is_enum) {
|
|
4289
|
+
/* An unresolvable (or enum) @type is a parse error even with
|
|
4290
|
+
* `ignore_unknown_fields`: the reference resolves the type before it
|
|
4291
|
+
* consults the option (protobuf 36.2 json parses `@type` into a
|
|
4292
|
+
* dynamic message and fails with INVALID_ARGUMENT). */
|
|
4293
|
+
return PBW_ERR_INVALID;
|
|
4294
|
+
}
|
|
4295
|
+
sub = pbw_message_new(resolved);
|
|
4296
|
+
if (sub == NULL) {
|
|
4297
|
+
return PBW_ERR_NOMEM;
|
|
4298
|
+
}
|
|
4299
|
+
/* Re-walk the buffered object, skipping @type. */
|
|
4300
|
+
{
|
|
4301
|
+
j_in inner;
|
|
4302
|
+
j_wkt wkt = j_classify(resolved);
|
|
4303
|
+
|
|
4304
|
+
inner.cur.p = start;
|
|
4305
|
+
inner.cur.end = end;
|
|
4306
|
+
inner.ignore_unknown = j->ignore_unknown;
|
|
4307
|
+
inner.depth = j->depth + 1;
|
|
4308
|
+
inner.dlim = j->dlim;
|
|
4309
|
+
/* start already points past the '{' the scan consumed. */
|
|
4310
|
+
for (;;) {
|
|
4311
|
+
pbw_buf key;
|
|
4312
|
+
int is_type;
|
|
4313
|
+
int is_value;
|
|
4314
|
+
|
|
4315
|
+
j_skip(&inner.cur);
|
|
4316
|
+
if (inner.cur.p < inner.cur.end && *inner.cur.p == '}') {
|
|
4317
|
+
inner.cur.p++;
|
|
4318
|
+
break;
|
|
4319
|
+
}
|
|
4320
|
+
pbw_buf_init(&key);
|
|
4321
|
+
if (j_parse_key(&inner, &key) != PBW_OK ||
|
|
4322
|
+
j_expect_ch(&inner, ':') != PBW_OK) {
|
|
4323
|
+
pbw_buf_free(&key);
|
|
4324
|
+
pbw_message_free(sub);
|
|
4325
|
+
return PBW_ERR_INVALID;
|
|
4326
|
+
}
|
|
4327
|
+
is_type = key.len == 5 && memcmp(key.data, "@type", 5) == 0;
|
|
4328
|
+
is_value = key.len == 5 && memcmp(key.data, "value", 5) == 0;
|
|
4329
|
+
if (is_type) {
|
|
4330
|
+
pbw_buf_free(&key);
|
|
4331
|
+
if (j_skip_value(&inner) != PBW_OK) {
|
|
4332
|
+
pbw_message_free(sub);
|
|
4333
|
+
return PBW_ERR_INVALID;
|
|
4334
|
+
}
|
|
4335
|
+
} else if (wkt != J_NOT_WKT) {
|
|
4336
|
+
/* Well-known payloads live under a "value" key. */
|
|
4337
|
+
pbw_buf_free(&key);
|
|
4338
|
+
if (!is_value) {
|
|
4339
|
+
pbw_message_free(sub);
|
|
4340
|
+
return PBW_ERR_INVALID;
|
|
4341
|
+
}
|
|
4342
|
+
{
|
|
4343
|
+
pbw_status awst = j_parse_message(&inner, sub);
|
|
4344
|
+
|
|
4345
|
+
if (awst != PBW_OK) {
|
|
4346
|
+
pbw_message_free(sub);
|
|
4347
|
+
return awst == PBW_ERR_LIMIT ? awst
|
|
4348
|
+
: PBW_ERR_INVALID;
|
|
4349
|
+
}
|
|
4350
|
+
}
|
|
4351
|
+
} else {
|
|
4352
|
+
/* Regular fields parse by name, like the outer object. */
|
|
4353
|
+
pbw_status fst = j_parse_named_field(&inner, sub, key.data,
|
|
4354
|
+
key.len);
|
|
4355
|
+
|
|
4356
|
+
pbw_buf_free(&key);
|
|
4357
|
+
if (fst != PBW_OK) {
|
|
4358
|
+
pbw_message_free(sub);
|
|
4359
|
+
return fst;
|
|
4360
|
+
}
|
|
4361
|
+
}
|
|
4362
|
+
j_skip(&inner.cur);
|
|
4363
|
+
if (inner.cur.p < inner.cur.end && *inner.cur.p == ',') {
|
|
4364
|
+
inner.cur.p++;
|
|
4365
|
+
continue;
|
|
4366
|
+
}
|
|
4367
|
+
if (j_expect_ch(&inner, '}') != PBW_OK) {
|
|
4368
|
+
pbw_message_free(sub);
|
|
4369
|
+
return PBW_ERR_INVALID;
|
|
4370
|
+
}
|
|
4371
|
+
break;
|
|
4372
|
+
}
|
|
4373
|
+
}
|
|
4374
|
+
/* Serialize the expansion into the Any's value bytes. */
|
|
4375
|
+
pbw_buf_init(&buf);
|
|
4376
|
+
st = pbw_encode(sub, &bytes, &bytes_len);
|
|
4377
|
+
pbw_message_free(sub);
|
|
4378
|
+
if (st != PBW_OK) {
|
|
4379
|
+
return st;
|
|
4380
|
+
}
|
|
4381
|
+
if (bytes_len > 0 &&
|
|
4382
|
+
j_store_bytes(j, msg, value_field, bytes, bytes_len) != PBW_OK) {
|
|
4383
|
+
pbw_free(bytes);
|
|
4384
|
+
return PBW_ERR_NOMEM;
|
|
4385
|
+
}
|
|
4386
|
+
pbw_free(bytes);
|
|
4387
|
+
return PBW_OK;
|
|
4388
|
+
}
|
|
4389
|
+
|
|
4390
|
+
/* Parses the value of one already-looked-up key. */
|
|
4391
|
+
static pbw_status
|
|
4392
|
+
j_parse_named_field(j_in *j, pbw_message *msg, const uint8_t *key,
|
|
4393
|
+
size_t len)
|
|
4394
|
+
{
|
|
4395
|
+
const pbw_field *field = NULL;
|
|
4396
|
+
|
|
4397
|
+
if (len >= 2 && key[0] == '[' && key[len - 1] == ']') {
|
|
4398
|
+
/* Extension names are dotted and global, like the text format:
|
|
4399
|
+
* room for scope + '.' + name, both capped at 512 bytes. */
|
|
4400
|
+
char name[1028];
|
|
4401
|
+
const pbw_field *ext;
|
|
4402
|
+
|
|
4403
|
+
if (len - 2 >= sizeof(name)) {
|
|
4404
|
+
return PBW_ERR_INVALID;
|
|
4405
|
+
}
|
|
4406
|
+
memcpy(name, key + 1, len - 2);
|
|
4407
|
+
name[len - 2] = '\0';
|
|
4408
|
+
ext = msg->desc->schema == NULL
|
|
4409
|
+
? NULL
|
|
4410
|
+
: pbw_schema_find_extension(msg->desc->schema, name);
|
|
4411
|
+
if (ext == NULL) {
|
|
4412
|
+
if (j->ignore_unknown) {
|
|
4413
|
+
return j_skip_value(j);
|
|
4414
|
+
}
|
|
4415
|
+
return PBW_ERR_INVALID;
|
|
4416
|
+
}
|
|
4417
|
+
/* The extension must belong to this message. */
|
|
4418
|
+
if (pbw_descriptor_find_field(msg->desc, ext->number) != ext) {
|
|
4419
|
+
return PBW_ERR_INVALID;
|
|
4420
|
+
}
|
|
4421
|
+
field = ext;
|
|
4422
|
+
} else if (!j_field_by_name(msg->desc, (const char *)key, len, &field)) {
|
|
4423
|
+
if (j->ignore_unknown) {
|
|
4424
|
+
return j_skip_value(j);
|
|
4425
|
+
}
|
|
4426
|
+
return PBW_ERR_INVALID;
|
|
4427
|
+
}
|
|
4428
|
+
return j_parse_field_value(j, msg, field, 0);
|
|
4429
|
+
}
|
|
4430
|
+
|
|
4431
|
+
/* The `{...}` body after the brace is consumed. */
|
|
4432
|
+
static pbw_status
|
|
4433
|
+
j_parse_object_body(j_in *j, pbw_message *msg)
|
|
4434
|
+
{
|
|
4435
|
+
if (j->depth >= j->dlim) {
|
|
4436
|
+
return PBW_ERR_LIMIT;
|
|
4437
|
+
}
|
|
4438
|
+
j->depth++;
|
|
4439
|
+
j_skip(&j->cur);
|
|
4440
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
4441
|
+
j->cur.p++;
|
|
4442
|
+
j->depth--;
|
|
4443
|
+
return PBW_OK;
|
|
4444
|
+
}
|
|
4445
|
+
for (;;) {
|
|
4446
|
+
pbw_buf key;
|
|
4447
|
+
pbw_status st;
|
|
4448
|
+
|
|
4449
|
+
pbw_buf_init(&key);
|
|
4450
|
+
if (j_parse_key(j, &key) != PBW_OK) {
|
|
4451
|
+
pbw_buf_free(&key);
|
|
4452
|
+
j->depth--;
|
|
4453
|
+
return PBW_ERR_INVALID;
|
|
4454
|
+
}
|
|
4455
|
+
if (j_expect_ch(j, ':') != PBW_OK) {
|
|
4456
|
+
pbw_buf_free(&key);
|
|
4457
|
+
j->depth--;
|
|
4458
|
+
return PBW_ERR_INVALID;
|
|
4459
|
+
}
|
|
4460
|
+
st = j_parse_named_field(j, msg, key.data, key.len);
|
|
4461
|
+
pbw_buf_free(&key);
|
|
4462
|
+
if (st != PBW_OK) {
|
|
4463
|
+
j->depth--;
|
|
4464
|
+
return st;
|
|
4465
|
+
}
|
|
4466
|
+
j_skip(&j->cur);
|
|
4467
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
4468
|
+
j->cur.p++;
|
|
4469
|
+
/* Trailing commas are accepted in legacy mode. */
|
|
4470
|
+
j_skip(&j->cur);
|
|
4471
|
+
if (j->cur.p < j->cur.end && *j->cur.p == '}') {
|
|
4472
|
+
j->cur.p++;
|
|
4473
|
+
j->depth--;
|
|
4474
|
+
return PBW_OK;
|
|
4475
|
+
}
|
|
4476
|
+
continue;
|
|
4477
|
+
}
|
|
4478
|
+
if (j_expect_ch(j, '}') != PBW_OK) {
|
|
4479
|
+
j->depth--;
|
|
4480
|
+
return PBW_ERR_INVALID;
|
|
4481
|
+
}
|
|
4482
|
+
j->depth--;
|
|
4483
|
+
return PBW_OK;
|
|
4484
|
+
}
|
|
4485
|
+
}
|
|
4486
|
+
|
|
4487
|
+
/* Parses the value of one field: nulls, oneof/duplicate rules, dispatch. */
|
|
4488
|
+
static pbw_status
|
|
4489
|
+
j_parse_field_value(j_in *j, pbw_message *msg, const pbw_field *field,
|
|
4490
|
+
int in_array)
|
|
4491
|
+
{
|
|
4492
|
+
int is_value = field->type == PBW_TYPE_MESSAGE && field->sub != NULL &&
|
|
4493
|
+
j_classify(field->sub) == J_VALUE;
|
|
4494
|
+
int is_nullvalue = field->type == PBW_TYPE_ENUM && field->sub != NULL &&
|
|
4495
|
+
j_classify(field->sub) == J_NULLVALUE;
|
|
4496
|
+
|
|
4497
|
+
(void)in_array;
|
|
4498
|
+
/* Any `null` is a no-op, except for Value and NullValue. */
|
|
4499
|
+
if (j_peek_kind(j) == JK_NULL && !is_value && !is_nullvalue) {
|
|
4500
|
+
return j_expect_lit(j, "null");
|
|
4501
|
+
}
|
|
4502
|
+
/* Singular fields parse once per oneof; anything else overwrites. */
|
|
4503
|
+
if (field->label != PBW_LABEL_REPEATED) {
|
|
4504
|
+
size_t index = (size_t)(field - msg->desc->fields);
|
|
4505
|
+
|
|
4506
|
+
if (field->oneof_index != 0) {
|
|
4507
|
+
if (msg->oneof_case[field->oneof_index] != 0) {
|
|
4508
|
+
return PBW_ERR_INVALID;
|
|
4509
|
+
}
|
|
4510
|
+
} else if (pbw_msg_present(msg, index)) {
|
|
4511
|
+
/* Legacy mode allows the duplicate (last wins): fall through
|
|
4512
|
+
* without clearing, like the reference. */
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
if (field->type == PBW_TYPE_MESSAGE || field->type == PBW_TYPE_GROUP) {
|
|
4516
|
+
if (field->sub != NULL && field->sub->map_entry) {
|
|
4517
|
+
return j_parse_map(j, msg, field);
|
|
4518
|
+
}
|
|
4519
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
4520
|
+
/* Like the reference, a repeated message field wants an array:
|
|
4521
|
+
* a single value does not parse (see SingleValue tests). Array
|
|
4522
|
+
* elements arrive here with in_array set and parse as one. */
|
|
4523
|
+
if (!in_array && j_peek_kind(j) != JK_ARR) {
|
|
4524
|
+
return PBW_ERR_INVALID;
|
|
4525
|
+
}
|
|
4526
|
+
if (!in_array) {
|
|
4527
|
+
return j_parse_array(j, msg, field);
|
|
4528
|
+
}
|
|
4529
|
+
}
|
|
4530
|
+
{
|
|
4531
|
+
pbw_message *sub = NULL;
|
|
4532
|
+
pbw_status st;
|
|
4533
|
+
size_t index = (size_t)(field - msg->desc->fields);
|
|
4534
|
+
|
|
4535
|
+
if (field->oneof_index == 0 && pbw_msg_present(msg, index) &&
|
|
4536
|
+
msg->values[index].kind == PBW_VALUE_MESSAGE &&
|
|
4537
|
+
msg->values[index].as.msg != NULL &&
|
|
4538
|
+
msg->values[index].as.msg->desc == field->sub) {
|
|
4539
|
+
/* Legacy mode allows a duplicate singular key; the reference
|
|
4540
|
+
* merges it into the submessage already parsed instead of
|
|
4541
|
+
* replacing it (so fields of the first copy survive). */
|
|
4542
|
+
st = j_parse_message(j, msg->values[index].as.msg);
|
|
4543
|
+
return st == PBW_ERR_LIMIT ? st : (st == PBW_OK ? PBW_OK : PBW_ERR_INVALID);
|
|
4544
|
+
}
|
|
4545
|
+
sub = pbw_message_new(field->sub);
|
|
4546
|
+
if (sub == NULL) {
|
|
4547
|
+
return PBW_ERR_NOMEM;
|
|
4548
|
+
}
|
|
4549
|
+
/* Like j_parse_value_msg: a depth breach stays LIMIT. */
|
|
4550
|
+
st = j_parse_message(j, sub);
|
|
4551
|
+
if (st != PBW_OK) {
|
|
4552
|
+
pbw_message_free(sub);
|
|
4553
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
4554
|
+
}
|
|
4555
|
+
return j_store_message(j, msg, field, sub);
|
|
4556
|
+
}
|
|
4557
|
+
}
|
|
4558
|
+
if (field->label == PBW_LABEL_REPEATED) {
|
|
4559
|
+
/* Like the reference, a repeated scalar field wants an array. */
|
|
4560
|
+
if (!in_array && j_peek_kind(j) != JK_ARR) {
|
|
4561
|
+
return PBW_ERR_INVALID;
|
|
4562
|
+
}
|
|
4563
|
+
if (!in_array) {
|
|
4564
|
+
return j_parse_array(j, msg, field);
|
|
4565
|
+
}
|
|
4566
|
+
if (field->type == PBW_TYPE_ENUM) {
|
|
4567
|
+
int stored = 0;
|
|
4568
|
+
|
|
4569
|
+
return j_parse_enum_value(j, msg, field, &stored);
|
|
4570
|
+
}
|
|
4571
|
+
return j_parse_scalar_value(j, msg, field);
|
|
4572
|
+
}
|
|
4573
|
+
if (field->type == PBW_TYPE_ENUM) {
|
|
4574
|
+
int stored = 0;
|
|
4575
|
+
pbw_status st = j_parse_enum_value(j, msg, field, &stored);
|
|
4576
|
+
|
|
4577
|
+
/* Unknown names are skipped under ignore_unknown, even for
|
|
4578
|
+
* proto3 implicit-presence enums (which the reference sets to 0,
|
|
4579
|
+
* indistinguishable from unset). */
|
|
4580
|
+
(void)stored;
|
|
4581
|
+
return st;
|
|
4582
|
+
}
|
|
4583
|
+
return j_parse_scalar_value(j, msg, field);
|
|
4584
|
+
}
|
|
4585
|
+
|
|
4586
|
+
/* Routes a message value by kind: Any/Value/Struct intercept objects and
|
|
4587
|
+
* everything else; List/wrappers/Timestamp/Duration/FieldMask only
|
|
4588
|
+
* intercept their special (non-object) form. */
|
|
4589
|
+
static pbw_status
|
|
4590
|
+
j_parse_message(j_in *j, pbw_message *msg)
|
|
4591
|
+
{
|
|
4592
|
+
j_wkt wkt;
|
|
4593
|
+
j_kind kind;
|
|
4594
|
+
|
|
4595
|
+
if (msg == NULL || msg->desc == NULL) {
|
|
4596
|
+
return PBW_ERR_INVALID;
|
|
4597
|
+
}
|
|
4598
|
+
if (msg->desc->is_enum) {
|
|
4599
|
+
return PBW_ERR_INVALID;
|
|
4600
|
+
}
|
|
4601
|
+
wkt = j_classify(msg->desc);
|
|
4602
|
+
switch (wkt) {
|
|
4603
|
+
case J_ANY:
|
|
4604
|
+
if (j_peek_kind(j) == JK_NULL) {
|
|
4605
|
+
return j_expect_lit(j, "null");
|
|
4606
|
+
}
|
|
4607
|
+
return j_parse_any(j, msg);
|
|
4608
|
+
case J_VALUE:
|
|
4609
|
+
return j_parse_value_msg(j, msg);
|
|
4610
|
+
case J_STRUCT:
|
|
4611
|
+
return j_parse_struct(j, msg);
|
|
4612
|
+
default:
|
|
4613
|
+
break;
|
|
4614
|
+
}
|
|
4615
|
+
kind = j_peek_kind(j);
|
|
4616
|
+
if (kind != JK_OBJ) {
|
|
4617
|
+
switch (wkt) {
|
|
4618
|
+
case J_LIST:
|
|
4619
|
+
return j_parse_list_of(j, msg);
|
|
4620
|
+
case J_WRAPPER: {
|
|
4621
|
+
const pbw_field *inner =
|
|
4622
|
+
pbw_descriptor_find_field(msg->desc, 1);
|
|
4623
|
+
|
|
4624
|
+
if (inner == NULL) {
|
|
4625
|
+
return PBW_ERR_INVALID;
|
|
4626
|
+
}
|
|
4627
|
+
return j_parse_field_value(j, msg, inner, 0);
|
|
4628
|
+
}
|
|
4629
|
+
case J_TIMESTAMP:
|
|
4630
|
+
return j_parse_timestamp(j, msg);
|
|
4631
|
+
case J_DURATION:
|
|
4632
|
+
return j_parse_duration(j, msg);
|
|
4633
|
+
case J_FIELDMASK:
|
|
4634
|
+
return j_parse_fieldmask(j, msg);
|
|
4635
|
+
case J_EMPTY:
|
|
4636
|
+
return PBW_ERR_INVALID;
|
|
4637
|
+
default:
|
|
4638
|
+
return PBW_ERR_INVALID;
|
|
4639
|
+
}
|
|
4640
|
+
}
|
|
4641
|
+
if (j_expect_ch(j, '{') != PBW_OK) {
|
|
4642
|
+
return PBW_ERR_INVALID;
|
|
4643
|
+
}
|
|
4644
|
+
return j_parse_object_body(j, msg);
|
|
4645
|
+
}
|
|
4646
|
+
|
|
4647
|
+
/* google.protobuf.ListValue: an array of Values. No flattening here. */
|
|
4648
|
+
static pbw_status
|
|
4649
|
+
j_parse_list_of(j_in *j, pbw_message *msg)
|
|
4650
|
+
{
|
|
4651
|
+
const pbw_field *entry_field = pbw_descriptor_find_field(msg->desc, 1);
|
|
4652
|
+
|
|
4653
|
+
if (entry_field == NULL || entry_field->sub == NULL) {
|
|
4654
|
+
return PBW_ERR_INVALID;
|
|
4655
|
+
}
|
|
4656
|
+
if (j_peek_kind(j) == JK_NULL) {
|
|
4657
|
+
return j_expect_lit(j, "null");
|
|
4658
|
+
}
|
|
4659
|
+
if (j_expect_ch(j, '[') != PBW_OK) {
|
|
4660
|
+
return PBW_ERR_INVALID;
|
|
4661
|
+
}
|
|
4662
|
+
if (j->depth >= j->dlim) {
|
|
4663
|
+
return PBW_ERR_LIMIT;
|
|
4664
|
+
}
|
|
4665
|
+
j->depth++;
|
|
4666
|
+
j_skip(&j->cur);
|
|
4667
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
4668
|
+
j->cur.p++;
|
|
4669
|
+
j->depth--;
|
|
4670
|
+
return PBW_OK;
|
|
4671
|
+
}
|
|
4672
|
+
for (;;) {
|
|
4673
|
+
pbw_message *item = pbw_message_new(entry_field->sub);
|
|
4674
|
+
pbw_status st;
|
|
4675
|
+
|
|
4676
|
+
if (item == NULL) {
|
|
4677
|
+
j->depth--;
|
|
4678
|
+
return PBW_ERR_NOMEM;
|
|
4679
|
+
}
|
|
4680
|
+
st = j_parse_value_msg(j, item);
|
|
4681
|
+
if (st != PBW_OK) {
|
|
4682
|
+
pbw_message_free(item);
|
|
4683
|
+
j->depth--;
|
|
4684
|
+
return st == PBW_ERR_LIMIT ? st : PBW_ERR_INVALID;
|
|
4685
|
+
}
|
|
4686
|
+
st = j_store_message(j, msg, entry_field, item);
|
|
4687
|
+
if (st != PBW_OK) {
|
|
4688
|
+
j->depth--;
|
|
4689
|
+
return st;
|
|
4690
|
+
}
|
|
4691
|
+
j_skip(&j->cur);
|
|
4692
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ',') {
|
|
4693
|
+
j->cur.p++;
|
|
4694
|
+
/* Trailing commas are accepted in legacy mode. */
|
|
4695
|
+
j_skip(&j->cur);
|
|
4696
|
+
if (j->cur.p < j->cur.end && *j->cur.p == ']') {
|
|
4697
|
+
j->cur.p++;
|
|
4698
|
+
j->depth--;
|
|
4699
|
+
return PBW_OK;
|
|
4700
|
+
}
|
|
4701
|
+
continue;
|
|
4702
|
+
}
|
|
4703
|
+
if (j_expect_ch(j, ']') != PBW_OK) {
|
|
4704
|
+
j->depth--;
|
|
4705
|
+
return PBW_ERR_INVALID;
|
|
4706
|
+
}
|
|
4707
|
+
j->depth--;
|
|
4708
|
+
return PBW_OK;
|
|
4709
|
+
}
|
|
4710
|
+
}
|
|
4711
|
+
|
|
4712
|
+
pbw_status
|
|
4713
|
+
pbw_json_parse_limits(const pbw_descriptor *desc, const char *text,
|
|
4714
|
+
size_t len, const pbw_limits *limits,
|
|
4715
|
+
const pbw_json_parse_options *options,
|
|
4716
|
+
pbw_message **out)
|
|
4717
|
+
{
|
|
4718
|
+
j_in j;
|
|
4719
|
+
pbw_message *msg;
|
|
4720
|
+
uint32_t dlim = PBW_MAX_DEPTH;
|
|
4721
|
+
|
|
4722
|
+
if (out != NULL) {
|
|
4723
|
+
*out = NULL;
|
|
4724
|
+
}
|
|
4725
|
+
if (desc == NULL || (text == NULL && len != 0) || out == NULL) {
|
|
4726
|
+
return PBW_ERR_INVALID;
|
|
4727
|
+
}
|
|
4728
|
+
if (limits != NULL) {
|
|
4729
|
+
/* Same bounds semantics as the binary decoder: 0 = default, above the
|
|
4730
|
+
* cap clamps, and the byte cap is checked before any recursion. */
|
|
4731
|
+
if (limits->max_depth != 0) {
|
|
4732
|
+
dlim = limits->max_depth < PBW_MAX_DEPTH ? limits->max_depth
|
|
4733
|
+
: PBW_MAX_DEPTH;
|
|
4734
|
+
}
|
|
4735
|
+
if (limits->max_bytes != 0 && len > limits->max_bytes) {
|
|
4736
|
+
return PBW_ERR_LIMIT;
|
|
4737
|
+
}
|
|
4738
|
+
}
|
|
4739
|
+
if (desc->is_enum) {
|
|
4740
|
+
return PBW_ERR_TYPE;
|
|
4741
|
+
}
|
|
4742
|
+
if (desc->schema != NULL && !desc->schema->linked) {
|
|
4743
|
+
return PBW_ERR_INVALID;
|
|
4744
|
+
}
|
|
4745
|
+
if (text == NULL) {
|
|
4746
|
+
text = (const char *)pbw_empty_input;
|
|
4747
|
+
}
|
|
4748
|
+
msg = pbw_message_new(desc);
|
|
4749
|
+
if (msg == NULL) {
|
|
4750
|
+
return PBW_ERR_NOMEM;
|
|
4751
|
+
}
|
|
4752
|
+
j.cur.p = text;
|
|
4753
|
+
j.cur.end = text + len;
|
|
4754
|
+
j.ignore_unknown =
|
|
4755
|
+
options != NULL && options->ignore_unknown_fields;
|
|
4756
|
+
j.depth = 0;
|
|
4757
|
+
j.dlim = dlim;
|
|
4758
|
+
{
|
|
4759
|
+
pbw_status st = j_parse_message(&j, msg);
|
|
4760
|
+
|
|
4761
|
+
if (st != PBW_OK) {
|
|
4762
|
+
pbw_message_free(msg);
|
|
4763
|
+
return st;
|
|
4764
|
+
}
|
|
4765
|
+
}
|
|
4766
|
+
j_skip(&j.cur);
|
|
4767
|
+
if (j.cur.p != j.cur.end) {
|
|
4768
|
+
pbw_message_free(msg);
|
|
4769
|
+
return PBW_ERR_INVALID;
|
|
4770
|
+
}
|
|
4771
|
+
/* A missing proto2 `required` field fails, like the binary decoder. */
|
|
4772
|
+
if (desc->has_required && !pbw_message_is_initialized(msg)) {
|
|
4773
|
+
pbw_message_free(msg);
|
|
4774
|
+
return PBW_ERR_REQUIRED;
|
|
4775
|
+
}
|
|
4776
|
+
*out = msg;
|
|
4777
|
+
return PBW_OK;
|
|
4778
|
+
}
|
|
4779
|
+
|
|
4780
|
+
pbw_status
|
|
4781
|
+
pbw_json_parse_opts(const pbw_descriptor *desc, const char *text, size_t len,
|
|
4782
|
+
const pbw_json_parse_options *options, pbw_message **out)
|
|
4783
|
+
{
|
|
4784
|
+
return pbw_json_parse_limits(desc, text, len, NULL, options, out);
|
|
4785
|
+
}
|
|
4786
|
+
|
|
4787
|
+
pbw_status
|
|
4788
|
+
pbw_json_parse(const pbw_descriptor *desc, const char *text, size_t len,
|
|
4789
|
+
pbw_message **out)
|
|
4790
|
+
{
|
|
4791
|
+
return pbw_json_parse_limits(desc, text, len, NULL, NULL, out);
|
|
4792
|
+
}
|