public function current() { $data = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $this->block); $file = array(); $file['name'] = trim($data['filename']); $file['mode'] = OctDec(trim($data['mode'])); $file['uid'] = OctDec(trim($data['uid'])); $file['gid'] = OctDec(trim($data['gid'])); $file['size'] = OctDec(trim($data['size'])); $file['mtime'] = OctDec(trim($data['mtime'])); $file['checksum'] = OctDec(trim($data['checksum'])); if ($file['checksum'] != $this->checksum($this->block)) { trigger_error('Bad tar format: ' . $this->filename, E_USER_ERROR); return false; } $file['type'] = $data['typeflag']; $file['offset'] = $this->offset + 512; $file['rs'] = $this->rs; $this->item = $file; $this->block = null; return $this->item; }
function PclTarHandleReadHeader($v_binary_data, &$v_header) { TrFctStart(__FILE__, __LINE__, "PclTarHandleReadHeader", ""); $v_result = 1; // ----- Read the 512 bytes header /* if ($p_tar_mode == "tar") $v_binary_data = fread($p_tar, 512); else $v_binary_data = gzread($p_tar, 512); */ // ----- Look for no more block if (strlen($v_binary_data) == 0) { $v_header[filename] = ""; $v_header[status] = "empty"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } // ----- Look for invalid block size if (strlen($v_binary_data) != 512) { $v_header[filename] = ""; $v_header[status] = "invalid_header"; TrFctMessage(__FILE__, __LINE__, 2, "Invalid block size : " . strlen($v_binary_data)); // ----- Error log PclErrorLog(-10, "Invalid block size : " . strlen($v_binary_data)); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156; $i < 512; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : {$v_checksum}"); // ----- Extract the values TrFctMessage(__FILE__, __LINE__, 2, "Header : '{$v_binary_data}'"); $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data); // ----- Extract the checksum for check $v_header[checksum] = OctDec(trim($v_data[checksum])); TrFctMessage(__FILE__, __LINE__, 3, "File checksum : {$v_header['checksum']}"); if ($v_header[checksum] != $v_checksum) { TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : {$v_checksum} calculated, {$v_header['checksum']} expected"); $v_header[filename] = ""; $v_header[status] = "invalid_header"; // ----- Look for last block (empty block) if ($v_checksum == 256 && $v_header[checksum] == 0) { $v_header[status] = "empty"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } // ----- Error log PclErrorLog(-13, "Invalid checksum : {$v_checksum} calculated, {$v_header['checksum']} expected"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ({$v_checksum})"); // ----- Extract the properties $v_header[filename] = trim($v_data[filename]); TrFctMessage(__FILE__, __LINE__, 2, "Name : '{$v_header['filename']}'"); $v_header[mode] = OctDec(trim($v_data[mode])); TrFctMessage(__FILE__, __LINE__, 2, "Mode : '" . DecOct($v_header[mode]) . "'"); $v_header[uid] = OctDec(trim($v_data[uid])); TrFctMessage(__FILE__, __LINE__, 2, "Uid : '{$v_header['uid']}'"); $v_header[gid] = OctDec(trim($v_data[gid])); TrFctMessage(__FILE__, __LINE__, 2, "Gid : '{$v_header['gid']}'"); $v_header[size] = OctDec(trim($v_data[size])); TrFctMessage(__FILE__, __LINE__, 2, "Size : '{$v_header['size']}'"); $v_header[mtime] = OctDec(trim($v_data[mtime])); TrFctMessage(__FILE__, __LINE__, 2, "Date : " . date("l dS of F Y h:i:s A", $v_header[mtime])); if (($v_header[typeflag] = $v_data[typeflag]) == "5") { $v_header[size] = 0; TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '{$v_header['size']}'"); } TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : {$v_header['typeflag']}"); /* ----- All these fields are removed form the header because they do not carry interesting info $v_header[link] = trim($v_data[link]); TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]"); $v_header[magic] = trim($v_data[magic]); TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]"); $v_header[version] = trim($v_data[version]); TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]"); $v_header[uname] = trim($v_data[uname]); TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]"); $v_header[gname] = trim($v_data[gname]); TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]"); $v_header[devmajor] = trim($v_data[devmajor]); TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]"); $v_header[devminor] = trim($v_data[devminor]); TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]"); */ // ----- Set the status field $v_header[status] = "ok"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
function _readHeader($p_dat) { if (!$p_dat || strlen($p_dat) != 512) { return false; } for ($i = 0, $chks = 0; $i < 148; $i++) { $chks += ord($p_dat[$i]); } for ($i = 156, $chks += 256; $i < 512; $i++) { $chks += ord($p_dat[$i]); } $headers = @unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $p_dat); if (!$headers) { return false; } $return['checksum'] = OctDec(trim($headers['checksum'])); if ($return['checksum'] != $chks) { return false; } $return['filename'] = trim($headers['filename']); $return['mode'] = OctDec(trim($headers['mode'])); $return['uid'] = OctDec(trim($headers['uid'])); $return['gid'] = OctDec(trim($headers['gid'])); $return['size'] = OctDec(trim($headers['size'])); $return['mtime'] = OctDec(trim($headers['mtime'])); $return['typeflag'] = $headers['typeflag']; $return['link'] = trim($headers['link']); $return['uname'] = trim($headers['uname']); $return['gname'] = trim($headers['gname']); return $return; }
function readHeader($Long = false) { $str = ''; while (trim($str) == '') { if (!($l = self::strlen($str = $this->readBlock($bIgnoreOpenNextError = true)))) { return 0; } // finish } if (!$Long) { $this->BlockHeader = $this->Block - 1; } if ($l != 512) { return $this->Error('Wrong block size: ' . self::strlen($str) . ' (block ' . $this->Block . ')'); } $data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $str); $chk = $data['devmajor'] . $data['devminor']; if (!is_numeric(trim($data['checksum'])) || $chk != '' && $chk != 0) { return $this->Error('Archive is corrupted, wrong block: ' . ($this->Block - 1)); } $header['filename'] = trim($data['prefix'] . '/' . $data['filename'], '/'); $header['mode'] = OctDec($data['mode']); $header['uid'] = OctDec($data['uid']); $header['gid'] = OctDec($data['gid']); $header['size'] = OctDec($data['size']); $header['mtime'] = OctDec($data['mtime']); $header['type'] = $data['type']; // $header['link'] = $data['link']; if (self::strpos($header['filename'], './') === 0) { $header['filename'] = self::substr($header['filename'], 2); } if ($header['type'] == 'L') { $n = ceil($header['size'] / 512); for ($i = 0; $i < $n; $i++) { $filename .= $this->readBlock(); } if (!is_array($header = $this->readHeader($Long = true))) { return $this->Error('Wrong long header, block: ' . $this->Block); } $header['filename'] = self::substr($filename, 0, self::strpos($filename, chr(0))); } if (self::strpos($header['filename'], '/') === 0) { // trailing slash $header['type'] = 5; } // Directory if ($header['type'] == '5') { $header['size'] = ''; } if ($header['filename'] == '') { return $this->Error('Filename is empty, wrong block: ' . ($this->Block - 1)); } if (!$this->checkCRC($str, $data)) { return $this->Error('Checksum error on file: ' . $header['filename']); } $this->header = $header; return $header; }
function read_tar() { $filename = $this->tarfile_path_name; if ($filename == "") { $this->error = 'No filename specified when attempting to read a tar file'; return array(); } if (!file_exists($filename)) { $this->error = 'Cannot locate the file ' . $filename; return array(); } $tar_info = array(); $this->tar_filename = $filename; // Open up the tar file and start the loop if (!($FH = fopen($this->tarfile_wrap . $filename, 'rb'))) { $this->error = "Cannot open {$filename} for reading"; return array(); } // Grrr, perl allows spaces, PHP doesn't. Pack strings are hard to read without // them, so to save my sanity, I'll create them with spaces and remove them here $this->tar_unpack_header = preg_replace("/\\s/", "", $this->tar_unpack_header); while (!feof($FH)) { $buffer = fread($FH, $this->tar_header_length); // check the block $checksum = 0; for ($i = 0; $i < 148; $i++) { $checksum += ord(substr($buffer, $i, 1)); } for ($i = 148; $i < 156; $i++) { $checksum += ord(' '); } for ($i = 156; $i < 512; $i++) { $checksum += ord(substr($buffer, $i, 1)); } $fa = unpack($this->tar_unpack_header, $buffer); $name = trim($fa['filename']); $mode = OctDec(trim($fa['mode'])); $uid = OctDec(trim($fa['uid'])); $gid = OctDec(trim($fa['gid'])); $size = OctDec(trim($fa['size'])); $mtime = OctDec(trim($fa['mtime'])); $chksum = OctDec(trim($fa['chksum'])); $typeflag = trim($fa['typeflag']); $linkname = trim($fa['linkname']); $magic = trim($fa['magic']); $version = trim($fa['version']); $uname = trim($fa['uname']); $gname = trim($fa['gname']); $devmajor = OctDec(trim($fa['devmajor'])); $devminor = OctDec(trim($fa['devminor'])); // Mod: Added if if (isset($fa['prefix'])) { $prefix = trim($fa['prefix']); } else { $prefix = ''; } if ($checksum == 256 && $chksum == 0) { //EOF! break; } // Mod: Added empty if (!empty($prefix)) { $name = $prefix . '/' . $name; } // Some broken tars don't set the type flag // correctly for directories, so we assume that // if it ends in / it's a directory... if (preg_match("#/\$#", $name) and !$name) { $typeflag = 5; } // If it's the end of the tarball... $test = $this->internal_build_string('\\0', 512); if ($buffer == $test) { break; } // Read the next chunk $data = fread($FH, $size); if (strlen($data) != $size) { $this->error = "Read error on tar file"; fclose($FH); return array(); } $diff = $size % 512; if ($diff != 0) { // Padding, throw away $crap = fread($FH, 512 - $diff); } // Protect against tarfiles with garbage at the end if ($name == "") { break; } $tar_info[] = array('name' => $name, 'mode' => $mode, 'uid' => $uid, 'gid' => $gid, 'size' => $size, 'mtime' => $mtime, 'chksum' => $chksum, 'typeflag' => $typeflag, 'linkname' => $linkname, 'magic' => $magic, 'version' => $version, 'uname' => $uname, 'gname' => $gname, 'devmajor' => $devmajor, 'devminor' => $devminor, 'prefix' => $prefix, 'data' => $data); } fclose($FH); return $tar_info; }
function _readHeader($v_binary_data, &$v_header) { if ((extension_loaded("mbstring") ? mb_strlen($v_binary_data, "latin1") : strlen($v_binary_data)) == 0) { $v_header['filename'] = ''; return true; } if ((extension_loaded("mbstring") ? mb_strlen($v_binary_data, "latin1") : strlen($v_binary_data)) != 512) { $v_header['filename'] = ''; $this->_arErrors[] = array("INV_BLOCK_SIZE", "Invalid block size : " . strlen($v_binary_data) . ""); return false; } $chars = count_chars(substr($v_binary_data, 0, 148) . ' ' . substr($v_binary_data, 156, 356)); $v_checksum = 0; foreach ($chars as $ch => $cnt) { $v_checksum += $ch * $cnt; } $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix/a12temp", $v_binary_data); $v_header['checksum'] = OctDec(trim($v_data['checksum'])); if ($v_header['checksum'] != $v_checksum) { $v_header['filename'] = ''; if ($v_checksum == 256 && $v_header['checksum'] == 0) { return true; } $this->_arErrors[] = array("INV_BLOCK_CHECK", "Invalid checksum for file '" . $v_data['filename'] . "' : " . $v_checksum . " calculated, " . $v_header['checksum'] . " expected"); return false; } // ----- Extract the properties $v_header['filename'] = trim($v_data['prefix'], "") . "/" . trim($v_data['filename'], ""); $v_header['mode'] = OctDec(trim($v_data['mode'])); $v_header['uid'] = OctDec(trim($v_data['uid'])); $v_header['gid'] = OctDec(trim($v_data['gid'])); $v_header['size'] = OctDec(trim($v_data['size'])); $v_header['mtime'] = OctDec(trim($v_data['mtime'])); if (($v_header['typeflag'] = $v_data['typeflag']) == "5") { $v_header['size'] = 0; } return true; }
private function _readHeader($v_binary_data, &$v_header) { if ((self::$bMbstring ? mb_strlen($v_binary_data, "latin1") : strlen($v_binary_data)) == 0) { $v_header['filename'] = ''; return true; } if ((self::$bMbstring ? mb_strlen($v_binary_data, "latin1") : strlen($v_binary_data)) != 512) { $v_header['filename'] = ''; $this->_arErrors[] = array("INV_BLOCK_SIZE", str_replace("#BLOCK_SIZE#", self::$bMbstring ? mb_strlen($v_binary_data, "latin1") : strlen($v_binary_data), GetMessage("MAIN_ARCHIVE_INV_BLOCK_SIZE"))); return false; } $v_checksum = 0; for ($i = 0; $i < 148; $i++) { $v_checksum += ord(self::$bMbstring ? mb_substr($v_binary_data, $i, 1, "latin1") : substr($v_binary_data, $i, 1)); } for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } for ($i = 156; $i < 512; $i++) { $v_checksum += ord(self::$bMbstring ? mb_substr($v_binary_data, $i, 1, "latin1") : substr($v_binary_data, $i, 1)); } //changed $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a131prefix", $v_binary_data); $v_header['checksum'] = OctDec(trim($v_data['checksum'])); if ($v_header['checksum'] != $v_checksum) { $v_header['filename'] = ''; if ($v_checksum == 256 && $v_header['checksum'] == 0) { return true; } $this->_arErrors[] = array("INV_BLOCK_CHECK", str_replace(array("#FILE_NAME#", "#CHECKSUM#", "#EXP_CHECKSUM#"), array(removeDocRoot($this->io->GetLogicalName($v_data['filename'])), $v_checksum, $v_header['checksum']), GetMessage("MAIN_ARCHIVE_INV_BLOCK_CHECK"))); return false; } // ----- Extract the properties $v_header['filename'] = trim($v_data['prefix'] . "/" . $v_data['filename']); $v_header['mode'] = OctDec(trim($v_data['mode'])); $v_header['uid'] = OctDec(trim($v_data['uid'])); $v_header['gid'] = OctDec(trim($v_data['gid'])); $v_header['size'] = OctDec(trim($v_data['size'])); $v_header['mtime'] = OctDec(trim($v_data['mtime'])); if (($v_header['typeflag'] = $v_data['typeflag']) == "5") { $v_header['size'] = 0; } return true; }
function _readHeader($v_binary_data, &$v_header) { if (strlen($v_binary_data)==0) { $v_header['filename'] = ''; return true; } if (strlen($v_binary_data) != 512) { $v_header['filename'] = ''; $this->_arErrors[] = array("INV_BLOCK_SIZE", "Invalid block size : ".strlen($v_binary_data).""); return false; } $v_checksum = 0; for ($i = 0; $i < 148; $i++) $v_checksum+=ord(substr($v_binary_data, $i, 1)); for ($i = 148; $i < 156; $i++) $v_checksum += ord(' '); for ($i = 156; $i < 512; $i++) $v_checksum+=ord(substr($v_binary_data, $i, 1)); $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data); $v_header['checksum'] = OctDec(trim($v_data['checksum'])); if ($v_header['checksum'] != $v_checksum) { $v_header['filename'] = ''; if (($v_checksum == 256) && ($v_header['checksum'] == 0)) return true; $this->_arErrors[] = array("INV_BLOCK_CHECK", "Invalid checksum for file '".$v_data['filename']."' : ".$v_checksum." calculated, ".$v_header['checksum']." expected"); return false; } // ----- Extract the properties $v_header['filename'] = trim($v_data['filename']); $v_header['mode'] = OctDec(trim($v_data['mode'])); $v_header['uid'] = OctDec(trim($v_data['uid'])); $v_header['gid'] = OctDec(trim($v_data['gid'])); $v_header['size'] = OctDec(trim($v_data['size'])); $v_header['mtime'] = OctDec(trim($v_data['mtime'])); if (($v_header['typeflag'] = $v_data['typeflag']) == "5") $v_header['size'] = 0; return true; }
/** * Decode the given tar file header * * @param string $block a 512 byte block containign the header data * @return false|array */ protected function parseHeader($block) { if (!$block || strlen($block) != 512) { return false; } for ($i = 0, $chks = 0; $i < 148; $i++) { $chks += ord($block[$i]); } for ($i = 156, $chks += 256; $i < 512; $i++) { $chks += ord($block[$i]); } $header = @unpack("a100filename/a8perm/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $block); if (!$header) { return false; } $return = array(); $return['checksum'] = OctDec(trim($header['checksum'])); if ($return['checksum'] != $chks) { return false; } $return['filename'] = trim($header['filename']); $return['perm'] = OctDec(trim($header['perm'])); $return['uid'] = OctDec(trim($header['uid'])); $return['gid'] = OctDec(trim($header['gid'])); $return['size'] = OctDec(trim($header['size'])); $return['mtime'] = OctDec(trim($header['mtime'])); $return['typeflag'] = $header['typeflag']; $return['link'] = trim($header['link']); $return['uname'] = trim($header['uname']); $return['gname'] = trim($header['gname']); // Handle ustar Posix compliant path prefixes if (trim($header['prefix'])) { $return['filename'] = trim($header['prefix']) . '/' . $return['filename']; } // Handle Long-Link entries from GNU Tar if ($return['typeflag'] == 'L') { // following data block(s) is the filename $filename = trim($this->readbytes(ceil($header['size'] / 512) * 512)); // next block is the real header $block = $this->readbytes(512); $return = $this->parseHeader($block); // overwrite the filename $return['filename'] = $filename; } return $return; }
/** * Convert Tar record size to actual size * * @param string $tar_size * @return size of tar record in bytes */ private function _tarRecToSize($tar_size) { /* * First byte of size has a special meaning if bit 7 is set. * * Bit 7 indicates base-256 encoding if set. * Bit 6 is the sign bit. * Bits 5:0 are most significant value bits. */ $ch = ord($tar_size[0]); if ($ch & 0x80) { // Full 12-bytes record is required. $rec_str = $tar_size . ""; $size = $ch & 0x40 ? -1 : 0; $size = $size << 6 | $ch & 0x3f; for ($num_ch = 1; $num_ch < 12; ++$num_ch) { $size = $size * 256 + ord($rec_str[$num_ch]); } return $size; } else { return OctDec(trim($tar_size)); } }
function readHeader($Long = false) { $str = ''; while(trim($str) == '') if (!strlen($str = $this->readBlock())) return 0; // finish if (!$Long) $this->BlockHeader = $this->Block - 1; if (strlen($str)!=512) return $this->Error('TAR_WRONG_BLOCK_SIZE',$this->Block.' ('.strlen($str).')'); $data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $str); $chk = $data['devmajor'].$data['devminor']; if (!is_numeric(trim($data['checksum'])) || $chk!='' && $chk!=0) return $this->Error('TAR_ERR_FORMAT',($this->Block-1).'<hr>Header: <br>'.htmlspecialchars($str)); // быстрая проверка $header['filename'] = trim($data['prefix'].'/'.$data['filename'],'/'); $header['mode'] = OctDec($data['mode']); $header['uid'] = OctDec($data['uid']); $header['gid'] = OctDec($data['gid']); $header['size'] = OctDec($data['size']); $header['mtime'] = OctDec($data['mtime']); $header['type'] = $data['type']; // $header['link'] = $data['link']; if (strpos($header['filename'],'./')===0) $header['filename'] = substr($header['filename'],2); if ($header['type']=='L') // Long header { $n = ceil($header['size']/512); for ($i = 0; $i < $n; $i++) $filename .= $this->readBlock(); $header = $this->readHeader($Long = true); $header['filename'] = substr($filename,0,strpos($filename,chr(0))); } if (substr($header['filename'],-1)=='/') // trailing slash $header['type'] = 5; // Directory if ($header['type']=='5') $header['size'] = ''; if ($header['filename']=='') return $this->Error('TAR_EMPTY_FILE',($this->Block-1)); if (!$this->checkCRC($str, $data)) return $this->Error('TAR_ERR_CRC',htmlspecialchars($header['filename'])); $this->header = $header; return $header; }
function read_tar() { $filename = $this->filename; if ($filename == "") { //$this->error = awcsarcade_funcs::get_word('no_tar_to_read'); return array(); } if (!file_exists($filename)) { //$this->error = awcsarcade_funcs::get_word('can_not_locate_file', '$filename', $filename); return array(); } $tar_info = array(); $this->tar_filename = $filename; // Open up the tar file and start the loop if (!($FH = fopen($filename, 'rb'))) { //$this->error = awcsarcade_funcs::get_word('can_not_read_tar', '$filename', $filename); return array(); } // Grrr, perl allows spaces, PHP doesn't. Pack strings are hard to read without // them, so to save my sanity, I'll create them with spaces and remove them here $this->tar_unpack_header = preg_replace("/\\s/", "", $this->tar_unpack_header); while (!feof($FH)) { $buffer = fread($FH, $this->tar_header_length); // check the block $checksum = 0; for ($i = 0; $i < 148; $i++) { $checksum += ord(substr($buffer, $i, 1)); } for ($i = 148; $i < 156; $i++) { $checksum += ord(' '); } for ($i = 156; $i < 512; $i++) { $checksum += ord(substr($buffer, $i, 1)); } $fa = unpack($this->tar_unpack_header, $buffer); $name = trim($fa[filename]); $mode = OctDec(trim($fa[mode])); $uid = OctDec(trim($fa[uid])); $gid = OctDec(trim($fa[gid])); $size = OctDec(trim($fa[size])); $mtime = OctDec(trim($fa[mtime])); $chksum = OctDec(trim($fa[chksum])); $typeflag = trim($fa[typeflag]); $linkname = trim($fa[linkname]); $magic = trim($fa[magic]); $version = trim($fa[version]); $uname = trim($fa[uname]); $gname = trim($fa[gname]); $devmajor = OctDec(trim($fa[devmajor])); $devminor = OctDec(trim($fa[devminor])); $prefix = trim($fa[prefix]); if ($checksum == 256 && $chksum == 0) { //EOF! break; } if ($prefix) { $name = $prefix . '/' . $name; } // Some broken tars don't set the type flag // correctly for directories, so we assume that // if it ends in / it's a directory... if (preg_match("#/\$#", $name) and !$name) { $typeflag = 5; } // If it's the end of the tarball... $test = $this->internal_build_string('\\0', 512); if ($buffer == $test) { break; } // Read the next chunk $data = fread($FH, $size); if (strlen($data) != $size) { $this->error = awcsarcade_funcs::get_word('read_error_on_tar'); fclose($FH); return array(); } $diff = $size % 512; if ($diff != 0) { // Padding, throw away $crap = fread($FH, 512 - $diff); } // Protect against tarfiles with garbage at the end if ($name == "") { break; } $tar_info[] = array('name' => $name, 'mode' => $mode, 'uid' => $uid, 'gid' => $gid, 'size' => $size, 'mtime' => $mtime, 'chksum' => $chksum, 'typeflag' => $typeflag, 'linkname' => $linkname, 'magic' => $magic, 'version' => $version, 'uname' => $uname, 'gname' => $gname, 'devmajor' => $devmajor, 'devminor' => $devminor, 'prefix' => $prefix, 'data' => $data); } fclose($FH); die(print_r($tar_info)); return $tar_info; }
/** * Read a tar file header block. * * @param $block * @param array $header * @return array * @throws \BackupMigrate\Core\Exception\BackupMigrateException */ private function readHeader($block, $header = []) { if (strlen($block) == 0) { $header['filename'] = ''; return true; } if (strlen($block) != 512) { $header['filename'] = ''; throw new BackupMigrateException('Invalid block size: %size bytes', ['%size' => strlen($block)]); } if (!is_array($header)) { $header = array(); } // Calculate the checksum $checksum = 0; // First part of the header for ($i = 0; $i < 148; $i++) { $checksum += ord(substr($block, $i, 1)); } // Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $checksum += ord(' '); } // Last part of the header for ($i = 156; $i < 512; $i++) { $checksum += ord(substr($block, $i, 1)); } if (version_compare(PHP_VERSION, "5.5.0-dev") < 0) { $fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" . "a8checksum/a1typeflag/a100link/a6magic/a2version/" . "a32uname/a32gname/a8devmajor/a8devminor/a131prefix"; } else { $fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" . "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" . "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix"; } $data = unpack($fmt, $block); if (strlen($data["prefix"]) > 0) { $data["filename"] = "{$data['prefix']}/{$data['filename']}"; } // Extract the checksum $header['checksum'] = OctDec(trim($data['checksum'])); if ($header['checksum'] != $checksum) { $header['filename'] = ''; // Look for last block (empty block) if ($checksum == 256 && $header['checksum'] == 0) { return $header; } throw new BackupMigrateException('Invalid checksum for file %filename', ['%filename' => $data['filename']]); } // Extract the properties $header['filename'] = rtrim($data['filename'], ""); $header['mode'] = OctDec(trim($data['mode'])); $header['uid'] = OctDec(trim($data['uid'])); $header['gid'] = OctDec(trim($data['gid'])); $header['size'] = OctDec(trim($data['size'])); $header['mtime'] = OctDec(trim($data['mtime'])); if (($header['typeflag'] = $data['typeflag']) == "5") { $header['size'] = 0; } $header['link'] = trim($data['link']); // Look for long filename if ($header['typeflag'] == 'L') { $header = $this->readLongHeader($header); } return $header; }
function readHeader($binaryData, &$header) { if (strlen($binaryData) == 0) { $header['filename'] = ''; return true; } if (strlen($binaryData) != 512) { $header['filename'] = ''; $this->__('Invalid block size') . ': ' . strlen($binaryData); return false; } $checksum = 0; for ($i = 0; $i < 148; $i++) { $checksum += ord(substr($binaryData, $i, 1)); } for ($i = 148; $i < 156; $i++) { $checksum += ord(' '); } for ($i = 156; $i < 512; $i++) { $checksum += ord(substr($binaryData, $i, 1)); } $unpack_data = unpack('a100filename/a8mode/a8user_id/a8group_id/a12size/a12time/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $binaryData); $header['checksum'] = OctDec(trim($unpack_data['checksum'])); if ($header['checksum'] != $checksum) { $header['filename'] = ''; if ($checksum == 256 && $header['checksum'] == 0) { return true; } $this->errors[] = __('Error checksum for file ') . $unpack_data['filename']; return false; } if (($header['typeflag'] = $unpack_data['typeflag']) == '5') { $header['size'] = 0; } $header['filename'] = trim($unpack_data['filename']); $header['mode'] = OctDec(trim($unpack_data['mode'])); $header['user_id'] = OctDec(trim($unpack_data['user_id'])); $header['group_id'] = OctDec(trim($unpack_data['group_id'])); $header['size'] = OctDec(trim($unpack_data['size'])); $header['time'] = OctDec(trim($unpack_data['time'])); return true; }
function readHeader($binaryData) { // default header $header = array(); $header['filename'] = ""; $header['status'] = "empty"; // ----- Look for no more block if (strlen($binaryData) == 0) { return $header; } // ----- Look for invalid block size $blockSize = strlen($binaryData); if ($blockSize != 512) { throw new Exception("Invalid block size: {$blockSize}."); } // ----- Calculate the checksum $checksum = $this->getChecksum($binaryData); // ----- Extract the values $data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $binaryData); // ----- Extract the checksum for check $header["checksum"] = OctDec(trim($data["checksum"])); // ----- Look for last block (empty block) if ($checksum == 256 && $header["checksum"] == 0) { return $header; } // Checksum validation if ($header["checksum"] != $checksum) { throw new Exception("File checksum is invalid: {$checksum} calculated, {$header['checksum']} expected."); } // ----- Extract the properties $header["filename"] = trim($data["filename"]); $header["mode"] = OctDec(trim($data["mode"])); $header["uid"] = OctDec(trim($data["uid"])); $header["gid"] = OctDec(trim($data["gid"])); $header["size"] = OctDec(trim($data["size"])); $header["mtime"] = OctDec(trim($data["mtime"])); // check if type is directory if (($header["typeflag"] = $data["typeflag"]) == "5") { $header["size"] = 0; } // ----- Set the status field $header["status"] = "ok"; // ----- Return return $header; }
function _readHeader($v_binary_data, &$v_header) { if (strlen($v_binary_data) == 0) { $v_header['filename'] = ''; return true; } if (strlen($v_binary_data) != 512) { $v_header['filename'] = ''; $this->_error('Invalid block size : ' . strlen($v_binary_data)); return false; } if (!is_array($v_header)) { $v_header = array(); } // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156; $i < 512; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } if (version_compare(PHP_VERSION, "5.5.0-dev") < 0) { $fmt = "a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" . "a8checksum/a1typeflag/a100link/a6magic/a2version/" . "a32uname/a32gname/a8devmajor/a8devminor/a131prefix"; } else { $fmt = "Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/" . "Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/" . "Z32uname/Z32gname/Z8devmajor/Z8devminor/Z131prefix"; } $v_data = unpack($fmt, $v_binary_data); if (strlen($v_data["prefix"]) > 0) { $v_data["filename"] = "{$v_data['prefix']}/{$v_data['filename']}"; } // ----- Extract the checksum $v_header['checksum'] = OctDec(trim($v_data['checksum'])); if ($v_header['checksum'] != $v_checksum) { $v_header['filename'] = ''; // ----- Look for last block (empty block) if ($v_checksum == 256 && $v_header['checksum'] == 0) { return true; } $this->_error('Invalid checksum for file "' . $v_data['filename'] . '" : ' . $v_checksum . ' calculated, ' . $v_header['checksum'] . ' expected'); return false; } // ----- Extract the properties $v_header['filename'] = $v_data['filename']; if ($this->_maliciousFilename($v_header['filename'])) { $this->_error('Malicious .tar detected, file "' . $v_header['filename'] . '" will not install in desired directory tree'); return false; } $v_header['mode'] = OctDec(trim($v_data['mode'])); $v_header['uid'] = OctDec(trim($v_data['uid'])); $v_header['gid'] = OctDec(trim($v_data['gid'])); $v_header['size'] = OctDec(trim($v_data['size'])); $v_header['mtime'] = OctDec(trim($v_data['mtime'])); if (($v_header['typeflag'] = $v_data['typeflag']) == "5") { $v_header['size'] = 0; } $v_header['link'] = trim($v_data['link']); /* ----- All these fields are removed form the header because they do not carry interesting info $v_header[magic] = trim($v_data[magic]); $v_header[version] = trim($v_data[version]); $v_header[uname] = trim($v_data[uname]); $v_header[gname] = trim($v_data[gname]); $v_header[devmajor] = trim($v_data[devmajor]); $v_header[devminor] = trim($v_data[devminor]); */ return true; }
function _readHeader($v_binary_data, &$v_header) { if (strlen($v_binary_data) == 0) { $v_header['filename'] = ''; return true; } if (strlen($v_binary_data) != 512) { $v_header['filename'] = ''; $this->_error('Invalid block size : ' . strlen($v_binary_data)); return false; } // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156; $i < 512; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/" . "a8checksum/a1typeflag/a100link/a6magic/a2version/" . "a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data); // ----- Extract the checksum $v_header['checksum'] = OctDec(trim($v_data['checksum'])); if ($v_header['checksum'] != $v_checksum) { $v_header['filename'] = ''; // ----- Look for last block (empty block) if ($v_checksum == 256 && $v_header['checksum'] == 0) { return true; } $this->_error('Invalid checksum for file "' . $v_data['filename'] . '" : ' . $v_checksum . ' calculated, ' . $v_header['checksum'] . ' expected'); return false; } // ----- Extract the properties $v_header['filename'] = trim($v_data['filename']); $v_header['mode'] = OctDec(trim($v_data['mode'])); $v_header['uid'] = OctDec(trim($v_data['uid'])); $v_header['gid'] = OctDec(trim($v_data['gid'])); $v_header['size'] = OctDec(trim($v_data['size'])); $v_header['mtime'] = OctDec(trim($v_data['mtime'])); if (($v_header['typeflag'] = $v_data['typeflag']) == "5") { $v_header['size'] = 0; } /* ----- All these fields are removed form the header because they do not carry interesting info $v_header[link] = trim($v_data[link]); $v_header[magic] = trim($v_data[magic]); $v_header[version] = trim($v_data[version]); $v_header[uname] = trim($v_data[uname]); $v_header[gname] = trim($v_data[gname]); $v_header[devmajor] = trim($v_data[devmajor]); $v_header[devminor] = trim($v_data[devminor]); */ return true; }
function PclTarHandleReadHeader($v_binary_data, &$v_header) { TrFctStart(__FILE__, __LINE__, "PclTarHandleReadHeader", ""); $v_result = 1; if (strlen($v_binary_data) == 0) { $v_header[filename] = ""; $v_header[status] = "empty"; TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } if (strlen($v_binary_data) != 512) { $v_header[filename] = ""; $v_header[status] = "invalid_header"; TrFctMessage(__FILE__, __LINE__, 2, "Invalid block size : " . strlen($v_binary_data)); PclErrorLog(-10, "Invalid block size : " . strlen($v_binary_data)); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } $v_checksum = 0; for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } for ($i = 156; $i < 512; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : {$v_checksum}"); TrFctMessage(__FILE__, __LINE__, 2, "Header : '{$v_binary_data}'"); $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data); $v_header[checksum] = OctDec(trim($v_data[checksum])); TrFctMessage(__FILE__, __LINE__, 3, "File checksum : {$v_header['checksum']}"); if ($v_header[checksum] != $v_checksum) { TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : {$v_checksum} calculated, {$v_header['checksum']} expected"); $v_header[filename] = ""; $v_header[status] = "invalid_header"; if ($v_checksum == 256 && $v_header[checksum] == 0) { $v_header[status] = "empty"; TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } PclErrorLog(-13, "Invalid checksum : {$v_checksum} calculated, {$v_header['checksum']} expected"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ({$v_checksum})"); $v_header[filename] = trim($v_data[filename]); TrFctMessage(__FILE__, __LINE__, 2, "Name : '{$v_header['filename']}'"); $v_header[mode] = OctDec(trim($v_data[mode])); TrFctMessage(__FILE__, __LINE__, 2, "Mode : '" . DecOct($v_header[mode]) . "'"); $v_header[uid] = OctDec(trim($v_data[uid])); TrFctMessage(__FILE__, __LINE__, 2, "Uid : '{$v_header['uid']}'"); $v_header[gid] = OctDec(trim($v_data[gid])); TrFctMessage(__FILE__, __LINE__, 2, "Gid : '{$v_header['gid']}'"); $v_header[size] = OctDec(trim($v_data[size])); TrFctMessage(__FILE__, __LINE__, 2, "Size : '{$v_header['size']}'"); $v_header[mtime] = OctDec(trim($v_data[mtime])); TrFctMessage(__FILE__, __LINE__, 2, "Date : " . date("l dS of F Y h:i:s A", $v_header[mtime])); if (($v_header[typeflag] = $v_data[typeflag]) == "5") { $v_header[size] = 0; TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '{$v_header['size']}'"); } TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : {$v_header['typeflag']}"); $v_header[status] = "ok"; TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }