Skip to content

Commit

Permalink
Add support for various BCJ filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Mar 24, 2019
1 parent 5d50ae2 commit dec3b88
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 0 deletions.
60 changes: 60 additions & 0 deletions py7zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def bytes(s, encoding):
COMPRESSION_METHOD_MISC_BZIP = unhexlify('0402') # '\x04\x02'
COMPRESSION_METHOD_7Z_AES256_SHA256 = unhexlify('06f10701') # '\x06\xf1\x07\x01'
COMPRESSION_METHOD_LZMA2 = unhexlify('21') # '\x21'
COMPRESSION_METHOD_BCJ = unhexlify('03030103') # '\x03\x03\x01\x03'
COMPRESSION_METHOD_BCJ_PPC = unhexlify('03030205') # '\x03\x03\x02\x05'
COMPRESSION_METHOD_BCJ_IA64 = unhexlify('03030401') # '\x03\x03\x03\x01'
COMPRESSION_METHOD_BCJ_ARM = unhexlify('03030501') # '\x03\x03\x05\x01'
COMPRESSION_METHOD_BCJ_ARMT = unhexlify('03030701') # '\x03\x03\x07\x01'
COMPRESSION_METHOD_BCJ_SPARC = unhexlify('03030805') # '\x03\x03\x08\x05'
COMPRESSION_METHOD_BCJ2 = unhexlify('0303011B') # '\x03\x03\x01\x1B'

FILE_ATTRIBUTE_DIRECTORY = 0x10
Expand Down Expand Up @@ -604,6 +610,12 @@ def __init__(self, info, start, src_start, folder, archive, maxsize=None):
COMPRESSION_METHOD_MISC_ZIP: '_read_zip',
COMPRESSION_METHOD_MISC_BZIP: '_read_bzip',
COMPRESSION_METHOD_7Z_AES256_SHA256: '_read_7z_aes256_sha256',
COMPRESSION_METHOD_BCJ: '_read_bcj',
COMPRESSION_METHOD_BCJ_PPC: '_read_bcj_ppc',
COMPRESSION_METHOD_BCJ_IA64: '_read_bcj_ia64',
COMPRESSION_METHOD_BCJ_ARM: '_read_bcj_arm',
COMPRESSION_METHOD_BCJ_ARMT: '_read_bcj_armt',
COMPRESSION_METHOD_BCJ_SPARC: '_read_bcj_sparc',
COMPRESSION_METHOD_BCJ2: '_read_bcj2',
}

Expand Down Expand Up @@ -784,6 +796,54 @@ def _read_bcj2(self, coder, input, level, num_coders):
rc_data = self._file.read(self._packsizes[3])
return pylzma.bcj2_decode(main_data, call_data, jump_data, rc_data, self.uncompressed)

def _read_bcj(self, coder, input, level, num_coders):
size = self._uncompressed[level]
if not input:
self._file.seek(self._src_start)
input = self._file.read(size)
data = pylzma.bcj_x86_convert(input)
return data[self._start:self._start+size]

def _read_bcj_ppc(self, coder, input, level, num_coders):
size = self._uncompressed[level]
if not input:
self._file.seek(self._src_start)
input = self._file.read(size)
data = pylzma.bcj_ppc_convert(input)
return data[self._start:self._start+size]

def _read_bcj_ia64(self, coder, input, level, num_coders):
size = self._uncompressed[level]
if not input:
self._file.seek(self._src_start)
input = self._file.read(size)
data = pylzma.bcj_ia64_convert(input)
return data[self._start:self._start+size]

def _read_bcj_arm(self, coder, input, level, num_coders):
size = self._uncompressed[level]
if not input:
self._file.seek(self._src_start)
input = self._file.read(size)
data = pylzma.bcj_arm_convert(input)
return data[self._start:self._start+size]

def _read_bcj_armt(self, coder, input, level, num_coders):
size = self._uncompressed[level]
if not input:
self._file.seek(self._src_start)
input = self._file.read(size)
data = pylzma.bcj_armt_convert(input)
return data[self._start:self._start+size]

def _read_bcj_sparc(self, coder, input, level, num_coders):
size = self._uncompressed[level]
if not input:
self._file.seek(self._src_start)
input = self._file.read(size)
data = pylzma.bcj_sparc_convert(input)
return data[self._start:self._start+size]

def checkcrc(self):
if self.digest is None:
return True
Expand Down
Binary file added tests/data/lzma_bcj.7z
Binary file not shown.
Binary file added tests/data/lzma_bcj_arm.7z
Binary file not shown.
Binary file added tests/data/lzma_bcj_armt.7z
Binary file not shown.
Binary file added tests/data/lzma_bcj_ia64.7z
Binary file not shown.
Binary file added tests/data/lzma_bcj_ppc.7z
Binary file not shown.
Binary file added tests/data/lzma_bcj_sparc.7z
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/test_7zfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ def test_copy(self):
# test loading of copy compressed files
self._test_archive('copy.7z')

def test_lzma_bcj(self):
# test loading of lzma compressed, filtered through bcj
self._test_archive('lzma_bcj.7z')

def test_lzma_bcj_arm(self):
# test loading of lzma compressed, filtered through bcj / ARM
self._test_archive('lzma_bcj_arm.7z')

def test_lzma_bcj_armt(self):
# test loading of lzma compressed, filtered through bcj / ARMT
self._test_archive('lzma_bcj_armt.7z')

def test_lzma_bcj_ia64(self):
# test loading of lzma compressed, filtered through bcj / IA64
self._test_archive('lzma_bcj_ia64.7z')

def test_lzma_bcj_ppc(self):
# test loading of lzma compressed, filtered through bcj / PPC
self._test_archive('lzma_bcj_ppc.7z')

def test_lzma_bcj_sparc(self):
# test loading of lzma compressed, filtered through bcj / SPARC
self._test_archive('lzma_bcj_sparc.7z')

def test_regress_1(self):
# prevent regression bug #1 reported by mail
fp = self._open_file(os.path.join(ROOT, 'data', 'regress_1.7z'), 'rb')
Expand Down

0 comments on commit dec3b88

Please sign in to comment.