| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2018-2020 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace CBOR; |
| 15 |
|
| 16 |
use Brick\Math\BigInteger; |
| 17 |
use GMP; |
| 18 |
use InvalidArgumentException; |
| 19 |
|
| 20 |
final class UnsignedIntegerObject extends AbstractCBORObject |
| 21 |
{ |
| 22 |
private const MAJOR_TYPE = 0b000; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string|null |
| 26 |
*/ |
| 27 |
private $data; |
| 28 |
|
| 29 |
public function __construct(int $additionalInformation, ?string $data) |
| 30 |
{ |
| 31 |
parent::__construct(self::MAJOR_TYPE, $additionalInformation); |
| 32 |
$this->data = $data; |
| 33 |
} |
| 34 |
|
| 35 |
public function __toString(): string |
| 36 |
{ |
| 37 |
$result = parent::__toString(); |
| 38 |
if (null !== $this->data) { |
| 39 |
$result .= $this->data; |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
public static function createObjectForValue(int $additionalInformation, ?string $data): self |
| 46 |
{ |
| 47 |
return new self($additionalInformation, $data); |
| 48 |
} |
| 49 |
|
| 50 |
public static function create(int $value): self |
| 51 |
{ |
| 52 |
return self::createFromString((string) $value); |
| 53 |
} |
| 54 |
|
| 55 |
public static function createFromHex(string $value): self |
| 56 |
{ |
| 57 |
$integer = BigInteger::fromBase($value, 16); |
| 58 |
|
| 59 |
return self::createBigInteger($integer); |
| 60 |
} |
| 61 |
|
| 62 |
public static function createFromString(string $value): self |
| 63 |
{ |
| 64 |
$integer = BigInteger::of($value); |
| 65 |
|
| 66 |
return self::createBigInteger($integer); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @deprecated Deprecated since v1.1 and will be removed in v2.0. Please use "create" or "createFromString" instead |
| 71 |
*/ |
| 72 |
public static function createFromGmpValue(GMP $value): self |
| 73 |
{ |
| 74 |
if (gmp_cmp($value, gmp_init(0)) < 0) { |
| 75 |
throw new InvalidArgumentException('The value must be a positive integer.'); |
| 76 |
} |
| 77 |
|
| 78 |
switch (true) { |
| 79 |
case gmp_cmp($value, gmp_init(24)) < 0: |
| 80 |
$ai = gmp_intval($value); |
| 81 |
$data = null; |
| 82 |
break; |
| 83 |
case gmp_cmp($value, gmp_init('FF', 16)) < 0: |
| 84 |
$ai = 24; |
| 85 |
$data = self::hex2bin(str_pad(gmp_strval($value, 16), 2, '0', STR_PAD_LEFT)); |
| 86 |
break; |
| 87 |
case gmp_cmp($value, gmp_init('FFFF', 16)) < 0: |
| 88 |
$ai = 25; |
| 89 |
$data = self::hex2bin(str_pad(gmp_strval($value, 16), 4, '0', STR_PAD_LEFT)); |
| 90 |
break; |
| 91 |
case gmp_cmp($value, gmp_init('FFFFFFFF', 16)) < 0: |
| 92 |
$ai = 26; |
| 93 |
$data = self::hex2bin(str_pad(gmp_strval($value, 16), 8, '0', STR_PAD_LEFT)); |
| 94 |
break; |
| 95 |
default: |
| 96 |
throw new InvalidArgumentException('Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.'); |
| 97 |
} |
| 98 |
|
| 99 |
return new self($ai, $data); |
| 100 |
} |
| 101 |
|
| 102 |
public function getMajorType(): int |
| 103 |
{ |
| 104 |
return self::MAJOR_TYPE; |
| 105 |
} |
| 106 |
|
| 107 |
public function getAdditionalInformation(): int |
| 108 |
{ |
| 109 |
return $this->additionalInformation; |
| 110 |
} |
| 111 |
|
| 112 |
public function getValue(): string |
| 113 |
{ |
| 114 |
return $this->getNormalizedData(); |
| 115 |
} |
| 116 |
|
| 117 |
public function getNormalizedData(bool $ignoreTags = false): string |
| 118 |
{ |
| 119 |
if (null === $this->data) { |
| 120 |
return (string) $this->additionalInformation; |
| 121 |
} |
| 122 |
|
| 123 |
$integer = BigInteger::fromBase(bin2hex($this->data), 16); |
| 124 |
|
| 125 |
return $integer->toBase(10); |
| 126 |
} |
| 127 |
|
| 128 |
private static function createBigInteger(BigInteger $integer): self |
| 129 |
{ |
| 130 |
if ($integer->isLessThan(BigInteger::zero())) { |
| 131 |
throw new InvalidArgumentException('The value must be a positive integer.'); |
| 132 |
} |
| 133 |
|
| 134 |
switch (true) { |
| 135 |
case $integer->isLessThan(BigInteger::of(24)): |
| 136 |
$ai = $integer->toInt(); |
| 137 |
$data = null; |
| 138 |
break; |
| 139 |
case $integer->isLessThan(BigInteger::fromBase('FF', 16)): |
| 140 |
$ai = 24; |
| 141 |
$data = self::hex2bin(str_pad($integer->toBase(16), 2, '0', STR_PAD_LEFT)); |
| 142 |
break; |
| 143 |
case $integer->isLessThan(BigInteger::fromBase('FFFF', 16)): |
| 144 |
$ai = 25; |
| 145 |
$data = self::hex2bin(str_pad($integer->toBase(16), 4, '0', STR_PAD_LEFT)); |
| 146 |
break; |
| 147 |
case $integer->isLessThan(BigInteger::fromBase('FFFFFFFF', 16)): |
| 148 |
$ai = 26; |
| 149 |
$data = self::hex2bin(str_pad($integer->toBase(16), 8, '0', STR_PAD_LEFT)); |
| 150 |
break; |
| 151 |
default: |
| 152 |
throw new InvalidArgumentException('Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.'); |
| 153 |
} |
| 154 |
|
| 155 |
return new self($ai, $data); |
| 156 |
} |
| 157 |
|
| 158 |
private static function hex2bin(string $data): string |
| 159 |
{ |
| 160 |
$result = hex2bin($data); |
| 161 |
if (false === $result) { |
| 162 |
throw new InvalidArgumentException('Unable to convert the data'); |
| 163 |
} |
| 164 |
|
| 165 |
return $result; |
| 166 |
} |
| 167 |
} |
| 168 |
|