From 5385de0f0a06c59ceafab3b160e12d17dffb83c1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 7 Jul 2022 12:02:25 -0700 Subject: [PATCH] Add MS-ZIP deserialize and deflate --- BurnOutSharp/FileType/MicrosoftCAB.cs | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/BurnOutSharp/FileType/MicrosoftCAB.cs b/BurnOutSharp/FileType/MicrosoftCAB.cs index b1a949a1..5fa8f504 100644 --- a/BurnOutSharp/FileType/MicrosoftCAB.cs +++ b/BurnOutSharp/FileType/MicrosoftCAB.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.IO; +using System.IO.Compression; using System.Text; using BurnOutSharp.Interfaces; using BurnOutSharp.Tools; @@ -1094,6 +1096,61 @@ namespace BurnOutSharp.FileType public byte[] Data { get; private set; } #endregion + + #region Serialization + + public static MSZIPBlock Deserialize(byte[] data, ref int dataPtr, int blockSize) + { + if (data == null || dataPtr < 0 || blockSize <= 0) + return null; + + MSZIPBlock block = new MSZIPBlock(); + + block.Signature = BitConverter.ToUInt16(data, dataPtr); dataPtr += 2; + if (block.Signature != SignatureValue) + return null; + + block.Data = new byte[blockSize]; + Array.Copy(data, dataPtr, block.Data, 0, blockSize); + dataPtr += blockSize; + + return block; + } + + #endregion + + #region Public Functionality + + /// + /// Decompress a single block of MS-ZIP data + /// + public byte[] DecompressBlock() + { + if (Data == null || Data.Length == 0) + return null; + + try + { + // Create the input objects + MemoryStream blockStream = new MemoryStream(Data); + DeflateStream deflateStream = new DeflateStream(blockStream, CompressionMode.Decompress); + + // Create the output object + MemoryStream outputStream = new MemoryStream(); + + // Inflate the data + deflateStream.CopyTo(outputStream); + + // Return the inflated data + return outputStream.ToArray(); + } + catch + { + return null; + } + } + + #endregion } #endregion