| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.IO.Compression |
| | | 7 | | { |
| | | 8 | | internal sealed class InflaterManaged |
| | | 9 | | { |
| | | 10 | | // The maximum match length emitted by a Deflate64 length code. Length code 285 carries |
| | | 11 | | // 16 extra bits and a base length of 3, so the maximum length is 3 + 65535 = 65538. |
| | | 12 | | // Deflate (non-64) special-cases code 285 to length 258, so this bound is conservative |
| | | 13 | | // there as well. |
| | | 14 | | private const int MaxMatchLength = 65538; |
| | | 15 | | |
| | | 16 | | // const tables used in decoding: |
| | | 17 | | |
| | | 18 | | // Extra bits for length code 257 - 285. |
| | | 19 | | private static ReadOnlySpan<byte> ExtraLengthBits => |
| | 0 | 20 | | [ |
| | 0 | 21 | | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, |
| | 0 | 22 | | 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 16 |
| | 0 | 23 | | ]; |
| | | 24 | | |
| | | 25 | | // The base length for length code 257 - 285. |
| | | 26 | | // The formula to get the real length for a length code is lengthBase[code - 257] + (value stored in extraBits) |
| | | 27 | | private static ReadOnlySpan<byte> LengthBase => |
| | 0 | 28 | | [ |
| | 0 | 29 | | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, |
| | 0 | 30 | | 59, 67, 83, 99, 115, 131, 163, 195, 227, 3 |
| | 0 | 31 | | ]; |
| | | 32 | | |
| | | 33 | | // The base distance for distance code 0 - 31 |
| | | 34 | | // The real distance for a distance code is distanceBasePosition[code] + (value stored in extraBits) |
| | | 35 | | private static ReadOnlySpan<ushort> DistanceBasePosition => |
| | 0 | 36 | | [ |
| | 0 | 37 | | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, |
| | 0 | 38 | | 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 32769, 49153 |
| | 0 | 39 | | ]; |
| | | 40 | | |
| | | 41 | | // code lengths for code length alphabet is stored in following order |
| | 0 | 42 | | private static ReadOnlySpan<byte> CodeOrder => [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
| | | 43 | | |
| | | 44 | | private static ReadOnlySpan<byte> StaticDistanceTreeTable => |
| | 0 | 45 | | [ |
| | 0 | 46 | | 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c, 0x02, 0x12, 0x0a, 0x1a, |
| | 0 | 47 | | 0x06, 0x16, 0x0e, 0x1e, 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d, |
| | 0 | 48 | | 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f |
| | 0 | 49 | | ]; |
| | | 50 | | |
| | | 51 | | private readonly OutputWindow _output; |
| | | 52 | | private readonly InputBuffer _input; |
| | | 53 | | private HuffmanTree? _literalLengthTree; |
| | | 54 | | private HuffmanTree? _distanceTree; |
| | | 55 | | |
| | | 56 | | private InflaterState _state; |
| | | 57 | | private int _bfinal; |
| | | 58 | | private BlockType _blockType; |
| | | 59 | | |
| | | 60 | | // uncompressed block |
| | 0 | 61 | | private readonly byte[] _blockLengthBuffer = new byte[4]; |
| | | 62 | | private int _blockLength; |
| | | 63 | | |
| | | 64 | | // compressed block |
| | | 65 | | private int _length; |
| | | 66 | | private int _distanceCode; |
| | | 67 | | private int _extraBits; |
| | | 68 | | |
| | | 69 | | private int _loopCounter; |
| | | 70 | | private int _literalLengthCodeCount; |
| | | 71 | | private int _distanceCodeCount; |
| | | 72 | | private int _codeLengthCodeCount; |
| | | 73 | | private int _codeArraySize; |
| | | 74 | | private int _lengthCode; |
| | | 75 | | |
| | | 76 | | private readonly byte[] _codeList; // temporary array to store the code length for literal/Length and distance |
| | | 77 | | private readonly byte[] _codeLengthTreeCodeLength; |
| | | 78 | | private readonly bool _deflate64; |
| | | 79 | | private HuffmanTree? _codeLengthTree; |
| | | 80 | | private readonly long _uncompressedSize; |
| | | 81 | | private long _currentInflatedCount; |
| | | 82 | | |
| | 0 | 83 | | internal InflaterManaged(bool deflate64, long uncompressedSize) |
| | 0 | 84 | | { |
| | 0 | 85 | | _output = new OutputWindow(); |
| | 0 | 86 | | _input = new InputBuffer(); |
| | | 87 | | |
| | 0 | 88 | | _codeList = new byte[HuffmanTree.MaxLiteralTreeElements + HuffmanTree.MaxDistTreeElements]; |
| | 0 | 89 | | _codeLengthTreeCodeLength = new byte[HuffmanTree.NumberOfCodeLengthTreeElements]; |
| | 0 | 90 | | _deflate64 = deflate64; |
| | 0 | 91 | | _uncompressedSize = uncompressedSize; |
| | 0 | 92 | | _state = InflaterState.ReadingBFinal; // start by reading BFinal bit |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public void SetInput(Memory<byte> inputBytes) => _input.SetInput(inputBytes); |
| | | 96 | | |
| | | 97 | | public void SetInput(byte[] inputBytes, int offset, int length) => |
| | 0 | 98 | | _input.SetInput(inputBytes, offset, length); // append the bytes |
| | | 99 | | |
| | 0 | 100 | | public bool Finished() => _state == InflaterState.Done || _state == InflaterState.VerifyingFooter; |
| | | 101 | | |
| | 0 | 102 | | public int AvailableOutput => _output.AvailableBytes; |
| | | 103 | | |
| | | 104 | | public int Inflate(Span<byte> bytes) |
| | 0 | 105 | | { |
| | | 106 | | // copy bytes from output to outputbytes if we have available bytes |
| | | 107 | | // if buffer is not filled up. keep decoding until no input are available |
| | | 108 | | // if decodeBlock returns false. Throw an exception. |
| | 0 | 109 | | int count = 0; |
| | | 110 | | do |
| | 0 | 111 | | { |
| | 0 | 112 | | int copied = 0; |
| | 0 | 113 | | if (_uncompressedSize == -1) |
| | 0 | 114 | | { |
| | 0 | 115 | | copied = _output.CopyTo(bytes); |
| | 0 | 116 | | } |
| | | 117 | | else |
| | 0 | 118 | | { |
| | 0 | 119 | | if (_uncompressedSize > _currentInflatedCount) |
| | 0 | 120 | | { |
| | 0 | 121 | | bytes = bytes.Slice(0, (int)Math.Min(bytes.Length, _uncompressedSize - _currentInflatedCount)); |
| | 0 | 122 | | copied = _output.CopyTo(bytes); |
| | 0 | 123 | | _currentInflatedCount += copied; |
| | 0 | 124 | | } |
| | | 125 | | else |
| | 0 | 126 | | { |
| | 0 | 127 | | _state = InflaterState.Done; |
| | 0 | 128 | | _output.ClearBytesUsed(); |
| | 0 | 129 | | } |
| | 0 | 130 | | } |
| | 0 | 131 | | if (copied > 0) |
| | 0 | 132 | | { |
| | 0 | 133 | | bytes = bytes.Slice(copied); |
| | 0 | 134 | | count += copied; |
| | 0 | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | if (bytes.IsEmpty) |
| | 0 | 138 | | { |
| | | 139 | | // filled in the bytes buffer |
| | 0 | 140 | | break; |
| | | 141 | | } |
| | | 142 | | // Decode will return false when more input is needed |
| | 0 | 143 | | } while (!Finished() && Decode()); |
| | | 144 | | |
| | 0 | 145 | | return count; |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | public int Inflate(byte[] bytes, int offset, int length) => Inflate(bytes.AsSpan(offset, length)); |
| | | 149 | | |
| | | 150 | | //Each block of compressed data begins with 3 header bits |
| | | 151 | | // containing the following data: |
| | | 152 | | // first bit BFINAL |
| | | 153 | | // next 2 bits BTYPE |
| | | 154 | | // Note that the header bits do not necessarily begin on a byte |
| | | 155 | | // boundary, since a block does not necessarily occupy an integral |
| | | 156 | | // number of bytes. |
| | | 157 | | // BFINAL is set if and only if this is the last block of the data |
| | | 158 | | // set. |
| | | 159 | | // BTYPE specifies how the data are compressed, as follows: |
| | | 160 | | // 00 - no compression |
| | | 161 | | // 01 - compressed with fixed Huffman codes |
| | | 162 | | // 10 - compressed with dynamic Huffman codes |
| | | 163 | | // 11 - reserved (error) |
| | | 164 | | // The only difference between the two compressed cases is how the |
| | | 165 | | // Huffman codes for the literal/length and distance alphabets are |
| | | 166 | | // defined. |
| | | 167 | | // |
| | | 168 | | // This function returns true for success (end of block or output window is full,) |
| | | 169 | | // false if we are short of input |
| | | 170 | | // |
| | | 171 | | private bool Decode() |
| | 0 | 172 | | { |
| | 0 | 173 | | bool eob = false; |
| | | 174 | | bool result; |
| | | 175 | | |
| | 0 | 176 | | if (Finished()) |
| | 0 | 177 | | { |
| | 0 | 178 | | return true; |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | if (_state == InflaterState.ReadingBFinal) |
| | 0 | 182 | | { |
| | | 183 | | // reading bfinal bit |
| | | 184 | | // Need 1 bit |
| | 0 | 185 | | if (!_input.EnsureBitsAvailable(1)) |
| | 0 | 186 | | return false; |
| | | 187 | | |
| | 0 | 188 | | _bfinal = _input.GetBits(1); |
| | 0 | 189 | | _state = InflaterState.ReadingBType; |
| | 0 | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | if (_state == InflaterState.ReadingBType) |
| | 0 | 193 | | { |
| | | 194 | | // Need 2 bits |
| | 0 | 195 | | if (!_input.EnsureBitsAvailable(2)) |
| | 0 | 196 | | { |
| | 0 | 197 | | _state = InflaterState.ReadingBType; |
| | 0 | 198 | | return false; |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | _blockType = (BlockType)_input.GetBits(2); |
| | 0 | 202 | | if (_blockType == BlockType.Dynamic) |
| | 0 | 203 | | { |
| | 0 | 204 | | _state = InflaterState.ReadingNumLitCodes; |
| | 0 | 205 | | } |
| | 0 | 206 | | else if (_blockType == BlockType.Static) |
| | 0 | 207 | | { |
| | 0 | 208 | | _literalLengthTree = HuffmanTree.StaticLiteralLengthTree; |
| | 0 | 209 | | _distanceTree = HuffmanTree.StaticDistanceTree; |
| | 0 | 210 | | _state = InflaterState.DecodeTop; |
| | 0 | 211 | | } |
| | 0 | 212 | | else if (_blockType == BlockType.Uncompressed) |
| | 0 | 213 | | { |
| | 0 | 214 | | _state = InflaterState.UncompressedAligning; |
| | 0 | 215 | | } |
| | | 216 | | else |
| | 0 | 217 | | { |
| | 0 | 218 | | throw new InvalidDataException(SR.UnknownBlockType); |
| | | 219 | | } |
| | 0 | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | if (_blockType == BlockType.Dynamic) |
| | 0 | 223 | | { |
| | 0 | 224 | | if (_state < InflaterState.DecodeTop) |
| | 0 | 225 | | { |
| | | 226 | | // we are reading the header |
| | 0 | 227 | | result = DecodeDynamicBlockHeader(); |
| | 0 | 228 | | } |
| | | 229 | | else |
| | 0 | 230 | | { |
| | 0 | 231 | | result = DecodeBlock(out eob); // this can returns true when output is full |
| | 0 | 232 | | } |
| | 0 | 233 | | } |
| | 0 | 234 | | else if (_blockType == BlockType.Static) |
| | 0 | 235 | | { |
| | 0 | 236 | | result = DecodeBlock(out eob); |
| | 0 | 237 | | } |
| | 0 | 238 | | else if (_blockType == BlockType.Uncompressed) |
| | 0 | 239 | | { |
| | 0 | 240 | | result = DecodeUncompressedBlock(out eob); |
| | 0 | 241 | | } |
| | | 242 | | else |
| | 0 | 243 | | { |
| | 0 | 244 | | throw new InvalidDataException(SR.UnknownBlockType); |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | // |
| | | 248 | | // If we reached the end of the block and the block we were decoding had |
| | | 249 | | // bfinal=1 (final block) |
| | | 250 | | // |
| | 0 | 251 | | if (eob && (_bfinal != 0)) |
| | 0 | 252 | | { |
| | 0 | 253 | | _state = InflaterState.Done; |
| | 0 | 254 | | } |
| | 0 | 255 | | return result; |
| | 0 | 256 | | } |
| | | 257 | | |
| | | 258 | | |
| | | 259 | | // Format of Non-compressed blocks (BTYPE=00): |
| | | 260 | | // |
| | | 261 | | // Any bits of input up to the next byte boundary are ignored. |
| | | 262 | | // The rest of the block consists of the following information: |
| | | 263 | | // |
| | | 264 | | // 0 1 2 3 4... |
| | | 265 | | // +---+---+---+---+================================+ |
| | | 266 | | // | LEN | NLEN |... LEN bytes of literal data...| |
| | | 267 | | // +---+---+---+---+================================+ |
| | | 268 | | // |
| | | 269 | | // LEN is the number of data bytes in the block. NLEN is the |
| | | 270 | | // one's complement of LEN. |
| | | 271 | | private bool DecodeUncompressedBlock(out bool end_of_block) |
| | 0 | 272 | | { |
| | 0 | 273 | | end_of_block = false; |
| | 0 | 274 | | while (true) |
| | 0 | 275 | | { |
| | 0 | 276 | | switch (_state) |
| | | 277 | | { |
| | | 278 | | case InflaterState.UncompressedAligning: // initial state when calling this function |
| | | 279 | | // we must skip to a byte boundary |
| | 0 | 280 | | _input.SkipToByteBoundary(); |
| | 0 | 281 | | _state = InflaterState.UncompressedByte1; |
| | 0 | 282 | | goto case InflaterState.UncompressedByte1; |
| | | 283 | | |
| | | 284 | | case InflaterState.UncompressedByte1: // decoding block length |
| | | 285 | | case InflaterState.UncompressedByte2: |
| | | 286 | | case InflaterState.UncompressedByte3: |
| | | 287 | | case InflaterState.UncompressedByte4: |
| | 0 | 288 | | int bits = _input.GetBits(8); |
| | 0 | 289 | | if (bits < 0) |
| | 0 | 290 | | { |
| | 0 | 291 | | return false; |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | _blockLengthBuffer[_state - InflaterState.UncompressedByte1] = (byte)bits; |
| | 0 | 295 | | if (_state == InflaterState.UncompressedByte4) |
| | 0 | 296 | | { |
| | 0 | 297 | | _blockLength = _blockLengthBuffer[0] + ((int)_blockLengthBuffer[1]) * 256; |
| | 0 | 298 | | int blockLengthComplement = _blockLengthBuffer[2] + ((int)_blockLengthBuffer[3]) * 256; |
| | | 299 | | |
| | | 300 | | // make sure complement matches |
| | 0 | 301 | | if ((ushort)_blockLength != (ushort)(~blockLengthComplement)) |
| | 0 | 302 | | { |
| | 0 | 303 | | throw new InvalidDataException(SR.InvalidBlockLength); |
| | | 304 | | } |
| | 0 | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | _state += 1; |
| | 0 | 308 | | break; |
| | | 309 | | |
| | | 310 | | case InflaterState.DecodingUncompressed: // copying block data |
| | | 311 | | |
| | | 312 | | // Directly copy bytes from input to output. |
| | 0 | 313 | | int bytesCopied = _output.CopyFrom(_input, _blockLength); |
| | 0 | 314 | | _blockLength -= bytesCopied; |
| | | 315 | | |
| | 0 | 316 | | if (_blockLength == 0) |
| | 0 | 317 | | { |
| | | 318 | | // Done with this block, need to re-init bit buffer for next block |
| | 0 | 319 | | _state = InflaterState.ReadingBFinal; |
| | 0 | 320 | | end_of_block = true; |
| | 0 | 321 | | return true; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | // We can fail to copy all bytes for two reasons: |
| | | 325 | | // Running out of Input |
| | | 326 | | // running out of free space in output window |
| | 0 | 327 | | if (_output.FreeBytes == 0) |
| | 0 | 328 | | { |
| | 0 | 329 | | return true; |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | return false; |
| | | 333 | | |
| | | 334 | | default: |
| | 0 | 335 | | Debug.Fail("check why we are here!"); |
| | | 336 | | throw new InvalidDataException(SR.UnknownState); |
| | | 337 | | } |
| | 0 | 338 | | } |
| | 0 | 339 | | } |
| | | 340 | | |
| | | 341 | | private bool DecodeBlock(out bool end_of_block_code_seen) |
| | 0 | 342 | | { |
| | 0 | 343 | | end_of_block_code_seen = false; |
| | | 344 | | |
| | 0 | 345 | | int freeBytes = _output.FreeBytes; // it is a little bit faster than frequently accessing the property |
| | 0 | 346 | | while (freeBytes >= MaxMatchLength) |
| | 0 | 347 | | { |
| | | 348 | | // With Deflate64 a length/distance pair can produce up to 65538 bytes, so we ensure |
| | | 349 | | // at least that much space is available in the OutputWindow before decoding the next |
| | | 350 | | // symbol to avoid overwriting previous unflushed output data. |
| | | 351 | | |
| | | 352 | | int symbol; |
| | 0 | 353 | | switch (_state) |
| | | 354 | | { |
| | | 355 | | case InflaterState.DecodeTop: |
| | | 356 | | // decode an element from the literal tree |
| | | 357 | | |
| | 0 | 358 | | Debug.Assert(_literalLengthTree != null); |
| | | 359 | | // TODO: optimize this!!! |
| | 0 | 360 | | symbol = _literalLengthTree.GetNextSymbol(_input); |
| | 0 | 361 | | if (symbol < 0) |
| | 0 | 362 | | { |
| | | 363 | | // running out of input |
| | 0 | 364 | | return false; |
| | | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | if (symbol < 256) |
| | 0 | 368 | | { |
| | | 369 | | // literal |
| | 0 | 370 | | _output.Write((byte)symbol); |
| | 0 | 371 | | --freeBytes; |
| | 0 | 372 | | } |
| | 0 | 373 | | else if (symbol == 256) |
| | 0 | 374 | | { |
| | | 375 | | // end of block |
| | 0 | 376 | | end_of_block_code_seen = true; |
| | | 377 | | // Reset state |
| | 0 | 378 | | _state = InflaterState.ReadingBFinal; |
| | 0 | 379 | | return true; |
| | | 380 | | } |
| | | 381 | | else |
| | 0 | 382 | | { |
| | | 383 | | // length/distance pair |
| | 0 | 384 | | symbol -= 257; // length code started at 257 |
| | 0 | 385 | | if (symbol < 8) |
| | 0 | 386 | | { |
| | 0 | 387 | | symbol += 3; // match length = 3,4,5,6,7,8,9,10 |
| | 0 | 388 | | _extraBits = 0; |
| | 0 | 389 | | } |
| | 0 | 390 | | else if (!_deflate64 && symbol == 28) |
| | 0 | 391 | | { |
| | | 392 | | // extra bits for code 285 is 0 |
| | 0 | 393 | | symbol = 258; // code 285 means length 258 |
| | 0 | 394 | | _extraBits = 0; |
| | 0 | 395 | | } |
| | | 396 | | else |
| | 0 | 397 | | { |
| | 0 | 398 | | if ((uint)symbol >= ExtraLengthBits.Length) |
| | 0 | 399 | | { |
| | 0 | 400 | | throw new InvalidDataException(SR.GenericInvalidData); |
| | | 401 | | } |
| | 0 | 402 | | _extraBits = ExtraLengthBits[symbol]; |
| | 0 | 403 | | Debug.Assert(_extraBits != 0, "We handle other cases separately!"); |
| | 0 | 404 | | } |
| | 0 | 405 | | _length = symbol; |
| | 0 | 406 | | goto case InflaterState.HaveInitialLength; |
| | | 407 | | } |
| | 0 | 408 | | break; |
| | | 409 | | |
| | | 410 | | case InflaterState.HaveInitialLength: |
| | 0 | 411 | | if (_extraBits > 0) |
| | 0 | 412 | | { |
| | 0 | 413 | | _state = InflaterState.HaveInitialLength; |
| | 0 | 414 | | int bits = _input.GetBits(_extraBits); |
| | 0 | 415 | | if (bits < 0) |
| | 0 | 416 | | { |
| | 0 | 417 | | return false; |
| | | 418 | | } |
| | | 419 | | |
| | 0 | 420 | | if (_length < 0 || _length >= LengthBase.Length) |
| | 0 | 421 | | { |
| | 0 | 422 | | throw new InvalidDataException(SR.GenericInvalidData); |
| | | 423 | | } |
| | 0 | 424 | | _length = LengthBase[_length] + bits; |
| | 0 | 425 | | } |
| | 0 | 426 | | _state = InflaterState.HaveFullLength; |
| | 0 | 427 | | goto case InflaterState.HaveFullLength; |
| | | 428 | | |
| | | 429 | | case InflaterState.HaveFullLength: |
| | 0 | 430 | | if (_blockType == BlockType.Dynamic) |
| | 0 | 431 | | { |
| | 0 | 432 | | Debug.Assert(_distanceTree != null); |
| | 0 | 433 | | _distanceCode = _distanceTree.GetNextSymbol(_input); |
| | 0 | 434 | | } |
| | | 435 | | else |
| | 0 | 436 | | { |
| | | 437 | | // get distance code directly for static block |
| | 0 | 438 | | _distanceCode = _input.GetBits(5); |
| | 0 | 439 | | if (_distanceCode >= 0) |
| | 0 | 440 | | { |
| | 0 | 441 | | _distanceCode = StaticDistanceTreeTable[_distanceCode]; |
| | 0 | 442 | | } |
| | 0 | 443 | | } |
| | | 444 | | |
| | 0 | 445 | | if (_distanceCode < 0) |
| | 0 | 446 | | { |
| | | 447 | | // running out input |
| | 0 | 448 | | return false; |
| | | 449 | | } |
| | | 450 | | |
| | 0 | 451 | | _state = InflaterState.HaveDistCode; |
| | 0 | 452 | | goto case InflaterState.HaveDistCode; |
| | | 453 | | |
| | | 454 | | case InflaterState.HaveDistCode: |
| | | 455 | | // To avoid a table lookup we note that for distanceCode > 3, |
| | | 456 | | // extra_bits = (distanceCode-2) >> 1 |
| | | 457 | | int offset; |
| | 0 | 458 | | if (_distanceCode > 3) |
| | 0 | 459 | | { |
| | 0 | 460 | | _extraBits = (_distanceCode - 2) >> 1; |
| | 0 | 461 | | int bits = _input.GetBits(_extraBits); |
| | 0 | 462 | | if (bits < 0) |
| | 0 | 463 | | { |
| | 0 | 464 | | return false; |
| | | 465 | | } |
| | 0 | 466 | | offset = DistanceBasePosition[_distanceCode] + bits; |
| | 0 | 467 | | } |
| | | 468 | | else |
| | 0 | 469 | | { |
| | 0 | 470 | | offset = _distanceCode + 1; |
| | 0 | 471 | | } |
| | | 472 | | |
| | 0 | 473 | | _output.WriteLengthDistance(_length, offset); |
| | 0 | 474 | | freeBytes -= _length; |
| | 0 | 475 | | _state = InflaterState.DecodeTop; |
| | 0 | 476 | | break; |
| | | 477 | | |
| | | 478 | | default: |
| | 0 | 479 | | Debug.Fail("check why we are here!"); |
| | | 480 | | throw new InvalidDataException(SR.UnknownState); |
| | | 481 | | } |
| | 0 | 482 | | } |
| | | 483 | | |
| | 0 | 484 | | return true; |
| | 0 | 485 | | } |
| | | 486 | | |
| | | 487 | | |
| | | 488 | | // Format of the dynamic block header: |
| | | 489 | | // 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286) |
| | | 490 | | // 5 Bits: HDIST, # of Distance codes - 1 (1 - 32) |
| | | 491 | | // 4 Bits: HCLEN, # of Code Length codes - 4 (4 - 19) |
| | | 492 | | // |
| | | 493 | | // (HCLEN + 4) x 3 bits: code lengths for the code length |
| | | 494 | | // alphabet given just above, in the order: 16, 17, 18, |
| | | 495 | | // 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 |
| | | 496 | | // |
| | | 497 | | // These code lengths are interpreted as 3-bit integers |
| | | 498 | | // (0-7); as above, a code length of 0 means the |
| | | 499 | | // corresponding symbol (literal/length or distance code |
| | | 500 | | // length) is not used. |
| | | 501 | | // |
| | | 502 | | // HLIT + 257 code lengths for the literal/length alphabet, |
| | | 503 | | // encoded using the code length Huffman code |
| | | 504 | | // |
| | | 505 | | // HDIST + 1 code lengths for the distance alphabet, |
| | | 506 | | // encoded using the code length Huffman code |
| | | 507 | | // |
| | | 508 | | // The code length repeat codes can cross from HLIT + 257 to the |
| | | 509 | | // HDIST + 1 code lengths. In other words, all code lengths form |
| | | 510 | | // a single sequence of HLIT + HDIST + 258 values. |
| | | 511 | | private bool DecodeDynamicBlockHeader() |
| | 0 | 512 | | { |
| | 0 | 513 | | switch (_state) |
| | | 514 | | { |
| | | 515 | | case InflaterState.ReadingNumLitCodes: |
| | 0 | 516 | | _literalLengthCodeCount = _input.GetBits(5); |
| | 0 | 517 | | if (_literalLengthCodeCount < 0) |
| | 0 | 518 | | { |
| | 0 | 519 | | return false; |
| | | 520 | | } |
| | 0 | 521 | | _literalLengthCodeCount += 257; |
| | 0 | 522 | | _state = InflaterState.ReadingNumDistCodes; |
| | 0 | 523 | | goto case InflaterState.ReadingNumDistCodes; |
| | | 524 | | |
| | | 525 | | case InflaterState.ReadingNumDistCodes: |
| | 0 | 526 | | _distanceCodeCount = _input.GetBits(5); |
| | 0 | 527 | | if (_distanceCodeCount < 0) |
| | 0 | 528 | | { |
| | 0 | 529 | | return false; |
| | | 530 | | } |
| | 0 | 531 | | _distanceCodeCount += 1; |
| | 0 | 532 | | _state = InflaterState.ReadingNumCodeLengthCodes; |
| | 0 | 533 | | goto case InflaterState.ReadingNumCodeLengthCodes; |
| | | 534 | | |
| | | 535 | | case InflaterState.ReadingNumCodeLengthCodes: |
| | 0 | 536 | | _codeLengthCodeCount = _input.GetBits(4); |
| | 0 | 537 | | if (_codeLengthCodeCount < 0) |
| | 0 | 538 | | { |
| | 0 | 539 | | return false; |
| | | 540 | | } |
| | 0 | 541 | | _codeLengthCodeCount += 4; |
| | 0 | 542 | | _loopCounter = 0; |
| | 0 | 543 | | _state = InflaterState.ReadingCodeLengthCodes; |
| | 0 | 544 | | goto case InflaterState.ReadingCodeLengthCodes; |
| | | 545 | | |
| | | 546 | | case InflaterState.ReadingCodeLengthCodes: |
| | 0 | 547 | | while (_loopCounter < _codeLengthCodeCount) |
| | 0 | 548 | | { |
| | 0 | 549 | | int bits = _input.GetBits(3); |
| | 0 | 550 | | if (bits < 0) |
| | 0 | 551 | | { |
| | 0 | 552 | | return false; |
| | | 553 | | } |
| | 0 | 554 | | _codeLengthTreeCodeLength[CodeOrder[_loopCounter]] = (byte)bits; |
| | 0 | 555 | | ++_loopCounter; |
| | 0 | 556 | | } |
| | | 557 | | |
| | 0 | 558 | | for (int i = _codeLengthCodeCount; i < CodeOrder.Length; i++) |
| | 0 | 559 | | { |
| | 0 | 560 | | _codeLengthTreeCodeLength[CodeOrder[i]] = 0; |
| | 0 | 561 | | } |
| | | 562 | | |
| | | 563 | | // create huffman tree for code length |
| | 0 | 564 | | _codeLengthTree = new HuffmanTree(_codeLengthTreeCodeLength); |
| | 0 | 565 | | _codeArraySize = _literalLengthCodeCount + _distanceCodeCount; |
| | 0 | 566 | | _loopCounter = 0; // reset loop count |
| | | 567 | | |
| | 0 | 568 | | _state = InflaterState.ReadingTreeCodesBefore; |
| | 0 | 569 | | goto case InflaterState.ReadingTreeCodesBefore; |
| | | 570 | | |
| | | 571 | | case InflaterState.ReadingTreeCodesBefore: |
| | | 572 | | case InflaterState.ReadingTreeCodesAfter: |
| | 0 | 573 | | while (_loopCounter < _codeArraySize) |
| | 0 | 574 | | { |
| | 0 | 575 | | if (_state == InflaterState.ReadingTreeCodesBefore) |
| | 0 | 576 | | { |
| | 0 | 577 | | Debug.Assert(_codeLengthTree != null); |
| | 0 | 578 | | if ((_lengthCode = _codeLengthTree.GetNextSymbol(_input)) < 0) |
| | 0 | 579 | | { |
| | 0 | 580 | | return false; |
| | | 581 | | } |
| | 0 | 582 | | } |
| | | 583 | | |
| | | 584 | | // The alphabet for code lengths is as follows: |
| | | 585 | | // 0 - 15: Represent code lengths of 0 - 15 |
| | | 586 | | // 16: Copy the previous code length 3 - 6 times. |
| | | 587 | | // The next 2 bits indicate repeat length |
| | | 588 | | // (0 = 3, ... , 3 = 6) |
| | | 589 | | // Example: Codes 8, 16 (+2 bits 11), |
| | | 590 | | // 16 (+2 bits 10) will expand to |
| | | 591 | | // 12 code lengths of 8 (1 + 6 + 5) |
| | | 592 | | // 17: Repeat a code length of 0 for 3 - 10 times. |
| | | 593 | | // (3 bits of length) |
| | | 594 | | // 18: Repeat a code length of 0 for 11 - 138 times |
| | | 595 | | // (7 bits of length) |
| | 0 | 596 | | if (_lengthCode <= 15) |
| | 0 | 597 | | { |
| | 0 | 598 | | _codeList[_loopCounter++] = (byte)_lengthCode; |
| | 0 | 599 | | } |
| | | 600 | | else |
| | 0 | 601 | | { |
| | | 602 | | int repeatCount; |
| | 0 | 603 | | if (_lengthCode == 16) |
| | 0 | 604 | | { |
| | 0 | 605 | | if (!_input.EnsureBitsAvailable(2)) |
| | 0 | 606 | | { |
| | 0 | 607 | | _state = InflaterState.ReadingTreeCodesAfter; |
| | 0 | 608 | | return false; |
| | | 609 | | } |
| | | 610 | | |
| | 0 | 611 | | if (_loopCounter == 0) |
| | 0 | 612 | | { |
| | | 613 | | // can't have "prev code" on first code |
| | 0 | 614 | | throw new InvalidDataException(); |
| | | 615 | | } |
| | | 616 | | |
| | 0 | 617 | | byte previousCode = _codeList[_loopCounter - 1]; |
| | 0 | 618 | | repeatCount = _input.GetBits(2) + 3; |
| | | 619 | | |
| | 0 | 620 | | if (_loopCounter + repeatCount > _codeArraySize) |
| | 0 | 621 | | { |
| | 0 | 622 | | throw new InvalidDataException(); |
| | | 623 | | } |
| | | 624 | | |
| | 0 | 625 | | _codeList.AsSpan(_loopCounter, repeatCount).Fill(previousCode); |
| | 0 | 626 | | _loopCounter += repeatCount; |
| | 0 | 627 | | } |
| | 0 | 628 | | else if (_lengthCode == 17) |
| | 0 | 629 | | { |
| | 0 | 630 | | if (!_input.EnsureBitsAvailable(3)) |
| | 0 | 631 | | { |
| | 0 | 632 | | _state = InflaterState.ReadingTreeCodesAfter; |
| | 0 | 633 | | return false; |
| | | 634 | | } |
| | | 635 | | |
| | 0 | 636 | | repeatCount = _input.GetBits(3) + 3; |
| | | 637 | | |
| | 0 | 638 | | if (_loopCounter + repeatCount > _codeArraySize) |
| | 0 | 639 | | { |
| | 0 | 640 | | throw new InvalidDataException(); |
| | | 641 | | } |
| | | 642 | | |
| | 0 | 643 | | _codeList.AsSpan(_loopCounter, repeatCount).Clear(); |
| | 0 | 644 | | _loopCounter += repeatCount; |
| | 0 | 645 | | } |
| | | 646 | | else |
| | 0 | 647 | | { |
| | | 648 | | // code == 18 |
| | 0 | 649 | | if (!_input.EnsureBitsAvailable(7)) |
| | 0 | 650 | | { |
| | 0 | 651 | | _state = InflaterState.ReadingTreeCodesAfter; |
| | 0 | 652 | | return false; |
| | | 653 | | } |
| | | 654 | | |
| | 0 | 655 | | repeatCount = _input.GetBits(7) + 11; |
| | | 656 | | |
| | 0 | 657 | | if (_loopCounter + repeatCount > _codeArraySize) |
| | 0 | 658 | | { |
| | 0 | 659 | | throw new InvalidDataException(); |
| | | 660 | | } |
| | | 661 | | |
| | 0 | 662 | | _codeList.AsSpan(_loopCounter, repeatCount).Clear(); |
| | 0 | 663 | | _loopCounter += repeatCount; |
| | 0 | 664 | | } |
| | 0 | 665 | | } |
| | 0 | 666 | | _state = InflaterState.ReadingTreeCodesBefore; // we want to read the next code. |
| | 0 | 667 | | } |
| | 0 | 668 | | break; |
| | | 669 | | |
| | | 670 | | default: |
| | 0 | 671 | | Debug.Fail("check why we are here!"); |
| | | 672 | | throw new InvalidDataException(SR.UnknownState); |
| | | 673 | | } |
| | | 674 | | |
| | 0 | 675 | | byte[] literalTreeCodeLength = new byte[HuffmanTree.MaxLiteralTreeElements]; |
| | 0 | 676 | | byte[] distanceTreeCodeLength = new byte[HuffmanTree.MaxDistTreeElements]; |
| | | 677 | | |
| | | 678 | | // Create literal and distance tables |
| | 0 | 679 | | Array.Copy(_codeList, literalTreeCodeLength, _literalLengthCodeCount); |
| | 0 | 680 | | Array.Copy(_codeList, _literalLengthCodeCount, distanceTreeCodeLength, 0, _distanceCodeCount); |
| | | 681 | | |
| | | 682 | | // Make sure there is an end-of-block code, otherwise how could we ever end? |
| | 0 | 683 | | if (literalTreeCodeLength[HuffmanTree.EndOfBlockCode] == 0) |
| | 0 | 684 | | { |
| | 0 | 685 | | throw new InvalidDataException(); |
| | | 686 | | } |
| | | 687 | | |
| | 0 | 688 | | _literalLengthTree = new HuffmanTree(literalTreeCodeLength); |
| | 0 | 689 | | _distanceTree = new HuffmanTree(distanceTreeCodeLength); |
| | 0 | 690 | | _state = InflaterState.DecodeTop; |
| | 0 | 691 | | return true; |
| | 0 | 692 | | } |
| | | 693 | | } |
| | | 694 | | } |