Asn1EncodableVector.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. /**
  8. * Mutable class for building ASN.1 constructed objects such as SETs or SEQUENCEs.
  9. */
  10. public class Asn1EncodableVector
  11. : IEnumerable<Asn1Encodable>
  12. {
  13. internal static readonly Asn1Encodable[] EmptyElements = new Asn1Encodable[0];
  14. private const int DefaultCapacity = 10;
  15. private Asn1Encodable[] elements;
  16. private int elementCount;
  17. private bool copyOnWrite;
  18. public static Asn1EncodableVector FromEnumerable(IEnumerable<Asn1Encodable> e)
  19. {
  20. Asn1EncodableVector v = new Asn1EncodableVector();
  21. foreach (Asn1Encodable obj in e)
  22. {
  23. v.Add(obj);
  24. }
  25. return v;
  26. }
  27. public Asn1EncodableVector()
  28. : this(DefaultCapacity)
  29. {
  30. }
  31. public Asn1EncodableVector(int initialCapacity)
  32. {
  33. if (initialCapacity < 0)
  34. throw new ArgumentException("must not be negative", "initialCapacity");
  35. this.elements = (initialCapacity == 0) ? EmptyElements : new Asn1Encodable[initialCapacity];
  36. this.elementCount = 0;
  37. this.copyOnWrite = false;
  38. }
  39. public Asn1EncodableVector(Asn1Encodable element)
  40. : this()
  41. {
  42. Add(element);
  43. }
  44. public Asn1EncodableVector(Asn1Encodable element1, Asn1Encodable element2)
  45. : this()
  46. {
  47. Add(element1);
  48. Add(element2);
  49. }
  50. public Asn1EncodableVector(params Asn1Encodable[] v)
  51. : this()
  52. {
  53. Add(v);
  54. }
  55. public void Add(Asn1Encodable element)
  56. {
  57. if (null == element)
  58. throw new ArgumentNullException("element");
  59. int capacity = elements.Length;
  60. int minCapacity = elementCount + 1;
  61. if ((minCapacity > capacity) | copyOnWrite)
  62. {
  63. Reallocate(minCapacity);
  64. }
  65. this.elements[elementCount] = element;
  66. this.elementCount = minCapacity;
  67. }
  68. public void Add(Asn1Encodable element1, Asn1Encodable element2)
  69. {
  70. Add(element1);
  71. Add(element2);
  72. }
  73. public void Add(params Asn1Encodable[] objs)
  74. {
  75. foreach (Asn1Encodable obj in objs)
  76. {
  77. Add(obj);
  78. }
  79. }
  80. public void AddOptional(Asn1Encodable element)
  81. {
  82. if (element != null)
  83. {
  84. Add(element);
  85. }
  86. }
  87. public void AddOptional(Asn1Encodable element1, Asn1Encodable element2)
  88. {
  89. if (element1 != null)
  90. {
  91. Add(element1);
  92. }
  93. if (element2 != null)
  94. {
  95. Add(element2);
  96. }
  97. }
  98. public void AddOptional(params Asn1Encodable[] elements)
  99. {
  100. if (elements != null)
  101. {
  102. foreach (var element in elements)
  103. {
  104. if (element != null)
  105. {
  106. Add(element);
  107. }
  108. }
  109. }
  110. }
  111. public void AddOptionalTagged(bool isExplicit, int tagNo, Asn1Encodable obj)
  112. {
  113. if (null != obj)
  114. {
  115. Add(new DerTaggedObject(isExplicit, tagNo, obj));
  116. }
  117. }
  118. public void AddOptionalTagged(bool isExplicit, int tagClass, int tagNo, Asn1Encodable obj)
  119. {
  120. if (null != obj)
  121. {
  122. Add(new DerTaggedObject(isExplicit, tagClass, tagNo, obj));
  123. }
  124. }
  125. public void AddAll(Asn1EncodableVector other)
  126. {
  127. if (null == other)
  128. throw new ArgumentNullException("other");
  129. int otherElementCount = other.Count;
  130. if (otherElementCount < 1)
  131. return;
  132. int capacity = elements.Length;
  133. int minCapacity = elementCount + otherElementCount;
  134. if ((minCapacity > capacity) | copyOnWrite)
  135. {
  136. Reallocate(minCapacity);
  137. }
  138. int i = 0;
  139. do
  140. {
  141. Asn1Encodable otherElement = other[i];
  142. if (null == otherElement)
  143. throw new NullReferenceException("'other' elements cannot be null");
  144. this.elements[elementCount + i] = otherElement;
  145. }
  146. while (++i < otherElementCount);
  147. this.elementCount = minCapacity;
  148. }
  149. public Asn1Encodable this[int index]
  150. {
  151. get
  152. {
  153. if (index >= elementCount)
  154. throw new IndexOutOfRangeException(index + " >= " + elementCount);
  155. return elements[index];
  156. }
  157. }
  158. public int Count
  159. {
  160. get { return elementCount; }
  161. }
  162. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  163. {
  164. return GetEnumerator();
  165. }
  166. public IEnumerator<Asn1Encodable> GetEnumerator()
  167. {
  168. IEnumerable<Asn1Encodable> e = CopyElements();
  169. return e.GetEnumerator();
  170. }
  171. internal Asn1Encodable[] CopyElements()
  172. {
  173. if (0 == elementCount)
  174. return EmptyElements;
  175. Asn1Encodable[] copy = new Asn1Encodable[elementCount];
  176. Array.Copy(elements, 0, copy, 0, elementCount);
  177. return copy;
  178. }
  179. internal Asn1Encodable[] TakeElements()
  180. {
  181. if (0 == elementCount)
  182. return EmptyElements;
  183. if (elements.Length == elementCount)
  184. {
  185. this.copyOnWrite = true;
  186. return elements;
  187. }
  188. Asn1Encodable[] copy = new Asn1Encodable[elementCount];
  189. Array.Copy(elements, 0, copy, 0, elementCount);
  190. return copy;
  191. }
  192. private void Reallocate(int minCapacity)
  193. {
  194. int oldCapacity = elements.Length;
  195. int newCapacity = System.Math.Max(oldCapacity, minCapacity + (minCapacity >> 1));
  196. Asn1Encodable[] copy = new Asn1Encodable[newCapacity];
  197. Array.Copy(elements, 0, copy, 0, elementCount);
  198. this.elements = copy;
  199. this.copyOnWrite = false;
  200. }
  201. internal static Asn1Encodable[] CloneElements(Asn1Encodable[] elements)
  202. {
  203. return elements.Length < 1 ? EmptyElements : (Asn1Encodable[])elements.Clone();
  204. }
  205. }
  206. }
  207. #pragma warning restore
  208. #endif