@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Tagged; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Tagged; |
6 | 6 |
@@ -11,149 +11,149 @@ |
||
11 | 11 | */ |
12 | 12 | class Flags |
13 | 13 | { |
14 | - /** |
|
15 | - * Flag octets. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $_flags; |
|
14 | + /** |
|
15 | + * Flag octets. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $_flags; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Number of flags. |
|
23 | - * |
|
24 | - * @var int |
|
25 | - */ |
|
26 | - protected $_width; |
|
21 | + /** |
|
22 | + * Number of flags. |
|
23 | + * |
|
24 | + * @var int |
|
25 | + */ |
|
26 | + protected $_width; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param int|string $flags Flags |
|
32 | - * @param int $width The number of flags. If width is larger than |
|
33 | - * number of bits in $flags, zeroes are prepended |
|
34 | - * to flag field. |
|
35 | - */ |
|
36 | - public function __construct($flags, int $width) |
|
37 | - { |
|
38 | - if (!$width) { |
|
39 | - $this->_flags = ''; |
|
40 | - } else { |
|
41 | - // calculate number of unused bits in last octet |
|
42 | - $last_octet_bits = $width % 8; |
|
43 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
44 | - $num = gmp_init($flags); |
|
45 | - // mask bits outside bitfield width |
|
46 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
47 | - $num &= $mask; |
|
48 | - // shift towards MSB if needed |
|
49 | - $data = gmp_export($num << $unused_bits, 1, |
|
50 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
51 | - $octets = unpack('C*', $data); |
|
52 | - assert(is_array($octets), new \RuntimeException('unpack() failed')); |
|
53 | - $bits = count($octets) * 8; |
|
54 | - // pad with zeroes |
|
55 | - while ($bits < $width) { |
|
56 | - array_unshift($octets, 0); |
|
57 | - $bits += 8; |
|
58 | - } |
|
59 | - $this->_flags = pack('C*', ...$octets); |
|
60 | - } |
|
61 | - $this->_width = $width; |
|
62 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param int|string $flags Flags |
|
32 | + * @param int $width The number of flags. If width is larger than |
|
33 | + * number of bits in $flags, zeroes are prepended |
|
34 | + * to flag field. |
|
35 | + */ |
|
36 | + public function __construct($flags, int $width) |
|
37 | + { |
|
38 | + if (!$width) { |
|
39 | + $this->_flags = ''; |
|
40 | + } else { |
|
41 | + // calculate number of unused bits in last octet |
|
42 | + $last_octet_bits = $width % 8; |
|
43 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
44 | + $num = gmp_init($flags); |
|
45 | + // mask bits outside bitfield width |
|
46 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
47 | + $num &= $mask; |
|
48 | + // shift towards MSB if needed |
|
49 | + $data = gmp_export($num << $unused_bits, 1, |
|
50 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
51 | + $octets = unpack('C*', $data); |
|
52 | + assert(is_array($octets), new \RuntimeException('unpack() failed')); |
|
53 | + $bits = count($octets) * 8; |
|
54 | + // pad with zeroes |
|
55 | + while ($bits < $width) { |
|
56 | + array_unshift($octets, 0); |
|
57 | + $bits += 8; |
|
58 | + } |
|
59 | + $this->_flags = pack('C*', ...$octets); |
|
60 | + } |
|
61 | + $this->_width = $width; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Initialize from BitString. |
|
66 | - * |
|
67 | - * @param BitString $bs |
|
68 | - * @param int $width |
|
69 | - * |
|
70 | - * @return self |
|
71 | - */ |
|
72 | - public static function fromBitString(BitString $bs, int $width): self |
|
73 | - { |
|
74 | - $num_bits = $bs->numBits(); |
|
75 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
76 | - $num >>= $bs->unusedBits(); |
|
77 | - if ($num_bits < $width) { |
|
78 | - $num <<= ($width - $num_bits); |
|
79 | - } |
|
80 | - return new self(gmp_strval($num, 10), $width); |
|
81 | - } |
|
64 | + /** |
|
65 | + * Initialize from BitString. |
|
66 | + * |
|
67 | + * @param BitString $bs |
|
68 | + * @param int $width |
|
69 | + * |
|
70 | + * @return self |
|
71 | + */ |
|
72 | + public static function fromBitString(BitString $bs, int $width): self |
|
73 | + { |
|
74 | + $num_bits = $bs->numBits(); |
|
75 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
76 | + $num >>= $bs->unusedBits(); |
|
77 | + if ($num_bits < $width) { |
|
78 | + $num <<= ($width - $num_bits); |
|
79 | + } |
|
80 | + return new self(gmp_strval($num, 10), $width); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Check whether a bit at given index is set. |
|
85 | - * |
|
86 | - * Index 0 is the leftmost bit. |
|
87 | - * |
|
88 | - * @param int $idx |
|
89 | - * |
|
90 | - * @throws \OutOfBoundsException |
|
91 | - * |
|
92 | - * @return bool |
|
93 | - */ |
|
94 | - public function test(int $idx): bool |
|
95 | - { |
|
96 | - if ($idx >= $this->_width) { |
|
97 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
98 | - } |
|
99 | - // octet index |
|
100 | - $oi = (int) floor($idx / 8); |
|
101 | - $byte = $this->_flags[$oi]; |
|
102 | - // bit index |
|
103 | - $bi = $idx % 8; |
|
104 | - // index 0 is the most significant bit in byte |
|
105 | - $mask = 0x01 << (7 - $bi); |
|
106 | - return (ord($byte) & $mask) > 0; |
|
107 | - } |
|
83 | + /** |
|
84 | + * Check whether a bit at given index is set. |
|
85 | + * |
|
86 | + * Index 0 is the leftmost bit. |
|
87 | + * |
|
88 | + * @param int $idx |
|
89 | + * |
|
90 | + * @throws \OutOfBoundsException |
|
91 | + * |
|
92 | + * @return bool |
|
93 | + */ |
|
94 | + public function test(int $idx): bool |
|
95 | + { |
|
96 | + if ($idx >= $this->_width) { |
|
97 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
98 | + } |
|
99 | + // octet index |
|
100 | + $oi = (int) floor($idx / 8); |
|
101 | + $byte = $this->_flags[$oi]; |
|
102 | + // bit index |
|
103 | + $bi = $idx % 8; |
|
104 | + // index 0 is the most significant bit in byte |
|
105 | + $mask = 0x01 << (7 - $bi); |
|
106 | + return (ord($byte) & $mask) > 0; |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Get flags as an octet string. |
|
111 | - * |
|
112 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function string(): string |
|
117 | - { |
|
118 | - return $this->_flags; |
|
119 | - } |
|
109 | + /** |
|
110 | + * Get flags as an octet string. |
|
111 | + * |
|
112 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function string(): string |
|
117 | + { |
|
118 | + return $this->_flags; |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Get flags as a base 10 integer. |
|
123 | - * |
|
124 | - * @return string Integer as a string |
|
125 | - */ |
|
126 | - public function number(): string |
|
127 | - { |
|
128 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
129 | - $last_octet_bits = $this->_width % 8; |
|
130 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
131 | - $num >>= $unused_bits; |
|
132 | - return gmp_strval($num, 10); |
|
133 | - } |
|
121 | + /** |
|
122 | + * Get flags as a base 10 integer. |
|
123 | + * |
|
124 | + * @return string Integer as a string |
|
125 | + */ |
|
126 | + public function number(): string |
|
127 | + { |
|
128 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
129 | + $last_octet_bits = $this->_width % 8; |
|
130 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
131 | + $num >>= $unused_bits; |
|
132 | + return gmp_strval($num, 10); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Get flags as an integer. |
|
137 | - * |
|
138 | - * @return int |
|
139 | - */ |
|
140 | - public function intNumber(): int |
|
141 | - { |
|
142 | - $num = new BigInt($this->number()); |
|
143 | - return $num->intVal(); |
|
144 | - } |
|
135 | + /** |
|
136 | + * Get flags as an integer. |
|
137 | + * |
|
138 | + * @return int |
|
139 | + */ |
|
140 | + public function intNumber(): int |
|
141 | + { |
|
142 | + $num = new BigInt($this->number()); |
|
143 | + return $num->intVal(); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Get flags as a BitString. |
|
148 | - * |
|
149 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
150 | - * |
|
151 | - * @return BitString |
|
152 | - */ |
|
153 | - public function bitString(): BitString |
|
154 | - { |
|
155 | - $last_octet_bits = $this->_width % 8; |
|
156 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
157 | - return new BitString($this->_flags, $unused_bits); |
|
158 | - } |
|
146 | + /** |
|
147 | + * Get flags as a BitString. |
|
148 | + * |
|
149 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
150 | + * |
|
151 | + * @return BitString |
|
152 | + */ |
|
153 | + public function bitString(): BitString |
|
154 | + { |
|
155 | + $last_octet_bits = $this->_width % 8; |
|
156 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
157 | + return new BitString($this->_flags, $unused_bits); |
|
158 | + } |
|
159 | 159 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Util; |
6 | 6 |