/plugins/FTP/tags/release-0-9-5/com/jcraft/jzlib/Deflate.java

# · Java · 1623 lines · 1008 code · 257 blank · 358 comment · 290 complexity · 6a749da96ce8dcfca9ee902b11e347e6 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* -*-mode:java; c-basic-offset:2; -*- */
  2. /*
  3. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the distribution.
  11. 3. The names of the authors may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  14. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  16. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /*
  25. * This program is based on zlib-1.1.3, so all credit should go authors
  26. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  27. * and contributors of zlib.
  28. */
  29. package com.jcraft.jzlib;
  30. public
  31. final class Deflate{
  32. static final private int MAX_MEM_LEVEL=9;
  33. static final private int Z_DEFAULT_COMPRESSION=-1;
  34. static final private int MAX_WBITS=15; // 32K LZ77 window
  35. static final private int DEF_MEM_LEVEL=8;
  36. static class Config{
  37. int good_length; // reduce lazy search above this match length
  38. int max_lazy; // do not perform lazy search above this match length
  39. int nice_length; // quit search above this match length
  40. int max_chain;
  41. int func;
  42. Config(int good_length, int max_lazy,
  43. int nice_length, int max_chain, int func){
  44. this.good_length=good_length;
  45. this.max_lazy=max_lazy;
  46. this.nice_length=nice_length;
  47. this.max_chain=max_chain;
  48. this.func=func;
  49. }
  50. }
  51. static final private int STORED=0;
  52. static final private int FAST=1;
  53. static final private int SLOW=2;
  54. static final private Config[] config_table;
  55. static{
  56. config_table=new Config[10];
  57. // good lazy nice chain
  58. config_table[0]=new Config(0, 0, 0, 0, STORED);
  59. config_table[1]=new Config(4, 4, 8, 4, FAST);
  60. config_table[2]=new Config(4, 5, 16, 8, FAST);
  61. config_table[3]=new Config(4, 6, 32, 32, FAST);
  62. config_table[4]=new Config(4, 4, 16, 16, SLOW);
  63. config_table[5]=new Config(8, 16, 32, 32, SLOW);
  64. config_table[6]=new Config(8, 16, 128, 128, SLOW);
  65. config_table[7]=new Config(8, 32, 128, 256, SLOW);
  66. config_table[8]=new Config(32, 128, 258, 1024, SLOW);
  67. config_table[9]=new Config(32, 258, 258, 4096, SLOW);
  68. }
  69. static final private String[] z_errmsg = {
  70. "need dictionary", // Z_NEED_DICT 2
  71. "stream end", // Z_STREAM_END 1
  72. "", // Z_OK 0
  73. "file error", // Z_ERRNO (-1)
  74. "stream error", // Z_STREAM_ERROR (-2)
  75. "data error", // Z_DATA_ERROR (-3)
  76. "insufficient memory", // Z_MEM_ERROR (-4)
  77. "buffer error", // Z_BUF_ERROR (-5)
  78. "incompatible version",// Z_VERSION_ERROR (-6)
  79. ""
  80. };
  81. // block not completed, need more input or more output
  82. static final private int NeedMore=0;
  83. // block flush performed
  84. static final private int BlockDone=1;
  85. // finish started, need only more output at next deflate
  86. static final private int FinishStarted=2;
  87. // finish done, accept no more input or output
  88. static final private int FinishDone=3;
  89. // preset dictionary flag in zlib header
  90. static final private int PRESET_DICT=0x20;
  91. static final private int Z_FILTERED=1;
  92. static final private int Z_HUFFMAN_ONLY=2;
  93. static final private int Z_DEFAULT_STRATEGY=0;
  94. static final private int Z_NO_FLUSH=0;
  95. static final private int Z_PARTIAL_FLUSH=1;
  96. static final private int Z_SYNC_FLUSH=2;
  97. static final private int Z_FULL_FLUSH=3;
  98. static final private int Z_FINISH=4;
  99. static final private int Z_OK=0;
  100. static final private int Z_STREAM_END=1;
  101. static final private int Z_NEED_DICT=2;
  102. static final private int Z_ERRNO=-1;
  103. static final private int Z_STREAM_ERROR=-2;
  104. static final private int Z_DATA_ERROR=-3;
  105. static final private int Z_MEM_ERROR=-4;
  106. static final private int Z_BUF_ERROR=-5;
  107. static final private int Z_VERSION_ERROR=-6;
  108. static final private int INIT_STATE=42;
  109. static final private int BUSY_STATE=113;
  110. static final private int FINISH_STATE=666;
  111. // The deflate compression method
  112. static final private int Z_DEFLATED=8;
  113. static final private int STORED_BLOCK=0;
  114. static final private int STATIC_TREES=1;
  115. static final private int DYN_TREES=2;
  116. // The three kinds of block type
  117. static final private int Z_BINARY=0;
  118. static final private int Z_ASCII=1;
  119. static final private int Z_UNKNOWN=2;
  120. static final private int Buf_size=8*2;
  121. // repeat previous bit length 3-6 times (2 bits of repeat count)
  122. static final private int REP_3_6=16;
  123. // repeat a zero length 3-10 times (3 bits of repeat count)
  124. static final private int REPZ_3_10=17;
  125. // repeat a zero length 11-138 times (7 bits of repeat count)
  126. static final private int REPZ_11_138=18;
  127. static final private int MIN_MATCH=3;
  128. static final private int MAX_MATCH=258;
  129. static final private int MIN_LOOKAHEAD=(MAX_MATCH+MIN_MATCH+1);
  130. static final private int MAX_BITS=15;
  131. static final private int D_CODES=30;
  132. static final private int BL_CODES=19;
  133. static final private int LENGTH_CODES=29;
  134. static final private int LITERALS=256;
  135. static final private int L_CODES=(LITERALS+1+LENGTH_CODES);
  136. static final private int HEAP_SIZE=(2*L_CODES+1);
  137. static final private int END_BLOCK=256;
  138. ZStream strm; // pointer back to this zlib stream
  139. int status; // as the name implies
  140. byte[] pending_buf; // output still pending
  141. int pending_buf_size; // size of pending_buf
  142. int pending_out; // next pending byte to output to the stream
  143. int pending; // nb of bytes in the pending buffer
  144. int noheader; // suppress zlib header and adler32
  145. byte data_type; // UNKNOWN, BINARY or ASCII
  146. byte method; // STORED (for zip only) or DEFLATED
  147. int last_flush; // value of flush param for previous deflate call
  148. int w_size; // LZ77 window size (32K by default)
  149. int w_bits; // log2(w_size) (8..16)
  150. int w_mask; // w_size - 1
  151. byte[] window;
  152. // Sliding window. Input bytes are read into the second half of the window,
  153. // and move to the first half later to keep a dictionary of at least wSize
  154. // bytes. With this organization, matches are limited to a distance of
  155. // wSize-MAX_MATCH bytes, but this ensures that IO is always
  156. // performed with a length multiple of the block size. Also, it limits
  157. // the window size to 64K, which is quite useful on MSDOS.
  158. // To do: use the user input buffer as sliding window.
  159. int window_size;
  160. // Actual size of window: 2*wSize, except when the user input buffer
  161. // is directly used as sliding window.
  162. short[] prev;
  163. // Link to older string with same hash index. To limit the size of this
  164. // array to 64K, this link is maintained only for the last 32K strings.
  165. // An index in this array is thus a window index modulo 32K.
  166. short[] head; // Heads of the hash chains or NIL.
  167. int ins_h; // hash index of string to be inserted
  168. int hash_size; // number of elements in hash table
  169. int hash_bits; // log2(hash_size)
  170. int hash_mask; // hash_size-1
  171. // Number of bits by which ins_h must be shifted at each input
  172. // step. It must be such that after MIN_MATCH steps, the oldest
  173. // byte no longer takes part in the hash key, that is:
  174. // hash_shift * MIN_MATCH >= hash_bits
  175. int hash_shift;
  176. // Window position at the beginning of the current output block. Gets
  177. // negative when the window is moved backwards.
  178. int block_start;
  179. int match_length; // length of best match
  180. int prev_match; // previous match
  181. int match_available; // set if previous match exists
  182. int strstart; // start of string to insert
  183. int match_start; // start of matching string
  184. int lookahead; // number of valid bytes ahead in window
  185. // Length of the best match at previous step. Matches not greater than this
  186. // are discarded. This is used in the lazy match evaluation.
  187. int prev_length;
  188. // To speed up deflation, hash chains are never searched beyond this
  189. // length. A higher limit improves compression ratio but degrades the speed.
  190. int max_chain_length;
  191. // Attempt to find a better match only when the current match is strictly
  192. // smaller than this value. This mechanism is used only for compression
  193. // levels >= 4.
  194. int max_lazy_match;
  195. // Insert new strings in the hash table only if the match length is not
  196. // greater than this length. This saves time but degrades compression.
  197. // max_insert_length is used only for compression levels <= 3.
  198. int level; // compression level (1..9)
  199. int strategy; // favor or force Huffman coding
  200. // Use a faster search when the previous match is longer than this
  201. int good_match;
  202. // Stop searching when current match exceeds this
  203. int nice_match;
  204. short[] dyn_ltree; // literal and length tree
  205. short[] dyn_dtree; // distance tree
  206. short[] bl_tree; // Huffman tree for bit lengths
  207. Tree l_desc=new Tree(); // desc for literal tree
  208. Tree d_desc=new Tree(); // desc for distance tree
  209. Tree bl_desc=new Tree(); // desc for bit length tree
  210. // number of codes at each bit length for an optimal tree
  211. short[] bl_count=new short[MAX_BITS+1];
  212. // heap used to build the Huffman trees
  213. int[] heap=new int[2*L_CODES+1];
  214. int heap_len; // number of elements in the heap
  215. int heap_max; // element of largest frequency
  216. // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  217. // The same heap array is used to build all trees.
  218. // Depth of each subtree used as tie breaker for trees of equal frequency
  219. byte[] depth=new byte[2*L_CODES+1];
  220. int l_buf; // index for literals or lengths */
  221. // Size of match buffer for literals/lengths. There are 4 reasons for
  222. // limiting lit_bufsize to 64K:
  223. // - frequencies can be kept in 16 bit counters
  224. // - if compression is not successful for the first block, all input
  225. // data is still in the window so we can still emit a stored block even
  226. // when input comes from standard input. (This can also be done for
  227. // all blocks if lit_bufsize is not greater than 32K.)
  228. // - if compression is not successful for a file smaller than 64K, we can
  229. // even emit a stored file instead of a stored block (saving 5 bytes).
  230. // This is applicable only for zip (not gzip or zlib).
  231. // - creating new Huffman trees less frequently may not provide fast
  232. // adaptation to changes in the input data statistics. (Take for
  233. // example a binary file with poorly compressible code followed by
  234. // a highly compressible string table.) Smaller buffer sizes give
  235. // fast adaptation but have of course the overhead of transmitting
  236. // trees more frequently.
  237. // - I can't count above 4
  238. int lit_bufsize;
  239. int last_lit; // running index in l_buf
  240. // Buffer for distances. To simplify the code, d_buf and l_buf have
  241. // the same number of elements. To use different lengths, an extra flag
  242. // array would be necessary.
  243. int d_buf; // index of pendig_buf
  244. int opt_len; // bit length of current block with optimal trees
  245. int static_len; // bit length of current block with static trees
  246. int matches; // number of string matches in current block
  247. int last_eob_len; // bit length of EOB code for last block
  248. // Output buffer. bits are inserted starting at the bottom (least
  249. // significant bits).
  250. short bi_buf;
  251. // Number of valid bits in bi_buf. All bits above the last valid bit
  252. // are always zero.
  253. int bi_valid;
  254. Deflate(){
  255. dyn_ltree=new short[HEAP_SIZE*2];
  256. dyn_dtree=new short[(2*D_CODES+1)*2]; // distance tree
  257. bl_tree=new short[(2*BL_CODES+1)*2]; // Huffman tree for bit lengths
  258. }
  259. void lm_init() {
  260. window_size=2*w_size;
  261. head[hash_size-1]=0;
  262. for(int i=0; i<hash_size-1; i++){
  263. head[i]=0;
  264. }
  265. // Set the default configuration parameters:
  266. max_lazy_match = Deflate.config_table[level].max_lazy;
  267. good_match = Deflate.config_table[level].good_length;
  268. nice_match = Deflate.config_table[level].nice_length;
  269. max_chain_length = Deflate.config_table[level].max_chain;
  270. strstart = 0;
  271. block_start = 0;
  272. lookahead = 0;
  273. match_length = prev_length = MIN_MATCH-1;
  274. match_available = 0;
  275. ins_h = 0;
  276. }
  277. // Initialize the tree data structures for a new zlib stream.
  278. void tr_init(){
  279. l_desc.dyn_tree = dyn_ltree;
  280. l_desc.stat_desc = StaticTree.static_l_desc;
  281. d_desc.dyn_tree = dyn_dtree;
  282. d_desc.stat_desc = StaticTree.static_d_desc;
  283. bl_desc.dyn_tree = bl_tree;
  284. bl_desc.stat_desc = StaticTree.static_bl_desc;
  285. bi_buf = 0;
  286. bi_valid = 0;
  287. last_eob_len = 8; // enough lookahead for inflate
  288. // Initialize the first block of the first file:
  289. init_block();
  290. }
  291. void init_block(){
  292. // Initialize the trees.
  293. for(int i = 0; i < L_CODES; i++) dyn_ltree[i*2] = 0;
  294. for(int i= 0; i < D_CODES; i++) dyn_dtree[i*2] = 0;
  295. for(int i= 0; i < BL_CODES; i++) bl_tree[i*2] = 0;
  296. dyn_ltree[END_BLOCK*2] = 1;
  297. opt_len = static_len = 0;
  298. last_lit = matches = 0;
  299. }
  300. // Restore the heap property by moving down the tree starting at node k,
  301. // exchanging a node with the smallest of its two sons if necessary, stopping
  302. // when the heap property is re-established (each father smaller than its
  303. // two sons).
  304. void pqdownheap(short[] tree, // the tree to restore
  305. int k // node to move down
  306. ){
  307. int v = heap[k];
  308. int j = k << 1; // left son of k
  309. while (j <= heap_len) {
  310. // Set j to the smallest of the two sons:
  311. if (j < heap_len &&
  312. smaller(tree, heap[j+1], heap[j], depth)){
  313. j++;
  314. }
  315. // Exit if v is smaller than both sons
  316. if(smaller(tree, v, heap[j], depth)) break;
  317. // Exchange v with the smallest son
  318. heap[k]=heap[j]; k = j;
  319. // And continue down the tree, setting j to the left son of k
  320. j <<= 1;
  321. }
  322. heap[k] = v;
  323. }
  324. static boolean smaller(short[] tree, int n, int m, byte[] depth){
  325. short tn2=tree[n*2];
  326. short tm2=tree[m*2];
  327. return (tn2<tm2 ||
  328. (tn2==tm2 && depth[n] <= depth[m]));
  329. }
  330. // Scan a literal or distance tree to determine the frequencies of the codes
  331. // in the bit length tree.
  332. void scan_tree (short[] tree,// the tree to be scanned
  333. int max_code // and its largest code of non zero frequency
  334. ){
  335. int n; // iterates over all tree elements
  336. int prevlen = -1; // last emitted length
  337. int curlen; // length of current code
  338. int nextlen = tree[0*2+1]; // length of next code
  339. int count = 0; // repeat count of the current code
  340. int max_count = 7; // max repeat count
  341. int min_count = 4; // min repeat count
  342. if (nextlen == 0){ max_count = 138; min_count = 3; }
  343. tree[(max_code+1)*2+1] = (short)0xffff; // guard
  344. for(n = 0; n <= max_code; n++) {
  345. curlen = nextlen; nextlen = tree[(n+1)*2+1];
  346. if(++count < max_count && curlen == nextlen) {
  347. continue;
  348. }
  349. else if(count < min_count) {
  350. bl_tree[curlen*2] += count;
  351. }
  352. else if(curlen != 0) {
  353. if(curlen != prevlen) bl_tree[curlen*2]++;
  354. bl_tree[REP_3_6*2]++;
  355. }
  356. else if(count <= 10) {
  357. bl_tree[REPZ_3_10*2]++;
  358. }
  359. else{
  360. bl_tree[REPZ_11_138*2]++;
  361. }
  362. count = 0; prevlen = curlen;
  363. if(nextlen == 0) {
  364. max_count = 138; min_count = 3;
  365. }
  366. else if(curlen == nextlen) {
  367. max_count = 6; min_count = 3;
  368. }
  369. else{
  370. max_count = 7; min_count = 4;
  371. }
  372. }
  373. }
  374. // Construct the Huffman tree for the bit lengths and return the index in
  375. // bl_order of the last bit length code to send.
  376. int build_bl_tree(){
  377. int max_blindex; // index of last bit length code of non zero freq
  378. // Determine the bit length frequencies for literal and distance trees
  379. scan_tree(dyn_ltree, l_desc.max_code);
  380. scan_tree(dyn_dtree, d_desc.max_code);
  381. // Build the bit length tree:
  382. bl_desc.build_tree(this);
  383. // opt_len now includes the length of the tree representations, except
  384. // the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  385. // Determine the number of bit length codes to send. The pkzip format
  386. // requires that at least 4 bit length codes be sent. (appnote.txt says
  387. // 3 but the actual value used is 4.)
  388. for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  389. if (bl_tree[Tree.bl_order[max_blindex]*2+1] != 0) break;
  390. }
  391. // Update opt_len to include the bit length tree and counts
  392. opt_len += 3*(max_blindex+1) + 5+5+4;
  393. return max_blindex;
  394. }
  395. // Send the header for a block using dynamic Huffman trees: the counts, the
  396. // lengths of the bit length codes, the literal tree and the distance tree.
  397. // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  398. void send_all_trees(int lcodes, int dcodes, int blcodes){
  399. int rank; // index in bl_order
  400. send_bits(lcodes-257, 5); // not +255 as stated in appnote.txt
  401. send_bits(dcodes-1, 5);
  402. send_bits(blcodes-4, 4); // not -3 as stated in appnote.txt
  403. for (rank = 0; rank < blcodes; rank++) {
  404. send_bits(bl_tree[Tree.bl_order[rank]*2+1], 3);
  405. }
  406. send_tree(dyn_ltree, lcodes-1); // literal tree
  407. send_tree(dyn_dtree, dcodes-1); // distance tree
  408. }
  409. // Send a literal or distance tree in compressed form, using the codes in
  410. // bl_tree.
  411. void send_tree (short[] tree,// the tree to be sent
  412. int max_code // and its largest code of non zero frequency
  413. ){
  414. int n; // iterates over all tree elements
  415. int prevlen = -1; // last emitted length
  416. int curlen; // length of current code
  417. int nextlen = tree[0*2+1]; // length of next code
  418. int count = 0; // repeat count of the current code
  419. int max_count = 7; // max repeat count
  420. int min_count = 4; // min repeat count
  421. if (nextlen == 0){ max_count = 138; min_count = 3; }
  422. for (n = 0; n <= max_code; n++) {
  423. curlen = nextlen; nextlen = tree[(n+1)*2+1];
  424. if(++count < max_count && curlen == nextlen) {
  425. continue;
  426. }
  427. else if(count < min_count) {
  428. do { send_code(curlen, bl_tree); } while (--count != 0);
  429. }
  430. else if(curlen != 0){
  431. if(curlen != prevlen){
  432. send_code(curlen, bl_tree); count--;
  433. }
  434. send_code(REP_3_6, bl_tree);
  435. send_bits(count-3, 2);
  436. }
  437. else if(count <= 10){
  438. send_code(REPZ_3_10, bl_tree);
  439. send_bits(count-3, 3);
  440. }
  441. else{
  442. send_code(REPZ_11_138, bl_tree);
  443. send_bits(count-11, 7);
  444. }
  445. count = 0; prevlen = curlen;
  446. if(nextlen == 0){
  447. max_count = 138; min_count = 3;
  448. }
  449. else if(curlen == nextlen){
  450. max_count = 6; min_count = 3;
  451. }
  452. else{
  453. max_count = 7; min_count = 4;
  454. }
  455. }
  456. }
  457. // Output a byte on the stream.
  458. // IN assertion: there is enough room in pending_buf.
  459. final void put_byte(byte[] p, int start, int len){
  460. System.arraycopy(p, start, pending_buf, pending, len);
  461. pending+=len;
  462. }
  463. final void put_byte(byte c){
  464. pending_buf[pending++]=c;
  465. }
  466. final void put_short(int w) {
  467. put_byte((byte)(w/*&0xff*/));
  468. put_byte((byte)(w>>>8));
  469. }
  470. final void putShortMSB(int b){
  471. put_byte((byte)(b>>8));
  472. put_byte((byte)(b/*&0xff*/));
  473. }
  474. final void send_code(int c, short[] tree){
  475. int c2=c*2;
  476. send_bits((tree[c2]&0xffff), (tree[c2+1]&0xffff));
  477. }
  478. void send_bits(int value, int length){
  479. int len = length;
  480. if (bi_valid > (int)Buf_size - len) {
  481. int val = value;
  482. // bi_buf |= (val << bi_valid);
  483. bi_buf |= ((val << bi_valid)&0xffff);
  484. put_short(bi_buf);
  485. bi_buf = (short)(val >>> (Buf_size - bi_valid));
  486. bi_valid += len - Buf_size;
  487. } else {
  488. // bi_buf |= (value) << bi_valid;
  489. bi_buf |= (((value) << bi_valid)&0xffff);
  490. bi_valid += len;
  491. }
  492. }
  493. // Send one empty static block to give enough lookahead for inflate.
  494. // This takes 10 bits, of which 7 may remain in the bit buffer.
  495. // The current inflate code requires 9 bits of lookahead. If the
  496. // last two codes for the previous block (real code plus EOB) were coded
  497. // on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  498. // the last real code. In this case we send two empty static blocks instead
  499. // of one. (There are no problems if the previous block is stored or fixed.)
  500. // To simplify the code, we assume the worst case of last real code encoded
  501. // on one bit only.
  502. void _tr_align(){
  503. send_bits(STATIC_TREES<<1, 3);
  504. send_code(END_BLOCK, StaticTree.static_ltree);
  505. bi_flush();
  506. // Of the 10 bits for the empty block, we have already sent
  507. // (10 - bi_valid) bits. The lookahead for the last real code (before
  508. // the EOB of the previous block) was thus at least one plus the length
  509. // of the EOB plus what we have just sent of the empty static block.
  510. if (1 + last_eob_len + 10 - bi_valid < 9) {
  511. send_bits(STATIC_TREES<<1, 3);
  512. send_code(END_BLOCK, StaticTree.static_ltree);
  513. bi_flush();
  514. }
  515. last_eob_len = 7;
  516. }
  517. // Save the match info and tally the frequency counts. Return true if
  518. // the current block must be flushed.
  519. boolean _tr_tally (int dist, // distance of matched string
  520. int lc // match length-MIN_MATCH or unmatched char (if dist==0)
  521. ){
  522. pending_buf[d_buf+last_lit*2] = (byte)(dist>>>8);
  523. pending_buf[d_buf+last_lit*2+1] = (byte)dist;
  524. pending_buf[l_buf+last_lit] = (byte)lc; last_lit++;
  525. if (dist == 0) {
  526. // lc is the unmatched char
  527. dyn_ltree[lc*2]++;
  528. }
  529. else {
  530. matches++;
  531. // Here, lc is the match length - MIN_MATCH
  532. dist--; // dist = match distance - 1
  533. dyn_ltree[(Tree._length_code[lc]+LITERALS+1)*2]++;
  534. dyn_dtree[Tree.d_code(dist)*2]++;
  535. }
  536. if ((last_lit & 0x1fff) == 0 && level > 2) {
  537. // Compute an upper bound for the compressed length
  538. int out_length = last_lit*8;
  539. int in_length = strstart - block_start;
  540. int dcode;
  541. for (dcode = 0; dcode < D_CODES; dcode++) {
  542. out_length += (int)dyn_dtree[dcode*2] *
  543. (5L+Tree.extra_dbits[dcode]);
  544. }
  545. out_length >>>= 3;
  546. if ((matches < (last_lit/2)) && out_length < in_length/2) return true;
  547. }
  548. return (last_lit == lit_bufsize-1);
  549. // We avoid equality with lit_bufsize because of wraparound at 64K
  550. // on 16 bit machines and because stored blocks are restricted to
  551. // 64K-1 bytes.
  552. }
  553. // Send the block data compressed using the given Huffman trees
  554. void compress_block(short[] ltree, short[] dtree){
  555. int dist; // distance of matched string
  556. int lc; // match length or unmatched char (if dist == 0)
  557. int lx = 0; // running index in l_buf
  558. int code; // the code to send
  559. int extra; // number of extra bits to send
  560. if (last_lit != 0){
  561. do{
  562. dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)|
  563. (pending_buf[d_buf+lx*2+1]&0xff);
  564. lc=(pending_buf[l_buf+lx])&0xff; lx++;
  565. if(dist == 0){
  566. send_code(lc, ltree); // send a literal byte
  567. }
  568. else{
  569. // Here, lc is the match length - MIN_MATCH
  570. code = Tree._length_code[lc];
  571. send_code(code+LITERALS+1, ltree); // send the length code
  572. extra = Tree.extra_lbits[code];
  573. if(extra != 0){
  574. lc -= Tree.base_length[code];
  575. send_bits(lc, extra); // send the extra length bits
  576. }
  577. dist--; // dist is now the match distance - 1
  578. code = Tree.d_code(dist);
  579. send_code(code, dtree); // send the distance code
  580. extra = Tree.extra_dbits[code];
  581. if (extra != 0) {
  582. dist -= Tree.base_dist[code];
  583. send_bits(dist, extra); // send the extra distance bits
  584. }
  585. } // literal or match pair ?
  586. // Check that the overlay between pending_buf and d_buf+l_buf is ok:
  587. }
  588. while (lx < last_lit);
  589. }
  590. send_code(END_BLOCK, ltree);
  591. last_eob_len = ltree[END_BLOCK*2+1];
  592. }
  593. // Set the data type to ASCII or BINARY, using a crude approximation:
  594. // binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  595. // IN assertion: the fields freq of dyn_ltree are set and the total of all
  596. // frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  597. void set_data_type(){
  598. int n = 0;
  599. int ascii_freq = 0;
  600. int bin_freq = 0;
  601. while(n<7){ bin_freq += dyn_ltree[n*2]; n++;}
  602. while(n<128){ ascii_freq += dyn_ltree[n*2]; n++;}
  603. while(n<LITERALS){ bin_freq += dyn_ltree[n*2]; n++;}
  604. data_type=(byte)(bin_freq > (ascii_freq >>> 2) ? Z_BINARY : Z_ASCII);
  605. }
  606. // Flush the bit buffer, keeping at most 7 bits in it.
  607. void bi_flush(){
  608. if (bi_valid == 16) {
  609. put_short(bi_buf);
  610. bi_buf=0;
  611. bi_valid=0;
  612. }
  613. else if (bi_valid >= 8) {
  614. put_byte((byte)bi_buf);
  615. bi_buf>>>=8;
  616. bi_valid-=8;
  617. }
  618. }
  619. // Flush the bit buffer and align the output on a byte boundary
  620. void bi_windup(){
  621. if (bi_valid > 8) {
  622. put_short(bi_buf);
  623. } else if (bi_valid > 0) {
  624. put_byte((byte)bi_buf);
  625. }
  626. bi_buf = 0;
  627. bi_valid = 0;
  628. }
  629. // Copy a stored block, storing first the length and its
  630. // one's complement if requested.
  631. void copy_block(int buf, // the input data
  632. int len, // its length
  633. boolean header // true if block header must be written
  634. ){
  635. int index=0;
  636. bi_windup(); // align on byte boundary
  637. last_eob_len = 8; // enough lookahead for inflate
  638. if (header) {
  639. put_short((short)len);
  640. put_short((short)~len);
  641. }
  642. // while(len--!=0) {
  643. // put_byte(window[buf+index]);
  644. // index++;
  645. // }
  646. put_byte(window, buf, len);
  647. }
  648. void flush_block_only(boolean eof){
  649. _tr_flush_block(block_start>=0 ? block_start : -1,
  650. strstart-block_start,
  651. eof);
  652. block_start=strstart;
  653. strm.flush_pending();
  654. }
  655. // Copy without compression as much as possible from the input stream, return
  656. // the current block state.
  657. // This function does not insert new strings in the dictionary since
  658. // uncompressible data is probably not useful. This function is used
  659. // only for the level=0 compression option.
  660. // NOTE: this function should be optimized to avoid extra copying from
  661. // window to pending_buf.
  662. int deflate_stored(int flush){
  663. // Stored blocks are limited to 0xffff bytes, pending_buf is limited
  664. // to pending_buf_size, and each stored block has a 5 byte header:
  665. int max_block_size = 0xffff;
  666. int max_start;
  667. if(max_block_size > pending_buf_size - 5) {
  668. max_block_size = pending_buf_size - 5;
  669. }
  670. // Copy as much as possible from input to output:
  671. while(true){
  672. // Fill the window as much as possible:
  673. if(lookahead<=1){
  674. fill_window();
  675. if(lookahead==0 && flush==Z_NO_FLUSH) return NeedMore;
  676. if(lookahead==0) break; // flush the current block
  677. }
  678. strstart+=lookahead;
  679. lookahead=0;
  680. // Emit a stored block if pending_buf will be full:
  681. max_start=block_start+max_block_size;
  682. if(strstart==0|| strstart>=max_start) {
  683. // strstart == 0 is possible when wraparound on 16-bit machine
  684. lookahead = (int)(strstart-max_start);
  685. strstart = (int)max_start;
  686. flush_block_only(false);
  687. if(strm.avail_out==0) return NeedMore;
  688. }
  689. // Flush if we may have to slide, otherwise block_start may become
  690. // negative and the data will be gone:
  691. if(strstart-block_start >= w_size-MIN_LOOKAHEAD) {
  692. flush_block_only(false);
  693. if(strm.avail_out==0) return NeedMore;
  694. }
  695. }
  696. flush_block_only(flush == Z_FINISH);
  697. if(strm.avail_out==0)
  698. return (flush == Z_FINISH) ? FinishStarted : NeedMore;
  699. return flush == Z_FINISH ? FinishDone : BlockDone;
  700. }
  701. // Send a stored block
  702. void _tr_stored_block(int buf, // input block
  703. int stored_len, // length of input block
  704. boolean eof // true if this is the last block for a file
  705. ){
  706. send_bits((STORED_BLOCK<<1)+(eof?1:0), 3); // send block type
  707. copy_block(buf, stored_len, true); // with header
  708. }
  709. // Determine the best encoding for the current block: dynamic trees, static
  710. // trees or store, and output the encoded block to the zip file.
  711. void _tr_flush_block(int buf, // input block, or NULL if too old
  712. int stored_len, // length of input block
  713. boolean eof // true if this is the last block for a file
  714. ) {
  715. int opt_lenb, static_lenb;// opt_len and static_len in bytes
  716. int max_blindex = 0; // index of last bit length code of non zero freq
  717. // Build the Huffman trees unless a stored block is forced
  718. if(level > 0) {
  719. // Check if the file is ascii or binary
  720. if(data_type == Z_UNKNOWN) set_data_type();
  721. // Construct the literal and distance trees
  722. l_desc.build_tree(this);
  723. d_desc.build_tree(this);
  724. // At this point, opt_len and static_len are the total bit lengths of
  725. // the compressed block data, excluding the tree representations.
  726. // Build the bit length tree for the above two trees, and get the index
  727. // in bl_order of the last bit length code to send.
  728. max_blindex=build_bl_tree();
  729. // Determine the best encoding. Compute first the block length in bytes
  730. opt_lenb=(opt_len+3+7)>>>3;
  731. static_lenb=(static_len+3+7)>>>3;
  732. if(static_lenb<=opt_lenb) opt_lenb=static_lenb;
  733. }
  734. else {
  735. opt_lenb=static_lenb=stored_len+5; // force a stored block
  736. }
  737. if(stored_len+4<=opt_lenb && buf != -1){
  738. // 4: two words for the lengths
  739. // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  740. // Otherwise we can't have processed more than WSIZE input bytes since
  741. // the last block flush, because compression would have been
  742. // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  743. // transform a block into a stored block.
  744. _tr_stored_block(buf, stored_len, eof);
  745. }
  746. else if(static_lenb == opt_lenb){
  747. send_bits((STATIC_TREES<<1)+(eof?1:0), 3);
  748. compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
  749. }
  750. else{
  751. send_bits((DYN_TREES<<1)+(eof?1:0), 3);
  752. send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
  753. compress_block(dyn_ltree, dyn_dtree);
  754. }
  755. // The above check is made mod 2^32, for files larger than 512 MB
  756. // and uLong implemented on 32 bits.
  757. init_block();
  758. if(eof){
  759. bi_windup();
  760. }
  761. }
  762. // Fill the window when the lookahead becomes insufficient.
  763. // Updates strstart and lookahead.
  764. //
  765. // IN assertion: lookahead < MIN_LOOKAHEAD
  766. // OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  767. // At least one byte has been read, or avail_in == 0; reads are
  768. // performed for at least two bytes (required for the zip translate_eol
  769. // option -- not supported here).
  770. void fill_window(){
  771. int n, m;
  772. int p;
  773. int more; // Amount of free space at the end of the window.
  774. do{
  775. more = (window_size-lookahead-strstart);
  776. // Deal with !@#$% 64K limit:
  777. if(more==0 && strstart==0 && lookahead==0){
  778. more = w_size;
  779. }
  780. else if(more==-1) {
  781. // Very unlikely, but possible on 16 bit machine if strstart == 0
  782. // and lookahead == 1 (input done one byte at time)
  783. more--;
  784. // If the window is almost full and there is insufficient lookahead,
  785. // move the upper half to the lower one to make room in the upper half.
  786. }
  787. else if(strstart >= w_size+ w_size-MIN_LOOKAHEAD) {
  788. System.arraycopy(window, w_size, window, 0, w_size);
  789. match_start-=w_size;
  790. strstart-=w_size; // we now have strstart >= MAX_DIST
  791. block_start-=w_size;
  792. // Slide the hash table (could be avoided with 32 bit values
  793. // at the expense of memory usage). We slide even when level == 0
  794. // to keep the hash table consistent if we switch back to level > 0
  795. // later. (Using level 0 permanently is not an optimal usage of
  796. // zlib, so we don't care about this pathological case.)
  797. n = hash_size;
  798. p=n;
  799. do {
  800. m = (head[--p]&0xffff);
  801. head[p]=(m>=w_size ? (short)(m-w_size) : 0);
  802. }
  803. while (--n != 0);
  804. n = w_size;
  805. p = n;
  806. do {
  807. m = (prev[--p]&0xffff);
  808. prev[p] = (m >= w_size ? (short)(m-w_size) : 0);
  809. // If n is not on any hash chain, prev[n] is garbage but
  810. // its value will never be used.
  811. }
  812. while (--n!=0);
  813. more += w_size;
  814. }
  815. if (strm.avail_in == 0) return;
  816. // If there was no sliding:
  817. // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  818. // more == window_size - lookahead - strstart
  819. // => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  820. // => more >= window_size - 2*WSIZE + 2
  821. // In the BIG_MEM or MMAP case (not yet supported),
  822. // window_size == input_size + MIN_LOOKAHEAD &&
  823. // strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  824. // Otherwise, window_size == 2*WSIZE so more >= 2.
  825. // If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  826. n = strm.read_buf(window, strstart + lookahead, more);
  827. lookahead += n;
  828. // Initialize the hash value now that we have some input:
  829. if(lookahead >= MIN_MATCH) {
  830. ins_h = window[strstart]&0xff;
  831. ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask;
  832. }
  833. // If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  834. // but this is not important since only literal bytes will be emitted.
  835. }
  836. while (lookahead < MIN_LOOKAHEAD && strm.avail_in != 0);
  837. }
  838. // Compress as much as possible from the input stream, return the current
  839. // block state.
  840. // This function does not perform lazy evaluation of matches and inserts
  841. // new strings in the dictionary only for unmatched strings or for short
  842. // matches. It is used only for the fast compression options.
  843. int deflate_fast(int flush){
  844. // short hash_head = 0; // head of the hash chain
  845. int hash_head = 0; // head of the hash chain
  846. boolean bflush; // set if current block must be flushed
  847. while(true){
  848. // Make sure that we always have enough lookahead, except
  849. // at the end of the input file. We need MAX_MATCH bytes
  850. // for the next match, plus MIN_MATCH bytes to insert the
  851. // string following the next match.
  852. if(lookahead < MIN_LOOKAHEAD){
  853. fill_window();
  854. if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH){
  855. return NeedMore;
  856. }
  857. if(lookahead == 0) break; // flush the current block
  858. }
  859. // Insert the string window[strstart .. strstart+2] in the
  860. // dictionary, and set hash_head to the head of the hash chain:
  861. if(lookahead >= MIN_MATCH){
  862. ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
  863. // prev[strstart&w_mask]=hash_head=head[ins_h];
  864. hash_head=(head[ins_h]&0xffff);
  865. prev[strstart&w_mask]=head[ins_h];
  866. head[ins_h]=(short)strstart;
  867. }
  868. // Find the longest match, discarding those <= prev_length.
  869. // At this point we have always match_length < MIN_MATCH
  870. if(hash_head!=0L &&
  871. ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD
  872. ){
  873. // To simplify the code, we prevent matches with the string
  874. // of window index 0 (in particular we have to avoid a match
  875. // of the string with itself at the start of the input file).
  876. if(strategy != Z_HUFFMAN_ONLY){
  877. match_length=longest_match (hash_head);
  878. }
  879. // longest_match() sets match_start
  880. }
  881. if(match_length>=MIN_MATCH){
  882. // check_match(strstart, match_start, match_length);
  883. bflush=_tr_tally(strstart-match_start, match_length-MIN_MATCH);
  884. lookahead -= match_length;
  885. // Insert new strings in the hash table only if the match length
  886. // is not too large. This saves time but degrades compression.
  887. if(match_length <= max_lazy_match &&
  888. lookahead >= MIN_MATCH) {
  889. match_length--; // string at strstart already in hash table
  890. do{
  891. strstart++;
  892. ins_h=((ins_h<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
  893. // prev[strstart&w_mask]=hash_head=head[ins_h];
  894. hash_head=(head[ins_h]&0xffff);
  895. prev[strstart&w_mask]=head[ins_h];
  896. head[ins_h]=(short)strstart;
  897. // strstart never exceeds WSIZE-MAX_MATCH, so there are
  898. // always MIN_MATCH bytes ahead.
  899. }
  900. while (--match_length != 0);
  901. strstart++;
  902. }
  903. else{
  904. strstart += match_length;
  905. match_length = 0;
  906. ins_h = window[strstart]&0xff;
  907. ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask;
  908. // If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  909. // matter since it will be recomputed at next deflate call.
  910. }
  911. }
  912. else {
  913. // No match, output a literal byte
  914. bflush=_tr_tally(0, window[strstart]&0xff);
  915. lookahead--;
  916. strstart++;
  917. }
  918. if (bflush){
  919. flush_block_only(false);
  920. if(strm.avail_out==0) return NeedMore;
  921. }
  922. }
  923. flush_block_only(flush == Z_FINISH);
  924. if(strm.avail_out==0){
  925. if(flush == Z_FINISH) return FinishStarted;
  926. else return NeedMore;
  927. }
  928. return flush==Z_FINISH ? FinishDone : BlockDone;
  929. }
  930. // Same as above, but achieves better compression. We use a lazy
  931. // evaluation for matches: a match is finally adopted only if there is
  932. // no better match at the next window position.
  933. int deflate_slow(int flush){
  934. // short hash_head = 0; // head of hash chain
  935. int hash_head = 0; // head of hash chain
  936. boolean bflush; // set if current block must be flushed
  937. // Process the input block.
  938. while(true){
  939. // Make sure that we always have enough lookahead, except
  940. // at the end of the input file. We need MAX_MATCH bytes
  941. // for the next match, plus MIN_MATCH bytes to insert the
  942. // string following the next match.
  943. if (lookahead < MIN_LOOKAHEAD) {
  944. fill_window();
  945. if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  946. return NeedMore;
  947. }
  948. if(lookahead == 0) break; // flush the current block
  949. }
  950. // Insert the string window[strstart .. strstart+2] in the
  951. // dictionary, and set hash_head to the head of the hash chain:
  952. if(lookahead >= MIN_MATCH) {
  953. ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff)) & hash_mask;
  954. // prev[strstart&w_mask]=hash_head=head[ins_h];
  955. hash_head=(head[ins_h]&0xffff);
  956. prev[strstart&w_mask]=head[ins_h];
  957. head[ins_h]=(short)strstart;
  958. }
  959. // Find the longest match, discarding those <= prev_length.
  960. prev_length = match_length; prev_match = match_start;
  961. match_length = MIN_MATCH-1;
  962. if (hash_head != 0 && prev_length < max_lazy_match &&
  963. ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD
  964. ){
  965. // To simplify the code, we prevent matches with the string
  966. // of window index 0 (in particular we have to avoid a match
  967. // of the string with itself at the start of the input file).
  968. if(strategy != Z_HUFFMAN_ONLY) {
  969. match_length = longest_match(hash_head);
  970. }
  971. // longest_match() sets match_start
  972. if (match_length <= 5 && (strategy == Z_FILTERED ||
  973. (match_length == MIN_MATCH &&
  974. strstart - match_start > 4096))) {
  975. // If prev_match is also MIN_MATCH, match_start is garbage
  976. // but we will ignore the current match anyway.
  977. match_length = MIN_MATCH-1;
  978. }
  979. }
  980. // If there was a match at the previous step and the current
  981. // match is not better, output the previous match:
  982. if(prev_length >= MIN_MATCH && match_length <= prev_length) {
  983. int max_insert = strstart + lookahead - MIN_MATCH;
  984. // Do not insert strings in hash table beyond this.
  985. // check_match(strstart-1, prev_match, prev_length);
  986. bflush=_tr_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
  987. // Insert in hash table all strings up to the end of the match.
  988. // strstart-1 and strstart are already inserted. If there is not
  989. // enough lookahead, the last two strings are not inserted in
  990. // the hash table.
  991. lookahead -= prev_length-1;
  992. prev_length -= 2;
  993. do{
  994. if(++strstart <= max_insert) {
  995. ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
  996. //prev[strstart&w_mask]=hash_head=head[ins_h];
  997. hash_head=(head[ins_h]&0xffff);
  998. prev[strstart&w_mask]=head[ins_h];
  999. head[ins_h]=(short)strstart;
  1000. }
  1001. }
  1002. while(--prev_length != 0);
  1003. match_available = 0;
  1004. match_length = MIN_MATCH-1;
  1005. strstart++;
  1006. if (bflush){
  1007. flush_block_only(false);
  1008. if(strm.avail_out==0) return NeedMore;
  1009. }
  1010. } else if (match_available!=0) {
  1011. // If there was no match at the previous position, output a
  1012. // single literal. If there was a match but the current match
  1013. // is longer, truncate the previous match to a single literal.
  1014. bflush=_tr_tally(0, window[strstart-1]&0xff);
  1015. if (bflush) {
  1016. flush_block_only(false);
  1017. }
  1018. strstart++;
  1019. lookahead--;
  1020. if(strm.avail_out == 0) return NeedMore;
  1021. } else {
  1022. // There is no previous match to compare with, wait for
  1023. // the next step to decide.
  1024. match_available = 1;
  1025. strstart++;
  1026. lookahead--;
  1027. }
  1028. }
  1029. if(match_available!=0) {
  1030. bflush=_tr_tally(0, window[strstart-1]&0xff);
  1031. match_available = 0;
  1032. }
  1033. flush_block_only(flush == Z_FINISH);
  1034. if(strm.avail_out==0){
  1035. if(flush == Z_FINISH) return FinishStarted;
  1036. else return NeedMore;
  1037. }
  1038. return flush == Z_FINISH ? FinishDone : BlockDone;
  1039. }
  1040. int longest_match(int cur_match){
  1041. int chain_length = max_chain_length; // max hash chain length
  1042. int scan = strstart; // current string
  1043. int match; // matched string
  1044. int len; // length of current match
  1045. int best_len = prev_length; // best match length so far
  1046. int limit = strstart>(w_size-MIN_LOOKAHEAD) ?
  1047. strstart-(w_size-MIN_LOOKAHEAD) : 0;
  1048. int nice_match=this.nice_match;
  1049. // Stop when cur_match becomes <= limit. To simplify the code,
  1050. // we prevent matches with the string of window index 0.
  1051. int wmask = w_mask;
  1052. int strend = strstart + MAX_MATCH;
  1053. byte scan_end1 = window[scan+best_len-1];
  1054. byte scan_end = window[scan+best_len];
  1055. // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1056. // It is easy to get rid of this optimization if necessary.
  1057. // Do not waste too much time if we already have a good match:
  1058. if (prev_length >= good_match) {
  1059. chain_length >>= 2;
  1060. }
  1061. // Do not look for matches beyond the end of the input. This is necessary
  1062. // to make deflate deterministic.
  1063. if (nice_match > lookahead) nice_match = lookahead;
  1064. do {
  1065. match = cur_match;
  1066. // Skip to next match if the match length cannot increase
  1067. // or if the match length is less than 2:
  1068. if (window[match+best_len] != scan_end ||
  1069. window[match+best_len-1] != scan_end1 ||
  1070. window[match] != window[scan] ||
  1071. window[++match] != window[scan+1]) continue;
  1072. // The check at best_len-1 can be removed because it will be made
  1073. // again later. (This heuristic is not always a win.)
  1074. // It is not necessary to compare scan[2] and match[2] since they
  1075. // are always equal when the other bytes match, given that
  1076. // the hash keys are equal and that HASH_BITS >= 8.
  1077. scan += 2; match++;
  1078. // We check for insufficient lookahead only every 8th comparison;
  1079. // the 256th check will be made at strstart+258.
  1080. do {
  1081. } while (window[++scan] == window[++match] &&
  1082. window[++scan] == window[++match] &&
  1083. window[++scan] == window[++match] &&
  1084. window[++scan] == window[++match] &&
  1085. window[++scan] == window[++match] &&
  1086. window[++scan] == window[++match] &&
  1087. window[++scan] == window[++match] &&
  1088. window[++scan] == window[++match] &&
  1089. scan < strend);
  1090. len = MAX_MATCH - (int)(strend - scan);
  1091. scan = strend - MAX_MATCH;
  1092. if(len>best_len) {
  1093. match_start = cur_match;
  1094. best_len = len;
  1095. if (len >= nice_match) break;
  1096. scan_end1 = window[scan+best_len-1];
  1097. scan_end = window[scan+best_len];
  1098. }
  1099. } while ((cur_match = (prev[cur_match & wmask]&0xffff)) > limit
  1100. && --chain_length != 0);
  1101. if (best_len <= lookahead) return best_len;
  1102. return lookahead;
  1103. }
  1104. int deflateInit(ZStream strm, int level, int bits){
  1105. return deflateInit2(strm, level, Z_DEFLATED, bits, DEF_MEM_LEVEL,
  1106. Z_DEFAULT_STRATEGY);
  1107. }
  1108. int deflateInit(ZStream strm, int level){
  1109. return deflateInit(strm, level, MAX_WBITS);
  1110. }
  1111. int deflateInit2(ZStream strm, int level, int method, int windowBits,
  1112. int memLevel, int strategy){
  1113. int noheader = 0;
  1114. // byte[] my_version=ZLIB_VERSION;
  1115. //
  1116. // if (version == null || version[0] != my_version[0]
  1117. // || stream_size != sizeof(z_stream)) {
  1118. // return Z_VERSION_ERROR;
  1119. // }
  1120. strm.msg = null;
  1121. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  1122. if (windowBits < 0) { // undocumented feature: suppress zlib header
  1123. noheader = 1;
  1124. windowBits = -windowBits;
  1125. }
  1126. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL ||
  1127. method != Z_DEFLATED ||
  1128. windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
  1129. strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  1130. return Z_STREAM_ERROR;
  1131. }
  1132. strm.dstate = (Deflate)this;
  1133. this.noheader = noheader;
  1134. w_bits = windowBits;
  1135. w_size = 1 << w_bits;
  1136. w_mask = w_size - 1;
  1137. hash_bits = memLevel + 7;
  1138. hash_size = 1 << hash_bits;
  1139. hash_mask = hash_size - 1;
  1140. hash_shift = ((hash_bits+MIN_MATCH-1)/MIN_MATCH);
  1141. window = new byte[w_size*2];
  1142. prev = new short[w_size];
  1143. head = new short[hash_size];
  1144. lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
  1145. // We overlay pending_buf and d_buf+l_buf. This works since the average
  1146. // output size for (length,distance) codes is <= 24 bits.
  1147. pending_buf = new byte[lit_bufsize*4];
  1148. pending_buf_size = lit_bufsize*4;
  1149. d_buf = lit_bufsize/2;
  1150. l_buf = (1+2)*lit_bufsize;
  1151. this.level = level;
  1152. //System.out.println("level="+level);
  1153. this.strategy = strategy;
  1154. this.method = (byte)method;
  1155. return deflateReset(strm);
  1156. }
  1157. int deflateReset(ZStream strm){
  1158. strm.total_in = strm.total_out = 0;
  1159. strm.msg = null; //
  1160. strm.data_type = Z_UNKNOWN;
  1161. pending = 0;
  1162. pending_out = 0;
  1163. if(noheader < 0) {
  1164. noheader = 0; // was set to -1 by deflate(..., Z_FINISH);
  1165. }
  1166. status = (noheader!=0) ? BUSY_STATE : INIT_STATE;
  1167. strm.adler=strm._adler.adler32(0, null, 0, 0);
  1168. last_flush = Z_NO_FLUSH;
  1169. tr_init();
  1170. lm_init();
  1171. return Z_OK;
  1172. }
  1173. int deflateEnd(){
  1174. if(status!=INIT_STATE && status!=BUSY_STATE && status!=FINISH_STATE){
  1175. return Z_STREAM_ERROR;
  1176. }
  1177. // Deallocate in reverse order of allocations:
  1178. pending_buf=null;
  1179. head=null;
  1180. prev=null;
  1181. window=null;
  1182. // free
  1183. // dstate=null;
  1184. return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  1185. }
  1186. int deflateParams(ZStream strm, int _level, int _strategy){
  1187. int err=Z_OK;
  1188. if(_level == Z_DEFAULT_COMPRESSION){
  1189. _level = 6;
  1190. }
  1191. if(_level < 0 || _level > 9 ||
  1192. _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
  1193. return Z_STREAM_ERROR;
  1194. }
  1195. if(config_table[level].func!=config_table[_level].func &&
  1196. strm.total_in != 0) {
  1197. // Flush the last buffer:
  1198. err = strm.deflate(Z_PARTIAL_FLUSH);
  1199. }
  1200. if(level != _level) {
  1201. level = _level;
  1202. max_lazy_match = config_table[level].max_lazy;
  1203. good_match = config_table[level].good_length;
  1204. nice_match = config_table[level].nice_length;
  1205. max_chain_length = config_table[level].max_chain;
  1206. }
  1207. strategy = _strategy;
  1208. return err;
  1209. }
  1210. int deflateSetDictionary (ZStream strm, byte[] dictionary, int dictLength){
  1211. int length = dictLength;
  1212. int index=0;
  1213. if(dictionary == null || status != INIT_STATE)
  1214. return Z_STREAM_ERROR;
  1215. strm.adler=strm._adler.adler32(strm.adler, dictionary, 0, dictLength);
  1216. if(length < MIN_MATCH) return Z_OK;
  1217. if(length > w_size-MIN_LOOKAHEAD){
  1218. length = w_size-MIN_LOOKAHEAD;
  1219. index=dictLength-length; // use the tail of the dictionary
  1220. }
  1221. System.arraycopy(dictionary, index, window, 0, length);
  1222. strstart = length;
  1223. block_start = length;
  1224. // Insert all strings in the hash table (except for the last two bytes).
  1225. // s->lookahead stays null, so s->ins_h will be recomputed at the next
  1226. // call of fill_window.
  1227. ins_h = window[0]&0xff;
  1228. ins_h=(((ins_h)<<hash_shift)^(window[1]&0xff))&hash_mask;
  1229. for(int n=0; n<=length-MIN_MATCH; n++){
  1230. ins_h=(((ins_h)<<hash_shift)^(window[(n)+(MIN_MATCH-1)]&0xff))&hash_mask;
  1231. prev[n&w_mask]=head[ins_h];
  1232. head[ins_h]=(short)n;
  1233. }
  1234. return Z_OK;
  1235. }
  1236. int deflate(ZStream strm, int flush){