@@ -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,50 +11,50 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class StringType extends Element |
13 | 13 | { |
14 | - /** |
|
15 | - * String value. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $_string; |
|
14 | + /** |
|
15 | + * String value. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $_string; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Constructor. |
|
23 | - * |
|
24 | - * @param string $string |
|
25 | - * |
|
26 | - * @throws \InvalidArgumentException |
|
27 | - */ |
|
28 | - public function __construct(string $string) |
|
29 | - { |
|
30 | - if (!$this->_validateString($string)) { |
|
31 | - throw new \InvalidArgumentException( |
|
32 | - sprintf('Not a valid %s string.', |
|
33 | - self::tagToName($this->_typeTag))); |
|
34 | - } |
|
35 | - $this->_string = $string; |
|
36 | - } |
|
21 | + /** |
|
22 | + * Constructor. |
|
23 | + * |
|
24 | + * @param string $string |
|
25 | + * |
|
26 | + * @throws \InvalidArgumentException |
|
27 | + */ |
|
28 | + public function __construct(string $string) |
|
29 | + { |
|
30 | + if (!$this->_validateString($string)) { |
|
31 | + throw new \InvalidArgumentException( |
|
32 | + sprintf('Not a valid %s string.', |
|
33 | + self::tagToName($this->_typeTag))); |
|
34 | + } |
|
35 | + $this->_string = $string; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Get the string value. |
|
40 | - * |
|
41 | - * @return string |
|
42 | - */ |
|
43 | - public function string(): string |
|
44 | - { |
|
45 | - return $this->_string; |
|
46 | - } |
|
38 | + /** |
|
39 | + * Get the string value. |
|
40 | + * |
|
41 | + * @return string |
|
42 | + */ |
|
43 | + public function string(): string |
|
44 | + { |
|
45 | + return $this->_string; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Check whether string is valid for the concrete type. |
|
50 | - * |
|
51 | - * @param string $string |
|
52 | - * |
|
53 | - * @return bool |
|
54 | - */ |
|
55 | - protected function _validateString(string $string): bool |
|
56 | - { |
|
57 | - // Override in derived classes |
|
58 | - return true; |
|
59 | - } |
|
48 | + /** |
|
49 | + * Check whether string is valid for the concrete type. |
|
50 | + * |
|
51 | + * @param string $string |
|
52 | + * |
|
53 | + * @return bool |
|
54 | + */ |
|
55 | + protected function _validateString(string $string): bool |
|
56 | + { |
|
57 | + // Override in derived classes |
|
58 | + return true; |
|
59 | + } |
|
60 | 60 | } |
@@ -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 | } |
@@ -6,107 +6,107 @@ |
||
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 | - * |
|
21 | - * @var null|int |
|
22 | - */ |
|
23 | - private $_intNum; |
|
16 | + /** |
|
17 | + * Number as an integer type. |
|
18 | + * |
|
19 | + * @internal Lazily initialized |
|
20 | + * |
|
21 | + * @var null|int |
|
22 | + */ |
|
23 | + private $_intNum; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param int|string $num |
|
29 | - */ |
|
30 | - public function __construct($num) |
|
31 | - { |
|
32 | - $this->_num = strval($num); |
|
33 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param int|string $num |
|
29 | + */ |
|
30 | + public function __construct($num) |
|
31 | + { |
|
32 | + $this->_num = strval($num); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function __toString() |
|
39 | - { |
|
40 | - return $this->base10(); |
|
41 | - } |
|
35 | + /** |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function __toString() |
|
39 | + { |
|
40 | + return $this->base10(); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the number as a base10 integer string. |
|
45 | - * |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function base10(): string |
|
49 | - { |
|
50 | - return $this->_num; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Get the number as a base10 integer string. |
|
45 | + * |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function base10(): string |
|
49 | + { |
|
50 | + return $this->_num; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the number as an integer. |
|
55 | - * |
|
56 | - * @throws \RuntimeException If number overflows integer size |
|
57 | - * |
|
58 | - * @return int |
|
59 | - */ |
|
60 | - public function intVal(): int |
|
61 | - { |
|
62 | - if (!isset($this->_intNum)) { |
|
63 | - $num = gmp_init($this->_num, 10); |
|
64 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
65 | - throw new \RuntimeException('Integer overflow.'); |
|
66 | - } |
|
67 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
68 | - throw new \RuntimeException('Integer underflow.'); |
|
69 | - } |
|
70 | - $this->_intNum = gmp_intval($num); |
|
71 | - } |
|
72 | - return $this->_intNum; |
|
73 | - } |
|
53 | + /** |
|
54 | + * Get the number as an integer. |
|
55 | + * |
|
56 | + * @throws \RuntimeException If number overflows integer size |
|
57 | + * |
|
58 | + * @return int |
|
59 | + */ |
|
60 | + public function intVal(): int |
|
61 | + { |
|
62 | + if (!isset($this->_intNum)) { |
|
63 | + $num = gmp_init($this->_num, 10); |
|
64 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
65 | + throw new \RuntimeException('Integer overflow.'); |
|
66 | + } |
|
67 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
68 | + throw new \RuntimeException('Integer underflow.'); |
|
69 | + } |
|
70 | + $this->_intNum = gmp_intval($num); |
|
71 | + } |
|
72 | + return $this->_intNum; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get the number as a GMP object. |
|
77 | - * |
|
78 | - * @return \GMP |
|
79 | - */ |
|
80 | - public function gmpObj(): \GMP |
|
81 | - { |
|
82 | - return gmp_init($this->_num, 10); |
|
83 | - } |
|
75 | + /** |
|
76 | + * Get the number as a GMP object. |
|
77 | + * |
|
78 | + * @return \GMP |
|
79 | + */ |
|
80 | + public function gmpObj(): \GMP |
|
81 | + { |
|
82 | + return gmp_init($this->_num, 10); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Get the maximum integer value. |
|
87 | - * |
|
88 | - * @return \GMP |
|
89 | - */ |
|
90 | - private function _intMaxGmp(): \GMP |
|
91 | - { |
|
92 | - static $gmp; |
|
93 | - if (!isset($gmp)) { |
|
94 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
95 | - } |
|
96 | - return $gmp; |
|
97 | - } |
|
85 | + /** |
|
86 | + * Get the maximum integer value. |
|
87 | + * |
|
88 | + * @return \GMP |
|
89 | + */ |
|
90 | + private function _intMaxGmp(): \GMP |
|
91 | + { |
|
92 | + static $gmp; |
|
93 | + if (!isset($gmp)) { |
|
94 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
95 | + } |
|
96 | + return $gmp; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get the minimum integer value. |
|
101 | - * |
|
102 | - * @return \GMP |
|
103 | - */ |
|
104 | - private function _intMinGmp(): \GMP |
|
105 | - { |
|
106 | - static $gmp; |
|
107 | - if (!isset($gmp)) { |
|
108 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
109 | - } |
|
110 | - return $gmp; |
|
111 | - } |
|
99 | + /** |
|
100 | + * Get the minimum integer value. |
|
101 | + * |
|
102 | + * @return \GMP |
|
103 | + */ |
|
104 | + private function _intMinGmp(): \GMP |
|
105 | + { |
|
106 | + static $gmp; |
|
107 | + if (!isset($gmp)) { |
|
108 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
109 | + } |
|
110 | + return $gmp; |
|
111 | + } |
|
112 | 112 | } |
@@ -13,301 +13,301 @@ |
||
13 | 13 | */ |
14 | 14 | class Identifier implements Encodable |
15 | 15 | { |
16 | - // Type class enumerations |
|
17 | - const CLASS_UNIVERSAL = 0b00; |
|
18 | - const CLASS_APPLICATION = 0b01; |
|
19 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
20 | - const CLASS_PRIVATE = 0b11; |
|
16 | + // Type class enumerations |
|
17 | + const CLASS_UNIVERSAL = 0b00; |
|
18 | + const CLASS_APPLICATION = 0b01; |
|
19 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
20 | + const CLASS_PRIVATE = 0b11; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Mapping from type class to human readable name. |
|
24 | - * |
|
25 | - * @internal |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - const MAP_CLASS_TO_NAME = [ |
|
30 | - self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
31 | - self::CLASS_APPLICATION => 'APPLICATION', |
|
32 | - self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
33 | - self::CLASS_PRIVATE => 'PRIVATE', |
|
34 | - ]; |
|
22 | + /** |
|
23 | + * Mapping from type class to human readable name. |
|
24 | + * |
|
25 | + * @internal |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + const MAP_CLASS_TO_NAME = [ |
|
30 | + self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
31 | + self::CLASS_APPLICATION => 'APPLICATION', |
|
32 | + self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
33 | + self::CLASS_PRIVATE => 'PRIVATE', |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var BigInt |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var BigInt |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = new BigInt($tag); |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = new BigInt($tag); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param null|int $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. |
|
81 | - * Variable is updated to the offset next to the |
|
82 | - * parsed identifier. If null, start from offset 0. |
|
83 | - * |
|
84 | - * @throws DecodeException If decoding fails |
|
85 | - * |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
89 | - { |
|
90 | - $idx = $offset ?? 0; |
|
91 | - $datalen = strlen($data); |
|
92 | - if ($idx >= $datalen) { |
|
93 | - throw new DecodeException('Invalid offset.'); |
|
94 | - } |
|
95 | - $byte = ord($data[$idx++]); |
|
96 | - // bits 8 and 7 (class) |
|
97 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
98 | - $class = (0b11000000 & $byte) >> 6; |
|
99 | - // bit 6 (0 = primitive / 1 = constructed) |
|
100 | - $pc = (0b00100000 & $byte) >> 5; |
|
101 | - // bits 5 to 1 (tag number) |
|
102 | - $tag = (0b00011111 & $byte); |
|
103 | - // long-form identifier |
|
104 | - if (0x1f == $tag) { |
|
105 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
106 | - } |
|
107 | - if (isset($offset)) { |
|
108 | - $offset = $idx; |
|
109 | - } |
|
110 | - return new self($class, $pc, $tag); |
|
111 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param null|int $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. |
|
81 | + * Variable is updated to the offset next to the |
|
82 | + * parsed identifier. If null, start from offset 0. |
|
83 | + * |
|
84 | + * @throws DecodeException If decoding fails |
|
85 | + * |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
89 | + { |
|
90 | + $idx = $offset ?? 0; |
|
91 | + $datalen = strlen($data); |
|
92 | + if ($idx >= $datalen) { |
|
93 | + throw new DecodeException('Invalid offset.'); |
|
94 | + } |
|
95 | + $byte = ord($data[$idx++]); |
|
96 | + // bits 8 and 7 (class) |
|
97 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
98 | + $class = (0b11000000 & $byte) >> 6; |
|
99 | + // bit 6 (0 = primitive / 1 = constructed) |
|
100 | + $pc = (0b00100000 & $byte) >> 5; |
|
101 | + // bits 5 to 1 (tag number) |
|
102 | + $tag = (0b00011111 & $byte); |
|
103 | + // long-form identifier |
|
104 | + if (0x1f == $tag) { |
|
105 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
106 | + } |
|
107 | + if (isset($offset)) { |
|
108 | + $offset = $idx; |
|
109 | + } |
|
110 | + return new self($class, $pc, $tag); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - public function toDER(): string |
|
117 | - { |
|
118 | - $bytes = []; |
|
119 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
120 | - $tag = $this->_tag->gmpObj(); |
|
121 | - if ($tag < 0x1f) { |
|
122 | - $bytes[] = $byte | $tag; |
|
123 | - } |
|
124 | - // long-form identifier |
|
125 | - else { |
|
126 | - $bytes[] = $byte | 0x1f; |
|
127 | - $octets = []; |
|
128 | - for (; $tag > 0; $tag >>= 7) { |
|
129 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
130 | - } |
|
131 | - // last octet has bit 8 set to zero |
|
132 | - $octets[0] &= 0x7f; |
|
133 | - foreach (array_reverse($octets) as $octet) { |
|
134 | - $bytes[] = $octet; |
|
135 | - } |
|
136 | - } |
|
137 | - return pack('C*', ...$bytes); |
|
138 | - } |
|
113 | + /** |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + public function toDER(): string |
|
117 | + { |
|
118 | + $bytes = []; |
|
119 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
120 | + $tag = $this->_tag->gmpObj(); |
|
121 | + if ($tag < 0x1f) { |
|
122 | + $bytes[] = $byte | $tag; |
|
123 | + } |
|
124 | + // long-form identifier |
|
125 | + else { |
|
126 | + $bytes[] = $byte | 0x1f; |
|
127 | + $octets = []; |
|
128 | + for (; $tag > 0; $tag >>= 7) { |
|
129 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
130 | + } |
|
131 | + // last octet has bit 8 set to zero |
|
132 | + $octets[0] &= 0x7f; |
|
133 | + foreach (array_reverse($octets) as $octet) { |
|
134 | + $bytes[] = $octet; |
|
135 | + } |
|
136 | + } |
|
137 | + return pack('C*', ...$bytes); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Get class of the type. |
|
142 | - * |
|
143 | - * @return int |
|
144 | - */ |
|
145 | - public function typeClass(): int |
|
146 | - { |
|
147 | - return $this->_class; |
|
148 | - } |
|
140 | + /** |
|
141 | + * Get class of the type. |
|
142 | + * |
|
143 | + * @return int |
|
144 | + */ |
|
145 | + public function typeClass(): int |
|
146 | + { |
|
147 | + return $this->_class; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Get P/C. |
|
152 | - * |
|
153 | - * @return int |
|
154 | - */ |
|
155 | - public function pc(): int |
|
156 | - { |
|
157 | - return $this->_pc; |
|
158 | - } |
|
150 | + /** |
|
151 | + * Get P/C. |
|
152 | + * |
|
153 | + * @return int |
|
154 | + */ |
|
155 | + public function pc(): int |
|
156 | + { |
|
157 | + return $this->_pc; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Get the tag number. |
|
162 | - * |
|
163 | - * @return string Base 10 integer string |
|
164 | - */ |
|
165 | - public function tag(): string |
|
166 | - { |
|
167 | - return $this->_tag->base10(); |
|
168 | - } |
|
160 | + /** |
|
161 | + * Get the tag number. |
|
162 | + * |
|
163 | + * @return string Base 10 integer string |
|
164 | + */ |
|
165 | + public function tag(): string |
|
166 | + { |
|
167 | + return $this->_tag->base10(); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Get the tag as an integer. |
|
172 | - * |
|
173 | - * @return int |
|
174 | - */ |
|
175 | - public function intTag(): int |
|
176 | - { |
|
177 | - return $this->_tag->intVal(); |
|
178 | - } |
|
170 | + /** |
|
171 | + * Get the tag as an integer. |
|
172 | + * |
|
173 | + * @return int |
|
174 | + */ |
|
175 | + public function intTag(): int |
|
176 | + { |
|
177 | + return $this->_tag->intVal(); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Check whether type is of an universal class. |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function isUniversal(): bool |
|
186 | - { |
|
187 | - return self::CLASS_UNIVERSAL === $this->_class; |
|
188 | - } |
|
180 | + /** |
|
181 | + * Check whether type is of an universal class. |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function isUniversal(): bool |
|
186 | + { |
|
187 | + return self::CLASS_UNIVERSAL === $this->_class; |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Check whether type is of an application class. |
|
192 | - * |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - public function isApplication(): bool |
|
196 | - { |
|
197 | - return self::CLASS_APPLICATION === $this->_class; |
|
198 | - } |
|
190 | + /** |
|
191 | + * Check whether type is of an application class. |
|
192 | + * |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + public function isApplication(): bool |
|
196 | + { |
|
197 | + return self::CLASS_APPLICATION === $this->_class; |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Check whether type is of a context specific class. |
|
202 | - * |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function isContextSpecific(): bool |
|
206 | - { |
|
207 | - return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
208 | - } |
|
200 | + /** |
|
201 | + * Check whether type is of a context specific class. |
|
202 | + * |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function isContextSpecific(): bool |
|
206 | + { |
|
207 | + return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * Check whether type is of a private class. |
|
212 | - * |
|
213 | - * @return bool |
|
214 | - */ |
|
215 | - public function isPrivate(): bool |
|
216 | - { |
|
217 | - return self::CLASS_PRIVATE === $this->_class; |
|
218 | - } |
|
210 | + /** |
|
211 | + * Check whether type is of a private class. |
|
212 | + * |
|
213 | + * @return bool |
|
214 | + */ |
|
215 | + public function isPrivate(): bool |
|
216 | + { |
|
217 | + return self::CLASS_PRIVATE === $this->_class; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Check whether content is primitive type. |
|
222 | - * |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function isPrimitive(): bool |
|
226 | - { |
|
227 | - return self::PRIMITIVE === $this->_pc; |
|
228 | - } |
|
220 | + /** |
|
221 | + * Check whether content is primitive type. |
|
222 | + * |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function isPrimitive(): bool |
|
226 | + { |
|
227 | + return self::PRIMITIVE === $this->_pc; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Check hether content is constructed type. |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - public function isConstructed(): bool |
|
236 | - { |
|
237 | - return self::CONSTRUCTED === $this->_pc; |
|
238 | - } |
|
230 | + /** |
|
231 | + * Check hether content is constructed type. |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + public function isConstructed(): bool |
|
236 | + { |
|
237 | + return self::CONSTRUCTED === $this->_pc; |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Get self with given type class. |
|
242 | - * |
|
243 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
244 | - * |
|
245 | - * @return self |
|
246 | - */ |
|
247 | - public function withClass(int $class): Identifier |
|
248 | - { |
|
249 | - $obj = clone $this; |
|
250 | - $obj->_class = 0b11 & $class; |
|
251 | - return $obj; |
|
252 | - } |
|
240 | + /** |
|
241 | + * Get self with given type class. |
|
242 | + * |
|
243 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
244 | + * |
|
245 | + * @return self |
|
246 | + */ |
|
247 | + public function withClass(int $class): Identifier |
|
248 | + { |
|
249 | + $obj = clone $this; |
|
250 | + $obj->_class = 0b11 & $class; |
|
251 | + return $obj; |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Get self with given type tag. |
|
256 | - * |
|
257 | - * @param int|string $tag Tag number |
|
258 | - * |
|
259 | - * @return self |
|
260 | - */ |
|
261 | - public function withTag($tag): Identifier |
|
262 | - { |
|
263 | - $obj = clone $this; |
|
264 | - $obj->_tag = new BigInt($tag); |
|
265 | - return $obj; |
|
266 | - } |
|
254 | + /** |
|
255 | + * Get self with given type tag. |
|
256 | + * |
|
257 | + * @param int|string $tag Tag number |
|
258 | + * |
|
259 | + * @return self |
|
260 | + */ |
|
261 | + public function withTag($tag): Identifier |
|
262 | + { |
|
263 | + $obj = clone $this; |
|
264 | + $obj->_tag = new BigInt($tag); |
|
265 | + return $obj; |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Get human readable name of the type class. |
|
270 | - * |
|
271 | - * @param int $class |
|
272 | - * |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public static function classToName(int $class): string |
|
276 | - { |
|
277 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
278 | - return "CLASS ${class}"; |
|
279 | - } |
|
280 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
281 | - } |
|
268 | + /** |
|
269 | + * Get human readable name of the type class. |
|
270 | + * |
|
271 | + * @param int $class |
|
272 | + * |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public static function classToName(int $class): string |
|
276 | + { |
|
277 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
278 | + return "CLASS ${class}"; |
|
279 | + } |
|
280 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Parse long form tag. |
|
285 | - * |
|
286 | - * @param string $data DER data |
|
287 | - * @param int $offset Reference to the variable containing offset to data |
|
288 | - * |
|
289 | - * @throws DecodeException If decoding fails |
|
290 | - * |
|
291 | - * @return string Tag number |
|
292 | - */ |
|
293 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
294 | - { |
|
295 | - $datalen = strlen($data); |
|
296 | - $tag = gmp_init(0, 10); |
|
297 | - while (true) { |
|
298 | - if ($offset >= $datalen) { |
|
299 | - throw new DecodeException( |
|
300 | - 'Unexpected end of data while decoding' . |
|
301 | - ' long form identifier.'); |
|
302 | - } |
|
303 | - $byte = ord($data[$offset++]); |
|
304 | - $tag <<= 7; |
|
305 | - $tag |= 0x7f & $byte; |
|
306 | - // last byte has bit 8 set to zero |
|
307 | - if (!(0x80 & $byte)) { |
|
308 | - break; |
|
309 | - } |
|
310 | - } |
|
311 | - return gmp_strval($tag, 10); |
|
312 | - } |
|
283 | + /** |
|
284 | + * Parse long form tag. |
|
285 | + * |
|
286 | + * @param string $data DER data |
|
287 | + * @param int $offset Reference to the variable containing offset to data |
|
288 | + * |
|
289 | + * @throws DecodeException If decoding fails |
|
290 | + * |
|
291 | + * @return string Tag number |
|
292 | + */ |
|
293 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
294 | + { |
|
295 | + $datalen = strlen($data); |
|
296 | + $tag = gmp_init(0, 10); |
|
297 | + while (true) { |
|
298 | + if ($offset >= $datalen) { |
|
299 | + throw new DecodeException( |
|
300 | + 'Unexpected end of data while decoding' . |
|
301 | + ' long form identifier.'); |
|
302 | + } |
|
303 | + $byte = ord($data[$offset++]); |
|
304 | + $tag <<= 7; |
|
305 | + $tag |= 0x7f & $byte; |
|
306 | + // last byte has bit 8 set to zero |
|
307 | + if (!(0x80 & $byte)) { |
|
308 | + break; |
|
309 | + } |
|
310 | + } |
|
311 | + return gmp_strval($tag, 10); |
|
312 | + } |
|
313 | 313 | } |
@@ -13,226 +13,226 @@ |
||
13 | 13 | */ |
14 | 14 | class Length implements Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Length. |
|
18 | - * |
|
19 | - * @var BigInt |
|
20 | - */ |
|
21 | - private $_length; |
|
16 | + /** |
|
17 | + * Length. |
|
18 | + * |
|
19 | + * @var BigInt |
|
20 | + */ |
|
21 | + private $_length; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Whether length is indefinite. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_indefinite; |
|
23 | + /** |
|
24 | + * Whether length is indefinite. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_indefinite; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $length Length |
|
34 | - * @param bool $indefinite Whether length is indefinite |
|
35 | - */ |
|
36 | - public function __construct($length, bool $indefinite = false) |
|
37 | - { |
|
38 | - $this->_length = new BigInt($length); |
|
39 | - $this->_indefinite = $indefinite; |
|
40 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $length Length |
|
34 | + * @param bool $indefinite Whether length is indefinite |
|
35 | + */ |
|
36 | + public function __construct($length, bool $indefinite = false) |
|
37 | + { |
|
38 | + $this->_length = new BigInt($length); |
|
39 | + $this->_indefinite = $indefinite; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Decode length component from DER data. |
|
44 | - * |
|
45 | - * @param string $data DER encoded data |
|
46 | - * @param null|int $offset Reference to the variable that contains offset |
|
47 | - * into the data where to start parsing. |
|
48 | - * Variable is updated to the offset next to the |
|
49 | - * parsed length component. If null, start from offset 0. |
|
50 | - * |
|
51 | - * @throws DecodeException If decoding fails |
|
52 | - * |
|
53 | - * @return self |
|
54 | - */ |
|
55 | - public static function fromDER(string $data, int &$offset = null): self |
|
56 | - { |
|
57 | - $idx = $offset ?? 0; |
|
58 | - $datalen = strlen($data); |
|
59 | - if ($idx >= $datalen) { |
|
60 | - throw new DecodeException( |
|
61 | - 'Unexpected end of data while decoding length.'); |
|
62 | - } |
|
63 | - $indefinite = false; |
|
64 | - $byte = ord($data[$idx++]); |
|
65 | - // bits 7 to 1 |
|
66 | - $length = (0x7f & $byte); |
|
67 | - // long form |
|
68 | - if (0x80 & $byte) { |
|
69 | - if (!$length) { |
|
70 | - $indefinite = true; |
|
71 | - } else { |
|
72 | - if ($idx + $length > $datalen) { |
|
73 | - throw new DecodeException( |
|
74 | - 'Unexpected end of data while decoding long form length.'); |
|
75 | - } |
|
76 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | - } |
|
78 | - } |
|
79 | - if (isset($offset)) { |
|
80 | - $offset = $idx; |
|
81 | - } |
|
82 | - return new self($length, $indefinite); |
|
83 | - } |
|
42 | + /** |
|
43 | + * Decode length component from DER data. |
|
44 | + * |
|
45 | + * @param string $data DER encoded data |
|
46 | + * @param null|int $offset Reference to the variable that contains offset |
|
47 | + * into the data where to start parsing. |
|
48 | + * Variable is updated to the offset next to the |
|
49 | + * parsed length component. If null, start from offset 0. |
|
50 | + * |
|
51 | + * @throws DecodeException If decoding fails |
|
52 | + * |
|
53 | + * @return self |
|
54 | + */ |
|
55 | + public static function fromDER(string $data, int &$offset = null): self |
|
56 | + { |
|
57 | + $idx = $offset ?? 0; |
|
58 | + $datalen = strlen($data); |
|
59 | + if ($idx >= $datalen) { |
|
60 | + throw new DecodeException( |
|
61 | + 'Unexpected end of data while decoding length.'); |
|
62 | + } |
|
63 | + $indefinite = false; |
|
64 | + $byte = ord($data[$idx++]); |
|
65 | + // bits 7 to 1 |
|
66 | + $length = (0x7f & $byte); |
|
67 | + // long form |
|
68 | + if (0x80 & $byte) { |
|
69 | + if (!$length) { |
|
70 | + $indefinite = true; |
|
71 | + } else { |
|
72 | + if ($idx + $length > $datalen) { |
|
73 | + throw new DecodeException( |
|
74 | + 'Unexpected end of data while decoding long form length.'); |
|
75 | + } |
|
76 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
77 | + } |
|
78 | + } |
|
79 | + if (isset($offset)) { |
|
80 | + $offset = $idx; |
|
81 | + } |
|
82 | + return new self($length, $indefinite); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Decode length from DER. |
|
87 | - * |
|
88 | - * Throws an exception if length doesn't match with expected or if data |
|
89 | - * doesn't contain enough bytes. |
|
90 | - * |
|
91 | - * Requirement of definite length is relaxed contrary to the specification |
|
92 | - * (sect. 10.1). |
|
93 | - * |
|
94 | - * @see self::fromDER |
|
95 | - * |
|
96 | - * @param string $data DER data |
|
97 | - * @param int $offset Reference to the offset variable |
|
98 | - * @param null|int $expected Expected length, null to bypass checking |
|
99 | - * |
|
100 | - * @throws DecodeException If decoding or expectation fails |
|
101 | - * |
|
102 | - * @return self |
|
103 | - */ |
|
104 | - public static function expectFromDER(string $data, int &$offset, |
|
105 | - int $expected = null): self |
|
106 | - { |
|
107 | - $idx = $offset; |
|
108 | - $length = self::fromDER($data, $idx); |
|
109 | - // if certain length was expected |
|
110 | - if (isset($expected)) { |
|
111 | - if ($length->isIndefinite()) { |
|
112 | - throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | - $expected); |
|
114 | - } |
|
115 | - if ($expected !== $length->intLength()) { |
|
116 | - throw new DecodeException( |
|
117 | - sprintf('Expected length %d, got %d.', $expected, |
|
118 | - $length->intLength())); |
|
119 | - } |
|
120 | - } |
|
121 | - // check that enough data is available |
|
122 | - if (!$length->isIndefinite() && |
|
123 | - strlen($data) < $idx + $length->intLength()) { |
|
124 | - throw new DecodeException( |
|
125 | - sprintf('Length %d overflows data, %d bytes left.', |
|
126 | - $length->intLength(), strlen($data) - $idx)); |
|
127 | - } |
|
128 | - $offset = $idx; |
|
129 | - return $length; |
|
130 | - } |
|
85 | + /** |
|
86 | + * Decode length from DER. |
|
87 | + * |
|
88 | + * Throws an exception if length doesn't match with expected or if data |
|
89 | + * doesn't contain enough bytes. |
|
90 | + * |
|
91 | + * Requirement of definite length is relaxed contrary to the specification |
|
92 | + * (sect. 10.1). |
|
93 | + * |
|
94 | + * @see self::fromDER |
|
95 | + * |
|
96 | + * @param string $data DER data |
|
97 | + * @param int $offset Reference to the offset variable |
|
98 | + * @param null|int $expected Expected length, null to bypass checking |
|
99 | + * |
|
100 | + * @throws DecodeException If decoding or expectation fails |
|
101 | + * |
|
102 | + * @return self |
|
103 | + */ |
|
104 | + public static function expectFromDER(string $data, int &$offset, |
|
105 | + int $expected = null): self |
|
106 | + { |
|
107 | + $idx = $offset; |
|
108 | + $length = self::fromDER($data, $idx); |
|
109 | + // if certain length was expected |
|
110 | + if (isset($expected)) { |
|
111 | + if ($length->isIndefinite()) { |
|
112 | + throw new DecodeException('Expected length %d, got indefinite.', |
|
113 | + $expected); |
|
114 | + } |
|
115 | + if ($expected !== $length->intLength()) { |
|
116 | + throw new DecodeException( |
|
117 | + sprintf('Expected length %d, got %d.', $expected, |
|
118 | + $length->intLength())); |
|
119 | + } |
|
120 | + } |
|
121 | + // check that enough data is available |
|
122 | + if (!$length->isIndefinite() && |
|
123 | + strlen($data) < $idx + $length->intLength()) { |
|
124 | + throw new DecodeException( |
|
125 | + sprintf('Length %d overflows data, %d bytes left.', |
|
126 | + $length->intLength(), strlen($data) - $idx)); |
|
127 | + } |
|
128 | + $offset = $idx; |
|
129 | + return $length; |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * @see Encodable::toDER() |
|
134 | - * |
|
135 | - * @throws \DomainException If length is too large to encode |
|
136 | - * |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function toDER(): string |
|
140 | - { |
|
141 | - $bytes = []; |
|
142 | - if ($this->_indefinite) { |
|
143 | - $bytes[] = 0x80; |
|
144 | - } else { |
|
145 | - $num = $this->_length->gmpObj(); |
|
146 | - // long form |
|
147 | - if ($num > 127) { |
|
148 | - $octets = []; |
|
149 | - for (; $num > 0; $num >>= 8) { |
|
150 | - $octets[] = gmp_intval(0xff & $num); |
|
151 | - } |
|
152 | - $count = count($octets); |
|
153 | - // first octet must not be 0xff |
|
154 | - if ($count >= 127) { |
|
155 | - throw new \DomainException('Too many length octets.'); |
|
156 | - } |
|
157 | - $bytes[] = 0x80 | $count; |
|
158 | - foreach (array_reverse($octets) as $octet) { |
|
159 | - $bytes[] = $octet; |
|
160 | - } |
|
161 | - } |
|
162 | - // short form |
|
163 | - else { |
|
164 | - $bytes[] = gmp_intval($num); |
|
165 | - } |
|
166 | - } |
|
167 | - return pack('C*', ...$bytes); |
|
168 | - } |
|
132 | + /** |
|
133 | + * @see Encodable::toDER() |
|
134 | + * |
|
135 | + * @throws \DomainException If length is too large to encode |
|
136 | + * |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function toDER(): string |
|
140 | + { |
|
141 | + $bytes = []; |
|
142 | + if ($this->_indefinite) { |
|
143 | + $bytes[] = 0x80; |
|
144 | + } else { |
|
145 | + $num = $this->_length->gmpObj(); |
|
146 | + // long form |
|
147 | + if ($num > 127) { |
|
148 | + $octets = []; |
|
149 | + for (; $num > 0; $num >>= 8) { |
|
150 | + $octets[] = gmp_intval(0xff & $num); |
|
151 | + } |
|
152 | + $count = count($octets); |
|
153 | + // first octet must not be 0xff |
|
154 | + if ($count >= 127) { |
|
155 | + throw new \DomainException('Too many length octets.'); |
|
156 | + } |
|
157 | + $bytes[] = 0x80 | $count; |
|
158 | + foreach (array_reverse($octets) as $octet) { |
|
159 | + $bytes[] = $octet; |
|
160 | + } |
|
161 | + } |
|
162 | + // short form |
|
163 | + else { |
|
164 | + $bytes[] = gmp_intval($num); |
|
165 | + } |
|
166 | + } |
|
167 | + return pack('C*', ...$bytes); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Get the length. |
|
172 | - * |
|
173 | - * @throws \LogicException If length is indefinite |
|
174 | - * |
|
175 | - * @return string Length as an integer string |
|
176 | - */ |
|
177 | - public function length(): string |
|
178 | - { |
|
179 | - if ($this->_indefinite) { |
|
180 | - throw new \LogicException('Length is indefinite.'); |
|
181 | - } |
|
182 | - return $this->_length->base10(); |
|
183 | - } |
|
170 | + /** |
|
171 | + * Get the length. |
|
172 | + * |
|
173 | + * @throws \LogicException If length is indefinite |
|
174 | + * |
|
175 | + * @return string Length as an integer string |
|
176 | + */ |
|
177 | + public function length(): string |
|
178 | + { |
|
179 | + if ($this->_indefinite) { |
|
180 | + throw new \LogicException('Length is indefinite.'); |
|
181 | + } |
|
182 | + return $this->_length->base10(); |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * Get the length as an integer. |
|
187 | - * |
|
188 | - * @throws \LogicException If length is indefinite |
|
189 | - * @throws \RuntimeException If length overflows integer size |
|
190 | - * |
|
191 | - * @return int |
|
192 | - */ |
|
193 | - public function intLength(): int |
|
194 | - { |
|
195 | - if ($this->_indefinite) { |
|
196 | - throw new \LogicException('Length is indefinite.'); |
|
197 | - } |
|
198 | - return $this->_length->intVal(); |
|
199 | - } |
|
185 | + /** |
|
186 | + * Get the length as an integer. |
|
187 | + * |
|
188 | + * @throws \LogicException If length is indefinite |
|
189 | + * @throws \RuntimeException If length overflows integer size |
|
190 | + * |
|
191 | + * @return int |
|
192 | + */ |
|
193 | + public function intLength(): int |
|
194 | + { |
|
195 | + if ($this->_indefinite) { |
|
196 | + throw new \LogicException('Length is indefinite.'); |
|
197 | + } |
|
198 | + return $this->_length->intVal(); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Whether length is indefinite. |
|
203 | - * |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - public function isIndefinite(): bool |
|
207 | - { |
|
208 | - return $this->_indefinite; |
|
209 | - } |
|
201 | + /** |
|
202 | + * Whether length is indefinite. |
|
203 | + * |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + public function isIndefinite(): bool |
|
207 | + { |
|
208 | + return $this->_indefinite; |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Decode long form length. |
|
213 | - * |
|
214 | - * @param int $length Number of octets |
|
215 | - * @param string $data Data |
|
216 | - * @param int $offset reference to the variable containing offset to the |
|
217 | - * data |
|
218 | - * |
|
219 | - * @throws DecodeException If decoding fails |
|
220 | - * |
|
221 | - * @return string Integer as a string |
|
222 | - */ |
|
223 | - private static function _decodeLongFormLength(int $length, string $data, |
|
224 | - int &$offset): string |
|
225 | - { |
|
226 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
227 | - if (127 == $length) { |
|
228 | - throw new DecodeException('Invalid number of length octets.'); |
|
229 | - } |
|
230 | - $num = gmp_init(0, 10); |
|
231 | - while (--$length >= 0) { |
|
232 | - $byte = ord($data[$offset++]); |
|
233 | - $num <<= 8; |
|
234 | - $num |= $byte; |
|
235 | - } |
|
236 | - return gmp_strval($num); |
|
237 | - } |
|
211 | + /** |
|
212 | + * Decode long form length. |
|
213 | + * |
|
214 | + * @param int $length Number of octets |
|
215 | + * @param string $data Data |
|
216 | + * @param int $offset reference to the variable containing offset to the |
|
217 | + * data |
|
218 | + * |
|
219 | + * @throws DecodeException If decoding fails |
|
220 | + * |
|
221 | + * @return string Integer as a string |
|
222 | + */ |
|
223 | + private static function _decodeLongFormLength(int $length, string $data, |
|
224 | + int &$offset): string |
|
225 | + { |
|
226 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
227 | + if (127 == $length) { |
|
228 | + throw new DecodeException('Invalid number of length octets.'); |
|
229 | + } |
|
230 | + $num = gmp_init(0, 10); |
|
231 | + while (--$length >= 0) { |
|
232 | + $byte = ord($data[$offset++]); |
|
233 | + $num <<= 8; |
|
234 | + $num |= $byte; |
|
235 | + } |
|
236 | + return gmp_strval($num); |
|
237 | + } |
|
238 | 238 | } |
@@ -13,91 +13,91 @@ |
||
13 | 13 | */ |
14 | 14 | interface ElementBase extends Encodable |
15 | 15 | { |
16 | - /** |
|
17 | - * Get the class of the ASN.1 type. |
|
18 | - * |
|
19 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
20 | - * |
|
21 | - * @return int |
|
22 | - */ |
|
23 | - public function typeClass(): int; |
|
16 | + /** |
|
17 | + * Get the class of the ASN.1 type. |
|
18 | + * |
|
19 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
20 | + * |
|
21 | + * @return int |
|
22 | + */ |
|
23 | + public function typeClass(): int; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Check whether the element is constructed. |
|
27 | - * |
|
28 | - * Otherwise it's primitive. |
|
29 | - * |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function isConstructed(): bool; |
|
25 | + /** |
|
26 | + * Check whether the element is constructed. |
|
27 | + * |
|
28 | + * Otherwise it's primitive. |
|
29 | + * |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function isConstructed(): bool; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the tag of the element. |
|
36 | - * |
|
37 | - * Interpretation of the tag depends on the context. For example it may |
|
38 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | - * tagged type. |
|
40 | - * |
|
41 | - * @return int |
|
42 | - */ |
|
43 | - public function tag(): int; |
|
34 | + /** |
|
35 | + * Get the tag of the element. |
|
36 | + * |
|
37 | + * Interpretation of the tag depends on the context. For example it may |
|
38 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
39 | + * tagged type. |
|
40 | + * |
|
41 | + * @return int |
|
42 | + */ |
|
43 | + public function tag(): int; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Check whether the element is a type of a given tag. |
|
47 | - * |
|
48 | - * @param int $tag Type tag |
|
49 | - * |
|
50 | - * @return bool |
|
51 | - */ |
|
52 | - public function isType(int $tag): bool; |
|
45 | + /** |
|
46 | + * Check whether the element is a type of a given tag. |
|
47 | + * |
|
48 | + * @param int $tag Type tag |
|
49 | + * |
|
50 | + * @return bool |
|
51 | + */ |
|
52 | + public function isType(int $tag): bool; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Check whether the element is a type of a given tag. |
|
56 | - * |
|
57 | - * Throws an exception if expectation fails. |
|
58 | - * |
|
59 | - * @param int $tag Type tag |
|
60 | - * |
|
61 | - * @throws \UnexpectedValueException If the element type differs from the |
|
62 | - * expected |
|
63 | - * |
|
64 | - * @return ElementBase |
|
65 | - */ |
|
66 | - public function expectType(int $tag): ElementBase; |
|
54 | + /** |
|
55 | + * Check whether the element is a type of a given tag. |
|
56 | + * |
|
57 | + * Throws an exception if expectation fails. |
|
58 | + * |
|
59 | + * @param int $tag Type tag |
|
60 | + * |
|
61 | + * @throws \UnexpectedValueException If the element type differs from the |
|
62 | + * expected |
|
63 | + * |
|
64 | + * @return ElementBase |
|
65 | + */ |
|
66 | + public function expectType(int $tag): ElementBase; |
|
67 | 67 | |
68 | - /** |
|
69 | - * Check whether the element is tagged (context specific). |
|
70 | - * |
|
71 | - * @return bool |
|
72 | - */ |
|
73 | - public function isTagged(): bool; |
|
68 | + /** |
|
69 | + * Check whether the element is tagged (context specific). |
|
70 | + * |
|
71 | + * @return bool |
|
72 | + */ |
|
73 | + public function isTagged(): bool; |
|
74 | 74 | |
75 | - /** |
|
76 | - * Check whether the element is tagged (context specific) and optionally has |
|
77 | - * a given tag. |
|
78 | - * |
|
79 | - * Throws an exception if the element is not tagged or tag differs from |
|
80 | - * the expected. |
|
81 | - * |
|
82 | - * @param null|int $tag Optional type tag |
|
83 | - * |
|
84 | - * @throws \UnexpectedValueException If expectation fails |
|
85 | - * |
|
86 | - * @return TaggedType |
|
87 | - */ |
|
88 | - public function expectTagged(?int $tag = null): TaggedType; |
|
75 | + /** |
|
76 | + * Check whether the element is tagged (context specific) and optionally has |
|
77 | + * a given tag. |
|
78 | + * |
|
79 | + * Throws an exception if the element is not tagged or tag differs from |
|
80 | + * the expected. |
|
81 | + * |
|
82 | + * @param null|int $tag Optional type tag |
|
83 | + * |
|
84 | + * @throws \UnexpectedValueException If expectation fails |
|
85 | + * |
|
86 | + * @return TaggedType |
|
87 | + */ |
|
88 | + public function expectTagged(?int $tag = null): TaggedType; |
|
89 | 89 | |
90 | - /** |
|
91 | - * Get the object as an abstract Element instance. |
|
92 | - * |
|
93 | - * @return Element |
|
94 | - */ |
|
95 | - public function asElement(): Element; |
|
90 | + /** |
|
91 | + * Get the object as an abstract Element instance. |
|
92 | + * |
|
93 | + * @return Element |
|
94 | + */ |
|
95 | + public function asElement(): Element; |
|
96 | 96 | |
97 | - /** |
|
98 | - * Get the object as an UnspecifiedType instance. |
|
99 | - * |
|
100 | - * @return UnspecifiedType |
|
101 | - */ |
|
102 | - public function asUnspecified(): UnspecifiedType; |
|
97 | + /** |
|
98 | + * Get the object as an UnspecifiedType instance. |
|
99 | + * |
|
100 | + * @return UnspecifiedType |
|
101 | + */ |
|
102 | + public function asUnspecified(): UnspecifiedType; |
|
103 | 103 | } |
@@ -17,276 +17,276 @@ |
||
17 | 17 | */ |
18 | 18 | class Real extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
23 | + /** |
|
24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Regex pattern to parse PHP exponent number format. |
|
32 | - * |
|
33 | - * @see http://php.net/manual/en/language.types.float.php |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - const PHP_EXPONENT_DNUM = '/^' . |
|
38 | - '([+\-]?' . // sign |
|
39 | - '(?:' . |
|
40 | - '\d+' . // LNUM |
|
41 | - '|' . |
|
42 | - '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
43 | - '))[eE]' . |
|
44 | - '([+\-]?\d+)' . // exponent |
|
45 | - '$/'; |
|
30 | + /** |
|
31 | + * Regex pattern to parse PHP exponent number format. |
|
32 | + * |
|
33 | + * @see http://php.net/manual/en/language.types.float.php |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + const PHP_EXPONENT_DNUM = '/^' . |
|
38 | + '([+\-]?' . // sign |
|
39 | + '(?:' . |
|
40 | + '\d+' . // LNUM |
|
41 | + '|' . |
|
42 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
43 | + '))[eE]' . |
|
44 | + '([+\-]?\d+)' . // exponent |
|
45 | + '$/'; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Number zero represented in NR3 form. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - const NR3_ZERO = '.E+0'; |
|
47 | + /** |
|
48 | + * Number zero represented in NR3 form. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + const NR3_ZERO = '.E+0'; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Number in NR3 form. |
|
56 | - * |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - private $_number; |
|
54 | + /** |
|
55 | + * Number in NR3 form. |
|
56 | + * |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + private $_number; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param string $number number in NR3 form |
|
65 | - */ |
|
66 | - public function __construct(string $number) |
|
67 | - { |
|
68 | - $this->_typeTag = self::TYPE_REAL; |
|
69 | - if (!self::_validateNumber($number)) { |
|
70 | - throw new \InvalidArgumentException( |
|
71 | - "'${number}' is not a valid NR3 form real."); |
|
72 | - } |
|
73 | - $this->_number = $number; |
|
74 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param string $number number in NR3 form |
|
65 | + */ |
|
66 | + public function __construct(string $number) |
|
67 | + { |
|
68 | + $this->_typeTag = self::TYPE_REAL; |
|
69 | + if (!self::_validateNumber($number)) { |
|
70 | + throw new \InvalidArgumentException( |
|
71 | + "'${number}' is not a valid NR3 form real."); |
|
72 | + } |
|
73 | + $this->_number = $number; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Initialize from float. |
|
78 | - * |
|
79 | - * @param float $number |
|
80 | - * |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - public static function fromFloat(float $number): self |
|
84 | - { |
|
85 | - return new self(self::_decimalToNR3(strval($number))); |
|
86 | - } |
|
76 | + /** |
|
77 | + * Initialize from float. |
|
78 | + * |
|
79 | + * @param float $number |
|
80 | + * |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + public static function fromFloat(float $number): self |
|
84 | + { |
|
85 | + return new self(self::_decimalToNR3(strval($number))); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get number as a float. |
|
90 | - * |
|
91 | - * @return float |
|
92 | - */ |
|
93 | - public function float(): float |
|
94 | - { |
|
95 | - return self::_nr3ToDecimal($this->_number); |
|
96 | - } |
|
88 | + /** |
|
89 | + * Get number as a float. |
|
90 | + * |
|
91 | + * @return float |
|
92 | + */ |
|
93 | + public function float(): float |
|
94 | + { |
|
95 | + return self::_nr3ToDecimal($this->_number); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * {@inheritdoc} |
|
100 | - */ |
|
101 | - protected function _encodedContentDER(): string |
|
102 | - { |
|
103 | - /* if the real value is the value zero, there shall be no contents |
|
98 | + /** |
|
99 | + * {@inheritdoc} |
|
100 | + */ |
|
101 | + protected function _encodedContentDER(): string |
|
102 | + { |
|
103 | + /* if the real value is the value zero, there shall be no contents |
|
104 | 104 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
105 | - if (self::NR3_ZERO == $this->_number) { |
|
106 | - return ''; |
|
107 | - } |
|
108 | - // encode in NR3 decimal encoding |
|
109 | - return chr(0x03) . $this->_number; |
|
110 | - } |
|
105 | + if (self::NR3_ZERO == $this->_number) { |
|
106 | + return ''; |
|
107 | + } |
|
108 | + // encode in NR3 decimal encoding |
|
109 | + return chr(0x03) . $this->_number; |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * {@inheritdoc} |
|
114 | - */ |
|
115 | - protected static function _decodeFromDER(Identifier $identifier, |
|
116 | - string $data, int &$offset): ElementBase |
|
117 | - { |
|
118 | - $idx = $offset; |
|
119 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
120 | - // if length is zero, value is zero (spec 8.5.2) |
|
121 | - if (!$length) { |
|
122 | - $obj = new self(self::NR3_ZERO); |
|
123 | - } else { |
|
124 | - $bytes = substr($data, $idx, $length); |
|
125 | - $byte = ord($bytes[0]); |
|
126 | - if (0x80 & $byte) { // bit 8 = 1 |
|
127 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
128 | - } elseif (0x00 == $byte >> 6) { // bit 8 = 0, bit 7 = 0 |
|
129 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
130 | - } else { // bit 8 = 0, bit 7 = 1 |
|
131 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
132 | - } |
|
133 | - } |
|
134 | - $offset = $idx + $length; |
|
135 | - return $obj; |
|
136 | - } |
|
112 | + /** |
|
113 | + * {@inheritdoc} |
|
114 | + */ |
|
115 | + protected static function _decodeFromDER(Identifier $identifier, |
|
116 | + string $data, int &$offset): ElementBase |
|
117 | + { |
|
118 | + $idx = $offset; |
|
119 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
120 | + // if length is zero, value is zero (spec 8.5.2) |
|
121 | + if (!$length) { |
|
122 | + $obj = new self(self::NR3_ZERO); |
|
123 | + } else { |
|
124 | + $bytes = substr($data, $idx, $length); |
|
125 | + $byte = ord($bytes[0]); |
|
126 | + if (0x80 & $byte) { // bit 8 = 1 |
|
127 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
128 | + } elseif (0x00 == $byte >> 6) { // bit 8 = 0, bit 7 = 0 |
|
129 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
130 | + } else { // bit 8 = 0, bit 7 = 1 |
|
131 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
132 | + } |
|
133 | + } |
|
134 | + $offset = $idx + $length; |
|
135 | + return $obj; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @todo Implement |
|
140 | - * |
|
141 | - * @param string $data |
|
142 | - */ |
|
143 | - protected static function _decodeBinaryEncoding(string $data) |
|
144 | - { |
|
145 | - throw new \RuntimeException( |
|
146 | - 'Binary encoding of REAL is not implemented.'); |
|
147 | - } |
|
138 | + /** |
|
139 | + * @todo Implement |
|
140 | + * |
|
141 | + * @param string $data |
|
142 | + */ |
|
143 | + protected static function _decodeBinaryEncoding(string $data) |
|
144 | + { |
|
145 | + throw new \RuntimeException( |
|
146 | + 'Binary encoding of REAL is not implemented.'); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @param string $data |
|
151 | - * |
|
152 | - * @throws \RuntimeException |
|
153 | - * |
|
154 | - * @return self |
|
155 | - */ |
|
156 | - protected static function _decodeDecimalEncoding(string $data): self |
|
157 | - { |
|
158 | - $nr = ord($data[0]) & 0x03; |
|
159 | - if (0x03 != $nr) { |
|
160 | - throw new \RuntimeException('Only NR3 form supported.'); |
|
161 | - } |
|
162 | - $str = substr($data, 1); |
|
163 | - return new self($str); |
|
164 | - } |
|
149 | + /** |
|
150 | + * @param string $data |
|
151 | + * |
|
152 | + * @throws \RuntimeException |
|
153 | + * |
|
154 | + * @return self |
|
155 | + */ |
|
156 | + protected static function _decodeDecimalEncoding(string $data): self |
|
157 | + { |
|
158 | + $nr = ord($data[0]) & 0x03; |
|
159 | + if (0x03 != $nr) { |
|
160 | + throw new \RuntimeException('Only NR3 form supported.'); |
|
161 | + } |
|
162 | + $str = substr($data, 1); |
|
163 | + return new self($str); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @todo Implement |
|
168 | - * |
|
169 | - * @param string $data |
|
170 | - */ |
|
171 | - protected static function _decodeSpecialRealValue(string $data) |
|
172 | - { |
|
173 | - if (1 != strlen($data)) { |
|
174 | - throw new DecodeException( |
|
175 | - 'SpecialRealValue must have one content octet.'); |
|
176 | - } |
|
177 | - $byte = ord($data[0]); |
|
178 | - if (0x40 == $byte) { // positive infinity |
|
179 | - throw new \RuntimeException('PLUS-INFINITY not supported.'); |
|
180 | - } |
|
181 | - if (0x41 == $byte) { // negative infinity |
|
182 | - throw new \RuntimeException('MINUS-INFINITY not supported.'); |
|
183 | - } |
|
184 | - throw new DecodeException('Invalid SpecialRealValue encoding.'); |
|
185 | - } |
|
166 | + /** |
|
167 | + * @todo Implement |
|
168 | + * |
|
169 | + * @param string $data |
|
170 | + */ |
|
171 | + protected static function _decodeSpecialRealValue(string $data) |
|
172 | + { |
|
173 | + if (1 != strlen($data)) { |
|
174 | + throw new DecodeException( |
|
175 | + 'SpecialRealValue must have one content octet.'); |
|
176 | + } |
|
177 | + $byte = ord($data[0]); |
|
178 | + if (0x40 == $byte) { // positive infinity |
|
179 | + throw new \RuntimeException('PLUS-INFINITY not supported.'); |
|
180 | + } |
|
181 | + if (0x41 == $byte) { // negative infinity |
|
182 | + throw new \RuntimeException('MINUS-INFINITY not supported.'); |
|
183 | + } |
|
184 | + throw new DecodeException('Invalid SpecialRealValue encoding.'); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Convert decimal number string to NR3 form. |
|
189 | - * |
|
190 | - * @param string $str |
|
191 | - * |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - private static function _decimalToNR3(string $str): string |
|
195 | - { |
|
196 | - // if number is in exponent form |
|
197 | - /** @var string[] $match */ |
|
198 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
199 | - $parts = explode('.', $match[1]); |
|
200 | - $m = ltrim($parts[0], '0'); |
|
201 | - $e = intval($match[2]); |
|
202 | - // if mantissa had decimals |
|
203 | - if (2 == count($parts)) { |
|
204 | - $d = rtrim($parts[1], '0'); |
|
205 | - $e -= strlen($d); |
|
206 | - $m .= $d; |
|
207 | - } |
|
208 | - } else { |
|
209 | - // explode from decimal |
|
210 | - $parts = explode('.', $str); |
|
211 | - $m = ltrim($parts[0], '0'); |
|
212 | - // if number had decimals |
|
213 | - if (2 == count($parts)) { |
|
214 | - // exponent is negative number of the decimals |
|
215 | - $e = -strlen($parts[1]); |
|
216 | - // append decimals to the mantissa |
|
217 | - $m .= $parts[1]; |
|
218 | - } else { |
|
219 | - $e = 0; |
|
220 | - } |
|
221 | - // shift trailing zeroes from the mantissa to the exponent |
|
222 | - while ('0' === substr($m, -1)) { |
|
223 | - ++$e; |
|
224 | - $m = substr($m, 0, -1); |
|
225 | - } |
|
226 | - } |
|
227 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
187 | + /** |
|
188 | + * Convert decimal number string to NR3 form. |
|
189 | + * |
|
190 | + * @param string $str |
|
191 | + * |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + private static function _decimalToNR3(string $str): string |
|
195 | + { |
|
196 | + // if number is in exponent form |
|
197 | + /** @var string[] $match */ |
|
198 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
199 | + $parts = explode('.', $match[1]); |
|
200 | + $m = ltrim($parts[0], '0'); |
|
201 | + $e = intval($match[2]); |
|
202 | + // if mantissa had decimals |
|
203 | + if (2 == count($parts)) { |
|
204 | + $d = rtrim($parts[1], '0'); |
|
205 | + $e -= strlen($d); |
|
206 | + $m .= $d; |
|
207 | + } |
|
208 | + } else { |
|
209 | + // explode from decimal |
|
210 | + $parts = explode('.', $str); |
|
211 | + $m = ltrim($parts[0], '0'); |
|
212 | + // if number had decimals |
|
213 | + if (2 == count($parts)) { |
|
214 | + // exponent is negative number of the decimals |
|
215 | + $e = -strlen($parts[1]); |
|
216 | + // append decimals to the mantissa |
|
217 | + $m .= $parts[1]; |
|
218 | + } else { |
|
219 | + $e = 0; |
|
220 | + } |
|
221 | + // shift trailing zeroes from the mantissa to the exponent |
|
222 | + while ('0' === substr($m, -1)) { |
|
223 | + ++$e; |
|
224 | + $m = substr($m, 0, -1); |
|
225 | + } |
|
226 | + } |
|
227 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
228 | 228 | (X.690 07-2002, section 11.3.2.6) */ |
229 | - if (0 == $e) { |
|
230 | - $es = '+'; |
|
231 | - } else { |
|
232 | - $es = $e < 0 ? '-' : ''; |
|
233 | - } |
|
234 | - return sprintf('%s.E%s%d', $m, $es, abs($e)); |
|
235 | - } |
|
229 | + if (0 == $e) { |
|
230 | + $es = '+'; |
|
231 | + } else { |
|
232 | + $es = $e < 0 ? '-' : ''; |
|
233 | + } |
|
234 | + return sprintf('%s.E%s%d', $m, $es, abs($e)); |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Convert NR3 form number to decimal. |
|
239 | - * |
|
240 | - * @param string $str |
|
241 | - * |
|
242 | - * @throws \UnexpectedValueException |
|
243 | - * |
|
244 | - * @return float |
|
245 | - */ |
|
246 | - private static function _nr3ToDecimal(string $str): float |
|
247 | - { |
|
248 | - /** @var string[] $match */ |
|
249 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
250 | - throw new \UnexpectedValueException( |
|
251 | - "'${str}' is not a valid NR3 form real."); |
|
252 | - } |
|
253 | - $m = $match[2]; |
|
254 | - // if number started with minus sign |
|
255 | - $inv = '-' == $match[1]; |
|
256 | - $e = intval($match[3]); |
|
257 | - // positive exponent |
|
258 | - if ($e > 0) { |
|
259 | - // pad with trailing zeroes |
|
260 | - $num = $m . str_repeat('0', $e); |
|
261 | - } elseif ($e < 0) { |
|
262 | - // pad with leading zeroes |
|
263 | - if (strlen($m) < abs($e)) { |
|
264 | - $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; |
|
265 | - } |
|
266 | - // insert decimal point |
|
267 | - $num = substr($m, 0, $e) . '.' . substr($m, $e); |
|
268 | - } else { |
|
269 | - $num = empty($m) ? '0' : $m; |
|
270 | - } |
|
271 | - // if number is negative |
|
272 | - if ($inv) { |
|
273 | - $num = "-${num}"; |
|
274 | - } |
|
275 | - return floatval($num); |
|
276 | - } |
|
237 | + /** |
|
238 | + * Convert NR3 form number to decimal. |
|
239 | + * |
|
240 | + * @param string $str |
|
241 | + * |
|
242 | + * @throws \UnexpectedValueException |
|
243 | + * |
|
244 | + * @return float |
|
245 | + */ |
|
246 | + private static function _nr3ToDecimal(string $str): float |
|
247 | + { |
|
248 | + /** @var string[] $match */ |
|
249 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
250 | + throw new \UnexpectedValueException( |
|
251 | + "'${str}' is not a valid NR3 form real."); |
|
252 | + } |
|
253 | + $m = $match[2]; |
|
254 | + // if number started with minus sign |
|
255 | + $inv = '-' == $match[1]; |
|
256 | + $e = intval($match[3]); |
|
257 | + // positive exponent |
|
258 | + if ($e > 0) { |
|
259 | + // pad with trailing zeroes |
|
260 | + $num = $m . str_repeat('0', $e); |
|
261 | + } elseif ($e < 0) { |
|
262 | + // pad with leading zeroes |
|
263 | + if (strlen($m) < abs($e)) { |
|
264 | + $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; |
|
265 | + } |
|
266 | + // insert decimal point |
|
267 | + $num = substr($m, 0, $e) . '.' . substr($m, $e); |
|
268 | + } else { |
|
269 | + $num = empty($m) ? '0' : $m; |
|
270 | + } |
|
271 | + // if number is negative |
|
272 | + if ($inv) { |
|
273 | + $num = "-${num}"; |
|
274 | + } |
|
275 | + return floatval($num); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * Test that number is valid for this context. |
|
280 | - * |
|
281 | - * @param mixed $num |
|
282 | - * |
|
283 | - * @return bool |
|
284 | - */ |
|
285 | - private static function _validateNumber($num): bool |
|
286 | - { |
|
287 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
288 | - return false; |
|
289 | - } |
|
290 | - return true; |
|
291 | - } |
|
278 | + /** |
|
279 | + * Test that number is valid for this context. |
|
280 | + * |
|
281 | + * @param mixed $num |
|
282 | + * |
|
283 | + * @return bool |
|
284 | + */ |
|
285 | + private static function _validateNumber($num): bool |
|
286 | + { |
|
287 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
288 | + return false; |
|
289 | + } |
|
290 | + return true; |
|
291 | + } |
|
292 | 292 | } |
@@ -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 | } |