comparison src/com/jcraft/jzlib/Deflate.java @ 357:46c2115ae1c8

update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
author Carl Byington <carl@five-ten-sg.com>
date Fri, 01 Aug 2014 13:34:58 -0700
parents 0ce5cc452d02
children
comparison
equal deleted inserted replaced
356:5e91b559b5fe 357:46c2115ae1c8
1 /* -*-mode:java; c-basic-offset:2; -*- */ 1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /* 2 /*
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. 3 Copyright (c) 2000-2011 ymnk, JCraft,Inc. All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met: 6 modification, are permitted provided that the following conditions are met:
7 7
8 1. Redistributions of source code must retain the above copyright notice, 8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
10 10
11 2. Redistributions in binary form must reproduce the above copyright 11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in 12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution. 13 the documentation and/or other materials provided with the distribution.
14 14
15 3. The names of the authors may not be used to endorse or promote products 15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission. 16 derived from this software without specific prior written permission.
17 17
32 * and contributors of zlib. 32 * and contributors of zlib.
33 */ 33 */
34 34
35 package com.jcraft.jzlib; 35 package com.jcraft.jzlib;
36 36
37 public 37 public
38 final class Deflate { 38 final class Deflate implements Cloneable {
39 39
40 static final private int MAX_MEM_LEVEL = 9; 40 static final private int MAX_MEM_LEVEL=9;
41 41
42 static final private int Z_DEFAULT_COMPRESSION = -1; 42 static final private int Z_DEFAULT_COMPRESSION=-1;
43 43
44 static final private int MAX_WBITS = 15; // 32K LZ77 window 44 static final private int MAX_WBITS=15; // 32K LZ77 window
45 static final private int DEF_MEM_LEVEL = 8; 45 static final private int DEF_MEM_LEVEL=8;
46 46
47 static class Config { 47 static class Config{
48 int good_length; // reduce lazy search above this match length 48 int good_length; // reduce lazy search above this match length
49 int max_lazy; // do not perform lazy search above this match length 49 int max_lazy; // do not perform lazy search above this match length
50 int nice_length; // quit search above this match length 50 int nice_length; // quit search above this match length
51 int max_chain; 51 int max_chain;
52 int func; 52 int func;
53 Config(int good_length, int max_lazy, 53 Config(int good_length, int max_lazy,
54 int nice_length, int max_chain, int func) { 54 int nice_length, int max_chain, int func){
55 this.good_length = good_length; 55 this.good_length=good_length;
56 this.max_lazy = max_lazy; 56 this.max_lazy=max_lazy;
57 this.nice_length = nice_length; 57 this.nice_length=nice_length;
58 this.max_chain = max_chain; 58 this.max_chain=max_chain;
59 this.func = func; 59 this.func=func;
60 } 60 }
61 } 61 }
62 62
63 static final private int STORED = 0; 63 static final private int STORED=0;
64 static final private int FAST = 1; 64 static final private int FAST=1;
65 static final private int SLOW = 2; 65 static final private int SLOW=2;
66 static final private Config[] config_table; 66 static final private Config[] config_table;
67 static { 67 static{
68 config_table = new Config[10]; 68 config_table=new Config[10];
69 // good lazy nice chain 69 // good lazy nice chain
70 config_table[0] = new Config(0, 0, 0, 0, STORED); 70 config_table[0]=new Config(0, 0, 0, 0, STORED);
71 config_table[1] = new Config(4, 4, 8, 4, FAST); 71 config_table[1]=new Config(4, 4, 8, 4, FAST);
72 config_table[2] = new Config(4, 5, 16, 8, FAST); 72 config_table[2]=new Config(4, 5, 16, 8, FAST);
73 config_table[3] = new Config(4, 6, 32, 32, FAST); 73 config_table[3]=new Config(4, 6, 32, 32, FAST);
74 config_table[4] = new Config(4, 4, 16, 16, SLOW); 74
75 config_table[5] = new Config(8, 16, 32, 32, SLOW); 75 config_table[4]=new Config(4, 4, 16, 16, SLOW);
76 config_table[6] = new Config(8, 16, 128, 128, SLOW); 76 config_table[5]=new Config(8, 16, 32, 32, SLOW);
77 config_table[7] = new Config(8, 32, 128, 256, SLOW); 77 config_table[6]=new Config(8, 16, 128, 128, SLOW);
78 config_table[8] = new Config(32, 128, 258, 1024, SLOW); 78 config_table[7]=new Config(8, 32, 128, 256, SLOW);
79 config_table[9] = new Config(32, 258, 258, 4096, SLOW); 79 config_table[8]=new Config(32, 128, 258, 1024, SLOW);
80 } 80 config_table[9]=new Config(32, 258, 258, 4096, SLOW);
81 81 }
82 static final private String[] z_errmsg = { 82
83 "need dictionary", // Z_NEED_DICT 2 83 static final private String[] z_errmsg = {
84 "stream end", // Z_STREAM_END 1 84 "need dictionary", // Z_NEED_DICT 2
85 "", // Z_OK 0 85 "stream end", // Z_STREAM_END 1
86 "file error", // Z_ERRNO (-1) 86 "", // Z_OK 0
87 "stream error", // Z_STREAM_ERROR (-2) 87 "file error", // Z_ERRNO (-1)
88 "data error", // Z_DATA_ERROR (-3) 88 "stream error", // Z_STREAM_ERROR (-2)
89 "insufficient memory", // Z_MEM_ERROR (-4) 89 "data error", // Z_DATA_ERROR (-3)
90 "buffer error", // Z_BUF_ERROR (-5) 90 "insufficient memory", // Z_MEM_ERROR (-4)
91 "incompatible version",// Z_VERSION_ERROR (-6) 91 "buffer error", // Z_BUF_ERROR (-5)
92 "" 92 "incompatible version",// Z_VERSION_ERROR (-6)
93 }; 93 ""
94 94 };
95 // block not completed, need more input or more output 95
96 static final private int NeedMore = 0; 96 // block not completed, need more input or more output
97 97 static final private int NeedMore=0;
98 // block flush performed 98
99 static final private int BlockDone = 1; 99 // block flush performed
100 100 static final private int BlockDone=1;
101 // finish started, need only more output at next deflate 101
102 static final private int FinishStarted = 2; 102 // finish started, need only more output at next deflate
103 103 static final private int FinishStarted=2;
104 // finish done, accept no more input or output 104
105 static final private int FinishDone = 3; 105 // finish done, accept no more input or output
106 106 static final private int FinishDone=3;
107 // preset dictionary flag in zlib header 107
108 static final private int PRESET_DICT = 0x20; 108 // preset dictionary flag in zlib header
109 109 static final private int PRESET_DICT=0x20;
110 static final private int Z_FILTERED = 1; 110
111 static final private int Z_HUFFMAN_ONLY = 2; 111 static final private int Z_FILTERED=1;
112 static final private int Z_DEFAULT_STRATEGY = 0; 112 static final private int Z_HUFFMAN_ONLY=2;
113 113 static final private int Z_DEFAULT_STRATEGY=0;
114 static final private int Z_NO_FLUSH = 0; 114
115 static final private int Z_PARTIAL_FLUSH = 1; 115 static final private int Z_NO_FLUSH=0;
116 static final private int Z_SYNC_FLUSH = 2; 116 static final private int Z_PARTIAL_FLUSH=1;
117 static final private int Z_FULL_FLUSH = 3; 117 static final private int Z_SYNC_FLUSH=2;
118 static final private int Z_FINISH = 4; 118 static final private int Z_FULL_FLUSH=3;
119 119 static final private int Z_FINISH=4;
120 static final private int Z_OK = 0; 120
121 static final private int Z_STREAM_END = 1; 121 static final private int Z_OK=0;
122 static final private int Z_NEED_DICT = 2; 122 static final private int Z_STREAM_END=1;
123 static final private int Z_ERRNO = -1; 123 static final private int Z_NEED_DICT=2;
124 static final private int Z_STREAM_ERROR = -2; 124 static final private int Z_ERRNO=-1;
125 static final private int Z_DATA_ERROR = -3; 125 static final private int Z_STREAM_ERROR=-2;
126 static final private int Z_MEM_ERROR = -4; 126 static final private int Z_DATA_ERROR=-3;
127 static final private int Z_BUF_ERROR = -5; 127 static final private int Z_MEM_ERROR=-4;
128 static final private int Z_VERSION_ERROR = -6; 128 static final private int Z_BUF_ERROR=-5;
129 129 static final private int Z_VERSION_ERROR=-6;
130 static final private int INIT_STATE = 42; 130
131 static final private int BUSY_STATE = 113; 131 static final private int INIT_STATE=42;
132 static final private int FINISH_STATE = 666; 132 static final private int BUSY_STATE=113;
133 133 static final private int FINISH_STATE=666;
134 // The deflate compression method 134
135 static final private int Z_DEFLATED = 8; 135 // The deflate compression method
136 136 static final private int Z_DEFLATED=8;
137 static final private int STORED_BLOCK = 0; 137
138 static final private int STATIC_TREES = 1; 138 static final private int STORED_BLOCK=0;
139 static final private int DYN_TREES = 2; 139 static final private int STATIC_TREES=1;
140 140 static final private int DYN_TREES=2;
141 // The three kinds of block type 141
142 static final private int Z_BINARY = 0; 142 // The three kinds of block type
143 static final private int Z_ASCII = 1; 143 static final private int Z_BINARY=0;
144 static final private int Z_UNKNOWN = 2; 144 static final private int Z_ASCII=1;
145 145 static final private int Z_UNKNOWN=2;
146 static final private int Buf_size = 8 * 2; 146
147 147 static final private int Buf_size=8*2;
148 // repeat previous bit length 3-6 times (2 bits of repeat count) 148
149 static final private int REP_3_6 = 16; 149 // repeat previous bit length 3-6 times (2 bits of repeat count)
150 150 static final private int REP_3_6=16;
151 // repeat a zero length 3-10 times (3 bits of repeat count) 151
152 static final private int REPZ_3_10 = 17; 152 // repeat a zero length 3-10 times (3 bits of repeat count)
153 153 static final private int REPZ_3_10=17;
154 // repeat a zero length 11-138 times (7 bits of repeat count) 154
155 static final private int REPZ_11_138 = 18; 155 // repeat a zero length 11-138 times (7 bits of repeat count)
156 156 static final private int REPZ_11_138=18;
157 static final private int MIN_MATCH = 3; 157
158 static final private int MAX_MATCH = 258; 158 static final private int MIN_MATCH=3;
159 static final private int MIN_LOOKAHEAD = (MAX_MATCH + MIN_MATCH + 1); 159 static final private int MAX_MATCH=258;
160 160 static final private int MIN_LOOKAHEAD=(MAX_MATCH+MIN_MATCH+1);
161 static final private int MAX_BITS = 15; 161
162 static final private int D_CODES = 30; 162 static final private int MAX_BITS=15;
163 static final private int BL_CODES = 19; 163 static final private int D_CODES=30;
164 static final private int LENGTH_CODES = 29; 164 static final private int BL_CODES=19;
165 static final private int LITERALS = 256; 165 static final private int LENGTH_CODES=29;
166 static final private int L_CODES = (LITERALS + 1 + LENGTH_CODES); 166 static final private int LITERALS=256;
167 static final private int HEAP_SIZE = (2 * L_CODES + 1); 167 static final private int L_CODES=(LITERALS+1+LENGTH_CODES);
168 168 static final private int HEAP_SIZE=(2*L_CODES+1);
169 static final private int END_BLOCK = 256; 169
170 170 static final private int END_BLOCK=256;
171 ZStream strm; // pointer back to this zlib stream 171
172 int status; // as the name implies 172 ZStream strm; // pointer back to this zlib stream
173 byte[] pending_buf; // output still pending 173 int status; // as the name implies
174 int pending_buf_size; // size of pending_buf 174 byte[] pending_buf; // output still pending
175 int pending_out; // next pending byte to output to the stream 175 int pending_buf_size; // size of pending_buf
176 int pending; // nb of bytes in the pending buffer 176 int pending_out; // next pending byte to output to the stream
177 int noheader; // suppress zlib header and adler32 177 int pending; // nb of bytes in the pending buffer
178 byte data_type; // UNKNOWN, BINARY or ASCII 178 int wrap = 1;
179 byte method; // STORED (for zip only) or DEFLATED 179 byte data_type; // UNKNOWN, BINARY or ASCII
180 int last_flush; // value of flush param for previous deflate call 180 byte method; // STORED (for zip only) or DEFLATED
181 181 int last_flush; // value of flush param for previous deflate call
182 int w_size; // LZ77 window size (32K by default) 182
183 int w_bits; // log2(w_size) (8..16) 183 int w_size; // LZ77 window size (32K by default)
184 int w_mask; // w_size - 1 184 int w_bits; // log2(w_size) (8..16)
185 185 int w_mask; // w_size - 1
186 byte[] window; 186
187 // Sliding window. Input bytes are read into the second half of the window, 187 byte[] window;
188 // and move to the first half later to keep a dictionary of at least wSize 188 // Sliding window. Input bytes are read into the second half of the window,
189 // bytes. With this organization, matches are limited to a distance of 189 // and move to the first half later to keep a dictionary of at least wSize
190 // wSize-MAX_MATCH bytes, but this ensures that IO is always 190 // bytes. With this organization, matches are limited to a distance of
191 // performed with a length multiple of the block size. Also, it limits 191 // wSize-MAX_MATCH bytes, but this ensures that IO is always
192 // the window size to 64K, which is quite useful on MSDOS. 192 // performed with a length multiple of the block size. Also, it limits
193 // To do: use the user input buffer as sliding window. 193 // the window size to 64K, which is quite useful on MSDOS.
194 194 // To do: use the user input buffer as sliding window.
195 int window_size; 195
196 // Actual size of window: 2*wSize, except when the user input buffer 196 int window_size;
197 // is directly used as sliding window. 197 // Actual size of window: 2*wSize, except when the user input buffer
198 198 // is directly used as sliding window.
199 short[] prev; 199
200 // Link to older string with same hash index. To limit the size of this 200 short[] prev;
201 // array to 64K, this link is maintained only for the last 32K strings. 201 // Link to older string with same hash index. To limit the size of this
202 // An index in this array is thus a window index modulo 32K. 202 // array to 64K, this link is maintained only for the last 32K strings.
203 203 // An index in this array is thus a window index modulo 32K.
204 short[] head; // Heads of the hash chains or NIL. 204
205 205 short[] head; // Heads of the hash chains or NIL.
206 int ins_h; // hash index of string to be inserted 206
207 int hash_size; // number of elements in hash table 207 int ins_h; // hash index of string to be inserted
208 int hash_bits; // log2(hash_size) 208 int hash_size; // number of elements in hash table
209 int hash_mask; // hash_size-1 209 int hash_bits; // log2(hash_size)
210 210 int hash_mask; // hash_size-1
211 // Number of bits by which ins_h must be shifted at each input 211
212 // step. It must be such that after MIN_MATCH steps, the oldest 212 // Number of bits by which ins_h must be shifted at each input
213 // byte no longer takes part in the hash key, that is: 213 // step. It must be such that after MIN_MATCH steps, the oldest
214 // hash_shift * MIN_MATCH >= hash_bits 214 // byte no longer takes part in the hash key, that is:
215 int hash_shift; 215 // hash_shift * MIN_MATCH >= hash_bits
216 216 int hash_shift;
217 // Window position at the beginning of the current output block. Gets 217
218 // negative when the window is moved backwards. 218 // Window position at the beginning of the current output block. Gets
219 219 // negative when the window is moved backwards.
220 int block_start; 220
221 221 int block_start;
222 int match_length; // length of best match 222
223 int prev_match; // previous match 223 int match_length; // length of best match
224 int match_available; // set if previous match exists 224 int prev_match; // previous match
225 int strstart; // start of string to insert 225 int match_available; // set if previous match exists
226 int match_start; // start of matching string 226 int strstart; // start of string to insert
227 int lookahead; // number of valid bytes ahead in window 227 int match_start; // start of matching string
228 228 int lookahead; // number of valid bytes ahead in window
229 // Length of the best match at previous step. Matches not greater than this 229
230 // are discarded. This is used in the lazy match evaluation. 230 // Length of the best match at previous step. Matches not greater than this
231 int prev_length; 231 // are discarded. This is used in the lazy match evaluation.
232 232 int prev_length;
233 // To speed up deflation, hash chains are never searched beyond this 233
234 // length. A higher limit improves compression ratio but degrades the speed. 234 // To speed up deflation, hash chains are never searched beyond this
235 int max_chain_length; 235 // length. A higher limit improves compression ratio but degrades the speed.
236 236 int max_chain_length;
237 // Attempt to find a better match only when the current match is strictly 237
238 // smaller than this value. This mechanism is used only for compression 238 // Attempt to find a better match only when the current match is strictly
239 // levels >= 4. 239 // smaller than this value. This mechanism is used only for compression
240 int max_lazy_match; 240 // levels >= 4.
241 241 int max_lazy_match;
242 // Insert new strings in the hash table only if the match length is not 242
243 // greater than this length. This saves time but degrades compression. 243 // Insert new strings in the hash table only if the match length is not
244 // max_insert_length is used only for compression levels <= 3. 244 // greater than this length. This saves time but degrades compression.
245 245 // max_insert_length is used only for compression levels <= 3.
246 int level; // compression level (1..9) 246
247 int strategy; // favor or force Huffman coding 247 int level; // compression level (1..9)
248 248 int strategy; // favor or force Huffman coding
249 // Use a faster search when the previous match is longer than this 249
250 int good_match; 250 // Use a faster search when the previous match is longer than this
251 251 int good_match;
252 // Stop searching when current match exceeds this 252
253 int nice_match; 253 // Stop searching when current match exceeds this
254 254 int nice_match;
255 short[] dyn_ltree; // literal and length tree 255
256 short[] dyn_dtree; // distance tree 256 short[] dyn_ltree; // literal and length tree
257 short[] bl_tree; // Huffman tree for bit lengths 257 short[] dyn_dtree; // distance tree
258 258 short[] bl_tree; // Huffman tree for bit lengths
259 Tree l_desc = new Tree(); // desc for literal tree 259
260 Tree d_desc = new Tree(); // desc for distance tree 260 Tree l_desc=new Tree(); // desc for literal tree
261 Tree bl_desc = new Tree(); // desc for bit length tree 261 Tree d_desc=new Tree(); // desc for distance tree
262 262 Tree bl_desc=new Tree(); // desc for bit length tree
263 // number of codes at each bit length for an optimal tree 263
264 short[] bl_count = new short[MAX_BITS + 1]; 264 // number of codes at each bit length for an optimal tree
265 265 short[] bl_count=new short[MAX_BITS+1];
266 // heap used to build the Huffman trees 266 // working area to be used in Tree#gen_codes()
267 int[] heap = new int[2 * L_CODES + 1]; 267 short[] next_code=new short[MAX_BITS+1];
268 268
269 int heap_len; // number of elements in the heap 269 // heap used to build the Huffman trees
270 int heap_max; // element of largest frequency 270 int[] heap=new int[2*L_CODES+1];
271 // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 271
272 // The same heap array is used to build all trees. 272 int heap_len; // number of elements in the heap
273 273 int heap_max; // element of largest frequency
274 // Depth of each subtree used as tie breaker for trees of equal frequency 274 // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
275 byte[] depth = new byte[2 * L_CODES + 1]; 275 // The same heap array is used to build all trees.
276 276
277 int l_buf; // index for literals or lengths */ 277 // Depth of each subtree used as tie breaker for trees of equal frequency
278 278 byte[] depth=new byte[2*L_CODES+1];
279 // Size of match buffer for literals/lengths. There are 4 reasons for 279
280 // limiting lit_bufsize to 64K: 280 byte[] l_buf; // index for literals or lengths */
281 // - frequencies can be kept in 16 bit counters 281
282 // - if compression is not successful for the first block, all input 282 // Size of match buffer for literals/lengths. There are 4 reasons for
283 // data is still in the window so we can still emit a stored block even 283 // limiting lit_bufsize to 64K:
284 // when input comes from standard input. (This can also be done for 284 // - frequencies can be kept in 16 bit counters
285 // all blocks if lit_bufsize is not greater than 32K.) 285 // - if compression is not successful for the first block, all input
286 // - if compression is not successful for a file smaller than 64K, we can 286 // data is still in the window so we can still emit a stored block even
287 // even emit a stored file instead of a stored block (saving 5 bytes). 287 // when input comes from standard input. (This can also be done for
288 // This is applicable only for zip (not gzip or zlib). 288 // all blocks if lit_bufsize is not greater than 32K.)
289 // - creating new Huffman trees less frequently may not provide fast 289 // - if compression is not successful for a file smaller than 64K, we can
290 // adaptation to changes in the input data statistics. (Take for 290 // even emit a stored file instead of a stored block (saving 5 bytes).
291 // example a binary file with poorly compressible code followed by 291 // This is applicable only for zip (not gzip or zlib).
292 // a highly compressible string table.) Smaller buffer sizes give 292 // - creating new Huffman trees less frequently may not provide fast
293 // fast adaptation but have of course the overhead of transmitting 293 // adaptation to changes in the input data statistics. (Take for
294 // trees more frequently. 294 // example a binary file with poorly compressible code followed by
295 // - I can't count above 4 295 // a highly compressible string table.) Smaller buffer sizes give
296 int lit_bufsize; 296 // fast adaptation but have of course the overhead of transmitting
297 297 // trees more frequently.
298 int last_lit; // running index in l_buf 298 // - I can't count above 4
299 299 int lit_bufsize;
300 // Buffer for distances. To simplify the code, d_buf and l_buf have 300
301 // the same number of elements. To use different lengths, an extra flag 301 int last_lit; // running index in l_buf
302 // array would be necessary. 302
303 303 // Buffer for distances. To simplify the code, d_buf and l_buf have
304 int d_buf; // index of pendig_buf 304 // the same number of elements. To use different lengths, an extra flag
305 305 // array would be necessary.
306 int opt_len; // bit length of current block with optimal trees 306
307 int static_len; // bit length of current block with static trees 307 int d_buf; // index of pendig_buf
308 int matches; // number of string matches in current block 308
309 int last_eob_len; // bit length of EOB code for last block 309 int opt_len; // bit length of current block with optimal trees
310 310 int static_len; // bit length of current block with static trees
311 // Output buffer. bits are inserted starting at the bottom (least 311 int matches; // number of string matches in current block
312 // significant bits). 312 int last_eob_len; // bit length of EOB code for last block
313 short bi_buf; 313
314 314 // Output buffer. bits are inserted starting at the bottom (least
315 // Number of valid bits in bi_buf. All bits above the last valid bit 315 // significant bits).
316 // are always zero. 316 short bi_buf;
317 int bi_valid; 317
318 318 // Number of valid bits in bi_buf. All bits above the last valid bit
319 Deflate() { 319 // are always zero.
320 dyn_ltree = new short[HEAP_SIZE * 2]; 320 int bi_valid;
321 dyn_dtree = new short[(2 * D_CODES + 1) * 2]; // distance tree 321
322 bl_tree = new short[(2 * BL_CODES + 1) * 2]; // Huffman tree for bit lengths 322 GZIPHeader gheader = null;
323 } 323
324 324 Deflate(ZStream strm){
325 void lm_init() { 325 this.strm=strm;
326 window_size = 2 * w_size; 326 dyn_ltree=new short[HEAP_SIZE*2];
327 head[hash_size - 1] = 0; 327 dyn_dtree=new short[(2*D_CODES+1)*2]; // distance tree
328 328 bl_tree=new short[(2*BL_CODES+1)*2]; // Huffman tree for bit lengths
329 for (int i = 0; i < hash_size - 1; i++) { 329 }
330 head[i] = 0; 330
331 } 331 void lm_init() {
332 332 window_size=2*w_size;
333 // Set the default configuration parameters: 333
334 max_lazy_match = Deflate.config_table[level].max_lazy; 334 head[hash_size-1]=0;
335 good_match = Deflate.config_table[level].good_length; 335 for(int i=0; i<hash_size-1; i++){
336 nice_match = Deflate.config_table[level].nice_length; 336 head[i]=0;
337 max_chain_length = Deflate.config_table[level].max_chain; 337 }
338 strstart = 0; 338
339 block_start = 0; 339 // Set the default configuration parameters:
340 lookahead = 0; 340 max_lazy_match = Deflate.config_table[level].max_lazy;
341 match_length = prev_length = MIN_MATCH - 1; 341 good_match = Deflate.config_table[level].good_length;
342 match_available = 0; 342 nice_match = Deflate.config_table[level].nice_length;
343 ins_h = 0; 343 max_chain_length = Deflate.config_table[level].max_chain;
344 } 344
345 345 strstart = 0;
346 // Initialize the tree data structures for a new zlib stream. 346 block_start = 0;
347 void tr_init() { 347 lookahead = 0;
348 l_desc.dyn_tree = dyn_ltree; 348 match_length = prev_length = MIN_MATCH-1;
349 l_desc.stat_desc = StaticTree.static_l_desc; 349 match_available = 0;
350 d_desc.dyn_tree = dyn_dtree; 350 ins_h = 0;
351 d_desc.stat_desc = StaticTree.static_d_desc; 351 }
352 bl_desc.dyn_tree = bl_tree; 352
353 bl_desc.stat_desc = StaticTree.static_bl_desc; 353 // Initialize the tree data structures for a new zlib stream.
354 bi_buf = 0; 354 void tr_init(){
355 bi_valid = 0; 355
356 last_eob_len = 8; // enough lookahead for inflate 356 l_desc.dyn_tree = dyn_ltree;
357 // Initialize the first block of the first file: 357 l_desc.stat_desc = StaticTree.static_l_desc;
358 init_block(); 358
359 } 359 d_desc.dyn_tree = dyn_dtree;
360 360 d_desc.stat_desc = StaticTree.static_d_desc;
361 void init_block() { 361
362 // Initialize the trees. 362 bl_desc.dyn_tree = bl_tree;
363 for (int i = 0; i < L_CODES; i++) dyn_ltree[i * 2] = 0; 363 bl_desc.stat_desc = StaticTree.static_bl_desc;
364 364
365 for (int i = 0; i < D_CODES; i++) dyn_dtree[i * 2] = 0; 365 bi_buf = 0;
366 366 bi_valid = 0;
367 for (int i = 0; i < BL_CODES; i++) bl_tree[i * 2] = 0; 367 last_eob_len = 8; // enough lookahead for inflate
368 368
369 dyn_ltree[END_BLOCK * 2] = 1; 369 // Initialize the first block of the first file:
370 opt_len = static_len = 0; 370 init_block();
371 last_lit = matches = 0; 371 }
372 } 372
373 373 void init_block(){
374 // Restore the heap property by moving down the tree starting at node k, 374 // Initialize the trees.
375 // exchanging a node with the smallest of its two sons if necessary, stopping 375 for(int i = 0; i < L_CODES; i++) dyn_ltree[i*2] = 0;
376 // when the heap property is re-established (each father smaller than its 376 for(int i= 0; i < D_CODES; i++) dyn_dtree[i*2] = 0;
377 // two sons). 377 for(int i= 0; i < BL_CODES; i++) bl_tree[i*2] = 0;
378 void pqdownheap(short[] tree, // the tree to restore 378
379 int k // node to move down 379 dyn_ltree[END_BLOCK*2] = 1;
380 ) { 380 opt_len = static_len = 0;
381 int v = heap[k]; 381 last_lit = matches = 0;
382 int j = k << 1; // left son of k 382 }
383 383
384 while (j <= heap_len) { 384 // Restore the heap property by moving down the tree starting at node k,
385 // Set j to the smallest of the two sons: 385 // exchanging a node with the smallest of its two sons if necessary, stopping
386 if (j < heap_len && 386 // when the heap property is re-established (each father smaller than its
387 smaller(tree, heap[j + 1], heap[j], depth)) { 387 // two sons).
388 j++; 388 void pqdownheap(short[] tree, // the tree to restore
389 } 389 int k // node to move down
390 390 ){
391 // Exit if v is smaller than both sons 391 int v = heap[k];
392 if (smaller(tree, v, heap[j], depth)) break; 392 int j = k << 1; // left son of k
393 393 while (j <= heap_len) {
394 // Exchange v with the smallest son 394 // Set j to the smallest of the two sons:
395 heap[k] = heap[j]; k = j; 395 if (j < heap_len &&
396 // And continue down the tree, setting j to the left son of k 396 smaller(tree, heap[j+1], heap[j], depth)){
397 j <<= 1; 397 j++;
398 } 398 }
399 399 // Exit if v is smaller than both sons
400 heap[k] = v; 400 if(smaller(tree, v, heap[j], depth)) break;
401 } 401
402 402 // Exchange v with the smallest son
403 static boolean smaller(short[] tree, int n, int m, byte[] depth) { 403 heap[k]=heap[j]; k = j;
404 short tn2 = tree[n * 2]; 404 // And continue down the tree, setting j to the left son of k
405 short tm2 = tree[m * 2]; 405 j <<= 1;
406 return (tn2 < tm2 || 406 }
407 (tn2 == tm2 && depth[n] <= depth[m])); 407 heap[k] = v;
408 } 408 }
409 409
410 // Scan a literal or distance tree to determine the frequencies of the codes 410 static boolean smaller(short[] tree, int n, int m, byte[] depth){
411 // in the bit length tree. 411 short tn2=tree[n*2];
412 void scan_tree(short[] tree, // the tree to be scanned 412 short tm2=tree[m*2];
413 int max_code // and its largest code of non zero frequency 413 return (tn2<tm2 ||
414 ) { 414 (tn2==tm2 && depth[n] <= depth[m]));
415 int n; // iterates over all tree elements 415 }
416 int prevlen = -1; // last emitted length 416
417 int curlen; // length of current code 417 // Scan a literal or distance tree to determine the frequencies of the codes
418 int nextlen = tree[0 * 2 + 1]; // length of next code 418 // in the bit length tree.
419 int count = 0; // repeat count of the current code 419 void scan_tree (short[] tree,// the tree to be scanned
420 int max_count = 7; // max repeat count 420 int max_code // and its largest code of non zero frequency
421 int min_count = 4; // min repeat count 421 ){
422 422 int n; // iterates over all tree elements
423 if (nextlen == 0) { max_count = 138; min_count = 3; } 423 int prevlen = -1; // last emitted length
424 424 int curlen; // length of current code
425 tree[(max_code + 1) * 2 + 1] = (short)0xffff; // guard 425 int nextlen = tree[0*2+1]; // length of next code
426 426 int count = 0; // repeat count of the current code
427 for (n = 0; n <= max_code; n++) { 427 int max_count = 7; // max repeat count
428 curlen = nextlen; nextlen = tree[(n + 1) * 2 + 1]; 428 int min_count = 4; // min repeat count
429 429
430 if (++count < max_count && curlen == nextlen) { 430 if (nextlen == 0){ max_count = 138; min_count = 3; }
431 continue; 431 tree[(max_code+1)*2+1] = (short)0xffff; // guard
432 } 432
433 else if (count < min_count) { 433 for(n = 0; n <= max_code; n++) {
434 bl_tree[curlen * 2] += count; 434 curlen = nextlen; nextlen = tree[(n+1)*2+1];
435 } 435 if(++count < max_count && curlen == nextlen) {
436 else if (curlen != 0) { 436 continue;
437 if (curlen != prevlen) bl_tree[curlen * 2]++; 437 }
438 438 else if(count < min_count) {
439 bl_tree[REP_3_6 * 2]++; 439 bl_tree[curlen*2] += count;
440 } 440 }
441 else if (count <= 10) { 441 else if(curlen != 0) {
442 bl_tree[REPZ_3_10 * 2]++; 442 if(curlen != prevlen) bl_tree[curlen*2]++;
443 } 443 bl_tree[REP_3_6*2]++;
444 else { 444 }
445 bl_tree[REPZ_11_138 * 2]++; 445 else if(count <= 10) {
446 } 446 bl_tree[REPZ_3_10*2]++;
447 447 }
448 count = 0; prevlen = curlen; 448 else{
449 449 bl_tree[REPZ_11_138*2]++;
450 if (nextlen == 0) { 450 }
451 max_count = 138; min_count = 3; 451 count = 0; prevlen = curlen;
452 } 452 if(nextlen == 0) {
453 else if (curlen == nextlen) { 453 max_count = 138; min_count = 3;
454 max_count = 6; min_count = 3; 454 }
455 } 455 else if(curlen == nextlen) {
456 else { 456 max_count = 6; min_count = 3;
457 max_count = 7; min_count = 4; 457 }
458 } 458 else{
459 } 459 max_count = 7; min_count = 4;
460 } 460 }
461 461 }
462 // Construct the Huffman tree for the bit lengths and return the index in 462 }
463 // bl_order of the last bit length code to send. 463
464 int build_bl_tree() { 464 // Construct the Huffman tree for the bit lengths and return the index in
465 int max_blindex; // index of last bit length code of non zero freq 465 // bl_order of the last bit length code to send.
466 // Determine the bit length frequencies for literal and distance trees 466 int build_bl_tree(){
467 scan_tree(dyn_ltree, l_desc.max_code); 467 int max_blindex; // index of last bit length code of non zero freq
468 scan_tree(dyn_dtree, d_desc.max_code); 468
469 // Build the bit length tree: 469 // Determine the bit length frequencies for literal and distance trees
470 bl_desc.build_tree(this); 470 scan_tree(dyn_ltree, l_desc.max_code);
471 471 scan_tree(dyn_dtree, d_desc.max_code);
472 // opt_len now includes the length of the tree representations, except 472
473 // the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 473 // Build the bit length tree:
474 // Determine the number of bit length codes to send. The pkzip format 474 bl_desc.build_tree(this);
475 // requires that at least 4 bit length codes be sent. (appnote.txt says 475 // opt_len now includes the length of the tree representations, except
476 // 3 but the actual value used is 4.) 476 // the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
477 for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) { 477
478 if (bl_tree[Tree.bl_order[max_blindex] * 2 + 1] != 0) break; 478 // Determine the number of bit length codes to send. The pkzip format
479 } 479 // requires that at least 4 bit length codes be sent. (appnote.txt says
480 480 // 3 but the actual value used is 4.)
481 // Update opt_len to include the bit length tree and counts 481 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
482 opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4; 482 if (bl_tree[Tree.bl_order[max_blindex]*2+1] != 0) break;
483 return max_blindex; 483 }
484 } 484 // Update opt_len to include the bit length tree and counts
485 485 opt_len += 3*(max_blindex+1) + 5+5+4;
486 486
487 // Send the header for a block using dynamic Huffman trees: the counts, the 487 return max_blindex;
488 // lengths of the bit length codes, the literal tree and the distance tree. 488 }
489 // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. 489
490 void send_all_trees(int lcodes, int dcodes, int blcodes) { 490
491 int rank; // index in bl_order 491 // Send the header for a block using dynamic Huffman trees: the counts, the
492 send_bits(lcodes - 257, 5); // not +255 as stated in appnote.txt 492 // lengths of the bit length codes, the literal tree and the distance tree.
493 send_bits(dcodes - 1, 5); 493 // IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
494 send_bits(blcodes - 4, 4); // not -3 as stated in appnote.txt 494 void send_all_trees(int lcodes, int dcodes, int blcodes){
495 495 int rank; // index in bl_order
496 for (rank = 0; rank < blcodes; rank++) { 496
497 send_bits(bl_tree[Tree.bl_order[rank] * 2 + 1], 3); 497 send_bits(lcodes-257, 5); // not +255 as stated in appnote.txt
498 } 498 send_bits(dcodes-1, 5);
499 499 send_bits(blcodes-4, 4); // not -3 as stated in appnote.txt
500 send_tree(dyn_ltree, lcodes - 1); // literal tree 500 for (rank = 0; rank < blcodes; rank++) {
501 send_tree(dyn_dtree, dcodes - 1); // distance tree 501 send_bits(bl_tree[Tree.bl_order[rank]*2+1], 3);
502 } 502 }
503 503 send_tree(dyn_ltree, lcodes-1); // literal tree
504 // Send a literal or distance tree in compressed form, using the codes in 504 send_tree(dyn_dtree, dcodes-1); // distance tree
505 // bl_tree. 505 }
506 void send_tree(short[] tree, // the tree to be sent 506
507 int max_code // and its largest code of non zero frequency 507 // Send a literal or distance tree in compressed form, using the codes in
508 ) { 508 // bl_tree.
509 int n; // iterates over all tree elements 509 void send_tree (short[] tree,// the tree to be sent
510 int prevlen = -1; // last emitted length 510 int max_code // and its largest code of non zero frequency
511 int curlen; // length of current code 511 ){
512 int nextlen = tree[0 * 2 + 1]; // length of next code 512 int n; // iterates over all tree elements
513 int count = 0; // repeat count of the current code 513 int prevlen = -1; // last emitted length
514 int max_count = 7; // max repeat count 514 int curlen; // length of current code
515 int min_count = 4; // min repeat count 515 int nextlen = tree[0*2+1]; // length of next code
516 516 int count = 0; // repeat count of the current code
517 if (nextlen == 0) { max_count = 138; min_count = 3; } 517 int max_count = 7; // max repeat count
518 518 int min_count = 4; // min repeat count
519 for (n = 0; n <= max_code; n++) { 519
520 curlen = nextlen; nextlen = tree[(n + 1) * 2 + 1]; 520 if (nextlen == 0){ max_count = 138; min_count = 3; }
521 521
522 if (++count < max_count && curlen == nextlen) { 522 for (n = 0; n <= max_code; n++) {
523 continue; 523 curlen = nextlen; nextlen = tree[(n+1)*2+1];
524 } 524 if(++count < max_count && curlen == nextlen) {
525 else if (count < min_count) { 525 continue;
526 do { send_code(curlen, bl_tree); } 526 }
527 while (--count != 0); 527 else if(count < min_count) {
528 } 528 do { send_code(curlen, bl_tree); } while (--count != 0);
529 else if (curlen != 0) { 529 }
530 if (curlen != prevlen) { 530 else if(curlen != 0){
531 send_code(curlen, bl_tree); count--; 531 if(curlen != prevlen){
532 } 532 send_code(curlen, bl_tree); count--;
533 533 }
534 send_code(REP_3_6, bl_tree); 534 send_code(REP_3_6, bl_tree);
535 send_bits(count - 3, 2); 535 send_bits(count-3, 2);
536 } 536 }
537 else if (count <= 10) { 537 else if(count <= 10){
538 send_code(REPZ_3_10, bl_tree); 538 send_code(REPZ_3_10, bl_tree);
539 send_bits(count - 3, 3); 539 send_bits(count-3, 3);
540 } 540 }
541 else { 541 else{
542 send_code(REPZ_11_138, bl_tree); 542 send_code(REPZ_11_138, bl_tree);
543 send_bits(count - 11, 7); 543 send_bits(count-11, 7);
544 } 544 }
545 545 count = 0; prevlen = curlen;
546 count = 0; prevlen = curlen; 546 if(nextlen == 0){
547 547 max_count = 138; min_count = 3;
548 if (nextlen == 0) { 548 }
549 max_count = 138; min_count = 3; 549 else if(curlen == nextlen){
550 } 550 max_count = 6; min_count = 3;
551 else if (curlen == nextlen) { 551 }
552 max_count = 6; min_count = 3; 552 else{
553 } 553 max_count = 7; min_count = 4;
554 else { 554 }
555 max_count = 7; min_count = 4; 555 }
556 } 556 }
557 } 557
558 } 558 // Output a byte on the stream.
559 559 // IN assertion: there is enough room in pending_buf.
560 // Output a byte on the stream. 560 final void put_byte(byte[] p, int start, int len){
561 // IN assertion: there is enough room in pending_buf. 561 System.arraycopy(p, start, pending_buf, pending, len);
562 final void put_byte(byte[] p, int start, int len) { 562 pending+=len;
563 System.arraycopy(p, start, pending_buf, pending, len); 563 }
564 pending += len; 564
565 } 565 final void put_byte(byte c){
566 566 pending_buf[pending++]=c;
567 final void put_byte(byte c) { 567 }
568 pending_buf[pending++] = c; 568 final void put_short(int w) {
569 } 569 put_byte((byte)(w/*&0xff*/));
570 final void put_short(int w) { 570 put_byte((byte)(w>>>8));
571 put_byte((byte)(w/*&0xff*/)); 571 }
572 put_byte((byte)(w >>> 8)); 572 final void putShortMSB(int b){
573 } 573 put_byte((byte)(b>>8));
574 final void putShortMSB(int b) { 574 put_byte((byte)(b/*&0xff*/));
575 put_byte((byte)(b >> 8)); 575 }
576 put_byte((byte)(b/*&0xff*/)); 576
577 } 577 final void send_code(int c, short[] tree){
578 578 int c2=c*2;
579 final void send_code(int c, short[] tree) { 579 send_bits((tree[c2]&0xffff), (tree[c2+1]&0xffff));
580 int c2 = c * 2; 580 }
581 send_bits((tree[c2] & 0xffff), (tree[c2 + 1] & 0xffff)); 581
582 } 582 void send_bits(int value, int length){
583 583 int len = length;
584 void send_bits(int value, int length) { 584 if (bi_valid > (int)Buf_size - len) {
585 int len = length; 585 int val = value;
586
587 if (bi_valid > (int)Buf_size - len) {
588 int val = value;
589 // bi_buf |= (val << bi_valid); 586 // bi_buf |= (val << bi_valid);
590 bi_buf |= ((val << bi_valid) & 0xffff); 587 bi_buf |= ((val << bi_valid)&0xffff);
591 put_short(bi_buf); 588 put_short(bi_buf);
592 bi_buf = (short)(val >>> (Buf_size - bi_valid)); 589 bi_buf = (short)(val >>> (Buf_size - bi_valid));
593 bi_valid += len - Buf_size; 590 bi_valid += len - Buf_size;
594 } 591 } else {
595 else {
596 // bi_buf |= (value) << bi_valid; 592 // bi_buf |= (value) << bi_valid;
597 bi_buf |= (((value) << bi_valid) & 0xffff); 593 bi_buf |= (((value) << bi_valid)&0xffff);
598 bi_valid += len; 594 bi_valid += len;
599 } 595 }
600 } 596 }
601 597
602 // Send one empty static block to give enough lookahead for inflate. 598 // Send one empty static block to give enough lookahead for inflate.
603 // This takes 10 bits, of which 7 may remain in the bit buffer. 599 // This takes 10 bits, of which 7 may remain in the bit buffer.
604 // The current inflate code requires 9 bits of lookahead. If the 600 // The current inflate code requires 9 bits of lookahead. If the
605 // last two codes for the previous block (real code plus EOB) were coded 601 // last two codes for the previous block (real code plus EOB) were coded
606 // on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode 602 // on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
607 // the last real code. In this case we send two empty static blocks instead 603 // the last real code. In this case we send two empty static blocks instead
608 // of one. (There are no problems if the previous block is stored or fixed.) 604 // of one. (There are no problems if the previous block is stored or fixed.)
609 // To simplify the code, we assume the worst case of last real code encoded 605 // To simplify the code, we assume the worst case of last real code encoded
610 // on one bit only. 606 // on one bit only.
611 void _tr_align() { 607 void _tr_align(){
612 send_bits(STATIC_TREES << 1, 3); 608 send_bits(STATIC_TREES<<1, 3);
613 send_code(END_BLOCK, StaticTree.static_ltree); 609 send_code(END_BLOCK, StaticTree.static_ltree);
614 bi_flush(); 610
615 611 bi_flush();
616 // Of the 10 bits for the empty block, we have already sent 612
617 // (10 - bi_valid) bits. The lookahead for the last real code (before 613 // Of the 10 bits for the empty block, we have already sent
618 // the EOB of the previous block) was thus at least one plus the length 614 // (10 - bi_valid) bits. The lookahead for the last real code (before
619 // of the EOB plus what we have just sent of the empty static block. 615 // the EOB of the previous block) was thus at least one plus the length
620 if (1 + last_eob_len + 10 - bi_valid < 9) { 616 // of the EOB plus what we have just sent of the empty static block.
621 send_bits(STATIC_TREES << 1, 3); 617 if (1 + last_eob_len + 10 - bi_valid < 9) {
622 send_code(END_BLOCK, StaticTree.static_ltree); 618 send_bits(STATIC_TREES<<1, 3);
623 bi_flush(); 619 send_code(END_BLOCK, StaticTree.static_ltree);
624 } 620 bi_flush();
625 621 }
626 last_eob_len = 7; 622 last_eob_len = 7;
627 } 623 }
628 624
629 625
630 // Save the match info and tally the frequency counts. Return true if 626 // Save the match info and tally the frequency counts. Return true if
631 // the current block must be flushed. 627 // the current block must be flushed.
632 boolean _tr_tally(int dist, // distance of matched string 628 boolean _tr_tally (int dist, // distance of matched string
633 int lc // match length-MIN_MATCH or unmatched char (if dist==0) 629 int lc // match length-MIN_MATCH or unmatched char (if dist==0)
634 ) { 630 ){
635 pending_buf[d_buf + last_lit * 2] = (byte)(dist >>> 8); 631
636 pending_buf[d_buf + last_lit * 2 + 1] = (byte)dist; 632 pending_buf[d_buf+last_lit*2] = (byte)(dist>>>8);
637 pending_buf[l_buf + last_lit] = (byte)lc; last_lit++; 633 pending_buf[d_buf+last_lit*2+1] = (byte)dist;
638 634
639 if (dist == 0) { 635 l_buf[last_lit] = (byte)lc; last_lit++;
640 // lc is the unmatched char 636
641 dyn_ltree[lc * 2]++; 637 if (dist == 0) {
642 } 638 // lc is the unmatched char
643 else { 639 dyn_ltree[lc*2]++;
644 matches++; 640 }
645 // Here, lc is the match length - MIN_MATCH 641 else {
646 dist--; // dist = match distance - 1 642 matches++;
647 dyn_ltree[(Tree._length_code[lc] + LITERALS + 1) * 2]++; 643 // Here, lc is the match length - MIN_MATCH
648 dyn_dtree[Tree.d_code(dist) * 2]++; 644 dist--; // dist = match distance - 1
649 } 645 dyn_ltree[(Tree._length_code[lc]+LITERALS+1)*2]++;
650 646 dyn_dtree[Tree.d_code(dist)*2]++;
651 if ((last_lit & 0x1fff) == 0 && level > 2) { 647 }
652 // Compute an upper bound for the compressed length 648
653 int out_length = last_lit * 8; 649 if ((last_lit & 0x1fff) == 0 && level > 2) {
654 int in_length = strstart - block_start; 650 // Compute an upper bound for the compressed length
655 int dcode; 651 int out_length = last_lit*8;
656 652 int in_length = strstart - block_start;
657 for (dcode = 0; dcode < D_CODES; dcode++) { 653 int dcode;
658 out_length += (int)dyn_dtree[dcode * 2] * 654 for (dcode = 0; dcode < D_CODES; dcode++) {
659 (5L + Tree.extra_dbits[dcode]); 655 out_length += (int)dyn_dtree[dcode*2] *
660 } 656 (5L+Tree.extra_dbits[dcode]);
661 657 }
662 out_length >>>= 3; 658 out_length >>>= 3;
663 659 if ((matches < (last_lit/2)) && out_length < in_length/2) return true;
664 if ((matches < (last_lit / 2)) && out_length < in_length / 2) return true; 660 }
665 } 661
666 662 return (last_lit == lit_bufsize-1);
667 return (last_lit == lit_bufsize - 1); 663 // We avoid equality with lit_bufsize because of wraparound at 64K
668 // We avoid equality with lit_bufsize because of wraparound at 64K 664 // on 16 bit machines and because stored blocks are restricted to
669 // on 16 bit machines and because stored blocks are restricted to 665 // 64K-1 bytes.
670 // 64K-1 bytes. 666 }
671 } 667
672 668 // Send the block data compressed using the given Huffman trees
673 // Send the block data compressed using the given Huffman trees 669 void compress_block(short[] ltree, short[] dtree){
674 void compress_block(short[] ltree, short[] dtree) { 670 int dist; // distance of matched string
675 int dist; // distance of matched string 671 int lc; // match length or unmatched char (if dist == 0)
676 int lc; // match length or unmatched char (if dist == 0) 672 int lx = 0; // running index in l_buf
677 int lx = 0; // running index in l_buf 673 int code; // the code to send
678 int code; // the code to send 674 int extra; // number of extra bits to send
679 int extra; // number of extra bits to send 675
680 676 if (last_lit != 0){
681 if (last_lit != 0) { 677 do{
682 do { 678 dist=((pending_buf[d_buf+lx*2]<<8)&0xff00)|
683 dist = ((pending_buf[d_buf + lx * 2] << 8) & 0xff00) | 679 (pending_buf[d_buf+lx*2+1]&0xff);
684 (pending_buf[d_buf + lx * 2 + 1] & 0xff); 680 lc=(l_buf[lx])&0xff; lx++;
685 lc = (pending_buf[l_buf + lx]) & 0xff; lx++; 681
686 682 if(dist == 0){
687 if (dist == 0) { 683 send_code(lc, ltree); // send a literal byte
688 send_code(lc, ltree); // send a literal byte 684 }
689 } 685 else{
690 else { 686 // Here, lc is the match length - MIN_MATCH
691 // Here, lc is the match length - MIN_MATCH 687 code = Tree._length_code[lc];
692 code = Tree._length_code[lc]; 688
693 send_code(code + LITERALS + 1, ltree); // send the length code 689 send_code(code+LITERALS+1, ltree); // send the length code
694 extra = Tree.extra_lbits[code]; 690 extra = Tree.extra_lbits[code];
695 691 if(extra != 0){
696 if (extra != 0) { 692 lc -= Tree.base_length[code];
697 lc -= Tree.base_length[code]; 693 send_bits(lc, extra); // send the extra length bits
698 send_bits(lc, extra); // send the extra length bits 694 }
699 } 695 dist--; // dist is now the match distance - 1
700 696 code = Tree.d_code(dist);
701 dist--; // dist is now the match distance - 1 697
702 code = Tree.d_code(dist); 698 send_code(code, dtree); // send the distance code
703 send_code(code, dtree); // send the distance code 699 extra = Tree.extra_dbits[code];
704 extra = Tree.extra_dbits[code]; 700 if (extra != 0) {
705 701 dist -= Tree.base_dist[code];
706 if (extra != 0) { 702 send_bits(dist, extra); // send the extra distance bits
707 dist -= Tree.base_dist[code]; 703 }
708 send_bits(dist, extra); // send the extra distance bits 704 } // literal or match pair ?
709 } 705
710 } // literal or match pair ? 706 // Check that the overlay between pending_buf and d_buf+l_buf is ok:
711 707 }
712 // Check that the overlay between pending_buf and d_buf+l_buf is ok: 708 while (lx < last_lit);
713 } 709 }
714 while (lx < last_lit); 710
715 } 711 send_code(END_BLOCK, ltree);
716 712 last_eob_len = ltree[END_BLOCK*2+1];
717 send_code(END_BLOCK, ltree); 713 }
718 last_eob_len = ltree[END_BLOCK * 2 + 1]; 714
719 } 715 // Set the data type to ASCII or BINARY, using a crude approximation:
720 716 // binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
721 // Set the data type to ASCII or BINARY, using a crude approximation: 717 // IN assertion: the fields freq of dyn_ltree are set and the total of all
722 // binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise. 718 // frequencies does not exceed 64K (to fit in an int on 16 bit machines).
723 // IN assertion: the fields freq of dyn_ltree are set and the total of all 719 void set_data_type(){
724 // frequencies does not exceed 64K (to fit in an int on 16 bit machines). 720 int n = 0;
725 void set_data_type() { 721 int ascii_freq = 0;
726 int n = 0; 722 int bin_freq = 0;
727 int ascii_freq = 0; 723 while(n<7){ bin_freq += dyn_ltree[n*2]; n++;}
728 int bin_freq = 0; 724 while(n<128){ ascii_freq += dyn_ltree[n*2]; n++;}
729 725 while(n<LITERALS){ bin_freq += dyn_ltree[n*2]; n++;}
730 while (n < 7) { bin_freq += dyn_ltree[n * 2]; n++;} 726 data_type=(byte)(bin_freq > (ascii_freq >>> 2) ? Z_BINARY : Z_ASCII);
731 727 }
732 while (n < 128) { ascii_freq += dyn_ltree[n * 2]; n++;} 728
733 729 // Flush the bit buffer, keeping at most 7 bits in it.
734 while (n < LITERALS) { bin_freq += dyn_ltree[n * 2]; n++;} 730 void bi_flush(){
735 731 if (bi_valid == 16) {
736 data_type = (byte)(bin_freq > (ascii_freq >>> 2) ? Z_BINARY : Z_ASCII); 732 put_short(bi_buf);
737 } 733 bi_buf=0;
738 734 bi_valid=0;
739 // Flush the bit buffer, keeping at most 7 bits in it. 735 }
740 void bi_flush() { 736 else if (bi_valid >= 8) {
741 if (bi_valid == 16) { 737 put_byte((byte)bi_buf);
742 put_short(bi_buf); 738 bi_buf>>>=8;
743 bi_buf = 0; 739 bi_valid-=8;
744 bi_valid = 0; 740 }
745 } 741 }
746 else if (bi_valid >= 8) { 742
747 put_byte((byte)bi_buf); 743 // Flush the bit buffer and align the output on a byte boundary
748 bi_buf >>>= 8; 744 void bi_windup(){
749 bi_valid -= 8; 745 if (bi_valid > 8) {
750 } 746 put_short(bi_buf);
751 } 747 } else if (bi_valid > 0) {
752 748 put_byte((byte)bi_buf);
753 // Flush the bit buffer and align the output on a byte boundary 749 }
754 void bi_windup() { 750 bi_buf = 0;
755 if (bi_valid > 8) { 751 bi_valid = 0;
756 put_short(bi_buf); 752 }
757 } 753
758 else if (bi_valid > 0) { 754 // Copy a stored block, storing first the length and its
759 put_byte((byte)bi_buf); 755 // one's complement if requested.
760 } 756 void copy_block(int buf, // the input data
761 757 int len, // its length
762 bi_buf = 0; 758 boolean header // true if block header must be written
763 bi_valid = 0; 759 ){
764 } 760 int index=0;
765 761 bi_windup(); // align on byte boundary
766 // Copy a stored block, storing first the length and its 762 last_eob_len = 8; // enough lookahead for inflate
767 // one's complement if requested. 763
768 void copy_block(int buf, // the input data 764 if (header) {
769 int len, // its length 765 put_short((short)len);
770 boolean header // true if block header must be written 766 put_short((short)~len);
771 ) { 767 }
772 int index = 0; 768
773 bi_windup(); // align on byte boundary 769 // while(len--!=0) {
774 last_eob_len = 8; // enough lookahead for inflate 770 // put_byte(window[buf+index]);
775 771 // index++;
776 if (header) { 772 // }
777 put_short((short)len); 773 put_byte(window, buf, len);
778 put_short((short)~len); 774 }
779 } 775
780 776 void flush_block_only(boolean eof){
781 // while(len--!=0) { 777 _tr_flush_block(block_start>=0 ? block_start : -1,
782 // put_byte(window[buf+index]); 778 strstart-block_start,
783 // index++; 779 eof);
784 // } 780 block_start=strstart;
785 put_byte(window, buf, len); 781 strm.flush_pending();
786 } 782 }
787 783
788 void flush_block_only(boolean eof) { 784 // Copy without compression as much as possible from the input stream, return
789 _tr_flush_block(block_start >= 0 ? block_start : -1, 785 // the current block state.
790 strstart - block_start, 786 // This function does not insert new strings in the dictionary since
791 eof); 787 // uncompressible data is probably not useful. This function is used
792 block_start = strstart; 788 // only for the level=0 compression option.
793 strm.flush_pending(); 789 // NOTE: this function should be optimized to avoid extra copying from
794 } 790 // window to pending_buf.
795 791 int deflate_stored(int flush){
796 // Copy without compression as much as possible from the input stream, return 792 // Stored blocks are limited to 0xffff bytes, pending_buf is limited
797 // the current block state. 793 // to pending_buf_size, and each stored block has a 5 byte header:
798 // This function does not insert new strings in the dictionary since 794
799 // uncompressible data is probably not useful. This function is used 795 int max_block_size = 0xffff;
800 // only for the level=0 compression option. 796 int max_start;
801 // NOTE: this function should be optimized to avoid extra copying from 797
802 // window to pending_buf. 798 if(max_block_size > pending_buf_size - 5) {
803 int deflate_stored(int flush) { 799 max_block_size = pending_buf_size - 5;
804 // Stored blocks are limited to 0xffff bytes, pending_buf is limited 800 }
805 // to pending_buf_size, and each stored block has a 5 byte header: 801
806 int max_block_size = 0xffff; 802 // Copy as much as possible from input to output:
807 int max_start; 803 while(true){
808 804 // Fill the window as much as possible:
809 if (max_block_size > pending_buf_size - 5) { 805 if(lookahead<=1){
810 max_block_size = pending_buf_size - 5; 806 fill_window();
811 } 807 if(lookahead==0 && flush==Z_NO_FLUSH) return NeedMore;
812 808 if(lookahead==0) break; // flush the current block
813 // Copy as much as possible from input to output: 809 }
814 while (true) { 810
815 // Fill the window as much as possible: 811 strstart+=lookahead;
816 if (lookahead <= 1) { 812 lookahead=0;
817 fill_window(); 813
818 814 // Emit a stored block if pending_buf will be full:
819 if (lookahead == 0 && flush == Z_NO_FLUSH) return NeedMore; 815 max_start=block_start+max_block_size;
820 816 if(strstart==0|| strstart>=max_start) {
821 if (lookahead == 0) break; // flush the current block 817 // strstart == 0 is possible when wraparound on 16-bit machine
822 } 818 lookahead = (int)(strstart-max_start);
823 819 strstart = (int)max_start;
824 strstart += lookahead; 820
825 lookahead = 0; 821 flush_block_only(false);
826 // Emit a stored block if pending_buf will be full: 822 if(strm.avail_out==0) return NeedMore;
827 max_start = block_start + max_block_size; 823
828 824 }
829 if (strstart == 0 || strstart >= max_start) { 825
830 // strstart == 0 is possible when wraparound on 16-bit machine 826 // Flush if we may have to slide, otherwise block_start may become
831 lookahead = (int)(strstart - max_start); 827 // negative and the data will be gone:
832 strstart = (int)max_start; 828 if(strstart-block_start >= w_size-MIN_LOOKAHEAD) {
833 flush_block_only(false); 829 flush_block_only(false);
834 830 if(strm.avail_out==0) return NeedMore;
835 if (strm.avail_out == 0) return NeedMore; 831 }
836 } 832 }
837 833
838 // Flush if we may have to slide, otherwise block_start may become 834 flush_block_only(flush == Z_FINISH);
839 // negative and the data will be gone: 835 if(strm.avail_out==0)
840 if (strstart - block_start >= w_size - MIN_LOOKAHEAD) { 836 return (flush == Z_FINISH) ? FinishStarted : NeedMore;
841 flush_block_only(false); 837
842 838 return flush == Z_FINISH ? FinishDone : BlockDone;
843 if (strm.avail_out == 0) return NeedMore; 839 }
844 } 840
845 } 841 // Send a stored block
846 842 void _tr_stored_block(int buf, // input block
847 flush_block_only(flush == Z_FINISH); 843 int stored_len, // length of input block
848 844 boolean eof // true if this is the last block for a file
849 if (strm.avail_out == 0) 845 ){
850 return (flush == Z_FINISH) ? FinishStarted : NeedMore; 846 send_bits((STORED_BLOCK<<1)+(eof?1:0), 3); // send block type
851 847 copy_block(buf, stored_len, true); // with header
852 return flush == Z_FINISH ? FinishDone : BlockDone; 848 }
853 } 849
854 850 // Determine the best encoding for the current block: dynamic trees, static
855 // Send a stored block 851 // trees or store, and output the encoded block to the zip file.
856 void _tr_stored_block(int buf, // input block 852 void _tr_flush_block(int buf, // input block, or NULL if too old
857 int stored_len, // length of input block 853 int stored_len, // length of input block
858 boolean eof // true if this is the last block for a file 854 boolean eof // true if this is the last block for a file
859 ) { 855 ) {
860 send_bits((STORED_BLOCK << 1) + (eof ? 1 : 0), 3); // send block type 856 int opt_lenb, static_lenb;// opt_len and static_len in bytes
861 copy_block(buf, stored_len, true); // with header 857 int max_blindex = 0; // index of last bit length code of non zero freq
862 } 858
863 859 // Build the Huffman trees unless a stored block is forced
864 // Determine the best encoding for the current block: dynamic trees, static 860 if(level > 0) {
865 // trees or store, and output the encoded block to the zip file. 861 // Check if the file is ascii or binary
866 void _tr_flush_block(int buf, // input block, or NULL if too old 862 if(data_type == Z_UNKNOWN) set_data_type();
867 int stored_len, // length of input block 863
868 boolean eof // true if this is the last block for a file 864 // Construct the literal and distance trees
869 ) { 865 l_desc.build_tree(this);
870 int opt_lenb, static_lenb;// opt_len and static_len in bytes 866
871 int max_blindex = 0; // index of last bit length code of non zero freq 867 d_desc.build_tree(this);
872 868
873 // Build the Huffman trees unless a stored block is forced 869 // At this point, opt_len and static_len are the total bit lengths of
874 if (level > 0) { 870 // the compressed block data, excluding the tree representations.
875 // Check if the file is ascii or binary 871
876 if (data_type == Z_UNKNOWN) set_data_type(); 872 // Build the bit length tree for the above two trees, and get the index
877 873 // in bl_order of the last bit length code to send.
878 // Construct the literal and distance trees 874 max_blindex=build_bl_tree();
879 l_desc.build_tree(this); 875
880 d_desc.build_tree(this); 876 // Determine the best encoding. Compute first the block length in bytes
881 // At this point, opt_len and static_len are the total bit lengths of 877 opt_lenb=(opt_len+3+7)>>>3;
882 // the compressed block data, excluding the tree representations. 878 static_lenb=(static_len+3+7)>>>3;
883 // Build the bit length tree for the above two trees, and get the index 879
884 // in bl_order of the last bit length code to send. 880 if(static_lenb<=opt_lenb) opt_lenb=static_lenb;
885 max_blindex = build_bl_tree(); 881 }
886 // Determine the best encoding. Compute first the block length in bytes 882 else {
887 opt_lenb = (opt_len + 3 + 7) >>> 3; 883 opt_lenb=static_lenb=stored_len+5; // force a stored block
888 static_lenb = (static_len + 3 + 7) >>> 3; 884 }
889 885
890 if (static_lenb <= opt_lenb) opt_lenb = static_lenb; 886 if(stored_len+4<=opt_lenb && buf != -1){
891 } 887 // 4: two words for the lengths
892 else { 888 // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
893 opt_lenb = static_lenb = stored_len + 5; // force a stored block 889 // Otherwise we can't have processed more than WSIZE input bytes since
894 } 890 // the last block flush, because compression would have been
895 891 // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
896 if (stored_len + 4 <= opt_lenb && buf != -1) { 892 // transform a block into a stored block.
897 // 4: two words for the lengths 893 _tr_stored_block(buf, stored_len, eof);
898 // The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. 894 }
899 // Otherwise we can't have processed more than WSIZE input bytes since 895 else if(static_lenb == opt_lenb){
900 // the last block flush, because compression would have been 896 send_bits((STATIC_TREES<<1)+(eof?1:0), 3);
901 // successful. If LIT_BUFSIZE <= WSIZE, it is never too late to 897 compress_block(StaticTree.static_ltree, StaticTree.static_dtree);
902 // transform a block into a stored block. 898 }
903 _tr_stored_block(buf, stored_len, eof); 899 else{
904 } 900 send_bits((DYN_TREES<<1)+(eof?1:0), 3);
905 else if (static_lenb == opt_lenb) { 901 send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
906 send_bits((STATIC_TREES << 1) + (eof ? 1 : 0), 3); 902 compress_block(dyn_ltree, dyn_dtree);
907 compress_block(StaticTree.static_ltree, StaticTree.static_dtree); 903 }
908 } 904
909 else { 905 // The above check is made mod 2^32, for files larger than 512 MB
910 send_bits((DYN_TREES << 1) + (eof ? 1 : 0), 3); 906 // and uLong implemented on 32 bits.
911 send_all_trees(l_desc.max_code + 1, d_desc.max_code + 1, max_blindex + 1); 907
912 compress_block(dyn_ltree, dyn_dtree); 908 init_block();
913 } 909
914 910 if(eof){
915 // The above check is made mod 2^32, for files larger than 512 MB 911 bi_windup();
916 // and uLong implemented on 32 bits. 912 }
917 init_block(); 913 }
918 914
919 if (eof) { 915 // Fill the window when the lookahead becomes insufficient.
920 bi_windup(); 916 // Updates strstart and lookahead.
921 } 917 //
922 } 918 // IN assertion: lookahead < MIN_LOOKAHEAD
923 919 // OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
924 // Fill the window when the lookahead becomes insufficient. 920 // At least one byte has been read, or avail_in == 0; reads are
925 // Updates strstart and lookahead. 921 // performed for at least two bytes (required for the zip translate_eol
922 // option -- not supported here).
923 void fill_window(){
924 int n, m;
925 int p;
926 int more; // Amount of free space at the end of the window.
927
928 do{
929 more = (window_size-lookahead-strstart);
930
931 // Deal with !@#$% 64K limit:
932 if(more==0 && strstart==0 && lookahead==0){
933 more = w_size;
934 }
935 else if(more==-1) {
936 // Very unlikely, but possible on 16 bit machine if strstart == 0
937 // and lookahead == 1 (input done one byte at time)
938 more--;
939
940 // If the window is almost full and there is insufficient lookahead,
941 // move the upper half to the lower one to make room in the upper half.
942 }
943 else if(strstart >= w_size+ w_size-MIN_LOOKAHEAD) {
944 System.arraycopy(window, w_size, window, 0, w_size);
945 match_start-=w_size;
946 strstart-=w_size; // we now have strstart >= MAX_DIST
947 block_start-=w_size;
948
949 // Slide the hash table (could be avoided with 32 bit values
950 // at the expense of memory usage). We slide even when level == 0
951 // to keep the hash table consistent if we switch back to level > 0
952 // later. (Using level 0 permanently is not an optimal usage of
953 // zlib, so we don't care about this pathological case.)
954
955 n = hash_size;
956 p=n;
957 do {
958 m = (head[--p]&0xffff);
959 head[p]=(m>=w_size ? (short)(m-w_size) : 0);
960 }
961 while (--n != 0);
962
963 n = w_size;
964 p = n;
965 do {
966 m = (prev[--p]&0xffff);
967 prev[p] = (m >= w_size ? (short)(m-w_size) : 0);
968 // If n is not on any hash chain, prev[n] is garbage but
969 // its value will never be used.
970 }
971 while (--n!=0);
972 more += w_size;
973 }
974
975 if (strm.avail_in == 0) return;
976
977 // If there was no sliding:
978 // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
979 // more == window_size - lookahead - strstart
980 // => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
981 // => more >= window_size - 2*WSIZE + 2
982 // In the BIG_MEM or MMAP case (not yet supported),
983 // window_size == input_size + MIN_LOOKAHEAD &&
984 // strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
985 // Otherwise, window_size == 2*WSIZE so more >= 2.
986 // If there was sliding, more >= WSIZE. So in all cases, more >= 2.
987
988 n = strm.read_buf(window, strstart + lookahead, more);
989 lookahead += n;
990
991 // Initialize the hash value now that we have some input:
992 if(lookahead >= MIN_MATCH) {
993 ins_h = window[strstart]&0xff;
994 ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask;
995 }
996 // If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
997 // but this is not important since only literal bytes will be emitted.
998 }
999 while (lookahead < MIN_LOOKAHEAD && strm.avail_in != 0);
1000 }
1001
1002 // Compress as much as possible from the input stream, return the current
1003 // block state.
1004 // This function does not perform lazy evaluation of matches and inserts
1005 // new strings in the dictionary only for unmatched strings or for short
1006 // matches. It is used only for the fast compression options.
1007 int deflate_fast(int flush){
1008 // short hash_head = 0; // head of the hash chain
1009 int hash_head = 0; // head of the hash chain
1010 boolean bflush; // set if current block must be flushed
1011
1012 while(true){
1013 // Make sure that we always have enough lookahead, except
1014 // at the end of the input file. We need MAX_MATCH bytes
1015 // for the next match, plus MIN_MATCH bytes to insert the
1016 // string following the next match.
1017 if(lookahead < MIN_LOOKAHEAD){
1018 fill_window();
1019 if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH){
1020 return NeedMore;
1021 }
1022 if(lookahead == 0) break; // flush the current block
1023 }
1024
1025 // Insert the string window[strstart .. strstart+2] in the
1026 // dictionary, and set hash_head to the head of the hash chain:
1027 if(lookahead >= MIN_MATCH){
1028 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
1029
1030 // prev[strstart&w_mask]=hash_head=head[ins_h];
1031 hash_head=(head[ins_h]&0xffff);
1032 prev[strstart&w_mask]=head[ins_h];
1033 head[ins_h]=(short)strstart;
1034 }
1035
1036 // Find the longest match, discarding those <= prev_length.
1037 // At this point we have always match_length < MIN_MATCH
1038
1039 if(hash_head!=0L &&
1040 ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD
1041 ){
1042 // To simplify the code, we prevent matches with the string
1043 // of window index 0 (in particular we have to avoid a match
1044 // of the string with itself at the start of the input file).
1045 if(strategy != Z_HUFFMAN_ONLY){
1046 match_length=longest_match (hash_head);
1047 }
1048 // longest_match() sets match_start
1049 }
1050 if(match_length>=MIN_MATCH){
1051 // check_match(strstart, match_start, match_length);
1052
1053 bflush=_tr_tally(strstart-match_start, match_length-MIN_MATCH);
1054
1055 lookahead -= match_length;
1056
1057 // Insert new strings in the hash table only if the match length
1058 // is not too large. This saves time but degrades compression.
1059 if(match_length <= max_lazy_match &&
1060 lookahead >= MIN_MATCH) {
1061 match_length--; // string at strstart already in hash table
1062 do{
1063 strstart++;
1064
1065 ins_h=((ins_h<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
1066 // prev[strstart&w_mask]=hash_head=head[ins_h];
1067 hash_head=(head[ins_h]&0xffff);
1068 prev[strstart&w_mask]=head[ins_h];
1069 head[ins_h]=(short)strstart;
1070
1071 // strstart never exceeds WSIZE-MAX_MATCH, so there are
1072 // always MIN_MATCH bytes ahead.
1073 }
1074 while (--match_length != 0);
1075 strstart++;
1076 }
1077 else{
1078 strstart += match_length;
1079 match_length = 0;
1080 ins_h = window[strstart]&0xff;
1081
1082 ins_h=(((ins_h)<<hash_shift)^(window[strstart+1]&0xff))&hash_mask;
1083 // If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1084 // matter since it will be recomputed at next deflate call.
1085 }
1086 }
1087 else {
1088 // No match, output a literal byte
1089
1090 bflush=_tr_tally(0, window[strstart]&0xff);
1091 lookahead--;
1092 strstart++;
1093 }
1094 if (bflush){
1095
1096 flush_block_only(false);
1097 if(strm.avail_out==0) return NeedMore;
1098 }
1099 }
1100
1101 flush_block_only(flush == Z_FINISH);
1102 if(strm.avail_out==0){
1103 if(flush == Z_FINISH) return FinishStarted;
1104 else return NeedMore;
1105 }
1106 return flush==Z_FINISH ? FinishDone : BlockDone;
1107 }
1108
1109 // Same as above, but achieves better compression. We use a lazy
1110 // evaluation for matches: a match is finally adopted only if there is
1111 // no better match at the next window position.
1112 int deflate_slow(int flush){
1113 // short hash_head = 0; // head of hash chain
1114 int hash_head = 0; // head of hash chain
1115 boolean bflush; // set if current block must be flushed
1116
1117 // Process the input block.
1118 while(true){
1119 // Make sure that we always have enough lookahead, except
1120 // at the end of the input file. We need MAX_MATCH bytes
1121 // for the next match, plus MIN_MATCH bytes to insert the
1122 // string following the next match.
1123
1124 if (lookahead < MIN_LOOKAHEAD) {
1125 fill_window();
1126 if(lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1127 return NeedMore;
1128 }
1129 if(lookahead == 0) break; // flush the current block
1130 }
1131
1132 // Insert the string window[strstart .. strstart+2] in the
1133 // dictionary, and set hash_head to the head of the hash chain:
1134
1135 if(lookahead >= MIN_MATCH) {
1136 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff)) & hash_mask;
1137 // prev[strstart&w_mask]=hash_head=head[ins_h];
1138 hash_head=(head[ins_h]&0xffff);
1139 prev[strstart&w_mask]=head[ins_h];
1140 head[ins_h]=(short)strstart;
1141 }
1142
1143 // Find the longest match, discarding those <= prev_length.
1144 prev_length = match_length; prev_match = match_start;
1145 match_length = MIN_MATCH-1;
1146
1147 if (hash_head != 0 && prev_length < max_lazy_match &&
1148 ((strstart-hash_head)&0xffff) <= w_size-MIN_LOOKAHEAD
1149 ){
1150 // To simplify the code, we prevent matches with the string
1151 // of window index 0 (in particular we have to avoid a match
1152 // of the string with itself at the start of the input file).
1153
1154 if(strategy != Z_HUFFMAN_ONLY) {
1155 match_length = longest_match(hash_head);
1156 }
1157 // longest_match() sets match_start
1158
1159 if (match_length <= 5 && (strategy == Z_FILTERED ||
1160 (match_length == MIN_MATCH &&
1161 strstart - match_start > 4096))) {
1162
1163 // If prev_match is also MIN_MATCH, match_start is garbage
1164 // but we will ignore the current match anyway.
1165 match_length = MIN_MATCH-1;
1166 }
1167 }
1168
1169 // If there was a match at the previous step and the current
1170 // match is not better, output the previous match:
1171 if(prev_length >= MIN_MATCH && match_length <= prev_length) {
1172 int max_insert = strstart + lookahead - MIN_MATCH;
1173 // Do not insert strings in hash table beyond this.
1174
1175 // check_match(strstart-1, prev_match, prev_length);
1176
1177 bflush=_tr_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
1178
1179 // Insert in hash table all strings up to the end of the match.
1180 // strstart-1 and strstart are already inserted. If there is not
1181 // enough lookahead, the last two strings are not inserted in
1182 // the hash table.
1183 lookahead -= prev_length-1;
1184 prev_length -= 2;
1185 do{
1186 if(++strstart <= max_insert) {
1187 ins_h=(((ins_h)<<hash_shift)^(window[(strstart)+(MIN_MATCH-1)]&0xff))&hash_mask;
1188 //prev[strstart&w_mask]=hash_head=head[ins_h];
1189 hash_head=(head[ins_h]&0xffff);
1190 prev[strstart&w_mask]=head[ins_h];
1191 head[ins_h]=(short)strstart;
1192 }
1193 }
1194 while(--prev_length != 0);
1195 match_available = 0;
1196 match_length = MIN_MATCH-1;
1197 strstart++;
1198
1199 if (bflush){
1200 flush_block_only(false);
1201 if(strm.avail_out==0) return NeedMore;
1202 }
1203 } else if (match_available!=0) {
1204
1205 // If there was no match at the previous position, output a
1206 // single literal. If there was a match but the current match
1207 // is longer, truncate the previous match to a single literal.
1208
1209 bflush=_tr_tally(0, window[strstart-1]&0xff);
1210
1211 if (bflush) {
1212 flush_block_only(false);
1213 }
1214 strstart++;
1215 lookahead--;
1216 if(strm.avail_out == 0) return NeedMore;
1217 } else {
1218 // There is no previous match to compare with, wait for
1219 // the next step to decide.
1220
1221 match_available = 1;
1222 strstart++;
1223 lookahead--;
1224 }
1225 }
1226
1227 if(match_available!=0) {
1228 bflush=_tr_tally(0, window[strstart-1]&0xff);
1229 match_available = 0;
1230 }
1231 flush_block_only(flush == Z_FINISH);
1232
1233 if(strm.avail_out==0){
1234 if(flush == Z_FINISH) return FinishStarted;
1235 else return NeedMore;
1236 }
1237
1238 return flush == Z_FINISH ? FinishDone : BlockDone;
1239 }
1240
1241 int longest_match(int cur_match){
1242 int chain_length = max_chain_length; // max hash chain length
1243 int scan = strstart; // current string
1244 int match; // matched string
1245 int len; // length of current match
1246 int best_len = prev_length; // best match length so far
1247 int limit = strstart>(w_size-MIN_LOOKAHEAD) ?
1248 strstart-(w_size-MIN_LOOKAHEAD) : 0;
1249 int nice_match=this.nice_match;
1250
1251 // Stop when cur_match becomes <= limit. To simplify the code,
1252 // we prevent matches with the string of window index 0.
1253
1254 int wmask = w_mask;
1255
1256 int strend = strstart + MAX_MATCH;
1257 byte scan_end1 = window[scan+best_len-1];
1258 byte scan_end = window[scan+best_len];
1259
1260 // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1261 // It is easy to get rid of this optimization if necessary.
1262
1263 // Do not waste too much time if we already have a good match:
1264 if (prev_length >= good_match) {
1265 chain_length >>= 2;
1266 }
1267
1268 // Do not look for matches beyond the end of the input. This is necessary
1269 // to make deflate deterministic.
1270 if (nice_match > lookahead) nice_match = lookahead;
1271
1272 do {
1273 match = cur_match;
1274
1275 // Skip to next match if the match length cannot increase
1276 // or if the match length is less than 2:
1277 if (window[match+best_len] != scan_end ||
1278 window[match+best_len-1] != scan_end1 ||
1279 window[match] != window[scan] ||
1280 window[++match] != window[scan+1]) continue;
1281
1282 // The check at best_len-1 can be removed because it will be made
1283 // again later. (This heuristic is not always a win.)
1284 // It is not necessary to compare scan[2] and match[2] since they
1285 // are always equal when the other bytes match, given that
1286 // the hash keys are equal and that HASH_BITS >= 8.
1287 scan += 2; match++;
1288
1289 // We check for insufficient lookahead only every 8th comparison;
1290 // the 256th check will be made at strstart+258.
1291 do {
1292 } while (window[++scan] == window[++match] &&
1293 window[++scan] == window[++match] &&
1294 window[++scan] == window[++match] &&
1295 window[++scan] == window[++match] &&
1296 window[++scan] == window[++match] &&
1297 window[++scan] == window[++match] &&
1298 window[++scan] == window[++match] &&
1299 window[++scan] == window[++match] &&
1300 scan < strend);
1301
1302 len = MAX_MATCH - (int)(strend - scan);
1303 scan = strend - MAX_MATCH;
1304
1305 if(len>best_len) {
1306 match_start = cur_match;
1307 best_len = len;
1308 if (len >= nice_match) break;
1309 scan_end1 = window[scan+best_len-1];
1310 scan_end = window[scan+best_len];
1311 }
1312
1313 } while ((cur_match = (prev[cur_match & wmask]&0xffff)) > limit
1314 && --chain_length != 0);
1315
1316 if (best_len <= lookahead) return best_len;
1317 return lookahead;
1318 }
1319
1320 int deflateInit(int level, int bits, int memlevel){
1321 return deflateInit(level, Z_DEFLATED, bits, memlevel,
1322 Z_DEFAULT_STRATEGY);
1323 }
1324
1325 int deflateInit(int level, int bits){
1326 return deflateInit(level, Z_DEFLATED, bits, DEF_MEM_LEVEL,
1327 Z_DEFAULT_STRATEGY);
1328 }
1329 int deflateInit(int level){
1330 return deflateInit(level, MAX_WBITS);
1331 }
1332 private int deflateInit(int level, int method, int windowBits,
1333 int memLevel, int strategy){
1334 int wrap = 1;
1335 // byte[] my_version=ZLIB_VERSION;
1336
926 // 1337 //
927 // IN assertion: lookahead < MIN_LOOKAHEAD 1338 // if (version == null || version[0] != my_version[0]
928 // OUT assertions: strstart <= window_size-MIN_LOOKAHEAD 1339 // || stream_size != sizeof(z_stream)) {
929 // At least one byte has been read, or avail_in == 0; reads are 1340 // return Z_VERSION_ERROR;
930 // performed for at least two bytes (required for the zip translate_eol 1341 // }
931 // option -- not supported here). 1342
932 void fill_window() { 1343 strm.msg = null;
933 int n, m; 1344
934 int p; 1345 if (level == Z_DEFAULT_COMPRESSION) level = 6;
935 int more; // Amount of free space at the end of the window. 1346
936 1347 if (windowBits < 0) { // undocumented feature: suppress zlib header
937 do { 1348 wrap = 0;
938 more = (window_size - lookahead - strstart); 1349 windowBits = -windowBits;
939 1350 }
940 // Deal with !@#$% 64K limit: 1351 else if(windowBits > 15){
941 if (more == 0 && strstart == 0 && lookahead == 0) { 1352 wrap = 2;
942 more = w_size; 1353 windowBits -= 16;
943 } 1354 strm.adler=new CRC32();
944 else if (more == -1) { 1355 }
945 // Very unlikely, but possible on 16 bit machine if strstart == 0 1356
946 // and lookahead == 1 (input done one byte at time) 1357 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL ||
947 more--; 1358 method != Z_DEFLATED ||
948 // If the window is almost full and there is insufficient lookahead, 1359 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
949 // move the upper half to the lower one to make room in the upper half. 1360 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
950 } 1361 return Z_STREAM_ERROR;
951 else if (strstart >= w_size + w_size - MIN_LOOKAHEAD) { 1362 }
952 System.arraycopy(window, w_size, window, 0, w_size); 1363
953 match_start -= w_size; 1364 strm.dstate = (Deflate)this;
954 strstart -= w_size; // we now have strstart >= MAX_DIST 1365
955 block_start -= w_size; 1366 this.wrap = wrap;
956 // Slide the hash table (could be avoided with 32 bit values 1367 w_bits = windowBits;
957 // at the expense of memory usage). We slide even when level == 0 1368 w_size = 1 << w_bits;
958 // to keep the hash table consistent if we switch back to level > 0 1369 w_mask = w_size - 1;
959 // later. (Using level 0 permanently is not an optimal usage of 1370
960 // zlib, so we don't care about this pathological case.) 1371 hash_bits = memLevel + 7;
961 n = hash_size; 1372 hash_size = 1 << hash_bits;
962 p = n; 1373 hash_mask = hash_size - 1;
963 1374 hash_shift = ((hash_bits+MIN_MATCH-1)/MIN_MATCH);
964 do { 1375
965 m = (head[--p] & 0xffff); 1376 window = new byte[w_size*2];
966 head[p] = (m >= w_size ? (short)(m - w_size) : 0); 1377 prev = new short[w_size];
967 } 1378 head = new short[hash_size];
968 while (--n != 0); 1379
969 1380 lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
970 n = w_size; 1381
971 p = n; 1382 // We overlay pending_buf and d_buf+l_buf. This works since the average
972 1383 // output size for (length,distance) codes is <= 24 bits.
973 do { 1384 pending_buf = new byte[lit_bufsize*3];
974 m = (prev[--p] & 0xffff); 1385 pending_buf_size = lit_bufsize*3;
975 prev[p] = (m >= w_size ? (short)(m - w_size) : 0); 1386
976 // If n is not on any hash chain, prev[n] is garbage but 1387 d_buf = lit_bufsize;
977 // its value will never be used. 1388 l_buf = new byte[lit_bufsize];
978 } 1389
979 while (--n != 0); 1390 this.level = level;
980 1391
981 more += w_size; 1392 this.strategy = strategy;
982 } 1393 this.method = (byte)method;
983 1394
984 if (strm.avail_in == 0) return; 1395 return deflateReset();
985 1396 }
986 // If there was no sliding: 1397
987 // strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && 1398 int deflateReset(){
988 // more == window_size - lookahead - strstart 1399 strm.total_in = strm.total_out = 0;
989 // => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) 1400 strm.msg = null; //
990 // => more >= window_size - 2*WSIZE + 2 1401 strm.data_type = Z_UNKNOWN;
991 // In the BIG_MEM or MMAP case (not yet supported), 1402
992 // window_size == input_size + MIN_LOOKAHEAD && 1403 pending = 0;
993 // strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. 1404 pending_out = 0;
994 // Otherwise, window_size == 2*WSIZE so more >= 2. 1405
995 // If there was sliding, more >= WSIZE. So in all cases, more >= 2. 1406 if(wrap < 0){
996 n = strm.read_buf(window, strstart + lookahead, more); 1407 wrap = -wrap;
997 lookahead += n; 1408 }
998 1409 status = (wrap==0) ? BUSY_STATE : INIT_STATE;
999 // Initialize the hash value now that we have some input: 1410 strm.adler.reset();
1000 if (lookahead >= MIN_MATCH) { 1411
1001 ins_h = window[strstart] & 0xff; 1412 last_flush = Z_NO_FLUSH;
1002 ins_h = (((ins_h) << hash_shift) ^ (window[strstart + 1] & 0xff)) & hash_mask; 1413
1003 } 1414 tr_init();
1004 1415 lm_init();
1005 // If the whole input has less than MIN_MATCH bytes, ins_h is garbage, 1416 return Z_OK;
1006 // but this is not important since only literal bytes will be emitted. 1417 }
1007 } 1418
1008 while (lookahead < MIN_LOOKAHEAD && strm.avail_in != 0); 1419 int deflateEnd(){
1009 } 1420 if(status!=INIT_STATE && status!=BUSY_STATE && status!=FINISH_STATE){
1010 1421 return Z_STREAM_ERROR;
1011 // Compress as much as possible from the input stream, return the current 1422 }
1012 // block state. 1423 // Deallocate in reverse order of allocations:
1013 // This function does not perform lazy evaluation of matches and inserts 1424 pending_buf=null;
1014 // new strings in the dictionary only for unmatched strings or for short 1425 l_buf=null;
1015 // matches. It is used only for the fast compression options. 1426 head=null;
1016 int deflate_fast(int flush) { 1427 prev=null;
1017 // short hash_head = 0; // head of the hash chain 1428 window=null;
1018 int hash_head = 0; // head of the hash chain 1429 // free
1019 boolean bflush; // set if current block must be flushed 1430 // dstate=null;
1020 1431 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1021 while (true) { 1432 }
1022 // Make sure that we always have enough lookahead, except 1433
1023 // at the end of the input file. We need MAX_MATCH bytes 1434 int deflateParams(int _level, int _strategy){
1024 // for the next match, plus MIN_MATCH bytes to insert the 1435 int err=Z_OK;
1025 // string following the next match. 1436
1026 if (lookahead < MIN_LOOKAHEAD) { 1437 if(_level == Z_DEFAULT_COMPRESSION){
1027 fill_window(); 1438 _level = 6;
1028 1439 }
1029 if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1440 if(_level < 0 || _level > 9 ||
1030 return NeedMore; 1441 _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
1031 } 1442 return Z_STREAM_ERROR;
1032 1443 }
1033 if (lookahead == 0) break; // flush the current block 1444
1034 } 1445 if(config_table[level].func!=config_table[_level].func &&
1035 1446 strm.total_in != 0) {
1036 // Insert the string window[strstart .. strstart+2] in the 1447 // Flush the last buffer:
1037 // dictionary, and set hash_head to the head of the hash chain: 1448 err = strm.deflate(Z_PARTIAL_FLUSH);
1038 if (lookahead >= MIN_MATCH) { 1449 }
1039 ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; 1450
1040 // prev[strstart&w_mask]=hash_head=head[ins_h]; 1451 if(level != _level) {
1041 hash_head = (head[ins_h] & 0xffff); 1452 level = _level;
1042 prev[strstart & w_mask] = head[ins_h]; 1453 max_lazy_match = config_table[level].max_lazy;
1043 head[ins_h] = (short)strstart; 1454 good_match = config_table[level].good_length;
1044 } 1455 nice_match = config_table[level].nice_length;
1045 1456 max_chain_length = config_table[level].max_chain;
1046 // Find the longest match, discarding those <= prev_length. 1457 }
1047 // At this point we have always match_length < MIN_MATCH 1458 strategy = _strategy;
1048 if (hash_head != 0L && 1459 return err;
1049 ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD 1460 }
1050 ) { 1461
1051 // To simplify the code, we prevent matches with the string 1462 int deflateSetDictionary (byte[] dictionary, int dictLength){
1052 // of window index 0 (in particular we have to avoid a match 1463 int length = dictLength;
1053 // of the string with itself at the start of the input file). 1464 int index=0;
1054 if (strategy != Z_HUFFMAN_ONLY) { 1465
1055 match_length = longest_match(hash_head); 1466 if(dictionary == null || status != INIT_STATE)
1056 } 1467 return Z_STREAM_ERROR;
1057 1468
1058 // longest_match() sets match_start 1469 strm.adler.update(dictionary, 0, dictLength);
1059 } 1470
1060 1471 if(length < MIN_MATCH) return Z_OK;
1061 if (match_length >= MIN_MATCH) { 1472 if(length > w_size-MIN_LOOKAHEAD){
1062 // check_match(strstart, match_start, match_length); 1473 length = w_size-MIN_LOOKAHEAD;
1063 bflush = _tr_tally(strstart - match_start, match_length - MIN_MATCH); 1474 index=dictLength-length; // use the tail of the dictionary
1064 lookahead -= match_length; 1475 }
1065 1476 System.arraycopy(dictionary, index, window, 0, length);
1066 // Insert new strings in the hash table only if the match length 1477 strstart = length;
1067 // is not too large. This saves time but degrades compression. 1478 block_start = length;
1068 if (match_length <= max_lazy_match && 1479
1069 lookahead >= MIN_MATCH) { 1480 // Insert all strings in the hash table (except for the last two bytes).
1070 match_length--; // string at strstart already in hash table 1481 // s->lookahead stays null, so s->ins_h will be recomputed at the next
1071 1482 // call of fill_window.
1072 do { 1483
1073 strstart++; 1484 ins_h = window[0]&0xff;
1074 ins_h = ((ins_h << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; 1485 ins_h=(((ins_h)<<hash_shift)^(window[1]&0xff))&hash_mask;
1075 // prev[strstart&w_mask]=hash_head=head[ins_h]; 1486
1076 hash_head = (head[ins_h] & 0xffff); 1487 for(int n=0; n<=length-MIN_MATCH; n++){
1077 prev[strstart & w_mask] = head[ins_h]; 1488 ins_h=(((ins_h)<<hash_shift)^(window[(n)+(MIN_MATCH-1)]&0xff))&hash_mask;
1078 head[ins_h] = (short)strstart; 1489 prev[n&w_mask]=head[ins_h];
1079 // strstart never exceeds WSIZE-MAX_MATCH, so there are 1490 head[ins_h]=(short)n;
1080 // always MIN_MATCH bytes ahead. 1491 }
1081 } 1492 return Z_OK;
1082 while (--match_length != 0); 1493 }
1083 1494
1084 strstart++; 1495 int deflate(int flush){
1085 } 1496 int old_flush;
1086 else { 1497
1087 strstart += match_length; 1498 if(flush>Z_FINISH || flush<0){
1088 match_length = 0; 1499 return Z_STREAM_ERROR;
1089 ins_h = window[strstart] & 0xff; 1500 }
1090 ins_h = (((ins_h) << hash_shift) ^ (window[strstart + 1] & 0xff)) & hash_mask; 1501
1091 // If lookahead < MIN_MATCH, ins_h is garbage, but it does not 1502 if(strm.next_out == null ||
1092 // matter since it will be recomputed at next deflate call. 1503 (strm.next_in == null && strm.avail_in != 0) ||
1093 } 1504 (status == FINISH_STATE && flush != Z_FINISH)) {
1094 } 1505 strm.msg=z_errmsg[Z_NEED_DICT-(Z_STREAM_ERROR)];
1095 else { 1506 return Z_STREAM_ERROR;
1096 // No match, output a literal byte 1507 }
1097 bflush = _tr_tally(0, window[strstart] & 0xff); 1508 if(strm.avail_out == 0){
1098 lookahead--; 1509 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
1099 strstart++; 1510 return Z_BUF_ERROR;
1100 } 1511 }
1101 1512
1102 if (bflush) { 1513 old_flush = last_flush;
1103 flush_block_only(false); 1514 last_flush = flush;
1104 1515
1105 if (strm.avail_out == 0) return NeedMore; 1516 // Write the zlib header
1106 } 1517 if(status == INIT_STATE) {
1107 } 1518 if(wrap == 2){
1108 1519 getGZIPHeader().put(this);
1109 flush_block_only(flush == Z_FINISH); 1520 status=BUSY_STATE;
1110 1521 strm.adler.reset();
1111 if (strm.avail_out == 0) { 1522 }
1112 if (flush == Z_FINISH) return FinishStarted; 1523 else{
1113 else return NeedMore; 1524 int header = (Z_DEFLATED+((w_bits-8)<<4))<<8;
1114 } 1525 int level_flags=((level-1)&0xff)>>1;
1115 1526
1116 return flush == Z_FINISH ? FinishDone : BlockDone; 1527 if(level_flags>3) level_flags=3;
1117 } 1528 header |= (level_flags<<6);
1118 1529 if(strstart!=0) header |= PRESET_DICT;
1119 // Same as above, but achieves better compression. We use a lazy 1530 header+=31-(header % 31);
1120 // evaluation for matches: a match is finally adopted only if there is 1531
1121 // no better match at the next window position. 1532 status=BUSY_STATE;
1122 int deflate_slow(int flush) { 1533 putShortMSB(header);
1123 // short hash_head = 0; // head of hash chain 1534
1124 int hash_head = 0; // head of hash chain 1535
1125 boolean bflush; // set if current block must be flushed 1536 // Save the adler32 of the preset dictionary:
1126 1537 if(strstart!=0){
1127 // Process the input block. 1538 long adler=strm.adler.getValue();
1128 while (true) { 1539 putShortMSB((int)(adler>>>16));
1129 // Make sure that we always have enough lookahead, except 1540 putShortMSB((int)(adler&0xffff));
1130 // at the end of the input file. We need MAX_MATCH bytes 1541 }
1131 // for the next match, plus MIN_MATCH bytes to insert the 1542 strm.adler.reset();
1132 // string following the next match. 1543 }
1133 if (lookahead < MIN_LOOKAHEAD) { 1544 }
1134 fill_window(); 1545
1135 1546 // Flush as much pending output as possible
1136 if (lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { 1547 if(pending != 0) {
1137 return NeedMore; 1548 strm.flush_pending();
1138 } 1549 if(strm.avail_out == 0) {
1139 1550 // Since avail_out is 0, deflate will be called again with
1140 if (lookahead == 0) break; // flush the current block 1551 // more output space, but possibly with both pending and
1141 } 1552 // avail_in equal to zero. There won't be anything to do,
1142 1553 // but this is not an error situation so make sure we
1143 // Insert the string window[strstart .. strstart+2] in the 1554 // return OK instead of BUF_ERROR at next call of deflate:
1144 // dictionary, and set hash_head to the head of the hash chain: 1555 last_flush = -1;
1145 if (lookahead >= MIN_MATCH) { 1556 return Z_OK;
1146 ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; 1557 }
1147 // prev[strstart&w_mask]=hash_head=head[ins_h]; 1558
1148 hash_head = (head[ins_h] & 0xffff); 1559 // Make sure there is something to do and avoid duplicate consecutive
1149 prev[strstart & w_mask] = head[ins_h]; 1560 // flushes. For repeated and useless calls with Z_FINISH, we keep
1150 head[ins_h] = (short)strstart; 1561 // returning Z_STREAM_END instead of Z_BUFF_ERROR.
1151 } 1562 }
1152 1563 else if(strm.avail_in==0 && flush <= old_flush &&
1153 // Find the longest match, discarding those <= prev_length. 1564 flush != Z_FINISH) {
1154 prev_length = match_length; prev_match = match_start; 1565 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
1155 match_length = MIN_MATCH - 1; 1566 return Z_BUF_ERROR;
1156 1567 }
1157 if (hash_head != 0 && prev_length < max_lazy_match && 1568
1158 ((strstart - hash_head) & 0xffff) <= w_size - MIN_LOOKAHEAD 1569 // User must not provide more input after the first FINISH:
1159 ) { 1570 if(status == FINISH_STATE && strm.avail_in != 0) {
1160 // To simplify the code, we prevent matches with the string 1571 strm.msg=z_errmsg[Z_NEED_DICT-(Z_BUF_ERROR)];
1161 // of window index 0 (in particular we have to avoid a match 1572 return Z_BUF_ERROR;
1162 // of the string with itself at the start of the input file). 1573 }
1163 if (strategy != Z_HUFFMAN_ONLY) { 1574
1164 match_length = longest_match(hash_head); 1575 // Start a new block or continue the current one.
1165 } 1576 if(strm.avail_in!=0 || lookahead!=0 ||
1166 1577 (flush != Z_NO_FLUSH && status != FINISH_STATE)) {
1167 // longest_match() sets match_start 1578 int bstate=-1;
1168 if (match_length <= 5 && (strategy == Z_FILTERED || 1579 switch(config_table[level].func){
1169 (match_length == MIN_MATCH && 1580 case STORED:
1170 strstart - match_start > 4096))) { 1581 bstate = deflate_stored(flush);
1171 // If prev_match is also MIN_MATCH, match_start is garbage 1582 break;
1172 // but we will ignore the current match anyway. 1583 case FAST:
1173 match_length = MIN_MATCH - 1; 1584 bstate = deflate_fast(flush);
1174 } 1585 break;
1175 } 1586 case SLOW:
1176 1587 bstate = deflate_slow(flush);
1177 // If there was a match at the previous step and the current 1588 break;
1178 // match is not better, output the previous match: 1589 default:
1179 if (prev_length >= MIN_MATCH && match_length <= prev_length) { 1590 }
1180 int max_insert = strstart + lookahead - MIN_MATCH; 1591
1181 // Do not insert strings in hash table beyond this. 1592 if (bstate==FinishStarted || bstate==FinishDone) {
1182 // check_match(strstart-1, prev_match, prev_length); 1593 status = FINISH_STATE;
1183 bflush = _tr_tally(strstart - 1 - prev_match, prev_length - MIN_MATCH); 1594 }
1184 // Insert in hash table all strings up to the end of the match. 1595 if (bstate==NeedMore || bstate==FinishStarted) {
1185 // strstart-1 and strstart are already inserted. If there is not 1596 if(strm.avail_out == 0) {
1186 // enough lookahead, the last two strings are not inserted in 1597 last_flush = -1; // avoid BUF_ERROR next call, see above
1187 // the hash table. 1598 }
1188 lookahead -= prev_length - 1; 1599 return Z_OK;
1189 prev_length -= 2; 1600 // If flush != Z_NO_FLUSH && avail_out == 0, the next call
1190 1601 // of deflate should use the same flush parameter to make sure
1191 do { 1602 // that the flush is complete. So we don't have to output an
1192 if (++strstart <= max_insert) { 1603 // empty block here, this will be done at next call. This also
1193 ins_h = (((ins_h) << hash_shift) ^ (window[(strstart) + (MIN_MATCH - 1)] & 0xff)) & hash_mask; 1604 // ensures that for a very small output buffer, we emit at most
1194 //prev[strstart&w_mask]=hash_head=head[ins_h]; 1605 // one empty block.
1195 hash_head = (head[ins_h] & 0xffff); 1606 }
1196 prev[strstart & w_mask] = head[ins_h]; 1607
1197 head[ins_h] = (short)strstart; 1608 if (bstate==BlockDone) {
1198 } 1609 if(flush == Z_PARTIAL_FLUSH) {
1199 } 1610 _tr_align();
1200 while (--prev_length != 0); 1611 }
1201 1612 else { // FULL_FLUSH or SYNC_FLUSH
1202 match_available = 0; 1613 _tr_stored_block(0, 0, false);
1203 match_length = MIN_MATCH - 1; 1614 // For a full flush, this empty block will be recognized
1204 strstart++; 1615 // as a special marker by inflate_sync().
1205 1616 if(flush == Z_FULL_FLUSH) {
1206 if (bflush) { 1617 //state.head[s.hash_size-1]=0;
1207 flush_block_only(false); 1618 for(int i=0; i<hash_size/*-1*/; i++) // forget history
1208 1619 head[i]=0;
1209 if (strm.avail_out == 0) return NeedMore; 1620 }
1210 } 1621 }
1211 } 1622 strm.flush_pending();
1212 else if (match_available != 0) { 1623 if(strm.avail_out == 0) {
1213 // If there was no match at the previous position, output a 1624 last_flush = -1; // avoid BUF_ERROR at next call, see above
1214 // single literal. If there was a match but the current match 1625 return Z_OK;
1215 // is longer, truncate the previous match to a single literal. 1626 }
1216 bflush = _tr_tally(0, window[strstart - 1] & 0xff); 1627 }
1217 1628 }
1218 if (bflush) { 1629
1219 flush_block_only(false); 1630 if(flush!=Z_FINISH) return Z_OK;
1220 } 1631 if(wrap<=0) return Z_STREAM_END;
1221 1632
1222 strstart++; 1633 if(wrap==2){
1223 lookahead--; 1634 long adler=strm.adler.getValue();
1224 1635 put_byte((byte)(adler&0xff));
1225 if (strm.avail_out == 0) return NeedMore; 1636 put_byte((byte)((adler>>8)&0xff));
1226 } 1637 put_byte((byte)((adler>>16)&0xff));
1227 else { 1638 put_byte((byte)((adler>>24)&0xff));
1228 // There is no previous match to compare with, wait for 1639 put_byte((byte)(strm.total_in&0xff));
1229 // the next step to decide. 1640 put_byte((byte)((strm.total_in>>8)&0xff));
1230 match_available = 1; 1641 put_byte((byte)((strm.total_in>>16)&0xff));
1231 strstart++; 1642 put_byte((byte)((strm.total_in>>24)&0xff));
1232 lookahead--; 1643
1233 } 1644 getGZIPHeader().setCRC(adler);
1234 } 1645 }
1235 1646 else{
1236 if (match_available != 0) { 1647 // Write the zlib trailer (adler32)
1237 bflush = _tr_tally(0, window[strstart - 1] & 0xff); 1648 long adler=strm.adler.getValue();
1238 match_available = 0; 1649 putShortMSB((int)(adler>>>16));
1239 } 1650 putShortMSB((int)(adler&0xffff));
1240 1651 }
1241 flush_block_only(flush == Z_FINISH); 1652
1242 1653 strm.flush_pending();
1243 if (strm.avail_out == 0) { 1654
1244 if (flush == Z_FINISH) return FinishStarted; 1655 // If avail_out is zero, the application will call deflate again
1245 else return NeedMore; 1656 // to flush the rest.
1246 } 1657
1247 1658 if(wrap > 0) wrap = -wrap; // write the trailer only once!
1248 return flush == Z_FINISH ? FinishDone : BlockDone; 1659 return pending != 0 ? Z_OK : Z_STREAM_END;
1249 } 1660 }
1250 1661
1251 int longest_match(int cur_match) { 1662 static int deflateCopy(ZStream dest, ZStream src){
1252 int chain_length = max_chain_length; // max hash chain length 1663
1253 int scan = strstart; // current string 1664 if(src.dstate == null){
1254 int match; // matched string 1665 return Z_STREAM_ERROR;
1255 int len; // length of current match 1666 }
1256 int best_len = prev_length; // best match length so far 1667
1257 int limit = strstart > (w_size - MIN_LOOKAHEAD) ? 1668 if(src.next_in!=null){
1258 strstart - (w_size - MIN_LOOKAHEAD) : 0; 1669 dest.next_in = new byte[src.next_in.length];
1259 int nice_match = this.nice_match; 1670 System.arraycopy(src.next_in, 0, dest.next_in, 0, src.next_in.length);
1260 // Stop when cur_match becomes <= limit. To simplify the code, 1671 }
1261 // we prevent matches with the string of window index 0. 1672 dest.next_in_index = src.next_in_index;
1262 int wmask = w_mask; 1673 dest.avail_in = src.avail_in;
1263 int strend = strstart + MAX_MATCH; 1674 dest.total_in = src.total_in;
1264 byte scan_end1 = window[scan + best_len - 1]; 1675
1265 byte scan_end = window[scan + best_len]; 1676 if(src.next_out!=null){
1266 1677 dest.next_out = new byte[src.next_out.length];
1267 // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. 1678 System.arraycopy(src.next_out, 0, dest.next_out ,0 , src.next_out.length);
1268 // It is easy to get rid of this optimization if necessary. 1679 }
1269 // Do not waste too much time if we already have a good match: 1680
1270 if (prev_length >= good_match) { 1681 dest.next_out_index = src.next_out_index;
1271 chain_length >>= 2; 1682 dest.avail_out = src.avail_out;
1272 } 1683 dest.total_out = src.total_out;
1273 1684
1274 // Do not look for matches beyond the end of the input. This is necessary 1685 dest.msg = src.msg;
1275 // to make deflate deterministic. 1686 dest.data_type = src.data_type;
1276 if (nice_match > lookahead) nice_match = lookahead; 1687 dest.adler = src.adler.copy();
1277 1688
1278 do { 1689 try{
1279 match = cur_match; 1690 dest.dstate = (Deflate)src.dstate.clone();
1280 1691 dest.dstate.strm = dest;
1281 // Skip to next match if the match length cannot increase 1692 }
1282 // or if the match length is less than 2: 1693 catch(CloneNotSupportedException e){
1283 if (window[match + best_len] != scan_end || 1694 //
1284 window[match + best_len - 1] != scan_end1 || 1695 }
1285 window[match] != window[scan] || 1696 return Z_OK;
1286 window[++match] != window[scan + 1]) continue; 1697 }
1287 1698
1288 // The check at best_len-1 can be removed because it will be made 1699 public Object clone() throws CloneNotSupportedException {
1289 // again later. (This heuristic is not always a win.) 1700 Deflate dest = (Deflate)super.clone();
1290 // It is not necessary to compare scan[2] and match[2] since they 1701
1291 // are always equal when the other bytes match, given that 1702 dest.pending_buf = dup(dest.pending_buf);
1292 // the hash keys are equal and that HASH_BITS >= 8. 1703 dest.d_buf = dest.d_buf;
1293 scan += 2; match++; 1704 dest.l_buf = dup(dest.l_buf);
1294 1705 dest.window = dup(dest.window);
1295 // We check for insufficient lookahead only every 8th comparison; 1706
1296 // the 256th check will be made at strstart+258. 1707 dest.prev = dup(dest.prev);
1297 do { 1708 dest.head = dup(dest.head);
1298 } 1709 dest.dyn_ltree = dup(dest.dyn_ltree);
1299 while (window[++scan] == window[++match] && 1710 dest.dyn_dtree = dup(dest.dyn_dtree);
1300 window[++scan] == window[++match] && 1711 dest.bl_tree = dup(dest.bl_tree);
1301 window[++scan] == window[++match] && 1712
1302 window[++scan] == window[++match] && 1713 dest.bl_count = dup(dest.bl_count);
1303 window[++scan] == window[++match] && 1714 dest.next_code = dup(dest.next_code);
1304 window[++scan] == window[++match] && 1715 dest.heap = dup(dest.heap);
1305 window[++scan] == window[++match] && 1716 dest.depth = dup(dest.depth);
1306 window[++scan] == window[++match] && 1717
1307 scan < strend); 1718 dest.l_desc.dyn_tree = dest.dyn_ltree;
1308 1719 dest.d_desc.dyn_tree = dest.dyn_dtree;
1309 len = MAX_MATCH - (int)(strend - scan); 1720 dest.bl_desc.dyn_tree = dest.bl_tree;
1310 scan = strend - MAX_MATCH; 1721
1311 1722 /*
1312 if (len > best_len) { 1723 dest.l_desc.stat_desc = StaticTree.static_l_desc;
1313 match_start = cur_match; 1724 dest.d_desc.stat_desc = StaticTree.static_d_desc;
1314 best_len = len; 1725 dest.bl_desc.stat_desc = StaticTree.static_bl_desc;
1315 1726 */
1316 if (len >= nice_match) break; 1727
1317 1728 if(dest.gheader!=null){
1318 scan_end1 = window[scan + best_len - 1]; 1729 dest.gheader = (GZIPHeader)dest.gheader.clone();
1319 scan_end = window[scan + best_len]; 1730 }
1320 } 1731
1321 } 1732 return dest;
1322 while ((cur_match = (prev[cur_match & wmask] & 0xffff)) > limit 1733 }
1323 && --chain_length != 0); 1734
1324 1735 private byte[] dup(byte[] buf){
1325 if (best_len <= lookahead) return best_len; 1736 byte[] foo = new byte[buf.length];
1326 1737 System.arraycopy(buf, 0, foo, 0, foo.length);
1327 return lookahead; 1738 return foo;
1328 } 1739 }
1329 1740 private short[] dup(short[] buf){
1330 int deflateInit(ZStream strm, int level, int bits) { 1741 short[] foo = new short[buf.length];
1331 return deflateInit2(strm, level, Z_DEFLATED, bits, DEF_MEM_LEVEL, 1742 System.arraycopy(buf, 0, foo, 0, foo.length);
1332 Z_DEFAULT_STRATEGY); 1743 return foo;
1333 } 1744 }
1334 int deflateInit(ZStream strm, int level) { 1745 private int[] dup(int[] buf){
1335 return deflateInit(strm, level, MAX_WBITS); 1746 int[] foo = new int[buf.length];
1336 } 1747 System.arraycopy(buf, 0, foo, 0, foo.length);
1337 int deflateInit2(ZStream strm, int level, int method, int windowBits, 1748 return foo;
1338 int memLevel, int strategy) { 1749 }
1339 int noheader = 0; 1750
1340 // byte[] my_version=ZLIB_VERSION; 1751 synchronized GZIPHeader getGZIPHeader(){
1341 // 1752 if(gheader==null){
1342 // if (version == null || version[0] != my_version[0] 1753 gheader = new GZIPHeader();
1343 // || stream_size != sizeof(z_stream)) { 1754 }
1344 // return Z_VERSION_ERROR; 1755 return gheader;
1345 // } 1756 }
1346 strm.msg = null;
1347
1348 if (level == Z_DEFAULT_COMPRESSION) level = 6;
1349
1350 if (windowBits < 0) { // undocumented feature: suppress zlib header
1351 noheader = 1;
1352 windowBits = -windowBits;
1353 }
1354
1355 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL ||
1356 method != Z_DEFLATED ||
1357 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
1358 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
1359 return Z_STREAM_ERROR;
1360 }
1361
1362 strm.dstate = (Deflate)this;
1363 this.noheader = noheader;
1364 w_bits = windowBits;
1365 w_size = 1 << w_bits;
1366 w_mask = w_size - 1;
1367 hash_bits = memLevel + 7;
1368 hash_size = 1 << hash_bits;
1369 hash_mask = hash_size - 1;
1370 hash_shift = ((hash_bits + MIN_MATCH - 1) / MIN_MATCH);
1371 window = new byte[w_size * 2];
1372 prev = new short[w_size];
1373 head = new short[hash_size];
1374 lit_bufsize = 1 << (memLevel + 6); // 16K elements by default
1375 // We overlay pending_buf and d_buf+l_buf. This works since the average
1376 // output size for (length,distance) codes is <= 24 bits.
1377 pending_buf = new byte[lit_bufsize * 4];
1378 pending_buf_size = lit_bufsize * 4;
1379 d_buf = lit_bufsize / 2;
1380 l_buf = (1 + 2) * lit_bufsize;
1381 this.level = level;
1382 //System.out.println("level="+level);
1383 this.strategy = strategy;
1384 this.method = (byte)method;
1385 return deflateReset(strm);
1386 }
1387
1388 int deflateReset(ZStream strm) {
1389 strm.total_in = strm.total_out = 0;
1390 strm.msg = null; //
1391 strm.data_type = Z_UNKNOWN;
1392 pending = 0;
1393 pending_out = 0;
1394
1395 if (noheader < 0) {
1396 noheader = 0; // was set to -1 by deflate(..., Z_FINISH);
1397 }
1398
1399 status = (noheader != 0) ? BUSY_STATE : INIT_STATE;
1400 strm.adler = strm._adler.adler32(0, null, 0, 0);
1401 last_flush = Z_NO_FLUSH;
1402 tr_init();
1403 lm_init();
1404 return Z_OK;
1405 }
1406
1407 int deflateEnd() {
1408 if (status != INIT_STATE && status != BUSY_STATE && status != FINISH_STATE) {
1409 return Z_STREAM_ERROR;
1410 }
1411
1412 // Deallocate in reverse order of allocations:
1413 pending_buf = null;
1414 head = null;
1415 prev = null;
1416 window = null;
1417 // free
1418 // dstate=null;
1419 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
1420 }
1421
1422 int deflateParams(ZStream strm, int _level, int _strategy) {
1423 int err = Z_OK;
1424
1425 if (_level == Z_DEFAULT_COMPRESSION) {
1426 _level = 6;
1427 }
1428
1429 if (_level < 0 || _level > 9 ||
1430 _strategy < 0 || _strategy > Z_HUFFMAN_ONLY) {
1431 return Z_STREAM_ERROR;
1432 }
1433
1434 if (config_table[level].func != config_table[_level].func &&
1435 strm.total_in != 0) {
1436 // Flush the last buffer:
1437 err = strm.deflate(Z_PARTIAL_FLUSH);
1438 }
1439
1440 if (level != _level) {
1441 level = _level;
1442 max_lazy_match = config_table[level].max_lazy;
1443 good_match = config_table[level].good_length;
1444 nice_match = config_table[level].nice_length;
1445 max_chain_length = config_table[level].max_chain;
1446 }
1447
1448 strategy = _strategy;
1449 return err;
1450 }
1451
1452 int deflateSetDictionary(ZStream strm, byte[] dictionary, int dictLength) {
1453 int length = dictLength;
1454 int index = 0;
1455
1456 if (dictionary == null || status != INIT_STATE)
1457 return Z_STREAM_ERROR;
1458
1459 strm.adler = strm._adler.adler32(strm.adler, dictionary, 0, dictLength);
1460
1461 if (length < MIN_MATCH) return Z_OK;
1462
1463 if (length > w_size - MIN_LOOKAHEAD) {
1464 length = w_size - MIN_LOOKAHEAD;
1465 index = dictLength - length; // use the tail of the dictionary
1466 }
1467
1468 System.arraycopy(dictionary, index, window, 0, length);
1469 strstart = length;
1470 block_start = length;
1471 // Insert all strings in the hash table (except for the last two bytes).
1472 // s->lookahead stays null, so s->ins_h will be recomputed at the next
1473 // call of fill_window.
1474 ins_h = window[0] & 0xff;
1475 ins_h = (((ins_h) << hash_shift) ^ (window[1] & 0xff)) & hash_mask;
1476
1477 for (int n = 0; n <= length - MIN_MATCH; n++) {
1478 ins_h = (((ins_h) << hash_shift) ^ (window[(n) + (MIN_MATCH - 1)] & 0xff)) & hash_mask;
1479 prev[n & w_mask] = head[ins_h];
1480 head[ins_h] = (short)n;
1481 }
1482
1483 return Z_OK;
1484 }
1485
1486 int deflate(ZStream strm, int flush) {
1487 int old_flush;
1488
1489 if (flush > Z_FINISH || flush < 0) {
1490 return Z_STREAM_ERROR;
1491 }
1492
1493 if (strm.next_out == null ||
1494 (strm.next_in == null && strm.avail_in != 0) ||
1495 (status == FINISH_STATE && flush != Z_FINISH)) {
1496 strm.msg = z_errmsg[Z_NEED_DICT - (Z_STREAM_ERROR)];
1497 return Z_STREAM_ERROR;
1498 }
1499
1500 if (strm.avail_out == 0) {
1501 strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
1502 return Z_BUF_ERROR;
1503 }
1504
1505 this.strm = strm; // just in case
1506 old_flush = last_flush;
1507 last_flush = flush;
1508
1509 // Write the zlib header
1510 if (status == INIT_STATE) {
1511 int header = (Z_DEFLATED + ((w_bits - 8) << 4)) << 8;
1512 int level_flags = ((level - 1) & 0xff) >> 1;
1513
1514 if (level_flags > 3) level_flags = 3;
1515
1516 header |= (level_flags << 6);
1517
1518 if (strstart != 0) header |= PRESET_DICT;
1519
1520 header += 31 - (header % 31);
1521 status = BUSY_STATE;
1522 putShortMSB(header);
1523
1524 // Save the adler32 of the preset dictionary:
1525 if (strstart != 0) {
1526 putShortMSB((int)(strm.adler >>> 16));
1527 putShortMSB((int)(strm.adler & 0xffff));
1528 }
1529
1530 strm.adler = strm._adler.adler32(0, null, 0, 0);
1531 }
1532
1533 // Flush as much pending output as possible
1534 if (pending != 0) {
1535 strm.flush_pending();
1536
1537 if (strm.avail_out == 0) {
1538 //System.out.println(" avail_out==0");
1539 // Since avail_out is 0, deflate will be called again with
1540 // more output space, but possibly with both pending and
1541 // avail_in equal to zero. There won't be anything to do,
1542 // but this is not an error situation so make sure we
1543 // return OK instead of BUF_ERROR at next call of deflate:
1544 last_flush = -1;
1545 return Z_OK;
1546 }
1547
1548 // Make sure there is something to do and avoid duplicate consecutive
1549 // flushes. For repeated and useless calls with Z_FINISH, we keep
1550 // returning Z_STREAM_END instead of Z_BUFF_ERROR.
1551 }
1552 else if (strm.avail_in == 0 && flush <= old_flush &&
1553 flush != Z_FINISH) {
1554 strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
1555 return Z_BUF_ERROR;
1556 }
1557
1558 // User must not provide more input after the first FINISH:
1559 if (status == FINISH_STATE && strm.avail_in != 0) {
1560 strm.msg = z_errmsg[Z_NEED_DICT - (Z_BUF_ERROR)];
1561 return Z_BUF_ERROR;
1562 }
1563
1564 // Start a new block or continue the current one.
1565 if (strm.avail_in != 0 || lookahead != 0 ||
1566 (flush != Z_NO_FLUSH && status != FINISH_STATE)) {
1567 int bstate = -1;
1568
1569 switch (config_table[level].func) {
1570 case STORED:
1571 bstate = deflate_stored(flush);
1572 break;
1573
1574 case FAST:
1575 bstate = deflate_fast(flush);
1576 break;
1577
1578 case SLOW:
1579 bstate = deflate_slow(flush);
1580 break;
1581
1582 default:
1583 }
1584
1585 if (bstate == FinishStarted || bstate == FinishDone) {
1586 status = FINISH_STATE;
1587 }
1588
1589 if (bstate == NeedMore || bstate == FinishStarted) {
1590 if (strm.avail_out == 0) {
1591 last_flush = -1; // avoid BUF_ERROR next call, see above
1592 }
1593
1594 return Z_OK;
1595 // If flush != Z_NO_FLUSH && avail_out == 0, the next call
1596 // of deflate should use the same flush parameter to make sure
1597 // that the flush is complete. So we don't have to output an
1598 // empty block here, this will be done at next call. This also
1599 // ensures that for a very small output buffer, we emit at most
1600 // one empty block.
1601 }
1602
1603 if (bstate == BlockDone) {
1604 if (flush == Z_PARTIAL_FLUSH) {
1605 _tr_align();
1606 }
1607 else { // FULL_FLUSH or SYNC_FLUSH
1608 _tr_stored_block(0, 0, false);
1609
1610 // For a full flush, this empty block will be recognized
1611 // as a special marker by inflate_sync().
1612 if (flush == Z_FULL_FLUSH) {
1613 //state.head[s.hash_size-1]=0;
1614 for (int i = 0; i < hash_size/*-1*/; i++) // forget history
1615 head[i] = 0;
1616 }
1617 }
1618
1619 strm.flush_pending();
1620
1621 if (strm.avail_out == 0) {
1622 last_flush = -1; // avoid BUF_ERROR at next call, see above
1623 return Z_OK;
1624 }
1625 }
1626 }
1627
1628 if (flush != Z_FINISH) return Z_OK;
1629
1630 if (noheader != 0) return Z_STREAM_END;
1631
1632 // Write the zlib trailer (adler32)
1633 putShortMSB((int)(strm.adler >>> 16));
1634 putShortMSB((int)(strm.adler & 0xffff));
1635 strm.flush_pending();
1636 // If avail_out is zero, the application will call deflate again
1637 // to flush the rest.
1638 noheader = -1; // write the trailer only once!
1639 return pending != 0 ? Z_OK : Z_STREAM_END;
1640 }
1641 } 1757 }