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: 7/10/2024 Ver. 0.13.12 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, 13, 14, "RC 1", 1];
24 $info['version_id'] = "$Id$";
25 $info['version_date_info'] = [2024, 7, 10, "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];
30 if ($info['version_info'][4] === null) {
31 $info['version_date_plusrc'] = $info['version_date'];
33 if ($info['version_info'][3] !== null) {
34 $info['version'] = $info['version_info'][0].".".$info['version_info'][1].".".$info['version_info'][2]." ".$info['version_info'][3];
36 if ($info['version_info'][3] === null) {
37 $info['version'] = $info['version_info'][0].".".$info['version_info'][1].".".$info['version_info'][2];
40 if (!function_exists('hex2bin')) {
41 function hex2bin($str)
45 for ($i = 0; $i < $len; $i +
= 2) {
46 $sbin .= pack("H*", substr($str, $i, 2));
53 function RemoveWindowsPath($dpath)
55 if ($dpath === null) {
58 $dpath = str_replace(DIRECTORY_SEPARATOR
, "/", $dpath);
59 $dpath = rtrim($dpath, "/");
60 if ($dpath == "." ||
$dpath == "..") {
66 function NormalizeRelativePath($inpath)
68 $inpath = RemoveWindowsPath($inpath);
69 if (strpos($inpath, '/') !== 0) { // Checks if not an absolute path
70 if (!str_starts_with($inpath, "./") && !str_starts_with($inpath, "../")) {
71 $inpath = "./" . $inpath;
77 function ListDir($dirpath, $followlink = false, $duplicates = false)
79 if (is_array($dirpath) ||
is_object($dirpath)) {
80 $dirpath = array_filter((array)$dirpath);
82 $dirpath = array_filter([$dirpath]);
85 foreach ($dirpath as $mydirfile) {
86 if (!file_exists($mydirfile)) {
89 $mydirfile = NormalizeRelativePath($mydirfile);
90 if (file_exists($mydirfile) && is_link($mydirfile) && $followlink) {
91 $mydirfile = RemoveWindowsPath(realpath($mydirfile));
93 if (file_exists($mydirfile) && is_dir($mydirfile)) {
94 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($mydirfile));
95 foreach ($files as $file) {
99 $fpath = RemoveWindowsPath($file->getPathname());
100 if (!$duplicates && !in_array($fpath, $retlist)) {
102 } elseif ($duplicates) {
107 $retlist[] = RemoveWindowsPath($mydirfile);
113 function ReadTillNullByte($fp)
116 while (($curByte = fgetc($fp)) !== "\0" && $curByte !== false) {
117 $curFullByte .= $curByte;
122 function ReadUntilNullByte($fp)
124 return readTillNullByte($fp);
127 function SeekToEndOfFile($fp)
129 fseek($fp, 0, SEEK_END
);
133 function ReadFileHeaderData($fp, $rounds = 0)
136 for ($roCount = 0; $roCount < $rounds; $roCount++
) {
137 $headerOut[$roCount] = ReadTillNullByte($fp);
142 function AppendNullByte($indata)
144 return $indata . "\0";
147 function AppendNullBytes($indata = [])
150 foreach ($indata as $item) {
151 $outData .= AppendNullByte($item);
156 function ReadTillNullByteAlt($fp)
159 while (($curByte = fgetc($fp)) !== "\0" && $curByte !== false) {
160 $bytesList .= $curByte;
165 function readUntilNullByteAlt($fp)
167 return readTillNullByteAlt($fp);
170 function ReadFileHeaderDataAlt($fp, $rounds = 0)
173 for ($roundCount = 0; $roundCount < $rounds; $roundCount++
) {
174 $headerOut[$roundCount] = ReadTillNullByteAlt($fp);
179 function AppendNullByteAlt($indata)
181 return $indata . "\0";
184 function AppendNullBytesAlt($indata = [])
186 return implode("\0", array_map('strval', $indata)) . "\0";
189 function CheckFileType($infile)
191 $catfp = fopen($infile, "rb");
193 $prefp = fread($catfp, 2);
195 if ($prefp == hex2bin("1f8b")) {
199 $prefp = fread($catfp, 3);
200 if ($prefp == hex2bin("425a68")) {
204 $prefp = fread($catfp, 7);
205 /*if($prefp==hex2bin("fd377a585a0000")) {
206 $filetype = "lzma"; }*/
207 if ($prefp == hex2bin("43617446696c65")) {
208 $filetype = "catfile";
214 function CompressCatFile($infile)
216 if (pathinfo($infile, PATHINFO_EXTENSION
) == "gz" or pathinfo($infile, PATHINFO_EXTENSION
) == "cgz") {
217 if (!function_exists("gzcompress")) {
220 if (file_exists(pathinfo($infile, PATHINFO_FILENAME
).".tmp")) {
221 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp");
223 rename($infile, pathinfo($infile, PATHINFO_FILENAME
).".tmp");
224 $catuncomp = fopen(pathinfo($infile, PATHINFO_FILENAME
).".tmp", "rb");
225 $catcomp = fopen($infile, "wb");
226 fseek($catuncomp, 0, SEEK_END
);
227 $endoffile = ftell($catuncomp);
228 fseek($catuncomp, 0, SEEK_SET
);
229 fwrite($catcomp, gzcompress(fread($catuncomp, $endoffile), 9));
232 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp");
234 if (pathinfo($infile, PATHINFO_EXTENSION
) == "bz2" or pathinfo($infile, PATHINFO_EXTENSION
) == "cbz") {
235 if (!function_exists("gzcompress")) {
238 if (file_exists(pathinfo($infile, PATHINFO_FILENAME
).".tmp")) {
239 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp");
241 rename($infile, pathinfo($infile, PATHINFO_FILENAME
).".tmp");
242 $catuncomp = fopen(pathinfo($infile, PATHINFO_FILENAME
).".tmp", "rb");
243 $catcomp = fopen($infile, "wb");
244 fseek($catuncomp, 0, SEEK_END
);
245 $endoffile = ftell($catuncomp);
246 fseek($catuncomp, 0, SEEK_SET
);
247 fwrite($catcomp, bzcompress(fread($catuncomp, $endoffile), 9));
250 unlink(pathinfo($infile, PATHINFO_FILENAME
).".tmp");
255 function PackCatFile($infiles, $outfile, $followlink = false, $checksumtype = "crc32", $verbose = false)
258 $catver = $info['version_info'][0].".".$info['version_info'][1].".".$info['version_info'][2];
259 $infiles = RemoveWindowsPath($infiles);
260 $outfile = RemoveWindowsPath($outfile);
261 $checksumtype = strtolower($checksumtype);
262 if ($checksumtype != "adler32" && $checksumtype != "crc32" && $checksumtype != "md5" && $checksumtype != "sha1" && $checksumtype != "sha224" && $checksumtype != "sha256" && $checksumtype != "sha384" && $checksumtype != "sha512") {
263 $checksumtype = "crc32";
265 if (file_exists($outfile)) {
268 $catfp = fopen($outfile, "wb");
269 $fileheaderver = intval(str_replace(".", "", $catver));
270 $fileheader = AppendNullByte("CatFile".$fileheaderver);
271 fwrite($catfp, $fileheader);
272 $GetDirList = ListDir($infiles);
273 foreach ($GetDirList as $curfname) {
275 if ($verbose === true) {
278 if ($followlink === false ||
$followlink === null) {
279 $fstatinfo = lstat($fname);
281 $fstatinfo = stat($fname);
284 if (is_file($fname)) {
287 if (is_link($fname)) {
290 if (is_dir($fname)) {
293 if ($ftype == 1 ||
$ftype == 2 ||
$ftype == 5) {
294 $fsize = strtolower(dechex(intval("0")));
297 $fsize = strtolower(dechex(intval($fstatinfo['size'])));
300 if ($ftype == 1 ||
$ftype == 2) {
301 $flinkname = readlink($fname);
303 $fatime = strtolower(dechex(intval($fstatinfo['atime'])));
304 $fmtime = strtolower(dechex(intval($fstatinfo['mtime'])));
305 $fmode = strtolower(dechex(intval($fstatinfo['mode'])));
306 $fuid = strtolower(dechex(intval($fstatinfo['uid'])));
307 $fgid = strtolower(dechex(intval($fstatinfo['gid'])));
308 $fdev_minor = strtolower(dechex(intval(0)));
309 $fdev_major = strtolower(dechex(intval(0)));
310 $frdev_minor = strtolower(dechex(intval(0)));
311 $frdev_major = strtolower(dechex(intval(0)));
314 $fpc = fopen($fname, "rb");
315 $fcontents = fread($fpc, intval($fstatinfo['size']));
318 if ($followlink === false && ($ftype == 1 && $ftype == 2)) {
319 $flstatinfo = stat($flinkname);
320 $fpc = fopen($flinkname, "rb");
321 $fcontents = fread($fpc, intval($flstatinfo['size']));
324 $ftypehex = strtolower(dechex($ftype));
325 $ftypeoutstr = $ftypehex;
326 $catfileoutstr = AppendNullByte($ftypeoutstr);
327 $catfileoutstr = $catfileoutstr.AppendNullByte($fname);
328 $catfileoutstr = $catfileoutstr.AppendNullByte($flinkname);
329 $catfileoutstr = $catfileoutstr.AppendNullByte($fsize);
330 $catfileoutstr = $catfileoutstr.AppendNullByte($fatime);
331 $catfileoutstr = $catfileoutstr.AppendNullByte($fmtime);
332 $catfileoutstr = $catfileoutstr.AppendNullByte($fmode);
333 $catfileoutstr = $catfileoutstr.AppendNullByte($fuid);
334 $catfileoutstr = $catfileoutstr.AppendNullByte($fgid);
335 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_minor);
336 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_major);
337 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_minor);
338 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_major);
339 $catfileoutstr = $catfileoutstr.AppendNullByte($checksumtype);
340 if ($checksumtype == "adler32" ||
$checksumtype == "crc32") {
341 $catfileheadercshex = strtolower(dechex(hash($checksumtype, $catfileoutstr)));
342 $catfilecontentcshex = strtolower(dechex(hash($checksumtype, $catfileoutstr)));
344 if ($checksumtype == "md5" ||
$checksumtype == "sha1" ||
$checksumtype == "sha224" ||
$checksumtype == "sha256" ||
$checksumtype == "sha384" ||
$checksumtype == "sha512") {
345 $catfileheadercshex = strtolower(hash($checksumtype, $catfileoutstr));
346 $catfilecontentcshex = strtolower(hash($checksumtype, $catfileoutstr));
348 $catfileoutstr = $catfileoutstr.AppendNullByte($catfileheadercshex);
349 $catfileoutstr = $catfileoutstr.AppendNullByte($catfilecontentcshex);
350 $catfileoutstrecd = $catfileoutstr;
352 $catfileout = $catfileoutstrecd.$fcontents.$Nullstrecd;
353 fwrite($catfp, $catfileout);
359 function CatFileToArray($infile, $seekstart = 0, $seekend = 0, $listonly = false, $skipchecksum = false)
361 $infile = RemoveWindowsPath($infile);
362 $compresscheck = CheckFileType($infile);
363 if ($compresscheck === false) {
366 if ($compresscheck === "gzip") {
367 $catfp = gzopen($infile, "rb");
369 if ($compresscheck === "bzip2") {
370 $catfp = bzopen($infile, "rb");
372 if ($compresscheck === "catfile") {
373 $catfp = fopen($infile, "rb");
375 fseek($catfp, 0, SEEK_END
);
376 $CatSize = ftell($catfp);
377 $CatSizeEnd = $CatSize;
378 fseek($catfp, 0, SEEK_SET
);
379 $catstring = ReadFileHeaderData($catfp, 1)[0];
380 preg_match("/([\d]+)$/", $catstring, $catm);
381 $catversion = $catm[0];
384 if ($seekstart != 0) {
385 fseek($catfp, $seekstart, SEEK_SET
);
387 if ($seekstart == 0) {
388 $seekstart = ftell($catfp);
391 $seekend = $CatSizeEnd;
393 while ($seekstart < $seekend) {
394 $catfhstart = ftell($catfp);
395 $catheaderdata = ReadFileHeaderData($catfp, 16);
396 $catftype = hexdec($catheaderdata[0]);
397 $catfname = $catheaderdata[1];
398 $catflinkname = $catheaderdata[2];
399 $catfsize = hexdec($catheaderdata[3]);
400 $catfatime = hexdec($catheaderdata[4]);
401 $catfmtime = hexdec($catheaderdata[5]);
402 $catfmode = decoct(hexdec($catheaderdata[6]));
403 $catfchmod = substr($catfmode, -3);
404 $catfuid = hexdec($catheaderdata[7]);
405 $catfgid = hexdec($catheaderdata[8]);
406 $catfdev_minor = hexdec($catheaderdata[9]);
407 $catfdev_major = hexdec($catheaderdata[10]);
408 $catfrdev_minor = hexdec($catheaderdata[11]);
409 $catfrdev_major = hexdec($catheaderdata[12]);
410 $catfchecksumtype = strtolower($catheaderdata[13]);
411 if ($catfchecksumtype == "adler32" ||
$catfchecksumtype == "crc32") {
412 $catfcs = hexdec($catheaderdata[14]);
413 $catfccs = hexdec($catheaderdata[15]);
415 if ($catfchecksumtype == "md5" ||
$catfchecksumtype == "sha1" ||
$catfchecksumtype == "sha224" ||
$catfchecksumtype == "sha256" ||
$catfchecksumtype == "sha384" ||
$catfchecksumtype == "sha512") {
416 $catfcs = $catheaderdata[14];
417 $catfccs = $catheaderdata[15];
420 $hcmax = count($catheaderdata) - 2;
422 while ($hc < $hcmax) {
423 $hout = $hout.AppendNullByte($catheaderdata[$hc]);
426 $catnewfcs = strtolower(hash($catfchecksumtype, $hout));
427 if ($catfcs != $catnewfcs && $skipchecksum === false) {
428 print("File Header Checksum Error with file " +
$catfname +
" at offset " +
$catfhstart);
431 $catfhend = ftell($catfp) - 1;
432 $catfcontentstart = ftell($catfp);
434 $phphascontents = false;
435 if ($catfsize > 1 && $listonly === false) {
436 $catfcontents = fread($catfp, $catfsize);
437 $catnewfccs = strtolower(hash($catfchecksumtype, $catfcontents));
438 if ($catfccs != $catnewfccs && $skipchecksum === false) {
439 print("File Content Checksum Error with file " +
$catfname +
" at offset " +
$catfcontentstart);
442 $phphascontents = true;
444 if ($catfsize > 1 && $listonly === true) {
445 fseek($catfp, $catfsize, SEEK_CUR
);
446 $phphascontents = false;
448 $catfcontentend = ftell($catfp);
449 $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);
450 fseek($catfp, 1, SEEK_CUR
);
451 $seekstart = ftell($catfp);
452 $fileidnum = $fileidnum +
1;
458 function CatFileToArrayIndex($infile, $seekstart = 0, $seekend = 0, $listonly = false, $skipchecksum = false)
460 if (is_array($infile)) {
461 $listcatfiles = $infile;
463 $infile = RemoveWindowsPath($infile);
464 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, $listonly, $skipchecksum);
466 if ($listcatfiles === false) {
469 $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())));
471 $lcfx = count($listcatfiles);
472 while ($lcfi < $lcfx) {
473 $fname = $listcatfiles[$lcfi]['fname'];
474 $fid = $listcatfiles[$lcfi]['fid'];
475 $catarray['filetoid'][$fname] = $fid;
476 $catarray['idtofile'][$fid] = $fname;
477 if ($listcatfiles[$lcfi]['ftype'] == 0) {
478 $catarray['filetypes']['files']['filetoid'][$fname] = $fid;
479 $catarray['filetypes']['files']['idtofile'][$fid] = $fname;
481 if ($listcatfiles[$lcfi]['ftype'] == 1) {
482 $catarray['filetypes']['hardlinks']['filetoid'][$fname] = $fid;
483 $catarray['filetypes']['hardlinks']['idtofile'][$fid] = $fname;
484 $catarray['filetypes']['links']['filetoid'][$fname] = $fid;
485 $catarray['filetypes']['links']['idtofile'][$fid] = $fname;
487 if ($listcatfiles[$lcfi]['ftype'] == 2) {
488 $catarray['filetypes']['symlinks']['filetoid'][$fname] = $fid;
489 $catarray['filetypes']['symlinks']['idtofile'][$fid] = $fname;
490 $catarray['filetypes']['links']['filetoid'][$fname] = $fid;
491 $catarray['filetypes']['links']['idtofile'][$fid] = $fname;
493 if ($listcatfiles[$lcfi]['ftype'] == 3) {
494 $catarray['filetypes']['character']['filetoid'][$fname] = $fid;
495 $catarray['filetypes']['character']['idtofile'][$fid] = $fname;
496 $catarray['filetypes']['devices']['filetoid'][$fname] = $fid;
497 $catarray['filetypes']['devices']['idtofile'][$fid] = $fname;
499 if ($listcatfiles[$lcfi]['ftype'] == 4) {
500 $catarray['filetypes']['block']['filetoid'][$fname] = $fid;
501 $catarray['filetypes']['block']['idtofile'][$fid] = $fname;
502 $catarray['filetypes']['devices']['filetoid'][$fname] = $fid;
503 $catarray['filetypes']['devices']['idtofile'][$fid] = $fname;
505 if ($listcatfiles[$lcfi]['ftype'] == 5) {
506 $catarray['filetypes']['directories']['filetoid'][$fname] = $fid;
507 $catarray['filetypes']['directories']['idtofile'][$fid] = $fname;
509 if ($listcatfiles[$lcfi]['ftype'] == 6) {
510 $catarray['filetypes']['fifo']['filetoid'][$fname] = $fid;
511 $catarray['filetypes']['fifo']['idtofile'][$fid] = $fname;
512 $catarray['filetypes']['devices']['filetoid'][$fname] = $fid;
513 $catarray['filetypes']['devices']['idtofile'][$fid] = $fname;
520 function RePackCatFile($infiles, $outfile, $seekstart = 0, $seekend = 0, $checksumtype = "crc32", $skipchecksum = false, $verbose = false)
522 if (is_array($infile)) {
523 $listcatfiles = $infile;
525 $infile = RemoveWindowsPath($infile);
526 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, false, $skipchecksum);
528 $checksumtype = strtolower($checksumtype);
529 if ($checksumtype != "adler32" && $checksumtype != "crc32" && $checksumtype != "md5" && $checksumtype != "sha1" && $checksumtype != "sha224" && $checksumtype != "sha256" && $checksumtype != "sha384" && $checksumtype != "sha512") {
530 $checksumtype = "crc32";
532 if ($listcatfiles === false) {
536 $lcfx = count($listcatfiles);
537 while ($lcfi < $lcfx) {
538 $fname = $listcatfiles[$lcfi]['fname'];
539 if ($verbose === true) {
542 $fsize = strtolower(dechex(intval($listcatfiles[$lcfi]['fsize'])));
543 $flinkname = $listcatfiles[$lcfi]['flinkname'];
544 $fatime = strtolower(dechex(intval($listcatfiles[$lcfi]['fatime'])));
545 $fmtime = strtolower(dechex(intval($listcatfiles[$lcfi]['fmtime'])));
546 $fmode = strtolower(dechex(intval($listcatfiles[$lcfi]['fmode'])));
547 $fuid = strtolower(dechex(intval($listcatfiles[$lcfi]['fuid'])));
548 $fgid = strtolower(dechex(intval($listcatfiles[$lcfi]['fgid'])));
549 $fdev_minor = strtolower(dechex(intval($listcatfiles[$lcfi]['fminor'])));
550 $fdev_major = strtolower(dechex(intval($listcatfiles[$lcfi]['fmajor'])));
551 $frdev_minor = strtolower(dechex(intval($listcatfiles[$lcfi]['frminor'])));
552 $frdev_major = strtolower(dechex(intval($listcatfiles[$lcfi]['frmajor'])));
553 $fcontents = $listcatfiles[$lcfi]['fcontents'];
554 $ftypehex = strtolower(dechex(intval($listcatfiles[$lcfi]['ftype'])));
555 $ftypeoutstr = $ftypehex;
556 $catfileoutstr = AppendNullByte($ftypeoutstr);
557 $catfileoutstr = $catfileoutstr.AppendNullByte($fname);
558 $catfileoutstr = $catfileoutstr.AppendNullByte($flinkname);
559 $catfileoutstr = $catfileoutstr.AppendNullByte($fsize);
560 $catfileoutstr = $catfileoutstr.AppendNullByte($fatime);
561 $catfileoutstr = $catfileoutstr.AppendNullByte($fmtime);
562 $catfileoutstr = $catfileoutstr.AppendNullByte($fmode);
563 $catfileoutstr = $catfileoutstr.AppendNullByte($fuid);
564 $catfileoutstr = $catfileoutstr.AppendNullByte($fgid);
565 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_minor);
566 $catfileoutstr = $catfileoutstr.AppendNullByte($fdev_major);
567 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_minor);
568 $catfileoutstr = $catfileoutstr.AppendNullByte($frdev_major);
569 $catfileoutstr = $catfileoutstr.AppendNullByte($checksumtype);
570 if ($checksumtype == "adler32" ||
$checksumtype == "crc32") {
571 $catfileheadercshex = strtolower(dechex(hash($checksumtype, $catfileoutstr)));
572 $catfilecontentcshex = strtolower(dechex(hash($checksumtype, $catfileoutstr)));
574 if ($checksumtype == "md5" ||
$checksumtype == "sha1" ||
$checksumtype == "sha224" ||
$checksumtype == "sha256" ||
$checksumtype == "sha384" ||
$checksumtype == "sha512") {
575 $catfileheadercshex = strtolower(hash($checksumtype, $catfileoutstr));
576 $catfilecontentcshex = strtolower(hash($checksumtype, $catfileoutstr));
578 $catfileoutstr = $catfileoutstr.AppendNullByte($catfileheadercshex);
579 $catfileoutstr = $catfileoutstr.AppendNullByte($catfilecontentcshex);
580 $catfileoutstrecd = $catfileoutstr;
582 $catfileout = $catfileoutstrecd.$fcontents.$Nullstrecd;
583 fwrite($catfp, $catfileout);
589 function UnPackCatFile($infile, $outdir = null, $verbose = false, $skipchecksum = false)
591 if ($outdir !== null) {
592 $outdir = RemoveWindowsPath($outdir);
594 if (is_array($infile)) {
595 $listcatfiles = $infile;
597 $infile = RemoveWindowsPath($infile);
598 $listcatfiles = CatFileToArray($infile, 0, 0, false, $skipchecksum);
600 if ($listcatfiles === false) {
604 $lcfx = count($listcatfiles);
605 while ($lcfi < $lcfx) {
606 if ($verbose === true) {
607 print($listcatfiles[$lcfi]['fname']."\n");
609 if ($listcatfiles[$lcfi]['ftype'] == 0) {
610 $fpc = fopen($listcatfiles[$lcfi]['fname'], "wb");
611 fwrite($fpc, $listcatfiles[$lcfi]['fcontents']);
613 chown($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fuid']);
614 chgrp($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fgid']);
615 chmod($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fchmod']);
616 touch($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fmtime'], $listcatfiles[$lcfi]['fatime']);
618 if ($listcatfiles[$lcfi]['ftype'] == 1) {
619 link($listcatfiles[$lcfi]['flinkname'], $listcatfiles[$lcfi]['fname']);
621 if ($listcatfiles[$lcfi]['ftype'] == 2) {
622 symlink($listcatfiles[$lcfi]['flinkname'], $listcatfiles[$lcfi]['fname']);
624 if ($listcatfiles[$lcfi]['ftype'] == 5) {
625 mkdir($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fchmod']);
626 chown($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fuid']);
627 chgrp($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fgid']);
628 chmod($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fchmod']);
629 touch($listcatfiles[$lcfi]['fname'], $listcatfiles[$lcfi]['fmtime'], $listcatfiles[$lcfi]['fatime']);
636 function CatFileListFiles($infile, $seekstart = 0, $seekend = 0, $verbose = false, $skipchecksum = false)
638 if (is_array($infile)) {
639 $listcatfiles = $infile;
641 $infile = RemoveWindowsPath($infile);
642 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, true, $skipchecksum);
644 if ($listcatfiles === false) {
648 $lcfx = count($listcatfiles);
649 $returnval = array();
650 while ($lcfi < $lcfx) {
651 $returnval[$lcfi] = $listcatfiles[$lcfi]['fname'];
652 if ($verbose === false) {
653 print($listcatfiles[$lcfi]['fname']."\n");
655 if ($verbose === true) {
657 if ($listcatfiles[$lcfi]['ftype'] == 0) {
658 $permissionstr = "-";
660 if ($listcatfiles[$lcfi]['ftype'] == 1) {
661 $permissionstr = "h";
663 if ($listcatfiles[$lcfi]['ftype'] == 2) {
664 $permissionstr = "l";
666 if ($listcatfiles[$lcfi]['ftype'] == 3) {
667 $permissionstr = "c";
669 if ($listcatfiles[$lcfi]['ftype'] == 4) {
670 $permissionstr = "b";
672 if ($listcatfiles[$lcfi]['ftype'] == 5) {
673 $permissionstr = "d";
675 if ($listcatfiles[$lcfi]['ftype'] == 6) {
676 $permissionstr = "f";
678 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0100) ?
'r' : '-');
679 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0080) ?
'w' : '-');
680 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0040) ?
681 (($listcatfiles[$lcfi]['fchmod'] & 0x0800) ?
's' : 'x') :
682 (($listcatfiles[$lcfi]['fchmod'] & 0x0800) ?
'S' : '-'));
683 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0020) ?
'r' : '-');
684 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0010) ?
'w' : '-');
685 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0008) ?
686 (($listcatfiles[$lcfi]['fchmod'] & 0x0400) ?
's' : 'x') :
687 (($listcatfiles[$lcfi]['fchmod'] & 0x0400) ?
'S' : '-'));
688 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0004) ?
'r' : '-');
689 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0002) ?
'w' : '-');
690 $permissionstr .= (($listcatfiles[$lcfi]['fchmod'] & 0x0001) ?
691 (($listcatfiles[$lcfi]['fchmod'] & 0x0200) ?
't' : 'x') :
692 (($listcatfiles[$lcfi]['fchmod'] & 0x0200) ?
'T' : '-'));
693 $printfname = $listcatfiles[$lcfi]['fname'];
694 if ($listcatfiles[$lcfi]['ftype'] == 1) {
695 $printfname = $listcatfiles[$lcfi]['fname']." link to " +
$listcatfiles[$lcfi]['flinkname'];
697 if ($listcatfiles[$lcfi]['ftype'] == 2) {
698 $printfname = $listcatfiles[$lcfi]['fname']." -> " +
$listcatfiles[$lcfi]['flinkname'];
700 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");