- md5
- 9f27c68dead386cfbd02194a8f8ce82a
- sha1
- cfeed12080bb3616acc73e4a172ad729daecfaa5
- sha256
- 8c07961cc2dd8d75ac618677b90720b24fa81354cd9ceba318e515f01fe55658
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace CBOR;
use CBOR\OtherObject\BreakObject;
use CBOR\OtherObject\OtherObjectManager;
use CBOR\Tag\TagObjectManager;
use InvalidArgumentException;
use RuntimeException;
final class Decoder
{
/**
* @var TagObjectManager
*/
private $tagObjectManager;
/**
* @var OtherObjectManager
*/
private $otherTypeManager;
public function __construct(TagObjectManager $tagObjectManager, OtherObjectManager $otherTypeManager)
{
$this->tagObjectManager = $tagObjectManager;
$this->otherTypeManager = $otherTypeManager;
}
public function decode(Stream $stream): CBORObject
{
return $this->process($stream);
}
private function process(Stream $stream, bool $breakable = false): CBORObject
{
$ib = \ord($stream->read(1));
$mt = $ib >> 5;
$ai = $ib & 0b00011111;
$val = null;
switch ($ai) {
case 0b00011000: //24
case 0b00011001: //25
case 0b00011010: //26
case 0b00011011: //27
$val = $stream->read(2 ** ($ai & 0b00000111));
break;
case 0b00011100: //28
case 0b00011101: //29
case 0b00011110: //30
throw new InvalidArgumentException(sprintf('Cannot parse the data. Found invalid Additional Information "%s" (%d).', str_pad(decbin($ai), 5, '0', STR_PAD_LEFT), $ai));
case 0b00011111: //31
return $this->processInfinite($stream, $mt, $breakable);
}
return $this->processFinite($stream, $mt, $ai, $val);
}
private function processFinite(Stream $stream, int $mt, int $ai, ?string $val): CBORObject
{
switch ($mt) {
case 0b000: //0
return UnsignedIntegerObject::createObjectForValue($ai, $val);
case 0b001: //1
return SignedIntegerObject::createObjectForValue($ai, $val);
case 0b010: //2
$length = null === $val ? $ai : gmp_intval(gmp_init(bin2hex($val), 16));
return new ByteStringObject($stream->read($length));
case 0b011: //3
$length = null === $val ? $ai : gmp_intval(gmp_init(bin2hex($val), 16));
return new TextStringObject($stream->read($length));
case 0b100: //4
$object = new ListObject();
$nbItems = null === $val ? $ai : gmp_intval(gmp_init(bin2hex($val), 16));
for ($i = 0; $i < $nbItems; ++$i) {
$object->add($this->process($stream));
}
return $object;
case 0b101: //5
$object = new MapObject();
$nbItems = null === $val ? $ai : gmp_intval(gmp_init(bin2hex($val), 16));
for ($i = 0; $i < $nbItems; ++$i) {
$object->add($this->process($stream), $this->process($stream));
}
return $object;
case 0b110: //6
return $this->tagObjectManager->createObjectForValue($ai, $val, $this->process($stream));
case 0b111: //7
return $this->otherTypeManager->createObjectForValue($ai, $val);
default:
throw new RuntimeException(sprintf('Unsupported major type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt)); // Should never append
}
}
private function processInfinite(Stream $stream, int $mt, bool $breakable): CBORObject
{
switch ($mt) {
case 0b010: //2
$object = new ByteStringWithChunkObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
if (!$it instanceof ByteStringObject) {
throw new RuntimeException('Unable to parse the data. Infinite Byte String object can only get Byte String objects.');
}
$object->add($it);
}
return $object;
case 0b011: //3
$object = new TextStringWithChunkObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
if (!$it instanceof TextStringObject) {
throw new RuntimeException('Unable to parse the data. Infinite Text String object can only get Text String objects.');
}
$object->add($it);
}
return $object;
case 0b100: //4
$object = new InfiniteListObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
$object->add($it);
}
return $object;
case 0b101: //5
$object = new InfiniteMapObject();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
$object->append($it, $this->process($stream));
}
return $object;
case 0b111: //7
if (!$breakable) {
throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.');
}
return new BreakObject();
case 0b000: //0
case 0b001: //1
case 0b110: //6
default:
throw new InvalidArgumentException(sprintf('Cannot parse the data. Found infinite length for Major Type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt));
}
}
}
The same content is in 3 releases, Joomla 4.0.0-beta to 4.0.0-beta3. See them all, or every version of this file.