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 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
¶
Pack value into 1 byte (see pack).
p16
¶
Pack value into 2 bytes (see pack).
p32
¶
Pack value into 4 bytes (see pack).
p64
¶
Pack value into 8 bytes (see pack).
u8
¶
Unpack exactly 1 byte into an int (see unpack).
u16
¶
Unpack exactly 2 bytes into an int (see unpack).
u32
¶
Unpack exactly 4 bytes into an int (see unpack).
u64
¶
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
¶
Return a classic xxd-like hexdump of data.
Identical consecutive lines are collapsed into * when skip_repeats is true.
xor
¶
XOR byte strings together, repeating shorter keys.
cut selects the output length: "max" (default), "min", "left" or "right".
Ints are treated as single bytes.
unhex
¶
Hex string to bytes. Whitespace is ignored; an odd length gets a leading 0.
b64d
¶
Base64-decode text (whitespace ignored, invalid characters rejected).
chunks
¶
Yield successive size-byte slices of data.
printable_ratio
¶
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
¶
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
¶
Standard CRC-32 (zlib/IEEE 802.3, as used by U-Boot env and uImage headers).
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.
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).