wiithon.binary.reader module

A reader that can read from a stream or bytes.

Used by everything in wiithon since it checks the validity of readed bytes

class wiithon.binary.reader.BinaryReader(stream, encoding='utf-8')[source]

Bases: object

A reader that can read from a stream or bytes.

Bytes are converted to a stream

Parameters:
stream

Stream to read for operations

encoding

Encoding used by default for reading string

classmethod from_bytes(data)[source]

Converted bytes to stream so BinaryReader can read it.

Parameters:

data (bytes) – Bytes to convert.

Return type:

BinaryReader

Returns:

A BinaryReader with a stream to read of

seek(offset)[source]

Change the stream’s cursor position to a given offset

Parameters:

offset (int) – The new stream’s cursor

Return type:

None

tell()[source]

Get the current stream cursor position

Return type:

int

Returns:

The current stream cursor position within the stream

skip(count)[source]

Skip a given number of bytes. It reads this number and returns nothing

Parameters:

count (int) – The number of bytes to skip

Return type:

None

u8()[source]

Read a single unsigned byte from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

The byte read from the stream

u16()[source]

Read two unsigned bytes from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

Integer reads from the stream

u32()[source]

Read four unsigned bytes from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

Integer reads from the stream

u64()[source]

Read eight unsigned bytes from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

The from the stream

s8()[source]

Read a single signed byte from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

The number read from the stream

s16()[source]

Read two signed bytes from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

Integer reads from the stream

s32()[source]

Read four signed bytes from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

Integer reads from the stream

s64()[source]

Read eight signed bytes from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

int

Returns:

Integer reads from the stream

float()[source]

Read a float number from the stream in big-endian at the current stream position. The cursor is placed after the read byte

Return type:

float

Returns:

Float reads from the stream

u32_shifted()[source]

Reads an unsigned 32 bits number then left shifted 2 times.

Some file formats use this.

Return type:

int

Returns:

The number left shifted 2 times from the stream

u32_le()[source]
Return type:

int

raw(size=-1)[source]

Read size raw bytes from the stream.

Parameters:

size (int, default: -1) – The number of bytes to read

Return type:

bytes

Returns:

Number of byte read from the stream

list_u32(size)[source]

Read a list of unsigned 32 bits numbers one after the other

Parameters:

size (int) – How many numbers to read

Return type:

list[int]

Returns:

A list of unsigned 32 bits numbers

string(size, encoding=None)[source]

Read a string from the stream with a certain size and a certain encoding

Parameters:
  • size (int) – The number of bytes to read

  • encoding (str | None, default: None) – The encoding to use

Return type:

str

Returns:

The string decoded

string_until_null(encoding=None)[source]

Read a string from the stream until a null byte (\) is found

Parameters:

encoding (str | None, default: None) – The encoding to use

Return type:

str

Returns:

The string decoded