Skip to content

Commit 8940df3

Browse files
committed
Add type checking
1 parent ab5e6d1 commit 8940df3

40 files changed

+349
-182
lines changed

dissect/util/_build.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Reference: https://setuptools.pypa.io/en/latest/build_meta.html#dynamic-build-dependencies-and-other-build-meta-tweaks
2+
# type: ignore
23
from __future__ import annotations
34

45
import os

dissect/util/compression/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
try:
2424
from dissect.util import _native
2525

26-
lz4 = lz4_native = _native.compression.lz4
27-
lzo = lzo_native = _native.compression.lzo
26+
lz4 = lz4_native = _native.compression.lz4 # type: ignore
27+
lzo = lzo_native = _native.compression.lzo # type: ignore
2828
except (ImportError, AttributeError):
2929
lz4_native = lzo_native = None
3030

dissect/util/compression/lz4.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def _get_length(src: BinaryIO, length: int) -> int:
2525

2626

2727
def decompress(
28-
src: bytes | BinaryIO,
28+
src: bytes | bytearray | memoryview | BinaryIO,
2929
uncompressed_size: int = -1,
3030
return_bytearray: bool = False,
31-
) -> bytes | tuple[bytes, int]:
31+
) -> bytes | bytearray | tuple[bytes | bytearray, int]:
3232
"""LZ4 decompress from a file-like object or bytes up to a certain length. Assumes no header.
3333
3434
Args:
@@ -39,7 +39,7 @@ def decompress(
3939
Returns:
4040
The decompressed data.
4141
"""
42-
if not hasattr(src, "read"):
42+
if isinstance(src, bytes | bytearray | memoryview):
4343
src = io.BytesIO(src)
4444

4545
dst = bytearray()

dissect/util/compression/lzbitmap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_H = struct.Struct("<H")
1212

1313

14-
def decompress(src: bytes | BinaryIO) -> bytes:
14+
def decompress(src: bytes | bytearray | memoryview | BinaryIO) -> bytes:
1515
"""LZBITMAP decompress from a file-like object or bytes.
1616
1717
Decompresses until EOF or EOS of the input data.
@@ -22,7 +22,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
2222
Returns:
2323
The decompressed data.
2424
"""
25-
if not hasattr(src, "read"):
25+
if isinstance(src, bytes | bytearray | memoryview):
2626
src = io.BytesIO(src)
2727

2828
if src.read(4) != b"ZBM\x09":
@@ -54,7 +54,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
5454
buf = memoryview(src.read(compressed_size))
5555

5656
# Build the bitmap/token map
57-
token_map = []
57+
token_map: list[tuple[int | None, int]] = []
5858
bits = int.from_bytes(buf[-17:], "little")
5959
for i in range(0xF):
6060
if i < 3:
@@ -97,7 +97,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
9797

9898
for _ in range(repeat):
9999
bitmap, token = token_map[idx]
100-
if idx < 3:
100+
if bitmap is None: # idx < 3, but this makes the type checker happy
101101
# Index 0, 1, 2 are special and indicate we need to read a bitmap from the bitmap region
102102
bitmap = buf[bitmap_offset]
103103
bitmap_offset += 1

dissect/util/compression/lzfse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def _decode_lmd(
398398
return bytes(dst)
399399

400400

401-
def decompress(src: bytes | BinaryIO) -> bytes:
401+
def decompress(src: bytes | bytearray | memoryview | BinaryIO) -> bytes:
402402
"""LZFSE decompress from a file-like object or bytes.
403403
404404
Decompresses until EOF or EOS of the input data.
@@ -409,7 +409,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
409409
Returns:
410410
The decompressed data.
411411
"""
412-
if not hasattr(src, "read"):
412+
if isinstance(src, bytes | bytearray | memoryview):
413413
src = io.BytesIO(src)
414414

415415
dst = bytearray()

dissect/util/compression/lznt1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _get_displacement(offset: int) -> int:
2525
TAG_MASKS = [(1 << i) for i in range(8)]
2626

2727

28-
def decompress(src: bytes | BinaryIO) -> bytes:
28+
def decompress(src: bytes | bytearray | memoryview | BinaryIO) -> bytes:
2929
"""LZNT1 decompress from a file-like object or bytes.
3030
3131
Args:
@@ -34,7 +34,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
3434
Returns:
3535
The decompressed data.
3636
"""
37-
if not hasattr(src, "read"):
37+
if isinstance(src, bytes | bytearray | memoryview):
3838
src = io.BytesIO(src)
3939

4040
offset = src.tell()

dissect/util/compression/lzo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _read_length(src: BinaryIO, val: int, mask: int) -> int:
2323
return length + mask + val
2424

2525

26-
def decompress(src: bytes | BinaryIO, header: bool = True, buflen: int = -1) -> bytes:
26+
def decompress(src: bytes | bytearray | memoryview | BinaryIO, header: bool = True, buflen: int = -1) -> bytes:
2727
"""LZO decompress from a file-like object or bytes. Assumes no header.
2828
2929
Arguments are largely compatible with python-lzo API.
@@ -36,7 +36,7 @@ def decompress(src: bytes | BinaryIO, header: bool = True, buflen: int = -1) ->
3636
Returns:
3737
The decompressed data.
3838
"""
39-
if not hasattr(src, "read"):
39+
if isinstance(src, bytes | bytearray | memoryview):
4040
src = io.BytesIO(src)
4141

4242
dst = bytearray()

dissect/util/compression/lzvn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
_H = struct.Struct("<H")
5757

5858

59-
def decompress(src: bytes | BinaryIO) -> bytes:
59+
def decompress(src: bytes | bytearray | memoryview | BinaryIO) -> bytes:
6060
"""LZVN decompress from a file-like object or bytes.
6161
6262
Decompresses until EOF or EOS of the input data.
@@ -67,7 +67,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
6767
Returns:
6868
The decompressed data.
6969
"""
70-
if not hasattr(src, "read"):
70+
if isinstance(src, bytes | bytearray | memoryview):
7171
src = io.BytesIO(src)
7272

7373
offset = src.tell()
@@ -207,7 +207,7 @@ def decompress(src: bytes | BinaryIO) -> bytes:
207207
if src_size < opc_len:
208208
break
209209

210-
src_size -= opc_len + L
210+
src_size -= opc_len
211211
break
212212

213213
elif opc in OP_UDEF:

0 commit comments

Comments
 (0)