Skip to content

Commit

Permalink
Select between native and Python implementations of lz4 and lzo
Browse files Browse the repository at this point in the history
Two new extra's "lz4" and "lzo" are defined which will install the
needed Python packages to do naitve lz4 and lzo decompression.

When installed the dissect.util.compression.lz4 and
dissect.util.compression.lzo modules will point to these native
versions. Otherwise they will point to the pure Python implementations
in this project.

The native (when available) and Python modules can be accessed
explicitly through:

- dissect.util.compression.lz4_native
- dissect.util.compression.lz4_python
- dissect.util.compression.lzo_native
- dissect.util.compression.lzo_python
  • Loading branch information
pyrco committed Jul 19, 2024
1 parent bd0f0a9 commit 8fe3b96
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
29 changes: 29 additions & 0 deletions dissect/util/compression/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from dissect.util.compression import lz4 as lz4_python
from dissect.util.compression import lzo as lzo_python

try:
import lz4.block as lz4
import lz4.block as lz4_native

Check warning on line 6 in dissect/util/compression/__init__.py

View check run for this annotation

Codecov / codecov/patch

dissect/util/compression/__init__.py#L6

Added line #L6 was not covered by tests
except ImportError:
lz4 = lz4_python
lz4_native = None

try:
import lzo
import lzo as lzo_native

Check warning on line 13 in dissect/util/compression/__init__.py

View check run for this annotation

Codecov / codecov/patch

dissect/util/compression/__init__.py#L13

Added line #L13 was not covered by tests
except ImportError:
lzo = lzo_python
lzo_native = None

__all__ = [
"lz4",
"lz4_native",
"lz4_python",
"lznt1",
"lzo",
"lzo_native",
"lzo_python",
"lzxpress",
"lzxpress_huffman",
"sevenbit",
]
8 changes: 7 additions & 1 deletion dissect/util/compression/lz4.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ def _get_length(src: BinaryIO, length: int) -> int:


def decompress(
src: Union[bytes, BinaryIO], max_length: int = -1, return_bytearray: bool = False, return_bytes_read: bool = False
src: Union[bytes, BinaryIO],
uncompressed_size: int = -1,
max_length: int = -1,
return_bytearray: bool = False,
return_bytes_read: bool = False,
) -> Union[bytes, tuple[bytes, int]]:
"""LZ4 decompress from a file-like object up to a certain length. Assumes no header.
Args:
src: File-like object to decompress from.
uncompressed_size: Ignored, present for compatibility with native lz4. The ``max_length``
parameter sort-of but not completely has the same function.
max_length: Decompress up to this many result bytes.
return_bytearray: Whether to return ``bytearray`` or ``bytes``.
return_bytes_read: Whether to return a tuple of ``(data, bytes_read)`` or just the data.
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ homepage = "https://dissect.tools"
documentation = "https://docs.dissect.tools/en/latest/projects/dissect.util"
repository = "https://github.com/fox-it/dissect.util"

[project.optional-dependencies]
lzo = [
# There are no Windows PyPy wheels available for python-lzo
# So we use a pure python fallback for it.
"python-lzo; platform_system != 'Windows' or platform_python_implementation != 'PyPy'",
]
lz4 = [
"lz4",
]

[project.scripts]
dump-nskeyedarchiver = "dissect.util.tools.dump_nskeyedarchiver:main"

Expand Down

0 comments on commit 8fe3b96

Please sign in to comment.