| 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; |
| 12 |
|
| 13 |
use Exception; |
| 14 |
|
| 15 |
abstract class AbstractString extends ASNObject implements Parsable |
| 16 |
{ |
| 17 |
/** @var string */ |
| 18 |
protected $value; |
| 19 |
private $checkStringForIllegalChars = true; |
| 20 |
private $allowedCharacters = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* The abstract base class for ASN.1 classes which represent some string of character. |
| 24 |
* |
| 25 |
* @param string $string |
| 26 |
*/ |
| 27 |
public function __construct($string) |
| 28 |
{ |
| 29 |
$this->value = $string; |
| 30 |
} |
| 31 |
|
| 32 |
public function getContent() |
| 33 |
{ |
| 34 |
return $this->value; |
| 35 |
} |
| 36 |
|
| 37 |
protected function allowCharacter($character) |
| 38 |
{ |
| 39 |
$this->allowedCharacters[] = $character; |
| 40 |
} |
| 41 |
|
| 42 |
protected function allowCharacters(...$characters) |
| 43 |
{ |
| 44 |
foreach ($characters as $character) { |
| 45 |
$this->allowedCharacters[] = $character; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
protected function allowNumbers() |
| 50 |
{ |
| 51 |
foreach (range('0', '9') as $char) { |
| 52 |
$this->allowedCharacters[] = (string) $char; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
protected function allowAllLetters() |
| 57 |
{ |
| 58 |
$this->allowSmallLetters(); |
| 59 |
$this->allowCapitalLetters(); |
| 60 |
} |
| 61 |
|
| 62 |
protected function allowSmallLetters() |
| 63 |
{ |
| 64 |
foreach (range('a', 'z') as $char) { |
| 65 |
$this->allowedCharacters[] = $char; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
protected function allowCapitalLetters() |
| 70 |
{ |
| 71 |
foreach (range('A', 'Z') as $char) { |
| 72 |
$this->allowedCharacters[] = $char; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
protected function allowSpaces() |
| 77 |
{ |
| 78 |
$this->allowedCharacters[] = ' '; |
| 79 |
} |
| 80 |
|
| 81 |
protected function allowAll() |
| 82 |
{ |
| 83 |
$this->checkStringForIllegalChars = false; |
| 84 |
} |
| 85 |
|
| 86 |
protected function calculateContentLength() |
| 87 |
{ |
| 88 |
return strlen($this->value); |
| 89 |
} |
| 90 |
|
| 91 |
protected function getEncodedValue() |
| 92 |
{ |
| 93 |
if ($this->checkStringForIllegalChars) { |
| 94 |
$this->checkString(); |
| 95 |
} |
| 96 |
|
| 97 |
return $this->value; |
| 98 |
} |
| 99 |
|
| 100 |
protected function checkString() |
| 101 |
{ |
| 102 |
$stringLength = $this->getContentLength(); |
| 103 |
for ($i = 0; $i < $stringLength; $i++) { |
| 104 |
if (in_array($this->value[$i], $this->allowedCharacters) == false) { |
| 105 |
$typeName = Identifier::getName($this->getType()); |
| 106 |
throw new Exception("Could not create a {$typeName} from the character sequence '{$this->value}'."); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 112 |
{ |
| 113 |
$parsedObject = new static(''); |
| 114 |
|
| 115 |
self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++); |
| 116 |
$contentLength = self::parseContentLength($binaryData, $offsetIndex); |
| 117 |
$string = substr($binaryData, $offsetIndex, $contentLength); |
| 118 |
$offsetIndex += $contentLength; |
| 119 |
|
| 120 |
$parsedObject->value = $string; |
| 121 |
$parsedObject->setContentLength($contentLength); |
| 122 |
return $parsedObject; |
| 123 |
} |
| 124 |
|
| 125 |
public static function isValid($string) |
| 126 |
{ |
| 127 |
$testObject = new static($string); |
| 128 |
try { |
| 129 |
$testObject->checkString(); |
| 130 |
|
| 131 |
return true; |
| 132 |
} catch (Exception $exception) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|