(HuffmanDecode): clean-up reverse-engineered stuff.

This commit is contained in:
morkt 2015-04-20 14:06:20 +04:00
parent 0a3fe37bb0
commit 167d9b53a1

View File

@ -277,12 +277,12 @@ namespace GameRes.Formats
ushort[] lhs = new ushort[512]; ushort[] lhs = new ushort[512];
ushort[] rhs = new ushort[512]; ushort[] rhs = new ushort[512];
ushort t1032 = 256; ushort token = 256;
int input_pos; int input_pos;
int remaining; int remaining;
int t8; int m_cached_bits;
int t12; int m_cache;
public HuffmanDecoder (byte[] src, int index, int length, byte[] dst) public HuffmanDecoder (byte[] src, int index, int length, byte[] dst)
{ {
@ -290,8 +290,8 @@ namespace GameRes.Formats
m_dst = dst; m_dst = dst;
input_pos = index; input_pos = index;
remaining = length; remaining = length;
t8 = 0; m_cached_bits = 0;
t12 = 0; m_cache = 0;
} }
public HuffmanDecoder (byte[] src, byte[] dst) : this (src, 0, src.Length, dst) public HuffmanDecoder (byte[] src, byte[] dst) : this (src, 0, src.Length, dst)
@ -300,50 +300,32 @@ namespace GameRes.Formats
public byte[] Unpack () public byte[] Unpack ()
{ {
int a2 = 0; int dst = 0;
t1032 = 256; token = 256;
ushort v3 = sub_401540(); ushort v3 = CreateTree();
ushort v13 = v3; // [sp+0h] [bp-4h]@1 while (dst < m_dst.Length)
while (a2 < m_dst.Length)
{ {
ushort v6 = v3; ushort symbol = v3;
if ( v6 >= 0x100u ) while ( symbol >= 0x100u )
{ {
do if ( 0 != GetBits (1) )
{ symbol = rhs[symbol];
while ( 0 == t8 ) else
{ symbol = lhs[symbol];
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; m_dst[dst++] = (byte)symbol;
} }
return m_dst; return m_dst;
} }
ushort sub_401540() ushort CreateTree()
{ {
if ( 0 != GetBits (1) ) if ( 0 != GetBits (1) )
{ {
ushort v4 = t1032++; ushort v = token++;
lhs[v4] = sub_401540(); lhs[v] = CreateTree();
rhs[v4] = sub_401540(); rhs[v] = CreateTree();
return v4; return v;
} }
else else
{ {
@ -353,18 +335,17 @@ namespace GameRes.Formats
uint GetBits (int n) uint GetBits (int n)
{ {
while ( n > t8 ) while ( n > m_cached_bits )
{ {
int v4 = m_src[input_pos++]; int v = m_src[input_pos++];
--remaining; --remaining;
t12 = v4 | (t12 << 8); m_cache = v | (m_cache << 8);
t8 += 8; m_cached_bits += 8;
} }
int v7 = t12; uint mask = (uint)m_cache;
uint v8 = (uint)t12; m_cached_bits -= n;
t8 -= n; m_cache &= ~(-1 << m_cached_bits);
t12 = v7 & ~(-1 << t8); return (uint)(((-1 << m_cached_bits) & mask) >> m_cached_bits);
return (uint)(((-1 << t8) & v8) >> t8);
} }
} }
} }