@@ -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\Component; |
6 | 6 |
@@ -13,225 +13,225 @@ |
||
13 | 13 | */ |
14 | 14 | class Length implements Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Length. |
|
18 | - * |
|
19 | - * @var BigInt |
|
20 | - */ |
|
21 | - private $_length; |
|
16 | + /** |
|
17 | + * Length. |
|
18 | + * |
|
19 | + * @var BigInt |
|
20 | + */ |
|
21 | + private $_length; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Whether length is indefinite. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_indefinite; |
|
23 | + /** |
|
24 | + * Whether length is indefinite. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_indefinite; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $length Length |
|
34 | - * @param bool $indefinite Whether length is indefinite |
|
35 | - */ |
|
36 | - public function __construct($length, bool $indefinite = false) |
|
37 | - { |
|
38 | - $this->_length = new BigInt($length); |
|
39 | - $this->_indefinite = $indefinite; |
|
40 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $length Length |
|
34 | + * @param bool $indefinite Whether length is indefinite |
|
35 | + */ |
|
36 | + public function __construct($length, bool $indefinite = false) |
|
37 | + { |
|
38 | + $this->_length = new BigInt($length); |
|
39 | + $this->_indefinite = $indefinite; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Decode length component from DER data. |
|
44 | - * |
|
45 | - * @param string $data DER encoded data |
|
46 | - * @param null|int $offset Reference to the variable that contains offset |
|
47 | - * into the data where to start parsing. |
|
48 | - * Variable is updated to the offset next to the |
|
49 | - * parsed length component. If null, start from offset 0. |
|
50 | - * |
|
51 | - * @throws DecodeException If decoding fails |
|
52 | - * |
|
53 | - * @return self |
|
54 | - */ |
|
55 | - public static function fromDER(string $data, int &$offset = null): self |
|
56 | - { |
|
57 | - $idx = $offset ?? 0; |
|
58 | - $datalen = strlen($data); |
|
59 | - if ($idx >= $datalen) { |
|
60 | - throw new DecodeException( |
|
61 | - 'Unexpected end of data while decoding length.'); |
|
62 | - } |
|
63 | - $indefinite = false; |
|
64 | - $byte = ord($data[$idx++]); |
|
65 | - // bits 7 to 1 |
|
66 | - $length = (0x7f & $byte); |
|
67 | - // long form |
|
68 | - if (0x80 & $byte) { |
|
69 | - if (!$length) { |
|
70 | - $indefinite = true; |
|
71 | - } else { |
|
72 | - if ($idx + $length > $datalen) { |
|
73 | - throw new DecodeException( |
|
74 | - 'Unexpected end of data while decoding long form length.'); |
|
75 | - } |
|
76 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | - } |
|
78 | - } |
|
79 | - if (isset($offset)) { |
|
80 | - $offset = $idx; |
|
81 | - } |
|
82 | - return new self($length, $indefinite); |
|
83 | - } |
|
42 | + /** |
|
43 | + * Decode length component from DER data. |
|
44 | + * |
|
45 | + * @param string $data DER encoded data |
|
46 | + * @param null|int $offset Reference to the variable that contains offset |
|
47 | + * into the data where to start parsing. |
|
48 | + * Variable is updated to the offset next to the |
|
49 | + * parsed length component. If null, start from offset 0. |
|
50 | + * |
|
51 | + * @throws DecodeException If decoding fails |
|
52 | + * |
|
53 | + * @return self |
|
54 | + */ |
|
55 | + public static function fromDER(string $data, int &$offset = null): self |
|
56 | + { |
|
57 | + $idx = $offset ?? 0; |
|
58 | + $datalen = strlen($data); |
|
59 | + if ($idx >= $datalen) { |
|
60 | + throw new DecodeException( |
|
61 | + 'Unexpected end of data while decoding length.'); |
|
62 | + } |
|
63 | + $indefinite = false; |
|
64 | + $byte = ord($data[$idx++]); |
|
65 | + // bits 7 to 1 |
|
66 | + $length = (0x7f & $byte); |
|
67 | + // long form |
|
68 | + if (0x80 & $byte) { |
|
69 | + if (!$length) { |
|
70 | + $indefinite = true; |
|
71 | + } else { |
|
72 | + if ($idx + $length > $datalen) { |
|
73 | + throw new DecodeException( |
|
74 | + 'Unexpected end of data while decoding long form length.'); |
|
75 | + } |
|
76 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | + } |
|
78 | + } |
|
79 | + if (isset($offset)) { |
|
80 | + $offset = $idx; |
|
81 | + } |
|
82 | + return new self($length, $indefinite); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Decode length from DER. |
|
87 | - * |
|
88 | - * Throws an exception if length doesn't match with expected or if data |
|
89 | - * doesn't contain enough bytes. |
|
90 | - * |
|
91 | - * Requirement of definite length is relaxed contrary to the specification |
|
92 | - * (sect. 10.1). |
|
93 | - * |
|
94 | - * @see self::fromDER |
|
95 | - * |
|
96 | - * @param string $data DER data |
|
97 | - * @param int $offset Reference to the offset variable |
|
98 | - * @param null|int $expected Expected length, null to bypass checking |
|
99 | - * |
|
100 | - * @throws DecodeException If decoding or expectation fails |
|
101 | - * |
|
102 | - * @return self |
|
103 | - */ |
|
104 | - public static function expectFromDER(string $data, int &$offset, |
|
105 | - int $expected = null): self |
|
106 | - { |
|
107 | - $idx = $offset; |
|
108 | - $length = self::fromDER($data, $idx); |
|
109 | - // if certain length was expected |
|
110 | - if (isset($expected)) { |
|
111 | - if ($length->isIndefinite()) { |
|
112 | - throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | - $expected); |
|
114 | - } |
|
115 | - if ($expected !== $length->intLength()) { |
|
116 | - throw new DecodeException( |
|
117 | - sprintf('Expected length %d, got %d.', $expected, |
|
118 | - $length->intLength())); |
|
119 | - } |
|
120 | - } |
|
121 | - // check that enough data is available |
|
122 | - if (!$length->isIndefinite() && |
|
123 | - strlen($data) < $idx + $length->intLength()) { |
|
124 | - throw new DecodeException( |
|
125 | - sprintf('Length %d overflows data, %d bytes left.', |
|
126 | - $length->intLength(), strlen($data) - $idx)); |
|
127 | - } |
|
128 | - $offset = $idx; |
|
129 | - return $length; |
|
130 | - } |
|
85 | + /** |
|
86 | + * Decode length from DER. |
|
87 | + * |
|
88 | + * Throws an exception if length doesn't match with expected or if data |
|
89 | + * doesn't contain enough bytes. |
|
90 | + * |
|
91 | + * Requirement of definite length is relaxed contrary to the specification |
|
92 | + * (sect. 10.1). |
|
93 | + * |
|
94 | + * @see self::fromDER |
|
95 | + * |
|
96 | + * @param string $data DER data |
|
97 | + * @param int $offset Reference to the offset variable |
|
98 | + * @param null|int $expected Expected length, null to bypass checking |
|
99 | + * |
|
100 | + * @throws DecodeException If decoding or expectation fails |
|
101 | + * |
|
102 | + * @return self |
|
103 | + */ |
|
104 | + public static function expectFromDER(string $data, int &$offset, |
|
105 | + int $expected = null): self |
|
106 | + { |
|
107 | + $idx = $offset; |
|
108 | + $length = self::fromDER($data, $idx); |
|
109 | + // if certain length was expected |
|
110 | + if (isset($expected)) { |
|
111 | + if ($length->isIndefinite()) { |
|
112 | + throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | + $expected); |
|
114 | + } |
|
115 | + if ($expected !== $length->intLength()) { |
|
116 | + throw new DecodeException( |
|
117 | + sprintf('Expected length %d, got %d.', $expected, |
|
118 | + $length->intLength())); |
|
119 | + } |
|
120 | + } |
|
121 | + // check that enough data is available |
|
122 | + if (!$length->isIndefinite() && |
|
123 | + strlen($data) < $idx + $length->intLength()) { |
|
124 | + throw new DecodeException( |
|
125 | + sprintf('Length %d overflows data, %d bytes left.', |
|
126 | + $length->intLength(), strlen($data) - $idx)); |
|
127 | + } |
|
128 | + $offset = $idx; |
|
129 | + return $length; |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * @see Encodable::toDER() |
|
134 | - * |
|
135 | - * @throws \DomainException If length is too large to encode |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function toDER(): string |
|
140 | - { |
|
141 | - $bytes = []; |
|
142 | - if ($this->_indefinite) { |
|
143 | - $bytes[] = 0x80; |
|
144 | - } else { |
|
145 | - $num = $this->_length->gmpObj(); |
|
146 | - // long form |
|
147 | - if ($num > 127) { |
|
148 | - $octets = []; |
|
149 | - for (; $num > 0; $num >>= 8) { |
|
150 | - $octets[] = gmp_intval(0xff & $num); |
|
151 | - } |
|
152 | - $count = count($octets); |
|
153 | - // first octet must not be 0xff |
|
154 | - if ($count >= 127) { |
|
155 | - throw new \DomainException('Too many length octets.'); |
|
156 | - } |
|
157 | - $bytes[] = 0x80 | $count; |
|
158 | - foreach (array_reverse($octets) as $octet) { |
|
159 | - $bytes[] = $octet; |
|
160 | - } |
|
161 | - } |
|
162 | - // short form |
|
163 | - else { |
|
164 | - $bytes[] = gmp_intval($num); |
|
165 | - } |
|
166 | - } |
|
167 | - return pack('C*', ...$bytes); |
|
168 | - } |
|
132 | + /** |
|
133 | + * @see Encodable::toDER() |
|
134 | + * |
|
135 | + * @throws \DomainException If length is too large to encode |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function toDER(): string |
|
140 | + { |
|
141 | + $bytes = []; |
|
142 | + if ($this->_indefinite) { |
|
143 | + $bytes[] = 0x80; |
|
144 | + } else { |
|
145 | + $num = $this->_length->gmpObj(); |
|
146 | + // long form |
|
147 | + if ($num > 127) { |
|
148 | + $octets = []; |
|
149 | + for (; $num > 0; $num >>= 8) { |
|
150 | + $octets[] = gmp_intval(0xff & $num); |
|
151 | + } |
|
152 | + $count = count($octets); |
|
153 | + // first octet must not be 0xff |
|
154 | + if ($count >= 127) { |
|
155 | + throw new \DomainException('Too many length octets.'); |
|
156 | + } |
|
157 | + $bytes[] = 0x80 | $count; |
|
158 | + foreach (array_reverse($octets) as $octet) { |
|
159 | + $bytes[] = $octet; |
|
160 | + } |
|
161 | + } |
|
162 | + // short form |
|
163 | + else { |
|
164 | + $bytes[] = gmp_intval($num); |
|
165 | + } |
|
166 | + } |
|
167 | + return pack('C*', ...$bytes); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Get the length. |
|
172 | - * |
|
173 | - * @throws \LogicException If length is indefinite |
|
174 | - * |
|
175 | - * @return string Length as an integer string |
|
176 | - */ |
|
177 | - public function length(): string |
|
178 | - { |
|
179 | - if ($this->_indefinite) { |
|
180 | - throw new \LogicException('Length is indefinite.'); |
|
181 | - } |
|
182 | - return $this->_length->base10(); |
|
183 | - } |
|
170 | + /** |
|
171 | + * Get the length. |
|
172 | + * |
|
173 | + * @throws \LogicException If length is indefinite |
|
174 | + * |
|
175 | + * @return string Length as an integer string |
|
176 | + */ |
|
177 | + public function length(): string |
|
178 | + { |
|
179 | + if ($this->_indefinite) { |
|
180 | + throw new \LogicException('Length is indefinite.'); |
|
181 | + } |
|
182 | + return $this->_length->base10(); |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * Get the length as an integer. |
|
187 | - * |
|
188 | - * @throws \LogicException If length is indefinite |
|
189 | - * @throws \RuntimeException If length overflows integer size |
|
190 | - * |
|
191 | - * @return int |
|
192 | - */ |
|
193 | - public function intLength(): int |
|
194 | - { |
|
195 | - if ($this->_indefinite) { |
|
196 | - throw new \LogicException('Length is indefinite.'); |
|
197 | - } |
|
198 | - return $this->_length->intVal(); |
|
199 | - } |
|
185 | + /** |
|
186 | + * Get the length as an integer. |
|
187 | + * |
|
188 | + * @throws \LogicException If length is indefinite |
|
189 | + * @throws \RuntimeException If length overflows integer size |
|
190 | + * |
|
191 | + * @return int |
|
192 | + */ |
|
193 | + public function intLength(): int |
|
194 | + { |
|
195 | + if ($this->_indefinite) { |
|
196 | + throw new \LogicException('Length is indefinite.'); |
|
197 | + } |
|
198 | + return $this->_length->intVal(); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Whether length is indefinite. |
|
203 | - * |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - public function isIndefinite(): bool |
|
207 | - { |
|
208 | - return $this->_indefinite; |
|
209 | - } |
|
201 | + /** |
|
202 | + * Whether length is indefinite. |
|
203 | + * |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + public function isIndefinite(): bool |
|
207 | + { |
|
208 | + return $this->_indefinite; |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Decode long form length. |
|
213 | - * |
|
214 | - * @param int $length Number of octets |
|
215 | - * @param string $data Data |
|
216 | - * @param int $offset reference to the variable containing offset to the data |
|
217 | - * |
|
218 | - * @throws DecodeException If decoding fails |
|
219 | - * |
|
220 | - * @return string Integer as a string |
|
221 | - */ |
|
222 | - private static function _decodeLongFormLength(int $length, string $data, |
|
223 | - int &$offset): string |
|
224 | - { |
|
225 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
226 | - if (127 === $length) { |
|
227 | - throw new DecodeException('Invalid number of length octets.'); |
|
228 | - } |
|
229 | - $num = gmp_init(0, 10); |
|
230 | - while (--$length >= 0) { |
|
231 | - $byte = ord($data[$offset++]); |
|
232 | - $num <<= 8; |
|
233 | - $num |= $byte; |
|
234 | - } |
|
235 | - return gmp_strval($num); |
|
236 | - } |
|
211 | + /** |
|
212 | + * Decode long form length. |
|
213 | + * |
|
214 | + * @param int $length Number of octets |
|
215 | + * @param string $data Data |
|
216 | + * @param int $offset reference to the variable containing offset to the data |
|
217 | + * |
|
218 | + * @throws DecodeException If decoding fails |
|
219 | + * |
|
220 | + * @return string Integer as a string |
|
221 | + */ |
|
222 | + private static function _decodeLongFormLength(int $length, string $data, |
|
223 | + int &$offset): string |
|
224 | + { |
|
225 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
226 | + if (127 === $length) { |
|
227 | + throw new DecodeException('Invalid number of length octets.'); |
|
228 | + } |
|
229 | + $num = gmp_init(0, 10); |
|
230 | + while (--$length >= 0) { |
|
231 | + $byte = ord($data[$offset++]); |
|
232 | + $num <<= 8; |
|
233 | + $num |= $byte; |
|
234 | + } |
|
235 | + return gmp_strval($num); |
|
236 | + } |
|
237 | 237 | } |
@@ -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\Feature; |
6 | 6 |
@@ -13,90 +13,90 @@ |
||
13 | 13 | */ |
14 | 14 | interface ElementBase extends Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Get the class of the ASN.1 type. |
|
18 | - * |
|
19 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
20 | - * |
|
21 | - * @return int |
|
22 | - */ |
|
23 | - public function typeClass(): int; |
|
16 | + /** |
|
17 | + * Get the class of the ASN.1 type. |
|
18 | + * |
|
19 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
20 | + * |
|
21 | + * @return int |
|
22 | + */ |
|
23 | + public function typeClass(): int; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Check whether the element is constructed. |
|
27 | - * |
|
28 | - * Otherwise it's primitive. |
|
29 | - * |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function isConstructed(): bool; |
|
25 | + /** |
|
26 | + * Check whether the element is constructed. |
|
27 | + * |
|
28 | + * Otherwise it's primitive. |
|
29 | + * |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function isConstructed(): bool; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the tag of the element. |
|
36 | - * |
|
37 | - * Interpretation of the tag depends on the context. For example it may |
|
38 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | - * tagged type. |
|
40 | - * |
|
41 | - * @return int |
|
42 | - */ |
|
43 | - public function tag(): int; |
|
34 | + /** |
|
35 | + * Get the tag of the element. |
|
36 | + * |
|
37 | + * Interpretation of the tag depends on the context. For example it may |
|
38 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | + * tagged type. |
|
40 | + * |
|
41 | + * @return int |
|
42 | + */ |
|
43 | + public function tag(): int; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Check whether the element is a type of a given tag. |
|
47 | - * |
|
48 | - * @param int $tag Type tag |
|
49 | - * |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - public function isType(int $tag): bool; |
|
45 | + /** |
|
46 | + * Check whether the element is a type of a given tag. |
|
47 | + * |
|
48 | + * @param int $tag Type tag |
|
49 | + * |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function isType(int $tag): bool; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Check whether the element is a type of a given tag. |
|
56 | - * |
|
57 | - * Throws an exception if expectation fails. |
|
58 | - * |
|
59 | - * @param int $tag Type tag |
|
60 | - * |
|
61 | - * @throws \UnexpectedValueException If the element type differs from the expected |
|
62 | - * |
|
63 | - * @return ElementBase |
|
64 | - */ |
|
65 | - public function expectType(int $tag): ElementBase; |
|
54 | + /** |
|
55 | + * Check whether the element is a type of a given tag. |
|
56 | + * |
|
57 | + * Throws an exception if expectation fails. |
|
58 | + * |
|
59 | + * @param int $tag Type tag |
|
60 | + * |
|
61 | + * @throws \UnexpectedValueException If the element type differs from the expected |
|
62 | + * |
|
63 | + * @return ElementBase |
|
64 | + */ |
|
65 | + public function expectType(int $tag): ElementBase; |
|
66 | 66 | |
67 | - /** |
|
68 | - * Check whether the element is tagged (context specific). |
|
69 | - * |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - public function isTagged(): bool; |
|
67 | + /** |
|
68 | + * Check whether the element is tagged (context specific). |
|
69 | + * |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + public function isTagged(): bool; |
|
73 | 73 | |
74 | - /** |
|
75 | - * Check whether the element is tagged (context specific) and optionally has |
|
76 | - * a given tag. |
|
77 | - * |
|
78 | - * Throws an exception if the element is not tagged or tag differs from |
|
79 | - * the expected. |
|
80 | - * |
|
81 | - * @param null|int $tag Optional type tag |
|
82 | - * |
|
83 | - * @throws \UnexpectedValueException If expectation fails |
|
84 | - * |
|
85 | - * @return TaggedType |
|
86 | - */ |
|
87 | - public function expectTagged(?int $tag = null): TaggedType; |
|
74 | + /** |
|
75 | + * Check whether the element is tagged (context specific) and optionally has |
|
76 | + * a given tag. |
|
77 | + * |
|
78 | + * Throws an exception if the element is not tagged or tag differs from |
|
79 | + * the expected. |
|
80 | + * |
|
81 | + * @param null|int $tag Optional type tag |
|
82 | + * |
|
83 | + * @throws \UnexpectedValueException If expectation fails |
|
84 | + * |
|
85 | + * @return TaggedType |
|
86 | + */ |
|
87 | + public function expectTagged(?int $tag = null): TaggedType; |
|
88 | 88 | |
89 | - /** |
|
90 | - * Get the object as an abstract Element instance. |
|
91 | - * |
|
92 | - * @return Element |
|
93 | - */ |
|
94 | - public function asElement(): Element; |
|
89 | + /** |
|
90 | + * Get the object as an abstract Element instance. |
|
91 | + * |
|
92 | + * @return Element |
|
93 | + */ |
|
94 | + public function asElement(): Element; |
|
95 | 95 | |
96 | - /** |
|
97 | - * Get the object as an UnspecifiedType instance. |
|
98 | - * |
|
99 | - * @return UnspecifiedType |
|
100 | - */ |
|
101 | - public function asUnspecified(): UnspecifiedType; |
|
96 | + /** |
|
97 | + * Get the object as an UnspecifiedType instance. |
|
98 | + * |
|
99 | + * @return UnspecifiedType |
|
100 | + */ |
|
101 | + public function asUnspecified(): UnspecifiedType; |
|
102 | 102 | } |
@@ -12,56 +12,56 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor. |
|
17 | - * |
|
18 | - * @param Element ...$elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor. |
|
17 | + * |
|
18 | + * @param Element ...$elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * |
|
29 | - * Used for DER encoding of SET type. |
|
30 | - * |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function sortedSet(): self |
|
34 | - { |
|
35 | - $obj = clone $this; |
|
36 | - usort($obj->_elements, |
|
37 | - function (Element $a, Element $b) { |
|
38 | - if ($a->typeClass() !== $b->typeClass()) { |
|
39 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | - } |
|
41 | - if ($a->tag() === $b->tag()) { |
|
42 | - return 0; |
|
43 | - } |
|
44 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | - }); |
|
46 | - return $obj; |
|
47 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * |
|
29 | + * Used for DER encoding of SET type. |
|
30 | + * |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function sortedSet(): self |
|
34 | + { |
|
35 | + $obj = clone $this; |
|
36 | + usort($obj->_elements, |
|
37 | + function (Element $a, Element $b) { |
|
38 | + if ($a->typeClass() !== $b->typeClass()) { |
|
39 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | + } |
|
41 | + if ($a->tag() === $b->tag()) { |
|
42 | + return 0; |
|
43 | + } |
|
44 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | + }); |
|
46 | + return $obj; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Sort by encoding ascending order. |
|
51 | - * |
|
52 | - * Used for DER encoding of SET OF type. |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public function sortedSetOf(): self |
|
57 | - { |
|
58 | - $obj = clone $this; |
|
59 | - usort($obj->_elements, |
|
60 | - function (Element $a, Element $b) { |
|
61 | - $a_der = $a->toDER(); |
|
62 | - $b_der = $b->toDER(); |
|
63 | - return strcmp($a_der, $b_der); |
|
64 | - }); |
|
65 | - return $obj; |
|
66 | - } |
|
49 | + /** |
|
50 | + * Sort by encoding ascending order. |
|
51 | + * |
|
52 | + * Used for DER encoding of SET OF type. |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public function sortedSetOf(): self |
|
57 | + { |
|
58 | + $obj = clone $this; |
|
59 | + usort($obj->_elements, |
|
60 | + function (Element $a, Element $b) { |
|
61 | + $a_der = $a->toDER(); |
|
62 | + $b_der = $b->toDER(); |
|
63 | + return strcmp($a_der, $b_der); |
|
64 | + }); |
|
65 | + return $obj; |
|
66 | + } |
|
67 | 67 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Constructed; |
6 | 6 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | { |
35 | 35 | $obj = clone $this; |
36 | 36 | usort($obj->_elements, |
37 | - function (Element $a, Element $b) { |
|
37 | + function(Element $a, Element $b) { |
|
38 | 38 | if ($a->typeClass() !== $b->typeClass()) { |
39 | 39 | return $a->typeClass() < $b->typeClass() ? -1 : 1; |
40 | 40 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | { |
58 | 58 | $obj = clone $this; |
59 | 59 | usort($obj->_elements, |
60 | - function (Element $a, Element $b) { |
|
60 | + function(Element $a, Element $b) { |
|
61 | 61 | $a_der = $a->toDER(); |
62 | 62 | $b_der = $b->toDER(); |
63 | 63 | return strcmp($a_der, $b_der); |
@@ -26,108 +26,108 @@ |
||
26 | 26 | */ |
27 | 27 | class ConstructedString extends Structure implements Stringable |
28 | 28 | { |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @internal Use create() method instead |
|
33 | - * |
|
34 | - * @param Element ...$elements Any number of elements |
|
35 | - */ |
|
36 | - protected function __construct(Element ...$elements) |
|
37 | - { |
|
38 | - parent::__construct(...$elements); |
|
39 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @internal Use create() method instead |
|
33 | + * |
|
34 | + * @param Element ...$elements Any number of elements |
|
35 | + */ |
|
36 | + protected function __construct(Element ...$elements) |
|
37 | + { |
|
38 | + parent::__construct(...$elements); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function __toString(): string |
|
47 | - { |
|
48 | - return $this->string(); |
|
49 | - } |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function __toString(): string |
|
47 | + { |
|
48 | + return $this->string(); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Create from a list of string type elements. |
|
53 | - * |
|
54 | - * All strings must have the same type. |
|
55 | - * |
|
56 | - * @param StringType ...$elements |
|
57 | - * |
|
58 | - * @throws \LogicException |
|
59 | - * |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - public static function create(StringType ...$elements): self |
|
63 | - { |
|
64 | - if (!count($elements)) { |
|
65 | - throw new \LogicException( |
|
66 | - 'No elements, unable to determine type tag.'); |
|
67 | - } |
|
68 | - $tag = $elements[0]->tag(); |
|
69 | - foreach ($elements as $el) { |
|
70 | - if ($el->tag() !== $tag) { |
|
71 | - throw new \LogicException( |
|
72 | - 'All elements in constructed string must have the same type.'); |
|
73 | - } |
|
74 | - } |
|
75 | - return self::createWithTag($tag, ...$elements); |
|
76 | - } |
|
51 | + /** |
|
52 | + * Create from a list of string type elements. |
|
53 | + * |
|
54 | + * All strings must have the same type. |
|
55 | + * |
|
56 | + * @param StringType ...$elements |
|
57 | + * |
|
58 | + * @throws \LogicException |
|
59 | + * |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + public static function create(StringType ...$elements): self |
|
63 | + { |
|
64 | + if (!count($elements)) { |
|
65 | + throw new \LogicException( |
|
66 | + 'No elements, unable to determine type tag.'); |
|
67 | + } |
|
68 | + $tag = $elements[0]->tag(); |
|
69 | + foreach ($elements as $el) { |
|
70 | + if ($el->tag() !== $tag) { |
|
71 | + throw new \LogicException( |
|
72 | + 'All elements in constructed string must have the same type.'); |
|
73 | + } |
|
74 | + } |
|
75 | + return self::createWithTag($tag, ...$elements); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Create from strings with a given type tag. |
|
80 | - * |
|
81 | - * Does not perform any validation on types. |
|
82 | - * |
|
83 | - * @param int $tag Type tag for the constructed string element |
|
84 | - * @param StringType ...$elements Any number of elements |
|
85 | - * |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public static function createWithTag(int $tag, StringType ...$elements) |
|
89 | - { |
|
90 | - $el = new self(...$elements); |
|
91 | - $el->_typeTag = $tag; |
|
92 | - return $el; |
|
93 | - } |
|
78 | + /** |
|
79 | + * Create from strings with a given type tag. |
|
80 | + * |
|
81 | + * Does not perform any validation on types. |
|
82 | + * |
|
83 | + * @param int $tag Type tag for the constructed string element |
|
84 | + * @param StringType ...$elements Any number of elements |
|
85 | + * |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public static function createWithTag(int $tag, StringType ...$elements) |
|
89 | + { |
|
90 | + $el = new self(...$elements); |
|
91 | + $el->_typeTag = $tag; |
|
92 | + return $el; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Get a list of strings in this structure. |
|
97 | - * |
|
98 | - * @return string[] |
|
99 | - */ |
|
100 | - public function strings(): array |
|
101 | - { |
|
102 | - return array_map(function (StringType $el) { |
|
103 | - return $el->string(); |
|
104 | - }, $this->_elements); |
|
105 | - } |
|
95 | + /** |
|
96 | + * Get a list of strings in this structure. |
|
97 | + * |
|
98 | + * @return string[] |
|
99 | + */ |
|
100 | + public function strings(): array |
|
101 | + { |
|
102 | + return array_map(function (StringType $el) { |
|
103 | + return $el->string(); |
|
104 | + }, $this->_elements); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Get the contained strings concatenated together. |
|
109 | - * |
|
110 | - * NOTE: It's unclear how bit strings with unused bits should be concatentated. |
|
111 | - * |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function string(): string |
|
115 | - { |
|
116 | - return implode('', $this->strings()); |
|
117 | - } |
|
107 | + /** |
|
108 | + * Get the contained strings concatenated together. |
|
109 | + * |
|
110 | + * NOTE: It's unclear how bit strings with unused bits should be concatentated. |
|
111 | + * |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function string(): string |
|
115 | + { |
|
116 | + return implode('', $this->strings()); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * {@inheritdoc} |
|
121 | - * |
|
122 | - * @return self |
|
123 | - */ |
|
124 | - protected static function _decodeFromDER(Identifier $identifier, |
|
125 | - string $data, int &$offset): ElementBase |
|
126 | - { |
|
127 | - /** @var ConstructedString $type */ |
|
128 | - $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
129 | - [$identifier, $data, &$offset]); |
|
130 | - $type->_typeTag = $identifier->intTag(); |
|
131 | - return $type; |
|
132 | - } |
|
119 | + /** |
|
120 | + * {@inheritdoc} |
|
121 | + * |
|
122 | + * @return self |
|
123 | + */ |
|
124 | + protected static function _decodeFromDER(Identifier $identifier, |
|
125 | + string $data, int &$offset): ElementBase |
|
126 | + { |
|
127 | + /** @var ConstructedString $type */ |
|
128 | + $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
129 | + [$identifier, $data, &$offset]); |
|
130 | + $type->_typeTag = $identifier->intTag(); |
|
131 | + return $type; |
|
132 | + } |
|
133 | 133 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Constructed; |
6 | 6 | |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function strings(): array |
101 | 101 | { |
102 | - return array_map(function (StringType $el) { |
|
102 | + return array_map(function(StringType $el) { |
|
103 | 103 | return $el->string(); |
104 | 104 | }, $this->_elements); |
105 | 105 | } |
@@ -9,17 +9,17 @@ |
||
9 | 9 | */ |
10 | 10 | interface Stringable |
11 | 11 | { |
12 | - /** |
|
13 | - * Convert object to string. |
|
14 | - * |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function __toString(): string; |
|
12 | + /** |
|
13 | + * Convert object to string. |
|
14 | + * |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function __toString(): string; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Get the string representation of the type. |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function string(): string; |
|
19 | + /** |
|
20 | + * Get the string representation of the type. |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function string(): string; |
|
25 | 25 | } |
@@ -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\Feature; |
6 | 6 |