Fixed-integer typesο
Object propertiesο
- class malduck.ints.IntType(value: Any)[source]ο
Fixed-size variant of int type with C-style operators and casting
Supports ctypes-like multiplication for unpacking tuple of values
IntTypes are derived from
int
type, so they are fully compatible with other numeric typesres = u32(0x8080FFFF) << 16 | 0xFFFF > 0xFFFFFFFF res = Int32(res) > -1
Using IntTypes you donβt need to mask everything with 0xFFFFFFFF, only if you remember about appropriate casting.
from malduck import DWORD def rol7_hash(name: bytes): hh = 0 for c in name: hh = DWORD(x).rol(7) ^ c return x def sdbm_hash(name: bytes): hh = 0 for c in name: hh = DWORD(c) + (hh << 6) + (hh << 16) - hh return hh
Type coercion between native and fixed integers depends on LHS type:
UInt32 = UInt32 + int int = int + UInt32
IntTypes can be multiplied like ctypes classes for unpacking tuple of values:
values = (BYTE * 3).unpack('\x01\x02\x03') values -> (1, 2, 3)
- classmethod unpack(other: bytes, offset: int = 0, fixed: bool = True) IntType | int | None [source]ο
Unpacks single value from provided buffer with little-endian order
- Parameters:
other (bytes) β Buffer object containing value to unpack
offset (int) β Buffer offset
fixed (bool (default: True)) β Convert to fixed-size integer (IntType instance)
- Return type:
IntType instance or None if there are not enough data to unpack
Warning
Fixed-size integer operations are 4-5 times slower than equivalent on built-in integer types
- classmethod unpack_be(other: bytes, offset: int = 0, fixed: bool = True) IntType | int | None [source]ο
Unpacks single value from provided buffer with big-endian order
- Parameters:
other (bytes) β Buffer object containing value to unpack
offset (int) β Buffer offset
fixed (bool (default: True)) β Convert to fixed-size integer (IntType instance)
- Return type:
IntType instance or None if there are not enough data to unpack
Warning
Fixed-size integer operations are 4-5 times slower than equivalent on built-in integer types