binobj.varints module

Serializers and deserializers for variable-length integers.

class EncodingInfo(encode: Callable[[int], bytes], decode: Callable[[BinaryIO], int], endian: typing_extensions.Literal[little, big], signed: bool)

Bases: object

Information describing a variable-length integer encoding.

decode: Callable[[BinaryIO], int]

A function used to deserialize a variable-length integer from a stream.

encode: Callable[[int], bytes]

A function used to serialize an integer into bytes.

endian: typing_extensions.Literal[little, big]

The endianness the string is stored in.

signed: bool

Indicates if this encoding accepts signed integers.

INTEGER_ENCODING_MAP = {<VarIntEncoding.COMPACT_INDICES: 'compact'>: EncodingInfo(encode=<function encode_integer_compact>, decode=<function decode_integer_compact>, endian='big', signed=True), <VarIntEncoding.LEB128: 'leb128'>: EncodingInfo(encode=<function encode_integer_leb128>, decode=<function decode_integer_leb128>, endian='little', signed=True), <VarIntEncoding.ULEB128: 'uleb128'>: EncodingInfo(encode=<function encode_integer_uleb128>, decode=<function decode_integer_uleb128>, endian='little', signed=False), <VarIntEncoding.VLQ: 'vlq'>: EncodingInfo(encode=<function encode_integer_vlq>, decode=<function decode_integer_vlq>, endian='big', signed=False)}

A mapping of encoding enums to encode/decode functions.

class VarIntEncoding(value)

Bases: Enum

All available encoding schemes for variable-length integers.

COMPACT_INDICES = 'compact'

Signed big-endian integer in modified VLQ format.

LEB128 = 'leb128'

Signed little-endian integer in LEB128 format.

ULEB128 = 'uleb128'

Unsigned little-endian integer in LEB128 format.

VLQ = 'vlq'

Unsigned big-endian integer in VLQ format.

decode_integer_compact(stream: BinaryIO) int

Decode an integer with signed VLQ encoding.

Parameters:

stream (BinaryIO) – The stream to read from.

Returns:

The decoded integer.

Return type:

int

decode_integer_leb128(stream: BinaryIO) int

Decode a signed LEB128-encoded integer from the given stream.

Parameters:

stream (BinaryIO) – The stream to read from.

Returns:

The decoded integer.

Return type:

int

decode_integer_uleb128(stream: BinaryIO) int

Decode an unsigned LEB128-encoded integer from the given stream.

Parameters:

stream (BinaryIO) – The stream to read from.

Returns:

The decoded integer.

Return type:

int

decode_integer_vlq(stream: BinaryIO) int

Decode an unsigned VLQ-encoded integer from the given stream.

Parameters:

stream (BinaryIO) – The stream to read from.

Returns:

The decoded integer.

Return type:

int

encode_integer_compact(value: int) bytes

Encode an integer with signed VLQ encoding.

Parameters:

value (int) – The value to encode.

Returns:

The encoded integer.

Return type:

bytes

encode_integer_leb128(value: int) bytes

Encode an integer with signed LEB128 encoding.

Parameters:

value (int) – The value to encode.

Returns:

value encoded as a variable-length integer in LEB128 format.

Return type:

bytes

encode_integer_uleb128(value: int) bytes

Encode an integer with unsigned LEB128 encoding.

Parameters:

value (int) – The value to encode.

Returns:

value encoded as a variable-length integer in ULEB128 format.

Return type:

bytes

encode_integer_vlq(value: int) bytes

Encode an integer with the unsigned VLQ encoding.

Parameters:

value (int) – The value to encode. Must be a non-negative integer.

Returns:

value encoded as a variable-length integer in VLQ format.

Return type:

bytes