1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBinaryReader; |
4
|
|
|
|
5
|
|
|
use PhpBinaryReader\Exception\InvalidDataException; |
6
|
|
|
use PhpBinaryReader\Type\Bit; |
7
|
|
|
use PhpBinaryReader\Type\Byte; |
8
|
|
|
use PhpBinaryReader\Type\Int8; |
9
|
|
|
use PhpBinaryReader\Type\Int16; |
10
|
|
|
use PhpBinaryReader\Type\Int32; |
11
|
|
|
use PhpBinaryReader\Type\Single; |
12
|
|
|
use PhpBinaryReader\Type\Str; |
13
|
|
|
|
14
|
|
|
class BinaryReader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private $machineByteOrder = Endian::ENDIAN_LITTLE; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var resource |
23
|
|
|
*/ |
24
|
|
|
private $inputHandle; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $currentBit; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
private $nextByte; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $position; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $eofPosition; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $endian; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \PhpBinaryReader\Type\Byte |
53
|
|
|
*/ |
54
|
|
|
private $byteReader; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \PhpBinaryReader\Type\Bit |
58
|
|
|
*/ |
59
|
|
|
private $bitReader; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \PhpBinaryReader\Type\Str |
63
|
|
|
*/ |
64
|
|
|
private $stringReader; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \PhpBinaryReader\Type\Int8 |
68
|
|
|
*/ |
69
|
|
|
private $int8Reader; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var \PhpBinaryReader\Type\Int16 |
73
|
|
|
*/ |
74
|
|
|
private $int16Reader; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var \PhpBinaryReader\Type\Int32 |
78
|
|
|
*/ |
79
|
|
|
private $int32Reader; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var \PhpBinaryReader\Type\Single |
83
|
|
|
*/ |
84
|
|
|
private $singleReader; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|resource $input |
88
|
|
|
* @param int|string $endian |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
*/ |
91
|
62 |
|
public function __construct($input, $endian = Endian::ENDIAN_LITTLE) |
92
|
|
|
{ |
93
|
62 |
|
if (!is_resource($input)) { |
94
|
19 |
|
$this->setInputString($input); |
|
|
|
|
95
|
19 |
|
} else { |
96
|
43 |
|
$this->setInputHandle($input); |
97
|
|
|
} |
98
|
|
|
|
99
|
62 |
|
$this->eofPosition = fstat($this->getInputHandle())['size']; |
100
|
|
|
|
101
|
62 |
|
$this->setEndian($endian); |
102
|
62 |
|
$this->setNextByte(false); |
103
|
62 |
|
$this->setCurrentBit(0); |
104
|
62 |
|
$this->setPosition(0); |
105
|
|
|
|
106
|
62 |
|
$this->bitReader = new Bit(); |
107
|
62 |
|
$this->stringReader = new Str(); |
108
|
62 |
|
$this->byteReader = new Byte(); |
109
|
62 |
|
$this->int8Reader = new Int8(); |
110
|
62 |
|
$this->int16Reader = new Int16(); |
111
|
62 |
|
$this->int32Reader = new Int32(); |
112
|
62 |
|
$this->singleReader = new Single(); |
113
|
62 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
2 |
|
public function isEof() |
119
|
|
|
{ |
120
|
2 |
|
return $this->position >= $this->eofPosition; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param int $length |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
114 |
|
public function canReadBytes($length = 0) |
128
|
|
|
{ |
129
|
114 |
|
return $this->position + $length <= $this->eofPosition; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
14 |
|
public function align() |
136
|
|
|
{ |
137
|
14 |
|
$this->setCurrentBit(0); |
138
|
14 |
|
$this->setNextByte(false); |
139
|
14 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $count |
143
|
|
|
* @return int |
144
|
|
|
*/ |
145
|
66 |
|
public function readBits($count) |
146
|
|
|
{ |
147
|
66 |
|
return $this->bitReader->readSigned($this, $count); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $count |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
4 |
|
public function readUBits($count) |
155
|
|
|
{ |
156
|
4 |
|
return $this->bitReader->read($this, $count); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $count |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
2 |
|
public function readBytes($count) |
164
|
|
|
{ |
165
|
2 |
|
return $this->byteReader->read($this, $count); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
32 |
|
public function readInt8() |
172
|
|
|
{ |
173
|
32 |
|
return $this->int8Reader->readSigned($this); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return int |
178
|
|
|
*/ |
179
|
4 |
|
public function readUInt8() |
180
|
|
|
{ |
181
|
4 |
|
return $this->int8Reader->read($this); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return int |
186
|
|
|
*/ |
187
|
4 |
|
public function readInt16() |
188
|
|
|
{ |
189
|
4 |
|
return $this->int16Reader->readSigned($this); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
6 |
|
public function readUInt16() |
196
|
|
|
{ |
197
|
6 |
|
return $this->int16Reader->read($this); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return int |
202
|
|
|
*/ |
203
|
6 |
|
public function readInt32() |
204
|
|
|
{ |
205
|
6 |
|
return $this->int32Reader->readSigned($this); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return int |
210
|
|
|
*/ |
211
|
6 |
|
public function readUInt32() |
212
|
|
|
{ |
213
|
6 |
|
return $this->int32Reader->read($this); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return float |
218
|
|
|
*/ |
219
|
2 |
|
public function readSingle() |
220
|
|
|
{ |
221
|
2 |
|
return $this->singleReader->read($this); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param int $length |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
2 |
|
public function readString($length) |
229
|
|
|
{ |
230
|
2 |
|
return $this->stringReader->read($this, $length); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param int $length |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
2 |
|
public function readAlignedString($length) |
238
|
|
|
{ |
239
|
2 |
|
return $this->stringReader->readAligned($this, $length); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param int $machineByteOrder |
244
|
|
|
* @return $this |
245
|
|
|
*/ |
246
|
6 |
|
public function setMachineByteOrder($machineByteOrder) |
247
|
|
|
{ |
248
|
6 |
|
$this->machineByteOrder = $machineByteOrder; |
249
|
|
|
|
250
|
6 |
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return int |
255
|
|
|
*/ |
256
|
32 |
|
public function getMachineByteOrder() |
257
|
|
|
{ |
258
|
32 |
|
return $this->machineByteOrder; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param resource $inputHandle |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
43 |
|
public function setInputHandle($inputHandle) |
266
|
|
|
{ |
267
|
43 |
|
$this->inputHandle = $inputHandle; |
268
|
|
|
|
269
|
43 |
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return resource |
274
|
|
|
*/ |
275
|
104 |
|
public function getInputHandle() |
276
|
|
|
{ |
277
|
104 |
|
return $this->inputHandle; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param string $inputString |
282
|
|
|
* @return $this |
283
|
|
|
*/ |
284
|
21 |
|
public function setInputString($inputString) |
285
|
|
|
{ |
286
|
21 |
|
$handle = fopen('php://memory', 'br+'); |
287
|
21 |
|
fwrite($handle, $inputString); |
288
|
21 |
|
rewind($handle); |
289
|
21 |
|
$this->inputHandle = $handle; |
290
|
|
|
|
291
|
21 |
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
2 |
|
public function getInputString() |
298
|
|
|
{ |
299
|
2 |
|
$handle = $this->getInputHandle(); |
300
|
2 |
|
$str = stream_get_contents($handle); |
301
|
2 |
|
rewind($handle); |
302
|
|
|
|
303
|
2 |
|
return $str; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param mixed $nextByte |
308
|
|
|
* @return $this |
309
|
|
|
*/ |
310
|
106 |
|
public function setNextByte($nextByte) |
311
|
|
|
{ |
312
|
106 |
|
$this->nextByte = $nextByte; |
313
|
|
|
|
314
|
106 |
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return mixed |
319
|
|
|
*/ |
320
|
58 |
|
public function getNextByte() |
321
|
|
|
{ |
322
|
58 |
|
return $this->nextByte; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param int $position |
327
|
|
|
* @return $this |
328
|
|
|
*/ |
329
|
104 |
|
public function setPosition($position) |
330
|
|
|
{ |
331
|
104 |
|
$this->position = $position; |
332
|
104 |
|
fseek($this->getInputHandle(), $position); |
333
|
|
|
|
334
|
104 |
|
return $this; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return int |
339
|
|
|
*/ |
340
|
4 |
|
public function getPosition() |
341
|
|
|
{ |
342
|
4 |
|
return $this->position; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @return int |
347
|
|
|
*/ |
348
|
2 |
|
public function getEofPosition() |
349
|
|
|
{ |
350
|
2 |
|
return $this->eofPosition; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param string $endian |
355
|
|
|
* @return $this |
356
|
|
|
* @throws InvalidDataException |
357
|
|
|
*/ |
358
|
64 |
|
public function setEndian($endian) |
359
|
|
|
{ |
360
|
64 |
|
if ($endian == 'big' || $endian == Endian::ENDIAN_BIG) { |
361
|
62 |
|
$this->endian = Endian::ENDIAN_BIG; |
362
|
64 |
|
} elseif ($endian == 'little' || $endian == Endian::ENDIAN_LITTLE) { |
363
|
64 |
|
$this->endian = Endian::ENDIAN_LITTLE; |
364
|
64 |
|
} else { |
365
|
2 |
|
throw new InvalidDataException('Endian must be set as big or little'); |
366
|
|
|
} |
367
|
|
|
|
368
|
64 |
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @return string |
373
|
|
|
*/ |
374
|
52 |
|
public function getEndian() |
375
|
|
|
{ |
376
|
52 |
|
return $this->endian; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @param int $currentBit |
381
|
|
|
* @return $this |
382
|
|
|
*/ |
383
|
106 |
|
public function setCurrentBit($currentBit) |
384
|
|
|
{ |
385
|
106 |
|
$this->currentBit = $currentBit; |
386
|
|
|
|
387
|
106 |
|
return $this; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @return int |
392
|
|
|
*/ |
393
|
104 |
|
public function getCurrentBit() |
394
|
|
|
{ |
395
|
104 |
|
return $this->currentBit; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return \PhpBinaryReader\Type\Bit |
400
|
|
|
*/ |
401
|
1 |
|
public function getBitReader() |
402
|
|
|
{ |
403
|
1 |
|
return $this->bitReader; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return \PhpBinaryReader\Type\Byte |
408
|
|
|
*/ |
409
|
1 |
|
public function getByteReader() |
410
|
|
|
{ |
411
|
1 |
|
return $this->byteReader; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @return \PhpBinaryReader\Type\Int8 |
416
|
|
|
*/ |
417
|
1 |
|
public function getInt8Reader() |
418
|
|
|
{ |
419
|
1 |
|
return $this->int8Reader; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @return \PhpBinaryReader\Type\Int16 |
424
|
|
|
*/ |
425
|
1 |
|
public function getInt16Reader() |
426
|
|
|
{ |
427
|
1 |
|
return $this->int16Reader; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @return \PhpBinaryReader\Type\Int32 |
432
|
|
|
*/ |
433
|
1 |
|
public function getInt32Reader() |
434
|
|
|
{ |
435
|
1 |
|
return $this->int32Reader; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @return \PhpBinaryReader\Type\Single |
440
|
|
|
*/ |
441
|
1 |
|
public function getSingleReader() |
442
|
|
|
{ |
443
|
1 |
|
return $this->singleReader; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @return \PhpBinaryReader\Type\Str |
448
|
|
|
*/ |
449
|
1 |
|
public function getStringReader() |
450
|
|
|
{ |
451
|
1 |
|
return $this->stringReader; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Read a length of characters from the input handle, updating the |
456
|
|
|
* internal position marker. |
457
|
|
|
* |
458
|
|
|
* @return string |
459
|
|
|
*/ |
460
|
110 |
|
public function readFromHandle($length) |
461
|
|
|
{ |
462
|
110 |
|
$this->position += $length; |
463
|
110 |
|
return fread($this->inputHandle, $length); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.