diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-22 15:22:28 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-11-22 15:22:28 +0700 |
commit | 2ee089e648a2aefe6d84244855b5c923b1496881 (patch) | |
tree | 89866bac74edd42edba77b26e0bb0b0dfa6aa337 /crypto/src/asn1/BerBitStringParser.cs | |
parent | Perf. opts. (diff) | |
download | BouncyCastle.NET-ed25519-2ee089e648a2aefe6d84244855b5c923b1496881.tar.xz |
Add BitString parsers
Diffstat (limited to 'crypto/src/asn1/BerBitStringParser.cs')
-rw-r--r-- | crypto/src/asn1/BerBitStringParser.cs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/crypto/src/asn1/BerBitStringParser.cs b/crypto/src/asn1/BerBitStringParser.cs new file mode 100644 index 000000000..656538ba1 --- /dev/null +++ b/crypto/src/asn1/BerBitStringParser.cs @@ -0,0 +1,56 @@ +using System; +using System.IO; + +using Org.BouncyCastle.Utilities.IO; + +namespace Org.BouncyCastle.Asn1 +{ + /// <summary>A parser for indefinite-length BIT STRINGs.</summary> + internal class BerBitStringParser + : Asn1BitStringParser + { + private readonly Asn1StreamParser m_parser; + + private ConstructedBitStream m_bitStream; + + internal BerBitStringParser(Asn1StreamParser parser) + { + m_parser = parser; + } + + public Stream GetOctetStream() + { + return m_bitStream = new ConstructedBitStream(m_parser, true); + } + + public Stream GetBitStream() + { + return m_bitStream = new ConstructedBitStream(m_parser, false); + } + + public int PadBits + { + get { return m_bitStream.PadBits; } + } + + public Asn1Object ToAsn1Object() + { + try + { + return Parse(m_parser); + } + catch (IOException e) + { + throw new Asn1ParsingException("IOException converting stream to byte array: " + e.Message, e); + } + } + + internal static BerBitString Parse(Asn1StreamParser sp) + { + ConstructedBitStream bitStream = new ConstructedBitStream(sp, false); + byte[] data = Streams.ReadAll(bitStream); + int padBits = bitStream.PadBits; + return new BerBitString(data, padBits); + } + } +} |