1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MessagePack; |
5
|
|
|
|
6
|
|
|
use MessagePack\Exception\DecodingFailed; |
7
|
|
|
use const MessagePack\ORD; |
8
|
|
|
use function MessagePack\{toDouble, toFloat}; |
9
|
|
|
|
10
|
|
|
final class Decoder |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
private $data = ''; |
14
|
|
|
|
15
|
|
|
/** @var int */ |
16
|
|
|
private $offset = 0; |
17
|
|
|
|
18
|
65 |
|
public function decode(string $data) |
19
|
|
|
{ |
20
|
65 |
|
$this->data = $data; |
21
|
65 |
|
$this->offset = 0; |
22
|
|
|
|
23
|
65 |
|
return $this->parse(); |
24
|
|
|
} |
25
|
|
|
|
26
|
65 |
|
private function parse() |
27
|
|
|
{ |
28
|
65 |
|
if (!isset($this->data[$this->offset])) { |
29
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 1); |
30
|
|
|
} |
31
|
|
|
|
32
|
64 |
|
$byte = ORD[$this->data[$this->offset++]]; |
33
|
|
|
|
34
|
64 |
|
if ($byte < 0xc0) { |
35
|
|
|
// positive fixint |
36
|
15 |
|
if ($byte < 0x80) { |
37
|
11 |
|
return $byte; |
38
|
|
|
} |
39
|
|
|
// fixmap |
40
|
7 |
|
if ($byte < 0x90) { |
41
|
2 |
|
return $this->decodeMap($byte & 0xf); |
42
|
|
|
} |
43
|
|
|
// fixarray |
44
|
5 |
|
if ($byte < 0xa0) { |
45
|
2 |
|
return $this->decodeArray($byte & 0x0f); |
46
|
|
|
} |
47
|
|
|
// fixstr |
48
|
3 |
|
return $this->decodeStr($byte & 0x1f); |
49
|
|
|
} |
50
|
|
|
// negative fixint |
51
|
55 |
|
if ($byte > 0xdf) { |
52
|
2 |
|
return $byte - 0x100; |
53
|
|
|
} |
54
|
|
|
|
55
|
53 |
|
switch ($byte) { |
56
|
53 |
|
case 0xc0: return null; |
|
|
|
|
57
|
52 |
|
case 0xc2: return false; |
|
|
|
|
58
|
51 |
|
case 0xc3: return true; |
|
|
|
|
59
|
|
|
|
60
|
|
|
// bin 8/16/32 |
61
|
49 |
|
case 0xc4: return $this->decodeStr($this->decodeUint8()); |
|
|
|
|
62
|
49 |
|
case 0xc5: return $this->decodeStr($this->decodeUint16()); |
|
|
|
|
63
|
49 |
|
case 0xc6: return $this->decodeStr($this->decodeUint32()); |
|
|
|
|
64
|
|
|
|
65
|
|
|
// float 32/64 |
66
|
49 |
|
case 0xca: return $this->decodeFloat32(); |
|
|
|
|
67
|
44 |
|
case 0xcb: return $this->decodeFloat64(); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// uint 8/16/32/64 |
70
|
37 |
|
case 0xcc: return $this->decodeUint8(); |
|
|
|
|
71
|
34 |
|
case 0xcd: return $this->decodeUint16(); |
|
|
|
|
72
|
31 |
|
case 0xce: return $this->decodeUint32(); |
|
|
|
|
73
|
28 |
|
case 0xcf: return $this->decodeUint64(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// int 8/16/32/64 |
76
|
25 |
|
case 0xd0: return $this->decodeInt8(); |
|
|
|
|
77
|
22 |
|
case 0xd1: return $this->decodeInt16(); |
|
|
|
|
78
|
19 |
|
case 0xd2: return $this->decodeInt32(); |
|
|
|
|
79
|
16 |
|
case 0xd3: return $this->decodeInt64(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
// str 8/16/32 |
82
|
12 |
|
case 0xd9: return $this->decodeStr($this->decodeUint8()); |
|
|
|
|
83
|
10 |
|
case 0xda: return $this->decodeStr($this->decodeUint16()); |
|
|
|
|
84
|
8 |
|
case 0xdb: return $this->decodeStr($this->decodeUint32()); |
|
|
|
|
85
|
|
|
|
86
|
|
|
// array 16/32 |
87
|
7 |
|
case 0xdc: return $this->decodeArray($this->decodeUint16()); |
|
|
|
|
88
|
5 |
|
case 0xdd: return $this->decodeArray($this->decodeUint32()); |
|
|
|
|
89
|
|
|
|
90
|
|
|
// map 16/32 |
91
|
4 |
|
case 0xde: return $this->decodeMap($this->decodeUint16()); |
|
|
|
|
92
|
2 |
|
case 0xdf: return $this->decodeMap($this->decodeUint32()); |
|
|
|
|
93
|
|
|
} |
|
|
|
|
94
|
|
|
|
95
|
1 |
|
throw DecodingFailed::unknownByteHeader($byte, $this->offset); |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
private function decodeFloat32(): float |
99
|
|
|
{ |
100
|
5 |
|
if (!isset($this->data[$this->offset + 3])) { |
101
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 4); |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
$num = ORD[$this->data[$this->offset++]] * 0x1000000 |
105
|
4 |
|
| ORD[$this->data[$this->offset++]] << 16 |
106
|
4 |
|
| ORD[$this->data[$this->offset++]] << 8 |
107
|
4 |
|
| ORD[$this->data[$this->offset++]]; |
108
|
|
|
|
109
|
4 |
|
return toFloat($num); |
110
|
|
|
} |
111
|
|
|
|
112
|
7 |
|
private function decodeFloat64(): float |
113
|
|
|
{ |
114
|
7 |
|
if (!isset($this->data[$this->offset + 7])) { |
115
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 8); |
116
|
|
|
} |
117
|
|
|
|
118
|
6 |
|
$x = ORD[$this->data[$this->offset++]] * 0x1000000 |
119
|
6 |
|
| ORD[$this->data[$this->offset++]] << 16 |
120
|
6 |
|
| ORD[$this->data[$this->offset++]] << 8 |
121
|
6 |
|
| ORD[$this->data[$this->offset++]]; |
122
|
|
|
|
123
|
6 |
|
$y = ORD[$this->data[$this->offset++]] * 0x1000000 |
124
|
6 |
|
| ORD[$this->data[$this->offset++]] << 16 |
125
|
6 |
|
| ORD[$this->data[$this->offset++]] << 8 |
126
|
6 |
|
| ORD[$this->data[$this->offset++]]; |
127
|
|
|
|
128
|
6 |
|
return toDouble($y, $x); |
129
|
|
|
} |
130
|
|
|
|
131
|
7 |
|
private function decodeUint8(): int |
132
|
|
|
{ |
133
|
7 |
|
if (!isset($this->data[$this->offset])) { |
134
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 1); |
135
|
|
|
} |
136
|
|
|
|
137
|
6 |
|
return ORD($this->data[$this->offset++]); |
138
|
|
|
} |
139
|
|
|
|
140
|
10 |
|
private function decodeUint16(): int |
141
|
|
|
{ |
142
|
10 |
|
if (!isset($this->data[$this->offset + 1])) { |
143
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 2); |
144
|
|
|
} |
145
|
|
|
|
146
|
9 |
|
return ORD[$this->data[$this->offset++]] << 8 |
147
|
9 |
|
| ORD[$this->data[$this->offset++]]; |
148
|
|
|
} |
149
|
|
|
|
150
|
6 |
|
private function decodeUint32(): int |
151
|
|
|
{ |
152
|
6 |
|
if (!isset($this->data[$this->offset + 3])) { |
153
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 4); |
154
|
|
|
} |
155
|
|
|
|
156
|
5 |
|
return ORD[$this->data[$this->offset++]] * 0x1000000 |
157
|
5 |
|
| ORD[$this->data[$this->offset++]] << 16 |
158
|
5 |
|
| ORD[$this->data[$this->offset++]] << 8 |
159
|
5 |
|
| ORD[$this->data[$this->offset++]]; |
160
|
|
|
} |
161
|
|
|
|
162
|
3 |
|
private function decodeUint64() |
163
|
|
|
{ |
164
|
3 |
|
if (!isset($this->data[$this->offset + 7])) { |
165
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 8); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
$num = (ORD[$this->data[$this->offset++]] * 0x1000000 |
169
|
2 |
|
| ORD[$this->data[$this->offset++]] << 16 |
170
|
2 |
|
| ORD[$this->data[$this->offset++]] << 8 |
171
|
2 |
|
| ORD[$this->data[$this->offset++]]) * 0x100000000 |
172
|
2 |
|
| ORD[$this->data[$this->offset++]] * 0x1000000 |
173
|
2 |
|
| ORD[$this->data[$this->offset++]] << 16 |
174
|
2 |
|
| ORD[$this->data[$this->offset++]] << 8 |
175
|
2 |
|
| ORD[$this->data[$this->offset++]]; |
176
|
|
|
|
177
|
2 |
|
return $num > 0 ? $num : \sprintf('%u', $num); |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
private function decodeInt8(): int |
181
|
|
|
{ |
182
|
3 |
|
if (!isset($this->data[$this->offset])) { |
183
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 1); |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
$num = ORD[$this->data[$this->offset++]]; |
187
|
|
|
|
188
|
2 |
|
return $num & 0x80 ? $num - 0x100 : $num; |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
private function decodeInt16(): int |
192
|
|
|
{ |
193
|
3 |
|
if (!isset($this->data[$this->offset + 1])) { |
194
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 2); |
195
|
|
|
} |
196
|
|
|
|
197
|
2 |
|
$num = ORD[$this->data[$this->offset++]] << 8 |
198
|
2 |
|
| ORD[$this->data[$this->offset++]]; |
199
|
|
|
|
200
|
2 |
|
return $num & 0x8000 ? $num - 0x10000 : $num; |
201
|
|
|
} |
202
|
|
|
|
203
|
3 |
|
private function decodeInt32(): int |
204
|
|
|
{ |
205
|
3 |
|
if (!isset($this->data[$this->offset + 3])) { |
206
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 4); |
207
|
|
|
} |
208
|
|
|
|
209
|
2 |
|
$num = ORD[$this->data[$this->offset++]] * 0x1000000 |
210
|
2 |
|
| ORD[$this->data[$this->offset++]] << 16 |
211
|
2 |
|
| ORD[$this->data[$this->offset++]] << 8 |
212
|
2 |
|
| ORD[$this->data[$this->offset++]]; |
213
|
|
|
|
214
|
2 |
|
return $num & 0x80000000 ? $num - 0x100000000 : $num; |
215
|
|
|
} |
216
|
|
|
|
217
|
4 |
|
private function decodeInt64(): int |
218
|
|
|
{ |
219
|
4 |
|
if (!isset($this->data[$this->offset + 7])) { |
220
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, 8); |
221
|
|
|
} |
222
|
|
|
|
223
|
3 |
|
$num = ORD[$this->data[$this->offset++]]; |
224
|
|
|
|
225
|
3 |
|
$negate = ($num & 0x80) === 0x80; |
226
|
|
|
|
227
|
3 |
|
if ($negate) { |
228
|
2 |
|
$num = (($num ^ 0xff) * 0x100000000000000) |
229
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x1000000000000 |
230
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x10000000000 |
231
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x100000000 |
232
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x1000000 |
233
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x10000 |
234
|
2 |
|
| (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x100 |
235
|
2 |
|
| ORD[$this->data[$this->offset++]] ^ 0xff; |
236
|
|
|
|
237
|
2 |
|
return ~$num; |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
$num = ($num * 0x100000000000000) |
241
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x1000000000000 |
242
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x10000000000 |
243
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x100000000 |
244
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x1000000 |
245
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x10000 |
246
|
1 |
|
| ORD[$this->data[$this->offset++]] * 0x100 |
247
|
1 |
|
| ORD[$this->data[$this->offset++]]; |
248
|
|
|
|
249
|
1 |
|
return $num; |
250
|
|
|
} |
251
|
|
|
|
252
|
8 |
|
private function decodeStr(int $length): string |
253
|
|
|
{ |
254
|
8 |
|
if (0 === $length) { |
255
|
1 |
|
return ''; |
256
|
|
|
} |
257
|
|
|
|
258
|
7 |
|
if (!isset($this->data[$this->offset + $length - 1])) { |
259
|
1 |
|
throw DecodingFailed::insufficientData($this->data, $this->offset, $length); |
260
|
|
|
} |
261
|
|
|
|
262
|
6 |
|
$str = \substr($this->data, $this->offset++, $length); |
263
|
6 |
|
$this->offset += $length; |
264
|
|
|
|
265
|
6 |
|
return $str; |
266
|
|
|
} |
267
|
|
|
|
268
|
5 |
|
private function decodeArray(int $size): array |
269
|
|
|
{ |
270
|
5 |
|
$array = []; |
271
|
5 |
|
while ($size--) { |
272
|
4 |
|
$array[] = $this->parse(); |
273
|
|
|
} |
274
|
|
|
|
275
|
5 |
|
return $array; |
276
|
|
|
} |
277
|
|
|
|
278
|
5 |
|
private function decodeMap(int $size): array |
279
|
|
|
{ |
280
|
5 |
|
$map = []; |
281
|
5 |
|
while ($size--) { |
282
|
5 |
|
$map[$this->parse()] = $this->parse(); |
283
|
|
|
} |
284
|
|
|
|
285
|
5 |
|
return $map; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
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.