Skip to content

Commit

Permalink
Merge pull request #32 from steffunky/fix-file-size-is-a-multiple-of-…
Browse files Browse the repository at this point in the history
…buffer

Fix block padding when the file buffer length is a multiple of 512 and smaller than Archive_Tar buffer length
  • Loading branch information
mrook authored Sep 15, 2020
2 parents 716aacb + a1ff994 commit e47f8c2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Archive/Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ public function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $v_
while (($v_buffer = fread($v_file, $this->buffer_length)) != '') {
$buffer_length = strlen("$v_buffer");
if ($buffer_length != $this->buffer_length) {
$pack_size = ((int)($buffer_length / 512) + 1) * 512;
$pack_size = ((int)($buffer_length / 512) + ($buffer_length % 512 !== 0 ? 1 : 0)) * 512;
$pack_format = sprintf('a%d', $pack_size);
} else {
$pack_format = sprintf('a%d', $this->buffer_length);
Expand Down
33 changes: 33 additions & 0 deletions tests/512nbytesfile.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
test file size that happens to be 512 * n bytes
--SKIPIF--
--FILE--
<?php
$dirname = dirname(__FILE__);
require_once $dirname . '/setup.php.inc';

$tar = new Archive_Tar($dirname . '/512nbytesfile.tar.gz', null, 2048);
$tar->add($dirname .'/testblock3');
$tar->listContent();
$phpunit->assertNoErrors('after tar archive listing');

$returnval = shell_exec('tar -Ptf ' . $dirname . '/512nbytesfile.tar.gz | sort');
$phpunit->assertNoErrors('after shell tar listing');

$expectedvalue =
<<< EOD
$dirname/testblock3
$dirname/testblock3/1024bytes.txt
$dirname/testblock3/randombytes.txt
EOD;
$phpunit->assertEquals($expectedvalue, $returnval, 'wrong output for shell tar verification');

echo 'test done'
?>
--CLEAN--
<?php
$dirname = dirname(__FILE__);
@unlink($dirname.'/512nbytesfile.tar.gz');
?>
--EXPECT--
test done
16 changes: 16 additions & 0 deletions tests/testblock3/1024bytes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
194 210 54 166 74 179 115 216 109 21 220 29 144 242 90 206
240 73 172 14 153 163 43 16 233 20 186 91 95 150 60 61
180 187 233 126 130 154 116 206 213 231 202 148 148 12 125 24
6 139 32 235 117 240 56 96 23 1 182 167 116 64 92 73
250 177 157 119 121 148 112 50 89 24 37 214 81 88 201 75
162 108 60 114 137 227 170 186 231 213 177 33 44 143 243 98
14 28 226 77 152 241 243 62 103 237 187 235 227 73 9 119
162 64 127 74 73 90 112 200 39 6 173 251 12 191 166 18
235 100 74 184 126 153 23 118 27 25 128 222 234 246 165 159
170 127 236 199 91 67 96 185 108 159 172 2 245 202 161 177
147 188 0 151 251 230 251 100 226 67 212 38 72 32 44 117
10 39 163 159 0 197 160 225 25 177 16 0 240 16 137 105
187 132 129 98 24 210 169 33 246 77 217 145 242 118 181 47
160 61 62 129 71 52 253 85 2 215 174 241 55 32 236 27
160 120 145 188 132 37 161 178 45 227 209 57 142 19 88 175
230 238 104 128 26 196 126 118 214 122 201 123
2 changes: 2 additions & 0 deletions tests/testblock3/randombytes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
250 97 98 194 5 149 229 27 58 62 133 73 95 230 219 128
224 98 210 250 162 58 240 80 202 249 173 203 125 186

0 comments on commit e47f8c2

Please sign in to comment.