mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
implemented Dogenzaka BIN archives (part of #24).
This commit is contained in:
150
ArcFormats/Dogenzaka/ArcBIN.cs
Normal file
150
ArcFormats/Dogenzaka/ArcBIN.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
//! \file ArcBIN.cs
|
||||
//! \date Wed Jul 06 09:46:31 2016
|
||||
//! \brief Dogenzaka Lab 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.Compression;
|
||||
|
||||
namespace GameRes.Formats.Dogenzaka
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class BinOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "BIN/Dogenzaka"; } }
|
||||
public override string Description { get { return "Dogenzaka Lab audio archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public BinOpener ()
|
||||
{
|
||||
Extensions = new string[] { "bin" };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (0);
|
||||
if (!IsSaneCount (count))
|
||||
return null;
|
||||
|
||||
var base_name = Path.GetFileNameWithoutExtension (file.Name);
|
||||
int index_offset = 4;
|
||||
int index_end = 4 + 8 * count;
|
||||
var dir = new List<Entry> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
var entry = new PackedEntry
|
||||
{
|
||||
Offset = file.View.ReadUInt32 (index_offset),
|
||||
Size = file.View.ReadUInt32 (index_offset+4),
|
||||
};
|
||||
if (entry.Offset < index_end || !entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
entry.Name = string.Format ("{0}#{1:D5}", base_name, i);
|
||||
dir.Add (entry);
|
||||
index_offset += 8;
|
||||
}
|
||||
foreach (PackedEntry entry in dir)
|
||||
{
|
||||
var n = file.View.ReadInt32 (entry.Offset);
|
||||
if (n <= 0)
|
||||
return null;
|
||||
var offset = file.View.ReadUInt32 (entry.Offset+4);
|
||||
var size = file.View.ReadUInt32 (entry.Offset+8);
|
||||
entry.Offset += offset;
|
||||
entry.Size = size & 0x3FFFFFFF;
|
||||
entry.IsPacked = 2 != (size >> 30);
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
var res = AutoEntry.DetectFileType (file.View.ReadUInt32 (entry.Offset));
|
||||
if (res != null)
|
||||
{
|
||||
entry.Type = res.Type;
|
||||
var ext = res.Extensions.FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty (ext))
|
||||
entry.Name = Path.ChangeExtension (entry.Name, ext);
|
||||
}
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
||||
var pent = entry as PackedEntry;
|
||||
if (null == pent || !pent.IsPacked)
|
||||
return input;
|
||||
return new LzssStream (input);
|
||||
}
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class GamedatOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "BIN/Dogenzaka/2"; } }
|
||||
public override string Description { get { return "Dogenzaka Lab archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public GamedatOpener ()
|
||||
{
|
||||
Extensions = new string[] { "bin" };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (0);
|
||||
if (!IsSaneCount (count-1))
|
||||
return null;
|
||||
uint base_offset = (uint)(4 + 4 * count);
|
||||
if (base_offset >= file.MaxOffset)
|
||||
return null;
|
||||
|
||||
uint index_offset = 4;
|
||||
uint next_offset = file.View.ReadUInt32 (index_offset);
|
||||
if (next_offset != 0)
|
||||
return null;
|
||||
var base_name = Path.GetFileNameWithoutExtension (file.Name);
|
||||
next_offset = base_offset;
|
||||
--count;
|
||||
var dir = new List<Entry> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
index_offset += 4;
|
||||
var name = string.Format ("{0}#{1:D4}", base_name, i);
|
||||
var entry = AutoEntry.Create (file, next_offset, name);
|
||||
next_offset = base_offset + file.View.ReadUInt32 (index_offset);
|
||||
if (next_offset > file.MaxOffset)
|
||||
return null;
|
||||
entry.Size = next_offset - (uint)entry.Offset;
|
||||
dir.Add (entry);
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
160
ArcFormats/Dogenzaka/ImageRSA.cs
Normal file
160
ArcFormats/Dogenzaka/ImageRSA.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
//! \file ImageRSA.cs
|
||||
//! \date Tue Jul 05 20:38:47 2016
|
||||
//! \brief RSA-encrypted PNG image.
|
||||
//
|
||||
// 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.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace GameRes.Formats.Dogenzaka
|
||||
{
|
||||
internal class Rc4PngMetaData : ImageMetaData
|
||||
{
|
||||
public byte[] Key;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class Rc4PngFormat : PngFormat
|
||||
{
|
||||
public override string Tag { get { return "PNG/RC4"; } }
|
||||
public override string Description { get { return "RC4 encrypted PNG image"; } }
|
||||
public override uint Signature { get { return 0xC4F7F61A; } }
|
||||
|
||||
public Rc4PngFormat ()
|
||||
{
|
||||
Extensions = new string[] { "a" };
|
||||
}
|
||||
|
||||
public static readonly byte[] KnownKey = Encoding.ASCII.GetBytes ("Hlk9D28p");
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
using (var sha = SHA1.Create())
|
||||
{
|
||||
var key = sha.ComputeHash (KnownKey).Take (16).ToArray();
|
||||
using (var proxy = new InputProxyStream (stream, true))
|
||||
using (var input = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read))
|
||||
{
|
||||
var info = base.ReadMetaData (input);
|
||||
if (null == info)
|
||||
return null;
|
||||
return new Rc4PngMetaData
|
||||
{
|
||||
Width = info.Width,
|
||||
Height = info.Height,
|
||||
OffsetX = info.OffsetX,
|
||||
OffsetY = info.OffsetY,
|
||||
BPP = info.BPP,
|
||||
Key = key,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
var rc4 = (Rc4PngMetaData)info;
|
||||
using (var sha = SHA1.Create())
|
||||
using (var proxy = new InputProxyStream (stream, true))
|
||||
using (var input = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read))
|
||||
return base.Read (input, info);
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException ("Rc4PngFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Rc4Transform : ICryptoTransform
|
||||
{
|
||||
private const int StateLength = 256;
|
||||
private const int BlockSize = 256;
|
||||
private byte[] m_state = new byte[StateLength];
|
||||
private int x;
|
||||
private int y;
|
||||
private byte[] m_key;
|
||||
|
||||
public bool CanReuseTransform { get { return false; } }
|
||||
public bool CanTransformMultipleBlocks { get { return true; } }
|
||||
public int InputBlockSize { get { return BlockSize; } }
|
||||
public int OutputBlockSize { get { return BlockSize; } }
|
||||
|
||||
public Rc4Transform (byte[] key)
|
||||
{
|
||||
m_key = key;
|
||||
x = 0;
|
||||
y = 0;
|
||||
|
||||
for (int i = 0; i < StateLength; ++i)
|
||||
{
|
||||
m_state[i] = (byte)i;
|
||||
}
|
||||
|
||||
int i1 = 0;
|
||||
int i2 = 0;
|
||||
|
||||
for (int i = 0; i < StateLength; ++i)
|
||||
{
|
||||
i2 = ((m_key[i1] & 0xFF) + m_state[i] + i2) & 0xFF;
|
||||
byte t = m_state[i];
|
||||
m_state[i] = m_state[i2];
|
||||
m_state[i2] = t;
|
||||
i1 = (i1+1) % m_key.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public int TransformBlock (byte[] inBuf, int inOffset, int inCount,
|
||||
byte[] outBuf, int outOffset)
|
||||
{
|
||||
for (int i = 0; i < inCount; i++)
|
||||
{
|
||||
x = (x + 1) & 0xFF;
|
||||
y = (m_state[x] + y) & 0xFF;
|
||||
|
||||
byte t = m_state[x];
|
||||
m_state[x] = m_state[y];
|
||||
m_state[y] = t;
|
||||
|
||||
outBuf[i+outOffset] = (byte)(inBuf[i + inOffset] ^ m_state[(m_state[x] + m_state[y]) & 0xFF]);
|
||||
}
|
||||
return inCount;
|
||||
}
|
||||
|
||||
public byte[] TransformFinalBlock (byte[] inBuf, int inOffset, int inCount)
|
||||
{
|
||||
byte[] output = new byte[inCount];
|
||||
TransformBlock (inBuf, inOffset, inCount, output, 0);
|
||||
return output;
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
GC.SuppressFinalize (this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user