@@ -14,29 +14,29 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class UniversalString extends PrimitiveString |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string |
|
| 23 | - */ |
|
| 24 | - public function __construct(string $string) |
|
| 25 | - { |
|
| 26 | - $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | - parent::__construct($string); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string |
|
| 23 | + */ |
|
| 24 | + public function __construct(string $string) |
|
| 25 | + { |
|
| 26 | + $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
| 27 | + parent::__construct($string); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - protected function _validateString(string $string): bool |
|
| 35 | - { |
|
| 36 | - // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 37 | - if (strlen($string) % 4 != 0) { |
|
| 38 | - return false; |
|
| 39 | - } |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + protected function _validateString(string $string): bool |
|
| 35 | + { |
|
| 36 | + // UCS-4 has fixed with of 4 octets (32 bits) |
|
| 37 | + if (strlen($string) % 4 != 0) { |
|
| 38 | + return false; |
|
| 39 | + } |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -14,25 +14,25 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class UTF8String extends PrimitiveString |
| 16 | 16 | { |
| 17 | - use UniversalClass; |
|
| 17 | + use UniversalClass; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param string $string |
|
| 23 | - */ |
|
| 24 | - public function __construct(string $string) |
|
| 25 | - { |
|
| 26 | - $this->_typeTag = self::TYPE_UTF8_STRING; |
|
| 27 | - parent::__construct($string); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param string $string |
|
| 23 | + */ |
|
| 24 | + public function __construct(string $string) |
|
| 25 | + { |
|
| 26 | + $this->_typeTag = self::TYPE_UTF8_STRING; |
|
| 27 | + parent::__construct($string); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * |
|
| 32 | - * {@inheritdoc} |
|
| 33 | - */ |
|
| 34 | - protected function _validateString(string $string): bool |
|
| 35 | - { |
|
| 36 | - return mb_check_encoding($string, "UTF-8"); |
|
| 37 | - } |
|
| 30 | + /** |
|
| 31 | + * |
|
| 32 | + * {@inheritdoc} |
|
| 33 | + */ |
|
| 34 | + protected function _validateString(string $string): bool |
|
| 35 | + { |
|
| 36 | + return mb_check_encoding($string, "UTF-8"); |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -12,27 +12,27 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class T61String extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_T61_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_T61_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - // allow everything since there's literally |
|
| 35 | - // thousands of allowed characters (16 bit composed characters) |
|
| 36 | - return true; |
|
| 37 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + // allow everything since there's literally |
|
| 35 | + // thousands of allowed characters (16 bit composed characters) |
|
| 36 | + return true; |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class VisibleString extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -12,26 +12,26 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class ObjectDescriptor extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $descriptor |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $descriptor) |
|
| 23 | - { |
|
| 24 | - $this->_string = $descriptor; |
|
| 25 | - $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $descriptor |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $descriptor) |
|
| 23 | + { |
|
| 24 | + $this->_string = $descriptor; |
|
| 25 | + $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Get the object descriptor. |
|
| 30 | - * |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function descriptor(): string |
|
| 34 | - { |
|
| 35 | - return $this->_string; |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * Get the object descriptor. |
|
| 30 | + * |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function descriptor(): string |
|
| 34 | + { |
|
| 35 | + return $this->_string; |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -12,25 +12,25 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class NumericString extends PrimitiveString |
| 14 | 14 | { |
| 15 | - use UniversalClass; |
|
| 15 | + use UniversalClass; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Constructor. |
|
| 19 | - * |
|
| 20 | - * @param string $string |
|
| 21 | - */ |
|
| 22 | - public function __construct(string $string) |
|
| 23 | - { |
|
| 24 | - $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
| 25 | - parent::__construct($string); |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * Constructor. |
|
| 19 | + * |
|
| 20 | + * @param string $string |
|
| 21 | + */ |
|
| 22 | + public function __construct(string $string) |
|
| 23 | + { |
|
| 24 | + $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
| 25 | + parent::__construct($string); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * |
|
| 30 | - * {@inheritdoc} |
|
| 31 | - */ |
|
| 32 | - protected function _validateString(string $string): bool |
|
| 33 | - { |
|
| 34 | - return preg_match('/[^0-9 ]/', $string) == 0; |
|
| 35 | - } |
|
| 28 | + /** |
|
| 29 | + * |
|
| 30 | + * {@inheritdoc} |
|
| 31 | + */ |
|
| 32 | + protected function _validateString(string $string): bool |
|
| 33 | + { |
|
| 34 | + return preg_match('/[^0-9 ]/', $string) == 0; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -11,48 +11,48 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class StringType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * String value. |
|
| 16 | - * |
|
| 17 | - * @var string $_string |
|
| 18 | - */ |
|
| 19 | - protected $_string; |
|
| 14 | + /** |
|
| 15 | + * String value. |
|
| 16 | + * |
|
| 17 | + * @var string $_string |
|
| 18 | + */ |
|
| 19 | + protected $_string; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param string $string |
|
| 25 | - * @throws \InvalidArgumentException |
|
| 26 | - */ |
|
| 27 | - public function __construct(string $string) |
|
| 28 | - { |
|
| 29 | - if (!$this->_validateString($string)) { |
|
| 30 | - throw new \InvalidArgumentException( |
|
| 31 | - sprintf("Not a valid %s string.", |
|
| 32 | - self::tagToName($this->_typeTag))); |
|
| 33 | - } |
|
| 34 | - $this->_string = $string; |
|
| 35 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param string $string |
|
| 25 | + * @throws \InvalidArgumentException |
|
| 26 | + */ |
|
| 27 | + public function __construct(string $string) |
|
| 28 | + { |
|
| 29 | + if (!$this->_validateString($string)) { |
|
| 30 | + throw new \InvalidArgumentException( |
|
| 31 | + sprintf("Not a valid %s string.", |
|
| 32 | + self::tagToName($this->_typeTag))); |
|
| 33 | + } |
|
| 34 | + $this->_string = $string; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get the string value. |
|
| 39 | - * |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function string(): string |
|
| 43 | - { |
|
| 44 | - return $this->_string; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Get the string value. |
|
| 39 | + * |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function string(): string |
|
| 43 | + { |
|
| 44 | + return $this->_string; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Check whether string is valid for the concrete type. |
|
| 49 | - * |
|
| 50 | - * @param string $string |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - protected function _validateString(string $string): bool |
|
| 54 | - { |
|
| 55 | - // Override in derived classes |
|
| 56 | - return true; |
|
| 57 | - } |
|
| 47 | + /** |
|
| 48 | + * Check whether string is valid for the concrete type. |
|
| 49 | + * |
|
| 50 | + * @param string $string |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + protected function _validateString(string $string): bool |
|
| 54 | + { |
|
| 55 | + // Override in derived classes |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -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 | } |