Skip to content

Commit 4abbc8c

Browse files
e3krisztianqkaiser
authored andcommitted
refactor(hdr): use FileSystem to report problems during extraction
1 parent edb4e5f commit 4abbc8c

File tree

1 file changed

+17
-17
lines changed
  • unblob/handlers/archive/xiaomi

1 file changed

+17
-17
lines changed

unblob/handlers/archive/xiaomi/hdr.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
from dissect.cstruct import Instance
77
from structlog import get_logger
88

9-
from unblob.extractor import carve_chunk_to_file, is_safe_path
10-
from unblob.file_utils import File, InvalidInputFormat, iterate_file, snull
9+
from unblob.file_utils import (
10+
File,
11+
FileSystem,
12+
InvalidInputFormat,
13+
iterate_file,
14+
snull,
15+
)
1116
from unblob.models import (
12-
Chunk,
1317
Endian,
1418
Extractor,
19+
ExtractResult,
1520
HexString,
1621
StructHandler,
1722
StructParser,
@@ -95,16 +100,13 @@ def __init__(self, header_struct: str):
95100
self._struct_parser = StructParser(C_DEFINITIONS)
96101

97102
def extract(self, inpath: Path, outdir: Path):
103+
fs = FileSystem(outdir)
98104
with File.from_path(inpath) as file:
99-
for output_path, chunk in self.parse(file):
100-
if not is_safe_path(outdir, output_path):
101-
logger.warning(
102-
"Path traversal attempt, discarding.", output_path=output_path
103-
)
104-
return
105-
carve_chunk_to_file(outdir.joinpath(output_path), file, chunk)
106-
107-
def parse(self, file: File) -> Iterable[Tuple[Path, Chunk]]:
105+
for output_path, start_offset, size in self.parse(file):
106+
fs.carve(output_path, file, start_offset, size)
107+
return ExtractResult(reports=list(fs.problems))
108+
109+
def parse(self, file: File) -> Iterable[Tuple[Path, int, int]]:
108110
header = self._struct_parser.parse(self.header_struct, file, Endian.LITTLE)
109111
for offset in cast(Iterable, header.blob_offsets):
110112
if not offset:
@@ -118,14 +120,12 @@ def parse(self, file: File) -> Iterable[Tuple[Path, Chunk]]:
118120
if not is_valid_blob_header(blob_header):
119121
raise InvalidInputFormat("Invalid HDR blob header.")
120122

121-
# file.tell() points to right after the blob_header
122123
yield (
123124
(
124125
Path(snull(blob_header.name).decode("utf-8")),
125-
Chunk(
126-
start_offset=file.tell(),
127-
end_offset=file.tell() + blob_header.size,
128-
),
126+
# file.tell() points to right after the blob_header == start_offset
127+
file.tell(),
128+
blob_header.size,
129129
)
130130
)
131131

0 commit comments

Comments
 (0)