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