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