Skip to content

Commit c13c28e

Browse files
committed
chore(dissect.cstruct): Git rid of Instance type annotation
- It was already interpreted essentially the same way as `typing.Any`, because of its `__getattr__` method - Newer versions no longer have the `Instance` proxy object - `cstruct().<x>` calls are annotated with `Any` in new versions - The actual runtime type will be `Structure` in newer versions that needs to be changed in `logging` (not changed for now) Relates-to: #888
1 parent e635cae commit c13c28e

File tree

20 files changed

+53
-74
lines changed

20 files changed

+53
-74
lines changed

tests/handlers/filesystem/test_jffs2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import binascii
2+
from typing import Any
23

34
import pytest
4-
from dissect.cstruct import Instance
55
from helpers import unhex
66

77
from unblob.file_utils import Endian, File
@@ -59,7 +59,7 @@ def get_valid_jffs2_old_be_header():
5959
)
6060

6161

62-
def calculate_crc(header: Instance):
62+
def calculate_crc(header: Any):
6363
return (binascii.crc32(header.dumps()[:-4], -1) ^ -1) & 0xFFFFFFFF
6464

6565

@@ -189,7 +189,7 @@ def calculate_crc(header: Instance):
189189
],
190190
)
191191
def test_valid_header_new(
192-
header: Instance, node_start_offset: int, eof: int, expected: bool
192+
header: Any, node_start_offset: int, eof: int, expected: bool
193193
):
194194
header.hdr_crc = calculate_crc(header)
195195
assert new_handler.valid_header(header, node_start_offset, eof) == expected
@@ -271,7 +271,7 @@ def test_valid_header_new(
271271
],
272272
)
273273
def test_valid_header_old(
274-
header: Instance, node_start_offset: int, eof: int, expected: bool
274+
header: Any, node_start_offset: int, eof: int, expected: bool
275275
):
276276
header.hdr_crc = calculate_crc(header)
277277
assert old_handler.valid_header(header, node_start_offset, eof) == expected

unblob/file_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import struct
1111
import unicodedata
1212
from pathlib import Path
13-
from typing import Iterable, Iterator, List, Literal, Optional, Tuple, Union
13+
from typing import Any, Iterable, Iterator, List, Literal, Optional, Tuple, Union
1414

15-
from dissect.cstruct import Instance, cstruct
15+
from dissect.cstruct import cstruct
1616
from structlog import get_logger
1717

1818
from .logging import format_hex
@@ -336,7 +336,7 @@ def parse(
336336
struct_name: str,
337337
file: Union[File, bytes],
338338
endian: Endian,
339-
) -> Instance:
339+
) -> Any:
340340
cparser = self.cparser_le if endian is Endian.LITTLE else self.cparser_be
341341
struct_parser = getattr(cparser, struct_name)
342342
return struct_parser(file)

unblob/handlers/archive/arc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from typing import Optional
1+
from typing import Any, Optional
22

3-
from dissect.cstruct import Instance
43
from structlog import get_logger
54

65
from unblob.extractors.command import Command
@@ -54,7 +53,7 @@ def valid_name(self, name: bytes) -> bool:
5453
except UnicodeDecodeError:
5554
return False
5655

57-
def valid_header(self, header: Instance) -> bool:
56+
def valid_header(self, header: Any) -> bool:
5857
if header.archive_marker != 0x1A:
5958
return False
6059
if header.header_type > 0x07:

unblob/handlers/archive/cpio.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import os
33
import stat
44
from pathlib import Path
5-
from typing import Optional, Type
5+
from typing import Any, Optional, Type
66

77
import attr
8-
from dissect.cstruct import Instance
98
from structlog import get_logger
109

