@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class VideotexString extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_VIDEOTEX_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_VIDEOTEX_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - protected function _validateString(string $string): bool |
|
| 32 | - { |
|
| 33 | - // allow everything |
|
| 34 | - return true; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + protected function _validateString(string $string): bool |
|
| 32 | + { |
|
| 33 | + // allow everything |
|
| 34 | + return true; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -15,28 +15,28 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class BMPString extends PrimitiveString |
| 17 | 17 | { |
| 18 | - use UniversalClass; |
|
| 18 | + use UniversalClass; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Constructor. |
|
| 22 | - * |
|
| 23 | - * @param string $string |
|
| 24 | - */ |
|
| 25 | - public function __construct(string $string) |
|
| 26 | - { |
|
| 27 | - $this->_typeTag = self::TYPE_BMP_STRING; |
|
| 28 | - parent::__construct($string); |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * Constructor. |
|
| 22 | + * |
|
| 23 | + * @param string $string |
|
| 24 | + */ |
|
| 25 | + public function __construct(string $string) |
|
| 26 | + { |
|
| 27 | + $this->_typeTag = self::TYPE_BMP_STRING; |
|
| 28 | + parent::__construct($string); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - protected function _validateString(string $string): bool |
|
| 35 | - { |
|
| 36 | - // UCS-2 has fixed with of 2 octets (16 bits) |
|
| 37 | - if (0 !== strlen($string) % 2) { |
|
| 38 | - return false; |
|
| 39 | - } |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 31 | + /** |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + protected function _validateString(string $string): bool |
|
| 35 | + { |
|
| 36 | + // UCS-2 has fixed with of 2 octets (16 bits) |
|
| 37 | + if (0 !== strlen($string) % 2) { |
|
| 38 | + return false; |
|
| 39 | + } |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -13,36 +13,36 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RelativeOID extends ObjectIdentifier |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $oid OID in dotted format |
|
| 20 | - */ |
|
| 21 | - public function __construct(string $oid) |
|
| 22 | - { |
|
| 23 | - $this->_oid = $oid; |
|
| 24 | - $this->_subids = self::_explodeDottedOID($oid); |
|
| 25 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 26 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $oid OID in dotted format |
|
| 20 | + */ |
|
| 21 | + public function __construct(string $oid) |
|
| 22 | + { |
|
| 23 | + $this->_oid = $oid; |
|
| 24 | + $this->_subids = self::_explodeDottedOID($oid); |
|
| 25 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - protected function _encodedContentDER(): string |
|
| 32 | - { |
|
| 33 | - return self::_encodeSubIDs(...$this->_subids); |
|
| 34 | - } |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + protected function _encodedContentDER(): string |
|
| 32 | + { |
|
| 33 | + return self::_encodeSubIDs(...$this->_subids); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 40 | - string $data, int &$offset): ElementBase |
|
| 41 | - { |
|
| 42 | - $idx = $offset; |
|
| 43 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 44 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 45 | - $offset = $idx + $len; |
|
| 46 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 40 | + string $data, int &$offset): ElementBase |
|
| 41 | + { |
|
| 42 | + $idx = $offset; |
|
| 43 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 44 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 45 | + $offset = $idx + $len; |
|
| 46 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GraphicString extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_GRAPHIC_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_GRAPHIC_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - protected function _validateString(string $string): bool |
|
| 32 | - { |
|
| 33 | - // allow everything |
|
| 34 | - return true; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + protected function _validateString(string $string): bool |
|
| 32 | + { |
|
| 33 | + // allow everything |
|
| 34 | + return true; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait PrimitiveType |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @see \Sop\ASN1\Feature\ElementBase::isConstructed() |
|
| 14 | - * |
|
| 15 | - * @return bool |
|
| 16 | - */ |
|
| 17 | - public function isConstructed(): bool |
|
| 18 | - { |
|
| 19 | - return false; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * @see \Sop\ASN1\Feature\ElementBase::isConstructed() |
|
| 14 | + * |
|
| 15 | + * @return bool |
|
| 16 | + */ |
|
| 17 | + public function isConstructed(): bool |
|
| 18 | + { |
|
| 19 | + return false; |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -14,79 +14,79 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | abstract class TaggedType extends Element |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Check whether element supports explicit tagging. |
|
| 19 | - * |
|
| 20 | - * @param null|int $expectedTag Optional outer tag expectation |
|
| 21 | - * |
|
| 22 | - * @throws \UnexpectedValueException If expectation fails |
|
| 23 | - * |
|
| 24 | - * @return ExplicitTagging |
|
| 25 | - */ |
|
| 26 | - public function expectExplicit(?int $expectedTag = null): ExplicitTagging |
|
| 27 | - { |
|
| 28 | - $el = $this; |
|
| 29 | - if (!$el instanceof ExplicitTagging) { |
|
| 30 | - throw new \UnexpectedValueException( |
|
| 31 | - "Element doesn't implement explicit tagging."); |
|
| 32 | - } |
|
| 33 | - if (isset($expectedTag)) { |
|
| 34 | - $el->expectTagged($expectedTag); |
|
| 35 | - } |
|
| 36 | - return $el; |
|
| 37 | - } |
|
| 17 | + /** |
|
| 18 | + * Check whether element supports explicit tagging. |
|
| 19 | + * |
|
| 20 | + * @param null|int $expectedTag Optional outer tag expectation |
|
| 21 | + * |
|
| 22 | + * @throws \UnexpectedValueException If expectation fails |
|
| 23 | + * |
|
| 24 | + * @return ExplicitTagging |
|
| 25 | + */ |
|
| 26 | + public function expectExplicit(?int $expectedTag = null): ExplicitTagging |
|
| 27 | + { |
|
| 28 | + $el = $this; |
|
| 29 | + if (!$el instanceof ExplicitTagging) { |
|
| 30 | + throw new \UnexpectedValueException( |
|
| 31 | + "Element doesn't implement explicit tagging."); |
|
| 32 | + } |
|
| 33 | + if (isset($expectedTag)) { |
|
| 34 | + $el->expectTagged($expectedTag); |
|
| 35 | + } |
|
| 36 | + return $el; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get the wrapped inner element employing explicit tagging. |
|
| 41 | - * |
|
| 42 | - * @param null|int $expectedTag Optional outer tag expectation |
|
| 43 | - * |
|
| 44 | - * @throws \UnexpectedValueException If expectation fails |
|
| 45 | - * |
|
| 46 | - * @return UnspecifiedType |
|
| 47 | - */ |
|
| 48 | - public function asExplicit(?int $expectedTag = null): UnspecifiedType |
|
| 49 | - { |
|
| 50 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
| 51 | - } |
|
| 39 | + /** |
|
| 40 | + * Get the wrapped inner element employing explicit tagging. |
|
| 41 | + * |
|
| 42 | + * @param null|int $expectedTag Optional outer tag expectation |
|
| 43 | + * |
|
| 44 | + * @throws \UnexpectedValueException If expectation fails |
|
| 45 | + * |
|
| 46 | + * @return UnspecifiedType |
|
| 47 | + */ |
|
| 48 | + public function asExplicit(?int $expectedTag = null): UnspecifiedType |
|
| 49 | + { |
|
| 50 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Check whether element supports implicit tagging. |
|
| 55 | - * |
|
| 56 | - * @param null|int $expectedTag Optional outer tag expectation |
|
| 57 | - * |
|
| 58 | - * @throws \UnexpectedValueException If expectation fails |
|
| 59 | - * |
|
| 60 | - * @return ImplicitTagging |
|
| 61 | - */ |
|
| 62 | - public function expectImplicit(?int $expectedTag = null): ImplicitTagging |
|
| 63 | - { |
|
| 64 | - $el = $this; |
|
| 65 | - if (!$el instanceof ImplicitTagging) { |
|
| 66 | - throw new \UnexpectedValueException( |
|
| 67 | - "Element doesn't implement implicit tagging."); |
|
| 68 | - } |
|
| 69 | - if (isset($expectedTag)) { |
|
| 70 | - $el->expectTagged($expectedTag); |
|
| 71 | - } |
|
| 72 | - return $el; |
|
| 73 | - } |
|
| 53 | + /** |
|
| 54 | + * Check whether element supports implicit tagging. |
|
| 55 | + * |
|
| 56 | + * @param null|int $expectedTag Optional outer tag expectation |
|
| 57 | + * |
|
| 58 | + * @throws \UnexpectedValueException If expectation fails |
|
| 59 | + * |
|
| 60 | + * @return ImplicitTagging |
|
| 61 | + */ |
|
| 62 | + public function expectImplicit(?int $expectedTag = null): ImplicitTagging |
|
| 63 | + { |
|
| 64 | + $el = $this; |
|
| 65 | + if (!$el instanceof ImplicitTagging) { |
|
| 66 | + throw new \UnexpectedValueException( |
|
| 67 | + "Element doesn't implement implicit tagging."); |
|
| 68 | + } |
|
| 69 | + if (isset($expectedTag)) { |
|
| 70 | + $el->expectTagged($expectedTag); |
|
| 71 | + } |
|
| 72 | + return $el; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Get the wrapped inner element employing implicit tagging. |
|
| 77 | - * |
|
| 78 | - * @param int $tag Type tag of the inner element |
|
| 79 | - * @param null|int $expectedTag Optional outer tag expectation |
|
| 80 | - * @param int $expectedClass Optional inner type class expectation |
|
| 81 | - * |
|
| 82 | - * @throws \UnexpectedValueException If expectation fails |
|
| 83 | - * |
|
| 84 | - * @return UnspecifiedType |
|
| 85 | - */ |
|
| 86 | - public function asImplicit(int $tag, ?int $expectedTag = null, |
|
| 87 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 88 | - { |
|
| 89 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 90 | - $expectedClass); |
|
| 91 | - } |
|
| 75 | + /** |
|
| 76 | + * Get the wrapped inner element employing implicit tagging. |
|
| 77 | + * |
|
| 78 | + * @param int $tag Type tag of the inner element |
|
| 79 | + * @param null|int $expectedTag Optional outer tag expectation |
|
| 80 | + * @param int $expectedClass Optional inner type class expectation |
|
| 81 | + * |
|
| 82 | + * @throws \UnexpectedValueException If expectation fails |
|
| 83 | + * |
|
| 84 | + * @return UnspecifiedType |
|
| 85 | + */ |
|
| 86 | + public function asImplicit(int $tag, ?int $expectedTag = null, |
|
| 87 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 88 | + { |
|
| 89 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 90 | + $expectedClass); |
|
| 91 | + } |
|
| 92 | 92 | } |
@@ -11,149 +11,149 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Flags |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Flag octets. |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - protected $_flags; |
|
| 14 | + /** |
|
| 15 | + * Flag octets. |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + protected $_flags; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Number of flags. |
|
| 23 | - * |
|
| 24 | - * @var int |
|
| 25 | - */ |
|
| 26 | - protected $_width; |
|
| 21 | + /** |
|
| 22 | + * Number of flags. |
|
| 23 | + * |
|
| 24 | + * @var int |
|
| 25 | + */ |
|
| 26 | + protected $_width; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param int|string $flags Flags |
|
| 32 | - * @param int $width The number of flags. If width is larger than |
|
| 33 | - * number of bits in $flags, zeroes are prepended |
|
| 34 | - * to flag field. |
|
| 35 | - */ |
|
| 36 | - public function __construct($flags, int $width) |
|
| 37 | - { |
|
| 38 | - if (!$width) { |
|
| 39 | - $this->_flags = ''; |
|
| 40 | - } else { |
|
| 41 | - // calculate number of unused bits in last octet |
|
| 42 | - $last_octet_bits = $width % 8; |
|
| 43 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 44 | - $num = gmp_init($flags); |
|
| 45 | - // mask bits outside bitfield width |
|
| 46 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
| 47 | - $num &= $mask; |
|
| 48 | - // shift towards MSB if needed |
|
| 49 | - $data = gmp_export($num << $unused_bits, 1, |
|
| 50 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 51 | - $octets = unpack('C*', $data); |
|
| 52 | - assert(is_array($octets), new \RuntimeException('unpack() failed')); |
|
| 53 | - $bits = count($octets) * 8; |
|
| 54 | - // pad with zeroes |
|
| 55 | - while ($bits < $width) { |
|
| 56 | - array_unshift($octets, 0); |
|
| 57 | - $bits += 8; |
|
| 58 | - } |
|
| 59 | - $this->_flags = pack('C*', ...$octets); |
|
| 60 | - } |
|
| 61 | - $this->_width = $width; |
|
| 62 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param int|string $flags Flags |
|
| 32 | + * @param int $width The number of flags. If width is larger than |
|
| 33 | + * number of bits in $flags, zeroes are prepended |
|
| 34 | + * to flag field. |
|
| 35 | + */ |
|
| 36 | + public function __construct($flags, int $width) |
|
| 37 | + { |
|
| 38 | + if (!$width) { |
|
| 39 | + $this->_flags = ''; |
|
| 40 | + } else { |
|
| 41 | + // calculate number of unused bits in last octet |
|
| 42 | + $last_octet_bits = $width % 8; |
|
| 43 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 44 | + $num = gmp_init($flags); |
|
| 45 | + // mask bits outside bitfield width |
|
| 46 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
| 47 | + $num &= $mask; |
|
| 48 | + // shift towards MSB if needed |
|
| 49 | + $data = gmp_export($num << $unused_bits, 1, |
|
| 50 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 51 | + $octets = unpack('C*', $data); |
|
| 52 | + assert(is_array($octets), new \RuntimeException('unpack() failed')); |
|
| 53 | + $bits = count($octets) * 8; |
|
| 54 | + // pad with zeroes |
|
| 55 | + while ($bits < $width) { |
|
| 56 | + array_unshift($octets, 0); |
|
| 57 | + $bits += 8; |
|
| 58 | + } |
|
| 59 | + $this->_flags = pack('C*', ...$octets); |
|
| 60 | + } |
|
| 61 | + $this->_width = $width; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Initialize from BitString. |
|
| 66 | - * |
|
| 67 | - * @param BitString $bs |
|
| 68 | - * @param int $width |
|
| 69 | - * |
|
| 70 | - * @return self |
|
| 71 | - */ |
|
| 72 | - public static function fromBitString(BitString $bs, int $width): self |
|
| 73 | - { |
|
| 74 | - $num_bits = $bs->numBits(); |
|
| 75 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 76 | - $num >>= $bs->unusedBits(); |
|
| 77 | - if ($num_bits < $width) { |
|
| 78 | - $num <<= ($width - $num_bits); |
|
| 79 | - } |
|
| 80 | - return new self(gmp_strval($num, 10), $width); |
|
| 81 | - } |
|
| 64 | + /** |
|
| 65 | + * Initialize from BitString. |
|
| 66 | + * |
|
| 67 | + * @param BitString $bs |
|
| 68 | + * @param int $width |
|
| 69 | + * |
|
| 70 | + * @return self |
|
| 71 | + */ |
|
| 72 | + public static function fromBitString(BitString $bs, int $width): self |
|
| 73 | + { |
|
| 74 | + $num_bits = $bs->numBits(); |
|
| 75 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 76 | + $num >>= $bs->unusedBits(); |
|
| 77 | + if ($num_bits < $width) { |
|
| 78 | + $num <<= ($width - $num_bits); |
|
| 79 | + } |
|
| 80 | + return new self(gmp_strval($num, 10), $width); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Check whether a bit at given index is set. |
|
| 85 | - * |
|
| 86 | - * Index 0 is the leftmost bit. |
|
| 87 | - * |
|
| 88 | - * @param int $idx |
|
| 89 | - * |
|
| 90 | - * @throws \OutOfBoundsException |
|
| 91 | - * |
|
| 92 | - * @return bool |
|
| 93 | - */ |
|
| 94 | - public function test(int $idx): bool |
|
| 95 | - { |
|
| 96 | - if ($idx >= $this->_width) { |
|
| 97 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
| 98 | - } |
|
| 99 | - // octet index |
|
| 100 | - $oi = (int) floor($idx / 8); |
|
| 101 | - $byte = $this->_flags[$oi]; |
|
| 102 | - // bit index |
|
| 103 | - $bi = $idx % 8; |
|
| 104 | - // index 0 is the most significant bit in byte |
|
| 105 | - $mask = 0x01 << (7 - $bi); |
|
| 106 | - return (ord($byte) & $mask) > 0; |
|
| 107 | - } |
|
| 83 | + /** |
|
| 84 | + * Check whether a bit at given index is set. |
|
| 85 | + * |
|
| 86 | + * Index 0 is the leftmost bit. |
|
| 87 | + * |
|
| 88 | + * @param int $idx |
|
| 89 | + * |
|
| 90 | + * @throws \OutOfBoundsException |
|
| 91 | + * |
|
| 92 | + * @return bool |
|
| 93 | + */ |
|
| 94 | + public function test(int $idx): bool |
|
| 95 | + { |
|
| 96 | + if ($idx >= $this->_width) { |
|
| 97 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
| 98 | + } |
|
| 99 | + // octet index |
|
| 100 | + $oi = (int) floor($idx / 8); |
|
| 101 | + $byte = $this->_flags[$oi]; |
|
| 102 | + // bit index |
|
| 103 | + $bi = $idx % 8; |
|
| 104 | + // index 0 is the most significant bit in byte |
|
| 105 | + $mask = 0x01 << (7 - $bi); |
|
| 106 | + return (ord($byte) & $mask) > 0; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Get flags as an octet string. |
|
| 111 | - * |
|
| 112 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function string(): string |
|
| 117 | - { |
|
| 118 | - return $this->_flags; |
|
| 119 | - } |
|
| 109 | + /** |
|
| 110 | + * Get flags as an octet string. |
|
| 111 | + * |
|
| 112 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function string(): string |
|
| 117 | + { |
|
| 118 | + return $this->_flags; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Get flags as a base 10 integer. |
|
| 123 | - * |
|
| 124 | - * @return string Integer as a string |
|
| 125 | - */ |
|
| 126 | - public function number(): string |
|
| 127 | - { |
|
| 128 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 129 | - $last_octet_bits = $this->_width % 8; |
|
| 130 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 131 | - $num >>= $unused_bits; |
|
| 132 | - return gmp_strval($num, 10); |
|
| 133 | - } |
|
| 121 | + /** |
|
| 122 | + * Get flags as a base 10 integer. |
|
| 123 | + * |
|
| 124 | + * @return string Integer as a string |
|
| 125 | + */ |
|
| 126 | + public function number(): string |
|
| 127 | + { |
|
| 128 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 129 | + $last_octet_bits = $this->_width % 8; |
|
| 130 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 131 | + $num >>= $unused_bits; |
|
| 132 | + return gmp_strval($num, 10); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Get flags as an integer. |
|
| 137 | - * |
|
| 138 | - * @return int |
|
| 139 | - */ |
|
| 140 | - public function intNumber(): int |
|
| 141 | - { |
|
| 142 | - $num = new BigInt($this->number()); |
|
| 143 | - return $num->intVal(); |
|
| 144 | - } |
|
| 135 | + /** |
|
| 136 | + * Get flags as an integer. |
|
| 137 | + * |
|
| 138 | + * @return int |
|
| 139 | + */ |
|
| 140 | + public function intNumber(): int |
|
| 141 | + { |
|
| 142 | + $num = new BigInt($this->number()); |
|
| 143 | + return $num->intVal(); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Get flags as a BitString. |
|
| 148 | - * |
|
| 149 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
| 150 | - * |
|
| 151 | - * @return BitString |
|
| 152 | - */ |
|
| 153 | - public function bitString(): BitString |
|
| 154 | - { |
|
| 155 | - $last_octet_bits = $this->_width % 8; |
|
| 156 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 157 | - return new BitString($this->_flags, $unused_bits); |
|
| 158 | - } |
|
| 146 | + /** |
|
| 147 | + * Get flags as a BitString. |
|
| 148 | + * |
|
| 149 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
| 150 | + * |
|
| 151 | + * @return BitString |
|
| 152 | + */ |
|
| 153 | + public function bitString(): BitString |
|
| 154 | + { |
|
| 155 | + $last_octet_bits = $this->_width % 8; |
|
| 156 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 157 | + return new BitString($this->_flags, $unused_bits); |
|
| 158 | + } |
|
| 159 | 159 | } |
@@ -23,135 +23,135 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class DERTaggedType extends TaggedType implements ExplicitTagging, 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 next byte after identifier. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_offset; |
|
| 40 | + /** |
|
| 41 | + * Offset to next byte after identifier. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_offset; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Offset to content. |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - private $_valueOffset; |
|
| 47 | + /** |
|
| 48 | + * Offset to content. |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + private $_valueOffset; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Length of the content. |
|
| 56 | - * |
|
| 57 | - * @var int |
|
| 58 | - */ |
|
| 59 | - private $_valueLength; |
|
| 54 | + /** |
|
| 55 | + * Length of the content. |
|
| 56 | + * |
|
| 57 | + * @var int |
|
| 58 | + */ |
|
| 59 | + private $_valueLength; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Constructor. |
|
| 63 | - * |
|
| 64 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 65 | - * @param string $data DER data |
|
| 66 | - * @param int $offset Offset to next byte after identifier |
|
| 67 | - * @param int $value_offset Offset to content |
|
| 68 | - * @param int $value_length Content length |
|
| 69 | - */ |
|
| 70 | - public function __construct(Identifier $identifier, string $data, |
|
| 71 | - int $offset, int $value_offset, int $value_length, |
|
| 72 | - bool $indefinite_length) |
|
| 73 | - { |
|
| 74 | - $this->_identifier = $identifier; |
|
| 75 | - $this->_data = $data; |
|
| 76 | - $this->_offset = $offset; |
|
| 77 | - $this->_valueOffset = $value_offset; |
|
| 78 | - $this->_valueLength = $value_length; |
|
| 79 | - $this->_indefiniteLength = $indefinite_length; |
|
| 80 | - $this->_typeTag = $identifier->intTag(); |
|
| 81 | - } |
|
| 61 | + /** |
|
| 62 | + * Constructor. |
|
| 63 | + * |
|
| 64 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 65 | + * @param string $data DER data |
|
| 66 | + * @param int $offset Offset to next byte after identifier |
|
| 67 | + * @param int $value_offset Offset to content |
|
| 68 | + * @param int $value_length Content length |
|
| 69 | + */ |
|
| 70 | + public function __construct(Identifier $identifier, string $data, |
|
| 71 | + int $offset, int $value_offset, int $value_length, |
|
| 72 | + bool $indefinite_length) |
|
| 73 | + { |
|
| 74 | + $this->_identifier = $identifier; |
|
| 75 | + $this->_data = $data; |
|
| 76 | + $this->_offset = $offset; |
|
| 77 | + $this->_valueOffset = $value_offset; |
|
| 78 | + $this->_valueLength = $value_length; |
|
| 79 | + $this->_indefiniteLength = $indefinite_length; |
|
| 80 | + $this->_typeTag = $identifier->intTag(); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - public function typeClass(): int |
|
| 87 | - { |
|
| 88 | - return $this->_identifier->typeClass(); |
|
| 89 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + public function typeClass(): int |
|
| 87 | + { |
|
| 88 | + return $this->_identifier->typeClass(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - public function isConstructed(): bool |
|
| 95 | - { |
|
| 96 | - return $this->_identifier->isConstructed(); |
|
| 97 | - } |
|
| 91 | + /** |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + public function isConstructed(): bool |
|
| 95 | + { |
|
| 96 | + return $this->_identifier->isConstructed(); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * {@inheritdoc} |
|
| 101 | - */ |
|
| 102 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 103 | - { |
|
| 104 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 105 | - $cls = self::_determineImplClass($identifier); |
|
| 106 | - $idx = $this->_offset; |
|
| 107 | - /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
| 108 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 109 | - return $element->asUnspecified(); |
|
| 110 | - } |
|
| 99 | + /** |
|
| 100 | + * {@inheritdoc} |
|
| 101 | + */ |
|
| 102 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 103 | + { |
|
| 104 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 105 | + $cls = self::_determineImplClass($identifier); |
|
| 106 | + $idx = $this->_offset; |
|
| 107 | + /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
| 108 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 109 | + return $element->asUnspecified(); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - */ |
|
| 115 | - public function explicit(): UnspecifiedType |
|
| 116 | - { |
|
| 117 | - $idx = $this->_valueOffset; |
|
| 118 | - return Element::fromDER($this->_data, $idx)->asUnspecified(); |
|
| 119 | - } |
|
| 112 | + /** |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + */ |
|
| 115 | + public function explicit(): UnspecifiedType |
|
| 116 | + { |
|
| 117 | + $idx = $this->_valueOffset; |
|
| 118 | + return Element::fromDER($this->_data, $idx)->asUnspecified(); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * {@inheritdoc} |
|
| 123 | - */ |
|
| 124 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 125 | - string $data, int &$offset): ElementBase |
|
| 126 | - { |
|
| 127 | - $idx = $offset; |
|
| 128 | - $length = Length::expectFromDER($data, $idx); |
|
| 129 | - // offset to inner value |
|
| 130 | - $value_offset = $idx; |
|
| 131 | - if ($length->isIndefinite()) { |
|
| 132 | - if ($identifier->isPrimitive()) { |
|
| 133 | - throw new DecodeException( |
|
| 134 | - 'Primitive type with indefinite length is not supported.'); |
|
| 135 | - } |
|
| 136 | - while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
| 137 | - // EOC consists of two octets. |
|
| 138 | - $value_length = $idx - $value_offset - 2; |
|
| 139 | - } else { |
|
| 140 | - $value_length = $length->intLength(); |
|
| 141 | - $idx += $value_length; |
|
| 142 | - } |
|
| 143 | - // late static binding since ApplicationType and PrivateType extend this class |
|
| 144 | - $type = new static($identifier, $data, $offset, $value_offset, |
|
| 145 | - $value_length, $length->isIndefinite()); |
|
| 146 | - $offset = $idx; |
|
| 147 | - return $type; |
|
| 148 | - } |
|
| 121 | + /** |
|
| 122 | + * {@inheritdoc} |
|
| 123 | + */ |
|
| 124 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 125 | + string $data, int &$offset): ElementBase |
|
| 126 | + { |
|
| 127 | + $idx = $offset; |
|
| 128 | + $length = Length::expectFromDER($data, $idx); |
|
| 129 | + // offset to inner value |
|
| 130 | + $value_offset = $idx; |
|
| 131 | + if ($length->isIndefinite()) { |
|
| 132 | + if ($identifier->isPrimitive()) { |
|
| 133 | + throw new DecodeException( |
|
| 134 | + 'Primitive type with indefinite length is not supported.'); |
|
| 135 | + } |
|
| 136 | + while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
| 137 | + // EOC consists of two octets. |
|
| 138 | + $value_length = $idx - $value_offset - 2; |
|
| 139 | + } else { |
|
| 140 | + $value_length = $length->intLength(); |
|
| 141 | + $idx += $value_length; |
|
| 142 | + } |
|
| 143 | + // late static binding since ApplicationType and PrivateType extend this class |
|
| 144 | + $type = new static($identifier, $data, $offset, $value_offset, |
|
| 145 | + $value_length, $length->isIndefinite()); |
|
| 146 | + $offset = $idx; |
|
| 147 | + return $type; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * {@inheritdoc} |
|
| 152 | - */ |
|
| 153 | - protected function _encodedContentDER(): string |
|
| 154 | - { |
|
| 155 | - return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
| 156 | - } |
|
| 150 | + /** |
|
| 151 | + * {@inheritdoc} |
|
| 152 | + */ |
|
| 153 | + protected function _encodedContentDER(): string |
|
| 154 | + { |
|
| 155 | + return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -12,10 +12,10 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | interface ExplicitTagging extends ElementBase |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Get explicitly tagged wrapped element. |
|
| 17 | - * |
|
| 18 | - * @return UnspecifiedType |
|
| 19 | - */ |
|
| 20 | - public function explicit(): UnspecifiedType; |
|
| 15 | + /** |
|
| 16 | + * Get explicitly tagged wrapped element. |
|
| 17 | + * |
|
| 18 | + * @return UnspecifiedType |
|
| 19 | + */ |
|
| 20 | + public function explicit(): UnspecifiedType; |
|
| 21 | 21 | } |