wiithon.binary.writer module

Write numbers, strings and raw bytes into a binary stream

This module exposes BinaryWriter, the counterpart of BinaryReader Everything is written in big-endian since it is what the Wii uses

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

Bases: object

A writer that appends structured values to a stream

The writer never owns the stream, it only writes in it, so closing it is up to the caller Contrary to the reader there is no from_bytes helper, wrap a io.BytesIO yourself and read it back with getvalue()

Example

>>> buf = BytesIO()
>>> writer = BinaryWriter(buf)
>>> writer.u32(0x5D1C9EA3)
>>> writer.string("GAME", 0x10)
>>> data = buf.getvalue()
Parameters:
stream

Stream to write for operations

encoding

Encoding used by default for writing

seek(offset)[source]

Change the stream cursor position to a given offset

Parameters:

offset (int) – The new stream cursor

Return type:

None

Note

Seeking past the end of the stream does not grow it, the gap is filled only when you write there

tell()[source]

Get the current stream cursor position

Return type:

int

Returns:

The current stream cursor position within the stream

pad(count, byte=b'\\x00')[source]

Write a filler of count bytes to the stream of the same byte, used to align structure on a boundary

Parameters:
  • count (int) – How many times byte is written

  • byte (bytes, default: b'\\x00') – The filler byte. Default is null byte

Return type:

None

Note

count is the number of repetitions, not a number of bytes.

size()[source]

Get the total size of the stream

The cursor is moved to the end to measure the stream then put back where it was.

Return type:

int

Returns:

The size of the whole stream in bytes, not the number of bytes written

u8(data)[source]

Write a single unsigned byte to the stream in big-endian at the current stream position The cursor is placed after the written byte

Parameters:

data (int) – The number to write, from 0 to 255

Return type:

None

u16(data)[source]

Write two unsigned bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (int) – The number to write, from 0 to 65535

Return type:

None

u32(data)[source]

Write four unsigned bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (int) – The number to write, from 0 to 4294967295

Return type:

None

u64(data)[source]

Write eight unsigned bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (int) – The number to write, from 0 to 18446744073709551615

Return type:

None

s8(data)[source]

Write a single signed byte to the stream in big-endian at the current stream position The cursor is placed after the written byte

Parameters:

data (int) – The number to write, from -128 to 127

Return type:

None

s16(data)[source]

Write two signed bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (int) – The number to write, from -32768 to 32767

Return type:

None

s32(data)[source]

Write four signed bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (int) – The number to write, from -2147483648 to 2147483647

Return type:

None

s64(data)[source]

Write eight signed bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (int) – The number to write, from -9223372036854775808 to 9223372036854775807

Return type:

None

float(data)[source]

Write a 32 bits float to the stream in big-endian at the current stream position The cursor is placed after the written bytes

Parameters:

data (float) – The number to write

Return type:

None

list_u32(numbers)[source]

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

Parameters:

numbers (list[int]) – The numbers to write, in the order they are stored

Return type:

None

Note

No count is written before the list. If the format needs one, write it yourself

u32_shifted(data)[source]

Right shift a number 2 times then write it as an unsigned 32 bits number

This is the inverse of u32_shifted(), for the formats that store offsets divided by 4

Parameters:

data (int) – The real value, before the shift

Return type:

None

Warning

The two low bits are lost.

u32_le(data)[source]

Write four unsigned bytes to the stream in little-endian at the current stream position

Parameters:

data (int) – The number to write

Return type:

None

Note

Almost everything on a Wii disc is big-endian. This is only for the few structures that are not

raw(data)[source]

Write bytes to the stream as they are, without any conversion

Parameters:

data (bytes) – The bytes to write

Return type:

None

string(value, size=None, padding=b'\\x00', encoding=None, *, add_null_byte=False)[source]

Write a string in a field of a fixed size, padded if it is too short

Parameters:
  • value (str) – The string to write

  • size (int | None, default: None) – The size of the field in bytes. Defaults to the encoded length of value, which writes it without any padding

  • padding (bytes, default: b'\\x00') – The byte used to fill the end of the field

  • encoding (str | None, default: None) – The encoding to use. Defaults to the encoding of the writer

  • add_null_byte (bool, default: False) – Write an extra null byte after the field, for the formats that store strings one after the other in a string table

Raises:

BinaryError – If the encoded string is longer than size

Return type:

None

Note

size counts encoded bytes, not characters. A japanese string in shift_jis takes more room than its length suggests

Warning

With add_null_byte the field takes size + 1 bytes, the null byte is not counted inside