diff --git a/Legacy/BRoom/ArcCPC.cs b/Legacy/BRoom/ArcCPC.cs new file mode 100644 index 00000000..3675eed3 --- /dev/null +++ b/Legacy/BRoom/ArcCPC.cs @@ -0,0 +1,109 @@ +//! \file ArcCPC.cs +//! \date 2019 Jan 02 +//! \brief Studio B-Room resource archive. +// +// Copyright (C) 2019 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; + +// [040604][Studio B-Room] Dice-ki! ~Koi wa Un Makase~ + +namespace GameRes.Formats.BRoom +{ + [Export(typeof(ArchiveFormat))] + public class CpcOpener : ArchiveFormat + { + public override string Tag { get { return "CPC"; } } + public override string Description { get { return "Studio B-Room resource archive"; } } + public override uint Signature { get { return 0x47435043; } } // 'CPCG' + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public override ArcFile TryOpen (ArcView file) + { + int count = (int)(file.View.ReadUInt32 (4) ^ 0xFF559977); + if (!IsSaneCount (count)) + return null; + bool encryption_flag = (file.View.ReadByte (8) ^ 0x8A) != 0; + int key_index = file.View.ReadByte (9) ^ 0xCE; + if (encryption_flag && key_index > OffsetKey.Length) + return null; + uint index_offset = 12; + var name_buffer = new byte[0x30]; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + uint offset = file.View.ReadUInt32 (index_offset); + uint size = file.View.ReadUInt32 (index_offset+4); + if (encryption_flag) + { + uint key = IndexKey[i & 0x3F]; + offset ^= key ^ OffsetKey[key_index]; + size ^= key ^ LengthKey[key_index]; + } + else + { + offset ^= (uint)i ^ 0x35846u; + size ^= (uint)i ^ 0x57982525u; + } + file.View.Read (index_offset+8, name_buffer, 0, 0x30); + int j; + for (j = 0; j < 0x30; ++j) + { + name_buffer[j] ^= NameKey[j]; + if (0 == name_buffer[j]) + break; + } + var name = Encodings.cp932.GetString (name_buffer, 0, j); + if (string.IsNullOrWhiteSpace (name)) + return null; + var entry = Create (name); + entry.Offset = offset; + entry.Size = size; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + index_offset += 0x38; + } + return new ArcFile (file, this, dir); + } + + static readonly byte[] NameKey = { + 0x13, 0x60, 0xFC, 0x4D, 0xDE, 0xD0, 0x79, 0xB3, 0x51, 0xC5, 0xEC, 0x9E, 0x06, 0x82, 0x63, 0x73, + 0x21, 0xAB, 0xBF, 0x1A, 0x32, 0x9C, 0xBA, 0xFA, 0x5D, 0xFF, 0x29, 0x25, 0xB8, 0x7F, 0xCF, 0xF4, + 0x75, 0x93, 0x05, 0x40, 0x0C, 0xA3, 0x6A, 0x04, 0x98, 0x67, 0x47, 0xEF, 0x8B, 0xAD, 0x56, 0x65, + }; + static readonly ushort[] IndexKey = { + 0x27F6, 0x940B, 0x611F, 0xD845, 0xE733, 0xE871, 0x8A11, 0x360E, 0xC7AA, 0x31BB, 0xB23A, 0xC957, + 0x28D2, 0xBF73, 0x1DFF, 0x29EB, 0xD3C2, 0x6CC6, 0xDF7B, 0xA22E, 0xB82B, 0x9256, 0xCEEC, 0xDC08, + 0xA96A, 0xE52D, 0x5F96, 0x7959, 0x81A4, 0x990D, 0x6826, 0xAF38, 0x1B01, 0x2A19, 0x679D, 0x494E, + 0x555C, 0xE623, 0xB797, 0x6214, 0x3CAD, 0xDECD, 0x775B, 0x16A7, 0x37CC, 0xE3AE, 0xD6D5, 0x9F9B, + 0x8C1E, 0xCAF3, 0x8BB1, 0x6DC5, 0x1320, 0xBA1A, 0x42BC, 0xED2F, 0xDAB9, 0xA89C, 0x53F9, 0x4691, + 0xF4E4, 0xFBD1, 0xE982, 0xBEB4, + }; + static readonly uint[] OffsetKey = { 0x89D9A054, 0x74E297E9, 0xEECA074F, 0xF2A42CE8, 0x2D6FBE0E }; + static readonly uint[] LengthKey = { 0x101C2885, 0x5F7E52F8, 0x3812A6B4, 0x99696CA1, 0x6B0BA9A7 }; + } +} diff --git a/Legacy/BRoom/ArcPK.cs b/Legacy/BRoom/ArcPK.cs new file mode 100644 index 00000000..b9ab4874 --- /dev/null +++ b/Legacy/BRoom/ArcPK.cs @@ -0,0 +1,135 @@ +//! \file ArcPK.cs +//! \date 2019 Jan 04 +//! \brief Studio B-Room resource archive. +// +// Copyright (C) 2019 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; +using GameRes.Utility; + +namespace GameRes.Formats.BRoom +{ + [Export(typeof(ArchiveFormat))] + public class PkOpener : ArchiveFormat + { + public override string Tag { get { return "PK/B-ROOM"; } } + public override string Description { get { return "Studio B-Room resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public PkOpener () + { + Extensions = new[] { "pk", "cpc" }; + } + + public override ArcFile TryOpen (ArcView file) + { + int count = file.View.ReadInt32 (0); + if (!IsSaneCount (count)) + return null; + uint index_offset = 4; + long data_offset = count * 0x18 + index_offset; + if (data_offset >= file.MaxOffset) + return null; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + uint size = file.View.ReadUInt32 (index_offset+4); + var name = file.View.ReadString (index_offset+8, 0x10); + if (string.IsNullOrWhiteSpace (name)) + return null; + var entry = Create (name); + entry.Offset = data_offset; + entry.Size = size; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + data_offset += size; + index_offset += 0x18; + } + if (data_offset != file.MaxOffset) + return null; + return new ArcFile (file, this, dir); + } + } + + [Export(typeof(ArchiveFormat))] + public class EncryptedPkOpener : ArchiveFormat + { + public override string Tag { get { return "PK/B-ROOM/E"; } } + public override string Description { get { return "Studio B-Room encrypted resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + static readonly byte[] DefaultNameKey = { + 0xF5, 0xB2, 0xA4, 0x45, 0x59, 0x0F, 0x15, 0x22, 0x43, 0x0B, 0x99, 0x3C, 0xDD, 0xE2 + }; + + public override ArcFile TryOpen (ArcView file) + { + int count = (int)(file.View.ReadUInt32 (0) ^ 0xFF559977); + if (!IsSaneCount (count)) + return null; + uint index_offset = 4; + long data_offset = count * 0x18 + index_offset; + if (data_offset >= file.MaxOffset) + return null; + var name_key = DefaultNameKey; + var name_buffer = new byte[0x10]; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + uint offset = file.View.ReadUInt32 (index_offset); + uint size = file.View.ReadUInt32 (index_offset+4); + file.View.Read (index_offset+8, name_buffer, 0, 14); + uint checksum = 0; + int j; + for (j = 0; j < 14; ++j) + { + name_buffer[j] ^= name_key[j]; + if (0 == name_buffer[j]) + break; + checksum += (uint)name_buffer[j] << ((j & 3) << 3); + } + checksum &= 0x3FF; + var name = Encodings.cp932.GetString (name_buffer, 0, j); + if (string.IsNullOrWhiteSpace (name)) + return null; + if (name.HasAnyOfExtensions ("", "e", "er")) + name = Path.ChangeExtension (name, ".Erp"); + var entry = Create (name); + entry.Offset = offset ^ checksum ^ 0x35846; + entry.Size = size ^ checksum ^ 0x57982525; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + index_offset += 0x18; + } + return new ArcFile (file, this, dir); + } + } +} diff --git a/Legacy/BRoom/AudioEZS.cs b/Legacy/BRoom/AudioEZS.cs new file mode 100644 index 00000000..2c4b7e1a --- /dev/null +++ b/Legacy/BRoom/AudioEZS.cs @@ -0,0 +1,75 @@ +//! \file AudioEZS.cs +//! \date 2019 Jan 04 +//! \brief Studio B-Room audio format. +// +// Copyright (C) 2019 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.ComponentModel.Composition; + +namespace GameRes.Formats.BRoom +{ + [Export(typeof(AudioFormat))] + public class EzsAudio : AudioFormat + { + public override string Tag { get { return "EZS"; } } + public override string Description { get { return "Studio B-Room audio format"; } } + public override uint Signature { get { return 0; } } + public override bool CanWrite { get { return false; } } + + const byte DefaultKey = 0xDD; + + public override SoundInput TryOpen (IBinaryStream file) + { + if (!file.Name.HasExtension ("EZS")) + return null; + var header = file.ReadHeader (0x1B); + uint pcm_size = header.ToUInt32 (0); + if (pcm_size > file.Length) + return null; + byte key = header[4]; + var format = new WaveFormat { + FormatTag = header.ToUInt16 (5), + Channels = header.ToUInt16 (7), + SamplesPerSecond = header.ToUInt32 (9), + AverageBytesPerSecond = header.ToUInt32 (13), + BlockAlign = header.ToUInt16 (17), + BitsPerSample = header.ToUInt16 (19), + }; + ushort cbSize = header.ToUInt16 (21); +// key ^= DefaultKey; + key = (byte)cbSize; + cbSize ^= key; + format.BitsPerSample ^= key; + if (format.BitsPerSample != 8 && format.BitsPerSample != 16) + return null; + format.AverageBytesPerSecond ^= cbSize; + format.BlockAlign ^= (ushort)format.AverageBytesPerSecond; + format.Channels ^= format.BlockAlign; + if (format.Channels < 1 || format.Channels > 2) + return null; + format.SamplesPerSecond ^= format.Channels; + format.FormatTag ^= format.BitsPerSample; + var input = new StreamRegion (file.AsStream, 0x1B); + return new RawPcmInput (input, format); + } + } +} diff --git a/Legacy/BRoom/ImageERP.cs b/Legacy/BRoom/ImageERP.cs new file mode 100644 index 00000000..3b5c5301 --- /dev/null +++ b/Legacy/BRoom/ImageERP.cs @@ -0,0 +1,400 @@ +//! \file ImageERP.cs +//! \date 2019 Jan 04 +//! \brief Studio B-Room image format. +// +// Copyright (C) 2019 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; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using GameRes.Utility; + +// [030502][Studio B-Room] Wataridori ni Yadorigi o +// [040604][Studio B-Room] Dice-ki! ~Koi wa Un Makase~ + +namespace GameRes.Formats.BRoom +{ + internal class ErpMetaData : ImageMetaData + { + public int Id; + public int Method; + public byte KeyIndex; + } + + [Export(typeof(ImageFormat))] + public class ErpFormat : ImageFormat + { + public override string Tag { get { return "ERP"; } } + public override string Description { get { return "Studio B-Room image format"; } } + public override uint Signature { get { return 0; } } + + static readonly byte[] HeaderKey = { + 0x7D, 0x45, 0x59, 0x26, 0x8D, 0x45, 0x98, 0x26, 0x69, 0x68, 0x57, 0x52, + 0x76, 0x85, 0x12, 0x18, 0x62, 0x47, 0x7F, 0x84, + }; + + public override ImageMetaData ReadMetaData (IBinaryStream file) + { + if ((file.Signature & 0xFFFFFF00) != 0x26594500) + return null; + var header = file.ReadHeader (0x14).ToArray(); + int idx = header[0] ^ HeaderKey[0]; + if (idx >= 31) + return null; + for (int i = 4; i < 0x14; ++i) + header[i] ^= HeaderKey[i]; + int method = header.ToInt32 (16) ^ idx; + if (method < 0 || method > 12) + return null; + int key_index = GuessKeyIndex (header, idx); + if (key_index < 0) + return null; + int bpp = header.ToInt32 (4) ^ DefaultKey[0, key_index, idx]; + if (bpp != 8 && bpp != 24) + return null; + return new ErpMetaData { + Width = (header.ToUInt32 (8) ^ DefaultKey[1, key_index, idx]) * 4, + Height = (header.ToUInt32 (12) ^ DefaultKey[2, key_index, idx]) * 4, + BPP = bpp, + Id = idx, + Method = method, + KeyIndex = (byte)key_index, + }; + } + + int? LastKeyIndex = null; + + int GuessKeyIndex (byte[] header, int idx) + { + if (LastKeyIndex != null) + return LastKeyIndex.Value; + for (int key_index = 2; key_index >= 0; --key_index) + { + int bpp = header[4] ^ DefaultKey[0, key_index, idx]; + if (bpp == 8 || bpp == 24) + { + if (idx != 9) + LastKeyIndex = key_index; + return key_index; + } + } + LastKeyIndex = null; + return -1; + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + var reader = new ErpReader (file, (ErpMetaData)info); + return reader.Unpack(); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("ErpFormat.Write not implemented"); + } + + static readonly byte[,,] DefaultKey = new byte[3,3,31] { + { + { 0x86, 0xBD, 0xAD, 0x4B, 0xBC, 0x1D, 0xAA, 0x4C, 0x23, 0xAB, 0x16, 0x8C, 0xE2, 0x29, 0x7F, 0xE1, + 0xF3, 0xAC, 0x9A, 0x09, 0x30, 0xD4, 0x7D, 0x7B, 0x32, 0x70, 0x73, 0x81, 0x5B, 0x3C, 0x47 }, + { 0x2E, 0x90, 0x20, 0x6C, 0x59, 0xB2, 0x83, 0x71, 0x12, 0x13, 0x87, 0xC4, 0xF4, 0x3E, 0xD9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x30, 0x6E, 0xBB, 0xD1, 0x35, 0xF5, 0x3A, 0xA9, 0xBF, 0x8A, 0x3D, + 0x31, 0xFA, 0x19, 0x45, 0x6D, 0x7E, 0x24, 0x9B, 0xAF, 0x1B, 0x65, 0xB9, 0x02, 0xFF, 0x0C }, + { 0xD0, 0x74, 0x77, 0x1E, 0x54, 0xBE, 0xD1, 0x44, 0xE0, 0x9D, 0xFE, 0x40, 0xC8, 0x31, 0x0B, 0xB2, + 0x5E, 0x92, 0xF1, 0x0A, 0x84, 0x64, 0x35, 0xF2, 0x9B, 0x19, 0x9E, 0x83, 0x0C, 0x14, 0x7A } + } + }; + } + + internal class ErpReader + { + IBinaryStream m_input; + ErpMetaData m_info; + byte[] m_output; + + public PixelFormat Format { get; private set; } + public BitmapPalette Palette { get; private set; } + + public ErpReader (IBinaryStream input, ErpMetaData info) + { + m_input = input; + m_info = info; + int stride = info.iWidth * info.BPP / 8; + m_output = new byte[stride * info.iHeight]; + if (8 == m_info.BPP) + Format = PixelFormats.Indexed8; + else if (24 == m_info.BPP) + Format = PixelFormats.Bgr24; + else + throw new InvalidFormatException(); + } + + public ImageData Unpack () + { + m_input.Position = 0x14; + if (8 == m_info.BPP) + { + Palette = ImageFormat.ReadPalette (m_input.AsStream); + Unpack8bpp(); + } + else if (0 == m_info.Method) + UnpackV0(); + else if (m_info.Method < 7) + UnpackV1(); + else + UnpackV7(); + return ImageData.Create (m_info, Format, Palette, m_output); + } + + void Unpack8bpp () + { + int dst = 0; + while (dst < m_output.Length) + { + int px = m_input.ReadByte(); + int count = m_input.ReadByte(); + if (count < 0) + break; + count ^= 9; + if (count > 0) + { + byte v = (byte)(px ^ 13); + while (count --> 0) + m_output[dst++] = v; + } + } + } + + void UnpackV0 () + { + var count_key = new ErpKey (m_info.Id ^ 0x68, DefaultKey[6, m_info.KeyIndex, m_info.Id]); + var r_key = new ErpKey (DefaultKey[0, m_info.KeyIndex, m_info.Id], + DefaultKey[3, m_info.KeyIndex, m_info.Id]); + var g_key = new ErpKey (DefaultKey[1, m_info.KeyIndex, m_info.Id], + DefaultKey[4, m_info.KeyIndex, m_info.Id]); + var b_key = new ErpKey (DefaultKey[2, m_info.KeyIndex, m_info.Id], + DefaultKey[5, m_info.KeyIndex, m_info.Id]); + var rgb = new byte[3]; + int dst = 0; + while (dst < m_output.Length) + { + m_input.Read (rgb, 0, 3); + int count = m_input.ReadByte(); + if (count < 0) + break; + count ^= count_key.Value ^ 0xE9; + if (count > 0) + { + m_output[dst++] = (byte)(rgb[0] ^ b_key.Value); + m_output[dst++] = (byte)(rgb[1] ^ g_key.Value); + m_output[dst++] = (byte)(rgb[2] ^ r_key.Value); + count = Math.Min ((count - 1) * 3, m_output.Length - dst); + if (count > 0) + { + Binary.CopyOverlapped (m_output, dst-3, dst, count); + dst += count; + } + } + r_key.Increment(); + g_key.Increment(); + b_key.Increment(); + count_key.Increment(); + } + } + + static readonly byte[,] ChannelOrder = new byte[6,3] { + { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 } + }; + + void UnpackV1 () + { + var count_list = new List(); + var count_key = new ErpKey (m_info.Id ^ 0x68, DefaultKey[6, m_info.KeyIndex, m_info.Id]); + int count; + while ((count = m_input.ReadByte()) >= 0) + { + count ^= count_key.Value ^ 0xE9; + if (0 == count) + break; + count_list.Add (count); + count_key.Increment(); + } + int order = m_info.Method - 1; + for (int i = 0; i < 3; ++i) + { + int channel = ChannelOrder[order, i]; + var pixel_key = new ErpKey (DefaultKey[channel, m_info.KeyIndex, m_info.Id], + DefaultKey[channel+3, m_info.KeyIndex, m_info.Id]); + int dst = 2-channel; + for (int j = 0; j < count_list.Count; ++j) + { + count = count_list[j]; + int px = m_input.ReadByte(); + byte v = (byte)(px ^ pixel_key.Value); + while (count --> 0) + { + m_output[dst] = v; + dst += 3; + } + pixel_key.Increment(); + } + } + } + + void UnpackV7 () + { + int order = m_info.Method - 7; + var channels = new byte[3][]; + for (int i = 0; i < 3; ++i) + { + int channel = ChannelOrder[order, i]; + channels[channel] = UnpackChannel (channel); + } + int src = 0; + int dst = 0; + while (dst < m_output.Length) + { + m_output[dst++] = channels[2][src]; + m_output[dst++] = channels[1][src]; + m_output[dst++] = channels[0][src]; + ++src; + } + } + + byte[] UnpackChannel (int channel) + { + var output = new byte[m_output.Length / 3]; + var count_key = new ErpKey (m_info.Id ^ 0x68, DefaultKey[6, m_info.KeyIndex, m_info.Id]); + var pixel_key = new ErpKey (DefaultKey[channel, m_info.KeyIndex, m_info.Id], + DefaultKey[channel+3, m_info.KeyIndex, m_info.Id]); + int dst = 0; + for (;;) + { + int px = m_input.ReadByte(); + int count = m_input.ReadByte(); + if (count < 0) + break; + count ^= count_key.Value ^ 0xE9; + if (0 == count) + break; + count = Math.Min (count, output.Length - dst); + byte v = (byte)(px ^ pixel_key.Value); + while (count --> 0) + output[dst++] = v; + + count_key.Increment(); + pixel_key.Increment(); + } + return output; + } + + static readonly byte[,,] DefaultKey = new byte[7,3,31] { + { + { 0x5C, 0xEC, 0xC5, 0xCB, 0x76, 0xEF, 0x08, 0x66, 0xBE, 0x0E, 0x05, 0x9E, 0x1F, 0x8D, 0x11, 0xCD, + 0xE5, 0x5F, 0x1A, 0x56, 0xD6, 0x01, 0xA9, 0xA4, 0x44, 0x03, 0x77, 0xB8, 0x0C, 0xF7, 0xF0 }, + { 0x85, 0x09, 0xE1, 0x8D, 0x1F, 0x56, 0xD2, 0x4B, 0xCB, 0xC3, 0xEF, 0x10, 0x08, 0xE0, 0x36, 0x6A, + 0xAC, 0x57, 0x7C, 0x94, 0xEC, 0x2D, 0xFE, 0xDF, 0x41, 0x8C, 0x5A, 0xC7, 0x77, 0x21, 0xEA }, + { 0xC4, 0xAF, 0x4B, 0x98, 0xF3, 0xCF, 0x5B, 0x62, 0x1F, 0x23, 0xE5, 0x32, 0x41, 0x27, 0x69, 0x15, + 0x4F, 0xE2, 0x4D, 0x16, 0xB7, 0xF7, 0xF8, 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B, 0xBB, 0xD6, 0x54 }, + { 0xFE, 0xCA, 0x34, 0xF4, 0x4A, 0x03, 0x1E, 0x9F, 0x97, 0xF9, 0x15, 0x53, 0x46, 0x87, 0x99, 0x24, + 0x3A, 0x8A, 0xC7, 0x1B, 0x3F, 0x79, 0x31, 0x9A, 0x9B, 0xBE, 0x0E, 0x86, 0x5D, 0x07, 0xD4 } + } + }; + } + + struct ErpKey + { + int m_value; + byte m_increment; + + public byte Value { get { return (byte)m_value; } } + + public ErpKey (int init_value, byte increment) + { + m_value = init_value; + m_increment = increment; + } + + public void Increment () + { + m_value += m_increment; + if (m_value > 0xFF) + m_value -= 0xFF; + } + } +} diff --git a/Legacy/Legacy.csproj b/Legacy/Legacy.csproj index 9f75ea95..e90f01e4 100644 --- a/Legacy/Legacy.csproj +++ b/Legacy/Legacy.csproj @@ -80,6 +80,10 @@ + + + + @@ -233,7 +237,6 @@ -