GARbro-mirror/ArcFormats/Kaguya/ArcLINK.cs

831 lines
26 KiB
C#
Raw Normal View History

2016-10-22 18:22:14 +08:00
//! \file ArcLINK.cs
//! \date Fri Jan 22 18:44:56 2016
//! \brief KaGuYa archive format.
//
// Copyright (C) 2016-2017 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;
2016-10-22 18:22:14 +08:00
using System.Text;
namespace GameRes.Formats.Kaguya
{
2016-10-22 18:22:14 +08:00
internal class LinkEntry : PackedEntry
{
public bool IsEncrypted;
}
internal class LinkArchive : ArcFile
{
public readonly LinkEncryption Encryption;
2022-11-29 21:49:48 +08:00
public LinkArchive(ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, LinkEncryption enc)
: base(arc, impl, dir)
2016-10-22 18:22:14 +08:00
{
Encryption = enc;
}
}
[Export(typeof(ArchiveFormat))]
2016-03-21 05:31:31 +08:00
public class LinkOpener : ArchiveFormat
{
2022-11-29 21:49:48 +08:00
public override string Tag { get { return "ARC/LINK"; } }
public override string Description { get { return "KaGuYa script engine resource archive"; } }
2022-11-29 21:49:48 +08:00
public override uint Signature { get { return 0x4B4E494C; } } // 'LINK'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
2022-11-29 21:49:48 +08:00
public LinkOpener()
2016-03-21 05:31:31 +08:00
{
Extensions = new string[] { "arc" };
}
2022-11-29 21:49:48 +08:00
public override ArcFile TryOpen(ArcView file)
{
2022-11-29 21:49:48 +08:00
int version = file.View.ReadByte(4) - '0';
2016-10-22 18:22:14 +08:00
if (version < 3 || version > 6)
2022-11-29 21:49:48 +08:00
return ReadOldIndex(file);
2022-11-29 21:49:48 +08:00
using (var reader = LinkReader.Create(file, version))
{
2016-10-22 18:22:14 +08:00
var dir = reader.ReadIndex();
if (null == dir)
return null;
2016-10-22 18:22:14 +08:00
if (reader.HasEncrypted)
{
var enc = reader.GetEncryption();
if (enc != null)
2022-11-29 21:49:48 +08:00
return new LinkArchive(file, this, dir, enc);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
return new ArcFile(file, this, dir);
}
}
2022-11-29 21:49:48 +08:00
public override Stream OpenEntry(ArcFile arc, Entry entry)
{
2016-10-22 18:22:14 +08:00
var lent = entry as LinkEntry;
if (null == lent || (!lent.IsPacked && !lent.IsEncrypted))
{
if (entry.Size > 8)
{
2022-11-29 21:49:48 +08:00
uint unpacked_size = arc.File.View.ReadUInt32(entry.Offset);
int id = arc.File.View.ReadUInt16(entry.Offset + 5);
if (id == 0x4D42) // 'BM'
{
2022-11-29 21:49:48 +08:00
using (var input = arc.File.CreateStream(entry.Offset + 4, entry.Size - 4, entry.Name))
{
2022-11-29 21:49:48 +08:00
var data = Lin2Opener.UnpackLzss(input, unpacked_size);
return new BinMemoryStream(data, entry.Name);
}
}
}
2022-11-29 21:49:48 +08:00
return base.OpenEntry(arc, entry);
}
2016-10-22 18:22:14 +08:00
if (lent.IsEncrypted)
{
var larc = arc as LinkArchive;
if (null == larc)
2022-11-29 21:49:48 +08:00
return base.OpenEntry(arc, entry);
return larc.Encryption.DecryptEntry(larc, lent);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
using (var input = arc.File.CreateStream(entry.Offset, entry.Size))
using (var bmr = new BmrDecoder(input))
{
bmr.Unpack();
2022-11-29 21:49:48 +08:00
return new BinMemoryStream(bmr.Data, entry.Name);
}
}
2022-11-29 21:49:48 +08:00
internal ArcFile ReadOldIndex(ArcView file)
{
2022-11-29 21:49:48 +08:00
int count = file.View.ReadInt32(4);
if (!IsSaneCount(count))
return null;
2022-11-29 21:49:48 +08:00
var dir = new List<Entry>(count);
using (var index = file.CreateStream())
{
index.Position = 8;
uint names_size = index.ReadUInt32();
for (int i = 0; i < count; ++i)
{
var name = index.ReadCString();
2022-11-29 21:49:48 +08:00
var entry = FormatCatalog.Instance.Create<Entry>(name);
dir.Add(entry);
}
index.Position = 12 + names_size;
foreach (var entry in dir)
{
entry.Offset = index.ReadUInt32();
2022-11-29 21:49:48 +08:00
entry.Size = index.ReadUInt32();
if (!entry.CheckPlacement(file.MaxOffset))
return null;
}
}
2022-11-29 21:49:48 +08:00
return new ArcFile(file, this, dir);
}
}
2016-10-22 18:22:14 +08:00
internal class LinkReader : IDisposable
{
2022-11-29 21:49:48 +08:00
IBinaryStream m_input;
readonly long m_max_offset;
bool m_has_encrypted;
2016-10-22 18:22:14 +08:00
2022-11-29 21:49:48 +08:00
protected LinkReader(ArcView file)
2016-10-22 18:22:14 +08:00
{
m_input = file.CreateStream();
m_max_offset = file.MaxOffset;
m_has_encrypted = false;
}
public IBinaryStream Input { get { return m_input; } }
2022-11-29 21:49:48 +08:00
public bool HasEncrypted { get { return m_has_encrypted; } }
2016-10-22 18:22:14 +08:00
2022-11-29 21:49:48 +08:00
public static LinkReader Create(ArcView file, int version)
2016-10-22 18:22:14 +08:00
{
if (version < 4)
2022-11-29 21:49:48 +08:00
return new LinkReader(file);
2016-10-22 18:22:14 +08:00
else if (version < 6)
2022-11-29 21:49:48 +08:00
return new Link4Reader(file);
2016-10-22 18:22:14 +08:00
else
2022-11-29 21:49:48 +08:00
return new Link6Reader(file);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
protected virtual long GetDataOffset()
2016-10-22 18:22:14 +08:00
{
return 8;
}
2022-11-29 21:49:48 +08:00
protected virtual string ReadName()
2016-10-22 18:22:14 +08:00
{
int name_length = m_input.ReadUInt8();
2022-11-29 21:49:48 +08:00
Skip(2);
return m_input.ReadCString(name_length);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
public List<Entry> ReadIndex()
2016-10-22 18:22:14 +08:00
{
m_input.Position = GetDataOffset();
var header = new byte[4];
var dir = new List<Entry>();
while (m_input.Position + 4 < m_max_offset)
{
long base_offset = m_input.Position;
uint size = m_input.ReadUInt32();
if (0 == size)
break;
if (size < 0x10)
return null;
2022-11-29 21:49:48 +08:00
int flags = m_input.ReadUInt16();
2016-10-22 18:22:14 +08:00
bool is_compressed = (flags & 3) != 0;
2022-11-29 21:49:48 +08:00
Skip(7);
2016-10-22 18:22:14 +08:00
var name = ReadName();
2022-11-29 21:49:48 +08:00
if (string.IsNullOrEmpty(name))
2016-10-22 18:22:14 +08:00
return null;
2022-11-29 21:49:48 +08:00
var entry = FormatCatalog.Instance.Create<LinkEntry>(name);
2016-10-22 18:22:14 +08:00
entry.Offset = m_input.Position;
2022-11-29 21:49:48 +08:00
entry.Size = size - (uint)(entry.Offset - base_offset);
2016-10-22 18:22:14 +08:00
if (is_compressed)
{
2022-11-29 21:49:48 +08:00
m_input.Read(header, 0, 4);
if (header.AsciiEqual("BMR"))
2016-10-22 18:22:14 +08:00
{
entry.IsPacked = true;
entry.UnpackedSize = m_input.ReadUInt32();
}
}
entry.IsEncrypted = (flags & 4) != 0;
m_has_encrypted = m_has_encrypted || entry.IsEncrypted;
2022-11-29 21:49:48 +08:00
dir.Add(entry);
2016-10-22 18:22:14 +08:00
m_input.Position = entry.Offset + entry.Size;
}
return dir;
}
2022-11-29 21:49:48 +08:00
public virtual LinkEncryption GetEncryption()
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
var params_dir = VFS.GetDirectoryName(m_input.Name);
var params_dat = VFS.CombinePath(params_dir, "params.dat");
if (!VFS.FileExists(params_dat))
2016-10-22 18:22:14 +08:00
return null;
2022-11-29 21:49:48 +08:00
using (var input = VFS.OpenBinaryStream(params_dat))
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
var param = ParamsDeserializer.Create(input);
2016-10-25 22:29:07 +08:00
return param.GetEncryption();
2016-10-22 18:22:14 +08:00
}
}
2022-11-29 21:49:48 +08:00
protected void Skip(int amount)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
m_input.Seek(amount, SeekOrigin.Current);
2016-10-22 18:22:14 +08:00
}
#region IDisposable Members
2022-11-29 21:49:48 +08:00
public void Dispose()
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
Dispose(true);
GC.SuppressFinalize(this);
2016-10-22 18:22:14 +08:00
}
bool _disposed = false;
2022-11-29 21:49:48 +08:00
protected virtual void Dispose(bool disposing)
2016-10-22 18:22:14 +08:00
{
if (!_disposed)
{
if (disposing)
m_input.Dispose();
_disposed = true;
}
}
#endregion
}
internal class Link4Reader : LinkReader
{
2022-11-29 21:49:48 +08:00
public Link4Reader(ArcView file) : base(file)
2016-10-22 18:22:14 +08:00
{
}
2022-11-29 21:49:48 +08:00
protected override long GetDataOffset()
2016-10-22 18:22:14 +08:00
{
return 0xA;
}
}
internal class Link6Reader : LinkReader
{
2022-11-29 21:49:48 +08:00
public Link6Reader(ArcView file) : base(file)
2016-10-22 18:22:14 +08:00
{
}
2022-11-29 21:49:48 +08:00
protected override long GetDataOffset()
2016-10-22 18:22:14 +08:00
{
Input.Position = 7;
return 8 + Input.ReadUInt8();
}
byte[] name_buffer = new byte[0x100];
2022-11-29 21:49:48 +08:00
protected override string ReadName()
2016-10-22 18:22:14 +08:00
{
int name_length = Input.ReadUInt16();
if (name_length > 0x400)
throw new InvalidFormatException();
if (name_length > name_buffer.Length)
name_buffer = new byte[name_length];
2022-11-29 21:49:48 +08:00
Input.Read(name_buffer, 0, name_length);
return Encoding.Unicode.GetString(name_buffer, 0, name_length);
2016-10-22 18:22:14 +08:00
}
}
internal class BmrDecoder : IDisposable
{
2022-11-29 21:49:48 +08:00
byte[] m_output;
MsbBitStream m_input;
int m_final_size;
int m_step;
int m_key;
public byte[] Data { get { return m_output; } }
2022-11-29 21:49:48 +08:00
public BmrDecoder(IBinaryStream input)
{
input.Position = 3;
m_step = input.ReadUInt8();
m_final_size = input.ReadInt32();
m_key = input.ReadInt32();
int unpacked_size = input.ReadInt32();
m_output = new byte[unpacked_size];
2022-11-29 21:49:48 +08:00
m_input = new MsbBitStream(input.AsStream, true);
}
2022-11-29 21:49:48 +08:00
public void Unpack()
{
m_input.Input.Position = 0x14;
UnpackHuffman();
2016-10-22 18:22:14 +08:00
UndoMoveToFront();
2022-11-29 21:49:48 +08:00
m_output = Decode(m_output, m_key);
if (m_step != 0)
2022-11-29 21:49:48 +08:00
m_output = DecompressRLE(m_output);
}
2022-11-29 21:49:48 +08:00
byte[] DecompressRLE(byte[] input)
{
var result = new byte[m_final_size];
int src = 0;
for (int i = 0; i < m_step; ++i)
{
2016-10-22 18:22:14 +08:00
byte v1 = input[src++];
result[i] = v1;
int dst = i + m_step;
while (dst < result.Length)
{
2016-10-22 18:22:14 +08:00
byte v2 = input[src++];
result[dst] = v2;
dst += m_step;
if (v2 == v1)
{
2016-10-22 18:22:14 +08:00
int count = input[src++];
if (0 != (count & 0x80))
2016-10-22 18:22:14 +08:00
count = input[src++] + ((count & 0x7F) << 8) + 128;
2022-11-29 21:49:48 +08:00
while (count-- > 0 && dst < result.Length)
{
result[dst] = v2;
dst += m_step;
}
2016-10-22 18:22:14 +08:00
if (dst < result.Length)
{
2016-10-22 18:22:14 +08:00
v2 = input[src++];
result[dst] = v2;
dst += m_step;
}
}
v1 = v2;
}
}
return result;
}
2022-11-29 21:49:48 +08:00
void UndoMoveToFront()
{
2016-10-22 18:22:14 +08:00
var dict = new byte[256];
for (int i = 0; i < 256; ++i)
2016-10-22 18:22:14 +08:00
dict[i] = (byte)i;
for (int i = 0; i < m_output.Length; ++i)
{
byte v = m_output[i];
2016-10-22 18:22:14 +08:00
m_output[i] = dict[v];
for (int j = v; j > 0; --j)
{
2022-11-29 21:49:48 +08:00
dict[j] = dict[j - 1];
}
2016-10-22 18:22:14 +08:00
dict[0] = m_output[i];
}
}
2022-11-29 21:49:48 +08:00
byte[] Decode(byte[] input, int key)
{
var freq_table = new int[256];
for (int i = 0; i < input.Length; ++i)
{
++freq_table[input[i]];
}
for (int i = 1; i < 256; ++i)
{
2022-11-29 21:49:48 +08:00
freq_table[i] += freq_table[i - 1];
}
var distrib_table = new int[input.Length];
2022-11-29 21:49:48 +08:00
for (int i = input.Length - 1; i >= 0; --i)
{
int v = input[i];
2016-10-22 18:22:14 +08:00
int freq = --freq_table[v];
distrib_table[freq] = i;
}
int pos = key;
var copy_out = new byte[input.Length];
for (int i = 0; i < copy_out.Length; ++i)
{
pos = distrib_table[pos];
copy_out[i] = input[pos];
}
return copy_out;
}
2022-11-29 21:49:48 +08:00
ushort m_token;
ushort[,] m_tree = new ushort[2, 256];
2022-11-29 21:49:48 +08:00
void UnpackHuffman()
{
m_token = 256;
ushort root = CreateHuffmanTree();
int dst = 0;
while (dst < m_output.Length)
{
ushort symbol = root;
while (symbol >= 0x100)
{
int bit = m_input.GetNextBit();
if (-1 == bit)
throw new EndOfStreamException();
2022-11-29 21:49:48 +08:00
symbol = m_tree[bit, symbol - 256];
}
m_output[dst++] = (byte)symbol;
}
}
2022-11-29 21:49:48 +08:00
ushort CreateHuffmanTree()
{
if (0 != m_input.GetNextBit())
{
ushort v = m_token++;
2022-11-29 21:49:48 +08:00
m_tree[0, v - 256] = CreateHuffmanTree();
m_tree[1, v - 256] = CreateHuffmanTree();
return v;
}
else
{
2022-11-29 21:49:48 +08:00
return (ushort)m_input.GetBits(8);
}
}
#region IDisposable Members
bool _disposed = false;
2022-11-29 21:49:48 +08:00
public void Dispose()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}
2016-10-22 18:22:14 +08:00
2016-10-25 22:29:07 +08:00
internal abstract class ParamsDeserializer
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
protected IBinaryStream m_input;
protected string m_title;
protected Version m_version;
2016-10-22 18:22:14 +08:00
2022-11-29 21:49:48 +08:00
protected ParamsDeserializer(IBinaryStream input, Version version)
2016-10-22 18:22:14 +08:00
{
m_input = input;
m_version = version;
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
public static ParamsDeserializer Create(IBinaryStream input)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
var header = input.ReadHeader(0x11);
if (header.AsciiEqual("[SCR-PARAMS]v0"))
{
Version version;
if ('.' == header[15])
2022-11-29 21:49:48 +08:00
version = Version.Parse(header.GetCString(13, 4));
else
2022-11-29 21:49:48 +08:00
version = new Version(header[14] - '0', 0);
2017-12-26 23:54:53 +08:00
if (2 == version.Major)
2022-11-29 21:49:48 +08:00
return new ParamsV2Deserializer(input, version);
else if (version.Major < 5)
2022-11-29 21:49:48 +08:00
return new ParamsV4Deserializer(input, version);
2017-12-26 23:54:53 +08:00
else if (5 == version.Major && (version.Minor >= 4 && version.Minor <= 7))
2022-11-29 21:49:48 +08:00
return new ParamsV5Deserializer(input, version);
}
throw new UnknownEncryptionScheme();
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
public virtual LinkEncryption GetEncryption()
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
return new LinkEncryption(GetKey());
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
public abstract byte[] GetKey();
2016-10-25 22:29:07 +08:00
2022-11-29 21:49:48 +08:00
protected byte[] ReadKey()
2016-10-22 18:22:14 +08:00
{
int key_length = m_input.ReadInt32();
2022-11-29 21:49:48 +08:00
return m_input.ReadBytes(key_length);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
protected virtual string ReadString()
2016-10-22 18:22:14 +08:00
{
int length = m_input.ReadUInt8();
2022-11-29 21:49:48 +08:00
return m_input.ReadCString(length);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
protected virtual void SkipString()
2016-10-22 18:22:14 +08:00
{
SkipChunk();
}
2022-11-29 21:49:48 +08:00
protected void Skip(int amount)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
m_input.Seek(amount, SeekOrigin.Current);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
protected void SkipChunk()
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
Skip(m_input.ReadUInt8());
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
protected void SkipArray()
2016-10-25 22:29:07 +08:00
{
int count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
SkipChunk();
}
2022-11-29 21:49:48 +08:00
protected void SkipDict()
2016-10-22 18:22:14 +08:00
{
int count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
SkipString();
SkipString();
}
}
2022-11-29 21:49:48 +08:00
protected void ReadHeader(int start)
{
m_input.Position = start;
SkipChunk();
m_title = ReadString();
if (m_version.Major < 3)
m_input.ReadCString();
SkipString();
SkipString();
m_input.ReadByte();
SkipString();
SkipString();
SkipDict();
m_input.ReadByte();
}
2016-10-22 18:22:14 +08:00
}
2016-10-25 22:29:07 +08:00
internal class ParamsV2Deserializer : ParamsDeserializer
{
2022-11-29 21:49:48 +08:00
public ParamsV2Deserializer(IBinaryStream input, Version version) : base(input, version)
2016-10-25 22:29:07 +08:00
{
}
2022-11-29 21:49:48 +08:00
public override LinkEncryption GetEncryption()
2016-10-25 22:29:07 +08:00
{
var key = GetKey();
2022-11-29 21:49:48 +08:00
return new LinkEncryption(key, m_title != "幼なじみと甘~くエッチに過ごす方法");
2016-10-25 22:29:07 +08:00
}
2022-11-29 21:49:48 +08:00
public override byte[] GetKey()
2016-10-25 22:29:07 +08:00
{
2022-11-29 21:49:48 +08:00
ReadHeader(0x17);
2016-10-25 22:29:07 +08:00
if ("幼なじみと甘~くエッチに過ごす方法" == m_title)
{
int count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
m_input.ReadByte();
SkipChunk();
SkipArray();
SkipChunk();
}
SkipArray();
SkipArray();
m_input.ReadInt32();
2022-11-29 21:49:48 +08:00
return m_input.ReadBytes(240000);
2016-10-25 22:29:07 +08:00
}
else // 毎日がM!
{
int count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
m_input.ReadByte();
SkipChunk();
SkipArray();
SkipArray();
}
SkipDict();
count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
SkipChunk();
SkipArray();
SkipArray();
}
return ReadKey();
}
}
}
internal class ParamsV4Deserializer : ParamsDeserializer
{
2022-11-29 21:49:48 +08:00
public ParamsV4Deserializer(IBinaryStream input, Version version) : base(input, version)
{
}
2022-11-29 21:49:48 +08:00
public override byte[] GetKey()
{
2022-11-29 21:49:48 +08:00
ReadHeader(0x19);
2022-11-29 21:49:48 +08:00
Skip(m_version.Major < 5 ? 12 : 11);
int count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
m_input.ReadByte();
SkipChunk();
SkipArray();
SkipArray();
}
SkipDict();
count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
SkipChunk();
SkipArray();
SkipArray();
}
return ReadKey();
}
}
2016-10-22 18:22:14 +08:00
internal class ParamsV5Deserializer : ParamsDeserializer
{
2022-11-29 21:49:48 +08:00
public ParamsV5Deserializer(IBinaryStream input, Version version) : base(input, version)
2016-10-22 18:22:14 +08:00
{
}
2022-11-29 21:49:48 +08:00
public override byte[] GetKey()
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
ReadHeader(0x1B);
2016-10-22 18:22:14 +08:00
2022-11-29 21:49:48 +08:00
Skip(m_version.Minor <= 4 ? 15 : 16);
2016-10-22 18:22:14 +08:00
for (int i = 0; i < 3; ++i)
{
if (0 != m_input.ReadUInt8())
SkipTree();
}
2022-11-29 21:49:48 +08:00
Skip(m_input.ReadInt32() * 0xC);
2016-10-22 18:22:14 +08:00
return ReadKey();
}
2022-11-29 21:49:48 +08:00
protected override void SkipString()
2016-10-22 18:22:14 +08:00
{
int length = m_input.ReadUInt16();
2022-11-29 21:49:48 +08:00
Skip(length);
2016-10-22 18:22:14 +08:00
}
byte[] name_buffer = new byte[0x100];
2022-11-29 21:49:48 +08:00
protected override string ReadString()
2016-10-22 18:22:14 +08:00
{
int length = m_input.ReadUInt16();
if (length > name_buffer.Length)
name_buffer = new byte[length];
2022-11-29 21:49:48 +08:00
m_input.Read(name_buffer, 0, length);
return Encoding.Unicode.GetString(name_buffer, 0, length);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
protected void SkipTree()
2016-10-22 18:22:14 +08:00
{
SkipString();
int count = m_input.ReadInt32();
2022-11-29 21:49:48 +08:00
while (count-- > 0)
2016-10-22 18:22:14 +08:00
{
SkipString();
SkipString();
}
count = m_input.ReadInt32();
2022-11-29 21:49:48 +08:00
while (count-- > 0)
2016-10-22 18:22:14 +08:00
SkipTree();
}
}
internal class LinkEncryption
{
2022-11-29 21:49:48 +08:00
byte[] m_key;
2016-10-22 18:22:14 +08:00
Tuple<string, Decryptor>[] m_type_table;
2022-11-29 21:49:48 +08:00
delegate Stream Decryptor(LinkArchive arc, LinkEntry entry);
2016-10-22 18:22:14 +08:00
2022-11-29 21:49:48 +08:00
static readonly ResourceInstance<AnmOpener> An00 = new ResourceInstance<AnmOpener>("ANM/KAGUYA");
static readonly ResourceInstance<An10Opener> An10 = new ResourceInstance<An10Opener>("AN10/KAGUYA");
static readonly ResourceInstance<An20Opener> An20 = new ResourceInstance<An20Opener>("AN20/KAGUYA");
static readonly ResourceInstance<Pl00Opener> Pl00 = new ResourceInstance<Pl00Opener>("PLT/KAGUYA");
2022-11-29 21:49:48 +08:00
public LinkEncryption(byte[] key, bool anm_encrypted = true)
2016-10-22 18:22:14 +08:00
{
if (null == key || 0 == key.Length)
2022-11-29 21:49:48 +08:00
throw new ArgumentException("Invalid encryption key");
2016-10-22 18:22:14 +08:00
m_key = key;
2016-10-25 22:29:07 +08:00
var table = new List<Tuple<string, Decryptor>>
2016-10-22 18:22:14 +08:00
{
new Tuple<string, Decryptor> ("BM", (a, e) => DecryptImage (a, e, 0x36)),
new Tuple<string, Decryptor> ("AP-2", (a, e) => DecryptImage (a, e, 0x18)),
new Tuple<string, Decryptor> ("AP-3", (a, e) => DecryptImage (a, e, 0x18)),
new Tuple<string, Decryptor> ("AP", (a, e) => DecryptImage (a, e, 0xC)),
};
2016-10-25 22:29:07 +08:00
if (anm_encrypted)
{
2022-11-29 21:49:48 +08:00
table.Add(new Tuple<string, Decryptor>("AN00", (a, e) => DecryptAnm(a, e, An00.Value)));
table.Add(new Tuple<string, Decryptor>("AN10", (a, e) => DecryptAnm(a, e, An10.Value)));
table.Add(new Tuple<string, Decryptor>("AN20", (a, e) => DecryptAnm(a, e, An20.Value)));
table.Add(new Tuple<string, Decryptor>("AN21", (a, e) => DecryptAn21(a, e)));
table.Add(new Tuple<string, Decryptor>("PL00", (a, e) => DecryptAnm(a, e, Pl00.Value)));
table.Add(new Tuple<string, Decryptor>("PL10", (a, e) => DecryptPl10(a, e)));
}
2016-10-25 22:29:07 +08:00
m_type_table = table.ToArray();
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
public Stream DecryptEntry(LinkArchive arc, LinkEntry entry)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
var header = arc.File.View.ReadBytes(entry.Offset, 4);
2016-10-22 18:22:14 +08:00
foreach (var type in m_type_table)
{
2022-11-29 21:49:48 +08:00
if (header.AsciiEqual(type.Item1))
return type.Item2(arc, entry);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
return arc.File.CreateStream(entry.Offset, entry.Size);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
Stream DecryptImage(LinkArchive arc, LinkEntry entry, uint data_offset)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
var header = arc.File.View.ReadBytes(entry.Offset, data_offset);
Stream body = arc.File.CreateStream(entry.Offset + data_offset, entry.Size - data_offset);
body = new ByteStringEncryptedStream(body, m_key);
return new PrefixStream(header, body);
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
Stream DecryptAnm(LinkArchive arc, LinkEntry entry, IAnmReader reader)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
var data = arc.File.View.ReadBytes(entry.Offset, entry.Size);
var input = new BinMemoryStream(data, entry.Name);
var dir = reader.GetFramesList(input);
if (dir != null)
2016-10-22 18:22:14 +08:00
{
2022-11-29 21:49:48 +08:00
foreach (AnmEntry frame in dir)
{
DecryptData(data, (int)frame.ImageDataOffset, (int)frame.ImageDataSize);
}
2016-10-22 18:22:14 +08:00
}
2022-11-29 21:49:48 +08:00
input.Position = 0;
return input;
}
2022-11-29 21:49:48 +08:00
Stream DecryptAn21(LinkArchive arc, LinkEntry entry)
2022-02-13 15:55:43 +08:00
{
var data = arc.File.View.ReadBytes(entry.Offset, entry.Size);
int count = data.ToUInt16(4);
int offset = 8;
for (int i = 0; i < count; ++i)
{
switch (data[offset++])
{
case 0: break;
case 1: offset += 8; break;
case 2:
case 3:
case 4:
case 5: offset += 4; break;
2022-02-13 15:55:43 +08:00
default: return new BinMemoryStream(data, entry.Name);
}
}
count = data.ToUInt16(offset);
offset += 2 + count * 8 + 0x21;
2022-11-29 21:49:48 +08:00
int w = data.ToInt32(offset);
int h = data.ToInt32(offset + 4);
int channels = data.ToInt32(offset + 8);
offset += 12;
2022-11-29 21:49:48 +08:00
DecryptData(data, offset, channels * w * h);
2022-02-13 00:55:33 +08:00
return new BinMemoryStream(data, entry.Name);
}
2022-11-29 21:49:48 +08:00
Stream DecryptPl10(LinkArchive arc, LinkEntry entry)
2022-02-12 21:33:18 +08:00
{
var data = arc.File.View.ReadBytes(entry.Offset, entry.Size);
int offset = 30;
int w = data.ToInt32(offset);
int h = data.ToInt32(offset + 4);
int channels = data.ToInt32(offset + 8);
offset += 12;
DecryptData(data, offset, channels * w * h);
return new BinMemoryStream(data, entry.Name);
}
2022-11-29 21:49:48 +08:00
void DecryptData(byte[] data, int index, int length)
2016-10-22 18:22:14 +08:00
{
while (length > 0)
{
2022-11-29 21:49:48 +08:00
int count = Math.Min(length, m_key.Length);
2016-10-22 18:22:14 +08:00
for (int i = 0; i < count; ++i)
{
data[index++] ^= m_key[i];
}
length -= count;
}
}
}
2022-11-29 21:49:48 +08:00
}