mirror of
https://github.com/crskycode/GARbro.git
synced 2026-06-27 14:18:06 +08:00
reorganized project directory structure.
This commit is contained in:
425
ArcFormats/CatSystem/ArcINT.cs
Normal file
425
ArcFormats/CatSystem/ArcINT.cs
Normal file
@@ -0,0 +1,425 @@
|
||||
//! \file ArcINT.cs
|
||||
//! \date Fri Jul 11 09:32:36 2014
|
||||
//! \brief Frontwing games archive.
|
||||
//
|
||||
// Copyright (C) 2014 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.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Simias.Encryption;
|
||||
using System.Runtime.InteropServices;
|
||||
using GameRes.Formats.Strings;
|
||||
using GameRes.Formats.Properties;
|
||||
|
||||
namespace GameRes.Formats.CatSystem
|
||||
{
|
||||
public class FrontwingArchive : ArcFile
|
||||
{
|
||||
public readonly Blowfish Encryption;
|
||||
|
||||
public FrontwingArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, Blowfish cipher)
|
||||
: base (arc, impl, dir)
|
||||
{
|
||||
Encryption = cipher;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable()]
|
||||
public class IntEncryptionInfo
|
||||
{
|
||||
public uint? Key { get; set; }
|
||||
public string Scheme { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
||||
public uint? GetKey ()
|
||||
{
|
||||
if (null != Key && Key.HasValue)
|
||||
return Key;
|
||||
|
||||
if (!string.IsNullOrEmpty (Scheme))
|
||||
{
|
||||
IntOpener.KeyData keydata;
|
||||
if (IntOpener.KnownSchemes.TryGetValue (Scheme, out keydata))
|
||||
return keydata.Key;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty (Password))
|
||||
return IntOpener.EncodePassPhrase (Password);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class IntOptions : ResourceOptions
|
||||
{
|
||||
public IntEncryptionInfo EncryptionInfo { get; set; }
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class IntOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "INT"; } }
|
||||
public override string Description { get { return arcStrings.INTDescription; } }
|
||||
public override uint Signature { get { return 0x0046494b; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanCreate { get { return true; } }
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
uint entry_count = file.View.ReadUInt32 (4);
|
||||
if (0 == entry_count || 0 != ((entry_count - 1) >> 0x14))
|
||||
{
|
||||
Trace.WriteLine (string.Format ("Invalid entry count ({0})", entry_count));
|
||||
return null;
|
||||
}
|
||||
if (file.View.AsciiEqual (8, "__key__.dat\x00"))
|
||||
{
|
||||
uint? key = QueryEncryptionInfo();
|
||||
if (null == key)
|
||||
throw new UnknownEncryptionScheme();
|
||||
return OpenEncrypted (file, entry_count, key.Value);
|
||||
}
|
||||
|
||||
long current_offset = 8;
|
||||
var dir = new List<Entry> ((int)entry_count);
|
||||
for (uint i = 0; i < entry_count; ++i)
|
||||
{
|
||||
string name = file.View.ReadString (current_offset, 0x40);
|
||||
var entry = FormatCatalog.Instance.CreateEntry (name);
|
||||
entry.Offset = file.View.ReadUInt32 (current_offset+0x40);
|
||||
entry.Size = file.View.ReadUInt32 (current_offset+0x44);
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
current_offset += 0x48;
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
|
||||
private ArcFile OpenEncrypted (ArcView file, uint entry_count, uint main_key)
|
||||
{
|
||||
if (1 == entry_count)
|
||||
return null; // empty archive
|
||||
long current_offset = 8;
|
||||
var twister = new Twister();
|
||||
|
||||
// [@@L1] = 32-bit key
|
||||
// [@@L1+4] = 0 if key is available, -1 otherwise
|
||||
uint key_data = file.View.ReadUInt32 (current_offset+0x44);
|
||||
uint twist_key = twister.Twist (key_data);
|
||||
// [@@L0] = 32-bit twist key
|
||||
byte[] blowfish_key = BitConverter.GetBytes (twist_key);
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
Array.Reverse (blowfish_key);
|
||||
|
||||
var blowfish = new Blowfish (blowfish_key);
|
||||
var dir = new List<Entry> ((int)entry_count-1);
|
||||
byte[] name_info = new byte[0x40];
|
||||
for (uint i = 1; i < entry_count; ++i)
|
||||
{
|
||||
current_offset += 0x48;
|
||||
file.View.Read (current_offset, name_info, 0, 0x40);
|
||||
uint eax = file.View.ReadUInt32 (current_offset+0x40);
|
||||
uint edx = file.View.ReadUInt32 (current_offset+0x44);
|
||||
eax += i;
|
||||
blowfish.Decipher (ref eax, ref edx);
|
||||
uint key = twister.Twist (main_key + i);
|
||||
string name = DecipherName (name_info, key);
|
||||
|
||||
var entry = FormatCatalog.Instance.CreateEntry (name);
|
||||
entry.Offset = eax;
|
||||
entry.Size = edx;
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
}
|
||||
return new FrontwingArchive (file, this, dir, blowfish);
|
||||
}
|
||||
|
||||
private Stream OpenEncryptedEntry (FrontwingArchive arc, Entry entry)
|
||||
{
|
||||
using (var view = arc.File.CreateViewAccessor (entry.Offset, entry.Size))
|
||||
{
|
||||
byte[] data = new byte[entry.Size];
|
||||
// below is supposedly faster version of
|
||||
//arc.File.View.Read (entry.Offset, data, 0, entry.Size);
|
||||
unsafe
|
||||
{
|
||||
byte* ptr = view.GetPointer (entry.Offset);
|
||||
try {
|
||||
Marshal.Copy (new IntPtr(ptr), data, 0, data.Length);
|
||||
} finally {
|
||||
view.SafeMemoryMappedViewHandle.ReleasePointer();
|
||||
}
|
||||
}
|
||||
arc.Encryption.Decipher (data, data.Length/8*8);
|
||||
return new MemoryStream (data, false);
|
||||
}
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
if (arc is FrontwingArchive)
|
||||
return OpenEncryptedEntry (arc as FrontwingArchive, entry);
|
||||
else
|
||||
return base.OpenEntry (arc, entry);
|
||||
}
|
||||
|
||||
public string DecipherName (byte[] name, uint key)
|
||||
{
|
||||
key += (key >> 8) + (key >> 16) + (key >> 24);
|
||||
key &= 0xff;
|
||||
key %= 0x34;
|
||||
int count = 0;
|
||||
for (int i = 0; i < name.Length; ++i)
|
||||
{
|
||||
byte al = name[i];
|
||||
if (0 == al)
|
||||
break;
|
||||
byte bl = (byte)key;
|
||||
++count;
|
||||
uint edx = al;
|
||||
al |= 0x20;
|
||||
al -= 0x61;
|
||||
if (al < 0x1a)
|
||||
{
|
||||
if (0 != (edx & 0x20))
|
||||
al += 0x1a;
|
||||
al = (byte)~al;
|
||||
al += 0x34;
|
||||
if (al >= bl)
|
||||
al -= bl;
|
||||
else
|
||||
al = (byte)(al - bl + 0x34);
|
||||
if (al >= 0x1a)
|
||||
al += 6;
|
||||
al += 0x41;
|
||||
name[i] = al;
|
||||
}
|
||||
++key;
|
||||
if (0x34 == key)
|
||||
key = 0;
|
||||
}
|
||||
return Encodings.cp932.GetString (name, 0, count);
|
||||
}
|
||||
|
||||
class Twister
|
||||
{
|
||||
const uint TwisterLength = 0x270;
|
||||
uint[] m_twister = new uint[TwisterLength];
|
||||
uint m_twister_pos = 0;
|
||||
|
||||
public uint Twist (uint key)
|
||||
{
|
||||
Init (key);
|
||||
return Next();
|
||||
}
|
||||
|
||||
public void Init (uint key)
|
||||
{
|
||||
uint edx = key;
|
||||
for (int i = 0; i < TwisterLength; ++i)
|
||||
{
|
||||
uint ecx = edx * 0x10dcd + 1;
|
||||
m_twister[i] = (edx & 0xffff0000) | (ecx >> 16);
|
||||
edx *= 0x1C587629;
|
||||
edx += 0x10dce;
|
||||
}
|
||||
m_twister_pos = 0;
|
||||
}
|
||||
|
||||
public uint Next ()
|
||||
{
|
||||
uint ecx = m_twister[m_twister_pos];
|
||||
uint edx = m_twister_pos + 1;
|
||||
if (TwisterLength == edx)
|
||||
edx = 0;
|
||||
uint edi = m_twister[edx];
|
||||
edi = ((edi ^ ecx) & 0x7FFFFFFF) ^ ecx;
|
||||
bool carry = 0 != (edi & 1);
|
||||
edi >>= 1;
|
||||
if (carry)
|
||||
edi ^= 0x9908B0DF;
|
||||
ecx = m_twister_pos + 0x18d;
|
||||
if (ecx >= TwisterLength)
|
||||
ecx -= TwisterLength;
|
||||
edi ^= m_twister[ecx];
|
||||
m_twister[m_twister_pos] = edi;
|
||||
m_twister_pos = edx;
|
||||
uint eax = edi ^ (edi >> 11);
|
||||
eax = ((eax & 0xFF3A58AD) << 7) ^ eax;
|
||||
eax = ((eax & 0xFFFFDF8C) << 15) ^ eax;
|
||||
eax = (eax >> 18) ^ eax;
|
||||
return eax;
|
||||
}
|
||||
}
|
||||
|
||||
public static uint EncodePassPhrase (string password)
|
||||
{
|
||||
byte[] pass_bytes = Encodings.cp932.GetBytes (password);
|
||||
uint key = 0xffffffff;
|
||||
foreach (var c in pass_bytes)
|
||||
{
|
||||
uint val = (uint)c << 24;
|
||||
key ^= val;
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
bool carry = 0 != (key & 0x80000000);
|
||||
key <<= 1;
|
||||
if (carry)
|
||||
key ^= 0x4C11DB7;
|
||||
}
|
||||
key = ~key;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
public struct KeyData
|
||||
{
|
||||
public uint Key;
|
||||
public string Passphrase;
|
||||
}
|
||||
|
||||
public static readonly Dictionary<string, KeyData> KnownSchemes = new Dictionary<string, KeyData> {
|
||||
{ "Grisaia no Kajitsu", new KeyData { Key=0x1DAD9120, Passphrase="FW-6JD55162" }},
|
||||
{ "Shukufuku no Campanella", new KeyData { Key=0x4260E643, Passphrase="CAMPANELLA" }},
|
||||
{ "Makai Tenshi Djibril -Episode 4-", new KeyData { Key=0xA5A166AA, Passphrase="FW_MAKAI-TENSHI_DJIBRIL4" }},
|
||||
{ "Sengoku Tenshi Djibril (trial)", new KeyData { Key=0xef870610, Passphrase="FW-8O9B6WDS" }},
|
||||
};
|
||||
|
||||
public override ResourceOptions GetDefaultOptions ()
|
||||
{
|
||||
return new IntOptions {
|
||||
EncryptionInfo = Settings.Default.INTEncryption ?? new IntEncryptionInfo(),
|
||||
};
|
||||
}
|
||||
|
||||
public override ResourceOptions GetOptions (object w)
|
||||
{
|
||||
var widget = w as GUI.WidgetINT;
|
||||
if (null != widget)
|
||||
{
|
||||
Settings.Default.INTEncryption = widget.Info;
|
||||
return new IntOptions { EncryptionInfo = widget.Info };
|
||||
}
|
||||
return this.GetDefaultOptions();
|
||||
}
|
||||
|
||||
public override object GetAccessWidget ()
|
||||
{
|
||||
return new GUI.WidgetINT ();
|
||||
}
|
||||
|
||||
public override object GetCreationWidget ()
|
||||
{
|
||||
return new GUI.CreateINTWidget();
|
||||
}
|
||||
|
||||
uint? QueryEncryptionInfo ()
|
||||
{
|
||||
var options = Query<IntOptions> (arcStrings.INTNotice);
|
||||
return options.EncryptionInfo.GetKey();
|
||||
}
|
||||
|
||||
public override void Create (Stream output, IEnumerable<Entry> list, ResourceOptions options,
|
||||
EntryCallback callback)
|
||||
{
|
||||
int file_count = list.Count();
|
||||
if (null != callback)
|
||||
callback (file_count+2, null, null);
|
||||
int callback_count = 0;
|
||||
using (var writer = new BinaryWriter (output, Encoding.ASCII, true))
|
||||
{
|
||||
writer.Write (Signature);
|
||||
writer.Write (file_count);
|
||||
long dir_offset = output.Position;
|
||||
|
||||
var encoding = Encodings.cp932.WithFatalFallback();
|
||||
byte[] name_buf = new byte[0x40];
|
||||
int previous_size = 0;
|
||||
|
||||
if (null != callback)
|
||||
callback (callback_count++, null, arcStrings.MsgWritingIndex);
|
||||
|
||||
// first, write names only
|
||||
foreach (var entry in list)
|
||||
{
|
||||
string name = Path.GetFileName (entry.Name);
|
||||
try
|
||||
{
|
||||
int size = encoding.GetBytes (name, 0, name.Length, name_buf, 0);
|
||||
for (int i = size; i < previous_size; ++i)
|
||||
name_buf[i] = 0;
|
||||
previous_size = size;
|
||||
}
|
||||
catch (EncoderFallbackException X)
|
||||
{
|
||||
throw new InvalidFileName (entry.Name, arcStrings.MsgIllegalCharacters, X);
|
||||
}
|
||||
catch (ArgumentException X)
|
||||
{
|
||||
throw new InvalidFileName (entry.Name, arcStrings.MsgFileNameTooLong, X);
|
||||
}
|
||||
writer.Write (name_buf);
|
||||
writer.BaseStream.Seek (8, SeekOrigin.Current);
|
||||
}
|
||||
|
||||
// now, write files and remember offset/sizes
|
||||
long current_offset = output.Position;
|
||||
foreach (var entry in list)
|
||||
{
|
||||
if (null != callback)
|
||||
callback (callback_count++, entry, arcStrings.MsgAddingFile);
|
||||
|
||||
entry.Offset = current_offset;
|
||||
using (var input = File.OpenRead (entry.Name))
|
||||
{
|
||||
var size = input.Length;
|
||||
if (size > uint.MaxValue || current_offset + size > uint.MaxValue)
|
||||
throw new FileSizeException();
|
||||
current_offset += (uint)size;
|
||||
entry.Size = (uint)size;
|
||||
input.CopyTo (output);
|
||||
}
|
||||
}
|
||||
|
||||
if (null != callback)
|
||||
callback (callback_count++, null, arcStrings.MsgUpdatingIndex);
|
||||
|
||||
// at last, go back to directory and write offset/sizes
|
||||
dir_offset += 0x40;
|
||||
foreach (var entry in list)
|
||||
{
|
||||
writer.BaseStream.Position = dir_offset;
|
||||
writer.Write ((uint)entry.Offset);
|
||||
writer.Write (entry.Size);
|
||||
dir_offset += 0x48;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
ArcFormats/CatSystem/CreateINTWidget.xaml
Normal file
6
ArcFormats/CatSystem/CreateINTWidget.xaml
Normal file
@@ -0,0 +1,6 @@
|
||||
<Grid x:Class="GameRes.Formats.GUI.CreateINTWidget"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:s="clr-namespace:GameRes.Formats.Strings">
|
||||
<TextBlock Text="{x:Static s:arcStrings.INTCreationNotice}" Margin="5"/>
|
||||
</Grid>
|
||||
15
ArcFormats/CatSystem/CreateINTWidget.xaml.cs
Normal file
15
ArcFormats/CatSystem/CreateINTWidget.xaml.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace GameRes.Formats.GUI
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for CreateINTWidget.xaml
|
||||
/// </summary>
|
||||
public partial class CreateINTWidget : Grid
|
||||
{
|
||||
public CreateINTWidget ()
|
||||
{
|
||||
InitializeComponent ();
|
||||
}
|
||||
}
|
||||
}
|
||||
362
ArcFormats/CatSystem/ImageHG3.cs
Normal file
362
ArcFormats/CatSystem/ImageHG3.cs
Normal file
@@ -0,0 +1,362 @@
|
||||
//! \file ImageHG3.cs
|
||||
//! \date Sat Jul 19 17:31:09 2014
|
||||
//! \brief Frontwing HG3 image format implementation.
|
||||
//
|
||||
// Copyright (C) 2014 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.IO;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Media;
|
||||
using GameRes.Compression;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.CatSystem
|
||||
{
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class Hg3Format : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "HG3"; } }
|
||||
public override string Description { get { return "Frontwing proprietary image format"; } }
|
||||
public override uint Signature { get { return 0x332d4748; } }
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
{
|
||||
var header = new byte[0x4c];
|
||||
if (0x4c != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
if (LittleEndian.ToUInt32 (header, 0) != Signature)
|
||||
return null;
|
||||
if (LittleEndian.ToUInt32 (header, 4) != 0x0c)
|
||||
return null;
|
||||
if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0"))
|
||||
return null;
|
||||
if (0x38 != LittleEndian.ToUInt32 (header, 0x1c))
|
||||
return null;
|
||||
if (0x20 != LittleEndian.ToUInt32 (header, 0x2c))
|
||||
return null;
|
||||
uint width = LittleEndian.ToUInt32 (header, 0x24); // @@L0
|
||||
uint height = LittleEndian.ToUInt32 (header, 0x28);
|
||||
int pos_x = LittleEndian.ToInt32 (header, 0x30);
|
||||
int pos_y = LittleEndian.ToInt32 (header, 0x34);
|
||||
pos_x -= LittleEndian.ToInt32 (header, 0x44);
|
||||
pos_y -= LittleEndian.ToInt32 (header, 0x48);
|
||||
|
||||
return new ImageMetaData
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
OffsetX = pos_x,
|
||||
OffsetY = pos_y,
|
||||
BPP = 32,
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
{
|
||||
stream.Position = 0x40;
|
||||
bool flipped = 0 == (stream.ReadByte() & 1) || info.OffsetY < 0;
|
||||
stream.Position = 0x4c;
|
||||
var header = new byte[0x28];
|
||||
if (0x28 != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
if (!Binary.AsciiEqual (header, "img0000\0"))
|
||||
return null;
|
||||
uint data_size = LittleEndian.ToUInt32 (header, 0x0c);
|
||||
if (data_size < 0x18)
|
||||
return null;
|
||||
if (info.Height != LittleEndian.ToUInt32 (header, 0x14))
|
||||
return null;
|
||||
uint packed2_size = LittleEndian.ToUInt32 (header, 0x18);
|
||||
uint unpacked2_size = LittleEndian.ToUInt32 (header, 0x1c);
|
||||
uint packed1_size = LittleEndian.ToUInt32 (header, 0x20);
|
||||
uint unpacked1_size = LittleEndian.ToUInt32 (header, 0x24);
|
||||
if (packed2_size + packed1_size < packed2_size)
|
||||
return null;
|
||||
if (unpacked2_size + unpacked1_size < unpacked2_size)
|
||||
return null;
|
||||
|
||||
long data_pos = stream.Position;
|
||||
using (var unpacked2 = ZLibCompressor.DeCompress (stream))
|
||||
{
|
||||
stream.Position = data_pos + packed2_size;
|
||||
using (var unpacked1 = ZLibCompressor.DeCompress (stream))
|
||||
{
|
||||
var decoder = new Decoder (unpacked1.GetBuffer(), unpacked1_size,
|
||||
unpacked2.GetBuffer(), unpacked2_size,
|
||||
info.Width, info.Height);
|
||||
decoder.Unpack();
|
||||
var pixels = decoder.Data;
|
||||
int stride = (int)info.Width * 4;
|
||||
var bitmap = BitmapSource.Create ((int)info.Width, (int)info.Height, 96, 96,
|
||||
PixelFormats.Bgra32, null, pixels, stride);
|
||||
if (flipped)
|
||||
{
|
||||
var flipped_bitmap = new TransformedBitmap();
|
||||
flipped_bitmap.BeginInit();
|
||||
flipped_bitmap.Source = bitmap;
|
||||
flipped_bitmap.Transform = new ScaleTransform { ScaleY = -1 };
|
||||
flipped_bitmap.EndInit();
|
||||
bitmap = flipped_bitmap;
|
||||
}
|
||||
bitmap.Freeze();
|
||||
return new ImageData (bitmap, info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new NotImplementedException ("Hg3Format.Write not implemented");
|
||||
}
|
||||
|
||||
class Decoder
|
||||
{
|
||||
byte[] m_in1;
|
||||
byte[] m_in2;
|
||||
byte[] m_image;
|
||||
uint m_in1_size;
|
||||
uint m_in2_size;
|
||||
uint m_dst_size;
|
||||
uint m_width;
|
||||
uint m_height;
|
||||
|
||||
public byte[] Data { get { return m_image; } }
|
||||
|
||||
public Decoder (byte[] in1, uint in1_size, byte[] in2, uint in2_size,
|
||||
uint width, uint height)
|
||||
{
|
||||
m_in1 = in1;
|
||||
m_in1_size = in1_size;
|
||||
m_in2 = in2;
|
||||
m_in2_size = in2_size;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_dst_size = width*height*4;
|
||||
m_image = new byte[(int)m_dst_size];
|
||||
}
|
||||
|
||||
uint esi;
|
||||
uint edi;
|
||||
uint eax;
|
||||
uint ebx;
|
||||
uint ecx;
|
||||
uint edx;
|
||||
|
||||
uint L0, L1, L2, L3, L4;
|
||||
uint m_plane;
|
||||
|
||||
public void Unpack ()
|
||||
{
|
||||
m_plane = 0;
|
||||
edi = 0;
|
||||
esi = 0;
|
||||
ebx = 0;
|
||||
eax = m_in1_size;
|
||||
L0 = m_in2_size;
|
||||
L1 = 0;
|
||||
bool skip_first = GetNextBit();
|
||||
Proc4();
|
||||
ecx = m_dst_size;
|
||||
if (eax > m_dst_size)
|
||||
throw new InvalidFormatException ("Underflow at Hg3Format.Decoder.Unpack()");
|
||||
m_dst_size -= eax;
|
||||
ecx >>= 2;
|
||||
L2 = eax;
|
||||
L3 = ecx;
|
||||
L4 = ecx;
|
||||
for (;;)
|
||||
{
|
||||
if (!skip_first)
|
||||
{
|
||||
// @@1:
|
||||
if (0 == L2)
|
||||
break;
|
||||
Proc4();
|
||||
ecx = eax;
|
||||
if (ecx > L2)
|
||||
throw new InvalidFormatException ("Overflow at Hg3Format.Decoder.Unpack()");
|
||||
L2 -= ecx;
|
||||
eax = 0;
|
||||
do
|
||||
Proc2();
|
||||
while (0 != --ecx);
|
||||
}
|
||||
// @@1a:
|
||||
if (0 == L2)
|
||||
break;
|
||||
Proc4();
|
||||
ecx = eax;
|
||||
if (ecx > L2 || ecx > L0)
|
||||
throw new InvalidFormatException ("Overflow (2) at Hg3Format.Decoder.Unpack()");
|
||||
L2 -= ecx;
|
||||
L0 -= ecx;
|
||||
do
|
||||
{
|
||||
eax = m_in2[L1++];
|
||||
Proc2();
|
||||
}
|
||||
while (0 != --ecx);
|
||||
skip_first = false;
|
||||
}
|
||||
// @@7:
|
||||
ecx = m_dst_size;
|
||||
esi = 0;
|
||||
if (0 != ecx)
|
||||
{
|
||||
eax = 0;
|
||||
do
|
||||
Proc2();
|
||||
while (0 != --ecx);
|
||||
}
|
||||
Proc6 (m_width, m_height);
|
||||
// @@9:
|
||||
eax = 0;
|
||||
edx = m_in1_size;
|
||||
}
|
||||
|
||||
void Proc2 () // @@2
|
||||
{
|
||||
m_image[edi] = (byte)eax;
|
||||
edi += 4;
|
||||
if (0 == --L3)
|
||||
{
|
||||
edi = ++m_plane;
|
||||
L3 = L4;
|
||||
}
|
||||
}
|
||||
|
||||
bool GetNextBit () // @@3
|
||||
{
|
||||
bool carry = 0 != (ebx & 1);
|
||||
ebx >>= 1;
|
||||
if (0 == ebx)
|
||||
{
|
||||
if (0 == m_in1_size--)
|
||||
throw new InvalidFormatException ("Hg3Format.Decoder.Underflow at GetNextBit()");
|
||||
ebx = (uint)(m_in1[esi++] | 0x100);
|
||||
carry = 0 != (ebx & 1);
|
||||
ebx >>= 1;
|
||||
}
|
||||
return carry;
|
||||
}
|
||||
|
||||
void Proc4 () // @@4
|
||||
{
|
||||
ecx = 0;
|
||||
eax = 0;
|
||||
do
|
||||
{
|
||||
if (ecx >= 0x20)
|
||||
throw new InvalidFormatException ("Hg3Format.Decoder.Overflow at Proc4");
|
||||
++ecx;
|
||||
}
|
||||
while (!GetNextBit());
|
||||
++eax;
|
||||
while (0 != --ecx)
|
||||
{
|
||||
eax += eax + (uint)(GetNextBit() ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
void Proc6 (uint width, uint height)
|
||||
{
|
||||
uint[] table = new uint[0x100];
|
||||
ecx = 0;
|
||||
for (uint i = 0; i < 0x100; ++i)
|
||||
{
|
||||
eax = 0xffffffff;
|
||||
edx = i;
|
||||
do
|
||||
{
|
||||
eax >>= 2;
|
||||
eax |= (edx & 3) << 30;
|
||||
eax >>= 6;
|
||||
edx >>= 2;
|
||||
}
|
||||
while (0 != (0x80 & eax));
|
||||
table[i] = eax;
|
||||
}
|
||||
ecx = width * height * 4;
|
||||
for (uint i = 0; i < ecx; i += 4)
|
||||
{
|
||||
eax = m_image[i];
|
||||
edx = table[eax];
|
||||
edx <<= 2;
|
||||
eax = m_image[i+1];
|
||||
edx += table[eax];
|
||||
edx <<= 2;
|
||||
eax = m_image[i+2];
|
||||
edx += table[eax];
|
||||
edx <<= 2;
|
||||
eax = m_image[i+3];
|
||||
edx += table[eax];
|
||||
m_image[i] = (byte)(edx);
|
||||
m_image[i+1] = (byte)(edx >> 8);
|
||||
m_image[i+2] = (byte)(edx >> 16);
|
||||
m_image[i+3] = (byte)(edx >> 24);
|
||||
}
|
||||
edi = 0;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
eax = m_image[edi];
|
||||
edx = (uint)(0 == (eax & 1) ? 0 : 0xff);
|
||||
eax >>= 1;
|
||||
eax ^= edx;
|
||||
m_image[edi++] = (byte)eax;
|
||||
}
|
||||
ecx = width;
|
||||
if (0 != --ecx)
|
||||
{
|
||||
ecx <<= 2;
|
||||
do
|
||||
{
|
||||
eax = m_image[edi];
|
||||
edx = (uint)(0 == (eax & 1) ? 0 : 0xff);
|
||||
eax >>= 1;
|
||||
eax ^= edx;
|
||||
eax += m_image[edi-4];
|
||||
m_image[edi++] = (byte)eax;
|
||||
}
|
||||
while (0 != --ecx);
|
||||
}
|
||||
ecx = height;
|
||||
if (0 != --ecx)
|
||||
{
|
||||
uint stride = width*4;
|
||||
ecx *= stride;
|
||||
do
|
||||
{
|
||||
eax = m_image[edi];
|
||||
edx = (uint)(0 == (eax & 1) ? 0 : 0xff);
|
||||
eax >>= 1;
|
||||
eax ^= edx;
|
||||
eax += m_image[edi-stride];
|
||||
m_image[edi++] = (byte)eax;
|
||||
}
|
||||
while (0 != --ecx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
ArcFormats/CatSystem/WidgetINT.xaml
Normal file
59
ArcFormats/CatSystem/WidgetINT.xaml
Normal file
@@ -0,0 +1,59 @@
|
||||
<Grid x:Class="GameRes.Formats.GUI.WidgetINT"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:s="clr-namespace:GameRes.Formats.Strings"
|
||||
xmlns:fmt="clr-namespace:GameRes.Formats.CatSystem"
|
||||
xmlns:local="clr-namespace:GameRes.Formats.GUI"
|
||||
MaxWidth="260">
|
||||
<Grid.Resources>
|
||||
<local:KeyConverter x:Key="keyConverter"/>
|
||||
<Style TargetType="{x:Type TextBox}">
|
||||
<Style.Triggers>
|
||||
<Trigger Property="Validation.HasError" Value="true">
|
||||
<Setter Property="ToolTip"
|
||||
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition MinWidth="130" Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition/>
|
||||
<RowDefinition/>
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
<Label Content="{x:Static s:arcStrings.INTLabelNumericKey}" Target="{Binding ElementName=Passkey}"
|
||||
Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right"/>
|
||||
<TextBox Name="Passkey" Grid.Column="1" Grid.Row="0" Margin="0,3,0,3">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Key" Converter="{StaticResource keyConverter}" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:PasskeyRule/>
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
<Validation.ErrorTemplate>
|
||||
<ControlTemplate>
|
||||
<DockPanel>
|
||||
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontWeight="Bold" Text="!" VerticalAlignment="Center"/>
|
||||
<Border BorderBrush="Red" BorderThickness="1">
|
||||
<AdornedElementPlaceholder Name="ValidationAdorner" />
|
||||
</Border>
|
||||
</DockPanel>
|
||||
</ControlTemplate>
|
||||
</Validation.ErrorTemplate>
|
||||
</TextBox>
|
||||
<Label Content="{x:Static s:arcStrings.LabelPassphrase}" Target="{Binding ElementName=Passphrase}"
|
||||
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right"/>
|
||||
<TextBox Name="Passphrase" Grid.Column="1" Grid.Row="1" Margin="0,3,0,3"
|
||||
Text="{Binding Path=Password}"/>
|
||||
<Label Content="{x:Static s:arcStrings.LabelScheme}" Target="{Binding ElementName=EncScheme}"
|
||||
Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right"/>
|
||||
<ComboBox Name="EncScheme" Grid.Column="1" Grid.Row="2" Margin="0,3,0,0"
|
||||
ItemsSource="{Binding Source={x:Static fmt:IntOpener.KnownSchemes}, Path=Keys, Mode=OneWay}"
|
||||
Width="{Binding ElementName=Passkey, Path=ActualWidth}"
|
||||
SelectedValue="{Binding Path=Scheme}"/>
|
||||
</Grid>
|
||||
97
ArcFormats/CatSystem/WidgetINT.xaml.cs
Normal file
97
ArcFormats/CatSystem/WidgetINT.xaml.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using GameRes.Formats.CatSystem;
|
||||
|
||||
namespace GameRes.Formats.GUI
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for WidgetINT.xaml
|
||||
/// </summary>
|
||||
public partial class WidgetINT : Grid
|
||||
{
|
||||
public WidgetINT ()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.DataContext = GameRes.Formats.Properties.Settings.Default.INTEncryption ?? new IntEncryptionInfo();
|
||||
|
||||
Passphrase.TextChanged += OnPassphraseChanged;
|
||||
EncScheme.SelectionChanged += OnSchemeChanged;
|
||||
}
|
||||
|
||||
public IntEncryptionInfo Info { get { return this.DataContext as IntEncryptionInfo; } }
|
||||
|
||||
void OnPasskeyChanged (object sender, TextChangedEventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
void OnPassphraseChanged (object sender, TextChangedEventArgs e)
|
||||
{
|
||||
var widget = sender as TextBox;
|
||||
uint key = IntOpener.EncodePassPhrase (widget.Text);
|
||||
Passkey.Text = key.ToString ("X8");
|
||||
}
|
||||
|
||||
void OnSchemeChanged (object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
var widget = sender as ComboBox;
|
||||
IntOpener.KeyData keydata;
|
||||
if (IntOpener.KnownSchemes.TryGetValue (widget.SelectedItem as string, out keydata))
|
||||
{
|
||||
Passphrase.TextChanged -= OnPassphraseChanged;
|
||||
try
|
||||
{
|
||||
Passphrase.Text = keydata.Passphrase;
|
||||
Passkey.Text = keydata.Key.ToString ("X8");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Passphrase.TextChanged += OnPassphraseChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[ValueConversion(typeof(uint?), typeof(string))]
|
||||
public class KeyConverter : IValueConverter
|
||||
{
|
||||
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
uint? key = (uint?)value;
|
||||
return null != key ? key.Value.ToString ("X") : "";
|
||||
}
|
||||
|
||||
public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
string strValue = value as string;
|
||||
uint result_key;
|
||||
if (uint.TryParse(strValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result_key))
|
||||
return new uint? (result_key);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class PasskeyRule : ValidationRule
|
||||
{
|
||||
public PasskeyRule()
|
||||
{
|
||||
}
|
||||
|
||||
public override ValidationResult Validate (object value, CultureInfo cultureInfo)
|
||||
{
|
||||
uint key = 0;
|
||||
try
|
||||
{
|
||||
if (((string)value).Length > 0)
|
||||
key = UInt32.Parse ((string)value, NumberStyles.HexNumber);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new ValidationResult (false, Strings.arcStrings.INTKeyRequirement);
|
||||
}
|
||||
return new ValidationResult (true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user