include/punk_observe/po_attr.h

filter: Statementstmt 83.8% 145 / 173 Branchbran 45.3% 87 / 192 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.5% 232 / 365 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_json_in.h ↑ Summary xs/scan.xs »
1 /* po_attr.h - OTLP AnyValue, flattened and sorted.
2 *
3 * An OTLP attribute value is a tagged union: string, bool, int, double,
4 * bytes, array or kvlist. Semantic conventions use the array and map forms in
5 * practice, so a store that keeps only string attributes drops real data.
6 *
7 * Two jobs here, and the second one is the load-bearing one.
8 *
9 * FLATTEN. Scalars encode to a tag byte plus a value. An array becomes
10 * key.0, key.1, ...; a map becomes key.sub, recursively, to a depth of four,
11 * below which the remaining subtree is kept as its own encoded bytes rather
12 * than expanded. Depth is bounded because the input is untrusted and a
13 * thousand nested kvlists would otherwise recurse the C stack to death.
14 *
15 * SORT. Two spans with the same attributes given in different orders MUST
16 * produce byte-identical blocks. Phase 4 derives a series id by hashing this
17 * block, with no coordination between workers - so if the order leaks into
18 * the bytes, the same series gets two ids, storage doubles, and the
19 * cardinality cap counts every series twice. The sort is not a tidiness
20 * measure; it is what makes a content-derived identity possible at all.
21 */
22 #ifndef PO_ATTR_H
23 #define PO_ATTR_H
24  
25 #include "punk_observe/po_compat.h"
26 #include "punk_observe/po_pb_read.h"
27 #include "punk_observe/po_rec.h"
28 #include "otel_proto.h" /* PB_ANYVALUE_*, PB_KEYVALUE_*, PB_KVLIST_* */
29  
30 #include <stdlib.h> /* qsort */
31  
32 #define PO_ATTR_MAX_DEPTH 4
33 #define PO_ATTR_MAX 128 /* OTLP's default attribute count limit */
34 #define PO_ATTR_KEYMAX 256
35  
36 /* value tags, one byte on the wire in the arena */
37 #define PO_AV_STRING 1
38 #define PO_AV_BOOL 2
39 #define PO_AV_INT 3
40 #define PO_AV_DOUBLE 4
41 #define PO_AV_BYTES 5
42 #define PO_AV_NESTED 6 /* kept encoded: past the depth bound */
43  
44 typedef struct {
45 char key[PO_ATTR_KEYMAX];
46 size_t klen;
47 uint8_t tag;
48 const uint8_t *sp; /* borrowed: string/bytes/nested */
49 size_t slen;
50 po_u64 u; /* int (two's complement) or bool */
51 double d;
52 } po_attr;
53  
54 typedef struct {
55 po_attr a[PO_ATTR_MAX];
56 int n;
57 int dropped; /* over the limit; OTLP wants the count */
58 } po_attrs;
59  
60 12551 static void po_attrs_init(po_attrs *s) { s->n = 0; s->dropped = 0; }
61  
62 /* Append with the key prefix already built. Refuses silently past the limit
63 * and counts it, which is what OTLP's dropped_attributes_count is for: a
64 * silently truncated attribute set is worse than a visibly truncated one. */
65 7580 static po_attr *po_attrs_push(po_attrs *s, const char *key, size_t klen) {
66 po_attr *a;
67 7580 ▶ if (s->n >= PO_ATTR_MAX || klen >= PO_ATTR_KEYMAX) { s->dropped++; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (s->n >= PO_ATTR_MAX || klen >= PO_ATTR_KEYMAX) { s->dropped++; return NULL; } 7580 0 7580
if (s->n >= PO_ATTR_MAX || klen >= PO_ATTR_KEYMAX) { s->dropped++; return NULL; } 0 7580 7580
68 7580 a = &s->a[s->n++];
69 7580 memcpy(a->key, key, klen);
70 7580 a->klen = klen;
71 7580 a->tag = 0; a->sp = NULL; a->slen = 0; a->u = 0; a->d = 0;
72 7580 return a;
73 }
74  
75 /* Build "<prefix>.<suffix>" into out, returning its length, or 0 if it would
76 * not fit. Used for both the array index and the map key. */
77 80 static size_t po_attr_join(char *out, size_t cap,
78 const char *pfx, size_t plen,
79 const char *sfx, size_t slen) {
80 80 ▶ if (plen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (plen == 0) { 71 9 80
81 71 ▶ if (slen >= cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (slen >= cap) return 0; 0 71 71
82 71 memcpy(out, sfx, slen);
83 71 return slen;
84 }
85 9 ▶ if (plen + 1 + slen >= cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (plen + 1 + slen >= cap) return 0; 0 9 9
86 9 memcpy(out, pfx, plen);
87 9 out[plen] = '.';
88 9 memcpy(out + plen + 1, sfx, slen);
89 9 return plen + 1 + slen;
90 }
91  
92 static int po_attr_value(po_pbr *r, po_attrs *s,
93 const char *key, size_t klen, int depth);
94  
95 /* One KeyValue: key at field 1, value at field 2. */
96 78 static int po_attr_kv(po_pbr *r, po_attrs *s,
97 const char *pfx, size_t plen, int depth) {
98 uint32_t f, w;
99 78 const uint8_t *kp = NULL; size_t kl = 0;
100 78 po_pbr vsub; int have_value = 0;
101 char key[PO_ATTR_KEYMAX];
102 size_t keylen;
103  
104 /* The value can precede the key on the wire, so both are collected before
105 * either is used. Field order within a message is not guaranteed. */
106 234 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 156 78 234
107 156 ▶ if (f == PB_KEYVALUE_KEY && w == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (f == PB_KEYVALUE_KEY && w == PO_PB_BYTES) { 78 78 156
if (f == PB_KEYVALUE_KEY && w == PO_PB_BYTES) { 78 0 78
108 78 ▶ if (!po_pbr_bytes(r, &kp, &kl)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &kp, &kl)) return 0; 0 78 78
109 }
110 78 ▶ else if (f == PB_KEYVALUE_VALUE && w == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
else if (f == PB_KEYVALUE_VALUE && w == PO_PB_BYTES) { 78 0 78
else if (f == PB_KEYVALUE_VALUE && w == PO_PB_BYTES) { 78 0 78
111 78 ▶ if (!po_pbr_sub(r, &vsub)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &vsub)) return 0; 0 78 78
112 78 have_value = 1;
113 }
114 0 ▶ else if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(r, w)) return 0; 0 0 0
115 }
116 78 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 78 78
117 78 ▶ if (!kp || !have_value) return 1; /* an incomplete pair is skipped */
Branch: 2 branches
BranchTrueFalseTotal
if (!kp || !have_value) return 1; /* an incomplete pair is skipped */ 78 0 78
if (!kp || !have_value) return 1; /* an incomplete pair is skipped */ 0 78 78
118  
119 78 keylen = po_attr_join(key, sizeof(key), pfx, plen, (const char *)kp, kl);
120 78 ▶ if (!keylen) { s->dropped++; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!keylen) { s->dropped++; return 1; } 0 78 78
121 78 return po_attr_value(&vsub, s, key, keylen, depth);
122 }
123  
124 /* One AnyValue. */
125 81 static int po_attr_value(po_pbr *r, po_attrs *s,
126 const char *key, size_t klen, int depth) {
127 uint32_t f, w;
128 81 const uint8_t *start = r->p;
129 81 size_t whole = (size_t)(r->end - r->p);
130  
131 161 ▶ while (po_pbr_next(r, &f, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(r, &f, &w)) { 81 80 161
132 81 switch (f) {
133 66 case PB_ANYVALUE_STRING: {
134 const uint8_t *p; size_t n; po_attr *a;
135 132 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 66 66
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
136 66 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 66 66
137 66 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 66 0 66
138 66 { a->tag = PO_AV_STRING; a->sp = p; a->slen = n; }
139 66 break;
140 }
141 1 case PB_ANYVALUE_BOOL: {
142 po_u64 v; po_attr *a;
143 2 ▶ if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
144 1 ▶ if (!po_pbr_varint(r, &v)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &v)) return 0; 0 1 1
145 1 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 1 0 1
146 1 { a->tag = PO_AV_BOOL; a->u = v ? 1 : 0; }
147 1 break;
148 }
149 5 case PB_ANYVALUE_INT: {
150 po_u64 v; po_attr *a;
151 10 ▶ if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 5 5
if (w != PO_PB_VARINT) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
152 5 ▶ if (!po_pbr_varint(r, &v)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_varint(r, &v)) return 0; 0 5 5
153 5 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 5 0 5
154 5 { a->tag = PO_AV_INT; a->u = v; } /* two's complement */
155 5 break;
156 }
157 1 case PB_ANYVALUE_DOUBLE: {
158 double d; po_attr *a;
159 2 ▶ if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_FIXED64) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
160 1 ▶ if (!po_pbr_double(r, &d)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_double(r, &d)) return 0; 0 1 1
161 1 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 1 0 1
162 1 { a->tag = PO_AV_DOUBLE; a->d = d; }
163 1 break;
164 }
165 0 case PB_ANYVALUE_BYTES: {
166 const uint8_t *p; size_t n; po_attr *a;
167 0 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
168 0 ▶ if (!po_pbr_bytes(r, &p, &n)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_bytes(r, &p, &n)) return 0; 0 0 0
169 0 ▶ if ((a = po_attrs_push(s, key, klen)))
Branch: 1 branch
BranchTrueFalseTotal
if ((a = po_attrs_push(s, key, klen))) 0 0 0
170 0 { a->tag = PO_AV_BYTES; a->sp = p; a->slen = n; }
171 0 break;
172 }
173 1 case PB_ANYVALUE_ARRAY: {
174 1 po_pbr arr; uint32_t af, aw; int idx = 0;
175 2 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 1 1
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
176 1 ▶ if (!po_pbr_sub(r, &arr)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &arr)) return 0; 0 1 1
177 1 ▶ if (depth >= PO_ATTR_MAX_DEPTH) goto keep_nested;
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= PO_ATTR_MAX_DEPTH) goto keep_nested; 0 1 1
178 3 ▶ while (po_pbr_next(&arr, &af, &aw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&arr, &af, &aw)) { 2 1 3
179 4 ▶ if (af == PB_ARRAYVALUE_VALUES && aw == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (af == PB_ARRAYVALUE_VALUES && aw == PO_PB_BYTES) { 2 0 2
if (af == PB_ARRAYVALUE_VALUES && aw == PO_PB_BYTES) { 2 0 2
180 po_pbr el; char k[PO_ATTR_KEYMAX]; char num[16];
181 2 size_t nl = 0, kl2; int t = idx++;
182 /* decimal index, written backwards then reversed */
183 2 ▶ if (t == 0) num[nl++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (t == 0) num[nl++] = '0'; 1 1 2
184 1 else { char tmp[16]; size_t tn = 0;
185 2 ▶ while (t) { tmp[tn++] = (char)('0' + t % 10); t /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (t) { tmp[tn++] = (char)('0' + t % 10); t /= 10; } 1 1 2
186 2 ▶ while (tn) num[nl++] = tmp[--tn]; }
Branch: 1 branch
BranchTrueFalseTotal
while (tn) num[nl++] = tmp[--tn]; } 1 1 2
187 2 ▶ if (!po_pbr_sub(&arr, &el)) { po_pbr_join(r, &arr); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&arr, &el)) { po_pbr_join(r, &arr); return 0; } 0 2 2
188 2 kl2 = po_attr_join(k, sizeof(k), key, klen, num, nl);
189 2 ▶ if (!kl2) { s->dropped++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!kl2) { s->dropped++; continue; } 0 2 2
190 2 ▶ if (!po_attr_value(&el, s, k, kl2, depth + 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attr_value(&el, s, k, kl2, depth + 1)) 0 2 2
191 0 { po_pbr_join(r, &arr); return 0; }
192 }
193 0 ▶ else if (!po_pbr_skip(&arr, aw)) { po_pbr_join(r, &arr); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&arr, aw)) { po_pbr_join(r, &arr); return 0; } 0 0 0
194 }
195 1 po_pbr_join(r, &arr);
196 1 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 1 1
197 1 break;
198 }
199 7 case PB_ANYVALUE_KVLIST: {
200 po_pbr kvl; uint32_t kf, kw;
201 13 ▶ if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 7 7
if (w != PO_PB_BYTES) { if (!po_pbr_skip(r, w)) return 0; break; } 0 0 0
202 7 ▶ if (!po_pbr_sub(r, &kvl)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &kvl)) return 0; 0 7 7
203 7 ▶ if (depth >= PO_ATTR_MAX_DEPTH) goto keep_nested;
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= PO_ATTR_MAX_DEPTH) goto keep_nested; 1 6 7
204 13 ▶ while (po_pbr_next(&kvl, &kf, &kw)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_pbr_next(&kvl, &kf, &kw)) { 7 6 13
205 14 ▶ if (kf == PB_KVLIST_VALUES && kw == PO_PB_BYTES) {
Branch: 2 branches
BranchTrueFalseTotal
if (kf == PB_KVLIST_VALUES && kw == PO_PB_BYTES) { 7 0 7
if (kf == PB_KVLIST_VALUES && kw == PO_PB_BYTES) { 7 0 7
206 po_pbr kv;
207 7 ▶ if (!po_pbr_sub(&kvl, &kv)) { po_pbr_join(r, &kvl); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(&kvl, &kv)) { po_pbr_join(r, &kvl); return 0; } 0 7 7
208 7 ▶ if (!po_attr_kv(&kv, s, key, klen, depth + 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attr_kv(&kv, s, key, klen, depth + 1)) 0 7 7
209 0 { po_pbr_join(r, &kvl); return 0; }
210 }
211 0 ▶ else if (!po_pbr_skip(&kvl, kw)) { po_pbr_join(r, &kvl); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_pbr_skip(&kvl, kw)) { po_pbr_join(r, &kvl); return 0; } 0 0 0
212 }
213 6 po_pbr_join(r, &kvl);
214 6 ▶ if (r->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->err) return 0; 0 6 6
215 6 break;
216 }
217 0 default:
218 0 ▶ if (!po_pbr_skip(r, w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_skip(r, w)) return 0; 0 0 0
219 }
220 }
221 80 return r->err ? 0 : 1;
222  
223 1 keep_nested: {
224 /* Past the depth bound: keep the subtree's own bytes rather than
225 * expanding it. The data is preserved and searchable as an opaque
226 * value, and the recursion stops. */
227 1 po_attr *a = po_attrs_push(s, key, klen);
228 1 ▶ if (a) { a->tag = PO_AV_NESTED; a->sp = start; a->slen = whole; }
Branch: 1 branch
BranchTrueFalseTotal
if (a) { a->tag = PO_AV_NESTED; a->sp = start; a->slen = whole; } 1 0 1
229 1 r->p = r->end;
230 1 return 1;
231 }
232 }
233  
234 /* Sort by key, then by tag, then by value bytes. A total order: two attribute
235 * sets that differ only in the order they were given must compare equal, and
236 * two that differ in any content must not. */
237 651 static int po_attr_cmp(const void *va, const void *vb) {
238 651 const po_attr *a = (const po_attr *)va, *b = (const po_attr *)vb;
239 651 size_t n = a->klen < b->klen ? a->klen : b->klen;
240 651 int c = memcmp(a->key, b->key, n);
241 651 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 651 0 651
242 0 ▶ if (a->klen != b->klen) return a->klen < b->klen ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->klen != b->klen) return a->klen < b->klen ? -1 : 1; 0 0 0
if (a->klen != b->klen) return a->klen < b->klen ? -1 : 1; 0 0 0
243 0 ▶ if (a->tag != b->tag) return a->tag < b->tag ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->tag != b->tag) return a->tag < b->tag ? -1 : 1; 0 0 0
if (a->tag != b->tag) return a->tag < b->tag ? -1 : 1; 0 0 0
244 0 switch (a->tag) {
245 0 case PO_AV_STRING: case PO_AV_BYTES: case PO_AV_NESTED: {
246 0 size_t m = a->slen < b->slen ? a->slen : b->slen;
247 0 ▶ c = m ? memcmp(a->sp, b->sp, m) : 0;
Branch: 1 branch
BranchTrueFalseTotal
c = m ? memcmp(a->sp, b->sp, m) : 0; 0 0 0
248 0 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 0 0 0
249 0 ▶ return a->slen == b->slen ? 0 : (a->slen < b->slen ? -1 : 1);
Branch: 2 branches
BranchTrueFalseTotal
return a->slen == b->slen ? 0 : (a->slen < b->slen ? -1 : 1); 0 0 0
return a->slen == b->slen ? 0 : (a->slen < b->slen ? -1 : 1); 0 0 0
250 }
251 0 case PO_AV_DOUBLE:
252 /* Bit pattern, not value: -0.0 and 0.0 are different attributes,
253 * and NaN must order consistently rather than compare unequal to
254 * itself and make the sort unstable. */
255 0 return memcmp(&a->d, &b->d, sizeof(double));
256 0 default:
257 0 ▶ return a->u == b->u ? 0 : (a->u < b->u ? -1 : 1);
Branch: 2 branches
BranchTrueFalseTotal
return a->u == b->u ? 0 : (a->u < b->u ? -1 : 1); 0 0 0
return a->u == b->u ? 0 : (a->u < b->u ? -1 : 1); 0 0 0
258 }
259 }
260  
261 7095 static void po_attrs_sort(po_attrs *s) {
262 7095 ▶ if (s->n > 1) qsort(s->a, (size_t)s->n, sizeof(po_attr), po_attr_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (s->n > 1) qsort(s->a, (size_t)s->n, sizeof(po_attr), po_attr_cmp); 622 6473 7095
263 7095 }
264  
265 /* Serialise the sorted set into the arena as a self-contained block:
266 *
267 * [varint count] ( [varint klen][key][tag][value] )*
268 *
269 * Deterministic by construction, so hashing it is hashing the attribute set.
270 * Returns the offset, or PO_ARENA_ERR. */
271 7095 static uint32_t po_attrs_encode(po_attrs *s, po_arena *ar, uint32_t *len_out) {
272 char hdr[16];
273 7095 size_t hn = 0;
274 uint32_t off, i;
275 7095 po_u64 n = (po_u64)s->n;
276  
277 7095 po_attrs_sort(s);
278  
279 7095 ▶ do { hdr[hn] = (char)((n & 0x7F) | (n > 0x7F ? 0x80 : 0)); n >>= 7; hn++; }
Branch: 1 branch
BranchTrueFalseTotal
do { hdr[hn] = (char)((n & 0x7F) | (n > 0x7F ? 0x80 : 0)); n >>= 7; hn++; } 0 7095 7095
280 7095 ▶ while (n);
Branch: 1 branch
BranchTrueFalseTotal
while (n); 0 7095 7095
281 7095 off = po_arena_put(ar, hdr, hn);
282 7095 ▶ if (off == PO_ARENA_ERR) return PO_ARENA_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (off == PO_ARENA_ERR) return PO_ARENA_ERR; 0 7095 7095
283  
284 14789 ▶ for (i = 0; i < (uint32_t)s->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (uint32_t)s->n; i++) { 7694 7095 14789
285 7694 const po_attr *a = &s->a[i];
286 7694 char b[16]; size_t bn = 0; po_u64 k = (po_u64)a->klen;
287 7694 char tag = (char)a->tag;
288 7694 ▶ do { b[bn] = (char)((k & 0x7F) | (k > 0x7F ? 0x80 : 0)); k >>= 7; bn++; }
Branch: 1 branch
BranchTrueFalseTotal
do { b[bn] = (char)((k & 0x7F) | (k > 0x7F ? 0x80 : 0)); k >>= 7; bn++; } 0 7694 7694
289 7694 ▶ while (k);
Branch: 1 branch
BranchTrueFalseTotal
while (k); 0 7694 7694
290 7694 ▶ if (po_arena_put(ar, b, bn) == PO_ARENA_ERR) return PO_ARENA_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (po_arena_put(ar, b, bn) == PO_ARENA_ERR) return PO_ARENA_ERR; 0 7694 7694
291 7694 ▶ if (po_arena_put(ar, a->key, a->klen) == PO_ARENA_ERR) return PO_ARENA_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (po_arena_put(ar, a->key, a->klen) == PO_ARENA_ERR) return PO_ARENA_ERR; 0 7694 7694
292 7694 ▶ if (po_arena_put(ar, &tag, 1) == PO_ARENA_ERR) return PO_ARENA_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (po_arena_put(ar, &tag, 1) == PO_ARENA_ERR) return PO_ARENA_ERR; 0 7694 7694
293 7694 switch (a->tag) {
294 7622 case PO_AV_STRING: case PO_AV_BYTES: case PO_AV_NESTED: {
295 7622 char l[16]; size_t ln = 0; po_u64 v = (po_u64)a->slen;
296 7624 ▶ do { l[ln] = (char)((v & 0x7F) | (v > 0x7F ? 0x80 : 0)); v >>= 7; ln++; }
Branch: 1 branch
BranchTrueFalseTotal
do { l[ln] = (char)((v & 0x7F) | (v > 0x7F ? 0x80 : 0)); v >>= 7; ln++; } 2 7622 7624
297 7624 ▶ while (v);
Branch: 1 branch
BranchTrueFalseTotal
while (v); 2 7622 7624
298 7622 ▶ if (po_arena_put(ar, l, ln) == PO_ARENA_ERR) return PO_ARENA_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (po_arena_put(ar, l, ln) == PO_ARENA_ERR) return PO_ARENA_ERR; 0 7622 7622
299 7622 ▶ if (a->slen && po_arena_put(ar, (const char *)a->sp, a->slen)
Branch: 2 branches
BranchTrueFalseTotal
if (a->slen && po_arena_put(ar, (const char *)a->sp, a->slen) 7607 15 7622
if (a->slen && po_arena_put(ar, (const char *)a->sp, a->slen) 0 7607 7607
300 0 == PO_ARENA_ERR) return PO_ARENA_ERR;
301 7622 break;
302 }
303 2 case PO_AV_DOUBLE: {
304 2 po_u64 bits; memcpy(&bits, &a->d, 8); bits = po_le64(bits);
305 2 ▶ if (po_arena_put(ar, (const char *)&bits, 8) == PO_ARENA_ERR)
Branch: 1 branch
BranchTrueFalseTotal
if (po_arena_put(ar, (const char *)&bits, 8) == PO_ARENA_ERR) 0 2 2
306 0 return PO_ARENA_ERR;
307 2 break;
308 }
309 70 default: {
310 70 po_u64 v = po_le64(a->u);
311 70 ▶ if (po_arena_put(ar, (const char *)&v, 8) == PO_ARENA_ERR)
Branch: 1 branch
BranchTrueFalseTotal
if (po_arena_put(ar, (const char *)&v, 8) == PO_ARENA_ERR) 0 70 70
312 0 return PO_ARENA_ERR;
313 70 break;
314 }
315 }
316 }
317 7095 *len_out = (uint32_t)(ar->len - off);
318 7095 return off;
319 }
320  
321 /* Read a repeated KeyValue field (resource, scope, span or log attributes). */
322 71 static int po_attrs_read(po_pbr *r, po_attrs *s) {
323 po_pbr kv;
324 71 ▶ if (!po_pbr_sub(r, &kv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_pbr_sub(r, &kv)) return 0; 0 71 71
325 71 ▶ if (!po_attr_kv(&kv, s, NULL, 0, 0)) { po_pbr_join(r, &kv); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_attr_kv(&kv, s, NULL, 0, 0)) { po_pbr_join(r, &kv); return 0; } 0 71 71
326 71 po_pbr_join(r, &kv);
327 71 return !r->err;
328 }
329  
330 #endif /* PO_ATTR_H */
« include/punk_observe/po_json_in.h ↑ Summary xs/scan.xs »