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.
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.
Parameters
data: the pointer to the first byte (must not be NULL)
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.
Parameters
data: the pointer to the first byte (must not be NULL)
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.
Parameters
data: the pointer to the first byte (must not be NULL)
Returns
the value in little endian format
uint64_from_be
converts a uint64_t to a bytes_t in big endian format.
Parameters
data: the pointer to the first byte (must not be NULL)
Returns
the value in big endian format
uint64_to_be
writes 8 bytes as big endian from the given value.
Parameters
data: the pointer to the first byte (must not be NULL)value: the value to write
uint64_to_le
writes 8 bytes as little endian from the given value.
Parameters
data: the pointer to the first byte (must not be NULL)value: the value to write
uint32_to_le
writes 4 bytes as little endian from the given value.
Parameters
data: the pointer to the first byte (must not be NULL)value: the value to write
buffer_append
appends the given bytes to the buffer.
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.
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
append chars to a buffer. An additional NULL-Terminator will be added to the end of the buffer.
Parameters
buffer: the buffer to append todata: the data to append
buffer_add_be
append a value as big endian to a buffer.
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.
Parameters
buffer: the buffer to append tovalue: the value to appendlen: the length of the value to append
buffer_free
frees a 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.
Parameters
buffer: the buffer to growmin_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
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.
Parameters
size: the size of the memory to allocate
Returns
the pointer to the allocated memory (never NULL)
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.
Parameters
num: the number of elements to allocatesize: the size of the memory to allocate
Returns
the pointer to the allocated memory (never NULL for non-zero sizes)
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.
Parameters
ptr: the pointer to the memory to reallocatenew_size: the new size of the memory
Returns
the pointer to the reallocated memory (never NULL for non-zero sizes)
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 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
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.
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.
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.
Parameters
data: the bytes_t to duplicate
Returns
the duplicated bytes_t
bytes_write
writes a bytes_t to a file.
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.
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.
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.
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.
Parameters
buf: the buffer to add the bytes tolen: the length of the bytes to add...: the bytes to add
Last updated