implemented NS2 archives.

This commit is contained in:
morkt 2016-07-30 22:30:33 +04:00
parent 92279cf4bf
commit ad1a9b0cda
3 changed files with 238 additions and 1 deletions

View File

@ -144,6 +144,7 @@
<DependentUpon>WidgetMGPK.xaml</DependentUpon> <DependentUpon>WidgetMGPK.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Marble\VideoANIM.cs" /> <Compile Include="Marble\VideoANIM.cs" />
<Compile Include="MD5.cs" />
<Compile Include="Nekopunch\ArcPAK.cs" /> <Compile Include="Nekopunch\ArcPAK.cs" />
<Compile Include="NitroPlus\ArcPAK.cs" /> <Compile Include="NitroPlus\ArcPAK.cs" />
<Compile Include="MnoViolet\ImageDIF.cs" /> <Compile Include="MnoViolet\ImageDIF.cs" />
@ -157,6 +158,7 @@
<Compile Include="NonColor\WidgetNCARC.xaml.cs"> <Compile Include="NonColor\WidgetNCARC.xaml.cs">
<DependentUpon>WidgetNCARC.xaml</DependentUpon> <DependentUpon>WidgetNCARC.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="NScripter\ArcNS2.cs" />
<Compile Include="Patisserie\ArcBIN.cs" /> <Compile Include="Patisserie\ArcBIN.cs" />
<Compile Include="Patisserie\ArcRAW.cs" /> <Compile Include="Patisserie\ArcRAW.cs" />
<Compile Include="RealLive\ArcG00.cs" /> <Compile Include="RealLive\ArcG00.cs" />

View File

