implemented older versions of Xuse archives.

This commit is contained in:
morkt 2016-02-13 02:02:07 +04:00
parent dcb3e44104
commit 71e01d1ee1
4 changed files with 270 additions and 123 deletions

View File

@ -250,6 +250,7 @@
<Compile Include="Xuse\ArcBIN.cs" />
<Compile Include="Xuse\ArcGD.cs" />
<Compile Include="Xuse\ArcWAG.cs" />
<Compile Include="Xuse\ArcXARC.cs" />
<Compile Include="Xuse\ArcXuse.cs" />
<Compile Include="Yuka\ArcYKC.cs" />
<Compile Include="BlackCyc\AudioVAW.cs" />

View File

@ -55,151 +55,42 @@ namespace GameRes.Formats.Xuse
public WagOpener ()
{
Extensions = new string[] { "wag", "4ag" };
Extensions = new string[] { "wag", "4ag", "004" };
Signatures = new uint[] { 0x40474157, 0x34464147 }; // 'GAF4'
}
public override ArcFile TryOpen (ArcView file)
{
if (0x0300 != file.View.ReadUInt16 (4))
int version = file.View.ReadUInt16 (4);
if (0x300 != version && 0x200 != version)
return null;
int count = file.View.ReadInt32 (0x46);
if (!IsSaneCount (count))
return null;
byte[] title = file.View.ReadBytes (6, 0x40);
if (0x40 != title.Length)
var reader = new IndexReader (file, version, count);
var dir = reader.ReadIndex();
if (null == dir)
return null;
int title_length = Array.IndexOf<byte> (title, 0);
if (-1 == title_length)
title_length = title.Length;
string arc_filename = Path.GetFileName (file.Name).ToLowerInvariant();
byte[] bin_filename = Encodings.cp932.GetBytes (arc_filename);
byte[] name_key = GenerateKey (bin_filename);
uint key_sum = (uint)name_key.Select (x => (int)x).Sum();
uint index_offset = 0x200 + key_sum;
for (int i = 0; i < name_key.Length; ++i)
{
index_offset ^= name_key[i];
index_offset = Binary.RotR (index_offset, 1);
}
for (int i = 0; i < name_key.Length; ++i)
{
index_offset ^= name_key[i];
index_offset = Binary.RotR (index_offset, 1);
}
index_offset %= 0x401;
index_offset += 0x4A;
byte[] index = file.View.ReadBytes (index_offset, (uint)(4*count));
if (index.Length != 4*count)
return null;
byte[] index_key = new byte[index.Length];
for (int i = 0; i < index_key.Length; ++i)
{
int v = name_key[(i+1) % name_key.Length] ^ (name_key[i % name_key.Length] + (i & 0xFF));
index_key[i] = (byte)(count + v);
}
Decrypt (index_offset, index_key, index);
var dir = new List<Entry> (count);
int current_offset = 0;
uint next_offset = LittleEndian.ToUInt32 (index, current_offset);
byte[] data_key = GenerateKey (title, title_length);
string base_filename = Path.GetFileNameWithoutExtension (arc_filename);
byte[] chunk_buf = new byte[8];
byte[] filename_buf = new byte[0x40];
for (int i = 0; i < count; ++i)
{
current_offset += 4;
uint entry_offset = next_offset;
if (entry_offset >= file.MaxOffset)
return null;
if (i + 1 == count)
next_offset = (uint)file.MaxOffset;
else
next_offset = LittleEndian.ToUInt32 (index, current_offset);
uint entry_size = next_offset - entry_offset;
if (8 != file.View.Read (entry_offset, chunk_buf, 0, 8))
return null;
Decrypt (entry_offset, data_key, chunk_buf);
if (!Binary.AsciiEqual (chunk_buf, "DSET"))
return null;
uint chunk_offset = entry_offset + 10;
int chunk_count = LittleEndian.ToInt32 (chunk_buf, 4);
string filename = null;
string type = null;
for (int chunk = 0; chunk < chunk_count; ++chunk)
{
if (8 != file.View.Read (chunk_offset, chunk_buf, 0, 8))
return null;
Decrypt (chunk_offset, data_key, chunk_buf);
int chunk_size = LittleEndian.ToInt32 (chunk_buf, 4);
if (chunk_size <= 0)
return null;
if (Binary.AsciiEqual (chunk_buf, "PICT"))
{
if (null == type)
{
type = "image";
entry_offset = chunk_offset + 0x10;
entry_size = (uint)chunk_size - 6;
}
}
else if (null == filename && Binary.AsciiEqual (chunk_buf, "FTAG"))
{
if (chunk_size > filename_buf.Length)
filename_buf = new byte[chunk_size];
if (chunk_size != file.View.Read (chunk_offset+10, filename_buf, 0, (uint)chunk_size))
return null;
Decrypt (chunk_offset+10, data_key, filename_buf, 0, chunk_size-2);
filename = Encodings.cp932.GetString (filename_buf, 0, chunk_size-2);
}
chunk_offset += 10 + (uint)chunk_size;
}
Entry entry;
if (!string.IsNullOrEmpty (filename))
{
filename = DriveRe.Replace (filename, "");
entry = FormatCatalog.Instance.Create<Entry> (filename);
}
else
{
entry = new Entry {
Name = string.Format ("{0}#{1:D4}", base_filename, i),
Type = type ?? ""
};
}
entry.Offset = entry_offset;
entry.Size = entry_size;
dir.Add (entry);
}
return new WagArchive (file, this, dir, data_key);
return new WagArchive (file, this, dir, reader.DataKey);
}
static readonly Regex DriveRe = new Regex (@"^(?:.+:)?\\+");
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var warc = arc as WagArchive;
if (null == warc)
return arc.File.CreateStream (entry.Offset, entry.Size);
var data = new byte[entry.Size];
arc.File.View.Read (entry.Offset, data, 0, entry.Size);
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
Decrypt ((uint)entry.Offset, warc.Key, data);
return new MemoryStream (data);
}
private byte[] GenerateKey (byte[] keyword)
static private byte[] GenerateKey (byte[] keyword)
{
return GenerateKey (keyword, keyword.Length);
}
private byte[] GenerateKey (byte[] keyword, int length)
static private byte[] GenerateKey (byte[] keyword, int length)
{
int hash = 0;
for (int i = 0; i < length; ++i)
@ -225,16 +116,175 @@ namespace GameRes.Formats.Xuse
return key;
}
private void Decrypt (uint offset, byte[] key, byte[] index)
static private void Decrypt (uint offset, byte[] key, byte[] index)
{
Decrypt (offset, key, index, 0, index.Length);
}
private void Decrypt (uint offset, byte[] key, byte[] index, int pos, int length)
static private void Decrypt (uint offset, byte[] key, byte[] index, int pos, int length)
{
uint key_last = (uint)key.Length-1;
for (uint i = 0; i < length; ++i)
index[pos+i] ^= key[(offset + i) % key_last];
}
internal class IndexReader
{
ArcView m_file;
int m_version;
int m_count;
byte[] m_data_key;
public byte[] DataKey { get { return m_data_key; } }
public IndexReader (ArcView file, int version, int count)
{
m_file = file;
m_version = version;
m_count = count;
}
byte[] m_chunk_buf = new byte[0x40];
public List<Entry> ReadIndex ()
{
byte[] title = m_file.View.ReadBytes (6, 0x40);
int title_length = Array.IndexOf<byte> (title, 0);
if (-1 == title_length)
title_length = title.Length;
string arc_filename = Path.GetFileName (m_file.Name);
if (0x200 != m_version)
arc_filename = arc_filename.ToLowerInvariant();
string base_filename = Path.GetFileNameWithoutExtension (arc_filename);
byte[] name_key = GenerateKey (Encodings.cp932.GetBytes (arc_filename));
uint index_offset = 0x200 + (uint)name_key.Select (x => (int)x).Sum();
for (int i = 0; i < name_key.Length; ++i)
{
index_offset ^= name_key[i];
index_offset = Binary.RotR (index_offset, 1);
}
for (int i = 0; i < name_key.Length; ++i)
{
index_offset ^= name_key[i];
index_offset = Binary.RotR (index_offset, 1);
}
index_offset %= 0x401;
index_offset += 0x4A;
byte[] index = m_file.View.ReadBytes (index_offset, (uint)(4*m_count));
if (index.Length != 4*m_count)
return null;
byte[] index_key = new byte[index.Length];
for (int i = 0; i < index_key.Length; ++i)
{
int v = name_key[(i+1) % name_key.Length] ^ (name_key[i % name_key.Length] + i);
index_key[i] = (byte)(m_count + v);
}
Decrypt (index_offset, index_key, index);
m_data_key = GenerateKey (title, title_length);
var dir = new List<Entry> (m_count);
int current_offset = 0;
uint next_offset = LittleEndian.ToUInt32 (index, current_offset);
for (int i = 0; i < m_count; ++i)
{
current_offset += 4;
uint entry_offset = next_offset;
if (entry_offset >= m_file.MaxOffset)
return null;
if (i + 1 == m_count)
next_offset = (uint)m_file.MaxOffset;
else
next_offset = LittleEndian.ToUInt32 (index, current_offset);
uint entry_size = next_offset - entry_offset;
var entry = ParseEntry (entry_offset, entry_size);
if (string.IsNullOrEmpty (entry.Name))
entry.Name = string.Format ("{0}#{1:D4}", base_filename, i);
dir.Add (entry);
}
return dir;
}
Entry ParseEntry (uint entry_offset, uint entry_size)
{
ReadChunk (entry_offset, 8);
if (0x200 == m_version)
return ParseEntryV2 (entry_offset, entry_size);
else
return ParseEntryV3 (entry_offset, entry_size);
}
void ReadChunk (uint offset, int chunk_size)
{
if (chunk_size > m_chunk_buf.Length)
m_chunk_buf = new byte[chunk_size];
if (chunk_size != m_file.View.Read (offset, m_chunk_buf, 0, (uint)chunk_size))
throw new InvalidFormatException();
Decrypt (offset, m_data_key, m_chunk_buf, 0, chunk_size);
}
Entry ParseEntryV2 (uint entry_offset, uint entry_size)
{
var data_size = LittleEndian.ToUInt32 (m_chunk_buf, 0);
if (data_size >= entry_size)
throw new InvalidFormatException();
var name_length = LittleEndian.ToInt32 (m_chunk_buf, 4);
entry_offset += 0x10;
var entry = new Entry();
if (name_length > 0)
{
ReadChunk (entry_offset+data_size, name_length);
if ('|' == m_chunk_buf[name_length-1])
--name_length;
entry.Name = Encodings.cp932.GetString (m_chunk_buf, 0, name_length);
entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name);
}
entry.Offset = entry_offset;
entry.Size = data_size;
return entry;
}
Entry ParseEntryV3 (uint entry_offset, uint entry_size)
{
if (!Binary.AsciiEqual (m_chunk_buf, "DSET"))
throw new InvalidFormatException();
uint chunk_offset = entry_offset + 10;
int chunk_count = LittleEndian.ToInt32 (m_chunk_buf, 4);
var entry = new Entry();
string filename = null;
for (int chunk = 0; chunk < chunk_count; ++chunk)
{
ReadChunk (chunk_offset, 8);
int chunk_size = LittleEndian.ToInt32 (m_chunk_buf, 4);
if (chunk_size <= 0)
throw new InvalidFormatException();
if (Binary.AsciiEqual (m_chunk_buf, "PICT"))
{
if (string.IsNullOrEmpty (entry.Type))
{
entry.Type = "image";
entry_offset = chunk_offset + 0x10;
entry_size = (uint)chunk_size - 6;
}
}
else if (null == filename && Binary.AsciiEqual (m_chunk_buf, "FTAG"))
{
ReadChunk (chunk_offset+10, chunk_size-2);
filename = Encodings.cp932.GetString (m_chunk_buf, 0, chunk_size-2);
}
chunk_offset += 10 + (uint)chunk_size;
}
if (!string.IsNullOrEmpty (filename))
entry.Name = DriveRe.Replace (filename, "");
entry.Offset = entry_offset;
entry.Size = entry_size;
return entry;
}
static readonly Regex DriveRe = new Regex (@"^(?:.+:)?\\+");
}
}
}

