From 75c3f8b61e5b2cecfb0dd8c3af0e79e9be131ddd Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 14 Apr 2015 10:28:00 +0400 Subject: [PATCH] (HuffmanDecoder): class made public and moved to ArcCommon.cs --- ArcFormats/ArcCommon.cs | 98 +++++++++++++++++++++++++++++++++++++++++ ArcFormats/ArcNexas.cs | 94 --------------------------------------- 2 files changed, 98 insertions(+), 94 deletions(-) diff --git a/ArcFormats/ArcCommon.cs b/ArcFormats/ArcCommon.cs index 4a85e9d5..1c914760 100644 --- a/ArcFormats/ArcCommon.cs +++ b/ArcFormats/ArcCommon.cs @@ -269,4 +269,102 @@ namespace GameRes.Formats } #endregion } + + public class HuffmanDecoder + { + byte[] m_src; + byte[] m_dst; + + ushort[] lhs = new ushort[512]; + ushort[] rhs = new ushort[512]; + ushort t1032 = 256; + + int input_pos; + int remaining; + int t8; + int t12; + + public HuffmanDecoder (byte[] src, int index, int length, byte[] dst) + { + m_src = src; + m_dst = dst; + input_pos = index; + remaining = length; + t8 = 0; + t12 = 0; + } + + public HuffmanDecoder (byte[] src, byte[] dst) : this (src, 0, src.Length, dst) + { + } + + public byte[] Unpack () + { + int a2 = 0; + t1032 = 256; + ushort v3 = sub_401540(); + ushort v13 = v3; // [sp+0h] [bp-4h]@1 + while (a2 < m_dst.Length) + { + ushort v6 = v3; + if ( v6 >= 0x100u ) + { + do + { + while ( 0 == t8 ) + { + int v8 = m_src[input_pos++]; + int v9 = t12; + t8 += 8; + --remaining; + t12 = v8 | (v9 << 8); + } + int v10 = t12; + int v11 = --t8; + uint v12 = (uint)t12; + t12 = v10 & ~(-1 << v11); + if ( 0 != (((-1 << v11) & v12) >> v11) ) + v6 = rhs[v6]; + else + v6 = lhs[v6]; + } + while ( v6 >= 0x100u ); + v3 = v13; + } + m_dst[a2++] = (byte)v6; + } + return m_dst; + } + + ushort sub_401540() + { + if ( 0 != GetBits (1) ) + { + ushort v4 = t1032++; + lhs[v4] = sub_401540(); + rhs[v4] = sub_401540(); + return v4; + } + else + { + return (ushort)GetBits (8); + } + } + + uint GetBits (int n) + { + while ( n > t8 ) + { + int v4 = m_src[input_pos++]; + --remaining; + t12 = v4 | (t12 << 8); + t8 += 8; + } + int v7 = t12; + uint v8 = (uint)t12; + t8 -= n; + t12 = v7 & ~(-1 << t8); + return (uint)(((-1 << t8) & v8) >> t8); + } + } } diff --git a/ArcFormats/ArcNexas.cs b/ArcFormats/ArcNexas.cs index 8e565539..2bfc1917 100644 --- a/ArcFormats/ArcNexas.cs +++ b/ArcFormats/ArcNexas.cs @@ -146,98 +146,4 @@ namespace GameRes.Formats.NeXAS return decoder.Unpack(); } } - - internal class HuffmanDecoder - { - byte[] m_src; - byte[] m_dst; - - ushort[] lhs = new ushort[512]; - ushort[] rhs = new ushort[512]; - ushort t1032 = 256; - - int t0; - int remaining; - int t8; - int t12; - - public HuffmanDecoder (byte[] src, byte[] dst) - { - m_src = src; - m_dst = dst; - t0 = 0; - remaining = src.Length; - t8 = 0; - t12 = 0; - } - - public byte[] Unpack () - { - int a2 = 0; - t1032 = 256; - ushort v3 = sub_401540(); - ushort v13 = v3; // [sp+0h] [bp-4h]@1 - while (a2 < m_dst.Length) - { - ushort v6 = v3; - if ( v6 >= 0x100u ) - { - do - { - while ( 0 == t8 ) - { - int v8 = m_src[t0++]; - int v9 = t12; - t8 += 8; - --remaining; - t12 = v8 | (v9 << 8); - } - int v10 = t12; - int v11 = --t8; - uint v12 = (uint)t12; - t12 = v10 & ~(-1 << v11); - if ( 0 != (((-1 << v11) & v12) >> v11) ) - v6 = rhs[v6]; - else - v6 = lhs[v6]; - } - while ( v6 >= 0x100u ); - v3 = v13; - } - m_dst[a2++] = (byte)v6; - } - return m_dst; - } - - ushort sub_401540() - { - if ( 0 != GetBits (1) ) - { - ushort v4 = t1032++; - lhs[v4] = sub_401540(); - rhs[v4] = sub_401540(); - return v4; - } - else - { - return (ushort)GetBits (8); - } - } - - uint GetBits (int n) - { - while ( n > t8 ) - { - int v4 = m_src[t0++]; - --remaining; - t12 = v4 | (t12 << 8); - t8 += 8; - } - int v7 = t12; - uint v8 = (uint)t12; - t8 -= n; - t12 = v7 & ~(-1 << t8); - return (uint)(((-1 << t8) & v8) >> t8); - } - } }