DLBitStringParser.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1
  6. {
  7. /// <summary>Parser for a DL encoded BIT STRING.</summary>
  8. internal class DLBitStringParser
  9. : Asn1BitStringParser
  10. {
  11. private readonly DefiniteLengthInputStream m_stream;
  12. private int m_padBits = 0;
  13. internal DLBitStringParser(DefiniteLengthInputStream stream)
  14. {
  15. m_stream = stream;
  16. }
  17. public Stream GetBitStream()
  18. {
  19. return GetBitStream(false);
  20. }
  21. public Stream GetOctetStream()
  22. {
  23. return GetBitStream(true);
  24. }
  25. public int PadBits
  26. {
  27. get { return m_padBits; }
  28. }
  29. public Asn1Object ToAsn1Object()
  30. {
  31. try
  32. {
  33. return DerBitString.CreatePrimitive(m_stream.ToArray());
  34. }
  35. catch (IOException e)
  36. {
  37. throw new Asn1ParsingException("IOException converting stream to byte array: " + e.Message, e);
  38. }
  39. }
  40. private Stream GetBitStream(bool octetAligned)
  41. {
  42. int length = m_stream.Remaining;
  43. if (length < 1)
  44. throw new InvalidOperationException("content octets cannot be empty");
  45. m_padBits = m_stream.ReadByte();
  46. if (m_padBits > 0)
  47. {
  48. if (length < 2)
  49. throw new InvalidOperationException("zero length data with non-zero pad bits");
  50. if (m_padBits > 7)
  51. throw new InvalidOperationException("pad bits cannot be greater than 7 or less than 0");
  52. if (octetAligned)
  53. throw new IOException("expected octet-aligned bitstring, but found padBits: " + m_padBits);
  54. }
  55. return m_stream;
  56. }
  57. }
  58. }
  59. #pragma warning restore
  60. #endif