1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the rybakit/msgpack.php package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace MessagePack; |
13
|
|
|
|
14
|
|
|
use MessagePack\Exception\InsufficientDataException; |
15
|
|
|
use MessagePack\Exception\IntegerOverflowException; |
16
|
|
|
use MessagePack\Exception\InvalidOptionException; |
17
|
|
|
use MessagePack\Exception\UnpackingFailedException; |
18
|
|
|
use MessagePack\TypeTransformer\Extension; |
19
|
|
|
|
20
|
|
|
class BufferUnpacker |
21
|
|
|
{ |
22
|
|
|
private $buffer; |
23
|
|
|
private $offset = 0; |
24
|
|
|
private $isBigIntAsStr; |
25
|
|
|
private $isBigIntAsGmp; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Extension[] |
29
|
|
|
*/ |
30
|
|
|
private $extensions = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param UnpackOptions|int|null $options |
34
|
|
|
* @param Extension[] $extensions |
35
|
|
|
* |
36
|
|
|
* @throws InvalidOptionException |
37
|
|
|
*/ |
38
|
318 |
|
public function __construct(string $buffer = '', $options = null, array $extensions = []) |
39
|
|
|
{ |
40
|
318 |
|
if (\is_null($options)) { |
41
|
317 |
|
$options = UnpackOptions::fromDefaults(); |
42
|
8 |
|
} elseif (!$options instanceof PackOptions) { |
|
|
|
|
43
|
8 |
|
$options = UnpackOptions::fromBitmask($options); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
318 |
|
$this->isBigIntAsStr = $options->isBigIntAsStrMode(); |
47
|
318 |
|
$this->isBigIntAsGmp = $options->isBigIntAsGmpMode(); |
48
|
|
|
|
49
|
318 |
|
$this->buffer = $buffer; |
50
|
|
|
|
51
|
318 |
|
if ([] !== $extensions) { |
52
|
1 |
|
$this->extensions = []; |
53
|
1 |
|
foreach ($extensions as $extension) { |
54
|
1 |
|
$this->extensions[$extension->getType()] = $extension; |
55
|
|
|
} |
56
|
|
|
} |
57
|
318 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function extendWith(Extension $extension, Extension ...$extensions) : self |
60
|
|
|
{ |
61
|
1 |
|
$new = clone $this; |
62
|
1 |
|
$new->extensions[$extension->getType()] = $extension; |
63
|
|
|
|
64
|
1 |
|
if ([] !== $extensions) { |
65
|
1 |
|
foreach ($extensions as $extraExtension) { |
66
|
1 |
|
$new->extensions[$extraExtension->getType()] = $extraExtension; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $new; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function withBuffer(string $buffer) : self |
74
|
|
|
{ |
75
|
2 |
|
$new = clone $this; |
76
|
2 |
|
$new->buffer = $buffer; |
77
|
2 |
|
$new->offset = 0; |
78
|
|
|
|
79
|
2 |
|
return $new; |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
public function append(string $data) : self |
83
|
|
|
{ |
84
|
10 |
|
$this->buffer .= $data; |
85
|
|
|
|
86
|
10 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
291 |
|
public function reset(string $buffer = '') : self |
90
|
|
|
{ |
91
|
291 |
|
$this->buffer = $buffer; |
92
|
291 |
|
$this->offset = 0; |
93
|
|
|
|
94
|
291 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
public function seek(int $offset) : self |
98
|
|
|
{ |
99
|
3 |
|
if ($offset < 0) { |
100
|
1 |
|
$offset += \strlen($this->buffer); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
if (!isset($this->buffer[$offset])) { |
104
|
1 |
|
throw new InsufficientDataException("Unable to seek to position $offset."); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
$this->offset = $offset; |
108
|
|
|
|
109
|
2 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function skip(int $length) : self |
113
|
|
|
{ |
114
|
2 |
|
$offset = $this->offset + $length; |
115
|
|
|
|
116
|
2 |
|
if (!isset($this->buffer[$offset])) { |
117
|
1 |
|
throw new InsufficientDataException("Unable to seek to position $offset."); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$this->offset = $offset; |
121
|
|
|
|
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int $length |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
47 |
|
public function read($length) |
131
|
|
|
{ |
132
|
47 |
|
if (!isset($this->buffer[$this->offset + $length - 1])) { |
133
|
1 |
|
throw new InsufficientDataException(); |
134
|
|
|
} |
135
|
|
|
|
136
|
47 |
|
$data = \substr($this->buffer, $this->offset, $length); |
137
|
47 |
|
$this->offset += $length; |
138
|
|
|
|
139
|
47 |
|
return $data; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
public function tryUnpack() : array |
143
|
|
|
{ |
144
|
3 |
|
$data = []; |
145
|
3 |
|
$offset = $this->offset; |
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
do { |
149
|
3 |
|
$data[] = $this->unpack(); |
150
|
3 |
|
$offset = $this->offset; |
151
|
3 |
|
} while (isset($this->buffer[$this->offset])); |
152
|
1 |
|
} catch (InsufficientDataException $e) { |
153
|
1 |
|
$this->offset = $offset; |
154
|
|
|
} |
155
|
|
|
|
156
|
3 |
|
if ($this->offset) { |
157
|
3 |
|
$this->buffer = isset($this->buffer[$this->offset]) ? \substr($this->buffer, $this->offset) : ''; |
158
|
3 |
|
$this->offset = 0; |
159
|
|
|
} |
160
|
|
|
|
161
|
3 |
|
return $data; |
162
|
|
|
} |
163
|
|
|
|
164
|
188 |
|
public function unpack() |
165
|
|
|
{ |
166
|
188 |
|
if (!isset($this->buffer[$this->offset])) { |
167
|
5 |
|
throw new InsufficientDataException(); |
168
|
|
|
} |
169
|
|
|
|
170
|
185 |
|
$c = \ord($this->buffer[$this->offset]); |
171
|
185 |
|
++$this->offset; |
172
|
|
|
|
173
|
|
|
// fixint |
174
|
185 |
|
if ($c <= 0x7f) { |
175
|
27 |
|
return $c; |
176
|
|
|
} |
177
|
|
|
// fixstr |
178
|
175 |
|
if ($c >= 0xa0 && $c <= 0xbf) { |
179
|
16 |
|
return ($c & 0x1f) ? $this->read($c & 0x1f) : ''; |
180
|
|
|
} |
181
|
|
|
// negfixint |
182
|
169 |
|
if ($c >= 0xe0) { |
183
|
5 |
|
return $c - 0x100; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
switch ($c) { |
187
|
165 |
|
case 0xc0: return null; |
188
|
163 |
|
case 0xc2: return false; |
189
|
159 |
|
case 0xc3: return true; |
190
|
|
|
|
191
|
|
|
// fixmap |
192
|
151 |
|
case 0x80: return []; |
193
|
150 |
|
case 0x81: return [$this->unpackMapKey() => $this->unpack()]; |
194
|
117 |
|
case 0x82: return [$this->unpackMapKey() => $this->unpack(), $this->unpackMapKey() => $this->unpack()]; |
195
|
116 |
|
case 0x83: return [$this->unpackMapKey() => $this->unpack(), $this->unpackMapKey() => $this->unpack(), $this->unpackMapKey() => $this->unpack()]; |
196
|
116 |
|
case 0x84: return $this->unpackMapData(4); |
197
|
116 |
|
case 0x85: return $this->unpackMapData(5); |
198
|
116 |
|
case 0x86: return $this->unpackMapData(6); |
199
|
116 |
|
case 0x87: return $this->unpackMapData(7); |
200
|
116 |
|
case 0x88: return $this->unpackMapData(8); |
201
|
116 |
|
case 0x89: return $this->unpackMapData(9); |
202
|
116 |
|
case 0x8a: return $this->unpackMapData(10); |
203
|
116 |
|
case 0x8b: return $this->unpackMapData(11); |
204
|
116 |
|
case 0x8c: return $this->unpackMapData(12); |
205
|
116 |
|
case 0x8d: return $this->unpackMapData(13); |
206
|
116 |
|
case 0x8e: return $this->unpackMapData(14); |
207
|
116 |
|
case 0x8f: return $this->unpackMapData(15); |
208
|
|
|
|
209
|
|
|
// fixarray |
210
|
116 |
|
case 0x90: return []; |
211
|
115 |
|
case 0x91: return [$this->unpack()]; |
212
|
115 |
|
case 0x92: return [$this->unpack(), $this->unpack()]; |
213
|
110 |
|
case 0x93: return [$this->unpack(), $this->unpack(), $this->unpack()]; |
214
|
109 |
|
case 0x94: return $this->unpackArrayData(4); |
215
|
109 |
|
case 0x95: return $this->unpackArrayData(5); |
216
|
109 |
|
case 0x96: return $this->unpackArrayData(6); |
217
|
109 |
|
case 0x97: return $this->unpackArrayData(7); |
218
|
109 |
|
case 0x98: return $this->unpackArrayData(8); |
219
|
109 |
|
case 0x99: return $this->unpackArrayData(9); |
220
|
109 |
|
case 0x9a: return $this->unpackArrayData(10); |
221
|
109 |
|
case 0x9b: return $this->unpackArrayData(11); |
222
|
109 |
|
case 0x9c: return $this->unpackArrayData(12); |
223
|
109 |
|
case 0x9d: return $this->unpackArrayData(13); |
224
|
109 |
|
case 0x9e: return $this->unpackArrayData(14); |
225
|
109 |
|
case 0x9f: return $this->unpackArrayData(15); |
226
|
|
|
|
227
|
|
|
// bin |
228
|
109 |
|
case 0xc4: return $this->read($this->unpackUint8()); |
229
|
104 |
|
case 0xc5: return $this->read($this->unpackUint16()); |
230
|
103 |
|
case 0xc6: return $this->read($this->unpackUint32()); |
231
|
|
|
|
232
|
|
|
// float |
233
|
102 |
|
case 0xca: return $this->unpackFloat32(); |
234
|
99 |
|
case 0xcb: return $this->unpackFloat64(); |
235
|
|
|
|
236
|
|
|
// uint |
237
|
95 |
|
case 0xcc: return $this->unpackUint8(); |
238
|
91 |
|
case 0xcd: return $this->unpackUint16(); |
239
|
85 |
|
case 0xce: return $this->unpackUint32(); |
240
|
81 |
|
case 0xcf: return $this->unpackUint64(); |
241
|
|
|
|
242
|
|
|
// int |
243
|
71 |
|
case 0xd0: return $this->unpackInt8(); |
244
|
66 |
|
case 0xd1: return $this->unpackInt16(); |
245
|
61 |
|
case 0xd2: return $this->unpackInt32(); |
246
|
56 |
|
case 0xd3: return $this->unpackInt64(); |
247
|
|
|
|
248
|
|
|
// str |
249
|
48 |
|
case 0xd9: return $this->read($this->unpackUint8()); |
250
|
44 |
|
case 0xda: return $this->read($this->unpackUint16()); |
251
|
42 |
|
case 0xdb: return $this->read($this->unpackUint32()); |
252
|
|
|
|
253
|
|
|
// array |
254
|
41 |
|
case 0xdc: return $this->unpackArrayData($this->unpackUint16()); |
255
|
39 |
|
case 0xdd: return $this->unpackArrayData($this->unpackUint32()); |
256
|
|
|
|
257
|
|
|
// map |
258
|
38 |
|
case 0xde: return $this->unpackMapData($this->unpackUint16()); |
259
|
36 |
|
case 0xdf: return $this->unpackMapData($this->unpackUint32()); |
260
|
|
|
|
261
|
|
|
// ext |
262
|
35 |
|
case 0xd4: return $this->unpackExtData(1); |
263
|
30 |
|
case 0xd5: return $this->unpackExtData(2); |
264
|
27 |
|
case 0xd6: return $this->unpackExtData(4); |
265
|
24 |
|
case 0xd7: return $this->unpackExtData(8); |
266
|
21 |
|
case 0xd8: return $this->unpackExtData(16); |
267
|
18 |
|
case 0xc7: return $this->unpackExtData($this->unpackUint8()); |
268
|
11 |
|
case 0xc8: return $this->unpackExtData($this->unpackUint16()); |
269
|
6 |
|
case 0xc9: return $this->unpackExtData($this->unpackUint32()); |
270
|
|
|
} |
271
|
|
|
|
272
|
1 |
|
throw UnpackingFailedException::unknownCode($c); |
273
|
|
|
} |
274
|
|
|
|
275
|
3 |
|
public function unpackNil() |
276
|
|
|
{ |
277
|
3 |
|
if (!isset($this->buffer[$this->offset])) { |
278
|
1 |
|
throw new InsufficientDataException(); |
279
|
|
|
} |
280
|
|
|
|
281
|
2 |
|
if ("\xc0" === $this->buffer[$this->offset]) { |
282
|
1 |
|
++$this->offset; |
283
|
|
|
|
284
|
1 |
|
return null; |
285
|
|
|
} |
286
|
|
|
|
287
|
1 |
|
throw UnpackingFailedException::unexpectedCode(\ord($this->buffer[$this->offset++]), 'nil'); |
288
|
|
|
} |
289
|
|
|
|
290
|
5 |
|
public function unpackBool() |
291
|
|
|
{ |
292
|
5 |
|
if (!isset($this->buffer[$this->offset])) { |
293
|
1 |
|
throw new InsufficientDataException(); |
294
|
|
|
} |
295
|
|
|
|
296
|
4 |
|
$c = \ord($this->buffer[$this->offset]); |
297
|
4 |
|
++$this->offset; |
298
|
|
|
|
299
|
4 |
|
if (0xc2 === $c) { |
300
|
2 |
|
return false; |
301
|
|
|
} |
302
|
2 |
|
if (0xc3 === $c) { |
303
|
1 |
|
return true; |
304
|
|
|
} |
305
|
|
|
|
306
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'bool'); |
307
|
|
|
} |
308
|
|
|
|
309
|
40 |
|
public function unpackInt() |
310
|
|
|
{ |
311
|
40 |
|
if (!isset($this->buffer[$this->offset])) { |
312
|
1 |
|
throw new InsufficientDataException(); |
313
|
|
|
} |
314
|
|
|
|
315
|
39 |
|
$c = \ord($this->buffer[$this->offset]); |
316
|
39 |
|
++$this->offset; |
317
|
|
|
|
318
|
|
|
// fixint |
319
|
39 |
|
if ($c <= 0x7f) { |
320
|
3 |
|
return $c; |
321
|
|
|
} |
322
|
|
|
// negfixint |
323
|
36 |
|
if ($c >= 0xe0) { |
324
|
3 |
|
return $c - 0x100; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
switch ($c) { |
328
|
|
|
// uint |
329
|
33 |
|
case 0xcc: return $this->unpackUint8(); |
330
|
30 |
|
case 0xcd: return $this->unpackUint16(); |
331
|
27 |
|
case 0xce: return $this->unpackUint32(); |
332
|
24 |
|
case 0xcf: return $this->unpackUint64(); |
333
|
|
|
|
334
|
|
|
// int |
335
|
20 |
|
case 0xd0: return $this->unpackInt8(); |
336
|
16 |
|
case 0xd1: return $this->unpackInt16(); |
337
|
12 |
|
case 0xd2: return $this->unpackInt32(); |
338
|
8 |
|
case 0xd3: return $this->unpackInt64(); |
339
|
|
|
} |
340
|
|
|
|
341
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'int'); |
342
|
|
|
} |
343
|
|
|
|
344
|
7 |
|
public function unpackFloat() |
345
|
|
|
{ |
346
|
7 |
|
if (!isset($this->buffer[$this->offset])) { |
347
|
1 |
|
throw new InsufficientDataException(); |
348
|
|
|
} |
349
|
|
|
|
350
|
6 |
|
$c = \ord($this->buffer[$this->offset]); |
351
|
6 |
|
++$this->offset; |
352
|
|
|
|
353
|
6 |
|
if (0xcb === $c) { |
354
|
3 |
|
return $this->unpackFloat64(); |
355
|
|
|
} |
356
|
3 |
|
if (0xca === $c) { |
357
|
2 |
|
return $this->unpackFloat32(); |
358
|
|
|
} |
359
|
|
|
|
360
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'float'); |
361
|
|
|
} |
362
|
|
|
|
363
|
14 |
|
public function unpackStr() |
364
|
|
|
{ |
365
|
14 |
|
if (!isset($this->buffer[$this->offset])) { |
366
|
1 |
|
throw new InsufficientDataException(); |
367
|
|
|
} |
368
|
|
|
|
369
|
13 |
|
$c = \ord($this->buffer[$this->offset]); |
370
|
13 |
|
++$this->offset; |
371
|
|
|
|
372
|
13 |
|
if ($c >= 0xa0 && $c <= 0xbf) { |
373
|
5 |
|
return ($c & 0x1f) ? $this->read($c & 0x1f) : ''; |
374
|
|
|
} |
375
|
8 |
|
if (0xd9 === $c) { |
376
|
4 |
|
return $this->read($this->unpackUint8()); |
377
|
|
|
} |
378
|
4 |
|
if (0xda === $c) { |
379
|
2 |
|
return $this->read($this->unpackUint16()); |
380
|
|
|
} |
381
|
2 |
|
if (0xdb === $c) { |
382
|
1 |
|
return $this->read($this->unpackUint32()); |
383
|
|
|
} |
384
|
|
|
|
385
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'str'); |
386
|
|
|
} |
387
|
|
|
|
388
|
7 |
|
public function unpackBin() |
389
|
|
|
{ |
390
|
7 |
|
if (!isset($this->buffer[$this->offset])) { |
391
|
1 |
|
throw new InsufficientDataException(); |
392
|
|
|
} |
393
|
|
|
|
394
|
6 |
|
$c = \ord($this->buffer[$this->offset]); |
395
|
6 |
|
++$this->offset; |
396
|
|
|
|
397
|
6 |
|
if (0xc4 === $c) { |
398
|
3 |
|
return $this->read($this->unpackUint8()); |
399
|
|
|
} |
400
|
3 |
|
if (0xc5 === $c) { |
401
|
1 |
|
return $this->read($this->unpackUint16()); |
402
|
|
|
} |
403
|
2 |
|
if (0xc6 === $c) { |
404
|
1 |
|
return $this->read($this->unpackUint32()); |
405
|
|
|
} |
406
|
|
|
|
407
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'bin'); |
408
|
|
|
} |
409
|
|
|
|
410
|
9 |
|
public function unpackArray() |
411
|
|
|
{ |
412
|
9 |
|
$size = $this->unpackArrayHeader(); |
413
|
|
|
|
414
|
7 |
|
$array = []; |
415
|
7 |
|
while ($size--) { |
416
|
6 |
|
$array[] = $this->unpack(); |
417
|
|
|
} |
418
|
|
|
|
419
|
7 |
|
return $array; |
420
|
|
|
} |
421
|
|
|
|
422
|
9 |
|
public function unpackArrayHeader() |
423
|
|
|
{ |
424
|
9 |
|
if (!isset($this->buffer[$this->offset])) { |
425
|
1 |
|
throw new InsufficientDataException(); |
426
|
|
|
} |
427
|
|
|
|
428
|
8 |
|
$c = \ord($this->buffer[$this->offset]); |
429
|
8 |
|
++$this->offset; |
430
|
|
|
|
431
|
8 |
|
if ($c >= 0x90 && $c <= 0x9f) { |
432
|
4 |
|
return $c & 0xf; |
433
|
|
|
} |
434
|
4 |
|
if (0xdc === $c) { |
435
|
2 |
|
return $this->unpackUint16(); |
436
|
|
|
} |
437
|
2 |
|
if (0xdd === $c) { |
438
|
1 |
|
return $this->unpackUint32(); |
439
|
|
|
} |
440
|
|
|
|
441
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'array'); |
442
|
|
|
} |
443
|
|
|
|
444
|
43 |
|
public function unpackMap() |
445
|
|
|
{ |
446
|
43 |
|
$size = $this->unpackMapHeader(); |
447
|
|
|
|
448
|
41 |
|
$map = []; |
449
|
41 |
|
while ($size--) { |
450
|
40 |
|
$map[$this->unpackMapKey()] = $this->unpack(); |
451
|
|
|
} |
452
|
|
|
|
453
|
9 |
|
return $map; |
454
|
|
|
} |
455
|
|
|
|
456
|
43 |
|
public function unpackMapHeader() |
457
|
|
|
{ |
458
|
43 |
|
if (!isset($this->buffer[$this->offset])) { |
459
|
1 |
|
throw new InsufficientDataException(); |
460
|
|
|
} |
461
|
|
|
|
462
|
42 |
|
$c = \ord($this->buffer[$this->offset]); |
463
|
42 |
|
++$this->offset; |
464
|
|
|
|
465
|
42 |
|
if ($c >= 0x80 && $c <= 0x8f) { |
466
|
38 |
|
return $c & 0xf; |
467
|
|
|
} |
468
|
4 |
|
if (0xde === $c) { |
469
|
2 |
|
return $this->unpackUint16(); |
470
|
|
|
} |
471
|
2 |
|
if (0xdf === $c) { |
472
|
1 |
|
return $this->unpackUint32(); |
473
|
|
|
} |
474
|
|
|
|
475
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'map'); |
476
|
|
|
} |
477
|
|
|
|
478
|
13 |
|
public function unpackExt() |
479
|
|
|
{ |
480
|
13 |
|
if (!isset($this->buffer[$this->offset])) { |
481
|
1 |
|
throw new InsufficientDataException(); |
482
|
|
|
} |
483
|
|
|
|
484
|
12 |
|
$c = \ord($this->buffer[$this->offset]); |
485
|
12 |
|
++$this->offset; |
486
|
|
|
|
487
|
|
|
switch ($c) { |
488
|
12 |
|
case 0xd4: return $this->unpackExtData(1); |
489
|
11 |
|
case 0xd5: return $this->unpackExtData(2); |
490
|
10 |
|
case 0xd6: return $this->unpackExtData(4); |
491
|
9 |
|
case 0xd7: return $this->unpackExtData(8); |
492
|
8 |
|
case 0xd8: return $this->unpackExtData(16); |
493
|
7 |
|
case 0xc7: return $this->unpackExtData($this->unpackUint8()); |
494
|
5 |
|
case 0xc8: return $this->unpackExtData($this->unpackUint16()); |
495
|
3 |
|
case 0xc9: return $this->unpackExtData($this->unpackUint32()); |
496
|
|
|
} |
497
|
|
|
|
498
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'ext'); |
499
|
|
|
} |
500
|
|
|
|
501
|
38 |
|
private function unpackUint8() |
502
|
|
|
{ |
503
|
38 |
|
if (!isset($this->buffer[$this->offset])) { |
504
|
2 |
|
throw new InsufficientDataException(); |
505
|
|
|
} |
506
|
|
|
|
507
|
36 |
|
return \ord($this->buffer[$this->offset++]); |
508
|
|
|
} |
509
|
|
|
|
510
|
32 |
|
private function unpackUint16() |
511
|
|
|
{ |
512
|
32 |
|
if (!isset($this->buffer[$this->offset + 1])) { |
513
|
2 |
|
throw new InsufficientDataException(); |
514
|
|
|
} |
515
|
|
|
|
516
|
30 |
|
return \ord($this->buffer[$this->offset++]) << 8 |
517
|
30 |
|
| \ord($this->buffer[$this->offset++]); |
518
|
|
|
} |
519
|
|
|
|
520
|
24 |
|
private function unpackUint32() |
521
|
|
|
{ |
522
|
24 |
|
if (!isset($this->buffer[$this->offset + 3])) { |
523
|
2 |
|
throw new InsufficientDataException(); |
524
|
|
|
} |
525
|
|
|
|
526
|
22 |
|
return \ord($this->buffer[$this->offset++]) << 24 |
527
|
22 |
|
| \ord($this->buffer[$this->offset++]) << 16 |
528
|
22 |
|
| \ord($this->buffer[$this->offset++]) << 8 |
529
|
22 |
|
| \ord($this->buffer[$this->offset++]); |
530
|
|
|
} |
531
|
|
|
|
532
|
16 |
|
private function unpackUint64() |
533
|
|
|
{ |
534
|
16 |
|
if (!isset($this->buffer[$this->offset + 7])) { |
535
|
1 |
|
throw new InsufficientDataException(); |
536
|
|
|
} |
537
|
|
|
|
538
|
15 |
|
$num = \unpack('J', $this->buffer, $this->offset)[1]; |
539
|
15 |
|
$this->offset += 8; |
540
|
|
|
|
541
|
15 |
|
return $num < 0 ? $this->handleIntOverflow($num) : $num; |
542
|
|
|
} |
543
|
|
|
|
544
|
9 |
|
private function unpackInt8() |
545
|
|
|
{ |
546
|
9 |
|
if (!isset($this->buffer[$this->offset])) { |
547
|
1 |
|
throw new InsufficientDataException(); |
548
|
|
|
} |
549
|
|
|
|
550
|
8 |
|
$num = \ord($this->buffer[$this->offset]); |
551
|
8 |
|
++$this->offset; |
552
|
|
|
|
553
|
8 |
|
return $num > 0x7f ? $num - 0x100 : $num; |
554
|
|
|
} |
555
|
|
|
|
556
|
9 |
|
private function unpackInt16() |
557
|
|
|
{ |
558
|
9 |
|
if (!isset($this->buffer[$this->offset + 1])) { |
559
|
1 |
|
throw new InsufficientDataException(); |
560
|
|
|
} |
561
|
|
|
|
562
|
8 |
|
$num = \ord($this->buffer[$this->offset++]) << 8 |
563
|
8 |
|
| \ord($this->buffer[$this->offset++]); |
564
|
|
|
|
565
|
8 |
|
return $num > 0x7fff ? $num - 0x10000 : $num; |
566
|
|
|
} |
567
|
|
|
|
568
|
9 |
|
private function unpackInt32() |
569
|
|
|
{ |
570
|
9 |
|
if (!isset($this->buffer[$this->offset + 3])) { |
571
|
1 |
|
throw new InsufficientDataException(); |
572
|
|
|
} |
573
|
|
|
|
574
|
8 |
|
$num = \ord($this->buffer[$this->offset++]) << 24 |
575
|
8 |
|
| \ord($this->buffer[$this->offset++]) << 16 |
576
|
8 |
|
| \ord($this->buffer[$this->offset++]) << 8 |
577
|
8 |
|
| \ord($this->buffer[$this->offset++]); |
578
|
|
|
|
579
|
8 |
|
return $num > 0x7fffffff ? $num - 0x100000000 : $num; |
580
|
|
|
} |
581
|
|
|
|
582
|
15 |
|
private function unpackInt64() |
583
|
|
|
{ |
584
|
15 |
|
if (!isset($this->buffer[$this->offset + 7])) { |
585
|
1 |
|
throw new InsufficientDataException(); |
586
|
|
|
} |
587
|
|
|
|
588
|
14 |
|
$num = \unpack('J', $this->buffer, $this->offset)[1]; |
589
|
14 |
|
$this->offset += 8; |
590
|
|
|
|
591
|
14 |
|
return $num; |
592
|
|
|
} |
593
|
|
|
|
594
|
5 |
|
private function unpackFloat32() |
595
|
|
|
{ |
596
|
5 |
|
if (!isset($this->buffer[$this->offset + 3])) { |
597
|
1 |
|
throw new InsufficientDataException(); |
598
|
|
|
} |
599
|
|
|
|
600
|
4 |
|
$num = \unpack('G', $this->buffer, $this->offset)[1]; |
601
|
4 |
|
$this->offset += 4; |
602
|
|
|
|
603
|
4 |
|
return $num; |
604
|
|
|
} |
605
|
|
|
|
606
|
7 |
|
private function unpackFloat64() |
607
|
|
|
{ |
608
|
7 |
|
if (!isset($this->buffer[$this->offset + 7])) { |
609
|
1 |
|
throw new InsufficientDataException(); |
610
|
|
|
} |
611
|
|
|
|
612
|
6 |
|
$num = \unpack('E', $this->buffer, $this->offset)[1]; |
613
|
6 |
|
$this->offset += 8; |
614
|
|
|
|
615
|
6 |
|
return $num; |
616
|
|
|
} |
617
|
|
|
|
618
|
4 |
|
private function unpackArrayData($size) |
619
|
|
|
{ |
620
|
4 |
|
$array = []; |
621
|
4 |
|
while ($size--) { |
622
|
4 |
|
$array[] = $this->unpack(); |
623
|
|
|
} |
624
|
|
|
|
625
|
4 |
|
return $array; |
626
|
|
|
} |
627
|
|
|
|
628
|
5 |
|
private function unpackMapData($size) |
629
|
|
|
{ |
630
|
5 |
|
$map = []; |
631
|
5 |
|
while ($size--) { |
632
|
5 |
|
$map[$this->unpackMapKey()] = $this->unpack(); |
633
|
|
|
} |
634
|
|
|
|
635
|
5 |
|
return $map; |
636
|
|
|
} |
637
|
|
|
|
638
|
82 |
|
private function unpackMapKey() |
639
|
|
|
{ |
640
|
82 |
|
if (!isset($this->buffer[$this->offset])) { |
641
|
|
|
throw new InsufficientDataException(); |
642
|
|
|
} |
643
|
|
|
|
644
|
82 |
|
$c = \ord($this->buffer[$this->offset]); |
645
|
82 |
|
++$this->offset; |
646
|
|
|
|
647
|
|
|
// fixint |
648
|
82 |
|
if ($c <= 0x7f) { |
649
|
14 |
|
return $c; |
650
|
|
|
} |
651
|
|
|
// fixstr |
652
|
76 |
|
if ($c >= 0xa0 && $c <= 0xbf) { |
653
|
2 |
|
return ($c & 0x1f) ? $this->read($c & 0x1f) : ''; |
654
|
|
|
} |
655
|
|
|
// negfixint |
656
|
74 |
|
if ($c >= 0xe0) { |
657
|
2 |
|
return $c - 0x100; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
switch ($c) { |
661
|
|
|
// uint |
662
|
72 |
|
case 0xcc: return $this->unpackUint8(); |
663
|
72 |
|
case 0xcd: return $this->unpackUint16(); |
664
|
70 |
|
case 0xce: return $this->unpackUint32(); |
665
|
68 |
|
case 0xcf: return $this->unpackUint64(); |
666
|
|
|
|
667
|
|
|
// int |
668
|
66 |
|
case 0xd0: return $this->unpackInt8(); |
669
|
66 |
|
case 0xd1: return $this->unpackInt16(); |
670
|
66 |
|
case 0xd2: return $this->unpackInt32(); |
671
|
66 |
|
case 0xd3: return $this->unpackInt64(); |
672
|
|
|
|
673
|
|
|
// str |
674
|
66 |
|
case 0xd9: return $this->read($this->unpackUint8()); |
675
|
66 |
|
case 0xda: return $this->read($this->unpackUint16()); |
676
|
66 |
|
case 0xdb: return $this->read($this->unpackUint32()); |
677
|
|
|
|
678
|
|
|
// bin |
679
|
66 |
|
case 0xc4: return $this->read($this->unpackUint8()); |
680
|
64 |
|
case 0xc5: return $this->read($this->unpackUint16()); |
681
|
64 |
|
case 0xc6: return $this->read($this->unpackUint32()); |
682
|
|
|
} |
683
|
|
|
|
684
|
64 |
|
throw UnpackingFailedException::invalidMapKeyType($c); |
685
|
|
|
} |
686
|
|
|
|
687
|
42 |
|
private function unpackExtData($length) |
688
|
|
|
{ |
689
|
42 |
|
if (!isset($this->buffer[$this->offset + $length])) { // 1 (type) + length - 1 |
690
|
16 |
|
throw new InsufficientDataException(); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
// int8 |
694
|
26 |
|
$type = \ord($this->buffer[$this->offset]); |
695
|
26 |
|
++$this->offset; |
696
|
|
|
|
697
|
26 |
|
if ($type > 0x7f) { |
698
|
|
|
$type -= 0x100; |
699
|
|
|
} |
700
|
|
|
|
701
|
26 |
|
if (isset($this->extensions[$type])) { |
702
|
2 |
|
return $this->extensions[$type]->unpackExt($this, $length); |
703
|
|
|
} |
704
|
|
|
|
705
|
24 |
|
$data = \substr($this->buffer, $this->offset, $length); |
706
|
24 |
|
$this->offset += $length; |
707
|
|
|
|
708
|
24 |
|
return new Ext($type, $data); |
709
|
|
|
} |
710
|
|
|
|
711
|
5 |
|
private function handleIntOverflow($value) |
712
|
|
|
{ |
713
|
5 |
|
if ($this->isBigIntAsStr) { |
714
|
3 |
|
return \sprintf('%u', $value); |
715
|
|
|
} |
716
|
2 |
|
if ($this->isBigIntAsGmp) { |
717
|
1 |
|
return \gmp_init(\sprintf('%u', $value)); |
718
|
|
|
} |
719
|
|
|
|
720
|
1 |
|
throw new IntegerOverflowException($value); |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
|