From b1e59f6b0d0f00fcee51a6b447765ca979071dda Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 3 Apr 2018 02:00:26 +0400 Subject: [PATCH] (KiriKiri): SenrenCxCrypt moved to YuzCrypt. --- ArcFormats/KiriKiri/CryptAlgorithms.cs | 28 ++++------ ArcFormats/KiriKiri/YuzCrypt.cs | 73 +++++++++++++++----------- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/ArcFormats/KiriKiri/CryptAlgorithms.cs b/ArcFormats/KiriKiri/CryptAlgorithms.cs index da9b0e05..8980e250 100644 --- a/ArcFormats/KiriKiri/CryptAlgorithms.cs +++ b/ArcFormats/KiriKiri/CryptAlgorithms.cs @@ -1035,12 +1035,20 @@ namespace GameRes.Formats.KiriKiri } [Serializable] - public class SenrenCxCrypt : CxEncryption + public class NanaCxCrypt : CxEncryption { + uint m_random_seed; + public string FileMapName { get; set; } - public SenrenCxCrypt (CxScheme scheme) : base (scheme) + public NanaCxCrypt (CxScheme scheme, uint seed) : base (scheme) { + m_random_seed = seed; + } + + internal override CxProgram NewProgram (uint seed) + { + return new CxProgramNana (seed, m_random_seed, ControlBlock); } public override string ReadName (BinaryReader header) @@ -1088,22 +1096,6 @@ namespace GameRes.Formats.KiriKiri Dictionary KnownNames = null; } - [Serializable] - public class NanaCxCrypt : SenrenCxCrypt - { - uint m_random_seed; - - public NanaCxCrypt (CxScheme scheme, uint seed) : base (scheme) - { - m_random_seed = seed; - } - - internal override CxProgram NewProgram (uint seed) - { - return new CxProgramNana (seed, m_random_seed, ControlBlock); - } - } - [Serializable] public class KissCrypt : CzCrypt { diff --git a/ArcFormats/KiriKiri/YuzCrypt.cs b/ArcFormats/KiriKiri/YuzCrypt.cs index d7d9321f..97bd4df1 100644 --- a/ArcFormats/KiriKiri/YuzCrypt.cs +++ b/ArcFormats/KiriKiri/YuzCrypt.cs @@ -32,7 +32,46 @@ using GameRes.Utility; namespace GameRes.Formats.KiriKiri { [Serializable] - public class RiddleCxCrypt : CxEncryption + public class SenrenCxCrypt : CxEncryption + { + public SenrenCxCrypt (CxScheme scheme) : base (scheme) + { + } + + internal virtual void ReadYuzNames (byte[] yuz, FilenameMap filename_map) + { + using (var ystream = new MemoryStream (yuz)) + using (var zstream = ZLibCompressor.DeCompress (ystream)) + using (var input = new BinaryReader (zstream, Encoding.Unicode)) + { + long dir_offset = 0; + while (-1 != input.PeekChar()) + { + uint entry_signature = input.ReadUInt32(); + long entry_size = input.ReadInt64(); + if (entry_size < 0) + return; + dir_offset += 12 + entry_size; + uint hash = input.ReadUInt32(); + int name_size = input.ReadInt16(); + if (name_size > 0) + { + entry_size -= 6; + if (name_size * 2 <= entry_size) + { + var filename = new string (input.ReadChars (name_size)); + filename_map.Add (hash, filename); + } + } + input.BaseStream.Position = dir_offset; + } + filename_map.AddShortcut ("$", "startup.tjs"); + } + } + } + + [Serializable] + public class RiddleCxCrypt : SenrenCxCrypt { uint m_random_seed; @@ -66,7 +105,7 @@ namespace GameRes.Formats.KiriKiri { ulong key = GetKeyFromHash (entry.Hash); key >>= (int)offset << 3; - int first_chunk = Math.Min (count, 8); + int first_chunk = Math.Min (count, 8 - (int)offset); for (int i = 0; i < first_chunk; ++i) { buffer[pos+i] ^= (byte)key; @@ -84,39 +123,13 @@ namespace GameRes.Formats.KiriKiri return (ulong)hi << 32 | lo; } - internal void ReadYuzNames (byte[] yuz, FilenameMap filename_map) + internal override void ReadYuzNames (byte[] yuz, FilenameMap filename_map) { if (null == YuzKey) throw new InvalidEncryptionScheme(); var decryptor = new YuzDecryptor (ControlBlock, YuzKey, YuzKey[4], YuzKey[5]); decryptor.Decrypt (yuz, Math.Min (yuz.Length, 0x100)); - using (var ystream = new MemoryStream (yuz)) - using (var zstream = ZLibCompressor.DeCompress (ystream)) - using (var input = new BinaryReader (zstream, Encoding.Unicode)) - { - long dir_offset = 0; - while (-1 != input.PeekChar()) - { - uint entry_signature = input.ReadUInt32(); - long entry_size = input.ReadInt64(); - if (entry_size < 0) - return; - dir_offset += 12 + entry_size; - uint hash = input.ReadUInt32(); - int name_size = input.ReadInt16(); - if (name_size > 0) - { - entry_size -= 6; - if (name_size * 2 <= entry_size) - { - var filename = new string (input.ReadChars (name_size)); - filename_map.Add (hash, filename); - } - } - input.BaseStream.Position = dir_offset; - } - filename_map.AddShortcut ("$", "startup.tjs"); - } + base.ReadYuzNames (yuz, filename_map); } }