| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace CBOR; |
| 6 |
|
| 7 |
use CBOR\OtherObject\BreakObject; |
| 8 |
use CBOR\OtherObject\DoublePrecisionFloatObject; |
| 9 |
use CBOR\OtherObject\FalseObject; |
| 10 |
use CBOR\OtherObject\HalfPrecisionFloatObject; |
| 11 |
use CBOR\OtherObject\NullObject; |
| 12 |
use CBOR\OtherObject\OtherObjectManager; |
| 13 |
use CBOR\OtherObject\OtherObjectManagerInterface; |
| 14 |
use CBOR\OtherObject\SimpleObject; |
| 15 |
use CBOR\OtherObject\SinglePrecisionFloatObject; |
| 16 |
use CBOR\OtherObject\TrueObject; |
| 17 |
use CBOR\OtherObject\UndefinedObject; |
| 18 |
use CBOR\Tag\Base16EncodingTag; |
| 19 |
use CBOR\Tag\Base64EncodingTag; |
| 20 |
use CBOR\Tag\Base64Tag; |
| 21 |
use CBOR\Tag\Base64UrlEncodingTag; |
| 22 |
use CBOR\Tag\Base64UrlTag; |
| 23 |
use CBOR\Tag\BigFloatTag; |
| 24 |
use CBOR\Tag\CBOREncodingTag; |
| 25 |
use CBOR\Tag\CBORTag; |
| 26 |
use CBOR\Tag\DatetimeTag; |
| 27 |
use CBOR\Tag\DecimalFractionTag; |
| 28 |
use CBOR\Tag\MimeTag; |
| 29 |
use CBOR\Tag\NegativeBigIntegerTag; |
| 30 |
use CBOR\Tag\TagManager; |
| 31 |
use CBOR\Tag\TagManagerInterface; |
| 32 |
use CBOR\Tag\TimestampTag; |
| 33 |
use CBOR\Tag\UnsignedBigIntegerTag; |
| 34 |
use CBOR\Tag\UriTag; |
| 35 |
use InvalidArgumentException; |
| 36 |
use function ord; |
| 37 |
use RuntimeException; |
| 38 |
use function sprintf; |
| 39 |
use const STR_PAD_LEFT; |
| 40 |
|
| 41 |
final class Decoder implements DecoderInterface |
| 42 |
{ |
| 43 |
private TagManagerInterface $tagObjectManager; |
| 44 |
|
| 45 |
private OtherObjectManagerInterface $otherTypeManager; |
| 46 |
|
| 47 |
public function __construct( |
| 48 |
?TagManagerInterface $tagObjectManager = null, |
| 49 |
?OtherObjectManagerInterface $otherTypeManager = null |
| 50 |
) { |
| 51 |
$this->tagObjectManager = $tagObjectManager ?? $this->generateTagManager(); |
| 52 |
$this->otherTypeManager = $otherTypeManager ?? $this->generateOtherObjectManager(); |
| 53 |
} |
| 54 |
|
| 55 |
public static function create( |
| 56 |
?TagManagerInterface $tagObjectManager = null, |
| 57 |
?OtherObjectManagerInterface $otherTypeManager = null |
| 58 |
): self { |
| 59 |
return new self($tagObjectManager, $otherTypeManager); |
| 60 |
} |
| 61 |
|
| 62 |
public function decode(Stream $stream): CBORObject |
| 63 |
{ |
| 64 |
return $this->process($stream, false); |
| 65 |
} |
| 66 |
|
| 67 |
private function process(Stream $stream, bool $breakable): CBORObject |
| 68 |
{ |
| 69 |
$ib = ord($stream->read(1)); |
| 70 |
$mt = $ib >> 5; |
| 71 |
$ai = $ib & 0b00011111; |
| 72 |
$val = null; |
| 73 |
switch ($ai) { |
| 74 |
case CBORObject::LENGTH_1_BYTE: |
| 75 |
case CBORObject::LENGTH_2_BYTES: |
| 76 |
case CBORObject::LENGTH_4_BYTES: |
| 77 |
case CBORObject::LENGTH_8_BYTES: |
| 78 |
$val = $stream->read(2 ** ($ai & 0b00000111)); |
| 79 |
break; |
| 80 |
case CBORObject::FUTURE_USE_1: |
| 81 |
case CBORObject::FUTURE_USE_2: |
| 82 |
case CBORObject::FUTURE_USE_3: |
| 83 |
throw new InvalidArgumentException(sprintf( |
| 84 |
'Cannot parse the data. Found invalid Additional Information "%s" (%d).', |
| 85 |
str_pad(decbin($ai), 8, '0', STR_PAD_LEFT), |
| 86 |
$ai |
| 87 |
)); |
| 88 |
case CBORObject::LENGTH_INDEFINITE: |
| 89 |
return $this->processInfinite($stream, $mt, $breakable); |
| 90 |
} |
| 91 |
|
| 92 |
return $this->processFinite($stream, $mt, $ai, $val); |
| 93 |
} |
| 94 |
|
| 95 |
private function processFinite(Stream $stream, int $mt, int $ai, ?string $val): CBORObject |
| 96 |
{ |
| 97 |
switch ($mt) { |
| 98 |
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: |
| 99 |
return UnsignedIntegerObject::createObjectForValue($ai, $val); |
| 100 |
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: |
| 101 |
return NegativeIntegerObject::createObjectForValue($ai, $val); |
| 102 |
case CBORObject::MAJOR_TYPE_BYTE_STRING: |
| 103 |
$length = $val === null ? $ai : Utils::binToInt($val); |
| 104 |
|
| 105 |
return ByteStringObject::create($stream->read($length)); |
| 106 |
case CBORObject::MAJOR_TYPE_TEXT_STRING: |
| 107 |
$length = $val === null ? $ai : Utils::binToInt($val); |
| 108 |
|
| 109 |
return TextStringObject::create($stream->read($length)); |
| 110 |
case CBORObject::MAJOR_TYPE_LIST: |
| 111 |
$object = ListObject::create(); |
| 112 |
$nbItems = $val === null ? $ai : Utils::binToInt($val); |
| 113 |
for ($i = 0; $i < $nbItems; ++$i) { |
| 114 |
$object->add($this->process($stream, false)); |
| 115 |
} |
| 116 |
|
| 117 |
return $object; |
| 118 |
case CBORObject::MAJOR_TYPE_MAP: |
| 119 |
$object = MapObject::create(); |
| 120 |
$nbItems = $val === null ? $ai : Utils::binToInt($val); |
| 121 |
for ($i = 0; $i < $nbItems; ++$i) { |
| 122 |
$object->add($this->process($stream, false), $this->process($stream, false)); |
| 123 |
} |
| 124 |
|
| 125 |
return $object; |
| 126 |
case CBORObject::MAJOR_TYPE_TAG: |
| 127 |
return $this->tagObjectManager->createObjectForValue($ai, $val, $this->process($stream, false)); |
| 128 |
case CBORObject::MAJOR_TYPE_OTHER_TYPE: |
| 129 |
return $this->otherTypeManager->createObjectForValue($ai, $val); |
| 130 |
default: |
| 131 |
throw new RuntimeException(sprintf( |
| 132 |
'Unsupported major type "%s" (%d).', |
| 133 |
str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), |
| 134 |
$mt |
| 135 |
)); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private function processInfinite(Stream $stream, int $mt, bool $breakable): CBORObject |
| 140 |
{ |
| 141 |
switch ($mt) { |
| 142 |
case CBORObject::MAJOR_TYPE_BYTE_STRING: |
| 143 |
$object = IndefiniteLengthByteStringObject::create(); |
| 144 |
while (! ($it = $this->process($stream, true)) instanceof BreakObject) { |
| 145 |
if (! $it instanceof ByteStringObject) { |
| 146 |
throw new RuntimeException( |
| 147 |
'Unable to parse the data. Infinite Byte String object can only get Byte String objects.' |
| 148 |
); |
| 149 |
} |
| 150 |
$object->add($it); |
| 151 |
} |
| 152 |
|
| 153 |
return $object; |
| 154 |
case CBORObject::MAJOR_TYPE_TEXT_STRING: |
| 155 |
$object = IndefiniteLengthTextStringObject::create(); |
| 156 |
while (! ($it = $this->process($stream, true)) instanceof BreakObject) { |
| 157 |
if (! $it instanceof TextStringObject) { |
| 158 |
throw new RuntimeException( |
| 159 |
'Unable to parse the data. Infinite Text String object can only get Text String objects.' |
| 160 |
); |
| 161 |
} |
| 162 |
$object->add($it); |
| 163 |
} |
| 164 |
|
| 165 |
return $object; |
| 166 |
case CBORObject::MAJOR_TYPE_LIST: |
| 167 |
$object = IndefiniteLengthListObject::create(); |
| 168 |
$it = $this->process($stream, true); |
| 169 |
while (! $it instanceof BreakObject) { |
| 170 |
$object->add($it); |
| 171 |
$it = $this->process($stream, true); |
| 172 |
} |
| 173 |
|
| 174 |
return $object; |
| 175 |
case CBORObject::MAJOR_TYPE_MAP: |
| 176 |
$object = IndefiniteLengthMapObject::create(); |
| 177 |
while (! ($it = $this->process($stream, true)) instanceof BreakObject) { |
| 178 |
$object->add($it, $this->process($stream, false)); |
| 179 |
} |
| 180 |
|
| 181 |
return $object; |
| 182 |
case CBORObject::MAJOR_TYPE_OTHER_TYPE: |
| 183 |
if (! $breakable) { |
| 184 |
throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.'); |
| 185 |
} |
| 186 |
|
| 187 |
return BreakObject::create(); |
| 188 |
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: |
| 189 |
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: |
| 190 |
case CBORObject::MAJOR_TYPE_TAG: |
| 191 |
default: |
| 192 |
throw new InvalidArgumentException(sprintf( |
| 193 |
'Cannot parse the data. Found infinite length for Major Type "%s" (%d).', |
| 194 |
str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), |
| 195 |
$mt |
| 196 |
)); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
private function generateTagManager(): TagManagerInterface |
| 201 |
{ |
| 202 |
return TagManager::create([ |
| 203 |
DatetimeTag::class, |
| 204 |
TimestampTag::class, |
| 205 |
|
| 206 |
UnsignedBigIntegerTag::class, |
| 207 |
NegativeBigIntegerTag::class, |
| 208 |
|
| 209 |
DecimalFractionTag::class, |
| 210 |
BigFloatTag::class, |
| 211 |
|
| 212 |
Base64UrlEncodingTag::class, |
| 213 |
Base64EncodingTag::class, |
| 214 |
Base16EncodingTag::class, |
| 215 |
CBOREncodingTag::class, |
| 216 |
|
| 217 |
UriTag::class, |
| 218 |
Base64UrlTag::class, |
| 219 |
Base64Tag::class, |
| 220 |
MimeTag::class, |
| 221 |
|
| 222 |
CBORTag::class, |
| 223 |
]); |
| 224 |
} |
| 225 |
|
| 226 |
private function generateOtherObjectManager(): OtherObjectManagerInterface |
| 227 |
{ |
| 228 |
return OtherObjectManager::create() |
| 229 |
->add(BreakObject::class) |
| 230 |
->add(SimpleObject::class) |
| 231 |
->add(FalseObject::class) |
| 232 |
->add(TrueObject::class) |
| 233 |
->add(NullObject::class) |
| 234 |
->add(UndefinedObject::class) |
| 235 |
->add(HalfPrecisionFloatObject::class) |
| 236 |
->add(SinglePrecisionFloatObject::class) |
| 237 |
->add(DoublePrecisionFloatObject::class) |
| 238 |
; |
| 239 |
} |
| 240 |
} |