0.2.0-dev Download ZIP
197 lines ·7.9 KB Raw
| 1 | namespace EvilEngine.Adapters.Oodle; |
| 2 | |
| 3 | internal static class LzSubChunk |
| 4 | { |
| 5 | private const int MaxEscapes = 512; |
| 6 | |
| 7 | public static void Decode(ReadOnlySpan<byte> chunk, Span<byte> output, int start, bool delta) |
| 8 | { |
| 9 | var length = output.Length - start; |
| 10 | if (chunk.Length < 13) |
| 11 | throw OodleException.Corrupt("an LZ sub-chunk is shorter than 13 bytes"); |
| 12 | var at = 0; |
| 13 | var position = start; |
| 14 | if (start == 0) |
| 15 | { |
| 16 | if (length < 8) |
| 17 | throw OodleException.Corrupt("an LZ sub-chunk at the output's start makes fewer than 8 bytes"); |
| 18 | chunk[..8].CopyTo(output); |
| 19 | at = 8; |
| 20 | position = 8; |
| 21 | } |
| 22 | |
| 23 | if ((chunk[at] & 0x80) != 0) |
| 24 | throw new OodleException("Kraken variant with extra bytes in its LZ sub-chunks", true); |
| 25 | var literals = EntropyArray.DecodeNew(chunk[at..], length, out var used); |
| 26 | at += used; |
| 27 | var commands = EntropyArray.DecodeNew(chunk[at..], length, out used); |
| 28 | at += used; |
| 29 | if (chunk.Length - at < 3) |
| 30 | throw OodleException.Corrupt("an LZ sub-chunk has fewer than 3 bytes left for its offsets"); |
| 31 | var scale = 0; |
| 32 | if ((chunk[at] & 0x80) != 0) |
| 33 | scale = chunk[at++] - 127; |
| 34 | var codes = EntropyArray.DecodeNew(chunk[at..], commands.Length, out used); |
| 35 | at += used; |
| 36 | byte[]? lows = null; |
| 37 | if (scale > 1) |
| 38 | { |
| 39 | lows = EntropyArray.DecodeNew(chunk[at..], codes.Length, out used); |
| 40 | at += used; |
| 41 | if (lows.Length != codes.Length) |
| 42 | throw OodleException.Corrupt("an LZ sub-chunk's low parts don't match its offset codes"); |
| 43 | } |
| 44 | |
| 45 | var lengthArray = EntropyArray.DecodeNew(chunk[at..], length / 4, out used); |
| 46 | at += used; |
| 47 | |
| 48 | var region = chunk[at..]; |
| 49 | var forward = new BitReader(region, false, false); |
| 50 | var backward = new BitReader(region, true, false); |
| 51 | var zeros = backward.Unary(18); |
| 52 | var escapeCount = (1 << zeros) + (int)backward.Read(zeros) - 1; |
| 53 | if (escapeCount > MaxEscapes) |
| 54 | throw OodleException.Corrupt("an LZ sub-chunk escapes more than 512 lengths"); |
| 55 | |
| 56 | var distances = new long[codes.Length]; |
| 57 | for (var i = 0; i < codes.Length; i++) |
| 58 | { |
| 59 | ref var bits = ref (i & 1) == 0 ? ref forward : ref backward; |
| 60 | int code = codes[i]; |
| 61 | if (scale == 0) |
| 62 | { |
| 63 | if (code < 0xF0) |
| 64 | { |
| 65 | var width = (code >> 4) + 4; |
| 66 | distances[i] = (((1L << width) + bits.Read(width)) << 4) + (code & 15) - 248; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | var width = (code & 15) + 4; |
| 71 | var high = bits.Read(width); |
| 72 | var low = bits.Read(12); |
| 73 | distances[i] = (((1L << width) + high) << 12) + low + 8_322_816; |
| 74 | } |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | var width = code >> 3; |
| 79 | if (width > 26) |
| 80 | throw OodleException.Corrupt("a scaled offset code is wider than 26 bits"); |
| 81 | var offset = ((8L + (code & 7)) << width) + bits.Read(width) - 8; |
| 82 | distances[i] = lows is null ? offset : offset * scale + lows[i]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | var escapes = new int[escapeCount]; |
| 87 | for (var i = 0; i < escapeCount; i++) |
| 88 | { |
| 89 | ref var bits = ref (i & 1) == 0 ? ref forward : ref backward; |
| 90 | var count = bits.Unary(12); |
| 91 | escapes[i] = (1 << (count + 6)) + (int)bits.Read(count + 6) - 64; |
| 92 | } |
| 93 | |
| 94 | if (forward.BytesUsed + backward.BytesUsed != region.Length) |
| 95 | throw OodleException.Corrupt("an LZ sub-chunk's bit region doesn't meet exactly"); |
| 96 | |
| 97 | var nextLength = 0; |
| 98 | var nextEscape = 0; |
| 99 | var nextDistance = 0; |
| 100 | var nextLiteral = 0; |
| 101 | long recent0 = 8, recent1 = 8, recent2 = 8; |
| 102 | foreach (var command in commands) |
| 103 | { |
| 104 | var literalCount = command & 3; |
| 105 | if (literalCount == 3) |
| 106 | literalCount = LongLength(lengthArray, escapes, ref nextLength, ref nextEscape); |
| 107 | var matchCode = (command >> 2) & 15; |
| 108 | var matchLength = matchCode == 15 |
| 109 | ? 14 + LongLength(lengthArray, escapes, ref nextLength, ref nextEscape) |
| 110 | : matchCode + 2; |
| 111 | |
| 112 | Literals(literals, ref nextLiteral, literalCount, output, ref position, delta, recent0); |
| 113 | |
| 114 | long distance; |
| 115 | switch (command >> 6) |
| 116 | { |
| 117 | case 0: |
| 118 | distance = recent0; |
| 119 | break; |
| 120 | case 1: |
| 121 | distance = recent1; |
| 122 | recent1 = recent0; |
| 123 | recent0 = distance; |
| 124 | break; |
| 125 | case 2: |
| 126 | distance = recent2; |
| 127 | recent2 = recent1; |
| 128 | recent1 = recent0; |
| 129 | recent0 = distance; |
| 130 | break; |
| 131 | default: |
| 132 | if (nextDistance >= distances.Length) |
| 133 | throw OodleException.Corrupt("an LZ sub-chunk's commands take more distances than it holds"); |
| 134 | distance = distances[nextDistance++]; |
| 135 | recent2 = recent1; |
| 136 | recent1 = recent0; |
| 137 | recent0 = distance; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | if (distance < 8) |
| 142 | throw OodleException.Corrupt("a match's distance is below 8, which the format doesn't define"); |
| 143 | if (distance > position) |
| 144 | throw OodleException.Corrupt("a match reaches before the output's start"); |
| 145 | if (matchLength > output.Length - position) |
| 146 | throw OodleException.Corrupt("a match runs past its sub-chunk"); |
| 147 | var from = position - (int)distance; |
| 148 | if (distance >= matchLength) |
| 149 | output.Slice(from, matchLength).CopyTo(output[position..]); |
| 150 | else |
| 151 | for (var i = 0; i < matchLength; i++) |
| 152 | output[position + i] = output[from + i]; |
| 153 | position += matchLength; |
| 154 | } |
| 155 | |
| 156 | var left = literals.Length - nextLiteral; |
| 157 | if (left != output.Length - position) |
| 158 | throw OodleException.Corrupt("an LZ sub-chunk's last literals don't fill it exactly"); |
| 159 | Literals(literals, ref nextLiteral, left, output, ref position, delta, recent0); |
| 160 | if (nextDistance != distances.Length || nextLength != lengthArray.Length || nextEscape != escapes.Length) |
| 161 | throw OodleException.Corrupt("an LZ sub-chunk leaves distances or lengths unused"); |
| 162 | } |
| 163 | |
| 164 | private static int LongLength(byte[] lengths, int[] escapes, ref int next, ref int escape) |
| 165 | { |
| 166 | if (next >= lengths.Length) |
| 167 | throw OodleException.Corrupt("an LZ sub-chunk's commands take more long lengths than it holds"); |
| 168 | var value = lengths[next++]; |
| 169 | if (value < 255) |
| 170 | return value + 3; |
| 171 | if (escape >= escapes.Length) |
| 172 | throw OodleException.Corrupt("an LZ sub-chunk's lengths take more escaped lengths than it holds"); |
| 173 | return escapes[escape++] + 258; |
| 174 | } |
| 175 | |
| 176 | private static void Literals(byte[] literals, ref int next, int count, Span<byte> output, ref int position, |
| 177 | bool delta, long recent) |
| 178 | { |
| 179 | if (count > literals.Length - next) |
| 180 | throw OodleException.Corrupt("an LZ sub-chunk's commands take more literals than it holds"); |
| 181 | if (count > output.Length - position) |
| 182 | throw OodleException.Corrupt("a literal run goes past its sub-chunk"); |
| 183 | if (!delta) |
| 184 | { |
| 185 | literals.AsSpan(next, count).CopyTo(output[position..]); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | var back = (int)recent; |
| 190 | for (var i = 0; i < count; i++) |
| 191 | output[position + i] = (byte)(literals[next + i] + output[position + i - back]); |
| 192 | } |
| 193 | |
| 194 | next += count; |
| 195 | position += count; |
| 196 | } |
| 197 | } |