InflaterManaged.cs 30 KB

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