ConstructedOctetStream.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.IO;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. internal class ConstructedOctetStream
  8. : BaseInputStream
  9. {
  10. private readonly Asn1StreamParser _parser;
  11. private bool _first = true;
  12. private Stream _currentStream;
  13. internal ConstructedOctetStream(
  14. Asn1StreamParser parser)
  15. {
  16. _parser = parser;
  17. }
  18. public override int Read(byte[] buffer, int offset, int count)
  19. {
  20. if (_currentStream == null)
  21. {
  22. if (!_first)
  23. return 0;
  24. Asn1OctetStringParser s = (Asn1OctetStringParser)_parser.ReadObject();
  25. if (s == null)
  26. return 0;
  27. _first = false;
  28. _currentStream = s.GetOctetStream();
  29. }
  30. int totalRead = 0;
  31. for (;;)
  32. {
  33. int numRead = _currentStream.Read(buffer, offset + totalRead, count - totalRead);
  34. if (numRead > 0)
  35. {
  36. totalRead += numRead;
  37. if (totalRead == count)
  38. return totalRead;
  39. }
  40. else
  41. {
  42. Asn1OctetStringParser aos = (Asn1OctetStringParser)_parser.ReadObject();
  43. if (aos == null)
  44. {
  45. _currentStream = null;
  46. return totalRead;
  47. }
  48. _currentStream = aos.GetOctetStream();
  49. }
  50. }
  51. }
  52. public override int ReadByte()
  53. {
  54. if (_currentStream == null)
  55. {
  56. if (!_first)
  57. return 0;
  58. Asn1OctetStringParser s = (Asn1OctetStringParser)_parser.ReadObject();
  59. if (s == null)
  60. return 0;
  61. _first = false;
  62. _currentStream = s.GetOctetStream();
  63. }
  64. for (;;)
  65. {
  66. int b = _currentStream.ReadByte();
  67. if (b >= 0)
  68. {
  69. return b;
  70. }
  71. Asn1OctetStringParser aos = (Asn1OctetStringParser)_parser.ReadObject();
  72. if (aos == null)
  73. {
  74. _currentStream = null;
  75. return -1;
  76. }
  77. _currentStream = aos.GetOctetStream();
  78. }
  79. }
  80. }
  81. }
  82. #pragma warning restore
  83. #endif