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,1606 @@
1
+ /* Wire encoder: dynamic message tree -> Protobuf binary.
2
+ *
3
+ * Single pass. The output buffer grows on demand, tags are copied from the
4
+ * precomputed bytes in the descriptor, string and bytes lengths are written
5
+ * before their payload, and nested messages use a one-byte length placeholder
6
+ * that is patched afterwards (the payload is shifted right when the length
7
+ * needs more than one byte). No sizing pass, no temporary buffer per nested
8
+ * message, no allocation per field.
9
+ */
10
+
11
+ #include "internal.h"
12
+
13
+ typedef struct {
14
+ pbw_buf *buf;
15
+ /* Cached buf->data and buf->cap: every field write needs the base pointer
16
+ * and every reserve the capacity, and the two loads through `buf` are
17
+ * dependent. They are refreshed in w_grow, the only place they change. */
18
+ uint8_t *base;
19
+ size_t limit;
20
+ size_t pos;
21
+ pbw_status status;
22
+ } writer;
23
+
24
+ int
25
+ pbw_is_packable(pbw_type type)
26
+ {
27
+ switch (type) {
28
+ case PBW_TYPE_STRING:
29
+ case PBW_TYPE_BYTES:
30
+ case PBW_TYPE_MESSAGE:
31
+ case PBW_TYPE_GROUP:
32
+ return 0;
33
+ default:
34
+ return 1;
35
+ }
36
+ }
37
+
38
+ uint32_t
39
+ pbw_expected_wire_type(pbw_type type)
40
+ {
41
+ switch (type) {
42
+ case PBW_TYPE_DOUBLE:
43
+ case PBW_TYPE_FIXED64:
44
+ case PBW_TYPE_SFIXED64:
45
+ return PBW_WIRE_64BIT;
46
+ case PBW_TYPE_FLOAT:
47
+ case PBW_TYPE_FIXED32:
48
+ case PBW_TYPE_SFIXED32:
49
+ return PBW_WIRE_32BIT;
50
+ case PBW_TYPE_STRING:
51
+ case PBW_TYPE_BYTES:
52
+ case PBW_TYPE_MESSAGE:
53
+ return PBW_WIRE_LEN;
54
+ case PBW_TYPE_GROUP:
55
+ return PBW_WIRE_START_GROUP;
56
+ default:
57
+ return PBW_WIRE_VARINT;
58
+ }
59
+ }
60
+
61
+ PBW_INLINE uint64_t
62
+ zigzag_encode(uint64_t value, unsigned width)
63
+ {
64
+ uint64_t mask = width == 32 ? 0xffffffffull : ~0ull;
65
+ uint64_t sign = width == 32 ? 31 : 63;
66
+ uint64_t u = value & mask;
67
+
68
+ return ((u << 1) ^ (0ull - (u >> sign))) & mask;
69
+ }
70
+
71
+ /* Value as it goes on the wire, before tag and length. Inlined: it runs once
72
+ * per varint field on the encode hot path. */
73
+ PBW_INLINE uint64_t
74
+ wire_value(pbw_type type, uint64_t u)
75
+ {
76
+ switch (type) {
77
+ case PBW_TYPE_INT32:
78
+ case PBW_TYPE_ENUM:
79
+ return (uint64_t)(int64_t)(int32_t)u;
80
+ case PBW_TYPE_UINT32:
81
+ return (uint32_t)u;
82
+ case PBW_TYPE_SINT32:
83
+ return zigzag_encode(u, 32);
84
+ case PBW_TYPE_SINT64:
85
+ return zigzag_encode(u, 64);
86
+ case PBW_TYPE_BOOL:
87
+ return u != 0 ? 1u : 0u;
88
+ default:
89
+ return u;
90
+ }
91
+ }
92
+
93
+ /* ---------------------------------------------------------------- writing */
94
+
95
+ /* Rare path of w_reserve(): the buffer has to grow. */
96
+ static int
97
+ w_grow(writer *w, size_t extra)
98
+ {
99
+ w->buf->len = w->pos;
100
+ if (!pbw_buf_reserve(w->buf, extra)) {
101
+ w->status = PBW_ERR_NOMEM;
102
+ return 0;
103
+ }
104
+ /* The reserve may have moved the block: refresh the cached views. */
105
+ w->base = w->buf->data;
106
+ w->limit = w->buf->cap;
107
+ return 1;
108
+ }
109
+
110
+ PBW_INLINE void
111
+ w_init(writer *w, pbw_buf *buf)
112
+ {
113
+ w->buf = buf;
114
+ w->base = buf->data;
115
+ w->limit = buf->cap;
116
+ w->pos = 0;
117
+ w->status = PBW_OK;
118
+ }
119
+
120
+ PBW_INLINE int
121
+ w_reserve(writer *w, size_t extra)
122
+ {
123
+ if (PBW_EXPECT(w->limit - w->pos >= extra, 1)) {
124
+ return 1;
125
+ }
126
+ return w_grow(w, extra);
127
+ }
128
+
129
+ /* Copy of a wire payload: most field values are short (ids, names, enum
130
+ * labels, short tags), and for those an out-of-line memcpy call costs more
131
+ * than the copy itself -- it showed up as 9% of the xlarge encode profile.
132
+ * Up to PBW_COPY_INLINE bytes the copy unrolls into a few stores; anything
133
+ * larger stays a memcpy call. Reads and writes stay inside [src, src+len),
134
+ * so a payload shorter than the unroll width is never over-read. */
135
+ #define PBW_COPY_INLINE 16
136
+
137
+ PBW_INLINE void
138
+ w_copy(uint8_t *dst, const uint8_t *src, size_t len)
139
+ {
140
+ size_t i = 0;
141
+
142
+ if (PBW_EXPECT(len > PBW_COPY_INLINE, 0)) {
143
+ memcpy(dst, src, len);
144
+ return;
145
+ }
146
+ /* Byte loop, not chunked memcpy: callers pass small stack tags (e.g. a
147
+ * 5-byte group END tag), and memcpy(dst + i, src + i, 8) trips
148
+ * -Werror=array-bounds under _FORTIFY_SOURCE even when the length guard
149
+ * rules it out at runtime. The compiler unrolls this on its own. */
150
+ for (; i < len; i++) {
151
+ dst[i] = src[i];
152
+ }
153
+ }
154
+
155
+ PBW_INLINE void
156
+ w_put(writer *w, const void *data, size_t len)
157
+ {
158
+ if (len == 0) {
159
+ return;
160
+ }
161
+ if (!w_reserve(w, len)) {
162
+ return;
163
+ }
164
+ w_copy(w->base + w->pos, (const uint8_t *)data, len);
165
+ w->pos += len;
166
+ }
167
+
168
+ PBW_INLINE void
169
+ w_varint(writer *w, uint64_t value)
170
+ {
171
+ uint8_t *out;
172
+
173
+ /* Reserve the maximum width so the value is written in place: encoding only
174
+ * appends, so the slack past w->pos is never read. */
175
+ if (!w_reserve(w, 10)) {
176
+ return;
177
+ }
178
+ out = w->base + w->pos;
179
+ if (value < 0x80) {
180
+ out[0] = (uint8_t)value;
181
+ w->pos += 1;
182
+ return;
183
+ }
184
+ if (value < 0x4000) {
185
+ out[0] = (uint8_t)((value & 0x7f) | 0x80);
186
+ out[1] = (uint8_t)(value >> 7);
187
+ w->pos += 2;
188
+ return;
189
+ }
190
+ {
191
+ size_t len = 0;
192
+
193
+ while (value >= 0x80) {
194
+ out[len++] = (uint8_t)((value & 0x7f) | 0x80);
195
+ value >>= 7;
196
+ }
197
+ out[len++] = (uint8_t)value;
198
+ w->pos += len;
199
+ }
200
+ }
201
+
202
+ /* Copies a precomputed tag. Tag lengths are one or two bytes for almost every
203
+ * schema, so avoid a memcpy call for those. */
204
+ /* Writes a precomputed tag word (little-endian by definition) as `len`
205
+ * bytes. A 32-bit store is the fast path, but only where the host byte
206
+ * order matches the wire: on big-endian the word would come out reversed,
207
+ * so those builds (and any unknown order) spell it byte by byte. */
208
+ PBW_INLINE void
209
+ w_tag_word_le(writer *w, uint32_t word, size_t len)
210
+ {
211
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
212
+ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
213
+ uint8_t *out = w->base + w->pos;
214
+ size_t i;
215
+
216
+ for (i = 0; i < len; i++) {
217
+ out[i] = (uint8_t)(word >> (8 * i));
218
+ }
219
+ #elif defined(_MSC_VER) || \
220
+ (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
221
+ __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
222
+ /* memcpy of a fixed size compiles to one store and does not assume the
223
+ * output offset is aligned. */
224
+ memcpy(w->base + w->pos, &word, sizeof(word));
225
+ #else
226
+ /* Unknown byte order: the portable spelling. */
227
+ uint8_t *out = w->base + w->pos;
228
+ size_t i;
229
+
230
+ for (i = 0; i < len; i++) {
231
+ out[i] = (uint8_t)(word >> (8 * i));
232
+ }
233
+ #endif
234
+ w->pos += len;
235
+ }
236
+
237
+ /* Writes a precomputed tag. Tags are at most five bytes and the buffer is
238
+ * reserved for five, so the four-byte word can be stored unconditionally and
239
+ * the slack is overwritten by the next write. */
240
+ PBW_INLINE void
241
+ w_tag(writer *w, uint32_t word, size_t len)
242
+ {
243
+ if (!w_reserve(w, 5)) {
244
+ return;
245
+ }
246
+ w_tag_word_le(w, word, len);
247
+ }
248
+
249
+ /* Same writes without a capacity check, for callers that reserved once for
250
+ * the whole field (tag plus value). Encoding only appends, so the reserved
251
+ * slack past w->pos is never read. */
252
+ PBW_INLINE void
253
+ w_tag_noreserve(writer *w, uint32_t word, size_t len)
254
+ {
255
+ w_tag_word_le(w, word, len);
256
+ }
257
+
258
+ PBW_INLINE void
259
+ w_varint_noreserve(writer *w, uint64_t value)
260
+ {
261
+ uint8_t *out = w->base + w->pos;
262
+ size_t len = 0;
263
+
264
+ if (value < 0x80) {
265
+ out[0] = (uint8_t)value;
266
+ w->pos += 1;
267
+ return;
268
+ }
269
+ if (value < 0x4000) {
270
+ out[0] = (uint8_t)((value & 0x7f) | 0x80);
271
+ out[1] = (uint8_t)(value >> 7);
272
+ w->pos += 2;
273
+ return;
274
+ }
275
+ /* Unrolled 5-byte and 10-byte stores: 5 bytes cover 35 bits, 10 bytes
276
+ * are exactly values with bit 63 set; 6-9 bytes keep the loop. */
277
+ if (value < ((uint64_t)1 << 35)) {
278
+ uint64_t v = value;
279
+ size_t nb = v < ((uint64_t)1 << 21) ? 3 : (v < ((uint64_t)1 << 28) ? 4 : 5);
280
+ out[0] = (uint8_t)((v & 0x7f) | 0x80);
281
+ out[1] = (uint8_t)(((v >> 7) & 0x7f) | 0x80);
282
+ out[2] = (uint8_t)(((v >> 14) & 0x7f) | (nb > 3 ? 0x80 : 0));
283
+ if (nb > 3) {
284
+ out[3] = (uint8_t)(((v >> 21) & 0x7f) | (nb > 4 ? 0x80 : 0));
285
+ if (nb > 4) {
286
+ out[4] = (uint8_t)(v >> 28);
287
+ }
288
+ }
289
+ w->pos += nb;
290
+ return;
291
+ }
292
+ if (value >> 63) {
293
+ out[0] = (uint8_t)((value & 0x7f) | 0x80);
294
+ out[1] = (uint8_t)(((value >> 7) & 0x7f) | 0x80);
295
+ out[2] = (uint8_t)(((value >> 14) & 0x7f) | 0x80);
296
+ out[3] = (uint8_t)(((value >> 21) & 0x7f) | 0x80);
297
+ out[4] = (uint8_t)(((value >> 28) & 0x7f) | 0x80);
298
+ out[5] = (uint8_t)(((value >> 35) & 0x7f) | 0x80);
299
+ out[6] = (uint8_t)(((value >> 42) & 0x7f) | 0x80);
300
+ out[7] = (uint8_t)(((value >> 49) & 0x7f) | 0x80);
301
+ out[8] = (uint8_t)(((value >> 56) & 0x7f) | 0x80);
302
+ out[9] = (uint8_t)(value >> 63);
303
+ w->pos += 10;
304
+ return;
305
+ }
306
+ while (value >= 0x80) {
307
+ out[len++] = (uint8_t)((value & 0x7f) | 0x80);
308
+ value >>= 7;
309
+ }
310
+ out[len++] = (uint8_t)value;
311
+ w->pos += len;
312
+ }
313
+
314
+ PBW_INLINE void
315
+ w_fixed_noreserve(writer *w, uint64_t value, size_t size)
316
+ {
317
+ uint8_t *out = w->base + w->pos;
318
+
319
+ if (size == 4) {
320
+ out[0] = (uint8_t)value;
321
+ out[1] = (uint8_t)(value >> 8);
322
+ out[2] = (uint8_t)(value >> 16);
323
+ out[3] = (uint8_t)(value >> 24);
324
+ } else {
325
+ out[0] = (uint8_t)value;
326
+ out[1] = (uint8_t)(value >> 8);
327
+ out[2] = (uint8_t)(value >> 16);
328
+ out[3] = (uint8_t)(value >> 24);
329
+ out[4] = (uint8_t)(value >> 32);
330
+ out[5] = (uint8_t)(value >> 40);
331
+ out[6] = (uint8_t)(value >> 48);
332
+ out[7] = (uint8_t)(value >> 56);
333
+ }
334
+ w->pos += size;
335
+ }
336
+
337
+ /* Field-number >= 2^21 (five tag bytes) is rare enough to take the slow path. */
338
+ static void
339
+ w_tag_bytes(writer *w, const uint8_t *tag, size_t len)
340
+ {
341
+ if (w_reserve(w, len)) {
342
+ w_copy(w->base + w->pos, tag, len);
343
+ w->pos += len;
344
+ }
345
+ }
346
+
347
+ PBW_INLINE void
348
+ w_field_tag(writer *w, const pbw_field *field, int packed)
349
+ {
350
+ if ((packed ? field->len_tag_len : field->tag_len) <= 4) {
351
+ w_tag(w, packed ? field->len_tag_word : field->tag_word,
352
+ packed ? field->len_tag_len : field->tag_len);
353
+ return;
354
+ }
355
+ w_tag_bytes(w, packed ? field->len_tag : field->tag,
356
+ packed ? field->len_tag_len : field->tag_len);
357
+ }
358
+
359
+ /* A length-delimited payload needs its length before its content, but the
360
+ * length is only known afterwards: write a one-byte placeholder, encode, then
361
+ * patch it, shifting the payload right when the length varint is longer. */
362
+ static void
363
+ w_len_begin(writer *w, size_t *mark)
364
+ {
365
+ *mark = w->pos;
366
+ if (!w_reserve(w, 1)) {
367
+ return;
368
+ }
369
+ w->base[w->pos] = 0;
370
+ w->pos += 1;
371
+ }
372
+
373
+ static void
374
+ w_len_end(writer *w, size_t mark)
375
+ {
376
+ size_t payload = w->pos - mark - 1;
377
+ size_t len_bytes = pbw_varint_size(payload);
378
+ size_t shift = len_bytes - 1;
379
+ uint8_t *base;
380
+
381
+ if (shift > 0) {
382
+ if (!w_reserve(w, shift)) {
383
+ return;
384
+ }
385
+ base = w->base;
386
+ memmove(base + mark + 1 + shift, base + mark + 1, payload);
387
+ w->pos += shift;
388
+ }
389
+ {
390
+ uint8_t *out = w->base + mark;
391
+
392
+ while (payload >= 0x80) {
393
+ *out++ = (uint8_t)((payload & 0x7f) | 0x80);
394
+ payload >>= 7;
395
+ }
396
+ *out = (uint8_t)payload;
397
+ }
398
+ }
399
+
400
+ PBW_INLINE void
401
+ w_fixed(writer *w, uint64_t value, size_t size)
402
+ {
403
+ uint8_t *out;
404
+
405
+ if (!w_reserve(w, size)) {
406
+ return;
407
+ }
408
+ out = w->base + w->pos;
409
+ if (size == 4) {
410
+ out[0] = (uint8_t)value;
411
+ out[1] = (uint8_t)(value >> 8);
412
+ out[2] = (uint8_t)(value >> 16);
413
+ out[3] = (uint8_t)(value >> 24);
414
+ } else {
415
+ out[0] = (uint8_t)value;
416
+ out[1] = (uint8_t)(value >> 8);
417
+ out[2] = (uint8_t)(value >> 16);
418
+ out[3] = (uint8_t)(value >> 24);
419
+ out[4] = (uint8_t)(value >> 32);
420
+ out[5] = (uint8_t)(value >> 40);
421
+ out[6] = (uint8_t)(value >> 48);
422
+ out[7] = (uint8_t)(value >> 56);
423
+ }
424
+ w->pos += size;
425
+ }
426
+
427
+ static void encode_message(writer *w, const pbw_message *msg, uint32_t depth,
428
+ int det);
429
+
430
+ /* Group framing shared by PBW_TYPE_GROUP and editions DELIMITED message
431
+ * fields: START tag, message body, END tag. Groups are rare either way, so
432
+ * the END tag is formatted per write instead of precomputed. */
433
+ static void
434
+ write_group_body(writer *w, const pbw_field *field, const pbw_value *value,
435
+ uint32_t depth, int det)
436
+ {
437
+ if (value->as.msg == NULL) {
438
+ w->status = PBW_ERR_INVALID;
439
+ return;
440
+ }
441
+ if (depth == PBW_MAX_DEPTH) {
442
+ w->status = PBW_ERR_LIMIT;
443
+ return;
444
+ }
445
+ w_field_tag(w, field, 0);
446
+ encode_message(w, value->as.msg, depth + 1, det);
447
+ if (w->status != PBW_OK) {
448
+ return;
449
+ }
450
+ /* Format the END tag straight into the output: bouncing it through a
451
+ * stack temp trips -Werror=array-bounds under _FORTIFY_SOURCE once
452
+ * w_copy inlines (a runtime length against a 5-byte object), and the
453
+ * extra copy is pointless anyway. */
454
+ if (!w_reserve(w, 5)) {
455
+ return;
456
+ }
457
+ w->pos += pbw_varint_write(w->base + w->pos,
458
+ ((uint64_t)field->number << 3) |
459
+ PBW_WIRE_END_GROUP);
460
+ }
461
+
462
+ /* One MessageSet item: START_GROUP(1), type_id (field 2), the extension
463
+ * payload (field 3, length-prefixed), END_GROUP(1). Tags are constants:
464
+ * the item number is always 1 on the wire, never the extension number. */
465
+ static void
466
+ write_messageset_item(writer *w, const pbw_field *field,
467
+ const pbw_message *sub, uint32_t depth, int det)
468
+ {
469
+ size_t mark;
470
+ uint8_t tag[2];
471
+
472
+ if (depth == PBW_MAX_DEPTH) {
473
+ w->status = PBW_ERR_LIMIT;
474
+ return;
475
+ }
476
+ /* START_GROUP(1) */
477
+ tag[0] = (1u << 3) | PBW_WIRE_START_GROUP;
478
+ w_put(w, tag, 1);
479
+ /* field 2: type_id */
480
+ tag[0] = (2u << 3) | PBW_WIRE_VARINT;
481
+ w_put(w, tag, 1);
482
+ w_varint(w, field->number);
483
+ /* field 3: message bytes, length patched like every LEN field */
484
+ tag[0] = (3u << 3) | PBW_WIRE_LEN;
485
+ w_put(w, tag, 1);
486
+ w_len_begin(w, &mark);
487
+ if (w->status != PBW_OK) {
488
+ return;
489
+ }
490
+ encode_message(w, sub, depth + 1, det);
491
+ if (w->status != PBW_OK) {
492
+ return;
493
+ }
494
+ w_len_end(w, mark);
495
+ /* END_GROUP(1) */
496
+ tag[0] = (1u << 3) | PBW_WIRE_END_GROUP;
497
+ w_put(w, tag, 1);
498
+ }
499
+
500
+ PBW_INLINE void
501
+ write_element(writer *w, const pbw_field *field, const pbw_value *value,
502
+ uint32_t depth, int det)
503
+ {
504
+ /* No depth check for scalars: only messages and groups recurse, and they
505
+ * guard against PBW_MAX_DEPTH below. Depth never exceeds the limit here
506
+ * (top-level starts at 0, nesting is capped before recursing). */
507
+ switch (field->type) {
508
+ case PBW_TYPE_STRING:
509
+ case PBW_TYPE_BYTES: {
510
+ /* `put` takes any value, so one wrong-kind slot would read the
511
+ * wrong union arm (an integer as a pointer). The packed path
512
+ * already checks each item; the singular path checks the arms it
513
+ * dereferences (message/bytes), fused into the existing guards. */
514
+ if (value == NULL || value->kind != PBW_VALUE_BYTES ||
515
+ (value->as.bytes.len > 0 && value->as.bytes.data == NULL)) {
516
+ /* Wrong kind, or a payload that cannot exist (a NULL pointer with
517
+ * a length): copying it would read through NULL. */
518
+ w->status = PBW_ERR_INVALID;
519
+ return;
520
+ }
521
+ /* Length is known before the payload: reserve once for tag,
522
+ * length and payload instead of three separate checks. The tag
523
+ * write stores a whole word, so reserve 4 for it. */
524
+ size_t lb = pbw_varint_size(value->as.bytes.len);
525
+ if (field->tag_len <= 4) {
526
+ if (value->as.bytes.len > SIZE_MAX - 4 - lb) {
527
+ /* A payload that big cannot exist (it would need SIZE_MAX
528
+ * bytes of live memory); refuse rather than wrap the sum into
529
+ * a tiny reserve and then copy past it. */
530
+ w->status = PBW_ERR_LIMIT;
531
+ return;
532
+ }
533
+ if (!w_reserve(w, 4 + lb + value->as.bytes.len)) {
534
+ w->status = PBW_ERR_NOMEM;
535
+ return;
536
+ }
537
+ w_tag_noreserve(w, field->tag_word, field->tag_len);
538
+ w_varint_noreserve(w, value->as.bytes.len);
539
+ if (value->as.bytes.len > 0) {
540
+ w_copy(w->base + w->pos, value->as.bytes.data,
541
+ value->as.bytes.len);
542
+ w->pos += value->as.bytes.len;
543
+ }
544
+ return;
545
+ }
546
+ w_field_tag(w, field, 0);
547
+ w_varint(w, value->as.bytes.len);
548
+ w_put(w, value->as.bytes.data, value->as.bytes.len);
549
+ return;
550
+ }
551
+ case PBW_TYPE_GROUP: {
552
+ if (value == NULL || value->as.msg == NULL ||
553
+ value->kind != PBW_VALUE_MESSAGE) {
554
+ w->status = PBW_ERR_INVALID;
555
+ return;
556
+ }
557
+ write_group_body(w, field, value, depth, det);
558
+ return;
559
+ }
560
+ case PBW_TYPE_MESSAGE: {
561
+ size_t mark;
562
+
563
+ if (value == NULL || value->as.msg == NULL ||
564
+ value->kind != PBW_VALUE_MESSAGE) {
565
+ w->status = PBW_ERR_INVALID;
566
+ return;
567
+ }
568
+ /* Editions message_encoding = DELIMITED travels as group tags. */
569
+ if (field->msg_enc == 2) {
570
+ write_group_body(w, field, value, depth, det);
571
+ return;
572
+ }
573
+ if (depth == PBW_MAX_DEPTH) {
574
+ w->status = PBW_ERR_LIMIT;
575
+ return;
576
+ }
577
+ /* Tag plus length placeholder in one reserve; the payload that
578
+ * follows grows on demand like before. The tag write stores a
579
+ * whole word, so reserve 4 for it. */
580
+ if (field->tag_len <= 4) {
581
+ if (!w_reserve(w, 4 + 1)) {
582
+ w->status = PBW_ERR_NOMEM;
583
+ return;
584
+ }
585
+ w_tag_noreserve(w, field->tag_word, field->tag_len);
586
+ mark = w->pos;
587
+ w->base[w->pos] = 0;
588
+ w->pos += 1;
589
+ } else {
590
+ w_field_tag(w, field, 0);
591
+ w_len_begin(w, &mark);
592
+ if (w->status != PBW_OK) {
593
+ return;
594
+ }
595
+ }
596
+ encode_message(w, value->as.msg, depth + 1, det);
597
+ if (w->status != PBW_OK) {
598
+ return;
599
+ }
600
+ w_len_end(w, mark);
601
+ return;
602
+ }
603
+ case PBW_TYPE_FLOAT:
604
+ case PBW_TYPE_FIXED32:
605
+ case PBW_TYPE_SFIXED32:
606
+ if (value == NULL || value->kind != PBW_VALUE_UINT) {
607
+ w->status = PBW_ERR_INVALID;
608
+ return;
609
+ }
610
+ /* One reserve for tag plus value: the common small-message path
611
+ * then writes without another capacity check. The tag write stores
612
+ * a whole word, so reserve 4 for it. */
613
+ if (field->tag_len <= 4) {
614
+ if (!w_reserve(w, 4 + 4)) {
615
+ w->status = PBW_ERR_NOMEM;
616
+ return;
617
+ }
618
+ w_tag_noreserve(w, field->tag_word, field->tag_len);
619
+ w_fixed_noreserve(w, value->as.u64, 4);
620
+ return;
621
+ }
622
+ w_field_tag(w, field, 0);
623
+ w_fixed(w, value->as.u64, 4);
624
+ return;
625
+ case PBW_TYPE_DOUBLE:
626
+ case PBW_TYPE_FIXED64:
627
+ case PBW_TYPE_SFIXED64:
628
+ if (value == NULL || value->kind != PBW_VALUE_UINT) {
629
+ w->status = PBW_ERR_INVALID;
630
+ return;
631
+ }
632
+ if (field->tag_len <= 4) {
633
+ if (!w_reserve(w, 4 + 8)) {
634
+ w->status = PBW_ERR_NOMEM;
635
+ return;
636
+ }
637
+ w_tag_noreserve(w, field->tag_word, field->tag_len);
638
+ w_fixed_noreserve(w, value->as.u64, 8);
639
+ return;
640
+ }
641
+ w_field_tag(w, field, 0);
642
+ w_fixed(w, value->as.u64, 8);
643
+ return;
644
+ default:
645
+ if (value == NULL || value->kind != PBW_VALUE_UINT) {
646
+ w->status = PBW_ERR_INVALID;
647
+ return;
648
+ }
649
+ if (field->tag_len <= 4) {
650
+ if (!w_reserve(w, 4 + 10)) {
651
+ w->status = PBW_ERR_NOMEM;
652
+ return;
653
+ }
654
+ w_tag_noreserve(w, field->tag_word, field->tag_len);
655
+ w_varint_noreserve(w, wire_value(field->type, value->as.u64));
656
+ return;
657
+ }
658
+ w_field_tag(w, field, 0);
659
+ w_varint(w, wire_value(field->type, value->as.u64));
660
+ return;
661
+ }
662
+ }
663
+
664
+ /* Bytes one packed item occupies. */
665
+ PBW_INLINE size_t
666
+ packed_item_size(pbw_type type, uint64_t bits)
667
+ {
668
+ switch (type) {
669
+ case PBW_TYPE_FLOAT:
670
+ case PBW_TYPE_FIXED32:
671
+ case PBW_TYPE_SFIXED32:
672
+ return 4;
673
+ case PBW_TYPE_DOUBLE:
674
+ case PBW_TYPE_FIXED64:
675
+ case PBW_TYPE_SFIXED64:
676
+ return 8;
677
+ default:
678
+ return pbw_varint_size(wire_value(type, bits));
679
+ }
680
+ }
681
+
682
+ /* One length-prefixed run of scalars. The payload size is computed first so the
683
+ * length prefix can be written in place: no placeholder, no memmove of the
684
+ * payload, and the reservation is exactly what the run needs (a caller-provided
685
+ * buffer therefore does not need room for a 10-bytes-per-item worst case). */
686
+ /* Appends a varint to a caller-hoisted cursor: the blind packed write
687
+ * loops advance a local pointer instead of the writer position per item. */
688
+ PBW_INLINE void
689
+ w_varint_out(uint8_t **out, uint64_t value)
690
+ {
691
+ uint8_t *p = *out;
692
+
693
+ while (value >= 0x80) {
694
+ *p++ = (uint8_t)((value & 0x7f) | 0x80);
695
+ value >>= 7;
696
+ }
697
+ *p++ = (uint8_t)value;
698
+ *out = p;
699
+ }
700
+
701
+ static void
702
+ write_packed(writer *w, const pbw_field *field, const pbw_value *list)
703
+ {
704
+ pbw_value **items;
705
+ size_t n;
706
+ size_t payload = 0;
707
+ size_t i;
708
+
709
+ if (w == NULL || w->status != PBW_OK || field == NULL || list == NULL) {
710
+ if (w != NULL && w->status == PBW_OK) {
711
+ w->status = PBW_ERR_INVALID;
712
+ }
713
+ return;
714
+ }
715
+ if (list->kind != PBW_VALUE_LIST) {
716
+ w->status = PBW_ERR_INVALID;
717
+ return;
718
+ }
719
+ items = list->as.list.items;
720
+ n = list->as.list.len;
721
+ if (n == 0) {
722
+ return;
723
+ }
724
+ if (items == NULL) {
725
+ w->status = PBW_ERR_INVALID;
726
+ return;
727
+ }
728
+ if (n > (SIZE_MAX - 16) / 10) {
729
+ w->status = PBW_ERR_LIMIT;
730
+ return;
731
+ }
732
+ /* One dispatch for the whole run: every item shares the field's type.
733
+ * Fixed-size kinds write in a single validated pass (their payload is
734
+ * known upfront); varint kinds size first and then run a blind
735
+ * kind-specific write. Every loop hoists the output pointer into a
736
+ * local: advancing through the writer struct per item stalls the
737
+ * pipeline and blocks vectorization. n is capped above, so n * 8 below
738
+ * cannot wrap. */
739
+ switch (field->type) {
740
+ case PBW_TYPE_BOOL: {
741
+ uint8_t *out;
742
+ payload = n;
743
+ if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
744
+ pbw_varint_size(payload) + payload)) {
745
+ w->status = PBW_ERR_NOMEM;
746
+ return;
747
+ }
748
+ if (field->len_tag_len <= 4) {
749
+ w_tag_noreserve(w, field->len_tag_word, field->len_tag_len);
750
+ } else {
751
+ w_copy(w->base + w->pos, field->len_tag, field->len_tag_len);
752
+ w->pos += field->len_tag_len;
753
+ }
754
+ w_varint_noreserve(w, payload);
755
+ out = w->base + w->pos;
756
+ for (i = 0; i < n; i++) {
757
+ pbw_value *item = items[i];
758
+ if (item == NULL || item->kind != PBW_VALUE_UINT) {
759
+ w->pos += i;
760
+ w->status = PBW_ERR_INVALID;
761
+ return;
762
+ }
763
+ out[i] = item->as.u64 != 0 ? 1u : 0u;
764
+ }
765
+ w->pos += n;
766
+ return;
767
+ }
768
+ case PBW_TYPE_FLOAT:
769
+ case PBW_TYPE_FIXED32:
770
+ case PBW_TYPE_SFIXED32: {
771
+ uint8_t *out;
772
+ payload = n * 4;
773
+ if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
774
+ pbw_varint_size(payload) + payload)) {
775
+ w->status = PBW_ERR_NOMEM;
776
+ return;
777
+ }
778
+ if (field->len_tag_len <= 4) {
779
+ w_tag_noreserve(w, field->len_tag_word, field->len_tag_len);
780
+ } else {
781
+ w_copy(w->base + w->pos, field->len_tag, field->len_tag_len);
782
+ w->pos += field->len_tag_len;
783
+ }
784
+ w_varint_noreserve(w, payload);
785
+ out = w->base + w->pos;
786
+ for (i = 0; i < n; i++) {
787
+ pbw_value *item = items[i];
788
+ uint32_t v;
789
+ if (item == NULL || item->kind != PBW_VALUE_UINT) {
790
+ w->pos += i * 4;
791
+ w->status = PBW_ERR_INVALID;
792
+ return;
793
+ }
794
+ v = (uint32_t)item->as.u64;
795
+ out[i * 4] = (uint8_t)v;
796
+ out[i * 4 + 1] = (uint8_t)(v >> 8);
797
+ out[i * 4 + 2] = (uint8_t)(v >> 16);
798
+ out[i * 4 + 3] = (uint8_t)(v >> 24);
799
+ }
800
+ w->pos += payload;
801
+ return;
802
+ }
803
+ case PBW_TYPE_DOUBLE:
804
+ case PBW_TYPE_FIXED64:
805
+ case PBW_TYPE_SFIXED64: {
806
+ uint8_t *out;
807
+ payload = n * 8;
808
+ if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
809
+ pbw_varint_size(payload) + payload)) {
810
+ w->status = PBW_ERR_NOMEM;
811
+ return;
812
+ }
813
+ if (field->len_tag_len <= 4) {
814
+ w_tag_noreserve(w, field->len_tag_word, field->len_tag_len);
815
+ } else {
816
+ w_copy(w->base + w->pos, field->len_tag, field->len_tag_len);
817
+ w->pos += field->len_tag_len;
818
+ }
819
+ w_varint_noreserve(w, payload);
820
+ out = w->base + w->pos;
821
+ for (i = 0; i < n; i++) {
822
+ pbw_value *item = items[i];
823
+ uint64_t v;
824
+ if (item == NULL || item->kind != PBW_VALUE_UINT) {
825
+ w->pos += i * 8;
826
+ w->status = PBW_ERR_INVALID;
827
+ return;
828
+ }
829
+ v = item->as.u64;
830
+ out[i * 8] = (uint8_t)v;
831
+ out[i * 8 + 1] = (uint8_t)(v >> 8);
832
+ out[i * 8 + 2] = (uint8_t)(v >> 16);
833
+ out[i * 8 + 3] = (uint8_t)(v >> 24);
834
+ out[i * 8 + 4] = (uint8_t)(v >> 32);
835
+ out[i * 8 + 5] = (uint8_t)(v >> 40);
836
+ out[i * 8 + 6] = (uint8_t)(v >> 48);
837
+ out[i * 8 + 7] = (uint8_t)(v >> 56);
838
+ }
839
+ w->pos += payload;
840
+ return;
841
+ }
842
+ case PBW_TYPE_INT32:
843
+ case PBW_TYPE_ENUM:
844
+ for (i = 0; i < n; i++) {
845
+ if (items[i] == NULL || items[i]->kind != PBW_VALUE_UINT) {
846
+ w->status = PBW_ERR_INVALID;
847
+ return;
848
+ }
849
+ payload += pbw_varint_size(
850
+ (uint64_t)(int64_t)(int32_t)items[i]->as.u64);
851
+ }
852
+ break;
853
+ case PBW_TYPE_UINT32:
854
+ for (i = 0; i < n; i++) {
855
+ if (items[i] == NULL || items[i]->kind != PBW_VALUE_UINT) {
856
+ w->status = PBW_ERR_INVALID;
857
+ return;
858
+ }
859
+ payload += pbw_varint_size((uint64_t)(uint32_t)items[i]->as.u64);
860
+ }
861
+ break;
862
+ case PBW_TYPE_SINT32:
863
+ for (i = 0; i < n; i++) {
864
+ if (items[i] == NULL || items[i]->kind != PBW_VALUE_UINT) {
865
+ w->status = PBW_ERR_INVALID;
866
+ return;
867
+ }
868
+ payload += pbw_varint_size(
869
+ zigzag_encode(items[i]->as.u64, 32));
870
+ }
871
+ break;
872
+ case PBW_TYPE_SINT64:
873
+ for (i = 0; i < n; i++) {
874
+ if (items[i] == NULL || items[i]->kind != PBW_VALUE_UINT) {
875
+ w->status = PBW_ERR_INVALID;
876
+ return;
877
+ }
878
+ payload += pbw_varint_size(
879
+ zigzag_encode(items[i]->as.u64, 64));
880
+ }
881
+ break;
882
+ default:
883
+ for (i = 0; i < n; i++) {
884
+ if (items[i] == NULL || items[i]->kind != PBW_VALUE_UINT) {
885
+ w->status = PBW_ERR_INVALID;
886
+ return;
887
+ }
888
+ payload += pbw_varint_size(items[i]->as.u64);
889
+ }
890
+ break;
891
+ }
892
+ /* Four bytes cover the tag word store; a five-byte length tag (field
893
+ * number >= 2^21) is copied byte by byte instead. */
894
+ if (!w_reserve(w, (field->len_tag_len <= 4 ? 4 : field->len_tag_len) +
895
+ pbw_varint_size(payload) + payload)) {
896
+ w->status = PBW_ERR_NOMEM;
897
+ return;
898
+ }
899
+ if (field->len_tag_len <= 4) {
900
+ w_tag_noreserve(w, field->len_tag_word, field->len_tag_len);
901
+ } else {
902
+ w_copy(w->base + w->pos, field->len_tag, field->len_tag_len);
903
+ w->pos += field->len_tag_len;
904
+ }
905
+ w_varint_noreserve(w, payload);
906
+ /* Blind write pass: every item was validated above, so the output
907
+ * pointer stays hoisted with no checks and no switches in the loop. */
908
+ switch (field->type) {
909
+ case PBW_TYPE_INT32:
910
+ case PBW_TYPE_ENUM: {
911
+ uint8_t *out = w->base + w->pos;
912
+ for (i = 0; i < n; i++) {
913
+ w_varint_out(&out,
914
+ (uint64_t)(int64_t)(int32_t)items[i]->as.u64);
915
+ }
916
+ w->pos += payload;
917
+ return;
918
+ }
919
+ case PBW_TYPE_UINT32: {
920
+ uint8_t *out = w->base + w->pos;
921
+ for (i = 0; i < n; i++) {
922
+ w_varint_out(&out, (uint64_t)(uint32_t)items[i]->as.u64);
923
+ }
924
+ w->pos += payload;
925
+ return;
926
+ }
927
+ case PBW_TYPE_SINT32: {
928
+ uint8_t *out = w->base + w->pos;
929
+ for (i = 0; i < n; i++) {
930
+ w_varint_out(&out, zigzag_encode(items[i]->as.u64, 32));
931
+ }
932
+ w->pos += payload;
933
+ return;
934
+ }
935
+ case PBW_TYPE_SINT64: {
936
+ uint8_t *out = w->base + w->pos;
937
+ for (i = 0; i < n; i++) {
938
+ w_varint_out(&out, zigzag_encode(items[i]->as.u64, 64));
939
+ }
940
+ w->pos += payload;
941
+ return;
942
+ }
943
+ default: {
944
+ uint8_t *out = w->base + w->pos;
945
+ for (i = 0; i < n; i++) {
946
+ w_varint_out(&out, items[i]->as.u64);
947
+ }
948
+ w->pos += payload;
949
+ return;
950
+ }
951
+ }
952
+ }
953
+
954
+ /* Deterministic encoding sorts map entries by key, like the reference
955
+ * deterministic serializers. Only wire order changes: the bytes of each entry
956
+ * are identical. */
957
+ static int
958
+ map_key_cmp(const pbw_field *kf, const pbw_value *a, const pbw_value *b)
959
+ {
960
+ const pbw_message *ea = a->kind == PBW_VALUE_MESSAGE ? a->as.msg : NULL;
961
+ const pbw_message *eb = b->kind == PBW_VALUE_MESSAGE ? b->as.msg : NULL;
962
+ const pbw_value *ka = ea != NULL ? pbw_message_get(ea, 1) : NULL;
963
+ const pbw_value *kb = eb != NULL ? pbw_message_get(eb, 1) : NULL;
964
+
965
+ if (ka == kb) {
966
+ return 0;
967
+ }
968
+ if (ka == NULL) {
969
+ return -1;
970
+ }
971
+ if (kb == NULL) {
972
+ return 1;
973
+ }
974
+ if (ka->kind == PBW_VALUE_BYTES && kb->kind == PBW_VALUE_BYTES) {
975
+ size_t n = ka->as.bytes.len < kb->as.bytes.len ? ka->as.bytes.len
976
+ : kb->as.bytes.len;
977
+ int r = n > 0 ? memcmp(ka->as.bytes.data, kb->as.bytes.data, n) : 0;
978
+
979
+ if (r != 0) {
980
+ return r < 0 ? -1 : 1;
981
+ }
982
+ if (ka->as.bytes.len != kb->as.bytes.len) {
983
+ return ka->as.bytes.len < kb->as.bytes.len ? -1 : 1;
984
+ }
985
+ return 0;
986
+ }
987
+ switch (kf->type) {
988
+ case PBW_TYPE_INT32:
989
+ case PBW_TYPE_INT64:
990
+ case PBW_TYPE_SINT32:
991
+ case PBW_TYPE_SINT64:
992
+ case PBW_TYPE_SFIXED32:
993
+ case PBW_TYPE_SFIXED64:
994
+ if ((int64_t)ka->as.u64 != (int64_t)kb->as.u64) {
995
+ return (int64_t)ka->as.u64 < (int64_t)kb->as.u64 ? -1 : 1;
996
+ }
997
+ return 0;
998
+ default:
999
+ if (ka->as.u64 != kb->as.u64) {
1000
+ return ka->as.u64 < kb->as.u64 ? -1 : 1;
1001
+ }
1002
+ return 0;
1003
+ }
1004
+ }
1005
+
1006
+ /* Merge sort: the recursion depth is log2(n), not message nesting, so the
1007
+ * PBW_MAX_DEPTH cap does not apply. */
1008
+ static void
1009
+ map_sort(const pbw_field *kf, pbw_value **items, pbw_value **tmp, size_t n)
1010
+ {
1011
+ size_t mid;
1012
+ size_t i;
1013
+ size_t j;
1014
+ size_t k;
1015
+
1016
+ if (n < 2) {
1017
+ return;
1018
+ }
1019
+ mid = n / 2;
1020
+ map_sort(kf, items, tmp, mid);
1021
+ map_sort(kf, items + mid, tmp + mid, n - mid);
1022
+ i = 0;
1023
+ j = mid;
1024
+ k = 0;
1025
+ while (i < mid && j < n) {
1026
+ if (map_key_cmp(kf, items[j], items[i]) < 0) {
1027
+ tmp[k++] = items[j++];
1028
+ } else {
1029
+ tmp[k++] = items[i++];
1030
+ }
1031
+ }
1032
+ while (i < mid) {
1033
+ tmp[k++] = items[i++];
1034
+ }
1035
+ while (j < n) {
1036
+ tmp[k++] = items[j++];
1037
+ }
1038
+ memcpy(items, tmp, n * sizeof(*items));
1039
+ }
1040
+
1041
+ static void
1042
+ write_map_sorted(writer *w, const pbw_field *field, const pbw_value *list,
1043
+ uint32_t depth, int det)
1044
+ {
1045
+ const pbw_field *kf = pbw_find_field(field->sub, 1);
1046
+ pbw_value **items;
1047
+ pbw_value **tmp;
1048
+ size_t n = list->as.list.len;
1049
+ size_t j;
1050
+
1051
+ if (kf == NULL) {
1052
+ /* No key field: keep insertion order. */
1053
+ for (j = 0; j < n; j++) {
1054
+ write_element(w, field, list->as.list.items[j], depth, det);
1055
+ if (w->status != PBW_OK) {
1056
+ return;
1057
+ }
1058
+ }
1059
+ return;
1060
+ }
1061
+ if (n == 0) {
1062
+ return;
1063
+ }
1064
+ if (n > SIZE_MAX / (2 * sizeof(*items))) {
1065
+ w->status = PBW_ERR_NOMEM;
1066
+ return;
1067
+ }
1068
+ /* One allocation split in two: halves the malloc/free calls on the
1069
+ * deterministic path with no behavior change. */
1070
+ items = (pbw_value **)malloc(2 * n * sizeof(*items));
1071
+ if (items == NULL) {
1072
+ w->status = PBW_ERR_NOMEM;
1073
+ return;
1074
+ }
1075
+ tmp = items + n;
1076
+ memcpy(items, list->as.list.items, n * sizeof(*items));
1077
+ /* A key payload that cannot exist fails the encode before map_key_cmp()
1078
+ * compares it: write_element() would refuse it, too late. A NULL entry
1079
+ * fails here too: the sort below reads every item's kind. */
1080
+ for (j = 0; j < n; j++) {
1081
+ const pbw_message *em;
1082
+ const pbw_value *k;
1083
+
1084
+ if (items[j] == NULL ||
1085
+ items[j]->kind != PBW_VALUE_MESSAGE ||
1086
+ items[j]->as.msg == NULL) {
1087
+ free(items);
1088
+ w->status = PBW_ERR_INVALID;
1089
+ return;
1090
+ }
1091
+ em = items[j]->as.msg;
1092
+ k = pbw_message_get(em, 1);
1093
+
1094
+ if (k != NULL && k->kind == PBW_VALUE_BYTES &&
1095
+ !pbw_bytes_readable(k)) {
1096
+ free(items);
1097
+ w->status = PBW_ERR_INVALID;
1098
+ return;
1099
+ }
1100
+ }
1101
+ map_sort(kf, items, tmp, n);
1102
+ for (j = 0; j < n; j++) {
1103
+ write_element(w, field, items[j], depth, det);
1104
+ if (w->status != PBW_OK) {
1105
+ break;
1106
+ }
1107
+ }
1108
+ free(items);
1109
+ }
1110
+
1111
+ static void
1112
+ encode_message(writer *w, const pbw_message *msg, uint32_t depth, int det)
1113
+ {
1114
+ size_t i;
1115
+ /* A nested message retired by a later add_field or link has fewer slots
1116
+ * (or a different field order) than its live descriptor, so walking it
1117
+ * would read past them or emit the wrong field numbers. The root gets the
1118
+ * full check on entry; every nested level (a submessage, a repeated item,
1119
+ * a MessageSet payload) only needs the epoch: msg and msg->desc are
1120
+ * non-NULL and a message is never built on an enum descriptor. One
1121
+ * compare, hinted not-taken: a full pbw_msg_fresh() here cost 9% on the
1122
+ * nested-message-heavy xlarge bench tier. */
1123
+ if (PBW_EXPECT(msg->epoch != msg->desc->epoch, 0)) {
1124
+ w->status = PBW_ERR_INVALID;
1125
+ return;
1126
+ }
1127
+ /* The presence bitmap never changes while encoding, so hoist the first
1128
+ * word: most messages have 32 fields or fewer and then test bits without
1129
+ * reloading the bitmap per field. Larger descriptors use the generic
1130
+ * accessor. */
1131
+ int single_word = msg->desc->field_count <= 32;
1132
+ uint32_t present0 =
1133
+ (single_word && msg->desc->field_count > 0 && msg->present != NULL)
1134
+ ? msg->present[0]
1135
+ : 0;
1136
+
1137
+ for (i = 0; i < msg->desc->field_count; i++) {
1138
+ const pbw_field *field = &msg->desc->fields[i];
1139
+ const pbw_value *value;
1140
+ size_t j;
1141
+ int present = single_word ? (int)((present0 >> (i & 31u)) & 1u)
1142
+ : pbw_msg_present(msg, i);
1143
+
1144
+ if (!PBW_EXPECT(present, 1)) {
1145
+ continue;
1146
+ }
1147
+ value = &msg->values[i];
1148
+ if (field->label == PBW_LABEL_REPEATED) {
1149
+ if (value->kind != PBW_VALUE_LIST || value->as.list.len == 0) {
1150
+ continue;
1151
+ }
1152
+ if (value->as.list.items == NULL) {
1153
+ /* Corrupt list (no producer builds one): refuse instead of
1154
+ * reading item 0 through a NULL array. */
1155
+ w->status = PBW_ERR_INVALID;
1156
+ return;
1157
+ }
1158
+ if (field->packed) {
1159
+ write_packed(w, field, value);
1160
+ continue;
1161
+ }
1162
+ if (det && field->type == PBW_TYPE_MESSAGE && field->sub != NULL &&
1163
+ field->sub->map_entry) {
1164
+ write_map_sorted(w, field, value, depth, det);
1165
+ if (w->status != PBW_OK) {
1166
+ return;
1167
+ }
1168
+ continue;
1169
+ }
1170
+ for (j = 0; j < value->as.list.len; j++) {
1171
+ write_element(w, field, value->as.list.items[j], depth, det);
1172
+ if (w->status != PBW_OK) {
1173
+ return;
1174
+ }
1175
+ }
1176
+ continue;
1177
+ }
1178
+ if (pbw_is_messageset_item(field, msg) &&
1179
+ value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL) {
1180
+ write_messageset_item(w, field, value->as.msg, depth, det);
1181
+ } else {
1182
+ write_element(w, field, value, depth, det);
1183
+ }
1184
+ if (w->status != PBW_OK) {
1185
+ return;
1186
+ }
1187
+ }
1188
+ /* Unknown fields go last, in the order they were read: the reference
1189
+ * implementations do the same, so a re-encoded message stays byte-identical
1190
+ * for a peer that only knows part of the schema. */
1191
+ w_put(w, msg->unknown, msg->unknown_len);
1192
+ }
1193
+
1194
+ /* ------------------------------------------------------------------ sizing */
1195
+
1196
+ /* Exact wire size, mirroring encode_message()/write_element() branch for
1197
+ * branch. The per-item helpers (packed_item_size, wire_value,
1198
+ * pbw_varint_size) are shared, so only the traversal itself can drift: the
1199
+ * self-checks and the bench difftest assert size == encoded length for every
1200
+ * message they walk, fuzzed ones included. */
1201
+
1202
+ /* a + b, refusing to wrap: lengths come from user data and a wrapped total
1203
+ * would make the caller allocate too little. */
1204
+ static int
1205
+ size_add(size_t *total, size_t extra)
1206
+ {
1207
+ if (*total > SIZE_MAX - extra) {
1208
+ return 0;
1209
+ }
1210
+ *total += extra;
1211
+ return 1;
1212
+ }
1213
+
1214
+ /* tag + length varint + payload, the shape of every length-delimited field. */
1215
+ static pbw_status
1216
+ size_len_prefixed(size_t *out, size_t tag_len, size_t body)
1217
+ {
1218
+ size_t total = tag_len;
1219
+
1220
+ if (!size_add(&total, pbw_varint_size(body)) || !size_add(&total, body)) {
1221
+ return PBW_ERR_LIMIT;
1222
+ }
1223
+ *out = total;
1224
+ return PBW_OK;
1225
+ }
1226
+
1227
+ static pbw_status size_message(const pbw_message *msg, uint32_t depth,
1228
+ size_t *out);
1229
+
1230
+ static pbw_status
1231
+ size_element(const pbw_field *field, const pbw_value *value, uint32_t depth,
1232
+ size_t *out)
1233
+ {
1234
+ size_t body;
1235
+ pbw_status status;
1236
+
1237
+ switch (field->type) {
1238
+ case PBW_TYPE_STRING:
1239
+ case PBW_TYPE_BYTES:
1240
+ if (value == NULL || value->kind != PBW_VALUE_BYTES ||
1241
+ (value->as.bytes.len > 0 && value->as.bytes.data == NULL)) {
1242
+ /* Same payload rule as write_element(): the size walk must
1243
+ * refuse exactly what the writer refuses. */
1244
+ return PBW_ERR_INVALID;
1245
+ }
1246
+ return size_len_prefixed(out, field->tag_len, value->as.bytes.len);
1247
+ case PBW_TYPE_MESSAGE:
1248
+ if (value == NULL || value->as.msg == NULL ||
1249
+ value->kind != PBW_VALUE_MESSAGE) {
1250
+ return PBW_ERR_INVALID;
1251
+ }
1252
+ if (depth == PBW_MAX_DEPTH) {
1253
+ return PBW_ERR_LIMIT;
1254
+ }
1255
+ status = size_message(value->as.msg, depth + 1, &body);
1256
+ if (status != PBW_OK) {
1257
+ return status;
1258
+ }
1259
+ if (field->msg_enc == 2) {
1260
+ /* Delimited: START tag + body + END tag, like a group. */
1261
+ if (!size_add(&body, pbw_varint_size(((uint64_t)field->number
1262
+ << 3) |
1263
+ PBW_WIRE_END_GROUP)) ||
1264
+ !size_add(&body, field->tag_len)) {
1265
+ return PBW_ERR_LIMIT;
1266
+ }
1267
+ *out = body;
1268
+ return PBW_OK;
1269
+ }
1270
+ return size_len_prefixed(out, field->tag_len, body);
1271
+ case PBW_TYPE_GROUP:
1272
+ if (value == NULL || value->as.msg == NULL ||
1273
+ value->kind != PBW_VALUE_MESSAGE) {
1274
+ return PBW_ERR_INVALID;
1275
+ }
1276
+ if (depth == PBW_MAX_DEPTH) {
1277
+ return PBW_ERR_LIMIT;
1278
+ }
1279
+ status = size_message(value->as.msg, depth + 1, &body);
1280
+ if (status != PBW_OK) {
1281
+ return status;
1282
+ }
1283
+ /* A group has no length prefix: the END_GROUP tag stands in for it. */
1284
+ if (!size_add(&body, pbw_varint_size(((uint64_t)field->number << 3) |
1285
+ PBW_WIRE_END_GROUP)) ||
1286
+ !size_add(&body, field->tag_len)) {
1287
+ return PBW_ERR_LIMIT;
1288
+ }
1289
+ *out = body;
1290
+ return PBW_OK;
1291
+ case PBW_TYPE_FLOAT:
1292
+ case PBW_TYPE_FIXED32:
1293
+ case PBW_TYPE_SFIXED32:
1294
+ if (value == NULL || value->kind != PBW_VALUE_UINT) {
1295
+ return PBW_ERR_INVALID;
1296
+ }
1297
+ *out = field->tag_len + 4;
1298
+ return PBW_OK;
1299
+ case PBW_TYPE_DOUBLE:
1300
+ case PBW_TYPE_FIXED64:
1301
+ case PBW_TYPE_SFIXED64:
1302
+ if (value == NULL || value->kind != PBW_VALUE_UINT) {
1303
+ return PBW_ERR_INVALID;
1304
+ }
1305
+ *out = field->tag_len + 8;
1306
+ return PBW_OK;
1307
+ default:
1308
+ if (value == NULL || value->kind != PBW_VALUE_UINT) {
1309
+ return PBW_ERR_INVALID;
1310
+ }
1311
+ body = pbw_varint_size(wire_value(field->type, value->as.u64));
1312
+ if (!size_add(&body, field->tag_len)) {
1313
+ return PBW_ERR_LIMIT;
1314
+ }
1315
+ *out = body;
1316
+ return PBW_OK;
1317
+ }
1318
+ }
1319
+
1320
+ static pbw_status
1321
+ size_message(const pbw_message *msg, uint32_t depth, size_t *out)
1322
+ {
1323
+ size_t total = 0;
1324
+ size_t i;
1325
+ /* Same staleness rule as encode_message(): the root is checked by the
1326
+ * caller, every nested level compares the epoch. */
1327
+ if (PBW_EXPECT(msg->epoch != msg->desc->epoch, 0)) {
1328
+ return PBW_ERR_INVALID;
1329
+ }
1330
+ /* Same presence hoist as encode_message(): one bitmap word covers most
1331
+ * descriptors, so the walk does not reload it per field. */
1332
+ int single_word = msg->desc->field_count <= 32;
1333
+ uint32_t present0 =
1334
+ (single_word && msg->desc->field_count > 0 && msg->present != NULL)
1335
+ ? msg->present[0]
1336
+ : 0;
1337
+
1338
+ for (i = 0; i < msg->desc->field_count; i++) {
1339
+ const pbw_field *field = &msg->desc->fields[i];
1340
+ const pbw_value *value;
1341
+ size_t part = 0;
1342
+ size_t j;
1343
+ int present = single_word ? (int)((present0 >> (i & 31u)) & 1u)
1344
+ : pbw_msg_present(msg, i);
1345
+
1346
+ if (!PBW_EXPECT(present, 1)) {
1347
+ continue;
1348
+ }
1349
+ value = &msg->values[i];
1350
+ if (field->label == PBW_LABEL_REPEATED) {
1351
+ size_t n;
1352
+ pbw_status status;
1353
+
1354
+ if (value->kind != PBW_VALUE_LIST || value->as.list.len == 0) {
1355
+ continue;
1356
+ }
1357
+ if (value->as.list.items == NULL) {
1358
+ return PBW_ERR_INVALID;
1359
+ }
1360
+ n = value->as.list.len;
1361
+ if (field->packed) {
1362
+ size_t payload = 0;
1363
+
1364
+ for (j = 0; j < n; j++) {
1365
+ pbw_value *item = value->as.list.items[j];
1366
+
1367
+ /* Same validation write_packed() does: the size walk
1368
+ * must reject exactly what the writer rejects, or a tree
1369
+ * that encode refuses would still report a length. */
1370
+ if (item == NULL || item->kind != PBW_VALUE_UINT) {
1371
+ return PBW_ERR_INVALID;
1372
+ }
1373
+ if (!size_add(&payload,
1374
+ packed_item_size(field->type,
1375
+ item->as.u64))) {
1376
+ return PBW_ERR_LIMIT;
1377
+ }
1378
+ }
1379
+ status = size_len_prefixed(&part, field->len_tag_len, payload);
1380
+ if (status != PBW_OK) {
1381
+ return status;
1382
+ }
1383
+ } else {
1384
+ for (j = 0; j < n; j++) {
1385
+ size_t item = 0;
1386
+
1387
+ status = size_element(field, value->as.list.items[j],
1388
+ depth, &item);
1389
+ if (status != PBW_OK) {
1390
+ return status;
1391
+ }
1392
+ if (!size_add(&part, item)) {
1393
+ return PBW_ERR_LIMIT;
1394
+ }
1395
+ }
1396
+ }
1397
+ } else {
1398
+ pbw_status status;
1399
+
1400
+ if (field->type == PBW_TYPE_MESSAGE &&
1401
+ pbw_is_messageset_item(field, msg) &&
1402
+ value->kind == PBW_VALUE_MESSAGE &&
1403
+ value->as.msg != NULL) {
1404
+ /* START + field2 tag + varint(id) + field3 tag +
1405
+ * varint(body) + body + END. */
1406
+ size_t body = 0;
1407
+
1408
+ /* Same cap as write_messageset_item(): the sizing walk must
1409
+ * reject what the writer rejects, never recurse deeper. */
1410
+ if (depth == PBW_MAX_DEPTH) {
1411
+ return PBW_ERR_LIMIT;
1412
+ }
1413
+ status = size_message(value->as.msg, depth + 1, &body);
1414
+ if (status != PBW_OK) {
1415
+ return status;
1416
+ }
1417
+ part = 4 + pbw_varint_size(field->number);
1418
+ if (!size_add(&part, pbw_varint_size(body)) ||
1419
+ !size_add(&part, body)) {
1420
+ return PBW_ERR_LIMIT;
1421
+ }
1422
+ } else {
1423
+ status = size_element(field, value, depth, &part);
1424
+ if (status != PBW_OK) {
1425
+ return status;
1426
+ }
1427
+ }
1428
+ }
1429
+ if (!size_add(&total, part)) {
1430
+ return PBW_ERR_LIMIT;
1431
+ }
1432
+ }
1433
+ /* Unknown fields are written verbatim after the known ones. */
1434
+ if (!size_add(&total, msg->unknown_len)) {
1435
+ return PBW_ERR_LIMIT;
1436
+ }
1437
+ *out = total;
1438
+ return PBW_OK;
1439
+ }
1440
+
1441
+ pbw_status
1442
+ pbw_message_encoded_size(const pbw_message *msg, size_t *len)
1443
+ {
1444
+ if (len == NULL) {
1445
+ return PBW_ERR_INVALID;
1446
+ }
1447
+ /* No length is known yet: never leave a stale one behind, like the
1448
+ * encode entry points that hand out a buffer. */
1449
+ *len = 0;
1450
+ if (!pbw_msg_fresh(msg) ||
1451
+ (msg->desc->schema != NULL && !msg->desc->schema->linked)) {
1452
+ return PBW_ERR_INVALID;
1453
+ }
1454
+ return size_message(msg, 0, len);
1455
+ }
1456
+
1457
+ /* -------------------------------------------------------------- entry points */
1458
+
1459
+ /* Encodes into a caller-provided buffer whose capacity is reused across calls. */
1460
+ pbw_status
1461
+ pbw_encode_buf(pbw_buf *buf, const pbw_message *msg)
1462
+ {
1463
+ writer w;
1464
+
1465
+ if (msg == NULL || buf == NULL || !pbw_msg_fresh(msg) ||
1466
+ (msg->desc->schema != NULL && !msg->desc->schema->linked)) {
1467
+ return PBW_ERR_INVALID;
1468
+ }
1469
+ /* A failed reserve marks the buffer out of memory; this call gets its own
1470
+ * status, so a retry after the caller freed memory must not inherit it. */
1471
+ buf->oom = 0;
1472
+ buf->len = 0;
1473
+ w_init(&w, buf);
1474
+ encode_message(&w, msg, 0, 0);
1475
+ buf->len = w.pos;
1476
+ return w.status;
1477
+ }
1478
+
1479
+ static pbw_status
1480
+ encode_alloc(const pbw_message *msg, int det, uint8_t **out, size_t *len)
1481
+ {
1482
+ pbw_buf buf;
1483
+ writer w;
1484
+
1485
+ if (out == NULL || len == NULL) {
1486
+ return PBW_ERR_INVALID;
1487
+ }
1488
+ /* Written on both paths, like every other entry point that hands out a
1489
+ * malloc'd buffer: a caller that reuses its pointer never keeps a stale
1490
+ * one after a failure. */
1491
+ *out = NULL;
1492
+ *len = 0;
1493
+ if (!pbw_msg_fresh(msg) ||
1494
+ (msg->desc->schema != NULL && !msg->desc->schema->linked)) {
1495
+ return PBW_ERR_INVALID;
1496
+ }
1497
+ pbw_buf_init(&buf);
1498
+ {
1499
+ /* Load-time hint first: a decoded tree re-encodes to about its wire
1500
+ * length, so no walk is needed (exact for untouched trees; the
1501
+ * writer grows if it was mutated, hence no invalidation).
1502
+ * Hand-built trees start from a small fixed buffer and grow on
1503
+ * demand: walking the tree to size it costs more than one or two
1504
+ * doublings for small messages, and large ones only pay a few
1505
+ * realloc copies (measured 2026-11). 256 stays in the fast tiny
1506
+ * allocation class on common allocators, so small messages avoid
1507
+ * both the sizing walk and a slow large malloc. No shared state is
1508
+ * touched, so concurrent encodes of one message stay independent. */
1509
+ size_t initial;
1510
+
1511
+ if (msg->wire_len > 0 && msg->wire_len <= SIZE_MAX - 32) {
1512
+ initial = msg->wire_len + 32;
1513
+ } else {
1514
+ /* Tried 512/1024 here (2026-11): large x100 saved ~230 ns by
1515
+ * skipping two doublings, but small x0/x1 lost 6-8 ns to the
1516
+ * bigger malloc and flipped below gen. Keep 256: the plain path
1517
+ * stays within noise on small tiers, ctx is the stable fast
1518
+ * path. Do not retry without a size hint that costs <10 ns.
1519
+ * Tried removing the per-write capacity checks instead (2026-11):
1520
+ * a 15-field hand-built message saved 1.6 ns (58.1 -> 56.5),
1521
+ * the checks already hit the fast path and the per-field dispatch
1522
+ * dominates. A cursor-hoisted rewrite like the struct codec's
1523
+ * tail is not worth it here; the malloc is the floor. */
1524
+ initial = 256;
1525
+ }
1526
+ if (!pbw_buf_reserve(&buf, initial)) {
1527
+ return PBW_ERR_NOMEM;
1528
+ }
1529
+ }
1530
+ w_init(&w, &buf);
1531
+ encode_message(&w, msg, 0, det);
1532
+ if (w.status != PBW_OK) {
1533
+ pbw_buf_free(&buf);
1534
+ return w.status;
1535
+ }
1536
+ if (buf.data == NULL) {
1537
+ /* Empty message: hand out a valid buffer the caller can free. */
1538
+ buf.data = (uint8_t *)malloc(1);
1539
+ if (buf.data == NULL) {
1540
+ return PBW_ERR_NOMEM;
1541
+ }
1542
+ }
1543
+ /* Transfer the buffer instead of copying: it is already at least the size
1544
+ * the message needed. */
1545
+ *out = buf.data;
1546
+ *len = w.pos;
1547
+ return PBW_OK;
1548
+ }
1549
+
1550
+ pbw_status
1551
+ pbw_encode(const pbw_message *msg, uint8_t **out, size_t *len)
1552
+ {
1553
+ return encode_alloc(msg, 0, out, len);
1554
+ }
1555
+
1556
+ pbw_status
1557
+ pbw_encode_deterministic(const pbw_message *msg, uint8_t **out, size_t *len)
1558
+ {
1559
+ return encode_alloc(msg, 1, out, len);
1560
+ }
1561
+
1562
+ /* Shared by the two caller-buffer entry points: only the deterministic flag
1563
+ * differs, so the validation and the fixed-buffer handling live once. */
1564
+ static pbw_status
1565
+ encode_into(const pbw_message *msg, int det, void *out, size_t cap, size_t *len)
1566
+ {
1567
+ pbw_buf buf;
1568
+ writer w;
1569
+ pbw_status status;
1570
+
1571
+ if (len == NULL) {
1572
+ return PBW_ERR_INVALID;
1573
+ }
1574
+ *len = 0; /* no bytes written yet: never leave a stale length behind */
1575
+ if (!pbw_msg_fresh(msg) || (out == NULL && cap > 0) ||
1576
+ (msg->desc->schema != NULL && !msg->desc->schema->linked)) {
1577
+ /* An unlinked descriptor has no tags yet: refuse instead of writing
1578
+ * values with no field numbers. */
1579
+ return PBW_ERR_INVALID;
1580
+ }
1581
+ pbw_buf_init_fixed(&buf, out, cap);
1582
+ w_init(&w, &buf);
1583
+ encode_message(&w, msg, 0, det);
1584
+ status = w.status;
1585
+ if (status == PBW_ERR_NOMEM && buf.fixed) {
1586
+ /* A fixed buffer cannot fail to allocate: the write did not fit. */
1587
+ status = PBW_ERR_LIMIT;
1588
+ }
1589
+ if (status == PBW_OK) {
1590
+ *len = w.pos;
1591
+ }
1592
+ return status;
1593
+ }
1594
+
1595
+ pbw_status
1596
+ pbw_encode_to(const pbw_message *msg, void *out, size_t cap, size_t *len)
1597
+ {
1598
+ return encode_into(msg, 0, out, cap, len);
1599
+ }
1600
+
1601
+ pbw_status
1602
+ pbw_encode_deterministic_to(const pbw_message *msg, void *out, size_t cap,
1603
+ size_t *len)
1604
+ {
1605
+ return encode_into(msg, 1, out, cap, len);
1606
+ }