LazyDLSet.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. internal class LazyDLSet
  9. : DLSet
  10. {
  11. private byte[] encoded;
  12. internal LazyDLSet(byte[] encoded)
  13. : base()
  14. {
  15. if (null == encoded)
  16. throw new ArgumentNullException("encoded");
  17. this.encoded = encoded;
  18. }
  19. public override Asn1Encodable this[int index]
  20. {
  21. get
  22. {
  23. Force();
  24. return base[index];
  25. }
  26. }
  27. public override IEnumerator<Asn1Encodable> GetEnumerator()
  28. {
  29. byte[] encoded = GetContents();
  30. if (null != encoded)
  31. return new LazyDLEnumerator(encoded);
  32. return base.GetEnumerator();
  33. }
  34. public override int Count
  35. {
  36. get
  37. {
  38. Force();
  39. return base.Count;
  40. }
  41. }
  42. public override Asn1Encodable[] ToArray()
  43. {
  44. Force();
  45. return base.ToArray();
  46. }
  47. public override string ToString()
  48. {
  49. Force();
  50. return base.ToString();
  51. }
  52. internal override IAsn1Encoding GetEncoding(int encoding)
  53. {
  54. if (Asn1OutputStream.EncodingBer == encoding)
  55. {
  56. byte[] encoded = GetContents();
  57. if (null != encoded)
  58. return new ConstructedLazyDLEncoding(Asn1Tags.Universal, Asn1Tags.Set, encoded);
  59. }
  60. else
  61. {
  62. Force();
  63. }
  64. return base.GetEncoding(encoding);
  65. }
  66. internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo)
  67. {
  68. if (Asn1OutputStream.EncodingBer == encoding)
  69. {
  70. byte[] encoded = GetContents();
  71. if (null != encoded)
  72. return new ConstructedLazyDLEncoding(tagClass, tagNo, encoded);
  73. }
  74. else
  75. {
  76. Force();
  77. }
  78. return base.GetEncodingImplicit(encoding, tagClass, tagNo);
  79. }
  80. private void Force()
  81. {
  82. lock (this)
  83. {
  84. if (null != encoded)
  85. {
  86. Asn1InputStream input = new LazyAsn1InputStream(encoded);
  87. try
  88. {
  89. Asn1EncodableVector v = input.ReadVector();
  90. this.elements = v.TakeElements();
  91. this.isSorted = elements.Length < 2;
  92. this.encoded = null;
  93. }
  94. catch (IOException e)
  95. {
  96. throw new Asn1ParsingException("malformed ASN.1: " + e.Message, e);
  97. }
  98. }
  99. }
  100. }
  101. private byte[] GetContents()
  102. {
  103. lock (this) return encoded;
  104. }
  105. }
  106. }
  107. #pragma warning restore
  108. #endif