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.
@@ -0,0 +1,2583 @@
1
+ /* Protobuf text format: human-readable message printing and parsing.
2
+ *
3
+ * The printer emits what the reference implementation prints (multi-line,
4
+ * two-space indent, `name: value` for scalars, `name { ... }` for messages,
5
+ * `[full.name]` for extensions, shortest round-tripping floats), so its
6
+ * output parses back to the same message. The parser accepts what the
7
+ * reference parser accepts: optional colons before `{`/`<`, required colons
8
+ * before scalar values, `[a, b]` lists, `<`/`>` delimiters, group type names,
9
+ * extensions, `Any` expansions, C escapes, decimal/hex/octal integers and the
10
+ * same range errors. Unknown field names fail unless reserved; reserved names
11
+ * are skipped silently, like the reference.
12
+ *
13
+ * Like the binary decoder, nesting is capped at PBW_MAX_DEPTH and a missing
14
+ * proto2 `required` field fails the parse. Unlike the binary format, string
15
+ * fields reject invalid UTF-8 (the reference text parser does too).
16
+ */
17
+
18
+ #include "internal.h"
19
+
20
+ #include <math.h>
21
+ #include <stdio.h>
22
+
23
+ /* ------------------------------------------------------------------ output */
24
+
25
+ typedef struct {
26
+ pbw_buf buf;
27
+ int indent;
28
+ int print_unknown;
29
+ pbw_status status;
30
+ } text_out;
31
+
32
+ static void
33
+ t_put(text_out *t, const void *data, size_t len)
34
+ {
35
+ if (t->status != PBW_OK || len == 0) {
36
+ return;
37
+ }
38
+ if (!pbw_buf_reserve(&t->buf, len)) {
39
+ t->status = PBW_ERR_NOMEM;
40
+ return;
41
+ }
42
+ memcpy(t->buf.data + t->buf.len, data, len);
43
+ t->buf.len += len;
44
+ }
45
+
46
+ static void
47
+ t_str(text_out *t, const char *s)
48
+ {
49
+ t_put(t, s, strlen(s));
50
+ }
51
+
52
+ static void
53
+ t_indent(text_out *t, int extra)
54
+ {
55
+ int i;
56
+ int n = t->indent + extra;
57
+
58
+ for (i = 0; i < n; i++) {
59
+ t_put(t, " ", 2);
60
+ }
61
+ }
62
+
63
+ static void
64
+ t_u64(text_out *t, uint64_t value)
65
+ {
66
+ char tmp[24];
67
+ int n = snprintf(tmp, sizeof(tmp), "%llu", (unsigned long long)value);
68
+
69
+ if (n > 0) {
70
+ t_put(t, tmp, (size_t)n);
71
+ }
72
+ }
73
+
74
+ static void
75
+ t_i64(text_out *t, int64_t value)
76
+ {
77
+ char tmp[24];
78
+ int n = snprintf(tmp, sizeof(tmp), "%lld", (long long)value);
79
+
80
+ if (n > 0) {
81
+ t_put(t, tmp, (size_t)n);
82
+ }
83
+ }
84
+
85
+ /* Shortest decimal that still parses back to the same bits. */
86
+ static void
87
+ t_double(text_out *t, double value, int is_float)
88
+ {
89
+ int prec;
90
+ int prec_max = 17;
91
+ int prec_min = is_float ? 9 : 15;
92
+
93
+ if (isnan(value)) {
94
+ t_str(t, "nan");
95
+ return;
96
+ }
97
+ if (isinf(value)) {
98
+ t_str(t, value < 0 ? "-inf" : "inf");
99
+ return;
100
+ }
101
+ for (prec = prec_min; prec <= prec_max; prec++) {
102
+ char tmp[32];
103
+ int n = pbw_format_g_c(tmp, sizeof(tmp), prec, value);
104
+ double back;
105
+
106
+ if (n <= 0 || (size_t)n >= sizeof(tmp)) {
107
+ continue;
108
+ }
109
+ back = pbw_strtod_c(tmp, NULL);
110
+ if ((is_float ? (double)(float)back == value : back == value)) {
111
+ t_put(t, tmp, (size_t)n);
112
+ return;
113
+ }
114
+ }
115
+ /* %.17g always round-trips a double; reaching here means snprintf failed,
116
+ * which cannot happen for a finite value. */
117
+ t_str(t, "0");
118
+ }
119
+
120
+ /* C escapes, exactly like the reference printer (absl::CEscape): short
121
+ * escapes only for tab/lf/cr, backslash escapes for both quotes, three-digit
122
+ * octal for the rest. */
123
+ static void
124
+ t_quoted(text_out *t, const uint8_t *data, size_t len)
125
+ {
126
+ size_t i;
127
+
128
+ if (data == NULL) {
129
+ if (len > 0) {
130
+ t->status = PBW_ERR_INVALID;
131
+ return;
132
+ }
133
+ data = pbw_empty_input;
134
+ }
135
+ t_put(t, "\"", 1);
136
+ for (i = 0; i < len; i++) {
137
+ uint8_t c = data[i];
138
+
139
+ if (c == '\t') {
140
+ t_str(t, "\\t");
141
+ } else if (c == '\n') {
142
+ t_str(t, "\\n");
143
+ } else if (c == '\r') {
144
+ t_str(t, "\\r");
145
+ } else if (c == '"') {
146
+ t_str(t, "\\\"");
147
+ } else if (c == '\\') {
148
+ t_str(t, "\\\\");
149
+ } else if (c == '\'') {
150
+ t_str(t, "\\'");
151
+ } else if (c >= 32 && c < 127) {
152
+ t_put(t, &c, 1);
153
+ } else {
154
+ char tmp[5];
155
+
156
+ tmp[0] = '\\';
157
+ tmp[1] = (char)('0' + ((c >> 6) & 7));
158
+ tmp[2] = (char)('0' + ((c >> 3) & 7));
159
+ tmp[3] = (char)('0' + (c & 7));
160
+ tmp[4] = '\0';
161
+ t_put(t, tmp, 4);
162
+ }
163
+ }
164
+ t_put(t, "\"", 1);
165
+ }
166
+
167
+ /* Short unqualified name: the reference prints groups by their type name. */
168
+ static const char *
169
+ short_name(const char *fq)
170
+ {
171
+ const char *dot = strrchr(fq, '.');
172
+
173
+ return dot == NULL ? fq : dot + 1;
174
+ }
175
+
176
+ static int
177
+ is_any_message(const pbw_descriptor *desc)
178
+ {
179
+ return desc != NULL && !desc->is_enum &&
180
+ strcmp(desc->name, "google.protobuf.Any") == 0;
181
+ }
182
+
183
+ static void t_print_message(text_out *t, const pbw_message *msg, uint32_t depth);
184
+
185
+ static void
186
+ t_print_scalar(text_out *t, const pbw_field *field, const pbw_value *value)
187
+ {
188
+ if (t == NULL || field == NULL || value == NULL) {
189
+ if (t != NULL && t->status == PBW_OK) {
190
+ t->status = PBW_ERR_INVALID;
191
+ }
192
+ return;
193
+ }
194
+ if ((field->type == PBW_TYPE_STRING || field->type == PBW_TYPE_BYTES)) {
195
+ /* checked in the case below */
196
+ } else if (field->type == PBW_TYPE_MESSAGE || field->type == PBW_TYPE_GROUP) {
197
+ if (value->kind != PBW_VALUE_MESSAGE) {
198
+ t->status = PBW_ERR_INVALID;
199
+ return;
200
+ }
201
+ } else {
202
+ if (value->kind != PBW_VALUE_UINT) {
203
+ t->status = PBW_ERR_INVALID;
204
+ return;
205
+ }
206
+ }
207
+ switch (field->type) {
208
+ case PBW_TYPE_DOUBLE: {
209
+ double d;
210
+
211
+ memcpy(&d, &value->as.u64, sizeof(d));
212
+ t_double(t, d, 0);
213
+ return;
214
+ }
215
+ case PBW_TYPE_FLOAT: {
216
+ /* Same mask as the JSON printer: the 4 float bytes are the low half
217
+ * of the stored bit pattern, not its first 4 memory bytes. */
218
+ uint32_t bits32 = (uint32_t)value->as.u64;
219
+ float f;
220
+
221
+ memcpy(&f, &bits32, sizeof(f));
222
+ t_double(t, (double)f, 1);
223
+ return;
224
+ }
225
+ case PBW_TYPE_BOOL:
226
+ t_str(t, value->as.u64 != 0 ? "true" : "false");
227
+ return;
228
+ case PBW_TYPE_ENUM: {
229
+ const char *name = NULL;
230
+
231
+ if (field->sub != NULL &&
232
+ pbw_enum_find_name(field->sub, (int32_t)value->as.u64, &name)) {
233
+ t_str(t, name);
234
+ return;
235
+ }
236
+ t_i64(t, (int64_t)(int32_t)value->as.u64);
237
+ return;
238
+ }
239
+ case PBW_TYPE_STRING:
240
+ case PBW_TYPE_BYTES:
241
+ if (value->kind != PBW_VALUE_BYTES) {
242
+ t->status = PBW_ERR_INVALID;
243
+ return;
244
+ }
245
+ if (value->as.bytes.len > 0 && value->as.bytes.data == NULL) {
246
+ t->status = PBW_ERR_INVALID;
247
+ return;
248
+ }
249
+ t_quoted(t, value->as.bytes.data, value->as.bytes.len);
250
+ return;
251
+ case PBW_TYPE_INT32:
252
+ case PBW_TYPE_SINT32:
253
+ case PBW_TYPE_SFIXED32:
254
+ t_i64(t, (int64_t)(int32_t)value->as.u64);
255
+ return;
256
+ case PBW_TYPE_INT64:
257
+ case PBW_TYPE_SINT64:
258
+ case PBW_TYPE_SFIXED64:
259
+ t_i64(t, (int64_t)value->as.u64);
260
+ return;
261
+ default:
262
+ t_u64(t, value->as.u64);
263
+ return;
264
+ }
265
+ }
266
+
267
+ /* Tries to read `data` as a sequence of fields; 1 when it parses cleanly, so
268
+ * the printer can show an unknown length-delimited value as a message. */
269
+ static int
270
+ unknown_is_message(const uint8_t *data, size_t len, uint32_t depth)
271
+ {
272
+ const uint8_t *p = data;
273
+ const uint8_t *end = data + len;
274
+
275
+ if (len == 0 || depth >= PBW_MAX_DEPTH) {
276
+ return 0;
277
+ }
278
+ while (p < end) {
279
+ uint32_t tag;
280
+ size_t used;
281
+ uint32_t wire;
282
+
283
+ if (!pbw_tag_read(p, end, &tag, &used)) {
284
+ return 0;
285
+ }
286
+ p += used;
287
+ wire = tag & 7;
288
+ if ((tag >> 3) == 0 || wire == PBW_WIRE_END_GROUP ||
289
+ wire > PBW_WIRE_32BIT) {
290
+ return 0;
291
+ }
292
+ switch (wire) {
293
+ case PBW_WIRE_VARINT: {
294
+ uint64_t scratch;
295
+ size_t n;
296
+
297
+ if (!pbw_varint_read(p, end, &scratch, &n)) {
298
+ return 0;
299
+ }
300
+ p += n;
301
+ break;
302
+ }
303
+ case PBW_WIRE_64BIT:
304
+ if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) < 8) {
305
+ return 0;
306
+ }
307
+ p += 8;
308
+ break;
309
+ case PBW_WIRE_32BIT:
310
+ if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) < 4) {
311
+ return 0;
312
+ }
313
+ p += 4;
314
+ break;
315
+ case PBW_WIRE_LEN: {
316
+ size_t ilen;
317
+ size_t n;
318
+
319
+ if (!pbw_len_read(p, end, &ilen, &n)) {
320
+ return 0;
321
+ }
322
+ p += n;
323
+ if (ilen > pbw_remaining((const uint8_t *)end, (const uint8_t *)p)) {
324
+ return 0;
325
+ }
326
+ p += ilen;
327
+ break;
328
+ }
329
+ case PBW_WIRE_START_GROUP: {
330
+ /* skip to the matching END_GROUP, counting nesting */
331
+ uint32_t number = tag >> 3;
332
+ uint32_t nesting = 1;
333
+
334
+ while (p < end && nesting > 0) {
335
+ if (!pbw_tag_read(p, end, &tag, &used)) {
336
+ return 0;
337
+ }
338
+ p += used;
339
+ wire = tag & 7;
340
+ if ((tag >> 3) == 0 || wire > PBW_WIRE_32BIT) {
341
+ return 0;
342
+ }
343
+ if (wire == PBW_WIRE_START_GROUP) {
344
+ nesting++;
345
+ } else if (wire == PBW_WIRE_END_GROUP) {
346
+ if (nesting == 1 && (tag >> 3) != number) {
347
+ return 0;
348
+ }
349
+ nesting--;
350
+ } else if (wire == PBW_WIRE_VARINT) {
351
+ uint64_t scratch;
352
+ size_t n;
353
+
354
+ if (!pbw_varint_read(p, end, &scratch, &n)) {
355
+ return 0;
356
+ }
357
+ p += n;
358
+ } else if (wire == PBW_WIRE_64BIT) {
359
+ if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) < 8) {
360
+ return 0;
361
+ }
362
+ p += 8;
363
+ } else if (wire == PBW_WIRE_32BIT) {
364
+ if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) < 4) {
365
+ return 0;
366
+ }
367
+ p += 4;
368
+ } else if (wire == PBW_WIRE_LEN) {
369
+ size_t ilen;
370
+ size_t n;
371
+
372
+ if (!pbw_len_read(p, end, &ilen, &n)) {
373
+ return 0;
374
+ }
375
+ p += n;
376
+ if (ilen > pbw_remaining((const uint8_t *)end, (const uint8_t *)p)) {
377
+ return 0;
378
+ }
379
+ p += ilen;
380
+ } else {
381
+ return 0;
382
+ }
383
+ }
384
+ if (nesting != 0) {
385
+ return 0;
386
+ }
387
+ break;
388
+ }
389
+ default:
390
+ return 0;
391
+ }
392
+ }
393
+ return 1;
394
+ }
395
+
396
+ static void t_print_unknowns(text_out *t, const uint8_t *p,
397
+ const uint8_t *end, uint32_t depth);
398
+
399
+ /* Prints one unknown value; `p` points past its tag. Indentation comes from
400
+ * t->indent, like known fields. */
401
+ static void
402
+ t_print_unknown_value(text_out *t, uint32_t number, uint32_t wire,
403
+ const uint8_t **pp, const uint8_t *end, uint32_t depth)
404
+ {
405
+ const uint8_t *p = *pp;
406
+ char num[24];
407
+ int n;
408
+
409
+ if (depth >= PBW_MAX_DEPTH) {
410
+ t->status = PBW_ERR_LIMIT;
411
+ return;
412
+ }
413
+ t_indent(t, 0);
414
+ n = snprintf(num, sizeof(num), "%u", number);
415
+ if (n > 0) {
416
+ t_put(t, num, (size_t)n);
417
+ }
418
+ switch (wire) {
419
+ case PBW_WIRE_VARINT: {
420
+ uint64_t value;
421
+ size_t used;
422
+
423
+ if (!pbw_varint_read(p, end, &value, &used)) {
424
+ t->status = PBW_ERR_INVALID;
425
+ return;
426
+ }
427
+ t_str(t, ": ");
428
+ t_u64(t, value);
429
+ t_str(t, "\n");
430
+ *pp = p + used;
431
+ return;
432
+ }
433
+ case PBW_WIRE_64BIT: {
434
+ char tmp[24];
435
+ uint64_t v = 0;
436
+ int i;
437
+
438
+ if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) < 8) {
439
+ t->status = PBW_ERR_INVALID;
440
+ return;
441
+ }
442
+ /* value order, like the reference: lowest byte first on the wire */
443
+ for (i = 7; i >= 0; i--) {
444
+ v = v * 256 + p[i];
445
+ }
446
+ snprintf(tmp, sizeof(tmp), ": 0x%016llx\n",
447
+ (unsigned long long)v);
448
+ t_str(t, tmp);
449
+ *pp = p + 8;
450
+ return;
451
+ }
452
+ case PBW_WIRE_32BIT: {
453
+ char tmp[16];
454
+ uint32_t v = 0;
455
+ int i;
456
+
457
+ if (pbw_remaining((const uint8_t *)end, (const uint8_t *)p) < 4) {
458
+ t->status = PBW_ERR_INVALID;
459
+ return;
460
+ }
461
+ for (i = 3; i >= 0; i--) {
462
+ v = v * 256 + p[i];
463
+ }
464
+ snprintf(tmp, sizeof(tmp), ": 0x%08x\n", v);
465
+ t_str(t, tmp);
466
+ *pp = p + 4;
467
+ return;
468
+ }
469
+ case PBW_WIRE_LEN: {
470
+ size_t len;
471
+ size_t used;
472
+
473
+ if (!pbw_len_read(p, end, &len, &used)) {
474
+ t->status = PBW_ERR_INVALID;
475
+ return;
476
+ }
477
+ p += used;
478
+ if (len > pbw_remaining((const uint8_t *)end, (const uint8_t *)p)) {
479
+ t->status = PBW_ERR_INVALID;
480
+ return;
481
+ }
482
+ if (unknown_is_message(p, len, depth)) {
483
+ t_str(t, " {\n");
484
+ t->indent++;
485
+ t_print_unknowns(t, p, p + len, depth + 1);
486
+ t->indent--;
487
+ t_indent(t, 0);
488
+ t_str(t, "}\n");
489
+ } else {
490
+ t_str(t, ": ");
491
+ t_quoted(t, p, len);
492
+ t_str(t, "\n");
493
+ }
494
+ *pp = p + len;
495
+ return;
496
+ }
497
+ case PBW_WIRE_START_GROUP: {
498
+ /* find the matching END_GROUP first, then print the body */
499
+ const uint8_t *body = p;
500
+ const uint8_t *bend = NULL;
501
+ uint32_t nesting = 1;
502
+ const uint8_t *q = p;
503
+
504
+ while (q < end && nesting > 0) {
505
+ uint32_t tag;
506
+ size_t used;
507
+
508
+ if (!pbw_tag_read(q, end, &tag, &used)) {
509
+ t->status = PBW_ERR_INVALID;
510
+ return;
511
+ }
512
+ q += used;
513
+ if ((tag & 7) == PBW_WIRE_START_GROUP) {
514
+ nesting++;
515
+ } else if ((tag & 7) == PBW_WIRE_END_GROUP) {
516
+ nesting--;
517
+ if (nesting == 0) {
518
+ if ((tag >> 3) != number) {
519
+ t->status = PBW_ERR_INVALID;
520
+ return;
521
+ }
522
+ bend = q - used;
523
+ }
524
+ } else {
525
+ /* skip the value to find the group end */
526
+ uint32_t w = tag & 7;
527
+ if (w == PBW_WIRE_VARINT) {
528
+ uint64_t scratch;
529
+ size_t nn;
530
+
531
+ if (!pbw_varint_read(q, end, &scratch, &nn)) {
532
+ t->status = PBW_ERR_INVALID;
533
+ return;
534
+ }
535
+ q += nn;
536
+ } else if (w == PBW_WIRE_64BIT || w == PBW_WIRE_32BIT) {
537
+ size_t need = w == PBW_WIRE_64BIT ? 8u : 4u;
538
+
539
+ /* Bounds-check before advancing: q + 8 past one-past-end
540
+ * is undefined even when nothing is read. */
541
+ if (pbw_remaining((const uint8_t *)end,
542
+ (const uint8_t *)q) < need) {
543
+ t->status = PBW_ERR_INVALID;
544
+ return;
545
+ }
546
+ q += need;
547
+ } else if (w == PBW_WIRE_LEN) {
548
+ size_t ilen;
549
+ size_t nn;
550
+
551
+ if (!pbw_len_read(q, end, &ilen, &nn)) {
552
+ t->status = PBW_ERR_INVALID;
553
+ return;
554
+ }
555
+ q += nn;
556
+ if (ilen > pbw_remaining((const uint8_t *)end,
557
+ (const uint8_t *)q)) {
558
+ t->status = PBW_ERR_INVALID;
559
+ return;
560
+ }
561
+ q += ilen;
562
+ } else {
563
+ t->status = PBW_ERR_INVALID;
564
+ return;
565
+ }
566
+ }
567
+ }
568
+ if (nesting != 0 || bend == NULL) {
569
+ t->status = PBW_ERR_INVALID;
570
+ return;
571
+ }
572
+ t_str(t, " {\n");
573
+ t->indent++;
574
+ t_print_unknowns(t, body, bend, depth + 1);
575
+ t->indent--;
576
+ t_indent(t, 0);
577
+ t_str(t, "}\n");
578
+ *pp = q;
579
+ return;
580
+ }
581
+ default:
582
+ t->status = PBW_ERR_INVALID;
583
+ return;
584
+ }
585
+ }
586
+
587
+ static void
588
+ t_print_unknowns(text_out *t, const uint8_t *p, const uint8_t *end,
589
+ uint32_t depth)
590
+ {
591
+ while (p < end && t->status == PBW_OK) {
592
+ uint32_t tag;
593
+ size_t used;
594
+
595
+ if (!pbw_tag_read(p, end, &tag, &used)) {
596
+ t->status = PBW_ERR_INVALID;
597
+ return;
598
+ }
599
+ p += used;
600
+ t_print_unknown_value(t, tag >> 3, tag & 7, &p, end, depth);
601
+ }
602
+ }
603
+
604
+ static void
605
+ t_print_field_value(text_out *t, const pbw_field *field,
606
+ const pbw_value *value, int indent, uint32_t depth)
607
+ {
608
+ if (t == NULL || field == NULL || value == NULL) {
609
+ if (t != NULL && t->status == PBW_OK) {
610
+ t->status = PBW_ERR_INVALID;
611
+ }
612
+ return;
613
+ }
614
+ if (field->type == PBW_TYPE_MESSAGE || field->type == PBW_TYPE_GROUP) {
615
+ if (value->kind != PBW_VALUE_MESSAGE || value->as.msg == NULL) {
616
+ t->status = PBW_ERR_INVALID;
617
+ return;
618
+ }
619
+ if (depth >= PBW_MAX_DEPTH) {
620
+ t->status = PBW_ERR_LIMIT;
621
+ return;
622
+ }
623
+ t_str(t, " {\n");
624
+ t->indent++;
625
+ t_print_message(t, value->as.msg, depth + 1);
626
+ t->indent--;
627
+ t_indent(t, indent);
628
+ t_str(t, "}\n");
629
+ return;
630
+ }
631
+ t_str(t, ": ");
632
+ t_print_scalar(t, field, value);
633
+ t_str(t, "\n");
634
+ }
635
+
636
+ static void
637
+ t_print_field(text_out *t, const pbw_field *field, const pbw_value *value,
638
+ uint32_t depth)
639
+ {
640
+ size_t j;
641
+
642
+ if (t == NULL || field == NULL || value == NULL) {
643
+ if (t != NULL && t->status == PBW_OK) {
644
+ t->status = PBW_ERR_INVALID;
645
+ }
646
+ return;
647
+ }
648
+ if (field->label == PBW_LABEL_REPEATED) {
649
+ if (value->kind != PBW_VALUE_LIST) {
650
+ return;
651
+ }
652
+ if (value->as.list.len > 0 && value->as.list.items == NULL) {
653
+ t->status = PBW_ERR_INVALID;
654
+ return;
655
+ }
656
+ for (j = 0; j < value->as.list.len; j++) {
657
+ if (value->as.list.items[j] == NULL) {
658
+ t->status = PBW_ERR_INVALID;
659
+ return;
660
+ }
661
+ t_indent(t, 0);
662
+ if (field->is_extension && field->full_name != NULL) {
663
+ t_put(t, "[", 1);
664
+ t_str(t, field->full_name);
665
+ t_put(t, "]", 1);
666
+ } else if (field->is_extension) {
667
+ t_put(t, "[", 1);
668
+ t_str(t, field->name);
669
+ t_put(t, "]", 1);
670
+ } else if (field->type == PBW_TYPE_GROUP) {
671
+ if (field->sub == NULL || field->sub->name == NULL) {
672
+ t->status = PBW_ERR_INVALID;
673
+ return;
674
+ }
675
+ t_str(t, short_name(field->sub->name));
676
+ } else {
677
+ t_str(t, field->name);
678
+ }
679
+ t_print_field_value(t, field, value->as.list.items[j], 0, depth);
680
+ if (t->status != PBW_OK) {
681
+ return;
682
+ }
683
+ }
684
+ return;
685
+ }
686
+ t_indent(t, 0);
687
+ if (field->is_extension && field->full_name != NULL) {
688
+ t_put(t, "[", 1);
689
+ t_str(t, field->full_name);
690
+ t_put(t, "]", 1);
691
+ } else if (field->is_extension) {
692
+ t_put(t, "[", 1);
693
+ t_str(t, field->name);
694
+ t_put(t, "]", 1);
695
+ } else if (field->type == PBW_TYPE_GROUP) {
696
+ if (field->sub == NULL || field->sub->name == NULL) {
697
+ t->status = PBW_ERR_INVALID;
698
+ return;
699
+ }
700
+ t_str(t, short_name(field->sub->name));
701
+ } else {
702
+ t_str(t, field->name);
703
+ }
704
+ t_print_field_value(t, field, value, 0, depth);
705
+ }
706
+
707
+ static void
708
+ t_print_message(text_out *t, const pbw_message *msg, uint32_t depth)
709
+ {
710
+ size_t i;
711
+
712
+ if (t == NULL || msg == NULL || msg->desc == NULL) {
713
+ if (t != NULL && t->status == PBW_OK) {
714
+ t->status = PBW_ERR_INVALID;
715
+ }
716
+ return;
717
+ }
718
+ if (msg->desc->field_count > 0 &&
719
+ (msg->values == NULL || msg->present == NULL)) {
720
+ t->status = PBW_ERR_INVALID;
721
+ return;
722
+ }
723
+ if (t->status != PBW_OK) {
724
+ return;
725
+ }
726
+ if (!pbw_msg_fresh(msg)) {
727
+ /* Same retired-nested-message rule as the JSON printer: the slot
728
+ * array is shorter than the live descriptor. */
729
+ t->status = PBW_ERR_INVALID;
730
+ return;
731
+ }
732
+ if (depth > PBW_MAX_DEPTH) {
733
+ t->status = PBW_ERR_LIMIT;
734
+ return;
735
+ }
736
+ /* Any values print as their raw type_url/value fields, like the reference
737
+ * printer; the expanded form is only accepted on input. */
738
+ for (i = 0; i < msg->desc->field_count; i++) {
739
+ const pbw_field *field = &msg->desc->fields[i];
740
+
741
+ if (!pbw_msg_present(msg, i)) {
742
+ continue;
743
+ }
744
+ /* proto3 implicit presence: the reference printer omits defaults. */
745
+ if (pbw_field_implicit_presence(field, msg->desc) &&
746
+ pbw_value_is_default(&msg->values[i])) {
747
+ continue;
748
+ }
749
+ t_print_field(t, field, &msg->values[i], depth);
750
+ if (t->status != PBW_OK) {
751
+ return;
752
+ }
753
+ }
754
+ if (t->print_unknown && msg->unknown_len > 0) {
755
+ if (msg->unknown == NULL) {
756
+ t->status = PBW_ERR_INVALID;
757
+ return;
758
+ }
759
+ t_print_unknowns(t, msg->unknown, msg->unknown + msg->unknown_len,
760
+ depth);
761
+ }
762
+ }
763
+
764
+ pbw_status
765
+ pbw_text_print_opts(const pbw_message *msg, int print_unknown_fields,
766
+ char **out, size_t *len)
767
+ {
768
+ text_out t;
769
+
770
+ if (!pbw_msg_fresh(msg) || out == NULL || len == NULL) {
771
+ return PBW_ERR_INVALID;
772
+ }
773
+ *out = NULL;
774
+ *len = 0;
775
+ pbw_buf_init(&t.buf);
776
+ t.indent = 0;
777
+ t.print_unknown = print_unknown_fields != 0;
778
+ t.status = PBW_OK;
779
+ if (!pbw_buf_reserve(&t.buf, 64)) {
780
+ return PBW_ERR_NOMEM;
781
+ }
782
+ t.buf.len = 0;
783
+ t_print_message(&t, msg, 0);
784
+ if (t.status != PBW_OK) {
785
+ pbw_buf_free(&t.buf);
786
+ return t.status;
787
+ }
788
+ if (!pbw_buf_reserve(&t.buf, 1)) {
789
+ pbw_buf_free(&t.buf);
790
+ return PBW_ERR_NOMEM;
791
+ }
792
+ t.buf.data[t.buf.len] = '\0';
793
+ *out = (char *)t.buf.data;
794
+ *len = t.buf.len;
795
+ return PBW_OK;
796
+ }
797
+
798
+ pbw_status
799
+ pbw_text_print(const pbw_message *msg, char **out, size_t *len)
800
+ {
801
+ /* same default as the reference printer: unknown fields are shown */
802
+ return pbw_text_print_opts(msg, 1, out, len);
803
+ }
804
+
805
+ /* ------------------------------------------------------------------ parsing */
806
+
807
+ typedef struct {
808
+ const char *p;
809
+ const char *end;
810
+ } text_cur;
811
+
812
+ static void
813
+ t_skip(text_cur *c)
814
+ {
815
+ for (;;) {
816
+ while (c->p < c->end &&
817
+ (*c->p == ' ' || *c->p == '\t' || *c->p == '\n' ||
818
+ *c->p == '\v' || *c->p == '\f' || *c->p == '\r')) {
819
+ c->p++;
820
+ }
821
+ if (c->p < c->end && *c->p == '#') {
822
+ while (c->p < c->end && *c->p != '\n') {
823
+ c->p++;
824
+ }
825
+ continue;
826
+ }
827
+ return;
828
+ }
829
+ }
830
+
831
+ static int
832
+ t_is_ident_start(char c)
833
+ {
834
+ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_';
835
+ }
836
+
837
+ static int
838
+ t_is_ident_char(char c)
839
+ {
840
+ return t_is_ident_start(c) || (c >= '0' && c <= '9');
841
+ }
842
+
843
+ static int
844
+ t_is_digit(char c)
845
+ {
846
+ return c >= '0' && c <= '9';
847
+ }
848
+
849
+ static int
850
+ t_is_hex(char c)
851
+ {
852
+ return t_is_digit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
853
+ }
854
+
855
+ /* Parses an identifier; the span covers the name. */
856
+ static int
857
+ t_ident(text_cur *c, const char **start, size_t *len)
858
+ {
859
+ const char *p;
860
+
861
+ t_skip(c);
862
+ p = c->p;
863
+ if (p >= c->end || !t_is_ident_start(*p)) {
864
+ return 0;
865
+ }
866
+ while (p < c->end && t_is_ident_char(*p)) {
867
+ p++;
868
+ }
869
+ *start = c->p;
870
+ *len = (size_t)(p - c->p);
871
+ c->p = p;
872
+ return 1;
873
+ }
874
+
875
+ typedef enum { T_NUM_INT,
876
+ T_NUM_FLOAT,
877
+ T_NUM_BAD } t_num_kind;
878
+
879
+ typedef struct {
880
+ t_num_kind kind;
881
+ int is_hex;
882
+ int is_oct;
883
+ const char *start;
884
+ size_t len;
885
+ } t_number;
886
+
887
+ /* Lexes a number starting at a digit or a dot-followed-by-digit. Mirrors the
888
+ * reference tokenizer: hex needs digits, octal rejects 8/9 later at value
889
+ * time, `e` needs exponent digits, `f/F` suffix makes a float. */
890
+ static int
891
+ t_lex_number(text_cur *c, t_number *out)
892
+ {
893
+ const char *p = c->p;
894
+
895
+ out->is_hex = 0;
896
+ out->is_oct = 0;
897
+ if (p < c->end && *p == '0' && p + 1 < c->end &&
898
+ (p[1] == 'x' || p[1] == 'X')) {
899
+ const char *q = p + 2;
900
+
901
+ while (q < c->end && t_is_hex(*q)) {
902
+ q++;
903
+ }
904
+ if (q == p + 2) {
905
+ return 0; /* "0x" without digits */
906
+ }
907
+ out->kind = T_NUM_INT;
908
+ out->is_hex = 1;
909
+ out->start = p;
910
+ out->len = (size_t)(q - p);
911
+ c->p = q;
912
+ return 1;
913
+ }
914
+ {
915
+ const char *q = p;
916
+ int is_float = 0;
917
+
918
+ while (q < c->end && t_is_digit(*q)) {
919
+ q++;
920
+ }
921
+ if (p < c->end && *p == '0' && q > p + 1) {
922
+ out->is_oct = 1;
923
+ }
924
+ if (q < c->end && *q == '.') {
925
+ is_float = 1;
926
+ q++;
927
+ while (q < c->end && t_is_digit(*q)) {
928
+ q++;
929
+ }
930
+ }
931
+ if (q < c->end && (*q == 'e' || *q == 'E')) {
932
+ const char *r = q + 1;
933
+
934
+ if (r < c->end && (*r == '-' || *r == '+')) {
935
+ r++;
936
+ }
937
+ if (r >= c->end || !t_is_digit(*r)) {
938
+ /* "1e" without exponent: a float-shaped token that no value
939
+ * parser accepts, like the reference. */
940
+ out->kind = T_NUM_FLOAT;
941
+ out->start = p;
942
+ out->len = (size_t)(q - p);
943
+ c->p = q;
944
+ return 1;
945
+ }
946
+ while (r < c->end && t_is_digit(*r)) {
947
+ r++;
948
+ }
949
+ q = r;
950
+ is_float = 1;
951
+ }
952
+ if (q < c->end && (*q == 'f' || *q == 'F')) {
953
+ q++;
954
+ is_float = 1;
955
+ }
956
+ if (q == p) {
957
+ return 0;
958
+ }
959
+ if (out->is_oct && is_float) {
960
+ /* "0" followed by a digit is an octal token for the reference
961
+ * tokenizer, where a decimal point or an exponent is an error
962
+ * (03.5, 01e5). A lone leading zero (0.5) stays a float. */
963
+ return 0;
964
+ }
965
+ out->kind = is_float ? T_NUM_FLOAT : T_NUM_INT;
966
+ out->start = p;
967
+ out->len = (size_t)(q - p);
968
+ c->p = q;
969
+ return 1;
970
+ }
971
+ }
972
+
973
+ /* A letter right after a number is an error ("need space"), like the
974
+ * reference tokenizer. */
975
+ static int
976
+ t_number_end(text_cur *c)
977
+ {
978
+ if (c->p < c->end && t_is_ident_start(*c->p)) {
979
+ return 0;
980
+ }
981
+ return 1;
982
+ }
983
+
984
+ static int
985
+ t_parse_number(text_cur *c, t_number *out)
986
+ {
987
+ int ok;
988
+
989
+ t_skip(c);
990
+ if (c->p >= c->end) {
991
+ return 0;
992
+ }
993
+ if (t_is_digit(*c->p)) {
994
+ ok = t_lex_number(c, out);
995
+ return ok && t_number_end(c);
996
+ }
997
+ if (*c->p == '.' && c->p + 1 < c->end && t_is_digit(c->p[1])) {
998
+ /* leading-dot float, no hex/octal here */
999
+ const char *p = c->p + 1;
1000
+ const char *q = p;
1001
+
1002
+ while (q < c->end && t_is_digit(*q)) {
1003
+ q++;
1004
+ }
1005
+ if (q < c->end && (*q == 'e' || *q == 'E')) {
1006
+ const char *r = q + 1;
1007
+
1008
+ if (r < c->end && (*r == '-' || *r == '+')) {
1009
+ r++;
1010
+ }
1011
+ if (r >= c->end || !t_is_digit(*r)) {
1012
+ out->kind = T_NUM_FLOAT;
1013
+ out->is_hex = 0;
1014
+ out->is_oct = 0;
1015
+ out->start = c->p;
1016
+ out->len = (size_t)(q - c->p);
1017
+ c->p = q;
1018
+ return 1;
1019
+ }
1020
+ while (r < c->end && t_is_digit(*r)) {
1021
+ r++;
1022
+ }
1023
+ q = r;
1024
+ }
1025
+ if (q < c->end && (*q == 'f' || *q == 'F')) {
1026
+ q++;
1027
+ }
1028
+ out->kind = T_NUM_FLOAT;
1029
+ out->is_hex = 0;
1030
+ out->is_oct = 0;
1031
+ out->start = c->p;
1032
+ out->len = (size_t)(q - c->p);
1033
+ c->p = q;
1034
+ return t_number_end(c);
1035
+ }
1036
+ return 0;
1037
+ }
1038
+
1039
+ /* Parses one quoted string; the span includes the quotes. Adjacent strings
1040
+ * are concatenated by the caller. */
1041
+ static int
1042
+ t_parse_string(text_cur *c, const char **start, size_t *len)
1043
+ {
1044
+ const char *p;
1045
+ char quote;
1046
+
1047
+ t_skip(c);
1048
+ if (c->p >= c->end || (*c->p != '"' && *c->p != '\'')) {
1049
+ return 0;
1050
+ }
1051
+ quote = *c->p;
1052
+ p = c->p + 1;
1053
+ for (;;) {
1054
+ if (p >= c->end) {
1055
+ return 0; /* unterminated */
1056
+ }
1057
+ if (*p == '\0') {
1058
+ return 0; /* NUL ends input for the reference too */
1059
+ }
1060
+ if (*p == '\n') {
1061
+ return 0; /* no multiline strings */
1062
+ }
1063
+ if (*p == '\\') {
1064
+ p++;
1065
+ if (p >= c->end || *p == '\n' || *p == '\r') {
1066
+ return 0;
1067
+ }
1068
+ p++;
1069
+ continue;
1070
+ }
1071
+ if (*p == quote) {
1072
+ p++;
1073
+ break;
1074
+ }
1075
+ p++;
1076
+ }
1077
+ *start = c->p;
1078
+ *len = (size_t)(p - c->p);
1079
+ c->p = p;
1080
+ return 1;
1081
+ }
1082
+
1083
+ static int
1084
+ t_hex_val(char c)
1085
+ {
1086
+ if (c >= '0' && c <= '9') {
1087
+ return c - '0';
1088
+ }
1089
+ if (c >= 'a' && c <= 'f') {
1090
+ return c - 'a' + 10;
1091
+ }
1092
+ return c - 'A' + 10;
1093
+ }
1094
+
1095
+ /* Appends the unescaped form of one quoted span. Surrogate halves and
1096
+ * out-of-range code points fail: the reference test suite requires it. */
1097
+ static pbw_status
1098
+ t_unescape(pbw_buf *out, const char *start, size_t len)
1099
+ {
1100
+ /* start includes both quotes */
1101
+ const char *p = start + 1;
1102
+ const char *end = start + len - 1;
1103
+
1104
+ while (p < end) {
1105
+ char c = *p++;
1106
+
1107
+ if (c != '\\') {
1108
+ if (!pbw_buf_reserve(out, 1)) {
1109
+ return PBW_ERR_NOMEM;
1110
+ }
1111
+ out->data[out->len++] = (uint8_t)c;
1112
+ continue;
1113
+ }
1114
+ if (p >= end) {
1115
+ return PBW_ERR_INVALID;
1116
+ }
1117
+ c = *p++;
1118
+ if (c >= '0' && c <= '7') {
1119
+ unsigned v = (unsigned)(c - '0');
1120
+ int i;
1121
+
1122
+ for (i = 0; i < 2 && p < end && *p >= '0' && *p <= '7'; i++) {
1123
+ v = v * 8 + (unsigned)(*p++ - '0');
1124
+ }
1125
+ /* values above 255 truncate, like the reference */
1126
+ if (!pbw_buf_reserve(out, 1)) {
1127
+ return PBW_ERR_NOMEM;
1128
+ }
1129
+ out->data[out->len++] = (uint8_t)v;
1130
+ } else if (c == 'x' || c == 'X') {
1131
+ unsigned v = 0;
1132
+ int digits = 0;
1133
+
1134
+ while (digits < 2 && p < end && t_is_hex(*p)) {
1135
+ v = v * 16 + (unsigned)t_hex_val(*p++);
1136
+ digits++;
1137
+ }
1138
+ if (digits == 0) {
1139
+ return PBW_ERR_INVALID;
1140
+ }
1141
+ if (!pbw_buf_reserve(out, 1)) {
1142
+ return PBW_ERR_NOMEM;
1143
+ }
1144
+ out->data[out->len++] = (uint8_t)v;
1145
+ } else if (c == 'u' || c == 'U') {
1146
+ int want = c == 'u' ? 4 : 8;
1147
+ int i;
1148
+ uint32_t v = 0;
1149
+
1150
+ for (i = 0; i < want; i++) {
1151
+ if (p >= end || !t_is_hex(*p)) {
1152
+ return PBW_ERR_INVALID;
1153
+ }
1154
+ v = v * 16 + (uint32_t)t_hex_val(*p++);
1155
+ }
1156
+ /* The tokenizer already limits \U to 00[01]......; values above
1157
+ * the planes and surrogates fail here. */
1158
+ if (c == 'u' && v >= 0xd800 && v <= 0xdbff && p + 5 < end &&
1159
+ p[0] == '\\' && p[1] == 'u') {
1160
+ /* a lead surrogate followed by \uXXXX: combine the pair
1161
+ * into the scalar, like the reference; a lone surrogate
1162
+ * stays invalid (bad UTF-8, which this parser rejects). */
1163
+ uint32_t trail = 0;
1164
+ int k;
1165
+
1166
+ for (k = 0; k < 4; k++) {
1167
+ if (!t_is_hex(p[2 + k])) {
1168
+ break;
1169
+ }
1170
+ trail = trail * 16 + (uint32_t)t_hex_val(p[2 + k]);
1171
+ }
1172
+ if (k == 4 && trail >= 0xdc00 && trail <= 0xdfff) {
1173
+ v = 0x10000u + ((v - 0xd800u) << 10) + (trail - 0xdc00u);
1174
+ p += 6;
1175
+ }
1176
+ }
1177
+ if (v > 0x10ffff || (v >= 0xd800 && v <= 0xdfff)) {
1178
+ return PBW_ERR_INVALID;
1179
+ }
1180
+ {
1181
+ uint8_t tmp[4];
1182
+ size_t n = 0;
1183
+
1184
+ if (v < 0x80) {
1185
+ tmp[n++] = (uint8_t)v;
1186
+ } else if (v < 0x800) {
1187
+ tmp[n++] = (uint8_t)(0xc0 | (v >> 6));
1188
+ tmp[n++] = (uint8_t)(0x80 | (v & 63));
1189
+ } else if (v < 0x10000) {
1190
+ tmp[n++] = (uint8_t)(0xe0 | (v >> 12));
1191
+ tmp[n++] = (uint8_t)(0x80 | ((v >> 6) & 63));
1192
+ tmp[n++] = (uint8_t)(0x80 | (v & 63));
1193
+ } else {
1194
+ tmp[n++] = (uint8_t)(0xf0 | (v >> 18));
1195
+ tmp[n++] = (uint8_t)(0x80 | ((v >> 12) & 63));
1196
+ tmp[n++] = (uint8_t)(0x80 | ((v >> 6) & 63));
1197
+ tmp[n++] = (uint8_t)(0x80 | (v & 63));
1198
+ }
1199
+ if (!pbw_buf_reserve(out, n)) {
1200
+ return PBW_ERR_NOMEM;
1201
+ }
1202
+ memcpy(out->data + out->len, tmp, n);
1203
+ out->len += n;
1204
+ }
1205
+ } else {
1206
+ char e;
1207
+
1208
+ switch (c) {
1209
+ case 'a':
1210
+ e = '\a';
1211
+ break;
1212
+ case 'b':
1213
+ e = '\b';
1214
+ break;
1215
+ case 'f':
1216
+ e = '\f';
1217
+ break;
1218
+ case 'n':
1219
+ e = '\n';
1220
+ break;
1221
+ case 'r':
1222
+ e = '\r';
1223
+ break;
1224
+ case 't':
1225
+ e = '\t';
1226
+ break;
1227
+ case 'v':
1228
+ e = '\v';
1229
+ break;
1230
+ case '\\':
1231
+ e = '\\';
1232
+ break;
1233
+ case '\'':
1234
+ e = '\'';
1235
+ break;
1236
+ case '"':
1237
+ e = '"';
1238
+ break;
1239
+ case '?':
1240
+ e = '?';
1241
+ break;
1242
+ default:
1243
+ return PBW_ERR_INVALID;
1244
+ }
1245
+ if (!pbw_buf_reserve(out, 1)) {
1246
+ return PBW_ERR_NOMEM;
1247
+ }
1248
+ out->data[out->len++] = (uint8_t)e;
1249
+ }
1250
+ }
1251
+ return PBW_OK;
1252
+ }
1253
+
1254
+ /* Parses one unsigned magnitude with the reference tokenizer rules: hex with
1255
+ * 0x, octal with a leading 0 (8/9 are an error), decimal otherwise. */
1256
+ static pbw_status
1257
+ t_parse_uint(const t_number *num, uint64_t max, uint64_t *out)
1258
+ {
1259
+ const char *p = num->start;
1260
+ size_t len = num->len;
1261
+ uint64_t value = 0;
1262
+
1263
+ if (num->kind != T_NUM_INT) {
1264
+ return PBW_ERR_INVALID;
1265
+ }
1266
+ if (num->is_hex) {
1267
+ size_t i;
1268
+
1269
+ p += 2;
1270
+ len -= 2;
1271
+ for (i = 0; i < len; i++) {
1272
+ unsigned d = (unsigned)t_hex_val(p[i]);
1273
+
1274
+ if (value > max / 16 ||
1275
+ (value == max / 16 && d > max % 16)) {
1276
+ return PBW_ERR_INVALID;
1277
+ }
1278
+ value = value * 16 + d;
1279
+ }
1280
+ *out = value;
1281
+ return PBW_OK;
1282
+ }
1283
+ {
1284
+ size_t i;
1285
+ int base = num->is_oct ? 8 : 10;
1286
+
1287
+ if (num->is_oct) {
1288
+ p += 1;
1289
+ len -= 1;
1290
+ }
1291
+ for (i = 0; i < len; i++) {
1292
+ unsigned d = (unsigned)(p[i] - '0');
1293
+
1294
+ if (d >= (unsigned)base) {
1295
+ return PBW_ERR_INVALID;
1296
+ }
1297
+ if (value > max / (uint64_t)base ||
1298
+ (value == max / (uint64_t)base &&
1299
+ d > max % (uint64_t)base)) {
1300
+ return PBW_ERR_INVALID;
1301
+ }
1302
+ value = value * (uint64_t)base + d;
1303
+ }
1304
+ *out = value;
1305
+ return PBW_OK;
1306
+ }
1307
+ }
1308
+
1309
+ static pbw_status
1310
+ t_parse_sint(const t_number *num, int negative, int64_t min, uint64_t max,
1311
+ int64_t *out)
1312
+ {
1313
+ uint64_t limit = negative ? max + 1 : max;
1314
+ uint64_t mag;
1315
+ pbw_status status = t_parse_uint(num, limit, &mag);
1316
+
1317
+ if (status != PBW_OK) {
1318
+ return status;
1319
+ }
1320
+ if (negative) {
1321
+ if (mag == max + 1) {
1322
+ *out = min;
1323
+ return PBW_OK;
1324
+ }
1325
+ *out = -(int64_t)mag;
1326
+ return PBW_OK;
1327
+ }
1328
+ *out = (int64_t)mag;
1329
+ return PBW_OK;
1330
+ }
1331
+
1332
+ static int
1333
+ t_ident_is(const char *start, size_t len, const char *word)
1334
+ {
1335
+ size_t n = strlen(word);
1336
+
1337
+ return len == n && memcmp(start, word, n) == 0;
1338
+ }
1339
+
1340
+ /* Case-insensitive spelling match: the reference lowercases the identifier
1341
+ * before comparing, so `Inf`, `iNfinity` and `nAN` all parse. */
1342
+ static int
1343
+ t_ident_case_is(const char *start, size_t len, const char *word)
1344
+ {
1345
+ size_t i;
1346
+ size_t n = strlen(word);
1347
+
1348
+ if (len != n) {
1349
+ return 0;
1350
+ }
1351
+ for (i = 0; i < n; i++) {
1352
+ char c = start[i];
1353
+
1354
+ if (c >= 'A' && c <= 'Z') {
1355
+ c = (char)(c + ('a' - 'A'));
1356
+ }
1357
+ if (c != word[i]) {
1358
+ return 0;
1359
+ }
1360
+ }
1361
+ return 1;
1362
+ }
1363
+
1364
+ typedef struct {
1365
+ text_cur cur;
1366
+ pbw_status status;
1367
+ uint32_t dlim; /* nesting cap for this call (pbw_limits.max_depth) */
1368
+ } text_in;
1369
+
1370
+ /* Stores a scalar; repeated fields append. */
1371
+ static pbw_status
1372
+ t_store_uint(text_in *t, pbw_message *msg, const pbw_field *field,
1373
+ uint64_t bits)
1374
+ {
1375
+ pbw_value *item;
1376
+
1377
+ (void)t;
1378
+
1379
+ if (field->label == PBW_LABEL_REPEATED) {
1380
+ item = pbw_value_new_uint_a(msg->arena, bits);
1381
+ if (item == NULL) {
1382
+ return PBW_ERR_NOMEM;
1383
+ }
1384
+ {
1385
+ pbw_value *list = pbw_message_list_for(msg, field);
1386
+
1387
+ if (list == NULL || !pbw_value_list_push(list, item, msg->arena)) {
1388
+ pbw_value_free(item);
1389
+ return PBW_ERR_NOMEM;
1390
+ }
1391
+ }
1392
+ return PBW_OK;
1393
+ }
1394
+ {
1395
+ pbw_value value;
1396
+
1397
+ value.kind = PBW_VALUE_UINT;
1398
+ value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
1399
+ value.as.u64 = bits;
1400
+ pbw_message_put(msg, (size_t)(field - msg->desc->fields), &value);
1401
+ return PBW_OK;
1402
+ }
1403
+ }
1404
+
1405
+ static pbw_status
1406
+ t_parse_int_value(text_in *t, pbw_message *msg, const pbw_field *field)
1407
+ {
1408
+ int negative = 0;
1409
+ t_number num;
1410
+ int64_t smin = 0;
1411
+ uint64_t umax = 0;
1412
+ int is_signed = 0;
1413
+
1414
+ t_skip(&t->cur);
1415
+ if (t->cur.p < t->cur.end && *t->cur.p == '-') {
1416
+ negative = 1;
1417
+ t->cur.p++;
1418
+ }
1419
+ if (!t_parse_number(&t->cur, &num)) {
1420
+ return PBW_ERR_INVALID;
1421
+ }
1422
+ switch (field->type) {
1423
+ case PBW_TYPE_INT32:
1424
+ case PBW_TYPE_SINT32:
1425
+ case PBW_TYPE_SFIXED32:
1426
+ smin = INT32_MIN;
1427
+ umax = INT32_MAX;
1428
+ is_signed = 1;
1429
+ break;
1430
+ case PBW_TYPE_INT64:
1431
+ case PBW_TYPE_SINT64:
1432
+ case PBW_TYPE_SFIXED64:
1433
+ smin = INT64_MIN;
1434
+ umax = INT64_MAX;
1435
+ is_signed = 1;
1436
+ break;
1437
+ case PBW_TYPE_UINT32:
1438
+ case PBW_TYPE_FIXED32:
1439
+ umax = UINT32_MAX;
1440
+ break;
1441
+ case PBW_TYPE_UINT64:
1442
+ case PBW_TYPE_FIXED64:
1443
+ umax = UINT64_MAX;
1444
+ break;
1445
+ case PBW_TYPE_BOOL: {
1446
+ uint64_t v;
1447
+
1448
+ if (negative || t_parse_uint(&num, 1, &v) != PBW_OK) {
1449
+ return PBW_ERR_INVALID;
1450
+ }
1451
+ return t_store_uint(t, msg, field, v);
1452
+ }
1453
+ default:
1454
+ return PBW_ERR_INVALID;
1455
+ }
1456
+ if (!is_signed && negative) {
1457
+ return PBW_ERR_INVALID;
1458
+ }
1459
+ if (is_signed) {
1460
+ int64_t v;
1461
+
1462
+ if (t_parse_sint(&num, negative, smin, umax, &v) != PBW_OK) {
1463
+ return PBW_ERR_INVALID;
1464
+ }
1465
+ /* like the binary decoder, sint values are stored decoded: the
1466
+ * zigzag only happens on the wire */
1467
+ return t_store_uint(t, msg, field, (uint64_t)v);
1468
+ }
1469
+ {
1470
+ uint64_t v;
1471
+
1472
+ if (t_parse_uint(&num, umax, &v) != PBW_OK) {
1473
+ return PBW_ERR_INVALID;
1474
+ }
1475
+ return t_store_uint(t, msg, field, v);
1476
+ }
1477
+ }
1478
+
1479
+ static pbw_status
1480
+ t_parse_float_value(text_in *t, pbw_message *msg, const pbw_field *field,
1481
+ int is_float)
1482
+ {
1483
+ double value = 0;
1484
+ int negative = 0;
1485
+ int have = 0;
1486
+
1487
+ t_skip(&t->cur);
1488
+ if (t->cur.p < t->cur.end && *t->cur.p == '-') {
1489
+ negative = 1;
1490
+ t->cur.p++;
1491
+ t_skip(&t->cur);
1492
+ }
1493
+ if (t->cur.p < t->cur.end &&
1494
+ (t_is_digit(*t->cur.p) ||
1495
+ (*t->cur.p == '.' && t->cur.p + 1 < t->cur.end &&
1496
+ t_is_digit(t->cur.p[1])))) {
1497
+ t_number num;
1498
+
1499
+ if (!t_parse_number(&t->cur, &num)) {
1500
+ return PBW_ERR_INVALID;
1501
+ }
1502
+ if (num.kind != T_NUM_FLOAT && (num.is_hex || num.is_oct)) {
1503
+ /* hex and octal float literals are invalid */
1504
+ return PBW_ERR_INVALID;
1505
+ }
1506
+ if (num.kind != T_NUM_FLOAT) {
1507
+ /* plain decimal integer: allowed; hex and octal are not */
1508
+ char stack[64];
1509
+ char *tmp = stack;
1510
+ char *endp;
1511
+
1512
+ if (num.is_hex || num.is_oct) {
1513
+ return PBW_ERR_INVALID;
1514
+ }
1515
+ /* Numbers fit the stack buffer in practice; a longer one is unusual,
1516
+ * not invalid. */
1517
+ if (num.len >= sizeof(stack)) {
1518
+ if (num.len == SIZE_MAX || num.start == NULL) {
1519
+ return PBW_ERR_INVALID;
1520
+ }
1521
+ tmp = (char *)malloc(num.len + 1);
1522
+ if (tmp == NULL) {
1523
+ return PBW_ERR_NOMEM;
1524
+ }
1525
+ }
1526
+ memcpy(tmp, num.start, num.len);
1527
+ tmp[num.len] = '\0';
1528
+ value = pbw_strtod_c(tmp, &endp);
1529
+ {
1530
+ int ok = endp != tmp;
1531
+
1532
+ if (tmp != stack) {
1533
+ free(tmp);
1534
+ }
1535
+ if (!ok) {
1536
+ return PBW_ERR_INVALID;
1537
+ }
1538
+ }
1539
+ } else {
1540
+ char stack[64];
1541
+ char *tmp = stack;
1542
+ char *endp;
1543
+ size_t used;
1544
+ size_t want = num.len;
1545
+
1546
+ if (num.len >= sizeof(stack)) {
1547
+ if (num.len == SIZE_MAX || num.start == NULL) {
1548
+ return PBW_ERR_INVALID;
1549
+ }
1550
+ tmp = (char *)malloc(num.len + 1);
1551
+ if (tmp == NULL) {
1552
+ return PBW_ERR_NOMEM;
1553
+ }
1554
+ }
1555
+ memcpy(tmp, num.start, num.len);
1556
+ /* strip a trailing f/F: strtod stops there anyway, but a bare
1557
+ * "1e" must fail instead of parsing as 1 */
1558
+ tmp[num.len] = '\0';
1559
+ value = pbw_strtod_c(tmp, &endp);
1560
+ used = (size_t)(endp - tmp);
1561
+ if (want > 0 && (tmp[want - 1] == 'f' || tmp[want - 1] == 'F')) {
1562
+ want--;
1563
+ }
1564
+ if (tmp != stack) {
1565
+ free(tmp);
1566
+ }
1567
+ if (used != want) {
1568
+ return PBW_ERR_INVALID;
1569
+ }
1570
+ }
1571
+ have = 1;
1572
+ } else {
1573
+ const char *start;
1574
+ size_t len;
1575
+
1576
+ if (!t_ident(&t->cur, &start, &len)) {
1577
+ return PBW_ERR_INVALID;
1578
+ }
1579
+ if (t_ident_case_is(start, len, "inf") ||
1580
+ t_ident_case_is(start, len, "infinity")) {
1581
+ value = HUGE_VAL;
1582
+ have = 1;
1583
+ } else if (t_ident_case_is(start, len, "nan")) {
1584
+ value = (double)NAN;
1585
+ have = 1;
1586
+ } else {
1587
+ return PBW_ERR_INVALID;
1588
+ }
1589
+ }
1590
+ if (!have) {
1591
+ return PBW_ERR_INVALID;
1592
+ }
1593
+ if (negative) {
1594
+ value = -value;
1595
+ }
1596
+ if (is_float) {
1597
+ /* Through a uint32: a 4-byte copy into a uint64 fills its high half
1598
+ * on a big-endian host, where the float bits must live in the low. */
1599
+ uint32_t bits32;
1600
+ float f = (float)value;
1601
+
1602
+ memcpy(&bits32, &f, sizeof(bits32));
1603
+ return t_store_uint(t, msg, field, bits32);
1604
+ } else {
1605
+ uint64_t bits = 0;
1606
+
1607
+ memcpy(&bits, &value, sizeof(value));
1608
+ return t_store_uint(t, msg, field, bits);
1609
+ }
1610
+ }
1611
+
1612
+ static pbw_status
1613
+ t_parse_bool_value(text_in *t, pbw_message *msg, const pbw_field *field)
1614
+ {
1615
+ t_skip(&t->cur);
1616
+ if (t->cur.p < t->cur.end &&
1617
+ (t_is_digit(*t->cur.p) || *t->cur.p == '-')) {
1618
+ return t_parse_int_value(t, msg, field);
1619
+ } else {
1620
+ const char *start;
1621
+ size_t len;
1622
+
1623
+ if (!t_ident(&t->cur, &start, &len)) {
1624
+ return PBW_ERR_INVALID;
1625
+ }
1626
+ if (t_ident_is(start, len, "true") || t_ident_is(start, len, "True") ||
1627
+ t_ident_is(start, len, "t")) {
1628
+ return t_store_uint(t, msg, field, 1);
1629
+ }
1630
+ if (t_ident_is(start, len, "false") ||
1631
+ t_ident_is(start, len, "False") || t_ident_is(start, len, "f")) {
1632
+ return t_store_uint(t, msg, field, 0);
1633
+ }
1634
+ return PBW_ERR_INVALID;
1635
+ }
1636
+ }
1637
+
1638
+ static pbw_status
1639
+ t_parse_enum_value(text_in *t, pbw_message *msg, const pbw_field *field)
1640
+ {
1641
+ int32_t number;
1642
+
1643
+ t_skip(&t->cur);
1644
+ if (t->cur.p < t->cur.end &&
1645
+ (t_is_digit(*t->cur.p) || *t->cur.p == '-')) {
1646
+ int negative = 0;
1647
+ t_number num;
1648
+ int64_t v;
1649
+
1650
+ if (*t->cur.p == '-') {
1651
+ negative = 1;
1652
+ t->cur.p++;
1653
+ }
1654
+ if (!t_parse_number(&t->cur, &num)) {
1655
+ return PBW_ERR_INVALID;
1656
+ }
1657
+ if (t_parse_sint(&num, negative, INT32_MIN, INT32_MAX, &v) != PBW_OK) {
1658
+ return PBW_ERR_INVALID;
1659
+ }
1660
+ number = (int32_t)v;
1661
+ } else {
1662
+ const char *start;
1663
+ size_t len;
1664
+ char name[520];
1665
+
1666
+ if (!t_ident(&t->cur, &start, &len)) {
1667
+ return PBW_ERR_INVALID;
1668
+ }
1669
+ if (len >= sizeof(name)) {
1670
+ return PBW_ERR_INVALID;
1671
+ }
1672
+ memcpy(name, start, len);
1673
+ name[len] = '\0';
1674
+ if (field->sub == NULL ||
1675
+ !pbw_enum_find_value(field->sub, name, &number)) {
1676
+ return PBW_ERR_INVALID;
1677
+ }
1678
+ return t_store_uint(t, msg, field, (uint64_t)(int64_t)number);
1679
+ }
1680
+ if (field->sub != NULL &&
1681
+ pbw_enum_find_name(field->sub, number, NULL)) {
1682
+ return t_store_uint(t, msg, field, (uint64_t)(int64_t)number);
1683
+ }
1684
+ /* unknown numbers are kept only by open enums, like on the wire and in
1685
+ * the JSON mapping. Closed-ness can come from the enum type's dialect or
1686
+ * from an editions per-field override, so ask the shared predicate. */
1687
+ if (field->sub == NULL || pbw_enum_is_closed(field)) {
1688
+ return PBW_ERR_INVALID;
1689
+ }
1690
+ return t_store_uint(t, msg, field, (uint64_t)(int64_t)number);
1691
+ }
1692
+
1693
+ static pbw_status
1694
+ t_parse_string_value(text_in *t, pbw_message *msg, const pbw_field *field)
1695
+ {
1696
+ pbw_buf out;
1697
+ pbw_status status;
1698
+ const char *start;
1699
+ size_t len;
1700
+
1701
+ pbw_buf_init(&out);
1702
+ for (;;) {
1703
+ if (!t_parse_string(&t->cur, &start, &len)) {
1704
+ pbw_buf_free(&out);
1705
+ return PBW_ERR_INVALID;
1706
+ }
1707
+ status = t_unescape(&out, start, len);
1708
+ if (status != PBW_OK) {
1709
+ pbw_buf_free(&out);
1710
+ return status;
1711
+ }
1712
+ /* adjacent literals concatenate */
1713
+ {
1714
+ text_cur save = t->cur;
1715
+
1716
+ t_skip(&t->cur);
1717
+ if (t->cur.p >= t->cur.end || (*t->cur.p != '"' && *t->cur.p != '\'')) {
1718
+ t->cur = save;
1719
+ break;
1720
+ }
1721
+ }
1722
+ }
1723
+ if (field->type == PBW_TYPE_STRING &&
1724
+ !pbw_utf8_valid(out.data == NULL ? (const uint8_t *)"" : out.data,
1725
+ out.len)) {
1726
+ pbw_buf_free(&out);
1727
+ return PBW_ERR_INVALID;
1728
+ }
1729
+ if (field->label == PBW_LABEL_REPEATED) {
1730
+ pbw_value *item = pbw_value_new_bytes_a(
1731
+ msg->arena, out.data == NULL ? (const uint8_t *)"" : out.data,
1732
+ out.len);
1733
+
1734
+ pbw_buf_free(&out);
1735
+ if (item == NULL) {
1736
+ return PBW_ERR_NOMEM;
1737
+ }
1738
+ {
1739
+ pbw_value *list = pbw_message_list_for(msg, field);
1740
+
1741
+ if (list == NULL ||
1742
+ !pbw_value_list_push(list, item, msg->arena)) {
1743
+ pbw_value_free(item);
1744
+ return PBW_ERR_NOMEM;
1745
+ }
1746
+ }
1747
+ return PBW_OK;
1748
+ }
1749
+ {
1750
+ pbw_value value;
1751
+
1752
+ if (!pbw_value_init_bytes(
1753
+ &value, msg->arena,
1754
+ out.data == NULL ? (const uint8_t *)"" : out.data,
1755
+ out.len)) {
1756
+ pbw_buf_free(&out);
1757
+ return PBW_ERR_NOMEM;
1758
+ }
1759
+ pbw_buf_free(&out);
1760
+ pbw_message_put(msg, (size_t)(field - msg->desc->fields), &value);
1761
+ return PBW_OK;
1762
+ }
1763
+ }
1764
+
1765
+ /* Parses one scalar (non-message) value of `field`. */
1766
+ static pbw_status
1767
+ t_parse_scalar_value(text_in *t, pbw_message *msg, const pbw_field *field)
1768
+ {
1769
+ switch (field->type) {
1770
+ case PBW_TYPE_DOUBLE:
1771
+ return t_parse_float_value(t, msg, field, 0);
1772
+ case PBW_TYPE_FLOAT:
1773
+ return t_parse_float_value(t, msg, field, 1);
1774
+ case PBW_TYPE_BOOL:
1775
+ return t_parse_bool_value(t, msg, field);
1776
+ case PBW_TYPE_ENUM:
1777
+ return t_parse_enum_value(t, msg, field);
1778
+ case PBW_TYPE_STRING:
1779
+ case PBW_TYPE_BYTES:
1780
+ return t_parse_string_value(t, msg, field);
1781
+ default:
1782
+ return t_parse_int_value(t, msg, field);
1783
+ }
1784
+ }
1785
+
1786
+ /* Skips one value whose type we can only guess (reserved field names). */
1787
+ static pbw_status
1788
+ t_skip_value(text_in *t, uint32_t depth)
1789
+ {
1790
+ t_skip(&t->cur);
1791
+ if (t->cur.p >= t->cur.end) {
1792
+ return PBW_ERR_INVALID;
1793
+ }
1794
+ if (*t->cur.p == '{' || *t->cur.p == '<') {
1795
+ char closer = *t->cur.p == '{' ? '}' : '>';
1796
+ pbw_status status;
1797
+
1798
+ if (depth >= t->dlim) {
1799
+ return PBW_ERR_LIMIT;
1800
+ }
1801
+ t->cur.p++;
1802
+ /* skip nested fields without storing anything */
1803
+ for (;;) {
1804
+ t_skip(&t->cur);
1805
+ if (t->cur.p >= t->cur.end) {
1806
+ return PBW_ERR_INVALID;
1807
+ }
1808
+ if (*t->cur.p == closer) {
1809
+ t->cur.p++;
1810
+ return PBW_OK;
1811
+ }
1812
+ /* field name or [extension] */
1813
+ if (*t->cur.p == '[') {
1814
+ int d = 1;
1815
+
1816
+ t->cur.p++;
1817
+ while (t->cur.p < t->cur.end && d > 0) {
1818
+ if (*t->cur.p == '[') {
1819
+ d++;
1820
+ } else if (*t->cur.p == ']') {
1821
+ d--;
1822
+ }
1823
+ t->cur.p++;
1824
+ }
1825
+ if (d != 0) {
1826
+ return PBW_ERR_INVALID;
1827
+ }
1828
+ } else {
1829
+ const char *start;
1830
+ size_t len;
1831
+
1832
+ if (!t_ident(&t->cur, &start, &len)) {
1833
+ return PBW_ERR_INVALID;
1834
+ }
1835
+ }
1836
+ t_skip(&t->cur);
1837
+ if (t->cur.p < t->cur.end &&
1838
+ (*t->cur.p == '{' || *t->cur.p == '<')) {
1839
+ status = t_skip_value(t, depth + 1);
1840
+ } else {
1841
+ if (t->cur.p >= t->cur.end || *t->cur.p != ':') {
1842
+ return PBW_ERR_INVALID;
1843
+ }
1844
+ t->cur.p++;
1845
+ status = t_skip_value(t, depth + 1);
1846
+ }
1847
+ if (status != PBW_OK) {
1848
+ return status;
1849
+ }
1850
+ t_skip(&t->cur);
1851
+ if (t->cur.p < t->cur.end &&
1852
+ (*t->cur.p == ',' || *t->cur.p == ';')) {
1853
+ t->cur.p++;
1854
+ }
1855
+ }
1856
+ }
1857
+ if (*t->cur.p == '[') {
1858
+ /* a list: skip comma-separated values */
1859
+ if (depth >= t->dlim) {
1860
+ return PBW_ERR_LIMIT;
1861
+ }
1862
+ t->cur.p++;
1863
+ t_skip(&t->cur);
1864
+ if (t->cur.p < t->cur.end && *t->cur.p == ']') {
1865
+ t->cur.p++;
1866
+ return PBW_OK;
1867
+ }
1868
+ for (;;) {
1869
+ pbw_status status = t_skip_value(t, depth + 1);
1870
+
1871
+ if (status != PBW_OK) {
1872
+ return status;
1873
+ }
1874
+ t_skip(&t->cur);
1875
+ if (t->cur.p < t->cur.end && *t->cur.p == ',') {
1876
+ t->cur.p++;
1877
+ continue;
1878
+ }
1879
+ if (t->cur.p < t->cur.end && *t->cur.p == ']') {
1880
+ t->cur.p++;
1881
+ return PBW_OK;
1882
+ }
1883
+ return PBW_ERR_INVALID;
1884
+ }
1885
+ }
1886
+ {
1887
+ const char *start;
1888
+ size_t len;
1889
+ t_number num;
1890
+
1891
+ if (t_parse_string(&t->cur, &start, &len)) {
1892
+ /* adjacent strings: keep skipping them as one value */
1893
+ for (;;) {
1894
+ text_cur save = t->cur;
1895
+
1896
+ t_skip(&t->cur);
1897
+ if (t->cur.p >= t->cur.end ||
1898
+ (*t->cur.p != '"' && *t->cur.p != '\'')) {
1899
+ t->cur = save;
1900
+ break;
1901
+ }
1902
+ if (!t_parse_string(&t->cur, &start, &len)) {
1903
+ return PBW_ERR_INVALID;
1904
+ }
1905
+ }
1906
+ return PBW_OK;
1907
+ }
1908
+ if (t->cur.p < t->cur.end && *t->cur.p == '-') {
1909
+ t->cur.p++;
1910
+ t_skip(&t->cur);
1911
+ }
1912
+ if (t_parse_number(&t->cur, &num)) {
1913
+ return PBW_OK;
1914
+ }
1915
+ if (t_ident(&t->cur, &start, &len)) {
1916
+ return PBW_OK;
1917
+ }
1918
+ return PBW_ERR_INVALID;
1919
+ }
1920
+ }
1921
+
1922
+ static int
1923
+ t_is_url_char(char c)
1924
+ {
1925
+ return t_is_ident_char(c) || c == '-' || c == '.' || c == '~' || c == '!' ||
1926
+ c == '$' || c == '&' || c == '(' || c == ')' || c == '*' ||
1927
+ c == '+' || c == ',' || c == ';' || c == '=' || c == '%' ||
1928
+ c == '/';
1929
+ }
1930
+
1931
+ /* Collects raw bracket contents, skipping whitespace and comments. Only the
1932
+ * reference tokenizer's URL characters accumulate; anything else ends the
1933
+ * collection and fails at the `]` check, like the reference. */
1934
+ static pbw_status
1935
+ t_parse_brackets(text_in *t, pbw_buf *out)
1936
+ {
1937
+ pbw_buf_init(out);
1938
+ t_skip(&t->cur);
1939
+ if (t->cur.p >= t->cur.end || *t->cur.p != '[') {
1940
+ return PBW_ERR_INVALID;
1941
+ }
1942
+ t->cur.p++;
1943
+ for (;;) {
1944
+ t_skip(&t->cur);
1945
+ if (t->cur.p >= t->cur.end) {
1946
+ pbw_buf_free(out);
1947
+ return PBW_ERR_INVALID;
1948
+ }
1949
+ if (*t->cur.p == ']') {
1950
+ t->cur.p++;
1951
+ if (!pbw_buf_reserve(out, 1)) {
1952
+ pbw_buf_free(out);
1953
+ return PBW_ERR_NOMEM;
1954
+ }
1955
+ out->data[out->len] = '\0';
1956
+ return PBW_OK;
1957
+ }
1958
+ if (!t_is_url_char(*t->cur.p)) {
1959
+ pbw_buf_free(out);
1960
+ return PBW_ERR_INVALID;
1961
+ }
1962
+ if (!pbw_buf_reserve(out, 1)) {
1963
+ pbw_buf_free(out);
1964
+ return PBW_ERR_NOMEM;
1965
+ }
1966
+ out->data[out->len++] = (uint8_t)*t->cur.p++;
1967
+ }
1968
+ }
1969
+
1970
+ /* Dotted identifiers only ("a.b.C"), no slashes. */
1971
+ static int
1972
+ t_valid_type_name(const char *s)
1973
+ {
1974
+ const char *p = s;
1975
+
1976
+ if (*p == '\0') {
1977
+ return 0;
1978
+ }
1979
+ for (;;) {
1980
+ if (!t_is_ident_start(*p)) {
1981
+ return 0;
1982
+ }
1983
+ while (t_is_ident_char(*p)) {
1984
+ p++;
1985
+ }
1986
+ if (*p == '\0') {
1987
+ return 1;
1988
+ }
1989
+ if (*p != '.') {
1990
+ return 0;
1991
+ }
1992
+ p++;
1993
+ }
1994
+ }
1995
+
1996
+ static pbw_status t_parse_message_body(text_in *t, pbw_message *msg,
1997
+ uint32_t depth, char closer);
1998
+
1999
+ /* Field name lookup for the text format: the plain name, then the lowercase
2000
+ * form (groups only, when it matches the lowercased type name), then a group's
2001
+ * unqualified type name (the printer emits that, so the parse has to accept
2002
+ * it). `reserved` reports a name the descriptor declared reserved, which the
2003
+ * parser skips. The name buffers live in this non-recursive frame: the
2004
+ * recursive body does not pay for them, and they are sized for the longest
2005
+ * identifier the descriptor loader accepts (512 bytes). */
2006
+ static pbw_status
2007
+ t_lookup_field(const pbw_message *msg, const char *name, size_t name_len,
2008
+ const pbw_field **out, int *reserved)
2009
+ {
2010
+ char tmp[520];
2011
+ char lower[520];
2012
+ size_t i;
2013
+
2014
+ *out = NULL;
2015
+ *reserved = 0;
2016
+ if (name_len >= sizeof(tmp)) {
2017
+ return PBW_ERR_INVALID;
2018
+ }
2019
+ memcpy(tmp, name, name_len);
2020
+ tmp[name_len] = '\0';
2021
+ *out = pbw_descriptor_find_field_name(msg->desc, tmp);
2022
+ /* extensions are only addressable as [full.name] */
2023
+ if (*out != NULL && (*out)->is_extension) {
2024
+ *out = NULL;
2025
+ }
2026
+ if (*out == NULL) {
2027
+ for (i = 0; i < name_len; i++) {
2028
+ char c = tmp[i];
2029
+
2030
+ lower[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
2031
+ }
2032
+ lower[name_len] = '\0';
2033
+ *out = pbw_descriptor_find_field_name(msg->desc, lower);
2034
+ if (*out != NULL &&
2035
+ ((*out)->is_extension || (*out)->type != PBW_TYPE_GROUP ||
2036
+ (*out)->sub == NULL ||
2037
+ strcmp(short_name((*out)->sub->name), tmp) != 0)) {
2038
+ *out = NULL;
2039
+ }
2040
+ for (i = 0; *out == NULL && i < msg->desc->field_count; i++) {
2041
+ const pbw_field *g = &msg->desc->fields[i];
2042
+ const char *short_sub;
2043
+
2044
+ if (g->is_extension || g->type != PBW_TYPE_GROUP ||
2045
+ g->sub == NULL) {
2046
+ continue;
2047
+ }
2048
+ short_sub = short_name(g->sub->name);
2049
+ if (strlen(short_sub) == name_len &&
2050
+ memcmp(short_sub, name, name_len) == 0) {
2051
+ *out = g;
2052
+ break;
2053
+ }
2054
+ }
2055
+ }
2056
+ if (*out == NULL && pbw_descriptor_is_reserved(msg->desc, tmp)) {
2057
+ *reserved = 1;
2058
+ }
2059
+ return PBW_OK;
2060
+ }
2061
+ static pbw_status t_parse_list(text_in *t, pbw_message *msg,
2062
+ const pbw_field *field, uint32_t depth);
2063
+
2064
+ /* Any expansion when the parser already consumed nothing: reads [url]{...}
2065
+ * and fills the Any message `msg` itself (no field: the message IS the Any). */
2066
+ static pbw_status
2067
+ t_parse_any_inner(text_in *t, pbw_message *msg, uint32_t depth)
2068
+ {
2069
+ pbw_buf raw;
2070
+ pbw_status status;
2071
+ const char *slash;
2072
+ const char *type_name;
2073
+ const pbw_descriptor *desc;
2074
+ pbw_message *inner = NULL;
2075
+ uint8_t *bytes = NULL;
2076
+ size_t bytes_len = 0;
2077
+ size_t i;
2078
+
2079
+ status = t_parse_brackets(t, &raw);
2080
+ if (status != PBW_OK) {
2081
+ return status;
2082
+ }
2083
+ slash = NULL;
2084
+ for (i = 0; i < raw.len; i++) {
2085
+ if (raw.data[i] == '/') {
2086
+ slash = (const char *)raw.data + i;
2087
+ }
2088
+ }
2089
+ if (slash == NULL || slash == (const char *)raw.data ||
2090
+ ((const char *)raw.data)[0] == '/') {
2091
+ pbw_buf_free(&raw);
2092
+ return PBW_ERR_INVALID;
2093
+ }
2094
+ for (i = 0; (const char *)raw.data + i < slash; i++) {
2095
+ if (raw.data[i] == '%') {
2096
+ if (i + 2 >= (size_t)(slash - (const char *)raw.data) ||
2097
+ !t_is_hex((char)raw.data[i + 1]) ||
2098
+ !t_is_hex((char)raw.data[i + 2])) {
2099
+ pbw_buf_free(&raw);
2100
+ return PBW_ERR_INVALID;
2101
+ }
2102
+ i += 2;
2103
+ }
2104
+ }
2105
+ type_name = slash + 1;
2106
+ if (!t_valid_type_name(type_name)) {
2107
+ pbw_buf_free(&raw);
2108
+ return PBW_ERR_INVALID;
2109
+ }
2110
+ if (msg->desc->schema == NULL ||
2111
+ (desc = pbw_schema_find(msg->desc->schema, type_name)) == NULL ||
2112
+ desc->is_enum) {
2113
+ pbw_buf_free(&raw);
2114
+ return PBW_ERR_INVALID;
2115
+ }
2116
+ t_skip(&t->cur);
2117
+ if (t->cur.p < t->cur.end && *t->cur.p == ':') {
2118
+ t->cur.p++;
2119
+ }
2120
+ t_skip(&t->cur);
2121
+ if (t->cur.p >= t->cur.end ||
2122
+ (*t->cur.p != '{' && *t->cur.p != '<')) {
2123
+ pbw_buf_free(&raw);
2124
+ return PBW_ERR_INVALID;
2125
+ }
2126
+ inner = pbw_message_new(desc);
2127
+ if (inner == NULL) {
2128
+ pbw_buf_free(&raw);
2129
+ return PBW_ERR_NOMEM;
2130
+ }
2131
+ {
2132
+ char closer = *t->cur.p == '{' ? '}' : '>';
2133
+
2134
+ t->cur.p++;
2135
+ status = t_parse_message_body(t, inner, depth + 1, closer);
2136
+ }
2137
+ if (status != PBW_OK) {
2138
+ pbw_message_free(inner);
2139
+ pbw_buf_free(&raw);
2140
+ return status;
2141
+ }
2142
+ status = pbw_encode(inner, &bytes, &bytes_len);
2143
+ pbw_message_free(inner);
2144
+ if (status != PBW_OK) {
2145
+ pbw_buf_free(&raw);
2146
+ return status;
2147
+ }
2148
+ {
2149
+ const pbw_field *url_f = pbw_descriptor_find_field(msg->desc, 1);
2150
+ const pbw_field *val_f = pbw_descriptor_find_field(msg->desc, 2);
2151
+ pbw_value *url_v;
2152
+ pbw_value *val_v;
2153
+
2154
+ if (url_f == NULL || val_f == NULL) {
2155
+ pbw_buf_free(&raw);
2156
+ pbw_free(bytes);
2157
+ return PBW_ERR_INVALID;
2158
+ }
2159
+ url_v = pbw_value_new_string((const char *)raw.data);
2160
+ val_v = pbw_value_new_bytes(bytes, bytes_len);
2161
+ pbw_buf_free(&raw);
2162
+ pbw_free(bytes);
2163
+ if (url_v == NULL || val_v == NULL) {
2164
+ pbw_value_free(url_v);
2165
+ pbw_value_free(val_v);
2166
+ return PBW_ERR_NOMEM;
2167
+ }
2168
+ /* set() owns the value on success only, so each one is released on
2169
+ * its own failure path: freeing url_v after a successful set would
2170
+ * touch memory the message already owns. */
2171
+ status = pbw_message_set(msg, 1, url_v);
2172
+ if (status != PBW_OK) {
2173
+ pbw_value_free(url_v);
2174
+ pbw_value_free(val_v);
2175
+ } else {
2176
+ status = pbw_message_set(msg, 2, val_v);
2177
+ if (status != PBW_OK) {
2178
+ pbw_value_free(val_v);
2179
+ }
2180
+ }
2181
+ if (status != PBW_OK) {
2182
+ /* like the message body above: malformed content is
2183
+ * INVALID, only resource failures propagate */
2184
+ return status == PBW_ERR_LIMIT || status == PBW_ERR_NOMEM
2185
+ ? status
2186
+ : PBW_ERR_INVALID;
2187
+ }
2188
+ return PBW_OK;
2189
+ }
2190
+ }
2191
+
2192
+ static pbw_status
2193
+ t_parse_message_body(text_in *t, pbw_message *msg, uint32_t depth,
2194
+ char closer);
2195
+
2196
+ /* Parses a message-typed value: `{...}` or `<...>`, merging into an existing
2197
+ * singular submessage like the binary decoder does. */
2198
+ static pbw_status
2199
+ t_parse_message_value(text_in *t, pbw_message *msg, const pbw_field *field,
2200
+ uint32_t depth)
2201
+ {
2202
+ char closer;
2203
+ pbw_message *slot;
2204
+
2205
+ if (depth >= t->dlim) {
2206
+ return PBW_ERR_LIMIT;
2207
+ }
2208
+ t_skip(&t->cur);
2209
+ if (t->cur.p < t->cur.end && *t->cur.p == ':') {
2210
+ t->cur.p++;
2211
+ t_skip(&t->cur);
2212
+ }
2213
+ /* Like the reference, a repeated message or group also accepts the
2214
+ * list form (`subs [{...}, ...]`); a singular one never does. */
2215
+ if (field->label == PBW_LABEL_REPEATED &&
2216
+ t->cur.p < t->cur.end && *t->cur.p == '[') {
2217
+ return t_parse_list(t, msg, field, depth);
2218
+ }
2219
+ if (t->cur.p >= t->cur.end ||
2220
+ (*t->cur.p != '{' && *t->cur.p != '<')) {
2221
+ return PBW_ERR_INVALID;
2222
+ }
2223
+ closer = *t->cur.p == '{' ? '}' : '>';
2224
+ t->cur.p++;
2225
+ if (field->label == PBW_LABEL_REPEATED) {
2226
+ slot = pbw_message_new(field->sub);
2227
+ if (slot == NULL) {
2228
+ return PBW_ERR_NOMEM;
2229
+ }
2230
+ {
2231
+ pbw_status status = t_parse_message_body(t, slot, depth + 1,
2232
+ closer);
2233
+
2234
+ if (status != PBW_OK) {
2235
+ pbw_message_free(slot);
2236
+ return status;
2237
+ }
2238
+ }
2239
+ {
2240
+ pbw_value *item = pbw_value_new_message(slot);
2241
+
2242
+ if (item == NULL) {
2243
+ pbw_message_free(slot);
2244
+ return PBW_ERR_NOMEM;
2245
+ }
2246
+ {
2247
+ pbw_value *list = pbw_message_list_for(msg, field);
2248
+
2249
+ if (list == NULL ||
2250
+ !pbw_value_list_push(list, item, msg->arena)) {
2251
+ pbw_value_free(item);
2252
+ return PBW_ERR_NOMEM;
2253
+ }
2254
+ }
2255
+ return PBW_OK;
2256
+ }
2257
+ }
2258
+ slot = pbw_message_set_message(msg, field->number);
2259
+ if (slot == NULL) {
2260
+ return PBW_ERR_INVALID;
2261
+ }
2262
+ return t_parse_message_body(t, slot, depth + 1, closer);
2263
+ }
2264
+
2265
+ /* Parses a `[a, b]` list for a repeated scalar field. */
2266
+ static pbw_status
2267
+ t_parse_list(text_in *t, pbw_message *msg, const pbw_field *field,
2268
+ uint32_t depth)
2269
+ {
2270
+ t_skip(&t->cur);
2271
+ if (t->cur.p >= t->cur.end || *t->cur.p != '[') {
2272
+ return PBW_ERR_INVALID;
2273
+ }
2274
+ t->cur.p++;
2275
+ t_skip(&t->cur);
2276
+ if (t->cur.p < t->cur.end && *t->cur.p == ']') {
2277
+ t->cur.p++;
2278
+ return PBW_OK; /* empty list */
2279
+ }
2280
+ for (;;) {
2281
+ pbw_status status;
2282
+
2283
+ if (field->type == PBW_TYPE_MESSAGE || field->type == PBW_TYPE_GROUP) {
2284
+ /* embedded messages in a list parse like standalone values */
2285
+ t_skip(&t->cur);
2286
+ if (t->cur.p >= t->cur.end ||
2287
+ (*t->cur.p != '{' && *t->cur.p != '<')) {
2288
+ return PBW_ERR_INVALID;
2289
+ }
2290
+ {
2291
+ char closer = *t->cur.p == '{' ? '}' : '>';
2292
+
2293
+ t->cur.p++;
2294
+ {
2295
+ pbw_message *slot = pbw_message_new(field->sub);
2296
+
2297
+ if (slot == NULL) {
2298
+ return PBW_ERR_NOMEM;
2299
+ }
2300
+ status = t_parse_message_body(t, slot, depth + 1,
2301
+ closer);
2302
+ if (status != PBW_OK) {
2303
+ pbw_message_free(slot);
2304
+ return status;
2305
+ }
2306
+ {
2307
+ pbw_value *item = pbw_value_new_message(slot);
2308
+
2309
+ if (item == NULL) {
2310
+ pbw_message_free(slot);
2311
+ return PBW_ERR_NOMEM;
2312
+ }
2313
+ {
2314
+ pbw_value *list =
2315
+ pbw_message_list_for(msg, field);
2316
+
2317
+ if (list == NULL ||
2318
+ !pbw_value_list_push(list, item,
2319
+ msg->arena)) {
2320
+ pbw_value_free(item);
2321
+ return PBW_ERR_NOMEM;
2322
+ }
2323
+ }
2324
+ }
2325
+ }
2326
+ }
2327
+ } else {
2328
+ /* adjacent string literals already concatenated into one
2329
+ * value inside the scalar parser */
2330
+ status = t_parse_scalar_value(t, msg, field);
2331
+ if (status != PBW_OK) {
2332
+ return status;
2333
+ }
2334
+ }
2335
+ t_skip(&t->cur);
2336
+ if (t->cur.p < t->cur.end && *t->cur.p == ',') {
2337
+ t->cur.p++;
2338
+ t_skip(&t->cur);
2339
+ if (t->cur.p < t->cur.end && *t->cur.p == ']') {
2340
+ return PBW_ERR_INVALID; /* trailing comma */
2341
+ }
2342
+ continue;
2343
+ }
2344
+ if (t->cur.p < t->cur.end && *t->cur.p == ']') {
2345
+ t->cur.p++;
2346
+ return PBW_OK;
2347
+ }
2348
+ return PBW_ERR_INVALID;
2349
+ }
2350
+ }
2351
+
2352
+ static pbw_status
2353
+ t_parse_message_body(text_in *t, pbw_message *msg, uint32_t depth,
2354
+ char closer)
2355
+ {
2356
+ if (depth > t->dlim) {
2357
+ return PBW_ERR_LIMIT;
2358
+ }
2359
+ for (;;) {
2360
+ const char *name = NULL;
2361
+ size_t name_len = 0;
2362
+ const pbw_field *field = NULL;
2363
+ int is_bracket = 0;
2364
+ pbw_status status;
2365
+
2366
+ t_skip(&t->cur);
2367
+ if (t->cur.p >= t->cur.end) {
2368
+ return closer == 0 ? PBW_OK : PBW_ERR_INVALID;
2369
+ }
2370
+ if (closer != 0 && *t->cur.p == closer) {
2371
+ t->cur.p++;
2372
+ return PBW_OK;
2373
+ }
2374
+ if (*t->cur.p == '[') {
2375
+ is_bracket = 1;
2376
+ } else if (!t_ident(&t->cur, &name, &name_len)) {
2377
+ return PBW_ERR_INVALID;
2378
+ }
2379
+ if (is_bracket) {
2380
+ /* Any expansion only applies inside google.protobuf.Any;
2381
+ * everywhere else brackets hold an extension name. */
2382
+ if (is_any_message(msg->desc)) {
2383
+ const pbw_field *url_f =
2384
+ pbw_descriptor_find_field(msg->desc, 1);
2385
+ /* like the reference, a second expansion is an error */
2386
+ if (url_f != NULL &&
2387
+ pbw_msg_present(msg,
2388
+ (size_t)(url_f - msg->desc->fields))) {
2389
+ return PBW_ERR_INVALID;
2390
+ }
2391
+ status = t_parse_any_inner(t, msg, depth);
2392
+ if (status != PBW_OK) {
2393
+ return status;
2394
+ }
2395
+ t_skip(&t->cur);
2396
+ if (t->cur.p < t->cur.end &&
2397
+ (*t->cur.p == ',' || *t->cur.p == ';')) {
2398
+ t->cur.p++;
2399
+ }
2400
+ continue;
2401
+ }
2402
+ {
2403
+ pbw_buf raw;
2404
+ const pbw_field *ext;
2405
+
2406
+ status = t_parse_brackets(t, &raw);
2407
+ if (status != PBW_OK) {
2408
+ return status;
2409
+ }
2410
+ {
2411
+ size_t i;
2412
+
2413
+ for (i = 0; i < raw.len; i++) {
2414
+ if (raw.data[i] == '/') {
2415
+ pbw_buf_free(&raw);
2416
+ return PBW_ERR_INVALID;
2417
+ }
2418
+ }
2419
+ }
2420
+ if (!t_valid_type_name((const char *)raw.data)) {
2421
+ pbw_buf_free(&raw);
2422
+ return PBW_ERR_INVALID;
2423
+ }
2424
+ ext = msg->desc->schema == NULL
2425
+ ? NULL
2426
+ : pbw_schema_find_extension(
2427
+ msg->desc->schema, (const char *)raw.data);
2428
+ pbw_buf_free(&raw);
2429
+ if (ext == NULL) {
2430
+ return PBW_ERR_INVALID;
2431
+ }
2432
+ /* the extension must belong to this message */
2433
+ {
2434
+ const pbw_field *own =
2435
+ pbw_descriptor_find_field(msg->desc, ext->number);
2436
+
2437
+ if (own != ext) {
2438
+ return PBW_ERR_INVALID;
2439
+ }
2440
+ }
2441
+ field = ext;
2442
+ }
2443
+ } else {
2444
+ int reserved = 0;
2445
+
2446
+ status = t_lookup_field(msg, name, name_len, &field, &reserved);
2447
+ if (status != PBW_OK) {
2448
+ return status;
2449
+ }
2450
+ if (field == NULL) {
2451
+ if (reserved) {
2452
+ /* reserved names are skipped by guessing the value */
2453
+ t_skip(&t->cur);
2454
+ if (t->cur.p < t->cur.end &&
2455
+ (*t->cur.p == '{' || *t->cur.p == '<')) {
2456
+ status = t_skip_value(t, depth);
2457
+ } else {
2458
+ if (t->cur.p >= t->cur.end || *t->cur.p != ':') {
2459
+ return PBW_ERR_INVALID;
2460
+ }
2461
+ t->cur.p++;
2462
+ status = t_skip_value(t, depth);
2463
+ }
2464
+ if (status != PBW_OK) {
2465
+ return status;
2466
+ }
2467
+ t_skip(&t->cur);
2468
+ if (t->cur.p < t->cur.end &&
2469
+ (*t->cur.p == ',' || *t->cur.p == ';')) {
2470
+ t->cur.p++;
2471
+ }
2472
+ continue;
2473
+ }
2474
+ return PBW_ERR_INVALID;
2475
+ }
2476
+ }
2477
+ /* like the reference parser, singular fields and oneof members may
2478
+ * only be specified once (no last-wins, no message merge) */
2479
+ if (field->label != PBW_LABEL_REPEATED) {
2480
+ size_t index = (size_t)(field - msg->desc->fields);
2481
+
2482
+ if (pbw_msg_present(msg, index)) {
2483
+ return PBW_ERR_INVALID;
2484
+ }
2485
+ if (field->oneof_index != 0 &&
2486
+ msg->oneof_case[field->oneof_index] != 0) {
2487
+ return PBW_ERR_INVALID;
2488
+ }
2489
+ }
2490
+ if (field->type == PBW_TYPE_MESSAGE || field->type == PBW_TYPE_GROUP) {
2491
+ status = t_parse_message_value(t, msg, field, depth);
2492
+ } else {
2493
+ /* scalar: a colon is required */
2494
+ t_skip(&t->cur);
2495
+ if (t->cur.p >= t->cur.end || *t->cur.p != ':') {
2496
+ return PBW_ERR_INVALID;
2497
+ }
2498
+ t->cur.p++;
2499
+ if (field->label == PBW_LABEL_REPEATED) {
2500
+ text_cur save = t->cur;
2501
+
2502
+ t_skip(&t->cur);
2503
+ if (t->cur.p < t->cur.end && *t->cur.p == '[') {
2504
+ t->cur = save;
2505
+ status = t_parse_list(t, msg, field, depth);
2506
+ } else {
2507
+ t->cur = save;
2508
+ status = t_parse_scalar_value(t, msg, field);
2509
+ }
2510
+ } else {
2511
+ status = t_parse_scalar_value(t, msg, field);
2512
+ }
2513
+ }
2514
+ if (status != PBW_OK) {
2515
+ return status;
2516
+ }
2517
+ t_skip(&t->cur);
2518
+ if (t->cur.p < t->cur.end && (*t->cur.p == ',' || *t->cur.p == ';')) {
2519
+ t->cur.p++;
2520
+ }
2521
+ }
2522
+ }
2523
+
2524
+ pbw_status
2525
+ pbw_text_parse_limits(const pbw_descriptor *desc, const char *text, size_t len,
2526
+ const pbw_limits *limits, pbw_message **out)
2527
+ {
2528
+ text_in t;
2529
+ pbw_message *msg;
2530
+ pbw_status status;
2531
+ uint32_t dlim = PBW_MAX_DEPTH;
2532
+
2533
+ if (out != NULL) {
2534
+ *out = NULL;
2535
+ }
2536
+ if (desc == NULL || out == NULL || desc->is_enum) {
2537
+ return PBW_ERR_INVALID;
2538
+ }
2539
+ if (text == NULL && len > 0) {
2540
+ return PBW_ERR_INVALID;
2541
+ }
2542
+ if (limits != NULL) {
2543
+ /* Same bounds semantics as the binary decoder: 0 = default, above the
2544
+ * cap clamps, and the byte cap is checked before any recursion. */
2545
+ if (limits->max_depth != 0) {
2546
+ dlim = limits->max_depth < PBW_MAX_DEPTH ? limits->max_depth
2547
+ : PBW_MAX_DEPTH;
2548
+ }
2549
+ if (limits->max_bytes != 0 && len > limits->max_bytes) {
2550
+ return PBW_ERR_LIMIT;
2551
+ }
2552
+ }
2553
+ if (desc->schema != NULL && !desc->schema->linked) {
2554
+ return PBW_ERR_INVALID;
2555
+ }
2556
+ msg = pbw_message_new(desc);
2557
+ if (msg == NULL) {
2558
+ return PBW_ERR_NOMEM;
2559
+ }
2560
+ t.cur.p = text == NULL ? "" : text;
2561
+ t.cur.end = t.cur.p + len;
2562
+ t.status = PBW_OK;
2563
+ t.dlim = dlim;
2564
+ status = t_parse_message_body(&t, msg, 0, 0);
2565
+ if (status != PBW_OK) {
2566
+ pbw_message_free(msg);
2567
+ return status;
2568
+ }
2569
+ /* a missing proto2 `required` field fails, like the binary decoder */
2570
+ if (desc->has_required && !pbw_message_is_initialized(msg)) {
2571
+ pbw_message_free(msg);
2572
+ return PBW_ERR_REQUIRED;
2573
+ }
2574
+ *out = msg;
2575
+ return PBW_OK;
2576
+ }
2577
+
2578
+ pbw_status
2579
+ pbw_text_parse(const pbw_descriptor *desc, const char *text, size_t len,
2580
+ pbw_message **out)
2581
+ {
2582
+ return pbw_text_parse_limits(desc, text, len, NULL, out);
2583
+ }