Add files via upload
[PyCatFile.git] / pycatfile.py
blobd2e84fc1e33ef9e41ba557c25d01669371544c1f
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: 3/15/2024 Ver. 0.5.0 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 try:
68 from safetar import is_tarfile;
69 except ImportError:
70 try:
71 from xtarfile import is_tarfile;
72 except ImportError:
73 from tarfile import is_tarfile;
75 try:
76 import safetar as tarfile;
77 except ImportError:
78 try:
79 import xtarfile as tarfile;
80 except ImportError:
81 import tarfile;
83 haveparamiko = False;
84 try:
85 import paramiko;
86 haveparamiko = True;
87 except ImportError:
88 haveparamiko = False;
90 havepysftp = False;
91 try:
92 import pysftp;
93 havepysftp = True;
94 except ImportError:
95 havepysftp = False;
97 haverequests = False;
98 try:
99 import requests;
100 haverequests = True;
101 except ImportError:
102 haverequests = False;
104 if(haverequests):
105 import urllib3;
106 logging.getLogger("urllib3").setLevel(logging.WARNING);
108 try:
109 # Python 3 imports
110 from urllib.request import Request, build_opener, HTTPBasicAuthHandler;
111 from urllib.parse import urlparse;
112 except ImportError:
113 # Python 2 imports
114 from urllib2 import Request, build_opener, HTTPBasicAuthHandler;
115 from urlparse import urlparse;
117 if(sys.version[0]=="2"):
118 try:
119 from io import StringIO, BytesIO;
120 except ImportError:
121 try:
122 from cStringIO import StringIO;
123 from cStringIO import StringIO as BytesIO;
124 except ImportError:
125 from StringIO import StringIO;
126 from StringIO import StringIO as BytesIO;
127 elif(sys.version[0]>="3"):
128 from io import StringIO, BytesIO;
129 else:
130 teststringio = 0;
131 if(teststringio<=0):
132 try:
133 from cStringIO import StringIO as BytesIO;
134 teststringio = 1;
135 except ImportError:
136 teststringio = 0;
137 if(teststringio<=0):
138 try:
139 from StringIO import StringIO as BytesIO;
140 teststringio = 2;
141 except ImportError:
142 teststringio = 0;
143 if(teststringio<=0):
144 try:
145 from io import BytesIO;
146 teststringio = 3;
147 except ImportError:
148 teststringio = 0;
150 __use_pysftp__ = False;
151 if(not havepysftp):
152 __use_pysftp__ = False;
153 __file_format_name__ = "CatFile";
154 __program_name__ = "Py"+__file_format_name__;
155 __file_format_lower__ = __file_format_name__.lower();
156 __file_format_magic__ = __file_format_name__;
157 __file_format_len__ = len(__file_format_magic__);
158 __file_format_hex__ = binascii.hexlify(__file_format_magic__.encode("UTF-8")).decode("UTF-8");
159 __file_format_delimiter__ = "\x00";
160 __file_format_ver__ = "001";
161 __use_new_style__ = True;
162 __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__];
163 __project__ = __program_name__;
164 __project_url__ = "https://github.com/GameMaker2k/PyCatFile";
165 __version_info__ = (0, 6, 0, "RC 1", 1);
166 __version_date_info__ = (2024, 3, 21, "RC 1", 1);
167 __version_date__ = str(__version_date_info__[0]) + "." + str(__version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2);
168 __revision__ = __version_info__[3];
169 __revision_id__ = "$Id$";
170 if(__version_info__[4] is not None):
171 __version_date_plusrc__ = __version_date__ + "-" + str(__version_date_info__[4]);
172 if(__version_info__[4] is None):
173 __version_date_plusrc__ = __version_date__;
174 if(__version_info__[3] is not None):
175 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]) + " " + str(__version_info__[3]);
176 if(__version_info__[3] is None):
177 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]);
179 PyBitness = platform.architecture();
180 if(PyBitness=="32bit" or PyBitness=="32"):
181 PyBitness = "32";
182 elif(PyBitness=="64bit" or PyBitness=="64"):
183 PyBitness = "64";
184 else:
185 PyBitness = "32";
187 geturls_ua_pycatfile_python = "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname=__project__, prover=__version__, prourl=__project_url__);
188 if(platform.python_implementation()!=""):
189 py_implementation = platform.python_implementation();
190 if(platform.python_implementation()==""):
191 py_implementation = "Python";
192 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__);
193 geturls_ua_googlebot_google = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
194 geturls_ua_googlebot_google_old = "Googlebot/2.1 (+http://www.google.com/bot.html)";
195 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)};
196 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)};
197 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"};
198 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"};
200 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
201 outextlist = ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
202 outextlistwd = ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
204 tarfile_mimetype = "application/tar";
205 tarfile_tar_mimetype = tarfile_mimetype;
206 zipfile_mimetype = "application/zip";
207 zipfile_zip_mimetype = zipfile_mimetype;
208 rarfile_mimetype = "application/rar";
209 rarfile_rar_mimetype = rarfile_mimetype;
210 archivefile_mimetype = "application/x-"+__file_format_list__[1]+"";
211 archivefile_cat_mimetype = archivefile_mimetype;
212 archivefile_gzip_mimetype = "application/x-"+__file_format_list__[1]+"+gzip";
213 archivefile_gz_mimetype = archivefile_gzip_mimetype;
214 archivefile_bzip2_mimetype = "application/x-"+__file_format_list__[1]+"+bzip2";
215 archivefile_bz2_mimetype = archivefile_bzip2_mimetype;
216 archivefile_lz4_mimetype = "application/x-"+__file_format_list__[1]+"+lz4";
217 archivefile_lzop_mimetype = "application/x-"+__file_format_list__[1]+"+lzop";
218 archivefile_lzo_mimetype = archivefile_lzop_mimetype;
219 archivefile_zstandard_mimetype = "application/x-"+__file_format_list__[1]+"+zstandard";
220 archivefile_zstd_mimetype = archivefile_zstandard_mimetype;
221 archivefile_lzma_mimetype = "application/x-"+__file_format_list__[1]+"+lzma";
222 archivefile_xz_mimetype = "application/x-"+__file_format_list__[1]+"+xz";
223 archivefile_extensions = ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
225 if __name__ == "__main__":
226 import subprocess;
227 curscrpath = os.path.dirname(sys.argv[0]);
228 if(curscrpath==""):
229 curscrpath = ".";
230 if(os.sep=="\\"):
231 curscrpath = curscrpath.replace(os.sep, "/");
232 curscrpath = curscrpath + "/";
233 scrfile = curscrpath + "catfile.py";
234 if(os.path.exists(scrfile) and os.path.isfile(scrfile)):
235 scrcmd = subprocess.Popen([sys.executable, scrfile] + sys.argv[1:]);
236 scrcmd.wait();
238 def VerbosePrintOut(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
239 if(not dbgenable):
240 return True;
241 log_functions = {
242 "print": print,
243 "log": logging.info,
244 "warning": logging.warning,
245 "error": logging.error,
246 "critical": logging.critical,
247 "exception": logging.exception,
248 "logalt": lambda x: logging.log(dgblevel, x),
249 "debug": logging.debug
251 log_function = log_functions.get(outtype);
252 if(log_function):
253 log_function(dbgtxt);
254 return True;
255 return False;
257 def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
258 VerbosePrintOut(dbgtxt, outtype, dbgenable, dgblevel);
259 return dbgtxt;
261 def RemoveWindowsPath(dpath):
262 if(dpath is None):
263 dpath = "";
264 if(os.sep!="/"):
265 dpath = dpath.replace(os.path.sep, "/");
266 dpath = dpath.rstrip("/");
267 if(dpath=="." or dpath==".."):
268 dpath = dpath + "/";
269 return dpath;
271 def NormalizeRelativePath(inpath):
272 inpath = RemoveWindowsPath(inpath);
273 if(os.path.isabs(inpath)):
274 outpath = inpath;
275 else:
276 if(inpath.startswith("./") or inpath.startswith("../")):
277 outpath = inpath;
278 else:
279 outpath = "./" + inpath;
280 return outpath;
282 def ListDir(dirpath, followlink=False, duplicates=False):
283 if(isinstance(dirpath, (list, tuple, ))):
284 dirpath = list(filter(None, dirpath));
285 elif(isinstance(dirpath, (str, ))):
286 dirpath = list(filter(None, [dirpath]));
287 retlist = [];
288 for mydirfile in dirpath:
289 if(not os.path.exists(mydirfile)):
290 return False;
291 mydirfile = NormalizeRelativePath(mydirfile);
292 if(os.path.exists(mydirfile) and os.path.islink(mydirfile)):
293 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile));
294 if(os.path.exists(mydirfile) and os.path.isdir(mydirfile)):
295 for root, dirs, filenames in os.walk(mydirfile):
296 dpath = root;
297 dpath = RemoveWindowsPath(dpath);
298 if(dpath not in retlist and not duplicates):
299 retlist.append(dpath);
300 if(duplicates):
301 retlist.append(dpath);
302 for file in filenames:
303 fpath = os.path.join(root, file);
304 fpath = RemoveWindowsPath(fpath);
305 if(fpath not in retlist and not duplicates):
306 retlist.append(fpath);
307 if(duplicates):
308 retlist.append(fpath);
309 else:
310 retlist.append(RemoveWindowsPath(mydirfile));
311 return retlist;
313 def ListDirAdvanced(dirpath, followlink=False, duplicates=False):
314 if isinstance(dirpath, (list, tuple)):
315 dirpath = list(filter(None, dirpath));
316 elif isinstance(dirpath, str):
317 dirpath = list(filter(None, [dirpath]));
318 retlist = []
319 for mydirfile in dirpath:
320 if not os.path.exists(mydirfile):
321 return False;
322 mydirfile = NormalizeRelativePath(mydirfile);
323 if os.path.exists(mydirfile) and os.path.islink(mydirfile) and followlink:
324 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile))
325 if os.path.exists(mydirfile) and os.path.isdir(mydirfile):
326 for root, dirs, filenames in os.walk(mydirfile):
327 # Sort dirs and filenames alphabetically in place
328 dirs.sort(key=lambda x: x.lower());
329 filenames.sort(key=lambda x: x.lower());
330 dpath = RemoveWindowsPath(root);
331 if not duplicates and dpath not in retlist:
332 retlist.append(dpath);
333 elif duplicates:
334 retlist.append(dpath);
335 for file in filenames:
336 fpath = os.path.join(root, file);
337 fpath = RemoveWindowsPath(fpath);
338 if not duplicates and fpath not in retlist:
339 retlist.append(fpath);
340 elif duplicates:
341 retlist.append(fpath);
342 else:
343 retlist.append(RemoveWindowsPath(mydirfile));
344 return retlist;
346 def create_alias_function(prefix, base_name, suffix, target_function):
347 # Define a new function that wraps the target function
348 def alias_function(*args, **kwargs):
349 return target_function(*args, **kwargs);
351 # Create the function name by combining the prefix, base name, and the suffix
352 function_name = "{}{}{}".format(prefix, base_name, suffix);
354 # Add the new function to the global namespace
355 globals()[function_name] = alias_function;
357 # initial_value can be 0xFFFF or 0x0000
358 def crc16_ansi(msg, initial_value=0xFFFF):
359 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
360 poly = 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
361 crc = initial_value; # Initial value
362 for b in msg:
363 crc ^= b << 8; # XOR byte into CRC top byte
364 for _ in range(8): # Process each bit
365 if crc & 0x8000: # If the top bit is set
366 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
367 else:
368 crc = crc << 1; # Just shift left
369 crc &= 0xFFFF; # Ensure CRC remains 16-bit
370 return crc;
372 # initial_value can be 0xFFFF or 0x0000
373 def crc16_ibm(msg, initial_value=0xFFFF):
374 return crc16_ansi(msg, initial_value);
376 # initial_value is 0xFFFF
377 def crc16(msg):
378 return crc16_ansi(msg, 0xFFFF);
380 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
381 def crc16_ccitt(msg, initial_value=0xFFFF):
382 # CRC-16-CCITT polynomial
383 poly = 0x1021; # Polynomial for CRC-16-CCITT
384 # Use the specified initial value
385 crc = initial_value;
386 for b in msg:
387 crc ^= b << 8; # XOR byte into CRC top byte
388 for _ in range(8): # Process each bit
389 if crc & 0x8000: # If the top bit is set
390 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
391 else:
392 crc = crc << 1; # Just shift left
393 crc &= 0xFFFF; # Ensure CRC remains 16-bit
394 return crc;
396 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
397 def crc64_ecma(msg, initial_value=0x0000000000000000):
398 # CRC-64-ECMA polynomial and initial value
399 poly = 0x42F0E1EBA9EA3693;
400 crc = initial_value; # Initial value for CRC-64-ECMA
401 for b in msg:
402 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
403 for _ in range(8): # Process each bit
404 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
405 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
406 else:
407 crc <<= 1; # Just shift left if the MSB is 0
408 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
409 return crc;
411 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
412 def crc64_iso(msg, initial_value=0xFFFFFFFFFFFFFFFF):
413 # CRC-64-ISO polynomial and initial value
414 poly = 0x000000000000001B;
415 crc = initial_value; # Common initial value for CRC-64-ISO
416 for b in msg:
417 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
418 for _ in range(8): # Process each bit
419 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
420 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
421 else:
422 crc <<= 1; # Just shift left if the MSB is 0
423 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
424 return crc;
426 def GetDataFromArray(data, path, default=None):
427 element = data;
428 try:
429 for key in path:
430 element = element[key];
431 return element;
432 except (KeyError, TypeError, IndexError):
433 return default;
435 def GetDataFromArrayAlt(structure, path, default=None):
436 element = structure;
437 for key in path:
438 if isinstance(element, dict) and key in element:
439 element = element[key];
440 elif isinstance(element, list) and isinstance(key, int) and -len(element) <= key < len(element):
441 element = element[key];
442 else:
443 return default;
444 return element;
446 def GetHeaderChecksum(inlist=[], checksumtype="crc32", encodedata=True, formatspecs=__file_format_list__):
447 if isinstance(inlist, list):
448 fileheader = AppendNullBytes(inlist, formatspecs[5]);
449 else:
450 fileheader = AppendNullByte(inlist, formatspecs[5]);
451 if(encodedata):
452 fileheader = fileheader.encode('UTF-8');
453 if(checksumtype=="none" or checksumtype==""):
454 catfileheadercshex = format(0, 'x').lower();
455 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
456 catfileheadercshex = format(crc16(fileheader) & 0xffff, '04x').lower();
457 elif(checksumtype=="crc16_ccitt"):
458 catfileheadercshex = format(crc16_ccitt(fileheader) & 0xffff, '04x').lower();
459 elif(checksumtype=="adler32"):
460 catfileheadercshex = format(zlib.adler32(fileheader) & 0xffffffff, '08x').lower();
461 elif(checksumtype=="crc32"):
462 catfileheadercshex = format(crc32(fileheader) & 0xffffffff, '08x').lower();
463 elif(checksumtype=="crc64_ecma"):
464 catfileheadercshex = format(crc64_ecma(fileheader) & 0xffffffffffffffff, '016x').lower();
465 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
466 catfileheadercshex = format(crc64_iso(fileheader) & 0xffffffffffffffff, '016x').lower();
467 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
468 checksumoutstr = hashlib.new(checksumtype);
469 checksumoutstr.update(fileheader);
470 catfileheadercshex = checksumoutstr.hexdigest().lower();
471 else:
472 catfileheadercshex = format(0, 'x').lower();
473 return catfileheadercshex;
475 def GetFileChecksum(infile, checksumtype="crc32", encodedata=True, formatspecs=__file_format_list__):
476 if(encodedata):
477 infile = infile.encode('UTF-8');
478 if(checksumtype=="none" or checksumtype==""):
479 catinfilecshex = format(0, 'x').lower();
480 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
481 catinfilecshex = format(crc16(infile) & 0xffff, '04x').lower();
482 elif(checksumtype=="crc16_ccitt"):
483 catinfilecshex = format(crc16_ccitt(infile) & 0xffff, '04x').lower();
484 elif(checksumtype=="adler32"):
485 catinfilecshex = format(zlib.adler32(infile) & 0xffffffff, '08x').lower();
486 elif(checksumtype=="crc32"):
487 catinfilecshex = format(crc32(infile) & 0xffffffff, '08x').lower();
488 elif(checksumtype=="crc64_ecma"):
489 catinfilecshex = format(crc64_ecma(infile) & 0xffffffffffffffff, '016x').lower();
490 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
491 catinfilecshex = format(crc64_iso(infile) & 0xffffffffffffffff, '016x').lower();
492 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
493 checksumoutstr = hashlib.new(checksumtype);
494 checksumoutstr.update(infile);
495 catinfilecshex = checksumoutstr.hexdigest().lower();
496 else:
497 catinfilecshex = format(0, 'x').lower();
498 return catinfilecshex;
500 def ValidateHeaderChecksum(inlist=[], checksumtype="crc32", inchecksum="0", formatspecs=__file_format_list__):
501 catfileheadercshex = GetHeaderChecksum(inlist, checksumtype, True, formatspecs);
502 inchecksum = inchecksum.lower();
503 catfileheadercshex = catfileheadercshex.lower();
504 if(inchecksum==catfileheadercshex):
505 return True;
506 else:
507 return False;
509 def ValidateFileChecksum(infile, checksumtype="crc32", inchecksum="0", formatspecs=__file_format_list__):
510 catinfilecshex = GetFileChecksum(infile, checksumtype, True, formatspecs);
511 inchecksum = inchecksum.lower();
512 catinfilecshex = catinfilecshex.lower();
513 if(inchecksum==catinfilecshex):
514 return True;
515 else:
516 return False;
518 def ReadTillNullByte(fp, delimiter=__file_format_delimiter__):
519 curbyte = b"";
520 curfullbyte = b"";
521 nullbyte = delimiter.encode("UTF-8");
522 while(True):
523 curbyte = fp.read(1);
524 if(curbyte==nullbyte or not curbyte):
525 break;
526 curfullbyte = curfullbyte + curbyte;
527 return curfullbyte.decode('UTF-8');
529 def ReadUntilNullByte(fp, delimiter=__file_format_delimiter__):
530 return ReadTillNullByte(fp, delimiter);
532 def SeekToEndOfFile(fp):
533 lasttell = 0;
534 while(True):
535 fp.seek(1, 1);
536 if(lasttell==fp.tell()):
537 break;
538 lasttell = fp.tell();
539 return True;
541 def ReadFileHeaderData(fp, rounds=0, delimiter=__file_format_delimiter__):
542 rocount = 0;
543 roend = int(rounds);
544 HeaderOut = [];
545 while(rocount<roend):
546 HeaderOut.append(ReadTillNullByte(fp, delimiter));
547 rocount = rocount + 1;
548 return HeaderOut;
550 def ReadFileHeaderDataBySize(fp, delimiter=__file_format_delimiter__):
551 headerpresize = ReadTillNullByte(fp, delimiter);
552 headersize = int(headerpresize, 16);
553 headercontent = str(fp.read(headersize).decode('UTF-8')).split(delimiter);
554 fp.seek(1, 1);
555 rocount = 0;
556 roend = int(len(headercontent));
557 HeaderOut = [headerpresize];
558 while(rocount<roend):
559 HeaderOut.append(headercontent[rocount]);
560 rocount = rocount + 1;
561 return HeaderOut;
563 def ReadFileHeaderDataBySizeWithContent(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
564 delimiter = formatspecs[5];
565 fheaderstart = fp.tell();
566 HeaderOut = ReadFileHeaderDataBySize(fp, delimiter);
567 if(re.findall("^[.|/]", HeaderOut[2])):
568 fname = HeaderOut[2];
569 else:
570 fname = "./"+HeaderOut[2];
571 fchecksumtype = HeaderOut[-3].lower();
572 fcs = HeaderOut[-2].lower();
573 fccs = HeaderOut[-1].lower();
574 fsize = int(HeaderOut[4], 16);
575 newfcs = GetHeaderChecksum(HeaderOut[:-2], HeaderOut[-3].lower(), True, formatspecs);
576 if(fcs!=newfcs and not skipchecksum):
577 VerbosePrintOut("File Header Checksum Error with file " + fname + " at offset " + str(fheaderstart));
578 return False;
579 fhend = fp.tell() - 1;
580 fcontentstart = fp.tell();
581 fcontents = "".encode('UTF-8');
582 if(fsize>0 and not listonly):
583 fcontents = fp.read(fsize);
584 elif(fsize>0 and listonly):
585 fp.seek(fsize, 1);
586 newfccs = GetFileChecksum(fcontents, HeaderOut[-3].lower(), False, formatspecs);
587 if(fccs!=newfccs and not skipchecksum and not listonly):
588 VerbosePrintOut("File Content Checksum Error with file " + fname + " at offset " + str(fcontentstart));
589 return False;
590 fp.seek(1, 1);
591 fcontentend = fp.tell() - 1;
592 HeaderOut.append(fcontents);
593 return HeaderOut;
595 def ReadFileHeaderDataBySizeWithContentToArray(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
596 delimiter = formatspecs[5];
597 fheaderstart = fp.tell();
598 HeaderOut = ReadFileHeaderDataBySize(fp, delimiter);
599 fheadsize = int(HeaderOut[0], 16);
600 ftype = int(HeaderOut[1], 16);
601 if(re.findall("^[.|/]", HeaderOut[2])):
602 fname = HeaderOut[2];
603 else:
604 fname = "./"+HeaderOut[2];
605 fbasedir = os.path.dirname(fname);
606 flinkname = HeaderOut[3];
607 fsize = int(HeaderOut[4], 16);
608 fatime = int(HeaderOut[5], 16);
609 fmtime = int(HeaderOut[6], 16);
610 fctime = int(HeaderOut[7], 16);
611 fbtime = int(HeaderOut[8], 16);
612 fmode = int(HeaderOut[9], 16);
613 fchmode = stat.S_IMODE(fmode);
614 ftypemod = stat.S_IFMT(fmode);
615 fuid = int(HeaderOut[10], 16);
616 funame = HeaderOut[11];
617 fgid = int(HeaderOut[12], 16);
618 fgname = HeaderOut[13];
619 fid = int(HeaderOut[14], 16);
620 finode = int(HeaderOut[15], 16);
621 flinkcount = int(HeaderOut[16], 16);
622 fdev_minor = int(HeaderOut[17], 16);
623 fdev_major = int(HeaderOut[18], 16);
624 frdev_minor = int(HeaderOut[19], 16);
625 frdev_major = int(HeaderOut[20], 16);
626 fextrasize = int(HeaderOut[21], 16);
627 fextrafields = int(HeaderOut[22], 16);
628 extrafieldslist = [];
629 extrastart = 23;
630 extraend = extrastart + fextrafields;
631 extrafieldslist = [];
632 if(extrastart<extraend):
633 extrafieldslist.append(HeaderOut[extrastart]);
634 extrastart = extrastart + 1;
635 fnumfields = 24 + fextrafields;
636 fchecksumtype = HeaderOut[extrastart].lower();
637 fcs = HeaderOut[extrastart + 1].lower();
638 fccs = HeaderOut[extrastart + 2].lower();
639 newfcs = GetHeaderChecksum(HeaderOut[:-2], HeaderOut[-3].lower(), True, formatspecs);
640 if(fcs!=newfcs and not skipchecksum):
641 VerbosePrintOut("File Header Checksum Error with file " + fname + " at offset " + str(fheaderstart));
642 return False;
643 fhend = fp.tell() - 1;
644 fcontentstart = fp.tell();
645 fcontents = "".encode('UTF-8');
646 pyhascontents = False;
647 if(fsize>0 and not listonly):
648 fcontents = fp.read(fsize);
649 pyhascontents = True;
650 elif(fsize>0 and listonly):
651 fp.seek(fsize, 1);
652 pyhascontents = False;
653 newfccs = GetFileChecksum(fcontents, HeaderOut[-3].lower(), False, formatspecs);
654 if(fccs!=newfccs and not skipchecksum and not listonly):
655 VerbosePrintOut("File Content Checksum Error with file " + fname + " at offset " + str(fcontentstart));
656 return False;
657 fp.seek(1, 1);
658 fcontentend = fp.tell() - 1;
659 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, '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, 'fextrafields': fextrafields, 'fextrafieldsize': fextrasize, 'fextralist': extrafieldslist, 'fheaderchecksum': fcs, 'fcontentchecksum': fccs, 'fhascontents': pyhascontents, 'fcontentstart': fcontentstart, 'fcontentend': fcontentend, 'fcontents': fcontents}
660 return catlist;
662 def ReadFileDataBySizeWithContent(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
663 delimiter = formatspecs[5];
664 catheader = ReadFileHeaderData(fp, 4, delimiter);
665 headercheck = ValidateHeaderChecksum(catheader[:-1], catheader[2], catheader[3], formatspecs);
666 if(not headercheck and not skipchecksum):
667 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
668 return False;
669 fnumfiles = int(catheader[1], 16);
670 countnum = 0;
671 flist = [];
672 while(countnum < fnumfiles):
673 flist.append(ReadFileHeaderDataBySizeWithContent(fp, listonly, skipchecksum, formatspecs));
674 countnum = countnum + 1;
675 return flist;
677 def ReadFileDataBySizeWithContentToArray(fp, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
678 delimiter = formatspecs[5];
679 catheader = ReadFileHeaderData(fp, 4, delimiter);
680 headercheck = ValidateHeaderChecksum(catheader[:-1], catheader[2], catheader[3], formatspecs);
681 if(not headercheck and not skipchecksum):
682 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
683 return False;
684 catstring = catheader[0];
685 catversion = re.findall(r"([\d]+)$", catstring);
686 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
687 fprenumfiles = catheader[1];
688 fnumfiles = int(fprenumfiles, 16);
689 fprechecksumtype = catheader[2];
690 fprechecksum = catheader[3];
691 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
692 countnum = 0;
693 while(countnum < fnumfiles):
694 catlist['ffilelist'].update({countnum: {'fid': countnum, 'fidalt': countnum}});
695 catlist['ffilelist'][countnum].update(ReadFileHeaderDataBySizeWithContentToArray(fp, listonly, skipchecksum, formatspecs));
696 countnum = countnum + 1;
697 return catlist;
699 def ReadFileDataBySizeWithContentToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
700 if(isinstance(infile, dict)):
701 listcatfiles = infile;
702 else:
703 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
704 return False;
705 listcatfiles = ReadFileDataBySizeWithContentToArray(infile, listonly, skipchecksum, formatspecs);
706 if(not listcatfiles):
707 return False;
708 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': {}}}};
709 lenlist = len(listcatfiles['ffilelist']);
710 if(seekstart>0):
711 lcfi = seekstart;
712 else:
713 lcfi = 0;
714 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
715 lcfx = seekend;
716 else:
717 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
718 lcfx = listcatfiles['fnumfiles'];
719 else:
720 lcfx = int(listcatfiles['fnumfiles']);
721 while(lcfi < lcfx):
722 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
723 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
724 catarray['filetoid'].update(filetoidarray);
725 catarray['idtofile'].update(idtofilearray);
726 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
727 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
728 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
729 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
730 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
731 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
732 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
733 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
734 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
735 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
736 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
737 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
738 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
739 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
740 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
741 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
742 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
743 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
744 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
745 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
746 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
747 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
748 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
749 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
750 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
751 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
752 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
753 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
754 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
755 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
756 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
757 lcfi = lcfi + 1;
758 return catarray;
760 def ReadInFileBySizeWithContent(infile, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
761 delimiter = formatspecs[5];
762 if(hasattr(infile, "read") or hasattr(infile, "write")):
763 fp = infile;
764 fp.seek(0, 0);
765 fp = UncompressArchiveFile(fp, formatspecs);
766 checkcompressfile = CheckCompressionSubType(fp, formatspecs);
767 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
768 return False;
769 if(not fp):
770 return False;
771 fp.seek(0, 0);
772 elif(infile=="-"):
773 fp = BytesIO();
774 if(hasattr(sys.stdin, "buffer")):
775 shutil.copyfileobj(sys.stdin.buffer, fp);
776 else:
777 shutil.copyfileobj(sys.stdin, fp);
778 fp.seek(0, 0);
779 fp = UncompressArchiveFile(fp, formatspecs);
780 if(not fp):
781 return False;
782 fp.seek(0, 0);
783 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
784 fp = download_file_from_internet_file(infile);
785 fp = UncompressArchiveFile(fp, formatspecs);
786 fp.seek(0, 0);
787 if(not fp):
788 return False;
789 fp.seek(0, 0);
790 else:
791 infile = RemoveWindowsPath(infile);
792 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
793 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
794 return False;
795 compresscheck = CheckCompressionType(infile, formatspecs, True);
796 if(not compresscheck):
797 fextname = os.path.splitext(infile)[1];
798 if(fextname==".gz"):
799 compresscheck = "gzip";
800 elif(fextname==".bz2"):
801 compresscheck = "bzip2";
802 elif(fextname==".zst"):
803 compresscheck = "zstd";
804 elif(fextname==".lz4" or fextname==".clz4"):
805 compresscheck = "lz4";
806 elif(fextname==".lzo" or fextname==".lzop"):
807 compresscheck = "lzo";
808 elif(fextname==".lzma" or fextname==".xz"):
809 compresscheck = "lzma";
810 else:
811 return False;
812 if(not compresscheck):
813 return False;
814 fp = UncompressFile(infile, formatspecs, "rb");
815 return ReadFileDataBySizeWithContent(fp, listonly, skipchecksum, formatspecs);
817 def ReadInFileBySizeWithContentToArray(infile, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
818 delimiter = formatspecs[5];
819 if(hasattr(infile, "read") or hasattr(infile, "write")):
820 fp = infile;
821 fp.seek(0, 0);
822 fp = UncompressArchiveFile(fp, formatspecs);
823 checkcompressfile = CheckCompressionSubType(fp, formatspecs);
824 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
825 return False;
826 if(not fp):
827 return False;
828 fp.seek(0, 0);
829 elif(infile=="-"):
830 fp = BytesIO();
831 if(hasattr(sys.stdin, "buffer")):
832 shutil.copyfileobj(sys.stdin.buffer, fp);
833 else:
834 shutil.copyfileobj(sys.stdin, fp);
835 fp.seek(0, 0);
836 fp = UncompressArchiveFile(fp, formatspecs);
837 if(not fp):
838 return False;
839 fp.seek(0, 0);
840 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
841 fp = download_file_from_internet_file(infile);
842 fp = UncompressArchiveFile(fp, formatspecs);
843 fp.seek(0, 0);
844 if(not fp):
845 return False;
846 fp.seek(0, 0);
847 else:
848 infile = RemoveWindowsPath(infile);
849 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
850 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
851 return False;
852 compresscheck = CheckCompressionType(infile, formatspecs, True);
853 if(not compresscheck):
854 fextname = os.path.splitext(infile)[1];
855 if(fextname==".gz"):
856 compresscheck = "gzip";
857 elif(fextname==".bz2"):
858 compresscheck = "bzip2";
859 elif(fextname==".zst"):
860 compresscheck = "zstd";
861 elif(fextname==".lz4" or fextname==".clz4"):
862 compresscheck = "lz4";
863 elif(fextname==".lzo" or fextname==".lzop"):
864 compresscheck = "lzo";
865 elif(fextname==".lzma" or fextname==".xz"):
866 compresscheck = "lzma";
867 else:
868 return False;
869 if(not compresscheck):
870 return False;
871 fp = UncompressFile(infile, formatspecs, "rb");
872 return ReadFileDataBySizeWithContentToArray(fp, listonly, skipchecksum, formatspecs);
874 def ReadInFileBySizeWithContentToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__):
875 if(isinstance(infile, dict)):
876 listcatfiles = infile;
877 else:
878 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
879 infile = RemoveWindowsPath(infile);
880 listcatfiles = ReadInFileBySizeWithContentToArray(infile, listonly, skipchecksum, formatspecs);
881 if(not listcatfiles):
882 return False;
883 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': {}}}};
884 lenlist = len(listcatfiles['ffilelist']);
885 if(seekstart>0):
886 lcfi = seekstart;
887 else:
888 lcfi = 0;
889 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
890 lcfx = seekend;
891 else:
892 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
893 lcfx = listcatfiles['fnumfiles'];
894 else:
895 lcfx = int(listcatfiles['fnumfiles']);
896 while(lcfi < lcfx):
897 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
898 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
899 catarray['filetoid'].update(filetoidarray);
900 catarray['idtofile'].update(idtofilearray);
901 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
902 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
903 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
904 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
905 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
906 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
907 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
908 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
909 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
910 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
911 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
912 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
913 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
914 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
915 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
916 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
917 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
918 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
919 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
920 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
921 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
922 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
923 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
924 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
925 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
926 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
927 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
928 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
929 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
930 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
931 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
932 lcfi = lcfi + 1;
933 return catarray;
935 def AppendNullByte(indata, delimiter=__file_format_delimiter__):
936 outdata = str(indata) + delimiter;
937 return outdata;
939 def AppendNullBytes(indata=[], delimiter=__file_format_delimiter__):
940 outdata = "";
941 inum = 0;
942 il = len(indata);
943 while(inum < il):
944 outdata = outdata + AppendNullByte(indata[inum], delimiter);
945 inum = inum + 1;
946 return outdata;
948 def PrintPermissionString(fchmode, ftype):
949 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' } };
950 permissionstr = "";
951 for fmodval in str(oct(fchmode))[-3:]:
952 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
953 if(ftype==0 or ftype==7):
954 permissionstr = "-" + permissionstr;
955 if(ftype==1):
956 permissionstr = "h" + permissionstr;
957 if(ftype==2):
958 permissionstr = "l" + permissionstr;
959 if(ftype==3):
960 permissionstr = "c" + permissionstr;
961 if(ftype==4):
962 permissionstr = "b" + permissionstr;
963 if(ftype==5):
964 permissionstr = "d" + permissionstr;
965 if(ftype==6):
966 permissionstr = "f" + permissionstr;
967 if(ftype==8):
968 permissionstr = "D" + permissionstr;
969 if(ftype==9):
970 permissionstr = "p" + permissionstr;
971 if(ftype==10):
972 permissionstr = "w" + permissionstr;
973 try:
974 permissionoutstr = stat.filemode(fchmode);
975 except AttributeError:
976 permissionoutstr = permissionstr;
977 except KeyError:
978 permissionoutstr = permissionstr;
979 return permissionoutstr;
981 def PrintPermissionStringAlt(fchmode, ftype):
982 permissions = {
983 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
984 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
986 # Translate file mode into permission string
987 permissionstr = ''.join([permissions[i] for i in str(oct(fchmode))[-3:]]);
988 # Append file type indicator
989 type_indicators = {
990 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
991 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
993 file_type = type_indicators.get(ftype, '-');
994 permissionstr = file_type + permissionstr;
995 try:
996 permissionoutstr = stat.filemode(fchmode);
997 except AttributeError:
998 permissionoutstr = permissionstr;
999 return permissionoutstr;
1001 def CompressionSupport():
1002 compression_list = [];
1003 try:
1004 import gzip;
1005 compression_list.append("gz");
1006 compression_list.append("gzip");
1007 except ImportError:
1008 '''return False;'''
1009 try:
1010 import bz2;
1011 compression_list.append("bz2");
1012 compression_list.append("bzip2");
1013 except ImportError:
1014 '''return False;'''
1015 try:
1016 import lz4;
1017 compression_list.append("lz4");
1018 except ImportError:
1019 '''return False;'''
1020 try:
1021 import lzo;
1022 compression_list.append("lzo");
1023 compression_list.append("lzop");
1024 except ImportError:
1025 '''return False;'''
1026 try:
1027 import zstandard;
1028 compression_list.append("zstd");
1029 compression_list.append("zstandard");
1030 except ImportError:
1031 '''return False;'''
1032 try:
1033 import lzma;
1034 compression_list.append("lzma");
1035 compression_list.append("xz");
1036 except ImportError:
1037 '''return False;'''
1038 return compression_list;
1040 def CheckCompressionType(infile, formatspecs=__file_format_list__, closefp=True):
1041 if(hasattr(infile, "read") or hasattr(infile, "write")):
1042 catfp = infile;
1043 else:
1044 try:
1045 catfp = open(infile, "rb");
1046 except FileNotFoundError:
1047 return False;
1048 catfp.seek(0, 0);
1049 prefp = catfp.read(2);
1050 filetype = False;
1051 if(prefp==binascii.unhexlify("1f8b")):
1052 filetype = "gzip";
1053 catfp.seek(0, 0);
1054 prefp = catfp.read(3);
1055 if(prefp==binascii.unhexlify("425a68")):
1056 filetype = "bzip2";
1057 if(prefp==binascii.unhexlify("5d0000")):
1058 filetype = "lzma";
1059 catfp.seek(0, 0);
1060 prefp = catfp.read(4);
1061 if(prefp==binascii.unhexlify("28b52ffd")):
1062 filetype = "zstd";
1063 if(prefp==binascii.unhexlify("04224d18")):
1064 filetype = "lz4";
1065 if(prefp==binascii.unhexlify("504B0304")):
1066 filetype = "zipfile";
1067 catfp.seek(0, 0);
1068 prefp = catfp.read(5);
1069 if(prefp==binascii.unhexlify("7573746172")):
1070 filetype = "tarfile";
1071 catfp.seek(0, 0);
1072 prefp = catfp.read(6);
1073 if(prefp==binascii.unhexlify("fd377a585a00")):
1074 filetype = "lzma";
1075 catfp.seek(0, 0);
1076 prefp = catfp.read(7);
1077 if(prefp==binascii.unhexlify("526172211a0700")):
1078 filetype = "rarfile";
1079 if(prefp==binascii.unhexlify("43617446696c65")):
1080 filetype = "catfile";
1081 catfp.seek(0, 0);
1082 prefp = catfp.read(8);
1083 if(prefp==binascii.unhexlify("526172211a070100")):
1084 filetype = "rarfile";
1085 catfp.seek(0, 0);
1086 prefp = catfp.read(formatspecs[3]);
1087 if(prefp==binascii.unhexlify(formatspecs[4])):
1088 filetype = formatspecs[2];
1089 catfp.seek(0, 0);
1090 prefp = catfp.read(9);
1091 if(prefp==binascii.unhexlify("894c5a4f000d0a1a0a")):
1092 filetype = "lzo";
1093 catfp.seek(0, 0);
1094 prefp = catfp.read(10);
1095 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
1096 filetype = "tarfile";
1097 catfp.seek(0, 0);
1098 if(closefp):
1099 catfp.close();
1100 return filetype;
1102 def CheckCompressionTypeFromString(instring, formatspecs=__file_format_list__, closefp=True):
1103 try:
1104 instringsfile = BytesIO(instring);
1105 except TypeError:
1106 instringsfile = BytesIO(instring.encode("UTF-8"));
1107 return CheckCompressionType(instringsfile, formatspecs, closefp);
1109 def GetCompressionMimeType(infile, formatspecs=__file_format_list__):
1110 compresscheck = CheckCompressionType(fp, formatspecs, False);
1111 if(compresscheck=="gzip" or compresscheck=="gz"):
1112 return archivefile_gzip_mimetype;
1113 if(compresscheck=="bzip2" or compresscheck=="bz2"):
1114 return archivefile_bzip2_mimetype;
1115 if(compresscheck=="zstd" or compresscheck=="zstandard"):
1116 return archivefile_zstandard_mimetype;
1117 if(compresscheck=="lz4"):
1118 return archivefile_lz4_mimetype;
1119 if(compresscheck=="lzo" or compresscheck=="lzop"):
1120 return archivefile_lzop_mimetype;
1121 if(compresscheck=="lzma"):
1122 return archivefile_lzma_mimetype;
1123 if(compresscheck=="xz"):
1124 return archivefile_xz_mimetype;
1125 if(compresscheck=="catfile" or compresscheck=="cat" or compresscheck==formatspecs[2]):
1126 return archivefile_cat_mimetype;
1127 if(not compresscheck):
1128 return False;
1129 return False;
1131 def UncompressArchiveFile(fp, formatspecs=__file_format_list__):
1132 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
1133 return False;
1134 compresscheck = CheckCompressionType(fp, formatspecs, False);
1135 if(compresscheck=="gzip"):
1136 try:
1137 import gzip;
1138 except ImportError:
1139 return False;
1140 catfp = gzip.GzipFile(fileobj=fp, mode="rb");
1141 if(compresscheck=="bzip2"):
1142 try:
1143 import bz2;
1144 except ImportError:
1145 return False;
1146 catfp = BytesIO();
1147 catfp.write(bz2.decompress(fp.read()));
1148 if(compresscheck=="zstd"):
1149 try:
1150 import zstandard;
1151 except ImportError:
1152 return False;
1153 catfp = BytesIO();
1154 catfp.write(zstandard.decompress(fp.read()));
1155 if(compresscheck=="lz4"):
1156 try:
1157 import lz4.frame;
1158 except ImportError:
1159 return False;
1160 catfp = BytesIO();
1161 catfp.write(lz4.frame.decompress(fp.read()));
1162 if(compresscheck=="lzo" or compresscheck=="lzop"):
1163 try:
1164 import lzo;
1165 except ImportError:
1166 return False;
1167 catfp = BytesIO();
1168 catfp.write(lzo.decompress(fp.read()));
1169 if(compresscheck=="lzma" or compresscheck=="xz"):
1170 try:
1171 import lzma;
1172 except ImportError:
1173 return False;
1174 catfp = BytesIO();
1175 catfp.write(lzma.decompress(fp.read()));
1176 if(compresscheck=="catfile" or compresscheck==formatspecs[2]):
1177 catfp = fp;
1178 if(not compresscheck):
1179 try:
1180 import lzma;
1181 except ImportError:
1182 return False;
1183 catfp = BytesIO();
1184 with fp as fpcontent:
1185 try:
1186 catfp.write(lzma.decompress(fp.read()));
1187 except lzma.LZMAError:
1188 return False;
1189 return catfp;
1191 create_alias_function("Uncompress", __file_format_name__, "", UncompressArchiveFile);
1193 def UncompressFile(infile, formatspecs=__file_format_list__, mode="rb"):
1194 compresscheck = CheckCompressionType(infile, formatspecs, False);
1195 if(sys.version_info[0]==2 and compresscheck):
1196 if(mode=="rt"):
1197 mode = "r";
1198 if(mode=="wt"):
1199 mode = "w";
1200 try:
1201 if(compresscheck=="gzip"):
1202 try:
1203 import gzip;
1204 except ImportError:
1205 return False;
1206 try:
1207 filefp = gzip.open(infile, mode, encoding="UTF-8");
1208 except (ValueError, TypeError) as e:
1209 filefp = gzip.open(infile, mode);
1210 if(compresscheck=="bzip2"):
1211 try:
1212 import bz2;
1213 except ImportError:
1214 return False;
1215 try:
1216 filefp = bz2.open(infile, mode, encoding="UTF-8");
1217 except (ValueError, TypeError) as e:
1218 filefp = bz2.open(infile, mode);
1219 if(compresscheck=="zstd"):
1220 try:
1221 import zstandard;
1222 except ImportError:
1223 return False;
1224 try:
1225 filefp = zstandard.open(infile, mode, encoding="UTF-8");
1226 except (ValueError, TypeError) as e:
1227 filefp = zstandard.open(infile, mode);
1228 if(compresscheck=="lz4"):
1229 try:
1230 import lz4.frame;
1231 except ImportError:
1232 return False;
1233 try:
1234 filefp = lz4.frame.open(infile, mode, encoding="UTF-8");
1235 except (ValueError, TypeError) as e:
1236 filefp = lz4.frame.open(infile, mode);
1237 if(compresscheck=="lzo"):
1238 try:
1239 import lzo;
1240 except ImportError:
1241 return False;
1242 try:
1243 filefp = lzo.open(infile, mode, encoding="UTF-8");
1244 except (ValueError, TypeError) as e:
1245 filefp = lzo.open(infile, mode);
1246 if(compresscheck=="lzma"):
1247 try:
1248 import lzma;
1249 except ImportError:
1250 return False;
1251 try:
1252 filefp = lzma.open(infile, mode, encoding="UTF-8");
1253 except (ValueError, TypeError) as e:
1254 filefp = lzma.open(infile, mode);
1255 if(compresscheck=="catfile" or compresscheck==formatspecs[2]):
1256 try:
1257 filefp = open(infile, mode, encoding="UTF-8");
1258 except (ValueError, TypeError) as e:
1259 filefp = open(infile, mode);
1260 if(not compresscheck):
1261 try:
1262 filefp = open(infile, mode, encoding="UTF-8");
1263 except (ValueError, TypeError) as e:
1264 filefp = open(infile, mode);
1265 except FileNotFoundError:
1266 return False;
1267 return filefp;
1269 def UncompressString(infile):
1270 compresscheck = CheckCompressionTypeFromString(infile, formatspecs, False);
1271 if(compresscheck=="gzip"):
1272 try:
1273 import gzip;
1274 except ImportError:
1275 return False;
1276 fileuz = gzip.decompress(infile);
1277 if(compresscheck=="bzip2"):
1278 try:
1279 import bz2;
1280 except ImportError:
1281 return False;
1282 fileuz = bz2.decompress(infile);
1283 if(compresscheck=="zstd"):
1284 try:
1285 import zstandard;
1286 except ImportError:
1287 return False;
1288 fileuz = zstandard.decompress(infile);
1289 if(compresscheck=="lz4"):
1290 try:
1291 import lz4.frame;
1292 except ImportError:
1293 return False;
1294 fileuz = lz4.frame.decompress(infile);
1295 if(compresscheck=="lzo"):
1296 try:
1297 import lzo;
1298 except ImportError:
1299 return False;
1300 fileuz = lzo.decompress(infile);
1301 if(compresscheck=="lzma"):
1302 try:
1303 import lzma;
1304 except ImportError:
1305 return False;
1306 fileuz = lzma.decompress(infile);
1307 if(not compresscheck):
1308 fileuz = infile;
1309 if(hasattr(fileuz, 'decode')):
1310 fileuz = fileuz.decode("UTF-8");
1311 return fileuz;
1313 def UncompressStringAlt(infile):
1314 filefp = StringIO();
1315 outstring = UncompressString(infile);
1316 filefp.write(outstring);
1317 filefp.seek(0);
1318 return filefp;
1320 def CheckCompressionSubType(infile, formatspecs=__file_format_list__):
1321 compresscheck = CheckCompressionType(infile, formatspecs, False);
1322 if(not compresscheck):
1323 fextname = os.path.splitext(infile)[1];
1324 if(fextname==".gz"):
1325 compresscheck = "gzip";
1326 elif(fextname==".bz2"):
1327 compresscheck = "bzip2";
1328 elif(fextname==".zst"):
1329 compresscheck = "zstd";
1330 elif(fextname==".lz4"):
1331 compresscheck = "lz4";
1332 elif(fextname==".lzo" or fextname==".lzop"):
1333 compresscheck = "lzo";
1334 elif(fextname==".lzma" or fextname==".xz"):
1335 compresscheck = "lzma";
1336 else:
1337 return False;
1338 if(not compresscheck):
1339 return False;
1340 if(compresscheck=="catfile"):
1341 return "catfile";
1342 if(compresscheck==formatspecs[2]):
1343 return formatspecs[2];
1344 if(compresscheck=="tarfile"):
1345 return "tarfile";
1346 if(compresscheck=="zipfile"):
1347 return "zipfile";
1348 if(hasattr(infile, "read") or hasattr(infile, "write")):
1349 catfp = UncompressArchiveFile(infile, formatspecs[2]);
1350 else:
1351 try:
1352 if(compresscheck=="gzip"):
1353 try:
1354 import gzip;
1355 except ImportError:
1356 return False;
1357 catfp = gzip.GzipFile(infile, "rb");
1358 if(compresscheck=="bzip2"):
1359 try:
1360 import bz2;
1361 except ImportError:
1362 return False;
1363 catfp = bz2.BZ2File(infile, "rb");
1364 if(compresscheck=="lz4"):
1365 try:
1366 import lz4.frame;
1367 except ImportError:
1368 return False;
1369 catfp = lz4.frame.open(infile, "rb");
1370 if(compresscheck=="zstd"):
1371 try:
1372 import zstandard;
1373 except ImportError:
1374 return False;
1375 catfp = zstandard.open(infile, "rb");
1376 if(compresscheck=="lzma" or compresscheck=="xz"):
1377 try:
1378 import lzma;
1379 except ImportError:
1380 return False;
1381 catfp = lzma.open(infile, "rb");
1382 except FileNotFoundError:
1383 return False;
1384 filetype = False;
1385 prefp = catfp.read(5);
1386 if(prefp==binascii.unhexlify("7573746172")):
1387 filetype = "tarfile";
1388 catfp.seek(0, 0);
1389 prefp = catfp.read(7);
1390 if(prefp==binascii.unhexlify("43617446696c65")):
1391 filetype = "catfile";
1392 catfp.seek(0, 0);
1393 prefp = catfp.read(formatspecs[3]);
1394 if(prefp==binascii.unhexlify(formatspecs[4])):
1395 filetype = formatspecs[2];
1396 catfp.seek(0, 0);
1397 prefp = catfp.read(10);
1398 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
1399 filetype = "tarfile";
1400 catfp.seek(0, 0);
1401 catfp.close();
1402 return filetype;
1404 def GZipCompress(data, compresslevel=9):
1405 try:
1406 import gzip;
1407 except ImportError:
1408 return False;
1409 tmpfp = tempfile.NamedTemporaryFile("wb", delete=False);
1410 tmpfp.close();
1411 tmpfp = gzip.GzipFile(tmpfp.name, mode="wb", compresslevel=compresslevel);
1412 tmpfp.write(data);
1413 tmpfp.close();
1414 try:
1415 catfp = open(tmpfp.name, "rb");
1416 except FileNotFoundError:
1417 return False;
1418 catdata = catfp.read();
1419 catfp.close();
1420 return catdata;
1422 def CompressArchiveFile(fp, compression="auto", compressionlevel=None, formatspecs=__file_format_list__):
1423 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1424 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
1425 return False;
1426 fp.seek(0, 0);
1427 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1428 compression = None;
1429 if(compression not in compressionlist and compression is None):
1430 compression = "auto";
1431 if(compression=="gzip"):
1432 try:
1433 import gzip;
1434 except ImportError:
1435 return False;
1436 catfp = BytesIO();
1437 if(compressionlevel is None):
1438 compressionlevel = 9;
1439 else:
1440 compressionlevel = int(compressionlevel);
1441 catfp.write(gzip.compress(fp.read(), compresslevel=compressionlevel));
1442 if(compression=="bzip2"):
1443 try:
1444 import bz2;
1445 except ImportError:
1446 return False;
1447 catfp = BytesIO();
1448 if(compressionlevel is None):
1449 compressionlevel = 9;
1450 else:
1451 compressionlevel = int(compressionlevel);
1452 catfp.write(bz2.compress(fp.read(), compresslevel=compressionlevel));
1453 if(compression=="lz4"):
1454 try:
1455 import lz4.frame;
1456 except ImportError:
1457 return False;
1458 catfp = BytesIO();
1459 if(compressionlevel is None):
1460 compressionlevel = 9;
1461 else:
1462 compressionlevel = int(compressionlevel);
1463 catfp.write(lz4.frame.compress(fp.read(), compression_level=compressionlevel));
1464 if(compression=="lzo" or compression=="lzop"):
1465 try:
1466 import lzo;
1467 except ImportError:
1468 return False;
1469 catfp = BytesIO();
1470 if(compressionlevel is None):
1471 compressionlevel = 9;
1472 else:
1473 compressionlevel = int(compressionlevel);
1474 catfp.write(lzo.compress(fp.read(), compresslevel=compressionlevel));
1475 if(compression=="zstd"):
1476 try:
1477 import zstandard;
1478 except ImportError:
1479 return False;
1480 catfp = BytesIO();
1481 if(compressionlevel is None):
1482 compressionlevel = 10;
1483 else:
1484 compressionlevel = int(compressionlevel);
1485 catfp.write(zstandard.compress(fp.read(), level=compressionlevel));
1486 if(compression=="lzma"):
1487 try:
1488 import lzma;
1489 except ImportError:
1490 return False;
1491 catfp = BytesIO();
1492 if(compressionlevel is None):
1493 compressionlevel = 9;
1494 else:
1495 compressionlevel = int(compressionlevel);
1496 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]));
1497 if(compression=="xz"):
1498 try:
1499 import lzma;
1500 except ImportError:
1501 return False;
1502 catfp = BytesIO();
1503 if(compressionlevel is None):
1504 compressionlevel = 9;
1505 else:
1506 compressionlevel = int(compressionlevel);
1507 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]));
1508 if(compression=="auto" or compression is None):
1509 catfp = fp;
1510 catfp.seek(0, 0);
1511 return catfp;
1513 create_alias_function("Compress", __file_format_name__, "", CompressArchiveFile);
1515 def CompressOpenFile(outfile, compressionlevel=None):
1516 if(outfile is None):
1517 return False;
1518 fbasename = os.path.splitext(outfile)[0];
1519 fextname = os.path.splitext(outfile)[1];
1520 if(compressionlevel is None and fextname!=".zst"):
1521 compressionlevel = 9;
1522 elif(compressionlevel is None and fextname==".zst"):
1523 compressionlevel = 10;
1524 else:
1525 compressionlevel = int(compressionlevel);
1526 if(sys.version_info[0]==2):
1527 mode = "w";
1528 else:
1529 mode = "wb";
1530 try:
1531 if(fextname not in outextlistwd):
1532 try:
1533 outfp = open(outfile, "wb", encoding="UTF-8");
1534 except (ValueError, TypeError) as e:
1535 outfp = open(outfile, "wb");
1536 elif(fextname==".gz"):
1537 try:
1538 import gzip;
1539 except ImportError:
1540 return False;
1541 try:
1542 outfp = gzip.open(outfile, mode, compressionlevel, encoding="UTF-8");
1543 except (ValueError, TypeError) as e:
1544 outfp = gzip.open(outfile, mode, compressionlevel);
1545 elif(fextname==".bz2"):
1546 try:
1547 import bz2;
1548 except ImportError:
1549 return False;
1550 try:
1551 outfp = bz2.open(outfile, mode, compressionlevel, encoding="UTF-8");
1552 except (ValueError, TypeError) as e:
1553 outfp = bz2.open(outfile, mode, compressionlevel);
1554 elif(fextname==".zst"):
1555 try:
1556 import zstandard;
1557 except ImportError:
1558 return False;
1559 try:
1560 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel), encoding="UTF-8");
1561 except (ValueError, TypeError) as e:
1562 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel));
1563 elif(fextname==".xz"):
1564 try:
1565 import lzma;
1566 except ImportError:
1567 return False;
1568 try:
1569 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}], encoding="UTF-8");
1570 except (ValueError, TypeError) as e:
1571 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]);
1572 elif(fextname==".lz4"):
1573 try:
1574 import lz4.frame;
1575 except ImportError:
1576 return False;
1577 try:
1578 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel, encoding="UTF-8");
1579 except (ValueError, TypeError) as e:
1580 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel);
1581 elif(fextname==".lzo"):
1582 try:
1583 import lzo;
1584 except ImportError:
1585 return False;
1586 try:
1587 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel, encoding="UTF-8");
1588 except (ValueError, TypeError) as e:
1589 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel);
1590 elif(fextname==".lzma"):
1591 try:
1592 import lzma;
1593 except ImportError:
1594 return False;
1595 try:
1596 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}], encoding="UTF-8");
1597 except (ValueError, TypeError) as e:
1598 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]);
1599 except FileNotFoundError:
1600 return False;
1601 return outfp;
1603 def GetDevMajorMinor(fdev):
1604 retdev = [];
1605 if(hasattr(os, "minor")):
1606 retdev.append(os.minor(fdev));
1607 else:
1608 retdev.append(0);
1609 if(hasattr(os, "major")):
1610 retdev.append(os.major(fdev));
1611 else:
1612 retdev.append(0);
1613 return retdev;
1615 def CheckSumSupport(checkfor, guaranteed=True):
1616 if(guaranteed):
1617 hash_list = sorted(list(hashlib.algorithms_guaranteed));
1618 else:
1619 hash_list = sorted(list(hashlib.algorithms_available));
1620 checklistout = sorted(hash_list + ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1621 if(checkfor in checklistout):
1622 return True;
1623 else:
1624 return False;
1626 def CheckSumSupportAlt(checkfor, guaranteed=True):
1627 if(guaranteed):
1628 hash_list = sorted(list(hashlib.algorithms_guaranteed));
1629 else:
1630 hash_list = sorted(list(hashlib.algorithms_available));
1631 checklistout = hash_list;
1632 if(checkfor in checklistout):
1633 return True;
1634 else:
1635 return False;
1637 def PackArchiveFile(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1638 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1639 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1640 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1641 advancedlist = True;
1642 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1643 outfile = RemoveWindowsPath(outfile);
1644 checksumtype = checksumtype.lower();
1645 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1646 checksumtype="crc32";
1647 if(checksumtype=="none"):
1648 checksumtype = "";
1649 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1650 compression = None;
1651 if(compression not in compressionlist and compression is None):
1652 compression = "auto";
1653 if(verbose):
1654 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1655 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1656 if(os.path.exists(outfile)):
1657 os.unlink(outfile);
1658 if(outfile=="-"):
1659 verbose = False;
1660 catfp = BytesIO();
1661 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1662 catfp = outfile;
1663 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1664 catfp = BytesIO();
1665 else:
1666 fbasename = os.path.splitext(outfile)[0];
1667 fextname = os.path.splitext(outfile)[1];
1668 catfp = CompressOpenFile(outfile, compressionlevel);
1669 catver = formatspecs[6];
1670 fileheaderver = str(int(catver.replace(".", "")));
1671 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
1672 catfp.write(fileheader.encode('UTF-8'));
1673 infilelist = [];
1674 if(infiles=="-"):
1675 for line in sys.stdin:
1676 infilelist.append(line.strip());
1677 infilelist = list(filter(None, infilelist));
1678 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
1679 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
1680 return False;
1681 with open(infiles, "r") as finfile:
1682 for line in finfile:
1683 infilelist.append(line.strip());
1684 infilelist = list(filter(None, infilelist));
1685 else:
1686 if(isinstance(infiles, (list, tuple, ))):
1687 infilelist = list(filter(None, infiles));
1688 elif(isinstance(infiles, (str, ))):
1689 infilelist = list(filter(None, [infiles]));
1690 if(advancedlist):
1691 GetDirList = ListDirAdvanced(infilelist, followlink, False);
1692 else:
1693 GetDirList = ListDir(infilelist, followlink, False);
1694 if(not GetDirList):
1695 return False;
1696 curinode = 0;
1697 curfid = 0;
1698 inodelist = [];
1699 inodetofile = {};
1700 filetoinode = {};
1701 inodetocatinode = {};
1702 fnumfiles = format(int(len(GetDirList)), 'x').lower();
1703 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
1704 if(checksumtype=="none" or checksumtype==""):
1705 catfileheadercshex = format(0, 'x').lower();
1706 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1707 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1708 elif(checksumtype=="crc16_ccitt"):
1709 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1710 elif(checksumtype=="adler32"):
1711 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1712 elif(checksumtype=="crc32"):
1713 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1714 elif(checksumtype=="crc64_ecma"):
1715 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1716 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1717 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1718 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1719 checksumoutstr = hashlib.new(checksumtype);
1720 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
1721 catfileheadercshex = checksumoutstr.hexdigest().lower();
1722 else:
1723 catfileheadercshex = format(0, 'x').lower
1724 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
1725 catfp.write(fnumfilesa.encode('UTF-8'));
1726 try:
1727 catfp.flush();
1728 os.fsync(catfp.fileno());
1729 except io.UnsupportedOperation:
1730 pass;
1731 except AttributeError:
1732 pass;
1733 for curfname in GetDirList:
1734 catfhstart = catfp.tell();
1735 if(re.findall("^[.|/]", curfname)):
1736 fname = curfname;
1737 else:
1738 fname = "./"+curfname;
1739 if(verbose):
1740 VerbosePrintOut(fname);
1741 if(not followlink or followlink is None):
1742 fstatinfo = os.lstat(fname);
1743 else:
1744 fstatinfo = os.stat(fname);
1745 fpremode = fstatinfo.st_mode;
1746 finode = fstatinfo.st_ino;
1747 flinkcount = fstatinfo.st_nlink;
1748 ftype = 0;
1749 if(stat.S_ISREG(fpremode)):
1750 ftype = 0;
1751 elif(stat.S_ISLNK(fpremode)):
1752 ftype = 2;
1753 elif(stat.S_ISCHR(fpremode)):
1754 ftype = 3;
1755 elif(stat.S_ISBLK(fpremode)):
1756 ftype = 4;
1757 elif(stat.S_ISDIR(fpremode)):
1758 ftype = 5;
1759 elif(stat.S_ISFIFO(fpremode)):
1760 ftype = 6;
1761 elif(stat.S_ISSOCK(fpremode)):
1762 ftype = 8;
1763 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
1764 ftype = 9;
1765 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
1766 ftype = 10;
1767 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
1768 ftype = 111;
1769 else:
1770 ftype = 0;
1771 flinkname = "";
1772 fcurfid = format(int(curfid), 'x').lower();
1773 if(not followlink and finode!=0):
1774 if(ftype!=1):
1775 if(finode in inodelist):
1776 ftype = 1;
1777 flinkname = inodetofile[finode];
1778 fcurinode = format(int(inodetocatinode[finode]), 'x').lower();
1779 if(finode not in inodelist):
1780 inodelist.append(finode);
1781 inodetofile.update({finode: fname});
1782 inodetocatinode.update({finode: curinode});
1783 fcurinode = format(int(curinode), 'x').lower();
1784 curinode = curinode + 1;
1785 else:
1786 fcurinode = format(int(curinode), 'x').lower();
1787 curinode = curinode + 1;
1788 curfid = curfid + 1;
1789 if(ftype==2):
1790 flinkname = os.readlink(fname);
1791 fdev = fstatinfo.st_dev;
1792 getfdev = GetDevMajorMinor(fdev);
1793 fdev_minor = getfdev[0];
1794 fdev_major = getfdev[1];
1795 frdev = fstatinfo.st_dev;
1796 if(hasattr(fstatinfo, "st_rdev")):
1797 frdev = fstatinfo.st_rdev;
1798 else:
1799 frdev = fstatinfo.st_dev;
1800 getfrdev = GetDevMajorMinor(frdev);
1801 frdev_minor = getfrdev[0];
1802 frdev_major = getfrdev[1];
1803 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
1804 fsize = format(int("0"), 'x').lower();
1805 elif(ftype==0 or ftype==7):
1806 fsize = format(int(fstatinfo.st_size), 'x').lower();
1807 else:
1808 fsize = format(int(fstatinfo.st_size)).lower();
1809 fatime = format(int(fstatinfo.st_atime), 'x').lower();
1810 fmtime = format(int(fstatinfo.st_mtime), 'x').lower();
1811 fctime = format(int(fstatinfo.st_ctime), 'x').lower();
1812 if(hasattr(fstatinfo, "st_birthtime")):
1813 fbtime = format(int(fstatinfo.st_birthtime), 'x').lower();
1814 else:
1815 fbtime = format(int(fstatinfo.st_ctime), 'x').lower();
1816 fmode = format(int(fstatinfo.st_mode), 'x').lower();
1817 fchmode = format(int(stat.S_IMODE(fstatinfo.st_mode)), 'x').lower();
1818 ftypemod = format(int(stat.S_IFMT(fstatinfo.st_mode)), 'x').lower();
1819 fuid = format(int(fstatinfo.st_uid), 'x').lower();
1820 fgid = format(int(fstatinfo.st_gid), 'x').lower();
1821 funame = "";
1822 try:
1823 import pwd;
1824 try:
1825 userinfo = pwd.getpwuid(fstatinfo.st_uid);
1826 funame = userinfo.pw_name;
1827 except KeyError:
1828 funame = "";
1829 except ImportError:
1830 funame = "";
1831 fgname = "";
1832 try:
1833 import grp;
1834 try:
1835 groupinfo = grp.getgrgid(fstatinfo.st_gid);
1836 fgname = groupinfo.gr_name;
1837 except KeyError:
1838 fgname = "";
1839 except ImportError:
1840 fgname = "";
1841 fdev_minor = format(int(fdev_minor), 'x').lower();
1842 fdev_major = format(int(fdev_major), 'x').lower();
1843 frdev_minor = format(int(frdev_minor), 'x').lower();
1844 frdev_major = format(int(frdev_major), 'x').lower();
1845 finode = format(int(finode), 'x').lower();
1846 flinkcount = format(int(flinkcount), 'x').lower();
1847 if(hasattr(fstatinfo, "st_file_attributes")):
1848 fwinattributes = format(int(fstatinfo.st_file_attributes), 'x').lower();
1849 else:
1850 fwinattributes = format(int(0), 'x').lower();
1851 fcontents = "".encode('UTF-8');
1852 chunk_size = 1024;
1853 if(ftype == 0 or ftype == 7):
1854 with open(fname, "rb") as fpc:
1855 while(True):
1856 chunk = fpc.read(chunk_size);
1857 if(not chunk):
1858 break
1859 fcontents += chunk;
1860 if(followlink and (ftype == 1 or ftype == 2)):
1861 flstatinfo = os.stat(flinkname);
1862 with open(flinkname, "rb") as fpc:
1863 while(True):
1864 chunk = fpc.read(chunk_size);
1865 if(not chunk):
1866 break;
1867 fcontents += chunk;
1868 ftypehex = format(ftype, 'x').lower();
1869 extrafields = format(len(extradata), 'x').lower();
1870 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
1871 if(len(extradata)>0):
1872 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
1873 extrasizelen = format(len(extrasizestr), 'x').lower();
1874 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
1875 if(len(extradata)>0):
1876 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
1877 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
1878 if(checksumtype=="none" or checksumtype==""):
1879 catfileheadercshex = format(0, 'x').lower();
1880 catfilecontentcshex = format(0, 'x').lower();
1881 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1882 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1883 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
1884 elif(checksumtype=="crc16_ccitt"):
1885 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1886 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
1887 elif(checksumtype=="adler32"):
1888 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1889 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
1890 elif(checksumtype=="crc32"):
1891 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1892 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
1893 elif(checksumtype=="crc64_ecma"):
1894 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1895 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
1896 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1897 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1898 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
1899 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1900 checksumoutstr = hashlib.new(checksumtype);
1901 checksumoutstr.update("".encode('UTF-8'));
1902 catfileheadercshex = checksumoutstr.hexdigest().lower();
1903 checksumoutstr = hashlib.new(checksumtype);
1904 checksumoutstr.update(fcontents);
1905 catfilecontentcshex = checksumoutstr.hexdigest().lower();
1906 else:
1907 catfileheadercshex = format(0, 'x').lower();
1908 catfilecontentcshex = format(0, 'x').lower();
1909 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1910 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
1911 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
1912 if(checksumtype=="none" or checksumtype==""):
1913 catfileheadercshex = format(0, 'x').lower();
1914 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1915 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1916 elif(checksumtype=="crc16_ccitt"):
1917 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1918 elif(checksumtype=="adler32"):
1919 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1920 elif(checksumtype=="crc32"):
1921 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1922 elif(checksumtype=="crc64_ecma"):
1923 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1924 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1925 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1926 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1927 checksumoutstr = hashlib.new(checksumtype);
1928 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
1929 catfileheadercshex = checksumoutstr.hexdigest().lower();
1930 else:
1931 catfileheadercshex = format(0, 'x').lower();
1932 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1933 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
1934 catfcontentstart = catfp.tell() + len(catfileoutstr);
1935 catfileoutstrecd = catfileoutstr.encode('UTF-8');
1936 nullstrecd = formatspecs[5].encode('UTF-8');
1937 catfileout = catfileoutstrecd + fcontents + nullstrecd;
1938 catfcontentend = (catfp.tell() - 1) + len(catfileout);
1939 catfp.write(catfileout);
1940 try:
1941 catfp.flush();
1942 os.fsync(catfp.fileno());
1943 except io.UnsupportedOperation:
1944 pass;
1945 except AttributeError:
1946 pass;
1947 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1948 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1949 try:
1950 catfp.flush();
1951 os.fsync(catfp.fileno());
1952 except io.UnsupportedOperation:
1953 pass;
1954 except AttributeError:
1955 pass;
1956 if(outfile=="-"):
1957 catfp.seek(0, 0);
1958 if(hasattr(sys.stdout, "buffer")):
1959 shutil.copyfileobj(catfp, sys.stdout.buffer);
1960 else:
1961 shutil.copyfileobj(catfp, sys.stdout);
1962 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1963 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1964 catfp.seek(0, 0);
1965 upload_file_to_internet_file(catfp, outfile);
1966 if(returnfp):
1967 catfp.seek(0, 0);
1968 return catfp;
1969 else:
1970 catfp.close();
1971 return True;
1973 create_alias_function("Pack", __file_format_name__, "", PackArchiveFile);
1975 if(hasattr(shutil, "register_archive_format")):
1976 def PackArchiveFileFunc(archive_name, source_dir, **kwargs):
1977 return PackArchiveFile(source_dir, archive_name, False, "auto", None, False, "crc32", [], __file_format_delimiter__, False, False);
1978 create_alias_function("Pack", __file_format_name__, "Func", PackArchiveFileFunc);
1980 def PackArchiveFileFromDirList(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1981 return PackArchiveFile(infiles, outfile, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, returnfp);
1983 def PackArchiveFileFromTarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1984 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1985 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1986 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1987 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1988 outfile = RemoveWindowsPath(outfile);
1989 checksumtype = checksumtype.lower();
1990 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1991 checksumtype="crc32";
1992 if(checksumtype=="none"):
1993 checksumtype = "";
1994 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1995 compression = None;
1996 if(compression not in compressionlist and compression is None):
1997 compression = "auto";
1998 if(verbose):
1999 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2000 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2001 if(os.path.exists(outfile)):
2002 os.unlink(outfile);
2003 if(outfile=="-"):
2004 verbose = False;
2005 catfp = BytesIO();
2006 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2007 catfp = outfile;
2008 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2009 catfp = BytesIO();
2010 else:
2011 fbasename = os.path.splitext(outfile)[0];
2012 fextname = os.path.splitext(outfile)[1];
2013 catfp = CompressOpenFile(outfile, compressionlevel);
2014 catver = formatspecs[6];
2015 fileheaderver = str(int(catver.replace(".", "")));
2016 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2017 catfp.write(fileheader.encode('UTF-8'));
2018 curinode = 0;
2019 curfid = 0;
2020 inodelist = [];
2021 inodetofile = {};
2022 filetoinode = {};
2023 inodetocatinode = {};
2024 if(infile=="-"):
2025 infile = BytesIO();
2026 if(hasattr(sys.stdin, "buffer")):
2027 shutil.copyfileobj(sys.stdin.buffer, infile);
2028 else:
2029 shutil.copyfileobj(sys.stdin, infile);
2030 infile.seek(0, 0);
2031 if(not infile):
2032 return False;
2033 infile.seek(0, 0);
2034 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
2035 infile = download_file_from_internet_file(infile);
2036 infile.seek(0, 0);
2037 if(not infile):
2038 return False;
2039 infile.seek(0, 0);
2040 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
2041 return False;
2042 elif(os.path.exists(infile) and os.path.isfile(infile)):
2043 try:
2044 if(not tarfile.is_tarfile(infile)):
2045 return False;
2046 except AttributeError:
2047 if(not is_tarfile(infile)):
2048 return False;
2049 else:
2050 return False;
2051 try:
2052 if(hasattr(infile, "read") or hasattr(infile, "write")):
2053 tarfp = tarfile.open(fileobj=infile, mode="r");
2054 else:
2055 tarfp = tarfile.open(infile, "r");
2056 except FileNotFoundError:
2057 return False;
2058 fnumfiles = format(int(len(tarfp.getmembers())), 'x').lower();
2059 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2060 if(checksumtype=="none" or checksumtype==""):
2061 catfileheadercshex = format(0, 'x').lower();
2062 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2063 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2064 elif(checksumtype=="crc16_ccitt"):
2065 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2066 elif(checksumtype=="adler32"):
2067 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2068 elif(checksumtype=="crc32"):
2069 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2070 elif(checksumtype=="crc64_ecma"):
2071 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2072 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2073 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2074 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2075 checksumoutstr = hashlib.new(checksumtype);
2076 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2077 catfileheadercshex = checksumoutstr.hexdigest().lower();
2078 else:
2079 catfileheadercshex = format(0, 'x').lower();
2080 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2081 catfp.write(fnumfilesa.encode('UTF-8'));
2082 try:
2083 catfp.flush();
2084 os.fsync(catfp.fileno());
2085 except io.UnsupportedOperation:
2086 pass;
2087 except AttributeError:
2088 pass;
2089 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
2090 catfhstart = catfp.tell();
2091 if(re.findall("^[.|/]", member.name)):
2092 fname = member.name;
2093 else:
2094 fname = "./"+member.name;
2095 if(verbose):
2096 VerbosePrintOut(fname);
2097 fpremode = member.mode;
2098 ffullmode = member.mode;
2099 flinkcount = 0;
2100 ftype = 0;
2101 if(member.isreg()):
2102 ffullmode = member.mode + stat.S_IFREG;
2103 ftype = 0;
2104 elif(member.isdev()):
2105 ffullmode = member.mode;
2106 ftype = 7;
2107 elif(member.islnk()):
2108 ffullmode = member.mode + stat.S_IFREG;
2109 ftype = 1;
2110 elif(member.issym()):
2111 ffullmode = member.mode + stat.S_IFLNK;
2112 ftype = 2;
2113 elif(member.ischr()):
2114 ffullmode = member.mode + stat.S_IFCHR;
2115 ftype = 3;
2116 elif(member.isblk()):
2117 ffullmode = member.mode + stat.S_IFBLK;
2118 ftype = 4;
2119 elif(member.isdir()):
2120 ffullmode = member.mode + stat.S_IFDIR;
2121 ftype = 5;
2122 elif(member.isfifo()):
2123 ffullmode = member.mode + stat.S_IFIFO;
2124 ftype = 6;
2125 elif(member.issparse()):
2126 ffullmode = member.mode;
2127 ftype = 12;
2128 else:
2129 ffullmode = member.mode;
2130 ftype = 0;
2131 flinkname = "";
2132 fcurfid = format(int(curfid), 'x').lower();
2133 fcurinode = format(int(0), 'x').lower();
2134 curfid = curfid + 1;
2135 if(ftype==2):
2136 flinkname = member.linkname;
2137 fdev_minor = format(int(member.devminor), 'x').lower();
2138 fdev_major = format(int(member.devmajor), 'x').lower();
2139 frdev_minor = format(int(member.devminor), 'x').lower();
2140 frdev_major = format(int(member.devmajor), 'x').lower();
2141 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
2142 fsize = format(int("0"), 'x').lower();
2143 elif(ftype==0 or ftype==7):
2144 fsize = format(int(member.size), 'x').lower();
2145 else:
2146 fsize = format(int(member.size), 'x').lower();
2147 fatime = format(int(member.mtime), 'x').lower();
2148 fmtime = format(int(member.mtime), 'x').lower();
2149 fctime = format(int(member.mtime), 'x').lower();
2150 fbtime = format(int(member.mtime), 'x').lower();
2151 fmode = format(int(ffullmode), 'x').lower();
2152 fchmode = format(int(stat.S_IMODE(ffullmode)), 'x').lower();
2153 ftypemod = format(int(stat.S_IFMT(ffullmode)), 'x').lower();
2154 fuid = format(int(member.uid), 'x').lower();
2155 fgid = format(int(member.gid), 'x').lower();
2156 funame = member.uname;
2157 fgname = member.gname;
2158 flinkcount = format(int(flinkcount), 'x').lower();
2159 fcontents = "".encode('UTF-8');
2160 chunk_size = 1024;
2161 if(ftype == 0 or ftype == 7):
2162 with tarfp.extractfile(member) as fpc:
2163 while(True):
2164 chunk = fpc.read(chunk_size);
2165 if(not chunk):
2166 break
2167 fcontents += chunk;
2168 ftypehex = format(ftype, 'x').lower();
2169 extrafields = format(len(extradata), 'x').lower();
2170 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2171 if(len(extradata)>0):
2172 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2173 extrasizelen = format(len(extrasizestr), 'x').lower();
2174 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
2175 if(len(extradata)>0):
2176 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2177 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2178 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2179 catfcontentstart = catfp.tell() + len(catfileoutstr);
2180 if(checksumtype=="none" or checksumtype==""):
2181 catfileheadercshex = format(0, 'x').lower();
2182 catfilecontentcshex = format(0, 'x').lower();
2183 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2184 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2185 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2186 elif(checksumtype=="crc16_ccitt"):
2187 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2188 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2189 elif(checksumtype=="adler32"):
2190 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2191 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2192 elif(checksumtype=="crc32"):
2193 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2194 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2195 elif(checksumtype=="crc64_ecma"):
2196 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2197 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2198 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2199 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2200 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2201 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2202 checksumoutstr = hashlib.new(checksumtype);
2203 checksumoutstr.update("".encode('UTF-8'));
2204 catfileheadercshex = checksumoutstr.hexdigest().lower();
2205 checksumoutstr = hashlib.new(checksumtype);
2206 checksumoutstr.update(fcontents);
2207 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2208 else:
2209 catfileheadercshex = format(0, 'x').lower();
2210 catfilecontentcshex = format(0, 'x').lower();
2211 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2212 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2213 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
2214 if(checksumtype=="none" or checksumtype==""):
2215 catfileheadercshex = format(0, 'x').lower();
2216 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2217 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2218 elif(checksumtype=="crc16_ccitt"):
2219 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2220 elif(checksumtype=="adler32"):
2221 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2222 elif(checksumtype=="crc32"):
2223 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2224 elif(checksumtype=="crc64_ecma"):
2225 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2226 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2227 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2228 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2229 checksumoutstr = hashlib.new(checksumtype);
2230 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
2231 catfileheadercshex = checksumoutstr.hexdigest().lower();
2232 else:
2233 catfileheadercshex = format(0, 'x').lower();
2234 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2235 catfileoutstrecd = catfileoutstr.encode('UTF-8');
2236 nullstrecd = formatspecs[5].encode('UTF-8');
2237 catfileout = catfileoutstrecd + fcontents + nullstrecd;
2238 catfcontentend = (catfp.tell() - 1) + len(catfileout);
2239 catfp.write(catfileout);
2240 try:
2241 catfp.flush();
2242 os.fsync(catfp.fileno());
2243 except io.UnsupportedOperation:
2244 pass;
2245 except AttributeError:
2246 pass;
2247 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2248 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2249 try:
2250 catfp.flush();
2251 os.fsync(catfp.fileno());
2252 except io.UnsupportedOperation:
2253 pass;
2254 except AttributeError:
2255 pass;
2256 if(outfile=="-"):
2257 catfp.seek(0, 0);
2258 if(hasattr(sys.stdout, "buffer")):
2259 shutil.copyfileobj(catfp, sys.stdout.buffer);
2260 else:
2261 shutil.copyfileobj(catfp, sys.stdout);
2262 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2263 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2264 catfp.seek(0, 0);
2265 upload_file_from_internet_file(catfp, outfile);
2266 if(returnfp):
2267 catfp.seek(0, 0);
2268 return catfp;
2269 else:
2270 catfp.close();
2271 return True;
2273 create_alias_function("Pack", __file_format_name__, "FromTarFile", PackArchiveFileFromTarFile);
2275 def PackArchiveFileFromZipFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2276 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2277 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2278 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2279 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2280 outfile = RemoveWindowsPath(outfile);
2281 checksumtype = checksumtype.lower();
2282 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2283 checksumtype="crc32";
2284 if(checksumtype=="none"):
2285 checksumtype = "";
2286 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2287 compression = None;
2288 if(compression not in compressionlist and compression is None):
2289 compression = "auto";
2290 if(verbose):
2291 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2292 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2293 if(os.path.exists(outfile)):
2294 os.unlink(outfile);
2295 if(outfile=="-"):
2296 verbose = False;
2297 catfp = BytesIO();
2298 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2299 catfp = outfile;
2300 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2301 catfp = BytesIO();
2302 else:
2303 fbasename = os.path.splitext(outfile)[0];
2304 fextname = os.path.splitext(outfile)[1];
2305 catfp = CompressOpenFile(outfile, compressionlevel);
2306 catver = formatspecs[6];
2307 fileheaderver = str(int(catver.replace(".", "")));
2308 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2309 catfp.write(fileheader.encode('UTF-8'));
2310 curinode = 0;
2311 curfid = 0;
2312 inodelist = [];
2313 inodetofile = {};
2314 filetoinode = {};
2315 inodetocatinode = {};
2316 if(infile=="-"):
2317 infile = BytesIO();
2318 if(hasattr(sys.stdin, "buffer")):
2319 shutil.copyfileobj(sys.stdin.buffer, infile);
2320 else:
2321 shutil.copyfileobj(sys.stdin, infile);
2322 infile.seek(0, 0);
2323 if(not infile):
2324 return False;
2325 infile.seek(0, 0);
2326 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
2327 infile = download_file_from_internet_file(infile);
2328 infile.seek(0, 0);
2329 if(not infile):
2330 return False;
2331 infile.seek(0, 0);
2332 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
2333 return False;
2334 else:
2335 return False;
2336 if(not zipfile.is_zipfile(infile)):
2337 return False;
2338 try:
2339 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
2340 except FileNotFoundError:
2341 return False;
2342 ziptest = zipfp.testzip();
2343 if(ziptest):
2344 VerbosePrintOut("Bad file found!");
2345 fnumfiles = format(int(len(zipfp.infolist())), 'x').lower();
2346 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2347 if(checksumtype=="none" or checksumtype==""):
2348 catfileheadercshex = format(0, 'x').lower();
2349 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2350 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2351 elif(checksumtype=="crc16_ccitt"):
2352 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2353 elif(checksumtype=="adler32"):
2354 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2355 elif(checksumtype=="crc32"):
2356 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2357 elif(checksumtype=="crc64_ecma"):
2358 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2359 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2360 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2361 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2362 checksumoutstr = hashlib.new(checksumtype);
2363 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2364 catfileheadercshex = checksumoutstr.hexdigest().lower();
2365 else:
2366 catfileheadercshex = format(0, 'x').lower();
2367 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2368 catfp.write(fnumfilesa.encode('UTF-8'));
2369 try:
2370 catfp.flush();
2371 os.fsync(catfp.fileno());
2372 except io.UnsupportedOperation:
2373 pass;
2374 except AttributeError:
2375 pass;
2376 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
2377 catfhstart = catfp.tell();
2378 if(re.findall("^[.|/]", member.filename)):
2379 fname = member.filename;
2380 else:
2381 fname = "./"+member.filename;
2382 zipinfo = zipfp.getinfo(member.filename);
2383 if(verbose):
2384 VerbosePrintOut(fname);
2385 if(not member.is_dir()):
2386 fpremode = int(stat.S_IFREG + 438);
2387 elif(member.is_dir()):
2388 fpremode = int(stat.S_IFDIR + 511);
2389 flinkcount = 0;
2390 ftype = 0;
2391 if(not member.is_dir()):
2392 ftype = 0;
2393 elif(member.is_dir()):
2394 ftype = 5;
2395 flinkname = "";
2396 fcurfid = format(int(curfid), 'x').lower();
2397 fcurinode = format(int(0), 'x').lower();
2398 curfid = curfid + 1;
2399 fdev_minor = format(int(0), 'x').lower();
2400 fdev_major = format(int(0), 'x').lower();
2401 frdev_minor = format(int(0), 'x').lower();
2402 frdev_major = format(int(0), 'x').lower();
2403 if(ftype==5):
2404 fsize = format(int("0"), 'x').lower();
2405 elif(ftype==0):
2406 fsize = format(int(member.file_size), 'x').lower();
2407 else:
2408 fsize = format(int(member.file_size), 'x').lower();
2409 fatime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2410 fmtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2411 fctime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2412 fbtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2413 if(not member.is_dir()):
2414 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
2415 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2416 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2417 if(member.is_dir()):
2418 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
2419 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
2420 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
2421 try:
2422 fuid = format(int(os.getuid()), 'x').lower();
2423 except AttributeError:
2424 fuid = format(int(0), 'x').lower();
2425 except KeyError:
2426 fuid = format(int(0), 'x').lower();
2427 try:
2428 fgid = format(int(os.getgid()), 'x').lower();
2429 except AttributeError:
2430 fgid = format(int(0), 'x').lower();
2431 except KeyError:
2432 fgid = format(int(0), 'x').lower();
2433 try:
2434 import pwd;
2435 try:
2436 userinfo = pwd.getpwuid(os.getuid());
2437 funame = userinfo.pw_name;
2438 except KeyError:
2439 funame = "";
2440 except AttributeError:
2441 funame = "";
2442 except ImportError:
2443 funame = "";
2444 fgname = "";
2445 try:
2446 import grp;
2447 try:
2448 groupinfo = grp.getgrgid(os.getgid());
2449 fgname = groupinfo.gr_name;
2450 except KeyError:
2451 fgname = "";
2452 except AttributeError:
2453 fgname = "";
2454 except ImportError:
2455 fgname = "";
2456 fcontents = "".encode('UTF-8');
2457 if(ftype==0):
2458 fcontents = zipfp.read(member.filename);
2459 ftypehex = format(ftype, 'x').lower();
2460 extrafields = format(len(extradata), 'x').lower();
2461 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2462 if(len(extradata)>0):
2463 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2464 extrasizelen = format(len(extrasizestr), 'x').lower();
2465 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
2466 if(len(extradata)>0):
2467 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2468 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2469 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2470 catfcontentstart = catfp.tell() + len(catfileoutstr);
2471 if(checksumtype=="none" or checksumtype==""):
2472 catfileheadercshex = format(0, 'x').lower();
2473 catfilecontentcshex = format(0, 'x').lower();
2474 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2475 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2476 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2477 elif(checksumtype=="crc16_ccitt"):
2478 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2479 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2480 elif(checksumtype=="adler32"):
2481 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2482 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2483 elif(checksumtype=="crc32"):
2484 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2485 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2486 elif(checksumtype=="crc64_ecma"):
2487 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2488 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2489 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2490 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2491 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2492 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2493 checksumoutstr = hashlib.new(checksumtype);
2494 checksumoutstr.update("".encode('UTF-8'));
2495 catfileheadercshex = checksumoutstr.hexdigest().lower();
2496 checksumoutstr = hashlib.new(checksumtype);
2497 checksumoutstr.update(fcontents);
2498 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2499 else:
2500 catfileheadercshex = format(0, 'x').lower();
2501 catfilecontentcshex = format(0, 'x').lower();
2502 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2503 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2504 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
2505 if(checksumtype=="none" or checksumtype==""):
2506 catfileheadercshex = format(0, 'x').lower();
2507 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2508 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2509 elif(checksumtype=="crc16_ccitt"):
2510 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2511 elif(checksumtype=="adler32"):
2512 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2513 elif(checksumtype=="crc32"):
2514 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2515 elif(checksumtype=="crc64_ecma"):
2516 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2517 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2518 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2519 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2520 checksumoutstr = hashlib.new(checksumtype);
2521 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
2522 catfileheadercshex = checksumoutstr.hexdigest().lower();
2523 else:
2524 catfileheadercshex = format(0, 'x').lower();
2525 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2526 catfileoutstrecd = catfileoutstr.encode('UTF-8');
2527 nullstrecd = formatspecs[5].encode('UTF-8');
2528 catfileout = catfileoutstrecd + fcontents + nullstrecd;
2529 catfcontentend = (catfp.tell() - 1) + len(catfileout);
2530 catfp.write(catfileout);
2531 try:
2532 catfp.flush();
2533 os.fsync(catfp.fileno());
2534 except io.UnsupportedOperation:
2535 pass;
2536 except AttributeError:
2537 pass;
2538 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2539 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2540 try:
2541 catfp.flush();
2542 os.fsync(catfp.fileno());
2543 except io.UnsupportedOperation:
2544 pass;
2545 except AttributeError:
2546 pass;
2547 if(outfile=="-"):
2548 catfp.seek(0, 0);
2549 if(hasattr(sys.stdout, "buffer")):
2550 shutil.copyfileobj(catfp, sys.stdout.buffer);
2551 else:
2552 shutil.copyfileobj(catfp, sys.stdout);
2553 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2554 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2555 catfp.seek(0, 0);
2556 upload_file_to_internet_file(catfp, outfile);
2557 if(returnfp):
2558 catfp.seek(0, 0);
2559 return catfp;
2560 else:
2561 catfp.close();
2562 return True;
2564 create_alias_function("Pack", __file_format_name__, "FromZipFile", PackArchiveFileFromZipFile);
2566 if(not rarfile_support):
2567 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2568 return False
2570 if(rarfile_support):
2571 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2572 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2573 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2574 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2575 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2576 outfile = RemoveWindowsPath(outfile);
2577 checksumtype = checksumtype.lower();
2578 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2579 checksumtype="crc32";
2580 if(checksumtype=="none"):
2581 checksumtype = "";
2582 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2583 compression = None;
2584 if(compression not in compressionlist and compression is None):
2585 compression = "auto";
2586 if(verbose):
2587 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2588 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2589 if(os.path.exists(outfile)):
2590 os.unlink(outfile);
2591 if(outfile=="-"):
2592 verbose = False;
2593 catfp = BytesIO();
2594 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2595 catfp = outfile;
2596 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2597 catfp = BytesIO();
2598 else:
2599 fbasename = os.path.splitext(outfile)[0];
2600 fextname = os.path.splitext(outfile)[1];
2601 catfp = CompressOpenFile(outfile, compressionlevel);
2602 catver = formatspecs[6];
2603 fileheaderver = str(int(catver.replace(".", "")));
2604 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2605 catfp.write(fileheader.encode('UTF-8'));
2606 curinode = 0;
2607 curfid = 0;
2608 inodelist = [];
2609 inodetofile = {};
2610 filetoinode = {};
2611 inodetocatinode = {};
2612 if(not os.path.exists(infile) or not os.path.isfile(infile)):
2613 return False;
2614 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
2615 return False;
2616 rarfp = rarfile.RarFile(infile, "r");
2617 rartest = rarfp.testrar();
2618 if(rartest):
2619 VerbosePrintOut("Bad file found!");
2620 fnumfiles = format(int(len(rarfp.infolist())), 'x').lower();
2621 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2622 if(checksumtype=="none" or checksumtype==""):
2623 catfileheadercshex = format(0, 'x').lower();
2624 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2625 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2626 elif(checksumtype=="crc16_ccitt"):
2627 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2628 elif(checksumtype=="adler32"):
2629 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2630 elif(checksumtype=="crc32"):
2631 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2632 elif(checksumtype=="crc64_ecma"):
2633 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2634 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2635 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2636 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2637 checksumoutstr = hashlib.new(checksumtype);
2638 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2639 catfileheadercshex = checksumoutstr.hexdigest().lower();
2640 else:
2641 catfileheadercshex = format(0, 'x').lower();
2642 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2643 catfp.write(fnumfilesa.encode('UTF-8'));
2644 try:
2645 catfp.flush();
2646 os.fsync(catfp.fileno());
2647 except io.UnsupportedOperation:
2648 pass;
2649 except AttributeError:
2650 pass;
2651 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
2652 is_unix = False;
2653 is_windows = False;
2654 if(member.host_os == rarfile.RAR_OS_UNIX):
2655 is_windows = False;
2656 try:
2657 member.external_attr
2658 is_unix = True;
2659 except AttributeError:
2660 is_unix = False;
2661 elif(member.host_os == rarfile.RAR_OS_WIN32):
2662 is_unix = False;
2663 try:
2664 member.external_attr
2665 is_windows = True;
2666 except AttributeError:
2667 is_windows = False;
2668 else:
2669 is_unix = False;
2670 is_windows = False;
2671 catfhstart = catfp.tell();
2672 if(re.findall("^[.|/]", member.filename)):
2673 fname = member.filename;
2674 else:
2675 fname = "./"+member.filename;
2676 rarinfo = rarfp.getinfo(member.filename);
2677 if(verbose):
2678 VerbosePrintOut(fname);
2679 if(is_unix and member.external_attr !=0):
2680 fpremode = int(member.external_attr);
2681 elif(member.is_file()):
2682 fpremode = int(stat.S_IFREG + 438);
2683 elif(member.is_symlink()):
2684 fpremode = int(stat.S_IFLNK + 438);
2685 elif(member.is_dir()):
2686 fpremode = int(stat.S_IFDIR + 511);
2687 if(is_windows and member.external_attr !=0):
2688 fwinattributes = format(int(member.external_attr), 'x').lower();
2689 else:
2690 fwinattributes = format(int(0), 'x').lower();
2691 flinkcount = 0;
2692 ftype = 0;
2693 if(member.is_file()):
2694 ftype = 0;
2695 elif(member.is_symlink()):
2696 ftype = 2;
2697 elif(member.is_dir()):
2698 ftype = 5;
2699 flinkname = "";
2700 if(ftype==2):
2701 flinkname = rarfp.read(member.filename).decode("UTF-8");
2702 fcurfid = format(int(curfid), 'x').lower();
2703 fcurinode = format(int(0), 'x').lower();
2704 curfid = curfid + 1;
2705 fdev_minor = format(int(0), 'x').lower();
2706 fdev_major = format(int(0), 'x').lower();
2707 frdev_minor = format(int(0), 'x').lower();
2708 frdev_major = format(int(0), 'x').lower();
2709 if(ftype==5):
2710 fsize = format(int("0"), 'x').lower();
2711 elif(ftype==0):
2712 fsize = format(int(member.file_size), 'x').lower();
2713 else:
2714 fsize = format(int(member.file_size), 'x').lower();
2715 try:
2716 if(member.atime):
2717 fatime = format(int(member.atime.timestamp()), 'x').lower();
2718 else:
2719 fatime = format(int(member.mtime.timestamp()), 'x').lower();
2720 except AttributeError:
2721 fatime = format(int(member.mtime.timestamp()), 'x').lower();
2722 fmtime = format(int(member.mtime.timestamp()), 'x').lower();
2723 try:
2724 if(member.ctime):
2725 fctime = format(int(member.ctime.timestamp()), 'x').lower();
2726 else:
2727 fctime = format(int(member.mtime.timestamp()), 'x').lower();
2728 except AttributeError:
2729 fctime = format(int(member.mtime.timestamp()), 'x').lower();
2730 fbtime = format(int(member.mtime.timestamp()), 'x').lower();
2731 if(is_unix and member.external_attr !=0):
2732 fmode = format(int(member.external_attr), 'x').lower();
2733 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
2734 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
2735 elif(member.is_file()):
2736 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
2737 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2738 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2739 elif(member.is_symlink()):
2740 fmode = format(int(stat.S_IFLNK + 438), 'x').lower();
2741 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2742 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2743 elif(member.is_dir()):
2744 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
2745 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
2746 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
2747 try:
2748 fuid = format(int(os.getuid()), 'x').lower();
2749 except AttributeError:
2750 fuid = format(int(0), 'x').lower();
2751 except KeyError:
2752 fuid = format(int(0), 'x').lower();
2753 try:
2754 fgid = format(int(os.getgid()), 'x').lower();
2755 except AttributeError:
2756 fgid = format(int(0), 'x').lower();
2757 except KeyError:
2758 fgid = format(int(0), 'x').lower();
2759 try:
2760 import pwd;
2761 try:
2762 userinfo = pwd.getpwuid(os.getuid());
2763 funame = userinfo.pw_name;
2764 except KeyError:
2765 funame = "";
2766 except AttributeError:
2767 funame = "";
2768 except ImportError:
2769 funame = "";
2770 fgname = "";
2771 try:
2772 import grp;
2773 try:
2774 groupinfo = grp.getgrgid(os.getgid());
2775 fgname = groupinfo.gr_name;
2776 except KeyError:
2777 fgname = "";
2778 except AttributeError:
2779 fgname = "";
2780 except ImportError:
2781 fgname = "";
2782 fcontents = "".encode('UTF-8');
2783 if(ftype==0):
2784 fcontents = rarfp.read(member.filename);
2785 ftypehex = format(ftype, 'x').lower();
2786 extrafields = format(len(extradata), 'x').lower();
2787 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2788 if(len(extradata)>0):
2789 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2790 extrasizelen = format(len(extrasizestr), 'x').lower();
2791 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
2792 if(len(extradata)>0):
2793 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2794 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2795 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2796 catfcontentstart = catfp.tell() + len(catfileoutstr);
2797 if(checksumtype=="none" or checksumtype==""):
2798 catfileheadercshex = format(0, 'x').lower();
2799 catfilecontentcshex = format(0, 'x').lower();
2800 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2801 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2802 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2803 elif(checksumtype=="crc16_ccitt"):
2804 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2805 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2806 elif(checksumtype=="adler32"):
2807 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2808 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2809 elif(checksumtype=="crc32"):
2810 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2811 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2812 elif(checksumtype=="crc64_ecma"):
2813 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2814 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2815 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2816 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2817 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2818 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2819 checksumoutstr = hashlib.new(checksumtype);
2820 checksumoutstr.update("".encode('UTF-8'));
2821 catfileheadercshex = checksumoutstr.hexdigest().lower();
2822 checksumoutstr = hashlib.new(checksumtype);
2823 checksumoutstr.update(fcontents);
2824 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2825 else:
2826 catfileheadercshex = format(0, 'x').lower();
2827 catfilecontentcshex = format(0, 'x').lower();
2828 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2829 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2830 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr
2831 if(checksumtype=="none" or checksumtype==""):
2832 catfileheadercshex = format(0, 'x').lower()
2833 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2834 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
2835 elif(checksumtype=="crc16_ccitt"):
2836 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
2837 elif(checksumtype=="adler32"):
2838 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
2839 elif(checksumtype=="crc32"):
2840 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
2841 elif(checksumtype=="crc64_ecma"):
2842 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2843 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2844 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2845 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2846 checksumoutstr = hashlib.new(checksumtype)
2847 checksumoutstr.update(catfileoutstr.encode('UTF-8'))
2848 catfileheadercshex = checksumoutstr.hexdigest().lower()
2849 else:
2850 catfileheadercshex = format(0, 'x').lower()
2851 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5])
2852 catfileoutstrecd = catfileoutstr.encode('UTF-8')
2853 nullstrecd = formatspecs[5].encode('UTF-8')
2854 catfileout = catfileoutstrecd + fcontents + nullstrecd
2855 catfcontentend = (catfp.tell() - 1) + len(catfileout)
2856 catfp.write(catfileout)
2857 try:
2858 catfp.flush()
2859 os.fsync(catfp.fileno())
2860 except io.UnsupportedOperation:
2861 pass
2862 except AttributeError:
2863 pass
2864 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2865 catfp = CompressArchiveFile(catfp, compression, formatspecs)
2866 try:
2867 catfp.flush()
2868 os.fsync(catfp.fileno())
2869 except io.UnsupportedOperation:
2870 pass
2871 except AttributeError:
2872 pass
2873 if(outfile=="-"):
2874 catfp.seek(0, 0)
2875 if(hasattr(sys.stdout, "buffer")):
2876 shutil.copyfileobj(catfp, sys.stdout.buffer);
2877 else:
2878 shutil.copyfileobj(catfp, sys.stdout);
2879 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2880 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2881 catfp.seek(0, 0);
2882 upload_file_to_internet_file(catfp, outfile);
2883 if(returnfp):
2884 catfp.seek(0, 0)
2885 return catfp
2886 else:
2887 catfp.close()
2888 return True;
2890 create_alias_function("Pack", __file_format_name__, "FromRarFile", PackArchiveFileFromRarFile);
2892 def ArchiveFileSeekToFileNum(infile, seekto=0, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
2893 if(hasattr(infile, "read") or hasattr(infile, "write")):
2894 catfp = infile;
2895 catfp.seek(0, 0);
2896 catfp = UncompressArchiveFile(catfp, formatspecs);
2897 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
2898 if(checkcompressfile=="tarfile"):
2899 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2900 if(checkcompressfile=="zipfile"):
2901 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2902 if(rarfile_support and checkcompressfile=="rarfile"):
2903 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2904 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
2905 return False;
2906 if(not catfp):
2907 return False;
2908 catfp.seek(0, 0);
2909 elif(infile=="-"):
2910 catfp = BytesIO();
2911 if(hasattr(sys.stdin, "buffer")):
2912 shutil.copyfileobj(sys.stdin.buffer, catfp);
2913 else:
2914 shutil.copyfileobj(sys.stdin, catfp);
2915 catfp.seek(0, 0);
2916 catfp = UncompressArchiveFile(catfp, formatspecs);
2917 if(not catfp):
2918 return False;
2919 catfp.seek(0, 0);
2920 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
2921 catfp = download_file_from_internet_file(infile);
2922 catfp.seek(0, 0);
2923 catfp = UncompressArchiveFile(catfp, formatspecs);
2924 if(not catfp):
2925 return False;
2926 catfp.seek(0, 0);
2927 else:
2928 infile = RemoveWindowsPath(infile);
2929 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
2930 if(checkcompressfile=="tarfile"):
2931 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2932 if(checkcompressfile=="zipfile"):
2933 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2934 if(rarfile_support and checkcompressfile=="rarfile"):
2935 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2936 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
2937 return False;
2938 compresscheck = CheckCompressionType(infile, formatspecs, True);
2939 if(not compresscheck):
2940 fextname = os.path.splitext(infile)[1];
2941 if(fextname==".gz"):
2942 compresscheck = "gzip";
2943 elif(fextname==".bz2"):
2944 compresscheck = "bzip2";
2945 elif(fextname==".zst"):
2946 compresscheck = "zstd";
2947 elif(fextname==".lz4" or fextname==".clz4"):
2948 compresscheck = "lz4";
2949 elif(fextname==".lzo" or fextname==".lzop"):
2950 compresscheck = "lzo";
2951 elif(fextname==".lzma" or fextname==".xz"):
2952 compresscheck = "lzma";
2953 else:
2954 return False;
2955 if(not compresscheck):
2956 return False;
2957 catfp = UncompressFile(infile, formatspecs, "rb");
2959 try:
2960 catfp.seek(0, 2);
2961 except OSError:
2962 SeekToEndOfFile(catfp);
2963 except ValueError:
2964 SeekToEndOfFile(catfp);
2965 CatSize = catfp.tell();
2966 CatSizeEnd = CatSize;
2968 try:
2969 catfp.seek(0, 0);
2970 except OSError:
2971 return False;
2972 except ValueError:
2973 return False;
2974 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
2975 catstring = catheader[0];
2976 catversion = re.findall(r"([\d]+)$", catstring);
2977 fprenumfiles = catheader[1];
2978 fnumfiles = int(fprenumfiles, 16);
2979 fprechecksumtype = catheader[2];
2980 fprechecksum = catheader[3];
2981 fileheader = AppendNullByte(catstring, formatspecs[5]);
2982 fnumfileshex = format(int(fnumfiles), 'x').lower();
2983 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
2984 if(fprechecksumtype=="none" or fprechecksumtype==""):
2985 catfileheadercshex = format(0, 'x').lower();
2986 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
2987 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2988 elif(fprechecksumtype=="crc16_ccitt"):
2989 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2990 elif(fprechecksumtype=="adler32"):
2991 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2992 elif(fprechecksumtype=="crc32"):
2993 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2994 elif(fprechecksumtype=="crc64_ecma"):
2995 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2996 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
2997 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2998 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
2999 checksumoutstr = hashlib.new(fprechecksumtype);
3000 checksumoutstr.update(fileheader.encode('UTF-8'));
3001 catfileheadercshex = checksumoutstr.hexdigest().lower();
3002 else:
3003 catfileheadercshex = format(0, 'x').lower();
3004 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3005 fheadtell = len(fileheader);
3006 if(fprechecksum!=catfileheadercshex and not skipchecksum):
3007 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3008 return False;
3009 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3010 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
3011 if(seekto>=fnumfiles):
3012 seekto = fnumfiles - 1;
3013 if(seekto<0):
3014 seekto = 0;
3015 if(seekto>=0):
3016 il = -1;
3017 while(il < seekto):
3018 prefhstart = catfp.tell();
3019 if(formatspecs[7]):
3020 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3021 else:
3022 preheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3023 prefheadsize = int(preheaderdata[0], 16);
3024 preftype = int(preheaderdata[1], 16);
3025 if(re.findall("^[.|/]", preheaderdata[2])):
3026 prefname = preheaderdata[2];
3027 else:
3028 prefname = "./"+preheaderdata[2];
3029 prefbasedir = os.path.dirname(prefname);
3030 preflinkname = preheaderdata[3];
3031 prefsize = int(preheaderdata[4], 16);
3032 prefatime = int(preheaderdata[5], 16);
3033 prefmtime = int(preheaderdata[6], 16);
3034 prefctime = int(preheaderdata[7], 16);
3035 prefbtime = int(preheaderdata[8], 16);
3036 prefmode = int(preheaderdata[9], 16);
3037 prefchmode = stat.S_IMODE(prefmode);
3038 preftypemod = stat.S_IFMT(prefmode);
3039 prefuid = int(preheaderdata[10], 16);
3040 prefuname = preheaderdata[11];
3041 prefgid = int(preheaderdata[12], 16);
3042 prefgname = preheaderdata[13];
3043 fid = int(preheaderdata[14], 16);
3044 finode = int(preheaderdata[15], 16);
3045 flinkcount = int(preheaderdata[16], 16);
3046 prefdev_minor = int(preheaderdata[17], 16);
3047 prefdev_major = int(preheaderdata[18], 16);
3048 prefrdev_minor = int(preheaderdata[19], 16);
3049 prefrdev_major = int(preheaderdata[20], 16);
3050 prefextrasize = int(preheaderdata[21], 16);
3051 prefextrafields = int(preheaderdata[22], 16);
3052 extrafieldslist = [];
3053 if(formatspecs[7]):
3054 extrastart = 23;
3055 extraend = extrastart + prefextrafields;
3056 extrafieldslist = [];
3057 if(extrastart<extraend):
3058 extrafieldslist.append(preheaderdata[extrastart]);
3059 extrastart = extrastart + 1;
3060 prefchecksumtype = preheaderdata[extrastart].lower();
3061 prefcs = preheaderdata[extrastart + 1].lower();
3062 prefccs = preheaderdata[extrastart + 2].lower();
3063 else:
3064 extrafieldslist = [];
3065 if(formatspecs[7]):
3066 extrafieldslist = ReadFileHeaderData(catfp, prefextrafields, formatspecs[5]);
3067 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3068 prefchecksumtype = checksumsval[0].lower();
3069 prefcs = checksumsval[1].lower();
3070 prefccs = checksumsval[2].lower();
3071 hc = 0;
3072 if(formatspecs[7]):
3073 hcmax = len(preheaderdata) - 2;
3074 else:
3075 hcmax = len(preheaderdata);
3076 hout = "";
3077 while(hc<hcmax):
3078 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
3079 hc = hc + 1;
3080 prefnumfields = 24 + prefextrafields;
3081 if(prefchecksumtype=="none" or prefchecksumtype==""):
3082 prenewfcs = 0;
3083 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3084 prenewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3085 elif(prefchecksumtype=="adler32"):
3086 prenewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3087 elif(prefchecksumtype=="crc32"):
3088 prenewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3089 elif(prefchecksumtype=="crc64_ecma"):
3090 prenewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3091 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3092 prenewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3093 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3094 checksumoutstr = hashlib.new(prefchecksumtype);
3095 checksumoutstr.update(hout.encode('UTF-8'));
3096 prenewfcs = checksumoutstr.hexdigest().lower();
3097 if(prefcs!=prenewfcs and not skipchecksum):
3098 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
3099 return False;
3100 valid_archive = False;
3101 invalid_archive = True;
3102 prefhend = catfp.tell() - 1;
3103 prefcontentstart = catfp.tell();
3104 prefcontents = "";
3105 pyhascontents = False;
3106 if(prefsize>0):
3107 prefcontents = catfp.read(prefsize);
3108 if(prefchecksumtype=="none" or prefchecksumtype==""):
3109 prenewfccs = 0;
3110 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3111 prenewfccs = format(crc16(prefcontents) & 0xffff, '04x').lower();
3112 elif(prefchecksumtype=="crc16_ccitt"):
3113 prenewfcs = format(crc16_ccitt(prefcontents) & 0xffff, '04x').lower();
3114 elif(prefchecksumtype=="adler32"):
3115 prenewfccs = format(zlib.adler32(prefcontents) & 0xffffffff, '08x').lower();
3116 elif(prefchecksumtype=="crc32"):
3117 prenewfccs = format(crc32(prefcontents) & 0xffffffff, '08x').lower();
3118 elif(prefchecksumtype=="crc64_ecma"):
3119 prenewfcs = format(crc64_ecma(prefcontents) & 0xffffffffffffffff, '016x').lower();
3120 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3121 prenewfcs = format(crc64_iso(prefcontents) & 0xffffffffffffffff, '016x').lower();
3122 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3123 checksumoutstr = hashlib.new(prefchecksumtype);
3124 checksumoutstr.update(prefcontents);
3125 prenewfccs = checksumoutstr.hexdigest().lower();
3126 pyhascontents = True;
3127 if(prefccs!=prenewfccs and not skipchecksum):
3128 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
3129 return False;
3130 catfp.seek(1, 1);
3131 il = il + 1;
3132 catfp.seek(seekstart, 0);
3133 fileidnum = il;
3134 catfheadsize = int(preheaderdata[0], 16);
3135 catftype = int(preheaderdata[1], 16);
3136 if(re.findall("^[.|/]", preheaderdata[2])):
3137 catfname = preheaderdata[2];
3138 else:
3139 catfname = "./"+preheaderdata[2];
3140 catflinkname = preheaderdata[3];
3141 catfsize = int(preheaderdata[4], 16);
3142 catfbasedir = os.path.dirname(catfname);
3143 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
3144 if(returnfp):
3145 catlist.update({'catfp': catfp});
3146 else:
3147 catfp.close();
3148 return catlist;
3150 create_alias_function("", __file_format_name__, "SeekToFileNum", ArchiveFileSeekToFileNum);
3152 def ArchiveFileSeekToFileName(infile, seekfile=None, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3153 if(hasattr(infile, "read") or hasattr(infile, "write")):
3154 catfp = infile;
3155 catfp.seek(0, 0);
3156 catfp = UncompressArchiveFile(catfp, formatspecs);
3157 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
3158 if(checkcompressfile=="tarfile"):
3159 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3160 if(checkcompressfile=="zipfile"):
3161 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3162 if(rarfile_support and checkcompressfile=="rarfile"):
3163 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3164 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3165 return False;
3166 if(not catfp):
3167 return False;
3168 catfp.seek(0, 0);
3169 elif(infile=="-"):
3170 catfp = BytesIO();
3171 if(hasattr(sys.stdin, "buffer")):
3172 shutil.copyfileobj(sys.stdin.buffer, catfp);
3173 else:
3174 shutil.copyfileobj(sys.stdin, catfp);
3175 catfp.seek(0, 0);
3176 catfp = UncompressArchiveFile(catfp, formatspecs);
3177 if(not catfp):
3178 return False;
3179 catfp.seek(0, 0);
3180 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
3181 catfp = download_file_from_internet_file(infile);
3182 catfp = UncompressArchiveFile(catfp, formatspecs);
3183 catfp.seek(0, 0);
3184 if(not catfp):
3185 return False;
3186 catfp.seek(0, 0);
3187 else:
3188 infile = RemoveWindowsPath(infile);
3189 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
3190 if(checkcompressfile=="tarfile"):
3191 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3192 if(checkcompressfile=="zipfile"):
3193 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3194 if(rarfile_support and checkcompressfile=="rarfile"):
3195 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
3196 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3197 return False;
3198 compresscheck = CheckCompressionType(infile, formatspecs, True);
3199 if(not compresscheck):
3200 fextname = os.path.splitext(infile)[1];
3201 if(fextname==".gz"):
3202 compresscheck = "gzip";
3203 elif(fextname==".bz2"):
3204 compresscheck = "bzip2";
3205 elif(fextname==".zst"):
3206 compresscheck = "zstd";
3207 elif(fextname==".lz4" or fextname==".clz4"):
3208 compresscheck = "lz4";
3209 elif(fextname==".lzo" or fextname==".lzop"):
3210 compresscheck = "lzo";
3211 elif(fextname==".lzma" or fextname==".xz"):
3212 compresscheck = "lzma";
3213 else:
3214 return False;
3215 if(not compresscheck):
3216 return False;
3217 catfp = UncompressFile(infile, formatspecs, "rb");
3219 try:
3220 catfp.seek(0, 2);
3221 except OSError:
3222 SeekToEndOfFile(catfp);
3223 except ValueError:
3224 SeekToEndOfFile(catfp);
3225 CatSize = catfp.tell();
3226 CatSizeEnd = CatSize;
3228 try:
3229 catfp.seek(0, 0);
3230 except OSError:
3231 return False;
3232 except ValueError:
3233 return False;
3234 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3235 catstring = catheader[0];
3236 catversion = re.findall(r"([\d]+)$", catstring);
3237 fprenumfiles = catheader[1];
3238 fnumfiles = int(fprenumfiles, 16);
3239 fprechecksumtype = catheader[2];
3240 fprechecksum = catheader[3];
3241 fileheader = AppendNullByte(catstring, formatspecs[5]);
3242 fnumfileshex = format(int(fnumfiles), 'x').lower();
3243 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3244 if(fprechecksumtype=="none" or fprechecksumtype==""):
3245 catfileheadercshex = format(0, 'x').lower();
3246 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3247 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3248 elif(fprechecksumtype=="crc16_ccitt"):
3249 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3250 elif(fprechecksumtype=="adler32"):
3251 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3252 elif(fprechecksumtype=="crc32"):
3253 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3254 elif(fprechecksumtype=="crc64_ecma"):
3255 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3256 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3257 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3258 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3259 checksumoutstr = hashlib.new(fprechecksumtype);
3260 checksumoutstr.update(fileheader.encode('UTF-8'));
3261 catfileheadercshex = checksumoutstr.hexdigest().lower();
3262 else:
3263 catfileheadercshex = format(0, 'x').lower();
3264 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3265 fheadtell = len(fileheader);
3266 if(fprechecksum!=catfileheadercshex and not skipchecksum):
3267 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3268 return False;
3269 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3270 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
3271 seekto = fnumfiles - 1
3272 filefound = False;
3273 if(seekto>=0):
3274 il = -1;
3275 while(il < seekto):
3276 prefhstart = catfp.tell();
3277 if(formatspecs[7]):
3278 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3279 else:
3280 preheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3281 prefheadsize = int(preheaderdata[0], 16);
3282 preftype = int(preheaderdata[1], 16);
3283 if(re.findall("^[.|/]", preheaderdata[2])):
3284 prefname = preheaderdata[2];
3285 else:
3286 prefname = "./"+preheaderdata[2];
3287 prefbasedir = os.path.dirname(prefname);
3288 preflinkname = preheaderdata[3];
3289 prefsize = int(preheaderdata[4], 16);
3290 prefatime = int(preheaderdata[5], 16);
3291 prefmtime = int(preheaderdata[6], 16);
3292 prefctime = int(preheaderdata[7], 16);
3293 prefbtime = int(preheaderdata[8], 16);
3294 prefmode = int(preheaderdata[9], 16);
3295 prefchmode = stat.S_IMODE(prefmode);
3296 preftypemod = stat.S_IFMT(prefmode);
3297 prefuid = int(preheaderdata[10], 16);
3298 prefuname = preheaderdata[11];
3299 prefgid = int(preheaderdata[12], 16);
3300 prefgname = preheaderdata[13];
3301 fid = int(preheaderdata[14], 16);
3302 finode = int(preheaderdata[15], 16);
3303 flinkcount = int(preheaderdata[16], 16);
3304 prefdev_minor = int(preheaderdata[17], 16);
3305 prefdev_major = int(preheaderdata[18], 16);
3306 prefrdev_minor = int(preheaderdata[19], 16);
3307 prefrdev_major = int(preheaderdata[20], 16);
3308 prefextrasize = int(preheaderdata[21], 16);
3309 prefextrafields = int(preheaderdata[22], 16);
3310 extrafieldslist = [];
3311 if(formatspecs[7]):
3312 extrastart = 23;
3313 extraend = extrastart + prefextrafields;
3314 extrafieldslist = [];
3315 if(extrastart<extraend):
3316 extrafieldslist.append(preheaderdata[extrastart]);
3317 extrastart = extrastart + 1;
3318 prefchecksumtype = preheaderdata[extrastart].lower();
3319 prefcs = preheaderdata[extrastart + 1].lower();
3320 prefccs = preheaderdata[extrastart + 2].lower();
3321 else:
3322 extrafieldslist = [];
3323 if(formatspecs[7]):
3324 extrafieldslist = ReadFileHeaderData(catfp, prefextrafields, formatspecs[5]);
3325 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3326 prefchecksumtype = checksumsval[0].lower();
3327 prefcs = checksumsval[1].lower();
3328 prefccs = checksumsval[2].lower();
3329 hc = 0;
3330 if(formatspecs[7]):
3331 hcmax = len(preheaderdata) - 2;
3332 else:
3333 hcmax = len(preheaderdata);
3334 hout = "";
3335 while(hc<hcmax):
3336 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
3337 hc = hc + 1;
3338 prefnumfields = 24 + prefextrafields;
3339 if(prefchecksumtype=="none" or prefchecksumtype==""):
3340 prenewfcs = 0;
3341 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3342 prenewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3343 elif(prefchecksumtype=="adler32"):
3344 prenewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3345 elif(prefchecksumtype=="crc32"):
3346 prenewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3347 elif(prefchecksumtype=="crc64_ecma"):
3348 prenewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3349 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3350 prenewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3351 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3352 checksumoutstr = hashlib.new(prefchecksumtype);
3353 checksumoutstr.update(hout.encode('UTF-8'));
3354 prenewfcs = checksumoutstr.hexdigest().lower();
3355 if(prefcs!=prenewfcs and not skipchecksum):
3356 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
3357 return False;
3358 valid_archive = False;
3359 invalid_archive = True;
3360 prefhend = catfp.tell() - 1;
3361 prefcontentstart = catfp.tell();
3362 prefcontents = "";
3363 pyhascontents = False;
3364 if(prefsize>0):
3365 prefcontents = catfp.read(prefsize);
3366 if(prefchecksumtype=="none" or prefchecksumtype==""):
3367 prenewfccs = 0;
3368 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3369 prenewfccs = format(crc16(prefcontents) & 0xffff, '04x').lower();
3370 elif(prefchecksumtype=="crc16_ccitt"):
3371 prenewfcs = format(crc16_ccitt(prefcontents) & 0xffff, '04x').lower();
3372 elif(prefchecksumtype=="adler32"):
3373 prenewfccs = format(zlib.adler32(prefcontents) & 0xffffffff, '08x').lower();
3374 elif(prefchecksumtype=="crc32"):
3375 prenewfccs = format(crc32(prefcontents) & 0xffffffff, '08x').lower();
3376 elif(prefchecksumtype=="crc64_ecma"):
3377 prenewfcs = format(crc64_ecma(prefcontents) & 0xffffffffffffffff, '016x').lower();
3378 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3379 prenewfcs = format(crc64_iso(prefcontents) & 0xffffffffffffffff, '016x').lower();
3380 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3381 checksumoutstr = hashlib.new(prefchecksumtype);
3382 checksumoutstr.update(prefcontents);
3383 prenewfccs = checksumoutstr.hexdigest().lower();
3384 pyhascontents = True;
3385 if(prefccs!=prenewfccs and not skipchecksum):
3386 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
3387 return False;
3388 catfp.seek(1, 1);
3389 il = il + 1;
3390 filefound = False;
3391 prefname = preheaderdata[2];
3392 if(re.findall("^[.|/]", preheaderdata[2])):
3393 prefname = preheaderdata[2];
3394 else:
3395 prefname = "./"+preheaderdata[2];
3396 if(prefname==seekfile):
3397 filefound = True;
3398 break;
3399 catfp.seek(seekstart, 0);
3400 fileidnum = il;
3401 catfheadsize = int(preheaderdata[0], 16);
3402 catftype = int(preheaderdata[1], 16);
3403 if(re.findall("^[.|/]", preheaderdata[2])):
3404 catfname = preheaderdata[2];
3405 else:
3406 catfname = "./"+preheaderdata[2];
3407 catflinkname = preheaderdata[3];
3408 catfsize = int(preheaderdata[4], 16);
3409 catfbasedir = os.path.dirname(catfname);
3410 if(filefound):
3411 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
3412 else:
3413 return False;
3414 if(returnfp):
3415 catlist.update({'catfp': catfp});
3416 else:
3417 catfp.close();
3418 return catlist;
3420 create_alias_function("", __file_format_name__, "SeekToFileName", ArchiveFileSeekToFileName);
3422 def ArchiveFileValidate(infile, formatspecs=__file_format_list__, verbose=False, returnfp=False):
3423 if(verbose):
3424 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
3425 if(hasattr(infile, "read") or hasattr(infile, "write")):
3426 catfp = infile;
3427 catfp.seek(0, 0);
3428 catfp = UncompressArchiveFile(catfp, formatspecs);
3429 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
3430 if(checkcompressfile=="tarfile"):
3431 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3432 if(checkcompressfile=="zipfile"):
3433 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3434 if(rarfile_support and checkcompressfile=="rarfile"):
3435 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3436 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3437 return False;
3438 if(not catfp):
3439 return False;
3440 catfp.seek(0, 0);
3441 elif(infile=="-"):
3442 catfp = BytesIO();
3443 if(hasattr(sys.stdin, "buffer")):
3444 shutil.copyfileobj(sys.stdin.buffer, catfp);
3445 else:
3446 shutil.copyfileobj(sys.stdin, catfp);
3447 catfp.seek(0, 0);
3448 catfp = UncompressArchiveFile(catfp, formatspecs);
3449 if(not catfp):
3450 return False;
3451 catfp.seek(0, 0);
3452 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
3453 catfp = download_file_from_internet_file(infile);
3454 catfp = UncompressArchiveFile(catfp, formatspecs);
3455 catfp.seek(0, 0);
3456 if(not catfp):
3457 return False;
3458 catfp.seek(0, 0);
3459 else:
3460 infile = RemoveWindowsPath(infile);
3461 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
3462 if(checkcompressfile=="tarfile"):
3463 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3464 if(checkcompressfile=="zipfile"):
3465 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3466 if(rarfile_support and checkcompressfile=="rarfile"):
3467 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3468 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3469 return False;
3470 compresscheck = CheckCompressionType(infile, formatspecs, True);
3471 if(not compresscheck):
3472 fextname = os.path.splitext(infile)[1];
3473 if(fextname==".gz"):
3474 compresscheck = "gzip";
3475 elif(fextname==".bz2"):
3476 compresscheck = "bzip2";
3477 elif(fextname==".zst"):
3478 compresscheck = "zstd";
3479 elif(fextname==".lz4" or fextname==".clz4"):
3480 compresscheck = "lz4";
3481 elif(fextname==".lzo" or fextname==".lzop"):
3482 compresscheck = "lzo";
3483 elif(fextname==".lzma" or fextname==".xz"):
3484 compresscheck = "lzma";
3485 else:
3486 return False;
3487 if(not compresscheck):
3488 return False;
3489 catfp = UncompressFile(infile, formatspecs, "rb");
3491 try:
3492 catfp.seek(0, 2);
3493 except OSError:
3494 SeekToEndOfFile(catfp);
3495 except ValueError:
3496 SeekToEndOfFile(catfp);
3497 CatSize = catfp.tell();
3498 CatSizeEnd = CatSize;
3500 try:
3501 catfp.seek(0, 0);
3502 except OSError:
3503 return False;
3504 except ValueError:
3505 return False;
3506 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3507 catstring = catheader[0];
3508 catversion = re.findall(r"([\d]+)$", catstring);
3509 fprenumfiles = catheader[1];
3510 fnumfiles = int(fprenumfiles, 16);
3511 fprechecksumtype = catheader[2];
3512 fprechecksum = catheader[3];
3513 il = 0;
3514 fileheader = AppendNullByte(catstring, formatspecs[5]);
3515 fnumfileshex = format(int(fnumfiles), 'x').lower();
3516 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3517 if(fprechecksumtype=="none" or fprechecksumtype==""):
3518 catfileheadercshex = format(0, 'x').lower();
3519 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3520 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3521 elif(fprechecksumtype=="crc16_ccitt"):
3522 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3523 elif(fprechecksumtype=="adler32"):
3524 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3525 elif(fprechecksumtype=="crc32"):
3526 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3527 elif(fprechecksumtype=="crc64_ecma"):
3528 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3529 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3530 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3531 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3532 checksumoutstr = hashlib.new(fprechecksumtype);
3533 checksumoutstr.update(fileheader.encode('UTF-8'));
3534 catfileheadercshex = checksumoutstr.hexdigest().lower();
3535 else:
3536 catfileheadercshex = format(0, 'x').lower();
3537 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3538 valid_archive = True;
3539 invalid_archive = False;
3540 if(verbose):
3541 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(0));
3542 VerbosePrintOut(infile);
3543 if(fprechecksum==catfileheadercshex):
3544 if(verbose):
3545 VerbosePrintOut("File Header Checksum Passed");
3546 else:
3547 if(verbose):
3548 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3549 valid_archive = False;
3550 invalid_archive = True;
3551 while(il<fnumfiles):
3552 catfhstart = catfp.tell();
3553 if(formatspecs[7]):
3554 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3555 else:
3556 catheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3557 catfheadsize = int(catheaderdata[0], 16);
3558 catftype = int(catheaderdata[1], 16);
3559 if(re.findall("^[.|/]", catheaderdata[2])):
3560 catfname = catheaderdata[2];
3561 else:
3562 catfname = "./"+catheaderdata[2];
3563 catfbasedir = os.path.dirname(catfname);
3564 catflinkname = catheaderdata[3];
3565 catfsize = int(catheaderdata[4], 16);
3566 catfatime = int(catheaderdata[5], 16);
3567 catfmtime = int(catheaderdata[6], 16);
3568 catfctime = int(catheaderdata[7], 16);
3569 catfbtime = int(catheaderdata[8], 16);
3570 catfmode = int(catheaderdata[9], 16);
3571 catfchmode = stat.S_IMODE(catfmode);
3572 catftypemod = stat.S_IFMT(catfmode);
3573 catfuid = int(catheaderdata[10], 16);
3574 catfuname = catheaderdata[11];
3575 catfgid = int(catheaderdata[12], 16);
3576 catfgname = catheaderdata[13];
3577 fid = int(catheaderdata[14], 16);
3578 finode = int(catheaderdata[15], 16);
3579 flinkcount = int(catheaderdata[16], 16);
3580 catfdev_minor = int(catheaderdata[17], 16);
3581 catfdev_major = int(catheaderdata[18], 16);
3582 catfrdev_minor = int(catheaderdata[19], 16);
3583 catfrdev_major = int(catheaderdata[20], 16);
3584 catfextrasize = int(catheaderdata[21], 16);
3585 catfextrafields = int(catheaderdata[22], 16);
3586 extrafieldslist = [];
3587 if(formatspecs[7]):
3588 extrastart = 23;
3589 extraend = extrastart + catfextrafields;
3590 extrafieldslist = [];
3591 if(extrastart<extraend):
3592 extrafieldslist.append(catheaderdata[extrastart]);
3593 extrastart = extrastart + 1;
3594 catfchecksumtype = catheaderdata[extrastart].lower();
3595 catfcs = catheaderdata[extrastart + 1].lower();
3596 catfccs = catheaderdata[extrastart + 2].lower();
3597 else:
3598 extrafieldslist = [];
3599 if(formatspecs[7]):
3600 extrafieldslist = ReadFileHeaderData(catfp, catfextrafields, formatspecs[5]);
3601 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3602 catfchecksumtype = checksumsval[0].lower();
3603 catfcs = checksumsval[1].lower();
3604 catfccs = checksumsval[2].lower();
3605 hc = 0;
3606 if(formatspecs[7]):
3607 hcmax = len(catheaderdata) - 2;
3608 else:
3609 hcmax = len(catheaderdata);
3610 hout = "";
3611 while(hc<hcmax):
3612 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
3613 hc = hc + 1;
3614 catfnumfields = 24 + catfextrafields;
3615 if(catfchecksumtype=="none" or catfchecksumtype==""):
3616 catnewfcs = 0;
3617 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
3618 catnewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3619 elif(catfchecksumtype=="adler32"):
3620 catnewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3621 elif(catfchecksumtype=="crc32"):
3622 catnewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3623 elif(catfchecksumtype=="crc64_ecma"):
3624 catnewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3625 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
3626 catnewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3627 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
3628 checksumoutstr = hashlib.new(catfchecksumtype);
3629 checksumoutstr.update(hout.encode('UTF-8'));
3630 catnewfcs = checksumoutstr.hexdigest().lower();
3631 if(verbose):
3632 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(catfhstart));
3633 VerbosePrintOut(catfname);
3634 if(catfcs==catnewfcs):
3635 if(verbose):
3636 VerbosePrintOut("File Header Checksum Passed");
3637 else:
3638 if(verbose):
3639 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
3640 valid_archive = False;
3641 invalid_archive = True;
3642 catfhend = catfp.tell() - 1;
3643 catfcontentstart = catfp.tell();
3644 catfcontents = "";
3645 pyhascontents = False;
3646 if(catfsize>0):
3647 catfcontents = catfp.read(catfsize);
3648 if(catfchecksumtype=="none" or catfchecksumtype==""):
3649 catnewfccs = 0;
3650 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
3651 catnewfccs = format(crc16(catfcontents) & 0xffff, '04x').lower();
3652 elif(catfchecksumtype=="crc16_ccitt"):
3653 catnewfcs = format(crc16_ccitt(catfcontents) & 0xffff, '04x').lower();
3654 elif(catfchecksumtype=="adler32"):
3655 catnewfccs = format(zlib.adler32(catfcontents) & 0xffffffff, '08x').lower();
3656 elif(catfchecksumtype=="crc32"):
3657 catnewfccs = format(crc32(catfcontents) & 0xffffffff, '08x').lower();
3658 elif(catfchecksumtype=="crc64_ecma"):
3659 catnewfcs = format(crc64_ecma(catfcontents) & 0xffffffffffffffff, '016x').lower();
3660 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
3661 catnewfcs = format(crc64_iso(catfcontents) & 0xffffffffffffffff, '016x').lower();
3662 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
3663 checksumoutstr = hashlib.new(catfchecksumtype);
3664 checksumoutstr.update(catfcontents);
3665 catnewfccs = checksumoutstr.hexdigest().lower();
3666 pyhascontents = True;
3667 if(verbose):
3668 VerbosePrintOut("Checking File Content Checksum of file at offset " + str(catfcontentstart));
3669 VerbosePrintOut(catfname);
3670 if(catfccs==catnewfccs):
3671 if(verbose):
3672 VerbosePrintOut("File Content Checksum Passed");
3673 else:
3674 if(verbose):
3675 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfcontentstart));
3676 valid_archive = False;
3677 invalid_archive = True;
3678 if(verbose):
3679 VerbosePrintOut("");
3680 catfp.seek(1, 1);
3681 il = il + 1;
3682 if(valid_archive):
3683 if(returnfp):
3684 return catfp;
3685 else:
3686 catfp.close();
3687 return True;
3688 else:
3689 catfp.close();
3690 return False;
3692 create_alias_function("", __file_format_name__, "Validate", ArchiveFileValidate);
3694 def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3695 if(hasattr(infile, "read") or hasattr(infile, "write")):
3696 catfp = infile;
3697 catfp.seek(0, 0);
3698 catfp = UncompressArchiveFile(catfp, formatspecs);
3699 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
3700 if(checkcompressfile=="tarfile"):
3701 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3702 if(checkcompressfile=="zipfile"):
3703 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3704 if(rarfile_support and checkcompressfile=="rarfile"):
3705 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3706 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3707 return False;
3708 if(not catfp):
3709 return False;
3710 catfp.seek(0, 0);
3711 elif(infile=="-"):
3712 catfp = BytesIO();
3713 if(hasattr(sys.stdin, "buffer")):
3714 shutil.copyfileobj(sys.stdin.buffer, catfp);
3715 else:
3716 shutil.copyfileobj(sys.stdin, catfp);
3717 catfp.seek(0, 0);
3718 catfp = UncompressArchiveFile(catfp, formatspecs);
3719 if(not catfp):
3720 return False;
3721 catfp.seek(0, 0);
3722 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
3723 catfp = download_file_from_internet_file(infile);
3724 catfp = UncompressArchiveFile(catfp, formatspecs);
3725 catfp.seek(0, 0);
3726 if(not catfp):
3727 return False;
3728 catfp.seek(0, 0);
3729 else:
3730 infile = RemoveWindowsPath(infile);
3731 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
3732 if(checkcompressfile=="tarfile"):
3733 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3734 if(checkcompressfile=="zipfile"):
3735 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3736 if(rarfile_support and checkcompressfile=="rarfile"):
3737 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3738 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3739 return False;
3740 compresscheck = CheckCompressionType(infile, formatspecs, True);
3741 if(not compresscheck):
3742 fextname = os.path.splitext(infile)[1];
3743 if(fextname==".gz"):
3744 compresscheck = "gzip";
3745 elif(fextname==".bz2"):
3746 compresscheck = "bzip2";
3747 elif(fextname==".zst"):
3748 compresscheck = "zstd";
3749 elif(fextname==".lz4" or fextname==".clz4"):
3750 compresscheck = "lz4";
3751 elif(fextname==".lzo" or fextname==".lzop"):
3752 compresscheck = "lzo";
3753 elif(fextname==".lzma" or fextname==".xz"):
3754 compresscheck = "lzma";
3755 else:
3756 return False;
3757 if(not compresscheck):
3758 return False;
3759 catfp = UncompressFile(infile, formatspecs, "rb");
3761 try:
3762 catfp.seek(0, 2);
3763 except OSError:
3764 SeekToEndOfFile(catfp);
3765 except ValueError:
3766 SeekToEndOfFile(catfp);
3767 CatSize = catfp.tell();
3768 CatSizeEnd = CatSize;
3770 try:
3771 catfp.seek(0, 0);
3772 except OSError:
3773 return False;
3774 except ValueError:
3775 return False;
3776 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3777 catstring = catheader[0];
3778 catversion = re.findall(r"([\d]+)$", catstring);
3779 fprenumfiles = catheader[1];
3780 fnumfiles = int(fprenumfiles, 16);
3781 fprechecksumtype = catheader[2];
3782 fprechecksum = catheader[3];
3783 fileheader = AppendNullByte(catstring, formatspecs[5]);
3784 fnumfileshex = format(int(fnumfiles), 'x').lower();
3785 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3786 if(fprechecksumtype=="none" or fprechecksumtype==""):
3787 catfileheadercshex = format(0, 'x').lower();
3788 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3789 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3790 elif(fprechecksumtype=="crc16_ccitt"):
3791 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3792 elif(fprechecksumtype=="adler32"):
3793 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3794 elif(fprechecksumtype=="crc32"):
3795 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3796 elif(fprechecksumtype=="crc64_ecma"):
3797 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3798 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3799 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3800 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3801 checksumoutstr = hashlib.new(fprechecksumtype);
3802 checksumoutstr.update(fileheader.encode('UTF-8'));
3803 catfileheadercshex = checksumoutstr.hexdigest().lower();
3804 else:
3805 catfileheadercshex = format(0, 'x').lower();
3806 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3807 fheadtell = len(fileheader);
3808 if(fprechecksum!=catfileheadercshex and not skipchecksum):
3809 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3810 return False;
3811 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3812 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
3813 if(seekstart<0 and seekstart>fnumfiles):
3814 seekstart = 0;
3815 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
3816 seekend = fnumfiles;
3817 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
3818 seekend = fnumfiles - abs(seekend);
3819 if(seekstart>0):
3820 il = 0;
3821 while(il < seekstart):
3822 prefhstart = catfp.tell();
3823 if(formatspecs[7]):
3824 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3825 else:
3826 preheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3827 prefheadsize = int(preheaderdata[0], 16);
3828 preftype = int(preheaderdata[1], 16);
3829 if(re.findall("^[.|/]", preheaderdata[2])):
3830 prefname = preheaderdata[2];
3831 else:
3832 prefname = "./"+preheaderdata[2];
3833 prefbasedir = os.path.dirname(prefname);
3834 preflinkname = preheaderdata[3];
3835 prefsize = int(preheaderdata[4], 16);
3836 prefatime = int(preheaderdata[5], 16);
3837 prefmtime = int(preheaderdata[6], 16);
3838 prefctime = int(preheaderdata[7], 16);
3839 prefbtime = int(preheaderdata[8], 16);
3840 prefmode = int(preheaderdata[9], 16);
3841 prefchmode = stat.S_IMODE(prefmode);
3842 preftypemod = stat.S_IFMT(prefmode);
3843 prefuid = int(preheaderdata[10], 16);
3844 prefuname = preheaderdata[11];
3845 prefgid = int(preheaderdata[12], 16);
3846 prefgname = preheaderdata[13];
3847 fid = int(preheaderdata[14], 16);
3848 finode = int(preheaderdata[15], 16);
3849 flinkcount = int(preheaderdata[16], 16);
3850 prefdev_minor = int(preheaderdata[17], 16);
3851 prefdev_major = int(preheaderdata[18], 16);
3852 prefrdev_minor = int(preheaderdata[19], 16);
3853 prefrdev_major = int(preheaderdata[20], 16);
3854 prefextrasize = int(preheaderdata[21], 16);
3855 prefextrafields = int(preheaderdata[22], 16);
3856 extrafieldslist = [];
3857 if(formatspecs[7]):
3858 extrastart = 23;
3859 extraend = extrastart + prefextrafields;
3860 extrafieldslist = [];
3861 if(extrastart<extraend):
3862 extrafieldslist.append(preheaderdata[extrastart]);
3863 extrastart = extrastart + 1;
3864 prefchecksumtype = preheaderdata[extrastart].lower();
3865 prefcs = preheaderdata[extrastart + 1].lower();
3866 prefccs = preheaderdata[extrastart + 2].lower();
3867 else:
3868 extrafieldslist = [];
3869 if(formatspecs[7]):
3870 extrafieldslist = ReadFileHeaderData(catfp, prefextrafields, formatspecs[5]);
3871 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3872 prefchecksumtype = checksumsval[0].lower();
3873 prefcs = checksumsval[1].lower();
3874 prefccs = checksumsval[2].lower();
3875 hc = 0;
3876 if(formatspecs[7]):
3877 hcmax = len(preheaderdata) - 2;
3878 else:
3879 hcmax = len(preheaderdata);
3880 hout = "";
3881 while(hc<hcmax):
3882 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
3883 hc = hc + 1;
3884 prefnumfields = 24 + prefextrafields;
3885 if(prefchecksumtype=="none" or prefchecksumtype==""):
3886 prenewfcs = 0;
3887 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3888 prenewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3889 elif(prefchecksumtype=="adler32"):
3890 prenewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3891 elif(prefchecksumtype=="crc32"):
3892 prenewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3893 elif(prefchecksumtype=="crc64_ecma"):
3894 prenewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3895 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3896 prenewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3897 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3898 checksumoutstr = hashlib.new(prefchecksumtype);
3899 checksumoutstr.update(hout.encode('UTF-8'));
3900 prenewfcs = checksumoutstr.hexdigest().lower();
3901 if(prefcs!=prenewfcs and not skipchecksum):
3902 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
3903 return False;
3904 valid_archive = False;
3905 invalid_archive = True;
3906 prefhend = catfp.tell() - 1;
3907 prefcontentstart = catfp.tell();
3908 prefcontents = "";
3909 pyhascontents = False;
3910 if(prefsize>0):
3911 prefcontents = catfp.read(prefsize);
3912 if(prefchecksumtype=="none" or prefchecksumtype==""):
3913 prenewfccs = 0;
3914 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3915 prenewfccs = format(crc16(prefcontents) & 0xffff, '04x').lower();
3916 elif(prefchecksumtype=="crc16_ccitt"):
3917 prenewfcs = format(crc16_ccitt(prefcontents) & 0xffff, '04x').lower();
3918 elif(prefchecksumtype=="adler32"):
3919 prenewfccs = format(zlib.adler32(prefcontents) & 0xffffffff, '08x').lower();
3920 elif(prefchecksumtype=="crc32"):
3921 prenewfccs = format(crc32(prefcontents) & 0xffffffff, '08x').lower();
3922 elif(prefchecksumtype=="crc64_ecma"):
3923 prenewfcs = format(crc64_ecma(prefcontents) & 0xffffffffffffffff, '016x').lower();
3924 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3925 prenewfcs = format(crc64_iso(prefcontents) & 0xffffffffffffffff, '016x').lower();
3926 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3927 checksumoutstr = hashlib.new(prefchecksumtype);
3928 checksumoutstr.update(prefcontents);
3929 prenewfccs = checksumoutstr.hexdigest().lower();
3930 pyhascontents = True;
3931 if(prefccs!=prenewfccs and not skipchecksum):
3932 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
3933 return False;
3934 catfp.seek(1, 1);
3935 il = il + 1;
3936 fileidnum = seekstart;
3937 realidnum = 0;
3938 while(fileidnum<seekend):
3939 catfhstart = catfp.tell();
3940 if(formatspecs[7]):
3941 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3942 else:
3943 catheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3944 catfheadsize = int(catheaderdata[0], 16);
3945 catftype = int(catheaderdata[1], 16);
3946 if(re.findall("^[.|/]", catheaderdata[2])):
3947 catfname = catheaderdata[2];
3948 else:
3949 catfname = "./"+catheaderdata[2];
3950 catfbasedir = os.path.dirname(catfname);
3951 catflinkname = catheaderdata[3];
3952 catfsize = int(catheaderdata[4], 16);
3953 catfatime = int(catheaderdata[5], 16);
3954 catfmtime = int(catheaderdata[6], 16);
3955 catfctime = int(catheaderdata[7], 16);
3956 catfbtime = int(catheaderdata[8], 16);
3957 catfmode = int(catheaderdata[9], 16);
3958 catfchmode = stat.S_IMODE(catfmode);
3959 catftypemod = stat.S_IFMT(catfmode);
3960 catfuid = int(catheaderdata[10], 16);
3961 catfuname = catheaderdata[11];
3962 catfgid = int(catheaderdata[12], 16);
3963 catfgname = catheaderdata[13];
3964 fid = int(catheaderdata[14], 16);
3965 finode = int(catheaderdata[15], 16);
3966 flinkcount = int(catheaderdata[16], 16);
3967 catfdev_minor = int(catheaderdata[17], 16);
3968 catfdev_major = int(catheaderdata[18], 16);
3969 catfrdev_minor = int(catheaderdata[19], 16);
3970 catfrdev_major = int(catheaderdata[20], 16);
3971 catfextrasize = int(catheaderdata[21], 16);
3972 catfextrafields = int(catheaderdata[22], 16);
3973 extrafieldslist = [];
3974 if(formatspecs[7]):
3975 extrastart = 23;
3976 extraend = extrastart + catfextrafields;
3977 extrafieldslist = [];
3978 if(extrastart<extraend):
3979 extrafieldslist.append(catheaderdata[extrastart]);
3980 extrastart = extrastart + 1;
3981 catfchecksumtype = catheaderdata[extrastart].lower();
3982 catfcs = catheaderdata[extrastart + 1].lower();
3983 catfccs = catheaderdata[extrastart + 2].lower();
3984 else:
3985 extrafieldslist = [];
3986 if(formatspecs[7]):
3987 extrafieldslist = ReadFileHeaderData(catfp, catfextrafields, formatspecs[5]);
3988 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3989 catfchecksumtype = checksumsval[0].lower();
3990 catfcs = checksumsval[1].lower();
3991 catfccs = checksumsval[2].lower();
3992 hc = 0;
3993 if(formatspecs[7]):
3994 hcmax = len(catheaderdata) - 2;
3995 else:
3996 hcmax = len(catheaderdata);
3997 hout = "";
3998 while(hc<hcmax):
3999 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
4000 hc = hc + 1;
4001 catfnumfields = 24 + catfextrafields;
4002 if(catfchecksumtype=="none" or catfchecksumtype==""):
4003 catnewfcs = 0;
4004 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
4005 catnewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
4006 elif(catfchecksumtype=="adler32"):
4007 catnewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
4008 elif(catfchecksumtype=="crc32"):
4009 catnewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
4010 elif(catfchecksumtype=="crc64_ecma"):
4011 catnewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4012 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
4013 catnewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4014 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
4015 checksumoutstr = hashlib.new(catfchecksumtype);
4016 checksumoutstr.update(hout.encode('UTF-8'));
4017 catnewfcs = checksumoutstr.hexdigest().lower();
4018 if(catfcs!=catnewfcs and not skipchecksum):
4019 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
4020 return False;
4021 catfhend = catfp.tell() - 1;
4022 catfcontentstart = catfp.tell();
4023 catfcontents = "";
4024 pyhascontents = False;
4025 if(catfsize>0 and not listonly):
4026 catfcontents = catfp.read(catfsize);
4027 if(catfchecksumtype=="none" or catfchecksumtype==""):
4028 catnewfccs = 0;
4029 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
4030 catnewfccs = format(crc16(catfcontents) & 0xffff, '04x').lower();
4031 elif(catfchecksumtype=="crc16_ccitt"):
4032 catnewfcs = format(crc16_ccitt(catfcontents) & 0xffff, '04x').lower();
4033 elif(catfchecksumtype=="adler32"):
4034 catnewfccs = format(zlib.adler32(catfcontents) & 0xffffffff, '08x').lower();
4035 elif(catfchecksumtype=="crc32"):
4036 catnewfccs = format(crc32(catfcontents) & 0xffffffff, '08x').lower();
4037 elif(catfchecksumtype=="crc64_ecma"):
4038 catnewfcs = format(crc64_ecma(catfcontents) & 0xffffffffffffffff, '016x').lower();
4039 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
4040 catnewfcs = format(crc64_iso(catfcontents) & 0xffffffffffffffff, '016x').lower();
4041 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
4042 checksumoutstr = hashlib.new(catfchecksumtype);
4043 checksumoutstr.update(catfcontents);
4044 catnewfccs = checksumoutstr.hexdigest().lower();
4045 pyhascontents = True;
4046 if(catfccs!=catnewfccs and skipchecksum):
4047 VerbosePrintOut("File Content Checksum Error with file " + catfname + " at offset " + str(catfcontentstart));
4048 return False;
4049 if(catfsize>0 and listonly):
4050 catfp.seek(catfsize, 1);
4051 pyhascontents = False;
4052 catfp.seek(1, 1);
4053 catfcontentend = catfp.tell() - 1;
4054 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, '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, 'fextrafields': catfextrafields, 'fextrafieldsize': catfextrasize, 'fextralist': extrafieldslist, 'fheaderchecksum': catfcs, 'fcontentchecksum': catfccs, 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': catfcontents} });
4055 fileidnum = fileidnum + 1;
4056 realidnum = realidnum + 1;
4057 if(returnfp):
4058 catlist.update({'catfp': catfp});
4059 else:
4060 catfp.close();
4061 return catlist;
4063 create_alias_function("", __file_format_name__, "ToArray", ArchiveFileToArray);
4065 def ArchiveFileStringToArray(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4066 catfp = BytesIO(catstr);
4067 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4068 return listcatfiles;
4070 create_alias_function("", __file_format_name__, "StringToArray", ArchiveFileStringToArray);
4072 def TarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4073 catfp = BytesIO();
4074 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4075 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4076 return listcatfiles;
4078 def ZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4079 catfp = BytesIO();
4080 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4081 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4082 return listcatfiles;
4084 if(not rarfile_support):
4085 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4086 return False;
4088 if(rarfile_support):
4089 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4090 catfp = BytesIO();
4091 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4092 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4093 return listcatfiles;
4095 def ListDirToArrayAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4096 catver = formatspecs[6];
4097 fileheaderver = str(int(catver.replace(".", "")));
4098 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4099 advancedlist = True;
4100 infilelist = [];
4101 if(infiles=="-"):
4102 for line in sys.stdin:
4103 infilelist.append(line.strip());
4104 infilelist = list(filter(None, infilelist));
4105 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
4106 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
4107 return False;
4108 with open(infiles, "r") as finfile:
4109 for line in finfile:
4110 infilelist.append(line.strip());
4111 infilelist = list(filter(None, infilelist));
4112 else:
4113 if(isinstance(infiles, (list, tuple, ))):
4114 infilelist = list(filter(None, infiles));
4115 elif(isinstance(infiles, (str, ))):
4116 infilelist = list(filter(None, [infiles]));
4117 if(advancedlist):
4118 GetDirList = ListDirAdvanced(infilelist, followlink, False);
4119 else:
4120 GetDirList = ListDir(infilelist, followlink, False);
4121 if(not GetDirList):
4122 return False;
4123 curinode = 0;
4124 curfid = 0;
4125 inodelist = [];
4126 inodetofile = {};
4127 filetoinode = {};
4128 inodetocatinode = {};
4129 fileidnum = 0;
4130 fnumfiles = int(len(GetDirList));
4131 catver = formatspecs[6];
4132 fileheaderver = str(int(catver.replace(".", "")));
4133 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4134 fnumfileshex = format(int(fnumfiles), 'x').lower();
4135 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4136 catversion = re.findall(r"([\d]+)$", fileheader);
4137 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4138 if(checksumtype=="none" or checksumtype==""):
4139 catfileheadercshex = format(0, 'x').lower();
4140 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4141 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4142 elif(checksumtype=="crc16_ccitt"):
4143 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4144 elif(checksumtype=="adler32"):
4145 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4146 elif(checksumtype=="crc32"):
4147 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4148 elif(checksumtype=="crc64_ecma"):
4149 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4150 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4151 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4152 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4153 checksumoutstr = hashlib.new(checksumtype);
4154 checksumoutstr.update(fileheader.encode('UTF-8'));
4155 catfileheadercshex = checksumoutstr.hexdigest().lower();
4156 else:
4157 catfileheadercshex = format(0, 'x').lower();
4158 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4159 fheadtell = len(fileheader);
4160 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4161 for curfname in GetDirList:
4162 if(re.findall("^[.|/]", curfname)):
4163 fname = curfname;
4164 else:
4165 fname = "./"+curfname;
4166 if(verbose):
4167 VerbosePrintOut(fname);
4168 if(not followlink or followlink is None):
4169 fstatinfo = os.lstat(fname);
4170 else:
4171 fstatinfo = os.stat(fname);
4172 fpremode = fstatinfo.st_mode;
4173 finode = fstatinfo.st_ino;
4174 flinkcount = fstatinfo.st_nlink;
4175 ftype = 0;
4176 if(stat.S_ISREG(fpremode)):
4177 ftype = 0;
4178 elif(stat.S_ISLNK(fpremode)):
4179 ftype = 2;
4180 elif(stat.S_ISCHR(fpremode)):
4181 ftype = 3;
4182 elif(stat.S_ISBLK(fpremode)):
4183 ftype = 4;
4184 elif(stat.S_ISDIR(fpremode)):
4185 ftype = 5;
4186 elif(stat.S_ISFIFO(fpremode)):
4187 ftype = 6;
4188 elif(stat.S_ISSOCK(fpremode)):
4189 ftype = 8;
4190 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
4191 ftype = 9;
4192 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
4193 ftype = 10;
4194 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
4195 ftype = 11;
4196 else:
4197 ftype = 0;
4198 flinkname = "";
4199 fbasedir = os.path.dirname(fname);
4200 fcurfid = curfid;
4201 if(not followlink and finode!=0):
4202 if(ftype!=1):
4203 if(finode in inodelist):
4204 ftype = 1;
4205 flinkname = inodetofile[finode];
4206 fcurinode = inodetocatinode[finode];
4207 if(finode not in inodelist):
4208 inodelist.append(finode);
4209 inodetofile.update({finode: fname});
4210 inodetocatinode.update({finode: curinode});
4211 fcurinode = curinode;
4212 curinode = curinode + 1;
4213 else:
4214 fcurinode = curinode;
4215 curinode = curinode + 1;
4216 curfid = curfid + 1;
4217 if(ftype==2):
4218 flinkname = os.readlink(fname);
4219 fdev = fstatinfo.st_dev;
4220 getfdev = GetDevMajorMinor(fdev);
4221 fdev_minor = getfdev[0];
4222 fdev_major = getfdev[1];
4223 frdev = fstatinfo.st_dev;
4224 if(hasattr(fstatinfo, "st_rdev")):
4225 frdev = fstatinfo.st_rdev;
4226 else:
4227 frdev = fstatinfo.st_dev;
4228 getfrdev = GetDevMajorMinor(frdev);
4229 frdev_minor = getfrdev[0];
4230 frdev_major = getfrdev[1];
4231 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
4232 fsize = "0";
4233 if(ftype==0 or ftype==7):
4234 fsize = fstatinfo.st_size;
4235 fatime = fstatinfo.st_atime;
4236 fmtime = fstatinfo.st_mtime;
4237 fctime = fstatinfo.st_ctime;
4238 if(hasattr(fstatinfo, "st_birthtime")):
4239 fbtime = fstatinfo.st_birthtime;
4240 else:
4241 fbtime = fstatinfo.st_ctime;
4242 fmode = fstatinfo.st_mode;
4243 fchmode = stat.S_IMODE(fstatinfo.st_mode);
4244 ftypemod = stat.S_IFMT(fstatinfo.st_mode);
4245 fuid = fstatinfo.st_uid;
4246 fgid = fstatinfo.st_gid;
4247 funame = "";
4248 try:
4249 import pwd;
4250 try:
4251 userinfo = pwd.getpwuid(fstatinfo.st_uid);
4252 funame = userinfo.pw_name;
4253 except KeyError:
4254 funame = "";
4255 except ImportError:
4256 funame = "";
4257 fgname = "";
4258 try:
4259 import grp;
4260 try:
4261 groupinfo = grp.getgrgid(fstatinfo.st_gid);
4262 fgname = groupinfo.gr_name;
4263 except KeyError:
4264 fgname = "";
4265 except ImportError:
4266 fgname = "";
4267 fdev_minor = fdev_minor;
4268 fdev_major = fdev_major;
4269 frdev_minor = frdev_minor;
4270 frdev_major = frdev_major;
4271 finode = finode;
4272 flinkcount = flinkcount;
4273 if(hasattr(fstatinfo, "st_file_attributes")):
4274 fwinattributes = fstatinfo.st_file_attributes;
4275 else:
4276 fwinattributes = 0;
4277 fcontents = "".encode('UTF-8');
4278 chunk_size = 1024;
4279 if(ftype == 0 or ftype == 7):
4280 with open(fname, "rb") as fpc:
4281 while(True):
4282 chunk = fpc.read(chunk_size);
4283 if(not chunk):
4284 break
4285 fcontents += chunk;
4286 if(followlink and (ftype == 1 or ftype == 2)):
4287 flstatinfo = os.stat(flinkname);
4288 with open(flinkname, "rb") as fpc:
4289 while(True):
4290 chunk = fpc.read(chunk_size);
4291 if(not chunk):
4292 break;
4293 fcontents += chunk;
4294 ftypehex = format(ftype, 'x').lower();
4295 extrafields = len(extradata);
4296 extrafieldslist = extradata;
4297 catfextrafields = extrafields;
4298 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
4299 if(len(extradata)>0):
4300 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
4301 extrasizelen = len(extrasizestr);
4302 extrasizelenhex = format(extrasizelen, 'x').lower();
4303 catfileoutstr = AppendNullBytes([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(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()], formatspecs[5]);
4304 if(len(extradata)>0):
4305 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
4306 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
4307 catfnumfields = 24 + catfextrafields;
4308 if(checksumtype=="none" or checksumtype==""):
4309 catfileheadercshex = format(0, 'x').lower();
4310 catfilecontentcshex = format(0, 'x').lower();
4311 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4312 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4313 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4314 elif(checksumtype=="crc16_ccitt"):
4315 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4316 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4317 elif(checksumtype=="adler32"):
4318 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4319 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4320 elif(checksumtype=="crc32"):
4321 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4322 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4323 elif(checksumtype=="crc64_ecma"):
4324 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4325 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4326 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4327 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4328 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4329 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4330 checksumoutstr = hashlib.new(checksumtype);
4331 checksumoutstr.update("".encode('UTF-8'));
4332 catfileheadercshex = checksumoutstr.hexdigest().lower();
4333 checksumoutstr = hashlib.new(checksumtype);
4334 checksumoutstr.update(fcontents);
4335 catfilecontentcshex = checksumoutstr.hexdigest().lower();
4336 else:
4337 catfileheadercshex = format(0, 'x').lower();
4338 catfilecontentcshex = format(0, 'x').lower();
4339 catfhstart = fheadtell;
4340 fheadtell += len(catfileoutstr);
4341 catfhend = fheadtell - 1;
4342 catfcontentstart = fheadtell;
4343 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4344 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4345 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
4346 if(checksumtype=="none" or checksumtype==""):
4347 catfileheadercshex = format(0, 'x').lower();
4348 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4349 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4350 elif(checksumtype=="crc16_ccitt"):
4351 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4352 elif(checksumtype=="adler32"):
4353 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4354 elif(checksumtype=="crc32"):
4355 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4356 elif(checksumtype=="crc64_ecma"):
4357 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4358 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4359 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4360 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4361 checksumoutstr = hashlib.new(checksumtype);
4362 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4363 catfileheadercshex = checksumoutstr.hexdigest().lower();
4364 else:
4365 catfileheadercshex = format(0, 'x').lower();
4366 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4367 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4368 nullstrecd = formatspecs[5].encode('UTF-8');
4369 fheadtell += len(catfileoutstr) + 1;
4370 catfcontentend = fheadtell - 1;
4371 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4372 pyhascontents = False;
4373 if(int(fsize)>0 and not listonly):
4374 pyhascontents = True;
4375 if(int(fsize)>0 and listonly):
4376 fcontents = "";
4377 pyhascontents = False;
4378 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, '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, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4379 fileidnum = fileidnum + 1;
4380 return catlist;
4382 def TarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4383 curinode = 0;
4384 curfid = 0;
4385 inodelist = [];
4386 inodetofile = {};
4387 filetoinode = {};
4388 inodetocatinode = {};
4389 fileidnum = 0;
4390 if(infile=="-"):
4391 infile = BytesIO();
4392 if(hasattr(sys.stdin, "buffer")):
4393 shutil.copyfileobj(sys.stdin.buffer, infile);
4394 else:
4395 shutil.copyfileobj(sys.stdin, infile);
4396 infile.seek(0, 0);
4397 if(not infile):
4398 return False;
4399 infile.seek(0, 0);
4400 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
4401 infile = download_file_from_internet_file(infile);
4402 infile.seek(0, 0);
4403 if(not infile):
4404 return False;
4405 infile.seek(0, 0);
4406 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
4407 return False;
4408 elif(os.path.exists(infile) and os.path.isfile(infile)):
4409 try:
4410 if(not tarfile.is_tarfile(infile)):
4411 return False;
4412 except AttributeError:
4413 if(not is_tarfile(infile)):
4414 return False;
4415 else:
4416 return False;
4417 try:
4418 if(hasattr(infile, "read") or hasattr(infile, "write")):
4419 tarfp = tarfile.open(fileobj=infile, mode="r");
4420 else:
4421 tarfp = tarfile.open(infile, "r");
4422 except FileNotFoundError:
4423 return False;
4424 fnumfiles = int(len(tarfp.getmembers()));
4425 catver = formatspecs[6];
4426 fileheaderver = str(int(catver.replace(".", "")));
4427 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4428 fnumfileshex = format(int(fnumfiles), 'x').lower();
4429 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4430 catversion = re.findall(r"([\d]+)$", fileheader);
4431 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4432 if(checksumtype=="none" or checksumtype==""):
4433 catfileheadercshex = format(0, 'x').lower();
4434 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4435 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4436 elif(checksumtype=="crc16_ccitt"):
4437 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4438 elif(checksumtype=="adler32"):
4439 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4440 elif(checksumtype=="crc32"):
4441 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4442 elif(checksumtype=="crc64_ecma"):
4443 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4444 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4445 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4446 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4447 checksumoutstr = hashlib.new(checksumtype);
4448 checksumoutstr.update(fileheader.encode('UTF-8'));
4449 catfileheadercshex = checksumoutstr.hexdigest().lower();
4450 else:
4451 catfileheadercshex = format(0, 'x').lower();
4452 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4453 fheadtell = len(fileheader);
4454 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4455 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
4456 if(re.findall("^[.|/]", member.name)):
4457 fname = member.name;
4458 else:
4459 fname = "./"+member.name;
4460 if(verbose):
4461 VerbosePrintOut(fname);
4462 fpremode = member.mode;
4463 ffullmode = member.mode;
4464 flinkcount = 0;
4465 ftype = 0;
4466 if(member.isreg()):
4467 ffullmode = member.mode + stat.S_IFREG;
4468 ftype = 0;
4469 elif(member.isdev()):
4470 ffullmode = member.mode;
4471 ftype = 7;
4472 elif(member.islnk()):
4473 ffullmode = member.mode + stat.S_IFREG;
4474 ftype = 1;
4475 elif(member.issym()):
4476 ffullmode = member.mode + stat.S_IFLNK;
4477 ftype = 2;
4478 elif(member.ischr()):
4479 ffullmode = member.mode + stat.S_IFCHR;
4480 ftype = 3;
4481 elif(member.isblk()):
4482 ffullmode = member.mode + stat.S_IFBLK;
4483 ftype = 4;
4484 elif(member.isdir()):
4485 ffullmode = member.mode + stat.S_IFDIR;
4486 ftype = 5;
4487 elif(member.isfifo()):
4488 ffullmode = member.mode + stat.S_IFIFO;
4489 ftype = 6;
4490 elif(member.issparse()):
4491 ffullmode = member.mode;
4492 ftype = 12;
4493 else:
4494 ffullmode = member.mode;
4495 ftype = 0;
4496 flinkname = "";
4497 fbasedir = os.path.dirname(fname);
4498 fcurfid = curfid;
4499 fcurinode = 0;
4500 finode = fcurinode;
4501 curfid = curfid + 1;
4502 if(ftype==2):
4503 flinkname = member.linkname;
4504 fdev_minor = member.devminor;
4505 fdev_major = member.devmajor;
4506 frdev_minor = member.devminor;
4507 frdev_major = member.devmajor;
4508 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
4509 fsize = "0";
4510 elif(ftype==0 or ftype==7):
4511 fsize = member.size;
4512 else:
4513 fsize = member.size;
4514 fatime = member.mtime;
4515 fmtime = member.mtime;
4516 fctime = member.mtime;
4517 fbtime = member.mtime;
4518 fmode = ffullmode;
4519 fchmode = stat.S_IMODE(ffullmode);
4520 ftypemod = stat.S_IFMT(ffullmode);
4521 fuid = member.uid;
4522 fgid = member.gid;
4523 funame = member.uname;
4524 fgname = member.gname;
4525 flinkcount = flinkcount;
4526 fcontents = "".encode('UTF-8');
4527 chunk_size = 1024;
4528 if(ftype == 0 or ftype == 7):
4529 with tarfp.extractfile(member) as fpc:
4530 while(True):
4531 chunk = fpc.read(chunk_size);
4532 if(not chunk):
4533 break
4534 fcontents += chunk;
4535 ftypehex = format(ftype, 'x').lower();
4536 extrafields = len(extradata);
4537 extrafieldslist = extradata;
4538 catfextrafields = extrafields;
4539 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
4540 if(len(extradata)>0):
4541 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
4542 extrasizelen = len(extrasizestr);
4543 extrasizelenhex = format(extrasizelen, 'x').lower();
4544 catfileoutstr = AppendNullBytes([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(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()], formatspecs[5]);
4545 if(len(extradata)>0):
4546 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
4547 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
4548 catfnumfields = 24 + catfextrafields;
4549 if(checksumtype=="none" or checksumtype==""):
4550 catfileheadercshex = format(0, 'x').lower();
4551 catfilecontentcshex = format(0, 'x').lower();
4552 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4553 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4554 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4555 elif(checksumtype=="crc16_ccitt"):
4556 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4557 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4558 elif(checksumtype=="adler32"):
4559 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4560 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4561 elif(checksumtype=="crc32"):
4562 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4563 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4564 elif(checksumtype=="crc64_ecma"):
4565 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4566 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4567 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4568 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4569 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4570 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4571 checksumoutstr = hashlib.new(checksumtype);
4572 checksumoutstr.update("".encode('UTF-8'));
4573 catfileheadercshex = checksumoutstr.hexdigest().lower();
4574 checksumoutstr = hashlib.new(checksumtype);
4575 checksumoutstr.update(fcontents);
4576 catfilecontentcshex = checksumoutstr.hexdigest().lower();
4577 else:
4578 catfileheadercshex = format(0, 'x').lower();
4579 catfilecontentcshex = format(0, 'x').lower();
4580 catfhstart = fheadtell;
4581 fheadtell += len(catfileoutstr);
4582 catfhend = fheadtell - 1;
4583 catfcontentstart = fheadtell;
4584 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4585 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4586 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
4587 if(checksumtype=="none" or checksumtype==""):
4588 catfileheadercshex = format(0, 'x').lower();
4589 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4590 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4591 elif(checksumtype=="crc16_ccitt"):
4592 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4593 elif(checksumtype=="adler32"):
4594 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4595 elif(checksumtype=="crc32"):
4596 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4597 elif(checksumtype=="crc64_ecma"):
4598 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4599 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4600 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4601 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4602 checksumoutstr = hashlib.new(checksumtype);
4603 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4604 catfileheadercshex = checksumoutstr.hexdigest().lower();
4605 else:
4606 catfileheadercshex = format(0, 'x').lower();
4607 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4608 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4609 nullstrecd = formatspecs[5].encode('UTF-8');
4610 fheadtell += len(catfileoutstr) + 1;
4611 catfcontentend = fheadtell - 1;
4612 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4613 pyhascontents = False;
4614 if(int(fsize)>0 and not listonly):
4615 pyhascontents = True;
4616 if(int(fsize)>0 and listonly):
4617 fcontents = "";
4618 pyhascontents = False;
4619 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, '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, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4620 fileidnum = fileidnum + 1;
4621 return catlist;
4623 def ZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4624 advancedlist = True;
4625 curinode = 0;
4626 curfid = 0;
4627 inodelist = [];
4628 inodetofile = {};
4629 filetoinode = {};
4630 inodetocatinode = {};
4631 fileidnum = 0;
4632 if(infile=="-"):
4633 infile = BytesIO();
4634 if(hasattr(sys.stdin, "buffer")):
4635 shutil.copyfileobj(sys.stdin.buffer, infile);
4636 else:
4637 shutil.copyfileobj(sys.stdin, infile);
4638 infile.seek(0, 0);
4639 if(not infile):
4640 return False;
4641 infile.seek(0, 0);
4642 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
4643 infile = download_file_from_internet_file(infile);
4644 infile.seek(0, 0);
4645 if(not infile):
4646 return False;
4647 infile.seek(0, 0);
4648 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
4649 return False;
4650 else:
4651 return False;
4652 if(not zipfile.is_zipfile(infile)):
4653 return False;
4654 try:
4655 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
4656 except FileNotFoundError:
4657 return False;
4658 ziptest = zipfp.testzip();
4659 if(ziptest):
4660 VerbosePrintOut("Bad file found!");
4661 fnumfiles = int(len(zipfp.infolist()));
4662 catver = formatspecs[6];
4663 fileheaderver = str(int(catver.replace(".", "")));
4664 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4665 catversion = re.findall(r"([\d]+)$", fileheader);
4666 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4667 fnumfileshex = format(int(fnumfiles), 'x').lower();
4668 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4669 if(checksumtype=="none" or checksumtype==""):
4670 catfileheadercshex = format(0, 'x').lower();
4671 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4672 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4673 elif(checksumtype=="crc16_ccitt"):
4674 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4675 elif(checksumtype=="adler32"):
4676 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4677 elif(checksumtype=="crc32"):
4678 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4679 elif(checksumtype=="crc64_ecma"):
4680 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4681 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4682 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4683 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4684 checksumoutstr = hashlib.new(checksumtype);
4685 checksumoutstr.update(fileheader.encode('UTF-8'));
4686 catfileheadercshex = checksumoutstr.hexdigest().lower();
4687 else:
4688 catfileheadercshex = format(0, 'x').lower();
4689 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4690 fheadtell = len(fileheader);
4691 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4692 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
4693 if(re.findall("^[.|/]", member.filename)):
4694 fname = member.filename;
4695 else:
4696 fname = "./"+member.filename;
4697 zipinfo = zipfp.getinfo(member.filename);
4698 if(verbose):
4699 VerbosePrintOut(fname);
4700 if(not member.is_dir()):
4701 fpremode = stat.S_IFREG + 438;
4702 elif(member.is_dir()):
4703 fpremode = stat.S_IFDIR + 511;
4704 flinkcount = 0;
4705 ftype = 0;
4706 if(not member.is_dir()):
4707 ftype = 0;
4708 elif(member.is_dir()):
4709 ftype = 5;
4710 flinkname = "";
4711 fbasedir = os.path.dirname(fname);
4712 fcurfid = curfid;
4713 fcurinode = 0;
4714 finode = fcurinode;
4715 curfid = curfid + 1;
4716 fdev_minor = 0;
4717 fdev_major = 0;
4718 frdev_minor = 0;
4719 frdev_major = 0;
4720 if(ftype==5):
4721 fsize = "0";
4722 elif(ftype==0):
4723 fsize = member.file_size;
4724 else:
4725 fsize = member.file_size;
4726 fatime = time.mktime(member.date_time + (0, 0, -1));
4727 fmtime = time.mktime(member.date_time + (0, 0, -1));
4728 fctime = time.mktime(member.date_time + (0, 0, -1));
4729 fbtime = time.mktime(member.date_time + (0, 0, -1));
4730 if(not member.is_dir()):
4731 fmode = stat.S_IFREG + 438;
4732 fchmode = stat.S_IMODE(int(stat.S_IFREG + 438));
4733 ftypemod = stat.S_IFMT(int(stat.S_IFREG + 438));
4734 if(member.is_dir()):
4735 fmode = stat.S_IFDIR + 511;
4736 fchmode = stat.S_IMODE(int(stat.S_IFDIR + 511));
4737 ftypemod = stat.S_IFMT(int(stat.S_IFDIR + 511));
4738 try:
4739 fuid = os.getuid();
4740 except AttributeError:
4741 fuid = 0;
4742 except KeyError:
4743 fuid = 0;
4744 try:
4745 fgid = os.getgid();
4746 except AttributeError:
4747 fgid = 0;
4748 except KeyError:
4749 fgid = 0;
4750 try:
4751 import pwd;
4752 try:
4753 userinfo = pwd.getpwuid(os.getuid());
4754 funame = userinfo.pw_name;
4755 except KeyError:
4756 funame = "";
4757 except AttributeError:
4758 funame = "";
4759 except ImportError:
4760 funame = "";
4761 fgname = "";
4762 try:
4763 import grp;
4764 try:
4765 groupinfo = grp.getgrgid(os.getgid());
4766 fgname = groupinfo.gr_name;
4767 except KeyError:
4768 fgname = "";
4769 except AttributeError:
4770 fgname = "";
4771 except ImportError:
4772 fgname = "";
4773 fcontents = "".encode('UTF-8');
4774 if(ftype==0):
4775 fcontents = zipfp.read(member.filename);
4776 ftypehex = format(ftype, 'x').lower();
4777 extrafields = len(extradata);
4778 extrafieldslist = extradata;
4779 catfextrafields = extrafields;
4780 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
4781 if(len(extradata)>0):
4782 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
4783 extrasizelen = len(extrasizestr);
4784 extrasizelenhex = format(extrasizelen, 'x').lower();
4785 catfileoutstr = AppendNullBytes([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(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()], formatspecs[5]);
4786 if(len(extradata)>0):
4787 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
4788 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
4789 catfnumfields = 24 + catfextrafields;
4790 if(checksumtype=="none" or checksumtype==""):
4791 catfileheadercshex = format(0, 'x').lower();
4792 catfilecontentcshex = format(0, 'x').lower();
4793 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4794 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4795 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4796 elif(checksumtype=="crc16_ccitt"):
4797 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4798 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4799 elif(checksumtype=="adler32"):
4800 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4801 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4802 elif(checksumtype=="crc32"):
4803 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4804 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4805 elif(checksumtype=="crc64_ecma"):
4806 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4807 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4808 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4809 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4810 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4811 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4812 checksumoutstr = hashlib.new(checksumtype);
4813 checksumoutstr.update("".encode('UTF-8'));
4814 catfileheadercshex = checksumoutstr.hexdigest().lower();
4815 checksumoutstr = hashlib.new(checksumtype);
4816 checksumoutstr.update(fcontents);
4817 catfilecontentcshex = checksumoutstr.hexdigest().lower();
4818 else:
4819 catfileheadercshex = format(0, 'x').lower();
4820 catfilecontentcshex = format(0, 'x').lower();
4821 catfhstart = fheadtell;
4822 fheadtell += len(catfileoutstr);
4823 catfhend = fheadtell - 1;
4824 catfcontentstart = fheadtell;
4825 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4826 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4827 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
4828 if(checksumtype=="none" or checksumtype==""):
4829 catfileheadercshex = format(0, 'x').lower();
4830 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4831 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4832 elif(checksumtype=="crc16_ccitt"):
4833 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4834 elif(checksumtype=="adler32"):
4835 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4836 elif(checksumtype=="crc32"):
4837 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4838 elif(checksumtype=="crc64_ecma"):
4839 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4840 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4841 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4842 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4843 checksumoutstr = hashlib.new(checksumtype);
4844 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4845 catfileheadercshex = checksumoutstr.hexdigest().lower();
4846 else:
4847 catfileheadercshex = format(0, 'x').lower();
4848 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4849 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4850 nullstrecd = formatspecs[5].encode('UTF-8');
4851 fheadtell += len(catfileoutstr) + 1;
4852 catfcontentend = fheadtell - 1;
4853 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4854 pyhascontents = False;
4855 if(int(fsize)>0 and not listonly):
4856 pyhascontents = True;
4857 if(int(fsize)>0 and listonly):
4858 fcontents = "";
4859 pyhascontents = False;
4860 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, '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, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4861 fileidnum = fileidnum + 1;
4862 return catlist;
4864 if(not rarfile_support):
4865 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4866 return False;
4868 if(rarfile_support):
4869 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4870 advancedlist = True;
4871 curinode = 0;
4872 curfid = 0;
4873 inodelist = [];
4874 inodetofile = {};
4875 filetoinode = {};
4876 inodetocatinode = {};
4877 fileidnum = 0;
4878 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
4879 return False;
4880 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
4881 return False;
4882 rarfp = rarfile.RarFile(infile, "r");
4883 rartest = rarfp.testrar();
4884 if(rartest):
4885 VerbosePrintOut("Bad file found!");
4886 fnumfiles = int(len(rarfp.infolist()));
4887 catver = formatspecs[6];
4888 fileheaderver = str(int(catver.replace(".", "")));
4889 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4890 catversion = re.findall(r"([\d]+)$", fileheader);
4891 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4892 fnumfileshex = format(int(fnumfiles), 'x').lower();
4893 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4894 if(checksumtype=="none" or checksumtype==""):
4895 catfileheadercshex = format(0, 'x').lower();
4896 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4897 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4898 elif(checksumtype=="crc16_ccitt"):
4899 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4900 elif(checksumtype=="adler32"):
4901 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4902 elif(checksumtype=="crc32"):
4903 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4904 elif(checksumtype=="crc64_ecma"):
4905 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4906 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4907 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4908 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4909 checksumoutstr = hashlib.new(checksumtype);
4910 checksumoutstr.update(fileheader.encode('UTF-8'));
4911 catfileheadercshex = checksumoutstr.hexdigest().lower();
4912 else:
4913 catfileheadercshex = format(0, 'x').lower();
4914 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4915 fheadtell = len(fileheader);
4916 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4917 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
4918 is_unix = False;
4919 is_windows = False;
4920 if(member.host_os == rarfile.RAR_OS_UNIX):
4921 is_windows = False;
4922 try:
4923 member.external_attr
4924 is_unix = True;
4925 except AttributeError:
4926 is_unix = False;
4927 elif(member.host_os == rarfile.RAR_OS_WIN32):
4928 is_unix = False;
4929 try:
4930 member.external_attr
4931 is_windows = True;
4932 except AttributeError:
4933 is_windows = False;
4934 else:
4935 is_unix = False;
4936 is_windows = False;
4937 if(re.findall("^[.|/]", member.filename)):
4938 fname = member.filename;
4939 else:
4940 fname = "./"+member.filename;
4941 rarinfo = rarfp.getinfo(member.filename);
4942 if(verbose):
4943 VerbosePrintOut(fname);
4944 if(is_unix and member.external_attr !=0):
4945 fpremode = int(member.external_attr);
4946 elif(member.is_file()):
4947 fpremode = stat.S_IFREG + 438;
4948 elif(member.is_symlink()):
4949 fpremode = stat.S_IFLNK + 438;
4950 elif(member.is_dir()):
4951 fpremode = stat.S_IFDIR + 511;
4952 if(is_windows and member.external_attr !=0):
4953 fwinattributes = int(member.external_attr);
4954 else:
4955 fwinattributes = int(0);
4956 flinkcount = 0;
4957 ftype = 0;
4958 if(member.is_file()):
4959 ftype = 0;
4960 elif(member.is_symlink()):
4961 ftype = 2;
4962 elif(member.is_dir()):
4963 ftype = 5;
4964 flinkname = "";
4965 if(ftype==2):
4966 flinkname = rarfp.read(member.filename).decode("UTF-8");
4967 fbasedir = os.path.dirname(fname);
4968 fcurfid = curfid;
4969 fcurinode = 0;
4970 finode = fcurinode;
4971 curfid = curfid + 1;
4972 fdev_minor = 0;
4973 fdev_major = 0;
4974 frdev_minor = 0;
4975 frdev_major = 0;
4976 if(ftype==5):
4977 fsize = "0";
4978 if(ftype==0):
4979 fsize = member.file_size;
4980 try:
4981 if(member.atime):
4982 fatime = int(member.atime.timestamp());
4983 else:
4984 fatime = int(member.mtime.timestamp());
4985 except AttributeError:
4986 fatime = int(member.mtime.timestamp());
4987 fmtime = int(member.mtime.timestamp());
4988 try:
4989 if(member.ctime):
4990 fctime = int(member.ctime.timestamp());
4991 else:
4992 fctime = int(member.mtime.timestamp());
4993 except AttributeError:
4994 fctime = int(member.mtime.timestamp());
4995 fbtime = int(member.mtime.timestamp());
4996 if(is_unix and member.external_attr !=0):
4997 fmode = format(int(member.external_attr), 'x').lower();
4998 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
4999 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
5000 elif(member.is_file()):
5001 fmode = int(stat.S_IFREG + 438)
5002 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
5003 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
5004 elif(member.is_symlink()):
5005 fmode = int(stat.S_IFLNK + 438)
5006 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
5007 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
5008 elif(member.is_dir()):
5009 fmode = int(stat.S_IFDIR + 511)
5010 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
5011 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
5012 try:
5013 fuid = os.getuid();
5014 except AttributeError:
5015 fuid = 0;
5016 except KeyError:
5017 fuid = 0;
5018 try:
5019 fgid = os.getgid();
5020 except AttributeError:
5021 fgid = 0;
5022 except KeyError:
5023 fgid = 0;
5024 try:
5025 import pwd;
5026 try:
5027 userinfo = pwd.getpwuid(os.getuid());
5028 funame = userinfo.pw_name;
5029 except KeyError:
5030 funame = "";
5031 except AttributeError:
5032 funame = "";
5033 except ImportError:
5034 funame = "";
5035 fgname = "";
5036 try:
5037 import grp;
5038 try:
5039 groupinfo = grp.getgrgid(os.getgid());
5040 fgname = groupinfo.gr_name;
5041 except KeyError:
5042 fgname = "";
5043 except AttributeError:
5044 fgname = "";
5045 except ImportError:
5046 fgname = "";
5047 fcontents = "".encode('UTF-8');
5048 if(ftype==0):
5049 fcontents = rarfp.read(member.filename);
5050 ftypehex = format(ftype, 'x').lower();
5051 extrafields = len(extradata);
5052 extrafieldslist = extradata;
5053 catfextrafields = extrafields;
5054 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5055 if(len(extradata)>0):
5056 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5057 extrasizelen = len(extrasizestr);
5058 extrasizelenhex = format(extrasizelen, 'x').lower();
5059 catfileoutstr = AppendNullBytes([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(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()], formatspecs[5]);
5060 if(len(extradata)>0):
5061 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
5062 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
5063 catfnumfields = 24 + catfextrafields;
5064 if(checksumtype=="none" or checksumtype==""):
5065 catfileheadercshex = format(0, 'x').lower();
5066 catfilecontentcshex = format(0, 'x').lower();
5067 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5068 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5069 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5070 elif(checksumtype=="crc16_ccitt"):
5071 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5072 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5073 elif(checksumtype=="adler32"):
5074 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5075 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5076 elif(checksumtype=="crc32"):
5077 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5078 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5079 elif(checksumtype=="crc64_ecma"):
5080 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5081 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5082 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5083 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5084 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5085 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5086 checksumoutstr = hashlib.new(checksumtype);
5087 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5088 catfileheadercshex = checksumoutstr.hexdigest().lower();
5089 else:
5090 catfileheadercshex = format(0, 'x').lower();
5091 catfilecontentcshex = format(0, 'x').lower();
5092 catfhstart = fheadtell;
5093 fheadtell += len(catfileoutstr);
5094 catfhend = fheadtell - 1;
5095 catfcontentstart = fheadtell;
5096 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5097 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5098 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5099 if(checksumtype=="none" or checksumtype==""):
5100 catfileheadercshex = format(0, 'x').lower();
5101 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5102 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5103 elif(checksumtype=="crc16_ccitt"):
5104 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5105 elif(checksumtype=="adler32"):
5106 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5107 elif(checksumtype=="crc32"):
5108 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5109 elif(checksumtype=="crc64_ecma"):
5110 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5111 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5112 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5113 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5114 checksumoutstr = hashlib.new(checksumtype);
5115 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5116 catfileheadercshex = checksumoutstr.hexdigest().lower();
5117 else:
5118 catfileheadercshex = format(0, 'x').lower();
5119 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5120 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5121 nullstrecd = formatspecs[5].encode('UTF-8');
5122 fheadtell += len(catfileoutstr) + 1;
5123 catfcontentend = fheadtell - 1;
5124 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5125 pyhascontents = False;
5126 if(int(fsize)>0 and not listonly):
5127 pyhascontents = True;
5128 if(int(fsize)>0 and listonly):
5129 fcontents = "";
5130 pyhascontents = False;
5131 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, '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, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
5132 fileidnum = fileidnum + 1;
5133 return catlist;
5135 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):
5136 outarray = BytesIO();
5137 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
5138 listcatfiles = ArchiveFileToArray(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5139 return listcatfiles;
5141 def ArchiveFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5142 if(isinstance(infile, dict)):
5143 listcatfiles = infile;
5144 else:
5145 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5146 infile = RemoveWindowsPath(infile);
5147 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5148 if(not listcatfiles):
5149 return False;
5150 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': {}}}};
5151 if(returnfp):
5152 catarray.update({'catfp': listcatfiles['catfp']});
5153 lenlist = len(listcatfiles['ffilelist']);
5154 if(seekstart>0):
5155 lcfi = seekstart;
5156 else:
5157 lcfi = 0;
5158 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5159 lcfx = seekend;
5160 else:
5161 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5162 lcfx = listcatfiles['fnumfiles'];
5163 else:
5164 lcfx = int(listcatfiles['fnumfiles']);
5165 while(lcfi < lcfx):
5166 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
5167 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
5168 catarray['filetoid'].update(filetoidarray);
5169 catarray['idtofile'].update(idtofilearray);
5170 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5171 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
5172 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
5173 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5174 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
5175 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
5176 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5177 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5178 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5179 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5180 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5181 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5182 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5183 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
5184 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
5185 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
5186 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5187 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5188 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
5189 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
5190 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
5191 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5192 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5193 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5194 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
5195 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
5196 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
5197 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5198 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5199 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5200 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5201 lcfi = lcfi + 1;
5202 return catarray;
5204 create_alias_function("", __file_format_name__, "ToArrayIndex", ArchiveFileToArrayIndex);
5206 def ListDirToArrayIndexAlt(infiles, dirlistfromtxt=False, followlink=False, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5207 listcatfiles = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, extradata, formatspecs, verbose);
5208 if(not listcatfiles):
5209 return False;
5210 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': {}}}};
5211 lenlist = len(listcatfiles['ffilelist']);
5212 if(seekstart>0):
5213 lcfi = seekstart;
5214 else:
5215 lcfi = 0;
5216 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5217 lcfx = seekend;
5218 else:
5219 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5220 lcfx = listcatfiles['fnumfiles'];
5221 else:
5222 lcfx = int(listcatfiles['fnumfiles']);
5223 while(lcfi < lcfx):
5224 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
5225 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
5226 catarray['filetoid'].update(filetoidarray);
5227 catarray['idtofile'].update(idtofilearray);
5228 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5229 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
5230 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
5231 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5232 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
5233 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
5234 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5235 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5236 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5237 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5238 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5239 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5240 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5241 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
5242 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
5243 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
5244 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5245 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5246 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
5247 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
5248 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
5249 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5250 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5251 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5252 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
5253 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
5254 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
5255 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5256 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5257 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5258 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5259 lcfi = lcfi + 1;
5260 return catarray;
5262 def TarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5263 listcatfiles = TarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
5264 if(not listcatfiles):
5265 return False;
5266 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': {}}}};
5267 lenlist = len(listcatfiles['ffilelist']);
5268 if(seekstart>0):
5269 lcfi = seekstart;
5270 else:
5271 lcfi = 0;
5272 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5273 lcfx = seekend;
5274 else:
5275 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5276 lcfx = listcatfiles['fnumfiles'];
5277 else:
5278 lcfx = int(listcatfiles['fnumfiles']);
5279 while(lcfi < lcfx):
5280 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
5281 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
5282 catarray['filetoid'].update(filetoidarray);
5283 catarray['idtofile'].update(idtofilearray);
5284 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5285 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
5286 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
5287 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5288 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
5289 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
5290 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5291 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5292 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5293 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5294 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5295 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5296 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5297 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
5298 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
5299 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
5300 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5301 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5302 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
5303 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
5304 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
5305 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5306 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5307 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5308 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
5309 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
5310 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
5311 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5312 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5313 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5314 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5315 lcfi = lcfi + 1;
5316 return catarray;
5318 def ZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5319 listcatfiles = ZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
5320 if(not listcatfiles):
5321 return False;
5322 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': {}}}};
5323 lenlist = len(listcatfiles['ffilelist']);
5324 if(seekstart>0):
5325 lcfi = seekstart;
5326 else:
5327 lcfi = 0;
5328 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5329 lcfx = seekend;
5330 else:
5331 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5332 lcfx = listcatfiles['fnumfiles'];
5333 else:
5334 lcfx = int(listcatfiles['fnumfiles']);
5335 while(lcfi < lcfx):
5336 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
5337 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
5338 catarray['filetoid'].update(filetoidarray);
5339 catarray['idtofile'].update(idtofilearray);
5340 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5341 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
5342 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
5343 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5344 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
5345 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
5346 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5347 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5348 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5349 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5350 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5351 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5352 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5353 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
5354 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
5355 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
5356 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5357 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5358 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
5359 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
5360 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
5361 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5362 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5363 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5364 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
5365 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
5366 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
5367 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5368 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5369 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5370 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5371 lcfi = lcfi + 1;
5372 return catarray;
5374 if(not rarfile_support):
5375 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5376 return False;
5378 if(rarfile_support):
5379 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5380 listcatfiles = RarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
5381 if(not listcatfiles):
5382 return False;
5383 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': {}}}};
5384 lenlist = len(listcatfiles['ffilelist']);
5385 if(seekstart>0):
5386 lcfi = seekstart;
5387 else:
5388 lcfi = 0;
5389 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5390 lcfx = seekend;
5391 else:
5392 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5393 lcfx = listcatfiles['fnumfiles'];
5394 else:
5395 lcfx = int(listcatfiles['fnumfiles']);
5396 while(lcfi < lcfx):
5397 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
5398 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
5399 catarray['filetoid'].update(filetoidarray);
5400 catarray['idtofile'].update(idtofilearray);
5401 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5402 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
5403 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
5404 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5405 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
5406 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
5407 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5408 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5409 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5410 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5411 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5412 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5413 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5414 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
5415 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
5416 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
5417 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5418 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5419 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
5420 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
5421 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
5422 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5423 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5424 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5425 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
5426 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
5427 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
5428 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5429 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5430 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5431 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5432 lcfi = lcfi + 1;
5433 return catarray;
5435 def ArchiveFileStringToArrayIndex(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5436 catfp = BytesIO(catstr);
5437 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5438 return listcatfiles;
5440 create_alias_function("", __file_format_name__, "StringToArrayIndex", ArchiveFileStringToArrayIndex);
5442 def TarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5443 catfp = BytesIO();
5444 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
5445 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5446 return listcatfiles;
5448 def ZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5449 catfp = BytesIO();
5450 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
5451 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5452 return listcatfiles;
5454 if(not rarfile_support):
5455 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5456 return False;
5458 if(rarfile_support):
5459 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5460 catfp = BytesIO();
5461 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
5462 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5463 return listcatfiles;
5465 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):
5466 outarray = BytesIO();
5467 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, verbose, True);
5468 listcatfiles = ArchiveFileToArrayIndex(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
5469 return listcatfiles;
5471 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):
5472 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
5473 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
5474 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
5475 if(isinstance(infile, dict)):
5476 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5477 listcatfiles = prelistcatfiles['list'];
5478 else:
5479 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5480 infile = RemoveWindowsPath(infile);
5481 if(followlink):
5482 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5483 listcatfiles = prelistcatfiles['list'];
5484 else:
5485 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5486 if(outfile!="-" and not hasattr(infile, "read") and not hasattr(outfile, "write")):
5487 outfile = RemoveWindowsPath(outfile);
5488 checksumtype = checksumtype.lower();
5489 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
5490 checksumtype="crc32";
5491 if(checksumtype=="none"):
5492 checksumtype = "";
5493 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
5494 compression = None;
5495 if(compression not in compressionlist and compression is None):
5496 compression = "auto";
5497 if(verbose):
5498 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5499 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
5500 if(os.path.exists(outfile)):
5501 os.unlink(outfile);
5502 if(not listcatfiles):
5503 return False;
5504 if(outfile=="-"):
5505 verbose = False;
5506 catfp = BytesIO();
5507 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
5508 catfp = outfile;
5509 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
5510 catfp = BytesIO();
5511 else:
5512 fbasename = os.path.splitext(outfile)[0];
5513 fextname = os.path.splitext(outfile)[1];
5514 catfp = CompressOpenFile(outfile, compressionlevel);
5515 catver = formatspecs[6];
5516 fileheaderver = str(int(catver.replace(".", "")));
5517 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5518 catfp.write(fileheader.encode('UTF-8'));
5519 lenlist = len(listcatfiles['ffilelist']);
5520 fnumfiles = int(listcatfiles['fnumfiles']);
5521 if(lenlist>fnumfiles or lenlist<fnumfiles):
5522 fnumfiles = lenlist;
5523 fnumfileshex = format(int(fnumfiles), 'x').lower();
5524 fnumfilesa = AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5525 if(checksumtype=="none" or checksumtype==""):
5526 catfileheadercshex = format(0, 'x').lower();
5527 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5528 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
5529 elif(checksumtype=="crc16_ccitt"):
5530 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
5531 elif(checksumtype=="adler32"):
5532 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
5533 elif(checksumtype=="crc32"):
5534 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
5535 elif(checksumtype=="crc64_ecma"):
5536 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5537 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5538 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5539 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5540 checksumoutstr = hashlib.new(checksumtype);
5541 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
5542 catfileheadercshex = checksumoutstr.hexdigest().lower();
5543 else:
5544 catfileheadercshex = format(0, 'x').lower();
5545 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
5546 catfp.write(fnumfilesa.encode('UTF-8'));
5547 try:
5548 catfp.flush();
5549 os.fsync(catfp.fileno());
5550 except io.UnsupportedOperation:
5551 pass;
5552 except AttributeError:
5553 pass;
5554 if(seekstart>0):
5555 lcfi = seekstart;
5556 else:
5557 lcfi = 0;
5558 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5559 lcfx = seekend;
5560 else:
5561 lcfx = int(listcatfiles['fnumfiles']);
5562 curinode = 0;
5563 curfid = 0;
5564 inodelist = [];
5565 inodetofile = {};
5566 filetoinode = {};
5567 reallcfi = 0;
5568 while(lcfi < lcfx):
5569 catfhstart = catfp.tell();
5570 if(re.findall("^[.|/]", listcatfiles['ffilelist'][reallcfi]['fname'])):
5571 fname = listcatfiles['ffilelist'][reallcfi]['fname'];
5572 else:
5573 fname = "./"+listcatfiles['ffilelist'][reallcfi]['fname'];
5574 if(verbose):
5575 VerbosePrintOut(fname);
5576 fheadersize = format(int(listcatfiles['ffilelist'][reallcfi]['fheadersize']), 'x').lower();
5577 fsize = format(int(listcatfiles['ffilelist'][reallcfi]['fsize']), 'x').lower();
5578 flinkname = listcatfiles['ffilelist'][reallcfi]['flinkname'];
5579 fatime = format(int(listcatfiles['ffilelist'][reallcfi]['fatime']), 'x').lower();
5580 fmtime = format(int(listcatfiles['ffilelist'][reallcfi]['fmtime']), 'x').lower();
5581 fctime = format(int(listcatfiles['ffilelist'][reallcfi]['fctime']), 'x').lower();
5582 fbtime = format(int(listcatfiles['ffilelist'][reallcfi]['fbtime']), 'x').lower();
5583 fmode = format(int(listcatfiles['ffilelist'][reallcfi]['fmode']), 'x').lower();
5584 fchmode = format(int(listcatfiles['ffilelist'][reallcfi]['fchmode']), 'x').lower();
5585 fuid = format(int(listcatfiles['ffilelist'][reallcfi]['fuid']), 'x').lower();
5586 funame = listcatfiles['ffilelist'][reallcfi]['funame'];
5587 fgid = format(int(listcatfiles['ffilelist'][reallcfi]['fgid']), 'x').lower();
5588 fgname = listcatfiles['ffilelist'][reallcfi]['fgname'];
5589 finode = listcatfiles['ffilelist'][reallcfi]['finode'];
5590 flinkcount = listcatfiles['ffilelist'][reallcfi]['flinkcount'];
5591 fdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['fminor']), 'x').lower();
5592 fdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['fmajor']), 'x').lower();
5593 frdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['frminor']), 'x').lower();
5594 frdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['frmajor']), 'x').lower();
5595 if(len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>listcatfiles['ffilelist'][reallcfi]['fextrafields'] and len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>0):
5596 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(listcatfiles['ffilelist'][reallcfi]['fextralist']);
5597 if(len(extradata) > 0):
5598 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(extradata);
5599 listcatfiles['ffilelist'][reallcfi]['fextralist'] = extradata;
5600 extrafields = format(int(listcatfiles['ffilelist'][reallcfi]['fextrafields']), 'x').lower();
5601 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5602 if(len(extradata)>0):
5603 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5604 extrasizelen = format(len(extrasizestr), 'x').lower();
5605 fcontents = listcatfiles['ffilelist'][reallcfi]['fcontents'];
5606 if(followlink):
5607 if(listcatfiles['ffilelist'][reallcfi]['ftype']==1 or listcatfiles['ffilelist'][reallcfi]['ftype']==2):
5608 getflinkpath = listcatfiles['ffilelist'][reallcfi]['flinkname'];
5609 flinkid = prelistcatfiles['filetoid'][getflinkpath];
5610 flinkinfo = listcatfiles['ffilelist'][flinkid];
5611 fheadersize = format(int(flinkinfo['fheadersize']), 'x').lower();
5612 fsize = format(int(flinkinfo['fsize']), 'x').lower();
5613 flinkname = flinkinfo['flinkname'];
5614 fatime = format(int(flinkinfo['fatime']), 'x').lower();
5615 fmtime = format(int(flinkinfo['fmtime']), 'x').lower();
5616 fctime = format(int(flinkinfo['fctime']), 'x').lower();
5617 fbtime = format(int(flinkinfo['fbtime']), 'x').lower();
5618 fmode = format(int(flinkinfo['fmode']), 'x').lower();
5619 fchmode = format(int(flinkinfo['fchmode']), 'x').lower();
5620 fuid = format(int(flinkinfo['fuid']), 'x').lower();
5621 funame = flinkinfo['funame'];
5622 fgid = format(int(flinkinfo['fgid']), 'x').lower();
5623 fgname = flinkinfo['fgname'];
5624 finode = flinkinfo['finode'];
5625 flinkcount = flinkinfo['flinkcount'];
5626 fdev_minor = format(int(flinkinfo['fminor']), 'x').lower();
5627 fdev_major = format(int(flinkinfo['fmajor']), 'x').lower();
5628 frdev_minor = format(int(flinkinfo['frminor']), 'x').lower();
5629 frdev_major = format(int(flinkinfo['frmajor']), 'x').lower();
5630 if(len(flinkinfo['fextralist'])>flinkinfo['fextrafields'] and len(flinkinfo['fextralist'])>0):
5631 flinkinfo['fextrafields'] = len(flinkinfo['fextralist']);
5632 if(len(extradata) > 0):
5633 flinkinfo['fextrafields'] = len(extradata);
5634 flinkinfo['fextralist'] = extradata;
5635 extrafields = format(int(flinkinfo['fextrafields']), 'x').lower();
5636 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5637 if(len(extradata)>0):
5638 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5639 extrasizelen = format(len(extrasizestr), 'x').lower();
5640 fcontents = flinkinfo['fcontents'];
5641 if(flinkinfo['ftype']!=0 and flinkinfo['ftype']!=7):
5642 try:
5643 fcontents = fcontents.encode('UTF-8');
5644 except AttributeError:
5645 pass;
5646 ftypehex = format(flinkinfo['ftype'], 'x').lower();
5647 else:
5648 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=0 and listcatfiles['ffilelist'][reallcfi]['ftype']!=7):
5649 try:
5650 fcontents = fcontents.encode('UTF-8');
5651 except AttributeError:
5652 pass;
5653 ftypehex = format(listcatfiles['ffilelist'][reallcfi]['ftype'], 'x').lower();
5654 fcurfid = format(curfid, 'x').lower();
5655 if(not followlink and finode!=0):
5656 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=1):
5657 fcurinode = format(int(curinode), 'x').lower();
5658 inodetofile.update({curinode: fname});
5659 filetoinode.update({fname: curinode});
5660 curinode = curinode + 1;
5661 else:
5662 fcurinode = format(int(filetoinode[flinkname]), 'x').lower();
5663 else:
5664 fcurinode = format(int(curinode), 'x').lower();
5665 curinode = curinode + 1;
5666 curfid = curfid + 1;
5667 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
5668 if(listcatfiles['ffilelist'][reallcfi]['fextrafields']>0):
5669 extrafieldslist = [];
5670 exi = 0;
5671 exil = listcatfiles['ffilelist'][reallcfi]['fextrafields'];
5672 while(exi < exil):
5673 extrafieldslist.append(listcatfiles['ffilelist'][reallcfi]['fextralist']);
5674 exi = exi + 1;
5675 catfileoutstr += AppendNullBytes([extrafieldslist], formatspecs[5]);
5676 catfileoutstr += AppendNullBytes([checksumtype], formatspecs[5]);
5677 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
5678 catfcontentstart = catfp.tell() + len(catfileoutstr);
5679 if(checksumtype=="none" or checksumtype==""):
5680 catfileheadercshex = format(0, 'x').lower();
5681 catfilecontentcshex = format(0, 'x').lower();
5682 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5683 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5684 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5685 elif(checksumtype=="crc16_ccitt"):
5686 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5687 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5688 elif(checksumtype=="adler32"):
5689 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5690 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5691 elif(checksumtype=="crc32"):
5692 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5693 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5694 elif(checksumtype=="crc64_ecma"):
5695 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5696 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5697 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5698 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5699 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5700 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5701 checksumoutstr = hashlib.new(checksumtype);
5702 checksumoutstr.update("".encode('UTF-8'));
5703 catfileheadercshex = checksumoutstr.hexdigest().lower();
5704 checksumoutstr = hashlib.new(checksumtype);
5705 checksumoutstr.update(fcontents);
5706 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5707 else:
5708 catfileheadercshex = format(0, 'x').lower();
5709 catfilecontentcshex = format(0, 'x').lower();
5710 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5711 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5712 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5713 if(checksumtype=="none" or checksumtype==""):
5714 catfileheadercshex = format(0, 'x').lower();
5715 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5716 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5717 elif(checksumtype=="crc16_ccitt"):
5718 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5719 elif(checksumtype=="adler32"):
5720 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5721 elif(checksumtype=="crc32"):
5722 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5723 elif(checksumtype=="crc64_ecma"):
5724 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5725 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5726 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5727 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5728 checksumoutstr = hashlib.new(checksumtype);
5729 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5730 catfileheadercshex = checksumoutstr.hexdigest().lower();
5731 else:
5732 catfileheadercshex = format(0, 'x').lower();
5733 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5734 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5735 nullstrecd = formatspecs[5].encode('UTF-8');
5736 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5737 catfcontentend = (catfp.tell() - 1) + len(catfileout);
5738 catfp.write(catfileout);
5739 try:
5740 catfp.flush();
5741 os.fsync(catfp.fileno());
5742 except io.UnsupportedOperation:
5743 pass;
5744 except AttributeError:
5745 pass;
5746 lcfi = lcfi + 1;
5747 reallcfi = reallcfi + 1;
5748 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
5749 catfp = CompressArchiveFile(catfp, compression, formatspecs);
5750 try:
5751 catfp.flush();
5752 os.fsync(catfp.fileno());
5753 except io.UnsupportedOperation:
5754 pass;
5755 except AttributeError:
5756 pass;
5757 if(outfile=="-"):
5758 catfp.seek(0, 0);
5759 if(hasattr(sys.stdout, "buffer")):
5760 shutil.copyfileobj(catfp, sys.stdout.buffer);
5761 else:
5762 shutil.copyfileobj(catfp, sys.stdout);
5763 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
5764 catfp = CompressArchiveFile(catfp, compression, formatspecs);
5765 catfp.seek(0, 0);
5766 upload_file_to_internet_file(catfp, outfile);
5767 if(returnfp):
5768 catfp.seek(0, 0);
5769 return catfp;
5770 else:
5771 catfp.close();
5772 return True;
5774 create_alias_function("RePack", __file_format_name__, "", RePackArchiveFile);
5776 def RePackArchiveFileFromString(catstr, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5777 catfp = BytesIO(catstr);
5778 listcatfiles = RePackArchiveFile(catfp, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
5779 return listcatfiles;
5781 create_alias_function("RePack", __file_format_name__, "FromString", RePackArchiveFileFromString);
5783 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):
5784 outarray = BytesIO();
5785 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
5786 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
5787 return listcatfiles;
5789 create_alias_function("Pack", __file_format_name__, "FromListDir", PackArchiveFileFromListDir);
5791 def ArchiveFileArrayBase64Encode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5792 if(verbose):
5793 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5794 if(isinstance(infile, dict)):
5795 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5796 listcatfiles = prelistcatfiles['list'];
5797 else:
5798 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5799 infile = RemoveWindowsPath(infile);
5800 if(followlink):
5801 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5802 listcatfiles = prelistcatfiles['list'];
5803 else:
5804 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5805 if(not listcatfiles):
5806 return False;
5807 lenlist = len(listcatfiles['ffilelist']);
5808 if(seekstart>0):
5809 lcfi = seekstart;
5810 else:
5811 lcfi = 0;
5812 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5813 lcfx = seekend;
5814 else:
5815 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5816 lcfx = listcatfiles['fnumfiles'];
5817 else:
5818 lcfx = int(listcatfiles['fnumfiles']);
5819 if(lenlist>lcfx or lenlist<lcfx):
5820 lcfx = lenlist;
5821 while(lcfi < lcfx):
5822 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
5823 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64encode(listcatfiles['ffilelist'][lcfi]['fcontents']).decode("UTF-8");
5824 lcfi = lcfi + 1;
5825 return listcatfiles;
5827 create_alias_function("", __file_format_name__, "ArrayBase64Encode", ArchiveFileArrayBase64Encode);
5829 def ArchiveFileArrayBase64Decode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5830 if(verbose):
5831 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5832 if(isinstance(infile, dict)):
5833 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5834 listcatfiles = prelistcatfiles['list'];
5835 else:
5836 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5837 infile = RemoveWindowsPath(infile);
5838 if(followlink):
5839 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5840 listcatfiles = prelistcatfiles['list'];
5841 else:
5842 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5843 if(not listcatfiles):
5844 return False;
5845 lenlist = len(listcatfiles['ffilelist']);
5846 if(seekstart>0):
5847 lcfi = seekstart;
5848 else:
5849 lcfi = 0;
5850 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5851 lcfx = seekend;
5852 else:
5853 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5854 lcfx = listcatfiles['fnumfiles'];
5855 else:
5856 lcfx = int(listcatfiles['fnumfiles']);
5857 if(lenlist>lcfx or lenlist<lcfx):
5858 lcfx = lenlist;
5859 while(lcfi < lcfx):
5860 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
5861 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64decode(listcatfiles['ffilelist'][lcfi]['fcontents'].encode("UTF-8"));
5862 lcfi = lcfi + 1;
5863 return listcatfiles;
5865 create_alias_function("", __file_format_name__, "ArrayBase64Decode", ArchiveFileArrayBase64Decode);
5867 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):
5868 if(outdir is not None):
5869 outdir = RemoveWindowsPath(outdir);
5870 if(verbose):
5871 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5872 if(isinstance(infile, dict)):
5873 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5874 listcatfiles = prelistcatfiles['list'];
5875 else:
5876 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5877 infile = RemoveWindowsPath(infile);
5878 if(followlink):
5879 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5880 listcatfiles = prelistcatfiles['list'];
5881 else:
5882 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5883 if(not listcatfiles):
5884 return False;
5885 lenlist = len(listcatfiles['ffilelist']);
5886 if(seekstart>0):
5887 lcfi = seekstart;
5888 else:
5889 lcfi = 0;
5890 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5891 lcfx = seekend;
5892 else:
5893 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5894 lcfx = listcatfiles['fnumfiles'];
5895 else:
5896 lcfx = int(listcatfiles['fnumfiles']);
5897 if(lenlist>lcfx or lenlist<lcfx):
5898 lcfx = lenlist;
5899 while(lcfi < lcfx):
5900 funame = "";
5901 try:
5902 import pwd;
5903 try:
5904 userinfo = pwd.getpwuid(listcatfiles['ffilelist'][lcfi]['fuid']);
5905 funame = userinfo.pw_name;
5906 except KeyError:
5907 funame = "";
5908 except ImportError:
5909 funame = "";
5910 fgname = "";
5911 try:
5912 import grp;
5913 try:
5914 groupinfo = grp.getgrgid(listcatfiles['ffilelist'][lcfi]['fgid']);
5915 fgname = groupinfo.gr_name;
5916 except KeyError:
5917 fgname = "";
5918 except ImportError:
5919 fgname = "";
5920 if(verbose):
5921 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
5922 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5923 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
5924 fpc.write(listcatfiles['ffilelist'][lcfi]['fcontents'])
5925 try:
5926 fpc.flush()
5927 os.fsync(fpc.fileno())
5928 except io.UnsupportedOperation:
5929 pass
5930 except AttributeError:
5931 pass
5932 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
5933 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
5934 if(preservepermissions):
5935 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
5936 if(preservetime):
5937 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
5938 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5939 if(followlink):
5940 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
5941 flinkid = prelistcatfiles['filetoid'][getflinkpath];
5942 flinkinfo = listcatfiles['ffilelist'][flinkid];
5943 funame = "";
5944 try:
5945 import pwd;
5946 try:
5947 userinfo = pwd.getpwuid(flinkinfo['fuid']);
5948 funame = userinfo.pw_name;
5949 except KeyError:
5950 funame = "";
5951 except ImportError:
5952 funame = "";
5953 fgname = "";
5954 try:
5955 import grp;
5956 try:
5957 groupinfo = grp.getgrgid(flinkinfo['fgid']);
5958 fgname = groupinfo.gr_name;
5959 except KeyError:
5960 fgname = "";
5961 except ImportError:
5962 fgname = "";
5963 if(flinkinfo['ftype'] == 0 or flinkinfo['ftype'] == 7):
5964 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
5965 fpc.write(flinkinfo['fcontents'])
5966 try:
5967 fpc.flush()
5968 os.fsync(fpc.fileno())
5969 except io.UnsupportedOperation:
5970 pass
5971 except AttributeError:
5972 pass
5973 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
5974 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
5975 if(preservepermissions):
5976 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5977 if(preservetime):
5978 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
5979 if(flinkinfo['ftype']==1):
5980 os.link(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5981 if(flinkinfo['ftype']==2):
5982 os.symlink(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5983 if(flinkinfo['ftype']==5):
5984 if(preservepermissions):
5985 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5986 else:
5987 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname']);
5988 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
5989 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
5990 if(preservepermissions):
5991 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5992 if(preservetime):
5993 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
5994 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
5995 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5996 else:
5997 os.link(listcatfiles['ffilelist'][lcfi]['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5998 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5999 if(followlink):
6000 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
6001 flinkid = prelistcatfiles['filetoid'][getflinkpath];
6002 flinkinfo = listcatfiles['ffilelist'][flinkid];
6003 funame = "";
6004 try:
6005 import pwd;
6006 try:
6007 userinfo = pwd.getpwuid(flinkinfo['fuid']);
6008 funame = userinfo.pw_name;
6009 except KeyError:
6010 funame = "";
6011 except ImportError:
6012 funame = "";
6013 fgname = "";
6014 try:
6015 import grp;
6016 try:
6017 groupinfo = grp.getgrgid(flinkinfo['fgid']);
6018 fgname = groupinfo.gr_name;
6019 except KeyError:
6020 fgname = "";
6021 except ImportError:
6022 fgname = "";
6023 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
6024 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
6025 fpc.write(flinkinfo['fcontents'])
6026 try:
6027 fpc.flush()
6028 os.fsync(fpc.fileno())
6029 except io.UnsupportedOperation:
6030 pass
6031 except AttributeError:
6032 pass
6033 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
6034 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
6035 if(preservepermissions):
6036 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
6037 if(preservetime):
6038 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
6039 if(flinkinfo['ftype']==1):
6040 os.link(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
6041 if(flinkinfo['ftype']==2):
6042 os.symlink(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
6043 if(flinkinfo['ftype']==5):
6044 if(preservepermissions):
6045 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
6046 else:
6047 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname']);
6048 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
6049 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
6050 if(preservepermissions):
6051 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
6052 if(preservetime):
6053 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
6054 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
6055 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
6056 else:
6057 os.symlink(listcatfiles['ffilelist'][lcfi]['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
6058 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
6059 if(preservepermissions):
6060 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
6061 else:
6062 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname']);
6063 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
6064 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
6065 if(preservepermissions):
6066 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
6067 if(preservetime):
6068 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
6069 if(listcatfiles['ffilelist'][lcfi]['ftype']==6 and hasattr(os, "mkfifo")):
6070 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
6071 lcfi = lcfi + 1;
6072 if(returnfp):
6073 return listcatfiles['ffilelist']['catfp'];
6074 else:
6075 return True;
6077 create_alias_function("UnPack", __file_format_name__, "", UnPackArchiveFile);
6079 if(hasattr(shutil, "register_unpack_format")):
6080 def UnPackArchiveFileFunc(archive_name, extract_dir=None, **kwargs):
6081 return UnPackArchiveFile(archive_name, extract_dir, False, 0, 0, False, __file_format_delimiter__, False, False);
6082 create_alias_function("UnPack", __file_format_name__, "Func", UnPackArchiveFileFunc);
6084 def UnPackArchiveFileString(catstr, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6085 catfp = BytesIO(catstr);
6086 listcatfiles = UnPackArchiveFile(catfp, outdir, followlink, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
6087 return listcatfiles;
6089 create_alias_function("UnPack", __file_format_name__, "String", UnPackArchiveFileString);
6091 def ArchiveFileListFiles(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6092 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6093 if(isinstance(infile, dict)):
6094 listcatfiles = infile;
6095 else:
6096 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
6097 infile = RemoveWindowsPath(infile);
6098 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, returnfp);
6099 if(not listcatfiles):
6100 return False;
6101 lenlist = len(listcatfiles['ffilelist']);
6102 lcfi = 0;
6103 lcfx = lenlist;
6104 returnval = {};
6105 while(lcfi < lcfx):
6106 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
6107 if(not verbose):
6108 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
6109 if(verbose):
6110 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' } };
6111 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
6112 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
6113 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
6114 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
6115 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
6116 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
6117 if(len(fuprint)<=0):
6118 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
6119 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
6120 if(len(fgprint)<=0):
6121 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
6122 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));
6123 lcfi = lcfi + 1;
6124 if(returnfp):
6125 return listcatfiles['catfp'];
6126 else:
6127 return True;
6129 create_alias_function("", __file_format_name__, "ListFiles", ArchiveFileListFiles);
6131 def ArchiveFileStringListFiles(catstr, followlink=False, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
6132 catfp = BytesIO(catstr);
6133 listcatfiles = UnPackArchiveFile(catfp, None, followlink, skipchecksum, formatspecs, verbose, returnfp);
6134 return listcatfiles;
6136 create_alias_function("", __file_format_name__, "StringListFiles", ArchiveFileListFiles);
6138 def TarFileListFiles(infile, verbose=False, returnfp=False):
6139 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6140 if(infile=="-"):
6141 infile = BytesIO();
6142 if(hasattr(sys.stdin, "buffer")):
6143 shutil.copyfileobj(sys.stdin.buffer, infile);
6144 else:
6145 shutil.copyfileobj(sys.stdin, infile);
6146 infile.seek(0, 0);
6147 if(not infile):
6148 return False;
6149 infile.seek(0, 0);
6150 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
6151 infile = download_file_from_internet_file(infile);
6152 infile.seek(0, 0);
6153 if(not infile):
6154 return False;
6155 infile.seek(0, 0);
6156 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
6157 return False;
6158 elif(os.path.exists(infile) and os.path.isfile(infile)):
6159 try:
6160 if(not tarfile.is_tarfile(infile)):
6161 return False;
6162 except AttributeError:
6163 if(not is_tarfile(infile)):
6164 return False;
6165 else:
6166 return False;
6167 try:
6168 if(hasattr(infile, "read") or hasattr(infile, "write")):
6169 tarfp = tarfile.open(fileobj=infile, mode="r");
6170 else:
6171 tarfp = tarfile.open(infile, "r");
6172 except FileNotFoundError:
6173 return False;
6174 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
6175 returnval.update({lcfi: member.name});
6176 fpremode = member.mode;
6177 ffullmode = member.mode;
6178 flinkcount = 0;
6179 ftype = 0;
6180 if(member.isreg()):
6181 ffullmode = member.mode + stat.S_IFREG;
6182 ftype = 0;
6183 elif(member.isdev()):
6184 ffullmode = member.mode;
6185 ftype = 7;
6186 elif(member.islnk()):
6187 ffullmode = member.mode + stat.S_IFREG;
6188 ftype = 1;
6189 elif(member.issym()):
6190 ffullmode = member.mode + stat.S_IFLNK;
6191 ftype = 2;
6192 elif(member.ischr()):
6193 ffullmode = member.mode + stat.S_IFCHR;
6194 ftype = 3;
6195 elif(member.isblk()):
6196 ffullmode = member.mode + stat.S_IFBLK;
6197 ftype = 4;
6198 elif(member.isdir()):
6199 ffullmode = member.mode + stat.S_IFDIR;
6200 ftype = 5;
6201 elif(member.isfifo()):
6202 ffullmode = member.mode + stat.S_IFIFO;
6203 ftype = 6;
6204 elif(member.issparse()):
6205 ffullmode = member.mode;
6206 ftype = 12;
6207 if(not verbose):
6208 VerbosePrintOut(member.name);
6209 elif(verbose):
6210 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' } };
6211 printfname = member.name;
6212 if(member.islnk()):
6213 printfname = member.name + " link to " + member.linkname;
6214 elif(member.issym()):
6215 printfname = member.name + " -> " + member.linkname;
6216 fuprint = member.uname;
6217 if(len(fuprint)<=0):
6218 fuprint = member.uid;
6219 fgprint = member.gname;
6220 if(len(fgprint)<=0):
6221 fgprint = member.gid;
6222 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));
6223 lcfi = lcfi + 1;
6224 if(returnfp):
6225 return listcatfiles['catfp'];
6226 else:
6227 return True;
6229 def ZipFileListFiles(infile, verbose=False, returnfp=False):
6230 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6231 if(infile=="-"):
6232 infile = BytesIO();
6233 if(hasattr(sys.stdin, "buffer")):
6234 shutil.copyfileobj(sys.stdin.buffer, infile);
6235 else:
6236 shutil.copyfileobj(sys.stdin, infile);
6237 infile.seek(0, 0);
6238 if(not infile):
6239 return False;
6240 infile.seek(0, 0);
6241 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
6242 infile = download_file_from_internet_file(infile);
6243 infile.seek(0, 0);
6244 if(not infile):
6245 return False;
6246 infile.seek(0, 0);
6247 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
6248 return False;
6249 else:
6250 return False;
6251 if(not zipfile.is_zipfile(infile)):
6252 return False;
6253 try:
6254 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
6255 except FileNotFoundError:
6256 return False;
6257 lcfi = 0;
6258 returnval = {};
6259 ziptest = zipfp.testzip();
6260 if(ziptest):
6261 VerbosePrintOut("Bad file found!");
6262 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
6263 if(not member.is_dir()):
6264 fpremode = int(stat.S_IFREG + 438);
6265 elif(member.is_dir()):
6266 fpremode = int(stat.S_IFDIR + 511);
6267 if(not member.is_dir()):
6268 fmode = int(stat.S_IFREG + 438);
6269 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
6270 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
6271 elif(member.is_dir()):
6272 fmode = int(stat.S_IFDIR + 511);
6273 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
6274 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
6275 returnval.update({lcfi: member.filename});
6276 if(not verbose):
6277 VerbosePrintOut(member.filename);
6278 if(verbose):
6279 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' } };
6280 permissionstr = "";
6281 for fmodval in str(oct(fmode))[-3:]:
6282 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
6283 if(not member.is_dir()):
6284 ftype = 0;
6285 permissionstr = "-" + permissionstr;
6286 elif(member.is_dir()):
6287 ftype = 5;
6288 permissionstr = "d" + permissionstr;
6289 printfname = member.filename;
6290 try:
6291 fuid = int(os.getuid());
6292 except AttributeError:
6293 fuid = int(0);
6294 except KeyError:
6295 fuid = int(0);
6296 try:
6297 fgid = int(os.getgid());
6298 except AttributeError:
6299 fgid = int(0);
6300 except KeyError:
6301 fgid = int(0);
6302 try:
6303 import pwd;
6304 try:
6305 userinfo = pwd.getpwuid(os.getuid());
6306 funame = userinfo.pw_name;
6307 except KeyError:
6308 funame = "";
6309 except AttributeError:
6310 funame = "";
6311 except ImportError:
6312 funame = "";
6313 fgname = "";
6314 try:
6315 import grp;
6316 try:
6317 groupinfo = grp.getgrgid(os.getgid());
6318 fgname = groupinfo.gr_name;
6319 except KeyError:
6320 fgname = "";
6321 except AttributeError:
6322 fgname = "";
6323 except ImportError:
6324 fgname = "";
6325 fuprint = funame;
6326 if(len(fuprint)<=0):
6327 fuprint = str(fuid);
6328 fgprint = fgname;
6329 if(len(fgprint)<=0):
6330 fgprint = str(fgid);
6331 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));
6332 lcfi = lcfi + 1;
6333 if(returnfp):
6334 return listcatfiles['catfp'];
6335 else:
6336 return True;
6338 if(not rarfile_support):
6339 def RarFileListFiles(infile, verbose=False, returnfp=False):
6340 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6341 if(not os.path.exists(infile) or not os.path.isfile(infile)):
6342 return False;
6344 if(rarfile_support):
6345 def RarFileListFiles(infile, verbose=False, returnfp=False):
6346 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
6347 if(not os.path.exists(infile) or not os.path.isfile(infile)):
6348 return False;
6349 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
6350 return False;
6351 lcfi = 0;
6352 returnval = {};
6353 rarfp = rarfile.RarFile(infile, "r");
6354 rartest = rarfp.testrar();
6355 if(rartest):
6356 VerbosePrintOut("Bad file found!");
6357 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
6358 is_unix = False;
6359 is_windows = False;
6360 if(member.host_os == rarfile.RAR_OS_UNIX):
6361 is_windows = False;
6362 try:
6363 member.external_attr
6364 is_unix = True;
6365 except AttributeError:
6366 is_unix = False;
6367 elif(member.host_os == rarfile.RAR_OS_WIN32):
6368 is_unix = False;
6369 try:
6370 member.external_attr
6371 is_windows = True;
6372 except AttributeError:
6373 is_windows = False;
6374 else:
6375 is_unix = False;
6376 is_windows = False;
6377 if(is_unix and member.external_attr !=0):
6378 fpremode = int(member.external_attr);
6379 elif(member.is_file()):
6380 fpremode = int(stat.S_IFREG + 438);
6381 elif(member.is_symlink()):
6382 fpremode = int(stat.S_IFLNK + 438);
6383 elif(member.is_dir()):
6384 fpremode = int(stat.S_IFDIR + 511);
6385 if(is_windows and member.external_attr !=0):
6386 fwinattributes = int(member.external_attr);
6387 else:
6388 fwinattributes = int(0);
6389 if(is_unix and member.external_attr !=0):
6390 fmode = int(member.external_attr);
6391 fchmode = int(stat.S_IMODE(member.external_attr));
6392 ftypemod = int(stat.S_IFMT(member.external_attr));
6393 elif(member.is_file()):
6394 fmode = int(stat.S_IFREG + 438);
6395 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
6396 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
6397 elif(member.is_symlink()):
6398 fmode = int(stat.S_IFLNK + 438);
6399 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
6400 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
6401 elif(member.is_dir()):
6402 fmode = int(stat.S_IFDIR + 511);
6403 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
6404 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
6405 returnval.update({lcfi: member.filename});
6406 if(not verbose):
6407 VerbosePrintOut(member.filename);
6408 if(verbose):
6409 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' } };
6410 permissionstr = "";
6411 for fmodval in str(oct(fmode))[-3:]:
6412 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
6413 if(member.is_file()):
6414 ftype = 0;
6415 permissionstr = "-" + permissionstr;
6416 printfname = member.filename;
6417 elif(member.is_symlink()):
6418 ftype = 2;
6419 permissionstr = "l" + permissionstr;
6420 printfname = member.name + " -> " + member.read().decode("UTF-8");
6421 elif(member.is_dir()):
6422 ftype = 5;
6423 permissionstr = "d" + permissionstr;
6424 printfname = member.filename;
6425 try:
6426 fuid = int(os.getuid());
6427 except AttributeError:
6428 fuid = int(0);
6429 except KeyError:
6430 fuid = int(0);
6431 try:
6432 fgid = int(os.getgid());
6433 except AttributeError:
6434 fgid = int(0);
6435 except KeyError:
6436 fgid = int(0);
6437 try:
6438 import pwd;
6439 try:
6440 userinfo = pwd.getpwuid(os.getuid());
6441 funame = userinfo.pw_name;
6442 except KeyError:
6443 funame = "";
6444 except AttributeError:
6445 funame = "";
6446 except ImportError:
6447 funame = "";
6448 fgname = "";
6449 try:
6450 import grp;
6451 try:
6452 groupinfo = grp.getgrgid(os.getgid());
6453 fgname = groupinfo.gr_name;
6454 except KeyError:
6455 fgname = "";
6456 except AttributeError:
6457 fgname = "";
6458 except ImportError:
6459 fgname = "";
6460 fuprint = funame;
6461 if(len(fuprint)<=0):
6462 fuprint = str(fuid);
6463 fgprint = fgname;
6464 if(len(fgprint)<=0):
6465 fgprint = str(fgid);
6466 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + member.mtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
6467 lcfi = lcfi + 1;
6468 if(returnfp):
6469 return listcatfiles['catfp'];
6470 else:
6471 return True;
6473 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):
6474 outarray = BytesIO();
6475 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, False, True);
6476 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
6477 return listcatfiles;
6479 def ListDirListFilesAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=True, seekstart=0, seekend=0, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
6480 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, formatspecs, verbose);
6481 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
6482 return listcatfiles;
6484 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):
6485 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, False, checksumtype, extradata, formatspecs, False);
6486 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, followlink, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
6487 return listcatfiles;
6489 create_alias_function("Pack", __file_format_name__, "FromListDirAlt", PackArchiveFileFromListDirAlt);
6491 def PackArchiveFileFromTarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6492 outarray = TarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
6493 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
6494 return listcatfiles;
6496 create_alias_function("Pack", __file_format_name__, "FromTarFileAlt", PackArchiveFileFromTarFileAlt);
6498 def PackArchiveFileFromZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6499 outarray = ZipFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
6500 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
6501 return listcatfiles;
6503 create_alias_function("Pack", __file_format_name__, "FromZipFileAlt", PackArchiveFileFromZipFileAlt);
6505 if(not rarfile_support):
6506 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6507 return False;
6509 if(rarfile_support):
6510 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6511 outarray = RarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
6512 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
6513 return listcatfiles;
6515 create_alias_function("Pack", __file_format_name__, "FromRarFileAlt", PackArchiveFileFromRarFileAlt);
6517 def download_file_from_ftp_file(url):
6518 urlparts = urlparse(url);
6519 file_name = os.path.basename(urlparts.path);
6520 file_dir = os.path.dirname(urlparts.path);
6521 if(urlparts.username is not None):
6522 ftp_username = urlparts.username;
6523 else:
6524 ftp_username = "anonymous";
6525 if(urlparts.password is not None):
6526 ftp_password = urlparts.password;
6527 elif(urlparts.password is None and urlparts.username=="anonymous"):
6528 ftp_password = "anonymous";
6529 else:
6530 ftp_password = "";
6531 if(urlparts.scheme=="ftp"):
6532 ftp = FTP();
6533 elif(urlparts.scheme=="ftps"):
6534 ftp = FTP_TLS();
6535 else:
6536 return False;
6537 if(urlparts.scheme=="sftp"):
6538 if(__use_pysftp__):
6539 return download_file_from_pysftp_file(url);
6540 else:
6541 return download_file_from_sftp_file(url);
6542 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6543 return download_file_from_http_file(url);
6544 ftp_port = urlparts.port;
6545 if(urlparts.port is None):
6546 ftp_port = 21;
6547 try:
6548 ftp.connect(urlparts.hostname, ftp_port);
6549 except socket.gaierror:
6550 log.info("Error With URL "+url);
6551 return False;
6552 except socket.timeout:
6553 log.info("Error With URL "+url);
6554 return False;
6555 ftp.login(urlparts.username, urlparts.password);
6556 if(urlparts.scheme=="ftps"):
6557 ftp.prot_p();
6558 ftpfile = BytesIO();
6559 ftp.retrbinary("RETR "+urlparts.path, ftpfile.write);
6560 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6561 ftp.close();
6562 ftpfile.seek(0, 0);
6563 return ftpfile;
6565 def download_file_from_ftp_string(url):
6566 ftpfile = download_file_from_ftp_file(url);
6567 return ftpfile.read();
6569 def upload_file_to_ftp_file(ftpfile, url):
6570 urlparts = urlparse(url);
6571 file_name = os.path.basename(urlparts.path);
6572 file_dir = os.path.dirname(urlparts.path);
6573 if(urlparts.username is not None):
6574 ftp_username = urlparts.username;
6575 else:
6576 ftp_username = "anonymous";
6577 if(urlparts.password is not None):
6578 ftp_password = urlparts.password;
6579 elif(urlparts.password is None and urlparts.username=="anonymous"):
6580 ftp_password = "anonymous";
6581 else:
6582 ftp_password = "";
6583 if(urlparts.scheme=="ftp"):
6584 ftp = FTP();
6585 elif(urlparts.scheme=="ftps"):
6586 ftp = FTP_TLS();
6587 else:
6588 return False;
6589 if(urlparts.scheme=="sftp"):
6590 if(__use_pysftp__):
6591 return upload_file_from_pysftp_file(url);
6592 else:
6593 return upload_file_from_sftp_file(url);
6594 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6595 return False;
6596 ftp_port = urlparts.port;
6597 if(urlparts.port is None):
6598 ftp_port = 21;
6599 try:
6600 ftp.connect(urlparts.hostname, ftp_port);
6601 except socket.gaierror:
6602 log.info("Error With URL "+url);
6603 return False;
6604 except socket.timeout:
6605 log.info("Error With URL "+url);
6606 return False;
6607 ftp.login(urlparts.username, urlparts.password);
6608 if(urlparts.scheme=="ftps"):
6609 ftp.prot_p();
6610 ftp.storbinary("STOR "+urlparts.path, ftpfile);
6611 ftp.close();
6612 ftpfile.seek(0, 0);
6613 return ftpfile;
6615 def upload_file_to_ftp_string(ftpstring, url):
6616 ftpfileo = BytesIO(ftpstring);
6617 ftpfile = upload_file_to_ftp_file(ftpfileo, url);
6618 ftpfileo.close();
6619 return ftpfile;
6621 def download_file_from_http_file(url, headers=geturls_headers_pycatfile_python_alt):
6622 # Parse the URL to extract username and password if present
6623 urlparts = urlparse(url);
6624 username = urlparts.username;
6625 password = urlparts.password;
6626 # Rebuild the URL without the username and password
6627 netloc = urlparts.hostname;
6628 if(urlparts.scheme=="sftp"):
6629 if(__use_pysftp__):
6630 return download_file_from_pysftp_file(url);
6631 else:
6632 return download_file_from_sftp_file(url);
6633 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6634 return download_file_from_ftp_file(url);
6635 if urlparts.port:
6636 netloc += ':' + str(urlparts.port);
6637 rebuilt_url = urlunparse((urlparts.scheme, netloc, urlparts.path, urlparts.params, urlparts.query, urlparts.fragment));
6638 # Create a temporary file object
6639 temp_file = BytesIO();
6640 if haverequests:
6641 # Use the requests library if available
6642 if username and password:
6643 response = requests.get(rebuilt_url, headers=headers, auth=(username, password), stream=True);
6644 else:
6645 response = requests.get(rebuilt_url, headers=headers, stream=True);
6646 response.raw.decode_content = True
6647 shutil.copyfileobj(response.raw, temp_file);
6648 else:
6649 # Build a Request object for urllib
6650 request = Request(rebuilt_url, headers=headers);
6651 # Create an opener object for handling URLs
6652 if username and password:
6653 # Create a password manager
6654 password_mgr = HTTPPasswordMgrWithDefaultRealm();
6655 # Add the username and password
6656 password_mgr.add_password(None, rebuilt_url, username, password);
6657 # Create an authentication handler using the password manager
6658 auth_handler = HTTPBasicAuthHandler(password_mgr);
6659 # Build the opener with the authentication handler
6660 opener = build_opener(auth_handler);
6661 else:
6662 opener = build_opener();
6663 with opener.open(request) as response:
6664 shutil.copyfileobj(response, temp_file);
6665 # Reset file pointer to the start
6666 temp_file.seek(0);
6667 # Return the temporary file object
6668 return temp_file;
6670 def download_file_from_http_string(url, headers=geturls_headers_pycatfile_python_alt):
6671 httpfile = download_file_from_http_file(url, headers);
6672 return ftpfile.read();
6674 if(haveparamiko):
6675 def download_file_from_sftp_file(url):
6676 urlparts = urlparse(url);
6677 file_name = os.path.basename(urlparts.path);
6678 file_dir = os.path.dirname(urlparts.path);
6679 sftp_port = urlparts.port;
6680 if(urlparts.port is None):
6681 sftp_port = 22;
6682 else:
6683 sftp_port = urlparts.port;
6684 if(urlparts.username is not None):
6685 sftp_username = urlparts.username;
6686 else:
6687 sftp_username = "anonymous";
6688 if(urlparts.password is not None):
6689 sftp_password = urlparts.password;
6690 elif(urlparts.password is None and urlparts.username=="anonymous"):
6691 sftp_password = "anonymous";
6692 else:
6693 sftp_password = "";
6694 if(urlparts.scheme=="ftp"):
6695 return download_file_from_ftp_file(url);
6696 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6697 return download_file_from_http_file(url);
6698 if(urlparts.scheme!="sftp"):
6699 return False;
6700 ssh = paramiko.SSHClient();
6701 ssh.load_system_host_keys();
6702 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
6703 try:
6704 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6705 except paramiko.ssh_exception.SSHException:
6706 return False;
6707 except socket.gaierror:
6708 log.info("Error With URL "+url);
6709 return False;
6710 except socket.timeout:
6711 log.info("Error With URL "+url);
6712 return False;
6713 sftp = ssh.open_sftp();
6714 sftpfile = BytesIO();
6715 sftp.getfo(urlparts.path, sftpfile);
6716 sftp.close();
6717 ssh.close();
6718 sftpfile.seek(0, 0);
6719 return sftpfile;
6720 else:
6721 def download_file_from_sftp_file(url):
6722 return False;
6724 if(haveparamiko):
6725 def download_file_from_sftp_string(url):
6726 sftpfile = download_file_from_sftp_file(url);
6727 return sftpfile.read();
6728 else:
6729 def download_file_from_ftp_string(url):
6730 return False;
6732 if(haveparamiko):
6733 def upload_file_to_sftp_file(sftpfile, url):
6734 urlparts = urlparse(url);
6735 file_name = os.path.basename(urlparts.path);
6736 file_dir = os.path.dirname(urlparts.path);
6737 sftp_port = urlparts.port;
6738 if(urlparts.port is None):
6739 sftp_port = 22;
6740 else:
6741 sftp_port = urlparts.port;
6742 if(urlparts.username is not None):
6743 sftp_username = urlparts.username;
6744 else:
6745 sftp_username = "anonymous";
6746 if(urlparts.password is not None):
6747 sftp_password = urlparts.password;
6748 elif(urlparts.password is None and urlparts.username=="anonymous"):
6749 sftp_password = "anonymous";
6750 else:
6751 sftp_password = "";
6752 if(urlparts.scheme=="ftp"):
6753 return upload_file_from_ftp_file(url);
6754 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6755 return False;
6756 if(urlparts.scheme!="sftp"):
6757 return False;
6758 ssh = paramiko.SSHClient();
6759 ssh.load_system_host_keys();
6760 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
6761 try:
6762 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6763 except paramiko.ssh_exception.SSHException:
6764 return False;
6765 except socket.gaierror:
6766 log.info("Error With URL "+url);
6767 return False;
6768 except socket.timeout:
6769 log.info("Error With URL "+url);
6770 return False;
6771 sftp = ssh.open_sftp();
6772 sftp.putfo(sftpfile, urlparts.path);
6773 sftp.close();
6774 ssh.close();
6775 sftpfile.seek(0, 0);
6776 return sftpfile;
6777 else:
6778 def upload_file_to_sftp_file(sftpfile, url):
6779 return False;
6781 if(haveparamiko):
6782 def upload_file_to_sftp_string(sftpstring, url):
6783 sftpfileo = BytesIO(sftpstring);
6784 sftpfile = upload_file_to_sftp_files(ftpfileo, url);
6785 sftpfileo.close();
6786 return sftpfile;
6787 else:
6788 def upload_file_to_sftp_string(url):
6789 return False;
6791 if(havepysftp):
6792 def download_file_from_pysftp_file(url):
6793 urlparts = urlparse(url);
6794 file_name = os.path.basename(urlparts.path);
6795 file_dir = os.path.dirname(urlparts.path);
6796 sftp_port = urlparts.port;
6797 if(urlparts.port is None):
6798 sftp_port = 22;
6799 else:
6800 sftp_port = urlparts.port;
6801 if(urlparts.username is not None):
6802 sftp_username = urlparts.username;
6803 else:
6804 sftp_username = "anonymous";
6805 if(urlparts.password is not None):
6806 sftp_password = urlparts.password;
6807 elif(urlparts.password is None and urlparts.username=="anonymous"):
6808 sftp_password = "anonymous";
6809 else:
6810 sftp_password = "";
6811 if(urlparts.scheme=="ftp"):
6812 return download_file_from_ftp_file(url);
6813 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6814 return download_file_from_http_file(url);
6815 if(urlparts.scheme!="sftp"):
6816 return False;
6817 try:
6818 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6819 except paramiko.ssh_exception.SSHException:
6820 return False;
6821 except socket.gaierror:
6822 log.info("Error With URL "+url);
6823 return False;
6824 except socket.timeout:
6825 log.info("Error With URL "+url);
6826 return False;
6827 sftp = ssh.open_sftp();
6828 sftpfile = BytesIO();
6829 sftp.getfo(urlparts.path, sftpfile);
6830 sftp.close();
6831 ssh.close();
6832 sftpfile.seek(0, 0);
6833 return sftpfile;
6834 else:
6835 def download_file_from_pysftp_file(url):
6836 return False;
6838 if(havepysftp):
6839 def download_file_from_pysftp_string(url):
6840 sftpfile = download_file_from_pysftp_file(url);
6841 return sftpfile.read();
6842 else:
6843 def download_file_from_ftp_string(url):
6844 return False;
6846 if(havepysftp):
6847 def upload_file_to_pysftp_file(sftpfile, url):
6848 urlparts = urlparse(url);
6849 file_name = os.path.basename(urlparts.path);
6850 file_dir = os.path.dirname(urlparts.path);
6851 sftp_port = urlparts.port;
6852 if(urlparts.port is None):
6853 sftp_port = 22;
6854 else:
6855 sftp_port = urlparts.port;
6856 if(urlparts.username is not None):
6857 sftp_username = urlparts.username;
6858 else:
6859 sftp_username = "anonymous";
6860 if(urlparts.password is not None):
6861 sftp_password = urlparts.password;
6862 elif(urlparts.password is None and urlparts.username=="anonymous"):
6863 sftp_password = "anonymous";
6864 else:
6865 sftp_password = "";
6866 if(urlparts.scheme=="ftp"):
6867 return upload_file_from_ftp_file(url);
6868 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6869 return False;
6870 if(urlparts.scheme!="sftp"):
6871 return False;
6872 try:
6873 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6874 except paramiko.ssh_exception.SSHException:
6875 return False;
6876 except socket.gaierror:
6877 log.info("Error With URL "+url);
6878 return False;
6879 except socket.timeout:
6880 log.info("Error With URL "+url);
6881 return False;
6882 sftp = ssh.open_sftp();
6883 sftp.putfo(sftpfile, urlparts.path);
6884 sftp.close();
6885 ssh.close();
6886 sftpfile.seek(0, 0);
6887 return sftpfile;
6888 else:
6889 def upload_file_to_pysftp_file(sftpfile, url):
6890 return False;
6892 if(havepysftp):
6893 def upload_file_to_pysftp_string(sftpstring, url):
6894 sftpfileo = BytesIO(sftpstring);
6895 sftpfile = upload_file_to_pysftp_files(ftpfileo, url);
6896 sftpfileo.close();
6897 return sftpfile;
6898 else:
6899 def upload_file_to_pysftp_string(url):
6900 return False;
6902 def download_file_from_internet_file(url, headers=geturls_headers_pycatfile_python_alt):
6903 urlparts = urlparse(url);
6904 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6905 return download_file_from_http_file(url, headers);
6906 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6907 return download_file_from_ftp_file(url);
6908 elif(urlparts.scheme=="sftp"):
6909 if(__use_pysftp__ and havepysftp):
6910 return download_file_from_pysftp_file(url);
6911 else:
6912 return download_file_from_sftp_file(url);
6913 else:
6914 return False;
6915 return False;
6917 def download_file_from_internet_string(url, headers=geturls_headers_pycatfile_python_alt):
6918 urlparts = urlparse(url);
6919 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6920 return download_file_from_http_string(url, headers);
6921 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6922 return download_file_from_ftp_string(url);
6923 elif(urlparts.scheme=="sftp"):
6924 if(__use_pysftp__ and havepysftp):
6925 return download_file_from_pysftp_string(url);
6926 else:
6927 return download_file_from_sftp_string(url);
6928 else:
6929 return False;
6930 return False;
6932 def upload_file_to_internet_file(ifp, url):
6933 urlparts = urlparse(url);
6934 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6935 return False;
6936 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6937 return upload_file_from_ftp_file(ifp, url);
6938 elif(urlparts.scheme=="sftp"):
6939 if(__use_pysftp__ and havepysftp):
6940 return upload_file_from_pysftp_file(ifp, url);
6941 else:
6942 return download_file_from_sftp_file(ifp, url);
6943 else:
6944 return False;
6945 return False;
6947 def upload_file_to_internet_string(ifp, url):
6948 urlparts = urlparse(url);
6949 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6950 return False;
6951 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6952 return upload_file_from_ftp_string(ifp, url);
6953 elif(urlparts.scheme=="sftp"):
6954 if(__use_pysftp__ and havepysftp):
6955 return upload_file_from_pysftp_string(ifp, url);
6956 else:
6957 return download_file_from_sftp_string(ifp, url);
6958 else:
6959 return False;
6960 return False;
6962 try:
6963 if(hasattr(shutil, "register_archive_format")):
6964 # Register the packing format
6965 shutil.register_archive_format(__file_format_name__, PackArchiveFileFunc, description='Pack concatenated files');
6966 except shutil.RegistryError:
6967 pass;
6969 try:
6970 if(hasattr(shutil, "register_unpack_format")):
6971 # Register the unpacking format
6972 shutil.register_unpack_format(__file_format_name__, archivefile_extensions, UnPackArchiveFileFunc, description='UnPack concatenated files');
6973 except shutil.RegistryError:
6974 pass;