(BinOpener): refined LZW decompression.

This commit is contained in:
morkt 2015-12-20 09:27:30 +04:00
parent d2e7882e6d
commit b592a7dd52

View File

@ -50,7 +50,7 @@ namespace GameRes.Formats.FVP
if (!file.View.AsciiEqual (4, "PK01")) if (!file.View.AsciiEqual (4, "PK01"))
return null; return null;
int count = file.View.ReadInt32 (8); int count = file.View.ReadInt32 (8);
if (count <= 0 || count > 0xfffff) if (!IsSaneCount (count))
return null; return null;
long index_offset = 0x0c; long index_offset = 0x0c;
uint index_size = (uint)(0x28 * count); uint index_size = (uint)(0x28 * count);
@ -73,117 +73,88 @@ namespace GameRes.Formats.FVP
public override Stream OpenEntry (ArcFile arc, Entry entry) public override Stream OpenEntry (ArcFile arc, Entry entry)
{ {
var entry_offset = entry.Offset; if (!(entry.Size > 8 && arc.File.View.AsciiEqual (entry.Offset, "acp\0")))
var input = new ArcView.Frame (arc.File, entry_offset, entry.Size); return base.OpenEntry (arc, entry);
try int unpacked_size = Binary.BigEndian (arc.File.View.ReadInt32 (entry.Offset+4));
using (var input = arc.File.CreateStream (entry.Offset+8, entry.Size-8))
using (var decoder = new LzwDecoder (input, unpacked_size))
{ {
if (entry.Size > 8 && input.AsciiEqual (entry_offset, "acp\0")) decoder.Unpack();
{ return new MemoryStream (decoder.Output);
using (var decoder = new LzwDecoder (input))
{
decoder.Unpack();
return new MemoryStream (decoder.Output, false);
}
}
return new ArcView.ArcStream (input, entry_offset, entry.Size);
}
catch
{
input.Dispose();
throw;
} }
} }
} }
internal sealed class LzwDecoder : IDisposable internal sealed class LzwDecoder : IDisposable
{ {
private Stream m_input; private MsbBitStream m_input;
private byte[] m_output; private byte[] m_output;
public byte[] Output { get { return m_output; } } public byte[] Output { get { return m_output; } }
uint m_dst_size;
public LzwDecoder (ArcView.Frame input) public LzwDecoder (Stream input, int unpacked_size)
{ {
m_dst_size = Binary.BigEndian (input.ReadUInt32 (input.Offset+4)); m_input = new MsbBitStream (input, true);
m_input = new ArcView.ArcStream (input, input.Offset+8, input.Reserved-8); m_output = new byte[unpacked_size];
m_output = new byte[m_dst_size];
} }
public void Unpack () public void Unpack ()
{ {
int dst = 0; int dst = 0;
int bits = 0; var lzw_dict = new int[0x8900];
var buf = new int[0x8c00]; int token_width = 9;
uint dst_left = m_dst_size; int dict_pos = 0;
uint L0 = 0x800000; while (dst < m_output.Length)
uint edx = 0x102;
for (;;)
{ {
uint eax = L0; int token = m_input.GetBits (token_width);
bool hi = false; if (-1 == token)
while (!hi)
{
bits <<= 1;
if (0 == (bits&0xff))
{
bits = m_input.ReadByte();
if (-1 == bits)
throw new EndOfStreamException ("Invalid compressed stream");
bits = (bits << 1) | 1;
}
int next_bit = (bits >> 8) & 1;
hi = 0 != (eax & 0x80000000);
eax = (eax << 1) | (uint)next_bit;
}
if (0x100 == (eax & 0xff00) && 2 >= (eax & 0xff))
{
if (2 == (eax & 0xff))
{
L0 = 0x800000;
edx = 0x102;
continue;
}
if (0 == (eax & 0xff))
break;
L0 >>= 1;
if (0 != (L0 & 0xff00))
throw new EndOfStreamException ("Invalid compressed stream");
continue;
}
++edx;
if (edx >= 0x8c00)
throw new EndOfStreamException ("Invalid compressed stream"); throw new EndOfStreamException ("Invalid compressed stream");
buf[edx] = dst; else if (0x100 == token) // end of input
if (0 == (eax & 0xff00)) break;
else if (0x101 == token) // increase token width
{ {
if (0 == dst_left--) ++token_width;
throw new EndOfStreamException ("Invalid compressed stream"); if (token_width > 24)
m_output[dst++] = (byte)(eax & 0xff); throw new InvalidFormatException ("Invalid comressed stream");
}
else if (0x102 == token) // reset dictionary
{
token_width = 9;
dict_pos = 0;
} }
else else
{ {
if (eax >= edx) if (dict_pos >= lzw_dict.Length)
throw new EndOfStreamException ("Invalid compressed stream"); throw new InvalidFormatException ("Invalid comressed stream");
int src = buf[eax]; lzw_dict[dict_pos++] = dst;
int count = buf[eax+1] - src + 1; if (token < 0x100)
if (dst_left < count) {
throw new EndOfStreamException ("Invalid compressed stream"); m_output[dst++] = (byte)token;
dst_left -= (uint)count; }
Binary.CopyOverlapped (m_output, src, dst, count); else
dst += count; {
token -= 0x103;
if (token >= dict_pos)
throw new InvalidFormatException ("Invalid comressed stream");
int src = lzw_dict[token];
int count = Math.Min (m_output.Length-dst, lzw_dict[token+1] - src + 1);
if (count < 0)
throw new InvalidFormatException ("Invalid comressed stream");
Binary.CopyOverlapped (m_output, src, dst, count);
dst += count;
}
} }
} }
} }
#region IDisposable Members #region IDisposable Members
bool _disposed = false;
public void Dispose () public void Dispose ()
{ {
if (null != m_input) if (!_disposed)
{ {
m_input.Dispose(); m_input.Dispose();
m_input = null; _disposed = true;
} }
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }