implemented Marble scripts decryption.

This commit is contained in:
morkt 2015-06-04 01:13:25 +04:00
parent 8849508e41
commit 2da5ed1961
10 changed files with 155 additions and 8 deletions

View File

@ -216,6 +216,9 @@
<SubType>Code</SubType>
<DependentUpon>WidgetLPK.xaml</DependentUpon>
</Compile>
<Compile Include="WidgetMBL.xaml.cs">
<DependentUpon>WidgetMBL.xaml</DependentUpon>
</Compile>
<Compile Include="WidgetNOA.xaml.cs">
<DependentUpon>WidgetNOA.xaml</DependentUpon>
</Compile>
@ -320,6 +323,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="WidgetMBL.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WidgetNOA.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -28,10 +28,28 @@ using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
using GameRes.Utility;
namespace GameRes.Formats.Marble
{
public class MblOptions : ResourceOptions
{
public string PassPhrase;
}
public class MblArchive : ArcFile
{
public readonly byte[] Key;
public MblArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, string password)
: base (arc, impl, dir)
{
Key = Encodings.cp932.GetBytes (password);
}
}
[Export(typeof(ArchiveFormat))]
public class MblOpener : ArchiveFormat
{
@ -64,6 +82,7 @@ namespace GameRes.Formats.Marble
return null;
try
{
bool contains_scripts = false;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
@ -79,13 +98,22 @@ namespace GameRes.Formats.Marble
name = name.ToLowerInvariant();
index_offset += (uint)filename_len;
uint offset = file.View.ReadUInt32 (index_offset);
var entry = new AutoEntry (name, () => {
uint signature = file.View.ReadUInt32 (offset);
var res = FormatCatalog.Instance.LookupSignature (signature);
if (!res.Any() && 0x4259 == (0xffff & signature))
res = FormatCatalog.Instance.ImageFormats.Where (x => x.Tag == "PRS");
return res.FirstOrDefault();
});
Entry entry;
if (name.EndsWith (".s"))
{
entry = new Entry { Name = name, Type = "script" };
contains_scripts = true;
}
else
{
entry = new AutoEntry (name, () => {
uint signature = file.View.ReadUInt32 (offset);
var res = FormatCatalog.Instance.LookupSignature (signature);
if (!res.Any() && 0x4259 == (0xffff & signature))
res = FormatCatalog.Instance.ImageFormats.Where (x => x.Tag == "PRS");
return res.FirstOrDefault();
});
}
entry.Offset = offset;
entry.Size = file.View.ReadUInt32 (index_offset+4);
if (offset < index_size || !entry.CheckPlacement (file.MaxOffset))
@ -95,6 +123,12 @@ namespace GameRes.Formats.Marble
}
if (0 == dir.Count)
return null;
if (contains_scripts)
{
var options = Query<MblOptions> ("Archive contains encrypted scripts.\nChoose encryption scheme or enter a passphrase.");
if (options.PassPhrase.Length > 0)
return new MblArchive (file, this, dir, options.PassPhrase);
}
return new ArcFile (file, this, dir);
}
catch
@ -102,5 +136,44 @@ namespace GameRes.Formats.Marble
return null;
}
}
public static Dictionary<string, string> KnownKeys = new Dictionary<string, string> {
{ arcStrings.ArcDefault, "" },
{ "Chikatetsu Fuusa Jiken", "naze" }
};
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
if (entry.Type != "script" || !entry.Name.EndsWith (".s"))
return arc.File.CreateStream (entry.Offset, entry.Size);
var marc = arc as MblArchive;
var data = new byte[entry.Size];
arc.File.View.Read (entry.Offset, data, 0, entry.Size);
if (null == marc || null == marc.Key)
{
for (int i = 0; i < data.Length; ++i)
{
data[i] = (byte)-data[i];
}
}
else
{
for (int i = 0; i < data.Length; ++i)
{
data[i] ^= marc.Key[i % marc.Key.Length];
}
}
return new MemoryStream (data);
}
public override ResourceOptions GetDefaultOptions ()
{
return new MblOptions { PassPhrase = Settings.Default.MBLPassPhrase };
}
public override object GetAccessWidget ()
{
return new GUI.WidgetMBL();
}
}
}

View File

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

View File

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

View File

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

View File

@ -318,4 +318,8 @@ Enter archive encryption key.</value>
<data name="KCAPDefault" xml:space="preserve">
<value>Default</value>
</data>
</root>
<data name="MBLNotice" xml:space="preserve">
<value>Archive contains encrypted scripts.
Choose encryption scheme or enter a passphrase.</value>
</data>
</root>

View File

@ -174,6 +174,10 @@
<data name="LabelScheme" xml:space="preserve">
<value>Вариант</value>
</data>
<data name="MBLNotice" xml:space="preserve">
<value>Архив содержит зашифрованные скрипты.
Выберите способ шифрования или введите текстовый пароль.</value>
</data>
<data name="MsgAddingFile" xml:space="preserve">
<value>Добавляется файл</value>
</data>

17
ArcFormats/WidgetMBL.xaml Normal file
View File

@ -0,0 +1,17 @@
<Grid x:Class="GameRes.Formats.GUI.WidgetMBL"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:GameRes.Formats.Strings"
xmlns:p="clr-namespace:GameRes.Formats.Properties"
xmlns:m="clr-namespace:GameRes.Formats.Marble">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ComboBox Name="EncScheme" Grid.Row="0" Margin="0,3,0,0" Width="160" HorizontalAlignment="Right"
ItemsSource="{Binding Source={x:Static m:MblOpener.KnownKeys}, Mode=OneWay}"
DisplayMemberPath="Key" SelectedValuePath="Value"
SelectedValue="{Binding ElementName=PassPhrase, Path=Text, Mode=TwoWay}"/>
<TextBox Name="PassPhrase" Grid.Row="1" Margin="0,3,0,3" Width="{Binding ElementName=EncScheme, Path=ActualWidth}" HorizontalAlignment="Right"
Text="{Binding Source={x:Static p:Settings.Default}, Path=MBLPassPhrase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

View File

@ -0,0 +1,15 @@
using System.Windows.Controls;
namespace GameRes.Formats.GUI
{
/// <summary>
/// Interaction logic for WidgetMBL.xaml
/// </summary>
public partial class WidgetMBL : Grid
{
public WidgetMBL ()
{
InitializeComponent ();
}
}
}

View File

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