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,1735 @@
1
+ /* Wire decoder: Protobuf binary -> dynamic message tree. Unknown fields are
2
+ * skipped. */
3
+
4
+ #include "internal.h"
5
+
6
+ typedef struct {
7
+ const uint8_t *p;
8
+ const uint8_t *end;
9
+ } reader;
10
+
11
+ PBW_INLINE uint64_t
12
+ zigzag_decode(uint64_t value, unsigned width)
13
+ {
14
+ uint64_t mask = width == 32 ? 0xffffffffull : ~0ull;
15
+ uint64_t u = value & mask;
16
+
17
+ return ((u >> 1) ^ (0ull - (u & 1))) & mask;
18
+ }
19
+
20
+ /* Little-endian loads without a cross-translation-unit call: the fixed-width
21
+ * fast paths run them once per field. */
22
+ PBW_INLINE uint32_t
23
+ dec_le32(const uint8_t *p)
24
+ {
25
+ return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
26
+ ((uint32_t)p[3] << 24);
27
+ }
28
+
29
+ PBW_INLINE uint64_t
30
+ dec_le64(const uint8_t *p)
31
+ {
32
+ return (uint64_t)dec_le32(p) | ((uint64_t)dec_le32(p + 4) << 32);
33
+ }
34
+
35
+ /* Reads a varint. One- and two-byte values (most tags and small numbers) are
36
+ * handled here; longer ones go through the general reader. */
37
+ PBW_INLINE int
38
+ read_varint(reader *r, uint64_t *out)
39
+ {
40
+ size_t used;
41
+
42
+ if (r == NULL || r->p == NULL || r->end == NULL || out == NULL) {
43
+ return 0;
44
+ }
45
+ if (pbw_remaining(r->end, r->p) >= 2) {
46
+ uint32_t first = r->p[0];
47
+
48
+ if (first < 0x80) {
49
+ *out = first;
50
+ r->p += 1;
51
+ return 1;
52
+ }
53
+ if (r->p[1] < 0x80) {
54
+ *out = (uint64_t)(first & 0x7f) | ((uint64_t)r->p[1] << 7);
55
+ r->p += 2;
56
+ return 1;
57
+ }
58
+ } else if (r->p < r->end && r->p[0] < 0x80) {
59
+ *out = r->p[0];
60
+ r->p += 1;
61
+ return 1;
62
+ }
63
+ if (!pbw_varint_read(r->p, r->end, out, &used)) {
64
+ return 0;
65
+ }
66
+ r->p += used;
67
+ return 1;
68
+ }
69
+
70
+ /* Upfront batch cap for packed runs: a hostile run (megabytes of varint
71
+ * terminators with an undecodable tail) must fail on its first byte, not
72
+ * on a whole-run reservation. Past the cap the reserve covers one chunk
73
+ * and the loop grows geometrically; bench tiers peak at 3000 packed items,
74
+ * so the fast path below never sees it. */
75
+ #define PBW_PACKED_BATCH_MAX 4096u
76
+
77
+ static pbw_status skip_group(reader *r, uint32_t number, uint32_t depth, uint32_t dlim);
78
+ static pbw_status skip_field(reader *r, pbw_message *msg, uint32_t number,
79
+ uint32_t wire_type, uint32_t depth, uint32_t dlim,
80
+ const uint8_t *start);
81
+ static pbw_status decode_message(pbw_arena *arena, const pbw_descriptor *desc,
82
+ const uint8_t *buf, size_t len,
83
+ pbw_message **out, uint32_t depth,
84
+ uint32_t dlim);
85
+
86
+ static pbw_status
87
+ skip_value(reader *r, uint32_t number, uint32_t wire_type,
88
+ uint32_t depth, uint32_t dlim)
89
+ {
90
+ uint64_t scratch;
91
+ size_t len;
92
+ size_t used;
93
+
94
+ switch (wire_type) {
95
+ case PBW_WIRE_VARINT:
96
+ return read_varint(r, &scratch) ? PBW_OK : PBW_ERR_INVALID;
97
+ case PBW_WIRE_64BIT:
98
+ if (pbw_remaining(r->end, r->p) < 8) {
99
+ return PBW_ERR_INVALID;
100
+ }
101
+ r->p += 8;
102
+ return PBW_OK;
103
+ case PBW_WIRE_32BIT:
104
+ if (pbw_remaining(r->end, r->p) < 4) {
105
+ return PBW_ERR_INVALID;
106
+ }
107
+ r->p += 4;
108
+ return PBW_OK;
109
+ case PBW_WIRE_LEN:
110
+ if (!pbw_len_read(r->p, r->end, &len, &used)) {
111
+ return PBW_ERR_INVALID;
112
+ }
113
+ r->p += used;
114
+ if (len > pbw_remaining(r->end, r->p)) {
115
+ return PBW_ERR_INVALID;
116
+ }
117
+ r->p += len;
118
+ return PBW_OK;
119
+ case PBW_WIRE_START_GROUP:
120
+ return skip_group(r, number, depth, dlim);
121
+ default:
122
+ return PBW_ERR_INVALID;
123
+ }
124
+ }
125
+
126
+ /* Skips a legacy group, matching its END_GROUP tag. Group skipping is
127
+ * recursive, so nesting is bounded by PBW_MAX_DEPTH. */
128
+ static pbw_status
129
+ skip_group(reader *r, uint32_t number, uint32_t depth, uint32_t dlim)
130
+ {
131
+ if (depth >= dlim) {
132
+ return PBW_ERR_LIMIT;
133
+ }
134
+ for (;;) {
135
+ uint32_t tag;
136
+ size_t used;
137
+ uint32_t wire_type;
138
+
139
+ if (r->p >= r->end) {
140
+ return PBW_ERR_INVALID;
141
+ }
142
+ if (!pbw_tag_read(r->p, r->end, &tag, &used)) {
143
+ return PBW_ERR_INVALID;
144
+ }
145
+ r->p += used;
146
+ wire_type = tag & 7;
147
+ /* Field number 0 is invalid, even inside a skipped group: the reference
148
+ * parser validates every tag it walks past. */
149
+ if ((tag >> 3) == 0) {
150
+ return PBW_ERR_INVALID;
151
+ }
152
+ if (wire_type == PBW_WIRE_END_GROUP) {
153
+ return (tag >> 3) == number ? PBW_OK : PBW_ERR_INVALID;
154
+ }
155
+ {
156
+ pbw_status status = skip_value(r, tag >> 3, wire_type, depth + 1, dlim);
157
+ if (status != PBW_OK) {
158
+ return status;
159
+ }
160
+ }
161
+ }
162
+ }
163
+
164
+ /* Skips one field and keeps its bytes. `start` points at the tag. */
165
+ static pbw_status
166
+ skip_field(reader *r, pbw_message *msg, uint32_t number, uint32_t wire_type,
167
+ uint32_t depth, uint32_t dlim, const uint8_t *start)
168
+ {
169
+ pbw_status status = skip_value(r, number, wire_type, depth, dlim);
170
+
171
+ if (status != PBW_OK) {
172
+ return status;
173
+ }
174
+ if (start == NULL || r->p == NULL || r->p < start) {
175
+ return PBW_ERR_INVALID;
176
+ }
177
+ return (pbw_status)pbw_message_add_unknown(msg, start,
178
+ pbw_remaining(r->p, start));
179
+ }
180
+
181
+ /* Reads one scalar of the given type. Strings/bytes/messages are handled by the
182
+ * caller. */
183
+ static pbw_status
184
+ read_scalar(reader *r, pbw_type type, uint64_t *out)
185
+ {
186
+ uint64_t raw;
187
+
188
+ switch (type) {
189
+ case PBW_TYPE_INT32:
190
+ case PBW_TYPE_ENUM:
191
+ if (!read_varint(r, &raw)) {
192
+ return PBW_ERR_INVALID;
193
+ }
194
+ *out = (uint64_t)(int64_t)(int32_t)raw;
195
+ return PBW_OK;
196
+ case PBW_TYPE_INT64:
197
+ case PBW_TYPE_UINT64:
198
+ return read_varint(r, out) ? PBW_OK : PBW_ERR_INVALID;
199
+ case PBW_TYPE_UINT32:
200
+ if (!read_varint(r, &raw)) {
201
+ return PBW_ERR_INVALID;
202
+ }
203
+ *out = (uint32_t)raw;
204
+ return PBW_OK;
205
+ case PBW_TYPE_SINT32:
206
+ if (!read_varint(r, &raw)) {
207
+ return PBW_ERR_INVALID;
208
+ }
209
+ *out = (uint64_t)(int64_t)(int32_t)zigzag_decode(raw, 32);
210
+ return PBW_OK;
211
+ case PBW_TYPE_SINT64:
212
+ if (!read_varint(r, &raw)) {
213
+ return PBW_ERR_INVALID;
214
+ }
215
+ *out = zigzag_decode(raw, 64);
216
+ return PBW_OK;
217
+ case PBW_TYPE_BOOL:
218
+ if (!read_varint(r, &raw)) {
219
+ return PBW_ERR_INVALID;
220
+ }
221
+ *out = raw != 0 ? 1u : 0u;
222
+ return PBW_OK;
223
+ case PBW_TYPE_FIXED32:
224
+ case PBW_TYPE_SFIXED32:
225
+ case PBW_TYPE_FLOAT:
226
+ if (pbw_remaining(r->end, r->p) < 4) {
227
+ return PBW_ERR_INVALID;
228
+ }
229
+ *out = (uint64_t)dec_le32(r->p);
230
+ if (type == PBW_TYPE_SFIXED32) {
231
+ /* Signed fixed values are represented sign extended, like int32. */
232
+ *out = (uint64_t)(int64_t)(int32_t)*out;
233
+ }
234
+ r->p += 4;
235
+ return PBW_OK;
236
+ case PBW_TYPE_FIXED64:
237
+ case PBW_TYPE_SFIXED64:
238
+ case PBW_TYPE_DOUBLE:
239
+ if (pbw_remaining(r->end, r->p) < 8) {
240
+ return PBW_ERR_INVALID;
241
+ }
242
+ *out = dec_le64(r->p);
243
+ r->p += 8;
244
+ return PBW_OK;
245
+ default:
246
+ return PBW_ERR_INVALID;
247
+ }
248
+ }
249
+
250
+ static pbw_status
251
+ read_length(reader *r, const uint8_t **data, size_t *len)
252
+ {
253
+ size_t value;
254
+ size_t used;
255
+
256
+ /* One-byte length (strings shorter than 128 bytes, the common case): the
257
+ * generic reader is a call into wire.c for what is one compare and add. */
258
+ if (r->p < r->end && r->p[0] < 0x80) {
259
+ size_t n = r->p[0];
260
+ size_t avail = pbw_remaining(r->end, r->p);
261
+
262
+ if (avail < 1 || n > avail - 1) {
263
+ return PBW_ERR_INVALID;
264
+ }
265
+ *data = r->p + 1;
266
+ *len = n;
267
+ r->p += 1 + n;
268
+ return PBW_OK;
269
+ }
270
+ if (!pbw_len_read(r->p, r->end, &value, &used)) {
271
+ return PBW_ERR_INVALID;
272
+ }
273
+ r->p += used;
274
+ if (value > pbw_remaining(r->end, r->p)) {
275
+ return PBW_ERR_INVALID;
276
+ }
277
+ *data = r->p;
278
+ *len = value;
279
+ r->p += value;
280
+ return PBW_OK;
281
+ }
282
+
283
+ /* Writes a scalar straight into its field slot: the common case of a singular,
284
+ * non-oneof field seen for the first time. */
285
+ static inline void
286
+ store_scalar(pbw_message *msg, size_t index, uint64_t bits)
287
+ {
288
+ pbw_value *value = &msg->values[index];
289
+
290
+ value->kind = PBW_VALUE_UINT;
291
+ value->flags = PBW_VF_EMBEDDED |
292
+ (msg->arena != NULL ? PBW_VF_ARENA : 0u);
293
+ value->as.u64 = bits;
294
+ pbw_msg_set_present(msg, index, 1);
295
+ }
296
+
297
+ /* Singular scalar fast path: first occurrence writes the slot directly,
298
+ * later ones overwrite through the generic store. */
299
+ static inline void
300
+ store_first_or_put(pbw_message *msg, size_t index, uint64_t bits)
301
+ {
302
+ if (!pbw_msg_present(msg, index)) {
303
+ store_scalar(msg, index, bits);
304
+ return;
305
+ }
306
+ {
307
+ pbw_value value;
308
+
309
+ value.kind = PBW_VALUE_UINT;
310
+ value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
311
+ value.as.u64 = bits;
312
+ pbw_message_put(msg, index, &value);
313
+ }
314
+ }
315
+
316
+ /* Singular string/bytes first occurrence: payload straight into the slot. */
317
+ static int
318
+ store_bytes_first(pbw_message *msg, size_t index, const uint8_t *data,
319
+ size_t len)
320
+ {
321
+ pbw_value *slot = &msg->values[index];
322
+ uint8_t *copy;
323
+
324
+ if (len > SIZE_MAX - 1) {
325
+ return 0;
326
+ }
327
+ if (len > 0 && data == NULL) {
328
+ return 0;
329
+ }
330
+ if (msg->arena != NULL) {
331
+ copy = (uint8_t *)pbw_arena_alloc(msg->arena, len + 1);
332
+ } else {
333
+ copy = (uint8_t *)malloc(len + 1);
334
+ }
335
+ if (copy == NULL) {
336
+ return 0;
337
+ }
338
+ if (len > 0) {
339
+ memcpy(copy, data, len);
340
+ }
341
+ copy[len] = '\0';
342
+ slot->kind = PBW_VALUE_BYTES;
343
+ slot->flags = PBW_VF_EMBEDDED |
344
+ (msg->arena != NULL ? PBW_VF_ARENA : 0u);
345
+ slot->as.bytes.data = copy;
346
+ slot->as.bytes.len = len;
347
+ pbw_msg_set_present(msg, index, 1);
348
+ return 1;
349
+ }
350
+
351
+ /* One decode loop's repeated-append cache. A repeated field's list lives in
352
+ * its message slot and never moves, so once created it stays valid for the
353
+ * whole decode_fields()/decode_group_contents() invocation: every later
354
+ * item of the same field skips the get-or-create call. The kind check keeps
355
+ * this safe even if a slot ever held something else. */
356
+ typedef struct {
357
+ const pbw_field *field;
358
+ pbw_value *list;
359
+ } rep_cache;
360
+
361
+ /* Appends one value to a repeated field. Returns PBW_OK or PBW_ERR_NOMEM. */
362
+ PBW_INLINE pbw_status
363
+ rep_append(pbw_message *msg, const pbw_field *field, pbw_value *item,
364
+ rep_cache *rep)
365
+ {
366
+ pbw_value *list;
367
+
368
+ if (item == NULL) {
369
+ return PBW_ERR_NOMEM;
370
+ }
371
+ if (rep->field == field && rep->list != NULL &&
372
+ rep->list->kind == PBW_VALUE_LIST) {
373
+ list = rep->list;
374
+ } else {
375
+ list = pbw_message_list_for(msg, field);
376
+ if (list == NULL) {
377
+ pbw_value_free(item);
378
+ return PBW_ERR_NOMEM;
379
+ }
380
+ rep->field = field;
381
+ rep->list = list;
382
+ }
383
+ if (!pbw_value_list_push(list, item, msg->arena)) {
384
+ pbw_value_free(item);
385
+ return PBW_ERR_NOMEM;
386
+ }
387
+ return PBW_OK;
388
+ }
389
+
390
+ /* Two-byte tag reader: field numbers 16..2047 encode to two bytes
391
+ * (first byte has the high bit set, second does not). The field lookup and
392
+ * the action switch below are shared with every other tag size. */
393
+ PBW_INLINE int
394
+ decode_tag2(const uint8_t *p, const uint8_t *end, uint32_t *tag)
395
+ {
396
+ if (p == NULL || end == NULL || tag == NULL ||
397
+ pbw_remaining(end, p) < 2 || p[0] < 0x80 || p[1] >= 0x80) {
398
+ return 0;
399
+ }
400
+ *tag = (uint32_t)(p[0] & 0x7f) | ((uint32_t)p[1] << 7);
401
+ return 1;
402
+ }
403
+
404
+ static pbw_status decode_fields(pbw_message *msg, const uint8_t *buf,
405
+ size_t len, uint32_t depth, uint32_t dlim);
406
+ static pbw_status decode_message(pbw_arena *arena, const pbw_descriptor *desc,
407
+ const uint8_t *buf, size_t len,
408
+ pbw_message **out, uint32_t depth, uint32_t dlim);
409
+ static pbw_status read_field(reader *r, const pbw_field *field,
410
+ pbw_message *msg, uint32_t depth, uint32_t dlim,
411
+ const uint8_t *start, int closed, rep_cache *rep);
412
+ static pbw_status read_packed(reader *r, const pbw_field *field,
413
+ pbw_message *msg, int closed, rep_cache *rep);
414
+
415
+ /* Reads the body of a group: fields of `group` until the END_GROUP tag that
416
+ * matches `number`. Same field rules as decode_fields, only the terminator
417
+ * differs. */
418
+ static pbw_status
419
+ decode_group_contents(reader *r, pbw_message *group, uint32_t number,
420
+ uint32_t depth, uint32_t dlim)
421
+ {
422
+ const pbw_descriptor *desc = group->desc;
423
+ rep_cache rep = {NULL, NULL};
424
+
425
+ /* No depth check here: both callers (read_group_field, and its merge path)
426
+ * already applied the same cap before recursing, and a second check would
427
+ * cost groups one nesting level against messages and unknown groups. */
428
+ for (;;) {
429
+ uint32_t tag;
430
+ size_t used = 1;
431
+ uint32_t wire_type;
432
+ uint32_t fnum;
433
+ const pbw_field *field;
434
+ const uint8_t *start = r->p;
435
+ pbw_status status;
436
+
437
+ if (r->p >= r->end) {
438
+ return PBW_ERR_INVALID; /* END_GROUP never came */
439
+ }
440
+ if (r->p[0] < 0x80) {
441
+ tag = r->p[0];
442
+ r->p++;
443
+ } else if (pbw_tag_read(r->p, r->end, &tag, &used)) {
444
+ r->p += used;
445
+ } else {
446
+ return PBW_ERR_INVALID;
447
+ }
448
+ wire_type = tag & 7;
449
+ fnum = tag >> 3;
450
+ if (fnum == 0 || wire_type > PBW_WIRE_32BIT) {
451
+ return PBW_ERR_INVALID;
452
+ }
453
+ if (wire_type == PBW_WIRE_END_GROUP) {
454
+ return fnum == number ? PBW_OK : PBW_ERR_INVALID;
455
+ }
456
+ if (desc->fnum_map != NULL) {
457
+ field = fnum < desc->fnum_map_size ? desc->fnum_map[fnum] : NULL;
458
+ } else {
459
+ field = pbw_find_field(desc, fnum);
460
+ }
461
+ if (field == NULL) {
462
+ status = skip_field(r, group, fnum, wire_type, depth, dlim, start);
463
+ } else if (wire_type == field->wire_type) {
464
+ status = read_field(r, field, group, depth, dlim, start,
465
+ desc->has_closed_enum, &rep);
466
+ } else if (wire_type == PBW_WIRE_LEN &&
467
+ field->label == PBW_LABEL_REPEATED &&
468
+ pbw_is_packable(field->type)) {
469
+ status = read_packed(r, field, group, desc->has_closed_enum, &rep);
470
+ } else {
471
+ status = skip_field(r, group, fnum, wire_type, depth, dlim, start);
472
+ }
473
+ if (status != PBW_OK) {
474
+ return status;
475
+ }
476
+ }
477
+ }
478
+
479
+ /* A group field: START_GROUP was already consumed, the body runs to the
480
+ * matching END_GROUP. Singular groups merge like singular messages. */
481
+ static pbw_status
482
+ read_group_field(reader *r, const pbw_field *field, pbw_message *msg,
483
+ uint32_t depth, uint32_t dlim, rep_cache *rep)
484
+ {
485
+ size_t index = (size_t)(field - msg->desc->fields);
486
+ pbw_message *nested = NULL;
487
+ pbw_status status;
488
+
489
+ if (depth >= dlim) {
490
+ return PBW_ERR_LIMIT;
491
+ }
492
+ if (field->label != PBW_LABEL_REPEATED &&
493
+ (field->oneof_index == 0 ||
494
+ msg->oneof_case[field->oneof_index] == field->number) &&
495
+ pbw_msg_present(msg, index) &&
496
+ msg->values[index].kind == PBW_VALUE_MESSAGE &&
497
+ msg->values[index].as.msg != NULL &&
498
+ msg->values[index].as.msg->desc == field->sub) {
499
+ return decode_group_contents(r, msg->values[index].as.msg,
500
+ field->number, depth + 1, dlim);
501
+ }
502
+ nested = pbw_message_new_a(msg->arena, field->sub);
503
+ if (nested == NULL) {
504
+ return PBW_ERR_NOMEM;
505
+ }
506
+ status = decode_group_contents(r, nested, field->number, depth + 1, dlim);
507
+ if (status != PBW_OK) {
508
+ pbw_message_free(nested);
509
+ return status;
510
+ }
511
+ if (field->label == PBW_LABEL_REPEATED) {
512
+ pbw_value *item = pbw_value_new_message_a(msg->arena, nested);
513
+
514
+ if (item == NULL) {
515
+ pbw_message_free(nested);
516
+ return PBW_ERR_NOMEM;
517
+ }
518
+ return rep_append(msg, field, item, rep);
519
+ }
520
+ {
521
+ pbw_value value;
522
+
523
+ value.kind = PBW_VALUE_MESSAGE;
524
+ value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
525
+ value.as.msg = nested;
526
+ pbw_message_put(msg, index, &value);
527
+ }
528
+ return PBW_OK;
529
+ }
530
+
531
+ /* A repeated numeric field may arrive packed regardless of the descriptor
532
+ * flag. */
533
+ static pbw_status
534
+ read_packed(reader *r, const pbw_field *field, pbw_message *msg, int closed,
535
+ rep_cache *rep)
536
+ {
537
+ const uint8_t *data;
538
+ size_t data_len;
539
+ pbw_status status;
540
+ reader sub;
541
+ pbw_value *vlist;
542
+ pbw_value **items;
543
+ pbw_value *batch = NULL;
544
+ uint32_t batched = 0;
545
+ uint32_t len;
546
+ uint32_t cap;
547
+ /* A closed enum drops items whose number it does not know, so a batched
548
+ * node block would hold nodes nothing uses: keep that case per-item. */
549
+ int batchable;
550
+
551
+ status = read_length(r, &data, &data_len);
552
+ if (status != PBW_OK) {
553
+ return status;
554
+ }
555
+ if (data == NULL) {
556
+ return data_len == 0 ? PBW_OK : PBW_ERR_INVALID;
557
+ }
558
+ if (data_len == 0) {
559
+ /* An empty packed field stays absent: no list is created for it. */
560
+ return PBW_OK;
561
+ }
562
+ /* A closed enum drops unknown values, so the scan count would over-reserve
563
+ * and the node block would hold nodes nothing uses: that case keeps the
564
+ * per-item path (and no scan, no reserve). Measured neutral against the
565
+ * scan+reserve form on a 100k-value run, so the smaller diff wins. */
566
+ batchable = !(closed && pbw_enum_is_closed(field));
567
+ /* Fixed-width items divide the length exactly, so the count is known
568
+ * before the first item: reserve it and skip the push growth chain.
569
+ * Varint runs count their items the same way: every value ends on a
570
+ * byte without the continuation bit. Only worth a scan past 64 bytes;
571
+ * shorter runs grow at most three times. */
572
+ vlist = pbw_message_list_for(msg, field);
573
+ if (vlist == NULL) {
574
+ return PBW_ERR_NOMEM;
575
+ }
576
+ rep->field = field;
577
+ rep->list = vlist;
578
+ if (batchable && data_len > 64 && field->type != PBW_TYPE_FIXED32 &&
579
+ field->type != PBW_TYPE_SFIXED32 && field->type != PBW_TYPE_FLOAT &&
580
+ field->type != PBW_TYPE_FIXED64 &&
581
+ field->type != PBW_TYPE_SFIXED64 && field->type != PBW_TYPE_DOUBLE) {
582
+ size_t n = 0;
583
+ size_t k;
584
+
585
+ for (k = 0; k < data_len; k++) {
586
+ n += (size_t)(data[k] < 0x80);
587
+ }
588
+ if (n > PBW_PACKED_BATCH_MAX) {
589
+ /* Hostile-sized run: reserve one chunk, grow geometrically,
590
+ * and leave the batch block off so the loop allocates the
591
+ * few nodes it actually stores before failing. */
592
+ if (!pbw_value_list_reserve(vlist, PBW_PACKED_BATCH_MAX,
593
+ msg->arena)) {
594
+ return PBW_ERR_NOMEM;
595
+ }
596
+ } else {
597
+ if (!pbw_value_list_reserve(
598
+ vlist, n > UINT32_MAX ? UINT32_MAX : (uint32_t)n,
599
+ msg->arena)) {
600
+ return PBW_ERR_NOMEM;
601
+ }
602
+ /* The scan counted the items: one bump for every node of this run
603
+ * instead of one allocation per item. */
604
+ if (n > 0 && n <= UINT32_MAX &&
605
+ n <= SIZE_MAX / sizeof(pbw_value)) {
606
+ batch = (pbw_value *)pbw_arena_alloc(
607
+ msg->arena, n * sizeof(pbw_value));
608
+ }
609
+ }
610
+ } else if (field->type == PBW_TYPE_FIXED32 ||
611
+ field->type == PBW_TYPE_SFIXED32 ||
612
+ field->type == PBW_TYPE_FLOAT ||
613
+ field->type == PBW_TYPE_FIXED64 ||
614
+ field->type == PBW_TYPE_SFIXED64 ||
615
+ field->type == PBW_TYPE_DOUBLE) {
616
+ size_t width = (field->type == PBW_TYPE_FIXED64 ||
617
+ field->type == PBW_TYPE_SFIXED64 ||
618
+ field->type == PBW_TYPE_DOUBLE)
619
+ ? 8u
620
+ : 4u;
621
+ size_t count = data_len / width;
622
+
623
+ if (count > PBW_PACKED_BATCH_MAX) {
624
+ if (!pbw_value_list_reserve(vlist, PBW_PACKED_BATCH_MAX,
625
+ msg->arena)) {
626
+ return PBW_ERR_NOMEM;
627
+ }
628
+ } else {
629
+ if (!pbw_value_list_reserve(
630
+ vlist, count > UINT32_MAX ? UINT32_MAX : (uint32_t)count,
631
+ msg->arena)) {
632
+ return PBW_ERR_NOMEM;
633
+ }
634
+ /* Fixed-width items need no scan: the payload length divides exactly,
635
+ * so the node block comes from one bump like a varint run's. A
636
+ * truncated tail fails in read_scalar() before it can index past the
637
+ * block: each successful read consumes `width` bytes. */
638
+ if (batchable && count > 0 && count <= UINT32_MAX &&
639
+ count <= SIZE_MAX / sizeof(pbw_value)) {
640
+ batch = (pbw_value *)pbw_arena_alloc(
641
+ msg->arena, count * sizeof(pbw_value));
642
+ }
643
+ }
644
+ }
645
+ items = vlist->as.list.items;
646
+ len = vlist->as.list.len;
647
+ cap = vlist->as.list.cap;
648
+ sub.p = data;
649
+ sub.end = data + data_len;
650
+ {
651
+ int closed_enum = !batchable; /* loop-invariant */
652
+ while (sub.p < sub.end) {
653
+ const uint8_t *item = sub.p;
654
+ uint64_t bits;
655
+ pbw_value *node;
656
+
657
+ status = read_scalar(&sub, field->type, &bits);
658
+ if (status != PBW_OK) {
659
+ return status;
660
+ }
661
+ if (closed_enum &&
662
+ !pbw_enum_find_name(field->sub, (int32_t)bits, NULL)) {
663
+ /* A closed enum's unknown value leaves the field: the reference
664
+ * parsers keep it as an unpacked unknown field instead. The
665
+ * list locals stay untouched: only the unknown bytes grow. */
666
+ status = (pbw_status)pbw_message_add_unknown(msg, field->tag,
667
+ field->tag_len);
668
+ if (status == PBW_OK) {
669
+ if (item == NULL || sub.p == NULL || sub.p < item) {
670
+ return PBW_ERR_INVALID;
671
+ }
672
+ status = (pbw_status)pbw_message_add_unknown(
673
+ msg, item, pbw_remaining(sub.p, item));
674
+ }
675
+ if (status != PBW_OK) {
676
+ return status;
677
+ }
678
+ continue;
679
+ }
680
+ if (batch != NULL) {
681
+ node = &batch[batched++];
682
+ node->kind = PBW_VALUE_UINT;
683
+ node->flags = PBW_VF_ARENA;
684
+ node->as.u64 = bits;
685
+ } else {
686
+ node = pbw_value_new_uint_a(msg->arena, bits);
687
+ if (node == NULL) {
688
+ return PBW_ERR_NOMEM;
689
+ }
690
+ }
691
+ if (len == cap) {
692
+ /* Same doubling as pbw_value_list_push, through the shared
693
+ * reserve: a push-equivalent growth without a call per item. */
694
+ uint32_t ncap = cap != 0 ? cap * 2u : 8u;
695
+ vlist->as.list.items = items;
696
+ vlist->as.list.len = len;
697
+ if (ncap < cap ||
698
+ !pbw_value_list_reserve(vlist, ncap, msg->arena)) {
699
+ pbw_value_free(node);
700
+ return PBW_ERR_NOMEM;
701
+ }
702
+ items = vlist->as.list.items;
703
+ cap = vlist->as.list.cap;
704
+ }
705
+ items[len++] = node;
706
+ }
707
+ }
708
+ vlist->as.list.items = items;
709
+ vlist->as.list.len = len;
710
+ if (len == 0) {
711
+ /* A closed-enum run whose values were all unknown stored nothing: the
712
+ * field stays absent, like an empty packed run, and the cached slot is
713
+ * dropped so the next occurrence creates a fresh list. */
714
+ pbw_msg_set_present(msg, (size_t)(field - msg->desc->fields), 0);
715
+ rep->field = NULL;
716
+ rep->list = NULL;
717
+ }
718
+ return PBW_OK;
719
+ }
720
+
721
+ /* A `string` field whose utf8_validation resolves to VERIFY must carry valid
722
+ * UTF-8: the reference parsers reject such a message instead of keeping bytes
723
+ * that later render as replacement characters. proto2 strings and `bytes`
724
+ * fields are unchecked, exactly like the reference. */
725
+ PBW_INLINE pbw_status
726
+ check_utf8(const pbw_field *field, const uint8_t *data, size_t len)
727
+ {
728
+ if (field->utf8_check && !pbw_utf8_ok(data, len)) {
729
+ return PBW_ERR_INVALID;
730
+ }
731
+ return PBW_OK;
732
+ }
733
+
734
+ static pbw_status
735
+ read_string_field(reader *r, const pbw_field *field, pbw_message *msg,
736
+ rep_cache *rep)
737
+ {
738
+ const uint8_t *data;
739
+ size_t data_len;
740
+ pbw_status status;
741
+
742
+ status = read_length(r, &data, &data_len);
743
+ if (status != PBW_OK) {
744
+ return status;
745
+ }
746
+ status = check_utf8(field, data, data_len);
747
+ if (status != PBW_OK) {
748
+ return status;
749
+ }
750
+ if (field->label == PBW_LABEL_REPEATED) {
751
+ return rep_append(
752
+ msg, field, pbw_value_new_bytes_a(msg->arena, data, data_len),
753
+ rep);
754
+ }
755
+ {
756
+ pbw_value value;
757
+
758
+ if (!pbw_value_init_bytes(&value, msg->arena, data, data_len)) {
759
+ return PBW_ERR_NOMEM;
760
+ }
761
+ pbw_message_put(msg, (size_t)(field - msg->desc->fields), &value);
762
+ }
763
+ return PBW_OK;
764
+ }
765
+
766
+ static pbw_status
767
+ read_message_field(reader *r, const pbw_field *field, pbw_message *msg,
768
+ uint32_t depth, uint32_t dlim, rep_cache *rep)
769
+ {
770
+ const uint8_t *data;
771
+ size_t data_len;
772
+ size_t index = (size_t)(field - msg->desc->fields);
773
+ pbw_message *nested = NULL;
774
+ pbw_status status;
775
+
776
+ if (depth >= dlim) {
777
+ return PBW_ERR_LIMIT;
778
+ }
779
+ status = read_length(r, &data, &data_len);
780
+ if (status != PBW_OK) {
781
+ return status;
782
+ }
783
+ /* A singular message field that appears more than once is merged into the
784
+ * existing submessage, not replaced (the wire format spec requires it;
785
+ * reference implementations do the same). */
786
+ if (field->label != PBW_LABEL_REPEATED &&
787
+ (field->oneof_index == 0 ||
788
+ msg->oneof_case[field->oneof_index] == field->number) &&
789
+ pbw_msg_present(msg, index) &&
790
+ msg->values[index].kind == PBW_VALUE_MESSAGE &&
791
+ msg->values[index].as.msg != NULL &&
792
+ msg->values[index].as.msg->desc == field->sub) {
793
+ return decode_fields(msg->values[index].as.msg, data, data_len,
794
+ depth + 1, dlim);
795
+ }
796
+ status = decode_message(msg->arena, field->sub, data, data_len, &nested,
797
+ depth + 1, dlim);
798
+ if (status != PBW_OK) {
799
+ return status;
800
+ }
801
+ if (field->label == PBW_LABEL_REPEATED) {
802
+ pbw_value *item = pbw_value_new_message_a(msg->arena, nested);
803
+
804
+ if (item == NULL) {
805
+ pbw_message_free(nested);
806
+ return PBW_ERR_NOMEM;
807
+ }
808
+ return rep_append(msg, field, item, rep);
809
+ }
810
+ {
811
+ pbw_value value;
812
+
813
+ value.kind = PBW_VALUE_MESSAGE;
814
+ value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
815
+ value.as.msg = nested;
816
+ pbw_message_put(msg, index, &value);
817
+ }
818
+ return PBW_OK;
819
+ }
820
+
821
+ /* Handles one field the schema knows about, wire type already matching.
822
+ * `start` points at the tag, so a value that turns out to belong to the unknown
823
+ * fields can be kept byte for byte. */
824
+ static pbw_status
825
+ read_field(reader *r, const pbw_field *field, pbw_message *msg,
826
+ uint32_t depth, uint32_t dlim, const uint8_t *start, int closed,
827
+ rep_cache *rep)
828
+ {
829
+ size_t index = (size_t)(field - msg->desc->fields);
830
+ pbw_status status;
831
+ uint64_t bits;
832
+ pbw_value value;
833
+
834
+ if (field->type == PBW_TYPE_STRING || field->type == PBW_TYPE_BYTES) {
835
+ return read_string_field(r, field, msg, rep);
836
+ }
837
+ if (field->type == PBW_TYPE_MESSAGE) {
838
+ /* Editions message_encoding = DELIMITED: the body runs to the
839
+ * matching END_GROUP, so it decodes like a group. Only fields
840
+ * that opted in take this path; anything else carrying group
841
+ * tags stays an unknown field, which keeps MessageSet passthrough
842
+ * byte-identical (see descriptor_set.c). */
843
+ if (field->msg_enc == 2) {
844
+ return read_group_field(r, field, msg, depth, dlim, rep);
845
+ }
846
+ return read_message_field(r, field, msg, depth, dlim, rep);
847
+ }
848
+ if (field->type == PBW_TYPE_GROUP) {
849
+ return read_group_field(r, field, msg, depth, dlim, rep);
850
+ }
851
+
852
+ status = read_scalar(r, field->type, &bits);
853
+ if (status != PBW_OK) {
854
+ return status;
855
+ }
856
+ if (closed && pbw_enum_is_closed(field) &&
857
+ !pbw_enum_find_name(field->sub, (int32_t)bits, NULL)) {
858
+ /* A closed enum's unknown value leaves the field: the reference parsers
859
+ * keep it as an unknown field instead. */
860
+ if (start == NULL || r->p == NULL || r->p < start) {
861
+ return PBW_ERR_INVALID;
862
+ }
863
+ return (pbw_status)pbw_message_add_unknown(msg, start,
864
+ pbw_remaining(r->p,
865
+ start));
866
+ }
867
+ if (field->label != PBW_LABEL_REPEATED) {
868
+ if (field->oneof_index == 0 && !pbw_msg_present(msg, index)) {
869
+ /* Singular, first occurrence: write the slot directly. */
870
+ store_scalar(msg, index, bits);
871
+ return PBW_OK;
872
+ }
873
+ value.kind = PBW_VALUE_UINT;
874
+ value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
875
+ value.as.u64 = bits;
876
+ pbw_message_put(msg, index, &value);
877
+ return PBW_OK;
878
+ }
879
+ return rep_append(msg, field, pbw_value_new_uint_a(msg->arena, bits),
880
+ rep);
881
+ }
882
+
883
+ /* Merges a validated MessageSet payload into the extension it extends:
884
+ * singular scalars overwrite, repeated items append, singular submessages
885
+ * merge recursively, unknown bytes append. Both share one arena (every
886
+ * decode runs in one), so values move by shallow copy; src slots are
887
+ * detached as they move so freeing src never releases a moved payload. */
888
+ static pbw_status
889
+ messageset_merge(pbw_message *dst, pbw_message *src)
890
+ {
891
+ size_t i;
892
+
893
+ if (dst == NULL || src == NULL || dst->desc != src->desc) {
894
+ return PBW_ERR_INVALID;
895
+ }
896
+ for (i = 0; i < src->nfields; i++) {
897
+ const pbw_field *field;
898
+ pbw_value *sv;
899
+
900
+ if (!pbw_msg_present(src, i)) {
901
+ continue;
902
+ }
903
+ field = &src->desc->fields[i];
904
+ sv = &src->values[i];
905
+ if (field->label == PBW_LABEL_REPEATED &&
906
+ sv->kind == PBW_VALUE_LIST) {
907
+ pbw_value *list = pbw_message_list_for(dst, field);
908
+ size_t k;
909
+
910
+ if (list == NULL) {
911
+ return PBW_ERR_NOMEM;
912
+ }
913
+ for (k = 0; k < sv->as.list.len; k++) {
914
+ if (!pbw_value_list_push(list, sv->as.list.items[k],
915
+ dst->arena)) {
916
+ return PBW_ERR_NOMEM;
917
+ }
918
+ }
919
+ if ((sv->flags & PBW_VF_ARENA) == 0) {
920
+ free(sv->as.list.items);
921
+ }
922
+ sv->as.list.items = NULL;
923
+ sv->as.list.len = 0;
924
+ sv->as.list.cap = 0;
925
+ pbw_msg_set_present(src, i, 0);
926
+ continue;
927
+ }
928
+ if ((field->type == PBW_TYPE_MESSAGE ||
929
+ field->type == PBW_TYPE_GROUP) &&
930
+ sv->kind == PBW_VALUE_MESSAGE && sv->as.msg != NULL &&
931
+ pbw_msg_present(dst, i) &&
932
+ dst->values[i].kind == PBW_VALUE_MESSAGE &&
933
+ dst->values[i].as.msg != NULL &&
934
+ dst->values[i].as.msg->desc == sv->as.msg->desc) {
935
+ pbw_message *schild = sv->as.msg;
936
+ pbw_status status =
937
+ messageset_merge(dst->values[i].as.msg, schild);
938
+
939
+ pbw_msg_set_present(src, i, 0);
940
+ if (status != PBW_OK) {
941
+ return status;
942
+ }
943
+ pbw_message_free(schild);
944
+ continue;
945
+ }
946
+ pbw_message_put(dst, i, sv);
947
+ pbw_msg_set_present(src, i, 0);
948
+ }
949
+ if (src->unknown_len > 0) {
950
+ return (pbw_status)pbw_message_add_unknown(dst, src->unknown,
951
+ src->unknown_len);
952
+ }
953
+ return PBW_OK;
954
+ }
955
+
956
+ /* A MessageSet item: START_GROUP(1) was consumed; the body holds type_id
957
+ * (field 2, varint) and message (field 3, bytes). The walk runs on a copy
958
+ * of the reader: anything anomalous (no id, no payload, unknown id, a
959
+ * non-message extension, undecodable bytes) falls back to unknown
960
+ * preservation from `start`, so verdicts never change — only successfully
961
+ * parsed items populate their extension field, like the reference parsers. */
962
+ static pbw_status
963
+ read_messageset_item(reader *r, pbw_message *msg, uint32_t depth,
964
+ uint32_t dlim, const uint8_t *start)
965
+ {
966
+ const pbw_descriptor *desc = msg->desc;
967
+ reader t = *r;
968
+ uint32_t type_id = 0;
969
+ int has_id = 0;
970
+ const uint8_t *payload = NULL;
971
+ size_t payload_len = 0;
972
+ int has_pay = 0;
973
+ const pbw_field *ext;
974
+ pbw_message *nested = NULL;
975
+ pbw_status status;
976
+ size_t index;
977
+ uint32_t closer = 0;
978
+
979
+ for (;;) {
980
+ uint32_t tag;
981
+ size_t used;
982
+ uint32_t number;
983
+ uint32_t wire_type;
984
+
985
+ if (t.p >= t.end || !pbw_tag_read(t.p, t.end, &tag, &used)) {
986
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
987
+ start);
988
+ }
989
+ t.p += used;
990
+ number = tag >> 3;
991
+ wire_type = tag & 7;
992
+ if (number == 0) {
993
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
994
+ start);
995
+ }
996
+ if (wire_type == PBW_WIRE_END_GROUP) {
997
+ closer = number;
998
+ break;
999
+ }
1000
+ if (number == 2 && wire_type == PBW_WIRE_VARINT) {
1001
+ uint64_t id;
1002
+ size_t vused;
1003
+
1004
+ if (!pbw_varint_read(t.p, t.end, &id, &vused)) {
1005
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth,
1006
+ dlim, start);
1007
+ }
1008
+ t.p += vused;
1009
+ /* Last one wins, like every singular field. */
1010
+ if (id >= 1 && id <= PBW_MAX_FIELD_NUMBER) {
1011
+ type_id = (uint32_t)id;
1012
+ has_id = 1;
1013
+ }
1014
+ continue;
1015
+ }
1016
+ if (number == 3 && wire_type == PBW_WIRE_LEN) {
1017
+ size_t len;
1018
+
1019
+ if (!pbw_len_read(t.p, t.end, &len, &used)) {
1020
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth,
1021
+ dlim, start);
1022
+ }
1023
+ t.p += used;
1024
+ if (len > pbw_remaining(t.end, t.p)) {
1025
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth,
1026
+ dlim, start);
1027
+ }
1028
+ payload = t.p;
1029
+ payload_len = len;
1030
+ has_pay = 1;
1031
+ t.p += len;
1032
+ continue;
1033
+ }
1034
+ if (depth + 1 >= dlim && wire_type == PBW_WIRE_START_GROUP) {
1035
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
1036
+ start);
1037
+ }
1038
+ status = skip_value(&t, number, wire_type, depth + 1, dlim);
1039
+ if (status != PBW_OK) {
1040
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
1041
+ start);
1042
+ }
1043
+ }
1044
+ /* The closer must be END_GROUP(1); t.p already passed it. */
1045
+ if (closer != 1 || !has_id || !has_pay) {
1046
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
1047
+ start);
1048
+ }
1049
+ ext = pbw_find_field(desc, type_id);
1050
+ if (ext == NULL || !ext->is_extension ||
1051
+ ext->type != PBW_TYPE_MESSAGE ||
1052
+ ext->label == PBW_LABEL_REPEATED) {
1053
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
1054
+ start);
1055
+ }
1056
+ if (depth >= dlim) {
1057
+ return PBW_ERR_LIMIT;
1058
+ }
1059
+ index = (size_t)(ext - desc->fields);
1060
+ /* Decode into a temp first: a broken payload must leave an already
1061
+ * present extension untouched (the item is kept as unknown instead). */
1062
+ status = decode_message(msg->arena, ext->sub, payload, payload_len,
1063
+ &nested, depth + 1, dlim);
1064
+ if (status != PBW_OK) {
1065
+ /* Undecodable payload: keep today's verdict (accept as unknown). */
1066
+ pbw_message_free(nested);
1067
+ return skip_field(r, msg, 1, PBW_WIRE_START_GROUP, depth, dlim,
1068
+ start);
1069
+ }
1070
+ if (pbw_msg_present(msg, index) &&
1071
+ msg->values[index].kind == PBW_VALUE_MESSAGE &&
1072
+ msg->values[index].as.msg != NULL &&
1073
+ msg->values[index].as.msg->desc == ext->sub) {
1074
+ /* Singular messages merge, MessageSet items included. */
1075
+ status = messageset_merge(msg->values[index].as.msg, nested);
1076
+ pbw_message_free(nested);
1077
+ if (status != PBW_OK) {
1078
+ return status;
1079
+ }
1080
+ } else {
1081
+ pbw_value value;
1082
+
1083
+ value.kind = PBW_VALUE_MESSAGE;
1084
+ value.flags = msg->arena != NULL ? PBW_VF_ARENA : 0;
1085
+ value.as.msg = nested;
1086
+ pbw_message_put(msg, index, &value);
1087
+ }
1088
+ *r = t;
1089
+ return PBW_OK;
1090
+ }
1091
+
1092
+ static pbw_status
1093
+ decode_fields(pbw_message *msg, const uint8_t *buf, size_t len, uint32_t depth, uint32_t dlim)
1094
+ {
1095
+ reader r;
1096
+ const pbw_descriptor *desc;
1097
+ pbw_field *const *fnum_map;
1098
+ uint32_t fnum_map_size;
1099
+ pbw_field *const *tag_map;
1100
+ int closed;
1101
+ rep_cache rep = {NULL, NULL};
1102
+
1103
+ if (msg == NULL || msg->desc == NULL || (buf == NULL && len > 0)) {
1104
+ return PBW_ERR_INVALID;
1105
+ }
1106
+ if (buf == NULL) {
1107
+ buf = pbw_empty_input;
1108
+ }
1109
+ if (dlim == 0 || dlim > PBW_MAX_DEPTH) {
1110
+ dlim = PBW_MAX_DEPTH;
1111
+ }
1112
+ desc = msg->desc;
1113
+ fnum_map = desc->fnum_map;
1114
+ fnum_map_size = desc->fnum_map_size;
1115
+ tag_map = desc->tag_map;
1116
+ closed = desc->has_closed_enum;
1117
+
1118
+ r.p = buf;
1119
+ /* buf is non-NULL here; len may be 0, so end is at most one past. */
1120
+ r.end = buf + len;
1121
+ while (r.p < r.end) {
1122
+ uint32_t tag;
1123
+ size_t used = 1;
1124
+ uint32_t wire_type;
1125
+ uint32_t number;
1126
+ const pbw_field *field;
1127
+ const uint8_t *start = r.p;
1128
+ pbw_status status;
1129
+
1130
+ /* Tag sizes share one lookup: a one-byte tag indexes the link-time
1131
+ * table, anything longer resolves through the field-number map. */
1132
+
1133
+ /* One-byte tag (field number < 16): the common case, dispatched by
1134
+ * the link-time tag table with a single indexed load. */
1135
+ if (PBW_EXPECT(r.p < r.end && r.p[0] < 0x80, 1)) {
1136
+ tag = r.p[0];
1137
+ r.p++;
1138
+ wire_type = tag & 7;
1139
+ number = tag >> 3;
1140
+ if (number == 0 || wire_type == PBW_WIRE_END_GROUP ||
1141
+ wire_type > PBW_WIRE_32BIT) {
1142
+ return PBW_ERR_INVALID;
1143
+ }
1144
+ /* No one-byte tag can be known when the table is missing: a
1145
+ * field numbered below 16 always encodes to one byte. */
1146
+ field = tag_map != NULL ? tag_map[tag] : NULL;
1147
+ } else if (decode_tag2(r.p, r.end, &tag)) {
1148
+ /* Two-byte tag (field number 16..2047): same lookup as any
1149
+ * other multibyte tag; the action switch below is shared. */
1150
+ r.p += 2;
1151
+ wire_type = tag & 7;
1152
+ number = tag >> 3;
1153
+ if (number == 0 || wire_type == PBW_WIRE_END_GROUP ||
1154
+ wire_type > PBW_WIRE_32BIT) {
1155
+ return PBW_ERR_INVALID;
1156
+ }
1157
+ if (fnum_map != NULL) {
1158
+ field = number < fnum_map_size ? fnum_map[number] : NULL;
1159
+ } else {
1160
+ field = pbw_find_field(desc, number);
1161
+ }
1162
+ } else if (pbw_tag_read(r.p, r.end, &tag, &used)) {
1163
+ r.p += used;
1164
+ wire_type = tag & 7;
1165
+ number = tag >> 3;
1166
+ if (number == 0 || wire_type == PBW_WIRE_END_GROUP ||
1167
+ wire_type > PBW_WIRE_32BIT) {
1168
+ return PBW_ERR_INVALID;
1169
+ }
1170
+ if (fnum_map != NULL) {
1171
+ field = number < fnum_map_size ? fnum_map[number] : NULL;
1172
+ } else {
1173
+ field = pbw_find_field(desc, number);
1174
+ }
1175
+ } else {
1176
+ return PBW_ERR_INVALID;
1177
+ }
1178
+ if (field == NULL) {
1179
+ if (wire_type == PBW_WIRE_START_GROUP && number == 1 &&
1180
+ desc->is_messageset) {
1181
+ /* A MessageSet item decodes into its extension; anything
1182
+ * anomalous stays unknown, as before. */
1183
+ status = read_messageset_item(&r, msg, depth, dlim, start);
1184
+ } else {
1185
+ status =
1186
+ skip_field(&r, msg, number, wire_type, depth, dlim, start);
1187
+ }
1188
+ } else if (wire_type == field->wire_type) {
1189
+ /* One action switch for every tag size. The link-time action
1190
+ * already encodes eligibility (oneof members, groups and closed
1191
+ * enums stay generic), and a one-byte tag implies its wire type
1192
+ * by construction, so no shape checks remain here. */
1193
+ size_t index = (size_t)(field - desc->fields);
1194
+ uint64_t bits;
1195
+ switch (field->dec_action) {
1196
+ case PBW_ACT_I32:
1197
+ if (!read_varint(&r, &bits)) {
1198
+ return PBW_ERR_INVALID;
1199
+ }
1200
+ store_first_or_put(msg, index,
1201
+ (uint64_t)(int64_t)(int32_t)bits);
1202
+ continue;
1203
+ case PBW_ACT_I64:
1204
+ if (!read_varint(&r, &bits)) {
1205
+ return PBW_ERR_INVALID;
1206
+ }
1207
+ store_first_or_put(msg, index, bits);
1208
+ continue;
1209
+ case PBW_ACT_U32:
1210
+ if (!read_varint(&r, &bits)) {
1211
+ return PBW_ERR_INVALID;
1212
+ }
1213
+ store_first_or_put(msg, index, (uint64_t)(uint32_t)bits);
1214
+ continue;
1215
+ case PBW_ACT_S32:
1216
+ if (!read_varint(&r, &bits)) {
1217
+ return PBW_ERR_INVALID;
1218
+ }
1219
+ bits = (uint64_t)(int64_t)(int32_t)zigzag_decode(bits, 32);
1220
+ store_first_or_put(msg, index, bits);
1221
+ continue;
1222
+ case PBW_ACT_S64:
1223
+ if (!read_varint(&r, &bits)) {
1224
+ return PBW_ERR_INVALID;
1225
+ }
1226
+ store_first_or_put(msg, index, zigzag_decode(bits, 64));
1227
+ continue;
1228
+ case PBW_ACT_BOOL:
1229
+ if (!read_varint(&r, &bits)) {
1230
+ return PBW_ERR_INVALID;
1231
+ }
1232
+ store_first_or_put(msg, index, bits != 0 ? 1u : 0u);
1233
+ continue;
1234
+ case PBW_ACT_F32:
1235
+ if (pbw_remaining(r.end, r.p) < 4) {
1236
+ return PBW_ERR_INVALID;
1237
+ }
1238
+ bits = dec_le32(r.p);
1239
+ if (field->type == PBW_TYPE_SFIXED32) {
1240
+ bits = (uint64_t)(int64_t)(int32_t)bits;
1241
+ }
1242
+ store_first_or_put(msg, index, bits);
1243
+ r.p += 4;
1244
+ continue;
1245
+ case PBW_ACT_F64:
1246
+ if (pbw_remaining(r.end, r.p) < 8) {
1247
+ return PBW_ERR_INVALID;
1248
+ }
1249
+ bits = dec_le64(r.p);
1250
+ if (field->type == PBW_TYPE_SFIXED64) {
1251
+ bits = (uint64_t)(int64_t)bits;
1252
+ }
1253
+ store_first_or_put(msg, index, bits);
1254
+ r.p += 8;
1255
+ continue;
1256
+ case PBW_ACT_STR: {
1257
+ const uint8_t *data;
1258
+ size_t data_len;
1259
+ status = read_length(&r, &data, &data_len);
1260
+ if (status != PBW_OK) {
1261
+ return status;
1262
+ }
1263
+ status = check_utf8(field, data, data_len);
1264
+ if (status != PBW_OK) {
1265
+ return status;
1266
+ }
1267
+ if (!pbw_msg_present(msg, index)) {
1268
+ if (!store_bytes_first(msg, index, data, data_len)) {
1269
+ return PBW_ERR_NOMEM;
1270
+ }
1271
+ } else {
1272
+ pbw_value value;
1273
+ if (!pbw_value_init_bytes(&value, msg->arena, data,
1274
+ data_len)) {
1275
+ return PBW_ERR_NOMEM;
1276
+ }
1277
+ pbw_message_put(msg, index, &value);
1278
+ }
1279
+ continue;
1280
+ }
1281
+ case PBW_ACT_MSG: {
1282
+ const uint8_t *data;
1283
+ size_t data_len;
1284
+ pbw_message *nested = NULL;
1285
+ if (depth >= dlim) {
1286
+ return PBW_ERR_LIMIT;
1287
+ }
1288
+ status = read_length(&r, &data, &data_len);
1289
+ if (status != PBW_OK) {
1290
+ return status;
1291
+ }
1292
+ if (pbw_msg_present(msg, index) &&
1293
+ msg->values[index].kind == PBW_VALUE_MESSAGE &&
1294
+ msg->values[index].as.msg != NULL &&
1295
+ msg->values[index].as.msg->desc == field->sub) {
1296
+ status = decode_fields(msg->values[index].as.msg, data,
1297
+ data_len, depth + 1, dlim);
1298
+ if (status != PBW_OK) {
1299
+ return status;
1300
+ }
1301
+ continue;
1302
+ }
1303
+ status = decode_message(msg->arena, field->sub, data,
1304
+ data_len, &nested, depth + 1, dlim);
1305
+ if (status != PBW_OK) {
1306
+ return status;
1307
+ }
1308
+ if (pbw_msg_present(msg, index)) {
1309
+ pbw_value_destroy(&msg->values[index]);
1310
+ }
1311
+ msg->values[index].kind = PBW_VALUE_MESSAGE;
1312
+ msg->values[index].flags = PBW_VF_EMBEDDED |
1313
+ (msg->arena != NULL ? PBW_VF_ARENA : 0u);
1314
+ msg->values[index].as.msg = nested;
1315
+ pbw_msg_set_present(msg, index, 1);
1316
+ continue;
1317
+ }
1318
+ case PBW_ACT_REP_VARINT:
1319
+ status = read_scalar(&r, field->type, &bits);
1320
+ if (status != PBW_OK) {
1321
+ return status;
1322
+ }
1323
+ status = rep_append(msg, field,
1324
+ pbw_value_new_uint_a(msg->arena, bits),
1325
+ &rep);
1326
+ if (status != PBW_OK) {
1327
+ return status;
1328
+ }
1329
+ continue;
1330
+ case PBW_ACT_REP_F32:
1331
+ if (pbw_remaining(r.end, r.p) < 4) {
1332
+ return PBW_ERR_INVALID;
1333
+ }
1334
+ bits = dec_le32(r.p);
1335
+ if (field->type == PBW_TYPE_SFIXED32) {
1336
+ /* Sign extended like the singular path: the API hands
1337
+ * back every signed value in the same representation,
1338
+ * list items included. */
1339
+ bits = (uint64_t)(int64_t)(int32_t)bits;
1340
+ }
1341
+ r.p += 4;
1342
+ status = rep_append(msg, field,
1343
+ pbw_value_new_uint_a(msg->arena, bits),
1344
+ &rep);
1345
+ if (status != PBW_OK) {
1346
+ return status;
1347
+ }
1348
+ continue;
1349
+ case PBW_ACT_REP_F64:
1350
+ if (pbw_remaining(r.end, r.p) < 8) {
1351
+ return PBW_ERR_INVALID;
1352
+ }
1353
+ bits = dec_le64(r.p);
1354
+ if (field->type == PBW_TYPE_SFIXED64) {
1355
+ bits = (uint64_t)(int64_t)bits;
1356
+ }
1357
+ r.p += 8;
1358
+ status = rep_append(msg, field,
1359
+ pbw_value_new_uint_a(msg->arena, bits),
1360
+ &rep);
1361
+ if (status != PBW_OK) {
1362
+ return status;
1363
+ }
1364
+ continue;
1365
+ case PBW_ACT_REP_STR: {
1366
+ const uint8_t *data;
1367
+ size_t data_len;
1368
+ status = read_length(&r, &data, &data_len);
1369
+ if (status != PBW_OK) {
1370
+ return status;
1371
+ }
1372
+ status = check_utf8(field, data, data_len);
1373
+ if (status != PBW_OK) {
1374
+ return status;
1375
+ }
1376
+ status = rep_append(
1377
+ msg, field,
1378
+ pbw_value_new_bytes_a(msg->arena, data, data_len),
1379
+ &rep);
1380
+ if (status != PBW_OK) {
1381
+ return status;
1382
+ }
1383
+ continue;
1384
+ }
1385
+ case PBW_ACT_REP_MSG: {
1386
+ const uint8_t *data;
1387
+ size_t data_len;
1388
+ pbw_message *nested = NULL;
1389
+ pbw_value *item;
1390
+ if (depth >= dlim) {
1391
+ return PBW_ERR_LIMIT;
1392
+ }
1393
+ status = read_length(&r, &data, &data_len);
1394
+ if (status != PBW_OK) {
1395
+ return status;
1396
+ }
1397
+ status = decode_message(msg->arena, field->sub, data,
1398
+ data_len, &nested, depth + 1, dlim);
1399
+ if (status != PBW_OK) {
1400
+ return status;
1401
+ }
1402
+ item = pbw_value_new_message_a(msg->arena, nested);
1403
+ if (item == NULL) {
1404
+ pbw_message_free(nested);
1405
+ return PBW_ERR_NOMEM;
1406
+ }
1407
+ status = rep_append(msg, field, item, &rep);
1408
+ if (status != PBW_OK) {
1409
+ return status;
1410
+ }
1411
+ continue;
1412
+ }
1413
+ default:
1414
+ break;
1415
+ }
1416
+ status = read_field(&r, field, msg, depth, dlim, start, closed,
1417
+ &rep);
1418
+ } else if (wire_type == PBW_WIRE_LEN &&
1419
+ field->label == PBW_LABEL_REPEATED &&
1420
+ pbw_is_packable(field->type)) {
1421
+ /* A repeated numeric field may arrive packed regardless of the
1422
+ * descriptor flag. */
1423
+ status = read_packed(&r, field, msg, closed, &rep);
1424
+ } else {
1425
+ /* Wire type mismatch: treat as an unknown field, like the reference
1426
+ * decoders. */
1427
+ status = skip_field(&r, msg, number, wire_type, depth, dlim, start);
1428
+ }
1429
+ if (status != PBW_OK) {
1430
+ return status;
1431
+ }
1432
+ }
1433
+ return PBW_OK;
1434
+ }
1435
+
1436
+ static pbw_status
1437
+ decode_message(pbw_arena *arena, const pbw_descriptor *desc, const uint8_t *buf,
1438
+ size_t len, pbw_message **out, uint32_t depth, uint32_t dlim)
1439
+ {
1440
+ pbw_message *msg;
1441
+ pbw_status status;
1442
+
1443
+ if (arena == NULL || desc == NULL || out == NULL ||
1444
+ (buf == NULL && len > 0)) {
1445
+ return PBW_ERR_INVALID;
1446
+ }
1447
+ if (buf == NULL) {
1448
+ buf = pbw_empty_input;
1449
+ }
1450
+ *out = NULL;
1451
+ msg = pbw_message_new_a(arena, desc);
1452
+ if (msg == NULL) {
1453
+ return PBW_ERR_NOMEM;
1454
+ }
1455
+ status = decode_fields(msg, buf, len, depth, dlim);
1456
+ if (status != PBW_OK) {
1457
+ pbw_message_free(msg);
1458
+ return status;
1459
+ }
1460
+ if (depth == 0) {
1461
+ /* Root only: nested messages recurse with depth + 1. The encode
1462
+ * pre-size reads exactly this, so nested trees need no copy. */
1463
+ msg->wire_len = len;
1464
+ } else {
1465
+ /* Nested results are installed in a parent (or merged and dropped):
1466
+ * mark them owned here so the store boundary's duplicate check holds
1467
+ * for decoded children too, exactly like pbw_message_put(). */
1468
+ msg->stored = 1;
1469
+ }
1470
+ *out = msg;
1471
+ return PBW_OK;
1472
+ }
1473
+
1474
+ pbw_status
1475
+ pbw_decode_arena_lim(pbw_arena *arena, const pbw_descriptor *desc,
1476
+ const void *buf, size_t len, uint32_t dlim,
1477
+ pbw_message **out)
1478
+ {
1479
+ pbw_status status;
1480
+ const uint8_t *bytes;
1481
+
1482
+ if (out != NULL) {
1483
+ *out = NULL;
1484
+ }
1485
+ if (arena == NULL || desc == NULL || out == NULL ||
1486
+ (buf == NULL && len > 0)) {
1487
+ return PBW_ERR_INVALID;
1488
+ }
1489
+ if (dlim == 0 || dlim > PBW_MAX_DEPTH) {
1490
+ dlim = PBW_MAX_DEPTH;
1491
+ }
1492
+ if (desc->is_enum) {
1493
+ return PBW_ERR_TYPE;
1494
+ }
1495
+ if (desc->schema != NULL && !desc->schema->linked) {
1496
+ return PBW_ERR_INVALID;
1497
+ }
1498
+ bytes = pbw_normalize_input(buf, len);
1499
+ status = decode_message(arena, desc, bytes, len, out, 0, dlim);
1500
+ /* The reference parsers check for missing `required` fields once the whole
1501
+ * message is in, not per nested occurrence, so this is the same point. The
1502
+ * per-descriptor flag keeps the walk off subtrees without `required`. */
1503
+ if (status == PBW_OK && desc->has_required &&
1504
+ !pbw_message_is_initialized(*out)) {
1505
+ pbw_message_free(*out);
1506
+ *out = NULL;
1507
+ return PBW_ERR_REQUIRED;
1508
+ }
1509
+ return status;
1510
+ }
1511
+
1512
+ /* Plain decoding owns one arena per message tree: the whole decode is a single
1513
+ * malloc instead of one per field, and pbw_message_free() releases it at once.
1514
+ * Wire length is a fair proxy for the tree size, so the first chunk is sized
1515
+ * from it and a small message rarely needs a second one. */
1516
+ pbw_status
1517
+ pbw_decode_limits(const pbw_descriptor *desc, const void *buf, size_t len,
1518
+ const pbw_limits *limits, pbw_message **out)
1519
+ {
1520
+ pbw_arena *arena;
1521
+ pbw_status status;
1522
+ const uint8_t *bytes;
1523
+
1524
+ if (out != NULL) {
1525
+ *out = NULL;
1526
+ }
1527
+ if (desc == NULL || out == NULL || (buf == NULL && len > 0)) {
1528
+ return PBW_ERR_INVALID;
1529
+ }
1530
+ /* Every failure below leaves NULL: callers can rely on *out alone. */
1531
+ if (limits != NULL && limits->max_bytes > 0 && len > limits->max_bytes) {
1532
+ return PBW_ERR_LIMIT;
1533
+ }
1534
+ {
1535
+ /* First-chunk hint from the wire length. Saturate instead of wrapping
1536
+ * for a length so large that the size arithmetic would overflow. */
1537
+ size_t hint = len;
1538
+
1539
+ if (len <= 4096) {
1540
+ hint = len * 6 + 1024;
1541
+ } else if (len <= SIZE_MAX - len / 2 - 512) {
1542
+ hint = len + len / 2 + 512;
1543
+ }
1544
+ arena = pbw_arena_new_packed(hint);
1545
+ }
1546
+ if (arena == NULL) {
1547
+ return PBW_ERR_NOMEM;
1548
+ }
1549
+ bytes = pbw_normalize_input(buf, len);
1550
+ status = pbw_decode_arena_lim(arena, desc, bytes, len,
1551
+ pbw_effective_depth(limits), out);
1552
+ if (status != PBW_OK) {
1553
+ pbw_arena_free(arena);
1554
+ return status;
1555
+ }
1556
+ (*out)->own = arena;
1557
+ return PBW_OK;
1558
+ }
1559
+
1560
+ pbw_status
1561
+ pbw_decode(const pbw_descriptor *desc, const void *buf, size_t len,
1562
+ pbw_message **out)
1563
+ {
1564
+ return pbw_decode_limits(desc, buf, len, NULL, out);
1565
+ }
1566
+
1567
+ /* Event scan: one validating pass over a single message level, no tree, no
1568
+ * allocation. Reuses the tag dispatch and field lookup of decode_fields, but
1569
+ * the per-field work is only a wire-type switch plus one callback: values
1570
+ * stay raw (no zigzag/sign fold, no enum/required checks, no packed split),
1571
+ * so a counting/filtering pass never pays for materialization. */
1572
+ #define VISIT_FAIL(off, st) \
1573
+ do { \
1574
+ if (fail_off != NULL) { \
1575
+ *fail_off = (off); \
1576
+ } \
1577
+ return (st); \
1578
+ } while (0)
1579
+
1580
+ pbw_status
1581
+ pbw_visit_limits(const pbw_descriptor *desc, const void *buf, size_t len,
1582
+ const pbw_limits *limits, pbw_visit_fn cb, void *ud,
1583
+ size_t *fail_off)
1584
+ {
1585
+ reader r;
1586
+ const uint8_t *base;
1587
+ pbw_field *const *fnum_map;
1588
+ uint32_t fnum_map_size;
1589
+ pbw_field *const *tag_map;
1590
+ uint32_t dlim;
1591
+
1592
+ if (desc == NULL || (buf == NULL && len > 0) || cb == NULL) {
1593
+ VISIT_FAIL(0, PBW_ERR_INVALID);
1594
+ }
1595
+ if (limits != NULL && limits->max_bytes > 0 && len > limits->max_bytes) {
1596
+ VISIT_FAIL(0, PBW_ERR_LIMIT);
1597
+ }
1598
+ if (desc->is_enum) {
1599
+ VISIT_FAIL(0, PBW_ERR_TYPE);
1600
+ }
1601
+ if (desc->schema != NULL && !desc->schema->linked) {
1602
+ VISIT_FAIL(0, PBW_ERR_INVALID);
1603
+ }
1604
+ base = pbw_normalize_input(buf, len);
1605
+ dlim = pbw_effective_depth(limits);
1606
+ fnum_map = desc->fnum_map;
1607
+ fnum_map_size = desc->fnum_map_size;
1608
+ tag_map = desc->tag_map;
1609
+ r.p = base;
1610
+ r.end = base + len;
1611
+ while (r.p < r.end) {
1612
+ uint32_t tag;
1613
+ size_t used = 1;
1614
+ uint32_t wire_type;
1615
+ uint32_t number;
1616
+ const pbw_field *field;
1617
+ const uint8_t *start = r.p;
1618
+ size_t at = (size_t)(start - base);
1619
+ pbw_visit_value value;
1620
+ pbw_status status;
1621
+
1622
+ if (PBW_EXPECT(r.p < r.end && r.p[0] < 0x80, 1)) {
1623
+ tag = r.p[0];
1624
+ r.p++;
1625
+ wire_type = tag & 7;
1626
+ number = tag >> 3;
1627
+ if (number == 0 || wire_type == PBW_WIRE_END_GROUP ||
1628
+ wire_type > PBW_WIRE_32BIT) {
1629
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1630
+ }
1631
+ field = tag_map != NULL ? tag_map[tag] : NULL;
1632
+ if (field == NULL) {
1633
+ /* Unknown or mismatched one-byte tag: fall back to the
1634
+ * number so a known field carrying another wire type
1635
+ * still resolves. Cold path only: the table hit above
1636
+ * already handled the common case. */
1637
+ if (fnum_map != NULL) {
1638
+ field =
1639
+ number < fnum_map_size ? fnum_map[number] : NULL;
1640
+ } else {
1641
+ field = pbw_find_field(desc, number);
1642
+ }
1643
+ }
1644
+ } else if (decode_tag2(r.p, r.end, &tag)) {
1645
+ r.p += 2;
1646
+ wire_type = tag & 7;
1647
+ number = tag >> 3;
1648
+ if (number == 0 || wire_type == PBW_WIRE_END_GROUP ||
1649
+ wire_type > PBW_WIRE_32BIT) {
1650
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1651
+ }
1652
+ if (fnum_map != NULL) {
1653
+ field = number < fnum_map_size ? fnum_map[number] : NULL;
1654
+ } else {
1655
+ field = pbw_find_field(desc, number);
1656
+ }
1657
+ } else if (pbw_tag_read(r.p, r.end, &tag, &used)) {
1658
+ r.p += used;
1659
+ wire_type = tag & 7;
1660
+ number = tag >> 3;
1661
+ if (number == 0 || wire_type == PBW_WIRE_END_GROUP ||
1662
+ wire_type > PBW_WIRE_32BIT) {
1663
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1664
+ }
1665
+ if (fnum_map != NULL) {
1666
+ field = number < fnum_map_size ? fnum_map[number] : NULL;
1667
+ } else {
1668
+ field = pbw_find_field(desc, number);
1669
+ }
1670
+ } else {
1671
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1672
+ }
1673
+ value.u64 = 0;
1674
+ value.data = NULL;
1675
+ value.len = 0;
1676
+ switch (wire_type) {
1677
+ case PBW_WIRE_VARINT:
1678
+ if (!read_varint(&r, &value.u64)) {
1679
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1680
+ }
1681
+ break;
1682
+ case PBW_WIRE_64BIT:
1683
+ if (pbw_remaining(r.end, r.p) < 8) {
1684
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1685
+ }
1686
+ value.u64 = dec_le64(r.p);
1687
+ r.p += 8;
1688
+ break;
1689
+ case PBW_WIRE_32BIT:
1690
+ if (pbw_remaining(r.end, r.p) < 4) {
1691
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1692
+ }
1693
+ value.u64 = dec_le32(r.p);
1694
+ r.p += 4;
1695
+ break;
1696
+ case PBW_WIRE_LEN: {
1697
+ const uint8_t *data;
1698
+ size_t data_len;
1699
+
1700
+ status = read_length(&r, &data, &data_len);
1701
+ if (status != PBW_OK) {
1702
+ VISIT_FAIL(at, PBW_ERR_INVALID);
1703
+ }
1704
+ value.data = data;
1705
+ value.len = data_len;
1706
+ break;
1707
+ }
1708
+ default: /* START_GROUP: deliver the whole field as raw bytes. */
1709
+ status = skip_value(&r, number, wire_type, 0, dlim);
1710
+ if (status != PBW_OK) {
1711
+ VISIT_FAIL(at, status);
1712
+ }
1713
+ value.data = start;
1714
+ value.len = pbw_remaining(r.p, start);
1715
+ break;
1716
+ }
1717
+ status = cb(ud, field, number, wire_type, &value);
1718
+ if (status != PBW_OK) {
1719
+ VISIT_FAIL(at, status);
1720
+ }
1721
+ }
1722
+ if (fail_off != NULL) {
1723
+ *fail_off = len;
1724
+ }
1725
+ return PBW_OK;
1726
+ }
1727
+
1728
+ #undef VISIT_FAIL
1729
+
1730
+ pbw_status
1731
+ pbw_visit(const pbw_descriptor *desc, const void *buf, size_t len,
1732
+ pbw_visit_fn cb, void *ud, size_t *fail_off)
1733
+ {
1734
+ return pbw_visit_limits(desc, buf, len, NULL, cb, ud, fail_off);
1735
+ }