16static void put(
pc_span *w, uint8_t b)
24static void head(
pc_span *w, uint8_t major, uint64_t val)
26 uint8_t m = (uint8_t)(major << 5);
29 put(w, (uint8_t)(m | val));
31 else if (val < 0x100ULL)
33 put(w, (uint8_t)(m | 24));
36 else if (val < 0x10000ULL)
38 put(w, (uint8_t)(m | 25));
41 else if (val < 0x100000000ULL)
43 put(w, (uint8_t)(m | 26));
48 put(w, (uint8_t)(m | 27));
53void pc_cbor_uint(
pc_span *w, uint64_t v)
58void pc_cbor_int(
pc_span *w, int64_t v)
62 head(w, 0, (uint64_t)v);
66 head(w, 1, (uint64_t)(-1 - v));
70void pc_cbor_bytes(
pc_span *w,
const uint8_t *data,
size_t len)
72 head(w, 2, (uint64_t)len);
73 for (
size_t i = 0; i < len; i++)
79void pc_cbor_str_n(
pc_span *w,
const char *s,
size_t len)
81 head(w, 3, (uint64_t)len);
82 for (
size_t i = 0; i < len; i++)
84 put(w, (uint8_t)s[i]);
88void pc_cbor_str(
pc_span *w,
const char *s)
90 pc_cbor_str_n(w, s, s ? strnlen(s, w->
cap + 1) : 0);
93void pc_cbor_bool(
pc_span *w,
bool b)
95 put(w, b ? 0xf5 : 0xf4);
103void pc_cbor_float(
pc_span *w,
float f)
106 memcpy(&bits, &f,
sizeof(bits));
111void pc_cbor_array(
pc_span *w,
size_t count)
113 head(w, 4, (uint64_t)count);
116void pc_cbor_map(
pc_span *w,
size_t count)
118 head(w, 5, (uint64_t)count);
121void pc_cbor_label(
pc_span *w,
const char *name, int64_t num)
133static bool read_head(
pc_cspan *r, uint8_t *major, uint64_t *val)
140 uint8_t b = r->
buf[r->
pos];
141 uint8_t info = (uint8_t)(b & 0x1f);
142 *major = (uint8_t)(b >> 5);
178 uint8_t b = r->
buf[r->
pos];
194 uint8_t info = (uint8_t)(b & 0x1f);
195 if (info == 20 || info == 21)
203 if (info == 26 || info == 27)
214bool pc_cbor_read_uint(
pc_cspan *r, uint64_t *out)
218 if (!read_head(r, &m, &v))
231bool pc_cbor_read_int(
pc_cspan *r, int64_t *out)
235 if (!read_head(r, &m, &v))
245 *out = -1 - (int64_t)v;
255bool pc_cbor_read_bool(
pc_cspan *r,
bool *out)
262 uint8_t b = r->
buf[r->
pos];
291bool pc_cbor_read_float(
pc_cspan *r,
float *out)
298 uint8_t b = r->
buf[r->
pos];
306 uint32_t bits = (uint32_t)v;
307 memcpy(out, &bits,
sizeof(*out));
318 memcpy(&d, &bits,
sizeof(d));
327static bool read_str(
pc_cspan *r, uint8_t want_major,
const uint8_t **out,
size_t *len)
331 if (!read_head(r, &m, &v))
335 if (m != want_major || r->
pos + v > r->
len)
346bool pc_cbor_read_str(
pc_cspan *r,
const char **out,
size_t *len)
348 return read_str(r, 3, (
const uint8_t **)out, len);
351bool pc_cbor_read_bytes(
pc_cspan *r,
const uint8_t **out,
size_t *len)
353 return read_str(r, 2, out, len);
356bool pc_cbor_read_array(
pc_cspan *r,
size_t *count)
360 if (!read_head(r, &m, &v))
373bool pc_cbor_read_map(
pc_cspan *r,
size_t *count)
377 if (!read_head(r, &m, &v))
The byte verbs - append into a pc_span, take out of a pc_cspan.
void pc_bw_put_be(pc_span *w, uint64_t val, int32_t nbytes)
Append the low nbytes of val, big-endian (network order).
void pc_bw_put(pc_span *w, uint8_t b)
Append one byte; on overflow set the flag but keep counting pos.
bool pc_br_take_be(pc_cspan *r, size_t nbytes, uint64_t *out)
Read nbytes big-endian immediately after the tag byte at pos, advancing past the tag and the argument...
Layer 6 (Presentation) - zero-heap CBOR (RFC 8949) encoder.
pc_codec_type
The next item's type, reported by pc_codec::peek without consuming it.
@ PC_CODEC_INVALID
end of buffer, a prior error, or an item this format does not carry
const uint8_t * buf
first byte, or nullptr when there is nothing to read
size_t len
readable bytes at buf (0 whenever buf is nullptr)
bool err
sticky: a read ran past len
A writable byte region: the storage, the capacity that belongs to it, and what has been produced into...
size_t cap
bytes writable at buf (0 whenever buf is nullptr)