| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the PHPASN1 library. |
| 4 |
* |
| 5 |
* Copyright © Friedrich Große <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WP_DEFENDER_VENDOR\FG\ASN1\Universal; |
| 12 |
|
| 13 |
use Exception; |
| 14 |
use WP_DEFENDER_VENDOR\FG\ASN1\Exception\ParserException; |
| 15 |
use WP_DEFENDER_VENDOR\FG\ASN1\Parsable; |
| 16 |
use WP_DEFENDER_VENDOR\FG\ASN1\Identifier; |
| 17 |
|
| 18 |
class BitString extends OctetString implements Parsable |
| 19 |
{ |
| 20 |
private $nrOfUnusedBits; |
| 21 |
|
| 22 |
/** |
| 23 |
* Creates a new ASN.1 BitString object. |
| 24 |
* |
| 25 |
* @param string|int $value Either the hexadecimal value as a string (spaces are allowed - leading 0x is optional) or a numeric value |
| 26 |
* @param int $nrOfUnusedBits the number of unused bits in the last octet [optional]. |
| 27 |
* |
| 28 |
* @throws Exception if the second parameter is no positive numeric value |
| 29 |
*/ |
| 30 |
public function __construct($value, $nrOfUnusedBits = 0) |
| 31 |
{ |
| 32 |
parent::__construct($value); |
| 33 |
|
| 34 |
if (!is_numeric($nrOfUnusedBits) || $nrOfUnusedBits < 0) { |
| 35 |
throw new Exception('BitString: second parameter needs to be a positive number (or zero)!'); |
| 36 |
} |
| 37 |
|
| 38 |
$this->nrOfUnusedBits = $nrOfUnusedBits; |
| 39 |
} |
| 40 |
|
| 41 |
public function getType() |
| 42 |
{ |
| 43 |
return Identifier::BITSTRING; |
| 44 |
} |
| 45 |
|
| 46 |
protected function calculateContentLength() |
| 47 |
{ |
| 48 |
// add one to the length for the first octet which encodes the number of unused bits in the last octet |
| 49 |
return parent::calculateContentLength() + 1; |
| 50 |
} |
| 51 |
|
| 52 |
protected function getEncodedValue() |
| 53 |
{ |
| 54 |
// the first octet determines the number of unused bits |
| 55 |
$nrOfUnusedBitsOctet = chr($this->nrOfUnusedBits); |
| 56 |
$actualContent = parent::getEncodedValue(); |
| 57 |
|
| 58 |
return $nrOfUnusedBitsOctet.$actualContent; |
| 59 |
} |
| 60 |
|
| 61 |
public function getNumberOfUnusedBits() |
| 62 |
{ |
| 63 |
return $this->nrOfUnusedBits; |
| 64 |
} |
| 65 |
|
| 66 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 67 |
{ |
| 68 |
self::parseIdentifier($binaryData[$offsetIndex], Identifier::BITSTRING, $offsetIndex++); |
| 69 |
$contentLength = self::parseContentLength($binaryData, $offsetIndex, 2); |
| 70 |
|
| 71 |
$nrOfUnusedBits = ord($binaryData[$offsetIndex]); |
| 72 |
$value = substr($binaryData, $offsetIndex + 1, $contentLength - 1); |
| 73 |
|
| 74 |
if ($nrOfUnusedBits > 7 || // no less than 1 used, otherwise non-minimal |
| 75 |
($contentLength - 1) == 1 && $nrOfUnusedBits > 0 || // content length only 1, no |
| 76 |
(ord($value[strlen($value)-1])&((1<<$nrOfUnusedBits)-1)) != 0 // unused bits set |
| 77 |
) { |
| 78 |
throw new ParserException("Can not parse bit string with invalid padding", $offsetIndex); |
| 79 |
} |
| 80 |
|
| 81 |
$offsetIndex += $contentLength; |
| 82 |
|
| 83 |
$parsedObject = new self(bin2hex($value), $nrOfUnusedBits); |
| 84 |
$parsedObject->setContentLength($contentLength); |
| 85 |
|
| 86 |
return $parsedObject; |
| 87 |
} |
| 88 |
} |
| 89 |
|