@@ -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\Primitive; |
| 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\Primitive; |
| 6 | 6 | |
@@ -17,61 +17,61 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Boolean extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Value. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - private $_bool; |
|
| 23 | + /** |
|
| 24 | + * Value. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + private $_bool; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param bool $bool |
|
| 34 | - */ |
|
| 35 | - public function __construct(bool $bool) |
|
| 36 | - { |
|
| 37 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
| 38 | - $this->_bool = $bool; |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param bool $bool |
|
| 34 | + */ |
|
| 35 | + public function __construct(bool $bool) |
|
| 36 | + { |
|
| 37 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
| 38 | + $this->_bool = $bool; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the value. |
|
| 43 | - * |
|
| 44 | - * @return bool |
|
| 45 | - */ |
|
| 46 | - public function value(): bool |
|
| 47 | - { |
|
| 48 | - return $this->_bool; |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Get the value. |
|
| 43 | + * |
|
| 44 | + * @return bool |
|
| 45 | + */ |
|
| 46 | + public function value(): bool |
|
| 47 | + { |
|
| 48 | + return $this->_bool; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - */ |
|
| 54 | - protected function _encodedContentDER(): string |
|
| 55 | - { |
|
| 56 | - return $this->_bool ? chr(0xff) : chr(0); |
|
| 57 | - } |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + */ |
|
| 54 | + protected function _encodedContentDER(): string |
|
| 55 | + { |
|
| 56 | + return $this->_bool ? chr(0xff) : chr(0); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * {@inheritdoc} |
|
| 61 | - */ |
|
| 62 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 63 | - string $data, int &$offset): ElementBase |
|
| 64 | - { |
|
| 65 | - $idx = $offset; |
|
| 66 | - Length::expectFromDER($data, $idx, 1); |
|
| 67 | - $byte = ord($data[$idx++]); |
|
| 68 | - if (0 !== $byte) { |
|
| 69 | - if (0xff !== $byte) { |
|
| 70 | - throw new DecodeException( |
|
| 71 | - 'DER encoded boolean true must have all bits set to 1.'); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - $offset = $idx; |
|
| 75 | - return new self(0 !== $byte); |
|
| 76 | - } |
|
| 59 | + /** |
|
| 60 | + * {@inheritdoc} |
|
| 61 | + */ |
|
| 62 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 63 | + string $data, int &$offset): ElementBase |
|
| 64 | + { |
|
| 65 | + $idx = $offset; |
|
| 66 | + Length::expectFromDER($data, $idx, 1); |
|
| 67 | + $byte = ord($data[$idx++]); |
|
| 68 | + if (0 !== $byte) { |
|
| 69 | + if (0xff !== $byte) { |
|
| 70 | + throw new DecodeException( |
|
| 71 | + 'DER encoded boolean true must have all bits set to 1.'); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + $offset = $idx; |
|
| 75 | + return new self(0 !== $byte); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -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\Primitive; |
| 6 | 6 | |
@@ -17,110 +17,110 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class GeneralizedTime 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 = '#^' . |
|
| 31 | - '(\d\d\d\d)' . // YYYY |
|
| 32 | - '(\d\d)' . // MM |
|
| 33 | - '(\d\d)' . // DD |
|
| 34 | - '(\d\d)' . // hh |
|
| 35 | - '(\d\d)' . // mm |
|
| 36 | - '(\d\d)' . // ss |
|
| 37 | - '(?:\.(\d+))?' . // frac |
|
| 38 | - 'Z' . // TZ |
|
| 39 | - '$#'; |
|
| 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 = '#^' . |
|
| 31 | + '(\d\d\d\d)' . // YYYY |
|
| 32 | + '(\d\d)' . // MM |
|
| 33 | + '(\d\d)' . // DD |
|
| 34 | + '(\d\d)' . // hh |
|
| 35 | + '(\d\d)' . // mm |
|
| 36 | + '(\d\d)' . // ss |
|
| 37 | + '(?:\.(\d+))?' . // frac |
|
| 38 | + 'Z' . // TZ |
|
| 39 | + '$#'; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Cached formatted date. |
|
| 43 | - * |
|
| 44 | - * @var null|string |
|
| 45 | - */ |
|
| 46 | - private $_formatted; |
|
| 41 | + /** |
|
| 42 | + * Cached formatted date. |
|
| 43 | + * |
|
| 44 | + * @var null|string |
|
| 45 | + */ |
|
| 46 | + private $_formatted; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Constructor. |
|
| 50 | - * |
|
| 51 | - * @param \DateTimeImmutable $dt |
|
| 52 | - */ |
|
| 53 | - public function __construct(\DateTimeImmutable $dt) |
|
| 54 | - { |
|
| 55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | - parent::__construct($dt); |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * Constructor. |
|
| 50 | + * |
|
| 51 | + * @param \DateTimeImmutable $dt |
|
| 52 | + */ |
|
| 53 | + public function __construct(\DateTimeImmutable $dt) |
|
| 54 | + { |
|
| 55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | + parent::__construct($dt); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Clear cached variables on clone. |
|
| 61 | - */ |
|
| 62 | - public function __clone() |
|
| 63 | - { |
|
| 64 | - $this->_formatted = null; |
|
| 65 | - } |
|
| 59 | + /** |
|
| 60 | + * Clear cached variables on clone. |
|
| 61 | + */ |
|
| 62 | + public function __clone() |
|
| 63 | + { |
|
| 64 | + $this->_formatted = null; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - */ |
|
| 70 | - protected function _encodedContentDER(): string |
|
| 71 | - { |
|
| 72 | - if (!isset($this->_formatted)) { |
|
| 73 | - $dt = $this->_dateTime->setTimezone( |
|
| 74 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | - $this->_formatted = $dt->format('YmdHis'); |
|
| 76 | - // if fractions were used |
|
| 77 | - $frac = $dt->format('u'); |
|
| 78 | - if (0 !== intval($frac)) { |
|
| 79 | - $frac = rtrim($frac, '0'); |
|
| 80 | - $this->_formatted .= ".{$frac}"; |
|
| 81 | - } |
|
| 82 | - // timezone |
|
| 83 | - $this->_formatted .= 'Z'; |
|
| 84 | - } |
|
| 85 | - return $this->_formatted; |
|
| 86 | - } |
|
| 67 | + /** |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + */ |
|
| 70 | + protected function _encodedContentDER(): string |
|
| 71 | + { |
|
| 72 | + if (!isset($this->_formatted)) { |
|
| 73 | + $dt = $this->_dateTime->setTimezone( |
|
| 74 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 75 | + $this->_formatted = $dt->format('YmdHis'); |
|
| 76 | + // if fractions were used |
|
| 77 | + $frac = $dt->format('u'); |
|
| 78 | + if (0 !== intval($frac)) { |
|
| 79 | + $frac = rtrim($frac, '0'); |
|
| 80 | + $this->_formatted .= ".{$frac}"; |
|
| 81 | + } |
|
| 82 | + // timezone |
|
| 83 | + $this->_formatted .= 'Z'; |
|
| 84 | + } |
|
| 85 | + return $this->_formatted; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * {@inheritdoc} |
|
| 90 | - */ |
|
| 91 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 92 | - string $data, int &$offset): ElementBase |
|
| 93 | - { |
|
| 94 | - $idx = $offset; |
|
| 95 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 96 | - $str = substr($data, $idx, $length); |
|
| 97 | - $idx += $length; |
|
| 98 | - /** @var string[] $match */ |
|
| 99 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 100 | - throw new DecodeException('Invalid GeneralizedTime format.'); |
|
| 101 | - } |
|
| 102 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
| 103 | - if (isset($match[7])) { |
|
| 104 | - $frac = $match[7]; |
|
| 105 | - // DER restricts trailing zeroes in fractional seconds component |
|
| 106 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
| 107 | - throw new DecodeException( |
|
| 108 | - 'Fractional seconds must omit trailing zeroes.'); |
|
| 109 | - } |
|
| 110 | - $frac = (int) $frac; |
|
| 111 | - } else { |
|
| 112 | - $frac = 0; |
|
| 113 | - } |
|
| 114 | - $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
| 115 | - self::TZ_UTC; |
|
| 116 | - $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
| 117 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 118 | - if (!$dt) { |
|
| 119 | - throw new DecodeException( |
|
| 120 | - 'Failed to decode GeneralizedTime: ' . |
|
| 121 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 122 | - } |
|
| 123 | - $offset = $idx; |
|
| 124 | - return new self($dt); |
|
| 125 | - } |
|
| 88 | + /** |
|
| 89 | + * {@inheritdoc} |
|
| 90 | + */ |
|
| 91 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 92 | + string $data, int &$offset): ElementBase |
|
| 93 | + { |
|
| 94 | + $idx = $offset; |
|
| 95 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 96 | + $str = substr($data, $idx, $length); |
|
| 97 | + $idx += $length; |
|
| 98 | + /** @var string[] $match */ |
|
| 99 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 100 | + throw new DecodeException('Invalid GeneralizedTime format.'); |
|
| 101 | + } |
|
| 102 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
| 103 | + if (isset($match[7])) { |
|
| 104 | + $frac = $match[7]; |
|
| 105 | + // DER restricts trailing zeroes in fractional seconds component |
|
| 106 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
| 107 | + throw new DecodeException( |
|
| 108 | + 'Fractional seconds must omit trailing zeroes.'); |
|
| 109 | + } |
|
| 110 | + $frac = (int) $frac; |
|
| 111 | + } else { |
|
| 112 | + $frac = 0; |
|
| 113 | + } |
|
| 114 | + $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
| 115 | + self::TZ_UTC; |
|
| 116 | + $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
| 117 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 118 | + if (!$dt) { |
|
| 119 | + throw new DecodeException( |
|
| 120 | + 'Failed to decode GeneralizedTime: ' . |
|
| 121 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 122 | + } |
|
| 123 | + $offset = $idx; |
|
| 124 | + return new self($dt); |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -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\Primitive; |
| 6 | 6 | |
@@ -14,28 +14,28 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class UniversalString extends PrimitiveString |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string |
|
| 23 | - */ |
|
| 24 | - public function __construct(string $string) |
|
| 25 | - { |
|
| 26 | - $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | - parent::__construct($string); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string |
|
| 23 | + */ |
|
| 24 | + public function __construct(string $string) |
|
| 25 | + { |
|
| 26 | + $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | + parent::__construct($string); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - protected function _validateString(string $string): bool |
|
| 34 | - { |
|
| 35 | - // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 36 | - if (0 !== strlen($string) % 4) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 30 | + /** |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + protected function _validateString(string $string): bool |
|
| 34 | + { |
|
| 35 | + // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 36 | + if (0 !== strlen($string) % 4) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -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\Primitive; |
| 6 | 6 | |
@@ -17,194 +17,194 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class BitString extends StringType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Number of unused bits in the last octet. |
|
| 25 | - * |
|
| 26 | - * @var int |
|
| 27 | - */ |
|
| 28 | - protected $_unusedBits; |
|
| 23 | + /** |
|
| 24 | + * Number of unused bits in the last octet. |
|
| 25 | + * |
|
| 26 | + * @var int |
|
| 27 | + */ |
|
| 28 | + protected $_unusedBits; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $string Content octets |
|
| 34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | - */ |
|
| 36 | - public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | - { |
|
| 38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | - parent::__construct($string); |
|
| 40 | - $this->_unusedBits = $unused_bits; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $string Content octets |
|
| 34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | + */ |
|
| 36 | + public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | + { |
|
| 38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | + parent::__construct($string); |
|
| 40 | + $this->_unusedBits = $unused_bits; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the number of bits in the string. |
|
| 45 | - * |
|
| 46 | - * @return int |
|
| 47 | - */ |
|
| 48 | - public function numBits(): int |
|
| 49 | - { |
|
| 50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Get the number of bits in the string. |
|
| 45 | + * |
|
| 46 | + * @return int |
|
| 47 | + */ |
|
| 48 | + public function numBits(): int |
|
| 49 | + { |
|
| 50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Get the number of unused bits in the last octet of the string. |
|
| 55 | - * |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public function unusedBits(): int |
|
| 59 | - { |
|
| 60 | - return $this->_unusedBits; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Get the number of unused bits in the last octet of the string. |
|
| 55 | + * |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public function unusedBits(): int |
|
| 59 | + { |
|
| 60 | + return $this->_unusedBits; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Test whether bit is set. |
|
| 65 | - * |
|
| 66 | - * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
| 67 | - * |
|
| 68 | - * @return bool |
|
| 69 | - */ |
|
| 70 | - public function testBit(int $idx): bool |
|
| 71 | - { |
|
| 72 | - // octet index |
|
| 73 | - $oi = (int) floor($idx / 8); |
|
| 74 | - // if octet is outside range |
|
| 75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
| 77 | - } |
|
| 78 | - // bit index |
|
| 79 | - $bi = $idx % 8; |
|
| 80 | - // if tested bit is last octet's unused bit |
|
| 81 | - if ($oi === strlen($this->_string) - 1) { |
|
| 82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | - throw new \OutOfBoundsException( |
|
| 84 | - 'Index refers to an unused bit.'); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - $byte = $this->_string[$oi]; |
|
| 88 | - // index 0 is the most significant bit in byte |
|
| 89 | - $mask = 0x01 << (7 - $bi); |
|
| 90 | - return (ord($byte) & $mask) > 0; |
|
| 91 | - } |
|
| 63 | + /** |
|
| 64 | + * Test whether bit is set. |
|
| 65 | + * |
|
| 66 | + * @param int $idx Bit index. Most significant bit of the first octet is index 0. |
|
| 67 | + * |
|
| 68 | + * @return bool |
|
| 69 | + */ |
|
| 70 | + public function testBit(int $idx): bool |
|
| 71 | + { |
|
| 72 | + // octet index |
|
| 73 | + $oi = (int) floor($idx / 8); |
|
| 74 | + // if octet is outside range |
|
| 75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
| 77 | + } |
|
| 78 | + // bit index |
|
| 79 | + $bi = $idx % 8; |
|
| 80 | + // if tested bit is last octet's unused bit |
|
| 81 | + if ($oi === strlen($this->_string) - 1) { |
|
| 82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | + throw new \OutOfBoundsException( |
|
| 84 | + 'Index refers to an unused bit.'); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + $byte = $this->_string[$oi]; |
|
| 88 | + // index 0 is the most significant bit in byte |
|
| 89 | + $mask = 0x01 << (7 - $bi); |
|
| 90 | + return (ord($byte) & $mask) > 0; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get range of bits. |
|
| 95 | - * |
|
| 96 | - * @param int $start Index of first bit |
|
| 97 | - * @param int $length Number of bits in range |
|
| 98 | - * |
|
| 99 | - * @throws \OutOfBoundsException |
|
| 100 | - * |
|
| 101 | - * @return string Integer of $length bits |
|
| 102 | - */ |
|
| 103 | - public function range(int $start, int $length): string |
|
| 104 | - { |
|
| 105 | - if (!$length) { |
|
| 106 | - return '0'; |
|
| 107 | - } |
|
| 108 | - if ($start + $length > $this->numBits()) { |
|
| 109 | - throw new \OutOfBoundsException('Not enough bits.'); |
|
| 110 | - } |
|
| 111 | - $bits = gmp_init(0); |
|
| 112 | - $idx = $start; |
|
| 113 | - $end = $start + $length; |
|
| 114 | - while (true) { |
|
| 115 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
| 116 | - $bits |= $bit; |
|
| 117 | - if (++$idx >= $end) { |
|
| 118 | - break; |
|
| 119 | - } |
|
| 120 | - $bits <<= 1; |
|
| 121 | - } |
|
| 122 | - return gmp_strval($bits, 10); |
|
| 123 | - } |
|
| 93 | + /** |
|
| 94 | + * Get range of bits. |
|
| 95 | + * |
|
| 96 | + * @param int $start Index of first bit |
|
| 97 | + * @param int $length Number of bits in range |
|
| 98 | + * |
|
| 99 | + * @throws \OutOfBoundsException |
|
| 100 | + * |
|
| 101 | + * @return string Integer of $length bits |
|
| 102 | + */ |
|
| 103 | + public function range(int $start, int $length): string |
|
| 104 | + { |
|
| 105 | + if (!$length) { |
|
| 106 | + return '0'; |
|
| 107 | + } |
|
| 108 | + if ($start + $length > $this->numBits()) { |
|
| 109 | + throw new \OutOfBoundsException('Not enough bits.'); |
|
| 110 | + } |
|
| 111 | + $bits = gmp_init(0); |
|
| 112 | + $idx = $start; |
|
| 113 | + $end = $start + $length; |
|
| 114 | + while (true) { |
|
| 115 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
| 116 | + $bits |= $bit; |
|
| 117 | + if (++$idx >= $end) { |
|
| 118 | + break; |
|
| 119 | + } |
|
| 120 | + $bits <<= 1; |
|
| 121 | + } |
|
| 122 | + return gmp_strval($bits, 10); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Get a copy of the bit string with trailing zeroes removed. |
|
| 127 | - * |
|
| 128 | - * @return self |
|
| 129 | - */ |
|
| 130 | - public function withoutTrailingZeroes(): self |
|
| 131 | - { |
|
| 132 | - // if bit string was empty |
|
| 133 | - if (!strlen($this->_string)) { |
|
| 134 | - return new self(''); |
|
| 135 | - } |
|
| 136 | - $bits = $this->_string; |
|
| 137 | - // count number of empty trailing octets |
|
| 138 | - $unused_octets = 0; |
|
| 139 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 140 | - if ("\x0" !== $bits[$idx]) { |
|
| 141 | - break; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - // strip trailing octets |
|
| 145 | - if ($unused_octets) { |
|
| 146 | - $bits = substr($bits, 0, -$unused_octets); |
|
| 147 | - } |
|
| 148 | - // if bit string was full of zeroes |
|
| 149 | - if (!strlen($bits)) { |
|
| 150 | - return new self(''); |
|
| 151 | - } |
|
| 152 | - // count number of trailing zeroes in the last octet |
|
| 153 | - $unused_bits = 0; |
|
| 154 | - $byte = ord($bits[strlen($bits) - 1]); |
|
| 155 | - while (!($byte & 0x01)) { |
|
| 156 | - ++$unused_bits; |
|
| 157 | - $byte >>= 1; |
|
| 158 | - } |
|
| 159 | - return new self($bits, $unused_bits); |
|
| 160 | - } |
|
| 125 | + /** |
|
| 126 | + * Get a copy of the bit string with trailing zeroes removed. |
|
| 127 | + * |
|
| 128 | + * @return self |
|
| 129 | + */ |
|
| 130 | + public function withoutTrailingZeroes(): self |
|
| 131 | + { |
|
| 132 | + // if bit string was empty |
|
| 133 | + if (!strlen($this->_string)) { |
|
| 134 | + return new self(''); |
|
| 135 | + } |
|
| 136 | + $bits = $this->_string; |
|
| 137 | + // count number of empty trailing octets |
|
| 138 | + $unused_octets = 0; |
|
| 139 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 140 | + if ("\x0" !== $bits[$idx]) { |
|
| 141 | + break; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + // strip trailing octets |
|
| 145 | + if ($unused_octets) { |
|
| 146 | + $bits = substr($bits, 0, -$unused_octets); |
|
| 147 | + } |
|
| 148 | + // if bit string was full of zeroes |
|
| 149 | + if (!strlen($bits)) { |
|
| 150 | + return new self(''); |
|
| 151 | + } |
|
| 152 | + // count number of trailing zeroes in the last octet |
|
| 153 | + $unused_bits = 0; |
|
| 154 | + $byte = ord($bits[strlen($bits) - 1]); |
|
| 155 | + while (!($byte & 0x01)) { |
|
| 156 | + ++$unused_bits; |
|
| 157 | + $byte >>= 1; |
|
| 158 | + } |
|
| 159 | + return new self($bits, $unused_bits); |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * {@inheritdoc} |
|
| 164 | - */ |
|
| 165 | - protected function _encodedContentDER(): string |
|
| 166 | - { |
|
| 167 | - $der = chr($this->_unusedBits); |
|
| 168 | - $der .= $this->_string; |
|
| 169 | - if ($this->_unusedBits) { |
|
| 170 | - $octet = $der[strlen($der) - 1]; |
|
| 171 | - // set unused bits to zero |
|
| 172 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 173 | - $der[strlen($der) - 1] = $octet; |
|
| 174 | - } |
|
| 175 | - return $der; |
|
| 176 | - } |
|
| 162 | + /** |
|
| 163 | + * {@inheritdoc} |
|
| 164 | + */ |
|
| 165 | + protected function _encodedContentDER(): string |
|
| 166 | + { |
|
| 167 | + $der = chr($this->_unusedBits); |
|
| 168 | + $der .= $this->_string; |
|
| 169 | + if ($this->_unusedBits) { |
|
| 170 | + $octet = $der[strlen($der) - 1]; |
|
| 171 | + // set unused bits to zero |
|
| 172 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 173 | + $der[strlen($der) - 1] = $octet; |
|
| 174 | + } |
|
| 175 | + return $der; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * {@inheritdoc} |
|
| 180 | - */ |
|
| 181 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 182 | - string $data, int &$offset): ElementBase |
|
| 183 | - { |
|
| 184 | - $idx = $offset; |
|
| 185 | - $length = Length::expectFromDER($data, $idx); |
|
| 186 | - if ($length->intLength() < 1) { |
|
| 187 | - throw new DecodeException('Bit string length must be at least 1.'); |
|
| 188 | - } |
|
| 189 | - $unused_bits = ord($data[$idx++]); |
|
| 190 | - if ($unused_bits > 7) { |
|
| 191 | - throw new DecodeException( |
|
| 192 | - 'Unused bits in a bit string must be less than 8.'); |
|
| 193 | - } |
|
| 194 | - $str_len = $length->intLength() - 1; |
|
| 195 | - if ($str_len) { |
|
| 196 | - $str = substr($data, $idx, $str_len); |
|
| 197 | - if ($unused_bits) { |
|
| 198 | - $mask = (1 << $unused_bits) - 1; |
|
| 199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | - throw new DecodeException( |
|
| 201 | - 'DER encoded bit string must have zero padding.'); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - } else { |
|
| 205 | - $str = ''; |
|
| 206 | - } |
|
| 207 | - $offset = $idx + $str_len; |
|
| 208 | - return new self($str, $unused_bits); |
|
| 209 | - } |
|
| 178 | + /** |
|
| 179 | + * {@inheritdoc} |
|
| 180 | + */ |
|
| 181 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 182 | + string $data, int &$offset): ElementBase |
|
| 183 | + { |
|
| 184 | + $idx = $offset; |
|
| 185 | + $length = Length::expectFromDER($data, $idx); |
|
| 186 | + if ($length->intLength() < 1) { |
|
| 187 | + throw new DecodeException('Bit string length must be at least 1.'); |
|
| 188 | + } |
|
| 189 | + $unused_bits = ord($data[$idx++]); |
|
| 190 | + if ($unused_bits > 7) { |
|
| 191 | + throw new DecodeException( |
|
| 192 | + 'Unused bits in a bit string must be less than 8.'); |
|
| 193 | + } |
|
| 194 | + $str_len = $length->intLength() - 1; |
|
| 195 | + if ($str_len) { |
|
| 196 | + $str = substr($data, $idx, $str_len); |
|
| 197 | + if ($unused_bits) { |
|
| 198 | + $mask = (1 << $unused_bits) - 1; |
|
| 199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | + throw new DecodeException( |
|
| 201 | + 'DER encoded bit string must have zero padding.'); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + } else { |
|
| 205 | + $str = ''; |
|
| 206 | + } |
|
| 207 | + $offset = $idx + $str_len; |
|
| 208 | + return new self($str, $unused_bits); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -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\Primitive; |
| 6 | 6 | |
@@ -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 | - * {@inheritdoc} |
|
| 67 | - */ |
|
| 68 | - protected function _encodedContentDER(): string |
|
| 69 | - { |
|
| 70 | - $num = $this->_number->gmpObj(); |
|
| 71 | - switch (gmp_sign($num)) { |
|
| 72 | - // positive |
|
| 73 | - case 1: |
|
| 74 | - return self::_encodePositiveInteger($num); |
|
| 75 | - // negative |
|
| 76 | - case -1: |
|
| 77 | - return self::_encodeNegativeInteger($num); |
|
| 78 | - } |
|
| 79 | - // zero |
|
| 80 | - return "\0"; |
|
| 81 | - } |
|
| 65 | + /** |
|
| 66 | + * {@inheritdoc} |
|
| 67 | + */ |
|
| 68 | + protected function _encodedContentDER(): string |
|
| 69 | + { |
|
| 70 | + $num = $this->_number->gmpObj(); |
|
| 71 | + switch (gmp_sign($num)) { |
|
| 72 | + // positive |
|
| 73 | + case 1: |
|
| 74 | + return self::_encodePositiveInteger($num); |
|
| 75 | + // negative |
|
| 76 | + case -1: |
|
| 77 | + return self::_encodeNegativeInteger($num); |
|
| 78 | + } |
|
| 79 | + // zero |
|
| 80 | + return "\0"; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 87 | - string $data, int &$offset): ElementBase |
|
| 88 | - { |
|
| 89 | - $idx = $offset; |
|
| 90 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 91 | - $bytes = substr($data, $idx, $length); |
|
| 92 | - $idx += $length; |
|
| 93 | - $neg = ord($bytes[0]) & 0x80; |
|
| 94 | - // negative, apply inversion of two's complement |
|
| 95 | - if ($neg) { |
|
| 96 | - $len = strlen($bytes); |
|
| 97 | - for ($i = 0; $i < $len; ++$i) { |
|
| 98 | - $bytes[$i] = ~$bytes[$i]; |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - $num = gmp_init(bin2hex($bytes), 16); |
|
| 102 | - // negative, apply addition of two's complement |
|
| 103 | - // and produce negative result |
|
| 104 | - if ($neg) { |
|
| 105 | - $num = gmp_neg($num + 1); |
|
| 106 | - } |
|
| 107 | - $offset = $idx; |
|
| 108 | - // late static binding since enumerated extends integer type |
|
| 109 | - return new static(gmp_strval($num, 10)); |
|
| 110 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 87 | + string $data, int &$offset): ElementBase |
|
| 88 | + { |
|
| 89 | + $idx = $offset; |
|
| 90 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 91 | + $bytes = substr($data, $idx, $length); |
|
| 92 | + $idx += $length; |
|
| 93 | + $neg = ord($bytes[0]) & 0x80; |
|
| 94 | + // negative, apply inversion of two's complement |
|
| 95 | + if ($neg) { |
|
| 96 | + $len = strlen($bytes); |
|
| 97 | + for ($i = 0; $i < $len; ++$i) { |
|
| 98 | + $bytes[$i] = ~$bytes[$i]; |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + $num = gmp_init(bin2hex($bytes), 16); |
|
| 102 | + // negative, apply addition of two's complement |
|
| 103 | + // and produce negative result |
|
| 104 | + if ($neg) { |
|
| 105 | + $num = gmp_neg($num + 1); |
|
| 106 | + } |
|
| 107 | + $offset = $idx; |
|
| 108 | + // late static binding since enumerated extends integer type |
|
| 109 | + return new static(gmp_strval($num, 10)); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * Encode positive integer to DER content. |
|
| 114 | - * |
|
| 115 | - * @param \GMP $num |
|
| 116 | - * |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - private static function _encodePositiveInteger(\GMP $num): string |
|
| 120 | - { |
|
| 121 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | - // if first bit is 1, prepend full zero byte |
|
| 123 | - // to represent positive two's complement |
|
| 124 | - if (ord($bin[0]) & 0x80) { |
|
| 125 | - $bin = chr(0x00) . $bin; |
|
| 126 | - } |
|
| 127 | - return $bin; |
|
| 128 | - } |
|
| 112 | + /** |
|
| 113 | + * Encode positive integer to DER content. |
|
| 114 | + * |
|
| 115 | + * @param \GMP $num |
|
| 116 | + * |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + private static function _encodePositiveInteger(\GMP $num): string |
|
| 120 | + { |
|
| 121 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | + // if first bit is 1, prepend full zero byte |
|
| 123 | + // to represent positive two's complement |
|
| 124 | + if (ord($bin[0]) & 0x80) { |
|
| 125 | + $bin = chr(0x00) . $bin; |
|
| 126 | + } |
|
| 127 | + return $bin; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Encode negative integer to DER content. |
|
| 132 | - * |
|
| 133 | - * @param \GMP $num |
|
| 134 | - * |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
| 138 | - { |
|
| 139 | - $num = gmp_abs($num); |
|
| 140 | - // compute number of bytes required |
|
| 141 | - $width = 1; |
|
| 142 | - if ($num > 128) { |
|
| 143 | - $tmp = $num; |
|
| 144 | - do { |
|
| 145 | - ++$width; |
|
| 146 | - $tmp >>= 8; |
|
| 147 | - } while ($tmp > 128); |
|
| 148 | - } |
|
| 149 | - // compute two's complement 2^n - x |
|
| 150 | - $num = gmp_pow('2', 8 * $width) - $num; |
|
| 151 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 152 | - // if first bit is 0, prepend full inverted byte |
|
| 153 | - // to represent negative two's complement |
|
| 154 | - if (!(ord($bin[0]) & 0x80)) { |
|
| 155 | - $bin = chr(0xff) . $bin; |
|
| 156 | - } |
|
| 157 | - return $bin; |
|
| 158 | - } |
|
| 130 | + /** |
|
| 131 | + * Encode negative integer to DER content. |
|
| 132 | + * |
|
| 133 | + * @param \GMP $num |
|
| 134 | + * |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
| 138 | + { |
|
| 139 | + $num = gmp_abs($num); |
|
| 140 | + // compute number of bytes required |
|
| 141 | + $width = 1; |
|
| 142 | + if ($num > 128) { |
|
| 143 | + $tmp = $num; |
|
| 144 | + do { |
|
| 145 | + ++$width; |
|
| 146 | + $tmp >>= 8; |
|
| 147 | + } while ($tmp > 128); |
|
| 148 | + } |
|
| 149 | + // compute two's complement 2^n - x |
|
| 150 | + $num = gmp_pow('2', 8 * $width) - $num; |
|
| 151 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 152 | + // if first bit is 0, prepend full inverted byte |
|
| 153 | + // to represent negative two's complement |
|
| 154 | + if (!(ord($bin[0]) & 0x80)) { |
|
| 155 | + $bin = chr(0xff) . $bin; |
|
| 156 | + } |
|
| 157 | + return $bin; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Test that number is valid for this context. |
|
| 162 | - * |
|
| 163 | - * @param mixed $num |
|
| 164 | - * |
|
| 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 | - } |
|
| 160 | + /** |
|
| 161 | + * Test that number is valid for this context. |
|
| 162 | + * |
|
| 163 | + * @param mixed $num |
|
| 164 | + * |
|
| 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 Sop\ASN1\Type\Primitive; |
| 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; |
| 6 | 6 | |
@@ -12,60 +12,60 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class StringType extends Element implements Stringable |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * String value. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $_string; |
|
| 15 | + /** |
|
| 16 | + * String value. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $_string; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Constructor. |
|
| 24 | - * |
|
| 25 | - * @param string $string |
|
| 26 | - * |
|
| 27 | - * @throws \InvalidArgumentException |
|
| 28 | - */ |
|
| 29 | - public function __construct(string $string) |
|
| 30 | - { |
|
| 31 | - if (!$this->_validateString($string)) { |
|
| 32 | - throw new \InvalidArgumentException( |
|
| 33 | - sprintf('Not a valid %s string.', |
|
| 34 | - self::tagToName($this->_typeTag))); |
|
| 35 | - } |
|
| 36 | - $this->_string = $string; |
|
| 37 | - } |
|
| 22 | + /** |
|
| 23 | + * Constructor. |
|
| 24 | + * |
|
| 25 | + * @param string $string |
|
| 26 | + * |
|
| 27 | + * @throws \InvalidArgumentException |
|
| 28 | + */ |
|
| 29 | + public function __construct(string $string) |
|
| 30 | + { |
|
| 31 | + if (!$this->_validateString($string)) { |
|
| 32 | + throw new \InvalidArgumentException( |
|
| 33 | + sprintf('Not a valid %s string.', |
|
| 34 | + self::tagToName($this->_typeTag))); |
|
| 35 | + } |
|
| 36 | + $this->_string = $string; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * {@inheritdoc} |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function __toString(): string |
|
| 45 | - { |
|
| 46 | - return $this->string(); |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * {@inheritdoc} |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function __toString(): string |
|
| 45 | + { |
|
| 46 | + return $this->string(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Get the string value. |
|
| 51 | - * |
|
| 52 | - * @return string |
|
| 53 | - */ |
|
| 54 | - public function string(): string |
|
| 55 | - { |
|
| 56 | - return $this->_string; |
|
| 57 | - } |
|
| 49 | + /** |
|
| 50 | + * Get the string value. |
|
| 51 | + * |
|
| 52 | + * @return string |
|
| 53 | + */ |
|
| 54 | + public function string(): string |
|
| 55 | + { |
|
| 56 | + return $this->_string; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Check whether string is valid for the concrete type. |
|
| 61 | - * |
|
| 62 | - * @param string $string |
|
| 63 | - * |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - protected function _validateString(string $string): bool |
|
| 67 | - { |
|
| 68 | - // Override in derived classes |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Check whether string is valid for the concrete type. |
|
| 61 | + * |
|
| 62 | + * @param string $string |
|
| 63 | + * |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + protected function _validateString(string $string): bool |
|
| 67 | + { |
|
| 68 | + // Override in derived classes |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -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; |
| 6 | 6 | |
@@ -14,76 +14,76 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class DERData extends Element |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * DER encoded data. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $_der; |
|
| 17 | + /** |
|
| 18 | + * DER encoded data. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $_der; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Identifier of the underlying type. |
|
| 26 | - * |
|
| 27 | - * @var Identifier |
|
| 28 | - */ |
|
| 29 | - protected $_identifier; |
|
| 24 | + /** |
|
| 25 | + * Identifier of the underlying type. |
|
| 26 | + * |
|
| 27 | + * @var Identifier |
|
| 28 | + */ |
|
| 29 | + protected $_identifier; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Offset to the content in DER data. |
|
| 33 | - * |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - protected $_contentOffset = 0; |
|
| 31 | + /** |
|
| 32 | + * Offset to the content in DER data. |
|
| 33 | + * |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + protected $_contentOffset = 0; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $data DER encoded data |
|
| 42 | - * |
|
| 43 | - * @throws \Sop\ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 44 | - */ |
|
| 45 | - public function __construct(string $data) |
|
| 46 | - { |
|
| 47 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 48 | - // check that length encoding is valid |
|
| 49 | - Length::expectFromDER($data, $this->_contentOffset); |
|
| 50 | - $this->_der = $data; |
|
| 51 | - $this->_typeTag = $this->_identifier->intTag(); |
|
| 52 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $data DER encoded data |
|
| 42 | + * |
|
| 43 | + * @throws \Sop\ASN1\Exception\DecodeException If data does not adhere to DER |
|
| 44 | + */ |
|
| 45 | + public function __construct(string $data) |
|
| 46 | + { |
|
| 47 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
| 48 | + // check that length encoding is valid |
|
| 49 | + Length::expectFromDER($data, $this->_contentOffset); |
|
| 50 | + $this->_der = $data; |
|
| 51 | + $this->_typeTag = $this->_identifier->intTag(); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * {@inheritdoc} |
|
| 56 | - */ |
|
| 57 | - public function typeClass(): int |
|
| 58 | - { |
|
| 59 | - return $this->_identifier->typeClass(); |
|
| 60 | - } |
|
| 54 | + /** |
|
| 55 | + * {@inheritdoc} |
|
| 56 | + */ |
|
| 57 | + public function typeClass(): int |
|
| 58 | + { |
|
| 59 | + return $this->_identifier->typeClass(); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * {@inheritdoc} |
|
| 64 | - */ |
|
| 65 | - public function isConstructed(): bool |
|
| 66 | - { |
|
| 67 | - return $this->_identifier->isConstructed(); |
|
| 68 | - } |
|
| 62 | + /** |
|
| 63 | + * {@inheritdoc} |
|
| 64 | + */ |
|
| 65 | + public function isConstructed(): bool |
|
| 66 | + { |
|
| 67 | + return $this->_identifier->isConstructed(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * {@inheritdoc} |
|
| 72 | - */ |
|
| 73 | - public function toDER(): string |
|
| 74 | - { |
|
| 75 | - return $this->_der; |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * {@inheritdoc} |
|
| 72 | + */ |
|
| 73 | + public function toDER(): string |
|
| 74 | + { |
|
| 75 | + return $this->_der; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * {@inheritdoc} |
|
| 80 | - */ |
|
| 81 | - protected function _encodedContentDER(): string |
|
| 82 | - { |
|
| 83 | - // if there's no content payload |
|
| 84 | - if (strlen($this->_der) === $this->_contentOffset) { |
|
| 85 | - return ''; |
|
| 86 | - } |
|
| 87 | - return substr($this->_der, $this->_contentOffset); |
|
| 88 | - } |
|
| 78 | + /** |
|
| 79 | + * {@inheritdoc} |
|
| 80 | + */ |
|
| 81 | + protected function _encodedContentDER(): string |
|
| 82 | + { |
|
| 83 | + // if there's no content payload |
|
| 84 | + if (strlen($this->_der) === $this->_contentOffset) { |
|
| 85 | + return ''; |
|
| 86 | + } |
|
| 87 | + return substr($this->_der, $this->_contentOffset); |
|
| 88 | + } |
|
| 89 | 89 | } |