View File

@ -0,0 +1,92 @@
//! \file ArcXARC.cs
//! \date Fri Feb 12 20:00:44 2016
//! \brief Xuse XARC resource archive.
//
// 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.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using GameRes.Utility;
namespace GameRes.Formats.Xuse
{
[Export(typeof(ArchiveFormat))]
public class XarcOpener : ArchiveFormat
{
public override string Tag { get { return "XARC/XUSE"; } }
public override string Description { get { return "Xuse resource archive"; } }
public override uint Signature { get { return 0x43524158; } } // 'XARC'
public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } }
public XarcOpener ()
{
Extensions = new string[] { "arc" };
}
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
return null;
uint index_offset = 8;
uint first_offset = file.View.ReadUInt32 (index_offset);
if ((uint)count*4 + 10 != first_offset)
return null;
var base_name = Path.GetFileNameWithoutExtension (file.Name);
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
var entry = new Entry { Offset = file.View.ReadUInt32 (index_offset) };
dir.Add (entry);
index_offset += 4;
}
foreach (var entry in dir)
{
if (!file.View.AsciiEqual (entry.Offset, "DATA"))
return null;
uint name_length = file.View.ReadUInt16 (entry.Offset+0x18);
entry.Size = file.View.ReadUInt32 (entry.Offset+0x1C);
var name = file.View.ReadBytes (entry.Offset+0x20, name_length);
entry.Name = DecryptName (name);
entry.Offset += 0x22 + name_length;
uint signature = file.View.ReadUInt32 (entry.Offset);
var res = AutoEntry.DetectFileType (signature);
if (res != null)
entry.Type = res.Type;
}
return new ArcFile (file, this, dir);
}
string DecryptName (byte[] name)
{
for (int i = 0; i < name.Length; ++i)
{
name[i] = Binary.RotByteL (name[i], 4);
}
return Encodings.cp932.GetString (name);
}
}
}

