ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
bytes.h File Reference

The byte verbs - append into a pc_span, take out of a pc_cspan. More...

#include "shared_primitives/endian.h"
#include "shared_primitives/span.h"
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Functions

void pc_bw_put (pc_span *w, uint8_t b)
 Append one byte; on overflow set the flag but keep counting pos.
 
void pc_bw_put_be (pc_span *w, uint64_t val, int32_t nbytes)
 Append the low nbytes of val, big-endian (network order).
 
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 (pos += 1 + nbytes).
 
bool pc_rd_u32 (const uint8_t *p, size_t len, size_t *off, uint32_t *out)
 Read a big-endian u32 at *off, advancing it by 4. False if it would run past len.
 
bool pc_rd_str (const uint8_t *p, size_t len, size_t *off, const uint8_t **out, uint32_t *slen)
 Read a u32-length-prefixed blob: out points into p, slen is its length.
 

Detailed Description

The byte verbs - append into a pc_span, take out of a pc_cspan.

A bounded byte region is one thing with two accessors. span.h is the region: where the storage came from, how big it is, how much has been produced, and whether anything overran. This file is what you do to it. The two halves are split that way so a region can be passed somewhere that only reads it without carrying an append API along.

The subtle invariants live here once, so a bug is fixed in one place and every codec inherits it: keep counting pos past cap on overflow so the caller can size the buffer, sticky fault flags, and network (big-endian) byte order.

These take pc_span / pc_cspan directly rather than being templated on a per-codec cursor struct. CBOR and MessagePack each declared their own writer and reader, all four field-identical to the spans, and the templates existed only to bind them by field name. One concrete pair replaces four near-duplicate structs and removes the deduction along with them.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file bytes.h.

Function Documentation

◆ pc_bw_put()

void pc_bw_put ( pc_span w,
uint8_t  b 
)
inline

Append one byte; on overflow set the flag but keep counting pos.

Definition at line 37 of file bytes.h.

References pc_span::buf, pc_span::cap, pc_span::overflow, and pc_span::pos.

Referenced by pc_bw_put_be().

◆ pc_bw_put_be()

void pc_bw_put_be ( pc_span w,
uint64_t  val,
int32_t  nbytes 
)
inline

Append the low nbytes of val, big-endian (network order).

Definition at line 51 of file bytes.h.

References pc_bw_put().

◆ pc_br_take_be()

bool pc_br_take_be ( pc_cspan r,
size_t  nbytes,
uint64_t *  out 
)
inline

Read nbytes big-endian immediately after the tag byte at pos, advancing past the tag and the argument (pos += 1 + nbytes).

Both CBOR heads and MessagePack format bytes are a 1-byte tag followed by a big-endian argument, so this consumes the tag + argument in one step. Sets the sticky err and returns false if the read would run past the buffer.

Definition at line 69 of file bytes.h.

References pc_cspan::buf, pc_cspan::err, pc_cspan::len, and pc_cspan::pos.

◆ pc_rd_u32()

bool pc_rd_u32 ( const uint8_t *  p,
size_t  len,
size_t *  off,
uint32_t *  out 
)
inline

Read a big-endian u32 at *off, advancing it by 4. False if it would run past len.

Definition at line 93 of file bytes.h.

References pc_rd32be().

Referenced by pc_rd_str().

◆ pc_rd_str()

bool pc_rd_str ( const uint8_t *  p,
size_t  len,
size_t *  off,
const uint8_t **  out,
uint32_t *  slen 
)
inline

Read a u32-length-prefixed blob: out points into p, slen is its length.

Nothing is copied, so the result must not outlive p. On a length that would run past the end, *off is left where it started so the caller can report which field failed.

Definition at line 110 of file bytes.h.

References pc_rd_u32().