Split MS-CAB into subfiles, LibMSPackSharp for .NET 6

This commit is contained in:
Matt Nadareski
2022-12-13 21:51:24 -08:00
parent 756a74eda6
commit 645a366dc5
5 changed files with 1600 additions and 1597 deletions

View File

@@ -0,0 +1,11 @@
namespace BurnOutSharp.FileType
{
#region TEMPORARY AREA FOR LZX COMPRESSION FORMAT
// See the following for details about implementation (there is no open spec):
// https://github.com/kyz/libmspack/blob/master/libmspack/mspack/lzx.h
// https://github.com/kyz/libmspack/blob/master/libmspack/mspack/lzxc.c
// https://github.com/kyz/libmspack/blob/master/libmspack/mspack/lzxd.c
#endregion
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,143 @@
using System;
using System.Linq;
using ComponentAce.Compression.Libs.zlib;
namespace BurnOutSharp.FileType
{
/// <summary>
/// Each MSZIP block MUST consist of a 2-byte MSZIP signature and one or more RFC 1951 blocks. The
/// 2-byte MSZIP signature MUST consist of the bytes 0x43 and 0x4B. The MSZIP signature MUST be
/// the first 2 bytes in the MSZIP block.The MSZIP signature is shown in the following packet diagram.
/// </summary>
internal class MSZIPBlock
{
#region Constants
/// <summary>
/// Human-readable signature
/// </summary>
public static readonly string SignatureString = "CK";
/// <summary>
/// Signature as an unsigned Int16 value
/// </summary>
public const ushort SignatureValue = 0x4B43;
/// <summary>
/// Signature as a byte array
/// </summary>
public static readonly byte[] SignatureBytes = new byte[] { 0x43, 0x4B };
#endregion
#region Properties
/// <summary>
/// 'CB'
/// </summary>
public ushort Signature { get; private set; }
/// <summary>
/// Each MSZIP block is the result of a single deflate compression operation, as defined in [RFC1951].
/// The compressor that performs the compression operation MUST generate one or more RFC 1951
/// blocks, as defined in [RFC1951]. The number, deflation mode, and type of RFC 1951 blocks in each
/// MSZIP block is determined by the compressor, as defined in [RFC1951]. The last RFC 1951 block in
/// each MSZIP block MUST be marked as the "end" of the stream(1), as defined by[RFC1951]
/// section 3.2.3. Decoding trees MUST be discarded after each RFC 1951 block, but the history buffer
/// MUST be maintained.Each MSZIP block MUST represent no more than 32 KB of uncompressed data.
///
/// The maximum compressed size of each MSZIP block is 32 KB + 12 bytes.This enables the MSZIP
/// block to contain 32 KB of data split between two noncompressed RFC 1951 blocks, each of which
/// has a value of BTYPE = 00.
/// </summary>
public byte[] Data { get; private set; }
#endregion
#region Static Properties
public static ZStream DecompressionStream { get; set; } = new ZStream();
#endregion
#region Serialization
public static MSZIPBlock Deserialize(byte[] data)
{
if (data == null)
return null;
MSZIPBlock block = new MSZIPBlock();
int dataPtr = 0;
block.Signature = BitConverter.ToUInt16(data, dataPtr); dataPtr += 2;
if (block.Signature != SignatureValue)
return null;
block.Data = new byte[data.Length - 2];
Array.Copy(data, dataPtr, block.Data, 0, data.Length - 2);
dataPtr += data.Length - 2;
return block;
}
#endregion
#region Public Functionality
/// <summary>
/// Decompress a single block of MS-ZIP data
/// </summary>
public byte[] DecompressBlock(int decompressedSize, byte[] previousBytes = null)
{
if (Data == null || Data.Length == 0)
return null;
try
{
// The first block can use DeflateStream since it has no history
if (previousBytes == null)
{
// Setup the input
DecompressionStream = new ZStream();
int initErr = DecompressionStream.inflateInit();
if (initErr != zlibConst.Z_OK)
return null;
}
// All n+1 blocks require the previous uncompressed data as a dictionary
else
{
// TODO: We need to force a dictionary setting - at this point, mode is 8 not 6
// Setup the dictionary
int dictErr = DecompressionStream.inflateSetDictionary(previousBytes, previousBytes.Length);
if (dictErr != zlibConst.Z_OK)
return null;
}
// Setup the output
byte[] output = new byte[decompressedSize];
DecompressionStream.next_out = output;
DecompressionStream.avail_out = decompressedSize;
// Inflate the data -- 0x78, 0x9C is needed to trick zlib
DecompressionStream.next_in = new byte[] { 0x78, 0x9C }.Concat(Data).ToArray();
DecompressionStream.next_in_index = 0;
DecompressionStream.avail_in = Data.Length + 2;
int err = DecompressionStream.inflate(zlibConst.Z_FULL_FLUSH);
if (err != zlibConst.Z_OK)
return null;
return output;
}
catch
{
return null;
}
}
#endregion
}
}

View File

@@ -0,0 +1,191 @@
namespace BurnOutSharp.FileType
{
#region TEMPORARY AREA FOR QUANTUM COMPRESSION FORMAT
// See http://www.russotto.net/quantumcomp.html for details about implementation
internal enum SelectorModel
{
/// <summary>
/// Literal model, 64 entries, start at symbol 0
/// </summary>
SELECTOR_0 = 0,
/// <summary>
/// Literal model, 64 entries, start at symbol 64
/// </summary>
SELECTOR_1 = 1,
/// <summary>
/// Literal model, 64 entries, start at symbol 128
/// </summary>
SELECTOR_2 = 2,
/// <summary>
/// Literal model, 64 entries, start at symbol 192
/// </summary>
SELECTOR_3 = 3,
/// <summary>
/// LZ model, 3 character matches, max 24 entries, start at symbol 0
/// </summary>
SELECTOR_4 = 4,
/// <summary>
/// LZ model, 4 character matches, max 36 entries, start at symbol 0
/// </summary>
SELECTOR_5 = 5,
/// <summary>
/// LZ model, 5+ character matches, max 42 entries, start at symbol 0
/// </summary>
SELECTOR_6_POSITION = 6,
/// <summary>
/// LZ model, 5+ character matches, max 27 entries, start at symbol 0
/// </summary>
SELECTOR_6_LENGTH = 7,
}
#region LZ Compression Tables
internal static class QuantumConstants
{
internal static readonly uint[] PositionBaseTable = new uint[]
{
0x00000, 0x00001, 0x00002, 0x00003, 0x00004, 0x00006, 0x00008, 0x0000c,
0x00010, 0x00018, 0x00020, 0x00030, 0x00040, 0x00060, 0x00080, 0x000c0,
0x00100, 0x00180, 0x00200, 0x00300, 0x00400, 0x00600, 0x00800, 0x00c00,
0x01000, 0x01800, 0x02000, 0x03000, 0x04000, 0x06000, 0x08000, 0x0c000,
0x10000, 0x18000, 0x20000, 0x30000, 0x40000, 0x60000, 0x80000, 0xc0000,
0x100000, 0x180000,
};
internal static readonly int[] PositionExtraBitsTable = new int[]
{
0, 0, 0, 0, 1, 1, 2, 2,
3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10,
11, 11, 12, 12, 13, 13, 14, 14,
15, 15, 16, 16, 17, 17, 18, 18,
19, 19,
};
internal static readonly byte[] LengthBaseTable = new byte[]
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08,
0x0a, 0x0c, 0x0e, 0x12, 0x16, 0x1a, 0x1e, 0x26,
0x2e, 0x36, 0x3e, 0x4e, 0x5e, 0x6e, 0x7e, 0x9e,
0xbe, 0xde, 0xfe
};
internal static readonly int[] LengthExtraBitsTable = new int[]
{
0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 2, 2, 2, 2, 3, 3,
3, 3, 4, 4, 4, 4, 5, 5,
5, 5, 0,
};
/// <summary>
/// Number of position slots for (tsize - 10)
/// </summary>
internal static readonly int[] NumberOfPositionSlots = new int[]
{
20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
};
}
#endregion
internal static class QuantumCompressor
{
// TODO: Determine how these values are set
private static uint CS_C = 0;
private static uint CS_H = 0;
private static uint CS_L = 0;
/// <summary>
/// Get frequency from code
/// </summary>
public static ushort GetFrequency(ushort totfreq)
{
uint range = ((CS_H - CS_L) & 0xFFFF) + 1;
uint freq = ((CS_C - CS_L + 1) * totfreq - 1) / range;
return (ushort)(freq & 0xFFFF);
}
/// <summary>
/// The decoder renormalization loop
/// </summary>
public static int GetCode(int cumfreqm1, int cumfreq, int totfreq)
{
uint range = (CS_H - CS_L) + 1;
CS_H = CS_L + (uint)((cumfreqm1 * range) / totfreq) - 1;
CS_L = CS_L + (uint)((cumfreq * range) / totfreq);
while (true)
{
if ((CS_L & 0x8000) != (CS_H & 0x8000))
{
if ((CS_L & 0x4000) != 0 && (CS_H & 0x4000) == 0)
{
// Underflow case
CS_C ^= 0x4000;
CS_L &= 0x3FFF;
CS_H |= 0x4000;
}
else
{
break;
}
}
CS_L <<= 1;
CS_H = (CS_H << 1) | 1;
CS_C = (CS_C << 1) | 0; // TODO: Figure out what `getbit()` is and replace the placeholder `0`
}
// TODO: Figure out what is supposed to return here
return 0;
}
public static int GetSymbol(Model model)
{
int freq = GetFrequency(model.Symbols[0].CumulativeFrequency);
int i = 1;
for (; i < model.Entries; i++)
{
if (model.Symbols[i].CumulativeFrequency <= freq)
break;
}
int sym = model.Symbols[i - 1].Symbol;
GetCode(model.Symbols[i - 1].CumulativeFrequency, model.Symbols[i].CumulativeFrequency, model.Symbols[0].CumulativeFrequency);
// TODO: Figure out what `update_model` does
//update_model(model, i);
return sym;
}
}
internal class ModelSymbol
{
public ushort Symbol { get; private set; }
public ushort CumulativeFrequency { get; private set; }
}
internal class Model
{
public int Entries { get; private set; }
public ModelSymbol[] Symbols { get; private set; }
}
#endregion
}

File diff suppressed because it is too large Load Diff