| @@ -17,276 +17,276 @@ | ||
| 17 | 17 | */ | 
| 18 | 18 | class Real extends Element | 
| 19 | 19 |  { | 
| 20 | - use UniversalClass; | |
| 21 | - use PrimitiveType; | |
| 20 | + use UniversalClass; | |
| 21 | + use PrimitiveType; | |
| 22 | 22 | |
| 23 | - /** | |
| 24 | - * Regex pattern to parse NR3 form number conforming to DER. | |
| 25 | - * | |
| 26 | - * @var string | |
| 27 | - */ | |
| 28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; | |
| 23 | + /** | |
| 24 | + * Regex pattern to parse NR3 form number conforming to DER. | |
| 25 | + * | |
| 26 | + * @var string | |
| 27 | + */ | |
| 28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; | |
| 29 | 29 | |
| 30 | - /** | |
| 31 | - * Regex pattern to parse PHP exponent number format. | |
| 32 | - * | |
| 33 | - * @see http://php.net/manual/en/language.types.float.php | |
| 34 | - * | |
| 35 | - * @var string | |
| 36 | - */ | |
| 37 | - const PHP_EXPONENT_DNUM = '/^' . | |
| 38 | - '([+\-]?' . // sign | |
| 39 | - '(?:' . | |
| 40 | - '\d+' . // LNUM | |
| 41 | - '|' . | |
| 42 | - '(?:\d*\.\d+|\d+\.\d*)' . // DNUM | |
| 43 | - '))[eE]' . | |
| 44 | - '([+\-]?\d+)' . // exponent | |
| 45 | - '$/'; | |
| 30 | + /** | |
| 31 | + * Regex pattern to parse PHP exponent number format. | |
| 32 | + * | |
| 33 | + * @see http://php.net/manual/en/language.types.float.php | |
| 34 | + * | |
| 35 | + * @var string | |
| 36 | + */ | |
| 37 | + const PHP_EXPONENT_DNUM = '/^' . | |
| 38 | + '([+\-]?' . // sign | |
| 39 | + '(?:' . | |
| 40 | + '\d+' . // LNUM | |
| 41 | + '|' . | |
| 42 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM | |
| 43 | + '))[eE]' . | |
| 44 | + '([+\-]?\d+)' . // exponent | |
| 45 | + '$/'; | |
| 46 | 46 | |
| 47 | - /** | |
| 48 | - * Number zero represented in NR3 form. | |
| 49 | - * | |
| 50 | - * @var string | |
| 51 | - */ | |
| 52 | - const NR3_ZERO = '.E+0'; | |
| 47 | + /** | |
| 48 | + * Number zero represented in NR3 form. | |
| 49 | + * | |
| 50 | + * @var string | |
| 51 | + */ | |
| 52 | + const NR3_ZERO = '.E+0'; | |
| 53 | 53 | |
| 54 | - /** | |
| 55 | - * Number in NR3 form. | |
| 56 | - * | |
| 57 | - * @var string | |
| 58 | - */ | |
| 59 | - private $_number; | |
| 54 | + /** | |
| 55 | + * Number in NR3 form. | |
| 56 | + * | |
| 57 | + * @var string | |
| 58 | + */ | |
| 59 | + private $_number; | |
| 60 | 60 | |
| 61 | - /** | |
| 62 | - * Constructor. | |
| 63 | - * | |
| 64 | - * @param string $number number in NR3 form | |
| 65 | - */ | |
| 66 | - public function __construct(string $number) | |
| 67 | -    { | |
| 68 | - $this->_typeTag = self::TYPE_REAL; | |
| 69 | -        if (!self::_validateNumber($number)) { | |
| 70 | - throw new \InvalidArgumentException( | |
| 71 | -                "'${number}' is not a valid NR3 form real."); | |
| 72 | - } | |
| 73 | - $this->_number = $number; | |
| 74 | - } | |
| 61 | + /** | |
| 62 | + * Constructor. | |
| 63 | + * | |
| 64 | + * @param string $number number in NR3 form | |
| 65 | + */ | |
| 66 | + public function __construct(string $number) | |
| 67 | +	{ | |
| 68 | + $this->_typeTag = self::TYPE_REAL; | |
| 69 | +		if (!self::_validateNumber($number)) { | |
| 70 | + throw new \InvalidArgumentException( | |
| 71 | +				"'${number}' is not a valid NR3 form real."); | |
| 72 | + } | |
| 73 | + $this->_number = $number; | |
| 74 | + } | |
| 75 | 75 | |
| 76 | - /** | |
| 77 | - * Initialize from float. | |
| 78 | - * | |
| 79 | - * @param float $number | |
| 80 | - * | |
| 81 | - * @return self | |
| 82 | - */ | |
| 83 | - public static function fromFloat(float $number): self | |
| 84 | -    { | |
| 85 | - return new self(self::_decimalToNR3(strval($number))); | |
| 86 | - } | |
| 76 | + /** | |
| 77 | + * Initialize from float. | |
| 78 | + * | |
| 79 | + * @param float $number | |
| 80 | + * | |
| 81 | + * @return self | |
| 82 | + */ | |
| 83 | + public static function fromFloat(float $number): self | |
| 84 | +	{ | |
| 85 | + return new self(self::_decimalToNR3(strval($number))); | |
| 86 | + } | |
| 87 | 87 | |
| 88 | - /** | |
| 89 | - * Get number as a float. | |
| 90 | - * | |
| 91 | - * @return float | |
| 92 | - */ | |
| 93 | - public function float(): float | |
| 94 | -    { | |
| 95 | - return self::_nr3ToDecimal($this->_number); | |
| 96 | - } | |
| 88 | + /** | |
| 89 | + * Get number as a float. | |
| 90 | + * | |
| 91 | + * @return float | |
| 92 | + */ | |
| 93 | + public function float(): float | |
| 94 | +	{ | |
| 95 | + return self::_nr3ToDecimal($this->_number); | |
| 96 | + } | |
| 97 | 97 | |
| 98 | - /** | |
| 99 | -     * {@inheritdoc} | |
| 100 | - */ | |
| 101 | - protected function _encodedContentDER(): string | |
| 102 | -    { | |
| 103 | - /* if the real value is the value zero, there shall be no contents | |
| 98 | + /** | |
| 99 | +	 * {@inheritdoc} | |
| 100 | + */ | |
| 101 | + protected function _encodedContentDER(): string | |
| 102 | +	{ | |
| 103 | + /* if the real value is the value zero, there shall be no contents | |
| 104 | 104 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ | 
| 105 | -        if (self::NR3_ZERO == $this->_number) { | |
| 106 | - return ''; | |
| 107 | - } | |
| 108 | - // encode in NR3 decimal encoding | |
| 109 | - return chr(0x03) . $this->_number; | |
| 110 | - } | |
| 105 | +		if (self::NR3_ZERO == $this->_number) { | |
| 106 | + return ''; | |
| 107 | + } | |
| 108 | + // encode in NR3 decimal encoding | |
| 109 | + return chr(0x03) . $this->_number; | |
| 110 | + } | |
| 111 | 111 | |
| 112 | - /** | |
| 113 | -     * {@inheritdoc} | |
| 114 | - */ | |
| 115 | - protected static function _decodeFromDER(Identifier $identifier, | |
| 116 | - string $data, int &$offset): ElementBase | |
| 117 | -    { | |
| 118 | - $idx = $offset; | |
| 119 | - $length = Length::expectFromDER($data, $idx)->intLength(); | |
| 120 | - // if length is zero, value is zero (spec 8.5.2) | |
| 121 | -        if (!$length) { | |
| 122 | - $obj = new self(self::NR3_ZERO); | |
| 123 | -        } else { | |
| 124 | - $bytes = substr($data, $idx, $length); | |
| 125 | - $byte = ord($bytes[0]); | |
| 126 | -            if (0x80 & $byte) { // bit 8 = 1 | |
| 127 | - $obj = self::_decodeBinaryEncoding($bytes); | |
| 128 | -            } elseif (0x00 == $byte >> 6) { // bit 8 = 0, bit 7 = 0 | |
| 129 | - $obj = self::_decodeDecimalEncoding($bytes); | |
| 130 | -            } else { // bit 8 = 0, bit 7 = 1 | |
| 131 | - $obj = self::_decodeSpecialRealValue($bytes); | |
| 132 | - } | |
| 133 | - } | |
| 134 | - $offset = $idx + $length; | |
| 135 | - return $obj; | |
| 136 | - } | |
| 112 | + /** | |
| 113 | +	 * {@inheritdoc} | |
| 114 | + */ | |
| 115 | + protected static function _decodeFromDER(Identifier $identifier, | |
| 116 | + string $data, int &$offset): ElementBase | |
| 117 | +	{ | |
| 118 | + $idx = $offset; | |
| 119 | + $length = Length::expectFromDER($data, $idx)->intLength(); | |
| 120 | + // if length is zero, value is zero (spec 8.5.2) | |
| 121 | +		if (!$length) { | |
| 122 | + $obj = new self(self::NR3_ZERO); | |
| 123 | +		} else { | |
| 124 | + $bytes = substr($data, $idx, $length); | |
| 125 | + $byte = ord($bytes[0]); | |
| 126 | +			if (0x80 & $byte) { // bit 8 = 1 | |
| 127 | + $obj = self::_decodeBinaryEncoding($bytes); | |
| 128 | +			} elseif (0x00 == $byte >> 6) { // bit 8 = 0, bit 7 = 0 | |
| 129 | + $obj = self::_decodeDecimalEncoding($bytes); | |
| 130 | +			} else { // bit 8 = 0, bit 7 = 1 | |
| 131 | + $obj = self::_decodeSpecialRealValue($bytes); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + $offset = $idx + $length; | |
| 135 | + return $obj; | |
| 136 | + } | |
| 137 | 137 | |
| 138 | - /** | |
| 139 | - * @todo Implement | |
| 140 | - * | |
| 141 | - * @param string $data | |
| 142 | - */ | |
| 143 | - protected static function _decodeBinaryEncoding(string $data) | |
| 144 | -    { | |
| 145 | - throw new \RuntimeException( | |
| 146 | - 'Binary encoding of REAL is not implemented.'); | |
| 147 | - } | |
| 138 | + /** | |
| 139 | + * @todo Implement | |
| 140 | + * | |
| 141 | + * @param string $data | |
| 142 | + */ | |
| 143 | + protected static function _decodeBinaryEncoding(string $data) | |
| 144 | +	{ | |
| 145 | + throw new \RuntimeException( | |
| 146 | + 'Binary encoding of REAL is not implemented.'); | |
| 147 | + } | |
| 148 | 148 | |
| 149 | - /** | |
| 150 | - * @param string $data | |
| 151 | - * | |
| 152 | - * @throws \RuntimeException | |
| 153 | - * | |
| 154 | - * @return self | |
| 155 | - */ | |
| 156 | - protected static function _decodeDecimalEncoding(string $data): self | |
| 157 | -    { | |
| 158 | - $nr = ord($data[0]) & 0x03; | |
| 159 | -        if (0x03 != $nr) { | |
| 160 | -            throw new \RuntimeException('Only NR3 form supported.'); | |
| 161 | - } | |
| 162 | - $str = substr($data, 1); | |
| 163 | - return new self($str); | |
| 164 | - } | |
| 149 | + /** | |
| 150 | + * @param string $data | |
| 151 | + * | |
| 152 | + * @throws \RuntimeException | |
| 153 | + * | |
| 154 | + * @return self | |
| 155 | + */ | |
| 156 | + protected static function _decodeDecimalEncoding(string $data): self | |
| 157 | +	{ | |
| 158 | + $nr = ord($data[0]) & 0x03; | |
| 159 | +		if (0x03 != $nr) { | |
| 160 | +			throw new \RuntimeException('Only NR3 form supported.'); | |
| 161 | + } | |
| 162 | + $str = substr($data, 1); | |
| 163 | + return new self($str); | |
| 164 | + } | |
| 165 | 165 | |
| 166 | - /** | |
| 167 | - * @todo Implement | |
| 168 | - * | |
| 169 | - * @param string $data | |
| 170 | - */ | |
| 171 | - protected static function _decodeSpecialRealValue(string $data) | |
| 172 | -    { | |
| 173 | -        if (1 != strlen($data)) { | |
| 174 | - throw new DecodeException( | |
| 175 | - 'SpecialRealValue must have one content octet.'); | |
| 176 | - } | |
| 177 | - $byte = ord($data[0]); | |
| 178 | -        if (0x40 == $byte) { // positive infinity | |
| 179 | -            throw new \RuntimeException('PLUS-INFINITY not supported.'); | |
| 180 | - } | |
| 181 | -        if (0x41 == $byte) { // negative infinity | |
| 182 | -            throw new \RuntimeException('MINUS-INFINITY not supported.'); | |
| 183 | - } | |
| 184 | -        throw new DecodeException('Invalid SpecialRealValue encoding.'); | |
| 185 | - } | |
| 166 | + /** | |
| 167 | + * @todo Implement | |
| 168 | + * | |
| 169 | + * @param string $data | |
| 170 | + */ | |
| 171 | + protected static function _decodeSpecialRealValue(string $data) | |
| 172 | +	{ | |
| 173 | +		if (1 != strlen($data)) { | |
| 174 | + throw new DecodeException( | |
| 175 | + 'SpecialRealValue must have one content octet.'); | |
| 176 | + } | |
| 177 | + $byte = ord($data[0]); | |
| 178 | +		if (0x40 == $byte) { // positive infinity | |
| 179 | +			throw new \RuntimeException('PLUS-INFINITY not supported.'); | |
| 180 | + } | |
| 181 | +		if (0x41 == $byte) { // negative infinity | |
| 182 | +			throw new \RuntimeException('MINUS-INFINITY not supported.'); | |
| 183 | + } | |
| 184 | +		throw new DecodeException('Invalid SpecialRealValue encoding.'); | |
| 185 | + } | |
| 186 | 186 | |
| 187 | - /** | |
| 188 | - * Convert decimal number string to NR3 form. | |
| 189 | - * | |
| 190 | - * @param string $str | |
| 191 | - * | |
| 192 | - * @return string | |
| 193 | - */ | |
| 194 | - private static function _decimalToNR3(string $str): string | |
| 195 | -    { | |
| 196 | - // if number is in exponent form | |
| 197 | - /** @var string[] $match */ | |
| 198 | -        if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { | |
| 199 | -            $parts = explode('.', $match[1]); | |
| 200 | - $m = ltrim($parts[0], '0'); | |
| 201 | - $e = intval($match[2]); | |
| 202 | - // if mantissa had decimals | |
| 203 | -            if (2 == count($parts)) { | |
| 204 | - $d = rtrim($parts[1], '0'); | |
| 205 | - $e -= strlen($d); | |
| 206 | - $m .= $d; | |
| 207 | - } | |
| 208 | -        } else { | |
| 209 | - // explode from decimal | |
| 210 | -            $parts = explode('.', $str); | |
| 211 | - $m = ltrim($parts[0], '0'); | |
| 212 | - // if number had decimals | |
| 213 | -            if (2 == count($parts)) { | |
| 214 | - // exponent is negative number of the decimals | |
| 215 | - $e = -strlen($parts[1]); | |
| 216 | - // append decimals to the mantissa | |
| 217 | - $m .= $parts[1]; | |
| 218 | -            } else { | |
| 219 | - $e = 0; | |
| 220 | - } | |
| 221 | - // shift trailing zeroes from the mantissa to the exponent | |
| 222 | -            while ('0' === substr($m, -1)) { | |
| 223 | - ++$e; | |
| 224 | - $m = substr($m, 0, -1); | |
| 225 | - } | |
| 226 | - } | |
| 227 | - /* if exponent is zero, it must be prefixed with a "+" sign | |
| 187 | + /** | |
| 188 | + * Convert decimal number string to NR3 form. | |
| 189 | + * | |
| 190 | + * @param string $str | |
| 191 | + * | |
| 192 | + * @return string | |
| 193 | + */ | |
| 194 | + private static function _decimalToNR3(string $str): string | |
| 195 | +	{ | |
| 196 | + // if number is in exponent form | |
| 197 | + /** @var string[] $match */ | |
| 198 | +		if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { | |
| 199 | +			$parts = explode('.', $match[1]); | |
| 200 | + $m = ltrim($parts[0], '0'); | |
| 201 | + $e = intval($match[2]); | |
| 202 | + // if mantissa had decimals | |
| 203 | +			if (2 == count($parts)) { | |
| 204 | + $d = rtrim($parts[1], '0'); | |
| 205 | + $e -= strlen($d); | |
| 206 | + $m .= $d; | |
| 207 | + } | |
| 208 | +		} else { | |
| 209 | + // explode from decimal | |
| 210 | +			$parts = explode('.', $str); | |
| 211 | + $m = ltrim($parts[0], '0'); | |
| 212 | + // if number had decimals | |
| 213 | +			if (2 == count($parts)) { | |
| 214 | + // exponent is negative number of the decimals | |
| 215 | + $e = -strlen($parts[1]); | |
| 216 | + // append decimals to the mantissa | |
| 217 | + $m .= $parts[1]; | |
| 218 | +			} else { | |
| 219 | + $e = 0; | |
| 220 | + } | |
| 221 | + // shift trailing zeroes from the mantissa to the exponent | |
| 222 | +			while ('0' === substr($m, -1)) { | |
| 223 | + ++$e; | |
| 224 | + $m = substr($m, 0, -1); | |
| 225 | + } | |
| 226 | + } | |
| 227 | + /* if exponent is zero, it must be prefixed with a "+" sign | |
| 228 | 228 | (X.690 07-2002, section 11.3.2.6) */ | 
| 229 | -        if (0 == $e) { | |
| 230 | - $es = '+'; | |
| 231 | -        } else { | |
| 232 | - $es = $e < 0 ? '-' : ''; | |
| 233 | - } | |
| 234 | -        return sprintf('%s.E%s%d', $m, $es, abs($e)); | |
| 235 | - } | |
| 229 | +		if (0 == $e) { | |
| 230 | + $es = '+'; | |
| 231 | +		} else { | |
| 232 | + $es = $e < 0 ? '-' : ''; | |
| 233 | + } | |
| 234 | +		return sprintf('%s.E%s%d', $m, $es, abs($e)); | |
| 235 | + } | |
| 236 | 236 | |
| 237 | - /** | |
| 238 | - * Convert NR3 form number to decimal. | |
| 239 | - * | |
| 240 | - * @param string $str | |
| 241 | - * | |
| 242 | - * @throws \UnexpectedValueException | |
| 243 | - * | |
| 244 | - * @return float | |
| 245 | - */ | |
| 246 | - private static function _nr3ToDecimal(string $str): float | |
| 247 | -    { | |
| 248 | - /** @var string[] $match */ | |
| 249 | -        if (!preg_match(self::NR3_REGEX, $str, $match)) { | |
| 250 | - throw new \UnexpectedValueException( | |
| 251 | -                "'${str}' is not a valid NR3 form real."); | |
| 252 | - } | |
| 253 | - $m = $match[2]; | |
| 254 | - // if number started with minus sign | |
| 255 | - $inv = '-' == $match[1]; | |
| 256 | - $e = intval($match[3]); | |
| 257 | - // positive exponent | |
| 258 | -        if ($e > 0) { | |
| 259 | - // pad with trailing zeroes | |
| 260 | -            $num = $m . str_repeat('0', $e); | |
| 261 | -        } elseif ($e < 0) { | |
| 262 | - // pad with leading zeroes | |
| 263 | -            if (strlen($m) < abs($e)) { | |
| 264 | -                $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; | |
| 265 | - } | |
| 266 | - // insert decimal point | |
| 267 | - $num = substr($m, 0, $e) . '.' . substr($m, $e); | |
| 268 | -        } else { | |
| 269 | - $num = empty($m) ? '0' : $m; | |
| 270 | - } | |
| 271 | - // if number is negative | |
| 272 | -        if ($inv) { | |
| 273 | -            $num = "-${num}"; | |
| 274 | - } | |
| 275 | - return floatval($num); | |
| 276 | - } | |
| 237 | + /** | |
| 238 | + * Convert NR3 form number to decimal. | |
| 239 | + * | |
| 240 | + * @param string $str | |
| 241 | + * | |
| 242 | + * @throws \UnexpectedValueException | |
| 243 | + * | |
| 244 | + * @return float | |
| 245 | + */ | |
| 246 | + private static function _nr3ToDecimal(string $str): float | |
| 247 | +	{ | |
| 248 | + /** @var string[] $match */ | |
| 249 | +		if (!preg_match(self::NR3_REGEX, $str, $match)) { | |
| 250 | + throw new \UnexpectedValueException( | |
| 251 | +				"'${str}' is not a valid NR3 form real."); | |
| 252 | + } | |
| 253 | + $m = $match[2]; | |
| 254 | + // if number started with minus sign | |
| 255 | + $inv = '-' == $match[1]; | |
| 256 | + $e = intval($match[3]); | |
| 257 | + // positive exponent | |
| 258 | +		if ($e > 0) { | |
| 259 | + // pad with trailing zeroes | |
| 260 | +			$num = $m . str_repeat('0', $e); | |
| 261 | +		} elseif ($e < 0) { | |
| 262 | + // pad with leading zeroes | |
| 263 | +			if (strlen($m) < abs($e)) { | |
| 264 | +				$m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; | |
| 265 | + } | |
| 266 | + // insert decimal point | |
| 267 | + $num = substr($m, 0, $e) . '.' . substr($m, $e); | |
| 268 | +		} else { | |
| 269 | + $num = empty($m) ? '0' : $m; | |
| 270 | + } | |
| 271 | + // if number is negative | |
| 272 | +		if ($inv) { | |
| 273 | +			$num = "-${num}"; | |
| 274 | + } | |
| 275 | + return floatval($num); | |
| 276 | + } | |
| 277 | 277 | |
| 278 | - /** | |
| 279 | - * Test that number is valid for this context. | |
| 280 | - * | |
| 281 | - * @param mixed $num | |
| 282 | - * | |
| 283 | - * @return bool | |
| 284 | - */ | |
| 285 | - private static function _validateNumber($num): bool | |
| 286 | -    { | |
| 287 | -        if (!preg_match(self::NR3_REGEX, $num)) { | |
| 288 | - return false; | |
| 289 | - } | |
| 290 | - return true; | |
| 291 | - } | |
| 278 | + /** | |
| 279 | + * Test that number is valid for this context. | |
| 280 | + * | |
| 281 | + * @param mixed $num | |
| 282 | + * | |
| 283 | + * @return bool | |
| 284 | + */ | |
| 285 | + private static function _validateNumber($num): bool | |
| 286 | +	{ | |
| 287 | +		if (!preg_match(self::NR3_REGEX, $num)) { | |
| 288 | + return false; | |
| 289 | + } | |
| 290 | + return true; | |
| 291 | + } | |
| 292 | 292 | } | 
| @@ -11,149 +11,149 @@ | ||
| 11 | 11 | */ | 
| 12 | 12 | class Flags | 
| 13 | 13 |  { | 
| 14 | - /** | |
| 15 | - * Flag octets. | |
| 16 | - * | |
| 17 | - * @var string | |
| 18 | - */ | |
| 19 | - protected $_flags; | |
| 14 | + /** | |
| 15 | + * Flag octets. | |
| 16 | + * | |
| 17 | + * @var string | |
| 18 | + */ | |
| 19 | + protected $_flags; | |
| 20 | 20 | |
| 21 | - /** | |
| 22 | - * Number of flags. | |
| 23 | - * | |
| 24 | - * @var int | |
| 25 | - */ | |
| 26 | - protected $_width; | |
| 21 | + /** | |
| 22 | + * Number of flags. | |
| 23 | + * | |
| 24 | + * @var int | |
| 25 | + */ | |
| 26 | + protected $_width; | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | - * Constructor. | |
| 30 | - * | |
| 31 | - * @param int|string $flags Flags | |
| 32 | - * @param int $width The number of flags. If width is larger than | |
| 33 | - * number of bits in $flags, zeroes are prepended | |
| 34 | - * to flag field. | |
| 35 | - */ | |
| 36 | - public function __construct($flags, int $width) | |
| 37 | -    { | |
| 38 | -        if (!$width) { | |
| 39 | - $this->_flags = ''; | |
| 40 | -        } else { | |
| 41 | - // calculate number of unused bits in last octet | |
| 42 | - $last_octet_bits = $width % 8; | |
| 43 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; | |
| 44 | - $num = gmp_init($flags); | |
| 45 | - // mask bits outside bitfield width | |
| 46 | - $mask = gmp_sub(gmp_init(1) << $width, 1); | |
| 47 | - $num &= $mask; | |
| 48 | - // shift towards MSB if needed | |
| 49 | - $data = gmp_export($num << $unused_bits, 1, | |
| 50 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 51 | -            $octets = unpack('C*', $data); | |
| 52 | -            assert(is_array($octets), new \RuntimeException('unpack() failed')); | |
| 53 | - $bits = count($octets) * 8; | |
| 54 | - // pad with zeroes | |
| 55 | -            while ($bits < $width) { | |
| 56 | - array_unshift($octets, 0); | |
| 57 | - $bits += 8; | |
| 58 | - } | |
| 59 | -            $this->_flags = pack('C*', ...$octets); | |
| 60 | - } | |
| 61 | - $this->_width = $width; | |
| 62 | - } | |
| 28 | + /** | |
| 29 | + * Constructor. | |
| 30 | + * | |
| 31 | + * @param int|string $flags Flags | |
| 32 | + * @param int $width The number of flags. If width is larger than | |
| 33 | + * number of bits in $flags, zeroes are prepended | |
| 34 | + * to flag field. | |
| 35 | + */ | |
| 36 | + public function __construct($flags, int $width) | |
| 37 | +	{ | |
| 38 | +		if (!$width) { | |
| 39 | + $this->_flags = ''; | |
| 40 | +		} else { | |
| 41 | + // calculate number of unused bits in last octet | |
| 42 | + $last_octet_bits = $width % 8; | |
| 43 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; | |
| 44 | + $num = gmp_init($flags); | |
| 45 | + // mask bits outside bitfield width | |
| 46 | + $mask = gmp_sub(gmp_init(1) << $width, 1); | |
| 47 | + $num &= $mask; | |
| 48 | + // shift towards MSB if needed | |
| 49 | + $data = gmp_export($num << $unused_bits, 1, | |
| 50 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 51 | +			$octets = unpack('C*', $data); | |
| 52 | +			assert(is_array($octets), new \RuntimeException('unpack() failed')); | |
| 53 | + $bits = count($octets) * 8; | |
| 54 | + // pad with zeroes | |
| 55 | +			while ($bits < $width) { | |
| 56 | + array_unshift($octets, 0); | |
| 57 | + $bits += 8; | |
| 58 | + } | |
| 59 | +			$this->_flags = pack('C*', ...$octets); | |
| 60 | + } | |
| 61 | + $this->_width = $width; | |
| 62 | + } | |
| 63 | 63 | |
| 64 | - /** | |
| 65 | - * Initialize from BitString. | |
| 66 | - * | |
| 67 | - * @param BitString $bs | |
| 68 | - * @param int $width | |
| 69 | - * | |
| 70 | - * @return self | |
| 71 | - */ | |
| 72 | - public static function fromBitString(BitString $bs, int $width): self | |
| 73 | -    { | |
| 74 | - $num_bits = $bs->numBits(); | |
| 75 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 76 | - $num >>= $bs->unusedBits(); | |
| 77 | -        if ($num_bits < $width) { | |
| 78 | - $num <<= ($width - $num_bits); | |
| 79 | - } | |
| 80 | - return new self(gmp_strval($num, 10), $width); | |
| 81 | - } | |
| 64 | + /** | |
| 65 | + * Initialize from BitString. | |
| 66 | + * | |
| 67 | + * @param BitString $bs | |
| 68 | + * @param int $width | |
| 69 | + * | |
| 70 | + * @return self | |
| 71 | + */ | |
| 72 | + public static function fromBitString(BitString $bs, int $width): self | |
| 73 | +	{ | |
| 74 | + $num_bits = $bs->numBits(); | |
| 75 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 76 | + $num >>= $bs->unusedBits(); | |
| 77 | +		if ($num_bits < $width) { | |
| 78 | + $num <<= ($width - $num_bits); | |
| 79 | + } | |
| 80 | + return new self(gmp_strval($num, 10), $width); | |
| 81 | + } | |
| 82 | 82 | |
| 83 | - /** | |
| 84 | - * Check whether a bit at given index is set. | |
| 85 | - * | |
| 86 | - * Index 0 is the leftmost bit. | |
| 87 | - * | |
| 88 | - * @param int $idx | |
| 89 | - * | |
| 90 | - * @throws \OutOfBoundsException | |
| 91 | - * | |
| 92 | - * @return bool | |
| 93 | - */ | |
| 94 | - public function test(int $idx): bool | |
| 95 | -    { | |
| 96 | -        if ($idx >= $this->_width) { | |
| 97 | -            throw new \OutOfBoundsException('Index is out of bounds.'); | |
| 98 | - } | |
| 99 | - // octet index | |
| 100 | - $oi = (int) floor($idx / 8); | |
| 101 | - $byte = $this->_flags[$oi]; | |
| 102 | - // bit index | |
| 103 | - $bi = $idx % 8; | |
| 104 | - // index 0 is the most significant bit in byte | |
| 105 | - $mask = 0x01 << (7 - $bi); | |
| 106 | - return (ord($byte) & $mask) > 0; | |
| 107 | - } | |
| 83 | + /** | |
| 84 | + * Check whether a bit at given index is set. | |
| 85 | + * | |
| 86 | + * Index 0 is the leftmost bit. | |
| 87 | + * | |
| 88 | + * @param int $idx | |
| 89 | + * | |
| 90 | + * @throws \OutOfBoundsException | |
| 91 | + * | |
| 92 | + * @return bool | |
| 93 | + */ | |
| 94 | + public function test(int $idx): bool | |
| 95 | +	{ | |
| 96 | +		if ($idx >= $this->_width) { | |
| 97 | +			throw new \OutOfBoundsException('Index is out of bounds.'); | |
| 98 | + } | |
| 99 | + // octet index | |
| 100 | + $oi = (int) floor($idx / 8); | |
| 101 | + $byte = $this->_flags[$oi]; | |
| 102 | + // bit index | |
| 103 | + $bi = $idx % 8; | |
| 104 | + // index 0 is the most significant bit in byte | |
| 105 | + $mask = 0x01 << (7 - $bi); | |
| 106 | + return (ord($byte) & $mask) > 0; | |
| 107 | + } | |
| 108 | 108 | |
| 109 | - /** | |
| 110 | - * Get flags as an octet string. | |
| 111 | - * | |
| 112 | - * Zeroes are appended to the last octet if width is not divisible by 8. | |
| 113 | - * | |
| 114 | - * @return string | |
| 115 | - */ | |
| 116 | - public function string(): string | |
| 117 | -    { | |
| 118 | - return $this->_flags; | |
| 119 | - } | |
| 109 | + /** | |
| 110 | + * Get flags as an octet string. | |
| 111 | + * | |
| 112 | + * Zeroes are appended to the last octet if width is not divisible by 8. | |
| 113 | + * | |
| 114 | + * @return string | |
| 115 | + */ | |
| 116 | + public function string(): string | |
| 117 | +	{ | |
| 118 | + return $this->_flags; | |
| 119 | + } | |
| 120 | 120 | |
| 121 | - /** | |
| 122 | - * Get flags as a base 10 integer. | |
| 123 | - * | |
| 124 | - * @return string Integer as a string | |
| 125 | - */ | |
| 126 | - public function number(): string | |
| 127 | -    { | |
| 128 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 129 | - $last_octet_bits = $this->_width % 8; | |
| 130 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; | |
| 131 | - $num >>= $unused_bits; | |
| 132 | - return gmp_strval($num, 10); | |
| 133 | - } | |
| 121 | + /** | |
| 122 | + * Get flags as a base 10 integer. | |
| 123 | + * | |
| 124 | + * @return string Integer as a string | |
| 125 | + */ | |
| 126 | + public function number(): string | |
| 127 | +	{ | |
| 128 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); | |
| 129 | + $last_octet_bits = $this->_width % 8; | |
| 130 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; | |
| 131 | + $num >>= $unused_bits; | |
| 132 | + return gmp_strval($num, 10); | |
| 133 | + } | |
| 134 | 134 | |
| 135 | - /** | |
| 136 | - * Get flags as an integer. | |
| 137 | - * | |
| 138 | - * @return int | |
| 139 | - */ | |
| 140 | - public function intNumber(): int | |
| 141 | -    { | |
| 142 | - $num = new BigInt($this->number()); | |
| 143 | - return $num->intVal(); | |
| 144 | - } | |
| 135 | + /** | |
| 136 | + * Get flags as an integer. | |
| 137 | + * | |
| 138 | + * @return int | |
| 139 | + */ | |
| 140 | + public function intNumber(): int | |
| 141 | +	{ | |
| 142 | + $num = new BigInt($this->number()); | |
| 143 | + return $num->intVal(); | |
| 144 | + } | |
| 145 | 145 | |
| 146 | - /** | |
| 147 | - * Get flags as a BitString. | |
| 148 | - * | |
| 149 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. | |
| 150 | - * | |
| 151 | - * @return BitString | |
| 152 | - */ | |
| 153 | - public function bitString(): BitString | |
| 154 | -    { | |
| 155 | - $last_octet_bits = $this->_width % 8; | |
| 156 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; | |
| 157 | - return new BitString($this->_flags, $unused_bits); | |
| 158 | - } | |
| 146 | + /** | |
| 147 | + * Get flags as a BitString. | |
| 148 | + * | |
| 149 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. | |
| 150 | + * | |
| 151 | + * @return BitString | |
| 152 | + */ | |
| 153 | + public function bitString(): BitString | |
| 154 | +	{ | |
| 155 | + $last_octet_bits = $this->_width % 8; | |
| 156 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; | |
| 157 | + return new BitString($this->_flags, $unused_bits); | |
| 158 | + } | |
| 159 | 159 | } | 
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php | 
| 2 | 2 | |
| 3 | -declare(strict_types = 1); | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | 5 | namespace Sop\ASN1\Util; | 
| 6 | 6 | |