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,980 @@
1
+ #ifndef PBWIRE_INTERNAL_H
2
+ #define PBWIRE_INTERNAL_H
3
+
4
+ #include "pbwire.h"
5
+
6
+ #include <stdint.h>
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /* Limits mirrored from the protobuf reference implementations and from
12
+ * pbwire's codec: unbounded nesting is a stack-overflow vector. */
13
+ #if defined(__GNUC__) || defined(__clang__)
14
+ #define PBW_INLINE __attribute__((always_inline)) static inline
15
+ /* Branch hint: a no-op on compilers without it, so the core stays buildable
16
+ * with a plain C99 compiler. */
17
+ #define PBW_EXPECT(x, v) __builtin_expect((x), (v))
18
+ #else
19
+ #define PBW_INLINE static inline
20
+ #define PBW_EXPECT(x, v) (x)
21
+ #endif
22
+
23
+ #define PBW_MAX_FIELD_NUMBER 536870911u /* 2^29 - 1 */
24
+ #define PBW_FNUM_MAP_LIMIT 512
25
+
26
+ /* Wire types. */
27
+ #define PBW_WIRE_VARINT 0
28
+ #define PBW_WIRE_64BIT 1
29
+ #define PBW_WIRE_LEN 2
30
+ #define PBW_WIRE_START_GROUP 3
31
+ #define PBW_WIRE_END_GROUP 4
32
+ #define PBW_WIRE_32BIT 5
33
+
34
+ /* Byte distance between two pointers into the same buffer, without the
35
+ * ptrdiff_t overflow that `(size_t)(end - p)` has for multi-gigabyte
36
+ * buffers on 32-bit targets. Both pointers must be into (or one past)
37
+ * the same object; otherwise the result is meaningless but still defined. */
38
+ PBW_INLINE size_t
39
+ pbw_remaining(const uint8_t *end, const uint8_t *p)
40
+ {
41
+ uintptr_t ue = (uintptr_t)(const void *)end;
42
+ uintptr_t up = (uintptr_t)(const void *)p;
43
+
44
+ if (ue < up) {
45
+ return 0;
46
+ }
47
+ return (size_t)(ue - up);
48
+ }
49
+
50
+ /* Non-NULL empty input: `NULL + 0` is undefined even when nothing is read,
51
+ * so every entry point normalizes a NULL empty buffer to this first. */
52
+ extern const uint8_t pbw_empty_input[1];
53
+ PBW_INLINE const uint8_t *
54
+ pbw_normalize_input(const void *buf, size_t len)
55
+ {
56
+ (void)len;
57
+ if (buf != NULL) {
58
+ return (const uint8_t *)buf;
59
+ }
60
+ return pbw_empty_input;
61
+ }
62
+
63
+ /* 1 when `pos <= cap`; guards every `cap - pos` site against wrap on
64
+ * corrupted state. Corruption cannot happen through the public API, but
65
+ * a wrapped subtraction would turn into a huge "available" length and
66
+ * then into an out-of-bounds access. */
67
+ PBW_INLINE int
68
+ pbw_cap_holds(size_t cap, size_t pos)
69
+ {
70
+ return cap >= pos;
71
+ }
72
+
73
+ typedef struct {
74
+ uint8_t *data;
75
+ size_t len;
76
+ size_t cap;
77
+ int oom;
78
+ /* Caller-owned memory (pbw_encode_to): growing is impossible, so a write
79
+ * that does not fit fails the encode instead of reallocating. */
80
+ int fixed;
81
+ } pbw_buf;
82
+
83
+ /* Bump allocator. Everything a caller decodes with a pbw_ctx lives in one of
84
+ * these, so resetting a context is a pointer move and freeing it is a handful
85
+ * of free() calls instead of one per field value. */
86
+ typedef struct pbw_arena_chunk {
87
+ struct pbw_arena_chunk *next;
88
+ size_t used;
89
+ size_t cap;
90
+ } pbw_arena_chunk;
91
+
92
+ typedef struct {
93
+ pbw_arena_chunk *head;
94
+ /* Oversized single allocations (see PBW_ARENA_LARGE_THRESHOLD): one malloc
95
+ * each, freed on reset/free instead of pinning the reusable head chunk. */
96
+ pbw_arena_chunk *large;
97
+ /* Max head-chunk bytes kept across reset; 0 drops everything (plain-like
98
+ * churn, no retention). Set by pbw_ctx_set_retain_cap, defaults to
99
+ * PBW_ARENA_LARGE_THRESHOLD. */
100
+ size_t retain_cap;
101
+ size_t chunk_size;
102
+ int readonly; /* a context arena: messages decoded into it are read-only */
103
+ void *block; /* non-NULL: the arena and its first chunk share one malloc */
104
+ int has_heap; /* a heap value was stored into this arena tree: free must walk */
105
+ int no_grow; /* 1: block is caller memory. Never malloc, never free it: an
106
+ * allocation that does not fit the head chunk is a NOMEM. */
107
+ } pbw_arena;
108
+
109
+ /* Payload offsets keep every arena allocation 8-byte aligned. malloc returns
110
+ * 8-aligned memory on the 32-bit targets this runs on, but sizeof of these
111
+ * headers is 12 and 28 bytes on ILP32, so (chunk + 1) would land 4 bytes off
112
+ * and a 64-bit value slot there would fault where 8-byte alignment is
113
+ * required (arm/aarch32). Rounding up costs a few bytes per chunk and
114
+ * nothing per allocation. */
115
+ #define PBW_CHUNK_HDR ((sizeof(pbw_arena_chunk) + 7u) & ~(size_t)7u)
116
+ #define PBW_ARENA_HDR ((sizeof(pbw_arena) + 7u) & ~(size_t)7u)
117
+
118
+ /* Single allocations above this bypass the bump chunk (nginx-style large
119
+ * handling) and are freed on reset, so one outlier never pins the context.
120
+ * Matches the chunk-growth cap so steady reuse still converges. */
121
+ #define PBW_ARENA_LARGE_THRESHOLD ((size_t)1 << 20)
122
+
123
+ struct pbw_field {
124
+ char *name;
125
+ /* dotted scope + "." + name for extensions ("pkg.Ext"), NULL otherwise */
126
+ char *full_name;
127
+ char *type_name;
128
+ /* proto2 default, as written in the .proto (unescaped); NULL when unset */
129
+ char *default_value;
130
+ pbw_schema *schema;
131
+ uint32_t number;
132
+ uint32_t packed;
133
+ uint32_t oneof_index;
134
+ uint8_t is_extension;
135
+ /* Editions FeatureSet resolution (0 = inherit / not an editions file).
136
+ * presence: 1 EXPLICIT, 2 IMPLICIT, 3 LEGACY_REQUIRED (required).
137
+ * enum_open: 1 OPEN, 2 CLOSED (unknown enum numbers move to unknowns).
138
+ * msg_enc: 1 LENGTH_PREFIXED, 2 DELIMITED (group tags on the wire).
139
+ * Only the descriptor-set loader sets these; hand-built schemas stay 0
140
+ * and keep the dialect-wide behavior. */
141
+ uint8_t presence;
142
+ uint8_t enum_open;
143
+ uint8_t msg_enc;
144
+ /* 1 = the field is a proto3/editions string with utf8_validation VERIFY:
145
+ * a decoded value is rejected when it is not valid UTF-8, like the
146
+ * reference parsers. 0 for proto2 strings and every non-string field. */
147
+ uint8_t utf8_check;
148
+ pbw_type type;
149
+ pbw_label label;
150
+ pbw_descriptor *sub;
151
+ /* Wire bytes precomputed by pbw_schema_link(): the tag the field is encoded
152
+ * with, the tag of its packed form, and its expected wire type. Encoding
153
+ * and decoding then never format a tag. */
154
+ uint8_t tag[5];
155
+ uint8_t len_tag[5];
156
+ /* The first four tag bytes as one word, for a single-store tag write. */
157
+ uint32_t tag_word;
158
+ uint32_t len_tag_word;
159
+ uint8_t tag_len;
160
+ uint8_t len_tag_len;
161
+ uint8_t wire_type;
162
+ /* Link-time decode action (see PBW_ACT_*): the shape-specific fast path
163
+ * decode_fields() runs for this field, or PBW_ACT_GENERIC when the
164
+ * generic field logic must run (oneof members, groups, closed enums).
165
+ * Computed for every field regardless of tag size, so one- and
166
+ * multi-byte tags share a single action switch. */
167
+ uint8_t dec_action;
168
+ };
169
+
170
+ struct pbw_enum_value {
171
+ char *name;
172
+ int32_t number;
173
+ };
174
+
175
+ /* One rpc method: resolved names only, no transport. Streaming flags are
176
+ * plain ints (0/1) so bindings read them without an enum. */
177
+ struct pbw_method {
178
+ char *name;
179
+ char *input_type; /* fully qualified, no leading dot */
180
+ char *output_type; /* fully qualified, no leading dot */
181
+ int client_streaming;
182
+ int server_streaming;
183
+ };
184
+
185
+ /* One service: the ordered method list from ServiceDescriptorProto. */
186
+ struct pbw_service {
187
+ char *name; /* fully qualified, no leading dot ("pkg.Service") */
188
+ pbw_method *methods;
189
+ size_t method_count;
190
+ pbw_schema *schema;
191
+ };
192
+
193
+ struct pbw_descriptor {
194
+ char *name;
195
+ int is_enum;
196
+ /* proto3/editions semantics: open enums, no required enforcement */
197
+ int proto3;
198
+ /* MessageSet wire format (MessageOptions.message_set_wire_format):
199
+ * group items decode into their extension fields instead of staying
200
+ * unknown. Only the descriptor-set loader sets this. */
201
+ int is_messageset;
202
+ /* Editions file (FileDescriptorProto.edition value, e.g. 1000 for
203
+ * EDITION_2023); 0 for syntax proto2/proto3 files and hand-built
204
+ * schemas. Field FeatureSet overrides only exist when this is set. */
205
+ int edition;
206
+ pbw_schema *schema;
207
+ /* Layout epoch: bumped by add_field() and by every link pass (even a
208
+ * failed one, which sorts before it validates). A message captures it
209
+ * at creation; a mismatch means the slots moved, so field access
210
+ * fails instead of reading the wrong slot. Field pointers must not
211
+ * be cached across an add or a link for the same reason. */
212
+ uint32_t epoch;
213
+ size_t field_count;
214
+ /* Rows allocated in `fields`, grown geometrically by add_field(): a
215
+ * descriptor set with thousands of fields in one message would otherwise
216
+ * pay a memmove per field (quadratic in the field count). */
217
+ size_t fields_cap;
218
+ pbw_field *fields; /* sorted by number after link */
219
+ /* Add-time duplicate index: open-addressed tables keyed by number and by
220
+ * name (values are field index + 1, 0 = empty), power-of-two sized. Without
221
+ * it pbw_message_add_field() compares against every earlier field, which is
222
+ * quadratic: 8000 fields cost 64 ms of scanning for 0.3 ms of work. Names
223
+ * index only non-extension rows (extensions may share a name by design).
224
+ * Growth is keyed on the total row count, because the number table holds
225
+ * every row: keying it on the name count would leave a message whose fields
226
+ * are all extensions with a full table and a probe that never finds a free
227
+ * slot. Dropped when an allocation fails, which falls back to the scan. */
228
+ uint32_t *add_nums;
229
+ uint32_t *add_names;
230
+ size_t add_size;
231
+ size_t enum_count;
232
+ struct pbw_enum_value *enum_values;
233
+ uint32_t oneof_count;
234
+ pbw_field **fnum_map; /* field number -> field, NULL when sparse */
235
+ uint32_t fnum_map_size;
236
+ /* reserved names from the descriptor (DescriptorProto.reserved_name) */
237
+ char **reserved;
238
+ size_t reserved_count;
239
+ /* map entry (MessageOptions.map_entry): JSON prints it as an object */
240
+ int map_entry;
241
+ /* set by pbw_schema_link(): some field is a proto2 closed enum, so the
242
+ * decoder must detour unknown enum numbers into the unknown fields */
243
+ int has_closed_enum;
244
+ /* set by pbw_schema_link(): this message or a reachable submessage
245
+ * declares a proto2 required field, so a decode has to walk the tree
246
+ * once to validate it. Pure-proto3 subtrees stay 0 and are skipped. */
247
+ int has_required;
248
+ /* Fast tag dispatch: tag_map[byte] is the field whose one-byte tag (or
249
+ * one-byte packed-form tag) equals `byte`, NULL when no field matches.
250
+ * Multibyte tags still use fnum_map. NULL when no field has a one-byte
251
+ * tag. The decode action lives on the field itself (see dec_action),
252
+ * so this table is lookup-only. */
253
+ pbw_field **tag_map;
254
+ /* Fast name dispatch for table/dict bindings: open-addressed hash of
255
+ * field name -> field, NULL when the message has no fields. Lookup is
256
+ * one hash plus probes instead of a linear strcmp scan. */
257
+ const pbw_field **name_map;
258
+ size_t name_map_size; /* power of two, 0 when empty */
259
+ };
260
+
261
+ struct pbw_schema {
262
+ pbw_descriptor **descs;
263
+ size_t count;
264
+ size_t cap;
265
+ /* Open-addressed name index over descs (power of two), so
266
+ * pbw_schema_find() is O(1): loading a FileDescriptorSet resolves one
267
+ * name per message/enum field and per extension, and a linear scan made
268
+ * that quadratic in the schema size. NULL when the index could not be
269
+ * built (out of memory), which falls back to the scan. */
270
+ pbw_descriptor **name_index;
271
+ size_t name_index_size;
272
+ size_t name_index_count;
273
+ struct pbw_service **services;
274
+ size_t service_count;
275
+ size_t service_cap;
276
+ /* Edition of the file currently being loaded (0 = syntax proto2/proto3);
277
+ * descriptors copy it at creation, like the proto3 dialect below. */
278
+ int edition;
279
+ int linked;
280
+ /* Set once a link has succeeded: field counts can no longer change, so a
281
+ * message's value slots always match its descriptor. */
282
+ int frozen;
283
+ int proto3; /* dialect handed to descriptors created from now on */
284
+ char error[192];
285
+ };
286
+
287
+ /* pbw_value.flags: the struct or its payload was allocated from an arena or
288
+ * lives in a message slot, so it must never be free()d on its own. */
289
+ #define PBW_VF_EMBEDDED 1u /* the struct is a slot inside its message */
290
+ #define PBW_VF_ARENA 2u /* allocated from a pbw_ctx arena */
291
+
292
+ struct pbw_value {
293
+ pbw_value_kind kind;
294
+ uint32_t flags;
295
+ union {
296
+ uint64_t u64;
297
+ struct {
298
+ uint8_t *data;
299
+ size_t len;
300
+ } bytes;
301
+ pbw_message *msg;
302
+ struct {
303
+ pbw_value **items;
304
+ uint32_t len; /* a list can never hold 2^32 items in memory */
305
+ uint32_t cap;
306
+ } list;
307
+ } as;
308
+ };
309
+
310
+ struct pbw_message {
311
+ const pbw_descriptor *desc;
312
+ pbw_arena *arena; /* non-NULL: the tree is allocated from an arena */
313
+ pbw_arena *own; /* non-NULL: this message frees that arena */
314
+ int stored; /* 1: owned by another message's slot or list */
315
+ pbw_value *values; /* nfields inline slots, see `present` */
316
+ uint32_t epoch; /* desc->epoch at creation: mismatch = stale */
317
+ uint32_t *present; /* field_count bits: the slot holds a value */
318
+ uint32_t *oneof_case; /* 1-based oneof index -> field number, 0 = unset */
319
+ size_t nfields; /* desc->field_count at creation: free needs no desc */
320
+ uint8_t *unknown;
321
+ size_t unknown_len;
322
+ size_t unknown_cap;
323
+ /* Wire bytes this tree was loaded from (root only, 0 = unknown).
324
+ * encode_alloc() uses it as the initial buffer guess: exact for an
325
+ * untouched tree, and merely a guess otherwise (the writer grows on
326
+ * demand), so it needs no invalidation on mutation. Set at decode. */
327
+ size_t wire_len;
328
+ };
329
+
330
+ /* Link-time decode actions, one per field (see dec_action). Only shapes
331
+ * whose semantics fit the inline fast path get one: oneof members, groups
332
+ * and closed enums keep PBW_ACT_GENERIC and run the generic field logic.
333
+ * The packed (length-delimited) form of a repeated packable field is
334
+ * detected by its wire type at decode time, so it needs no action of its
335
+ * own: every repeated action below describes the unpacked form. */
336
+ enum {
337
+ PBW_ACT_GENERIC = 0,
338
+ PBW_ACT_I32, /* singular varint, sign-extended from 32 bits */
339
+ PBW_ACT_I64, /* singular varint raw (int64, uint64) */
340
+ PBW_ACT_U32, /* singular varint, masked to 32 bits (uint32) */
341
+ PBW_ACT_S32, /* singular varint, zigzag-decoded to 32 bits */
342
+ PBW_ACT_S64, /* singular varint, zigzag-decoded to 64 bits */
343
+ PBW_ACT_BOOL, /* singular varint, normalized to 0/1 (bool) */
344
+ PBW_ACT_F32, /* singular 4 little-endian bytes */
345
+ PBW_ACT_F64, /* singular 8 little-endian bytes */
346
+ PBW_ACT_STR, /* singular string/bytes, first or later occurrence */
347
+ PBW_ACT_MSG, /* singular message, merge or replace */
348
+ PBW_ACT_REP_VARINT, /* repeated unpacked varint scalar */
349
+ PBW_ACT_REP_F32, /* repeated unpacked 4-byte scalar */
350
+ PBW_ACT_REP_F64, /* repeated unpacked 8-byte scalar */
351
+ PBW_ACT_REP_STR, /* repeated string/bytes */
352
+ PBW_ACT_REP_MSG /* repeated message */
353
+ };
354
+
355
+ /* 1 when the message's slots still match its descriptor's layout: same
356
+ * field order and count as at creation. An add or a link (even a failed
357
+ * one, which sorts before it validates) bumps the descriptor epoch and
358
+ * retires every message built from it. */
359
+ static inline int
360
+ pbw_msg_fresh(const pbw_message *msg)
361
+ {
362
+ return msg != NULL && msg->desc != NULL && !msg->desc->is_enum &&
363
+ msg->epoch == msg->desc->epoch;
364
+ }
365
+
366
+ /* 1 when a bytes/string payload of `value` can be read: a zero length may
367
+ * carry NULL, a longer payload must have a pointer. The constructors and
368
+ * decode guarantee it, and pbw_message_put() refuses the shape, so only a
369
+ * slot poked by hand can hold it; no reader may touch that. */
370
+ PBW_INLINE int
371
+ pbw_bytes_readable(const pbw_value *value)
372
+ {
373
+ return value->as.bytes.len == 0 || value->as.bytes.data != NULL;
374
+ }
375
+
376
+ /* A message keeps its values inline: one allocation holds the struct, the value
377
+ * slots, the presence bitmap and the oneof cases. */
378
+ static inline int
379
+ pbw_msg_present(const pbw_message *msg, size_t index)
380
+ {
381
+ return (int)((msg->present[index >> 5] >> (index & 31u)) & 1u);
382
+ }
383
+
384
+ static inline void
385
+ pbw_msg_set_present(pbw_message *msg, size_t index, int on)
386
+ {
387
+ uint32_t bit = 1u << (index & 31u);
388
+
389
+ if (on) {
390
+ msg->present[index >> 5] |= bit;
391
+ } else {
392
+ msg->present[index >> 5] &= ~bit;
393
+ }
394
+ }
395
+
396
+ /* Appends raw wire bytes to msg->unknown; PBW_OK or PBW_ERR_NOMEM. */
397
+ int pbw_message_add_unknown(pbw_message *msg, const uint8_t *data, size_t len);
398
+
399
+ /* wire.c */
400
+ static inline size_t
401
+ pbw_varint_size(uint64_t value)
402
+ {
403
+ size_t size = 1;
404
+ while (value >= 0x80) {
405
+ value >>= 7;
406
+ size++;
407
+ }
408
+ return size;
409
+ }
410
+ size_t pbw_varint_write(uint8_t *out, uint64_t value);
411
+ /* Decimal conversions for the JSON and text formats. The wire format defines
412
+ * '.' as the decimal separator, full stop: these never consult the host
413
+ * locale (no localeconv, no locale-sensitive strtod/printf), so output is
414
+ * byte-identical under any LC_NUMERIC and no concurrent setlocale can skew
415
+ * a parse. The first 25 significant digits accumulate into a (hi, lo)
416
+ * pair (error ~2^-100 relative) scaled by exact x10 steps, so ordinary
417
+ * inputs round exactly once, like strtod; saturation (inf/zero) is decided
418
+ * by integer decimal-exponent math, so DBL_MAX/DBL_MIN parse exactly.
419
+ * shortcut: a decimal landing exactly halfway between two doubles may round
420
+ * to either neighbor (the pair cannot see which side the true value is on).
421
+ * Differential fuzzing against strtod shows only such 1-ulp razor cases and
422
+ * no systematic drift; exact-halfway needs bignum the core does not buy.
423
+ * The differential and conformance suites pin the precision. */
424
+ double pbw_strtod_c(const char *text, char **end);
425
+ int pbw_format_g_c(char *buf, size_t cap, int precision, double value);
426
+ int pbw_varint_read(const uint8_t *p, const uint8_t *end, uint64_t *value,
427
+ size_t *used);
428
+ /* Reads a field tag. A tag is a varint32: at most 5 bytes, and the value is
429
+ * truncated to 32 bits exactly like the reference implementations. */
430
+ int pbw_tag_read(const uint8_t *p, const uint8_t *end, uint32_t *tag,
431
+ size_t *used);
432
+ /* Reads a length prefix: a strict varint32, unlike pbw_varint_read(). */
433
+ int pbw_len_read(const uint8_t *p, const uint8_t *end, size_t *len,
434
+ size_t *used);
435
+ /* Structural UTF-8 check: rejects overlong forms, surrogates and values past
436
+ * U+10FFFF. Shared by the string parser, the text/JSON parsers and decode(). */
437
+ int pbw_utf8_valid(const uint8_t *data, size_t len);
438
+
439
+ /* UTF-8 rules for one multi-byte sequence: returns the bytes it consumes, or 0
440
+ * when it is malformed or truncated. The one definition behind both callers
441
+ * below, so the rules are not spelled out three times. */
442
+ PBW_INLINE int
443
+ pbw_utf8_seq(const uint8_t *d, size_t avail)
444
+ {
445
+ uint8_t c = d[0];
446
+
447
+ if ((c & 0xe0) == 0xc0) {
448
+ if (c < 0xc2 || avail < 2 || (d[1] & 0xc0) != 0x80) {
449
+ return 0;
450
+ }
451
+ return 2;
452
+ }
453
+ if ((c & 0xf0) == 0xe0) {
454
+ uint32_t v;
455
+
456
+ if (avail < 3 || (d[1] & 0xc0) != 0x80 || (d[2] & 0xc0) != 0x80) {
457
+ return 0;
458
+ }
459
+ v = ((uint32_t)(c & 15) << 12) | ((uint32_t)(d[1] & 63) << 6) |
460
+ (uint32_t)(d[2] & 63);
461
+ if (v < 0x800 || (v >= 0xd800 && v <= 0xdfff)) {
462
+ return 0;
463
+ }
464
+ return 3;
465
+ }
466
+ if ((c & 0xf8) == 0xf0) {
467
+ uint32_t v;
468
+
469
+ if (c > 0xf4 || avail < 4 || (d[1] & 0xc0) != 0x80 ||
470
+ (d[2] & 0xc0) != 0x80 || (d[3] & 0xc0) != 0x80) {
471
+ return 0;
472
+ }
473
+ v = ((uint32_t)(c & 7) << 18) | ((uint32_t)(d[1] & 63) << 12) |
474
+ ((uint32_t)(d[2] & 63) << 6) | (uint32_t)(d[3] & 63);
475
+ if (v < 0x10000 || v > 0x10ffff) {
476
+ return 0;
477
+ }
478
+ return 4;
479
+ }
480
+ return 0; /* a continuation byte, or a 5/6-byte form */
481
+ }
482
+
483
+ /* The check the decoder runs per decoded string. Most strings are shorter than
484
+ * a machine word (a name, a tag, an enum label), so they are validated here
485
+ * without a call; longer ones go to pbw_utf8_valid's word-at-a-time scan,
486
+ * which is what keeps a multi-KB value from paying a byte-per-iteration walk. */
487
+ #define PBW_UTF8_SHORT 8
488
+ PBW_INLINE int
489
+ pbw_utf8_ok(const uint8_t *data, size_t len)
490
+ {
491
+ size_t i;
492
+
493
+ if (len >= PBW_UTF8_SHORT) {
494
+ return pbw_utf8_valid(data, len);
495
+ }
496
+ for (i = 0; i < len; i++) {
497
+ int n;
498
+
499
+ if (data[i] < 0x80) {
500
+ continue;
501
+ }
502
+ n = pbw_utf8_seq(data + i, len - i);
503
+ if (n == 0) {
504
+ return 0;
505
+ }
506
+ i += (size_t)(n - 1);
507
+ }
508
+ return 1;
509
+ }
510
+ void pbw_buf_init(pbw_buf *buf);
511
+ /* Wrap caller memory: reserve never reallocates, it fails once the buffer is
512
+ * full. The memory stays the caller's. */
513
+ void pbw_buf_init_fixed(pbw_buf *buf, void *data, size_t cap);
514
+ void pbw_buf_free(pbw_buf *buf);
515
+ /* Grows buf so that `extra` more bytes fit; capacity survives across calls. */
516
+ int pbw_buf_reserve(pbw_buf *buf, size_t extra);
517
+ uint32_t pbw_read_le32(const uint8_t *in);
518
+
519
+ /* arena.c */
520
+ void *pbw_arena_alloc_slow(pbw_arena *arena, size_t size);
521
+
522
+ static inline void *
523
+ pbw_arena_alloc(pbw_arena *arena, size_t size)
524
+ {
525
+ /* Sizes near SIZE_MAX would wrap the align_up below into a tiny request:
526
+ * bounce them to the slow path, which rejects what it cannot hold. */
527
+ pbw_arena_chunk *chunk;
528
+ void *ptr;
529
+
530
+ if (arena == NULL) {
531
+ return NULL;
532
+ }
533
+ chunk = arena->head;
534
+ if (PBW_EXPECT(size > SIZE_MAX - (size_t)7u, 0)) {
535
+ return pbw_arena_alloc_slow(arena, size);
536
+ }
537
+ size = (size + 7u) & ~(size_t)7u;
538
+ if (chunk == NULL || !pbw_cap_holds(chunk->cap, chunk->used) ||
539
+ chunk->cap - chunk->used < size) {
540
+ return pbw_arena_alloc_slow(arena, size);
541
+ }
542
+ ptr = (char *)chunk + PBW_CHUNK_HDR + chunk->used;
543
+ chunk->used += size;
544
+ return ptr;
545
+ }
546
+ /* Allocates an arena whose struct, first chunk and chunk header are one malloc,
547
+ * for a tree that is decoded and freed as a whole. */
548
+ pbw_arena *pbw_arena_new_packed(size_t first_chunk);
549
+ /* Same layout on caller memory: the struct and its only chunk live in mem, so
550
+ * the arena never allocates and never frees. mem must be 8-byte aligned and
551
+ * len must cover PBW_CHUNK_HDR plus room for one allocation. 0 = too small. */
552
+ int pbw_arena_init_mem(pbw_arena *arena, void *mem, size_t len);
553
+ void pbw_arena_reset(pbw_arena *arena);
554
+ void pbw_arena_free(pbw_arena *arena);
555
+ size_t pbw_arena_used(const pbw_arena *arena);
556
+ /* 1 when ptr points into live arena memory (below `used` in a head chunk
557
+ * or inside an oversized single allocation). Used at the store boundary so
558
+ * a value from one arena cannot be stored into a message of another: the
559
+ * source reset would free it under the tree. */
560
+ static inline int
561
+ pbw_arena_owns(const pbw_arena *arena, const void *ptr)
562
+ {
563
+ const pbw_arena_chunk *chunk;
564
+
565
+ if (arena == NULL || ptr == NULL) {
566
+ return ptr == NULL;
567
+ }
568
+ for (chunk = arena->head; chunk != NULL; chunk = chunk->next) {
569
+ const uint8_t *base = (const uint8_t *)chunk + PBW_CHUNK_HDR;
570
+
571
+ if ((const uint8_t *)ptr >= base &&
572
+ (const uint8_t *)ptr < base + chunk->used) {
573
+ return 1;
574
+ }
575
+ }
576
+ for (chunk = arena->large; chunk != NULL; chunk = chunk->next) {
577
+ const uint8_t *base = (const uint8_t *)chunk + PBW_CHUNK_HDR;
578
+
579
+ if ((const uint8_t *)ptr >= base &&
580
+ (const uint8_t *)ptr < base + chunk->used) {
581
+ return 1;
582
+ }
583
+ }
584
+ return 0;
585
+ }
586
+
587
+ /* decode.c / encode.c */
588
+ /* Same with an explicit nesting cap (PBW_MAX_DEPTH when limits are default). */
589
+ pbw_status pbw_decode_arena_lim(pbw_arena *arena, const pbw_descriptor *desc,
590
+ const void *buf, size_t len, uint32_t dlim,
591
+ pbw_message **out);
592
+ /* Effective nesting cap: 0 or an over-large value means the default. */
593
+ PBW_INLINE uint32_t
594
+ pbw_effective_depth(const pbw_limits *limits)
595
+ {
596
+ if (limits == NULL || limits->max_depth == 0 ||
597
+ limits->max_depth > PBW_MAX_DEPTH) {
598
+ return PBW_MAX_DEPTH;
599
+ }
600
+ return limits->max_depth;
601
+ }
602
+ pbw_status pbw_encode_buf(pbw_buf *buf, const pbw_message *msg);
603
+
604
+ /* schema.c */
605
+ /* Hot path of pbw_descriptor_find_field(): inlined on purpose, it runs once per
606
+ * field while decoding. */
607
+ static inline const pbw_field *
608
+ pbw_find_field(const pbw_descriptor *desc, uint32_t number)
609
+ {
610
+ size_t lo;
611
+ size_t hi;
612
+
613
+ if (desc->fnum_map != NULL) {
614
+ if (number < desc->fnum_map_size) {
615
+ return desc->fnum_map[number];
616
+ }
617
+ return NULL;
618
+ }
619
+ lo = 0;
620
+ hi = desc->field_count;
621
+ while (lo < hi) {
622
+ size_t mid = lo + (hi - lo) / 2;
623
+ if (desc->fields[mid].number < number) {
624
+ lo = mid + 1;
625
+ } else {
626
+ hi = mid;
627
+ }
628
+ }
629
+ if (lo < desc->field_count && desc->fields[lo].number == number) {
630
+ return &desc->fields[lo];
631
+ }
632
+ return NULL;
633
+ }
634
+
635
+ void pbw_descriptor_free(pbw_descriptor *desc);
636
+ /* 1 when `name` is a reserved field name of this message. */
637
+ int pbw_descriptor_is_reserved(const pbw_descriptor *desc, const char *name);
638
+ /* Finds an extension field by its full name ("pkg.Ext"); hand-built
639
+ * extensions without a full name match by their short name. */
640
+ const pbw_field *pbw_schema_find_extension(const pbw_schema *schema,
641
+ const char *name);
642
+
643
+ /* message.c */
644
+ /* Fills value with a NUL-terminated copy of data; 0 on allocation failure. */
645
+ int pbw_value_init_bytes(pbw_value *value, pbw_arena *arena, const void *data,
646
+ size_t len);
647
+ /* Copies value into the field slot and marks it present (decoder storage). */
648
+ void pbw_message_put(pbw_message *msg, size_t index, const pbw_value *value);
649
+ pbw_value *pbw_message_list_for(pbw_message *msg, const pbw_field *field);
650
+ /* Grows list capacity to `cap` without adding items, for inputs whose item
651
+ * count is known upfront (packed fixed-width fields). Mirrors the growth
652
+ * policy of pbw_value_list_push; 1 on success (or when already big enough). */
653
+ int pbw_value_list_reserve(pbw_value *list, uint32_t cap, pbw_arena *arena);
654
+ /* Frees what a value points at (bytes, nested message, list items) but not the
655
+ * pbw_value struct itself. */
656
+ void pbw_value_destroy(pbw_value *value);
657
+ /* Stores a caller-owned value into a field and takes ownership of it. */
658
+ int pbw_message_store(pbw_message *msg, const pbw_field *field,
659
+ pbw_value *value);
660
+
661
+ /* encode.c / decode.c */
662
+ int pbw_is_packable(pbw_type type);
663
+ uint32_t pbw_expected_wire_type(pbw_type type);
664
+
665
+ /* A MessageSet item on the encode path: a singular message extension of a
666
+ * message_set_wire_format parent travels as one group item, not as its own
667
+ * field number. */
668
+ static inline int
669
+ pbw_is_messageset_item(const pbw_field *field, const pbw_message *msg)
670
+ {
671
+ return msg->desc->is_messageset && field->is_extension &&
672
+ field->type == PBW_TYPE_MESSAGE &&
673
+ field->label != PBW_LABEL_REPEATED;
674
+ }
675
+
676
+ /* A proto2 (closed) enum rejects unknown numbers: the reference parsers move
677
+ * them into the unknown fields instead of the field. Editions may close one
678
+ * enum field (or one enum type) in an otherwise open file; the per-field
679
+ * override wins, otherwise the enum type's dialect decides. */
680
+ static inline int
681
+ pbw_enum_is_closed(const pbw_field *field)
682
+ {
683
+ if (field->enum_open == 2) {
684
+ return 1;
685
+ }
686
+ if (field->enum_open == 1) {
687
+ return 0;
688
+ }
689
+ return field->type == PBW_TYPE_ENUM && field->sub != NULL &&
690
+ !field->sub->proto3;
691
+ }
692
+
693
+ /* A field whose absence fails decoding (PBW_ERR_REQUIRED). Proto2 spells it
694
+ * with the REQUIRED label; editions spell it with field_presence =
695
+ * LEGACY_REQUIRED (the label stays OPTIONAL there, by spec). */
696
+ static inline int
697
+ pbw_field_is_required(const pbw_field *field, const pbw_descriptor *msg)
698
+ {
699
+ if (field->label == PBW_LABEL_REQUIRED && !msg->proto3) {
700
+ return 1;
701
+ }
702
+ return msg->edition != 0 && field->presence == 3 &&
703
+ field->label != PBW_LABEL_REPEATED;
704
+ }
705
+
706
+ /* A plain proto3 field with neither a oneof nor explicit presence: the
707
+ * JSON/text printers omit it when it holds the default value, like the
708
+ * reference printers. `presence` 1 is EXPLICIT (editions, proto3 `optional`),
709
+ * 3 is LEGACY_REQUIRED. */
710
+ static inline int
711
+ pbw_field_implicit_presence(const pbw_field *field, const pbw_descriptor *msg)
712
+ {
713
+ if (!msg->proto3 || field->oneof_index != 0) {
714
+ return 0;
715
+ }
716
+ return field->presence != 1 && field->presence != 3;
717
+ }
718
+
719
+ /* The default value of a present scalar, as the proto3 printers define it:
720
+ * an empty string/bytes, or all-zero bits (`-0.0` has a non-zero sign bit and
721
+ * prints, exactly like the reference). Messages and lists are never default. */
722
+ static inline int
723
+ pbw_value_is_default(const pbw_value *value)
724
+ {
725
+ if (value->kind == PBW_VALUE_BYTES) {
726
+ return value->as.bytes.len == 0;
727
+ }
728
+ if (value->kind == PBW_VALUE_UINT) {
729
+ return value->as.u64 == 0;
730
+ }
731
+ return 0;
732
+ }
733
+
734
+ /* The tree builders below are inline: the decoder runs one per decoded value
735
+ * (or nested message), and the call alone is a measurable slice of a decode.
736
+ * One definition, no copy per caller; a translation unit that never calls one
737
+ * emits nothing for it. They sit after arena.c because they allocate. */
738
+
739
+ /* A message is one allocation: the struct, its value slots, the presence bitmap
740
+ * and the oneof cases follow each other in the same block. Only the bitmap and
741
+ * the oneof cases are zeroed; a value slot is written before it is marked
742
+ * present. */
743
+ PBW_INLINE pbw_message *
744
+ pbw_message_new_a(pbw_arena *arena, const pbw_descriptor *desc)
745
+ {
746
+ pbw_message *msg;
747
+ size_t values_off;
748
+ size_t present_off;
749
+ size_t oneof_off;
750
+ size_t size;
751
+ size_t words;
752
+ uint32_t oneof_max;
753
+ uint8_t *block;
754
+
755
+ if (desc == NULL || desc->is_enum) {
756
+ return NULL;
757
+ }
758
+ /* oneof_count is computed by the link; an unlinked descriptor still
759
+ * builds messages (pbw_message_set_field with a field pointer is how the
760
+ * printers take one), so size the case table from the fields themselves
761
+ * there: a oneof member would otherwise index past the block. */
762
+ oneof_max = desc->oneof_count;
763
+ if (desc->schema == NULL || !desc->schema->linked) {
764
+ size_t k;
765
+
766
+ oneof_max = 0;
767
+ for (k = 0; k < desc->field_count; k++) {
768
+ if (desc->fields[k].oneof_index > oneof_max) {
769
+ oneof_max = desc->fields[k].oneof_index;
770
+ }
771
+ }
772
+ /* A oneof index the link would reject must not size the case
773
+ * table here: on 32-bit the size math wraps and the table
774
+ * writes out of bounds. Link enforces the same bound. */
775
+ if (oneof_max > desc->field_count + 1) {
776
+ return NULL;
777
+ }
778
+ }
779
+ if (desc->field_count > (SIZE_MAX - sizeof(pbw_message)) / sizeof(pbw_value)) {
780
+ return NULL;
781
+ }
782
+ /* sizeof(pbw_message) is 44 bytes on ILP32, so a slot array that simply
783
+ * follows it would put every 64-bit value 4 bytes off alignment where
784
+ * 8-byte alignment is required (arm/aarch32). Round up: free on LP64. */
785
+ values_off = (sizeof(pbw_message) + 7u) & ~(size_t)7u;
786
+ present_off = values_off + desc->field_count * sizeof(pbw_value);
787
+ words = (desc->field_count + 31u) / 32u;
788
+ /* The bitmap plus alignment slack must fit past present_off: words * 4
789
+ * cannot wrap on its own (field_count is bounded above), so this
790
+ * subtraction sequence is well-defined and rejects the wrap. */
791
+ if (present_off > SIZE_MAX - words * sizeof(uint32_t) - (size_t)3) {
792
+ return NULL;
793
+ }
794
+ oneof_off = (present_off + words * sizeof(uint32_t) + 3u) & ~(size_t)3u;
795
+ if ((size_t)oneof_max + 1u >
796
+ (SIZE_MAX - oneof_off) / sizeof(uint32_t)) {
797
+ return NULL;
798
+ }
799
+ size = oneof_off + ((size_t)oneof_max + 1u) * sizeof(uint32_t);
800
+
801
+ if (arena != NULL) {
802
+ block = (uint8_t *)pbw_arena_alloc(arena, size);
803
+ } else {
804
+ block = (uint8_t *)malloc(size);
805
+ }
806
+ if (block == NULL) {
807
+ return NULL;
808
+ }
809
+ /* Every member is assigned below: the struct needs no blanket zeroing,
810
+ * only the presence bitmap and the oneof cases start cleared. */
811
+ msg = (pbw_message *)block;
812
+ msg->desc = desc;
813
+ msg->epoch = desc->epoch;
814
+ msg->arena = arena;
815
+ msg->own = NULL;
816
+ msg->stored = 0;
817
+ msg->nfields = desc->field_count;
818
+ msg->values = (pbw_value *)(void *)(block + values_off);
819
+ msg->present = (uint32_t *)(void *)(block + present_off);
820
+ msg->oneof_case = (uint32_t *)(void *)(block + oneof_off);
821
+ msg->unknown = NULL;
822
+ msg->unknown_len = 0;
823
+ msg->unknown_cap = 0;
824
+ msg->wire_len = 0;
825
+ if (words > 0) {
826
+ memset(msg->present, 0, words * sizeof(uint32_t));
827
+ }
828
+ if (oneof_max > 0) {
829
+ memset(msg->oneof_case, 0,
830
+ ((size_t)oneof_max + 1u) * sizeof(uint32_t));
831
+ }
832
+ return msg;
833
+ }
834
+
835
+ PBW_INLINE pbw_value *
836
+ pbw_value_new_a(pbw_arena *arena, pbw_value_kind kind)
837
+ {
838
+ pbw_value *value;
839
+
840
+ if (arena != NULL) {
841
+ value = (pbw_value *)pbw_arena_alloc(arena, sizeof(pbw_value));
842
+ } else {
843
+ value = (pbw_value *)malloc(sizeof(pbw_value));
844
+ }
845
+ if (value != NULL) {
846
+ value->kind = kind;
847
+ value->flags = arena != NULL ? PBW_VF_ARENA : 0;
848
+ }
849
+ return value;
850
+ }
851
+
852
+ PBW_INLINE pbw_value *
853
+ pbw_value_new_uint_a(pbw_arena *arena, uint64_t bits)
854
+ {
855
+ pbw_value *value = pbw_value_new_a(arena, PBW_VALUE_UINT);
856
+
857
+ if (value != NULL) {
858
+ value->as.u64 = bits;
859
+ }
860
+ return value;
861
+ }
862
+
863
+ PBW_INLINE pbw_value *
864
+ pbw_value_new_bytes_a(pbw_arena *arena, const void *data, size_t len)
865
+ {
866
+ pbw_value *value;
867
+ uint8_t *payload;
868
+
869
+ if (arena == NULL) {
870
+ value = pbw_value_new_a(NULL, PBW_VALUE_BYTES);
871
+ if (value == NULL || !pbw_value_init_bytes(value, NULL, data, len)) {
872
+ pbw_value_free(value);
873
+ return NULL;
874
+ }
875
+ return value;
876
+ }
877
+ /* Arena: the value struct and its payload are one bump, so a decoded
878
+ * string or bytes item costs one allocation instead of two. The payload
879
+ * is a byte string, so it needs no stronger alignment than the arena
880
+ * already provides. */
881
+ if (len > SIZE_MAX - sizeof(*value) - 8) {
882
+ return NULL;
883
+ }
884
+ value = (pbw_value *)pbw_arena_alloc(arena, sizeof(*value) + len + 1);
885
+ if (value == NULL) {
886
+ return NULL;
887
+ }
888
+ if (len > 0 && data == NULL) {
889
+ data = pbw_empty_input;
890
+ }
891
+ payload = (uint8_t *)(value + 1);
892
+ if (len > 0) {
893
+ if (data != pbw_empty_input) {
894
+ memcpy(payload, data, len);
895
+ } else {
896
+ memset(payload, 0, len);
897
+ }
898
+ }
899
+ payload[len] = '\0';
900
+ value->kind = PBW_VALUE_BYTES;
901
+ value->flags = PBW_VF_ARENA;
902
+ value->as.bytes.data = payload;
903
+ value->as.bytes.len = len;
904
+ return value;
905
+ }
906
+
907
+ PBW_INLINE pbw_value *
908
+ pbw_value_new_message_a(pbw_arena *arena, pbw_message *msg)
909
+ {
910
+ pbw_value *value = pbw_value_new_a(arena, PBW_VALUE_MESSAGE);
911
+
912
+ if (value == NULL) {
913
+ return NULL;
914
+ }
915
+ value->as.msg = msg;
916
+ return value;
917
+ }
918
+
919
+ PBW_INLINE int
920
+ pbw_value_list_push(pbw_value *list, pbw_value *item, pbw_arena *arena)
921
+ {
922
+ pbw_value **items;
923
+
924
+ if (list == NULL || item == NULL || list->kind != PBW_VALUE_LIST ||
925
+ list->as.list.len == UINT32_MAX) {
926
+ return 0;
927
+ }
928
+ if (list->as.list.items == NULL &&
929
+ (list->as.list.len > 0 || list->as.list.cap > 0)) {
930
+ /* Corrupt list (capacity or items but no array): every append would
931
+ * write through NULL. Refuse instead. */
932
+ return 0;
933
+ }
934
+ if (list->as.list.len == list->as.list.cap) {
935
+ uint32_t cap = list->as.list.cap ? list->as.list.cap * 2u : 8u;
936
+ size_t bytes;
937
+
938
+ if (cap < list->as.list.cap) {
939
+ /* Doubling overflowed: the list cannot grow any further. */
940
+ return 0;
941
+ }
942
+ bytes = (size_t)cap * sizeof(*items);
943
+ if (bytes / sizeof(*items) != cap) {
944
+ /* 32-bit size_t: the byte count wrapped. */
945
+ return 0;
946
+ }
947
+ if (arena != NULL) {
948
+ /* No realloc in an arena: move to a bigger block, the old one is
949
+ * recycled by pbw_ctx_reset(). */
950
+ items = (pbw_value **)pbw_arena_alloc(arena, bytes);
951
+ if (items == NULL) {
952
+ return 0;
953
+ }
954
+ if (list->as.list.len > 0) {
955
+ memcpy(items, list->as.list.items,
956
+ list->as.list.len * sizeof(*items));
957
+ }
958
+ } else {
959
+ items = (pbw_value **)realloc(list->as.list.items, bytes);
960
+ if (items == NULL) {
961
+ return 0;
962
+ }
963
+ }
964
+ list->as.list.items = items;
965
+ list->as.list.cap = cap;
966
+ }
967
+ /* The list owns the wrapper from here on: pbw_value_free() on an item
968
+ * handed out by pbw_value_at() is the documented no-op, and the list
969
+ * destroy path releases the wrapper itself. A message item is owned
970
+ * from here on too, so every push site (decode, set, the JSON and text
971
+ * parsers) marks it owned in this one place. */
972
+ item->flags |= PBW_VF_EMBEDDED;
973
+ if (item->kind == PBW_VALUE_MESSAGE && item->as.msg != NULL) {
974
+ item->as.msg->stored = 1;
975
+ }
976
+ list->as.list.items[list->as.list.len++] = item;
977
+ return 1;
978
+ }
979
+
980
+ #endif /* PBWIRE_INTERNAL_H */