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\Feature\Stringable; |
11
|
|
|
use Sop\ASN1\Type\StringType; |
12
|
|
|
use Sop\ASN1\Type\Structure; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements constructed type of simple strings. |
16
|
|
|
* |
17
|
|
|
* Constructed strings only exist in BER encodings, and often with |
18
|
|
|
* indefinite length. Generally constructed string must contain only elements |
19
|
|
|
* that have the same type tag as the constructing element. |
20
|
|
|
* For example: |
21
|
|
|
* OCTET STRING (cons) { |
22
|
|
|
* OCTET STRING (prim) "ABC" |
23
|
|
|
* OCTET STRING (prim) "DEF" |
24
|
|
|
* } |
25
|
|
|
* Canonically this corresponds to a payload of "ABCDEF" string. |
26
|
|
|
*/ |
27
|
|
|
class ConstructedString extends Structure implements Stringable |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @internal Use create() method instead |
33
|
|
|
* |
34
|
|
|
* @param Element ...$elements Any number of elements |
35
|
|
|
*/ |
36
|
8 |
|
protected function __construct(Element ...$elements) |
37
|
|
|
{ |
38
|
8 |
|
parent::__construct(...$elements); |
39
|
8 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
1 |
|
public function __toString(): string |
47
|
|
|
{ |
48
|
1 |
|
return $this->string(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create from a list of string type elements. |
53
|
|
|
* |
54
|
|
|
* All strings must have the same type. |
55
|
|
|
* |
56
|
|
|
* @param StringType ...$elements |
57
|
|
|
* |
58
|
|
|
* @throws \LogicException |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
3 |
|
public static function create(StringType ...$elements): self |
63
|
|
|
{ |
64
|
3 |
|
if (!count($elements)) { |
65
|
1 |
|
throw new \LogicException( |
66
|
1 |
|
'No elements, unable to determine type tag.'); |
67
|
|
|
} |
68
|
2 |
|
$tag = $elements[0]->tag(); |
69
|
2 |
|
foreach ($elements as $el) { |
70
|
2 |
|
if ($el->tag() !== $tag) { |
71
|
1 |
|
throw new \LogicException( |
72
|
2 |
|
'All elements in constructed string must have the same type.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
1 |
|
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
|
4 |
|
public static function createWithTag(int $tag, StringType ...$elements) |
89
|
|
|
{ |
90
|
4 |
|
$el = new self(...$elements); |
91
|
4 |
|
$el->_typeTag = $tag; |
92
|
4 |
|
return $el; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a list of strings in this structure. |
97
|
|
|
* |
98
|
|
|
* @return string[] |
99
|
|
|
*/ |
100
|
2 |
|
public function strings(): array |
101
|
|
|
{ |
102
|
|
|
return array_map(function (StringType $el) { |
103
|
2 |
|
return $el->string(); |
104
|
2 |
|
}, $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 concatentated. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
public function string(): string |
115
|
|
|
{ |
116
|
1 |
|
return implode('', $this->strings()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
4 |
|
protected static function _decodeFromDER(Identifier $identifier, |
125
|
|
|
string $data, int &$offset): ElementBase |
126
|
|
|
{ |
127
|
|
|
/** @var ConstructedString $type */ |
128
|
4 |
|
$type = forward_static_call_array([parent::class, __FUNCTION__], |
129
|
4 |
|
[$identifier, $data, &$offset]); |
130
|
4 |
|
$type->_typeTag = $identifier->intTag(); |
131
|
4 |
|
return $type; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|