Skip to content

Commit

Permalink
Add missing fixed argument for multiplied ints (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
nazywam authored May 24, 2022
1 parent e6a0b78 commit addb808
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
25 changes: 11 additions & 14 deletions malduck/ints.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
from abc import ABCMeta, abstractmethod
from struct import error, pack, unpack_from
from typing import (
Any,
Callable,
Generic,
Iterator,
Optional,
Tuple,
Type,
TypeVar,
Union,
cast,
)
from typing import Any, Callable, Generic, Iterator, Tuple, Type, TypeVar, Union, cast

from .bits import rol

Expand Down Expand Up @@ -49,7 +38,9 @@ class MultipliedIntTypeBase(IntTypeBase, Generic[T], metaclass=ABCMeta):

@staticmethod
@abstractmethod
def unpack(other: bytes, offset: int = 0) -> Optional[Tuple[T, ...]]:
def unpack(
other: bytes, offset: int = 0, foxed: bool = False
) -> Union[Tuple[T, ...], int, None]:
raise NotImplementedError()


Expand Down Expand Up @@ -87,7 +78,9 @@ class MultipliedIntTypeClass(MultipliedIntTypeBase):
mul = multiplier

@staticmethod
def unpack(other: bytes, offset: int = 0) -> Optional[Tuple[T, ...]]:
def unpack(
other: bytes, offset: int = 0, fixed: bool = True
) -> Union[Tuple[T, ...], int, None]:
"""
Unpacks multiple values from provided buffer
:param other: Buffer object containing value to unpack
Expand All @@ -99,6 +92,10 @@ def unpack(other: bytes, offset: int = 0) -> Optional[Tuple[T, ...]]:
ret = unpack_from(fmt, other, offset=offset)
except error:
return None

if not fixed:
return tuple(ret)

ints: Iterator[T] = map(cls, ret)
return tuple(ints)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ def test_multi_unpack():
assert (Int64 * 3).unpack(b"\x11\x22\x33\x44\xff\xff\xff\xff\x00\x00\x00\x00") is None


def test_fixed():
assert type(UInt32.unpack(b'A'*16)) is UInt32
assert type(UInt32.unpack(b'A'*16, fixed=False)) is int

assert type((UInt32*4).unpack(b'A'*16)[0]) is UInt32
assert type((UInt32*4).unpack(b'A'*16, fixed=False)[0]) is int


def test_unpack_from():
assert UInt32.unpack(b"\xAA\xAA\xF0\x0F\xF0\x0B\xAA", offset=2) == u32(b"\xF0\x0F\xF0\x0B")
assert UInt32.unpack(b"\xAA\xAA\xF0\x0F\xF0\x0B", offset=2) == u32(b"\xF0\x0F\xF0\x0B")
Expand Down

0 comments on commit addb808

Please sign in to comment.