forked from crskycode/GARbro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageEENC.cs
150 lines (138 loc) · 5.19 KB
/
ImageEENC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! \file ImageEENC.cs
//! \date Sat Jun 18 06:26:23 2016
//! \brief SepterApp encrypted images.
//
// Copyright (C) 2016 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.ComponentModel.Composition;
using System.IO;
using GameRes.Compression;
using GameRes.Utility;
namespace GameRes.Formats.Bruns
{
internal class EencMetaData : ImageMetaData
{
public uint Key;
public ImageMetaData Info;
public ImageFormat Format;
public bool Compressed;
}
[Export(typeof(ImageFormat))]
public class EencFormat : ImageFormat
{
public override string Tag { get { return "EENC"; } }
public override string Description { get { return "Bruns system encrypted image"; } }
public override uint Signature { get { return 0x434E4545; } } // 'EENC'
static readonly uint EencKey = 0xDEADBEEF;
public EencFormat ()
{
Extensions = new string[] { "brs", "png", "bmp" };
Signatures = new uint[] { 0x434E4545, 0x5A4E4545 }; // 'EENC', 'EENZ'
}
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = stream.ReadHeader (8);
bool compressed = 'Z' == header[3];
uint key = header.ToUInt32 (4) ^ EencKey;
Stream input = new StreamRegion (stream.AsStream, 8, true);
try
{
input = new EencStream (input, key);
if (compressed)
{
input = new ZLibStream (input, CompressionMode.Decompress);
input = new SeekableStream (input);
}
using (var bin = new BinaryStream (input, stream.Name, true))
{
var format = FindFormat (bin);
if (null == format)
return null;
return new EencMetaData
{
Width = format.Item2.Width,
Height = format.Item2.Height,
BPP = format.Item2.BPP,
Key = key,
Info = format.Item2,
Format = format.Item1,
Compressed = compressed,
};
}
}
finally
{
input.Dispose();
}
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (EencMetaData)info;
meta.Info.FileName = info.FileName;
Stream input = new StreamRegion (stream.AsStream, 8, true);
try
{
input = new EencStream (input, meta.Key);
if (meta.Compressed)
input = new ZLibStream (input, CompressionMode.Decompress);
using (var bin = new BinaryStream (input, stream.Name, true))
return meta.Format.Read (bin, meta.Info);
}
finally
{
input.Dispose();
}
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("EencFormat.Write not implemented");
}
}
internal class EencStream : InputProxyStream
{
uint m_key;
public EencStream (Stream main, uint key, bool leave_open = false)
: base (main, leave_open)
{
m_key = key;
}
public override int Read (byte[] buffer, int offset, int count)
{
int pos = (int)Position & 3;
int read = BaseStream.Read (buffer, offset, count);
for (int i = 0; i < read; ++i)
{
buffer[offset+i] ^= (byte)(m_key >> (pos << 3));
pos = (pos + 1) & 3;
}
return read;
}
public override int ReadByte ()
{
int pos = (int)Position & 3;
int b = BaseStream.ReadByte();
if (-1 != b)
b ^= (byte)(m_key >> (pos << 3));
return b;
}
}
}