ConstructedBitStream.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  8. {
  9. internal class ConstructedBitStream
  10. : BaseInputStream
  11. {
  12. private readonly Asn1StreamParser m_parser;
  13. private readonly bool m_octetAligned;
  14. private bool m_first = true;
  15. private int m_padBits = 0;
  16. private Asn1BitStringParser m_currentParser;
  17. private Stream m_currentStream;
  18. internal ConstructedBitStream(Asn1StreamParser parser, bool octetAligned)
  19. {
  20. m_parser = parser;
  21. m_octetAligned = octetAligned;
  22. }
  23. internal int PadBits
  24. {
  25. get { return m_padBits; }
  26. }
  27. public override int Read(byte[] buffer, int offset, int count)
  28. {
  29. Streams.ValidateBufferArguments(buffer, offset, count);
  30. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  31. return Read(buffer.AsSpan(offset, count));
  32. #else
  33. if (count < 1)
  34. return 0;
  35. if (m_currentStream == null)
  36. {
  37. if (!m_first)
  38. return 0;
  39. m_currentParser = GetNextParser();
  40. if (m_currentParser == null)
  41. return 0;
  42. m_first = false;
  43. m_currentStream = m_currentParser.GetBitStream();
  44. }
  45. int totalRead = 0;
  46. for (;;)
  47. {
  48. int numRead = m_currentStream.Read(buffer, offset + totalRead, count - totalRead);
  49. if (numRead > 0)
  50. {
  51. totalRead += numRead;
  52. if (totalRead == count)
  53. return totalRead;
  54. }
  55. else
  56. {
  57. m_padBits = m_currentParser.PadBits;
  58. m_currentParser = GetNextParser();
  59. if (m_currentParser == null)
  60. {
  61. m_currentStream = null;
  62. return totalRead;
  63. }
  64. m_currentStream = m_currentParser.GetBitStream();
  65. }
  66. }
  67. #endif
  68. }
  69. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  70. public override int Read(Span<byte> buffer)
  71. {
  72. if (buffer.IsEmpty)
  73. return 0;
  74. if (m_currentStream == null)
  75. {
  76. if (!m_first)
  77. return 0;
  78. m_currentParser = GetNextParser();
  79. if (m_currentParser == null)
  80. return 0;
  81. m_first = false;
  82. m_currentStream = m_currentParser.GetBitStream();
  83. }
  84. int totalRead = 0;
  85. for (;;)
  86. {
  87. int numRead = m_currentStream.Read(buffer[totalRead..]);
  88. if (numRead > 0)
  89. {
  90. totalRead += numRead;
  91. if (totalRead == buffer.Length)
  92. return totalRead;
  93. }
  94. else
  95. {
  96. m_padBits = m_currentParser.PadBits;
  97. m_currentParser = GetNextParser();
  98. if (m_currentParser == null)
  99. {
  100. m_currentStream = null;
  101. return totalRead;
  102. }
  103. m_currentStream = m_currentParser.GetBitStream();
  104. }
  105. }
  106. }
  107. #endif
  108. public override int ReadByte()
  109. {
  110. if (m_currentStream == null)
  111. {
  112. if (!m_first)
  113. return -1;
  114. m_currentParser = GetNextParser();
  115. if (m_currentParser == null)
  116. return -1;
  117. m_first = false;
  118. m_currentStream = m_currentParser.GetBitStream();
  119. }
  120. for (;;)
  121. {
  122. int b = m_currentStream.ReadByte();
  123. if (b >= 0)
  124. return b;
  125. m_padBits = m_currentParser.PadBits;
  126. m_currentParser = GetNextParser();
  127. if (m_currentParser == null)
  128. {
  129. m_currentStream = null;
  130. return -1;
  131. }
  132. m_currentStream = m_currentParser.GetBitStream();
  133. }
  134. }
  135. private Asn1BitStringParser GetNextParser()
  136. {
  137. IAsn1Convertible asn1Obj = m_parser.ReadObject();
  138. if (asn1Obj == null)
  139. {
  140. if (m_octetAligned && m_padBits != 0)
  141. throw new IOException("expected octet-aligned bitstring, but found padBits: " + m_padBits);
  142. return null;
  143. }
  144. if (asn1Obj is Asn1BitStringParser)
  145. {
  146. if (m_padBits != 0)
  147. throw new IOException("only the last nested bitstring can have padding");
  148. return (Asn1BitStringParser)asn1Obj;
  149. }
  150. throw new IOException("unknown object encountered: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(asn1Obj));
  151. }
  152. }
  153. }
  154. #pragma warning restore
  155. #endif