bytes.h
The bytes API is used to handle bytes data. and buffers.
bytes_t
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
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
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
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
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
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
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
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 bytevalue
: the value to write
uint64_to_le
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 bytevalue
: the value to write
uint32_to_le
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 bytevalue
: the value to write
buffer_append
appends the given bytes to the buffer.
uint32_t buffer_append(buffer_t* buffer, bytes_t data);
Parameters
buffer
: the buffer to append todata
: the bytes to append
Returns
the new length of the buffer
buffer_splice
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 intooffset
: the offset to insert atlen
: the length of the bytes to replacedata
: the bytes to insert
buffer_add_chars_escaped
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 todata
: the data to append
buffer_add_chars
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 todata
: the data to append
buffer_add_be
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 tovalue
: the value to appendlen
: the length of the value to append
buffer_add_le
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 tovalue
: the value to appendlen
: the length of the value to append
buffer_add_hex_chars
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 todata
: the data to appendprefix
: the prefix to add or NULLsuffix
: the suffix to add or NULL
buffer_free
frees a buffer.
void buffer_free(buffer_t* buffer);
Parameters
buffer
: the buffer to free
buffer_grow
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 growmin_len
: the minimum length of the buffer
safe_malloc
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
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 allocatesize
: the size of the memory to allocate
safe_realloc
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 reallocatenew_size
: the new size of the memory
safe_free
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
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 tofmt
: the format string
Returns
the pointer to the start of the buffer as char*
bytes_all_equal
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 checkvalue
: the value to check against
Returns
true if all bytes are equal to the value, false otherwise
bytes_eq
checks if two bytes_t are equal.
bool bytes_eq(bytes_t a, bytes_t b);
Parameters
a
: the first bytes_t to checkb
: the second bytes_t to check
Returns
true if the bytes_t are equal, false otherwise
bytes_dup
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
writes a bytes_t to a file.
void bytes_write(bytes_t data, FILE* f, bool close);
Parameters
data
: the bytes_t to writef
: the file to write toclose
: true if the file should be closed after writing, false otherwise
bytes_read
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
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 convertlen
: the length of the hex stringbuffer
: the buffer to store the result
Returns
the length of the bytes written into the buffer
bytes_remove_leading_zeros
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
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 tolen
: the length of the bytes to add...
: the bytes to add
Last updated