From f89b319a11832ccf4db3d11faccba09bf5239ba8 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 19 May 2022 20:20:04 +0700 Subject: Add ValidateBufferArguments --- crypto/src/util/io/Streams.cs | 80 +++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 34 deletions(-) (limited to 'crypto/src/util') diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs index a86367e56..3623dfe39 100644 --- a/crypto/src/util/io/Streams.cs +++ b/crypto/src/util/io/Streams.cs @@ -19,38 +19,6 @@ namespace Org.BouncyCastle.Utilities.IO } } - public static byte[] ReadAll(Stream inStr) - { - MemoryStream buf = new MemoryStream(); - PipeAll(inStr, buf); - return buf.ToArray(); - } - - public static byte[] ReadAllLimited(Stream inStr, int limit) - { - MemoryStream buf = new MemoryStream(); - PipeAllLimited(inStr, limit, buf); - return buf.ToArray(); - } - - public static int ReadFully(Stream inStr, byte[] buf) - { - return ReadFully(inStr, buf, 0, buf.Length); - } - - public static int ReadFully(Stream inStr, byte[] buf, int off, int len) - { - int totalRead = 0; - while (totalRead < len) - { - int numRead = inStr.Read(buf, off + totalRead, len - totalRead); - if (numRead < 1) - break; - totalRead += numRead; - } - return totalRead; - } - /// Write the full contents of inStr to the destination stream outStr. /// Source stream. /// Destination stream. @@ -105,8 +73,52 @@ namespace Org.BouncyCastle.Utilities.IO return total; } - /// - public static void WriteBufTo(MemoryStream buf, Stream output) + public static byte[] ReadAll(Stream inStr) + { + MemoryStream buf = new MemoryStream(); + PipeAll(inStr, buf); + return buf.ToArray(); + } + + public static byte[] ReadAllLimited(Stream inStr, int limit) + { + MemoryStream buf = new MemoryStream(); + PipeAllLimited(inStr, limit, buf); + return buf.ToArray(); + } + + public static int ReadFully(Stream inStr, byte[] buf) + { + return ReadFully(inStr, buf, 0, buf.Length); + } + + public static int ReadFully(Stream inStr, byte[] buf, int off, int len) + { + int totalRead = 0; + while (totalRead < len) + { + int numRead = inStr.Read(buf, off + totalRead, len - totalRead); + if (numRead < 1) + break; + totalRead += numRead; + } + return totalRead; + } + + public static void ValidateBufferArguments(byte[] buffer, int offset, int count) + { + if (buffer == null) + throw new ArgumentNullException("buffer"); + int available = buffer.Length - offset; + if ((offset | available) < 0) + throw new ArgumentOutOfRangeException("offset"); + int remaining = available - count; + if ((count | remaining) < 0) + throw new ArgumentOutOfRangeException("count"); + } + + /// + public static void WriteBufTo(MemoryStream buf, Stream output) { buf.WriteTo(output); } -- cgit 1.4.1