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

@@ -2,7 +2,7 @@
//! \date Tue Nov 24 11:27:23 2015
//! \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
// of this software and associated documentation files (the "Software"), to
@@ -29,7 +29,6 @@ using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using GameRes.Utility;
using System.Security.Cryptography;
namespace GameRes.Formats.Purple
{
@@ -41,6 +40,7 @@ namespace GameRes.Formats.Purple
public Cmvs.Md5Variant Md5Variant;
public uint DecoderFactor;
public uint EntryInitKey;
public uint EntrySubKey = 0x5C29E87B;
public byte EntryTailKey;
public byte EntryKeyPos = 9;
public uint IndexSeed = 0x2A65CB4E;
@@ -57,17 +57,6 @@ namespace GameRes.Formats.Purple
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
{
public CpzHeader Header;
@@ -98,7 +87,7 @@ namespace GameRes.Formats.Purple
public CpzOpener ()
{
Signatures = new uint[] { 0x355A5043, 0x365A5043 };
Signatures = new uint[] { 0x355A5043, 0x365A5043, 0x375A5043 };
}
public static Dictionary<string, CmvsScheme> KnownSchemes = new Dictionary<string, CmvsScheme>();
@@ -113,75 +102,40 @@ namespace GameRes.Formats.Purple
{
if (null == KnownSchemes)
throw new OperationCanceledException ("Outdated encryption schemes database");
var header = file.View.ReadBytes (0, 0x3C);
var checksum = file.View.ReadUInt32 (0x3C);
if (checksum != CheckSum (header, 0, 0x3C, 0x923A564Cu))
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)
var cpz = CpzHeader.Parse (file);
if (null == cpz)
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);
if (!header.Skip (0x10).Take (0x10).SequenceEqual (hash))
return null;
var index_key = UnpackIndexKey (index, file_table_size, cpz.IndexKeySize);
for (int i = 0; i < file_table_size; ++i)
{
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[];
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);
if (null != arc)
return arc;
// both CmvsMd5 and index was altered by ReadIndex in decryption attempt
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();
}
CpzHeader ReadHeaderV5 (byte[] header)
{
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)
internal ArcFile ReadIndex (ArcView file, CmvsScheme scheme, CpzHeader cpz, byte[] index)
{
var cmvs_md5 = Cmvs.MD5.Create (scheme.Md5Variant);
cmvs_md5.Compute (cpz.CmvsMd5);
@@ -200,7 +154,7 @@ namespace GameRes.Formats.Purple
DecryptIndexDirectory (index, cpz.DirEntriesSize, key);
decoder.Init (cpz.MasterKey, cpz.CmvsMd5[2]);
int base_offset = 0x40 + index.Length;
uint base_offset = cpz.IndexOffset + cpz.IndexSize;
int dir_offset = 0;
var dir = new List<Entry>();
for (int i = 0; i < cpz.DirCount; ++i)
@@ -239,16 +193,20 @@ namespace GameRes.Formats.Purple
for (int j = 0; j < file_count; ++j)
{
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;
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)
name = Path.Combine (dir_name, name);
var entry = FormatCatalog.Instance.Create<CpzEntry> (name);
entry.Offset = LittleEndian.ToInt64 (index, cur_offset+4) + base_offset;
entry.Size = LittleEndian.ToUInt32 (index, cur_offset+0xC);
entry.CheckSum = LittleEndian.ToUInt32 (index, cur_offset+0x10);
entry.Key = LittleEndian.ToUInt32 (index, cur_offset+0x14) + dir_key;
int key_offset = cur_offset + 0x10;
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))
return null;
dir.Add (entry);
@@ -271,7 +229,8 @@ namespace GameRes.Formats.Purple
var data = carc.File.View.ReadBytes (entry.Offset, entry.Size);
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;
carc.Decoder.DecryptEntry (data, carc.Header.CmvsMd5, key);
}
@@ -282,6 +241,21 @@ namespace GameRes.Formats.Purple
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)
{
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)
{
DecryptPs2 (data);
@@ -471,6 +418,8 @@ namespace GameRes.Formats.Purple
protected byte[] m_decode_table = new byte[0x100];
protected CmvsScheme m_scheme;
public CmvsScheme Scheme { get { return m_scheme; } }
public Cpz5Decoder (CmvsScheme scheme, uint key, uint summand)
{
m_scheme = scheme;