2015-06-15 12:52:58 +08:00
|
|
|
//! \file ArcQLIE.cs
|
|
|
|
//! \date Mon Jun 15 04:03:18 2015
|
|
|
|
//! \brief QLIE engine archives implementation.
|
|
|
|
//
|
2017-02-22 23:32:02 +08:00
|
|
|
// Copyright (C) 2015-2017 by morkt
|
2015-06-15 12:52:58 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2015-11-07 07:11:26 +08:00
|
|
|
using System;
|
2015-06-15 12:52:58 +08:00
|
|
|
using System.IO;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
2015-11-07 07:11:26 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2015-06-15 12:52:58 +08:00
|
|
|
using GameRes.Utility;
|
2015-11-07 07:11:26 +08:00
|
|
|
using GameRes.Formats.Strings;
|
2017-02-22 23:32:02 +08:00
|
|
|
using GameRes.Formats.Borland;
|
2015-06-15 12:52:58 +08:00
|
|
|
|
|
|
|
namespace GameRes.Formats.Qlie
|
|
|
|
{
|
|
|
|
internal class QlieEntry : PackedEntry
|
|
|
|
{
|
2017-07-29 06:13:22 +08:00
|
|
|
public int EncryptionMethod;
|
2015-06-15 12:52:58 +08:00
|
|
|
public uint Hash;
|
2015-11-07 07:11:26 +08:00
|
|
|
public byte[] RawName;
|
|
|
|
|
2017-07-29 06:13:22 +08:00
|
|
|
public bool IsEncrypted { get { return EncryptionMethod != 0; } }
|
|
|
|
|
2015-11-07 07:11:26 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Data from a separate key file "key.fkey" that comes with installed game.
|
|
|
|
/// null if not used.
|
|
|
|
/// </summary>
|
|
|
|
public byte[] KeyFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class QlieArchive : ArcFile
|
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
public readonly IEncryption Encryption;
|
2015-11-07 07:11:26 +08:00
|
|
|
|
2017-03-12 09:45:36 +08:00
|
|
|
public QlieArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, IEncryption enc)
|
2015-11-07 07:11:26 +08:00
|
|
|
: base (arc, impl, dir)
|
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
Encryption = enc;
|
2015-11-07 07:11:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class QlieOptions : ResourceOptions
|
|
|
|
{
|
|
|
|
public byte[] GameKeyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
public class QlieScheme : ResourceScheme
|
|
|
|
{
|
|
|
|
public Dictionary<string, byte[]> KnownKeys;
|
2015-06-15 12:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ArchiveFormat))]
|
|
|
|
public class PackOpener : ArchiveFormat
|
|
|
|
{
|
2015-07-25 14:10:36 +08:00
|
|
|
public override string Tag { get { return "PACK/QLIE"; } }
|
2015-06-15 12:52:58 +08:00
|
|
|
public override string Description { get { return "QLIE engine resource archive"; } }
|
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
public override bool IsHierarchic { get { return true; } }
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-06-15 12:52:58 +08:00
|
|
|
|
|
|
|
public PackOpener ()
|
|
|
|
{
|
|
|
|
Extensions = new string [] { "pack" };
|
|
|
|
}
|
|
|
|
|
2015-11-07 07:11:26 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Possible locations of the 'key.fkey' file relative to an archive being accessed.
|
|
|
|
/// </summary>
|
|
|
|
static readonly string[] KeyLocations = { ".", "..", @"..\DLL", "DLL" };
|
|
|
|
|
|
|
|
public static Dictionary<string, byte[]> KnownKeys = new Dictionary<string, byte[]>();
|
|
|
|
|
|
|
|
public override ResourceScheme Scheme
|
|
|
|
{
|
|
|
|
get { return new QlieScheme { KnownKeys = KnownKeys }; }
|
|
|
|
set { KnownKeys = ((QlieScheme)value).KnownKeys; }
|
|
|
|
}
|
|
|
|
|
2015-06-15 12:52:58 +08:00
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
|
|
|
if (file.MaxOffset <= 0x1c)
|
|
|
|
return null;
|
|
|
|
long index_offset = file.MaxOffset - 0x1c;
|
|
|
|
if (!file.View.AsciiEqual (index_offset, "FilePackVer")
|
2017-03-12 09:45:36 +08:00
|
|
|
|| '.' != file.View.ReadByte (index_offset+0xC))
|
2015-06-15 12:52:58 +08:00
|
|
|
return null;
|
2017-03-12 09:45:36 +08:00
|
|
|
var pack_version = new Version (file.View.ReadByte (index_offset+0xB) - '0',
|
|
|
|
file.View.ReadByte (index_offset+0xD) - '0');
|
2015-06-15 12:52:58 +08:00
|
|
|
int count = file.View.ReadInt32 (index_offset+0x10);
|
2015-10-28 06:49:44 +08:00
|
|
|
if (!IsSaneCount (count))
|
2015-06-15 12:52:58 +08:00
|
|
|
return null;
|
|
|
|
index_offset = file.View.ReadInt64 (index_offset+0x14);
|
|
|
|
if (index_offset < 0 || index_offset >= file.MaxOffset)
|
|
|
|
return null;
|
|
|
|
|
2015-11-07 07:11:26 +08:00
|
|
|
byte[] arc_key = null;
|
|
|
|
byte[] key_file = null;
|
2016-05-20 08:04:49 +08:00
|
|
|
bool use_pack_keyfile = false;
|
2017-03-12 09:45:36 +08:00
|
|
|
if (pack_version.Major >= 3)
|
2015-10-28 06:49:44 +08:00
|
|
|
{
|
2015-11-07 07:11:26 +08:00
|
|
|
key_file = FindKeyFile (file);
|
2016-05-20 08:04:49 +08:00
|
|
|
use_pack_keyfile = key_file != null;
|
2015-11-07 07:11:26 +08:00
|
|
|
// currently, user is prompted to choose encryption scheme only if there's 'key.fkey' file found.
|
2016-05-20 08:04:49 +08:00
|
|
|
if (use_pack_keyfile)
|
2017-02-22 23:32:02 +08:00
|
|
|
arc_key = QueryEncryption (file);
|
2016-05-20 08:04:49 +08:00
|
|
|
// use_pack_keyfile = null != arc_key;
|
2015-10-28 06:49:44 +08:00
|
|
|
}
|
2017-03-12 09:45:36 +08:00
|
|
|
var enc = QlieEncryption.Create (file, pack_version, arc_key);
|
2015-10-28 06:49:44 +08:00
|
|
|
|
2018-03-12 01:21:35 +08:00
|
|
|
bool read_pack_keyfile = 3 == pack_version.Major && use_pack_keyfile;
|
2015-06-15 12:52:58 +08:00
|
|
|
var name_buffer = new byte[0x100];
|
|
|
|
var dir = new List<Entry> (count);
|
2017-03-12 09:45:36 +08:00
|
|
|
using (var index = file.CreateStream (index_offset))
|
2015-06-15 12:52:58 +08:00
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2015-11-07 07:11:26 +08:00
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
int name_length = index.ReadUInt16();
|
|
|
|
if (enc.IsUnicode)
|
|
|
|
name_length *= 2;
|
|
|
|
if (name_length > name_buffer.Length)
|
|
|
|
name_buffer = new byte[name_length];
|
|
|
|
if (name_length != index.Read (name_buffer, 0, name_length))
|
|
|
|
return null;
|
|
|
|
var name = enc.DecryptName (name_buffer, name_length);
|
|
|
|
var entry = FormatCatalog.Instance.Create<QlieEntry> (name);
|
|
|
|
if (use_pack_keyfile)
|
|
|
|
entry.RawName = name_buffer.Take (name_length).ToArray();
|
|
|
|
|
|
|
|
entry.Offset = index.ReadInt64(); // [+00]
|
|
|
|
entry.Size = index.ReadUInt32(); // [+08]
|
|
|
|
if (!entry.CheckPlacement (file.MaxOffset))
|
|
|
|
return null;
|
|
|
|
entry.UnpackedSize = index.ReadUInt32(); // [+0C]
|
|
|
|
entry.IsPacked = 0 != index.ReadInt32(); // [+10]
|
2017-07-29 06:13:22 +08:00
|
|
|
entry.EncryptionMethod = index.ReadInt32(); // [+14]
|
2017-03-12 09:45:36 +08:00
|
|
|
entry.Hash = index.ReadUInt32(); // [+18]
|
|
|
|
entry.KeyFile = key_file;
|
2018-03-12 01:21:35 +08:00
|
|
|
if (read_pack_keyfile && entry.Name.Contains ("pack_keyfile"))
|
2017-03-12 09:45:36 +08:00
|
|
|
{
|
|
|
|
// note that 'pack_keyfile' itself is encrypted using 'key.fkey' file contents.
|
|
|
|
key_file = ReadEntryBytes (file, entry, enc);
|
2018-03-12 01:21:35 +08:00
|
|
|
read_pack_keyfile = false;
|
2017-03-12 09:45:36 +08:00
|
|
|
}
|
|
|
|
dir.Add (entry);
|
2015-11-07 07:11:26 +08:00
|
|
|
}
|
2015-06-15 12:52:58 +08:00
|
|
|
}
|
2017-03-12 09:45:36 +08:00
|
|
|
return new QlieArchive (file, this, dir, enc);
|
2015-06-15 12:52:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
var qent = entry as QlieEntry;
|
2015-11-07 07:11:26 +08:00
|
|
|
var qarc = arc as QlieArchive;
|
|
|
|
if (null == qent || null == qarc || (!qent.IsEncrypted && !qent.IsPacked))
|
2015-06-15 12:52:58 +08:00
|
|
|
return arc.File.CreateStream (entry.Offset, entry.Size);
|
2017-03-12 09:45:36 +08:00
|
|
|
var data = ReadEntryBytes (arc.File, qent, qarc.Encryption);
|
2016-10-16 13:22:53 +08:00
|
|
|
return new BinMemoryStream (data, entry.Name);
|
2015-11-07 07:11:26 +08:00
|
|
|
}
|
2015-06-15 12:52:58 +08:00
|
|
|
|
2017-03-12 09:45:36 +08:00
|
|
|
private byte[] ReadEntryBytes (ArcView file, QlieEntry entry, IEncryption enc)
|
2015-11-07 07:11:26 +08:00
|
|
|
{
|
2016-05-20 08:04:49 +08:00
|
|
|
var data = file.View.ReadBytes (entry.Offset, entry.Size);
|
2015-11-07 07:11:26 +08:00
|
|
|
if (entry.IsEncrypted)
|
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
enc.DecryptEntry (data, 0, data.Length, entry);
|
2015-11-07 07:11:26 +08:00
|
|
|
}
|
|
|
|
if (entry.IsPacked)
|
2015-06-15 12:52:58 +08:00
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
data = Decompress (data) ?? data;
|
2015-06-15 12:52:58 +08:00
|
|
|
}
|
2015-11-07 07:11:26 +08:00
|
|
|
return data;
|
2015-06-15 12:52:58 +08:00
|
|
|
}
|
|
|
|
|
2015-10-29 03:12:43 +08:00
|
|
|
internal static byte[] Decompress (byte[] input)
|
2015-06-15 12:52:58 +08:00
|
|
|
{
|
2017-03-12 09:45:36 +08:00
|
|
|
if (LittleEndian.ToUInt32 (input, 0) != 0xFF435031) // '1PC\xFF'
|
2015-06-15 12:52:58 +08:00
|
|
|
return null;
|
|
|
|
|
|
|
|
bool is_16bit = 0 != (input[4] & 1);
|
|
|
|
|
|
|
|
var node = new byte[2,256];
|
|
|
|
var child_node = new byte[256];
|
|
|
|
|
|
|
|
int output_length = LittleEndian.ToInt32 (input, 8);
|
|
|
|
var output = new byte[output_length];
|
|
|
|
|
|
|
|
int src = 12;
|
|
|
|
int dst = 0;
|
|
|
|
while (src < input.Length)
|
|
|
|
{
|
|
|
|
int i, k, count, index;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
node[0,i] = (byte)i;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; )
|
|
|
|
{
|
|
|
|
count = input[src++];
|
|
|
|
|
|
|
|
if (count > 127)
|
|
|
|
{
|
|
|
|
int step = count - 127;
|
|
|
|
i += step;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 255)
|
|
|
|
break;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
for (k = 0; k < count; k++)
|
|
|
|
{
|
|
|
|
node[0,i] = input[src++];
|
|
|
|
if (node[0,i] != i)
|
|
|
|
node[1,i] = input[src++];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_16bit)
|
|
|
|
{
|
|
|
|
count = LittleEndian.ToUInt16 (input, src);
|
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = LittleEndian.ToInt32 (input, src);
|
|
|
|
src += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (k > 0)
|
|
|
|
index = child_node[--k];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (0 == count)
|
|
|
|
break;
|
|
|
|
count--;
|
|
|
|
index = input[src++];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node[0,index] == index)
|
|
|
|
output[dst++] = (byte)index;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child_node[k++] = node[1,index];
|
|
|
|
child_node[k++] = node[0,index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dst != output.Length)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
2015-10-28 06:49:44 +08:00
|
|
|
|
2015-11-07 07:11:26 +08:00
|
|
|
public override ResourceOptions GetDefaultOptions ()
|
|
|
|
{
|
|
|
|
return new QlieOptions {
|
2018-01-09 00:04:23 +08:00
|
|
|
GameKeyData = GetKeyData (Properties.Settings.Default.QLIEScheme)
|
2015-11-07 07:11:26 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object GetAccessWidget ()
|
|
|
|
{
|
|
|
|
return new GUI.WidgetQLIE();
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:32:02 +08:00
|
|
|
byte[] QueryEncryption (ArcView file)
|
2015-11-07 07:11:26 +08:00
|
|
|
{
|
2017-02-22 23:32:02 +08:00
|
|
|
var title = FormatCatalog.Instance.LookupGame (file.Name, @"..\*.exe");
|
|
|
|
byte[] key = null;
|
2017-03-13 23:16:18 +08:00
|
|
|
if (!string.IsNullOrEmpty (title) && KnownKeys.ContainsKey (title))
|
|
|
|
return KnownKeys[title];
|
2017-02-22 23:32:02 +08:00
|
|
|
if (null == key)
|
|
|
|
key = GuessKeyData (file.Name);
|
|
|
|
if (null == key)
|
|
|
|
{
|
|
|
|
var options = Query<QlieOptions> (arcStrings.ArcEncryptedNotice);
|
|
|
|
key = options.GameKeyData;
|
|
|
|
}
|
|
|
|
return key;
|
2015-11-07 07:11:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static byte[] GetKeyData (string scheme)
|
|
|
|
{
|
|
|
|
byte[] key;
|
|
|
|
if (KnownKeys.TryGetValue (scheme, out key))
|
|
|
|
return key;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Look for 'key.fkey' file within nearby directories specified by KeyLocations.
|
|
|
|
/// </summary>
|
|
|
|
static byte[] FindKeyFile (ArcView arc_file)
|
|
|
|
{
|
|
|
|
// QLIE archives with key could be opened at the physical file system level only
|
|
|
|
if (VFS.IsVirtual)
|
|
|
|
return null;
|
|
|
|
var dir_name = Path.GetDirectoryName (arc_file.Name);
|
|
|
|
foreach (var path in KeyLocations)
|
|
|
|
{
|
|
|
|
var name = Path.Combine (dir_name, path, "key.fkey");
|
|
|
|
if (File.Exists (name))
|
|
|
|
{
|
|
|
|
Trace.WriteLine ("reading key from "+name, "[QLIE]");
|
|
|
|
return File.ReadAllBytes (name);
|
|
|
|
}
|
|
|
|
}
|
2017-07-29 06:13:22 +08:00
|
|
|
var pattern = VFS.CombinePath (dir_name, @"..\*.exe");
|
|
|
|
foreach (var exe_file in VFS.GetFiles (pattern))
|
|
|
|
{
|
|
|
|
using (var exe = new ExeFile.ResourceAccessor (exe_file.Name))
|
|
|
|
{
|
|
|
|
var reskey = exe.GetResource ("RESKEY", "#10");
|
|
|
|
if (reskey != null)
|
|
|
|
return reskey;
|
|
|
|
}
|
|
|
|
}
|
2015-11-07 07:11:26 +08:00
|
|
|
return null;
|
|
|
|
}
|
2017-02-22 23:32:02 +08:00
|
|
|
|
|
|
|
byte[] GuessKeyData (string arc_name)
|
|
|
|
{
|
|
|
|
if (VFS.IsVirtual)
|
|
|
|
return null;
|
|
|
|
// XXX add button to query dialog like with CatSystem?
|
|
|
|
var pattern = VFS.CombinePath (VFS.GetDirectoryName (arc_name), @"..\*.exe");
|
|
|
|
foreach (var file in VFS.GetFiles (pattern))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var key = GetKeyDataFromExe (file.Name);
|
|
|
|
if (key != null)
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
catch { /* ignore errors */ }
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] GetKeyDataFromExe (string filename)
|
|
|
|
{
|
|
|
|
using (var exe = new ExeFile.ResourceAccessor (filename))
|
|
|
|
{
|
|
|
|
var tform = exe.GetResource ("TFORM1", "#10");
|
|
|
|
if (null == tform || !tform.AsciiEqual (0, "TPF0"))
|
|
|
|
return null;
|
|
|
|
using (var input = new BinMemoryStream (tform))
|
|
|
|
{
|
|
|
|
var deserializer = new DelphiDeserializer (input);
|
|
|
|
var form = deserializer.Deserialize();
|
|
|
|
var image = form.Contents.FirstOrDefault (n => n.Name == "IconKeyImage");
|
|
|
|
if (null == image)
|
|
|
|
return null;
|
|
|
|
var icon = image.Props["Picture.Data"] as byte[];
|
|
|
|
if (null == icon || icon.Length < 0x106 || !icon.AsciiEqual (0, "\x05TIcon"))
|
|
|
|
return null;
|
|
|
|
return new CowArray<byte> (icon, 6, 0x100).ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 12:52:58 +08:00
|
|
|
}
|
|
|
|
}
|