Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable19] Add missing TarHeader.php #466

Merged
merged 1 commit into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2917,6 +2917,7 @@
'ZipStreamer\\UNIX' => $vendorDir . '/deepdiver/zipstreamer/src/ZipStreamer.php',
'ZipStreamer\\ZipStreamer' => $vendorDir . '/deepdiver/zipstreamer/src/ZipStreamer.php',
'bantu\\IniGetWrapper\\IniGetWrapper' => $vendorDir . '/bantu/ini-get-wrapper/src/IniGetWrapper.php',
'ownCloud\\TarStreamer\\TarHeader' => $vendorDir . '/deepdiver1975/tarstreamer/src/TarHeader.php',
'ownCloud\\TarStreamer\\TarStreamer' => $vendorDir . '/deepdiver1975/tarstreamer/src/TarStreamer.php',
'phpseclib\\Crypt\\AES' => $vendorDir . '/phpseclib/phpseclib/phpseclib/Crypt/AES.php',
'phpseclib\\Crypt\\Base' => $vendorDir . '/phpseclib/phpseclib/phpseclib/Crypt/Base.php',
Expand Down
1 change: 1 addition & 0 deletions composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -3437,6 +3437,7 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652
'ZipStreamer\\UNIX' => __DIR__ . '/..' . '/deepdiver/zipstreamer/src/ZipStreamer.php',
'ZipStreamer\\ZipStreamer' => __DIR__ . '/..' . '/deepdiver/zipstreamer/src/ZipStreamer.php',
'bantu\\IniGetWrapper\\IniGetWrapper' => __DIR__ . '/..' . '/bantu/ini-get-wrapper/src/IniGetWrapper.php',
'ownCloud\\TarStreamer\\TarHeader' => __DIR__ . '/..' . '/deepdiver1975/tarstreamer/src/TarHeader.php',
'ownCloud\\TarStreamer\\TarStreamer' => __DIR__ . '/..' . '/deepdiver1975/tarstreamer/src/TarStreamer.php',
'phpseclib\\Crypt\\AES' => __DIR__ . '/..' . '/phpseclib/phpseclib/phpseclib/Crypt/AES.php',
'phpseclib\\Crypt\\Base' => __DIR__ . '/..' . '/phpseclib/phpseclib/phpseclib/Crypt/Base.php',
Expand Down
141 changes: 141 additions & 0 deletions deepdiver1975/tarstreamer/src/TarHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace ownCloud\TarStreamer;

class TarHeader {
private $name = '';

private $mode = '777';

private $uid = '0';

private $gid = '0';

private $size;

private $mtime = '';

private $checksum;

private $typeflag;

private $linkname = '';

private $magic = 'ustar';

private $version = '00';

private $uname = '';

private $gname = '';

private $devmajor = '';

private $devminor = '';

private $prefix = '';

private $reserved = '';

public function setName($name){
$this->name = $name;
return $this;
}

public function setSize($size){
$this->size = $size;
return $this;
}

public function setMtime($mtime){
$this->mtime = $mtime;
return $this;
}

public function setTypeflag($typeflag){
$this->typeflag = $typeflag;
return $this;
}

public function setPrefix($prefix){
$this->prefix = $prefix;
return $this;
}

public function getHeader(){
$fields = [
['a100', substr($this->name, 0, 100)],
['a8', str_pad($this->mode, 7, '0', STR_PAD_LEFT)],
['a8', decoct(str_pad($this->uid, 7, '0', STR_PAD_LEFT))],
['a8', decoct(str_pad($this->gid, 7, '0', STR_PAD_LEFT))],
['a12', str_pad(decoct($this->size), 11, '0', STR_PAD_LEFT)],
['a12', str_pad(decoct($this->mtime), 11, '0', STR_PAD_LEFT)],
// We calculate checksum later
['a8', ''],
['a1', $this->typeflag],
['a100', $this->linkname],
['a6', $this->magic],
['a2', $this->version],
['a32', $this->uname],
['a32', $this->gname],
['a8', $this->devmajor],
['a8', $this->devminor],
['a155', substr($this->prefix, 0, 155)],
['a12', $this->reserved],
];

// pack fields and calculate "total" length
$header = $this->packFields($fields);

// Compute header checksum
$checksum = str_pad(decoct($this->computeUnsignedChecksum($header)), 6, "0", STR_PAD_LEFT);
for ($i = 0; $i < 6; $i++){
$header[(148 + $i)] = substr($checksum, $i, 1);
}
$header[154] = chr(0);
$header[155] = chr(32);

return $header;
}

/**
* Create a format string and argument list for pack(), then call pack() and return the result.
*
* @param array $fields key being the format string and value being the data to pack
* @return string binary packed data returned from pack()
*/
protected function packFields($fields){
list ($fmt, $args) = ['', []];

// populate format string and argument list
foreach ($fields as $field){
$fmt .= $field[0];
$args[] = $field[1];
}

// prepend format string to argument list
array_unshift($args, $fmt);

// build output string from header and compressed data
return call_user_func_array('pack', $args);
}

/**
* Generate unsigned checksum of header
*
* @param string $header
* @return string unsigned checksum
*/
protected function computeUnsignedChecksum($header){
$unsignedChecksum = 0;
for ($i = 0; $i < 512; $i++){
$unsignedChecksum += ord($header[$i]);
}
for ($i = 0; $i < 8; $i++){
$unsignedChecksum -= ord($header[148 + $i]);
}
$unsignedChecksum += ord(" ") * 8;

return $unsignedChecksum;
}
}