From 95852ac7297def2efbb55985d453acd16f133e09 Mon Sep 17 00:00:00 2001 From: morkt Date: Wed, 7 Feb 2018 03:45:07 +0400 Subject: [PATCH] implemented Utage encrypted images. --- ArcFormats/ArcFormats.csproj | 3 + ArcFormats/Unity/Utage/ImageUTAGE.cs | 102 +++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 ArcFormats/Unity/Utage/ImageUTAGE.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index ab72653e..5016f39a 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -91,6 +91,7 @@ + @@ -144,6 +145,7 @@ + @@ -636,6 +638,7 @@ + diff --git a/ArcFormats/Unity/Utage/ImageUTAGE.cs b/ArcFormats/Unity/Utage/ImageUTAGE.cs new file mode 100644 index 00000000..a8458126 --- /dev/null +++ b/ArcFormats/Unity/Utage/ImageUTAGE.cs @@ -0,0 +1,102 @@ +//! \file ImageUTAGE.cs +//! \date 2018 Feb 05 +//! \brief Utage engine encrypted image. +// +// Copyright (C) 2018 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; +using System.IO; +using System.Text; + +namespace GameRes.Formats.Unity.Utage +{ + [Export(typeof(ImageFormat))] + public class UtageFormat : ImageFormat + { + public override string Tag { get { return "UTAGE"; } } + public override string Description { get { return "Utage engine encrypted image"; } } + public override uint Signature { get { return 0x323E3EC0; } } + + public static readonly byte[] KnownKey = Encoding.UTF8.GetBytes ("InputOriginalKey"); + + public override ImageMetaData ReadMetaData (IBinaryStream file) + { + using (var input = OpenEncryptedStream (file, KnownKey)) + return Png.ReadMetaData (input); + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + using (var input = OpenEncryptedStream (file, KnownKey)) + return Png.Read (input, info); + } + + internal IBinaryStream OpenEncryptedStream (IBinaryStream file, byte[] key) + { + var input = new UtageEncryptedStream (file.AsStream, key, true); + return new BinaryStream (input, file.Name); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("UtageFormat.Write not implemented"); + } + } + + internal class UtageEncryptedStream : InputProxyStream + { + byte[] m_key; + + public UtageEncryptedStream (Stream main, byte[] key, bool leave_open = false) + : base (main, leave_open) + { + m_key = key; + } + + public override int Read (byte[] buffer, int offset, int count) + { + int key_length = m_key.Length; + int start_pos = (int)(BaseStream.Position % key_length); + int read = BaseStream.Read (buffer, offset, count); + for (int i = 0; i < read; ++i) + { + byte key = m_key[(start_pos + i) % key_length]; + if (buffer[offset+i] != 0 && buffer[offset+i] != key) + buffer[offset+i] ^= key; + } + return read; + } + + public override int ReadByte () + { + long pos = BaseStream.Position; + int b = BaseStream.ReadByte(); + if (b > 0) + { + byte key = m_key[pos % m_key.Length]; + if (b != key) + b ^= key; + } + return b; + } + } +}