Basic gzip/gunzip in memory buffer examples using zlib code.

Dependencies:   mbed-rtos mbed

There are small changes needed to the zconf.h file in the zlib distribution (I used 1.2.7). The zlib license applies to the zlib code - I have only imported a subset of the source.

The MBED has limited memory, so we need the following (near the top of zconf.h) to restrict memory allocation sizes:

#define    MAX_MEM_LEVEL    3
#define    MAX_WBITS        10

Because MAX_MEM_LEVEL and MAX_WBITS are so much lower than the default, there is a danger that the mbed cannot gunzip data compressed by a 'normal' zlib build. My use-case is to gzip on the mbed more than gunzip on the mbed so I have not given much time to this issue.

I also included this (also near the top of zconf.h) to prefix defines with Z_

#define    Z_PREFIX

In zconf.h, in the zlib distribution, the includes for <fcntl.h> and <sys/types.h> need commenting out when using the online compiler. No need when using GCC4MBED.

I also looked at miniz. I chose zlib because I needed the gzip headers and miniz does not implement them.

The sample main.cpp reads source data, compresses it, decompresses it, and finally compares the input data with the output data to confirm they are the same.

    unsigned char input_data[2048];
    unsigned long input_data_length = 0;
    FILE *ifp = fopen("/local/src.txt", "r");
    if (ifp) {
        int br = fread(input_data, 1, sizeof(input_data), ifp);
        fclose(ifp);
        input_data_length = br;
    }
    printf("%s:%d: input_data_length:%lu%s", __FILE__, __LINE__, input_data_length, newline);
 
 
    unsigned char gzip_data[2048];
    unsigned long gzip_data_length = 0;
    if (input_data_length > 0) {
        gzip_data_length = sizeof(gzip_data);
        int rv = gzip(gzip_data, &gzip_data_length, input_data, input_data_length);
        if (Z_OK == rv) {
            FILE *ofp = fopen("/local/dst.gz", "w");
            if (ofp) {
                int bw = fwrite(gzip_data, 1, gzip_data_length, ofp);
                fclose(ofp);
            }
        } else {
            printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline);
        }
    }
    printf("%s:%d: gzip_data_length:%lu%s", __FILE__, __LINE__, gzip_data_length, newline);
 
 
    unsigned char output_data[2048];
    unsigned long output_data_length = 0;
    if (gzip_data_length > 0) {
        output_data_length = sizeof(output_data);
        int rv = gunzip(output_data, &output_data_length, gzip_data, gzip_data_length);
        if (Z_OK != rv) {
            printf("%s:%d: %d%s", __FILE__, __LINE__, rv, newline);
        }
    }
    printf("%s:%d: output_data_length:%lu%s", __FILE__, __LINE__, output_data_length, newline);
 
 
    if (input_data_length > 0 and input_data_length > 0) {
        bool input_matches_output = false;
        if (input_data_length == output_data_length) {
            input_matches_output = true;
            for ( size_t i = 0 ; input_matches_output && i < input_data_length ; i++ ) {
                if (input_data[i] != output_data[i]) {
                    input_matches_output = false;
                }
            }
        }
        printf("%s:%d: input (%lu bytes) %s output (%lu bytes)%s", __FILE__, __LINE__, input_data_length, input_matches_output?"matches":"does not match", output_data_length, newline);
    } else {
        printf("%s:%d: input and/or output length is 0%s", __FILE__, __LINE__, newline);
    }
