| 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\ASNObject; |
| 15 |
use WP_DEFENDER_VENDOR\FG\ASN1\Parsable; |
| 16 |
use WP_DEFENDER_VENDOR\FG\ASN1\Identifier; |
| 17 |
|
| 18 |
class OctetString extends ASNObject implements Parsable |
| 19 |
{ |
| 20 |
protected $value; |
| 21 |
|
| 22 |
public function __construct($value) |
| 23 |
{ |
| 24 |
if (is_string($value)) { |
| 25 |
// remove gaps between hex digits |
| 26 |
$value = preg_replace('/\s|0x/', '', $value); |
| 27 |
} elseif (is_numeric($value)) { |
| 28 |
$value = dechex($value); |
| 29 |
} elseif ($value === null) { |
| 30 |
return; |
| 31 |
} else { |
| 32 |
throw new Exception('OctetString: unrecognized input type!'); |
| 33 |
} |
| 34 |
|
| 35 |
if (strlen($value) % 2 != 0) { |
| 36 |
// transform values like 1F2 to 01F2 |
| 37 |
$value = '0'.$value; |
| 38 |
} |
| 39 |
|
| 40 |
$this->value = $value; |
| 41 |
} |
| 42 |
|
| 43 |
public function getType() |
| 44 |
{ |
| 45 |
return Identifier::OCTETSTRING; |
| 46 |
} |
| 47 |
|
| 48 |
protected function calculateContentLength() |
| 49 |
{ |
| 50 |
return strlen($this->value) / 2; |
| 51 |
} |
| 52 |
|
| 53 |
protected function getEncodedValue() |
| 54 |
{ |
| 55 |
$value = $this->value; |
| 56 |
$result = ''; |
| 57 |
|
| 58 |
//Actual content |
| 59 |
while (strlen($value) >= 2) { |
| 60 |
// get the hex value byte by byte from the string and and add it to binary result |
| 61 |
$result .= chr(hexdec(substr($value, 0, 2))); |
| 62 |
$value = substr($value, 2); |
| 63 |
} |
| 64 |
|
| 65 |
return $result; |
| 66 |
} |
| 67 |
|
| 68 |
public function getContent() |
| 69 |
{ |
| 70 |
return strtoupper($this->value); |
| 71 |
} |
| 72 |
|
| 73 |
public function getBinaryContent() |
| 74 |
{ |
| 75 |
return $this->getEncodedValue(); |
| 76 |
} |
| 77 |
|
| 78 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 79 |
{ |
| 80 |
self::parseIdentifier($binaryData[$offsetIndex], Identifier::OCTETSTRING, $offsetIndex++); |
| 81 |
$contentLength = self::parseContentLength($binaryData, $offsetIndex); |
| 82 |
|
| 83 |
$value = substr($binaryData, $offsetIndex, $contentLength); |
| 84 |
$offsetIndex += $contentLength; |
| 85 |
|
| 86 |
$parsedObject = new self(bin2hex($value)); |
| 87 |
$parsedObject->setContentLength($contentLength); |
| 88 |
|
| 89 |
return $parsedObject; |
| 90 |
} |
| 91 |
} |
| 92 |
|