| 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 ArrayAccess; |
| 14 |
use ArrayIterator; |
| 15 |
use Countable; |
| 16 |
use WP_DEFENDER_VENDOR\FG\ASN1\Exception\ParserException; |
| 17 |
use Iterator; |
| 18 |
|
| 19 |
abstract class Construct extends ASNObject implements Countable, ArrayAccess, Iterator, Parsable |
| 20 |
{ |
| 21 |
/** @var \WP_DEFENDER_VENDOR\FG\ASN1\ASNObject[] */ |
| 22 |
protected $children; |
| 23 |
private $iteratorPosition; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param \WP_DEFENDER_VENDOR\FG\ASN1\ASNObject[] $children the variadic type hint is commented due to https://github.com/facebook/hhvm/issues/4858 |
| 27 |
*/ |
| 28 |
public function __construct(/* HH_FIXME[4858]: variadic + strict */ ...$children) |
| 29 |
{ |
| 30 |
$this->children = $children; |
| 31 |
$this->iteratorPosition = 0; |
| 32 |
} |
| 33 |
|
| 34 |
public function getContent() |
| 35 |
{ |
| 36 |
return $this->children; |
| 37 |
} |
| 38 |
|
| 39 |
#[\ReturnTypeWillChange] |
| 40 |
public function rewind() |
| 41 |
{ |
| 42 |
$this->iteratorPosition = 0; |
| 43 |
} |
| 44 |
|
| 45 |
#[\ReturnTypeWillChange] |
| 46 |
public function current() |
| 47 |
{ |
| 48 |
return $this->children[$this->iteratorPosition]; |
| 49 |
} |
| 50 |
|
| 51 |
#[\ReturnTypeWillChange] |
| 52 |
public function key() |
| 53 |
{ |
| 54 |
return $this->iteratorPosition; |
| 55 |
} |
| 56 |
|
| 57 |
#[\ReturnTypeWillChange] |
| 58 |
public function next() |
| 59 |
{ |
| 60 |
$this->iteratorPosition++; |
| 61 |
} |
| 62 |
|
| 63 |
#[\ReturnTypeWillChange] |
| 64 |
public function valid() |
| 65 |
{ |
| 66 |
return isset($this->children[$this->iteratorPosition]); |
| 67 |
} |
| 68 |
|
| 69 |
#[\ReturnTypeWillChange] |
| 70 |
public function offsetExists($offset) |
| 71 |
{ |
| 72 |
return array_key_exists($offset, $this->children); |
| 73 |
} |
| 74 |
|
| 75 |
#[\ReturnTypeWillChange] |
| 76 |
public function offsetGet($offset) |
| 77 |
{ |
| 78 |
return $this->children[$offset]; |
| 79 |
} |
| 80 |
|
| 81 |
#[\ReturnTypeWillChange] |
| 82 |
public function offsetSet($offset, $value) |
| 83 |
{ |
| 84 |
if ($offset === null) { |
| 85 |
$offset = count($this->children); |
| 86 |
} |
| 87 |
|
| 88 |
$this->children[$offset] = $value; |
| 89 |
} |
| 90 |
|
| 91 |
#[\ReturnTypeWillChange] |
| 92 |
public function offsetUnset($offset) |
| 93 |
{ |
| 94 |
unset($this->children[$offset]); |
| 95 |
} |
| 96 |
|
| 97 |
protected function calculateContentLength() |
| 98 |
{ |
| 99 |
$length = 0; |
| 100 |
foreach ($this->children as $component) { |
| 101 |
$length += $component->getObjectLength(); |
| 102 |
} |
| 103 |
|
| 104 |
return $length; |
| 105 |
} |
| 106 |
|
| 107 |
protected function getEncodedValue() |
| 108 |
{ |
| 109 |
$result = ''; |
| 110 |
foreach ($this->children as $component) { |
| 111 |
$result .= $component->getBinary(); |
| 112 |
} |
| 113 |
|
| 114 |
return $result; |
| 115 |
} |
| 116 |
|
| 117 |
public function addChild(ASNObject $child) |
| 118 |
{ |
| 119 |
$this->children[] = $child; |
| 120 |
} |
| 121 |
|
| 122 |
public function addChildren(array $children) |
| 123 |
{ |
| 124 |
foreach ($children as $child) { |
| 125 |
$this->addChild($child); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public function __toString() |
| 130 |
{ |
| 131 |
$nrOfChildren = $this->getNumberOfChildren(); |
| 132 |
$childString = $nrOfChildren == 1 ? 'child' : 'children'; |
| 133 |
|
| 134 |
return "[{$nrOfChildren} {$childString}]"; |
| 135 |
} |
| 136 |
|
| 137 |
public function getNumberOfChildren() |
| 138 |
{ |
| 139 |
return count($this->children); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return \WP_DEFENDER_VENDOR\FG\ASN1\ASNObject[] |
| 144 |
*/ |
| 145 |
public function getChildren() |
| 146 |
{ |
| 147 |
return $this->children; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @return \WP_DEFENDER_VENDOR\FG\ASN1\ASNObject |
| 152 |
*/ |
| 153 |
public function getFirstChild() |
| 154 |
{ |
| 155 |
return $this->children[0]; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param string $binaryData |
| 160 |
* @param int $offsetIndex |
| 161 |
* |
| 162 |
* @throws Exception\ParserException |
| 163 |
* |
| 164 |
* @return Construct|static |
| 165 |
*/ |
| 166 |
#[\ReturnTypeWillChange] |
| 167 |
public static function fromBinary(&$binaryData, &$offsetIndex = 0) |
| 168 |
{ |
| 169 |
$parsedObject = new static(); |
| 170 |
self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++); |
| 171 |
$contentLength = self::parseContentLength($binaryData, $offsetIndex); |
| 172 |
$startIndex = $offsetIndex; |
| 173 |
|
| 174 |
$children = []; |
| 175 |
$octetsToRead = $contentLength; |
| 176 |
while ($octetsToRead > 0) { |
| 177 |
$newChild = ASNObject::fromBinary($binaryData, $offsetIndex); |
| 178 |
$octetsToRead -= $newChild->getObjectLength(); |
| 179 |
$children[] = $newChild; |
| 180 |
} |
| 181 |
|
| 182 |
if ($octetsToRead !== 0) { |
| 183 |
throw new ParserException("Sequence length incorrect", $startIndex); |
| 184 |
} |
| 185 |
|
| 186 |
$parsedObject->addChildren($children); |
| 187 |
$parsedObject->setContentLength($contentLength); |
| 188 |
|
| 189 |
return $parsedObject; |
| 190 |
} |
| 191 |
|
| 192 |
#[\ReturnTypeWillChange] |
| 193 |
public function count($mode = COUNT_NORMAL) |
| 194 |
{ |
| 195 |
return count($this->children, $mode); |
| 196 |
} |
| 197 |
|
| 198 |
public function getIterator() |
| 199 |
{ |
| 200 |
return new ArrayIterator($this->children); |
| 201 |
} |
| 202 |
} |