Committer:
jonathonfletcher
Date:
Sun Oct 21 07:46:41 2012 +0000
Revision:
0:54f5be781526
initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jonathonfletcher 0:54f5be781526 1 /* deflate.h -- internal compression state
jonathonfletcher 0:54f5be781526 2 * Copyright (C) 1995-2012 Jean-loup Gailly
jonathonfletcher 0:54f5be781526 3 * For conditions of distribution and use, see copyright notice in zlib.h
jonathonfletcher 0:54f5be781526 4 */
jonathonfletcher 0:54f5be781526 5
jonathonfletcher 0:54f5be781526 6 /* WARNING: this file should *not* be used by applications. It is
jonathonfletcher 0:54f5be781526 7 part of the implementation of the compression library and is
jonathonfletcher 0:54f5be781526 8 subject to change. Applications should only use zlib.h.
jonathonfletcher 0:54f5be781526 9 */
jonathonfletcher 0:54f5be781526 10
jonathonfletcher 0:54f5be781526 11 /* @(#) $Id$ */
jonathonfletcher 0:54f5be781526 12
jonathonfletcher 0:54f5be781526 13 #ifndef DEFLATE_H
jonathonfletcher 0:54f5be781526 14 #define DEFLATE_H
jonathonfletcher 0:54f5be781526 15
jonathonfletcher 0:54f5be781526 16 #include "zutil.h"
jonathonfletcher 0:54f5be781526 17
jonathonfletcher 0:54f5be781526 18 /* define NO_GZIP when compiling if you want to disable gzip header and
jonathonfletcher 0:54f5be781526 19 trailer creation by deflate(). NO_GZIP would be used to avoid linking in
jonathonfletcher 0:54f5be781526 20 the crc code when it is not needed. For shared libraries, gzip encoding
jonathonfletcher 0:54f5be781526 21 should be left enabled. */
jonathonfletcher 0:54f5be781526 22 #ifndef NO_GZIP
jonathonfletcher 0:54f5be781526 23 # define GZIP
jonathonfletcher 0:54f5be781526 24 #endif
jonathonfletcher 0:54f5be781526 25
jonathonfletcher 0:54f5be781526 26 /* ===========================================================================
jonathonfletcher 0:54f5be781526 27 * Internal compression state.
jonathonfletcher 0:54f5be781526 28 */
jonathonfletcher 0:54f5be781526 29
jonathonfletcher 0:54f5be781526 30 #define LENGTH_CODES 29
jonathonfletcher 0:54f5be781526 31 /* number of length codes, not counting the special END_BLOCK code */
jonathonfletcher 0:54f5be781526 32
jonathonfletcher 0:54f5be781526 33 #define LITERALS 256
jonathonfletcher 0:54f5be781526 34 /* number of literal bytes 0..255 */
jonathonfletcher 0:54f5be781526 35
jonathonfletcher 0:54f5be781526 36 #define L_CODES (LITERALS+1+LENGTH_CODES)
jonathonfletcher 0:54f5be781526 37 /* number of Literal or Length codes, including the END_BLOCK code */
jonathonfletcher 0:54f5be781526 38
jonathonfletcher 0:54f5be781526 39 #define D_CODES 30
jonathonfletcher 0:54f5be781526 40 /* number of distance codes */
jonathonfletcher 0:54f5be781526 41
jonathonfletcher 0:54f5be781526 42 #define BL_CODES 19
jonathonfletcher 0:54f5be781526 43 /* number of codes used to transfer the bit lengths */
jonathonfletcher 0:54f5be781526 44
jonathonfletcher 0:54f5be781526 45 #define HEAP_SIZE (2*L_CODES+1)
jonathonfletcher 0:54f5be781526 46 /* maximum heap size */
jonathonfletcher 0:54f5be781526 47
jonathonfletcher 0:54f5be781526 48 #define MAX_BITS 15
jonathonfletcher 0:54f5be781526 49 /* All codes must not exceed MAX_BITS bits */
jonathonfletcher 0:54f5be781526 50
jonathonfletcher 0:54f5be781526 51 #define Buf_size 16
jonathonfletcher 0:54f5be781526 52 /* size of bit buffer in bi_buf */
jonathonfletcher 0:54f5be781526 53
jonathonfletcher 0:54f5be781526 54 #define INIT_STATE 42
jonathonfletcher 0:54f5be781526 55 #define EXTRA_STATE 69
jonathonfletcher 0:54f5be781526 56 #define NAME_STATE 73
jonathonfletcher 0:54f5be781526 57 #define COMMENT_STATE 91
jonathonfletcher 0:54f5be781526 58 #define HCRC_STATE 103
jonathonfletcher 0:54f5be781526 59 #define BUSY_STATE 113
jonathonfletcher 0:54f5be781526 60 #define FINISH_STATE 666
jonathonfletcher 0:54f5be781526 61 /* Stream status */
jonathonfletcher 0:54f5be781526 62
jonathonfletcher 0:54f5be781526 63
jonathonfletcher 0:54f5be781526 64 /* Data structure describing a single value and its code string. */
jonathonfletcher 0:54f5be781526 65 typedef struct ct_data_s {
jonathonfletcher 0:54f5be781526 66 union {
jonathonfletcher 0:54f5be781526 67 ush freq; /* frequency count */
jonathonfletcher 0:54f5be781526 68 ush code; /* bit string */
jonathonfletcher 0:54f5be781526 69 } fc;
jonathonfletcher 0:54f5be781526 70 union {
jonathonfletcher 0:54f5be781526 71 ush dad; /* father node in Huffman tree */
jonathonfletcher 0:54f5be781526 72 ush len; /* length of bit string */
jonathonfletcher 0:54f5be781526 73 } dl;
jonathonfletcher 0:54f5be781526 74 } FAR ct_data;
jonathonfletcher 0:54f5be781526 75
jonathonfletcher 0:54f5be781526 76 #define Freq fc.freq
jonathonfletcher 0:54f5be781526 77 #define Code fc.code
jonathonfletcher 0:54f5be781526 78 #define Dad dl.dad
jonathonfletcher 0:54f5be781526 79 #define Len dl.len
jonathonfletcher 0:54f5be781526 80
jonathonfletcher 0:54f5be781526 81 typedef struct static_tree_desc_s static_tree_desc;
jonathonfletcher 0:54f5be781526 82
jonathonfletcher 0:54f5be781526 83 typedef struct tree_desc_s {
jonathonfletcher 0:54f5be781526 84 ct_data *dyn_tree; /* the dynamic tree */
jonathonfletcher 0:54f5be781526 85 int max_code; /* largest code with non zero frequency */
jonathonfletcher 0:54f5be781526 86 static_tree_desc *stat_desc; /* the corresponding static tree */
jonathonfletcher 0:54f5be781526 87 } FAR tree_desc;
jonathonfletcher 0:54f5be781526 88
jonathonfletcher 0:54f5be781526 89 typedef ush Pos;
jonathonfletcher 0:54f5be781526 90 typedef Pos FAR Posf;
jonathonfletcher 0:54f5be781526 91 typedef unsigned IPos;
jonathonfletcher 0:54f5be781526 92
jonathonfletcher 0:54f5be781526 93 /* A Pos is an index in the character window. We use short instead of int to
jonathonfletcher 0:54f5be781526 94 * save space in the various tables. IPos is used only for parameter passing.
jonathonfletcher 0:54f5be781526 95 */
jonathonfletcher 0:54f5be781526 96
jonathonfletcher 0:54f5be781526 97 typedef struct internal_state {
jonathonfletcher 0:54f5be781526 98 z_streamp strm; /* pointer back to this zlib stream */
jonathonfletcher 0:54f5be781526 99 int status; /* as the name implies */
jonathonfletcher 0:54f5be781526 100 Bytef *pending_buf; /* output still pending */
jonathonfletcher 0:54f5be781526 101 ulg pending_buf_size; /* size of pending_buf */
jonathonfletcher 0:54f5be781526 102 Bytef *pending_out; /* next pending byte to output to the stream */
jonathonfletcher 0:54f5be781526 103 uInt pending; /* nb of bytes in the pending buffer */
jonathonfletcher 0:54f5be781526 104 int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
jonathonfletcher 0:54f5be781526 105 gz_headerp gzhead; /* gzip header information to write */
jonathonfletcher 0:54f5be781526 106 uInt gzindex; /* where in extra, name, or comment */
jonathonfletcher 0:54f5be781526 107 Byte method; /* STORED (for zip only) or DEFLATED */
jonathonfletcher 0:54f5be781526 108 int last_flush; /* value of flush param for previous deflate call */
jonathonfletcher 0:54f5be781526 109
jonathonfletcher 0:54f5be781526 110 /* used by deflate.c: */
jonathonfletcher 0:54f5be781526 111
jonathonfletcher 0:54f5be781526 112 uInt w_size; /* LZ77 window size (32K by default) */
jonathonfletcher 0:54f5be781526 113 uInt w_bits; /* log2(w_size) (8..16) */
jonathonfletcher 0:54f5be781526 114 uInt w_mask; /* w_size - 1 */
jonathonfletcher 0:54f5be781526 115
jonathonfletcher 0:54f5be781526 116 Bytef *window;
jonathonfletcher 0:54f5be781526 117 /* Sliding window. Input bytes are read into the second half of the window,
jonathonfletcher 0:54f5be781526 118 * and move to the first half later to keep a dictionary of at least wSize
jonathonfletcher 0:54f5be781526 119 * bytes. With this organization, matches are limited to a distance of
jonathonfletcher 0:54f5be781526 120 * wSize-MAX_MATCH bytes, but this ensures that IO is always
jonathonfletcher 0:54f5be781526 121 * performed with a length multiple of the block size. Also, it limits
jonathonfletcher 0:54f5be781526 122 * the window size to 64K, which is quite useful on MSDOS.
jonathonfletcher 0:54f5be781526 123 * To do: use the user input buffer as sliding window.
jonathonfletcher 0:54f5be781526 124 */
jonathonfletcher 0:54f5be781526 125
jonathonfletcher 0:54f5be781526 126 ulg window_size;
jonathonfletcher 0:54f5be781526 127 /* Actual size of window: 2*wSize, except when the user input buffer
jonathonfletcher 0:54f5be781526 128 * is directly used as sliding window.
jonathonfletcher 0:54f5be781526 129 */
jonathonfletcher 0:54f5be781526 130
jonathonfletcher 0:54f5be781526 131 Posf *prev;
jonathonfletcher 0:54f5be781526 132 /* Link to older string with same hash index. To limit the size of this
jonathonfletcher 0:54f5be781526 133 * array to 64K, this link is maintained only for the last 32K strings.
jonathonfletcher 0:54f5be781526 134 * An index in this array is thus a window index modulo 32K.
jonathonfletcher 0:54f5be781526 135 */
jonathonfletcher 0:54f5be781526 136
jonathonfletcher 0:54f5be781526 137 Posf *head; /* Heads of the hash chains or NIL. */
jonathonfletcher 0:54f5be781526 138
jonathonfletcher 0:54f5be781526 139 uInt ins_h; /* hash index of string to be inserted */
jonathonfletcher 0:54f5be781526 140 uInt hash_size; /* number of elements in hash table */
jonathonfletcher 0:54f5be781526 141 uInt hash_bits; /* log2(hash_size) */
jonathonfletcher 0:54f5be781526 142 uInt hash_mask; /* hash_size-1 */
jonathonfletcher 0:54f5be781526 143
jonathonfletcher 0:54f5be781526 144 uInt hash_shift;
jonathonfletcher 0:54f5be781526 145 /* Number of bits by which ins_h must be shifted at each input
jonathonfletcher 0:54f5be781526 146 * step. It must be such that after MIN_MATCH steps, the oldest
jonathonfletcher 0:54f5be781526 147 * byte no longer takes part in the hash key, that is:
jonathonfletcher 0:54f5be781526 148 * hash_shift * MIN_MATCH >= hash_bits
jonathonfletcher 0:54f5be781526 149 */
jonathonfletcher 0:54f5be781526 150
jonathonfletcher 0:54f5be781526 151 long block_start;
jonathonfletcher 0:54f5be781526 152 /* Window position at the beginning of the current output block. Gets
jonathonfletcher 0:54f5be781526 153 * negative when the window is moved backwards.
jonathonfletcher 0:54f5be781526 154 */
jonathonfletcher 0:54f5be781526 155
jonathonfletcher 0:54f5be781526 156 uInt match_length; /* length of best match */
jonathonfletcher 0:54f5be781526 157 IPos prev_match; /* previous match */
jonathonfletcher 0:54f5be781526 158 int match_available; /* set if previous match exists */
jonathonfletcher 0:54f5be781526 159 uInt strstart; /* start of string to insert */
jonathonfletcher 0:54f5be781526 160 uInt match_start; /* start of matching string */
jonathonfletcher 0:54f5be781526 161 uInt lookahead; /* number of valid bytes ahead in window */
jonathonfletcher 0:54f5be781526 162
jonathonfletcher 0:54f5be781526 163 uInt prev_length;
jonathonfletcher 0:54f5be781526 164 /* Length of the best match at previous step. Matches not greater than this
jonathonfletcher 0:54f5be781526 165 * are discarded. This is used in the lazy match evaluation.
jonathonfletcher 0:54f5be781526 166 */
jonathonfletcher 0:54f5be781526 167
jonathonfletcher 0:54f5be781526 168 uInt max_chain_length;
jonathonfletcher 0:54f5be781526 169 /* To speed up deflation, hash chains are never searched beyond this
jonathonfletcher 0:54f5be781526 170 * length. A higher limit improves compression ratio but degrades the
jonathonfletcher 0:54f5be781526 171 * speed.
jonathonfletcher 0:54f5be781526 172 */
jonathonfletcher 0:54f5be781526 173
jonathonfletcher 0:54f5be781526 174 uInt max_lazy_match;
jonathonfletcher 0:54f5be781526 175 /* Attempt to find a better match only when the current match is strictly
jonathonfletcher 0:54f5be781526 176 * smaller than this value. This mechanism is used only for compression
jonathonfletcher 0:54f5be781526 177 * levels >= 4.
jonathonfletcher 0:54f5be781526 178 */
jonathonfletcher 0:54f5be781526 179 # define max_insert_length max_lazy_match
jonathonfletcher 0:54f5be781526 180 /* Insert new strings in the hash table only if the match length is not
jonathonfletcher 0:54f5be781526 181 * greater than this length. This saves time but degrades compression.
jonathonfletcher 0:54f5be781526 182 * max_insert_length is used only for compression levels <= 3.
jonathonfletcher 0:54f5be781526 183 */
jonathonfletcher 0:54f5be781526 184
jonathonfletcher 0:54f5be781526 185 int level; /* compression level (1..9) */
jonathonfletcher 0:54f5be781526 186 int strategy; /* favor or force Huffman coding*/
jonathonfletcher 0:54f5be781526 187
jonathonfletcher 0:54f5be781526 188 uInt good_match;
jonathonfletcher 0:54f5be781526 189 /* Use a faster search when the previous match is longer than this */
jonathonfletcher 0:54f5be781526 190
jonathonfletcher 0:54f5be781526 191 int nice_match; /* Stop searching when current match exceeds this */
jonathonfletcher 0:54f5be781526 192
jonathonfletcher 0:54f5be781526 193 /* used by trees.c: */
jonathonfletcher 0:54f5be781526 194 /* Didn't use ct_data typedef below to suppress compiler warning */
jonathonfletcher 0:54f5be781526 195 struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
jonathonfletcher 0:54f5be781526 196 struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
jonathonfletcher 0:54f5be781526 197 struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
jonathonfletcher 0:54f5be781526 198
jonathonfletcher 0:54f5be781526 199 struct tree_desc_s l_desc; /* desc. for literal tree */
jonathonfletcher 0:54f5be781526 200 struct tree_desc_s d_desc; /* desc. for distance tree */
jonathonfletcher 0:54f5be781526 201 struct tree_desc_s bl_desc; /* desc. for bit length tree */
jonathonfletcher 0:54f5be781526 202
jonathonfletcher 0:54f5be781526 203 ush bl_count[MAX_BITS+1];
jonathonfletcher 0:54f5be781526 204 /* number of codes at each bit length for an optimal tree */
jonathonfletcher 0:54f5be781526 205
jonathonfletcher 0:54f5be781526 206 int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
jonathonfletcher 0:54f5be781526 207 int heap_len; /* number of elements in the heap */
jonathonfletcher 0:54f5be781526 208 int heap_max; /* element of largest frequency */
jonathonfletcher 0:54f5be781526 209 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
jonathonfletcher 0:54f5be781526 210 * The same heap array is used to build all trees.
jonathonfletcher 0:54f5be781526 211 */
jonathonfletcher 0:54f5be781526 212
jonathonfletcher 0:54f5be781526 213 uch depth[2*L_CODES+1];
jonathonfletcher 0:54f5be781526 214 /* Depth of each subtree used as tie breaker for trees of equal frequency
jonathonfletcher 0:54f5be781526 215 */
jonathonfletcher 0:54f5be781526 216
jonathonfletcher 0:54f5be781526 217 uchf *l_buf; /* buffer for literals or lengths */
jonathonfletcher 0:54f5be781526 218
jonathonfletcher 0:54f5be781526 219 uInt lit_bufsize;
jonathonfletcher 0:54f5be781526 220 /* Size of match buffer for literals/lengths. There are 4 reasons for
jonathonfletcher 0:54f5be781526 221 * limiting lit_bufsize to 64K:
jonathonfletcher 0:54f5be781526 222 * - frequencies can be kept in 16 bit counters
jonathonfletcher 0:54f5be781526 223 * - if compression is not successful for the first block, all input
jonathonfletcher 0:54f5be781526 224 * data is still in the window so we can still emit a stored block even
jonathonfletcher 0:54f5be781526 225 * when input comes from standard input. (This can also be done for
jonathonfletcher 0:54f5be781526 226 * all blocks if lit_bufsize is not greater than 32K.)
jonathonfletcher 0:54f5be781526 227 * - if compression is not successful for a file smaller than 64K, we can
jonathonfletcher 0:54f5be781526 228 * even emit a stored file instead of a stored block (saving 5 bytes).
jonathonfletcher 0:54f5be781526 229 * This is applicable only for zip (not gzip or zlib).
jonathonfletcher 0:54f5be781526 230 * - creating new Huffman trees less frequently may not provide fast
jonathonfletcher 0:54f5be781526 231 * adaptation to changes in the input data statistics. (Take for
jonathonfletcher 0:54f5be781526 232 * example a binary file with poorly compressible code followed by
jonathonfletcher 0:54f5be781526 233 * a highly compressible string table.) Smaller buffer sizes give
jonathonfletcher 0:54f5be781526 234 * fast adaptation but have of course the overhead of transmitting
jonathonfletcher 0:54f5be781526 235 * trees more frequently.
jonathonfletcher 0:54f5be781526 236 * - I can't count above 4
jonathonfletcher 0:54f5be781526 237 */
jonathonfletcher 0:54f5be781526 238
jonathonfletcher 0:54f5be781526 239 uInt last_lit; /* running index in l_buf */
jonathonfletcher 0:54f5be781526 240
jonathonfletcher 0:54f5be781526 241 ushf *d_buf;
jonathonfletcher 0:54f5be781526 242 /* Buffer for distances. To simplify the code, d_buf and l_buf have
jonathonfletcher 0:54f5be781526 243 * the same number of elements. To use different lengths, an extra flag
jonathonfletcher 0:54f5be781526 244 * array would be necessary.
jonathonfletcher 0:54f5be781526 245 */
jonathonfletcher 0:54f5be781526 246
jonathonfletcher 0:54f5be781526 247 ulg opt_len; /* bit length of current block with optimal trees */
jonathonfletcher 0:54f5be781526 248 ulg static_len; /* bit length of current block with static trees */
jonathonfletcher 0:54f5be781526 249 uInt matches; /* number of string matches in current block */
jonathonfletcher 0:54f5be781526 250 uInt insert; /* bytes at end of window left to insert */
jonathonfletcher 0:54f5be781526 251
jonathonfletcher 0:54f5be781526 252 #ifdef DEBUG
jonathonfletcher 0:54f5be781526 253 ulg compressed_len; /* total bit length of compressed file mod 2^32 */
jonathonfletcher 0:54f5be781526 254 ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
jonathonfletcher 0:54f5be781526 255 #endif
jonathonfletcher 0:54f5be781526 256
jonathonfletcher 0:54f5be781526 257 ush bi_buf;
jonathonfletcher 0:54f5be781526 258 /* Output buffer. bits are inserted starting at the bottom (least
jonathonfletcher 0:54f5be781526 259 * significant bits).
jonathonfletcher 0:54f5be781526 260 */
jonathonfletcher 0:54f5be781526 261 int bi_valid;
jonathonfletcher 0:54f5be781526 262 /* Number of valid bits in bi_buf. All bits above the last valid bit
jonathonfletcher 0:54f5be781526 263 * are always zero.
jonathonfletcher 0:54f5be781526 264 */
jonathonfletcher 0:54f5be781526 265
jonathonfletcher 0:54f5be781526 266 ulg high_water;
jonathonfletcher 0:54f5be781526 267 /* High water mark offset in window for initialized bytes -- bytes above
jonathonfletcher 0:54f5be781526 268 * this are set to zero in order to avoid memory check warnings when
jonathonfletcher 0:54f5be781526 269 * longest match routines access bytes past the input. This is then
jonathonfletcher 0:54f5be781526 270 * updated to the new high water mark.
jonathonfletcher 0:54f5be781526 271 */
jonathonfletcher 0:54f5be781526 272
jonathonfletcher 0:54f5be781526 273 } FAR deflate_state;
jonathonfletcher 0:54f5be781526 274
jonathonfletcher 0:54f5be781526 275 /* Output a byte on the stream.
jonathonfletcher 0:54f5be781526 276 * IN assertion: there is enough room in pending_buf.
jonathonfletcher 0:54f5be781526 277 */
jonathonfletcher 0:54f5be781526 278 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
jonathonfletcher 0:54f5be781526 279
jonathonfletcher 0:54f5be781526 280
jonathonfletcher 0:54f5be781526 281 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
jonathonfletcher 0:54f5be781526 282 /* Minimum amount of lookahead, except at the end of the input file.
jonathonfletcher 0:54f5be781526 283 * See deflate.c for comments about the MIN_MATCH+1.
jonathonfletcher 0:54f5be781526 284 */
jonathonfletcher 0:54f5be781526 285
jonathonfletcher 0:54f5be781526 286 #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
jonathonfletcher 0:54f5be781526 287 /* In order to simplify the code, particularly on 16 bit machines, match
jonathonfletcher 0:54f5be781526 288 * distances are limited to MAX_DIST instead of WSIZE.
jonathonfletcher 0:54f5be781526 289 */
jonathonfletcher 0:54f5be781526 290
jonathonfletcher 0:54f5be781526 291 #define WIN_INIT MAX_MATCH
jonathonfletcher 0:54f5be781526 292 /* Number of bytes after end of data in window to initialize in order to avoid
jonathonfletcher 0:54f5be781526 293 memory checker errors from longest match routines */
jonathonfletcher 0:54f5be781526 294
jonathonfletcher 0:54f5be781526 295 /* in trees.c */
jonathonfletcher 0:54f5be781526 296 void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
jonathonfletcher 0:54f5be781526 297 int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
jonathonfletcher 0:54f5be781526 298 void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
jonathonfletcher 0:54f5be781526 299 ulg stored_len, int last));
jonathonfletcher 0:54f5be781526 300 void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
jonathonfletcher 0:54f5be781526 301 void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
jonathonfletcher 0:54f5be781526 302 void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
jonathonfletcher 0:54f5be781526 303 ulg stored_len, int last));
jonathonfletcher 0:54f5be781526 304
jonathonfletcher 0:54f5be781526 305 #define d_code(dist) \
jonathonfletcher 0:54f5be781526 306 ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
jonathonfletcher 0:54f5be781526 307 /* Mapping from a distance to a distance code. dist is the distance - 1 and
jonathonfletcher 0:54f5be781526 308 * must not have side effects. _dist_code[256] and _dist_code[257] are never
jonathonfletcher 0:54f5be781526 309 * used.
jonathonfletcher 0:54f5be781526 310 */
jonathonfletcher 0:54f5be781526 311
jonathonfletcher 0:54f5be781526 312 #ifndef DEBUG
jonathonfletcher 0:54f5be781526 313 /* Inline versions of _tr_tally for speed: */
jonathonfletcher 0:54f5be781526 314
jonathonfletcher 0:54f5be781526 315 #if defined(GEN_TREES_H) || !defined(STDC)
jonathonfletcher 0:54f5be781526 316 extern uch ZLIB_INTERNAL _length_code[];
jonathonfletcher 0:54f5be781526 317 extern uch ZLIB_INTERNAL _dist_code[];
jonathonfletcher 0:54f5be781526 318 #else
jonathonfletcher 0:54f5be781526 319 extern const uch ZLIB_INTERNAL _length_code[];
jonathonfletcher 0:54f5be781526 320 extern const uch ZLIB_INTERNAL _dist_code[];
jonathonfletcher 0:54f5be781526 321 #endif
jonathonfletcher 0:54f5be781526 322
jonathonfletcher 0:54f5be781526 323 # define _tr_tally_lit(s, c, flush) \
jonathonfletcher 0:54f5be781526 324 { uch cc = (c); \
jonathonfletcher 0:54f5be781526 325 s->d_buf[s->last_lit] = 0; \
jonathonfletcher 0:54f5be781526 326 s->l_buf[s->last_lit++] = cc; \
jonathonfletcher 0:54f5be781526 327 s->dyn_ltree[cc].Freq++; \
jonathonfletcher 0:54f5be781526 328 flush = (s->last_lit == s->lit_bufsize-1); \
jonathonfletcher 0:54f5be781526 329 }
jonathonfletcher 0:54f5be781526 330 # define _tr_tally_dist(s, distance, length, flush) \
jonathonfletcher 0:54f5be781526 331 { uch len = (length); \
jonathonfletcher 0:54f5be781526 332 ush dist = (distance); \
jonathonfletcher 0:54f5be781526 333 s->d_buf[s->last_lit] = dist; \
jonathonfletcher 0:54f5be781526 334 s->l_buf[s->last_lit++] = len; \
jonathonfletcher 0:54f5be781526 335 dist--; \
jonathonfletcher 0:54f5be781526 336 s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
jonathonfletcher 0:54f5be781526 337 s->dyn_dtree[d_code(dist)].Freq++; \
jonathonfletcher 0:54f5be781526 338 flush = (s->last_lit == s->lit_bufsize-1); \
jonathonfletcher 0:54f5be781526 339 }
jonathonfletcher 0:54f5be781526 340 #else
jonathonfletcher 0:54f5be781526 341 # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
jonathonfletcher 0:54f5be781526 342 # define _tr_tally_dist(s, distance, length, flush) \
jonathonfletcher 0:54f5be781526 343 flush = _tr_tally(s, distance, length)
jonathonfletcher 0:54f5be781526 344 #endif
jonathonfletcher 0:54f5be781526 345
jonathonfletcher 0:54f5be781526 346 #endif /* DEFLATE_H */