diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-09-03 00:36:40 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-09-03 00:36:40 +0700 |
commit | aa5f3f0929c50fc942325f18ed7ae48129d4c992 (patch) | |
tree | 0382f7336cb55c4a01bd38782d7694d1cb094921 /crypto/src/tls | |
parent | Clean up tests (diff) | |
download | BouncyCastle.NET-ed25519-aa5f3f0929c50fc942325f18ed7ae48129d4c992.tar.xz |
Stream modernization
Diffstat (limited to 'crypto/src/tls')
-rw-r--r-- | crypto/src/tls/ByteQueue.cs | 59 | ||||
-rw-r--r-- | crypto/src/tls/ByteQueueInputStream.cs | 20 | ||||
-rw-r--r-- | crypto/src/tls/ByteQueueOutputStream.cs | 7 | ||||
-rw-r--r-- | crypto/src/tls/TlsStream.cs | 12 |
4 files changed, 77 insertions, 21 deletions
diff --git a/crypto/src/tls/ByteQueue.cs b/crypto/src/tls/ByteQueue.cs index 45bec8be4..b85106528 100644 --- a/crypto/src/tls/ByteQueue.cs +++ b/crypto/src/tls/ByteQueue.cs @@ -56,6 +56,9 @@ namespace Org.BouncyCastle.Tls /// <param name="len">How many bytes to read from the array.</param> public void AddData(byte[] buf, int off, int len) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + AddData(buf.AsSpan(off, len)); +#else if (m_readOnlyBuf) throw new InvalidOperationException("Cannot add data to read-only buffer"); @@ -86,8 +89,46 @@ namespace Org.BouncyCastle.Tls Array.Copy(buf, off, m_databuf, m_skipped + m_available, len); m_available += len; +#endif } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public void AddData(ReadOnlySpan<byte> buffer) + { + if (m_readOnlyBuf) + throw new InvalidOperationException("Cannot add data to read-only buffer"); + + int len = buffer.Length; + if (m_available == 0) + { + if (len > m_databuf.Length) + { + int desiredSize = NextTwoPow(len | 256); + m_databuf = new byte[desiredSize]; + } + m_skipped = 0; + } + else if ((m_skipped + m_available + len) > m_databuf.Length) + { + int desiredSize = NextTwoPow(m_available + len); + if (desiredSize > m_databuf.Length) + { + byte[] tmp = new byte[desiredSize]; + Array.Copy(m_databuf, m_skipped, tmp, 0, m_available); + m_databuf = tmp; + } + else + { + Array.Copy(m_databuf, m_skipped, m_databuf, 0, m_available); + } + m_skipped = 0; + } + + buffer.CopyTo(m_databuf.AsSpan(m_skipped + m_available)); + m_available += len; + } +#endif + /// <returns>The number of bytes which are available in this buffer.</returns> public int Available { @@ -124,6 +165,16 @@ namespace Org.BouncyCastle.Tls Array.Copy(m_databuf, m_skipped + skip, buf, offset, len); } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public void Read(Span<byte> buffer, int skip) + { + if ((m_available - skip) < buffer.Length) + throw new InvalidOperationException("Not enough data to read"); + + m_databuf.AsSpan(m_skipped + skip, buffer.Length).CopyTo(buffer); + } +#endif + /// <summary>Return a <see cref="HandshakeMessageInput"/> over some bytes at the beginning of the data. /// </summary> /// <param name="length">How many bytes will be readable.</param> @@ -182,6 +233,14 @@ namespace Org.BouncyCastle.Tls RemoveData(skip + len); } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public void RemoveData(Span<byte> buffer, int skip) + { + Read(buffer, skip); + RemoveData(skip + buffer.Length); + } +#endif + public byte[] RemoveData(int len, int skip) { byte[] buf = new byte[len]; diff --git a/crypto/src/tls/ByteQueueInputStream.cs b/crypto/src/tls/ByteQueueInputStream.cs index 0b15071ad..ab26faa98 100644 --- a/crypto/src/tls/ByteQueueInputStream.cs +++ b/crypto/src/tls/ByteQueueInputStream.cs @@ -40,6 +40,15 @@ namespace Org.BouncyCastle.Tls return bytesToRead; } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public override int Read(Span<byte> buffer) + { + int bytesToRead = System.Math.Min(m_buffer.Available, buffer.Length); + m_buffer.RemoveData(buffer[..bytesToRead], 0); + return bytesToRead; + } +#endif + public override int ReadByte() { if (m_buffer.Available == 0) @@ -59,16 +68,5 @@ namespace Org.BouncyCastle.Tls { get { return m_buffer.Available; } } - -#if PORTABLE - //protected override void Dispose(bool disposing) - //{ - // base.Dispose(disposing); - //} -#else - public override void Close() - { - } -#endif } } diff --git a/crypto/src/tls/ByteQueueOutputStream.cs b/crypto/src/tls/ByteQueueOutputStream.cs index 441a3773a..ecb1f865d 100644 --- a/crypto/src/tls/ByteQueueOutputStream.cs +++ b/crypto/src/tls/ByteQueueOutputStream.cs @@ -27,6 +27,13 @@ namespace Org.BouncyCastle.Tls m_buffer.AddData(buffer, offset, count); } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public override void Write(ReadOnlySpan<byte> buffer) + { + m_buffer.AddData(buffer); + } +#endif + public override void WriteByte(byte value) { m_buffer.AddData(new byte[]{ value }, 0, 1); diff --git a/crypto/src/tls/TlsStream.cs b/crypto/src/tls/TlsStream.cs index f3dea1574..01b990799 100644 --- a/crypto/src/tls/TlsStream.cs +++ b/crypto/src/tls/TlsStream.cs @@ -28,7 +28,6 @@ namespace Org.BouncyCastle.Tls get { return true; } } -#if PORTABLE protected override void Dispose(bool disposing) { if (disposing) @@ -37,13 +36,6 @@ namespace Org.BouncyCastle.Tls } base.Dispose(disposing); } -#else - public override void Close() - { - m_handler.Close(); - base.Close(); - } -#endif public override void Flush() { @@ -69,7 +61,7 @@ namespace Org.BouncyCastle.Tls public override int ReadByte() { byte[] buf = new byte[1]; - int ret = Read(buf, 0, 1); + int ret = m_handler.ReadApplicationData(buf, 0, 1); return ret <= 0 ? -1 : buf[0]; } @@ -90,7 +82,7 @@ namespace Org.BouncyCastle.Tls public override void WriteByte(byte value) { - Write(new byte[]{ value }, 0, 1); + m_handler.WriteApplicationData(new byte[]{ value }, 0, 1); } } } |