-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncTextDecoder.cs
More file actions
246 lines (209 loc) · 6.45 KB
/
EncTextDecoder.cs
File metadata and controls
246 lines (209 loc) · 6.45 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System.Text;
namespace CFRezManager;
public enum TextPreviewStorageKind
{
Plain,
EncBase64,
LtcConverted,
CrossFireDat,
LithTechSprite,
ResourceDecoded,
FmodBank
}
public sealed record TextPreviewDocument(
string Text,
string EncodingName,
TextPreviewStorageKind StorageKind,
int SourceByteCount,
int DecodedByteCount);
internal static class EncTextDecoder
{
private const byte XorKey = 0x0B;
public static bool IsCandidate(string fileName, string extension)
{
return string.Equals(extension, "enc", StringComparison.OrdinalIgnoreCase) ||
fileName.EndsWith(".enc", StringComparison.OrdinalIgnoreCase);
}
public static bool TryDecode(ReadOnlySpan<byte> source, out byte[] decoded)
{
decoded = [];
if (source.IsEmpty || source.Length % 4 != 0)
{
return false;
}
byte[] base64Bytes = new byte[source.Length];
for (int i = 0; i < source.Length; i++)
{
base64Bytes[i] = (byte)((~source[i] & 0xFF) ^ XorKey);
if (!IsBase64Byte(base64Bytes[i]))
{
return false;
}
}
try
{
decoded = Convert.FromBase64String(Encoding.ASCII.GetString(base64Bytes));
return true;
}
catch (FormatException)
{
return false;
}
}
private static bool IsBase64Byte(byte value)
{
return value is >= (byte)'A' and <= (byte)'Z' ||
value is >= (byte)'a' and <= (byte)'z' ||
value is >= (byte)'0' and <= (byte)'9' ||
value is (byte)'+' or (byte)'/' or (byte)'=';
}
}
internal static class TextPreviewDecoder
{
private static readonly Encoding Utf8 =
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: false);
private static readonly Encoding Utf16Le =
new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: false);
private static readonly Encoding Utf16Be =
new UnicodeEncoding(bigEndian: true, byteOrderMark: true, throwOnInvalidBytes: false);
private static readonly HashSet<string> PlainTextExtensions = new(StringComparer.OrdinalIgnoreCase)
{
"cfg",
"csv",
"ini",
"json",
"log",
"lua",
"nut",
"txt",
"xml"
};
static TextPreviewDecoder()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
public static bool IsPlainTextExtension(string extension)
{
return PlainTextExtensions.Contains(extension);
}
public static bool TryDecode(byte[] data, bool preferKorean, out string text, out string encodingName)
{
ReadOnlySpan<byte> bytes = TrimTrailingNulls(data);
if (TryDecodeBom(bytes, out text, out encodingName))
{
return true;
}
foreach (EncodingCandidate candidate in GetCandidates(preferKorean))
{
if (TryDecodeWithEncoding(bytes, candidate.Encoding, out text) && LooksLikeText(text))
{
encodingName = candidate.DisplayName;
return true;
}
}
text = string.Empty;
encodingName = string.Empty;
return false;
}
private static ReadOnlySpan<byte> TrimTrailingNulls(byte[] data)
{
int length = data.Length;
while (length > 0 && data[length - 1] == 0)
{
length--;
}
return data.AsSpan(0, length);
}
private static bool TryDecodeBom(ReadOnlySpan<byte> bytes, out string text, out string encodingName)
{
if (StartsWithBytes(bytes, 0xEF, 0xBB, 0xBF))
{
return TryDecodeNamed(bytes[3..], Utf8, "UTF-8", out text, out encodingName);
}
if (StartsWithBytes(bytes, 0xFF, 0xFE))
{
return TryDecodeNamed(bytes[2..], Utf16Le, "UTF-16 LE", out text, out encodingName);
}
if (StartsWithBytes(bytes, 0xFE, 0xFF))
{
return TryDecodeNamed(bytes[2..], Utf16Be, "UTF-16 BE", out text, out encodingName);
}
text = string.Empty;
encodingName = string.Empty;
return false;
}
private static bool StartsWithBytes(ReadOnlySpan<byte> bytes, params byte[] prefix)
{
return bytes.Length >= prefix.Length && bytes[..prefix.Length].SequenceEqual(prefix);
}
private static bool TryDecodeNamed(
ReadOnlySpan<byte> bytes,
Encoding encoding,
string displayName,
out string text,
out string encodingName)
{
if (TryDecodeWithEncoding(bytes, encoding, out text) && LooksLikeText(text))
{
encodingName = displayName;
return true;
}
encodingName = string.Empty;
return false;
}
private static IEnumerable<EncodingCandidate> GetCandidates(bool preferKorean)
{
EncodingCandidate utf8 = new("UTF-8", Utf8);
EncodingCandidate cp949 = new("CP949", GetTextEncoding(949));
EncodingCandidate gb18030 = new("GB18030", GetTextEncoding("GB18030"));
if (preferKorean)
{
yield return cp949;
yield return utf8;
}
else
{
yield return utf8;
yield return cp949;
}
yield return gb18030;
}
private static Encoding GetTextEncoding(int codePage)
{
return Encoding.GetEncoding(codePage);
}
private static Encoding GetTextEncoding(string name)
{
return Encoding.GetEncoding(name);
}
private static bool TryDecodeWithEncoding(ReadOnlySpan<byte> bytes, Encoding encoding, out string text)
{
try
{
text = encoding.GetString(bytes);
return text.IndexOf('\uFFFD') < 0;
}
catch (ArgumentException)
{
text = string.Empty;
return false;
}
}
private static bool LooksLikeText(string text)
{
if (text.Length == 0)
{
return true;
}
int suspicious = 0;
foreach (char ch in text)
{
if (ch == '\uFFFD' || char.IsControl(ch) && ch is not ('\r' or '\n' or '\t'))
{
suspicious++;
}
}
return suspicious <= Math.Max(2, text.Length / 50);
}
private sealed record EncodingCandidate(string DisplayName, Encoding Encoding);
}