implemented DPK resource archives.

known encryption keys for:
Inbou no Wakusei
Ryoshuu
Ryobaku ~Haitoku no Atelier~
Ryoshuu ~Jogakusei Choukyou~
Shirogane no Cal to Soukuu no Joou
Shiromiko
Shoujotachi no Saezuri
Yumemiru Tsuki no Lunalutia
This commit is contained in:
morkt 2015-06-02 03:16:11 +04:00
parent 314f4869cd
commit 884fd12607
12 changed files with 394 additions and 3 deletions

224
ArcFormats/ArcDPK.cs Normal file
View File

@ -0,0 +1,224 @@
//! \file ArcDPK.cs
//! \date Mon Jun 01 13:29:09 2015
//! \brief DPK archive
//
// Copyright (C) 2015 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
// 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.Globalization;
using System.IO;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
using GameRes.Utility;
namespace GameRes.Formats.Dac
{
internal class DpkOptions : ResourceOptions
{
public uint Key1;
public uint Key2;
}
public class DpkScheme
{
public uint Key1 { get; set; }
public uint Key2 { get; set; }
public string Name { get; set; }
public string OriginalTitle { get; set; }
}
internal class DpkEntry : Entry
{
public uint Hash;
}
internal class DpkArchive : ArcFile
{
public readonly uint Key1;
public readonly uint Key2;
public DpkArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, DpkOptions opt)
: base (arc, impl, dir)
{
Key1 = opt.Key1;
Key2 = opt.Key2;
}
}
[Export(typeof(ArchiveFormat))]
public class DpkOpener : ArchiveFormat
{
public override string Tag { get { return "DPK"; } }
public override string Description { get { return "DAC engine resource archive"; } }
public override uint Signature { get { return 0x004b5044; } } // 'DPK'
public override bool IsHierarchic { get { return true; } }
public override bool CanCreate { get { return false; } }
public static readonly DpkScheme[] KnownSchemes = new DpkScheme[]
{
new DpkScheme { Key1 = 0x0FF98, Name = "Default",
Key2 = 0x43E78A5C },
new DpkScheme { Key1 = 0x0C3BD, Name = "Inbou no Wakusei",
Key2 = 0x577D4861, OriginalTitle = "淫暴の惑星~破壊と欲望の衝動~" },
new DpkScheme { Key1 = 0x04D49, Name = "Ryoshuu",
Key2 = 0x39712FED, OriginalTitle = "虜囚 -RYOSYU-" },
new DpkScheme { Key1 = 0x11EAF, Name = "Ryobaku ~Haitoku no Atelier~",
Key2 = 0xB9976112, OriginalTitle = "虜縛~背徳のアトリエ~" },
new DpkScheme { Key1 = 0x0527F, Name = "Ryoshuu ~Jogakusei Choukyou~",
Key2 = 0x339B266F, OriginalTitle = "虜讐~女学生調教~" },
new DpkScheme { Key1 = 0x0946E, Name = "Shirogane no Cal to Soukuu no Joou",
Key2 = 0xB1956783, OriginalTitle = "白銀のカルと蒼空の女王" },
new DpkScheme { Key1 = 0x0BB51, Name = "Shiromiko",
Key2 = 0x891F52A3, OriginalTitle = "白神子 ~しろみこ~" },
new DpkScheme { Key1 = 0x09F59, Name = "Shoujotachi no Saezuri",
Key2 = 0x5DDE9B8D, OriginalTitle = "少女達のさえずり" },
new DpkScheme { Key1 = 0x0583F, Name = "Yumemiru Tsuki no Lunalutia",
Key2 = 0xB81031D7, OriginalTitle = "夢みる月のルナルティア" },
};
public override ArcFile TryOpen (ArcView file)
{
var header = new byte[8];
if (8 != file.View.Read (8, header, 0, 8))
return null;
byte last = header[7];
for (int i = 0; i < 8; i++)
{
header[i] ^= (byte)(i - 8);
}
int data_offset = LittleEndian.ToInt32 (header, 0);
if (data_offset <= 16 || data_offset >= file.MaxOffset)
return null;
int index_length = data_offset - 16;
var index = new byte[index_length];
if (index_length != file.View.Read (16, index, 0, (uint)index_length))
return null;
DecryptIndex (index, 16, index_length, last);
int count = LittleEndian.ToInt32 (index, 0);
if (count <= 0 || count > 0xfffff)
return null;
var options = Query<DpkOptions> (arcStrings.ArcEncryptedNotice);
var dir = new List<Entry> (count);
int base_offset = 4 + count * 4;
for (int i = 0; i < count; ++i)
{
var index_offset = base_offset + LittleEndian.ToInt32 (index, 4+i*4);
int name_begin = index_offset+0x0c;
int name_end = Array.IndexOf (index, (byte)0, name_begin);
if (-1 == name_end)
name_end = index.Length;
if (name_end == name_begin)
continue;
if ('z' == index[name_end-1])
--name_end; // strip 'z' from file extensions
var name = Encodings.cp932.GetString (index, name_begin, name_end-name_begin);
uint size = LittleEndian.ToUInt32 (index, index_offset + 4);
var entry = new DpkEntry
{
Name = name,
Type = FormatCatalog.Instance.GetTypeFromName (name),
Hash = GetNameHash (index, name_begin, name_end-name_begin, options.Key1, options.Key2, size),
Offset = data_offset + LittleEndian.ToUInt32 (index, index_offset),
Size = size,
};
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
if (0 == dir.Count)
return null;
return new DpkArchive (file, this, dir, options);
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var parc = arc as DpkArchive;
var pentry = entry as DpkEntry;
if (null == parc || null == pentry)
return arc.File.CreateStream (entry.Offset, entry.Size);
var data = new byte[entry.Size];
arc.File.View.Read (entry.Offset, data, 0, entry.Size);
DecryptEntry (data, parc.Key1, parc.Key2, pentry);
return new MemoryStream (data);
}
private void DecryptIndex (byte[] buf, int base_offset, int length, byte last)
{
for (int i = 0; i < length; i++)
{
int key = base_offset + i + last;
last = buf[i];
buf[i] ^= (byte)key;
}
}
private void DecryptEntry (byte[] data, uint key1, uint key2, DpkEntry entry)
{
for (uint i = 0; i < data.Length; ++i)
{
data[i] ^= (byte)(key1 + (key1 >> 8));
data[i] -= (byte)entry.Hash;
key1 += key2;
}
}
private uint GetNameHash (byte[] name, int begin, int length, uint key1, uint key2, uint entry_size)
{
uint hash = 0;
for (int i = begin+length-1; i >= begin && name[i] != '\\'; --i)
{
hash += key1 + key2 * (entry_size + name[i]);
}
return hash;
}
public override ResourceOptions GetDefaultOptions ()
{
return new DpkOptions {
Key1 = Settings.Default.DPKKey1,
Key2 = Settings.Default.DPKKey2,
};
}
public override ResourceOptions GetOptions (object w)
{
var widget = w as GUI.WidgetDPK;
if (null != widget)
{
uint result_key;
if (uint.TryParse (widget.Key1.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result_key))
Settings.Default.DPKKey1 = result_key;
if (uint.TryParse (widget.Key2.Text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result_key))
Settings.Default.DPKKey2 = result_key;
}
return this.GetDefaultOptions();
}
public override object GetAccessWidget ()
{
return new GUI.WidgetDPK();
}
}
}

