DeflaterEngine.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. // DeflaterEngine.cs
  2. // Copyright (C) 2001 Mike Krueger
  3. //
  4. // This file was translated from java, it was part of the GNU Classpath
  5. // Copyright (C) 2001 Free Software Foundation, Inc.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // Linking this library statically or dynamically with other modules is
  22. // making a combined work based on this library. Thus, the terms and
  23. // conditions of the GNU General Public License cover the whole
  24. // combination.
  25. //
  26. // As a special exception, the copyright holders of this library give you
  27. // permission to link this library with independent modules to produce an
  28. // executable, regardless of the license terms of these independent
  29. // modules, and to copy and distribute the resulting executable under
  30. // terms of your choice, provided that you also meet, for each linked
  31. // independent module, the terms and conditions of the license of that
  32. // module. An independent module is a module which is not derived from
  33. // or based on this library. If you modify this library, you may extend
  34. // this exception to your version of the library, but you are not
  35. // obligated to do so. If you do not wish to do so, delete this
  36. // exception statement from your version.
  37. using System;
  38. using ICSharpCode.SharpZipLib.Checksums;
  39. namespace ICSharpCode.SharpZipLib.Zip.Compression
  40. {
  41. public enum DeflateStrategy
  42. {
  43. // The default strategy.
  44. Default = 0,
  45. // This strategy will only allow longer string repetitions. It is
  46. // useful for random data with a small character set.
  47. Filtered = 1,
  48. // This strategy will not look for string repetitions at all. It
  49. // only encodes with Huffman trees (which means, that more common
  50. // characters get a smaller encoding.
  51. HuffmanOnly = 2
  52. }
  53. public class DeflaterEngine : DeflaterConstants
  54. {
  55. static int TOO_FAR = 4096;
  56. int ins_h;
  57. // private byte[] buffer;
  58. short[] head;
  59. short[] prev;
  60. int matchStart, matchLen;
  61. bool prevAvailable;
  62. int blockStart;
  63. int strstart, lookahead;
  64. byte[] window;
  65. DeflateStrategy strategy;
  66. int max_chain, max_lazy, niceLength, goodLength;
  67. /// <summary>
  68. /// The current compression function.
  69. /// </summary>
  70. int comprFunc;
  71. /// <summary>
  72. /// The input data for compression.
  73. /// </summary>
  74. byte[] inputBuf;
  75. /// <summary>
  76. /// The total bytes of input read.
  77. /// </summary>
  78. int totalIn;
  79. /// <summary>
  80. /// The offset into inputBuf, where input data starts.
  81. /// </summary>
  82. int inputOff;
  83. /// <summary>
  84. /// The end offset of the input data.
  85. /// </summary>
  86. int inputEnd;
  87. DeflaterPending pending;
  88. DeflaterHuffman huffman;
  89. /// <summary>
  90. /// The adler checksum
  91. /// </summary>
  92. Adler32 adler;
  93. public DeflaterEngine(DeflaterPending pending)
  94. {
  95. this.pending = pending;
  96. huffman = new DeflaterHuffman(pending);
  97. adler = new Adler32();
  98. window = new byte[2 * WSIZE];
  99. head = new short[HASH_SIZE];
  100. prev = new short[WSIZE];
  101. /* We start at index 1, to avoid a implementation deficiency, that
  102. * we cannot build a repeat pattern at index 0.
  103. */
  104. blockStart = strstart = 1;
  105. }
  106. public void Reset()
  107. {
  108. huffman.Reset();
  109. adler.Reset();
  110. blockStart = strstart = 1;
  111. lookahead = 0;
  112. totalIn = 0;
  113. prevAvailable = false;
  114. matchLen = MIN_MATCH - 1;
  115. for (int i = 0; i < HASH_SIZE; i++) {
  116. head[i] = 0;
  117. }
  118. for (int i = 0; i < WSIZE; i++) {
  119. prev[i] = 0;
  120. }
  121. }
  122. public void ResetAdler()
  123. {
  124. adler.Reset();
  125. }
  126. public int Adler {
  127. get {
  128. return (int)adler.Value;
  129. }
  130. }
  131. public int TotalIn {
  132. get {
  133. return totalIn;
  134. }
  135. }
  136. public DeflateStrategy Strategy {
  137. get {
  138. return strategy;
  139. }
  140. set {
  141. strategy = value;
  142. }
  143. }
  144. public void SetLevel(int lvl)
  145. {
  146. goodLength = DeflaterConstants.GOOD_LENGTH[lvl];
  147. max_lazy = DeflaterConstants.MAX_LAZY[lvl];
  148. niceLength = DeflaterConstants.NICE_LENGTH[lvl];
  149. max_chain = DeflaterConstants.MAX_CHAIN[lvl];
  150. if (DeflaterConstants.COMPR_FUNC[lvl] != comprFunc) {
  151. // if (DeflaterConstants.DEBUGGING) {
  152. // //Console.WriteLine("Change from "+comprFunc +" to "
  153. // + DeflaterConstants.COMPR_FUNC[lvl]);
  154. // }
  155. switch (comprFunc) {
  156. case DEFLATE_STORED:
  157. if (strstart > blockStart) {
  158. huffman.FlushStoredBlock(window, blockStart,
  159. strstart - blockStart, false);
  160. blockStart = strstart;
  161. }
  162. UpdateHash();
  163. break;
  164. case DEFLATE_FAST:
  165. if (strstart > blockStart) {
  166. huffman.FlushBlock(window, blockStart, strstart - blockStart,
  167. false);
  168. blockStart = strstart;
  169. }
  170. break;
  171. case DEFLATE_SLOW:
  172. if (prevAvailable) {
  173. huffman.TallyLit(window[strstart-1] & 0xff);
  174. }
  175. if (strstart > blockStart) {
  176. huffman.FlushBlock(window, blockStart, strstart - blockStart, false);
  177. blockStart = strstart;
  178. }
  179. prevAvailable = false;
  180. matchLen = MIN_MATCH - 1;
  181. break;
  182. }
  183. comprFunc = COMPR_FUNC[lvl];
  184. }
  185. }
  186. void UpdateHash()
  187. {
  188. // if (DEBUGGING) {
  189. // //Console.WriteLine("updateHash: "+strstart);
  190. // }
  191. ins_h = (window[strstart] << HASH_SHIFT) ^ window[strstart + 1];
  192. }
  193. int InsertString()
  194. {
  195. short match;
  196. int hash = ((ins_h << HASH_SHIFT) ^ window[strstart + (MIN_MATCH -1)]) & HASH_MASK;
  197. // if (DEBUGGING) {
  198. // if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^
  199. // (window[strstart + 1] << HASH_SHIFT) ^
  200. // (window[strstart + 2])) & HASH_MASK)) {
  201. // throw new Exception("hash inconsistent: "+hash+"/"
  202. // +window[strstart]+","
  203. // +window[strstart+1]+","
  204. // +window[strstart+2]+","+HASH_SHIFT);
  205. // }
  206. // }
  207. prev[strstart & WMASK] = match = head[hash];
  208. head[hash] = (short)strstart;
  209. ins_h = hash;
  210. return match & 0xffff;
  211. }
  212. void SlideWindow()
  213. {
  214. Array.Copy(window, WSIZE, window, 0, WSIZE);
  215. matchStart -= WSIZE;
  216. strstart -= WSIZE;
  217. blockStart -= WSIZE;
  218. /* Slide the hash table (could be avoided with 32 bit values
  219. * at the expense of memory usage).
  220. */
  221. for (int i = 0; i < HASH_SIZE; ++i) {
  222. int m = head[i] & 0xffff;
  223. head[i] = (short)(m >= WSIZE ? (m - WSIZE) : 0);
  224. }
  225. /* Slide the prev table. */
  226. for (int i = 0; i < WSIZE; i++) {
  227. int m = prev[i] & 0xffff;
  228. prev[i] = (short)(m >= WSIZE ? (m - WSIZE) : 0);
  229. }
  230. }
  231. public void FillWindow()
  232. {
  233. /* If the window is almost full and there is insufficient lookahead,
  234. * move the upper half to the lower one to make room in the upper half.
  235. */
  236. if (strstart >= WSIZE + MAX_DIST) {
  237. SlideWindow();
  238. }
  239. /* If there is not enough lookahead, but still some input left,
  240. * read in the input
  241. */
  242. while (lookahead < DeflaterConstants.MIN_LOOKAHEAD && inputOff < inputEnd) {
  243. int more = 2 * WSIZE - lookahead - strstart;
  244. if (more > inputEnd - inputOff) {
  245. more = inputEnd - inputOff;
  246. }
  247. System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more);
  248. adler.Update(inputBuf, inputOff, more);
  249. inputOff += more;
  250. totalIn += more;
  251. lookahead += more;
  252. }
  253. if (lookahead >= MIN_MATCH) {
  254. UpdateHash();
  255. }
  256. }
  257. bool FindLongestMatch(int curMatch)
  258. {
  259. int chainLength = this.max_chain;
  260. int niceLength = this.niceLength;
  261. short[] prev = this.prev;
  262. int scan = this.strstart;
  263. int match;
  264. int best_end = this.strstart + matchLen;
  265. int best_len = Math.Max(matchLen, MIN_MATCH - 1);
  266. int limit = Math.Max(strstart - MAX_DIST, 0);
  267. int strend = strstart + MAX_MATCH - 1;
  268. byte scan_end1 = window[best_end - 1];
  269. byte scan_end = window[best_end];
  270. /* Do not waste too much time if we already have a good match: */
  271. if (best_len >= this.goodLength) {
  272. chainLength >>= 2;
  273. }
  274. /* Do not look for matches beyond the end of the input. This is necessary
  275. * to make deflate deterministic.
  276. */
  277. if (niceLength > lookahead) {
  278. niceLength = lookahead;
  279. }
  280. if (DeflaterConstants.DEBUGGING && strstart > 2 * WSIZE - MIN_LOOKAHEAD) {
  281. throw new InvalidOperationException("need lookahead");
  282. }
  283. do {
  284. if (DeflaterConstants.DEBUGGING && curMatch >= strstart) {
  285. throw new InvalidOperationException("future match");
  286. }
  287. if (window[curMatch + best_len] != scan_end ||
  288. window[curMatch + best_len - 1] != scan_end1 ||
  289. window[curMatch] != window[scan] ||
  290. window[curMatch + 1] != window[scan + 1]) {
  291. continue;
  292. }
  293. match = curMatch + 2;
  294. scan += 2;
  295. /* We check for insufficient lookahead only every 8th comparison;
  296. * the 256th check will be made at strstart+258.
  297. */
  298. while (window[++scan] == window[++match] &&
  299. window[++scan] == window[++match] &&
  300. window[++scan] == window[++match] &&
  301. window[++scan] == window[++match] &&
  302. window[++scan] == window[++match] &&
  303. window[++scan] == window[++match] &&
  304. window[++scan] == window[++match] &&
  305. window[++scan] == window[++match] && scan < strend) ;
  306. if (scan > best_end) {
  307. // if (DeflaterConstants.DEBUGGING && ins_h == 0)
  308. // System.err.println("Found match: "+curMatch+"-"+(scan-strstart));
  309. matchStart = curMatch;
  310. best_end = scan;
  311. best_len = scan - strstart;
  312. if (best_len >= niceLength) {
  313. break;
  314. }
  315. scan_end1 = window[best_end - 1];
  316. scan_end = window[best_end];
  317. }
  318. scan = strstart;
  319. } while ((curMatch = (prev[curMatch & WMASK] & 0xffff)) > limit && --chainLength != 0);
  320. matchLen = Math.Min(best_len, lookahead);
  321. return matchLen >= MIN_MATCH;
  322. }
  323. public void SetDictionary(byte[] buffer, int offset, int length)
  324. {
  325. if (DeflaterConstants.DEBUGGING && strstart != 1) {
  326. throw new InvalidOperationException("strstart not 1");
  327. }
  328. adler.Update(buffer, offset, length);
  329. if (length < MIN_MATCH) {
  330. return;
  331. }
  332. if (length > MAX_DIST) {
  333. offset += length - MAX_DIST;
  334. length = MAX_DIST;
  335. }
  336. System.Array.Copy(buffer, offset, window, strstart, length);
  337. UpdateHash();
  338. --length;
  339. while (--length > 0) {
  340. InsertString();
  341. strstart++;
  342. }
  343. strstart += 2;
  344. blockStart = strstart;
  345. }
  346. bool DeflateStored(bool flush, bool finish)
  347. {
  348. if (!flush && lookahead == 0) {
  349. return false;
  350. }
  351. strstart += lookahead;
  352. lookahead = 0;
  353. int storedLen = strstart - blockStart;
  354. if ((storedLen >= DeflaterConstants.MAX_BLOCK_SIZE) || /* Block is full */
  355. (blockStart < WSIZE && storedLen >= MAX_DIST) || /* Block may move out of window */
  356. flush) {
  357. bool lastBlock = finish;
  358. if (storedLen > DeflaterConstants.MAX_BLOCK_SIZE) {
  359. storedLen = DeflaterConstants.MAX_BLOCK_SIZE;
  360. lastBlock = false;
  361. }
  362. // if (DeflaterConstants.DEBUGGING) {
  363. // //Console.WriteLine("storedBlock["+storedLen+","+lastBlock+"]");
  364. // }
  365. huffman.FlushStoredBlock(window, blockStart, storedLen, lastBlock);
  366. blockStart += storedLen;
  367. return !lastBlock;
  368. }
  369. return true;
  370. }
  371. private bool DeflateFast(bool flush, bool finish)
  372. {
  373. if (lookahead < MIN_LOOKAHEAD && !flush) {
  374. return false;
  375. }
  376. while (lookahead >= MIN_LOOKAHEAD || flush) {
  377. if (lookahead == 0) {
  378. /* We are flushing everything */
  379. huffman.FlushBlock(window, blockStart, strstart - blockStart, finish);
  380. blockStart = strstart;
  381. return false;
  382. }
  383. if (strstart > 2 * WSIZE - MIN_LOOKAHEAD) {
  384. /* slide window, as findLongestMatch need this.
  385. * This should only happen when flushing and the window
  386. * is almost full.
  387. */
  388. SlideWindow();
  389. }
  390. int hashHead;
  391. if (lookahead >= MIN_MATCH &&
  392. (hashHead = InsertString()) != 0 &&
  393. strategy != DeflateStrategy.HuffmanOnly &&
  394. strstart - hashHead <= MAX_DIST &&
  395. FindLongestMatch(hashHead)) {
  396. /* longestMatch sets matchStart and matchLen */
  397. // if (DeflaterConstants.DEBUGGING) {
  398. // for (int i = 0 ; i < matchLen; i++) {
  399. // if (window[strstart+i] != window[matchStart + i]) {
  400. // throw new Exception();
  401. // }
  402. // }
  403. // }
  404. // -jr- Hak hak hak this stops problems with fast/low compression and index out of range
  405. if (huffman.TallyDist(strstart - matchStart, matchLen)) {
  406. bool lastBlock = finish && lookahead == 0;
  407. huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock);
  408. blockStart = strstart;
  409. }
  410. lookahead -= matchLen;
  411. if (matchLen <= max_lazy && lookahead >= MIN_MATCH) {
  412. while (--matchLen > 0) {
  413. ++strstart;
  414. InsertString();
  415. }
  416. ++strstart;
  417. } else {
  418. strstart += matchLen;
  419. if (lookahead >= MIN_MATCH - 1) {
  420. UpdateHash();
  421. }
  422. }
  423. matchLen = MIN_MATCH - 1;
  424. continue;
  425. } else {
  426. /* No match found */
  427. huffman.TallyLit(window[strstart] & 0xff);
  428. ++strstart;
  429. --lookahead;
  430. }
  431. if (huffman.IsFull()) {
  432. bool lastBlock = finish && lookahead == 0;
  433. huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock);
  434. blockStart = strstart;
  435. return !lastBlock;
  436. }
  437. }
  438. return true;
  439. }
  440. bool DeflateSlow(bool flush, bool finish)
  441. {
  442. if (lookahead < MIN_LOOKAHEAD && !flush) {
  443. return false;
  444. }
  445. while (lookahead >= MIN_LOOKAHEAD || flush) {
  446. if (lookahead == 0) {
  447. if (prevAvailable) {
  448. huffman.TallyLit(window[strstart-1] & 0xff);
  449. }
  450. prevAvailable = false;
  451. /* We are flushing everything */
  452. if (DeflaterConstants.DEBUGGING && !flush) {
  453. throw new Exception("Not flushing, but no lookahead");
  454. }
  455. huffman.FlushBlock(window, blockStart, strstart - blockStart,
  456. finish);
  457. blockStart = strstart;
  458. return false;
  459. }
  460. if (strstart >= 2 * WSIZE - MIN_LOOKAHEAD) {
  461. /* slide window, as findLongestMatch need this.
  462. * This should only happen when flushing and the window
  463. * is almost full.
  464. */
  465. SlideWindow();
  466. }
  467. int prevMatch = matchStart;
  468. int prevLen = matchLen;
  469. if (lookahead >= MIN_MATCH) {
  470. int hashHead = InsertString();
  471. if (strategy != DeflateStrategy.HuffmanOnly && hashHead != 0 && strstart - hashHead <= MAX_DIST && FindLongestMatch(hashHead)) {
  472. /* longestMatch sets matchStart and matchLen */
  473. /* Discard match if too small and too far away */
  474. if (matchLen <= 5 && (strategy == DeflateStrategy.Filtered || (matchLen == MIN_MATCH && strstart - matchStart > TOO_FAR))) {
  475. matchLen = MIN_MATCH - 1;
  476. }
  477. }
  478. }
  479. /* previous match was better */
  480. if (prevLen >= MIN_MATCH && matchLen <= prevLen) {
  481. // if (DeflaterConstants.DEBUGGING) {
  482. // for (int i = 0 ; i < matchLen; i++) {
  483. // if (window[strstart-1+i] != window[prevMatch + i])
  484. // throw new Exception();
  485. // }
  486. // }
  487. huffman.TallyDist(strstart - 1 - prevMatch, prevLen);
  488. prevLen -= 2;
  489. do {
  490. strstart++;
  491. lookahead--;
  492. if (lookahead >= MIN_MATCH) {
  493. InsertString();
  494. }
  495. } while (--prevLen > 0);
  496. strstart ++;
  497. lookahead--;
  498. prevAvailable = false;
  499. matchLen = MIN_MATCH - 1;
  500. } else {
  501. if (prevAvailable) {
  502. huffman.TallyLit(window[strstart-1] & 0xff);
  503. }
  504. prevAvailable = true;
  505. strstart++;
  506. lookahead--;
  507. }
  508. if (huffman.IsFull()) {
  509. int len = strstart - blockStart;
  510. if (prevAvailable) {
  511. len--;
  512. }
  513. bool lastBlock = (finish && lookahead == 0 && !prevAvailable);
  514. huffman.FlushBlock(window, blockStart, len, lastBlock);
  515. blockStart += len;
  516. return !lastBlock;
  517. }
  518. }
  519. return true;
  520. }
  521. public bool Deflate(bool flush, bool finish)
  522. {
  523. bool progress;
  524. do {
  525. FillWindow();
  526. bool canFlush = flush && inputOff == inputEnd;
  527. // if (DeflaterConstants.DEBUGGING) {
  528. // //Console.WriteLine("window: ["+blockStart+","+strstart+","
  529. // +lookahead+"], "+comprFunc+","+canFlush);
  530. // }
  531. switch (comprFunc) {
  532. case DEFLATE_STORED:
  533. progress = DeflateStored(canFlush, finish);
  534. break;
  535. case DEFLATE_FAST:
  536. progress = DeflateFast(canFlush, finish);
  537. break;
  538. case DEFLATE_SLOW:
  539. progress = DeflateSlow(canFlush, finish);
  540. break;
  541. default:
  542. throw new InvalidOperationException("unknown comprFunc");
  543. }
  544. } while (pending.IsFlushed && progress); /* repeat while we have no pending output and progress was made */
  545. return progress;
  546. }
  547. public void SetInput(byte[] buf, int off, int len)
  548. {
  549. if (inputOff < inputEnd) {
  550. throw new InvalidOperationException("Old input was not completely processed");
  551. }
  552. int end = off + len;
  553. /* We want to throw an ArrayIndexOutOfBoundsException early. The
  554. * check is very tricky: it also handles integer wrap around.
  555. */
  556. if (0 > off || off > end || end > buf.Length) {
  557. throw new ArgumentOutOfRangeException();
  558. }
  559. inputBuf = buf;
  560. inputOff = off;
  561. inputEnd = end;
  562. }
  563. public bool NeedsInput()
  564. {
  565. return inputEnd == inputOff;
  566. }
  567. }
  568. }