Corpus Core Docs

bytes.h

The bytes API is used to handle bytes data. and buffers.

bytes_t

util/bytes.h

a basic type struct, which holds a pointer to a bytes buffer and the length of the buffer. It should be used as a fat pointer, which is a pointer and passed by value.

typedef struct {
  uint32_t      len;             // the length of the data
  uint8_t* data COUNTED_BY(len); // the data pointer
} bytes_t;

bytes_as_le

util/bytes.h

converts a bytes_t to a uint64_t in little endian format.

uint64_t bytes_as_le(bytes_t data);

Parameters

  • data : the bytes_t to convert

Returns

the uint64_t in little endian format

bytes_as_be

util/bytes.h

converts a bytes_t to a uint64_t in big endian format.

uint64_t bytes_as_be(bytes_t data);

Parameters

  • data : the bytes_t to convert

Returns

the uint64_t in big endian format

uint16_from_le

util/bytes.h

converts a uint16_t to a bytes_t in little endian format. expecting 2 bytes.

uint16_t uint16_from_le(uint8_t* data) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)

Returns

the value in little endian format

uint32_from_le

util/bytes.h

converts a uint32_t to a bytes_t in little endian format. expecting 4 bytes.

uint32_t uint32_from_le(uint8_t* data) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)

Returns

the value in little endian format

uint64_from_le

util/bytes.h

converts a uint64_t to a bytes_t in little endian format. expecting 8 bytes.

uint64_t uint64_from_le(uint8_t* data) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)

Returns

the value in little endian format

uint64_from_be

util/bytes.h

converts a uint64_t to a bytes_t in big endian format.

uint64_t uint64_from_be(uint8_t* data) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)

Returns

the value in big endian format

uint64_to_be

util/bytes.h

writes 8 bytes as big endian from the given value.

void uint64_to_be(uint8_t* data, uint64_t value) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)
  • value : the value to write

uint64_to_le

util/bytes.h

writes 8 bytes as little endian from the given value.

void uint64_to_le(uint8_t* data, uint64_t value) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)
  • value : the value to write

uint32_to_le

util/bytes.h

writes 4 bytes as little endian from the given value.

void uint32_to_le(uint8_t* data, uint32_t value) NONNULL;

Parameters

  • data : the pointer to the first byte (must not be NULL)
  • value : the value to write

buffer_append

util/bytes.h

appends the given bytes to the buffer.

uint32_t buffer_append(buffer_t* buffer, bytes_t data);

Parameters

  • buffer : the buffer to append to
  • data : the bytes to append

Returns

the new length of the buffer

buffer_splice

util/bytes.h

inserts or deletes a segment from or into the buffer.

void buffer_splice(buffer_t* buffer, size_t offset, uint32_t len, bytes_t data);

Parameters

  • buffer : the buffer to insert into
  • offset : the offset to insert at
  • len : the length of the bytes to replace
  • data : the bytes to insert

buffer_add_chars

util/bytes.h

append chars to a buffer. An additional NULL-Terminator will be added to the end of the buffer.

void buffer_add_chars(buffer_t* buffer, const char* data);

Parameters

  • buffer : the buffer to append to
  • data : the data to append

buffer_add_be

util/bytes.h

append a value as big endian to a buffer.

void buffer_add_be(buffer_t* buffer, uint64_t value, uint32_t len);

Parameters

  • buffer : the buffer to append to
  • value : the value to append
  • len : the length of the value to append

buffer_add_le

util/bytes.h

append a value as little endian to a buffer.

void buffer_add_le(buffer_t* buffer, uint64_t value, uint32_t len);

Parameters

  • buffer : the buffer to append to
  • value : the value to append
  • len : the length of the value to append

buffer_free

util/bytes.h

frees a buffer.

void buffer_free(buffer_t* buffer);

Parameters

  • buffer : the buffer to free

buffer_grow

util/bytes.h

grows the buffer if needed, so it will be able to hold the min_len of bytes. If the allocated is <0 and this is a fixed buffer, it will do nothing.

size_t buffer_grow(buffer_t* buffer, size_t min_len);

Parameters

  • buffer : the buffer to grow
  • min_len : the minimum length of the buffer

Returns

the available length of the buffer. Never write more than this value to the buffer.

safe_malloc

util/bytes.h

calls malloc and check if the returned pointer is not NULL. if the memory could not be allocated, the program will exit with an error message.

void* safe_malloc(size_t size) RETURNS_NONNULL M_RET;

Parameters

  • size : the size of the memory to allocate

Returns

the pointer to the allocated memory (never NULL)

safe_aligned_alloc

util/bytes.h

Allocates size bytes aligned to alignment.

malloc only guarantees alignment up to max_align_t (16 bytes on most 64-bit targets). Types such as intx_uint256_t are aligned to 32 bytes, and GCC then emits aligned vector moves. A 16-byte-aligned heap object makes those moves fault.

alignment must be a power of two. Smaller or non-power-of-two values are raised to sizeof(void *). The returned address is the base of the allocation (posix_memalign, or _aligned_malloc on Windows), so a pointer kept for the process lifetime stays the block base. The result is never NULL: allocation failure aborts, same as safe_malloc. Release the pointer with safe_aligned_free.

void* safe_aligned_alloc(size_t alignment, size_t size) RETURNS_NONNULL M_RET;

Parameters

  • alignment : required alignment in bytes
  • size : number of bytes to allocate

Returns

aligned pointer

safe_aligned_free

