summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-07-15 23:36:16 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-07-15 23:36:16 +0700
commit7edbca353a0ba900c7f942797d72ca6b115d103a (patch)
treec23673424f03f1fe479c865ee7db4fd58b561348
parentAvoid redundant CheckUsageInRole calls (diff)
downloadBouncyCastle.NET-ed25519-7edbca353a0ba900c7f942797d72ca6b115d103a.tar.xz
Fix bzip2 compression for empty contents
- see https://github.com/bcgit/bc-java/issues/993
-rw-r--r--crypto/bzip2/src/CBZip2OutputStream.cs5
-rw-r--r--crypto/test/src/openpgp/test/PGPCompressionTest.cs69
2 files changed, 39 insertions, 35 deletions
diff --git a/crypto/bzip2/src/CBZip2OutputStream.cs b/crypto/bzip2/src/CBZip2OutputStream.cs
index ffac073fb..6e9d86bba 100644
--- a/crypto/bzip2/src/CBZip2OutputStream.cs
+++ b/crypto/bzip2/src/CBZip2OutputStream.cs
@@ -423,7 +423,10 @@ namespace Org.BouncyCastle.Apache.Bzip2
                 WriteRun();
             }
             currentChar = -1;
-            EndBlock();
+            if (last >= 0)
+            {
+                EndBlock();
+            }
             EndCompression();
             finished = true;
             Flush();
diff --git a/crypto/test/src/openpgp/test/PGPCompressionTest.cs b/crypto/test/src/openpgp/test/PGPCompressionTest.cs
index c8bca7470..f42ccfb88 100644
--- a/crypto/test/src/openpgp/test/PGPCompressionTest.cs
+++ b/crypto/test/src/openpgp/test/PGPCompressionTest.cs
@@ -13,55 +13,57 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 	public class PgpCompressionTest
 		: SimpleTest
 	{
-		private static readonly byte[] Data = Encoding.ASCII.GetBytes("hello world! !dlrow olleh");
+        private static readonly byte[] Data1 = new byte[0];
+        private static readonly byte[] Data2 = Encoding.ASCII.GetBytes("hello world! !dlrow olleh");
 
-		[Test]
+        [Test]
+        public void TestBZip2()
+        {
+            DoTestCompression(Data1, CompressionAlgorithmTag.BZip2);
+            DoTestCompression(Data2, CompressionAlgorithmTag.BZip2);
+        }
+
+        [Test]
 		public void TestUncompressed()
 		{
-			doTestCompression(CompressionAlgorithmTag.Uncompressed);
-		}
+			DoTestCompression(Data1, CompressionAlgorithmTag.Uncompressed);
+            DoTestCompression(Data2, CompressionAlgorithmTag.Uncompressed);
+        }
 
-		[Test]
+        [Test]
 		public void TestZip()
 		{
-			doTestCompression(CompressionAlgorithmTag.Zip);
-		}
+			DoTestCompression(Data1, CompressionAlgorithmTag.Zip);
+            DoTestCompression(Data2, CompressionAlgorithmTag.Zip);
+        }
 
-		[Test]
+        [Test]
 		public void TestZLib()
 		{
-			doTestCompression(CompressionAlgorithmTag.ZLib);
-		}
-
-		[Test]
-		public void TestBZip2()
-		{
-			doTestCompression(CompressionAlgorithmTag.BZip2);
-		}
+			DoTestCompression(Data1, CompressionAlgorithmTag.ZLib);
+            DoTestCompression(Data2, CompressionAlgorithmTag.ZLib);
+        }
 
-		public override void PerformTest()
+        public override void PerformTest()
 		{
-			doTestCompression(CompressionAlgorithmTag.Uncompressed);
-			doTestCompression(CompressionAlgorithmTag.Zip);
-			doTestCompression(CompressionAlgorithmTag.ZLib);
-			doTestCompression(CompressionAlgorithmTag.BZip2);
+            TestBZip2();
+            TestUncompressed();
+            TestZip();
+            TestZLib();
 		}
 
-		private void doTestCompression(
-			CompressionAlgorithmTag type)
+		private void DoTestCompression(byte[] data, CompressionAlgorithmTag type)
 		{
-			doTestCompression(type, true);
-			doTestCompression(type, false);
+			DoTestCompression(data, type, true);
+			DoTestCompression(data, type, false);
 		}
 
-		private void doTestCompression(
-			CompressionAlgorithmTag	type,
-			bool					streamClose)
+		private void DoTestCompression(byte[] data, CompressionAlgorithmTag	type, bool streamClose)
 		{
 			MemoryStream bOut = new MemoryStream();
 			PgpCompressedDataGenerator cPacket = new PgpCompressedDataGenerator(type);
-			Stream os = cPacket.Open(new UncloseableStream(bOut), new byte[Data.Length - 1]);
-			os.Write(Data, 0, Data.Length);
+            Stream os = cPacket.Open(new UncloseableStream(bOut));
+			os.Write(data, 0, data.Length);
 
 			if (streamClose)
 			{
@@ -72,7 +74,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 				cPacket.Close();
 			}
 
-			ValidateData(bOut.ToArray());
+			ValidateData(data, bOut.ToArray());
 
 			try
 			{
@@ -85,8 +87,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 			}
 		}
 
-		private void ValidateData(
-			byte[] compressed)
+		private void ValidateData(byte[] data, byte[] compressed)
 		{
 			PgpObjectFactory pgpFact = new PgpObjectFactory(compressed);
 			PgpCompressedData c1 = (PgpCompressedData) pgpFact.NextPgpObject();
@@ -95,7 +96,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
 			byte[] bytes = Streams.ReadAll(pIn);
 			pIn.Close();
 
-			if (!AreEqual(bytes, Data))
+			if (!AreEqual(bytes, data))
 			{
 				Fail("compression test failed");
 			}