2015-04-22 09:14:41 +08:00
|
|
|
//! \file ArcLPK.cs
|
|
|
|
//! \date Mon Feb 16 17:21:47 2015
|
|
|
|
//! \brief Lucifen Easy Game System archive implementation.
|
|
|
|
//
|
2016-01-10 06:37:28 +08:00
|
|
|
// Copyright (C) 2015-2016 by morkt
|
2015-04-22 09:14:41 +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.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2015-07-25 08:20:20 +08:00
|
|
|
using GameRes.Compression;
|
2015-04-25 06:07:14 +08:00
|
|
|
using GameRes.Formats.Properties;
|
2015-04-22 09:14:41 +08:00
|
|
|
using GameRes.Formats.Strings;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.Lucifen
|
|
|
|
{
|
|
|
|
internal class LuciEntry : PackedEntry
|
|
|
|
{
|
|
|
|
public byte Flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class LuciArchive : ArcFile
|
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
public LpkInfo Info;
|
|
|
|
public EncryptionScheme Scheme;
|
2015-04-22 09:14:41 +08:00
|
|
|
|
2015-04-25 06:07:14 +08:00
|
|
|
public LuciArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, EncryptionScheme scheme, LpkInfo info)
|
2015-04-22 09:14:41 +08:00
|
|
|
: base (arc, impl, dir)
|
|
|
|
{
|
|
|
|
Info = info;
|
2015-04-25 06:07:14 +08:00
|
|
|
Scheme = scheme;
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 05:27:53 +08:00
|
|
|
[Serializable]
|
2015-04-25 06:07:14 +08:00
|
|
|
public class EncryptionScheme
|
|
|
|
{
|
|
|
|
public LpkOpener.Key BaseKey;
|
|
|
|
public byte ContentXor;
|
|
|
|
public uint RotatePattern;
|
|
|
|
public bool ImportGameInit;
|
|
|
|
}
|
|
|
|
|
2015-09-18 05:27:53 +08:00
|
|
|
[Serializable]
|
|
|
|
public class LpkScheme : ResourceScheme
|
|
|
|
{
|
|
|
|
public Dictionary<string, EncryptionScheme> KnownSchemes;
|
|
|
|
public Dictionary<string, Dictionary<string, LpkOpener.Key>> KnownKeys;
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:14:41 +08:00
|
|
|
internal class LuciOptions : ResourceOptions
|
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
public string Scheme;
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
internal class LpkInfo
|
|
|
|
{
|
|
|
|
public bool AlignedOffset;
|
|
|
|
public bool Flag1;
|
|
|
|
public bool IsEncrypted;
|
|
|
|
public bool PackedEntries;
|
|
|
|
public bool WholeCrypt;
|
|
|
|
public uint Key;
|
|
|
|
public byte[] Prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ArchiveFormat))]
|
|
|
|
public class LpkOpener : ArchiveFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "LPK"; } }
|
|
|
|
public override string Description { get { return "Lucifen system resource archive"; } }
|
|
|
|
public override uint Signature { get { return 0x314b504c; } } // 'LPK1'
|
2016-01-06 23:50:43 +08:00
|
|
|
public override bool IsHierarchic { get { return true; } }
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-04-22 09:14:41 +08:00
|
|
|
|
2015-09-18 05:27:53 +08:00
|
|
|
[Serializable]
|
2015-04-23 17:39:04 +08:00
|
|
|
public class Key
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
|
|
|
public uint Key1, Key2;
|
|
|
|
public Key (uint k1, uint k2)
|
|
|
|
{
|
|
|
|
Key1 = k1;
|
|
|
|
Key2 = k2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 01:08:17 +08:00
|
|
|
static readonly EncryptionScheme DefaultScheme = new EncryptionScheme {
|
|
|
|
BaseKey = new Key (0xA5B9AC6B, 0x9A639DE5), ContentXor = 0x5d, RotatePattern = 0x31746285,
|
|
|
|
ImportGameInit = true
|
|
|
|
};
|
2016-01-06 23:50:43 +08:00
|
|
|
public static Dictionary<string, EncryptionScheme> KnownSchemes = new Dictionary<string, EncryptionScheme> { { "Default", DefaultScheme } };
|
2015-09-18 05:27:53 +08:00
|
|
|
static Dictionary<string, Dictionary<string, Key>> KnownKeys = new Dictionary<string, Dictionary<string, Key>>();
|
2015-04-22 09:14:41 +08:00
|
|
|
|
2015-09-18 05:27:53 +08:00
|
|
|
EncryptionScheme CurrentScheme = DefaultScheme;
|
2015-04-25 06:07:14 +08:00
|
|
|
Dictionary<string, Key> CurrentFileMap = new Dictionary<string, Key>();
|
2015-04-22 09:14:41 +08:00
|
|
|
|
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
|
|
|
string name = Path.GetFileName (file.Name).ToUpperInvariant();
|
|
|
|
if (string.IsNullOrEmpty (name))
|
|
|
|
return null;
|
2015-04-25 06:07:14 +08:00
|
|
|
Key file_key = null;
|
|
|
|
var basename = Encodings.cp932.GetBytes (Path.GetFileNameWithoutExtension (name));
|
|
|
|
if (name != "SCRIPT.LPK")
|
|
|
|
CurrentFileMap.TryGetValue (name, out file_key);
|
|
|
|
try
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
var arc = Open (basename, file, CurrentScheme, file_key);
|
|
|
|
if (null != arc)
|
|
|
|
return arc;
|
|
|
|
}
|
|
|
|
catch { /* unknown encryption, ignore parse errors */ }
|
|
|
|
var new_scheme = QueryEncryptionScheme();
|
|
|
|
if (new_scheme == CurrentScheme && !CurrentScheme.ImportGameInit)
|
|
|
|
return null;
|
|
|
|
CurrentScheme = new_scheme;
|
|
|
|
if (name != "SCRIPT.LPK" && CurrentScheme.ImportGameInit)
|
|
|
|
{
|
|
|
|
if (0 == CurrentFileMap.Count)
|
2015-04-23 17:39:04 +08:00
|
|
|
ImportKeys (file.Name);
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
2016-01-06 23:50:43 +08:00
|
|
|
if (CurrentFileMap.Count > 0 && !CurrentFileMap.TryGetValue (name, out file_key))
|
|
|
|
return null;
|
2015-04-25 06:07:14 +08:00
|
|
|
return Open (basename, file, CurrentScheme, file_key);
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
if (0 == entry.Size)
|
|
|
|
return Stream.Null;
|
|
|
|
var larc = arc as LuciArchive;
|
|
|
|
var lent = entry as LuciEntry;
|
|
|
|
Stream input = arc.File.CreateStream (entry.Offset, entry.Size);
|
|
|
|
if (null == larc || null == lent)
|
|
|
|
return input;
|
|
|
|
byte[] data;
|
|
|
|
using (input)
|
|
|
|
{
|
|
|
|
if (lent.IsPacked)
|
|
|
|
{
|
|
|
|
using (var reader = new LzssReader (input, (int)lent.Size, (int)lent.UnpackedSize))
|
|
|
|
{
|
|
|
|
reader.Unpack();
|
|
|
|
data = reader.Data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = new byte[lent.Size];
|
|
|
|
input.Read (data, 0, data.Length);
|
|
|
|
}
|
|
|
|
}
|
2015-04-23 17:39:04 +08:00
|
|
|
if (larc.Info.WholeCrypt)
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
DecryptContent (data, larc.Scheme.ContentXor);
|
2015-04-23 17:39:04 +08:00
|
|
|
}
|
|
|
|
if (larc.Info.IsEncrypted)
|
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
int count = Math.Min (data.Length, 0x100);
|
2015-04-22 09:14:41 +08:00
|
|
|
if (count != 0)
|
2015-04-25 06:07:14 +08:00
|
|
|
DecryptEntry (data, count, larc.Info.Key, larc.Scheme.RotatePattern);
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
2016-10-16 13:22:53 +08:00
|
|
|
input = new BinMemoryStream (data, entry.Name);
|
2015-04-22 09:14:41 +08:00
|
|
|
if (null != larc.Info.Prefix)
|
|
|
|
return new PrefixStream (larc.Info.Prefix, input);
|
|
|
|
else
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-04-25 06:07:14 +08:00
|
|
|
static void DecryptContent (byte[] data, byte key)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Length; ++i)
|
|
|
|
{
|
|
|
|
int v = data[i] ^ key;
|
2016-01-06 23:50:43 +08:00
|
|
|
data[i] = Binary.RotByteR ((byte)v, 4);
|
2015-04-25 06:07:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsafe void DecryptEntry (byte[] data, int length, uint key, uint pattern)
|
|
|
|
{
|
|
|
|
fixed (byte* buf_raw = data)
|
|
|
|
{
|
|
|
|
uint* encoded = (uint*)buf_raw;
|
|
|
|
length /= 4;
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
encoded[i] ^= key;
|
2016-01-06 23:50:43 +08:00
|
|
|
pattern = Binary.RotR (pattern, 4);
|
|
|
|
key = Binary.RotL (key, (int)pattern);
|
2015-04-25 06:07:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsafe void DecryptIndex (byte[] data, int length, uint key, uint pattern)
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
fixed (byte* buf_raw = data)
|
|
|
|
{
|
|
|
|
uint* encoded = (uint*)buf_raw;
|
|
|
|
length /= 4;
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
encoded[i] ^= key;
|
2016-01-06 23:50:43 +08:00
|
|
|
pattern = Binary.RotL (pattern, 4);
|
|
|
|
key = Binary.RotR (key, (int)pattern);
|
2015-04-25 06:07:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArcFile Open (byte[] basename, ArcView file, EncryptionScheme scheme, Key key)
|
|
|
|
{
|
|
|
|
uint key1 = scheme.BaseKey.Key1;
|
|
|
|
uint key2 = scheme.BaseKey.Key2;
|
2015-04-22 09:14:41 +08:00
|
|
|
for (int b = 0, e = basename.Length-1; e >= 0; ++b, --e)
|
|
|
|
{
|
|
|
|
key1 ^= basename[e];
|
|
|
|
key2 ^= basename[b];
|
2016-01-06 23:50:43 +08:00
|
|
|
key1 = Binary.RotR (key1, 7);
|
|
|
|
key2 = Binary.RotL (key2, 7);
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
2015-04-25 06:07:14 +08:00
|
|
|
if (null != key)
|
|
|
|
{
|
|
|
|
key1 ^= key.Key1;
|
|
|
|
key2 ^= key.Key2;
|
|
|
|
}
|
2015-04-22 09:14:41 +08:00
|
|
|
uint code = file.View.ReadUInt32 (4) ^ key2;
|
|
|
|
int index_size = (int)(code & 0xffffff);
|
|
|
|
byte flags = (byte)(code >> 24);
|
|
|
|
if (0 != (flags & 1))
|
|
|
|
index_size = (index_size << 11) - 8;
|
2015-04-25 06:07:14 +08:00
|
|
|
if (index_size < 5 || index_size >= file.MaxOffset)
|
2015-04-22 09:14:41 +08:00
|
|
|
return null;
|
|
|
|
var index = new byte[index_size];
|
|
|
|
if (index_size != file.View.Read (8, index, 0, (uint)index_size))
|
|
|
|
return null;
|
2015-04-25 06:07:14 +08:00
|
|
|
DecryptIndex (index, index_size, key2, scheme.RotatePattern);
|
2015-04-22 09:14:41 +08:00
|
|
|
var lpk_info = new LpkInfo
|
|
|
|
{
|
|
|
|
AlignedOffset = 0 != (flags & 1),
|
|
|
|
Flag1 = 0 != (flags & 2),
|
|
|
|
IsEncrypted = 0 != (flags & 4),
|
|
|
|
PackedEntries = 0 != (flags & 8),
|
|
|
|
WholeCrypt = 0 != (flags & 0x10),
|
|
|
|
Key = key1
|
|
|
|
};
|
|
|
|
var reader = new IndexReader (lpk_info);
|
|
|
|
var dir = reader.Read (index);
|
|
|
|
if (null == dir)
|
|
|
|
return null;
|
2016-01-10 06:37:28 +08:00
|
|
|
// this condition is fishy, probably patch files have additional bitflag set
|
|
|
|
if (lpk_info.WholeCrypt && Binary.AsciiEqual (basename, "PATCH"))
|
2016-01-06 23:50:43 +08:00
|
|
|
lpk_info.WholeCrypt = false;
|
2015-04-25 06:07:14 +08:00
|
|
|
return new LuciArchive (file, this, dir, scheme, reader.Info);
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
|
2015-04-25 06:07:14 +08:00
|
|
|
static readonly byte[] ScriptName = Encoding.ASCII.GetBytes ("SCRIPT");
|
|
|
|
|
2015-04-22 09:14:41 +08:00
|
|
|
void ImportKeys (string source_name)
|
|
|
|
{
|
2015-09-01 18:20:18 +08:00
|
|
|
var script_lpk = VFS.CombinePath (Path.GetDirectoryName (source_name), "SCRIPT.LPK");
|
|
|
|
using (var script_file = VFS.OpenView (script_lpk))
|
2015-04-25 06:07:14 +08:00
|
|
|
using (var script_arc = Open (ScriptName, script_file, CurrentScheme, null))
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
if (null == script_arc)
|
|
|
|
throw new UnknownEncryptionScheme();
|
2016-01-10 06:37:28 +08:00
|
|
|
var entry = script_arc.Dir.FirstOrDefault (e => e.Name.Equals ("gameinit.sob", StringComparison.InvariantCultureIgnoreCase));
|
2015-04-22 09:14:41 +08:00
|
|
|
if (null == entry)
|
|
|
|
throw new FileNotFoundException ("Missing 'gameinit.sob' entry in SCRIPT.LPK");
|
|
|
|
using (var gameinit = script_arc.OpenEntry (entry))
|
|
|
|
{
|
|
|
|
var init_data = new byte[gameinit.Length];
|
|
|
|
gameinit.Read (init_data, 0, init_data.Length);
|
2015-04-23 17:39:04 +08:00
|
|
|
if (!ParseGameInit (init_data))
|
2015-04-22 09:14:41 +08:00
|
|
|
throw new UnknownEncryptionScheme();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:39:04 +08:00
|
|
|
bool ParseGameInit (byte[] sob)
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
CurrentFileMap.Clear();
|
2015-04-22 09:14:41 +08:00
|
|
|
if (!Binary.AsciiEqual (sob, "SOB0"))
|
|
|
|
return false;
|
|
|
|
int offset = LittleEndian.ToInt32 (sob, 4) + 8;
|
|
|
|
if (offset <= 0 || offset >= sob.Length)
|
|
|
|
return false;
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
fixed (byte* buf_raw = sob)
|
|
|
|
{
|
|
|
|
uint* p = (uint*)(buf_raw + offset);
|
|
|
|
uint index_len = *p;
|
|
|
|
if (offset+8+(int)index_len >= sob.Length)
|
|
|
|
return false;
|
|
|
|
p += 2;
|
|
|
|
uint* last = p + index_len / 4 - 28;
|
|
|
|
|
|
|
|
while (p < last)
|
|
|
|
{
|
|
|
|
if (p[0] == 0x28 && p[1] == 0 && p[2] != 0 && p[3] == 8 &&
|
|
|
|
p[4] == 1 && p[5] == 8 && p[6] == 1 && p[7] == 8 &&
|
|
|
|
p[8] != 0 && p[9] == 8 && p[10] != 0 && p[11] == 5 &&
|
|
|
|
p[17] == 0x28 && p[18] == 0 && p[19] != 0 && p[20] == 8 &&
|
|
|
|
p[21] != 0 && p[22] == 8 && p[23] == 0xffffffff && p[24] == 8 &&
|
|
|
|
p[25] == 1 && p[26] == 8 && p[27] == 1 && p[28] == 5)
|
|
|
|
{
|
|
|
|
byte* lpk = (byte*)p + p[21] - p[2] + 0x34;
|
|
|
|
int name_index = (int)(lpk - buf_raw);
|
2015-04-25 06:07:14 +08:00
|
|
|
if (name_index < 0 || name_index >= sob.Length)
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
|
|
|
++p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
string name = Binary.GetCString (sob, name_index, sob.Length-name_index);
|
|
|
|
name = name.ToUpperInvariant();
|
2015-04-25 06:07:14 +08:00
|
|
|
CurrentFileMap[name] = new Key (p[8], p[10]);
|
2015-04-22 09:14:41 +08:00
|
|
|
p += 0x22;
|
|
|
|
} else
|
|
|
|
++p;
|
|
|
|
}
|
2016-02-22 21:42:49 +08:00
|
|
|
return CurrentFileMap.Count > 0;
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 06:07:14 +08:00
|
|
|
public override ResourceOptions GetDefaultOptions ()
|
2015-04-22 09:14:41 +08:00
|
|
|
{
|
2015-04-25 06:07:14 +08:00
|
|
|
return new LuciOptions { Scheme = Settings.Default.LPKScheme };
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object GetAccessWidget ()
|
|
|
|
{
|
|
|
|
return new GUI.WidgetLPK();
|
|
|
|
}
|
|
|
|
|
|
|
|
EncryptionScheme QueryEncryptionScheme ()
|
|
|
|
{
|
|
|
|
CurrentFileMap.Clear();
|
2015-04-22 09:14:41 +08:00
|
|
|
var options = Query<LuciOptions> (arcStrings.ArcEncryptedNotice);
|
|
|
|
if (null == options)
|
2016-01-06 23:50:43 +08:00
|
|
|
return DefaultScheme;
|
2015-04-25 06:07:14 +08:00
|
|
|
string title = options.Scheme;
|
2016-02-22 02:49:05 +08:00
|
|
|
Dictionary<string, Key> file_map;
|
|
|
|
if (KnownKeys.TryGetValue (title, out file_map))
|
|
|
|
CurrentFileMap = new Dictionary<string, Key> (file_map);
|
2015-04-25 06:07:14 +08:00
|
|
|
return KnownSchemes[title];
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
2015-09-18 05:27:53 +08:00
|
|
|
|
|
|
|
public override ResourceScheme Scheme
|
|
|
|
{
|
|
|
|
get { return new LpkScheme { KnownSchemes = KnownSchemes, KnownKeys = KnownKeys }; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
var scheme = (LpkScheme)value;
|
|
|
|
KnownSchemes = scheme.KnownSchemes;
|
|
|
|
KnownKeys = scheme.KnownKeys;
|
2016-01-06 23:50:43 +08:00
|
|
|
foreach (var key in KnownSchemes.Keys.Where (x => null == KnownSchemes[x]).ToArray())
|
2015-09-18 05:27:53 +08:00
|
|
|
{
|
2016-01-06 23:50:43 +08:00
|
|
|
KnownSchemes[key] = DefaultScheme;
|
2015-09-18 05:27:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
internal class IndexReader
|
|
|
|
{
|
|
|
|
byte[] m_index;
|
|
|
|
LpkInfo m_info;
|
|
|
|
List<Entry> m_dir;
|
|
|
|
byte[] m_name;
|
|
|
|
int m_index_width;
|
|
|
|
int m_entries_offset;
|
|
|
|
|
2015-07-19 19:32:07 +08:00
|
|
|
public LpkInfo Info { get { return m_info; } }
|
|
|
|
public int EntrySize { get; private set; }
|
2015-04-22 09:14:41 +08:00
|
|
|
|
|
|
|
public IndexReader (LpkInfo info)
|
|
|
|
{
|
|
|
|
if (!info.Flag1)
|
|
|
|
throw new NotSupportedException ("Not supported LPK index format");
|
|
|
|
m_info = info;
|
2015-07-19 19:32:07 +08:00
|
|
|
EntrySize = m_info.PackedEntries ? 13 : 9;
|
2015-04-22 09:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Entry> Read (byte[] index)
|
|
|
|
{
|
|
|
|
m_index = index;
|
|
|
|
int count = LittleEndian.ToInt32 (m_index, 0);
|
|
|
|
if (count <= 0 || count > 0xfffff)
|
|
|
|
return null;
|
|
|
|
int index_offset = 4;
|
|
|
|
int prefix_length = m_index[index_offset++];
|
|
|
|
if (0 != prefix_length)
|
|
|
|
{
|
|
|
|
m_info.Prefix = new byte[prefix_length];
|
|
|
|
Buffer.BlockCopy (m_index, index_offset, m_info.Prefix, 0, prefix_length);
|
|
|
|
index_offset += prefix_length;
|
|
|
|
}
|
|
|
|
m_index_width = 0 != m_index[index_offset++] ? 4 : 2;
|
|
|
|
int letter_table_length = LittleEndian.ToInt32 (m_index, index_offset);
|
|
|
|
index_offset += 4;
|
|
|
|
m_entries_offset = index_offset + letter_table_length;
|
2015-07-19 19:32:07 +08:00
|
|
|
if (m_entries_offset >= m_index.Length)
|
|
|
|
return null;
|
|
|
|
if ((m_index.Length - m_entries_offset) / EntrySize < count)
|
|
|
|
EntrySize = (m_index.Length - m_entries_offset) / count;
|
|
|
|
if (EntrySize < 8 || m_info.PackedEntries && EntrySize < 12)
|
|
|
|
return null;
|
2015-04-22 09:14:41 +08:00
|
|
|
|
|
|
|
m_dir = new List<Entry> (count);
|
|
|
|
m_name = new byte[260];
|
|
|
|
TraverseIndex (index_offset, 0);
|
|
|
|
return m_dir.Count == count ? m_dir : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraverseIndex (int index_offset, int name_length)
|
|
|
|
{
|
|
|
|
if (index_offset < 0 || index_offset >= m_index.Length)
|
|
|
|
throw new InvalidFormatException ("Error parsing LPK index");
|
|
|
|
if (name_length >= m_name.Length)
|
|
|
|
throw new InvalidFormatException ("Entry filename is too long");
|
|
|
|
int entries = m_index[index_offset++];
|
|
|
|
for (int i = 0; i < entries; ++i)
|
|
|
|
{
|
|
|
|
byte next_letter = m_index[index_offset++];
|
|
|
|
int next_offset = 4 == m_index_width ? LittleEndian.ToInt32 (m_index, index_offset)
|
|
|
|
: (int)LittleEndian.ToUInt16 (m_index, index_offset);
|
|
|
|
m_name[name_length] = next_letter;
|
|
|
|
index_offset += m_index_width;;
|
|
|
|
if (0 != next_letter)
|
|
|
|
TraverseIndex (index_offset+next_offset, name_length+1);
|
|
|
|
else
|
|
|
|
AddEntry (name_length, next_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddEntry (int name_length, int entry_num)
|
|
|
|
{
|
|
|
|
if (name_length < 1)
|
|
|
|
throw new InvalidFormatException ("Invalid LPK entry name");
|
|
|
|
string name = Encodings.cp932.GetString (m_name, 0, name_length);
|
2016-05-24 02:56:36 +08:00
|
|
|
var entry = FormatCatalog.Instance.Create<LuciEntry> (name);
|
2015-07-19 19:32:07 +08:00
|
|
|
int entry_pos = m_entries_offset + EntrySize * entry_num;
|
|
|
|
if (entry_pos+EntrySize > m_index.Length)
|
2015-04-22 09:14:41 +08:00
|
|
|
throw new InvalidFormatException ("Invalid LPK entry index");
|
2015-07-19 19:32:07 +08:00
|
|
|
if (0 != (EntrySize & 1))
|
|
|
|
entry.Flag = m_index[entry_pos++];
|
|
|
|
long offset = LittleEndian.ToUInt32 (m_index, entry_pos);
|
2015-04-22 09:14:41 +08:00
|
|
|
entry.Offset = m_info.AlignedOffset ? offset << 11 : offset;
|
2015-07-19 19:32:07 +08:00
|
|
|
entry.Size = LittleEndian.ToUInt32 (m_index, entry_pos+4);
|
2016-05-24 02:56:36 +08:00
|
|
|
entry.UnpackedSize = entry.Size;
|
|
|
|
if (m_info.PackedEntries)
|
|
|
|
{
|
|
|
|
uint unpacked_size = LittleEndian.ToUInt32 (m_index, entry_pos+8);
|
|
|
|
entry.IsPacked = unpacked_size != 0;
|
|
|
|
if (entry.IsPacked)
|
|
|
|
entry.UnpackedSize = unpacked_size;
|
|
|
|
}
|
2015-04-22 09:14:41 +08:00
|
|
|
m_dir.Add (entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|