@@ -14,82 +14,82 @@ |
||
14 | 14 | */ |
15 | 15 | class DERData extends Element |
16 | 16 | { |
17 | - /** |
|
18 | - * DER encoded data. |
|
19 | - * |
|
20 | - * @var string $_der |
|
21 | - */ |
|
22 | - protected $_der; |
|
17 | + /** |
|
18 | + * DER encoded data. |
|
19 | + * |
|
20 | + * @var string $_der |
|
21 | + */ |
|
22 | + protected $_der; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Identifier of the underlying type. |
|
26 | - * |
|
27 | - * @var Identifier $_identifier |
|
28 | - */ |
|
29 | - protected $_identifier; |
|
24 | + /** |
|
25 | + * Identifier of the underlying type. |
|
26 | + * |
|
27 | + * @var Identifier $_identifier |
|
28 | + */ |
|
29 | + protected $_identifier; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Offset to the content in DER data. |
|
33 | - * |
|
34 | - * @var int $_contentOffset |
|
35 | - */ |
|
36 | - protected $_contentOffset = 0; |
|
31 | + /** |
|
32 | + * Offset to the content in DER data. |
|
33 | + * |
|
34 | + * @var int $_contentOffset |
|
35 | + */ |
|
36 | + protected $_contentOffset = 0; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Constructor. |
|
40 | - * |
|
41 | - * @param string $data DER encoded data |
|
42 | - * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
43 | - */ |
|
44 | - public function __construct(string $data) |
|
45 | - { |
|
46 | - $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
47 | - Length::expectFromDER($data, $this->_contentOffset); |
|
48 | - $this->_der = $data; |
|
49 | - $this->_typeTag = $this->_identifier->intTag(); |
|
50 | - } |
|
38 | + /** |
|
39 | + * Constructor. |
|
40 | + * |
|
41 | + * @param string $data DER encoded data |
|
42 | + * @throws \ASN1\Exception\DecodeException If data does not adhere to DER |
|
43 | + */ |
|
44 | + public function __construct(string $data) |
|
45 | + { |
|
46 | + $this->_identifier = Identifier::fromDER($data, $this->_contentOffset); |
|
47 | + Length::expectFromDER($data, $this->_contentOffset); |
|
48 | + $this->_der = $data; |
|
49 | + $this->_typeTag = $this->_identifier->intTag(); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * |
|
54 | - * @see \ASN1\Element::typeClass() |
|
55 | - * @return int |
|
56 | - */ |
|
57 | - public function typeClass(): int |
|
58 | - { |
|
59 | - return $this->_identifier->typeClass(); |
|
60 | - } |
|
52 | + /** |
|
53 | + * |
|
54 | + * @see \ASN1\Element::typeClass() |
|
55 | + * @return int |
|
56 | + */ |
|
57 | + public function typeClass(): int |
|
58 | + { |
|
59 | + return $this->_identifier->typeClass(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * |
|
64 | - * @see \ASN1\Element::isConstructed() |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function isConstructed(): bool |
|
68 | - { |
|
69 | - return $this->_identifier->isConstructed(); |
|
70 | - } |
|
62 | + /** |
|
63 | + * |
|
64 | + * @see \ASN1\Element::isConstructed() |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function isConstructed(): bool |
|
68 | + { |
|
69 | + return $this->_identifier->isConstructed(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * |
|
74 | - * @see \ASN1\Element::_encodedContentDER() |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - protected function _encodedContentDER(): string |
|
78 | - { |
|
79 | - // if there's no content payload |
|
80 | - if (strlen($this->_der) == $this->_contentOffset) { |
|
81 | - return ""; |
|
82 | - } |
|
83 | - return substr($this->_der, $this->_contentOffset); |
|
84 | - } |
|
72 | + /** |
|
73 | + * |
|
74 | + * @see \ASN1\Element::_encodedContentDER() |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + protected function _encodedContentDER(): string |
|
78 | + { |
|
79 | + // if there's no content payload |
|
80 | + if (strlen($this->_der) == $this->_contentOffset) { |
|
81 | + return ""; |
|
82 | + } |
|
83 | + return substr($this->_der, $this->_contentOffset); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * |
|
88 | - * @see \ASN1\Element::toDER() |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function toDER(): string |
|
92 | - { |
|
93 | - return $this->_der; |
|
94 | - } |
|
86 | + /** |
|
87 | + * |
|
88 | + * @see \ASN1\Element::toDER() |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function toDER(): string |
|
92 | + { |
|
93 | + return $this->_der; |
|
94 | + } |
|
95 | 95 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1; |
6 | 6 |
@@ -13,13 +13,13 @@ |
||
13 | 13 | */ |
14 | 14 | interface ImplicitTagging extends ElementBase |
15 | 15 | { |
16 | - /** |
|
17 | - * Get implicitly tagged wrapped element. |
|
18 | - * |
|
19 | - * @param int $tag Tag of the element |
|
20 | - * @param int $class Expected type class of the element |
|
21 | - * @throws \UnexpectedValueException If expectation fails |
|
22 | - * @return \ASN1\Type\UnspecifiedType |
|
23 | - */ |
|
24 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
16 | + /** |
|
17 | + * Get implicitly tagged wrapped element. |
|
18 | + * |
|
19 | + * @param int $tag Tag of the element |
|
20 | + * @param int $class Expected type class of the element |
|
21 | + * @throws \UnexpectedValueException If expectation fails |
|
22 | + * @return \ASN1\Type\UnspecifiedType |
|
23 | + */ |
|
24 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
25 | 25 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |
@@ -17,58 +17,58 @@ |
||
17 | 17 | */ |
18 | 18 | class ImplicitlyTaggedType extends TaggedTypeWrap implements ImplicitTagging |
19 | 19 | { |
20 | - /** |
|
21 | - * Constructor. |
|
22 | - * |
|
23 | - * @param int $tag Tag number |
|
24 | - * @param Element $element Wrapped element |
|
25 | - * @param int $class Type class |
|
26 | - */ |
|
27 | - public function __construct(int $tag, Element $element, |
|
28 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | - { |
|
30 | - $this->_typeTag = $tag; |
|
31 | - $this->_element = $element; |
|
32 | - $this->_class = $class; |
|
33 | - } |
|
20 | + /** |
|
21 | + * Constructor. |
|
22 | + * |
|
23 | + * @param int $tag Tag number |
|
24 | + * @param Element $element Wrapped element |
|
25 | + * @param int $class Type class |
|
26 | + */ |
|
27 | + public function __construct(int $tag, Element $element, |
|
28 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | + { |
|
30 | + $this->_typeTag = $tag; |
|
31 | + $this->_element = $element; |
|
32 | + $this->_class = $class; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * |
|
37 | - * @see \ASN1\Element::isConstructed() |
|
38 | - * @return bool |
|
39 | - */ |
|
40 | - public function isConstructed(): bool |
|
41 | - { |
|
42 | - // depends on the underlying type |
|
43 | - return $this->_element->isConstructed(); |
|
44 | - } |
|
35 | + /** |
|
36 | + * |
|
37 | + * @see \ASN1\Element::isConstructed() |
|
38 | + * @return bool |
|
39 | + */ |
|
40 | + public function isConstructed(): bool |
|
41 | + { |
|
42 | + // depends on the underlying type |
|
43 | + return $this->_element->isConstructed(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * |
|
48 | - * @see \ASN1\Element::_encodedContentDER() |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - protected function _encodedContentDER(): string |
|
52 | - { |
|
53 | - // get only the content of the wrapped element. |
|
54 | - return $this->_element->_encodedContentDER(); |
|
55 | - } |
|
46 | + /** |
|
47 | + * |
|
48 | + * @see \ASN1\Element::_encodedContentDER() |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + protected function _encodedContentDER(): string |
|
52 | + { |
|
53 | + // get only the content of the wrapped element. |
|
54 | + return $this->_element->_encodedContentDER(); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * |
|
59 | - * {@inheritdoc} |
|
60 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
61 | - * @return UnspecifiedType |
|
62 | - */ |
|
63 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
64 | - { |
|
65 | - $this->_element->expectType($tag); |
|
66 | - if ($this->_element->typeClass() != $class) { |
|
67 | - throw new \UnexpectedValueException( |
|
68 | - sprintf("Type class %s expected, got %s.", |
|
69 | - Identifier::classToName($class), |
|
70 | - Identifier::classToName($this->_element->typeClass()))); |
|
71 | - } |
|
72 | - return $this->_element->asUnspecified(); |
|
73 | - } |
|
57 | + /** |
|
58 | + * |
|
59 | + * {@inheritdoc} |
|
60 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
61 | + * @return UnspecifiedType |
|
62 | + */ |
|
63 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
64 | + { |
|
65 | + $this->_element->expectType($tag); |
|
66 | + if ($this->_element->typeClass() != $class) { |
|
67 | + throw new \UnexpectedValueException( |
|
68 | + sprintf("Type class %s expected, got %s.", |
|
69 | + Identifier::classToName($class), |
|
70 | + Identifier::classToName($this->_element->typeClass()))); |
|
71 | + } |
|
72 | + return $this->_element->asUnspecified(); |
|
73 | + } |
|
74 | 74 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |
@@ -16,53 +16,53 @@ |
||
16 | 16 | */ |
17 | 17 | class ExplicitlyTaggedType extends TaggedTypeWrap implements ExplicitTagging |
18 | 18 | { |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param int $tag Tag number |
|
23 | - * @param Element $element Wrapped element |
|
24 | - * @param int $class Type class |
|
25 | - */ |
|
26 | - public function __construct(int $tag, Element $element, |
|
27 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
28 | - { |
|
29 | - $this->_typeTag = $tag; |
|
30 | - $this->_element = $element; |
|
31 | - $this->_class = $class; |
|
32 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param int $tag Tag number |
|
23 | + * @param Element $element Wrapped element |
|
24 | + * @param int $class Type class |
|
25 | + */ |
|
26 | + public function __construct(int $tag, Element $element, |
|
27 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
28 | + { |
|
29 | + $this->_typeTag = $tag; |
|
30 | + $this->_element = $element; |
|
31 | + $this->_class = $class; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * |
|
36 | - * @see \ASN1\Element::isConstructed() |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function isConstructed(): bool |
|
40 | - { |
|
41 | - return true; |
|
42 | - } |
|
34 | + /** |
|
35 | + * |
|
36 | + * @see \ASN1\Element::isConstructed() |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function isConstructed(): bool |
|
40 | + { |
|
41 | + return true; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * |
|
46 | - * @see \ASN1\Element::_encodedContentDER() |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - protected function _encodedContentDER(): string |
|
50 | - { |
|
51 | - // get the full encoding of the wrapped element |
|
52 | - return $this->_element->toDER(); |
|
53 | - } |
|
44 | + /** |
|
45 | + * |
|
46 | + * @see \ASN1\Element::_encodedContentDER() |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + protected function _encodedContentDER(): string |
|
50 | + { |
|
51 | + // get the full encoding of the wrapped element |
|
52 | + return $this->_element->toDER(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * |
|
57 | - * {@inheritdoc} |
|
58 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
59 | - * @return UnspecifiedType |
|
60 | - */ |
|
61 | - public function explicit($expectedTag = null): UnspecifiedType |
|
62 | - { |
|
63 | - if (isset($expectedTag)) { |
|
64 | - $this->_element->expectType($expectedTag); |
|
65 | - } |
|
66 | - return $this->_element->asUnspecified(); |
|
67 | - } |
|
55 | + /** |
|
56 | + * |
|
57 | + * {@inheritdoc} |
|
58 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
59 | + * @return UnspecifiedType |
|
60 | + */ |
|
61 | + public function explicit($expectedTag = null): UnspecifiedType |
|
62 | + { |
|
63 | + if (isset($expectedTag)) { |
|
64 | + $this->_element->expectType($expectedTag); |
|
65 | + } |
|
66 | + return $this->_element->asUnspecified(); |
|
67 | + } |
|
68 | 68 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |
@@ -11,27 +11,27 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class TaggedTypeWrap extends TaggedType |
13 | 13 | { |
14 | - /** |
|
15 | - * Wrapped element. |
|
16 | - * |
|
17 | - * @var \ASN1\Element $_element |
|
18 | - */ |
|
19 | - protected $_element; |
|
14 | + /** |
|
15 | + * Wrapped element. |
|
16 | + * |
|
17 | + * @var \ASN1\Element $_element |
|
18 | + */ |
|
19 | + protected $_element; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Type class. |
|
23 | - * |
|
24 | - * @var int |
|
25 | - */ |
|
26 | - protected $_class; |
|
21 | + /** |
|
22 | + * Type class. |
|
23 | + * |
|
24 | + * @var int |
|
25 | + */ |
|
26 | + protected $_class; |
|
27 | 27 | |
28 | - /** |
|
29 | - * |
|
30 | - * @see \ASN1\Element::typeClass() |
|
31 | - * @return int |
|
32 | - */ |
|
33 | - public function typeClass(): int |
|
34 | - { |
|
35 | - return $this->_class; |
|
36 | - } |
|
28 | + /** |
|
29 | + * |
|
30 | + * @see \ASN1\Element::typeClass() |
|
31 | + * @return int |
|
32 | + */ |
|
33 | + public function typeClass(): int |
|
34 | + { |
|
35 | + return $this->_class; |
|
36 | + } |
|
37 | 37 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |
@@ -274,7 +274,7 @@ |
||
274 | 274 | /** |
275 | 275 | * Test that number is valid for this context. |
276 | 276 | * |
277 | - * @param mixed $num |
|
277 | + * @param string $num |
|
278 | 278 | * @return bool |
279 | 279 | */ |
280 | 280 | private static function _validateNumber($num): bool |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Primitive; |
6 | 6 | |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | * @link http://php.net/manual/en/language.types.float.php |
34 | 34 | * @var string |
35 | 35 | */ |
36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
37 | - '([+\-]?'. // sign |
|
38 | - '(?:'. |
|
39 | - '\d+'. // LNUM |
|
40 | - '|'. |
|
41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
42 | - '))[eE]'. |
|
43 | - '([+\-]?\d+)'. // exponent |
|
36 | + const PHP_EXPONENT_DNUM = '/^' . /* @formatter:off */ |
|
37 | + '([+\-]?' . // sign |
|
38 | + '(?:' . |
|
39 | + '\d+' . // LNUM |
|
40 | + '|' . |
|
41 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
42 | + '))[eE]' . |
|
43 | + '([+\-]?\d+)' . // exponent |
|
44 | 44 | '$/'; /* @formatter:on */ |
45 | 45 | |
46 | 46 | /** |
@@ -16,273 +16,273 @@ |
||
16 | 16 | */ |
17 | 17 | class Real extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
22 | + /** |
|
23 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Regex pattern to parse PHP exponent number format. |
|
31 | - * |
|
32 | - * @link http://php.net/manual/en/language.types.float.php |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
36 | - '([+\-]?'. // sign |
|
37 | - '(?:'. |
|
38 | - '\d+'. // LNUM |
|
39 | - '|'. |
|
40 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
41 | - '))[eE]'. |
|
42 | - '([+\-]?\d+)'. // exponent |
|
43 | - '$/'; /* @formatter:on */ |
|
29 | + /** |
|
30 | + * Regex pattern to parse PHP exponent number format. |
|
31 | + * |
|
32 | + * @link http://php.net/manual/en/language.types.float.php |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
36 | + '([+\-]?'. // sign |
|
37 | + '(?:'. |
|
38 | + '\d+'. // LNUM |
|
39 | + '|'. |
|
40 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
41 | + '))[eE]'. |
|
42 | + '([+\-]?\d+)'. // exponent |
|
43 | + '$/'; /* @formatter:on */ |
|
44 | 44 | |
45 | - /** |
|
46 | - * Number zero represented in NR3 form. |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - const NR3_ZERO = ".E+0"; |
|
45 | + /** |
|
46 | + * Number zero represented in NR3 form. |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + const NR3_ZERO = ".E+0"; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Number in NR3 form. |
|
54 | - * |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - private $_number; |
|
52 | + /** |
|
53 | + * Number in NR3 form. |
|
54 | + * |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + private $_number; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Constructor. |
|
61 | - * |
|
62 | - * @param string $number Number in NR3 form. |
|
63 | - */ |
|
64 | - public function __construct(string $number) |
|
65 | - { |
|
66 | - $this->_typeTag = self::TYPE_REAL; |
|
67 | - if (!self::_validateNumber($number)) { |
|
68 | - throw new \InvalidArgumentException( |
|
69 | - "'$number' is not a valid NR3 form real."); |
|
70 | - } |
|
71 | - $this->_number = $number; |
|
72 | - } |
|
59 | + /** |
|
60 | + * Constructor. |
|
61 | + * |
|
62 | + * @param string $number Number in NR3 form. |
|
63 | + */ |
|
64 | + public function __construct(string $number) |
|
65 | + { |
|
66 | + $this->_typeTag = self::TYPE_REAL; |
|
67 | + if (!self::_validateNumber($number)) { |
|
68 | + throw new \InvalidArgumentException( |
|
69 | + "'$number' is not a valid NR3 form real."); |
|
70 | + } |
|
71 | + $this->_number = $number; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Initialize from float. |
|
76 | - * |
|
77 | - * @param float $number |
|
78 | - * @return self |
|
79 | - */ |
|
80 | - public static function fromFloat(float $number): self |
|
81 | - { |
|
82 | - return new self(self::_decimalToNR3(strval($number))); |
|
83 | - } |
|
74 | + /** |
|
75 | + * Initialize from float. |
|
76 | + * |
|
77 | + * @param float $number |
|
78 | + * @return self |
|
79 | + */ |
|
80 | + public static function fromFloat(float $number): self |
|
81 | + { |
|
82 | + return new self(self::_decimalToNR3(strval($number))); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Get number as a float. |
|
87 | - * |
|
88 | - * @return float |
|
89 | - */ |
|
90 | - public function float(): float |
|
91 | - { |
|
92 | - return self::_nr3ToDecimal($this->_number); |
|
93 | - } |
|
85 | + /** |
|
86 | + * Get number as a float. |
|
87 | + * |
|
88 | + * @return float |
|
89 | + */ |
|
90 | + public function float(): float |
|
91 | + { |
|
92 | + return self::_nr3ToDecimal($this->_number); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - protected function _encodedContentDER(): string |
|
100 | - { |
|
101 | - /* if the real value is the value zero, there shall be no contents |
|
95 | + /** |
|
96 | + * |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + protected function _encodedContentDER(): string |
|
100 | + { |
|
101 | + /* if the real value is the value zero, there shall be no contents |
|
102 | 102 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
103 | - if (self::NR3_ZERO == $this->_number) { |
|
104 | - return ""; |
|
105 | - } |
|
106 | - // encode in NR3 decimal encoding |
|
107 | - $data = chr(0x03) . $this->_number; |
|
108 | - return $data; |
|
109 | - } |
|
103 | + if (self::NR3_ZERO == $this->_number) { |
|
104 | + return ""; |
|
105 | + } |
|
106 | + // encode in NR3 decimal encoding |
|
107 | + $data = chr(0x03) . $this->_number; |
|
108 | + return $data; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * |
|
113 | - * {@inheritdoc} |
|
114 | - * @return self |
|
115 | - */ |
|
116 | - protected static function _decodeFromDER(Identifier $identifier, |
|
117 | - string $data, int &$offset): ElementBase |
|
118 | - { |
|
119 | - $idx = $offset; |
|
120 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
121 | - // if length is zero, value is zero (spec 8.5.2) |
|
122 | - if (!$length) { |
|
123 | - $obj = new self(self::NR3_ZERO); |
|
124 | - } else { |
|
125 | - $bytes = substr($data, $idx, $length); |
|
126 | - $byte = ord($bytes[0]); |
|
127 | - if (0x80 & $byte) { // bit 8 = 1 |
|
128 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
129 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
130 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
131 | - } else { // bit 8 = 0, bit 7 = 1 |
|
132 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
133 | - } |
|
134 | - } |
|
135 | - $offset = $idx + $length; |
|
136 | - return $obj; |
|
137 | - } |
|
111 | + /** |
|
112 | + * |
|
113 | + * {@inheritdoc} |
|
114 | + * @return self |
|
115 | + */ |
|
116 | + protected static function _decodeFromDER(Identifier $identifier, |
|
117 | + string $data, int &$offset): ElementBase |
|
118 | + { |
|
119 | + $idx = $offset; |
|
120 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
121 | + // if length is zero, value is zero (spec 8.5.2) |
|
122 | + if (!$length) { |
|
123 | + $obj = new self(self::NR3_ZERO); |
|
124 | + } else { |
|
125 | + $bytes = substr($data, $idx, $length); |
|
126 | + $byte = ord($bytes[0]); |
|
127 | + if (0x80 & $byte) { // bit 8 = 1 |
|
128 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
129 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
130 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
131 | + } else { // bit 8 = 0, bit 7 = 1 |
|
132 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
133 | + } |
|
134 | + } |
|
135 | + $offset = $idx + $length; |
|
136 | + return $obj; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * |
|
141 | - * @todo Implement |
|
142 | - * @param string $data |
|
143 | - */ |
|
144 | - protected static function _decodeBinaryEncoding(string $data) |
|
145 | - { |
|
146 | - throw new \RuntimeException( |
|
147 | - "Binary encoding of REAL is not implemented."); |
|
148 | - } |
|
139 | + /** |
|
140 | + * |
|
141 | + * @todo Implement |
|
142 | + * @param string $data |
|
143 | + */ |
|
144 | + protected static function _decodeBinaryEncoding(string $data) |
|
145 | + { |
|
146 | + throw new \RuntimeException( |
|
147 | + "Binary encoding of REAL is not implemented."); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * |
|
152 | - * @param string $data |
|
153 | - * @throws \RuntimeException |
|
154 | - * @return \ASN1\Type\Primitive\Real |
|
155 | - */ |
|
156 | - protected static function _decodeDecimalEncoding(string $data): Real |
|
157 | - { |
|
158 | - $nr = ord($data[0]) & 0x03; |
|
159 | - if ($nr != 0x03) { |
|
160 | - throw new \RuntimeException("Only NR3 form supported."); |
|
161 | - } |
|
162 | - $str = substr($data, 1); |
|
163 | - return new self($str); |
|
164 | - } |
|
150 | + /** |
|
151 | + * |
|
152 | + * @param string $data |
|
153 | + * @throws \RuntimeException |
|
154 | + * @return \ASN1\Type\Primitive\Real |
|
155 | + */ |
|
156 | + protected static function _decodeDecimalEncoding(string $data): Real |
|
157 | + { |
|
158 | + $nr = ord($data[0]) & 0x03; |
|
159 | + if ($nr != 0x03) { |
|
160 | + throw new \RuntimeException("Only NR3 form supported."); |
|
161 | + } |
|
162 | + $str = substr($data, 1); |
|
163 | + return new self($str); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * |
|
168 | - * @todo Implement |
|
169 | - * @param string $data |
|
170 | - */ |
|
171 | - protected static function _decodeSpecialRealValue(string $data) |
|
172 | - { |
|
173 | - if (strlen($data) != 1) { |
|
174 | - throw new DecodeException( |
|
175 | - "SpecialRealValue must have one content octet."); |
|
176 | - } |
|
177 | - $byte = ord($data[0]); |
|
178 | - if ($byte == 0x40) { // positive infinity |
|
179 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
180 | - } else if ($byte == 0x41) { // negative infinity |
|
181 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
182 | - } else { |
|
183 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
184 | - } |
|
185 | - } |
|
166 | + /** |
|
167 | + * |
|
168 | + * @todo Implement |
|
169 | + * @param string $data |
|
170 | + */ |
|
171 | + protected static function _decodeSpecialRealValue(string $data) |
|
172 | + { |
|
173 | + if (strlen($data) != 1) { |
|
174 | + throw new DecodeException( |
|
175 | + "SpecialRealValue must have one content octet."); |
|
176 | + } |
|
177 | + $byte = ord($data[0]); |
|
178 | + if ($byte == 0x40) { // positive infinity |
|
179 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
180 | + } else if ($byte == 0x41) { // negative infinity |
|
181 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
182 | + } else { |
|
183 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
184 | + } |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Convert decimal number string to NR3 form. |
|
189 | - * |
|
190 | - * @param string $str |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - private static function _decimalToNR3(string $str): string |
|
194 | - { |
|
195 | - // if number is in exponent form |
|
196 | - /** @var $match string[] */ |
|
197 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
198 | - $parts = explode(".", $match[1]); |
|
199 | - $m = ltrim($parts[0], "0"); |
|
200 | - $e = intval($match[2]); |
|
201 | - // if mantissa had decimals |
|
202 | - if (count($parts) == 2) { |
|
203 | - $d = rtrim($parts[1], "0"); |
|
204 | - $e -= strlen($d); |
|
205 | - $m .= $d; |
|
206 | - } |
|
207 | - } else { |
|
208 | - // explode from decimal |
|
209 | - $parts = explode(".", $str); |
|
210 | - $m = ltrim($parts[0], "0"); |
|
211 | - // if number had decimals |
|
212 | - if (count($parts) == 2) { |
|
213 | - // exponent is negative number of the decimals |
|
214 | - $e = -strlen($parts[1]); |
|
215 | - // append decimals to the mantissa |
|
216 | - $m .= $parts[1]; |
|
217 | - } else { |
|
218 | - $e = 0; |
|
219 | - } |
|
220 | - // shift trailing zeroes from the mantissa to the exponent |
|
221 | - while (substr($m, -1) === "0") { |
|
222 | - $e++; |
|
223 | - $m = substr($m, 0, -1); |
|
224 | - } |
|
225 | - } |
|
226 | - /* 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 | + * @return string |
|
192 | + */ |
|
193 | + private static function _decimalToNR3(string $str): string |
|
194 | + { |
|
195 | + // if number is in exponent form |
|
196 | + /** @var $match string[] */ |
|
197 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
198 | + $parts = explode(".", $match[1]); |
|
199 | + $m = ltrim($parts[0], "0"); |
|
200 | + $e = intval($match[2]); |
|
201 | + // if mantissa had decimals |
|
202 | + if (count($parts) == 2) { |
|
203 | + $d = rtrim($parts[1], "0"); |
|
204 | + $e -= strlen($d); |
|
205 | + $m .= $d; |
|
206 | + } |
|
207 | + } else { |
|
208 | + // explode from decimal |
|
209 | + $parts = explode(".", $str); |
|
210 | + $m = ltrim($parts[0], "0"); |
|
211 | + // if number had decimals |
|
212 | + if (count($parts) == 2) { |
|
213 | + // exponent is negative number of the decimals |
|
214 | + $e = -strlen($parts[1]); |
|
215 | + // append decimals to the mantissa |
|
216 | + $m .= $parts[1]; |
|
217 | + } else { |
|
218 | + $e = 0; |
|
219 | + } |
|
220 | + // shift trailing zeroes from the mantissa to the exponent |
|
221 | + while (substr($m, -1) === "0") { |
|
222 | + $e++; |
|
223 | + $m = substr($m, 0, -1); |
|
224 | + } |
|
225 | + } |
|
226 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
227 | 227 | (X.690 07-2002, section 11.3.2.6) */ |
228 | - if (0 == $e) { |
|
229 | - $es = "+"; |
|
230 | - } else { |
|
231 | - $es = $e < 0 ? "-" : ""; |
|
232 | - } |
|
233 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
234 | - } |
|
228 | + if (0 == $e) { |
|
229 | + $es = "+"; |
|
230 | + } else { |
|
231 | + $es = $e < 0 ? "-" : ""; |
|
232 | + } |
|
233 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * Convert NR3 form number to decimal. |
|
238 | - * |
|
239 | - * @param string $str |
|
240 | - * @throws \UnexpectedValueException |
|
241 | - * @return float |
|
242 | - */ |
|
243 | - private static function _nr3ToDecimal(string $str): float |
|
244 | - { |
|
245 | - /** @var $match string[] */ |
|
246 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
247 | - throw new \UnexpectedValueException( |
|
248 | - "'$str' is not a valid NR3 form real."); |
|
249 | - } |
|
250 | - $m = $match[2]; |
|
251 | - // if number started with minus sign |
|
252 | - $inv = $match[1] == "-"; |
|
253 | - $e = intval($match[3]); |
|
254 | - // positive exponent |
|
255 | - if ($e > 0) { |
|
256 | - // pad with trailing zeroes |
|
257 | - $num = $m . str_repeat("0", $e); |
|
258 | - } else if ($e < 0) { |
|
259 | - // pad with leading zeroes |
|
260 | - if (strlen($m) < abs($e)) { |
|
261 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
262 | - } |
|
263 | - // insert decimal point |
|
264 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
265 | - } else { |
|
266 | - $num = empty($m) ? "0" : $m; |
|
267 | - } |
|
268 | - // if number is negative |
|
269 | - if ($inv) { |
|
270 | - $num = "-$num"; |
|
271 | - } |
|
272 | - return floatval($num); |
|
273 | - } |
|
236 | + /** |
|
237 | + * Convert NR3 form number to decimal. |
|
238 | + * |
|
239 | + * @param string $str |
|
240 | + * @throws \UnexpectedValueException |
|
241 | + * @return float |
|
242 | + */ |
|
243 | + private static function _nr3ToDecimal(string $str): float |
|
244 | + { |
|
245 | + /** @var $match string[] */ |
|
246 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
247 | + throw new \UnexpectedValueException( |
|
248 | + "'$str' is not a valid NR3 form real."); |
|
249 | + } |
|
250 | + $m = $match[2]; |
|
251 | + // if number started with minus sign |
|
252 | + $inv = $match[1] == "-"; |
|
253 | + $e = intval($match[3]); |
|
254 | + // positive exponent |
|
255 | + if ($e > 0) { |
|
256 | + // pad with trailing zeroes |
|
257 | + $num = $m . str_repeat("0", $e); |
|
258 | + } else if ($e < 0) { |
|
259 | + // pad with leading zeroes |
|
260 | + if (strlen($m) < abs($e)) { |
|
261 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
262 | + } |
|
263 | + // insert decimal point |
|
264 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
265 | + } else { |
|
266 | + $num = empty($m) ? "0" : $m; |
|
267 | + } |
|
268 | + // if number is negative |
|
269 | + if ($inv) { |
|
270 | + $num = "-$num"; |
|
271 | + } |
|
272 | + return floatval($num); |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Test that number is valid for this context. |
|
277 | - * |
|
278 | - * @param mixed $num |
|
279 | - * @return bool |
|
280 | - */ |
|
281 | - private static function _validateNumber($num): bool |
|
282 | - { |
|
283 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
284 | - return false; |
|
285 | - } |
|
286 | - return true; |
|
287 | - } |
|
275 | + /** |
|
276 | + * Test that number is valid for this context. |
|
277 | + * |
|
278 | + * @param mixed $num |
|
279 | + * @return bool |
|
280 | + */ |
|
281 | + private static function _validateNumber($num): bool |
|
282 | + { |
|
283 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
284 | + return false; |
|
285 | + } |
|
286 | + return true; |
|
287 | + } |
|
288 | 288 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | interface ExplicitTagging extends ElementBase |
14 | 14 | { |
15 | - /** |
|
16 | - * Get explicitly tagged wrapped element. |
|
17 | - * |
|
18 | - * <strong>NOTE!</strong> Expectation checking is deprecated and shall be |
|
19 | - * done with <code>UnspecifiedType</code>. |
|
20 | - * |
|
21 | - * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * @return \ASN1\Type\UnspecifiedType |
|
24 | - */ |
|
25 | - public function explicit($expectedTag = null): UnspecifiedType; |
|
15 | + /** |
|
16 | + * Get explicitly tagged wrapped element. |
|
17 | + * |
|
18 | + * <strong>NOTE!</strong> Expectation checking is deprecated and shall be |
|
19 | + * done with <code>UnspecifiedType</code>. |
|
20 | + * |
|
21 | + * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * @return \ASN1\Type\UnspecifiedType |
|
24 | + */ |
|
25 | + public function explicit($expectedTag = null): UnspecifiedType; |
|
26 | 26 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Tagged; |
6 | 6 |
@@ -12,25 +12,25 @@ |
||
12 | 12 | */ |
13 | 13 | class IA5String 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_IA5_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_IA5_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - protected function _validateString(string $string): bool |
|
33 | - { |
|
34 | - return preg_match('/[^\x01-\x7f]/', $string) == 0; |
|
35 | - } |
|
28 | + /** |
|
29 | + * |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + protected function _validateString(string $string): bool |
|
33 | + { |
|
34 | + return preg_match('/[^\x01-\x7f]/', $string) == 0; |
|
35 | + } |
|
36 | 36 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type\Primitive; |
6 | 6 |
@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | trait PrimitiveType |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * @see \ASN1\Element::isConstructed() |
|
15 | - * @return bool |
|
16 | - */ |
|
17 | - public function isConstructed(): bool |
|
18 | - { |
|
19 | - return false; |
|
20 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * @see \ASN1\Element::isConstructed() |
|
15 | + * @return bool |
|
16 | + */ |
|
17 | + public function isConstructed(): bool |
|
18 | + { |
|
19 | + return false; |
|
20 | + } |
|
21 | 21 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace ASN1\Type; |
6 | 6 |