diff --git a/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs b/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs index a6ede2ec..7f6756d5 100644 --- a/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs +++ b/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs @@ -11,6 +11,7 @@ */ using System; +using static LibMSPackSharp.Compression.Constants; namespace LibMSPackSharp.Compression { @@ -121,7 +122,63 @@ namespace LibMSPackSharp.Compression * to the bit buffer when the bit buffer already has 1 to 15 bits left. */ - public Error ReadInput() + #region Common + + /// + /// Initialises bitstream state in state structure + /// + public void INIT_BITS() + { + InputPointer = 0; + InputEnd = 0; + BitBuffer = 0; + BitsLeft = 0; + EndOfInput = 0; + } + + /// + /// Stores bitstream state in state structure + /// + public void STORE_BITS(int i_ptr, int i_end, uint bit_buffer, int bits_left) + { + InputPointer = i_ptr; + InputEnd = i_end; + BitBuffer = bit_buffer; + BitsLeft = bits_left; + } + + /// + /// Restores bitstream state from state structure + /// + public void RESTORE_BITS(out int i_ptr, out int i_end, out uint bit_buffer, out int bits_left) + { + i_ptr = InputPointer; + i_end = InputEnd; + bit_buffer = BitBuffer; + bits_left = BitsLeft; + } + + /// + /// Read from the input if the buffer is empty + /// + public Error READ_IF_NEEDED(ref int i_ptr, ref int i_end) + { + if (i_ptr >= i_end) + { + if (ReadInput() != Error.MSPACK_ERR_OK) + return Error; + + i_ptr = InputPointer; + i_end = InputEnd; + } + + return Error.MSPACK_ERR_OK; + } + + /// + /// Read an input stream and fill the buffer + /// + protected Error ReadInput() { int read = System.Read(InputFileHandle, InputBuffer, 0, (int)InputBufferSize); if (read < 0) @@ -152,6 +209,67 @@ namespace LibMSPackSharp.Compression #endregion + #region MSB + + /// + /// Inject data into the bit buffer + /// + public void INJECT_BITS_MSB(int bitdata, int nbits, ref uint bit_buffer, ref int bits_left) + { + bit_buffer |= (uint)(bitdata << (BITBUF_WIDTH - nbits - bits_left)); + bits_left += nbits; + } + + /// + /// Extracts without removing N bits from the bit buffer + /// + public long PEEK_BITS_MSB(int nbits, uint bit_buffer) => (bit_buffer >> (BITBUF_WIDTH - (nbits))); + + /// + /// Removes N bits from the bit buffer + /// + public void REMOVE_BITS_MSB(int nbits, ref uint bit_buffer, ref int bits_left) + { + bit_buffer <<= nbits; + bits_left -= nbits; + } + + #endregion + + #region LSB + + /// + /// Inject data into the bit buffer + /// + public void INJECT_BITS_LSB(int bitdata, int nbits, ref uint bit_buffer, ref int bits_left) + { + bit_buffer |= (uint)(bitdata << bits_left); + bits_left += nbits; + } + + /// + /// Extracts without removing N bits from the bit buffer + /// + public long PEEK_BITS_LSB(int nbits, uint bit_buffer) => (bit_buffer & ((1 << (nbits)) - 1)); + + /// + /// Extracts without removing N bits from the bit buffer using a bit mask + /// + public long PEEK_BITS_T_LSB(int nbits, uint bit_buffer) => bit_buffer & lsb_bit_mask[(nbits)]; + + /// + /// Removes N bits from the bit buffer + /// + public void REMOVE_BITS_LSB(int nbits, ref uint bit_buffer, ref int bits_left) + { + bit_buffer >>= nbits; + bits_left -= nbits; + } + + #endregion + + #endregion + // TODO: These should be in a separate file #region ReadHuff Methods diff --git a/BurnOutSharp/External/libmspack/Compression/LZHKWAJ.cs b/BurnOutSharp/External/libmspack/Compression/LZHKWAJ.cs index 3245dc44..c72a193d 100644 --- a/BurnOutSharp/External/libmspack/Compression/LZHKWAJ.cs +++ b/BurnOutSharp/External/libmspack/Compression/LZHKWAJ.cs @@ -41,32 +41,16 @@ namespace LibMSPackSharp.Compression public static Error Decompress(LZHKWAJStream lzh) { - uint bit_buffer; - int bits_left, i; + int i; ushort sym; - int i_ptr, i_end, lit_run = 0; + int lit_run = 0; int j, pos = 0, len, offset; Error err; uint[] types = new uint[6]; // Reset global state - - //INIT_BITS - { - lzh.InputPointer = 0; - lzh.InputEnd = 0; - lzh.BitBuffer = 0; - lzh.BitsLeft = 0; - lzh.EndOfInput = 0; - } - - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.INIT_BITS(); + lzh.RESTORE_BITS(out int i_ptr, out int i_end, out uint bit_buffer, out int bits_left); for (i = 0; i < LZSS.LZSS_WINDOW_SIZE; i++) { @@ -95,22 +79,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (types[i]) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (types[i]) = (uint)lzh.PEEK_BITS_MSB(4, bit_buffer); + lzh.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -122,25 +97,13 @@ namespace LibMSPackSharp.Compression //BUILD_TREE(MATCHLEN1, types[0]) { - //STORE_BITS - { - lzh.InputPointer = i_ptr; - lzh.InputEnd = i_end; - lzh.BitBuffer = bit_buffer; - lzh.BitsLeft = bits_left; - } + lzh.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); err = ReadLens(lzh, types[0], KWAJ_MATCHLEN1_SYMS, lzh.MATCHLEN1_len); if (err != Error.MSPACK_ERR_OK) return err; - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); if (!CompressionStream.MakeDecodeTable(KWAJ_MATCHLEN1_SYMS, KWAJ_TABLEBITS, lzh.MATCHLEN1_len, lzh.MATCHLEN1_table, msb: true)) return Error.MSPACK_ERR_DATAFORMAT; @@ -148,25 +111,13 @@ namespace LibMSPackSharp.Compression //BUILD_TREE(MATCHLEN2, types[1]) { - //STORE_BITS - { - lzh.InputPointer = i_ptr; - lzh.InputEnd = i_end; - lzh.BitBuffer = bit_buffer; - lzh.BitsLeft = bits_left; - } + lzh.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); err = ReadLens(lzh, types[1], KWAJ_MATCHLEN2_SYMS, lzh.MATCHLEN2_len); if (err != Error.MSPACK_ERR_OK) return err; - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); if (!CompressionStream.MakeDecodeTable(KWAJ_MATCHLEN2_SYMS, KWAJ_TABLEBITS, lzh.MATCHLEN2_len, lzh.MATCHLEN2_table, msb: true)) return Error.MSPACK_ERR_DATAFORMAT; @@ -174,25 +125,13 @@ namespace LibMSPackSharp.Compression //BUILD_TREE(LITLEN, types[2]) { - //STORE_BITS - { - lzh.InputPointer = i_ptr; - lzh.InputEnd = i_end; - lzh.BitBuffer = bit_buffer; - lzh.BitsLeft = bits_left; - } + lzh.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); err = ReadLens(lzh, types[2], KWAJ_LITLEN_SYMS, lzh.LITLEN_len); if (err != Error.MSPACK_ERR_OK) return err; - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); if (!CompressionStream.MakeDecodeTable(KWAJ_LITLEN_SYMS, KWAJ_TABLEBITS, lzh.LITLEN_len, lzh.LITLEN_table, msb: true)) return Error.MSPACK_ERR_DATAFORMAT; @@ -200,25 +139,13 @@ namespace LibMSPackSharp.Compression //BUILD_TREE(OFFSET, types[3]) { - //STORE_BITS - { - lzh.InputPointer = i_ptr; - lzh.InputEnd = i_end; - lzh.BitBuffer = bit_buffer; - lzh.BitsLeft = bits_left; - } + lzh.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); err = ReadLens(lzh, types[3], KWAJ_OFFSET_SYMS, lzh.OFFSET_len); if (err != Error.MSPACK_ERR_OK) return err; - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); if (!CompressionStream.MakeDecodeTable(KWAJ_OFFSET_SYMS, KWAJ_TABLEBITS, lzh.OFFSET_len, lzh.OFFSET_table, msb: true)) return Error.MSPACK_ERR_DATAFORMAT; @@ -226,25 +153,13 @@ namespace LibMSPackSharp.Compression //BUILD_TREE(LITERAL, types[4]) { - //STORE_BITS - { - lzh.InputPointer = i_ptr; - lzh.InputEnd = i_end; - lzh.BitBuffer = bit_buffer; - lzh.BitsLeft = bits_left; - } + lzh.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); err = ReadLens(lzh, types[4], KWAJ_LITERAL_SYMS, lzh.LITERAL_len); if (err != Error.MSPACK_ERR_OK) return err; - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); if (!CompressionStream.MakeDecodeTable(KWAJ_LITERAL_SYMS, KWAJ_TABLEBITS, lzh.LITERAL_len, lzh.LITERAL_table, msb: true)) return Error.MSPACK_ERR_DATAFORMAT; @@ -273,16 +188,12 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = lzh.MATCHLEN2_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (KWAJ_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(MATCHLEN2)) + sym = lzh.MATCHLEN2_table[lzh.PEEK_BITS_MSB(KWAJ_TABLEBITS, bit_buffer)]; if (sym >= KWAJ_MATCHLEN2_SYMS) { //HUFF_TRAVERSE(MATCHLEN2) @@ -300,12 +211,7 @@ namespace LibMSPackSharp.Compression (len) = sym; i = lzh.MATCHLEN2_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzh.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -333,16 +239,12 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = lzh.MATCHLEN1_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (KWAJ_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(MATCHLEN1)) + sym = lzh.MATCHLEN1_table[lzh.PEEK_BITS_MSB(KWAJ_TABLEBITS, bit_buffer)]; if (sym >= KWAJ_MATCHLEN1_SYMS) { //HUFF_TRAVERSE(MATCHLEN1) @@ -360,12 +262,7 @@ namespace LibMSPackSharp.Compression (len) = sym; i = lzh.MATCHLEN1_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzh.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -397,16 +294,12 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = lzh.OFFSET_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (KWAJ_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(OFFSET)) + sym = lzh.OFFSET_table[lzh.PEEK_BITS_MSB(KWAJ_TABLEBITS, bit_buffer)]; if (sym >= KWAJ_OFFSET_SYMS) { //HUFF_TRAVERSE(OFFSET) @@ -424,12 +317,7 @@ namespace LibMSPackSharp.Compression (j) = sym; i = lzh.OFFSET_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzh.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -457,22 +345,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (j) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (6))); //PEEK_BITS(6) - - //REMOVE_BITS(6) - { - bit_buffer <<= (6); - bits_left -= (6); - } + (j) = (int)lzh.PEEK_BITS_MSB(6, bit_buffer); + lzh.REMOVE_BITS_MSB(6, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -517,16 +396,12 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = lzh.LITLEN_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (KWAJ_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(LITLEN)) + sym = lzh.LITLEN_table[lzh.PEEK_BITS_MSB(KWAJ_TABLEBITS, bit_buffer)]; if (sym >= KWAJ_LITLEN_SYMS) { //HUFF_TRAVERSE(tbl) @@ -544,12 +419,7 @@ namespace LibMSPackSharp.Compression (len) = sym; i = lzh.LITLEN_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzh.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -579,16 +449,12 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = lzh.LITERAL_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (KWAJ_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(LITERAL)) + sym = lzh.LITERAL_table[lzh.PEEK_BITS_MSB(KWAJ_TABLEBITS, bit_buffer)]; if (sym >= KWAJ_LITERAL_SYMS) { //HUFF_TRAVERSE(LITERAL) @@ -606,12 +472,7 @@ namespace LibMSPackSharp.Compression (j) = sym; i = lzh.LITERAL_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzh.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -643,13 +504,7 @@ namespace LibMSPackSharp.Compression uint i, c, sel; Error err; - //RESTORE_BITS - { - i_ptr = lzh.InputPointer; - i_end = lzh.InputEnd; - bit_buffer = lzh.BitBuffer; - bits_left = lzh.BitsLeft; - } + lzh.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); switch (type) { @@ -683,22 +538,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (c) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (c) = (uint)lzh.PEEK_BITS_MSB(4, bit_buffer); + lzh.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -727,22 +573,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (sel) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + (sel) = (uint)lzh.PEEK_BITS_MSB(1, bit_buffer); + lzh.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -774,22 +611,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (sel) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + (sel) = (uint)lzh.PEEK_BITS_MSB(1, bit_buffer); + lzh.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -821,22 +649,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (c) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (c) = (uint)lzh.PEEK_BITS_MSB(4, bit_buffer); + lzh.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -870,22 +689,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (c) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (c) = (uint)lzh.PEEK_BITS_MSB(4, bit_buffer); + lzh.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -914,22 +724,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (sel) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (2))); //PEEK_BITS(2) - - //REMOVE_BITS(2) - { - bit_buffer <<= (2); - bits_left -= (2); - } + (sel) = (uint)lzh.PEEK_BITS_MSB(2, bit_buffer); + lzh.REMOVE_BITS_MSB(2, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -957,22 +758,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (c) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (c) = (uint)lzh.PEEK_BITS_MSB(4, bit_buffer); + lzh.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -1011,22 +803,13 @@ namespace LibMSPackSharp.Compression i_end = lzh.InputEnd; } - //INJECT_BITS(lzh.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)(lzh.InputBuffer[i_ptr++]) << (CompressionStream.BITBUF_WIDTH - (8) - bits_left); - bits_left += (8); - } + lzh.INJECT_BITS_MSB(lzh.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (c) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (c) = (uint)lzh.PEEK_BITS_MSB(4, bit_buffer); + lzh.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } if (lzh.EndOfInput != 0 && bits_left < lzh.EndOfInput) @@ -1039,13 +822,7 @@ namespace LibMSPackSharp.Compression break; } - //STORE_BITS - { - lzh.InputPointer = i_ptr; - lzh.InputEnd = i_end; - lzh.BitBuffer = bit_buffer; - lzh.BitsLeft = bits_left; - } + lzh.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); return Error.MSPACK_ERR_OK; } diff --git a/BurnOutSharp/External/libmspack/Compression/LZX.cs b/BurnOutSharp/External/libmspack/Compression/LZX.cs index a20c25ec..8b8ebeb4 100644 --- a/BurnOutSharp/External/libmspack/Compression/LZX.cs +++ b/BurnOutSharp/External/libmspack/Compression/LZX.cs @@ -330,15 +330,7 @@ namespace LibMSPackSharp.Compression }; ResetState(lzx); - - //INIT_BITS - { - lzx.InputPointer = 0; - lzx.InputEnd = 0; - lzx.BitBuffer = 0; - lzx.BitsLeft = 0; - lzx.EndOfInput = 0; - } + lzx.INIT_BITS(); return lzx; } @@ -482,15 +474,7 @@ namespace LibMSPackSharp.Compression return Error.MSPACK_ERR_OK; // Restore local state - - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } - + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); window = lzx.Window; window_posn = lzx.WindowPosition; R0 = lzx.R0; @@ -531,48 +515,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - //REMOVE_BITS(16) - { - bit_buffer <<= (16); - bits_left -= (16); - } + lzx.REMOVE_BITS_MSB(16, ref bit_buffer, ref bits_left); } // Calculate size of frame: all frames are 32k except the final frame @@ -592,17 +551,9 @@ namespace LibMSPackSharp.Compression // Realign if previous block was an odd-sized UNCOMPRESSED block if ((lzx.BlockType == LZXBlockType.LZX_BLOCKTYPE_UNCOMPRESSED) && (lzx.BlockLength & 1) != 0) { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; i_ptr++; } @@ -617,50 +568,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (lzx.BlockType) = (LZXBlockType)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (3))); //PEEK_BITS(3) - - //REMOVE_BITS(3) - { - bit_buffer <<= (3); - bits_left -= (3); - } + (lzx.BlockType) = (LZXBlockType)lzx.PEEK_BITS_MSB(3, bit_buffer); + lzx.REMOVE_BITS_MSB(3, ref bit_buffer, ref bits_left); } // Read header if necessary @@ -678,50 +603,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + (i) = (int)lzx.PEEK_BITS_MSB(1, bit_buffer); + lzx.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } if (i != 0) @@ -734,50 +633,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (16))); //PEEK_BITS(16) - - //REMOVE_BITS(16) - { - bit_buffer <<= (16); - bits_left -= (16); - } + (i) = (int)lzx.PEEK_BITS_MSB(16, bit_buffer); + lzx.REMOVE_BITS_MSB(16, ref bit_buffer, ref bits_left); } //READ_BITS(j, 16) @@ -788,50 +661,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (j) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (16))); //PEEK_BITS(16) - - //REMOVE_BITS(16) - { - bit_buffer <<= (16); - bits_left -= (16); - } + (j) = (int)lzx.PEEK_BITS_MSB(16, bit_buffer); + lzx.REMOVE_BITS_MSB(16, ref bit_buffer, ref bits_left); } } @@ -847,50 +694,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (16))); //PEEK_BITS(16) - - //REMOVE_BITS(16) - { - bit_buffer <<= (16); - bits_left -= (16); - } + (i) = (int)lzx.PEEK_BITS_MSB(16, bit_buffer); + lzx.REMOVE_BITS_MSB(16, ref bit_buffer, ref bits_left); } //READ_BITS(j, 8) @@ -901,50 +722,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (j) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (8))); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer <<= (8); - bits_left -= (8); - } + (j) = (int)lzx.PEEK_BITS_MSB(8, bit_buffer); + lzx.REMOVE_BITS_MSB(8, ref bit_buffer, ref bits_left); } lzx.BlockRemaining = lzx.BlockLength = (uint)((i << 8) | j); @@ -965,50 +760,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (j) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (3))); //PEEK_BITS(3) - - //REMOVE_BITS(3) - { - bit_buffer <<= (3); - bits_left -= (3); - } + (j) = (int)lzx.PEEK_BITS_MSB(3, bit_buffer); + lzx.REMOVE_BITS_MSB(3, ref bit_buffer, ref bits_left); } lzx.ALIGNED_len[i] = (byte)j; @@ -1027,46 +796,22 @@ namespace LibMSPackSharp.Compression //READ_LENGTHS(MAINTREE, 0, 256) { - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if (ReadLens(lzx, lzx.MAINTREE_len, (0), (uint)(256)) != Error.MSPACK_ERR_OK) return lzx.Error; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } //READ_LENGTHS(MAINTREE, 256, LZX_NUM_CHARS + lzx.NumOffsets) { - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if (ReadLens(lzx, lzx.MAINTREE_len, (256), (uint)(LZX_NUM_CHARS + lzx.NumOffsets)) != Error.MSPACK_ERR_OK) return lzx.Error; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } //BUILD_TABLE(MAINTREE) @@ -1086,24 +831,12 @@ namespace LibMSPackSharp.Compression //READ_LENGTHS(LENGTH, 0, LZX_NUM_SECONDARY_LENGTHS) { - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if (ReadLens(lzx, lzx.LENGTH_len, (0), (uint)(LZX_NUM_SECONDARY_LENGTHS)) != Error.MSPACK_ERR_OK) return lzx.Error; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } //BUILD_TABLE_MAYBE_EMPTY(LENGTH) @@ -1132,46 +865,22 @@ namespace LibMSPackSharp.Compression //READ_LENGTHS(MAINTREE, 0, 256) { - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if (ReadLens(lzx, lzx.MAINTREE_len, (0), (uint)(256)) != Error.MSPACK_ERR_OK) return lzx.Error; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } //READ_LENGTHS(MAINTREE, 256, LZX_NUM_CHARS + lzx.NumOffsets) { - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if (ReadLens(lzx, lzx.MAINTREE_len, (256), (uint)(LZX_NUM_CHARS + lzx.NumOffsets)) != Error.MSPACK_ERR_OK) return lzx.Error; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } //BUILD_TABLE(MAINTREE) @@ -1191,24 +900,12 @@ namespace LibMSPackSharp.Compression //READ_LENGTHS(LENGTH, 0, LZX_NUM_SECONDARY_LENGTHS) { - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if (ReadLens(lzx, lzx.LENGTH_len, (0), (uint)(LZX_NUM_SECONDARY_LENGTHS)) != Error.MSPACK_ERR_OK) return lzx.Error; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } //BUILD_TABLE_MAYBE_EMPTY(LENGTH) @@ -1245,39 +942,18 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } @@ -1288,17 +964,9 @@ namespace LibMSPackSharp.Compression // Read 12 bytes of stored R0 / R1 / R2 values for (rundest = 0, i = 0; i < 12; i++) { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; buf[rundest++] = lzx.InputBuffer[i_ptr++]; } @@ -1340,44 +1008,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - sym = lzx.MAINTREE_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (LZX_MAINTREE_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(MAINTREE)) + sym = lzx.MAINTREE_table[lzx.PEEK_BITS_MSB(LZX_MAINTREE_TABLEBITS, bit_buffer)]; if (sym >= LZX_MAINTREE_MAXSYMBOLS) { //HUFF_TRAVERSE(MAINTREE) @@ -1395,12 +1042,7 @@ namespace LibMSPackSharp.Compression (main_element) = sym; i = lzx.MAINTREE_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzx.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } if (main_element < LZX_NUM_CHARS) @@ -1432,44 +1074,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - sym = lzx.LENGTH_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (LZX_LENGTH_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(LENGTH)) + sym = lzx.LENGTH_table[lzx.PEEK_BITS_MSB(LZX_LENGTH_TABLEBITS, bit_buffer)]; if (sym >= LZX_LENGTH_MAXSYMBOLS) { //HUFF_TRAVERSE(LENGTH) @@ -1487,12 +1108,7 @@ namespace LibMSPackSharp.Compression (length_footer) = sym; i = lzx.LENGTH_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzx.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } match_length += length_footer; @@ -1538,50 +1154,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (verbatim_bits) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (extra))); //PEEK_BITS(extra) - - //REMOVE_BITS(extra) - { - bit_buffer <<= (extra); - bits_left -= (extra); - } + (verbatim_bits) = (int)lzx.PEEK_BITS_MSB(extra, bit_buffer); + lzx.REMOVE_BITS_MSB(extra, ref bit_buffer, ref bits_left); } match_offset = (uint)(position_base[match_offset] - 2 + verbatim_bits); @@ -1607,50 +1197,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (verbatim_bits) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (extra))); //PEEK_BITS(extra) - - //REMOVE_BITS(extra) - { - bit_buffer <<= (extra); - bits_left -= (extra); - } + (verbatim_bits) = (int)lzx.PEEK_BITS_MSB(extra, bit_buffer); + lzx.REMOVE_BITS_MSB(extra, ref bit_buffer, ref bits_left); } match_offset += (uint)(verbatim_bits << 3); @@ -1663,44 +1227,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - sym = lzx.ALIGNED_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (LZX_ALIGNED_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(ALIGNED)) + sym = lzx.ALIGNED_table[lzx.PEEK_BITS_MSB(LZX_ALIGNED_TABLEBITS, bit_buffer)]; if (sym >= LZX_ALIGNED_MAXSYMBOLS) { //HUFF_TRAVERSE(ALIGNED) @@ -1718,12 +1261,7 @@ namespace LibMSPackSharp.Compression (aligned_bits) = sym; i = lzx.ALIGNED_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzx.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } match_offset += (uint)aligned_bits; @@ -1740,44 +1278,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - sym = lzx.ALIGNED_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (LZX_ALIGNED_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(ALIGNED)) + sym = lzx.ALIGNED_table[lzx.PEEK_BITS_MSB(LZX_ALIGNED_TABLEBITS, bit_buffer)]; if (sym >= LZX_ALIGNED_MAXSYMBOLS) { //HUFF_TRAVERSE(ALIGNED) @@ -1795,12 +1312,7 @@ namespace LibMSPackSharp.Compression (aligned_bits) = sym; i = lzx.ALIGNED_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzx.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } match_offset += (uint)aligned_bits; @@ -1817,50 +1329,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (verbatim_bits) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (extra))); //PEEK_BITS(extra) - - //REMOVE_BITS(extra) - { - bit_buffer <<= (extra); - bits_left -= (extra); - } + (verbatim_bits) = (int)lzx.PEEK_BITS_MSB(extra, bit_buffer); + lzx.REMOVE_BITS_MSB(extra, ref bit_buffer, ref bits_left); } match_offset += (uint)verbatim_bits; @@ -1891,51 +1377,26 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } // '0' . 8 extra length bits - if ((bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1))) == 0) //PEEK_BITS(1) + if (lzx.PEEK_BITS_MSB(1, bit_buffer) == 0) { - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + lzx.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); //READ_BITS(extra_len, 8) { @@ -1945,61 +1406,31 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (extra_len) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (8))); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer <<= (8); - bits_left -= (8); - } + (extra_len) = (int)lzx.PEEK_BITS_MSB(8, bit_buffer); + lzx.REMOVE_BITS_MSB(8, ref bit_buffer, ref bits_left); } } // '10' . 10 extra length bits + 0x100 - else if ((bit_buffer >> (CompressionStream.BITBUF_WIDTH - (2))) == 2) //PEEK_BITS(2) + else if (lzx.PEEK_BITS_MSB(2, bit_buffer) == 2) { - //REMOVE_BITS(2) - { - bit_buffer <<= (2); - bits_left -= (2); - } + lzx.REMOVE_BITS_MSB(2, ref bit_buffer, ref bits_left); //READ_BITS(extra_len, 10) { @@ -2009,63 +1440,33 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (extra_len) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (10))); //PEEK_BITS(10) - - //REMOVE_BITS(10) - { - bit_buffer <<= (10); - bits_left -= (10); - } + (extra_len) = (int)lzx.PEEK_BITS_MSB(10, bit_buffer); + lzx.REMOVE_BITS_MSB(10, ref bit_buffer, ref bits_left); } extra_len += 0x100; } // '110' . 12 extra length bits + 0x500 - else if ((bit_buffer >> (CompressionStream.BITBUF_WIDTH - (3))) == 6) //PEEK_BITS(3) + else if (lzx.PEEK_BITS_MSB(3, bit_buffer) == 6) { - //REMOVE_BITS(3) - { - bit_buffer <<= (3); - bits_left -= (3); - } + lzx.REMOVE_BITS_MSB(3, ref bit_buffer, ref bits_left); //READ_BITS(extra_len, 12) { @@ -2075,50 +1476,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (extra_len) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (12))); //PEEK_BITS(12) - - //REMOVE_BITS(12) - { - bit_buffer <<= (12); - bits_left -= (12); - } + (extra_len) = (int)lzx.PEEK_BITS_MSB(12, bit_buffer); + lzx.REMOVE_BITS_MSB(12, ref bit_buffer, ref bits_left); } extra_len += 0x500; @@ -2127,11 +1502,7 @@ namespace LibMSPackSharp.Compression // '111' . 15 extra length bits else { - //REMOVE_BITS(3) - { - bit_buffer <<= (3); - bits_left -= (3); - } + lzx.REMOVE_BITS_MSB(3, ref bit_buffer, ref bits_left); //READ_BITS(extra_len, 15) { @@ -2141,50 +1512,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (extra_len) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (15))); //PEEK_BITS(15) - - //REMOVE_BITS(15) - { - bit_buffer <<= (15); - bits_left -= (15); - } + (extra_len) = (int)lzx.PEEK_BITS_MSB(15, bit_buffer); + lzx.REMOVE_BITS_MSB(15, ref bit_buffer, ref bits_left); } } @@ -2261,17 +1606,9 @@ namespace LibMSPackSharp.Compression { if ((i = i_end - i_ptr) == 0) { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; } else { @@ -2320,52 +1657,25 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } } if ((bits_left & 15) != 0) - { - //REMOVE_BITS(bits_left & 15) - { - bit_buffer <<= (bits_left & 15); - bits_left -= (bits_left & 15); - } - } + lzx.REMOVE_BITS_MSB(bits_left & 15, ref bit_buffer, ref bits_left); // Check that we've used all of the previous frame first if (lzx.OutputPointer != lzx.OutputEnd) @@ -2450,15 +1760,7 @@ namespace LibMSPackSharp.Compression } // Store local state - - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } - + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); lzx.WindowPosition = window_posn; lzx.R0 = R0; lzx.R1 = R1; @@ -2478,13 +1780,7 @@ namespace LibMSPackSharp.Compression uint x, y; int z; - //RESTORE_BITS - { - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - bit_buffer = lzx.BitBuffer; - bits_left = lzx.BitsLeft; - } + lzx.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); // Read lengths for pretree (20 symbols, lengths stored in fixed 4 bits) for (x = 0; x < 20; x++) @@ -2497,50 +1793,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (y) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (y) = (uint)lzx.PEEK_BITS_MSB(4, bit_buffer); + lzx.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } lzx.PRETREE_len[x] = (byte)y; @@ -2565,44 +1835,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - sym = lzx.PRETREE_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (LZX_PRETREE_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(PRETREE)) + sym = lzx.PRETREE_table[lzx.PEEK_BITS_MSB(LZX_PRETREE_TABLEBITS, bit_buffer)]; if (sym >= LZX_PRETREE_MAXSYMBOLS) { //HUFF_TRAVERSE(PRETREE) @@ -2620,12 +1869,7 @@ namespace LibMSPackSharp.Compression (z) = sym; i = lzx.PRETREE_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzx.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } // Code = 17, run of ([read 4 bits]+4) zeros @@ -2639,50 +1883,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (y) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (4))); //PEEK_BITS(nbits) - - //REMOVE_BITS(4) - { - bit_buffer <<= (4); - bits_left -= (4); - } + (y) = (uint)lzx.PEEK_BITS_MSB(4, bit_buffer); + lzx.REMOVE_BITS_MSB(4, ref bit_buffer, ref bits_left); } y += 4; @@ -2703,50 +1921,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (y) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (5))); //PEEK_BITS(5) - - //REMOVE_BITS(5) - { - bit_buffer <<= (5); - bits_left -= (5); - } + (y) = (uint)lzx.PEEK_BITS_MSB(5, bit_buffer); + lzx.REMOVE_BITS_MSB(5, ref bit_buffer, ref bits_left); } y += 20; @@ -2767,50 +1959,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - (y) = (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + (y) = (uint)lzx.PEEK_BITS_MSB(1, bit_buffer); + lzx.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } y += 4; @@ -2823,44 +1989,23 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b0 = lzx.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) - return lzx.Error; - - i_ptr = lzx.InputPointer; - i_end = lzx.InputEnd; - } - } + lzx.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (lzx.Error != Error.MSPACK_ERR_OK) + return lzx.Error; byte b1 = lzx.InputBuffer[i_ptr++]; - - //INJECT_BITS((b1 << 8) | b0, 16) - { - bit_buffer |= (uint)((b1 << 8) | b0) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + lzx.INJECT_BITS_MSB((b1 << 8) | b0, 16, ref bit_buffer, ref bits_left); } } } - sym = lzx.PRETREE_table[(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (LZX_PRETREE_TABLEBITS)))]; //PEEK_BITS(TABLEBITS(PRETREE)) + sym = lzx.PRETREE_table[lzx.PEEK_BITS_MSB(LZX_PRETREE_TABLEBITS, bit_buffer)]; if (sym >= LZX_PRETREE_MAXSYMBOLS) { //HUFF_TRAVERSE(PRETREE) @@ -2878,12 +2023,7 @@ namespace LibMSPackSharp.Compression (z) = sym; i = lzx.PRETREE_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer <<= (i); - bits_left -= (i); - } + lzx.REMOVE_BITS_MSB(i, ref bit_buffer, ref bits_left); } z = lens[x] - z; @@ -2907,13 +2047,7 @@ namespace LibMSPackSharp.Compression } } - //STORE_BITS - { - lzx.InputPointer = i_ptr; - lzx.InputEnd = i_end; - lzx.BitBuffer = bit_buffer; - lzx.BitsLeft = bits_left; - } + lzx.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); return Error.MSPACK_ERR_OK; } diff --git a/BurnOutSharp/External/libmspack/Compression/MSZIP.cs b/BurnOutSharp/External/libmspack/Compression/MSZIP.cs index 1e3a4b7f..a5ceb08f 100644 --- a/BurnOutSharp/External/libmspack/Compression/MSZIP.cs +++ b/BurnOutSharp/External/libmspack/Compression/MSZIP.cs @@ -189,25 +189,13 @@ namespace LibMSPackSharp.Compression while (out_bytes > 0) { // Unpack another block - - //RESTORE_BITS - { - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; - } + zip.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); // Skip to next read 'CK' header i = bits_left & 7; // Align to bytestream - - //REMOVE_BITS(i) - { - bit_buffer >>= (i); - bits_left -= (i); - } + zip.REMOVE_BITS_LSB(i, ref bit_buffer, ref bits_left); state = 0; do @@ -220,34 +208,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer >>= (8); - bits_left -= (8); - } + (i) = (int)zip.PEEK_BITS_LSB(8, bit_buffer); + zip.REMOVE_BITS_LSB(8, ref bit_buffer, ref bits_left); } if (i == 'C') @@ -262,13 +233,7 @@ namespace LibMSPackSharp.Compression zip.WindowPosition = 0; zip.BytesOutput = 0; - //STORE_BITS - { - zip.InputPointer = i_ptr; - zip.InputEnd = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - } + zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if ((error = Inflate(zip)) != Error.MSPACK_ERR_OK) { @@ -335,22 +300,11 @@ namespace LibMSPackSharp.Compression // Unpack blocks until block_len == 0 for (; ; ) { - //RESTORE_BITS - { - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; - } + zip.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); // Align to bytestream, read block_len i = bits_left & 7; - - //REMOVE_BITS(i) - { - bit_buffer >>= (i); - bits_left -= (i); - } + zip.REMOVE_BITS_LSB(i, ref bit_buffer, ref bits_left); //READ_BITS(block_len, 8) { @@ -360,34 +314,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (block_len) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer >>= (8); - bits_left -= (8); - } + (block_len) = (int)zip.PEEK_BITS_LSB(8, bit_buffer); + zip.REMOVE_BITS_LSB(8, ref bit_buffer, ref bits_left); } //READ_BITS(i, 8) @@ -398,34 +335,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer >>= (8); - bits_left -= (8); - } + (i) = (int)zip.PEEK_BITS_LSB(8, bit_buffer); + zip.REMOVE_BITS_LSB(8, ref bit_buffer, ref bits_left); } block_len |= i << 8; @@ -443,34 +363,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer >>= (8); - bits_left -= (8); - } + (i) = (int)zip.PEEK_BITS_LSB(8, bit_buffer); + zip.REMOVE_BITS_LSB(8, ref bit_buffer, ref bits_left); } if (i != 'C') @@ -484,34 +387,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - - //REMOVE_BITS(nbits) - { - bit_buffer >>= (8); - bits_left -= (8); - } + (i) = (int)zip.PEEK_BITS_LSB(8, bit_buffer); + zip.REMOVE_BITS_LSB(8, ref bit_buffer, ref bits_left); } if (i != 'K') @@ -521,13 +407,7 @@ namespace LibMSPackSharp.Compression zip.WindowPosition = 0; zip.BytesOutput = 0; - //STORE_BITS - { - zip.InputPointer = i_ptr; - zip.InputEnd = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - } + zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if ((error = Inflate(zip)) != Error.MSPACK_ERR_OK) { @@ -545,11 +425,6 @@ namespace LibMSPackSharp.Compression private static Error ReadLens(MSZIPDStream zip) { - // For the bit buffer and huffman decoding - uint bit_buffer; - int bits_left; - int i_ptr, i_end; - // bitlen Huffman codes -- immediate lookup, 7 bit max code length ushort[] bl_table = new ushort[(1 << 7)]; byte[] bl_len = new byte[19]; @@ -557,13 +432,7 @@ namespace LibMSPackSharp.Compression byte[] lens = new byte[MSZIP_LITERAL_MAXSYMBOLS + MSZIP_DISTANCE_MAXSYMBOLS]; uint lit_codes, dist_codes, code, last_code = 0, bitlen_codes, i, run; - //RESTORE_BITS - { - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; - } + zip.RESTORE_BITS(out int i_ptr, out int i_end, out uint bit_buffer, out int bits_left); // Read the number of codes @@ -575,34 +444,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (lit_codes) = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5) - - //REMOVE_BITS(5) - { - bit_buffer >>= (5); - bits_left -= (5); - } + (lit_codes) = (uint)zip.PEEK_BITS_LSB(5, bit_buffer); + zip.REMOVE_BITS_LSB(5, ref bit_buffer, ref bits_left); } lit_codes += 257; @@ -615,34 +467,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (dist_codes) = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5) - - //REMOVE_BITS(5) - { - bit_buffer >>= (5); - bits_left -= (5); - } + (dist_codes) = (uint)zip.PEEK_BITS_LSB(5, bit_buffer); + zip.REMOVE_BITS_LSB(5, ref bit_buffer, ref bits_left); } dist_codes += 1; @@ -655,34 +490,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (bitlen_codes) = (bit_buffer & ((1 << (4)) - 1)); //PEEK_BITS(4) - - //REMOVE_BITS(4) - { - bit_buffer >>= (4); - bits_left -= (4); - } + (bitlen_codes) = (uint)zip.PEEK_BITS_LSB(4, bit_buffer); + zip.REMOVE_BITS_LSB(4, ref bit_buffer, ref bits_left); } bitlen_codes += 4; @@ -702,34 +520,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (bl_len[bitlen_order[i]]) = (byte)(bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3) - - //REMOVE_BITS(3) - { - bit_buffer >>= (3); - bits_left -= (3); - } + (bl_len[bitlen_order[i]]) = (byte)zip.PEEK_BITS_LSB(3, bit_buffer); + zip.REMOVE_BITS_LSB(3, ref bit_buffer, ref bits_left); } } @@ -753,34 +554,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - code = bl_table[(bit_buffer & ((1 << (7)) - 1))]; //PEEK_BITS(7) - - //REMOVE_BITS(bl_len[code]) - { - bit_buffer >>= (bl_len[code]); - bits_left -= (bl_len[code]); - } + code = bl_table[zip.PEEK_BITS_LSB(7, bit_buffer)]; + zip.REMOVE_BITS_LSB(bl_len[code], ref bit_buffer, ref bits_left); if (code < 16) { @@ -799,34 +583,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (run) = (bit_buffer & ((1 << (2)) - 1)); //PEEK_BITS(2) - - //REMOVE_BITS(2) - { - bit_buffer >>= (2); - bits_left -= (2); - } + (run) = (uint)zip.PEEK_BITS_LSB(2, bit_buffer); + zip.REMOVE_BITS_LSB(2, ref bit_buffer, ref bits_left); } run += 3; @@ -842,34 +609,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (run) = (bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3) - - //REMOVE_BITS(3) - { - bit_buffer >>= (3); - bits_left -= (3); - } + (run) = (uint)zip.PEEK_BITS_LSB(3, bit_buffer); + zip.REMOVE_BITS_LSB(3, ref bit_buffer, ref bits_left); } run += 3; @@ -885,34 +635,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (run) = (bit_buffer & ((1 << (7)) - 1)); //PEEK_BITS(7) - - //REMOVE_BITS(7) - { - bit_buffer >>= (7); - bits_left -= (7); - } + (run) = (uint)zip.PEEK_BITS_LSB(7, bit_buffer); + zip.REMOVE_BITS_LSB(7, ref bit_buffer, ref bits_left); } run += 11; @@ -951,13 +684,7 @@ namespace LibMSPackSharp.Compression zip.DISTANCE_len[i++] = 0; } - //STORE_BITS - { - zip.InputPointer = i_ptr; - zip.InputEnd = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - } + zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); return 0; } @@ -969,20 +696,9 @@ namespace LibMSPackSharp.Compression { uint last_block, block_type, distance, length, this_run, i; Error err; - - // For the bit buffer and huffman decoding - uint bit_buffer; - int bits_left; ushort sym; - int i_ptr, i_end; - //RESTORE_BITS - { - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; - } + zip.RESTORE_BITS(out int i_ptr, out int i_end, out uint bit_buffer, out int bits_left); do { @@ -996,34 +712,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (last_block) = (bit_buffer & ((1 << (1)) - 1)); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer >>= (1); - bits_left -= (1); - } + (last_block) = (uint)zip.PEEK_BITS_LSB(1, bit_buffer); + zip.REMOVE_BITS_LSB(1, ref bit_buffer, ref bits_left); } // Read in block type @@ -1036,34 +735,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (block_type) = (bit_buffer & ((1 << (2)) - 1)); //PEEK_BITS(2) - - //REMOVE_BITS(2) - { - bit_buffer >>= (2); - bits_left -= (2); - } + (block_type) = (uint)zip.PEEK_BITS_LSB(2, bit_buffer); + zip.REMOVE_BITS_LSB(2, ref bit_buffer, ref bits_left); } if (block_type == 0) @@ -1073,12 +755,7 @@ namespace LibMSPackSharp.Compression // Go to byte boundary i = (uint)(bits_left & 7); - - //REMOVE_BITS(i) - { - bit_buffer >>= (int)(i); - bits_left -= (int)(i); - } + zip.REMOVE_BITS_LSB((int)i, ref bit_buffer, ref bits_left); // Read 4 bytes of data, emptying the bit-buffer if necessary for (i = 0; (bits_left >= 8); i++) @@ -1086,13 +763,8 @@ namespace LibMSPackSharp.Compression if (i == 4) return Error.INF_ERR_BITBUF; - lens_buf[i] = (byte)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer >>= (8); - bits_left -= (8); - } + lens_buf[i] = (byte)zip.PEEK_BITS_LSB(8, bit_buffer); + zip.REMOVE_BITS_LSB(8, ref bit_buffer, ref bits_left); } if (bits_left != 0) @@ -1100,17 +772,9 @@ namespace LibMSPackSharp.Compression while (i < 4) { - //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.InputEnd; - } - } + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; lens_buf[i++] = zip.InputBuffer[i_ptr++]; } @@ -1126,17 +790,9 @@ namespace LibMSPackSharp.Compression // Read and copy the uncompressed data into the window while (length > 0) { - //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.InputEnd; - } - } + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; this_run = length; if (this_run > (uint)(i_end - i_ptr)) @@ -1200,25 +856,12 @@ namespace LibMSPackSharp.Compression else { // Block with dynamic Huffman codes - - //STORE_BITS - { - zip.InputPointer = i_ptr; - zip.InputEnd = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - } + zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); if ((err = ReadLens(zip)) != Error.MSPACK_ERR_OK) return err; - //RESTORE_BITS - { - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; - } + zip.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); } // Now huffman lengths are read for either kind of block, @@ -1240,28 +883,16 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = zip.LITERAL_table[(bit_buffer & ((1 << (MSZIP_LITERAL_TABLEBITS)) - 1))]; //PEEK_BITS(TABLEBITS(LITERAL)) + sym = zip.LITERAL_table[zip.PEEK_BITS_LSB(MSZIP_LITERAL_TABLEBITS, bit_buffer)]; if (sym >= MSZIP_LITERAL_MAXSYMBOLS) { //HUFF_TRAVERSE(LITERAL) @@ -1279,12 +910,7 @@ namespace LibMSPackSharp.Compression (code) = sym; i = zip.LITERAL_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer >>= (int)(i); - bits_left -= (int)(i); - } + zip.REMOVE_BITS_LSB((int)i, ref bit_buffer, ref bits_left); } if (code < 256) @@ -1321,34 +947,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (length) = (bit_buffer & lsb_bit_mask[(lit_extrabits[code])]); //PEEK_BITS_T(lit_extrabits[code]) - - //REMOVE_BITS(lit_extrabits[code]) - { - bit_buffer >>= (lit_extrabits[code]); - bits_left -= (lit_extrabits[code]); - } + (length) = (uint)zip.PEEK_BITS_T_LSB(lit_extrabits[code], bit_buffer); + zip.REMOVE_BITS_LSB(lit_extrabits[code], ref bit_buffer, ref bits_left); } length += lit_lengths[code]; @@ -1361,28 +970,16 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - sym = zip.DISTANCE_table[(bit_buffer & ((1 << (MSZIP_DISTANCE_TABLEBITS)) - 1))]; //PEEK_BITS(TABLEBITS(DISTANCE)) + sym = zip.DISTANCE_table[zip.PEEK_BITS_LSB(MSZIP_DISTANCE_TABLEBITS, bit_buffer)]; if (sym >= MSZIP_DISTANCE_MAXSYMBOLS) { //HUFF_TRAVERSE(DISTANCE) @@ -1400,12 +997,7 @@ namespace LibMSPackSharp.Compression (code) = sym; i = zip.DISTANCE_len[sym]; - - //REMOVE_BITS(i) - { - bit_buffer >>= (int)(i); - bits_left -= (int)(i); - } + zip.REMOVE_BITS_LSB((int)i, ref bit_buffer, ref bits_left); } if (code >= 30) @@ -1419,34 +1011,17 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + zip.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (zip.Error != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputEnd; - } - } - - //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) - { - bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); - bits_left += (8); - } + zip.INJECT_BITS_LSB(zip.InputBuffer[i_ptr++], 8, ref bit_buffer, ref bits_left); } } } - (distance) = (bit_buffer & lsb_bit_mask[(dist_extrabits[code])]); //PEEK_BITS_T(dist_extrabits[code]) - - //REMOVE_BITS(dist_extrabits[code]) - { - bit_buffer >>= (dist_extrabits[code]); - bits_left -= (dist_extrabits[code]); - } + (distance) = (uint)zip.PEEK_BITS_T_LSB(dist_extrabits[code], bit_buffer); + zip.REMOVE_BITS_LSB(dist_extrabits[code], ref bit_buffer, ref bits_left); } distance += dist_offsets[code]; @@ -1533,13 +1108,7 @@ namespace LibMSPackSharp.Compression return Error.INF_ERR_FLUSH; } - //STORE_BITS - { - zip.InputPointer = i_ptr; - zip.InputEnd = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - } + zip.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); // Return success return Error.MSPACK_ERR_OK; diff --git a/BurnOutSharp/External/libmspack/Compression/QTM.cs b/BurnOutSharp/External/libmspack/Compression/QTM.cs index 36808b9f..08036b98 100644 --- a/BurnOutSharp/External/libmspack/Compression/QTM.cs +++ b/BurnOutSharp/External/libmspack/Compression/QTM.cs @@ -214,15 +214,7 @@ namespace LibMSPackSharp.Compression return Error.MSPACK_ERR_OK; // Restore local state - - //RESTORE_BITS - { - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - bit_buffer = qtm.BitBuffer; - bits_left = qtm.BitsLeft; - } - + qtm.RESTORE_BITS(out i_ptr, out i_end, out bit_buffer, out bits_left); window = qtm.Window; window_posn = qtm.WindowPosition; frame_todo = qtm.FrameTODO; @@ -247,50 +239,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - (C) = (ushort)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (16))); //PEEK_BITS(16) - - //REMOVE_BITS(16) - { - bit_buffer <<= (16); - bits_left -= (16); - } + (C) = (ushort)qtm.PEEK_BITS_MSB(16, bit_buffer); + qtm.REMOVE_BITS_MSB(16, ref bit_buffer, ref bits_left); } qtm.HeaderRead = 1; @@ -358,50 +324,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - C = (ushort)((C << 1) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + C = (ushort)((C << 1) | (qtm.PEEK_BITS_MSB(1, bit_buffer))); + qtm.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } } @@ -481,50 +421,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - C = (ushort)((C << 1) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + C = (ushort)((C << 1) | (qtm.PEEK_BITS_MSB(1, bit_buffer))); + qtm.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } } @@ -590,50 +504,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - C = (ushort)((C << 1) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + C = (ushort)((C << 1) | (qtm.PEEK_BITS_MSB(1, bit_buffer))); + qtm.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } } @@ -647,51 +535,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } bitrun = (byte)((bits_left < needed) ? bits_left : needed); - (extra) = (int)(((extra) << bitrun) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (bitrun)))); //PEEK_BITS(bitrun) - - //REMOVE_BITS(bitrun) - { - bit_buffer <<= (bitrun); - bits_left -= (bitrun); - } - + (extra) = (int)(((extra) << bitrun) | (qtm.PEEK_BITS_MSB(bitrun, bit_buffer))); + qtm.REMOVE_BITS_MSB(bitrun, ref bit_buffer, ref bits_left); needed -= bitrun; } } @@ -754,50 +615,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - C = (ushort)((C << 1) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + C = (ushort)((C << 1) | (qtm.PEEK_BITS_MSB(1, bit_buffer))); + qtm.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } } @@ -811,51 +646,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } bitrun = (byte)((bits_left < needed) ? bits_left : needed); - (extra) = (int)(((extra) << bitrun) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (bitrun)))); //PEEK_BITS(bitrun) - - //REMOVE_BITS(bitrun) - { - bit_buffer <<= (bitrun); - bits_left -= (bitrun); - } - + (extra) = (int)(((extra) << bitrun) | (qtm.PEEK_BITS_MSB(bitrun, bit_buffer))); + qtm.REMOVE_BITS_MSB(bitrun, ref bit_buffer, ref bits_left); needed -= bitrun; } } @@ -918,50 +726,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - C = (ushort)((C << 1) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + C = (ushort)((C << 1) | (qtm.PEEK_BITS_MSB(1, bit_buffer))); + qtm.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } } @@ -975,51 +757,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } bitrun = (byte)((bits_left < needed) ? bits_left : needed); - (extra) = (int)(((extra) << bitrun) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (bitrun)))); //PEEK_BITS(bitrun) - - //REMOVE_BITS(bitrun) - { - bit_buffer <<= (bitrun); - bits_left -= (bitrun); - } - + (extra) = (int)(((extra) << bitrun) | (qtm.PEEK_BITS_MSB(bitrun, bit_buffer))); + qtm.REMOVE_BITS_MSB(bitrun, ref bit_buffer, ref bits_left); needed -= bitrun; } } @@ -1078,50 +833,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - C = (ushort)((C << 1) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) - - //REMOVE_BITS(1) - { - bit_buffer <<= (1); - bits_left -= (1); - } + C = (ushort)((C << 1) | (qtm.PEEK_BITS_MSB(1, bit_buffer))); + qtm.REMOVE_BITS_MSB(1, ref bit_buffer, ref bits_left); } } @@ -1135,51 +864,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } bitrun = (byte)((bits_left < needed) ? bits_left : needed); - (extra) = (int)(((extra) << bitrun) | (bit_buffer >> (CompressionStream.BITBUF_WIDTH - (bitrun)))); //PEEK_BITS(bitrun) - - //REMOVE_BITS(bitrun) - { - bit_buffer <<= (bitrun); - bits_left -= (bitrun); - } - + (extra) = (int)(((extra) << bitrun) | (qtm.PEEK_BITS_MSB(bitrun, bit_buffer))); + qtm.REMOVE_BITS_MSB(bitrun, ref bit_buffer, ref bits_left); needed -= bitrun; } } @@ -1305,13 +1007,7 @@ namespace LibMSPackSharp.Compression { // Re-align input if ((bits_left & 7) != 0) - { - //REMOVE_BITS(bits_left & 7) - { - bit_buffer <<= (bits_left & 7); - bits_left -= (bits_left & 7); - } - } + qtm.REMOVE_BITS_MSB(bits_left & 7, ref bit_buffer, ref bits_left); // Special Quantum hack -- cabd.c injects a trailer byte to allow the // decompressor to realign itself. CAB Quantum blocks, unlike LZX @@ -1326,50 +1022,24 @@ namespace LibMSPackSharp.Compression { //READ_BYTES { - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b0 = qtm.InputBuffer[i_ptr++]; - //READ_IF_NEEDED - { - if (i_ptr >= i_end) - { - if (qtm.ReadInput() != Error.MSPACK_ERR_OK) - return qtm.Error; - - i_ptr = qtm.InputPointer; - i_end = qtm.InputEnd; - } - } + qtm.READ_IF_NEEDED(ref i_ptr, ref i_end); + if (qtm.Error != Error.MSPACK_ERR_OK) + return qtm.Error; byte b1 = qtm.InputBuffer[i_ptr++]; - - //INJECT_BITS(bitdata, 16) - { - bit_buffer |= (uint)((b0 << 8) | b1) << (CompressionStream.BITBUF_WIDTH - (16) - bits_left); - bits_left += (16); - } + qtm.INJECT_BITS_MSB((b0 << 8) | b1, 16, ref bit_buffer, ref bits_left); } } } - (i) = (int)(bit_buffer >> (CompressionStream.BITBUF_WIDTH - (8))); //PEEK_BITS(8) - - //REMOVE_BITS(8) - { - bit_buffer <<= (8); - bits_left -= (8); - } + (i) = (int)qtm.PEEK_BITS_MSB(8, bit_buffer); + qtm.REMOVE_BITS_MSB(8, ref bit_buffer, ref bits_left); } } while (i != 0xFF); @@ -1410,15 +1080,7 @@ namespace LibMSPackSharp.Compression } // Store local state - - //STORE_BITS - { - qtm.InputPointer = i_ptr; - qtm.InputEnd = i_end; - qtm.BitBuffer = bit_buffer; - qtm.BitsLeft = bits_left; - } - + qtm.STORE_BITS(i_ptr, i_end, bit_buffer, bits_left); qtm.WindowPosition = window_posn; qtm.FrameTODO = frame_todo; qtm.High = H;