diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-05-23 22:44:57 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-05-23 22:44:57 +0700 |
commit | 98b3ce6512812cd1d91c60ca0e69d69c32838d8e (patch) | |
tree | 8af69c097b389fe494e97401f7808590314a5b6b /crypto/src/asn1/BEROctetStringGenerator.cs | |
parent | Add ValidateBufferArguments (diff) | |
download | BouncyCastle.NET-ed25519-98b3ce6512812cd1d91c60ca0e69d69c32838d8e.tar.xz |
Work on Stream classes
- consistent naming, ordering - CanRead/Seek/Write fixes - argument validation - some cases of blocking zero-byte Read calls
Diffstat (limited to 'crypto/src/asn1/BEROctetStringGenerator.cs')
-rw-r--r-- | crypto/src/asn1/BEROctetStringGenerator.cs | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/crypto/src/asn1/BEROctetStringGenerator.cs b/crypto/src/asn1/BEROctetStringGenerator.cs index de0a6c0b8..bed40b566 100644 --- a/crypto/src/asn1/BEROctetStringGenerator.cs +++ b/crypto/src/asn1/BEROctetStringGenerator.cs @@ -60,48 +60,49 @@ namespace Org.BouncyCastle.Asn1 _derOut = Asn1OutputStream.Create(_gen.Out, Asn1Encodable.Der); } - public override void WriteByte( - byte b) + public override void Write(byte[] buffer, int offset, int count) { - _buf[_off++] = b; + Streams.ValidateBufferArguments(buffer, offset, count); - if (_off == _buf.Length) - { - DerOctetString.Encode(_derOut, _buf, 0, _off); - _off = 0; - } - } - - public override void Write(byte[] b, int off, int len) - { int bufLen = _buf.Length; int available = bufLen - _off; - if (len < available) + if (count < available) { - Array.Copy(b, off, _buf, _off, len); - _off += len; + Array.Copy(buffer, offset, _buf, _off, count); + _off += count; return; } - int count = 0; + int pos = 0; if (_off > 0) { - Array.Copy(b, off, _buf, _off, available); - count += available; + Array.Copy(buffer, offset, _buf, _off, available); + pos += available; DerOctetString.Encode(_derOut, _buf, 0, bufLen); } int remaining; - while ((remaining = len - count) >= bufLen) + while ((remaining = count - pos) >= bufLen) { - DerOctetString.Encode(_derOut, b, off + count, bufLen); - count += bufLen; + DerOctetString.Encode(_derOut, buffer, offset + pos, bufLen); + pos += bufLen; } - Array.Copy(b, off + count, _buf, 0, remaining); + Array.Copy(buffer, offset + pos, _buf, 0, remaining); this._off = remaining; } + public override void WriteByte(byte value) + { + _buf[_off++] = value; + + if (_off == _buf.Length) + { + DerOctetString.Encode(_derOut, _buf, 0, _off); + _off = 0; + } + } + #if PORTABLE protected override void Dispose(bool disposing) { @@ -119,7 +120,7 @@ namespace Org.BouncyCastle.Asn1 base.Dispose(disposing); } #else - public override void Close() + public override void Close() { if (_off != 0) { |