Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Cbor/CborDecoder.php

raw symbols SCIP
1 <?php
2 namespace Aws\Cbor;
3
4 use Aws\Cbor\Exception\CborException;
5
6 /**
7 * Decodes Concise Binary Object Representation encoded strings
8 * into PHP values according to RFC 8949
9 *
10 * https://www.rfc-editor.org/rfc/rfc8949.html
11 *
12 * Supports Major types 0-7 including:
13 * - Type 0: Unsigned integers
14 * - Type 1: Negative integers
15 * - Type 2: Byte strings
16 * - Type 3: Text strings (UTF-8)
17 * - Type 4: Arrays
18 * - Type 5: Maps
19 * - Type 6: Tagged values (timestamps)
20 * - Type 7: Simple values (null, bool, float)
21 *
22 * @internal
23 */
24 final class CborDecoder
25 {
26 private int $offset;
27 private int $length;
28
29 /**
30 * Decode CBOR binary data to PHP value
31 *
32 * @param string $data The CBOR-encoded binary data to decode
33 *
34 * @return mixed The decoded PHP value (can be any type: int, string, array, bool, null, float)
35 * @throws CborException If data is empty or malformed CBOR
36 */
37 public function decode(string $data): mixed
38 {
39 if ($data === '') {
40 throw new CborException("No data to decode");
41 }
42
43 $this->offset = 0;
44 $this->length = strlen($data);
45
46 return $this->decodeValue($data);
47 }
48
49 /**
50 * Decode multiple CBOR values from sequential binary data
51 *
52 * @param string $data The CBOR-encoded binary data containing multiple values
53 *
54 * @return array Array of decoded PHP values in the order they appear in the data
55 * @throws CborException If data is malformed CBOR
56 */
57 public function decodeAll(string $data): array
58 {
59 $this->length = strlen($data);
60 $this->offset = 0;
61 $values = [];
62
63 while ($this->offset < $this->length) {
64 $values[] = $this->decodeValue($data);
65 }
66
67 return $values;
68 }
69
70 /**
71 * Decodes a single CBOR value at the current offset
72 *
73 * @param string $data Reference to the CBOR data being decoded
74 *
75 * @return mixed The decoded value
76 * @throws CborException If unexpected end of data or invalid CBOR format
77 */
78 private function decodeValue(string &$data): mixed
79 {
80 $offset = $this->offset;
81 $length = $this->length;
82
83 if ($offset >= $length) {
84 throw new CborException("Unexpected end of data");
85 }
86
87 $byte = ord($data[$offset++]);
88 $majorType = $byte >> 5;
89 $info = $byte & 0x1F;
90
91 switch ($majorType) {
92 case 0: // Unsigned integer
93 if ($info < 24) {
94 $this->offset = $offset;
95
96 return $info;
97 }
98
99 switch ($info) {
100 case 24:
101 if ($offset >= $length) {
102 throw new CborException("Not enough data");
103 }
104
105 $this->offset = $offset + 1;
106
107 return ord($data[$offset]);
108
109 case 25:
110 if ($offset + 2 > $length) {
111 throw new CborException("Not enough data");
112 }
113
114 $this->offset = $offset + 2;
115
116 return (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
117
118 case 26:
119 if ($offset + 4 > $length) {
120 throw new CborException("Not enough data");
121 }
122
123 $this->offset = $offset + 4;
124
125 return unpack('N', $data, $offset)[1];
126
127 case 27:
128 if ($offset + 8 > $length) {
129 throw new CborException("Not enough data");
130 }
131
132 $this->offset = $offset + 8;
133
134 return unpack('J', $data, $offset)[1];
135
136 default:
137 throw new CborException("Invalid additional info for integer: $info");
138 }
139
140 case 1: // Negative integer
141 if ($info < 24) {
142 $this->offset = $offset;
143
144 return -1 - $info;
145 }
146
147 switch ($info) {
148 case 24:
149 if ($offset >= $length) {
150 throw new CborException("Not enough data");
151 }
152
153 $this->offset = $offset + 1;
154
155 return -1 - ord($data[$offset]);
156
157 case 25:
158 if ($offset + 2 > $length) {
159 throw new CborException("Not enough data");
160 }
161
162 $this->offset = $offset + 2;
163
164 return -1 - ((ord($data[$offset]) << 8) | ord($data[$offset + 1]));
165
166 case 26:
167 if ($offset + 4 > $length) {
168 throw new CborException("Not enough data");
169 }
170
171 $this->offset = $offset + 4;
172
173 return -1 - unpack('N', $data, $offset)[1];
174
175 case 27:
176 if ($offset + 8 > $length) {
177 throw new CborException("Not enough data");
178 }
179
180 $this->offset = $offset + 8;
181 $unsigned = unpack('J', $data, $offset)[1];
182
183 return ($unsigned === 9223372036854775807) ? PHP_INT_MIN : -1 - $unsigned;
184
185 default:
186 throw new CborException("Invalid additional info for integer: $info");
187 }
188
189 case 2: // Byte string
190 if ($info < 24) {
191 $len = $info;
192 } else {
193 switch ($info) {
194 case 24:
195 if ($offset >= $length) {
196 throw new CborException("Not enough data");
197 }
198
199 $len = ord($data[$offset++]);
200 break;
201
202 case 25:
203 if ($offset + 2 > $length) {
204 throw new CborException("Not enough data");
205 }
206
207 $len = (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
208 $offset += 2;
209 break;
210
211 case 26:
212 if ($offset + 4 > $length) {
213 throw new CborException("Not enough data");
214 }
215
216 $len = unpack('N', $data, $offset)[1];
217 $offset += 4;
218 break;
219
220 case 27:
221 if ($offset + 8 > $length) {
222 throw new CborException("Not enough data");
223 }
224
225 $len = unpack('J', $data, $offset)[1];
226 $offset += 8;
227 break;
228
229 case 31:
230 $this->offset = $offset;
231
232 return $this->decodeIndefiniteString($data, 0x40);
233
234 default:
235 throw new CborException("Invalid additional info for byte string: $info");
236 }
237 }
238
239 if ($offset + $len > $length) {
240 throw new CborException("Not enough data");
241 }
242
243 $this->offset = $offset + $len;
244
245 return substr($data, $offset, $len);
246
247 case 3: // Text string
248 if ($info < 24) {
249 $len = $info;
250 } else {
251 switch ($info) {
252 case 24:
253 if ($offset >= $length) {
254 throw new CborException("Not enough data");
255 }
256
257 $len = ord($data[$offset++]);
258 break;
259
260 case 25:
261 if ($offset + 2 > $length) {
262 throw new CborException("Not enough data");
263 }
264
265 $len = (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
266 $offset += 2;
267 break;
268
269 case 26:
270 if ($offset + 4 > $length) {
271 throw new CborException("Not enough data");
272 }
273
274 $len = unpack('N', $data, $offset)[1];
275 $offset += 4;
276 break;
277
278 case 27:
279 if ($offset + 8 > $length) {
280 throw new CborException("Not enough data");
281 }
282
283 $len = unpack('J', $data, $offset)[1];
284 $offset += 8;
285 break;
286
287 case 31:
288 $this->offset = $offset;
289
290 return $this->decodeIndefiniteString($data, 0x60);
291
292 default:
293 throw new CborException("Invalid additional info for text string: $info");
294 }
295 }
296
297 if ($offset + $len > $length) {
298 throw new CborException("Not enough data");
299 }
300
301 $this->offset = $offset + $len;
302
303 return substr($data, $offset, $len);
304
305 case 4: // Array
306 if ($info < 24) {
307 $count = $info;
308 } else {
309 switch ($info) {
310 case 24:
311 if ($offset >= $length) {
312 throw new CborException("Not enough data");
313 }
314
315 $count = ord($data[$offset++]);
316 break;
317
318 case 25:
319 if ($offset + 2 > $length) {
320 throw new CborException("Not enough data");
321 }
322
323 $count = (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
324 $offset += 2;
325 break;
326
327 case 26:
328 if ($offset + 4 > $length) {
329 throw new CborException("Not enough data");
330 }
331
332 $count = unpack('N', $data, $offset)[1];
333 $offset += 4;
334 break;
335
336 case 27:
337 if ($offset + 8 > $length) {
338 throw new CborException("Not enough data");
339 }
340
341 $count = unpack('J', $data, $offset)[1];
342 $offset += 8;
343 break;
344
345 case 31:
346 $this->offset = $offset;
347
348 return $this->decodeIndefiniteArray($data);
349
350 default:
351 throw new CborException("Invalid additional info for array: $info");
352 }
353 }
354
355 $this->offset = $offset;
356 $arr = [];
357
358 for ($i = 0; $i < $count; $i++) {
359 $arr[] = $this->decodeValue($data);
360 }
361
362 return $arr;
363
364 case 5: // Map
365 if ($info < 24) {
366 $count = $info;
367 } else {
368 switch ($info) {
369 case 24:
370 if ($offset >= $length) {
371 throw new CborException("Not enough data");
372 }
373
374 $count = ord($data[$offset++]);
375 break;
376
377 case 25:
378 if ($offset + 2 > $length) {
379 throw new CborException("Not enough data");
380 }
381
382 $count = (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
383 $offset += 2;
384 break;
385
386 case 26:
387 if ($offset + 4 > $length) {
388 throw new CborException("Not enough data");
389 }
390
391 $count = unpack('N', $data, $offset)[1];
392 $offset += 4;
393 break;
394
395 case 27:
396 if ($offset + 8 > $length) {
397 throw new CborException("Not enough data");
398 }
399
400 $count = unpack('J', $data, $offset)[1];
401 $offset += 8;
402 break;
403
404 case 31:
405 $this->offset = $offset;
406
407 return $this->decodeIndefiniteMap($data);
408
409 default:
410 throw new CborException("Invalid additional info for map: $info");
411 }
412 }
413
414 $this->offset = $offset;
415 $map = [];
416
417 for ($i = 0; $i < $count; $i++) {
418 $key = $this->decodeValue($data);
419 $map[$key] = $this->decodeValue($data);
420 }
421
422 return $map;
423
424 case 6: // Tag
425 switch ($info) {
426 case 24:
427 $offset++;
428 break;
429
430 case 25:
431 $offset += 2;
432 break;
433
434 case 26:
435 $offset += 4;
436 break;
437
438 case 27:
439 $offset += 8;
440 break;
441 }
442
443 $this->offset = $offset;
444
445 return $this->decodeValue($data);
446
447 case 7: // Simple/float
448 switch ($info) {
449 case 20:
450 $this->offset = $offset;
451
452 return false;
453
454 case 21:
455 $this->offset = $offset;
456
457 return true;
458
459 case 22:
460 case 23:
461 $this->offset = $offset;
462
463 return null;
464
465 case 25: // Half-precision float
466 if ($offset + 2 > $length) {
467 throw new CborException("Not enough data");
468 }
469
470 $this->offset = $offset + 2;
471 $half = (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
472 $sign = ($half >> 15) & 0x01;
473 $exp = ($half >> 10) & 0x1F;
474 $mant = $half & 0x3FF;
475
476 if ($exp === 0) {
477 return $mant === 0
478 ? ($sign ? -0.0 : 0.0)
479 : ($sign ? -1 : 1) * pow(2, -14) * ($mant / 1024);
480 }
481
482 if ($exp === 31) {
483 return $mant === 0 ? ($sign ? -INF : INF) : NAN;
484 }
485
486 return (float) (($sign ? -1 : 1) * pow(2, $exp - 15) * (1 + $mant / 1024));
487
488 case 26: // Single-precision float
489 if ($offset + 4 > $length) {
490 throw new CborException("Not enough data");
491 }
492
493 $this->offset = $offset + 4;
494
495 return unpack('G', $data, $offset)[1];
496
497 case 27: // Double-precision float
498 if ($offset + 8 > $length) {
499 throw new CborException("Not enough data");
500 }
501
502 $this->offset = $offset + 8;
503
504 return unpack('E', $data, $offset)[1];
505
506 case 31:
507 throw new CborException("Unexpected break");
508
509 default:
510 throw new CborException("Unknown simple value: $info");
511 }
512
513 default:
514 throw new CborException("Unknown major type: $majorType");
515 }
516 }
517
518 /**
519 * Decode indefinite-length string (byte or text)
520 *
521 * @param string $data Reference to the CBOR data being decoded
522 * @param int $expectedMajor Expected major type (0x40 for byte string, 0x60 for text string)
523 *
524 * @return string The concatenated string from all chunks
525 * @throws CborException If invalid chunk format or unexpected end of data
526 */
527 private function decodeIndefiniteString(string &$data, int $expectedMajor): string
528 {
529 $chunks = [];
530
531 while (true) {
532 $offset = $this->offset;
533 $length = $this->length;
534
535 if ($offset >= $length) {
536 throw new CborException("Unexpected end of data");
537 }
538
539 $byte = ord($data[$offset++]);
540
541 if ($byte === 0xFF) {
542 $this->offset = $offset;
543
544 return implode('', $chunks);
545 }
546
547 if (($byte & 0xE0) !== $expectedMajor) {
548 throw new CborException("Invalid chunk in indefinite string");
549 }
550
551 $info = $byte & 0x1F;
552
553 if ($info === 31) {
554 throw new CborException("Nested indefinite string");
555 }
556
557 if ($info < 24) {
558 $len = $info;
559 } else {
560 switch ($info) {
561 case 24:
562 if ($offset >= $length) {
563 throw new CborException("Not enough data");
564 }
565
566 $len = ord($data[$offset++]);
567 break;
568
569 case 25:
570 if ($offset + 2 > $length) {
571 throw new CborException("Not enough data");
572 }
573
574 $len = (ord($data[$offset]) << 8) | ord($data[$offset + 1]);
575 $offset += 2;
576 break;
577
578 case 26:
579 if ($offset + 4 > $length) {
580 throw new CborException("Not enough data");
581 }
582
583 $len = unpack('N', $data, $offset)[1];
584 $offset += 4;
585 break;
586
587 case 27:
588 if ($offset + 8 > $length) {
589 throw new CborException("Not enough data");
590 }
591
592 $len = unpack('J', $data, $offset)[1];
593 $offset += 8;
594 break;
595
596 default:
597 throw new CborException("Invalid chunk length info: $info");
598 }
599 }
600
601 if ($offset + $len > $length) {
602 throw new CborException("Not enough data for chunk");
603 }
604
605 $chunks[] = substr($data, $offset, $len);
606 $this->offset = $offset + $len;
607 }
608 }
609
610 /**
611 * Decode indefinite-length array
612 *
613 * @param string $data Reference to the CBOR data being decoded
614 *
615 * @return array The decoded array elements
616 * @throws CborException If unexpected end of data
617 */
618 private function decodeIndefiniteArray(string &$data): array
619 {
620 $result = [];
621
622 while (true) {
623 if ($this->offset >= $this->length) {
624 throw new CborException("Unexpected end of data");
625 }
626
627 if (ord($data[$this->offset]) === 0xFF) {
628 $this->offset++;
629
630 return $result;
631 }
632
633 $result[] = $this->decodeValue($data);
634 }
635 }
636
637 /**
638 * Decode indefinite-length map
639 *
640 * @param string $data Reference to the CBOR data being decoded
641 *
642 * @return array The decoded map as associative array
643 * @throws CborException If unexpected end of data or odd number of items
644 */
645 private function decodeIndefiniteMap(string &$data): array
646 {
647 $result = [];
648
649 while (true) {
650 if ($this->offset >= $this->length) {
651 throw new CborException("Unexpected end of data");
652 }
653
654 if (ord($data[$this->offset]) === 0xFF) {
655 $this->offset++;
656
657 return $result;
658 }
659
660 $key = $this->decodeValue($data);
661 $result[$key] = $this->decodeValue($data);
662 }
663 }
664 }