implemented ecrypted ACTGS archives.

This commit is contained in:
morkt 2018-01-09 04:30:43 +04:00
parent 444a15b94a
commit 77459967fa
3 changed files with 143 additions and 31 deletions

79
ArcFormats/Actgs/ArcCG.cs Normal file
View File

@ -0,0 +1,79 @@
//! \file ArcCG.cs
//! \date 2018 Jan 09
//! \brief ACTGS engine encrypted archive.
//
// Copyright (C) 2018 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.Linq;
namespace GameRes.Formats.Actgs
{
[Export(typeof(ArchiveFormat))]
public class CgOpener : DatOpener
{
public override string Tag { get { return "CG/ACTGS"; } }
public override string Description { get { return "ACTGS engine resource archive"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
var key = FindKey (file);
if (null == key)
return null;
uint signature_key = key.ToUInt32 (0);
int count = (int)(file.View.ReadUInt32 (0) ^ signature_key);
if (!IsSaneCount (count))
return null;
uint index_size = 32 * (uint)count;
using (var enc = file.CreateStream (0x10, index_size))
using (var input = new ByteStringEncryptedStream (enc, key))
using (var index = new BinaryStream (input, file.Name))
{
var reader = new IndexReader (file.MaxOffset);
var dir = reader.Read (index, count);
if (null == dir)
return null;
return new ActressArchive (file, this, dir, key);
}
}
byte[] FindKey (ArcView file)
{
var pattern = file.View.ReadBytes (4, 8);
return Array.Find (KnownKeys, k => KeySequence (k).Skip (4).Take (8).SequenceEqual (pattern));
}
internal static IEnumerable<byte> KeySequence (byte[] key)
{
for (;;)
{
for (int i = 0; i < key.Length; ++i)
yield return key[i];
}
}
}
}

View File

@ -64,7 +64,7 @@ namespace GameRes.Formats.Actgs
Extensions = new string[] { "dat" };
}
public static byte[][] KnownKeys = { };
internal static byte[][] KnownKeys = { };
public override ArcFile TryOpen (ArcView file)
{
@ -74,40 +74,34 @@ namespace GameRes.Formats.Actgs
if (0 != (file.View.ReadInt32 (4) | file.View.ReadInt32 (8) | file.View.ReadInt32 (12)))
return null;
const int entry_size = 0x20;
var index = new byte[count * entry_size];
if (index.Length != file.View.Read (0x10, index, 0, (uint)index.Length))
return null;
uint first_offset = 0x10u + (uint)index.Length;
uint actual_offset = LittleEndian.ToUInt32 (index, 0);
byte[] key = null;
if (actual_offset != first_offset)
uint index_length = (uint)(count * entry_size);
IBinaryStream input = file.CreateStream (0x10, index_length);
try
{
key = FindKey (first_offset, actual_offset);
uint first_offset = 0x10u + index_length;
uint actual_offset = input.Signature;
byte[] key = null;
if (actual_offset != first_offset)
{
key = FindKey (first_offset, actual_offset);
if (null == key)
return null;
var decrypted = new ByteStringEncryptedStream (input.AsStream, key);
input = new BinaryStream (decrypted, file.Name);
}
var reader = new IndexReader (file.MaxOffset);
var dir = reader.Read (input, count);
if (null == dir)
return null;
if (null == key)
return null;
Decrypt (index, 0, index.Length, key);
return new ArcFile (file, this, dir);
else
return new ActressArchive (file, this, dir, key);
}
int index_offset = 0;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
finally
{
var name = Binary.GetCString (index, index_offset+8, 0x18);
if (0 == name.Length)
return null;
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = LittleEndian.ToUInt32 (index, index_offset);
entry.Size = LittleEndian.ToUInt32 (index, index_offset+4);
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
index_offset += 0x20;
input.Dispose();
}
if (null == key)
return new ArcFile (file, this, dir);
else
return new ActressArchive (file, this, dir, key);
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
@ -148,7 +142,7 @@ namespace GameRes.Formats.Actgs
return Array.Find (KnownKeys, k => k.Take (4).SequenceEqual (pattern));
}
static void Decrypt (byte[] data, int index, int length, byte[] key)
internal static void Decrypt (byte[] data, int index, int length, byte[] key)
{
for (int i = 0; i < length; ++i)
{
@ -162,4 +156,42 @@ namespace GameRes.Formats.Actgs
set { KnownKeys = ((ActressScheme)value).KnownKeys; }
}
}
internal sealed class IndexReader
{
long m_arc_length;
List<Entry> m_dir = new List<Entry>();
public IndexReader (long arc_length)
{
m_arc_length = arc_length;
}
public List<Entry> Read (IBinaryStream input, int count)
{
m_dir.Clear();
if (m_dir.Capacity < count)
m_dir.Capacity = count;
try
{
for (int i = 0; i < count; ++i)
{
uint offset = input.ReadUInt32();
uint size = input.ReadUInt32();
var name = input.ReadCString (0x18);
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = offset;
entry.Size = size;
if (!entry.CheckPlacement (m_arc_length))
return null;
m_dir.Add (entry);
}
return m_dir;
}
catch
{
return null;
}
}
}
}

View File

@ -78,6 +78,7 @@
<Compile Include="Abogado\ArcDSK.cs" />
<Compile Include="Abogado\AudioADP.cs" />
<Compile Include="Abogado\ImageKG.cs" />
<Compile Include="Actgs\ArcCG.cs" />
<Compile Include="Actgs\ArcDAT.cs" />
<Compile Include="AdvSys\ArcAdvSys3.cs" />
<Compile Include="AdvSys\ImageGWD.cs" />