Back to home page

OSCL-LXR

 
 

    


0001 #ifndef DEFUTIL_H
0002 #define DEFUTIL_H
0003 
0004 #include <linux/zutil.h>
0005 
0006 #define Assert(err, str) 
0007 #define Trace(dummy) 
0008 #define Tracev(dummy) 
0009 #define Tracecv(err, dummy) 
0010 #define Tracevv(dummy) 
0011 
0012 
0013 
0014 #define LENGTH_CODES 29
0015 /* number of length codes, not counting the special END_BLOCK code */
0016 
0017 #define LITERALS  256
0018 /* number of literal bytes 0..255 */
0019 
0020 #define L_CODES (LITERALS+1+LENGTH_CODES)
0021 /* number of Literal or Length codes, including the END_BLOCK code */
0022 
0023 #define D_CODES   30
0024 /* number of distance codes */
0025 
0026 #define BL_CODES  19
0027 /* number of codes used to transfer the bit lengths */
0028 
0029 #define HEAP_SIZE (2*L_CODES+1)
0030 /* maximum heap size */
0031 
0032 #define MAX_BITS 15
0033 /* All codes must not exceed MAX_BITS bits */
0034 
0035 #define INIT_STATE    42
0036 #define BUSY_STATE   113
0037 #define FINISH_STATE 666
0038 /* Stream status */
0039 
0040 
0041 /* Data structure describing a single value and its code string. */
0042 typedef struct ct_data_s {
0043     union {
0044         ush  freq;       /* frequency count */
0045         ush  code;       /* bit string */
0046     } fc;
0047     union {
0048         ush  dad;        /* father node in Huffman tree */
0049         ush  len;        /* length of bit string */
0050     } dl;
0051 } ct_data;
0052 
0053 #define Freq fc.freq
0054 #define Code fc.code
0055 #define Dad  dl.dad
0056 #define Len  dl.len
0057 
0058 typedef struct static_tree_desc_s  static_tree_desc;
0059 
0060 typedef struct tree_desc_s {
0061     ct_data *dyn_tree;           /* the dynamic tree */
0062     int     max_code;            /* largest code with non zero frequency */
0063     static_tree_desc *stat_desc; /* the corresponding static tree */
0064 } tree_desc;
0065 
0066 typedef ush Pos;
0067 typedef unsigned IPos;
0068 
0069 /* A Pos is an index in the character window. We use short instead of int to
0070  * save space in the various tables. IPos is used only for parameter passing.
0071  */
0072 
0073 typedef struct deflate_state {
0074     z_streamp strm;      /* pointer back to this zlib stream */
0075     int   status;        /* as the name implies */
0076     Byte *pending_buf;   /* output still pending */
0077     ulg   pending_buf_size; /* size of pending_buf */
0078     Byte *pending_out;   /* next pending byte to output to the stream */
0079     int   pending;       /* nb of bytes in the pending buffer */
0080     int   noheader;      /* suppress zlib header and adler32 */
0081     Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
0082     Byte  method;        /* STORED (for zip only) or DEFLATED */
0083     int   last_flush;    /* value of flush param for previous deflate call */
0084 
0085                 /* used by deflate.c: */
0086 
0087     uInt  w_size;        /* LZ77 window size (32K by default) */
0088     uInt  w_bits;        /* log2(w_size)  (8..16) */
0089     uInt  w_mask;        /* w_size - 1 */
0090 
0091     Byte *window;
0092     /* Sliding window. Input bytes are read into the second half of the window,
0093      * and move to the first half later to keep a dictionary of at least wSize
0094      * bytes. With this organization, matches are limited to a distance of
0095      * wSize-MAX_MATCH bytes, but this ensures that IO is always
0096      * performed with a length multiple of the block size. Also, it limits
0097      * the window size to 64K, which is quite useful on MSDOS.
0098      * To do: use the user input buffer as sliding window.
0099      */
0100 
0101     ulg window_size;
0102     /* Actual size of window: 2*wSize, except when the user input buffer
0103      * is directly used as sliding window.
0104      */
0105 
0106     Pos *prev;
0107     /* Link to older string with same hash index. To limit the size of this
0108      * array to 64K, this link is maintained only for the last 32K strings.
0109      * An index in this array is thus a window index modulo 32K.
0110      */
0111 
0112     Pos *head; /* Heads of the hash chains or NIL. */
0113 
0114     uInt  ins_h;          /* hash index of string to be inserted */
0115     uInt  hash_size;      /* number of elements in hash table */
0116     uInt  hash_bits;      /* log2(hash_size) */
0117     uInt  hash_mask;      /* hash_size-1 */
0118 
0119     uInt  hash_shift;
0120     /* Number of bits by which ins_h must be shifted at each input
0121      * step. It must be such that after MIN_MATCH steps, the oldest
0122      * byte no longer takes part in the hash key, that is:
0123      *   hash_shift * MIN_MATCH >= hash_bits
0124      */
0125 
0126     long block_start;
0127     /* Window position at the beginning of the current output block. Gets
0128      * negative when the window is moved backwards.
0129      */
0130 
0131     uInt match_length;           /* length of best match */
0132     IPos prev_match;             /* previous match */
0133     int match_available;         /* set if previous match exists */
0134     uInt strstart;               /* start of string to insert */
0135     uInt match_start;            /* start of matching string */
0136     uInt lookahead;              /* number of valid bytes ahead in window */
0137 
0138     uInt prev_length;
0139     /* Length of the best match at previous step. Matches not greater than this
0140      * are discarded. This is used in the lazy match evaluation.
0141      */
0142 
0143     uInt max_chain_length;
0144     /* To speed up deflation, hash chains are never searched beyond this
0145      * length.  A higher limit improves compression ratio but degrades the
0146      * speed.
0147      */
0148 
0149     uInt max_lazy_match;
0150     /* Attempt to find a better match only when the current match is strictly
0151      * smaller than this value. This mechanism is used only for compression
0152      * levels >= 4.
0153      */
0154 #   define max_insert_length  max_lazy_match
0155     /* Insert new strings in the hash table only if the match length is not
0156      * greater than this length. This saves time but degrades compression.
0157      * max_insert_length is used only for compression levels <= 3.
0158      */
0159 
0160     int level;    /* compression level (1..9) */
0161     int strategy; /* favor or force Huffman coding*/
0162 
0163     uInt good_match;
0164     /* Use a faster search when the previous match is longer than this */
0165 
0166     int nice_match; /* Stop searching when current match exceeds this */
0167 
0168                 /* used by trees.c: */
0169     /* Didn't use ct_data typedef below to suppress compiler warning */
0170     struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
0171     struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
0172     struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
0173 
0174     struct tree_desc_s l_desc;               /* desc. for literal tree */
0175     struct tree_desc_s d_desc;               /* desc. for distance tree */
0176     struct tree_desc_s bl_desc;              /* desc. for bit length tree */
0177 
0178     ush bl_count[MAX_BITS+1];
0179     /* number of codes at each bit length for an optimal tree */
0180 
0181     int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
0182     int heap_len;               /* number of elements in the heap */
0183     int heap_max;               /* element of largest frequency */
0184     /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
0185      * The same heap array is used to build all trees.
0186      */
0187 
0188     uch depth[2*L_CODES+1];
0189     /* Depth of each subtree used as tie breaker for trees of equal frequency
0190      */
0191 
0192     uch *l_buf;          /* buffer for literals or lengths */
0193 
0194     uInt  lit_bufsize;
0195     /* Size of match buffer for literals/lengths.  There are 4 reasons for
0196      * limiting lit_bufsize to 64K:
0197      *   - frequencies can be kept in 16 bit counters
0198      *   - if compression is not successful for the first block, all input
0199      *     data is still in the window so we can still emit a stored block even
0200      *     when input comes from standard input.  (This can also be done for
0201      *     all blocks if lit_bufsize is not greater than 32K.)
0202      *   - if compression is not successful for a file smaller than 64K, we can
0203      *     even emit a stored file instead of a stored block (saving 5 bytes).
0204      *     This is applicable only for zip (not gzip or zlib).
0205      *   - creating new Huffman trees less frequently may not provide fast
0206      *     adaptation to changes in the input data statistics. (Take for
0207      *     example a binary file with poorly compressible code followed by
0208      *     a highly compressible string table.) Smaller buffer sizes give
0209      *     fast adaptation but have of course the overhead of transmitting
0210      *     trees more frequently.
0211      *   - I can't count above 4
0212      */
0213 
0214     uInt last_lit;      /* running index in l_buf */
0215 
0216     ush *d_buf;
0217     /* Buffer for distances. To simplify the code, d_buf and l_buf have
0218      * the same number of elements. To use different lengths, an extra flag
0219      * array would be necessary.
0220      */
0221 
0222     ulg opt_len;        /* bit length of current block with optimal trees */
0223     ulg static_len;     /* bit length of current block with static trees */
0224     ulg compressed_len; /* total bit length of compressed file */
0225     uInt matches;       /* number of string matches in current block */
0226     int last_eob_len;   /* bit length of EOB code for last block */
0227 
0228 #ifdef DEBUG_ZLIB
0229     ulg bits_sent;      /* bit length of the compressed data */
0230 #endif
0231 
0232     ush bi_buf;
0233     /* Output buffer. bits are inserted starting at the bottom (least
0234      * significant bits).
0235      */
0236     int bi_valid;
0237     /* Number of valid bits in bi_buf.  All bits above the last valid bit
0238      * are always zero.
0239      */
0240 
0241 } deflate_state;
0242 
0243 #ifdef CONFIG_ZLIB_DFLTCC
0244 #define zlib_deflate_window_memsize(windowBits) \
0245     (2 * (1 << (windowBits)) * sizeof(Byte) + PAGE_SIZE)
0246 #else
0247 #define zlib_deflate_window_memsize(windowBits) \
0248     (2 * (1 << (windowBits)) * sizeof(Byte))
0249 #endif
0250 #define zlib_deflate_prev_memsize(windowBits) \
0251     ((1 << (windowBits)) * sizeof(Pos))
0252 #define zlib_deflate_head_memsize(memLevel) \
0253     ((1 << ((memLevel)+7)) * sizeof(Pos))
0254 #define zlib_deflate_overlay_memsize(memLevel) \
0255     ((1 << ((memLevel)+6)) * (sizeof(ush)+2))
0256 
0257 /* Output a byte on the stream.
0258  * IN assertion: there is enough room in pending_buf.
0259  */
0260 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
0261 
0262 
0263 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
0264 /* Minimum amount of lookahead, except at the end of the input file.
0265  * See deflate.c for comments about the MIN_MATCH+1.
0266  */
0267 
0268 #define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
0269 /* In order to simplify the code, particularly on 16 bit machines, match
0270  * distances are limited to MAX_DIST instead of WSIZE.
0271  */
0272 
0273         /* in trees.c */
0274 void zlib_tr_init         (deflate_state *s);
0275 int  zlib_tr_tally        (deflate_state *s, unsigned dist, unsigned lc);
0276 ulg  zlib_tr_flush_block  (deflate_state *s, char *buf, ulg stored_len,
0277                int eof);
0278 void zlib_tr_align        (deflate_state *s);
0279 void zlib_tr_stored_block (deflate_state *s, char *buf, ulg stored_len,
0280                int eof);
0281 void zlib_tr_stored_type_only (deflate_state *);
0282 
0283 
0284 /* ===========================================================================
0285  * Output a short LSB first on the stream.
0286  * IN assertion: there is enough room in pendingBuf.
0287  */
0288 #define put_short(s, w) { \
0289     put_byte(s, (uch)((w) & 0xff)); \
0290     put_byte(s, (uch)((ush)(w) >> 8)); \
0291 }
0292 
0293 /* ===========================================================================
0294  * Reverse the first len bits of a code, using straightforward code (a faster
0295  * method would use a table)
0296  * IN assertion: 1 <= len <= 15
0297  */
0298 static inline unsigned  bi_reverse(
0299     unsigned code, /* the value to invert */
0300     int len        /* its bit length */
0301 )
0302 {
0303     register unsigned res = 0;
0304     do {
0305         res |= code & 1;
0306         code >>= 1, res <<= 1;
0307     } while (--len > 0);
0308     return res >> 1;
0309 }
0310 
0311 /* ===========================================================================
0312  * Flush the bit buffer, keeping at most 7 bits in it.
0313  */
0314 static inline void bi_flush(deflate_state *s)
0315 {
0316     if (s->bi_valid == 16) {
0317         put_short(s, s->bi_buf);
0318         s->bi_buf = 0;
0319         s->bi_valid = 0;
0320     } else if (s->bi_valid >= 8) {
0321         put_byte(s, (Byte)s->bi_buf);
0322         s->bi_buf >>= 8;
0323         s->bi_valid -= 8;
0324     }
0325 }
0326 
0327 /* ===========================================================================
0328  * Flush the bit buffer and align the output on a byte boundary
0329  */
0330 static inline void bi_windup(deflate_state *s)
0331 {
0332     if (s->bi_valid > 8) {
0333         put_short(s, s->bi_buf);
0334     } else if (s->bi_valid > 0) {
0335         put_byte(s, (Byte)s->bi_buf);
0336     }
0337     s->bi_buf = 0;
0338     s->bi_valid = 0;
0339 #ifdef DEBUG_ZLIB
0340     s->bits_sent = (s->bits_sent+7) & ~7;
0341 #endif
0342 }
0343 
0344 typedef enum {
0345     need_more,      /* block not completed, need more input or more output */
0346     block_done,     /* block flush performed */
0347     finish_started, /* finish started, need only more output at next deflate */
0348     finish_done     /* finish done, accept no more input or output */
0349 } block_state;
0350 
0351 #define Buf_size (8 * 2*sizeof(char))
0352 /* Number of bits used within bi_buf. (bi_buf might be implemented on
0353  * more than 16 bits on some systems.)
0354  */
0355 
0356 /* ===========================================================================
0357  * Send a value on a given number of bits.
0358  * IN assertion: length <= 16 and value fits in length bits.
0359  */
0360 #ifdef DEBUG_ZLIB
0361 static void send_bits      (deflate_state *s, int value, int length);
0362 
0363 static void send_bits(
0364     deflate_state *s,
0365     int value,  /* value to send */
0366     int length  /* number of bits */
0367 )
0368 {
0369     Tracevv((stderr," l %2d v %4x ", length, value));
0370     Assert(length > 0 && length <= 15, "invalid length");
0371     s->bits_sent += (ulg)length;
0372 
0373     /* If not enough room in bi_buf, use (valid) bits from bi_buf and
0374      * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
0375      * unused bits in value.
0376      */
0377     if (s->bi_valid > (int)Buf_size - length) {
0378         s->bi_buf |= (value << s->bi_valid);
0379         put_short(s, s->bi_buf);
0380         s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
0381         s->bi_valid += length - Buf_size;
0382     } else {
0383         s->bi_buf |= value << s->bi_valid;
0384         s->bi_valid += length;
0385     }
0386 }
0387 #else /* !DEBUG_ZLIB */
0388 
0389 #define send_bits(s, value, length) \
0390 { int len = length;\
0391   if (s->bi_valid > (int)Buf_size - len) {\
0392     int val = value;\
0393     s->bi_buf |= (val << s->bi_valid);\
0394     put_short(s, s->bi_buf);\
0395     s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
0396     s->bi_valid += len - Buf_size;\
0397   } else {\
0398     s->bi_buf |= (value) << s->bi_valid;\
0399     s->bi_valid += len;\
0400   }\
0401 }
0402 #endif /* DEBUG_ZLIB */
0403 
0404 static inline void zlib_tr_send_bits(
0405     deflate_state *s,
0406     int value,
0407     int length
0408 )
0409 {
0410     send_bits(s, value, length);
0411 }
0412 
0413 /* =========================================================================
0414  * Flush as much pending output as possible. All deflate() output goes
0415  * through this function so some applications may wish to modify it
0416  * to avoid allocating a large strm->next_out buffer and copying into it.
0417  * (See also read_buf()).
0418  */
0419 static inline void flush_pending(
0420     z_streamp strm
0421 )
0422 {
0423     deflate_state *s = (deflate_state *) strm->state;
0424     unsigned len = s->pending;
0425 
0426     if (len > strm->avail_out) len = strm->avail_out;
0427     if (len == 0) return;
0428 
0429     if (strm->next_out != NULL) {
0430     memcpy(strm->next_out, s->pending_out, len);
0431     strm->next_out += len;
0432     }
0433     s->pending_out += len;
0434     strm->total_out += len;
0435     strm->avail_out  -= len;
0436     s->pending -= len;
0437     if (s->pending == 0) {
0438         s->pending_out = s->pending_buf;
0439     }
0440 }
0441 #endif /* DEFUTIL_H */