Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: zipfile.py
"""
[0] Fix | Delete
Read and write ZIP files.
[1] Fix | Delete
"""
[2] Fix | Delete
import struct, os, time, sys, shutil
[3] Fix | Delete
import binascii, cStringIO, stat
[4] Fix | Delete
import io
[5] Fix | Delete
import re
[6] Fix | Delete
import string
[7] Fix | Delete
[8] Fix | Delete
try:
[9] Fix | Delete
import zlib # We may need its compression method
[10] Fix | Delete
crc32 = zlib.crc32
[11] Fix | Delete
except ImportError:
[12] Fix | Delete
zlib = None
[13] Fix | Delete
crc32 = binascii.crc32
[14] Fix | Delete
[15] Fix | Delete
__all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
[16] Fix | Delete
"ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
[17] Fix | Delete
[18] Fix | Delete
class BadZipfile(Exception):
[19] Fix | Delete
pass
[20] Fix | Delete
[21] Fix | Delete
[22] Fix | Delete
class LargeZipFile(Exception):
[23] Fix | Delete
"""
[24] Fix | Delete
Raised when writing a zipfile, the zipfile requires ZIP64 extensions
[25] Fix | Delete
and those extensions are disabled.
[26] Fix | Delete
"""
[27] Fix | Delete
[28] Fix | Delete
error = BadZipfile # The exception raised by this module
[29] Fix | Delete
[30] Fix | Delete
ZIP64_LIMIT = (1 << 31) - 1
[31] Fix | Delete
ZIP_FILECOUNT_LIMIT = (1 << 16) - 1
[32] Fix | Delete
ZIP_MAX_COMMENT = (1 << 16) - 1
[33] Fix | Delete
[34] Fix | Delete
# constants for Zip file compression methods
[35] Fix | Delete
ZIP_STORED = 0
[36] Fix | Delete
ZIP_DEFLATED = 8
[37] Fix | Delete
# Other ZIP compression methods not supported
[38] Fix | Delete
[39] Fix | Delete
# Below are some formats and associated data for reading/writing headers using
[40] Fix | Delete
# the struct module. The names and structures of headers/records are those used
[41] Fix | Delete
# in the PKWARE description of the ZIP file format:
[42] Fix | Delete
# http://www.pkware.com/documents/casestudies/APPNOTE.TXT
[43] Fix | Delete
# (URL valid as of January 2008)
[44] Fix | Delete
[45] Fix | Delete
# The "end of central directory" structure, magic number, size, and indices
[46] Fix | Delete
# (section V.I in the format document)
[47] Fix | Delete
structEndArchive = "<4s4H2LH"
[48] Fix | Delete
stringEndArchive = "PK\005\006"
[49] Fix | Delete
sizeEndCentDir = struct.calcsize(structEndArchive)
[50] Fix | Delete
[51] Fix | Delete
_ECD_SIGNATURE = 0
[52] Fix | Delete
_ECD_DISK_NUMBER = 1
[53] Fix | Delete
_ECD_DISK_START = 2
[54] Fix | Delete
_ECD_ENTRIES_THIS_DISK = 3
[55] Fix | Delete
_ECD_ENTRIES_TOTAL = 4
[56] Fix | Delete
_ECD_SIZE = 5
[57] Fix | Delete
_ECD_OFFSET = 6
[58] Fix | Delete
_ECD_COMMENT_SIZE = 7
[59] Fix | Delete
# These last two indices are not part of the structure as defined in the
[60] Fix | Delete
# spec, but they are used internally by this module as a convenience
[61] Fix | Delete
_ECD_COMMENT = 8
[62] Fix | Delete
_ECD_LOCATION = 9
[63] Fix | Delete
[64] Fix | Delete
# The "central directory" structure, magic number, size, and indices
[65] Fix | Delete
# of entries in the structure (section V.F in the format document)
[66] Fix | Delete
structCentralDir = "<4s4B4HL2L5H2L"
[67] Fix | Delete
stringCentralDir = "PK\001\002"
[68] Fix | Delete
sizeCentralDir = struct.calcsize(structCentralDir)
[69] Fix | Delete
[70] Fix | Delete
# indexes of entries in the central directory structure
[71] Fix | Delete
_CD_SIGNATURE = 0
[72] Fix | Delete
_CD_CREATE_VERSION = 1
[73] Fix | Delete
_CD_CREATE_SYSTEM = 2
[74] Fix | Delete
_CD_EXTRACT_VERSION = 3
[75] Fix | Delete
_CD_EXTRACT_SYSTEM = 4
[76] Fix | Delete
_CD_FLAG_BITS = 5
[77] Fix | Delete
_CD_COMPRESS_TYPE = 6
[78] Fix | Delete
_CD_TIME = 7
[79] Fix | Delete
_CD_DATE = 8
[80] Fix | Delete
_CD_CRC = 9
[81] Fix | Delete
_CD_COMPRESSED_SIZE = 10
[82] Fix | Delete
_CD_UNCOMPRESSED_SIZE = 11
[83] Fix | Delete
_CD_FILENAME_LENGTH = 12
[84] Fix | Delete
_CD_EXTRA_FIELD_LENGTH = 13
[85] Fix | Delete
_CD_COMMENT_LENGTH = 14
[86] Fix | Delete
_CD_DISK_NUMBER_START = 15
[87] Fix | Delete
_CD_INTERNAL_FILE_ATTRIBUTES = 16
[88] Fix | Delete
_CD_EXTERNAL_FILE_ATTRIBUTES = 17
[89] Fix | Delete
_CD_LOCAL_HEADER_OFFSET = 18
[90] Fix | Delete
[91] Fix | Delete
# The "local file header" structure, magic number, size, and indices
[92] Fix | Delete
# (section V.A in the format document)
[93] Fix | Delete
structFileHeader = "<4s2B4HL2L2H"
[94] Fix | Delete
stringFileHeader = "PK\003\004"
[95] Fix | Delete
sizeFileHeader = struct.calcsize(structFileHeader)
[96] Fix | Delete
[97] Fix | Delete
_FH_SIGNATURE = 0
[98] Fix | Delete
_FH_EXTRACT_VERSION = 1
[99] Fix | Delete
_FH_EXTRACT_SYSTEM = 2
[100] Fix | Delete
_FH_GENERAL_PURPOSE_FLAG_BITS = 3
[101] Fix | Delete
_FH_COMPRESSION_METHOD = 4
[102] Fix | Delete
_FH_LAST_MOD_TIME = 5
[103] Fix | Delete
_FH_LAST_MOD_DATE = 6
[104] Fix | Delete
_FH_CRC = 7
[105] Fix | Delete
_FH_COMPRESSED_SIZE = 8
[106] Fix | Delete
_FH_UNCOMPRESSED_SIZE = 9
[107] Fix | Delete
_FH_FILENAME_LENGTH = 10
[108] Fix | Delete
_FH_EXTRA_FIELD_LENGTH = 11
[109] Fix | Delete
[110] Fix | Delete
# The "Zip64 end of central directory locator" structure, magic number, and size
[111] Fix | Delete
structEndArchive64Locator = "<4sLQL"
[112] Fix | Delete
stringEndArchive64Locator = "PK\x06\x07"
[113] Fix | Delete
sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator)
[114] Fix | Delete
[115] Fix | Delete
# The "Zip64 end of central directory" record, magic number, size, and indices
[116] Fix | Delete
# (section V.G in the format document)
[117] Fix | Delete
structEndArchive64 = "<4sQ2H2L4Q"
[118] Fix | Delete
stringEndArchive64 = "PK\x06\x06"
[119] Fix | Delete
sizeEndCentDir64 = struct.calcsize(structEndArchive64)
[120] Fix | Delete
[121] Fix | Delete
_CD64_SIGNATURE = 0
[122] Fix | Delete
_CD64_DIRECTORY_RECSIZE = 1
[123] Fix | Delete
_CD64_CREATE_VERSION = 2
[124] Fix | Delete
_CD64_EXTRACT_VERSION = 3
[125] Fix | Delete
_CD64_DISK_NUMBER = 4
[126] Fix | Delete
_CD64_DISK_NUMBER_START = 5
[127] Fix | Delete
_CD64_NUMBER_ENTRIES_THIS_DISK = 6
[128] Fix | Delete
_CD64_NUMBER_ENTRIES_TOTAL = 7
[129] Fix | Delete
_CD64_DIRECTORY_SIZE = 8
[130] Fix | Delete
_CD64_OFFSET_START_CENTDIR = 9
[131] Fix | Delete
[132] Fix | Delete
_DD_SIGNATURE = 0x08074b50
[133] Fix | Delete
[134] Fix | Delete
_EXTRA_FIELD_STRUCT = struct.Struct('<HH')
[135] Fix | Delete
[136] Fix | Delete
def _strip_extra(extra, xids):
[137] Fix | Delete
# Remove Extra Fields with specified IDs.
[138] Fix | Delete
unpack = _EXTRA_FIELD_STRUCT.unpack
[139] Fix | Delete
modified = False
[140] Fix | Delete
buffer = []
[141] Fix | Delete
start = i = 0
[142] Fix | Delete
while i + 4 <= len(extra):
[143] Fix | Delete
xid, xlen = unpack(extra[i : i + 4])
[144] Fix | Delete
j = i + 4 + xlen
[145] Fix | Delete
if xid in xids:
[146] Fix | Delete
if i != start:
[147] Fix | Delete
buffer.append(extra[start : i])
[148] Fix | Delete
start = j
[149] Fix | Delete
modified = True
[150] Fix | Delete
i = j
[151] Fix | Delete
if not modified:
[152] Fix | Delete
return extra
[153] Fix | Delete
return b''.join(buffer)
[154] Fix | Delete
[155] Fix | Delete
def _check_zipfile(fp):
[156] Fix | Delete
try:
[157] Fix | Delete
if _EndRecData(fp):
[158] Fix | Delete
return True # file has correct magic number
[159] Fix | Delete
except IOError:
[160] Fix | Delete
pass
[161] Fix | Delete
return False
[162] Fix | Delete
[163] Fix | Delete
def is_zipfile(filename):
[164] Fix | Delete
"""Quickly see if a file is a ZIP file by checking the magic number.
[165] Fix | Delete
[166] Fix | Delete
The filename argument may be a file or file-like object too.
[167] Fix | Delete
"""
[168] Fix | Delete
result = False
[169] Fix | Delete
try:
[170] Fix | Delete
if hasattr(filename, "read"):
[171] Fix | Delete
result = _check_zipfile(fp=filename)
[172] Fix | Delete
else:
[173] Fix | Delete
with open(filename, "rb") as fp:
[174] Fix | Delete
result = _check_zipfile(fp)
[175] Fix | Delete
except IOError:
[176] Fix | Delete
pass
[177] Fix | Delete
return result
[178] Fix | Delete
[179] Fix | Delete
def _EndRecData64(fpin, offset, endrec):
[180] Fix | Delete
"""
[181] Fix | Delete
Read the ZIP64 end-of-archive records and use that to update endrec
[182] Fix | Delete
"""
[183] Fix | Delete
try:
[184] Fix | Delete
fpin.seek(offset - sizeEndCentDir64Locator, 2)
[185] Fix | Delete
except IOError:
[186] Fix | Delete
# If the seek fails, the file is not large enough to contain a ZIP64
[187] Fix | Delete
# end-of-archive record, so just return the end record we were given.
[188] Fix | Delete
return endrec
[189] Fix | Delete
[190] Fix | Delete
data = fpin.read(sizeEndCentDir64Locator)
[191] Fix | Delete
if len(data) != sizeEndCentDir64Locator:
[192] Fix | Delete
return endrec
[193] Fix | Delete
sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
[194] Fix | Delete
if sig != stringEndArchive64Locator:
[195] Fix | Delete
return endrec
[196] Fix | Delete
[197] Fix | Delete
if diskno != 0 or disks != 1:
[198] Fix | Delete
raise BadZipfile("zipfiles that span multiple disks are not supported")
[199] Fix | Delete
[200] Fix | Delete
# Assume no 'zip64 extensible data'
[201] Fix | Delete
fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
[202] Fix | Delete
data = fpin.read(sizeEndCentDir64)
[203] Fix | Delete
if len(data) != sizeEndCentDir64:
[204] Fix | Delete
return endrec
[205] Fix | Delete
sig, sz, create_version, read_version, disk_num, disk_dir, \
[206] Fix | Delete
dircount, dircount2, dirsize, diroffset = \
[207] Fix | Delete
struct.unpack(structEndArchive64, data)
[208] Fix | Delete
if sig != stringEndArchive64:
[209] Fix | Delete
return endrec
[210] Fix | Delete
[211] Fix | Delete
# Update the original endrec using data from the ZIP64 record
[212] Fix | Delete
endrec[_ECD_SIGNATURE] = sig
[213] Fix | Delete
endrec[_ECD_DISK_NUMBER] = disk_num
[214] Fix | Delete
endrec[_ECD_DISK_START] = disk_dir
[215] Fix | Delete
endrec[_ECD_ENTRIES_THIS_DISK] = dircount
[216] Fix | Delete
endrec[_ECD_ENTRIES_TOTAL] = dircount2
[217] Fix | Delete
endrec[_ECD_SIZE] = dirsize
[218] Fix | Delete
endrec[_ECD_OFFSET] = diroffset
[219] Fix | Delete
return endrec
[220] Fix | Delete
[221] Fix | Delete
[222] Fix | Delete
def _EndRecData(fpin):
[223] Fix | Delete
"""Return data from the "End of Central Directory" record, or None.
[224] Fix | Delete
[225] Fix | Delete
The data is a list of the nine items in the ZIP "End of central dir"
[226] Fix | Delete
record followed by a tenth item, the file seek offset of this record."""
[227] Fix | Delete
[228] Fix | Delete
# Determine file size
[229] Fix | Delete
fpin.seek(0, 2)
[230] Fix | Delete
filesize = fpin.tell()
[231] Fix | Delete
[232] Fix | Delete
# Check to see if this is ZIP file with no archive comment (the
[233] Fix | Delete
# "end of central directory" structure should be the last item in the
[234] Fix | Delete
# file if this is the case).
[235] Fix | Delete
try:
[236] Fix | Delete
fpin.seek(-sizeEndCentDir, 2)
[237] Fix | Delete
except IOError:
[238] Fix | Delete
return None
[239] Fix | Delete
data = fpin.read()
[240] Fix | Delete
if (len(data) == sizeEndCentDir and
[241] Fix | Delete
data[0:4] == stringEndArchive and
[242] Fix | Delete
data[-2:] == b"\000\000"):
[243] Fix | Delete
# the signature is correct and there's no comment, unpack structure
[244] Fix | Delete
endrec = struct.unpack(structEndArchive, data)
[245] Fix | Delete
endrec=list(endrec)
[246] Fix | Delete
[247] Fix | Delete
# Append a blank comment and record start offset
[248] Fix | Delete
endrec.append("")
[249] Fix | Delete
endrec.append(filesize - sizeEndCentDir)
[250] Fix | Delete
[251] Fix | Delete
# Try to read the "Zip64 end of central directory" structure
[252] Fix | Delete
return _EndRecData64(fpin, -sizeEndCentDir, endrec)
[253] Fix | Delete
[254] Fix | Delete
# Either this is not a ZIP file, or it is a ZIP file with an archive
[255] Fix | Delete
# comment. Search the end of the file for the "end of central directory"
[256] Fix | Delete
# record signature. The comment is the last item in the ZIP file and may be
[257] Fix | Delete
# up to 64K long. It is assumed that the "end of central directory" magic
[258] Fix | Delete
# number does not appear in the comment.
[259] Fix | Delete
maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0)
[260] Fix | Delete
fpin.seek(maxCommentStart, 0)
[261] Fix | Delete
data = fpin.read()
[262] Fix | Delete
start = data.rfind(stringEndArchive)
[263] Fix | Delete
if start >= 0:
[264] Fix | Delete
# found the magic number; attempt to unpack and interpret
[265] Fix | Delete
recData = data[start:start+sizeEndCentDir]
[266] Fix | Delete
if len(recData) != sizeEndCentDir:
[267] Fix | Delete
# Zip file is corrupted.
[268] Fix | Delete
return None
[269] Fix | Delete
endrec = list(struct.unpack(structEndArchive, recData))
[270] Fix | Delete
commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
[271] Fix | Delete
comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
[272] Fix | Delete
endrec.append(comment)
[273] Fix | Delete
endrec.append(maxCommentStart + start)
[274] Fix | Delete
[275] Fix | Delete
# Try to read the "Zip64 end of central directory" structure
[276] Fix | Delete
return _EndRecData64(fpin, maxCommentStart + start - filesize,
[277] Fix | Delete
endrec)
[278] Fix | Delete
[279] Fix | Delete
# Unable to find a valid end of central directory structure
[280] Fix | Delete
return None
[281] Fix | Delete
[282] Fix | Delete
[283] Fix | Delete
class ZipInfo (object):
[284] Fix | Delete
"""Class with attributes describing each file in the ZIP archive."""
[285] Fix | Delete
[286] Fix | Delete
__slots__ = (
[287] Fix | Delete
'orig_filename',
[288] Fix | Delete
'filename',
[289] Fix | Delete
'date_time',
[290] Fix | Delete
'compress_type',
[291] Fix | Delete
'comment',
[292] Fix | Delete
'extra',
[293] Fix | Delete
'create_system',
[294] Fix | Delete
'create_version',
[295] Fix | Delete
'extract_version',
[296] Fix | Delete
'reserved',
[297] Fix | Delete
'flag_bits',
[298] Fix | Delete
'volume',
[299] Fix | Delete
'internal_attr',
[300] Fix | Delete
'external_attr',
[301] Fix | Delete
'header_offset',
[302] Fix | Delete
'CRC',
[303] Fix | Delete
'compress_size',
[304] Fix | Delete
'file_size',
[305] Fix | Delete
'_raw_time',
[306] Fix | Delete
)
[307] Fix | Delete
[308] Fix | Delete
def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
[309] Fix | Delete
self.orig_filename = filename # Original file name in archive
[310] Fix | Delete
[311] Fix | Delete
# Terminate the file name at the first null byte. Null bytes in file
[312] Fix | Delete
# names are used as tricks by viruses in archives.
[313] Fix | Delete
null_byte = filename.find(chr(0))
[314] Fix | Delete
if null_byte >= 0:
[315] Fix | Delete
filename = filename[0:null_byte]
[316] Fix | Delete
# This is used to ensure paths in generated ZIP files always use
[317] Fix | Delete
# forward slashes as the directory separator, as required by the
[318] Fix | Delete
# ZIP format specification.
[319] Fix | Delete
if os.sep != "/" and os.sep in filename:
[320] Fix | Delete
filename = filename.replace(os.sep, "/")
[321] Fix | Delete
[322] Fix | Delete
self.filename = filename # Normalized file name
[323] Fix | Delete
self.date_time = date_time # year, month, day, hour, min, sec
[324] Fix | Delete
[325] Fix | Delete
if date_time[0] < 1980:
[326] Fix | Delete
raise ValueError('ZIP does not support timestamps before 1980')
[327] Fix | Delete
[328] Fix | Delete
# Standard values:
[329] Fix | Delete
self.compress_type = ZIP_STORED # Type of compression for the file
[330] Fix | Delete
self.comment = "" # Comment for each file
[331] Fix | Delete
self.extra = "" # ZIP extra data
[332] Fix | Delete
if sys.platform == 'win32':
[333] Fix | Delete
self.create_system = 0 # System which created ZIP archive
[334] Fix | Delete
else:
[335] Fix | Delete
# Assume everything else is unix-y
[336] Fix | Delete
self.create_system = 3 # System which created ZIP archive
[337] Fix | Delete
self.create_version = 20 # Version which created ZIP archive
[338] Fix | Delete
self.extract_version = 20 # Version needed to extract archive
[339] Fix | Delete
self.reserved = 0 # Must be zero
[340] Fix | Delete
self.flag_bits = 0 # ZIP flag bits
[341] Fix | Delete
self.volume = 0 # Volume number of file header
[342] Fix | Delete
self.internal_attr = 0 # Internal attributes
[343] Fix | Delete
self.external_attr = 0 # External file attributes
[344] Fix | Delete
# Other attributes are set by class ZipFile:
[345] Fix | Delete
# header_offset Byte offset to the file header
[346] Fix | Delete
# CRC CRC-32 of the uncompressed file
[347] Fix | Delete
# compress_size Size of the compressed file
[348] Fix | Delete
# file_size Size of the uncompressed file
[349] Fix | Delete
[350] Fix | Delete
def FileHeader(self, zip64=None):
[351] Fix | Delete
"""Return the per-file header as a string."""
[352] Fix | Delete
dt = self.date_time
[353] Fix | Delete
dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
[354] Fix | Delete
dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
[355] Fix | Delete
if self.flag_bits & 0x08:
[356] Fix | Delete
# Set these to zero because we write them after the file data
[357] Fix | Delete
CRC = compress_size = file_size = 0
[358] Fix | Delete
else:
[359] Fix | Delete
CRC = self.CRC
[360] Fix | Delete
compress_size = self.compress_size
[361] Fix | Delete
file_size = self.file_size
[362] Fix | Delete
[363] Fix | Delete
extra = self.extra
[364] Fix | Delete
[365] Fix | Delete
if zip64 is None:
[366] Fix | Delete
zip64 = file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT
[367] Fix | Delete
if zip64:
[368] Fix | Delete
fmt = '<HHQQ'
[369] Fix | Delete
extra = extra + struct.pack(fmt,
[370] Fix | Delete
1, struct.calcsize(fmt)-4, file_size, compress_size)
[371] Fix | Delete
if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:
[372] Fix | Delete
if not zip64:
[373] Fix | Delete
raise LargeZipFile("Filesize would require ZIP64 extensions")
[374] Fix | Delete
# File is larger than what fits into a 4 byte integer,
[375] Fix | Delete
# fall back to the ZIP64 extension
[376] Fix | Delete
file_size = 0xffffffff
[377] Fix | Delete
compress_size = 0xffffffff
[378] Fix | Delete
self.extract_version = max(45, self.extract_version)
[379] Fix | Delete
self.create_version = max(45, self.extract_version)
[380] Fix | Delete
[381] Fix | Delete
filename, flag_bits = self._encodeFilenameFlags()
[382] Fix | Delete
header = struct.pack(structFileHeader, stringFileHeader,
[383] Fix | Delete
self.extract_version, self.reserved, flag_bits,
[384] Fix | Delete
self.compress_type, dostime, dosdate, CRC,
[385] Fix | Delete
compress_size, file_size,
[386] Fix | Delete
len(filename), len(extra))
[387] Fix | Delete
return header + filename + extra
[388] Fix | Delete
[389] Fix | Delete
def _encodeFilenameFlags(self):
[390] Fix | Delete
if isinstance(self.filename, unicode):
[391] Fix | Delete
try:
[392] Fix | Delete
return self.filename.encode('ascii'), self.flag_bits
[393] Fix | Delete
except UnicodeEncodeError:
[394] Fix | Delete
return self.filename.encode('utf-8'), self.flag_bits | 0x800
[395] Fix | Delete
else:
[396] Fix | Delete
return self.filename, self.flag_bits
[397] Fix | Delete
[398] Fix | Delete
def _decodeFilename(self):
[399] Fix | Delete
if self.flag_bits & 0x800:
[400] Fix | Delete
return self.filename.decode('utf-8')
[401] Fix | Delete
else:
[402] Fix | Delete
return self.filename
[403] Fix | Delete
[404] Fix | Delete
def _decodeExtra(self):
[405] Fix | Delete
# Try to decode the extra field.
[406] Fix | Delete
extra = self.extra
[407] Fix | Delete
unpack = struct.unpack
[408] Fix | Delete
while len(extra) >= 4:
[409] Fix | Delete
tp, ln = unpack('<HH', extra[:4])
[410] Fix | Delete
if tp == 1:
[411] Fix | Delete
if ln >= 24:
[412] Fix | Delete
counts = unpack('<QQQ', extra[4:28])
[413] Fix | Delete
elif ln == 16:
[414] Fix | Delete
counts = unpack('<QQ', extra[4:20])
[415] Fix | Delete
elif ln == 8:
[416] Fix | Delete
counts = unpack('<Q', extra[4:12])
[417] Fix | Delete
elif ln == 0:
[418] Fix | Delete
counts = ()
[419] Fix | Delete
else:
[420] Fix | Delete
raise RuntimeError, "Corrupt extra field %s"%(ln,)
[421] Fix | Delete
[422] Fix | Delete
idx = 0
[423] Fix | Delete
[424] Fix | Delete
# ZIP64 extension (large files and/or large archives)
[425] Fix | Delete
if self.file_size in (0xffffffffffffffffL, 0xffffffffL):
[426] Fix | Delete
self.file_size = counts[idx]
[427] Fix | Delete
idx += 1
[428] Fix | Delete
[429] Fix | Delete
if self.compress_size == 0xFFFFFFFFL:
[430] Fix | Delete
self.compress_size = counts[idx]
[431] Fix | Delete
idx += 1
[432] Fix | Delete
[433] Fix | Delete
if self.header_offset == 0xffffffffL:
[434] Fix | Delete
old = self.header_offset
[435] Fix | Delete
self.header_offset = counts[idx]
[436] Fix | Delete
idx+=1
[437] Fix | Delete
[438] Fix | Delete
extra = extra[ln+4:]
[439] Fix | Delete
[440] Fix | Delete
[441] Fix | Delete
class _ZipDecrypter:
[442] Fix | Delete
"""Class to handle decryption of files stored within a ZIP archive.
[443] Fix | Delete
[444] Fix | Delete
ZIP supports a password-based form of encryption. Even though known
[445] Fix | Delete
plaintext attacks have been found against it, it is still useful
[446] Fix | Delete
to be able to get data out of such a file.
[447] Fix | Delete
[448] Fix | Delete
Usage:
[449] Fix | Delete
zd = _ZipDecrypter(mypwd)
[450] Fix | Delete
plain_char = zd(cypher_char)
[451] Fix | Delete
plain_text = map(zd, cypher_text)
[452] Fix | Delete
"""
[453] Fix | Delete
[454] Fix | Delete
def _GenerateCRCTable():
[455] Fix | Delete
"""Generate a CRC-32 table.
[456] Fix | Delete
[457] Fix | Delete
ZIP encryption uses the CRC32 one-byte primitive for scrambling some
[458] Fix | Delete
internal keys. We noticed that a direct implementation is faster than
[459] Fix | Delete
relying on binascii.crc32().
[460] Fix | Delete
"""
[461] Fix | Delete
poly = 0xedb88320
[462] Fix | Delete
table = [0] * 256
[463] Fix | Delete
for i in range(256):
[464] Fix | Delete
crc = i
[465] Fix | Delete
for j in range(8):
[466] Fix | Delete
if crc & 1:
[467] Fix | Delete
crc = ((crc >> 1) & 0x7FFFFFFF) ^ poly
[468] Fix | Delete
else:
[469] Fix | Delete
crc = ((crc >> 1) & 0x7FFFFFFF)
[470] Fix | Delete
table[i] = crc
[471] Fix | Delete
return table
[472] Fix | Delete
crctable = _GenerateCRCTable()
[473] Fix | Delete
[474] Fix | Delete
def _crc32(self, ch, crc):
[475] Fix | Delete
"""Compute the CRC32 primitive on one byte."""
[476] Fix | Delete
return ((crc >> 8) & 0xffffff) ^ self.crctable[(crc ^ ord(ch)) & 0xff]
[477] Fix | Delete
[478] Fix | Delete
def __init__(self, pwd):
[479] Fix | Delete
self.key0 = 305419896
[480] Fix | Delete
self.key1 = 591751049
[481] Fix | Delete
self.key2 = 878082192
[482] Fix | Delete
for p in pwd:
[483] Fix | Delete
self._UpdateKeys(p)
[484] Fix | Delete
[485] Fix | Delete
def _UpdateKeys(self, c):
[486] Fix | Delete
self.key0 = self._crc32(c, self.key0)
[487] Fix | Delete
self.key1 = (self.key1 + (self.key0 & 255)) & 4294967295
[488] Fix | Delete
self.key1 = (self.key1 * 134775813 + 1) & 4294967295
[489] Fix | Delete
self.key2 = self._crc32(chr((self.key1 >> 24) & 255), self.key2)
[490] Fix | Delete
[491] Fix | Delete
def __call__(self, c):
[492] Fix | Delete
"""Decrypt a single character."""
[493] Fix | Delete
c = ord(c)
[494] Fix | Delete
k = self.key2 | 2
[495] Fix | Delete
c = c ^ (((k * (k^1)) >> 8) & 255)
[496] Fix | Delete
c = chr(c)
[497] Fix | Delete
self._UpdateKeys(c)
[498] Fix | Delete
return c
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function