Add files via upload
[PyCatFile.git] / phpcatfile.php
blob6182cb78aed5d37345f3e1c1f2e891250d79bc67
1 <?php
2 /*
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: 3/13/2024 Ver. 0.4.0 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, 4, 0, "RC 1", 1];
24 $info['version_id'] = "$Id$";
25 $info['version_date_info'] = [2024, 3, 13, "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 ) {
38 $sbin = "";
39 $len = strlen( $str );
40 for ( $i = 0; $i < $len; $i += 2 ) {
41 $sbin .= pack( "H*", substr( $str, $i, 2 ) );
44 return $sbin;
48 function RemoveWindowsPath($dpath) {
49 if ($dpath === null) {
50 $dpath = "";
52 $dpath = str_replace(DIRECTORY_SEPARATOR, "/", $dpath);
53 $dpath = rtrim($dpath, "/");
54 if ($dpath == "." || $dpath == "..") {
55 $dpath .= "/";
57 return $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;
67 return $inpath;
70 function ListDir($dirpath, $followlink = false, $duplicates = false) {
71 if (is_array($dirpath) || is_object($dirpath)) {
72 $dirpath = array_filter((array)$dirpath);
73 } else {
74 $dirpath = array_filter([$dirpath]);
76 $retlist = [];
77 foreach ($dirpath as $mydirfile) {
78 if (!file_exists($mydirfile)) {
79 return false;
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)) {
91 $retlist[] = $fpath;
92 } elseif ($duplicates) {
93 $retlist[] = $fpath;
96 } else {
97 $retlist[] = RemoveWindowsPath($mydirfile);
100 return $retlist;
103 function ReadTillNullByte($fp) {
104 $curFullByte = "";
105 while(($curByte = fgetc($fp)) !== "\0" && $curByte !== false) {
106 $curFullByte .= $curByte;
108 return $curFullByte;
111 function ReadUntilNullByte($fp) {
112 return readTillNullByte($fp);
115 function SeekToEndOfFile($fp) {
116 fseek($fp, 0, SEEK_END);
117 return true;
120 function ReadFileHeaderData($fp, $rounds = 0) {
121 $headerOut = [];
122 for ($roCount = 0; $roCount < $rounds; $roCount++) {
123 $headerOut[$roCount] = ReadTillNullByte($fp);
125 return $headerOut;
128 function AppendNullByte($indata) {
129 return $indata . "\0";
132 function AppendNullBytes($indata = []) {
133 $outData = "";
134 foreach ($indata as $item) {
135 $outData .= AppendNullByte($item);
137 return $outData;
140 function ReadTillNullByteAlt($fp) {
141 $bytesList = "";
142 while (($curByte = fgetc($fp)) !== "\0" && $curByte !== false) {
143 $bytesList .= $curByte;
145 return $bytesList;
148 function readUntilNullByteAlt($fp) {
149 return readTillNullByteAlt($fp);
152 function ReadFileHeaderDataAlt($fp, $rounds = 0) {
153 $headerOut = [];
154 for ($roundCount = 0; $roundCount < $rounds; $roundCount++) {
155 $headerOut[$roundCount] = ReadTillNullByteAlt($fp);
157 return $headerOut;
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");
170 fseek($catfp, 0, 0);
171 $prefp = fread($catfp, 2);
172 $filetype = False;
173 if($prefp==hex2bin("1f8b")) {
174 $filetype = "gzip"; }
175 fseek($catfp, 0, 0);
176 $prefp = fread($catfp, 3);
177 if($prefp==hex2bin("425a68")) {
178 $filetype = "bzip2"; }
179 fseek($catfp, 0, 0);
180 $prefp = fread($catfp, 7);
181 /*if($prefp==hex2bin("fd377a585a0000")) {
182 $filetype = "lzma"; }*/
183 if($prefp==hex2bin("43617446696c65")) {
184 $filetype = "catfile"; }
185 fclose($catfp);
186 return $filetype; }
188 function CompressCatFile($infile) {
189 if(pathinfo($infile, PATHINFO_EXTENSION)=="gz" or pathinfo($infile, PATHINFO_EXTENSION)=="cgz") {
190 if(!function_exists("gzcompress")) {
191 return False; }
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));
201 fclose($catcomp);
202 fclose($catuncomp);
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")) {
206 return False; }
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));
216 fclose($catcomp);
217 fclose($catuncomp);
218 unlink(pathinfo($infile, PATHINFO_FILENAME).".tmp"); }
219 return True; }
221 function PackCatFile($infiles, $outfile, $followlink=False, $checksumtype="crc32", $verbose=False) {
222 global $info;
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)) {
230 unlink($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) {
237 $fname = $curfname;
238 if($verbose===True) {
239 print($fname."\n"); }
240 if($followlink===False || $followlink===Null) {
241 $fstatinfo = lstat($fname); }
242 else {
243 $fstatinfo = stat($fname); }
244 $ftype = 0;
245 if(is_file($fname)) {
246 $ftype = 0; }
247 if(is_link($fname)) {
248 $ftype = 2; }
249 if(is_dir($fname)) {
250 $ftype = 5; }
251 if($ftype==1 || $ftype==2 || $ftype==5) {
252 $fsize = strtolower(dechex(intval("0"))); }
253 if($ftype==0) {
254 $fsize = strtolower(dechex(intval($fstatinfo['size']))); }
255 $flinkname = "";
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)));
267 $fcontents = "";
268 if($ftype==0) {
269 $fpc = fopen($fname, "rb");
270 $fcontents = fread($fpc, intval($fstatinfo['size']));
271 fclose($fpc); }
272 if($followlink===False && ($ftype==1 && $ftype==2)) {
273 $flstatinfo = stat($flinkname);
274 $fpc = fopen($flinkname, "rb");
275 $fcontents = fread($fpc, intval($flstatinfo['size']));
276 fclose($fpc); }
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;
302 $Nullstrecd = "\0";
303 $catfileout = $catfileoutstrecd.$fcontents.$Nullstrecd;
304 fwrite($catfp, $catfileout); }
305 fclose($catfp);
306 return True; }
308 function CatFileToArray($infile, $seekstart=0, $seekend=0, $listonly=False, $skipchecksum=False) {
309 $infile = RemoveWindowsPath($infile);
310 $compresscheck = CheckFileType($infile);
311 if($compresscheck===False) {
312 return 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];
326 $catlist = array();
327 $fileidnum = 0;
328 if($seekstart!=0) {
329 fseek($catfp, $seekstart, SEEK_SET); }
330 if($seekstart==0) {
331 $seekstart = ftell($catfp); }
332 if($seekend==0) {
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]; }
358 $hc = 0;
359 $hcmax = count($catheaderdata) - 2;
360 $hout = "";
361 while($hc<$hcmax) {
362 $hout = $hout.AppendNullByte($catheaderdata[$hc]);
363 $hc = $hc + 1; }
364 $catnewfcs = strtolower(hash($catfchecksumtype, $hout));
365 if($catfcs!=$catnewfcs && $skipchecksum===False) {
366 print("File Header Checksum Error with file "+$catfname+" at offset "+$catfhstart);
367 return False; }
368 $catfhend = ftell($catfp) - 1;
369 $catfcontentstart = ftell($catfp);
370 $catfcontents = "";
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);
377 return False; }
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; }
387 fclose($catfp);
388 return $catlist; }
390 function CatFileToArrayIndex($infile, $seekstart=0, $seekend=0, $listonly=False, $skipchecksum=False) {
391 if(is_array($infile)) {
392 $listcatfiles = $infile; }
393 else {
394 $infile = RemoveWindowsPath($infile);
395 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, $listonly, $skipchecksum); }
396 if($listcatfiles===False) {
397 return 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())));
399 $lcfi = 0;
400 $lcfx = count($listcatfiles);
401 while($lcfi<$lcfx) {
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; }
437 $lcfi = $lcfi + 1; }
438 return $catarray; }
440 function RePackCatFile($infiles, $outfile, $seekstart=0, $seekend=0, $checksumtype="crc32", $skipchecksum=False, $verbose=False) {
441 if(is_array($infile)) {
442 $listcatfiles = $infile; }
443 else {
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) {
450 return False; }
451 $lcfi = 0;
452 $lcfx = count($listcatfiles);
453 while($lcfi<$lcfx) {
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;
494 $Nullstrecd = "\0";
495 $catfileout = $catfileoutstrecd.$fcontents.$Nullstrecd;
496 fwrite($catfp, $catfileout); }
497 fclose($catfp);
498 return True; }
500 function UnPackCatFile($infile, $outdir=Null, $verbose=False, $skipchecksum=False) {
501 if($outdir!==Null) {
502 $outdir = RemoveWindowsPath($outdir); }
503 if(is_array($infile)) {
504 $listcatfiles = $infile; }
505 else {
506 $infile = RemoveWindowsPath($infile);
507 $listcatfiles = CatFileToArray($infile, 0, 0, False, $skipchecksum); }
508 if($listcatfiles===False) {
509 return False; }
510 $lcfi = 0;
511 $lcfx = count($listcatfiles);
512 while($lcfi<$lcfx) {
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']);
518 fclose($fpc);
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']); }
533 $lcfi = $lcfi + 1; }
534 return True; }
536 function CatFileListFiles($infile, $seekstart=0, $seekend=0, $verbose=False, $skipchecksum=False) {
537 if(is_array($infile)) {
538 $listcatfiles = $infile; }
539 else {
540 $infile = RemoveWindowsPath($infile);
541 $listcatfiles = CatFileToArray($infile, $seekstart, $seekend, True, $skipchecksum); }
542 if($listcatfiles===False) {
543 return False; }
544 $lcfi = 0;
545 $lcfx = count($listcatfiles);
546 $returnval = array();
547 while($lcfi<$lcfx) {
548 $returnval[$lcfi] = $listcatfiles[$lcfi]['fname'];
549 if($verbose===False) {
550 print($listcatfiles[$lcfi]['fname']."\n"); }
551 if($verbose===True) {
552 $permissionstr = "";
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"); }
588 $lcfi = $lcfi + 1; }
589 return True; }