1# 2# p7zr library 3# 4# Copyright (c) 2019 Hiroshi Miura <miurahr@linux.com> 5# Copyright (c) 2004-2015 by Joachim Bauch, mail@joachim-bauch.de 6# 7-Zip Copyright (C) 1999-2010 Igor Pavlov 7# LZMA SDK Copyright (C) 1999-2010 Igor Pavlov 8# 9# This library is free software; you can redistribute it and/or 10# modify it under the terms of the GNU Lesser General Public 11# License as published by the Free Software Foundation; either 12# version 2.1 of the License, or (at your option) any later version. 13# 14# This library is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17# Lesser General Public License for more details. 18# 19# You should have received a copy of the GNU Lesser General Public 20# License along with this library; if not, write to the Free Software 21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22# 23 24import binascii 25from enum import Enum 26from typing import Optional 27 28MAGIC_7Z = binascii.unhexlify('377abcaf271c') 29FINISH_7Z = binascii.unhexlify('377abcaf271d') 30READ_BLOCKSIZE = 32248 31QUEUELEN = READ_BLOCKSIZE * 2 32 33READ_BLOCKSIZE = 32248 34 35 36class ByteEnum(bytes, Enum): 37 pass 38 39 40class Property(ByteEnum): 41 """Hold 7zip property fixed values.""" 42 END = binascii.unhexlify('00') 43 HEADER = binascii.unhexlify('01') 44 ARCHIVE_PROPERTIES = binascii.unhexlify('02') 45 ADDITIONAL_STREAMS_INFO = binascii.unhexlify('03') 46 MAIN_STREAMS_INFO = binascii.unhexlify('04') 47 FILES_INFO = binascii.unhexlify('05') 48 PACK_INFO = binascii.unhexlify('06') 49 UNPACK_INFO = binascii.unhexlify('07') 50 SUBSTREAMS_INFO = binascii.unhexlify('08') 51 SIZE = binascii.unhexlify('09') 52 CRC = binascii.unhexlify('0a') 53 FOLDER = binascii.unhexlify('0b') 54 CODERS_UNPACK_SIZE = binascii.unhexlify('0c') 55 NUM_UNPACK_STREAM = binascii.unhexlify('0d') 56 EMPTY_STREAM = binascii.unhexlify('0e') 57 EMPTY_FILE = binascii.unhexlify('0f') 58 ANTI = binascii.unhexlify('10') 59 NAME = binascii.unhexlify('11') 60 CREATION_TIME = binascii.unhexlify('12') 61 LAST_ACCESS_TIME = binascii.unhexlify('13') 62 LAST_WRITE_TIME = binascii.unhexlify('14') 63 ATTRIBUTES = binascii.unhexlify('15') 64 COMMENT = binascii.unhexlify('16') 65 ENCODED_HEADER = binascii.unhexlify('17') 66 START_POS = binascii.unhexlify('18') 67 DUMMY = binascii.unhexlify('19') 68 69 70class CompressionMethod(ByteEnum): 71 """Hold fixed values for method parameter.""" 72 COPY = binascii.unhexlify('00') 73 DELTA = binascii.unhexlify('03') 74 BCJ = binascii.unhexlify('04') 75 PPC = binascii.unhexlify('05') 76 IA64 = binascii.unhexlify('06') 77 ARM = binascii.unhexlify('07') 78 ARMT = binascii.unhexlify('08') 79 SPARC = binascii.unhexlify('09') 80 # SWAP = 02.. 81 SWAP2 = binascii.unhexlify('020302') 82 SWAP4 = binascii.unhexlify('020304') 83 # 7Z = 03.. 84 LZMA = binascii.unhexlify('030101') 85 PPMD = binascii.unhexlify('030401') 86 P7Z_BCJ = binascii.unhexlify('03030103') 87 P7Z_BCJ2 = binascii.unhexlify('0303011B') 88 BCJ_PPC = binascii.unhexlify('03030205') 89 BCJ_IA64 = binascii.unhexlify('03030401') 90 BCJ_ARM = binascii.unhexlify('03030501') 91 BCJ_ARMT = binascii.unhexlify('03030701') 92 BCJ_SPARC = binascii.unhexlify('03030805') 93 LZMA2 = binascii.unhexlify('21') 94 # MISC : 04.. 95 MISC_ZIP = binascii.unhexlify('0401') 96 MISC_BZIP2 = binascii.unhexlify('040202') 97 MISC_DEFLATE = binascii.unhexlify('040108') 98 MISC_DEFLATE64 = binascii.unhexlify('040109') 99 MISC_Z = binascii.unhexlify('0405') 100 MISC_LZH = binascii.unhexlify('0406') 101 NSIS_DEFLATE = binascii.unhexlify('040901') 102 NSIS_BZIP2 = binascii.unhexlify('040902') 103 # 104 MISC_ZSTD = binascii.unhexlify('04f71101') 105 MISC_BROTLI = binascii.unhexlify('04f71102') 106 MISC_LZ4 = binascii.unhexlify('04f71104') 107 MISC_LZS = binascii.unhexlify('04f71105') 108 MISC_LIZARD = binascii.unhexlify('04f71106') 109 # CRYPTO 06.. 110 CRYPT_ZIPCRYPT = binascii.unhexlify('06f10101') 111 CRYPT_RAR29AES = binascii.unhexlify('06f10303') 112 CRYPT_AES256_SHA256 = binascii.unhexlify('06f10701') 113 114 115class SupportedMethods: 116 """Hold list of methods which python3 can support.""" 117 formats = [{'name': "7z", 'magic': MAGIC_7Z}] 118 codecs = [{'id': CompressionMethod.LZMA, 'name': "LZMA"}, 119 {'id': CompressionMethod.LZMA2, 'name': "LZMA2"}, 120 {'id': CompressionMethod.DELTA, 'name': "DELTA"}, 121 {'id': CompressionMethod.P7Z_BCJ, 'name': "BCJ"}, 122 {'id': CompressionMethod.BCJ_PPC, 'name': 'PPC'}, 123 {'id': CompressionMethod.BCJ_IA64, 'name': 'IA64'}, 124 {'id': CompressionMethod.BCJ_ARM, 'name': "ARM"}, 125 {'id': CompressionMethod.BCJ_ARMT, 'name': "ARMT"}, 126 {'id': CompressionMethod.BCJ_SPARC, 'name': 'SPARC'} 127 ] 128 129 130# this class is Borg/Singleton 131class ArchivePassword: 132 133 _shared_state = { 134 '_password': None, 135 } 136 137 def __init__(self, password: Optional[str] = None): 138 self.__dict__ = self._shared_state 139 if password is not None: 140 self._password = password 141 142 def set(self, password): 143 self._password = password 144 145 def get(self): 146 if self._password is not None: 147 return self._password 148 else: 149 return '' 150 151 def __str__(self): 152 if self._password is not None: 153 return self._password 154 else: 155 return '' 156