diff --git a/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs b/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs index fff3ad89..7d922e98 100644 --- a/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs +++ b/BurnOutSharp/External/libmspack/Compression/CompressionStream.cs @@ -36,7 +36,7 @@ namespace LibMSPackSharp.Compression /// public FileStream Output { get; set; } - public Error Error { get; set; } + public LibMSPackSharp.Error Error { get; set; } #region I/O buffering @@ -63,6 +63,7 @@ namespace LibMSPackSharp.Compression #endregion + // TODO: These should be in a separate file #region ReadBits Methods /* This header defines macros that read data streams by @@ -167,11 +168,11 @@ namespace LibMSPackSharp.Compression bits_left -= (nbits); } - public Error ReadInput() + public LibMSPackSharp.Error ReadInput() { int read = Sys.Read(Input, InputBuffer, 0, (int)InputBufferSize); if (read < 0) - return Error = Error.MSPACK_ERR_READ; + return Error = LibMSPackSharp.Error.MSPACK_ERR_READ; // We might overrun the input stream by asking for bits we don't use, // so fake 2 more bytes at the end of input @@ -180,7 +181,7 @@ namespace LibMSPackSharp.Compression if (InputEnd != 0) { Console.WriteLine("out of input bytes"); - return Error = Error.MSPACK_ERR_READ; + return Error = LibMSPackSharp.Error.MSPACK_ERR_READ; } else { @@ -193,11 +194,12 @@ namespace LibMSPackSharp.Compression // Update i_ptr and i_end InputPointer = 0; InputLength = read; - return Error = Error.MSPACK_ERR_OK; + return Error = LibMSPackSharp.Error.MSPACK_ERR_OK; } #endregion + // TODO: These should be in a separate file #region ReadHuff Methods private const int HUFF_MAXBITS = 16; @@ -214,7 +216,7 @@ namespace LibMSPackSharp.Compression READ_BYTES; } - if (Error != Error.MSPACK_ERR_OK) + if (Error != LibMSPackSharp.Error.MSPACK_ERR_OK) return (int)Error; int peek; @@ -246,7 +248,7 @@ namespace LibMSPackSharp.Compression bits_left -= i; } - return (int)Error.MSPACK_ERR_OK; + return (int)LibMSPackSharp.Error.MSPACK_ERR_OK; } public int HUFF_TRAVERSE(ushort[] decodingTable, int tablebits, int maxsymbols, ref int i, ref ushort sym, uint bit_buffer, bool msb) @@ -274,7 +276,7 @@ namespace LibMSPackSharp.Compression } while (sym >= maxsymbols); } - return (int)Error.MSPACK_ERR_OK; + return (int)LibMSPackSharp.Error.MSPACK_ERR_OK; } public abstract int HUFF_ERROR(); diff --git a/BurnOutSharp/External/libmspack/Compression/Enums.cs b/BurnOutSharp/External/libmspack/Compression/Enums.cs index 5f6a6b56..5d6f57d0 100644 --- a/BurnOutSharp/External/libmspack/Compression/Enums.cs +++ b/BurnOutSharp/External/libmspack/Compression/Enums.cs @@ -9,81 +9,6 @@ namespace LibMSPackSharp.Compression { - public enum InflateErrorCode - { - INF_ERR_OK = 0, - - /// - /// Unknown block type - /// - INF_ERR_BLOCKTYPE = -1, - - /// - /// Block size complement mismatch - /// - INF_ERR_COMPLEMENT = -2, - - /// - /// Error from flush_window callback - /// - INF_ERR_FLUSH = -3, - - /// - /// Too many bits in bit buffer - /// - INF_ERR_BITBUF = -4, - - /// - /// Too many symbols in blocktype 2 header - /// - INF_ERR_SYMLENS = -5, - - /// - /// Failed to build bitlens huffman table - /// - INF_ERR_BITLENTBL = -6, - - /// - /// Failed to build literals huffman table - /// - INF_ERR_LITERALTBL = -7, - - /// - /// Failed to build distance huffman table - /// - INF_ERR_DISTANCETBL = -8, - - /// - /// Bitlen RLE code goes over table size - /// - INF_ERR_BITOVERRUN = -9, - - /// - /// Invalid bit-length code - /// - INF_ERR_BADBITLEN = -10, - - /// - /// Out-of-range literal code - /// - INF_ERR_LITCODE = -11, - - /// - /// Out-of-range distance code - /// - INF_ERR_DISTCODE = -12, - - /// - /// Somehow, distance is beyond 32k - /// - INF_ERR_DISTANCE = -13, - - /// - /// Out of bits decoding huffman symbol - /// - INF_ERR_HUFFSYM = -14, - } - public enum LZSSMode { LZSS_MODE_EXPAND = 0, diff --git a/BurnOutSharp/External/libmspack/Compression/LZSS.cs b/BurnOutSharp/External/libmspack/Compression/LZSS.cs index 082d6075..66def40b 100644 --- a/BurnOutSharp/External/libmspack/Compression/LZSS.cs +++ b/BurnOutSharp/External/libmspack/Compression/LZSS.cs @@ -56,11 +56,11 @@ namespace LibMSPackSharp.Compression /// /// one of LZSSMode values /// an error code, or MSPACK_ERR_OK if successful - public static Error Decompress(SystemImpl system, FileStream input, FileStream output, int input_buffer_size, LZSSMode mode) + public static LibMSPackSharp.Error Decompress(SystemImpl system, FileStream input, FileStream output, int input_buffer_size, LZSSMode mode) { // Check parameters if (system == null || input_buffer_size < 1 || (mode != LZSSMode.LZSS_MODE_EXPAND && mode != LZSSMode.LZSS_MODE_MSHELP && mode != LZSSMode.LZSS_MODE_QBASIC)) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; // Allocate memory byte[] window = new byte[LZSS_WINDOW_SIZE + input_buffer_size]; @@ -85,7 +85,7 @@ namespace LibMSPackSharp.Compression { read = input.Read(window, inbuf, input_buffer_size); if (read <= 0) - return (read < 0) ? Error.MSPACK_ERR_READ : Error.MSPACK_ERR_OK; + return (read < 0) ? LibMSPackSharp.Error.MSPACK_ERR_READ : LibMSPackSharp.Error.MSPACK_ERR_OK; i_ptr = inbuf; i_end = read; @@ -104,7 +104,7 @@ namespace LibMSPackSharp.Compression { read = input.Read(window, inbuf, input_buffer_size); if (read <= 0) - return (read < 0) ? Error.MSPACK_ERR_READ : Error.MSPACK_ERR_OK; + return (read < 0) ? LibMSPackSharp.Error.MSPACK_ERR_READ : LibMSPackSharp.Error.MSPACK_ERR_OK; i_ptr = inbuf; i_end = read; @@ -116,7 +116,7 @@ namespace LibMSPackSharp.Compression //WRITE_BYTE { try { output.Write(window, (int)pos, 1); } - catch { return Error.MSPACK_ERR_WRITE; } + catch { return LibMSPackSharp.Error.MSPACK_ERR_WRITE; } } pos++; @@ -132,7 +132,7 @@ namespace LibMSPackSharp.Compression { read = input.Read(window, inbuf, input_buffer_size); if (read <= 0) - return (read < 0) ? Error.MSPACK_ERR_READ : Error.MSPACK_ERR_OK; + return (read < 0) ? LibMSPackSharp.Error.MSPACK_ERR_READ : LibMSPackSharp.Error.MSPACK_ERR_OK; i_ptr = inbuf; i_end = read; @@ -147,7 +147,7 @@ namespace LibMSPackSharp.Compression { read = input.Read(window, inbuf, input_buffer_size); if (read <= 0) - return (read < 0) ? Error.MSPACK_ERR_READ : Error.MSPACK_ERR_OK; + return (read < 0) ? LibMSPackSharp.Error.MSPACK_ERR_READ : LibMSPackSharp.Error.MSPACK_ERR_OK; i_ptr = inbuf; i_end = read; @@ -164,7 +164,7 @@ namespace LibMSPackSharp.Compression //WRITE_BYTE { try { output.Write(window, (int)pos, 1); } - catch { return Error.MSPACK_ERR_WRITE; } + catch { return LibMSPackSharp.Error.MSPACK_ERR_WRITE; } } pos++; @@ -177,7 +177,7 @@ namespace LibMSPackSharp.Compression } /* not reached */ - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; } } } diff --git a/BurnOutSharp/External/libmspack/Compression/LZX.cs b/BurnOutSharp/External/libmspack/Compression/LZX.cs index 05e54e96..b9771f9b 100644 --- a/BurnOutSharp/External/libmspack/Compression/LZX.cs +++ b/BurnOutSharp/External/libmspack/Compression/LZX.cs @@ -322,7 +322,7 @@ namespace LibMSPackSharp.Compression ResetInterval = (uint)reset_interval, IntelFileSize = 0, IntelStarted = false, - Error = Error.MSPACK_ERR_OK, + Error = LibMSPackSharp.Error.MSPACK_ERR_OK, NumOffsets = position_slots[window_bits - 15] << 3, IsDelta = is_delta, @@ -362,33 +362,33 @@ namespace LibMSPackSharp.Compression /// than the LZX window size. /// /// an error code, or MSPACK_ERR_OK if successful - public static Error SetReferenceData(LZXDStream lzx, SystemImpl system, FileStream input, uint length) + public static LibMSPackSharp.Error SetReferenceData(LZXDStream lzx, SystemImpl system, FileStream input, uint length) { if (lzx == null) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; if (!lzx.IsDelta) { Console.WriteLine("Only LZX DELTA streams support reference data"); - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; } if (lzx.Offset != 0) { Console.WriteLine("Too late to set reference data after decoding starts"); - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; } if (length > lzx.WindowSize) { Console.WriteLine($"Reference length ({length}) is longer than the window"); - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; } if (length > 0 && (system == null || input == null)) { Console.WriteLine("Length > 0 but no system or input"); - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; } lzx.ReferenceDataSize = length; @@ -400,11 +400,11 @@ namespace LibMSPackSharp.Compression // Length can't be more than 2^25, so no signedness problem if (bytes < (int)length) - return Error.MSPACK_ERR_READ; + return LibMSPackSharp.Error.MSPACK_ERR_READ; } lzx.ReferenceDataSize = length; - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; } // See description of outputLength in lzxd_init() @@ -440,11 +440,11 @@ namespace LibMSPackSharp.Compression /// the number of bytes of data to decompress. /// an error code, or MSPACK_ERR_OK if successful // TODO: Huffman tree implementation - public static Error Decompress(object o, long out_bytes) + public static LibMSPackSharp.Error Decompress(object o, long out_bytes) { LZXDStream lzx = o as LZXDStream; if (lzx == null) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; // Bitstream and huffman reading variables uint bit_buffer; @@ -461,9 +461,9 @@ namespace LibMSPackSharp.Compression // Easy answers if (lzx == null || (out_bytes < 0)) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; - if (lzx.Error != Error.MSPACK_ERR_OK) + if (lzx.Error != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; // Flush out any stored-up bytes before we begin @@ -474,7 +474,7 @@ namespace LibMSPackSharp.Compression if (i != 0) { try { lzx.Output.Write(lzx.e8_buf, lzx.OutputPointer, i); } - catch { return lzx.Error = Error.MSPACK_ERR_WRITE; } + catch { return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; } lzx.OutputPointer += i; lzx.Offset += i; @@ -482,7 +482,7 @@ namespace LibMSPackSharp.Compression } if (out_bytes == 0) - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; // Restore local state @@ -538,7 +538,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -552,7 +552,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -597,7 +597,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -611,7 +611,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -653,7 +653,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -667,7 +667,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -707,7 +707,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -721,7 +721,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -775,7 +775,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -800,7 +800,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -814,7 +814,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -854,7 +854,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -868,7 +868,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -908,7 +908,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -922,7 +922,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -972,7 +972,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -986,7 +986,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1067,7 +1067,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1081,7 +1081,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1110,7 +1110,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1129,7 +1129,7 @@ namespace LibMSPackSharp.Compression default: Console.WriteLine("Bad block type"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } } @@ -1170,7 +1170,7 @@ namespace LibMSPackSharp.Compression if (lzx.LENGTH_empty != 0) { Console.WriteLine("LENGTH symbol needed but tree is empty"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } READ_HUFFSYM(LENGTH, length_footer); @@ -1222,7 +1222,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1236,7 +1236,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1291,7 +1291,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1305,7 +1305,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1363,7 +1363,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1377,7 +1377,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1437,7 +1437,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1451,7 +1451,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1491,7 +1491,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1505,7 +1505,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1555,7 +1555,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1569,7 +1569,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1621,7 +1621,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1635,7 +1635,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1687,7 +1687,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1701,7 +1701,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1736,7 +1736,7 @@ namespace LibMSPackSharp.Compression if ((window_posn + match_length) > lzx.WindowSize) { Console.WriteLine("Match ran over window wrap"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } // Copy match @@ -1749,7 +1749,7 @@ namespace LibMSPackSharp.Compression if (match_offset > lzx.Offset && (match_offset - window_posn) > lzx.ReferenceDataSize) { Console.WriteLine("Match offset beyond LZX stream"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } // j = length from match offset to end of window @@ -1757,7 +1757,7 @@ namespace LibMSPackSharp.Compression if (j > (int)lzx.WindowSize) { Console.WriteLine("Match offset beyond window boundaries"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } runsrc = (int)(lzx.WindowSize - j); @@ -1807,7 +1807,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1830,7 +1830,7 @@ namespace LibMSPackSharp.Compression break; default: - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; // Might as well + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; // Might as well } // Did the final match overrun our desired this_run length? @@ -1839,7 +1839,7 @@ namespace LibMSPackSharp.Compression if ((uint)(-this_run) > lzx.BlockRemaining) { Console.WriteLine($"Overrun went past end of block by {-this_run} ({lzx.BlockRemaining} remaining)"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } lzx.BlockRemaining -= (uint)-this_run; @@ -1850,7 +1850,7 @@ namespace LibMSPackSharp.Compression if ((window_posn - lzx.FramePosition) != frame_size) { Console.WriteLine($"Decode beyond output frame limits! {window_posn - lzx.FramePosition} != {frame_size}"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } // Re-align input bitstream @@ -1866,7 +1866,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1880,7 +1880,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -1913,7 +1913,7 @@ namespace LibMSPackSharp.Compression if (lzx.OutputPointer != lzx.OutputLength) { Console.WriteLine($"{lzx.OutputLength - lzx.OutputPointer} avail bytes, new {frame_size} frame"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } // Does this intel block _really_ need decoding? @@ -1956,7 +1956,7 @@ namespace LibMSPackSharp.Compression // Write a frame i = (int)((out_bytes < frame_size) ? out_bytes : frame_size); try { lzx.Output.Write(lzx.e8_buf, lzx.OutputPointer, i); } - catch { return lzx.Error = Error.MSPACK_ERR_WRITE; } + catch { return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; } } else { @@ -1966,7 +1966,7 @@ namespace LibMSPackSharp.Compression // Write a frame i = (int)((out_bytes < frame_size) ? out_bytes : frame_size); try { lzx.Output.Write(lzx.Window, lzx.OutputPointer, i); } - catch { return lzx.Error = Error.MSPACK_ERR_WRITE; } + catch { return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; } } lzx.OutputPointer += i; @@ -1988,7 +1988,7 @@ namespace LibMSPackSharp.Compression if (out_bytes != 0) { Console.WriteLine("Bytes left to output"); - return lzx.Error = Error.MSPACK_ERR_DECRUNCH; + return lzx.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } // Store local state @@ -2006,11 +2006,11 @@ namespace LibMSPackSharp.Compression lzx.R1 = R1; lzx.R2 = R2; - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; } // TODO: Huffman tree implementation - private static Error ReadLens(LZXDStream lzx, byte[] lens, uint first, uint last) + private static LibMSPackSharp.Error ReadLens(LZXDStream lzx, byte[] lens, uint first, uint last) { // Bit buffer and huffman symbol decode variables uint bit_buffer; @@ -2044,7 +2044,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2058,7 +2058,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2110,7 +2110,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2124,7 +2124,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2174,7 +2174,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2188,7 +2188,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2238,7 +2238,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2252,7 +2252,7 @@ namespace LibMSPackSharp.Compression { if (i_ptr >= i_end) { - if (lzx.ReadInput() != Error.MSPACK_ERR_OK) + if (lzx.ReadInput() != LibMSPackSharp.Error.MSPACK_ERR_OK) return lzx.Error; i_ptr = lzx.InputPointer; @@ -2313,7 +2313,7 @@ namespace LibMSPackSharp.Compression lzx.BitsLeft = bits_left; } - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; } } } diff --git a/BurnOutSharp/External/libmspack/Compression/LZXDStream.cs b/BurnOutSharp/External/libmspack/Compression/LZXDStream.cs index a9edbba9..1959caff 100644 --- a/BurnOutSharp/External/libmspack/Compression/LZXDStream.cs +++ b/BurnOutSharp/External/libmspack/Compression/LZXDStream.cs @@ -137,6 +137,6 @@ namespace LibMSPackSharp.Compression // This is used purely for doing the intel E8 transform public byte[] e8_buf { get; set; } = new byte[LZX.LZX_FRAME_SIZE]; - public override int HUFF_ERROR() => (int)(Error = Error.MSPACK_ERR_DECRUNCH); + public override int HUFF_ERROR() => (int)(Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH); } } diff --git a/BurnOutSharp/External/libmspack/Compression/MSZIP.cs b/BurnOutSharp/External/libmspack/Compression/MSZIP.cs index 03ff9207..ca3411d0 100644 --- a/BurnOutSharp/External/libmspack/Compression/MSZIP.cs +++ b/BurnOutSharp/External/libmspack/Compression/MSZIP.cs @@ -156,16 +156,16 @@ namespace LibMSPackSharp.Compression return Error.MSPACK_ERR_ARGS; // For the bit buffer - uint bit_buffer = 0; - int bits_left = 0; - int i_ptr = 0, i_end = 0; + uint bit_buffer; + int bits_left; + int i_ptr, i_end; - int i, state, error; + int i, state; + Error error; // Easy answers if (zip == null || (out_bytes < 0)) return Error.MSPACK_ERR_ARGS; - if (zip.Error != Error.MSPACK_ERR_OK) return zip.Error; @@ -190,49 +190,66 @@ namespace LibMSPackSharp.Compression { // Unpack another block - //RESTORE_BITS; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; + //RESTORE_BITS + { + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + bit_buffer = zip.BitBuffer; + bits_left = zip.BitsLeft; + } // Skip to next read 'CK' header i = bits_left & 7; - //REMOVE_BITS(i); - bit_buffer >>= (i); - bits_left -= (i); + // Align to bytestream + + //REMOVE_BITS(i) + { + bit_buffer >>= (i); + bits_left -= (i); + } state = 0; - do { - //READ_BITS(i, 8); - - //ENSURE_BITS(8); - while (bits_left < 8) + //READ_BITS(i, 8) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(8) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + while (bits_left < (8)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) + + //REMOVE_BITS(8) + { + bit_buffer >>= (8); + bits_left -= (8); + } } - i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8); - - //REMOVE_BITS(8); - bit_buffer >>= (8); - bits_left -= (8); - if (i == 'C') state = 1; else if ((state == 1) && (i == 'K')) @@ -245,15 +262,17 @@ namespace LibMSPackSharp.Compression zip.WindowPosition = 0; zip.BytesOutput = 0; - //STORE_BITS; - zip.InputPointer = i_ptr; - zip.InputLength = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - - if ((error = (int)Inflate(zip)) != 0) + //STORE_BITS { - Console.WriteLine($"Inflate error {(InflateErrorCode)error}"); + zip.InputPointer = i_ptr; + zip.InputLength = i_end; + zip.BitBuffer = bit_buffer; + zip.BitsLeft = bits_left; + } + + if ((error = Inflate(zip)) != Error.MSPACK_ERR_OK) + { + Console.WriteLine($"Inflate error {error}"); if (zip.RepairMode) { // Recover partially-inflated buffers @@ -270,28 +289,21 @@ namespace LibMSPackSharp.Compression } else { - return zip.Error = (error > 0) ? (Error)error : Error.MSPACK_ERR_DECRUNCH; + return zip.Error = (error > 0) ? error : Error.MSPACK_ERR_DECRUNCH; } } + zip.OutputPointer = 0; zip.OutputLength = zip.BytesOutput; // Write a frame i = (out_bytes < zip.BytesOutput) ? (int)out_bytes : zip.BytesOutput; - if (zip.Output is FileStream) - { - if (SystemImpl.DefaultSystem.Write(zip.Output, zip.Window, zip.OutputPointer, i) != i) - return zip.Error = Error.MSPACK_ERR_WRITE; - } - else - { - if (zip.Sys.Write(zip.Output, zip.Window, zip.OutputPointer, i) != i) - return zip.Error = Error.MSPACK_ERR_WRITE; - } + if (zip.Sys.Write(zip.Output, zip.Window, zip.OutputPointer, i) != i) + return zip.Error = Error.MSPACK_ERR_WRITE; // mspack errors (i.e. read errors) are fatal and can't be recovered - if (error > 0 && zip.RepairMode) - return (Error)error; + if ((error > 0) && zip.RepairMode) + return error; zip.OutputPointer += i; out_bytes -= i; @@ -317,76 +329,105 @@ namespace LibMSPackSharp.Compression int bits_left; int i_ptr, i_end; - int i, error, block_len; + int i, block_len; + Error error; // Unpack blocks until block_len == 0 for (; ; ) { - //RESTORE_BITS; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; + //RESTORE_BITS + { + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + bit_buffer = zip.BitBuffer; + bits_left = zip.BitsLeft; + } // Align to bytestream, read block_len - i = (int)(bits_left & 7); + i = bits_left & 7; - //REMOVE_BITS(i); - bit_buffer >>= (i); - bits_left -= (i); - - //READ_BITS(block_len, 8); - - //ENSURE_BITS(8); - while (bits_left < 8) + //REMOVE_BITS(i) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) - { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; - - i_ptr = zip.InputPointer; - i_end = zip.InputLength; - } - - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + bit_buffer >>= (i); + bits_left -= (i); } - block_len = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8); - - //REMOVE_BITS(8); - bit_buffer >>= (8); - bits_left -= (8); - - // READ_BITS(i, 8); - - //ENSURE_BITS(8); - while (bits_left < 8) + //READ_BITS(block_len, 8) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(8) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + while (bits_left < (8)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (block_len) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) + + //REMOVE_BITS(8) + { + bit_buffer >>= (8); + bits_left -= (8); + } } - i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8); + //READ_BITS(i, 8) + { + //ENSURE_BITS(8) + { + while (bits_left < (8)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - //REMOVE_BITS(8); - bit_buffer >>= (8); - bits_left -= (8); + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } + } + + (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) + + //REMOVE_BITS(8) + { + bit_buffer >>= (8); + bits_left -= (8); + } + } + block_len |= i << 8; if (block_len == 0) @@ -394,61 +435,85 @@ namespace LibMSPackSharp.Compression // Read "CK" header - // READ_BITS(i, 8); - - //ENSURE_BITS(8); - while (bits_left < 8) + //READ_BITS(i, 8) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(8) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + while (bits_left < (8)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) + + //REMOVE_BITS(8) + { + bit_buffer >>= (8); + bits_left -= (8); + } } - i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8); - - //REMOVE_BITS(8); - bit_buffer >>= (8); - bits_left -= (8); - if (i != 'C') return Error.MSPACK_ERR_DATAFORMAT; - // READ_BITS(i, 8); - - //ENSURE_BITS(8); - while (bits_left < 8) + //READ_BITS(i, 8) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(8) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return zip.Error; + while (bits_left < (8)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (i) = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) + + //REMOVE_BITS(nbits) + { + bit_buffer >>= (8); + bits_left -= (8); + } } - i = (int)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8); - - //REMOVE_BITS(8); - bit_buffer >>= (8); - bits_left -= (8); - if (i != 'K') return Error.MSPACK_ERR_DATAFORMAT; @@ -456,27 +521,29 @@ namespace LibMSPackSharp.Compression zip.WindowPosition = 0; zip.BytesOutput = 0; - //STORE_BITS; - zip.InputPointer = i_ptr; - zip.InputLength = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; - - if ((error = (int)Inflate(zip)) != 0) + //STORE_BITS { - Console.WriteLine($"inflate error {(InflateErrorCode)error}"); - return zip.Error = (error > 0) ? (Error)error : Error.MSPACK_ERR_DECRUNCH; + zip.InputPointer = i_ptr; + zip.InputLength = i_end; + zip.BitBuffer = bit_buffer; + zip.BitsLeft = bits_left; + } + + if ((error = Inflate(zip)) != Error.MSPACK_ERR_OK) + { + Console.WriteLine($"Inflate error {error}"); + return zip.Error = (error > 0) ? error : Error.MSPACK_ERR_DECRUNCH; } // Write inflated block - if (zip.Sys.Write(zip.Output, zip.Window, 0, zip.BytesOutput) != zip.BytesOutput) - return zip.Error = Error.MSPACK_ERR_WRITE; + try { zip.Sys.Write(zip.Output, zip.Window, 0, zip.BytesOutput); } + catch { return zip.Error = Error.MSPACK_ERR_WRITE; } } return Error.MSPACK_ERR_OK; } - private static InflateErrorCode ReadLens(MSZIPDStream zip) + private static Error ReadLens(MSZIPDStream zip) { // For the bit buffer and huffman decoding uint bit_buffer; @@ -484,175 +551,233 @@ namespace LibMSPackSharp.Compression int i_ptr, i_end; // bitlen Huffman codes -- immediate lookup, 7 bit max code length - ushort[] bl_table = new ushort[1 << 7]; + ushort[] bl_table = new ushort[(1 << 7)]; byte[] bl_len = new byte[19]; 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.InputLength; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; + //RESTORE_BITS + { + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + bit_buffer = zip.BitBuffer; + bits_left = zip.BitsLeft; + } // Read the number of codes - // READ_BITS(lit_codes, 5); - - //ENSURE_BITS(5); - while (bits_left < 5) + //READ_BITS(lit_codes, 5) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(5) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + while (bits_left < (5)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (lit_codes) = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5) + + //REMOVE_BITS(5) + { + bit_buffer >>= (5); + bits_left -= (5); + } } - lit_codes = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5); - - //REMOVE_BITS(5); - bit_buffer >>= (5); - bits_left -= (5); - lit_codes += 257; - // READ_BITS(dist_codes, 5); - - //ENSURE_BITS(5); - while (bits_left < 5) + //READ_BITS_T(dist_codes, 5) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(5) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + while (bits_left < (5)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (dist_codes) = (bit_buffer & lsb_bit_mask[(5)]); //PEEK_BITS_T(5) + + //REMOVE_BITS(5) + { + bit_buffer >>= (5); + bits_left -= (5); + } } - dist_codes = (bit_buffer & ((1 << (5)) - 1)); //PEEK_BITS(5); - - //REMOVE_BITS(5); - bit_buffer >>= (5); - bits_left -= (5); - dist_codes += 1; - // READ_BITS(bitlen_codes, 4); - - //ENSURE_BITS(4); - while (bits_left < 4) + //READ_BITS(bitlen_codes, 4) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(4) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + while (bits_left < (4)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (bitlen_codes) = (bit_buffer & ((1 << (4)) - 1)); //PEEK_BITS(4) + + //REMOVE_BITS(nbits) + { + bit_buffer >>= (4); + bits_left -= (4); + } } - bitlen_codes = (bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(4); - - //REMOVE_BITS(4); - bit_buffer >>= (4); - bits_left -= (4); - bitlen_codes += 4; if (lit_codes > MSZIP_LITERAL_MAXSYMBOLS) - return InflateErrorCode.INF_ERR_SYMLENS; + return Error.INF_ERR_SYMLENS; if (dist_codes > MSZIP_DISTANCE_MAXSYMBOLS) - return InflateErrorCode.INF_ERR_SYMLENS; + return Error.INF_ERR_SYMLENS; // Read in the bit lengths in their unusual order for (i = 0; i < bitlen_codes; i++) { - // READ_BITS(bl_len[bitlen_order[i]], 3); - - //ENSURE_BITS(3); - while (bits_left < 3) + //READ_BITS(bl_len[bitlen_order[i]], 3) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(3) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + while (bits_left < (3)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (bl_len[bitlen_order[i]]) = (byte)(bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3) + + //REMOVE_BITS(3) + { + bit_buffer >>= (3); + bits_left -= (3); + } } - - bl_len[bitlen_order[i]] = (byte)(bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3); - - //REMOVE_BITS(3); - bit_buffer >>= (3); - bits_left -= (3); } - while (i < 19) - { - bl_len[bitlen_order[i++]] = 0; - } + while (i < 19) bl_len[bitlen_order[i++]] = 0; // Create decoding table with an immediate lookup - if (MSZIPDStream.MakeDecodeTable(19, 7, bl_len, bl_table, msb: false)) - return InflateErrorCode.INF_ERR_BITLENTBL; + if (CompressionStream.MakeDecodeTable(19, 7, bl_len, bl_table, msb: false)) + return Error.INF_ERR_BITLENTBL; - // Read literal / distance code lengths + // Read literal / distance code lengths */ for (i = 0; i < (lit_codes + dist_codes); i++) { // Single-level huffman lookup - //ENSURE_BITS(7); - while (bits_left < 7) + //ENSURE_BITS(7) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + while (bits_left < (7)) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } } - - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); } - code = bl_table[(bit_buffer & ((1 << (7)) - 1))]; //PEEK_BITS(7); + 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]); + //REMOVE_BITS(bl_len[code]) + { + bit_buffer >>= (bl_len[code]); + bits_left -= (bl_len[code]); + } if (code < 16) { @@ -666,16 +791,39 @@ namespace LibMSPackSharp.Compression //READ_BITS(run, 2) { //ENSURE_BITS(2) - while (bits_left < (2)) { - READ_BYTES; + while (bits_left < (2)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; + + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - run = (int)(bit_buffer >> (BITBUF_WIDTH - (2))); + (run) = (bit_buffer & ((1 << (2)) - 1)); //PEEK_BITS(2) - // REMOVE_BITS(2); - bit_buffer <<= (2); - bits_left -= (2); + //REMOVE_BITS(2) + { + bit_buffer >>= (2); + bits_left -= (2); + } } run += 3; @@ -686,19 +834,41 @@ namespace LibMSPackSharp.Compression //READ_BITS(run, 3) { //ENSURE_BITS(3) - while (bits_left < (3)) { - READ_BYTES; + while (bits_left < (3)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; + + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - run = (int)(bit_buffer >> (BITBUF_WIDTH - (3))); + (run) = (bit_buffer & ((1 << (3)) - 1)); //PEEK_BITS(3) - // REMOVE_BITS(3); - bit_buffer <<= (3); - bits_left -= (3); + //REMOVE_BITS(3) + { + bit_buffer >>= (3); + bits_left -= (3); + } } - run += 3; code = 0; break; @@ -707,16 +877,39 @@ namespace LibMSPackSharp.Compression //READ_BITS(run, 7) { //ENSURE_BITS(7) - while (bits_left < (7)) { - READ_BYTES; + while (bits_left < (7)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; + + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - run = (int)(bit_buffer >> (BITBUF_WIDTH - (7))); + (run) = (bit_buffer & ((1 << (7)) - 1)); //PEEK_BITS(7) - // REMOVE_BITS(7); - bit_buffer <<= (7); - bits_left -= (7); + //REMOVE_BITS(7) + { + bit_buffer >>= (7); + bits_left -= (7); + } } run += 11; @@ -725,11 +918,11 @@ namespace LibMSPackSharp.Compression default: Console.WriteLine($"Bad code!: {code}"); - return InflateErrorCode.INF_ERR_BADBITLEN; + return Error.INF_ERR_BADBITLEN; } if ((i + run) > (lit_codes + dist_codes)) - return InflateErrorCode.INF_ERR_BITOVERRUN; + return Error.INF_ERR_BITOVERRUN; while (run-- != 0) { @@ -755,11 +948,13 @@ namespace LibMSPackSharp.Compression zip.DISTANCE_len[i++] = 0; } - //STORE_BITS; - zip.InputPointer = i_ptr; - zip.InputLength = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; + //STORE_BITS + { + zip.InputPointer = i_ptr; + zip.InputLength = i_end; + zip.BitBuffer = bit_buffer; + zip.BitsLeft = bits_left; + } return 0; } @@ -767,21 +962,25 @@ namespace LibMSPackSharp.Compression /// /// A clean implementation of RFC 1951 / inflate /// - private static InflateErrorCode Inflate(MSZIPDStream zip) + // TODO: Huffman tree implementation + private static Error Inflate(MSZIPDStream zip) { - int last_block, block_type, distance = 0, length = 0, this_run, i; + 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; - InflateErrorCode err; - //RESTORE_BITS; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; + //RESTORE_BITS + { + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + bit_buffer = zip.BitBuffer; + bits_left = zip.BitsLeft; + } do { @@ -790,16 +989,39 @@ namespace LibMSPackSharp.Compression //READ_BITS(last_block, 1) { //ENSURE_BITS(1) - while (bits_left < (1)) { - READ_BYTES; + while (bits_left < (1)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; + + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - last_block = (int)(bit_buffer >> (BITBUF_WIDTH - (1))); + (last_block) = (bit_buffer & ((1 << (1)) - 1)); //PEEK_BITS(1) - // REMOVE_BITS(1); - bit_buffer <<= (1); - bits_left -= (1); + //REMOVE_BITS(1) + { + bit_buffer >>= (1); + bits_left -= (1); + } } // Read in block type @@ -807,110 +1029,144 @@ namespace LibMSPackSharp.Compression //READ_BITS(block_type, 2) { //ENSURE_BITS(2) - while (bits_left < (2)) { - READ_BYTES; + while (bits_left < (2)) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; + + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - block_type = (int)(bit_buffer >> (BITBUF_WIDTH - (2))); + (block_type) = (bit_buffer & ((1 << (2)) - 1)); //PEEK_BITS(2) - // REMOVE_BITS(2); - bit_buffer <<= (2); - bits_left -= (2); + //REMOVE_BITS(2) + { + bit_buffer >>= (2); + bits_left -= (2); + } } - // Uncompressed block if (block_type == 0) { + // Uncompressed block byte[] lens_buf = new byte[4]; // Go to byte boundary - i = bits_left & 7; + i = (uint)(bits_left & 7); - //REMOVE_BITS(i); - bit_buffer >>= (i); - bits_left -= (i); + //REMOVE_BITS(i) + { + bit_buffer >>= (int)(i); + bits_left -= (int)(i); + } // Read 4 bytes of data, emptying the bit-buffer if necessary for (i = 0; (bits_left >= 8); i++) { if (i == 4) - return InflateErrorCode.INF_ERR_BITBUF; + return Error.INF_ERR_BITBUF; - lens_buf[i] = (byte)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8); + lens_buf[i] = (byte)(bit_buffer & ((1 << (8)) - 1)); //PEEK_BITS(8) - //REMOVE_BITS(8); - bit_buffer >>= (8); - bits_left -= (8); + //REMOVE_BITS(8) + { + bit_buffer >>= (8); + bits_left -= (8); + } } if (bits_left != 0) - return InflateErrorCode.INF_ERR_BITBUF; + return Error.INF_ERR_BITBUF; while (i < 4) { - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //READ_IF_NEEDED { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } } lens_buf[i++] = zip.InputBuffer[i_ptr++]; } // Get the length and its complement - length = lens_buf[0] | (lens_buf[1] << 8); - i = lens_buf[2] | (lens_buf[3] << 8); + length = (uint)(lens_buf[0] | (lens_buf[1] << 8)); + i = (uint)(lens_buf[2] | (lens_buf[3] << 8)); if (length != (~i & 0xFFFF)) - return InflateErrorCode.INF_ERR_COMPLEMENT; + return Error.INF_ERR_COMPLEMENT; // Read and copy the uncompressed data into the window while (length > 0) { - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //READ_IF_NEEDED { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } } this_run = length; if (this_run > (uint)(i_end - i_ptr)) - this_run = i_end - i_ptr; + this_run = (uint)(i_end - i_ptr); if (this_run > (MSZIP_FRAME_SIZE - zip.WindowPosition)) - this_run = (int)(MSZIP_FRAME_SIZE - zip.WindowPosition); + this_run = MSZIP_FRAME_SIZE - zip.WindowPosition; - Array.Copy(zip.InputBuffer, i_ptr, zip.Window, (int)zip.WindowPosition, this_run); - zip.WindowPosition += (uint)this_run; - i_ptr += this_run; + Array.Copy(zip.InputBuffer, i_ptr, zip.Window, zip.WindowPosition, this_run); + + zip.WindowPosition += this_run; + i_ptr += (int)this_run; length -= this_run; - // FLUSH_IF_NEEDED - if (zip.WindowPosition == MSZIP_FRAME_SIZE) + //FLUSH_IF_NEEDED { - if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) - return InflateErrorCode.INF_ERR_FLUSH; + if (zip.WindowPosition == MSZIP_FRAME_SIZE) + { + if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) + return Error.INF_ERR_FLUSH; - zip.WindowPosition = 0; + zip.WindowPosition = 0; + } } } } else if ((block_type == 1) || (block_type == 2)) { // Huffman-compressed LZ77 block - uint match_posn, code = 0; + uint match_posn, code; - // Block with fixed Huffman codes if (block_type == 1) { + // Block with fixed Huffman codes i = 0; while (i < 144) { @@ -937,51 +1193,56 @@ namespace LibMSPackSharp.Compression zip.DISTANCE_len[i] = 5; } } - - // Block with dynamic Huffman codes else { - //STORE_BITS; - zip.InputPointer = i_ptr; - zip.InputLength = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; + // Block with dynamic Huffman codes - if ((i = (int)ReadLens(zip)) != 0) - return (InflateErrorCode)i; + //STORE_BITS + { + zip.InputPointer = i_ptr; + zip.InputLength = i_end; + zip.BitBuffer = bit_buffer; + zip.BitsLeft = bits_left; + } - //RESTORE_BITS; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; - bit_buffer = zip.BitBuffer; - bits_left = zip.BitsLeft; + if ((err = ReadLens(zip)) != Error.MSPACK_ERR_OK) + return err; + + //RESTORE_BITS + { + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + bit_buffer = zip.BitBuffer; + bits_left = zip.BitsLeft; + } } // Now huffman lengths are read for either kind of block, // create huffman decoding tables - if (!MSZIPDStream.MakeDecodeTable(MSZIP_LITERAL_MAXSYMBOLS, MSZIP_LITERAL_TABLEBITS, zip.LITERAL_len, zip.LITERAL_table, msb: false)) - return InflateErrorCode.INF_ERR_LITERALTBL; + if (CompressionStream.MakeDecodeTable(MSZIP_LITERAL_MAXSYMBOLS, MSZIP_LITERAL_TABLEBITS, zip.LITERAL_len, zip.LITERAL_table, msb: false)) + return Error.INF_ERR_LITERALTBL; - if (!MSZIPDStream.MakeDecodeTable(MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_DISTANCE_TABLEBITS, zip.DISTANCE_len, zip.DISTANCE_table, msb: false)) - return InflateErrorCode.INF_ERR_DISTANCETBL; + if (CompressionStream.MakeDecodeTable(MSZIP_DISTANCE_MAXSYMBOLS, MSZIP_DISTANCE_TABLEBITS, zip.DISTANCE_len, zip.DISTANCE_table, msb: false)) + return Error.INF_ERR_DISTANCETBL; // Decode forever until end of block code for (; ; ) { - if ((err = (InflateErrorCode)zip.READ_HUFFSYM(zip.LITERAL_table, ref code, MSZIP_LITERAL_TABLEBITS, zip.LITERAL_len, MSZIP_LITERAL_MAXSYMBOLS, ref i, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: false)) != InflateErrorCode.INF_ERR_OK) - return err; + READ_HUFFSYM(LITERAL, code); if (code < 256) { zip.Window[zip.WindowPosition++] = (byte)code; - // FLUSH_IF_NEEDED - if (zip.WindowPosition == MSZIP_FRAME_SIZE) + //FLUSH_IF_NEEDED { - if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) - return InflateErrorCode.INF_ERR_FLUSH; + if (zip.WindowPosition == MSZIP_FRAME_SIZE) + { + if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) + return Error.INF_ERR_FLUSH; - zip.WindowPosition = 0; + zip.WindowPosition = 0; + } } } else if (code == 256) @@ -993,74 +1254,97 @@ namespace LibMSPackSharp.Compression { code -= 257; // Codes 257-285 are matches if (code >= 29) - return InflateErrorCode.INF_ERR_LITCODE; // Codes 286-287 are illegal + return Error.INF_ERR_LITCODE; // Codes 286-287 are illegal //READ_BITS_T(length, lit_extrabits[code]) - - //ENSURE_BITS(lit_extrabits[code]); - while (bits_left < lit_extrabits[code]) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(lit_extrabits[code]) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + while (bits_left < (lit_extrabits[code])) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (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) = (int)(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 += lit_lengths[code]; - if ((err = (InflateErrorCode)zip.READ_HUFFSYM(zip.DISTANCE_table, ref code, MSZIP_DISTANCE_TABLEBITS, zip.DISTANCE_len, MSZIP_DISTANCE_MAXSYMBOLS, ref i, ref i_ptr, ref i_end, ref bits_left, ref bit_buffer, msb: false)) != 0) - return err; + READ_HUFFSYM(DISTANCE, code); if (code >= 30) - return InflateErrorCode.INF_ERR_DISTCODE; + return Error.INF_ERR_DISTCODE; //READ_BITS_T(distance, dist_extrabits[code]) - - //ENSURE_BITS(dist_extrabits[code]); - while (bits_left < dist_extrabits[code]) { - //READ_BYTES; - - //READ_IF_NEEDED; - if (i_ptr >= i_end) + //ENSURE_BITS(dist_extrabits[code]) { - if (zip.ReadInput() != Error.MSPACK_ERR_OK) - return (InflateErrorCode)zip.Error; + while (bits_left < (dist_extrabits[code])) + { + //READ_BYTES + { + //READ_IF_NEEDED + { + if (i_ptr >= i_end) + { + if (zip.ReadInput() != Error.MSPACK_ERR_OK) + return zip.Error; - i_ptr = zip.InputPointer; - i_end = zip.InputLength; + i_ptr = zip.InputPointer; + i_end = zip.InputLength; + } + } + + //INJECT_BITS(zip.InputBuffer[i_ptr++], 8) + { + bit_buffer |= (uint)((zip.InputBuffer[i_ptr++]) << bits_left); + bits_left += (8); + } + } + } } - bit_buffer |= (uint)(zip.InputBuffer[i_ptr++]) << bits_left; bits_left += (8); // INJECT_BITS(zip.InputBuffer[i_ptr++], 8); + (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) = (int)(bit_buffer & lsb_bit_mask[(dist_extrabits[code])]); //PEEK_BITS_T(dist_extrabits[code]); - - //REMOVE_BITS(lit_extrabits[code]); - bit_buffer >>= (dist_extrabits[code]); - bits_left -= (dist_extrabits[code]); - distance += dist_offsets[code]; // Match position is window position minus distance. If distance // is more than window position numerically, it must 'wrap // around' the frame size. - match_posn = (uint)(((distance > zip.WindowPosition) ? MSZIP_FRAME_SIZE : 0) + zip.WindowPosition - distance); + match_posn = (uint)((distance > zip.WindowPosition) ? MSZIP_FRAME_SIZE : 0) + zip.WindowPosition - distance; // Copy match if (length < 12) @@ -1071,35 +1355,35 @@ namespace LibMSPackSharp.Compression zip.Window[zip.WindowPosition++] = zip.Window[match_posn++]; match_posn &= MSZIP_FRAME_SIZE - 1; - // FLUSH_IF_NEEDED - if (zip.WindowPosition == MSZIP_FRAME_SIZE) + //FLUSH_IF_NEEDED { - if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) - return InflateErrorCode.INF_ERR_FLUSH; + if (zip.WindowPosition == MSZIP_FRAME_SIZE) + { + if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) + return Error.INF_ERR_FLUSH; - zip.WindowPosition = 0; + zip.WindowPosition = 0; + } } } } else { - // Longer match, use faster loop but with setup expense + // Longer match, use faster loop but with setup expense */ int runsrc, rundest; do { this_run = length; if ((match_posn + this_run) > MSZIP_FRAME_SIZE) - this_run = MSZIP_FRAME_SIZE - (int)match_posn; - + this_run = MSZIP_FRAME_SIZE - match_posn; if ((zip.WindowPosition + this_run) > MSZIP_FRAME_SIZE) - this_run = MSZIP_FRAME_SIZE - (int)zip.WindowPosition; + this_run = MSZIP_FRAME_SIZE - zip.WindowPosition; rundest = (int)zip.WindowPosition; - zip.WindowPosition += (uint)this_run; + zip.WindowPosition += this_run; runsrc = (int)match_posn; - match_posn += (uint)this_run; + match_posn += this_run; length -= this_run; - while (this_run-- != 0) { zip.Window[rundest++] = zip.Window[runsrc++]; @@ -1108,23 +1392,27 @@ namespace LibMSPackSharp.Compression if (match_posn == MSZIP_FRAME_SIZE) match_posn = 0; - // FLUSH_IF_NEEDED - if (zip.WindowPosition == MSZIP_FRAME_SIZE) + //FLUSH_IF_NEEDED { - if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) - return InflateErrorCode.INF_ERR_FLUSH; + if (zip.WindowPosition == MSZIP_FRAME_SIZE) + { + if (zip.FlushWindow(zip, MSZIP_FRAME_SIZE) != Error.MSPACK_ERR_OK) + return Error.INF_ERR_FLUSH; - zip.WindowPosition = 0; + zip.WindowPosition = 0; + } } } while (length > 0); } + } + } } else { // block_type == 3 -- bad block type - return InflateErrorCode.INF_ERR_BLOCKTYPE; + return Error.INF_ERR_BLOCKTYPE; } } while (last_block == 0); @@ -1132,17 +1420,19 @@ namespace LibMSPackSharp.Compression if (zip.WindowPosition != 0) { if (zip.FlushWindow(zip, zip.WindowPosition) != Error.MSPACK_ERR_OK) - return InflateErrorCode.INF_ERR_FLUSH; + return Error.INF_ERR_FLUSH; } - //STORE_BITS; - zip.InputPointer = i_ptr; - zip.InputLength = i_end; - zip.BitBuffer = bit_buffer; - zip.BitsLeft = bits_left; + //STORE_BITS + { + zip.InputPointer = i_ptr; + zip.InputLength = i_end; + zip.BitBuffer = bit_buffer; + zip.BitsLeft = bits_left; + } // Return success - return InflateErrorCode.INF_ERR_OK; + return Error.MSPACK_ERR_OK; } /// diff --git a/BurnOutSharp/External/libmspack/Compression/MSZIPDStream.cs b/BurnOutSharp/External/libmspack/Compression/MSZIPDStream.cs index dab87e26..9bffac77 100644 --- a/BurnOutSharp/External/libmspack/Compression/MSZIPDStream.cs +++ b/BurnOutSharp/External/libmspack/Compression/MSZIPDStream.cs @@ -29,7 +29,7 @@ namespace LibMSPackSharp.Compression /// /// inflate() will call this whenever the window should be emptied. /// - public Func FlushWindow; + public Func FlushWindow; public bool RepairMode { get; set; } @@ -49,6 +49,6 @@ namespace LibMSPackSharp.Compression #endregion - public override int HUFF_ERROR() => (int)InflateErrorCode.INF_ERR_HUFFSYM; + public override int HUFF_ERROR() => (int)Error.INF_ERR_HUFFSYM; } } diff --git a/BurnOutSharp/External/libmspack/Compression/None.cs b/BurnOutSharp/External/libmspack/Compression/None.cs index 3d26968a..8bd1ae8a 100644 --- a/BurnOutSharp/External/libmspack/Compression/None.cs +++ b/BurnOutSharp/External/libmspack/Compression/None.cs @@ -25,11 +25,11 @@ namespace LibMSPackSharp.Compression }; } - public static Error Decompress(object s, long bytes) + public static LibMSPackSharp.Error Decompress(object s, long bytes) { NoneState state = (NoneState)s; if (state == null) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; int run; while (bytes > 0) @@ -37,14 +37,14 @@ namespace LibMSPackSharp.Compression run = (bytes > state.BufferSize) ? state.BufferSize : (int)bytes; if (state.Sys.Read(state.Input, state.Buffer, 0, run) != run) - return Error.MSPACK_ERR_READ; + return LibMSPackSharp.Error.MSPACK_ERR_READ; if (state.Sys.Write(state.Output, state.Buffer, 0, run) != run) - return Error.MSPACK_ERR_WRITE; + return LibMSPackSharp.Error.MSPACK_ERR_WRITE; bytes -= run; } - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; } } } diff --git a/BurnOutSharp/External/libmspack/Compression/QTM.cs b/BurnOutSharp/External/libmspack/Compression/QTM.cs index 49e9a67a..375c4811 100644 --- a/BurnOutSharp/External/libmspack/Compression/QTM.cs +++ b/BurnOutSharp/External/libmspack/Compression/QTM.cs @@ -123,7 +123,7 @@ namespace LibMSPackSharp.Compression qtm.WindowPosition = 0; qtm.FrameTODO = QTM_FRAME_SIZE; qtm.HeaderRead = 0; - qtm.Error = Error.MSPACK_ERR_OK; + qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_OK; qtm.InputPointer = qtm.InputLength = 0; qtm.OutputPointer = qtm.OutputLength = 0; @@ -170,11 +170,11 @@ namespace LibMSPackSharp.Compression /// qtmd_init(). This will continue until system.read() returns 0 bytes, /// or an error. /// - public static Error Decompress(object o, long out_bytes) + public static LibMSPackSharp.Error Decompress(object o, long out_bytes) { QTMDStream qtm = o as QTMDStream; if (qtm == null) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; uint frame_end, match_offset, range = 0, extra = 0; int i_ptr = 0, i_end = 0, runsrc, rundest; @@ -186,9 +186,9 @@ namespace LibMSPackSharp.Compression // Easy answers if (qtm == null || (out_bytes < 0)) - return Error.MSPACK_ERR_ARGS; + return LibMSPackSharp.Error.MSPACK_ERR_ARGS; - if (qtm.Error != Error.MSPACK_ERR_OK) + if (qtm.Error != LibMSPackSharp.Error.MSPACK_ERR_OK) return qtm.Error; // Flush out any stored-up bytes before we begin @@ -199,14 +199,14 @@ namespace LibMSPackSharp.Compression if (i != 0) { if (qtm.Sys.Write(qtm.Output, qtm.Window, qtm.OutputPointer, i) != i) - return qtm.Error = Error.MSPACK_ERR_WRITE; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; qtm.OutputPointer += i; out_bytes -= i; } if (out_bytes == 0) - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; // Restore local state @@ -407,7 +407,7 @@ namespace LibMSPackSharp.Compression default: // Should be impossible, model7 can only return 0-6 Console.WriteLine("got %d from selector", selector); - return qtm.Error = Error.MSPACK_ERR_DECRUNCH; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } rundest = (int)window_posn; @@ -436,11 +436,11 @@ namespace LibMSPackSharp.Compression if (i > out_bytes) { Console.WriteLine($"during window-wrap match; {i} bytes to flush but only need {out_bytes}"); - return qtm.Error = Error.MSPACK_ERR_DECRUNCH; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } if (qtm.Sys.Write(qtm.Output, qtm.Window, qtm.OutputPointer, i) != i) - return qtm.Error = Error.MSPACK_ERR_WRITE; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; out_bytes -= i; qtm.OutputPointer = 0; @@ -471,7 +471,7 @@ namespace LibMSPackSharp.Compression if (j > (int)qtm.WindowSize) { Console.WriteLine("match offset beyond window boundaries"); - return qtm.Error = Error.MSPACK_ERR_DECRUNCH; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } runsrc = (int)(qtm.WindowSize - j); @@ -513,7 +513,7 @@ namespace LibMSPackSharp.Compression if (frame_todo > QTM_FRAME_SIZE) { Console.WriteLine("overshot frame alignment"); - return qtm.Error = Error.MSPACK_ERR_DECRUNCH; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_DECRUNCH; } // Another frame completed? @@ -564,7 +564,7 @@ namespace LibMSPackSharp.Compression break; if (qtm.Sys.Write(qtm.Output, qtm.Window, qtm.OutputPointer, i) != i) - return qtm.Error = Error.MSPACK_ERR_WRITE; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; out_bytes -= i; qtm.OutputPointer = 0; @@ -577,7 +577,7 @@ namespace LibMSPackSharp.Compression { i = (int)out_bytes; if (qtm.Sys.Write(qtm.Output, qtm.Window, qtm.OutputPointer, i) != i) - return qtm.Error = Error.MSPACK_ERR_WRITE; + return qtm.Error = LibMSPackSharp.Error.MSPACK_ERR_WRITE; qtm.OutputPointer += i; } @@ -596,7 +596,7 @@ namespace LibMSPackSharp.Compression qtm.Low = low; qtm.Current = (ushort)current; - return Error.MSPACK_ERR_OK; + return LibMSPackSharp.Error.MSPACK_ERR_OK; } /// @@ -661,7 +661,7 @@ namespace LibMSPackSharp.Compression READ_BYTES; } - if (qtm.Error != Error.MSPACK_ERR_OK) + if (qtm.Error != LibMSPackSharp.Error.MSPACK_ERR_OK) return; current = (ushort)((current << 1) | (bit_buffer >> (LZXDStream.BITBUF_WIDTH - (1)))); //PEEK_BITS(1) diff --git a/BurnOutSharp/External/libmspack/Compression/QTMDStream.cs b/BurnOutSharp/External/libmspack/Compression/QTMDStream.cs index 6132645b..ee1a39c6 100644 --- a/BurnOutSharp/External/libmspack/Compression/QTMDStream.cs +++ b/BurnOutSharp/External/libmspack/Compression/QTMDStream.cs @@ -120,6 +120,6 @@ namespace LibMSPackSharp.Compression public QTMDModelSym[] Model7Symbols { get; set; } = new QTMDModelSym[7 + 1]; - public override int HUFF_ERROR() => (int)Error.MSPACK_ERR_OK; + public override int HUFF_ERROR() => (int)LibMSPackSharp.Error.MSPACK_ERR_OK; } } diff --git a/BurnOutSharp/External/libmspack/Enums.cs b/BurnOutSharp/External/libmspack/Enums.cs index a3917055..0acb3d44 100644 --- a/BurnOutSharp/External/libmspack/Enums.cs +++ b/BurnOutSharp/External/libmspack/Enums.cs @@ -24,6 +24,8 @@ namespace LibMSPackSharp /// public enum Error { + #region MSPACK Errors + /// /// Used to indicate success. /// This error code is defined as zero, all other code are non-zero. @@ -84,6 +86,82 @@ namespace LibMSPackSharp /// Error during decompression /// MSPACK_ERR_DECRUNCH = 11, + + #endregion + + #region Inflate Errors + + /// + /// Unknown block type + /// + INF_ERR_BLOCKTYPE = -1, + + /// + /// Block size complement mismatch + /// + INF_ERR_COMPLEMENT = -2, + + /// + /// Error from flush_window callback + /// + INF_ERR_FLUSH = -3, + + /// + /// Too many bits in bit buffer + /// + INF_ERR_BITBUF = -4, + + /// + /// Too many symbols in blocktype 2 header + /// + INF_ERR_SYMLENS = -5, + + /// + /// Failed to build bitlens huffman table + /// + INF_ERR_BITLENTBL = -6, + + /// + /// Failed to build literals huffman table + /// + INF_ERR_LITERALTBL = -7, + + /// + /// Failed to build distance huffman table + /// + INF_ERR_DISTANCETBL = -8, + + /// + /// Bitlen RLE code goes over table size + /// + INF_ERR_BITOVERRUN = -9, + + /// + /// Invalid bit-length code + /// + INF_ERR_BADBITLEN = -10, + + /// + /// Out-of-range literal code + /// + INF_ERR_LITCODE = -11, + + /// + /// Out-of-range distance code + /// + INF_ERR_DISTCODE = -12, + + /// + /// Somehow, distance is beyond 32k + /// + INF_ERR_DISTANCE = -13, + + /// + /// Out of bits decoding huffman symbol + /// + INF_ERR_HUFFSYM = -14, + + #endregion } ///