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,1100 @@
1
+ /* Dynamic message value tree: allocation, accessors, field storage, oneof
2
+ * handling. */
3
+
4
+ #include "internal.h"
5
+
6
+ pbw_message *
7
+ pbw_message_new(const pbw_descriptor *desc)
8
+ {
9
+ return pbw_message_new_a(NULL, desc);
10
+ }
11
+
12
+ /* Releases the payloads of a message without touching the message struct or its
13
+ * arena: used both for heap messages (which are then freed) and for messages
14
+ * that live inside an arena, where the struct itself is not individually
15
+ * freeable. */
16
+ static void
17
+ message_release(pbw_message *msg)
18
+ {
19
+ size_t i;
20
+
21
+ /* nfields, not desc->field_count: freeing after pbw_schema_free must
22
+ * not touch the freed descriptor. */
23
+ for (i = 0; i < msg->nfields; i++) {
24
+ if (pbw_msg_present(msg, i)) {
25
+ pbw_value_destroy(&msg->values[i]);
26
+ /* An arena message's struct outlives its payload (a clear() only
27
+ * releases the child), so a released slot must never read as
28
+ * present. */
29
+ pbw_msg_set_present(msg, i, 0);
30
+ }
31
+ }
32
+ if (msg->arena == NULL) {
33
+ /* Heap message: the unknown bytes and the struct are ours to free. In an
34
+ * arena the unknown bytes are arena memory, released with the arena. */
35
+ free(msg->unknown);
36
+ free(msg);
37
+ }
38
+ }
39
+
40
+ /* Releases a message together with whatever it owns, ignoring `stored`: the
41
+ * caller IS its owner (a value slot, a list item, or the tree being freed). */
42
+ static void
43
+ message_free_body(pbw_message *msg)
44
+ {
45
+ if (msg->own != NULL) {
46
+ /* Root of a decoded tree: when nothing heap was stored into it
47
+ * (the pure decode fast path, including every bench decode), the
48
+ * whole tree lives in the arena and a bulk free is enough. Only a
49
+ * mutated tree needs the slot walk to release heap payloads. */
50
+ pbw_arena *own = msg->own;
51
+ int packed = own->block != NULL;
52
+
53
+ if (own->has_heap) {
54
+ message_release(msg);
55
+ }
56
+ pbw_arena_free(own);
57
+ if (!packed) {
58
+ free(own);
59
+ }
60
+ return;
61
+ }
62
+ if (msg->arena != NULL) {
63
+ /* A submessage of an arena tree dies with that tree (or context). */
64
+ return;
65
+ }
66
+ message_release(msg);
67
+ }
68
+
69
+ void
70
+ pbw_message_free(pbw_message *msg)
71
+ {
72
+ if (msg == NULL || msg->stored) {
73
+ /* Owned by a parent slot or list (set/set_message, an adopted decoded
74
+ * root, or a decoded tree's submessage): it dies with the parent,
75
+ * like arena memory. The release walk calls message_free_body()
76
+ * directly, so a direct free here is a no-op. */
77
+ return;
78
+ }
79
+ message_free_body(msg);
80
+ }
81
+
82
+ const pbw_descriptor *
83
+ pbw_message_descriptor(const pbw_message *msg)
84
+ {
85
+ return msg == NULL ? NULL : msg->desc;
86
+ }
87
+
88
+ size_t
89
+ pbw_message_wire_size(const pbw_message *msg)
90
+ {
91
+ return msg == NULL ? 0 : msg->wire_len;
92
+ }
93
+
94
+ int
95
+ pbw_message_add_unknown(pbw_message *msg, const uint8_t *data, size_t len)
96
+ {
97
+ size_t need;
98
+ uint8_t *grown;
99
+
100
+ if (msg == NULL) {
101
+ return PBW_ERR_INVALID;
102
+ }
103
+ if (len == 0) {
104
+ return PBW_OK;
105
+ }
106
+ if (data == NULL) {
107
+ return PBW_ERR_INVALID;
108
+ }
109
+ if (msg->unknown_len > SIZE_MAX - len) {
110
+ return PBW_ERR_NOMEM;
111
+ }
112
+ need = msg->unknown_len + len;
113
+ if (!pbw_cap_holds(msg->unknown_cap, msg->unknown_len)) {
114
+ return PBW_ERR_NOMEM;
115
+ }
116
+ if (need > msg->unknown_cap) {
117
+ size_t cap = msg->unknown_cap == 0 ? 64 : msg->unknown_cap;
118
+
119
+ while (cap < need) {
120
+ if (cap > SIZE_MAX / 2) {
121
+ cap = need;
122
+ break;
123
+ }
124
+ cap *= 2;
125
+ }
126
+ if (msg->arena != NULL) {
127
+ if (msg->unknown == NULL && msg->unknown_len > 0) {
128
+ return PBW_ERR_NOMEM;
129
+ }
130
+ grown = (uint8_t *)pbw_arena_alloc(msg->arena, cap);
131
+ if (grown != NULL && msg->unknown_len > 0) {
132
+ memcpy(grown, msg->unknown, msg->unknown_len);
133
+ }
134
+ } else {
135
+ grown = (uint8_t *)realloc(msg->unknown, cap);
136
+ }
137
+ if (grown == NULL) {
138
+ return PBW_ERR_NOMEM;
139
+ }
140
+ msg->unknown = grown;
141
+ msg->unknown_cap = cap;
142
+ }
143
+ memcpy(msg->unknown + msg->unknown_len, data, len);
144
+ msg->unknown_len = need;
145
+ return PBW_OK;
146
+ }
147
+
148
+ /* 1 when `target` is `msg` or sits anywhere below it: attaching such a tree
149
+ * would make one message its own ancestor, and clear()/free() would release
150
+ * the arena (or struct) it is walking inside. Only reachable path: an
151
+ * unowned heap root attached into a nested node of an existing tree, which is
152
+ * why the walk is only paid there. */
153
+ static int
154
+ message_contains(const pbw_message *msg, const pbw_message *target,
155
+ uint32_t depth)
156
+ {
157
+ size_t i;
158
+
159
+ if (msg == target) {
160
+ return 1;
161
+ }
162
+ if (depth > PBW_MAX_DEPTH) {
163
+ /* Deeper than the encode nesting cap (encode and the printers allow a
164
+ * node at PBW_MAX_DEPTH, not below it): such a tree is already
165
+ * unusable, so answer "contains" and let the store be refused.
166
+ * Answering 0 accepted a cycle closed below the walk, whose root is
167
+ * then `stored` and unreachable for every free. */
168
+ return 1;
169
+ }
170
+ for (i = 0; i < msg->nfields; i++) {
171
+ const pbw_value *v;
172
+
173
+ if (!pbw_msg_present(msg, i)) {
174
+ continue;
175
+ }
176
+ v = &msg->values[i];
177
+ if (v->kind == PBW_VALUE_LIST) {
178
+ size_t j;
179
+
180
+ if (v->as.list.items == NULL) {
181
+ continue;
182
+ }
183
+ for (j = 0; j < v->as.list.len; j++) {
184
+ const pbw_value *item = v->as.list.items[j];
185
+
186
+ if (item == NULL || item->kind != PBW_VALUE_MESSAGE ||
187
+ item->as.msg == NULL) {
188
+ continue;
189
+ }
190
+ if (message_contains(item->as.msg, target, depth + 1)) {
191
+ return 1;
192
+ }
193
+ }
194
+ } else if (v->kind == PBW_VALUE_MESSAGE && v->as.msg != NULL) {
195
+ if (message_contains(v->as.msg, target, depth + 1)) {
196
+ return 1;
197
+ }
198
+ }
199
+ }
200
+ return 0;
201
+ }
202
+
203
+ static size_t
204
+ value_field_index(const pbw_message *msg, const pbw_field *field)
205
+ {
206
+ return (size_t)(field - msg->desc->fields);
207
+ }
208
+
209
+ pbw_value *
210
+ pbw_message_get(const pbw_message *msg, uint32_t number)
211
+ {
212
+ const pbw_field *field;
213
+ size_t index;
214
+
215
+ if (!pbw_msg_fresh(msg)) {
216
+ return NULL;
217
+ }
218
+ field = pbw_descriptor_find_field(msg->desc, number);
219
+ if (field == NULL) {
220
+ return NULL;
221
+ }
222
+ index = value_field_index(msg, field);
223
+ return pbw_msg_present(msg, index) ? &msg->values[index] : NULL;
224
+ }
225
+
226
+ pbw_value *
227
+ pbw_message_get_field(const pbw_message *msg, const pbw_field *field)
228
+ {
229
+ const pbw_field *base;
230
+ size_t index;
231
+
232
+ if (!pbw_msg_fresh(msg) || field == NULL) {
233
+ return NULL;
234
+ }
235
+ base = msg->desc->fields;
236
+ if (field < base || field >= base + msg->desc->field_count) {
237
+ return NULL;
238
+ }
239
+ index = (size_t)(field - base);
240
+ return pbw_msg_present(msg, index) ? &msg->values[index] : NULL;
241
+ }
242
+
243
+ static int
244
+ is_initialized(const pbw_message *msg, uint32_t depth)
245
+ {
246
+ size_t i;
247
+
248
+ /* Read `>` like the decoder: a message at depth PBW_MAX_DEPTH is
249
+ * accepted on the wire, so the required-field walk must judge it too. */
250
+ if (!pbw_msg_fresh(msg) || depth > PBW_MAX_DEPTH) {
251
+ return 0;
252
+ }
253
+ /* The flag is computed by pbw_schema_link(); an unlinked descriptor is
254
+ * unsupported for messages already (its fields are still unsorted), so
255
+ * only a linked tree may take the shortcut. */
256
+ if (!msg->desc->has_required && (msg->desc->schema == NULL ||
257
+ msg->desc->schema->linked)) {
258
+ return 1; /* no required field in this subtree */
259
+ }
260
+ for (i = 0; i < msg->desc->field_count; i++) {
261
+ const pbw_field *field = &msg->desc->fields[i];
262
+ const pbw_value *value = &msg->values[i];
263
+
264
+ if (!pbw_msg_present(msg, i)) {
265
+ if (pbw_field_is_required(field, msg->desc)) {
266
+ return 0;
267
+ }
268
+ continue;
269
+ }
270
+ if (field->type != PBW_TYPE_MESSAGE && field->type != PBW_TYPE_GROUP) {
271
+ continue;
272
+ }
273
+ if (field->label == PBW_LABEL_REPEATED) {
274
+ size_t j;
275
+
276
+ if (value->kind != PBW_VALUE_LIST) {
277
+ continue;
278
+ }
279
+ if (value->as.list.len > 0 && value->as.list.items == NULL) {
280
+ /* Corrupt list (items but no array), same shape the
281
+ * encoder and the printers refuse: report uninitialized
282
+ * instead of reading NULL[0]. */
283
+ return 0;
284
+ }
285
+ for (j = 0; j < value->as.list.len; j++) {
286
+ const pbw_value *item = value->as.list.items[j];
287
+
288
+ /* Same guard as encode: a hostile put() can plant a
289
+ * non-message item here, and dereferencing it crashes
290
+ * instead of reporting uninitialized. */
291
+ if (item == NULL || item->kind != PBW_VALUE_MESSAGE ||
292
+ item->as.msg == NULL ||
293
+ !is_initialized(item->as.msg, depth + 1)) {
294
+ return 0;
295
+ }
296
+ }
297
+ continue;
298
+ }
299
+ if (value->kind != PBW_VALUE_MESSAGE ||
300
+ !is_initialized(value->as.msg, depth + 1)) {
301
+ return 0;
302
+ }
303
+ }
304
+ return 1;
305
+ }
306
+
307
+ int
308
+ pbw_message_is_initialized(const pbw_message *msg)
309
+ {
310
+ return is_initialized(msg, 0);
311
+ }
312
+
313
+ size_t
314
+ pbw_message_present_list(const pbw_message *msg, uint32_t *fnums,
315
+ const pbw_value **values, size_t cap)
316
+ {
317
+ size_t i;
318
+ size_t n = 0;
319
+
320
+ if (!pbw_msg_fresh(msg) || fnums == NULL || values == NULL) {
321
+ return 0;
322
+ }
323
+ for (i = 0; i < msg->desc->field_count && n < cap; i++) {
324
+ if (pbw_msg_present(msg, i)) {
325
+ fnums[n] = msg->desc->fields[i].number;
326
+ values[n] = &msg->values[i];
327
+ n++;
328
+ }
329
+ }
330
+ return n;
331
+ }
332
+
333
+ size_t
334
+ pbw_message_field_count(const pbw_message *msg)
335
+ {
336
+ size_t i;
337
+ size_t count = 0;
338
+
339
+ if (!pbw_msg_fresh(msg)) {
340
+ return 0;
341
+ }
342
+ for (i = 0; i < msg->desc->field_count; i++) {
343
+ count += (size_t)pbw_msg_present(msg, i);
344
+ }
345
+ return count;
346
+ }
347
+
348
+ const pbw_field *
349
+ pbw_message_field_at(const pbw_message *msg, size_t index,
350
+ pbw_value **value)
351
+ {
352
+ size_t i;
353
+ size_t seen = 0;
354
+
355
+ if (!pbw_msg_fresh(msg)) {
356
+ return NULL;
357
+ }
358
+ for (i = 0; i < msg->desc->field_count; i++) {
359
+ if (!pbw_msg_present(msg, i)) {
360
+ continue;
361
+ }
362
+ if (seen == index) {
363
+ if (value != NULL) {
364
+ *value = &msg->values[i];
365
+ }
366
+ return &msg->desc->fields[i];
367
+ }
368
+ seen++;
369
+ }
370
+ return NULL;
371
+ }
372
+
373
+ /* Releases a message a wrapper owned, whichever wrapper: an arena
374
+ * submessage's struct outlives the release, so it becomes unowned again (a
375
+ * caller that cleared the field may adopt it elsewhere); a heap message and an
376
+ * adopted decoded root are released with their payloads. */
377
+ static void
378
+ message_release_slot(pbw_message *m)
379
+ {
380
+ if (m->arena != NULL && m->own == NULL) {
381
+ message_release(m);
382
+ m->stored = 0;
383
+ return;
384
+ }
385
+ message_free_body(m);
386
+ }
387
+
388
+ /* 1 when the message lives in a decoded tree's arena but no tree owns it any
389
+ * more: pbw_message_clear() released it, so no teardown walk reaches its slots
390
+ * (a context or caller-memory arena keeps this false: those are roots). */
391
+ static int
392
+ message_detached(const pbw_message *msg)
393
+ {
394
+ return msg->arena != NULL && msg->own == NULL && msg->stored == 0 &&
395
+ msg->arena->block != NULL && !msg->arena->no_grow;
396
+ }
397
+
398
+ void
399
+ pbw_value_destroy(pbw_value *value)
400
+ {
401
+ size_t i;
402
+
403
+ switch (value->kind) {
404
+ case PBW_VALUE_BYTES:
405
+ if ((value->flags & PBW_VF_ARENA) == 0) {
406
+ free(value->as.bytes.data);
407
+ }
408
+ break;
409
+ case PBW_VALUE_MESSAGE:
410
+ if (value->as.msg != NULL) {
411
+ pbw_message *m = value->as.msg;
412
+
413
+ value->as.msg = NULL;
414
+ if ((value->flags & PBW_VF_EMBEDDED) == 0 && m->stored) {
415
+ /* A wrapper the caller made around a message another tree
416
+ * owns: that tree releases it, not this wrapper. */
417
+ break;
418
+ }
419
+ /* An arena submessage's struct dies with the arena, but a heap
420
+ * payload stored into it (or into one of its children) does not:
421
+ * that walk is the only place it can be released. */
422
+ message_release_slot(m);
423
+ }
424
+ break;
425
+ case PBW_VALUE_LIST:
426
+ /* Items pushed into a list carry PBW_VF_EMBEDDED (pbw_value_free on
427
+ * a borrowed item is the documented no-op), so their wrappers are
428
+ * released here instead of through pbw_value_free. Arena items die
429
+ * with their arena. */
430
+ if (value->as.list.items != NULL) {
431
+ /* A list with items but no array is corrupt: nothing to walk. */
432
+ for (i = 0; i < value->as.list.len; i++) {
433
+ pbw_value *item = value->as.list.items[i];
434
+
435
+ if (item == NULL) {
436
+ continue;
437
+ }
438
+ if ((item->flags & PBW_VF_ARENA) != 0) {
439
+ /* The wrapper dies with the arena, but a heap payload
440
+ * stored into an arena message item must be released. */
441
+ if (item->kind == PBW_VALUE_MESSAGE &&
442
+ item->as.msg != NULL) {
443
+ message_release_slot(item->as.msg);
444
+ }
445
+ continue;
446
+ }
447
+ pbw_value_destroy(item);
448
+ free(item);
449
+ }
450
+ }
451
+ if ((value->flags & PBW_VF_ARENA) == 0) {
452
+ free(value->as.list.items);
453
+ }
454
+ break;
455
+ case PBW_VALUE_UINT:
456
+ default:
457
+ break;
458
+ }
459
+ }
460
+
461
+ void
462
+ pbw_value_free(pbw_value *value)
463
+ {
464
+ if (value == NULL || (value->flags & (PBW_VF_EMBEDDED | PBW_VF_ARENA)) != 0) {
465
+ /* An embedded value belongs to its message, an arena value to its
466
+ * context. */
467
+ return;
468
+ }
469
+ pbw_value_destroy(value);
470
+ free(value);
471
+ }
472
+
473
+ pbw_value *
474
+ pbw_value_new_uint(uint64_t bits)
475
+ {
476
+ return pbw_value_new_uint_a(NULL, bits);
477
+ }
478
+
479
+ pbw_value *
480
+ pbw_value_new_int(int64_t number)
481
+ {
482
+ return pbw_value_new_uint((uint64_t)number);
483
+ }
484
+
485
+ pbw_value *
486
+ pbw_value_new_bool(int flag)
487
+ {
488
+ return pbw_value_new_uint(flag ? 1u : 0u);
489
+ }
490
+
491
+ pbw_value *
492
+ pbw_value_new_float(float number)
493
+ {
494
+ uint32_t bits;
495
+
496
+ memcpy(&bits, &number, sizeof(bits));
497
+ return pbw_value_new_uint(bits);
498
+ }
499
+
500
+ pbw_value *
501
+ pbw_value_new_double(double number)
502
+ {
503
+ uint64_t bits;
504
+
505
+ memcpy(&bits, &number, sizeof(bits));
506
+ return pbw_value_new_uint(bits);
507
+ }
508
+
509
+ /* Always allocates len + 1 and terminates: string and bytes share one value
510
+ * kind, and decoded strings must be usable as C strings. */
511
+ int
512
+ pbw_value_init_bytes(pbw_value *value, pbw_arena *arena, const void *data,
513
+ size_t len)
514
+ {
515
+ /* Leave the value destructible on every failure path: the caller hands a
516
+ * failed init to pbw_value_free(). */
517
+ if (value == NULL) {
518
+ return 0;
519
+ }
520
+ value->kind = PBW_VALUE_BYTES;
521
+ value->flags = arena != NULL ? PBW_VF_ARENA : 0;
522
+ value->as.bytes.data = NULL;
523
+ value->as.bytes.len = 0;
524
+ if (len > SIZE_MAX - 8) {
525
+ /* len + 1 must fit and stay 8-byte alignable for the arena; there is no
526
+ * such buffer. */
527
+ return 0;
528
+ }
529
+ if (len > 0 && data == NULL) {
530
+ /* A NULL payload with a nonzero length would otherwise expose
531
+ * uninitialized heap/arena bytes on the wire. Zero it instead. */
532
+ data = pbw_empty_input;
533
+ /* Fall through to the zero-fill below. */
534
+ }
535
+ if (arena != NULL) {
536
+ value->as.bytes.data = (uint8_t *)pbw_arena_alloc(arena, len + 1);
537
+ } else {
538
+ value->as.bytes.data = (uint8_t *)malloc(len + 1);
539
+ }
540
+ if (value->as.bytes.data == NULL) {
541
+ return 0;
542
+ }
543
+ if (len > 0) {
544
+ if (data != NULL && data != pbw_empty_input) {
545
+ memcpy(value->as.bytes.data, data, len);
546
+ } else {
547
+ memset(value->as.bytes.data, 0, len);
548
+ }
549
+ }
550
+ value->as.bytes.data[len] = '\0';
551
+ value->as.bytes.len = len;
552
+ return 1;
553
+ }
554
+
555
+ pbw_value *
556
+ pbw_value_new_bytes(const void *data, size_t len)
557
+ {
558
+ return pbw_value_new_bytes_a(NULL, data, len);
559
+ }
560
+
561
+ pbw_value *
562
+ pbw_value_new_string(const char *str)
563
+ {
564
+ return str == NULL ? NULL : pbw_value_new_bytes(str, strlen(str));
565
+ }
566
+
567
+ pbw_value *
568
+ pbw_value_new_message(pbw_message *msg)
569
+ {
570
+ return pbw_value_new_message_a(NULL, msg);
571
+ }
572
+
573
+ int
574
+ pbw_value_list_reserve(pbw_value *list, uint32_t cap, pbw_arena *arena)
575
+ {
576
+ pbw_value **items;
577
+ size_t bytes;
578
+
579
+ if (list == NULL || list->kind != PBW_VALUE_LIST ||
580
+ cap <= list->as.list.cap) {
581
+ return list != NULL && list->kind == PBW_VALUE_LIST;
582
+ }
583
+ bytes = (size_t)cap * sizeof(*items);
584
+ if (bytes / sizeof(*items) != cap) {
585
+ return 0;
586
+ }
587
+ if (arena != NULL) {
588
+ /* Same as the push growth: no realloc in an arena, the old block is
589
+ * recycled by pbw_ctx_reset(). */
590
+ items = (pbw_value **)pbw_arena_alloc(arena, bytes);
591
+ if (items == NULL) {
592
+ return 0;
593
+ }
594
+ if (list->as.list.len > 0) {
595
+ memcpy(items, list->as.list.items,
596
+ list->as.list.len * sizeof(*items));
597
+ }
598
+ } else {
599
+ items = (pbw_value **)realloc(list->as.list.items, bytes);
600
+ if (items == NULL) {
601
+ return 0;
602
+ }
603
+ }
604
+ list->as.list.items = items;
605
+ list->as.list.cap = cap;
606
+ return 1;
607
+ }
608
+
609
+ pbw_value_kind
610
+ pbw_value_get_kind(const pbw_value *value)
611
+ {
612
+ return value == NULL ? PBW_VALUE_UINT : value->kind;
613
+ }
614
+
615
+ uint64_t
616
+ pbw_value_get_uint(const pbw_value *value)
617
+ {
618
+ return value == NULL ? 0 : value->as.u64;
619
+ }
620
+
621
+ int64_t
622
+ pbw_value_get_int(const pbw_value *value)
623
+ {
624
+ return (int64_t)pbw_value_get_uint(value);
625
+ }
626
+
627
+ int32_t
628
+ pbw_value_get_int32(const pbw_value *value)
629
+ {
630
+ return (int32_t)pbw_value_get_uint(value);
631
+ }
632
+
633
+ uint32_t
634
+ pbw_value_get_uint32(const pbw_value *value)
635
+ {
636
+ return (uint32_t)pbw_value_get_uint(value);
637
+ }
638
+
639
+ int
640
+ pbw_value_get_bool(const pbw_value *value)
641
+ {
642
+ return pbw_value_get_uint(value) != 0;
643
+ }
644
+
645
+ float
646
+ pbw_value_get_float(const pbw_value *value)
647
+ {
648
+ uint32_t bits = (uint32_t)pbw_value_get_uint(value);
649
+ float number;
650
+
651
+ memcpy(&number, &bits, sizeof(number));
652
+ return number;
653
+ }
654
+
655
+ double
656
+ pbw_value_get_double(const pbw_value *value)
657
+ {
658
+ uint64_t bits = pbw_value_get_uint(value);
659
+ double number;
660
+
661
+ memcpy(&number, &bits, sizeof(number));
662
+ return number;
663
+ }
664
+
665
+ const void *
666
+ pbw_value_get_bytes(const pbw_value *value, size_t *len)
667
+ {
668
+ if (value == NULL || value->kind != PBW_VALUE_BYTES) {
669
+ if (len != NULL) {
670
+ *len = 0;
671
+ }
672
+ return NULL;
673
+ }
674
+ if (len != NULL) {
675
+ *len = value->as.bytes.len;
676
+ }
677
+ return value->as.bytes.data;
678
+ }
679
+
680
+ const char *
681
+ pbw_value_get_string(const pbw_value *value)
682
+ {
683
+ return (const char *)pbw_value_get_bytes(value, NULL);
684
+ }
685
+
686
+ pbw_message *
687
+ pbw_value_get_message(const pbw_value *value)
688
+ {
689
+ return value != NULL && value->kind == PBW_VALUE_MESSAGE ? value->as.msg
690
+ : NULL;
691
+ }
692
+
693
+ size_t
694
+ pbw_value_count(const pbw_value *value)
695
+ {
696
+ return value != NULL && value->kind == PBW_VALUE_LIST
697
+ ? (size_t)value->as.list.len
698
+ : 0;
699
+ }
700
+
701
+ pbw_value *
702
+ pbw_value_at(const pbw_value *value, size_t index)
703
+ {
704
+ if (value == NULL || value->kind != PBW_VALUE_LIST ||
705
+ value->as.list.items == NULL || index >= value->as.list.len) {
706
+ /* A list with items but no array is corrupt: a failed lookup, not
707
+ * a NULL-before-array read. */
708
+ return NULL;
709
+ }
710
+ return value->as.list.items[index];
711
+ }
712
+
713
+ /* --------------------------------------------------------------- storage */
714
+
715
+ static int
716
+ value_matches(const pbw_field *field, const pbw_value *value)
717
+ {
718
+ switch (field->type) {
719
+ case PBW_TYPE_STRING:
720
+ case PBW_TYPE_BYTES:
721
+ return value->kind == PBW_VALUE_BYTES;
722
+ case PBW_TYPE_MESSAGE:
723
+ case PBW_TYPE_GROUP:
724
+ return value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL &&
725
+ (field->sub == NULL || value->as.msg->desc == field->sub);
726
+ default:
727
+ return value->kind == PBW_VALUE_UINT;
728
+ }
729
+ }
730
+
731
+ /* Drops the value of one field slot (no presence bookkeeping). */
732
+ static void
733
+ clear_slot(pbw_message *msg, size_t index)
734
+ {
735
+ if (pbw_msg_present(msg, index)) {
736
+ /* Absent before destroy, for the same reason the release walk does it:
737
+ * an arena message's struct outlives its payload. */
738
+ pbw_msg_set_present(msg, index, 0);
739
+ pbw_value_destroy(&msg->values[index]);
740
+ }
741
+ }
742
+
743
+ /* Copies value into a field slot and marks it present. Used by the decoder,
744
+ * which owns no value struct of its own. */
745
+ void
746
+ pbw_message_put(pbw_message *msg, size_t index, const pbw_value *value)
747
+ {
748
+ const pbw_field *field = &msg->desc->fields[index];
749
+
750
+ if (value->kind == PBW_VALUE_BYTES && value->as.bytes.len > 0 &&
751
+ value->as.bytes.data == NULL) {
752
+ /* Every constructor NUL-terminates and refuses this shape, so it can
753
+ * only arrive from a hand-built value: refuse it here instead of
754
+ * planting a payload every writer would read through NULL. Checked
755
+ * before the oneof update, so a rejected value cannot leave the
756
+ * group pointing at an absent field. */
757
+ return;
758
+ }
759
+ if (field->oneof_index > 0) {
760
+ uint32_t number = msg->oneof_case[field->oneof_index];
761
+
762
+ if (number != 0 && number != field->number) {
763
+ const pbw_field *previous = pbw_find_field(msg->desc, number);
764
+
765
+ if (previous == NULL &&
766
+ (msg->desc->schema == NULL ||
767
+ !msg->desc->schema->linked)) {
768
+ /* Unsorted rows: the binary search above cannot see them,
769
+ * so an old sibling would stay present beside the new one. */
770
+ size_t k;
771
+
772
+ for (k = 0; k < msg->desc->field_count; k++) {
773
+ if (msg->desc->fields[k].number == number) {
774
+ previous = &msg->desc->fields[k];
775
+ break;
776
+ }
777
+ }
778
+ }
779
+ if (previous != NULL) {
780
+ clear_slot(msg, value_field_index(msg, previous));
781
+ }
782
+ }
783
+ msg->oneof_case[field->oneof_index] = field->number;
784
+ }
785
+ clear_slot(msg, index);
786
+ msg->values[index] = *value;
787
+ /* Keep the caller's arena flag: a decoded payload lives in the arena, one
788
+ * the caller passed in is theirs to free with the message. */
789
+ msg->values[index].flags = value->flags | PBW_VF_EMBEDDED;
790
+ if (value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL) {
791
+ /* Owned now: storing the same sub-message twice would free it
792
+ * twice, so set_field() rejects it (see the stored check). */
793
+ value->as.msg->stored = 1;
794
+ }
795
+ pbw_msg_set_present(msg, index, 1);
796
+ }
797
+
798
+ /* Stores a value the caller owns: singular fields copy it into the slot and
799
+ * release the wrapper, repeated fields keep the value in their list. */
800
+ int
801
+ pbw_message_store(pbw_message *msg, const pbw_field *field, pbw_value *value)
802
+ {
803
+ if (msg->arena != NULL && (value->flags & PBW_VF_ARENA) == 0) {
804
+ /* Heap payload entering an arena tree: the bulk free fast path no
805
+ * longer applies, the release walk must run. */
806
+ msg->arena->has_heap = 1;
807
+ }
808
+ if (field->label == PBW_LABEL_REPEATED) {
809
+ size_t index = value_field_index(msg, field);
810
+ int fresh = !pbw_msg_present(msg, index);
811
+ pbw_value *list = pbw_message_list_for(msg, field);
812
+
813
+ if (!pbw_value_list_push(list, value, msg->arena)) {
814
+ /* Do not leave an empty list behind as a present field when the
815
+ * push is what failed: the caller keeps the value and the field
816
+ * stays absent, like a singular store that failed. */
817
+ if (fresh) {
818
+ clear_slot(msg, index);
819
+ }
820
+ return 0;
821
+ }
822
+ /* The message item is marked owned by pbw_value_list_push(). */
823
+ return 1;
824
+ }
825
+ pbw_message_put(msg, value_field_index(msg, field), value);
826
+ /* put() copied the payload into the slot (shared, not cloned): release
827
+ * only the wrapper. Arena wrappers die with their arena, which is what
828
+ * makes arena-built trees work. */
829
+ if ((value->flags & (PBW_VF_EMBEDDED | PBW_VF_ARENA)) == 0) {
830
+ free(value);
831
+ }
832
+ return 1;
833
+ }
834
+
835
+ /* Get-or-create the list storage of a repeated field. */
836
+ pbw_value *
837
+ pbw_message_list_for(pbw_message *msg, const pbw_field *field)
838
+ {
839
+ size_t index = value_field_index(msg, field);
840
+ pbw_value *list;
841
+
842
+ if (pbw_msg_present(msg, index)) {
843
+ list = &msg->values[index];
844
+ if (list->kind == PBW_VALUE_LIST) {
845
+ return list;
846
+ }
847
+ clear_slot(msg, index);
848
+ }
849
+ list = &msg->values[index];
850
+ list->kind = PBW_VALUE_LIST;
851
+ list->flags = PBW_VF_EMBEDDED | (msg->arena != NULL ? PBW_VF_ARENA : 0u);
852
+ list->as.list.items = NULL;
853
+ list->as.list.len = 0;
854
+ list->as.list.cap = 0;
855
+ pbw_msg_set_present(msg, index, 1);
856
+ return list;
857
+ }
858
+
859
+ void
860
+ pbw_message_clear(pbw_message *msg, uint32_t number)
861
+ {
862
+ const pbw_field *field;
863
+ size_t index;
864
+
865
+ if (msg == NULL || !pbw_msg_fresh(msg) ||
866
+ (msg->arena != NULL && msg->arena->readonly)) {
867
+ /* Context-owned messages are read-only; stale ones are dead. */
868
+ return;
869
+ }
870
+ field = pbw_descriptor_find_field(msg->desc, number);
871
+ if (field == NULL && msg->desc != NULL && msg->desc->schema != NULL &&
872
+ !msg->desc->schema->linked) {
873
+ /* An unlinked descriptor has no sorted rows or number table, so the
874
+ * lookup above refuses it; set_field() still accepts a field pointer,
875
+ * so clear has to work there too. Linear scan over the added rows. */
876
+ size_t i;
877
+
878
+ for (i = 0; i < msg->desc->field_count; i++) {
879
+ if (msg->desc->fields[i].number == number) {
880
+ field = &msg->desc->fields[i];
881
+ break;
882
+ }
883
+ }
884
+ }
885
+ if (field == NULL) {
886
+ return;
887
+ }
888
+ index = value_field_index(msg, field);
889
+ clear_slot(msg, index);
890
+ if (field->oneof_index > 0 &&
891
+ msg->oneof_case[field->oneof_index] == field->number) {
892
+ /* Only the member that is actually set clears the case: clearing
893
+ * an unset sibling must leave the active member addressable. */
894
+ msg->oneof_case[field->oneof_index] = 0;
895
+ }
896
+ }
897
+
898
+ size_t
899
+ pbw_message_unknown_len(const pbw_message *msg)
900
+ {
901
+ return msg == NULL ? 0 : msg->unknown_len;
902
+ }
903
+
904
+ const uint8_t *
905
+ pbw_message_unknown_bytes(const pbw_message *msg)
906
+ {
907
+ return msg == NULL ? NULL : msg->unknown;
908
+ }
909
+
910
+ pbw_status
911
+ pbw_message_add_unknown_bytes(pbw_message *msg, const void *data, size_t len)
912
+ {
913
+ if (msg == NULL || (data == NULL && len > 0)) {
914
+ return PBW_ERR_INVALID;
915
+ }
916
+ if (msg->arena != NULL && msg->arena->readonly) {
917
+ return PBW_ERR_INVALID;
918
+ }
919
+ return (pbw_status)pbw_message_add_unknown(msg, (const uint8_t *)data, len);
920
+ }
921
+
922
+ pbw_status
923
+ pbw_message_set(pbw_message *msg, uint32_t number, pbw_value *value)
924
+ {
925
+ const pbw_field *field;
926
+
927
+ if (msg == NULL || value == NULL || (value->flags & PBW_VF_EMBEDDED) != 0 ||
928
+ !pbw_msg_fresh(msg)) {
929
+ return PBW_ERR_INVALID;
930
+ }
931
+ if (msg->arena != NULL && msg->arena->readonly) {
932
+ /* Messages decoded through a context are read-only: a stored value would
933
+ * outlive the arena that owns its siblings. */
934
+ return PBW_ERR_INVALID;
935
+ }
936
+ field = msg->desc != NULL ? pbw_descriptor_find_field(msg->desc, number) : NULL;
937
+ if (field == NULL) {
938
+ return PBW_ERR_NOT_FOUND;
939
+ }
940
+ return pbw_message_set_field(msg, field, value);
941
+ }
942
+
943
+ pbw_status
944
+ pbw_message_set_field(pbw_message *msg, const pbw_field *field, pbw_value *value)
945
+ {
946
+ const pbw_field *base;
947
+
948
+ if (msg == NULL || value == NULL || (value->flags & PBW_VF_EMBEDDED) != 0 ||
949
+ !pbw_msg_fresh(msg) || field == NULL) {
950
+ return PBW_ERR_INVALID;
951
+ }
952
+ base = msg->desc->fields;
953
+ if (field < base || field >= base + msg->desc->field_count) {
954
+ return PBW_ERR_INVALID;
955
+ }
956
+ if (message_detached(msg)) {
957
+ /* pbw_message_clear() released it: nothing walks its slots any more, so
958
+ * a payload stored here (or a child created here) would leak. Store it
959
+ * back into a tree first; the same message is writable again afterwards. */
960
+ return PBW_ERR_INVALID;
961
+ }
962
+ if (msg->arena != NULL) {
963
+ if (msg->arena->readonly) {
964
+ return PBW_ERR_INVALID;
965
+ }
966
+ if ((msg->arena->block == NULL || msg->arena->no_grow) &&
967
+ (value->flags & PBW_VF_ARENA) == 0) {
968
+ /* A context arena (heap-grown or caller memory) releases by reset,
969
+ * so it cannot free a heap payload: only values built in that arena
970
+ * (pbw_value_new_*_ctx) can be stored. A decoded tree's own packed
971
+ * arena can: pbw_message_free() walks its slots (has_heap). */
972
+ return PBW_ERR_INVALID;
973
+ }
974
+ } else if ((value->flags & PBW_VF_ARENA) != 0) {
975
+ /* A heap tree cannot hold a context-arena payload: the next reset
976
+ * would free it under the tree. */
977
+ return PBW_ERR_INVALID;
978
+ }
979
+ if (msg->arena != NULL && (value->flags & PBW_VF_ARENA) != 0) {
980
+ /* Same arena only: a value from another arena dies with its own
981
+ * reset, and a value from before this arena's reset is already
982
+ * dead (owns() sees only live bytes, below `used`). The wrapper
983
+ * itself counts too: the repeated path keeps the wrapper pointer,
984
+ * so a foreign wrapper would dangle even when it carries no
985
+ * payload (plain integers). */
986
+ const void *payload = NULL;
987
+
988
+ if (!pbw_arena_owns(msg->arena, value)) {
989
+ return PBW_ERR_INVALID;
990
+ }
991
+ if (value->kind == PBW_VALUE_BYTES) {
992
+ payload = value->as.bytes.data;
993
+ } else if (value->kind == PBW_VALUE_MESSAGE) {
994
+ payload = value->as.msg;
995
+ } else if (value->kind == PBW_VALUE_LIST) {
996
+ payload = value->as.list.items;
997
+ }
998
+ if (!pbw_arena_owns(msg->arena, payload)) {
999
+ return PBW_ERR_INVALID;
1000
+ }
1001
+ }
1002
+ if (value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL &&
1003
+ value->as.msg->stored) {
1004
+ /* Already owned by another message: sharing it would free it twice. */
1005
+ return PBW_ERR_INVALID;
1006
+ }
1007
+ if (value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL) {
1008
+ pbw_message *kid = value->as.msg;
1009
+
1010
+ /* No message may become its own ancestor: clear() or free() would
1011
+ * then release the arena (or struct) it is walking inside. The two
1012
+ * cheap cases are the message itself and the root of the decoded tree
1013
+ * this message lives in (they share the arena). */
1014
+ if (kid == msg ||
1015
+ (msg->arena != NULL && kid->own == msg->arena)) {
1016
+ return PBW_ERR_INVALID;
1017
+ }
1018
+ /* The remaining way to close a cycle is an unowned root attached into
1019
+ * a nested node; that root's tree is the only place the target could
1020
+ * be, so one walk over it answers exactly. */
1021
+ if (kid->stored == 0 && msg->stored != 0 &&
1022
+ message_contains(kid, msg, 0)) {
1023
+ return PBW_ERR_INVALID;
1024
+ }
1025
+ }
1026
+ if (value->kind == PBW_VALUE_MESSAGE && value->as.msg != NULL &&
1027
+ value->as.msg->arena != NULL) {
1028
+ /* A child with an arena must die with this tree, or a reset frees it
1029
+ * under the parent. Two ways that holds: it shares this tree's arena
1030
+ * (checked above when the wrapper is an arena value), or it owns its
1031
+ * arena (a decoded root) and this tree walks its slots on free -- a
1032
+ * heap tree, or a decoded tree's packed arena, where a heap payload
1033
+ * arms the walk (pbw_message_store()). A context or caller-memory
1034
+ * arena never walks (reset is a pointer move), so an adopted root's
1035
+ * arena would leak there. */
1036
+ pbw_message *kid = value->as.msg;
1037
+ int shared = msg->arena != NULL && pbw_arena_owns(msg->arena, kid);
1038
+ int walks = msg->arena == NULL ||
1039
+ (msg->arena->block != NULL && !msg->arena->no_grow);
1040
+
1041
+ if (!shared && (kid->own == NULL || !walks)) {
1042
+ return PBW_ERR_INVALID;
1043
+ }
1044
+ }
1045
+ if (value->kind == PBW_VALUE_BYTES && !pbw_bytes_readable(value)) {
1046
+ /* A NULL pointer with a length can only arrive from a hand-built
1047
+ * value (the constructors refuse it): the slot copy below would
1048
+ * silently drop it while reporting success, and the repeated path
1049
+ * would plant an item every writer refuses. Fail it here instead. */
1050
+ return PBW_ERR_INVALID;
1051
+ }
1052
+ if (!value_matches(field, value)) {
1053
+ return PBW_ERR_TYPE;
1054
+ }
1055
+ if (!pbw_message_store(msg, field, value)) {
1056
+ return PBW_ERR_NOMEM;
1057
+ }
1058
+ return PBW_OK;
1059
+ }
1060
+
1061
+ pbw_message *
1062
+ pbw_message_set_message(pbw_message *msg, uint32_t number)
1063
+ {
1064
+ const pbw_field *field;
1065
+ pbw_value *value;
1066
+ pbw_message *nested;
1067
+
1068
+ if (msg == NULL || !pbw_msg_fresh(msg) || message_detached(msg) ||
1069
+ (msg->arena != NULL && msg->arena->readonly)) {
1070
+ return NULL;
1071
+ }
1072
+ field = pbw_descriptor_find_field(msg->desc, number);
1073
+ if (field == NULL ||
1074
+ (field->type != PBW_TYPE_MESSAGE && field->type != PBW_TYPE_GROUP) ||
1075
+ field->sub == NULL) {
1076
+ return NULL;
1077
+ }
1078
+ if (field->label != PBW_LABEL_REPEATED) {
1079
+ size_t index = value_field_index(msg, field);
1080
+
1081
+ if (pbw_msg_present(msg, index) &&
1082
+ msg->values[index].kind == PBW_VALUE_MESSAGE) {
1083
+ return msg->values[index].as.msg;
1084
+ }
1085
+ }
1086
+ nested = pbw_message_new_a(msg->arena, field->sub);
1087
+ if (nested == NULL) {
1088
+ return NULL;
1089
+ }
1090
+ value = pbw_value_new_message_a(msg->arena, nested);
1091
+ if (value == NULL) {
1092
+ pbw_message_free(nested);
1093
+ return NULL;
1094
+ }
1095
+ if (!pbw_message_store(msg, field, value)) {
1096
+ pbw_value_free(value);
1097
+ return NULL;
1098
+ }
1099
+ return nested;
1100
+ }