bitstring.c

Include dependency graph for bitstring.c:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "14" [label="atom.h" tooltip="atom.h"] "1" [label="/__w/AtomVM/AtomVM/src/libAtomVM/bitstring.c" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/bitstring.c" fillcolor="#BFBFBF"] "2" [label="bitstring.h" tooltip="bitstring.h"] "16" [label="erl_nif.h" tooltip="erl_nif.h"] "3" [label="intn.h" tooltip="intn.h"] "21" [label="list.h" tooltip="list.h"] "23" [label="mailbox.h" tooltip="mailbox.h"] "15" [label="memory.h" tooltip="memory.h"] "20" [label="refc_binary.h" tooltip="refc_binary.h"] "22" [label="resources.h" tooltip="resources.h"] "25" [label="smp.h" tooltip="smp.h"] "24" [label="synclist.h" tooltip="synclist.h"] "11" [label="term.h" tooltip="term.h"] "17" [label="term_typedef.h" tooltip="term_typedef.h"] "26" [label="unicode.h" tooltip="unicode.h"] "6" [label="utils.h" tooltip="utils.h"] "18" [label="assert.h" tooltip="assert.h"] "7" [label="inttypes.h" tooltip="inttypes.h"] "19" [label="limits.h" tooltip="limits.h"] "27" [label="math.h" tooltip="math.h"] "4" [label="stdbool.h" tooltip="stdbool.h"] "8" [label="stddef.h" tooltip="stddef.h"] "13" [label="stdint.h" tooltip="stdint.h"] "9" [label="stdio.h" tooltip="stdio.h"] "10" [label="stdlib.h" tooltip="stdlib.h"] "5" [label="string.h" tooltip="string.h"] "12" [label="sys/types.h" tooltip="sys/types.h"] "14" -> "13" [dir=forward tooltip="include"] "14" -> "10" [dir=forward tooltip="include"] "1" -> "2" [dir=forward tooltip="include"] "1" -> "18" [dir=forward tooltip="include"] "1" -> "27" [dir=forward tooltip="include"] "2" -> "3" [dir=forward tooltip="include"] "2" -> "11" [dir=forward tooltip="include"] "2" -> "26" [dir=forward tooltip="include"] "2" -> "4" [dir=forward tooltip="include"] "2" -> "13" [dir=forward tooltip="include"] "16" -> "17" [dir=forward tooltip="include"] "3" -> "4" [dir=forward tooltip="include"] "3" -> "5" [dir=forward tooltip="include"] "3" -> "6" [dir=forward tooltip="include"] "21" -> "4" [dir=forward tooltip="include"] "23" -> "4" [dir=forward tooltip="include"] "23" -> "21" [dir=forward tooltip="include"] "23" -> "17" [dir=forward tooltip="include"] "23" -> "6" [dir=forward tooltip="include"] "15" -> "13" [dir=forward tooltip="include"] "15" -> "10" [dir=forward tooltip="include"] "15" -> "16" [dir=forward tooltip="include"] "15" -> "17" [dir=forward tooltip="include"] "15" -> "6" [dir=forward tooltip="include"] "20" -> "4" [dir=forward tooltip="include"] "20" -> "10" [dir=forward tooltip="include"] "20" -> "21" [dir=forward tooltip="include"] "20" -> "22" [dir=forward tooltip="include"] "22" -> "10" [dir=forward tooltip="include"] "22" -> "16" [dir=forward tooltip="include"] "22" -> "21" [dir=forward tooltip="include"] "22" -> "23" [dir=forward tooltip="include"] "22" -> "15" [dir=forward tooltip="include"] "22" -> "24" [dir=forward tooltip="include"] "25" -> "4" [dir=forward tooltip="include"] "24" -> "9" [dir=forward tooltip="include"] "24" -> "21" [dir=forward tooltip="include"] "24" -> "25" [dir=forward tooltip="include"] "11" -> "12" [dir=forward tooltip="include"] "11" -> "4" [dir=forward tooltip="include"] "11" -> "13" [dir=forward tooltip="include"] "11" -> "9" [dir=forward tooltip="include"] "11" -> "10" [dir=forward tooltip="include"] "11" -> "5" [dir=forward tooltip="include"] "11" -> "14" [dir=forward tooltip="include"] "11" -> "3" [dir=forward tooltip="include"] "11" -> "15" [dir=forward tooltip="include"] "11" -> "20" [dir=forward tooltip="include"] "11" -> "22" [dir=forward tooltip="include"] "11" -> "6" [dir=forward tooltip="include"] "11" -> "17" [dir=forward tooltip="include"] "17" -> "18" [dir=forward tooltip="include"] "17" -> "19" [dir=forward tooltip="include"] "17" -> "7" [dir=forward tooltip="include"] "17" -> "13" [dir=forward tooltip="include"] "26" -> "4" [dir=forward tooltip="include"] "26" -> "8" [dir=forward tooltip="include"] "26" -> "13" [dir=forward tooltip="include"] "6" -> "7" [dir=forward tooltip="include"] "6" -> "4" [dir=forward tooltip="include"] "6" -> "8" [dir=forward tooltip="include"] "6" -> "9" [dir=forward tooltip="include"] "6" -> "10" [dir=forward tooltip="include"] }

Functions

bool bitstring_extract_any_integer(const uint8_t *src, size_t offset, avm_int_t n, enum BitstringFlags bs_flags, union maybe_unsigned_int64 *dst)
static void insert_bits_msb_first(uint8_t *dst, size_t offset, uint64_t value, size_t n)
static void insert_sign_bits(uint8_t *dst, size_t offset, size_t count, bool negative)
bool bitstring_insert_any_integer(uint8_t *dst, avm_int_t offset, avm_int64_t value, size_t n, enum BitstringFlags bs_flags)
bool bitstring_insert_bigint(term dst_bin, size_t offset, const intn_digit_t big_value[], size_t big_len, intn_integer_sign_t big_sign, size_t n, enum BitstringFlags bs_flags)

Insert a big integer, truncated or sign-extended to n bits.

Parameters:
  • dst_bin – binary to insert to

  • offset – offset, in bits, to where to insert the integer

  • big_value – digits of the big integer

  • big_len – number of digits

  • big_sign – sign of the big integer

  • n – size of the field, in bits

  • bs_flags – flags to encode the integer (little/big/native)

Returns:

true if the field was written, false if it does not fit in dst_bin

bool bitstring_extract_bigint_bytes(term src_bin, size_t offset, size_t n, enum BitstringFlags bs_flags, uint8_t out[256 / 8], size_t *out_size)

Extract an integer field of up to 256 bits as whole bytes for intn_from_integer_bytes, sign-extended if signed.

Parameters:
  • src_bin – binary to extract from

  • offset – offset, in bits, to where the field starts

  • n – size of the field, in bits

  • bs_flags – flags to decode the integer (little/big/native, signed)

  • out – the bytes of the integer, on output

  • out_size – the number of bytes written to out, on output

Returns:

true if the field was extracted, false if it is too large or out of bounds

static bool is_invalid_codepoint(int32_t v)
bool bitstring_utf8_encode(uint32_t c, uint8_t *buf, size_t *out_size)

Encode a character to UTF-8.

Parameters:
  • c – character to encode

  • buf – the buffer to encode the sring to or NULL to only compute the size.

  • out_size – the size in bytes, on output (if not NULL)

Returns:

true if encoding was successful, false if c is not a valid unicode character

bool bitstring_utf16_encode(uint32_t c, uint8_t *buf, enum BitstringFlags bs_flags, size_t *out_size)

Encode a character to UTF-16.

Parameters:
  • c – character to encode

  • buf – the buffer to encode the character to or NULL to only compute the size.

  • bs_flags – flags to encode the character (undefined/little/big/native)

  • out_size – the size in bytes, on output (if not NULL)

Returns:

true if encoding was successful, false if c is not a valid unicode character

bool bitstring_utf16_decode(const uint8_t *buf, size_t len, int32_t *c, size_t *out_size, enum BitstringFlags bs_flags)

Decode a character from UTF-16.

Parameters:
  • c – int value to decode to

  • buf – the buffer froom which to decode the sring to or NULL to only compute the size.

  • len – the length (in bytes) of the bytes in buf

  • bs_flags – flags to decode the character (undefined/little/big/native)

  • out_size – the size in bytes, on output (if not NULL)

Returns:

true if decoding was successful, false if character starting at buf is not a valid unicode character

bool bitstring_utf32_encode(uint32_t c, uint8_t *buf, enum BitstringFlags bs_flags)

Encode a character to UTF-32.

Parameters:
  • c – character to encode

  • buf – the buffer to encode the character

  • bs_flags – flags to encode the character (undefined/little/big/native)

Returns:

true if encoding was successful, false if c is not a valid unicode character

bool bitstring_utf32_decode(const uint8_t *buf, size_t len, int32_t *c, enum BitstringFlags bs_flags)

Decode a character from UTF-32.

Parameters:
  • c – int value to decode to

  • buf – the buffer froom which to decode the sring to or NULL to only compute the size.

  • len – the length (in bytes) of the bytes in buf

  • bs_flags – flags to decode the character (undefined/little/big/native)

Returns:

true if decoding was successful, false if character starting at buf is not a valid unicode character

static const uint8_t *bitstring_unit_source(term src_bin, size_t offset, uint8_t buf[4], size_t *len)
bool bitstring_insert_utf8(term dst_bin, size_t offset, uint32_t c, size_t *out_size)

Insert a character in UTF-8 format.

Parameters:
  • dst_bin – binary to insert to

  • offset – offset, in bits, to where to insert the character

  • c – character to encode

  • out_size – the size in bytes, on output

Returns:

true if encoding was successful, false if c is not a valid unicode character

bool bitstring_match_utf8(term src_bin, size_t offset, uint32_t *c, size_t *out_size)

Match a character in UTF-8 format.

Parameters:
  • src_bin – binary to match against

  • offset – offset, in bits, to where to start to match the character

  • c – int to decode to

  • out_size – the size in bytes, on output

Returns:

true if encoding was successful, false if src_bin at offset is not a valid unicode character

bool bitstring_insert_utf16(term dst_bin, size_t offset, uint32_t c, enum BitstringFlags bs_flags, size_t *out_size)

Insert a character in UTF-&§ format.

Parameters:
  • dst_bin – binary to insert to

  • offset – offset, in bits, to where to insert the character

  • c – character to encode

  • bs_flags – flags to encode the character (undefined/little/big/native)

  • out_size – the size in bytes, on output

Returns:

true if encoding was successful, false if c is not a valid unicode character

bool bitstring_match_utf16(term src_bin, size_t offset, int32_t *c, size_t *out_size, enum BitstringFlags bs_flags)

Match a character in UTF-16 format.

Parameters:
  • src_bin – binary to match against

  • offset – offset, in bits, to where to start to match the character

  • c – int to decode to

  • bs_flags – flags to decode the character (undefined/little/big/native)

  • out_size – the size in bytes, on output

Returns:

true if encoding was successful, false if src_bin at offset is not a valid unicode character

bool bitstring_insert_utf32(term dst_bin, size_t offset, uint32_t c, enum BitstringFlags bs_flags)

Insert a character in UTF-32 format.

Parameters:
  • dst_bin – binary to insert to

  • offset – offset, in bits, to where to insert the character

  • c – character to encode

  • bs_flags – flags to encode the character (undefined/little/big/native)

Returns:

true if encoding was successful, false if c is not a valid unicode character

bool bitstring_match_utf32(term src_bin, size_t offset, int32_t *c, enum BitstringFlags bs_flags)

Match a character in UTF-32 format.

Parameters:
  • src_bin – binary to match against

  • offset – offset, in bits, to where to start to match the character

  • c – int to decode to

  • bs_flags – flags to decode the character (undefined/little/big/native)

Returns:

true if encoding was successful, false if src_bin at offset is not a valid unicode character

void bitstring_copy_bits_incomplete_bytes(uint8_t *dst, size_t bits_offset, const uint8_t *src, size_t bits_count)

Copy bits_count bits from src to dst[bits_offset..].

Called by bitstring_copy_bits when bytes are not complete.

Parameters:
  • dst – destination buffer

  • bits_offset – offset in bits in destination buffer

  • src – source buffer

  • bits_count – number of bits

void bitstring_copy_bits_from(uint8_t *dst, const uint8_t *src, size_t src_offset, size_t bits_count)

Copy bits_count bits starting at bit src_offset of src into dst[0..].

Used to materialize a non-byte-aligned bitstring slice (e.g. a matched tail whose start is not a byte boundary) into a fresh byte-aligned buffer. The destination must be pre-zeroed; only set bits are written.

Parameters:
  • dst – destination buffer (pre-zeroed), receiving bits at offset 0

  • src – source buffer

  • src_offset – offset in bits in the source buffer

  • bits_count – number of bits to copy

static bool bitstring_slice_can_share(term bs_bin, size_t offset, size_t len_bits)
size_t bitstring_slice_heap_size(term bs_bin, size_t offset, size_t len_bits)

Heap words needed to extract a len_bits slice at bit offset offset.

Worst case: a byte-aligned offset and length shares storage via a sub-binary; otherwise the bits are copied into a fresh (possibly bitstring) binary. Pair with bitstring_slice.

term bitstring_slice(term bs_bin, size_t offset, size_t len_bits, Heap *heap, GlobalContext *glb)

Extract a len_bits slice of bs_bin starting at bit offset offset.

Byte-aligned offset+length shares storage via a sub-binary; a non-byte-aligned offset or length copies the bits into a fresh bitstring. Heap must already be reserved (see bitstring_slice_heap_size).

Returns:

the extracted slice, or term_invalid_term() if a refc binary buffer could not be allocated for a copied slice.

static bool bitstring_read_float_bits(term src_bin, size_t offset, size_t n, enum BitstringFlags bs_flags, uint64_t *bits)
static bool bitstring_write_float_bits(term dst_bin, size_t offset, uint64_t bits, size_t n, enum BitstringFlags bs_flags)
bool bitstring_extract_f16(term src_bin, size_t offset, avm_int_t n, enum BitstringFlags bs_flags, avm_float_t *dst)
bool bitstring_extract_f32(term src_bin, size_t offset, avm_int_t n, enum BitstringFlags bs_flags, avm_float_t *dst)
bool bitstring_extract_f64(term src_bin, size_t offset, avm_int_t n, enum BitstringFlags bs_flags, avm_float_t *dst)
intn_from_integer_options_t bitstring_flags_to_intn_opts(enum BitstringFlags bf)
bool bitstring_insert_f16(term dst_bin, size_t offset, avm_float_t value, enum BitstringFlags bs_flags)
bool bitstring_insert_f32(term dst_bin, size_t offset, avm_float_t value, enum BitstringFlags bs_flags)
bool bitstring_insert_f64(term dst_bin, size_t offset, avm_float_t value, enum BitstringFlags bs_flags)