pbwire 0.1.18 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +68 -19
- data/ext/pbwire_ext/core_ctx.c +16 -8
- data/ext/pbwire_ext/core_decode.c +30 -4
- data/ext/pbwire_ext/core_descriptor_set.c +115 -15
- data/ext/pbwire_ext/core_encode.c +122 -81
- data/ext/pbwire_ext/core_json.c +487 -123
- data/ext/pbwire_ext/core_message.c +36 -2
- data/ext/pbwire_ext/core_schema.c +69 -12
- data/ext/pbwire_ext/core_stream.c +82 -22
- data/ext/pbwire_ext/core_text.c +25 -15
- data/ext/pbwire_ext/core_wire.c +7 -3
- data/ext/pbwire_ext/core_wkt.c +3 -2
- data/ext/pbwire_ext/internal.h +32 -7
- data/ext/pbwire_ext/pbwire.h +137 -53
- data/ext/pbwire_ext/pbwire_ext.c +759 -7
- data/lib/pbwire.rb +7 -4
- metadata +16 -2
data/ext/pbwire_ext/pbwire.h
CHANGED
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
/* ABI version: bumped on every breaking change to the C ABI (removed or
|
|
36
36
|
* reordered exports, changed struct layouts visible to callers). Additive
|
|
37
37
|
* changes keep the number. The shared library SONAME carries the same
|
|
38
|
-
* number, so `libpbwire.so.
|
|
39
|
-
#define PBW_ABI_VERSION
|
|
38
|
+
* number, so `libpbwire.so.2` and `PBW_ABI_VERSION == 2` always agree. */
|
|
39
|
+
#define PBW_ABI_VERSION 2
|
|
40
40
|
|
|
41
41
|
#ifdef __cplusplus
|
|
42
42
|
extern "C" {
|
|
@@ -103,10 +103,17 @@ typedef struct pbw_method pbw_method;
|
|
|
103
103
|
/* ---------------------------------------------------------------- schema */
|
|
104
104
|
|
|
105
105
|
pbw_schema *pbw_schema_new(void);
|
|
106
|
+
/* Frees the schema and every descriptor in it. The schema must outlive every
|
|
107
|
+
* message built from it: a message keeps a raw pointer to its descriptor,
|
|
108
|
+
* so using a message after its schema is freed is use-after-free. Only
|
|
109
|
+
* pbw_message_free() stays safe after the schema is gone (it never touches
|
|
110
|
+
* the descriptor). Messages that live in a pbw_ctx arena die at
|
|
111
|
+
* pbw_ctx_reset()/pbw_ctx_free(), which must also precede the schema free. */
|
|
106
112
|
void pbw_schema_free(pbw_schema *schema);
|
|
107
113
|
|
|
108
|
-
/* Create a message
|
|
114
|
+
/* Create a message descriptor. Names must be unique within a schema; NULL on a frozen schema. */
|
|
109
115
|
pbw_descriptor *pbw_schema_add_message(pbw_schema *schema, const char *name);
|
|
116
|
+
/* Create an enum descriptor. Names must be unique within a schema; NULL on a frozen schema. */
|
|
110
117
|
pbw_descriptor *pbw_schema_add_enum(pbw_schema *schema, const char *name);
|
|
111
118
|
pbw_descriptor *pbw_schema_find(const pbw_schema *schema, const char *name);
|
|
112
119
|
|
|
@@ -153,8 +160,11 @@ void pbw_field_set_packed(pbw_field *field, int packed);
|
|
|
153
160
|
* as an object instead of an array of entries. The loader sets this from
|
|
154
161
|
* the descriptor; call it for hand-built map entries. */
|
|
155
162
|
void pbw_descriptor_set_map_entry(pbw_descriptor *msg);
|
|
156
|
-
/* 1-based oneof group index; 0 means the field is not in a oneof.
|
|
157
|
-
|
|
163
|
+
/* 1-based oneof group index; 0 means the field is not in a oneof. Set it
|
|
164
|
+
* before the first successful pbw_schema_link(): the layout freezes then, so
|
|
165
|
+
* a later set is refused with PBW_ERR_INVALID (a silent no-op would leave
|
|
166
|
+
* two members present and encode both). A NULL field is PBW_ERR_INVALID. */
|
|
167
|
+
pbw_status pbw_field_set_oneof(pbw_field *field, uint32_t oneof_index);
|
|
158
168
|
|
|
159
169
|
/* Resolve type names, sort fields by number and build lookup tables. Idempotent
|
|
160
170
|
* while no field is added; the first success freezes the field layout, so
|
|
@@ -171,7 +181,13 @@ const char *pbw_schema_error(const pbw_schema *schema);
|
|
|
171
181
|
* Descriptors are named fully qualified without the leading dot:
|
|
172
182
|
* "pkg.Outer.Inner". Field type names resolve the same way.
|
|
173
183
|
* *out is always written; on failure it holds the partially built schema so
|
|
174
|
-
* pbw_schema_error() explains why, and the caller frees it as usual.
|
|
184
|
+
* pbw_schema_error() explains why, and the caller frees it as usual.
|
|
185
|
+
* The input is capped at 1 MiB (PBW_ERR_LIMIT past it): sets are trusted
|
|
186
|
+
* input, but an unbounded load turns one oversized read into unbounded
|
|
187
|
+
* allocation. Larger trusted sets load with pbw_descriptor_set_load_limits()
|
|
188
|
+
* and an explicit max_bytes. Concurrent loads are safe (each builds its own
|
|
189
|
+
* schema); sharing one schema across threads while loading or linking is not,
|
|
190
|
+
* and the error string truncates past 512 bytes. */
|
|
175
191
|
pbw_status pbw_descriptor_set_load(const void *data, size_t len,
|
|
176
192
|
pbw_schema **out);
|
|
177
193
|
|
|
@@ -270,8 +286,10 @@ int pbw_field_is_packed(const pbw_field *field);
|
|
|
270
286
|
int pbw_field_requires_utf8_validation(const pbw_field *field);
|
|
271
287
|
/* Structural UTF-8 check: 1 when data is valid UTF-8, 0 for overlong forms,
|
|
272
288
|
* surrogates and values past U+10FFFF. The same check decode() applies to a
|
|
273
|
-
* field whose utf8_validation resolves to VERIFY
|
|
274
|
-
*
|
|
289
|
+
* field whose utf8_validation resolves to VERIFY; the JSON parser applies it
|
|
290
|
+
* to every string it accepts, and the text parser follows the same per-field
|
|
291
|
+
* rule as decode() (a proto2 string keeps its bytes). Bindings that validate
|
|
292
|
+
* on their
|
|
275
293
|
* own (a host runtime that decodes strings itself) should use this one so the
|
|
276
294
|
* rule stays single-sourced. */
|
|
277
295
|
int pbw_utf8_valid(const uint8_t *data, size_t len);
|
|
@@ -280,13 +298,20 @@ const pbw_descriptor *pbw_field_message_type(const pbw_field *field);
|
|
|
280
298
|
|
|
281
299
|
/* --------------------------------------------------------------- message */
|
|
282
300
|
|
|
283
|
-
/* A message keeps pointing at its descriptor: the schema must outlive
|
|
284
|
-
*
|
|
285
|
-
*
|
|
301
|
+
/* A message keeps pointing at its descriptor: the schema must outlive every
|
|
302
|
+
* message built from it (see pbw_schema_free), or use after the free is
|
|
303
|
+
* use-after-free. The descriptor must be linked (pbw_schema_link() returned
|
|
304
|
+
* PBW_OK), because field lookup relies on the number-sorted field array the
|
|
305
|
+
* link builds. A later add or link (even a failed one, which sorts before it
|
|
306
|
+
* validates) retires every existing message: get/set/encode and the printers
|
|
307
|
+
* then fail with PBW_ERR_INVALID (get returns NULL, clear silently ignores
|
|
308
|
+
* the retired message), so build messages only after the last link. */
|
|
286
309
|
pbw_message *pbw_message_new(const pbw_descriptor *desc);
|
|
287
310
|
/* Frees a message the caller owns and everything under it. A submessage
|
|
288
311
|
* handed out by a parent (pbw_message_set_message(), a decoded tree) is owned
|
|
289
|
-
* by that parent, so freeing it here is a no-op, like arena memory
|
|
312
|
+
* by that parent, so freeing it here is a no-op, like arena memory; freeing
|
|
313
|
+
* NULL is a no-op too. The no-op cases are by design (they make ownership
|
|
314
|
+
* transfer safe to write), not errors, so they stay silent. */
|
|
290
315
|
void pbw_message_free(pbw_message *msg);
|
|
291
316
|
const pbw_descriptor *pbw_message_descriptor(const pbw_message *msg);
|
|
292
317
|
/* Wire length this tree was decoded from, 0 when it was built by hand. A
|
|
@@ -301,9 +326,10 @@ size_t pbw_message_wire_size(const pbw_message *msg);
|
|
|
301
326
|
* on a NULL submessage or a message retired by a later schema change; *len is
|
|
302
327
|
* 0 on every failure. */
|
|
303
328
|
pbw_status pbw_message_encoded_size(const pbw_message *msg, size_t *len);
|
|
304
|
-
/* Slack
|
|
305
|
-
*
|
|
306
|
-
*
|
|
329
|
+
/* Slack once needed past pbw_message_encoded_size(): the writer used to
|
|
330
|
+
* reserve tag/varint room upfront. The writer now reserves exactly what each
|
|
331
|
+
* field needs, so a buffer of exactly the encoded size fits; the macro stays
|
|
332
|
+
* for source compatibility (sizing `size + PBW_ENCODE_SLACK` still works). */
|
|
307
333
|
#define PBW_ENCODE_SLACK 12
|
|
308
334
|
|
|
309
335
|
/* Present value of a field, or NULL. Repeated fields expose a
|
|
@@ -336,7 +362,9 @@ int pbw_message_is_initialized(const pbw_message *msg);
|
|
|
336
362
|
* field owned is released with it; one that lives in the tree's arena
|
|
337
363
|
* survives the release unowned, so it can be stored into another field. While
|
|
338
364
|
* detached it rejects stores (nothing would release what it grew there) and
|
|
339
|
-
* becomes writable again once a tree owns it.
|
|
365
|
+
* becomes writable again once a tree owns it. On a message retired by a
|
|
366
|
+
* later schema add or link (see pbw_message_new) clearing silently does
|
|
367
|
+
* nothing; every other accessor fails instead. */
|
|
340
368
|
void pbw_message_clear(pbw_message *msg, uint32_t number);
|
|
341
369
|
|
|
342
370
|
/* Unknown fields kept from decoding, as raw wire bytes (each item is its
|
|
@@ -345,7 +373,15 @@ void pbw_message_clear(pbw_message *msg, uint32_t number);
|
|
|
345
373
|
size_t pbw_message_unknown_len(const pbw_message *msg);
|
|
346
374
|
const uint8_t *pbw_message_unknown_bytes(const pbw_message *msg);
|
|
347
375
|
/* Append raw unknown-field bytes (tag + payload per item, as produced by
|
|
348
|
-
* pbw_message_unknown_bytes).
|
|
376
|
+
* pbw_message_unknown_bytes). During decode the unknown store is bounded by
|
|
377
|
+
* the input byte cap (unknowns are a subset of the input, checked against
|
|
378
|
+
* limits->max_bytes up front, plus a few tag bytes for closed-enum
|
|
379
|
+
* mismatches); this hand-append path is caller-bounded instead, like any
|
|
380
|
+
* buffer append: intentionally no max_bytes check here, so repeated calls
|
|
381
|
+
* grow the store without bound and the caller must bound total bytes/calls
|
|
382
|
+
* itself. Fails on context-owned read-only messages.
|
|
383
|
+
* shortcut: caller-bounded, upgrade to checked wrapper when gateway exposes
|
|
384
|
+
* raw append to untrusted input. */
|
|
349
385
|
pbw_status pbw_message_add_unknown_bytes(pbw_message *msg, const void *data,
|
|
350
386
|
size_t len);
|
|
351
387
|
|
|
@@ -401,10 +437,20 @@ pbw_value *pbw_value_new_uint_ctx(pbw_ctx *ctx, uint64_t bits);
|
|
|
401
437
|
pbw_value *pbw_value_new_bytes_ctx(pbw_ctx *ctx, const void *data, size_t len);
|
|
402
438
|
/* Frees a value created by pbw_value_new*(). Values owned by a message (from
|
|
403
439
|
* pbw_message_get(), pbw_value_at(), ...) are released with it: this call is a
|
|
404
|
-
* no-op on them.
|
|
440
|
+
* no-op on them, as it is on NULL and on arena values. The no-op cases are by
|
|
441
|
+
* design (a borrowed value must never be freed twice), not errors, so they
|
|
442
|
+
* stay silent. */
|
|
405
443
|
void pbw_value_free(pbw_value *value);
|
|
406
444
|
|
|
407
445
|
pbw_value_kind pbw_value_get_kind(const pbw_value *value);
|
|
446
|
+
/* Numeric getters read the raw bit pattern of a PBW_VALUE_UINT value; any
|
|
447
|
+
* other kind (bytes, message, list) reads as 0 (0.0 for float/double), so a
|
|
448
|
+
* caller that skips the kind check gets zero instead of reinterpreted
|
|
449
|
+
* payload bytes. A wrong-kind (or NULL) 0 is indistinguishable from a
|
|
450
|
+
* genuine 0, so callers for whom 0 is meaningful must check
|
|
451
|
+
* pbw_value_get_kind() == PBW_VALUE_UINT first. Signatures stay as-is for
|
|
452
|
+
* ABI compatibility: there is no status-returning checked accessor, the
|
|
453
|
+
* kind check is the check. */
|
|
408
454
|
uint64_t pbw_value_get_uint(const pbw_value *value);
|
|
409
455
|
int64_t pbw_value_get_int(const pbw_value *value);
|
|
410
456
|
int32_t pbw_value_get_int32(const pbw_value *value);
|
|
@@ -439,7 +485,7 @@ pbw_ctx *pbw_ctx_new(void);
|
|
|
439
485
|
* handle plus a chunk header plus room for one allocation; NULL when it is not. An allocation that does not fit returns PBW_ERR_NOMEM from decode and
|
|
440
486
|
* PBW_ERR_LIMIT from pbw_encode_to: the block is the hard cap, so a steady
|
|
441
487
|
* workload never calls malloc. pbw_encode_ctx() needs a growable buffer and
|
|
442
|
-
* reports
|
|
488
|
+
* reports PBW_ERR_INVALID here; use pbw_encode_to() instead. */
|
|
443
489
|
pbw_ctx *pbw_ctx_new_mem(void *mem, size_t len);
|
|
444
490
|
void pbw_ctx_free(pbw_ctx *ctx);
|
|
445
491
|
/* Opaque binding slot, e.g. the language binding's per-thread state.
|
|
@@ -451,6 +497,11 @@ void *pbw_ctx_user(const pbw_ctx *ctx);
|
|
|
451
497
|
* Every message built or decoded with this context dies here too: touching
|
|
452
498
|
* one afterwards reads whatever the next user of the chunk wrote. */
|
|
453
499
|
void pbw_ctx_reset(pbw_ctx *ctx);
|
|
500
|
+
/* Default cap for inputs with no explicit byte bound: plain decode hints,
|
|
501
|
+
* descriptor-set loads and NULL-limit streams (1 MiB). Single allocations
|
|
502
|
+
* above it bypass the reusable chunks and are freed on reset, so one outlier
|
|
503
|
+
* never pins the context. */
|
|
504
|
+
#define PBW_DEFAULT_MAX_BYTES ((size_t)1 << 20)
|
|
454
505
|
/* Max bytes the context keeps resident across resets (decode arena chunks +
|
|
455
506
|
* encode buffer), default 1 MiB. Single allocations above it bypass the
|
|
456
507
|
* reusable chunks and are freed on reset, so one outlier never pins the
|
|
@@ -475,10 +526,18 @@ pbw_pool *pbw_pool_new(size_t max_idle, size_t retain_cap);
|
|
|
475
526
|
void pbw_pool_free(pbw_pool *pool);
|
|
476
527
|
/* Exclusive use until released; NULL when pool is NULL or out of memory. */
|
|
477
528
|
pbw_ctx *pbw_pool_acquire(pbw_pool *pool);
|
|
478
|
-
|
|
529
|
+
/* Release back to the pool (already reset, ready to re-acquire) or free past
|
|
530
|
+
* max_idle. PBW_OK when the context was pooled or freed, PBW_ERR_INVALID
|
|
531
|
+
* when the pool or context is NULL, or when the context lives on caller
|
|
532
|
+
* memory (pbw_ctx_new_mem): such a context can never grow like the pooled
|
|
533
|
+
* ones, so pooling it would hand out a context that fails where a pooled one
|
|
534
|
+
* succeeds, and the release is a no-op. */
|
|
535
|
+
pbw_status pbw_pool_release(pbw_pool *pool, pbw_ctx *ctx);
|
|
479
536
|
/* Idle contexts held now (for sizing/debugging, the printf side). */
|
|
480
537
|
size_t pbw_pool_idle(const pbw_pool *pool);
|
|
481
|
-
/*
|
|
538
|
+
/* Arena bytes currently held in the context (for sizing/debugging). The
|
|
539
|
+
* reusable encode buffer (out.cap) is excluded: budget retain_cap against
|
|
540
|
+
* used() plus the live out.cap when the encode buffer matters. */
|
|
482
541
|
size_t pbw_ctx_used(const pbw_ctx *ctx);
|
|
483
542
|
|
|
484
543
|
/* Decode into the context instead of one allocation per value. A sealed
|
|
@@ -488,7 +547,10 @@ size_t pbw_ctx_used(const pbw_ctx *ctx);
|
|
|
488
547
|
pbw_status pbw_decode_ctx(pbw_ctx *ctx, const pbw_descriptor *desc,
|
|
489
548
|
const void *buf, size_t len, pbw_message **out);
|
|
490
549
|
/* Encode into the context's buffer. *out stays valid until the next
|
|
491
|
-
* pbw_encode_ctx()/pbw_ctx_reset()/pbw_ctx_free() on the same context.
|
|
550
|
+
* pbw_encode_ctx()/pbw_ctx_reset()/pbw_ctx_free() on the same context.
|
|
551
|
+
* Unsupported on caller-memory contexts (pbw_ctx_new_mem): their buffer
|
|
552
|
+
* cannot grow, so this reports PBW_ERR_INVALID there; encode with
|
|
553
|
+
* pbw_encode_to() into caller memory instead. */
|
|
492
554
|
pbw_status pbw_encode_ctx(pbw_ctx *ctx, const pbw_message *msg,
|
|
493
555
|
const uint8_t **out, size_t *len);
|
|
494
556
|
|
|
@@ -501,13 +563,12 @@ typedef struct pbw_limits {
|
|
|
501
563
|
|
|
502
564
|
/* ------------------------------------------------------------------ wire */
|
|
503
565
|
|
|
504
|
-
/* The text and JSON mappings define '.' as the decimal separator
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* another thread calls into this library. */
|
|
566
|
+
/* The text and JSON mappings define '.' as the decimal separator. The parsers
|
|
567
|
+
* use a locale-independent conversion that accepts '.' only; the printers
|
|
568
|
+
* format with snprintf("%g") and replace a foreign decimal separator with
|
|
569
|
+
* '.', so the output stays valid under any LC_NUMERIC. No locale state is
|
|
570
|
+
* queried (no localeconv, no per-number setlocale dance), which keeps the
|
|
571
|
+
* printers safe to call from any thread. */
|
|
511
572
|
|
|
512
573
|
/* Print msg in protobuf text format. *out is malloc'd, NUL-terminated and owned
|
|
513
574
|
* by the caller (pbw_free); *len excludes the NUL. Unknown fields are shown
|
|
@@ -560,8 +621,8 @@ pbw_status pbw_json_parse(const pbw_descriptor *desc, const char *text,
|
|
|
560
621
|
size_t len, pbw_message **out);
|
|
561
622
|
/* Same with explicit bounds and options (either pointer may be NULL for the
|
|
562
623
|
* defaults). The nesting and byte caps behave like pbw_text_parse_limits();
|
|
563
|
-
*
|
|
564
|
-
*
|
|
624
|
+
* only message objects count toward max_depth (array wrappers do not, so a
|
|
625
|
+
* repeated message chain costs one level per message like the decoder). */
|
|
565
626
|
pbw_status pbw_json_parse_limits(const pbw_descriptor *desc, const char *text,
|
|
566
627
|
size_t len, const pbw_limits *limits,
|
|
567
628
|
const pbw_json_parse_options *options,
|
|
@@ -572,10 +633,11 @@ pbw_status pbw_json_parse_limits(const pbw_descriptor *desc, const char *text,
|
|
|
572
633
|
pbw_status pbw_encode(const pbw_message *msg, uint8_t **out, size_t *len);
|
|
573
634
|
/* Encode into caller memory of `cap` bytes, so a binding can write straight
|
|
574
635
|
* into its own buffer. No allocation happens and nothing is freed; `cap` is
|
|
575
|
-
* the hard limit
|
|
576
|
-
* `pbw_message_encoded_size(msg)
|
|
577
|
-
*
|
|
578
|
-
*
|
|
636
|
+
* the hard limit. The writer reserves exactly what each field needs, so a
|
|
637
|
+
* buffer of exactly `pbw_message_encoded_size(msg)` bytes fits (sizing
|
|
638
|
+
* `size + PBW_ENCODE_SLACK` still works, the macro is kept for compatibility).
|
|
639
|
+
* *len is the bytes written, 0 after any failure. PBW_ERR_LIMIT when the
|
|
640
|
+
* message does not fit. */
|
|
579
641
|
pbw_status pbw_encode_to(const pbw_message *msg, void *out, size_t cap,
|
|
580
642
|
size_t *len);
|
|
581
643
|
/* Decode buf into a new message of the given type. Unknown fields are kept.
|
|
@@ -590,16 +652,16 @@ pbw_status pbw_decode(const pbw_descriptor *desc, const void *buf, size_t len,
|
|
|
590
652
|
|
|
591
653
|
/* ------------------------------------------------------------------ limits */
|
|
592
654
|
|
|
593
|
-
/* Nesting cap the decoder enforces (100
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
#define PBW_MAX_DEPTH
|
|
655
|
+
/* Nesting cap the decoder enforces (100, like the reference implementations;
|
|
656
|
+
* still rejects conformance test 20000). A decoder that walks wire bytes
|
|
657
|
+
* itself must use the same bound. The cap bounds recursion, not stack: one
|
|
658
|
+
* nesting level costs about 1.4 KB of C stack in a release build and up to
|
|
659
|
+
* 3.8 KB in a debug build (~1 MB total under ASan/UBSan), so a thread that
|
|
660
|
+
* parses input up to the cap needs roughly 1 MB free (test_threads pins it
|
|
661
|
+
* to 2 MB; macOS worker threads default to 512 KB). Hosts on small stacks
|
|
662
|
+
* (NIF threads, firmware workers) pass pbw_limits.max_depth to lower the cap
|
|
663
|
+
* for the call instead of trusting the default. */
|
|
664
|
+
#define PBW_MAX_DEPTH 100
|
|
603
665
|
|
|
604
666
|
/* Decode with explicit bounds; identical to pbw_decode() with NULL limits. */
|
|
605
667
|
pbw_status pbw_decode_limits(const pbw_descriptor *desc, const void *buf,
|
|
@@ -613,7 +675,7 @@ pbw_status pbw_decode_ctx_limits(pbw_ctx *ctx, const pbw_descriptor *desc,
|
|
|
613
675
|
* nesting at limits->max_depth (0 = PBW_MAX_DEPTH, above clamps to it).
|
|
614
676
|
* Descriptors stay trusted input; the bounds only cap resource use from
|
|
615
677
|
* oversized or hostile sets. NULL limits behaves exactly like
|
|
616
|
-
* pbw_descriptor_set_load(). */
|
|
678
|
+
* pbw_descriptor_set_load(), including its 1 MiB default cap. */
|
|
617
679
|
pbw_status pbw_descriptor_set_load_limits(const void *data, size_t len,
|
|
618
680
|
const pbw_limits *limits,
|
|
619
681
|
pbw_schema **out);
|
|
@@ -624,8 +686,12 @@ pbw_status pbw_descriptor_set_load_limits(const void *data, size_t len,
|
|
|
624
686
|
pbw_status pbw_encode_deterministic(const pbw_message *msg, uint8_t **out,
|
|
625
687
|
size_t *len);
|
|
626
688
|
/* The caller-buffer twin of pbw_encode_deterministic(), with the same
|
|
627
|
-
* contract as pbw_encode_to():
|
|
628
|
-
*
|
|
689
|
+
* contract as pbw_encode_to(): `cap` is the hard limit and PBW_ERR_LIMIT when
|
|
690
|
+
* the message does not fit. The output buffer itself costs no allocation, but
|
|
691
|
+
* the map-entry sort does, so a message with a map can also fail here with
|
|
692
|
+
* PBW_ERR_NOMEM. Heap-free (embedded/caller-memory) callers must therefore
|
|
693
|
+
* use plain pbw_encode_to() for maps: the deterministic map sort is
|
|
694
|
+
* unavailable without the heap, and there is no sorted fallback. */
|
|
629
695
|
pbw_status pbw_encode_deterministic_to(const pbw_message *msg, void *out,
|
|
630
696
|
size_t cap, size_t *len);
|
|
631
697
|
|
|
@@ -682,10 +748,10 @@ pbw_status pbw_encode_sink(const pbw_message *msg, pbw_sink_fn sink,
|
|
|
682
748
|
pbw_status pbw_encode_delimited(const pbw_message *msg, uint8_t **out,
|
|
683
749
|
size_t *len);
|
|
684
750
|
/* Same framing into caller memory: no allocation, `cap` is the hard limit.
|
|
685
|
-
*
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
*
|
|
751
|
+
* A buffer of exactly pbw_message_encoded_size(msg) plus its length prefix
|
|
752
|
+
* fits (the prefix is one byte under 128 bytes, one more per 7 bits above;
|
|
753
|
+
* adding PBW_ENCODE_SLACK still works). *len is the whole frame, 0 after
|
|
754
|
+
* any failure. */
|
|
689
755
|
pbw_status pbw_encode_delimited_to(const pbw_message *msg, void *out,
|
|
690
756
|
size_t cap, size_t *len);
|
|
691
757
|
|
|
@@ -694,10 +760,28 @@ typedef struct pbw_istream pbw_istream;
|
|
|
694
760
|
/* Incremental delimited-message reader: feed() appends wire bytes as they
|
|
695
761
|
* arrive, next() decodes one message per call. next() writes NULL to *out
|
|
696
762
|
* and returns PBW_OK when the buffer holds no complete message yet. */
|
|
697
|
-
|
|
763
|
+
/* NULL limits mean capped defaults (max_bytes = PBW_DEFAULT_MAX_BYTES, no
|
|
764
|
+
* depth cap past PBW_MAX_DEPTH): an unbounded stream buffer turns a sender
|
|
765
|
+
* that never completes a frame into unbounded growth. Pass explicit limits
|
|
766
|
+
* with max_bytes = 0 only for trusted input that must stream uncapped. */
|
|
767
|
+
pbw_istream *pbw_istream_new(const pbw_limits *limits);
|
|
698
768
|
void pbw_istream_free(pbw_istream *s);
|
|
699
769
|
/* Drop buffered bytes (complete and partial frames alike). */
|
|
700
770
|
void pbw_istream_reset(pbw_istream *s);
|
|
771
|
+
/* Pipelined complete frames stay feedable past max_bytes + 5 buffered
|
|
772
|
+
* bytes: while the buffer holds a complete in-cap frame, feed() appends
|
|
773
|
+
* and next() drains one frame per call, still policing each frame against
|
|
774
|
+
* the limits, so back-to-back frames that together pass the cap stream
|
|
775
|
+
* instead of failing LIMIT with deliverable bytes sitting in the buffer.
|
|
776
|
+
* Only a buffer with no complete frame in it is refused past the cap: an
|
|
777
|
+
* empty buffer takes one bounded pipelined batch (at most ~2x max_bytes
|
|
778
|
+
* + 5, opening with an in-cap frame), and otherwise only the chunk
|
|
779
|
+
* completing a split within-cap frame (plus one cap window of trailing)
|
|
780
|
+
* grows the buffer. A single over-cap frame is still refused, at feed
|
|
781
|
+
* time from empty and at next() time when pipelined. When a length prefix
|
|
782
|
+
* splits across feeds, complete it with a <=5-byte feed first; a longer
|
|
783
|
+
* completion may report LIMIT though the frame fits. Always check the
|
|
784
|
+
* feed status. */
|
|
701
785
|
pbw_status pbw_istream_feed(pbw_istream *s, const void *data, size_t len);
|
|
702
786
|
pbw_status pbw_istream_next(pbw_istream *s, const pbw_descriptor *desc,
|
|
703
787
|
pbw_message **out);
|