@@ -17,161 +17,161 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Integer extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The number. |
|
| 25 | - * |
|
| 26 | - * @var BigInt |
|
| 27 | - */ |
|
| 28 | - private $_number; |
|
| 23 | + /** |
|
| 24 | + * The number. |
|
| 25 | + * |
|
| 26 | + * @var BigInt |
|
| 27 | + */ |
|
| 28 | + private $_number; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param int|string $number Base 10 integer |
|
| 34 | - */ |
|
| 35 | - public function __construct($number) |
|
| 36 | - { |
|
| 37 | - $this->_typeTag = self::TYPE_INTEGER; |
|
| 38 | - if (!self::_validateNumber($number)) { |
|
| 39 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 40 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
| 41 | - } |
|
| 42 | - $this->_number = new BigInt($number); |
|
| 43 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param int|string $number Base 10 integer |
|
| 34 | + */ |
|
| 35 | + public function __construct($number) |
|
| 36 | + { |
|
| 37 | + $this->_typeTag = self::TYPE_INTEGER; |
|
| 38 | + if (!self::_validateNumber($number)) { |
|
| 39 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
| 40 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
| 41 | + } |
|
| 42 | + $this->_number = new BigInt($number); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Get the number as a base 10. |
|
| 47 | - * |
|
| 48 | - * @return string Integer as a string |
|
| 49 | - */ |
|
| 50 | - public function number(): string |
|
| 51 | - { |
|
| 52 | - return $this->_number->base10(); |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * Get the number as a base 10. |
|
| 47 | + * |
|
| 48 | + * @return string Integer as a string |
|
| 49 | + */ |
|
| 50 | + public function number(): string |
|
| 51 | + { |
|
| 52 | + return $this->_number->base10(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get the number as an integer type. |
|
| 57 | - * |
|
| 58 | - * @return int |
|
| 59 | - */ |
|
| 60 | - public function intNumber(): int |
|
| 61 | - { |
|
| 62 | - return $this->_number->intVal(); |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Get the number as an integer type. |
|
| 57 | + * |
|
| 58 | + * @return int |
|
| 59 | + */ |
|
| 60 | + public function intNumber(): int |
|
| 61 | + { |
|
| 62 | + return $this->_number->intVal(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * |
|
| 67 | - * {@inheritdoc} |
|
| 68 | - */ |
|
| 69 | - protected function _encodedContentDER(): string |
|
| 70 | - { |
|
| 71 | - $num = $this->_number->gmpObj(); |
|
| 72 | - switch (gmp_sign($num)) { |
|
| 73 | - // positive |
|
| 74 | - case 1: |
|
| 75 | - return self::_encodePositiveInteger($num); |
|
| 76 | - // negative |
|
| 77 | - case -1: |
|
| 78 | - return self::_encodeNegativeInteger($num); |
|
| 79 | - } |
|
| 80 | - // zero |
|
| 81 | - return "\0"; |
|
| 82 | - } |
|
| 65 | + /** |
|
| 66 | + * |
|
| 67 | + * {@inheritdoc} |
|
| 68 | + */ |
|
| 69 | + protected function _encodedContentDER(): string |
|
| 70 | + { |
|
| 71 | + $num = $this->_number->gmpObj(); |
|
| 72 | + switch (gmp_sign($num)) { |
|
| 73 | + // positive |
|
| 74 | + case 1: |
|
| 75 | + return self::_encodePositiveInteger($num); |
|
| 76 | + // negative |
|
| 77 | + case -1: |
|
| 78 | + return self::_encodeNegativeInteger($num); |
|
| 79 | + } |
|
| 80 | + // zero |
|
| 81 | + return "\0"; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Encode positive integer to DER content. |
|
| 86 | - * |
|
| 87 | - * @param \GMP|resource $num |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - private static function _encodePositiveInteger(\GMP $num): string |
|
| 91 | - { |
|
| 92 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 93 | - // if first bit is 1, prepend full zero byte |
|
| 94 | - // to represent positive two's complement |
|
| 95 | - if (ord($bin[0]) & 0x80) { |
|
| 96 | - $bin = chr(0x00) . $bin; |
|
| 97 | - } |
|
| 98 | - return $bin; |
|
| 99 | - } |
|
| 84 | + /** |
|
| 85 | + * Encode positive integer to DER content. |
|
| 86 | + * |
|
| 87 | + * @param \GMP|resource $num |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + private static function _encodePositiveInteger(\GMP $num): string |
|
| 91 | + { |
|
| 92 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 93 | + // if first bit is 1, prepend full zero byte |
|
| 94 | + // to represent positive two's complement |
|
| 95 | + if (ord($bin[0]) & 0x80) { |
|
| 96 | + $bin = chr(0x00) . $bin; |
|
| 97 | + } |
|
| 98 | + return $bin; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Encode negative integer to DER content. |
|
| 103 | - * |
|
| 104 | - * @param \GMP|resource $num |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
| 108 | - { |
|
| 109 | - $num = gmp_abs($num); |
|
| 110 | - // compute number of bytes required |
|
| 111 | - $width = 1; |
|
| 112 | - if ($num > 128) { |
|
| 113 | - $tmp = $num; |
|
| 114 | - do { |
|
| 115 | - $width++; |
|
| 116 | - $tmp >>= 8; |
|
| 117 | - } while ($tmp > 128); |
|
| 118 | - } |
|
| 119 | - // compute two's complement 2^n - x |
|
| 120 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
| 121 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | - // if first bit is 0, prepend full inverted byte |
|
| 123 | - // to represent negative two's complement |
|
| 124 | - if (!(ord($bin[0]) & 0x80)) { |
|
| 125 | - $bin = chr(0xff) . $bin; |
|
| 126 | - } |
|
| 127 | - return $bin; |
|
| 128 | - } |
|
| 101 | + /** |
|
| 102 | + * Encode negative integer to DER content. |
|
| 103 | + * |
|
| 104 | + * @param \GMP|resource $num |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
| 108 | + { |
|
| 109 | + $num = gmp_abs($num); |
|
| 110 | + // compute number of bytes required |
|
| 111 | + $width = 1; |
|
| 112 | + if ($num > 128) { |
|
| 113 | + $tmp = $num; |
|
| 114 | + do { |
|
| 115 | + $width++; |
|
| 116 | + $tmp >>= 8; |
|
| 117 | + } while ($tmp > 128); |
|
| 118 | + } |
|
| 119 | + // compute two's complement 2^n - x |
|
| 120 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
| 121 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | + // if first bit is 0, prepend full inverted byte |
|
| 123 | + // to represent negative two's complement |
|
| 124 | + if (!(ord($bin[0]) & 0x80)) { |
|
| 125 | + $bin = chr(0xff) . $bin; |
|
| 126 | + } |
|
| 127 | + return $bin; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * |
|
| 132 | - * {@inheritdoc} |
|
| 133 | - * @return self |
|
| 134 | - */ |
|
| 135 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 136 | - int &$offset): ElementBase |
|
| 137 | - { |
|
| 138 | - $idx = $offset; |
|
| 139 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 140 | - $bytes = substr($data, $idx, $length); |
|
| 141 | - $idx += $length; |
|
| 142 | - $neg = ord($bytes[0]) & 0x80; |
|
| 143 | - // negative, apply inversion of two's complement |
|
| 144 | - if ($neg) { |
|
| 145 | - $len = strlen($bytes); |
|
| 146 | - for ($i = 0; $i < $len; $i++) { |
|
| 147 | - $bytes[$i] = ~$bytes[$i]; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - $num = gmp_init(bin2hex($bytes), 16); |
|
| 151 | - // negative, apply addition of two's complement |
|
| 152 | - // and produce negative result |
|
| 153 | - if ($neg) { |
|
| 154 | - $num = gmp_neg($num + 1); |
|
| 155 | - } |
|
| 156 | - $offset = $idx; |
|
| 157 | - // late static binding since enumerated extends integer type |
|
| 158 | - return new static(gmp_strval($num, 10)); |
|
| 159 | - } |
|
| 130 | + /** |
|
| 131 | + * |
|
| 132 | + * {@inheritdoc} |
|
| 133 | + * @return self |
|
| 134 | + */ |
|
| 135 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 136 | + int &$offset): ElementBase |
|
| 137 | + { |
|
| 138 | + $idx = $offset; |
|
| 139 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 140 | + $bytes = substr($data, $idx, $length); |
|
| 141 | + $idx += $length; |
|
| 142 | + $neg = ord($bytes[0]) & 0x80; |
|
| 143 | + // negative, apply inversion of two's complement |
|
| 144 | + if ($neg) { |
|
| 145 | + $len = strlen($bytes); |
|
| 146 | + for ($i = 0; $i < $len; $i++) { |
|
| 147 | + $bytes[$i] = ~$bytes[$i]; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + $num = gmp_init(bin2hex($bytes), 16); |
|
| 151 | + // negative, apply addition of two's complement |
|
| 152 | + // and produce negative result |
|
| 153 | + if ($neg) { |
|
| 154 | + $num = gmp_neg($num + 1); |
|
| 155 | + } |
|
| 156 | + $offset = $idx; |
|
| 157 | + // late static binding since enumerated extends integer type |
|
| 158 | + return new static(gmp_strval($num, 10)); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Test that number is valid for this context. |
|
| 163 | - * |
|
| 164 | - * @param mixed $num |
|
| 165 | - * @return bool |
|
| 166 | - */ |
|
| 167 | - private static function _validateNumber($num): bool |
|
| 168 | - { |
|
| 169 | - if (is_int($num)) { |
|
| 170 | - return true; |
|
| 171 | - } |
|
| 172 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 173 | - return true; |
|
| 174 | - } |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 161 | + /** |
|
| 162 | + * Test that number is valid for this context. |
|
| 163 | + * |
|
| 164 | + * @param mixed $num |
|
| 165 | + * @return bool |
|
| 166 | + */ |
|
| 167 | + private static function _validateNumber($num): bool |
|
| 168 | + { |
|
| 169 | + if (is_int($num)) { |
|
| 170 | + return true; |
|
| 171 | + } |
|
| 172 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
| 173 | + return true; |
|
| 174 | + } |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -13,206 +13,206 @@ |
||
| 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 boolean |
|
| 27 | - */ |
|
| 28 | - private $_indefinite; |
|
| 23 | + /** |
|
| 24 | + * Whether length is indefinite. |
|
| 25 | + * |
|
| 26 | + * @var boolean |
|
| 27 | + */ |
|
| 28 | + private $_indefinite; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param int|string $length Length |
|
| 34 | - * @param boolean $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 boolean $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 int|null $offset Reference to the variable that contains offset |
|
| 47 | - * into the data where to start parsing. Variable is updated to |
|
| 48 | - * the offset next to the parsed length component. If null, start |
|
| 49 | - * from offset 0. |
|
| 50 | - * @throws DecodeException If decoding fails |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - public static function fromDER(string $data, int &$offset = null): self |
|
| 54 | - { |
|
| 55 | - $idx = $offset ? $offset : 0; |
|
| 56 | - $datalen = strlen($data); |
|
| 57 | - if ($idx >= $datalen) { |
|
| 58 | - throw new DecodeException("Invalid offset."); |
|
| 59 | - } |
|
| 60 | - $indefinite = false; |
|
| 61 | - $byte = ord($data[$idx++]); |
|
| 62 | - // bits 7 to 1 |
|
| 63 | - $length = (0x7f & $byte); |
|
| 64 | - // long form |
|
| 65 | - if (0x80 & $byte) { |
|
| 66 | - if (!$length) { |
|
| 67 | - $indefinite = true; |
|
| 68 | - } else { |
|
| 69 | - if ($idx + $length > $datalen) { |
|
| 70 | - throw new DecodeException("Too many length octets."); |
|
| 71 | - } |
|
| 72 | - $length = self::_decodeLongFormLength($length, $data, $idx); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - if (isset($offset)) { |
|
| 76 | - $offset = $idx; |
|
| 77 | - } |
|
| 78 | - return new self($length, $indefinite); |
|
| 79 | - } |
|
| 42 | + /** |
|
| 43 | + * Decode length component from DER data. |
|
| 44 | + * |
|
| 45 | + * @param string $data DER encoded data |
|
| 46 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 47 | + * into the data where to start parsing. Variable is updated to |
|
| 48 | + * the offset next to the parsed length component. If null, start |
|
| 49 | + * from offset 0. |
|
| 50 | + * @throws DecodeException If decoding fails |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + public static function fromDER(string $data, int &$offset = null): self |
|
| 54 | + { |
|
| 55 | + $idx = $offset ? $offset : 0; |
|
| 56 | + $datalen = strlen($data); |
|
| 57 | + if ($idx >= $datalen) { |
|
| 58 | + throw new DecodeException("Invalid offset."); |
|
| 59 | + } |
|
| 60 | + $indefinite = false; |
|
| 61 | + $byte = ord($data[$idx++]); |
|
| 62 | + // bits 7 to 1 |
|
| 63 | + $length = (0x7f & $byte); |
|
| 64 | + // long form |
|
| 65 | + if (0x80 & $byte) { |
|
| 66 | + if (!$length) { |
|
| 67 | + $indefinite = true; |
|
| 68 | + } else { |
|
| 69 | + if ($idx + $length > $datalen) { |
|
| 70 | + throw new DecodeException("Too many length octets."); |
|
| 71 | + } |
|
| 72 | + $length = self::_decodeLongFormLength($length, $data, $idx); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + if (isset($offset)) { |
|
| 76 | + $offset = $idx; |
|
| 77 | + } |
|
| 78 | + return new self($length, $indefinite); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Decode long form length. |
|
| 83 | - * |
|
| 84 | - * @param int $length Number of octets |
|
| 85 | - * @param string $data Data |
|
| 86 | - * @param int $offset Reference to the variable containing offset to the |
|
| 87 | - * data. |
|
| 88 | - * @throws DecodeException If decoding fails |
|
| 89 | - * @return string Integer as a string |
|
| 90 | - */ |
|
| 91 | - private static function _decodeLongFormLength(int $length, string $data, |
|
| 92 | - int &$offset): string |
|
| 93 | - { |
|
| 94 | - // first octet must not be 0xff (spec 8.1.3.5c) |
|
| 95 | - if ($length == 127) { |
|
| 96 | - throw new DecodeException("Invalid number of length octets."); |
|
| 97 | - } |
|
| 98 | - $num = gmp_init(0, 10); |
|
| 99 | - while (--$length >= 0) { |
|
| 100 | - $byte = ord($data[$offset++]); |
|
| 101 | - $num <<= 8; |
|
| 102 | - $num |= $byte; |
|
| 103 | - } |
|
| 104 | - return gmp_strval($num); |
|
| 105 | - } |
|
| 81 | + /** |
|
| 82 | + * Decode long form length. |
|
| 83 | + * |
|
| 84 | + * @param int $length Number of octets |
|
| 85 | + * @param string $data Data |
|
| 86 | + * @param int $offset Reference to the variable containing offset to the |
|
| 87 | + * data. |
|
| 88 | + * @throws DecodeException If decoding fails |
|
| 89 | + * @return string Integer as a string |
|
| 90 | + */ |
|
| 91 | + private static function _decodeLongFormLength(int $length, string $data, |
|
| 92 | + int &$offset): string |
|
| 93 | + { |
|
| 94 | + // first octet must not be 0xff (spec 8.1.3.5c) |
|
| 95 | + if ($length == 127) { |
|
| 96 | + throw new DecodeException("Invalid number of length octets."); |
|
| 97 | + } |
|
| 98 | + $num = gmp_init(0, 10); |
|
| 99 | + while (--$length >= 0) { |
|
| 100 | + $byte = ord($data[$offset++]); |
|
| 101 | + $num <<= 8; |
|
| 102 | + $num |= $byte; |
|
| 103 | + } |
|
| 104 | + return gmp_strval($num); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * Decode length from DER. |
|
| 109 | - * |
|
| 110 | - * Throws an exception if length doesn't match with expected or if data |
|
| 111 | - * doesn't contain enough bytes. |
|
| 112 | - * |
|
| 113 | - * @see self::fromDER |
|
| 114 | - * @param string $data DER data |
|
| 115 | - * @param int $offset Reference to the offset variable |
|
| 116 | - * @param int|null $expected Expected length, null to bypass checking |
|
| 117 | - * @throws DecodeException If decoding or expectation fails |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public static function expectFromDER(string $data, int &$offset, |
|
| 121 | - int $expected = null): self |
|
| 122 | - { |
|
| 123 | - $idx = $offset; |
|
| 124 | - $length = self::fromDER($data, $idx); |
|
| 125 | - // DER encoding must have definite length (spec 10.1) |
|
| 126 | - if ($length->isIndefinite()) { |
|
| 127 | - throw new DecodeException("DER encoding must have definite length."); |
|
| 128 | - } |
|
| 129 | - // if certain length was expected |
|
| 130 | - if (isset($expected) && $expected != $length->intLength()) { |
|
| 131 | - throw new DecodeException( |
|
| 132 | - sprintf("Expected length %d, got %d.", $expected, |
|
| 133 | - $length->intLength())); |
|
| 134 | - } |
|
| 135 | - // check that enough data is available |
|
| 136 | - if (strlen($data) < $idx + $length->intLength()) { |
|
| 137 | - throw new DecodeException( |
|
| 138 | - sprintf("Length %d overflows data, %d bytes left.", |
|
| 139 | - $length->intLength(), strlen($data) - $idx)); |
|
| 140 | - } |
|
| 141 | - $offset = $idx; |
|
| 142 | - return $length; |
|
| 143 | - } |
|
| 107 | + /** |
|
| 108 | + * Decode length from DER. |
|
| 109 | + * |
|
| 110 | + * Throws an exception if length doesn't match with expected or if data |
|
| 111 | + * doesn't contain enough bytes. |
|
| 112 | + * |
|
| 113 | + * @see self::fromDER |
|
| 114 | + * @param string $data DER data |
|
| 115 | + * @param int $offset Reference to the offset variable |
|
| 116 | + * @param int|null $expected Expected length, null to bypass checking |
|
| 117 | + * @throws DecodeException If decoding or expectation fails |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public static function expectFromDER(string $data, int &$offset, |
|
| 121 | + int $expected = null): self |
|
| 122 | + { |
|
| 123 | + $idx = $offset; |
|
| 124 | + $length = self::fromDER($data, $idx); |
|
| 125 | + // DER encoding must have definite length (spec 10.1) |
|
| 126 | + if ($length->isIndefinite()) { |
|
| 127 | + throw new DecodeException("DER encoding must have definite length."); |
|
| 128 | + } |
|
| 129 | + // if certain length was expected |
|
| 130 | + if (isset($expected) && $expected != $length->intLength()) { |
|
| 131 | + throw new DecodeException( |
|
| 132 | + sprintf("Expected length %d, got %d.", $expected, |
|
| 133 | + $length->intLength())); |
|
| 134 | + } |
|
| 135 | + // check that enough data is available |
|
| 136 | + if (strlen($data) < $idx + $length->intLength()) { |
|
| 137 | + throw new DecodeException( |
|
| 138 | + sprintf("Length %d overflows data, %d bytes left.", |
|
| 139 | + $length->intLength(), strlen($data) - $idx)); |
|
| 140 | + } |
|
| 141 | + $offset = $idx; |
|
| 142 | + return $length; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * |
|
| 147 | - * @see Encodable::toDER() |
|
| 148 | - * @throws \DomainException If length is too large to encode |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 151 | - public function toDER(): string |
|
| 152 | - { |
|
| 153 | - $bytes = []; |
|
| 154 | - if ($this->_indefinite) { |
|
| 155 | - $bytes[] = 0x80; |
|
| 156 | - } else { |
|
| 157 | - $num = $this->_length->gmpObj(); |
|
| 158 | - // long form |
|
| 159 | - if ($num > 127) { |
|
| 160 | - $octets = []; |
|
| 161 | - for (; $num > 0; $num >>= 8) { |
|
| 162 | - $octets[] = gmp_intval(0xff & $num); |
|
| 163 | - } |
|
| 164 | - $count = count($octets); |
|
| 165 | - // first octet must not be 0xff |
|
| 166 | - if ($count >= 127) { |
|
| 167 | - throw new \DomainException("Too many length octets."); |
|
| 168 | - } |
|
| 169 | - $bytes[] = 0x80 | $count; |
|
| 170 | - foreach (array_reverse($octets) as $octet) { |
|
| 171 | - $bytes[] = $octet; |
|
| 172 | - } |
|
| 173 | - } else { // short form |
|
| 174 | - $bytes[] = gmp_intval($num); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - return pack("C*", ...$bytes); |
|
| 178 | - } |
|
| 145 | + /** |
|
| 146 | + * |
|
| 147 | + * @see Encodable::toDER() |
|
| 148 | + * @throws \DomainException If length is too large to encode |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | + public function toDER(): string |
|
| 152 | + { |
|
| 153 | + $bytes = []; |
|
| 154 | + if ($this->_indefinite) { |
|
| 155 | + $bytes[] = 0x80; |
|
| 156 | + } else { |
|
| 157 | + $num = $this->_length->gmpObj(); |
|
| 158 | + // long form |
|
| 159 | + if ($num > 127) { |
|
| 160 | + $octets = []; |
|
| 161 | + for (; $num > 0; $num >>= 8) { |
|
| 162 | + $octets[] = gmp_intval(0xff & $num); |
|
| 163 | + } |
|
| 164 | + $count = count($octets); |
|
| 165 | + // first octet must not be 0xff |
|
| 166 | + if ($count >= 127) { |
|
| 167 | + throw new \DomainException("Too many length octets."); |
|
| 168 | + } |
|
| 169 | + $bytes[] = 0x80 | $count; |
|
| 170 | + foreach (array_reverse($octets) as $octet) { |
|
| 171 | + $bytes[] = $octet; |
|
| 172 | + } |
|
| 173 | + } else { // short form |
|
| 174 | + $bytes[] = gmp_intval($num); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + return pack("C*", ...$bytes); |
|
| 178 | + } |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * Get the length. |
|
| 182 | - * |
|
| 183 | - * @throws \LogicException If length is indefinite |
|
| 184 | - * @return string Length as an integer string |
|
| 185 | - */ |
|
| 186 | - public function length(): string |
|
| 187 | - { |
|
| 188 | - if ($this->_indefinite) { |
|
| 189 | - throw new \LogicException("Length is indefinite."); |
|
| 190 | - } |
|
| 191 | - return $this->_length->base10(); |
|
| 192 | - } |
|
| 180 | + /** |
|
| 181 | + * Get the length. |
|
| 182 | + * |
|
| 183 | + * @throws \LogicException If length is indefinite |
|
| 184 | + * @return string Length as an integer string |
|
| 185 | + */ |
|
| 186 | + public function length(): string |
|
| 187 | + { |
|
| 188 | + if ($this->_indefinite) { |
|
| 189 | + throw new \LogicException("Length is indefinite."); |
|
| 190 | + } |
|
| 191 | + return $this->_length->base10(); |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Get the length as an integer. |
|
| 196 | - * |
|
| 197 | - * @throws \LogicException If length is indefinite |
|
| 198 | - * @throws \RuntimeException If length overflows integer size |
|
| 199 | - * @return int |
|
| 200 | - */ |
|
| 201 | - public function intLength(): int |
|
| 202 | - { |
|
| 203 | - if ($this->_indefinite) { |
|
| 204 | - throw new \LogicException("Length is indefinite."); |
|
| 205 | - } |
|
| 206 | - return $this->_length->intVal(); |
|
| 207 | - } |
|
| 194 | + /** |
|
| 195 | + * Get the length as an integer. |
|
| 196 | + * |
|
| 197 | + * @throws \LogicException If length is indefinite |
|
| 198 | + * @throws \RuntimeException If length overflows integer size |
|
| 199 | + * @return int |
|
| 200 | + */ |
|
| 201 | + public function intLength(): int |
|
| 202 | + { |
|
| 203 | + if ($this->_indefinite) { |
|
| 204 | + throw new \LogicException("Length is indefinite."); |
|
| 205 | + } |
|
| 206 | + return $this->_length->intVal(); |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Whether length is indefinite. |
|
| 211 | - * |
|
| 212 | - * @return boolean |
|
| 213 | - */ |
|
| 214 | - public function isIndefinite(): bool |
|
| 215 | - { |
|
| 216 | - return $this->_indefinite; |
|
| 217 | - } |
|
| 209 | + /** |
|
| 210 | + * Whether length is indefinite. |
|
| 211 | + * |
|
| 212 | + * @return boolean |
|
| 213 | + */ |
|
| 214 | + public function isIndefinite(): bool |
|
| 215 | + { |
|
| 216 | + return $this->_indefinite; |
|
| 217 | + } |
|
| 218 | 218 | } |
@@ -6,106 +6,106 @@ |
||
| 6 | 6 | |
| 7 | 7 | class BigInt |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Number as a base10 integer string. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - private $_num; |
|
| 9 | + /** |
|
| 10 | + * Number as a base10 integer string. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + private $_num; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Number as an integer type. |
|
| 18 | - * |
|
| 19 | - * @internal Lazily initialized |
|
| 20 | - * @var int|null |
|
| 21 | - */ |
|
| 22 | - private $_intNum; |
|
| 16 | + /** |
|
| 17 | + * Number as an integer type. |
|
| 18 | + * |
|
| 19 | + * @internal Lazily initialized |
|
| 20 | + * @var int|null |
|
| 21 | + */ |
|
| 22 | + private $_intNum; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Constructor. |
|
| 26 | - * |
|
| 27 | - * @param string|int $num |
|
| 28 | - */ |
|
| 29 | - public function __construct($num) |
|
| 30 | - { |
|
| 31 | - $this->_num = strval($num); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Constructor. |
|
| 26 | + * |
|
| 27 | + * @param string|int $num |
|
| 28 | + */ |
|
| 29 | + public function __construct($num) |
|
| 30 | + { |
|
| 31 | + $this->_num = strval($num); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the number as a base10 integer string. |
|
| 36 | - * |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public function base10(): string |
|
| 40 | - { |
|
| 41 | - return $this->_num; |
|
| 42 | - } |
|
| 34 | + /** |
|
| 35 | + * Get the number as a base10 integer string. |
|
| 36 | + * |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public function base10(): string |
|
| 40 | + { |
|
| 41 | + return $this->_num; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Get the number as an integer. |
|
| 46 | - * |
|
| 47 | - * @throws \RuntimeException If number overflows integer size |
|
| 48 | - * @return int |
|
| 49 | - */ |
|
| 50 | - public function intVal(): int |
|
| 51 | - { |
|
| 52 | - if (!isset($this->_intNum)) { |
|
| 53 | - $num = gmp_init($this->_num, 10); |
|
| 54 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
| 55 | - throw new \RuntimeException("Integer overflow."); |
|
| 56 | - } |
|
| 57 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
| 58 | - throw new \RuntimeException("Integer underflow."); |
|
| 59 | - } |
|
| 60 | - $this->_intNum = gmp_intval($num); |
|
| 61 | - } |
|
| 62 | - return $this->_intNum; |
|
| 63 | - } |
|
| 44 | + /** |
|
| 45 | + * Get the number as an integer. |
|
| 46 | + * |
|
| 47 | + * @throws \RuntimeException If number overflows integer size |
|
| 48 | + * @return int |
|
| 49 | + */ |
|
| 50 | + public function intVal(): int |
|
| 51 | + { |
|
| 52 | + if (!isset($this->_intNum)) { |
|
| 53 | + $num = gmp_init($this->_num, 10); |
|
| 54 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
| 55 | + throw new \RuntimeException("Integer overflow."); |
|
| 56 | + } |
|
| 57 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
| 58 | + throw new \RuntimeException("Integer underflow."); |
|
| 59 | + } |
|
| 60 | + $this->_intNum = gmp_intval($num); |
|
| 61 | + } |
|
| 62 | + return $this->_intNum; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Get the maximum integer value. |
|
| 67 | - * |
|
| 68 | - * @return \GMP |
|
| 69 | - */ |
|
| 70 | - private function _intMaxGmp(): \GMP |
|
| 71 | - { |
|
| 72 | - static $gmp; |
|
| 73 | - if (!isset($gmp)) { |
|
| 74 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
| 75 | - } |
|
| 76 | - return $gmp; |
|
| 77 | - } |
|
| 65 | + /** |
|
| 66 | + * Get the maximum integer value. |
|
| 67 | + * |
|
| 68 | + * @return \GMP |
|
| 69 | + */ |
|
| 70 | + private function _intMaxGmp(): \GMP |
|
| 71 | + { |
|
| 72 | + static $gmp; |
|
| 73 | + if (!isset($gmp)) { |
|
| 74 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
| 75 | + } |
|
| 76 | + return $gmp; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Get the minimum integer value. |
|
| 81 | - * |
|
| 82 | - * @return \GMP |
|
| 83 | - */ |
|
| 84 | - private function _intMinGmp(): \GMP |
|
| 85 | - { |
|
| 86 | - static $gmp; |
|
| 87 | - if (!isset($gmp)) { |
|
| 88 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
| 89 | - } |
|
| 90 | - return $gmp; |
|
| 91 | - } |
|
| 79 | + /** |
|
| 80 | + * Get the minimum integer value. |
|
| 81 | + * |
|
| 82 | + * @return \GMP |
|
| 83 | + */ |
|
| 84 | + private function _intMinGmp(): \GMP |
|
| 85 | + { |
|
| 86 | + static $gmp; |
|
| 87 | + if (!isset($gmp)) { |
|
| 88 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
| 89 | + } |
|
| 90 | + return $gmp; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get the number as a GMP object. |
|
| 95 | - * |
|
| 96 | - * @return \GMP |
|
| 97 | - */ |
|
| 98 | - public function gmpObj(): \GMP |
|
| 99 | - { |
|
| 100 | - return gmp_init($this->_num, 10); |
|
| 101 | - } |
|
| 93 | + /** |
|
| 94 | + * Get the number as a GMP object. |
|
| 95 | + * |
|
| 96 | + * @return \GMP |
|
| 97 | + */ |
|
| 98 | + public function gmpObj(): \GMP |
|
| 99 | + { |
|
| 100 | + return gmp_init($this->_num, 10); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function __toString() |
|
| 108 | - { |
|
| 109 | - return $this->base10(); |
|
| 110 | - } |
|
| 103 | + /** |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function __toString() |
|
| 108 | + { |
|
| 109 | + return $this->base10(); |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -11,141 +11,141 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Flags |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Flag octets. |
|
| 16 | - * |
|
| 17 | - * @var string $_flags |
|
| 18 | - */ |
|
| 19 | - protected $_flags; |
|
| 14 | + /** |
|
| 15 | + * Flag octets. |
|
| 16 | + * |
|
| 17 | + * @var string $_flags |
|
| 18 | + */ |
|
| 19 | + protected $_flags; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Number of flags. |
|
| 23 | - * |
|
| 24 | - * @var int $_width |
|
| 25 | - */ |
|
| 26 | - protected $_width; |
|
| 21 | + /** |
|
| 22 | + * Number of flags. |
|
| 23 | + * |
|
| 24 | + * @var int $_width |
|
| 25 | + */ |
|
| 26 | + protected $_width; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param int|string $flags Flags |
|
| 32 | - * @param int $width The number of flags. If width is larger than number of |
|
| 33 | - * bits in $flags, zeroes are prepended to flag field. |
|
| 34 | - */ |
|
| 35 | - public function __construct($flags, int $width) |
|
| 36 | - { |
|
| 37 | - if (!$width) { |
|
| 38 | - $this->_flags = ""; |
|
| 39 | - } else { |
|
| 40 | - // calculate number of unused bits in last octet |
|
| 41 | - $last_octet_bits = $width % 8; |
|
| 42 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 43 | - $num = gmp_init($flags); |
|
| 44 | - // mask bits outside bitfield width |
|
| 45 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
| 46 | - $num &= $mask; |
|
| 47 | - // shift towards MSB if needed |
|
| 48 | - $data = gmp_export($num << $unused_bits, 1, |
|
| 49 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 50 | - $octets = unpack("C*", $data); |
|
| 51 | - $bits = count($octets) * 8; |
|
| 52 | - // pad with zeroes |
|
| 53 | - while ($bits < $width) { |
|
| 54 | - array_unshift($octets, 0); |
|
| 55 | - $bits += 8; |
|
| 56 | - } |
|
| 57 | - $this->_flags = pack("C*", ...$octets); |
|
| 58 | - } |
|
| 59 | - $this->_width = $width; |
|
| 60 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param int|string $flags Flags |
|
| 32 | + * @param int $width The number of flags. If width is larger than number of |
|
| 33 | + * bits in $flags, zeroes are prepended to flag field. |
|
| 34 | + */ |
|
| 35 | + public function __construct($flags, int $width) |
|
| 36 | + { |
|
| 37 | + if (!$width) { |
|
| 38 | + $this->_flags = ""; |
|
| 39 | + } else { |
|
| 40 | + // calculate number of unused bits in last octet |
|
| 41 | + $last_octet_bits = $width % 8; |
|
| 42 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 43 | + $num = gmp_init($flags); |
|
| 44 | + // mask bits outside bitfield width |
|
| 45 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
| 46 | + $num &= $mask; |
|
| 47 | + // shift towards MSB if needed |
|
| 48 | + $data = gmp_export($num << $unused_bits, 1, |
|
| 49 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 50 | + $octets = unpack("C*", $data); |
|
| 51 | + $bits = count($octets) * 8; |
|
| 52 | + // pad with zeroes |
|
| 53 | + while ($bits < $width) { |
|
| 54 | + array_unshift($octets, 0); |
|
| 55 | + $bits += 8; |
|
| 56 | + } |
|
| 57 | + $this->_flags = pack("C*", ...$octets); |
|
| 58 | + } |
|
| 59 | + $this->_width = $width; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Initialize from BitString. |
|
| 64 | - * |
|
| 65 | - * @param BitString $bs |
|
| 66 | - * @param int $width |
|
| 67 | - * @return self |
|
| 68 | - */ |
|
| 69 | - public static function fromBitString(BitString $bs, int $width): self |
|
| 70 | - { |
|
| 71 | - $num_bits = $bs->numBits(); |
|
| 72 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 73 | - $num >>= $bs->unusedBits(); |
|
| 74 | - if ($num_bits < $width) { |
|
| 75 | - $num <<= ($width - $num_bits); |
|
| 76 | - } |
|
| 77 | - return new self(gmp_strval($num, 10), $width); |
|
| 78 | - } |
|
| 62 | + /** |
|
| 63 | + * Initialize from BitString. |
|
| 64 | + * |
|
| 65 | + * @param BitString $bs |
|
| 66 | + * @param int $width |
|
| 67 | + * @return self |
|
| 68 | + */ |
|
| 69 | + public static function fromBitString(BitString $bs, int $width): self |
|
| 70 | + { |
|
| 71 | + $num_bits = $bs->numBits(); |
|
| 72 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 73 | + $num >>= $bs->unusedBits(); |
|
| 74 | + if ($num_bits < $width) { |
|
| 75 | + $num <<= ($width - $num_bits); |
|
| 76 | + } |
|
| 77 | + return new self(gmp_strval($num, 10), $width); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Check whether a bit at given index is set. |
|
| 82 | - * Index 0 is the leftmost bit. |
|
| 83 | - * |
|
| 84 | - * @param int $idx |
|
| 85 | - * @throws \OutOfBoundsException |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - public function test(int $idx): bool |
|
| 89 | - { |
|
| 90 | - if ($idx >= $this->_width) { |
|
| 91 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 92 | - } |
|
| 93 | - // octet index |
|
| 94 | - $oi = (int) floor($idx / 8); |
|
| 95 | - $byte = $this->_flags[$oi]; |
|
| 96 | - // bit index |
|
| 97 | - $bi = $idx % 8; |
|
| 98 | - // index 0 is the most significant bit in byte |
|
| 99 | - $mask = 0x01 << (7 - $bi); |
|
| 100 | - return (ord($byte) & $mask) > 0; |
|
| 101 | - } |
|
| 80 | + /** |
|
| 81 | + * Check whether a bit at given index is set. |
|
| 82 | + * Index 0 is the leftmost bit. |
|
| 83 | + * |
|
| 84 | + * @param int $idx |
|
| 85 | + * @throws \OutOfBoundsException |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + public function test(int $idx): bool |
|
| 89 | + { |
|
| 90 | + if ($idx >= $this->_width) { |
|
| 91 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 92 | + } |
|
| 93 | + // octet index |
|
| 94 | + $oi = (int) floor($idx / 8); |
|
| 95 | + $byte = $this->_flags[$oi]; |
|
| 96 | + // bit index |
|
| 97 | + $bi = $idx % 8; |
|
| 98 | + // index 0 is the most significant bit in byte |
|
| 99 | + $mask = 0x01 << (7 - $bi); |
|
| 100 | + return (ord($byte) & $mask) > 0; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Get flags as an octet string. |
|
| 105 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function string(): string |
|
| 110 | - { |
|
| 111 | - return $this->_flags; |
|
| 112 | - } |
|
| 103 | + /** |
|
| 104 | + * Get flags as an octet string. |
|
| 105 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function string(): string |
|
| 110 | + { |
|
| 111 | + return $this->_flags; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Get flags as a base 10 integer. |
|
| 116 | - * |
|
| 117 | - * @return string Integer as a string |
|
| 118 | - */ |
|
| 119 | - public function number(): string |
|
| 120 | - { |
|
| 121 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | - $last_octet_bits = $this->_width % 8; |
|
| 123 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 124 | - $num >>= $unused_bits; |
|
| 125 | - return gmp_strval($num, 10); |
|
| 126 | - } |
|
| 114 | + /** |
|
| 115 | + * Get flags as a base 10 integer. |
|
| 116 | + * |
|
| 117 | + * @return string Integer as a string |
|
| 118 | + */ |
|
| 119 | + public function number(): string |
|
| 120 | + { |
|
| 121 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
| 122 | + $last_octet_bits = $this->_width % 8; |
|
| 123 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 124 | + $num >>= $unused_bits; |
|
| 125 | + return gmp_strval($num, 10); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get flags as an integer. |
|
| 130 | - * |
|
| 131 | - * @return int |
|
| 132 | - */ |
|
| 133 | - public function intNumber(): int |
|
| 134 | - { |
|
| 135 | - $num = new BigInt($this->number()); |
|
| 136 | - return $num->intVal(); |
|
| 137 | - } |
|
| 128 | + /** |
|
| 129 | + * Get flags as an integer. |
|
| 130 | + * |
|
| 131 | + * @return int |
|
| 132 | + */ |
|
| 133 | + public function intNumber(): int |
|
| 134 | + { |
|
| 135 | + $num = new BigInt($this->number()); |
|
| 136 | + return $num->intVal(); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Get flags as a BitString. |
|
| 141 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
| 142 | - * |
|
| 143 | - * @return BitString |
|
| 144 | - */ |
|
| 145 | - public function bitString(): BitString |
|
| 146 | - { |
|
| 147 | - $last_octet_bits = $this->_width % 8; |
|
| 148 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 149 | - return new BitString($this->_flags, $unused_bits); |
|
| 150 | - } |
|
| 139 | + /** |
|
| 140 | + * Get flags as a BitString. |
|
| 141 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
| 142 | + * |
|
| 143 | + * @return BitString |
|
| 144 | + */ |
|
| 145 | + public function bitString(): BitString |
|
| 146 | + { |
|
| 147 | + $last_octet_bits = $this->_width % 8; |
|
| 148 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
| 149 | + return new BitString($this->_flags, $unused_bits); |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -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 | } |
@@ -17,194 +17,194 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class BitString extends StringType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Number of unused bits in the last octet. |
|
| 25 | - * |
|
| 26 | - * @var int $_unusedBits |
|
| 27 | - */ |
|
| 28 | - protected $_unusedBits; |
|
| 23 | + /** |
|
| 24 | + * Number of unused bits in the last octet. |
|
| 25 | + * |
|
| 26 | + * @var int $_unusedBits |
|
| 27 | + */ |
|
| 28 | + protected $_unusedBits; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $string Content octets |
|
| 34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | - */ |
|
| 36 | - public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | - { |
|
| 38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | - parent::__construct($string); |
|
| 40 | - $this->_unusedBits = $unused_bits; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $string Content octets |
|
| 34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
| 35 | + */ |
|
| 36 | + public function __construct(string $string, int $unused_bits = 0) |
|
| 37 | + { |
|
| 38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 39 | + parent::__construct($string); |
|
| 40 | + $this->_unusedBits = $unused_bits; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the number of bits in the string. |
|
| 45 | - * |
|
| 46 | - * @return int |
|
| 47 | - */ |
|
| 48 | - public function numBits(): int |
|
| 49 | - { |
|
| 50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Get the number of bits in the string. |
|
| 45 | + * |
|
| 46 | + * @return int |
|
| 47 | + */ |
|
| 48 | + public function numBits(): int |
|
| 49 | + { |
|
| 50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Get the number of unused bits in the last octet of the string. |
|
| 55 | - * |
|
| 56 | - * @return int |
|
| 57 | - */ |
|
| 58 | - public function unusedBits(): int |
|
| 59 | - { |
|
| 60 | - return $this->_unusedBits; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Get the number of unused bits in the last octet of the string. |
|
| 55 | + * |
|
| 56 | + * @return int |
|
| 57 | + */ |
|
| 58 | + public function unusedBits(): int |
|
| 59 | + { |
|
| 60 | + return $this->_unusedBits; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Test whether bit is set. |
|
| 65 | - * |
|
| 66 | - * @param int $idx Bit index. |
|
| 67 | - * Most significant bit of the first octet is index 0. |
|
| 68 | - * @return boolean |
|
| 69 | - */ |
|
| 70 | - public function testBit(int $idx): bool |
|
| 71 | - { |
|
| 72 | - // octet index |
|
| 73 | - $oi = (int) floor($idx / 8); |
|
| 74 | - // if octet is outside range |
|
| 75 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 77 | - } |
|
| 78 | - // bit index |
|
| 79 | - $bi = $idx % 8; |
|
| 80 | - // if tested bit is last octet's unused bit |
|
| 81 | - if ($oi == strlen($this->_string) - 1) { |
|
| 82 | - if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | - throw new \OutOfBoundsException("Index refers to an unused bit."); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - $byte = $this->_string[$oi]; |
|
| 87 | - // index 0 is the most significant bit in byte |
|
| 88 | - $mask = 0x01 << (7 - $bi); |
|
| 89 | - return (ord($byte) & $mask) > 0; |
|
| 90 | - } |
|
| 63 | + /** |
|
| 64 | + * Test whether bit is set. |
|
| 65 | + * |
|
| 66 | + * @param int $idx Bit index. |
|
| 67 | + * Most significant bit of the first octet is index 0. |
|
| 68 | + * @return boolean |
|
| 69 | + */ |
|
| 70 | + public function testBit(int $idx): bool |
|
| 71 | + { |
|
| 72 | + // octet index |
|
| 73 | + $oi = (int) floor($idx / 8); |
|
| 74 | + // if octet is outside range |
|
| 75 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 76 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 77 | + } |
|
| 78 | + // bit index |
|
| 79 | + $bi = $idx % 8; |
|
| 80 | + // if tested bit is last octet's unused bit |
|
| 81 | + if ($oi == strlen($this->_string) - 1) { |
|
| 82 | + if ($bi >= 8 - $this->_unusedBits) { |
|
| 83 | + throw new \OutOfBoundsException("Index refers to an unused bit."); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + $byte = $this->_string[$oi]; |
|
| 87 | + // index 0 is the most significant bit in byte |
|
| 88 | + $mask = 0x01 << (7 - $bi); |
|
| 89 | + return (ord($byte) & $mask) > 0; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Get range of bits. |
|
| 94 | - * |
|
| 95 | - * @param int $start Index of first bit |
|
| 96 | - * @param int $length Number of bits in range |
|
| 97 | - * @throws \OutOfBoundsException |
|
| 98 | - * @return string Integer of $length bits |
|
| 99 | - */ |
|
| 100 | - public function range(int $start, int $length): string |
|
| 101 | - { |
|
| 102 | - if (!$length) { |
|
| 103 | - return "0"; |
|
| 104 | - } |
|
| 105 | - if ($start + $length > $this->numBits()) { |
|
| 106 | - throw new \OutOfBoundsException("Not enough bits."); |
|
| 107 | - } |
|
| 108 | - $bits = gmp_init(0); |
|
| 109 | - $idx = $start; |
|
| 110 | - $end = $start + $length; |
|
| 111 | - while (true) { |
|
| 112 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
| 113 | - $bits |= $bit; |
|
| 114 | - if (++$idx >= $end) { |
|
| 115 | - break; |
|
| 116 | - } |
|
| 117 | - $bits <<= 1; |
|
| 118 | - } |
|
| 119 | - return gmp_strval($bits, 10); |
|
| 120 | - } |
|
| 92 | + /** |
|
| 93 | + * Get range of bits. |
|
| 94 | + * |
|
| 95 | + * @param int $start Index of first bit |
|
| 96 | + * @param int $length Number of bits in range |
|
| 97 | + * @throws \OutOfBoundsException |
|
| 98 | + * @return string Integer of $length bits |
|
| 99 | + */ |
|
| 100 | + public function range(int $start, int $length): string |
|
| 101 | + { |
|
| 102 | + if (!$length) { |
|
| 103 | + return "0"; |
|
| 104 | + } |
|
| 105 | + if ($start + $length > $this->numBits()) { |
|
| 106 | + throw new \OutOfBoundsException("Not enough bits."); |
|
| 107 | + } |
|
| 108 | + $bits = gmp_init(0); |
|
| 109 | + $idx = $start; |
|
| 110 | + $end = $start + $length; |
|
| 111 | + while (true) { |
|
| 112 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
| 113 | + $bits |= $bit; |
|
| 114 | + if (++$idx >= $end) { |
|
| 115 | + break; |
|
| 116 | + } |
|
| 117 | + $bits <<= 1; |
|
| 118 | + } |
|
| 119 | + return gmp_strval($bits, 10); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Get a copy of the bit string with trailing zeroes removed. |
|
| 124 | - * |
|
| 125 | - * @return self |
|
| 126 | - */ |
|
| 127 | - public function withoutTrailingZeroes(): self |
|
| 128 | - { |
|
| 129 | - // if bit string was empty |
|
| 130 | - if (!strlen($this->_string)) { |
|
| 131 | - return new self(""); |
|
| 132 | - } |
|
| 133 | - $bits = $this->_string; |
|
| 134 | - // count number of empty trailing octets |
|
| 135 | - $unused_octets = 0; |
|
| 136 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 137 | - if ($bits[$idx] != "\x0") { |
|
| 138 | - break; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - // strip trailing octets |
|
| 142 | - if ($unused_octets) { |
|
| 143 | - $bits = substr($bits, 0, -$unused_octets); |
|
| 144 | - } |
|
| 145 | - // if bit string was full of zeroes |
|
| 146 | - if (!strlen($bits)) { |
|
| 147 | - return new self(""); |
|
| 148 | - } |
|
| 149 | - // count number of trailing zeroes in the last octet |
|
| 150 | - $unused_bits = 0; |
|
| 151 | - $byte = ord($bits[strlen($bits) - 1]); |
|
| 152 | - while (!($byte & 0x01)) { |
|
| 153 | - $unused_bits++; |
|
| 154 | - $byte >>= 1; |
|
| 155 | - } |
|
| 156 | - return new self($bits, $unused_bits); |
|
| 157 | - } |
|
| 122 | + /** |
|
| 123 | + * Get a copy of the bit string with trailing zeroes removed. |
|
| 124 | + * |
|
| 125 | + * @return self |
|
| 126 | + */ |
|
| 127 | + public function withoutTrailingZeroes(): self |
|
| 128 | + { |
|
| 129 | + // if bit string was empty |
|
| 130 | + if (!strlen($this->_string)) { |
|
| 131 | + return new self(""); |
|
| 132 | + } |
|
| 133 | + $bits = $this->_string; |
|
| 134 | + // count number of empty trailing octets |
|
| 135 | + $unused_octets = 0; |
|
| 136 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 137 | + if ($bits[$idx] != "\x0") { |
|
| 138 | + break; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + // strip trailing octets |
|
| 142 | + if ($unused_octets) { |
|
| 143 | + $bits = substr($bits, 0, -$unused_octets); |
|
| 144 | + } |
|
| 145 | + // if bit string was full of zeroes |
|
| 146 | + if (!strlen($bits)) { |
|
| 147 | + return new self(""); |
|
| 148 | + } |
|
| 149 | + // count number of trailing zeroes in the last octet |
|
| 150 | + $unused_bits = 0; |
|
| 151 | + $byte = ord($bits[strlen($bits) - 1]); |
|
| 152 | + while (!($byte & 0x01)) { |
|
| 153 | + $unused_bits++; |
|
| 154 | + $byte >>= 1; |
|
| 155 | + } |
|
| 156 | + return new self($bits, $unused_bits); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * |
|
| 161 | - * {@inheritdoc} |
|
| 162 | - */ |
|
| 163 | - protected function _encodedContentDER(): string |
|
| 164 | - { |
|
| 165 | - $der = chr($this->_unusedBits); |
|
| 166 | - $der .= $this->_string; |
|
| 167 | - if ($this->_unusedBits) { |
|
| 168 | - $octet = $der[strlen($der) - 1]; |
|
| 169 | - // set unused bits to zero |
|
| 170 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 171 | - $der[strlen($der) - 1] = $octet; |
|
| 172 | - } |
|
| 173 | - return $der; |
|
| 174 | - } |
|
| 159 | + /** |
|
| 160 | + * |
|
| 161 | + * {@inheritdoc} |
|
| 162 | + */ |
|
| 163 | + protected function _encodedContentDER(): string |
|
| 164 | + { |
|
| 165 | + $der = chr($this->_unusedBits); |
|
| 166 | + $der .= $this->_string; |
|
| 167 | + if ($this->_unusedBits) { |
|
| 168 | + $octet = $der[strlen($der) - 1]; |
|
| 169 | + // set unused bits to zero |
|
| 170 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 171 | + $der[strlen($der) - 1] = $octet; |
|
| 172 | + } |
|
| 173 | + return $der; |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * |
|
| 178 | - * {@inheritdoc} |
|
| 179 | - * @return self |
|
| 180 | - */ |
|
| 181 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 182 | - int &$offset): ElementBase |
|
| 183 | - { |
|
| 184 | - $idx = $offset; |
|
| 185 | - $length = Length::expectFromDER($data, $idx); |
|
| 186 | - if ($length->intLength() < 1) { |
|
| 187 | - throw new DecodeException("Bit string length must be at least 1."); |
|
| 188 | - } |
|
| 189 | - $unused_bits = ord($data[$idx++]); |
|
| 190 | - if ($unused_bits > 7) { |
|
| 191 | - throw new DecodeException( |
|
| 192 | - "Unused bits in a bit string must be less than 8."); |
|
| 193 | - } |
|
| 194 | - $str_len = $length->intLength() - 1; |
|
| 195 | - if ($str_len) { |
|
| 196 | - $str = substr($data, $idx, $str_len); |
|
| 197 | - if ($unused_bits) { |
|
| 198 | - $mask = (1 << $unused_bits) - 1; |
|
| 199 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | - throw new DecodeException( |
|
| 201 | - "DER encoded bit string must have zero padding."); |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - } else { |
|
| 205 | - $str = ""; |
|
| 206 | - } |
|
| 207 | - $offset = $idx + $str_len; |
|
| 208 | - return new self($str, $unused_bits); |
|
| 209 | - } |
|
| 176 | + /** |
|
| 177 | + * |
|
| 178 | + * {@inheritdoc} |
|
| 179 | + * @return self |
|
| 180 | + */ |
|
| 181 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 182 | + int &$offset): ElementBase |
|
| 183 | + { |
|
| 184 | + $idx = $offset; |
|
| 185 | + $length = Length::expectFromDER($data, $idx); |
|
| 186 | + if ($length->intLength() < 1) { |
|
| 187 | + throw new DecodeException("Bit string length must be at least 1."); |
|
| 188 | + } |
|
| 189 | + $unused_bits = ord($data[$idx++]); |
|
| 190 | + if ($unused_bits > 7) { |
|
| 191 | + throw new DecodeException( |
|
| 192 | + "Unused bits in a bit string must be less than 8."); |
|
| 193 | + } |
|
| 194 | + $str_len = $length->intLength() - 1; |
|
| 195 | + if ($str_len) { |
|
| 196 | + $str = substr($data, $idx, $str_len); |
|
| 197 | + if ($unused_bits) { |
|
| 198 | + $mask = (1 << $unused_bits) - 1; |
|
| 199 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 200 | + throw new DecodeException( |
|
| 201 | + "DER encoded bit string must have zero padding."); |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + } else { |
|
| 205 | + $str = ""; |
|
| 206 | + } |
|
| 207 | + $offset = $idx + $str_len; |
|
| 208 | + return new self($str, $unused_bits); |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -17,160 +17,160 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class ObjectIdentifier extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Object identifier in dotted format. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $_oid; |
|
| 23 | + /** |
|
| 24 | + * Object identifier in dotted format. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $_oid; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $oid OID in dotted format |
|
| 34 | - */ |
|
| 35 | - public function __construct(string $oid) |
|
| 36 | - { |
|
| 37 | - $this->_oid = $oid; |
|
| 38 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $oid OID in dotted format |
|
| 34 | + */ |
|
| 35 | + public function __construct(string $oid) |
|
| 36 | + { |
|
| 37 | + $this->_oid = $oid; |
|
| 38 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get OID in dotted format. |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - public function oid(): string |
|
| 47 | - { |
|
| 48 | - return $this->_oid; |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Get OID in dotted format. |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + public function oid(): string |
|
| 47 | + { |
|
| 48 | + return $this->_oid; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - */ |
|
| 55 | - protected function _encodedContentDER(): string |
|
| 56 | - { |
|
| 57 | - $subids = self::_explodeDottedOID($this->_oid); |
|
| 58 | - // encode first two subids to one according to spec section 8.19.4 |
|
| 59 | - if (count($subids) >= 2) { |
|
| 60 | - $num = ($subids[0] * 40) + $subids[1]; |
|
| 61 | - array_splice($subids, 0, 2, array($num)); |
|
| 62 | - } |
|
| 63 | - return self::_encodeSubIDs(...$subids); |
|
| 64 | - } |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + */ |
|
| 55 | + protected function _encodedContentDER(): string |
|
| 56 | + { |
|
| 57 | + $subids = self::_explodeDottedOID($this->_oid); |
|
| 58 | + // encode first two subids to one according to spec section 8.19.4 |
|
| 59 | + if (count($subids) >= 2) { |
|
| 60 | + $num = ($subids[0] * 40) + $subids[1]; |
|
| 61 | + array_splice($subids, 0, 2, array($num)); |
|
| 62 | + } |
|
| 63 | + return self::_encodeSubIDs(...$subids); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - * @return self |
|
| 70 | - */ |
|
| 71 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 72 | - int &$offset): ElementBase |
|
| 73 | - { |
|
| 74 | - $idx = $offset; |
|
| 75 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 76 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 77 | - $idx += $len; |
|
| 78 | - // decode first subidentifier according to spec section 8.19.4 |
|
| 79 | - if (isset($subids[0])) { |
|
| 80 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
| 81 | - array_splice($subids, 0, 1, array($x, $y)); |
|
| 82 | - } |
|
| 83 | - $offset = $idx; |
|
| 84 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 85 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + * @return self |
|
| 70 | + */ |
|
| 71 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 72 | + int &$offset): ElementBase |
|
| 73 | + { |
|
| 74 | + $idx = $offset; |
|
| 75 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 76 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 77 | + $idx += $len; |
|
| 78 | + // decode first subidentifier according to spec section 8.19.4 |
|
| 79 | + if (isset($subids[0])) { |
|
| 80 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
| 81 | + array_splice($subids, 0, 1, array($x, $y)); |
|
| 82 | + } |
|
| 83 | + $offset = $idx; |
|
| 84 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Explode dotted OID to an array of sub ID's. |
|
| 89 | - * |
|
| 90 | - * @param string $oid OID in dotted format |
|
| 91 | - * @return \GMP[] Array of GMP numbers |
|
| 92 | - */ |
|
| 93 | - protected static function _explodeDottedOID(string $oid): array |
|
| 94 | - { |
|
| 95 | - $subids = []; |
|
| 96 | - foreach (explode(".", $oid) as $subid) { |
|
| 97 | - $subids[] = gmp_init($subid, 10); |
|
| 98 | - } |
|
| 99 | - return $subids; |
|
| 100 | - } |
|
| 87 | + /** |
|
| 88 | + * Explode dotted OID to an array of sub ID's. |
|
| 89 | + * |
|
| 90 | + * @param string $oid OID in dotted format |
|
| 91 | + * @return \GMP[] Array of GMP numbers |
|
| 92 | + */ |
|
| 93 | + protected static function _explodeDottedOID(string $oid): array |
|
| 94 | + { |
|
| 95 | + $subids = []; |
|
| 96 | + foreach (explode(".", $oid) as $subid) { |
|
| 97 | + $subids[] = gmp_init($subid, 10); |
|
| 98 | + } |
|
| 99 | + return $subids; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Implode an array of sub IDs to dotted OID format. |
|
| 104 | - * |
|
| 105 | - * @param \GMP[] $subids |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
| 109 | - { |
|
| 110 | - return implode(".", |
|
| 111 | - array_map( |
|
| 112 | - function ($num) { |
|
| 113 | - return gmp_strval($num, 10); |
|
| 114 | - }, $subids)); |
|
| 115 | - } |
|
| 102 | + /** |
|
| 103 | + * Implode an array of sub IDs to dotted OID format. |
|
| 104 | + * |
|
| 105 | + * @param \GMP[] $subids |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
| 109 | + { |
|
| 110 | + return implode(".", |
|
| 111 | + array_map( |
|
| 112 | + function ($num) { |
|
| 113 | + return gmp_strval($num, 10); |
|
| 114 | + }, $subids)); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Encode sub ID's to DER. |
|
| 119 | - * |
|
| 120 | - * @param \GMP[] $subids |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
| 124 | - { |
|
| 125 | - $data = ""; |
|
| 126 | - foreach ($subids as $subid) { |
|
| 127 | - // if number fits to one base 128 byte |
|
| 128 | - if ($subid < 128) { |
|
| 129 | - $data .= chr(intval($subid)); |
|
| 130 | - } else { // encode to multiple bytes |
|
| 131 | - $bytes = []; |
|
| 132 | - do { |
|
| 133 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
| 134 | - $subid >>= 7; |
|
| 135 | - } while ($subid > 0); |
|
| 136 | - // all bytes except last must have bit 8 set to one |
|
| 137 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
| 138 | - $data .= chr(0x80 | $byte); |
|
| 139 | - } |
|
| 140 | - $data .= chr(reset($bytes)); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - return $data; |
|
| 144 | - } |
|
| 117 | + /** |
|
| 118 | + * Encode sub ID's to DER. |
|
| 119 | + * |
|
| 120 | + * @param \GMP[] $subids |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
| 124 | + { |
|
| 125 | + $data = ""; |
|
| 126 | + foreach ($subids as $subid) { |
|
| 127 | + // if number fits to one base 128 byte |
|
| 128 | + if ($subid < 128) { |
|
| 129 | + $data .= chr(intval($subid)); |
|
| 130 | + } else { // encode to multiple bytes |
|
| 131 | + $bytes = []; |
|
| 132 | + do { |
|
| 133 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
| 134 | + $subid >>= 7; |
|
| 135 | + } while ($subid > 0); |
|
| 136 | + // all bytes except last must have bit 8 set to one |
|
| 137 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
| 138 | + $data .= chr(0x80 | $byte); |
|
| 139 | + } |
|
| 140 | + $data .= chr(reset($bytes)); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + return $data; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Decode sub ID's from DER data. |
|
| 148 | - * |
|
| 149 | - * @param string $data |
|
| 150 | - * @throws DecodeException |
|
| 151 | - * @return \GMP[] Array of GMP numbers |
|
| 152 | - */ |
|
| 153 | - protected static function _decodeSubIDs(string $data): array |
|
| 154 | - { |
|
| 155 | - $subids = []; |
|
| 156 | - $idx = 0; |
|
| 157 | - $end = strlen($data); |
|
| 158 | - while ($idx < $end) { |
|
| 159 | - $num = gmp_init("0", 10); |
|
| 160 | - while (true) { |
|
| 161 | - if ($idx >= $end) { |
|
| 162 | - throw new DecodeException("Unexpected end of data."); |
|
| 163 | - } |
|
| 164 | - $byte = ord($data[$idx++]); |
|
| 165 | - $num |= $byte & 0x7f; |
|
| 166 | - // bit 8 of the last octet is zero |
|
| 167 | - if (!($byte & 0x80)) { |
|
| 168 | - break; |
|
| 169 | - } |
|
| 170 | - $num <<= 7; |
|
| 171 | - } |
|
| 172 | - $subids[] = $num; |
|
| 173 | - } |
|
| 174 | - return $subids; |
|
| 175 | - } |
|
| 146 | + /** |
|
| 147 | + * Decode sub ID's from DER data. |
|
| 148 | + * |
|
| 149 | + * @param string $data |
|
| 150 | + * @throws DecodeException |
|
| 151 | + * @return \GMP[] Array of GMP numbers |
|
| 152 | + */ |
|
| 153 | + protected static function _decodeSubIDs(string $data): array |
|
| 154 | + { |
|
| 155 | + $subids = []; |
|
| 156 | + $idx = 0; |
|
| 157 | + $end = strlen($data); |
|
| 158 | + while ($idx < $end) { |
|
| 159 | + $num = gmp_init("0", 10); |
|
| 160 | + while (true) { |
|
| 161 | + if ($idx >= $end) { |
|
| 162 | + throw new DecodeException("Unexpected end of data."); |
|
| 163 | + } |
|
| 164 | + $byte = ord($data[$idx++]); |
|
| 165 | + $num |= $byte & 0x7f; |
|
| 166 | + // bit 8 of the last octet is zero |
|
| 167 | + if (!($byte & 0x80)) { |
|
| 168 | + break; |
|
| 169 | + } |
|
| 170 | + $num <<= 7; |
|
| 171 | + } |
|
| 172 | + $subids[] = $num; |
|
| 173 | + } |
|
| 174 | + return $subids; |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -17,85 +17,85 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | abstract class TaggedType extends Element |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * |
|
| 22 | - * {@inheritdoc} |
|
| 23 | - */ |
|
| 24 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 25 | - int &$offset): ElementBase |
|
| 26 | - { |
|
| 27 | - $idx = $offset; |
|
| 28 | - $type = new DERTaggedType($identifier, $data, $idx); |
|
| 29 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 30 | - $offset = $idx + $length; |
|
| 31 | - return $type; |
|
| 32 | - } |
|
| 20 | + /** |
|
| 21 | + * |
|
| 22 | + * {@inheritdoc} |
|
| 23 | + */ |
|
| 24 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 25 | + int &$offset): ElementBase |
|
| 26 | + { |
|
| 27 | + $idx = $offset; |
|
| 28 | + $type = new DERTaggedType($identifier, $data, $idx); |
|
| 29 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 30 | + $offset = $idx + $length; |
|
| 31 | + return $type; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Check whether element supports explicit tagging. |
|
| 36 | - * |
|
| 37 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 38 | - * @throws \UnexpectedValueException If expectation fails |
|
| 39 | - * @return ExplicitTagging |
|
| 40 | - */ |
|
| 41 | - public function expectExplicit($expectedTag = null): ExplicitTagging |
|
| 42 | - { |
|
| 43 | - $el = $this; |
|
| 44 | - if (!$el instanceof ExplicitTagging) { |
|
| 45 | - throw new \UnexpectedValueException( |
|
| 46 | - "Element doesn't implement explicit tagging."); |
|
| 47 | - } |
|
| 48 | - if (isset($expectedTag)) { |
|
| 49 | - $el->expectTagged($expectedTag); |
|
| 50 | - } |
|
| 51 | - return $el; |
|
| 52 | - } |
|
| 34 | + /** |
|
| 35 | + * Check whether element supports explicit tagging. |
|
| 36 | + * |
|
| 37 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 38 | + * @throws \UnexpectedValueException If expectation fails |
|
| 39 | + * @return ExplicitTagging |
|
| 40 | + */ |
|
| 41 | + public function expectExplicit($expectedTag = null): ExplicitTagging |
|
| 42 | + { |
|
| 43 | + $el = $this; |
|
| 44 | + if (!$el instanceof ExplicitTagging) { |
|
| 45 | + throw new \UnexpectedValueException( |
|
| 46 | + "Element doesn't implement explicit tagging."); |
|
| 47 | + } |
|
| 48 | + if (isset($expectedTag)) { |
|
| 49 | + $el->expectTagged($expectedTag); |
|
| 50 | + } |
|
| 51 | + return $el; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get the wrapped inner element employing explicit tagging. |
|
| 56 | - * |
|
| 57 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 58 | - * @throws \UnexpectedValueException If expectation fails |
|
| 59 | - * @return UnspecifiedType |
|
| 60 | - */ |
|
| 61 | - public function asExplicit($expectedTag = null): UnspecifiedType |
|
| 62 | - { |
|
| 63 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
| 64 | - } |
|
| 54 | + /** |
|
| 55 | + * Get the wrapped inner element employing explicit tagging. |
|
| 56 | + * |
|
| 57 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 58 | + * @throws \UnexpectedValueException If expectation fails |
|
| 59 | + * @return UnspecifiedType |
|
| 60 | + */ |
|
| 61 | + public function asExplicit($expectedTag = null): UnspecifiedType |
|
| 62 | + { |
|
| 63 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Check whether element supports implicit tagging. |
|
| 68 | - * |
|
| 69 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 70 | - * @throws \UnexpectedValueException If expectation fails |
|
| 71 | - * @return ImplicitTagging |
|
| 72 | - */ |
|
| 73 | - public function expectImplicit($expectedTag = null): ImplicitTagging |
|
| 74 | - { |
|
| 75 | - $el = $this; |
|
| 76 | - if (!$el instanceof ImplicitTagging) { |
|
| 77 | - throw new \UnexpectedValueException( |
|
| 78 | - "Element doesn't implement implicit tagging."); |
|
| 79 | - } |
|
| 80 | - if (isset($expectedTag)) { |
|
| 81 | - $el->expectTagged($expectedTag); |
|
| 82 | - } |
|
| 83 | - return $el; |
|
| 84 | - } |
|
| 66 | + /** |
|
| 67 | + * Check whether element supports implicit tagging. |
|
| 68 | + * |
|
| 69 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 70 | + * @throws \UnexpectedValueException If expectation fails |
|
| 71 | + * @return ImplicitTagging |
|
| 72 | + */ |
|
| 73 | + public function expectImplicit($expectedTag = null): ImplicitTagging |
|
| 74 | + { |
|
| 75 | + $el = $this; |
|
| 76 | + if (!$el instanceof ImplicitTagging) { |
|
| 77 | + throw new \UnexpectedValueException( |
|
| 78 | + "Element doesn't implement implicit tagging."); |
|
| 79 | + } |
|
| 80 | + if (isset($expectedTag)) { |
|
| 81 | + $el->expectTagged($expectedTag); |
|
| 82 | + } |
|
| 83 | + return $el; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get the wrapped inner element employing implicit tagging. |
|
| 88 | - * |
|
| 89 | - * @param int $tag Type tag of the inner element |
|
| 90 | - * @param int|null $expectedTag Optional outer tag expectation |
|
| 91 | - * @param int $expectedClass Optional inner type class expectation |
|
| 92 | - * @throws \UnexpectedValueException If expectation fails |
|
| 93 | - * @return UnspecifiedType |
|
| 94 | - */ |
|
| 95 | - public function asImplicit(int $tag, $expectedTag = null, |
|
| 96 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 97 | - { |
|
| 98 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 99 | - $expectedClass); |
|
| 100 | - } |
|
| 86 | + /** |
|
| 87 | + * Get the wrapped inner element employing implicit tagging. |
|
| 88 | + * |
|
| 89 | + * @param int $tag Type tag of the inner element |
|
| 90 | + * @param int|null $expectedTag Optional outer tag expectation |
|
| 91 | + * @param int $expectedClass Optional inner type class expectation |
|
| 92 | + * @throws \UnexpectedValueException If expectation fails |
|
| 93 | + * @return UnspecifiedType |
|
| 94 | + */ |
|
| 95 | + public function asImplicit(int $tag, $expectedTag = null, |
|
| 96 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 97 | + { |
|
| 98 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
| 99 | + $expectedClass); |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -11,87 +11,87 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class TimeType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * UTC timezone. |
|
| 16 | - * |
|
| 17 | - * @var string |
|
| 18 | - */ |
|
| 19 | - const TZ_UTC = "UTC"; |
|
| 14 | + /** |
|
| 15 | + * UTC timezone. |
|
| 16 | + * |
|
| 17 | + * @var string |
|
| 18 | + */ |
|
| 19 | + const TZ_UTC = "UTC"; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Date and time. |
|
| 23 | - * |
|
| 24 | - * @var \DateTimeImmutable $_dateTime |
|
| 25 | - */ |
|
| 26 | - protected $_dateTime; |
|
| 21 | + /** |
|
| 22 | + * Date and time. |
|
| 23 | + * |
|
| 24 | + * @var \DateTimeImmutable $_dateTime |
|
| 25 | + */ |
|
| 26 | + protected $_dateTime; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Constructor. |
|
| 30 | - * |
|
| 31 | - * @param \DateTimeImmutable $dt |
|
| 32 | - */ |
|
| 33 | - public function __construct(\DateTimeImmutable $dt) |
|
| 34 | - { |
|
| 35 | - $this->_dateTime = $dt; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Constructor. |
|
| 30 | + * |
|
| 31 | + * @param \DateTimeImmutable $dt |
|
| 32 | + */ |
|
| 33 | + public function __construct(\DateTimeImmutable $dt) |
|
| 34 | + { |
|
| 35 | + $this->_dateTime = $dt; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Initialize from datetime string. |
|
| 40 | - * |
|
| 41 | - * @param string $time Time string |
|
| 42 | - * @param string|null $tz Timezone, if null use default. |
|
| 43 | - * @throws \RuntimeException |
|
| 44 | - * @return self |
|
| 45 | - */ |
|
| 46 | - public static function fromString(string $time, string $tz = null): self |
|
| 47 | - { |
|
| 48 | - try { |
|
| 49 | - if (!isset($tz)) { |
|
| 50 | - $tz = date_default_timezone_get(); |
|
| 51 | - } |
|
| 52 | - return new static( |
|
| 53 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
| 54 | - } catch (\Exception $e) { |
|
| 55 | - throw new \RuntimeException( |
|
| 56 | - "Failed to create DateTime: " . |
|
| 57 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 58 | - } |
|
| 59 | - } |
|
| 38 | + /** |
|
| 39 | + * Initialize from datetime string. |
|
| 40 | + * |
|
| 41 | + * @param string $time Time string |
|
| 42 | + * @param string|null $tz Timezone, if null use default. |
|
| 43 | + * @throws \RuntimeException |
|
| 44 | + * @return self |
|
| 45 | + */ |
|
| 46 | + public static function fromString(string $time, string $tz = null): self |
|
| 47 | + { |
|
| 48 | + try { |
|
| 49 | + if (!isset($tz)) { |
|
| 50 | + $tz = date_default_timezone_get(); |
|
| 51 | + } |
|
| 52 | + return new static( |
|
| 53 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
| 54 | + } catch (\Exception $e) { |
|
| 55 | + throw new \RuntimeException( |
|
| 56 | + "Failed to create DateTime: " . |
|
| 57 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get the date and time. |
|
| 63 | - * |
|
| 64 | - * @return \DateTimeImmutable |
|
| 65 | - */ |
|
| 66 | - public function dateTime(): \DateTimeImmutable |
|
| 67 | - { |
|
| 68 | - return $this->_dateTime; |
|
| 69 | - } |
|
| 61 | + /** |
|
| 62 | + * Get the date and time. |
|
| 63 | + * |
|
| 64 | + * @return \DateTimeImmutable |
|
| 65 | + */ |
|
| 66 | + public function dateTime(): \DateTimeImmutable |
|
| 67 | + { |
|
| 68 | + return $this->_dateTime; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Create DateTimeZone object from string. |
|
| 73 | - * |
|
| 74 | - * @param string $tz |
|
| 75 | - * @throws \UnexpectedValueException If timezone is invalid |
|
| 76 | - * @return \DateTimeZone |
|
| 77 | - */ |
|
| 78 | - protected static function _createTimeZone(string $tz): \DateTimeZone |
|
| 79 | - { |
|
| 80 | - try { |
|
| 81 | - return new \DateTimeZone($tz); |
|
| 82 | - } catch (\Exception $e) { |
|
| 83 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 84 | - } |
|
| 85 | - } |
|
| 71 | + /** |
|
| 72 | + * Create DateTimeZone object from string. |
|
| 73 | + * |
|
| 74 | + * @param string $tz |
|
| 75 | + * @throws \UnexpectedValueException If timezone is invalid |
|
| 76 | + * @return \DateTimeZone |
|
| 77 | + */ |
|
| 78 | + protected static function _createTimeZone(string $tz): \DateTimeZone |
|
| 79 | + { |
|
| 80 | + try { |
|
| 81 | + return new \DateTimeZone($tz); |
|
| 82 | + } catch (\Exception $e) { |
|
| 83 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Get last error caused by DateTimeImmutable. |
|
| 89 | - * |
|
| 90 | - * @return string |
|
| 91 | - */ |
|
| 92 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 93 | - { |
|
| 94 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 95 | - return implode(", ", $errors); |
|
| 96 | - } |
|
| 87 | + /** |
|
| 88 | + * Get last error caused by DateTimeImmutable. |
|
| 89 | + * |
|
| 90 | + * @return string |
|
| 91 | + */ |
|
| 92 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 93 | + { |
|
| 94 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 95 | + return implode(", ", $errors); |
|
| 96 | + } |
|
| 97 | 97 | } |