From 884fd126076d80be9e400958dd7f71980a6a1267 Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 2 Jun 2015 03:16:11 +0400 Subject: [PATCH] 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 --- ArcFormats/ArcDPK.cs | 224 +++++++++++++++++++++ ArcFormats/ArcFormats.csproj | 8 + ArcFormats/Properties/AssemblyInfo.cs | 4 +- ArcFormats/Properties/Settings.Designer.cs | 36 ++++ ArcFormats/Properties/Settings.settings | 9 + ArcFormats/Strings/arcStrings.Designer.cs | 18 ++ ArcFormats/Strings/arcStrings.resx | 6 + ArcFormats/Strings/arcStrings.ru-RU.resx | 6 + ArcFormats/WidgetDPK.xaml | 54 +++++ ArcFormats/WidgetDPK.xaml.cs | 17 ++ ArcFormats/app.config | 9 + supported.html | 6 +- 12 files changed, 394 insertions(+), 3 deletions(-) create mode 100644 ArcFormats/ArcDPK.cs create mode 100644 ArcFormats/WidgetDPK.xaml create mode 100644 ArcFormats/WidgetDPK.xaml.cs diff --git a/ArcFormats/ArcDPK.cs b/ArcFormats/ArcDPK.cs new file mode 100644 index 00000000..43f27ca7 --- /dev/null +++ b/ArcFormats/ArcDPK.cs @@ -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 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 (arcStrings.ArcEncryptedNotice); + var dir = new List (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(); + } + } +} diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index a0d0e18b..4c372e6a 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -75,6 +75,7 @@ + @@ -201,6 +202,9 @@ True arcStrings.resx + + WidgetDPK.xaml + WidgetINT.xaml @@ -299,6 +303,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + MSBuild:Compile Designer diff --git a/ArcFormats/Properties/AssemblyInfo.cs b/ArcFormats/Properties/AssemblyInfo.cs index f7d9a5e5..7d11315c 100644 --- a/ArcFormats/Properties/AssemblyInfo.cs +++ b/ArcFormats/Properties/AssemblyInfo.cs @@ -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")] diff --git a/ArcFormats/Properties/Settings.Designer.cs b/ArcFormats/Properties/Settings.Designer.cs index 945f21f7..10de39ac 100644 --- a/ArcFormats/Properties/Settings.Designer.cs +++ b/ArcFormats/Properties/Settings.Designer.cs @@ -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; + } + } } } diff --git a/ArcFormats/Properties/Settings.settings b/ArcFormats/Properties/Settings.settings index e07cca84..5cdab448 100644 --- a/ArcFormats/Properties/Settings.settings +++ b/ArcFormats/Properties/Settings.settings @@ -77,5 +77,14 @@ + + 65432 + + + 1139247708 + + + + \ No newline at end of file diff --git a/ArcFormats/Strings/arcStrings.Designer.cs b/ArcFormats/Strings/arcStrings.Designer.cs index e2d2d37d..e3f46f8a 100644 --- a/ArcFormats/Strings/arcStrings.Designer.cs +++ b/ArcFormats/Strings/arcStrings.Designer.cs @@ -133,6 +133,24 @@ namespace GameRes.Formats.Strings { } } + /// + /// Looks up a localized string similar to Encryption scheme. + /// + public static string ArcScheme { + get { + return ResourceManager.GetString("ArcScheme", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Encryption keys. + /// + public static string DPKKeys { + get { + return ResourceManager.GetString("DPKKeys", resourceCulture); + } + } + /// /// Looks up a localized string similar to âge proprietary image format. /// diff --git a/ArcFormats/Strings/arcStrings.resx b/ArcFormats/Strings/arcStrings.resx index baba98b5..adcd859c 100644 --- a/ArcFormats/Strings/arcStrings.resx +++ b/ArcFormats/Strings/arcStrings.resx @@ -318,4 +318,10 @@ Enter archive encryption key. Ignore encryption + + Encryption scheme + + + Encryption keys + \ No newline at end of file diff --git a/ArcFormats/Strings/arcStrings.ru-RU.resx b/ArcFormats/Strings/arcStrings.ru-RU.resx index 66becc3c..631cc860 100644 --- a/ArcFormats/Strings/arcStrings.ru-RU.resx +++ b/ArcFormats/Strings/arcStrings.ru-RU.resx @@ -139,6 +139,12 @@ Сбросить + + Способ шифрования + + + Ключи шифрования + Создание зашифрованных архивов не реализовано. diff --git a/ArcFormats/WidgetDPK.xaml b/ArcFormats/WidgetDPK.xaml new file mode 100644 index 00000000..5f648f5d --- /dev/null +++ b/ArcFormats/WidgetDPK.xaml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + diff --git a/ArcFormats/WidgetDPK.xaml.cs b/ArcFormats/WidgetDPK.xaml.cs new file mode 100644 index 00000000..c088c0ad --- /dev/null +++ b/ArcFormats/WidgetDPK.xaml.cs @@ -0,0 +1,17 @@ +using System.Windows.Controls; + +namespace GameRes.Formats.GUI +{ + /// + /// Interaction logic for WidgetDPK.xaml + /// + public partial class WidgetDPK : Grid + { + public WidgetDPK () + { + InitializeComponent (); + if (null == EncScheme.SelectedItem) + EncScheme.SelectedIndex = 0; + } + } +} diff --git a/ArcFormats/app.config b/ArcFormats/app.config index 457d4f15..390df4ea 100644 --- a/ArcFormats/app.config +++ b/ArcFormats/app.config @@ -79,6 +79,15 @@ + + 65432 + + + 1139247708 + + + + diff --git a/supported.html b/supported.html index 488e6440..43cb3db2 100644 --- a/supported.html +++ b/supported.html @@ -120,7 +120,10 @@ Swan Song
*.dat-NoBlack RainbowSaiminjutsu *.bmd_BMDYes *.dat-YesStudio e.go!Men at Work 2 -*.mbl-NoMarbleIkusa Otome Valkyrie +*.mbl-NoMarble +Ikusa Otome Valkyrie
+Chikatetsu Fuusa Jiken
+ *.prsYBNo *.dat-NoM no VioletNanase Ren *gra
masNo @@ -182,6 +185,7 @@ Yatohime Zankikou
Onna Kyoushi *.bg_
*.cg_APYes +*.dpkDPKNoDACYumemiru Tsuki no Lunalutia

[1] Non-encrypted only