implemented CPZ7 archives.

This commit is contained in:
morkt 2017-11-27 12:47:15 +04:00
parent ab5d1e03fc
commit ae0e966564
5 changed files with 356 additions and 102 deletions

View File

@ -104,6 +104,8 @@
<Compile Include="BlackRainbow\ArcSPPAK.cs" /> <Compile Include="BlackRainbow\ArcSPPAK.cs" />
<Compile Include="Cadath\ArcDAF.cs" /> <Compile Include="Cadath\ArcDAF.cs" />
<Compile Include="Cadath\ImageCGF.cs" /> <Compile Include="Cadath\ImageCGF.cs" />
<Compile Include="Cmvs\CpzHeader.cs" />
<Compile Include="Cmvs\HuffmanDecoder.cs" />
<Compile Include="Crowd\ImageCRZ.cs" /> <Compile Include="Crowd\ImageCRZ.cs" />
<Compile Include="CsWare\ImageBPC.cs" /> <Compile Include="CsWare\ImageBPC.cs" />
<Compile Include="Cyberworks\ArcApp.cs" /> <Compile Include="Cyberworks\ArcApp.cs" />

View File

@ -2,7 +2,7 @@
//! \date Tue Nov 24 11:27:23 2015 //! \date Tue Nov 24 11:27:23 2015
//! \brief Purple Software resource archive. //! \brief Purple Software resource archive.
// //
// Copyright (C) 2015-2016 by morkt // Copyright (C) 2015-2017 by morkt
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
@ -29,7 +29,6 @@ using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using GameRes.Utility; using GameRes.Utility;
using System.Security.Cryptography;
namespace GameRes.Formats.Purple namespace GameRes.Formats.Purple
{ {
@ -41,6 +40,7 @@ namespace GameRes.Formats.Purple
public Cmvs.Md5Variant Md5Variant; public Cmvs.Md5Variant Md5Variant;
public uint DecoderFactor; public uint DecoderFactor;
public uint EntryInitKey; public uint EntryInitKey;
public uint EntrySubKey = 0x5C29E87B;
public byte EntryTailKey; public byte EntryTailKey;
public byte EntryKeyPos = 9; public byte EntryKeyPos = 9;
public uint IndexSeed = 0x2A65CB4E; public uint IndexSeed = 0x2A65CB4E;
@ -57,17 +57,6 @@ namespace GameRes.Formats.Purple
public uint Key; public uint Key;
} }
internal class CpzHeader
{
public int DirCount;
public int DirEntriesSize;
public int FileEntriesSize;
public uint[] CmvsMd5 = new uint[4];
public uint MasterKey;
public bool IsEncrypted;
public uint EntryKey;
}
internal class CpzArchive : ArcFile internal class CpzArchive : ArcFile
{ {
public CpzHeader Header; public CpzHeader Header;
@ -98,7 +87,7 @@ namespace GameRes.Formats.Purple
public CpzOpener () public CpzOpener ()
{ {
Signatures = new uint[] { 0x355A5043, 0x365A5043 }; Signatures = new uint[] { 0x355A5043, 0x365A5043, 0x375A5043 };
} }
public static Dictionary<string, CmvsScheme> KnownSchemes = new Dictionary<string, CmvsScheme>(); public static Dictionary<string, CmvsScheme> KnownSchemes = new Dictionary<string, CmvsScheme>();
@ -113,75 +102,40 @@ namespace GameRes.Formats.Purple
{ {
if (null == KnownSchemes) if (null == KnownSchemes)
throw new OperationCanceledException ("Outdated encryption schemes database"); throw new OperationCanceledException ("Outdated encryption schemes database");
var header = file.View.ReadBytes (0, 0x3C);
var checksum = file.View.ReadUInt32 (0x3C); var cpz = CpzHeader.Parse (file);
if (checksum != CheckSum (header, 0, 0x3C, 0x923A564Cu)) if (null == cpz)
return null;
int version = header[3] - '0';
CpzHeader cpz = 5 == version ? ReadHeaderV5 (header) : ReadHeaderV6 (header);
int index_size = cpz.DirEntriesSize + cpz.FileEntriesSize;
var index = file.View.ReadBytes (0x40, (uint)index_size);
if (index.Length != index_size)
return null; return null;
using (var md5 = MD5.Create()) var index = file.View.ReadBytes (cpz.IndexOffset, cpz.IndexSize);
if (!cpz.VerifyIndex (index))
return null;
int file_table_size = cpz.DirEntriesSize + cpz.FileEntriesSize;
if (cpz.IndexKeySize > 24)
{ {
var hash = md5.ComputeHash (index); var index_key = UnpackIndexKey (index, file_table_size, cpz.IndexKeySize);
if (!header.Skip (0x10).Take (0x10).SequenceEqual (hash)) for (int i = 0; i < file_table_size; ++i)
return null; {
index[i] ^= index_key[(i + 3) % 0x3FF];
}
} }
var index_copy = new CowArray<byte> (index, 0, file_table_size).ToArray();
var cmvs_md5 = cpz.CmvsMd5.Clone() as uint[]; var cmvs_md5 = cpz.CmvsMd5.Clone() as uint[];
foreach (var scheme in KnownSchemes.Values.Where (s => s.Version == version)) foreach (var scheme in KnownSchemes.Values.Where (s => s.Version == cpz.Version))
{ {
var arc = ReadIndex (file, scheme, cpz, index); var arc = ReadIndex (file, scheme, cpz, index);
if (null != arc) if (null != arc)
return arc; return arc;
// both CmvsMd5 and index was altered by ReadIndex in decryption attempt // both CmvsMd5 and index was altered by ReadIndex in decryption attempt
Array.Copy (cmvs_md5, cpz.CmvsMd5, 4); Array.Copy (cmvs_md5, cpz.CmvsMd5, 4);
file.View.Read (0x40, index, 0, (uint)index_size); Array.Copy (index, index_copy, file_table_size);
} }
throw new UnknownEncryptionScheme(); throw new UnknownEncryptionScheme();
} }
CpzHeader ReadHeaderV5 (byte[] header) internal ArcFile ReadIndex (ArcView file, CmvsScheme scheme, CpzHeader cpz, byte[] index)
{
uint entry_key = 0x37ACF831 ^ LittleEndian.ToUInt32 (header, 0x38);
var cpz = new CpzHeader
{
DirCount = -0x1C5AC27 ^ LittleEndian.ToInt32 (header, 4),
DirEntriesSize = 0x37F298E7 ^ LittleEndian.ToInt32 (header, 8),
FileEntriesSize = 0x7A6F3A2C ^ LittleEndian.ToInt32 (header, 0x0C),
MasterKey = 0xAE7D39BF ^ LittleEndian.ToUInt32 (header, 0x30),
IsEncrypted = 0 != (0xFB73A955 ^ LittleEndian.ToUInt32 (header, 0x34)),
EntryKey = 0,
};
cpz.CmvsMd5[0] = 0x43DE7C19 ^ LittleEndian.ToUInt32 (header, 0x20);
cpz.CmvsMd5[1] = 0xCC65F415 ^ LittleEndian.ToUInt32 (header, 0x24);
cpz.CmvsMd5[2] = 0xD016A93C ^ LittleEndian.ToUInt32 (header, 0x28);
cpz.CmvsMd5[3] = 0x97A3BA9A ^ LittleEndian.ToUInt32 (header, 0x2C);
return cpz;
}
CpzHeader ReadHeaderV6 (byte[] header)
{
uint entry_key = 0x37ACF832 ^ LittleEndian.ToUInt32 (header, 0x38);
var cpz = new CpzHeader
{
DirCount = -0x1C5AC26 ^ LittleEndian.ToInt32 (header, 4),
DirEntriesSize = 0x37F298E8 ^ LittleEndian.ToInt32 (header, 8),
FileEntriesSize = 0x7A6F3A2D ^ LittleEndian.ToInt32 (header, 0x0C),
MasterKey = 0xAE7D39B7 ^ LittleEndian.ToUInt32 (header, 0x30),
IsEncrypted = 0 != (0xFB73A956 ^ LittleEndian.ToUInt32 (header, 0x34)),
EntryKey = 0x7DA8F173 * Binary.RotR (entry_key, 5) + 0x13712765,
};
cpz.CmvsMd5[0] = 0x43DE7C1A ^ LittleEndian.ToUInt32 (header, 0x20);
cpz.CmvsMd5[1] = 0xCC65F416 ^ LittleEndian.ToUInt32 (header, 0x24);
cpz.CmvsMd5[2] = 0xD016A93D ^ LittleEndian.ToUInt32 (header, 0x28);
cpz.CmvsMd5[3] = 0x97A3BA9B ^ LittleEndian.ToUInt32 (header, 0x2C);
return cpz;
}
ArcFile ReadIndex (ArcView file, CmvsScheme scheme, CpzHeader cpz, byte[] index)
{ {
var cmvs_md5 = Cmvs.MD5.Create (scheme.Md5Variant); var cmvs_md5 = Cmvs.MD5.Create (scheme.Md5Variant);
cmvs_md5.Compute (cpz.CmvsMd5); cmvs_md5.Compute (cpz.CmvsMd5);
@ -200,7 +154,7 @@ namespace GameRes.Formats.Purple
DecryptIndexDirectory (index, cpz.DirEntriesSize, key); DecryptIndexDirectory (index, cpz.DirEntriesSize, key);
decoder.Init (cpz.MasterKey, cpz.CmvsMd5[2]); decoder.Init (cpz.MasterKey, cpz.CmvsMd5[2]);
int base_offset = 0x40 + index.Length; uint base_offset = cpz.IndexOffset + cpz.IndexSize;
int dir_offset = 0; int dir_offset = 0;
var dir = new List<Entry>(); var dir = new List<Entry>();
for (int i = 0; i < cpz.DirCount; ++i) for (int i = 0; i < cpz.DirCount; ++i)
@ -239,16 +193,20 @@ namespace GameRes.Formats.Purple
for (int j = 0; j < file_count; ++j) for (int j = 0; j < file_count; ++j)
{ {
int entry_size = LittleEndian.ToInt32 (index, cur_offset); int entry_size = LittleEndian.ToInt32 (index, cur_offset);
if (entry_size > index.Length || entry_size <= 0x18) if (entry_size > index.Length || entry_size <= cpz.EntryNameOffset)
return null; return null;
var name = Binary.GetCString (index, cur_offset+0x18, cur_entries_end-(cur_offset+0x18)); int name_offset = cur_offset + cpz.EntryNameOffset;
var name = Binary.GetCString (index, name_offset, cur_entries_end - name_offset);
if (!is_root_dir) if (!is_root_dir)
name = Path.Combine (dir_name, name); name = Path.Combine (dir_name, name);
var entry = FormatCatalog.Instance.Create<CpzEntry> (name); var entry = FormatCatalog.Instance.Create<CpzEntry> (name);
entry.Offset = LittleEndian.ToInt64 (index, cur_offset+4) + base_offset; entry.Offset = LittleEndian.ToInt64 (index, cur_offset+4) + base_offset;
entry.Size = LittleEndian.ToUInt32 (index, cur_offset+0xC); entry.Size = LittleEndian.ToUInt32 (index, cur_offset+0xC);
entry.CheckSum = LittleEndian.ToUInt32 (index, cur_offset+0x10); int key_offset = cur_offset + 0x10;
entry.Key = LittleEndian.ToUInt32 (index, cur_offset+0x14) + dir_key; if (cpz.IsLongSize)
key_offset += 4;
entry.CheckSum = LittleEndian.ToUInt32 (index, key_offset);
entry.Key = LittleEndian.ToUInt32 (index, key_offset+4) + dir_key;
if (!entry.CheckPlacement (file.MaxOffset)) if (!entry.CheckPlacement (file.MaxOffset))
return null; return null;
dir.Add (entry); dir.Add (entry);
@ -271,7 +229,8 @@ namespace GameRes.Formats.Purple
var data = carc.File.View.ReadBytes (entry.Offset, entry.Size); var data = carc.File.View.ReadBytes (entry.Offset, entry.Size);
if (carc.Header.IsEncrypted) if (carc.Header.IsEncrypted)
{ {
uint key = (carc.Header.MasterKey ^ cent.Key) + (uint)carc.Header.DirCount - 0x5C29E87B; uint key = (carc.Header.MasterKey ^ cent.Key) + (uint)carc.Header.DirCount;
key -= carc.Decoder.Scheme.EntrySubKey;
key ^= carc.Header.EntryKey; key ^= carc.Header.EntryKey;
carc.Decoder.DecryptEntry (data, carc.Header.CmvsMd5, key); carc.Decoder.DecryptEntry (data, carc.Header.CmvsMd5, key);
} }
@ -282,6 +241,21 @@ namespace GameRes.Formats.Purple
return new BinMemoryStream (data, entry.Name); return new BinMemoryStream (data, entry.Name);
} }
internal byte[] UnpackIndexKey (byte[] data, int offset, int length)
{
int key_offset = offset + 20;
int packed_offset = offset + 24;
int packed_length = length - 24;
for (int i = 0; i < packed_length; ++i)
{
data[packed_offset + i] ^= data[key_offset + (i & 3)];
}
int unpacked_length = data.ToInt32 (offset + 16);
var output = new byte[unpacked_length];
var decoder = new HuffmanDecoder (data, packed_offset, packed_length, output);
return decoder.Unpack();
}
void DecryptIndexStage1 (byte[] data, uint key, CmvsScheme scheme) void DecryptIndexStage1 (byte[] data, uint key, CmvsScheme scheme)
{ {
var secret = scheme.Cpz5Secret; var secret = scheme.Cpz5Secret;
@ -367,33 +341,6 @@ namespace GameRes.Formats.Purple
} }
} }
public static uint CheckSum (byte[] data, int index, int length, uint crc)
{
if (null == data)
throw new ArgumentNullException ("data");
if (index < 0 || index > data.Length)
throw new ArgumentOutOfRangeException ("index");
if (length < 0 || length > data.Length || length > data.Length-index)
throw new ArgumentException ("length");
int dwords = length / 4;
if (dwords > 0)
{
unsafe
{
fixed (byte* raw = &data[index])
{
uint* raw32 = (uint*)raw;
for (int i = 0; i < dwords; ++i)
crc += raw32[i];
}
}
index += length & -4;
}
for (int i = 0; i < (length & 3); ++i)
crc += data[index+i];
return crc;
}
byte[] UnpackPs2 (byte[] data) byte[] UnpackPs2 (byte[] data)
{ {
DecryptPs2 (data); DecryptPs2 (data);
@ -471,6 +418,8 @@ namespace GameRes.Formats.Purple
protected byte[] m_decode_table = new byte[0x100]; protected byte[] m_decode_table = new byte[0x100];
protected CmvsScheme m_scheme; protected CmvsScheme m_scheme;
public CmvsScheme Scheme { get { return m_scheme; } }
public Cpz5Decoder (CmvsScheme scheme, uint key, uint summand) public Cpz5Decoder (CmvsScheme scheme, uint key, uint summand)
{ {
m_scheme = scheme; m_scheme = scheme;

View File

@ -27,7 +27,7 @@ using GameRes.Utility;
namespace GameRes.Formats.Cmvs namespace GameRes.Formats.Cmvs
{ {
public enum Md5Variant { A, B, Chrono, Memoria, Natsu } public enum Md5Variant { A, B, Chrono, Memoria, Natsu, Aoi }
public abstract class MD5 : Cryptography.MD5Base public abstract class MD5 : Cryptography.MD5Base
{ {
@ -45,6 +45,7 @@ namespace GameRes.Formats.Cmvs
case Md5Variant.Chrono: return new Md5Chrono(); case Md5Variant.Chrono: return new Md5Chrono();
case Md5Variant.Memoria: return new Md5Memoria(); case Md5Variant.Memoria: return new Md5Memoria();
case Md5Variant.Natsu: return new Md5Natsu(); case Md5Variant.Natsu: return new Md5Natsu();
case Md5Variant.Aoi: return new Md5Aoi();
default: throw new System.ArgumentException ("Unknown MD5 variant", "variant"); default: throw new System.ArgumentException ("Unknown MD5 variant", "variant");
} }
} }
@ -151,4 +152,23 @@ namespace GameRes.Formats.Cmvs
data[3] = m_state[0] ^ 0xE3F9A742; data[3] = m_state[0] ^ 0xE3F9A742;
} }
} }
public class Md5Aoi : MD5
{
protected override void InitState ()
{
m_state[0] = 0xC74A2B02;
m_state[1] = 0xE7C8AB8F;
m_state[2] = 0x38BEBC4E;
m_state[3] = 0x7531A4C3;
}
protected override void SetResult (uint[] data)
{
data[0] = m_state[2] ^ 0x53A76D2E;
data[1] = m_state[1] + 0x5BB17FDA;
data[2] = m_state[0] + 0x6853E14D;
data[3] = m_state[3] ^ 0xF5C6A9A3;
}
}
} }

