Add files via upload
[PyCatFile.git] / pycatfile.py
blobe589bb030688e16663a9f91b1a4fd791124fb796
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 '''
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: pycatfile.py - Last Update: 4/12/2024 Ver. 0.7.8 RC 1 - Author: cooldude2k $
18 '''
20 from __future__ import absolute_import, division, print_function, unicode_literals;
21 import io, os, re, sys, time, stat, zlib, base64, shutil, socket, hashlib, datetime, logging, binascii, tempfile, zipfile, platform;
22 from ftplib import FTP, FTP_TLS;
23 if(sys.version[0]=="2"):
24 from urlparse import urlparse, urlunparse;
25 elif(sys.version[0]>="3"):
26 from urllib.parse import urlunparse;
27 from urllib.parse import urlparse;
29 if os.name == 'nt': # Only modify if on Windows
30 if sys.version[0] == "2":
31 import codecs;
32 sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
33 sys.stderr = codecs.getwriter('utf-8')(sys.stderr);
34 else:
35 import io;
36 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace', line_buffering=True);
37 sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace', line_buffering=True);
39 hashlib_guaranteed = False;
40 os.environ["PYTHONIOENCODING"] = "UTF-8";
41 os.environ["LC_CTYPE"] = "UTF-8";
42 try:
43 reload(sys);
44 try:
45 sys.setdefaultencoding('UTF-8');
46 except NameError:
47 pass;
48 except AttributeError:
49 pass;
50 except NameError:
51 pass;
52 except AttributeError:
53 pass;
55 try:
56 from zlib import crc32;
57 except ImportError:
58 from binascii import crc32;
60 rarfile_support = False;
61 try:
62 import rarfile;
63 rarfile_support = True;
64 except ImportError:
65 rarfile_support = False;
67 py7zr_support = False;
68 try:
69 import py7zr;
70 py7zr_support = True;
71 except ImportError:
72 py7zr_support = False;
74 try:
75 from safetar import is_tarfile;
76 except ImportError:
77 try:
78 from xtarfile import is_tarfile;
79 except ImportError:
80 from tarfile import is_tarfile;
82 try:
83 import safetar as tarfile;
84 except ImportError:
85 try:
86 import xtarfile as tarfile;
87 except ImportError:
88 import tarfile;
90 haveparamiko = False;
91 try:
92 import paramiko;
93 haveparamiko = True;
94 except ImportError:
95 haveparamiko = False;
97 havepysftp = False;
98 try:
99 import pysftp;
100 havepysftp = True;
101 except ImportError:
102 havepysftp = False;
104 haverequests = False;
105 try:
106 import requests;
107 haverequests = True;
108 except ImportError:
109 haverequests = False;
111 if(haverequests):
112 import urllib3;
113 logging.getLogger("urllib3").setLevel(logging.WARNING);
115 try:
116 # Python 3 imports
117 from urllib.request import Request, build_opener, HTTPBasicAuthHandler;
118 from urllib.parse import urlparse;
119 except ImportError:
120 # Python 2 imports
121 from urllib2 import Request, build_opener, HTTPBasicAuthHandler;
122 from urlparse import urlparse;
124 if(sys.version[0]=="2"):
125 try:
126 from io import StringIO, BytesIO;
127 except ImportError:
128 try:
129 from cStringIO import StringIO;
130 from cStringIO import StringIO as BytesIO;
131 except ImportError:
132 from StringIO import StringIO;
133 from StringIO import StringIO as BytesIO;
134 elif(sys.version[0]>="3"):
135 from io import StringIO, BytesIO;
136 else:
137 teststringio = 0;
138 if(teststringio<=0):
139 try:
140 from cStringIO import StringIO as BytesIO;
141 teststringio = 1;
142 except ImportError:
143 teststringio = 0;
144 if(teststringio<=0):
145 try:
146 from StringIO import StringIO as BytesIO;
147 teststringio = 2;
148 except ImportError:
149 teststringio = 0;
150 if(teststringio<=0):
151 try:
152 from io import BytesIO;
153 teststringio = 3;
154 except ImportError:
155 teststringio = 0;
157 __use_pysftp__ = False;
158 if(not havepysftp):
159 __use_pysftp__ = False;
160 __file_format_name__ = "CatFile";
161 __program_name__ = "Py"+__file_format_name__;
162 __file_format_lower__ = __file_format_name__.lower();
163 __file_format_magic__ = __file_format_name__;
164 __file_format_len__ = len(__file_format_magic__);
165 __file_format_hex__ = binascii.hexlify(__file_format_magic__.encode("UTF-8")).decode("UTF-8");
166 __file_format_delimiter__ = "\x00";
167 __file_format_ver__ = "001";
168 __use_new_style__ = True;
169 __use_advanced_list__ = True;
170 __use_alt_inode__ = False;
171 __file_format_list__ = [__file_format_name__, __file_format_magic__, __file_format_lower__, __file_format_len__, __file_format_hex__, __file_format_delimiter__, __file_format_ver__, __use_new_style__, __use_advanced_list__, __use_alt_inode__];
172 __project__ = __program_name__;
173 __project_url__ = "https://github.com/GameMaker2k/PyCatFile";
174 __version_info__ = (0, 7, 8, "RC 1", 1);
175 __version_date_info__ = (2024, 4, 12, "RC 1", 1);
176 __version_date__ = str(__version_date_info__[0]) + "." + str(__version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2);
177 __revision__ = __version_info__[3];
178 __revision_id__ = "$Id$";
179 if(__version_info__[4] is not None):
180 __version_date_plusrc__ = __version_date__ + "-" + str(__version_date_info__[4]);
181 if(__version_info__[4] is None):
182 __version_date_plusrc__ = __version_date__;
183 if(__version_info__[3] is not None):
184 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]) + " " + str(__version_info__[3]);
185 if(__version_info__[3] is None):
186 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]);
188 PyBitness = platform.architecture();
189 if(PyBitness=="32bit" or PyBitness=="32"):
190 PyBitness = "32";
191 elif(PyBitness=="64bit" or PyBitness=="64"):
192 PyBitness = "64";
193 else:
194 PyBitness = "32";
196 geturls_ua_pycatfile_python = "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname=__project__, prover=__version__, prourl=__project_url__);
197 if(platform.python_implementation()!=""):
198 py_implementation = platform.python_implementation();
199 if(platform.python_implementation()==""):
200 py_implementation = "Python";
201 geturls_ua_pycatfile_python_alt = "Mozilla/5.0 ({osver}; {archtype}; +{prourl}) {pyimp}/{pyver} (KHTML, like Gecko) {proname}/{prover}".format(osver=platform.system()+" "+platform.release(), archtype=platform.machine(), prourl=__project_url__, pyimp=py_implementation, pyver=platform.python_version(), proname=__project__, prover=__version__);
202 geturls_ua_googlebot_google = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
203 geturls_ua_googlebot_google_old = "Googlebot/2.1 (+http://www.google.com/bot.html)";
204 geturls_headers_pycatfile_python = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__+"\";v=\""+str(__version__)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation+"\";v=\""+str(platform.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__), 'SEC-CH-UA-PLATFORM': ""+py_implementation+"", 'SEC-CH-UA-ARCH': ""+platform.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__), 'SEC-CH-UA-BITNESS': str(PyBitness)};
205 geturls_headers_pycatfile_python_alt = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python_alt, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__+"\";v=\""+str(__version__)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation+"\";v=\""+str(platform.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__), 'SEC-CH-UA-PLATFORM': ""+py_implementation+"", 'SEC-CH-UA-ARCH': ""+platform.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__), 'SEC-CH-UA-BITNESS': str(PyBitness)};
206 geturls_headers_googlebot_google = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
207 geturls_headers_googlebot_google_old = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google_old, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
209 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
210 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
211 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
213 tarfile_mimetype = "application/tar";
214 tarfile_tar_mimetype = tarfile_mimetype;
215 zipfile_mimetype = "application/zip";
216 zipfile_zip_mimetype = zipfile_mimetype;
217 rarfile_mimetype = "application/rar";
218 rarfile_rar_mimetype = rarfile_mimetype;
219 archivefile_mimetype = "application/x-"+__file_format_list__[1]+"";
220 archivefile_cat_mimetype = archivefile_mimetype;
221 archivefile_gzip_mimetype = "application/x-"+__file_format_list__[1]+"+gzip";
222 archivefile_gz_mimetype = archivefile_gzip_mimetype;
223 archivefile_bzip2_mimetype = "application/x-"+__file_format_list__[1]+"+bzip2";
224 archivefile_bz2_mimetype = archivefile_bzip2_mimetype;
225 archivefile_lz4_mimetype = "application/x-"+__file_format_list__[1]+"+lz4";
226 archivefile_lzop_mimetype = "application/x-"+__file_format_list__[1]+"+lzop";
227 archivefile_lzo_mimetype = archivefile_lzop_mimetype;
228 archivefile_zstandard_mimetype = "application/x-"+__file_format_list__[1]+"+zstandard";
229 archivefile_zstd_mimetype = archivefile_zstandard_mimetype;
230 archivefile_lzma_mimetype = "application/x-"+__file_format_list__[1]+"+lzma";
231 archivefile_xz_mimetype = "application/x-"+__file_format_list__[1]+"+xz";
232 archivefile_extensions = ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
234 if __name__ == "__main__":
235 import subprocess;
236 curscrpath = os.path.dirname(sys.argv[0]);
237 if(curscrpath==""):
238 curscrpath = ".";
239 if(os.sep=="\\"):
240 curscrpath = curscrpath.replace(os.sep, "/");
241 curscrpath = curscrpath + "/";
242 scrfile = curscrpath + "catfile.py";
243 if(os.path.exists(scrfile) and os.path.isfile(scrfile)):
244 scrcmd = subprocess.Popen([sys.executable, scrfile] + sys.argv[1:]);
245 scrcmd.wait();
247 def VerbosePrintOut(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
248 if(not dbgenable):
249 return True;
250 log_functions = {
251 "print": print,
252 "log": logging.info,
253 "warning": logging.warning,
254 "error": logging.error,
255 "critical": logging.critical,
256 "exception": logging.exception,
257 "logalt": lambda x: logging.log(dgblevel, x),
258 "debug": logging.debug
260 log_function = log_functions.get(outtype);
261 if(log_function):
262 log_function(dbgtxt);
263 return True;
264 return False;
266 def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
267 VerbosePrintOut(dbgtxt, outtype, dbgenable, dgblevel);
268 return dbgtxt;
270 def RemoveWindowsPath(dpath):
271 if(dpath is None):
272 dpath = "";
273 if(os.sep!="/"):
274 dpath = dpath.replace(os.path.sep, "/");
275 dpath = dpath.rstrip("/");
276 if(dpath=="." or dpath==".."):
277 dpath = dpath + "/";
278 return dpath;
280 def NormalizeRelativePath(inpath):
281 inpath = RemoveWindowsPath(inpath);
282 if(os.path.isabs(inpath)):
283 outpath = inpath;
284 else:
285 if(inpath.startswith("./") or inpath.startswith("../")):
286 outpath = inpath;
287 else:
288 outpath = "./" + inpath;
289 return outpath;
291 def PrependPath(base_dir, child_path):
292 # Check if base_dir is None or empty, if so, return child_path as is
293 if not base_dir:
294 return child_path;
295 # Ensure base_dir ends with exactly one slash
296 if not base_dir.endswith('/'):
297 base_dir += '/';
298 # Check if child_path starts with ./ or ../ (indicating a relative path)
299 if child_path.startswith('./') or child_path.startswith('../'):
300 # For relative paths, we don't alter the child_path
301 return base_dir + child_path;
302 else:
303 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
304 return base_dir + child_path.lstrip('/');
306 def ListDir(dirpath, followlink=False, duplicates=False):
307 if(isinstance(dirpath, (list, tuple, ))):
308 dirpath = list(filter(None, dirpath));
309 elif(isinstance(dirpath, (str, ))):
310 dirpath = list(filter(None, [dirpath]));
311 retlist = [];
312 for mydirfile in dirpath:
313 if(not os.path.exists(mydirfile)):
314 return False;
315 mydirfile = NormalizeRelativePath(mydirfile);
316 if(os.path.exists(mydirfile) and os.path.islink(mydirfile)):
317 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile));
318 if(os.path.exists(mydirfile) and os.path.isdir(mydirfile)):
319 for root, dirs, filenames in os.walk(mydirfile):
320 dpath = root;
321 dpath = RemoveWindowsPath(dpath);
322 if(dpath not in retlist and not duplicates):
323 retlist.append(dpath);
324 if(duplicates):
325 retlist.append(dpath);
326 for file in filenames:
327 fpath = os.path.join(root, file);
328 fpath = RemoveWindowsPath(fpath);
329 if(fpath not in retlist and not duplicates):
330 retlist.append(fpath);
331 if(duplicates):
332 retlist.append(fpath);
333 else:
334 retlist.append(RemoveWindowsPath(mydirfile));
335 return retlist;
337 def ListDirAdvanced(dirpath, followlink=False, duplicates=False):
338 if isinstance(dirpath, (list, tuple)):
339 dirpath = list(filter(None, dirpath));
340 elif isinstance(dirpath, str):
341 dirpath = list(filter(None, [dirpath]));
342 retlist = []
343 for mydirfile in dirpath:
344 if not os.path.exists(mydirfile):
345 return False;
346 mydirfile = NormalizeRelativePath(mydirfile);
347 if os.path.exists(mydirfile) and os.path.islink(mydirfile) and followlink:
348 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile))
349 if os.path.exists(mydirfile) and os.path.isdir(mydirfile):
350 for root, dirs, filenames in os.walk(mydirfile):
351 # Sort dirs and filenames alphabetically in place
352 dirs.sort(key=lambda x: x.lower());
353 filenames.sort(key=lambda x: x.lower());
354 dpath = RemoveWindowsPath(root);
355 if not duplicates and dpath not in retlist:
356 retlist.append(dpath);
357 elif duplicates:
358 retlist.append(dpath);
359 for file in filenames:
360 fpath = os.path.join(root, file);
361 fpath = RemoveWindowsPath(fpath);
362 if not duplicates and fpath not in retlist:
363 retlist.append(fpath);
364 elif duplicates:
365 retlist.append(fpath);
366 else:
367 retlist.append(RemoveWindowsPath(mydirfile));
368 return retlist;
370 def create_alias_function(prefix, base_name, suffix, target_function):
371 # Define a new function that wraps the target function
372 def alias_function(*args, **kwargs):
373 return target_function(*args, **kwargs);
375 # Create the function name by combining the prefix, base name, and the suffix
376 function_name = "{}{}{}".format(prefix, base_name, suffix);
378 # Add the new function to the global namespace
379 globals()[function_name] = alias_function;
381 def create_alias_function_alt(prefix, base_name, suffix, target_function):
382 # Create the function name by combining the prefix, base name, and the suffix
383 # Use the format method for string formatting, compatible with Python 2 and 3
384 function_name = "{}{}{}".format(prefix, base_name, suffix)
385 # Add the new function (alias of the target_function) to the global namespace
386 # This line is compatible as-is with both Python 2 and 3
387 globals()[function_name] = target_function
389 # initial_value can be 0xFFFF or 0x0000
390 def crc16_ansi(msg, initial_value=0xFFFF):
391 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
392 poly = 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
393 crc = initial_value; # Initial value
394 for b in msg:
395 crc ^= b << 8; # XOR byte into CRC top byte
396 for _ in range(8): # Process each bit
397 if crc & 0x8000: # If the top bit is set
398 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
399 else:
400 crc = crc << 1; # Just shift left
401 crc &= 0xFFFF; # Ensure CRC remains 16-bit
402 return crc;
404 # initial_value can be 0xFFFF or 0x0000
405 def crc16_ibm(msg, initial_value=0xFFFF):
406 return crc16_ansi(msg, initial_value);
408 # initial_value is 0xFFFF
409 def crc16(msg):
410 return crc16_ansi(msg, 0xFFFF);
412 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
413 def crc16_ccitt(msg, initial_value=0xFFFF):
414 # CRC-16-CCITT polynomial
415 poly = 0x1021; # Polynomial for CRC-16-CCITT
416 # Use the specified initial value
417 crc = initial_value;
418 for b in msg:
419 crc ^= b << 8; # XOR byte into CRC top byte
420 for _ in range(8): # Process each bit
421 if crc & 0x8000: # If the top bit is set
422 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
423 else:
424 crc = crc << 1; # Just shift left
425 crc &= 0xFFFF; # Ensure CRC remains 16-bit
426 return crc;
428 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
429 def crc64_ecma(msg, initial_value=0x0000000000000000):
430 # CRC-64-ECMA polynomial and initial value
431 poly = 0x42F0E1EBA9EA3693;
432 crc = initial_value; # Initial value for CRC-64-ECMA
433 for b in msg:
434 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
435 for _ in range(8): # Process each bit
436 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
437 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
438 else:
439 crc <<= 1; # Just shift left if the MSB is 0
440 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
441 return crc;
443 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
444 def crc64_iso(msg, initial_value=0xFFFFFFFFFFFFFFFF):
445 # CRC-64-ISO polynomial and initial value
446 poly = 0x000000000000001B;
447 crc = initial_value; # Common initial value for CRC-64-ISO
448 for b in msg:
449 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
450 for _ in range(8): # Process each bit
451 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
452 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
453 else:
454 crc <<= 1; # Just shift left if the MSB is 0
455 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
456 return crc;
458 def GetDataFromArray(data, path, default=None):
459 element = data;
460 try:
461 for key in path:
462 element = element[key];
463 return element;
464 except (KeyError, TypeError, IndexError):
465 return default;
467 def GetDataFromArrayAlt(structure, path, default=None):
468 element = structure;
469 for key in path:
470 if isinstance(element, dict) and key in element:
471 element = element[key];
472 elif isinstance(element, list) and isinstance(key, int) and -len(element) <= key < len(element):
473 element = element[key];
474 else:
475 return default;
476 return element;
478 def GetHeaderChecksum(inlist=[], checksumtype="crc32", encodedata=True, formatspecs=__file_format_list__):
479 if isinstance(inlist, list):
480 fileheader = AppendNullBytes(inlist, formatspecs[5]);
481 else:
482 fileheader = AppendNullByte(inlist, formatspecs[5]);
483 if(encodedata):
484 fileheader = fileheader.encode('UTF-8');
485 if(checksumtype=="none" or checksumtype==""):
486 catfileheadercshex = format(0, 'x').lower();
487 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
488 catfileheadercshex = format(crc16(fileheader) & 0xffff, '04x').lower();
489 elif(checksumtype=="crc16_ccitt"):
490 catfileheadercshex = format(crc16_ccitt(fileheader) & 0xffff, '04x').lower();
491 elif(checksumtype=="adler32"):
492 catfileheadercshex = format(zlib.adler32(fileheader) & 0xffffffff, '08x').lower();
493 elif(checksumtype=="crc32"):
494 catfileheadercshex = format(crc32(fileheader) & 0xffffffff, '08x').lower();
495 elif(checksumtype=="crc64_ecma"):
496 catfileheadercshex = format(crc64_ecma(fileheader) & 0xffffffffffffffff, '016x').lower();
497 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
498 catfileheadercshex = format(crc64_iso(fileheader) & 0xffffffffffffffff, '016x').lower();
499 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
500 checksumoutstr = hashlib.new(checksumtype);
501 checksumoutstr.update(fileheader);
502 catfileheadercshex = checksumoutstr.hexdigest().lower();
503 else:
504 catfileheadercshex = format(0, 'x').lower();
505 return catfileheadercshex;
507 def GetFileChecksum(instr, checksumtype="crc32", encodedata=True, formatspecs=__file_format_list__):
508 if(encodedata):
509 instr = instr.encode('UTF-8');
510 if(checksumtype=="none" or checksumtype==""):
511 catinstrcshex = format(0, 'x').lower();
512 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
513 catinstrcshex = format(crc16(instr) & 0xffff, '04x').lower();
514 elif(checksumtype=="crc16_ccitt"):
515 catinstrcshex = format(crc16_ccitt(instr) & 0xffff, '04x').lower();
516 elif(checksumtype=="adler32"):
517 catinstrcshex = format(zlib.adler32(instr) & 0xffffffff, '08x').lower();
518 elif(checksumtype=="crc32"):
519 catinstrcshex = format(crc32(instr) & 0xffffffff, '08x').lower();
520 elif(checksumtype=="crc64_ecma"):
521 catinstrcshex = format(crc64_ecma(instr) & 0xffffffffffffffff, '016x').lower();
522 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
523 catinstrcshex = format(crc64_iso(instr) & 0xffffffffffffffff, '016x').lower();
524 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
525 checksumoutstr = hashlib.new(checksumtype);
526 checksumoutstr.update(instr);
527 catinstrcshex = checksumoutstr.hexdigest().lower();
528 else:
529 catinstrcshex = format(0, 'x').lower();
530 return catinstrcshex;
532 def ValidateHeaderChecksum(inlist=[], checksumtype="crc32", inchecksum="0", formatspecs=__file_format_list__):
533 catfileheadercshex = GetHeaderChecksum(inlist, checksumtype, True, formatspecs);
534 inchecksum = inchecksum.lower();
535 catfileheadercshex = catfileheadercshex.lower();
536 if(inchecksum==catfileheadercshex):
537 return True;
538 else:
539 return False;
541 def ValidateFileChecksum(infile, checksumtype="crc32", inchecksum="0", formatspecs=__file_format_list__):
542 catinfilecshex = GetFileChecksum(infile, checksumtype, True, formatspecs);
543 inchecksum = inchecksum.lower();
544 catinfilecshex = catinfilecshex.lower();
545 if(inchecksum==catinfilecshex):
546 return True;
547 else:
548 return False;
550 def ReadTillNullByte(fp, delimiter=__file_format_delimiter__):
551 curbyte = b"";
552 curfullbyte = b"";
553 nullbyte = delimiter.encode("UTF-8");
554 while(True):
555 curbyte = fp.read(1);
556 if(curbyte==nullbyte or not curbyte):
557 break;
558 curfullbyte = curfullbyte + curbyte;
559 return curfullbyte.decode('UTF-8');
561 def ReadUntilNullByte(fp, delimiter=__file_format_delimiter__):
562 return ReadTillNullByte(fp, delimiter);
564 def SeekToEndOfFile(fp):
565 lasttell = 0;
566 while(True):
567 fp.seek(1, 1);
568 if(lasttell==fp.tell()):
569 break;
570 lasttell = fp.tell();
571 return True;
573 def ReadFileHeaderData(fp, rounds=0, delimiter=__file_format_delimiter__):
574 rocount = 0;
575 roend = int(rounds);
576 HeaderOut = [];
577 while(rocount<roend):
578 HeaderOut.append(ReadTillNullByte(fp, delimiter));
579 rocount = rocount + 1;
580 return HeaderOut;
582 def ReadFileHeaderDataBySize(fp, delimiter=__file_format_delimiter__):
583 headerpresize = ReadTillNullByte(fp, delimiter);
584 headersize = int(headerpresize, 16);
585 if(headersize<=0):
586 return [];
587 headercontent = str(fp.read(headersize).decode('UTF-8')).split(delimiter);
588 fp.seek(1, 1);
589 rocount = 0;
590 roend = int(len(headercontent));
591 HeaderOut = [headerpresize];
592 while(rocount<roend):
593 HeaderOut.append(headercontent[rocount]);
594 rocount = rocount + 1;
595 return HeaderOut;
597 def ReadFileHeaderDataWoSize(fp, delimiter=__file_format_delimiter__):
598 preheaderdata = ReadFileHeaderData(fp, 2, delimiter);
599 headersize = int(preheaderdata[0], 16);
600 headernumfields = int(preheaderdata[1], 16);
601 if(headersize<=0 or headernumfields<=0):
602 return [];
603 headerdata = ReadFileHeaderData(fp, headernumfields, delimiter);
604 HeaderOut = preheaderdata + headerdata;
605 return HeaderOut;
607 def ReadFileHeaderDataBySizeWithContent(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
608 delimiter = formatspecs[5];
609 fheaderstart = fp.tell();
610 HeaderOut = ReadFileHeaderDataBySize(fp, delimiter);
611 if(len(HeaderOut)==0):
612 return False;
613 if(re.findall("^[.|/]", HeaderOut[3])):
614 fname = HeaderOut[3];
615 else:
616 fname = "./"+HeaderOut[3];
617 fchecksumtype = HeaderOut[-3].lower();
618 fcs = HeaderOut[-2].lower();
619 fccs = HeaderOut[-1].lower();
620 fsize = int(HeaderOut[5], 16);
621 newfcs = GetHeaderChecksum(HeaderOut[:-2], HeaderOut[-3].lower(), True, formatspecs);
622 if(fcs!=newfcs and not skipchecksum):
623 VerbosePrintOut("File Header Checksum Error with file " + fname + " at offset " + str(fheaderstart));
624 return False;
625 fhend = fp.tell() - 1;
626 fcontentstart = fp.tell();
627 fcontents = "".encode('UTF-8');
628 if(fsize>0 and not listonly):
629 fcontents = fp.read(fsize);
630 elif(fsize>0 and listonly):
631 fp.seek(fsize, 1);
632 newfccs = GetFileChecksum(fcontents, HeaderOut[-3].lower(), False, formatspecs);
633 if(fccs!=newfccs and not skipchecksum and not listonly):
634 VerbosePrintOut("File Content Checksum Error with file " + fname + " at offset " + str(fcontentstart));
635 return False;
636 fp.seek(1, 1);
637 fcontentend = fp.tell() - 1;
638 HeaderOut.append(fcontents);
639 return HeaderOut;
641 def ReadFileHeaderDataBySizeWithContentToArray(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
642 delimiter = formatspecs[5];
643 fheaderstart = fp.tell();
644 if(formatspecs[7]):
645 HeaderOut = ReadFileHeaderDataBySize(fp, delimiter);
646 else:
647 HeaderOut = ReadFileHeaderDataWoSize(fp, delimiter);
648 if(len(HeaderOut)==0):
649 return False;
650 fheadsize = int(HeaderOut[0], 16);
651 fnumfields = int(HeaderOut[1], 16);
652 ftype = int(HeaderOut[2], 16);
653 if(re.findall("^[.|/]", HeaderOut[3])):
654 fname = HeaderOut[3];
655 else:
656 fname = "./"+HeaderOut[3];
657 fbasedir = os.path.dirname(fname);
658 flinkname = HeaderOut[4];
659 fsize = int(HeaderOut[5], 16);
660 fatime = int(HeaderOut[6], 16);
661 fmtime = int(HeaderOut[7], 16);
662 fctime = int(HeaderOut[8], 16);
663 fbtime = int(HeaderOut[9], 16);
664 fmode = int(HeaderOut[10], 16);
665 fchmode = stat.S_IMODE(fmode);
666 ftypemod = stat.S_IFMT(fmode);
667 fwinattributes = int(HeaderOut[11], 16);
668 fuid = int(HeaderOut[12], 16);
669 funame = HeaderOut[13];
670 fgid = int(HeaderOut[14], 16);
671 fgname = HeaderOut[15];
672 fid = int(HeaderOut[16], 16);
673 finode = int(HeaderOut[17], 16);
674 flinkcount = int(HeaderOut[18], 16);
675 fdev_minor = int(HeaderOut[19], 16);
676 fdev_major = int(HeaderOut[20], 16);
677 frdev_minor = int(HeaderOut[21], 16);
678 frdev_major = int(HeaderOut[22], 16);
679 fextrasize = int(HeaderOut[23], 16);
680 fextrafields = int(HeaderOut[24], 16);
681 extrafieldslist = [];
682 extrastart = 25;
683 extraend = extrastart + fextrafields;
684 extrafieldslist = [];
685 if(extrastart<extraend):
686 extrafieldslist.append(HeaderOut[extrastart]);
687 extrastart = extrastart + 1;
688 fchecksumtype = HeaderOut[extrastart].lower();
689 fcs = HeaderOut[extrastart + 1].lower();
690 fccs = HeaderOut[extrastart + 2].lower();
691 newfcs = GetHeaderChecksum(HeaderOut[:-2], HeaderOut[-3].lower(), True, formatspecs);
692 if(fcs!=newfcs and not skipchecksum):
693 VerbosePrintOut("File Header Checksum Error with file " + fname + " at offset " + str(fheaderstart));
694 return False;
695 fhend = fp.tell() - 1;
696 fcontentstart = fp.tell();
697 fcontents = "".encode('UTF-8');
698 pyhascontents = False;
699 if(fsize>0 and not listonly):
700 fcontents = fp.read(fsize);
701 pyhascontents = True;
702 elif(fsize>0 and listonly):
703 fp.seek(fsize, 1);
704 pyhascontents = False;
705 newfccs = GetFileChecksum(fcontents, HeaderOut[-3].lower(), False, formatspecs);
706 if(fccs!=newfccs and not skipchecksum and not listonly):
707 VerbosePrintOut("File Content Checksum Error with file " + fname + " at offset " + str(fcontentstart));
708 return False;
709 fp.seek(1, 1);
710 fcontentend = fp.tell() - 1;
711 catlist = {'fheadersize': fheadsize, 'fhstart': fheaderstart, 'fhend': fhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fwinattributes': fwinattributes, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': fchecksumtype, 'fnumfields': fnumfields + 2, 'frawheader': HeaderOut, 'fextrafields': fextrafields, 'fextrafieldsize': fextrasize, 'fextralist': extrafieldslist, 'fheaderchecksum': fcs, 'fcontentchecksum': fccs, 'fhascontents': pyhascontents, 'fcontentstart': fcontentstart, 'fcontentend': fcontentend, 'fcontents': fcontents};
712 return catlist;
714 def ReadFileHeaderDataBySizeWithContentToList(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
715 delimiter = formatspecs[5];
716 fheaderstart = fp.tell();
717 if(formatspecs[7]):
718 HeaderOut = ReadFileHeaderDataBySize(fp, delimiter);
719 else:
720 HeaderOut = ReadFileHeaderDataWoSize(fp, delimiter);
721 if(len(HeaderOut)==0):
722 return False;
723 fheadsize = int(HeaderOut[0], 16);
724 fnumfields = int(HeaderOut[1], 16);
725 ftype = int(HeaderOut[2], 16);
726 if(re.findall("^[.|/]", HeaderOut[3])):
727 fname = HeaderOut[3];
728 else:
729 fname = "./"+HeaderOut[3];
730 fbasedir = os.path.dirname(fname);
731 flinkname = HeaderOut[4];
732 fsize = int(HeaderOut[5], 16);
733 fatime = int(HeaderOut[6], 16);
734 fmtime = int(HeaderOut[7], 16);
735 fctime = int(HeaderOut[8], 16);
736 fbtime = int(HeaderOut[9], 16);
737 fmode = int(HeaderOut[10], 16);
738 fchmode = stat.S_IMODE(fmode);
739 ftypemod = stat.S_IFMT(fmode);
740 fwinattributes = int(HeaderOut[11], 16);
741 fuid = int(HeaderOut[12], 16);
742 funame = HeaderOut[13];
743 fgid = int(HeaderOut[14], 16);
744 fgname = HeaderOut[15];
745 fid = int(HeaderOut[16], 16);
746 finode = int(HeaderOut[17], 16);
747 flinkcount = int(HeaderOut[18], 16);
748 fdev_minor = int(HeaderOut[19], 16);
749 fdev_major = int(HeaderOut[20], 16);
750 frdev_minor = int(HeaderOut[21], 16);
751 frdev_major = int(HeaderOut[22], 16);
752 fextrasize = int(HeaderOut[23], 16);
753 fextrafields = int(HeaderOut[24], 16);
754 extrafieldslist = [];
755 extrastart = 25;
756 extraend = extrastart + fextrafields;
757 extrafieldslist = [];
758 if(extrastart<extraend):
759 extrafieldslist.append(HeaderOut[extrastart]);
760 extrastart = extrastart + 1;
761 fchecksumtype = HeaderOut[extrastart].lower();
762 fcs = HeaderOut[extrastart + 1].lower();
763 fccs = HeaderOut[extrastart + 2].lower();
764 newfcs = GetHeaderChecksum(HeaderOut[:-2], HeaderOut[-3].lower(), True, formatspecs);
765 if(fcs!=newfcs and not skipchecksum):
766 VerbosePrintOut("File Header Checksum Error with file " + fname + " at offset " + str(fheaderstart));
767 return False;
768 fhend = fp.tell() - 1;
769 fcontentstart = fp.tell();
770 fcontents = "".encode('UTF-8');
771 pyhascontents = False;
772 if(fsize>0 and not listonly):
773 fcontents = fp.read(fsize);
774 pyhascontents = True;
775 elif(fsize>0 and listonly):
776 fp.seek(fsize, 1);
777 pyhascontents = False;
778 newfccs = GetFileChecksum(fcontents, HeaderOut[-3].lower(), False, formatspecs);
779 if(fccs!=newfccs and not skipchecksum and not listonly):
780 VerbosePrintOut("File Content Checksum Error with file " + fname + " at offset " + str(fcontentstart));
781 return False;
782 fp.seek(1, 1);
783 fcontentend = fp.tell() - 1;
784 catlist = [ftype, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fid, finode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrafieldslist, fchecksumtype, fcontents];
785 return catlist;
787 def ReadFileDataBySizeWithContent(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
788 delimiter = formatspecs[5];
789 curloc = fp.tell();
790 if(curloc>0):
791 fp.seek(0, 0);
792 catheader = ReadFileHeaderData(fp, 4, delimiter);
793 if(curloc>0):
794 fp.seek(curloc, 0);
795 headercheck = ValidateHeaderChecksum(catheader[:-1], catheader[2], catheader[3], formatspecs);
796 if(not headercheck and not skipchecksum):
797 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
798 return False;
799 fnumfiles = int(catheader[1], 16);
800 countnum = 0;
801 flist = [];
802 while(countnum < fnumfiles):
803 HeaderOut = ReadFileHeaderDataBySizeWithContent(fp, listonly, skipchecksum, formatspecs)
804 if(len(HeaderOut)==0):
805 break;
806 flist.append(HeaderOut);
807 countnum = countnum + 1;
808 return flist;
810 def ReadFileDataBySizeWithContentToArray(fp, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
811 delimiter = formatspecs[5];
812 curloc = fp.tell();
813 if(curloc>0):
814 fp.seek(0, 0);
815 catheader = ReadFileHeaderData(fp, 4, delimiter);
816 if(curloc>0):
817 fp.seek(curloc, 0);
818 headercheck = ValidateHeaderChecksum(catheader[:-1], catheader[2], catheader[3], formatspecs);
819 if(not headercheck and not skipchecksum):
820 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
821 return False;
822 catstring = catheader[0];
823 catversion = re.findall(r"([\d]+)$", catstring);
824 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
825 fprenumfiles = catheader[1];
826 fnumfiles = int(fprenumfiles, 16);
827 fprechecksumtype = catheader[2];
828 fprechecksum = catheader[3];
829 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
830 if(seekstart<0 and seekstart>fnumfiles):
831 seekstart = 0;
832 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
833 seekend = fnumfiles;
834 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
835 seekend = fnumfiles - abs(seekend);
836 if(seekstart>0):
837 il = 0;
838 while(il < seekstart):
839 prefhstart = fp.tell();
840 preheaderdata = ReadFileHeaderDataBySize(fp, formatspecs[5]);
841 if(len(preheaderdata)==0):
842 break;
843 prefsize = int(preheaderdata[5], 16);
844 prenewfcs = GetHeaderChecksum(preheaderdata[:-2], preheaderdata[-3].lower(), True, formatspecs);
845 prefcs = preheaderdata[-2];
846 if(prefcs!=prenewfcs and not skipchecksum):
847 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
848 return False;
849 valid_archive = False;
850 invalid_archive = True;
851 prefhend = fp.tell() - 1;
852 prefcontentstart = fp.tell();
853 prefcontents = "";
854 pyhascontents = False;
855 if(prefsize>0):
856 prefcontents = fp.read(prefsize);
857 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
858 prefccs = preheaderdata[-1];
859 pyhascontents = True;
860 if(prefccs!=prenewfccs and not skipchecksum):
861 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
862 return False;
863 fp.seek(1, 1);
864 il = il + 1;
865 realidnum = 0;
866 countnum = seekstart;
867 while(countnum < seekend):
868 catlist['ffilelist'].update({realidnum: {'fid': realidnum, 'fidalt': realidnum}});
869 HeaderOut = ReadFileHeaderDataBySizeWithContentToArray(fp, listonly, skipchecksum, formatspecs);
870 if(len(HeaderOut)):
871 break;
872 catlist['ffilelist'][realidnum].update(HeaderOut);
873 countnum = countnum + 1;
874 realidnum = realidnum + 1;
875 return catlist;
877 def ReadFileDataBySizeWithContentToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
878 if(isinstance(infile, dict)):
879 listcatfiles = infile;
880 else:
881 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
882 return False;
883 listcatfiles = ReadFileDataBySizeWithContentToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs);
884 if(not listcatfiles):
885 return False;
886 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
887 lenlist = len(listcatfiles['ffilelist']);
888 lcfi = 0;
889 lcfx = int(listcatfiles['fnumfiles']);
890 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
891 lcfx = int(lenlist);
892 else:
893 lcfx = int(listcatfiles['fnumfiles']);
894 while(lcfi < lcfx):
895 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
896 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
897 catarray['filetoid'].update(filetoidarray);
898 catarray['idtofile'].update(idtofilearray);
899 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
900 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
901 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
902 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
903 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
904 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
905 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
906 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
907 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
908 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
909 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
910 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
911 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
912 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
913 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
914 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
915 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
916 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
917 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
918 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
919 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
920 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
921 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
922 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
923 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
924 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
925 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
926 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
927 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
928 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
929 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
930 lcfi = lcfi + 1;
931 return catarray;
933 def ReadFileDataBySizeWithContentToList(fp, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
934 delimiter = formatspecs[5];
935 curloc = fp.tell();
936 if(curloc>0):
937 fp.seek(0, 0);
938 catheader = ReadFileHeaderData(fp, 4, delimiter);
939 if(curloc>0):
940 fp.seek(curloc, 0);
941 headercheck = ValidateHeaderChecksum(catheader[:-1], catheader[2], catheader[3], formatspecs);
942 if(not headercheck and not skipchecksum):
943 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
944 return False;
945 catstring = catheader[0];
946 catversion = re.findall(r"([\d]+)$", catstring);
947 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
948 fprenumfiles = catheader[1];
949 fnumfiles = int(fprenumfiles, 16);
950 fprechecksumtype = catheader[2];
951 fprechecksum = catheader[3];
952 catlist = [];
953 if(seekstart<0 and seekstart>fnumfiles):
954 seekstart = 0;
955 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
956 seekend = fnumfiles;
957 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
958 seekend = fnumfiles - abs(seekend);
959 if(seekstart>0):
960 il = 0;
961 while(il < seekstart):
962 prefhstart = fp.tell();
963 preheaderdata = ReadFileHeaderDataBySize(fp, formatspecs[5]);
964 if(len(preheaderdata)==0):
965 break;
966 prefsize = int(preheaderdata[5], 16);
967 prenewfcs = GetHeaderChecksum(preheaderdata[:-2], preheaderdata[-3].lower(), True, formatspecs);
968 prefcs = preheaderdata[-2];
969 if(prefcs!=prenewfcs and not skipchecksum):
970 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
971 return False;
972 valid_archive = False;
973 invalid_archive = True;
974 prefhend = fp.tell() - 1;
975 prefcontentstart = fp.tell();
976 prefcontents = "";
977 pyhascontents = False;
978 if(prefsize>0):
979 prefcontents = fp.read(prefsize);
980 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
981 prefccs = preheaderdata[-1];
982 pyhascontents = True;
983 if(prefccs!=prenewfccs and not skipchecksum):
984 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
985 return False;
986 fp.seek(1, 1);
987 il = il + 1;
988 realidnum = 0;
989 countnum = seekstart;
990 while(countnum < seekend):
991 HeaderOut = ReadFileHeaderDataBySizeWithContentToList(fp, listonly, skipchecksum, formatspecs);
992 if(len(HeaderOut)):
993 break;
994 catlist.append(HeaderOut);
995 countnum = countnum + 1;
996 realidnum = realidnum + 1;
997 return catlist;
999 def ReadInFileBySizeWithContentToArray(infile, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
1000 delimiter = formatspecs[5];
1001 if(hasattr(infile, "read") or hasattr(infile, "write")):
1002 fp = infile;
1003 fp.seek(0, 0);
1004 fp = UncompressArchiveFile(fp, formatspecs);
1005 checkcompressfile = CheckCompressionSubType(fp, formatspecs, True);
1006 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
1007 return False;
1008 if(not fp):
1009 return False;
1010 fp.seek(0, 0);
1011 elif(infile=="-"):
1012 fp = BytesIO();
1013 if(hasattr(sys.stdin, "buffer")):
1014 shutil.copyfileobj(sys.stdin.buffer, fp);
1015 else:
1016 shutil.copyfileobj(sys.stdin, fp);
1017 fp.seek(0, 0);
1018 fp = UncompressArchiveFile(fp, formatspecs);
1019 if(not fp):
1020 return False;
1021 fp.seek(0, 0);
1022 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
1023 fp = download_file_from_internet_file(infile);
1024 fp = UncompressArchiveFile(fp, formatspecs);
1025 fp.seek(0, 0);
1026 if(not fp):
1027 return False;
1028 fp.seek(0, 0);
1029 else:
1030 infile = RemoveWindowsPath(infile);
1031 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
1032 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
1033 return False;
1034 compresscheck = CheckCompressionType(infile, formatspecs, True);
1035 if(not compresscheck):
1036 fextname = os.path.splitext(infile)[1];
1037 if(fextname==".gz"):
1038 compresscheck = "gzip";
1039 elif(fextname==".bz2"):
1040 compresscheck = "bzip2";
1041 elif(fextname==".zst"):
1042 compresscheck = "zstd";
1043 elif(fextname==".lz4" or fextname==".clz4"):
1044 compresscheck = "lz4";
1045 elif(fextname==".lzo" or fextname==".lzop"):
1046 compresscheck = "lzo";
1047 elif(fextname==".lzma" or fextname==".xz"):
1048 compresscheck = "lzma";
1049 else:
1050 return False;
1051 if(not compresscheck):
1052 return False;
1053 fp = UncompressFile(infile, formatspecs, "rb");
1054 return ReadFileDataBySizeWithContentToArray(fp, listonly, skipchecksum, formatspecs);
1056 def ReadInFileBySizeWithContentToList(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
1057 delimiter = formatspecs[5];
1058 if(hasattr(infile, "read") or hasattr(infile, "write")):
1059 fp = infile;
1060 fp.seek(0, 0);
1061 fp = UncompressArchiveFile(fp, formatspecs);
1062 checkcompressfile = CheckCompressionSubType(fp, formatspecs, True);
1063 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
1064 return False;
1065 if(not fp):
1066 return False;
1067 fp.seek(0, 0);
1068 elif(infile=="-"):
1069 fp = BytesIO();
1070 if(hasattr(sys.stdin, "buffer")):
1071 shutil.copyfileobj(sys.stdin.buffer, fp);
1072 else:
1073 shutil.copyfileobj(sys.stdin, fp);
1074 fp.seek(0, 0);
1075 fp = UncompressArchiveFile(fp, formatspecs);
1076 if(not fp):
1077 return False;
1078 fp.seek(0, 0);
1079 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
1080 fp = download_file_from_internet_file(infile);
1081 fp = UncompressArchiveFile(fp, formatspecs);
1082 fp.seek(0, 0);
1083 if(not fp):
1084 return False;
1085 fp.seek(0, 0);
1086 else:
1087 infile = RemoveWindowsPath(infile);
1088 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
1089 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
1090 return False;
1091 compresscheck = CheckCompressionType(infile, formatspecs, True);
1092 if(not compresscheck):
1093 fextname = os.path.splitext(infile)[1];
1094 if(fextname==".gz"):
1095 compresscheck = "gzip";
1096 elif(fextname==".bz2"):
1097 compresscheck = "bzip2";
1098 elif(fextname==".zst"):
1099 compresscheck = "zstd";
1100 elif(fextname==".lz4" or fextname==".clz4"):
1101 compresscheck = "lz4";
1102 elif(fextname==".lzo" or fextname==".lzop"):
1103 compresscheck = "lzo";
1104 elif(fextname==".lzma" or fextname==".xz"):
1105 compresscheck = "lzma";
1106 else:
1107 return False;
1108 if(not compresscheck):
1109 return False;
1110 fp = UncompressFile(infile, formatspecs, "rb");
1111 return ReadFileDataBySizeWithContentToList(fp, seekstart, seekend, listonly, skipchecksum, formatspecs);
1113 def ReadInFileBySizeWithContentToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
1114 if(isinstance(infile, dict)):
1115 listcatfiles = infile;
1116 else:
1117 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
1118 infile = RemoveWindowsPath(infile);
1119 listcatfiles = ReadInFileBySizeWithContentToArray(infile, listonly, skipchecksum, formatspecs);
1120 if(not listcatfiles):
1121 return False;
1122 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
1123 lenlist = len(listcatfiles['ffilelist']);
1124 lcfi = 0;
1125 lcfx = int(listcatfiles['fnumfiles']);
1126 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
1127 lcfx = int(lenlist);
1128 else:
1129 lcfx = int(listcatfiles['fnumfiles']);
1130 while(lcfi < lcfx):
1131 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
1132 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
1133 catarray['filetoid'].update(filetoidarray);
1134 catarray['idtofile'].update(idtofilearray);
1135 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
1136 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
1137 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
1138 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
1139 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
1140 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
1141 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
1142 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
1143 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
1144 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
1145 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
1146 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
1147 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
1148 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
1149 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
1150 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
1151 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
1152 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
1153 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
1154 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
1155 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
1156 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
1157 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
1158 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
1159 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
1160 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
1161 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
1162 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
1163 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
1164 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
1165 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
1166 lcfi = lcfi + 1;
1167 return catarray;
1169 def AppendNullByte(indata, delimiter=__file_format_delimiter__):
1170 outdata = str(indata) + delimiter;
1171 return outdata;
1173 def AppendNullBytes(indata=[], delimiter=__file_format_delimiter__):
1174 outdata = "";
1175 inum = 0;
1176 il = len(indata);
1177 while(inum < il):
1178 outdata = outdata + AppendNullByte(indata[inum], delimiter);
1179 inum = inum + 1;
1180 return outdata;
1182 def AppendFileHeader(fp, numfiles, checksumtype="crc32", formatspecs=__file_format_list__):
1183 delimiter = formatspecs[5];
1184 catver = formatspecs[6];
1185 fileheaderver = str(int(catver.replace(".", "")));
1186 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
1187 fp.write(fileheader.encode('UTF-8'));
1188 fnumfiles = format(int(numfiles), 'x').lower();
1189 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
1190 catfileheadercshex = GetFileChecksum(fileheader + fnumfilesa, checksumtype, True, formatspecs);
1191 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
1192 fp.write(fnumfilesa.encode('UTF-8'));
1193 try:
1194 fp.flush();
1195 os.fsync(fp.fileno());
1196 except io.UnsupportedOperation:
1197 pass;
1198 except AttributeError:
1199 pass;
1200 return fp;
1202 def AppendFileHeaderWithContent(fp, filevalues=[], extradata=[], filecontent="", checksumtype="crc32", formatspecs=__file_format_list__):
1203 extrafields = format(len(extradata), 'x').lower();
1204 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
1205 if(len(extradata)>0):
1206 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
1207 extrasizelen = format(len(extrasizestr), 'x').lower();
1208 catoutlen = len(filevalues) + len(extradata) + 5;
1209 catoutlenhex = format(catoutlen, 'x').lower();
1210 catoutlist = filevalues;
1211 catoutlist.insert(0, catoutlenhex);
1212 catoutlist.append(extrasizelen);
1213 catoutlist.append(extrafields);
1214 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
1215 if(len(extradata)>0):
1216 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
1217 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
1218 catfileheadercshex = GetFileChecksum(catfileoutstr, checksumtype, True, formatspecs);
1219 catfilecontentcshex = GetFileChecksum(filecontent, checksumtype, False, formatspecs);
1220 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1221 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower();
1222 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
1223 catfileheadercshex = GetFileChecksum(catfileoutstr, checksumtype, True, formatspecs);
1224 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1225 catfileoutstrecd = catfileoutstr.encode('UTF-8');
1226 nullstrecd = formatspecs[5].encode('UTF-8');
1227 catfileout = catfileoutstrecd + filecontent + nullstrecd;
1228 fp.write(catfileout);
1229 try:
1230 fp.flush();
1231 os.fsync(fp.fileno());
1232 except io.UnsupportedOperation:
1233 pass;
1234 except AttributeError:
1235 pass;
1236 return fp;
1238 def AppendFilesWithContent(infiles, fp, dirlistfromtxt=False, filevalues=[], extradata=[], followlink=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False):
1239 advancedlist = formatspecs[8];
1240 altinode = formatspecs[9];
1241 if(verbose):
1242 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1243 if(infiles=="-"):
1244 for line in sys.stdin:
1245 infilelist.append(line.strip());
1246 infilelist = list(filter(None, infilelist));
1247 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
1248 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
1249 return False;
1250 with UncompressFile(infiles, formatspecs, "r") as finfile:
1251 for line in finfile:
1252 infilelist.append(line.strip());
1253 infilelist = list(filter(None, infilelist));
1254 else:
1255 if(isinstance(infiles, (list, tuple, ))):
1256 infilelist = list(filter(None, infiles));
1257 elif(isinstance(infiles, (str, ))):
1258 infilelist = list(filter(None, [infiles]));
1259 if(advancedlist):
1260 GetDirList = ListDirAdvanced(infilelist, followlink, False);
1261 else:
1262 GetDirList = ListDir(infilelist, followlink, False);
1263 if(not GetDirList):
1264 return False;
1265 curinode = 0;
1266 curfid = 0;
1267 inodelist = [];
1268 inodetofile = {};
1269 filetoinode = {};
1270 inodetocatinode = {};
1271 numfiles = int(len(GetDirList));
1272 fnumfiles = format(numfiles, 'x').lower();
1273 AppendFileHeader(fp, fnumfiles, checksumtype, formatspecs);
1274 for curfname in GetDirList:
1275 catfhstart = fp.tell();
1276 if(re.findall("^[.|/]", curfname)):
1277 fname = curfname;
1278 else:
1279 fname = "./"+curfname;
1280 if(verbose):
1281 VerbosePrintOut(fname);
1282 if(not followlink or followlink is None):
1283 fstatinfo = os.lstat(fname);
1284 else:
1285 fstatinfo = os.stat(fname);
1286 fpremode = fstatinfo.st_mode;
1287 finode = fstatinfo.st_ino;
1288 flinkcount = fstatinfo.st_nlink;
1289 ftype = 0;
1290 if(stat.S_ISREG(fpremode)):
1291 ftype = 0;
1292 elif(stat.S_ISLNK(fpremode)):
1293 ftype = 2;
1294 elif(stat.S_ISCHR(fpremode)):
1295 ftype = 3;
1296 elif(stat.S_ISBLK(fpremode)):
1297 ftype = 4;
1298 elif(stat.S_ISDIR(fpremode)):
1299 ftype = 5;
1300 elif(stat.S_ISFIFO(fpremode)):
1301 ftype = 6;
1302 elif(stat.S_ISSOCK(fpremode)):
1303 ftype = 8;
1304 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
1305 ftype = 9;
1306 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
1307 ftype = 10;
1308 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
1309 ftype = 111;
1310 else:
1311 ftype = 0;
1312 flinkname = "";
1313 fcurfid = format(int(curfid), 'x').lower();
1314 if(not followlink and finode!=0):
1315 if(ftype!=1):
1316 if(finode in inodelist):
1317 ftype = 1;
1318 flinkname = inodetofile[finode];
1319 if(altinode):
1320 fcurinode = format(int(finode), 'x').lower();
1321 else:
1322 fcurinode = format(int(inodetocatinode[finode]), 'x').lower();
1323 if(finode not in inodelist):
1324 inodelist.append(finode);
1325 inodetofile.update({finode: fname});
1326 inodetocatinode.update({finode: curinode});
1327 if(altinode):
1328 fcurinode = format(int(finode), 'x').lower();
1329 else:
1330 fcurinode = format(int(curinode), 'x').lower();
1331 curinode = curinode + 1;
1332 else:
1333 fcurinode = format(int(curinode), 'x').lower();
1334 curinode = curinode + 1;
1335 curfid = curfid + 1;
1336 if(ftype==2):
1337 flinkname = os.readlink(fname);
1338 fdev = fstatinfo.st_dev;
1339 getfdev = GetDevMajorMinor(fdev);
1340 fdev_minor = getfdev[0];
1341 fdev_major = getfdev[1];
1342 frdev = fstatinfo.st_dev;
1343 if(hasattr(fstatinfo, "st_rdev")):
1344 frdev = fstatinfo.st_rdev;
1345 else:
1346 frdev = fstatinfo.st_dev;
1347 getfrdev = GetDevMajorMinor(frdev);
1348 frdev_minor = getfrdev[0];
1349 frdev_major = getfrdev[1];
1350 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
1351 fsize = format(int("0"), 'x').lower();
1352 elif(ftype==0 or ftype==7):
1353 fsize = format(int(fstatinfo.st_size), 'x').lower();
1354 else:
1355 fsize = format(int(fstatinfo.st_size)).lower();
1356 fatime = format(int(fstatinfo.st_atime), 'x').lower();
1357 fmtime = format(int(fstatinfo.st_mtime), 'x').lower();
1358 fctime = format(int(fstatinfo.st_ctime), 'x').lower();
1359 if(hasattr(fstatinfo, "st_birthtime")):
1360 fbtime = format(int(fstatinfo.st_birthtime), 'x').lower();
1361 else:
1362 fbtime = format(int(fstatinfo.st_ctime), 'x').lower();
1363 fmode = format(int(fstatinfo.st_mode), 'x').lower();
1364 fchmode = format(int(stat.S_IMODE(fstatinfo.st_mode)), 'x').lower();
1365 ftypemod = format(int(stat.S_IFMT(fstatinfo.st_mode)), 'x').lower();
1366 fuid = format(int(fstatinfo.st_uid), 'x').lower();
1367 fgid = format(int(fstatinfo.st_gid), 'x').lower();
1368 funame = "";
1369 try:
1370 import pwd;
1371 try:
1372 userinfo = pwd.getpwuid(fstatinfo.st_uid);
1373 funame = userinfo.pw_name;
1374 except KeyError:
1375 funame = "";
1376 except ImportError:
1377 funame = "";
1378 fgname = "";
1379 try:
1380 import grp;
1381 try:
1382 groupinfo = grp.getgrgid(fstatinfo.st_gid);
1383 fgname = groupinfo.gr_name;
1384 except KeyError:
1385 fgname = "";
1386 except ImportError:
1387 fgname = "";
1388 fdev_minor = format(int(fdev_minor), 'x').lower();
1389 fdev_major = format(int(fdev_major), 'x').lower();
1390 frdev_minor = format(int(frdev_minor), 'x').lower();
1391 frdev_major = format(int(frdev_major), 'x').lower();
1392 finode = format(int(finode), 'x').lower();
1393 flinkcount = format(int(flinkcount), 'x').lower();
1394 if(hasattr(fstatinfo, "st_file_attributes")):
1395 fwinattributes = format(int(fstatinfo.st_file_attributes), 'x').lower();
1396 else:
1397 fwinattributes = format(int(0), 'x').lower();
1398 fcontents = "".encode('UTF-8');
1399 chunk_size = 1024;
1400 if(ftype==0 or ftype==7):
1401 with open(fname, "rb") as fpc:
1402 while(True):
1403 chunk = fpc.read(chunk_size);
1404 if(not chunk):
1405 break
1406 fcontents += chunk;
1407 if(followlink and (ftype==1 or ftype==2)):
1408 flstatinfo = os.stat(flinkname);
1409 with open(flinkname, "rb") as fpc:
1410 while(True):
1411 chunk = fpc.read(chunk_size);
1412 if(not chunk):
1413 break;
1414 fcontents += chunk;
1415 ftypehex = format(ftype, 'x').lower();
1416 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major];
1417 fp = AppendFileHeaderWithContent(fp, catoutlist, extradata, fcontents, checksumtype, formatspecs);
1418 if(numfiles>0):
1419 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
1420 fp.seek(0, 0);
1421 return fp;
1423 def AppendListsWithContent(inlist, fp, dirlistfromtxt=False, filevalues=[], extradata=[], followlink=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False):
1424 if(verbose):
1425 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1426 GetDirList = inlist;
1427 if(not GetDirList):
1428 return False;
1429 curinode = 0;
1430 curfid = 0;
1431 inodelist = [];
1432 inodetofile = {};
1433 filetoinode = {};
1434 inodetocatinode = {};
1435 numfiles = int(len(GetDirList));
1436 fnumfiles = format(numfiles, 'x').lower();
1437 AppendFileHeader(fp, fnumfiles, checksumtype, formatspecs);
1438 for curfname in GetDirList:
1439 ftype = format(curfname[0], 'x').lower();
1440 if(re.findall("^[.|/]", curfname[1])):
1441 fname = curfname[1];
1442 else:
1443 fname = "./"+curfname[1];
1444 fbasedir = os.path.dirname(fname);
1445 flinkname = curfname[2];
1446 fsize = format(curfname[3], 'x').lower();
1447 fatime = format(curfname[4], 'x').lower();
1448 fmtime = format(curfname[5], 'x').lower();
1449 fctime = format(curfname[6], 'x').lower();
1450 fbtime = format(curfname[7], 'x').lower();
1451 fmode = format(curfname[8], 'x').lower();
1452 fwinattributes = format(curfname[9], 'x').lower();
1453 fuid = format(curfname[10], 'x').lower();
1454 funame = curfname[11];
1455 fgid = format(curfname[12], 'x').lower();
1456 fgname = curfname[13];
1457 fid = format(curfname[14], 'x').lower();
1458 finode = format(curfname[15], 'x').lower();
1459 flinkcount = format(curfname[16], 'x').lower();
1460 fdev_minor = format(curfname[17], 'x').lower();
1461 fdev_major = format(curfname[18], 'x').lower();
1462 frdev_minor = format(curfname[19], 'x').lower();
1463 frdev_major = format(curfname[20], 'x').lower();
1464 extradata = curfname[21];
1465 fchecksumtype = curfname[22];
1466 fcontents = curfname[23];
1467 catoutlist = [ftype, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fid, finode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major];
1468 fp = AppendFileHeaderWithContent(fp, catoutlist, extradata, fcontents, checksumtype, formatspecs);
1469 if(numfiles>0):
1470 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
1471 fp.seek(0, 0);
1472 return fp;
1474 def AppendInFileWithContent(infile, fp, dirlistfromtxt=False, filevalues=[], extradata=[], followlink=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False):
1475 inlist = ReadInFileBySizeWithContentToList(infile, 0, 0, False, False, formatspecs);
1476 return AppendListsWithContent(inlist, fp, dirlistfromtxt, filevalues, extradata, followlink, checksumtype, formatspecs, verbose);
1478 def AppendFilesWithContentToOutFile(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, filevalues=[], extradata=[], followlink=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
1479 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1480 if(os.path.exists(outfile)):
1481 os.unlink(outfile);
1482 if(outfile=="-"):
1483 verbose = False;
1484 catfpfp = BytesIO();
1485 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1486 catfp = outfile;
1487 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
1488 catfp = BytesIO();
1489 else:
1490 fbasename = os.path.splitext(outfile)[0];
1491 fextname = os.path.splitext(outfile)[1];
1492 catfp = CompressOpenFile(outfile, compressionlevel);
1493 catfp = AppendFilesWithContent(infiles, catfp, dirlistfromtxt, filevalues, extradata, followlink, checksumtype, formatspecs, verbose);
1494 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1495 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1496 try:
1497 catfp.flush();
1498 os.fsync(catfp.fileno());
1499 except io.UnsupportedOperation:
1500 pass;
1501 except AttributeError:
1502 pass;
1503 if(outfile=="-"):
1504 catfp.seek(0, 0);
1505 if(hasattr(sys.stdout, "buffer")):
1506 shutil.copyfileobj(catfp, sys.stdout.buffer);
1507 else:
1508 shutil.copyfileobj(catfp, sys.stdout);
1509 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
1510 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1511 catfp.seek(0, 0);
1512 upload_file_to_internet_file(catfp, outfile);
1513 if(returnfp):
1514 catfp.seek(0, 0);
1515 return catfp;
1516 else:
1517 catfp.close();
1518 return True;
1520 def AppendListsWithContentToOutFile(inlist, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, filevalues=[], extradata=[], followlink=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
1521 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1522 if(os.path.exists(outfile)):
1523 os.unlink(outfile);
1524 if(outfile=="-"):
1525 verbose = False;
1526 catfpfp = BytesIO();
1527 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1528 catfp = outfile;
1529 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
1530 catfp = BytesIO();
1531 else:
1532 fbasename = os.path.splitext(outfile)[0];
1533 fextname = os.path.splitext(outfile)[1];
1534 catfp = CompressOpenFile(outfile, compressionlevel);
1535 catfp = AppendListsWithContent(inlist, catfp, dirlistfromtxt, filevalues, extradata, followlink, checksumtype, formatspecs, verbose);
1536 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1537 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1538 try:
1539 catfp.flush();
1540 os.fsync(catfp.fileno());
1541 except io.UnsupportedOperation:
1542 pass;
1543 except AttributeError:
1544 pass;
1545 if(outfile=="-"):
1546 catfp.seek(0, 0);
1547 if(hasattr(sys.stdout, "buffer")):
1548 shutil.copyfileobj(catfp, sys.stdout.buffer);
1549 else:
1550 shutil.copyfileobj(catfp, sys.stdout);
1551 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
1552 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1553 catfp.seek(0, 0);
1554 upload_file_to_internet_file(catfp, outfile);
1555 if(returnfp):
1556 catfp.seek(0, 0);
1557 return catfp;
1558 else:
1559 catfp.close();
1560 return True;
1562 def AppendInFileWithContentToOutFile(infile, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, filevalues=[], extradata=[], followlink=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
1563 inlist = ReadInFileBySizeWithContentToList(infile, 0, 0, False, False, formatspecs);
1564 return AppendListsWithContentToOutFile(inlist, outfile, dirlistfromtxt, compression, compressionlevel, filevalues, extradata, followlink, checksumtype, formatspecs, verbose, returnfp);
1566 def PrintPermissionString(fchmode, ftype):
1567 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
1568 permissionstr = "";
1569 for fmodval in str(oct(fchmode))[-3:]:
1570 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
1571 if(ftype==0 or ftype==7):
1572 permissionstr = "-" + permissionstr;
1573 if(ftype==1):
1574 permissionstr = "h" + permissionstr;
1575 if(ftype==2):
1576 permissionstr = "l" + permissionstr;
1577 if(ftype==3):
1578 permissionstr = "c" + permissionstr;
1579 if(ftype==4):
1580 permissionstr = "b" + permissionstr;
1581 if(ftype==5):
1582 permissionstr = "d" + permissionstr;
1583 if(ftype==6):
1584 permissionstr = "f" + permissionstr;
1585 if(ftype==8):
1586 permissionstr = "D" + permissionstr;
1587 if(ftype==9):
1588 permissionstr = "p" + permissionstr;
1589 if(ftype==10):
1590 permissionstr = "w" + permissionstr;
1591 try:
1592 permissionoutstr = stat.filemode(fchmode);
1593 except AttributeError:
1594 permissionoutstr = permissionstr;
1595 except KeyError:
1596 permissionoutstr = permissionstr;
1597 return permissionoutstr;
1599 def PrintPermissionStringAlt(fchmode, ftype):
1600 permissions = {
1601 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1602 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1604 # Translate file mode into permission string
1605 permissionstr = ''.join([permissions[i] for i in str(oct(fchmode))[-3:]]);
1606 # Append file type indicator
1607 type_indicators = {
1608 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1609 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1611 file_type = type_indicators.get(ftype, '-');
1612 permissionstr = file_type + permissionstr;
1613 try:
1614 permissionoutstr = stat.filemode(fchmode);
1615 except AttributeError:
1616 permissionoutstr = permissionstr;
1617 return permissionoutstr;
1619 def CompressionSupport():
1620 compression_list = [];
1621 try:
1622 import gzip;
1623 compression_list.append("gz");
1624 compression_list.append("gzip");
1625 except ImportError:
1626 '''return False;'''
1627 try:
1628 import bz2;
1629 compression_list.append("bz2");
1630 compression_list.append("bzip2");
1631 except ImportError:
1632 '''return False;'''
1633 try:
1634 import lz4;
1635 compression_list.append("lz4");
1636 except ImportError:
1637 '''return False;'''
1638 try:
1639 import lzo;
1640 compression_list.append("lzo");
1641 compression_list.append("lzop");
1642 except ImportError:
1643 '''return False;'''
1644 try:
1645 import zstandard;
1646 compression_list.append("zstd");
1647 compression_list.append("zstandard");
1648 except ImportError:
1649 '''return False;'''
1650 try:
1651 import lzma;
1652 compression_list.append("lzma");
1653 compression_list.append("xz");
1654 except ImportError:
1655 '''return False;'''
1656 return compression_list;
1658 def CheckCompressionType(infile, formatspecs=__file_format_list__, closefp=True):
1659 if(hasattr(infile, "read") or hasattr(infile, "write")):
1660 catfp = infile;
1661 else:
1662 try:
1663 catfp = open(infile, "rb");
1664 except FileNotFoundError:
1665 return False;
1666 catfp.seek(0, 0);
1667 prefp = catfp.read(2);
1668 filetype = False;
1669 if(prefp==binascii.unhexlify("1f8b")):
1670 filetype = "gzip";
1671 catfp.seek(0, 0);
1672 prefp = catfp.read(3);
1673 if(prefp==binascii.unhexlify("425a68")):
1674 filetype = "bzip2";
1675 if(prefp==binascii.unhexlify("5d0000")):
1676 filetype = "lzma";
1677 catfp.seek(0, 0);
1678 prefp = catfp.read(4);
1679 if(prefp==binascii.unhexlify("28b52ffd")):
1680 filetype = "zstd";
1681 if(prefp==binascii.unhexlify("04224d18")):
1682 filetype = "lz4";
1683 if(prefp==binascii.unhexlify("504B0304")):
1684 filetype = "zipfile";
1685 catfp.seek(0, 0);
1686 prefp = catfp.read(5);
1687 if(prefp==binascii.unhexlify("7573746172")):
1688 filetype = "tarfile";
1689 catfp.seek(0, 0);
1690 prefp = catfp.read(6);
1691 if(prefp==binascii.unhexlify("fd377a585a00")):
1692 filetype = "lzma";
1693 if(prefp==binascii.unhexlify("377abcaf271c")):
1694 filetype = "7zipfile";
1695 catfp.seek(0, 0);
1696 prefp = catfp.read(7);
1697 if(prefp==binascii.unhexlify("526172211a0700")):
1698 filetype = "rarfile";
1699 if(prefp==binascii.unhexlify("43617446696c65")):
1700 filetype = "catfile";
1701 catfp.seek(0, 0);
1702 prefp = catfp.read(8);
1703 if(prefp==binascii.unhexlify("526172211a070100")):
1704 filetype = "rarfile";
1705 catfp.seek(0, 0);
1706 prefp = catfp.read(formatspecs[3]);
1707 if(prefp==binascii.unhexlify(formatspecs[4])):
1708 filetype = formatspecs[2];
1709 catfp.seek(0, 0);
1710 prefp = catfp.read(9);
1711 if(prefp==binascii.unhexlify("894c5a4f000d0a1a0a")):
1712 filetype = "lzo";
1713 catfp.seek(0, 0);
1714 prefp = catfp.read(10);
1715 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
1716 filetype = "tarfile";
1717 catfp.seek(0, 0);
1718 if(closefp):
1719 catfp.close();
1720 return filetype;
1722 def CheckCompressionTypeFromString(instring, formatspecs=__file_format_list__, closefp=True):
1723 try:
1724 instringsfile = BytesIO(instring);
1725 except TypeError:
1726 instringsfile = BytesIO(instring.encode("UTF-8"));
1727 return CheckCompressionType(instringsfile, formatspecs, closefp);
1729 def GetCompressionMimeType(infile, formatspecs=__file_format_list__):
1730 compresscheck = CheckCompressionType(fp, formatspecs, False);
1731 if(compresscheck=="gzip" or compresscheck=="gz"):
1732 return archivefile_gzip_mimetype;
1733 if(compresscheck=="bzip2" or compresscheck=="bz2"):
1734 return archivefile_bzip2_mimetype;
1735 if(compresscheck=="zstd" or compresscheck=="zstandard"):
1736 return archivefile_zstandard_mimetype;
1737 if(compresscheck=="lz4"):
1738 return archivefile_lz4_mimetype;
1739 if(compresscheck=="lzo" or compresscheck=="lzop"):
1740 return archivefile_lzop_mimetype;
1741 if(compresscheck=="lzma"):
1742 return archivefile_lzma_mimetype;
1743 if(compresscheck=="xz"):
1744 return archivefile_xz_mimetype;
1745 if(compresscheck=="catfile" or compresscheck=="cat" or compresscheck==formatspecs[2]):
1746 return archivefile_cat_mimetype;
1747 if(not compresscheck):
1748 return False;
1749 return False;
1751 def UncompressArchiveFile(fp, formatspecs=__file_format_list__):
1752 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
1753 return False;
1754 compresscheck = CheckCompressionType(fp, formatspecs, False);
1755 if(compresscheck=="gzip"):
1756 try:
1757 import gzip;
1758 except ImportError:
1759 return False;
1760 catfp = gzip.GzipFile(fileobj=fp, mode="rb");
1761 if(compresscheck=="bzip2"):
1762 try:
1763 import bz2;
1764 except ImportError:
1765 return False;
1766 catfp = BytesIO();
1767 catfp.write(bz2.decompress(fp.read()));
1768 if(compresscheck=="zstd"):
1769 try:
1770 import zstandard;
1771 except ImportError:
1772 return False;
1773 catfp = BytesIO();
1774 catfp.write(zstandard.decompress(fp.read()));
1775 if(compresscheck=="lz4"):
1776 try:
1777 import lz4.frame;
1778 except ImportError:
1779 return False;
1780 catfp = BytesIO();
1781 catfp.write(lz4.frame.decompress(fp.read()));
1782 if(compresscheck=="lzo" or compresscheck=="lzop"):
1783 try:
1784 import lzo;
1785 except ImportError:
1786 return False;
1787 catfp = BytesIO();
1788 catfp.write(lzo.decompress(fp.read()));
1789 if(compresscheck=="lzma" or compresscheck=="xz"):
1790 try:
1791 import lzma;
1792 except ImportError:
1793 return False;
1794 catfp = BytesIO();
1795 catfp.write(lzma.decompress(fp.read()));
1796 if(compresscheck=="catfile" or compresscheck==formatspecs[2]):
1797 catfp = fp;
1798 if(not compresscheck):
1799 try:
1800 import lzma;
1801 except ImportError:
1802 return False;
1803 catfp = BytesIO();
1804 with fp as fpcontent:
1805 try:
1806 catfp.write(lzma.decompress(fp.read()));
1807 except lzma.LZMAError:
1808 return False;
1809 return catfp;
1811 create_alias_function("Uncompress", __file_format_name__, "", UncompressArchiveFile);
1813 def UncompressFile(infile, formatspecs=__file_format_list__, mode="rb"):
1814 compresscheck = CheckCompressionType(infile, formatspecs, False);
1815 if(sys.version_info[0]==2 and compresscheck):
1816 if(mode=="rt"):
1817 mode = "r";
1818 if(mode=="wt"):
1819 mode = "w";
1820 try:
1821 if(compresscheck=="gzip"):
1822 try:
1823 import gzip;
1824 except ImportError:
1825 return False;
1826 try:
1827 filefp = gzip.open(infile, mode, encoding="UTF-8");
1828 except (ValueError, TypeError) as e:
1829 filefp = gzip.open(infile, mode);
1830 if(compresscheck=="bzip2"):
1831 try:
1832 import bz2;
1833 except ImportError:
1834 return False;
1835 try:
1836 filefp = bz2.open(infile, mode, encoding="UTF-8");
1837 except (ValueError, TypeError) as e:
1838 filefp = bz2.open(infile, mode);
1839 if(compresscheck=="zstd"):
1840 try:
1841 import zstandard;
1842 except ImportError:
1843 return False;
1844 try:
1845 filefp = zstandard.open(infile, mode, encoding="UTF-8");
1846 except (ValueError, TypeError) as e:
1847 filefp = zstandard.open(infile, mode);
1848 if(compresscheck=="lz4"):
1849 try:
1850 import lz4.frame;
1851 except ImportError:
1852 return False;
1853 try:
1854 filefp = lz4.frame.open(infile, mode, encoding="UTF-8");
1855 except (ValueError, TypeError) as e:
1856 filefp = lz4.frame.open(infile, mode);
1857 if(compresscheck=="lzo"):
1858 try:
1859 import lzo;
1860 except ImportError:
1861 return False;
1862 try:
1863 filefp = lzo.open(infile, mode, encoding="UTF-8");
1864 except (ValueError, TypeError) as e:
1865 filefp = lzo.open(infile, mode);
1866 if(compresscheck=="lzma"):
1867 try:
1868 import lzma;
1869 except ImportError:
1870 return False;
1871 try:
1872 filefp = lzma.open(infile, mode, encoding="UTF-8");
1873 except (ValueError, TypeError) as e:
1874 filefp = lzma.open(infile, mode);
1875 if(compresscheck=="catfile" or compresscheck==formatspecs[2]):
1876 try:
1877 filefp = open(infile, mode, encoding="UTF-8");
1878 except (ValueError, TypeError) as e:
1879 filefp = open(infile, mode);
1880 if(not compresscheck):
1881 try:
1882 filefp = open(infile, mode, encoding="UTF-8");
1883 except (ValueError, TypeError) as e:
1884 filefp = open(infile, mode);
1885 except FileNotFoundError:
1886 return False;
1887 return filefp;
1889 def UncompressString(infile):
1890 compresscheck = CheckCompressionTypeFromString(infile, formatspecs, False);
1891 if(compresscheck=="gzip"):
1892 try:
1893 import gzip;
1894 except ImportError:
1895 return False;
1896 fileuz = gzip.decompress(infile);
1897 if(compresscheck=="bzip2"):
1898 try:
1899 import bz2;
1900 except ImportError:
1901 return False;
1902 fileuz = bz2.decompress(infile);
1903 if(compresscheck=="zstd"):
1904 try:
1905 import zstandard;
1906 except ImportError:
1907 return False;
1908 fileuz = zstandard.decompress(infile);
1909 if(compresscheck=="lz4"):
1910 try:
1911 import lz4.frame;
1912 except ImportError:
1913 return False;
1914 fileuz = lz4.frame.decompress(infile);
1915 if(compresscheck=="lzo"):
1916 try:
1917 import lzo;
1918 except ImportError:
1919 return False;
1920 fileuz = lzo.decompress(infile);
1921 if(compresscheck=="lzma"):
1922 try:
1923 import lzma;
1924 except ImportError:
1925 return False;
1926 fileuz = lzma.decompress(infile);
1927 if(not compresscheck):
1928 fileuz = infile;
1929 if(hasattr(fileuz, 'decode')):
1930 fileuz = fileuz.decode("UTF-8");
1931 return fileuz;
1933 def UncompressStringAlt(infile):
1934 filefp = StringIO();
1935 outstring = UncompressString(infile);
1936 filefp.write(outstring);
1937 filefp.seek(0, 0);
1938 return filefp;
1940 def CheckCompressionSubType(infile, formatspecs=__file_format_list__, closefp=True):
1941 compresscheck = CheckCompressionType(infile, formatspecs, False);
1942 if(not compresscheck):
1943 fextname = os.path.splitext(infile)[1];
1944 if(fextname==".gz"):
1945 compresscheck = "gzip";
1946 elif(fextname==".bz2"):
1947 compresscheck = "bzip2";
1948 elif(fextname==".zst"):
1949 compresscheck = "zstd";
1950 elif(fextname==".lz4"):
1951 compresscheck = "lz4";
1952 elif(fextname==".lzo" or fextname==".lzop"):
1953 compresscheck = "lzo";
1954 elif(fextname==".lzma" or fextname==".xz"):
1955 compresscheck = "lzma";
1956 else:
1957 return False;
1958 if(not compresscheck):
1959 return False;
1960 if(compresscheck=="catfile"):
1961 return "catfile";
1962 if(compresscheck==formatspecs[2]):
1963 return formatspecs[2];
1964 if(compresscheck=="tarfile"):
1965 return "tarfile";
1966 if(compresscheck=="zipfile"):
1967 return "zipfile";
1968 if(rarfile_support and compresscheck=="rarfile"):
1969 return "rarfile";
1970 if(py7zr_support and compresscheck=="7zipfile"):
1971 return "7zipfile";
1972 if(hasattr(infile, "read") or hasattr(infile, "write")):
1973 catfp = UncompressArchiveFile(infile, formatspecs[2]);
1974 else:
1975 try:
1976 if(compresscheck=="gzip"):
1977 try:
1978 import gzip;
1979 except ImportError:
1980 return False;
1981 catfp = gzip.GzipFile(infile, "rb");
1982 if(compresscheck=="bzip2"):
1983 try:
1984 import bz2;
1985 except ImportError:
1986 return False;
1987 catfp = bz2.BZ2File(infile, "rb");
1988 if(compresscheck=="lz4"):
1989 try:
1990 import lz4.frame;
1991 except ImportError:
1992 return False;
1993 catfp = lz4.frame.open(infile, "rb");
1994 if(compresscheck=="zstd"):
1995 try:
1996 import zstandard;
1997 except ImportError:
1998 return False;
1999 catfp = zstandard.open(infile, "rb");
2000 if(compresscheck=="lzma" or compresscheck=="xz"):
2001 try:
2002 import lzma;
2003 except ImportError:
2004 return False;
2005 catfp = lzma.open(infile, "rb");
2006 except FileNotFoundError:
2007 return False;
2008 filetype = False;
2009 prefp = catfp.read(5);
2010 if(prefp==binascii.unhexlify("7573746172")):
2011 filetype = "tarfile";
2012 catfp.seek(0, 0);
2013 prefp = catfp.read(7);
2014 if(prefp==binascii.unhexlify("43617446696c65")):
2015 filetype = "catfile";
2016 catfp.seek(0, 0);
2017 prefp = catfp.read(formatspecs[3]);
2018 if(prefp==binascii.unhexlify(formatspecs[4])):
2019 filetype = formatspecs[2];
2020 catfp.seek(0, 0);
2021 prefp = catfp.read(10);
2022 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
2023 filetype = "tarfile";
2024 catfp.seek(0, 0);
2025 if(closefp):
2026 catfp.close();
2027 return filetype;
2029 def GZipCompress(data, compresslevel=9):
2030 try:
2031 import gzip;
2032 except ImportError:
2033 return False;
2034 tmpfp = tempfile.NamedTemporaryFile("wb", delete=False);
2035 tmpfp.close();
2036 tmpfp = gzip.GzipFile(tmpfp.name, mode="wb", compresslevel=compresslevel);
2037 tmpfp.write(data);
2038 tmpfp.close();
2039 try:
2040 catfp = open(tmpfp.name, "rb");
2041 except FileNotFoundError:
2042 return False;
2043 catdata = catfp.read();
2044 catfp.close();
2045 return catdata;
2047 def CompressArchiveFile(fp, compression="auto", compressionlevel=None, formatspecs=__file_format_list__):
2048 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
2049 return False;
2050 fp.seek(0, 0);
2051 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2052 compression = None;
2053 if(compression not in compressionlist and compression is None):
2054 compression = "auto";
2055 if(compression=="gzip"):
2056 try:
2057 import gzip;
2058 except ImportError:
2059 return False;
2060 catfp = BytesIO();
2061 if(compressionlevel is None):
2062 compressionlevel = 9;
2063 else:
2064 compressionlevel = int(compressionlevel);
2065 catfp.write(gzip.compress(fp.read(), compresslevel=compressionlevel));
2066 if(compression=="bzip2"):
2067 try:
2068 import bz2;
2069 except ImportError:
2070 return False;
2071 catfp = BytesIO();
2072 if(compressionlevel is None):
2073 compressionlevel = 9;
2074 else:
2075 compressionlevel = int(compressionlevel);
2076 catfp.write(bz2.compress(fp.read(), compresslevel=compressionlevel));
2077 if(compression=="lz4"):
2078 try:
2079 import lz4.frame;
2080 except ImportError:
2081 return False;
2082 catfp = BytesIO();
2083 if(compressionlevel is None):
2084 compressionlevel = 9;
2085 else:
2086 compressionlevel = int(compressionlevel);
2087 catfp.write(lz4.frame.compress(fp.read(), compression_level=compressionlevel));
2088 if(compression=="lzo" or compression=="lzop"):
2089 try:
2090 import lzo;
2091 except ImportError:
2092 return False;
2093 catfp = BytesIO();
2094 if(compressionlevel is None):
2095 compressionlevel = 9;
2096 else:
2097 compressionlevel = int(compressionlevel);
2098 catfp.write(lzo.compress(fp.read(), compresslevel=compressionlevel));
2099 if(compression=="zstd"):
2100 try:
2101 import zstandard;
2102 except ImportError:
2103 return False;
2104 catfp = BytesIO();
2105 if(compressionlevel is None):
2106 compressionlevel = 10;
2107 else:
2108 compressionlevel = int(compressionlevel);
2109 catfp.write(zstandard.compress(fp.read(), level=compressionlevel));
2110 if(compression=="lzma"):
2111 try:
2112 import lzma;
2113 except ImportError:
2114 return False;
2115 catfp = BytesIO();
2116 if(compressionlevel is None):
2117 compressionlevel = 9;
2118 else:
2119 compressionlevel = int(compressionlevel);
2120 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]));
2121 if(compression=="xz"):
2122 try:
2123 import lzma;
2124 except ImportError:
2125 return False;
2126 catfp = BytesIO();
2127 if(compressionlevel is None):
2128 compressionlevel = 9;
2129 else:
2130 compressionlevel = int(compressionlevel);
2131 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]));
2132 if(compression=="auto" or compression is None):
2133 catfp = fp;
2134 catfp.seek(0, 0);
2135 return catfp;
2137 create_alias_function("Compress", __file_format_name__, "", CompressArchiveFile);
2139 def CompressOpenFile(outfile, compressionlevel=None):
2140 if(outfile is None):
2141 return False;
2142 fbasename = os.path.splitext(outfile)[0];
2143 fextname = os.path.splitext(outfile)[1];
2144 if(compressionlevel is None and fextname!=".zst"):
2145 compressionlevel = 9;
2146 elif(compressionlevel is None and fextname==".zst"):
2147 compressionlevel = 10;
2148 else:
2149 compressionlevel = int(compressionlevel);
2150 if(sys.version_info[0]==2):
2151 mode = "w";
2152 else:
2153 mode = "wb";
2154 try:
2155 if(fextname not in outextlistwd):
2156 try:
2157 outfp = open(outfile, "wb", encoding="UTF-8");
2158 except (ValueError, TypeError) as e:
2159 outfp = open(outfile, "wb");
2160 elif(fextname==".gz"):
2161 try:
2162 import gzip;
2163 except ImportError:
2164 return False;
2165 try:
2166 outfp = gzip.open(outfile, mode, compressionlevel, encoding="UTF-8");
2167 except (ValueError, TypeError) as e:
2168 outfp = gzip.open(outfile, mode, compressionlevel);
2169 elif(fextname==".bz2"):
2170 try:
2171 import bz2;
2172 except ImportError:
2173 return False;
2174 try:
2175 outfp = bz2.open(outfile, mode, compressionlevel, encoding="UTF-8");
2176 except (ValueError, TypeError) as e:
2177 outfp = bz2.open(outfile, mode, compressionlevel);
2178 elif(fextname==".zst"):
2179 try:
2180 import zstandard;
2181 except ImportError:
2182 return False;
2183 try:
2184 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel), encoding="UTF-8");
2185 except (ValueError, TypeError) as e:
2186 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel));
2187 elif(fextname==".xz"):
2188 try:
2189 import lzma;
2190 except ImportError:
2191 return False;
2192 try:
2193 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}], encoding="UTF-8");
2194 except (ValueError, TypeError) as e:
2195 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]);
2196 elif(fextname==".lz4"):
2197 try:
2198 import lz4.frame;
2199 except ImportError:
2200 return False;
2201 try:
2202 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel, encoding="UTF-8");
2203 except (ValueError, TypeError) as e:
2204 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel);
2205 elif(fextname==".lzo"):
2206 try:
2207 import lzo;
2208 except ImportError:
2209 return False;
2210 try:
2211 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel, encoding="UTF-8");
2212 except (ValueError, TypeError) as e:
2213 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel);
2214 elif(fextname==".lzma"):
2215 try:
2216 import lzma;
2217 except ImportError:
2218 return False;
2219 try:
2220 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}], encoding="UTF-8");
2221 except (ValueError, TypeError) as e:
2222 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]);
2223 except FileNotFoundError:
2224 return False;
2225 return outfp;
2227 def GetDevMajorMinor(fdev):
2228 retdev = [];
2229 if(hasattr(os, "minor")):
2230 retdev.append(os.minor(fdev));
2231 else:
2232 retdev.append(0);
2233 if(hasattr(os, "major")):
2234 retdev.append(os.major(fdev));
2235 else:
2236 retdev.append(0);
2237 return retdev;
2239 def CheckSumSupport(checkfor, guaranteed=True):
2240 if(guaranteed):
2241 hash_list = sorted(list(hashlib.algorithms_guaranteed));
2242 else:
2243 hash_list = sorted(list(hashlib.algorithms_available));
2244 checklistout = sorted(hash_list + ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
2245 if(checkfor in checklistout):
2246 return True;
2247 else:
2248 return False;
2250 def CheckSumSupportAlt(checkfor, guaranteed=True):
2251 if(guaranteed):
2252 hash_list = sorted(list(hashlib.algorithms_guaranteed));
2253 else:
2254 hash_list = sorted(list(hashlib.algorithms_available));
2255 checklistout = hash_list;
2256 if(checkfor in checklistout):
2257 return True;
2258 else:
2259 return False;
2261 def PackArchiveFile(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2262 advancedlist = formatspecs[8];
2263 altinode = formatspecs[9];
2264 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2265 outfile = RemoveWindowsPath(outfile);
2266 checksumtype = checksumtype.lower();
2267 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2268 checksumtype="crc32";
2269 if(checksumtype=="none"):
2270 checksumtype = "";
2271 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2272 compression = None;
2273 if(compression not in compressionlist and compression is None):
2274 compression = "auto";
2275 if(verbose):
2276 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2277 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2278 if(os.path.exists(outfile)):
2279 os.unlink(outfile);
2280 if(outfile=="-"):
2281 verbose = False;
2282 catfp = BytesIO();
2283 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2284 catfp = outfile;
2285 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
2286 catfp = BytesIO();
2287 else:
2288 fbasename = os.path.splitext(outfile)[0];
2289 fextname = os.path.splitext(outfile)[1];
2290 catfp = CompressOpenFile(outfile, compressionlevel);
2291 catver = formatspecs[6];
2292 fileheaderver = str(int(catver.replace(".", "")));
2293 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2294 catfp.write(fileheader.encode('UTF-8'));
2295 infilelist = [];
2296 if(infiles=="-"):
2297 for line in sys.stdin:
2298 infilelist.append(line.strip());
2299 infilelist = list(filter(None, infilelist));
2300 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
2301 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
2302 return False;
2303 with UncompressFile(infiles, formatspecs, "r") as finfile:
2304 for line in finfile:
2305 infilelist.append(line.strip());
2306 infilelist = list(filter(None, infilelist));
2307 else:
2308 if(isinstance(infiles, (list, tuple, ))):
2309 infilelist = list(filter(None, infiles));
2310 elif(isinstance(infiles, (str, ))):
2311 infilelist = list(filter(None, [infiles]));
2312 if(advancedlist):
2313 GetDirList = ListDirAdvanced(infilelist, followlink, False);
2314 else:
2315 GetDirList = ListDir(infilelist, followlink, False);
2316 if(not GetDirList):
2317 return False;
2318 curinode = 0;
2319 curfid = 0;
2320 inodelist = [];
2321 inodetofile = {};
2322 filetoinode = {};
2323 inodetocatinode = {};
2324 numfiles = int(len(GetDirList));
2325 fnumfiles = format(int(len(GetDirList)), 'x').lower();
2326 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2327 if(checksumtype=="none" or checksumtype==""):
2328 catfileheadercshex = format(0, 'x').lower();
2329 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2330 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2331 elif(checksumtype=="crc16_ccitt"):
2332 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2333 elif(checksumtype=="adler32"):
2334 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2335 elif(checksumtype=="crc32"):
2336 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2337 elif(checksumtype=="crc64_ecma"):
2338 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2339 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2340 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2341 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2342 checksumoutstr = hashlib.new(checksumtype);
2343 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2344 catfileheadercshex = checksumoutstr.hexdigest().lower();
2345 else:
2346 catfileheadercshex = format(0, 'x').lower
2347 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2348 catfp.write(fnumfilesa.encode('UTF-8'));
2349 try:
2350 catfp.flush();
2351 os.fsync(catfp.fileno());
2352 except io.UnsupportedOperation:
2353 pass;
2354 except AttributeError:
2355 pass;
2356 for curfname in GetDirList:
2357 catfhstart = catfp.tell();
2358 if(re.findall("^[.|/]", curfname)):
2359 fname = curfname;
2360 else:
2361 fname = "./"+curfname;
2362 if(verbose):
2363 VerbosePrintOut(fname);
2364 if(not followlink or followlink is None):
2365 fstatinfo = os.lstat(fname);
2366 else:
2367 fstatinfo = os.stat(fname);
2368 fpremode = fstatinfo.st_mode;
2369 finode = fstatinfo.st_ino;
2370 flinkcount = fstatinfo.st_nlink;
2371 ftype = 0;
2372 if(stat.S_ISREG(fpremode)):
2373 ftype = 0;
2374 elif(stat.S_ISLNK(fpremode)):
2375 ftype = 2;
2376 elif(stat.S_ISCHR(fpremode)):
2377 ftype = 3;
2378 elif(stat.S_ISBLK(fpremode)):
2379 ftype = 4;
2380 elif(stat.S_ISDIR(fpremode)):
2381 ftype = 5;
2382 elif(stat.S_ISFIFO(fpremode)):
2383 ftype = 6;
2384 elif(stat.S_ISSOCK(fpremode)):
2385 ftype = 8;
2386 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
2387 ftype = 9;
2388 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
2389 ftype = 10;
2390 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
2391 ftype = 111;
2392 else:
2393 ftype = 0;
2394 flinkname = "";
2395 fcurfid = format(int(curfid), 'x').lower();
2396 if(not followlink and finode!=0):
2397 if(ftype!=1):
2398 if(finode in inodelist):
2399 ftype = 1;
2400 flinkname = inodetofile[finode];
2401 if(altinode):
2402 fcurinode = format(int(finode), 'x').lower();
2403 else:
2404 fcurinode = format(int(inodetocatinode[finode]), 'x').lower();
2405 if(finode not in inodelist):
2406 inodelist.append(finode);
2407 inodetofile.update({finode: fname});
2408 inodetocatinode.update({finode: curinode});
2409 if(altinode):
2410 fcurinode = format(int(finode), 'x').lower();
2411 else:
2412 fcurinode = format(int(curinode), 'x').lower();
2413 curinode = curinode + 1;
2414 else:
2415 fcurinode = format(int(curinode), 'x').lower();
2416 curinode = curinode + 1;
2417 curfid = curfid + 1;
2418 if(ftype==2):
2419 flinkname = os.readlink(fname);
2420 fdev = fstatinfo.st_dev;
2421 getfdev = GetDevMajorMinor(fdev);
2422 fdev_minor = getfdev[0];
2423 fdev_major = getfdev[1];
2424 frdev = fstatinfo.st_dev;
2425 if(hasattr(fstatinfo, "st_rdev")):
2426 frdev = fstatinfo.st_rdev;
2427 else:
2428 frdev = fstatinfo.st_dev;
2429 getfrdev = GetDevMajorMinor(frdev);
2430 frdev_minor = getfrdev[0];
2431 frdev_major = getfrdev[1];
2432 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
2433 fsize = format(int("0"), 'x').lower();
2434 elif(ftype==0 or ftype==7):
2435 fsize = format(int(fstatinfo.st_size), 'x').lower();
2436 else:
2437 fsize = format(int(fstatinfo.st_size)).lower();
2438 fatime = format(int(fstatinfo.st_atime), 'x').lower();
2439 fmtime = format(int(fstatinfo.st_mtime), 'x').lower();
2440 fctime = format(int(fstatinfo.st_ctime), 'x').lower();
2441 if(hasattr(fstatinfo, "st_birthtime")):
2442 fbtime = format(int(fstatinfo.st_birthtime), 'x').lower();
2443 else:
2444 fbtime = format(int(fstatinfo.st_ctime), 'x').lower();
2445 fmode = format(int(fstatinfo.st_mode), 'x').lower();
2446 fchmode = format(int(stat.S_IMODE(fstatinfo.st_mode)), 'x').lower();
2447 ftypemod = format(int(stat.S_IFMT(fstatinfo.st_mode)), 'x').lower();
2448 fuid = format(int(fstatinfo.st_uid), 'x').lower();
2449 fgid = format(int(fstatinfo.st_gid), 'x').lower();
2450 funame = "";
2451 try:
2452 import pwd;
2453 try:
2454 userinfo = pwd.getpwuid(fstatinfo.st_uid);
2455 funame = userinfo.pw_name;
2456 except KeyError:
2457 funame = "";
2458 except ImportError:
2459 funame = "";
2460 fgname = "";
2461 try:
2462 import grp;
2463 try:
2464 groupinfo = grp.getgrgid(fstatinfo.st_gid);
2465 fgname = groupinfo.gr_name;
2466 except KeyError:
2467 fgname = "";
2468 except ImportError:
2469 fgname = "";
2470 fdev_minor = format(int(fdev_minor), 'x').lower();
2471 fdev_major = format(int(fdev_major), 'x').lower();
2472 frdev_minor = format(int(frdev_minor), 'x').lower();
2473 frdev_major = format(int(frdev_major), 'x').lower();
2474 finode = format(int(finode), 'x').lower();
2475 flinkcount = format(int(flinkcount), 'x').lower();
2476 if(hasattr(fstatinfo, "st_file_attributes")):
2477 fwinattributes = format(int(fstatinfo.st_file_attributes), 'x').lower();
2478 else:
2479 fwinattributes = format(int(0), 'x').lower();
2480 fcontents = "".encode('UTF-8');
2481 chunk_size = 1024;
2482 if(ftype==0 or ftype==7):
2483 with open(fname, "rb") as fpc:
2484 while(True):
2485 chunk = fpc.read(chunk_size);
2486 if(not chunk):
2487 break
2488 fcontents += chunk;
2489 if(followlink and (ftype==1 or ftype==2)):
2490 flstatinfo = os.stat(flinkname);
2491 with open(flinkname, "rb") as fpc:
2492 while(True):
2493 chunk = fpc.read(chunk_size);
2494 if(not chunk):
2495 break;
2496 fcontents += chunk;
2497 ftypehex = format(ftype, 'x').lower();
2498 extrafields = format(len(extradata), 'x').lower();
2499 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2500 if(len(extradata)>0):
2501 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2502 extrasizelen = format(len(extrasizestr), 'x').lower();
2503 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields];
2504 catoutlen = len(catoutlist) + len(extradata) + 3;
2505 catoutlenhex = format(catoutlen, 'x').lower();
2506 catoutlist.insert(0, catoutlenhex);
2507 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
2508 if(len(extradata)>0):
2509 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2510 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2511 if(checksumtype=="none" or checksumtype==""):
2512 catfileheadercshex = format(0, 'x').lower();
2513 catfilecontentcshex = format(0, 'x').lower();
2514 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2515 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2516 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2517 elif(checksumtype=="crc16_ccitt"):
2518 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2519 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2520 elif(checksumtype=="adler32"):
2521 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2522 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2523 elif(checksumtype=="crc32"):
2524 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2525 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2526 elif(checksumtype=="crc64_ecma"):
2527 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2528 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2529 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2530 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2531 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2532 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2533 checksumoutstr = hashlib.new(checksumtype);
2534 checksumoutstr.update("".encode('UTF-8'));
2535 catfileheadercshex = checksumoutstr.hexdigest().lower();
2536 checksumoutstr = hashlib.new(checksumtype);
2537 checksumoutstr.update(fcontents);
2538 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2539 else:
2540 catfileheadercshex = format(0, 'x').lower();
2541 catfilecontentcshex = format(0, 'x').lower();
2542 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2543 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2544 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
2545 if(checksumtype=="none" or checksumtype==""):
2546 catfileheadercshex = format(0, 'x').lower();
2547 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2548 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2549 elif(checksumtype=="crc16_ccitt"):
2550 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2551 elif(checksumtype=="adler32"):
2552 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2553 elif(checksumtype=="crc32"):
2554 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2555 elif(checksumtype=="crc64_ecma"):
2556 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2557 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2558 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2559 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2560 checksumoutstr = hashlib.new(checksumtype);
2561 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
2562 catfileheadercshex = checksumoutstr.hexdigest().lower();
2563 else:
2564 catfileheadercshex = format(0, 'x').lower();
2565 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2566 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2567 catfcontentstart = catfp.tell() + len(catfileoutstr);
2568 catfileoutstrecd = catfileoutstr.encode('UTF-8');
2569 nullstrecd = formatspecs[5].encode('UTF-8');
2570 catfileout = catfileoutstrecd + fcontents + nullstrecd;
2571 catfcontentend = (catfp.tell() - 1) + len(catfileout);
2572 catfp.write(catfileout);
2573 try:
2574 catfp.flush();
2575 os.fsync(catfp.fileno());
2576 except io.UnsupportedOperation:
2577 pass;
2578 except AttributeError:
2579 pass;
2580 if(numfiles>0):
2581 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
2582 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2583 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2584 try:
2585 catfp.flush();
2586 os.fsync(catfp.fileno());
2587 except io.UnsupportedOperation:
2588 pass;
2589 except AttributeError:
2590 pass;
2591 if(outfile=="-"):
2592 catfp.seek(0, 0);
2593 if(hasattr(sys.stdout, "buffer")):
2594 shutil.copyfileobj(catfp, sys.stdout.buffer);
2595 else:
2596 shutil.copyfileobj(catfp, sys.stdout);
2597 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
2598 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2599 catfp.seek(0, 0);
2600 upload_file_to_internet_file(catfp, outfile);
2601 if(returnfp):
2602 catfp.seek(0, 0);
2603 return catfp;
2604 else:
2605 catfp.close();
2606 return True;
2608 create_alias_function("Pack", __file_format_name__, "", PackArchiveFile);
2610 if(hasattr(shutil, "register_archive_format")):
2611 def PackArchiveFileFunc(archive_name, source_dir, **kwargs):
2612 return PackArchiveFile(source_dir, archive_name, False, "auto", None, False, "crc32", [], __file_format_delimiter__, False, False);
2613 create_alias_function("Pack", __file_format_name__, "Func", PackArchiveFileFunc);
2615 def PackArchiveFileFromDirList(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2616 return PackArchiveFile(infiles, outfile, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, returnfp);
2618 def PackArchiveFileFromTarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2619 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2620 outfile = RemoveWindowsPath(outfile);
2621 checksumtype = checksumtype.lower();
2622 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2623 checksumtype="crc32";
2624 if(checksumtype=="none"):
2625 checksumtype = "";
2626 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2627 compression = None;
2628 if(compression not in compressionlist and compression is None):
2629 compression = "auto";
2630 if(verbose):
2631 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2632 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2633 if(os.path.exists(outfile)):
2634 os.unlink(outfile);
2635 if(outfile=="-"):
2636 verbose = False;
2637 catfp = BytesIO();
2638 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2639 catfp = outfile;
2640 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
2641 catfp = BytesIO();
2642 else:
2643 fbasename = os.path.splitext(outfile)[0];
2644 fextname = os.path.splitext(outfile)[1];
2645 catfp = CompressOpenFile(outfile, compressionlevel);
2646 catver = formatspecs[6];
2647 fileheaderver = str(int(catver.replace(".", "")));
2648 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2649 catfp.write(fileheader.encode('UTF-8'));
2650 curinode = 0;
2651 curfid = 0;
2652 inodelist = [];
2653 inodetofile = {};
2654 filetoinode = {};
2655 inodetocatinode = {};
2656 if(infile=="-"):
2657 infile = BytesIO();
2658 if(hasattr(sys.stdin, "buffer")):
2659 shutil.copyfileobj(sys.stdin.buffer, infile);
2660 else:
2661 shutil.copyfileobj(sys.stdin, infile);
2662 infile.seek(0, 0);
2663 if(not infile):
2664 return False;
2665 infile.seek(0, 0);
2666 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
2667 infile = download_file_from_internet_file(infile);
2668 infile.seek(0, 0);
2669 if(not infile):
2670 return False;
2671 infile.seek(0, 0);
2672 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
2673 return False;
2674 elif(os.path.exists(infile) and os.path.isfile(infile)):
2675 try:
2676 if(not tarfile.is_tarfile(infile)):
2677 return False;
2678 except AttributeError:
2679 if(not is_tarfile(infile)):
2680 return False;
2681 else:
2682 return False;
2683 try:
2684 if(hasattr(infile, "read") or hasattr(infile, "write")):
2685 tarfp = tarfile.open(fileobj=infile, mode="r");
2686 else:
2687 tarfp = tarfile.open(infile, "r");
2688 except FileNotFoundError:
2689 return False;
2690 numfiles = int(len(tarfp.getmembers()));
2691 fnumfiles = format(int(len(tarfp.getmembers())), 'x').lower();
2692 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2693 if(checksumtype=="none" or checksumtype==""):
2694 catfileheadercshex = format(0, 'x').lower();
2695 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2696 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2697 elif(checksumtype=="crc16_ccitt"):
2698 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2699 elif(checksumtype=="adler32"):
2700 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2701 elif(checksumtype=="crc32"):
2702 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2703 elif(checksumtype=="crc64_ecma"):
2704 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2705 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2706 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2707 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2708 checksumoutstr = hashlib.new(checksumtype);
2709 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2710 catfileheadercshex = checksumoutstr.hexdigest().lower();
2711 else:
2712 catfileheadercshex = format(0, 'x').lower();
2713 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2714 catfp.write(fnumfilesa.encode('UTF-8'));
2715 try:
2716 catfp.flush();
2717 os.fsync(catfp.fileno());
2718 except io.UnsupportedOperation:
2719 pass;
2720 except AttributeError:
2721 pass;
2722 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
2723 catfhstart = catfp.tell();
2724 if(re.findall("^[.|/]", member.name)):
2725 fname = member.name;
2726 else:
2727 fname = "./"+member.name;
2728 if(verbose):
2729 VerbosePrintOut(fname);
2730 fpremode = member.mode;
2731 ffullmode = member.mode;
2732 flinkcount = 0;
2733 ftype = 0;
2734 if(member.isreg()):
2735 ffullmode = member.mode + stat.S_IFREG;
2736 ftype = 0;
2737 elif(member.isdev()):
2738 ffullmode = member.mode;
2739 ftype = 7;
2740 elif(member.islnk()):
2741 ffullmode = member.mode + stat.S_IFREG;
2742 ftype = 1;
2743 elif(member.issym()):
2744 ffullmode = member.mode + stat.S_IFLNK;
2745 ftype = 2;
2746 elif(member.ischr()):
2747 ffullmode = member.mode + stat.S_IFCHR;
2748 ftype = 3;
2749 elif(member.isblk()):
2750 ffullmode = member.mode + stat.S_IFBLK;
2751 ftype = 4;
2752 elif(member.isdir()):
2753 ffullmode = member.mode + stat.S_IFDIR;
2754 ftype = 5;
2755 elif(member.isfifo()):
2756 ffullmode = member.mode + stat.S_IFIFO;
2757 ftype = 6;
2758 elif(member.issparse()):
2759 ffullmode = member.mode;
2760 ftype = 12;
2761 else:
2762 ffullmode = member.mode;
2763 ftype = 0;
2764 flinkname = "";
2765 fcurfid = format(int(curfid), 'x').lower();
2766 fcurinode = format(int(curfid), 'x').lower();
2767 curfid = curfid + 1;
2768 if(ftype==2):
2769 flinkname = member.linkname;
2770 fdev_minor = format(int(member.devminor), 'x').lower();
2771 fdev_major = format(int(member.devmajor), 'x').lower();
2772 frdev_minor = format(int(member.devminor), 'x').lower();
2773 frdev_major = format(int(member.devmajor), 'x').lower();
2774 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
2775 fsize = format(int("0"), 'x').lower();
2776 elif(ftype==0 or ftype==7):
2777 fsize = format(int(member.size), 'x').lower();
2778 else:
2779 fsize = format(int(member.size), 'x').lower();
2780 fatime = format(int(member.mtime), 'x').lower();
2781 fmtime = format(int(member.mtime), 'x').lower();
2782 fctime = format(int(member.mtime), 'x').lower();
2783 fbtime = format(int(member.mtime), 'x').lower();
2784 fmode = format(int(ffullmode), 'x').lower();
2785 fchmode = format(int(stat.S_IMODE(ffullmode)), 'x').lower();
2786 ftypemod = format(int(stat.S_IFMT(ffullmode)), 'x').lower();
2787 fuid = format(int(member.uid), 'x').lower();
2788 fgid = format(int(member.gid), 'x').lower();
2789 funame = member.uname;
2790 fgname = member.gname;
2791 flinkcount = format(int(flinkcount), 'x').lower();
2792 fwinattributes = format(int(0), 'x').lower();
2793 fcontents = "".encode('UTF-8');
2794 chunk_size = 1024;
2795 if(ftype==0 or ftype==7):
2796 with tarfp.extractfile(member) as fpc:
2797 while(True):
2798 chunk = fpc.read(chunk_size);
2799 if(not chunk):
2800 break
2801 fcontents += chunk;
2802 ftypehex = format(ftype, 'x').lower();
2803 extrafields = format(len(extradata), 'x').lower();
2804 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2805 if(len(extradata)>0):
2806 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2807 extrasizelen = format(len(extrasizestr), 'x').lower();
2808 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields];
2809 catoutlen = len(catoutlist) + len(extradata) + 3;
2810 catoutlenhex = format(catoutlen, 'x').lower();
2811 catoutlist.insert(0, catoutlenhex);
2812 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
2813 if(len(extradata)>0):
2814 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2815 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2816 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2817 catfcontentstart = catfp.tell() + len(catfileoutstr);
2818 if(checksumtype=="none" or checksumtype==""):
2819 catfileheadercshex = format(0, 'x').lower();
2820 catfilecontentcshex = format(0, 'x').lower();
2821 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2822 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2823 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2824 elif(checksumtype=="crc16_ccitt"):
2825 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2826 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2827 elif(checksumtype=="adler32"):
2828 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2829 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2830 elif(checksumtype=="crc32"):
2831 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2832 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2833 elif(checksumtype=="crc64_ecma"):
2834 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2835 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2836 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2837 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2838 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2839 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2840 checksumoutstr = hashlib.new(checksumtype);
2841 checksumoutstr.update("".encode('UTF-8'));
2842 catfileheadercshex = checksumoutstr.hexdigest().lower();
2843 checksumoutstr = hashlib.new(checksumtype);
2844 checksumoutstr.update(fcontents);
2845 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2846 else:
2847 catfileheadercshex = format(0, 'x').lower();
2848 catfilecontentcshex = format(0, 'x').lower();
2849 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2850 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2851 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
2852 if(checksumtype=="none" or checksumtype==""):
2853 catfileheadercshex = format(0, 'x').lower();
2854 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2855 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2856 elif(checksumtype=="crc16_ccitt"):
2857 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2858 elif(checksumtype=="adler32"):
2859 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2860 elif(checksumtype=="crc32"):
2861 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2862 elif(checksumtype=="crc64_ecma"):
2863 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2864 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2865 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2866 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2867 checksumoutstr = hashlib.new(checksumtype);
2868 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
2869 catfileheadercshex = checksumoutstr.hexdigest().lower();
2870 else:
2871 catfileheadercshex = format(0, 'x').lower();
2872 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2873 catfileoutstrecd = catfileoutstr.encode('UTF-8');
2874 nullstrecd = formatspecs[5].encode('UTF-8');
2875 catfileout = catfileoutstrecd + fcontents + nullstrecd;
2876 catfcontentend = (catfp.tell() - 1) + len(catfileout);
2877 catfp.write(catfileout);
2878 try:
2879 catfp.flush();
2880 os.fsync(catfp.fileno());
2881 except io.UnsupportedOperation:
2882 pass;
2883 except AttributeError:
2884 pass;
2885 if(numfiles>0):
2886 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
2887 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2888 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2889 try:
2890 catfp.flush();
2891 os.fsync(catfp.fileno());
2892 except io.UnsupportedOperation:
2893 pass;
2894 except AttributeError:
2895 pass;
2896 if(outfile=="-"):
2897 catfp.seek(0, 0);
2898 if(hasattr(sys.stdout, "buffer")):
2899 shutil.copyfileobj(catfp, sys.stdout.buffer);
2900 else:
2901 shutil.copyfileobj(catfp, sys.stdout);
2902 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
2903 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2904 catfp.seek(0, 0);
2905 upload_file_from_internet_file(catfp, outfile);
2906 if(returnfp):
2907 catfp.seek(0, 0);
2908 return catfp;
2909 else:
2910 catfp.close();
2911 return True;
2913 create_alias_function("Pack", __file_format_name__, "FromTarFile", PackArchiveFileFromTarFile);
2915 def PackArchiveFileFromZipFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2916 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2917 outfile = RemoveWindowsPath(outfile);
2918 checksumtype = checksumtype.lower();
2919 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2920 checksumtype="crc32";
2921 if(checksumtype=="none"):
2922 checksumtype = "";
2923 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2924 compression = None;
2925 if(compression not in compressionlist and compression is None):
2926 compression = "auto";
2927 if(verbose):
2928 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2929 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2930 if(os.path.exists(outfile)):
2931 os.unlink(outfile);
2932 if(outfile=="-"):
2933 verbose = False;
2934 catfp = BytesIO();
2935 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2936 catfp = outfile;
2937 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
2938 catfp = BytesIO();
2939 else:
2940 fbasename = os.path.splitext(outfile)[0];
2941 fextname = os.path.splitext(outfile)[1];
2942 catfp = CompressOpenFile(outfile, compressionlevel);
2943 catver = formatspecs[6];
2944 fileheaderver = str(int(catver.replace(".", "")));
2945 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2946 catfp.write(fileheader.encode('UTF-8'));
2947 curinode = 0;
2948 curfid = 0;
2949 inodelist = [];
2950 inodetofile = {};
2951 filetoinode = {};
2952 inodetocatinode = {};
2953 if(infile=="-"):
2954 infile = BytesIO();
2955 if(hasattr(sys.stdin, "buffer")):
2956 shutil.copyfileobj(sys.stdin.buffer, infile);
2957 else:
2958 shutil.copyfileobj(sys.stdin, infile);
2959 infile.seek(0, 0);
2960 if(not infile):
2961 return False;
2962 infile.seek(0, 0);
2963 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
2964 infile = download_file_from_internet_file(infile);
2965 infile.seek(0, 0);
2966 if(not infile):
2967 return False;
2968 infile.seek(0, 0);
2969 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
2970 return False;
2971 else:
2972 return False;
2973 if(not zipfile.is_zipfile(infile)):
2974 return False;
2975 try:
2976 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
2977 except FileNotFoundError:
2978 return False;
2979 ziptest = zipfp.testzip();
2980 if(ziptest):
2981 VerbosePrintOut("Bad file found!");
2982 numfiles = int(len(zipfp.infolist()));
2983 fnumfiles = format(int(len(zipfp.infolist())), 'x').lower();
2984 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2985 if(checksumtype=="none" or checksumtype==""):
2986 catfileheadercshex = format(0, 'x').lower();
2987 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2988 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2989 elif(checksumtype=="crc16_ccitt"):
2990 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2991 elif(checksumtype=="adler32"):
2992 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2993 elif(checksumtype=="crc32"):
2994 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2995 elif(checksumtype=="crc64_ecma"):
2996 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2997 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2998 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2999 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3000 checksumoutstr = hashlib.new(checksumtype);
3001 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
3002 catfileheadercshex = checksumoutstr.hexdigest().lower();
3003 else:
3004 catfileheadercshex = format(0, 'x').lower();
3005 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
3006 catfp.write(fnumfilesa.encode('UTF-8'));
3007 try:
3008 catfp.flush();
3009 os.fsync(catfp.fileno());
3010 except io.UnsupportedOperation:
3011 pass;
3012 except AttributeError:
3013 pass;
3014 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
3015 catfhstart = catfp.tell();
3016 if(re.findall("^[.|/]", member.filename)):
3017 fname = member.filename;
3018 else:
3019 fname = "./"+member.filename;
3020 zipinfo = zipfp.getinfo(member.filename);
3021 if(verbose):
3022 VerbosePrintOut(fname);
3023 if(not member.is_dir()):
3024 fpremode = int(stat.S_IFREG + 438);
3025 elif(member.is_dir()):
3026 fpremode = int(stat.S_IFDIR + 511);
3027 flinkcount = 0;
3028 ftype = 0;
3029 if(not member.is_dir()):
3030 ftype = 0;
3031 elif(member.is_dir()):
3032 ftype = 5;
3033 flinkname = "";
3034 fcurfid = format(int(curfid), 'x').lower();
3035 fcurinode = format(int(curfid), 'x').lower();
3036 curfid = curfid + 1;
3037 fdev_minor = format(int(0), 'x').lower();
3038 fdev_major = format(int(0), 'x').lower();
3039 frdev_minor = format(int(0), 'x').lower();
3040 frdev_major = format(int(0), 'x').lower();
3041 if(ftype==5):
3042 fsize = format(int("0"), 'x').lower();
3043 elif(ftype==0):
3044 fsize = format(int(member.file_size), 'x').lower();
3045 else:
3046 fsize = format(int(member.file_size), 'x').lower();
3047 fatime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
3048 fmtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
3049 fctime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
3050 fbtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
3051 if(zipinfo.create_system==0 or zipinfo.create_system==10):
3052 fwinattributes = format(int(zipinfo.external_attr), 'x').lower();
3053 if(not member.is_dir()):
3054 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
3055 fchmode = stat.S_IMODE(fmode);
3056 ftypemod = stat.S_IFMT(fmode);
3057 elif(member.is_dir()):
3058 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
3059 fchmode = stat.S_IMODE(fmode);
3060 ftypemod = stat.S_IFMT(fmode);
3061 elif(zipinfo.create_system==3):
3062 fwinattributes = format(int(0), 'x').lower();
3063 fmode = format(int(zipinfo.external_attr), 'x').lower();
3064 fchmode = stat.S_IMODE(fmode);
3065 ftypemod = stat.S_IFMT(fmode);
3066 else:
3067 fwinattributes = format(int(0), 'x').lower();
3068 if(not member.is_dir()):
3069 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
3070 fchmode = stat.S_IMODE(fmode);
3071 ftypemod = stat.S_IFMT(fmode);
3072 elif(member.is_dir()):
3073 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
3074 fchmode = stat.S_IMODE(fmode);
3075 ftypemod = stat.S_IFMT(fmode);
3076 try:
3077 fuid = format(int(os.getuid()), 'x').lower();
3078 except AttributeError:
3079 fuid = format(int(0), 'x').lower();
3080 except KeyError:
3081 fuid = format(int(0), 'x').lower();
3082 try:
3083 fgid = format(int(os.getgid()), 'x').lower();
3084 except AttributeError:
3085 fgid = format(int(0), 'x').lower();
3086 except KeyError:
3087 fgid = format(int(0), 'x').lower();
3088 try:
3089 import pwd;
3090 try:
3091 userinfo = pwd.getpwuid(os.getuid());
3092 funame = userinfo.pw_name;
3093 except KeyError:
3094 funame = "";
3095 except AttributeError:
3096 funame = "";
3097 except ImportError:
3098 funame = "";
3099 fgname = "";
3100 try:
3101 import grp;
3102 try:
3103 groupinfo = grp.getgrgid(os.getgid());
3104 fgname = groupinfo.gr_name;
3105 except KeyError:
3106 fgname = "";
3107 except AttributeError:
3108 fgname = "";
3109 except ImportError:
3110 fgname = "";
3111 fcontents = "".encode('UTF-8');
3112 if(ftype==0):
3113 fcontents = zipfp.read(member.filename);
3114 ftypehex = format(ftype, 'x').lower();
3115 extrafields = format(len(extradata), 'x').lower();
3116 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
3117 if(len(extradata)>0):
3118 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
3119 extrasizelen = format(len(extrasizestr), 'x').lower();
3120 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields];
3121 catoutlen = len(catoutlist) + len(extradata) + 3;
3122 catoutlenhex = format(catoutlen, 'x').lower();
3123 catoutlist.insert(0, catoutlenhex);
3124 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
3125 if(len(extradata)>0):
3126 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
3127 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
3128 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
3129 catfcontentstart = catfp.tell() + len(catfileoutstr);
3130 if(checksumtype=="none" or checksumtype==""):
3131 catfileheadercshex = format(0, 'x').lower();
3132 catfilecontentcshex = format(0, 'x').lower();
3133 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3134 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3135 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3136 elif(checksumtype=="crc16_ccitt"):
3137 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3138 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3139 elif(checksumtype=="adler32"):
3140 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3141 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3142 elif(checksumtype=="crc32"):
3143 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3144 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3145 elif(checksumtype=="crc64_ecma"):
3146 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3147 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3148 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3149 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3150 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3151 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3152 checksumoutstr = hashlib.new(checksumtype);
3153 checksumoutstr.update("".encode('UTF-8'));
3154 catfileheadercshex = checksumoutstr.hexdigest().lower();
3155 checksumoutstr = hashlib.new(checksumtype);
3156 checksumoutstr.update(fcontents);
3157 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3158 else:
3159 catfileheadercshex = format(0, 'x').lower();
3160 catfilecontentcshex = format(0, 'x').lower();
3161 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
3162 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3163 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
3164 if(checksumtype=="none" or checksumtype==""):
3165 catfileheadercshex = format(0, 'x').lower();
3166 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3167 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3168 elif(checksumtype=="crc16_ccitt"):
3169 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3170 elif(checksumtype=="adler32"):
3171 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3172 elif(checksumtype=="crc32"):
3173 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3174 elif(checksumtype=="crc64_ecma"):
3175 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3176 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3177 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3178 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3179 checksumoutstr = hashlib.new(checksumtype);
3180 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3181 catfileheadercshex = checksumoutstr.hexdigest().lower();
3182 else:
3183 catfileheadercshex = format(0, 'x').lower();
3184 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
3185 catfileoutstrecd = catfileoutstr.encode('UTF-8');
3186 nullstrecd = formatspecs[5].encode('UTF-8');
3187 catfileout = catfileoutstrecd + fcontents + nullstrecd;
3188 catfcontentend = (catfp.tell() - 1) + len(catfileout);
3189 catfp.write(catfileout);
3190 try:
3191 catfp.flush();
3192 os.fsync(catfp.fileno());
3193 except io.UnsupportedOperation:
3194 pass;
3195 except AttributeError:
3196 pass;
3197 if(numfiles>0):
3198 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
3199 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
3200 catfp = CompressArchiveFile(catfp, compression, formatspecs);
3201 try:
3202 catfp.flush();
3203 os.fsync(catfp.fileno());
3204 except io.UnsupportedOperation:
3205 pass;
3206 except AttributeError:
3207 pass;
3208 if(outfile=="-"):
3209 catfp.seek(0, 0);
3210 if(hasattr(sys.stdout, "buffer")):
3211 shutil.copyfileobj(catfp, sys.stdout.buffer);
3212 else:
3213 shutil.copyfileobj(catfp, sys.stdout);
3214 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
3215 catfp = CompressArchiveFile(catfp, compression, formatspecs);
3216 catfp.seek(0, 0);
3217 upload_file_to_internet_file(catfp, outfile);
3218 if(returnfp):
3219 catfp.seek(0, 0);
3220 return catfp;
3221 else:
3222 catfp.close();
3223 return True;
3225 create_alias_function("Pack", __file_format_name__, "FromZipFile", PackArchiveFileFromZipFile);
3227 if(not rarfile_support):
3228 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
3229 return False
3231 if(rarfile_support):
3232 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
3233 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
3234 outfile = RemoveWindowsPath(outfile);
3235 checksumtype = checksumtype.lower();
3236 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
3237 checksumtype="crc32";
3238 if(checksumtype=="none"):
3239 checksumtype = "";
3240 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
3241 compression = None;
3242 if(compression not in compressionlist and compression is None):
3243 compression = "auto";
3244 if(verbose):
3245 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
3246 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
3247 if(os.path.exists(outfile)):
3248 os.unlink(outfile);
3249 if(outfile=="-"):
3250 verbose = False;
3251 catfp = BytesIO();
3252 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
3253 catfp = outfile;
3254 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
3255 catfp = BytesIO();
3256 else:
3257 fbasename = os.path.splitext(outfile)[0];
3258 fextname = os.path.splitext(outfile)[1];
3259 catfp = CompressOpenFile(outfile, compressionlevel);
3260 catver = formatspecs[6];
3261 fileheaderver = str(int(catver.replace(".", "")));
3262 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
3263 catfp.write(fileheader.encode('UTF-8'));
3264 curinode = 0;
3265 curfid = 0;
3266 inodelist = [];
3267 inodetofile = {};
3268 filetoinode = {};
3269 inodetocatinode = {};
3270 if(not os.path.exists(infile) or not os.path.isfile(infile)):
3271 return False;
3272 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
3273 return False;
3274 rarfp = rarfile.RarFile(infile, "r");
3275 rartest = rarfp.testrar();
3276 if(rartest):
3277 VerbosePrintOut("Bad file found!");
3278 numfiles = int(len(rarfp.infolist()));
3279 fnumfiles = format(int(len(rarfp.infolist())), 'x').lower();
3280 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
3281 if(checksumtype=="none" or checksumtype==""):
3282 catfileheadercshex = format(0, 'x').lower();
3283 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3284 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
3285 elif(checksumtype=="crc16_ccitt"):
3286 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
3287 elif(checksumtype=="adler32"):
3288 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
3289 elif(checksumtype=="crc32"):
3290 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
3291 elif(checksumtype=="crc64_ecma"):
3292 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3293 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3294 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3295 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3296 checksumoutstr = hashlib.new(checksumtype);
3297 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
3298 catfileheadercshex = checksumoutstr.hexdigest().lower();
3299 else:
3300 catfileheadercshex = format(0, 'x').lower();
3301 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
3302 catfp.write(fnumfilesa.encode('UTF-8'));
3303 try:
3304 catfp.flush();
3305 os.fsync(catfp.fileno());
3306 except io.UnsupportedOperation:
3307 pass;
3308 except AttributeError:
3309 pass;
3310 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
3311 is_unix = False;
3312 is_windows = False;
3313 if(member.host_os==rarfile.RAR_OS_UNIX):
3314 is_windows = False;
3315 try:
3316 member.external_attr
3317 is_unix = True;
3318 except AttributeError:
3319 is_unix = False;
3320 elif(member.host_os==rarfile.RAR_OS_WIN32):
3321 is_unix = False;
3322 try:
3323 member.external_attr
3324 is_windows = True;
3325 except AttributeError:
3326 is_windows = False;
3327 else:
3328 is_unix = False;
3329 is_windows = False;
3330 catfhstart = catfp.tell();
3331 if(re.findall("^[.|/]", member.filename)):
3332 fname = member.filename;
3333 else:
3334 fname = "./"+member.filename;
3335 rarinfo = rarfp.getinfo(member.filename);
3336 if(verbose):
3337 VerbosePrintOut(fname);
3338 if(is_unix and member.external_attr !=0):
3339 fpremode = int(member.external_attr);
3340 elif(member.is_file()):
3341 fpremode = int(stat.S_IFREG + 438);
3342 elif(member.is_symlink()):
3343 fpremode = int(stat.S_IFLNK + 438);
3344 elif(member.is_dir()):
3345 fpremode = int(stat.S_IFDIR + 511);
3346 if(is_windows and member.external_attr !=0):
3347 fwinattributes = format(int(member.external_attr), 'x').lower();
3348 else:
3349 fwinattributes = format(int(0), 'x').lower();
3350 flinkcount = 0;
3351 ftype = 0;
3352 if(member.is_file()):
3353 ftype = 0;
3354 elif(member.is_symlink()):
3355 ftype = 2;
3356 elif(member.is_dir()):
3357 ftype = 5;
3358 flinkname = "";
3359 if(ftype==2):
3360 flinkname = rarfp.read(member.filename).decode("UTF-8");
3361 fcurfid = format(int(curfid), 'x').lower();
3362 fcurinode = format(int(curfid), 'x').lower();
3363 curfid = curfid + 1;
3364 fdev_minor = format(int(0), 'x').lower();
3365 fdev_major = format(int(0), 'x').lower();
3366 frdev_minor = format(int(0), 'x').lower();
3367 frdev_major = format(int(0), 'x').lower();
3368 if(ftype==5):
3369 fsize = format(int("0"), 'x').lower();
3370 elif(ftype==0):
3371 fsize = format(int(member.file_size), 'x').lower();
3372 else:
3373 fsize = format(int(member.file_size), 'x').lower();
3374 try:
3375 if(member.atime):
3376 fatime = format(int(member.atime.timestamp()), 'x').lower();
3377 else:
3378 fatime = format(int(member.mtime.timestamp()), 'x').lower();
3379 except AttributeError:
3380 fatime = format(int(member.mtime.timestamp()), 'x').lower();
3381 fmtime = format(int(member.mtime.timestamp()), 'x').lower();
3382 try:
3383 if(member.ctime):
3384 fctime = format(int(member.ctime.timestamp()), 'x').lower();
3385 else:
3386 fctime = format(int(member.mtime.timestamp()), 'x').lower();
3387 except AttributeError:
3388 fctime = format(int(member.mtime.timestamp()), 'x').lower();
3389 fbtime = format(int(member.mtime.timestamp()), 'x').lower();
3390 if(is_unix and member.external_attr !=0):
3391 fmode = format(int(member.external_attr), 'x').lower();
3392 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
3393 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
3394 elif(member.is_file()):
3395 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
3396 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
3397 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
3398 elif(member.is_symlink()):
3399 fmode = format(int(stat.S_IFLNK + 438), 'x').lower();
3400 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
3401 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
3402 elif(member.is_dir()):
3403 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
3404 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
3405 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
3406 try:
3407 fuid = format(int(os.getuid()), 'x').lower();
3408 except AttributeError:
3409 fuid = format(int(0), 'x').lower();
3410 except KeyError:
3411 fuid = format(int(0), 'x').lower();
3412 try:
3413 fgid = format(int(os.getgid()), 'x').lower();
3414 except AttributeError:
3415 fgid = format(int(0), 'x').lower();
3416 except KeyError:
3417 fgid = format(int(0), 'x').lower();
3418 try:
3419 import pwd;
3420 try:
3421 userinfo = pwd.getpwuid(os.getuid());
3422 funame = userinfo.pw_name;
3423 except KeyError:
3424 funame = "";
3425 except AttributeError:
3426 funame = "";
3427 except ImportError:
3428 funame = "";
3429 fgname = "";
3430 try:
3431 import grp;
3432 try:
3433 groupinfo = grp.getgrgid(os.getgid());
3434 fgname = groupinfo.gr_name;
3435 except KeyError:
3436 fgname = "";
3437 except AttributeError:
3438 fgname = "";
3439 except ImportError:
3440 fgname = "";
3441 fcontents = "".encode('UTF-8');
3442 if(ftype==0):
3443 fcontents = rarfp.read(member.filename);
3444 ftypehex = format(ftype, 'x').lower();
3445 extrafields = format(len(extradata), 'x').lower();
3446 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
3447 if(len(extradata)>0):
3448 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
3449 extrasizelen = format(len(extrasizestr), 'x').lower();
3450 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields];
3451 catoutlen = len(catoutlist) + len(extradata) + 3;
3452 catoutlenhex = format(catoutlen, 'x').lower();
3453 catoutlist.insert(0, catoutlenhex);
3454 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
3455 if(len(extradata)>0):
3456 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
3457 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
3458 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
3459 catfcontentstart = catfp.tell() + len(catfileoutstr);
3460 if(checksumtype=="none" or checksumtype==""):
3461 catfileheadercshex = format(0, 'x').lower();
3462 catfilecontentcshex = format(0, 'x').lower();
3463 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3464 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3465 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3466 elif(checksumtype=="crc16_ccitt"):
3467 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3468 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3469 elif(checksumtype=="adler32"):
3470 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3471 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3472 elif(checksumtype=="crc32"):
3473 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3474 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3475 elif(checksumtype=="crc64_ecma"):
3476 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3477 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3478 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3479 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3480 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3481 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3482 checksumoutstr = hashlib.new(checksumtype);
3483 checksumoutstr.update("".encode('UTF-8'));
3484 catfileheadercshex = checksumoutstr.hexdigest().lower();
3485 checksumoutstr = hashlib.new(checksumtype);
3486 checksumoutstr.update(fcontents);
3487 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3488 else:
3489 catfileheadercshex = format(0, 'x').lower();
3490 catfilecontentcshex = format(0, 'x').lower();
3491 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
3492 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3493 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr
3494 if(checksumtype=="none" or checksumtype==""):
3495 catfileheadercshex = format(0, 'x').lower()
3496 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3497 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
3498 elif(checksumtype=="crc16_ccitt"):
3499 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
3500 elif(checksumtype=="adler32"):
3501 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
3502 elif(checksumtype=="crc32"):
3503 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
3504 elif(checksumtype=="crc64_ecma"):
3505 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3506 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3507 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3508 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3509 checksumoutstr = hashlib.new(checksumtype)
3510 checksumoutstr.update(catfileoutstr.encode('UTF-8'))
3511 catfileheadercshex = checksumoutstr.hexdigest().lower()
3512 else:
3513 catfileheadercshex = format(0, 'x').lower()
3514 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5])
3515 catfileoutstrecd = catfileoutstr.encode('UTF-8')
3516 nullstrecd = formatspecs[5].encode('UTF-8')
3517 catfileout = catfileoutstrecd + fcontents + nullstrecd
3518 catfcontentend = (catfp.tell() - 1) + len(catfileout)
3519 catfp.write(catfileout)
3520 try:
3521 catfp.flush()
3522 os.fsync(catfp.fileno())
3523 except io.UnsupportedOperation:
3524 pass
3525 except AttributeError:
3526 pass
3527 if(numfiles>0):
3528 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
3529 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
3530 catfp = CompressArchiveFile(catfp, compression, formatspecs)
3531 try:
3532 catfp.flush()
3533 os.fsync(catfp.fileno())
3534 except io.UnsupportedOperation:
3535 pass
3536 except AttributeError:
3537 pass
3538 if(outfile=="-"):
3539 catfp.seek(0, 0)
3540 if(hasattr(sys.stdout, "buffer")):
3541 shutil.copyfileobj(catfp, sys.stdout.buffer);
3542 else:
3543 shutil.copyfileobj(catfp, sys.stdout);
3544 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
3545 catfp = CompressArchiveFile(catfp, compression, formatspecs);
3546 catfp.seek(0, 0);
3547 upload_file_to_internet_file(catfp, outfile);
3548 if(returnfp):
3549 catfp.seek(0, 0)
3550 return catfp
3551 else:
3552 catfp.close()
3553 return True;
3555 create_alias_function("Pack", __file_format_name__, "FromRarFile", PackArchiveFileFromRarFile);
3557 if(not py7zr_support):
3558 def PackArchiveFileFromSevenZipFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
3559 return False
3561 if(py7zr_support):
3562 def PackArchiveFileFromSevenZipFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
3563 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
3564 outfile = RemoveWindowsPath(outfile);
3565 checksumtype = checksumtype.lower();
3566 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
3567 checksumtype="crc32";
3568 if(checksumtype=="none"):
3569 checksumtype = "";
3570 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
3571 compression = None;
3572 if(compression not in compressionlist and compression is None):
3573 compression = "auto";
3574 if(verbose):
3575 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
3576 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
3577 if(os.path.exists(outfile)):
3578 os.unlink(outfile);
3579 if(outfile=="-"):
3580 verbose = False;
3581 catfp = BytesIO();
3582 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
3583 catfp = outfile;
3584 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
3585 catfp = BytesIO();
3586 else:
3587 fbasename = os.path.splitext(outfile)[0];
3588 fextname = os.path.splitext(outfile)[1];
3589 catfp = CompressOpenFile(outfile, compressionlevel);
3590 catver = formatspecs[6];
3591 fileheaderver = str(int(catver.replace(".", "")));
3592 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
3593 catfp.write(fileheader.encode('UTF-8'));
3594 curinode = 0;
3595 curfid = 0;
3596 inodelist = [];
3597 inodetofile = {};
3598 filetoinode = {};
3599 inodetocatinode = {};
3600 if(not os.path.exists(infile) or not os.path.isfile(infile)):
3601 return False;
3602 szpfp = py7zr.SevenZipFile(infile, mode="r");
3603 file_content = szpfp.readall();
3604 #sztest = szpfp.testzip();
3605 sztestalt = szpfp.test();
3606 if(sztestalt):
3607 VerbosePrintOut("Bad file found!");
3608 numfiles = int(len(szpfp.list()));
3609 fnumfiles = format(int(len(szpfp.list())), 'x').lower();
3610 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
3611 if(checksumtype=="none" or checksumtype==""):
3612 catfileheadercshex = format(0, 'x').lower();
3613 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3614 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
3615 elif(checksumtype=="crc16_ccitt"):
3616 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
3617 elif(checksumtype=="adler32"):
3618 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
3619 elif(checksumtype=="crc32"):
3620 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
3621 elif(checksumtype=="crc64_ecma"):
3622 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3623 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3624 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3625 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3626 checksumoutstr = hashlib.new(checksumtype);
3627 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
3628 catfileheadercshex = checksumoutstr.hexdigest().lower();
3629 else:
3630 catfileheadercshex = format(0, 'x').lower();
3631 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
3632 catfp.write(fnumfilesa.encode('UTF-8'));
3633 try:
3634 catfp.flush();
3635 os.fsync(catfp.fileno());
3636 except io.UnsupportedOperation:
3637 pass;
3638 except AttributeError:
3639 pass;
3640 for member in sorted(szpfp.list(), key=lambda x: x.filename):
3641 catfhstart = catfp.tell();
3642 if(re.findall("^[.|/]", member.filename)):
3643 fname = member.filename;
3644 else:
3645 fname = "./"+member.filename;
3646 if(verbose):
3647 VerbosePrintOut(fname);
3648 if(not member.is_directory):
3649 fpremode = int(stat.S_IFREG + 438);
3650 elif(member.is_directory):
3651 fpremode = int(stat.S_IFDIR + 511);
3652 fwinattributes = format(int(0), 'x').lower();
3653 flinkcount = 0;
3654 ftype = 0;
3655 if(member.is_directory):
3656 ftype = 5;
3657 else:
3658 ftype = 0;
3659 flinkname = "";
3660 fcurfid = format(int(curfid), 'x').lower();
3661 fcurinode = format(int(curfid), 'x').lower();
3662 curfid = curfid + 1;
3663 fdev_minor = format(int(0), 'x').lower();
3664 fdev_major = format(int(0), 'x').lower();
3665 frdev_minor = format(int(0), 'x').lower();
3666 frdev_major = format(int(0), 'x').lower();
3667 if(ftype==5):
3668 fsize = format(int("0"), 'x').lower();
3669 fatime = format(int(member.creationtime.timestamp()), 'x').lower();
3670 fmtime = format(int(member.creationtime.timestamp()), 'x').lower();
3671 fctime = format(int(member.creationtime.timestamp()), 'x').lower();
3672 fbtime = format(int(member.creationtime.timestamp()), 'x').lower();
3673 if(member.is_directory):
3674 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
3675 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
3676 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
3677 else:
3678 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
3679 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
3680 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
3681 try:
3682 fuid = format(int(os.getuid()), 'x').lower();
3683 except AttributeError:
3684 fuid = format(int(0), 'x').lower();
3685 except KeyError:
3686 fuid = format(int(0), 'x').lower();
3687 try:
3688 fgid = format(int(os.getgid()), 'x').lower();
3689 except AttributeError:
3690 fgid = format(int(0), 'x').lower();
3691 except KeyError:
3692 fgid = format(int(0), 'x').lower();
3693 try:
3694 import pwd;
3695 try:
3696 userinfo = pwd.getpwuid(os.getuid());
3697 funame = userinfo.pw_name;
3698 except KeyError:
3699 funame = "";
3700 except AttributeError:
3701 funame = "";
3702 except ImportError:
3703 funame = "";
3704 fgname = "";
3705 try:
3706 import grp;
3707 try:
3708 groupinfo = grp.getgrgid(os.getgid());
3709 fgname = groupinfo.gr_name;
3710 except KeyError:
3711 fgname = "";
3712 except AttributeError:
3713 fgname = "";
3714 except ImportError:
3715 fgname = "";
3716 fcontents = "".encode('UTF-8');
3717 if(ftype==0):
3718 fcontents = file_content[member.filename].read();
3719 fsize = format(len(fcontents), 'x').lower();
3720 file_content[member.filename].close();
3721 ftypehex = format(ftype, 'x').lower();
3722 extrafields = format(len(extradata), 'x').lower();
3723 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
3724 if(len(extradata)>0):
3725 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
3726 extrasizelen = format(len(extrasizestr), 'x').lower();
3727 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields];
3728 catoutlen = len(catoutlist) + len(extradata) + 3;
3729 catoutlenhex = format(catoutlen, 'x').lower();
3730 catoutlist.insert(0, catoutlenhex);
3731 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
3732 if(len(extradata)>0):
3733 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
3734 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
3735 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
3736 catfcontentstart = catfp.tell() + len(catfileoutstr);
3737 if(checksumtype=="none" or checksumtype==""):
3738 catfileheadercshex = format(0, 'x').lower();
3739 catfilecontentcshex = format(0, 'x').lower();
3740 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3741 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3742 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3743 elif(checksumtype=="crc16_ccitt"):
3744 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3745 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3746 elif(checksumtype=="adler32"):
3747 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3748 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3749 elif(checksumtype=="crc32"):
3750 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3751 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3752 elif(checksumtype=="crc64_ecma"):
3753 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3754 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3755 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3756 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3757 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3758 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3759 checksumoutstr = hashlib.new(checksumtype);
3760 checksumoutstr.update("".encode('UTF-8'));
3761 catfileheadercshex = checksumoutstr.hexdigest().lower();
3762 checksumoutstr = hashlib.new(checksumtype);
3763 checksumoutstr.update(fcontents);
3764 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3765 else:
3766 catfileheadercshex = format(0, 'x').lower();
3767 catfilecontentcshex = format(0, 'x').lower();
3768 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
3769 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3770 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr
3771 if(checksumtype=="none" or checksumtype==""):
3772 catfileheadercshex = format(0, 'x').lower()
3773 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3774 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
3775 elif(checksumtype=="crc16_ccitt"):
3776 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
3777 elif(checksumtype=="adler32"):
3778 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
3779 elif(checksumtype=="crc32"):
3780 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
3781 elif(checksumtype=="crc64_ecma"):
3782 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3783 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3784 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3785 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3786 checksumoutstr = hashlib.new(checksumtype)
3787 checksumoutstr.update(catfileoutstr.encode('UTF-8'))
3788 catfileheadercshex = checksumoutstr.hexdigest().lower()
3789 else:
3790 catfileheadercshex = format(0, 'x').lower()
3791 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5])
3792 catfileoutstrecd = catfileoutstr.encode('UTF-8')
3793 nullstrecd = formatspecs[5].encode('UTF-8')
3794 catfileout = catfileoutstrecd + fcontents + nullstrecd
3795 catfcontentend = (catfp.tell() - 1) + len(catfileout)
3796 catfp.write(catfileout)
3797 try:
3798 catfp.flush()
3799 os.fsync(catfp.fileno())
3800 except io.UnsupportedOperation:
3801 pass
3802 except AttributeError:
3803 pass
3804 if(numfiles>0):
3805 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
3806 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
3807 catfp = CompressArchiveFile(catfp, compression, formatspecs)
3808 try:
3809 catfp.flush()
3810 os.fsync(catfp.fileno())
3811 except io.UnsupportedOperation:
3812 pass
3813 except AttributeError:
3814 pass
3815 if(outfile=="-"):
3816 catfp.seek(0, 0)
3817 if(hasattr(sys.stdout, "buffer")):
3818 shutil.copyfileobj(catfp, sys.stdout.buffer);
3819 else:
3820 shutil.copyfileobj(catfp, sys.stdout);
3821 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
3822 catfp = CompressArchiveFile(catfp, compression, formatspecs);
3823 catfp.seek(0, 0);
3824 upload_file_to_internet_file(catfp, outfile);
3825 if(returnfp):
3826 catfp.seek(0, 0)
3827 return catfp
3828 else:
3829 catfp.close()
3830 return True;
3832 create_alias_function("Pack", __file_format_name__, "FromSevenZipFile", PackArchiveFileFromSevenZipFile);
3834 def ArchiveFileSeekToFileNum(infile, seekto=0, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3835 if(hasattr(infile, "read") or hasattr(infile, "write")):
3836 catfp = infile;
3837 catfp.seek(0, 0);
3838 catfp = UncompressArchiveFile(catfp, formatspecs);
3839 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
3840 if(checkcompressfile=="tarfile"):
3841 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3842 if(checkcompressfile=="zipfile"):
3843 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3844 if(rarfile_support and checkcompressfile=="rarfile"):
3845 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3846 if(py7zr_support and checkcompressfile=="7zipfile"):
3847 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3848 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3849 return False;
3850 if(not catfp):
3851 return False;
3852 catfp.seek(0, 0);
3853 elif(infile=="-"):
3854 catfp = BytesIO();
3855 if(hasattr(sys.stdin, "buffer")):
3856 shutil.copyfileobj(sys.stdin.buffer, catfp);
3857 else:
3858 shutil.copyfileobj(sys.stdin, catfp);
3859 catfp.seek(0, 0);
3860 catfp = UncompressArchiveFile(catfp, formatspecs);
3861 if(not catfp):
3862 return False;
3863 catfp.seek(0, 0);
3864 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
3865 catfp = download_file_from_internet_file(infile);
3866 catfp.seek(0, 0);
3867 catfp = UncompressArchiveFile(catfp, formatspecs);
3868 if(not catfp):
3869 return False;
3870 catfp.seek(0, 0);
3871 else:
3872 infile = RemoveWindowsPath(infile);
3873 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
3874 if(checkcompressfile=="tarfile"):
3875 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3876 if(checkcompressfile=="zipfile"):
3877 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3878 if(rarfile_support and checkcompressfile=="rarfile"):
3879 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3880 if(py7zr_support and checkcompressfile=="7zipfile"):
3881 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3882 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3883 return False;
3884 compresscheck = CheckCompressionType(infile, formatspecs, True);
3885 if(not compresscheck):
3886 fextname = os.path.splitext(infile)[1];
3887 if(fextname==".gz"):
3888 compresscheck = "gzip";
3889 elif(fextname==".bz2"):
3890 compresscheck = "bzip2";
3891 elif(fextname==".zst"):
3892 compresscheck = "zstd";
3893 elif(fextname==".lz4" or fextname==".clz4"):
3894 compresscheck = "lz4";
3895 elif(fextname==".lzo" or fextname==".lzop"):
3896 compresscheck = "lzo";
3897 elif(fextname==".lzma" or fextname==".xz"):
3898 compresscheck = "lzma";
3899 else:
3900 return False;
3901 if(not compresscheck):
3902 return False;
3903 catfp = UncompressFile(infile, formatspecs, "rb");
3905 try:
3906 catfp.seek(0, 2);
3907 except OSError:
3908 SeekToEndOfFile(catfp);
3909 except ValueError:
3910 SeekToEndOfFile(catfp);
3911 CatSize = catfp.tell();
3912 CatSizeEnd = CatSize;
3914 try:
3915 catfp.seek(0, 0);
3916 except OSError:
3917 return False;
3918 except ValueError:
3919 return False;
3920 curloc = catfp.tell();
3921 if(curloc>0):
3922 catfp.seek(0, 0);
3923 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3924 if(curloc>0):
3925 catfp.seek(curloc, 0);
3926 catstring = catheader[0];
3927 catversion = re.findall(r"([\d]+)$", catstring);
3928 fprenumfiles = catheader[1];
3929 fnumfiles = int(fprenumfiles, 16);
3930 fprechecksumtype = catheader[2];
3931 fprechecksum = catheader[3];
3932 fileheader = AppendNullByte(catstring, formatspecs[5]);
3933 fnumfileshex = format(int(fnumfiles), 'x').lower();
3934 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3935 if(fprechecksumtype=="none" or fprechecksumtype==""):
3936 catfileheadercshex = format(0, 'x').lower();
3937 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3938 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3939 elif(fprechecksumtype=="crc16_ccitt"):
3940 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3941 elif(fprechecksumtype=="adler32"):
3942 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3943 elif(fprechecksumtype=="crc32"):
3944 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3945 elif(fprechecksumtype=="crc64_ecma"):
3946 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3947 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3948 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3949 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3950 checksumoutstr = hashlib.new(fprechecksumtype);
3951 checksumoutstr.update(fileheader.encode('UTF-8'));
3952 catfileheadercshex = checksumoutstr.hexdigest().lower();
3953 else:
3954 catfileheadercshex = format(0, 'x').lower();
3955 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3956 fheadtell = len(fileheader);
3957 if(fprechecksum!=catfileheadercshex and not skipchecksum):
3958 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3959 return False;
3960 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3961 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
3962 if(seekto>=fnumfiles):
3963 seekto = fnumfiles - 1;
3964 if(seekto<0):
3965 seekto = 0;
3966 if(seekto>=0):
3967 il = -1;
3968 while(il < seekto):
3969 prefhstart = catfp.tell();
3970 if(formatspecs[7]):
3971 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3972 else:
3973 preheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
3974 if(len(preheaderdata)==0):
3975 break;
3976 prefheadsize = int(preheaderdata[0], 16);
3977 prefnumfields = int(preheaderdata[1], 16);
3978 preftype = int(preheaderdata[2], 16);
3979 if(re.findall("^[.|/]", preheaderdata[3])):
3980 prefname = preheaderdata[3];
3981 else:
3982 prefname = "./"+preheaderdata[3];
3983 prefbasedir = os.path.dirname(prefname);
3984 preflinkname = preheaderdata[4];
3985 prefsize = int(preheaderdata[5], 16);
3986 prefatime = int(preheaderdata[6], 16);
3987 prefmtime = int(preheaderdata[7], 16);
3988 prefctime = int(preheaderdata[8], 16);
3989 prefbtime = int(preheaderdata[9], 16);
3990 prefmode = int(preheaderdata[10], 16);
3991 prefchmode = stat.S_IMODE(prefmode);
3992 preftypemod = stat.S_IFMT(prefmode);
3993 prefwinattributes = int(preheaderdata[11], 16);
3994 prefuid = int(preheaderdata[12], 16);
3995 prefuname = preheaderdata[13];
3996 prefgid = int(preheaderdata[14], 16);
3997 prefgname = preheaderdata[15];
3998 fid = int(preheaderdata[16], 16);
3999 finode = int(preheaderdata[17], 16);
4000 flinkcount = int(preheaderdata[18], 16);
4001 prefdev_minor = int(preheaderdata[19], 16);
4002 prefdev_major = int(preheaderdata[20], 16);
4003 prefrdev_minor = int(preheaderdata[22], 16);
4004 prefrdev_major = int(preheaderdata[23], 16);
4005 prefextrasize = int(preheaderdata[24], 16);
4006 prefextrafields = int(preheaderdata[25], 16);
4007 extrafieldslist = [];
4008 extrastart = 25;
4009 extraend = extrastart + prefextrafields;
4010 extrafieldslist = [];
4011 if(extrastart<extraend):
4012 extrafieldslist.append(preheaderdata[extrastart]);
4013 extrastart = extrastart + 1;
4014 prefchecksumtype = preheaderdata[extrastart].lower();
4015 prefcs = preheaderdata[extrastart + 1].lower();
4016 prefccs = preheaderdata[extrastart + 2].lower();
4017 hc = 0;
4018 hcmax = len(preheaderdata) - 2;
4019 hout = "";
4020 while(hc<hcmax):
4021 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
4022 hc = hc + 1;
4023 prenewfcs = GetFileChecksum(hout, preheaderdata[-3].lower(), True, formatspecs);
4024 if(prefcs!=prenewfcs and not skipchecksum):
4025 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
4026 return False;
4027 valid_archive = False;
4028 invalid_archive = True;
4029 prefhend = catfp.tell() - 1;
4030 prefcontentstart = catfp.tell();
4031 prefcontents = "";
4032 pyhascontents = False;
4033 if(prefsize>0):
4034 prefcontents = catfp.read(prefsize);
4035 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
4036 pyhascontents = True;
4037 if(prefccs!=prenewfccs and not skipchecksum):
4038 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
4039 return False;
4040 catfp.seek(1, 1);
4041 il = il + 1;
4042 catfp.seek(seekstart, 0);
4043 fileidnum = il;
4044 catfheadsize = int(preheaderdata[0], 16);
4045 catfnumfields = int(preheaderdata[1], 16);
4046 catftype = int(preheaderdata[2], 16);
4047 if(re.findall("^[.|/]", preheaderdata[3])):
4048 catfname = preheaderdata[3];
4049 else:
4050 catfname = "./"+preheaderdata[3];
4051 catflinkname = preheaderdata[4];
4052 catfsize = int(preheaderdata[5], 16);
4053 catfbasedir = os.path.dirname(catfname);
4054 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
4055 if(returnfp):
4056 catlist.update({'catfp': catfp});
4057 else:
4058 catfp.close();
4059 return catlist;
4061 create_alias_function("", __file_format_name__, "SeekToFileNum", ArchiveFileSeekToFileNum);
4063 def ArchiveFileSeekToFileName(infile, seekfile=None, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4064 if(hasattr(infile, "read") or hasattr(infile, "write")):
4065 catfp = infile;
4066 catfp.seek(0, 0);
4067 catfp = UncompressArchiveFile(catfp, formatspecs);
4068 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
4069 if(checkcompressfile=="tarfile"):
4070 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4071 if(checkcompressfile=="zipfile"):
4072 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4073 if(rarfile_support and checkcompressfile=="rarfile"):
4074 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4075 if(py7zr_support and checkcompressfile=="7zipfile"):
4076 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4077 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4078 return False;
4079 if(not catfp):
4080 return False;
4081 catfp.seek(0, 0);
4082 elif(infile=="-"):
4083 catfp = BytesIO();
4084 if(hasattr(sys.stdin, "buffer")):
4085 shutil.copyfileobj(sys.stdin.buffer, catfp);
4086 else:
4087 shutil.copyfileobj(sys.stdin, catfp);
4088 catfp.seek(0, 0);
4089 catfp = UncompressArchiveFile(catfp, formatspecs);
4090 if(not catfp):
4091 return False;
4092 catfp.seek(0, 0);
4093 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
4094 catfp = download_file_from_internet_file(infile);
4095 catfp = UncompressArchiveFile(catfp, formatspecs);
4096 catfp.seek(0, 0);
4097 if(not catfp):
4098 return False;
4099 catfp.seek(0, 0);
4100 else:
4101 infile = RemoveWindowsPath(infile);
4102 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4103 if(checkcompressfile=="tarfile"):
4104 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4105 if(checkcompressfile=="zipfile"):
4106 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4107 if(rarfile_support and checkcompressfile=="rarfile"):
4108 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4109 if(py7zr_support and checkcompressfile=="7zipfile"):
4110 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4111 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4112 return False;
4113 compresscheck = CheckCompressionType(infile, formatspecs, True);
4114 if(not compresscheck):
4115 fextname = os.path.splitext(infile)[1];
4116 if(fextname==".gz"):
4117 compresscheck = "gzip";
4118 elif(fextname==".bz2"):
4119 compresscheck = "bzip2";
4120 elif(fextname==".zst"):
4121 compresscheck = "zstd";
4122 elif(fextname==".lz4" or fextname==".clz4"):
4123 compresscheck = "lz4";
4124 elif(fextname==".lzo" or fextname==".lzop"):
4125 compresscheck = "lzo";
4126 elif(fextname==".lzma" or fextname==".xz"):
4127 compresscheck = "lzma";
4128 else:
4129 return False;
4130 if(not compresscheck):
4131 return False;
4132 catfp = UncompressFile(infile, formatspecs, "rb");
4134 try:
4135 catfp.seek(0, 2);
4136 except OSError:
4137 SeekToEndOfFile(catfp);
4138 except ValueError:
4139 SeekToEndOfFile(catfp);
4140 CatSize = catfp.tell();
4141 CatSizeEnd = CatSize;
4143 try:
4144 catfp.seek(0, 0);
4145 except OSError:
4146 return False;
4147 except ValueError:
4148 return False;
4149 curloc = catfp.tell();
4150 if(curloc>0):
4151 catfp.seek(0, 0);
4152 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
4153 if(curloc>0):
4154 catfp.seek(curloc, 0);
4155 catstring = catheader[0];
4156 catversion = re.findall(r"([\d]+)$", catstring);
4157 fprenumfiles = catheader[1];
4158 fnumfiles = int(fprenumfiles, 16);
4159 fprechecksumtype = catheader[2];
4160 fprechecksum = catheader[3];
4161 fileheader = AppendNullByte(catstring, formatspecs[5]);
4162 fnumfileshex = format(int(fnumfiles), 'x').lower();
4163 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
4164 if(fprechecksumtype=="none" or fprechecksumtype==""):
4165 catfileheadercshex = format(0, 'x').lower();
4166 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
4167 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4168 elif(fprechecksumtype=="crc16_ccitt"):
4169 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4170 elif(fprechecksumtype=="adler32"):
4171 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4172 elif(fprechecksumtype=="crc32"):
4173 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4174 elif(fprechecksumtype=="crc64_ecma"):
4175 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4176 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
4177 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4178 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
4179 checksumoutstr = hashlib.new(fprechecksumtype);
4180 checksumoutstr.update(fileheader.encode('UTF-8'));
4181 catfileheadercshex = checksumoutstr.hexdigest().lower();
4182 else:
4183 catfileheadercshex = format(0, 'x').lower();
4184 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4185 fheadtell = len(fileheader);
4186 if(fprechecksum!=catfileheadercshex and not skipchecksum):
4187 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
4188 return False;
4189 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4190 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
4191 seekto = fnumfiles - 1
4192 filefound = False;
4193 if(seekto>=0):
4194 il = -1;
4195 while(il < seekto):
4196 prefhstart = catfp.tell();
4197 if(formatspecs[7]):
4198 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4199 else:
4200 preheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4201 if(len(preheaderdata)==0):
4202 break;
4203 prefheadsize = int(preheaderdata[0], 16);
4204 prefnumfields = int(preheaderdata[1], 16);
4205 preftype = int(preheaderdata[2], 16);
4206 if(re.findall("^[.|/]", preheaderdata[3])):
4207 prefname = preheaderdata[3];
4208 else:
4209 prefname = "./"+preheaderdata[3];
4210 prefbasedir = os.path.dirname(prefname);
4211 preflinkname = preheaderdata[4];
4212 prefsize = int(preheaderdata[5], 16);
4213 prefatime = int(preheaderdata[6], 16);
4214 prefmtime = int(preheaderdata[7], 16);
4215 prefctime = int(preheaderdata[8], 16);
4216 prefbtime = int(preheaderdata[9], 16);
4217 prefmode = int(preheaderdata[10], 16);
4218 prefchmode = stat.S_IMODE(prefmode);
4219 preftypemod = stat.S_IFMT(prefmode);
4220 prefwinattributes = int(preheaderdata[11], 16);
4221 prefuid = int(preheaderdata[12], 16);
4222 prefuname = preheaderdata[13];
4223 prefgid = int(preheaderdata[14], 16);
4224 prefgname = preheaderdata[15];
4225 fid = int(preheaderdata[16], 16);
4226 finode = int(preheaderdata[17], 16);
4227 flinkcount = int(preheaderdata[18], 16);
4228 prefdev_minor = int(preheaderdata[19], 16);
4229 prefdev_major = int(preheaderdata[20], 16);
4230 prefrdev_minor = int(preheaderdata[22], 16);
4231 prefrdev_major = int(preheaderdata[23], 16);
4232 prefextrasize = int(preheaderdata[24], 16);
4233 prefextrafields = int(preheaderdata[25], 16);
4234 extrafieldslist = [];
4235 extrastart = 25;
4236 extraend = extrastart + prefextrafields;
4237 extrafieldslist = [];
4238 if(extrastart<extraend):
4239 extrafieldslist.append(preheaderdata[extrastart]);
4240 extrastart = extrastart + 1;
4241 prefchecksumtype = preheaderdata[extrastart].lower();
4242 prefcs = preheaderdata[extrastart + 1].lower();
4243 prefccs = preheaderdata[extrastart + 2].lower();
4244 hc = 0;
4245 hcmax = len(preheaderdata) - 2;
4246 hout = "";
4247 while(hc<hcmax):
4248 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
4249 hc = hc + 1;
4250 prenewfcs = GetFileChecksum(hout, preheaderdata[-3].lower(), True, formatspecs);
4251 if(prefcs!=prenewfcs and not skipchecksum):
4252 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
4253 return False;
4254 valid_archive = False;
4255 invalid_archive = True;
4256 prefhend = catfp.tell() - 1;
4257 prefcontentstart = catfp.tell();
4258 prefcontents = "";
4259 pyhascontents = False;
4260 if(prefsize>0):
4261 prefcontents = catfp.read(prefsize);
4262 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
4263 pyhascontents = True;
4264 if(prefccs!=prenewfccs and not skipchecksum):
4265 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
4266 return False;
4267 catfp.seek(1, 1);
4268 il = il + 1;
4269 filefound = False;
4270 prefname = preheaderdata[2];
4271 if(re.findall("^[.|/]", preheaderdata[2])):
4272 prefname = preheaderdata[2];
4273 else:
4274 prefname = "./"+preheaderdata[2];
4275 if(prefname==seekfile):
4276 filefound = True;
4277 break;
4278 catfp.seek(seekstart, 0);
4279 fileidnum = il;
4280 catfheadsize = int(preheaderdata[0], 16);
4281 catfnumfields = int(preheaderdata[1], 16);
4282 catftype = int(preheaderdata[2], 16);
4283 if(re.findall("^[.|/]", preheaderdata[3])):
4284 catfname = preheaderdata[3];
4285 else:
4286 catfname = "./"+preheaderdata[3];
4287 catflinkname = preheaderdata[4];
4288 catfsize = int(preheaderdata[5], 16);
4289 catfbasedir = os.path.dirname(catfname);
4290 if(filefound):
4291 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
4292 else:
4293 return False;
4294 if(returnfp):
4295 catlist.update({'catfp': catfp});
4296 else:
4297 catfp.close();
4298 return catlist;
4300 create_alias_function("", __file_format_name__, "SeekToFileName", ArchiveFileSeekToFileName);
4302 def ArchiveFileValidate(infile, formatspecs=__file_format_list__, verbose=False, returnfp=False):
4303 if(verbose):
4304 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4305 if(hasattr(infile, "read") or hasattr(infile, "write")):
4306 catfp = infile;
4307 catfp.seek(0, 0);
4308 catfp = UncompressArchiveFile(catfp, formatspecs);
4309 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
4310 if(checkcompressfile=="tarfile"):
4311 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4312 if(checkcompressfile=="zipfile"):
4313 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4314 if(rarfile_support and checkcompressfile=="rarfile"):
4315 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4316 if(py7zr_support and checkcompressfile=="7zipfile"):
4317 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4318 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4319 return False;
4320 if(not catfp):
4321 return False;
4322 catfp.seek(0, 0);
4323 elif(infile=="-"):
4324 catfp = BytesIO();
4325 if(hasattr(sys.stdin, "buffer")):
4326 shutil.copyfileobj(sys.stdin.buffer, catfp);
4327 else:
4328 shutil.copyfileobj(sys.stdin, catfp);
4329 catfp.seek(0, 0);
4330 catfp = UncompressArchiveFile(catfp, formatspecs);
4331 if(not catfp):
4332 return False;
4333 catfp.seek(0, 0);
4334 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
4335 catfp = download_file_from_internet_file(infile);
4336 catfp = UncompressArchiveFile(catfp, formatspecs);
4337 catfp.seek(0, 0);
4338 if(not catfp):
4339 return False;
4340 catfp.seek(0, 0);
4341 else:
4342 infile = RemoveWindowsPath(infile);
4343 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4344 if(checkcompressfile=="tarfile"):
4345 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4346 if(checkcompressfile=="zipfile"):
4347 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4348 if(rarfile_support and checkcompressfile=="rarfile"):
4349 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4350 if(py7zr_support and checkcompressfile=="7zipfile"):
4351 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4352 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4353 return False;
4354 compresscheck = CheckCompressionType(infile, formatspecs, True);
4355 if(not compresscheck):
4356 fextname = os.path.splitext(infile)[1];
4357 if(fextname==".gz"):
4358 compresscheck = "gzip";
4359 elif(fextname==".bz2"):
4360 compresscheck = "bzip2";
4361 elif(fextname==".zst"):
4362 compresscheck = "zstd";
4363 elif(fextname==".lz4" or fextname==".clz4"):
4364 compresscheck = "lz4";
4365 elif(fextname==".lzo" or fextname==".lzop"):
4366 compresscheck = "lzo";
4367 elif(fextname==".lzma" or fextname==".xz"):
4368 compresscheck = "lzma";
4369 else:
4370 return False;
4371 if(not compresscheck):
4372 return False;
4373 catfp = UncompressFile(infile, formatspecs, "rb");
4375 try:
4376 catfp.seek(0, 2);
4377 except OSError:
4378 SeekToEndOfFile(catfp);
4379 except ValueError:
4380 SeekToEndOfFile(catfp);
4381 CatSize = catfp.tell();
4382 CatSizeEnd = CatSize;
4384 try:
4385 catfp.seek(0, 0);
4386 except OSError:
4387 return False;
4388 except ValueError:
4389 return False;
4390 curloc = catfp.tell();
4391 if(curloc>0):
4392 catfp.seek(0, 0);
4393 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
4394 if(curloc>0):
4395 catfp.seek(curloc, 0);
4396 catstring = catheader[0];
4397 catversion = re.findall(r"([\d]+)$", catstring);
4398 fprenumfiles = catheader[1];
4399 fnumfiles = int(fprenumfiles, 16);
4400 fprechecksumtype = catheader[2];
4401 fprechecksum = catheader[3];
4402 il = 0;
4403 fileheader = AppendNullByte(catstring, formatspecs[5]);
4404 fnumfileshex = format(int(fnumfiles), 'x').lower();
4405 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
4406 if(fprechecksumtype=="none" or fprechecksumtype==""):
4407 catfileheadercshex = format(0, 'x').lower();
4408 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
4409 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4410 elif(fprechecksumtype=="crc16_ccitt"):
4411 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4412 elif(fprechecksumtype=="adler32"):
4413 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4414 elif(fprechecksumtype=="crc32"):
4415 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4416 elif(fprechecksumtype=="crc64_ecma"):
4417 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4418 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
4419 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4420 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
4421 checksumoutstr = hashlib.new(fprechecksumtype);
4422 checksumoutstr.update(fileheader.encode('UTF-8'));
4423 catfileheadercshex = checksumoutstr.hexdigest().lower();
4424 else:
4425 catfileheadercshex = format(0, 'x').lower();
4426 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4427 valid_archive = True;
4428 invalid_archive = False;
4429 if(verbose):
4430 VerbosePrintOut(infile);
4431 VerbosePrintOut("Number of Records " + str(fnumfiles));
4432 if(fprechecksum==catfileheadercshex):
4433 if(verbose):
4434 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4435 else:
4436 if(verbose):
4437 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4438 valid_archive = False;
4439 invalid_archive = True;
4440 if(verbose):
4441 VerbosePrintOut("");
4442 while(il<fnumfiles):
4443 catfhstart = catfp.tell();
4444 if(formatspecs[7]):
4445 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4446 else:
4447 catheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4448 if(len(catheaderdata)==0):
4449 break;
4450 catfheadsize = int(catheaderdata[0], 16);
4451 catfnumfields = int(catheaderdata[1], 16);
4452 catftype = int(catheaderdata[2], 16);
4453 if(re.findall("^[.|/]", catheaderdata[3])):
4454 catfname = catheaderdata[3];
4455 else:
4456 catfname = "./"+catheaderdata[3];
4457 catfbasedir = os.path.dirname(catfname);
4458 catflinkname = catheaderdata[4];
4459 catfsize = int(catheaderdata[5], 16);
4460 catfatime = int(catheaderdata[6], 16);
4461 catfmtime = int(catheaderdata[7], 16);
4462 catfctime = int(catheaderdata[8], 16);
4463 catfbtime = int(catheaderdata[9], 16);
4464 catfmode = int(catheaderdata[10], 16);
4465 catfchmode = stat.S_IMODE(catfmode);
4466 catftypemod = stat.S_IFMT(catfmode);
4467 prefwinattributes = int(catheaderdata[11], 16);
4468 catfuid = int(catheaderdata[12], 16);
4469 catfuname = catheaderdata[13];
4470 catfgid = int(catheaderdata[14], 16);
4471 catfgname = catheaderdata[15];
4472 fid = int(catheaderdata[16], 16);
4473 finode = int(catheaderdata[17], 16);
4474 flinkcount = int(catheaderdata[18], 16);
4475 catfdev_minor = int(catheaderdata[19], 16);
4476 catfdev_major = int(catheaderdata[20], 16);
4477 catfrdev_minor = int(catheaderdata[21], 16);
4478 catfrdev_major = int(catheaderdata[22], 16);
4479 catfextrasize = int(catheaderdata[23], 16);
4480 catfextrafields = int(catheaderdata[24], 16);
4481 extrafieldslist = [];
4482 extrastart = 25;
4483 extraend = extrastart + catfextrafields;
4484 extrafieldslist = [];
4485 if(extrastart<extraend):
4486 extrafieldslist.append(catheaderdata[extrastart]);
4487 extrastart = extrastart + 1;
4488 catfchecksumtype = catheaderdata[extrastart].lower();
4489 catfcs = catheaderdata[extrastart + 1].lower();
4490 catfccs = catheaderdata[extrastart + 2].lower();
4491 hc = 0;
4492 hcmax = len(catheaderdata) - 2;
4493 hout = "";
4494 while(hc<hcmax):
4495 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
4496 hc = hc + 1;
4497 catnewfcs = GetFileChecksum(hout, catheaderdata[-3].lower(), True, formatspecs);
4498 if(verbose):
4499 VerbosePrintOut(catfname);
4500 VerbosePrintOut("Record Number " + str(il) + "; File ID " + str(fid) + "; iNode Number " + str(finode));
4501 if(catfcs==catnewfcs):
4502 if(verbose):
4503 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart));
4504 else:
4505 if(verbose):
4506 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart));
4507 valid_archive = False;
4508 invalid_archive = True;
4509 catfhend = catfp.tell() - 1;
4510 catfcontentstart = catfp.tell();
4511 catfcontents = "";
4512 pyhascontents = False;
4513 if(catfsize>0):
4514 catfcontents = catfp.read(catfsize);
4515 catnewfccs = GetFileChecksum(catfcontents, catheaderdata[-3].lower(), False, formatspecs);
4516 pyhascontents = True;
4517 if(catfccs==catnewfccs):
4518 if(verbose):
4519 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart));
4520 else:
4521 if(verbose):
4522 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart));
4523 valid_archive = False;
4524 invalid_archive = True;
4525 if(verbose):
4526 VerbosePrintOut("");
4527 catfp.seek(1, 1);
4528 il = il + 1;
4529 if(valid_archive):
4530 if(returnfp):
4531 return catfp;
4532 else:
4533 catfp.close();
4534 return True;
4535 else:
4536 catfp.close();
4537 return False;
4539 create_alias_function("", __file_format_name__, "Validate", ArchiveFileValidate);
4541 def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4542 if(hasattr(infile, "read") or hasattr(infile, "write")):
4543 catfp = infile;
4544 catfp.seek(0, 0);
4545 catfp = UncompressArchiveFile(catfp, formatspecs);
4546 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
4547 if(checkcompressfile=="tarfile"):
4548 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4549 if(checkcompressfile=="zipfile"):
4550 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4551 if(rarfile_support and checkcompressfile=="rarfile"):
4552 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4553 if(py7zr_support and checkcompressfile=="7zipfile"):
4554 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4555 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4556 return False;
4557 if(not catfp):
4558 return False;
4559 catfp.seek(0, 0);
4560 elif(infile=="-"):
4561 catfp = BytesIO();
4562 if(hasattr(sys.stdin, "buffer")):
4563 shutil.copyfileobj(sys.stdin.buffer, catfp);
4564 else:
4565 shutil.copyfileobj(sys.stdin, catfp);
4566 catfp.seek(0, 0);
4567 catfp = UncompressArchiveFile(catfp, formatspecs);
4568 if(not catfp):
4569 return False;
4570 catfp.seek(0, 0);
4571 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
4572 catfp = download_file_from_internet_file(infile);
4573 catfp = UncompressArchiveFile(catfp, formatspecs);
4574 catfp.seek(0, 0);
4575 if(not catfp):
4576 return False;
4577 catfp.seek(0, 0);
4578 else:
4579 infile = RemoveWindowsPath(infile);
4580 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4581 if(checkcompressfile=="tarfile"):
4582 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4583 if(checkcompressfile=="zipfile"):
4584 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4585 if(rarfile_support and checkcompressfile=="rarfile"):
4586 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4587 if(py7zr_support and checkcompressfile=="7zipfile"):
4588 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4589 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4590 return False;
4591 compresscheck = CheckCompressionType(infile, formatspecs, True);
4592 if(not compresscheck):
4593 fextname = os.path.splitext(infile)[1];
4594 if(fextname==".gz"):
4595 compresscheck = "gzip";
4596 elif(fextname==".bz2"):
4597 compresscheck = "bzip2";
4598 elif(fextname==".zst"):
4599 compresscheck = "zstd";
4600 elif(fextname==".lz4" or fextname==".clz4"):
4601 compresscheck = "lz4";
4602 elif(fextname==".lzo" or fextname==".lzop"):
4603 compresscheck = "lzo";
4604 elif(fextname==".lzma" or fextname==".xz"):
4605 compresscheck = "lzma";
4606 else:
4607 return False;
4608 if(not compresscheck):
4609 return False;
4610 catfp = UncompressFile(infile, formatspecs, "rb");
4612 try:
4613 catfp.seek(0, 2);
4614 except OSError:
4615 SeekToEndOfFile(catfp);
4616 except ValueError:
4617 SeekToEndOfFile(catfp);
4618 CatSize = catfp.tell();
4619 CatSizeEnd = CatSize;
4621 try:
4622 catfp.seek(0, 0);
4623 except OSError:
4624 return False;
4625 except ValueError:
4626 return False;
4627 curloc = catfp.tell();
4628 if(curloc>0):
4629 catfp.seek(0, 0);
4630 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
4631 if(curloc>0):
4632 catfp.seek(curloc, 0);
4633 catstring = catheader[0];
4634 catversion = re.findall(r"([\d]+)$", catstring);
4635 fprenumfiles = catheader[1];
4636 fnumfiles = int(fprenumfiles, 16);
4637 fprechecksumtype = catheader[2];
4638 fprechecksum = catheader[3];
4639 fileheader = AppendNullByte(catstring, formatspecs[5]);
4640 fnumfileshex = format(int(fnumfiles), 'x').lower();
4641 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
4642 if(fprechecksumtype=="none" or fprechecksumtype==""):
4643 catfileheadercshex = format(0, 'x').lower();
4644 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
4645 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4646 elif(fprechecksumtype=="crc16_ccitt"):
4647 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4648 elif(fprechecksumtype=="adler32"):
4649 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4650 elif(fprechecksumtype=="crc32"):
4651 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4652 elif(fprechecksumtype=="crc64_ecma"):
4653 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4654 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
4655 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4656 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
4657 checksumoutstr = hashlib.new(fprechecksumtype);
4658 checksumoutstr.update(fileheader.encode('UTF-8'));
4659 catfileheadercshex = checksumoutstr.hexdigest().lower();
4660 else:
4661 catfileheadercshex = format(0, 'x').lower();
4662 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4663 fheadtell = len(fileheader);
4664 if(fprechecksum!=catfileheadercshex and not skipchecksum):
4665 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
4666 return False;
4667 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4668 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
4669 if(seekstart<0 and seekstart>fnumfiles):
4670 seekstart = 0;
4671 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
4672 seekend = fnumfiles;
4673 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
4674 seekend = fnumfiles - abs(seekend);
4675 if(seekstart>0):
4676 il = 0;
4677 while(il < seekstart):
4678 prefhstart = catfp.tell();
4679 if(formatspecs[7]):
4680 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4681 else:
4682 preheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4683 if(len(preheaderdata)==0):
4684 break;
4685 prefheadsize = int(preheaderdata[0], 16);
4686 prefnumfields = int(preheaderdata[1], 16);
4687 if(re.findall("^[.|/]", preheaderdata[3])):
4688 prefname = preheaderdata[3];
4689 else:
4690 prefname = "./"+preheaderdata[3];
4691 prefsize = int(preheaderdata[5], 16);
4692 prefextrasize = int(preheaderdata[23], 16);
4693 prefextrafields = int(preheaderdata[24], 16);
4694 extrafieldslist = [];
4695 extrastart = 25;
4696 extraend = extrastart + prefextrafields;
4697 extrafieldslist = [];
4698 if(extrastart<extraend):
4699 extrafieldslist.append(preheaderdata[extrastart]);
4700 extrastart = extrastart + 1;
4701 prefchecksumtype = preheaderdata[extrastart].lower();
4702 prefcs = preheaderdata[extrastart + 1].lower();
4703 prefccs = preheaderdata[extrastart + 2].lower();
4704 hc = 0;
4705 hcmax = len(preheaderdata) - 2;
4706 hout = "";
4707 while(hc<hcmax):
4708 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
4709 hc = hc + 1;
4710 prenewfcs = GetFileChecksum(hout, preheaderdata[-3].lower(), True, formatspecs);
4711 if(prefcs!=prenewfcs and not skipchecksum):
4712 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
4713 return False;
4714 valid_archive = False;
4715 invalid_archive = True;
4716 prefhend = catfp.tell() - 1;
4717 prefcontentstart = catfp.tell();
4718 prefcontents = "";
4719 pyhascontents = False;
4720 if(prefsize>0):
4721 prefcontents = catfp.read(prefsize);
4722 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
4723 pyhascontents = True;
4724 if(prefccs!=prenewfccs and not skipchecksum):
4725 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
4726 return False;
4727 catfp.seek(1, 1);
4728 il = il + 1;
4729 fileidnum = seekstart;
4730 realidnum = 0;
4731 while(fileidnum<seekend):
4732 catfhstart = catfp.tell();
4733 if(formatspecs[7]):
4734 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4735 else:
4736 catheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4737 if(len(catheaderdata)==0):
4738 break;
4739 catfheadsize = int(catheaderdata[0], 16);
4740 catfnumfields = int(catheaderdata[1], 16);
4741 catftype = int(catheaderdata[2], 16);
4742 if(re.findall("^[.|/]", catheaderdata[3])):
4743 catfname = catheaderdata[3];
4744 else:
4745 catfname = "./"+catheaderdata[3];
4746 catfbasedir = os.path.dirname(catfname);
4747 catflinkname = catheaderdata[4];
4748 catfsize = int(catheaderdata[5], 16);
4749 catfatime = int(catheaderdata[6], 16);
4750 catfmtime = int(catheaderdata[7], 16);
4751 catfctime = int(catheaderdata[8], 16);
4752 catfbtime = int(catheaderdata[9], 16);
4753 catfmode = int(catheaderdata[10], 16);
4754 catfchmode = stat.S_IMODE(catfmode);
4755 catftypemod = stat.S_IFMT(catfmode);
4756 catfwinattributes = int(catheaderdata[11], 16);
4757 catfuid = int(catheaderdata[12], 16);
4758 catfuname = catheaderdata[13];
4759 catfgid = int(catheaderdata[14], 16);
4760 catfgname = catheaderdata[15];
4761 fid = int(catheaderdata[16], 16);
4762 finode = int(catheaderdata[17], 16);
4763 flinkcount = int(catheaderdata[18], 16);
4764 catfdev_minor = int(catheaderdata[19], 16);
4765 catfdev_major = int(catheaderdata[20], 16);
4766 catfrdev_minor = int(catheaderdata[21], 16);
4767 catfrdev_major = int(catheaderdata[22], 16);
4768 catfextrasize = int(catheaderdata[23], 16);
4769 catfextrafields = int(catheaderdata[24], 16);
4770 extrafieldslist = [];
4771 extrastart = 25;
4772 extraend = extrastart + catfextrafields;
4773 extrafieldslist = [];
4774 if(extrastart<extraend):
4775 extrafieldslist.append(catheaderdata[extrastart]);
4776 extrastart = extrastart + 1;
4777 catfchecksumtype = catheaderdata[extrastart].lower();
4778 catfcs = catheaderdata[extrastart + 1].lower();
4779 catfccs = catheaderdata[extrastart + 2].lower();
4780 hc = 0;
4781 hcmax = len(catheaderdata) - 2;
4782 hout = "";
4783 while(hc<hcmax):
4784 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
4785 hc = hc + 1;
4786 catnewfcs = GetFileChecksum(hout, catheaderdata[-3].lower(), True, formatspecs);
4787 if(catfcs!=catnewfcs and not skipchecksum):
4788 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
4789 return False;
4790 catfhend = catfp.tell() - 1;
4791 catfcontentstart = catfp.tell();
4792 catfcontents = "";
4793 pyhascontents = False;
4794 if(catfsize>0 and not listonly):
4795 catfcontents = catfp.read(catfsize);
4796 catnewfccs = GetFileChecksum(catfcontents, catheaderdata[-3].lower(), False, formatspecs);
4797 pyhascontents = True;
4798 if(catfccs!=catnewfccs and skipchecksum):
4799 VerbosePrintOut("File Content Checksum Error with file " + catfname + " at offset " + str(catfcontentstart));
4800 return False;
4801 if(catfsize>0 and listonly):
4802 catfp.seek(catfsize, 1);
4803 pyhascontents = False;
4804 catfp.seek(1, 1);
4805 catfcontentend = catfp.tell() - 1;
4806 catlist['ffilelist'].update({realidnum: {'fid': realidnum, 'fidalt': fileidnum, 'fheadersize': catfheadsize, 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize, 'fatime': catfatime, 'fmtime': catfmtime, 'fctime': catfctime, 'fbtime': catfbtime, 'fmode': catfmode, 'fchmode': catfchmode, 'ftypemod': catftypemod, 'fwinattributes': catfwinattributes, 'fuid': catfuid, 'funame': catfuname, 'fgid': catfgid, 'fgname': catfgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': catfdev_minor, 'fmajor': catfdev_major, 'frminor': catfrdev_minor, 'frmajor': catfrdev_major, 'fchecksumtype': catfchecksumtype, 'fnumfields': catfnumfields + 2, 'frawheader': catheaderdata, 'fextrafields': catfextrafields, 'fextrafieldsize': catfextrasize, 'fextralist': extrafieldslist, 'fheaderchecksum': catfcs, 'fcontentchecksum': catfccs, 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': catfcontents} });
4807 fileidnum = fileidnum + 1;
4808 realidnum = realidnum + 1;
4809 if(returnfp):
4810 catlist.update({'catfp': catfp});
4811 else:
4812 catfp.close();
4813 return catlist;
4815 create_alias_function("", __file_format_name__, "ToArray", ArchiveFileToArray);
4817 def ArchiveFileStringToArray(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4818 catfp = BytesIO(catstr);
4819 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4820 return listcatfiles;
4822 create_alias_function("", __file_format_name__, "StringToArray", ArchiveFileStringToArray);
4824 def TarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4825 catfp = BytesIO();
4826 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4827 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4828 return listcatfiles;
4830 def ZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4831 catfp = BytesIO();
4832 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4833 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4834 return listcatfiles;
4836 if(not rarfile_support):
4837 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4838 return False;
4840 if(rarfile_support):
4841 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4842 catfp = BytesIO();
4843 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4844 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4845 return listcatfiles;
4847 def ListDirToArrayAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4848 catver = formatspecs[6];
4849 fileheaderver = str(int(catver.replace(".", "")));
4850 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4851 advancedlist = formatspecs[8];
4852 altinode = formatspecs[9];
4853 infilelist = [];
4854 if(infiles=="-"):
4855 for line in sys.stdin:
4856 infilelist.append(line.strip());
4857 infilelist = list(filter(None, infilelist));
4858 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
4859 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
4860 return False;
4861 with UncompressFile(infiles, formatspecs, "r") as finfile:
4862 for line in finfile:
4863 infilelist.append(line.strip());
4864 infilelist = list(filter(None, infilelist));
4865 else:
4866 if(isinstance(infiles, (list, tuple, ))):
4867 infilelist = list(filter(None, infiles));
4868 elif(isinstance(infiles, (str, ))):
4869 infilelist = list(filter(None, [infiles]));
4870 if(advancedlist):
4871 GetDirList = ListDirAdvanced(infilelist, followlink, False);
4872 else:
4873 GetDirList = ListDir(infilelist, followlink, False);
4874 if(not GetDirList):
4875 return False;
4876 curinode = 0;
4877 curfid = 0;
4878 inodelist = [];
4879 inodetofile = {};
4880 filetoinode = {};
4881 inodetocatinode = {};
4882 fileidnum = 0;
4883 fnumfiles = int(len(GetDirList));
4884 catver = formatspecs[6];
4885 fileheaderver = str(int(catver.replace(".", "")));
4886 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4887 fnumfileshex = format(int(fnumfiles), 'x').lower();
4888 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4889 catversion = re.findall(r"([\d]+)$", fileheader);
4890 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4891 if(checksumtype=="none" or checksumtype==""):
4892 catfileheadercshex = format(0, 'x').lower();
4893 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4894 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4895 elif(checksumtype=="crc16_ccitt"):
4896 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4897 elif(checksumtype=="adler32"):
4898 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4899 elif(checksumtype=="crc32"):
4900 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4901 elif(checksumtype=="crc64_ecma"):
4902 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4903 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4904 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4905 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4906 checksumoutstr = hashlib.new(checksumtype);
4907 checksumoutstr.update(fileheader.encode('UTF-8'));
4908 catfileheadercshex = checksumoutstr.hexdigest().lower();
4909 else:
4910 catfileheadercshex = format(0, 'x').lower();
4911 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4912 fheadtell = len(fileheader);
4913 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4914 for curfname in GetDirList:
4915 if(re.findall("^[.|/]", curfname)):
4916 fname = curfname;
4917 else:
4918 fname = "./"+curfname;
4919 if(verbose):
4920 VerbosePrintOut(fname);
4921 if(not followlink or followlink is None):
4922 fstatinfo = os.lstat(fname);
4923 else:
4924 fstatinfo = os.stat(fname);
4925 fpremode = fstatinfo.st_mode;
4926 finode = fstatinfo.st_ino;
4927 flinkcount = fstatinfo.st_nlink;
4928 ftype = 0;
4929 if(stat.S_ISREG(fpremode)):
4930 ftype = 0;
4931 elif(stat.S_ISLNK(fpremode)):
4932 ftype = 2;
4933 elif(stat.S_ISCHR(fpremode)):
4934 ftype = 3;
4935 elif(stat.S_ISBLK(fpremode)):
4936 ftype = 4;
4937 elif(stat.S_ISDIR(fpremode)):
4938 ftype = 5;
4939 elif(stat.S_ISFIFO(fpremode)):
4940 ftype = 6;
4941 elif(stat.S_ISSOCK(fpremode)):
4942 ftype = 8;
4943 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
4944 ftype = 9;
4945 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
4946 ftype = 10;
4947 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
4948 ftype = 11;
4949 else:
4950 ftype = 0;
4951 flinkname = "";
4952 fbasedir = os.path.dirname(fname);
4953 fcurfid = curfid;
4954 if(not followlink and finode!=0):
4955 if(ftype!=1):
4956 if(finode in inodelist):
4957 ftype = 1;
4958 flinkname = inodetofile[finode];
4959 if(altinode):
4960 fcurinode = finode;
4961 else:
4962 fcurinode = inodetocatinode[finode];
4963 if(finode not in inodelist):
4964 inodelist.append(finode);
4965 inodetofile.update({finode: fname});
4966 inodetocatinode.update({finode: curinode});
4967 if(altinode):
4968 fcurinode = finode;
4969 else:
4970 fcurinode = curinode;
4971 curinode = curinode + 1;
4972 else:
4973 fcurinode = curinode;
4974 curinode = curinode + 1;
4975 curfid = curfid + 1;
4976 if(ftype==2):
4977 flinkname = os.readlink(fname);
4978 fdev = fstatinfo.st_dev;
4979 getfdev = GetDevMajorMinor(fdev);
4980 fdev_minor = getfdev[0];
4981 fdev_major = getfdev[1];
4982 frdev = fstatinfo.st_dev;
4983 if(hasattr(fstatinfo, "st_rdev")):
4984 frdev = fstatinfo.st_rdev;
4985 else:
4986 frdev = fstatinfo.st_dev;
4987 getfrdev = GetDevMajorMinor(frdev);
4988 frdev_minor = getfrdev[0];
4989 frdev_major = getfrdev[1];
4990 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
4991 fsize = "0";
4992 if(ftype==0 or ftype==7):
4993 fsize = fstatinfo.st_size;
4994 fatime = fstatinfo.st_atime;
4995 fmtime = fstatinfo.st_mtime;
4996 fctime = fstatinfo.st_ctime;
4997 if(hasattr(fstatinfo, "st_birthtime")):
4998 fbtime = fstatinfo.st_birthtime;
4999 else:
5000 fbtime = fstatinfo.st_ctime;
5001 fmode = fstatinfo.st_mode;
5002 fchmode = stat.S_IMODE(fstatinfo.st_mode);
5003 ftypemod = stat.S_IFMT(fstatinfo.st_mode);
5004 fuid = fstatinfo.st_uid;
5005 fgid = fstatinfo.st_gid;
5006 funame = "";
5007 try:
5008 import pwd;
5009 try:
5010 userinfo = pwd.getpwuid(fstatinfo.st_uid);
5011 funame = userinfo.pw_name;
5012 except KeyError:
5013 funame = "";
5014 except ImportError:
5015 funame = "";
5016 fgname = "";
5017 try:
5018 import grp;
5019 try:
5020 groupinfo = grp.getgrgid(fstatinfo.st_gid);
5021 fgname = groupinfo.gr_name;
5022 except KeyError:
5023 fgname = "";
5024 except ImportError:
5025 fgname = "";
5026 fdev_minor = fdev_minor;
5027 fdev_major = fdev_major;
5028 frdev_minor = frdev_minor;
5029 frdev_major = frdev_major;
5030 finode = finode;
5031 flinkcount = flinkcount;
5032 if(hasattr(fstatinfo, "st_file_attributes")):
5033 fwinattributes = fstatinfo.st_file_attributes;
5034 else:
5035 fwinattributes = 0;
5036 fcontents = "".encode('UTF-8');
5037 chunk_size = 1024;
5038 if(ftype==0 or ftype==7):
5039 with open(fname, "rb") as fpc:
5040 while(True):
5041 chunk = fpc.read(chunk_size);
5042 if(not chunk):
5043 break
5044 fcontents += chunk;
5045 if(followlink and (ftype==1 or ftype==2)):
5046 flstatinfo = os.stat(flinkname);
5047 with open(flinkname, "rb") as fpc:
5048 while(True):
5049 chunk = fpc.read(chunk_size);
5050 if(not chunk):
5051 break;
5052 fcontents += chunk;
5053 ftypehex = format(ftype, 'x').lower();
5054 extrafields = len(extradata);
5055 extrafieldslist = extradata;
5056 catfextrafields = extrafields;
5057 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5058 if(len(extradata)>0):
5059 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5060 extrasizelen = len(extrasizestr);
5061 extrasizelenhex = format(extrasizelen, 'x').lower();
5062 catoutlist = [ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fwinattributes), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()];
5063 catoutlen = len(catoutlist) + len(extradata) + 3;
5064 catoutlenhex = format(catoutlen, 'x').lower();
5065 catoutlist.insert(0, catoutlenhex);
5066 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5067 if(len(extradata)>0):
5068 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5069 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5070 catfnumfields = catoutlen;
5071 if(checksumtype=="none" or checksumtype==""):
5072 catfileheadercshex = format(0, 'x').lower();
5073 catfilecontentcshex = format(0, 'x').lower();
5074 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5075 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5076 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5077 elif(checksumtype=="crc16_ccitt"):
5078 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5079 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5080 elif(checksumtype=="adler32"):
5081 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5082 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5083 elif(checksumtype=="crc32"):
5084 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5085 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5086 elif(checksumtype=="crc64_ecma"):
5087 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5088 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5089 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5090 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5091 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5092 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5093 checksumoutstr = hashlib.new(checksumtype);
5094 checksumoutstr.update("".encode('UTF-8'));
5095 catfileheadercshex = checksumoutstr.hexdigest().lower();
5096 checksumoutstr = hashlib.new(checksumtype);
5097 checksumoutstr.update(fcontents);
5098 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5099 else:
5100 catfileheadercshex = format(0, 'x').lower();
5101 catfilecontentcshex = format(0, 'x').lower();
5102 catfhstart = fheadtell;
5103 fheadtell += len(catfileoutstr);
5104 catfhend = fheadtell - 1;
5105 catfcontentstart = fheadtell;
5106 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5107 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5108 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5109 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex]
5110 if(checksumtype=="none" or checksumtype==""):
5111 catfileheadercshex = format(0, 'x').lower();
5112 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5113 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5114 elif(checksumtype=="crc16_ccitt"):
5115 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5116 elif(checksumtype=="adler32"):
5117 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5118 elif(checksumtype=="crc32"):
5119 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5120 elif(checksumtype=="crc64_ecma"):
5121 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5122 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5123 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5124 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5125 checksumoutstr = hashlib.new(checksumtype);
5126 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5127 catfileheadercshex = checksumoutstr.hexdigest().lower();
5128 else:
5129 catfileheadercshex = format(0, 'x').lower();
5130 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5131 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5132 nullstrecd = formatspecs[5].encode('UTF-8');
5133 fheadtell += len(catfileoutstr) + 1;
5134 catfcontentend = fheadtell - 1;
5135 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5136 pyhascontents = False;
5137 if(int(fsize)>0 and not listonly):
5138 pyhascontents = True;
5139 if(int(fsize)>0 and listonly):
5140 fcontents = "";
5141 pyhascontents = False;
5142 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fwinattributes': fwinattributes, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields + 2, 'frawheader': catheaderdata, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
5143 fileidnum = fileidnum + 1;
5144 return catlist;
5146 def TarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5147 curinode = 0;
5148 curfid = 0;
5149 inodelist = [];
5150 inodetofile = {};
5151 filetoinode = {};
5152 inodetocatinode = {};
5153 fileidnum = 0;
5154 if(infile=="-"):
5155 infile = BytesIO();
5156 if(hasattr(sys.stdin, "buffer")):
5157 shutil.copyfileobj(sys.stdin.buffer, infile);
5158 else:
5159 shutil.copyfileobj(sys.stdin, infile);
5160 infile.seek(0, 0);
5161 if(not infile):
5162 return False;
5163 infile.seek(0, 0);
5164 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
5165 infile = download_file_from_internet_file(infile);
5166 infile.seek(0, 0);
5167 if(not infile):
5168 return False;
5169 infile.seek(0, 0);
5170 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
5171 return False;
5172 elif(os.path.exists(infile) and os.path.isfile(infile)):
5173 try:
5174 if(not tarfile.is_tarfile(infile)):
5175 return False;
5176 except AttributeError:
5177 if(not is_tarfile(infile)):
5178 return False;
5179 else:
5180 return False;
5181 try:
5182 if(hasattr(infile, "read") or hasattr(infile, "write")):
5183 tarfp = tarfile.open(fileobj=infile, mode="r");
5184 else:
5185 tarfp = tarfile.open(infile, "r");
5186 except FileNotFoundError:
5187 return False;
5188 fnumfiles = int(len(tarfp.getmembers()));
5189 catver = formatspecs[6];
5190 fileheaderver = str(int(catver.replace(".", "")));
5191 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5192 fnumfileshex = format(int(fnumfiles), 'x').lower();
5193 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5194 catversion = re.findall(r"([\d]+)$", fileheader);
5195 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5196 if(checksumtype=="none" or checksumtype==""):
5197 catfileheadercshex = format(0, 'x').lower();
5198 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5199 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5200 elif(checksumtype=="crc16_ccitt"):
5201 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5202 elif(checksumtype=="adler32"):
5203 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5204 elif(checksumtype=="crc32"):
5205 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5206 elif(checksumtype=="crc64_ecma"):
5207 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5208 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5209 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5210 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5211 checksumoutstr = hashlib.new(checksumtype);
5212 checksumoutstr.update(fileheader.encode('UTF-8'));
5213 catfileheadercshex = checksumoutstr.hexdigest().lower();
5214 else:
5215 catfileheadercshex = format(0, 'x').lower();
5216 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5217 fheadtell = len(fileheader);
5218 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5219 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
5220 if(re.findall("^[.|/]", member.name)):
5221 fname = member.name;
5222 else:
5223 fname = "./"+member.name;
5224 if(verbose):
5225 VerbosePrintOut(fname);
5226 fpremode = member.mode;
5227 ffullmode = member.mode;
5228 flinkcount = 0;
5229 ftype = 0;
5230 if(member.isreg()):
5231 ffullmode = member.mode + stat.S_IFREG;
5232 ftype = 0;
5233 elif(member.isdev()):
5234 ffullmode = member.mode;
5235 ftype = 7;
5236 elif(member.islnk()):
5237 ffullmode = member.mode + stat.S_IFREG;
5238 ftype = 1;
5239 elif(member.issym()):
5240 ffullmode = member.mode + stat.S_IFLNK;
5241 ftype = 2;
5242 elif(member.ischr()):
5243 ffullmode = member.mode + stat.S_IFCHR;
5244 ftype = 3;
5245 elif(member.isblk()):
5246 ffullmode = member.mode + stat.S_IFBLK;
5247 ftype = 4;
5248 elif(member.isdir()):
5249 ffullmode = member.mode + stat.S_IFDIR;
5250 ftype = 5;
5251 elif(member.isfifo()):
5252 ffullmode = member.mode + stat.S_IFIFO;
5253 ftype = 6;
5254 elif(member.issparse()):
5255 ffullmode = member.mode;
5256 ftype = 12;
5257 else:
5258 ffullmode = member.mode;
5259 ftype = 0;
5260 flinkname = "";
5261 fbasedir = os.path.dirname(fname);
5262 fcurfid = curfid;
5263 fcurinode = curfid;
5264 finode = fcurinode;
5265 curfid = curfid + 1;
5266 if(ftype==2):
5267 flinkname = member.linkname;
5268 fdev_minor = member.devminor;
5269 fdev_major = member.devmajor;
5270 frdev_minor = member.devminor;
5271 frdev_major = member.devmajor;
5272 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
5273 fsize = "0";
5274 elif(ftype==0 or ftype==7):
5275 fsize = member.size;
5276 else:
5277 fsize = member.size;
5278 fatime = member.mtime;
5279 fmtime = member.mtime;
5280 fctime = member.mtime;
5281 fbtime = member.mtime;
5282 fmode = ffullmode;
5283 fchmode = stat.S_IMODE(ffullmode);
5284 ftypemod = stat.S_IFMT(ffullmode);
5285 fuid = member.uid;
5286 fgid = member.gid;
5287 funame = member.uname;
5288 fgname = member.gname;
5289 flinkcount = flinkcount;
5290 fwinattributes = int(0);
5291 fcontents = "".encode('UTF-8');
5292 chunk_size = 1024;
5293 if(ftype==0 or ftype==7):
5294 with tarfp.extractfile(member) as fpc:
5295 while(True):
5296 chunk = fpc.read(chunk_size);
5297 if(not chunk):
5298 break
5299 fcontents += chunk;
5300 ftypehex = format(ftype, 'x').lower();
5301 extrafields = len(extradata);
5302 extrafieldslist = extradata;
5303 catfextrafields = extrafields;
5304 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5305 if(len(extradata)>0):
5306 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5307 extrasizelen = len(extrasizestr);
5308 extrasizelenhex = format(extrasizelen, 'x').lower();
5309 catoutlist = [ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fwinattributes), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()];
5310 catoutlen = len(catoutlist) + len(extradata) + 3;
5311 catoutlenhex = format(catoutlen, 'x').lower();
5312 catoutlist.insert(0, catoutlenhex);
5313 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5314 if(len(extradata)>0):
5315 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5316 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5317 catfnumfields = catoutlen;
5318 if(checksumtype=="none" or checksumtype==""):
5319 catfileheadercshex = format(0, 'x').lower();
5320 catfilecontentcshex = format(0, 'x').lower();
5321 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5322 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5323 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5324 elif(checksumtype=="crc16_ccitt"):
5325 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5326 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5327 elif(checksumtype=="adler32"):
5328 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5329 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5330 elif(checksumtype=="crc32"):
5331 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5332 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5333 elif(checksumtype=="crc64_ecma"):
5334 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5335 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5336 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5337 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5338 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5339 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5340 checksumoutstr = hashlib.new(checksumtype);
5341 checksumoutstr.update("".encode('UTF-8'));
5342 catfileheadercshex = checksumoutstr.hexdigest().lower();
5343 checksumoutstr = hashlib.new(checksumtype);
5344 checksumoutstr.update(fcontents);
5345 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5346 else:
5347 catfileheadercshex = format(0, 'x').lower();
5348 catfilecontentcshex = format(0, 'x').lower();
5349 catfhstart = fheadtell;
5350 fheadtell += len(catfileoutstr);
5351 catfhend = fheadtell - 1;
5352 catfcontentstart = fheadtell;
5353 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5354 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5355 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5356 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex]
5357 if(checksumtype=="none" or checksumtype==""):
5358 catfileheadercshex = format(0, 'x').lower();
5359 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5360 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5361 elif(checksumtype=="crc16_ccitt"):
5362 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5363 elif(checksumtype=="adler32"):
5364 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5365 elif(checksumtype=="crc32"):
5366 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5367 elif(checksumtype=="crc64_ecma"):
5368 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5369 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5370 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5371 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5372 checksumoutstr = hashlib.new(checksumtype);
5373 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5374 catfileheadercshex = checksumoutstr.hexdigest().lower();
5375 else:
5376 catfileheadercshex = format(0, 'x').lower();
5377 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5378 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5379 nullstrecd = formatspecs[5].encode('UTF-8');
5380 fheadtell += len(catfileoutstr) + 1;
5381 catfcontentend = fheadtell - 1;
5382 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5383 pyhascontents = False;
5384 if(int(fsize)>0 and not listonly):
5385 pyhascontents = True;
5386 if(int(fsize)>0 and listonly):
5387 fcontents = "";
5388 pyhascontents = False;
5389 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fwinattributes': fwinattributes, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields + 2, 'frawheader': catheaderdata, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
5390 fileidnum = fileidnum + 1;
5391 return catlist;
5393 def ZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5394 curinode = 0;
5395 curfid = 0;
5396 inodelist = [];
5397 inodetofile = {};
5398 filetoinode = {};
5399 inodetocatinode = {};
5400 fileidnum = 0;
5401 if(infile=="-"):
5402 infile = BytesIO();
5403 if(hasattr(sys.stdin, "buffer")):
5404 shutil.copyfileobj(sys.stdin.buffer, infile);
5405 else:
5406 shutil.copyfileobj(sys.stdin, infile);
5407 infile.seek(0, 0);
5408 if(not infile):
5409 return False;
5410 infile.seek(0, 0);
5411 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
5412 infile = download_file_from_internet_file(infile);
5413 infile.seek(0, 0);
5414 if(not infile):
5415 return False;
5416 infile.seek(0, 0);
5417 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
5418 return False;
5419 else:
5420 return False;
5421 if(not zipfile.is_zipfile(infile)):
5422 return False;
5423 try:
5424 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
5425 except FileNotFoundError:
5426 return False;
5427 ziptest = zipfp.testzip();
5428 if(ziptest):
5429 VerbosePrintOut("Bad file found!");
5430 fnumfiles = int(len(zipfp.infolist()));
5431 catver = formatspecs[6];
5432 fileheaderver = str(int(catver.replace(".", "")));
5433 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5434 catversion = re.findall(r"([\d]+)$", fileheader);
5435 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5436 fnumfileshex = format(int(fnumfiles), 'x').lower();
5437 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5438 if(checksumtype=="none" or checksumtype==""):
5439 catfileheadercshex = format(0, 'x').lower();
5440 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5441 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5442 elif(checksumtype=="crc16_ccitt"):
5443 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5444 elif(checksumtype=="adler32"):
5445 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5446 elif(checksumtype=="crc32"):
5447 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5448 elif(checksumtype=="crc64_ecma"):
5449 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5450 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5451 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5452 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5453 checksumoutstr = hashlib.new(checksumtype);
5454 checksumoutstr.update(fileheader.encode('UTF-8'));
5455 catfileheadercshex = checksumoutstr.hexdigest().lower();
5456 else:
5457 catfileheadercshex = format(0, 'x').lower();
5458 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5459 fheadtell = len(fileheader);
5460 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5461 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
5462 if(re.findall("^[.|/]", member.filename)):
5463 fname = member.filename;
5464 else:
5465 fname = "./"+member.filename;
5466 zipinfo = zipfp.getinfo(member.filename);
5467 if(verbose):
5468 VerbosePrintOut(fname);
5469 if(not member.is_dir()):
5470 fpremode = stat.S_IFREG + 438;
5471 elif(member.is_dir()):
5472 fpremode = stat.S_IFDIR + 511;
5473 flinkcount = 0;
5474 ftype = 0;
5475 if(not member.is_dir()):
5476 ftype = 0;
5477 elif(member.is_dir()):
5478 ftype = 5;
5479 flinkname = "";
5480 fbasedir = os.path.dirname(fname);
5481 fcurfid = curfid;
5482 fcurinode = curfid;
5483 finode = fcurinode;
5484 curfid = curfid + 1;
5485 fdev_minor = 0;
5486 fdev_major = 0;
5487 frdev_minor = 0;
5488 frdev_major = 0;
5489 if(ftype==5):
5490 fsize = "0";
5491 elif(ftype==0):
5492 fsize = member.file_size;
5493 else:
5494 fsize = member.file_size;
5495 fatime = time.mktime(member.date_time + (0, 0, -1));
5496 fmtime = time.mktime(member.date_time + (0, 0, -1));
5497 fctime = time.mktime(member.date_time + (0, 0, -1));
5498 fbtime = time.mktime(member.date_time + (0, 0, -1));
5499 if(zipinfo.create_system==0 or zipinfo.create_system==10):
5500 fwinattributes = int(zipinfo.external_attr);
5501 if(not member.is_dir()):
5502 fmode = int(stat.S_IFREG + 438);
5503 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5504 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5505 elif(member.is_dir()):
5506 fmode = int(stat.S_IFDIR + 511);
5507 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5508 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5509 elif(zipinfo.create_system==3):
5510 fwinattributes = int(0);
5511 fmode = int(zipinfo.external_attr);
5512 else:
5513 fwinattributes = int(0);
5514 if(not member.is_dir()):
5515 fmode = int(stat.S_IFREG + 438);
5516 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5517 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5518 elif(member.is_dir()):
5519 fmode = int(stat.S_IFDIR + 511);
5520 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5521 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5522 try:
5523 fuid = os.getuid();
5524 except AttributeError:
5525 fuid = 0;
5526 except KeyError:
5527 fuid = 0;
5528 try:
5529 fgid = os.getgid();
5530 except AttributeError:
5531 fgid = 0;
5532 except KeyError:
5533 fgid = 0;
5534 try:
5535 import pwd;
5536 try:
5537 userinfo = pwd.getpwuid(os.getuid());
5538 funame = userinfo.pw_name;
5539 except KeyError:
5540 funame = "";
5541 except AttributeError:
5542 funame = "";
5543 except ImportError:
5544 funame = "";
5545 fgname = "";
5546 try:
5547 import grp;
5548 try:
5549 groupinfo = grp.getgrgid(os.getgid());
5550 fgname = groupinfo.gr_name;
5551 except KeyError:
5552 fgname = "";
5553 except AttributeError:
5554 fgname = "";
5555 except ImportError:
5556 fgname = "";
5557 fcontents = "".encode('UTF-8');
5558 if(ftype==0):
5559 fcontents = zipfp.read(member.filename);
5560 ftypehex = format(ftype, 'x').lower();
5561 extrafields = len(extradata);
5562 extrafieldslist = extradata;
5563 catfextrafields = extrafields;
5564 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5565 if(len(extradata)>0):
5566 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5567 extrasizelen = len(extrasizestr);
5568 extrasizelenhex = format(extrasizelen, 'x').lower();
5569 catoutlist = [ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fwinattributes), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()];
5570 catoutlen = len(catoutlist) + len(extradata) + 3;
5571 catoutlenhex = format(catoutlen, 'x').lower();
5572 catoutlist.insert(0, catoutlenhex);
5573 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5574 if(len(extradata)>0):
5575 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5576 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5577 catfnumfields = catoutlen;
5578 if(checksumtype=="none" or checksumtype==""):
5579 catfileheadercshex = format(0, 'x').lower();
5580 catfilecontentcshex = format(0, 'x').lower();
5581 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5582 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5583 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5584 elif(checksumtype=="crc16_ccitt"):
5585 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5586 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5587 elif(checksumtype=="adler32"):
5588 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5589 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5590 elif(checksumtype=="crc32"):
5591 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5592 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5593 elif(checksumtype=="crc64_ecma"):
5594 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5595 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5596 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5597 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5598 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5599 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5600 checksumoutstr = hashlib.new(checksumtype);
5601 checksumoutstr.update("".encode('UTF-8'));
5602 catfileheadercshex = checksumoutstr.hexdigest().lower();
5603 checksumoutstr = hashlib.new(checksumtype);
5604 checksumoutstr.update(fcontents);
5605 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5606 else:
5607 catfileheadercshex = format(0, 'x').lower();
5608 catfilecontentcshex = format(0, 'x').lower();
5609 catfhstart = fheadtell;
5610 fheadtell += len(catfileoutstr);
5611 catfhend = fheadtell - 1;
5612 catfcontentstart = fheadtell;
5613 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5614 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5615 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5616 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex];
5617 if(checksumtype=="none" or checksumtype==""):
5618 catfileheadercshex = format(0, 'x').lower();
5619 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5620 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5621 elif(checksumtype=="crc16_ccitt"):
5622 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5623 elif(checksumtype=="adler32"):
5624 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5625 elif(checksumtype=="crc32"):
5626 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5627 elif(checksumtype=="crc64_ecma"):
5628 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5629 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5630 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5631 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5632 checksumoutstr = hashlib.new(checksumtype);
5633 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5634 catfileheadercshex = checksumoutstr.hexdigest().lower();
5635 else:
5636 catfileheadercshex = format(0, 'x').lower();
5637 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5638 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5639 nullstrecd = formatspecs[5].encode('UTF-8');
5640 fheadtell += len(catfileoutstr) + 1;
5641 catfcontentend = fheadtell - 1;
5642 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5643 pyhascontents = False;
5644 if(int(fsize)>0 and not listonly):
5645 pyhascontents = True;
5646 if(int(fsize)>0 and listonly):
5647 fcontents = "";
5648 pyhascontents = False;
5649 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fwinattributes': fwinattributes, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields + 2, 'frawheader': catheaderdata, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
5650 fileidnum = fileidnum + 1;
5651 return catlist;
5653 if(not rarfile_support):
5654 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5655 return False;
5657 if(rarfile_support):
5658 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5659 curinode = 0;
5660 curfid = 0;
5661 inodelist = [];
5662 inodetofile = {};
5663 filetoinode = {};
5664 inodetocatinode = {};
5665 fileidnum = 0;
5666 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
5667 return False;
5668 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
5669 return False;
5670 rarfp = rarfile.RarFile(infile, "r");
5671 rartest = rarfp.testrar();
5672 if(rartest):
5673 VerbosePrintOut("Bad file found!");
5674 fnumfiles = int(len(rarfp.infolist()));
5675 catver = formatspecs[6];
5676 fileheaderver = str(int(catver.replace(".", "")));
5677 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5678 catversion = re.findall(r"([\d]+)$", fileheader);
5679 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5680 fnumfileshex = format(int(fnumfiles), 'x').lower();
5681 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5682 if(checksumtype=="none" or checksumtype==""):
5683 catfileheadercshex = format(0, 'x').lower();
5684 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5685 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5686 elif(checksumtype=="crc16_ccitt"):
5687 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5688 elif(checksumtype=="adler32"):
5689 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5690 elif(checksumtype=="crc32"):
5691 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5692 elif(checksumtype=="crc64_ecma"):
5693 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5694 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5695 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5696 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5697 checksumoutstr = hashlib.new(checksumtype);
5698 checksumoutstr.update(fileheader.encode('UTF-8'));
5699 catfileheadercshex = checksumoutstr.hexdigest().lower();
5700 else:
5701 catfileheadercshex = format(0, 'x').lower();
5702 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5703 fheadtell = len(fileheader);
5704 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5705 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
5706 is_unix = False;
5707 is_windows = False;
5708 if(member.host_os==rarfile.RAR_OS_UNIX):
5709 is_windows = False;
5710 try:
5711 member.external_attr
5712 is_unix = True;
5713 except AttributeError:
5714 is_unix = False;
5715 elif(member.host_os==rarfile.RAR_OS_WIN32):
5716 is_unix = False;
5717 try:
5718 member.external_attr
5719 is_windows = True;
5720 except AttributeError:
5721 is_windows = False;
5722 else:
5723 is_unix = False;
5724 is_windows = False;
5725 if(re.findall("^[.|/]", member.filename)):
5726 fname = member.filename;
5727 else:
5728 fname = "./"+member.filename;
5729 rarinfo = rarfp.getinfo(member.filename);
5730 if(verbose):
5731 VerbosePrintOut(fname);
5732 if(is_unix and member.external_attr !=0):
5733 fpremode = int(member.external_attr);
5734 elif(member.is_file()):
5735 fpremode = stat.S_IFREG + 438;
5736 elif(member.is_symlink()):
5737 fpremode = stat.S_IFLNK + 438;
5738 elif(member.is_dir()):
5739 fpremode = stat.S_IFDIR + 511;
5740 if(is_windows and member.external_attr !=0):
5741 fwinattributes = int(member.external_attr);
5742 else:
5743 fwinattributes = int(0);
5744 flinkcount = 0;
5745 ftype = 0;
5746 if(member.is_file()):
5747 ftype = 0;
5748 elif(member.is_symlink()):
5749 ftype = 2;
5750 elif(member.is_dir()):
5751 ftype = 5;
5752 flinkname = "";
5753 if(ftype==2):
5754 flinkname = rarfp.read(member.filename).decode("UTF-8");
5755 fbasedir = os.path.dirname(fname);
5756 fcurfid = curfid;
5757 fcurinode = curfid;
5758 finode = fcurinode;
5759 curfid = curfid + 1;
5760 fdev_minor = 0;
5761 fdev_major = 0;
5762 frdev_minor = 0;
5763 frdev_major = 0;
5764 if(ftype==5):
5765 fsize = "0";
5766 if(ftype==0):
5767 fsize = member.file_size;
5768 try:
5769 if(member.atime):
5770 fatime = int(member.atime.timestamp());
5771 else:
5772 fatime = int(member.mtime.timestamp());
5773 except AttributeError:
5774 fatime = int(member.mtime.timestamp());
5775 fmtime = int(member.mtime.timestamp());
5776 try:
5777 if(member.ctime):
5778 fctime = int(member.ctime.timestamp());
5779 else:
5780 fctime = int(member.mtime.timestamp());
5781 except AttributeError:
5782 fctime = int(member.mtime.timestamp());
5783 fbtime = int(member.mtime.timestamp());
5784 if(is_unix and member.external_attr !=0):
5785 fmode = int(member.external_attr);
5786 fchmode = int(stat.S_IMODE(member.external_attr));
5787 ftypemod = int(stat.S_IFMT(member.external_attr));
5788 elif(member.is_file()):
5789 fmode = int(stat.S_IFREG + 438)
5790 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
5791 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
5792 elif(member.is_symlink()):
5793 fmode = int(stat.S_IFLNK + 438)
5794 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
5795 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
5796 elif(member.is_dir()):
5797 fmode = int(stat.S_IFDIR + 511)
5798 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
5799 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
5800 try:
5801 fuid = os.getuid();
5802 except AttributeError:
5803 fuid = 0;
5804 except KeyError:
5805 fuid = 0;
5806 try:
5807 fgid = os.getgid();
5808 except AttributeError:
5809 fgid = 0;
5810 except KeyError:
5811 fgid = 0;
5812 try:
5813 import pwd;
5814 try:
5815 userinfo = pwd.getpwuid(os.getuid());
5816 funame = userinfo.pw_name;
5817 except KeyError:
5818 funame = "";
5819 except AttributeError:
5820 funame = "";
5821 except ImportError:
5822 funame = "";
5823 fgname = "";
5824 try:
5825 import grp;
5826 try:
5827 groupinfo = grp.getgrgid(os.getgid());
5828 fgname = groupinfo.gr_name;
5829 except KeyError:
5830 fgname = "";
5831 except AttributeError:
5832 fgname = "";
5833 except ImportError:
5834 fgname = "";
5835 fcontents = "".encode('UTF-8');
5836 if(ftype==0):
5837 fcontents = rarfp.read(member.filename);
5838 ftypehex = format(ftype, 'x').lower();
5839 extrafields = len(extradata);
5840 extrafieldslist = extradata;
5841 catfextrafields = extrafields;
5842 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5843 if(len(extradata)>0):
5844 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5845 extrasizelen = len(extrasizestr);
5846 extrasizelenhex = format(extrasizelen, 'x').lower();
5847 catoutlist = [ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fwinattributes), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()];
5848 catoutlen = len(catoutlist) + len(extradata) + 3;
5849 catoutlenhex = format(catoutlen, 'x').lower();
5850 catoutlist.insert(0, catoutlenhex);
5851 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5852 if(len(extradata)>0):
5853 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5854 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5855 catfnumfields = 24 + catfextrafields;
5856 if(checksumtype=="none" or checksumtype==""):
5857 catfileheadercshex = format(0, 'x').lower();
5858 catfilecontentcshex = format(0, 'x').lower();
5859 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5860 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5861 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5862 elif(checksumtype=="crc16_ccitt"):
5863 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5864 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5865 elif(checksumtype=="adler32"):
5866 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5867 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5868 elif(checksumtype=="crc32"):
5869 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5870 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5871 elif(checksumtype=="crc64_ecma"):
5872 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5873 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5874 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5875 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5876 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5877 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5878 checksumoutstr = hashlib.new(checksumtype);
5879 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5880 catfileheadercshex = checksumoutstr.hexdigest().lower();
5881 else:
5882 catfileheadercshex = format(0, 'x').lower();
5883 catfilecontentcshex = format(0, 'x').lower();
5884 catfhstart = fheadtell;
5885 fheadtell += len(catfileoutstr);
5886 catfhend = fheadtell - 1;
5887 catfcontentstart = fheadtell;
5888 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5889 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5890 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5891 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex];
5892 if(checksumtype=="none" or checksumtype==""):
5893 catfileheadercshex = format(0, 'x').lower();
5894 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5895 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5896 elif(checksumtype=="crc16_ccitt"):
5897 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5898 elif(checksumtype=="adler32"):
5899 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5900 elif(checksumtype=="crc32"):
5901 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5902 elif(checksumtype=="crc64_ecma"):
5903 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5904 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5905 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5906 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5907 checksumoutstr = hashlib.new(checksumtype);
5908 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5909 catfileheadercshex = checksumoutstr.hexdigest().lower();
5910 else:
5911 catfileheadercshex = format(0, 'x').lower();
5912 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5913 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5914 nullstrecd = formatspecs[5].encode('UTF-8');
5915 fheadtell += len(catfileoutstr) + 1;
5916 catfcontentend = fheadtell - 1;
5917 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5918 pyhascontents = False;
5919 if(int(fsize)>0 and not listonly):
5920 pyhascontents = True;
5921 if(int(fsize)>0 and listonly):
5922 fcontents = "";
5923 pyhascontents = False;
5924 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fwinattributes': fwinattributes, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields + 2, 'frawheader': catheaderdata, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
5925 fileidnum = fileidnum + 1;
5926 return catlist;
5928 if(not py7zr_support):
5929 def SevenZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5930 return False;
5932 if(py7zr_support):
5933 def SevenZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5934 curinode = 0;
5935 curfid = 0;
5936 inodelist = [];
5937 inodetofile = {};
5938 filetoinode = {};
5939 inodetocatinode = {};
5940 fileidnum = 0;
5941 szpfp = py7zr.SevenZipFile(infile, mode="r");
5942 file_content = szpfp.readall();
5943 #sztest = szpfp.testzip();
5944 sztestalt = szpfp.test();
5945 if(sztestalt):
5946 VerbosePrintOut("Bad file found!");
5947 numfiles = int(len(szpfp.list()));
5948 catver = formatspecs[6];
5949 fileheaderver = str(int(catver.replace(".", "")));
5950 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5951 catversion = re.findall(r"([\d]+)$", fileheader);
5952 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5953 fnumfileshex = format(int(fnumfiles), 'x').lower();
5954 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5955 if(checksumtype=="none" or checksumtype==""):
5956 catfileheadercshex = format(0, 'x').lower();
5957 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5958 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5959 elif(checksumtype=="crc16_ccitt"):
5960 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5961 elif(checksumtype=="adler32"):
5962 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5963 elif(checksumtype=="crc32"):
5964 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5965 elif(checksumtype=="crc64_ecma"):
5966 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5967 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5968 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5969 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5970 checksumoutstr = hashlib.new(checksumtype);
5971 checksumoutstr.update(fileheader.encode('UTF-8'));
5972 catfileheadercshex = checksumoutstr.hexdigest().lower();
5973 else:
5974 catfileheadercshex = format(0, 'x').lower();
5975 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5976 fheadtell = len(fileheader);
5977 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5978 for member in sorted(szpfp.list(), key=lambda x: x.filename):
5979 if(re.findall("^[.|/]", member.filename)):
5980 fname = member.filename;
5981 else:
5982 fname = "./"+member.filename;
5983 if(not member.is_directory):
5984 fpremode = int(stat.S_IFREG + 438);
5985 elif(member.is_directory):
5986 fpremode = int(stat.S_IFDIR + 511);
5987 fwinattributes = int(0);
5988 flinkcount = 0;
5989 ftype = 0;
5990 if(member.is_directory):
5991 ftype = 5;
5992 else:
5993 ftype = 0;
5994 flinkname = "";
5995 fbasedir = os.path.dirname(fname);
5996 fcurfid = curfid;
5997 fcurinode = curfid;
5998 finode = fcurinode;
5999 curfid = curfid + 1;
6000 fdev_minor = 0;
6001 fdev_major = 0;
6002 frdev_minor = 0;
6003 frdev_major = 0;
6004 if(ftype==5):
6005 fsize = "0";
6006 fatime = int(member.creationtime.timestamp());
6007 fmtime = int(member.creationtime.timestamp());
6008 fctime = int(member.creationtime.timestamp());
6009 fbtime = int(member.creationtime.timestamp());
6010 if(member.is_directory):
6011 fmode = int(stat.S_IFDIR + 511)
6012 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
6013 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
6014 else:
6015 fmode = int(stat.S_IFLNK + 438)
6016 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
6017 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
6018 try:
6019 fuid = os.getuid();
6020 except AttributeError:
6021 fuid = 0;
6022 except KeyError:
6023 fuid = 0;
6024 try:
6025 fgid = os.getgid();
6026 except AttributeError:
6027 fgid = 0;
6028 except KeyError:
6029 fgid = 0;
6030 try:
6031 import pwd;
6032 try:
6033 userinfo = pwd.getpwuid(os.getuid());
6034 funame = userinfo.pw_name;
6035 except KeyError:
6036 funame = "";
6037 except AttributeError:
6038 funame = "";
6039 except ImportError:
6040 funame = "";
6041 fgname = "";
6042 try:
6043 import grp;
6044 try:
6045 groupinfo = grp.getgrgid(os.getgid());
6046 fgname = groupinfo.gr_name;
6047 except KeyError:
6048 fgname = "";
6049 except AttributeError:
6050 fgname = "";
6051 except ImportError:
6052 fgname = "";
6053 fcontents = "".encode('UTF-8');
6054 if(ftype==0):
6055 fcontents = file_content[member.filename].read();
6056 fsize = format(len(fcontents), 'x').lower();
6057 fileop.close();
6058 ftypehex = format(ftype, 'x').lower();
6059 extrafields = len(extradata);
6060 extrafieldslist = extradata;
6061 catfextrafields = extrafields;
6062 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
6063 if(len(extradata)>0):
6064 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
6065 extrasizelen = len(extrasizestr);
6066 extrasizelenhex = format(extrasizelen, 'x').lower();
6067 catoutlist = [ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fwinattributes), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()];
6068 catoutlen = len(catoutlist) + len(extradata) + 3;
6069 catoutlenhex = format(catoutlen, 'x').lower();
6070 catoutlist.insert(0, catoutlenhex);
6071 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
6072 if(len(extradata)>0):
6073 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
6074 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
6075 catfnumfields = 24 + catfextrafields;
6076 if(checksumtype=="none" or checksumtype==""):
6077 catfileheadercshex = format(0, 'x').lower();
6078 catfilecontentcshex = format(0, 'x').lower();
6079 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6080 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6081 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
6082 elif(checksumtype=="crc16_ccitt"):
6083 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6084 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
6085 elif(checksumtype=="adler32"):
6086 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6087 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
6088 elif(checksumtype=="crc32"):
6089 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6090 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
6091 elif(checksumtype=="crc64_ecma"):
6092 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6093 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
6094 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6095 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6096 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
6097 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6098 checksumoutstr = hashlib.new(checksumtype);
6099 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
6100 catfileheadercshex = checksumoutstr.hexdigest().lower();
6101 else:
6102 catfileheadercshex = format(0, 'x').lower();
6103 catfilecontentcshex = format(0, 'x').lower();
6104 catfhstart = fheadtell;
6105 fheadtell += len(catfileoutstr);
6106 catfhend = fheadtell - 1;
6107 catfcontentstart = fheadtell;
6108 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6109 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
6110 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
6111 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex];
6112 if(checksumtype=="none" or checksumtype==""):
6113 catfileheadercshex = format(0, 'x').lower();
6114 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6115 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6116 elif(checksumtype=="crc16_ccitt"):
6117 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6118 elif(checksumtype=="adler32"):
6119 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6120 elif(checksumtype=="crc32"):
6121 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6122 elif(checksumtype=="crc64_ecma"):
6123 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6124 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6125 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6126 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6127 checksumoutstr = hashlib.new(checksumtype);
6128 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
6129 catfileheadercshex = checksumoutstr.hexdigest().lower();
6130 else:
6131 catfileheadercshex = format(0, 'x').lower();
6132 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6133 catfileoutstrecd = catfileoutstr.encode('UTF-8');
6134 nullstrecd = formatspecs[5].encode('UTF-8');
6135 fheadtell += len(catfileoutstr) + 1;
6136 catfcontentend = fheadtell - 1;
6137 catfileout = catfileoutstrecd + fcontents + nullstrecd;
6138 pyhascontents = False;
6139 if(int(fsize)>0 and not listonly):
6140 pyhascontents = True;
6141 if(int(fsize)>0 and listonly):
6142 fcontents = "";
6143 pyhascontents = False;
6144 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fwinattributes': fwinattributes, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields + 2, 'frawheader': catheaderdata, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
6145 fileidnum = fileidnum + 1;
6146 return catlist;
6148 def ListDirToArray(infiles, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, listonly=False, skipchecksum=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6149 outarray = BytesIO();
6150 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
6151 listcatfiles = ArchiveFileToArray(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6152 return listcatfiles;
6154 def ArchiveFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6155 if(isinstance(infile, dict)):
6156 listcatfiles = infile;
6157 else:
6158 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6159 infile = RemoveWindowsPath(infile);
6160 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6161 if(not listcatfiles):
6162 return False;
6163 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6164 if(returnfp):
6165 catarray.update({'catfp': listcatfiles['catfp']});
6166 lenlist = len(listcatfiles['ffilelist']);
6167 lcfi = 0;
6168 lcfx = int(listcatfiles['fnumfiles']);
6169 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6170 lcfx = int(lenlist);
6171 else:
6172 lcfx = int(listcatfiles['fnumfiles']);
6173 while(lcfi < lcfx):
6174 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6175 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6176 catarray['filetoid'].update(filetoidarray);
6177 catarray['idtofile'].update(idtofilearray);
6178 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6179 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6180 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6181 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6182 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6183 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6184 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6185 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6186 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6187 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6188 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6189 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6190 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6191 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6192 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6193 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6194 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6195 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6196 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6197 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6198 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6199 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6200 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6201 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6202 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6203 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6204 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6205 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6206 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6207 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6208 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6209 lcfi = lcfi + 1;
6210 return catarray;
6212 create_alias_function("", __file_format_name__, "ToArrayIndex", ArchiveFileToArrayIndex);
6214 def ListDirToArrayIndexAlt(infiles, dirlistfromtxt=False, followlink=False, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6215 listcatfiles = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, extradata, formatspecs, verbose);
6216 if(not listcatfiles):
6217 return False;
6218 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6219 lenlist = len(listcatfiles['ffilelist']);
6220 lcfi = 0;
6221 lcfx = int(listcatfiles['fnumfiles']);
6222 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6223 lcfx = int(lenlist);
6224 else:
6225 lcfx = int(listcatfiles['fnumfiles']);
6226 while(lcfi < lcfx):
6227 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6228 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6229 catarray['filetoid'].update(filetoidarray);
6230 catarray['idtofile'].update(idtofilearray);
6231 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6232 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6233 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6234 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6235 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6236 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6237 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6238 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6239 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6240 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6241 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6242 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6243 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6244 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6245 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6246 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6247 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6248 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6249 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6250 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6251 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6252 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6253 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6254 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6255 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6256 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6257 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6258 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6259 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6260 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6261 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6262 lcfi = lcfi + 1;
6263 return catarray;
6265 def TarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6266 listcatfiles = TarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6267 if(not listcatfiles):
6268 return False;
6269 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6270 lenlist = len(listcatfiles['ffilelist']);
6271 lcfi = 0;
6272 lcfx = int(listcatfiles['fnumfiles']);
6273 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6274 lcfx = int(lenlist);
6275 else:
6276 lcfx = int(listcatfiles['fnumfiles']);
6277 while(lcfi < lcfx):
6278 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6279 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6280 catarray['filetoid'].update(filetoidarray);
6281 catarray['idtofile'].update(idtofilearray);
6282 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6283 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6284 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6285 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6286 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6287 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6288 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6289 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6290 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6291 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6292 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6293 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6294 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6295 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6296 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6297 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6298 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6299 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6300 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6301 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6302 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6303 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6304 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6305 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6306 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6307 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6308 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6309 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6310 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6311 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6312 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6313 lcfi = lcfi + 1;
6314 return catarray;
6316 def ZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6317 listcatfiles = ZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6318 if(not listcatfiles):
6319 return False;
6320 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6321 lenlist = len(listcatfiles['ffilelist']);
6322 lcfi = 0;
6323 lcfx = int(listcatfiles['fnumfiles']);
6324 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6325 lcfx = int(lenlist);
6326 else:
6327 lcfx = int(listcatfiles['fnumfiles']);
6328 while(lcfi < lcfx):
6329 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6330 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6331 catarray['filetoid'].update(filetoidarray);
6332 catarray['idtofile'].update(idtofilearray);
6333 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6334 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6335 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6336 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6337 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6338 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6339 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6340 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6341 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6342 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6343 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6344 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6345 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6346 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6347 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6348 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6349 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6350 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6351 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6352 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6353 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6354 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6355 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6356 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6357 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6358 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6359 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6360 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6361 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6362 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6363 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6364 lcfi = lcfi + 1;
6365 return catarray;
6367 if(not rarfile_support):
6368 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6369 return False;
6371 if(rarfile_support):
6372 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6373 listcatfiles = RarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6374 if(not listcatfiles):
6375 return False;
6376 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6377 lenlist = len(listcatfiles['ffilelist']);
6378 lcfi = 0;
6379 lcfx = int(listcatfiles['fnumfiles']);
6380 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6381 lcfx = int(lenlist);
6382 else:
6383 lcfx = int(listcatfiles['fnumfiles']);
6384 while(lcfi < lcfx):
6385 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6386 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6387 catarray['filetoid'].update(filetoidarray);
6388 catarray['idtofile'].update(idtofilearray);
6389 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6390 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6391 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6392 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6393 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6394 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6395 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6396 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6397 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6398 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6399 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6400 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6401 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6402 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6403 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6404 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6405 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6406 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6407 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6408 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6409 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6410 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6411 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6412 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6413 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6414 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6415 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6416 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6417 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6418 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6419 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6420 lcfi = lcfi + 1;
6421 return catarray;
6423 if(not py7zr_support):
6424 def SevenZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6425 return False;
6427 if(py7zr_support):
6428 def SevenZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6429 listcatfiles = SevenZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6430 if(not listcatfiles):
6431 return False;
6432 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6433 lenlist = len(listcatfiles['ffilelist']);
6434 lcfi = 0;
6435 lcfx = int(listcatfiles['fnumfiles']);
6436 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6437 lcfx = int(lenlist);
6438 else:
6439 lcfx = int(listcatfiles['fnumfiles']);
6440 while(lcfi < lcfx):
6441 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6442 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6443 catarray['filetoid'].update(filetoidarray);
6444 catarray['idtofile'].update(idtofilearray);
6445 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6446 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6447 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6448 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6449 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6450 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6451 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6452 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6453 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6454 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6455 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6456 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6457 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6458 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6459 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6460 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6461 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6462 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6463 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6464 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6465 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6466 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6467 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6468 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6469 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6470 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6471 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6472 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6473 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6474 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6475 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6476 lcfi = lcfi + 1;
6477 return catarray;
6479 def ArchiveFileStringToArrayIndex(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6480 catfp = BytesIO(catstr);
6481 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6482 return listcatfiles;
6484 create_alias_function("", __file_format_name__, "StringToArrayIndex", ArchiveFileStringToArrayIndex);
6486 def TarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6487 catfp = BytesIO();
6488 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6489 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6490 return listcatfiles;
6492 def ZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6493 catfp = BytesIO();
6494 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6495 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6496 return listcatfiles;
6498 if(not rarfile_support):
6499 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6500 return False;
6502 if(rarfile_support):
6503 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6504 catfp = BytesIO();
6505 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6506 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6507 return listcatfiles;
6509 if(not rarfile_support):
6510 def SevenZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6511 return False;
6513 if(rarfile_support):
6514 def SevenZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6515 catfp = BytesIO();
6516 catfp = PackArchiveFileFromSevenZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6517 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6518 return listcatfiles;
6520 def ListDirToArrayIndex(infiles, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, listonly=False, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
6521 outarray = BytesIO();
6522 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, verbose, True);
6523 listcatfiles = ArchiveFileToArrayIndex(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
6524 return listcatfiles;
6526 def RePackArchiveFile(infile, outfile, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6527 if(isinstance(infile, dict)):
6528 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6529 listcatfiles = prelistcatfiles['list'];
6530 else:
6531 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6532 infile = RemoveWindowsPath(infile);
6533 if(followlink):
6534 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6535 listcatfiles = prelistcatfiles['list'];
6536 else:
6537 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6538 if(outfile!="-" and not hasattr(infile, "read") and not hasattr(outfile, "write")):
6539 outfile = RemoveWindowsPath(outfile);
6540 checksumtype = checksumtype.lower();
6541 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
6542 checksumtype="crc32";
6543 if(checksumtype=="none"):
6544 checksumtype = "";
6545 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
6546 compression = None;
6547 if(compression not in compressionlist and compression is None):
6548 compression = "auto";
6549 if(verbose):
6550 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6551 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
6552 if(os.path.exists(outfile)):
6553 os.unlink(outfile);
6554 if(not listcatfiles):
6555 return False;
6556 if(outfile=="-"):
6557 verbose = False;
6558 catfp = BytesIO();
6559 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
6560 catfp = outfile;
6561 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
6562 catfp = BytesIO();
6563 else:
6564 fbasename = os.path.splitext(outfile)[0];
6565 fextname = os.path.splitext(outfile)[1];
6566 catfp = CompressOpenFile(outfile, compressionlevel);
6567 catver = formatspecs[6];
6568 fileheaderver = str(int(catver.replace(".", "")));
6569 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
6570 catfp.write(fileheader.encode('UTF-8'));
6571 lenlist = len(listcatfiles['ffilelist']);
6572 fnumfiles = int(listcatfiles['fnumfiles']);
6573 if(lenlist>fnumfiles or lenlist<fnumfiles):
6574 fnumfiles = lenlist;
6575 fnumfileshex = format(int(fnumfiles), 'x').lower();
6576 fnumfilesa = AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
6577 if(checksumtype=="none" or checksumtype==""):
6578 catfileheadercshex = format(0, 'x').lower();
6579 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6580 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
6581 elif(checksumtype=="crc16_ccitt"):
6582 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
6583 elif(checksumtype=="adler32"):
6584 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
6585 elif(checksumtype=="crc32"):
6586 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
6587 elif(checksumtype=="crc64_ecma"):
6588 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6589 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6590 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6591 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6592 checksumoutstr = hashlib.new(checksumtype);
6593 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
6594 catfileheadercshex = checksumoutstr.hexdigest().lower();
6595 else:
6596 catfileheadercshex = format(0, 'x').lower();
6597 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
6598 catfp.write(fnumfilesa.encode('UTF-8'));
6599 try:
6600 catfp.flush();
6601 os.fsync(catfp.fileno());
6602 except io.UnsupportedOperation:
6603 pass;
6604 except AttributeError:
6605 pass;
6606 lenlist = len(listcatfiles['ffilelist']);
6607 fnumfiles = int(listcatfiles['fnumfiles']);
6608 lcfi = 0;
6609 lcfx = int(listcatfiles['fnumfiles']);
6610 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6611 lcfx = int(lenlist);
6612 else:
6613 lcfx = int(listcatfiles['fnumfiles']);
6614 curinode = 0;
6615 curfid = 0;
6616 inodelist = [];
6617 inodetofile = {};
6618 filetoinode = {};
6619 reallcfi = 0;
6620 while(lcfi < lcfx):
6621 catfhstart = catfp.tell();
6622 if(re.findall("^[.|/]", listcatfiles['ffilelist'][reallcfi]['fname'])):
6623 fname = listcatfiles['ffilelist'][reallcfi]['fname'];
6624 else:
6625 fname = "./"+listcatfiles['ffilelist'][reallcfi]['fname'];
6626 if(verbose):
6627 VerbosePrintOut(fname);
6628 fheadersize = format(int(listcatfiles['ffilelist'][reallcfi]['fheadersize']), 'x').lower();
6629 fsize = format(int(listcatfiles['ffilelist'][reallcfi]['fsize']), 'x').lower();
6630 flinkname = listcatfiles['ffilelist'][reallcfi]['flinkname'];
6631 fatime = format(int(listcatfiles['ffilelist'][reallcfi]['fatime']), 'x').lower();
6632 fmtime = format(int(listcatfiles['ffilelist'][reallcfi]['fmtime']), 'x').lower();
6633 fctime = format(int(listcatfiles['ffilelist'][reallcfi]['fctime']), 'x').lower();
6634 fbtime = format(int(listcatfiles['ffilelist'][reallcfi]['fbtime']), 'x').lower();
6635 fmode = format(int(listcatfiles['ffilelist'][reallcfi]['fmode']), 'x').lower();
6636 fchmode = format(int(listcatfiles['ffilelist'][reallcfi]['fchmode']), 'x').lower();
6637 fuid = format(int(listcatfiles['ffilelist'][reallcfi]['fuid']), 'x').lower();
6638 funame = listcatfiles['ffilelist'][reallcfi]['funame'];
6639 fgid = format(int(listcatfiles['ffilelist'][reallcfi]['fgid']), 'x').lower();
6640 fgname = listcatfiles['ffilelist'][reallcfi]['fgname'];
6641 finode = listcatfiles['ffilelist'][reallcfi]['finode'];
6642 flinkcount = listcatfiles['ffilelist'][reallcfi]['flinkcount'];
6643 fwinattributes = flinkinfo['ffilelist'][reallcfi]['fwinattributes'];
6644 fdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['fminor']), 'x').lower();
6645 fdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['fmajor']), 'x').lower();
6646 frdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['frminor']), 'x').lower();
6647 frdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['frmajor']), 'x').lower();
6648 if(len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>listcatfiles['ffilelist'][reallcfi]['fextrafields'] and len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>0):
6649 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(listcatfiles['ffilelist'][reallcfi]['fextralist']);
6650 if(len(extradata)>0):
6651 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(extradata);
6652 listcatfiles['ffilelist'][reallcfi]['fextralist'] = extradata;
6653 extrafields = format(int(listcatfiles['ffilelist'][reallcfi]['fextrafields']), 'x').lower();
6654 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
6655 if(len(extradata)>0):
6656 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
6657 extrasizelen = format(len(extrasizestr), 'x').lower();
6658 fcontents = listcatfiles['ffilelist'][reallcfi]['fcontents'];
6659 if(followlink):
6660 if(listcatfiles['ffilelist'][reallcfi]['ftype']==1 or listcatfiles['ffilelist'][reallcfi]['ftype']==2):
6661 getflinkpath = listcatfiles['ffilelist'][reallcfi]['flinkname'];
6662 flinkid = prelistcatfiles['filetoid'][getflinkpath];
6663 flinkinfo = listcatfiles['ffilelist'][flinkid];
6664 fheadersize = format(int(flinkinfo['fheadersize']), 'x').lower();
6665 fsize = format(int(flinkinfo['fsize']), 'x').lower();
6666 flinkname = flinkinfo['flinkname'];
6667 fatime = format(int(flinkinfo['fatime']), 'x').lower();
6668 fmtime = format(int(flinkinfo['fmtime']), 'x').lower();
6669 fctime = format(int(flinkinfo['fctime']), 'x').lower();
6670 fbtime = format(int(flinkinfo['fbtime']), 'x').lower();
6671 fmode = format(int(flinkinfo['fmode']), 'x').lower();
6672 fchmode = format(int(flinkinfo['fchmode']), 'x').lower();
6673 fuid = format(int(flinkinfo['fuid']), 'x').lower();
6674 funame = flinkinfo['funame'];
6675 fgid = format(int(flinkinfo['fgid']), 'x').lower();
6676 fgname = flinkinfo['fgname'];
6677 finode = flinkinfo['finode'];
6678 flinkcount = flinkinfo['flinkcount'];
6679 fwinattributes = flinkinfo['fwinattributes'];
6680 fdev_minor = format(int(flinkinfo['fminor']), 'x').lower();
6681 fdev_major = format(int(flinkinfo['fmajor']), 'x').lower();
6682 frdev_minor = format(int(flinkinfo['frminor']), 'x').lower();
6683 frdev_major = format(int(flinkinfo['frmajor']), 'x').lower();
6684 if(len(flinkinfo['fextralist'])>flinkinfo['fextrafields'] and len(flinkinfo['fextralist'])>0):
6685 flinkinfo['fextrafields'] = len(flinkinfo['fextralist']);
6686 if(len(extradata)>0):
6687 flinkinfo['fextrafields'] = len(extradata);
6688 flinkinfo['fextralist'] = extradata;
6689 extrafields = format(int(flinkinfo['fextrafields']), 'x').lower();
6690 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
6691 if(len(extradata)>0):
6692 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
6693 extrasizelen = format(len(extrasizestr), 'x').lower();
6694 fcontents = flinkinfo['fcontents'];
6695 if(flinkinfo['ftype']!=0 and flinkinfo['ftype']!=7):
6696 try:
6697 fcontents = fcontents.encode('UTF-8');
6698 except AttributeError:
6699 pass;
6700 ftypehex = format(flinkinfo['ftype'], 'x').lower();
6701 else:
6702 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=0 and listcatfiles['ffilelist'][reallcfi]['ftype']!=7):
6703 try:
6704 fcontents = fcontents.encode('UTF-8');
6705 except AttributeError:
6706 pass;
6707 ftypehex = format(listcatfiles['ffilelist'][reallcfi]['ftype'], 'x').lower();
6708 fcurfid = format(curfid, 'x').lower();
6709 if(not followlink and finode!=0):
6710 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=1):
6711 fcurinode = format(int(curinode), 'x').lower();
6712 inodetofile.update({curinode: fname});
6713 filetoinode.update({fname: curinode});
6714 curinode = curinode + 1;
6715 else:
6716 fcurinode = format(int(filetoinode[flinkname]), 'x').lower();
6717 else:
6718 fcurinode = format(int(curinode), 'x').lower();
6719 curinode = curinode + 1;
6720 curfid = curfid + 1;
6721 catoutlist = [ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fwinattributes, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields];
6722 catoutlen = len(catoutlist) + len(extradata) + 3;
6723 catoutlenhex = format(catoutlen, 'x').lower();
6724 catoutlist.insert(0, catoutlenhex);
6725 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
6726 if(listcatfiles['ffilelist'][reallcfi]['fextrafields']>0):
6727 extrafieldslist = [];
6728 exi = 0;
6729 exil = listcatfiles['ffilelist'][reallcfi]['fextrafields'];
6730 while(exi<exil):
6731 extrafieldslist.append(listcatfiles['ffilelist'][reallcfi]['fextralist']);
6732 exi = exi + 1;
6733 catfileoutstr += AppendNullBytes([extrafieldslist], formatspecs[5]);
6734 catfileoutstr += AppendNullBytes([checksumtype], formatspecs[5]);
6735 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
6736 catfcontentstart = catfp.tell() + len(catfileoutstr);
6737 if(checksumtype=="none" or checksumtype==""):
6738 catfileheadercshex = format(0, 'x').lower();
6739 catfilecontentcshex = format(0, 'x').lower();
6740 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6741 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
6742 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
6743 elif(checksumtype=="crc16_ccitt"):
6744 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
6745 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
6746 elif(checksumtype=="adler32"):
6747 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6748 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
6749 elif(checksumtype=="crc32"):
6750 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6751 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
6752 elif(checksumtype=="crc64_ecma"):
6753 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6754 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
6755 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6756 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6757 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
6758 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6759 checksumoutstr = hashlib.new(checksumtype);
6760 checksumoutstr.update("".encode('UTF-8'));
6761 catfileheadercshex = checksumoutstr.hexdigest().lower();
6762 checksumoutstr = hashlib.new(checksumtype);
6763 checksumoutstr.update(fcontents);
6764 catfilecontentcshex = checksumoutstr.hexdigest().lower();
6765 else:
6766 catfileheadercshex = format(0, 'x').lower();
6767 catfilecontentcshex = format(0, 'x').lower();
6768 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6769 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
6770 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
6771 if(checksumtype=="none" or checksumtype==""):
6772 catfileheadercshex = format(0, 'x').lower();
6773 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6774 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6775 elif(checksumtype=="crc16_ccitt"):
6776 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6777 elif(checksumtype=="adler32"):
6778 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6779 elif(checksumtype=="crc32"):
6780 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6781 elif(checksumtype=="crc64_ecma"):
6782 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6783 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6784 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6785 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6786 checksumoutstr = hashlib.new(checksumtype);
6787 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
6788 catfileheadercshex = checksumoutstr.hexdigest().lower();
6789 else:
6790 catfileheadercshex = format(0, 'x').lower();
6791 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6792 catfileoutstrecd = catfileoutstr.encode('UTF-8');
6793 nullstrecd = formatspecs[5].encode('UTF-8');
6794 catfileout = catfileoutstrecd + fcontents + nullstrecd;
6795 catfcontentend = (catfp.tell() - 1) + len(catfileout);
6796 catfp.write(catfileout);
6797 try:
6798 catfp.flush();
6799 os.fsync(catfp.fileno());
6800 except io.UnsupportedOperation:
6801 pass;
6802 except AttributeError:
6803 pass;
6804 lcfi = lcfi + 1;
6805 reallcfi = reallcfi + 1;
6806 if(lcfx>0):
6807 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
6808 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
6809 catfp = CompressArchiveFile(catfp, compression, formatspecs);
6810 try:
6811 catfp.flush();
6812 os.fsync(catfp.fileno());
6813 except io.UnsupportedOperation:
6814 pass;
6815 except AttributeError:
6816 pass;
6817 if(outfile=="-"):
6818 catfp.seek(0, 0);
6819 if(hasattr(sys.stdout, "buffer")):
6820 shutil.copyfileobj(catfp, sys.stdout.buffer);
6821 else:
6822 shutil.copyfileobj(catfp, sys.stdout);
6823 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
6824 catfp = CompressArchiveFile(catfp, compression, formatspecs);
6825 catfp.seek(0, 0);
6826 upload_file_to_internet_file(catfp, outfile);
6827 if(returnfp):
6828 catfp.seek(0, 0);
6829 return catfp;
6830 else:
6831 catfp.close();
6832 return True;
6834 create_alias_function("RePack", __file_format_name__, "", RePackArchiveFile);
6836 def RePackArchiveFileFromString(catstr, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6837 catfp = BytesIO(catstr);
6838 listcatfiles = RePackArchiveFile(catfp, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
6839 return listcatfiles;
6841 create_alias_function("RePack", __file_format_name__, "FromString", RePackArchiveFileFromString);
6843 def PackArchiveFileFromListDir(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, skipchecksum=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6844 outarray = BytesIO();
6845 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
6846 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
6847 return listcatfiles;
6849 create_alias_function("Pack", __file_format_name__, "FromListDir", PackArchiveFileFromListDir);
6851 def ArchiveFileArrayBase64Encode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6852 if(verbose):
6853 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6854 if(isinstance(infile, dict)):
6855 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6856 listcatfiles = prelistcatfiles['list'];
6857 else:
6858 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6859 infile = RemoveWindowsPath(infile);
6860 if(followlink):
6861 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6862 listcatfiles = prelistcatfiles['list'];
6863 else:
6864 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6865 if(not listcatfiles):
6866 return False;
6867 lenlist = len(listcatfiles['ffilelist']);
6868 fnumfiles = int(listcatfiles['fnumfiles']);
6869 lcfi = 0;
6870 lcfx = int(listcatfiles['fnumfiles']);
6871 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6872 lcfx = int(lenlist);
6873 else:
6874 lcfx = int(listcatfiles['fnumfiles']);
6875 while(lcfi<lcfx):
6876 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
6877 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64encode(listcatfiles['ffilelist'][lcfi]['fcontents']).decode("UTF-8");
6878 lcfi = lcfi + 1;
6879 return listcatfiles;
6881 create_alias_function("", __file_format_name__, "ArrayBase64Encode", ArchiveFileArrayBase64Encode);
6883 def ArchiveFileArrayBase64Decode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6884 if(verbose):
6885 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6886 if(isinstance(infile, dict)):
6887 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6888 listcatfiles = prelistcatfiles['list'];
6889 else:
6890 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6891 infile = RemoveWindowsPath(infile);
6892 if(followlink):
6893 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6894 listcatfiles = prelistcatfiles['list'];
6895 else:
6896 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6897 if(not listcatfiles):
6898 return False;
6899 lenlist = len(listcatfiles['ffilelist']);
6900 fnumfiles = int(listcatfiles['fnumfiles']);
6901 lcfi = 0;
6902 lcfx = int(listcatfiles['fnumfiles']);
6903 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6904 lcfx = int(lenlist);
6905 else:
6906 lcfx = int(listcatfiles['fnumfiles']);
6907 while(lcfi<lcfx):
6908 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
6909 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64decode(listcatfiles['ffilelist'][lcfi]['fcontents'].encode("UTF-8"));
6910 lcfi = lcfi + 1;
6911 return listcatfiles;
6913 create_alias_function("", __file_format_name__, "ArrayBase64Decode", ArchiveFileArrayBase64Decode);
6915 def UnPackArchiveFile(infile, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, preservepermissions=True, preservetime=True, verbose=False, returnfp=False):
6916 if(outdir is not None):
6917 outdir = RemoveWindowsPath(outdir);
6918 if(verbose):
6919 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6920 if(isinstance(infile, dict)):
6921 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6922 listcatfiles = prelistcatfiles['list'];
6923 else:
6924 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6925 infile = RemoveWindowsPath(infile);
6926 if(followlink):
6927 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6928 listcatfiles = prelistcatfiles['list'];
6929 else:
6930 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6931 if(not listcatfiles):
6932 return False;
6933 lenlist = len(listcatfiles['ffilelist']);
6934 fnumfiles = int(listcatfiles['fnumfiles']);
6935 lcfi = 0;
6936 lcfx = int(listcatfiles['fnumfiles']);
6937 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6938 lcfx = int(lenlist);
6939 else:
6940 lcfx = int(listcatfiles['fnumfiles']);
6941 while(lcfi<lcfx):
6942 funame = "";
6943 try:
6944 import pwd;
6945 try:
6946 userinfo = pwd.getpwuid(listcatfiles['ffilelist'][lcfi]['fuid']);
6947 funame = userinfo.pw_name;
6948 except KeyError:
6949 funame = "";
6950 except ImportError:
6951 funame = "";
6952 fgname = "";
6953 try:
6954 import grp;
6955 try:
6956 groupinfo = grp.getgrgid(listcatfiles['ffilelist'][lcfi]['fgid']);
6957 fgname = groupinfo.gr_name;
6958 except KeyError:
6959 fgname = "";
6960 except ImportError:
6961 fgname = "";
6962 if(verbose):
6963 VerbosePrintOut(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
6964 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6965 with open(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), "wb") as fpc:
6966 fpc.write(listcatfiles['ffilelist'][lcfi]['fcontents'])
6967 try:
6968 fpc.flush()
6969 os.fsync(fpc.fileno())
6970 except io.UnsupportedOperation:
6971 pass
6972 except AttributeError:
6973 pass
6974 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
6975 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
6976 if(preservepermissions):
6977 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
6978 if(preservetime):
6979 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
6980 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6981 if(followlink):
6982 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
6983 flinkid = prelistcatfiles['filetoid'][getflinkpath];
6984 flinkinfo = listcatfiles['ffilelist'][flinkid];
6985 funame = "";
6986 try:
6987 import pwd;
6988 try:
6989 userinfo = pwd.getpwuid(flinkinfo['fuid']);
6990 funame = userinfo.pw_name;
6991 except KeyError:
6992 funame = "";
6993 except ImportError:
6994 funame = "";
6995 fgname = "";
6996 try:
6997 import grp;
6998 try:
6999 groupinfo = grp.getgrgid(flinkinfo['fgid']);
7000 fgname = groupinfo.gr_name;
7001 except KeyError:
7002 fgname = "";
7003 except ImportError:
7004 fgname = "";
7005 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
7006 with open(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), "wb") as fpc:
7007 fpc.write(flinkinfo['fcontents'])
7008 try:
7009 fpc.flush()
7010 os.fsync(fpc.fileno())
7011 except io.UnsupportedOperation:
7012 pass
7013 except AttributeError:
7014 pass
7015 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7016 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7017 if(preservepermissions):
7018 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7019 if(preservetime):
7020 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7021 if(flinkinfo['ftype']==1):
7022 os.link(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7023 if(flinkinfo['ftype']==2):
7024 os.symlink(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7025 if(flinkinfo['ftype']==5):
7026 if(preservepermissions):
7027 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7028 else:
7029 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7030 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7031 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7032 if(preservepermissions):
7033 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7034 if(preservetime):
7035 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7036 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
7037 os.mkfifo(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7038 else:
7039 os.link(listcatfiles['ffilelist'][lcfi]['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7040 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
7041 if(followlink):
7042 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
7043 flinkid = prelistcatfiles['filetoid'][getflinkpath];
7044 flinkinfo = listcatfiles['ffilelist'][flinkid];
7045 funame = "";
7046 try:
7047 import pwd;
7048 try:
7049 userinfo = pwd.getpwuid(flinkinfo['fuid']);
7050 funame = userinfo.pw_name;
7051 except KeyError:
7052 funame = "";
7053 except ImportError:
7054 funame = "";
7055 fgname = "";
7056 try:
7057 import grp;
7058 try:
7059 groupinfo = grp.getgrgid(flinkinfo['fgid']);
7060 fgname = groupinfo.gr_name;
7061 except KeyError:
7062 fgname = "";
7063 except ImportError:
7064 fgname = "";
7065 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
7066 with open(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), "wb") as fpc:
7067 fpc.write(flinkinfo['fcontents'])
7068 try:
7069 fpc.flush()
7070 os.fsync(fpc.fileno())
7071 except io.UnsupportedOperation:
7072 pass
7073 except AttributeError:
7074 pass
7075 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7076 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7077 if(preservepermissions):
7078 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7079 if(preservetime):
7080 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7081 if(flinkinfo['ftype']==1):
7082 os.link(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7083 if(flinkinfo['ftype']==2):
7084 os.symlink(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7085 if(flinkinfo['ftype']==5):
7086 if(preservepermissions):
7087 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7088 else:
7089 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7090 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7091 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7092 if(preservepermissions):
7093 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7094 if(preservetime):
7095 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7096 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
7097 os.mkfifo(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7098 else:
7099 os.symlink(listcatfiles['ffilelist'][lcfi]['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7100 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
7101 if(preservepermissions):
7102 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7103 else:
7104 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7105 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
7106 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
7107 if(preservepermissions):
7108 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7109 if(preservetime):
7110 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
7111 if(listcatfiles['ffilelist'][lcfi]['ftype']==6 and hasattr(os, "mkfifo")):
7112 os.mkfifo(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7113 lcfi = lcfi + 1;
7114 if(returnfp):
7115 return listcatfiles['ffilelist']['catfp'];
7116 else:
7117 return True;
7119 create_alias_function("UnPack", __file_format_name__, "", UnPackArchiveFile);
7121 if(hasattr(shutil, "register_unpack_format")):
7122 def UnPackArchiveFileFunc(archive_name, extract_dir=None, **kwargs):
7123 return UnPackArchiveFile(archive_name, extract_dir, False, 0, 0, False, __file_format_delimiter__, False, False);
7124 create_alias_function("UnPack", __file_format_name__, "Func", UnPackArchiveFileFunc);
7126 def UnPackArchiveFileString(catstr, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7127 catfp = BytesIO(catstr);
7128 listcatfiles = UnPackArchiveFile(catfp, outdir, followlink, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7129 return listcatfiles;
7131 create_alias_function("UnPack", __file_format_name__, "String", UnPackArchiveFileString);
7133 def ArchiveFileListFiles(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7134 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7135 if(isinstance(infile, dict)):
7136 listcatfiles = infile;
7137 else:
7138 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
7139 infile = RemoveWindowsPath(infile);
7140 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, returnfp);
7141 if(not listcatfiles):
7142 return False;
7143 lenlist = len(listcatfiles['ffilelist']);
7144 fnumfiles = int(listcatfiles['fnumfiles']);
7145 lcfi = 0;
7146 lcfx = int(listcatfiles['fnumfiles']);
7147 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
7148 lcfx = int(lenlist);
7149 else:
7150 lcfx = int(listcatfiles['fnumfiles']);
7151 returnval = {};
7152 while(lcfi<lcfx):
7153 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
7154 if(not verbose):
7155 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
7156 if(verbose):
7157 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7158 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
7159 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
7160 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7161 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
7162 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7163 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
7164 if(len(fuprint)<=0):
7165 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
7166 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
7167 if(len(fgprint)<=0):
7168 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
7169 VerbosePrintOut(PrintPermissionString(listcatfiles['ffilelist'][lcfi]['fmode'], listcatfiles['ffilelist'][lcfi]['ftype']) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(listcatfiles['ffilelist'][lcfi]['fsize']).rjust(15) + " " + datetime.datetime.utcfromtimestamp(listcatfiles['ffilelist'][lcfi]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname));
7170 lcfi = lcfi + 1;
7171 if(returnfp):
7172 return listcatfiles['catfp'];
7173 else:
7174 return True;
7176 create_alias_function("", __file_format_name__, "ListFiles", ArchiveFileListFiles);
7178 def ArchiveFileStringListFiles(catstr, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7179 catfp = BytesIO(catstr);
7180 listcatfiles = ArchiveFileListFiles(catstr, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7181 return listcatfiles;
7183 create_alias_function("", __file_format_name__, "StringListFiles", ArchiveFileListFiles);
7185 def ArchiveFileListFilesAlt(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7186 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7187 if(isinstance(infile, dict)):
7188 listcatfiles = infile;
7189 else:
7190 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
7191 infile = RemoveWindowsPath(infile);
7192 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, returnfp);
7193 if(not listcatfiles):
7194 return False;
7195 lenlist = len(listcatfiles['ffilelist']);
7196 fnumfiles = int(listcatfiles['fnumfiles']);
7197 lcfi = 0;
7198 lcfx = int(listcatfiles['fnumfiles']);
7199 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
7200 lcfx = int(lenlist);
7201 else:
7202 lcfx = int(listcatfiles['fnumfiles']);
7203 returnval = {};
7204 while(lcfi<lcfx):
7205 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
7206 if(not verbose):
7207 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
7208 if(verbose):
7209 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7210 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
7211 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
7212 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7213 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
7214 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7215 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
7216 if(len(fuprint)<=0):
7217 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
7218 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
7219 if(len(fgprint)<=0):
7220 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
7221 VerbosePrintOut(PrintPermissionString(listcatfiles['ffilelist'][lcfi]['fmode'], listcatfiles['ffilelist'][lcfi]['ftype']) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(listcatfiles['ffilelist'][lcfi]['fsize']).rjust(15) + " " + datetime.datetime.utcfromtimestamp(listcatfiles['ffilelist'][lcfi]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname));
7222 lcfi = lcfi + 1;
7223 if(returnfp):
7224 return listcatfiles['catfp'];
7225 else:
7226 return True;
7228 create_alias_function("", __file_format_name__, "ListFilesAlt", ArchiveFileListFilesAlt);
7230 def ArchiveFileStringListFilesAlt(catstr, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7231 catfp = BytesIO(catstr);
7232 listcatfiles = ArchiveFileListFilesAlt(catstr, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7233 return listcatfiles;
7235 create_alias_function("", __file_format_name__, "StringListFilesAlt", ArchiveFileListFilesAlt);
7237 def TarFileListFiles(infile, verbose=False, returnfp=False):
7238 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7239 if(infile=="-"):
7240 infile = BytesIO();
7241 if(hasattr(sys.stdin, "buffer")):
7242 shutil.copyfileobj(sys.stdin.buffer, infile);
7243 else:
7244 shutil.copyfileobj(sys.stdin, infile);
7245 infile.seek(0, 0);
7246 if(not infile):
7247 return False;
7248 infile.seek(0, 0);
7249 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
7250 infile = download_file_from_internet_file(infile);
7251 infile.seek(0, 0);
7252 if(not infile):
7253 return False;
7254 infile.seek(0, 0);
7255 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
7256 return False;
7257 elif(os.path.exists(infile) and os.path.isfile(infile)):
7258 try:
7259 if(not tarfile.is_tarfile(infile)):
7260 return False;
7261 except AttributeError:
7262 if(not is_tarfile(infile)):
7263 return False;
7264 else:
7265 return False;
7266 try:
7267 if(hasattr(infile, "read") or hasattr(infile, "write")):
7268 tarfp = tarfile.open(fileobj=infile, mode="r");
7269 else:
7270 tarfp = tarfile.open(infile, "r");
7271 except FileNotFoundError:
7272 return False;
7273 lcfi = 0
7274 returnval = {};
7275 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
7276 returnval.update({lcfi: member.name});
7277 fpremode = member.mode;
7278 ffullmode = member.mode;
7279 flinkcount = 0;
7280 ftype = 0;
7281 if(member.isreg()):
7282 ffullmode = member.mode + stat.S_IFREG;
7283 ftype = 0;
7284 elif(member.isdev()):
7285 ffullmode = member.mode;
7286 ftype = 7;
7287 elif(member.islnk()):
7288 ffullmode = member.mode + stat.S_IFREG;
7289 ftype = 1;
7290 elif(member.issym()):
7291 ffullmode = member.mode + stat.S_IFLNK;
7292 ftype = 2;
7293 elif(member.ischr()):
7294 ffullmode = member.mode + stat.S_IFCHR;
7295 ftype = 3;
7296 elif(member.isblk()):
7297 ffullmode = member.mode + stat.S_IFBLK;
7298 ftype = 4;
7299 elif(member.isdir()):
7300 ffullmode = member.mode + stat.S_IFDIR;
7301 ftype = 5;
7302 elif(member.isfifo()):
7303 ffullmode = member.mode + stat.S_IFIFO;
7304 ftype = 6;
7305 elif(member.issparse()):
7306 ffullmode = member.mode;
7307 ftype = 12;
7308 if(not verbose):
7309 VerbosePrintOut(member.name);
7310 elif(verbose):
7311 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7312 printfname = member.name;
7313 if(member.islnk()):
7314 printfname = member.name + " link to " + member.linkname;
7315 elif(member.issym()):
7316 printfname = member.name + " -> " + member.linkname;
7317 fuprint = member.uname;
7318 if(len(fuprint)<=0):
7319 fuprint = member.uid;
7320 fgprint = member.gname;
7321 if(len(fgprint)<=0):
7322 fgprint = member.gid;
7323 VerbosePrintOut(PrintPermissionString(ffullmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.size).rjust(15) + " " + datetime.datetime.utcfromtimestamp(member.mtime).strftime('%Y-%m-%d %H:%M') + " " + printfname));
7324 lcfi = lcfi + 1;
7325 if(returnfp):
7326 return listcatfiles['catfp'];
7327 else:
7328 return True;
7330 def ZipFileListFiles(infile, verbose=False, returnfp=False):
7331 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7332 if(infile=="-"):
7333 infile = BytesIO();
7334 if(hasattr(sys.stdin, "buffer")):
7335 shutil.copyfileobj(sys.stdin.buffer, infile);
7336 else:
7337 shutil.copyfileobj(sys.stdin, infile);
7338 infile.seek(0, 0);
7339 if(not infile):
7340 return False;
7341 infile.seek(0, 0);
7342 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
7343 infile = download_file_from_internet_file(infile);
7344 infile.seek(0, 0);
7345 if(not infile):
7346 return False;
7347 infile.seek(0, 0);
7348 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
7349 return False;
7350 else:
7351 return False;
7352 if(not zipfile.is_zipfile(infile)):
7353 return False;
7354 try:
7355 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
7356 except FileNotFoundError:
7357 return False;
7358 lcfi = 0;
7359 returnval = {};
7360 ziptest = zipfp.testzip();
7361 if(ziptest):
7362 VerbosePrintOut("Bad file found!");
7363 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
7364 if(zipinfo.create_system==0 or zipinfo.create_system==10):
7365 fwinattributes = int(zipinfo.external_attr);
7366 if(not member.is_dir()):
7367 fmode = int(stat.S_IFREG + 438);
7368 fchmode = int(stat.S_IMODE(fmode));
7369 ftypemod = int(stat.S_IFMT(fmode));
7370 elif(member.is_dir()):
7371 fmode = int(stat.S_IFDIR + 511);
7372 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7373 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7374 elif(zipinfo.create_system==3):
7375 fwinattributes =int(0);
7376 fmode = int(zipinfo.external_attr);
7377 fchmode = int(stat.S_IMODE(fmode));
7378 ftypemod = int(stat.S_IFMT(fmode));
7379 else:
7380 fwinattributes = int(0);
7381 if(not member.is_dir()):
7382 fmode = int(stat.S_IFREG + 438);
7383 fchmode = int(stat.S_IMODE(fmode));
7384 ftypemod = int(stat.S_IFMT(fmode));
7385 elif(member.is_dir()):
7386 fmode = int(stat.S_IFDIR + 511);
7387 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7388 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7389 returnval.update({lcfi: member.filename});
7390 if(not verbose):
7391 VerbosePrintOut(member.filename);
7392 if(verbose):
7393 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7394 permissionstr = "";
7395 for fmodval in str(oct(fmode))[-3:]:
7396 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
7397 if(not member.is_dir()):
7398 ftype = 0;
7399 permissionstr = "-" + permissionstr;
7400 elif(member.is_dir()):
7401 ftype = 5;
7402 permissionstr = "d" + permissionstr;
7403 printfname = member.filename;
7404 try:
7405 fuid = int(os.getuid());
7406 except AttributeError:
7407 fuid = int(0);
7408 except KeyError:
7409 fuid = int(0);
7410 try:
7411 fgid = int(os.getgid());
7412 except AttributeError:
7413 fgid = int(0);
7414 except KeyError:
7415 fgid = int(0);
7416 try:
7417 import pwd;
7418 try:
7419 userinfo = pwd.getpwuid(os.getuid());
7420 funame = userinfo.pw_name;
7421 except KeyError:
7422 funame = "";
7423 except AttributeError:
7424 funame = "";
7425 except ImportError:
7426 funame = "";
7427 fgname = "";
7428 try:
7429 import grp;
7430 try:
7431 groupinfo = grp.getgrgid(os.getgid());
7432 fgname = groupinfo.gr_name;
7433 except KeyError:
7434 fgname = "";
7435 except AttributeError:
7436 fgname = "";
7437 except ImportError:
7438 fgname = "";
7439 fuprint = funame;
7440 if(len(fuprint)<=0):
7441 fuprint = str(fuid);
7442 fgprint = fgname;
7443 if(len(fgprint)<=0):
7444 fgprint = str(fgid);
7445 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + datetime.datetime.utcfromtimestamp(int(time.mktime(member.date_time + (0, 0, -1)))).strftime('%Y-%m-%d %H:%M') + " " + printfname));
7446 lcfi = lcfi + 1;
7447 if(returnfp):
7448 return listcatfiles['catfp'];
7449 else:
7450 return True;
7452 if(not rarfile_support):
7453 def RarFileListFiles(infile, verbose=False, returnfp=False):
7454 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7455 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7456 return False;
7458 if(rarfile_support):
7459 def RarFileListFiles(infile, verbose=False, returnfp=False):
7460 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7461 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7462 return False;
7463 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
7464 return False;
7465 lcfi = 0;
7466 returnval = {};
7467 rarfp = rarfile.RarFile(infile, "r");
7468 rartest = rarfp.testrar();
7469 if(rartest):
7470 VerbosePrintOut("Bad file found!");
7471 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
7472 is_unix = False;
7473 is_windows = False;
7474 if(member.host_os==rarfile.RAR_OS_UNIX):
7475 is_windows = False;
7476 try:
7477 member.external_attr
7478 is_unix = True;
7479 except AttributeError:
7480 is_unix = False;
7481 elif(member.host_os==rarfile.RAR_OS_WIN32):
7482 is_unix = False;
7483 try:
7484 member.external_attr
7485 is_windows = True;
7486 except AttributeError:
7487 is_windows = False;
7488 else:
7489 is_unix = False;
7490 is_windows = False;
7491 if(is_unix and member.external_attr !=0):
7492 fpremode = int(member.external_attr);
7493 elif(member.is_file()):
7494 fpremode = int(stat.S_IFREG + 438);
7495 elif(member.is_symlink()):
7496 fpremode = int(stat.S_IFLNK + 438);
7497 elif(member.is_dir()):
7498 fpremode = int(stat.S_IFDIR + 511);
7499 if(is_windows and member.external_attr !=0):
7500 fwinattributes = int(member.external_attr);
7501 else:
7502 fwinattributes = int(0);
7503 if(is_unix and member.external_attr !=0):
7504 fmode = int(member.external_attr);
7505 fchmode = int(stat.S_IMODE(member.external_attr));
7506 ftypemod = int(stat.S_IFMT(member.external_attr));
7507 elif(member.is_file()):
7508 fmode = int(stat.S_IFREG + 438);
7509 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
7510 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
7511 elif(member.is_symlink()):
7512 fmode = int(stat.S_IFLNK + 438);
7513 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
7514 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
7515 elif(member.is_dir()):
7516 fmode = int(stat.S_IFDIR + 511);
7517 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7518 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7519 returnval.update({lcfi: member.filename});
7520 if(not verbose):
7521 VerbosePrintOut(member.filename);
7522 if(verbose):
7523 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7524 permissionstr = "";
7525 for fmodval in str(oct(fmode))[-3:]:
7526 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
7527 if(member.is_file()):
7528 ftype = 0;
7529 permissionstr = "-" + permissionstr;
7530 printfname = member.filename;
7531 elif(member.is_symlink()):
7532 ftype = 2;
7533 permissionstr = "l" + permissionstr;
7534 printfname = member.name + " -> " + member.read().decode("UTF-8");
7535 elif(member.is_dir()):
7536 ftype = 5;
7537 permissionstr = "d" + permissionstr;
7538 printfname = member.filename;
7539 try:
7540 fuid = int(os.getuid());
7541 except AttributeError:
7542 fuid = int(0);
7543 except KeyError:
7544 fuid = int(0);
7545 try:
7546 fgid = int(os.getgid());
7547 except AttributeError:
7548 fgid = int(0);
7549 except KeyError:
7550 fgid = int(0);
7551 try:
7552 import pwd;
7553 try:
7554 userinfo = pwd.getpwuid(os.getuid());
7555 funame = userinfo.pw_name;
7556 except KeyError:
7557 funame = "";
7558 except AttributeError:
7559 funame = "";
7560 except ImportError:
7561 funame = "";
7562 fgname = "";
7563 try:
7564 import grp;
7565 try:
7566 groupinfo = grp.getgrgid(os.getgid());
7567 fgname = groupinfo.gr_name;
7568 except KeyError:
7569 fgname = "";
7570 except AttributeError:
7571 fgname = "";
7572 except ImportError:
7573 fgname = "";
7574 fuprint = funame;
7575 if(len(fuprint)<=0):
7576 fuprint = str(fuid);
7577 fgprint = fgname;
7578 if(len(fgprint)<=0):
7579 fgprint = str(fgid);
7580 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + member.mtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
7581 lcfi = lcfi + 1;
7582 if(returnfp):
7583 return listcatfiles['catfp'];
7584 else:
7585 return True;
7587 if(not py7zr_support):
7588 def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
7589 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7590 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7591 return False;
7593 if(py7zr_support):
7594 def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
7595 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7596 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7597 return False;
7598 lcfi = 0;
7599 returnval = {};
7600 szpfp = py7zr.SevenZipFile(infile, mode="r");
7601 file_content = szpfp.readall();
7602 #sztest = szpfp.testzip();
7603 sztestalt = szpfp.test();
7604 if(sztestalt):
7605 VerbosePrintOut("Bad file found!");
7606 for member in sorted(szpfp.list(), key=lambda x: x.filename):
7607 if(re.findall("^[.|/]", member.filename)):
7608 fname = member.filename;
7609 else:
7610 fname = "./"+member.filename;
7611 if(not member.is_directory):
7612 fpremode = int(stat.S_IFREG + 438);
7613 elif(member.is_directory):
7614 fpremode = int(stat.S_IFDIR + 511);
7615 fwinattributes = int(0);
7616 if(member.is_directory):
7617 fmode = int(stat.S_IFDIR + 511);
7618 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7619 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7620 else:
7621 fmode = int(stat.S_IFLNK + 438);
7622 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
7623 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
7624 returnval.update({lcfi: member.filename});
7625 if(not verbose):
7626 VerbosePrintOut(member.filename);
7627 if(verbose):
7628 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7629 permissionstr = "";
7630 for fmodval in str(oct(fmode))[-3:]:
7631 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
7632 fsize = int("0");
7633 if(not member.is_directory):
7634 ftype = 0;
7635 permissionstr = "-" + permissionstr;
7636 printfname = member.filename;
7637 elif(member.is_directory):
7638 ftype = 5;
7639 permissionstr = "d" + permissionstr;
7640 printfname = member.filename;
7641 if(ftype==0):
7642 fsize = len(file_content[member.filename].read());
7643 file_content[member.filename].close();
7644 try:
7645 fuid = int(os.getuid());
7646 except AttributeError:
7647 fuid = int(0);
7648 except KeyError:
7649 fuid = int(0);
7650 try:
7651 fgid = int(os.getgid());
7652 except AttributeError:
7653 fgid = int(0);
7654 except KeyError:
7655 fgid = int(0);
7656 try:
7657 import pwd;
7658 try:
7659 userinfo = pwd.getpwuid(os.getuid());
7660 funame = userinfo.pw_name;
7661 except KeyError:
7662 funame = "";
7663 except AttributeError:
7664 funame = "";
7665 except ImportError:
7666 funame = "";
7667 fgname = "";
7668 try:
7669 import grp;
7670 try:
7671 groupinfo = grp.getgrgid(os.getgid());
7672 fgname = groupinfo.gr_name;
7673 except KeyError:
7674 fgname = "";
7675 except AttributeError:
7676 fgname = "";
7677 except ImportError:
7678 fgname = "";
7679 fuprint = funame;
7680 if(len(fuprint)<=0):
7681 fuprint = str(fuid);
7682 fgprint = fgname;
7683 if(len(fgprint)<=0):
7684 fgprint = str(fgid);
7685 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(fsize).rjust(15) + " " + member.creationtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
7686 lcfi = lcfi + 1;
7687 if(returnfp):
7688 return listcatfiles['catfp'];
7689 else:
7690 return True;
7692 def ListDirListFiles(infiles, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
7693 outarray = BytesIO();
7694 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, False, True);
7695 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7696 return listcatfiles;
7698 def ListDirListFilesAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=True, seekstart=0, seekend=0, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
7699 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, formatspecs, verbose);
7700 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7701 return listcatfiles;
7703 def PackArchiveFileFromListDirAlt(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, skipchecksum=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7704 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, False, checksumtype, extradata, formatspecs, False);
7705 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, followlink, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
7706 return listcatfiles;
7708 create_alias_function("Pack", __file_format_name__, "FromListDirAlt", PackArchiveFileFromListDirAlt);
7710 def PackArchiveFileFromTarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7711 outarray = TarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7712 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7713 return listcatfiles;
7715 create_alias_function("Pack", __file_format_name__, "FromTarFileAlt", PackArchiveFileFromTarFileAlt);
7717 def PackArchiveFileFromZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7718 outarray = ZipFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7719 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7720 return listcatfiles;
7722 create_alias_function("Pack", __file_format_name__, "FromZipFileAlt", PackArchiveFileFromZipFileAlt);
7724 if(not rarfile_support):
7725 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7726 return False;
7728 if(rarfile_support):
7729 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7730 outarray = RarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7731 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7732 return listcatfiles;
7734 create_alias_function("Pack", __file_format_name__, "FromRarFileAlt", PackArchiveFileFromRarFileAlt);
7736 def download_file_from_ftp_file(url):
7737 urlparts = urlparse(url);
7738 file_name = os.path.basename(urlparts.path);
7739 file_dir = os.path.dirname(urlparts.path);
7740 if(urlparts.username is not None):
7741 ftp_username = urlparts.username;
7742 else:
7743 ftp_username = "anonymous";
7744 if(urlparts.password is not None):
7745 ftp_password = urlparts.password;
7746 elif(urlparts.password is None and urlparts.username=="anonymous"):
7747 ftp_password = "anonymous";
7748 else:
7749 ftp_password = "";
7750 if(urlparts.scheme=="ftp"):
7751 ftp = FTP();
7752 elif(urlparts.scheme=="ftps"):
7753 ftp = FTP_TLS();
7754 else:
7755 return False;
7756 if(urlparts.scheme=="sftp"):
7757 if(__use_pysftp__):
7758 return download_file_from_pysftp_file(url);
7759 else:
7760 return download_file_from_sftp_file(url);
7761 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
7762 return download_file_from_http_file(url);
7763 ftp_port = urlparts.port;
7764 if(urlparts.port is None):
7765 ftp_port = 21;
7766 try:
7767 ftp.connect(urlparts.hostname, ftp_port);
7768 except socket.gaierror:
7769 log.info("Error With URL "+url);
7770 return False;
7771 except socket.timeout:
7772 log.info("Error With URL "+url);
7773 return False;
7774 ftp.login(urlparts.username, urlparts.password);
7775 if(urlparts.scheme=="ftps"):
7776 ftp.prot_p();
7777 ftpfile = BytesIO();
7778 ftp.retrbinary("RETR "+urlparts.path, ftpfile.write);
7779 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7780 ftp.close();
7781 ftpfile.seek(0, 0);
7782 return ftpfile;
7784 def download_file_from_ftp_string(url):
7785 ftpfile = download_file_from_ftp_file(url);
7786 return ftpfile.read();
7788 def upload_file_to_ftp_file(ftpfile, url):
7789 urlparts = urlparse(url);
7790 file_name = os.path.basename(urlparts.path);
7791 file_dir = os.path.dirname(urlparts.path);
7792 if(urlparts.username is not None):
7793 ftp_username = urlparts.username;
7794 else:
7795 ftp_username = "anonymous";
7796 if(urlparts.password is not None):
7797 ftp_password = urlparts.password;
7798 elif(urlparts.password is None and urlparts.username=="anonymous"):
7799 ftp_password = "anonymous";
7800 else:
7801 ftp_password = "";
7802 if(urlparts.scheme=="ftp"):
7803 ftp = FTP();
7804 elif(urlparts.scheme=="ftps"):
7805 ftp = FTP_TLS();
7806 else:
7807 return False;
7808 if(urlparts.scheme=="sftp"):
7809 if(__use_pysftp__):
7810 return upload_file_from_pysftp_file(url);
7811 else:
7812 return upload_file_from_sftp_file(url);
7813 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
7814 return False;
7815 ftp_port = urlparts.port;
7816 if(urlparts.port is None):
7817 ftp_port = 21;
7818 try:
7819 ftp.connect(urlparts.hostname, ftp_port);
7820 except socket.gaierror:
7821 log.info("Error With URL "+url);
7822 return False;
7823 except socket.timeout:
7824 log.info("Error With URL "+url);
7825 return False;
7826 ftp.login(urlparts.username, urlparts.password);
7827 if(urlparts.scheme=="ftps"):
7828 ftp.prot_p();
7829 ftp.storbinary("STOR "+urlparts.path, ftpfile);
7830 ftp.close();
7831 ftpfile.seek(0, 0);
7832 return ftpfile;
7834 def upload_file_to_ftp_string(ftpstring, url):
7835 ftpfileo = BytesIO(ftpstring);
7836 ftpfile = upload_file_to_ftp_file(ftpfileo, url);
7837 ftpfileo.close();
7838 return ftpfile;
7840 def download_file_from_http_file(url, headers=geturls_headers_pycatfile_python_alt):
7841 # Parse the URL to extract username and password if present
7842 urlparts = urlparse(url);
7843 username = urlparts.username;
7844 password = urlparts.password;
7845 # Rebuild the URL without the username and password
7846 netloc = urlparts.hostname;
7847 if(urlparts.scheme=="sftp"):
7848 if(__use_pysftp__):
7849 return download_file_from_pysftp_file(url);
7850 else:
7851 return download_file_from_sftp_file(url);
7852 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
7853 return download_file_from_ftp_file(url);
7854 if urlparts.port:
7855 netloc += ':' + str(urlparts.port);
7856 rebuilt_url = urlunparse((urlparts.scheme, netloc, urlparts.path, urlparts.params, urlparts.query, urlparts.fragment));
7857 # Create a temporary file object
7858 httpfile = BytesIO();
7859 if haverequests:
7860 # Use the requests library if available
7861 if username and password:
7862 response = requests.get(rebuilt_url, headers=headers, auth=(username, password), stream=True);
7863 else:
7864 response = requests.get(rebuilt_url, headers=headers, stream=True);
7865 response.raw.decode_content = True
7866 shutil.copyfileobj(response.raw, httpfile);
7867 else:
7868 # Build a Request object for urllib
7869 request = Request(rebuilt_url, headers=headers);
7870 # Create an opener object for handling URLs
7871 if username and password:
7872 # Create a password manager
7873 password_mgr = HTTPPasswordMgrWithDefaultRealm();
7874 # Add the username and password
7875 password_mgr.add_password(None, rebuilt_url, username, password);
7876 # Create an authentication handler using the password manager
7877 auth_handler = HTTPBasicAuthHandler(password_mgr);
7878 # Build the opener with the authentication handler
7879 opener = build_opener(auth_handler);
7880 else:
7881 opener = build_opener();
7882 with opener.open(request) as response:
7883 shutil.copyfileobj(response, httpfile);
7884 # Reset file pointer to the start
7885 httpfile.seek(0, 0);
7886 # Return the temporary file object
7887 return httpfile;
7889 def download_file_from_http_string(url, headers=geturls_headers_pycatfile_python_alt):
7890 httpfile = download_file_from_http_file(url, headers);
7891 return ftpfile.read();
7893 if(haveparamiko):
7894 def download_file_from_sftp_file(url):
7895 urlparts = urlparse(url);
7896 file_name = os.path.basename(urlparts.path);
7897 file_dir = os.path.dirname(urlparts.path);
7898 sftp_port = urlparts.port;
7899 if(urlparts.port is None):
7900 sftp_port = 22;
7901 else:
7902 sftp_port = urlparts.port;
7903 if(urlparts.username is not None):
7904 sftp_username = urlparts.username;
7905 else:
7906 sftp_username = "anonymous";
7907 if(urlparts.password is not None):
7908 sftp_password = urlparts.password;
7909 elif(urlparts.password is None and urlparts.username=="anonymous"):
7910 sftp_password = "anonymous";
7911 else:
7912 sftp_password = "";
7913 if(urlparts.scheme=="ftp"):
7914 return download_file_from_ftp_file(url);
7915 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
7916 return download_file_from_http_file(url);
7917 if(urlparts.scheme!="sftp"):
7918 return False;
7919 ssh = paramiko.SSHClient();
7920 ssh.load_system_host_keys();
7921 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
7922 try:
7923 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
7924 except paramiko.ssh_exception.SSHException:
7925 return False;
7926 except socket.gaierror:
7927 log.info("Error With URL "+url);
7928 return False;
7929 except socket.timeout:
7930 log.info("Error With URL "+url);
7931 return False;
7932 sftp = ssh.open_sftp();
7933 sftpfile = BytesIO();
7934 sftp.getfo(urlparts.path, sftpfile);
7935 sftp.close();
7936 ssh.close();
7937 sftpfile.seek(0, 0);
7938 return sftpfile;
7939 else:
7940 def download_file_from_sftp_file(url):
7941 return False;
7943 if(haveparamiko):
7944 def download_file_from_sftp_string(url):
7945 sftpfile = download_file_from_sftp_file(url);
7946 return sftpfile.read();
7947 else:
7948 def download_file_from_ftp_string(url):
7949 return False;
7951 if(haveparamiko):
7952 def upload_file_to_sftp_file(sftpfile, url):
7953 urlparts = urlparse(url);
7954 file_name = os.path.basename(urlparts.path);
7955 file_dir = os.path.dirname(urlparts.path);
7956 sftp_port = urlparts.port;
7957 if(urlparts.port is None):
7958 sftp_port = 22;
7959 else:
7960 sftp_port = urlparts.port;
7961 if(urlparts.username is not None):
7962 sftp_username = urlparts.username;
7963 else:
7964 sftp_username = "anonymous";
7965 if(urlparts.password is not None):
7966 sftp_password = urlparts.password;
7967 elif(urlparts.password is None and urlparts.username=="anonymous"):
7968 sftp_password = "anonymous";
7969 else:
7970 sftp_password = "";
7971 if(urlparts.scheme=="ftp"):
7972 return upload_file_from_ftp_file(url);
7973 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
7974 return False;
7975 if(urlparts.scheme!="sftp"):
7976 return False;
7977 ssh = paramiko.SSHClient();
7978 ssh.load_system_host_keys();
7979 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
7980 try:
7981 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
7982 except paramiko.ssh_exception.SSHException:
7983 return False;
7984 except socket.gaierror:
7985 log.info("Error With URL "+url);
7986 return False;
7987 except socket.timeout:
7988 log.info("Error With URL "+url);
7989 return False;
7990 sftp = ssh.open_sftp();
7991 sftp.putfo(sftpfile, urlparts.path);
7992 sftp.close();
7993 ssh.close();
7994 sftpfile.seek(0, 0);
7995 return sftpfile;
7996 else:
7997 def upload_file_to_sftp_file(sftpfile, url):
7998 return False;
8000 if(haveparamiko):
8001 def upload_file_to_sftp_string(sftpstring, url):
8002 sftpfileo = BytesIO(sftpstring);
8003 sftpfile = upload_file_to_sftp_files(ftpfileo, url);
8004 sftpfileo.close();
8005 return sftpfile;
8006 else:
8007 def upload_file_to_sftp_string(url):
8008 return False;
8010 if(havepysftp):
8011 def download_file_from_pysftp_file(url):
8012 urlparts = urlparse(url);
8013 file_name = os.path.basename(urlparts.path);
8014 file_dir = os.path.dirname(urlparts.path);
8015 sftp_port = urlparts.port;
8016 if(urlparts.port is None):
8017 sftp_port = 22;
8018 else:
8019 sftp_port = urlparts.port;
8020 if(urlparts.username is not None):
8021 sftp_username = urlparts.username;
8022 else:
8023 sftp_username = "anonymous";
8024 if(urlparts.password is not None):
8025 sftp_password = urlparts.password;
8026 elif(urlparts.password is None and urlparts.username=="anonymous"):
8027 sftp_password = "anonymous";
8028 else:
8029 sftp_password = "";
8030 if(urlparts.scheme=="ftp"):
8031 return download_file_from_ftp_file(url);
8032 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
8033 return download_file_from_http_file(url);
8034 if(urlparts.scheme!="sftp"):
8035 return False;
8036 try:
8037 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
8038 except paramiko.ssh_exception.SSHException:
8039 return False;
8040 except socket.gaierror:
8041 log.info("Error With URL "+url);
8042 return False;
8043 except socket.timeout:
8044 log.info("Error With URL "+url);
8045 return False;
8046 sftp = ssh.open_sftp();
8047 sftpfile = BytesIO();
8048 sftp.getfo(urlparts.path, sftpfile);
8049 sftp.close();
8050 ssh.close();
8051 sftpfile.seek(0, 0);
8052 return sftpfile;
8053 else:
8054 def download_file_from_pysftp_file(url):
8055 return False;
8057 if(havepysftp):
8058 def download_file_from_pysftp_string(url):
8059 sftpfile = download_file_from_pysftp_file(url);
8060 return sftpfile.read();
8061 else:
8062 def download_file_from_ftp_string(url):
8063 return False;
8065 if(havepysftp):
8066 def upload_file_to_pysftp_file(sftpfile, url):
8067 urlparts = urlparse(url);
8068 file_name = os.path.basename(urlparts.path);
8069 file_dir = os.path.dirname(urlparts.path);
8070 sftp_port = urlparts.port;
8071 if(urlparts.port is None):
8072 sftp_port = 22;
8073 else:
8074 sftp_port = urlparts.port;
8075 if(urlparts.username is not None):
8076 sftp_username = urlparts.username;
8077 else:
8078 sftp_username = "anonymous";
8079 if(urlparts.password is not None):
8080 sftp_password = urlparts.password;
8081 elif(urlparts.password is None and urlparts.username=="anonymous"):
8082 sftp_password = "anonymous";
8083 else:
8084 sftp_password = "";
8085 if(urlparts.scheme=="ftp"):
8086 return upload_file_from_ftp_file(url);
8087 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
8088 return False;
8089 if(urlparts.scheme!="sftp"):
8090 return False;
8091 try:
8092 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
8093 except paramiko.ssh_exception.SSHException:
8094 return False;
8095 except socket.gaierror:
8096 log.info("Error With URL "+url);
8097 return False;
8098 except socket.timeout:
8099 log.info("Error With URL "+url);
8100 return False;
8101 sftp = ssh.open_sftp();
8102 sftp.putfo(sftpfile, urlparts.path);
8103 sftp.close();
8104 ssh.close();
8105 sftpfile.seek(0, 0);
8106 return sftpfile;
8107 else:
8108 def upload_file_to_pysftp_file(sftpfile, url):
8109 return False;
8111 if(havepysftp):
8112 def upload_file_to_pysftp_string(sftpstring, url):
8113 sftpfileo = BytesIO(sftpstring);
8114 sftpfile = upload_file_to_pysftp_files(ftpfileo, url);
8115 sftpfileo.close();
8116 return sftpfile;
8117 else:
8118 def upload_file_to_pysftp_string(url):
8119 return False;
8121 def download_file_from_internet_file(url, headers=geturls_headers_pycatfile_python_alt):
8122 urlparts = urlparse(url);
8123 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8124 return download_file_from_http_file(url, headers);
8125 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8126 return download_file_from_ftp_file(url);
8127 elif(urlparts.scheme=="sftp"):
8128 if(__use_pysftp__ and havepysftp):
8129 return download_file_from_pysftp_file(url);
8130 else:
8131 return download_file_from_sftp_file(url);
8132 else:
8133 return False;
8134 return False;
8136 def download_file_from_internet_uncompress_file(url, headers=geturls_headers_pycatfile_python_alt, formatspecs=__file_format_list__):
8137 fp = download_file_from_internet_file(url);
8138 fp = UncompressArchiveFile(fp, formatspecs);
8139 fp.seek(0, 0);
8140 if(not fp):
8141 return False;
8142 return fp;
8144 def download_file_from_internet_string(url, headers=geturls_headers_pycatfile_python_alt):
8145 urlparts = urlparse(url);
8146 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8147 return download_file_from_http_string(url, headers);
8148 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8149 return download_file_from_ftp_string(url);
8150 elif(urlparts.scheme=="sftp"):
8151 if(__use_pysftp__ and havepysftp):
8152 return download_file_from_pysftp_string(url);
8153 else:
8154 return download_file_from_sftp_string(url);
8155 else:
8156 return False;
8157 return False;
8159 def download_file_from_internet_uncompress_string(url, headers=geturls_headers_pycatfile_python_alt, formatspecs=__file_format_list__):
8160 fp = download_file_from_internet_string(url);
8161 fp = UncompressArchiveFile(fp, formatspecs);
8162 fp.seek(0, 0);
8163 if(not fp):
8164 return False;
8165 return fp;
8167 def upload_file_to_internet_file(ifp, url):
8168 urlparts = urlparse(url);
8169 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8170 return False;
8171 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8172 return upload_file_from_ftp_file(ifp, url);
8173 elif(urlparts.scheme=="sftp"):
8174 if(__use_pysftp__ and havepysftp):
8175 return upload_file_from_pysftp_file(ifp, url);
8176 else:
8177 return download_file_from_sftp_file(ifp, url);
8178 else:
8179 return False;
8180 return False;
8182 def upload_file_to_internet_compress_file(ifp, url, formatspecs=__file_format_list__):
8183 catfp = CompressArchiveFile(catfp, compression, formatspecs);
8184 if(not catfileout):
8185 return False;
8186 catfp.seek(0, 0);
8187 upload_file_to_internet_file(catfp, outfile);
8188 return True;
8190 def upload_file_to_internet_string(ifp, url):
8191 urlparts = urlparse(url);
8192 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8193 return False;
8194 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8195 return upload_file_from_ftp_string(ifp, url);
8196 elif(urlparts.scheme=="sftp"):
8197 if(__use_pysftp__ and havepysftp):
8198 return upload_file_from_pysftp_string(ifp, url);
8199 else:
8200 return download_file_from_sftp_string(ifp, url);
8201 else:
8202 return False;
8203 return False;
8205 def upload_file_to_internet_compress_string(ifp, url, formatspecs=__file_format_list__):
8206 catfp = CompressArchiveFile(BytesIO(ifp), compression, formatspecs);
8207 if(not catfileout):
8208 return False;
8209 catfp.seek(0, 0);
8210 upload_file_to_internet_file(catfp, outfile);
8211 return True;
8213 try:
8214 if(hasattr(shutil, "register_archive_format")):
8215 # Register the packing format
8216 shutil.register_archive_format(__file_format_name__, PackArchiveFileFunc, description='Pack concatenated files');
8217 except shutil.RegistryError:
8218 pass;
8220 try:
8221 if(hasattr(shutil, "register_unpack_format")):
8222 # Register the unpacking format
8223 shutil.register_unpack_format(__file_format_name__, archivefile_extensions, UnPackArchiveFileFunc, description='UnPack concatenated files');
8224 except shutil.RegistryError:
8225 pass;