Start of de-macro-ification

This commit is contained in:
Matt Nadareski
2022-05-19 23:52:08 -07:00
parent 3ea294f4d9
commit 08c97d291e
11 changed files with 562 additions and 163 deletions

View File

@@ -54,7 +54,7 @@ namespace LibMSPackSharp.Compression
public uint BitBuffer { get; set; }
public uint BitsLeft { get; set; }
public int BitsLeft { get; set; }
/// <summary>
/// Have we reached the end of input?
@@ -141,7 +141,7 @@ namespace LibMSPackSharp.Compression
InputEnd = 0;
}
public void STORE_BITS(int inputPointer, int inputLength, uint bit_buffer, uint bits_left)
public void STORE_BITS(int inputPointer, int inputLength, uint bit_buffer, int bits_left)
{
InputPointer = inputPointer;
InputLength = inputLength;
@@ -149,7 +149,7 @@ namespace LibMSPackSharp.Compression
BitsLeft = bits_left;
}
public void RESTORE_BITS(ref int inputPointer, ref int inputLength, ref uint bit_buffer, ref uint bits_left)
public void RESTORE_BITS(ref int inputPointer, ref int inputLength, ref uint bit_buffer, ref int bits_left)
{
inputPointer = InputPointer;
inputLength = InputLength;
@@ -157,7 +157,7 @@ namespace LibMSPackSharp.Compression
bits_left = BitsLeft;
}
public void ENSURE_BITS(int nbits, ref int i_ptr, ref int i_end, ref uint bits_left, ref uint bit_buffer, bool msb)
public void ENSURE_BITS(int nbits, ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
while (bits_left < nbits)
{
@@ -169,7 +169,7 @@ namespace LibMSPackSharp.Compression
Error = Error.MSPACK_ERR_OK;
}
public void READ_BITS(ref int val, int nbits, ref int i_ptr, ref int i_end, ref uint bits_left, ref uint bit_buffer, bool msb)
public void READ_BITS(ref int val, int nbits, ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
ENSURE_BITS(nbits, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb);
if (Error != Error.MSPACK_ERR_OK)
@@ -180,7 +180,7 @@ namespace LibMSPackSharp.Compression
Error = Error.MSPACK_ERR_OK;
}
public void READ_MANY_BITS(ref uint val, byte bits, ref int i_ptr, ref int i_end, ref uint bits_left, ref uint bit_buffer, bool msb)
public void READ_MANY_BITS(ref uint val, byte bits, ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
byte needed = bits, bitrun;
val = 0;
@@ -210,35 +210,35 @@ namespace LibMSPackSharp.Compression
return (int)(bit_buffer & ((1 << (nbits)) - 1));
}
public void REMOVE_BITS(int nbits, ref uint bits_left, ref uint bit_buffer, bool msb)
public void REMOVE_BITS(int nbits, ref int bits_left, ref uint bit_buffer, bool msb)
{
if (msb)
{
bit_buffer <<= nbits;
bits_left -= (uint)nbits;
bits_left -= nbits;
}
else
{
bit_buffer >>= nbits;
bits_left -= (uint)nbits;
bits_left -= nbits;
}
}
public void INJECT_BITS(uint bitdata, int nbits, ref uint bits_left, ref uint bit_buffer, bool msb)
public void INJECT_BITS(uint bitdata, int nbits, ref int bits_left, ref uint bit_buffer, bool msb)
{
if (msb)
{
bit_buffer |= bitdata << (int)bits_left;
bits_left += (uint)nbits;
bits_left += nbits;
}
else
{
bit_buffer |= bitdata << (int)bits_left;
bits_left += (uint)nbits;
bits_left += nbits;
}
}
public abstract void READ_BYTES(ref int i_ptr, ref int i_end, ref uint bits_left, ref uint bit_buffer, bool msb);
public abstract void READ_BYTES(ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb);
// lsb_bit_mask[n] = (1 << n) - 1 */
private static readonly ushort[] lsb_bit_mask = new ushort[17]
@@ -252,7 +252,7 @@ namespace LibMSPackSharp.Compression
return (int)(BitBuffer & lsb_bit_mask[nbits]);
}
public void READ_BITS_T(ref int val, int nbits, ref int i_ptr, ref int i_end, ref uint bits_left, ref uint bit_buffer, bool msb)
public void READ_BITS_T(ref int val, int nbits, ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
ENSURE_BITS(nbits, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb);
if (Error != Error.MSPACK_ERR_OK)
@@ -278,14 +278,11 @@ namespace LibMSPackSharp.Compression
Error = Error.MSPACK_ERR_OK;
}
private void ReadInput()
public Error ReadInput()
{
int read = Sys.Read(Input, InputBuffer, 0, (int)InputBufferSize);
if (read < 0)
{
Error = Error.MSPACK_ERR_READ;
return;
}
return Error = Error.MSPACK_ERR_READ;
// We might overrun the input stream by asking for bits we don't use,
// so fake 2 more bytes at the end of input
@@ -294,8 +291,7 @@ namespace LibMSPackSharp.Compression
if (InputEnd != 0)
{
Console.WriteLine("out of input bytes");
Error = Error.MSPACK_ERR_READ;
return;
return Error = Error.MSPACK_ERR_READ;
}
else
{
@@ -308,7 +304,7 @@ namespace LibMSPackSharp.Compression
// Update i_ptr and i_end
InputPointer = 0;
InputLength = read;
Error = Error.MSPACK_ERR_OK;
return Error = Error.MSPACK_ERR_OK;
}
#endregion
@@ -321,7 +317,7 @@ namespace LibMSPackSharp.Compression
/// Decodes the next huffman symbol from the input bitstream into var.
/// Do not use this macro on a table unless build_decode_table() succeeded.
/// </summary>
public int READ_HUFFSYM(ushort[] decodingTable, ref uint var, int tablebits, byte[] lengthTable, int maxsymbols, ref int i, ref int i_ptr, ref int i_end, ref uint bits_left, ref uint bit_buffer, bool msb)
public int READ_HUFFSYM(ushort[] decodingTable, ref uint var, int tablebits, byte[] lengthTable, int maxsymbols, ref int i, ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
ENSURE_BITS(HUFF_MAXBITS, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb);
if (Error != Error.MSPACK_ERR_OK)

View File

@@ -0,0 +1,21 @@
/* This file is part of libmspack.
* (C) 2003-2010 Stuart Caie.
*
* libmspack is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License (LGPL) version 2.1
*
* For further details, see the file COPYING.LIB distributed with libmspack
*/
namespace LibMSPackSharp.Compression
{
internal static class Constants
{
/* lsb_bit_mask[n] = (1 << n) - 1 */
internal static readonly ushort[] lsb_bit_mask = new ushort[17]
{
0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
};
}
}

View File

@@ -432,7 +432,8 @@ namespace LibMSPackSharp.Compression
return Error.MSPACK_ERR_ARGS;
// Bitstream and huffman reading variables
uint bit_buffer = 0, bits_left = 0;
uint bit_buffer = 0;
int bits_left = 0;
int i_ptr = 0, i_end = 0;
int match_length, extra, verbatim_bits = 0, bytes_todo;
@@ -1116,7 +1117,8 @@ namespace LibMSPackSharp.Compression
private static Error ReadLens(LZXDStream lzx, byte[] lens, uint first, uint last)
{
// Bit buffer and huffman symbol decode variables
uint bit_buffer = 0, bits_left = 0;
uint bit_buffer = 0;
int bits_left = 0;
int i = 0;
int i_ptr = 0, i_end = 0;
@@ -1128,7 +1130,7 @@ namespace LibMSPackSharp.Compression
// Read lengths for pretree (20 symbols, lengths stored in fixed 4 bits)
for (x = 0; x < 20; x++)
{
lzx.READ_BITS(ref y, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzx.READ_BITS(ref y, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzx.Error != Error.MSPACK_ERR_OK)
return lzx.Error;
@@ -1150,7 +1152,7 @@ namespace LibMSPackSharp.Compression
if (z == 17)
{
// Code = 17, run of ([read 4 bits]+4) zeros
lzx.READ_BITS(ref y, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzx.READ_BITS(ref y, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzx.Error != Error.MSPACK_ERR_OK)
return lzx.Error;
@@ -1163,7 +1165,7 @@ namespace LibMSPackSharp.Compression
else if (z == 18)
{
// Code = 18, run of ([read 5 bits]+20) zeros
lzx.READ_BITS(ref y, 5, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzx.READ_BITS(ref y, 5, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzx.Error != Error.MSPACK_ERR_OK)
return lzx.Error;
@@ -1176,7 +1178,7 @@ namespace LibMSPackSharp.Compression
else if (z == 19)
{
// Code = 19, run of ([read 1 bit]+4) [read huffman symbol]
lzx.READ_BITS(ref y, 1, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzx.READ_BITS(ref y, 1, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzx.Error != Error.MSPACK_ERR_OK)
return lzx.Error;

View File

@@ -137,7 +137,7 @@ namespace LibMSPackSharp.Compression
// This is used purely for doing the intel E8 transform
public byte[] e8_buf { get; set; } = new byte[LZX.LZX_FRAME_SIZE];
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref uint bitsLeft, ref uint bitBuffer, bool msb)
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
READ_IF_NEEDED(ref i_ptr, ref i_end);
if (Error != Error.MSPACK_ERR_OK)
@@ -150,7 +150,7 @@ namespace LibMSPackSharp.Compression
return;
byte b1 = InputBuffer[i_ptr++];
INJECT_BITS((uint)((b1 << 8) | b0), 16, ref bitsLeft, ref bitBuffer, msb);
INJECT_BITS((uint)((b1 << 8) | b0), 16, ref bits_left, ref bit_buffer, msb);
Error = Error.MSPACK_ERR_OK;
}

View File

@@ -12,6 +12,7 @@
using System;
using System.IO;
using static LibMSPackSharp.Compression.Constants;
namespace LibMSPackSharp.Compression
{
@@ -153,7 +154,8 @@ namespace LibMSPackSharp.Compression
return Error.MSPACK_ERR_ARGS;
// For the bit buffer
uint bit_buffer = 0, bits_left = 0;
uint bit_buffer = 0;
int bits_left = 0;
int i_ptr = 0, i_end = 0;
int i, state, error;
@@ -185,18 +187,42 @@ namespace LibMSPackSharp.Compression
while (out_bytes > 0)
{
// Unpack another block
zip.RESTORE_BITS(ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
//RESTORE_BITS;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
bit_buffer = zip.BitBuffer;
bits_left = zip.BitsLeft;
// Skip to next read 'CK' header
i = (int)(bits_left & 7);
zip.REMOVE_BITS(i, ref bits_left, ref bit_buffer, msb: false); // Align to bytestream
bit_buffer >>= (i); bits_left -= (i); //REMOVE_BITS(i);
state = 0;
do
{
zip.READ_BITS(ref i, 8, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return zip.Error;
//READ_BITS(i, 8);
//ENSURE_BITS(8);
while (bits_left < 8)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8);
bit_buffer >>= (8); bits_left -= (8); //REMOVE_BITS(8);
if (i == 'C')
state = 1;
@@ -209,7 +235,12 @@ namespace LibMSPackSharp.Compression
// Inflate a block, repair and realign if necessary
zip.WindowPosition = 0;
zip.BytesOutput = 0;
zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left);
//STORE_BITS;
zip.InputPointer = i_ptr;
zip.InputLength = i_end;
zip.BitBuffer = bit_buffer;
zip.BitsLeft = bits_left;
if ((error = (int)Inflate(zip)) != 0)
{
@@ -273,7 +304,8 @@ namespace LibMSPackSharp.Compression
public static Error DecompressKWAJ(MSZIPDStream zip)
{
// For the bit buffer
uint bit_buffer = 0, bits_left = 0;
uint bit_buffer = 0;
int bits_left = 0;
int i_ptr = 0, i_end = 0;
int i = 0, error, block_len = 0;
@@ -281,18 +313,61 @@ namespace LibMSPackSharp.Compression
// Unpack blocks until block_len == 0
for (; ; )
{
zip.RESTORE_BITS(ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
//RESTORE_BITS;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
bit_buffer = zip.BitBuffer;
bits_left = zip.BitsLeft;
// Align to bytestream, read block_len
i = (int)(bits_left & 7);
zip.REMOVE_BITS(i, ref bits_left, ref bit_buffer, msb: false);
zip.READ_BITS(ref block_len, 8, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return zip.Error;
bit_buffer >>= (i); bits_left -= (i); //REMOVE_BITS(i);
zip.READ_BITS(ref i, 8, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return zip.Error;
//READ_BITS(block_len, 8);
//ENSURE_BITS(8);
while (bits_left < 8)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
block_len = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8);
bit_buffer >>= (8); bits_left -= (8); //REMOVE_BITS(8);
// READ_BITS(block_len, 8);
//ENSURE_BITS(8);
while (bits_left < 8)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8);
bit_buffer >>= (8); bits_left -= (8); //REMOVE_BITS(8);
block_len |= i << 8;
@@ -300,22 +375,69 @@ namespace LibMSPackSharp.Compression
break;
// Read "CK" header
zip.READ_BITS(ref i, 8, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return zip.Error;
// READ_BITS(i, 8);
//ENSURE_BITS(8);
while (bits_left < 8)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8);
bit_buffer >>= (8); bits_left -= (8); //REMOVE_BITS(8);
if (i != 'C')
return Error.MSPACK_ERR_DATAFORMAT;
zip.READ_BITS(ref i, 8, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return zip.Error;
// READ_BITS(i, 8);
//ENSURE_BITS(8);
while (bits_left < 8)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8);
bit_buffer >>= (8); bits_left -= (8); //REMOVE_BITS(8);
if (i != 'K')
return Error.MSPACK_ERR_DATAFORMAT;
// Inflate block
zip.WindowPosition = 0;
zip.BytesOutput = 0;
zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left);
//STORE_BITS;
zip.InputPointer = i_ptr;
zip.InputLength = i_end;
zip.BitBuffer = bit_buffer;
zip.BitsLeft = bits_left;
if ((error = (int)Inflate(zip)) != 0)
{
Console.WriteLine($"inflate error {(InflateErrorCode)error}");
@@ -333,35 +455,99 @@ namespace LibMSPackSharp.Compression
private static InflateErrorCode ReadLens(MSZIPDStream zip)
{
// For the bit buffer and huffman decoding
uint bit_buffer = 0, bits_left = 0;
int i_ptr = 0, i_end = 0;
uint bit_buffer;
int bits_left;
int i_ptr, i_end;
// Bitlen Huffman codes -- immediate lookup, 7 bit max code length
// bitlen Huffman codes -- immediate lookup, 7 bit max code length
ushort[] bl_table = new ushort[1 << 7];
byte[] bl_len = new byte[19];
byte[] lens = new byte[MSZIP_LITERAL_MAXSYMBOLS + MSZIP_DISTANCE_MAXSYMBOLS];
int lit_codes = 0, dist_codes = 0, code, last_code = 0, bitlen_codes = 0, i, run = 0;
uint lit_codes, dist_codes, code, last_code = 0, bitlen_codes, i, run;
zip.RESTORE_BITS(ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
//RESTORE_BITS;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
bit_buffer = zip.BitBuffer;
bits_left = zip.BitsLeft;
// Read the number of codes
zip.READ_BITS(ref lit_codes, 5, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
// READ_BITS(lit_codes, 5);
//ENSURE_BITS(5);
while (bits_left < 5)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
lit_codes = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5);
bit_buffer >>= (5); bits_left -= (5); //REMOVE_BITS(5);
lit_codes += 257;
zip.READ_BITS(ref dist_codes, 5, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
// READ_BITS(dist_codes, 5);
//ENSURE_BITS(5);
while (bits_left < 5)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
dist_codes = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5);
bit_buffer >>= (5); bits_left -= (5); //REMOVE_BITS(5);
dist_codes += 1;
zip.READ_BITS(ref bitlen_codes, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
// READ_BITS(bitlen_codes, 4);
//ENSURE_BITS(4);
while (bits_left < 4)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
bitlen_codes = (bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(4);
bit_buffer >>= (4); bits_left -= (4); //REMOVE_BITS(4);
bitlen_codes += 4;
if (lit_codes > MSZIP_LITERAL_MAXSYMBOLS)
return InflateErrorCode.INF_ERR_SYMLENS;
if (dist_codes > MSZIP_DISTANCE_MAXSYMBOLS)
@@ -370,12 +556,28 @@ namespace LibMSPackSharp.Compression
// Read in the bit lengths in their unusual order
for (i = 0; i < bitlen_codes; i++)
{
int blLenTemp = bl_len[bitlen_order[i]];
zip.READ_BITS(ref blLenTemp, 3, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
// READ_BITS(bl_len[bitlen_order[i]], 3);
bl_len[bitlen_order[i]] = (byte)blLenTemp;
//ENSURE_BITS(3);
while (bits_left < 3)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
bl_len[bitlen_order[i]] = (byte)(bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3);
bit_buffer >>= (3); bits_left -= (3); //REMOVE_BITS(3);
}
while (i < 19)
@@ -384,19 +586,34 @@ namespace LibMSPackSharp.Compression
}
// Create decoding table with an immediate lookup
if (!MSZIPDStream.MakeDecodeTable(19, 7, bl_len, bl_table, msb: false))
if (MSZIPDStream.MakeDecodeTable(19, 7, bl_len, bl_table, msb: false))
return InflateErrorCode.INF_ERR_BITLENTBL;
// Read literal / distance code lengths
for (i = 0; i < (lit_codes + dist_codes); i++)
{
// Single-level huffman lookup
zip.ENSURE_BITS(7, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITBUF;
code = bl_table[zip.PEEK_BITS(7, bit_buffer, msb: false)];
zip.REMOVE_BITS(bl_len[code], ref bits_left, ref bit_buffer, msb: false);
//ENSURE_BITS(7);
while (bits_left < 7)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
code = bl_table[(bit_buffer & ((1 << (7)) - 1))]; //PEEK_BITS(7);
bit_buffer >>= (bl_len[code]); bits_left -= (bl_len[code]); //REMOVE_BITS(bl_len[code]);
if (code < 16)
{
@@ -407,34 +624,91 @@ namespace LibMSPackSharp.Compression
switch (code)
{
case 16:
zip.READ_BITS(ref run, 2, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
//READ_BITS(run, 2);
//ENSURE_BITS(2);
while (bits_left < 2)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
run = (bit_buffer & ((1 << (2)) - 1)); //PEEK_BITS(2);
bit_buffer >>= (2); bits_left -= (2); //REMOVE_BITS(2);
run += 3;
code = last_code;
break;
case 17:
zip.READ_BITS(ref run, 3, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
//READ_BITS(run, 3);
//ENSURE_BITS(3);
while (bits_left < 3)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
run = (bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3);
bit_buffer >>= (3); bits_left -= (3); //REMOVE_BITS(3);
run += 3;
code = 0;
break;
case 18:
zip.READ_BITS(ref run, 7, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
//READ_BITS(run, 7);
//ENSURE_BITS(7);
while (bits_left < 7)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
run = (bit_buffer & ((1 << (7)) - 1)); //PEEK_BITS(7);
bit_buffer >>= (7); bits_left -= (7); //REMOVE_BITS(7);
run += 11;
code = 0;
break;
default:
Console.WriteLine($"bad code!: {code}");
Console.WriteLine($"Bad code!: {code}");
return InflateErrorCode.INF_ERR_BADBITLEN;
}
@@ -465,7 +739,12 @@ namespace LibMSPackSharp.Compression
zip.DISTANCE_len[i++] = 0;
}
zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left);
//STORE_BITS;
zip.InputPointer = i_ptr;
zip.InputLength = i_end;
zip.BitBuffer = bit_buffer;
zip.BitsLeft = bits_left;
return 0;
}
@@ -474,35 +753,80 @@ namespace LibMSPackSharp.Compression
/// </summary>
private static InflateErrorCode Inflate(MSZIPDStream zip)
{
int last_block = 0, block_type = 0, distance = 0, length = 0, this_run, i;
int last_block, block_type, distance = 0, length = 0, this_run, i;
// For the bit buffer and huffman decoding
uint bit_buffer = 0, bits_left = 0;
int i_ptr = 0, i_end = 0;
uint bit_buffer;
int bits_left;
int i_ptr, i_end;
InflateErrorCode err;
zip.RESTORE_BITS(ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
//RESTORE_BITS;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
bit_buffer = zip.BitBuffer;
bits_left = zip.BitsLeft;
do
{
// Read in last block bit
zip.READ_BITS(ref last_block, 1, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
//READ_BITS(last_block, 1);
//ENSURE_BITS(1);
while (bits_left < 1)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
last_block = (int)(bit_buffer & ((1 << (1)) - 1)); //PEEK_BITS(1);
bit_buffer >>= (1); bits_left -= (1); //REMOVE_BITS(1);
// Read in block type
zip.READ_BITS(ref block_type, 2, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITOVERRUN;
//READ_BITS(block_type, 2);
//ENSURE_BITS(2);
while (bits_left < 2)
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
block_type = (int)(bit_buffer & ((1 << (2)) - 1)); //PEEK_BITS(2);
bit_buffer >>= (2); bits_left -= (2); //REMOVE_BITS(2);
// Uncompressed block
if (block_type == 0)
{
// Uncompressed block
byte[] lens_buf = new byte[4];
// Go to byte boundary
i = (int)(bits_left & 7);
zip.REMOVE_BITS(i, ref bits_left, ref bit_buffer, msb: false);
i = bits_left & 7;
bit_buffer >>= (i); bits_left -= (i); //REMOVE_BITS(i);
// Read 4 bytes of data, emptying the bit-buffer if necessary
for (i = 0; (bits_left >= 8); i++)
@@ -510,8 +834,8 @@ namespace LibMSPackSharp.Compression
if (i == 4)
return InflateErrorCode.INF_ERR_BITBUF;
lens_buf[i] = (byte)zip.PEEK_BITS(8, bit_buffer, msb: false);
zip.REMOVE_BITS(8, ref bits_left, ref bit_buffer, msb: false);
lens_buf[i] = (byte)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8);
bit_buffer >>= (8); bits_left -= (8); //REMOVE_BITS(8);
}
if (bits_left != 0)
@@ -519,9 +843,15 @@ namespace LibMSPackSharp.Compression
while (i < 4)
{
zip.READ_IF_NEEDED(ref i_ptr, ref i_end);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITBUF;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
lens_buf[i++] = zip.InputBuffer[i_ptr++];
}
@@ -535,9 +865,15 @@ namespace LibMSPackSharp.Compression
// Read and copy the uncompressed data into the window
while (length > 0)
{
zip.READ_IF_NEEDED(ref i_ptr, ref i_end);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_BITBUF;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
this_run = length;
if (this_run > (uint)(i_end - i_ptr))
@@ -566,9 +902,9 @@ namespace LibMSPackSharp.Compression
// Huffman-compressed LZ77 block
uint match_posn, code = 0;
// Block with fixed Huffman codes
if (block_type == 1)
{
// Block with fixed Huffman codes
i = 0;
while (i < 144)
{
@@ -595,30 +931,33 @@ namespace LibMSPackSharp.Compression
zip.DISTANCE_len[i] = 5;
}
}
// Block with dynamic Huffman codes
else
{
// Block with dynamic Huffman codes
zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left);
//STORE_BITS;
zip.InputPointer = i_ptr;
zip.InputLength = i_end;
zip.BitBuffer = bit_buffer;
zip.BitsLeft = bits_left;
if ((i = (int)ReadLens(zip)) != 0)
return (InflateErrorCode)i;
zip.RESTORE_BITS(ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
//RESTORE_BITS;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
bit_buffer = zip.BitBuffer;
bits_left = zip.BitsLeft;
}
// Now huffman lengths are read for either kind of block,
// create huffman decoding tables
if (!MSZIPDStream.MakeDecodeTable(MSZIP_LITERAL_MAXSYMBOLS, MSZIP_LITERAL_TABLEBITS, zip.LITERAL_len, zip.LITERAL_table, msb: false))
{
// TODO: Figure out why this always gets hit
//return InflateErrorCode.INF_ERR_LITERALTBL;
}
return InflateErrorCode.INF_ERR_LITERALTBL;
if (!MSZIPDStream.MakeDecodeTable(MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_DISTANCE_TABLEBITS, zip.DISTANCE_len, zip.DISTANCE_table, msb: false))
{
// TODO: Figure out why this always gets hit
//return InflateErrorCode.INF_ERR_DISTANCETBL;
}
return InflateErrorCode.INF_ERR_DISTANCETBL;
// Decode forever until end of block code
for (; ; )
@@ -650,9 +989,28 @@ namespace LibMSPackSharp.Compression
if (code >= 29)
return InflateErrorCode.INF_ERR_LITCODE; // Codes 286-287 are illegal
zip.READ_BITS_T(ref length, lit_extrabits[code], ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_LITCODE;
//READ_BITS_T(length, lit_extrabits[code])
//ENSURE_BITS(lit_extrabits[code]);
while (bits_left < lit_extrabits[code])
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
(length) = (int)(bit_buffer & lsb_bit_mask[(lit_extrabits[code])]); //PEEK_BITS_T(lit_extrabits[code]);
bit_buffer >>= (lit_extrabits[code]); bits_left -= (lit_extrabits[code]); //REMOVE_BITS(lit_extrabits[code]);
length += lit_lengths[code];
@@ -662,9 +1020,28 @@ namespace LibMSPackSharp.Compression
if (code >= 30)
return InflateErrorCode.INF_ERR_DISTCODE;
zip.READ_BITS_T(ref distance, dist_extrabits[code], ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: false);
if (zip.Error != Error.MSPACK_ERR_OK)
return InflateErrorCode.INF_ERR_DISTCODE;
//READ_BITS_T(distance, dist_extrabits[code])
//ENSURE_BITS(dist_extrabits[code]);
while (bits_left < dist_extrabits[code])
{
//READ_BYTES;
//READ_IF_NEEDED;
if (i_ptr >= i_end)
{
if (zip.ReadInput() != Error.MSPACK_ERR_OK)
return (InflateErrorCode)zip.Error;
i_ptr = zip.InputPointer;
i_end = zip.InputLength;
}
bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8);
}
(distance) = (int)(bit_buffer & lsb_bit_mask[(dist_extrabits[code])]); //PEEK_BITS_T(dist_extrabits[code]);
bit_buffer >>= (dist_extrabits[code]); bits_left -= (dist_extrabits[code]); //REMOVE_BITS(lit_extrabits[code]);
distance += dist_offsets[code];
@@ -746,7 +1123,11 @@ namespace LibMSPackSharp.Compression
return InflateErrorCode.INF_ERR_FLUSH;
}
zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left);
//STORE_BITS;
zip.InputPointer = i_ptr;
zip.InputLength = i_end;
zip.BitBuffer = bit_buffer;
zip.BitsLeft = bits_left;
// Return success
return InflateErrorCode.INF_ERR_OK;
@@ -763,7 +1144,7 @@ namespace LibMSPackSharp.Compression
zip.BytesOutput += (int)data_flushed;
if (zip.BytesOutput > MSZIP_FRAME_SIZE)
{
Console.WriteLine($"overflow: {data_flushed} bytes flushed, total is now {zip.BytesOutput}");
Console.WriteLine($"Overflow: {data_flushed} bytes flushed, total is now {zip.BytesOutput}");
return Error.MSPACK_ERR_ARGS;
}

View File

@@ -49,13 +49,13 @@ namespace LibMSPackSharp.Compression
#endregion
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref uint bitsLeft, ref uint bitBuffer, bool msb)
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
READ_IF_NEEDED(ref i_ptr, ref i_end);
if (Error != Error.MSPACK_ERR_OK)
return;
INJECT_BITS(InputBuffer[i_ptr++], 8, ref bitsLeft, ref bitBuffer, msb);
INJECT_BITS(InputBuffer[i_ptr++], 8, ref bits_left, ref bit_buffer, msb);
Error = Error.MSPACK_ERR_OK;
}

View File

@@ -181,7 +181,8 @@ namespace LibMSPackSharp.Compression
int i, j, selector = 0, sym = 0, match_length;
ushort symf = 0;
uint bit_buffer = 0, bits_left = 0;
uint bit_buffer = 0;
int bits_left = 0;
// Easy answers
if (qtm == null || (out_bytes < 0))
@@ -226,7 +227,7 @@ namespace LibMSPackSharp.Compression
high = 0xFFFF;
low = 0;
int tempCurrent = current;
qtm.READ_BITS(ref tempCurrent, 16, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
qtm.READ_BITS(ref tempCurrent, 16, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
current = (ushort)tempCurrent;
qtm.HeaderRead = 1;
}
@@ -267,7 +268,7 @@ namespace LibMSPackSharp.Compression
// Selector 4 = fixed length match (3 bytes)
case 4:
GET_SYMBOL(qtm, qtm.Model4, ref sym, ref range, ref symf, ref high, ref low, ref current, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
qtm.READ_MANY_BITS(ref extra, extra_bits[sym], ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
qtm.READ_MANY_BITS(ref extra, extra_bits[sym], ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (qtm.Error != Error.MSPACK_ERR_OK)
return qtm.Error;
@@ -278,7 +279,7 @@ namespace LibMSPackSharp.Compression
// Selector 5 = fixed length match (4 bytes)
case 5:
GET_SYMBOL(qtm, qtm.Model5, ref sym, ref range, ref symf, ref high, ref low, ref current, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
qtm.READ_MANY_BITS(ref extra, extra_bits[sym], ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
qtm.READ_MANY_BITS(ref extra, extra_bits[sym], ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (qtm.Error != Error.MSPACK_ERR_OK)
return qtm.Error;
@@ -289,14 +290,14 @@ namespace LibMSPackSharp.Compression
// Selector 6 = variable length match
case 6:
GET_SYMBOL(qtm, qtm.Model6Len, ref sym, ref range, ref symf, ref high, ref low, ref current, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
qtm.READ_MANY_BITS(ref extra, length_extra[sym], ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
qtm.READ_MANY_BITS(ref extra, length_extra[sym], ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (qtm.Error != Error.MSPACK_ERR_OK)
return qtm.Error;
match_length = (int)(length_base[sym] + extra + 5);
GET_SYMBOL(qtm, qtm.Model6, ref sym, ref range, ref symf, ref high, ref low, ref current, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left);
qtm.READ_MANY_BITS(ref extra, extra_bits[sym], ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
qtm.READ_MANY_BITS(ref extra, extra_bits[sym], ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (qtm.Error != Error.MSPACK_ERR_OK)
return qtm.Error;
@@ -420,14 +421,14 @@ namespace LibMSPackSharp.Compression
{
// Re-align input
if ((bits_left & 7) != 0)
qtm.REMOVE_BITS((int)bits_left & 7, ref bits_left, ref bit_buffer, msb: true);
qtm.REMOVE_BITS(bits_left & 7, ref bits_left, ref bit_buffer, msb: true);
// Special Quantum hack -- cabd.c injects a trailer byte to allow the
// decompressor to realign itself. CAB Quantum blocks, unlike LZX
// blocks, can have anything from 0 to 4 trailing null bytes. */
do
{
qtm.READ_BITS(ref i, 8, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
qtm.READ_BITS(ref i, 8, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
} while (i != 0xFF);
qtm.HeaderRead = 0;
@@ -484,7 +485,7 @@ namespace LibMSPackSharp.Compression
/// If necessary, qtmd_update_model() is called.
/// </summary>
private static void GET_SYMBOL(QTMDStream qtm, QTMDModel model, ref int var, ref uint range, ref ushort symf, ref ushort high, ref ushort low, ref ushort current,
ref int i_ptr, ref int i_end, ref uint bit_buffer, ref uint bits_left)
ref int i_ptr, ref int i_end, ref uint bit_buffer, ref int bits_left)
{
range = (uint)(((high - low) & 0xFFFF) + 1);
symf = (ushort)(((((current - low + 1) * model.Syms[0].CumulativeFrequency) - 1) / range) & 0xFFFF);

View File

@@ -120,7 +120,7 @@ namespace LibMSPackSharp.Compression
public QTMDModelSym[] Model7Symbols { get; set; } = new QTMDModelSym[7 + 1];
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref uint bitsLeft, ref uint bitBuffer, bool msb)
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
READ_IF_NEEDED(ref i_ptr, ref i_end);
if (Error != Error.MSPACK_ERR_OK)
@@ -133,7 +133,7 @@ namespace LibMSPackSharp.Compression
return;
byte b1 = InputBuffer[i_ptr++];
INJECT_BITS((uint)((b0 << 8) | b1), 16, ref bitsLeft, ref bitBuffer, msb);
INJECT_BITS((uint)((b0 << 8) | b1), 16, ref bits_left, ref bit_buffer, msb);
Error = Error.MSPACK_ERR_OK;
}

View File

@@ -417,8 +417,8 @@ namespace LibMSPackSharp.KWAJ
private static Error LZHDecompress(InternalStream lzh)
{
uint bit_buffer = 0, bits_left = 0, len = 0, j = 0;
int i;
uint bit_buffer = 0, len = 0, j = 0;
int i, bits_left = 0;
int i_ptr = 0, i_end = 0;
bool lit_run = false;
int pos = 0, offset;
@@ -436,7 +436,7 @@ namespace LibMSPackSharp.KWAJ
for (i = 0; i < 6; i++)
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref types[i], 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref types[i], 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.Error != Error.MSPACK_ERR_OK)
return lzh.Error;
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
@@ -529,7 +529,7 @@ namespace LibMSPackSharp.KWAJ
//READ_BITS_SAFE(val, n)
int tempj = (int)j;
lzh.READ_BITS(ref tempj, 6, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref tempj, 6, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.Error != Error.MSPACK_ERR_OK)
return lzh.Error;
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
@@ -583,7 +583,8 @@ namespace LibMSPackSharp.KWAJ
public static Error LZHReadLens(InternalStream lzh, uint type, uint numsyms, byte[] lens)
{
uint bit_buffer = 0, bits_left = 0;
uint bit_buffer = 0;
int bits_left = 0;
int i_ptr = 0, i_end = 0;
uint i;
int c = 0, sel = 0;
@@ -603,7 +604,7 @@ namespace LibMSPackSharp.KWAJ
case 1:
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
@@ -611,7 +612,7 @@ namespace LibMSPackSharp.KWAJ
for (i = 1; i < numsyms; i++)
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref sel, 1, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref sel, 1, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
@@ -622,7 +623,7 @@ namespace LibMSPackSharp.KWAJ
else
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref sel, 1, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref sel, 1, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
@@ -633,7 +634,7 @@ namespace LibMSPackSharp.KWAJ
else
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
@@ -645,7 +646,7 @@ namespace LibMSPackSharp.KWAJ
case 2:
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
@@ -653,14 +654,14 @@ namespace LibMSPackSharp.KWAJ
for (i = 1; i < numsyms; i++)
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref sel, 2, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref sel, 2, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
if (sel == 3)
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;
}
@@ -678,7 +679,7 @@ namespace LibMSPackSharp.KWAJ
for (i = 0; i < numsyms; i++)
{
//READ_BITS_SAFE(val, n)
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bit_buffer, ref bits_left, msb: true);
lzh.READ_BITS(ref c, 4, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: true);
if (lzh.InputEnd != 0 && bits_left < lzh.InputEnd)
return Error.MSPACK_ERR_OK;

View File

@@ -33,7 +33,7 @@ namespace LibMSPackSharp.KWAJ
public byte[] Window { get; set; } = new byte[LZSS.LZSS_WINDOW_SIZE];
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref uint bitsLeft, ref uint bitBuffer, bool msb)
public override void READ_BYTES(ref int i_ptr, ref int i_end, ref int bits_left, ref uint bit_buffer, bool msb)
{
if (i_ptr >= i_end)
{
@@ -44,7 +44,7 @@ namespace LibMSPackSharp.KWAJ
i_end = InputLength;
}
INJECT_BITS(InputBuffer[i_ptr++], 8, ref bitsLeft, ref bitBuffer, msb);
INJECT_BITS(InputBuffer[i_ptr++], 8, ref bits_left, ref bit_buffer, msb);
Error = Error.MSPACK_ERR_OK;
}

View File

@@ -42,9 +42,6 @@ namespace BurnOutSharp.FileType
Directory.CreateDirectory(tempPath);
var decompressor = Library.CreateCABDecompressor(null);
//decompressor.SetParam(LibMSPackSharp.CAB.Parameters.MSCABD_PARAM_FIXMSZIP, 1);
//decompressor.SetParam(LibMSPackSharp.CAB.Parameters.MSCABD_PARAM_SALVAGE, 1);
var cabFile = decompressor.Open(file);
var sub = cabFile.Files;