@@ -17,72 +17,72 @@ |
||
17 | 17 | */ |
18 | 18 | class UTCTime extends TimeType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = /* @formatter:off */ '#^' . |
|
31 | - '(\d\d)' . /* YY */ |
|
32 | - '(\d\d)' . /* MM */ |
|
33 | - '(\d\d)' . /* DD */ |
|
34 | - '(\d\d)' . /* hh */ |
|
35 | - '(\d\d)' . /* mm */ |
|
36 | - '(\d\d)' . /* ss */ |
|
37 | - 'Z' . /* TZ */ |
|
38 | - '$#' /* @formatter:on */; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = /* @formatter:off */ '#^' . |
|
31 | + '(\d\d)' . /* YY */ |
|
32 | + '(\d\d)' . /* MM */ |
|
33 | + '(\d\d)' . /* DD */ |
|
34 | + '(\d\d)' . /* hh */ |
|
35 | + '(\d\d)' . /* mm */ |
|
36 | + '(\d\d)' . /* ss */ |
|
37 | + 'Z' . /* TZ */ |
|
38 | + '$#' /* @formatter:on */; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor. |
|
42 | - * |
|
43 | - * @param \DateTimeImmutable $dt |
|
44 | - */ |
|
45 | - public function __construct(\DateTimeImmutable $dt) |
|
46 | - { |
|
47 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | - parent::__construct($dt); |
|
49 | - } |
|
40 | + /** |
|
41 | + * Constructor. |
|
42 | + * |
|
43 | + * @param \DateTimeImmutable $dt |
|
44 | + */ |
|
45 | + public function __construct(\DateTimeImmutable $dt) |
|
46 | + { |
|
47 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | + parent::__construct($dt); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
58 | - return $dt->format("ymdHis\Z"); |
|
59 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
58 | + return $dt->format("ymdHis\Z"); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * |
|
63 | - * {@inheritdoc} |
|
64 | - * @return self |
|
65 | - */ |
|
66 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
67 | - int &$offset): ElementBase |
|
68 | - { |
|
69 | - $idx = $offset; |
|
70 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
71 | - $str = substr($data, $idx, $length); |
|
72 | - $idx += $length; |
|
73 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
74 | - throw new DecodeException("Invalid UTCTime format."); |
|
75 | - } |
|
76 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
77 | - $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
78 | - $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
79 | - self::_createTimeZone(self::TZ_UTC)); |
|
80 | - if (!$dt) { |
|
81 | - throw new DecodeException( |
|
82 | - "Failed to decode UTCTime: " . |
|
83 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
84 | - } |
|
85 | - $offset = $idx; |
|
86 | - return new self($dt); |
|
87 | - } |
|
61 | + /** |
|
62 | + * |
|
63 | + * {@inheritdoc} |
|
64 | + * @return self |
|
65 | + */ |
|
66 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
67 | + int &$offset): ElementBase |
|
68 | + { |
|
69 | + $idx = $offset; |
|
70 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
71 | + $str = substr($data, $idx, $length); |
|
72 | + $idx += $length; |
|
73 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
74 | + throw new DecodeException("Invalid UTCTime format."); |
|
75 | + } |
|
76 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
77 | + $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
78 | + $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
79 | + self::_createTimeZone(self::TZ_UTC)); |
|
80 | + if (!$dt) { |
|
81 | + throw new DecodeException( |
|
82 | + "Failed to decode UTCTime: " . |
|
83 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
84 | + } |
|
85 | + $offset = $idx; |
|
86 | + return new self($dt); |
|
87 | + } |
|
88 | 88 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Primitive; |
6 | 6 |
@@ -71,11 +71,11 @@ |
||
71 | 71 | $num = $this->_number->gmpObj(); |
72 | 72 | switch (gmp_sign($num)) { |
73 | 73 | // positive |
74 | - case 1: |
|
75 | - return self::_encodePositiveInteger($num); |
|
76 | - // negative |
|
77 | - case -1: |
|
78 | - return self::_encodeNegativeInteger($num); |
|
74 | + case 1: |
|
75 | + return self::_encodePositiveInteger($num); |
|
76 | + // negative |
|
77 | + case -1: |
|
78 | + return self::_encodeNegativeInteger($num); |
|
79 | 79 | } |
80 | 80 | // zero |
81 | 81 | return "\0"; |
@@ -17,161 +17,161 @@ |
||
17 | 17 | */ |
18 | 18 | class Integer extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * The number. |
|
25 | - * |
|
26 | - * @var BigInt |
|
27 | - */ |
|
28 | - private $_number; |
|
23 | + /** |
|
24 | + * The number. |
|
25 | + * |
|
26 | + * @var BigInt |
|
27 | + */ |
|
28 | + private $_number; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $number Base 10 integer |
|
34 | - */ |
|
35 | - public function __construct($number) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_INTEGER; |
|
38 | - if (!self::_validateNumber($number)) { |
|
39 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
40 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
41 | - } |
|
42 | - $this->_number = new BigInt($number); |
|
43 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $number Base 10 integer |
|
34 | + */ |
|
35 | + public function __construct($number) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_INTEGER; |
|
38 | + if (!self::_validateNumber($number)) { |
|
39 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
40 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
41 | + } |
|
42 | + $this->_number = new BigInt($number); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get the number as a base 10. |
|
47 | - * |
|
48 | - * @return string Integer as a string |
|
49 | - */ |
|
50 | - public function number(): string |
|
51 | - { |
|
52 | - return $this->_number->base10(); |
|
53 | - } |
|
45 | + /** |
|
46 | + * Get the number as a base 10. |
|
47 | + * |
|
48 | + * @return string Integer as a string |
|
49 | + */ |
|
50 | + public function number(): string |
|
51 | + { |
|
52 | + return $this->_number->base10(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get the number as an integer type. |
|
57 | - * |
|
58 | - * @return int |
|
59 | - */ |
|
60 | - public function intNumber(): int |
|
61 | - { |
|
62 | - return $this->_number->intVal(); |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get the number as an integer type. |
|
57 | + * |
|
58 | + * @return int |
|
59 | + */ |
|
60 | + public function intNumber(): int |
|
61 | + { |
|
62 | + return $this->_number->intVal(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected function _encodedContentDER(): string |
|
70 | - { |
|
71 | - $num = $this->_number->gmpObj(); |
|
72 | - switch (gmp_sign($num)) { |
|
73 | - // positive |
|
74 | - case 1: |
|
75 | - return self::_encodePositiveInteger($num); |
|
76 | - // negative |
|
77 | - case -1: |
|
78 | - return self::_encodeNegativeInteger($num); |
|
79 | - } |
|
80 | - // zero |
|
81 | - return "\0"; |
|
82 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected function _encodedContentDER(): string |
|
70 | + { |
|
71 | + $num = $this->_number->gmpObj(); |
|
72 | + switch (gmp_sign($num)) { |
|
73 | + // positive |
|
74 | + case 1: |
|
75 | + return self::_encodePositiveInteger($num); |
|
76 | + // negative |
|
77 | + case -1: |
|
78 | + return self::_encodeNegativeInteger($num); |
|
79 | + } |
|
80 | + // zero |
|
81 | + return "\0"; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Encode positive integer to DER content. |
|
86 | - * |
|
87 | - * @param \GMP $num |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - private static function _encodePositiveInteger(\GMP $num): string |
|
91 | - { |
|
92 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
93 | - // if first bit is 1, prepend full zero byte |
|
94 | - // to represent positive two's complement |
|
95 | - if (ord($bin[0]) & 0x80) { |
|
96 | - $bin = chr(0x00) . $bin; |
|
97 | - } |
|
98 | - return $bin; |
|
99 | - } |
|
84 | + /** |
|
85 | + * Encode positive integer to DER content. |
|
86 | + * |
|
87 | + * @param \GMP $num |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + private static function _encodePositiveInteger(\GMP $num): string |
|
91 | + { |
|
92 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
93 | + // if first bit is 1, prepend full zero byte |
|
94 | + // to represent positive two's complement |
|
95 | + if (ord($bin[0]) & 0x80) { |
|
96 | + $bin = chr(0x00) . $bin; |
|
97 | + } |
|
98 | + return $bin; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Encode negative integer to DER content. |
|
103 | - * |
|
104 | - * @param \GMP $num |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
108 | - { |
|
109 | - $num = gmp_abs($num); |
|
110 | - // compute number of bytes required |
|
111 | - $width = 1; |
|
112 | - if ($num > 128) { |
|
113 | - $tmp = $num; |
|
114 | - do { |
|
115 | - $width++; |
|
116 | - $tmp >>= 8; |
|
117 | - } while ($tmp > 128); |
|
118 | - } |
|
119 | - // compute two's complement 2^n - x |
|
120 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
121 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | - // if first bit is 0, prepend full inverted byte |
|
123 | - // to represent negative two's complement |
|
124 | - if (!(ord($bin[0]) & 0x80)) { |
|
125 | - $bin = chr(0xff) . $bin; |
|
126 | - } |
|
127 | - return $bin; |
|
128 | - } |
|
101 | + /** |
|
102 | + * Encode negative integer to DER content. |
|
103 | + * |
|
104 | + * @param \GMP $num |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
108 | + { |
|
109 | + $num = gmp_abs($num); |
|
110 | + // compute number of bytes required |
|
111 | + $width = 1; |
|
112 | + if ($num > 128) { |
|
113 | + $tmp = $num; |
|
114 | + do { |
|
115 | + $width++; |
|
116 | + $tmp >>= 8; |
|
117 | + } while ($tmp > 128); |
|
118 | + } |
|
119 | + // compute two's complement 2^n - x |
|
120 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
121 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | + // if first bit is 0, prepend full inverted byte |
|
123 | + // to represent negative two's complement |
|
124 | + if (!(ord($bin[0]) & 0x80)) { |
|
125 | + $bin = chr(0xff) . $bin; |
|
126 | + } |
|
127 | + return $bin; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * |
|
132 | - * {@inheritdoc} |
|
133 | - * @return self |
|
134 | - */ |
|
135 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
136 | - int &$offset): ElementBase |
|
137 | - { |
|
138 | - $idx = $offset; |
|
139 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
140 | - $bytes = substr($data, $idx, $length); |
|
141 | - $idx += $length; |
|
142 | - $neg = ord($bytes[0]) & 0x80; |
|
143 | - // negative, apply inversion of two's complement |
|
144 | - if ($neg) { |
|
145 | - $len = strlen($bytes); |
|
146 | - for ($i = 0; $i < $len; $i++) { |
|
147 | - $bytes[$i] = ~$bytes[$i]; |
|
148 | - } |
|
149 | - } |
|
150 | - $num = gmp_init(bin2hex($bytes), 16); |
|
151 | - // negative, apply addition of two's complement |
|
152 | - // and produce negative result |
|
153 | - if ($neg) { |
|
154 | - $num = gmp_neg($num + 1); |
|
155 | - } |
|
156 | - $offset = $idx; |
|
157 | - // late static binding since enumerated extends integer type |
|
158 | - return new static(gmp_strval($num, 10)); |
|
159 | - } |
|
130 | + /** |
|
131 | + * |
|
132 | + * {@inheritdoc} |
|
133 | + * @return self |
|
134 | + */ |
|
135 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
136 | + int &$offset): ElementBase |
|
137 | + { |
|
138 | + $idx = $offset; |
|
139 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
140 | + $bytes = substr($data, $idx, $length); |
|
141 | + $idx += $length; |
|
142 | + $neg = ord($bytes[0]) & 0x80; |
|
143 | + // negative, apply inversion of two's complement |
|
144 | + if ($neg) { |
|
145 | + $len = strlen($bytes); |
|
146 | + for ($i = 0; $i < $len; $i++) { |
|
147 | + $bytes[$i] = ~$bytes[$i]; |
|
148 | + } |
|
149 | + } |
|
150 | + $num = gmp_init(bin2hex($bytes), 16); |
|
151 | + // negative, apply addition of two's complement |
|
152 | + // and produce negative result |
|
153 | + if ($neg) { |
|
154 | + $num = gmp_neg($num + 1); |
|
155 | + } |
|
156 | + $offset = $idx; |
|
157 | + // late static binding since enumerated extends integer type |
|
158 | + return new static(gmp_strval($num, 10)); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Test that number is valid for this context. |
|
163 | - * |
|
164 | - * @param mixed $num |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - private static function _validateNumber($num): bool |
|
168 | - { |
|
169 | - if (is_int($num)) { |
|
170 | - return true; |
|
171 | - } |
|
172 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
173 | - return true; |
|
174 | - } |
|
175 | - return false; |
|
176 | - } |
|
161 | + /** |
|
162 | + * Test that number is valid for this context. |
|
163 | + * |
|
164 | + * @param mixed $num |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + private static function _validateNumber($num): bool |
|
168 | + { |
|
169 | + if (is_int($num)) { |
|
170 | + return true; |
|
171 | + } |
|
172 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
173 | + return true; |
|
174 | + } |
|
175 | + return false; |
|
176 | + } |
|
177 | 177 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Primitive; |
6 | 6 |
@@ -6,106 +6,106 @@ |
||
6 | 6 | |
7 | 7 | class BigInt |
8 | 8 | { |
9 | - /** |
|
10 | - * Number as a base10 integer string. |
|
11 | - * |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - private $_num; |
|
9 | + /** |
|
10 | + * Number as a base10 integer string. |
|
11 | + * |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + private $_num; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Number as an integer type. |
|
18 | - * |
|
19 | - * @internal Lazily initialized |
|
20 | - * @var int|null |
|
21 | - */ |
|
22 | - private $_intNum; |
|
16 | + /** |
|
17 | + * Number as an integer type. |
|
18 | + * |
|
19 | + * @internal Lazily initialized |
|
20 | + * @var int|null |
|
21 | + */ |
|
22 | + private $_intNum; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param string|int $num |
|
28 | - */ |
|
29 | - public function __construct($num) |
|
30 | - { |
|
31 | - $this->_num = strval($num); |
|
32 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param string|int $num |
|
28 | + */ |
|
29 | + public function __construct($num) |
|
30 | + { |
|
31 | + $this->_num = strval($num); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the number as a base10 integer string. |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function base10(): string |
|
40 | - { |
|
41 | - return $this->_num; |
|
42 | - } |
|
34 | + /** |
|
35 | + * Get the number as a base10 integer string. |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function base10(): string |
|
40 | + { |
|
41 | + return $this->_num; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get the number as an integer. |
|
46 | - * |
|
47 | - * @throws \RuntimeException If number overflows integer size |
|
48 | - * @return int |
|
49 | - */ |
|
50 | - public function intVal(): int |
|
51 | - { |
|
52 | - if (!isset($this->_intNum)) { |
|
53 | - $num = gmp_init($this->_num, 10); |
|
54 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
55 | - throw new \RuntimeException("Integer overflow."); |
|
56 | - } |
|
57 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
58 | - throw new \RuntimeException("Integer underflow."); |
|
59 | - } |
|
60 | - $this->_intNum = gmp_intval($num); |
|
61 | - } |
|
62 | - return $this->_intNum; |
|
63 | - } |
|
44 | + /** |
|
45 | + * Get the number as an integer. |
|
46 | + * |
|
47 | + * @throws \RuntimeException If number overflows integer size |
|
48 | + * @return int |
|
49 | + */ |
|
50 | + public function intVal(): int |
|
51 | + { |
|
52 | + if (!isset($this->_intNum)) { |
|
53 | + $num = gmp_init($this->_num, 10); |
|
54 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
55 | + throw new \RuntimeException("Integer overflow."); |
|
56 | + } |
|
57 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
58 | + throw new \RuntimeException("Integer underflow."); |
|
59 | + } |
|
60 | + $this->_intNum = gmp_intval($num); |
|
61 | + } |
|
62 | + return $this->_intNum; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Get the maximum integer value. |
|
67 | - * |
|
68 | - * @return \GMP |
|
69 | - */ |
|
70 | - private function _intMaxGmp(): \GMP |
|
71 | - { |
|
72 | - static $gmp; |
|
73 | - if (!isset($gmp)) { |
|
74 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
75 | - } |
|
76 | - return $gmp; |
|
77 | - } |
|
65 | + /** |
|
66 | + * Get the maximum integer value. |
|
67 | + * |
|
68 | + * @return \GMP |
|
69 | + */ |
|
70 | + private function _intMaxGmp(): \GMP |
|
71 | + { |
|
72 | + static $gmp; |
|
73 | + if (!isset($gmp)) { |
|
74 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
75 | + } |
|
76 | + return $gmp; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Get the minimum integer value. |
|
81 | - * |
|
82 | - * @return \GMP |
|
83 | - */ |
|
84 | - private function _intMinGmp(): \GMP |
|
85 | - { |
|
86 | - static $gmp; |
|
87 | - if (!isset($gmp)) { |
|
88 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
89 | - } |
|
90 | - return $gmp; |
|
91 | - } |
|
79 | + /** |
|
80 | + * Get the minimum integer value. |
|
81 | + * |
|
82 | + * @return \GMP |
|
83 | + */ |
|
84 | + private function _intMinGmp(): \GMP |
|
85 | + { |
|
86 | + static $gmp; |
|
87 | + if (!isset($gmp)) { |
|
88 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
89 | + } |
|
90 | + return $gmp; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Get the number as a GMP object. |
|
95 | - * |
|
96 | - * @return \GMP |
|
97 | - */ |
|
98 | - public function gmpObj(): \GMP |
|
99 | - { |
|
100 | - return gmp_init($this->_num, 10); |
|
101 | - } |
|
93 | + /** |
|
94 | + * Get the number as a GMP object. |
|
95 | + * |
|
96 | + * @return \GMP |
|
97 | + */ |
|
98 | + public function gmpObj(): \GMP |
|
99 | + { |
|
100 | + return gmp_init($this->_num, 10); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function __toString() |
|
108 | - { |
|
109 | - return $this->base10(); |
|
110 | - } |
|
103 | + /** |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function __toString() |
|
108 | + { |
|
109 | + return $this->base10(); |
|
110 | + } |
|
111 | 111 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Util; |
6 | 6 |
@@ -11,141 +11,141 @@ |
||
11 | 11 | */ |
12 | 12 | class Flags |
13 | 13 | { |
14 | - /** |
|
15 | - * Flag octets. |
|
16 | - * |
|
17 | - * @var string $_flags |
|
18 | - */ |
|
19 | - protected $_flags; |
|
14 | + /** |
|
15 | + * Flag octets. |
|
16 | + * |
|
17 | + * @var string $_flags |
|
18 | + */ |
|
19 | + protected $_flags; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Number of flags. |
|
23 | - * |
|
24 | - * @var int $_width |
|
25 | - */ |
|
26 | - protected $_width; |
|
21 | + /** |
|
22 | + * Number of flags. |
|
23 | + * |
|
24 | + * @var int $_width |
|
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 number of |
|
33 | - * bits in $flags, zeroes are prepended to flag field. |
|
34 | - */ |
|
35 | - public function __construct($flags, int $width) |
|
36 | - { |
|
37 | - if (!$width) { |
|
38 | - $this->_flags = ""; |
|
39 | - } else { |
|
40 | - // calculate number of unused bits in last octet |
|
41 | - $last_octet_bits = $width % 8; |
|
42 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
43 | - $num = gmp_init($flags); |
|
44 | - // mask bits outside bitfield width |
|
45 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
46 | - $num &= $mask; |
|
47 | - // shift towards MSB if needed |
|
48 | - $data = gmp_export($num << $unused_bits, 1, |
|
49 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
50 | - $octets = unpack("C*", $data); |
|
51 | - $bits = count($octets) * 8; |
|
52 | - // pad with zeroes |
|
53 | - while ($bits < $width) { |
|
54 | - array_unshift($octets, 0); |
|
55 | - $bits += 8; |
|
56 | - } |
|
57 | - $this->_flags = pack("C*", ...$octets); |
|
58 | - } |
|
59 | - $this->_width = $width; |
|
60 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param int|string $flags Flags |
|
32 | + * @param int $width The number of flags. If width is larger than number of |
|
33 | + * bits in $flags, zeroes are prepended to flag field. |
|
34 | + */ |
|
35 | + public function __construct($flags, int $width) |
|
36 | + { |
|
37 | + if (!$width) { |
|
38 | + $this->_flags = ""; |
|
39 | + } else { |
|
40 | + // calculate number of unused bits in last octet |
|
41 | + $last_octet_bits = $width % 8; |
|
42 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
43 | + $num = gmp_init($flags); |
|
44 | + // mask bits outside bitfield width |
|
45 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
46 | + $num &= $mask; |
|
47 | + // shift towards MSB if needed |
|
48 | + $data = gmp_export($num << $unused_bits, 1, |
|
49 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
50 | + $octets = unpack("C*", $data); |
|
51 | + $bits = count($octets) * 8; |
|
52 | + // pad with zeroes |
|
53 | + while ($bits < $width) { |
|
54 | + array_unshift($octets, 0); |
|
55 | + $bits += 8; |
|
56 | + } |
|
57 | + $this->_flags = pack("C*", ...$octets); |
|
58 | + } |
|
59 | + $this->_width = $width; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Initialize from BitString. |
|
64 | - * |
|
65 | - * @param BitString $bs |
|
66 | - * @param int $width |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public static function fromBitString(BitString $bs, int $width): self |
|
70 | - { |
|
71 | - $num_bits = $bs->numBits(); |
|
72 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
73 | - $num >>= $bs->unusedBits(); |
|
74 | - if ($num_bits < $width) { |
|
75 | - $num <<= ($width - $num_bits); |
|
76 | - } |
|
77 | - return new self(gmp_strval($num, 10), $width); |
|
78 | - } |
|
62 | + /** |
|
63 | + * Initialize from BitString. |
|
64 | + * |
|
65 | + * @param BitString $bs |
|
66 | + * @param int $width |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public static function fromBitString(BitString $bs, int $width): self |
|
70 | + { |
|
71 | + $num_bits = $bs->numBits(); |
|
72 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
73 | + $num >>= $bs->unusedBits(); |
|
74 | + if ($num_bits < $width) { |
|
75 | + $num <<= ($width - $num_bits); |
|
76 | + } |
|
77 | + return new self(gmp_strval($num, 10), $width); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Check whether a bit at given index is set. |
|
82 | - * Index 0 is the leftmost bit. |
|
83 | - * |
|
84 | - * @param int $idx |
|
85 | - * @throws \OutOfBoundsException |
|
86 | - * @return bool |
|
87 | - */ |
|
88 | - public function test(int $idx): bool |
|
89 | - { |
|
90 | - if ($idx >= $this->_width) { |
|
91 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
92 | - } |
|
93 | - // octet index |
|
94 | - $oi = (int) floor($idx / 8); |
|
95 | - $byte = $this->_flags[$oi]; |
|
96 | - // bit index |
|
97 | - $bi = $idx % 8; |
|
98 | - // index 0 is the most significant bit in byte |
|
99 | - $mask = 0x01 << (7 - $bi); |
|
100 | - return (ord($byte) & $mask) > 0; |
|
101 | - } |
|
80 | + /** |
|
81 | + * Check whether a bit at given index is set. |
|
82 | + * Index 0 is the leftmost bit. |
|
83 | + * |
|
84 | + * @param int $idx |
|
85 | + * @throws \OutOfBoundsException |
|
86 | + * @return bool |
|
87 | + */ |
|
88 | + public function test(int $idx): bool |
|
89 | + { |
|
90 | + if ($idx >= $this->_width) { |
|
91 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
92 | + } |
|
93 | + // octet index |
|
94 | + $oi = (int) floor($idx / 8); |
|
95 | + $byte = $this->_flags[$oi]; |
|
96 | + // bit index |
|
97 | + $bi = $idx % 8; |
|
98 | + // index 0 is the most significant bit in byte |
|
99 | + $mask = 0x01 << (7 - $bi); |
|
100 | + return (ord($byte) & $mask) > 0; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get flags as an octet string. |
|
105 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
106 | - * |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function string(): string |
|
110 | - { |
|
111 | - return $this->_flags; |
|
112 | - } |
|
103 | + /** |
|
104 | + * Get flags as an octet string. |
|
105 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
106 | + * |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function string(): string |
|
110 | + { |
|
111 | + return $this->_flags; |
|
112 | + } |
|
113 | 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 = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | - $last_octet_bits = $this->_width % 8; |
|
123 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
124 | - $num >>= $unused_bits; |
|
125 | - return gmp_strval($num, 10); |
|
126 | - } |
|
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 = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | + $last_octet_bits = $this->_width % 8; |
|
123 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
124 | + $num >>= $unused_bits; |
|
125 | + return gmp_strval($num, 10); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get flags as an integer. |
|
130 | - * |
|
131 | - * @return int |
|
132 | - */ |
|
133 | - public function intNumber(): int |
|
134 | - { |
|
135 | - $num = new BigInt($this->number()); |
|
136 | - return $num->intVal(); |
|
137 | - } |
|
128 | + /** |
|
129 | + * Get flags as an integer. |
|
130 | + * |
|
131 | + * @return int |
|
132 | + */ |
|
133 | + public function intNumber(): int |
|
134 | + { |
|
135 | + $num = new BigInt($this->number()); |
|
136 | + return $num->intVal(); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Get flags as a BitString. |
|
141 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
142 | - * |
|
143 | - * @return BitString |
|
144 | - */ |
|
145 | - public function bitString(): BitString |
|
146 | - { |
|
147 | - $last_octet_bits = $this->_width % 8; |
|
148 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
149 | - return new BitString($this->_flags, $unused_bits); |
|
150 | - } |
|
139 | + /** |
|
140 | + * Get flags as a BitString. |
|
141 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
142 | + * |
|
143 | + * @return BitString |
|
144 | + */ |
|
145 | + public function bitString(): BitString |
|
146 | + { |
|
147 | + $last_octet_bits = $this->_width % 8; |
|
148 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
149 | + return new BitString($this->_flags, $unused_bits); |
|
150 | + } |
|
151 | 151 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Util; |
6 | 6 |
@@ -14,82 +14,82 @@ |
||
14 | 14 | */ |
15 | 15 | class DERData extends Element |
16 | 16 | { |
17 | - /** |
|
18 | - * DER encoded data. |
|
19 | - * |
|
20 | - * @var string $_der |
|
21 | - */ |
|
22 | - protected $_der; |
|
17 | + /** |
|
18 | + * DER encoded data. |
|
19 | + * |
|
20 | + * @var string $_der |
|
21 | + */ |
|
22 | + protected $_der; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Identifier of the underlying type. |
|
26 | - * |
|
27 | - * @var Identifier $_identifier |
|
28 | - */ |
|
29 | - protected $_identifier; |
|
24 | + /** |
|
25 | + * Identifier of the underlying type. |
|
26 | + * |
|
27 | + * @var Identifier $_identifier |
|
28 | + */ |
|
29 | + protected $_identifier; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Offset to the content in DER data. |
|
33 | - * |
|
34 | - * @var int $_contentOffset |
|
35 | - */ |
|
36 | - protected $_contentOffset = 0; |
|
31 | + /** |
|
32 | + * Offset to the content in DER data. |
|
33 | + * |
|
34 | + * @var int $_contentOffset |
|
35 | + */ |
|
36 | + protected $_contentOffset = 0; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param string $data DER encoded data |
|
42 | - * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
43 | - */ |
|
44 | - public function __construct(string $data) |
|
45 | - { |
|
46 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
47 | - Length::expectFromDER($data, $this->_contentOffset); |
|
48 | - $this->_der = $data; |
|
49 | - $this->_typeTag = $this->_identifier->intTag(); |
|
50 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param string $data DER encoded data |
|
42 | + * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
43 | + */ |
|
44 | + public function __construct(string $data) |
|
45 | + { |
|
46 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
47 | + Length::expectFromDER($data, $this->_contentOffset); |
|
48 | + $this->_der = $data; |
|
49 | + $this->_typeTag = $this->_identifier->intTag(); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * |
|
54 | - * @see \ASN1\Element::typeClass() |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function typeClass(): int |
|
58 | - { |
|
59 | - return $this->_identifier->typeClass(); |
|
60 | - } |
|
52 | + /** |
|
53 | + * |
|
54 | + * @see \ASN1\Element::typeClass() |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function typeClass(): int |
|
58 | + { |
|
59 | + return $this->_identifier->typeClass(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * |
|
64 | - * @see \ASN1\Element::isConstructed() |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function isConstructed(): bool |
|
68 | - { |
|
69 | - return $this->_identifier->isConstructed(); |
|
70 | - } |
|
62 | + /** |
|
63 | + * |
|
64 | + * @see \ASN1\Element::isConstructed() |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function isConstructed(): bool |
|
68 | + { |
|
69 | + return $this->_identifier->isConstructed(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * |
|
74 | - * @see \ASN1\Element::_encodedContentDER() |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - protected function _encodedContentDER(): string |
|
78 | - { |
|
79 | - // if there's no content payload |
|
80 | - if (strlen($this->_der) == $this->_contentOffset) { |
|
81 | - return ""; |
|
82 | - } |
|
83 | - return substr($this->_der, $this->_contentOffset); |
|
84 | - } |
|
72 | + /** |
|
73 | + * |
|
74 | + * @see \ASN1\Element::_encodedContentDER() |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + protected function _encodedContentDER(): string |
|
78 | + { |
|
79 | + // if there's no content payload |
|
80 | + if (strlen($this->_der) == $this->_contentOffset) { |
|
81 | + return ""; |
|
82 | + } |
|
83 | + return substr($this->_der, $this->_contentOffset); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * |
|
88 | - * @see \ASN1\Element::toDER() |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function toDER(): string |
|
92 | - { |
|
93 | - return $this->_der; |
|
94 | - } |
|
86 | + /** |
|
87 | + * |
|
88 | + * @see \ASN1\Element::toDER() |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function toDER(): string |
|
92 | + { |
|
93 | + return $this->_der; |
|
94 | + } |
|
95 | 95 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1; |
6 | 6 |
@@ -17,85 +17,85 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class TaggedType extends Element |
19 | 19 | { |
20 | - /** |
|
21 | - * |
|
22 | - * {@inheritdoc} |
|
23 | - */ |
|
24 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | - int &$offset): ElementBase |
|
26 | - { |
|
27 | - $idx = $offset; |
|
28 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
30 | - $offset = $idx + $length; |
|
31 | - return $type; |
|
32 | - } |
|
20 | + /** |
|
21 | + * |
|
22 | + * {@inheritdoc} |
|
23 | + */ |
|
24 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
25 | + int &$offset): ElementBase |
|
26 | + { |
|
27 | + $idx = $offset; |
|
28 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
29 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
30 | + $offset = $idx + $length; |
|
31 | + return $type; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Check whether element supports explicit tagging. |
|
36 | - * |
|
37 | - * @param int|null $expectedTag Optional outer tag expectation |
|
38 | - * @throws \UnexpectedValueException If expectation fails |
|
39 | - * @return ExplicitTagging |
|
40 | - */ |
|
41 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
42 | - { |
|
43 | - $el = $this; |
|
44 | - if (!$el instanceof ExplicitTagging) { |
|
45 | - throw new \UnexpectedValueException( |
|
46 | - "Element doesn't implement explicit tagging."); |
|
47 | - } |
|
48 | - if (isset($expectedTag)) { |
|
49 | - $el->expectTagged($expectedTag); |
|
50 | - } |
|
51 | - return $el; |
|
52 | - } |
|
34 | + /** |
|
35 | + * Check whether element supports explicit tagging. |
|
36 | + * |
|
37 | + * @param int|null $expectedTag Optional outer tag expectation |
|
38 | + * @throws \UnexpectedValueException If expectation fails |
|
39 | + * @return ExplicitTagging |
|
40 | + */ |
|
41 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
42 | + { |
|
43 | + $el = $this; |
|
44 | + if (!$el instanceof ExplicitTagging) { |
|
45 | + throw new \UnexpectedValueException( |
|
46 | + "Element doesn't implement explicit tagging."); |
|
47 | + } |
|
48 | + if (isset($expectedTag)) { |
|
49 | + $el->expectTagged($expectedTag); |
|
50 | + } |
|
51 | + return $el; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get the wrapped inner element employing explicit tagging. |
|
56 | - * |
|
57 | - * @param int|null $expectedTag Optional outer tag expectation |
|
58 | - * @throws \UnexpectedValueException If expectation fails |
|
59 | - * @return UnspecifiedType |
|
60 | - */ |
|
61 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | - { |
|
63 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | - } |
|
54 | + /** |
|
55 | + * Get the wrapped inner element employing explicit tagging. |
|
56 | + * |
|
57 | + * @param int|null $expectedTag Optional outer tag expectation |
|
58 | + * @throws \UnexpectedValueException If expectation fails |
|
59 | + * @return UnspecifiedType |
|
60 | + */ |
|
61 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
62 | + { |
|
63 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Check whether element supports implicit tagging. |
|
68 | - * |
|
69 | - * @param int|null $expectedTag Optional outer tag expectation |
|
70 | - * @throws \UnexpectedValueException If expectation fails |
|
71 | - * @return ImplicitTagging |
|
72 | - */ |
|
73 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | - { |
|
75 | - $el = $this; |
|
76 | - if (!$el instanceof ImplicitTagging) { |
|
77 | - throw new \UnexpectedValueException( |
|
78 | - "Element doesn't implement implicit tagging."); |
|
79 | - } |
|
80 | - if (isset($expectedTag)) { |
|
81 | - $el->expectTagged($expectedTag); |
|
82 | - } |
|
83 | - return $el; |
|
84 | - } |
|
66 | + /** |
|
67 | + * Check whether element supports implicit tagging. |
|
68 | + * |
|
69 | + * @param int|null $expectedTag Optional outer tag expectation |
|
70 | + * @throws \UnexpectedValueException If expectation fails |
|
71 | + * @return ImplicitTagging |
|
72 | + */ |
|
73 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
74 | + { |
|
75 | + $el = $this; |
|
76 | + if (!$el instanceof ImplicitTagging) { |
|
77 | + throw new \UnexpectedValueException( |
|
78 | + "Element doesn't implement implicit tagging."); |
|
79 | + } |
|
80 | + if (isset($expectedTag)) { |
|
81 | + $el->expectTagged($expectedTag); |
|
82 | + } |
|
83 | + return $el; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get the wrapped inner element employing implicit tagging. |
|
88 | - * |
|
89 | - * @param int $tag Type tag of the inner element |
|
90 | - * @param int|null $expectedTag Optional outer tag expectation |
|
91 | - * @param int $expectedClass Optional inner type class expectation |
|
92 | - * @throws \UnexpectedValueException If expectation fails |
|
93 | - * @return UnspecifiedType |
|
94 | - */ |
|
95 | - public function asImplicit(int $tag, $expectedTag = null, |
|
96 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | - { |
|
98 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | - $expectedClass); |
|
100 | - } |
|
86 | + /** |
|
87 | + * Get the wrapped inner element employing implicit tagging. |
|
88 | + * |
|
89 | + * @param int $tag Type tag of the inner element |
|
90 | + * @param int|null $expectedTag Optional outer tag expectation |
|
91 | + * @param int $expectedClass Optional inner type class expectation |
|
92 | + * @throws \UnexpectedValueException If expectation fails |
|
93 | + * @return UnspecifiedType |
|
94 | + */ |
|
95 | + public function asImplicit(int $tag, $expectedTag = null, |
|
96 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
97 | + { |
|
98 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
99 | + $expectedClass); |
|
100 | + } |
|
101 | 101 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type; |
6 | 6 |
@@ -13,13 +13,13 @@ |
||
13 | 13 | */ |
14 | 14 | interface ImplicitTagging extends ElementBase |
15 | 15 | { |
16 | - /** |
|
17 | - * Get implicitly tagged wrapped element. |
|
18 | - * |
|
19 | - * @param int $tag Tag of the element |
|
20 | - * @param int $class Expected type class of the element |
|
21 | - * @throws \UnexpectedValueException If expectation fails |
|
22 | - * @return \ASN1\Type\UnspecifiedType |
|
23 | - */ |
|
24 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
16 | + /** |
|
17 | + * Get implicitly tagged wrapped element. |
|
18 | + * |
|
19 | + * @param int $tag Tag of the element |
|
20 | + * @param int $class Expected type class of the element |
|
21 | + * @throws \UnexpectedValueException If expectation fails |
|
22 | + * @return \ASN1\Type\UnspecifiedType |
|
23 | + */ |
|
24 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
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 ASN1\Type\Tagged; |
6 | 6 |
@@ -20,106 +20,106 @@ |
||
20 | 20 | * May be encoded back to complete DER encoding. |
21 | 21 | */ |
22 | 22 | class DERTaggedType extends TaggedType implements |
23 | - ExplicitTagging, |
|
24 | - ImplicitTagging |
|
23 | + ExplicitTagging, |
|
24 | + ImplicitTagging |
|
25 | 25 | { |
26 | - /** |
|
27 | - * Identifier. |
|
28 | - * |
|
29 | - * @var Identifier |
|
30 | - */ |
|
31 | - private $_identifier; |
|
26 | + /** |
|
27 | + * Identifier. |
|
28 | + * |
|
29 | + * @var Identifier |
|
30 | + */ |
|
31 | + private $_identifier; |
|
32 | 32 | |
33 | - /** |
|
34 | - * DER data. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $_data; |
|
33 | + /** |
|
34 | + * DER data. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $_data; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Offset to data. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_offset; |
|
40 | + /** |
|
41 | + * Offset to data. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_offset; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Constructor. |
|
49 | - * |
|
50 | - * @param Identifier $identifier |
|
51 | - * @param string $data |
|
52 | - * @param int $offset Offset to next byte after identifier |
|
53 | - */ |
|
54 | - public function __construct(Identifier $identifier, string $data, int $offset) |
|
55 | - { |
|
56 | - $this->_identifier = $identifier; |
|
57 | - $this->_data = $data; |
|
58 | - $this->_offset = $offset; |
|
59 | - $this->_typeTag = $identifier->intTag(); |
|
60 | - } |
|
47 | + /** |
|
48 | + * Constructor. |
|
49 | + * |
|
50 | + * @param Identifier $identifier |
|
51 | + * @param string $data |
|
52 | + * @param int $offset Offset to next byte after identifier |
|
53 | + */ |
|
54 | + public function __construct(Identifier $identifier, string $data, int $offset) |
|
55 | + { |
|
56 | + $this->_identifier = $identifier; |
|
57 | + $this->_data = $data; |
|
58 | + $this->_offset = $offset; |
|
59 | + $this->_typeTag = $identifier->intTag(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * |
|
64 | - * @see \ASN1\Element::typeClass() |
|
65 | - * @return int |
|
66 | - */ |
|
67 | - public function typeClass(): int |
|
68 | - { |
|
69 | - return $this->_identifier->typeClass(); |
|
70 | - } |
|
62 | + /** |
|
63 | + * |
|
64 | + * @see \ASN1\Element::typeClass() |
|
65 | + * @return int |
|
66 | + */ |
|
67 | + public function typeClass(): int |
|
68 | + { |
|
69 | + return $this->_identifier->typeClass(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * |
|
74 | - * @see \ASN1\Element::isConstructed() |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function isConstructed(): bool |
|
78 | - { |
|
79 | - return $this->_identifier->isConstructed(); |
|
80 | - } |
|
72 | + /** |
|
73 | + * |
|
74 | + * @see \ASN1\Element::isConstructed() |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function isConstructed(): bool |
|
78 | + { |
|
79 | + return $this->_identifier->isConstructed(); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * |
|
84 | - * @see \ASN1\Element::_encodedContentDER() |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected function _encodedContentDER(): string |
|
88 | - { |
|
89 | - $idx = $this->_offset; |
|
90 | - $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
91 | - return substr($this->_data, $idx, $length); |
|
92 | - } |
|
82 | + /** |
|
83 | + * |
|
84 | + * @see \ASN1\Element::_encodedContentDER() |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected function _encodedContentDER(): string |
|
88 | + { |
|
89 | + $idx = $this->_offset; |
|
90 | + $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
91 | + return substr($this->_data, $idx, $length); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * |
|
96 | - * {@inheritdoc} |
|
97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
98 | - * @return UnspecifiedType |
|
99 | - */ |
|
100 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
101 | - { |
|
102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
103 | - $cls = self::_determineImplClass($identifier); |
|
104 | - $idx = $this->_offset; |
|
105 | - /** @var \ASN1\Feature\ElementBase $element */ |
|
106 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
107 | - return $element->asUnspecified(); |
|
108 | - } |
|
94 | + /** |
|
95 | + * |
|
96 | + * {@inheritdoc} |
|
97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
98 | + * @return UnspecifiedType |
|
99 | + */ |
|
100 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
101 | + { |
|
102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
103 | + $cls = self::_determineImplClass($identifier); |
|
104 | + $idx = $this->_offset; |
|
105 | + /** @var \ASN1\Feature\ElementBase $element */ |
|
106 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
107 | + return $element->asUnspecified(); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * |
|
112 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
113 | - * @return UnspecifiedType |
|
114 | - */ |
|
115 | - public function explicit($expectedTag = null): UnspecifiedType |
|
116 | - { |
|
117 | - $idx = $this->_offset; |
|
118 | - Length::expectFromDER($this->_data, $idx); |
|
119 | - $element = Element::fromDER($this->_data, $idx); |
|
120 | - if (isset($expectedTag)) { |
|
121 | - $element->expectType($expectedTag); |
|
122 | - } |
|
123 | - return $element->asUnspecified(); |
|
124 | - } |
|
110 | + /** |
|
111 | + * |
|
112 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
113 | + * @return UnspecifiedType |
|
114 | + */ |
|
115 | + public function explicit($expectedTag = null): UnspecifiedType |
|
116 | + { |
|
117 | + $idx = $this->_offset; |
|
118 | + Length::expectFromDER($this->_data, $idx); |
|
119 | + $element = Element::fromDER($this->_data, $idx); |
|
120 | + if (isset($expectedTag)) { |
|
121 | + $element->expectType($expectedTag); |
|
122 | + } |
|
123 | + return $element->asUnspecified(); |
|
124 | + } |
|
125 | 125 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |
@@ -17,58 +17,58 @@ |
||
17 | 17 | */ |
18 | 18 | class ImplicitlyTaggedType extends TaggedTypeWrap implements ImplicitTagging |
19 | 19 | { |
20 | - /** |
|
21 | - * Constructor. |
|
22 | - * |
|
23 | - * @param int $tag Tag number |
|
24 | - * @param Element $element Wrapped element |
|
25 | - * @param int $class Type class |
|
26 | - */ |
|
27 | - public function __construct(int $tag, Element $element, |
|
28 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | - { |
|
30 | - $this->_typeTag = $tag; |
|
31 | - $this->_element = $element; |
|
32 | - $this->_class = $class; |
|
33 | - } |
|
20 | + /** |
|
21 | + * Constructor. |
|
22 | + * |
|
23 | + * @param int $tag Tag number |
|
24 | + * @param Element $element Wrapped element |
|
25 | + * @param int $class Type class |
|
26 | + */ |
|
27 | + public function __construct(int $tag, Element $element, |
|
28 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | + { |
|
30 | + $this->_typeTag = $tag; |
|
31 | + $this->_element = $element; |
|
32 | + $this->_class = $class; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * |
|
37 | - * @see \ASN1\Element::isConstructed() |
|
38 | - * @return bool |
|
39 | - */ |
|
40 | - public function isConstructed(): bool |
|
41 | - { |
|
42 | - // depends on the underlying type |
|
43 | - return $this->_element->isConstructed(); |
|
44 | - } |
|
35 | + /** |
|
36 | + * |
|
37 | + * @see \ASN1\Element::isConstructed() |
|
38 | + * @return bool |
|
39 | + */ |
|
40 | + public function isConstructed(): bool |
|
41 | + { |
|
42 | + // depends on the underlying type |
|
43 | + return $this->_element->isConstructed(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * |
|
48 | - * @see \ASN1\Element::_encodedContentDER() |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - protected function _encodedContentDER(): string |
|
52 | - { |
|
53 | - // get only the content of the wrapped element. |
|
54 | - return $this->_element->_encodedContentDER(); |
|
55 | - } |
|
46 | + /** |
|
47 | + * |
|
48 | + * @see \ASN1\Element::_encodedContentDER() |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + protected function _encodedContentDER(): string |
|
52 | + { |
|
53 | + // get only the content of the wrapped element. |
|
54 | + return $this->_element->_encodedContentDER(); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * |
|
59 | - * {@inheritdoc} |
|
60 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
61 | - * @return UnspecifiedType |
|
62 | - */ |
|
63 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
64 | - { |
|
65 | - $this->_element->expectType($tag); |
|
66 | - if ($this->_element->typeClass() != $class) { |
|
67 | - throw new \UnexpectedValueException( |
|
68 | - sprintf("Type class %s expected, got %s.", |
|
69 | - Identifier::classToName($class), |
|
70 | - Identifier::classToName($this->_element->typeClass()))); |
|
71 | - } |
|
72 | - return $this->_element->asUnspecified(); |
|
73 | - } |
|
57 | + /** |
|
58 | + * |
|
59 | + * {@inheritdoc} |
|
60 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
61 | + * @return UnspecifiedType |
|
62 | + */ |
|
63 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
64 | + { |
|
65 | + $this->_element->expectType($tag); |
|
66 | + if ($this->_element->typeClass() != $class) { |
|
67 | + throw new \UnexpectedValueException( |
|
68 | + sprintf("Type class %s expected, got %s.", |
|
69 | + Identifier::classToName($class), |
|
70 | + Identifier::classToName($this->_element->typeClass()))); |
|
71 | + } |
|
72 | + return $this->_element->asUnspecified(); |
|
73 | + } |
|
74 | 74 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |