1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\ASN1\Type\Constructed; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Component\Identifier; |
8
|
|
|
use Sop\ASN1\Element; |
9
|
|
|
use Sop\ASN1\Feature\ElementBase; |
10
|
|
|
use Sop\ASN1\Type\StringType; |
11
|
|
|
use Sop\ASN1\Type\Structure; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Implements constructed type of simple strings. |
15
|
|
|
* |
16
|
|
|
* Constructed strings only exist in BER encodings, and often with |
17
|
|
|
* indefinite length. Generally constructed string must contain only elements |
18
|
|
|
* that have the same type tag as the constructing element. |
19
|
|
|
* For example: |
20
|
|
|
* ``` |
21
|
|
|
* OCTET STRING (cons) { |
22
|
|
|
* OCTET STRING (prim) "ABC" |
23
|
|
|
* OCTET STRING (prim) "DEF" |
24
|
|
|
* } |
25
|
|
|
* ``` |
26
|
|
|
* Canonically this corresponds to a payload of "ABCDEF" string. |
27
|
|
|
* |
28
|
|
|
* From API standpoint this can also be seen as a string type |
29
|
|
|
* (as it implements `StringType`), and thus `UnspecifiedType::asString()` |
30
|
|
|
* method may return `ConstructedString` instances. |
31
|
|
|
*/ |
32
|
|
|
class ConstructedString extends Structure implements StringType |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @internal Use `create()` or `createWithTag()` method instead |
38
|
|
|
* |
39
|
|
|
* @param Element ...$elements Any number of elements |
40
|
|
|
*/ |
41
|
26 |
|
protected function __construct(Element ...$elements) |
42
|
|
|
{ |
43
|
26 |
|
parent::__construct(...$elements); |
44
|
26 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function __toString(): string |
50
|
|
|
{ |
51
|
1 |
|
return $this->string(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create from a list of string type elements. |
56
|
|
|
* |
57
|
|
|
* All strings must have the same type. |
58
|
|
|
* |
59
|
|
|
* @param StringType ...$elements |
60
|
|
|
* |
61
|
|
|
* @throws \LogicException |
62
|
|
|
*/ |
63
|
20 |
|
public static function create(StringType ...$elements): self |
64
|
|
|
{ |
65
|
20 |
|
if (!count($elements)) { |
66
|
1 |
|
throw new \LogicException('No elements, unable to determine type tag.'); |
67
|
|
|
} |
68
|
19 |
|
$tag = $elements[0]->tag(); |
69
|
19 |
|
foreach ($elements as $el) { |
70
|
19 |
|
if ($el->tag() !== $tag) { |
71
|
1 |
|
throw new \LogicException( |
72
|
19 |
|
'All elements in constructed string must have the same type.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
18 |
|
return self::createWithTag($tag, ...$elements); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create from strings with a given type tag. |
80
|
|
|
* |
81
|
|
|
* Does not perform any validation on types. |
82
|
|
|
* |
83
|
|
|
* @param int $tag Type tag for the constructed string element |
84
|
|
|
* @param StringType ...$elements Any number of elements |
85
|
|
|
* |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
21 |
|
public static function createWithTag(int $tag, StringType ...$elements) |
89
|
|
|
{ |
90
|
21 |
|
$el = new self(...$elements); |
91
|
21 |
|
$el->_typeTag = $tag; |
92
|
21 |
|
return $el; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a list of strings in this structure. |
97
|
|
|
* |
98
|
|
|
* @return string[] |
99
|
|
|
*/ |
100
|
19 |
|
public function strings(): array |
101
|
|
|
{ |
102
|
|
|
return array_map(function (StringType $el) { |
103
|
19 |
|
return $el->string(); |
104
|
19 |
|
}, $this->_elements); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the contained strings concatenated together. |
109
|
|
|
* |
110
|
|
|
* NOTE: It's unclear how bit strings with unused bits should be concatenated. |
111
|
|
|
*/ |
112
|
18 |
|
public function string(): string |
113
|
|
|
{ |
114
|
18 |
|
return implode('', $this->strings()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
* |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
22 |
|
protected static function _decodeFromDER(Identifier $identifier, |
123
|
|
|
string $data, int &$offset): ElementBase |
124
|
|
|
{ |
125
|
|
|
/** @var ConstructedString $type */ |
126
|
22 |
|
$type = forward_static_call_array([parent::class, __FUNCTION__], |
127
|
22 |
|
[$identifier, $data, &$offset]); |
128
|
22 |
|
$type->_typeTag = $identifier->intTag(); |
129
|
22 |
|
return $type; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|