@@ -10,25 +10,25 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class IA5String extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_IA5_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_IA5_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - return preg_match('/[^\x01-\x7f]/', $string) == 0; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + return preg_match('/[^\x01-\x7f]/', $string) == 0; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -10,26 +10,26 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class GeneralString extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_GENERAL_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_GENERAL_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - // allow everything |
|
| 33 | - return true; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + // allow everything |
|
| 33 | + return true; |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -7,16 +7,16 @@ |
||
| 7 | 7 | |
| 8 | 8 | class CharacterString extends PrimitiveString |
| 9 | 9 | { |
| 10 | - use UniversalClass; |
|
| 10 | + use UniversalClass; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Constructor. |
|
| 14 | - * |
|
| 15 | - * @param string $string |
|
| 16 | - */ |
|
| 17 | - public function __construct($string) |
|
| 18 | - { |
|
| 19 | - $this->_typeTag = self::TYPE_CHARACTER_STRING; |
|
| 20 | - parent::__construct($string); |
|
| 21 | - } |
|
| 12 | + /** |
|
| 13 | + * Constructor. |
|
| 14 | + * |
|
| 15 | + * @param string $string |
|
| 16 | + */ |
|
| 17 | + public function __construct($string) |
|
| 18 | + { |
|
| 19 | + $this->_typeTag = self::TYPE_CHARACTER_STRING; |
|
| 20 | + parent::__construct($string); |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -14,41 +14,41 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class NullType extends Element |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 18 | - use PrimitiveType; |
|
| 17 | + use UniversalClass; |
|
| 18 | + use PrimitiveType; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Constructor. |
|
| 22 | - */ |
|
| 23 | - public function __construct() |
|
| 24 | - { |
|
| 25 | - $this->_typeTag = self::TYPE_NULL; |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * Constructor. |
|
| 22 | + */ |
|
| 23 | + public function __construct() |
|
| 24 | + { |
|
| 25 | + $this->_typeTag = self::TYPE_NULL; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _encodedContentDER() |
|
| 33 | - { |
|
| 34 | - return ""; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _encodedContentDER() |
|
| 33 | + { |
|
| 34 | + return ""; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * |
|
| 39 | - * {@inheritdoc} |
|
| 40 | - * @return self |
|
| 41 | - */ |
|
| 42 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 43 | - &$offset) |
|
| 44 | - { |
|
| 45 | - $idx = $offset; |
|
| 46 | - if (!$identifier->isPrimitive()) { |
|
| 47 | - throw new DecodeException("Null value must be primitive."); |
|
| 48 | - } |
|
| 49 | - // null type has always zero length |
|
| 50 | - Length::expectFromDER($data, $idx, 0); |
|
| 51 | - $offset = $idx; |
|
| 52 | - return new self(); |
|
| 53 | - } |
|
| 37 | + /** |
|
| 38 | + * |
|
| 39 | + * {@inheritdoc} |
|
| 40 | + * @return self |
|
| 41 | + */ |
|
| 42 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 43 | + &$offset) |
|
| 44 | + { |
|
| 45 | + $idx = $offset; |
|
| 46 | + if (!$identifier->isPrimitive()) { |
|
| 47 | + throw new DecodeException("Null value must be primitive."); |
|
| 48 | + } |
|
| 49 | + // null type has always zero length |
|
| 50 | + Length::expectFromDER($data, $idx, 0); |
|
| 51 | + $offset = $idx; |
|
| 52 | + return new self(); |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -13,29 +13,29 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class BMPString extends PrimitiveString |
| 15 | 15 | { |
| 16 | - use UniversalClass; |
|
| 16 | + use UniversalClass; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @param string $string |
|
| 22 | - */ |
|
| 23 | - public function __construct($string) |
|
| 24 | - { |
|
| 25 | - $this->_typeTag = self::TYPE_BMP_STRING; |
|
| 26 | - parent::__construct($string); |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @param string $string |
|
| 22 | + */ |
|
| 23 | + public function __construct($string) |
|
| 24 | + { |
|
| 25 | + $this->_typeTag = self::TYPE_BMP_STRING; |
|
| 26 | + parent::__construct($string); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - */ |
|
| 33 | - protected function _validateString($string) |
|
| 34 | - { |
|
| 35 | - // UCS-2 has fixed with of 2 octets (16 bits) |
|
| 36 | - if (strlen($string) % 2 !== 0) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 29 | + /** |
|
| 30 | + * |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + */ |
|
| 33 | + protected function _validateString($string) |
|
| 34 | + { |
|
| 35 | + // UCS-2 has fixed with of 2 octets (16 bits) |
|
| 36 | + if (strlen($string) % 2 !== 0) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -10,25 +10,25 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class VisibleString extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -10,39 +10,39 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class RelativeOID extends ObjectIdentifier |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Constructor. |
|
| 15 | - * |
|
| 16 | - * @param string $oid OID in dotted format |
|
| 17 | - */ |
|
| 18 | - public function __construct($oid) |
|
| 19 | - { |
|
| 20 | - assert('is_string($oid)', "got " . gettype($oid)); |
|
| 21 | - $this->_oid = $oid; |
|
| 22 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 23 | - } |
|
| 13 | + /** |
|
| 14 | + * Constructor. |
|
| 15 | + * |
|
| 16 | + * @param string $oid OID in dotted format |
|
| 17 | + */ |
|
| 18 | + public function __construct($oid) |
|
| 19 | + { |
|
| 20 | + assert('is_string($oid)', "got " . gettype($oid)); |
|
| 21 | + $this->_oid = $oid; |
|
| 22 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - protected function _encodedContentDER() |
|
| 30 | - { |
|
| 31 | - return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + protected function _encodedContentDER() |
|
| 30 | + { |
|
| 31 | + return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - * @return self |
|
| 38 | - */ |
|
| 39 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 40 | - &$offset) |
|
| 41 | - { |
|
| 42 | - $idx = $offset; |
|
| 43 | - $len = Length::expectFromDER($data, $idx)->length(); |
|
| 44 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 45 | - $offset = $idx + $len; |
|
| 46 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 47 | - } |
|
| 34 | + /** |
|
| 35 | + * |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + * @return self |
|
| 38 | + */ |
|
| 39 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 40 | + &$offset) |
|
| 41 | + { |
|
| 42 | + $idx = $offset; |
|
| 43 | + $len = Length::expectFromDER($data, $idx)->length(); |
|
| 44 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 45 | + $offset = $idx + $len; |
|
| 46 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -10,26 +10,26 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class VideotexString extends PrimitiveString |
| 12 | 12 | { |
| 13 | - use UniversalClass; |
|
| 13 | + use UniversalClass; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Constructor. |
|
| 17 | - * |
|
| 18 | - * @param string $string |
|
| 19 | - */ |
|
| 20 | - public function __construct($string) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_VIDEOTEX_STRING; |
|
| 23 | - parent::__construct($string); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor. |
|
| 17 | + * |
|
| 18 | + * @param string $string |
|
| 19 | + */ |
|
| 20 | + public function __construct($string) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_VIDEOTEX_STRING; |
|
| 23 | + parent::__construct($string); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - protected function _validateString($string) |
|
| 31 | - { |
|
| 32 | - // allow everything |
|
| 33 | - return true; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + protected function _validateString($string) |
|
| 31 | + { |
|
| 32 | + // allow everything |
|
| 33 | + return true; |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -14,194 +14,194 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class BitString extends StringType |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 18 | - use PrimitiveType; |
|
| 17 | + use UniversalClass; |
|
| 18 | + use PrimitiveType; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Number of unused bits in the last octet. |
|
| 22 | - * |
|
| 23 | - * @var int $_unusedBits |
|
| 24 | - */ |
|
| 25 | - protected $_unusedBits; |
|
| 20 | + /** |
|
| 21 | + * Number of unused bits in the last octet. |
|
| 22 | + * |
|
| 23 | + * @var int $_unusedBits |
|
| 24 | + */ |
|
| 25 | + protected $_unusedBits; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param string $string Content octets |
|
| 31 | - * @param int $unused_bits Number of unused bits in the last octet |
|
| 32 | - */ |
|
| 33 | - public function __construct($string, $unused_bits = 0) |
|
| 34 | - { |
|
| 35 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 36 | - parent::__construct($string); |
|
| 37 | - $this->_unusedBits = $unused_bits; |
|
| 38 | - } |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param string $string Content octets |
|
| 31 | + * @param int $unused_bits Number of unused bits in the last octet |
|
| 32 | + */ |
|
| 33 | + public function __construct($string, $unused_bits = 0) |
|
| 34 | + { |
|
| 35 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
| 36 | + parent::__construct($string); |
|
| 37 | + $this->_unusedBits = $unused_bits; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Get the number of bits in the string. |
|
| 42 | - * |
|
| 43 | - * @return int |
|
| 44 | - */ |
|
| 45 | - public function numBits() |
|
| 46 | - { |
|
| 47 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 48 | - } |
|
| 40 | + /** |
|
| 41 | + * Get the number of bits in the string. |
|
| 42 | + * |
|
| 43 | + * @return int |
|
| 44 | + */ |
|
| 45 | + public function numBits() |
|
| 46 | + { |
|
| 47 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Get the number of unused bits in the last octet of the string. |
|
| 52 | - * |
|
| 53 | - * @return int |
|
| 54 | - */ |
|
| 55 | - public function unusedBits() |
|
| 56 | - { |
|
| 57 | - return $this->_unusedBits; |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * Get the number of unused bits in the last octet of the string. |
|
| 52 | + * |
|
| 53 | + * @return int |
|
| 54 | + */ |
|
| 55 | + public function unusedBits() |
|
| 56 | + { |
|
| 57 | + return $this->_unusedBits; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Test whether bit is set. |
|
| 62 | - * |
|
| 63 | - * @param int $idx Bit index. |
|
| 64 | - * Most significant bit of the first octet is index 0. |
|
| 65 | - * @return boolean |
|
| 66 | - */ |
|
| 67 | - public function testBit($idx) |
|
| 68 | - { |
|
| 69 | - // octet index |
|
| 70 | - $oi = (int) floor($idx / 8); |
|
| 71 | - // if octet is outside range |
|
| 72 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 73 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 74 | - } |
|
| 75 | - // bit index |
|
| 76 | - $bi = $idx % 8; |
|
| 77 | - // if tested bit is last octet's unused bit |
|
| 78 | - if ($oi == strlen($this->_string) - 1) { |
|
| 79 | - if ($bi >= 8 - $this->_unusedBits) { |
|
| 80 | - throw new \OutOfBoundsException("Index refers to an unused bit."); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - $byte = $this->_string[$oi]; |
|
| 84 | - // index 0 is the most significant bit in byte |
|
| 85 | - $mask = 0x01 << (7 - $bi); |
|
| 86 | - return (ord($byte) & $mask) > 0; |
|
| 87 | - } |
|
| 60 | + /** |
|
| 61 | + * Test whether bit is set. |
|
| 62 | + * |
|
| 63 | + * @param int $idx Bit index. |
|
| 64 | + * Most significant bit of the first octet is index 0. |
|
| 65 | + * @return boolean |
|
| 66 | + */ |
|
| 67 | + public function testBit($idx) |
|
| 68 | + { |
|
| 69 | + // octet index |
|
| 70 | + $oi = (int) floor($idx / 8); |
|
| 71 | + // if octet is outside range |
|
| 72 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
| 73 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
| 74 | + } |
|
| 75 | + // bit index |
|
| 76 | + $bi = $idx % 8; |
|
| 77 | + // if tested bit is last octet's unused bit |
|
| 78 | + if ($oi == strlen($this->_string) - 1) { |
|
| 79 | + if ($bi >= 8 - $this->_unusedBits) { |
|
| 80 | + throw new \OutOfBoundsException("Index refers to an unused bit."); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + $byte = $this->_string[$oi]; |
|
| 84 | + // index 0 is the most significant bit in byte |
|
| 85 | + $mask = 0x01 << (7 - $bi); |
|
| 86 | + return (ord($byte) & $mask) > 0; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Get range of bits. |
|
| 91 | - * |
|
| 92 | - * @param int $start Index of first bit |
|
| 93 | - * @param int $length Number of bits in range |
|
| 94 | - * @throws \OutOfBoundsException |
|
| 95 | - * @return number Integer of $length bits |
|
| 96 | - */ |
|
| 97 | - public function range($start, $length) |
|
| 98 | - { |
|
| 99 | - if (!$length) { |
|
| 100 | - return 0; |
|
| 101 | - } |
|
| 102 | - if ($start + $length > $this->numBits()) { |
|
| 103 | - throw new \OutOfBoundsException("Not enough bits."); |
|
| 104 | - } |
|
| 105 | - $bits = gmp_init(0); |
|
| 106 | - $idx = $start; |
|
| 107 | - $end = $start + $length; |
|
| 108 | - while (true) { |
|
| 109 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
| 110 | - $bits |= $bit; |
|
| 111 | - if (++$idx >= $end) { |
|
| 112 | - break; |
|
| 113 | - } |
|
| 114 | - $bits <<= 1; |
|
| 115 | - } |
|
| 116 | - return gmp_strval($bits, 10); |
|
| 117 | - } |
|
| 89 | + /** |
|
| 90 | + * Get range of bits. |
|
| 91 | + * |
|
| 92 | + * @param int $start Index of first bit |
|
| 93 | + * @param int $length Number of bits in range |
|
| 94 | + * @throws \OutOfBoundsException |
|
| 95 | + * @return number Integer of $length bits |
|
| 96 | + */ |
|
| 97 | + public function range($start, $length) |
|
| 98 | + { |
|
| 99 | + if (!$length) { |
|
| 100 | + return 0; |
|
| 101 | + } |
|
| 102 | + if ($start + $length > $this->numBits()) { |
|
| 103 | + throw new \OutOfBoundsException("Not enough bits."); |
|
| 104 | + } |
|
| 105 | + $bits = gmp_init(0); |
|
| 106 | + $idx = $start; |
|
| 107 | + $end = $start + $length; |
|
| 108 | + while (true) { |
|
| 109 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
| 110 | + $bits |= $bit; |
|
| 111 | + if (++$idx >= $end) { |
|
| 112 | + break; |
|
| 113 | + } |
|
| 114 | + $bits <<= 1; |
|
| 115 | + } |
|
| 116 | + return gmp_strval($bits, 10); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Get a copy of the bit string with trailing zeroes removed. |
|
| 121 | - * |
|
| 122 | - * @return self |
|
| 123 | - */ |
|
| 124 | - public function withoutTrailingZeroes() |
|
| 125 | - { |
|
| 126 | - // if bit string was empty |
|
| 127 | - if (!strlen($this->_string)) { |
|
| 128 | - return new self(""); |
|
| 129 | - } |
|
| 130 | - $bits = $this->_string; |
|
| 131 | - // count number of empty trailing octets |
|
| 132 | - $unused_octets = 0; |
|
| 133 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 134 | - if ($bits[$idx] != "\x0") { |
|
| 135 | - break; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - // strip trailing octets |
|
| 139 | - if ($unused_octets) { |
|
| 140 | - $bits = substr($bits, 0, -$unused_octets); |
|
| 141 | - } |
|
| 142 | - // if bit string was full of zeroes |
|
| 143 | - if (!strlen($bits)) { |
|
| 144 | - return new self(""); |
|
| 145 | - } |
|
| 146 | - // count number of trailing zeroes in the last octet |
|
| 147 | - $unused_bits = 0; |
|
| 148 | - $byte = ord($bits[strlen($bits) - 1]); |
|
| 149 | - while (!($byte & 0x01)) { |
|
| 150 | - $unused_bits++; |
|
| 151 | - $byte >>= 1; |
|
| 152 | - } |
|
| 153 | - return new self($bits, $unused_bits); |
|
| 154 | - } |
|
| 119 | + /** |
|
| 120 | + * Get a copy of the bit string with trailing zeroes removed. |
|
| 121 | + * |
|
| 122 | + * @return self |
|
| 123 | + */ |
|
| 124 | + public function withoutTrailingZeroes() |
|
| 125 | + { |
|
| 126 | + // if bit string was empty |
|
| 127 | + if (!strlen($this->_string)) { |
|
| 128 | + return new self(""); |
|
| 129 | + } |
|
| 130 | + $bits = $this->_string; |
|
| 131 | + // count number of empty trailing octets |
|
| 132 | + $unused_octets = 0; |
|
| 133 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
| 134 | + if ($bits[$idx] != "\x0") { |
|
| 135 | + break; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + // strip trailing octets |
|
| 139 | + if ($unused_octets) { |
|
| 140 | + $bits = substr($bits, 0, -$unused_octets); |
|
| 141 | + } |
|
| 142 | + // if bit string was full of zeroes |
|
| 143 | + if (!strlen($bits)) { |
|
| 144 | + return new self(""); |
|
| 145 | + } |
|
| 146 | + // count number of trailing zeroes in the last octet |
|
| 147 | + $unused_bits = 0; |
|
| 148 | + $byte = ord($bits[strlen($bits) - 1]); |
|
| 149 | + while (!($byte & 0x01)) { |
|
| 150 | + $unused_bits++; |
|
| 151 | + $byte >>= 1; |
|
| 152 | + } |
|
| 153 | + return new self($bits, $unused_bits); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * |
|
| 158 | - * {@inheritdoc} |
|
| 159 | - */ |
|
| 160 | - protected function _encodedContentDER() |
|
| 161 | - { |
|
| 162 | - $der = chr($this->_unusedBits); |
|
| 163 | - $der .= $this->_string; |
|
| 164 | - if ($this->_unusedBits) { |
|
| 165 | - $octet = $der[strlen($der) - 1]; |
|
| 166 | - // set unused bits to zero |
|
| 167 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 168 | - $der[strlen($der) - 1] = $octet; |
|
| 169 | - } |
|
| 170 | - return $der; |
|
| 171 | - } |
|
| 156 | + /** |
|
| 157 | + * |
|
| 158 | + * {@inheritdoc} |
|
| 159 | + */ |
|
| 160 | + protected function _encodedContentDER() |
|
| 161 | + { |
|
| 162 | + $der = chr($this->_unusedBits); |
|
| 163 | + $der .= $this->_string; |
|
| 164 | + if ($this->_unusedBits) { |
|
| 165 | + $octet = $der[strlen($der) - 1]; |
|
| 166 | + // set unused bits to zero |
|
| 167 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
| 168 | + $der[strlen($der) - 1] = $octet; |
|
| 169 | + } |
|
| 170 | + return $der; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * |
|
| 175 | - * {@inheritdoc} |
|
| 176 | - * @return self |
|
| 177 | - */ |
|
| 178 | - protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 179 | - &$offset) |
|
| 180 | - { |
|
| 181 | - $idx = $offset; |
|
| 182 | - $length = Length::expectFromDER($data, $idx); |
|
| 183 | - if ($length->length() < 1) { |
|
| 184 | - throw new DecodeException("Bit string length must be at least 1."); |
|
| 185 | - } |
|
| 186 | - $unused_bits = ord($data[$idx++]); |
|
| 187 | - if ($unused_bits > 7) { |
|
| 188 | - throw new DecodeException( |
|
| 189 | - "Unused bits in a bit string must be less than 8."); |
|
| 190 | - } |
|
| 191 | - $str_len = $length->length() - 1; |
|
| 192 | - if ($str_len) { |
|
| 193 | - $str = substr($data, $idx, $str_len); |
|
| 194 | - if ($unused_bits) { |
|
| 195 | - $mask = (1 << $unused_bits) - 1; |
|
| 196 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 197 | - throw new DecodeException( |
|
| 198 | - "DER encoded bit string must have zero padding."); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } else { |
|
| 202 | - $str = ""; |
|
| 203 | - } |
|
| 204 | - $offset = $idx + $str_len; |
|
| 205 | - return new self($str, $unused_bits); |
|
| 206 | - } |
|
| 173 | + /** |
|
| 174 | + * |
|
| 175 | + * {@inheritdoc} |
|
| 176 | + * @return self |
|
| 177 | + */ |
|
| 178 | + protected static function _decodeFromDER(Identifier $identifier, $data, |
|
| 179 | + &$offset) |
|
| 180 | + { |
|
| 181 | + $idx = $offset; |
|
| 182 | + $length = Length::expectFromDER($data, $idx); |
|
| 183 | + if ($length->length() < 1) { |
|
| 184 | + throw new DecodeException("Bit string length must be at least 1."); |
|
| 185 | + } |
|
| 186 | + $unused_bits = ord($data[$idx++]); |
|
| 187 | + if ($unused_bits > 7) { |
|
| 188 | + throw new DecodeException( |
|
| 189 | + "Unused bits in a bit string must be less than 8."); |
|
| 190 | + } |
|
| 191 | + $str_len = $length->length() - 1; |
|
| 192 | + if ($str_len) { |
|
| 193 | + $str = substr($data, $idx, $str_len); |
|
| 194 | + if ($unused_bits) { |
|
| 195 | + $mask = (1 << $unused_bits) - 1; |
|
| 196 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
| 197 | + throw new DecodeException( |
|
| 198 | + "DER encoded bit string must have zero padding."); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } else { |
|
| 202 | + $str = ""; |
|
| 203 | + } |
|
| 204 | + $offset = $idx + $str_len; |
|
| 205 | + return new self($str, $unused_bits); |
|
| 206 | + } |
|
| 207 | 207 | } |