diff options
Diffstat (limited to 'crypto/src/util/io')
-rw-r--r-- | crypto/src/util/io/BinaryReaders.cs | 94 | ||||
-rw-r--r-- | crypto/src/util/io/BinaryWriters.cs | 86 | ||||
-rw-r--r-- | crypto/src/util/io/Streams.cs | 13 | ||||
-rw-r--r-- | crypto/src/util/io/compression/Bzip2.cs | 21 | ||||
-rw-r--r-- | crypto/src/util/io/compression/ZLib.cs | 46 | ||||
-rw-r--r-- | crypto/src/util/io/compression/Zip.cs | 33 |
6 files changed, 287 insertions, 6 deletions
diff --git a/crypto/src/util/io/BinaryReaders.cs b/crypto/src/util/io/BinaryReaders.cs new file mode 100644 index 000000000..c5f99a712 --- /dev/null +++ b/crypto/src/util/io/BinaryReaders.cs @@ -0,0 +1,94 @@ +using System; +using System.IO; + +namespace Org.BouncyCastle.Utilities.IO +{ + public static class BinaryReaders + { + public static byte[] ReadBytesFully(BinaryReader binaryReader, int count) + { + byte[] bytes = binaryReader.ReadBytes(count); + if (bytes == null || bytes.Length != count) + throw new EndOfStreamException(); + return bytes; + } + + public static short ReadInt16BigEndian(BinaryReader binaryReader) + { + short n = binaryReader.ReadInt16(); + return BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n; + } + + public static short ReadInt16LittleEndian(BinaryReader binaryReader) + { + short n = binaryReader.ReadInt16(); + return BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n); + } + + public static int ReadInt32BigEndian(BinaryReader binaryReader) + { + int n = binaryReader.ReadInt32(); + return BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n; + } + + public static int ReadInt32LittleEndian(BinaryReader binaryReader) + { + int n = binaryReader.ReadInt32(); + return BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n); + } + + public static long ReadInt64BigEndian(BinaryReader binaryReader) + { + long n = binaryReader.ReadInt64(); + return BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n; + } + + public static long ReadInt64LittleEndian(BinaryReader binaryReader) + { + long n = binaryReader.ReadInt64(); + return BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n); + } + + [CLSCompliant(false)] + public static ushort ReadUInt16BigEndian(BinaryReader binaryReader) + { + ushort n = binaryReader.ReadUInt16(); + return BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n; + } + + [CLSCompliant(false)] + public static ushort ReadUInt16LittleEndian(BinaryReader binaryReader) + { + ushort n = binaryReader.ReadUInt16(); + return BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n); + } + + [CLSCompliant(false)] + public static uint ReadUInt32BigEndian(BinaryReader binaryReader) + { + uint n = binaryReader.ReadUInt32(); + return BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n; + } + + [CLSCompliant(false)] + public static uint ReadUInt32LittleEndian(BinaryReader binaryReader) + { + uint n = binaryReader.ReadUInt32(); + return BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n); + } + + [CLSCompliant(false)] + public static ulong ReadUInt64BigEndian(BinaryReader binaryReader) + { + ulong n = binaryReader.ReadUInt64(); + return BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n; + } + + [CLSCompliant(false)] + public static ulong ReadUInt64LittleEndian(BinaryReader binaryReader) + { + ulong n = binaryReader.ReadUInt64(); + return BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n); + } + } +} diff --git a/crypto/src/util/io/BinaryWriters.cs b/crypto/src/util/io/BinaryWriters.cs new file mode 100644 index 000000000..6650dcda5 --- /dev/null +++ b/crypto/src/util/io/BinaryWriters.cs @@ -0,0 +1,86 @@ +using System; +using System.IO; + +namespace Org.BouncyCastle.Utilities.IO +{ + public static class BinaryWriters + { + public static void WriteInt16BigEndian(BinaryWriter binaryWriter, short n) + { + short bigEndian = BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n; + binaryWriter.Write(bigEndian); + } + + public static void WriteInt16LittleEndian(BinaryWriter binaryWriter, short n) + { + short littleEndian = BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n); + binaryWriter.Write(littleEndian); + } + + public static void WriteInt32BigEndian(BinaryWriter binaryWriter, int n) + { + int bigEndian = BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n; + binaryWriter.Write(bigEndian); + } + + public static void WriteInt32LittleEndian(BinaryWriter binaryWriter, int n) + { + int littleEndian = BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n); + binaryWriter.Write(littleEndian); + } + + public static void WriteInt64BigEndian(BinaryWriter binaryWriter, long n) + { + long bigEndian = BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n; + binaryWriter.Write(bigEndian); + } + + public static void WriteInt64LittleEndian(BinaryWriter binaryWriter, long n) + { + long littleEndian = BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n); + binaryWriter.Write(littleEndian); + } + + [CLSCompliant(false)] + public static void WriteUInt16BigEndian(BinaryWriter binaryWriter, ushort n) + { + ushort bigEndian = BitConverter.IsLittleEndian ? Shorts.ReverseBytes(n) : n; + binaryWriter.Write(bigEndian); + } + + [CLSCompliant(false)] + public static void WriteUInt16LittleEndian(BinaryWriter binaryWriter, ushort n) + { + ushort littleEndian = BitConverter.IsLittleEndian ? n : Shorts.ReverseBytes(n); + binaryWriter.Write(littleEndian); + } + + [CLSCompliant(false)] + public static void WriteUInt32BigEndian(BinaryWriter binaryWriter, uint n) + { + uint bigEndian = BitConverter.IsLittleEndian ? Integers.ReverseBytes(n) : n; + binaryWriter.Write(bigEndian); + } + + [CLSCompliant(false)] + public static void WriteUInt32LittleEndian(BinaryWriter binaryWriter, uint n) + { + uint littleEndian = BitConverter.IsLittleEndian ? n : Integers.ReverseBytes(n); + binaryWriter.Write(littleEndian); + } + + [CLSCompliant(false)] + public static void WriteUInt64BigEndian(BinaryWriter binaryWriter, ulong n) + { + ulong bigEndian = BitConverter.IsLittleEndian ? Longs.ReverseBytes(n) : n; + binaryWriter.Write(bigEndian); + } + + [CLSCompliant(false)] + public static void WriteUInt64LittleEndian(BinaryWriter binaryWriter, ulong n) + { + ulong littleEndian = BitConverter.IsLittleEndian ? n : Longs.ReverseBytes(n); + binaryWriter.Write(littleEndian); + } + } +} diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs index c23332909..da8f01068 100644 --- a/crypto/src/util/io/Streams.cs +++ b/crypto/src/util/io/Streams.cs @@ -3,14 +3,10 @@ using System.IO; namespace Org.BouncyCastle.Utilities.IO { - public sealed class Streams + public static class Streams { private const int BufferSize = 4096; - private Streams() - { - } - public static void Drain(Stream inStr) { inStr.CopyTo(Stream.Null, BufferSize); @@ -64,7 +60,12 @@ namespace Org.BouncyCastle.Utilities.IO return buf.ToArray(); } - public static byte[] ReadAllLimited(Stream inStr, int limit) + public static byte[] ReadAll(MemoryStream inStr) + { + return inStr.ToArray(); + } + + public static byte[] ReadAllLimited(Stream inStr, int limit) { MemoryStream buf = new MemoryStream(); PipeAllLimited(inStr, limit, buf); diff --git a/crypto/src/util/io/compression/Bzip2.cs b/crypto/src/util/io/compression/Bzip2.cs new file mode 100644 index 000000000..72b006dc9 --- /dev/null +++ b/crypto/src/util/io/compression/Bzip2.cs @@ -0,0 +1,21 @@ +using System.IO; + +namespace Org.BouncyCastle.Utilities.IO.Compression +{ + using Impl = Utilities.Bzip2; + + internal static class Bzip2 + { + internal static Stream CompressOutput(Stream stream, bool leaveOpen = false) + { + return leaveOpen + ? new Impl.CBZip2OutputStreamLeaveOpen(stream) + : new Impl.CBZip2OutputStream(stream); + } + + internal static Stream DecompressInput(Stream stream) + { + return new Impl.CBZip2InputStream(stream); + } + } +} diff --git a/crypto/src/util/io/compression/ZLib.cs b/crypto/src/util/io/compression/ZLib.cs new file mode 100644 index 000000000..1254da012 --- /dev/null +++ b/crypto/src/util/io/compression/ZLib.cs @@ -0,0 +1,46 @@ +using System.IO; + +#if NET6_0_OR_GREATER +using System.IO.Compression; +#else +using Org.BouncyCastle.Utilities.Zlib; +#endif + +namespace Org.BouncyCastle.Utilities.IO.Compression +{ + internal static class ZLib + { + internal static Stream CompressOutput(Stream stream, int zlibCompressionLevel, bool leaveOpen = false) + { +#if NET6_0_OR_GREATER + return new ZLibStream(stream, GetCompressionLevel(zlibCompressionLevel), leaveOpen); +#else + return leaveOpen + ? new ZOutputStreamLeaveOpen(stream, zlibCompressionLevel, false) + : new ZOutputStream(stream, zlibCompressionLevel, false); +#endif + } + + internal static Stream DecompressInput(Stream stream) + { +#if NET6_0_OR_GREATER + return new ZLibStream(stream, CompressionMode.Decompress, leaveOpen: false); +#else + return new ZInputStream(stream); +#endif + } + +#if NET6_0_OR_GREATER + internal static CompressionLevel GetCompressionLevel(int zlibCompressionLevel) + { + return zlibCompressionLevel switch + { + 0 => CompressionLevel.NoCompression, + 1 or 2 or 3 => CompressionLevel.Fastest, + 7 or 8 or 9 => CompressionLevel.SmallestSize, + _ => CompressionLevel.Optimal, + }; + } +#endif + } +} diff --git a/crypto/src/util/io/compression/Zip.cs b/crypto/src/util/io/compression/Zip.cs new file mode 100644 index 000000000..f2773d63b --- /dev/null +++ b/crypto/src/util/io/compression/Zip.cs @@ -0,0 +1,33 @@ +using System.IO; + +#if NET6_0_OR_GREATER +using System.IO.Compression; +#else +using Org.BouncyCastle.Utilities.Zlib; +#endif + +namespace Org.BouncyCastle.Utilities.IO.Compression +{ + internal static class Zip + { + internal static Stream CompressOutput(Stream stream, int zlibCompressionLevel, bool leaveOpen = false) + { +#if NET6_0_OR_GREATER + return new DeflateStream(stream, ZLib.GetCompressionLevel(zlibCompressionLevel), leaveOpen); +#else + return leaveOpen + ? new ZOutputStreamLeaveOpen(stream, zlibCompressionLevel, true) + : new ZOutputStream(stream, zlibCompressionLevel, true); +#endif + } + + internal static Stream DecompressInput(Stream stream) + { +#if NET6_0_OR_GREATER + return new DeflateStream(stream, CompressionMode.Decompress, leaveOpen: false); +#else + return new ZInputStream(stream, true); +#endif + } + } +} |