ConstructedOctetStream.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 ConstructedOctetStream
  10. : BaseInputStream
  11. {
  12. private readonly Asn1StreamParser m_parser;
  13. private bool m_first = true;
  14. private Stream m_currentStream;
  15. internal ConstructedOctetStream(Asn1StreamParser parser)
  16. {
  17. m_parser = parser;
  18. }
  19. public override int Read(byte[] buffer, int offset, int count)
  20. {
  21. Streams.ValidateBufferArguments(buffer, offset, count);
  22. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  23. return Read(buffer.AsSpan(offset, count));
  24. #else
  25. if (count < 1)
  26. return 0;
  27. if (m_currentStream == null)
  28. {
  29. if (!m_first)
  30. return 0;
  31. Asn1OctetStringParser next = GetNextParser();
  32. if (next == null)
  33. return 0;
  34. m_first = false;
  35. m_currentStream = next.GetOctetStream();
  36. }
  37. int totalRead = 0;
  38. for (;;)
  39. {
  40. int numRead = m_currentStream.Read(buffer, offset + totalRead, count - totalRead);
  41. if (numRead > 0)
  42. {
  43. totalRead += numRead;
  44. if (totalRead == count)
  45. return totalRead;
  46. }
  47. else
  48. {
  49. Asn1OctetStringParser next = GetNextParser();
  50. if (next == null)
  51. {
  52. m_currentStream = null;
  53. return totalRead;
  54. }
  55. m_currentStream = next.GetOctetStream();
  56. }
  57. }
  58. #endif
  59. }
  60. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  61. public override int Read(Span<byte> buffer)
  62. {
  63. if (buffer.IsEmpty)
  64. return 0;
  65. if (m_currentStream == null)
  66. {
  67. if (!m_first)
  68. return 0;
  69. Asn1OctetStringParser next = GetNextParser();
  70. if (next == null)
  71. return 0;
  72. m_first = false;
  73. m_currentStream = next.GetOctetStream();
  74. }
  75. int totalRead = 0;
  76. for (;;)
  77. {
  78. int numRead = m_currentStream.Read(buffer[totalRead..]);
  79. if (numRead > 0)
  80. {
  81. totalRead += numRead;
  82. if (totalRead == buffer.Length)
  83. return totalRead;
  84. }
  85. else
  86. {
  87. Asn1OctetStringParser next = GetNextParser();
  88. if (next == null)
  89. {
  90. m_currentStream = null;
  91. return totalRead;
  92. }
  93. m_currentStream = next.GetOctetStream();
  94. }
  95. }
  96. }
  97. #endif
  98. public override int ReadByte()
  99. {
  100. if (m_currentStream == null)
  101. {
  102. if (!m_first)
  103. return -1;
  104. Asn1OctetStringParser next = GetNextParser();
  105. if (next == null)
  106. return -1;
  107. m_first = false;
  108. m_currentStream = next.GetOctetStream();
  109. }
  110. for (;;)
  111. {
  112. int b = m_currentStream.ReadByte();
  113. if (b >= 0)
  114. return b;
  115. Asn1OctetStringParser next = GetNextParser();
  116. if (next == null)
  117. {
  118. m_currentStream = null;
  119. return -1;
  120. }
  121. m_currentStream = next.GetOctetStream();
  122. }
  123. }
  124. private Asn1OctetStringParser GetNextParser()
  125. {
  126. IAsn1Convertible asn1Obj = m_parser.ReadObject();
  127. if (asn1Obj == null)
  128. return null;
  129. if (asn1Obj is Asn1OctetStringParser)
  130. return (Asn1OctetStringParser)asn1Obj;
  131. throw new IOException("unknown object encountered: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(asn1Obj));
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif