Add files via upload
[PyCatFile.git] / pycatfile.py
blobb70621200491fa3848a2aa6846179b1d4619f34a
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/8/2024 Ver. 0.3.4 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, hashlib, datetime, logging, binascii, tempfile, zipfile, ftplib;
23 if os.name == 'nt': # Only modify if on Windows
24 if sys.version[0] == "2":
25 import codecs;
26 sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
27 sys.stderr = codecs.getwriter('utf-8')(sys.stderr);
28 else:
29 import io;
30 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace', line_buffering=True);
31 sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace', line_buffering=True);
33 hashlib_guaranteed = False;
34 os.environ["PYTHONIOENCODING"] = "UTF-8";
35 os.environ["LC_CTYPE"] = "UTF-8";
36 try:
37 reload(sys);
38 try:
39 sys.setdefaultencoding('UTF-8');
40 except NameError:
41 pass;
42 except AttributeError:
43 pass;
44 except NameError:
45 pass;
46 except AttributeError:
47 pass;
49 try:
50 import simplejson as json;
51 except ImportError:
52 import json;
54 try:
55 from zlib import crc32;
56 except ImportError:
57 from binascii import crc32;
59 rarfile_support = False;
60 try:
61 import rarfile;
62 rarfile_support = True;
63 except ImportError:
64 rarfile_support = False;
66 try:
67 from safetar import is_tarfile;
68 except ImportError:
69 try:
70 from xtarfile import is_tarfile;
71 except ImportError:
72 from tarfile import is_tarfile;
74 try:
75 import safetar as tarfile;
76 except ImportError:
77 try:
78 import xtarfile as tarfile;
79 except ImportError:
80 import tarfile;
82 if(sys.version[0]=="2"):
83 try:
84 from io import StringIO, BytesIO;
85 except ImportError:
86 try:
87 from cStringIO import StringIO;
88 from cStringIO import StringIO as BytesIO;
89 except ImportError:
90 from StringIO import StringIO;
91 from StringIO import StringIO as BytesIO;
92 elif(sys.version[0]>="3"):
93 from io import StringIO, BytesIO;
94 else:
95 teststringio = 0;
96 if(teststringio<=0):
97 try:
98 from cStringIO import StringIO as BytesIO;
99 teststringio = 1;
100 except ImportError:
101 teststringio = 0;
102 if(teststringio<=0):
103 try:
104 from StringIO import StringIO as BytesIO;
105 teststringio = 2;
106 except ImportError:
107 teststringio = 0;
108 if(teststringio<=0):
109 try:
110 from io import BytesIO;
111 teststringio = 3;
112 except ImportError:
113 teststringio = 0;
115 __file_format_name__ = "CatFile";
116 __program_name__ = "Py"+__file_format_name__;
117 __file_format_lower__ = __file_format_name__.lower();
118 __file_format_len__ = len(__file_format_name__);
119 __file_format_hex__ = binascii.hexlify(__file_format_name__.encode("UTF-8")).decode("UTF-8");
120 __file_format_delimiter__ = "\x00";
121 __file_format_ver__ = "001";
122 __file_format_list__ = [__file_format_name__, __file_format_lower__, __file_format_len__, __file_format_hex__, __file_format_delimiter__, __file_format_ver__];
123 __project__ = __program_name__;
124 __project_url__ = "https://github.com/GameMaker2k/PyCatFile";
125 __version_info__ = (0, 3, 4, "RC 1", 1);
126 __version_date_info__ = (2024, 3, 3, "RC 1", 1);
127 __version_date__ = str(__version_date_info__[0]) + "." + str(__version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2);
128 __revision__ = __version_info__[3];
129 __revision_id__ = "$Id$";
130 if(__version_info__[4] is not None):
131 __version_date_plusrc__ = __version_date__ + "-" + str(__version_date_info__[4]);
132 if(__version_info__[4] is None):
133 __version_date_plusrc__ = __version_date__;
134 if(__version_info__[3] is not None):
135 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]) + " " + str(__version_info__[3]);
136 if(__version_info__[3] is None):
137 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]);
139 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
140 outextlist = ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
141 outextlistwd = ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
143 tarfile_mimetype = "application/tar";
144 tarfile_tar_mimetype = tarfile_mimetype;
145 zipfile_mimetype = "application/zip";
146 zipfile_zip_mimetype = zipfile_mimetype;
147 rarfile_mimetype = "application/rar";
148 rarfile_rar_mimetype = rarfile_mimetype;
149 archivefile_mimetype = "application/x-"+__file_format_list__[1]+"";
150 archivefile_cat_mimetype = archivefile_mimetype;
151 archivefile_gzip_mimetype = "application/x-"+__file_format_list__[1]+"+gzip";
152 archivefile_gz_mimetype = archivefile_gzip_mimetype;
153 archivefile_bzip2_mimetype = "application/x-"+__file_format_list__[1]+"+bzip2";
154 archivefile_bz2_mimetype = archivefile_bzip2_mimetype;
155 archivefile_lz4_mimetype = "application/x-"+__file_format_list__[1]+"+lz4";
156 archivefile_lzop_mimetype = "application/x-"+__file_format_list__[1]+"+lzop";
157 archivefile_lzo_mimetype = archivefile_lzop_mimetype;
158 archivefile_zstandard_mimetype = "application/x-"+__file_format_list__[1]+"+zstandard";
159 archivefile_zstd_mimetype = archivefile_zstandard_mimetype;
160 archivefile_lzma_mimetype = "application/x-"+__file_format_list__[1]+"+lzma";
161 archivefile_xz_mimetype = "application/x-"+__file_format_list__[1]+"+xz";
162 archivefile_extensions = ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
164 if __name__ == "__main__":
165 import subprocess;
166 curscrpath = os.path.dirname(sys.argv[0]);
167 if(curscrpath==""):
168 curscrpath = ".";
169 if(os.sep=="\\"):
170 curscrpath = curscrpath.replace(os.sep, "/");
171 curscrpath = curscrpath + "/";
172 scrfile = curscrpath + "catfile.py";
173 if(os.path.exists(scrfile) and os.path.isfile(scrfile)):
174 scrcmd = subprocess.Popen([sys.executable, scrfile] + sys.argv[1:]);
175 scrcmd.wait();
177 def VerbosePrintOut(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
178 if(not dbgenable):
179 return True;
180 log_functions = {
181 "print": print,
182 "log": logging.info,
183 "warning": logging.warning,
184 "error": logging.error,
185 "critical": logging.critical,
186 "exception": logging.exception,
187 "logalt": lambda x: logging.log(dgblevel, x),
188 "debug": logging.debug
190 log_function = log_functions.get(outtype);
191 if(log_function):
192 log_function(dbgtxt);
193 return True;
194 return False;
196 def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
197 VerbosePrintOut(dbgtxt, outtype, dbgenable, dgblevel);
198 return dbgtxt;
200 def RemoveWindowsPath(dpath):
201 if(dpath is None):
202 dpath = "";
203 if(os.sep!="/"):
204 dpath = dpath.replace(os.path.sep, "/");
205 dpath = dpath.rstrip("/");
206 if(dpath=="." or dpath==".."):
207 dpath = dpath + "/";
208 return dpath;
210 def NormalizeRelativePath(inpath):
211 inpath = RemoveWindowsPath(inpath);
212 if(os.path.isabs(inpath)):
213 outpath = inpath;
214 else:
215 if(inpath.startswith("./") or inpath.startswith("../")):
216 outpath = inpath;
217 else:
218 outpath = "./" + inpath;
219 return outpath;
221 def ListDir(dirpath, followlink=False, duplicates=False):
222 if(isinstance(dirpath, (list, tuple, ))):
223 dirpath = list(filter(None, dirpath));
224 elif(isinstance(dirpath, (str, ))):
225 dirpath = list(filter(None, [dirpath]));
226 retlist = [];
227 for mydirfile in dirpath:
228 if(not os.path.exists(mydirfile)):
229 return False;
230 mydirfile = NormalizeRelativePath(mydirfile);
231 if(os.path.exists(mydirfile) and os.path.islink(mydirfile)):
232 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile));
233 if(os.path.exists(mydirfile) and os.path.isdir(mydirfile)):
234 for root, dirs, filenames in os.walk(mydirfile):
235 dpath = root;
236 dpath = RemoveWindowsPath(dpath);
237 if(dpath not in retlist and not duplicates):
238 retlist.append(dpath);
239 if(duplicates):
240 retlist.append(dpath);
241 for file in filenames:
242 fpath = os.path.join(root, file);
243 fpath = RemoveWindowsPath(fpath);
244 if(fpath not in retlist and not duplicates):
245 retlist.append(fpath);
246 if(duplicates):
247 retlist.append(fpath);
248 else:
249 retlist.append(RemoveWindowsPath(mydirfile));
250 return retlist;
252 def ListDirAdvanced(dirpath, followlink=False, duplicates=False):
253 if isinstance(dirpath, (list, tuple)):
254 dirpath = list(filter(None, dirpath));
255 elif isinstance(dirpath, str):
256 dirpath = list(filter(None, [dirpath]));
257 retlist = []
258 for mydirfile in dirpath:
259 if not os.path.exists(mydirfile):
260 return False;
261 mydirfile = NormalizeRelativePath(mydirfile);
262 if os.path.exists(mydirfile) and os.path.islink(mydirfile) and followlink:
263 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile))
264 if os.path.exists(mydirfile) and os.path.isdir(mydirfile):
265 for root, dirs, filenames in os.walk(mydirfile):
266 # Sort dirs and filenames alphabetically in place
267 dirs.sort(key=lambda x: x.lower());
268 filenames.sort(key=lambda x: x.lower());
269 dpath = RemoveWindowsPath(root);
270 if not duplicates and dpath not in retlist:
271 retlist.append(dpath);
272 elif duplicates:
273 retlist.append(dpath);
274 for file in filenames:
275 fpath = os.path.join(root, file);
276 fpath = RemoveWindowsPath(fpath);
277 if not duplicates and fpath not in retlist:
278 retlist.append(fpath);
279 elif duplicates:
280 retlist.append(fpath);
281 else:
282 retlist.append(RemoveWindowsPath(mydirfile));
283 return retlist;
285 def create_alias_function(prefix, base_name, suffix, target_function):
286 # Define a new function that wraps the target function
287 def alias_function(*args, **kwargs):
288 return target_function(*args, **kwargs);
290 # Create the function name by combining the prefix, base name, and the suffix
291 function_name = "{}{}{}".format(prefix, base_name, suffix);
293 # Add the new function to the global namespace
294 globals()[function_name] = alias_function;
296 # initial_value can be 0xFFFF or 0x0000
297 def crc16_ansi(msg, initial_value=0xFFFF):
298 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
299 poly = 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
300 crc = initial_value; # Initial value
301 for b in msg:
302 crc ^= b << 8; # XOR byte into CRC top byte
303 for _ in range(8): # Process each bit
304 if crc & 0x8000: # If the top bit is set
305 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
306 else:
307 crc = crc << 1; # Just shift left
308 crc &= 0xFFFF; # Ensure CRC remains 16-bit
309 return crc;
311 # initial_value can be 0xFFFF or 0x0000
312 def crc16_ibm(msg, initial_value=0xFFFF):
313 return crc16_ansi(msg, initial_value);
315 # initial_value is 0xFFFF
316 def crc16(msg):
317 return crc16_ansi(msg, 0xFFFF);
319 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
320 def crc16_ccitt(msg, initial_value=0xFFFF):
321 # CRC-16-CCITT polynomial
322 poly = 0x1021; # Polynomial for CRC-16-CCITT
323 # Use the specified initial value
324 crc = initial_value;
325 for b in msg:
326 crc ^= b << 8; # XOR byte into CRC top byte
327 for _ in range(8): # Process each bit
328 if crc & 0x8000: # If the top bit is set
329 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
330 else:
331 crc = crc << 1; # Just shift left
332 crc &= 0xFFFF; # Ensure CRC remains 16-bit
333 return crc;
335 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
336 def crc64_ecma(msg, initial_value=0x0000000000000000):
337 # CRC-64-ECMA polynomial and initial value
338 poly = 0x42F0E1EBA9EA3693;
339 crc = initial_value; # Initial value for CRC-64-ECMA
340 for b in msg:
341 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
342 for _ in range(8): # Process each bit
343 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
344 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
345 else:
346 crc <<= 1; # Just shift left if the MSB is 0
347 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
348 return crc;
350 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
351 def crc64_iso(msg, initial_value=0xFFFFFFFFFFFFFFFF):
352 # CRC-64-ISO polynomial and initial value
353 poly = 0x000000000000001B;
354 crc = initial_value; # Common initial value for CRC-64-ISO
355 for b in msg:
356 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
357 for _ in range(8): # Process each bit
358 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
359 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
360 else:
361 crc <<= 1; # Just shift left if the MSB is 0
362 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
363 return crc;
365 def ReadTillNullByte(fp, delimiter=__file_format_delimiter__):
366 curbyte = b"";
367 curfullbyte = b"";
368 nullbyte = delimiter.encode("UTF-8");
369 while(True):
370 curbyte = fp.read(1);
371 if(curbyte==nullbyte or not curbyte):
372 break;
373 curfullbyte = curfullbyte + curbyte;
374 return curfullbyte.decode('UTF-8');
376 def ReadUntilNullByte(fp, delimiter=__file_format_delimiter__):
377 return ReadTillNullByte(fp, delimiter);
379 def SeekToEndOfFile(fp):
380 lasttell = 0;
381 while(True):
382 fp.seek(1, 1);
383 if(lasttell==fp.tell()):
384 break;
385 lasttell = fp.tell();
386 return True;
388 def ReadFileHeaderData(fp, rounds=0, delimiter=__file_format_delimiter__):
389 rocount = 0;
390 roend = int(rounds);
391 HeaderOut = [];
392 while(rocount<roend):
393 HeaderOut.append(ReadTillNullByte(fp, delimiter));
394 rocount = rocount + 1;
395 return HeaderOut;
397 def ReadFileHeaderDataBySize(fp, delimiter=__file_format_delimiter__):
398 headerpresize = ReadTillNullByte(fp, delimiter);
399 headersize = int(headerpresize, 16);
400 headercontent = str(fp.read(headersize).decode('UTF-8')).split(delimiter);
401 fp.seek(1, 1);
402 rocount = 0;
403 roend = int(len(headercontent));
404 HeaderOut = [headerpresize];
405 while(rocount<roend):
406 HeaderOut.append(headercontent[rocount]);
407 rocount = rocount + 1;
408 return HeaderOut;
410 def ReadFileHeaderDataByList(fp, listval=[], delimiter=__file_format_delimiter__):
411 rocount = 0;
412 roend = int(len(listval));
413 HeaderOut = {};
414 while(rocount<roend):
415 RoundArray = {listval[rocount]: ReadTillNullByte(fp, delimiter)};
416 HeaderOut.update(RoundArray);
417 rocount = rocount + 1;
418 return HeaderOut;
420 def ReadFileHeaderDataByListSize(fp, listval=[], delimiter=__file_format_delimiter__):
421 headerpresize = ReadTillNullByte(fp, delimiter);
422 headersize = int(headerpresize, 16);
423 headercontent = str(fp.read(headersize).decode('UTF-8')).split(delimiter);
424 fp.seek(1, 1);
425 rocount = 0;
426 listcount = 1;
427 roend = int(len(headercontent));
428 HeaderOut = {listval[0]: headerpresize};
429 while(rocount<roend):
430 RoundArray = {listval[rocount]: headercontent[rocount]};
431 HeaderOut.update(RoundArray);
432 rocount = rocount + 1;
433 listcount = listcount + 1;
434 return HeaderOut;
436 def AppendNullByte(indata, delimiter=__file_format_delimiter__):
437 outdata = str(indata) + delimiter;
438 return outdata;
440 def AppendNullBytes(indata=[], delimiter=__file_format_delimiter__):
441 outdata = "";
442 inum = 0;
443 il = len(indata);
444 while(inum < il):
445 outdata = outdata + AppendNullByte(indata[inum], delimiter);
446 inum = inum + 1;
447 return outdata;
449 def ReadTillNullByteAlt(fp, delimiter=__file_format_delimiter__):
450 """Read bytes from file pointer until a null byte is encountered."""
451 bytes_list = [] # Use list for efficient append operation.
452 while True:
453 cur_byte = fp.read(1);
454 if cur_byte == delimiter.encode() or not cur_byte:
455 break;
456 bytes_list.append(cur_byte);
457 return b''.join(bytes_list).decode('UTF-8');
459 def ReadUntilNullByteAlt(fp, delimiter=__file_format_delimiter__):
460 return ReadTillNullByteAlt(fp, delimiter);
462 def ReadFileHeaderDataAlt(fp, rounds=0, delimiter=__file_format_delimiter__):
463 """Read multiple null-byte terminated strings from a file."""
464 header_out = [];
465 for round_count in range(rounds):
466 header_out[round_count] = ReadTillNullByteAlt(fp, delimiter);
467 return header_out;
469 def ReadFileHeaderDataBySizeAlt(fp, delimiter=__file_format_delimiter__):
470 # Read and convert header size from hexadecimal to integer
471 header_pre_size = ReadTillNullByte(fp, delimiter);
472 header_size = int(header_pre_size, 16);
473 # Read and split the header content
474 header_content = str(fp.read(header_size).decode('UTF-8')).split(delimiter);
475 fp.seek(1, 1);
476 # Prepend the pre-size and return the combined list
477 return [header_pre_size] + header_content;
479 def ReadFileHeaderDataByListSizeAlt(fp, listval=[], delimiter=__file_format_delimiter__):
480 # Read the size and content from the header
481 header_pre_size = ReadTillNullByte(fp, delimiter);
482 header_size = int(header_pre_size, 16);
483 header_content = str(fp.read(header_size).decode('UTF-8')).split(delimiter);
484 fp.seek(1, 1);
485 # Initialize HeaderOut with the header pre-size if listval is not empty
486 HeaderOut = {listval[0]: header_pre_size} if listval else {};
487 # Map the remaining listval items to their corresponding header content, starting from the second item
488 for i in range(1, min(len(header_content) + 1, len(listval))):
489 HeaderOut[listval[i]] = header_content[i - 1]; # -1 because header_content is 0-indexed
490 return HeaderOut;
492 def ReadFileHeaderDataByListAlt(fp, listval=[], delimiter=__file_format_delimiter__):
493 """Read multiple null-byte terminated strings from a file."""
494 header_out = {};
495 for round_count in listval:
496 header_out.append(ReadTillNullByteAlt(fp, delimiter));
497 return header_out;
499 def AppendNullByteAlt(indata, delimiter=__file_format_delimiter__):
500 """Append a null byte to the given data."""
501 return str(indata) + delimiter;
503 def AppendNullBytesAlt(indata=[], delimiter=__file_format_delimiter__):
504 """Append a null byte to each element in the list and concatenate."""
505 return delimiter.join(map(str, indata)) + delimiter; # Efficient concatenation with null byte.
507 def PrintPermissionString(fchmode, ftype):
508 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' } };
509 permissionstr = "";
510 for fmodval in str(oct(fchmode))[-3:]:
511 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
512 if(ftype==0 or ftype==7):
513 permissionstr = "-" + permissionstr;
514 if(ftype==1):
515 permissionstr = "h" + permissionstr;
516 if(ftype==2):
517 permissionstr = "l" + permissionstr;
518 if(ftype==3):
519 permissionstr = "c" + permissionstr;
520 if(ftype==4):
521 permissionstr = "b" + permissionstr;
522 if(ftype==5):
523 permissionstr = "d" + permissionstr;
524 if(ftype==6):
525 permissionstr = "f" + permissionstr;
526 if(ftype==8):
527 permissionstr = "D" + permissionstr;
528 if(ftype==9):
529 permissionstr = "p" + permissionstr;
530 if(ftype==10):
531 permissionstr = "w" + permissionstr;
532 try:
533 permissionoutstr = stat.filemode(fchmode);
534 except AttributeError:
535 permissionoutstr = permissionstr;
536 except KeyError:
537 permissionoutstr = permissionstr;
538 return permissionoutstr;
540 def PrintPermissionStringAlt(fchmode, ftype):
541 permissions = {
542 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
543 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
545 # Translate file mode into permission string
546 permissionstr = ''.join([permissions[i] for i in str(oct(fchmode))[-3:]]);
547 # Append file type indicator
548 type_indicators = {
549 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
550 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
552 file_type = type_indicators.get(ftype, '-');
553 permissionstr = file_type + permissionstr;
554 try:
555 permissionoutstr = stat.filemode(fchmode);
556 except AttributeError:
557 permissionoutstr = permissionstr;
558 return permissionoutstr;
560 def CompressionSupport():
561 compression_list = [];
562 try:
563 import gzip;
564 compression_list.append("gz");
565 compression_list.append("gzip");
566 except ImportError:
567 '''return False;'''
568 try:
569 import bz2;
570 compression_list.append("bz2");
571 compression_list.append("bzip2");
572 except ImportError:
573 '''return False;'''
574 try:
575 import lz4;
576 compression_list.append("lz4");
577 except ImportError:
578 '''return False;'''
579 try:
580 import lzo;
581 compression_list.append("lzo");
582 compression_list.append("lzop");
583 except ImportError:
584 '''return False;'''
585 try:
586 import zstandard;
587 compression_list.append("zstd");
588 compression_list.append("zstandard");
589 except ImportError:
590 '''return False;'''
591 try:
592 import lzma;
593 compression_list.append("lzma");
594 compression_list.append("xz");
595 except ImportError:
596 '''return False;'''
597 return compression_list;
599 def CheckCompressionType(infile, formatspecs=__file_format_list__, closefp=True):
600 if(hasattr(infile, "read") or hasattr(infile, "write")):
601 catfp = infile;
602 else:
603 try:
604 catfp = open(infile, "rb");
605 except FileNotFoundError:
606 return False;
607 catfp.seek(0, 0);
608 prefp = catfp.read(2);
609 filetype = False;
610 if(prefp==binascii.unhexlify("1f8b")):
611 filetype = "gzip";
612 catfp.seek(0, 0);
613 prefp = catfp.read(3);
614 if(prefp==binascii.unhexlify("425a68")):
615 filetype = "bzip2";
616 if(prefp==binascii.unhexlify("5d0000")):
617 filetype = "lzma";
618 catfp.seek(0, 0);
619 prefp = catfp.read(4);
620 if(prefp==binascii.unhexlify("28b52ffd")):
621 filetype = "zstd";
622 if(prefp==binascii.unhexlify("04224d18")):
623 filetype = "lz4";
624 if(prefp==binascii.unhexlify("504B0304")):
625 filetype = "zipfile";
626 catfp.seek(0, 0);
627 prefp = catfp.read(5);
628 if(prefp==binascii.unhexlify("7573746172")):
629 filetype = "tarfile";
630 catfp.seek(0, 0);
631 prefp = catfp.read(6);
632 if(prefp==binascii.unhexlify("fd377a585a00")):
633 filetype = "lzma";
634 catfp.seek(0, 0);
635 prefp = catfp.read(7);
636 if(prefp==binascii.unhexlify("526172211a0700")):
637 filetype = "rarfile";
638 if(prefp==binascii.unhexlify("43617446696c65")):
639 filetype = "catfile";
640 catfp.seek(0, 0);
641 prefp = catfp.read(8);
642 if(prefp==binascii.unhexlify("526172211a070100")):
643 filetype = "rarfile";
644 catfp.seek(0, 0);
645 prefp = catfp.read(formatspecs[2]);
646 if(prefp==binascii.unhexlify(formatspecs[3])):
647 filetype = formatspecs[1];
648 catfp.seek(0, 0);
649 prefp = catfp.read(9);
650 if(prefp==binascii.unhexlify("894c5a4f000d0a1a0a")):
651 filetype = "lzo";
652 catfp.seek(0, 0);
653 prefp = catfp.read(10);
654 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
655 filetype = "tarfile";
656 catfp.seek(0, 0);
657 if(closefp):
658 catfp.close();
659 return filetype;
661 def CheckCompressionTypeFromString(instring, formatspecs=__file_format_list__, closefp=True):
662 try:
663 instringsfile = BytesIO(instring);
664 except TypeError:
665 instringsfile = BytesIO(instring.encode("UTF-8"));
666 return CheckCompressionType(instringsfile, formatspecs, closefp);
668 def GetCompressionMimeType(infile, formatspecs=__file_format_list__):
669 compresscheck = CheckCompressionType(fp, formatspecs, False);
670 if(compresscheck=="gzip" or compresscheck=="gz"):
671 return archivefile_gzip_mimetype;
672 if(compresscheck=="bzip2" or compresscheck=="bz2"):
673 return archivefile_bzip2_mimetype;
674 if(compresscheck=="zstd" or compresscheck=="zstandard"):
675 return archivefile_zstandard_mimetype;
676 if(compresscheck=="lz4"):
677 return archivefile_lz4_mimetype;
678 if(compresscheck=="lzo" or compresscheck=="lzop"):
679 return archivefile_lzop_mimetype;
680 if(compresscheck=="lzma"):
681 return archivefile_lzma_mimetype;
682 if(compresscheck=="xz"):
683 return archivefile_xz_mimetype;
684 if(compresscheck=="catfile" or compresscheck=="cat" or compresscheck==formatspecs[1]):
685 return archivefile_cat_mimetype;
686 if(not compresscheck):
687 return False;
688 return False;
690 def UncompressArchiveFile(fp, formatspecs=__file_format_list__):
691 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
692 return False;
693 compresscheck = CheckCompressionType(fp, formatspecs, False);
694 if(compresscheck=="gzip"):
695 try:
696 import gzip;
697 except ImportError:
698 return False;
699 catfp = gzip.GzipFile(fileobj=fp, mode="rb");
700 if(compresscheck=="bzip2"):
701 try:
702 import bz2;
703 except ImportError:
704 return False;
705 catfp = BytesIO();
706 catfp.write(bz2.decompress(fp.read()));
707 if(compresscheck=="zstd"):
708 try:
709 import zstandard;
710 except ImportError:
711 return False;
712 catfp = BytesIO();
713 catfp.write(zstandard.decompress(fp.read()));
714 if(compresscheck=="lz4"):
715 try:
716 import lz4.frame;
717 except ImportError:
718 return False;
719 catfp = BytesIO();
720 catfp.write(lz4.frame.decompress(fp.read()));
721 if(compresscheck=="lzo" or compresscheck=="lzop"):
722 try:
723 import lzo;
724 except ImportError:
725 return False;
726 catfp = BytesIO();
727 catfp.write(lzo.decompress(fp.read()));
728 if(compresscheck=="lzma" or compresscheck=="xz"):
729 try:
730 import lzma;
731 except ImportError:
732 return False;
733 catfp = BytesIO();
734 catfp.write(lzma.decompress(fp.read()));
735 if(compresscheck=="catfile" or compresscheck==formatspecs[1]):
736 catfp = fp;
737 if(not compresscheck):
738 try:
739 import lzma;
740 except ImportError:
741 return False;
742 catfp = BytesIO();
743 with fp as fpcontent:
744 try:
745 catfp.write(lzma.decompress(fp.read()));
746 except lzma.LZMAError:
747 return False;
748 return catfp;
750 create_alias_function("Uncompress", __file_format_name__, "", UncompressArchiveFile);
752 def UncompressFile(infile, formatspecs=__file_format_list__, mode="rb"):
753 compresscheck = CheckCompressionType(infile, formatspecs, False);
754 if(sys.version_info[0]==2 and compresscheck):
755 if(mode=="rt"):
756 mode = "r";
757 if(mode=="wt"):
758 mode = "w";
759 try:
760 if(compresscheck=="gzip"):
761 try:
762 import gzip;
763 except ImportError:
764 return False;
765 try:
766 filefp = gzip.open(infile, mode, encoding="UTF-8");
767 except (ValueError, TypeError) as e:
768 filefp = gzip.open(infile, mode);
769 if(compresscheck=="bzip2"):
770 try:
771 import bz2;
772 except ImportError:
773 return False;
774 try:
775 filefp = bz2.open(infile, mode, encoding="UTF-8");
776 except (ValueError, TypeError) as e:
777 filefp = bz2.open(infile, mode);
778 if(compresscheck=="zstd"):
779 try:
780 import zstandard;
781 except ImportError:
782 return False;
783 try:
784 filefp = zstandard.open(infile, mode, encoding="UTF-8");
785 except (ValueError, TypeError) as e:
786 filefp = zstandard.open(infile, mode);
787 if(compresscheck=="lz4"):
788 try:
789 import lz4.frame;
790 except ImportError:
791 return False;
792 try:
793 filefp = lz4.frame.open(infile, mode, encoding="UTF-8");
794 except (ValueError, TypeError) as e:
795 filefp = lz4.frame.open(infile, mode);
796 if(compresscheck=="lzo"):
797 try:
798 import lzo;
799 except ImportError:
800 return False;
801 try:
802 filefp = lzo.open(infile, mode, encoding="UTF-8");
803 except (ValueError, TypeError) as e:
804 filefp = lzo.open(infile, mode);
805 if(compresscheck=="lzma"):
806 try:
807 import lzma;
808 except ImportError:
809 return False;
810 try:
811 filefp = lzma.open(infile, mode, encoding="UTF-8");
812 except (ValueError, TypeError) as e:
813 filefp = lzma.open(infile, mode);
814 if(compresscheck=="catfile" or compresscheck==formatspecs[1]):
815 try:
816 filefp = open(infile, mode, encoding="UTF-8");
817 except (ValueError, TypeError) as e:
818 filefp = open(infile, mode);
819 if(not compresscheck):
820 try:
821 filefp = open(infile, mode, encoding="UTF-8");
822 except (ValueError, TypeError) as e:
823 filefp = open(infile, mode);
824 except FileNotFoundError:
825 return False;
826 return filefp;
828 def UncompressString(infile):
829 compresscheck = CheckCompressionTypeFromString(infile, formatspecs, False);
830 if(compresscheck=="gzip"):
831 try:
832 import gzip;
833 except ImportError:
834 return False;
835 fileuz = gzip.decompress(infile);
836 if(compresscheck=="bzip2"):
837 try:
838 import bz2;
839 except ImportError:
840 return False;
841 fileuz = bz2.decompress(infile);
842 if(compresscheck=="zstd"):
843 try:
844 import zstandard;
845 except ImportError:
846 return False;
847 fileuz = zstandard.decompress(infile);
848 if(compresscheck=="lz4"):
849 try:
850 import lz4.frame;
851 except ImportError:
852 return False;
853 fileuz = lz4.frame.decompress(infile);
854 if(compresscheck=="lzo"):
855 try:
856 import lzo;
857 except ImportError:
858 return False;
859 fileuz = lzo.decompress(infile);
860 if(compresscheck=="lzma"):
861 try:
862 import lzma;
863 except ImportError:
864 return False;
865 fileuz = lzma.decompress(infile);
866 if(not compresscheck):
867 fileuz = infile;
868 if(hasattr(fileuz, 'decode')):
869 fileuz = fileuz.decode("UTF-8");
870 return fileuz;
872 def UncompressStringAlt(infile):
873 filefp = StringIO();
874 outstring = UncompressString(infile);
875 filefp.write(outstring);
876 filefp.seek(0);
877 return filefp;
879 def CheckCompressionSubType(infile, formatspecs=__file_format_list__):
880 compresscheck = CheckCompressionType(infile, formatspecs, False);
881 if(not compresscheck):
882 fextname = os.path.splitext(infile)[1];
883 if(fextname==".gz"):
884 compresscheck = "gzip";
885 if(fextname==".bz2"):
886 compresscheck = "bzip2";
887 if(fextname==".zst"):
888 compresscheck = "zstd";
889 if(fextname==".lz4"):
890 compresscheck = "lz4";
891 if(fextname==".lzo" or fextname==".lzop"):
892 compresscheck = "lzo";
893 if(fextname==".lzma" or fextname==".xz"):
894 compresscheck = "lzma";
895 if(not compresscheck):
896 return False;
897 if(compresscheck=="catfile"):
898 return "catfile";
899 if(compresscheck==formatspecs[1]):
900 return formatspecs[1];
901 if(compresscheck=="tarfile"):
902 return "tarfile";
903 if(compresscheck=="zipfile"):
904 return "zipfile";
905 if(hasattr(infile, "read") or hasattr(infile, "write")):
906 catfp = UncompressArchiveFile(infile, formatspecs[1]);
907 else:
908 try:
909 if(compresscheck=="gzip"):
910 try:
911 import gzip;
912 except ImportError:
913 return False;
914 catfp = gzip.GzipFile(infile, "rb");
915 if(compresscheck=="bzip2"):
916 try:
917 import bz2;
918 except ImportError:
919 return False;
920 catfp = bz2.BZ2File(infile, "rb");
921 if(compresscheck=="lz4"):
922 try:
923 import lz4.frame;
924 except ImportError:
925 return False;
926 catfp = lz4.frame.open(infile, "rb");
927 if(compresscheck=="zstd"):
928 try:
929 import zstandard;
930 except ImportError:
931 return False;
932 catfp = zstandard.open(infile, "rb");
933 if(compresscheck=="lzma" or compresscheck=="xz"):
934 try:
935 import lzma;
936 except ImportError:
937 return False;
938 catfp = lzma.open(infile, "rb");
939 except FileNotFoundError:
940 return False;
941 filetype = False;
942 prefp = catfp.read(5);
943 if(prefp==binascii.unhexlify("7573746172")):
944 filetype = "tarfile";
945 catfp.seek(0, 0);
946 prefp = catfp.read(7);
947 if(prefp==binascii.unhexlify("43617446696c65")):
948 filetype = "catfile";
949 catfp.seek(0, 0);
950 prefp = catfp.read(formatspecs[2]);
951 if(prefp==binascii.unhexlify(formatspecs[3])):
952 filetype = formatspecs[1];
953 catfp.seek(0, 0);
954 prefp = catfp.read(10);
955 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
956 filetype = "tarfile";
957 catfp.seek(0, 0);
958 catfp.close();
959 return filetype;
961 def GZipCompress(data, compresslevel=9):
962 try:
963 import gzip;
964 except ImportError:
965 return False;
966 tmpfp = tempfile.NamedTemporaryFile("wb", delete=False);
967 tmpfp.close();
968 tmpfp = gzip.GzipFile(tmpfp.name, mode="wb", compresslevel=compresslevel);
969 tmpfp.write(data);
970 tmpfp.close();
971 try:
972 catfp = open(tmpfp.name, "rb");
973 except FileNotFoundError:
974 return False;
975 catdata = catfp.read();
976 catfp.close();
977 return catdata;
979 def CompressArchiveFile(fp, compression="auto", compressionlevel=None, formatspecs=__file_format_list__):
980 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
981 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
982 return False;
983 fp.seek(0, 0);
984 if(not compression or compression or compression=="catfile" or compression==formatspecs[1]):
985 compression = None;
986 if(compression not in compressionlist and compression is None):
987 compression = "auto";
988 if(compression=="gzip"):
989 try:
990 import gzip;
991 except ImportError:
992 return False;
993 catfp = BytesIO();
994 if(compressionlevel is None):
995 compressionlevel = 9;
996 else:
997 compressionlevel = int(compressionlevel);
998 catfp.write(gzip.compress(fp.read(), compresslevel=compressionlevel));
999 if(compression=="bzip2"):
1000 try:
1001 import bz2;
1002 except ImportError:
1003 return False;
1004 catfp = BytesIO();
1005 if(compressionlevel is None):
1006 compressionlevel = 9;
1007 else:
1008 compressionlevel = int(compressionlevel);
1009 catfp.write(bz2.compress(fp.read(), compresslevel=compressionlevel));
1010 if(compression=="lz4"):
1011 try:
1012 import lz4.frame;
1013 except ImportError:
1014 return False;
1015 catfp = BytesIO();
1016 if(compressionlevel is None):
1017 compressionlevel = 9;
1018 else:
1019 compressionlevel = int(compressionlevel);
1020 catfp.write(lz4.frame.compress(fp.read(), compression_level=compressionlevel));
1021 if(compression=="lzo" or compression=="lzop"):
1022 try:
1023 import lzo;
1024 except ImportError:
1025 return False;
1026 catfp = BytesIO();
1027 if(compressionlevel is None):
1028 compressionlevel = 9;
1029 else:
1030 compressionlevel = int(compressionlevel);
1031 catfp.write(lzo.compress(fp.read(), compresslevel=compressionlevel));
1032 if(compression=="zstd"):
1033 try:
1034 import zstandard;
1035 except ImportError:
1036 return False;
1037 catfp = BytesIO();
1038 if(compressionlevel is None):
1039 compressionlevel = 10;
1040 else:
1041 compressionlevel = int(compressionlevel);
1042 catfp.write(zstandard.compress(fp.read(), level=compressionlevel));
1043 if(compression=="lzma"):
1044 try:
1045 import lzma;
1046 except ImportError:
1047 return False;
1048 catfp = BytesIO();
1049 if(compressionlevel is None):
1050 compressionlevel = 9;
1051 else:
1052 compressionlevel = int(compressionlevel);
1053 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]));
1054 if(compression=="xz"):
1055 try:
1056 import lzma;
1057 except ImportError:
1058 return False;
1059 catfp = BytesIO();
1060 if(compressionlevel is None):
1061 compressionlevel = 9;
1062 else:
1063 compressionlevel = int(compressionlevel);
1064 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]));
1065 if(compression=="auto" or compression is None):
1066 catfp = fp;
1067 catfp.seek(0, 0);
1068 return catfp;
1070 create_alias_function("Compress", __file_format_name__, "", CompressArchiveFile);
1072 def CompressOpenFile(outfile, compressionlevel=None):
1073 if(outfile is None):
1074 return False;
1075 fbasename = os.path.splitext(outfile)[0];
1076 fextname = os.path.splitext(outfile)[1];
1077 if(compressionlevel is None and fextname!=".zst"):
1078 compressionlevel = 9;
1079 elif(compressionlevel is None and fextname==".zst"):
1080 compressionlevel = 10;
1081 else:
1082 compressionlevel = int(compressionlevel);
1083 if(sys.version_info[0]==2):
1084 mode = "w";
1085 else:
1086 mode = "wb";
1087 try:
1088 if(fextname not in outextlistwd):
1089 try:
1090 outfp = open(outfile, "wb", encoding="UTF-8");
1091 except (ValueError, TypeError) as e:
1092 outfp = open(outfile, "wb");
1093 elif(fextname==".gz"):
1094 try:
1095 import gzip;
1096 except ImportError:
1097 return False;
1098 try:
1099 outfp = gzip.open(outfile, mode, compressionlevel, encoding="UTF-8");
1100 except (ValueError, TypeError) as e:
1101 outfp = gzip.open(outfile, mode, compressionlevel);
1102 elif(fextname==".bz2"):
1103 try:
1104 import bz2;
1105 except ImportError:
1106 return False;
1107 try:
1108 outfp = bz2.open(outfile, mode, compressionlevel, encoding="UTF-8");
1109 except (ValueError, TypeError) as e:
1110 outfp = bz2.open(outfile, mode, compressionlevel);
1111 elif(fextname==".zst"):
1112 try:
1113 import zstandard;
1114 except ImportError:
1115 return False;
1116 try:
1117 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel), encoding="UTF-8");
1118 except (ValueError, TypeError) as e:
1119 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel));
1120 elif(fextname==".xz"):
1121 try:
1122 import lzma;
1123 except ImportError:
1124 return False;
1125 try:
1126 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}], encoding="UTF-8");
1127 except (ValueError, TypeError) as e:
1128 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]);
1129 elif(fextname==".lz4"):
1130 try:
1131 import lz4.frame;
1132 except ImportError:
1133 return False;
1134 try:
1135 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel, encoding="UTF-8");
1136 except (ValueError, TypeError) as e:
1137 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel);
1138 elif(fextname==".lzo"):
1139 try:
1140 import lzo;
1141 except ImportError:
1142 return False;
1143 try:
1144 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel, encoding="UTF-8");
1145 except (ValueError, TypeError) as e:
1146 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel);
1147 elif(fextname==".lzma"):
1148 try:
1149 import lzma;
1150 except ImportError:
1151 return False;
1152 try:
1153 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}], encoding="UTF-8");
1154 except (ValueError, TypeError) as e:
1155 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]);
1156 except FileNotFoundError:
1157 return False;
1158 return outfp;
1160 def GetDevMajorMinor(fdev):
1161 retdev = [];
1162 if(hasattr(os, "minor")):
1163 retdev.append(os.minor(fdev));
1164 else:
1165 retdev.append(0);
1166 if(hasattr(os, "major")):
1167 retdev.append(os.major(fdev));
1168 else:
1169 retdev.append(0);
1170 return retdev;
1172 def CheckSumSupport(checkfor, guaranteed=True):
1173 if(guaranteed):
1174 hash_list = sorted(list(hashlib.algorithms_guaranteed));
1175 else:
1176 hash_list = sorted(list(hashlib.algorithms_available));
1177 checklistout = sorted(hash_list + ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1178 if(checkfor in checklistout):
1179 return True;
1180 else:
1181 return False;
1183 def CheckSumSupportAlt(checkfor, guaranteed=True):
1184 if(guaranteed):
1185 hash_list = sorted(list(hashlib.algorithms_guaranteed));
1186 else:
1187 hash_list = sorted(list(hashlib.algorithms_available));
1188 checklistout = hash_list;
1189 if(checkfor in checklistout):
1190 return True;
1191 else:
1192 return False;
1194 def PackArchiveFile(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1195 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1196 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1197 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1198 advancedlist = True;
1199 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1200 outfile = RemoveWindowsPath(outfile);
1201 checksumtype = checksumtype.lower();
1202 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1203 checksumtype="crc32";
1204 if(checksumtype=="none"):
1205 checksumtype = "";
1206 if(not compression or compression or compression=="catfile" or compression==formatspecs[1]):
1207 compression = None;
1208 if(compression not in compressionlist and compression is None):
1209 compression = "auto";
1210 if(verbose):
1211 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1212 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1213 if(os.path.exists(outfile)):
1214 os.unlink(outfile);
1215 if(outfile=="-"):
1216 verbose = False;
1217 catfp = BytesIO();
1218 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1219 catfp = outfile;
1220 else:
1221 fbasename = os.path.splitext(outfile)[0];
1222 fextname = os.path.splitext(outfile)[1];
1223 catfp = CompressOpenFile(outfile, compressionlevel);
1224 catver = formatspecs[5];
1225 fileheaderver = str(int(catver.replace(".", "")));
1226 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
1227 catfp.write(fileheader.encode('UTF-8'));
1228 infilelist = [];
1229 if(infiles=="-"):
1230 for line in sys.stdin:
1231 infilelist.append(line.strip());
1232 infilelist = list(filter(None, infilelist));
1233 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
1234 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
1235 return False;
1236 with open(infiles, "r") as finfile:
1237 for line in finfile:
1238 infilelist.append(line.strip());
1239 infilelist = list(filter(None, infilelist));
1240 else:
1241 if(isinstance(infiles, (list, tuple, ))):
1242 infilelist = list(filter(None, infiles));
1243 elif(isinstance(infiles, (str, ))):
1244 infilelist = list(filter(None, [infiles]));
1245 if(advancedlist):
1246 GetDirList = ListDirAdvanced(infilelist, followlink, False);
1247 else:
1248 GetDirList = ListDir(infilelist, followlink, False);
1249 if(not GetDirList):
1250 return False;
1251 curinode = 0;
1252 curfid = 0;
1253 inodelist = [];
1254 inodetofile = {};
1255 filetoinode = {};
1256 inodetocatinode = {};
1257 fnumfiles = format(int(len(GetDirList)), 'x').lower();
1258 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[4]);
1259 if(checksumtype=="none" or checksumtype==""):
1260 catfileheadercshex = format(0, 'x').lower();
1261 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1262 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1263 elif(checksumtype=="crc16_ccitt"):
1264 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1265 elif(checksumtype=="adler32"):
1266 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1267 elif(checksumtype=="crc32"):
1268 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1269 elif(checksumtype=="crc64_ecma"):
1270 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1271 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1272 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1273 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1274 checksumoutstr = hashlib.new(checksumtype);
1275 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
1276 catfileheadercshex = checksumoutstr.hexdigest().lower();
1277 else:
1278 catfileheadercshex = format(0, 'x').lower
1279 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[4]);
1280 catfp.write(fnumfilesa.encode('UTF-8'));
1281 try:
1282 catfp.flush();
1283 os.fsync(catfp.fileno());
1284 except io.UnsupportedOperation:
1285 pass;
1286 except AttributeError:
1287 pass;
1288 for curfname in GetDirList:
1289 catfhstart = catfp.tell();
1290 if(re.findall("^[.|/]", curfname)):
1291 fname = curfname;
1292 else:
1293 fname = "./"+curfname;
1294 if(verbose):
1295 VerbosePrintOut(fname);
1296 if(not followlink or followlink is None):
1297 fstatinfo = os.lstat(fname);
1298 else:
1299 fstatinfo = os.stat(fname);
1300 fpremode = fstatinfo.st_mode;
1301 finode = fstatinfo.st_ino;
1302 flinkcount = fstatinfo.st_nlink;
1303 ftype = 0;
1304 if(stat.S_ISREG(fpremode)):
1305 ftype = 0;
1306 elif(stat.S_ISLNK(fpremode)):
1307 ftype = 2;
1308 elif(stat.S_ISCHR(fpremode)):
1309 ftype = 3;
1310 elif(stat.S_ISBLK(fpremode)):
1311 ftype = 4;
1312 elif(stat.S_ISDIR(fpremode)):
1313 ftype = 5;
1314 elif(stat.S_ISFIFO(fpremode)):
1315 ftype = 6;
1316 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
1317 ftype = 8;
1318 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
1319 ftype = 9;
1320 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
1321 ftype = 10;
1322 else:
1323 ftype = 0;
1324 flinkname = "";
1325 fcurfid = format(int(curfid), 'x').lower();
1326 if(not followlink and finode!=0):
1327 if(ftype!=1):
1328 if(finode in inodelist):
1329 ftype = 1;
1330 flinkname = inodetofile[finode];
1331 fcurinode = format(int(inodetocatinode[finode]), 'x').lower();
1332 if(finode not in inodelist):
1333 inodelist.append(finode);
1334 inodetofile.update({finode: fname});
1335 inodetocatinode.update({finode: curinode});
1336 fcurinode = format(int(curinode), 'x').lower();
1337 curinode = curinode + 1;
1338 else:
1339 fcurinode = format(int(curinode), 'x').lower();
1340 curinode = curinode + 1;
1341 curfid = curfid + 1;
1342 if(ftype==2):
1343 flinkname = os.readlink(fname);
1344 fdev = fstatinfo.st_dev;
1345 getfdev = GetDevMajorMinor(fdev);
1346 fdev_minor = getfdev[0];
1347 fdev_major = getfdev[1];
1348 frdev = fstatinfo.st_dev;
1349 if(hasattr(fstatinfo, "st_rdev")):
1350 frdev = fstatinfo.st_rdev;
1351 else:
1352 frdev = fstatinfo.st_dev;
1353 getfrdev = GetDevMajorMinor(frdev);
1354 frdev_minor = getfrdev[0];
1355 frdev_major = getfrdev[1];
1356 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
1357 fsize = format(int("0"), 'x').lower();
1358 elif(ftype==0 or ftype==7):
1359 fsize = format(int(fstatinfo.st_size), 'x').lower();
1360 else:
1361 fsize = format(int(fstatinfo.st_size)).lower();
1362 fatime = format(int(fstatinfo.st_atime), 'x').lower();
1363 fmtime = format(int(fstatinfo.st_mtime), 'x').lower();
1364 fctime = format(int(fstatinfo.st_ctime), 'x').lower();
1365 if(hasattr(fstatinfo, "st_birthtime")):
1366 fbtime = format(int(fstatinfo.st_birthtime), 'x').lower();
1367 else:
1368 fbtime = format(int(fstatinfo.st_ctime), 'x').lower();
1369 fmode = format(int(fstatinfo.st_mode), 'x').lower();
1370 fchmode = format(int(stat.S_IMODE(fstatinfo.st_mode)), 'x').lower();
1371 ftypemod = format(int(stat.S_IFMT(fstatinfo.st_mode)), 'x').lower();
1372 fuid = format(int(fstatinfo.st_uid), 'x').lower();
1373 fgid = format(int(fstatinfo.st_gid), 'x').lower();
1374 funame = "";
1375 try:
1376 import pwd;
1377 try:
1378 userinfo = pwd.getpwuid(fstatinfo.st_uid);
1379 funame = userinfo.pw_name;
1380 except KeyError:
1381 funame = "";
1382 except ImportError:
1383 funame = "";
1384 fgname = "";
1385 try:
1386 import grp;
1387 try:
1388 groupinfo = grp.getgrgid(fstatinfo.st_gid);
1389 fgname = groupinfo.gr_name;
1390 except KeyError:
1391 fgname = "";
1392 except ImportError:
1393 fgname = "";
1394 fdev_minor = format(int(fdev_minor), 'x').lower();
1395 fdev_major = format(int(fdev_major), 'x').lower();
1396 frdev_minor = format(int(frdev_minor), 'x').lower();
1397 frdev_major = format(int(frdev_major), 'x').lower();
1398 finode = format(int(finode), 'x').lower();
1399 flinkcount = format(int(flinkcount), 'x').lower();
1400 if(hasattr(fstatinfo, "st_file_attributes")):
1401 fwinattributes = format(int(fstatinfo.st_file_attributes), 'x').lower();
1402 else:
1403 fwinattributes = format(int(0), 'x').lower();
1404 fcontents = "".encode('UTF-8');
1405 chunk_size = 1024;
1406 if(ftype == 0 or ftype == 7):
1407 with open(fname, "rb") as fpc:
1408 while(True):
1409 chunk = fpc.read(chunk_size);
1410 if(not chunk):
1411 break
1412 fcontents += chunk;
1413 if(followlink and (ftype == 1 or ftype == 2)):
1414 flstatinfo = os.stat(flinkname);
1415 with open(flinkname, "rb") as fpc:
1416 while(True):
1417 chunk = fpc.read(chunk_size);
1418 if(not chunk):
1419 break;
1420 fcontents += chunk;
1421 ftypehex = format(ftype, 'x').lower();
1422 extrafields = format(len(extradata), 'x').lower();
1423 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
1424 if(len(extradata)>0):
1425 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
1426 extrasizelen = format(len(extrasizestr), 'x').lower();
1427 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[4]);
1428 if(len(extradata)>0):
1429 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
1430 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
1431 if(checksumtype=="none" or checksumtype==""):
1432 catfileheadercshex = format(0, 'x').lower();
1433 catfilecontentcshex = format(0, 'x').lower();
1434 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1435 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1436 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
1437 elif(checksumtype=="crc16_ccitt"):
1438 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1439 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
1440 elif(checksumtype=="adler32"):
1441 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1442 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
1443 elif(checksumtype=="crc32"):
1444 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1445 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
1446 elif(checksumtype=="crc64_ecma"):
1447 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1448 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
1449 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1450 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1451 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
1452 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1453 checksumoutstr = hashlib.new(checksumtype);
1454 checksumoutstr.update("".encode('UTF-8'));
1455 catfileheadercshex = checksumoutstr.hexdigest().lower();
1456 checksumoutstr = hashlib.new(checksumtype);
1457 checksumoutstr.update(fcontents);
1458 catfilecontentcshex = checksumoutstr.hexdigest().lower();
1459 else:
1460 catfileheadercshex = format(0, 'x').lower();
1461 catfilecontentcshex = format(0, 'x').lower();
1462 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
1463 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
1464 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
1465 if(checksumtype=="none" or checksumtype==""):
1466 catfileheadercshex = format(0, 'x').lower();
1467 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1468 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1469 elif(checksumtype=="crc16_ccitt"):
1470 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1471 elif(checksumtype=="adler32"):
1472 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1473 elif(checksumtype=="crc32"):
1474 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1475 elif(checksumtype=="crc64_ecma"):
1476 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1477 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1478 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1479 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1480 checksumoutstr = hashlib.new(checksumtype);
1481 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
1482 catfileheadercshex = checksumoutstr.hexdigest().lower();
1483 else:
1484 catfileheadercshex = format(0, 'x').lower();
1485 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
1486 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
1487 catfcontentstart = catfp.tell() + len(catfileoutstr);
1488 catfileoutstrecd = catfileoutstr.encode('UTF-8');
1489 nullstrecd = formatspecs[4].encode('UTF-8');
1490 catfileout = catfileoutstrecd + fcontents + nullstrecd;
1491 catfcontentend = (catfp.tell() - 1) + len(catfileout);
1492 catfp.write(catfileout);
1493 try:
1494 catfp.flush();
1495 os.fsync(catfp.fileno());
1496 except io.UnsupportedOperation:
1497 pass;
1498 except AttributeError:
1499 pass;
1500 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1501 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1502 try:
1503 catfp.flush();
1504 os.fsync(catfp.fileno());
1505 except io.UnsupportedOperation:
1506 pass;
1507 except AttributeError:
1508 pass;
1509 if(outfile=="-"):
1510 catfp.seek(0, 0);
1511 if(hasattr(sys.stdout, "buffer")):
1512 shutil.copyfileobj(catfp, sys.stdout.buffer);
1513 else:
1514 shutil.copyfileobj(catfp, sys.stdout);
1515 if(returnfp):
1516 catfp.seek(0, 0);
1517 return catfp;
1518 else:
1519 catfp.close();
1520 return True;
1522 create_alias_function("Pack", __file_format_name__, "", PackArchiveFile);
1524 if(hasattr(shutil, "register_archive_format")):
1525 def PackArchiveFileFunc(archive_name, source_dir, **kwargs):
1526 return PackArchiveFile(source_dir, archive_name, False, "auto", None, False, "crc32", [], __file_format_delimiter__, False, False);
1527 create_alias_function("Pack", __file_format_name__, "Func", PackArchiveFileFunc);
1529 def PackArchiveFileFromDirList(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1530 return PackArchiveFile(infiles, outfile, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, returnfp);
1532 def PackArchiveFileFromTarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1533 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1534 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1535 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1536 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1537 outfile = RemoveWindowsPath(outfile);
1538 checksumtype = checksumtype.lower();
1539 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1540 checksumtype="crc32";
1541 if(checksumtype=="none"):
1542 checksumtype = "";
1543 if(not compression or compression or compression=="catfile" or compression==formatspecs[1]):
1544 compression = None;
1545 if(compression not in compressionlist and compression is None):
1546 compression = "auto";
1547 if(verbose):
1548 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1549 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1550 if(os.path.exists(outfile)):
1551 os.unlink(outfile);
1552 if(outfile=="-"):
1553 verbose = False;
1554 catfp = BytesIO();
1555 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1556 catfp = outfile;
1557 else:
1558 fbasename = os.path.splitext(outfile)[0];
1559 fextname = os.path.splitext(outfile)[1];
1560 catfp = CompressOpenFile(outfile, compressionlevel);
1561 catver = formatspecs[5];
1562 fileheaderver = str(int(catver.replace(".", "")));
1563 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
1564 catfp.write(fileheader.encode('UTF-8'));
1565 curinode = 0;
1566 curfid = 0;
1567 inodelist = [];
1568 inodetofile = {};
1569 filetoinode = {};
1570 inodetocatinode = {};
1571 if(not os.path.exists(infile) or not os.path.isfile(infile)):
1572 return False;
1573 try:
1574 if(not tarfile.is_tarfile(infile)):
1575 return False;
1576 except AttributeError:
1577 if(not is_tarfile(infile)):
1578 return False;
1579 try:
1580 tarfp = tarfile.open(infile, "r");
1581 except FileNotFoundError:
1582 return False;
1583 fnumfiles = format(int(len(tarfp.getmembers())), 'x').lower();
1584 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[4]);
1585 if(checksumtype=="none" or checksumtype==""):
1586 catfileheadercshex = format(0, 'x').lower();
1587 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1588 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1589 elif(checksumtype=="crc16_ccitt"):
1590 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1591 elif(checksumtype=="adler32"):
1592 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1593 elif(checksumtype=="crc32"):
1594 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1595 elif(checksumtype=="crc64_ecma"):
1596 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1597 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1598 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1599 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1600 checksumoutstr = hashlib.new(checksumtype);
1601 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
1602 catfileheadercshex = checksumoutstr.hexdigest().lower();
1603 else:
1604 catfileheadercshex = format(0, 'x').lower();
1605 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[4]);
1606 catfp.write(fnumfilesa.encode('UTF-8'));
1607 try:
1608 catfp.flush();
1609 os.fsync(catfp.fileno());
1610 except io.UnsupportedOperation:
1611 pass;
1612 except AttributeError:
1613 pass;
1614 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
1615 catfhstart = catfp.tell();
1616 if(re.findall("^[.|/]", member.name)):
1617 fname = member.name;
1618 else:
1619 fname = "./"+member.name;
1620 if(verbose):
1621 VerbosePrintOut(fname);
1622 fpremode = member.mode;
1623 ffullmode = member.mode;
1624 flinkcount = 0;
1625 ftype = 0;
1626 if(member.isreg()):
1627 ffullmode = member.mode + stat.S_IFREG;
1628 ftype = 0;
1629 elif(member.isdev()):
1630 ffullmode = member.mode;
1631 ftype = 7;
1632 elif(member.islnk()):
1633 ffullmode = member.mode + stat.S_IFREG;
1634 ftype = 1;
1635 elif(member.issym()):
1636 ffullmode = member.mode + stat.S_IFLNK;
1637 ftype = 2;
1638 elif(member.ischr()):
1639 ffullmode = member.mode + stat.S_IFCHR;
1640 ftype = 3;
1641 elif(member.isblk()):
1642 ffullmode = member.mode + stat.S_IFBLK;
1643 ftype = 4;
1644 elif(member.isdir()):
1645 ffullmode = member.mode + stat.S_IFDIR;
1646 ftype = 5;
1647 elif(member.isfifo()):
1648 ffullmode = member.mode + stat.S_IFIFO;
1649 ftype = 6;
1650 elif(member.issparse()):
1651 ffullmode = member.mode;
1652 ftype = 8;
1653 else:
1654 ffullmode = member.mode;
1655 ftype = 0;
1656 flinkname = "";
1657 fcurfid = format(int(curfid), 'x').lower();
1658 fcurinode = format(int(0), 'x').lower();
1659 curfid = curfid + 1;
1660 if(ftype==2):
1661 flinkname = member.linkname;
1662 fdev_minor = format(int(member.devminor), 'x').lower();
1663 fdev_major = format(int(member.devmajor), 'x').lower();
1664 frdev_minor = format(int(member.devminor), 'x').lower();
1665 frdev_major = format(int(member.devmajor), 'x').lower();
1666 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
1667 fsize = format(int("0"), 'x').lower();
1668 elif(ftype==0 or ftype==7):
1669 fsize = format(int(member.size), 'x').lower();
1670 else:
1671 fsize = format(int(member.size), 'x').lower();
1672 fatime = format(int(member.mtime), 'x').lower();
1673 fmtime = format(int(member.mtime), 'x').lower();
1674 fctime = format(int(member.mtime), 'x').lower();
1675 fbtime = format(int(member.mtime), 'x').lower();
1676 fmode = format(int(ffullmode), 'x').lower();
1677 fchmode = format(int(stat.S_IMODE(ffullmode)), 'x').lower();
1678 ftypemod = format(int(stat.S_IFMT(ffullmode)), 'x').lower();
1679 fuid = format(int(member.uid), 'x').lower();
1680 fgid = format(int(member.gid), 'x').lower();
1681 funame = member.uname;
1682 fgname = member.gname;
1683 flinkcount = format(int(flinkcount), 'x').lower();
1684 fcontents = "".encode('UTF-8');
1685 chunk_size = 1024;
1686 if(ftype == 0 or ftype == 7):
1687 with tarfp.extractfile(member) as fpc:
1688 while(True):
1689 chunk = fpc.read(chunk_size);
1690 if(not chunk):
1691 break
1692 fcontents += chunk;
1693 ftypehex = format(ftype, 'x').lower();
1694 extrafields = format(len(extradata), 'x').lower();
1695 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
1696 if(len(extradata)>0):
1697 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
1698 extrasizelen = format(len(extrasizestr), 'x').lower();
1699 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[4]);
1700 if(len(extradata)>0):
1701 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
1702 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
1703 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
1704 catfcontentstart = catfp.tell() + len(catfileoutstr);
1705 if(checksumtype=="none" or checksumtype==""):
1706 catfileheadercshex = format(0, 'x').lower();
1707 catfilecontentcshex = format(0, 'x').lower();
1708 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1709 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1710 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
1711 elif(checksumtype=="crc16_ccitt"):
1712 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1713 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
1714 elif(checksumtype=="adler32"):
1715 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1716 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
1717 elif(checksumtype=="crc32"):
1718 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1719 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
1720 elif(checksumtype=="crc64_ecma"):
1721 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1722 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
1723 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1724 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1725 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
1726 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1727 checksumoutstr = hashlib.new(checksumtype);
1728 checksumoutstr.update("".encode('UTF-8'));
1729 catfileheadercshex = checksumoutstr.hexdigest().lower();
1730 checksumoutstr = hashlib.new(checksumtype);
1731 checksumoutstr.update(fcontents);
1732 catfilecontentcshex = checksumoutstr.hexdigest().lower();
1733 else:
1734 catfileheadercshex = format(0, 'x').lower();
1735 catfilecontentcshex = format(0, 'x').lower();
1736 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
1737 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
1738 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
1739 if(checksumtype=="none" or checksumtype==""):
1740 catfileheadercshex = format(0, 'x').lower();
1741 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1742 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1743 elif(checksumtype=="crc16_ccitt"):
1744 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1745 elif(checksumtype=="adler32"):
1746 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1747 elif(checksumtype=="crc32"):
1748 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1749 elif(checksumtype=="crc64_ecma"):
1750 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1751 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1752 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1753 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1754 checksumoutstr = hashlib.new(checksumtype);
1755 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
1756 catfileheadercshex = checksumoutstr.hexdigest().lower();
1757 else:
1758 catfileheadercshex = format(0, 'x').lower();
1759 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
1760 catfileoutstrecd = catfileoutstr.encode('UTF-8');
1761 nullstrecd = formatspecs[4].encode('UTF-8');
1762 catfileout = catfileoutstrecd + fcontents + nullstrecd;
1763 catfcontentend = (catfp.tell() - 1) + len(catfileout);
1764 catfp.write(catfileout);
1765 try:
1766 catfp.flush();
1767 os.fsync(catfp.fileno());
1768 except io.UnsupportedOperation:
1769 pass;
1770 except AttributeError:
1771 pass;
1772 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1773 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1774 try:
1775 catfp.flush();
1776 os.fsync(catfp.fileno());
1777 except io.UnsupportedOperation:
1778 pass;
1779 except AttributeError:
1780 pass;
1781 if(outfile=="-"):
1782 catfp.seek(0, 0);
1783 if(hasattr(sys.stdout, "buffer")):
1784 shutil.copyfileobj(catfp, sys.stdout.buffer);
1785 else:
1786 shutil.copyfileobj(catfp, sys.stdout);
1787 if(returnfp):
1788 catfp.seek(0, 0);
1789 return catfp;
1790 else:
1791 catfp.close();
1792 return True;
1794 create_alias_function("Pack", __file_format_name__, "FromTarFile", PackArchiveFileFromTarFile);
1796 def PackArchiveFileFromZipFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1797 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1798 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1799 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1800 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1801 outfile = RemoveWindowsPath(outfile);
1802 checksumtype = checksumtype.lower();
1803 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1804 checksumtype="crc32";
1805 if(checksumtype=="none"):
1806 checksumtype = "";
1807 if(not compression or compression or compression=="catfile" or compression==formatspecs[1]):
1808 compression = None;
1809 if(compression not in compressionlist and compression is None):
1810 compression = "auto";
1811 if(verbose):
1812 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1813 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1814 if(os.path.exists(outfile)):
1815 os.unlink(outfile);
1816 if(outfile=="-"):
1817 verbose = False;
1818 catfp = BytesIO();
1819 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1820 catfp = outfile;
1821 else:
1822 fbasename = os.path.splitext(outfile)[0];
1823 fextname = os.path.splitext(outfile)[1];
1824 catfp = CompressOpenFile(outfile, compressionlevel);
1825 catver = formatspecs[5];
1826 fileheaderver = str(int(catver.replace(".", "")));
1827 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
1828 catfp.write(fileheader.encode('UTF-8'));
1829 curinode = 0;
1830 curfid = 0;
1831 inodelist = [];
1832 inodetofile = {};
1833 filetoinode = {};
1834 inodetocatinode = {};
1835 if(not os.path.exists(infile) or not os.path.isfile(infile)):
1836 return False;
1837 if(not zipfile.is_zipfile(infile)):
1838 return False;
1839 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
1840 ziptest = zipfp.testzip();
1841 if(ziptest):
1842 VerbosePrintOut("Bad file found!");
1843 fnumfiles = format(int(len(zipfp.infolist())), 'x').lower();
1844 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[4]);
1845 if(checksumtype=="none" or checksumtype==""):
1846 catfileheadercshex = format(0, 'x').lower();
1847 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1848 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1849 elif(checksumtype=="crc16_ccitt"):
1850 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1851 elif(checksumtype=="adler32"):
1852 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1853 elif(checksumtype=="crc32"):
1854 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1855 elif(checksumtype=="crc64_ecma"):
1856 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1857 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1858 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1859 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1860 checksumoutstr = hashlib.new(checksumtype);
1861 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
1862 catfileheadercshex = checksumoutstr.hexdigest().lower();
1863 else:
1864 catfileheadercshex = format(0, 'x').lower();
1865 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[4]);
1866 catfp.write(fnumfilesa.encode('UTF-8'));
1867 try:
1868 catfp.flush();
1869 os.fsync(catfp.fileno());
1870 except io.UnsupportedOperation:
1871 pass;
1872 except AttributeError:
1873 pass;
1874 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
1875 catfhstart = catfp.tell();
1876 if(re.findall("^[.|/]", member.filename)):
1877 fname = member.filename;
1878 else:
1879 fname = "./"+member.filename;
1880 zipinfo = zipfp.getinfo(member.filename);
1881 if(verbose):
1882 VerbosePrintOut(fname);
1883 if(not member.is_dir()):
1884 fpremode = int(stat.S_IFREG + 438);
1885 elif(member.is_dir()):
1886 fpremode = int(stat.S_IFDIR + 511);
1887 flinkcount = 0;
1888 ftype = 0;
1889 if(not member.is_dir()):
1890 ftype = 0;
1891 elif(member.is_dir()):
1892 ftype = 5;
1893 flinkname = "";
1894 fcurfid = format(int(curfid), 'x').lower();
1895 fcurinode = format(int(0), 'x').lower();
1896 curfid = curfid + 1;
1897 fdev_minor = format(int(0), 'x').lower();
1898 fdev_major = format(int(0), 'x').lower();
1899 frdev_minor = format(int(0), 'x').lower();
1900 frdev_major = format(int(0), 'x').lower();
1901 if(ftype==5):
1902 fsize = format(int("0"), 'x').lower();
1903 elif(ftype==0):
1904 fsize = format(int(member.file_size), 'x').lower();
1905 else:
1906 fsize = format(int(member.file_size), 'x').lower();
1907 fatime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
1908 fmtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
1909 fctime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
1910 fbtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
1911 if(not member.is_dir()):
1912 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
1913 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
1914 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
1915 if(member.is_dir()):
1916 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
1917 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
1918 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
1919 try:
1920 fuid = format(int(os.getuid()), 'x').lower();
1921 except AttributeError:
1922 fuid = format(int(0), 'x').lower();
1923 except KeyError:
1924 fuid = format(int(0), 'x').lower();
1925 try:
1926 fgid = format(int(os.getgid()), 'x').lower();
1927 except AttributeError:
1928 fgid = format(int(0), 'x').lower();
1929 except KeyError:
1930 fgid = format(int(0), 'x').lower();
1931 try:
1932 import pwd;
1933 try:
1934 userinfo = pwd.getpwuid(os.getuid());
1935 funame = userinfo.pw_name;
1936 except KeyError:
1937 funame = "";
1938 except AttributeError:
1939 funame = "";
1940 except ImportError:
1941 funame = "";
1942 fgname = "";
1943 try:
1944 import grp;
1945 try:
1946 groupinfo = grp.getgrgid(os.getgid());
1947 fgname = groupinfo.gr_name;
1948 except KeyError:
1949 fgname = "";
1950 except AttributeError:
1951 fgname = "";
1952 except ImportError:
1953 fgname = "";
1954 fcontents = "".encode('UTF-8');
1955 if(ftype==0):
1956 fcontents = zipfp.read(member.filename);
1957 ftypehex = format(ftype, 'x').lower();
1958 extrafields = format(len(extradata), 'x').lower();
1959 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
1960 if(len(extradata)>0):
1961 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
1962 extrasizelen = format(len(extrasizestr), 'x').lower();
1963 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[4]);
1964 if(len(extradata)>0):
1965 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
1966 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
1967 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
1968 catfcontentstart = catfp.tell() + len(catfileoutstr);
1969 if(checksumtype=="none" or checksumtype==""):
1970 catfileheadercshex = format(0, 'x').lower();
1971 catfilecontentcshex = format(0, 'x').lower();
1972 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1973 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1974 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
1975 elif(checksumtype=="crc16_ccitt"):
1976 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1977 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
1978 elif(checksumtype=="adler32"):
1979 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1980 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
1981 elif(checksumtype=="crc32"):
1982 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1983 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
1984 elif(checksumtype=="crc64_ecma"):
1985 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1986 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
1987 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1988 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1989 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
1990 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1991 checksumoutstr = hashlib.new(checksumtype);
1992 checksumoutstr.update("".encode('UTF-8'));
1993 catfileheadercshex = checksumoutstr.hexdigest().lower();
1994 checksumoutstr = hashlib.new(checksumtype);
1995 checksumoutstr.update(fcontents);
1996 catfilecontentcshex = checksumoutstr.hexdigest().lower();
1997 else:
1998 catfileheadercshex = format(0, 'x').lower();
1999 catfilecontentcshex = format(0, 'x').lower();
2000 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
2001 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2002 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
2003 if(checksumtype=="none" or checksumtype==""):
2004 catfileheadercshex = format(0, 'x').lower();
2005 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2006 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2007 elif(checksumtype=="crc16_ccitt"):
2008 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2009 elif(checksumtype=="adler32"):
2010 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2011 elif(checksumtype=="crc32"):
2012 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2013 elif(checksumtype=="crc64_ecma"):
2014 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2015 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2016 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2017 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2018 checksumoutstr = hashlib.new(checksumtype);
2019 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
2020 catfileheadercshex = checksumoutstr.hexdigest().lower();
2021 else:
2022 catfileheadercshex = format(0, 'x').lower();
2023 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
2024 catfileoutstrecd = catfileoutstr.encode('UTF-8');
2025 nullstrecd = formatspecs[4].encode('UTF-8');
2026 catfileout = catfileoutstrecd + fcontents + nullstrecd;
2027 catfcontentend = (catfp.tell() - 1) + len(catfileout);
2028 catfp.write(catfileout);
2029 try:
2030 catfp.flush();
2031 os.fsync(catfp.fileno());
2032 except io.UnsupportedOperation:
2033 pass;
2034 except AttributeError:
2035 pass;
2036 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2037 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2038 try:
2039 catfp.flush();
2040 os.fsync(catfp.fileno());
2041 except io.UnsupportedOperation:
2042 pass;
2043 except AttributeError:
2044 pass;
2045 if(outfile=="-"):
2046 catfp.seek(0, 0);
2047 if(hasattr(sys.stdout, "buffer")):
2048 shutil.copyfileobj(catfp, sys.stdout.buffer);
2049 else:
2050 shutil.copyfileobj(catfp, sys.stdout);
2051 if(returnfp):
2052 catfp.seek(0, 0);
2053 return catfp;
2054 else:
2055 catfp.close();
2056 return True;
2058 create_alias_function("Pack", __file_format_name__, "FromZipFile", PackArchiveFileFromZipFile);
2060 if(not rarfile_support):
2061 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2062 return False
2064 if(rarfile_support):
2065 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2066 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2067 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2068 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2069 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2070 outfile = RemoveWindowsPath(outfile);
2071 checksumtype = checksumtype.lower();
2072 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2073 checksumtype="crc32";
2074 if(checksumtype=="none"):
2075 checksumtype = "";
2076 if(not compression or compression or compression=="catfile" or compression==formatspecs[1]):
2077 compression = None;
2078 if(compression not in compressionlist and compression is None):
2079 compression = "auto";
2080 if(verbose):
2081 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2082 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2083 if(os.path.exists(outfile)):
2084 os.unlink(outfile);
2085 if(outfile=="-"):
2086 verbose = False;
2087 catfp = BytesIO();
2088 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2089 catfp = outfile;
2090 else:
2091 fbasename = os.path.splitext(outfile)[0];
2092 fextname = os.path.splitext(outfile)[1];
2093 catfp = CompressOpenFile(outfile, compressionlevel);
2094 catver = formatspecs[5];
2095 fileheaderver = str(int(catver.replace(".", "")));
2096 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
2097 catfp.write(fileheader.encode('UTF-8'));
2098 curinode = 0;
2099 curfid = 0;
2100 inodelist = [];
2101 inodetofile = {};
2102 filetoinode = {};
2103 inodetocatinode = {};
2104 if(not os.path.exists(infile) or not os.path.isfile(infile)):
2105 return False;
2106 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
2107 return False;
2108 rarfp = rarfile.RarFile(infile, "r");
2109 rartest = rarfp.testrar();
2110 if(rartest):
2111 VerbosePrintOut("Bad file found!");
2112 fnumfiles = format(int(len(rarfp.infolist())), 'x').lower();
2113 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[4]);
2114 if(checksumtype=="none" or checksumtype==""):
2115 catfileheadercshex = format(0, 'x').lower();
2116 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2117 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2118 elif(checksumtype=="crc16_ccitt"):
2119 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2120 elif(checksumtype=="adler32"):
2121 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2122 elif(checksumtype=="crc32"):
2123 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2124 elif(checksumtype=="crc64_ecma"):
2125 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2126 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2127 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2128 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2129 checksumoutstr = hashlib.new(checksumtype);
2130 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2131 catfileheadercshex = checksumoutstr.hexdigest().lower();
2132 else:
2133 catfileheadercshex = format(0, 'x').lower();
2134 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[4]);
2135 catfp.write(fnumfilesa.encode('UTF-8'));
2136 try:
2137 catfp.flush();
2138 os.fsync(catfp.fileno());
2139 except io.UnsupportedOperation:
2140 pass;
2141 except AttributeError:
2142 pass;
2143 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
2144 is_unix = False;
2145 is_windows = False;
2146 if(member.host_os == rarfile.RAR_OS_UNIX):
2147 is_windows = False;
2148 try:
2149 member.external_attr
2150 is_unix = True;
2151 except AttributeError:
2152 is_unix = False;
2153 elif(member.host_os == rarfile.RAR_OS_WIN32):
2154 is_unix = False;
2155 try:
2156 member.external_attr
2157 is_windows = True;
2158 except AttributeError:
2159 is_windows = False;
2160 else:
2161 is_unix = False;
2162 is_windows = False;
2163 catfhstart = catfp.tell();
2164 if(re.findall("^[.|/]", member.filename)):
2165 fname = member.filename;
2166 else:
2167 fname = "./"+member.filename;
2168 rarinfo = rarfp.getinfo(member.filename);
2169 if(verbose):
2170 VerbosePrintOut(fname);
2171 if(is_unix and member.external_attr !=0):
2172 fpremode = int(member.external_attr);
2173 elif(member.is_file()):
2174 fpremode = int(stat.S_IFREG + 438);
2175 elif(member.is_symlink()):
2176 fpremode = int(stat.S_IFLNK + 438);
2177 elif(member.is_dir()):
2178 fpremode = int(stat.S_IFDIR + 511);
2179 if(is_windows and member.external_attr !=0):
2180 fwinattributes = format(int(member.external_attr), 'x').lower();
2181 else:
2182 fwinattributes = format(int(0), 'x').lower();
2183 flinkcount = 0;
2184 ftype = 0;
2185 if(member.is_file()):
2186 ftype = 0;
2187 elif(member.is_symlink()):
2188 ftype = 2;
2189 elif(member.is_dir()):
2190 ftype = 5;
2191 flinkname = "";
2192 if(ftype==2):
2193 flinkname = rarfp.read(member.filename).decode("UTF-8");
2194 fcurfid = format(int(curfid), 'x').lower();
2195 fcurinode = format(int(0), 'x').lower();
2196 curfid = curfid + 1;
2197 fdev_minor = format(int(0), 'x').lower();
2198 fdev_major = format(int(0), 'x').lower();
2199 frdev_minor = format(int(0), 'x').lower();
2200 frdev_major = format(int(0), 'x').lower();
2201 if(ftype==5):
2202 fsize = format(int("0"), 'x').lower();
2203 elif(ftype==0):
2204 fsize = format(int(member.file_size), 'x').lower();
2205 else:
2206 fsize = format(int(member.file_size), 'x').lower();
2207 try:
2208 if(member.atime):
2209 fatime = format(int(member.atime.timestamp()), 'x').lower();
2210 else:
2211 fatime = format(int(member.mtime.timestamp()), 'x').lower();
2212 except AttributeError:
2213 fatime = format(int(member.mtime.timestamp()), 'x').lower();
2214 fmtime = format(int(member.mtime.timestamp()), 'x').lower();
2215 try:
2216 if(member.ctime):
2217 fctime = format(int(member.ctime.timestamp()), 'x').lower();
2218 else:
2219 fctime = format(int(member.mtime.timestamp()), 'x').lower();
2220 except AttributeError:
2221 fctime = format(int(member.mtime.timestamp()), 'x').lower();
2222 fbtime = format(int(member.mtime.timestamp()), 'x').lower();
2223 if(is_unix and member.external_attr !=0):
2224 fmode = format(int(member.external_attr), 'x').lower();
2225 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
2226 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
2227 elif(member.is_file()):
2228 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
2229 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2230 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2231 elif(member.is_symlink()):
2232 fmode = format(int(stat.S_IFLNK + 438), 'x').lower();
2233 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2234 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2235 elif(member.is_dir()):
2236 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
2237 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
2238 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
2239 try:
2240 fuid = format(int(os.getuid()), 'x').lower();
2241 except AttributeError:
2242 fuid = format(int(0), 'x').lower();
2243 except KeyError:
2244 fuid = format(int(0), 'x').lower();
2245 try:
2246 fgid = format(int(os.getgid()), 'x').lower();
2247 except AttributeError:
2248 fgid = format(int(0), 'x').lower();
2249 except KeyError:
2250 fgid = format(int(0), 'x').lower();
2251 try:
2252 import pwd;
2253 try:
2254 userinfo = pwd.getpwuid(os.getuid());
2255 funame = userinfo.pw_name;
2256 except KeyError:
2257 funame = "";
2258 except AttributeError:
2259 funame = "";
2260 except ImportError:
2261 funame = "";
2262 fgname = "";
2263 try:
2264 import grp;
2265 try:
2266 groupinfo = grp.getgrgid(os.getgid());
2267 fgname = groupinfo.gr_name;
2268 except KeyError:
2269 fgname = "";
2270 except AttributeError:
2271 fgname = "";
2272 except ImportError:
2273 fgname = "";
2274 fcontents = "".encode('UTF-8');
2275 if(ftype==0):
2276 fcontents = rarfp.read(member.filename);
2277 ftypehex = format(ftype, 'x').lower();
2278 extrafields = format(len(extradata), 'x').lower();
2279 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
2280 if(len(extradata)>0):
2281 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
2282 extrasizelen = format(len(extrasizestr), 'x').lower();
2283 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[4]);
2284 if(len(extradata)>0):
2285 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
2286 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
2287 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2288 catfcontentstart = catfp.tell() + len(catfileoutstr);
2289 if(checksumtype=="none" or checksumtype==""):
2290 catfileheadercshex = format(0, 'x').lower();
2291 catfilecontentcshex = format(0, 'x').lower();
2292 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2293 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2294 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2295 elif(checksumtype=="crc16_ccitt"):
2296 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2297 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2298 elif(checksumtype=="adler32"):
2299 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2300 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2301 elif(checksumtype=="crc32"):
2302 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2303 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2304 elif(checksumtype=="crc64_ecma"):
2305 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2306 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2307 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2308 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2309 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2310 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2311 checksumoutstr = hashlib.new(checksumtype);
2312 checksumoutstr.update("".encode('UTF-8'));
2313 catfileheadercshex = checksumoutstr.hexdigest().lower();
2314 checksumoutstr = hashlib.new(checksumtype);
2315 checksumoutstr.update(fcontents);
2316 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2317 else:
2318 catfileheadercshex = format(0, 'x').lower();
2319 catfilecontentcshex = format(0, 'x').lower();
2320 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
2321 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2322 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr
2323 if(checksumtype=="none" or checksumtype==""):
2324 catfileheadercshex = format(0, 'x').lower()
2325 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2326 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
2327 elif(checksumtype=="crc16_ccitt"):
2328 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
2329 elif(checksumtype=="adler32"):
2330 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
2331 elif(checksumtype=="crc32"):
2332 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
2333 elif(checksumtype=="crc64_ecma"):
2334 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2335 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2336 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2337 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2338 checksumoutstr = hashlib.new(checksumtype)
2339 checksumoutstr.update(catfileoutstr.encode('UTF-8'))
2340 catfileheadercshex = checksumoutstr.hexdigest().lower()
2341 else:
2342 catfileheadercshex = format(0, 'x').lower()
2343 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4])
2344 catfileoutstrecd = catfileoutstr.encode('UTF-8')
2345 nullstrecd = formatspecs[4].encode('UTF-8')
2346 catfileout = catfileoutstrecd + fcontents + nullstrecd
2347 catfcontentend = (catfp.tell() - 1) + len(catfileout)
2348 catfp.write(catfileout)
2349 try:
2350 catfp.flush()
2351 os.fsync(catfp.fileno())
2352 except io.UnsupportedOperation:
2353 pass
2354 except AttributeError:
2355 pass
2356 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2357 catfp = CompressArchiveFile(catfp, compression, formatspecs)
2358 try:
2359 catfp.flush()
2360 os.fsync(catfp.fileno())
2361 except io.UnsupportedOperation:
2362 pass
2363 except AttributeError:
2364 pass
2365 if(outfile=="-"):
2366 catfp.seek(0, 0)
2367 if(hasattr(sys.stdout, "buffer")):
2368 shutil.copyfileobj(catfp, sys.stdout.buffer)
2369 else:
2370 shutil.copyfileobj(catfp, sys.stdout)
2371 if(returnfp):
2372 catfp.seek(0, 0)
2373 return catfp
2374 else:
2375 catfp.close()
2376 return True;
2378 create_alias_function("Pack", __file_format_name__, "FromRarFile", PackArchiveFileFromRarFile);
2380 def ArchiveFileSeekToFileNum(infile, seekto=0, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
2381 if(hasattr(infile, "read") or hasattr(infile, "write")):
2382 catfp = infile;
2383 catfp.seek(0, 0);
2384 catfp = UncompressArchiveFile(catfp, formatspecs);
2385 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
2386 if(checkcompressfile=="tarfile"):
2387 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2388 if(checkcompressfile=="zipfile"):
2389 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2390 if(rarfile_support and checkcompressfile=="rarfile"):
2391 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2392 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[1]):
2393 return False;
2394 if(not catfp):
2395 return False;
2396 catfp.seek(0, 0);
2397 elif(infile=="-"):
2398 catfp = BytesIO();
2399 if(hasattr(sys.stdin, "buffer")):
2400 shutil.copyfileobj(sys.stdin.buffer, catfp);
2401 else:
2402 shutil.copyfileobj(sys.stdin, catfp);
2403 catfp.seek(0, 0);
2404 catfp = UncompressArchiveFile(catfp, formatspecs);
2405 if(not catfp):
2406 return False;
2407 catfp.seek(0, 0);
2408 else:
2409 infile = RemoveWindowsPath(infile);
2410 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
2411 if(checkcompressfile=="tarfile"):
2412 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2413 if(checkcompressfile=="zipfile"):
2414 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2415 if(rarfile_support and checkcompressfile=="rarfile"):
2416 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2417 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[1]):
2418 return False;
2419 compresscheck = CheckCompressionType(infile, formatspecs, True);
2420 if(not compresscheck):
2421 fextname = os.path.splitext(infile)[1];
2422 if(fextname==".gz"):
2423 compresscheck = "gzip";
2424 if(fextname==".bz2"):
2425 compresscheck = "bzip2";
2426 if(fextname==".zst"):
2427 compresscheck = "zstd";
2428 if(fextname==".lz4" or fextname==".clz4"):
2429 compresscheck = "lz4";
2430 if(fextname==".lzo" or fextname==".lzop"):
2431 compresscheck = "lzo";
2432 if(fextname==".lzma" or fextname==".xz"):
2433 compresscheck = "lzma";
2434 if(not compresscheck):
2435 return False;
2436 catfp = UncompressFile(infile, formatspecs, "rb");
2438 try:
2439 catfp.seek(0, 2);
2440 except OSError:
2441 SeekToEndOfFile(catfp);
2442 except ValueError:
2443 SeekToEndOfFile(catfp);
2444 CatSize = catfp.tell();
2445 CatSizeEnd = CatSize;
2447 try:
2448 catfp.seek(0, 0);
2449 except OSError:
2450 return False;
2451 except ValueError:
2452 return False;
2453 catheader = ReadFileHeaderData(catfp, 4, formatspecs[4]);
2454 catstring = catheader[0];
2455 catversion = re.findall(r"([\d]+)$", catstring);
2456 fprenumfiles = catheader[1];
2457 fnumfiles = int(fprenumfiles, 16);
2458 fprechecksumtype = catheader[2];
2459 fprechecksum = catheader[3];
2460 fileheader = AppendNullByte(catstring, formatspecs[4]);
2461 fnumfileshex = format(int(fnumfiles), 'x').lower();
2462 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[4]);
2463 if(fprechecksumtype=="none" or fprechecksumtype==""):
2464 catfileheadercshex = format(0, 'x').lower();
2465 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
2466 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2467 elif(fprechecksumtype=="crc16_ccitt"):
2468 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2469 elif(fprechecksumtype=="adler32"):
2470 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2471 elif(fprechecksumtype=="crc32"):
2472 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2473 elif(fprechecksumtype=="crc64_ecma"):
2474 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2475 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
2476 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2477 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
2478 checksumoutstr = hashlib.new(fprechecksumtype);
2479 checksumoutstr.update(fileheader.encode('UTF-8'));
2480 catfileheadercshex = checksumoutstr.hexdigest().lower();
2481 else:
2482 catfileheadercshex = format(0, 'x').lower();
2483 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
2484 fheadtell = len(fileheader);
2485 if(fprechecksum!=catfileheadercshex and not skipchecksum):
2486 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(catfp.tell()));
2487 return False;
2488 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
2489 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
2490 if(seekto>=fnumfiles):
2491 seekto = fnumfiles - 1;
2492 if(seekto<0):
2493 seekto = 0;
2494 if(seekto>=0):
2495 il = -1;
2496 while(il < seekto):
2497 seekstart = catfp.tell();
2498 preheaderdata = ReadFileHeaderData(catfp, 5, formatspecs[4]);
2499 prefheadsize = int(preheaderdata[0], 16);
2500 prefseek = prefheadsize - (int(len(preheaderdata[1]) + 1) + int(len(preheaderdata[2]) + 1) + int(len(preheaderdata[3]) + 1) + int(len(preheaderdata[4]) + 1));
2501 preftype = int(preheaderdata[1], 16);
2502 prefsize = int(preheaderdata[4], 16);
2503 catfp.seek(prefseek, 1);
2504 catfp.seek(1, 1);
2505 catfp.seek(prefsize, 1);
2506 catfp.seek(1, 1);
2507 il = il + 1;
2508 catfp.seek(seekstart, 0);
2509 fileidnum = il;
2510 catfheadsize = int(preheaderdata[0], 16);
2511 catftype = int(preheaderdata[1], 16);
2512 if(re.findall("^[.|/]", preheaderdata[2])):
2513 catfname = preheaderdata[2];
2514 else:
2515 catfname = "./"+preheaderdata[2];
2516 catflinkname = preheaderdata[3];
2517 catfsize = int(preheaderdata[4], 16);
2518 catfbasedir = os.path.dirname(catfname);
2519 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
2520 if(returnfp):
2521 catlist.update({'catfp': catfp});
2522 else:
2523 catfp.close();
2524 return catlist;
2526 create_alias_function("", __file_format_name__, "SeekToFileNum", ArchiveFileSeekToFileNum);
2528 def ArchiveFileSeekToFileName(infile, seekfile=None, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
2529 if(hasattr(infile, "read") or hasattr(infile, "write")):
2530 catfp = infile;
2531 catfp.seek(0, 0);
2532 catfp = UncompressArchiveFile(catfp, formatspecs);
2533 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
2534 if(checkcompressfile=="tarfile"):
2535 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2536 if(checkcompressfile=="zipfile"):
2537 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2538 if(rarfile_support and checkcompressfile=="rarfile"):
2539 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2540 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[1]):
2541 return False;
2542 if(not catfp):
2543 return False;
2544 catfp.seek(0, 0);
2545 elif(infile=="-"):
2546 catfp = BytesIO();
2547 if(hasattr(sys.stdin, "buffer")):
2548 shutil.copyfileobj(sys.stdin.buffer, catfp);
2549 else:
2550 shutil.copyfileobj(sys.stdin, catfp);
2551 catfp.seek(0, 0);
2552 catfp = UncompressArchiveFile(catfp, formatspecs);
2553 if(not catfp):
2554 return False;
2555 catfp.seek(0, 0);
2556 else:
2557 infile = RemoveWindowsPath(infile);
2558 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
2559 if(checkcompressfile=="tarfile"):
2560 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2561 if(checkcompressfile=="zipfile"):
2562 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2563 if(rarfile_support and checkcompressfile=="rarfile"):
2564 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2565 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[1]):
2566 return False;
2567 compresscheck = CheckCompressionType(infile, formatspecs, True);
2568 if(not compresscheck):
2569 fextname = os.path.splitext(infile)[1];
2570 if(fextname==".gz"):
2571 compresscheck = "gzip";
2572 if(fextname==".bz2"):
2573 compresscheck = "bzip2";
2574 if(fextname==".zst"):
2575 compresscheck = "zstd";
2576 if(fextname==".lz4" or fextname==".clz4"):
2577 compresscheck = "lz4";
2578 if(fextname==".lzo" or fextname==".lzop"):
2579 compresscheck = "lzo";
2580 if(fextname==".lzma" or fextname==".xz"):
2581 compresscheck = "lzma";
2582 if(not compresscheck):
2583 return False;
2584 catfp = UncompressFile(infile, formatspecs, "rb");
2586 try:
2587 catfp.seek(0, 2);
2588 except OSError:
2589 SeekToEndOfFile(catfp);
2590 except ValueError:
2591 SeekToEndOfFile(catfp);
2592 CatSize = catfp.tell();
2593 CatSizeEnd = CatSize;
2595 try:
2596 catfp.seek(0, 0);
2597 except OSError:
2598 return False;
2599 except ValueError:
2600 return False;
2601 catheader = ReadFileHeaderData(catfp, 4, formatspecs[4]);
2602 catstring = catheader[0];
2603 catversion = re.findall(r"([\d]+)$", catstring);
2604 fprenumfiles = catheader[1];
2605 fnumfiles = int(fprenumfiles, 16);
2606 fprechecksumtype = catheader[2];
2607 fprechecksum = catheader[3];
2608 fileheader = AppendNullByte(catstring, formatspecs[4]);
2609 fnumfileshex = format(int(fnumfiles), 'x').lower();
2610 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[4]);
2611 if(fprechecksumtype=="none" or fprechecksumtype==""):
2612 catfileheadercshex = format(0, 'x').lower();
2613 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
2614 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2615 elif(fprechecksumtype=="crc16_ccitt"):
2616 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2617 elif(fprechecksumtype=="adler32"):
2618 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2619 elif(fprechecksumtype=="crc32"):
2620 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2621 elif(fprechecksumtype=="crc64_ecma"):
2622 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2623 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
2624 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2625 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
2626 checksumoutstr = hashlib.new(fprechecksumtype);
2627 checksumoutstr.update(fileheader.encode('UTF-8'));
2628 catfileheadercshex = checksumoutstr.hexdigest().lower();
2629 else:
2630 catfileheadercshex = format(0, 'x').lower();
2631 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
2632 fheadtell = len(fileheader);
2633 if(fprechecksum!=catfileheadercshex and not skipchecksum):
2634 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(catfp.tell()));
2635 return False;
2636 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
2637 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
2638 seekto = fnumfiles - 1
2639 filefound = False;
2640 if(seekto>=0):
2641 il = -1;
2642 while(il < seekto):
2643 seekstart = catfp.tell();
2644 preheaderdata = ReadFileHeaderData(catfp, 5, formatspecs[4]);
2645 prefheadsize = int(preheaderdata[0], 16);
2646 prefseek = prefheadsize - (int(len(preheaderdata[1]) + 1) + int(len(preheaderdata[2]) + 1) + int(len(preheaderdata[3]) + 1) + int(len(preheaderdata[4]) + 1));
2647 preftype = int(preheaderdata[1], 16);
2648 prefsize = int(preheaderdata[4], 16);
2649 catfp.seek(prefseek, 1);
2650 catfp.seek(1, 1);
2651 catfp.seek(prefsize, 1);
2652 catfp.seek(1, 1);
2653 il = il + 1;
2654 filefound = False;
2655 prefname = preheaderdata[2];
2656 if(re.findall("^[.|/]", preheaderdata[2])):
2657 prefname = preheaderdata[2];
2658 else:
2659 prefname = "./"+preheaderdata[2];
2660 if(prefname==seekfile):
2661 filefound = True;
2662 break;
2663 catfp.seek(seekstart, 0);
2664 fileidnum = il;
2665 catfheadsize = int(preheaderdata[0], 16);
2666 catftype = int(preheaderdata[1], 16);
2667 if(re.findall("^[.|/]", preheaderdata[2])):
2668 catfname = preheaderdata[2];
2669 else:
2670 catfname = "./"+preheaderdata[2];
2671 catflinkname = preheaderdata[3];
2672 catfsize = int(preheaderdata[4], 16);
2673 catfbasedir = os.path.dirname(catfname);
2674 if(filefound):
2675 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
2676 else:
2677 return False;
2678 if(returnfp):
2679 catlist.update({'catfp': catfp});
2680 else:
2681 catfp.close();
2682 return catlist;
2684 create_alias_function("", __file_format_name__, "SeekToFileName", ArchiveFileSeekToFileName);
2686 def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
2687 if(hasattr(infile, "read") or hasattr(infile, "write")):
2688 catfp = infile;
2689 catfp.seek(0, 0);
2690 catfp = UncompressArchiveFile(catfp, formatspecs);
2691 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
2692 if(checkcompressfile=="tarfile"):
2693 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
2694 if(checkcompressfile=="zipfile"):
2695 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
2696 if(rarfile_support and checkcompressfile=="rarfile"):
2697 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
2698 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[1]):
2699 return False;
2700 if(not catfp):
2701 return False;
2702 catfp.seek(0, 0);
2703 elif(infile=="-"):
2704 catfp = BytesIO();
2705 if(hasattr(sys.stdin, "buffer")):
2706 shutil.copyfileobj(sys.stdin.buffer, catfp);
2707 else:
2708 shutil.copyfileobj(sys.stdin, catfp);
2709 catfp.seek(0, 0);
2710 catfp = UncompressArchiveFile(catfp, formatspecs);
2711 if(not catfp):
2712 return False;
2713 catfp.seek(0, 0);
2714 else:
2715 infile = RemoveWindowsPath(infile);
2716 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
2717 if(checkcompressfile=="tarfile"):
2718 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
2719 if(checkcompressfile=="zipfile"):
2720 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
2721 if(rarfile_support and checkcompressfile=="rarfile"):
2722 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
2723 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[1]):
2724 return False;
2725 compresscheck = CheckCompressionType(infile, formatspecs, True);
2726 if(not compresscheck):
2727 fextname = os.path.splitext(infile)[1];
2728 if(fextname==".gz"):
2729 compresscheck = "gzip";
2730 if(fextname==".bz2"):
2731 compresscheck = "bzip2";
2732 if(fextname==".zst"):
2733 compresscheck = "zstd";
2734 if(fextname==".lz4" or fextname==".clz4"):
2735 compresscheck = "lz4";
2736 if(fextname==".lzo" or fextname==".lzop"):
2737 compresscheck = "lzo";
2738 if(fextname==".lzma" or fextname==".xz"):
2739 compresscheck = "lzma";
2740 if(not compresscheck):
2741 return False;
2742 catfp = UncompressFile(infile, formatspecs, "rb");
2744 try:
2745 catfp.seek(0, 2);
2746 except OSError:
2747 SeekToEndOfFile(catfp);
2748 except ValueError:
2749 SeekToEndOfFile(catfp);
2750 CatSize = catfp.tell();
2751 CatSizeEnd = CatSize;
2753 try:
2754 catfp.seek(0, 0);
2755 except OSError:
2756 return False;
2757 except ValueError:
2758 return False;
2759 catheader = ReadFileHeaderData(catfp, 4, formatspecs[4]);
2760 catstring = catheader[0];
2761 catversion = re.findall(r"([\d]+)$", catstring);
2762 fprenumfiles = catheader[1];
2763 fnumfiles = int(fprenumfiles, 16);
2764 fprechecksumtype = catheader[2];
2765 fprechecksum = catheader[3];
2766 fileheader = AppendNullByte(catstring, formatspecs[4]);
2767 fnumfileshex = format(int(fnumfiles), 'x').lower();
2768 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[4]);
2769 if(fprechecksumtype=="none" or fprechecksumtype==""):
2770 catfileheadercshex = format(0, 'x').lower();
2771 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
2772 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2773 elif(fprechecksumtype=="crc16_ccitt"):
2774 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2775 elif(fprechecksumtype=="adler32"):
2776 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2777 elif(fprechecksumtype=="crc32"):
2778 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2779 elif(fprechecksumtype=="crc64_ecma"):
2780 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2781 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
2782 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2783 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
2784 checksumoutstr = hashlib.new(fprechecksumtype);
2785 checksumoutstr.update(fileheader.encode('UTF-8'));
2786 catfileheadercshex = checksumoutstr.hexdigest().lower();
2787 else:
2788 catfileheadercshex = format(0, 'x').lower();
2789 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
2790 fheadtell = len(fileheader);
2791 if(fprechecksum!=catfileheadercshex and not skipchecksum):
2792 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(catfp.tell()));
2793 return False;
2794 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
2795 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
2796 if(seekstart<0 and seekstart>fnumfiles):
2797 seekstart = 0;
2798 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
2799 seekend = fnumfiles;
2800 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
2801 seekend = fnumfiles - abs(seekend);
2802 else:
2803 seekend = fnumfiles;
2804 if(seekstart>0):
2805 il = 0;
2806 while(il < seekstart):
2807 preheaderdata = ReadFileHeaderData(catfp, 5, formatspecs[4]);
2808 prefheadsize = int(preheaderdata[0], 16);
2809 prefseek = prefheadsize - (int(len(preheaderdata[1]) + 1) + int(len(preheaderdata[2]) + 1) + int(len(preheaderdata[3]) + 1) + int(len(preheaderdata[4]) + 1));
2810 preftype = int(preheaderdata[1], 16);
2811 prefsize = int(preheaderdata[4], 16);
2812 catfp.seek(prefseek, 1);
2813 catfp.seek(1, 1);
2814 catfp.seek(prefsize, 1);
2815 catfp.seek(1, 1);
2816 il = il + 1;
2817 fileidnum = seekstart;
2818 realidnum = 0;
2819 while(fileidnum<seekend):
2820 catfhstart = catfp.tell();
2821 if(usenewstyle):
2822 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[4]);
2823 else:
2824 catheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[4]);
2825 catfheadsize = int(catheaderdata[0], 16);
2826 catftype = int(catheaderdata[1], 16);
2827 if(re.findall("^[.|/]", catheaderdata[2])):
2828 catfname = catheaderdata[2];
2829 else:
2830 catfname = "./"+catheaderdata[2];
2831 catfbasedir = os.path.dirname(catfname);
2832 catflinkname = catheaderdata[3];
2833 catfsize = int(catheaderdata[4], 16);
2834 catfatime = int(catheaderdata[5], 16);
2835 catfmtime = int(catheaderdata[6], 16);
2836 catfctime = int(catheaderdata[7], 16);
2837 catfbtime = int(catheaderdata[8], 16);
2838 catfmode = int(catheaderdata[9], 16);
2839 catfchmode = stat.S_IMODE(catfmode);
2840 catftypemod = stat.S_IFMT(catfmode);
2841 catfuid = int(catheaderdata[10], 16);
2842 catfuname = catheaderdata[11];
2843 catfgid = int(catheaderdata[12], 16);
2844 catfgname = catheaderdata[13];
2845 fid = int(catheaderdata[14], 16);
2846 finode = int(catheaderdata[15], 16);
2847 flinkcount = int(catheaderdata[16], 16);
2848 catfdev_minor = int(catheaderdata[17], 16);
2849 catfdev_major = int(catheaderdata[18], 16);
2850 catfrdev_minor = int(catheaderdata[19], 16);
2851 catfrdev_major = int(catheaderdata[20], 16);
2852 catfextrasize = int(catheaderdata[21], 16);
2853 catfextrafields = int(catheaderdata[22], 16);
2854 extrafieldslist = [];
2855 if(usenewstyle):
2856 extrastart = 23;
2857 extraend = extrastart + catfextrafields;
2858 extrafieldslist = [];
2859 if(extrastart<extraend):
2860 extrafieldslist.append(catheaderdata[extrastart]);
2861 extrastart = extrastart + 1;
2862 catfchecksumtype = catheaderdata[extrastart].lower();
2863 catfcs = catheaderdata[extrastart + 1].lower();
2864 catfccs = catheaderdata[extrastart + 2].lower();
2865 else:
2866 extrafieldslist = [];
2867 if(catfextrafields>0):
2868 extrafieldslist = ReadFileHeaderData(catfp, catfextrafields, formatspecs[4]);
2869 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[4]);
2870 catfchecksumtype = checksumsval[0].lower();
2871 catfcs = checksumsval[1].lower();
2872 catfccs = checksumsval[2].lower();
2873 hc = 0;
2874 if(usenewstyle):
2875 hcmax = len(catheaderdata) - 2;
2876 else:
2877 hcmax = len(catheaderdata);
2878 hout = "";
2879 while(hc<hcmax):
2880 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[4]);
2881 hc = hc + 1;
2882 catfnumfields = 24 + catfextrafields;
2883 if(catfchecksumtype=="none" or catfchecksumtype==""):
2884 catnewfcs = 0;
2885 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
2886 catnewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
2887 elif(catfchecksumtype=="adler32"):
2888 catnewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
2889 elif(catfchecksumtype=="crc32"):
2890 catnewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
2891 elif(catfchecksumtype=="crc64_ecma"):
2892 catnewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2893 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
2894 catnewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2895 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
2896 checksumoutstr = hashlib.new(catfchecksumtype);
2897 checksumoutstr.update(hout.encode('UTF-8'));
2898 catnewfcs = checksumoutstr.hexdigest().lower();
2899 if(catfcs!=catnewfcs and not skipchecksum):
2900 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
2901 return False;
2902 catfhend = catfp.tell() - 1;
2903 catfcontentstart = catfp.tell();
2904 catfcontents = "";
2905 pyhascontents = False;
2906 if(catfsize>0 and not listonly):
2907 catfcontents = catfp.read(catfsize);
2908 if(catfchecksumtype=="none" or catfchecksumtype==""):
2909 catnewfccs = 0;
2910 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
2911 catnewfccs = format(crc16(catfcontents) & 0xffff, '04x').lower();
2912 elif(catfchecksumtype=="crc16_ccitt"):
2913 catnewfcs = format(crc16_ccitt(catfcontents) & 0xffff, '04x').lower();
2914 elif(catfchecksumtype=="adler32"):
2915 catnewfccs = format(zlib.adler32(catfcontents) & 0xffffffff, '08x').lower();
2916 elif(catfchecksumtype=="crc32"):
2917 catnewfccs = format(crc32(catfcontents) & 0xffffffff, '08x').lower();
2918 elif(catfchecksumtype=="crc64_ecma"):
2919 catnewfcs = format(crc64_ecma(catfcontents) & 0xffffffffffffffff, '016x').lower();
2920 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
2921 catnewfcs = format(crc64_iso(catfcontents) & 0xffffffffffffffff, '016x').lower();
2922 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
2923 checksumoutstr = hashlib.new(catfchecksumtype);
2924 checksumoutstr.update(catfcontents);
2925 catnewfccs = checksumoutstr.hexdigest().lower();
2926 pyhascontents = True;
2927 if(catfccs!=catnewfccs and skipchecksum):
2928 VerbosePrintOut("File Content Checksum Error with file " + catfname + " at offset " + str(catfhstart));
2929 return False;
2930 if(catfsize>0 and listonly):
2931 catfp.seek(catfsize, 1);
2932 pyhascontents = False;
2933 catfp.seek(1, 1);
2934 catfcontentend = catfp.tell() - 1;
2935 catlist['ffilelist'].update({realidnum: {'fid': realidnum, 'fidalt': fileidnum, 'fheadersize': catfheadsize, 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize, 'fatime': catfatime, 'fmtime': catfmtime, 'fctime': catfctime, 'fbtime': catfbtime, 'fmode': catfmode, 'fchmode': catfchmode, 'ftypemod': catftypemod, 'fuid': catfuid, 'funame': catfuname, 'fgid': catfgid, 'fgname': catfgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': catfdev_minor, 'fmajor': catfdev_major, 'frminor': catfrdev_minor, 'frmajor': catfrdev_major, 'fchecksumtype': catfchecksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': catfextrasize, 'fextralist': extrafieldslist, 'fheaderchecksum': catfcs, 'fcontentchecksum': catfccs, 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': catfcontents} });
2936 fileidnum = fileidnum + 1;
2937 realidnum = realidnum + 1;
2938 if(returnfp):
2939 catlist.update({'catfp': catfp});
2940 else:
2941 catfp.close();
2942 return catlist;
2944 create_alias_function("", __file_format_name__, "ToArray", ArchiveFileToArray);
2946 def ArchiveFileStringToArray(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
2947 catfp = BytesIO(catstr);
2948 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
2949 return listcatfiles;
2951 create_alias_function("", __file_format_name__, "StringToArray", ArchiveFileStringToArray);
2953 def TarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
2954 catfp = BytesIO();
2955 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
2956 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
2957 return listcatfiles;
2959 def ZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
2960 catfp = BytesIO();
2961 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
2962 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
2963 return listcatfiles;
2965 if(not rarfile_support):
2966 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
2967 return False;
2969 if(rarfile_support):
2970 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
2971 catfp = BytesIO();
2972 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
2973 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
2974 return listcatfiles;
2976 def ListDirToArrayAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
2977 catver = formatspecs[5];
2978 fileheaderver = str(int(catver.replace(".", "")));
2979 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
2980 advancedlist = True;
2981 infilelist = [];
2982 if(infiles=="-"):
2983 for line in sys.stdin:
2984 infilelist.append(line.strip());
2985 infilelist = list(filter(None, infilelist));
2986 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
2987 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
2988 return False;
2989 with open(infiles, "r") as finfile:
2990 for line in finfile:
2991 infilelist.append(line.strip());
2992 infilelist = list(filter(None, infilelist));
2993 else:
2994 if(isinstance(infiles, (list, tuple, ))):
2995 infilelist = list(filter(None, infiles));
2996 elif(isinstance(infiles, (str, ))):
2997 infilelist = list(filter(None, [infiles]));
2998 if(advancedlist):
2999 GetDirList = ListDirAdvanced(infilelist, followlink, False);
3000 else:
3001 GetDirList = ListDir(infilelist, followlink, False);
3002 if(not GetDirList):
3003 return False;
3004 curinode = 0;
3005 curfid = 0;
3006 inodelist = [];
3007 inodetofile = {};
3008 filetoinode = {};
3009 inodetocatinode = {};
3010 fileidnum = 0;
3011 fnumfiles = int(len(GetDirList));
3012 catver = formatspecs[5];
3013 fileheaderver = str(int(catver.replace(".", "")));
3014 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
3015 fnumfileshex = format(int(fnumfiles), 'x').lower();
3016 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[4]);
3017 catversion = fileheaderver;
3018 if(checksumtype=="none" or checksumtype==""):
3019 catfileheadercshex = format(0, 'x').lower();
3020 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3021 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3022 elif(checksumtype=="crc16_ccitt"):
3023 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3024 elif(checksumtype=="adler32"):
3025 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3026 elif(checksumtype=="crc32"):
3027 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3028 elif(checksumtype=="crc64_ecma"):
3029 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3030 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3031 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3032 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3033 checksumoutstr = hashlib.new(checksumtype);
3034 checksumoutstr.update(fileheader.encode('UTF-8'));
3035 catfileheadercshex = checksumoutstr.hexdigest().lower();
3036 else:
3037 catfileheadercshex = format(0, 'x').lower();
3038 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
3039 fheadtell = len(fileheader);
3040 catlist = {'fnumfiles': fnumfiles, 'fformat': formatspecs[0], 'fversion': catversion, 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
3041 for curfname in GetDirList:
3042 if(re.findall("^[.|/]", curfname)):
3043 fname = curfname;
3044 else:
3045 fname = "./"+curfname;
3046 if(verbose):
3047 VerbosePrintOut(fname);
3048 if(not followlink or followlink is None):
3049 fstatinfo = os.lstat(fname);
3050 else:
3051 fstatinfo = os.stat(fname);
3052 fpremode = fstatinfo.st_mode;
3053 finode = fstatinfo.st_ino;
3054 flinkcount = fstatinfo.st_nlink;
3055 ftype = 0;
3056 if(stat.S_ISREG(fpremode)):
3057 ftype = 0;
3058 elif(stat.S_ISLNK(fpremode)):
3059 ftype = 2;
3060 elif(stat.S_ISCHR(fpremode)):
3061 ftype = 3;
3062 elif(stat.S_ISBLK(fpremode)):
3063 ftype = 4;
3064 elif(stat.S_ISDIR(fpremode)):
3065 ftype = 5;
3066 elif(stat.S_ISFIFO(fpremode)):
3067 ftype = 6;
3068 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
3069 ftype = 8;
3070 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
3071 ftype = 9;
3072 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
3073 ftype = 10;
3074 else:
3075 ftype = 0;
3076 flinkname = "";
3077 fbasedir = os.path.dirname(fname);
3078 fcurfid = curfid;
3079 if(not followlink and finode!=0):
3080 if(ftype!=1):
3081 if(finode in inodelist):
3082 ftype = 1;
3083 flinkname = inodetofile[finode];
3084 fcurinode = inodetocatinode[finode];
3085 if(finode not in inodelist):
3086 inodelist.append(finode);
3087 inodetofile.update({finode: fname});
3088 inodetocatinode.update({finode: curinode});
3089 fcurinode = curinode;
3090 curinode = curinode + 1;
3091 else:
3092 fcurinode = curinode;
3093 curinode = curinode + 1;
3094 curfid = curfid + 1;
3095 if(ftype==2):
3096 flinkname = os.readlink(fname);
3097 fdev = fstatinfo.st_dev;
3098 getfdev = GetDevMajorMinor(fdev);
3099 fdev_minor = getfdev[0];
3100 fdev_major = getfdev[1];
3101 frdev = fstatinfo.st_dev;
3102 if(hasattr(fstatinfo, "st_rdev")):
3103 frdev = fstatinfo.st_rdev;
3104 else:
3105 frdev = fstatinfo.st_dev;
3106 getfrdev = GetDevMajorMinor(frdev);
3107 frdev_minor = getfrdev[0];
3108 frdev_major = getfrdev[1];
3109 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
3110 fsize = "0";
3111 if(ftype==0 or ftype==7):
3112 fsize = fstatinfo.st_size;
3113 fatime = fstatinfo.st_atime;
3114 fmtime = fstatinfo.st_mtime;
3115 fctime = fstatinfo.st_ctime;
3116 if(hasattr(fstatinfo, "st_birthtime")):
3117 fbtime = fstatinfo.st_birthtime;
3118 else:
3119 fbtime = fstatinfo.st_ctime;
3120 fmode = fstatinfo.st_mode;
3121 fchmode = stat.S_IMODE(fstatinfo.st_mode);
3122 ftypemod = stat.S_IFMT(fstatinfo.st_mode);
3123 fuid = fstatinfo.st_uid;
3124 fgid = fstatinfo.st_gid;
3125 funame = "";
3126 try:
3127 import pwd;
3128 try:
3129 userinfo = pwd.getpwuid(fstatinfo.st_uid);
3130 funame = userinfo.pw_name;
3131 except KeyError:
3132 funame = "";
3133 except ImportError:
3134 funame = "";
3135 fgname = "";
3136 try:
3137 import grp;
3138 try:
3139 groupinfo = grp.getgrgid(fstatinfo.st_gid);
3140 fgname = groupinfo.gr_name;
3141 except KeyError:
3142 fgname = "";
3143 except ImportError:
3144 fgname = "";
3145 fdev_minor = fdev_minor;
3146 fdev_major = fdev_major;
3147 frdev_minor = frdev_minor;
3148 frdev_major = frdev_major;
3149 finode = finode;
3150 flinkcount = flinkcount;
3151 if(hasattr(fstatinfo, "st_file_attributes")):
3152 fwinattributes = fstatinfo.st_file_attributes;
3153 else:
3154 fwinattributes = 0;
3155 fcontents = "".encode('UTF-8');
3156 chunk_size = 1024;
3157 if(ftype == 0 or ftype == 7):
3158 with open(fname, "rb") as fpc:
3159 while(True):
3160 chunk = fpc.read(chunk_size);
3161 if(not chunk):
3162 break
3163 fcontents += chunk;
3164 if(followlink and (ftype == 1 or ftype == 2)):
3165 flstatinfo = os.stat(flinkname);
3166 with open(flinkname, "rb") as fpc:
3167 while(True):
3168 chunk = fpc.read(chunk_size);
3169 if(not chunk):
3170 break;
3171 fcontents += chunk;
3172 ftypehex = format(ftype, 'x').lower();
3173 extrafields = len(extradata);
3174 extrafieldslist = extradata;
3175 catfextrafields = extrafields;
3176 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
3177 if(len(extradata)>0):
3178 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
3179 extrasizelen = len(extrasizestr);
3180 extrasizelenhex = format(extrasizelen, 'x').lower();
3181 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[4]);
3182 if(len(extradata)>0):
3183 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
3184 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
3185 catfnumfields = 24 + catfextrafields;
3186 if(checksumtype=="none" or checksumtype==""):
3187 catfileheadercshex = format(0, 'x').lower();
3188 catfilecontentcshex = format(0, 'x').lower();
3189 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3190 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3191 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3192 elif(checksumtype=="crc16_ccitt"):
3193 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3194 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3195 elif(checksumtype=="adler32"):
3196 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3197 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3198 elif(checksumtype=="crc32"):
3199 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3200 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3201 elif(checksumtype=="crc64_ecma"):
3202 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3203 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3204 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3205 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3206 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3207 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3208 checksumoutstr = hashlib.new(checksumtype);
3209 checksumoutstr.update("".encode('UTF-8'));
3210 catfileheadercshex = checksumoutstr.hexdigest().lower();
3211 checksumoutstr = hashlib.new(checksumtype);
3212 checksumoutstr.update(fcontents);
3213 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3214 else:
3215 catfileheadercshex = format(0, 'x').lower();
3216 catfilecontentcshex = format(0, 'x').lower();
3217 catfhstart = fheadtell;
3218 fheadtell += len(catfileoutstr);
3219 catfhend = fheadtell - 1;
3220 catfcontentstart = fheadtell;
3221 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3222 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3223 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
3224 if(checksumtype=="none" or checksumtype==""):
3225 catfileheadercshex = format(0, 'x').lower();
3226 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3227 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3228 elif(checksumtype=="crc16_ccitt"):
3229 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3230 elif(checksumtype=="adler32"):
3231 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3232 elif(checksumtype=="crc32"):
3233 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3234 elif(checksumtype=="crc64_ecma"):
3235 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3236 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3237 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3238 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3239 checksumoutstr = hashlib.new(checksumtype);
3240 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3241 catfileheadercshex = checksumoutstr.hexdigest().lower();
3242 else:
3243 catfileheadercshex = format(0, 'x').lower();
3244 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3245 catfileoutstrecd = catfileoutstr.encode('UTF-8');
3246 nullstrecd = formatspecs[4].encode('UTF-8');
3247 fheadtell += len(catfileoutstr) + 1;
3248 catfcontentend = fheadtell - 1;
3249 catfileout = catfileoutstrecd + fcontents + nullstrecd;
3250 pyhascontents = False;
3251 if(int(fsize)>0 and not listonly):
3252 pyhascontents = True;
3253 if(int(fsize)>0 and listonly):
3254 fcontents = "";
3255 pyhascontents = False;
3256 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
3257 fileidnum = fileidnum + 1;
3258 return catlist;
3260 def TarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
3261 curinode = 0;
3262 curfid = 0;
3263 inodelist = [];
3264 inodetofile = {};
3265 filetoinode = {};
3266 inodetocatinode = {};
3267 fileidnum = 0;
3268 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
3269 return False;
3270 try:
3271 if(not tarfile.is_tarfile(infiles)):
3272 return False;
3273 except AttributeError:
3274 if(not is_tarfile(infiles)):
3275 return False;
3276 try:
3277 tarfp = tarfile.open(infiles, "r");
3278 except FileNotFoundError:
3279 return False;
3280 fnumfiles = int(len(tarfp.getmembers()));
3281 catver = formatspecs[5];
3282 fileheaderver = str(int(catver.replace(".", "")));
3283 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
3284 fnumfileshex = format(int(fnumfiles), 'x').lower();
3285 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[4]);
3286 catversion = fileheaderver;
3287 if(checksumtype=="none" or checksumtype==""):
3288 catfileheadercshex = format(0, 'x').lower();
3289 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3290 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3291 elif(checksumtype=="crc16_ccitt"):
3292 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3293 elif(checksumtype=="adler32"):
3294 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3295 elif(checksumtype=="crc32"):
3296 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3297 elif(checksumtype=="crc64_ecma"):
3298 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3299 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3300 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3301 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3302 checksumoutstr = hashlib.new(checksumtype);
3303 checksumoutstr.update(fileheader.encode('UTF-8'));
3304 catfileheadercshex = checksumoutstr.hexdigest().lower();
3305 else:
3306 catfileheadercshex = format(0, 'x').lower();
3307 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
3308 fheadtell = len(fileheader);
3309 catlist = {'fnumfiles': fnumfiles, 'fformat': formatspecs[0], 'fversion': catversion, 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
3310 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
3311 if(re.findall("^[.|/]", member.name)):
3312 fname = member.name;
3313 else:
3314 fname = "./"+member.name;
3315 if(verbose):
3316 VerbosePrintOut(fname);
3317 fpremode = member.mode;
3318 ffullmode = member.mode;
3319 flinkcount = 0;
3320 ftype = 0;
3321 if(member.isreg()):
3322 ffullmode = member.mode + stat.S_IFREG;
3323 ftype = 0;
3324 elif(member.isdev()):
3325 ffullmode = member.mode;
3326 ftype = 7;
3327 elif(member.islnk()):
3328 ffullmode = member.mode + stat.S_IFREG;
3329 ftype = 1;
3330 elif(member.issym()):
3331 ffullmode = member.mode + stat.S_IFLNK;
3332 ftype = 2;
3333 elif(member.ischr()):
3334 ffullmode = member.mode + stat.S_IFCHR;
3335 ftype = 3;
3336 elif(member.isblk()):
3337 ffullmode = member.mode + stat.S_IFBLK;
3338 ftype = 4;
3339 elif(member.isdir()):
3340 ffullmode = member.mode + stat.S_IFDIR;
3341 ftype = 5;
3342 elif(member.isfifo()):
3343 ffullmode = member.mode + stat.S_IFIFO;
3344 ftype = 6;
3345 elif(member.issparse()):
3346 ffullmode = member.mode;
3347 ftype = 8;
3348 else:
3349 ffullmode = member.mode;
3350 ftype = 0;
3351 flinkname = "";
3352 fbasedir = os.path.dirname(fname);
3353 fcurfid = curfid;
3354 fcurinode = 0;
3355 finode = fcurinode;
3356 curfid = curfid + 1;
3357 if(ftype==2):
3358 flinkname = member.linkname;
3359 fdev_minor = member.devminor;
3360 fdev_major = member.devmajor;
3361 frdev_minor = member.devminor;
3362 frdev_major = member.devmajor;
3363 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
3364 fsize = "0";
3365 elif(ftype==0 or ftype==7):
3366 fsize = member.size;
3367 else:
3368 fsize = member.size;
3369 fatime = member.mtime;
3370 fmtime = member.mtime;
3371 fctime = member.mtime;
3372 fbtime = member.mtime;
3373 fmode = ffullmode;
3374 fchmode = stat.S_IMODE(ffullmode);
3375 ftypemod = stat.S_IFMT(ffullmode);
3376 fuid = member.uid;
3377 fgid = member.gid;
3378 funame = member.uname;
3379 fgname = member.gname;
3380 flinkcount = flinkcount;
3381 fcontents = "".encode('UTF-8');
3382 chunk_size = 1024;
3383 if(ftype == 0 or ftype == 7):
3384 with tarfp.extractfile(member) as fpc:
3385 while(True):
3386 chunk = fpc.read(chunk_size);
3387 if(not chunk):
3388 break
3389 fcontents += chunk;
3390 ftypehex = format(ftype, 'x').lower();
3391 extrafields = len(extradata);
3392 extrafieldslist = extradata;
3393 catfextrafields = extrafields;
3394 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
3395 if(len(extradata)>0):
3396 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
3397 extrasizelen = len(extrasizestr);
3398 extrasizelenhex = format(extrasizelen, 'x').lower();
3399 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[4]);
3400 if(len(extradata)>0):
3401 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
3402 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
3403 catfnumfields = 24 + catfextrafields;
3404 if(checksumtype=="none" or checksumtype==""):
3405 catfileheadercshex = format(0, 'x').lower();
3406 catfilecontentcshex = format(0, 'x').lower();
3407 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3408 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3409 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3410 elif(checksumtype=="crc16_ccitt"):
3411 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3412 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3413 elif(checksumtype=="adler32"):
3414 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3415 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3416 elif(checksumtype=="crc32"):
3417 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3418 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3419 elif(checksumtype=="crc64_ecma"):
3420 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3421 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3422 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3423 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3424 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3425 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3426 checksumoutstr = hashlib.new(checksumtype);
3427 checksumoutstr.update("".encode('UTF-8'));
3428 catfileheadercshex = checksumoutstr.hexdigest().lower();
3429 checksumoutstr = hashlib.new(checksumtype);
3430 checksumoutstr.update(fcontents);
3431 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3432 else:
3433 catfileheadercshex = format(0, 'x').lower();
3434 catfilecontentcshex = format(0, 'x').lower();
3435 catfhstart = fheadtell;
3436 fheadtell += len(catfileoutstr);
3437 catfhend = fheadtell - 1;
3438 catfcontentstart = fheadtell;
3439 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3440 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3441 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
3442 if(checksumtype=="none" or checksumtype==""):
3443 catfileheadercshex = format(0, 'x').lower();
3444 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3445 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3446 elif(checksumtype=="crc16_ccitt"):
3447 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3448 elif(checksumtype=="adler32"):
3449 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3450 elif(checksumtype=="crc32"):
3451 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3452 elif(checksumtype=="crc64_ecma"):
3453 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3454 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3455 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3456 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3457 checksumoutstr = hashlib.new(checksumtype);
3458 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3459 catfileheadercshex = checksumoutstr.hexdigest().lower();
3460 else:
3461 catfileheadercshex = format(0, 'x').lower();
3462 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3463 catfileoutstrecd = catfileoutstr.encode('UTF-8');
3464 nullstrecd = formatspecs[4].encode('UTF-8');
3465 fheadtell += len(catfileoutstr) + 1;
3466 catfcontentend = fheadtell - 1;
3467 catfileout = catfileoutstrecd + fcontents + nullstrecd;
3468 pyhascontents = False;
3469 if(int(fsize)>0 and not listonly):
3470 pyhascontents = True;
3471 if(int(fsize)>0 and listonly):
3472 fcontents = "";
3473 pyhascontents = False;
3474 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
3475 fileidnum = fileidnum + 1;
3476 return catlist;
3478 def ZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
3479 advancedlist = True;
3480 curinode = 0;
3481 curfid = 0;
3482 inodelist = [];
3483 inodetofile = {};
3484 filetoinode = {};
3485 inodetocatinode = {};
3486 fileidnum = 0;
3487 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
3488 return False;
3489 if(not zipfile.is_zipfile(infiles)):
3490 return False;
3491 zipfp = zipfile.ZipFile(infiles, "r", allowZip64=True);
3492 ziptest = zipfp.testzip();
3493 if(ziptest):
3494 VerbosePrintOut("Bad file found!");
3495 fnumfiles = int(len(zipfp.infolist()));
3496 catver = formatspecs[5];
3497 fileheaderver = str(int(catver.replace(".", "")));
3498 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
3499 catversion = fileheaderver;
3500 fnumfileshex = format(int(fnumfiles), 'x').lower();
3501 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[4]);
3502 if(checksumtype=="none" or checksumtype==""):
3503 catfileheadercshex = format(0, 'x').lower();
3504 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3505 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3506 elif(checksumtype=="crc16_ccitt"):
3507 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3508 elif(checksumtype=="adler32"):
3509 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3510 elif(checksumtype=="crc32"):
3511 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3512 elif(checksumtype=="crc64_ecma"):
3513 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3514 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3515 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3516 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3517 checksumoutstr = hashlib.new(checksumtype);
3518 checksumoutstr.update(fileheader.encode('UTF-8'));
3519 catfileheadercshex = checksumoutstr.hexdigest().lower();
3520 else:
3521 catfileheadercshex = format(0, 'x').lower();
3522 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
3523 fheadtell = len(fileheader);
3524 catlist = {'fnumfiles': fnumfiles, 'fformat': formatspecs[0], 'fversion': catversion, 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
3525 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
3526 if(re.findall("^[.|/]", member.filename)):
3527 fname = member.filename;
3528 else:
3529 fname = "./"+member.filename;
3530 zipinfo = zipfp.getinfo(member.filename);
3531 if(verbose):
3532 VerbosePrintOut(fname);
3533 if(not member.is_dir()):
3534 fpremode = stat.S_IFREG + 438;
3535 elif(member.is_dir()):
3536 fpremode = stat.S_IFDIR + 511;
3537 flinkcount = 0;
3538 ftype = 0;
3539 if(not member.is_dir()):
3540 ftype = 0;
3541 elif(member.is_dir()):
3542 ftype = 5;
3543 flinkname = "";
3544 fbasedir = os.path.dirname(fname);
3545 fcurfid = curfid;
3546 fcurinode = 0;
3547 finode = fcurinode;
3548 curfid = curfid + 1;
3549 fdev_minor = 0;
3550 fdev_major = 0;
3551 frdev_minor = 0;
3552 frdev_major = 0;
3553 if(ftype==5):
3554 fsize = "0";
3555 elif(ftype==0):
3556 fsize = member.file_size;
3557 else:
3558 fsize = member.file_size;
3559 fatime = time.mktime(member.date_time + (0, 0, -1));
3560 fmtime = time.mktime(member.date_time + (0, 0, -1));
3561 fctime = time.mktime(member.date_time + (0, 0, -1));
3562 fbtime = time.mktime(member.date_time + (0, 0, -1));
3563 if(not member.is_dir()):
3564 fmode = stat.S_IFREG + 438;
3565 fchmode = stat.S_IMODE(int(stat.S_IFREG + 438));
3566 ftypemod = stat.S_IFMT(int(stat.S_IFREG + 438));
3567 if(member.is_dir()):
3568 fmode = stat.S_IFDIR + 511;
3569 fchmode = stat.S_IMODE(int(stat.S_IFDIR + 511));
3570 ftypemod = stat.S_IFMT(int(stat.S_IFDIR + 511));
3571 try:
3572 fuid = os.getuid();
3573 except AttributeError:
3574 fuid = 0;
3575 except KeyError:
3576 fuid = 0;
3577 try:
3578 fgid = os.getgid();
3579 except AttributeError:
3580 fgid = 0;
3581 except KeyError:
3582 fgid = 0;
3583 try:
3584 import pwd;
3585 try:
3586 userinfo = pwd.getpwuid(os.getuid());
3587 funame = userinfo.pw_name;
3588 except KeyError:
3589 funame = "";
3590 except AttributeError:
3591 funame = "";
3592 except ImportError:
3593 funame = "";
3594 fgname = "";
3595 try:
3596 import grp;
3597 try:
3598 groupinfo = grp.getgrgid(os.getgid());
3599 fgname = groupinfo.gr_name;
3600 except KeyError:
3601 fgname = "";
3602 except AttributeError:
3603 fgname = "";
3604 except ImportError:
3605 fgname = "";
3606 fcontents = "".encode('UTF-8');
3607 if(ftype==0):
3608 fcontents = zipfp.read(member.filename);
3609 ftypehex = format(ftype, 'x').lower();
3610 extrafields = len(extradata);
3611 extrafieldslist = extradata;
3612 catfextrafields = extrafields;
3613 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
3614 if(len(extradata)>0):
3615 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
3616 extrasizelen = len(extrasizestr);
3617 extrasizelenhex = format(extrasizelen, 'x').lower();
3618 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[4]);
3619 if(len(extradata)>0):
3620 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
3621 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
3622 catfnumfields = 24 + catfextrafields;
3623 if(checksumtype=="none" or checksumtype==""):
3624 catfileheadercshex = format(0, 'x').lower();
3625 catfilecontentcshex = format(0, 'x').lower();
3626 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3627 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3628 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3629 elif(checksumtype=="crc16_ccitt"):
3630 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3631 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3632 elif(checksumtype=="adler32"):
3633 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3634 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3635 elif(checksumtype=="crc32"):
3636 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3637 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3638 elif(checksumtype=="crc64_ecma"):
3639 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3640 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3641 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3642 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3643 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3644 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3645 checksumoutstr = hashlib.new(checksumtype);
3646 checksumoutstr.update("".encode('UTF-8'));
3647 catfileheadercshex = checksumoutstr.hexdigest().lower();
3648 checksumoutstr = hashlib.new(checksumtype);
3649 checksumoutstr.update(fcontents);
3650 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3651 else:
3652 catfileheadercshex = format(0, 'x').lower();
3653 catfilecontentcshex = format(0, 'x').lower();
3654 catfhstart = fheadtell;
3655 fheadtell += len(catfileoutstr);
3656 catfhend = fheadtell - 1;
3657 catfcontentstart = fheadtell;
3658 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3659 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3660 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
3661 if(checksumtype=="none" or checksumtype==""):
3662 catfileheadercshex = format(0, 'x').lower();
3663 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3664 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3665 elif(checksumtype=="crc16_ccitt"):
3666 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3667 elif(checksumtype=="adler32"):
3668 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3669 elif(checksumtype=="crc32"):
3670 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3671 elif(checksumtype=="crc64_ecma"):
3672 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3673 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3674 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3675 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3676 checksumoutstr = hashlib.new(checksumtype);
3677 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3678 catfileheadercshex = checksumoutstr.hexdigest().lower();
3679 else:
3680 catfileheadercshex = format(0, 'x').lower();
3681 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3682 catfileoutstrecd = catfileoutstr.encode('UTF-8');
3683 nullstrecd = formatspecs[4].encode('UTF-8');
3684 fheadtell += len(catfileoutstr) + 1;
3685 catfcontentend = fheadtell - 1;
3686 catfileout = catfileoutstrecd + fcontents + nullstrecd;
3687 pyhascontents = False;
3688 if(int(fsize)>0 and not listonly):
3689 pyhascontents = True;
3690 if(int(fsize)>0 and listonly):
3691 fcontents = "";
3692 pyhascontents = False;
3693 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
3694 fileidnum = fileidnum + 1;
3695 return catlist;
3697 if(not rarfile_support):
3698 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
3699 return False;
3701 if(rarfile_support):
3702 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
3703 advancedlist = True;
3704 curinode = 0;
3705 curfid = 0;
3706 inodelist = [];
3707 inodetofile = {};
3708 filetoinode = {};
3709 inodetocatinode = {};
3710 fileidnum = 0;
3711 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
3712 return False;
3713 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
3714 return False;
3715 rarfp = rarfile.RarFile(infile, "r");
3716 rartest = rarfp.testrar();
3717 if(rartest):
3718 VerbosePrintOut("Bad file found!");
3719 fnumfiles = int(len(rarfp.infolist()));
3720 catver = formatspecs[5];
3721 fileheaderver = str(int(catver.replace(".", "")));
3722 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
3723 catversion = fileheaderver;
3724 fnumfileshex = format(int(fnumfiles), 'x').lower();
3725 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[4]);
3726 if(checksumtype=="none" or checksumtype==""):
3727 catfileheadercshex = format(0, 'x').lower();
3728 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3729 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3730 elif(checksumtype=="crc16_ccitt"):
3731 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3732 elif(checksumtype=="adler32"):
3733 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3734 elif(checksumtype=="crc32"):
3735 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3736 elif(checksumtype=="crc64_ecma"):
3737 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3738 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3739 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3740 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3741 checksumoutstr = hashlib.new(checksumtype);
3742 checksumoutstr.update(fileheader.encode('UTF-8'));
3743 catfileheadercshex = checksumoutstr.hexdigest().lower();
3744 else:
3745 catfileheadercshex = format(0, 'x').lower();
3746 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[4]);
3747 fheadtell = len(fileheader);
3748 catlist = {'fnumfiles': fnumfiles, 'fformat': formatspecs[0], 'fversion': catversion, 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
3749 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
3750 is_unix = False;
3751 is_windows = False;
3752 if(member.host_os == rarfile.RAR_OS_UNIX):
3753 is_windows = False;
3754 try:
3755 member.external_attr
3756 is_unix = True;
3757 except AttributeError:
3758 is_unix = False;
3759 elif(member.host_os == rarfile.RAR_OS_WIN32):
3760 is_unix = False;
3761 try:
3762 member.external_attr
3763 is_windows = True;
3764 except AttributeError:
3765 is_windows = False;
3766 else:
3767 is_unix = False;
3768 is_windows = False;
3769 if(re.findall("^[.|/]", member.filename)):
3770 fname = member.filename;
3771 else:
3772 fname = "./"+member.filename;
3773 rarinfo = rarfp.getinfo(member.filename);
3774 if(verbose):
3775 VerbosePrintOut(fname);
3776 if(is_unix and member.external_attr !=0):
3777 fpremode = int(member.external_attr);
3778 elif(member.is_file()):
3779 fpremode = stat.S_IFREG + 438;
3780 elif(member.is_symlink()):
3781 fpremode = stat.S_IFLNK + 438;
3782 elif(member.is_dir()):
3783 fpremode = stat.S_IFDIR + 511;
3784 if(is_windows and member.external_attr !=0):
3785 fwinattributes = int(member.external_attr);
3786 else:
3787 fwinattributes = int(0);
3788 flinkcount = 0;
3789 ftype = 0;
3790 if(member.is_file()):
3791 ftype = 0;
3792 elif(member.is_symlink()):
3793 ftype = 2;
3794 elif(member.is_dir()):
3795 ftype = 5;
3796 flinkname = "";
3797 if(ftype==2):
3798 flinkname = rarfp.read(member.filename).decode("UTF-8");
3799 fbasedir = os.path.dirname(fname);
3800 fcurfid = curfid;
3801 fcurinode = 0;
3802 finode = fcurinode;
3803 curfid = curfid + 1;
3804 fdev_minor = 0;
3805 fdev_major = 0;
3806 frdev_minor = 0;
3807 frdev_major = 0;
3808 if(ftype==5):
3809 fsize = "0";
3810 if(ftype==0):
3811 fsize = member.file_size;
3812 try:
3813 if(member.atime):
3814 fatime = int(member.atime.timestamp());
3815 else:
3816 fatime = int(member.mtime.timestamp());
3817 except AttributeError:
3818 fatime = int(member.mtime.timestamp());
3819 fmtime = int(member.mtime.timestamp());
3820 try:
3821 if(member.ctime):
3822 fctime = int(member.ctime.timestamp());
3823 else:
3824 fctime = int(member.mtime.timestamp());
3825 except AttributeError:
3826 fctime = int(member.mtime.timestamp());
3827 fbtime = int(member.mtime.timestamp());
3828 if(is_unix and member.external_attr !=0):
3829 fmode = format(int(member.external_attr), 'x').lower();
3830 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
3831 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
3832 elif(member.is_file()):
3833 fmode = int(stat.S_IFREG + 438)
3834 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
3835 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
3836 elif(member.is_symlink()):
3837 fmode = int(stat.S_IFLNK + 438)
3838 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
3839 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
3840 elif(member.is_dir()):
3841 fmode = int(stat.S_IFDIR + 511)
3842 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
3843 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
3844 try:
3845 fuid = os.getuid();
3846 except AttributeError:
3847 fuid = 0;
3848 except KeyError:
3849 fuid = 0;
3850 try:
3851 fgid = os.getgid();
3852 except AttributeError:
3853 fgid = 0;
3854 except KeyError:
3855 fgid = 0;
3856 try:
3857 import pwd;
3858 try:
3859 userinfo = pwd.getpwuid(os.getuid());
3860 funame = userinfo.pw_name;
3861 except KeyError:
3862 funame = "";
3863 except AttributeError:
3864 funame = "";
3865 except ImportError:
3866 funame = "";
3867 fgname = "";
3868 try:
3869 import grp;
3870 try:
3871 groupinfo = grp.getgrgid(os.getgid());
3872 fgname = groupinfo.gr_name;
3873 except KeyError:
3874 fgname = "";
3875 except AttributeError:
3876 fgname = "";
3877 except ImportError:
3878 fgname = "";
3879 fcontents = "".encode('UTF-8');
3880 if(ftype==0):
3881 fcontents = rarfp.read(member.filename);
3882 ftypehex = format(ftype, 'x').lower();
3883 extrafields = len(extradata);
3884 extrafieldslist = extradata;
3885 catfextrafields = extrafields;
3886 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
3887 if(len(extradata)>0):
3888 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
3889 extrasizelen = len(extrasizestr);
3890 extrasizelenhex = format(extrasizelen, 'x').lower();
3891 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[4]);
3892 if(len(extradata)>0):
3893 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[4]);
3894 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[4]);
3895 catfnumfields = 24 + catfextrafields;
3896 if(checksumtype=="none" or checksumtype==""):
3897 catfileheadercshex = format(0, 'x').lower();
3898 catfilecontentcshex = format(0, 'x').lower();
3899 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3900 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3901 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3902 elif(checksumtype=="crc16_ccitt"):
3903 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3904 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3905 elif(checksumtype=="adler32"):
3906 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3907 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3908 elif(checksumtype=="crc32"):
3909 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3910 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3911 elif(checksumtype=="crc64_ecma"):
3912 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3913 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3914 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3915 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3916 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3917 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3918 checksumoutstr = hashlib.new(checksumtype);
3919 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3920 catfileheadercshex = checksumoutstr.hexdigest().lower();
3921 else:
3922 catfileheadercshex = format(0, 'x').lower();
3923 catfilecontentcshex = format(0, 'x').lower();
3924 catfhstart = fheadtell;
3925 fheadtell += len(catfileoutstr);
3926 catfhend = fheadtell - 1;
3927 catfcontentstart = fheadtell;
3928 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3929 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3930 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
3931 if(checksumtype=="none" or checksumtype==""):
3932 catfileheadercshex = format(0, 'x').lower();
3933 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3934 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3935 elif(checksumtype=="crc16_ccitt"):
3936 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3937 elif(checksumtype=="adler32"):
3938 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3939 elif(checksumtype=="crc32"):
3940 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3941 elif(checksumtype=="crc64_ecma"):
3942 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3943 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3944 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3945 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3946 checksumoutstr = hashlib.new(checksumtype);
3947 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3948 catfileheadercshex = checksumoutstr.hexdigest().lower();
3949 else:
3950 catfileheadercshex = format(0, 'x').lower();
3951 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
3952 catfileoutstrecd = catfileoutstr.encode('UTF-8');
3953 nullstrecd = formatspecs[4].encode('UTF-8');
3954 fheadtell += len(catfileoutstr) + 1;
3955 catfcontentend = fheadtell - 1;
3956 catfileout = catfileoutstrecd + fcontents + nullstrecd;
3957 pyhascontents = False;
3958 if(int(fsize)>0 and not listonly):
3959 pyhascontents = True;
3960 if(int(fsize)>0 and listonly):
3961 fcontents = "";
3962 pyhascontents = False;
3963 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
3964 fileidnum = fileidnum + 1;
3965 return catlist;
3967 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, usenewstyle=True, returnfp=False):
3968 outarray = BytesIO();
3969 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
3970 listcatfiles = ArchiveFileToArray(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
3971 return listcatfiles;
3973 def ArchiveFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
3974 if(isinstance(infile, dict)):
3975 listcatfiles = infile;
3976 else:
3977 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
3978 infile = RemoveWindowsPath(infile);
3979 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
3980 if(not listcatfiles):
3981 return False;
3982 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': {}}}};
3983 if(returnfp):
3984 catarray.update({'catfp': listcatfiles['catfp']});
3985 lenlist = len(listcatfiles['ffilelist']);
3986 if(seekstart>0):
3987 lcfi = seekstart;
3988 else:
3989 lcfi = 0;
3990 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
3991 lcfx = seekend;
3992 else:
3993 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
3994 lcfx = listcatfiles['fnumfiles'];
3995 else:
3996 lcfx = int(listcatfiles['fnumfiles']);
3997 while(lcfi < lcfx):
3998 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
3999 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4000 catarray['filetoid'].update(filetoidarray);
4001 catarray['idtofile'].update(idtofilearray);
4002 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4003 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4004 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4005 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4006 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4007 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4008 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4009 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4010 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4011 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4012 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4013 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4014 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4015 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4016 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4017 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4018 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4019 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4020 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4021 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4022 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4023 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4024 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4025 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4026 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4027 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4028 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4029 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4030 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4031 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4032 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4033 lcfi = lcfi + 1;
4034 return catarray;
4036 create_alias_function("", __file_format_name__, "ToArrayIndex", ArchiveFileToArrayIndex);
4038 def ListDirToArrayIndexAlt(infiles, dirlistfromtxt=False, followlink=False, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4039 listcatfiles = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, extradata, formatspecs, verbose);
4040 if(not listcatfiles):
4041 return False;
4042 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': {}}}};
4043 lenlist = len(listcatfiles['ffilelist']);
4044 if(seekstart>0):
4045 lcfi = seekstart;
4046 else:
4047 lcfi = 0;
4048 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4049 lcfx = seekend;
4050 else:
4051 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4052 lcfx = listcatfiles['fnumfiles'];
4053 else:
4054 lcfx = int(listcatfiles['fnumfiles']);
4055 while(lcfi < lcfx):
4056 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4057 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4058 catarray['filetoid'].update(filetoidarray);
4059 catarray['idtofile'].update(idtofilearray);
4060 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4061 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4062 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4063 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4064 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4065 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4066 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4067 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4068 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4069 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4070 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4071 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4072 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4073 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4074 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4075 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4076 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4077 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4078 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4079 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4080 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4081 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4082 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4083 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4084 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4085 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4086 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4087 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4088 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4089 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4090 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4091 lcfi = lcfi + 1;
4092 return catarray;
4094 def TarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4095 listcatfiles = TarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
4096 if(not listcatfiles):
4097 return False;
4098 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': {}}}};
4099 lenlist = len(listcatfiles['ffilelist']);
4100 if(seekstart>0):
4101 lcfi = seekstart;
4102 else:
4103 lcfi = 0;
4104 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4105 lcfx = seekend;
4106 else:
4107 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4108 lcfx = listcatfiles['fnumfiles'];
4109 else:
4110 lcfx = int(listcatfiles['fnumfiles']);
4111 while(lcfi < lcfx):
4112 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4113 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4114 catarray['filetoid'].update(filetoidarray);
4115 catarray['idtofile'].update(idtofilearray);
4116 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4117 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4118 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4119 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4120 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4121 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4122 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4123 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4124 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4125 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4126 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4127 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4128 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4129 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4130 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4131 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4132 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4133 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4134 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4135 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4136 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4137 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4138 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4139 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4140 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4141 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4142 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4143 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4144 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4145 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4146 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4147 lcfi = lcfi + 1;
4148 return catarray;
4150 def ZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4151 listcatfiles = ZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
4152 if(not listcatfiles):
4153 return False;
4154 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': {}}}};
4155 lenlist = len(listcatfiles['ffilelist']);
4156 if(seekstart>0):
4157 lcfi = seekstart;
4158 else:
4159 lcfi = 0;
4160 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4161 lcfx = seekend;
4162 else:
4163 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4164 lcfx = listcatfiles['fnumfiles'];
4165 else:
4166 lcfx = int(listcatfiles['fnumfiles']);
4167 while(lcfi < lcfx):
4168 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4169 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4170 catarray['filetoid'].update(filetoidarray);
4171 catarray['idtofile'].update(idtofilearray);
4172 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4173 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4174 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4175 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4176 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4177 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4178 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4179 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4180 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4181 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4182 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4183 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4184 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4185 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4186 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4187 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4188 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4189 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4190 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4191 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4192 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4193 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4194 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4195 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4196 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4197 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4198 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4199 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4200 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4201 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4202 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4203 lcfi = lcfi + 1;
4204 return catarray;
4206 if(not rarfile_support):
4207 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4208 return False;
4210 if(rarfile_support):
4211 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4212 listcatfiles = RarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
4213 if(not listcatfiles):
4214 return False;
4215 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': {}}}};
4216 lenlist = len(listcatfiles['ffilelist']);
4217 if(seekstart>0):
4218 lcfi = seekstart;
4219 else:
4220 lcfi = 0;
4221 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4222 lcfx = seekend;
4223 else:
4224 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4225 lcfx = listcatfiles['fnumfiles'];
4226 else:
4227 lcfx = int(listcatfiles['fnumfiles']);
4228 while(lcfi < lcfx):
4229 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4230 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4231 catarray['filetoid'].update(filetoidarray);
4232 catarray['idtofile'].update(idtofilearray);
4233 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4234 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4235 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4236 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4237 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4238 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4239 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4240 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4241 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4242 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4243 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4244 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4245 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4246 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4247 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4248 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4249 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4250 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4251 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4252 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4253 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4254 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4255 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4256 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4257 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4258 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4259 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4260 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4261 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4262 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4263 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4264 lcfi = lcfi + 1;
4265 return catarray;
4267 def ArchiveFileStringToArrayIndex(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
4268 catfp = BytesIO(catstr);
4269 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
4270 return listcatfiles;
4272 create_alias_function("", __file_format_name__, "StringToArrayIndex", ArchiveFileStringToArrayIndex);
4274 def TarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
4275 catfp = BytesIO();
4276 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4277 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
4278 return listcatfiles;
4280 def ZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
4281 catfp = BytesIO();
4282 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4283 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
4284 return listcatfiles;
4286 if(not rarfile_support):
4287 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4288 return False;
4290 if(rarfile_support):
4291 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, usenewstyle=True, returnfp=False):
4292 catfp = BytesIO();
4293 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
4294 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp);
4295 return listcatfiles;
4297 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, usenewstyle=True, returnfp=False):
4298 outarray = BytesIO();
4299 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, verbose, True);
4300 listcatfiles = ArchiveFileToArrayIndex(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, usenewstyle, returnfp)
4301 return listcatfiles;
4303 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, usenewstyle=True, returnfp=False):
4304 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
4305 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
4306 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
4307 if(isinstance(infile, dict)):
4308 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4309 listcatfiles = prelistcatfiles['list'];
4310 else:
4311 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
4312 infile = RemoveWindowsPath(infile);
4313 if(followlink):
4314 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4315 listcatfiles = prelistcatfiles['list'];
4316 else:
4317 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4318 if(outfile!="-" and not hasattr(infile, "read") and not hasattr(outfile, "write")):
4319 outfile = RemoveWindowsPath(outfile);
4320 checksumtype = checksumtype.lower();
4321 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
4322 checksumtype="crc32";
4323 if(checksumtype=="none"):
4324 checksumtype = "";
4325 if(not compression or compression or compression=="catfile" or compression==formatspecs[1]):
4326 compression = None;
4327 if(compression not in compressionlist and compression is None):
4328 compression = "auto";
4329 if(verbose):
4330 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4331 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
4332 if(os.path.exists(outfile)):
4333 os.unlink(outfile);
4334 if(not listcatfiles):
4335 return False;
4336 if(outfile=="-"):
4337 verbose = False;
4338 catfp = BytesIO();
4339 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
4340 catfp = outfile;
4341 else:
4342 fbasename = os.path.splitext(outfile)[0];
4343 fextname = os.path.splitext(outfile)[1];
4344 catfp = CompressOpenFile(outfile, compressionlevel);
4345 catver = formatspecs[5];
4346 fileheaderver = str(int(catver.replace(".", "")));
4347 fileheader = AppendNullByte(formatspecs[0] + fileheaderver, formatspecs[4]);
4348 catfp.write(fileheader.encode('UTF-8'));
4349 lenlist = len(listcatfiles['ffilelist']);
4350 fnumfiles = int(listcatfiles['fnumfiles']);
4351 if(lenlist>fnumfiles or lenlist<fnumfiles):
4352 fnumfiles = lenlist;
4353 fnumfileshex = format(int(fnumfiles), 'x').lower();
4354 fnumfilesa = AppendNullBytes([fnumfileshex, checksumtype], formatspecs[4]);
4355 if(checksumtype=="none" or checksumtype==""):
4356 catfileheadercshex = format(0, 'x').lower();
4357 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4358 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
4359 elif(checksumtype=="crc16_ccitt"):
4360 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
4361 elif(checksumtype=="adler32"):
4362 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
4363 elif(checksumtype=="crc32"):
4364 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
4365 elif(checksumtype=="crc64_ecma"):
4366 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4367 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4368 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4369 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4370 checksumoutstr = hashlib.new(checksumtype);
4371 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
4372 catfileheadercshex = checksumoutstr.hexdigest().lower();
4373 else:
4374 catfileheadercshex = format(0, 'x').lower();
4375 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[4]);
4376 catfp.write(fnumfilesa.encode('UTF-8'));
4377 try:
4378 catfp.flush();
4379 os.fsync(catfp.fileno());
4380 except io.UnsupportedOperation:
4381 pass;
4382 except AttributeError:
4383 pass;
4384 if(seekstart>0):
4385 lcfi = seekstart;
4386 else:
4387 lcfi = 0;
4388 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4389 lcfx = seekend;
4390 else:
4391 lcfx = int(listcatfiles['fnumfiles']);
4392 curinode = 0;
4393 curfid = 0;
4394 inodelist = [];
4395 inodetofile = {};
4396 filetoinode = {};
4397 reallcfi = 0;
4398 while(lcfi < lcfx):
4399 catfhstart = catfp.tell();
4400 if(re.findall("^[.|/]", listcatfiles['ffilelist'][reallcfi]['fname'])):
4401 fname = listcatfiles['ffilelist'][reallcfi]['fname'];
4402 else:
4403 fname = "./"+listcatfiles['ffilelist'][reallcfi]['fname'];
4404 if(verbose):
4405 VerbosePrintOut(fname);
4406 fheadersize = format(int(listcatfiles['ffilelist'][reallcfi]['fheadersize']), 'x').lower();
4407 fsize = format(int(listcatfiles['ffilelist'][reallcfi]['fsize']), 'x').lower();
4408 flinkname = listcatfiles['ffilelist'][reallcfi]['flinkname'];
4409 fatime = format(int(listcatfiles['ffilelist'][reallcfi]['fatime']), 'x').lower();
4410 fmtime = format(int(listcatfiles['ffilelist'][reallcfi]['fmtime']), 'x').lower();
4411 fctime = format(int(listcatfiles['ffilelist'][reallcfi]['fctime']), 'x').lower();
4412 fbtime = format(int(listcatfiles['ffilelist'][reallcfi]['fbtime']), 'x').lower();
4413 fmode = format(int(listcatfiles['ffilelist'][reallcfi]['fmode']), 'x').lower();
4414 fchmode = format(int(listcatfiles['ffilelist'][reallcfi]['fchmode']), 'x').lower();
4415 fuid = format(int(listcatfiles['ffilelist'][reallcfi]['fuid']), 'x').lower();
4416 funame = listcatfiles['ffilelist'][reallcfi]['funame'];
4417 fgid = format(int(listcatfiles['ffilelist'][reallcfi]['fgid']), 'x').lower();
4418 fgname = listcatfiles['ffilelist'][reallcfi]['fgname'];
4419 finode = listcatfiles['ffilelist'][reallcfi]['finode'];
4420 flinkcount = listcatfiles['ffilelist'][reallcfi]['flinkcount'];
4421 fdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['fminor']), 'x').lower();
4422 fdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['fmajor']), 'x').lower();
4423 frdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['frminor']), 'x').lower();
4424 frdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['frmajor']), 'x').lower();
4425 if(len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>listcatfiles['ffilelist'][reallcfi]['fextrafields'] and len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>0):
4426 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(listcatfiles['ffilelist'][reallcfi]['fextralist']);
4427 if(len(extradata) > 0):
4428 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(extradata);
4429 listcatfiles['ffilelist'][reallcfi]['fextralist'] = extradata;
4430 extrafields = format(int(listcatfiles['ffilelist'][reallcfi]['fextrafields']), 'x').lower();
4431 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
4432 if(len(extradata)>0):
4433 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
4434 extrasizelen = format(len(extrasizestr), 'x').lower();
4435 fcontents = listcatfiles['ffilelist'][reallcfi]['fcontents'];
4436 if(followlink):
4437 if(listcatfiles['ffilelist'][reallcfi]['ftype']==1 or listcatfiles['ffilelist'][reallcfi]['ftype']==2):
4438 getflinkpath = listcatfiles['ffilelist'][reallcfi]['flinkname'];
4439 flinkid = prelistcatfiles['filetoid'][getflinkpath];
4440 flinkinfo = listcatfiles['ffilelist'][flinkid];
4441 fheadersize = format(int(flinkinfo['fheadersize']), 'x').lower();
4442 fsize = format(int(flinkinfo['fsize']), 'x').lower();
4443 flinkname = flinkinfo['flinkname'];
4444 fatime = format(int(flinkinfo['fatime']), 'x').lower();
4445 fmtime = format(int(flinkinfo['fmtime']), 'x').lower();
4446 fctime = format(int(flinkinfo['fctime']), 'x').lower();
4447 fbtime = format(int(flinkinfo['fbtime']), 'x').lower();
4448 fmode = format(int(flinkinfo['fmode']), 'x').lower();
4449 fchmode = format(int(flinkinfo['fchmode']), 'x').lower();
4450 fuid = format(int(flinkinfo['fuid']), 'x').lower();
4451 funame = flinkinfo['funame'];
4452 fgid = format(int(flinkinfo['fgid']), 'x').lower();
4453 fgname = flinkinfo['fgname'];
4454 finode = flinkinfo['finode'];
4455 flinkcount = flinkinfo['flinkcount'];
4456 fdev_minor = format(int(flinkinfo['fminor']), 'x').lower();
4457 fdev_major = format(int(flinkinfo['fmajor']), 'x').lower();
4458 frdev_minor = format(int(flinkinfo['frminor']), 'x').lower();
4459 frdev_major = format(int(flinkinfo['frmajor']), 'x').lower();
4460 if(len(flinkinfo['fextralist'])>flinkinfo['fextrafields'] and len(flinkinfo['fextralist'])>0):
4461 flinkinfo['fextrafields'] = len(flinkinfo['fextralist']);
4462 if(len(extradata) > 0):
4463 flinkinfo['fextrafields'] = len(extradata);
4464 flinkinfo['fextralist'] = extradata;
4465 extrafields = format(int(flinkinfo['fextrafields']), 'x').lower();
4466 extrasizestr = AppendNullByte(extrafields, formatspecs[4]);
4467 if(len(extradata)>0):
4468 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[4]);
4469 extrasizelen = format(len(extrasizestr), 'x').lower();
4470 fcontents = flinkinfo['fcontents'];
4471 if(flinkinfo['ftype']!=0 and flinkinfo['ftype']!=7):
4472 try:
4473 fcontents = fcontents.encode('UTF-8');
4474 except AttributeError:
4475 pass;
4476 ftypehex = format(flinkinfo['ftype'], 'x').lower();
4477 else:
4478 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=0 and listcatfiles['ffilelist'][reallcfi]['ftype']!=7):
4479 try:
4480 fcontents = fcontents.encode('UTF-8');
4481 except AttributeError:
4482 pass;
4483 ftypehex = format(listcatfiles['ffilelist'][reallcfi]['ftype'], 'x').lower();
4484 fcurfid = format(curfid, 'x').lower();
4485 if(not followlink and finode!=0):
4486 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=1):
4487 fcurinode = format(int(curinode), 'x').lower();
4488 inodetofile.update({curinode: fname});
4489 filetoinode.update({fname: curinode});
4490 curinode = curinode + 1;
4491 else:
4492 fcurinode = format(int(filetoinode[flinkname]), 'x').lower();
4493 else:
4494 fcurinode = format(int(curinode), 'x').lower();
4495 curinode = curinode + 1;
4496 curfid = curfid + 1;
4497 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[4]);
4498 if(listcatfiles['ffilelist'][reallcfi]['fextrafields']>0):
4499 extrafieldslist = [];
4500 exi = 0;
4501 exil = listcatfiles['ffilelist'][reallcfi]['fextrafields'];
4502 while(exi < exil):
4503 extrafieldslist.append(listcatfiles['ffilelist'][reallcfi]['fextralist']);
4504 exi = exi + 1;
4505 catfileoutstr += AppendNullBytes([extrafieldslist], formatspecs[4]);
4506 catfileoutstr += AppendNullBytes([checksumtype], formatspecs[4]);
4507 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
4508 catfcontentstart = catfp.tell() + len(catfileoutstr);
4509 if(checksumtype=="none" or checksumtype==""):
4510 catfileheadercshex = format(0, 'x').lower();
4511 catfilecontentcshex = format(0, 'x').lower();
4512 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4513 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4514 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4515 elif(checksumtype=="crc16_ccitt"):
4516 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4517 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4518 elif(checksumtype=="adler32"):
4519 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4520 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4521 elif(checksumtype=="crc32"):
4522 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4523 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4524 elif(checksumtype=="crc64_ecma"):
4525 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4526 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4527 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4528 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4529 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4530 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4531 checksumoutstr = hashlib.new(checksumtype);
4532 checksumoutstr.update("".encode('UTF-8'));
4533 catfileheadercshex = checksumoutstr.hexdigest().lower();
4534 checksumoutstr = hashlib.new(checksumtype);
4535 checksumoutstr.update(fcontents);
4536 catfilecontentcshex = checksumoutstr.hexdigest().lower();
4537 else:
4538 catfileheadercshex = format(0, 'x').lower();
4539 catfilecontentcshex = format(0, 'x').lower();
4540 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
4541 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4542 catfileoutstr = AppendNullByte(catheaersize, formatspecs[4]) + catfileoutstr;
4543 if(checksumtype=="none" or checksumtype==""):
4544 catfileheadercshex = format(0, 'x').lower();
4545 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4546 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4547 elif(checksumtype=="crc16_ccitt"):
4548 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4549 elif(checksumtype=="adler32"):
4550 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4551 elif(checksumtype=="crc32"):
4552 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4553 elif(checksumtype=="crc64_ecma"):
4554 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4555 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4556 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4557 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4558 checksumoutstr = hashlib.new(checksumtype);
4559 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4560 catfileheadercshex = checksumoutstr.hexdigest().lower();
4561 else:
4562 catfileheadercshex = format(0, 'x').lower();
4563 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[4]);
4564 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4565 nullstrecd = formatspecs[4].encode('UTF-8');
4566 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4567 catfcontentend = (catfp.tell() - 1) + len(catfileout);
4568 catfp.write(catfileout);
4569 try:
4570 catfp.flush();
4571 os.fsync(catfp.fileno());
4572 except io.UnsupportedOperation:
4573 pass;
4574 except AttributeError:
4575 pass;
4576 lcfi = lcfi + 1;
4577 reallcfi = reallcfi + 1;
4578 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
4579 catfp = CompressArchiveFile(catfp, compression, formatspecs);
4580 try:
4581 catfp.flush();
4582 os.fsync(catfp.fileno());
4583 except io.UnsupportedOperation:
4584 pass;
4585 except AttributeError:
4586 pass;
4587 if(outfile=="-"):
4588 catfp.seek(0, 0);
4589 if(hasattr(sys.stdout, "buffer")):
4590 shutil.copyfileobj(catfp, sys.stdout.buffer);
4591 else:
4592 shutil.copyfileobj(catfp, sys.stdout);
4593 if(returnfp):
4594 catfp.seek(0, 0);
4595 return catfp;
4596 else:
4597 catfp.close();
4598 return True;
4600 create_alias_function("RePack", __file_format_name__, "", RePackArchiveFile);
4602 def RePackArchiveFileFromString(catstr, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
4603 catfp = BytesIO(catstr);
4604 listcatfiles = RePackArchiveFile(catfp, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
4605 return listcatfiles;
4607 create_alias_function("RePack", __file_format_name__, "FromString", RePackArchiveFileFromString);
4609 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):
4610 outarray = BytesIO();
4611 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
4612 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
4613 return listcatfiles;
4615 create_alias_function("Pack", __file_format_name__, "FromListDir", PackArchiveFileFromListDir);
4617 def ArchiveFileArrayBase64Encode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, usenewstyle=True, returnfp=False):
4618 if(verbose):
4619 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4620 if(isinstance(infile, dict)):
4621 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4622 listcatfiles = prelistcatfiles['list'];
4623 else:
4624 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
4625 infile = RemoveWindowsPath(infile);
4626 if(followlink):
4627 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4628 listcatfiles = prelistcatfiles['list'];
4629 else:
4630 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4631 if(not listcatfiles):
4632 return False;
4633 lenlist = len(listcatfiles['ffilelist']);
4634 if(seekstart>0):
4635 lcfi = seekstart;
4636 else:
4637 lcfi = 0;
4638 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4639 lcfx = seekend;
4640 else:
4641 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4642 lcfx = listcatfiles['fnumfiles'];
4643 else:
4644 lcfx = int(listcatfiles['fnumfiles']);
4645 if(lenlist>lcfx or lenlist<lcfx):
4646 lcfx = lenlist;
4647 while(lcfi < lcfx):
4648 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
4649 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64encode(listcatfiles['ffilelist'][lcfi]['fcontents']).decode("UTF-8");
4650 lcfi = lcfi + 1;
4651 return listcatfiles;
4653 create_alias_function("", __file_format_name__, "ArrayBase64Encode", ArchiveFileArrayBase64Encode);
4655 def ArchiveFileArrayBase64Decode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, usenewstyle=True, returnfp=False):
4656 if(verbose):
4657 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4658 if(isinstance(infile, dict)):
4659 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4660 listcatfiles = prelistcatfiles['list'];
4661 else:
4662 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
4663 infile = RemoveWindowsPath(infile);
4664 if(followlink):
4665 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4666 listcatfiles = prelistcatfiles['list'];
4667 else:
4668 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4669 if(not listcatfiles):
4670 return False;
4671 lenlist = len(listcatfiles['ffilelist']);
4672 if(seekstart>0):
4673 lcfi = seekstart;
4674 else:
4675 lcfi = 0;
4676 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4677 lcfx = seekend;
4678 else:
4679 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4680 lcfx = listcatfiles['fnumfiles'];
4681 else:
4682 lcfx = int(listcatfiles['fnumfiles']);
4683 if(lenlist>lcfx or lenlist<lcfx):
4684 lcfx = lenlist;
4685 while(lcfi < lcfx):
4686 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
4687 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64decode(listcatfiles['ffilelist'][lcfi]['fcontents'].encode("UTF-8"));
4688 lcfi = lcfi + 1;
4689 return listcatfiles;
4691 create_alias_function("", __file_format_name__, "ArrayBase64Decode", ArchiveFileArrayBase64Decode);
4693 def UnPackArchiveFile(infile, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, usenewstyle=True, returnfp=False):
4694 if(outdir is not None):
4695 outdir = RemoveWindowsPath(outdir);
4696 if(verbose):
4697 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4698 if(isinstance(infile, dict)):
4699 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4700 listcatfiles = prelistcatfiles['list'];
4701 else:
4702 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
4703 infile = RemoveWindowsPath(infile);
4704 if(followlink):
4705 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4706 listcatfiles = prelistcatfiles['list'];
4707 else:
4708 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, usenewstyle, returnfp);
4709 if(not listcatfiles):
4710 return False;
4711 lenlist = len(listcatfiles['ffilelist']);
4712 if(seekstart>0):
4713 lcfi = seekstart;
4714 else:
4715 lcfi = 0;
4716 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4717 lcfx = seekend;
4718 else:
4719 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4720 lcfx = listcatfiles['fnumfiles'];
4721 else:
4722 lcfx = int(listcatfiles['fnumfiles']);
4723 if(lenlist>lcfx or lenlist<lcfx):
4724 lcfx = lenlist;
4725 while(lcfi < lcfx):
4726 funame = "";
4727 try:
4728 import pwd;
4729 try:
4730 userinfo = pwd.getpwuid(listcatfiles['ffilelist'][lcfi]['fuid']);
4731 funame = userinfo.pw_name;
4732 except KeyError:
4733 funame = "";
4734 except ImportError:
4735 funame = "";
4736 fgname = "";
4737 try:
4738 import grp;
4739 try:
4740 groupinfo = grp.getgrgid(listcatfiles['ffilelist'][lcfi]['fgid']);
4741 fgname = groupinfo.gr_name;
4742 except KeyError:
4743 fgname = "";
4744 except ImportError:
4745 fgname = "";
4746 if(verbose):
4747 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
4748 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4749 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
4750 fpc.write(listcatfiles['ffilelist'][lcfi]['fcontents'])
4751 try:
4752 fpc.flush()
4753 os.fsync(fpc.fileno())
4754 except io.UnsupportedOperation:
4755 pass
4756 except AttributeError:
4757 pass
4758 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname']):
4759 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
4760 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], int(listcatfiles['ffilelist'][lcfi]['fchmode'], 8));
4761 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
4762 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4763 if(followlink):
4764 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
4765 flinkid = prelistcatfiles['filetoid'][getflinkpath];
4766 flinkinfo = listcatfiles['ffilelist'][flinkid];
4767 funame = "";
4768 try:
4769 import pwd;
4770 try:
4771 userinfo = pwd.getpwuid(flinkinfo['fuid']);
4772 funame = userinfo.pw_name;
4773 except KeyError:
4774 funame = "";
4775 except ImportError:
4776 funame = "";
4777 fgname = "";
4778 try:
4779 import grp;
4780 try:
4781 groupinfo = grp.getgrgid(flinkinfo['fgid']);
4782 fgname = groupinfo.gr_name;
4783 except KeyError:
4784 fgname = "";
4785 except ImportError:
4786 fgname = "";
4787 if(flinkinfo['ftype'] == 0 or flinkinfo['ftype'] == 7):
4788 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
4789 fpc.write(flinkinfo['fcontents'])
4790 try:
4791 fpc.flush()
4792 os.fsync(fpc.fileno())
4793 except io.UnsupportedOperation:
4794 pass
4795 except AttributeError:
4796 pass
4797 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname']):
4798 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
4799 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4800 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
4801 if(flinkinfo['ftype']==1):
4802 os.link(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
4803 if(flinkinfo['ftype']==2):
4804 os.symlink(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
4805 if(flinkinfo['ftype']==5):
4806 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4807 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname']):
4808 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
4809 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4810 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
4811 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
4812 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4813 else:
4814 os.link(listcatfiles['ffilelist'][lcfi]['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
4815 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4816 if(followlink):
4817 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
4818 flinkid = prelistcatfiles['filetoid'][getflinkpath];
4819 flinkinfo = listcatfiles['ffilelist'][flinkid];
4820 funame = "";
4821 try:
4822 import pwd;
4823 try:
4824 userinfo = pwd.getpwuid(flinkinfo['fuid']);
4825 funame = userinfo.pw_name;
4826 except KeyError:
4827 funame = "";
4828 except ImportError:
4829 funame = "";
4830 fgname = "";
4831 try:
4832 import grp;
4833 try:
4834 groupinfo = grp.getgrgid(flinkinfo['fgid']);
4835 fgname = groupinfo.gr_name;
4836 except KeyError:
4837 fgname = "";
4838 except ImportError:
4839 fgname = "";
4840 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
4841 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
4842 fpc.write(flinkinfo['fcontents'])
4843 try:
4844 fpc.flush()
4845 os.fsync(fpc.fileno())
4846 except io.UnsupportedOperation:
4847 pass
4848 except AttributeError:
4849 pass
4850 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname']):
4851 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
4852 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4853 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
4854 if(flinkinfo['ftype']==1):
4855 os.link(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
4856 if(flinkinfo['ftype']==2):
4857 os.symlink(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
4858 if(flinkinfo['ftype']==5):
4859 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4860 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname']):
4861 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
4862 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4863 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
4864 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
4865 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], int(flinkinfo['fchmode'], 8));
4866 else:
4867 os.symlink(listcatfiles['ffilelist'][lcfi]['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
4868 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4869 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], int(listcatfiles['ffilelist'][lcfi]['fchmode'], 8));
4870 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname']):
4871 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
4872 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], int(listcatfiles['ffilelist'][lcfi]['fchmode'], 8));
4873 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
4874 if(listcatfiles['ffilelist'][lcfi]['ftype']==6 and hasattr(os, "mkfifo")):
4875 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], int(listcatfiles['ffilelist'][lcfi]['fchmode'], 8));
4876 lcfi = lcfi + 1;
4877 if(returnfp):
4878 return listcatfiles['ffilelist']['catfp'];
4879 else:
4880 return True;
4882 create_alias_function("UnPack", __file_format_name__, "", UnPackArchiveFile);
4884 if(hasattr(shutil, "register_unpack_format")):
4885 def UnPackArchiveFileFunc(archive_name, extract_dir=None, **kwargs):
4886 return UnPackArchiveFile(archive_name, extract_dir, False, 0, 0, False, __file_format_delimiter__, False, False);
4887 create_alias_function("UnPack", __file_format_name__, "Func", UnPackArchiveFileFunc);
4889 def UnPackArchiveFileString(catstr, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
4890 catfp = BytesIO(catstr);
4891 listcatfiles = UnPackArchiveFile(catfp, outdir, followlink, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
4892 return listcatfiles;
4894 create_alias_function("UnPack", __file_format_name__, "String", UnPackArchiveFileString);
4896 def ArchiveFileListFiles(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, usenewstyle=True, returnfp=False):
4897 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4898 if(isinstance(infile, dict)):
4899 listcatfiles = infile;
4900 else:
4901 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
4902 infile = RemoveWindowsPath(infile);
4903 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, usenewstyle, returnfp);
4904 if(not listcatfiles):
4905 return False;
4906 lenlist = len(listcatfiles['ffilelist']);
4907 lcfi = 0;
4908 lcfx = lenlist;
4909 returnval = {};
4910 while(lcfi < lcfx):
4911 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
4912 if(not verbose):
4913 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
4914 if(verbose):
4915 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' } };
4916 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
4917 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4918 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
4919 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4920 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
4921 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
4922 if(len(fuprint)<=0):
4923 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
4924 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
4925 if(len(fgprint)<=0):
4926 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
4927 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));
4928 lcfi = lcfi + 1;
4929 if(returnfp):
4930 return listcatfiles['catfp'];
4931 else:
4932 return True;
4934 create_alias_function("", __file_format_name__, "ListFiles", ArchiveFileListFiles);
4936 def ArchiveFileStringListFiles(catstr, followlink=False, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
4937 catfp = BytesIO(catstr);
4938 listcatfiles = UnPackArchiveFile(catfp, None, followlink, skipchecksum, formatspecs, verbose, returnfp);
4939 return listcatfiles;
4941 create_alias_function("", __file_format_name__, "StringListFiles", ArchiveFileListFiles);
4943 def TarFileListFiles(infile, verbose=False, returnfp=False):
4944 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
4945 if(not os.path.exists(infile) or not os.path.isfile(infile)):
4946 return False;
4947 try:
4948 if(not tarfile.is_tarfile(infile)):
4949 return False;
4950 except AttributeError:
4951 if(not is_tarfile(infile)):
4952 return False;
4953 lcfi = 0;
4954 returnval = {};
4955 try:
4956 tarfp = tarfile.open(infiles, "r");
4957 except FileNotFoundError:
4958 return False;
4959 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
4960 returnval.update({lcfi: member.name});
4961 fpremode = member.mode;
4962 ffullmode = member.mode;
4963 flinkcount = 0;
4964 ftype = 0;
4965 if(member.isreg()):
4966 ffullmode = member.mode + stat.S_IFREG;
4967 ftype = 0;
4968 elif(member.isdev()):
4969 ffullmode = member.mode;
4970 ftype = 7;
4971 elif(member.islnk()):
4972 ffullmode = member.mode + stat.S_IFREG;
4973 ftype = 1;
4974 elif(member.issym()):
4975 ffullmode = member.mode + stat.S_IFLNK;
4976 ftype = 2;
4977 elif(member.ischr()):
4978 ffullmode = member.mode + stat.S_IFCHR;
4979 ftype = 3;
4980 elif(member.isblk()):
4981 ffullmode = member.mode + stat.S_IFBLK;
4982 ftype = 4;
4983 elif(member.isdir()):
4984 ffullmode = member.mode + stat.S_IFDIR;
4985 ftype = 5;
4986 elif(member.isfifo()):
4987 ffullmode = member.mode + stat.S_IFIFO;
4988 ftype = 6;
4989 elif(member.issparse()):
4990 ffullmode = member.mode;
4991 ftype = 8;
4992 if(not verbose):
4993 VerbosePrintOut(member.name);
4994 elif(verbose):
4995 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' } };
4996 printfname = member.name;
4997 if(member.islnk()):
4998 printfname = member.name + " link to " + member.linkname;
4999 elif(member.issym()):
5000 printfname = member.name + " -> " + member.linkname;
5001 fuprint = member.uname;
5002 if(len(fuprint)<=0):
5003 fuprint = member.uid;
5004 fgprint = member.gname;
5005 if(len(fgprint)<=0):
5006 fgprint = member.gid;
5007 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));
5008 lcfi = lcfi + 1;
5009 if(returnfp):
5010 return listcatfiles['catfp'];
5011 else:
5012 return True;
5014 def ZipFileListFiles(infile, verbose=False, returnfp=False):
5015 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5016 if(not os.path.exists(infile) or not os.path.isfile(infile)):
5017 return False;
5018 if(not zipfile.is_zipfile(infile)):
5019 return False;
5020 lcfi = 0;
5021 returnval = {};
5022 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
5023 ziptest = zipfp.testzip();
5024 if(ziptest):
5025 VerbosePrintOut("Bad file found!");
5026 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
5027 if(not member.is_dir()):
5028 fpremode = int(stat.S_IFREG + 438);
5029 elif(member.is_dir()):
5030 fpremode = int(stat.S_IFDIR + 511);
5031 if(not member.is_dir()):
5032 fmode = int(stat.S_IFREG + 438);
5033 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5034 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5035 elif(member.is_dir()):
5036 fmode = int(stat.S_IFDIR + 511);
5037 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5038 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5039 returnval.update({lcfi: member.filename});
5040 if(not verbose):
5041 VerbosePrintOut(member.filename);
5042 if(verbose):
5043 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' } };
5044 permissionstr = "";
5045 for fmodval in str(oct(fmode))[-3:]:
5046 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
5047 if(not member.is_dir()):
5048 ftype = 0;
5049 permissionstr = "-" + permissionstr;
5050 elif(member.is_dir()):
5051 ftype = 5;
5052 permissionstr = "d" + permissionstr;
5053 printfname = member.filename;
5054 try:
5055 fuid = int(os.getuid());
5056 except AttributeError:
5057 fuid = int(0);
5058 except KeyError:
5059 fuid = int(0);
5060 try:
5061 fgid = int(os.getgid());
5062 except AttributeError:
5063 fgid = int(0);
5064 except KeyError:
5065 fgid = int(0);
5066 try:
5067 import pwd;
5068 try:
5069 userinfo = pwd.getpwuid(os.getuid());
5070 funame = userinfo.pw_name;
5071 except KeyError:
5072 funame = "";
5073 except AttributeError:
5074 funame = "";
5075 except ImportError:
5076 funame = "";
5077 fgname = "";
5078 try:
5079 import grp;
5080 try:
5081 groupinfo = grp.getgrgid(os.getgid());
5082 fgname = groupinfo.gr_name;
5083 except KeyError:
5084 fgname = "";
5085 except AttributeError:
5086 fgname = "";
5087 except ImportError:
5088 fgname = "";
5089 fuprint = funame;
5090 if(len(fuprint)<=0):
5091 fuprint = str(fuid);
5092 fgprint = fgname;
5093 if(len(fgprint)<=0):
5094 fgprint = str(fgid);
5095 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));
5096 lcfi = lcfi + 1;
5097 if(returnfp):
5098 return listcatfiles['catfp'];
5099 else:
5100 return True;
5102 if(not rarfile_support):
5103 def RarFileListFiles(infile, verbose=False, returnfp=False):
5104 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5105 if(not os.path.exists(infile) or not os.path.isfile(infile)):
5106 return False;
5108 if(rarfile_support):
5109 def RarFileListFiles(infile, verbose=False, returnfp=False):
5110 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5111 if(not os.path.exists(infile) or not os.path.isfile(infile)):
5112 return False;
5113 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
5114 return False;
5115 lcfi = 0;
5116 returnval = {};
5117 rarfp = rarfile.RarFile(infile, "r");
5118 rartest = rarfp.testrar();
5119 if(rartest):
5120 VerbosePrintOut("Bad file found!");
5121 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
5122 is_unix = False;
5123 is_windows = False;
5124 if(member.host_os == rarfile.RAR_OS_UNIX):
5125 is_windows = False;
5126 try:
5127 member.external_attr
5128 is_unix = True;
5129 except AttributeError:
5130 is_unix = False;
5131 elif(member.host_os == rarfile.RAR_OS_WIN32):
5132 is_unix = False;
5133 try:
5134 member.external_attr
5135 is_windows = True;
5136 except AttributeError:
5137 is_windows = False;
5138 else:
5139 is_unix = False;
5140 is_windows = False;
5141 if(is_unix and member.external_attr !=0):
5142 fpremode = int(member.external_attr);
5143 elif(member.is_file()):
5144 fpremode = int(stat.S_IFREG + 438);
5145 elif(member.is_symlink()):
5146 fpremode = int(stat.S_IFLNK + 438);
5147 elif(member.is_dir()):
5148 fpremode = int(stat.S_IFDIR + 511);
5149 if(is_windows and member.external_attr !=0):
5150 fwinattributes = int(member.external_attr);
5151 else:
5152 fwinattributes = int(0);
5153 if(is_unix and member.external_attr !=0):
5154 fmode = int(member.external_attr);
5155 fchmode = int(stat.S_IMODE(member.external_attr));
5156 ftypemod = int(stat.S_IFMT(member.external_attr));
5157 elif(member.is_file()):
5158 fmode = int(stat.S_IFREG + 438);
5159 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5160 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5161 elif(member.is_symlink()):
5162 fmode = int(stat.S_IFLNK + 438);
5163 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
5164 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
5165 elif(member.is_dir()):
5166 fmode = int(stat.S_IFDIR + 511);
5167 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5168 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5169 returnval.update({lcfi: member.filename});
5170 if(not verbose):
5171 VerbosePrintOut(member.filename);
5172 if(verbose):
5173 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' } };
5174 permissionstr = "";
5175 for fmodval in str(oct(fmode))[-3:]:
5176 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
5177 if(member.is_file()):
5178 ftype = 0;
5179 permissionstr = "-" + permissionstr;
5180 printfname = member.filename;
5181 elif(member.is_symlink()):
5182 ftype = 2;
5183 permissionstr = "l" + permissionstr;
5184 printfname = member.name + " -> " + member.read().decode("UTF-8");
5185 elif(member.is_dir()):
5186 ftype = 5;
5187 permissionstr = "d" + permissionstr;
5188 printfname = member.filename;
5189 try:
5190 fuid = int(os.getuid());
5191 except AttributeError:
5192 fuid = int(0);
5193 except KeyError:
5194 fuid = int(0);
5195 try:
5196 fgid = int(os.getgid());
5197 except AttributeError:
5198 fgid = int(0);
5199 except KeyError:
5200 fgid = int(0);
5201 try:
5202 import pwd;
5203 try:
5204 userinfo = pwd.getpwuid(os.getuid());
5205 funame = userinfo.pw_name;
5206 except KeyError:
5207 funame = "";
5208 except AttributeError:
5209 funame = "";
5210 except ImportError:
5211 funame = "";
5212 fgname = "";
5213 try:
5214 import grp;
5215 try:
5216 groupinfo = grp.getgrgid(os.getgid());
5217 fgname = groupinfo.gr_name;
5218 except KeyError:
5219 fgname = "";
5220 except AttributeError:
5221 fgname = "";
5222 except ImportError:
5223 fgname = "";
5224 fuprint = funame;
5225 if(len(fuprint)<=0):
5226 fuprint = str(fuid);
5227 fgprint = fgname;
5228 if(len(fgprint)<=0):
5229 fgprint = str(fgid);
5230 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + member.mtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
5231 lcfi = lcfi + 1;
5232 if(returnfp):
5233 return listcatfiles['catfp'];
5234 else:
5235 return True;
5237 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):
5238 outarray = BytesIO();
5239 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, False, True);
5240 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
5241 return listcatfiles;
5243 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):
5244 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, formatspecs, verbose);
5245 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
5246 return listcatfiles;
5248 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):
5249 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, False, checksumtype, extradata, formatspecs, False);
5250 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, followlink, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
5251 return listcatfiles;
5253 create_alias_function("Pack", __file_format_name__, "FromListDirAlt", PackArchiveFileFromListDirAlt);
5255 def PackArchiveFileFromTarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5256 outarray = TarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
5257 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
5258 return listcatfiles;
5260 create_alias_function("Pack", __file_format_name__, "FromTarFileAlt", PackArchiveFileFromTarFileAlt);
5262 def PackArchiveFileFromZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5263 outarray = ZipFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
5264 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
5265 return listcatfiles;
5267 create_alias_function("Pack", __file_format_name__, "FromZipFileAlt", PackArchiveFileFromZipFileAlt);
5269 if(not rarfile_support):
5270 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5271 return False;
5273 if(rarfile_support):
5274 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5275 outarray = RarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
5276 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
5277 return listcatfiles;
5279 create_alias_function("Pack", __file_format_name__, "FromRarFileAlt", PackArchiveFileFromRarFileAlt);
5281 def download_file_from_ftp_file(url):
5282 urlparts = urlparse.urlparse(url);
5283 file_name = os.path.basename(urlparts.path);
5284 file_dir = os.path.dirname(urlparts.path);
5285 if(urlparts.username is not None):
5286 ftp_username = urlparts.username;
5287 else:
5288 ftp_username = "anonymous";
5289 if(urlparts.password is not None):
5290 ftp_password = urlparts.password;
5291 elif(urlparts.password is None and urlparts.username=="anonymous"):
5292 ftp_password = "anonymous";
5293 else:
5294 ftp_password = "";
5295 if(urlparts.scheme=="ftp"):
5296 ftp = FTP();
5297 elif(urlparts.scheme=="ftps"):
5298 ftp = FTP_TLS();
5299 else:
5300 return False;
5301 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
5302 return False;
5303 ftp_port = urlparts.port;
5304 if(urlparts.port is None):
5305 ftp_port = 21;
5306 try:
5307 ftp.connect(urlparts.hostname, ftp_port);
5308 except socket.gaierror:
5309 log.info("Error With URL "+httpurl);
5310 return False;
5311 except socket.timeout:
5312 log.info("Error With URL "+httpurl);
5313 return False;
5314 ftp.login(urlparts.username, urlparts.password);
5315 if(urlparts.scheme=="ftps"):
5316 ftp.prot_p();
5317 ftpfile = BytesIO();
5318 ftp.retrbinary("RETR "+urlparts.path, ftpfile.write);
5319 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
5320 ftp.close();
5321 ftpfile.seek(0, 0);
5322 return ftpfile;
5324 def download_file_from_ftp_string(url):
5325 ftpfile = download_file_from_ftp_file(url);
5326 return ftpfile.read();
5328 def upload_file_to_ftp_file(ftpfile, url):
5329 urlparts = urlparse.urlparse(url);
5330 file_name = os.path.basename(urlparts.path);
5331 file_dir = os.path.dirname(urlparts.path);
5332 if(urlparts.username is not None):
5333 ftp_username = urlparts.username;
5334 else:
5335 ftp_username = "anonymous";
5336 if(urlparts.password is not None):
5337 ftp_password = urlparts.password;
5338 elif(urlparts.password is None and urlparts.username=="anonymous"):
5339 ftp_password = "anonymous";
5340 else:
5341 ftp_password = "";
5342 if(urlparts.scheme=="ftp"):
5343 ftp = FTP();
5344 elif(urlparts.scheme=="ftps"):
5345 ftp = FTP_TLS();
5346 else:
5347 return False;
5348 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
5349 return False;
5350 ftp_port = urlparts.port;
5351 if(urlparts.port is None):
5352 ftp_port = 21;
5353 try:
5354 ftp.connect(urlparts.hostname, ftp_port);
5355 except socket.gaierror:
5356 log.info("Error With URL "+httpurl);
5357 return False;
5358 except socket.timeout:
5359 log.info("Error With URL "+httpurl);
5360 return False;
5361 ftp.login(urlparts.username, urlparts.password);
5362 if(urlparts.scheme=="ftps"):
5363 ftp.prot_p();
5364 ftp.storbinary("STOR "+urlparts.path, ftpfile);
5365 ftp.close();
5366 ftpfile.seek(0, 0);
5367 return ftpfile;
5369 def upload_file_to_ftp_string(ftpstring, url):
5370 ftpfileo = BytesIO(ftpstring);
5371 ftpfile = upload_file_to_ftp_file(ftpfileo, url);
5372 ftpfileo.close();
5373 return ftpfile;
5375 try:
5376 if(hasattr(shutil, "register_archive_format")):
5377 # Register the packing format
5378 shutil.register_archive_format(__file_format_name__, PackArchiveFileFunc, description='Pack concatenated files');
5379 except shutil.RegistryError:
5380 pass;
5382 try:
5383 if(hasattr(shutil, "register_unpack_format")):
5384 # Register the unpacking format
5385 shutil.register_unpack_format(__file_format_name__, archivefile_extensions, UnPackArchiveFileFunc, description='UnPack concatenated files');
5386 except shutil.RegistryError:
5387 pass;