View File

@ -264,6 +264,7 @@ Taijoku no Ori<br/>
Aiyoku no Apron<br/>
Para-Sol<br/>
Shouhei-kun no Hani-Kami Life☆<br/>
Touka Gettan<br/>
</td></tr>
<tr class="odd"><td>*</td><td><tt>\x00\x00\x04\x00</tt></td><td>No</td></tr>
<tr><td>*.war</td><td><tt>WARC 1.7</tt><br/><tt>WARC 1.5</tt><br/><tt>WARC 1.3</tt></td><td>No</td><td rowspan="5">Shiina Rio</td><td rowspan="5">
@ -358,6 +359,7 @@ Alea Akaki Tsuki o Haruka ni Nozomi<br/>
Iroha ~Aki no Yuuhi ni Kagefumi o~<br/>
Konneko<br/>
Natsu no Ame<br/>
Nerawareta Megami Tenshi Angeltia<br/>
Sakura Musubi<br/>
Santaful☆Summer<br/>
Space☆Trouble<br/>
@ -371,7 +373,6 @@ Dungeon Crusaderz 2 ~Eigou no Rakudo~<br/>
Onna Kyoushi<br/>
Magical Witch Academy<br/>
Medorei ~Okasareta Houkago~<br/>
Nerawareta Megami Tenshi Angeltia<br/>
Saishuu Chikan Densha<br/>
Serina<br/>
Ura Nyuugaku ~Ineki ni Nureta Kyoukasho~<br/>
@ -391,6 +392,7 @@ Ryoujoku Gojuusou<br/>
<tr class="odd"><td>*.eog</td><td><tt>CRM</tt></td><td>No</td></tr>
<tr class="odd"><td>*.zbm<br/>*.cwl</td><td><tt>SZDD</tt></td><td>No</td></tr>
<tr><td>*.pack</td><td><tt>FilePackVer1.0</tt><br/><tt>FilePackVer2.0</tt><br/><tt>FilePackVer3.0</tt></td><td>No</td><td rowspan="3">QLIE</td><td rowspan="3">
Bishoujo Mangekyou -Kami ga Tsukuritamouta Shoujo-tachi-<br/>
Makai Tenshi Djibril -episode 3-<br/>
Mehime no Toriko<br/>
Nanatsu no Fushigi no Owaru Toki<br/>
@ -448,6 +450,7 @@ Yuukyou Gangu 2<br/>
<tr class="odd"><td>*.wpn</td><td><tt>WBD\x1AWAV</tt></td><td>No</td></tr>
<tr class="odd"><td>*.wwa</td><td><tt>WPX\x1AWAV</tt></td><td>No</td></tr>
<tr><td>*.mrg</td><td><tt>MRG</tt></td><td>No</td><td rowspan="3">F&amp;C</td><td rowspan="3">
Asa no Konai Yoru ni Dakarete -Eternal Night-<br/>
Guren ni Somaru Gin no Rozario<br/>
Konata yori Kanata made<br/>
</td></tr>
@ -487,7 +490,8 @@ Answer Dead<br/>
Kango Sentai Nurse Ranger<br/>
</td></tr>
<tr class="odd"><td>*.abm</td><td><tt>BM</tt></td><td>No</td></tr>
<tr><td>*.arc<br/>*.xarc<br/>*.bin</td><td><tt>MIKO</tt><br/><tt>KOTORI</tt></td><td>No</td><td rowspan="2">Xuse<br/>ETERNAL</td><td rowspan="2">
<tr><td>*.arc<br/>*.xarc<br/>*.bin<br/>*.004</td><td><tt>MIKO</tt><br/><tt>KOTORI</tt><br/><tt>XARC</tt></td><td>No</td><td rowspan="2">Xuse<br/>ETERNAL</td><td rowspan="2">
Barbaroi<br/>
Kikouyoku Senki Gin no Toki no Corona<br/>
Kikouyoku Senki Tenkuu no Yumina<br/>
Nega0<br/>