implemented Utage encrypted images.

This commit is contained in:
morkt 2018-02-07 03:45:07 +04:00
parent d520c98d84
commit 95852ac729
2 changed files with 105 additions and 0 deletions

View File

@ -91,6 +91,7 @@
<Compile Include="Abogado\ImageKG.cs" /> <Compile Include="Abogado\ImageKG.cs" />
<Compile Include="Actgs\ArcCG.cs" /> <Compile Include="Actgs\ArcCG.cs" />
<Compile Include="Actgs\ArcDAT.cs" /> <Compile Include="Actgs\ArcDAT.cs" />
<Compile Include="AdvScripter\ArcPAK.cs" />
<Compile Include="AdvSys\ArcAdvSys3.cs" /> <Compile Include="AdvSys\ArcAdvSys3.cs" />
<Compile Include="AdvSys\ImageGWD.cs" /> <Compile Include="AdvSys\ImageGWD.cs" />
<Compile Include="Ail\ArcLNK2.cs" /> <Compile Include="Ail\ArcLNK2.cs" />
@ -144,6 +145,7 @@
<Compile Include="Ikura\ImageTAN.cs" /> <Compile Include="Ikura\ImageTAN.cs" />
<Compile Include="Ikura\ImageVRS.cs" /> <Compile Include="Ikura\ImageVRS.cs" />
<Compile Include="ImageMNG.cs" /> <Compile Include="ImageMNG.cs" />
<Compile Include="Ivory\ArcSG.cs" />
<Compile Include="KiriKiri\MoreCrypt.cs" /> <Compile Include="KiriKiri\MoreCrypt.cs" />
<Compile Include="Lambda\ArcLAX.cs" /> <Compile Include="Lambda\ArcLAX.cs" />
<Compile Include="Lambda\ImageCLS.cs" /> <Compile Include="Lambda\ImageCLS.cs" />
@ -636,6 +638,7 @@
<Compile Include="Unity\BundleStream.cs" /> <Compile Include="Unity\BundleStream.cs" />
<Compile Include="Unity\OggStream.cs" /> <Compile Include="Unity\OggStream.cs" />
<Compile Include="Unity\Texture2D.cs" /> <Compile Include="Unity\Texture2D.cs" />
<Compile Include="Unity\Utage\ImageUTAGE.cs" />
<Compile Include="Unity\Vorbis.cs" /> <Compile Include="Unity\Vorbis.cs" />
<Compile Include="Valkyria\ArcDAT.cs" /> <Compile Include="Valkyria\ArcDAT.cs" />
<Compile Include="Valkyria\ArcODN.cs" /> <Compile Include="Valkyria\ArcODN.cs" />

View File

@ -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;
}
}
}