diff --git a/SabreTools.IO.Test/Compression/MSZIPTests.cs b/SabreTools.IO.Test/Compression/MSZIPTests.cs new file mode 100644 index 0000000..9357744 --- /dev/null +++ b/SabreTools.IO.Test/Compression/MSZIPTests.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Text; +using SabreTools.IO.Compression.MSZIP; +using SabreTools.IO.Extensions; +using Xunit; + +namespace SabreTools.IO.Test.Compression +{ + public class MSZIPTests + { + [Fact] + public void DecompressorTest() + { + // Testing Note: This is a fake file that has multiple blocks + // sequentially. In real cabinet files, these are embedded in + // CFDATA blocks. + string path = Path.Combine(Environment.CurrentDirectory, "TestData", "test-archive.msz"); + byte[] inputBytes = File.ReadAllBytes(path); + MemoryStream input = new MemoryStream(inputBytes); + MemoryStream output = new MemoryStream(); + + var decompressor = Decompressor.Create(); + input.SeekIfPossible(0x0000); + decompressor.CopyTo(input, output); + input.SeekIfPossible(0x3969); + decompressor.CopyTo(input, output); + + Assert.Equal(38470, output.Length); + } + } +} \ No newline at end of file diff --git a/SabreTools.IO.Test/TestData/test-archive.msz b/SabreTools.IO.Test/TestData/test-archive.msz new file mode 100644 index 0000000..d8b0984 Binary files /dev/null and b/SabreTools.IO.Test/TestData/test-archive.msz differ diff --git a/SabreTools.IO/Compression/MSZIP/Decompressor.cs b/SabreTools.IO/Compression/MSZIP/Decompressor.cs index c2cb54c..3fd7a7c 100644 --- a/SabreTools.IO/Compression/MSZIP/Decompressor.cs +++ b/SabreTools.IO/Compression/MSZIP/Decompressor.cs @@ -42,6 +42,10 @@ namespace SabreTools.IO.Compression.MSZIP if (!dest.CanWrite) return false; + // Ignore if the end of the stream is reached + if (source.Position >= source.Length) + return false; + // Validate the header var header = new BlockHeader(); header.Signature = source.ReadUInt16LittleEndian(); @@ -49,7 +53,7 @@ namespace SabreTools.IO.Compression.MSZIP throw new InvalidDataException(nameof(source)); byte[] buffer = new byte[32 * 1024]; - var blockStream = new Deflate.DeflateStream(source, Deflate.CompressionMode.Decompress); + var blockStream = new Deflate.DeflateStream(source, Deflate.CompressionMode.Decompress, leaveOpen: true); if (_history != null) blockStream.SetDictionary(_history, check: false);