DeflaterEngine.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. using System.Net.ApiService.Compression.Checksum;
  2. using System;
  3. namespace System.Net.ApiService.Compression.Zip.Compression {
  4. /// <summary>
  5. /// Strategies for deflater
  6. /// </summary>
  7. public enum DeflateStrategy
  8. {
  9. /// <summary>
  10. /// The default strategy
  11. /// </summary>
  12. Default = 0,
  13. /// <summary>
  14. /// This strategy will only allow longer string repetitions. It is
  15. /// useful for random data with a small character set.
  16. /// </summary>
  17. Filtered = 1,
  18. /// <summary>
  19. /// This strategy will not look for string repetitions at all. It
  20. /// only encodes with Huffman trees (which means, that more common
  21. /// characters get a smaller encoding.
  22. /// </summary>
  23. HuffmanOnly = 2
  24. }
  25. // DEFLATE ALGORITHM:
  26. //
  27. // The uncompressed stream is inserted into the window array. When
  28. // the window array is full the first half is thrown away and the
  29. // second half is copied to the beginning.
  30. //
  31. // The head array is a hash table. Three characters build a hash value
  32. // and they the value points to the corresponding index in window of
  33. // the last string with this hash. The prev array implements a
  34. // linked list of matches with the same hash: prev[index & WMASK] points
  35. // to the previous index with the same hash.
  36. //
  37. /// <summary>
  38. /// Low level compression engine for deflate algorithm which uses a 32K sliding window
  39. /// with secondary compression from Huffman/Shannon-Fano codes.
  40. /// </summary>
  41. public class DeflaterEngine
  42. {
  43. #region Constants
  44. private const int TooFar = 4096;
  45. #endregion Constants
  46. #region Constructors
  47. /// <summary>
  48. /// Construct instance with pending buffer
  49. /// Adler calculation will be peformed
  50. /// </summary>
  51. /// <param name="pending">
  52. /// Pending buffer to use
  53. /// </param>
  54. public DeflaterEngine(DeflaterPending pending)
  55. : this (pending, false)
  56. {
  57. }
  58. /// <summary>
  59. /// Construct instance with pending buffer
  60. /// </summary>
  61. /// <param name="pending">
  62. /// Pending buffer to use
  63. /// </param>
  64. /// <param name="noAdlerCalculation">
  65. /// If no adler calculation should be performed
  66. /// </param>
  67. public DeflaterEngine(DeflaterPending pending, bool noAdlerCalculation)
  68. {
  69. this.pending = pending;
  70. huffman = new DeflaterHuffman(pending);
  71. if (!noAdlerCalculation)
  72. adler = new Adler32();
  73. window = new byte[2 * DeflaterConstants.WSIZE];
  74. head = new short[DeflaterConstants.HASH_SIZE];
  75. prev = new short[DeflaterConstants.WSIZE];
  76. // We start at index 1, to avoid an implementation deficiency, that
  77. // we cannot build a repeat pattern at index 0.
  78. blockStart = strstart = 1;
  79. }
  80. #endregion Constructors
  81. /// <summary>
  82. /// Deflate drives actual compression of data
  83. /// </summary>
  84. /// <param name="flush">True to flush input buffers</param>
  85. /// <param name="finish">Finish deflation with the current input.</param>
  86. /// <returns>Returns true if progress has been made.</returns>
  87. public bool Deflate(bool flush, bool finish)
  88. {
  89. bool progress;
  90. do
  91. {
  92. FillWindow();
  93. bool canFlush = flush && (inputOff == inputEnd);
  94. #if DebugDeflation
  95. if (DeflaterConstants.DEBUGGING) {
  96. Console.WriteLine("window: [" + blockStart + "," + strstart + ","
  97. + lookahead + "], " + compressionFunction + "," + canFlush);
  98. }
  99. #endif
  100. switch (compressionFunction)
  101. {
  102. case DeflaterConstants.DEFLATE_STORED:
  103. progress = DeflateStored(canFlush, finish);
  104. break;
  105. case DeflaterConstants.DEFLATE_FAST:
  106. progress = DeflateFast(canFlush, finish);
  107. break;
  108. case DeflaterConstants.DEFLATE_SLOW:
  109. progress = DeflateSlow(canFlush, finish);
  110. break;
  111. default:
  112. throw new InvalidOperationException("unknown compressionFunction");
  113. }
  114. } while (pending.IsFlushed && progress); // repeat while we have no pending output and progress was made
  115. return progress;
  116. }
  117. /// <summary>
  118. /// Sets input data to be deflated. Should only be called when <code>NeedsInput()</code>
  119. /// returns true
  120. /// </summary>
  121. /// <param name="buffer">The buffer containing input data.</param>
  122. /// <param name="offset">The offset of the first byte of data.</param>
  123. /// <param name="count">The number of bytes of data to use as input.</param>
  124. public void SetInput(byte[] buffer, int offset, int count)
  125. {
  126. if (buffer == null)
  127. {
  128. throw new ArgumentNullException(nameof(buffer));
  129. }
  130. if (offset < 0)
  131. {
  132. throw new ArgumentOutOfRangeException(nameof(offset));
  133. }
  134. if (count < 0)
  135. {
  136. throw new ArgumentOutOfRangeException(nameof(count));
  137. }
  138. if (inputOff < inputEnd)
  139. {
  140. throw new InvalidOperationException("Old input was not completely processed");
  141. }
  142. int end = offset + count;
  143. /* We want to throw an ArrayIndexOutOfBoundsException early. The
  144. * check is very tricky: it also handles integer wrap around.
  145. */
  146. if ((offset > end) || (end > buffer.Length))
  147. {
  148. throw new ArgumentOutOfRangeException(nameof(count));
  149. }
  150. inputBuf = buffer;
  151. inputOff = offset;
  152. inputEnd = end;
  153. }
  154. /// <summary>
  155. /// Determines if more <see cref="SetInput">input</see> is needed.
  156. /// </summary>
  157. /// <returns>Return true if input is needed via <see cref="SetInput">SetInput</see></returns>
  158. public bool NeedsInput()
  159. {
  160. return (inputEnd == inputOff);
  161. }
  162. /// <summary>
  163. /// Set compression dictionary
  164. /// </summary>
  165. /// <param name="buffer">The buffer containing the dictionary data</param>
  166. /// <param name="offset">The offset in the buffer for the first byte of data</param>
  167. /// <param name="length">The length of the dictionary data.</param>
  168. public void SetDictionary(byte[] buffer, int offset, int length)
  169. {
  170. #if DebugDeflation
  171. if (DeflaterConstants.DEBUGGING && (strstart != 1) )
  172. {
  173. throw new InvalidOperationException("strstart not 1");
  174. }
  175. #endif
  176. adler?.Update(new ArraySegment<byte>(buffer, offset, length));
  177. if (length < DeflaterConstants.MIN_MATCH)
  178. {
  179. return;
  180. }
  181. if (length > DeflaterConstants.MAX_DIST)
  182. {
  183. offset += length - DeflaterConstants.MAX_DIST;
  184. length = DeflaterConstants.MAX_DIST;
  185. }
  186. System.Array.Copy(buffer, offset, window, strstart, length);
  187. UpdateHash();
  188. --length;
  189. while (--length > 0)
  190. {
  191. InsertString();
  192. strstart++;
  193. }
  194. strstart += 2;
  195. blockStart = strstart;
  196. }
  197. /// <summary>
  198. /// Reset internal state
  199. /// </summary>
  200. public void Reset()
  201. {
  202. huffman.Reset();
  203. adler?.Reset();
  204. blockStart = strstart = 1;
  205. lookahead = 0;
  206. totalIn = 0;
  207. prevAvailable = false;
  208. matchLen = DeflaterConstants.MIN_MATCH - 1;
  209. for (int i = 0; i < DeflaterConstants.HASH_SIZE; i++)
  210. {
  211. head[i] = 0;
  212. }
  213. for (int i = 0; i < DeflaterConstants.WSIZE; i++)
  214. {
  215. prev[i] = 0;
  216. }
  217. }
  218. /// <summary>
  219. /// Reset Adler checksum
  220. /// </summary>
  221. public void ResetAdler()
  222. {
  223. adler?.Reset();
  224. }
  225. /// <summary>
  226. /// Get current value of Adler checksum
  227. /// </summary>
  228. public int Adler
  229. {
  230. get
  231. {
  232. return (adler != null) ? unchecked((int)adler.Value) : 0;
  233. }
  234. }
  235. /// <summary>
  236. /// Total data processed
  237. /// </summary>
  238. public long TotalIn
  239. {
  240. get
  241. {
  242. return totalIn;
  243. }
  244. }
  245. /// <summary>
  246. /// Get/set the <see cref="DeflateStrategy">deflate strategy</see>
  247. /// </summary>
  248. public DeflateStrategy Strategy
  249. {
  250. get
  251. {
  252. return strategy;
  253. }
  254. set
  255. {
  256. strategy = value;
  257. }
  258. }
  259. /// <summary>
  260. /// Set the deflate level (0-9)
  261. /// </summary>
  262. /// <param name="level">The value to set the level to.</param>
  263. public void SetLevel(int level)
  264. {
  265. if ((level < 0) || (level > 9))
  266. {
  267. throw new ArgumentOutOfRangeException(nameof(level));
  268. }
  269. goodLength = DeflaterConstants.GOOD_LENGTH[level];
  270. max_lazy = DeflaterConstants.MAX_LAZY[level];
  271. niceLength = DeflaterConstants.NICE_LENGTH[level];
  272. max_chain = DeflaterConstants.MAX_CHAIN[level];
  273. if (DeflaterConstants.COMPR_FUNC[level] != compressionFunction)
  274. {
  275. #if DebugDeflation
  276. if (DeflaterConstants.DEBUGGING) {
  277. Console.WriteLine("Change from " + compressionFunction + " to "
  278. + DeflaterConstants.COMPR_FUNC[level]);
  279. }
  280. #endif
  281. switch (compressionFunction)
  282. {
  283. case DeflaterConstants.DEFLATE_STORED:
  284. if (strstart > blockStart)
  285. {
  286. huffman.FlushStoredBlock(window, blockStart,
  287. strstart - blockStart, false);
  288. blockStart = strstart;
  289. }
  290. UpdateHash();
  291. break;
  292. case DeflaterConstants.DEFLATE_FAST:
  293. if (strstart > blockStart)
  294. {
  295. huffman.FlushBlock(window, blockStart, strstart - blockStart,
  296. false);
  297. blockStart = strstart;
  298. }
  299. break;
  300. case DeflaterConstants.DEFLATE_SLOW:
  301. if (prevAvailable)
  302. {
  303. huffman.TallyLit(window[strstart - 1] & 0xff);
  304. }
  305. if (strstart > blockStart)
  306. {
  307. huffman.FlushBlock(window, blockStart, strstart - blockStart, false);
  308. blockStart = strstart;
  309. }
  310. prevAvailable = false;
  311. matchLen = DeflaterConstants.MIN_MATCH - 1;
  312. break;
  313. }
  314. compressionFunction = DeflaterConstants.COMPR_FUNC[level];
  315. }
  316. }
  317. /// <summary>
  318. /// Fill the window
  319. /// </summary>
  320. public void FillWindow()
  321. {
  322. /* If the window is almost full and there is insufficient lookahead,
  323. * move the upper half to the lower one to make room in the upper half.
  324. */
  325. if (strstart >= DeflaterConstants.WSIZE + DeflaterConstants.MAX_DIST)
  326. {
  327. SlideWindow();
  328. }
  329. /* If there is not enough lookahead, but still some input left,
  330. * read in the input
  331. */
  332. if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && inputOff < inputEnd)
  333. {
  334. int more = 2 * DeflaterConstants.WSIZE - lookahead - strstart;
  335. if (more > inputEnd - inputOff)
  336. {
  337. more = inputEnd - inputOff;
  338. }
  339. System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more);
  340. adler?.Update(new ArraySegment<byte>(inputBuf, inputOff, more));
  341. inputOff += more;
  342. totalIn += more;
  343. lookahead += more;
  344. }
  345. if (lookahead >= DeflaterConstants.MIN_MATCH)
  346. {
  347. UpdateHash();
  348. }
  349. }
  350. private void UpdateHash()
  351. {
  352. /*
  353. if (DEBUGGING) {
  354. Console.WriteLine("updateHash: "+strstart);
  355. }
  356. */
  357. ins_h = (window[strstart] << DeflaterConstants.HASH_SHIFT) ^ window[strstart + 1];
  358. }
  359. /// <summary>
  360. /// Inserts the current string in the head hash and returns the previous
  361. /// value for this hash.
  362. /// </summary>
  363. /// <returns>The previous hash value</returns>
  364. private int InsertString()
  365. {
  366. short match;
  367. int hash = ((ins_h << DeflaterConstants.HASH_SHIFT) ^ window[strstart + (DeflaterConstants.MIN_MATCH - 1)]) & DeflaterConstants.HASH_MASK;
  368. #if DebugDeflation
  369. if (DeflaterConstants.DEBUGGING)
  370. {
  371. if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^
  372. (window[strstart + 1] << HASH_SHIFT) ^
  373. (window[strstart + 2])) & HASH_MASK)) {
  374. throw new SharpZipBaseException("hash inconsistent: " + hash + "/"
  375. +window[strstart] + ","
  376. +window[strstart + 1] + ","
  377. +window[strstart + 2] + "," + HASH_SHIFT);
  378. }
  379. }
  380. #endif
  381. prev[strstart & DeflaterConstants.WMASK] = match = head[hash];
  382. head[hash] = unchecked((short)strstart);
  383. ins_h = hash;
  384. return match & 0xffff;
  385. }
  386. private void SlideWindow()
  387. {
  388. Array.Copy(window, DeflaterConstants.WSIZE, window, 0, DeflaterConstants.WSIZE);
  389. matchStart -= DeflaterConstants.WSIZE;
  390. strstart -= DeflaterConstants.WSIZE;
  391. blockStart -= DeflaterConstants.WSIZE;
  392. // Slide the hash table (could be avoided with 32 bit values
  393. // at the expense of memory usage).
  394. for (int i = 0; i < DeflaterConstants.HASH_SIZE; ++i)
  395. {
  396. int m = head[i] & 0xffff;
  397. head[i] = (short)(m >= DeflaterConstants.WSIZE ? (m - DeflaterConstants.WSIZE) : 0);
  398. }
  399. // Slide the prev table.
  400. for (int i = 0; i < DeflaterConstants.WSIZE; i++)
  401. {
  402. int m = prev[i] & 0xffff;
  403. prev[i] = (short)(m >= DeflaterConstants.WSIZE ? (m - DeflaterConstants.WSIZE) : 0);
  404. }
  405. }
  406. /// <summary>
  407. /// Find the best (longest) string in the window matching the
  408. /// string starting at strstart.
  409. ///
  410. /// Preconditions:
  411. /// <code>
  412. /// strstart + DeflaterConstants.MAX_MATCH &lt;= window.length.</code>
  413. /// </summary>
  414. /// <param name="curMatch"></param>
  415. /// <returns>True if a match greater than the minimum length is found</returns>
  416. private bool FindLongestMatch(int curMatch)
  417. {
  418. int match;
  419. int scan = strstart;
  420. // scanMax is the highest position that we can look at
  421. int scanMax = scan + Math.Min(DeflaterConstants.MAX_MATCH, lookahead) - 1;
  422. int limit = Math.Max(scan - DeflaterConstants.MAX_DIST, 0);
  423. byte[] window = this.window;
  424. short[] prev = this.prev;
  425. int chainLength = this.max_chain;
  426. int niceLength = Math.Min(this.niceLength, lookahead);
  427. matchLen = Math.Max(matchLen, DeflaterConstants.MIN_MATCH - 1);
  428. if (scan + matchLen > scanMax) return false;
  429. byte scan_end1 = window[scan + matchLen - 1];
  430. byte scan_end = window[scan + matchLen];
  431. // Do not waste too much time if we already have a good match:
  432. if (matchLen >= this.goodLength) chainLength >>= 2;
  433. do
  434. {
  435. match = curMatch;
  436. scan = strstart;
  437. if (window[match + matchLen] != scan_end
  438. || window[match + matchLen - 1] != scan_end1
  439. || window[match] != window[scan]
  440. || window[++match] != window[++scan])
  441. {
  442. continue;
  443. }
  444. // scan is set to strstart+1 and the comparison passed, so
  445. // scanMax - scan is the maximum number of bytes we can compare.
  446. // below we compare 8 bytes at a time, so first we compare
  447. // (scanMax - scan) % 8 bytes, so the remainder is a multiple of 8
  448. switch ((scanMax - scan) % 8)
  449. {
  450. case 1:
  451. if (window[++scan] == window[++match]) break;
  452. break;
  453. case 2:
  454. if (window[++scan] == window[++match]
  455. && window[++scan] == window[++match]) break;
  456. break;
  457. case 3:
  458. if (window[++scan] == window[++match]
  459. && window[++scan] == window[++match]
  460. && window[++scan] == window[++match]) break;
  461. break;
  462. case 4:
  463. if (window[++scan] == window[++match]
  464. && window[++scan] == window[++match]
  465. && window[++scan] == window[++match]
  466. && window[++scan] == window[++match]) break;
  467. break;
  468. case 5:
  469. if (window[++scan] == window[++match]
  470. && window[++scan] == window[++match]
  471. && window[++scan] == window[++match]
  472. && window[++scan] == window[++match]
  473. && window[++scan] == window[++match]) break;
  474. break;
  475. case 6:
  476. if (window[++scan] == window[++match]
  477. && window[++scan] == window[++match]
  478. && window[++scan] == window[++match]
  479. && window[++scan] == window[++match]
  480. && window[++scan] == window[++match]
  481. && window[++scan] == window[++match]) break;
  482. break;
  483. case 7:
  484. if (window[++scan] == window[++match]
  485. && window[++scan] == window[++match]
  486. && window[++scan] == window[++match]
  487. && window[++scan] == window[++match]
  488. && window[++scan] == window[++match]
  489. && window[++scan] == window[++match]
  490. && window[++scan] == window[++match]) break;
  491. break;
  492. }
  493. if (window[scan] == window[match])
  494. {
  495. /* We check for insufficient lookahead only every 8th comparison;
  496. * the 256th check will be made at strstart + 258 unless lookahead is
  497. * exhausted first.
  498. */
  499. do
  500. {
  501. if (scan == scanMax)
  502. {
  503. ++scan; // advance to first position not matched
  504. ++match;
  505. break;
  506. }
  507. }
  508. while (window[++scan] == window[++match]
  509. && window[++scan] == window[++match]
  510. && window[++scan] == window[++match]
  511. && window[++scan] == window[++match]
  512. && window[++scan] == window[++match]
  513. && window[++scan] == window[++match]
  514. && window[++scan] == window[++match]
  515. && window[++scan] == window[++match]);
  516. }
  517. if (scan - strstart > matchLen)
  518. {
  519. #if DebugDeflation
  520. if (DeflaterConstants.DEBUGGING && (ins_h == 0) )
  521. Console.Error.WriteLine("Found match: " + curMatch + "-" + (scan - strstart));
  522. #endif
  523. matchStart = curMatch;
  524. matchLen = scan - strstart;
  525. if (matchLen >= niceLength)
  526. break;
  527. scan_end1 = window[scan - 1];
  528. scan_end = window[scan];
  529. }
  530. } while ((curMatch = (prev[curMatch & DeflaterConstants.WMASK] & 0xffff)) > limit && 0 != --chainLength);
  531. return matchLen >= DeflaterConstants.MIN_MATCH;
  532. }
  533. private bool DeflateStored(bool flush, bool finish)
  534. {
  535. if (!flush && (lookahead == 0))
  536. {
  537. return false;
  538. }
  539. strstart += lookahead;
  540. lookahead = 0;
  541. int storedLength = strstart - blockStart;
  542. if ((storedLength >= DeflaterConstants.MAX_BLOCK_SIZE) || // Block is full
  543. (blockStart < DeflaterConstants.WSIZE && storedLength >= DeflaterConstants.MAX_DIST) || // Block may move out of window
  544. flush)
  545. {
  546. bool lastBlock = finish;
  547. if (storedLength > DeflaterConstants.MAX_BLOCK_SIZE)
  548. {
  549. storedLength = DeflaterConstants.MAX_BLOCK_SIZE;
  550. lastBlock = false;
  551. }
  552. #if DebugDeflation
  553. if (DeflaterConstants.DEBUGGING)
  554. {
  555. Console.WriteLine("storedBlock[" + storedLength + "," + lastBlock + "]");
  556. }
  557. #endif
  558. huffman.FlushStoredBlock(window, blockStart, storedLength, lastBlock);
  559. blockStart += storedLength;
  560. return !(lastBlock || storedLength == 0);
  561. }
  562. return true;
  563. }
  564. private bool DeflateFast(bool flush, bool finish)
  565. {
  566. if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && !flush)
  567. {
  568. return false;
  569. }
  570. while (lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush)
  571. {
  572. if (lookahead == 0)
  573. {
  574. // We are flushing everything
  575. huffman.FlushBlock(window, blockStart, strstart - blockStart, finish);
  576. blockStart = strstart;
  577. return false;
  578. }
  579. if (strstart > 2 * DeflaterConstants.WSIZE - DeflaterConstants.MIN_LOOKAHEAD)
  580. {
  581. /* slide window, as FindLongestMatch needs this.
  582. * This should only happen when flushing and the window
  583. * is almost full.
  584. */
  585. SlideWindow();
  586. }
  587. int hashHead;
  588. if (lookahead >= DeflaterConstants.MIN_MATCH &&
  589. (hashHead = InsertString()) != 0 &&
  590. strategy != DeflateStrategy.HuffmanOnly &&
  591. strstart - hashHead <= DeflaterConstants.MAX_DIST &&
  592. FindLongestMatch(hashHead))
  593. {
  594. // longestMatch sets matchStart and matchLen
  595. #if DebugDeflation
  596. if (DeflaterConstants.DEBUGGING)
  597. {
  598. for (int i = 0 ; i < matchLen; i++) {
  599. if (window[strstart + i] != window[matchStart + i]) {
  600. throw new SharpZipBaseException("Match failure");
  601. }
  602. }
  603. }
  604. #endif
  605. bool full = huffman.TallyDist(strstart - matchStart, matchLen);
  606. lookahead -= matchLen;
  607. if (matchLen <= max_lazy && lookahead >= DeflaterConstants.MIN_MATCH)
  608. {
  609. while (--matchLen > 0)
  610. {
  611. ++strstart;
  612. InsertString();
  613. }
  614. ++strstart;
  615. }
  616. else
  617. {
  618. strstart += matchLen;
  619. if (lookahead >= DeflaterConstants.MIN_MATCH - 1)
  620. {
  621. UpdateHash();
  622. }
  623. }
  624. matchLen = DeflaterConstants.MIN_MATCH - 1;
  625. if (!full)
  626. {
  627. continue;
  628. }
  629. }
  630. else
  631. {
  632. // No match found
  633. huffman.TallyLit(window[strstart] & 0xff);
  634. ++strstart;
  635. --lookahead;
  636. }
  637. if (huffman.IsFull())
  638. {
  639. bool lastBlock = finish && (lookahead == 0);
  640. huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock);
  641. blockStart = strstart;
  642. return !lastBlock;
  643. }
  644. }
  645. return true;
  646. }
  647. private bool DeflateSlow(bool flush, bool finish)
  648. {
  649. if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && !flush)
  650. {
  651. return false;
  652. }
  653. while (lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush)
  654. {
  655. if (lookahead == 0)
  656. {
  657. if (prevAvailable)
  658. {
  659. huffman.TallyLit(window[strstart - 1] & 0xff);
  660. }
  661. prevAvailable = false;
  662. // We are flushing everything
  663. #if DebugDeflation
  664. if (DeflaterConstants.DEBUGGING && !flush)
  665. {
  666. throw new SharpZipBaseException("Not flushing, but no lookahead");
  667. }
  668. #endif
  669. huffman.FlushBlock(window, blockStart, strstart - blockStart,
  670. finish);
  671. blockStart = strstart;
  672. return false;
  673. }
  674. if (strstart >= 2 * DeflaterConstants.WSIZE - DeflaterConstants.MIN_LOOKAHEAD)
  675. {
  676. /* slide window, as FindLongestMatch needs this.
  677. * This should only happen when flushing and the window
  678. * is almost full.
  679. */
  680. SlideWindow();
  681. }
  682. int prevMatch = matchStart;
  683. int prevLen = matchLen;
  684. if (lookahead >= DeflaterConstants.MIN_MATCH)
  685. {
  686. int hashHead = InsertString();
  687. if (strategy != DeflateStrategy.HuffmanOnly &&
  688. hashHead != 0 &&
  689. strstart - hashHead <= DeflaterConstants.MAX_DIST &&
  690. FindLongestMatch(hashHead))
  691. {
  692. // longestMatch sets matchStart and matchLen
  693. // Discard match if too small and too far away
  694. if (matchLen <= 5 && (strategy == DeflateStrategy.Filtered || (matchLen == DeflaterConstants.MIN_MATCH && strstart - matchStart > TooFar)))
  695. {
  696. matchLen = DeflaterConstants.MIN_MATCH - 1;
  697. }
  698. }
  699. }
  700. // previous match was better
  701. if ((prevLen >= DeflaterConstants.MIN_MATCH) && (matchLen <= prevLen))
  702. {
  703. #if DebugDeflation
  704. if (DeflaterConstants.DEBUGGING)
  705. {
  706. for (int i = 0 ; i < matchLen; i++) {
  707. if (window[strstart-1+i] != window[prevMatch + i])
  708. throw new SharpZipBaseException();
  709. }
  710. }
  711. #endif
  712. huffman.TallyDist(strstart - 1 - prevMatch, prevLen);
  713. prevLen -= 2;
  714. do
  715. {
  716. strstart++;
  717. lookahead--;
  718. if (lookahead >= DeflaterConstants.MIN_MATCH)
  719. {
  720. InsertString();
  721. }
  722. } while (--prevLen > 0);
  723. strstart++;
  724. lookahead--;
  725. prevAvailable = false;
  726. matchLen = DeflaterConstants.MIN_MATCH - 1;
  727. }
  728. else
  729. {
  730. if (prevAvailable)
  731. {
  732. huffman.TallyLit(window[strstart - 1] & 0xff);
  733. }
  734. prevAvailable = true;
  735. strstart++;
  736. lookahead--;
  737. }
  738. if (huffman.IsFull())
  739. {
  740. int len = strstart - blockStart;
  741. if (prevAvailable)
  742. {
  743. len--;
  744. }
  745. bool lastBlock = (finish && (lookahead == 0) && !prevAvailable);
  746. huffman.FlushBlock(window, blockStart, len, lastBlock);
  747. blockStart += len;
  748. return !lastBlock;
  749. }
  750. }
  751. return true;
  752. }
  753. #region Instance Fields
  754. // Hash index of string to be inserted
  755. private int ins_h;
  756. /// <summary>
  757. /// Hashtable, hashing three characters to an index for window, so
  758. /// that window[index]..window[index+2] have this hash code.
  759. /// Note that the array should really be unsigned short, so you need
  760. /// to and the values with 0xffff.
  761. /// </summary>
  762. private short[] head;
  763. /// <summary>
  764. /// <code>prev[index &amp; WMASK]</code> points to the previous index that has the
  765. /// same hash code as the string starting at index. This way
  766. /// entries with the same hash code are in a linked list.
  767. /// Note that the array should really be unsigned short, so you need
  768. /// to and the values with 0xffff.
  769. /// </summary>
  770. private short[] prev;
  771. private int matchStart;
  772. // Length of best match
  773. private int matchLen;
  774. // Set if previous match exists
  775. private bool prevAvailable;
  776. private int blockStart;
  777. /// <summary>
  778. /// Points to the current character in the window.
  779. /// </summary>
  780. private int strstart;
  781. /// <summary>
  782. /// lookahead is the number of characters starting at strstart in
  783. /// window that are valid.
  784. /// So window[strstart] until window[strstart+lookahead-1] are valid
  785. /// characters.
  786. /// </summary>
  787. private int lookahead;
  788. /// <summary>
  789. /// This array contains the part of the uncompressed stream that
  790. /// is of relevance. The current character is indexed by strstart.
  791. /// </summary>
  792. private byte[] window;
  793. private DeflateStrategy strategy;
  794. private int max_chain, max_lazy, niceLength, goodLength;
  795. /// <summary>
  796. /// The current compression function.
  797. /// </summary>
  798. private int compressionFunction;
  799. /// <summary>
  800. /// The input data for compression.
  801. /// </summary>
  802. private byte[] inputBuf;
  803. /// <summary>
  804. /// The total bytes of input read.
  805. /// </summary>
  806. private long totalIn;
  807. /// <summary>
  808. /// The offset into inputBuf, where input data starts.
  809. /// </summary>
  810. private int inputOff;
  811. /// <summary>
  812. /// The end offset of the input data.
  813. /// </summary>
  814. private int inputEnd;
  815. private DeflaterPending pending;
  816. private DeflaterHuffman huffman;
  817. /// <summary>
  818. /// The adler checksum
  819. /// </summary>
  820. private Adler32 adler;
  821. #endregion Instance Fields
  822. }
  823. }