using System.Buffers.Binary; namespace EvilEngine.Adapters.Oodle; internal static class EntropyArray { public const int MaxSize = 1 << 18; private const int MaxDepth = 32; public static int Decode(ReadOnlySpan source, Span place, out int size, int depth = 0) { var header = ReadHeader(source, out var form, out var stored, out size); if (stored > source.Length - header) throw OodleException.Corrupt("an entropy array runs past the bytes it's in"); if (size > place.Length) throw OodleException.Corrupt("an entropy array decodes to more bytes than its place allows"); if (depth > MaxDepth) throw OodleException.Corrupt($"entropy arrays nest more than {MaxDepth} deep"); var body = source.Slice(header, stored); var output = place[..size]; switch (form) { case 0: body.CopyTo(output); break; case 1: Tans.Decode(body, output); break; case 2: Huffman.Decode(body, output, 1); break; case 3: Rle(body, output, depth); break; case 4: Huffman.Decode(body, output, 2); break; default: Recursive(body, output, depth); break; } return header + stored; } public static byte[] DecodeNew(ReadOnlySpan source, int limit, out int used, int depth = 0) { ReadHeader(source, out _, out _, out var size); if (size > limit) throw OodleException.Corrupt("an entropy array decodes to more bytes than its place allows"); var output = new byte[size]; used = Decode(source, output, out _, depth); return output; } private static int ReadHeader(ReadOnlySpan source, out int form, out int stored, out int size) { if (source.Length < 2) throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in"); var first = source[0]; form = (first >> 4) & 7; if (form == 0) { if (first >= 0x80) { stored = size = ((first << 8) | source[1]) & 0xFFF; return 2; } if (source.Length < 3) throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in"); var value = (first << 16) | (source[1] << 8) | source[2]; if (value >= MaxSize) throw OodleException.Corrupt("a raw array's long size is 2^18 or more"); stored = size = value; return 3; } if (form >= 6) throw OodleException.Corrupt($"an entropy array has form {form}"); if (first >= 0x80) { if (source.Length < 3) throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in"); var value = (first << 16) | (source[1] << 8) | source[2]; stored = value & 0x3FF; size = stored + ((value >> 10) & 0x3FF) + 1; return 3; } if (source.Length < 5) throw OodleException.Corrupt("an entropy array's header runs past the bytes it's in"); var packed = BinaryPrimitives.ReadUInt32BigEndian(source[1..]); stored = (int)(packed & 0x3FFFF); size = (((first & 0xF) << 14) | (int)(packed >> 18)) + 1; if (stored >= size) throw OodleException.Corrupt("an entropy array's long header stores as many bytes as it decodes to"); return 5; } private static void Rle(ReadOnlySpan body, Span output, int depth) { if (body.Length == 1) { output.Fill(body[0]); return; } ReadOnlySpan commands; if (body[0] == 0) { commands = body[1..]; } else { var prefix = DecodeNew(body, MaxSize, out var used, depth + 1); var joined = new byte[prefix.Length + body.Length - used]; prefix.CopyTo(joined, 0); body[used..].CopyTo(joined.AsSpan(prefix.Length)); commands = joined; } var front = 0; var back = commands.Length; var made = 0; byte run = 0; while (front < back) { var last = commands[back - 1]; int literals; int runs; if (last == 0 || last >= 0x30) { back--; literals = 15 - (last & 15); runs = last >> 4; } else if (last == 1) { back--; if (front >= back) throw OodleException.Corrupt("an RLE command buffer's run-byte command has no literal byte left"); run = commands[front++]; continue; } else { if (back - front < 2) throw OodleException.Corrupt("an RLE command buffer's two-byte command meets its literals"); var word = commands[back - 2] | (last << 8); back -= 2; if (last >= 0x10) { var value = word - 4096; literals = value & 63; runs = value >> 6; } else if (last >= 9) { literals = 0; runs = (word - 0x8FF) * 128; } else { literals = (word - 511) * 64; runs = 0; } } if (literals > back - front) throw OodleException.Corrupt("an RLE command copies more literal bytes than are left"); if (literals + runs > output.Length - made) throw OodleException.Corrupt("an RLE command writes past its output"); commands.Slice(front, literals).CopyTo(output[made..]); front += literals; made += literals; output.Slice(made, runs).Fill(run); made += runs; } if (front != back || made != output.Length) throw OodleException.Corrupt("an RLE command buffer doesn't meet exactly or fill its output"); } private static void Recursive(ReadOnlySpan body, Span output, int depth) { if (body.Length < 6) throw OodleException.Corrupt("a recursive array is shorter than 6 bytes"); var first = body[0]; if ((first & 0x80) != 0) { MultiArray(body, output, depth); return; } var count = first & 0x7F; if (count < 2) throw OodleException.Corrupt("a recursive array joins fewer than 2 arrays"); var at = 1; var made = 0; for (var i = 0; i < count; i++) { at += Decode(body[at..], output[made..], out var size, depth + 1); made += size; } if (at != body.Length || made != output.Length) throw OodleException.Corrupt( "a recursive array's arrays don't use up its bytes or fill its output exactly"); } private static void MultiArray(ReadOnlySpan body, Span output, int depth) { var sourceCount = body[0] & 0x3F; var at = 1; if (sourceCount == 0) { at += Decode(body[at..], output, out var only, depth + 1); if (at != body.Length || only != output.Length) throw OodleException.Corrupt("a multi-array with no sources doesn't fill its bytes and output exactly"); return; } var sources = new byte[output.Length]; var starts = new int[sourceCount + 1]; for (var i = 0; i < sourceCount; i++) { at += Decode(body[at..], sources.AsSpan(starts[i]), out var size, depth + 1); starts[i + 1] = starts[i] + size; } var total = starts[sourceCount]; if (body.Length - at < 3) throw OodleException.Corrupt("a multi-array has fewer than 3 bytes left for its length header"); var header = BinaryPrimitives.ReadUInt16LittleEndian(body[at..]); at += 2; var packed = (header & 0x8000) != 0; var lengthBytes = header & 0x3FFF; var indexes = DecodeNew(body[at..], total, out var used, depth + 1); at += used; int lengthCount; byte[] widths; if (packed) { lengthCount = indexes.Length; widths = new byte[lengthCount]; for (var i = 0; i < lengthCount; i++) { widths[i] = (byte)(indexes[i] >> 4); indexes[i] &= 15; } } else { lengthCount = indexes.Length - 1; if (lengthCount < 1) throw OodleException.Corrupt("an unpacked multi-array has fewer than 2 indexes"); widths = DecodeNew(body[at..], lengthCount, out used, depth + 1); at += used; if (widths.Length != lengthCount) throw OodleException.Corrupt( "an unpacked multi-array's width array isn't one shorter than its indexes"); foreach (var width in widths) if (width > 16) throw OodleException.Corrupt("a multi-array length is wider than 16 bits"); } if (body.Length - at != lengthBytes) throw OodleException.Corrupt("a multi-array's length bits don't end its recursive array"); var bits = body[at..]; var forward = new BitReader(bits, false, false); var backward = new BitReader(bits, true, false); var lengths = new int[lengthCount]; for (var i = 0; i < lengthCount; i++) { ref var reader = ref (i & 1) == 0 ? ref forward : ref backward; lengths[i] = (1 << widths[i]) + (int)reader.Read(widths[i]); } if (forward.BytesUsed + backward.BytesUsed != lengthBytes) throw OodleException.Corrupt("a multi-array's two length streams don't meet exactly"); var cursors = new int[sourceCount]; var made = 0; var next = 0; for (var i = 0; i < indexes.Length; i++) { var index = indexes[i]; if (index == 0) { if (i != indexes.Length - 1) throw OodleException.Corrupt("a multi-array ends before its last index"); if (packed) next++; break; } if (index > sourceCount) throw OodleException.Corrupt("a multi-array index names a source it doesn't have"); if (next >= lengthCount) throw OodleException.Corrupt("a multi-array runs out of lengths"); var length = lengths[next++]; var source = index - 1; var available = starts[source + 1] - starts[source] - cursors[source]; if (length > available) throw OodleException.Corrupt("a multi-array takes more bytes from a source than it holds"); if (length > output.Length - made) throw OodleException.Corrupt("a multi-array writes past its output"); sources.AsSpan(starts[source] + cursors[source], length).CopyTo(output[made..]); cursors[source] += length; made += length; } if (indexes.Length == 0 || indexes[^1] != 0) throw OodleException.Corrupt("a multi-array's last index isn't 0"); if (next != lengthCount) throw OodleException.Corrupt("a multi-array leaves lengths unused"); for (var source = 0; source < sourceCount; source++) if (cursors[source] != starts[source + 1] - starts[source]) throw OodleException.Corrupt("a multi-array leaves bytes of a source unused"); if (made != output.Length) throw OodleException.Corrupt("a multi-array doesn't fill its output"); } }