Bitstream

In order to implement the functionality that 010 editor has of treating the entire stream as a bitstream, a stream-wrapping class (pfp.bitwrap.BitwrappedStream) was made to allow a normal stream to tread like a limited bit stream.

This may be useful in other applications outside of pfp.

BitwrappedStream Reference Documentation

class pfp.bitwrap.BitwrappedStream(stream)[source]

A stream that wraps other streams to provide bit-level access

close()[source]

Close the stream

flush()[source]

Flush the stream

is_eof()[source]

Return if the stream has reached EOF or not without discarding any unflushed bits

Returns:True/False
isatty()[source]

Return if the stream is a tty

read(num)[source]

Read num number of bytes from the stream. Note that this will automatically resets/ends the current bit-reading if it does not end on an even byte AND self.padded is True. If self.padded is True, then the entire stream is treated as a bitstream.

Num:number of bytes to read
Returns:the read bytes, or empty string if EOF has been reached
read_bits(num)[source]

Read num number of bits from the stream

Num:number of bits to read
Returns:a list of num bits, or an empty list if EOF has been reached
seek(pos, seek_type=0)[source]

Seek to the specified position in the stream with seek_type. Unflushed bits will be discarded in the case of a seek.

The stream will also keep track of which bytes have and have not been consumed so that the dom will capture all of the bytes in the stream.

Pos:offset
Seek_type:direction
Returns:TODO
size()[source]

Return the size of the stream, or -1 if it cannot be determined.

tell()[source]

Return the current position in the stream (ignoring bit position)

Returns:int for the position in the stream
unconsumed_ranges()[source]

Return an IntervalTree of unconsumed ranges, of the format (start, end] with the end value not being included

write(data)[source]

Write data to the stream

Data:the data to write to the stream
Returns:None
write_bits(bits)[source]

Write the bits to the stream.

Add the bits to the existing unflushed bits and write complete bytes to the stream.

pfp.bitwrap.bits_to_bytes(bits)[source]

Convert the bit list into bytes. (Assumes bits is a list whose length is a multiple of 8)

pfp.bitwrap.byte_to_bits(b)[source]

Convert a byte into bits

pfp.bitwrap.bytes_to_bits(bytes_)[source]

Convert bytes to a list of bits