mrcrowbar.bits module

class mrcrowbar.bits.BitStream(buffer: BytesReadType | None = None, start_offset: int | tuple[int, int] | None = None, bytes_reverse: bool = False, bit_endian: EndianEncoding = 'big', io_endian: EndianEncoding = 'big')[source]

Bases: object

Create a BitStream instance.

buffer
Target byte array to read/write from. Defaults to an empty array.
start_offset
Position in the target to start reading from. Can be an integer byte offset, or a tuple containing the byte and bit offsets. Defaults to the start of the stream, depending on the endianness and ordering options.
bytes_reverse
If enabled, fetch successive bytes from the source in reverse order.
bit_endian
Endianness of the backing storage; either ‘big’ or ‘little’. Defaults to big (i.e. starting from the most-significant bit (0x80) through least-significant bit (0x10)).
io_endian
Endianness of data returned from read/write; either ‘big’ or ‘little’. Defaults to big (i.e. starting from the most-significant bit (0x80) through least-significant bit (0x10)).
get_buffer() → bytes[source]

Return a byte string containing the target.

in_bounds() → bool[source]

Returns True if the current position is within the bounds of the target.

read(count: int) → int[source]

Get an integer containing the next [count] bits from the source.

seek(offset: int | tuple[int, int], origin: str = 'start') → None[source]

Seek to a location in the target.

offset
Relative offset in the target to move to. Can be an integer byte offset, or a tuple containing the byte and bit offsets.
origin
Position to measure the offset from. Can be either “start”, “current” or “end”. Defaults to “start”.
tell() → tuple[int, int][source]

Get the current byte and bit position.

write(value: int, count: int) → None[source]

Write an unsigned integer containing [count] bits to the source.

mrcrowbar.bits.mask(size)
mrcrowbar.bits.pack_bits(longbits: int) → int[source]

Crunch a 64-bit int (8 bool bytes) into a bitfield.

mrcrowbar.bits.read_bits(buffer: Union[bytes, bytearray, mmap.mmap, memoryview], byte_offset: int, bit_offset: int, size: int, bytes_reverse: bool = False, bit_endian: typing_extensions.Literal['big', 'little'][big, little] = 'big', io_endian: typing_extensions.Literal['big', 'little'][big, little] = 'big') → int[source]
mrcrowbar.bits.reverse_bits(number: int, size: int = 8) → int[source]
mrcrowbar.bits.reverse_bytes(buffer: Union[bytes, bytearray, mmap.mmap, memoryview]) → bytes[source]
mrcrowbar.bits.unpack_bits(byte: int) → int[source]

Expand a bitfield into a 64-bit int (8 bool bytes).

mrcrowbar.bits.write_bits(value: int, buffer: bytearray, byte_offset: int, bit_offset: int, size: int, bytes_reverse: bool = False, bit_endian: typing_extensions.Literal['big', 'little'][big, little] = 'big', io_endian: typing_extensions.Literal['big', 'little'][big, little] = 'big') → None[source]