diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-24 15:49:27 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-24 15:49:27 +0700 |
commit | be97c80fdecadac37413a6a6a8de417e0332f6bf (patch) | |
tree | def3bf31b937a4e59458574f6441036ffdf61616 | |
parent | Use correct OID (diff) | |
download | BouncyCastle.NET-ed25519-be97c80fdecadac37413a6a6a8de417e0332f6bf.tar.xz |
Use platform compression where available
- Move Bzip2 code into Utilities
-rw-r--r-- | crypto/src/cms/CMSCompressedData.cs | 8 | ||||
-rw-r--r-- | crypto/src/cms/CMSCompressedDataGenerator.cs | 10 | ||||
-rw-r--r-- | crypto/src/cms/CMSCompressedDataParser.cs | 5 | ||||
-rw-r--r-- | crypto/src/cms/CMSCompressedDataStreamGenerator.cs | 12 | ||||
-rw-r--r-- | crypto/src/openpgp/PgpCompressedData.cs | 23 | ||||
-rw-r--r-- | crypto/src/openpgp/PgpCompressedDataGenerator.cs | 58 | ||||
-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.cs | 21 | ||||
-rw-r--r-- | crypto/src/util/io/compression/ZLib.cs | 46 | ||||
-rw-r--r-- | crypto/src/util/io/compression/Zip.cs | 33 | ||||
-rw-r--r-- | crypto/src/util/zlib/ZOutputStream.cs | 34 |
14 files changed, 203 insertions, 81 deletions
diff --git a/crypto/src/cms/CMSCompressedData.cs b/crypto/src/cms/CMSCompressedData.cs index 21651f041..5f8165005 100644 --- a/crypto/src/cms/CMSCompressedData.cs +++ b/crypto/src/cms/CMSCompressedData.cs @@ -1,10 +1,9 @@ -using System; using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Zlib; +using Org.BouncyCastle.Utilities.IO.Compression; namespace Org.BouncyCastle.Cms { @@ -45,7 +44,7 @@ namespace Org.BouncyCastle.Cms ContentInfo content = comData.EncapContentInfo; Asn1OctetString bytes = (Asn1OctetString) content.Content; - ZInputStream zIn = new ZInputStream(bytes.GetOctetStream()); + Stream zIn = ZLib.DecompressInput(bytes.GetOctetStream()); try { @@ -76,8 +75,7 @@ namespace Org.BouncyCastle.Cms ContentInfo content = comData.EncapContentInfo; Asn1OctetString bytes = (Asn1OctetString)content.Content; - - ZInputStream zIn = new ZInputStream(new MemoryStream(bytes.GetOctets(), false)); + Stream zIn = ZLib.DecompressInput(bytes.GetOctetStream()); try { diff --git a/crypto/src/cms/CMSCompressedDataGenerator.cs b/crypto/src/cms/CMSCompressedDataGenerator.cs index bea04752a..872c4a144 100644 --- a/crypto/src/cms/CMSCompressedDataGenerator.cs +++ b/crypto/src/cms/CMSCompressedDataGenerator.cs @@ -5,7 +5,6 @@ using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Zlib; namespace Org.BouncyCastle.Cms { @@ -22,9 +21,12 @@ namespace Org.BouncyCastle.Cms */ public class CmsCompressedDataGenerator { - public const string ZLib = "1.2.840.113549.1.9.16.3.8"; + public const string ZLibOid = "1.2.840.113549.1.9.16.3.8"; - public CmsCompressedDataGenerator() + [Obsolete("Use 'ZLibOid' instead")] + public const string ZLib = ZLibOid; + + public CmsCompressedDataGenerator() { } @@ -41,7 +43,7 @@ namespace Org.BouncyCastle.Cms try { MemoryStream bOut = new MemoryStream(); - ZOutputStream zOut = new ZOutputStream(bOut, JZlib.Z_DEFAULT_COMPRESSION); + Stream zOut = Utilities.IO.Compression.ZLib.CompressOutput(bOut, -1); content.Write(zOut); diff --git a/crypto/src/cms/CMSCompressedDataParser.cs b/crypto/src/cms/CMSCompressedDataParser.cs index b107ff608..38ff88968 100644 --- a/crypto/src/cms/CMSCompressedDataParser.cs +++ b/crypto/src/cms/CMSCompressedDataParser.cs @@ -3,7 +3,7 @@ using System.IO; using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; -using Org.BouncyCastle.Utilities.Zlib; +using Org.BouncyCastle.Utilities.IO.Compression; namespace Org.BouncyCastle.Cms { @@ -44,8 +44,9 @@ namespace Org.BouncyCastle.Cms ContentInfoParser content = comData.GetEncapContentInfo(); Asn1OctetStringParser bytes = (Asn1OctetStringParser)content.GetContent(Asn1Tags.OctetString); + Stream zIn = ZLib.DecompressInput(bytes.GetOctetStream()); - return new CmsTypedStream(content.ContentType.ToString(), new ZInputStream(bytes.GetOctetStream())); + return new CmsTypedStream(content.ContentType.ToString(), zIn); } catch (IOException e) { diff --git a/crypto/src/cms/CMSCompressedDataStreamGenerator.cs b/crypto/src/cms/CMSCompressedDataStreamGenerator.cs index 9a9c29b01..855367320 100644 --- a/crypto/src/cms/CMSCompressedDataStreamGenerator.cs +++ b/crypto/src/cms/CMSCompressedDataStreamGenerator.cs @@ -6,7 +6,6 @@ using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.IO; -using Org.BouncyCastle.Utilities.Zlib; namespace Org.BouncyCastle.Cms { @@ -27,7 +26,10 @@ namespace Org.BouncyCastle.Cms */ public class CmsCompressedDataStreamGenerator { - public const string ZLib = "1.2.840.113549.1.9.16.3.8"; + public const string ZLibOid = "1.2.840.113549.1.9.16.3.8"; + + [Obsolete("Use 'ZLibOid' instead")] + public const string ZLib = ZLibOid; private int _bufferSize; @@ -88,19 +90,19 @@ namespace Org.BouncyCastle.Cms eiGen.GetRawOutputStream(), 0, true, _bufferSize); return new CmsCompressedOutputStream( - new ZOutputStream(octetStream, JZlib.Z_DEFAULT_COMPRESSION), sGen, cGen, eiGen); + Utilities.IO.Compression.ZLib.CompressOutput(octetStream, -1), sGen, cGen, eiGen); } private class CmsCompressedOutputStream : BaseOutputStream { - private ZOutputStream _out; + private Stream _out; private BerSequenceGenerator _sGen; private BerSequenceGenerator _cGen; private BerSequenceGenerator _eiGen; internal CmsCompressedOutputStream( - ZOutputStream outStream, + Stream outStream, BerSequenceGenerator sGen, BerSequenceGenerator cGen, BerSequenceGenerator eiGen) diff --git a/crypto/src/openpgp/PgpCompressedData.cs b/crypto/src/openpgp/PgpCompressedData.cs index fc7d200d0..346b0b1a1 100644 --- a/crypto/src/openpgp/PgpCompressedData.cs +++ b/crypto/src/openpgp/PgpCompressedData.cs @@ -1,7 +1,6 @@ using System.IO; -using Org.BouncyCastle.Bzip2; -using Org.BouncyCastle.Utilities.Zlib; +using Org.BouncyCastle.Utilities.IO.Compression; namespace Org.BouncyCastle.Bcpg.OpenPgp { @@ -38,16 +37,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { switch (Algorithm) { - case CompressionAlgorithmTag.Uncompressed: - return GetInputStream(); - case CompressionAlgorithmTag.Zip: - return new ZInputStream(GetInputStream(), true); - case CompressionAlgorithmTag.ZLib: - return new ZInputStream(GetInputStream()); - case CompressionAlgorithmTag.BZip2: - return new CBZip2InputStream(GetInputStream()); - default: - throw new PgpException("can't recognise compression algorithm: " + Algorithm); + case CompressionAlgorithmTag.Uncompressed: + return GetInputStream(); + case CompressionAlgorithmTag.Zip: + return Zip.DecompressInput(GetInputStream()); + case CompressionAlgorithmTag.ZLib: + return ZLib.DecompressInput(GetInputStream()); + case CompressionAlgorithmTag.BZip2: + return Bzip2.DecompressInput(GetInputStream()); + default: + throw new PgpException("can't recognise compression algorithm: " + Algorithm); } } } diff --git a/crypto/src/openpgp/PgpCompressedDataGenerator.cs b/crypto/src/openpgp/PgpCompressedDataGenerator.cs index 791aac0bf..d13d7402b 100644 --- a/crypto/src/openpgp/PgpCompressedDataGenerator.cs +++ b/crypto/src/openpgp/PgpCompressedDataGenerator.cs @@ -1,8 +1,8 @@ using System; using System.IO; -using Org.BouncyCastle.Bzip2; using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.IO.Compression; using Org.BouncyCastle.Utilities.Zlib; namespace Org.BouncyCastle.Bcpg.OpenPgp @@ -131,21 +131,21 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp switch (algorithm) { - case CompressionAlgorithmTag.Uncompressed: - dOut = pkOut; - break; - case CompressionAlgorithmTag.Zip: - dOut = new SafeZOutputStream(pkOut, compression, true); - break; - case CompressionAlgorithmTag.ZLib: - dOut = new SafeZOutputStream(pkOut, compression, false); - break; - case CompressionAlgorithmTag.BZip2: - dOut = new SafeCBZip2OutputStream(pkOut); - break; - default: - // Constructor should guard against this possibility - throw new InvalidOperationException(); + case CompressionAlgorithmTag.Uncompressed: + dOut = pkOut; + break; + case CompressionAlgorithmTag.Zip: + dOut = Zip.CompressOutput(pkOut, compression, true); + break; + case CompressionAlgorithmTag.ZLib: + dOut = ZLib.CompressOutput(pkOut, compression, true); + break; + case CompressionAlgorithmTag.BZip2: + dOut = Bzip2.CompressOutput(pkOut, true); + break; + default: + // Constructor should guard against this possibility + throw new InvalidOperationException(); } } @@ -165,31 +165,5 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp pkOut = null; } } - - private class SafeCBZip2OutputStream : CBZip2OutputStream - { - public SafeCBZip2OutputStream(Stream output) - : base(output) - { - } - - protected override void Dispose(bool disposing) - { - Detach(disposing); - } - } - - private class SafeZOutputStream : ZOutputStream - { - public SafeZOutputStream(Stream output, int level, bool nowrap) - : base(output, level, nowrap) - { - } - - protected override void Dispose(bool disposing) - { - Detach(disposing); - } - } } } 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); + } + } } |