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\UnpackingFailedException; |
17
|
|
|
use MessagePack\TypeTransformer\Unpackable; |
18
|
|
|
|
19
|
|
|
class BufferUnpacker |
20
|
|
|
{ |
21
|
|
|
private $buffer; |
22
|
|
|
private $offset = 0; |
23
|
|
|
private $isBigIntAsStr; |
24
|
|
|
private $isBigIntAsGmp; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Unpackable[]|null |
28
|
|
|
*/ |
29
|
|
|
private $transformers; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $buffer |
33
|
|
|
* @param UnpackOptions|int|null $options |
34
|
|
|
* |
35
|
|
|
* @throws \MessagePack\Exception\InvalidOptionException |
36
|
|
|
*/ |
37
|
241 |
|
public function __construct(string $buffer = '', $options = null) |
38
|
|
|
{ |
39
|
241 |
|
if (null === $options) { |
40
|
240 |
|
$options = UnpackOptions::fromDefaults(); |
41
|
8 |
|
} elseif (!$options instanceof PackOptions) { |
42
|
8 |
|
$options = UnpackOptions::fromBitmask($options); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
241 |
|
$this->isBigIntAsStr = $options->isBigIntAsStrMode(); |
46
|
241 |
|
$this->isBigIntAsGmp = $options->isBigIntAsGmpMode(); |
47
|
|
|
|
48
|
241 |
|
$this->buffer = $buffer; |
49
|
241 |
|
} |
50
|
|
|
|
51
|
1 |
|
public function registerTransformer(Unpackable $transformer) : self |
52
|
|
|
{ |
53
|
1 |
|
$this->transformers[$transformer->getType()] = $transformer; |
54
|
|
|
|
55
|
1 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
public function append(string $data) : self |
59
|
|
|
{ |
60
|
9 |
|
$this->buffer .= $data; |
61
|
|
|
|
62
|
9 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
213 |
|
public function reset(string $buffer = '') : self |
66
|
|
|
{ |
67
|
213 |
|
$this->buffer = $buffer; |
68
|
213 |
|
$this->offset = 0; |
69
|
|
|
|
70
|
213 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
public function seek(int $offset, int $whence = \SEEK_SET) : self |
74
|
|
|
{ |
75
|
6 |
|
if (\SEEK_CUR === $whence) { |
76
|
1 |
|
$offset += $this->offset; |
77
|
5 |
|
} elseif (\SEEK_END === $whence) { |
78
|
1 |
|
$offset += \strlen($this->buffer); |
79
|
4 |
|
} elseif (\SEEK_SET !== $whence) { |
80
|
1 |
|
throw new \InvalidArgumentException("Invalid seek origin $whence, use SEEK_SET, SEEK_CUR or SEEK_END."); |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
if (!isset($this->buffer[$offset])) { |
84
|
1 |
|
throw new \OutOfBoundsException("Invalid seek position $offset."); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
$this->offset = $offset; |
88
|
|
|
|
89
|
4 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function __clone() |
93
|
|
|
{ |
94
|
1 |
|
$this->buffer = ''; |
95
|
1 |
|
$this->offset = 0; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
3 |
|
public function tryUnpack() : array |
99
|
|
|
{ |
100
|
3 |
|
$data = []; |
101
|
3 |
|
$offset = $this->offset; |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
do { |
105
|
3 |
|
$data[] = $this->unpack(); |
106
|
3 |
|
$offset = $this->offset; |
107
|
3 |
|
} while (isset($this->buffer[$this->offset])); |
108
|
1 |
|
} catch (InsufficientDataException $e) { |
109
|
1 |
|
$this->offset = $offset; |
110
|
|
|
} |
111
|
|
|
|
112
|
3 |
|
if ($this->offset) { |
113
|
3 |
|
$this->buffer = isset($this->buffer[$this->offset]) ? \substr($this->buffer, $this->offset) : ''; |
114
|
3 |
|
$this->offset = 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return $data; |
118
|
|
|
} |
119
|
|
|
|
120
|
144 |
|
public function unpack() |
121
|
|
|
{ |
122
|
144 |
|
if (!isset($this->buffer[$this->offset])) { |
123
|
5 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
124
|
|
|
} |
125
|
|
|
|
126
|
141 |
|
$c = \ord($this->buffer[$this->offset]); |
127
|
141 |
|
++$this->offset; |
128
|
|
|
|
129
|
|
|
// fixint |
130
|
141 |
|
if ($c <= 0x7f) { |
131
|
31 |
|
return $c; |
132
|
|
|
} |
133
|
|
|
// fixstr |
134
|
134 |
View Code Duplication |
if ($c >= 0xa0 && $c <= 0xbf) { |
|
|
|
|
135
|
18 |
|
return ($c & 0x1f) ? $this->unpackStrData($c & 0x1f) : ''; |
136
|
|
|
} |
137
|
|
|
// fixarray |
138
|
127 |
|
if ($c >= 0x90 && $c <= 0x9f) { |
139
|
9 |
|
return ($c & 0xf) ? $this->unpackArrayData($c & 0xf) : []; |
140
|
|
|
} |
141
|
|
|
// fixmap |
142
|
124 |
|
if ($c >= 0x80 && $c <= 0x8f) { |
143
|
13 |
|
return ($c & 0xf) ? $this->unpackMapData($c & 0xf) : []; |
144
|
|
|
} |
145
|
|
|
// negfixint |
146
|
120 |
|
if ($c >= 0xe0) { |
147
|
5 |
|
return $c - 0x100; |
148
|
|
|
} |
149
|
|
|
|
150
|
115 |
|
switch ($c) { |
151
|
115 |
|
case 0xc0: return null; |
|
|
|
|
152
|
112 |
|
case 0xc2: return false; |
|
|
|
|
153
|
109 |
|
case 0xc3: return true; |
|
|
|
|
154
|
|
|
|
155
|
|
|
// bin |
156
|
98 |
|
case 0xc4: return $this->unpackStrData($this->unpackUint8()); |
|
|
|
|
157
|
93 |
|
case 0xc5: return $this->unpackStrData($this->unpackUint16()); |
|
|
|
|
158
|
92 |
|
case 0xc6: return $this->unpackStrData($this->unpackUint32()); |
|
|
|
|
159
|
|
|
|
160
|
|
|
// float |
161
|
91 |
|
case 0xca: return $this->unpackFloat32(); |
|
|
|
|
162
|
88 |
|
case 0xcb: return $this->unpackFloat64(); |
|
|
|
|
163
|
|
|
|
164
|
|
|
// uint |
165
|
84 |
|
case 0xcc: return $this->unpackUint8(); |
|
|
|
|
166
|
80 |
|
case 0xcd: return $this->unpackUint16(); |
|
|
|
|
167
|
73 |
|
case 0xce: return $this->unpackUint32(); |
|
|
|
|
168
|
68 |
|
case 0xcf: return $this->unpackUint64(); |
|
|
|
|
169
|
|
|
|
170
|
|
|
// int |
171
|
56 |
|
case 0xd0: return $this->unpackInt8(); |
|
|
|
|
172
|
51 |
|
case 0xd1: return $this->unpackInt16(); |
|
|
|
|
173
|
46 |
|
case 0xd2: return $this->unpackInt32(); |
|
|
|
|
174
|
41 |
|
case 0xd3: return $this->unpackInt64(); |
|
|
|
|
175
|
|
|
|
176
|
|
|
// str |
177
|
33 |
|
case 0xd9: return $this->unpackStrData($this->unpackUint8()); |
|
|
|
|
178
|
29 |
|
case 0xda: return $this->unpackStrData($this->unpackUint16()); |
|
|
|
|
179
|
27 |
|
case 0xdb: return $this->unpackStrData($this->unpackUint32()); |
|
|
|
|
180
|
|
|
|
181
|
|
|
// array |
182
|
26 |
|
case 0xdc: return $this->unpackArrayData($this->unpackUint16()); |
|
|
|
|
183
|
24 |
|
case 0xdd: return $this->unpackArrayData($this->unpackUint32()); |
|
|
|
|
184
|
|
|
|
185
|
|
|
// map |
186
|
23 |
|
case 0xde: return $this->unpackMapData($this->unpackUint16()); |
|
|
|
|
187
|
21 |
|
case 0xdf: return $this->unpackMapData($this->unpackUint32()); |
|
|
|
|
188
|
|
|
|
189
|
|
|
// ext |
190
|
20 |
|
case 0xd4: return $this->unpackExtData(1); |
|
|
|
|
191
|
17 |
|
case 0xd5: return $this->unpackExtData(2); |
|
|
|
|
192
|
15 |
|
case 0xd6: return $this->unpackExtData(4); |
|
|
|
|
193
|
13 |
|
case 0xd7: return $this->unpackExtData(8); |
|
|
|
|
194
|
11 |
|
case 0xd8: return $this->unpackExtData(16); |
|
|
|
|
195
|
9 |
|
case 0xc7: return $this->unpackExtData($this->unpackUint8()); |
|
|
|
|
196
|
5 |
|
case 0xc8: return $this->unpackExtData($this->unpackUint16()); |
|
|
|
|
197
|
3 |
|
case 0xc9: return $this->unpackExtData($this->unpackUint32()); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
throw UnpackingFailedException::unknownCode($c); |
201
|
|
|
} |
202
|
|
|
|
203
|
3 |
|
public function unpackNil() |
204
|
|
|
{ |
205
|
3 |
|
if (!isset($this->buffer[$this->offset])) { |
206
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
if ("\xc0" === $this->buffer[$this->offset]) { |
210
|
1 |
|
++$this->offset; |
211
|
|
|
|
212
|
1 |
|
return null; |
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
throw UnpackingFailedException::unexpectedCode(\ord($this->buffer[$this->offset++]), 'nil'); |
216
|
|
|
} |
217
|
|
|
|
218
|
5 |
|
public function unpackBool() |
219
|
|
|
{ |
220
|
5 |
|
if (!isset($this->buffer[$this->offset])) { |
221
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
222
|
|
|
} |
223
|
|
|
|
224
|
4 |
|
$c = \ord($this->buffer[$this->offset]); |
225
|
4 |
|
++$this->offset; |
226
|
|
|
|
227
|
4 |
|
if (0xc2 === $c) { |
228
|
2 |
|
return false; |
229
|
|
|
} |
230
|
2 |
|
if (0xc3 === $c) { |
231
|
1 |
|
return true; |
232
|
|
|
} |
233
|
|
|
|
234
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'bool'); |
235
|
|
|
} |
236
|
|
|
|
237
|
40 |
|
public function unpackInt() |
238
|
|
|
{ |
239
|
40 |
|
if (!isset($this->buffer[$this->offset])) { |
240
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
241
|
|
|
} |
242
|
|
|
|
243
|
39 |
|
$c = \ord($this->buffer[$this->offset]); |
244
|
39 |
|
++$this->offset; |
245
|
|
|
|
246
|
|
|
// fixint |
247
|
39 |
|
if ($c <= 0x7f) { |
248
|
3 |
|
return $c; |
249
|
|
|
} |
250
|
|
|
// negfixint |
251
|
36 |
|
if ($c >= 0xe0) { |
252
|
3 |
|
return $c - 0x100; |
253
|
|
|
} |
254
|
|
|
|
255
|
33 |
|
switch ($c) { |
256
|
|
|
// uint |
257
|
33 |
|
case 0xcc: return $this->unpackUint8(); |
|
|
|
|
258
|
30 |
|
case 0xcd: return $this->unpackUint16(); |
|
|
|
|
259
|
27 |
|
case 0xce: return $this->unpackUint32(); |
|
|
|
|
260
|
24 |
|
case 0xcf: return $this->unpackUint64(); |
|
|
|
|
261
|
|
|
|
262
|
|
|
// int |
263
|
20 |
|
case 0xd0: return $this->unpackInt8(); |
|
|
|
|
264
|
16 |
|
case 0xd1: return $this->unpackInt16(); |
|
|
|
|
265
|
12 |
|
case 0xd2: return $this->unpackInt32(); |
|
|
|
|
266
|
8 |
|
case 0xd3: return $this->unpackInt64(); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'int'); |
270
|
|
|
} |
271
|
|
|
|
272
|
7 |
|
public function unpackFloat() |
273
|
|
|
{ |
274
|
7 |
|
if (!isset($this->buffer[$this->offset])) { |
275
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
276
|
|
|
} |
277
|
|
|
|
278
|
6 |
|
$c = \ord($this->buffer[$this->offset]); |
279
|
6 |
|
++$this->offset; |
280
|
|
|
|
281
|
6 |
|
if (0xcb === $c) { |
282
|
3 |
|
return $this->unpackFloat64(); |
283
|
|
|
} |
284
|
3 |
|
if (0xca === $c) { |
285
|
2 |
|
return $this->unpackFloat32(); |
286
|
|
|
} |
287
|
|
|
|
288
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'float'); |
289
|
|
|
} |
290
|
|
|
|
291
|
14 |
|
public function unpackStr() |
292
|
|
|
{ |
293
|
14 |
|
if (!isset($this->buffer[$this->offset])) { |
294
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
295
|
|
|
} |
296
|
|
|
|
297
|
13 |
|
$c = \ord($this->buffer[$this->offset]); |
298
|
13 |
|
++$this->offset; |
299
|
|
|
|
300
|
13 |
View Code Duplication |
if ($c >= 0xa0 && $c <= 0xbf) { |
|
|
|
|
301
|
5 |
|
return ($c & 0x1f) ? $this->unpackStrData($c & 0x1f) : ''; |
302
|
|
|
} |
303
|
8 |
|
if (0xd9 === $c) { |
304
|
4 |
|
return $this->unpackStrData($this->unpackUint8()); |
305
|
|
|
} |
306
|
4 |
|
if (0xda === $c) { |
307
|
2 |
|
return $this->unpackStrData($this->unpackUint16()); |
308
|
|
|
} |
309
|
2 |
|
if (0xdb === $c) { |
310
|
1 |
|
return $this->unpackStrData($this->unpackUint32()); |
311
|
|
|
} |
312
|
|
|
|
313
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'str'); |
314
|
|
|
} |
315
|
|
|
|
316
|
7 |
|
public function unpackBin() |
317
|
|
|
{ |
318
|
7 |
|
if (!isset($this->buffer[$this->offset])) { |
319
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
320
|
|
|
} |
321
|
|
|
|
322
|
6 |
|
$c = \ord($this->buffer[$this->offset]); |
323
|
6 |
|
++$this->offset; |
324
|
|
|
|
325
|
6 |
|
if (0xc4 === $c) { |
326
|
3 |
|
return $this->unpackStrData($this->unpackUint8()); |
327
|
|
|
} |
328
|
3 |
|
if (0xc5 === $c) { |
329
|
1 |
|
return $this->unpackStrData($this->unpackUint16()); |
330
|
|
|
} |
331
|
2 |
|
if (0xc6 === $c) { |
332
|
1 |
|
return $this->unpackStrData($this->unpackUint32()); |
333
|
|
|
} |
334
|
|
|
|
335
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'bin'); |
336
|
|
|
} |
337
|
|
|
|
338
|
9 |
|
public function unpackArray() |
339
|
|
|
{ |
340
|
9 |
|
$size = $this->unpackArrayHeader(); |
341
|
|
|
|
342
|
7 |
|
$array = []; |
343
|
7 |
|
while ($size--) { |
344
|
6 |
|
$array[] = $this->unpack(); |
345
|
|
|
} |
346
|
|
|
|
347
|
7 |
|
return $array; |
348
|
|
|
} |
349
|
|
|
|
350
|
9 |
View Code Duplication |
public function unpackArrayHeader() |
|
|
|
|
351
|
|
|
{ |
352
|
9 |
|
if (!isset($this->buffer[$this->offset])) { |
353
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
354
|
|
|
} |
355
|
|
|
|
356
|
8 |
|
$c = \ord($this->buffer[$this->offset]); |
357
|
8 |
|
++$this->offset; |
358
|
|
|
|
359
|
8 |
|
if ($c >= 0x90 && $c <= 0x9f) { |
360
|
4 |
|
return $c & 0xf; |
361
|
|
|
} |
362
|
4 |
|
if (0xdc === $c) { |
363
|
2 |
|
return $this->unpackUint16(); |
364
|
|
|
} |
365
|
2 |
|
if (0xdd === $c) { |
366
|
1 |
|
return $this->unpackUint32(); |
367
|
|
|
} |
368
|
|
|
|
369
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'array header'); |
370
|
|
|
} |
371
|
|
|
|
372
|
13 |
|
public function unpackMap() |
373
|
|
|
{ |
374
|
13 |
|
$size = $this->unpackMapHeader(); |
375
|
|
|
|
376
|
11 |
|
$map = []; |
377
|
11 |
|
while ($size--) { |
378
|
10 |
|
$map[$this->unpack()] = $this->unpack(); |
379
|
|
|
} |
380
|
|
|
|
381
|
11 |
|
return $map; |
382
|
|
|
} |
383
|
|
|
|
384
|
13 |
View Code Duplication |
public function unpackMapHeader() |
|
|
|
|
385
|
|
|
{ |
386
|
13 |
|
if (!isset($this->buffer[$this->offset])) { |
387
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
388
|
|
|
} |
389
|
|
|
|
390
|
12 |
|
$c = \ord($this->buffer[$this->offset]); |
391
|
12 |
|
++$this->offset; |
392
|
|
|
|
393
|
12 |
|
if ($c >= 0x80 && $c <= 0x8f) { |
394
|
8 |
|
return $c & 0xf; |
395
|
|
|
} |
396
|
4 |
|
if (0xde === $c) { |
397
|
2 |
|
return $this->unpackUint16(); |
398
|
|
|
} |
399
|
2 |
|
if (0xdf === $c) { |
400
|
1 |
|
return $this->unpackUint32(); |
401
|
|
|
} |
402
|
|
|
|
403
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'map header'); |
404
|
|
|
} |
405
|
|
|
|
406
|
10 |
|
public function unpackExt() |
407
|
|
|
{ |
408
|
10 |
|
if (!isset($this->buffer[$this->offset])) { |
409
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
410
|
|
|
} |
411
|
|
|
|
412
|
9 |
|
$c = \ord($this->buffer[$this->offset]); |
413
|
9 |
|
++$this->offset; |
414
|
|
|
|
415
|
9 |
|
switch ($c) { |
416
|
9 |
|
case 0xd4: return $this->unpackExtData(1); |
|
|
|
|
417
|
8 |
|
case 0xd5: return $this->unpackExtData(2); |
|
|
|
|
418
|
7 |
|
case 0xd6: return $this->unpackExtData(4); |
|
|
|
|
419
|
6 |
|
case 0xd7: return $this->unpackExtData(8); |
|
|
|
|
420
|
5 |
|
case 0xd8: return $this->unpackExtData(16); |
|
|
|
|
421
|
4 |
|
case 0xc7: return $this->unpackExtData($this->unpackUint8()); |
|
|
|
|
422
|
3 |
|
case 0xc8: return $this->unpackExtData($this->unpackUint16()); |
|
|
|
|
423
|
2 |
|
case 0xc9: return $this->unpackExtData($this->unpackUint32()); |
|
|
|
|
424
|
|
|
} |
425
|
|
|
|
426
|
1 |
|
throw UnpackingFailedException::unexpectedCode($c, 'ext header'); |
427
|
|
|
} |
428
|
|
|
|
429
|
34 |
|
private function unpackUint8() |
430
|
|
|
{ |
431
|
34 |
|
if (!isset($this->buffer[$this->offset])) { |
432
|
2 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
433
|
|
|
} |
434
|
|
|
|
435
|
32 |
|
return \ord($this->buffer[$this->offset++]); |
436
|
|
|
} |
437
|
|
|
|
438
|
28 |
|
private function unpackUint16() |
439
|
|
|
{ |
440
|
28 |
|
if (!isset($this->buffer[$this->offset + 1])) { |
441
|
2 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 2); |
442
|
|
|
} |
443
|
|
|
|
444
|
26 |
|
$hi = \ord($this->buffer[$this->offset]); |
445
|
26 |
|
$lo = \ord($this->buffer[++$this->offset]); |
446
|
26 |
|
++$this->offset; |
447
|
|
|
|
448
|
26 |
|
return $hi << 8 | $lo; |
449
|
|
|
} |
450
|
|
|
|
451
|
20 |
|
private function unpackUint32() |
452
|
|
|
{ |
453
|
20 |
|
if (!isset($this->buffer[$this->offset + 3])) { |
454
|
2 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 4); |
455
|
|
|
} |
456
|
|
|
|
457
|
18 |
|
$num = \unpack('N', $this->buffer, $this->offset)[1]; |
458
|
18 |
|
$this->offset += 4; |
459
|
|
|
|
460
|
18 |
|
return $num; |
461
|
|
|
} |
462
|
|
|
|
463
|
16 |
View Code Duplication |
private function unpackUint64() |
|
|
|
|
464
|
|
|
{ |
465
|
16 |
|
if (!isset($this->buffer[$this->offset + 7])) { |
466
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 8); |
467
|
|
|
} |
468
|
|
|
|
469
|
15 |
|
$num = \unpack('J', $this->buffer, $this->offset)[1]; |
470
|
15 |
|
$this->offset += 8; |
471
|
|
|
|
472
|
15 |
|
return $num < 0 ? $this->handleIntOverflow($num) : $num; |
473
|
|
|
} |
474
|
|
|
|
475
|
9 |
|
private function unpackInt8() |
476
|
|
|
{ |
477
|
9 |
|
if (!isset($this->buffer[$this->offset])) { |
478
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 1); |
479
|
|
|
} |
480
|
|
|
|
481
|
8 |
|
$num = \ord($this->buffer[$this->offset]); |
482
|
8 |
|
++$this->offset; |
483
|
|
|
|
484
|
8 |
|
return $num > 0x7f ? $num - 0x100 : $num; |
485
|
|
|
} |
486
|
|
|
|
487
|
9 |
|
private function unpackInt16() |
488
|
|
|
{ |
489
|
9 |
|
if (!isset($this->buffer[$this->offset + 1])) { |
490
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 2); |
491
|
|
|
} |
492
|
|
|
|
493
|
8 |
|
$hi = \ord($this->buffer[$this->offset]); |
494
|
8 |
|
$lo = \ord($this->buffer[++$this->offset]); |
495
|
8 |
|
++$this->offset; |
496
|
|
|
|
497
|
8 |
|
return $hi > 0x7f ? $hi << 8 | $lo - 0x10000 : $hi << 8 | $lo; |
498
|
|
|
} |
499
|
|
|
|
500
|
9 |
View Code Duplication |
private function unpackInt32() |
|
|
|
|
501
|
|
|
{ |
502
|
9 |
|
if (!isset($this->buffer[$this->offset + 3])) { |
503
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 4); |
504
|
|
|
} |
505
|
|
|
|
506
|
8 |
|
$num = \unpack('N', $this->buffer, $this->offset)[1]; |
507
|
8 |
|
$this->offset += 4; |
508
|
|
|
|
509
|
8 |
|
return $num > 0x7fffffff ? $num - 0x100000000 : $num; |
510
|
|
|
} |
511
|
|
|
|
512
|
15 |
|
private function unpackInt64() |
513
|
|
|
{ |
514
|
15 |
|
if (!isset($this->buffer[$this->offset + 7])) { |
515
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 8); |
516
|
|
|
} |
517
|
|
|
|
518
|
14 |
|
$num = \unpack('J', $this->buffer, $this->offset)[1]; |
519
|
14 |
|
$this->offset += 8; |
520
|
|
|
|
521
|
14 |
|
return $num; |
522
|
|
|
} |
523
|
|
|
|
524
|
5 |
|
private function unpackFloat32() |
525
|
|
|
{ |
526
|
5 |
|
if (!isset($this->buffer[$this->offset + 3])) { |
527
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 4); |
528
|
|
|
} |
529
|
|
|
|
530
|
4 |
|
$num = \unpack('G', $this->buffer, $this->offset)[1]; |
531
|
4 |
|
$this->offset += 4; |
532
|
|
|
|
533
|
4 |
|
return $num; |
534
|
|
|
} |
535
|
|
|
|
536
|
7 |
|
private function unpackFloat64() |
537
|
|
|
{ |
538
|
7 |
|
if (!isset($this->buffer[$this->offset + 7])) { |
539
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, 8); |
540
|
|
|
} |
541
|
|
|
|
542
|
6 |
|
$num = \unpack('E', $this->buffer, $this->offset)[1]; |
543
|
6 |
|
$this->offset += 8; |
544
|
|
|
|
545
|
6 |
|
return $num; |
546
|
|
|
} |
547
|
|
|
|
548
|
47 |
|
private function unpackStrData($length) |
549
|
|
|
{ |
550
|
47 |
|
if (!isset($this->buffer[$this->offset + $length - 1])) { |
551
|
1 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, $length); |
552
|
|
|
} |
553
|
|
|
|
554
|
47 |
|
$str = \substr($this->buffer, $this->offset, $length); |
555
|
47 |
|
$this->offset += $length; |
556
|
|
|
|
557
|
47 |
|
return $str; |
558
|
|
|
} |
559
|
|
|
|
560
|
11 |
|
private function unpackArrayData($size) |
561
|
|
|
{ |
562
|
11 |
|
$array = []; |
563
|
11 |
|
while ($size--) { |
564
|
11 |
|
$array[] = $this->unpack(); |
565
|
|
|
} |
566
|
|
|
|
567
|
11 |
|
return $array; |
568
|
|
|
} |
569
|
|
|
|
570
|
15 |
|
private function unpackMapData($size) |
571
|
|
|
{ |
572
|
15 |
|
$map = []; |
573
|
15 |
|
while ($size--) { |
574
|
15 |
|
$map[$this->unpack()] = $this->unpack(); |
575
|
|
|
} |
576
|
|
|
|
577
|
15 |
|
return $map; |
578
|
|
|
} |
579
|
|
|
|
580
|
24 |
|
private function unpackExtData($length) |
581
|
|
|
{ |
582
|
24 |
|
if (!isset($this->buffer[$this->offset + $length - 1])) { |
583
|
5 |
|
throw InsufficientDataException::unexpectedLength($this->buffer, $this->offset, $length); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
// int8 |
587
|
19 |
|
$type = \ord($this->buffer[$this->offset]); |
588
|
19 |
|
++$this->offset; |
589
|
|
|
|
590
|
19 |
|
if ($type > 0x7f) { |
591
|
|
|
$type -= 0x100; |
592
|
|
|
} |
593
|
|
|
|
594
|
19 |
|
if (isset($this->transformers[$type])) { |
595
|
1 |
|
return $this->transformers[$type]->unpack($this, $length); |
596
|
|
|
} |
597
|
|
|
|
598
|
18 |
|
$data = \substr($this->buffer, $this->offset, $length); |
599
|
18 |
|
$this->offset += $length; |
600
|
|
|
|
601
|
18 |
|
return new Ext($type, $data); |
602
|
|
|
} |
603
|
|
|
|
604
|
5 |
|
private function handleIntOverflow($value) |
605
|
|
|
{ |
606
|
5 |
|
if ($this->isBigIntAsStr) { |
607
|
3 |
|
return \sprintf('%u', $value); |
608
|
|
|
} |
609
|
2 |
|
if ($this->isBigIntAsGmp) { |
610
|
1 |
|
return \gmp_init(\sprintf('%u', $value)); |
611
|
|
|
} |
612
|
|
|
|
613
|
1 |
|
throw new IntegerOverflowException($value); |
614
|
|
|
} |
615
|
|
|
} |
616
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.