View File

@ -0,0 +1,175 @@
//! \file CpzHeader.cs
//! \date 2017 Nov 27
//! \brief CPZ archives header class.
//
// Copyright (C) 2017 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using GameRes.Utility;
namespace GameRes.Formats.Purple
{
internal class CpzHeader
{
public int Version;
public int DirCount;
public int DirEntriesSize;
public int FileEntriesSize;
public uint[] CmvsMd5 = new uint[4];
public uint MasterKey;
public bool IsEncrypted;
public uint EntryKey;
public int IndexKeySize;
public uint Checksum;
public uint InitChecksum = 0x923A564Cu;
public uint IndexOffset;
public uint IndexSize;
public IEnumerable<byte> IndexMd5;
public int EntryNameOffset;
public bool IsLongSize { get { return Version > 6; } }
public static CpzHeader Parse (ArcView file)
{
var cpz = new CpzHeader { Version = file.View.ReadByte (3) - '0' };
int checksum_length;
byte[] header;
if (cpz.Version < 7)
{
header = file.View.ReadBytes (0, 0x40);
if (cpz.Version < 6)
cpz.ParseV5 (header);
else
cpz.ParseV6 (header);
cpz.IndexOffset = 0x40;
cpz.IndexSize = (uint)(cpz.DirEntriesSize + cpz.FileEntriesSize);
cpz.Checksum = header.ToUInt32 (0x3C);
checksum_length = 0x3C;
}
else
{
header = file.View.ReadBytes (0, 0x48);
cpz.ParseV7 (header);
cpz.IndexOffset = 0x48;
cpz.IndexSize = (uint)(cpz.DirEntriesSize + cpz.FileEntriesSize + cpz.IndexKeySize);
cpz.Checksum = header.ToUInt32 (0x44);
checksum_length = 0x40;
}
if (cpz.Checksum != CheckSum (header, 0, checksum_length, cpz.InitChecksum))
return null;
cpz.IndexMd5 = header.Skip (0x10).Take (0x10).ToArray();
return cpz;
}
private void ParseV5 (byte[] header)
{
DirCount = -0x1C5AC27 ^ LittleEndian.ToInt32 (header, 4);
DirEntriesSize = 0x37F298E7 ^ LittleEndian.ToInt32 (header, 8);
FileEntriesSize = 0x7A6F3A2C ^ LittleEndian.ToInt32 (header, 0x0C);
MasterKey = 0xAE7D39BF ^ LittleEndian.ToUInt32 (header, 0x30);
IsEncrypted = 0 != (0xFB73A955 ^ LittleEndian.ToUInt32 (header, 0x34));
EntryKey = 0;
EntryNameOffset = 0x18;
CmvsMd5[0] = 0x43DE7C19 ^ LittleEndian.ToUInt32 (header, 0x20);
CmvsMd5[1] = 0xCC65F415 ^ LittleEndian.ToUInt32 (header, 0x24);
CmvsMd5[2] = 0xD016A93C ^ LittleEndian.ToUInt32 (header, 0x28);
CmvsMd5[3] = 0x97A3BA9A ^ LittleEndian.ToUInt32 (header, 0x2C);
}
private void ParseV6 (byte[] header)
{
uint entry_key = 0x37ACF832 ^ LittleEndian.ToUInt32 (header, 0x38);
DirCount = -0x1C5AC26 ^ LittleEndian.ToInt32 (header, 4);
DirEntriesSize = 0x37F298E8 ^ LittleEndian.ToInt32 (header, 8);
FileEntriesSize = 0x7A6F3A2D ^ LittleEndian.ToInt32 (header, 0x0C);
MasterKey = 0xAE7D39B7 ^ LittleEndian.ToUInt32 (header, 0x30);
IsEncrypted = 0 != (0xFB73A956 ^ LittleEndian.ToUInt32 (header, 0x34));
EntryKey = 0x7DA8F173 * Binary.RotR (entry_key, 5) + 0x13712765;
EntryNameOffset = 0x18;
CmvsMd5[0] = 0x43DE7C1A ^ LittleEndian.ToUInt32 (header, 0x20);
CmvsMd5[1] = 0xCC65F416 ^ LittleEndian.ToUInt32 (header, 0x24);
CmvsMd5[2] = 0xD016A93D ^ LittleEndian.ToUInt32 (header, 0x28);
CmvsMd5[3] = 0x97A3BA9B ^ LittleEndian.ToUInt32 (header, 0x2C);
}
private void ParseV7 (byte[] header)
{
ParseV6 (header);
var index_key_size = LittleEndian.ToInt32 (header, 0x40);
IndexKeySize = 0x65EF99F3 ^ index_key_size;
InitChecksum = (uint)index_key_size - 0x6DC5A9B4u;
EntryNameOffset = 0x1C;
}
public bool VerifyIndex (byte[] index)
{
if (index.Length != (int)IndexSize)
return false;
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash (index);
if (!hash.SequenceEqual (IndexMd5))
return false;
if (Version > 6 && IndexKeySize > 0x10)
{
int index_size = DirEntriesSize + FileEntriesSize;
hash = md5.ComputeHash (index, index_size+0x10, IndexKeySize-0x10);
if (!hash.SequenceEqual (index.Skip (index_size).Take (0x10)))
return false;
}
return true;
}
}
public static uint CheckSum (byte[] data, int index, int length, uint crc)
{
if (null == data)
throw new ArgumentNullException ("data");
if (index < 0 || index > data.Length)
throw new ArgumentOutOfRangeException ("index");
if (length < 0 || length > data.Length || length > data.Length-index)
throw new ArgumentException ("length");
int dwords = length / 4;
if (dwords > 0)
{
unsafe
{
fixed (byte* raw = &data[index])
{
uint* raw32 = (uint*)raw;
for (int i = 0; i < dwords; ++i)
crc += raw32[i];
}
}
index += length & -4;
}
for (int i = 0; i < (length & 3); ++i)
crc += data[index+i];
return crc;
}
}
}

