| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\ASN1\Util; |
| 6 |
|
| 7 |
use function assert; |
| 8 |
use Brick\Math\BigInteger; |
| 9 |
use function count; |
| 10 |
use function is_array; |
| 11 |
use function ord; |
| 12 |
use OutOfBoundsException; |
| 13 |
use RuntimeException; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\BitString; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
final class Flags |
| 21 |
{ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private string $_flags; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private function __construct( |
| 34 |
BigInteger|int|string $flags, |
| 35 |
private readonly int $_width |
| 36 |
) { |
| 37 |
if ($_width === 0) { |
| 38 |
$this->_flags = ''; |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
$last_octet_bits = $_width % 8; |
| 44 |
$unused_bits = $last_octet_bits !== 0 ? 8 - $last_octet_bits : 0; |
| 45 |
|
| 46 |
$num = BigInteger::of($flags); |
| 47 |
$mask = BigInteger::of(1)->shiftedLeft($_width)->minus(1); |
| 48 |
$num = $num->and($mask); |
| 49 |
|
| 50 |
|
| 51 |
$data = $num->shiftedLeft($unused_bits) |
| 52 |
->toBytes(false); |
| 53 |
$octets = unpack('C*', $data); |
| 54 |
assert(is_array($octets), new RuntimeException('unpack() failed')); |
| 55 |
$bits = count($octets) * 8; |
| 56 |
|
| 57 |
while ($bits < $_width) { |
| 58 |
array_unshift($octets, 0); |
| 59 |
$bits += 8; |
| 60 |
} |
| 61 |
$this->_flags = pack('C*', ...$octets); |
| 62 |
} |
| 63 |
|
| 64 |
public static function create(BigInteger|int|string $flags, int $_width): self |
| 65 |
{ |
| 66 |
return new self($flags, $_width); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function fromBitString(BitString $bs, int $width): self |
| 73 |
{ |
| 74 |
$num_bits = $bs->numBits(); |
| 75 |
$data = $bs->string(); |
| 76 |
$num = $data === '' ? BigInteger::of(0) : BigInteger::fromBytes($data, false); |
| 77 |
$num = $num->shiftedRight($bs->unusedBits()); |
| 78 |
if ($num_bits < $width) { |
| 79 |
$num = $num->shiftedLeft($width - $num_bits); |
| 80 |
} |
| 81 |
return self::create($num, $width); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function test(int $idx): bool |
| 90 |
{ |
| 91 |
if ($idx >= $this->_width) { |
| 92 |
throw new OutOfBoundsException('Index is out of bounds.'); |
| 93 |
} |
| 94 |
|
| 95 |
$oi = (int) floor($idx / 8); |
| 96 |
$byte = $this->_flags[$oi]; |
| 97 |
|
| 98 |
$bi = $idx % 8; |
| 99 |
|
| 100 |
$mask = 0x01 << (7 - $bi); |
| 101 |
return (ord($byte) & $mask) > 0; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function string(): string |
| 110 |
{ |
| 111 |
return $this->_flags; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function number(): string |
| 120 |
{ |
| 121 |
$num = $this->_flags === '' ? BigInteger::of(0) : BigInteger::fromBytes($this->_flags, false); |
| 122 |
$last_octet_bits = $this->_width % 8; |
| 123 |
$unused_bits = $last_octet_bits !== 0 ? 8 - $last_octet_bits : 0; |
| 124 |
$num = $num->shiftedRight($unused_bits); |
| 125 |
return $num->toBase(10); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function intNumber(): int |
| 132 |
{ |
| 133 |
$num = BigInt::create($this->number()); |
| 134 |
return $num->toInt(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function bitString(): BitString |
| 143 |
{ |
| 144 |
$last_octet_bits = $this->_width % 8; |
| 145 |
$unused_bits = $last_octet_bits !== 0 ? 8 - $last_octet_bits : 0; |
| 146 |
return BitString::create($this->_flags, $unused_bits); |
| 147 |
} |
| 148 |
} |