You are here

public function ArchiveTar::_readHeader in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Archiver/ArchiveTar.php \Drupal\Core\Archiver\ArchiveTar::_readHeader()

Parameters

mixed $v_binary_data:

mixed $v_header:

Return value

bool

3 calls to ArchiveTar::_readHeader()
ArchiveTar::_extractInString in core/lib/Drupal/Core/Archiver/ArchiveTar.php
This method extract from the archive one file identified by $p_filename. The return value is a string with the file content, or null on error.
ArchiveTar::_extractList in core/lib/Drupal/Core/Archiver/ArchiveTar.php
ArchiveTar::_readLongHeader in core/lib/Drupal/Core/Archiver/ArchiveTar.php

File

core/lib/Drupal/Core/Archiver/ArchiveTar.php, line 1701

Class

ArchiveTar

Namespace

Drupal\Core\Archiver

Code

public 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'] = rtrim($v_data['filename'], "\0");
  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;
}