3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 Revised BSD License for more details.
11 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
12 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
13 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
15 $FileInfo: phpcatfile.php - Last Update: 5/2/2024 Ver. 0.9.8 RC 1 - Author: cooldude2k $
18 date_default_timezone_set('UTC');
20 $info['program_name'] = "PHPCatFile";
21 $info['project'] = $info['program_name'];
22 $info['project_url'] = "https://github.com/GameMaker2k/PyCatFile";
23 $info['version_info'] = [0, 9, 8, "RC 1", 1];
24 $info['version_id'] = "$Id$";
25 $info['version_date_info'] = [2024, 5, 2, "RC 1", 1];
26 $info['version_date'] = $info['version_date_info'][0].".".str_pad($info['version_date_info'][1], 2, "-=", STR_PAD_LEFT
).".".str_pad($info['version_date_info'][2], 2, "-=", STR_PAD_LEFT
);
27 if($info['version_info'][4]!==Null) {
28 $info['version_date_plusrc'] = $info['version_date']."-".$info['version_date_info'][4]; }
29 if($info['version_info'][4]===Null) {
30 $info['version_date_plusrc'] = $info['version_date']; }
31 if($info['version_info'][3]!==Null) {
32 $info['version'] = $info['version_info'][0].".".$info['version_info'][1].".".$info['version_info'][2]." ".$info['version_info'][3]; }
33 if($info['version_info'][3]===Null) {
34 $info['version'] = $info['version_info'][0].".".$info['version_info'][1].".".$info['version_info'][2]; }
36 if ( !function_exists( 'hex2bin' ) ) {
37 function hex2bin( $str ) {
39 $len = strlen( $str );
40 for ( $i = 0; $i < $len; $i +
= 2 ) {
41 $sbin .= pack( "H*", substr( $str, $i, 2 ) );
48 function RemoveWindowsPath($dpath) {
49 if ($dpath === null) {
52 $dpath = str_replace(DIRECTORY_SEPARATOR
, "/", $dpath);
53 $dpath = rtrim($dpath, "/");
54 if ($dpath == "." ||
$dpath == "..") {
60 function NormalizeRelativePath($inpath) {
61 $inpath = RemoveWindowsPath($inpath);
62 if (strpos($inpath, '/') !== 0) { // Checks if not an absolute path
63 if (!str_starts_with($inpath, "./") && !str_starts_with($inpath, "../")) {
64 $inpath = "./" . $inpath;
70 function ListDir($dirpath, $followlink = false, $duplicates = false) {
71 if (is_array($dirpath) ||
is_object($dirpath)) {
72 $dirpath = array_filter((array)$dirpath);
74 $dirpath = array_filter([$dirpath]);
77 foreach ($dirpath as $mydirfile) {
78 if (!file_exists($mydirfile)) {
81 $mydirfile = NormalizeRelativePath($mydirfile);
82 if (file_exists($mydirfile) && is_link($mydirfile) && $followlink) {
83 $mydirfile = RemoveWindowsPath(realpath($mydirfile));
85 if (file_exists($mydirfile) && is_dir($mydirfile)) {
86 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($mydirfile));
87 foreach ($files as $file) {
88 if ($file->isDir()) continue;
89 $fpath = RemoveWindowsPath($file->getPathname());
90 if (!$duplicates && !in_array($fpath, $retlist)) {
92 } elseif ($duplicates) {
97 $retlist[] = RemoveWindowsPath($mydirfile);
103 function ReadTillNullByte($fp) {
105 while(($curByte = fgetc($fp)) !== "\0" && $curByte !== false) {
106 $curFullByte .= $curByte;
111 function ReadUntilNullByte($fp) {
112 return readTillNullByte($fp);
115 function SeekToEndOfFile($fp) {
116 fseek($fp, 0, SEEK_END
);
120 function ReadFileHeaderData($fp, $rounds = 0) {
122 for ($roCount = 0; $roCount < $rounds; $roCount++
) {
123 $headerOut[$roCount] = ReadTillNullByte($fp);
128 function AppendNullByte($indata) {
129 return $indata . "\0";
132 function AppendNullBytes($indata = []) {
134 foreach ($indata as $item) {
135 $outData .= AppendNullByte($item);
140 function ReadTillNullByteAlt($fp) {
142 while (($curByte = fgetc($fp)) !== "\0" && $curByte !== false) {
143 $bytesList .= $curByte;
148 function readUntilNullByteAlt($fp) {
149 return readTillNullByteAlt($fp);
152 function ReadFileHeaderDataAlt($fp, $rounds = 0) {
154 for ($roundCount = 0; $roundCount < $rounds; $roundCount++
) {
155 $headerOut[$roundCount] = ReadTillNullByteAlt($fp);
160 function AppendNullByteAlt($indata) {
161 return $indata . "\0";
164 function AppendNullBytesAlt($indata = []) {
165 return implode("\0", array_map('strval', $indata)) . "\0";
168 function CheckFileType($infile) {
169 $catfp = fopen($infile, "rb");
171 $prefp = fread($catfp, 2);
173 if($prefp==hex2bin("1f8b")) {
174 $filetype = "gzip"; }
176 $prefp = fread($catfp, 3);
177 if($prefp==hex2bin("425a68")) {
178 $filetype = "bzip2"; }
180 $prefp = fread($catfp, 7);
181 /*if($prefp==hex2bin("fd377a585a0000")) {
182 $filetype = "lzma"; }*/
183 if($prefp==hex2bin("43617446696c65")) {
184 $filetype = "catfile"; }
188 function CompressCatFile($infile) {
189 if(pathinfo($infile, PATHINFO_EXTENSION
)=="gz" or pathinfo($infile, PATHINFO_EXTENSION
)=="cgz") {
190 if(!function_exists("gzcompress")) {
192 if(file_exists(pathinfo($infile, PATHINFO_FILENAME
).".tmp")) {
193 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp"); }
194 rename($infile, pathinfo($infile, PATHINFO_FILENAME
).".tmp");
195 $catuncomp = fopen(pathinfo($infile, PATHINFO_FILENAME
).".tmp", "rb");
196 $catcomp = fopen($infile, "wb");
197 fseek($catuncomp, 0, SEEK_END
);
198 $endoffile = ftell($catuncomp);
199 fseek($catuncomp, 0, SEEK_SET
);
200 fwrite($catcomp, gzcompress(fread($catuncomp, $endoffile), 9));
203 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp"); }
204 if(pathinfo($infile, PATHINFO_EXTENSION
)=="bz2" or pathinfo($infile, PATHINFO_EXTENSION
)=="cbz") {
205 if(!function_exists("gzcompress")) {
207 if(file_exists(pathinfo($infile, PATHINFO_FILENAME
).".tmp")) {
208 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp"); }
209 rename($infile, pathinfo($infile, PATHINFO_FILENAME
).".tmp");
210 $catuncomp = fopen(pathinfo($infile, PATHINFO_FILENAME
).".tmp", "rb");
211 $catcomp = fopen($infile, "wb");
212 fseek($catuncomp, 0, SEEK_END
);
213 $endoffile = ftell($catuncomp);
214 fseek($catuncomp, 0, SEEK_SET
);
215 fwrite($catcomp, bzcompress(fread($catuncomp, $endoffile), 9));
218 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp"); }
221 function PackCatFile($infiles, $outfile, $followlink=False, $checksumtype="crc32", $verbose=False) {
223 $catver = $info['version_info'][0].".".$info['version_info'][1].".".$info['version_info'][2];
224 $infiles = RemoveWindowsPath($infiles);
225 $outfile = RemoveWindowsPath($outfile);
226 $checksumtype = strtolower($checksumtype);
227 if($checksumtype!="adler32" && $checksumtype!="crc32" && $checksumtype!="md5" && $checksumtype!="sha1" && $checksumtype!="sha224" && $checksumtype!="sha256" && $checksumtype!="sha384" && $checksumtype!="sha512") {
228 $checksumtype="crc32"; }
229 if(file_exists($outfile)) {
231 $catfp = fopen($outfile, "wb");
232 $fileheaderver = intval(str_replace(".", "", $catver));
233 $fileheader = AppendNullByte("CatFile".$fileheaderver);
234 fwrite($catfp, $fileheader);
235 $GetDirList = ListDir($infiles);
236 foreach($GetDirList as $curfname) {
238 if($verbose===True) {
239 print($fname."\n"); }
240 if($followlink===False ||
$followlink===Null) {
241 $fstatinfo = lstat($fname); }
243 $fstatinfo = stat($fname); }
245 if(is_file($fname)) {
247 if(is_link($fname)) {
251 if($ftype==1 ||
$ftype==2 ||
$ftype==5) {
252 $fsize = strtolower(dechex(intval("0"))); }
254 $fsize = strtolower(dechex(intval($fstatinfo['size']))); }
256 if($ftype==1 ||
$ftype==2) {
257 $flinkname = readlink($fname); }
258 $fatime = strtolower(dechex(intval($fstatinfo['atime'])));
259 $fmtime = strtolower(dechex(intval($fstatinfo['mtime'])));
260 $fmode = strtolower(dechex(intval($fstatinfo['mode'])));
261 $fuid = strtolower(dechex(intval($fstatinfo['uid'])));
262 $fgid = strtolower(dechex(intval($fstatinfo['gid'])));
263 $fdev_minor = strtolower(dechex(intval(0)));
264 $fdev_major = strtolower(dechex(intval(0)));
265 $frdev_minor = strtolower(dechex(intval(0)));
266 $frdev_major = strtolower(dechex(intval(0)));
269 $fpc = fopen($fname, "rb");
270 $fcontents = fread($fpc, intval($fstatinfo['size']));
272 if($followlink===False && ($ftype==1 && $ftype==2)) {
273 $flstatinfo = stat($flinkname);
274 $fpc = fopen($flinkname, "rb");
275 $fcontents = fread($fpc, intval($flstatinfo['size']));
277 $ftypehex = strtolower(dechex($ftype));
278 $ftypeoutstr = $ftypehex;
279 $catfileoutstr = AppendNullByte($ftypeoutstr);
280 $catfileoutstr = $catfileoutstr.AppendNullByte($fname);
281 $catfileoutstr = $catfileoutstr.AppendNullByte($flinkname);
282 $catfileoutstr = $catfileoutstr.AppendNullByte($fsize);
283 $catfileoutstr = $catfileoutstr.AppendNullByte($fatime);
284 $catfileoutstr = $catfileoutstr.AppendNullByte($fmtime);
285 $catfileoutstr = $catfileoutstr.AppendNullByte($fmode);
286 $catfileoutstr = $catfileoutstr.AppendNullByte($fuid);
287 $catfileoutstr = $catfileoutstr.AppendNullByte($fgid);
288 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_minor);
289 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_major);
290 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_minor);
291 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_major);
292 $catfileoutstr = $catfileoutstr.AppendNullByte($checksumtype);
293 if($checksumtype=="adler32" ||
$checksumtype=="crc32") {
294 $catfileheadercshex = strtolower(dechex(hash($checksumtype, $catfileoutstr)));
295 $catfilecontentcshex = strtolower(dechex(hash($checksumtype, $catfileoutstr))); }
296 if($checksumtype=="md5" ||
$checksumtype=="sha1" ||
$checksumtype=="sha224" ||
$checksumtype=="sha256" ||
$checksumtype=="sha384" ||
$checksumtype=="sha512") {
297 $catfileheadercshex = strtolower(hash($checksumtype, $catfileoutstr));
298 $catfilecontentcshex = strtolower(hash($checksumtype, $catfileoutstr)); }
299 $catfileoutstr = $catfileoutstr.AppendNullByte($catfileheadercshex);
300 $catfileoutstr = $catfileoutstr.AppendNullByte($catfilecontentcshex);
301 $catfileoutstrecd = $catfileoutstr;
303 $catfileout = $catfileoutstrecd.$fcontents.$Nullstrecd;
304 fwrite($catfp, $catfileout); }
308 function CatFileToArray($infile, $seekstart=0, $seekend=0, $listonly=False, $skipchecksum=False) {
309 $infile = RemoveWindowsPath($infile);
310 $compresscheck = CheckFileType($infile);
311 if($compresscheck===False) {
313 if($compresscheck==="gzip") {
314 $catfp = gzopen($infile, "rb"); }
315 if($compresscheck==="bzip2") {
316 $catfp = bzopen($infile, "rb"); }
317 if($compresscheck==="catfile") {
318 $catfp = fopen($infile, "rb"); }
319 fseek($catfp, 0, SEEK_END
);
320 $CatSize = ftell($catfp);
321 $CatSizeEnd = $CatSize;
322 fseek($catfp, 0, SEEK_SET
);
323 $catstring = ReadFileHeaderData($catfp, 1)[0];
324 preg_match("/([\d]+)$/", $catstring, $catm);
325 $catversion = $catm[0];
329 fseek($catfp, $seekstart, SEEK_SET
); }
331 $seekstart = ftell($catfp); }
333 $seekend = $CatSizeEnd; }
334 while($seekstart<$seekend) {
335 $catfhstart = ftell($catfp);
336 $catheaderdata = ReadFileHeaderData($catfp, 16);
337 $catftype = hexdec($catheaderdata[0]);
338 $catfname = $catheaderdata[1];
339 $catflinkname = $catheaderdata[2];
340 $catfsize = hexdec($catheaderdata[3]);
341 $catfatime = hexdec($catheaderdata[4]);
342 $catfmtime = hexdec($catheaderdata[5]);
343 $catfmode = decoct(hexdec($catheaderdata[6]));
344 $catfchmod = substr($catfmode, -3);
345 $catfuid = hexdec($catheaderdata[7]);
346 $catfgid = hexdec($catheaderdata[8]);
347 $catfdev_minor = hexdec($catheaderdata[9]);
348 $catfdev_major = hexdec($catheaderdata[10]);
349 $catfrdev_minor = hexdec($catheaderdata[11]);
350 $catfrdev_major = hexdec($catheaderdata[12]);
351 $catfchecksumtype = strtolower($catheaderdata[13]);
352 if($catfchecksumtype=="adler32" ||
$catfchecksumtype=="crc32") {
353 $catfcs = hexdec($catheaderdata[14]);
354 $catfccs = hexdec($catheaderdata[15]); }
355 if($catfchecksumtype=="md5" ||
$catfchecksumtype=="sha1" ||
$catfchecksumtype=="sha224" ||
$catfchecksumtype=="sha256" ||
$catfchecksumtype=="sha384" ||
$catfchecksumtype=="sha512") {
356 $catfcs = $catheaderdata[14];
357 $catfccs = $catheaderdata[15]; }
359 $hcmax = count($catheaderdata) - 2;
362 $hout = $hout.AppendNullByte($catheaderdata[$hc]);
364 $catnewfcs = strtolower(hash($catfchecksumtype, $hout));
365 if($catfcs!=$catnewfcs && $skipchecksum===False) {
366 print("File Header Checksum Error with file "+
$catfname+
" at offset "+
$catfhstart);
368 $catfhend = ftell($catfp) - 1;
369 $catfcontentstart = ftell($catfp);
371 $phphascontents = False;
372 if($catfsize>1 && $listonly===False) {
373 $catfcontents = fread($catfp, $catfsize);
374 $catnewfccs = strtolower(hash($catfchecksumtype, $catfcontents));
375 if($catfccs!=$catnewfccs && $skipchecksum===False) {
376 print("File Content Checksum Error with file "+
$catfname+
" at offset "+
$catfcontentstart);
378 $phphascontents = True; }
379 if($catfsize>1 && $listonly===True) {
380 fseek($catfp, $catfsize, SEEK_CUR
);
381 $phphascontents = False; }
382 $catfcontentend = ftell($catfp);
383 $catlist[$fileidnum] = array('catfileversion' => $catversion, 'fid' => $fileidnum, 'fhstart' => $catfhstart, 'fhend' => $catfhend, 'ftype' => $catftype, 'fname' => $catfname, 'flinkname' => $catflinkname, 'fsize' => $catfsize, 'fatime' => $catfatime, 'fmtime' => $catfmtime, 'fmode' => $catfmode, 'fchmod' => $catfchmod, 'fuid' => $catfuid, 'fgid' => $catfgid, 'fminor' => $catfdev_minor, 'fmajor' => $catfdev_major, 'fchecksumtype' => $catfchecksumtype, 'fheaderchecksum' => $catfcs, 'fcontentchecksum' => $catfccs, 'fhascontents' => $phphascontents, 'fcontentstart' => $catfcontentstart, 'fcontentend' => $catfcontentend, 'fcontents' => $catfcontents);
384 fseek($catfp, 1, SEEK_CUR
);
385 $seekstart = ftell($catfp);
386 $fileidnum = $fileidnum +
1; }
390 function CatFileToArrayIndex($infile, $seekstart=0, $seekend=0, $listonly=False, $skipchecksum=False) {
391 if(is_array($infile)) {
392 $listcatfiles = $infile; }
394 $infile = RemoveWindowsPath($infile);
395 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, $listonly, $skipchecksum); }
396 if($listcatfiles===False) {
398 $catarray = array('list' => $listcatfiles, 'filetoid' => array(), 'idtofile' => array(), 'filetypes' => array('directories' => array('filetoid' => array(), 'idtofile' => array()), 'files' => array('filetoid' => array(), 'idtofile' => array()), 'links' => array('filetoid' => array(), 'idtofile' => array()), 'symlinks' => array('filetoid' => array(), 'idtofile' => array()), 'hardlinks' => array('filetoid' => array(), 'idtofile' => array()), 'character' => array('filetoid' => array(), 'idtofile' => array()), 'block' => array('filetoid' => array(), 'idtofile' => array()), 'fifo' => array('filetoid' => array(), 'idtofile' => array()), 'devices' => array('filetoid' => array(), 'idtofile' => array())));
400 $lcfx = count($listcatfiles);
402 $fname = $listcatfiles[$lcfi]['fname'];
403 $fid = $listcatfiles[$lcfi]['fid'];
404 $catarray['filetoid'][$fname] = $fid;
405 $catarray['idtofile'][$fid] = $fname;
406 if($listcatfiles[$lcfi]['ftype']==0) {
407 $catarray['filetypes']['files']['filetoid'][$fname] = $fid;
408 $catarray['filetypes']['files']['idtofile'][$fid] = $fname; }
409 if($listcatfiles[$lcfi]['ftype']==1) {
410 $catarray['filetypes']['hardlinks']['filetoid'][$fname] = $fid;
411 $catarray['filetypes']['hardlinks']['idtofile'][$fid] = $fname;
412 $catarray['filetypes']['links']['filetoid'][$fname] = $fid;
413 $catarray['filetypes']['links']['idtofile'][$fid] = $fname; }
414 if($listcatfiles[$lcfi]['ftype']==2) {
415 $catarray['filetypes']['symlinks']['filetoid'][$fname] = $fid;
416 $catarray['filetypes']['symlinks']['idtofile'][$fid] = $fname;
417 $catarray['filetypes']['links']['filetoid'][$fname] = $fid;
418 $catarray['filetypes']['links']['idtofile'][$fid] = $fname; }
419 if($listcatfiles[$lcfi]['ftype']==3) {
420 $catarray['filetypes']['character']['filetoid'][$fname] = $fid;
421 $catarray['filetypes']['character']['idtofile'][$fid] = $fname;
422 $catarray['filetypes']['devices']['filetoid'][$fname] = $fid;
423 $catarray['filetypes']['devices']['idtofile'][$fid] = $fname; }
424 if($listcatfiles[$lcfi]['ftype']==4) {
425 $catarray['filetypes']['block']['filetoid'][$fname] = $fid;
426 $catarray['filetypes']['block']['idtofile'][$fid] = $fname;
427 $catarray['filetypes']['devices']['filetoid'][$fname] = $fid;
428 $catarray['filetypes']['devices']['idtofile'][$fid] = $fname; }
429 if($listcatfiles[$lcfi]['ftype']==5) {
430 $catarray['filetypes']['directories']['filetoid'][$fname] = $fid;
431 $catarray['filetypes']['directories']['idtofile'][$fid] = $fname; }
432 if($listcatfiles[$lcfi]['ftype']==6) {
433 $catarray['filetypes']['fifo']['filetoid'][$fname] = $fid;
434 $catarray['filetypes']['fifo']['idtofile'][$fid] = $fname;
435 $catarray['filetypes']['devices']['filetoid'][$fname] = $fid;
436 $catarray['filetypes']['devices']['idtofile'][$fid] = $fname; }
440 function RePackCatFile($infiles, $outfile, $seekstart=0, $seekend=0, $checksumtype="crc32", $skipchecksum=False, $verbose=False) {
441 if(is_array($infile)) {
442 $listcatfiles = $infile; }
444 $infile = RemoveWindowsPath($infile);
445 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, False, $skipchecksum); }
446 $checksumtype = strtolower($checksumtype);
447 if($checksumtype!="adler32" && $checksumtype!="crc32" && $checksumtype!="md5" && $checksumtype!="sha1" && $checksumtype!="sha224" && $checksumtype!="sha256" && $checksumtype!="sha384" && $checksumtype!="sha512") {
448 $checksumtype="crc32"; }
449 if($listcatfiles===False) {
452 $lcfx = count($listcatfiles);
454 $fname = $listcatfiles[$lcfi]['fname'];
455 if($verbose===True) {
456 print($fname."\n"); }
457 $fsize = strtolower(dechex(intval($listcatfiles[$lcfi]['fsize'])));
458 $flinkname = $listcatfiles[$lcfi]['flinkname'];
459 $fatime = strtolower(dechex(intval($listcatfiles[$lcfi]['fatime'])));
460 $fmtime = strtolower(dechex(intval($listcatfiles[$lcfi]['fmtime'])));
461 $fmode = strtolower(dechex(intval($listcatfiles[$lcfi]['fmode'])));
462 $fuid = strtolower(dechex(intval($listcatfiles[$lcfi]['fuid'])));
463 $fgid = strtolower(dechex(intval($listcatfiles[$lcfi]['fgid'])));
464 $fdev_minor = strtolower(dechex(intval($listcatfiles[$lcfi]['fminor'])));
465 $fdev_major = strtolower(dechex(intval($listcatfiles[$lcfi]['fmajor'])));
466 $frdev_minor = strtolower(dechex(intval($listcatfiles[$lcfi]['frminor'])));
467 $frdev_major = strtolower(dechex(intval($listcatfiles[$lcfi]['frmajor'])));
468 $fcontents = $listcatfiles[$lcfi]['fcontents'];
469 $ftypehex = strtolower(dechex(intval($listcatfiles[$lcfi]['ftype'])));
470 $ftypeoutstr = $ftypehex;
471 $catfileoutstr = AppendNullByte($ftypeoutstr);
472 $catfileoutstr = $catfileoutstr.AppendNullByte($fname);
473 $catfileoutstr = $catfileoutstr.AppendNullByte($flinkname);
474 $catfileoutstr = $catfileoutstr.AppendNullByte($fsize);
475 $catfileoutstr = $catfileoutstr.AppendNullByte($fatime);
476 $catfileoutstr = $catfileoutstr.AppendNullByte($fmtime);
477 $catfileoutstr = $catfileoutstr.AppendNullByte($fmode);
478 $catfileoutstr = $catfileoutstr.AppendNullByte($fuid);
479 $catfileoutstr = $catfileoutstr.AppendNullByte($fgid);
480 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_minor);
481 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_major);
482 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_minor);
483 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_major);
484 $catfileoutstr = $catfileoutstr.AppendNullByte($checksumtype);
485 if($checksumtype=="adler32" ||
$checksumtype=="crc32") {
486 $catfileheadercshex = strtolower(dechex(hash($checksumtype, $catfileoutstr)));
487 $catfilecontentcshex = strtolower(dechex(hash($checksumtype, $catfileoutstr))); }
488 if($checksumtype=="md5" ||
$checksumtype=="sha1" ||
$checksumtype=="sha224" ||
$checksumtype=="sha256" ||
$checksumtype=="sha384" ||
$checksumtype=="sha512") {
489 $catfileheadercshex = strtolower(hash($checksumtype, $catfileoutstr));
490 $catfilecontentcshex = strtolower(hash($checksumtype, $catfileoutstr)); }
491 $catfileoutstr = $catfileoutstr.AppendNullByte($catfileheadercshex);
492 $catfileoutstr = $catfileoutstr.AppendNullByte($catfilecontentcshex);
493 $catfileoutstrecd = $catfileoutstr;
495 $catfileout = $catfileoutstrecd.$fcontents.$Nullstrecd;
496 fwrite($catfp, $catfileout); }
500 function UnPackCatFile($infile, $outdir=Null, $verbose=False, $skipchecksum=False) {
502 $outdir = RemoveWindowsPath($outdir); }
503 if(is_array($infile)) {
504 $listcatfiles = $infile; }
506 $infile = RemoveWindowsPath($infile);
507 $listcatfiles = CatFileToArray($infile, 0, 0, False, $skipchecksum); }
508 if($listcatfiles===False) {
511 $lcfx = count($listcatfiles);
513 if($verbose===True) {
514 print($listcatfiles[$lcfi]['fname']."\n"); }
515 if($listcatfiles[$lcfi]['ftype']==0) {
516 $fpc = fopen($listcatfiles[$lcfi]['fname'], "wb");
517 fwrite($fpc, $listcatfiles[$lcfi]['fcontents']);
519 chown($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fuid']);
520 chgrp($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fgid']);
521 chmod($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fchmod']);
522 touch($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fmtime'], $listcatfiles[$lcfi]['fatime']); }
523 if($listcatfiles[$lcfi]['ftype']==1) {
524 link($listcatfiles[$lcfi]['flinkname'], $listcatfiles[$lcfi]['fname']); }
525 if($listcatfiles[$lcfi]['ftype']==2) {
526 symlink($listcatfiles[$lcfi]['flinkname'], $listcatfiles[$lcfi]['fname']); }
527 if($listcatfiles[$lcfi]['ftype']==5) {
528 mkdir($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fchmod']);
529 chown($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fuid']);
530 chgrp($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fgid']);
531 chmod($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fchmod']);
532 touch($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fmtime'], $listcatfiles[$lcfi]['fatime']); }
536 function CatFileListFiles($infile, $seekstart=0, $seekend=0, $verbose=False, $skipchecksum=False) {
537 if(is_array($infile)) {
538 $listcatfiles = $infile; }
540 $infile = RemoveWindowsPath($infile);
541 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, True, $skipchecksum); }
542 if($listcatfiles===False) {
545 $lcfx = count($listcatfiles);
546 $returnval = array();
548 $returnval[$lcfi] = $listcatfiles[$lcfi]['fname'];
549 if($verbose===False) {
550 print($listcatfiles[$lcfi]['fname']."\n"); }
551 if($verbose===True) {
553 if($listcatfiles[$lcfi]['ftype']==0) {
554 $permissionstr = "-"; }
555 if($listcatfiles[$lcfi]['ftype']==1) {
556 $permissionstr = "h"; }
557 if($listcatfiles[$lcfi]['ftype']==2) {
558 $permissionstr = "l"; }
559 if($listcatfiles[$lcfi]['ftype']==3) {
560 $permissionstr = "c"; }
561 if($listcatfiles[$lcfi]['ftype']==4) {
562 $permissionstr = "b"; }
563 if($listcatfiles[$lcfi]['ftype']==5) {
564 $permissionstr = "d"; }
565 if($listcatfiles[$lcfi]['ftype']==6) {
566 $permissionstr = "f"; }
567 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0100) ?
'r' : '-');
568 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0080) ?
'w' : '-');
569 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0040) ?
570 (($listcatfiles[$lcfi]['fchmod'] & 0x0800) ?
's' : 'x' ) :
571 (($listcatfiles[$lcfi]['fchmod'] & 0x0800) ?
'S' : '-'));
572 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0020) ?
'r' : '-');
573 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0010) ?
'w' : '-');
574 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0008) ?
575 (($listcatfiles[$lcfi]['fchmod'] & 0x0400) ?
's' : 'x' ) :
576 (($listcatfiles[$lcfi]['fchmod'] & 0x0400) ?
'S' : '-'));
577 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0004) ?
'r' : '-');
578 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0002) ?
'w' : '-');
579 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0001) ?
580 (($listcatfiles[$lcfi]['fchmod'] & 0x0200) ?
't' : 'x' ) :
581 (($listcatfiles[$lcfi]['fchmod'] & 0x0200) ?
'T' : '-'));
582 $printfname = $listcatfiles[$lcfi]['fname'];
583 if($listcatfiles[$lcfi]['ftype']==1) {
584 $printfname = $listcatfiles[$lcfi]['fname']." link to "+
$listcatfiles[$lcfi]['flinkname']; }
585 if($listcatfiles[$lcfi]['ftype']==2) {
586 $printfname = $listcatfiles[$lcfi]['fname']." -> "+
$listcatfiles[$lcfi]['flinkname']; }
587 print($permissionstr." ".$listcatfiles[$lcfi]['fuid']."/".$listcatfiles[$lcfi]['fgid']." ".str_pad($listcatfiles[$lcfi]['fsize'], 15, " ", STR_PAD_LEFT
)." ".gmdate('Y-m-d H:i', $listcatfiles[$lcfi]['fmtime'])." ".$printfname."\n"); }