diff --git a/Libraries/UniversalEditor.Compression/Modules/LZX/Internal/Constants.cs b/Libraries/UniversalEditor.Compression/Modules/LZX/Internal/Constants.cs index fecb1476..febd3cfc 100644 --- a/Libraries/UniversalEditor.Compression/Modules/LZX/Internal/Constants.cs +++ b/Libraries/UniversalEditor.Compression/Modules/LZX/Internal/Constants.cs @@ -11,14 +11,6 @@ namespace UniversalEditor.Compression.Modules.LZX.Internal public const ushort MAX_MATCH = 257; public const ushort NUM_CHARS = 256; - public enum BLOCKTYPE - { - INVALID = 0, - VERBATIM = 1, - ALIGNED = 2, - UNCOMPRESSED = 3 - } - public const ushort PRETREE_NUM_ELEMENTS = 20; public const ushort ALIGNED_NUM_ELEMENTS = 8; public const ushort NUM_PRIMARY_LENGTHS = 7; diff --git a/Libraries/UniversalEditor.Compression/Modules/LZX/LZXBlockType.cs b/Libraries/UniversalEditor.Compression/Modules/LZX/LZXBlockType.cs new file mode 100644 index 00000000..2dbac620 --- /dev/null +++ b/Libraries/UniversalEditor.Compression/Modules/LZX/LZXBlockType.cs @@ -0,0 +1,31 @@ +// +// LZXBlockType.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2020 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.Compression.Modules.LZX +{ + public enum LZXBlockType + { + Invalid = 0, + Verbatim = 1, + Aligned = 2, + Uncompressed = 3 + } +} diff --git a/Libraries/UniversalEditor.Compression/Modules/LZX/LZXCompressionModule.cs b/Libraries/UniversalEditor.Compression/Modules/LZX/LZXCompressionModule.cs index 5ee187df..ddbade91 100644 --- a/Libraries/UniversalEditor.Compression/Modules/LZX/LZXCompressionModule.cs +++ b/Libraries/UniversalEditor.Compression/Modules/LZX/LZXCompressionModule.cs @@ -105,7 +105,7 @@ namespace UniversalEditor.Compression.Modules.LZX m_state.header_read = 0; m_state.frames_read = 0; m_state.block_remaining = 0; - m_state.block_type = Constants.BLOCKTYPE.INVALID; + m_state.block_type = LZXBlockType.Invalid; m_state.intel_curpos = 0; m_state.intel_started = 0; @@ -168,28 +168,28 @@ namespace UniversalEditor.Compression.Modules.LZX if (m_state.block_remaining == 0) { // TODO may screw something up here - if (m_state.block_type == Constants.BLOCKTYPE.UNCOMPRESSED) + if (m_state.block_type == LZXBlockType.Uncompressed) { if ((m_state.block_length & 1) == 1) inputStream.ReadByte(); /* realign bitstream to word */ bitbuf.InitBitStream(); } - m_state.block_type = (Constants.BLOCKTYPE)bitbuf.ReadBits(3); ; + m_state.block_type = (LZXBlockType)bitbuf.ReadBits(3); ; i = bitbuf.ReadBits(16); j = bitbuf.ReadBits(8); m_state.block_remaining = m_state.block_length = (uint)((i << 8) | j); switch (m_state.block_type) { - case Constants.BLOCKTYPE.ALIGNED: + case LZXBlockType.Aligned: { for (i = 0, j = 0; i < 8; i++) { j = bitbuf.ReadBits(3); m_state.ALIGNED_len[i] = (byte)j; } MakeDecodeTable(Constants.ALIGNED_MAXSYMBOLS, Constants.ALIGNED_TABLEBITS, m_state.ALIGNED_len, m_state.ALIGNED_table); /* rest of aligned header is same as verbatim */ - goto case Constants.BLOCKTYPE.VERBATIM; + goto case LZXBlockType.Verbatim; } - case Constants.BLOCKTYPE.VERBATIM: + case LZXBlockType.Verbatim: { ReadLengths(m_state.MAINTREE_len, 0, 256, bitbuf); ReadLengths(m_state.MAINTREE_len, 256, m_state.main_elements, bitbuf); @@ -202,7 +202,7 @@ namespace UniversalEditor.Compression.Modules.LZX m_state.LENGTH_len, m_state.LENGTH_table); break; } - case Constants.BLOCKTYPE.UNCOMPRESSED: + case LZXBlockType.Uncompressed: { m_state.intel_started = 1; /* because we can't assume otherwise */ bitbuf.EnsureBits(16); /* get up to 16 pad bits into the buffer */ @@ -256,7 +256,7 @@ namespace UniversalEditor.Compression.Modules.LZX switch (m_state.block_type) { - case Constants.BLOCKTYPE.VERBATIM: + case LZXBlockType.Verbatim: { while (this_run > 0) { @@ -347,7 +347,7 @@ namespace UniversalEditor.Compression.Modules.LZX } break; } - case Constants.BLOCKTYPE.ALIGNED: + case LZXBlockType.Aligned: { while (this_run > 0) { @@ -461,7 +461,7 @@ namespace UniversalEditor.Compression.Modules.LZX } break; } - case Constants.BLOCKTYPE.UNCOMPRESSED: + case LZXBlockType.Uncompressed: { if ((inputStream.Position + this_run) > endpos) throw new EndOfStreamException(); @@ -742,7 +742,7 @@ namespace UniversalEditor.Compression.Modules.LZX public uint R0, R1, R2; /* for the LRU offset system */ public ushort main_elements; /* number of main tree elements */ public int header_read; /* have we started decoding at all yet? */ - public Constants.BLOCKTYPE block_type; /* type of this block */ + public LZXBlockType block_type; /* type of this block */ public uint block_length; /* uncompressed length of this block */ public uint block_remaining; /* uncompressed bytes still left to decode */ public uint frames_read; /* the number of CFDATA blocks processed */ diff --git a/Libraries/UniversalEditor.Compression/Modules/XMemLZX/XMemLZXCompressionModule.cs b/Libraries/UniversalEditor.Compression/Modules/XMemLZX/XMemLZXCompressionModule.cs index 692f3d13..cfbc5cae 100644 --- a/Libraries/UniversalEditor.Compression/Modules/XMemLZX/XMemLZXCompressionModule.cs +++ b/Libraries/UniversalEditor.Compression/Modules/XMemLZX/XMemLZXCompressionModule.cs @@ -25,10 +25,6 @@ namespace UniversalEditor.Compression.Modules.XMemLZX private const int LZX_MIN_MATCH = 2; private const int LZX_MAX_MATCH = 257; private const int LZX_NUM_CHARS = 256; - private const int LZX_BLOCKTYPE_INVALID = 0; /* also blocktypes 4-7 invalid */ - private const int LZX_BLOCKTYPE_VERBATIM = 1; - private const int LZX_BLOCKTYPE_ALIGNED = 2; - private const int LZX_BLOCKTYPE_UNCOMPRESSED = 3; private const int LZX_PRETREE_NUM_ELEMENTS = 20; private const int LZX_ALIGNED_NUM_ELEMENTS = 8; /* aligned offset tree #elements */ private const int LZX_NUM_PRIMARY_LENGTHS = 7; /* this one missing from spec! */ @@ -57,7 +53,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX public ULONG R0, R1, R2; /* for the LRU offset system */ public UWORD main_elements; /* number of main tree elements */ public int header_read; /* have we started decoding at all yet? */ - public ULONG block_type; /* type of this block */ + public LZX.LZXBlockType block_type; /* type of this block */ public ULONG block_length; /* uncompressed length of this block */ public ULONG block_remaining; /* uncompressed bytes still left to decode */ public ULONG frames_read; /* the number of CFDATA blocks processed */ @@ -181,7 +177,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX pState.header_read = 0; pState.frames_read = 0; pState.block_remaining = 0; - pState.block_type = LZX_BLOCKTYPE_INVALID; + pState.block_type = LZX.LZXBlockType.Invalid; pState.intel_curpos = 0; pState.intel_started = 0; pState.window_posn = 0; @@ -206,7 +202,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX pState.header_read = 0; pState.frames_read = 0; pState.block_remaining = 0; - pState.block_type = LZX_BLOCKTYPE_INVALID; + pState.block_type = LZX.LZXBlockType.Invalid; pState.intel_curpos = 0; pState.intel_started = 0; pState.window_posn = 0; @@ -550,20 +546,23 @@ namespace UniversalEditor.Compression.Modules.XMemLZX /* last block finished, new block expected */ if (pState.block_remaining == 0) { - if (pState.block_type == LZX_BLOCKTYPE_UNCOMPRESSED) + if (pState.block_type == LZX.LZXBlockType.Uncompressed) { if (Convert.ToBoolean(pState.block_length & 1)) inpos++; /* realign bitstream to word */ INIT_BITSTREAM(ref bitsleft, ref bitbuf); } - READ_BITS(ref pState.block_type, 3, ref bitbuf, ref bitsleft, ref ip, ref inpos); + ULONG ulBlockType = (ULONG)pState.block_type; + READ_BITS(ref ulBlockType, 3, ref bitbuf, ref bitsleft, ref ip, ref inpos); + pState.block_type = (LZX.LZXBlockType)ulBlockType; + READ_BITS(ref i, 16, ref bitbuf, ref bitsleft, ref ip, ref inpos); READ_BITS(ref j, 8, ref bitbuf, ref bitsleft, ref ip, ref inpos); pState.block_remaining = pState.block_length = (i << 8) | j; switch (pState.block_type) { - case LZX_BLOCKTYPE_ALIGNED: + case LZX.LZXBlockType.Aligned: for (i = 0; i < 8; i++) { READ_BITS(ref j, 3, ref bitbuf, ref bitsleft, ref ip, ref inpos); @@ -573,9 +572,9 @@ namespace UniversalEditor.Compression.Modules.XMemLZX LZX_ALIGNED_TABLEBITS, LZX_ALIGNED_MAXSYMBOLS)) throw new InvalidOperationException("BUILD_TABLE failed"); /* rest of aligned header is same as verbatim */ - goto case LZX_BLOCKTYPE_VERBATIM; + goto case LZX.LZXBlockType.Verbatim; - case LZX_BLOCKTYPE_VERBATIM: + case LZX.LZXBlockType.Verbatim: if (!READ_LENGTHS(0, 256, lb, ref bitbuf, ref bitsleft, ref ip, ref inpos, pState, ref pState.MAINTREE_len)) throw new InvalidOperationException("READ_LENGTHS failed"); @@ -595,7 +594,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX throw new InvalidOperationException("BUILD_TABLE failed"); break; - case LZX_BLOCKTYPE_UNCOMPRESSED: + case LZX.LZXBlockType.Uncompressed: pState.intel_started = 1; /* because we can't assume otherwise */ ENSURE_BITS(ref bitsleft, ref bitbuf, ref ip, ref inpos, 16); /* get up to 16 pad bits into the buffer */ if (bitsleft > 16) inpos -= 2; /* and align the bitstream! */ @@ -639,7 +638,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX switch (pState.block_type) { - case LZX_BLOCKTYPE_VERBATIM: + case LZX.LZXBlockType.Verbatim: while (this_run > 0) { hr = READ_HUFFSYM(ref pState.MAINTREE_table, ref hufftbl, ref bitsleft, @@ -724,7 +723,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX } break; - case LZX_BLOCKTYPE_ALIGNED: + case LZX.LZXBlockType.Aligned: while (this_run > 0) { hr = READ_HUFFSYM(ref pState.MAINTREE_table, ref hufftbl, ref bitsleft, @@ -836,7 +835,7 @@ namespace UniversalEditor.Compression.Modules.XMemLZX } break; - case LZX_BLOCKTYPE_UNCOMPRESSED: + case LZX.LZXBlockType.Uncompressed: if ((inpos + this_run) > endinp) throw new InvalidOperationException("eieio");; Array.Copy(ip, inpos, window, window_posn, this_run); inpos += this_run; window_posn += (ULONG)this_run; diff --git a/Libraries/UniversalEditor.Compression/UniversalEditor.Compression.csproj b/Libraries/UniversalEditor.Compression/UniversalEditor.Compression.csproj index 9097b147..c0af76a3 100644 --- a/Libraries/UniversalEditor.Compression/UniversalEditor.Compression.csproj +++ b/Libraries/UniversalEditor.Compression/UniversalEditor.Compression.csproj @@ -126,6 +126,7 @@ +