ConstructedOctetStream.cs 1.8 KB

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