-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
compression.py
493 lines (419 loc) · 17.6 KB
/
compression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/python -u
#
# p7zr library
#
# Copyright (c) 2019 Hiroshi Miura <miurahr@linux.com>
# Copyright (c) 2004-2015 by Joachim Bauch, mail@joachim-bauch.de
# 7-Zip Copyright (C) 1999-2010 Igor Pavlov
# LZMA SDK Copyright (C) 1999-2010 Igor Pavlov
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import bz2
import concurrent.futures
import hashlib
import io
import lzma
import sys
from io import BytesIO
from typing import Any, BinaryIO, Dict, List, Optional, Union
from Crypto.Cipher import AES
from py7zr import UnsupportedCompressionMethodError
from py7zr.helpers import calculate_crc32
from py7zr.properties import ArchivePassword, CompressionMethod, Configuration
if sys.version_info < (3, 6):
import pathlib2 as pathlib
else:
import pathlib
class NullHandler():
'''Null handler pass to null the data.'''
def __init__(self):
pass
def open(self, mode=None):
pass
def write(self, t):
pass
def read(self, size):
return b''
def seek(self, offset, whence=1):
pass
def truncate(self, size):
pass
def close(self):
pass
def stat(self):
return None
class BufferHandler():
'''Buffer handler handles BytesIO/StringIO buffers.'''
def __init__(self, target: BytesIO) -> None:
self.buf = target
self.target = "memory buffer"
def open(self, mode=None) -> None:
pass
def write(self, data: bytes) -> None:
self.buf.write(data)
def read(self, size=None):
if size is not None:
return self.buf.read(size)
else:
return self.buf.read()
def seek(self, offset, whence=1):
self.buf.seek(offset, whence)
def truncate(self, size):
pass
def close(self) -> None:
pass
def stat(self):
return None
class FileHandler():
'''File handler treat fileish object'''
def __init__(self, target: pathlib.Path) -> None:
self.target = target
def open(self, mode='wb') -> None:
self.fp = self.target.open(mode=mode)
def write(self, data: bytes) -> None:
self.fp.write(data)
def read(self, size=None):
if size is not None:
return self.fp.read(size)
else:
return self.fp.read()
def seek(self, offset, whence=1):
self.fp.seek(offset, whence)
def truncate(self, size=None):
self.fp.truncate(size)
def close(self) -> None:
self.fp.close()
def stat(self):
return self.target.stat()
Handler = Union[NullHandler, BufferHandler, FileHandler]
class AESDecompressor:
lzma_methods_map = {
CompressionMethod.LZMA: lzma.FILTER_LZMA1,
CompressionMethod.LZMA2: lzma.FILTER_LZMA2,
CompressionMethod.DELTA: lzma.FILTER_DELTA,
CompressionMethod.P7Z_BCJ: lzma.FILTER_X86,
CompressionMethod.BCJ_ARM: lzma.FILTER_ARM,
CompressionMethod.BCJ_ARMT: lzma.FILTER_ARMTHUMB,
CompressionMethod.BCJ_IA64: lzma.FILTER_IA64,
CompressionMethod.BCJ_PPC: lzma.FILTER_POWERPC,
CompressionMethod.BCJ_SPARC: lzma.FILTER_SPARC,
}
def __init__(self, aes_properties: bytes, password: str, coders: List[Dict[str, Any]]) -> None:
byte_password = password.encode('utf-16LE')
firstbyte = aes_properties[0]
numcyclespower = firstbyte & 0x3f
if firstbyte & 0xc0 != 0:
saltsize = (firstbyte >> 7) & 1
ivsize = (firstbyte >> 6) & 1
secondbyte = aes_properties[1]
saltsize += (secondbyte >> 4)
ivsize += (secondbyte & 0x0f)
assert len(aes_properties) == 2 + saltsize + ivsize
salt = aes_properties[2:2 + saltsize]
iv = aes_properties[2 + saltsize:2 + saltsize + ivsize]
assert len(salt) == saltsize
assert len(iv) == ivsize
assert numcyclespower <= 24
if ivsize < 16:
iv += bytes('\x00' * (16 - ivsize), 'ascii')
key = self._calculate_key(byte_password, numcyclespower, salt, 'sha256')
self.lzma_decompressor = self._set_lzma_decompressor(coders)
self.cipher = AES.new(key, AES.MODE_CBC, iv)
else:
raise UnsupportedCompressionMethodError
# set pipeline decompressor
def _set_lzma_decompressor(self, coders: List[Dict[str, Any]]):
filters = [] # type: List[Dict[str, Any]]
for coder in coders:
filter = self.lzma_methods_map.get(coder['method'], None)
if filter is not None:
properties = coder.get('properties', None)
if properties is not None:
filters[:0] = [lzma._decode_filter_properties(filter, properties)] # type: ignore
else:
filters[:0] = [{'id': filter}]
else:
raise UnsupportedCompressionMethodError
return lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters)
@staticmethod
def _calculate_key(password: bytes, cycles: int, salt: bytes, digest: str) -> bytes:
assert digest == 'sha256'
if cycles == 0x3f:
ba = bytearray()
ba.extend(salt)
ba.extend(password)
for i in range(32):
ba.append(0)
key = ba[:32] # type: bytes
else:
rounds = 1 << cycles
m = hashlib.sha256()
for round in range(rounds):
m.update(salt)
m.update(password)
m.update(round.to_bytes(8, byteorder='little', signed=False))
key = m.digest()[:32]
return key
@property
def needs_input(self) -> bool:
return self.lzma_decompressor.needs_input
@property
def eof(self) -> bool:
return self.lzma_decompressor.eof
def decompress(self, data: bytes, max_length: Optional[int] = None) -> bytes:
temp = self.cipher.decrypt(data)
return self.lzma_decompressor.decompress(temp, max_length)
@property
def unused_data(self):
return self.unused_data
class Worker:
"""Extract worker class to invoke handler"""
def __init__(self, files, src_start: int, header) -> None:
self.target_filepath = {} # type: Dict[int, Handler]
self.files = files
self.src_start = src_start
self.header = header
def set_output_filepath(self, index: int, func: Handler) -> None:
self.target_filepath[index] = func
def extract(self, fp: BinaryIO, multithread: bool = False) -> None:
"""Extract worker method to handle 7zip folder and decompress each files."""
if multithread:
numfolders = self.header.main_streams.unpackinfo.numfolders
positions = self.header.main_streams.packinfo.packpositions
folders = self.header.main_streams.unpackinfo.folders
filename = getattr(fp, 'name', None)
empty_files = [f for f in self.files if f.emptystream]
with concurrent.futures.ThreadPoolExecutor() as executor:
threads = []
threads.append(executor.submit(self.extract_single, open(filename, 'rb'),
empty_files, 0))
for i in range(numfolders):
threads.append(executor.submit(self.extract_single, open(filename, 'rb'),
folders[i].files, self.src_start + positions[i]))
for future in concurrent.futures.as_completed(threads):
try:
future.result()
except Exception as e:
raise e
else:
self.extract_single(fp, self.files, self.src_start)
def extract_single(self, fp: BinaryIO, files, src_start: int) -> None:
"""Single thread extractor that takes file lists in single 7zip folder."""
fp.seek(src_start)
for f in files:
fileish = self.target_filepath.get(f.id, NullHandler()) # type: Handler
fileish.open()
# Skip empty file read
if f.emptystream:
fileish.write(b'')
else:
self.decompress(fp, f.folder, fileish, f.uncompressed[-1], f.compressed)
fileish.close()
def decompress(self, fp: BinaryIO, folder, fileish: Handler,
size: int, compressed_size: Optional[int]) -> None:
"""decompressor wrapper called from extract method."""
assert folder is not None
out_remaining = size
decompressor = folder.get_decompressor(compressed_size)
while out_remaining > 0:
if not decompressor.eof:
max_length = min(out_remaining, io.DEFAULT_BUFFER_SIZE)
if decompressor.needs_input:
read_size = min(Configuration.get('read_blocksize'), decompressor.remaining_size)
inp = fp.read(read_size)
tmp = decompressor.decompress(inp, max_length)
if len(tmp) == 0:
# FIXME: there is a bug in python core?
break
else:
tmp = decompressor.decompress(b'', max_length)
if out_remaining >= len(tmp):
out_remaining -= len(tmp)
fileish.write(tmp)
if out_remaining <= 0:
break
else:
break
assert out_remaining == 0
if decompressor.eof:
if decompressor.crc is not None and not decompressor.check_crc():
print('\nCRC error! expected: {}, real: {}'.format(decompressor.crc, decompressor.digest))
return
def archive(self, fp: BinaryIO, folder):
"""Run archive task for specified 7zip folder."""
fp.seek(self.src_start)
for f in self.files:
if not f['emptystream']:
target = self.target_filepath.get(f.id, NullHandler()) # type: Handler
target.open()
length = self.compress(fp, folder, target)
target.close()
f['compressed'] = length
self.files.append(f)
fp.flush()
def compress(self, fp: BinaryIO, folder, f: Handler):
"""Compress specified file-ish into folder where fp placed."""
compressor = folder.get_compressor()
length = 0
for indata in f.read(Configuration.get('read_blocksize')):
arcdata = compressor.compress(indata)
folder.crc = calculate_crc32(arcdata, folder.crc)
length += len(arcdata)
fp.write(arcdata)
arcdata = compressor.flush()
folder.crc = calculate_crc32(arcdata, folder.crc)
length += len(arcdata)
fp.write(arcdata)
return length
def register_filelike(self, id: int, fileish: Union[pathlib.Path, BinaryIO, None]) -> None:
"""register file-ish to worker. File-ish can be union of BinaryIO, str and None.
When BytesIO specified use BufferHandler. When None use NullHandler, and
and str is recognized as a path."""
if fileish is None:
self.set_output_filepath(id, NullHandler())
elif isinstance(fileish, io.BytesIO):
self.set_output_filepath(id, BufferHandler(fileish))
elif isinstance(fileish, pathlib.Path):
self.set_output_filepath(id, FileHandler(fileish))
else:
raise
class SevenZipDecompressor:
"""Main decompressor object which is properly configured and bind to each 7zip folder.
because 7zip folder can have a custom compression method"""
lzma_methods_map = {
CompressionMethod.LZMA: lzma.FILTER_LZMA1,
CompressionMethod.LZMA2: lzma.FILTER_LZMA2,
CompressionMethod.DELTA: lzma.FILTER_DELTA,
CompressionMethod.P7Z_BCJ: lzma.FILTER_X86,
CompressionMethod.BCJ_ARM: lzma.FILTER_ARM,
CompressionMethod.BCJ_ARMT: lzma.FILTER_ARMTHUMB,
CompressionMethod.BCJ_IA64: lzma.FILTER_IA64,
CompressionMethod.BCJ_PPC: lzma.FILTER_POWERPC,
CompressionMethod.BCJ_SPARC: lzma.FILTER_SPARC,
}
FILTER_BZIP2 = 0x31
FILTER_ZIP = 0x32
FILTER_COPY = 0x33
alt_methods_map = {
CompressionMethod.MISC_BZIP2: FILTER_BZIP2,
CompressionMethod.COPY: FILTER_COPY,
}
FILTER_AES = 0
enc_methods_map = {
CompressionMethod.CRYPT_AES256_SHA256: FILTER_AES,
}
def __init__(self, coders: List[Dict[str, Any]], size: int, crc: Optional[int]) -> None:
# Get password which was set when creation of py7zr.SevenZipFile object.
self.input_size = size
self.consumed = 0 # type: int
self.crc = crc
self.digest = None # type: Optional[int]
filters = [] # type: List[Dict[str, Any]]
try:
for coder in coders:
if coder['numinstreams'] != 1 or coder['numoutstreams'] != 1:
raise UnsupportedCompressionMethodError('Only a simple compression method is currently supported.')
filter = self.lzma_methods_map.get(coder['method'], None)
if filter is not None:
properties = coder.get('properties', None)
if properties is not None:
filters[:0] = [lzma._decode_filter_properties(filter, properties)] # type: ignore
else:
filters[:0] = [{'id': filter}]
else:
raise UnsupportedCompressionMethodError
except UnsupportedCompressionMethodError as e:
filter = self.alt_methods_map.get(coders[0]['method'], None)
if len(coders) == 1 and filter is not None:
if filter == self.FILTER_BZIP2:
self.decompressor = bz2.BZ2Decompressor() # type: Union[bz2.BZ2Decompressor, lzma.LZMADecompressor, AESDecompressor] # noqa
elif filter == self.FILTER_COPY:
# FIXME
raise e
else:
raise e
self.can_partial_decompress = False
filter = self.enc_methods_map.get(coders[0]['method'], None)
if filter == self.FILTER_AES:
password = ArchivePassword().get()
properties = coders[0].get('properties', None)
self.decompressor = AESDecompressor(properties, password, coders[1:])
else:
raise e
else:
self.decompressor = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters)
self.can_partial_decompress = True
self.filters = filters
@property
def needs_input(self) -> bool:
return self.decompressor.needs_input
@property
def eof(self) -> bool:
return self.decompressor.eof
def decompress(self, data: bytes, max_length: Optional[int] = None) -> bytes:
self.consumed += len(data)
if max_length is not None:
folder_data = self.decompressor.decompress(data, max_length=max_length)
else:
folder_data = self.decompressor.decompress(data)
# calculate CRC with uncompressed data
if self.crc is not None:
self.digest = calculate_crc32(folder_data, self.digest)
return folder_data
@property
def unused_data(self):
return self.decompressor.unused_data
@property
def remaining_size(self) -> int:
return self.input_size - self.consumed
def check_crc(self):
return self.crc == self.digest
class SevenZipCompressor():
"""Main compressor object to configured for each 7zip folder."""
__slots__ = ['filters', 'compressor', 'coders']
lzma_methods_map_r = {
lzma.FILTER_LZMA2: CompressionMethod.LZMA2,
lzma.FILTER_DELTA: CompressionMethod.DELTA,
lzma.FILTER_X86: CompressionMethod.P7Z_BCJ,
}
def __init__(self, filters=None):
if filters is None:
self.filters = [{"id": lzma.FILTER_LZMA2, "preset": 7 | lzma.PRESET_EXTREME}, ]
else:
self.filters = filters
self.compressor = lzma.LZMACompressor(format=lzma.FORMAT_RAW, filters=self.filters)
self.coders = []
for filter in self.filters:
if filter is None:
break
method = self.lzma_methods_map_r[filter['id']]
properties = lzma._encode_filter_properties(filter)
self.coders.append({'method': method, 'properties': properties, 'numinstreams': 1, 'numoutstreams': 1})
def compress(self, data):
return self.compressor.compress(data)
def flush(self):
return self.compressor.flush()
def get_methods_names(coders: List[dict]) -> List[str]:
"""Return human readable method names for specified coders"""
methods_name_map = {
CompressionMethod.LZMA2: "LZMA2",
CompressionMethod.LZMA: "LZMA",
CompressionMethod.DELTA: "delta",
}
methods_names = [] # type: List[str]
for coder in coders:
methods_names.append(methods_name_map[coder['method']])
return methods_names