mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathAsn1EncodableVector.cs
More file actions
271 lines (220 loc) · 7.88 KB
/
Copy pathAsn1EncodableVector.cs
File metadata and controls
271 lines (220 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
namespace Org.BouncyCastle.Asn1
{
/**
* Mutable class for building ASN.1 constructed objects such as SETs or SEQUENCEs.
*/
public class Asn1EncodableVector
: IReadOnlyCollection<Asn1Encodable>
{
internal static readonly Asn1Encodable[] EmptyElements = Array.Empty<Asn1Encodable>();
private const int DefaultCapacity = 10;
public static Asn1EncodableVector FromCollection(IReadOnlyCollection<Asn1Encodable> c)
{
Asn1EncodableVector v = new Asn1EncodableVector(c.Count);
v.AddAll((IEnumerable<Asn1Encodable>)c);
return v;
}
public static Asn1EncodableVector FromElement(Asn1Encodable element) => new Asn1EncodableVector(1){ element };
public static Asn1EncodableVector FromEnumerable(IEnumerable<Asn1Encodable> e)
{
Asn1EncodableVector v = new Asn1EncodableVector();
v.AddAll(e);
return v;
}
private Asn1Encodable[] m_elements;
private int m_elementCount;
private bool m_copyOnWrite;
public Asn1EncodableVector()
: this(DefaultCapacity)
{
}
public Asn1EncodableVector(int initialCapacity)
{
if (initialCapacity < 0)
throw new ArgumentException("must not be negative", nameof(initialCapacity));
m_elements = initialCapacity == 0 ? EmptyElements : new Asn1Encodable[initialCapacity];
m_elementCount = 0;
m_copyOnWrite = false;
}
public Asn1EncodableVector(Asn1Encodable element)
: this()
{
Add(element);
}
public Asn1EncodableVector(Asn1Encodable element1, Asn1Encodable element2)
: this()
{
Add(element1);
Add(element2);
}
public Asn1EncodableVector(params Asn1Encodable[] v)
: this()
{
Add(v);
}
public void Add(Asn1Encodable element)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
PrepareCapacity(1);
m_elements[m_elementCount++] = element;
}
public void Add(Asn1Encodable element1, Asn1Encodable element2)
{
Add(element1);
Add(element2);
}
public void Add(params Asn1Encodable[] objs)
{
foreach (Asn1Encodable obj in objs)
{
Add(obj);
}
}
public void AddOptional(Asn1Encodable element)
{
if (element != null)
{
Add(element);
}
}
public void AddOptional(Asn1Encodable element1, Asn1Encodable element2)
{
AddOptional(element1);
AddOptional(element2);
}
public void AddOptional(params Asn1Encodable[] elements)
{
if (elements != null)
{
foreach (var element in elements)
{
AddOptional(element);
}
}
}
// TODO[api] Rename 'isExplicit' to 'declaredExplicit', 'obj' to 'element'
public void AddOptionalTagged(bool isExplicit, int tagNo, Asn1Encodable obj)
{
if (null != obj)
{
AddTagged(isExplicit, tagNo, obj);
}
}
// TODO[api] Rename 'isExplicit' to 'declaredExplicit', 'obj' to 'element'
public void AddOptionalTagged(bool isExplicit, int tagClass, int tagNo, Asn1Encodable obj)
{
if (null != obj)
{
AddTagged(isExplicit, tagClass, tagNo, obj);
}
}
public void AddTagged(bool declaredExplicit, int tagNo, Asn1Encodable element)
{
Add(new DerTaggedObject(declaredExplicit, tagNo, element));
}
public void AddTagged(bool declaredExplicit, int tagClass, int tagNo, Asn1Encodable element)
{
Add(new DerTaggedObject(declaredExplicit, tagClass, tagNo, element));
}
public void AddAll(IEnumerable<Asn1Encodable> e)
{
if (e == null)
throw new ArgumentNullException(nameof(e));
foreach (Asn1Encodable obj in e)
{
Add(obj);
}
}
public void AddAll(IReadOnlyCollection<Asn1Encodable> c)
{
if (c == null)
throw new ArgumentNullException(nameof(c));
int otherElementCount = c.Count;
if (otherElementCount < 1)
return;
PrepareCapacity(otherElementCount);
AddAll((IEnumerable<Asn1Encodable>)c);
}
public void AddAll(Asn1EncodableVector other)
{
if (other == null)
throw new ArgumentNullException(nameof(other));
int otherElementCount = other.Count;
if (otherElementCount < 1)
return;
PrepareCapacity(otherElementCount);
Array.Copy(other.m_elements, 0, m_elements, m_elementCount, otherElementCount);
m_elementCount += otherElementCount;
}
public Asn1Encodable this[int index]
{
get
{
if (index >= m_elementCount)
throw new IndexOutOfRangeException(index + " >= " + m_elementCount);
return m_elements[index];
}
}
public int Count => m_elementCount;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Asn1Encodable> GetEnumerator()
{
IEnumerable<Asn1Encodable> e = EnumerateElements(m_elements, m_elementCount);
return e.GetEnumerator();
}
internal Asn1Encodable[] CopyElements() => CopyElements(m_elements, m_elementCount);
internal Asn1Encodable[] TakeElements()
{
if (m_elementCount == 0)
return EmptyElements;
if (m_elements.Length == m_elementCount)
{
m_copyOnWrite = true;
return m_elements;
}
Asn1Encodable[] copy = new Asn1Encodable[m_elementCount];
Array.Copy(m_elements, 0, copy, 0, m_elementCount);
return copy;
}
private int PrepareCapacity(int requiredCapacity)
{
int capacity = m_elements.Length;
int minCapacity = m_elementCount + requiredCapacity;
if ((minCapacity > capacity) | m_copyOnWrite)
{
Reallocate(minCapacity);
}
return minCapacity;
}
private void Reallocate(int minCapacity)
{
int oldCapacity = m_elements.Length;
int newCapacity = System.Math.Max(oldCapacity, minCapacity + (minCapacity >> 1));
Asn1Encodable[] copy = new Asn1Encodable[newCapacity];
Array.Copy(m_elements, 0, copy, 0, m_elementCount);
m_elements = copy;
m_copyOnWrite = false;
}
internal static Asn1Encodable[] CloneElements(Asn1Encodable[] elements) =>
CopyElements(elements, elementCount: elements.Length);
private static Asn1Encodable[] CopyElements(Asn1Encodable[] elements, int elementCount)
{
if (elementCount < 1)
return EmptyElements;
Asn1Encodable[] copy = new Asn1Encodable[elementCount];
Array.Copy(elements, 0, copy, 0, elementCount);
return copy;
}
private static IEnumerable<Asn1Encodable> EnumerateElements(Asn1Encodable[] elements, int elementCount)
{
for (int i = 0; i < elementCount; ++i)
yield return elements[i];
}
}
}