mrcrowbar.bits module

class mrcrowbar.bits.BitStream(buffer=None, start_offset=None, bytes_reverse=False, bit_endian='big', io_endian='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()[source]

Return a byte string containing the target.

in_bounds()[source]

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

read(count)[source]

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

seek(offset, origin='start')[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()[source]

Get the current byte and bit position.

write(value, count)[source]

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

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

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

mrcrowbar.bits.read_bits(buffer, byte_offset, bit_offset, size, bytes_reverse=False, bit_endian='big', io_endian='big')[source]
mrcrowbar.bits.reverse_bits(number, size=8)[source]
mrcrowbar.bits.reverse_bytes(buffer)[source]
mrcrowbar.bits.unpack_bits(byte)[source]

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

mrcrowbar.bits.write_bits(value, buffer, byte_offset, bit_offset, size, bytes_reverse=False, bit_endian='big', io_endian='big')[source]