#include <zdeflate.h>
Public Member Functions | |
| ZipDeflate () | |
| ZipDeflate constructor. | |
| ~ZipDeflate () | |
| ZipDeflate destructor. | |
| INT32 | Init (z_stream *strm, INT32 level) |
| Initializes the internal stream state for compression. The fields zalloc, zfree and opaque must be initialized before by the caller. If zalloc and zfree are set to Z_NULL, deflateInit updates them to use default allocation functions. | |
| INT32 | deflate (z_stream *strm, INT32 flush) |
| Actually go and deflate the stream. | |
| INT32 | End (z_stream *strm) |
| End the deflation on this stream. | |
| INT32 | Init (z_stream *strm, INT32 level, INT32 method, INT32 windowBits, INT32 memLevel, INT32 strategy) |
| This is another version of deflate Init with more compression options. The fields next_in, zalloc and zfree must be initialized before by the caller. | |
| INT32 | Reset (z_stream *strm) |
| This function is equivalent to deflate End followed by deflateInit, but does not free and reallocate all the internal compression state. The stream will keep the same compression level and any other attributes that may have been set by deflate Init. | |
| INT32 | SetDictionary (z_stream *strm, const Bytef *dictionary, uInt dictLength) |
| Initializes the compression dictionary (history buffer) from the given byte sequence without producing any compressed output. This function must be called immediately after deflateInit or deflateInit2, before any call of deflate. The compressor and decompressor must use exactly the same dictionary (see inflateSetDictionary). The dictionary should consist of strings (byte sequences) that are likely to be encountered later in the data to be compressed, with the most commonly used strings preferably put towards the end of the dictionary. Using a dictionary is most useful when the data to be compressed is short and can be predicted with good accuracy; the data can then be compressed better than with the default empty dictionary. In this version of the library, only the last 32K bytes of the dictionary are used. Upon return of this function, strm->adler is set to the Adler32 value of the dictionary; the decompressor may later use this value to determine which dictionary has been used by the compressor. (The Adler32 value applies to the whole dictionary even if only a subset of the dictionary is actually used by the compressor.). | |
| INT32 | Params (z_stream *strm, INT32 level, INT32 strategy) |
| New for version 0.99 Dynamically update the compression level and compression strategy. This can be used to switch between compression and straight copy of the input data, or to switch to a different kind of input data requiring a different strategy. If the compression level is changed, the input available so far is compressed with the old level (and may be flushed); the new level will take effect only at the next call of deflate(). | |
Protected Member Functions | |
| void | ct_init (DeflateState *s) |
| INT32 | ct_tally (DeflateState *s, INT32 dist, INT32 lc) |
| ulg | ct_flush_block (DeflateState *s, char *buf, ulg stored_len, INT32 eof) |
| void | ct_stored_block (DeflateState *s, char *buf, ulg stored_len, INT32 eof) |
| void | _tr_init (DeflateState *s) |
| Initialize the tree data structures for a new zlib stream. | |
| INT32 | _tr_tally (DeflateState *s, unsigned dist, unsigned lc) |
| Save the match info and tally the frequency counts. Return true if the current block must be flushed. | |
| ulg | _tr_flush_block (DeflateState *s, charf *buf, ulg stored_len, INT32 eof) |
| void | _tr_align (DeflateState *s) |
| void | _tr_stored_block (DeflateState *s, charf *buf, ulg stored_len, INT32 eof) |
Private Member Functions | |
| CC_DECLARE_MEMDUMP (ZipDeflate) | |
| INT32 | deflate_stored (DeflateState *s, INT32 flush) |
| Copy without compression as much as possible from the input stream, return true if processing was terminated prematurely (no more input or output space). This function does not insert new strings in the dictionary since uncompressible data is probably not useful. This function is used only for the level=0 compression option. NOTE: this function should be optimized to avoid extra copying. | |
| INT32 | deflate_fast (DeflateState *s, INT32 flush) |
| Compress as much as possible from the input stream, return true if processing was terminated prematurely (no more input or output space). This function does not perform lazy evaluationof matches and inserts new strings in the dictionary only for unmatched strings or for short matches. It is used only for the fast compression options. | |
| INT32 | deflate_slow (DeflateState *s, INT32 flush) |
| Same as above, but achieves better compression. We use a lazy evaluation for matches: a match is finally adopted only if there is no better match at the next window position. | |
| void | fill_window (DeflateState *s) |
| Fill the window when the lookahead becomes insufficient. Updates strstart and lookahead. | |
| void | lm_init (DeflateState *s) |
| Initialize the "longest match" routines for a new zlib stream. | |
| uInt | longest_match (DeflateState *s, IPos cur_match) |
| Set match_start to the longest match starting at the given string and return its length. Matches shorter or equal to prev_length are discarded, in which case the result is equal to prev_length and match_start is garbage. IN assertions: cur_match is the head of the hash chain for the current string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 OUT assertion: the match length is not greater than s->lookahead. | |
| void | putShortMSB (DeflateState *s, uInt b) |
| Put a short the pending_out buffer. The 16-bit value is put in MSB order. IN assertion: the stream state is correct and there is enough room in the pending_out buffer. | |
| void | flush_pending (z_stream *strm) |
| Flush as much pending output as possible. All deflate() output goes through this function so some applications may wish to modify it to avoid allocating a large strm->next_out buffer and copying into it. (See also read_buf()). | |
| INT32 | read_buf (z_stream *strm, charf *buf, unsigned size) |
| Read a new buffer from the current input stream, update the adler32 and total number of bytes read. All deflate() input goes through this function so some applications may wish to modify it to avoid allocating a large strm->next_in buffer and copying from it. (See also flush_pending()). | |
| void | tr_static_init (void) |
| Initialize the various 'constant' tables. To do: do this at compile time. | |
| void | init_block (DeflateState *s) |
| Initialize a new block. | |
| void | pqdownheap (DeflateState *s, ct_data *tree, INT32 k) |
| Restore the heap property by moving down the tree starting at node k, exchanging a node with the smallest of its two sons if necessary, stopping when the heap property is re-established (each father smaller than its two sons). | |
| void | gen_bitlen (DeflateState *s, tree_desc *desc) |
| Compute the optimal bit lengths for a tree and update the total bit length for the current block. IN assertion: the fields freq and dad are set, heap[heap_max] and above are the tree nodes sorted by increasing frequency. OUT assertions: the field len is set to the optimal bit length, the array bl_count contains the frequencies for each bit length. The length opt_len is updated; static_len is also updated if stree is not null. | |
| void | gen_codes (ct_data *tree, INT32 max_code, ushf bl_count[]) |
| Generate the codes for a given tree and bit counts (which need not be optimal). IN assertion: the array bl_count contains the bit length statistics for the given tree and the field len is set for all tree elements. OUT assertion: the field code is set for all tree elements of non zero code length. | |
| void | build_tree (DeflateState *s, tree_desc *desc) |
| Construct one Huffman tree and assigns the code bit strings and lengths. Update the total bit length for the current block. IN assertion: the field freq is set for all tree elements. OUT assertions: the fields len and code are set to the optimal bit length and corresponding code. The length opt_len is updated; static_len is also updated if stree is not null. The field max_code is set. | |
| void | scan_tree (DeflateState *s, ct_data *tree, INT32 max_code) |
| Scan a literal or distance tree to determine the frequencies of the codes in the bit length tree. | |
| void | send_tree (DeflateState *s, ct_data *tree, INT32 max_code) |
| Send a literal or distance tree in compressed form, using the codes in bl_tree. | |
| INT32 | build_bl_tree (DeflateState *s) |
| Construct the Huffman tree for the bit lengths and return the index in bl_order of the last bit length code to send. | |
| void | send_all_trees (DeflateState *s, INT32 lcodes, INT32 dcodes, INT32 blcodes) |
| Send the header for a block using dynamic Huffman trees: the counts, the lengths of the bit length codes, the literal tree and the distance tree. IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. | |
| void | compress_block (DeflateState *s, ct_data *ltree, ct_data *dtree) |
| Send the block data compressed using the given Huffman trees. | |
| void | set_data_type (DeflateState *s) |
| Set the data type to ASCII or BINARY, using a crude approximation: binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise. IN assertion: the fields freq of dyn_ltree are set and the total of all frequencies does not exceed 64K (to fit in an INT32 on 16 bit machines). | |
| unsigned | bi_reverse (unsigned value, INT32 length) |
| Send a value on a given number of bits. IN assertion: length <= 16 and value fits in length bits.Reverse the first len bits of a code, using straightforward code (a faster method would use a table) IN assertion: 1 <= len <= 15. | |
| void | bi_windup (DeflateState *s) |
| Write out any remaining bits in an incomplete byte. Flush the bit buffer and align the output on a byte boundary. | |
| void | bi_flush (DeflateState *s) |
| Flush the bit buffer, keeping at most 7 bits in it. New Version 0.99. | |
| void | copy_block (DeflateState *s, char *buf, unsigned len, INT32 header) |
| Copy a stored block, storing first the length and its one's complement if requested. | |
Definition at line 409 of file zdeflate.h.
|
|
ZipDeflate constructor.
Definition at line 259 of file zdeflate.cpp.
|
|
|
ZipDeflate destructor.
Definition at line 273 of file zdeflate.cpp.
|
|
|
Definition at line 988 of file zdftrees.cpp. 00989 { 00990 send_bits(s, STATIC_TREES<<1, 3); 00991 send_code(s, END_BLOCK, static_ltree); 00992 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ 00993 bi_flush(s); 00994 /* Of the 10 bits for the empty block, we have already sent 00995 * (10 - bi_valid) bits. The lookahead for the last real code (before 00996 * the EOB of the previous block) was thus at least one plus the length 00997 * of the EOB plus what we have just sent of the empty static block. 00998 */ 00999 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) { 01000 send_bits(s, STATIC_TREES<<1, 3); 01001 send_code(s, END_BLOCK, static_ltree); 01002 s->compressed_len += 10L; 01003 bi_flush(s); 01004 } 01005 s->last_eob_len = 7; 01006 }
|
|
||||||||||||||||||||
|
|
|
|
Initialize the tree data structures for a new zlib stream.
Definition at line 405 of file zdftrees.cpp. 00406 { 00407 tr_static_init(); 00408 00409 s->compressed_len = 0L; 00410 00411 s->l_desc.dyn_tree = s->dyn_ltree; 00412 s->l_desc.stat_desc = &static_l_desc; 00413 00414 s->d_desc.dyn_tree = s->dyn_dtree; 00415 s->d_desc.stat_desc = &static_d_desc; 00416 00417 s->bl_desc.dyn_tree = s->bl_tree; 00418 s->bl_desc.stat_desc = &static_bl_desc; 00419 00420 s->bi_buf = 0; 00421 s->bi_valid = 0; 00422 s->last_eob_len = 8; /* enough lookahead for inflate */ 00423 #ifdef DEBUG 00424 s->bits_sent = 0L; 00425 #endif 00426 00427 /* Initialize the first block of the first file: */ 00428 init_block(s); 00429 }
|
|
||||||||||||||||||||
|
|
|
||||||||||||||||
|
Save the match info and tally the frequency counts. Return true if the current block must be flushed.
Definition at line 1143 of file zdftrees.cpp. 01144 { 01145 s->d_buf[s->last_lit] = (ush)dist; 01146 s->l_buf[s->last_lit++] = (uch)lc; 01147 if (dist == 0) { 01148 /* lc is the unmatched char */ 01149 s->dyn_ltree[lc].Freq++; 01150 } else { 01151 s->matches++; 01152 /* Here, lc is the match length - MIN_MATCH */ 01153 dist--; /* dist = match distance - 1 */ 01154 Assert((ush)dist < (ush)MAX_DIST(s) && 01155 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && 01156 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); 01157 01158 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++; 01159 s->dyn_dtree[d_code(dist)].Freq++; 01160 } 01161 01162 /* Try to guess if it is profitable to stop the current block here */ 01163 if (s->level > 2 && (s->last_lit & 0xfff) == 0) { 01164 /* Compute an upper bound for the compressed length */ 01165 ulg out_length = (ulg)s->last_lit*8L; 01166 ulg in_length = (ulg)((INT32)s->strstart - s->block_start); 01167 INT32 dcode; 01168 for (dcode = 0; dcode < D_CODES; dcode++) { 01169 out_length += (ulg)s->dyn_dtree[dcode].Freq * 01170 (5L+extra_dbits[dcode]); 01171 } 01172 out_length >>= 3; 01173 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", 01174 s->last_lit, in_length, out_length, 01175 100L - out_length*100L/in_length)); 01176 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; 01177 } 01178 return (s->last_lit == s->lit_bufsize-1); 01179 /* We avoid equality with lit_bufsize because of wraparound at 64K 01180 * on 16 bit machines and because stored blocks are restricted to 01181 * 64K-1 bytes. 01182 */ 01183 }
|
|
|
Flush the bit buffer, keeping at most 7 bits in it. New Version 0.99.
Definition at line 1340 of file zdftrees.cpp. 01341 { 01342 if (s->bi_valid == 16) { 01343 put_short(s, s->bi_buf); 01344 s->bi_buf = 0; 01345 s->bi_valid = 0; 01346 } else if (s->bi_valid >= 8) { 01347 put_byte(s, (Byte)s->bi_buf); 01348 s->bi_buf >>= 8; 01349 s->bi_valid -= 8; 01350 } 01351 }
|
|
||||||||||||
|
Send a value on a given number of bits. IN assertion: length <= 16 and value fits in length bits.Reverse the first len bits of a code, using straightforward code (a faster method would use a table) IN assertion: 1 <= len <= 15.
Definition at line 1318 of file zdftrees.cpp. 01319 { 01320 register unsigned res = 0; 01321 do { 01322 res |= code & 1; 01323 code >>= 1, res <<= 1; 01324 } while (--len > 0); 01325 return res >> 1; 01326 }
|
|
|
Write out any remaining bits in an incomplete byte. Flush the bit buffer and align the output on a byte boundary.
Definition at line 1365 of file zdftrees.cpp. 01366 { 01367 if (s->bi_valid > 8) { 01368 put_short(s, s->bi_buf); 01369 } else if (s->bi_valid > 0) { 01370 put_byte(s, (Byte)s->bi_buf); 01371 } 01372 s->bi_buf = 0; 01373 s->bi_valid = 0; 01374 #ifdef DEBUG 01375 s->bits_sent = (s->bits_sent+7) & ~7; 01376 #endif 01377 }
|
|
|
Construct the Huffman tree for the bit lengths and return the index in bl_order of the last bit length code to send.
Definition at line 886 of file zdftrees.cpp. 00887 { 00888 INT32 max_blindex; /* index of last bit length code of non zero freq */ 00889 00890 /* Determine the bit length frequencies for literal and distance trees */ 00891 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); 00892 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); 00893 00894 /* Build the bit length tree: */ 00895 build_tree(s, (tree_desc *)(&(s->bl_desc))); 00896 /* opt_len now includes the length of the tree representations, except 00897 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. 00898 */ 00899 00900 /* Determine the number of bit length codes to send. The pkzip format 00901 * requires that at least 4 bit length codes be sent. (appnote.txt says 00902 * 3 but the actual value used is 4.) 00903 */ 00904 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { 00905 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; 00906 } 00907 /* Update opt_len to include the bit length tree and counts */ 00908 s->opt_len += 3*(max_blindex+1) + 5+5+4; 00909 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", 00910 s->opt_len, s->static_len)); 00911 00912 return max_blindex; 00913 }
|
|
||||||||||||
|
Construct one Huffman tree and assigns the code bit strings and lengths. Update the total bit length for the current block. IN assertion: the field freq is set for all tree elements. OUT assertions: the fields len and code are set to the optimal bit length and corresponding code. The length opt_len is updated; static_len is also updated if stree is not null. The field max_code is set.
Definition at line 682 of file zdftrees.cpp. 00683 { 00684 ct_data *tree = desc->dyn_tree; 00685 ct_data *stree = desc->stat_desc->static_tree; 00686 INT32 elems = desc->stat_desc->elems; 00687 INT32 n, m; /* iterate over heap elements */ 00688 INT32 max_code = -1; /* largest code with non zero frequency */ 00689 INT32 node; /* new node being created */ 00690 00691 /* Construct the initial heap, with least frequent element in 00692 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. 00693 * heap[0] is not used. 00694 */ 00695 s->heap_len = 0, s->heap_max = HEAP_SIZE; 00696 00697 for (n = 0; n < elems; n++) { 00698 if (tree[n].Freq != 0) { 00699 s->heap[++(s->heap_len)] = max_code = n; 00700 s->depth[n] = 0; 00701 } else { 00702 tree[n].Len = 0; 00703 } 00704 } 00705 00706 /* The pkzip format requires that at least one distance code exists, 00707 * and that at least one bit should be sent even if there is only one 00708 * possible code. So to avoid special checks later on we force at least 00709 * two codes of non zero frequency. 00710 */ 00711 while (s->heap_len < 2) { 00712 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); 00713 tree[node].Freq = 1; 00714 s->depth[node] = 0; 00715 s->opt_len--; if (stree) s->static_len -= stree[node].Len; 00716 /* node is 0 or 1 so it does not have extra bits */ 00717 } 00718 desc->max_code = max_code; 00719 00720 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, 00721 * establish sub-heaps of increasing lengths: 00722 */ 00723 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); 00724 00725 /* Construct the Huffman tree by repeatedly combining the least two 00726 * frequent nodes. 00727 */ 00728 node = elems; /* next internal node of the tree */ 00729 do { 00730 pqremove(s, tree, n); /* n = node of least frequency */ 00731 m = s->heap[SMALLEST]; /* m = node of next least frequency */ 00732 00733 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ 00734 s->heap[--(s->heap_max)] = m; 00735 00736 /* Create a new node father of n and m */ 00737 tree[node].Freq = tree[n].Freq + tree[m].Freq; 00738 s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1); 00739 tree[n].Dad = tree[m].Dad = (ush)node; 00740 #ifdef DUMP_BL_TREE 00741 if (tree == s->bl_tree) { 00742 _ftprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", 00743 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); 00744 } 00745 #endif 00746 /* and insert the new node in the heap */ 00747 s->heap[SMALLEST] = node++; 00748 pqdownheap(s, tree, SMALLEST); 00749 00750 } while (s->heap_len >= 2); 00751 00752 s->heap[--(s->heap_max)] = s->heap[SMALLEST]; 00753 00754 /* At this point, the fields freq and dad are set. We can now 00755 * generate the bit lengths. 00756 */ 00757 gen_bitlen(s, (tree_desc *)desc); 00758 00759 /* The field len is now set, we can generate the bit codes */ 00760 gen_codes ((ct_data *)tree, max_code, s->bl_count); 00761 }
|
|
|
|
|
||||||||||||||||
|
Send the block data compressed using the given Huffman trees.
Definition at line 1198 of file zdftrees.cpp. 01199 { 01200 unsigned dist; /* distance of matched string */ 01201 INT32 lc; /* match length or unmatched char (if dist == 0) */ 01202 UINT32 lx = 0; /* running index in l_buf */ 01203 unsigned code; /* the code to send */ 01204 INT32 extra; /* number of extra bits to send */ 01205 01206 if (s->last_lit != 0) do { 01207 dist = s->d_buf[lx]; 01208 lc = s->l_buf[lx++]; 01209 if (dist == 0) { 01210 send_code(s, lc, ltree); /* send a literal byte */ 01211 Tracecv(_istgraph(lc), (stderr," '%c' ", lc)); 01212 } else { 01213 /* Here, lc is the match length - MIN_MATCH */ 01214 code = length_code[lc]; 01215 send_code(s, code+LITERALS+1, ltree); /* send the length code */ 01216 extra = extra_lbits[code]; 01217 if (extra != 0) { 01218 lc -= base_length[code]; 01219 send_bits(s, lc, extra); /* send the extra length bits */ 01220 } 01221 dist--; /* dist is now the match distance - 1 */ 01222 code = d_code(dist); 01223 Assert (code < D_CODES, "bad d_code"); 01224 01225 send_code(s, code, dtree); /* send the distance code */ 01226 extra = extra_dbits[code]; 01227 if (extra != 0) { 01228 dist -= base_dist[code]; 01229 send_bits(s, dist, extra); /* send the extra distance bits */ 01230 } 01231 } /* literal or match pair ? */ 01232 01233 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ 01234 Assert(s->pending < ( INT32 ) ( s->lit_bufsize + ( 2 * lx ) ), "pendingBuf overflow"); 01235 01236 } while (lx < s->last_lit); 01237 01238 send_code(s, END_BLOCK, ltree); 01239 s->last_eob_len = ltree[END_BLOCK].Len; 01240 }
|
|
||||||||||||||||||||
|
Copy a stored block, storing first the length and its one's complement if requested.
Definition at line 1394 of file zdftrees.cpp. 01395 { 01396 bi_windup(s); /* align on byte boundary */ 01397 s->last_eob_len = 8; /* enough lookahead for inflate */ 01398 01399 if (header) { 01400 put_short(s, (ush)len); 01401 put_short(s, (ush)~len); 01402 #ifdef DEBUG 01403 s->bits_sent += 2*16; 01404 #endif 01405 } 01406 #ifdef DEBUG 01407 s->bits_sent += (ulg)len<<3; 01408 #endif 01409 while (len--) { 01410 put_byte(s, *buf++); 01411 } 01412 }
|
|
||||||||||||||||||||
|
|
|
|
|
|
||||||||||||||||||||
|
|
|
||||||||||||||||
|
|
|
||||||||||||
|
Actually go and deflate the stream.
Before the call of deflate(), the application should ensure that at least one of the actions is possible, by providing more input and/or consuming more output, and updating avail_in or avail_out accordingly; avail_out should never be zero before the call. The application can consume the compressed output when it wants, for example when the output buffer is full (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK and with zero avail_out, it must be called again after making room in the output buffer because there might be more output pending. If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression block is terminated and flushed to the output buffer so that the decompressor can get all input data available so far. For method 9, a future variant on method 8, the current block will be flushed but not terminated. If flush is set to Z_FULL_FLUSH, the compression block is terminated, a special marker is output and the compression dictionary is discarded; this is useful to allow the decompressor to synchronize if one compressed block has been damaged (see inflateSync below). Flushing degrades compression and so should be used only when necessary. Using Z_FULL_FLUSH too often can seriously degrade the compression. If the parameter flush is set to Z_FINISH, all pending input is processed, all pending output is flushed and deflate returns with ZStream_END if there was enough output space; if deflate returns with Z_OK, this function must be called again with Z_FINISH and more output space (updated avail_out) but no more input data, until it returns with ZStream_END or an error. After deflate has returned ZStream_END, the only possible operations on the stream are deflateReset or deflateEnd. Z_FINISH can be used immediately after deflateInit if all the compression is to be done in a single step. In this case, avail_out must be at least 0.1% larger than avail_in plus 12 bytes. If deflate does not return ZStream_END, then it must be called again as described above. deflate() may update data_type if it can make a good guess about the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered binary. This field is only for information purposes and does not affect the compression algorithm in any manner. deflate() returns Z_OK if some progress has been made (more input processed or more output produced), ZStream_END if all input has been consumed and all output has been produced (only when flush is set to Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible. Definition at line 779 of file zdeflate.cpp. 00780 { 00781 INT32 old_flush; /* value of flush param for previous deflate call */ 00782 DeflateState *s; 00783 00784 if (strm == Z_NULL || strm->De_state == Z_NULL || flush > Z_FINISH || flush < 0) 00785 { 00786 return Z_STREAM_ERROR; 00787 } 00788 00789 s = strm->De_state; 00790 00791 if (strm->next_out == Z_NULL || 00792 (strm->next_in == Z_NULL && strm->avail_in != 0) || 00793 (s->status == FINISH_STATE && flush != Z_FINISH)) 00794 { 00795 ERR_RETURN(strm, Z_STREAM_ERROR); 00796 } 00797 00798 if (strm->avail_out == 0) 00799 ERR_RETURN(strm, Z_BUF_ERROR); 00800 00801 strm->De_state->strm = strm; /* just in case */ 00802 old_flush = s->last_flush; 00803 s->last_flush = flush; 00804 00805 /* Write the zlib header */ 00806 if (s->status == INIT_STATE) 00807 { 00808 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; 00809 uInt level_flags = (s->level-1) >> 1; 00810 00811 if (level_flags > 3) 00812 level_flags = 3; 00813 header |= (level_flags << 6); 00814 00815 if (s->strstart != 0) 00816 header |= PRESET_DICT; 00817 header += 31 - (header % 31); 00818 00819 s->status = BUSY_STATE; 00820 putShortMSB(s, header); 00821 00822 /* Save the adler32 of the preset dictionary: */ 00823 if (s->strstart != 0) 00824 { 00825 putShortMSB(s, (uInt)(strm->adler >> 16)); 00826 putShortMSB(s, (uInt)(strm->adler & 0xffff)); 00827 } 00828 strm->adler = 1L; 00829 } 00830 00831 /* Flush as much pending output as possible */ 00832 if (s->pending != 0) 00833 { 00834 flush_pending(strm); 00835 if (strm->avail_out == 0) 00836 { 00837 /* Since avail_out is 0, deflate will be called again with 00838 * more output space, but possibly with both pending and 00839 * avail_in equal to zero. There won't be anything to do, 00840 * but this is not an error situation so make sure we 00841 * return OK instead of BUF_ERROR at next call of deflate: 00842 */ 00843 s->last_flush = -1; 00844 return Z_OK; 00845 } 00846 00847 /* Make sure there is something to do and avoid duplicate consecutive 00848 * flushes. For repeated and useless calls with Z_FINISH, we keep 00849 * returning Z_STREAM_END instead of Z_BUFF_ERROR. 00850 */ 00851 } 00852 else if (strm->avail_in == 0 && flush <= old_flush && flush != Z_FINISH) 00853 { 00854 ERR_RETURN(strm, Z_BUF_ERROR); 00855 } 00856 00857 /* User must not provide more input after the first FINISH: */ 00858 if (s->status == FINISH_STATE && strm->avail_in != 0) 00859 { 00860 ERR_RETURN(strm, Z_BUF_ERROR); 00861 } 00862 00863 /* Start a new block or continue the current one. 00864 */ 00865 if (strm->avail_in != 0 || s->lookahead != 0 || 00866 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) 00867 { 00868 INT32 quit; 00869 00870 if (flush == Z_FINISH) 00871 { 00872 s->status = FINISH_STATE; 00873 } 00874 00875 //quit = (*(configuration_table[s->level].func))(s, flush); 00876 switch (configuration_table[s->level].func) 00877 { 00878 case Z_deflate_stored : 00879 quit = deflate_stored(s, flush); 00880 break; 00881 case Z_deflate_fast : 00882 quit = deflate_fast(s, flush); 00883 break; 00884 case Z_deflate_slow : 00885 quit = deflate_slow(s, flush); 00886 break; 00887 default: 00888 // We have a type we don't understand so error 00889 return Z_STREAM_ERROR; 00890 } 00891 00892 if (strm->avail_out == 0) 00893 { 00894 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 00895 } 00896 00897 if (quit || strm->avail_out == 0) 00898 return Z_OK; 00899 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call 00900 * of deflate should use the same flush parameter to make sure 00901 * that the flush is complete. So we don't have to output an 00902 * empty block here, this will be done at next call. This also 00903 * ensures that for a very small output buffer, we emit at most 00904 * one empty block. 00905 */ 00906 if (flush != Z_NO_FLUSH && flush != Z_FINISH) 00907 { 00908 if (flush == Z_PARTIAL_FLUSH) 00909 { 00910 _tr_align(s); 00911 } 00912 else 00913 { 00914 /* FULL_FLUSH or SYNC_FLUSH */ 00915 _tr_stored_block(s, (char*)0, 0L, 0); 00916 /* For a full flush, this empty block will be recognized 00917 * as a special marker by inflate_sync(). 00918 */ 00919 if (flush == Z_FULL_FLUSH) 00920 { 00921 CLEAR_HASH(s); /* forget history */ 00922 } 00923 } 00924 00925 flush_pending(strm); 00926 00927 if (strm->avail_out == 0) 00928 { 00929 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ 00930 return Z_OK; 00931 } 00932 } 00933 } 00934 Assert(strm->avail_out > 0, "bug2"); 00935 00936 if (flush != Z_FINISH) 00937 return Z_OK; 00938 if (s->noheader) 00939 return Z_STREAM_END; 00940 00941 /* Write the zlib trailer (adler32) */ 00942 putShortMSB(s, (uInt)(strm->adler >> 16)); 00943 putShortMSB(s, (uInt)(strm->adler & 0xffff)); 00944 flush_pending(strm); 00945 /* If avail_out is zero, the application will call deflate again 00946 * to flush the rest. 00947 */ 00948 s->noheader = -1; /* write the trailer only once! */ 00949 return s->pending != 0 ? Z_OK : Z_STREAM_END; 00950 }
|
|
||||||||||||
|
Compress as much as possible from the input stream, return true if processing was terminated prematurely (no more input or output space). This function does not perform lazy evaluationof matches and inserts new strings in the dictionary only for unmatched strings or for short matches. It is used only for the fast compression options.
Definition at line 1494 of file zdeflate.cpp. 01495 { 01496 IPos hash_head = NIL; /* head of the hash chain */ 01497 INT32 bflush; /* set if current block must be flushed */ 01498 01499 for (;;) { 01500 /* Make sure that we always have enough lookahead, except 01501 * at the end of the input file. We need MAX_MATCH bytes 01502 * for the next match, plus MIN_MATCH bytes to insert the 01503 * string following the next match. 01504 */ 01505 if (s->lookahead < MIN_LOOKAHEAD) { 01506 fill_window(s); 01507 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; 01508 01509 if (s->lookahead == 0) break; /* flush the current block */ 01510 } 01511 01512 /* Insert the string window[strstart .. strstart+2] in the 01513 * dictionary, and set hash_head to the head of the hash chain: 01514 */ 01515 if (s->lookahead >= MIN_MATCH) { 01516 INSERT_STRING(s, s->strstart, hash_head); 01517 } 01518 01519 /* Find the longest match, discarding those <= prev_length. 01520 * At this point we have always match_length < MIN_MATCH 01521 */ 01522 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { 01523 /* To simplify the code, we prevent matches with the string 01524 * of window index 0 (in particular we have to avoid a match 01525 * of the string with itself at the start of the input file). 01526 */ 01527 if (s->strategy != Z_HUFFMAN_ONLY) { 01528 s->match_length = longest_match (s, hash_head); 01529 } 01530 /* longest_match() sets match_start */ 01531 } 01532 if (s->match_length >= MIN_MATCH) { 01533 check_match(s, s->strstart, s->match_start, s->match_length); 01534 01535 bflush = _tr_tally(s, s->strstart - s->match_start, 01536 s->match_length - MIN_MATCH); 01537 01538 s->lookahead -= s->match_length; 01539 01540 /* Insert new strings in the hash table only if the match length 01541 * is not too large. This saves time but degrades compression. 01542 */ 01543 if (s->match_length <= s->max_insert_length && 01544 s->lookahead >= MIN_MATCH) { 01545 s->match_length--; /* string at strstart already in hash table */ 01546 do { 01547 s->strstart++; 01548 INSERT_STRING(s, s->strstart, hash_head); 01549 /* strstart never exceeds WSIZE-MAX_MATCH, so there are 01550 * always MIN_MATCH bytes ahead. 01551 */ 01552 } while (--s->match_length != 0); 01553 s->strstart++; 01554 } else { 01555 s->strstart += s->match_length; 01556 s->match_length = 0; 01557 s->ins_h = s->window[s->strstart]; 01558 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); 01559 #if MIN_MATCH != 3 01560 Call UPDATE_HASH() MIN_MATCH-3 more times 01561 #endif 01562 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not 01563 * matter since it will be recomputed at next deflate call. 01564 */ 01565 } 01566 } else { 01567 /* No match, output a literal byte */ 01568 Tracevv((stderr,"%c", s->window[s->strstart])); 01569 bflush = _tr_tally (s, 0, s->window[s->strstart]); 01570 s->lookahead--; 01571 s->strstart++; 01572 } 01573 if (bflush) FLUSH_BLOCK(s, 0); 01574 } 01575 FLUSH_BLOCK(s, flush == Z_FINISH); 01576 return 0; /* normal exit */ 01577 }
|
|
||||||||||||
|
Same as above, but achieves better compression. We use a lazy evaluation for matches: a match is finally adopted only if there is no better match at the next window position.
Definition at line 1591 of file zdeflate.cpp. 01592 { 01593 IPos hash_head = NIL; /* head of hash chain */ 01594 INT32 bflush; /* set if current block must be flushed */ 01595 01596 /* Process the input block. */ 01597 for (;;) { 01598 /* Make sure that we always have enough lookahead, except 01599 * at the end of the input file. We need MAX_MATCH bytes 01600 * for the next match, plus MIN_MATCH bytes to insert the 01601 * string following the next match. 01602 */ 01603 if (s->lookahead < MIN_LOOKAHEAD) { 01604 fill_window(s); 01605 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) return 1; 01606 01607 if (s->lookahead == 0) break; /* flush the current block */ 01608 } 01609 01610 /* Insert the string window[strstart .. strstart+2] in the 01611 * dictionary, and set hash_head to the head of the hash chain: 01612 */ 01613 if (s-> |