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,450 @@
1
+ /* Streaming helpers: length-delimited framing (varint length + message) over an
2
+ * incremental byte stream. The reader buffers input until a whole frame
3
+ * arrives and then decodes it with the one-shot decoder, so no second parsing
4
+ * path can drift out of sync; the sink encodes as usual and hands the bytes
5
+ * to a caller callback in 8 KiB chunks.
6
+ */
7
+
8
+ #include "internal.h"
9
+
10
+ struct pbw_istream {
11
+ uint8_t *data;
12
+ size_t len;
13
+ size_t cap;
14
+ size_t off; /* consumed prefix, compacted when it grows */
15
+ pbw_limits limits;
16
+ };
17
+
18
+ #define PBW_SINK_CHUNK 8192
19
+
20
+ pbw_istream *
21
+ pbw_istream_new(const pbw_limits *limits)
22
+ {
23
+ pbw_istream *s = (pbw_istream *)calloc(1, sizeof(*s));
24
+
25
+ if (s == NULL) {
26
+ return NULL;
27
+ }
28
+ if (limits != NULL) {
29
+ s->limits = *limits;
30
+ }
31
+ return s;
32
+ }
33
+
34
+ void
35
+ pbw_istream_free(pbw_istream *s)
36
+ {
37
+ if (s == NULL) {
38
+ return;
39
+ }
40
+ free(s->data);
41
+ free(s);
42
+ }
43
+
44
+ void
45
+ pbw_istream_reset(pbw_istream *s)
46
+ {
47
+ if (s == NULL) {
48
+ return;
49
+ }
50
+ s->len = 0;
51
+ s->off = 0;
52
+ }
53
+
54
+ /* True when the bytes hold at least one complete frame (length prefix plus
55
+ * body), without decoding anything. Lets feed() tell pipelined frames
56
+ * (which next() will drain and police) from never-completing bytes (which
57
+ * would otherwise grow the buffer forever). */
58
+ static int
59
+ has_complete_frame(const uint8_t *base, size_t avail)
60
+ {
61
+ size_t used = 0;
62
+ size_t frame;
63
+
64
+ if (base == NULL) {
65
+ return 0;
66
+ }
67
+ if (!pbw_len_read(base, base + avail, &frame, &used)) {
68
+ return 0;
69
+ }
70
+ return used <= avail && frame <= avail - used;
71
+ }
72
+
73
+ pbw_status
74
+ pbw_istream_feed(pbw_istream *s, const void *data, size_t len)
75
+ {
76
+ if (s == NULL || (data == NULL && len > 0)) {
77
+ return PBW_ERR_INVALID;
78
+ }
79
+ if (len == 0) {
80
+ return PBW_OK;
81
+ }
82
+ /* Bound never-completing bytes: without a cap a sender that never forms
83
+ * a frame grows the buffer forever. Pipelined complete frames stay
84
+ * allowed past the cap (next() polices each frame); only a buffer with
85
+ * no complete frame in it is refused. The +5 is the length-prefix slack:
86
+ * a max-sized frame needs at most max_bytes + 5 buffered bytes. */
87
+ if (s->limits.max_bytes > 0) {
88
+ size_t max = s->limits.max_bytes;
89
+ size_t buffered = s->len - s->off;
90
+ size_t total;
91
+ int over;
92
+
93
+ if (len > SIZE_MAX - buffered) {
94
+ /* Absurd chunk: let the growth path below report NOMEM. */
95
+ over = 0;
96
+ total = 0;
97
+ } else {
98
+ total = buffered + len;
99
+ over = total > max && total - max > 5;
100
+ }
101
+ if (over) {
102
+ const uint8_t *base =
103
+ (buffered > 0 && s->data != NULL) ? s->data + s->off : NULL;
104
+
105
+ if (base != NULL && has_complete_frame(base, buffered)) {
106
+ /* Pipelined: next() drains each frame and polices it. */
107
+ } else if (buffered == 0) {
108
+ if (!has_complete_frame((const uint8_t *)data, len)) {
109
+ return PBW_ERR_LIMIT;
110
+ }
111
+ } else {
112
+ /* A split frame is only let through when this chunk
113
+ * completes a within-cap frame; anything else (a partial
114
+ * prefix, an oversized frame, trailing garbage) is refused
115
+ * before it grows the buffer. */
116
+ size_t used = 0;
117
+ size_t frame = 0;
118
+
119
+ if (base == NULL ||
120
+ !pbw_len_read(base, base + buffered, &frame, &used) ||
121
+ frame > max || used > max + 5 - frame ||
122
+ used + frame <= buffered ||
123
+ used + frame > buffered + len) {
124
+ return PBW_ERR_LIMIT;
125
+ }
126
+ }
127
+ }
128
+ }
129
+ if (!pbw_cap_holds(s->cap, s->len) || s->off > s->len) {
130
+ return PBW_ERR_INVALID;
131
+ }
132
+ if (s->data == NULL && (s->len > 0 || s->cap > 0 || s->off > 0)) {
133
+ return PBW_ERR_INVALID;
134
+ }
135
+ /* Compact the consumed prefix only when the tail has no room: a steady
136
+ * feed/next loop then appends without a memmove per chunk. */
137
+ if (len > s->cap - s->len) {
138
+ size_t cap;
139
+ uint8_t *grown;
140
+
141
+ if (s->off > 0) {
142
+ if (s->data == NULL) {
143
+ return PBW_ERR_INVALID;
144
+ }
145
+ memmove(s->data, s->data + s->off, s->len - s->off);
146
+ s->len -= s->off;
147
+ s->off = 0;
148
+ }
149
+ if (len > s->cap - s->len) {
150
+ /* Compacting (or none needed): the tail still has to grow. */
151
+ if (s->cap > SIZE_MAX - s->len) {
152
+ return PBW_ERR_NOMEM;
153
+ }
154
+ cap = s->cap != 0 ? s->cap : 256;
155
+ while (cap - s->len < len) {
156
+ if (cap > SIZE_MAX / 2) {
157
+ /* No allocation can hold this chunk; report it instead of
158
+ * spinning on a wrapped capacity. */
159
+ return PBW_ERR_NOMEM;
160
+ }
161
+ cap *= 2;
162
+ }
163
+ grown = (uint8_t *)realloc(s->data, cap);
164
+ if (grown == NULL) {
165
+ return PBW_ERR_NOMEM;
166
+ }
167
+ s->data = grown;
168
+ s->cap = cap;
169
+ }
170
+ }
171
+ if (s->data == NULL) {
172
+ return PBW_ERR_NOMEM;
173
+ }
174
+ if (s->len > SIZE_MAX - len) {
175
+ return PBW_ERR_NOMEM;
176
+ }
177
+ memcpy(s->data + s->len, data, len);
178
+ s->len += len;
179
+ return PBW_OK;
180
+ }
181
+
182
+ /* One delimited frame from the buffered bytes: NULL + PBW_OK means more data
183
+ * is needed. A frame that fails its limits or decodes badly leaves the buffer
184
+ * untouched, so the caller can reset() and carry on with the next stream. */
185
+ pbw_status
186
+ pbw_istream_next(pbw_istream *s, const pbw_descriptor *desc, pbw_message **out)
187
+ {
188
+ const uint8_t *base;
189
+ size_t avail;
190
+ size_t used = 0;
191
+ size_t frame;
192
+ pbw_status status;
193
+
194
+ if (s == NULL || desc == NULL || out == NULL) {
195
+ return PBW_ERR_INVALID;
196
+ }
197
+ *out = NULL;
198
+ if (s->off > s->len || !pbw_cap_holds(s->cap, s->len)) {
199
+ return PBW_ERR_INVALID;
200
+ }
201
+ avail = s->len - s->off;
202
+ if (avail == 0) {
203
+ return PBW_OK;
204
+ }
205
+ if (s->data == NULL) {
206
+ return PBW_ERR_INVALID;
207
+ }
208
+ base = s->data + s->off;
209
+ if (!pbw_len_read(base, base + avail, &frame, &used)) {
210
+ /* A length varint is at most 5 bytes: anything longer is corrupt,
211
+ * anything shorter may just be split across feeds. */
212
+ if (avail >= 5) {
213
+ return PBW_ERR_INVALID;
214
+ }
215
+ return PBW_OK;
216
+ }
217
+ if (s->limits.max_bytes > 0 && frame > s->limits.max_bytes) {
218
+ return PBW_ERR_LIMIT;
219
+ }
220
+ if (used > avail || frame > avail - used) {
221
+ return PBW_OK;
222
+ }
223
+ status = pbw_decode_limits(desc, base + used, frame, &s->limits, out);
224
+ if (status != PBW_OK) {
225
+ return status;
226
+ }
227
+ s->off += used + frame;
228
+ return PBW_OK;
229
+ }
230
+
231
+ pbw_status
232
+ pbw_encode_sink(const pbw_message *msg, pbw_sink_fn sink, void *opaque)
233
+ {
234
+ pbw_buf buf;
235
+ pbw_status encode;
236
+ size_t at = 0;
237
+
238
+ if (msg == NULL || sink == NULL) {
239
+ return PBW_ERR_INVALID;
240
+ }
241
+ pbw_buf_init(&buf);
242
+ encode = pbw_encode_buf(&buf, msg);
243
+ if (encode != PBW_OK) {
244
+ pbw_buf_free(&buf);
245
+ return encode;
246
+ }
247
+ /* shortcut: one callback per chunk, not per field; a per-field sink would
248
+ * re-enter the encoder for every tag, upgrade when zero-copy matters. */
249
+ while (at < buf.len) {
250
+ size_t n = buf.len - at > PBW_SINK_CHUNK ? PBW_SINK_CHUNK : buf.len - at;
251
+ pbw_status fed = sink(buf.data + at, n, opaque);
252
+
253
+ if (fed != PBW_OK) {
254
+ pbw_buf_free(&buf);
255
+ return fed;
256
+ }
257
+ at += n;
258
+ }
259
+ pbw_buf_free(&buf);
260
+ return PBW_OK;
261
+ }
262
+
263
+ pbw_status
264
+ pbw_encode_delimited(const pbw_message *msg, uint8_t **out, size_t *len)
265
+ {
266
+ uint8_t *body = NULL;
267
+ size_t body_len = 0;
268
+ size_t prefix;
269
+ uint8_t *framed;
270
+ pbw_status status;
271
+
272
+ if (msg == NULL || out == NULL || len == NULL) {
273
+ return PBW_ERR_INVALID;
274
+ }
275
+ *out = NULL;
276
+ *len = 0;
277
+ status = pbw_encode(msg, &body, &body_len);
278
+ if (status != PBW_OK) {
279
+ return status;
280
+ }
281
+ prefix = pbw_varint_size(body_len);
282
+ if (prefix > SIZE_MAX - body_len) {
283
+ pbw_free(body);
284
+ return PBW_ERR_NOMEM;
285
+ }
286
+ framed = (uint8_t *)malloc(prefix + body_len);
287
+ if (framed == NULL) {
288
+ pbw_free(body);
289
+ return PBW_ERR_NOMEM;
290
+ }
291
+ pbw_varint_write(framed, body_len);
292
+ if (body_len > 0) {
293
+ memcpy(framed + prefix, body, body_len);
294
+ }
295
+ pbw_free(body);
296
+ *out = framed;
297
+ *len = prefix + body_len;
298
+ return PBW_OK;
299
+ }
300
+
301
+ pbw_status
302
+ pbw_encode_delimited_to(const pbw_message *msg, void *out, size_t cap,
303
+ size_t *len)
304
+ {
305
+ size_t body = 0;
306
+ size_t prefix;
307
+ size_t written = 0;
308
+ pbw_status status;
309
+
310
+ if (len == NULL) {
311
+ return PBW_ERR_INVALID;
312
+ }
313
+ *len = 0;
314
+ if (msg == NULL || (out == NULL && cap > 0)) {
315
+ return PBW_ERR_INVALID;
316
+ }
317
+ status = pbw_message_encoded_size(msg, &body);
318
+ if (status != PBW_OK) {
319
+ return status;
320
+ }
321
+ prefix = pbw_varint_size(body);
322
+ if (prefix > cap || body > cap - prefix) {
323
+ return PBW_ERR_LIMIT;
324
+ }
325
+ pbw_varint_write((uint8_t *)out, body);
326
+ /* The residual cap carries the slack pbw_encode_to() needs; the caller
327
+ * sized the buffer, so a short one fails instead of growing. */
328
+ status = pbw_encode_to(msg, (uint8_t *)out + prefix, cap - prefix, &written);
329
+ if (status != PBW_OK) {
330
+ return status;
331
+ }
332
+ if (written != body) {
333
+ /* encoded_size() is exact, so a mismatch would emit a frame whose
334
+ * declared length lies about its body. Never hand that back. */
335
+ return PBW_ERR_INVALID;
336
+ }
337
+ *len = prefix + written;
338
+ return PBW_OK;
339
+ }
340
+
341
+ /* Split one buffered length-delimited frame into its declared length and its
342
+ * body. PBW_ERR_LIMIT when the declared length is past the cap (checked before
343
+ * anything is decoded, like pbw_istream_next); PBW_ERR_INVALID on a corrupt
344
+ * prefix or a frame that runs past the buffer, since a one-shot decode has
345
+ * nowhere to wait for more bytes. */
346
+ static pbw_status
347
+ frame_split(const void *buf, size_t len, const pbw_limits *limits,
348
+ const uint8_t **body, size_t *frame, size_t *used)
349
+ {
350
+ const uint8_t *base = (const uint8_t *)buf;
351
+
352
+ if (!pbw_len_read(base, base + len, frame, used)) {
353
+ return PBW_ERR_INVALID;
354
+ }
355
+ if (*used > len || *frame > len - *used) {
356
+ return PBW_ERR_INVALID;
357
+ }
358
+ if (limits != NULL && limits->max_bytes > 0 && *frame > limits->max_bytes) {
359
+ return PBW_ERR_LIMIT;
360
+ }
361
+ *body = base + *used;
362
+ return PBW_OK;
363
+ }
364
+
365
+ pbw_status
366
+ pbw_decode_delimited_limits(const pbw_descriptor *desc, const void *buf,
367
+ size_t len, const pbw_limits *limits,
368
+ pbw_message **out, size_t *consumed)
369
+ {
370
+ const uint8_t *body = NULL;
371
+ size_t frame = 0;
372
+ size_t used = 0;
373
+ pbw_status status;
374
+
375
+ if (out != NULL) {
376
+ *out = NULL;
377
+ }
378
+ if (consumed != NULL) {
379
+ *consumed = 0;
380
+ }
381
+ if (desc == NULL || out == NULL || consumed == NULL ||
382
+ (buf == NULL && len > 0)) {
383
+ return PBW_ERR_INVALID;
384
+ }
385
+ if (len == 0) {
386
+ return PBW_ERR_INVALID;
387
+ }
388
+ status = frame_split(buf, len, limits, &body, &frame, &used);
389
+ if (status != PBW_OK) {
390
+ return status;
391
+ }
392
+ status = pbw_decode_limits(desc, body, frame, limits, out);
393
+ if (status != PBW_OK) {
394
+ return status;
395
+ }
396
+ *consumed = used + frame;
397
+ return PBW_OK;
398
+ }
399
+
400
+ pbw_status
401
+ pbw_decode_delimited_ctx_limits(pbw_ctx *ctx, const pbw_descriptor *desc,
402
+ const void *buf, size_t len,
403
+ const pbw_limits *limits, pbw_message **out,
404
+ size_t *consumed)
405
+ {
406
+ const uint8_t *body = NULL;
407
+ size_t frame = 0;
408
+ size_t used = 0;
409
+ pbw_status status;
410
+
411
+ if (out != NULL) {
412
+ *out = NULL;
413
+ }
414
+ if (consumed != NULL) {
415
+ *consumed = 0;
416
+ }
417
+ if (ctx == NULL || desc == NULL || out == NULL || consumed == NULL ||
418
+ (buf == NULL && len > 0)) {
419
+ return PBW_ERR_INVALID;
420
+ }
421
+ if (len == 0) {
422
+ return PBW_ERR_INVALID;
423
+ }
424
+ status = frame_split(buf, len, limits, &body, &frame, &used);
425
+ if (status != PBW_OK) {
426
+ return status;
427
+ }
428
+ status = pbw_decode_ctx_limits(ctx, desc, body, frame, limits, out);
429
+ if (status != PBW_OK) {
430
+ return status;
431
+ }
432
+ *consumed = used + frame;
433
+ return PBW_OK;
434
+ }
435
+
436
+ pbw_status
437
+ pbw_decode_delimited(const pbw_descriptor *desc, const void *buf, size_t len,
438
+ pbw_message **out, size_t *consumed)
439
+ {
440
+ return pbw_decode_delimited_limits(desc, buf, len, NULL, out, consumed);
441
+ }
442
+
443
+ pbw_status
444
+ pbw_decode_delimited_ctx(pbw_ctx *ctx, const pbw_descriptor *desc,
445
+ const void *buf, size_t len, pbw_message **out,
446
+ size_t *consumed)
447
+ {
448
+ return pbw_decode_delimited_ctx_limits(ctx, desc, buf, len, NULL, out,
449
+ consumed);
450
+ }