Asn1EncodableVector.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. namespace BestHTTP.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
  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 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(params Asn1Encodable[] v)
  40. : this()
  41. {
  42. Add(v);
  43. }
  44. public void Add(Asn1Encodable element)
  45. {
  46. if (null == element)
  47. throw new ArgumentNullException("element");
  48. int capacity = elements.Length;
  49. int minCapacity = elementCount + 1;
  50. if ((minCapacity > capacity) | copyOnWrite)
  51. {
  52. Reallocate(minCapacity);
  53. }
  54. this.elements[elementCount] = element;
  55. this.elementCount = minCapacity;
  56. }
  57. public void Add(params Asn1Encodable[] objs)
  58. {
  59. foreach (Asn1Encodable obj in objs)
  60. {
  61. Add(obj);
  62. }
  63. }
  64. public void AddOptional(params Asn1Encodable[] objs)
  65. {
  66. if (objs != null)
  67. {
  68. foreach (Asn1Encodable obj in objs)
  69. {
  70. if (obj != null)
  71. {
  72. Add(obj);
  73. }
  74. }
  75. }
  76. }
  77. public void AddOptionalTagged(bool isExplicit, int tagNo, Asn1Encodable obj)
  78. {
  79. if (null != obj)
  80. {
  81. Add(new DerTaggedObject(isExplicit, tagNo, obj));
  82. }
  83. }
  84. public void AddAll(Asn1EncodableVector other)
  85. {
  86. if (null == other)
  87. throw new ArgumentNullException("other");
  88. int otherElementCount = other.Count;
  89. if (otherElementCount < 1)
  90. return;
  91. int capacity = elements.Length;
  92. int minCapacity = elementCount + otherElementCount;
  93. if ((minCapacity > capacity) | copyOnWrite)
  94. {
  95. Reallocate(minCapacity);
  96. }
  97. int i = 0;
  98. do
  99. {
  100. Asn1Encodable otherElement = other[i];
  101. if (null == otherElement)
  102. throw new NullReferenceException("'other' elements cannot be null");
  103. this.elements[elementCount + i] = otherElement;
  104. }
  105. while (++i < otherElementCount);
  106. this.elementCount = minCapacity;
  107. }
  108. public Asn1Encodable this[int index]
  109. {
  110. get
  111. {
  112. if (index >= elementCount)
  113. throw new IndexOutOfRangeException(index + " >= " + elementCount);
  114. return elements[index];
  115. }
  116. }
  117. public int Count
  118. {
  119. get { return elementCount; }
  120. }
  121. public IEnumerator GetEnumerator()
  122. {
  123. return CopyElements().GetEnumerator();
  124. }
  125. internal Asn1Encodable[] CopyElements()
  126. {
  127. if (0 == elementCount)
  128. return EmptyElements;
  129. Asn1Encodable[] copy = new Asn1Encodable[elementCount];
  130. Array.Copy(elements, 0, copy, 0, elementCount);
  131. return copy;
  132. }
  133. internal Asn1Encodable[] TakeElements()
  134. {
  135. if (0 == elementCount)
  136. return EmptyElements;
  137. if (elements.Length == elementCount)
  138. {
  139. this.copyOnWrite = true;
  140. return elements;
  141. }
  142. Asn1Encodable[] copy = new Asn1Encodable[elementCount];
  143. Array.Copy(elements, 0, copy, 0, elementCount);
  144. return copy;
  145. }
  146. private void Reallocate(int minCapacity)
  147. {
  148. int oldCapacity = elements.Length;
  149. int newCapacity = System.Math.Max(oldCapacity, minCapacity + (minCapacity >> 1));
  150. Asn1Encodable[] copy = new Asn1Encodable[newCapacity];
  151. Array.Copy(elements, 0, copy, 0, elementCount);
  152. this.elements = copy;
  153. this.copyOnWrite = false;
  154. }
  155. internal static Asn1Encodable[] CloneElements(Asn1Encodable[] elements)
  156. {
  157. return elements.Length < 1 ? EmptyElements : (Asn1Encodable[])elements.Clone();
  158. }
  159. }
  160. }
  161. #pragma warning restore
  162. #endif