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\Collection; |
18
|
|
|
|
19
|
|
|
class BufferUnpacker |
20
|
|
|
{ |
21
|
|
|
const INT_AS_EXCEPTION = 0; |
22
|
|
|
const INT_AS_STR = 1; |
23
|
|
|
const INT_AS_GMP = 2; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $intOverflowMode = self::INT_AS_EXCEPTION; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $buffer = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $offset = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Collection |
42
|
|
|
*/ |
43
|
|
|
private $transformers; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int|null $intOverflowMode |
47
|
|
|
*/ |
48
|
97 |
|
public function __construct($intOverflowMode = null) |
49
|
|
|
{ |
50
|
97 |
|
if (null !== $intOverflowMode) { |
51
|
2 |
|
$this->intOverflowMode = $intOverflowMode; |
52
|
2 |
|
} |
53
|
97 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Collection|null $transformers |
57
|
|
|
*/ |
58
|
2 |
|
public function setTransformers(Collection $transformers = null) |
59
|
|
|
{ |
60
|
2 |
|
$this->transformers = $transformers; |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Collection|null |
65
|
|
|
*/ |
66
|
1 |
|
public function getTransformers() |
67
|
|
|
{ |
68
|
1 |
|
return $this->transformers; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $intOverflowMode |
73
|
|
|
* |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
2 |
|
public function setIntOverflowMode($intOverflowMode) |
77
|
|
|
{ |
78
|
2 |
|
if (!\in_array($intOverflowMode, [ |
79
|
2 |
|
self::INT_AS_EXCEPTION, |
80
|
2 |
|
self::INT_AS_STR, |
81
|
2 |
|
self::INT_AS_GMP, |
82
|
2 |
|
], true)) { |
83
|
1 |
|
throw new \InvalidArgumentException(\sprintf('Invalid integer overflow mode: %s.', $intOverflowMode)); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$this->intOverflowMode = $intOverflowMode; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
1 |
|
public function getIntOverflowMode() |
93
|
|
|
{ |
94
|
1 |
|
return $this->intOverflowMode; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $data |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
5 |
|
public function append($data) |
103
|
|
|
{ |
104
|
5 |
|
$this->buffer .= $data; |
105
|
|
|
|
106
|
5 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $buffer |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
89 |
|
public function reset($buffer = '') |
115
|
|
|
{ |
116
|
89 |
|
$this->buffer = $buffer; |
117
|
89 |
|
$this->offset = 0; |
118
|
|
|
|
119
|
89 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
3 |
|
public function tryUnpack() |
126
|
|
|
{ |
127
|
3 |
|
$data = []; |
128
|
3 |
|
$offset = $this->offset; |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
do { |
132
|
3 |
|
$data[] = $this->unpack(); |
133
|
3 |
|
$offset = $this->offset; |
134
|
3 |
|
} while (isset($this->buffer[$this->offset])); |
135
|
3 |
|
} catch (InsufficientDataException $e) { |
136
|
1 |
|
$this->offset = $offset; |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
if ($this->offset) { |
140
|
3 |
|
$this->buffer = isset($this->buffer[$this->offset]) ? \substr($this->buffer, $this->offset) : ''; |
141
|
3 |
|
$this->offset = 0; |
142
|
3 |
|
} |
143
|
|
|
|
144
|
3 |
|
return $data; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return mixed |
149
|
|
|
* |
150
|
|
|
* @throws UnpackingFailedException |
151
|
|
|
*/ |
152
|
93 |
|
public function unpack() |
153
|
|
|
{ |
154
|
93 |
|
$this->ensureLength(1); |
155
|
|
|
|
156
|
91 |
|
$c = \ord($this->buffer[$this->offset]); |
157
|
91 |
|
$this->offset += 1; |
158
|
|
|
|
159
|
|
|
// fixint |
160
|
91 |
|
if ($c <= 0x7f) { |
161
|
20 |
|
return $c; |
162
|
|
|
} |
163
|
|
|
// fixstr |
164
|
88 |
|
if ($c >= 0xa0 && $c <= 0xbf) { |
165
|
12 |
|
return $this->unpackStr($c & 0x1f); |
166
|
|
|
} |
167
|
|
|
// fixarray |
168
|
83 |
|
if ($c >= 0x90 && $c <= 0x9f) { |
169
|
6 |
|
return $this->unpackArray($c & 0xf); |
170
|
|
|
} |
171
|
|
|
// fixmap |
172
|
80 |
|
if ($c >= 0x80 && $c <= 0x8f) { |
173
|
11 |
|
return $this->unpackMap($c & 0xf); |
174
|
|
|
} |
175
|
|
|
// negfixint |
176
|
76 |
|
if ($c >= 0xe0) { |
177
|
4 |
|
return $c - 256; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
switch ($c) { |
181
|
72 |
|
case 0xc0: return null; |
|
|
|
|
182
|
71 |
|
case 0xc2: return false; |
|
|
|
|
183
|
69 |
|
case 0xc3: return true; |
|
|
|
|
184
|
|
|
|
185
|
|
|
// MP_BIN |
186
|
64 |
|
case 0xc4: return $this->unpackStr($this->unpackU8()); |
|
|
|
|
187
|
60 |
|
case 0xc5: return $this->unpackStr($this->unpackU16()); |
|
|
|
|
188
|
59 |
|
case 0xc6: return $this->unpackStr($this->unpackU32()); |
|
|
|
|
189
|
|
|
|
190
|
|
|
// MP_FLOAT |
191
|
58 |
|
case 0xca: return $this->unpackFloat(); |
|
|
|
|
192
|
56 |
|
case 0xcb: return $this->unpackDouble(); |
|
|
|
|
193
|
|
|
|
194
|
|
|
// MP_UINT |
195
|
53 |
|
case 0xcc: return $this->unpackU8(); |
|
|
|
|
196
|
50 |
|
case 0xcd: return $this->unpackU16(); |
|
|
|
|
197
|
46 |
|
case 0xce: return $this->unpackU32(); |
|
|
|
|
198
|
43 |
|
case 0xcf: return $this->unpackU64(); |
|
|
|
|
199
|
|
|
|
200
|
|
|
// MP_INT |
201
|
36 |
|
case 0xd0: return $this->unpackI8(); |
|
|
|
|
202
|
33 |
|
case 0xd1: return $this->unpackI16(); |
|
|
|
|
203
|
30 |
|
case 0xd2: return $this->unpackI32(); |
|
|
|
|
204
|
27 |
|
case 0xd3: return $this->unpackI64(); |
|
|
|
|
205
|
|
|
|
206
|
|
|
// MP_STR |
207
|
24 |
|
case 0xd9: return $this->unpackStr($this->unpackU8()); |
|
|
|
|
208
|
20 |
|
case 0xda: return $this->unpackStr($this->unpackU16()); |
|
|
|
|
209
|
18 |
|
case 0xdb: return $this->unpackStr($this->unpackU32()); |
|
|
|
|
210
|
|
|
|
211
|
|
|
// MP_ARRAY |
212
|
17 |
|
case 0xdc: return $this->unpackArray($this->unpackU16()); |
|
|
|
|
213
|
15 |
|
case 0xdd: return $this->unpackArray($this->unpackU32()); |
|
|
|
|
214
|
|
|
|
215
|
|
|
// MP_MAP |
216
|
14 |
|
case 0xde: return $this->unpackMap($this->unpackU16()); |
|
|
|
|
217
|
12 |
|
case 0xdf: return $this->unpackMap($this->unpackU32()); |
|
|
|
|
218
|
|
|
|
219
|
|
|
// MP_EXT |
220
|
11 |
|
case 0xd4: return $this->unpackExt(1); |
|
|
|
|
221
|
9 |
|
case 0xd5: return $this->unpackExt(2); |
|
|
|
|
222
|
8 |
|
case 0xd6: return $this->unpackExt(4); |
|
|
|
|
223
|
7 |
|
case 0xd7: return $this->unpackExt(8); |
|
|
|
|
224
|
6 |
|
case 0xd8: return $this->unpackExt(16); |
|
|
|
|
225
|
5 |
|
case 0xc7: return $this->unpackExt($this->unpackU8()); |
|
|
|
|
226
|
3 |
|
case 0xc8: return $this->unpackExt($this->unpackU16()); |
|
|
|
|
227
|
2 |
|
case 0xc9: return $this->unpackExt($this->unpackU32()); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
1 |
|
throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c)); |
231
|
|
|
} |
232
|
|
|
|
233
|
16 |
|
private function unpackU8() |
234
|
|
|
{ |
235
|
16 |
|
$this->ensureLength(1); |
236
|
|
|
|
237
|
16 |
|
$num = $this->buffer[$this->offset]; |
238
|
16 |
|
$this->offset += 1; |
239
|
|
|
|
240
|
16 |
|
$num = \unpack('C', $num); |
241
|
|
|
|
242
|
16 |
|
return $num[1]; |
243
|
|
|
} |
244
|
|
|
|
245
|
13 |
View Code Duplication |
private function unpackU16() |
|
|
|
|
246
|
|
|
{ |
247
|
13 |
|
$this->ensureLength(2); |
248
|
|
|
|
249
|
13 |
|
$num = $this->buffer[$this->offset].$this->buffer[$this->offset + 1]; |
250
|
13 |
|
$this->offset += 2; |
251
|
|
|
|
252
|
13 |
|
$num = \unpack('n', $num); |
253
|
|
|
|
254
|
13 |
|
return $num[1]; |
255
|
|
|
} |
256
|
|
|
|
257
|
9 |
View Code Duplication |
private function unpackU32() |
|
|
|
|
258
|
|
|
{ |
259
|
9 |
|
$this->ensureLength(4); |
260
|
|
|
|
261
|
9 |
|
$num = \substr($this->buffer, $this->offset, 4); |
262
|
9 |
|
$this->offset += 4; |
263
|
|
|
|
264
|
9 |
|
$num = \unpack('N', $num); |
265
|
|
|
|
266
|
9 |
|
return $num[1]; |
267
|
|
|
} |
268
|
|
|
|
269
|
7 |
|
private function unpackU64() |
270
|
|
|
{ |
271
|
7 |
|
$this->ensureLength(8); |
272
|
|
|
|
273
|
7 |
|
$num = \substr($this->buffer, $this->offset, 8); |
274
|
7 |
|
$this->offset += 8; |
275
|
|
|
|
276
|
|
|
//$num = \unpack('J', $num); |
|
|
|
|
277
|
|
|
|
278
|
7 |
|
$set = \unpack('N2', $num); |
279
|
7 |
|
$value = $set[1] << 32 | $set[2]; |
280
|
|
|
|
281
|
|
|
// PHP does not support unsigned integers. |
282
|
|
|
// If a number is bigger than 2^63, it will be interpreted as a float. |
283
|
|
|
// @link http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow |
284
|
|
|
|
285
|
7 |
|
return ($value < 0) ? $this->handleIntOverflow($value) : $value; |
286
|
|
|
} |
287
|
|
|
|
288
|
13 |
|
private function unpackI8() |
289
|
|
|
{ |
290
|
13 |
|
$this->ensureLength(1); |
291
|
|
|
|
292
|
13 |
|
$num = $this->buffer[$this->offset]; |
293
|
13 |
|
$this->offset += 1; |
294
|
|
|
|
295
|
13 |
|
$num = \unpack('c', $num); |
296
|
|
|
|
297
|
13 |
|
return $num[1]; |
298
|
|
|
} |
299
|
|
|
|
300
|
3 |
View Code Duplication |
private function unpackI16() |
|
|
|
|
301
|
|
|
{ |
302
|
3 |
|
$this->ensureLength(2); |
303
|
|
|
|
304
|
3 |
|
$num = $this->buffer[$this->offset].$this->buffer[$this->offset + 1]; |
305
|
3 |
|
$this->offset += 2; |
306
|
|
|
|
307
|
3 |
|
$num = \unpack('s', \strrev($num)); |
308
|
|
|
|
309
|
3 |
|
return $num[1]; |
310
|
|
|
} |
311
|
|
|
|
312
|
3 |
|
private function unpackI32() |
313
|
|
|
{ |
314
|
3 |
|
$this->ensureLength(4); |
315
|
|
|
|
316
|
3 |
|
$num = \substr($this->buffer, $this->offset, 4); |
317
|
3 |
|
$this->offset += 4; |
318
|
|
|
|
319
|
3 |
|
$num = \unpack('i', \strrev($num)); |
320
|
|
|
|
321
|
3 |
|
return $num[1]; |
322
|
|
|
} |
323
|
|
|
|
324
|
3 |
View Code Duplication |
private function unpackI64() |
|
|
|
|
325
|
|
|
{ |
326
|
3 |
|
$this->ensureLength(8); |
327
|
|
|
|
328
|
3 |
|
$num = \substr($this->buffer, $this->offset, 8); |
329
|
3 |
|
$this->offset += 8; |
330
|
|
|
|
331
|
3 |
|
$set = \unpack('N2', $num); |
332
|
|
|
|
333
|
3 |
|
return $set[1] << 32 | $set[2]; |
334
|
|
|
} |
335
|
|
|
|
336
|
2 |
|
private function unpackFloat() |
337
|
|
|
{ |
338
|
2 |
|
$this->ensureLength(4); |
339
|
|
|
|
340
|
2 |
|
$num = \substr($this->buffer, $this->offset, 4); |
341
|
2 |
|
$this->offset += 4; |
342
|
|
|
|
343
|
2 |
|
$num = \unpack('f', \strrev($num)); |
344
|
|
|
|
345
|
2 |
|
return $num[1]; |
346
|
|
|
} |
347
|
|
|
|
348
|
3 |
|
private function unpackDouble() |
349
|
|
|
{ |
350
|
3 |
|
$this->ensureLength(8); |
351
|
|
|
|
352
|
3 |
|
$num = \substr($this->buffer, $this->offset, 8); |
353
|
3 |
|
$this->offset += 8; |
354
|
|
|
|
355
|
3 |
|
$num = \unpack('d', \strrev($num)); |
356
|
|
|
|
357
|
3 |
|
return $num[1]; |
358
|
|
|
} |
359
|
|
|
|
360
|
25 |
|
private function unpackStr($length) |
361
|
|
|
{ |
362
|
25 |
|
if (!$length) { |
363
|
1 |
|
return ''; |
364
|
|
|
} |
365
|
|
|
|
366
|
24 |
|
$this->ensureLength($length); |
367
|
|
|
|
368
|
24 |
|
$str = \substr($this->buffer, $this->offset, $length); |
369
|
24 |
|
$this->offset += $length; |
370
|
|
|
|
371
|
24 |
|
return $str; |
372
|
|
|
} |
373
|
|
|
|
374
|
9 |
|
private function unpackArray($size) |
375
|
|
|
{ |
376
|
9 |
|
$array = []; |
377
|
|
|
|
378
|
9 |
|
for ($i = $size; $i; $i--) { |
379
|
8 |
|
$array[] = $this->unpack(); |
380
|
8 |
|
} |
381
|
|
|
|
382
|
9 |
|
return $array; |
383
|
|
|
} |
384
|
|
|
|
385
|
14 |
|
private function unpackMap($size) |
386
|
|
|
{ |
387
|
14 |
|
$map = []; |
388
|
|
|
|
389
|
14 |
|
for ($i = $size; $i; $i--) { |
390
|
13 |
|
$key = $this->unpack(); |
391
|
13 |
|
$value = $this->unpack(); |
392
|
|
|
|
393
|
13 |
|
$map[$key] = $value; |
394
|
13 |
|
} |
395
|
|
|
|
396
|
14 |
|
return $map; |
397
|
|
|
} |
398
|
|
|
|
399
|
10 |
|
private function unpackExt($length) |
400
|
|
|
{ |
401
|
10 |
|
$this->ensureLength($length); |
402
|
|
|
|
403
|
10 |
|
$type = $this->unpackI8(); |
404
|
10 |
|
$data = \substr($this->buffer, $this->offset, $length); |
405
|
|
|
|
406
|
10 |
|
if ($this->transformers && $transformer = $this->transformers->find($type)) { |
407
|
1 |
|
return $transformer->reverseTransform($this->unpack()); |
408
|
|
|
} |
409
|
|
|
|
410
|
9 |
|
$this->offset += $length; |
411
|
|
|
|
412
|
9 |
|
return new Ext($type, $data); |
413
|
|
|
} |
414
|
|
|
|
415
|
93 |
|
private function ensureLength($length) |
416
|
|
|
{ |
417
|
93 |
|
if (!isset($this->buffer[$this->offset + $length - 1])) { |
418
|
4 |
|
throw new InsufficientDataException($length, \strlen($this->buffer) - $this->offset); |
419
|
|
|
} |
420
|
91 |
|
} |
421
|
|
|
|
422
|
3 |
|
private function handleIntOverflow($value) |
423
|
|
|
{ |
424
|
3 |
|
if (self::INT_AS_STR === $this->intOverflowMode) { |
425
|
1 |
|
return \sprintf('%u', $value); |
426
|
|
|
} |
427
|
2 |
|
if (self::INT_AS_GMP === $this->intOverflowMode) { |
428
|
1 |
|
return \gmp_init(\sprintf('%u', $value)); |
429
|
|
|
} |
430
|
|
|
|
431
|
1 |
|
throw new IntegerOverflowException($value); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.