1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MessagePack; |
5
|
|
|
|
6
|
|
|
use MessagePack\{Exception\DecodingFailed, Ext}; |
|
|
|
|
7
|
|
|
use const MessagePack\ORD; |
8
|
|
|
use function MessagePack\{toDouble, toFloat}; |
9
|
|
|
use function sprintf; |
10
|
|
|
use function substr; |
11
|
|
|
|
12
|
|
|
final class Decoder |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
private $data = ''; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $offset = 0; |
19
|
|
|
|
20
|
82 |
|
public function decode(string $data) |
21
|
|
|
{ |
22
|
82 |
|
$this->data = $data; |
23
|
82 |
|
$this->offset = 0; |
24
|
|
|
|
25
|
82 |
|
return $this->parse(); |
26
|
|
|
} |
27
|
|
|
|
28
|
82 |
|
private function parse() |
29
|
|
|
{ |
30
|
82 |
|
if (!isset($this->data[$this->offset])) { |
31
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 1); |
32
|
|
|
} |
33
|
|
|
|
34
|
81 |
|
$byte = ORD[$this->data[$this->offset++]]; |
35
|
|
|
|
36
|
81 |
|
if ($byte < 0xc0) { |
37
|
|
|
// positive fixint |
38
|
15 |
|
if ($byte < 0x80) { |
39
|
11 |
|
return $byte; |
40
|
|
|
} |
41
|
|
|
// fixmap |
42
|
7 |
|
if ($byte < 0x90) { |
43
|
2 |
|
return $this->decodeMap($byte & 0xf); |
44
|
|
|
} |
45
|
|
|
// fixarray |
46
|
5 |
|
if ($byte < 0xa0) { |
47
|
2 |
|
return $this->decodeArray($byte & 0x0f); |
48
|
|
|
} |
49
|
|
|
// fixstr |
50
|
3 |
|
return $this->decodeStr($byte & 0x1f); |
51
|
|
|
} |
52
|
|
|
// negative fixint |
53
|
72 |
|
if ($byte > 0xdf) { |
54
|
2 |
|
return $byte - 0x100; |
55
|
|
|
} |
56
|
|
|
|
57
|
70 |
|
switch ($byte) { |
58
|
70 |
|
case 0xc0: return null; |
|
|
|
|
59
|
69 |
|
case 0xc2: return false; |
|
|
|
|
60
|
68 |
|
case 0xc3: return true; |
|
|
|
|
61
|
|
|
|
62
|
|
|
// bin 8/16/32 |
63
|
66 |
|
case 0xc4: return $this->decodeStr($this->decodeUint8()); |
|
|
|
|
64
|
66 |
|
case 0xc5: return $this->decodeStr($this->decodeUint16()); |
|
|
|
|
65
|
66 |
|
case 0xc6: return $this->decodeStr($this->decodeUint32()); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// float 32/64 |
68
|
66 |
|
case 0xca: return $this->decodeFloat32(); |
|
|
|
|
69
|
61 |
|
case 0xcb: return $this->decodeFloat64(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
// uint 8/16/32/64 |
72
|
54 |
|
case 0xcc: return $this->decodeUint8(); |
|
|
|
|
73
|
51 |
|
case 0xcd: return $this->decodeUint16(); |
|
|
|
|
74
|
48 |
|
case 0xce: return $this->decodeUint32(); |
|
|
|
|
75
|
45 |
|
case 0xcf: return $this->decodeUint64(); |
|
|
|
|
76
|
|
|
|
77
|
|
|
// int 8/16/32/64 |
78
|
42 |
|
case 0xd0: return $this->decodeInt8(); |
|
|
|
|
79
|
39 |
|
case 0xd1: return $this->decodeInt16(); |
|
|
|
|
80
|
36 |
|
case 0xd2: return $this->decodeInt32(); |
|
|
|
|
81
|
33 |
|
case 0xd3: return $this->decodeInt64(); |
|
|
|
|
82
|
|
|
|
83
|
|
|
// str 8/16/32 |
84
|
29 |
|
case 0xd9: return $this->decodeStr($this->decodeUint8()); |
|
|
|
|
85
|
27 |
|
case 0xda: return $this->decodeStr($this->decodeUint16()); |
|
|
|
|
86
|
25 |
|
case 0xdb: return $this->decodeStr($this->decodeUint32()); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// array 16/32 |
89
|
24 |
|
case 0xdc: return $this->decodeArray($this->decodeUint16()); |
|
|
|
|
90
|
22 |
|
case 0xdd: return $this->decodeArray($this->decodeUint32()); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// map 16/32 |
93
|
21 |
|
case 0xde: return $this->decodeMap($this->decodeUint16()); |
|
|
|
|
94
|
19 |
|
case 0xdf: return $this->decodeMap($this->decodeUint32()); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// fixext 1-16 |
97
|
18 |
|
case 0xd4: return $this->decodeExt(1); |
|
|
|
|
98
|
16 |
|
case 0xd5: return $this->decodeExt(2); |
|
|
|
|
99
|
14 |
|
case 0xd6: return $this->decodeExt(4); |
|
|
|
|
100
|
12 |
|
case 0xd7: return $this->decodeExt(8); |
|
|
|
|
101
|
10 |
|
case 0xd8: return $this->decodeExt(16); |
|
|
|
|
102
|
|
|
|
103
|
|
|
// ext 8/16/32 |
104
|
8 |
|
case 0xc7: return $this->decodeExt($this->decodeUint8()); |
|
|
|
|
105
|
6 |
|
case 0xc8: return $this->decodeExt($this->decodeUint16()); |
|
|
|
|
106
|
3 |
|
case 0xc9: return $this->decodeExt($this->decodeUint32()); |
|
|
|
|
107
|
|
|
} |
|
|
|
|
108
|
|
|
|
109
|
1 |
|
throw DecodingFailed::unknownByteHeader($byte, $this->offset); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
private function decodeFloat32(): float |
113
|
|
|
{ |
114
|
5 |
|
if (!isset($this->data[$this->offset + 3])) { |
115
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 4); |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
$num = ORD[$this->data[$this->offset++]] * 0x1000000 |
119
|
4 |
|
| ORD[$this->data[$this->offset++]] << 16 |
120
|
4 |
|
| ORD[$this->data[$this->offset++]] << 8 |
121
|
4 |
|
| ORD[$this->data[$this->offset++]]; |
122
|
|
|
|
123
|
4 |
|
return toFloat($num); |
124
|
|
|
} |
125
|
|
|
|
126
|
7 |
|
private function decodeFloat64(): float |
127
|
|
|
{ |
128
|
7 |
|
if (!isset($this->data[$this->offset + 7])) { |
129
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 8); |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
$x = ORD[$this->data[$this->offset++]] * 0x1000000 |
133
|
6 |
|
| ORD[$this->data[$this->offset++]] << 16 |
134
|
6 |
|
| ORD[$this->data[$this->offset++]] << 8 |
135
|
6 |
|
| ORD[$this->data[$this->offset++]]; |
136
|
|
|
|
137
|
6 |
|
$y = ORD[$this->data[$this->offset++]] * 0x1000000 |
138
|
6 |
|
| ORD[$this->data[$this->offset++]] << 16 |
139
|
6 |
|
| ORD[$this->data[$this->offset++]] << 8 |
140
|
6 |
|
| ORD[$this->data[$this->offset++]]; |
141
|
|
|
|
142
|
6 |
|
return toDouble($y, $x); |
143
|
|
|
} |
144
|
|
|
|
145
|
9 |
|
private function decodeUint8(): int |
146
|
|
|
{ |
147
|
9 |
|
if (!isset($this->data[$this->offset])) { |
148
|
2 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 1); |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
return ORD($this->data[$this->offset++]); |
152
|
|
|
} |
153
|
|
|
|
154
|
13 |
|
private function decodeUint16(): int |
155
|
|
|
{ |
156
|
13 |
|
if (!isset($this->data[$this->offset + 1])) { |
157
|
2 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 2); |
158
|
|
|
} |
159
|
|
|
|
160
|
11 |
|
return ORD[$this->data[$this->offset++]] << 8 |
161
|
11 |
|
| ORD[$this->data[$this->offset++]]; |
162
|
|
|
} |
163
|
|
|
|
164
|
8 |
|
private function decodeUint32(): int |
165
|
|
|
{ |
166
|
8 |
|
if (!isset($this->data[$this->offset + 3])) { |
167
|
2 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 4); |
168
|
|
|
} |
169
|
|
|
|
170
|
6 |
|
return ORD[$this->data[$this->offset++]] * 0x1000000 |
171
|
6 |
|
| ORD[$this->data[$this->offset++]] << 16 |
172
|
6 |
|
| ORD[$this->data[$this->offset++]] << 8 |
173
|
6 |
|
| ORD[$this->data[$this->offset++]]; |
174
|
|
|
} |
175
|
|
|
|
176
|
3 |
|
private function decodeUint64() |
177
|
|
|
{ |
178
|
3 |
|
if (!isset($this->data[$this->offset + 7])) { |
179
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 8); |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
$num = (ORD[$this->data[$this->offset++]] * 0x1000000 |
183
|
2 |
|
| ORD[$this->data[$this->offset++]] << 16 |
184
|
2 |
|
| ORD[$this->data[$this->offset++]] << 8 |
185
|
2 |
|
| ORD[$this->data[$this->offset++]]) * 0x100000000 |
186
|
2 |
|
| ORD[$this->data[$this->offset++]] * 0x1000000 |
187
|
2 |
|
| ORD[$this->data[$this->offset++]] << 16 |
188
|
2 |
|
| ORD[$this->data[$this->offset++]] << 8 |
189
|
2 |
|
| ORD[$this->data[$this->offset++]]; |
190
|
|
|
|
191
|
2 |
|
return $num < 0 ? sprintf('%u', $num) : $num; |
192
|
|
|
} |
193
|
|
|
|
194
|
12 |
|
private function decodeInt8(): int |
195
|
|
|
{ |
196
|
12 |
|
if (!isset($this->data[$this->offset])) { |
197
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 1); |
198
|
|
|
} |
199
|
|
|
|
200
|
11 |
|
$num = ORD[$this->data[$this->offset++]]; |
201
|
|
|
|
202
|
11 |
|
return $num & 0x80 ? $num - 0x100 : $num; |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
private function decodeInt16(): int |
206
|
|
|
{ |
207
|
3 |
|
if (!isset($this->data[$this->offset + 1])) { |
208
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 2); |
209
|
|
|
} |
210
|
|
|
|
211
|
2 |
|
$num = ORD[$this->data[$this->offset++]] << 8 |
212
|
2 |
|
| ORD[$this->data[$this->offset++]]; |
213
|
|
|
|
214
|
2 |
|
return $num & 0x8000 ? $num - 0x10000 : $num; |
215
|
|
|
} |
216
|
|
|
|
217
|
3 |
|
private function decodeInt32(): int |
218
|
|
|
{ |
219
|
3 |
|
if (!isset($this->data[$this->offset + 3])) { |
220
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 4); |
221
|
|
|
} |
222
|
|
|
|
223
|
2 |
|
$num = ORD[$this->data[$this->offset++]] * 0x1000000 |
224
|
2 |
|
| ORD[$this->data[$this->offset++]] << 16 |
225
|
2 |
|
| ORD[$this->data[$this->offset++]] << 8 |
226
|
2 |
|
| ORD[$this->data[$this->offset++]]; |
227
|
|
|
|
228
|
2 |
|
return $num & 0x80000000 ? $num - 0x100000000 : $num; |
229
|
|
|
} |
230
|
|
|
|
231
|
4 |
|
private function decodeInt64(): int |
232
|
|
|
{ |
233
|
4 |
|
if (!isset($this->data[$this->offset + 7])) { |
234
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 8); |
235
|
|
|
} |
236
|
|
|
|
237
|
3 |
|
$num = ORD[$this->data[$this->offset++]]; |
238
|
|
|
|
239
|
3 |
|
$negate = ($num & 0x80) === 0x80; |
240
|
|
|
|
241
|
3 |
|
if ($negate) { |
242
|
2 |
|
$num = (($num ^ 0xff) * 0x100000000000000) |
243
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x1000000000000 |
244
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x10000000000 |
245
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x100000000 |
246
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x1000000 |
247
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x10000 |
248
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x100 |
249
|
2 |
|
| ORD[$this->data[$this->offset++]] ^ 0xff; |
250
|
|
|
|
251
|
2 |
|
return ~$num; |
252
|
|
|
} |
253
|
|
|
|
254
|
1 |
|
$num = ($num * 0x100000000000000) |
255
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x1000000000000 |
256
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x10000000000 |
257
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x100000000 |
258
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x1000000 |
259
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x10000 |
260
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x100 |
261
|
1 |
|
| ORD[$this->data[$this->offset++]]; |
262
|
|
|
|
263
|
1 |
|
return $num; |
264
|
|
|
} |
265
|
|
|
|
266
|
8 |
|
private function decodeStr(int $length): string |
267
|
|
|
{ |
268
|
8 |
|
if (0 === $length) { |
269
|
1 |
|
return ''; |
270
|
|
|
} |
271
|
|
|
|
272
|
7 |
|
if (!isset($this->data[$this->offset + $length - 1])) { |
273
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, $length); |
274
|
|
|
} |
275
|
|
|
|
276
|
6 |
|
$str = substr($this->data, $this->offset++, $length); |
277
|
6 |
|
$this->offset += $length; |
278
|
|
|
|
279
|
6 |
|
return $str; |
280
|
|
|
} |
281
|
|
|
|
282
|
5 |
|
private function decodeArray(int $size): array |
283
|
|
|
{ |
284
|
5 |
|
$array = []; |
285
|
5 |
|
while ($size--) { |
286
|
4 |
|
$array[] = $this->parse(); |
287
|
|
|
} |
288
|
|
|
|
289
|
5 |
|
return $array; |
290
|
|
|
} |
291
|
|
|
|
292
|
5 |
|
private function decodeMap(int $size): array |
293
|
|
|
{ |
294
|
5 |
|
$map = []; |
295
|
5 |
|
while ($size--) { |
296
|
5 |
|
$map[$this->parse()] = $this->parse(); |
297
|
|
|
} |
298
|
|
|
|
299
|
5 |
|
return $map; |
300
|
|
|
} |
301
|
|
|
|
302
|
14 |
|
private function decodeExt(int $length): Ext |
303
|
|
|
{ |
304
|
14 |
|
if (!isset($this->data[$this->offset + $length - 1])) { |
305
|
5 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, $length); |
306
|
|
|
} |
307
|
|
|
|
308
|
9 |
|
$type = $this->decodeInt8(); |
309
|
|
|
|
310
|
9 |
|
$data = substr($this->data, $this->offset++, $length); |
311
|
9 |
|
$this->offset += $length; |
312
|
|
|
|
313
|
9 |
|
return Ext::make($type, $data); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: