implemented ISF scripts decryption.

This commit is contained in:
morkt 2015-06-18 17:04:12 +04:00
parent 5288e75c6b
commit 6f8b57cc03
11 changed files with 270 additions and 7 deletions

View File

@ -23,12 +23,15 @@
// IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using GameRes.Utility;
using GameRes.Formats.Strings;
using GameRes.Formats.Properties;
namespace GameRes.Formats.DRS
{
@ -90,6 +93,22 @@ namespace GameRes.Formats.DRS
}
}
internal class IsfOptions : ResourceOptions
{
public byte[] Secret;
}
internal class IsfArchive : ArcFile
{
public byte[] Secret;
public IsfArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, byte[] secret = null)
: base (arc, impl, dir)
{
Secret = secret;
}
}
[Export(typeof(ArchiveFormat))]
public class MpxOpener : ArchiveFormat
{
@ -119,6 +138,7 @@ namespace GameRes.Formats.DRS
long dir_offset = 0x20;
var dir = new List<Entry> (count);
bool has_scripts = false;
for (int i = 0; i < count; ++i)
{
file.View.Read (dir_offset, name_raw, 0, 12);
@ -129,8 +149,11 @@ namespace GameRes.Formats.DRS
return null;
string name = encoding.GetString (name_raw, 0, name_length).ToLowerInvariant();
Entry entry;
if (name.EndsWith (".isf", System.StringComparison.InvariantCultureIgnoreCase))
if (name.EndsWith (".isf") || name.EndsWith (".snr"))
{
entry = new Entry { Name = name, Type = "script" };
has_scripts = true;
}
else
entry = FormatCatalog.Instance.CreateEntry (name);
entry.Offset = file.View.ReadUInt32 (dir_offset+12);
@ -140,7 +163,179 @@ namespace GameRes.Formats.DRS
dir.Add (entry);
dir_offset += 0x14;
}
if (has_scripts)
return new IsfArchive (file, this, dir);
else
return new ArcFile (file, this, dir);
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
if (!(arc is IsfArchive) || entry.Type != "script" || entry.Size < 0x10)
return arc.File.CreateStream (entry.Offset, entry.Size);
bool encoded = arc.File.View.AsciiEqual (entry.Offset+entry.Size-0x10, "SECRETFILTER100a");
uint entry_size = entry.Size;
var isf = arc as IsfArchive;
if (encoded)
{
entry_size -= 0x10;
if (null == isf.Secret)
isf.Secret = QuerySecret();
if (null == isf.Secret || 0 == isf.Secret.Length)
return arc.File.CreateStream (entry.Offset, entry.Size);
}
var data = new byte[entry_size];
arc.File.View.Read (entry.Offset, data, 0, entry_size);
if (encoded)
{
var decoder = new IsfDecoder (isf.Secret);
decoder.Decode (data);
}
int signature = LittleEndian.ToUInt16 (data, 4);
if (0x9795 == signature)
{
ApplyTransformation (data, 8, x => x >> 2 | x << 6);
}
else if (0xd197 == signature)
{
ApplyTransformation (data, 8, x => ~x);
}
else if (0xce89 == signature && 0 != data[6])
{
byte key = data[6];
ApplyTransformation (data, 8, x => x ^ key);
}
return new MemoryStream (data);
}
public override ResourceOptions GetDefaultOptions ()
{
return new IsfOptions {
Secret = GetSecret (Settings.Default.ISFScheme),
};
}
public override object GetAccessWidget ()
{
return new GUI.WidgetISF();
}
private byte[] QuerySecret ()
{
var options = Query<IsfOptions> (arcStrings.ArcEncryptedNotice);
return options.Secret;
}
private static byte[] GetSecret (string scheme)
{
byte[] secret;
if (KnownSecrets.TryGetValue (scheme, out secret))
return secret;
return null;
}
private static void ApplyTransformation (byte[] data, int offset, Func<byte, int> method)
{
for (int i = offset; i < data.Length; ++i)
data[i] = (byte)method (data[i]);
}
public static readonly Dictionary<string, byte[]> KnownSecrets = new Dictionary<string, byte[]>
{
{ arcStrings.ISFIgnoreEncryption, new byte[0] },
{ "Anata no Osanazuma", Encoding.ASCII.GetBytes("UOB0GMVM8K812GZP1VJCADNP2LX543R3BPOSMRU6BAKIVYB6W2JRRQFHN3YC5NSV5AITT6OXIDOW0YROTRT7MFJ45R1VYFF71MGI4XIOUZC511R0HAJC6WZU0EZEUUH0K7AT0U749KIOVLF5FA4C3BFXM2T4G8F57SW5EG64LU9GXRKZ2O0YGN7QA4QRUMALDFFJJ6NHLTVYYHKHRMWERQ8K782KITVCCH2G65C2WRQ85HINSMLH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WLO4L3D3CS9BIMWH4WMHYHEADJ8HM661ZNOGW4WX1070E9VO3QXXON5OT9JQ4BX6HVUFO5YMS1NG9195O8JOLWQ2IE4JR42OV4UBHVN199QIWSSYZHDE0GR043ROOGBTTQR5G11D6JZRSD3G66OHYMO9K1SKP8810P1QGF20KDDL0QPALG451MXXTETJ9CC5HFMNYUJPFN94CS2DR2TDNTSC2WYLGR068XRA238G2WFC8HJVD99VOJF5NUML0GJGKP6RL2TJW50ZRUDA7TD9S50B1ZQ5DF5MUTY11MTHHYNHOWRD92ES84IZWNKMLQZKNTMTFQQ6IDA6COQ5OT4MYTUN0OT8FG1GPV3LLCXQV2C4UBAYRHMMLRW3CHKX8O8RUKPNXGOJM5M5VVAEWYGFBGCDZZCE0LG83361H26AXBBWTKKCRNJJ3FVS8L17JADWO5NM17JY5S0SOJRY2K76G8ZZHAYN31P0WZNZY0QSSBS041IOR78BNH6SCMVTP1Y79D5JAKOCH51IIAC9WLQRHTRFJLUJ1JMCELHHQ16D23L2TKMOPAA4KV1CXSHHEV2K5HFRNKZ0IIP6O7P8KEUP0D68MBO2TBIAGE542EOMX807FJSH1JTOP1") },
};
}
internal class IsfDecoder
{
byte[] m_secret;
public IsfDecoder (byte[] secret)
{
m_secret = secret;
}
public void Decode (byte[] data)
{
var key_string = CreateKeyString();
int n = 0;
for (int i = 0; i < data.Length; )
{
DecodePrepare (n++, key_string);
for (int j = 0; j < key_string.Length && i < data.Length; )
{
data[i++] ^= key_string[j++];
}
}
}
private byte[] CreateKeyString ()
{
byte[] len_str = new byte[2];
for (int i = 0; i < 2; i++)
len_str[i] = EncodeHex ((byte)(Chr2HexCode (m_secret[0x500 + i]) - Chr2HexCode (m_secret[0x100 + i])));
byte[] key_string = new byte[Str2Hex (len_str)];
for (int i = 0; i < key_string.Length; i++)
key_string[i] = EncodeHex ((byte)(Chr2HexCode (m_secret[0x510 + i]) - Chr2HexCode (m_secret[0x110 + i])));
return key_string;
}
private void DecodePrepare (int index, byte[] key_string)
{
int p = (index & 0x3f) * 16; // index within SecretTable
for (int i = 0; i < key_string.Length; i++)
key_string[i] = EncodeHex ((byte)(Chr2HexCode (key_string[i]) + Chr2HexCode (m_secret[p+i])));
}
private static byte EncodeHex (byte symbol)
{
if (symbol < 0x80)
return HexEncodeMap[symbol % 36];
symbol = (byte)(-(sbyte)symbol % 36);
if (0 == symbol)
return HexEncodeMap[0];
return HexEncodeMap[36 - symbol];
}
private static byte Chr2HexCode (byte chr)
{
return HexTable[Chr2Hex (chr)];
}
private static byte Chr2Hex (byte chr)
{
byte code;
if (chr >= '0' && chr <= '9')
code = (byte)(chr - '0');
else if (chr >= 'a' && chr <= 'z')
code = (byte)(chr - 'a' + 10);
else if (chr >= 'A' && chr <= 'Z')
code = (byte)(chr - 'A' + 10);
else
code = 0;
return code;
}
private static int Str2Hex (byte[] shex)
{
int idec = 0;
for (int i = 0; i < shex.Length; ++i)
{
int mid = Chr2Hex (shex[i]);
mid <<= ((shex.Length - i - 1) << 2);
idec |= mid;
}
return idec;
}
static readonly byte[] HexEncodeMap = Encoding.ASCII.GetBytes("G5FXIL094MPRKWCJ3OEBVA7HQ2SU8Y6TZ1ND");
static readonly byte[] HexTable = new byte[] {
0x06, 0x21, 0x19, 0x10, 0x08, 0x01, 0x1E, 0x16, 0x1C, 0x07, 0x15, 0x13, 0x0E, 0x23, 0x12, 0x02,
0x00, 0x17, 0x04, 0x0F, 0x0C, 0x05, 0x09, 0x22, 0x11, 0x0A, 0x18, 0x0B, 0x1A, 0x1F, 0x1B, 0x14,
0x0D, 0x03, 0x1D, 0x20,
};
}
}

View File

@ -221,6 +221,9 @@
<Compile Include="WidgetINT.xaml.cs">
<DependentUpon>WidgetINT.xaml</DependentUpon>
</Compile>
<Compile Include="WidgetISF.xaml.cs">
<DependentUpon>WidgetISF.xaml</DependentUpon>
</Compile>
<Compile Include="WidgetKCAP.xaml.cs">
<DependentUpon>WidgetKCAP.xaml</DependentUpon>
</Compile>
@ -327,6 +330,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="WidgetISF.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WidgetKCAP.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("1.0.7.65")]
[assembly: AssemblyFileVersion ("1.0.7.65")]
[assembly: AssemblyVersion ("1.0.7.66")]
[assembly: AssemblyFileVersion ("1.0.7.66")]

View File

@ -369,5 +369,17 @@ namespace GameRes.Formats.Properties {
this["MBLPassPhrase"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string ISFScheme {
get {
return ((string)(this["ISFScheme"]));
}
set {
this["ISFScheme"] = value;
}
}
}
}

View File

@ -89,5 +89,8 @@
<Setting Name="MBLPassPhrase" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="ISFScheme" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View File

@ -225,6 +225,15 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Ignore encryption.
/// </summary>
public static string ISFIgnoreEncryption {
get {
return ResourceManager.GetString("ISFIgnoreEncryption", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Default.
/// </summary>
@ -280,7 +289,8 @@ namespace GameRes.Formats.Strings {
}
/// <summary>
/// Looks up a localized string similar to Archive contains encrypted scripts.Choose encryption scheme or enter a passphrase..
/// Looks up a localized string similar to Archive contains encrypted scripts.
///Choose encryption scheme or enter a passphrase..
/// </summary>
public static string MBLNotice {
get {

View File

@ -322,4 +322,7 @@ Enter archive encryption key.</value>
<value>Archive contains encrypted scripts.
Choose encryption scheme or enter a passphrase.</value>
</data>
<data name="ISFIgnoreEncryption" xml:space="preserve">
<value>Ignore encryption</value>
</data>
</root>

View File

@ -162,6 +162,9 @@
Введите ключ шифрования или выберите
один из предопределённых вариантов.</value>
</data>
<data name="ISFIgnoreEncryption" xml:space="preserve">
<value>Игнорировать шифрование</value>
</data>
<data name="KCAPDefault" xml:space="preserve">
<value>Неизвестно</value>
</data>

10
ArcFormats/WidgetISF.xaml Normal file
View File

@ -0,0 +1,10 @@
<StackPanel x:Class="GameRes.Formats.GUI.WidgetISF"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fmt="clr-namespace:GameRes.Formats.DRS"
xmlns:p="clr-namespace:GameRes.Formats.Properties"
Orientation="Vertical" MaxWidth="250">
<ComboBox Name="Scheme" ItemsSource="{Binding Source={x:Static fmt:MpxOpener.KnownSecrets}, Path=Keys, Mode=OneWay}"
SelectedValue="{Binding Source={x:Static p:Settings.Default}, Path=ISFScheme, Mode=TwoWay}"
Width="200" Grid.Row="0"/>
</StackPanel>

View File

@ -0,0 +1,17 @@
using System.Windows.Controls;
namespace GameRes.Formats.GUI
{
/// <summary>
/// Interaction logic for WidgetISF.xaml
/// </summary>
public partial class WidgetISF : StackPanel
{
public WidgetISF ()
{
InitializeComponent ();
if (-1 == Scheme.SelectedIndex)
Scheme.SelectedIndex = 0;
}
}
}

View File

@ -91,6 +91,9 @@
<setting name="MBLPassPhrase" serializeAs="String">
<value />
</setting>
<setting name="ISFScheme" serializeAs="String">
<value />
</setting>
</GameRes.Formats.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>