-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCborDecoder.php
More file actions
304 lines (263 loc) · 7.26 KB
/
Copy pathCborDecoder.php
File metadata and controls
304 lines (263 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
declare(strict_types=1);
namespace Pps\Encoding;
use Pps\Exception\EncodingException;
/**
* Deterministic CBOR decoder.
*
* Decodes CBOR bytes to PHP values. Rejects non-canonical encoding:
* - Indefinite-length items
* - Duplicate map keys
* - Non-minimal integer encoding
*
* @package Pps\Encoding
*/
final class CborDecoder
{
/**
* Current position in the byte stream.
*/
private int $pos = 0;
/**
* Input byte stream.
*/
private string $data;
/**
* Decode CBOR bytes to a PHP value.
*
* @param string $data CBOR-encoded bytes.
* @return mixed Decoded value.
* @throws EncodingException If decoding fails or input is non-canonical.
*/
public static function decode(string $data): mixed
{
$decoder = new self();
$decoder->data = $data;
$decoder->pos = 0;
$result = $decoder->decodeValue();
if ($decoder->pos !== strlen($data)) {
throw new EncodingException('Trailing bytes after CBOR value.');
}
return $result;
}
/**
* Decode a single value.
*
* @return mixed Decoded value.
* @throws EncodingException
*/
private function decodeValue(): mixed
{
if ($this->pos >= strlen($this->data)) {
throw new EncodingException('Unexpected end of CBOR data.');
}
$byte = ord($this->data[$this->pos]);
$majorType = $byte >> 5;
$additionalInfo = $byte & 0x1F;
$this->pos++;
return match ($majorType) {
0 => $this->decodeUnsignedInteger($additionalInfo),
1 => $this->decodeNegativeInteger($additionalInfo),
2 => $this->decodeByteString($additionalInfo),
3 => $this->decodeTextString($additionalInfo),
4 => $this->decodeArray($additionalInfo),
5 => $this->decodeMap($additionalInfo),
7 => $this->decodeSimple($additionalInfo),
default => throw new EncodingException("Unsupported CBOR major type: $majorType"),
};
}
/**
* Decode an unsigned integer.
*
* @param int $info Additional info.
* @return int Decoded integer.
* @throws EncodingException
*/
private function decodeUnsignedInteger(int $info): int
{
return $this->decodeArgument($info);
}
/**
* Decode a negative integer.
*
* @param int $info Additional info.
* @return int Decoded integer.
* @throws EncodingException
*/
private function decodeNegativeInteger(int $info): int
{
return -1 - $this->decodeArgument($info);
}
/**
* Decode an argument value.
*
* @param int $info Additional info.
* @return int Argument value.
* @throws EncodingException
*/
private function decodeArgument(int $info): int
{
if ($info < 24) {
return $info;
}
if ($info === 24) {
return $this->readByte();
}
if ($info === 25) {
return $this->readUint16();
}
if ($info === 26) {
return $this->readUint32();
}
if ($info === 27) {
return $this->readUint64();
}
throw new EncodingException("Invalid CBOR additional info: $info");
}
/**
* Decode a byte string.
*
* @param int $info Additional info.
* @return string Raw bytes.
* @throws EncodingException
*/
private function decodeByteString(int $info): string
{
$length = $this->decodeArgument($info);
return $this->readBytes($length);
}
/**
* Decode a text string.
*
* @param int $info Additional info.
* @return string UTF-8 string.
* @throws EncodingException
*/
private function decodeTextString(int $info): string
{
$length = $this->decodeArgument($info);
return $this->readBytes($length);
}
/**
* Decode an array.
*
* @param int $info Additional info.
* @return array Sequential array.
* @throws EncodingException
*/
private function decodeArray(int $info): array
{
$count = $this->decodeArgument($info);
$result = [];
for ($i = 0; $i < $count; $i++) {
$result[] = $this->decodeValue();
}
return $result;
}
/**
* Decode a map.
*
* @param int $info Additional info.
* @return array Associative array.
* @throws EncodingException
*/
private function decodeMap(int $info): array
{
$count = $this->decodeArgument($info);
$result = [];
$keys = [];
for ($i = 0; $i < $count; $i++) {
$key = $this->decodeValue();
if (!is_int($key) && !is_string($key)) {
throw new EncodingException('Invalid CBOR map key type.');
}
// Check for duplicate keys
$keyHash = is_int($key) ? "i:$key" : "s:$key";
if (isset($keys[$keyHash])) {
throw new EncodingException('Duplicate CBOR map key detected.');
}
$keys[$keyHash] = true;
$value = $this->decodeValue();
$result[$key] = $value;
}
return $result;
}
/**
* Decode a simple value (boolean, null).
*
* @param int $info Additional info.
* @return mixed Decoded value.
* @throws EncodingException
*/
private function decodeSimple(int $info): mixed
{
return match ($info) {
20 => false,
21 => true,
22 => null,
default => throw new EncodingException("Unsupported CBOR simple value: $info"),
};
}
/**
* Read a single byte.
*
* @return int Byte value.
* @throws EncodingException
*/
private function readByte(): int
{
if ($this->pos >= strlen($this->data)) {
throw new EncodingException('Unexpected end of CBOR data.');
}
return ord($this->data[$this->pos++]);
}
/**
* Read a 16-bit unsigned integer (big-endian).
*
* @return int Integer value.
* @throws EncodingException
*/
private function readUint16(): int
{
$bytes = $this->readBytes(2);
return unpack('n', $bytes)[1];
}
/**
* Read a 32-bit unsigned integer (big-endian).
*
* @return int Integer value.
* @throws EncodingException
*/
private function readUint32(): int
{
$bytes = $this->readBytes(4);
return unpack('N', $bytes)[1];
}
/**
* Read a 64-bit unsigned integer (big-endian).
*
* @return int Integer value.
* @throws EncodingException
*/
private function readUint64(): int
{
$bytes = $this->readBytes(8);
return unpack('J', $bytes)[1];
}
/**
* Read a specified number of bytes.
*
* @param int $length Number of bytes.
* @return string Raw bytes.
* @throws EncodingException
*/
private function readBytes(int $length): string
{
if ($this->pos + $length > strlen($this->data)) {
throw new EncodingException('Unexpected end of CBOR data.');
}
$bytes = substr($this->data, $this->pos, $length);
$this->pos += $length;
return $bytes;
}
}