Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/ASN1/Util/Flags.php

raw symbols SCIP
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 * Class to handle a bit string as a field of flags.
18 * @see \SpomkyLabs\Pki\Test\ASN1\Util\FlagsTest
19 */
20 final class Flags
21 {
22 /**
23 * Flag octets.
24 */
25 private string $_flags;
26
27 /**
28 * @param BigInteger|int|string $flags Flags
29 * @param int $_width The number of flags. If width is larger than
30 * number of bits in $flags, zeroes are prepended
31 * to flag field.
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 // calculate number of unused bits in last octet
43 $last_octet_bits = $_width % 8;
44 $unused_bits = $last_octet_bits !== 0 ? 8 - $last_octet_bits : 0;
45 // mask bits outside bitfield width
46 $num = BigInteger::of($flags);
47 $mask = BigInteger::of(1)->shiftedLeft($_width)->minus(1);
48 $num = $num->and($mask);
49
50 // shift towards MSB if needed
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 // pad with zeroes
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 * Initialize from `BitString`.
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 * Check whether a bit at given index is set.
86 *
87 * Index 0 is the leftmost bit.
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 // octet index
95 $oi = (int) floor($idx / 8);
96 $byte = $this->_flags[$oi];
97 // bit index
98 $bi = $idx % 8;
99 // index 0 is the most significant bit in byte
100 $mask = 0x01 << (7 - $bi);
101 return (ord($byte) & $mask) > 0;
102 }
103
104 /**
105 * Get flags as an octet string.
106 *
107 * Zeroes are appended to the last octet if width is not divisible by 8.
108 */
109 public function string(): string
110 {
111 return $this->_flags;
112 }
113
114 /**
115 * Get flags as a base 10 integer.
116 *
117 * @return string Integer as a string
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 * Get flags as an integer.
130 */
131 public function intNumber(): int
132 {
133 $num = BigInt::create($this->number());
134 return $num->toInt();
135 }
136
137 /**
138 * Get flags as a `BitString` object.
139 *
140 * Unused bits are set accordingly. Trailing zeroes are not stripped.
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 }