-
Notifications
You must be signed in to change notification settings - Fork 152
feature/zstd #72
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
EdwardLarson
merged 5 commits into
redballoonsecurity:master
from
thejoelpatrol:feature/zstd
Oct 19, 2022
Merged
feature/zstd #72
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import subprocess | ||
| import tempfile | ||
| from dataclasses import dataclass | ||
| from typing import Optional | ||
|
|
||
| from ofrak import Packer, Unpacker, Resource | ||
| from ofrak.component.packer import PackerError | ||
| from ofrak.component.unpacker import UnpackerError | ||
| from ofrak.core import ( | ||
| GenericBinary, | ||
| format_called_process_error, | ||
| MagicMimeIdentifier, | ||
| MagicDescriptionIdentifier, | ||
| ) | ||
| from ofrak.model.component_model import CC, ComponentConfig | ||
| from ofrak_type.range import Range | ||
|
|
||
|
|
||
| class ZstdData(GenericBinary): | ||
| """ | ||
| A zstd binary blob. | ||
| """ | ||
|
|
||
| async def get_child(self) -> GenericBinary: | ||
| return await self.resource.get_only_child_as_view(GenericBinary) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZstdPackerConfig(ComponentConfig): | ||
| compression_level: int | ||
|
rbs-jacob marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class ZstdUnpacker(Unpacker[None]): | ||
| """ | ||
| Unpack (decompress) a zstd file. | ||
| """ | ||
|
|
||
| id = b"ZstdUnpacker" | ||
| targets = (ZstdData,) | ||
| children = (GenericBinary,) | ||
|
|
||
| async def unpack(self, resource: Resource, config: CC) -> None: | ||
| with tempfile.NamedTemporaryFile(suffix=".zstd") as compressed_file: | ||
| compressed_file.write(await resource.get_data()) | ||
| compressed_file.flush() | ||
| output_filename = tempfile.mktemp() | ||
|
|
||
| command = ["zstd", "-d", "-k", compressed_file.name, "-o", output_filename] | ||
| try: | ||
| subprocess.run(command, check=True) | ||
| with open(output_filename, "rb") as f: | ||
| result = f.read() | ||
| except subprocess.CalledProcessError as e: | ||
| raise UnpackerError(format_called_process_error(e)) | ||
|
|
||
| await resource.create_child(tags=(GenericBinary,), data=result) | ||
|
|
||
|
|
||
| class ZstdPacker(Packer[ZstdPackerConfig]): | ||
| """ | ||
| Pack data into a compressed zstd file. | ||
| """ | ||
|
|
||
| targets = (ZstdData,) | ||
|
|
||
| async def pack(self, resource: Resource, config: Optional[ZstdPackerConfig] = None): | ||
| if config is None: | ||
| config = ZstdPackerConfig(compression_level=19) | ||
| zstd_view = await resource.view_as(ZstdData) | ||
| child_file = await zstd_view.get_child() | ||
| uncompressed_data = await child_file.resource.get_data() | ||
|
|
||
| with tempfile.NamedTemporaryFile() as uncompressed_file: | ||
| uncompressed_file.write(uncompressed_data) | ||
| uncompressed_file.flush() | ||
| output_filename = tempfile.mktemp() | ||
|
|
||
| command = ["zstd", "-T0", f"-{config.compression_level}"] | ||
| if config.compression_level > 19: | ||
| command.append("--ultra") | ||
| command.extend([uncompressed_file.name, "-o", output_filename]) | ||
| try: | ||
| subprocess.run(command, check=True) | ||
| with open(output_filename, "rb") as f: | ||
| result = f.read() | ||
| except subprocess.CalledProcessError as e: | ||
| raise PackerError(format_called_process_error(e)) | ||
|
|
||
| compressed_data = result | ||
| original_size = await zstd_view.resource.get_data_length() | ||
| resource.queue_patch(Range(0, original_size), compressed_data) | ||
|
|
||
|
|
||
| MagicMimeIdentifier.register(ZstdData, "application/x-zstd") | ||
| MagicDescriptionIdentifier.register( | ||
| ZstdData, lambda s: s.lower().startswith("zstandard compressed data") | ||
| ) | ||
45 changes: 45 additions & 0 deletions
45
ofrak_components/ofrak_components_test/test_zstd_component.py
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,45 @@ | ||
| import subprocess | ||
| import tempfile | ||
|
|
||
| import pytest | ||
|
|
||
| from ofrak.core.filesystem import format_called_process_error | ||
| from ofrak.resource import Resource | ||
| from pytest_ofrak.patterns.compressed_filesystem_unpack_modify_pack import ( | ||
| CompressedFileUnpackModifyPackPattern, | ||
| ) | ||
|
|
||
|
|
||
| class TestZstdUnpackModifyPack(CompressedFileUnpackModifyPackPattern): | ||
| @pytest.fixture(autouse=True) | ||
| def create_test_file(self, tmpdir): | ||
| d = tmpdir.mkdir("zstd") | ||
| uncompressed_filename = d.join("hello.txt").realpath() | ||
| with open(uncompressed_filename, "wb") as f: | ||
| f.write(self.INITIAL_DATA) | ||
|
|
||
| compressed_filename = d.join("hello.zstd").realpath() | ||
| command = ["zstd", "-19", uncompressed_filename, "-o", compressed_filename] | ||
| try: | ||
| subprocess.run(command, check=True, capture_output=True) | ||
| except subprocess.CalledProcessError as e: | ||
| raise RuntimeError(format_called_process_error(e)) | ||
|
|
||
| self._test_file = compressed_filename | ||
|
|
||
| async def verify(self, repacked_root_resource: Resource) -> None: | ||
| compressed_data = await repacked_root_resource.get_data() | ||
| with tempfile.NamedTemporaryFile(suffix=".zstd") as compressed_file: | ||
| compressed_file.write(compressed_data) | ||
| compressed_file.flush() | ||
| output_filename = tempfile.mktemp() | ||
|
|
||
| command = ["zstd", "-d", "-k", compressed_file.name, "-o", output_filename] | ||
| try: | ||
| subprocess.run(command, check=True, capture_output=True) | ||
| with open(output_filename, "rb") as f: | ||
| result = f.read() | ||
| except subprocess.CalledProcessError as e: | ||
| raise RuntimeError(format_called_process_error(e)) | ||
|
|
||
| assert result == self.EXPECTED_REPACKED_DATA |
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.