Skip to content
0.2.0-dev Download ZIP
326 lines ·12.0 KB Raw
1using System.Buffers.Binary;
2
3namespace EvilEngine.Adapters.Oodle;
4
5internal static class EntropyArray
6{
7 public const int MaxSize = 1 << 18;
8 private const int MaxDepth = 32;
9
10 public static int Decode(ReadOnlySpan<byte> source, Span<byte> place, out int size, int depth = 0)
11 {
12 var header = ReadHeader(source, out var form, out var stored, out size);
13 if (stored > source.Length - header)
14 throw OodleException.Corrupt("an entropy array runs past the bytes it's in");
15 if (size > place.Length)
16 throw OodleException.Corrupt("an entropy array decodes to more bytes than its place allows");
17 if (depth > MaxDepth)
18 throw OodleException.Corrupt($"entropy arrays nest more than {MaxDepth} deep");
19 var body = source.Slice(header, stored);
20 var output = place[..size];
21 switch (form)
22 {
23 case 0:
24 body.CopyTo(output);
25 break;
26 case 1:
27 Tans.Decode(body, output);
28 break;
29 case 2:
30 Huffman.Decode(body, output, 1);
31 break;
32 case 3:
33 Rle(body, output, depth);
34 break;
35 case 4:
36 Huffman.Decode(body, output, 2);
37 break;
38 default:
39 Recursive(body, output, depth);
40 break;
41 }
42
43 return header + stored;
44 }
45
46 public static byte[] DecodeNew(ReadOnlySpan<byte> source, int limit, out int used, int depth = 0)
47 {
48 ReadHeader(source, out _, out _, out var size);
49 if (size > limit)
50 throw OodleException.Corrupt("an entropy array decodes to more bytes than its place allows");
51 var output = new byte[size];
52 used = Decode(source, output, out _, depth);
53 return output;
54 }
55
56 private static int ReadHeader(ReadOnlySpan<byte> source, out int form, out int stored, out int size)
57 {
58 if (source.Length < 2)
59 throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in");
60 var first = source[0];
61 form = (first >> 4) & 7;
62 if (form == 0)
63 {
64 if (first >= 0x80)
65 {
66 stored = size = ((first << 8) | source[1]) & 0xFFF;
67 return 2;
68 }
69
70 if (source.Length < 3)
71 throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in");
72 var value = (first << 16) | (source[1] << 8) | source[2];
73 if (value >= MaxSize)
74 throw OodleException.Corrupt("a raw array's long size is 2^18 or more");
75 stored = size = value;
76 return 3;
77 }
78
79 if (form >= 6)
80 throw OodleException.Corrupt($"an entropy array has form {form}");
81 if (first >= 0x80)
82 {
83 if (source.Length < 3)
84 throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in");
85 var value = (first << 16) | (source[1] << 8) | source[2];
86 stored = value & 0x3FF;
87 size = stored + ((value >> 10) & 0x3FF) + 1;
88 return 3;
89 }
90
91 if (source.Length < 5)
92 throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in");
93 var packed = BinaryPrimitives.ReadUInt32BigEndian(source[1..]);
94 stored = (int)(packed & 0x3FFFF);
95 size = (((first & 0xF) << 14) | (int)(packed >> 18)) + 1;
96 if (stored >= size)
97 throw OodleException.Corrupt("an entropy array's long header stores as many bytes as it decodes to");
98 return 5;
99 }
100
101 private static void Rle(ReadOnlySpan<byte> body, Span<byte> output, int depth)
102 {
103 if (body.Length == 1)
104 {
105 output.Fill(body[0]);
106 return;
107 }
108
109 ReadOnlySpan<byte> commands;
110 if (body[0] == 0)
111 {
112 commands = body[1..];
113 }
114 else
115 {
116 var prefix = DecodeNew(body, MaxSize, out var used, depth + 1);
117 var joined = new byte[prefix.Length + body.Length - used];
118 prefix.CopyTo(joined, 0);
119 body[used..].CopyTo(joined.AsSpan(prefix.Length));
120 commands = joined;
121 }
122
123 var front = 0;
124 var back = commands.Length;
125 var made = 0;
126 byte run = 0;
127 while (front < back)
128 {
129 var last = commands[back - 1];
130 int literals;
131 int runs;
132 if (last == 0 || last >= 0x30)
133 {
134 back--;
135 literals = 15 - (last & 15);
136 runs = last >> 4;
137 }
138 else if (last == 1)
139 {
140 back--;
141 if (front >= back)
142 throw OodleException.Corrupt("an RLE command buffer's run-byte command has no literal byte left");
143 run = commands[front++];
144 continue;
145 }
146 else
147 {
148 if (back - front < 2)
149 throw OodleException.Corrupt("an RLE command buffer's two-byte command meets its literals");
150 var word = commands[back - 2] | (last << 8);
151 back -= 2;
152 if (last >= 0x10)
153 {
154 var value = word - 4096;
155 literals = value & 63;
156 runs = value >> 6;
157 }
158 else if (last >= 9)
159 {
160 literals = 0;
161 runs = (word - 0x8FF) * 128;
162 }
163 else
164 {
165 literals = (word - 511) * 64;
166 runs = 0;
167 }
168 }
169
170 if (literals > back - front)
171 throw OodleException.Corrupt("an RLE command copies more literal bytes than are left");
172 if (literals + runs > output.Length - made)
173 throw OodleException.Corrupt("an RLE command writes past its output");
174 commands.Slice(front, literals).CopyTo(output[made..]);
175 front += literals;
176 made += literals;
177 output.Slice(made, runs).Fill(run);
178 made += runs;
179 }
180
181 if (front != back || made != output.Length)
182 throw OodleException.Corrupt("an RLE command buffer doesn't meet exactly or fill its output");
183 }
184
185 private static void Recursive(ReadOnlySpan<byte> body, Span<byte> output, int depth)
186 {
187 if (body.Length < 6)
188 throw OodleException.Corrupt("a recursive array is shorter than 6 bytes");
189 var first = body[0];
190 if ((first & 0x80) != 0)
191 {
192 MultiArray(body, output, depth);
193 return;
194 }
195
196 var count = first & 0x7F;
197 if (count < 2)
198 throw OodleException.Corrupt("a recursive array joins fewer than 2 arrays");
199 var at = 1;
200 var made = 0;
201 for (var i = 0; i < count; i++)
202 {
203 at += Decode(body[at..], output[made..], out var size, depth + 1);
204 made += size;
205 }
206
207 if (at != body.Length || made != output.Length)
208 throw OodleException.Corrupt(
209 "a recursive array's arrays don't use up its bytes or fill its output exactly");
210 }
211
212 private static void MultiArray(ReadOnlySpan<byte> body, Span<byte> output, int depth)
213 {
214 var sourceCount = body[0] & 0x3F;
215 var at = 1;
216 if (sourceCount == 0)
217 {
218 at += Decode(body[at..], output, out var only, depth + 1);
219 if (at != body.Length || only != output.Length)
220 throw OodleException.Corrupt("a multi-array with no sources doesn't fill its bytes and output exactly");
221 return;
222 }
223
224 var sources = new byte[output.Length];
225 var starts = new int[sourceCount + 1];
226 for (var i = 0; i < sourceCount; i++)
227 {
228 at += Decode(body[at..], sources.AsSpan(starts[i]), out var size, depth + 1);
229 starts[i + 1] = starts[i] + size;
230 }
231
232 var total = starts[sourceCount];
233
234 if (body.Length - at < 3)
235 throw OodleException.Corrupt("a multi-array has fewer than 3 bytes left for its length header");
236 var header = BinaryPrimitives.ReadUInt16LittleEndian(body[at..]);
237 at += 2;
238 var packed = (header & 0x8000) != 0;
239 var lengthBytes = header & 0x3FFF;
240
241 var indexes = DecodeNew(body[at..], total, out var used, depth + 1);
242 at += used;
243 int lengthCount;
244 byte[] widths;
245 if (packed)
246 {
247 lengthCount = indexes.Length;
248 widths = new byte[lengthCount];
249 for (var i = 0; i < lengthCount; i++)
250 {
251 widths[i] = (byte)(indexes[i] >> 4);
252 indexes[i] &= 15;
253 }
254 }
255 else
256 {
257 lengthCount = indexes.Length - 1;
258 if (lengthCount < 1)
259 throw OodleException.Corrupt("an unpacked multi-array has fewer than 2 indexes");
260 widths = DecodeNew(body[at..], lengthCount, out used, depth + 1);
261 at += used;
262 if (widths.Length != lengthCount)
263 throw OodleException.Corrupt(
264 "an unpacked multi-array's width array isn't one shorter than its indexes");
265 foreach (var width in widths)
266 if (width > 16)
267 throw OodleException.Corrupt("a multi-array length is wider than 16 bits");
268 }
269
270 if (body.Length - at != lengthBytes)
271 throw OodleException.Corrupt("a multi-array's length bits don't end its recursive array");
272 var bits = body[at..];
273 var forward = new BitReader(bits, false, false);
274 var backward = new BitReader(bits, true, false);
275 var lengths = new int[lengthCount];
276 for (var i = 0; i < lengthCount; i++)
277 {
278 ref var reader = ref (i & 1) == 0 ? ref forward : ref backward;
279 lengths[i] = (1 << widths[i]) + (int)reader.Read(widths[i]);
280 }
281
282 if (forward.BytesUsed + backward.BytesUsed != lengthBytes)
283 throw OodleException.Corrupt("a multi-array's two length streams don't meet exactly");
284
285 var cursors = new int[sourceCount];
286 var made = 0;
287 var next = 0;
288 for (var i = 0; i < indexes.Length; i++)
289 {
290 var index = indexes[i];
291 if (index == 0)
292 {
293 if (i != indexes.Length - 1)
294 throw OodleException.Corrupt("a multi-array ends before its last index");
295 if (packed)
296 next++;
297 break;
298 }
299
300 if (index > sourceCount)
301 throw OodleException.Corrupt("a multi-array index names a source it doesn't have");
302 if (next >= lengthCount)
303 throw OodleException.Corrupt("a multi-array runs out of lengths");
304 var length = lengths[next++];
305 var source = index - 1;
306 var available = starts[source + 1] - starts[source] - cursors[source];
307 if (length > available)
308 throw OodleException.Corrupt("a multi-array takes more bytes from a source than it holds");
309 if (length > output.Length - made)
310 throw OodleException.Corrupt("a multi-array writes past its output");
311 sources.AsSpan(starts[source] + cursors[source], length).CopyTo(output[made..]);
312 cursors[source] += length;
313 made += length;
314 }
315
316 if (indexes.Length == 0 || indexes[^1] != 0)
317 throw OodleException.Corrupt("a multi-array's last index isn't 0");
318 if (next != lengthCount)
319 throw OodleException.Corrupt("a multi-array leaves lengths unused");
320 for (var source = 0; source < sourceCount; source++)
321 if (cursors[source] != starts[source + 1] - starts[source])
322 throw OodleException.Corrupt("a multi-array leaves bytes of a source unused");
323 if (made != output.Length)
324 throw OodleException.Corrupt("a multi-array doesn't fill its output");
325 }
326}