View File

@ -75,6 +75,7 @@
<Compile Include="ArcBGI.cs" />
<Compile Include="ArcBlackPackage.cs" />
<Compile Include="ArcCommon.cs" />
<Compile Include="ArcDPK.cs" />
<Compile Include="ArcDRS.cs" />
<Compile Include="ArcEAGLS.cs" />
<Compile Include="ArcEGO.cs" />
@ -201,6 +202,9 @@
<DesignTime>True</DesignTime>
<DependentUpon>arcStrings.resx</DependentUpon>
</Compile>
<Compile Include="WidgetDPK.xaml.cs">
<DependentUpon>WidgetDPK.xaml</DependentUpon>
</Compile>
<Compile Include="WidgetINT.xaml.cs">
<DependentUpon>WidgetINT.xaml</DependentUpon>
</Compile>
@ -299,6 +303,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WidgetDPK.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WidgetINT.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

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.5.56")]
[assembly: AssemblyFileVersion ("1.0.5.56")]
[assembly: AssemblyVersion ("1.0.5.57")]
[assembly: AssemblyFileVersion ("1.0.5.57")]

View File

@ -321,5 +321,41 @@ namespace GameRes.Formats.Properties {
this["NOAPassPhrase"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("65432")]
public uint DPKKey1 {
get {
return ((uint)(this["DPKKey1"]));
}
set {
this["DPKKey1"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("1139247708")]
public uint DPKKey2 {
get {
return ((uint)(this["DPKKey2"]));
}
set {
this["DPKKey2"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string DPKLastScheme {
get {
return ((string)(this["DPKLastScheme"]));
}
set {
this["DPKLastScheme"] = value;
}
}
}
}

View File

@ -77,5 +77,14 @@
<Setting Name="NOAPassPhrase" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="DPKKey1" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">65432</Value>
</Setting>
<Setting Name="DPKKey2" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">1139247708</Value>
</Setting>
<Setting Name="DPKLastScheme" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View File

@ -133,6 +133,24 @@ namespace GameRes.Formats.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to Encryption scheme.
/// </summary>
public static string ArcScheme {
get {
return ResourceManager.GetString("ArcScheme", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Encryption keys.
/// </summary>
public static string DPKKeys {
get {
return ResourceManager.GetString("DPKKeys", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to âge proprietary image format.
/// </summary>

View File

@ -318,4 +318,10 @@ Enter archive encryption key.</value>
<data name="NOAIgnoreEncryption" xml:space="preserve">
<value>Ignore encryption</value>
</data>
<data name="ArcScheme" xml:space="preserve">
<value>Encryption scheme</value>
</data>
<data name="DPKKeys" xml:space="preserve">
<value>Encryption keys</value>
</data>
</root>

View File

@ -139,6 +139,12 @@
<data name="ArcReset" xml:space="preserve">
<value>Сбросить</value>
</data>
<data name="ArcScheme" xml:space="preserve">
<value>Способ шифрования</value>
</data>
<data name="DPKKeys" xml:space="preserve">
<value>Ключи шифрования</value>
</data>
<data name="INTCreationNotice" xml:space="preserve">
<value>Создание зашифрованных архивов не реализовано.</value>
</data>

54
ArcFormats/WidgetDPK.xaml Normal file
View File

@ -0,0 +1,54 @@
<Grid x:Class="GameRes.Formats.GUI.WidgetDPK"
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:dac="clr-namespace:GameRes.Formats.Dac"
xmlns:local="clr-namespace:GameRes.Formats.GUI">
<Grid.Resources>
<local:KeyConverter x:Key="keyConverter"/>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="130" Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="{x:Static s:arcStrings.ArcScheme}" Target="{Binding ElementName=EncScheme}"
Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right"/>
<ComboBox Name="EncScheme" Grid.Column="1" Grid.Row="0" Margin="0,3,0,0" Width="200"
ItemsSource="{Binding Source={x:Static dac:DpkOpener.KnownSchemes}, Mode=OneWay}"
DisplayMemberPath="Name" SelectedValuePath="Name"
SelectedValue="{Binding Source={x:Static p:Settings.Default}, Path=DPKLastScheme, Mode=TwoWay}"/>
<TextBox Name="Original" Background="Transparent" BorderThickness="0" Text="{Binding Path=OriginalTitle}"
IsReadOnly="True" TextWrapping="NoWrap" Grid.Column="1" Grid.Row="1" Margin="0,3,0,3"
DataContext="{Binding ElementName=EncScheme, Path=SelectedItem}"/>
<Label Content="{x:Static s:arcStrings.DPKKeys}" Target="{Binding ElementName=Key1}"
Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right"/>
<TextBox Name="Key1" Grid.Column="1" Grid.Row="2" Margin="0,3,0,3" Width="100" HorizontalAlignment="Left"
DataContext="{Binding ElementName=EncScheme, Path=SelectedItem}">
<TextBox.Text>
<Binding Path="Key1" Mode="OneWay" Converter="{StaticResource keyConverter}" UpdateSourceTrigger="PropertyChanged"/>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Name="Key2" Grid.Column="1" Grid.Row="3" Margin="0,3,0,3" Width="100" HorizontalAlignment="Left"
DataContext="{Binding ElementName=EncScheme, Path=SelectedItem}">
<TextBox.Text>
<Binding Path="Key2" Mode="OneWay" Converter="{StaticResource keyConverter}" UpdateSourceTrigger="PropertyChanged"/>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>

View File

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

View File

@ -79,6 +79,15 @@
<setting name="NOAPassPhrase" serializeAs="String">
<value />
</setting>
<setting name="DPKKey1" serializeAs="String">
<value>65432</value>
</setting>
<setting name="DPKKey2" serializeAs="String">
<value>1139247708</value>
</setting>
<setting name="DPKLastScheme" serializeAs="String">
<value />
</setting>
</GameRes.Formats.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>

View File

@ -120,7 +120,10 @@ Swan Song<br/>
<tr class="odd"><td>*.dat</td><td>-</td><td>No</td><td rowspan="2">Black Rainbow</td><td rowspan="2">Saiminjutsu</td></tr>
<tr class="odd"><td>*.bmd</td><td><tt>_BMD</tt></td><td>Yes</td></tr>
<tr><td>*.dat</td><td>-</td><td>Yes</td><td>Studio e.go!</td><td>Men at Work 2</td></tr>
<tr class="odd"><td>*.mbl</td><td>-</td><td>No</td><td rowspan="2">Marble</td><td rowspan="2">Ikusa Otome Valkyrie</td></tr>
<tr class="odd"><td>*.mbl</td><td>-</td><td>No</td><td rowspan="2">Marble</td><td rowspan="2">
Ikusa Otome Valkyrie<br/>
Chikatetsu Fuusa Jiken<br/>
</td></tr>
<tr class="odd"><td>*.prs</td><td><tt>YB</tt></td><td>No</td></tr>
<tr><td>*.dat</td><td>-</td><td>No</td><td rowspan="2">M no Violet</td><td rowspan="2">Nanase Ren</td></tr>
<tr><td>*</td><td><tt>gra</tt><br/><tt>mas</tt></td><td>No</td></tr>
@ -182,6 +185,7 @@ Yatohime Zankikou<br/>
Onna Kyoushi
</td></tr>
<tr class="odd"><td>*.bg_<br/>*.cg_</td><td><tt>AP</tt></td><td>Yes</td></tr>
<tr><td>*.dpk</td><td>DPK</td><td>No</td><td>DAC</td><td>Yumemiru Tsuki no Lunalutia</td></tr>
</table>
<p><a name="note-1">[1]</a> Non-encrypted only</p>
</body>