-
Notifications
You must be signed in to change notification settings - Fork 5
/
Decoder.php
267 lines (217 loc) · 8.57 KB
/
Decoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Clue\React\Tar;
use Evenement\EventEmitter;
use React\Stream\ThroughStream;
use React\Stream\WritableStreamInterface;
use RuntimeException;
/**
* Decodes a TAR stream and emits "entry" events for each individual file in the archive.
*
* At the moment, this class implements the the `UStar` (Uniform Standard Tape ARchive) format,
* introduced by POSIX IEEE P1003.1. In the future, it should support more of
* the less common alternative formats.
*
* @event entry(array $header, \React\Stream\ReadableStreamInterface $stream)
* @event error(Exception $e)
* @event close()
*/
class Decoder extends EventEmitter implements WritableStreamInterface
{
private $buffer = '';
private $writable = true;
private $closing = false;
private $paused = false;
private $streaming = null;
private $remaining = 0;
private $padding = 0;
private $format;
const BLOCK_SIZE = 512;
public function __construct()
{
$this->format = "Z100name/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/Z8checksum/Z1type/Z100symlink/Z6magic/Z2version/Z32owner/Z32group/Z8deviceMajor/Z8deviceMinor/Z155prefix/Z12unpacked";
if (PHP_VERSION < 5.5) {
// PHP 5.5 replaced 'a' with 'Z' (read X bytes and removing trailing NULL bytes)
$this->format = str_replace('Z', 'a', $this->format); // @codeCoverageIgnore
}
}
public function write($data)
{
if (!$this->writable) {
return false;
}
// incomplete entry => read until end of entry before expecting next header
if ($this->streaming !== null) {
$data = $this->consumeEntry($data);
// entry still incomplete => wait for next chunk
if ($this->streaming !== null) {
return !$this->paused;
}
}
// trailing padding remaining => skip padding before expecting next header
if ($this->padding !== 0) {
$data = $this->consumePadding($data);
// padding still remaining => wait for next chunk
if ($this->padding !== 0) {
return true;
}
}
$this->buffer .= $data;
while (isset($this->buffer[self::BLOCK_SIZE - 1])) {
$header = substr($this->buffer, 0, self::BLOCK_SIZE);
$this->buffer = (string)substr($this->buffer, self::BLOCK_SIZE);
if (rtrim($header, "\0") === '') {
// skip if whole header consists of null bytes
// trailing nulls indicate end of archive, but continue reading next block anyway
continue;
}
try {
$header = $this->readHeader($header);
} catch (RuntimeException $e) {
// clean up before throwing
$this->buffer = '';
$this->writable = false;
$this->emit('error', array($e));
$this->close();
return false;
}
$this->streaming = new ThroughStream();
$this->remaining = $header['size'];
$this->padding = $header['padding'];
// entry stream is not paused by default - unless explicitly paused
// emit "drain" even when entry stream is ready again to support backpressure
$that = $this;
$paused =& $this->paused;
$paused = false;
$this->streaming->on('drain', function () use (&$paused, $that) {
$paused = false;
$that->emit('drain');
});
$this->streaming->on('close', function () use (&$paused, $that) {
if ($paused) {
$paused = false;
$that->emit('drain');
}
});
$this->emit('entry', array($header, $this->streaming));
if ($this->remaining === 0) {
$this->streaming->end();
$this->streaming = null;
} else {
$this->buffer = $this->consumeEntry($this->buffer);
}
// incomplete entry => do not read next header
if ($this->streaming !== null) {
return !$this->paused;
}
if ($this->padding !== 0) {
$this->buffer = $this->consumePadding($this->buffer);
}
// incomplete padding => do not read next header
if ($this->padding !== 0) {
return true;
}
}
return true;
}
public function end($data = null)
{
if ($data !== null) {
$this->write($data);
}
if ($this->streaming !== null) {
// input stream ended but we were still streaming an entry => emit error about incomplete entry
$this->streaming->emit('error', array(new \RuntimeException('TAR input stream ended unexpectedly')));
$this->streaming->close();
$this->streaming = null;
// add some dummy data to also trigger error on decoder stream
$this->buffer = '.';
}
if ($this->buffer !== '') {
// incomplete entry in buffer
$this->emit('error', array(new \RuntimeException('Stream ended with incomplete entry')));
$this->buffer = '';
}
$this->writable = false;
$this->close();
}
public function close()
{
if ($this->closing) {
return;
}
$this->closing = true;
$this->writable = false;
$this->buffer = '';
if ($this->streaming !== null) {
// input stream ended but we were still streaming an entry => forcefully close without error
$this->streaming->close();
$this->streaming = null;
}
// ignore whether we're still expecting NUL-padding
$this->emit('close');
$this->removeAllListeners();
}
public function isWritable()
{
return $this->writable;
}
private function consumeEntry($buffer)
{
// try to read up to [remaining] bytes from buffer
$data = substr($buffer, 0, $this->remaining);
$len = strlen($data);
// reduce remaining buffer by number of bytes actually read
$buffer = substr($buffer, $len);
$this->remaining -= $len;
// emit chunk of data
$ret = $this->streaming->write($data);
// nothing remaining => entry stream finished
if ($this->remaining === 0) {
$this->streaming->end();
$this->streaming = null;
}
// throttle input when streaming entry is still writable but returns false (backpressure)
if ($ret === false && $this->streaming !== null && $this->streaming->isWritable()) {
$this->paused = true;
}
return $buffer;
}
private function consumePadding($buffer)
{
if (strlen($buffer) > $this->padding) {
// data exceeds padding => skip padding and continue
$buffer = (string)substr($buffer, $this->padding);
$this->padding = 0;
return $buffer;
}
// less data than padding, skip only a bit of the padding and wait for next chunk
$this->padding -= strlen($buffer);
return '';
}
// https://github.com/mishak87/archive-tar/blob/master/Reader.php#L155
private function readHeader($header)
{
$record = unpack($this->format, $header);
// we only support "ustar" format (for now?)
if ($record['magic'] !== 'ustar') {
throw new RuntimeException('Unsupported archive type, expected "ustar", but found "' . $record['magic'] . '"');
}
// convert to decimal values
foreach (array('uid', 'gid', 'size', 'mtime', 'checksum') as $key) {
$record[$key] = octdec($record[$key]);
}
// calculate and compare header checksum
$checksum = 0;
for ($i = 0; $i < self::BLOCK_SIZE; $i++) {
$checksum += 148 <= $i && $i < 156 ? 32 : ord($header[$i]);
}
if ($record['checksum'] != $checksum) {
throw new RuntimeException('Invalid header checksum, expected "' . $record['checksum'] . '", but calculated "' . $checksum . '" (looks like the archive is corrupted)');
}
// padding consists of X NULL bytes after record entry until next BLOCK_SIZE boundary
$record['padding'] = (self::BLOCK_SIZE - ($record['size'] % self::BLOCK_SIZE)) % self::BLOCK_SIZE;
// filename consists of prefix and name
$record['filename'] = $record['prefix'] . $record['name'];
return $record;
}
}