bytes.h

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

bytes_t

util/bytes.harrow-up-right

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.harrow-up-right

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.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

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

util/bytes.harrow-up-right

appends the given bytes to the buffer.

Parameters

  • buffer : the buffer to append to

  • data : the bytes to append

Returns

the new length of the buffer

buffer_splice

util/bytes.harrow-up-right

inserts or deletes a segment from or into the buffer.

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.harrow-up-right

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

Parameters

  • buffer : the buffer to append to

  • data : the data to append

buffer_add_be

util/bytes.harrow-up-right

append a value as big endian to a buffer.

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.harrow-up-right

append a value as little endian to a buffer.

Parameters

  • buffer : the buffer to append to

  • value : the value to append

  • len : the length of the value to append

buffer_free

util/bytes.harrow-up-right

frees a buffer.

Parameters

  • buffer : the buffer to free

buffer_grow

util/bytes.harrow-up-right

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 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.harrow-up-right

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

util/bytes.harrow-up-right

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 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.harrow-up-right

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 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.harrow-up-right

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 to

  • fmt : the format string

Returns

the pointer to the start of the buffer as char*

bytes_all_equal

util/bytes.harrow-up-right

checks if all bytes in the bytes_t are equal to the given 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.harrow-up-right

checks if two bytes_t are equal.

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.harrow-up-right

duplicates a bytes_t.

Parameters

  • data : the bytes_t to duplicate

Returns

the duplicated bytes_t

bytes_write

util/bytes.harrow-up-right

writes a bytes_t to a file.

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.harrow-up-right

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

util/bytes.harrow-up-right

converts a hex string to a bytes_t.

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.harrow-up-right

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

util/bytes.harrow-up-right

adds a variable number of bytes to a buffer.

Parameters

  • buf : the buffer to add the bytes to

  • len : the length of the bytes to add

  • ... : the bytes to add

Last updated