Update pycatfile.py
[PyCatFile.git] / pycatfile.py
blob0badc1fcf7f77a4dd54125c9a42a5cd493cab98d
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/26/2024 Ver. 0.8.6 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, 8, 6, "RC 1", 1);
175 __version_date_info__ = (2024, 4, 26, "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 PackArchiveFileFromInFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
3835 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
3836 if(verbose):
3837 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
3838 if(checkcompressfile=="tarfile"):
3839 return PackArchiveFileFromTarFile(infile, outfile, compression, compressionlevel, checksumtype, extradata, formatspecs, verbose, returnfp);
3840 elif(checkcompressfile=="zipfile"):
3841 return PackArchiveFileFromZipFile(infile, outfile, compression, compressionlevel, checksumtype, extradata, formatspecs, verbose, returnfp);
3842 elif(checkcompressfile=="catfile"):
3843 return RePackArchiveFile(infile, outfile, compression, compressionlevel, False, 0, 0, checksumtype, False, extradata, formatspecs, verbose, returnfp);
3844 elif(rarfile_support and checkcompressfile=="rarfile"):
3845 return PackArchiveFileFromRarFile(infile, outfile, compression, compressionlevel, checksumtype, extradata, formatspecs, verbose, returnfp);
3846 elif(py7zr_support and checkcompressfile=="7zipfile"):
3847 return PackArchiveFileFromSevenZipFile(infile, outfile, compression, compressionlevel, checksumtype, extradata, formatspecs, verbose, returnfp);
3848 else:
3849 return False;
3850 return False;
3852 create_alias_function("Pack", __file_format_name__, "FromInFile", PackArchiveFileFromInFile);
3854 def ArchiveFileSeekToFileNum(infile, seekto=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3855 if(hasattr(infile, "read") or hasattr(infile, "write")):
3856 catfp = infile;
3857 catfp.seek(0, 0);
3858 catfp = UncompressArchiveFile(catfp, formatspecs);
3859 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
3860 if(checkcompressfile=="tarfile"):
3861 return TarFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3862 if(checkcompressfile=="zipfile"):
3863 return ZipFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3864 if(rarfile_support and checkcompressfile=="rarfile"):
3865 return RarFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3866 if(py7zr_support and checkcompressfile=="7zipfile"):
3867 return SevenZipFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3868 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3869 return False;
3870 if(not catfp):
3871 return False;
3872 catfp.seek(0, 0);
3873 elif(infile=="-"):
3874 catfp = BytesIO();
3875 if(hasattr(sys.stdin, "buffer")):
3876 shutil.copyfileobj(sys.stdin.buffer, catfp);
3877 else:
3878 shutil.copyfileobj(sys.stdin, catfp);
3879 catfp.seek(0, 0);
3880 catfp = UncompressArchiveFile(catfp, formatspecs);
3881 if(not catfp):
3882 return False;
3883 catfp.seek(0, 0);
3884 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
3885 catfp = download_file_from_internet_file(infile);
3886 catfp.seek(0, 0);
3887 catfp = UncompressArchiveFile(catfp, formatspecs);
3888 if(not catfp):
3889 return False;
3890 catfp.seek(0, 0);
3891 else:
3892 infile = RemoveWindowsPath(infile);
3893 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
3894 if(checkcompressfile=="tarfile"):
3895 return TarFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3896 if(checkcompressfile=="zipfile"):
3897 return ZipFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3898 if(rarfile_support and checkcompressfile=="rarfile"):
3899 return RarFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3900 if(py7zr_support and checkcompressfile=="7zipfile"):
3901 return SevenZipFileToArray(infile, seekto, 0, listonly, skipchecksum, formatspecs, returnfp);
3902 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3903 return False;
3904 compresscheck = CheckCompressionType(infile, formatspecs, True);
3905 if(not compresscheck):
3906 fextname = os.path.splitext(infile)[1];
3907 if(fextname==".gz"):
3908 compresscheck = "gzip";
3909 elif(fextname==".bz2"):
3910 compresscheck = "bzip2";
3911 elif(fextname==".zst"):
3912 compresscheck = "zstd";
3913 elif(fextname==".lz4" or fextname==".clz4"):
3914 compresscheck = "lz4";
3915 elif(fextname==".lzo" or fextname==".lzop"):
3916 compresscheck = "lzo";
3917 elif(fextname==".lzma" or fextname==".xz"):
3918 compresscheck = "lzma";
3919 else:
3920 return False;
3921 if(not compresscheck):
3922 return False;
3923 catfp = UncompressFile(infile, formatspecs, "rb");
3925 try:
3926 catfp.seek(0, 2);
3927 except OSError:
3928 SeekToEndOfFile(catfp);
3929 except ValueError:
3930 SeekToEndOfFile(catfp);
3931 CatSize = catfp.tell();
3932 CatSizeEnd = CatSize;
3934 try:
3935 catfp.seek(0, 0);
3936 except OSError:
3937 return False;
3938 except ValueError:
3939 return False;
3940 curloc = catfp.tell();
3941 if(curloc>0):
3942 catfp.seek(0, 0);
3943 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3944 if(curloc>0):
3945 catfp.seek(curloc, 0);
3946 catstring = catheader[0];
3947 catversion = re.findall(r"([\d]+)$", catstring);
3948 fprenumfiles = catheader[1];
3949 fnumfiles = int(fprenumfiles, 16);
3950 fprechecksumtype = catheader[2];
3951 fprechecksum = catheader[3];
3952 fileheader = AppendNullByte(catstring, formatspecs[5]);
3953 fnumfileshex = format(int(fnumfiles), 'x').lower();
3954 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3955 if(fprechecksumtype=="none" or fprechecksumtype==""):
3956 catfileheadercshex = format(0, 'x').lower();
3957 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3958 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3959 elif(fprechecksumtype=="crc16_ccitt"):
3960 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3961 elif(fprechecksumtype=="adler32"):
3962 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3963 elif(fprechecksumtype=="crc32"):
3964 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3965 elif(fprechecksumtype=="crc64_ecma"):
3966 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3967 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3968 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3969 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3970 checksumoutstr = hashlib.new(fprechecksumtype);
3971 checksumoutstr.update(fileheader.encode('UTF-8'));
3972 catfileheadercshex = checksumoutstr.hexdigest().lower();
3973 else:
3974 catfileheadercshex = format(0, 'x').lower();
3975 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3976 fheadtell = len(fileheader);
3977 if(fprechecksum!=catfileheadercshex and not skipchecksum):
3978 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3979 return False;
3980 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3981 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
3982 if(seekto>=fnumfiles):
3983 seekto = fnumfiles - 1;
3984 if(seekto<0):
3985 seekto = 0;
3986 if(seekto>=0):
3987 il = -1;
3988 while(il < seekto):
3989 prefhstart = catfp.tell();
3990 if(formatspecs[7]):
3991 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3992 else:
3993 preheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
3994 if(len(preheaderdata)==0):
3995 break;
3996 prefheadsize = int(preheaderdata[0], 16);
3997 prefnumfields = int(preheaderdata[1], 16);
3998 preftype = int(preheaderdata[2], 16);
3999 if(re.findall("^[.|/]", preheaderdata[3])):
4000 prefname = preheaderdata[3];
4001 else:
4002 prefname = "./"+preheaderdata[3];
4003 prefbasedir = os.path.dirname(prefname);
4004 preflinkname = preheaderdata[4];
4005 prefsize = int(preheaderdata[5], 16);
4006 prefatime = int(preheaderdata[6], 16);
4007 prefmtime = int(preheaderdata[7], 16);
4008 prefctime = int(preheaderdata[8], 16);
4009 prefbtime = int(preheaderdata[9], 16);
4010 prefmode = int(preheaderdata[10], 16);
4011 prefchmode = stat.S_IMODE(prefmode);
4012 preftypemod = stat.S_IFMT(prefmode);
4013 prefwinattributes = int(preheaderdata[11], 16);
4014 prefuid = int(preheaderdata[12], 16);
4015 prefuname = preheaderdata[13];
4016 prefgid = int(preheaderdata[14], 16);
4017 prefgname = preheaderdata[15];
4018 fid = int(preheaderdata[16], 16);
4019 finode = int(preheaderdata[17], 16);
4020 flinkcount = int(preheaderdata[18], 16);
4021 prefdev_minor = int(preheaderdata[19], 16);
4022 prefdev_major = int(preheaderdata[20], 16);
4023 prefrdev_minor = int(preheaderdata[22], 16);
4024 prefrdev_major = int(preheaderdata[23], 16);
4025 prefextrasize = int(preheaderdata[24], 16);
4026 prefextrafields = int(preheaderdata[25], 16);
4027 extrafieldslist = [];
4028 extrastart = 25;
4029 extraend = extrastart + prefextrafields;
4030 extrafieldslist = [];
4031 if(extrastart<extraend):
4032 extrafieldslist.append(preheaderdata[extrastart]);
4033 extrastart = extrastart + 1;
4034 prefchecksumtype = preheaderdata[extrastart].lower();
4035 prefcs = preheaderdata[extrastart + 1].lower();
4036 prefccs = preheaderdata[extrastart + 2].lower();
4037 hc = 0;
4038 hcmax = len(preheaderdata) - 2;
4039 hout = "";
4040 while(hc<hcmax):
4041 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
4042 hc = hc + 1;
4043 prenewfcs = GetFileChecksum(hout, preheaderdata[-3].lower(), True, formatspecs);
4044 if(prefcs!=prenewfcs and not skipchecksum):
4045 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
4046 return False;
4047 valid_archive = False;
4048 invalid_archive = True;
4049 prefhend = catfp.tell() - 1;
4050 prefcontentstart = catfp.tell();
4051 prefcontents = "";
4052 pyhascontents = False;
4053 if(prefsize>0):
4054 prefcontents = catfp.read(prefsize);
4055 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
4056 pyhascontents = True;
4057 if(prefccs!=prenewfccs and not skipchecksum):
4058 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
4059 return False;
4060 catfp.seek(1, 1);
4061 il = il + 1;
4062 catfp.seek(seekstart, 0);
4063 fileidnum = il;
4064 catfheadsize = int(preheaderdata[0], 16);
4065 catfnumfields = int(preheaderdata[1], 16);
4066 catftype = int(preheaderdata[2], 16);
4067 if(re.findall("^[.|/]", preheaderdata[3])):
4068 catfname = preheaderdata[3];
4069 else:
4070 catfname = "./"+preheaderdata[3];
4071 catflinkname = preheaderdata[4];
4072 catfsize = int(preheaderdata[5], 16);
4073 catfbasedir = os.path.dirname(catfname);
4074 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
4075 if(returnfp):
4076 catlist.update({'catfp': catfp});
4077 else:
4078 catfp.close();
4079 return catlist;
4081 create_alias_function("", __file_format_name__, "SeekToFileNum", ArchiveFileSeekToFileNum);
4083 def ArchiveFileSeekToFileName(infile, seekfile=None, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4084 if(hasattr(infile, "read") or hasattr(infile, "write")):
4085 catfp = infile;
4086 catfp.seek(0, 0);
4087 catfp = UncompressArchiveFile(catfp, formatspecs);
4088 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
4089 if(checkcompressfile=="tarfile"):
4090 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4091 if(checkcompressfile=="zipfile"):
4092 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4093 if(rarfile_support and checkcompressfile=="rarfile"):
4094 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4095 if(py7zr_support and checkcompressfile=="7zipfile"):
4096 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4097 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4098 return False;
4099 if(not catfp):
4100 return False;
4101 catfp.seek(0, 0);
4102 elif(infile=="-"):
4103 catfp = BytesIO();
4104 if(hasattr(sys.stdin, "buffer")):
4105 shutil.copyfileobj(sys.stdin.buffer, catfp);
4106 else:
4107 shutil.copyfileobj(sys.stdin, catfp);
4108 catfp.seek(0, 0);
4109 catfp = UncompressArchiveFile(catfp, formatspecs);
4110 if(not catfp):
4111 return False;
4112 catfp.seek(0, 0);
4113 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
4114 catfp = download_file_from_internet_file(infile);
4115 catfp = UncompressArchiveFile(catfp, formatspecs);
4116 catfp.seek(0, 0);
4117 if(not catfp):
4118 return False;
4119 catfp.seek(0, 0);
4120 else:
4121 infile = RemoveWindowsPath(infile);
4122 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4123 if(checkcompressfile=="tarfile"):
4124 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4125 if(checkcompressfile=="zipfile"):
4126 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4127 if(rarfile_support and checkcompressfile=="rarfile"):
4128 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4129 if(py7zr_support and checkcompressfile=="7zipfile"):
4130 return SevenZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
4131 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4132 return False;
4133 compresscheck = CheckCompressionType(infile, formatspecs, True);
4134 if(not compresscheck):
4135 fextname = os.path.splitext(infile)[1];
4136 if(fextname==".gz"):
4137 compresscheck = "gzip";
4138 elif(fextname==".bz2"):
4139 compresscheck = "bzip2";
4140 elif(fextname==".zst"):
4141 compresscheck = "zstd";
4142 elif(fextname==".lz4" or fextname==".clz4"):
4143 compresscheck = "lz4";
4144 elif(fextname==".lzo" or fextname==".lzop"):
4145 compresscheck = "lzo";
4146 elif(fextname==".lzma" or fextname==".xz"):
4147 compresscheck = "lzma";
4148 else:
4149 return False;
4150 if(not compresscheck):
4151 return False;
4152 catfp = UncompressFile(infile, formatspecs, "rb");
4154 try:
4155 catfp.seek(0, 2);
4156 except OSError:
4157 SeekToEndOfFile(catfp);
4158 except ValueError:
4159 SeekToEndOfFile(catfp);
4160 CatSize = catfp.tell();
4161 CatSizeEnd = CatSize;
4163 try:
4164 catfp.seek(0, 0);
4165 except OSError:
4166 return False;
4167 except ValueError:
4168 return False;
4169 curloc = catfp.tell();
4170 if(curloc>0):
4171 catfp.seek(0, 0);
4172 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
4173 if(curloc>0):
4174 catfp.seek(curloc, 0);
4175 catstring = catheader[0];
4176 catversion = re.findall(r"([\d]+)$", catstring);
4177 fprenumfiles = catheader[1];
4178 fnumfiles = int(fprenumfiles, 16);
4179 fprechecksumtype = catheader[2];
4180 fprechecksum = catheader[3];
4181 fileheader = AppendNullByte(catstring, formatspecs[5]);
4182 fnumfileshex = format(int(fnumfiles), 'x').lower();
4183 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
4184 if(fprechecksumtype=="none" or fprechecksumtype==""):
4185 catfileheadercshex = format(0, 'x').lower();
4186 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
4187 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4188 elif(fprechecksumtype=="crc16_ccitt"):
4189 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4190 elif(fprechecksumtype=="adler32"):
4191 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4192 elif(fprechecksumtype=="crc32"):
4193 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4194 elif(fprechecksumtype=="crc64_ecma"):
4195 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4196 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
4197 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4198 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
4199 checksumoutstr = hashlib.new(fprechecksumtype);
4200 checksumoutstr.update(fileheader.encode('UTF-8'));
4201 catfileheadercshex = checksumoutstr.hexdigest().lower();
4202 else:
4203 catfileheadercshex = format(0, 'x').lower();
4204 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4205 fheadtell = len(fileheader);
4206 if(fprechecksum!=catfileheadercshex and not skipchecksum):
4207 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
4208 return False;
4209 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4210 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
4211 seekto = fnumfiles - 1
4212 filefound = False;
4213 if(seekto>=0):
4214 il = -1;
4215 while(il < seekto):
4216 prefhstart = catfp.tell();
4217 if(formatspecs[7]):
4218 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4219 else:
4220 preheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4221 if(len(preheaderdata)==0):
4222 break;
4223 prefheadsize = int(preheaderdata[0], 16);
4224 prefnumfields = int(preheaderdata[1], 16);
4225 preftype = int(preheaderdata[2], 16);
4226 if(re.findall("^[.|/]", preheaderdata[3])):
4227 prefname = preheaderdata[3];
4228 else:
4229 prefname = "./"+preheaderdata[3];
4230 prefbasedir = os.path.dirname(prefname);
4231 preflinkname = preheaderdata[4];
4232 prefsize = int(preheaderdata[5], 16);
4233 prefatime = int(preheaderdata[6], 16);
4234 prefmtime = int(preheaderdata[7], 16);
4235 prefctime = int(preheaderdata[8], 16);
4236 prefbtime = int(preheaderdata[9], 16);
4237 prefmode = int(preheaderdata[10], 16);
4238 prefchmode = stat.S_IMODE(prefmode);
4239 preftypemod = stat.S_IFMT(prefmode);
4240 prefwinattributes = int(preheaderdata[11], 16);
4241 prefuid = int(preheaderdata[12], 16);
4242 prefuname = preheaderdata[13];
4243 prefgid = int(preheaderdata[14], 16);
4244 prefgname = preheaderdata[15];
4245 fid = int(preheaderdata[16], 16);
4246 finode = int(preheaderdata[17], 16);
4247 flinkcount = int(preheaderdata[18], 16);
4248 prefdev_minor = int(preheaderdata[19], 16);
4249 prefdev_major = int(preheaderdata[20], 16);
4250 prefrdev_minor = int(preheaderdata[22], 16);
4251 prefrdev_major = int(preheaderdata[23], 16);
4252 prefextrasize = int(preheaderdata[24], 16);
4253 prefextrafields = int(preheaderdata[25], 16);
4254 extrafieldslist = [];
4255 extrastart = 25;
4256 extraend = extrastart + prefextrafields;
4257 extrafieldslist = [];
4258 if(extrastart<extraend):
4259 extrafieldslist.append(preheaderdata[extrastart]);
4260 extrastart = extrastart + 1;
4261 prefchecksumtype = preheaderdata[extrastart].lower();
4262 prefcs = preheaderdata[extrastart + 1].lower();
4263 prefccs = preheaderdata[extrastart + 2].lower();
4264 hc = 0;
4265 hcmax = len(preheaderdata) - 2;
4266 hout = "";
4267 while(hc<hcmax):
4268 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
4269 hc = hc + 1;
4270 prenewfcs = GetFileChecksum(hout, preheaderdata[-3].lower(), True, formatspecs);
4271 if(prefcs!=prenewfcs and not skipchecksum):
4272 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
4273 return False;
4274 valid_archive = False;
4275 invalid_archive = True;
4276 prefhend = catfp.tell() - 1;
4277 prefcontentstart = catfp.tell();
4278 prefcontents = "";
4279 pyhascontents = False;
4280 if(prefsize>0):
4281 prefcontents = catfp.read(prefsize);
4282 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
4283 pyhascontents = True;
4284 if(prefccs!=prenewfccs and not skipchecksum):
4285 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
4286 return False;
4287 catfp.seek(1, 1);
4288 il = il + 1;
4289 filefound = False;
4290 prefname = preheaderdata[2];
4291 if(re.findall("^[.|/]", preheaderdata[2])):
4292 prefname = preheaderdata[2];
4293 else:
4294 prefname = "./"+preheaderdata[2];
4295 if(prefname==seekfile):
4296 filefound = True;
4297 break;
4298 catfp.seek(seekstart, 0);
4299 fileidnum = il;
4300 catfheadsize = int(preheaderdata[0], 16);
4301 catfnumfields = int(preheaderdata[1], 16);
4302 catftype = int(preheaderdata[2], 16);
4303 if(re.findall("^[.|/]", preheaderdata[3])):
4304 catfname = preheaderdata[3];
4305 else:
4306 catfname = "./"+preheaderdata[3];
4307 catflinkname = preheaderdata[4];
4308 catfsize = int(preheaderdata[5], 16);
4309 catfbasedir = os.path.dirname(catfname);
4310 if(filefound):
4311 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
4312 else:
4313 return False;
4314 if(returnfp):
4315 catlist.update({'catfp': catfp});
4316 else:
4317 catfp.close();
4318 return catlist;
4320 create_alias_function("", __file_format_name__, "SeekToFileName", ArchiveFileSeekToFileName);
4322 def ArchiveFileValidate(infile, formatspecs=__file_format_list__, verbose=False, returnfp=False):
4323 if(verbose):
4324 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4325 if(hasattr(infile, "read") or hasattr(infile, "write")):
4326 catfp = infile;
4327 catfp.seek(0, 0);
4328 catfp = UncompressArchiveFile(catfp, formatspecs);
4329 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
4330 if(checkcompressfile=="tarfile"):
4331 return TarFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4332 if(checkcompressfile=="zipfile"):
4333 return ZipFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4334 if(rarfile_support and checkcompressfile=="rarfile"):
4335 return RarFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4336 if(py7zr_support and checkcompressfile=="7zipfile"):
4337 return SevenZipFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4338 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4339 return False;
4340 if(not catfp):
4341 return False;
4342 catfp.seek(0, 0);
4343 elif(infile=="-"):
4344 catfp = BytesIO();
4345 if(hasattr(sys.stdin, "buffer")):
4346 shutil.copyfileobj(sys.stdin.buffer, catfp);
4347 else:
4348 shutil.copyfileobj(sys.stdin, catfp);
4349 catfp.seek(0, 0);
4350 catfp = UncompressArchiveFile(catfp, formatspecs);
4351 if(not catfp):
4352 return False;
4353 catfp.seek(0, 0);
4354 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
4355 catfp = download_file_from_internet_file(infile);
4356 catfp = UncompressArchiveFile(catfp, formatspecs);
4357 catfp.seek(0, 0);
4358 if(not catfp):
4359 return False;
4360 catfp.seek(0, 0);
4361 else:
4362 infile = RemoveWindowsPath(infile);
4363 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4364 if(checkcompressfile=="tarfile"):
4365 return TarFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4366 if(checkcompressfile=="zipfile"):
4367 return ZipFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4368 if(rarfile_support and checkcompressfile=="rarfile"):
4369 return RarFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4370 if(py7zr_support and checkcompressfile=="7zipfile"):
4371 return SevenZipFileToArray(infile, 0, 0, False, False, formatspecs, returnfp);
4372 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4373 return False;
4374 compresscheck = CheckCompressionType(infile, formatspecs, True);
4375 if(not compresscheck):
4376 fextname = os.path.splitext(infile)[1];
4377 if(fextname==".gz"):
4378 compresscheck = "gzip";
4379 elif(fextname==".bz2"):
4380 compresscheck = "bzip2";
4381 elif(fextname==".zst"):
4382 compresscheck = "zstd";
4383 elif(fextname==".lz4" or fextname==".clz4"):
4384 compresscheck = "lz4";
4385 elif(fextname==".lzo" or fextname==".lzop"):
4386 compresscheck = "lzo";
4387 elif(fextname==".lzma" or fextname==".xz"):
4388 compresscheck = "lzma";
4389 else:
4390 return False;
4391 if(not compresscheck):
4392 return False;
4393 catfp = UncompressFile(infile, formatspecs, "rb");
4395 try:
4396 catfp.seek(0, 2);
4397 except OSError:
4398 SeekToEndOfFile(catfp);
4399 except ValueError:
4400 SeekToEndOfFile(catfp);
4401 CatSize = catfp.tell();
4402 CatSizeEnd = CatSize;
4404 try:
4405 catfp.seek(0, 0);
4406 except OSError:
4407 return False;
4408 except ValueError:
4409 return False;
4410 curloc = catfp.tell();
4411 if(curloc>0):
4412 catfp.seek(0, 0);
4413 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
4414 if(curloc>0):
4415 catfp.seek(curloc, 0);
4416 catstring = catheader[0];
4417 catversion = re.findall(r"([\d]+)$", catstring);
4418 fprenumfiles = catheader[1];
4419 fnumfiles = int(fprenumfiles, 16);
4420 fprechecksumtype = catheader[2];
4421 fprechecksum = catheader[3];
4422 il = 0;
4423 fileheader = AppendNullByte(catstring, formatspecs[5]);
4424 fnumfileshex = format(int(fnumfiles), 'x').lower();
4425 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
4426 if(fprechecksumtype=="none" or fprechecksumtype==""):
4427 catfileheadercshex = format(0, 'x').lower();
4428 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
4429 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4430 elif(fprechecksumtype=="crc16_ccitt"):
4431 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4432 elif(fprechecksumtype=="adler32"):
4433 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4434 elif(fprechecksumtype=="crc32"):
4435 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4436 elif(fprechecksumtype=="crc64_ecma"):
4437 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4438 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
4439 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4440 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
4441 checksumoutstr = hashlib.new(fprechecksumtype);
4442 checksumoutstr.update(fileheader.encode('UTF-8'));
4443 catfileheadercshex = checksumoutstr.hexdigest().lower();
4444 else:
4445 catfileheadercshex = format(0, 'x').lower();
4446 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4447 valid_archive = True;
4448 invalid_archive = False;
4449 if(verbose):
4450 VerbosePrintOut(infile);
4451 VerbosePrintOut("Number of Records " + str(fnumfiles));
4452 if(fprechecksum==catfileheadercshex):
4453 if(verbose):
4454 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4455 else:
4456 if(verbose):
4457 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4458 valid_archive = False;
4459 invalid_archive = True;
4460 if(verbose):
4461 VerbosePrintOut("");
4462 while(il<fnumfiles):
4463 catfhstart = catfp.tell();
4464 if(formatspecs[7]):
4465 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4466 else:
4467 catheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4468 if(len(catheaderdata)==0):
4469 break;
4470 catfheadsize = int(catheaderdata[0], 16);
4471 catfnumfields = int(catheaderdata[1], 16);
4472 catftype = int(catheaderdata[2], 16);
4473 if(re.findall("^[.|/]", catheaderdata[3])):
4474 catfname = catheaderdata[3];
4475 else:
4476 catfname = "./"+catheaderdata[3];
4477 catfbasedir = os.path.dirname(catfname);
4478 catflinkname = catheaderdata[4];
4479 catfsize = int(catheaderdata[5], 16);
4480 catfatime = int(catheaderdata[6], 16);
4481 catfmtime = int(catheaderdata[7], 16);
4482 catfctime = int(catheaderdata[8], 16);
4483 catfbtime = int(catheaderdata[9], 16);
4484 catfmode = int(catheaderdata[10], 16);
4485 catfchmode = stat.S_IMODE(catfmode);
4486 catftypemod = stat.S_IFMT(catfmode);
4487 prefwinattributes = int(catheaderdata[11], 16);
4488 catfuid = int(catheaderdata[12], 16);
4489 catfuname = catheaderdata[13];
4490 catfgid = int(catheaderdata[14], 16);
4491 catfgname = catheaderdata[15];
4492 fid = int(catheaderdata[16], 16);
4493 finode = int(catheaderdata[17], 16);
4494 flinkcount = int(catheaderdata[18], 16);
4495 catfdev_minor = int(catheaderdata[19], 16);
4496 catfdev_major = int(catheaderdata[20], 16);
4497 catfrdev_minor = int(catheaderdata[21], 16);
4498 catfrdev_major = int(catheaderdata[22], 16);
4499 catfextrasize = int(catheaderdata[23], 16);
4500 catfextrafields = int(catheaderdata[24], 16);
4501 extrafieldslist = [];
4502 extrastart = 25;
4503 extraend = extrastart + catfextrafields;
4504 extrafieldslist = [];
4505 if(extrastart<extraend):
4506 extrafieldslist.append(catheaderdata[extrastart]);
4507 extrastart = extrastart + 1;
4508 catfchecksumtype = catheaderdata[extrastart].lower();
4509 catfcs = catheaderdata[extrastart + 1].lower();
4510 catfccs = catheaderdata[extrastart + 2].lower();
4511 hc = 0;
4512 hcmax = len(catheaderdata) - 2;
4513 hout = "";
4514 while(hc<hcmax):
4515 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
4516 hc = hc + 1;
4517 catnewfcs = GetFileChecksum(hout, catheaderdata[-3].lower(), True, formatspecs);
4518 if(verbose):
4519 VerbosePrintOut(catfname);
4520 VerbosePrintOut("Record Number " + str(il) + "; File ID " + str(fid) + "; iNode Number " + str(finode));
4521 if(catfcs==catnewfcs):
4522 if(verbose):
4523 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart));
4524 else:
4525 if(verbose):
4526 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart));
4527 valid_archive = False;
4528 invalid_archive = True;
4529 catfhend = catfp.tell() - 1;
4530 catfcontentstart = catfp.tell();
4531 catfcontents = "";
4532 pyhascontents = False;
4533 if(catfsize>0):
4534 catfcontents = catfp.read(catfsize);
4535 catnewfccs = GetFileChecksum(catfcontents, catheaderdata[-3].lower(), False, formatspecs);
4536 pyhascontents = True;
4537 if(catfccs==catnewfccs):
4538 if(verbose):
4539 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart));
4540 else:
4541 if(verbose):
4542 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart));
4543 valid_archive = False;
4544 invalid_archive = True;
4545 if(verbose):
4546 VerbosePrintOut("");
4547 catfp.seek(1, 1);
4548 il = il + 1;
4549 if(valid_archive):
4550 if(returnfp):
4551 return catfp;
4552 else:
4553 catfp.close();
4554 return True;
4555 else:
4556 catfp.close();
4557 return False;
4559 create_alias_function("", __file_format_name__, "Validate", ArchiveFileValidate);
4561 def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4562 if(hasattr(infile, "read") or hasattr(infile, "write")):
4563 catfp = infile;
4564 catfp.seek(0, 0);
4565 catfp = UncompressArchiveFile(catfp, formatspecs);
4566 checkcompressfile = CheckCompressionSubType(catfp, formatspecs, True);
4567 if(checkcompressfile=="tarfile"):
4568 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4569 if(checkcompressfile=="zipfile"):
4570 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4571 if(rarfile_support and checkcompressfile=="rarfile"):
4572 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4573 if(py7zr_support and checkcompressfile=="7zipfile"):
4574 return SevenZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4575 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4576 return False;
4577 if(not catfp):
4578 return False;
4579 catfp.seek(0, 0);
4580 elif(infile=="-"):
4581 catfp = BytesIO();
4582 if(hasattr(sys.stdin, "buffer")):
4583 shutil.copyfileobj(sys.stdin.buffer, catfp);
4584 else:
4585 shutil.copyfileobj(sys.stdin, catfp);
4586 catfp.seek(0, 0);
4587 catfp = UncompressArchiveFile(catfp, formatspecs);
4588 if(not catfp):
4589 return False;
4590 catfp.seek(0, 0);
4591 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
4592 catfp = download_file_from_internet_file(infile);
4593 catfp = UncompressArchiveFile(catfp, formatspecs);
4594 catfp.seek(0, 0);
4595 if(not catfp):
4596 return False;
4597 catfp.seek(0, 0);
4598 else:
4599 infile = RemoveWindowsPath(infile);
4600 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4601 if(checkcompressfile=="tarfile"):
4602 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4603 if(checkcompressfile=="zipfile"):
4604 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4605 if(rarfile_support and checkcompressfile=="rarfile"):
4606 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4607 if(py7zr_support and checkcompressfile=="7zipfile"):
4608 return SevenZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4609 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
4610 return False;
4611 compresscheck = CheckCompressionType(infile, formatspecs, True);
4612 if(not compresscheck):
4613 fextname = os.path.splitext(infile)[1];
4614 if(fextname==".gz"):
4615 compresscheck = "gzip";
4616 elif(fextname==".bz2"):
4617 compresscheck = "bzip2";
4618 elif(fextname==".zst"):
4619 compresscheck = "zstd";
4620 elif(fextname==".lz4" or fextname==".clz4"):
4621 compresscheck = "lz4";
4622 elif(fextname==".lzo" or fextname==".lzop"):
4623 compresscheck = "lzo";
4624 elif(fextname==".lzma" or fextname==".xz"):
4625 compresscheck = "lzma";
4626 else:
4627 return False;
4628 if(not compresscheck):
4629 return False;
4630 catfp = UncompressFile(infile, formatspecs, "rb");
4632 try:
4633 catfp.seek(0, 2);
4634 except OSError:
4635 SeekToEndOfFile(catfp);
4636 except ValueError:
4637 SeekToEndOfFile(catfp);
4638 CatSize = catfp.tell();
4639 CatSizeEnd = CatSize;
4641 try:
4642 catfp.seek(0, 0);
4643 except OSError:
4644 return False;
4645 except ValueError:
4646 return False;
4647 curloc = catfp.tell();
4648 if(curloc>0):
4649 catfp.seek(0, 0);
4650 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
4651 if(curloc>0):
4652 catfp.seek(curloc, 0);
4653 catstring = catheader[0];
4654 catversion = re.findall(r"([\d]+)$", catstring);
4655 fprenumfiles = catheader[1];
4656 fnumfiles = int(fprenumfiles, 16);
4657 fprechecksumtype = catheader[2];
4658 fprechecksum = catheader[3];
4659 fileheader = AppendNullByte(catstring, formatspecs[5]);
4660 fnumfileshex = format(int(fnumfiles), 'x').lower();
4661 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
4662 if(fprechecksumtype=="none" or fprechecksumtype==""):
4663 catfileheadercshex = format(0, 'x').lower();
4664 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
4665 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4666 elif(fprechecksumtype=="crc16_ccitt"):
4667 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4668 elif(fprechecksumtype=="adler32"):
4669 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4670 elif(fprechecksumtype=="crc32"):
4671 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4672 elif(fprechecksumtype=="crc64_ecma"):
4673 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4674 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
4675 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4676 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
4677 checksumoutstr = hashlib.new(fprechecksumtype);
4678 checksumoutstr.update(fileheader.encode('UTF-8'));
4679 catfileheadercshex = checksumoutstr.hexdigest().lower();
4680 else:
4681 catfileheadercshex = format(0, 'x').lower();
4682 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4683 fheadtell = len(fileheader);
4684 if(fprechecksum!=catfileheadercshex and not skipchecksum):
4685 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
4686 return False;
4687 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4688 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
4689 if(seekstart<0 and seekstart>fnumfiles):
4690 seekstart = 0;
4691 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
4692 seekend = fnumfiles;
4693 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
4694 seekend = fnumfiles - abs(seekend);
4695 if(seekstart>0):
4696 il = 0;
4697 while(il < seekstart):
4698 prefhstart = catfp.tell();
4699 if(formatspecs[7]):
4700 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4701 else:
4702 preheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4703 if(len(preheaderdata)==0):
4704 break;
4705 prefheadsize = int(preheaderdata[0], 16);
4706 prefnumfields = int(preheaderdata[1], 16);
4707 if(re.findall("^[.|/]", preheaderdata[3])):
4708 prefname = preheaderdata[3];
4709 else:
4710 prefname = "./"+preheaderdata[3];
4711 prefsize = int(preheaderdata[5], 16);
4712 prefextrasize = int(preheaderdata[23], 16);
4713 prefextrafields = int(preheaderdata[24], 16);
4714 extrafieldslist = [];
4715 extrastart = 25;
4716 extraend = extrastart + prefextrafields;
4717 extrafieldslist = [];
4718 if(extrastart<extraend):
4719 extrafieldslist.append(preheaderdata[extrastart]);
4720 extrastart = extrastart + 1;
4721 prefchecksumtype = preheaderdata[extrastart].lower();
4722 prefcs = preheaderdata[extrastart + 1].lower();
4723 prefccs = preheaderdata[extrastart + 2].lower();
4724 hc = 0;
4725 hcmax = len(preheaderdata) - 2;
4726 hout = "";
4727 while(hc<hcmax):
4728 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
4729 hc = hc + 1;
4730 prenewfcs = GetFileChecksum(hout, preheaderdata[-3].lower(), True, formatspecs);
4731 if(prefcs!=prenewfcs and not skipchecksum):
4732 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
4733 return False;
4734 valid_archive = False;
4735 invalid_archive = True;
4736 prefhend = catfp.tell() - 1;
4737 prefcontentstart = catfp.tell();
4738 prefcontents = "";
4739 pyhascontents = False;
4740 if(prefsize>0):
4741 prefcontents = catfp.read(prefsize);
4742 prenewfccs = GetFileChecksum(prefcontents, preheaderdata[-3].lower(), False, formatspecs);
4743 pyhascontents = True;
4744 if(prefccs!=prenewfccs and not skipchecksum):
4745 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
4746 return False;
4747 catfp.seek(1, 1);
4748 il = il + 1;
4749 fileidnum = seekstart;
4750 realidnum = 0;
4751 while(fileidnum<seekend):
4752 catfhstart = catfp.tell();
4753 if(formatspecs[7]):
4754 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
4755 else:
4756 catheaderdata = ReadFileHeaderDataWoSize(catfp, formatspecs[5]);
4757 if(len(catheaderdata)==0):
4758 break;
4759 catfheadsize = int(catheaderdata[0], 16);
4760 catfnumfields = int(catheaderdata[1], 16);
4761 catftype = int(catheaderdata[2], 16);
4762 if(re.findall("^[.|/]", catheaderdata[3])):
4763 catfname = catheaderdata[3];
4764 else:
4765 catfname = "./"+catheaderdata[3];
4766 catfbasedir = os.path.dirname(catfname);
4767 catflinkname = catheaderdata[4];
4768 catfsize = int(catheaderdata[5], 16);
4769 catfatime = int(catheaderdata[6], 16);
4770 catfmtime = int(catheaderdata[7], 16);
4771 catfctime = int(catheaderdata[8], 16);
4772 catfbtime = int(catheaderdata[9], 16);
4773 catfmode = int(catheaderdata[10], 16);
4774 catfchmode = stat.S_IMODE(catfmode);
4775 catftypemod = stat.S_IFMT(catfmode);
4776 catfwinattributes = int(catheaderdata[11], 16);
4777 catfuid = int(catheaderdata[12], 16);
4778 catfuname = catheaderdata[13];
4779 catfgid = int(catheaderdata[14], 16);
4780 catfgname = catheaderdata[15];
4781 fid = int(catheaderdata[16], 16);
4782 finode = int(catheaderdata[17], 16);
4783 flinkcount = int(catheaderdata[18], 16);
4784 catfdev_minor = int(catheaderdata[19], 16);
4785 catfdev_major = int(catheaderdata[20], 16);
4786 catfrdev_minor = int(catheaderdata[21], 16);
4787 catfrdev_major = int(catheaderdata[22], 16);
4788 catfextrasize = int(catheaderdata[23], 16);
4789 catfextrafields = int(catheaderdata[24], 16);
4790 extrafieldslist = [];
4791 extrastart = 25;
4792 extraend = extrastart + catfextrafields;
4793 extrafieldslist = [];
4794 if(extrastart<extraend):
4795 extrafieldslist.append(catheaderdata[extrastart]);
4796 extrastart = extrastart + 1;
4797 catfchecksumtype = catheaderdata[extrastart].lower();
4798 catfcs = catheaderdata[extrastart + 1].lower();
4799 catfccs = catheaderdata[extrastart + 2].lower();
4800 hc = 0;
4801 hcmax = len(catheaderdata) - 2;
4802 hout = "";
4803 while(hc<hcmax):
4804 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
4805 hc = hc + 1;
4806 catnewfcs = GetFileChecksum(hout, catheaderdata[-3].lower(), True, formatspecs);
4807 if(catfcs!=catnewfcs and not skipchecksum):
4808 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
4809 return False;
4810 catfhend = catfp.tell() - 1;
4811 catfcontentstart = catfp.tell();
4812 catfcontents = "";
4813 pyhascontents = False;
4814 if(catfsize>0 and not listonly):
4815 catfcontents = catfp.read(catfsize);
4816 catnewfccs = GetFileChecksum(catfcontents, catheaderdata[-3].lower(), False, formatspecs);
4817 pyhascontents = True;
4818 if(catfccs!=catnewfccs and skipchecksum):
4819 VerbosePrintOut("File Content Checksum Error with file " + catfname + " at offset " + str(catfcontentstart));
4820 return False;
4821 if(catfsize>0 and listonly):
4822 catfp.seek(catfsize, 1);
4823 pyhascontents = False;
4824 catfp.seek(1, 1);
4825 catfcontentend = catfp.tell() - 1;
4826 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} });
4827 fileidnum = fileidnum + 1;
4828 realidnum = realidnum + 1;
4829 if(returnfp):
4830 catlist.update({'catfp': catfp});
4831 else:
4832 catfp.close();
4833 return catlist;
4835 create_alias_function("", __file_format_name__, "ToArray", ArchiveFileToArray);
4837 def ArchiveFileStringToArray(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4838 catfp = BytesIO(catstr);
4839 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4840 return listcatfiles;
4842 create_alias_function("", __file_format_name__, "StringToArray", ArchiveFileStringToArray);
4844 def TarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4845 catfp = BytesIO();
4846 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4847 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4848 return listcatfiles;
4850 def ZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4851 catfp = BytesIO();
4852 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4853 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4854 return listcatfiles;
4856 if(not rarfile_support):
4857 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4858 return False;
4860 if(rarfile_support):
4861 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4862 catfp = BytesIO();
4863 catfp = PackArchiveFileFromSevenZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4864 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4865 return listcatfiles;
4867 if(not py7zr_support):
4868 def SevenZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4869 return False;
4871 if(py7zr_support):
4872 def SevenZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4873 catfp = BytesIO();
4874 catfp = PackArchiveFileFromSevenZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4875 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4876 return listcatfiles;
4878 def InFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4879 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
4880 if(checkcompressfile=="tarfile"):
4881 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4882 elif(checkcompressfile=="zipfile"):
4883 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4884 elif(checkcompressfile=="catfile"):
4885 return ArchiveFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4886 elif(rarfile_support and checkcompressfile=="rarfile"):
4887 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4888 elif(py7zr_support and checkcompressfile=="7zipfile"):
4889 return SevenZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4890 else:
4891 return False;
4892 return False;
4894 def ListDirToArrayAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4895 catver = formatspecs[6];
4896 fileheaderver = str(int(catver.replace(".", "")));
4897 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4898 advancedlist = formatspecs[8];
4899 altinode = formatspecs[9];
4900 infilelist = [];
4901 if(infiles=="-"):
4902 for line in sys.stdin:
4903 infilelist.append(line.strip());
4904 infilelist = list(filter(None, infilelist));
4905 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
4906 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
4907 return False;
4908 with UncompressFile(infiles, formatspecs, "r") as finfile:
4909 for line in finfile:
4910 infilelist.append(line.strip());
4911 infilelist = list(filter(None, infilelist));
4912 else:
4913 if(isinstance(infiles, (list, tuple, ))):
4914 infilelist = list(filter(None, infiles));
4915 elif(isinstance(infiles, (str, ))):
4916 infilelist = list(filter(None, [infiles]));
4917 if(advancedlist):
4918 GetDirList = ListDirAdvanced(infilelist, followlink, False);
4919 else:
4920 GetDirList = ListDir(infilelist, followlink, False);
4921 if(not GetDirList):
4922 return False;
4923 curinode = 0;
4924 curfid = 0;
4925 inodelist = [];
4926 inodetofile = {};
4927 filetoinode = {};
4928 inodetocatinode = {};
4929 fileidnum = 0;
4930 fnumfiles = int(len(GetDirList));
4931 catver = formatspecs[6];
4932 fileheaderver = str(int(catver.replace(".", "")));
4933 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4934 fnumfileshex = format(int(fnumfiles), 'x').lower();
4935 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4936 catversion = re.findall(r"([\d]+)$", fileheader);
4937 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4938 if(checksumtype=="none" or checksumtype==""):
4939 catfileheadercshex = format(0, 'x').lower();
4940 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4941 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4942 elif(checksumtype=="crc16_ccitt"):
4943 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4944 elif(checksumtype=="adler32"):
4945 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4946 elif(checksumtype=="crc32"):
4947 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4948 elif(checksumtype=="crc64_ecma"):
4949 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4950 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4951 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4952 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4953 checksumoutstr = hashlib.new(checksumtype);
4954 checksumoutstr.update(fileheader.encode('UTF-8'));
4955 catfileheadercshex = checksumoutstr.hexdigest().lower();
4956 else:
4957 catfileheadercshex = format(0, 'x').lower();
4958 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4959 fheadtell = len(fileheader);
4960 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4961 for curfname in GetDirList:
4962 if(re.findall("^[.|/]", curfname)):
4963 fname = curfname;
4964 else:
4965 fname = "./"+curfname;
4966 if(verbose):
4967 VerbosePrintOut(fname);
4968 if(not followlink or followlink is None):
4969 fstatinfo = os.lstat(fname);
4970 else:
4971 fstatinfo = os.stat(fname);
4972 fpremode = fstatinfo.st_mode;
4973 finode = fstatinfo.st_ino;
4974 flinkcount = fstatinfo.st_nlink;
4975 ftype = 0;
4976 if(stat.S_ISREG(fpremode)):
4977 ftype = 0;
4978 elif(stat.S_ISLNK(fpremode)):
4979 ftype = 2;
4980 elif(stat.S_ISCHR(fpremode)):
4981 ftype = 3;
4982 elif(stat.S_ISBLK(fpremode)):
4983 ftype = 4;
4984 elif(stat.S_ISDIR(fpremode)):
4985 ftype = 5;
4986 elif(stat.S_ISFIFO(fpremode)):
4987 ftype = 6;
4988 elif(stat.S_ISSOCK(fpremode)):
4989 ftype = 8;
4990 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
4991 ftype = 9;
4992 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
4993 ftype = 10;
4994 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
4995 ftype = 11;
4996 else:
4997 ftype = 0;
4998 flinkname = "";
4999 fbasedir = os.path.dirname(fname);
5000 fcurfid = curfid;
5001 if(not followlink and finode!=0):
5002 if(ftype!=1):
5003 if(finode in inodelist):
5004 ftype = 1;
5005 flinkname = inodetofile[finode];
5006 if(altinode):
5007 fcurinode = finode;
5008 else:
5009 fcurinode = inodetocatinode[finode];
5010 if(finode not in inodelist):
5011 inodelist.append(finode);
5012 inodetofile.update({finode: fname});
5013 inodetocatinode.update({finode: curinode});
5014 if(altinode):
5015 fcurinode = finode;
5016 else:
5017 fcurinode = curinode;
5018 curinode = curinode + 1;
5019 else:
5020 fcurinode = curinode;
5021 curinode = curinode + 1;
5022 curfid = curfid + 1;
5023 if(ftype==2):
5024 flinkname = os.readlink(fname);
5025 fdev = fstatinfo.st_dev;
5026 getfdev = GetDevMajorMinor(fdev);
5027 fdev_minor = getfdev[0];
5028 fdev_major = getfdev[1];
5029 frdev = fstatinfo.st_dev;
5030 if(hasattr(fstatinfo, "st_rdev")):
5031 frdev = fstatinfo.st_rdev;
5032 else:
5033 frdev = fstatinfo.st_dev;
5034 getfrdev = GetDevMajorMinor(frdev);
5035 frdev_minor = getfrdev[0];
5036 frdev_major = getfrdev[1];
5037 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
5038 fsize = "0";
5039 if(ftype==0 or ftype==7):
5040 fsize = fstatinfo.st_size;
5041 fatime = fstatinfo.st_atime;
5042 fmtime = fstatinfo.st_mtime;
5043 fctime = fstatinfo.st_ctime;
5044 if(hasattr(fstatinfo, "st_birthtime")):
5045 fbtime = fstatinfo.st_birthtime;
5046 else:
5047 fbtime = fstatinfo.st_ctime;
5048 fmode = fstatinfo.st_mode;
5049 fchmode = stat.S_IMODE(fstatinfo.st_mode);
5050 ftypemod = stat.S_IFMT(fstatinfo.st_mode);
5051 fuid = fstatinfo.st_uid;
5052 fgid = fstatinfo.st_gid;
5053 funame = "";
5054 try:
5055 import pwd;
5056 try:
5057 userinfo = pwd.getpwuid(fstatinfo.st_uid);
5058 funame = userinfo.pw_name;
5059 except KeyError:
5060 funame = "";
5061 except ImportError:
5062 funame = "";
5063 fgname = "";
5064 try:
5065 import grp;
5066 try:
5067 groupinfo = grp.getgrgid(fstatinfo.st_gid);
5068 fgname = groupinfo.gr_name;
5069 except KeyError:
5070 fgname = "";
5071 except ImportError:
5072 fgname = "";
5073 fdev_minor = fdev_minor;
5074 fdev_major = fdev_major;
5075 frdev_minor = frdev_minor;
5076 frdev_major = frdev_major;
5077 finode = finode;
5078 flinkcount = flinkcount;
5079 if(hasattr(fstatinfo, "st_file_attributes")):
5080 fwinattributes = fstatinfo.st_file_attributes;
5081 else:
5082 fwinattributes = 0;
5083 fcontents = "".encode('UTF-8');
5084 chunk_size = 1024;
5085 if(ftype==0 or ftype==7):
5086 with open(fname, "rb") as fpc:
5087 while(True):
5088 chunk = fpc.read(chunk_size);
5089 if(not chunk):
5090 break
5091 fcontents += chunk;
5092 if(followlink and (ftype==1 or ftype==2)):
5093 flstatinfo = os.stat(flinkname);
5094 with open(flinkname, "rb") as fpc:
5095 while(True):
5096 chunk = fpc.read(chunk_size);
5097 if(not chunk):
5098 break;
5099 fcontents += chunk;
5100 ftypehex = format(ftype, 'x').lower();
5101 extrafields = len(extradata);
5102 extrafieldslist = extradata;
5103 catfextrafields = extrafields;
5104 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5105 if(len(extradata)>0):
5106 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5107 extrasizelen = len(extrasizestr);
5108 extrasizelenhex = format(extrasizelen, 'x').lower();
5109 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()];
5110 catoutlen = len(catoutlist) + len(extradata) + 3;
5111 catoutlenhex = format(catoutlen, 'x').lower();
5112 catoutlist.insert(0, catoutlenhex);
5113 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5114 if(len(extradata)>0):
5115 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5116 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5117 catfnumfields = catoutlen;
5118 if(checksumtype=="none" or checksumtype==""):
5119 catfileheadercshex = format(0, 'x').lower();
5120 catfilecontentcshex = format(0, 'x').lower();
5121 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5122 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5123 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5124 elif(checksumtype=="crc16_ccitt"):
5125 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5126 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5127 elif(checksumtype=="adler32"):
5128 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5129 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5130 elif(checksumtype=="crc32"):
5131 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5132 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5133 elif(checksumtype=="crc64_ecma"):
5134 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5135 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5136 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5137 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5138 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5139 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5140 checksumoutstr = hashlib.new(checksumtype);
5141 checksumoutstr.update("".encode('UTF-8'));
5142 catfileheadercshex = checksumoutstr.hexdigest().lower();
5143 checksumoutstr = hashlib.new(checksumtype);
5144 checksumoutstr.update(fcontents);
5145 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5146 else:
5147 catfileheadercshex = format(0, 'x').lower();
5148 catfilecontentcshex = format(0, 'x').lower();
5149 catfhstart = fheadtell;
5150 fheadtell += len(catfileoutstr);
5151 catfhend = fheadtell - 1;
5152 catfcontentstart = fheadtell;
5153 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5154 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5155 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5156 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex]
5157 if(checksumtype=="none" or checksumtype==""):
5158 catfileheadercshex = format(0, 'x').lower();
5159 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5160 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5161 elif(checksumtype=="crc16_ccitt"):
5162 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5163 elif(checksumtype=="adler32"):
5164 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5165 elif(checksumtype=="crc32"):
5166 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5167 elif(checksumtype=="crc64_ecma"):
5168 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5169 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5170 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5171 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5172 checksumoutstr = hashlib.new(checksumtype);
5173 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5174 catfileheadercshex = checksumoutstr.hexdigest().lower();
5175 else:
5176 catfileheadercshex = format(0, 'x').lower();
5177 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5178 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5179 nullstrecd = formatspecs[5].encode('UTF-8');
5180 fheadtell += len(catfileoutstr) + 1;
5181 catfcontentend = fheadtell - 1;
5182 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5183 pyhascontents = False;
5184 if(int(fsize)>0 and not listonly):
5185 pyhascontents = True;
5186 if(int(fsize)>0 and listonly):
5187 fcontents = "";
5188 pyhascontents = False;
5189 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} });
5190 fileidnum = fileidnum + 1;
5191 return catlist;
5193 def TarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5194 curinode = 0;
5195 curfid = 0;
5196 inodelist = [];
5197 inodetofile = {};
5198 filetoinode = {};
5199 inodetocatinode = {};
5200 fileidnum = 0;
5201 if(infile=="-"):
5202 infile = BytesIO();
5203 if(hasattr(sys.stdin, "buffer")):
5204 shutil.copyfileobj(sys.stdin.buffer, infile);
5205 else:
5206 shutil.copyfileobj(sys.stdin, infile);
5207 infile.seek(0, 0);
5208 if(not infile):
5209 return False;
5210 infile.seek(0, 0);
5211 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
5212 infile = download_file_from_internet_file(infile);
5213 infile.seek(0, 0);
5214 if(not infile):
5215 return False;
5216 infile.seek(0, 0);
5217 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
5218 return False;
5219 elif(os.path.exists(infile) and os.path.isfile(infile)):
5220 try:
5221 if(not tarfile.is_tarfile(infile)):
5222 return False;
5223 except AttributeError:
5224 if(not is_tarfile(infile)):
5225 return False;
5226 else:
5227 return False;
5228 try:
5229 if(hasattr(infile, "read") or hasattr(infile, "write")):
5230 tarfp = tarfile.open(fileobj=infile, mode="r");
5231 else:
5232 tarfp = tarfile.open(infile, "r");
5233 except FileNotFoundError:
5234 return False;
5235 fnumfiles = int(len(tarfp.getmembers()));
5236 catver = formatspecs[6];
5237 fileheaderver = str(int(catver.replace(".", "")));
5238 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5239 fnumfileshex = format(int(fnumfiles), 'x').lower();
5240 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5241 catversion = re.findall(r"([\d]+)$", fileheader);
5242 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5243 if(checksumtype=="none" or checksumtype==""):
5244 catfileheadercshex = format(0, 'x').lower();
5245 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5246 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5247 elif(checksumtype=="crc16_ccitt"):
5248 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5249 elif(checksumtype=="adler32"):
5250 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5251 elif(checksumtype=="crc32"):
5252 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5253 elif(checksumtype=="crc64_ecma"):
5254 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5255 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5256 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5257 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5258 checksumoutstr = hashlib.new(checksumtype);
5259 checksumoutstr.update(fileheader.encode('UTF-8'));
5260 catfileheadercshex = checksumoutstr.hexdigest().lower();
5261 else:
5262 catfileheadercshex = format(0, 'x').lower();
5263 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5264 fheadtell = len(fileheader);
5265 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5266 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
5267 if(re.findall("^[.|/]", member.name)):
5268 fname = member.name;
5269 else:
5270 fname = "./"+member.name;
5271 if(verbose):
5272 VerbosePrintOut(fname);
5273 fpremode = member.mode;
5274 ffullmode = member.mode;
5275 flinkcount = 0;
5276 ftype = 0;
5277 if(member.isreg()):
5278 ffullmode = member.mode + stat.S_IFREG;
5279 ftype = 0;
5280 elif(member.isdev()):
5281 ffullmode = member.mode;
5282 ftype = 7;
5283 elif(member.islnk()):
5284 ffullmode = member.mode + stat.S_IFREG;
5285 ftype = 1;
5286 elif(member.issym()):
5287 ffullmode = member.mode + stat.S_IFLNK;
5288 ftype = 2;
5289 elif(member.ischr()):
5290 ffullmode = member.mode + stat.S_IFCHR;
5291 ftype = 3;
5292 elif(member.isblk()):
5293 ffullmode = member.mode + stat.S_IFBLK;
5294 ftype = 4;
5295 elif(member.isdir()):
5296 ffullmode = member.mode + stat.S_IFDIR;
5297 ftype = 5;
5298 elif(member.isfifo()):
5299 ffullmode = member.mode + stat.S_IFIFO;
5300 ftype = 6;
5301 elif(member.issparse()):
5302 ffullmode = member.mode;
5303 ftype = 12;
5304 else:
5305 ffullmode = member.mode;
5306 ftype = 0;
5307 flinkname = "";
5308 fbasedir = os.path.dirname(fname);
5309 fcurfid = curfid;
5310 fcurinode = curfid;
5311 finode = fcurinode;
5312 curfid = curfid + 1;
5313 if(ftype==2):
5314 flinkname = member.linkname;
5315 fdev_minor = member.devminor;
5316 fdev_major = member.devmajor;
5317 frdev_minor = member.devminor;
5318 frdev_major = member.devmajor;
5319 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
5320 fsize = "0";
5321 elif(ftype==0 or ftype==7):
5322 fsize = member.size;
5323 else:
5324 fsize = member.size;
5325 fatime = member.mtime;
5326 fmtime = member.mtime;
5327 fctime = member.mtime;
5328 fbtime = member.mtime;
5329 fmode = ffullmode;
5330 fchmode = stat.S_IMODE(ffullmode);
5331 ftypemod = stat.S_IFMT(ffullmode);
5332 fuid = member.uid;
5333 fgid = member.gid;
5334 funame = member.uname;
5335 fgname = member.gname;
5336 flinkcount = flinkcount;
5337 fwinattributes = int(0);
5338 fcontents = "".encode('UTF-8');
5339 chunk_size = 1024;
5340 if(ftype==0 or ftype==7):
5341 with tarfp.extractfile(member) as fpc:
5342 while(True):
5343 chunk = fpc.read(chunk_size);
5344 if(not chunk):
5345 break
5346 fcontents += chunk;
5347 ftypehex = format(ftype, 'x').lower();
5348 extrafields = len(extradata);
5349 extrafieldslist = extradata;
5350 catfextrafields = extrafields;
5351 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5352 if(len(extradata)>0):
5353 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5354 extrasizelen = len(extrasizestr);
5355 extrasizelenhex = format(extrasizelen, 'x').lower();
5356 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()];
5357 catoutlen = len(catoutlist) + len(extradata) + 3;
5358 catoutlenhex = format(catoutlen, 'x').lower();
5359 catoutlist.insert(0, catoutlenhex);
5360 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5361 if(len(extradata)>0):
5362 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5363 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5364 catfnumfields = catoutlen;
5365 if(checksumtype=="none" or checksumtype==""):
5366 catfileheadercshex = format(0, 'x').lower();
5367 catfilecontentcshex = format(0, 'x').lower();
5368 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5369 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5370 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5371 elif(checksumtype=="crc16_ccitt"):
5372 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5373 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5374 elif(checksumtype=="adler32"):
5375 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5376 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5377 elif(checksumtype=="crc32"):
5378 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5379 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5380 elif(checksumtype=="crc64_ecma"):
5381 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5382 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5383 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5384 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5385 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5386 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5387 checksumoutstr = hashlib.new(checksumtype);
5388 checksumoutstr.update("".encode('UTF-8'));
5389 catfileheadercshex = checksumoutstr.hexdigest().lower();
5390 checksumoutstr = hashlib.new(checksumtype);
5391 checksumoutstr.update(fcontents);
5392 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5393 else:
5394 catfileheadercshex = format(0, 'x').lower();
5395 catfilecontentcshex = format(0, 'x').lower();
5396 catfhstart = fheadtell;
5397 fheadtell += len(catfileoutstr);
5398 catfhend = fheadtell - 1;
5399 catfcontentstart = fheadtell;
5400 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5401 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5402 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5403 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex]
5404 if(checksumtype=="none" or checksumtype==""):
5405 catfileheadercshex = format(0, 'x').lower();
5406 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5407 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5408 elif(checksumtype=="crc16_ccitt"):
5409 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5410 elif(checksumtype=="adler32"):
5411 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5412 elif(checksumtype=="crc32"):
5413 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5414 elif(checksumtype=="crc64_ecma"):
5415 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5416 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5417 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5418 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5419 checksumoutstr = hashlib.new(checksumtype);
5420 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5421 catfileheadercshex = checksumoutstr.hexdigest().lower();
5422 else:
5423 catfileheadercshex = format(0, 'x').lower();
5424 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5425 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5426 nullstrecd = formatspecs[5].encode('UTF-8');
5427 fheadtell += len(catfileoutstr) + 1;
5428 catfcontentend = fheadtell - 1;
5429 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5430 pyhascontents = False;
5431 if(int(fsize)>0 and not listonly):
5432 pyhascontents = True;
5433 if(int(fsize)>0 and listonly):
5434 fcontents = "";
5435 pyhascontents = False;
5436 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} });
5437 fileidnum = fileidnum + 1;
5438 return catlist;
5440 def ZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5441 curinode = 0;
5442 curfid = 0;
5443 inodelist = [];
5444 inodetofile = {};
5445 filetoinode = {};
5446 inodetocatinode = {};
5447 fileidnum = 0;
5448 if(infile=="-"):
5449 infile = BytesIO();
5450 if(hasattr(sys.stdin, "buffer")):
5451 shutil.copyfileobj(sys.stdin.buffer, infile);
5452 else:
5453 shutil.copyfileobj(sys.stdin, infile);
5454 infile.seek(0, 0);
5455 if(not infile):
5456 return False;
5457 infile.seek(0, 0);
5458 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
5459 infile = download_file_from_internet_file(infile);
5460 infile.seek(0, 0);
5461 if(not infile):
5462 return False;
5463 infile.seek(0, 0);
5464 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
5465 return False;
5466 else:
5467 return False;
5468 if(not zipfile.is_zipfile(infile)):
5469 return False;
5470 try:
5471 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
5472 except FileNotFoundError:
5473 return False;
5474 ziptest = zipfp.testzip();
5475 if(ziptest):
5476 VerbosePrintOut("Bad file found!");
5477 fnumfiles = int(len(zipfp.infolist()));
5478 catver = formatspecs[6];
5479 fileheaderver = str(int(catver.replace(".", "")));
5480 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5481 catversion = re.findall(r"([\d]+)$", fileheader);
5482 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5483 fnumfileshex = format(int(fnumfiles), 'x').lower();
5484 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5485 if(checksumtype=="none" or checksumtype==""):
5486 catfileheadercshex = format(0, 'x').lower();
5487 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5488 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5489 elif(checksumtype=="crc16_ccitt"):
5490 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5491 elif(checksumtype=="adler32"):
5492 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5493 elif(checksumtype=="crc32"):
5494 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5495 elif(checksumtype=="crc64_ecma"):
5496 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5497 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5498 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5499 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5500 checksumoutstr = hashlib.new(checksumtype);
5501 checksumoutstr.update(fileheader.encode('UTF-8'));
5502 catfileheadercshex = checksumoutstr.hexdigest().lower();
5503 else:
5504 catfileheadercshex = format(0, 'x').lower();
5505 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5506 fheadtell = len(fileheader);
5507 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5508 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
5509 if(re.findall("^[.|/]", member.filename)):
5510 fname = member.filename;
5511 else:
5512 fname = "./"+member.filename;
5513 zipinfo = zipfp.getinfo(member.filename);
5514 if(verbose):
5515 VerbosePrintOut(fname);
5516 if(not member.is_dir()):
5517 fpremode = stat.S_IFREG + 438;
5518 elif(member.is_dir()):
5519 fpremode = stat.S_IFDIR + 511;
5520 flinkcount = 0;
5521 ftype = 0;
5522 if(not member.is_dir()):
5523 ftype = 0;
5524 elif(member.is_dir()):
5525 ftype = 5;
5526 flinkname = "";
5527 fbasedir = os.path.dirname(fname);
5528 fcurfid = curfid;
5529 fcurinode = curfid;
5530 finode = fcurinode;
5531 curfid = curfid + 1;
5532 fdev_minor = 0;
5533 fdev_major = 0;
5534 frdev_minor = 0;
5535 frdev_major = 0;
5536 if(ftype==5):
5537 fsize = "0";
5538 elif(ftype==0):
5539 fsize = member.file_size;
5540 else:
5541 fsize = member.file_size;
5542 fatime = time.mktime(member.date_time + (0, 0, -1));
5543 fmtime = time.mktime(member.date_time + (0, 0, -1));
5544 fctime = time.mktime(member.date_time + (0, 0, -1));
5545 fbtime = time.mktime(member.date_time + (0, 0, -1));
5546 if(zipinfo.create_system==0 or zipinfo.create_system==10):
5547 fwinattributes = int(zipinfo.external_attr);
5548 if(not member.is_dir()):
5549 fmode = int(stat.S_IFREG + 438);
5550 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5551 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5552 elif(member.is_dir()):
5553 fmode = int(stat.S_IFDIR + 511);
5554 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5555 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5556 elif(zipinfo.create_system==3):
5557 fwinattributes = int(0);
5558 fmode = int(zipinfo.external_attr);
5559 else:
5560 fwinattributes = int(0);
5561 if(not member.is_dir()):
5562 fmode = int(stat.S_IFREG + 438);
5563 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5564 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5565 elif(member.is_dir()):
5566 fmode = int(stat.S_IFDIR + 511);
5567 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5568 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5569 try:
5570 fuid = os.getuid();
5571 except AttributeError:
5572 fuid = 0;
5573 except KeyError:
5574 fuid = 0;
5575 try:
5576 fgid = os.getgid();
5577 except AttributeError:
5578 fgid = 0;
5579 except KeyError:
5580 fgid = 0;
5581 try:
5582 import pwd;
5583 try:
5584 userinfo = pwd.getpwuid(os.getuid());
5585 funame = userinfo.pw_name;
5586 except KeyError:
5587 funame = "";
5588 except AttributeError:
5589 funame = "";
5590 except ImportError:
5591 funame = "";
5592 fgname = "";
5593 try:
5594 import grp;
5595 try:
5596 groupinfo = grp.getgrgid(os.getgid());
5597 fgname = groupinfo.gr_name;
5598 except KeyError:
5599 fgname = "";
5600 except AttributeError:
5601 fgname = "";
5602 except ImportError:
5603 fgname = "";
5604 fcontents = "".encode('UTF-8');
5605 if(ftype==0):
5606 fcontents = zipfp.read(member.filename);
5607 ftypehex = format(ftype, 'x').lower();
5608 extrafields = len(extradata);
5609 extrafieldslist = extradata;
5610 catfextrafields = extrafields;
5611 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5612 if(len(extradata)>0):
5613 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5614 extrasizelen = len(extrasizestr);
5615 extrasizelenhex = format(extrasizelen, 'x').lower();
5616 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()];
5617 catoutlen = len(catoutlist) + len(extradata) + 3;
5618 catoutlenhex = format(catoutlen, 'x').lower();
5619 catoutlist.insert(0, catoutlenhex);
5620 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5621 if(len(extradata)>0):
5622 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5623 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5624 catfnumfields = catoutlen;
5625 if(checksumtype=="none" or checksumtype==""):
5626 catfileheadercshex = format(0, 'x').lower();
5627 catfilecontentcshex = format(0, 'x').lower();
5628 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5629 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5630 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5631 elif(checksumtype=="crc16_ccitt"):
5632 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5633 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5634 elif(checksumtype=="adler32"):
5635 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5636 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5637 elif(checksumtype=="crc32"):
5638 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5639 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5640 elif(checksumtype=="crc64_ecma"):
5641 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5642 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5643 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5644 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5645 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5646 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5647 checksumoutstr = hashlib.new(checksumtype);
5648 checksumoutstr.update("".encode('UTF-8'));
5649 catfileheadercshex = checksumoutstr.hexdigest().lower();
5650 checksumoutstr = hashlib.new(checksumtype);
5651 checksumoutstr.update(fcontents);
5652 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5653 else:
5654 catfileheadercshex = format(0, 'x').lower();
5655 catfilecontentcshex = format(0, 'x').lower();
5656 catfhstart = fheadtell;
5657 fheadtell += len(catfileoutstr);
5658 catfhend = fheadtell - 1;
5659 catfcontentstart = fheadtell;
5660 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5661 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5662 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5663 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex];
5664 if(checksumtype=="none" or checksumtype==""):
5665 catfileheadercshex = format(0, 'x').lower();
5666 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5667 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5668 elif(checksumtype=="crc16_ccitt"):
5669 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5670 elif(checksumtype=="adler32"):
5671 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5672 elif(checksumtype=="crc32"):
5673 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5674 elif(checksumtype=="crc64_ecma"):
5675 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5676 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5677 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5678 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5679 checksumoutstr = hashlib.new(checksumtype);
5680 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5681 catfileheadercshex = checksumoutstr.hexdigest().lower();
5682 else:
5683 catfileheadercshex = format(0, 'x').lower();
5684 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5685 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5686 nullstrecd = formatspecs[5].encode('UTF-8');
5687 fheadtell += len(catfileoutstr) + 1;
5688 catfcontentend = fheadtell - 1;
5689 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5690 pyhascontents = False;
5691 if(int(fsize)>0 and not listonly):
5692 pyhascontents = True;
5693 if(int(fsize)>0 and listonly):
5694 fcontents = "";
5695 pyhascontents = False;
5696 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} });
5697 fileidnum = fileidnum + 1;
5698 return catlist;
5700 if(not rarfile_support):
5701 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5702 return False;
5704 if(rarfile_support):
5705 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5706 curinode = 0;
5707 curfid = 0;
5708 inodelist = [];
5709 inodetofile = {};
5710 filetoinode = {};
5711 inodetocatinode = {};
5712 fileidnum = 0;
5713 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
5714 return False;
5715 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
5716 return False;
5717 rarfp = rarfile.RarFile(infile, "r");
5718 rartest = rarfp.testrar();
5719 if(rartest):
5720 VerbosePrintOut("Bad file found!");
5721 fnumfiles = int(len(rarfp.infolist()));
5722 catver = formatspecs[6];
5723 fileheaderver = str(int(catver.replace(".", "")));
5724 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5725 catversion = re.findall(r"([\d]+)$", fileheader);
5726 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
5727 fnumfileshex = format(int(fnumfiles), 'x').lower();
5728 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5729 if(checksumtype=="none" or checksumtype==""):
5730 catfileheadercshex = format(0, 'x').lower();
5731 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5732 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5733 elif(checksumtype=="crc16_ccitt"):
5734 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
5735 elif(checksumtype=="adler32"):
5736 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5737 elif(checksumtype=="crc32"):
5738 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
5739 elif(checksumtype=="crc64_ecma"):
5740 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5741 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5742 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5743 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5744 checksumoutstr = hashlib.new(checksumtype);
5745 checksumoutstr.update(fileheader.encode('UTF-8'));
5746 catfileheadercshex = checksumoutstr.hexdigest().lower();
5747 else:
5748 catfileheadercshex = format(0, 'x').lower();
5749 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
5750 fheadtell = len(fileheader);
5751 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
5752 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
5753 is_unix = False;
5754 is_windows = False;
5755 if(member.host_os==rarfile.RAR_OS_UNIX):
5756 is_windows = False;
5757 try:
5758 member.external_attr
5759 is_unix = True;
5760 except AttributeError:
5761 is_unix = False;
5762 elif(member.host_os==rarfile.RAR_OS_WIN32):
5763 is_unix = False;
5764 try:
5765 member.external_attr
5766 is_windows = True;
5767 except AttributeError:
5768 is_windows = False;
5769 else:
5770 is_unix = False;
5771 is_windows = False;
5772 if(re.findall("^[.|/]", member.filename)):
5773 fname = member.filename;
5774 else:
5775 fname = "./"+member.filename;
5776 rarinfo = rarfp.getinfo(member.filename);
5777 if(verbose):
5778 VerbosePrintOut(fname);
5779 if(is_unix and member.external_attr !=0):
5780 fpremode = int(member.external_attr);
5781 elif(member.is_file()):
5782 fpremode = stat.S_IFREG + 438;
5783 elif(member.is_symlink()):
5784 fpremode = stat.S_IFLNK + 438;
5785 elif(member.is_dir()):
5786 fpremode = stat.S_IFDIR + 511;
5787 if(is_windows and member.external_attr !=0):
5788 fwinattributes = int(member.external_attr);
5789 else:
5790 fwinattributes = int(0);
5791 flinkcount = 0;
5792 ftype = 0;
5793 if(member.is_file()):
5794 ftype = 0;
5795 elif(member.is_symlink()):
5796 ftype = 2;
5797 elif(member.is_dir()):
5798 ftype = 5;
5799 flinkname = "";
5800 if(ftype==2):
5801 flinkname = rarfp.read(member.filename).decode("UTF-8");
5802 fbasedir = os.path.dirname(fname);
5803 fcurfid = curfid;
5804 fcurinode = curfid;
5805 finode = fcurinode;
5806 curfid = curfid + 1;
5807 fdev_minor = 0;
5808 fdev_major = 0;
5809 frdev_minor = 0;
5810 frdev_major = 0;
5811 if(ftype==5):
5812 fsize = "0";
5813 if(ftype==0):
5814 fsize = member.file_size;
5815 try:
5816 if(member.atime):
5817 fatime = int(member.atime.timestamp());
5818 else:
5819 fatime = int(member.mtime.timestamp());
5820 except AttributeError:
5821 fatime = int(member.mtime.timestamp());
5822 fmtime = int(member.mtime.timestamp());
5823 try:
5824 if(member.ctime):
5825 fctime = int(member.ctime.timestamp());
5826 else:
5827 fctime = int(member.mtime.timestamp());
5828 except AttributeError:
5829 fctime = int(member.mtime.timestamp());
5830 fbtime = int(member.mtime.timestamp());
5831 if(is_unix and member.external_attr !=0):
5832 fmode = int(member.external_attr);
5833 fchmode = int(stat.S_IMODE(member.external_attr));
5834 ftypemod = int(stat.S_IFMT(member.external_attr));
5835 elif(member.is_file()):
5836 fmode = int(stat.S_IFREG + 438)
5837 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
5838 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
5839 elif(member.is_symlink()):
5840 fmode = int(stat.S_IFLNK + 438)
5841 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
5842 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
5843 elif(member.is_dir()):
5844 fmode = int(stat.S_IFDIR + 511)
5845 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
5846 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
5847 try:
5848 fuid = os.getuid();
5849 except AttributeError:
5850 fuid = 0;
5851 except KeyError:
5852 fuid = 0;
5853 try:
5854 fgid = os.getgid();
5855 except AttributeError:
5856 fgid = 0;
5857 except KeyError:
5858 fgid = 0;
5859 try:
5860 import pwd;
5861 try:
5862 userinfo = pwd.getpwuid(os.getuid());
5863 funame = userinfo.pw_name;
5864 except KeyError:
5865 funame = "";
5866 except AttributeError:
5867 funame = "";
5868 except ImportError:
5869 funame = "";
5870 fgname = "";
5871 try:
5872 import grp;
5873 try:
5874 groupinfo = grp.getgrgid(os.getgid());
5875 fgname = groupinfo.gr_name;
5876 except KeyError:
5877 fgname = "";
5878 except AttributeError:
5879 fgname = "";
5880 except ImportError:
5881 fgname = "";
5882 fcontents = "".encode('UTF-8');
5883 if(ftype==0):
5884 fcontents = rarfp.read(member.filename);
5885 ftypehex = format(ftype, 'x').lower();
5886 extrafields = len(extradata);
5887 extrafieldslist = extradata;
5888 catfextrafields = extrafields;
5889 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5890 if(len(extradata)>0):
5891 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5892 extrasizelen = len(extrasizestr);
5893 extrasizelenhex = format(extrasizelen, 'x').lower();
5894 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()];
5895 catoutlen = len(catoutlist) + len(extradata) + 3;
5896 catoutlenhex = format(catoutlen, 'x').lower();
5897 catoutlist.insert(0, catoutlenhex);
5898 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
5899 if(len(extradata)>0):
5900 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5901 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5902 catfnumfields = 24 + catfextrafields;
5903 if(checksumtype=="none" or checksumtype==""):
5904 catfileheadercshex = format(0, 'x').lower();
5905 catfilecontentcshex = format(0, 'x').lower();
5906 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5907 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5908 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5909 elif(checksumtype=="crc16_ccitt"):
5910 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5911 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5912 elif(checksumtype=="adler32"):
5913 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5914 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5915 elif(checksumtype=="crc32"):
5916 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5917 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5918 elif(checksumtype=="crc64_ecma"):
5919 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5920 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5921 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5922 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5923 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5924 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5925 checksumoutstr = hashlib.new(checksumtype);
5926 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5927 catfileheadercshex = checksumoutstr.hexdigest().lower();
5928 else:
5929 catfileheadercshex = format(0, 'x').lower();
5930 catfilecontentcshex = format(0, 'x').lower();
5931 catfhstart = fheadtell;
5932 fheadtell += len(catfileoutstr);
5933 catfhend = fheadtell - 1;
5934 catfcontentstart = fheadtell;
5935 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5936 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5937 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5938 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex];
5939 if(checksumtype=="none" or checksumtype==""):
5940 catfileheadercshex = format(0, 'x').lower();
5941 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5942 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5943 elif(checksumtype=="crc16_ccitt"):
5944 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5945 elif(checksumtype=="adler32"):
5946 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5947 elif(checksumtype=="crc32"):
5948 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5949 elif(checksumtype=="crc64_ecma"):
5950 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5951 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5952 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5953 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5954 checksumoutstr = hashlib.new(checksumtype);
5955 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5956 catfileheadercshex = checksumoutstr.hexdigest().lower();
5957 else:
5958 catfileheadercshex = format(0, 'x').lower();
5959 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5960 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5961 nullstrecd = formatspecs[5].encode('UTF-8');
5962 fheadtell += len(catfileoutstr) + 1;
5963 catfcontentend = fheadtell - 1;
5964 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5965 pyhascontents = False;
5966 if(int(fsize)>0 and not listonly):
5967 pyhascontents = True;
5968 if(int(fsize)>0 and listonly):
5969 fcontents = "";
5970 pyhascontents = False;
5971 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} });
5972 fileidnum = fileidnum + 1;
5973 return catlist;
5975 if(not py7zr_support):
5976 def SevenZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5977 return False;
5979 if(py7zr_support):
5980 def SevenZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5981 curinode = 0;
5982 curfid = 0;
5983 inodelist = [];
5984 inodetofile = {};
5985 filetoinode = {};
5986 inodetocatinode = {};
5987 fileidnum = 0;
5988 szpfp = py7zr.SevenZipFile(infile, mode="r");
5989 file_content = szpfp.readall();
5990 #sztest = szpfp.testzip();
5991 sztestalt = szpfp.test();
5992 if(sztestalt):
5993 VerbosePrintOut("Bad file found!");
5994 numfiles = int(len(szpfp.list()));
5995 catver = formatspecs[6];
5996 fileheaderver = str(int(catver.replace(".", "")));
5997 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5998 catversion = re.findall(r"([\d]+)$", fileheader);
5999 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
6000 fnumfileshex = format(int(fnumfiles), 'x').lower();
6001 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
6002 if(checksumtype=="none" or checksumtype==""):
6003 catfileheadercshex = format(0, 'x').lower();
6004 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6005 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
6006 elif(checksumtype=="crc16_ccitt"):
6007 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
6008 elif(checksumtype=="adler32"):
6009 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
6010 elif(checksumtype=="crc32"):
6011 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
6012 elif(checksumtype=="crc64_ecma"):
6013 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6014 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6015 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6016 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6017 checksumoutstr = hashlib.new(checksumtype);
6018 checksumoutstr.update(fileheader.encode('UTF-8'));
6019 catfileheadercshex = checksumoutstr.hexdigest().lower();
6020 else:
6021 catfileheadercshex = format(0, 'x').lower();
6022 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
6023 fheadtell = len(fileheader);
6024 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
6025 for member in sorted(szpfp.list(), key=lambda x: x.filename):
6026 if(re.findall("^[.|/]", member.filename)):
6027 fname = member.filename;
6028 else:
6029 fname = "./"+member.filename;
6030 if(not member.is_directory):
6031 fpremode = int(stat.S_IFREG + 438);
6032 elif(member.is_directory):
6033 fpremode = int(stat.S_IFDIR + 511);
6034 fwinattributes = int(0);
6035 flinkcount = 0;
6036 ftype = 0;
6037 if(member.is_directory):
6038 ftype = 5;
6039 else:
6040 ftype = 0;
6041 flinkname = "";
6042 fbasedir = os.path.dirname(fname);
6043 fcurfid = curfid;
6044 fcurinode = curfid;
6045 finode = fcurinode;
6046 curfid = curfid + 1;
6047 fdev_minor = 0;
6048 fdev_major = 0;
6049 frdev_minor = 0;
6050 frdev_major = 0;
6051 if(ftype==5):
6052 fsize = "0";
6053 fatime = int(member.creationtime.timestamp());
6054 fmtime = int(member.creationtime.timestamp());
6055 fctime = int(member.creationtime.timestamp());
6056 fbtime = int(member.creationtime.timestamp());
6057 if(member.is_directory):
6058 fmode = int(stat.S_IFDIR + 511)
6059 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
6060 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
6061 else:
6062 fmode = int(stat.S_IFLNK + 438)
6063 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
6064 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
6065 try:
6066 fuid = os.getuid();
6067 except AttributeError:
6068 fuid = 0;
6069 except KeyError:
6070 fuid = 0;
6071 try:
6072 fgid = os.getgid();
6073 except AttributeError:
6074 fgid = 0;
6075 except KeyError:
6076 fgid = 0;
6077 try:
6078 import pwd;
6079 try:
6080 userinfo = pwd.getpwuid(os.getuid());
6081 funame = userinfo.pw_name;
6082 except KeyError:
6083 funame = "";
6084 except AttributeError:
6085 funame = "";
6086 except ImportError:
6087 funame = "";
6088 fgname = "";
6089 try:
6090 import grp;
6091 try:
6092 groupinfo = grp.getgrgid(os.getgid());
6093 fgname = groupinfo.gr_name;
6094 except KeyError:
6095 fgname = "";
6096 except AttributeError:
6097 fgname = "";
6098 except ImportError:
6099 fgname = "";
6100 fcontents = "".encode('UTF-8');
6101 if(ftype==0):
6102 fcontents = file_content[member.filename].read();
6103 fsize = format(len(fcontents), 'x').lower();
6104 fileop.close();
6105 ftypehex = format(ftype, 'x').lower();
6106 extrafields = len(extradata);
6107 extrafieldslist = extradata;
6108 catfextrafields = extrafields;
6109 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
6110 if(len(extradata)>0):
6111 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
6112 extrasizelen = len(extrasizestr);
6113 extrasizelenhex = format(extrasizelen, 'x').lower();
6114 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()];
6115 catoutlen = len(catoutlist) + len(extradata) + 3;
6116 catoutlenhex = format(catoutlen, 'x').lower();
6117 catoutlist.insert(0, catoutlenhex);
6118 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
6119 if(len(extradata)>0):
6120 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
6121 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
6122 catfnumfields = 24 + catfextrafields;
6123 if(checksumtype=="none" or checksumtype==""):
6124 catfileheadercshex = format(0, 'x').lower();
6125 catfilecontentcshex = format(0, 'x').lower();
6126 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6127 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6128 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
6129 elif(checksumtype=="crc16_ccitt"):
6130 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6131 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
6132 elif(checksumtype=="adler32"):
6133 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6134 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
6135 elif(checksumtype=="crc32"):
6136 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6137 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
6138 elif(checksumtype=="crc64_ecma"):
6139 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6140 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
6141 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6142 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6143 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
6144 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6145 checksumoutstr = hashlib.new(checksumtype);
6146 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
6147 catfileheadercshex = checksumoutstr.hexdigest().lower();
6148 else:
6149 catfileheadercshex = format(0, 'x').lower();
6150 catfilecontentcshex = format(0, 'x').lower();
6151 catfhstart = fheadtell;
6152 fheadtell += len(catfileoutstr);
6153 catfhend = fheadtell - 1;
6154 catfcontentstart = fheadtell;
6155 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6156 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
6157 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
6158 catheaderdata = [catheaersize] + catoutlist + extradata + [checksumtype, catfileheadercshex, catfilecontentcshex];
6159 if(checksumtype=="none" or checksumtype==""):
6160 catfileheadercshex = format(0, 'x').lower();
6161 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6162 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6163 elif(checksumtype=="crc16_ccitt"):
6164 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6165 elif(checksumtype=="adler32"):
6166 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6167 elif(checksumtype=="crc32"):
6168 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6169 elif(checksumtype=="crc64_ecma"):
6170 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6171 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6172 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6173 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6174 checksumoutstr = hashlib.new(checksumtype);
6175 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
6176 catfileheadercshex = checksumoutstr.hexdigest().lower();
6177 else:
6178 catfileheadercshex = format(0, 'x').lower();
6179 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6180 catfileoutstrecd = catfileoutstr.encode('UTF-8');
6181 nullstrecd = formatspecs[5].encode('UTF-8');
6182 fheadtell += len(catfileoutstr) + 1;
6183 catfcontentend = fheadtell - 1;
6184 catfileout = catfileoutstrecd + fcontents + nullstrecd;
6185 pyhascontents = False;
6186 if(int(fsize)>0 and not listonly):
6187 pyhascontents = True;
6188 if(int(fsize)>0 and listonly):
6189 fcontents = "";
6190 pyhascontents = False;
6191 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} });
6192 fileidnum = fileidnum + 1;
6193 return catlist;
6195 def InFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6196 checkcompressfile = CheckCompressionSubType(infiles, formatspecs, True);
6197 if(checkcompressfile=="tarfile"):
6198 return TarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6199 elif(checkcompressfile=="zipfile"):
6200 return ZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6201 elif(checkcompressfile=="catfile"):
6202 return ArchiveFileToArray(infile, 0, 0, listonly, False, formatspecs, False);
6203 elif(rarfile_support and checkcompressfile=="rarfile"):
6204 return RarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6205 elif(py7zr_support and checkcompressfile=="7zipfile"):
6206 return SevenZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6207 else:
6208 return False;
6209 return False;
6211 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):
6212 outarray = BytesIO();
6213 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
6214 listcatfiles = ArchiveFileToArray(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6215 return listcatfiles;
6217 def ArchiveFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6218 if(isinstance(infile, dict)):
6219 listcatfiles = infile;
6220 else:
6221 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6222 infile = RemoveWindowsPath(infile);
6223 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6224 if(not listcatfiles):
6225 return False;
6226 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': {}}}};
6227 if(returnfp):
6228 catarray.update({'catfp': listcatfiles['catfp']});
6229 lenlist = len(listcatfiles['ffilelist']);
6230 lcfi = 0;
6231 lcfx = int(listcatfiles['fnumfiles']);
6232 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6233 lcfx = int(lenlist);
6234 else:
6235 lcfx = int(listcatfiles['fnumfiles']);
6236 while(lcfi < lcfx):
6237 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6238 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6239 catarray['filetoid'].update(filetoidarray);
6240 catarray['idtofile'].update(idtofilearray);
6241 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6242 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6243 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6244 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6245 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6246 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6247 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6248 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6249 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6250 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6251 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6252 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6253 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6254 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6255 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6256 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6257 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6258 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6259 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6260 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6261 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6262 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6263 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6264 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6265 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6266 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6267 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6268 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6269 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6270 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6271 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6272 lcfi = lcfi + 1;
6273 return catarray;
6275 create_alias_function("", __file_format_name__, "ToArrayIndex", ArchiveFileToArrayIndex);
6277 def ListDirToArrayIndexAlt(infiles, dirlistfromtxt=False, followlink=False, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6278 listcatfiles = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, extradata, formatspecs, verbose);
6279 if(not listcatfiles):
6280 return False;
6281 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': {}}}};
6282 lenlist = len(listcatfiles['ffilelist']);
6283 lcfi = 0;
6284 lcfx = int(listcatfiles['fnumfiles']);
6285 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6286 lcfx = int(lenlist);
6287 else:
6288 lcfx = int(listcatfiles['fnumfiles']);
6289 while(lcfi < lcfx):
6290 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6291 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6292 catarray['filetoid'].update(filetoidarray);
6293 catarray['idtofile'].update(idtofilearray);
6294 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6295 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6296 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6297 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6298 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6299 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6300 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6301 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6302 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6303 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6304 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6305 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6306 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6307 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6308 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6309 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6310 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6311 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6312 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6313 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6314 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6315 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6316 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6317 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6318 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6319 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6320 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6321 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6322 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6323 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6324 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6325 lcfi = lcfi + 1;
6326 return catarray;
6328 def TarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6329 listcatfiles = TarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6330 if(not listcatfiles):
6331 return False;
6332 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': {}}}};
6333 lenlist = len(listcatfiles['ffilelist']);
6334 lcfi = 0;
6335 lcfx = int(listcatfiles['fnumfiles']);
6336 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6337 lcfx = int(lenlist);
6338 else:
6339 lcfx = int(listcatfiles['fnumfiles']);
6340 while(lcfi < lcfx):
6341 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6342 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6343 catarray['filetoid'].update(filetoidarray);
6344 catarray['idtofile'].update(idtofilearray);
6345 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6346 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6347 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6348 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6349 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6350 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6351 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6352 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6353 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6354 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6355 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6356 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6357 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6358 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6359 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6360 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6361 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6362 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6363 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6364 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6365 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6366 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6367 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6368 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6369 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6370 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6371 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6372 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6373 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6374 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6375 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6376 lcfi = lcfi + 1;
6377 return catarray;
6379 def ZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6380 listcatfiles = ZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6381 if(not listcatfiles):
6382 return False;
6383 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': {}}}};
6384 lenlist = len(listcatfiles['ffilelist']);
6385 lcfi = 0;
6386 lcfx = int(listcatfiles['fnumfiles']);
6387 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6388 lcfx = int(lenlist);
6389 else:
6390 lcfx = int(listcatfiles['fnumfiles']);
6391 while(lcfi < lcfx):
6392 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6393 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6394 catarray['filetoid'].update(filetoidarray);
6395 catarray['idtofile'].update(idtofilearray);
6396 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6397 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6398 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6399 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6400 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6401 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6402 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6403 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6404 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6405 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6406 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6407 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6408 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6409 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6410 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6411 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6412 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6413 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6414 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6415 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6416 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6417 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6418 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6419 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6420 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6421 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6422 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6423 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6424 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6425 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6426 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6427 lcfi = lcfi + 1;
6428 return catarray;
6430 if(not rarfile_support):
6431 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6432 return False;
6434 if(rarfile_support):
6435 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6436 listcatfiles = RarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6437 if(not listcatfiles):
6438 return False;
6439 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': {}}}};
6440 lenlist = len(listcatfiles['ffilelist']);
6441 lcfi = 0;
6442 lcfx = int(listcatfiles['fnumfiles']);
6443 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6444 lcfx = int(lenlist);
6445 else:
6446 lcfx = int(listcatfiles['fnumfiles']);
6447 while(lcfi < lcfx):
6448 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6449 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6450 catarray['filetoid'].update(filetoidarray);
6451 catarray['idtofile'].update(idtofilearray);
6452 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6453 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6454 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6455 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6456 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6457 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6458 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6459 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6460 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6461 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6462 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6463 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6464 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6465 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6466 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6467 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6468 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6469 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6470 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6471 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6472 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6473 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6474 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6475 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6476 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6477 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6478 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6479 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6480 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6481 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6482 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6483 lcfi = lcfi + 1;
6484 return catarray;
6486 if(not py7zr_support):
6487 def SevenZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6488 return False;
6490 if(py7zr_support):
6491 def SevenZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
6492 listcatfiles = SevenZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
6493 if(not listcatfiles):
6494 return False;
6495 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': {}}}};
6496 lenlist = len(listcatfiles['ffilelist']);
6497 lcfi = 0;
6498 lcfx = int(listcatfiles['fnumfiles']);
6499 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6500 lcfx = int(lenlist);
6501 else:
6502 lcfx = int(listcatfiles['fnumfiles']);
6503 while(lcfi < lcfx):
6504 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
6505 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
6506 catarray['filetoid'].update(filetoidarray);
6507 catarray['idtofile'].update(idtofilearray);
6508 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
6509 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
6510 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
6511 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6512 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
6513 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
6514 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6515 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6516 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6517 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6518 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6519 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
6520 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
6521 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
6522 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
6523 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
6524 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6525 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6526 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
6527 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
6528 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
6529 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6530 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6531 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6532 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
6533 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
6534 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
6535 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
6536 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
6537 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
6538 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
6539 lcfi = lcfi + 1;
6540 return catarray;
6542 def ArchiveFileStringToArrayIndex(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6543 catfp = BytesIO(catstr);
6544 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6545 return listcatfiles;
6547 create_alias_function("", __file_format_name__, "StringToArrayIndex", ArchiveFileStringToArrayIndex);
6549 def TarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6550 catfp = BytesIO();
6551 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6552 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6553 return listcatfiles;
6555 def ZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6556 catfp = BytesIO();
6557 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6558 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6559 return listcatfiles;
6561 if(not rarfile_support):
6562 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6563 return False;
6565 if(rarfile_support):
6566 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6567 catfp = BytesIO();
6568 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6569 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6570 return listcatfiles;
6572 if(not py7zr_support):
6573 def SevenZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6574 return False;
6576 if(py7zr_support):
6577 def SevenZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
6578 catfp = BytesIO();
6579 catfp = PackArchiveFileFromSevenZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
6580 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
6581 return listcatfiles;
6583 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):
6584 outarray = BytesIO();
6585 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, verbose, True);
6586 listcatfiles = ArchiveFileToArrayIndex(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
6587 return listcatfiles;
6589 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):
6590 if(isinstance(infile, dict)):
6591 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6592 listcatfiles = prelistcatfiles['list'];
6593 else:
6594 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6595 infile = RemoveWindowsPath(infile);
6596 if(followlink):
6597 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6598 listcatfiles = prelistcatfiles['list'];
6599 else:
6600 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6601 if(outfile!="-" and not hasattr(infile, "read") and not hasattr(outfile, "write")):
6602 outfile = RemoveWindowsPath(outfile);
6603 checksumtype = checksumtype.lower();
6604 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
6605 checksumtype="crc32";
6606 if(checksumtype=="none"):
6607 checksumtype = "";
6608 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
6609 compression = None;
6610 if(compression not in compressionlist and compression is None):
6611 compression = "auto";
6612 if(verbose):
6613 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6614 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
6615 if(os.path.exists(outfile)):
6616 os.unlink(outfile);
6617 if(not listcatfiles):
6618 return False;
6619 if(outfile=="-"):
6620 verbose = False;
6621 catfp = BytesIO();
6622 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
6623 catfp = outfile;
6624 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
6625 catfp = BytesIO();
6626 else:
6627 fbasename = os.path.splitext(outfile)[0];
6628 fextname = os.path.splitext(outfile)[1];
6629 catfp = CompressOpenFile(outfile, compressionlevel);
6630 catver = formatspecs[6];
6631 fileheaderver = str(int(catver.replace(".", "")));
6632 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
6633 catfp.write(fileheader.encode('UTF-8'));
6634 lenlist = len(listcatfiles['ffilelist']);
6635 fnumfiles = int(listcatfiles['fnumfiles']);
6636 if(lenlist>fnumfiles or lenlist<fnumfiles):
6637 fnumfiles = lenlist;
6638 fnumfileshex = format(int(fnumfiles), 'x').lower();
6639 fnumfilesa = AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
6640 if(checksumtype=="none" or checksumtype==""):
6641 catfileheadercshex = format(0, 'x').lower();
6642 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6643 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
6644 elif(checksumtype=="crc16_ccitt"):
6645 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
6646 elif(checksumtype=="adler32"):
6647 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
6648 elif(checksumtype=="crc32"):
6649 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
6650 elif(checksumtype=="crc64_ecma"):
6651 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6652 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6653 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6654 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6655 checksumoutstr = hashlib.new(checksumtype);
6656 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
6657 catfileheadercshex = checksumoutstr.hexdigest().lower();
6658 else:
6659 catfileheadercshex = format(0, 'x').lower();
6660 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
6661 catfp.write(fnumfilesa.encode('UTF-8'));
6662 try:
6663 catfp.flush();
6664 os.fsync(catfp.fileno());
6665 except io.UnsupportedOperation:
6666 pass;
6667 except AttributeError:
6668 pass;
6669 lenlist = len(listcatfiles['ffilelist']);
6670 fnumfiles = int(listcatfiles['fnumfiles']);
6671 lcfi = 0;
6672 lcfx = int(listcatfiles['fnumfiles']);
6673 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6674 lcfx = int(lenlist);
6675 else:
6676 lcfx = int(listcatfiles['fnumfiles']);
6677 curinode = 0;
6678 curfid = 0;
6679 inodelist = [];
6680 inodetofile = {};
6681 filetoinode = {};
6682 reallcfi = 0;
6683 while(lcfi < lcfx):
6684 catfhstart = catfp.tell();
6685 if(re.findall("^[.|/]", listcatfiles['ffilelist'][reallcfi]['fname'])):
6686 fname = listcatfiles['ffilelist'][reallcfi]['fname'];
6687 else:
6688 fname = "./"+listcatfiles['ffilelist'][reallcfi]['fname'];
6689 if(verbose):
6690 VerbosePrintOut(fname);
6691 fheadersize = format(int(listcatfiles['ffilelist'][reallcfi]['fheadersize']), 'x').lower();
6692 fsize = format(int(listcatfiles['ffilelist'][reallcfi]['fsize']), 'x').lower();
6693 flinkname = listcatfiles['ffilelist'][reallcfi]['flinkname'];
6694 fatime = format(int(listcatfiles['ffilelist'][reallcfi]['fatime']), 'x').lower();
6695 fmtime = format(int(listcatfiles['ffilelist'][reallcfi]['fmtime']), 'x').lower();
6696 fctime = format(int(listcatfiles['ffilelist'][reallcfi]['fctime']), 'x').lower();
6697 fbtime = format(int(listcatfiles['ffilelist'][reallcfi]['fbtime']), 'x').lower();
6698 fmode = format(int(listcatfiles['ffilelist'][reallcfi]['fmode']), 'x').lower();
6699 fchmode = format(int(listcatfiles['ffilelist'][reallcfi]['fchmode']), 'x').lower();
6700 fuid = format(int(listcatfiles['ffilelist'][reallcfi]['fuid']), 'x').lower();
6701 funame = listcatfiles['ffilelist'][reallcfi]['funame'];
6702 fgid = format(int(listcatfiles['ffilelist'][reallcfi]['fgid']), 'x').lower();
6703 fgname = listcatfiles['ffilelist'][reallcfi]['fgname'];
6704 finode = listcatfiles['ffilelist'][reallcfi]['finode'];
6705 flinkcount = listcatfiles['ffilelist'][reallcfi]['flinkcount'];
6706 fwinattributes = flinkinfo['ffilelist'][reallcfi]['fwinattributes'];
6707 fdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['fminor']), 'x').lower();
6708 fdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['fmajor']), 'x').lower();
6709 frdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['frminor']), 'x').lower();
6710 frdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['frmajor']), 'x').lower();
6711 if(len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>listcatfiles['ffilelist'][reallcfi]['fextrafields'] and len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>0):
6712 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(listcatfiles['ffilelist'][reallcfi]['fextralist']);
6713 if(len(extradata)>0):
6714 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(extradata);
6715 listcatfiles['ffilelist'][reallcfi]['fextralist'] = extradata;
6716 extrafields = format(int(listcatfiles['ffilelist'][reallcfi]['fextrafields']), 'x').lower();
6717 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
6718 if(len(extradata)>0):
6719 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
6720 extrasizelen = format(len(extrasizestr), 'x').lower();
6721 fcontents = listcatfiles['ffilelist'][reallcfi]['fcontents'];
6722 if(followlink):
6723 if(listcatfiles['ffilelist'][reallcfi]['ftype']==1 or listcatfiles['ffilelist'][reallcfi]['ftype']==2):
6724 getflinkpath = listcatfiles['ffilelist'][reallcfi]['flinkname'];
6725 flinkid = prelistcatfiles['filetoid'][getflinkpath];
6726 flinkinfo = listcatfiles['ffilelist'][flinkid];
6727 fheadersize = format(int(flinkinfo['fheadersize']), 'x').lower();
6728 fsize = format(int(flinkinfo['fsize']), 'x').lower();
6729 flinkname = flinkinfo['flinkname'];
6730 fatime = format(int(flinkinfo['fatime']), 'x').lower();
6731 fmtime = format(int(flinkinfo['fmtime']), 'x').lower();
6732 fctime = format(int(flinkinfo['fctime']), 'x').lower();
6733 fbtime = format(int(flinkinfo['fbtime']), 'x').lower();
6734 fmode = format(int(flinkinfo['fmode']), 'x').lower();
6735 fchmode = format(int(flinkinfo['fchmode']), 'x').lower();
6736 fuid = format(int(flinkinfo['fuid']), 'x').lower();
6737 funame = flinkinfo['funame'];
6738 fgid = format(int(flinkinfo['fgid']), 'x').lower();
6739 fgname = flinkinfo['fgname'];
6740 finode = flinkinfo['finode'];
6741 flinkcount = flinkinfo['flinkcount'];
6742 fwinattributes = flinkinfo['fwinattributes'];
6743 fdev_minor = format(int(flinkinfo['fminor']), 'x').lower();
6744 fdev_major = format(int(flinkinfo['fmajor']), 'x').lower();
6745 frdev_minor = format(int(flinkinfo['frminor']), 'x').lower();
6746 frdev_major = format(int(flinkinfo['frmajor']), 'x').lower();
6747 if(len(flinkinfo['fextralist'])>flinkinfo['fextrafields'] and len(flinkinfo['fextralist'])>0):
6748 flinkinfo['fextrafields'] = len(flinkinfo['fextralist']);
6749 if(len(extradata)>0):
6750 flinkinfo['fextrafields'] = len(extradata);
6751 flinkinfo['fextralist'] = extradata;
6752 extrafields = format(int(flinkinfo['fextrafields']), 'x').lower();
6753 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
6754 if(len(extradata)>0):
6755 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
6756 extrasizelen = format(len(extrasizestr), 'x').lower();
6757 fcontents = flinkinfo['fcontents'];
6758 if(flinkinfo['ftype']!=0 and flinkinfo['ftype']!=7):
6759 try:
6760 fcontents = fcontents.encode('UTF-8');
6761 except AttributeError:
6762 pass;
6763 ftypehex = format(flinkinfo['ftype'], 'x').lower();
6764 else:
6765 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=0 and listcatfiles['ffilelist'][reallcfi]['ftype']!=7):
6766 try:
6767 fcontents = fcontents.encode('UTF-8');
6768 except AttributeError:
6769 pass;
6770 ftypehex = format(listcatfiles['ffilelist'][reallcfi]['ftype'], 'x').lower();
6771 fcurfid = format(curfid, 'x').lower();
6772 if(not followlink and finode!=0):
6773 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=1):
6774 fcurinode = format(int(curinode), 'x').lower();
6775 inodetofile.update({curinode: fname});
6776 filetoinode.update({fname: curinode});
6777 curinode = curinode + 1;
6778 else:
6779 fcurinode = format(int(filetoinode[flinkname]), 'x').lower();
6780 else:
6781 fcurinode = format(int(curinode), 'x').lower();
6782 curinode = curinode + 1;
6783 curfid = curfid + 1;
6784 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];
6785 catoutlen = len(catoutlist) + len(extradata) + 3;
6786 catoutlenhex = format(catoutlen, 'x').lower();
6787 catoutlist.insert(0, catoutlenhex);
6788 catfileoutstr = AppendNullBytes(catoutlist, formatspecs[5]);
6789 if(listcatfiles['ffilelist'][reallcfi]['fextrafields']>0):
6790 extrafieldslist = [];
6791 exi = 0;
6792 exil = listcatfiles['ffilelist'][reallcfi]['fextrafields'];
6793 while(exi<exil):
6794 extrafieldslist.append(listcatfiles['ffilelist'][reallcfi]['fextralist']);
6795 exi = exi + 1;
6796 catfileoutstr += AppendNullBytes([extrafieldslist], formatspecs[5]);
6797 catfileoutstr += AppendNullBytes([checksumtype], formatspecs[5]);
6798 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
6799 catfcontentstart = catfp.tell() + len(catfileoutstr);
6800 if(checksumtype=="none" or checksumtype==""):
6801 catfileheadercshex = format(0, 'x').lower();
6802 catfilecontentcshex = format(0, 'x').lower();
6803 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6804 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
6805 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
6806 elif(checksumtype=="crc16_ccitt"):
6807 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
6808 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
6809 elif(checksumtype=="adler32"):
6810 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6811 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
6812 elif(checksumtype=="crc32"):
6813 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6814 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
6815 elif(checksumtype=="crc64_ecma"):
6816 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6817 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
6818 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6819 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6820 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
6821 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6822 checksumoutstr = hashlib.new(checksumtype);
6823 checksumoutstr.update("".encode('UTF-8'));
6824 catfileheadercshex = checksumoutstr.hexdigest().lower();
6825 checksumoutstr = hashlib.new(checksumtype);
6826 checksumoutstr.update(fcontents);
6827 catfilecontentcshex = checksumoutstr.hexdigest().lower();
6828 else:
6829 catfileheadercshex = format(0, 'x').lower();
6830 catfilecontentcshex = format(0, 'x').lower();
6831 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6832 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
6833 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
6834 if(checksumtype=="none" or checksumtype==""):
6835 catfileheadercshex = format(0, 'x').lower();
6836 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
6837 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6838 elif(checksumtype=="crc16_ccitt"):
6839 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
6840 elif(checksumtype=="adler32"):
6841 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6842 elif(checksumtype=="crc32"):
6843 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
6844 elif(checksumtype=="crc64_ecma"):
6845 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6846 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
6847 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6848 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
6849 checksumoutstr = hashlib.new(checksumtype);
6850 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
6851 catfileheadercshex = checksumoutstr.hexdigest().lower();
6852 else:
6853 catfileheadercshex = format(0, 'x').lower();
6854 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
6855 catfileoutstrecd = catfileoutstr.encode('UTF-8');
6856 nullstrecd = formatspecs[5].encode('UTF-8');
6857 catfileout = catfileoutstrecd + fcontents + nullstrecd;
6858 catfcontentend = (catfp.tell() - 1) + len(catfileout);
6859 catfp.write(catfileout);
6860 try:
6861 catfp.flush();
6862 os.fsync(catfp.fileno());
6863 except io.UnsupportedOperation:
6864 pass;
6865 except AttributeError:
6866 pass;
6867 lcfi = lcfi + 1;
6868 reallcfi = reallcfi + 1;
6869 if(lcfx>0):
6870 catfp.write(AppendNullBytes([0, 0], formatspecs[5]).encode("UTF-8"));
6871 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
6872 catfp = CompressArchiveFile(catfp, compression, formatspecs);
6873 try:
6874 catfp.flush();
6875 os.fsync(catfp.fileno());
6876 except io.UnsupportedOperation:
6877 pass;
6878 except AttributeError:
6879 pass;
6880 if(outfile=="-"):
6881 catfp.seek(0, 0);
6882 if(hasattr(sys.stdout, "buffer")):
6883 shutil.copyfileobj(catfp, sys.stdout.buffer);
6884 else:
6885 shutil.copyfileobj(catfp, sys.stdout);
6886 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", str(outfile))):
6887 catfp = CompressArchiveFile(catfp, compression, formatspecs);
6888 catfp.seek(0, 0);
6889 upload_file_to_internet_file(catfp, outfile);
6890 if(returnfp):
6891 catfp.seek(0, 0);
6892 return catfp;
6893 else:
6894 catfp.close();
6895 return True;
6897 create_alias_function("RePack", __file_format_name__, "", RePackArchiveFile);
6899 def RePackArchiveFileFromString(catstr, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6900 catfp = BytesIO(catstr);
6901 listcatfiles = RePackArchiveFile(catfp, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
6902 return listcatfiles;
6904 create_alias_function("RePack", __file_format_name__, "FromString", RePackArchiveFileFromString);
6906 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):
6907 outarray = BytesIO();
6908 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
6909 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
6910 return listcatfiles;
6912 create_alias_function("Pack", __file_format_name__, "FromListDir", PackArchiveFileFromListDir);
6914 def ArchiveFileArrayBase64Encode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6915 if(verbose):
6916 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6917 if(isinstance(infile, dict)):
6918 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6919 listcatfiles = prelistcatfiles['list'];
6920 else:
6921 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6922 infile = RemoveWindowsPath(infile);
6923 if(followlink):
6924 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6925 listcatfiles = prelistcatfiles['list'];
6926 else:
6927 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6928 if(not listcatfiles):
6929 return False;
6930 lenlist = len(listcatfiles['ffilelist']);
6931 fnumfiles = int(listcatfiles['fnumfiles']);
6932 lcfi = 0;
6933 lcfx = int(listcatfiles['fnumfiles']);
6934 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6935 lcfx = int(lenlist);
6936 else:
6937 lcfx = int(listcatfiles['fnumfiles']);
6938 while(lcfi<lcfx):
6939 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
6940 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64encode(listcatfiles['ffilelist'][lcfi]['fcontents']).decode("UTF-8");
6941 lcfi = lcfi + 1;
6942 return listcatfiles;
6944 create_alias_function("", __file_format_name__, "ArrayBase64Encode", ArchiveFileArrayBase64Encode);
6946 def ArchiveFileArrayBase64Decode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6947 if(verbose):
6948 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6949 if(isinstance(infile, dict)):
6950 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6951 listcatfiles = prelistcatfiles['list'];
6952 else:
6953 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6954 infile = RemoveWindowsPath(infile);
6955 if(followlink):
6956 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6957 listcatfiles = prelistcatfiles['list'];
6958 else:
6959 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6960 if(not listcatfiles):
6961 return False;
6962 lenlist = len(listcatfiles['ffilelist']);
6963 fnumfiles = int(listcatfiles['fnumfiles']);
6964 lcfi = 0;
6965 lcfx = int(listcatfiles['fnumfiles']);
6966 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
6967 lcfx = int(lenlist);
6968 else:
6969 lcfx = int(listcatfiles['fnumfiles']);
6970 while(lcfi<lcfx):
6971 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
6972 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64decode(listcatfiles['ffilelist'][lcfi]['fcontents'].encode("UTF-8"));
6973 lcfi = lcfi + 1;
6974 return listcatfiles;
6976 create_alias_function("", __file_format_name__, "ArrayBase64Decode", ArchiveFileArrayBase64Decode);
6978 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):
6979 if(outdir is not None):
6980 outdir = RemoveWindowsPath(outdir);
6981 if(verbose):
6982 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6983 if(isinstance(infile, dict)):
6984 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6985 listcatfiles = prelistcatfiles['list'];
6986 else:
6987 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6988 infile = RemoveWindowsPath(infile);
6989 if(followlink):
6990 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6991 listcatfiles = prelistcatfiles['list'];
6992 else:
6993 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
6994 if(not listcatfiles):
6995 return False;
6996 lenlist = len(listcatfiles['ffilelist']);
6997 fnumfiles = int(listcatfiles['fnumfiles']);
6998 lcfi = 0;
6999 lcfx = int(listcatfiles['fnumfiles']);
7000 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
7001 lcfx = int(lenlist);
7002 else:
7003 lcfx = int(listcatfiles['fnumfiles']);
7004 while(lcfi<lcfx):
7005 funame = "";
7006 try:
7007 import pwd;
7008 try:
7009 userinfo = pwd.getpwuid(listcatfiles['ffilelist'][lcfi]['fuid']);
7010 funame = userinfo.pw_name;
7011 except KeyError:
7012 funame = "";
7013 except ImportError:
7014 funame = "";
7015 fgname = "";
7016 try:
7017 import grp;
7018 try:
7019 groupinfo = grp.getgrgid(listcatfiles['ffilelist'][lcfi]['fgid']);
7020 fgname = groupinfo.gr_name;
7021 except KeyError:
7022 fgname = "";
7023 except ImportError:
7024 fgname = "";
7025 if(verbose):
7026 VerbosePrintOut(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7027 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
7028 with open(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), "wb") as fpc:
7029 fpc.write(listcatfiles['ffilelist'][lcfi]['fcontents'])
7030 try:
7031 fpc.flush()
7032 os.fsync(fpc.fileno())
7033 except io.UnsupportedOperation:
7034 pass
7035 except AttributeError:
7036 pass
7037 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
7038 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
7039 if(preservepermissions):
7040 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7041 if(preservetime):
7042 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
7043 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
7044 if(followlink):
7045 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
7046 flinkid = prelistcatfiles['filetoid'][getflinkpath];
7047 flinkinfo = listcatfiles['ffilelist'][flinkid];
7048 funame = "";
7049 try:
7050 import pwd;
7051 try:
7052 userinfo = pwd.getpwuid(flinkinfo['fuid']);
7053 funame = userinfo.pw_name;
7054 except KeyError:
7055 funame = "";
7056 except ImportError:
7057 funame = "";
7058 fgname = "";
7059 try:
7060 import grp;
7061 try:
7062 groupinfo = grp.getgrgid(flinkinfo['fgid']);
7063 fgname = groupinfo.gr_name;
7064 except KeyError:
7065 fgname = "";
7066 except ImportError:
7067 fgname = "";
7068 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
7069 with open(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), "wb") as fpc:
7070 fpc.write(flinkinfo['fcontents'])
7071 try:
7072 fpc.flush()
7073 os.fsync(fpc.fileno())
7074 except io.UnsupportedOperation:
7075 pass
7076 except AttributeError:
7077 pass
7078 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7079 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7080 if(preservepermissions):
7081 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7082 if(preservetime):
7083 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7084 if(flinkinfo['ftype']==1):
7085 os.link(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7086 if(flinkinfo['ftype']==2):
7087 os.symlink(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7088 if(flinkinfo['ftype']==5):
7089 if(preservepermissions):
7090 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7091 else:
7092 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7093 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7094 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7095 if(preservepermissions):
7096 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7097 if(preservetime):
7098 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7099 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
7100 os.mkfifo(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7101 else:
7102 os.link(listcatfiles['ffilelist'][lcfi]['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7103 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
7104 if(followlink):
7105 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
7106 flinkid = prelistcatfiles['filetoid'][getflinkpath];
7107 flinkinfo = listcatfiles['ffilelist'][flinkid];
7108 funame = "";
7109 try:
7110 import pwd;
7111 try:
7112 userinfo = pwd.getpwuid(flinkinfo['fuid']);
7113 funame = userinfo.pw_name;
7114 except KeyError:
7115 funame = "";
7116 except ImportError:
7117 funame = "";
7118 fgname = "";
7119 try:
7120 import grp;
7121 try:
7122 groupinfo = grp.getgrgid(flinkinfo['fgid']);
7123 fgname = groupinfo.gr_name;
7124 except KeyError:
7125 fgname = "";
7126 except ImportError:
7127 fgname = "";
7128 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
7129 with open(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), "wb") as fpc:
7130 fpc.write(flinkinfo['fcontents'])
7131 try:
7132 fpc.flush()
7133 os.fsync(fpc.fileno())
7134 except io.UnsupportedOperation:
7135 pass
7136 except AttributeError:
7137 pass
7138 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7139 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7140 if(preservepermissions):
7141 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7142 if(preservetime):
7143 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7144 if(flinkinfo['ftype']==1):
7145 os.link(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7146 if(flinkinfo['ftype']==2):
7147 os.symlink(flinkinfo['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7148 if(flinkinfo['ftype']==5):
7149 if(preservepermissions):
7150 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7151 else:
7152 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7153 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
7154 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fuid'], flinkinfo['fgid']);
7155 if(preservepermissions):
7156 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7157 if(preservetime):
7158 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (flinkinfo['fatime'], flinkinfo['fmtime']));
7159 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
7160 os.mkfifo(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), flinkinfo['fchmode']);
7161 else:
7162 os.symlink(listcatfiles['ffilelist'][lcfi]['flinkname'], PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7163 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
7164 if(preservepermissions):
7165 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7166 else:
7167 os.mkdir(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']));
7168 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
7169 os.chown(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
7170 if(preservepermissions):
7171 os.chmod(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7172 if(preservetime):
7173 os.utime(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
7174 if(listcatfiles['ffilelist'][lcfi]['ftype']==6 and hasattr(os, "mkfifo")):
7175 os.mkfifo(PrependPath(outdir, listcatfiles['ffilelist'][lcfi]['fname']), listcatfiles['ffilelist'][lcfi]['fchmode']);
7176 lcfi = lcfi + 1;
7177 if(returnfp):
7178 return listcatfiles['ffilelist']['catfp'];
7179 else:
7180 return True;
7182 create_alias_function("UnPack", __file_format_name__, "", UnPackArchiveFile);
7184 if(hasattr(shutil, "register_unpack_format")):
7185 def UnPackArchiveFileFunc(archive_name, extract_dir=None, **kwargs):
7186 return UnPackArchiveFile(archive_name, extract_dir, False, 0, 0, False, __file_format_delimiter__, False, False);
7187 create_alias_function("UnPack", __file_format_name__, "Func", UnPackArchiveFileFunc);
7189 def UnPackArchiveFileString(catstr, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7190 catfp = BytesIO(catstr);
7191 listcatfiles = UnPackArchiveFile(catfp, outdir, followlink, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7192 return listcatfiles;
7194 create_alias_function("UnPack", __file_format_name__, "String", UnPackArchiveFileString);
7196 def ArchiveFileListFiles(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7197 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7198 if(isinstance(infile, dict)):
7199 listcatfiles = infile;
7200 else:
7201 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
7202 infile = RemoveWindowsPath(infile);
7203 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, returnfp);
7204 if(not listcatfiles):
7205 return False;
7206 lenlist = len(listcatfiles['ffilelist']);
7207 fnumfiles = int(listcatfiles['fnumfiles']);
7208 lcfi = 0;
7209 lcfx = int(listcatfiles['fnumfiles']);
7210 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
7211 lcfx = int(lenlist);
7212 else:
7213 lcfx = int(listcatfiles['fnumfiles']);
7214 returnval = {};
7215 while(lcfi<lcfx):
7216 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
7217 if(not verbose):
7218 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
7219 if(verbose):
7220 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' } };
7221 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
7222 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
7223 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7224 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
7225 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7226 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
7227 if(len(fuprint)<=0):
7228 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
7229 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
7230 if(len(fgprint)<=0):
7231 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
7232 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));
7233 lcfi = lcfi + 1;
7234 if(returnfp):
7235 return listcatfiles['catfp'];
7236 else:
7237 return True;
7239 create_alias_function("", __file_format_name__, "ListFiles", ArchiveFileListFiles);
7241 def ArchiveFileStringListFiles(catstr, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7242 catfp = BytesIO(catstr);
7243 listcatfiles = ArchiveFileListFiles(catstr, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7244 return listcatfiles;
7246 create_alias_function("", __file_format_name__, "StringListFiles", ArchiveFileListFiles);
7248 def ArchiveFileListFilesAlt(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7249 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7250 if(isinstance(infile, dict)):
7251 listcatfiles = infile;
7252 else:
7253 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
7254 infile = RemoveWindowsPath(infile);
7255 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, returnfp);
7256 if(not listcatfiles):
7257 return False;
7258 lenlist = len(listcatfiles['ffilelist']);
7259 fnumfiles = int(listcatfiles['fnumfiles']);
7260 lcfi = 0;
7261 lcfx = int(listcatfiles['fnumfiles']);
7262 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
7263 lcfx = int(lenlist);
7264 else:
7265 lcfx = int(listcatfiles['fnumfiles']);
7266 returnval = {};
7267 while(lcfi<lcfx):
7268 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
7269 if(not verbose):
7270 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
7271 if(verbose):
7272 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' } };
7273 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
7274 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
7275 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7276 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
7277 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
7278 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
7279 if(len(fuprint)<=0):
7280 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
7281 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
7282 if(len(fgprint)<=0):
7283 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
7284 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));
7285 lcfi = lcfi + 1;
7286 if(returnfp):
7287 return listcatfiles['catfp'];
7288 else:
7289 return True;
7291 create_alias_function("", __file_format_name__, "ListFilesAlt", ArchiveFileListFilesAlt);
7293 def ArchiveFileStringListFilesAlt(catstr, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
7294 catfp = BytesIO(catstr);
7295 listcatfiles = ArchiveFileListFilesAlt(catstr, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7296 return listcatfiles;
7298 create_alias_function("", __file_format_name__, "StringListFilesAlt", ArchiveFileListFilesAlt);
7300 def TarFileListFiles(infile, verbose=False, returnfp=False):
7301 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7302 if(infile=="-"):
7303 infile = BytesIO();
7304 if(hasattr(sys.stdin, "buffer")):
7305 shutil.copyfileobj(sys.stdin.buffer, infile);
7306 else:
7307 shutil.copyfileobj(sys.stdin, infile);
7308 infile.seek(0, 0);
7309 if(not infile):
7310 return False;
7311 infile.seek(0, 0);
7312 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
7313 infile = download_file_from_internet_file(infile);
7314 infile.seek(0, 0);
7315 if(not infile):
7316 return False;
7317 infile.seek(0, 0);
7318 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
7319 return False;
7320 elif(os.path.exists(infile) and os.path.isfile(infile)):
7321 try:
7322 if(not tarfile.is_tarfile(infile)):
7323 return False;
7324 except AttributeError:
7325 if(not is_tarfile(infile)):
7326 return False;
7327 else:
7328 return False;
7329 try:
7330 if(hasattr(infile, "read") or hasattr(infile, "write")):
7331 tarfp = tarfile.open(fileobj=infile, mode="r");
7332 else:
7333 tarfp = tarfile.open(infile, "r");
7334 except FileNotFoundError:
7335 return False;
7336 lcfi = 0
7337 returnval = {};
7338 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
7339 returnval.update({lcfi: member.name});
7340 fpremode = member.mode;
7341 ffullmode = member.mode;
7342 flinkcount = 0;
7343 ftype = 0;
7344 if(member.isreg()):
7345 ffullmode = member.mode + stat.S_IFREG;
7346 ftype = 0;
7347 elif(member.isdev()):
7348 ffullmode = member.mode;
7349 ftype = 7;
7350 elif(member.islnk()):
7351 ffullmode = member.mode + stat.S_IFREG;
7352 ftype = 1;
7353 elif(member.issym()):
7354 ffullmode = member.mode + stat.S_IFLNK;
7355 ftype = 2;
7356 elif(member.ischr()):
7357 ffullmode = member.mode + stat.S_IFCHR;
7358 ftype = 3;
7359 elif(member.isblk()):
7360 ffullmode = member.mode + stat.S_IFBLK;
7361 ftype = 4;
7362 elif(member.isdir()):
7363 ffullmode = member.mode + stat.S_IFDIR;
7364 ftype = 5;
7365 elif(member.isfifo()):
7366 ffullmode = member.mode + stat.S_IFIFO;
7367 ftype = 6;
7368 elif(member.issparse()):
7369 ffullmode = member.mode;
7370 ftype = 12;
7371 if(not verbose):
7372 VerbosePrintOut(member.name);
7373 elif(verbose):
7374 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' } };
7375 printfname = member.name;
7376 if(member.islnk()):
7377 printfname = member.name + " link to " + member.linkname;
7378 elif(member.issym()):
7379 printfname = member.name + " -> " + member.linkname;
7380 fuprint = member.uname;
7381 if(len(fuprint)<=0):
7382 fuprint = member.uid;
7383 fgprint = member.gname;
7384 if(len(fgprint)<=0):
7385 fgprint = member.gid;
7386 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));
7387 lcfi = lcfi + 1;
7388 if(returnfp):
7389 return listcatfiles['catfp'];
7390 else:
7391 return True;
7393 def ZipFileListFiles(infile, verbose=False, returnfp=False):
7394 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7395 if(infile=="-"):
7396 infile = BytesIO();
7397 if(hasattr(sys.stdin, "buffer")):
7398 shutil.copyfileobj(sys.stdin.buffer, infile);
7399 else:
7400 shutil.copyfileobj(sys.stdin, infile);
7401 infile.seek(0, 0);
7402 if(not infile):
7403 return False;
7404 infile.seek(0, 0);
7405 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile))):
7406 infile = download_file_from_internet_file(infile);
7407 infile.seek(0, 0);
7408 if(not infile):
7409 return False;
7410 infile.seek(0, 0);
7411 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
7412 return False;
7413 else:
7414 return False;
7415 if(not zipfile.is_zipfile(infile)):
7416 return False;
7417 try:
7418 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
7419 except FileNotFoundError:
7420 return False;
7421 lcfi = 0;
7422 returnval = {};
7423 ziptest = zipfp.testzip();
7424 if(ziptest):
7425 VerbosePrintOut("Bad file found!");
7426 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
7427 if(zipinfo.create_system==0 or zipinfo.create_system==10):
7428 fwinattributes = int(zipinfo.external_attr);
7429 if(not member.is_dir()):
7430 fmode = int(stat.S_IFREG + 438);
7431 fchmode = int(stat.S_IMODE(fmode));
7432 ftypemod = int(stat.S_IFMT(fmode));
7433 elif(member.is_dir()):
7434 fmode = int(stat.S_IFDIR + 511);
7435 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7436 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7437 elif(zipinfo.create_system==3):
7438 fwinattributes =int(0);
7439 fmode = int(zipinfo.external_attr);
7440 fchmode = int(stat.S_IMODE(fmode));
7441 ftypemod = int(stat.S_IFMT(fmode));
7442 else:
7443 fwinattributes = int(0);
7444 if(not member.is_dir()):
7445 fmode = int(stat.S_IFREG + 438);
7446 fchmode = int(stat.S_IMODE(fmode));
7447 ftypemod = int(stat.S_IFMT(fmode));
7448 elif(member.is_dir()):
7449 fmode = int(stat.S_IFDIR + 511);
7450 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7451 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7452 returnval.update({lcfi: member.filename});
7453 if(not verbose):
7454 VerbosePrintOut(member.filename);
7455 if(verbose):
7456 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' } };
7457 permissionstr = "";
7458 for fmodval in str(oct(fmode))[-3:]:
7459 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
7460 if(not member.is_dir()):
7461 ftype = 0;
7462 permissionstr = "-" + permissionstr;
7463 elif(member.is_dir()):
7464 ftype = 5;
7465 permissionstr = "d" + permissionstr;
7466 printfname = member.filename;
7467 try:
7468 fuid = int(os.getuid());
7469 except AttributeError:
7470 fuid = int(0);
7471 except KeyError:
7472 fuid = int(0);
7473 try:
7474 fgid = int(os.getgid());
7475 except AttributeError:
7476 fgid = int(0);
7477 except KeyError:
7478 fgid = int(0);
7479 try:
7480 import pwd;
7481 try:
7482 userinfo = pwd.getpwuid(os.getuid());
7483 funame = userinfo.pw_name;
7484 except KeyError:
7485 funame = "";
7486 except AttributeError:
7487 funame = "";
7488 except ImportError:
7489 funame = "";
7490 fgname = "";
7491 try:
7492 import grp;
7493 try:
7494 groupinfo = grp.getgrgid(os.getgid());
7495 fgname = groupinfo.gr_name;
7496 except KeyError:
7497 fgname = "";
7498 except AttributeError:
7499 fgname = "";
7500 except ImportError:
7501 fgname = "";
7502 fuprint = funame;
7503 if(len(fuprint)<=0):
7504 fuprint = str(fuid);
7505 fgprint = fgname;
7506 if(len(fgprint)<=0):
7507 fgprint = str(fgid);
7508 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));
7509 lcfi = lcfi + 1;
7510 if(returnfp):
7511 return listcatfiles['catfp'];
7512 else:
7513 return True;
7515 if(not rarfile_support):
7516 def RarFileListFiles(infile, verbose=False, returnfp=False):
7517 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7518 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7519 return False;
7521 if(rarfile_support):
7522 def RarFileListFiles(infile, verbose=False, returnfp=False):
7523 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7524 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7525 return False;
7526 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
7527 return False;
7528 lcfi = 0;
7529 returnval = {};
7530 rarfp = rarfile.RarFile(infile, "r");
7531 rartest = rarfp.testrar();
7532 if(rartest):
7533 VerbosePrintOut("Bad file found!");
7534 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
7535 is_unix = False;
7536 is_windows = False;
7537 if(member.host_os==rarfile.RAR_OS_UNIX):
7538 is_windows = False;
7539 try:
7540 member.external_attr
7541 is_unix = True;
7542 except AttributeError:
7543 is_unix = False;
7544 elif(member.host_os==rarfile.RAR_OS_WIN32):
7545 is_unix = False;
7546 try:
7547 member.external_attr
7548 is_windows = True;
7549 except AttributeError:
7550 is_windows = False;
7551 else:
7552 is_unix = False;
7553 is_windows = False;
7554 if(is_unix and member.external_attr !=0):
7555 fpremode = int(member.external_attr);
7556 elif(member.is_file()):
7557 fpremode = int(stat.S_IFREG + 438);
7558 elif(member.is_symlink()):
7559 fpremode = int(stat.S_IFLNK + 438);
7560 elif(member.is_dir()):
7561 fpremode = int(stat.S_IFDIR + 511);
7562 if(is_windows and member.external_attr !=0):
7563 fwinattributes = int(member.external_attr);
7564 else:
7565 fwinattributes = int(0);
7566 if(is_unix and member.external_attr !=0):
7567 fmode = int(member.external_attr);
7568 fchmode = int(stat.S_IMODE(member.external_attr));
7569 ftypemod = int(stat.S_IFMT(member.external_attr));
7570 elif(member.is_file()):
7571 fmode = int(stat.S_IFREG + 438);
7572 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
7573 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
7574 elif(member.is_symlink()):
7575 fmode = int(stat.S_IFLNK + 438);
7576 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
7577 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
7578 elif(member.is_dir()):
7579 fmode = int(stat.S_IFDIR + 511);
7580 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7581 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7582 returnval.update({lcfi: member.filename});
7583 if(not verbose):
7584 VerbosePrintOut(member.filename);
7585 if(verbose):
7586 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' } };
7587 permissionstr = "";
7588 for fmodval in str(oct(fmode))[-3:]:
7589 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
7590 if(member.is_file()):
7591 ftype = 0;
7592 permissionstr = "-" + permissionstr;
7593 printfname = member.filename;
7594 elif(member.is_symlink()):
7595 ftype = 2;
7596 permissionstr = "l" + permissionstr;
7597 printfname = member.name + " -> " + member.read().decode("UTF-8");
7598 elif(member.is_dir()):
7599 ftype = 5;
7600 permissionstr = "d" + permissionstr;
7601 printfname = member.filename;
7602 try:
7603 fuid = int(os.getuid());
7604 except AttributeError:
7605 fuid = int(0);
7606 except KeyError:
7607 fuid = int(0);
7608 try:
7609 fgid = int(os.getgid());
7610 except AttributeError:
7611 fgid = int(0);
7612 except KeyError:
7613 fgid = int(0);
7614 try:
7615 import pwd;
7616 try:
7617 userinfo = pwd.getpwuid(os.getuid());
7618 funame = userinfo.pw_name;
7619 except KeyError:
7620 funame = "";
7621 except AttributeError:
7622 funame = "";
7623 except ImportError:
7624 funame = "";
7625 fgname = "";
7626 try:
7627 import grp;
7628 try:
7629 groupinfo = grp.getgrgid(os.getgid());
7630 fgname = groupinfo.gr_name;
7631 except KeyError:
7632 fgname = "";
7633 except AttributeError:
7634 fgname = "";
7635 except ImportError:
7636 fgname = "";
7637 fuprint = funame;
7638 if(len(fuprint)<=0):
7639 fuprint = str(fuid);
7640 fgprint = fgname;
7641 if(len(fgprint)<=0):
7642 fgprint = str(fgid);
7643 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + member.mtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
7644 lcfi = lcfi + 1;
7645 if(returnfp):
7646 return listcatfiles['catfp'];
7647 else:
7648 return True;
7650 if(not py7zr_support):
7651 def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
7652 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7653 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7654 return False;
7656 if(py7zr_support):
7657 def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
7658 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7659 if(not os.path.exists(infile) or not os.path.isfile(infile)):
7660 return False;
7661 lcfi = 0;
7662 returnval = {};
7663 szpfp = py7zr.SevenZipFile(infile, mode="r");
7664 file_content = szpfp.readall();
7665 #sztest = szpfp.testzip();
7666 sztestalt = szpfp.test();
7667 if(sztestalt):
7668 VerbosePrintOut("Bad file found!");
7669 for member in sorted(szpfp.list(), key=lambda x: x.filename):
7670 if(re.findall("^[.|/]", member.filename)):
7671 fname = member.filename;
7672 else:
7673 fname = "./"+member.filename;
7674 if(not member.is_directory):
7675 fpremode = int(stat.S_IFREG + 438);
7676 elif(member.is_directory):
7677 fpremode = int(stat.S_IFDIR + 511);
7678 fwinattributes = int(0);
7679 if(member.is_directory):
7680 fmode = int(stat.S_IFDIR + 511);
7681 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
7682 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
7683 else:
7684 fmode = int(stat.S_IFLNK + 438);
7685 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
7686 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
7687 returnval.update({lcfi: member.filename});
7688 if(not verbose):
7689 VerbosePrintOut(member.filename);
7690 if(verbose):
7691 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' } };
7692 permissionstr = "";
7693 for fmodval in str(oct(fmode))[-3:]:
7694 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
7695 fsize = int("0");
7696 if(not member.is_directory):
7697 ftype = 0;
7698 permissionstr = "-" + permissionstr;
7699 printfname = member.filename;
7700 elif(member.is_directory):
7701 ftype = 5;
7702 permissionstr = "d" + permissionstr;
7703 printfname = member.filename;
7704 if(ftype==0):
7705 fsize = len(file_content[member.filename].read());
7706 file_content[member.filename].close();
7707 try:
7708 fuid = int(os.getuid());
7709 except AttributeError:
7710 fuid = int(0);
7711 except KeyError:
7712 fuid = int(0);
7713 try:
7714 fgid = int(os.getgid());
7715 except AttributeError:
7716 fgid = int(0);
7717 except KeyError:
7718 fgid = int(0);
7719 try:
7720 import pwd;
7721 try:
7722 userinfo = pwd.getpwuid(os.getuid());
7723 funame = userinfo.pw_name;
7724 except KeyError:
7725 funame = "";
7726 except AttributeError:
7727 funame = "";
7728 except ImportError:
7729 funame = "";
7730 fgname = "";
7731 try:
7732 import grp;
7733 try:
7734 groupinfo = grp.getgrgid(os.getgid());
7735 fgname = groupinfo.gr_name;
7736 except KeyError:
7737 fgname = "";
7738 except AttributeError:
7739 fgname = "";
7740 except ImportError:
7741 fgname = "";
7742 fuprint = funame;
7743 if(len(fuprint)<=0):
7744 fuprint = str(fuid);
7745 fgprint = fgname;
7746 if(len(fgprint)<=0):
7747 fgprint = str(fgid);
7748 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(fsize).rjust(15) + " " + member.creationtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
7749 lcfi = lcfi + 1;
7750 if(returnfp):
7751 return listcatfiles['catfp'];
7752 else:
7753 return True;
7755 def InFileListFiles(infile, verbose=False, formatspecs=__file_format_list__, returnfp=False):
7756 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
7757 checkcompressfile = CheckCompressionSubType(infile, formatspecs, True);
7758 if(checkcompressfile=="tarfile"):
7759 return TarFileListFiles(infile, verbose, returnfp);
7760 elif(checkcompressfile=="zipfile"):
7761 return ZipFileListFiles(infile, verbose, returnfp);
7762 elif(checkcompressfile=="catfile"):
7763 return ArchiveFileListFilesAlt(infile, 0, 0, False, formatspecs, verbose, returnfp):
7764 elif(rarfile_support and checkcompressfile=="rarfile"):
7765 return RarFileListFiles(infile, verbose, returnfp);
7766 elif(py7zr_support and checkcompressfile=="7zipfile"):
7767 return SevenZipFileListFiles(infile, verbose, returnfp);
7768 else:
7769 return False;
7770 return False;
7772 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):
7773 outarray = BytesIO();
7774 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, False, True);
7775 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7776 return listcatfiles;
7778 def ListDirListFilesAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=False, seekstart=0, seekend=0, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
7779 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, formatspecs, verbose);
7780 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
7781 return listcatfiles;
7783 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):
7784 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, False, checksumtype, extradata, formatspecs, False);
7785 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, followlink, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
7786 return listcatfiles;
7788 create_alias_function("Pack", __file_format_name__, "FromListDirAlt", PackArchiveFileFromListDirAlt);
7790 def PackArchiveFileFromTarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7791 outarray = TarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7792 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7793 return listcatfiles;
7795 create_alias_function("Pack", __file_format_name__, "FromTarFileAlt", PackArchiveFileFromTarFileAlt);
7797 def PackArchiveFileFromZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7798 outarray = ZipFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7799 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7800 return listcatfiles;
7802 create_alias_function("Pack", __file_format_name__, "FromZipFileAlt", PackArchiveFileFromZipFileAlt);
7804 if(not rarfile_support):
7805 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7806 return False;
7808 if(rarfile_support):
7809 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7810 outarray = RarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7811 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7812 return listcatfiles;
7814 create_alias_function("Pack", __file_format_name__, "FromRarFileAlt", PackArchiveFileFromRarFileAlt);
7816 if(not py7zr_support):
7817 def PackArchiveFileFromSevenZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7818 return False;
7820 if(py7zr_support):
7821 def PackArchiveFileFromSevenZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
7822 outarray = SevenZipFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
7823 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
7824 return listcatfiles;
7826 create_alias_function("Pack", __file_format_name__, "FromSevenZipFileAlt", PackArchiveFileFromSevenZipFileAlt);
7828 def download_file_from_ftp_file(url):
7829 urlparts = urlparse(url);
7830 file_name = os.path.basename(urlparts.path);
7831 file_dir = os.path.dirname(urlparts.path);
7832 if(urlparts.username is not None):
7833 ftp_username = urlparts.username;
7834 else:
7835 ftp_username = "anonymous";
7836 if(urlparts.password is not None):
7837 ftp_password = urlparts.password;
7838 elif(urlparts.password is None and urlparts.username=="anonymous"):
7839 ftp_password = "anonymous";
7840 else:
7841 ftp_password = "";
7842 if(urlparts.scheme=="ftp"):
7843 ftp = FTP();
7844 elif(urlparts.scheme=="ftps"):
7845 ftp = FTP_TLS();
7846 else:
7847 return False;
7848 if(urlparts.scheme=="sftp"):
7849 if(__use_pysftp__):
7850 return download_file_from_pysftp_file(url);
7851 else:
7852 return download_file_from_sftp_file(url);
7853 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
7854 return download_file_from_http_file(url);
7855 ftp_port = urlparts.port;
7856 if(urlparts.port is None):
7857 ftp_port = 21;
7858 try:
7859 ftp.connect(urlparts.hostname, ftp_port);
7860 except socket.gaierror:
7861 log.info("Error With URL "+url);
7862 return False;
7863 except socket.timeout:
7864 log.info("Error With URL "+url);
7865 return False;
7866 ftp.login(urlparts.username, urlparts.password);
7867 if(urlparts.scheme=="ftps"):
7868 ftp.prot_p();
7869 ftpfile = BytesIO();
7870 ftp.retrbinary("RETR "+urlparts.path, ftpfile.write);
7871 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7872 ftp.close();
7873 ftpfile.seek(0, 0);
7874 return ftpfile;
7876 def download_file_from_ftp_string(url):
7877 ftpfile = download_file_from_ftp_file(url);
7878 return ftpfile.read();
7880 def upload_file_to_ftp_file(ftpfile, url):
7881 urlparts = urlparse(url);
7882 file_name = os.path.basename(urlparts.path);
7883 file_dir = os.path.dirname(urlparts.path);
7884 if(urlparts.username is not None):
7885 ftp_username = urlparts.username;
7886 else:
7887 ftp_username = "anonymous";
7888 if(urlparts.password is not None):
7889 ftp_password = urlparts.password;
7890 elif(urlparts.password is None and urlparts.username=="anonymous"):
7891 ftp_password = "anonymous";
7892 else:
7893 ftp_password = "";
7894 if(urlparts.scheme=="ftp"):
7895 ftp = FTP();
7896 elif(urlparts.scheme=="ftps"):
7897 ftp = FTP_TLS();
7898 else:
7899 return False;
7900 if(urlparts.scheme=="sftp"):
7901 if(__use_pysftp__):
7902 return upload_file_from_pysftp_file(url);
7903 else:
7904 return upload_file_from_sftp_file(url);
7905 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
7906 return False;
7907 ftp_port = urlparts.port;
7908 if(urlparts.port is None):
7909 ftp_port = 21;
7910 try:
7911 ftp.connect(urlparts.hostname, ftp_port);
7912 except socket.gaierror:
7913 log.info("Error With URL "+url);
7914 return False;
7915 except socket.timeout:
7916 log.info("Error With URL "+url);
7917 return False;
7918 ftp.login(urlparts.username, urlparts.password);
7919 if(urlparts.scheme=="ftps"):
7920 ftp.prot_p();
7921 ftp.storbinary("STOR "+urlparts.path, ftpfile);
7922 ftp.close();
7923 ftpfile.seek(0, 0);
7924 return ftpfile;
7926 def upload_file_to_ftp_string(ftpstring, url):
7927 ftpfileo = BytesIO(ftpstring);
7928 ftpfile = upload_file_to_ftp_file(ftpfileo, url);
7929 ftpfileo.close();
7930 return ftpfile;
7932 def download_file_from_http_file(url, headers=geturls_headers_pycatfile_python_alt):
7933 # Parse the URL to extract username and password if present
7934 urlparts = urlparse(url);
7935 username = urlparts.username;
7936 password = urlparts.password;
7937 # Rebuild the URL without the username and password
7938 netloc = urlparts.hostname;
7939 if(urlparts.scheme=="sftp"):
7940 if(__use_pysftp__):
7941 return download_file_from_pysftp_file(url);
7942 else:
7943 return download_file_from_sftp_file(url);
7944 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
7945 return download_file_from_ftp_file(url);
7946 if urlparts.port:
7947 netloc += ':' + str(urlparts.port);
7948 rebuilt_url = urlunparse((urlparts.scheme, netloc, urlparts.path, urlparts.params, urlparts.query, urlparts.fragment));
7949 # Create a temporary file object
7950 httpfile = BytesIO();
7951 if haverequests:
7952 # Use the requests library if available
7953 if username and password:
7954 response = requests.get(rebuilt_url, headers=headers, auth=(username, password), stream=True);
7955 else:
7956 response = requests.get(rebuilt_url, headers=headers, stream=True);
7957 response.raw.decode_content = True
7958 shutil.copyfileobj(response.raw, httpfile);
7959 else:
7960 # Build a Request object for urllib
7961 request = Request(rebuilt_url, headers=headers);
7962 # Create an opener object for handling URLs
7963 if username and password:
7964 # Create a password manager
7965 password_mgr = HTTPPasswordMgrWithDefaultRealm();
7966 # Add the username and password
7967 password_mgr.add_password(None, rebuilt_url, username, password);
7968 # Create an authentication handler using the password manager
7969 auth_handler = HTTPBasicAuthHandler(password_mgr);
7970 # Build the opener with the authentication handler
7971 opener = build_opener(auth_handler);
7972 else:
7973 opener = build_opener();
7974 with opener.open(request) as response:
7975 shutil.copyfileobj(response, httpfile);
7976 # Reset file pointer to the start
7977 httpfile.seek(0, 0);
7978 # Return the temporary file object
7979 return httpfile;
7981 def download_file_from_http_string(url, headers=geturls_headers_pycatfile_python_alt):
7982 httpfile = download_file_from_http_file(url, headers);
7983 return ftpfile.read();
7985 if(haveparamiko):
7986 def download_file_from_sftp_file(url):
7987 urlparts = urlparse(url);
7988 file_name = os.path.basename(urlparts.path);
7989 file_dir = os.path.dirname(urlparts.path);
7990 sftp_port = urlparts.port;
7991 if(urlparts.port is None):
7992 sftp_port = 22;
7993 else:
7994 sftp_port = urlparts.port;
7995 if(urlparts.username is not None):
7996 sftp_username = urlparts.username;
7997 else:
7998 sftp_username = "anonymous";
7999 if(urlparts.password is not None):
8000 sftp_password = urlparts.password;
8001 elif(urlparts.password is None and urlparts.username=="anonymous"):
8002 sftp_password = "anonymous";
8003 else:
8004 sftp_password = "";
8005 if(urlparts.scheme=="ftp"):
8006 return download_file_from_ftp_file(url);
8007 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
8008 return download_file_from_http_file(url);
8009 if(urlparts.scheme!="sftp"):
8010 return False;
8011 ssh = paramiko.SSHClient();
8012 ssh.load_system_host_keys();
8013 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
8014 try:
8015 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
8016 except paramiko.ssh_exception.SSHException:
8017 return False;
8018 except socket.gaierror:
8019 log.info("Error With URL "+url);
8020 return False;
8021 except socket.timeout:
8022 log.info("Error With URL "+url);
8023 return False;
8024 sftp = ssh.open_sftp();
8025 sftpfile = BytesIO();
8026 sftp.getfo(urlparts.path, sftpfile);
8027 sftp.close();
8028 ssh.close();
8029 sftpfile.seek(0, 0);
8030 return sftpfile;
8031 else:
8032 def download_file_from_sftp_file(url):
8033 return False;
8035 if(haveparamiko):
8036 def download_file_from_sftp_string(url):
8037 sftpfile = download_file_from_sftp_file(url);
8038 return sftpfile.read();
8039 else:
8040 def download_file_from_ftp_string(url):
8041 return False;
8043 if(haveparamiko):
8044 def upload_file_to_sftp_file(sftpfile, url):
8045 urlparts = urlparse(url);
8046 file_name = os.path.basename(urlparts.path);
8047 file_dir = os.path.dirname(urlparts.path);
8048 sftp_port = urlparts.port;
8049 if(urlparts.port is None):
8050 sftp_port = 22;
8051 else:
8052 sftp_port = urlparts.port;
8053 if(urlparts.username is not None):
8054 sftp_username = urlparts.username;
8055 else:
8056 sftp_username = "anonymous";
8057 if(urlparts.password is not None):
8058 sftp_password = urlparts.password;
8059 elif(urlparts.password is None and urlparts.username=="anonymous"):
8060 sftp_password = "anonymous";
8061 else:
8062 sftp_password = "";
8063 if(urlparts.scheme=="ftp"):
8064 return upload_file_from_ftp_file(url);
8065 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
8066 return False;
8067 if(urlparts.scheme!="sftp"):
8068 return False;
8069 ssh = paramiko.SSHClient();
8070 ssh.load_system_host_keys();
8071 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
8072 try:
8073 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
8074 except paramiko.ssh_exception.SSHException:
8075 return False;
8076 except socket.gaierror:
8077 log.info("Error With URL "+url);
8078 return False;
8079 except socket.timeout:
8080 log.info("Error With URL "+url);
8081 return False;
8082 sftp = ssh.open_sftp();
8083 sftp.putfo(sftpfile, urlparts.path);
8084 sftp.close();
8085 ssh.close();
8086 sftpfile.seek(0, 0);
8087 return sftpfile;
8088 else:
8089 def upload_file_to_sftp_file(sftpfile, url):
8090 return False;
8092 if(haveparamiko):
8093 def upload_file_to_sftp_string(sftpstring, url):
8094 sftpfileo = BytesIO(sftpstring);
8095 sftpfile = upload_file_to_sftp_files(ftpfileo, url);
8096 sftpfileo.close();
8097 return sftpfile;
8098 else:
8099 def upload_file_to_sftp_string(url):
8100 return False;
8102 if(havepysftp):
8103 def download_file_from_pysftp_file(url):
8104 urlparts = urlparse(url);
8105 file_name = os.path.basename(urlparts.path);
8106 file_dir = os.path.dirname(urlparts.path);
8107 sftp_port = urlparts.port;
8108 if(urlparts.port is None):
8109 sftp_port = 22;
8110 else:
8111 sftp_port = urlparts.port;
8112 if(urlparts.username is not None):
8113 sftp_username = urlparts.username;
8114 else:
8115 sftp_username = "anonymous";
8116 if(urlparts.password is not None):
8117 sftp_password = urlparts.password;
8118 elif(urlparts.password is None and urlparts.username=="anonymous"):
8119 sftp_password = "anonymous";
8120 else:
8121 sftp_password = "";
8122 if(urlparts.scheme=="ftp"):
8123 return download_file_from_ftp_file(url);
8124 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
8125 return download_file_from_http_file(url);
8126 if(urlparts.scheme!="sftp"):
8127 return False;
8128 try:
8129 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
8130 except paramiko.ssh_exception.SSHException:
8131 return False;
8132 except socket.gaierror:
8133 log.info("Error With URL "+url);
8134 return False;
8135 except socket.timeout:
8136 log.info("Error With URL "+url);
8137 return False;
8138 sftp = ssh.open_sftp();
8139 sftpfile = BytesIO();
8140 sftp.getfo(urlparts.path, sftpfile);
8141 sftp.close();
8142 ssh.close();
8143 sftpfile.seek(0, 0);
8144 return sftpfile;
8145 else:
8146 def download_file_from_pysftp_file(url):
8147 return False;
8149 if(havepysftp):
8150 def download_file_from_pysftp_string(url):
8151 sftpfile = download_file_from_pysftp_file(url);
8152 return sftpfile.read();
8153 else:
8154 def download_file_from_ftp_string(url):
8155 return False;
8157 if(havepysftp):
8158 def upload_file_to_pysftp_file(sftpfile, url):
8159 urlparts = urlparse(url);
8160 file_name = os.path.basename(urlparts.path);
8161 file_dir = os.path.dirname(urlparts.path);
8162 sftp_port = urlparts.port;
8163 if(urlparts.port is None):
8164 sftp_port = 22;
8165 else:
8166 sftp_port = urlparts.port;
8167 if(urlparts.username is not None):
8168 sftp_username = urlparts.username;
8169 else:
8170 sftp_username = "anonymous";
8171 if(urlparts.password is not None):
8172 sftp_password = urlparts.password;
8173 elif(urlparts.password is None and urlparts.username=="anonymous"):
8174 sftp_password = "anonymous";
8175 else:
8176 sftp_password = "";
8177 if(urlparts.scheme=="ftp"):
8178 return upload_file_from_ftp_file(url);
8179 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
8180 return False;
8181 if(urlparts.scheme!="sftp"):
8182 return False;
8183 try:
8184 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
8185 except paramiko.ssh_exception.SSHException:
8186 return False;
8187 except socket.gaierror:
8188 log.info("Error With URL "+url);
8189 return False;
8190 except socket.timeout:
8191 log.info("Error With URL "+url);
8192 return False;
8193 sftp = ssh.open_sftp();
8194 sftp.putfo(sftpfile, urlparts.path);
8195 sftp.close();
8196 ssh.close();
8197 sftpfile.seek(0, 0);
8198 return sftpfile;
8199 else:
8200 def upload_file_to_pysftp_file(sftpfile, url):
8201 return False;
8203 if(havepysftp):
8204 def upload_file_to_pysftp_string(sftpstring, url):
8205 sftpfileo = BytesIO(sftpstring);
8206 sftpfile = upload_file_to_pysftp_files(ftpfileo, url);
8207 sftpfileo.close();
8208 return sftpfile;
8209 else:
8210 def upload_file_to_pysftp_string(url):
8211 return False;
8213 def download_file_from_internet_file(url, headers=geturls_headers_pycatfile_python_alt):
8214 urlparts = urlparse(url);
8215 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8216 return download_file_from_http_file(url, headers);
8217 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8218 return download_file_from_ftp_file(url);
8219 elif(urlparts.scheme=="sftp"):
8220 if(__use_pysftp__ and havepysftp):
8221 return download_file_from_pysftp_file(url);
8222 else:
8223 return download_file_from_sftp_file(url);
8224 else:
8225 return False;
8226 return False;
8228 def download_file_from_internet_uncompress_file(url, headers=geturls_headers_pycatfile_python_alt, formatspecs=__file_format_list__):
8229 fp = download_file_from_internet_file(url);
8230 fp = UncompressArchiveFile(fp, formatspecs);
8231 fp.seek(0, 0);
8232 if(not fp):
8233 return False;
8234 return fp;
8236 def download_file_from_internet_string(url, headers=geturls_headers_pycatfile_python_alt):
8237 urlparts = urlparse(url);
8238 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8239 return download_file_from_http_string(url, headers);
8240 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8241 return download_file_from_ftp_string(url);
8242 elif(urlparts.scheme=="sftp"):
8243 if(__use_pysftp__ and havepysftp):
8244 return download_file_from_pysftp_string(url);
8245 else:
8246 return download_file_from_sftp_string(url);
8247 else:
8248 return False;
8249 return False;
8251 def download_file_from_internet_uncompress_string(url, headers=geturls_headers_pycatfile_python_alt, formatspecs=__file_format_list__):
8252 fp = download_file_from_internet_string(url);
8253 fp = UncompressArchiveFile(fp, formatspecs);
8254 fp.seek(0, 0);
8255 if(not fp):
8256 return False;
8257 return fp;
8259 def upload_file_to_internet_file(ifp, url):
8260 urlparts = urlparse(url);
8261 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8262 return False;
8263 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8264 return upload_file_from_ftp_file(ifp, url);
8265 elif(urlparts.scheme=="sftp"):
8266 if(__use_pysftp__ and havepysftp):
8267 return upload_file_from_pysftp_file(ifp, url);
8268 else:
8269 return download_file_from_sftp_file(ifp, url);
8270 else:
8271 return False;
8272 return False;
8274 def upload_file_to_internet_compress_file(ifp, url, formatspecs=__file_format_list__):
8275 catfp = CompressArchiveFile(catfp, compression, formatspecs);
8276 if(not catfileout):
8277 return False;
8278 catfp.seek(0, 0);
8279 upload_file_to_internet_file(catfp, outfile);
8280 return True;
8282 def upload_file_to_internet_string(ifp, url):
8283 urlparts = urlparse(url);
8284 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
8285 return False;
8286 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
8287 return upload_file_from_ftp_string(ifp, url);
8288 elif(urlparts.scheme=="sftp"):
8289 if(__use_pysftp__ and havepysftp):
8290 return upload_file_from_pysftp_string(ifp, url);
8291 else:
8292 return download_file_from_sftp_string(ifp, url);
8293 else:
8294 return False;
8295 return False;
8297 def upload_file_to_internet_compress_string(ifp, url, formatspecs=__file_format_list__):
8298 catfp = CompressArchiveFile(BytesIO(ifp), compression, formatspecs);
8299 if(not catfileout):
8300 return False;
8301 catfp.seek(0, 0);
8302 upload_file_to_internet_file(catfp, outfile);
8303 return True;
8305 try:
8306 if(hasattr(shutil, "register_archive_format")):
8307 # Register the packing format
8308 shutil.register_archive_format(__file_format_name__, PackArchiveFileFunc, description='Pack concatenated files');
8309 except shutil.RegistryError:
8310 pass;
8312 try:
8313 if(hasattr(shutil, "register_unpack_format")):
8314 # Register the unpacking format
8315 shutil.register_unpack_format(__file_format_name__, archivefile_extensions, UnPackArchiveFileFunc, description='UnPack concatenated files');
8316 except shutil.RegistryError:
8317 pass;