@ -0,0 +1,227 @@
//! \file ArcNS2.cs
//! \date Sat Jul 30 13:00:13 2016
//! \brief NS2 resorce archives.
//
// Copyright (C) 2016 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.IO;
using System.Security.Cryptography;
using System.Text;
using GameRes.Formats.Properties;
using GameRes.Formats.Strings;
namespace GameRes.Formats.NScripter
{
[Export(typeof(ArchiveFormat))]
public class Ns2Opener : ArchiveFormat
{
public override string Tag { get { return "NS2"; } }
public override string Description { get { return arcStrings.NSADescription; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanCreate { get { return false; } }
public static Dictionary<string, string> KnownKeys = new Dictionary<string, string>();
public override ResourceScheme Scheme
{
get { return new NsaScheme { KnownKeys = KnownKeys }; }
set { KnownKeys = ((NsaScheme)value).KnownKeys; }
}
public override ArcFile TryOpen (ArcView file)
{
List<Entry> dir = null;
uint data_offset = file.View.ReadUInt32 (0);
if (data_offset > 4 && data_offset < file.MaxOffset)
{
try
{
using (var input = file.CreateStream())
{
dir = ReadIndex (input);
if (null != dir)
return new ArcFile (file, this, dir);
}
}
catch { /* ignore parse errors */ }
}
if (!file.Name.EndsWith (".ns2", StringComparison.InvariantCultureIgnoreCase))
return null;
var password = QueryPassword();
if (string.IsNullOrEmpty (password))
return null;
var key = Encoding.ASCII.GetBytes (password);
if (key.Length < 96)
throw new OperationCanceledException ("Password should be at least 96 characters long");
using (var input = new Ns2Stream (file, key))
{
dir = ReadIndex (input);
if (null == dir)
return null;
return new NsaEncryptedArchive (file, this, dir, key);
}
}
protected List<Entry> ReadIndex (Stream file)
{
using (var input = new BinaryReader (file, Encodings.cp932, true))
{
uint base_offset = input.ReadUInt32();
if (base_offset <= 4 || base_offset >= file.Length)
return null;
var name_buffer = new char[0x100];
long current_offset = base_offset;
var dir = new List<Entry>();
while (file.Position < base_offset)
{
if (input.ReadChar() != '"')
break;
char c;
int i = 0;
while ((c = input.ReadChar()) != '"')
{
if (name_buffer.Length == i)
return null;
name_buffer[i++] = c;
}
if (0 == i)
return null;
var name = new string (name_buffer, 0, i);
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = current_offset;
entry.Size = input.ReadUInt32();
if (!entry.CheckPlacement (file.Length))
return null;
current_offset += entry.Size;
dir.Add (entry);
}
return dir.Count > 0 ? dir : null;
}
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var nsa_arc = arc as NsaEncryptedArchive;
if (null == nsa_arc)
{
return arc.File.CreateStream (entry.Offset, entry.Size);
}
var encrypted = new Ns2Stream (arc.File, nsa_arc.Key);
return new StreamRegion (encrypted, entry.Offset, entry.Size);
}
private string QueryPassword ()
{
var options = Query<NsaOptions> (arcStrings.ArcEncryptedNotice);
return options.Password;
}
public override ResourceOptions GetDefaultOptions ()
{
return new NsaOptions { Password = Settings.Default.NSAPassword };
}
public override ResourceOptions GetOptions (object widget)
{
var w = widget as GUI.WidgetNSA;
if (null != w)
Settings.Default.NSAPassword = w.Password.Text;
return GetDefaultOptions();
}
public override object GetAccessWidget ()
{
return new GUI.WidgetNSA (KnownKeys);
}
}
internal class Ns2Stream : ViewStreamBase
{
byte[] m_key;
static readonly Cryptography.MD5 MD5 = new Cryptography.MD5();
const int BlockSize = 32;
public Ns2Stream (ArcView mmap, byte[] key) : base (mmap)
{
m_key = key;
}
byte[] m_seed = new byte[64];
protected override void DecryptBlock ()
{
int block_count = m_current_block_length / BlockSize;
var temp = new byte[32];
var hash = new byte[16];
for (int src = 0; src < m_current_block_length; src += BlockSize)
{
int src2 = src + 16;
int key1 = 0; // within m_key
int key2 = 48;
Buffer.BlockCopy (m_current_block, src2, m_seed, 0, 16);
Buffer.BlockCopy (m_key, key1, m_seed, 16, 48);
MD5.Initialize();
MD5.Update (m_seed, 0, m_seed.Length);
Buffer.BlockCopy (MD5.State, 0, hash, 0, 16);
for (int j = 0; j < 16; ++j)
{
temp[j] = m_seed[j] = (byte)(hash[j] ^ m_current_block[src + j]);
}
Buffer.BlockCopy (m_key, key2, m_seed, 16, 48);
MD5.Initialize();
MD5.Update (m_seed, 0, m_seed.Length);
Buffer.BlockCopy (MD5.State, 0, hash, 0, 16);
for (int j = 0; j < 16; ++j)
{
temp[16 + j] = m_seed[j] = (byte)(hash[j] ^ m_current_block[src2 + j]);
}
Buffer.BlockCopy (m_key, key1, m_seed, 16, 48);
MD5.Initialize();
MD5.Update (m_seed, 0, m_seed.Length);
Buffer.BlockCopy (MD5.State, 0, hash, 0, 16);
Buffer.BlockCopy (temp, 16, m_current_block, src, 16);
for (int j = 0; j < 16; ++j)
{
m_current_block[src + 16 + j] = (byte)(hash[j] ^ temp[j]);
}
}
}
}
}

View File

@ -119,6 +119,7 @@ Arpeggio ~Kimi Iro no Melody~<br/>
Chikan Kizoku<br/> Chikan Kizoku<br/>
Futagoza no Paradox<br/> Futagoza no Paradox<br/>
Happy Princess<br/> Happy Princess<br/>
Idol ☆ Revolution<br/>
Narimono<br/> Narimono<br/>
Reconquista<br/> Reconquista<br/>
White ~blanche comme la lune~<br/> White ~blanche comme la lune~<br/>
@ -163,6 +164,7 @@ Ren'ai Jugyou ~Oshiego no Yuuwaku~<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.nsa<br/>*.sar</td><td>-</td><td>Yes<a href="#note-1" class="footnote">1</a></td><td>NScripter</td><td> <tr class="odd"><td>*.nsa<br/>*.sar</td><td>-</td><td>Yes<a href="#note-1" class="footnote">1</a></td><td>NScripter</td><td>
Binary Pot<br/> Binary Pot<br/>
Chou Gedou Yuusha<br/>
Tsukihime<br/> Tsukihime<br/>
Umineko<br/> Umineko<br/>
</td></tr> </td></tr>
@ -305,15 +307,17 @@ Nanase Ren<br/>
Imouto ~Mitsutsubo Complete Edition~<br/> Imouto ~Mitsutsubo Complete Edition~<br/>
</td></tr> </td></tr>
<tr><td>*</td><td><tt>gra</tt><br/><tt>mas</tt><br/><tt>dif</tt></td><td>No</td></tr> <tr><td>*</td><td><tt>gra</tt><br/><tt>mas</tt><br/><tt>dif</tt></td><td>No</td></tr>
<tr class="odd"><td>*.ald</td><td>-</td><td>No</td><td rowspan="4">Alice Soft</td><td rowspan="4"> <tr class="odd"><td>*.ald</td><td>-</td><td>No</td><td rowspan="5">Alice Soft</td><td rowspan="5">
Daiteikoku<br/> Daiteikoku<br/>
Pastel Chime 3 Bind Seeker<br/> Pastel Chime 3 Bind Seeker<br/>
Shaman's Sanctuary -Miko no Seiiki-<br/> Shaman's Sanctuary -Miko no Seiiki-<br/>
Tsuma Shibori<br/> Tsuma Shibori<br/>
Tsumamigui 3<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.afa</td><td><tt>AFAH</tt></td><td>No</td></tr> <tr class="odd"><td>*.afa</td><td><tt>AFAH</tt></td><td>No</td></tr>
<tr class="odd"><td>*.alk</td><td><tt>ALK0</tt></td><td>No</td></tr> <tr class="odd"><td>*.alk</td><td><tt>ALK0</tt></td><td>No</td></tr>
<tr class="odd"><td>*.qnt</td><td><tt>QNT</tt></td><td>No</td></tr> <tr class="odd"><td>*.qnt</td><td><tt>QNT</tt></td><td>No</td></tr>
<tr class="odd"><td>*.dcf</td><td><tt>dcf</tt></td><td>No</td></tr>
<tr><td>*.pd<br/>*.pb</td><td>-</td><td>No</td><td>Discovery</td><td>Tsuma Tsuma</td></tr> <tr><td>*.pd<br/>*.pb</td><td>-</td><td>No</td><td>Discovery</td><td>Tsuma Tsuma</td></tr>
<tr class="odd"><td>*.ifl</td><td><tt>IFLS</tt></td><td>No</td><td rowspan="3">Silky's</td><td rowspan="3"> <tr class="odd"><td>*.ifl</td><td><tt>IFLS</tt></td><td>No</td><td rowspan="3">Silky's</td><td rowspan="3">
Flutter of Birds<br/> Flutter of Birds<br/>
@ -592,6 +596,7 @@ Thirua Panic<br/>
<tr><td>*.acd</td><td><tt>ACD 1.00</tt></td><td>No</td></tr> <tr><td>*.acd</td><td><tt>ACD 1.00</tt></td><td>No</td></tr>
<tr class="odd"><td>*.pk<br/>*.gpk<br/>*.tpk<br/>*.wpk<br/></td><td>-</td><td>No</td><td rowspan="2">U-Me Soft</td><td rowspan="2"> <tr class="odd"><td>*.pk<br/>*.gpk<br/>*.tpk<br/>*.wpk<br/></td><td>-</td><td>No</td><td rowspan="2">U-Me Soft</td><td rowspan="2">
Fetish Omote no Kioku/Ura no Kioku series<br/> Fetish Omote no Kioku/Ura no Kioku series<br/>
Kyonyuu Maid Nakadashi Tengoku!<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.grx</td><td><tt>GRX\x1a</tt><br/><tt>SGX\x1a</tt></td><td>No</td></tr> <tr class="odd"><td>*.grx</td><td><tt>GRX\x1a</tt><br/><tt>SGX\x1a</tt></td><td>No</td></tr>
<tr><td>*.fpk</td><td><tt>MFWY</tt></td><td>No</td><td rowspan="2">Caligula</td><td rowspan="2"> <tr><td>*.fpk</td><td><tt>MFWY</tt></td><td>No</td><td rowspan="2">Caligula</td><td rowspan="2">
@ -987,6 +992,9 @@ Nukiani!! Sweet Home<br/>
Denpa no Dorei<br/> Denpa no Dorei<br/>
Soushinjutsu Plus<br/> Soushinjutsu Plus<br/>
</td></tr> </td></tr>
<tr><td>*.ns2</td><td>-</td><td>No</td><td>NScripter</td><td>
Rakuin Hime Runed Princess<br/>
</td></tr>
</table> </table>
<p><a name="note-1" class="footnote">1</a> Non-encrypted only</p> <p><a name="note-1" class="footnote">1</a> Non-encrypted only</p>
</body> </body>