Fix 0 checks, fix aligned table creation

This commit is contained in:
Matt Nadareski
2022-05-31 15:44:22 -07:00
parent 9ca24a3053
commit 1d26b06592
14 changed files with 159 additions and 150 deletions

View File

@@ -380,7 +380,7 @@ namespace LibMSPackSharp.CAB
Error = Error.MSPACK_ERR_OK;
// If file has more than 0 bytes
if (filelen != 0)
if (filelen > 0)
{
// Set the output file handle to null
State.OutputFileHandle = null;
@@ -391,7 +391,7 @@ namespace LibMSPackSharp.CAB
// - If SysRead() has an error, it will set self.ReadError
// and pass back MSPACK_ERR_READ
long bytes = file.Header.FolderOffset - State.Offset;
if (bytes != 0)
if (bytes > 0)
{
Error error = State.Decompress(State.DecompressorState, bytes);
Error = (error == Error.MSPACK_ERR_READ) ? ReadError : error;
@@ -567,7 +567,7 @@ namespace LibMSPackSharp.CAB
avail = State.InputEnd - State.InputPointer;
// If out of input data, read a new block
if (avail != 0)
if (avail > 0)
{
// Copy as many input bytes available as possible
if (avail > todo)
@@ -688,7 +688,7 @@ namespace LibMSPackSharp.CAB
return Error.MSPACK_ERR_READ;
// Perform checksum test on the block (if one is stored)
if (dataBlockHeader.CheckSum != 0)
if (dataBlockHeader.CheckSum > 0)
{
uint sum2 = Checksum(State.Input, State.InputEnd, dataBlockHeader.CompressedSize, 0);
if (Checksum(hdr, 4, 4, sum2) != dataBlockHeader.CheckSum)
@@ -708,8 +708,8 @@ namespace LibMSPackSharp.CAB
// Otherwise, this is the last part of the block, and no more block
// reading needs to be done.
// EXIT POINT OF LOOP -- uncompressed size != 0
if ((output = dataBlockHeader.UncompressedSize) != 0)
// EXIT POINT OF LOOP -- uncompressed size > 0
if ((output = dataBlockHeader.UncompressedSize) > 0)
return Error.MSPACK_ERR_OK;
// Otherwise, advance to next cabinet
@@ -1009,7 +1009,7 @@ namespace LibMSPackSharp.CAB
}
}
if (false_cabs != 0)
if (false_cabs > 0)
Console.WriteLine($"{false_cabs} false cabinets found");
return Error.MSPACK_ERR_OK;
@@ -1235,7 +1235,7 @@ namespace LibMSPackSharp.CAB
cabinetHeader.PopulateExtendedHeader(buf);
// Skip the reserved header
if (cab.Header.HeaderReserved != 0)
if (cab.Header.HeaderReserved > 0)
{
if (!System.Seek(fh, cab.Header.HeaderReserved, SeekMode.MSPACK_SYS_SEEK_CUR))
return Error = Error.MSPACK_ERR_SEEK;
@@ -1326,7 +1326,7 @@ namespace LibMSPackSharp.CAB
if ((int)file.Header.FolderIndex < cab.Header.NumFolders)
{
Folder ifol = cab.Folders;
while (file.Header.FolderIndex-- != 0)
while (file.Header.FolderIndex-- > 0)
{
if (ifol != null)
ifol = ifol.Next;
@@ -1404,7 +1404,7 @@ namespace LibMSPackSharp.CAB
if (System.Read(fh, buf, 0, _FolderHeader.Size) != _FolderHeader.Size)
return Error.MSPACK_ERR_READ;
if (cab.Header.FolderReserved != 0)
if (cab.Header.FolderReserved > 0)
{
if (!System.Seek(fh, cab.Header.FolderReserved, SeekMode.MSPACK_SYS_SEEK_CUR))
return Error.MSPACK_ERR_SEEK;

View File

@@ -243,7 +243,7 @@ namespace LibMSPackSharp.CHM
return Error = err;
// Validate reset_interval
if (lzxControlData.ResetInterval == 0 || (lzxControlData.ResetInterval % LZX_FRAME_SIZE) != 0)
if (lzxControlData.ResetInterval == 0 || (lzxControlData.ResetInterval % LZX_FRAME_SIZE) > 0)
{
Console.WriteLine("Bad controldata reset interval");
return Error = Error.MSPACK_ERR_DATAFORMAT;
@@ -403,7 +403,7 @@ namespace LibMSPackSharp.CHM
// Get to correct offset.
State.OutputFileHandle = null;
long bytes;
if ((bytes = file.Offset - State.Offset) != 0)
if ((bytes = file.Offset - State.Offset) > 0)
Error = State.State.Decompress(bytes);
// If getting to the correct offset was error free, unpack file
@@ -872,7 +872,7 @@ namespace LibMSPackSharp.CHM
// Read and process all chunks from FirstPMGL to LastPMGL
errors = 0;
while (numChunks-- != 0)
while (numChunks-- > 0)
{
// Read next chunk
if (System.Read(fh, chunk, 0, (int)chm.HeaderSection1.ChunkSize) != (int)chm.HeaderSection1.ChunkSize)
@@ -897,7 +897,7 @@ namespace LibMSPackSharp.CHM
end = (int)(chm.HeaderSection1.ChunkSize - 2);
numEntries = BitConverter.ToUInt16(chunk, end);
while (numEntries-- != 0)
while (numEntries-- > 0)
{
// READ_ENCINT(nameLen)
{

View File

@@ -72,7 +72,7 @@ namespace LibMSPackSharp.CHM
headerSection.Unknown2 = BitConverter.ToUInt32(buffer, 0x0004);
headerSection.FileLength = BitConverter.ToInt64(buffer, 0x0008);
// TODO: Is this supposed to be == 0?
// TODO: Is this supposed to be <= 0?
if (headerSection.FileLength != 0)
return Error.MSPACK_ERR_DATAFORMAT;

View File

@@ -141,7 +141,7 @@ namespace LibMSPackSharp.Compression
// so fake 2 more bytes at the end of input
if (read == 0)
{
if (EndOfInput != 0)
if (EndOfInput > 0)
{
Console.WriteLine("Out of input bytes");
Error = Error.MSPACK_ERR_READ;

View File

@@ -240,7 +240,7 @@ namespace LibMSPackSharp.Compression
leaf <<= 1;
leaf |= reverse & 1;
reverse >>= 1;
} while (--fill != 0);
} while (--fill > 0);
if ((pos += bit_mask) > table_mask)
return false; // Table overrun
@@ -253,7 +253,7 @@ namespace LibMSPackSharp.Compression
{
table[leaf] = sym;
leaf += next_symbol;
} while (--fill != 0);
} while (--fill > 0);
}
bit_mask >>= 1;
@@ -275,7 +275,7 @@ namespace LibMSPackSharp.Compression
leaf <<= 1;
leaf |= reverse & 1;
reverse >>= 1;
} while (--fill != 0);
} while (--fill > 0);
table[leaf] = 0xFFFF;
}
@@ -308,7 +308,7 @@ namespace LibMSPackSharp.Compression
leaf <<= 1;
leaf |= reverse & 1;
reverse >>= 1;
} while (--fill != 0);
} while (--fill > 0);
for (fill = 0; fill < (bit_num - nbits); fill++)
{

View File

@@ -83,42 +83,31 @@ namespace LibMSPackSharp.Compression
#region LZX static data tables
/* LZX static data tables:
*
* LZX uses 'position slots' to represent match offsets. For every match,
* a small 'position slot' number and a small offset from that slot are
* encoded instead of one large offset.
*
* The number of slots is decided by how many are needed to encode the
* largest offset for a given window size. This is easy when the gap between
* slots is less than 128Kb, it's a linear relationship. But when extra_bits
* reaches its limit of 17 (because LZX can only ensure reading 17 bits of
* data at a time), we can only jump 128Kb at a time and have to start
* using more and more position slots as each window size doubles.
*
* position_base[] is an index to the position slot bases
*
* extra_bits[] states how many bits of offset-from-base data is needed.
*
* They are calculated as follows:
* extra_bits[i] = 0 where i < 4
* extra_bits[i] = floor(i/2)-1 where i >= 4 && i < 36
* extra_bits[i] = 17 where i >= 36
* position_base[0] = 0
* position_base[i] = position_base[i-1] + (1 << extra_bits[i-1])
*/
/// <summary>
/// LZX uses 'position slots' to represent match offsets. For every match,
/// a small 'position slot' number and a small offset from that slot are
/// encoded instead of one large offset.
///
/// The number of slots is decided by how many are needed to encode the
/// largest offset for a given window size. This is easy when the gap between
/// slots is less than 128Kb, it's a linear relationship. But when extra_bits
/// reaches its limit of 17 (because LZX can only ensure reading 17 bits of
/// data at a time), we can only jump 128Kb at a time and have to start
/// using more and more position slots as each window size doubles.
/// </summary>
public static readonly uint[] LZXPositionSlots = new uint[11]
{
30, 32, 34, 36, 38, 42, 50, 66, 98, 162, 290
};
public static readonly byte[] LZXExtraBits = new byte[36]
{
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
};
/// <summary>
/// An index to the position slot bases
/// </summary>
/// <remarks>
/// Calculated as follows:
/// LZXPositionBase[0] = 0
/// LZXPositionBase[i] = LZXPositionBase[i - 1] + (1 << ExtraBits(i - 1))
/// </remarks>
public static readonly uint[] LZXPositionBase = new uint[290]
{
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512,

View File

@@ -71,7 +71,7 @@ namespace LibMSPackSharp.Compression
while (EndOfInput == 0)
{
if (lit_run != 0)
if (lit_run > 0)
{
len = (int)READ_HUFFSYM_SAFE(MATCHLEN2_table, MATCHLEN2_len, KWAJ_MATCHLEN2_TBLSIZE, KWAJ_MATCHLEN2_SYMS);
if (Error == Error.MSPACK_ERR_NOMEMORY)

View File

@@ -82,6 +82,8 @@ namespace LibMSPackSharp.Compression
{
public partial class LZX
{
#region Public Functionality
/// <summary>
/// Allocates and initialises LZX decompression state for decoding an LZX
/// stream.
@@ -91,47 +93,24 @@ namespace LibMSPackSharp.Compression
/// null is returned.
/// </summary>
/// <param name="system">A SystemImpl structure used to read from the input stream and write to the output stream, also to allocate and free memory.</param>
/// <param name="input">an input stream with the LZX data.</param>
/// <param name="output">an output stream to write the decoded data to.</param>
/// <param name="window_bits">
/// the size of the decoding window, which must be
/// between 15 and 21 inclusive for regular LZX
/// data, or between 17 and 25 inclusive for
/// LZX DELTA data.</param>
/// <param name="input">An input stream with the LZX data.</param>
/// <param name="output">An output stream to write the decoded data to.</param>
/// <param name="window_bits">The size of the decoding window, which must be between 15 and 21 inclusive for regular LZX data, or between 17 and 25 inclusive for LZX DELTA data.</param>
/// <param name="reset_interval">
/// the interval at which the LZX bitstream is
/// reset, in multiples of LZX frames (32678
/// bytes), e.g. a value of 2 indicates the input
/// stream resets after every 65536 output bytes.
/// A value of 0 indicates that the bitstream never
/// resets, such as in CAB LZX streams.
/// </param>
/// <param name="input_buffer_size">
/// the number of bytes to use as an input
/// bitstream buffer.
/// The interval at which the LZX bitstream is reset, in multiples of LZX frames (32678 bytes),
/// e.g. a value of 2 indicates the input stream resets after every 65536 output bytes.
/// A value of 0 indicates that the bitstream never resets, such as in CAB LZX streams.
/// </param>
/// <param name="input_buffer_size">The number of bytes to use as an input bitstream buffer.</param>
/// <param name="output_length">
/// the length in bytes of the entirely
/// decompressed output stream, if known in
/// advance. It is used to correctly perform the
/// Intel E8 transformation, which must stop 6
/// bytes before the very end of the
/// decompressed stream. It is not otherwise used
/// or adhered to. If the full decompressed
/// length is known in advance, set it here.
/// If it is NOT known, use the value 0, and call
/// lzxd_set_outputLength() once it is
/// known. If never set, 4 of the final 6 bytes
/// of the output stream may be incorrect.
/// The length in bytes of the entirely decompressed output stream, if known in advance.
/// It is used to correctly perform the Intel E8 transformation, which must stop 6 bytes before the very end of the decompressed stream.
/// It is not otherwise used or adhered to. If the full decompressed length is known in advance, set it here.
/// If it is NOT known, use the value 0, and call lzxd_set_outputLength() once it is known.
/// If never set, 4 of the final 6 bytes of the output stream may be incorrect
/// </param>
/// <param name="is_delta">
/// should be zero for all regular LZX data,
/// non-zero for LZX DELTA encoded data.
/// </param>
/// <returns>
/// a pointer to an initialised LZX structure, or null if
/// there was not enough memory or parameters to the function were wrong.
/// </returns>
/// <param name="is_delta">Should be zero for all regular LZX data, non-zero for LZX DELTA encoded data.</param>
/// <returns>A pointer to an initialised LZX structure, or null if there was not enough memory or parameters to the function were wrong.</returns>
public static LZX Init(SystemImpl system, FileStream input, FileStream output, int window_bits, int reset_interval, int input_buffer_size, long output_length, bool is_delta)
{
if (system == null)
@@ -204,11 +183,8 @@ namespace LibMSPackSharp.Compression
///
/// Call this before the first call to lzxd_decompress().
/// </summary>
/// <param name="system">
/// an mspack_system implementation to use with the
/// input param. Only read() will be called.
/// </param>
/// <param name="input"> an input file handle to read reference data using system.read().</param>
/// <param name="system">A SystemImpl structure to use with the input param. Only read() will be called.</param>
/// <param name="input">An input file handle to read reference data using system.read().</param>
/// <param name="length">The length of the reference data. Cannot be longer than the LZX window size.</param>
/// <returns>An error code, or MSPACK_ERR_OK if successful</returns>
public Error SetReferenceData(SystemImpl system, FileStream input, uint length)
@@ -219,7 +195,7 @@ namespace LibMSPackSharp.Compression
return Error.MSPACK_ERR_ARGS;
}
if (Offset != 0)
if (Offset > 0)
{
Console.WriteLine("Too late to set reference data after decoding starts");
return Error.MSPACK_ERR_ARGS;
@@ -282,7 +258,7 @@ namespace LibMSPackSharp.Compression
/// should be considered unusable and lzxd_decompress() should not be
/// called again on this stream.
/// </summary>
/// <param name="out_bytes">the number of bytes of data to decompress.</param>
/// <param name="out_bytes">The number of bytes of data to decompress.</param>
/// <returns>an error code, or MSPACK_ERR_OK if successful</returns>
public Error Decompress(long out_bytes)
{
@@ -290,7 +266,7 @@ namespace LibMSPackSharp.Compression
int this_run, main_element, aligned_bits, j, warned = 0;
byte[] buf = new byte[12];
int runsrc, rundest;
int frame_size = 0, end_frame, match_offset;
uint frame_size = 0, end_frame, match_offset;
// Easy answers
if (out_bytes < 0)
@@ -303,7 +279,7 @@ namespace LibMSPackSharp.Compression
if (i > out_bytes)
i = (int)out_bytes;
if (i != 0)
if (i > 0)
{
if (System.Write(OutputFileHandle, WriteFromE8 ? E8Buffer : Window, OutputPointer, i) != i)
return Error = Error.MSPACK_ERR_WRITE;
@@ -316,14 +292,14 @@ namespace LibMSPackSharp.Compression
if (out_bytes == 0)
return Error.MSPACK_ERR_OK;
end_frame = (int)((uint)((Offset + out_bytes) / LZX_FRAME_SIZE) + 1);
end_frame = (uint)((Offset + out_bytes) / LZX_FRAME_SIZE) + 1;
while (Frame < end_frame)
{
// Have we reached the reset interval? (if there is one?)
if (ResetInterval != 0 && ((Frame % ResetInterval) == 0))
{
if (BlockRemaining != 0)
if (BlockRemaining > 0)
{
// This is a file format error, we can make a best effort to extract what we can
Console.WriteLine($"{BlockRemaining} bytes remaining at reset interval");
@@ -367,7 +343,7 @@ namespace LibMSPackSharp.Compression
// has been filled in.
frame_size = LZX_FRAME_SIZE;
if (Length != 0 && (Length - Offset) < frame_size)
frame_size = (int)(Length - Offset);
frame_size = (uint)(Length - Offset);
// Decode until one more frame is available
bytes_todo = (int)(FramePosition + frame_size - WindowPosition);
@@ -401,11 +377,13 @@ namespace LibMSPackSharp.Compression
ALIGNED_len[i] = (byte)j;
}
BUILD_TABLE(ALIGNED_table, ALIGNED_len, LZX_ALIGNED_TABLEBITS, LZX_ALIGNED_MAXSYMBOLS);
// Rest of aligned header is same as verbatim
// Read lengths of and build main huffman decoding tree
ReadLens(MAINTREE_len, 0, 256);
ReadLens(MAINTREE_len, 256, LZX_NUM_CHARS + NumOffsets);
ReadLengths(MAINTREE_len, 0, 256);
ReadLengths(MAINTREE_len, 256, LZX_NUM_CHARS + NumOffsets);
BUILD_TABLE(MAINTREE_table, MAINTREE_len, LZX_MAINTREE_TABLEBITS, LZX_MAINTREE_MAXSYMBOLS);
// If the literal 0xE8 is anywhere in the block...
@@ -413,14 +391,14 @@ namespace LibMSPackSharp.Compression
IntelStarted = true;
// Read lengths of and build lengths huffman decoding tree
ReadLens(LENGTH_len, 0, LZX_NUM_SECONDARY_LENGTHS);
ReadLengths(LENGTH_len, 0, LZX_NUM_SECONDARY_LENGTHS);
BUILD_TABLE_MAYBE_EMPTY();
break;
case LZXBlockType.LZX_BLOCKTYPE_VERBATIM:
// Read lengths of and build main huffman decoding tree
ReadLens(MAINTREE_len, 0, 256);
ReadLens(MAINTREE_len, 256, LZX_NUM_CHARS + NumOffsets);
ReadLengths(MAINTREE_len, 0, 256);
ReadLengths(MAINTREE_len, 256, LZX_NUM_CHARS + NumOffsets);
BUILD_TABLE(MAINTREE_table, MAINTREE_len, LZX_MAINTREE_TABLEBITS, LZX_MAINTREE_MAXSYMBOLS);
// If the literal 0xE8 is anywhere in the block...
@@ -428,7 +406,7 @@ namespace LibMSPackSharp.Compression
IntelStarted = true;
// Read lengths of and build lengths huffman decoding tree
ReadLens(LENGTH_len, 0, LZX_NUM_SECONDARY_LENGTHS);
ReadLengths(LENGTH_len, 0, LZX_NUM_SECONDARY_LENGTHS);
BUILD_TABLE_MAYBE_EMPTY();
break;
@@ -494,7 +472,7 @@ namespace LibMSPackSharp.Compression
match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
if (match_length == LZX_NUM_PRIMARY_LENGTHS)
{
if (LENGTH_empty != 0)
if (LENGTH_empty)
{
Console.WriteLine("LENGTH symbol needed but tree is empty");
return Error = Error.MSPACK_ERR_DECRUNCH;
@@ -507,22 +485,22 @@ namespace LibMSPackSharp.Compression
match_length += LZX_MIN_MATCH;
// Get match offset
switch ((match_offset = (main_element >> 3)))
switch ((match_offset = (uint)(main_element >> 3)))
{
case 0:
match_offset = (int)R[0];
match_offset = R[0];
break;
case 1:
match_offset = (int)R[1];
match_offset = R[1];
R[1] = R[0];
R[0] = (uint)match_offset;
R[0] = match_offset;
break;
case 2:
match_offset = (int)R[2];
match_offset = R[2];
R[2] = R[0];
R[0] = (uint)match_offset;
R[0] = match_offset;
break;
default:
@@ -534,32 +512,32 @@ namespace LibMSPackSharp.Compression
}
else
{
extra = (match_offset >= 36) ? 17 : LZXExtraBits[match_offset];
extra = ExtraBits(match_offset);
verbatim_bits = (int)READ_BITS_MSB(extra);
match_offset = (int)(LZXPositionBase[match_offset] - 2 + verbatim_bits);
match_offset = (uint)(PositionBase(match_offset) - 2 + verbatim_bits);
}
}
else // LZX_BLOCKTYPE_ALIGNED
{
extra = (match_offset >= 36) ? 17 : LZXExtraBits[match_offset];
match_offset = (int)(LZXPositionBase[match_offset] - 2);
extra = ExtraBits(match_offset);
match_offset = (uint)(PositionBase(match_offset) - 2);
if (extra > 3) // >3: verbatim and aligned bits
{
extra -= 3;
verbatim_bits = (int)READ_BITS_MSB(extra);
match_offset += (verbatim_bits << 3);
match_offset += (uint)(verbatim_bits << 3);
aligned_bits = (int)READ_HUFFSYM_MSB(ALIGNED_table, ALIGNED_len, LZX_ALIGNED_TABLEBITS, LZX_ALIGNED_MAXSYMBOLS);
match_offset += aligned_bits;
match_offset += (uint)aligned_bits;
}
else if (extra == 3) // 3: aligned bits only
{
aligned_bits = (int)READ_HUFFSYM_MSB(ALIGNED_table, ALIGNED_len, LZX_ALIGNED_TABLEBITS, LZX_ALIGNED_MAXSYMBOLS);
match_offset += aligned_bits;
match_offset += (uint)aligned_bits;
}
else if (extra > 0) // 1-2: verbatim bits only
{
verbatim_bits = (int)READ_BITS_MSB(extra);
match_offset += verbatim_bits;
match_offset += (uint)verbatim_bits;
}
else // 0: not defined in LZX specification!
{
@@ -570,7 +548,7 @@ namespace LibMSPackSharp.Compression
// Update repeated offset LRU queue
R[2] = R[1];
R[1] = R[0];
R[0] = (uint)match_offset;
R[0] = match_offset;
break;
}
@@ -611,6 +589,10 @@ namespace LibMSPackSharp.Compression
return Error = Error.MSPACK_ERR_DECRUNCH;
}
// TODO: This falls out of sync after (91,3)
// - Official program goes to (94, 3)
// - Ours goes to (95, 3)
// Copy match
rundest = WindowPosition;
i = match_length;
@@ -625,7 +607,7 @@ namespace LibMSPackSharp.Compression
}
// j = length from match offset to end of window
j = match_offset - WindowPosition;
j = (int)match_offset - WindowPosition;
if (j > (int)WindowSize)
{
Console.WriteLine("Match offset beyond window boundaries");
@@ -652,7 +634,7 @@ namespace LibMSPackSharp.Compression
}
else
{
runsrc = rundest - match_offset;
runsrc = rundest - (int)match_offset;
while (i-- > 0)
{
Window[rundest++] = Window[runsrc++];
@@ -732,7 +714,7 @@ namespace LibMSPackSharp.Compression
if (IntelStarted && IntelFileSize != 0 && Frame < 32768 && frame_size > 10)
{
int data = 0;
int dataend = frame_size - 10;
int dataend = (int)frame_size - 10;
int curpos = (int)Offset;
int filesize = IntelFileSize;
int abs_off, rel_off;
@@ -770,7 +752,7 @@ namespace LibMSPackSharp.Compression
OutputPointer = (int)FramePosition;
}
OutputEnd = frame_size;
OutputEnd = (int)frame_size;
// Write a frame
i = (int)(out_bytes < frame_size ? out_bytes : frame_size);
@@ -792,7 +774,7 @@ namespace LibMSPackSharp.Compression
FramePosition = 0;
}
if (out_bytes != 0)
if (out_bytes > 0)
{
Console.WriteLine($"{out_bytes} bytes left to output");
return Error = Error.MSPACK_ERR_DECRUNCH;
@@ -801,6 +783,43 @@ namespace LibMSPackSharp.Compression
return Error.MSPACK_ERR_OK;
}
#endregion
#region Helpers
/// <summary>
/// States how many bits of offset-from-base data is needed.
/// </summary>
private int ExtraBits(uint offset)
{
if (offset < 4)
return 0;
else if (offset >= 4 && offset < 36)
return (int)Math.Floor((double)(offset / 2)) - 1;
else // offset >= 36
return 17;
}
/// <summary>
/// An index to the position slot bases
/// </summary>
private long PositionBase(uint offset)
{
if (offset < 0 || offset >= 290)
return 0;
return LZXPositionBase[offset];
// TODO: Replace naieve recursive implementation
if (offset == 0)
return 0;
else
return PositionBase(offset - 1) + (1 << ExtraBits(offset - 1));
}
/// <summary>
/// Reset the internal state
/// </summary>
private void ResetState()
{
R[0] = 1;
@@ -822,5 +841,7 @@ namespace LibMSPackSharp.Compression
LENGTH_len[i] = 0;
}
}
#endregion
}
}

View File

@@ -36,7 +36,7 @@ namespace LibMSPackSharp.Compression
private Error BUILD_TABLE_MAYBE_EMPTY()
{
LENGTH_empty = 0;
LENGTH_empty = false;
if (!MakeDecodeTableMSB(LZX_LENGTH_MAXSYMBOLS, LZX_LENGTH_TABLEBITS, LENGTH_len, LENGTH_table))
{
for (int i = 0; i < LZX_LENGTH_MAXSYMBOLS; i++)
@@ -49,7 +49,7 @@ namespace LibMSPackSharp.Compression
}
// Empty tree - allow it, but don't decode symbols with it
LENGTH_empty = 1;
LENGTH_empty = true;
}
return Error = Error.MSPACK_ERR_OK;
@@ -60,7 +60,7 @@ namespace LibMSPackSharp.Compression
/// first to last in the given table. The code lengths are stored in their
/// own special LZX way.
/// </summary>
private Error ReadLens(byte[] lens, uint first, uint last)
private Error ReadLengths(byte[] lens, uint first, uint last)
{
uint x, y;
int z;
@@ -84,7 +84,7 @@ namespace LibMSPackSharp.Compression
// Code = 17, run of ([read 4 bits]+4) zeros
y = (uint)READ_BITS_MSB(4);
y += 4;
while (y-- != 0)
while (y-- > 0)
{
lens[x++] = 0;
}
@@ -94,7 +94,7 @@ namespace LibMSPackSharp.Compression
// Code = 18, run of ([read 5 bits]+20) zeros
y = (uint)READ_BITS_MSB(5);
y += 20;
while (y-- != 0)
while (y-- > 0)
{
lens[x++] = 0;
}
@@ -110,7 +110,7 @@ namespace LibMSPackSharp.Compression
if (z < 0)
z += 17;
while (y-- != 0)
while (y-- > 0)
{
lens[x++] = (byte)z;
}

View File

@@ -124,7 +124,7 @@ namespace LibMSPackSharp.Compression
#endregion
public byte LENGTH_empty { get; set; }
public bool LENGTH_empty { get; set; }
public byte[] E8Buffer { get; set; } = new byte[LZX_FRAME_SIZE];

View File

@@ -100,7 +100,7 @@ namespace LibMSPackSharp.Compression
if (i > out_bytes)
i = (int)out_bytes;
if (i != 0)
if (i > 0)
{
if (System.Write(OutputFileHandle, Window, OutputPointer, i) != i)
return Error = Error.MSPACK_ERR_WRITE;
@@ -176,7 +176,7 @@ namespace LibMSPackSharp.Compression
out_bytes -= i;
}
if (out_bytes != 0)
if (out_bytes > 0)
{
Console.WriteLine($"Bytes left to output: {out_bytes}");
return Error = Error.MSPACK_ERR_DECRUNCH;
@@ -317,7 +317,7 @@ namespace LibMSPackSharp.Compression
if ((i + run) > (lit_codes + dist_codes))
return Error.INF_ERR_BITOVERRUN;
while (run-- != 0)
while (run-- > 0)
{
lens[i++] = (byte)code;
}
@@ -517,7 +517,7 @@ namespace LibMSPackSharp.Compression
if (length < 12)
{
// Short match, use slower loop but no loop setup code
while (length-- != 0)
while (length-- > 0)
{
Window[WindowPosition++] = Window[match_posn++];
match_posn &= MSZIP_FRAME_SIZE - 1;
@@ -543,7 +543,7 @@ namespace LibMSPackSharp.Compression
runsrc = (int)match_posn;
match_posn += this_run;
length -= this_run;
while (this_run-- != 0)
while (this_run-- > 0)
{
Window[rundest++] = Window[runsrc++];
}
@@ -569,7 +569,7 @@ namespace LibMSPackSharp.Compression
} while (last_block == 0);
// Flush the remaining data
if (WindowPosition != 0)
if (WindowPosition > 0)
{
if (FlushWindow(WindowPosition) != Error.MSPACK_ERR_OK)
return Error.INF_ERR_FLUSH;

View File

@@ -139,7 +139,7 @@ namespace LibMSPackSharp.Compression
if (i > out_bytes)
i = (int)out_bytes;
if (i != 0)
if (i > 0)
{
if (System.Write(OutputFileHandle, Window, OutputPointer, i) != i)
return Error = Error.MSPACK_ERR_WRITE;
@@ -260,7 +260,7 @@ namespace LibMSPackSharp.Compression
i = (int)(WindowSize - window_posn);
j = (int)(window_posn - match_offset);
while (i-- != 0)
while (i-- > 0)
{
window[rundest++] = window[j++ & (WindowSize - 1)];
}
@@ -288,7 +288,7 @@ namespace LibMSPackSharp.Compression
// Copy second part of match, after window wrap
rundest = 0;
i = (int)(match_length - (WindowSize - window_posn));
while (i-- != 0)
while (i-- > 0)
{
window[rundest++] = window[j++ & (WindowSize - 1)];
}
@@ -396,7 +396,7 @@ namespace LibMSPackSharp.Compression
}
if (out_bytes != 0)
if (out_bytes > 0)
{
i = (int)out_bytes;
@@ -476,7 +476,7 @@ namespace LibMSPackSharp.Compression
QTMDModelSym tmp;
int i, j;
if (--model.ShiftsLeft != 0)
if (--model.ShiftsLeft > 0)
{
for (i = model.Entries - 1; i >= 0; i--)
{

View File

@@ -103,7 +103,7 @@ namespace LibMSPackSharp.OAB
out_ofh.OrigSys = System;
out_ofh.OrigFile = outfh;
while (target_size != 0)
while (target_size > 0)
{
if (System.Read(infh, buf, 0, oabblk_SIZEOF) != oabblk_SIZEOF)
{
@@ -303,7 +303,7 @@ namespace LibMSPackSharp.OAB
out_ofh.OrigSys = System;
out_ofh.OrigFile = outfh;
while (target_size != 0)
while (target_size > 0)
{
if (System.Read(infh, buf, 0, patchblk_SIZEOF) != patchblk_SIZEOF)
{
@@ -479,7 +479,7 @@ namespace LibMSPackSharp.OAB
/// </summary>
private Error CopyFileHandle(FileStream input, FileStream output, int bytesToCopy, byte[] buf, int bufferSize)
{
while (bytesToCopy != 0)
while (bytesToCopy > 0)
{
int run = bufferSize;
if (run > bytesToCopy)

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text.RegularExpressions;
using BurnOutSharp.Interfaces;
using BurnOutSharp.Tools;
using LibMSPackSharp;