namespace EvilEngine.Adapters.Oodle; internal static class LzSubChunk { private const int MaxEscapes = 512; public static void Decode(ReadOnlySpan chunk, Span output, int start, bool delta) { var length = output.Length - start; if (chunk.Length < 13) throw OodleException.Corrupt("an LZ sub-chunk is shorter than 13 bytes"); var at = 0; var position = start; if (start == 0) { if (length < 8) throw OodleException.Corrupt("an LZ sub-chunk at the output's start makes fewer than 8 bytes"); chunk[..8].CopyTo(output); at = 8; position = 8; } if ((chunk[at] & 0x80) != 0) throw new OodleException("Kraken variant with extra bytes in its LZ sub-chunks", true); var literals = EntropyArray.DecodeNew(chunk[at..], length, out var used); at += used; var commands = EntropyArray.DecodeNew(chunk[at..], length, out used); at += used; if (chunk.Length - at < 3) throw OodleException.Corrupt("an LZ sub-chunk has fewer than 3 bytes left for its offsets"); var scale = 0; if ((chunk[at] & 0x80) != 0) scale = chunk[at++] - 127; var codes = EntropyArray.DecodeNew(chunk[at..], commands.Length, out used); at += used; byte[]? lows = null; if (scale > 1) { lows = EntropyArray.DecodeNew(chunk[at..], codes.Length, out used); at += used; if (lows.Length != codes.Length) throw OodleException.Corrupt("an LZ sub-chunk's low parts don't match its offset codes"); } var lengthArray = EntropyArray.DecodeNew(chunk[at..], length / 4, out used); at += used; var region = chunk[at..]; var forward = new BitReader(region, false, false); var backward = new BitReader(region, true, false); var zeros = backward.Unary(18); var escapeCount = (1 << zeros) + (int)backward.Read(zeros) - 1; if (escapeCount > MaxEscapes) throw OodleException.Corrupt("an LZ sub-chunk escapes more than 512 lengths"); var distances = new long[codes.Length]; for (var i = 0; i < codes.Length; i++) { ref var bits = ref (i & 1) == 0 ? ref forward : ref backward; int code = codes[i]; if (scale == 0) { if (code < 0xF0) { var width = (code >> 4) + 4; distances[i] = (((1L << width) + bits.Read(width)) << 4) + (code & 15) - 248; } else { var width = (code & 15) + 4; var high = bits.Read(width); var low = bits.Read(12); distances[i] = (((1L << width) + high) << 12) + low + 8_322_816; } } else { var width = code >> 3; if (width > 26) throw OodleException.Corrupt("a scaled offset code is wider than 26 bits"); var offset = ((8L + (code & 7)) << width) + bits.Read(width) - 8; distances[i] = lows is null ? offset : offset * scale + lows[i]; } } var escapes = new int[escapeCount]; for (var i = 0; i < escapeCount; i++) { ref var bits = ref (i & 1) == 0 ? ref forward : ref backward; var count = bits.Unary(12); escapes[i] = (1 << (count + 6)) + (int)bits.Read(count + 6) - 64; } if (forward.BytesUsed + backward.BytesUsed != region.Length) throw OodleException.Corrupt("an LZ sub-chunk's bit region doesn't meet exactly"); var nextLength = 0; var nextEscape = 0; var nextDistance = 0; var nextLiteral = 0; long recent0 = 8, recent1 = 8, recent2 = 8; foreach (var command in commands) { var literalCount = command & 3; if (literalCount == 3) literalCount = LongLength(lengthArray, escapes, ref nextLength, ref nextEscape); var matchCode = (command >> 2) & 15; var matchLength = matchCode == 15 ? 14 + LongLength(lengthArray, escapes, ref nextLength, ref nextEscape) : matchCode + 2; Literals(literals, ref nextLiteral, literalCount, output, ref position, delta, recent0); long distance; switch (command >> 6) { case 0: distance = recent0; break; case 1: distance = recent1; recent1 = recent0; recent0 = distance; break; case 2: distance = recent2; recent2 = recent1; recent1 = recent0; recent0 = distance; break; default: if (nextDistance >= distances.Length) throw OodleException.Corrupt("an LZ sub-chunk's commands take more distances than it holds"); distance = distances[nextDistance++]; recent2 = recent1; recent1 = recent0; recent0 = distance; break; } if (distance < 8) throw OodleException.Corrupt("a match's distance is below 8, which the format doesn't define"); if (distance > position) throw OodleException.Corrupt("a match reaches before the output's start"); if (matchLength > output.Length - position) throw OodleException.Corrupt("a match runs past its sub-chunk"); var from = position - (int)distance; if (distance >= matchLength) output.Slice(from, matchLength).CopyTo(output[position..]); else for (var i = 0; i < matchLength; i++) output[position + i] = output[from + i]; position += matchLength; } var left = literals.Length - nextLiteral; if (left != output.Length - position) throw OodleException.Corrupt("an LZ sub-chunk's last literals don't fill it exactly"); Literals(literals, ref nextLiteral, left, output, ref position, delta, recent0); if (nextDistance != distances.Length || nextLength != lengthArray.Length || nextEscape != escapes.Length) throw OodleException.Corrupt("an LZ sub-chunk leaves distances or lengths unused"); } private static int LongLength(byte[] lengths, int[] escapes, ref int next, ref int escape) { if (next >= lengths.Length) throw OodleException.Corrupt("an LZ sub-chunk's commands take more long lengths than it holds"); var value = lengths[next++]; if (value < 255) return value + 3; if (escape >= escapes.Length) throw OodleException.Corrupt("an LZ sub-chunk's lengths take more escaped lengths than it holds"); return escapes[escape++] + 258; } private static void Literals(byte[] literals, ref int next, int count, Span output, ref int position, bool delta, long recent) { if (count > literals.Length - next) throw OodleException.Corrupt("an LZ sub-chunk's commands take more literals than it holds"); if (count > output.Length - position) throw OodleException.Corrupt("a literal run goes past its sub-chunk"); if (!delta) { literals.AsSpan(next, count).CopyTo(output[position..]); } else { var back = (int)recent; for (var i = 0; i < count; i++) output[position + i] = (byte)(literals[next + i] + output[position + i - back]); } next += count; position += count; } }