-
Notifications
You must be signed in to change notification settings - Fork 84
feat(handler): add geom_uzip handler #1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import lzma | ||
import re | ||
import zlib | ||
from pathlib import Path | ||
from typing import Callable, Optional | ||
|
||
import pyzstd | ||
|
||
from unblob.file_utils import ( | ||
Endian, | ||
FileSystem, | ||
InvalidInputFormat, | ||
StructParser, | ||
iterate_file, | ||
) | ||
from unblob.models import ( | ||
Extractor, | ||
ExtractResult, | ||
File, | ||
Regex, | ||
StructHandler, | ||
ValidChunk, | ||
) | ||
|
||
# [Ref] https://github.com/freebsd/freebsd-src/tree/master/sys/geom/uzip | ||
C_DEFINITIONS = r""" | ||
typedef struct uzip_header{ | ||
char magic[16]; | ||
char format[112]; | ||
uint32_t block_size; | ||
uint32_t block_count; | ||
uint64_t toc[block_count]; | ||
} uzip_header_t; | ||
""" | ||
|
||
HEADER_STRUCT = "uzip_header_t" | ||
|
||
ZLIB_COMPRESSION = "#!/bin/sh\x0a#V2.0\x20" | ||
LZMA_COMPRESSION = "#!/bin/sh\x0a#L3.0\x0a" | ||
ZSTD_COMPRESSION = "#!/bin/sh\x0a#Z4.0\x20" | ||
|
||
|
||
class Decompressor: | ||
DECOMPRESSOR: Callable | ||
|
||
def __init__(self): | ||
self._decompressor = self.DECOMPRESSOR() | ||
|
||
def decompress(self, data: bytes) -> bytes: | ||
return self._decompressor.decompress(data) | ||
|
||
def flush(self) -> bytes: | ||
return b"" | ||
|
||
|
||
class LZMADecompressor(Decompressor): | ||
DECOMPRESSOR = lzma.LZMADecompressor | ||
|
||
|
||
class ZLIBDecompressor(Decompressor): | ||
DECOMPRESSOR = zlib.decompressobj | ||
|
||
def flush(self) -> bytes: | ||
return self._decompressor.flush() | ||
|
||
|
||
class ZSTDDecompressor(Decompressor): | ||
DECOMPRESSOR = pyzstd.EndlessZstdDecompressor | ||
|
||
|
||
DECOMPRESS_METHOD: dict[bytes, type[Decompressor]] = { | ||
ZLIB_COMPRESSION.encode(): ZLIBDecompressor, | ||
LZMA_COMPRESSION.encode(): LZMADecompressor, | ||
ZSTD_COMPRESSION.encode(): ZSTDDecompressor, | ||
} | ||
|
||
|
||
class UZIPExtractor(Extractor): | ||
def extract(self, inpath: Path, outdir: Path): | ||
with File.from_path(inpath) as infile: | ||
parser = StructParser(C_DEFINITIONS) | ||
header = parser.parse(HEADER_STRUCT, infile, Endian.BIG) | ||
fs = FileSystem(outdir) | ||
outpath = Path(inpath.stem) | ||
|
||
try: | ||
decompressor_cls = DECOMPRESS_METHOD[header.magic] | ||
except LookupError: | ||
raise InvalidInputFormat("unsupported compression format") from None | ||
|
||
with fs.open(outpath, "wb+") as outfile: | ||
for current_offset, next_offset in zip(header.toc[:-1], header.toc[1:]): | ||
compressed_len = next_offset - current_offset | ||
if compressed_len == 0: | ||
continue | ||
decompressor = decompressor_cls() | ||
for chunk in iterate_file(infile, current_offset, compressed_len): | ||
outfile.write(decompressor.decompress(chunk)) | ||
outfile.write(decompressor.flush()) | ||
return ExtractResult(reports=fs.problems) | ||
|
||
|
||
class UZIPHandler(StructHandler): | ||
NAME = "uzip" | ||
PATTERNS = [ | ||
Regex(re.escape(ZLIB_COMPRESSION)), | ||
Regex(re.escape(LZMA_COMPRESSION)), | ||
Regex(re.escape(ZSTD_COMPRESSION)), | ||
] | ||
HEADER_STRUCT = HEADER_STRUCT | ||
C_DEFINITIONS = C_DEFINITIONS | ||
EXTRACTOR = UZIPExtractor() | ||
|
||
def is_valid_header(self, header) -> bool: | ||
return ( | ||
header.block_count > 0 | ||
and header.block_size > 0 | ||
and header.block_size % 512 == 0 | ||
) | ||
|
||
def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]: | ||
header = self.parse_header(file, Endian.BIG) | ||
|
||
if not self.is_valid_header(header): | ||
raise InvalidInputFormat("Invalid uzip header.") | ||
|
||
# take the last TOC block offset, end of file is that block offset, | ||
# starting from the start offset | ||
end_offset = start_offset + header.toc[-1] | ||
return ValidChunk( | ||
start_offset=start_offset, | ||
end_offset=end_offset, | ||
) |
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/lzma/__input__/myfs.img.ulzma
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/lzma/__output__/myfs.img.ulzma_extract/0-59316.uzip
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...tion/compression/uzip/lzma/__output__/myfs.img.ulzma_extract/0-59316.uzip_extract/0-59316
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...s/integration/compression/uzip/lzma/__output__/myfs.img.ulzma_extract/59316-59392.padding
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zlib/__input__/myfs.img.uzip
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zlib/__input__/myfs.padded.img.uzip
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zlib/__output__/myfs.img.uzip_extract/0-59397.uzip
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...ation/compression/uzip/zlib/__output__/myfs.img.uzip_extract/0-59397.uzip_extract/0-59397
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zlib/__output__/myfs.img.uzip_extract/59397-59904.padding
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zlib/__output__/myfs.padded.img.uzip_extract/0-64.unknown
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...gration/compression/uzip/zlib/__output__/myfs.padded.img.uzip_extract/59461-60032.unknown
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...s/integration/compression/uzip/zlib/__output__/myfs.padded.img.uzip_extract/64-59461.uzip
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...pression/uzip/zlib/__output__/myfs.padded.img.uzip_extract/64-59461.uzip_extract/64-59461
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zstd/__input__/myfs.img.uzst
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zstd/__output__/myfs.img.uzst_extract/0-58269.uzip
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...ation/compression/uzip/zstd/__output__/myfs.img.uzst_extract/0-58269.uzip_extract/0-58269
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/compression/uzip/zstd/__output__/myfs.img.uzst_extract/58269-58368.padding
Git LFS file not shown
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.