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