1110
from ...file_utils import (
@@ -252,7 +251,7 @@ def _pad_file(self, end_offset: int) -> int:
252251
return end_offset
253252

254253
@classmethod
255-
def _pad_header(cls, header: Instance, c_namesize: int) -> int:
254+
def _pad_header(cls, header: Any, c_namesize: int) -> int:
256255
return round_up(len(header) + c_namesize, cls._PAD_ALIGN)
257256

258257
@classmethod

unblob/handlers/archive/dlink/encrpted_img.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import io
22
from pathlib import Path
3-
from typing import Optional
3+
from typing import Any, Optional
44

55
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
6-
from dissect.cstruct import Instance
76
from structlog import get_logger
87

98
from unblob.file_utils import File, InvalidInputFormat
@@ -53,7 +52,7 @@ class EncrptedHandler(StructHandler):
5352
HEADER_STRUCT = "dlink_header_t"
5453
EXTRACTOR = EncrptedExtractor()
5554

56-
def is_valid_header(self, header: Instance) -> bool:
55+
def is_valid_header(self, header: Any) -> bool:
5756
if header.size < len(header):
5857
return False
5958
return True

unblob/handlers/archive/dlink/shrs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import hashlib
22
import io
33
from pathlib import Path
4-
from typing import Optional
4+
from typing import Any, Optional
55

66
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
7-
from dissect.cstruct import Instance
87
from structlog import get_logger
98

109
from unblob.file_utils import File, InvalidInputFormat
@@ -70,7 +69,7 @@ class SHRSHandler(StructHandler):
7069
HEADER_STRUCT = "dlink_header_t"
7170
EXTRACTOR = SHRSExtractor()
7271

73-
def is_valid_header(self, header: Instance, file: File) -> bool:
72+
def is_valid_header(self, header: Any, file: File) -> bool:
7473
if header.file_size < len(header):
7574
return False
7675
# we're exactly past the header, we compute the digest

unblob/handlers/archive/engeniustech/engenius.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from pathlib import Path
2-
from typing import Optional
2+
from typing import Any, Optional
33

4-
from dissect.cstruct import Instance
54
from structlog import get_logger
65

76
from unblob.file_utils import Endian, File, InvalidInputFormat, StructParser
@@ -81,7 +80,7 @@ class EngeniusHandler(StructHandler):
8180
EXTRACTOR = EngeniusExtractor()
8281
PATTERN_MATCH_OFFSET = -0x5C
8382

84-
def is_valid_header(self, header: Instance) -> bool:
83+
def is_valid_header(self, header: Any) -> bool:
8584
if header.length <= len(header):
8685
return False
8786
try:

unblob/handlers/archive/hp/bdl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import io
22
from pathlib import Path
3-
from typing import Optional
3+
from typing import Any, Optional
44

5-
from dissect.cstruct import Instance
65
from structlog import get_logger
76

87
from unblob.extractor import carve_chunk_to_file
@@ -35,7 +34,7 @@
3534
"""
3635

3736

38-
def is_valid_header(header: Instance) -> bool:
37+
def is_valid_header(header: Any) -> bool:
3938
if header.toc_offset == 0 or header.toc_entries == 0:
4039
return False
4140
try:

unblob/handlers/archive/hp/ipkg.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import io
22
from pathlib import Path
3-
from typing import Optional
3+
from typing import Any, Optional
44

5-
from dissect.cstruct import Instance
65
from structlog import get_logger
76

87
from unblob.file_utils import (
@@ -48,7 +47,7 @@
4847
"""
4948

5049

51-
def is_valid_header(header: Instance) -> bool:
50+
def is_valid_header(header: Any) -> bool:
5251
if header.toc_offset == 0 or header.toc_entries == 0:
5352
return False
5453
try:

unblob/handlers/archive/instar/bneg.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from pathlib import Path
2-
from typing import Optional
2+
from typing import Any, Optional
33

4-
from dissect.cstruct import Instance
54
from structlog import get_logger
65

76
from unblob.extractor import carve_chunk_to_file
@@ -70,7 +69,7 @@ class BNEGHandler(StructHandler):
7069
HEADER_STRUCT = "bneg_header_t"
7170
EXTRACTOR = BNEGExtractor()
7271

73-
def is_valid_header(self, header: Instance) -> bool:
72+
def is_valid_header(self, header: Any) -> bool:
7473
if header.partition_1_size == 0:
7574
return False
7675
if header.partition_2_size == 0:

0 commit comments

Comments
 (0)