Skip to content

Utilities

All of these are also exported by from hwntools import *. Packing helpers use context.endian unless you pass endian=.

packing

Integer packing helpers (p32/u32 and friends) and flat.

pack

pack(value: int, bits: int | None = None, endian: str | None = None, signed: bool = False) -> bytes

Pack value into bits bits (default context.bits).

Negative values are accepted and packed as two's complement even when signed is false, so p32(-1) == b"\xff\xff\xff\xff".

unpack

unpack(data: bytes, bits: int | None = None, endian: str | None = None, signed: bool = False) -> int

Unpack exactly bits // 8 bytes into an int.

p8

p8(value: int, endian: str | None = None, signed: bool = False) -> bytes

Pack value into 1 byte (see pack).

p16

p16(value: int, endian: str | None = None, signed: bool = False) -> bytes

Pack value into 2 bytes (see pack).

p32

p32(value: int, endian: str | None = None, signed: bool = False) -> bytes

Pack value into 4 bytes (see pack).

p64

p64(value: int, endian: str | None = None, signed: bool = False) -> bytes

Pack value into 8 bytes (see pack).

u8

u8(data: bytes, endian: str | None = None, signed: bool = False) -> int

Unpack exactly 1 byte into an int (see unpack).

u16

u16(data: bytes, endian: str | None = None, signed: bool = False) -> int

Unpack exactly 2 bytes into an int (see unpack).

u32

u32(data: bytes, endian: str | None = None, signed: bool = False) -> int

Unpack exactly 4 bytes into an int (see unpack).

u64

u64(data: bytes, endian: str | None = None, signed: bool = False) -> int

Unpack exactly 8 bytes into an int (see unpack).

flat

flat(*items: object, word_size: int | None = None, endian: str | None = None, filler: bytes = b'\x00', length: int | None = None) -> bytes

Flatten ints, bytes, strings, lists and {offset: value} dicts into bytes.

Ints are packed with word_size bits (default context.bits). Dict keys are absolute offsets in the output; gaps and the optional final length padding use filler.

flat(0x41424344, b"!", {8: b"X"}, word_size=32, endian="big") b'ABCD!\x00\x00\x00X'

misc

Byte helpers: hexdump, xor, hex/base64 conversion, chunking.

hexdump

hexdump(data: bytes, base: int = 0, width: int = 16, skip_repeats: bool = True) -> str

Return a classic xxd-like hexdump of data.

Identical consecutive lines are collapsed into * when skip_repeats is true.

xor

xor(*args: bytes | str | int, cut: str = 'max') -> bytes

XOR byte strings together, repeating shorter keys.

cut selects the output length: "max" (default), "min", "left" or "right". Ints are treated as single bytes.

enhex

enhex(data: bytes) -> str

Bytes to a lowercase hex string.

unhex

unhex(text: str | bytes) -> bytes

Hex string to bytes. Whitespace is ignored; an odd length gets a leading 0.

b64e

b64e(data: bytes) -> str

Base64-encode data to str.

b64d

b64d(text: str | bytes) -> bytes

Base64-decode text (whitespace ignored, invalid characters rejected).

chunks

chunks(data: bytes, size: int) -> Iterator[bytes]

Yield successive size-byte slices of data.

printable_ratio

printable_ratio(data: bytes) -> float

Fraction of bytes that are printable ASCII or common whitespace (0.0 for empty).

cyclic

De Bruijn sequences for finding offsets (cyclic/cyclic_find).

cyclic

cyclic(length: int | None = None, alphabet: bytes = _DEFAULT_ALPHABET, n: int = 4) -> bytes

Return a De Bruijn sequence where every n-byte window is unique.

Without length the full sequence (len(alphabet) ** n bytes) is returned.

cyclic_find

cyclic_find(subseq: bytes | int, alphabet: bytes = _DEFAULT_ALPHABET, n: int | None = None, endian: str = 'little') -> int

Return the offset of subseq in cyclic output, or -1.

An int is converted to 4 bytes using endian (as read from a crashed register).

crc

CRC helpers: a generic parametrised CRC plus common presets.

crc_generic

crc_generic(data: bytes, width: int, poly: int, init: int = 0, refin: bool = False, refout: bool = False, xorout: int = 0) -> int

Bitwise CRC using the Rocksoft/Williams parameter model (any width 1..64).

crc32

crc32(data: bytes, init: int = 0) -> int

Standard CRC-32 (zlib/IEEE 802.3, as used by U-Boot env and uImage headers).

crc32c

crc32c(data: bytes) -> int

CRC-32C (Castagnoli).

crc16

crc16(data: bytes, poly: int = 4129, init: int = 65535, refin: bool = False, refout: bool = False, xorout: int = 0) -> int

Generic CRC-16. Defaults to CRC-16/CCITT-FALSE.

crc16_ccitt

crc16_ccitt(data: bytes) -> int

CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF).

crc16_xmodem

crc16_xmodem(data: bytes) -> int

CRC-16/XMODEM (poly 0x1021, init 0).

crc16_modbus

crc16_modbus(data: bytes) -> int

CRC-16/MODBUS (poly 0x8005 reflected, init 0xFFFF).

crc8

crc8(data: bytes, poly: int = 7, init: int = 0, refin: bool = False, refout: bool = False, xorout: int = 0) -> int

Generic CRC-8. Defaults to CRC-8/SMBUS (poly 0x07).