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);

Parameters

  • data : the pointer to the first byte

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);

Parameters

  • data : the pointer to the first byte

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);

Parameters

  • data : the pointer to the first byte

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);

Parameters

  • data : the pointer to the first byte

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);

Parameters

  • data : the pointer to the first byte

  • 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);

Parameters

  • data : the pointer to the first byte

  • 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);

Parameters

  • data : the pointer to the first byte

  • 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_escaped

util/bytes.h

append chars to a buffer while escaping special characters. an additional NULL-Terminator will be added to the end of the buffer.

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

Parameters

  • buffer : the buffer to append to

  • data : the data to append

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_add_hex_chars

util/bytes.h

append bytes as hex chars to a buffer.

void buffer_add_hex_chars(buffer_t* buffer, bytes_t data, char* prefix, char* suffix);

Parameters

  • buffer : the buffer to append to

  • data : the data to append

  • prefix : the prefix to add or NULL

  • suffix : the suffix to add or NULL

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.

void buffer_grow(buffer_t* buffer, size_t min_len);

Parameters

  • buffer : the buffer to grow

  • min_len : the minimum length of 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);

Parameters

  • size : the size of the memory to allocate

Returns

the pointer to the allocated memory

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

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);

Parameters

  • ptr : the pointer to the memory to reallocate

  • new_size : the new size of the memory

safe_free

util/bytes.h

calls free and check if the pointer is not NULL.

void safe_free(void* ptr);

Parameters

  • ptr : the pointer to the memory to free

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 on 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 json using numbers for uint

  • %Z: ssz_ob_t as json using hex without leading zeros for uint

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_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

Last updated