zdeflate.h

Go to the documentation of this file.
00001 // $Id: zdeflate.h 751 2006-03-31 15:43:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 
00099 // This class contains all the file deflating code
00100 
00101 #ifndef INC_ZIPDEFLATE
00102 #define INC_ZIPDEFLATE
00103 
00104 #include "zutil.h"
00105 
00106 //#include "zstream.h"
00107 
00108 /* ===========================================================================
00109  * Internal compression state.
00110  */
00111 
00112 #define LENGTH_CODES 29
00113 /* number of length codes, not counting the special END_BLOCK code */
00114 
00115 #define LITERALS  256
00116 /* number of literal bytes 0..255 */
00117 
00118 #define L_CODES (LITERALS+1+LENGTH_CODES)
00119 /* number of Literal or Length codes, including the END_BLOCK code */
00120 
00121 #define D_CODES   30
00122 /* number of distance codes */
00123 
00124 #define BL_CODES  19
00125 /* number of codes used to transfer the bit lengths */
00126 
00127 #define HEAP_SIZE (2*L_CODES+1)
00128 /* maximum heap size */
00129 
00130 #define MAX_BITS 15
00131 /* All codes must not exceed MAX_BITS bits */
00132 
00133 #define INIT_STATE    42
00134 #define BUSY_STATE   113
00135 #define FINISH_STATE 666
00136 /* Stream status */
00137 
00138 
00139 /* Data structure describing a single value and its code string. */
00140 typedef struct ct_data_s {
00141     union {
00142         ush  freq;       /* frequency count */
00143         ush  code;       /* bit string */
00144     } fc;
00145     union {
00146         ush  dad;        /* father node in Huffman tree */
00147         ush  len;        /* length of bit string */
00148     } dl;
00149 } ct_data;
00150 
00151 typedef struct static_tree_desc_s  static_tree_desc;
00152 
00153 typedef struct tree_desc_s {
00154     ct_data *dyn_tree;           /* the dynamic tree */
00155     INT32     max_code;            /* largest code with non zero frequency */
00156     static_tree_desc *stat_desc; /* the corresponding static tree */
00157 } tree_desc;
00158 
00159 typedef ush Pos;
00160 typedef Pos FAR Posf;
00161 typedef unsigned IPos;
00162 
00163 /* A Pos is an index in the character window. We use short instead of INT32 to
00164  * save space in the various tables. IPos is used only for parameter passing.
00165  */
00166 
00167 /* Output a byte on the stream.
00168  * IN assertion: there is enough room in pending_buf.
00169  */
00170 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
00171 
00172 
00173 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
00174 /* Minimum amount of lookahead, except at the end of the input file.
00175  * See deflate.c for comments about the MIN_MATCH+1.
00176  */
00177 
00178 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
00179 /* In order to simplify the code, particularly on 16 bit machines, match
00180  * distances are limited to MAX_DIST instead of WSIZE.
00181  */
00182 
00183 class ZStream;
00184 
00185 /********************************************************************************************
00186 
00187 >   class DeflateState : public CCObject
00188 
00189     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com> Humprhys
00190     Created:    23/5/95
00191     Purpose:    A deflate state object for the deflater for the file compressor and decompressor.
00192 
00193 ********************************************************************************************/
00194 
00195 class DeflateState : public CC_CLASS_MEMDUMP
00196 {
00197     // Give my name in memory dumps
00198     // If you need a different sort of decare, replace this one. 
00199     // See CCObject in the help file for info about DECLARE types
00200     CC_DECLARE_MEMDUMP(DeflateState);
00201 
00202 public:
00203     DeflateState();
00204     ~DeflateState();
00205 
00206 public:
00207     ZStream *strm;      /* pointer back to this zlib stream */
00208     INT32   status;        /* as the name implies */
00209     Bytef *pending_buf;   /* output still pending */
00210     Bytef *pending_out;   /* next pending byte to output to the stream */
00211     INT32   pending;       /* nb of bytes in the pending buffer */
00212  // removed verison 0.99
00213  //   uLong adler;         /* adler32 of uncompressed data */
00214     INT32   noheader;      /* suppress zlib header and adler32 */
00215     Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
00216     Byte  method;        /* STORED (for zip only) or DEFLATED */
00217     INT32   last_flush;    /* value of flush param for previous deflate call */
00218 
00219                 /* used by deflate.c: */
00220 
00221     uInt  w_size;        /* LZ77 window size (32K by default) */
00222     uInt  w_bits;        /* log2(w_size)  (8..16) */
00223     uInt  w_mask;        /* w_size - 1 */
00224 
00225     Byte *window;
00226     /* Sliding window. Input bytes are read into the second half of the window,
00227      * and move to the first half later to keep a dictionary of at least wSize
00228      * bytes. With this organization, matches are limited to a distance of
00229      * wSize-MAX_MATCH bytes, but this ensures that IO is always
00230      * performed with a length multiple of the block size. Also, it limits
00231      * the window size to 64K, which is quite useful on MSDOS.
00232      * To do: use the user input buffer as sliding window.
00233      */
00234 
00235     ulg window_size;
00236     /* Actual size of window: 2*wSize, except when the user input buffer
00237      * is directly used as sliding window.
00238      */
00239 
00240     Pos *prev;
00241     /* Link to older string with same hash index. To limit the size of this
00242      * array to 64K, this link is maintained only for the last 32K strings.
00243      * An index in this array is thus a window index modulo 32K.
00244      */
00245 
00246     Pos *head; /* Heads of the hash chains or NIL. */
00247 
00248     uInt  ins_h;          /* hash index of string to be inserted */
00249     uInt  hash_size;      /* number of elements in hash table */
00250     uInt  hash_bits;      /* log2(hash_size) */
00251     uInt  hash_mask;      /* hash_size-1 */
00252 
00253     uInt  hash_shift;
00254     /* Number of bits by which ins_h must be shifted at each input
00255      * step. It must be such that after MIN_MATCH steps, the oldest
00256      * byte no longer takes part in the hash key, that is:
00257      *   hash_shift * MIN_MATCH >= hash_bits
00258      */
00259 
00260     INT32 block_start;
00261     /* Window position at the beginning of the current output block. Gets
00262      * negative when the window is moved backwards.
00263      */
00264 
00265     uInt match_length;           /* length of best match */
00266     IPos prev_match;             /* previous match */
00267     INT32 match_available;         /* set if previous match exists */
00268     uInt strstart;               /* start of string to insert */
00269     uInt match_start;            /* start of matching string */
00270     uInt lookahead;              /* number of valid bytes ahead in window */
00271 
00272     uInt prev_length;
00273     /* Length of the best match at previous step. Matches not greater than this
00274      * are discarded. This is used in the lazy match evaluation.
00275      */
00276 
00277     uInt max_chain_length;
00278     /* To speed up deflation, hash chains are never searched beyond this
00279      * length.  A higher limit improves compression ratio but degrades the
00280      * speed.
00281      */
00282 
00283     uInt max_lazy_match;
00284     /* Attempt to find a better match only when the current match is strictly
00285      * smaller than this value. This mechanism is used only for compression
00286      * levels >= 4.
00287      */
00288 #define max_insert_length  max_lazy_match
00289     /* Insert new strings in the hash table only if the match length is not
00290      * greater than this length. This saves time but degrades compression.
00291      * max_insert_length is used only for compression levels <= 3.
00292      */
00293 
00294     INT32 level;    /* compression level (1..9) */
00295     INT32 strategy; /* favor or force Huffman coding*/
00296 
00297     uInt good_match;
00298     /* Use a faster search when the previous match is longer than this */
00299 
00300     INT32 nice_match; /* Stop searching when current match exceeds this */
00301 
00302 
00303 
00304     /* used by trees.c: */
00305 
00306     ct_data dyn_ltree[HEAP_SIZE];   /* literal and length tree */
00307     ct_data dyn_dtree[2*D_CODES+1]; /* distance tree */
00308     ct_data bl_tree[2*BL_CODES+1];  /* Huffman tree for the bit lengths */
00309 
00310     tree_desc l_desc;               /* descriptor for literal tree */
00311     tree_desc d_desc;               /* descriptor for distance tree */
00312     tree_desc bl_desc;              /* descriptor for bit length tree */
00313 
00314     ush bl_count[MAX_BITS+1];
00315     /* number of codes at each bit length for an optimal tree */
00316 
00317     INT32 heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
00318     INT32 heap_len;               /* number of elements in the heap */
00319     INT32 heap_max;               /* element of largest frequency */
00320     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
00321      * The same heap array is used to build all trees.
00322      */
00323 
00324     uch depth[2*L_CODES+1];
00325     /* Depth of each subtree used as tie breaker for trees of equal frequency
00326      */
00327 
00328     uch *l_buf;           /* buffer for literals or lengths */
00329 
00330     /* Size of match buffer for literals/lengths.  There are 4 reasons for
00331      * limiting lit_bufsize to 64K:
00332      *   - frequencies can be kept in 16 bit counters
00333      *   - if compression is not successful for the first block, all input
00334      *     data is still in the window so we can still emit a stored block even
00335      *     when input comes from standard input.  (This can also be done for
00336      *     all blocks if lit_bufsize is not greater than 32K.)
00337      *   - if compression is not successful for a file smaller than 64K, we can
00338      *     even emit a stored file instead of a stored block (saving 5 bytes).
00339      *     This is applicable only for zip (not gzip or zlib).
00340      *   - creating new Huffman trees less frequently may not provide fast
00341      *     adaptation to changes in the input data statistics. (Take for
00342      *     example a binary file with poorly compressible code followed by
00343      *     a highly compressible string table.) Smaller buffer sizes give
00344      *     fast adaptation but have of course the overhead of transmitting
00345      *     trees more frequently.
00346      *   - I can't count above 4
00347      */
00348     uInt  lit_bufsize;
00349 
00350     uInt last_lit;      /* running index in l_buf */
00351 
00352     // Buffer for distances. To simplify the code, d_buf and l_buf have
00353     // the same number of elements. To use different lengths, an extra flag
00354     // array would be necessary.
00355     ush *d_buf;
00356 
00357     ulg opt_len;        /* bit length of current block with optimal trees */
00358     ulg static_len;     /* bit length of current block with static trees */
00359     ulg compressed_len; /* total bit length of compressed file */
00360     uInt matches;       /* number of string matches in current block */
00361 // New version 0.99
00362     INT32 last_eob_len;   /* bit length of EOB code for last block */
00363 
00364 #ifdef DEBUG
00365     ulg bits_sent;      /* bit length of the compressed data */
00366 #endif
00367 
00368     // Output buffer. bits are inserted starting at the bottom (least
00369     // significant bits).
00370     ush bi_buf;
00371 
00372     // Number of valid bits in bi_buf.  All bits above the last valid bit
00373     // are always zero.
00374     INT32 bi_valid;
00375 };
00376 
00377 
00378 // New for verison 0.99 
00379 
00380 /* Output a byte on the stream.
00381  * IN assertion: there is enough room in pending_buf.
00382  */
00383 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
00384 
00385 
00386 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
00387 /* Minimum amount of lookahead, except at the end of the input file.
00388  * See deflate.c for comments about the MIN_MATCH+1.
00389  */
00390 
00391 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
00392 /* In order to simplify the code, particularly on 16 bit machines, match
00393  * distances are limited to MAX_DIST instead of WSIZE.
00394  */
00395 
00396 
00397 /********************************************************************************************
00398 
00399 >   class ZipDeflate : public CCObject
00400 
00401     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com> Humprhys
00402     Created:    23/5/95
00403     Purpose:    The deflater for the file compressor and decompressor.
00404                 Allows the CCDiskFile to offer compression of files. At present just
00405                 used to compress the native file format.
00406 
00407 ********************************************************************************************/
00408 
00409 class ZipDeflate : public CC_CLASS_MEMDUMP
00410 {
00411     // Give my name in memory dumps
00412     // If you need a different sort of decare, replace this one. 
00413     // See CCObject in the help file for info about DECLARE types
00414     CC_DECLARE_MEMDUMP(ZipDeflate);
00415 
00416 public:
00417     ZipDeflate();
00418     ~ZipDeflate();
00419 
00420 public:
00421     INT32 Init(ZStream *strm, INT32 level);
00422 
00423     INT32 deflate(ZStream *strm, INT32 flush);
00424 
00425     INT32 End(ZStream *strm);
00426 
00427     INT32 Init(ZStream *strm, INT32  level, INT32  method, INT32  windowBits,
00428              INT32  memLevel, INT32  strategy);
00429              //const char *version = ZLIB_VERSION, INT32 stream_size = sizeof(ZStream));
00430 
00431     //INT32 Copy(ZStream *dest, ZStream *source);
00432 
00433     INT32 Reset(ZStream *strm);
00434     
00435     // New for version 1.1
00436     INT32 SetDictionary(ZStream *strm, const Bytef *dictionary, uInt dictLength);
00437     INT32 Params(ZStream *strm, INT32 level, INT32 strategy);
00438 
00439 private:
00440     // these are accessed from configuration_table
00441     INT32  deflate_stored( DeflateState *s, INT32 flush); // New for version 1.1
00442     INT32  deflate_fast( DeflateState *s, INT32 flush);
00443     INT32  deflate_slow( DeflateState *s, INT32 flush);
00444     
00445     void fill_window( DeflateState *s);
00446     void lm_init( DeflateState *s);
00447     uInt longest_match( DeflateState *s, IPos cur_match);
00448     void putShortMSB( DeflateState *s, uInt b);
00449     void flush_pending(ZStream *strm);
00450     INT32 read_buf(ZStream *strm, charf *buf, unsigned size);
00451 #ifdef ASMV
00452       void match_init(void); /* asm code initialization */
00453 #endif
00454 
00455 #ifdef DEBUG
00456     void check_match( DeflateState *s, IPos start, IPos match, INT32 length);
00457     void send_bits(DeflateState *s, INT32 value, INT32 length);
00458 #endif
00459 
00460 protected:
00461     // functions this calls in ztrees.c
00462     void ct_init( DeflateState *s);
00463     INT32  ct_tally( DeflateState *s, INT32 dist, INT32 lc);
00464     ulg ct_flush_block( DeflateState *s, char *buf, ulg stored_len, INT32 eof);
00465     void ct_stored_block( DeflateState *s, char *buf, ulg stored_len, INT32 eof);
00466 
00467 
00468     // New for version 0.99
00469     void _tr_init(DeflateState *s);
00470     INT32  _tr_tally(DeflateState *s, unsigned dist, unsigned lc);
00471     ulg  _tr_flush_block(DeflateState *s, charf *buf, ulg stored_len, INT32 eof);
00472     void _tr_align(DeflateState *s);
00473     void _tr_stored_block(DeflateState *s, charf *buf, ulg stored_len, INT32 eof);
00474 
00475 private:
00476     // functions local to ztrees.c
00477     void tr_static_init(void);
00478     void init_block( DeflateState *s);
00479     void pqdownheap( DeflateState *s, ct_data *tree, INT32 k);
00480     void gen_bitlen( DeflateState *s, tree_desc *desc);
00481     void gen_codes(ct_data *tree, INT32 max_code, ushf bl_count[]);
00482     void build_tree( DeflateState *s, tree_desc *desc);
00483     void scan_tree( DeflateState *s, ct_data *tree, INT32 max_code);
00484     void send_tree( DeflateState *s, ct_data *tree, INT32 max_code);
00485     INT32  build_bl_tree( DeflateState *s);
00486     void send_all_trees( DeflateState *s, INT32 lcodes, INT32 dcodes, INT32 blcodes);
00487     void compress_block( DeflateState *s, ct_data *ltree, ct_data *dtree);
00488     void set_data_type( DeflateState *s);
00489 //  void send_bits( DeflateState *s, INT32 value, INT32 length);
00490     unsigned bi_reverse(unsigned value, INT32 length);
00491     void bi_windup( DeflateState *s);
00492     void bi_flush(DeflateState *s); // new version 0.99
00493     void copy_block( DeflateState *s, char *buf, unsigned len, INT32 header);
00494 
00495 };
00496 
00497 #endif  // INC_ZIPDEFLATE
00498 
00499 

Generated on Sat Nov 10 03:47:26 2007 for Camelot by  doxygen 1.4.4