util/bytes.h

Frees a pointer returned by safe_aligned_alloc.

free must not be used on that pointer. NULL is ignored.

void safe_aligned_free(void* ptr);

Parameters

  • ptr : aligned pointer, or NULL

safe_calloc

util/bytes.h

calls calloc and check if the returned pointer is not NULL. if the memory could not be allocated, the program will exit with an error message.

void* safe_calloc(size_t num, size_t size);

Parameters

  • num : the number of elements to allocate
  • size : the size of the memory to allocate

Returns

the pointer to the allocated memory (never NULL for non-zero sizes)

safe_realloc

util/bytes.h

calls realloc and check if the returned pointer is not NULL. if the memory could not be allocated, the program will exit with an error message.

void* safe_realloc(void* ptr, size_t new_size) M_RET;

Parameters

  • ptr : the pointer to the memory to reallocate
  • new_size : the new size of the memory

Returns

the pointer to the reallocated memory (never NULL for non-zero sizes)

bprintf

util/bytes.h

writes to the buffer. the format is similar to printf. but those are the supported formats:

  • %s: char*
  • %S: chars, but escaped
  • %x: bytes_t as hex
  • %u: bytes_t as hex without leading zeros
  • %c: char as char
  • %J: json_t adds as json string
  • %j: json_t adds as json string, but in case of a string, the quotes are removed
  • %l: uint64_t as number
  • %lx: uint64_t as hex
  • %d: uint32_t as number
  • %dx: uint32_t as hex
  • %z: ssz_ob_t as value using numbers for uint (quotes are removed)
  • %Z: ssz_ob_t as value using hex without leading zeros for uint (quotes are removed)
  • %r: raw bytes as string
char* bprintf(buffer_t* buf, const char* fmt, ...);

Parameters

  • buf : the buffer to write to
  • fmt : the format string

Returns

the pointer to the start of the buffer as char*

bytes_all_equal

util/bytes.h

checks if all bytes in the bytes_t are equal to the given value.

bool bytes_all_equal(bytes_t a, uint8_t value);

Parameters

  • a : the bytes_t to check
  • value : the value to check against

Returns

true if all bytes are equal to the value, false otherwise

bytes_eq

util/bytes.h

checks if two bytes_t are equal.

bool bytes_eq(bytes_t a, bytes_t b);

Parameters

  • a : the first bytes_t to check
  • b : the second bytes_t to check

Returns

true if the bytes_t are equal, false otherwise

bytes_contains

util/bytes.h

Checks whether needle occurs in data.

The search uses the raw bytes of data and does not require a trailing NUL. NULL data, a NULL needle, an empty needle and a needle longer than data all return false.

bool bytes_contains(bytes_t data, const char* needle);

Parameters

  • data : bytes to search
  • needle : NUL-terminated string to find

Returns

true if needle occurs in data

bytes_dup

util/bytes.h

duplicates a bytes_t.

bytes_t bytes_dup(bytes_t data);

Parameters

  • data : the bytes_t to duplicate

Returns

the duplicated bytes_t

bytes_write

util/bytes.h

writes a bytes_t to a file.

void bytes_write(bytes_t data, FILE* f, bool close);

Parameters

  • data : the bytes_t to write
  • f : the file to write to
  • close : true if the file should be closed after writing, false otherwise

bytes_read

util/bytes.h

reads a bytes_t from a file.

bytes_t bytes_read(char* filename);

Parameters

  • filename : the name of the file to read

Returns

the bytes_t read from the file

hex_to_bytes

util/bytes.h

converts a hex string to a bytes_t.

int hex_to_bytes(const char* hexstring, int len, bytes_t buffer);

Parameters

  • hexstring : the hex string to convert
  • len : the length of the hex string
  • buffer : the buffer to store the result

Returns

the length of the bytes written into the buffer

bytes_remove_leading_zeros

util/bytes.h

removes leading zeros from a bytes_t.

bytes_t bytes_remove_leading_zeros(bytes_t data);

Parameters

  • data : the bytes_t to remove leading zeros from

Returns

the bytes_t with leading zeros removed

buffer_add_bytes

util/bytes.h

adds a variable number of bytes to a buffer.

void buffer_add_bytes(buffer_t* buf, uint32_t len, ...);

Parameters

  • buf : the buffer to add the bytes to
  • len : the length of the bytes to add
  • ... : the bytes to add

=

util/bytes.h

File-based bprintf - formats and writes output directly to a FILE stream. This macro creates a temporary buffer, formats the data using bprintf, writes it to the specified file using fwrite, and cleans up automatically. Useful for replacing fprintf without linking printf family functions.

Example usage:

fbprintf(stderr, "Error code: %d\n", error_code);
fbprintf(log_file, "Processing item %l of %l\n", current, total);
#define fbprintf(file, format, ...)                          \
  do {                                                       \
    buffer_t __buf = {0};                                    \
    char*    __str = bprintf(&__buf, format, ##__VA_ARGS__); \
    fwrite(__str, 1, __buf.data.len, file);                  \
    buffer_free(&__buf);                                     \
  } while (0)

static inline bytes_t bytes_cpy(void* dst, size_t offset, bytes_t src) {
  uint8_t* dest = ((uint8_t*) dst) + offset;

Parameters

  • file : FILE* pointer to write to (e.g., stdout, stderr, or fopen result)
  • format : Format string (bprintf syntax: %s, %d, %l, %lx, %x, %S, etc.)
  • ... : Format arguments