summary refs log tree commit diff
path: root/crypto/src/util
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-24 15:49:27 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-24 15:49:27 +0700
commitbe97c80fdecadac37413a6a6a8de417e0332f6bf (patch)
treedef3bf31b937a4e59458574f6441036ffdf61616 /crypto/src/util
parentUse correct OID (diff)
downloadBouncyCastle.NET-ed25519-be97c80fdecadac37413a6a6a8de417e0332f6bf.tar.xz
Use platform compression where available
- Move Bzip2 code into Utilities
Diffstat (limited to '')
-rw-r--r--crypto/src/util/bzip2/BZip2Constants.cs (renamed from crypto/src/bzip2/BZip2Constants.cs)4
-rw-r--r--crypto/src/util/bzip2/CBZip2InputStream.cs (renamed from crypto/src/bzip2/CBZip2InputStream.cs)3
-rw-r--r--crypto/src/util/bzip2/CBZip2OutputStream.cs (renamed from crypto/src/bzip2/CBZip2OutputStream.cs)22
-rw-r--r--crypto/src/util/bzip2/CRC.cs (renamed from crypto/src/bzip2/CRC.cs)5
-rw-r--r--crypto/src/util/io/compression/Bzip2.cs21
-rw-r--r--crypto/src/util/io/compression/ZLib.cs46
-rw-r--r--crypto/src/util/io/compression/Zip.cs33
-rw-r--r--crypto/src/util/zlib/ZOutputStream.cs34
8 files changed, 157 insertions, 11 deletions
diff --git a/crypto/src/bzip2/BZip2Constants.cs b/crypto/src/util/bzip2/BZip2Constants.cs
index 81db7fa57..6fc15e55f 100644
--- a/crypto/src/bzip2/BZip2Constants.cs
+++ b/crypto/src/util/bzip2/BZip2Constants.cs
@@ -22,9 +22,7 @@
  * great code.
  */
 
-using System;
-
-namespace Org.BouncyCastle.Bzip2
+namespace Org.BouncyCastle.Utilities.Bzip2
 {
     /**
     * Base class for both the compress and decompress classes.
diff --git a/crypto/src/bzip2/CBZip2InputStream.cs b/crypto/src/util/bzip2/CBZip2InputStream.cs
index b73e52a01..7879f28af 100644
--- a/crypto/src/bzip2/CBZip2InputStream.cs
+++ b/crypto/src/util/bzip2/CBZip2InputStream.cs
@@ -26,10 +26,9 @@ using System;
 using System.Diagnostics;
 using System.IO;
 
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
-namespace Org.BouncyCastle.Bzip2
+namespace Org.BouncyCastle.Utilities.Bzip2
 {
 	/**
     * An input stream that decompresses from the BZip2 format (with the file
diff --git a/crypto/src/bzip2/CBZip2OutputStream.cs b/crypto/src/util/bzip2/CBZip2OutputStream.cs
index 5521dce56..b896f36c6 100644
--- a/crypto/src/bzip2/CBZip2OutputStream.cs
+++ b/crypto/src/util/bzip2/CBZip2OutputStream.cs
@@ -27,10 +27,9 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
-namespace Org.BouncyCastle.Bzip2
+namespace Org.BouncyCastle.Utilities.Bzip2
 {
 	/**
     * An output stream that compresses into the BZip2 format (with the file
@@ -1598,4 +1597,23 @@ namespace Org.BouncyCastle.Bzip2
             return a;
         }
     }
+
+    public class CBZip2OutputStreamLeaveOpen
+        : CBZip2OutputStream
+    {
+        public CBZip2OutputStreamLeaveOpen(Stream outStream)
+            : base(outStream)
+        {
+        }
+
+        public CBZip2OutputStreamLeaveOpen(Stream outStream, int blockSize)
+            : base(outStream, blockSize)
+        {
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            Detach(disposing);
+        }
+    }
 }
diff --git a/crypto/src/bzip2/CRC.cs b/crypto/src/util/bzip2/CRC.cs
index 70d05e084..30c7e9c7d 100644
--- a/crypto/src/bzip2/CRC.cs
+++ b/crypto/src/util/bzip2/CRC.cs
@@ -22,12 +22,9 @@
  * great code.
  */
 
-using System;
 using System.Diagnostics;
 
-using Org.BouncyCastle.Utilities;
-
-namespace Org.BouncyCastle.Bzip2
+namespace Org.BouncyCastle.Utilities.Bzip2
 {
     /**
     * A simple class the hold and calculate the CRC for sanity checking
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
+        }
+    }
+}
diff --git a/crypto/src/util/zlib/ZOutputStream.cs b/crypto/src/util/zlib/ZOutputStream.cs
index 301516e57..51a5050dd 100644
--- a/crypto/src/util/zlib/ZOutputStream.cs
+++ b/crypto/src/util/zlib/ZOutputStream.cs
@@ -264,4 +264,38 @@ namespace Org.BouncyCastle.Utilities.Zlib
             Write(buf1, 0, 1);
         }
     }
+
+    public class ZOutputStreamLeaveOpen
+        : ZOutputStream
+    {
+        public ZOutputStreamLeaveOpen(Stream output)
+            : base(output)
+        {
+        }
+
+        public ZOutputStreamLeaveOpen(Stream output, bool nowrap)
+            : base(output, nowrap)
+        {
+        }
+
+        public ZOutputStreamLeaveOpen(Stream output, ZStream z)
+            : base(output, z)
+        {
+        }
+
+        public ZOutputStreamLeaveOpen(Stream output, int level)
+            : base(output, level)
+        {
+        }
+
+        public ZOutputStreamLeaveOpen(Stream output, int level, bool nowrap)
+            : base(output, level, nowrap)
+        {
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            Detach(disposing);
+        }
+    }
 }