View File

@ -0,0 +1,108 @@
//! \file HuffmanDecoder.cs
//! \date 2017 Nov 27
//! \brief Custom Huffman decoder for CPZ archives.
//
// Copyright (C) 2017 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System.IO;
using GameRes.Utility;
namespace GameRes.Formats.Purple
{
internal sealed class HuffmanDecoder
{
byte[] m_input;
byte[] m_output;
int m_src;
int m_bits;
int m_bit_count;
ushort[] lhs = new ushort[512];
ushort[] rhs = new ushort[512];
ushort token = 256;
public HuffmanDecoder (byte[] src, int index, int length, byte[] dst)
{
m_input = src;
m_output = dst;
m_src = index;
m_bit_count = 0;
}
public byte[] Unpack ()
{
int dst = 0;
token = 256;
ushort root = CreateTree();
while (dst < m_output.Length)
{
ushort symbol = root;
while (symbol >= 0x100)
{
if (0 != GetBits (1))
symbol = rhs[symbol];
else
symbol = lhs[symbol];
}
m_output[dst++] = (byte)symbol;
}
return m_output;
}
ushort CreateTree()
{
if (0 != GetBits (1))
{
ushort v = token++;
if (v >= 511)
throw new InvalidDataException ("Invalid compressed data");
lhs[v] = CreateTree();
rhs[v] = CreateTree();
return v;
}
else
{
return (ushort)GetBits (8);
}
}
int GetBits (int count)
{
int bits = 0;
while (count --> 0)
{
if (0 == m_bit_count)
{
m_bits = LittleEndian.ToInt32 (m_input, m_src);
m_src += 4;
m_bit_count = 32;
}
bits = bits << 1 | (m_bits & 1);
m_bits >>= 1;
--m_bit_count;
}
return bits;
}
}
}