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