on master help sign in
zlib-bindings
commitb35a59c4a55077a816565a9143750e0999382918 authorMichael Snoyman <michael@snoyman.com> dateSun 10 Oct 2010, 10:18 (16 years ago) parentfc5379c
25 files changed+37−10943
Network/Wai/Zlib.hs+29−84 c/helper.c−56 cbits/adler32.c−149 cbits/compress.c−79 cbits/crc32.c−423 cbits/crc32.h−441 cbits/deflate.c−1736 cbits/deflate.h−331 cbits/gzio.c−1026 cbits/infback.c−623 cbits/inffast.c−318 cbits/inffast.h−11 cbits/inffixed.h−94 cbits/inflate.c−1368 cbits/inflate.h−115 cbits/inftrees.c−329 cbits/inftrees.h−55 cbits/trees.c−1219 cbits/trees.h−128 cbits/uncompr.c−61 cbits/zconf.h−332 cbits/zlib.h−1357 cbits/zutil.c−318 cbits/zutil.h−269 wai-extra.cabal+8−21
Network/Wai/Zlib.hs+29−84
@@ -1,106 +1,51 @@
1−{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2−{-# LANGUAGE EmptyDataDecls #-}
31module Network.Wai.Zlib (compress) where
42
5−import Prelude hiding (length)
6−import Network.Wai hiding (status)
3+import Prelude
4+import Network.Wai
5+import Data.ByteString (ByteString)
76
8−import Data.ByteString.Unsafe
9−import Data.ByteString.Lazy.Internal (defaultChunkSize)
10−
11−import Foreign.C
12−import Foreign.Ptr
13−import Foreign.Marshal.Alloc
14−import Control.Exception
15−import Control.Monad
16−
17−import Data.ByteString.Char8 hiding (putStrLn)
18−
19−chunkSize :: Int
20−chunkSize = defaultChunkSize
7+import Codec.Zlib
218
229compress :: Enumerator -> Enumerator
2310compress enum =
24− Enumerator $ \iter acc ->
25− bracket
26− c_create_z_stream
27− c_free_z_stream
28− (compressInner iter acc enum)
11+ Enumerator $ \iter acc -> do
12+ def <- initDeflate 7 $ WindowBits 31
13+ compressInner iter acc enum def
2914
3015compressInner :: (acc -> ByteString -> IO (Either acc acc))
3116 -> acc
3217 -> Enumerator
33− -> ZStream
18+ -> Deflate
3419 -> IO (Either acc acc)
35−compressInner iter acc enum stream = allocaBytes chunkSize $ \outbuff -> do
36− when (stream == nullPtr) $ error "Error from zlib library"
37− c_set_avail_out stream outbuff $ fromIntegral chunkSize
38− eacc <- runEnumerator enum (compressIter iter stream outbuff) acc
20+compressInner iter acc enum def = do
21+ eacc <- runEnumerator enum (compressIter iter def) acc
3922 case eacc of
4023 Left acc' -> return $ Left acc'
41− Right acc' -> finishStream iter stream outbuff acc'
42−
43−drainOutput :: Bool -- ^ are we finishing?
44− -> ZStream
45− -> Ptr CChar -- ^ output buffer
46− -> (acc -> ByteString -> IO (Either acc acc))
47− -> acc
48− -> IO (Either acc acc)
49−drainOutput isFinish stream outbuff iter acc = do
50− if isFinish
51− then c_call_deflate_finish stream >> return ()
52− else c_call_deflate_noflush stream
53− newAvail <- fromIntegral `fmap` c_get_avail_out stream
54− if newAvail == 0 || (isFinish && newAvail /= chunkSize)
55− then do
56− bs <- unsafePackCStringLen (outbuff, chunkSize - newAvail)
57− eacc <- iter acc bs
58− case eacc of
59− Left acc' -> return $ Left acc'
60− Right acc' -> do
61− c_set_avail_out stream outbuff $ fromIntegral chunkSize
62− drainOutput isFinish stream outbuff iter acc'
63− else return $ Right acc
24+ Right acc' -> finishStream iter def acc'
6425
6526finishStream :: (acc -> ByteString -> IO (Either acc acc))
66− -> ZStream
67− -> Ptr CChar
27+ -> Deflate
6828 -> acc
6929 -> IO (Either acc acc)
70−finishStream iter stream outbuff acc =
71− drainOutput True stream outbuff iter acc
30+finishStream iter def acc = finishDeflate def $ drain iter acc
7231
7332compressIter :: (acc -> ByteString -> IO (Either acc acc))
74− -> ZStream
75− -> Ptr CChar
33+ -> Deflate
7634 -> acc
7735 -> ByteString
7836 -> IO (Either acc acc)
79−compressIter iter stream outbuff acc bsInput = do
80− unsafeUseAsCStringLen bsInput $ \(cstr, len) ->
81− c_set_avail_in stream cstr $ fromIntegral len
82− drainOutput False stream outbuff iter acc
83−
84−data ZStreamStruct
85−type ZStream = Ptr ZStreamStruct
86−
87−foreign import ccall unsafe "create_z_stream"
88− c_create_z_stream :: IO ZStream
89−
90−foreign import ccall unsafe "free_z_stream"
91− c_free_z_stream :: ZStream -> IO ()
92−
93−foreign import ccall unsafe "set_avail_in"
94− c_set_avail_in :: ZStream -> Ptr CChar -> CUInt -> IO ()
95−
96−foreign import ccall unsafe "set_avail_out"
97− c_set_avail_out :: ZStream -> Ptr CChar -> CUInt -> IO ()
98−
99−foreign import ccall unsafe "call_deflate_noflush"
100− c_call_deflate_noflush :: ZStream -> IO ()
101−
102−foreign import ccall unsafe "call_deflate_finish"
103− c_call_deflate_finish :: ZStream -> IO CInt
104−
105−foreign import ccall unsafe "get_avail_out"
106− c_get_avail_out :: ZStream -> IO CUInt
37+compressIter iter def acc bsI = withDeflateInput def bsI $ drain iter acc
38+
39+drain :: (acc -> ByteString -> IO (Either acc acc))
40+ -> acc
41+ -> IO (Maybe ByteString)
42+ -> IO (Either acc acc)
43+drain iter acc pop = do
44+ mbs <- pop
45+ case mbs of
46+ Nothing -> return $ Right acc
47+ Just bs -> do
48+ eacc' <- iter acc bs
49+ case eacc' of
50+ Left acc' -> return $ Left acc'
51+ Right acc' -> drain iter acc' pop
c/helper.cdeleted−56
@@ -1,56 +0,0 @@
1−#include <zlib.h>
2−#include <stdlib.h>
3−#include <assert.h>
4−#include <stdio.h>
5−
6−z_stream * create_z_stream (void)
7−{
8− z_stream *ret = malloc(sizeof(z_stream));
9− ret->zalloc = Z_NULL;
10− ret->zfree = Z_NULL;
11− ret->opaque = Z_NULL;
12− ret->next_in = NULL;
13− ret->avail_in = 0;
14−
15− if (deflateInit2(ret, 7, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY)
16− != Z_OK) return NULL;
17− else return ret;
18−}
19−
20−void free_z_stream (z_stream *stream)
21−{
22− deflateEnd(stream);
23− free(stream);
24−}
25−
26−void set_avail_in (z_stream *stream, char *buff, unsigned int avail)
27−{
28− stream->next_in = buff;
29− stream->avail_in = avail;
30−}
31−
32−void set_avail_out (z_stream *stream, char *buff, unsigned int avail)
33−{
34− stream->next_out = buff;
35− stream->avail_out = avail;
36−}
37−
38−void call_deflate_noflush (z_stream *stream)
39−{
40− int ret;
41− ret = deflate(stream, Z_NO_FLUSH);
42− assert (ret == Z_OK);
43−}
44−
45−int call_deflate_finish (z_stream *stream)
46−{
47− int ret;
48− ret = deflate(stream, Z_FINISH);
49− assert (ret == Z_OK || ret == Z_STREAM_END);
50− return ret;
51−}
52−
53−unsigned int get_avail_out (z_stream *stream)
54−{
55− return stream->avail_out;
56−}
cbits/adler32.cdeleted−149
@@ -1,149 +0,0 @@
1−/* adler32.c -- compute the Adler-32 checksum of a data stream
2− * Copyright (C) 1995-2004 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#define ZLIB_INTERNAL
9−#include "zlib.h"
10−
11−#define BASE 65521UL /* largest prime smaller than 65536 */
12−#define NMAX 5552
13−/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
14−
15−#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
16−#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
17−#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
18−#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
19−#define DO16(buf) DO8(buf,0); DO8(buf,8);
20−
21−/* use NO_DIVIDE if your processor does not do division in hardware */
22−#ifdef NO_DIVIDE
23−# define MOD(a) \
24− do { \
25− if (a >= (BASE << 16)) a -= (BASE << 16); \
26− if (a >= (BASE << 15)) a -= (BASE << 15); \
27− if (a >= (BASE << 14)) a -= (BASE << 14); \
28− if (a >= (BASE << 13)) a -= (BASE << 13); \
29− if (a >= (BASE << 12)) a -= (BASE << 12); \
30− if (a >= (BASE << 11)) a -= (BASE << 11); \
31− if (a >= (BASE << 10)) a -= (BASE << 10); \
32− if (a >= (BASE << 9)) a -= (BASE << 9); \
33− if (a >= (BASE << 8)) a -= (BASE << 8); \
34− if (a >= (BASE << 7)) a -= (BASE << 7); \
35− if (a >= (BASE << 6)) a -= (BASE << 6); \
36− if (a >= (BASE << 5)) a -= (BASE << 5); \
37− if (a >= (BASE << 4)) a -= (BASE << 4); \
38− if (a >= (BASE << 3)) a -= (BASE << 3); \
39− if (a >= (BASE << 2)) a -= (BASE << 2); \
40− if (a >= (BASE << 1)) a -= (BASE << 1); \
41− if (a >= BASE) a -= BASE; \
42− } while (0)
43−# define MOD4(a) \
44− do { \
45− if (a >= (BASE << 4)) a -= (BASE << 4); \
46− if (a >= (BASE << 3)) a -= (BASE << 3); \
47− if (a >= (BASE << 2)) a -= (BASE << 2); \
48− if (a >= (BASE << 1)) a -= (BASE << 1); \
49− if (a >= BASE) a -= BASE; \
50− } while (0)
51−#else
52−# define MOD(a) a %= BASE
53−# define MOD4(a) a %= BASE
54−#endif
55−
56−/* ========================================================================= */
57−uLong ZEXPORT adler32(adler, buf, len)
58− uLong adler;
59− const Bytef *buf;
60− uInt len;
61−{
62− unsigned long sum2;
63− unsigned n;
64−
65− /* split Adler-32 into component sums */
66− sum2 = (adler >> 16) & 0xffff;
67− adler &= 0xffff;
68−
69− /* in case user likes doing a byte at a time, keep it fast */
70− if (len == 1) {
71− adler += buf[0];
72− if (adler >= BASE)
73− adler -= BASE;
74− sum2 += adler;
75− if (sum2 >= BASE)
76− sum2 -= BASE;
77− return adler | (sum2 << 16);
78− }
79−
80− /* initial Adler-32 value (deferred check for len == 1 speed) */
81− if (buf == Z_NULL)
82− return 1L;
83−
84− /* in case short lengths are provided, keep it somewhat fast */
85− if (len < 16) {
86− while (len--) {
87− adler += *buf++;
88− sum2 += adler;
89− }
90− if (adler >= BASE)
91− adler -= BASE;
92− MOD4(sum2); /* only added so many BASE's */
93− return adler | (sum2 << 16);
94− }
95−
96− /* do length NMAX blocks -- requires just one modulo operation */
97− while (len >= NMAX) {
98− len -= NMAX;
99− n = NMAX / 16; /* NMAX is divisible by 16 */
100− do {
101− DO16(buf); /* 16 sums unrolled */
102− buf += 16;
103− } while (--n);
104− MOD(adler);
105− MOD(sum2);
106− }
107−
108− /* do remaining bytes (less than NMAX, still just one modulo) */
109− if (len) { /* avoid modulos if none remaining */
110− while (len >= 16) {
111− len -= 16;
112− DO16(buf);
113− buf += 16;
114− }
115− while (len--) {
116− adler += *buf++;
117− sum2 += adler;
118− }
119− MOD(adler);
120− MOD(sum2);
121− }
122−
123− /* return recombined sums */
124− return adler | (sum2 << 16);
125−}
126−
127−/* ========================================================================= */
128−uLong ZEXPORT adler32_combine(adler1, adler2, len2)
129− uLong adler1;
130− uLong adler2;
131− z_off_t len2;
132−{
133− unsigned long sum1;
134− unsigned long sum2;
135− unsigned rem;
136−
137− /* the derivation of this formula is left as an exercise for the reader */
138− rem = (unsigned)(len2 % BASE);
139− sum1 = adler1 & 0xffff;
140− sum2 = rem * sum1;
141− MOD(sum2);
142− sum1 += (adler2 & 0xffff) + BASE - 1;
143− sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
144− if (sum1 > BASE) sum1 -= BASE;
145− if (sum1 > BASE) sum1 -= BASE;
146− if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
147− if (sum2 > BASE) sum2 -= BASE;
148− return sum1 | (sum2 << 16);
149−}
cbits/compress.cdeleted−79
@@ -1,79 +0,0 @@
1−/* compress.c -- compress a memory buffer
2− * Copyright (C) 1995-2003 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#define ZLIB_INTERNAL
9−#include "zlib.h"
10−
11−/* ===========================================================================
12− Compresses the source buffer into the destination buffer. The level
13− parameter has the same meaning as in deflateInit. sourceLen is the byte
14− length of the source buffer. Upon entry, destLen is the total size of the
15− destination buffer, which must be at least 0.1% larger than sourceLen plus
16− 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17−
18− compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19− memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20− Z_STREAM_ERROR if the level parameter is invalid.
21−*/
22−int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
23− Bytef *dest;
24− uLongf *destLen;
25− const Bytef *source;
26− uLong sourceLen;
27− int level;
28−{
29− z_stream stream;
30− int err;
31−
32− stream.next_in = (Bytef*)source;
33− stream.avail_in = (uInt)sourceLen;
34−#ifdef MAXSEG_64K
35− /* Check for source > 64K on 16-bit machine: */
36− if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
37−#endif
38− stream.next_out = dest;
39− stream.avail_out = (uInt)*destLen;
40− if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
41−
42− stream.zalloc = (alloc_func)0;
43− stream.zfree = (free_func)0;
44− stream.opaque = (voidpf)0;
45−
46− err = deflateInit(&stream, level);
47− if (err != Z_OK) return err;
48−
49− err = deflate(&stream, Z_FINISH);
50− if (err != Z_STREAM_END) {
51− deflateEnd(&stream);
52− return err == Z_OK ? Z_BUF_ERROR : err;
53− }
54− *destLen = stream.total_out;
55−
56− err = deflateEnd(&stream);
57− return err;
58−}
59−
60−/* ===========================================================================
61− */
62−int ZEXPORT compress (dest, destLen, source, sourceLen)
63− Bytef *dest;
64− uLongf *destLen;
65− const Bytef *source;
66− uLong sourceLen;
67−{
68− return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
69−}
70−
71−/* ===========================================================================
72− If the default memLevel or windowBits for deflateInit() is changed, then
73− this function needs to be updated.
74− */
75−uLong ZEXPORT compressBound (sourceLen)
76− uLong sourceLen;
77−{
78− return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
79−}
cbits/crc32.cdeleted−423
@@ -1,423 +0,0 @@
1−/* crc32.c -- compute the CRC-32 of a data stream
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− *
5− * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
6− * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
7− * tables for updating the shift register in one step with three exclusive-ors
8− * instead of four steps with four exclusive-ors. This results in about a
9− * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
10− */
11−
12−/* @(#) $Id$ */
13−
14−/*
15− Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
16− protection on the static variables used to control the first-use generation
17− of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
18− first call get_crc_table() to initialize the tables before allowing more than
19− one thread to use crc32().
20− */
21−
22−#ifdef MAKECRCH
23−# include <stdio.h>
24−# ifndef DYNAMIC_CRC_TABLE
25−# define DYNAMIC_CRC_TABLE
26−# endif /* !DYNAMIC_CRC_TABLE */
27−#endif /* MAKECRCH */
28−
29−#include "zutil.h" /* for STDC and FAR definitions */
30−
31−#define local static
32−
33−/* Find a four-byte integer type for crc32_little() and crc32_big(). */
34−#ifndef NOBYFOUR
35−# ifdef STDC /* need ANSI C limits.h to determine sizes */
36−# include <limits.h>
37−# define BYFOUR
38−# if (UINT_MAX == 0xffffffffUL)
39− typedef unsigned int u4;
40−# else
41−# if (ULONG_MAX == 0xffffffffUL)
42− typedef unsigned long u4;
43−# else
44−# if (USHRT_MAX == 0xffffffffUL)
45− typedef unsigned short u4;
46−# else
47−# undef BYFOUR /* can't find a four-byte integer type! */
48−# endif
49−# endif
50−# endif
51−# endif /* STDC */
52−#endif /* !NOBYFOUR */
53−
54−/* Definitions for doing the crc four data bytes at a time. */
55−#ifdef BYFOUR
56−# define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
57− (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58− local unsigned long crc32_little OF((unsigned long,
59− const unsigned char FAR *, unsigned));
60− local unsigned long crc32_big OF((unsigned long,
61− const unsigned char FAR *, unsigned));
62−# define TBLS 8
63−#else
64−# define TBLS 1
65−#endif /* BYFOUR */
66−
67−/* Local functions for crc concatenation */
68−local unsigned long gf2_matrix_times OF((unsigned long *mat,
69− unsigned long vec));
70−local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
71−
72−#ifdef DYNAMIC_CRC_TABLE
73−
74−local volatile int crc_table_empty = 1;
75−local unsigned long FAR crc_table[TBLS][256];
76−local void make_crc_table OF((void));
77−#ifdef MAKECRCH
78− local void write_table OF((FILE *, const unsigned long FAR *));
79−#endif /* MAKECRCH */
80−/*
81− Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
82− x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
83−
84− Polynomials over GF(2) are represented in binary, one bit per coefficient,
85− with the lowest powers in the most significant bit. Then adding polynomials
86− is just exclusive-or, and multiplying a polynomial by x is a right shift by
87− one. If we call the above polynomial p, and represent a byte as the
88− polynomial q, also with the lowest power in the most significant bit (so the
89− byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
90− where a mod b means the remainder after dividing a by b.
91−
92− This calculation is done using the shift-register method of multiplying and
93− taking the remainder. The register is initialized to zero, and for each
94− incoming bit, x^32 is added mod p to the register if the bit is a one (where
95− x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
96− x (which is shifting right by one and adding x^32 mod p if the bit shifted
97− out is a one). We start with the highest power (least significant bit) of
98− q and repeat for all eight bits of q.
99−
100− The first table is simply the CRC of all possible eight bit values. This is
101− all the information needed to generate CRCs on data a byte at a time for all
102− combinations of CRC register values and incoming bytes. The remaining tables
103− allow for word-at-a-time CRC calculation for both big-endian and little-
104− endian machines, where a word is four bytes.
105−*/
106−local void make_crc_table()
107−{
108− unsigned long c;
109− int n, k;
110− unsigned long poly; /* polynomial exclusive-or pattern */
111− /* terms of polynomial defining this crc (except x^32): */
112− static volatile int first = 1; /* flag to limit concurrent making */
113− static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
114−
115− /* See if another task is already doing this (not thread-safe, but better
116− than nothing -- significantly reduces duration of vulnerability in
117− case the advice about DYNAMIC_CRC_TABLE is ignored) */
118− if (first) {
119− first = 0;
120−
121− /* make exclusive-or pattern from polynomial (0xedb88320UL) */
122− poly = 0UL;
123− for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
124− poly |= 1UL << (31 - p[n]);
125−
126− /* generate a crc for every 8-bit value */
127− for (n = 0; n < 256; n++) {
128− c = (unsigned long)n;
129− for (k = 0; k < 8; k++)
130− c = c & 1 ? poly ^ (c >> 1) : c >> 1;
131− crc_table[0][n] = c;
132− }
133−
134−#ifdef BYFOUR
135− /* generate crc for each value followed by one, two, and three zeros,
136− and then the byte reversal of those as well as the first table */
137− for (n = 0; n < 256; n++) {
138− c = crc_table[0][n];
139− crc_table[4][n] = REV(c);
140− for (k = 1; k < 4; k++) {
141− c = crc_table[0][c & 0xff] ^ (c >> 8);
142− crc_table[k][n] = c;
143− crc_table[k + 4][n] = REV(c);
144− }
145− }
146−#endif /* BYFOUR */
147−
148− crc_table_empty = 0;
149− }
150− else { /* not first */
151− /* wait for the other guy to finish (not efficient, but rare) */
152− while (crc_table_empty)
153− ;
154− }
155−
156−#ifdef MAKECRCH
157− /* write out CRC tables to crc32.h */
158− {
159− FILE *out;
160−
161− out = fopen("crc32.h", "w");
162− if (out == NULL) return;
163− fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
164− fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
165− fprintf(out, "local const unsigned long FAR ");
166− fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
167− write_table(out, crc_table[0]);
168−# ifdef BYFOUR
169− fprintf(out, "#ifdef BYFOUR\n");
170− for (k = 1; k < 8; k++) {
171− fprintf(out, " },\n {\n");
172− write_table(out, crc_table[k]);
173− }
174− fprintf(out, "#endif\n");
175−# endif /* BYFOUR */
176− fprintf(out, " }\n};\n");
177− fclose(out);
178− }
179−#endif /* MAKECRCH */
180−}
181−
182−#ifdef MAKECRCH
183−local void write_table(out, table)
184− FILE *out;
185− const unsigned long FAR *table;
186−{
187− int n;
188−
189− for (n = 0; n < 256; n++)
190− fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n],
191− n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
192−}
193−#endif /* MAKECRCH */
194−
195−#else /* !DYNAMIC_CRC_TABLE */
196−/* ========================================================================
197− * Tables of CRC-32s of all single-byte values, made by make_crc_table().
198− */
199−#include "crc32.h"
200−#endif /* DYNAMIC_CRC_TABLE */
201−
202−/* =========================================================================
203− * This function can be used by asm versions of crc32()
204− */
205−const unsigned long FAR * ZEXPORT get_crc_table()
206−{
207−#ifdef DYNAMIC_CRC_TABLE
208− if (crc_table_empty)
209− make_crc_table();
210−#endif /* DYNAMIC_CRC_TABLE */
211− return (const unsigned long FAR *)crc_table;
212−}
213−
214−/* ========================================================================= */
215−#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
216−#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
217−
218−/* ========================================================================= */
219−unsigned long ZEXPORT crc32(crc, buf, len)
220− unsigned long crc;
221− const unsigned char FAR *buf;
222− unsigned len;
223−{
224− if (buf == Z_NULL) return 0UL;
225−
226−#ifdef DYNAMIC_CRC_TABLE
227− if (crc_table_empty)
228− make_crc_table();
229−#endif /* DYNAMIC_CRC_TABLE */
230−
231−#ifdef BYFOUR
232− if (sizeof(void *) == sizeof(ptrdiff_t)) {
233− u4 endian;
234−
235− endian = 1;
236− if (*((unsigned char *)(&endian)))
237− return crc32_little(crc, buf, len);
238− else
239− return crc32_big(crc, buf, len);
240− }
241−#endif /* BYFOUR */
242− crc = crc ^ 0xffffffffUL;
243− while (len >= 8) {
244− DO8;
245− len -= 8;
246− }
247− if (len) do {
248− DO1;
249− } while (--len);
250− return crc ^ 0xffffffffUL;
251−}
252−
253−#ifdef BYFOUR
254−
255−/* ========================================================================= */
256−#define DOLIT4 c ^= *buf4++; \
257− c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
258− crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
259−#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
260−
261−/* ========================================================================= */
262−local unsigned long crc32_little(crc, buf, len)
263− unsigned long crc;
264− const unsigned char FAR *buf;
265− unsigned len;
266−{
267− register u4 c;
268− register const u4 FAR *buf4;
269−
270− c = (u4)crc;
271− c = ~c;
272− while (len && ((ptrdiff_t)buf & 3)) {
273− c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
274− len--;
275− }
276−
277− buf4 = (const u4 FAR *)(const void FAR *)buf;
278− while (len >= 32) {
279− DOLIT32;
280− len -= 32;
281− }
282− while (len >= 4) {
283− DOLIT4;
284− len -= 4;
285− }
286− buf = (const unsigned char FAR *)buf4;
287−
288− if (len) do {
289− c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
290− } while (--len);
291− c = ~c;
292− return (unsigned long)c;
293−}
294−
295−/* ========================================================================= */
296−#define DOBIG4 c ^= *++buf4; \
297− c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
298− crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
299−#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
300−
301−/* ========================================================================= */
302−local unsigned long crc32_big(crc, buf, len)
303− unsigned long crc;
304− const unsigned char FAR *buf;
305− unsigned len;
306−{
307− register u4 c;
308− register const u4 FAR *buf4;
309−
310− c = REV((u4)crc);
311− c = ~c;
312− while (len && ((ptrdiff_t)buf & 3)) {
313− c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
314− len--;
315− }
316−
317− buf4 = (const u4 FAR *)(const void FAR *)buf;
318− buf4--;
319− while (len >= 32) {
320− DOBIG32;
321− len -= 32;
322− }
323− while (len >= 4) {
324− DOBIG4;
325− len -= 4;
326− }
327− buf4++;
328− buf = (const unsigned char FAR *)buf4;
329−
330− if (len) do {
331− c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
332− } while (--len);
333− c = ~c;
334− return (unsigned long)(REV(c));
335−}
336−
337−#endif /* BYFOUR */
338−
339−#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
340−
341−/* ========================================================================= */
342−local unsigned long gf2_matrix_times(mat, vec)
343− unsigned long *mat;
344− unsigned long vec;
345−{
346− unsigned long sum;
347−
348− sum = 0;
349− while (vec) {
350− if (vec & 1)
351− sum ^= *mat;
352− vec >>= 1;
353− mat++;
354− }
355− return sum;
356−}
357−
358−/* ========================================================================= */
359−local void gf2_matrix_square(square, mat)
360− unsigned long *square;
361− unsigned long *mat;
362−{
363− int n;
364−
365− for (n = 0; n < GF2_DIM; n++)
366− square[n] = gf2_matrix_times(mat, mat[n]);
367−}
368−
369−/* ========================================================================= */
370−uLong ZEXPORT crc32_combine(crc1, crc2, len2)
371− uLong crc1;
372− uLong crc2;
373− z_off_t len2;
374−{
375− int n;
376− unsigned long row;
377− unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
378− unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
379−
380− /* degenerate case */
381− if (len2 == 0)
382− return crc1;
383−
384− /* put operator for one zero bit in odd */
385− odd[0] = 0xedb88320L; /* CRC-32 polynomial */
386− row = 1;
387− for (n = 1; n < GF2_DIM; n++) {
388− odd[n] = row;
389− row <<= 1;
390− }
391−
392− /* put operator for two zero bits in even */
393− gf2_matrix_square(even, odd);
394−
395− /* put operator for four zero bits in odd */
396− gf2_matrix_square(odd, even);
397−
398− /* apply len2 zeros to crc1 (first square will put the operator for one
399− zero byte, eight zero bits, in even) */
400− do {
401− /* apply zeros operator for this bit of len2 */
402− gf2_matrix_square(even, odd);
403− if (len2 & 1)
404− crc1 = gf2_matrix_times(even, crc1);
405− len2 >>= 1;
406−
407− /* if no more bits set, then done */
408− if (len2 == 0)
409− break;
410−
411− /* another iteration of the loop with odd and even swapped */
412− gf2_matrix_square(odd, even);
413− if (len2 & 1)
414− crc1 = gf2_matrix_times(odd, crc1);
415− len2 >>= 1;
416−
417− /* if no more bits set, then done */
418− } while (len2 != 0);
419−
420− /* return combined crc */
421− crc1 ^= crc2;
422− return crc1;
423−}
cbits/crc32.hdeleted−441
@@ -1,441 +0,0 @@
1−/* crc32.h -- tables for rapid CRC calculation
2− * Generated automatically by crc32.c
3− */
4−
5−local const unsigned long FAR crc_table[TBLS][256] =
6−{
7− {
8− 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
9− 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
10− 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
11− 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
12− 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
13− 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
14− 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
15− 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
16− 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
17− 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
18− 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
19− 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
20− 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
21− 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
22− 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
23− 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
24− 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
25− 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
26− 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
27− 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
28− 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
29− 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
30− 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
31− 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
32− 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
33− 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
34− 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
35− 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
36− 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
37− 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
38− 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
39− 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
40− 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
41− 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
42− 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
43− 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
44− 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
45− 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
46− 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
47− 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
48− 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
49− 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
50− 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
51− 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
52− 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
53− 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
54− 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
55− 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
56− 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
57− 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
58− 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
59− 0x2d02ef8dUL
60−#ifdef BYFOUR
61− },
62− {
63− 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
64− 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
65− 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
66− 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
67− 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
68− 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
69− 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
70− 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
71− 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
72− 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
73− 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
74− 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
75− 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
76− 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
77− 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
78− 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
79− 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
80− 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
81− 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
82− 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
83− 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
84− 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
85− 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
86− 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
87− 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
88− 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
89− 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
90− 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
91− 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
92− 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
93− 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
94− 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
95− 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
96− 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
97− 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
98− 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
99− 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
100− 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
101− 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
102− 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
103− 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
104− 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
105− 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
106− 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
107− 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
108− 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
109− 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
110− 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
111− 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
112− 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
113− 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
114− 0x9324fd72UL
115− },
116− {
117− 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
118− 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
119− 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
120− 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
121− 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
122− 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
123− 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
124− 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
125− 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
126− 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
127− 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
128− 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
129− 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
130− 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
131− 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
132− 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
133− 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
134− 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
135− 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
136− 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
137− 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
138− 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
139− 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
140− 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
141− 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
142− 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
143− 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
144− 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
145− 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
146− 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
147− 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
148− 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
149− 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
150− 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
151− 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
152− 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
153− 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
154− 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
155− 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
156− 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
157− 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
158− 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
159− 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
160− 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
161− 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
162− 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
163− 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
164− 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
165− 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
166− 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
167− 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
168− 0xbe9834edUL
169− },
170− {
171− 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
172− 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
173− 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
174− 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
175− 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
176− 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
177− 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
178− 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
179− 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
180− 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
181− 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
182− 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
183− 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
184− 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
185− 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
186− 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
187− 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
188− 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
189− 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
190− 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
191− 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
192− 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
193− 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
194− 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
195− 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
196− 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
197− 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
198− 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
199− 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
200− 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
201− 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
202− 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
203− 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
204− 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
205− 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
206− 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
207− 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
208− 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
209− 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
210− 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
211− 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
212− 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
213− 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
214− 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
215− 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
216− 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
217− 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
218− 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
219− 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
220− 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
221− 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
222− 0xde0506f1UL
223− },
224− {
225− 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
226− 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
227− 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
228− 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
229− 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
230− 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
231− 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
232− 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
233− 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
234− 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
235− 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
236− 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
237− 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
238− 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
239− 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
240− 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
241− 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
242− 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
243− 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
244− 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
245− 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
246− 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
247− 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
248− 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
249− 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
250− 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
251− 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
252− 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
253− 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
254− 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
255− 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
256− 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
257− 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
258− 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
259− 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
260− 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
261− 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
262− 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
263− 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
264− 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
265− 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
266− 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
267− 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
268− 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
269− 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
270− 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
271− 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
272− 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
273− 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
274− 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
275− 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
276− 0x8def022dUL
277− },
278− {
279− 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
280− 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
281− 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
282− 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
283− 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
284− 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
285− 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
286− 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
287− 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
288− 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
289− 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
290− 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
291− 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
292− 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
293− 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
294− 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
295− 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
296− 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
297− 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
298− 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
299− 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
300− 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
301− 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
302− 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
303− 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
304− 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
305− 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
306− 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
307− 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
308− 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
309− 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
310− 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
311− 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
312− 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
313− 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
314− 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
315− 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
316− 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
317− 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
318− 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
319− 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
320− 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
321− 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
322− 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
323− 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
324− 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
325− 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
326− 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
327− 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
328− 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
329− 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
330− 0x72fd2493UL
331− },
332− {
333− 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
334− 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
335− 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
336− 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
337− 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
338− 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
339− 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
340− 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
341− 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
342− 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
343− 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
344− 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
345− 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
346− 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
347− 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
348− 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
349− 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
350− 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
351− 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
352− 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
353− 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
354− 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
355− 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
356− 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
357− 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
358− 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
359− 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
360− 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
361− 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
362− 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
363− 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
364− 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
365− 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
366− 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
367− 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
368− 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
369− 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
370− 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
371− 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
372− 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
373− 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
374− 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
375− 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
376− 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
377− 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
378− 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
379− 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
380− 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
381− 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
382− 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
383− 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
384− 0xed3498beUL
385− },
386− {
387− 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
388− 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
389− 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
390− 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
391− 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
392− 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
393− 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
394− 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
395− 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
396− 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
397− 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
398− 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
399− 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
400− 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
401− 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
402− 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
403− 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
404− 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
405− 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
406− 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
407− 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
408− 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
409− 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
410− 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
411− 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
412− 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
413− 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
414− 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
415− 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
416− 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
417− 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
418− 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
419− 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
420− 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
421− 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
422− 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
423− 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
424− 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
425− 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
426− 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
427− 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
428− 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
429− 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
430− 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
431− 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
432− 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
433− 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
434− 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
435− 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
436− 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
437− 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
438− 0xf10605deUL
439−#endif
440− }
441−};
cbits/deflate.cdeleted−1736
@@ -1,1736 +0,0 @@
1−/* deflate.c -- compress data using the deflation algorithm
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− * ALGORITHM
8− *
9− * The "deflation" process depends on being able to identify portions
10− * of the input text which are identical to earlier input (within a
11− * sliding window trailing behind the input currently being processed).
12− *
13− * The most straightforward technique turns out to be the fastest for
14− * most input files: try all possible matches and select the longest.
15− * The key feature of this algorithm is that insertions into the string
16− * dictionary are very simple and thus fast, and deletions are avoided
17− * completely. Insertions are performed at each input character, whereas
18− * string matches are performed only when the previous match ends. So it
19− * is preferable to spend more time in matches to allow very fast string
20− * insertions and avoid deletions. The matching algorithm for small
21− * strings is inspired from that of Rabin & Karp. A brute force approach
22− * is used to find longer strings when a small match has been found.
23− * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24− * (by Leonid Broukhis).
25− * A previous version of this file used a more sophisticated algorithm
26− * (by Fiala and Greene) which is guaranteed to run in linear amortized
27− * time, but has a larger average cost, uses more memory and is patented.
28− * However the F&G algorithm may be faster for some highly redundant
29− * files if the parameter max_chain_length (described below) is too large.
30− *
31− * ACKNOWLEDGEMENTS
32− *
33− * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34− * I found it in 'freeze' written by Leonid Broukhis.
35− * Thanks to many people for bug reports and testing.
36− *
37− * REFERENCES
38− *
39− * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40− * Available in http://www.ietf.org/rfc/rfc1951.txt
41− *
42− * A description of the Rabin and Karp algorithm is given in the book
43− * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44− *
45− * Fiala,E.R., and Greene,D.H.
46− * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47− *
48− */
49−
50−/* @(#) $Id$ */
51−
52−#include "deflate.h"
53−
54−const char deflate_copyright[] =
55− " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
56−/*
57− If you use the zlib library in a product, an acknowledgment is welcome
58− in the documentation of your product. If for some reason you cannot
59− include such an acknowledgment, I would appreciate that you keep this
60− copyright string in the executable of your product.
61− */
62−
63−/* ===========================================================================
64− * Function prototypes.
65− */
66−typedef enum {
67− need_more, /* block not completed, need more input or more output */
68− block_done, /* block flush performed */
69− finish_started, /* finish started, need only more output at next deflate */
70− finish_done /* finish done, accept no more input or output */
71−} block_state;
72−
73−typedef block_state (*compress_func) OF((deflate_state *s, int flush));
74−/* Compression function. Returns the block state after the call. */
75−
76−local void fill_window OF((deflate_state *s));
77−local block_state deflate_stored OF((deflate_state *s, int flush));
78−local block_state deflate_fast OF((deflate_state *s, int flush));
79−#ifndef FASTEST
80−local block_state deflate_slow OF((deflate_state *s, int flush));
81−#endif
82−local void lm_init OF((deflate_state *s));
83−local void putShortMSB OF((deflate_state *s, uInt b));
84−local void flush_pending OF((z_streamp strm));
85−local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
86−#ifndef FASTEST
87−#ifdef ASMV
88− void match_init OF((void)); /* asm code initialization */
89− uInt longest_match OF((deflate_state *s, IPos cur_match));
90−#else
91−local uInt longest_match OF((deflate_state *s, IPos cur_match));
92−#endif
93−#endif
94−local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
95−
96−#ifdef DEBUG
97−local void check_match OF((deflate_state *s, IPos start, IPos match,
98− int length));
99−#endif
100−
101−/* ===========================================================================
102− * Local data
103− */
104−
105−#define NIL 0
106−/* Tail of hash chains */
107−
108−#ifndef TOO_FAR
109−# define TOO_FAR 4096
110−#endif
111−/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
112−
113−#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
114−/* Minimum amount of lookahead, except at the end of the input file.
115− * See deflate.c for comments about the MIN_MATCH+1.
116− */
117−
118−/* Values for max_lazy_match, good_match and max_chain_length, depending on
119− * the desired pack level (0..9). The values given below have been tuned to
120− * exclude worst case performance for pathological files. Better values may be
121− * found for specific files.
122− */
123−typedef struct config_s {
124− ush good_length; /* reduce lazy search above this match length */
125− ush max_lazy; /* do not perform lazy search above this match length */
126− ush nice_length; /* quit search above this match length */
127− ush max_chain;
128− compress_func func;
129−} config;
130−
131−#ifdef FASTEST
132−local const config configuration_table[2] = {
133−/* good lazy nice chain */
134−/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
135−/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
136−#else
137−local const config configuration_table[10] = {
138−/* good lazy nice chain */
139−/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
140−/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
141−/* 2 */ {4, 5, 16, 8, deflate_fast},
142−/* 3 */ {4, 6, 32, 32, deflate_fast},
143−
144−/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
145−/* 5 */ {8, 16, 32, 32, deflate_slow},
146−/* 6 */ {8, 16, 128, 128, deflate_slow},
147−/* 7 */ {8, 32, 128, 256, deflate_slow},
148−/* 8 */ {32, 128, 258, 1024, deflate_slow},
149−/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
150−#endif
151−
152−/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
153− * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
154− * meaning.
155− */
156−
157−#define EQUAL 0
158−/* result of memcmp for equal strings */
159−
160−#ifndef NO_DUMMY_DECL
161−struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
162−#endif
163−
164−/* ===========================================================================
165− * Update a hash value with the given input byte
166− * IN assertion: all calls to to UPDATE_HASH are made with consecutive
167− * input characters, so that a running hash key can be computed from the
168− * previous key instead of complete recalculation each time.
169− */
170−#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
171−
172−
173−/* ===========================================================================
174− * Insert string str in the dictionary and set match_head to the previous head
175− * of the hash chain (the most recent string with same hash key). Return
176− * the previous length of the hash chain.
177− * If this file is compiled with -DFASTEST, the compression level is forced
178− * to 1, and no hash chains are maintained.
179− * IN assertion: all calls to to INSERT_STRING are made with consecutive
180− * input characters and the first MIN_MATCH bytes of str are valid
181− * (except for the last MIN_MATCH-1 bytes of the input file).
182− */
183−#ifdef FASTEST
184−#define INSERT_STRING(s, str, match_head) \
185− (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
186− match_head = s->head[s->ins_h], \
187− s->head[s->ins_h] = (Pos)(str))
188−#else
189−#define INSERT_STRING(s, str, match_head) \
190− (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
191− match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
192− s->head[s->ins_h] = (Pos)(str))
193−#endif
194−
195−/* ===========================================================================
196− * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
197− * prev[] will be initialized on the fly.
198− */
199−#define CLEAR_HASH(s) \
200− s->head[s->hash_size-1] = NIL; \
201− zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
202−
203−/* ========================================================================= */
204−int ZEXPORT deflateInit_(strm, level, version, stream_size)
205− z_streamp strm;
206− int level;
207− const char *version;
208− int stream_size;
209−{
210− return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
211− Z_DEFAULT_STRATEGY, version, stream_size);
212− /* To do: ignore strm->next_in if we use it as window */
213−}
214−
215−/* ========================================================================= */
216−int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
217− version, stream_size)
218− z_streamp strm;
219− int level;
220− int method;
221− int windowBits;
222− int memLevel;
223− int strategy;
224− const char *version;
225− int stream_size;
226−{
227− deflate_state *s;
228− int wrap = 1;
229− static const char my_version[] = ZLIB_VERSION;
230−
231− ushf *overlay;
232− /* We overlay pending_buf and d_buf+l_buf. This works since the average
233− * output size for (length,distance) codes is <= 24 bits.
234− */
235−
236− if (version == Z_NULL || version[0] != my_version[0] ||
237− stream_size != sizeof(z_stream)) {
238− return Z_VERSION_ERROR;
239− }
240− if (strm == Z_NULL) return Z_STREAM_ERROR;
241−
242− strm->msg = Z_NULL;
243− if (strm->zalloc == (alloc_func)0) {
244− strm->zalloc = zcalloc;
245− strm->opaque = (voidpf)0;
246− }
247− if (strm->zfree == (free_func)0) strm->zfree = zcfree;
248−
249−#ifdef FASTEST
250− if (level != 0) level = 1;
251−#else
252− if (level == Z_DEFAULT_COMPRESSION) level = 6;
253−#endif
254−
255− if (windowBits < 0) { /* suppress zlib wrapper */
256− wrap = 0;
257− windowBits = -windowBits;
258− }
259−#ifdef GZIP
260− else if (windowBits > 15) {
261− wrap = 2; /* write gzip wrapper instead */
262− windowBits -= 16;
263− }
264−#endif
265− if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
266− windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
267− strategy < 0 || strategy > Z_FIXED) {
268− return Z_STREAM_ERROR;
269− }
270− if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
271− s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
272− if (s == Z_NULL) return Z_MEM_ERROR;
273− strm->state = (struct internal_state FAR *)s;
274− s->strm = strm;
275−
276− s->wrap = wrap;
277− s->gzhead = Z_NULL;
278− s->w_bits = windowBits;
279− s->w_size = 1 << s->w_bits;
280− s->w_mask = s->w_size - 1;
281−
282− s->hash_bits = memLevel + 7;
283− s->hash_size = 1 << s->hash_bits;
284− s->hash_mask = s->hash_size - 1;
285− s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
286−
287− s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
288− s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
289− s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
290−
291− s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
292−
293− overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
294− s->pending_buf = (uchf *) overlay;
295− s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
296−
297− if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
298− s->pending_buf == Z_NULL) {
299− s->status = FINISH_STATE;
300− strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
301− deflateEnd (strm);
302− return Z_MEM_ERROR;
303− }
304− s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
305− s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
306−
307− s->level = level;
308− s->strategy = strategy;
309− s->method = (Byte)method;
310−
311− return deflateReset(strm);
312−}
313−
314−/* ========================================================================= */
315−int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
316− z_streamp strm;
317− const Bytef *dictionary;
318− uInt dictLength;
319−{
320− deflate_state *s;
321− uInt length = dictLength;
322− uInt n;
323− IPos hash_head = 0;
324−
325− if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
326− strm->state->wrap == 2 ||
327− (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
328− return Z_STREAM_ERROR;
329−
330− s = strm->state;
331− if (s->wrap)
332− strm->adler = adler32(strm->adler, dictionary, dictLength);
333−
334− if (length < MIN_MATCH) return Z_OK;
335− if (length > MAX_DIST(s)) {
336− length = MAX_DIST(s);
337− dictionary += dictLength - length; /* use the tail of the dictionary */
338− }
339− zmemcpy(s->window, dictionary, length);
340− s->strstart = length;
341− s->block_start = (long)length;
342−
343− /* Insert all strings in the hash table (except for the last two bytes).
344− * s->lookahead stays null, so s->ins_h will be recomputed at the next
345− * call of fill_window.
346− */
347− s->ins_h = s->window[0];
348− UPDATE_HASH(s, s->ins_h, s->window[1]);
349− for (n = 0; n <= length - MIN_MATCH; n++) {
350− INSERT_STRING(s, n, hash_head);
351− }
352− if (hash_head) hash_head = 0; /* to make compiler happy */
353− return Z_OK;
354−}
355−
356−/* ========================================================================= */
357−int ZEXPORT deflateReset (strm)
358− z_streamp strm;
359−{
360− deflate_state *s;
361−
362− if (strm == Z_NULL || strm->state == Z_NULL ||
363− strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
364− return Z_STREAM_ERROR;
365− }
366−
367− strm->total_in = strm->total_out = 0;
368− strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
369− strm->data_type = Z_UNKNOWN;
370−
371− s = (deflate_state *)strm->state;
372− s->pending = 0;
373− s->pending_out = s->pending_buf;
374−
375− if (s->wrap < 0) {
376− s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
377− }
378− s->status = s->wrap ? INIT_STATE : BUSY_STATE;
379− strm->adler =
380−#ifdef GZIP
381− s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
382−#endif
383− adler32(0L, Z_NULL, 0);
384− s->last_flush = Z_NO_FLUSH;
385−
386− _tr_init(s);
387− lm_init(s);
388−
389− return Z_OK;
390−}
391−
392−/* ========================================================================= */
393−int ZEXPORT deflateSetHeader (strm, head)
394− z_streamp strm;
395− gz_headerp head;
396−{
397− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
398− if (strm->state->wrap != 2) return Z_STREAM_ERROR;
399− strm->state->gzhead = head;
400− return Z_OK;
401−}
402−
403−/* ========================================================================= */
404−int ZEXPORT deflatePrime (strm, bits, value)
405− z_streamp strm;
406− int bits;
407− int value;
408−{
409− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
410− strm->state->bi_valid = bits;
411− strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
412− return Z_OK;
413−}
414−
415−/* ========================================================================= */
416−int ZEXPORT deflateParams(strm, level, strategy)
417− z_streamp strm;
418− int level;
419− int strategy;
420−{
421− deflate_state *s;
422− compress_func func;
423− int err = Z_OK;
424−
425− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
426− s = strm->state;
427−
428−#ifdef FASTEST
429− if (level != 0) level = 1;
430−#else
431− if (level == Z_DEFAULT_COMPRESSION) level = 6;
432−#endif
433− if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
434− return Z_STREAM_ERROR;
435− }
436− func = configuration_table[s->level].func;
437−
438− if (func != configuration_table[level].func && strm->total_in != 0) {
439− /* Flush the last buffer: */
440− err = deflate(strm, Z_PARTIAL_FLUSH);
441− }
442− if (s->level != level) {
443− s->level = level;
444− s->max_lazy_match = configuration_table[level].max_lazy;
445− s->good_match = configuration_table[level].good_length;
446− s->nice_match = configuration_table[level].nice_length;
447− s->max_chain_length = configuration_table[level].max_chain;
448− }
449− s->strategy = strategy;
450− return err;
451−}
452−
453−/* ========================================================================= */
454−int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
455− z_streamp strm;
456− int good_length;
457− int max_lazy;
458− int nice_length;
459− int max_chain;
460−{
461− deflate_state *s;
462−
463− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
464− s = strm->state;
465− s->good_match = good_length;
466− s->max_lazy_match = max_lazy;
467− s->nice_match = nice_length;
468− s->max_chain_length = max_chain;
469− return Z_OK;
470−}
471−
472−/* =========================================================================
473− * For the default windowBits of 15 and memLevel of 8, this function returns
474− * a close to exact, as well as small, upper bound on the compressed size.
475− * They are coded as constants here for a reason--if the #define's are
476− * changed, then this function needs to be changed as well. The return
477− * value for 15 and 8 only works for those exact settings.
478− *
479− * For any setting other than those defaults for windowBits and memLevel,
480− * the value returned is a conservative worst case for the maximum expansion
481− * resulting from using fixed blocks instead of stored blocks, which deflate
482− * can emit on compressed data for some combinations of the parameters.
483− *
484− * This function could be more sophisticated to provide closer upper bounds
485− * for every combination of windowBits and memLevel, as well as wrap.
486− * But even the conservative upper bound of about 14% expansion does not
487− * seem onerous for output buffer allocation.
488− */
489−uLong ZEXPORT deflateBound(strm, sourceLen)
490− z_streamp strm;
491− uLong sourceLen;
492−{
493− deflate_state *s;
494− uLong destLen;
495−
496− /* conservative upper bound */
497− destLen = sourceLen +
498− ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
499−
500− /* if can't get parameters, return conservative bound */
501− if (strm == Z_NULL || strm->state == Z_NULL)
502− return destLen;
503−
504− /* if not default parameters, return conservative bound */
505− s = strm->state;
506− if (s->w_bits != 15 || s->hash_bits != 8 + 7)
507− return destLen;
508−
509− /* default settings: return tight bound for that case */
510− return compressBound(sourceLen);
511−}
512−
513−/* =========================================================================
514− * Put a short in the pending buffer. The 16-bit value is put in MSB order.
515− * IN assertion: the stream state is correct and there is enough room in
516− * pending_buf.
517− */
518−local void putShortMSB (s, b)
519− deflate_state *s;
520− uInt b;
521−{
522− put_byte(s, (Byte)(b >> 8));
523− put_byte(s, (Byte)(b & 0xff));
524−}
525−
526−/* =========================================================================
527− * Flush as much pending output as possible. All deflate() output goes
528− * through this function so some applications may wish to modify it
529− * to avoid allocating a large strm->next_out buffer and copying into it.
530− * (See also read_buf()).
531− */
532−local void flush_pending(strm)
533− z_streamp strm;
534−{
535− unsigned len = strm->state->pending;
536−
537− if (len > strm->avail_out) len = strm->avail_out;
538− if (len == 0) return;
539−
540− zmemcpy(strm->next_out, strm->state->pending_out, len);
541− strm->next_out += len;
542− strm->state->pending_out += len;
543− strm->total_out += len;
544− strm->avail_out -= len;
545− strm->state->pending -= len;
546− if (strm->state->pending == 0) {
547− strm->state->pending_out = strm->state->pending_buf;
548− }
549−}
550−
551−/* ========================================================================= */
552−int ZEXPORT deflate (strm, flush)
553− z_streamp strm;
554− int flush;
555−{
556− int old_flush; /* value of flush param for previous deflate call */
557− deflate_state *s;
558−
559− if (strm == Z_NULL || strm->state == Z_NULL ||
560− flush > Z_FINISH || flush < 0) {
561− return Z_STREAM_ERROR;
562− }
563− s = strm->state;
564−
565− if (strm->next_out == Z_NULL ||
566− (strm->next_in == Z_NULL && strm->avail_in != 0) ||
567− (s->status == FINISH_STATE && flush != Z_FINISH)) {
568− ERR_RETURN(strm, Z_STREAM_ERROR);
569− }
570− if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
571−
572− s->strm = strm; /* just in case */
573− old_flush = s->last_flush;
574− s->last_flush = flush;
575−
576− /* Write the header */
577− if (s->status == INIT_STATE) {
578−#ifdef GZIP
579− if (s->wrap == 2) {
580− strm->adler = crc32(0L, Z_NULL, 0);
581− put_byte(s, 31);
582− put_byte(s, 139);
583− put_byte(s, 8);
584− if (s->gzhead == NULL) {
585− put_byte(s, 0);
586− put_byte(s, 0);
587− put_byte(s, 0);
588− put_byte(s, 0);
589− put_byte(s, 0);
590− put_byte(s, s->level == 9 ? 2 :
591− (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
592− 4 : 0));
593− put_byte(s, OS_CODE);
594− s->status = BUSY_STATE;
595− }
596− else {
597− put_byte(s, (s->gzhead->text ? 1 : 0) +
598− (s->gzhead->hcrc ? 2 : 0) +
599− (s->gzhead->extra == Z_NULL ? 0 : 4) +
600− (s->gzhead->name == Z_NULL ? 0 : 8) +
601− (s->gzhead->comment == Z_NULL ? 0 : 16)
602− );
603− put_byte(s, (Byte)(s->gzhead->time & 0xff));
604− put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
605− put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
606− put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
607− put_byte(s, s->level == 9 ? 2 :
608− (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
609− 4 : 0));
610− put_byte(s, s->gzhead->os & 0xff);
611− if (s->gzhead->extra != NULL) {
612− put_byte(s, s->gzhead->extra_len & 0xff);
613− put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
614− }
615− if (s->gzhead->hcrc)
616− strm->adler = crc32(strm->adler, s->pending_buf,
617− s->pending);
618− s->gzindex = 0;
619− s->status = EXTRA_STATE;
620− }
621− }
622− else
623−#endif
624− {
625− uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
626− uInt level_flags;
627−
628− if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
629− level_flags = 0;
630− else if (s->level < 6)
631− level_flags = 1;
632− else if (s->level == 6)
633− level_flags = 2;
634− else
635− level_flags = 3;
636− header |= (level_flags << 6);
637− if (s->strstart != 0) header |= PRESET_DICT;
638− header += 31 - (header % 31);
639−
640− s->status = BUSY_STATE;
641− putShortMSB(s, header);
642−
643− /* Save the adler32 of the preset dictionary: */
644− if (s->strstart != 0) {
645− putShortMSB(s, (uInt)(strm->adler >> 16));
646− putShortMSB(s, (uInt)(strm->adler & 0xffff));
647− }
648− strm->adler = adler32(0L, Z_NULL, 0);
649− }
650− }
651−#ifdef GZIP
652− if (s->status == EXTRA_STATE) {
653− if (s->gzhead->extra != NULL) {
654− uInt beg = s->pending; /* start of bytes to update crc */
655−
656− while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
657− if (s->pending == s->pending_buf_size) {
658− if (s->gzhead->hcrc && s->pending > beg)
659− strm->adler = crc32(strm->adler, s->pending_buf + beg,
660− s->pending - beg);
661− flush_pending(strm);
662− beg = s->pending;
663− if (s->pending == s->pending_buf_size)
664− break;
665− }
666− put_byte(s, s->gzhead->extra[s->gzindex]);
667− s->gzindex++;
668− }
669− if (s->gzhead->hcrc && s->pending > beg)
670− strm->adler = crc32(strm->adler, s->pending_buf + beg,
671− s->pending - beg);
672− if (s->gzindex == s->gzhead->extra_len) {
673− s->gzindex = 0;
674− s->status = NAME_STATE;
675− }
676− }
677− else
678− s->status = NAME_STATE;
679− }
680− if (s->status == NAME_STATE) {
681− if (s->gzhead->name != NULL) {
682− uInt beg = s->pending; /* start of bytes to update crc */
683− int val;
684−
685− do {
686− if (s->pending == s->pending_buf_size) {
687− if (s->gzhead->hcrc && s->pending > beg)
688− strm->adler = crc32(strm->adler, s->pending_buf + beg,
689− s->pending - beg);
690− flush_pending(strm);
691− beg = s->pending;
692− if (s->pending == s->pending_buf_size) {
693− val = 1;
694− break;
695− }
696− }
697− val = s->gzhead->name[s->gzindex++];
698− put_byte(s, val);
699− } while (val != 0);
700− if (s->gzhead->hcrc && s->pending > beg)
701− strm->adler = crc32(strm->adler, s->pending_buf + beg,
702− s->pending - beg);
703− if (val == 0) {
704− s->gzindex = 0;
705− s->status = COMMENT_STATE;
706− }
707− }
708− else
709− s->status = COMMENT_STATE;
710− }
711− if (s->status == COMMENT_STATE) {
712− if (s->gzhead->comment != NULL) {
713− uInt beg = s->pending; /* start of bytes to update crc */
714− int val;
715−
716− do {
717− if (s->pending == s->pending_buf_size) {
718− if (s->gzhead->hcrc && s->pending > beg)
719− strm->adler = crc32(strm->adler, s->pending_buf + beg,
720− s->pending - beg);
721− flush_pending(strm);
722− beg = s->pending;
723− if (s->pending == s->pending_buf_size) {
724− val = 1;
725− break;
726− }
727− }
728− val = s->gzhead->comment[s->gzindex++];
729− put_byte(s, val);
730− } while (val != 0);
731− if (s->gzhead->hcrc && s->pending > beg)
732− strm->adler = crc32(strm->adler, s->pending_buf + beg,
733− s->pending - beg);
734− if (val == 0)
735− s->status = HCRC_STATE;
736− }
737− else
738− s->status = HCRC_STATE;
739− }
740− if (s->status == HCRC_STATE) {
741− if (s->gzhead->hcrc) {
742− if (s->pending + 2 > s->pending_buf_size)
743− flush_pending(strm);
744− if (s->pending + 2 <= s->pending_buf_size) {
745− put_byte(s, (Byte)(strm->adler & 0xff));
746− put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
747− strm->adler = crc32(0L, Z_NULL, 0);
748− s->status = BUSY_STATE;
749− }
750− }
751− else
752− s->status = BUSY_STATE;
753− }
754−#endif
755−
756− /* Flush as much pending output as possible */
757− if (s->pending != 0) {
758− flush_pending(strm);
759− if (strm->avail_out == 0) {
760− /* Since avail_out is 0, deflate will be called again with
761− * more output space, but possibly with both pending and
762− * avail_in equal to zero. There won't be anything to do,
763− * but this is not an error situation so make sure we
764− * return OK instead of BUF_ERROR at next call of deflate:
765− */
766− s->last_flush = -1;
767− return Z_OK;
768− }
769−
770− /* Make sure there is something to do and avoid duplicate consecutive
771− * flushes. For repeated and useless calls with Z_FINISH, we keep
772− * returning Z_STREAM_END instead of Z_BUF_ERROR.
773− */
774− } else if (strm->avail_in == 0 && flush <= old_flush &&
775− flush != Z_FINISH) {
776− ERR_RETURN(strm, Z_BUF_ERROR);
777− }
778−
779− /* User must not provide more input after the first FINISH: */
780− if (s->status == FINISH_STATE && strm->avail_in != 0) {
781− ERR_RETURN(strm, Z_BUF_ERROR);
782− }
783−
784− /* Start a new block or continue the current one.
785− */
786− if (strm->avail_in != 0 || s->lookahead != 0 ||
787− (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
788− block_state bstate;
789−
790− bstate = (*(configuration_table[s->level].func))(s, flush);
791−
792− if (bstate == finish_started || bstate == finish_done) {
793− s->status = FINISH_STATE;
794− }
795− if (bstate == need_more || bstate == finish_started) {
796− if (strm->avail_out == 0) {
797− s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
798− }
799− return Z_OK;
800− /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
801− * of deflate should use the same flush parameter to make sure
802− * that the flush is complete. So we don't have to output an
803− * empty block here, this will be done at next call. This also
804− * ensures that for a very small output buffer, we emit at most
805− * one empty block.
806− */
807− }
808− if (bstate == block_done) {
809− if (flush == Z_PARTIAL_FLUSH) {
810− _tr_align(s);
811− } else { /* FULL_FLUSH or SYNC_FLUSH */
812− _tr_stored_block(s, (char*)0, 0L, 0);
813− /* For a full flush, this empty block will be recognized
814− * as a special marker by inflate_sync().
815− */
816− if (flush == Z_FULL_FLUSH) {
817− CLEAR_HASH(s); /* forget history */
818− }
819− }
820− flush_pending(strm);
821− if (strm->avail_out == 0) {
822− s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
823− return Z_OK;
824− }
825− }
826− }
827− Assert(strm->avail_out > 0, "bug2");
828−
829− if (flush != Z_FINISH) return Z_OK;
830− if (s->wrap <= 0) return Z_STREAM_END;
831−
832− /* Write the trailer */
833−#ifdef GZIP
834− if (s->wrap == 2) {
835− put_byte(s, (Byte)(strm->adler & 0xff));
836− put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
837− put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
838− put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
839− put_byte(s, (Byte)(strm->total_in & 0xff));
840− put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
841− put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
842− put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
843− }
844− else
845−#endif
846− {
847− putShortMSB(s, (uInt)(strm->adler >> 16));
848− putShortMSB(s, (uInt)(strm->adler & 0xffff));
849− }
850− flush_pending(strm);
851− /* If avail_out is zero, the application will call deflate again
852− * to flush the rest.
853− */
854− if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
855− return s->pending != 0 ? Z_OK : Z_STREAM_END;
856−}
857−
858−/* ========================================================================= */
859−int ZEXPORT deflateEnd (strm)
860− z_streamp strm;
861−{
862− int status;
863−
864− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
865−
866− status = strm->state->status;
867− if (status != INIT_STATE &&
868− status != EXTRA_STATE &&
869− status != NAME_STATE &&
870− status != COMMENT_STATE &&
871− status != HCRC_STATE &&
872− status != BUSY_STATE &&
873− status != FINISH_STATE) {
874− return Z_STREAM_ERROR;
875− }
876−
877− /* Deallocate in reverse order of allocations: */
878− TRY_FREE(strm, strm->state->pending_buf);
879− TRY_FREE(strm, strm->state->head);
880− TRY_FREE(strm, strm->state->prev);
881− TRY_FREE(strm, strm->state->window);
882−
883− ZFREE(strm, strm->state);
884− strm->state = Z_NULL;
885−
886− return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
887−}
888−
889−/* =========================================================================
890− * Copy the source state to the destination state.
891− * To simplify the source, this is not supported for 16-bit MSDOS (which
892− * doesn't have enough memory anyway to duplicate compression states).
893− */
894−int ZEXPORT deflateCopy (dest, source)
895− z_streamp dest;
896− z_streamp source;
897−{
898−#ifdef MAXSEG_64K
899− return Z_STREAM_ERROR;
900−#else
901− deflate_state *ds;
902− deflate_state *ss;
903− ushf *overlay;
904−
905−
906− if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
907− return Z_STREAM_ERROR;
908− }
909−
910− ss = source->state;
911−
912− zmemcpy(dest, source, sizeof(z_stream));
913−
914− ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
915− if (ds == Z_NULL) return Z_MEM_ERROR;
916− dest->state = (struct internal_state FAR *) ds;
917− zmemcpy(ds, ss, sizeof(deflate_state));
918− ds->strm = dest;
919−
920− ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
921− ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
922− ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
923− overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
924− ds->pending_buf = (uchf *) overlay;
925−
926− if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
927− ds->pending_buf == Z_NULL) {
928− deflateEnd (dest);
929− return Z_MEM_ERROR;
930− }
931− /* following zmemcpy do not work for 16-bit MSDOS */
932− zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
933− zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
934− zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
935− zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
936−
937− ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
938− ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
939− ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
940−
941− ds->l_desc.dyn_tree = ds->dyn_ltree;
942− ds->d_desc.dyn_tree = ds->dyn_dtree;
943− ds->bl_desc.dyn_tree = ds->bl_tree;
944−
945− return Z_OK;
946−#endif /* MAXSEG_64K */
947−}
948−
949−/* ===========================================================================
950− * Read a new buffer from the current input stream, update the adler32
951− * and total number of bytes read. All deflate() input goes through
952− * this function so some applications may wish to modify it to avoid
953− * allocating a large strm->next_in buffer and copying from it.
954− * (See also flush_pending()).
955− */
956−local int read_buf(strm, buf, size)
957− z_streamp strm;
958− Bytef *buf;
959− unsigned size;
960−{
961− unsigned len = strm->avail_in;
962−
963− if (len > size) len = size;
964− if (len == 0) return 0;
965−
966− strm->avail_in -= len;
967−
968− if (strm->state->wrap == 1) {
969− strm->adler = adler32(strm->adler, strm->next_in, len);
970− }
971−#ifdef GZIP
972− else if (strm->state->wrap == 2) {
973− strm->adler = crc32(strm->adler, strm->next_in, len);
974− }
975−#endif
976− zmemcpy(buf, strm->next_in, len);
977− strm->next_in += len;
978− strm->total_in += len;
979−
980− return (int)len;
981−}
982−
983−/* ===========================================================================
984− * Initialize the "longest match" routines for a new zlib stream
985− */
986−local void lm_init (s)
987− deflate_state *s;
988−{
989− s->window_size = (ulg)2L*s->w_size;
990−
991− CLEAR_HASH(s);
992−
993− /* Set the default configuration parameters:
994− */
995− s->max_lazy_match = configuration_table[s->level].max_lazy;
996− s->good_match = configuration_table[s->level].good_length;
997− s->nice_match = configuration_table[s->level].nice_length;
998− s->max_chain_length = configuration_table[s->level].max_chain;
999−
1000− s->strstart = 0;
1001− s->block_start = 0L;
1002− s->lookahead = 0;
1003− s->match_length = s->prev_length = MIN_MATCH-1;
1004− s->match_available = 0;
1005− s->ins_h = 0;
1006−#ifndef FASTEST
1007−#ifdef ASMV
1008− match_init(); /* initialize the asm code */
1009−#endif
1010−#endif
1011−}
1012−
1013−#ifndef FASTEST
1014−/* ===========================================================================
1015− * Set match_start to the longest match starting at the given string and
1016− * return its length. Matches shorter or equal to prev_length are discarded,
1017− * in which case the result is equal to prev_length and match_start is
1018− * garbage.
1019− * IN assertions: cur_match is the head of the hash chain for the current
1020− * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1021− * OUT assertion: the match length is not greater than s->lookahead.
1022− */
1023−#ifndef ASMV
1024−/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1025− * match.S. The code will be functionally equivalent.
1026− */
1027−local uInt longest_match(s, cur_match)
1028− deflate_state *s;
1029− IPos cur_match; /* current match */
1030−{
1031− unsigned chain_length = s->max_chain_length;/* max hash chain length */
1032− register Bytef *scan = s->window + s->strstart; /* current string */
1033− register Bytef *match; /* matched string */
1034− register int len; /* length of current match */
1035− int best_len = s->prev_length; /* best match length so far */
1036− int nice_match = s->nice_match; /* stop if match long enough */
1037− IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1038− s->strstart - (IPos)MAX_DIST(s) : NIL;
1039− /* Stop when cur_match becomes <= limit. To simplify the code,
1040− * we prevent matches with the string of window index 0.
1041− */
1042− Posf *prev = s->prev;
1043− uInt wmask = s->w_mask;
1044−
1045−#ifdef UNALIGNED_OK
1046− /* Compare two bytes at a time. Note: this is not always beneficial.
1047− * Try with and without -DUNALIGNED_OK to check.
1048− */
1049− register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1050− register ush scan_start = *(ushf*)scan;
1051− register ush scan_end = *(ushf*)(scan+best_len-1);
1052−#else
1053− register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1054− register Byte scan_end1 = scan[best_len-1];
1055− register Byte scan_end = scan[best_len];
1056−#endif
1057−
1058− /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1059− * It is easy to get rid of this optimization if necessary.
1060− */
1061− Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1062−
1063− /* Do not waste too much time if we already have a good match: */
1064− if (s->prev_length >= s->good_match) {
1065− chain_length >>= 2;
1066− }
1067− /* Do not look for matches beyond the end of the input. This is necessary
1068− * to make deflate deterministic.
1069− */
1070− if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1071−
1072− Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1073−
1074− do {
1075− Assert(cur_match < s->strstart, "no future");
1076− match = s->window + cur_match;
1077−
1078− /* Skip to next match if the match length cannot increase
1079− * or if the match length is less than 2. Note that the checks below
1080− * for insufficient lookahead only occur occasionally for performance
1081− * reasons. Therefore uninitialized memory will be accessed, and
1082− * conditional jumps will be made that depend on those values.
1083− * However the length of the match is limited to the lookahead, so
1084− * the output of deflate is not affected by the uninitialized values.
1085− */
1086−#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1087− /* This code assumes sizeof(unsigned short) == 2. Do not use
1088− * UNALIGNED_OK if your compiler uses a different size.
1089− */
1090− if (*(ushf*)(match+best_len-1) != scan_end ||
1091− *(ushf*)match != scan_start) continue;
1092−
1093− /* It is not necessary to compare scan[2] and match[2] since they are
1094− * always equal when the other bytes match, given that the hash keys
1095− * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1096− * strstart+3, +5, ... up to strstart+257. We check for insufficient
1097− * lookahead only every 4th comparison; the 128th check will be made
1098− * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1099− * necessary to put more guard bytes at the end of the window, or
1100− * to check more often for insufficient lookahead.
1101− */
1102− Assert(scan[2] == match[2], "scan[2]?");
1103− scan++, match++;
1104− do {
1105− } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1106− *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1107− *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1108− *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1109− scan < strend);
1110− /* The funny "do {}" generates better code on most compilers */
1111−
1112− /* Here, scan <= window+strstart+257 */
1113− Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1114− if (*scan == *match) scan++;
1115−
1116− len = (MAX_MATCH - 1) - (int)(strend-scan);
1117− scan = strend - (MAX_MATCH-1);
1118−
1119−#else /* UNALIGNED_OK */
1120−
1121− if (match[best_len] != scan_end ||
1122− match[best_len-1] != scan_end1 ||
1123− *match != *scan ||
1124− *++match != scan[1]) continue;
1125−
1126− /* The check at best_len-1 can be removed because it will be made
1127− * again later. (This heuristic is not always a win.)
1128− * It is not necessary to compare scan[2] and match[2] since they
1129− * are always equal when the other bytes match, given that
1130− * the hash keys are equal and that HASH_BITS >= 8.
1131− */
1132− scan += 2, match++;
1133− Assert(*scan == *match, "match[2]?");
1134−
1135− /* We check for insufficient lookahead only every 8th comparison;
1136− * the 256th check will be made at strstart+258.
1137− */
1138− do {
1139− } while (*++scan == *++match && *++scan == *++match &&
1140− *++scan == *++match && *++scan == *++match &&
1141− *++scan == *++match && *++scan == *++match &&
1142− *++scan == *++match && *++scan == *++match &&
1143− scan < strend);
1144−
1145− Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1146−
1147− len = MAX_MATCH - (int)(strend - scan);
1148− scan = strend - MAX_MATCH;
1149−
1150−#endif /* UNALIGNED_OK */
1151−
1152− if (len > best_len) {
1153− s->match_start = cur_match;
1154− best_len = len;
1155− if (len >= nice_match) break;
1156−#ifdef UNALIGNED_OK
1157− scan_end = *(ushf*)(scan+best_len-1);
1158−#else
1159− scan_end1 = scan[best_len-1];
1160− scan_end = scan[best_len];
1161−#endif
1162− }
1163− } while ((cur_match = prev[cur_match & wmask]) > limit
1164− && --chain_length != 0);
1165−
1166− if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1167− return s->lookahead;
1168−}
1169−#endif /* ASMV */
1170−#endif /* FASTEST */
1171−
1172−/* ---------------------------------------------------------------------------
1173− * Optimized version for level == 1 or strategy == Z_RLE only
1174− */
1175−local uInt longest_match_fast(s, cur_match)
1176− deflate_state *s;
1177− IPos cur_match; /* current match */
1178−{
1179− register Bytef *scan = s->window + s->strstart; /* current string */
1180− register Bytef *match; /* matched string */
1181− register int len; /* length of current match */
1182− register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1183−
1184− /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1185− * It is easy to get rid of this optimization if necessary.
1186− */
1187− Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1188−
1189− Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1190−
1191− Assert(cur_match < s->strstart, "no future");
1192−
1193− match = s->window + cur_match;
1194−
1195− /* Return failure if the match length is less than 2:
1196− */
1197− if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1198−
1199− /* The check at best_len-1 can be removed because it will be made
1200− * again later. (This heuristic is not always a win.)
1201− * It is not necessary to compare scan[2] and match[2] since they
1202− * are always equal when the other bytes match, given that
1203− * the hash keys are equal and that HASH_BITS >= 8.
1204− */
1205− scan += 2, match += 2;
1206− Assert(*scan == *match, "match[2]?");
1207−
1208− /* We check for insufficient lookahead only every 8th comparison;
1209− * the 256th check will be made at strstart+258.
1210− */
1211− do {
1212− } while (*++scan == *++match && *++scan == *++match &&
1213− *++scan == *++match && *++scan == *++match &&
1214− *++scan == *++match && *++scan == *++match &&
1215− *++scan == *++match && *++scan == *++match &&
1216− scan < strend);
1217−
1218− Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1219−
1220− len = MAX_MATCH - (int)(strend - scan);
1221−
1222− if (len < MIN_MATCH) return MIN_MATCH - 1;
1223−
1224− s->match_start = cur_match;
1225− return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1226−}
1227−
1228−#ifdef DEBUG
1229−/* ===========================================================================
1230− * Check that the match at match_start is indeed a match.
1231− */
1232−local void check_match(s, start, match, length)
1233− deflate_state *s;
1234− IPos start, match;
1235− int length;
1236−{
1237− /* check that the match is indeed a match */
1238− if (zmemcmp(s->window + match,
1239− s->window + start, length) != EQUAL) {
1240− fprintf(stderr, " start %u, match %u, length %d\n",
1241− start, match, length);
1242− do {
1243− fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1244− } while (--length != 0);
1245− z_error("invalid match");
1246− }
1247− if (z_verbose > 1) {
1248− fprintf(stderr,"\\[%d,%d]", start-match, length);
1249− do { putc(s->window[start++], stderr); } while (--length != 0);
1250− }
1251−}
1252−#else
1253−# define check_match(s, start, match, length)
1254−#endif /* DEBUG */
1255−
1256−/* ===========================================================================
1257− * Fill the window when the lookahead becomes insufficient.
1258− * Updates strstart and lookahead.
1259− *
1260− * IN assertion: lookahead < MIN_LOOKAHEAD
1261− * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1262− * At least one byte has been read, or avail_in == 0; reads are
1263− * performed for at least two bytes (required for the zip translate_eol
1264− * option -- not supported here).
1265− */
1266−local void fill_window(s)
1267− deflate_state *s;
1268−{
1269− register unsigned n, m;
1270− register Posf *p;
1271− unsigned more; /* Amount of free space at the end of the window. */
1272− uInt wsize = s->w_size;
1273−
1274− do {
1275− more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1276−
1277− /* Deal with !@#$% 64K limit: */
1278− if (sizeof(int) <= 2) {
1279− if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1280− more = wsize;
1281−
1282− } else if (more == (unsigned)(-1)) {
1283− /* Very unlikely, but possible on 16 bit machine if
1284− * strstart == 0 && lookahead == 1 (input done a byte at time)
1285− */
1286− more--;
1287− }
1288− }
1289−
1290− /* If the window is almost full and there is insufficient lookahead,
1291− * move the upper half to the lower one to make room in the upper half.
1292− */
1293− if (s->strstart >= wsize+MAX_DIST(s)) {
1294−
1295− zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1296− s->match_start -= wsize;
1297− s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1298− s->block_start -= (long) wsize;
1299−
1300− /* Slide the hash table (could be avoided with 32 bit values
1301− at the expense of memory usage). We slide even when level == 0
1302− to keep the hash table consistent if we switch back to level > 0
1303− later. (Using level 0 permanently is not an optimal usage of
1304− zlib, so we don't care about this pathological case.)
1305− */
1306− /* %%% avoid this when Z_RLE */
1307− n = s->hash_size;
1308− p = &s->head[n];
1309− do {
1310− m = *--p;
1311− *p = (Pos)(m >= wsize ? m-wsize : NIL);
1312− } while (--n);
1313−
1314− n = wsize;
1315−#ifndef FASTEST
1316− p = &s->prev[n];
1317− do {
1318− m = *--p;
1319− *p = (Pos)(m >= wsize ? m-wsize : NIL);
1320− /* If n is not on any hash chain, prev[n] is garbage but
1321− * its value will never be used.
1322− */
1323− } while (--n);
1324−#endif
1325− more += wsize;
1326− }
1327− if (s->strm->avail_in == 0) return;
1328−
1329− /* If there was no sliding:
1330− * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1331− * more == window_size - lookahead - strstart
1332− * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1333− * => more >= window_size - 2*WSIZE + 2
1334− * In the BIG_MEM or MMAP case (not yet supported),
1335− * window_size == input_size + MIN_LOOKAHEAD &&
1336− * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1337− * Otherwise, window_size == 2*WSIZE so more >= 2.
1338− * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1339− */
1340− Assert(more >= 2, "more < 2");
1341−
1342− n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1343− s->lookahead += n;
1344−
1345− /* Initialize the hash value now that we have some input: */
1346− if (s->lookahead >= MIN_MATCH) {
1347− s->ins_h = s->window[s->strstart];
1348− UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1349−#if MIN_MATCH != 3
1350− Call UPDATE_HASH() MIN_MATCH-3 more times
1351−#endif
1352− }
1353− /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1354− * but this is not important since only literal bytes will be emitted.
1355− */
1356−
1357− } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1358−}
1359−
1360−/* ===========================================================================
1361− * Flush the current block, with given end-of-file flag.
1362− * IN assertion: strstart is set to the end of the current match.
1363− */
1364−#define FLUSH_BLOCK_ONLY(s, eof) { \
1365− _tr_flush_block(s, (s->block_start >= 0L ? \
1366− (charf *)&s->window[(unsigned)s->block_start] : \
1367− (charf *)Z_NULL), \
1368− (ulg)((long)s->strstart - s->block_start), \
1369− (eof)); \
1370− s->block_start = s->strstart; \
1371− flush_pending(s->strm); \
1372− Tracev((stderr,"[FLUSH]")); \
1373−}
1374−
1375−/* Same but force premature exit if necessary. */
1376−#define FLUSH_BLOCK(s, eof) { \
1377− FLUSH_BLOCK_ONLY(s, eof); \
1378− if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1379−}
1380−
1381−/* ===========================================================================
1382− * Copy without compression as much as possible from the input stream, return
1383− * the current block state.
1384− * This function does not insert new strings in the dictionary since
1385− * uncompressible data is probably not useful. This function is used
1386− * only for the level=0 compression option.
1387− * NOTE: this function should be optimized to avoid extra copying from
1388− * window to pending_buf.
1389− */
1390−local block_state deflate_stored(s, flush)
1391− deflate_state *s;
1392− int flush;
1393−{
1394− /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1395− * to pending_buf_size, and each stored block has a 5 byte header:
1396− */
1397− ulg max_block_size = 0xffff;
1398− ulg max_start;
1399−
1400− if (max_block_size > s->pending_buf_size - 5) {
1401− max_block_size = s->pending_buf_size - 5;
1402− }
1403−
1404− /* Copy as much as possible from input to output: */
1405− for (;;) {
1406− /* Fill the window as much as possible: */
1407− if (s->lookahead <= 1) {
1408−
1409− Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1410− s->block_start >= (long)s->w_size, "slide too late");
1411−
1412− fill_window(s);
1413− if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1414−
1415− if (s->lookahead == 0) break; /* flush the current block */
1416− }
1417− Assert(s->block_start >= 0L, "block gone");
1418−
1419− s->strstart += s->lookahead;
1420− s->lookahead = 0;
1421−
1422− /* Emit a stored block if pending_buf will be full: */
1423− max_start = s->block_start + max_block_size;
1424− if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1425− /* strstart == 0 is possible when wraparound on 16-bit machine */
1426− s->lookahead = (uInt)(s->strstart - max_start);
1427− s->strstart = (uInt)max_start;
1428− FLUSH_BLOCK(s, 0);
1429− }
1430− /* Flush if we may have to slide, otherwise block_start may become
1431− * negative and the data will be gone:
1432− */
1433− if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1434− FLUSH_BLOCK(s, 0);
1435− }
1436− }
1437− FLUSH_BLOCK(s, flush == Z_FINISH);
1438− return flush == Z_FINISH ? finish_done : block_done;
1439−}
1440−
1441−/* ===========================================================================
1442− * Compress as much as possible from the input stream, return the current
1443− * block state.
1444− * This function does not perform lazy evaluation of matches and inserts
1445− * new strings in the dictionary only for unmatched strings or for short
1446− * matches. It is used only for the fast compression options.
1447− */
1448−local block_state deflate_fast(s, flush)
1449− deflate_state *s;
1450− int flush;
1451−{
1452− IPos hash_head = NIL; /* head of the hash chain */
1453− int bflush; /* set if current block must be flushed */
1454−
1455− for (;;) {
1456− /* Make sure that we always have enough lookahead, except
1457− * at the end of the input file. We need MAX_MATCH bytes
1458− * for the next match, plus MIN_MATCH bytes to insert the
1459− * string following the next match.
1460− */
1461− if (s->lookahead < MIN_LOOKAHEAD) {
1462− fill_window(s);
1463− if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1464− return need_more;
1465− }
1466− if (s->lookahead == 0) break; /* flush the current block */
1467− }
1468−
1469− /* Insert the string window[strstart .. strstart+2] in the
1470− * dictionary, and set hash_head to the head of the hash chain:
1471− */
1472− if (s->lookahead >= MIN_MATCH) {
1473− INSERT_STRING(s, s->strstart, hash_head);
1474− }
1475−
1476− /* Find the longest match, discarding those <= prev_length.
1477− * At this point we have always match_length < MIN_MATCH
1478− */
1479− if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1480− /* To simplify the code, we prevent matches with the string
1481− * of window index 0 (in particular we have to avoid a match
1482− * of the string with itself at the start of the input file).
1483− */
1484−#ifdef FASTEST
1485− if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
1486− (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
1487− s->match_length = longest_match_fast (s, hash_head);
1488− }
1489−#else
1490− if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1491− s->match_length = longest_match (s, hash_head);
1492− } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1493− s->match_length = longest_match_fast (s, hash_head);
1494− }
1495−#endif
1496− /* longest_match() or longest_match_fast() sets match_start */
1497− }
1498− if (s->match_length >= MIN_MATCH) {
1499− check_match(s, s->strstart, s->match_start, s->match_length);
1500−
1501− _tr_tally_dist(s, s->strstart - s->match_start,
1502− s->match_length - MIN_MATCH, bflush);
1503−
1504− s->lookahead -= s->match_length;
1505−
1506− /* Insert new strings in the hash table only if the match length
1507− * is not too large. This saves time but degrades compression.
1508− */
1509−#ifndef FASTEST
1510− if (s->match_length <= s->max_insert_length &&
1511− s->lookahead >= MIN_MATCH) {
1512− s->match_length--; /* string at strstart already in table */
1513− do {
1514− s->strstart++;
1515− INSERT_STRING(s, s->strstart, hash_head);
1516− /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1517− * always MIN_MATCH bytes ahead.
1518− */
1519− } while (--s->match_length != 0);
1520− s->strstart++;
1521− } else
1522−#endif
1523− {
1524− s->strstart += s->match_length;
1525− s->match_length = 0;
1526− s->ins_h = s->window[s->strstart];
1527− UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1528−#if MIN_MATCH != 3
1529− Call UPDATE_HASH() MIN_MATCH-3 more times
1530−#endif
1531− /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1532− * matter since it will be recomputed at next deflate call.
1533− */
1534− }
1535− } else {
1536− /* No match, output a literal byte */
1537− Tracevv((stderr,"%c", s->window[s->strstart]));
1538− _tr_tally_lit (s, s->window[s->strstart], bflush);
1539− s->lookahead--;
1540− s->strstart++;
1541− }
1542− if (bflush) FLUSH_BLOCK(s, 0);
1543− }
1544− FLUSH_BLOCK(s, flush == Z_FINISH);
1545− return flush == Z_FINISH ? finish_done : block_done;
1546−}
1547−
1548−#ifndef FASTEST
1549−/* ===========================================================================
1550− * Same as above, but achieves better compression. We use a lazy
1551− * evaluation for matches: a match is finally adopted only if there is
1552− * no better match at the next window position.
1553− */
1554−local block_state deflate_slow(s, flush)
1555− deflate_state *s;
1556− int flush;
1557−{
1558− IPos hash_head = NIL; /* head of hash chain */
1559− int bflush; /* set if current block must be flushed */
1560−
1561− /* Process the input block. */
1562− for (;;) {
1563− /* Make sure that we always have enough lookahead, except
1564− * at the end of the input file. We need MAX_MATCH bytes
1565− * for the next match, plus MIN_MATCH bytes to insert the
1566− * string following the next match.
1567− */
1568− if (s->lookahead < MIN_LOOKAHEAD) {
1569− fill_window(s);
1570− if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1571− return need_more;
1572− }
1573− if (s->lookahead == 0) break; /* flush the current block */
1574− }
1575−
1576− /* Insert the string window[strstart .. strstart+2] in the
1577− * dictionary, and set hash_head to the head of the hash chain:
1578− */
1579− if (s->lookahead >= MIN_MATCH) {
1580− INSERT_STRING(s, s->strstart, hash_head);
1581− }
1582−
1583− /* Find the longest match, discarding those <= prev_length.
1584− */
1585− s->prev_length = s->match_length, s->prev_match = s->match_start;
1586− s->match_length = MIN_MATCH-1;
1587−
1588− if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1589− s->strstart - hash_head <= MAX_DIST(s)) {
1590− /* To simplify the code, we prevent matches with the string
1591− * of window index 0 (in particular we have to avoid a match
1592− * of the string with itself at the start of the input file).
1593− */
1594− if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1595− s->match_length = longest_match (s, hash_head);
1596− } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1597− s->match_length = longest_match_fast (s, hash_head);
1598− }
1599− /* longest_match() or longest_match_fast() sets match_start */
1600−
1601− if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1602−#if TOO_FAR <= 32767
1603− || (s->match_length == MIN_MATCH &&
1604− s->strstart - s->match_start > TOO_FAR)
1605−#endif
1606− )) {
1607−
1608− /* If prev_match is also MIN_MATCH, match_start is garbage
1609− * but we will ignore the current match anyway.
1610− */
1611− s->match_length = MIN_MATCH-1;
1612− }
1613− }
1614− /* If there was a match at the previous step and the current
1615− * match is not better, output the previous match:
1616− */
1617− if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1618− uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1619− /* Do not insert strings in hash table beyond this. */
1620−
1621− check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1622−
1623− _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1624− s->prev_length - MIN_MATCH, bflush);
1625−
1626− /* Insert in hash table all strings up to the end of the match.
1627− * strstart-1 and strstart are already inserted. If there is not
1628− * enough lookahead, the last two strings are not inserted in
1629− * the hash table.
1630− */
1631− s->lookahead -= s->prev_length-1;
1632− s->prev_length -= 2;
1633− do {
1634− if (++s->strstart <= max_insert) {
1635− INSERT_STRING(s, s->strstart, hash_head);
1636− }
1637− } while (--s->prev_length != 0);
1638− s->match_available = 0;
1639− s->match_length = MIN_MATCH-1;
1640− s->strstart++;
1641−
1642− if (bflush) FLUSH_BLOCK(s, 0);
1643−
1644− } else if (s->match_available) {
1645− /* If there was no match at the previous position, output a
1646− * single literal. If there was a match but the current match
1647− * is longer, truncate the previous match to a single literal.
1648− */
1649− Tracevv((stderr,"%c", s->window[s->strstart-1]));
1650− _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1651− if (bflush) {
1652− FLUSH_BLOCK_ONLY(s, 0);
1653− }
1654− s->strstart++;
1655− s->lookahead--;
1656− if (s->strm->avail_out == 0) return need_more;
1657− } else {
1658− /* There is no previous match to compare with, wait for
1659− * the next step to decide.
1660− */
1661− s->match_available = 1;
1662− s->strstart++;
1663− s->lookahead--;
1664− }
1665− }
1666− Assert (flush != Z_NO_FLUSH, "no flush?");
1667− if (s->match_available) {
1668− Tracevv((stderr,"%c", s->window[s->strstart-1]));
1669− _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1670− s->match_available = 0;
1671− }
1672− FLUSH_BLOCK(s, flush == Z_FINISH);
1673− return flush == Z_FINISH ? finish_done : block_done;
1674−}
1675−#endif /* FASTEST */
1676−
1677−#if 0
1678−/* ===========================================================================
1679− * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1680− * one. Do not maintain a hash table. (It will be regenerated if this run of
1681− * deflate switches away from Z_RLE.)
1682− */
1683−local block_state deflate_rle(s, flush)
1684− deflate_state *s;
1685− int flush;
1686−{
1687− int bflush; /* set if current block must be flushed */
1688− uInt run; /* length of run */
1689− uInt max; /* maximum length of run */
1690− uInt prev; /* byte at distance one to match */
1691− Bytef *scan; /* scan for end of run */
1692−
1693− for (;;) {
1694− /* Make sure that we always have enough lookahead, except
1695− * at the end of the input file. We need MAX_MATCH bytes
1696− * for the longest encodable run.
1697− */
1698− if (s->lookahead < MAX_MATCH) {
1699− fill_window(s);
1700− if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
1701− return need_more;
1702− }
1703− if (s->lookahead == 0) break; /* flush the current block */
1704− }
1705−
1706− /* See how many times the previous byte repeats */
1707− run = 0;
1708− if (s->strstart > 0) { /* if there is a previous byte, that is */
1709− max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
1710− scan = s->window + s->strstart - 1;
1711− prev = *scan++;
1712− do {
1713− if (*scan++ != prev)
1714− break;
1715− } while (++run < max);
1716− }
1717−
1718− /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1719− if (run >= MIN_MATCH) {
1720− check_match(s, s->strstart, s->strstart - 1, run);
1721− _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
1722− s->lookahead -= run;
1723− s->strstart += run;
1724− } else {
1725− /* No match, output a literal byte */
1726− Tracevv((stderr,"%c", s->window[s->strstart]));
1727− _tr_tally_lit (s, s->window[s->strstart], bflush);
1728− s->lookahead--;
1729− s->strstart++;
1730− }
1731− if (bflush) FLUSH_BLOCK(s, 0);
1732− }
1733− FLUSH_BLOCK(s, flush == Z_FINISH);
1734− return flush == Z_FINISH ? finish_done : block_done;
1735−}
1736−#endif
cbits/deflate.hdeleted−331
@@ -1,331 +0,0 @@
1−/* deflate.h -- internal compression state
2− * Copyright (C) 1995-2004 Jean-loup Gailly
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* @(#) $Id$ */
12−
13−#ifndef DEFLATE_H
14−#define DEFLATE_H
15−
16−#include "zutil.h"
17−
18−/* define NO_GZIP when compiling if you want to disable gzip header and
19− trailer creation by deflate(). NO_GZIP would be used to avoid linking in
20− the crc code when it is not needed. For shared libraries, gzip encoding
21− should be left enabled. */
22−#ifndef NO_GZIP
23−# define GZIP
24−#endif
25−
26−/* ===========================================================================
27− * Internal compression state.
28− */
29−
30−#define LENGTH_CODES 29
31−/* number of length codes, not counting the special END_BLOCK code */
32−
33−#define LITERALS 256
34−/* number of literal bytes 0..255 */
35−
36−#define L_CODES (LITERALS+1+LENGTH_CODES)
37−/* number of Literal or Length codes, including the END_BLOCK code */
38−
39−#define D_CODES 30
40−/* number of distance codes */
41−
42−#define BL_CODES 19
43−/* number of codes used to transfer the bit lengths */
44−
45−#define HEAP_SIZE (2*L_CODES+1)
46−/* maximum heap size */
47−
48−#define MAX_BITS 15
49−/* All codes must not exceed MAX_BITS bits */
50−
51−#define INIT_STATE 42
52−#define EXTRA_STATE 69
53−#define NAME_STATE 73
54−#define COMMENT_STATE 91
55−#define HCRC_STATE 103
56−#define BUSY_STATE 113
57−#define FINISH_STATE 666
58−/* Stream status */
59−
60−
61−/* Data structure describing a single value and its code string. */
62−typedef struct ct_data_s {
63− union {
64− ush freq; /* frequency count */
65− ush code; /* bit string */
66− } fc;
67− union {
68− ush dad; /* father node in Huffman tree */
69− ush len; /* length of bit string */
70− } dl;
71−} FAR ct_data;
72−
73−#define Freq fc.freq
74−#define Code fc.code
75−#define Dad dl.dad
76−#define Len dl.len
77−
78−typedef struct static_tree_desc_s static_tree_desc;
79−
80−typedef struct tree_desc_s {
81− ct_data *dyn_tree; /* the dynamic tree */
82− int max_code; /* largest code with non zero frequency */
83− static_tree_desc *stat_desc; /* the corresponding static tree */
84−} FAR tree_desc;
85−
86−typedef ush Pos;
87−typedef Pos FAR Posf;
88−typedef unsigned IPos;
89−
90−/* A Pos is an index in the character window. We use short instead of int to
91− * save space in the various tables. IPos is used only for parameter passing.
92− */
93−
94−typedef struct internal_state {
95− z_streamp strm; /* pointer back to this zlib stream */
96− int status; /* as the name implies */
97− Bytef *pending_buf; /* output still pending */
98− ulg pending_buf_size; /* size of pending_buf */
99− Bytef *pending_out; /* next pending byte to output to the stream */
100− uInt pending; /* nb of bytes in the pending buffer */
101− int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
102− gz_headerp gzhead; /* gzip header information to write */
103− uInt gzindex; /* where in extra, name, or comment */
104− Byte method; /* STORED (for zip only) or DEFLATED */
105− int last_flush; /* value of flush param for previous deflate call */
106−
107− /* used by deflate.c: */
108−
109− uInt w_size; /* LZ77 window size (32K by default) */
110− uInt w_bits; /* log2(w_size) (8..16) */
111− uInt w_mask; /* w_size - 1 */
112−
113− Bytef *window;
114− /* Sliding window. Input bytes are read into the second half of the window,
115− * and move to the first half later to keep a dictionary of at least wSize
116− * bytes. With this organization, matches are limited to a distance of
117− * wSize-MAX_MATCH bytes, but this ensures that IO is always
118− * performed with a length multiple of the block size. Also, it limits
119− * the window size to 64K, which is quite useful on MSDOS.
120− * To do: use the user input buffer as sliding window.
121− */
122−
123− ulg window_size;
124− /* Actual size of window: 2*wSize, except when the user input buffer
125− * is directly used as sliding window.
126− */
127−
128− Posf *prev;
129− /* Link to older string with same hash index. To limit the size of this
130− * array to 64K, this link is maintained only for the last 32K strings.
131− * An index in this array is thus a window index modulo 32K.
132− */
133−
134− Posf *head; /* Heads of the hash chains or NIL. */
135−
136− uInt ins_h; /* hash index of string to be inserted */
137− uInt hash_size; /* number of elements in hash table */
138− uInt hash_bits; /* log2(hash_size) */
139− uInt hash_mask; /* hash_size-1 */
140−
141− uInt hash_shift;
142− /* Number of bits by which ins_h must be shifted at each input
143− * step. It must be such that after MIN_MATCH steps, the oldest
144− * byte no longer takes part in the hash key, that is:
145− * hash_shift * MIN_MATCH >= hash_bits
146− */
147−
148− long block_start;
149− /* Window position at the beginning of the current output block. Gets
150− * negative when the window is moved backwards.
151− */
152−
153− uInt match_length; /* length of best match */
154− IPos prev_match; /* previous match */
155− int match_available; /* set if previous match exists */
156− uInt strstart; /* start of string to insert */
157− uInt match_start; /* start of matching string */
158− uInt lookahead; /* number of valid bytes ahead in window */
159−
160− uInt prev_length;
161− /* Length of the best match at previous step. Matches not greater than this
162− * are discarded. This is used in the lazy match evaluation.
163− */
164−
165− uInt max_chain_length;
166− /* To speed up deflation, hash chains are never searched beyond this
167− * length. A higher limit improves compression ratio but degrades the
168− * speed.
169− */
170−
171− uInt max_lazy_match;
172− /* Attempt to find a better match only when the current match is strictly
173− * smaller than this value. This mechanism is used only for compression
174− * levels >= 4.
175− */
176−# define max_insert_length max_lazy_match
177− /* Insert new strings in the hash table only if the match length is not
178− * greater than this length. This saves time but degrades compression.
179− * max_insert_length is used only for compression levels <= 3.
180− */
181−
182− int level; /* compression level (1..9) */
183− int strategy; /* favor or force Huffman coding*/
184−
185− uInt good_match;
186− /* Use a faster search when the previous match is longer than this */
187−
188− int nice_match; /* Stop searching when current match exceeds this */
189−
190− /* used by trees.c: */
191− /* Didn't use ct_data typedef below to supress compiler warning */
192− struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
193− struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
194− struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
195−
196− struct tree_desc_s l_desc; /* desc. for literal tree */
197− struct tree_desc_s d_desc; /* desc. for distance tree */
198− struct tree_desc_s bl_desc; /* desc. for bit length tree */
199−
200− ush bl_count[MAX_BITS+1];
201− /* number of codes at each bit length for an optimal tree */
202−
203− int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
204− int heap_len; /* number of elements in the heap */
205− int heap_max; /* element of largest frequency */
206− /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
207− * The same heap array is used to build all trees.
208− */
209−
210− uch depth[2*L_CODES+1];
211− /* Depth of each subtree used as tie breaker for trees of equal frequency
212− */
213−
214− uchf *l_buf; /* buffer for literals or lengths */
215−
216− uInt lit_bufsize;
217− /* Size of match buffer for literals/lengths. There are 4 reasons for
218− * limiting lit_bufsize to 64K:
219− * - frequencies can be kept in 16 bit counters
220− * - if compression is not successful for the first block, all input
221− * data is still in the window so we can still emit a stored block even
222− * when input comes from standard input. (This can also be done for
223− * all blocks if lit_bufsize is not greater than 32K.)
224− * - if compression is not successful for a file smaller than 64K, we can
225− * even emit a stored file instead of a stored block (saving 5 bytes).
226− * This is applicable only for zip (not gzip or zlib).
227− * - creating new Huffman trees less frequently may not provide fast
228− * adaptation to changes in the input data statistics. (Take for
229− * example a binary file with poorly compressible code followed by
230− * a highly compressible string table.) Smaller buffer sizes give
231− * fast adaptation but have of course the overhead of transmitting
232− * trees more frequently.
233− * - I can't count above 4
234− */
235−
236− uInt last_lit; /* running index in l_buf */
237−
238− ushf *d_buf;
239− /* Buffer for distances. To simplify the code, d_buf and l_buf have
240− * the same number of elements. To use different lengths, an extra flag
241− * array would be necessary.
242− */
243−
244− ulg opt_len; /* bit length of current block with optimal trees */
245− ulg static_len; /* bit length of current block with static trees */
246− uInt matches; /* number of string matches in current block */
247− int last_eob_len; /* bit length of EOB code for last block */
248−
249−#ifdef DEBUG
250− ulg compressed_len; /* total bit length of compressed file mod 2^32 */
251− ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
252−#endif
253−
254− ush bi_buf;
255− /* Output buffer. bits are inserted starting at the bottom (least
256− * significant bits).
257− */
258− int bi_valid;
259− /* Number of valid bits in bi_buf. All bits above the last valid bit
260− * are always zero.
261− */
262−
263−} FAR deflate_state;
264−
265−/* Output a byte on the stream.
266− * IN assertion: there is enough room in pending_buf.
267− */
268−#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
269−
270−
271−#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
272−/* Minimum amount of lookahead, except at the end of the input file.
273− * See deflate.c for comments about the MIN_MATCH+1.
274− */
275−
276−#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
277−/* In order to simplify the code, particularly on 16 bit machines, match
278− * distances are limited to MAX_DIST instead of WSIZE.
279− */
280−
281− /* in trees.c */
282−void _tr_init OF((deflate_state *s));
283−int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
284−void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
285− int eof));
286−void _tr_align OF((deflate_state *s));
287−void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
288− int eof));
289−
290−#define d_code(dist) \
291− ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
292−/* Mapping from a distance to a distance code. dist is the distance - 1 and
293− * must not have side effects. _dist_code[256] and _dist_code[257] are never
294− * used.
295− */
296−
297−#ifndef DEBUG
298−/* Inline versions of _tr_tally for speed: */
299−
300−#if defined(GEN_TREES_H) || !defined(STDC)
301− extern uch _length_code[];
302− extern uch _dist_code[];
303−#else
304− extern const uch _length_code[];
305− extern const uch _dist_code[];
306−#endif
307−
308−# define _tr_tally_lit(s, c, flush) \
309− { uch cc = (c); \
310− s->d_buf[s->last_lit] = 0; \
311− s->l_buf[s->last_lit++] = cc; \
312− s->dyn_ltree[cc].Freq++; \
313− flush = (s->last_lit == s->lit_bufsize-1); \
314− }
315−# define _tr_tally_dist(s, distance, length, flush) \
316− { uch len = (length); \
317− ush dist = (distance); \
318− s->d_buf[s->last_lit] = dist; \
319− s->l_buf[s->last_lit++] = len; \
320− dist--; \
321− s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
322− s->dyn_dtree[d_code(dist)].Freq++; \
323− flush = (s->last_lit == s->lit_bufsize-1); \
324− }
325−#else
326−# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
327−# define _tr_tally_dist(s, distance, length, flush) \
328− flush = _tr_tally(s, distance, length)
329−#endif
330−
331−#endif /* DEFLATE_H */
cbits/gzio.cdeleted−1026
@@ -1,1026 +0,0 @@
1−/* gzio.c -- IO on .gz files
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− *
5− * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
6− */
7−
8−/* @(#) $Id$ */
9−
10−#include <stdio.h>
11−
12−#include "zutil.h"
13−
14−#ifdef NO_DEFLATE /* for compatibility with old definition */
15−# define NO_GZCOMPRESS
16−#endif
17−
18−#ifndef NO_DUMMY_DECL
19−struct internal_state {int dummy;}; /* for buggy compilers */
20−#endif
21−
22−#ifndef Z_BUFSIZE
23−# ifdef MAXSEG_64K
24−# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
25−# else
26−# define Z_BUFSIZE 16384
27−# endif
28−#endif
29−#ifndef Z_PRINTF_BUFSIZE
30−# define Z_PRINTF_BUFSIZE 4096
31−#endif
32−
33−#ifdef __MVS__
34−# pragma map (fdopen , "\174\174FDOPEN")
35− FILE *fdopen(int, const char *);
36−#endif
37−
38−#ifndef STDC
39−extern voidp malloc OF((uInt size));
40−extern void free OF((voidpf ptr));
41−#endif
42−
43−#define ALLOC(size) malloc(size)
44−#define TRYFREE(p) {if (p) free(p);}
45−
46−static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
47−
48−/* gzip flag byte */
49−#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
50−#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
51−#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
52−#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
53−#define COMMENT 0x10 /* bit 4 set: file comment present */
54−#define RESERVED 0xE0 /* bits 5..7: reserved */
55−
56−typedef struct gz_stream {
57− z_stream stream;
58− int z_err; /* error code for last stream operation */
59− int z_eof; /* set if end of input file */
60− FILE *file; /* .gz file */
61− Byte *inbuf; /* input buffer */
62− Byte *outbuf; /* output buffer */
63− uLong crc; /* crc32 of uncompressed data */
64− char *msg; /* error message */
65− char *path; /* path name for debugging only */
66− int transparent; /* 1 if input file is not a .gz file */
67− char mode; /* 'w' or 'r' */
68− z_off_t start; /* start of compressed data in file (header skipped) */
69− z_off_t in; /* bytes into deflate or inflate */
70− z_off_t out; /* bytes out of deflate or inflate */
71− int back; /* one character push-back */
72− int last; /* true if push-back is last character */
73−} gz_stream;
74−
75−
76−local gzFile gz_open OF((const char *path, const char *mode, int fd));
77−local int do_flush OF((gzFile file, int flush));
78−local int get_byte OF((gz_stream *s));
79−local void check_header OF((gz_stream *s));
80−local int destroy OF((gz_stream *s));
81−local void putLong OF((FILE *file, uLong x));
82−local uLong getLong OF((gz_stream *s));
83−
84−/* ===========================================================================
85− Opens a gzip (.gz) file for reading or writing. The mode parameter
86− is as in fopen ("rb" or "wb"). The file is given either by file descriptor
87− or path name (if fd == -1).
88− gz_open returns NULL if the file could not be opened or if there was
89− insufficient memory to allocate the (de)compression state; errno
90− can be checked to distinguish the two cases (if errno is zero, the
91− zlib error is Z_MEM_ERROR).
92−*/
93−local gzFile gz_open (path, mode, fd)
94− const char *path;
95− const char *mode;
96− int fd;
97−{
98− int err;
99− int level = Z_DEFAULT_COMPRESSION; /* compression level */
100− int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
101− char *p = (char*)mode;
102− gz_stream *s;
103− char fmode[80]; /* copy of mode, without the compression level */
104− char *m = fmode;
105−
106− if (!path || !mode) return Z_NULL;
107−
108− s = (gz_stream *)ALLOC(sizeof(gz_stream));
109− if (!s) return Z_NULL;
110−
111− s->stream.zalloc = (alloc_func)0;
112− s->stream.zfree = (free_func)0;
113− s->stream.opaque = (voidpf)0;
114− s->stream.next_in = s->inbuf = Z_NULL;
115− s->stream.next_out = s->outbuf = Z_NULL;
116− s->stream.avail_in = s->stream.avail_out = 0;
117− s->file = NULL;
118− s->z_err = Z_OK;
119− s->z_eof = 0;
120− s->in = 0;
121− s->out = 0;
122− s->back = EOF;
123− s->crc = crc32(0L, Z_NULL, 0);
124− s->msg = NULL;
125− s->transparent = 0;
126−
127− s->path = (char*)ALLOC(strlen(path)+1);
128− if (s->path == NULL) {
129− return destroy(s), (gzFile)Z_NULL;
130− }
131− strcpy(s->path, path); /* do this early for debugging */
132−
133− s->mode = '\0';
134− do {
135− if (*p == 'r') s->mode = 'r';
136− if (*p == 'w' || *p == 'a') s->mode = 'w';
137− if (*p >= '0' && *p <= '9') {
138− level = *p - '0';
139− } else if (*p == 'f') {
140− strategy = Z_FILTERED;
141− } else if (*p == 'h') {
142− strategy = Z_HUFFMAN_ONLY;
143− } else if (*p == 'R') {
144− strategy = Z_RLE;
145− } else {
146− *m++ = *p; /* copy the mode */
147− }
148− } while (*p++ && m != fmode + sizeof(fmode));
149− if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
150−
151− if (s->mode == 'w') {
152−#ifdef NO_GZCOMPRESS
153− err = Z_STREAM_ERROR;
154−#else
155− err = deflateInit2(&(s->stream), level,
156− Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
157− /* windowBits is passed < 0 to suppress zlib header */
158−
159− s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
160−#endif
161− if (err != Z_OK || s->outbuf == Z_NULL) {
162− return destroy(s), (gzFile)Z_NULL;
163− }
164− } else {
165− s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
166−
167− err = inflateInit2(&(s->stream), -MAX_WBITS);
168− /* windowBits is passed < 0 to tell that there is no zlib header.
169− * Note that in this case inflate *requires* an extra "dummy" byte
170− * after the compressed stream in order to complete decompression and
171− * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
172− * present after the compressed stream.
173− */
174− if (err != Z_OK || s->inbuf == Z_NULL) {
175− return destroy(s), (gzFile)Z_NULL;
176− }
177− }
178− s->stream.avail_out = Z_BUFSIZE;
179−
180− errno = 0;
181− s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
182−
183− if (s->file == NULL) {
184− return destroy(s), (gzFile)Z_NULL;
185− }
186− if (s->mode == 'w') {
187− /* Write a very simple .gz header:
188− */
189− fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
190− Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
191− s->start = 10L;
192− /* We use 10L instead of ftell(s->file) to because ftell causes an
193− * fflush on some systems. This version of the library doesn't use
194− * start anyway in write mode, so this initialization is not
195− * necessary.
196− */
197− } else {
198− check_header(s); /* skip the .gz header */
199− s->start = ftell(s->file) - s->stream.avail_in;
200− }
201−
202− return (gzFile)s;
203−}
204−
205−/* ===========================================================================
206− Opens a gzip (.gz) file for reading or writing.
207−*/
208−gzFile ZEXPORT gzopen (path, mode)
209− const char *path;
210− const char *mode;
211−{
212− return gz_open (path, mode, -1);
213−}
214−
215−/* ===========================================================================
216− Associate a gzFile with the file descriptor fd. fd is not dup'ed here
217− to mimic the behavio(u)r of fdopen.
218−*/
219−gzFile ZEXPORT gzdopen (fd, mode)
220− int fd;
221− const char *mode;
222−{
223− char name[46]; /* allow for up to 128-bit integers */
224−
225− if (fd < 0) return (gzFile)Z_NULL;
226− sprintf(name, "<fd:%d>", fd); /* for debugging */
227−
228− return gz_open (name, mode, fd);
229−}
230−
231−/* ===========================================================================
232− * Update the compression level and strategy
233− */
234−int ZEXPORT gzsetparams (file, level, strategy)
235− gzFile file;
236− int level;
237− int strategy;
238−{
239− gz_stream *s = (gz_stream*)file;
240−
241− if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
242−
243− /* Make room to allow flushing */
244− if (s->stream.avail_out == 0) {
245−
246− s->stream.next_out = s->outbuf;
247− if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
248− s->z_err = Z_ERRNO;
249− }
250− s->stream.avail_out = Z_BUFSIZE;
251− }
252−
253− return deflateParams (&(s->stream), level, strategy);
254−}
255−
256−/* ===========================================================================
257− Read a byte from a gz_stream; update next_in and avail_in. Return EOF
258− for end of file.
259− IN assertion: the stream s has been sucessfully opened for reading.
260−*/
261−local int get_byte(s)
262− gz_stream *s;
263−{
264− if (s->z_eof) return EOF;
265− if (s->stream.avail_in == 0) {
266− errno = 0;
267− s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
268− if (s->stream.avail_in == 0) {
269− s->z_eof = 1;
270− if (ferror(s->file)) s->z_err = Z_ERRNO;
271− return EOF;
272− }
273− s->stream.next_in = s->inbuf;
274− }
275− s->stream.avail_in--;
276− return *(s->stream.next_in)++;
277−}
278−
279−/* ===========================================================================
280− Check the gzip header of a gz_stream opened for reading. Set the stream
281− mode to transparent if the gzip magic header is not present; set s->err
282− to Z_DATA_ERROR if the magic header is present but the rest of the header
283− is incorrect.
284− IN assertion: the stream s has already been created sucessfully;
285− s->stream.avail_in is zero for the first time, but may be non-zero
286− for concatenated .gz files.
287−*/
288−local void check_header(s)
289− gz_stream *s;
290−{
291− int method; /* method byte */
292− int flags; /* flags byte */
293− uInt len;
294− int c;
295−
296− /* Assure two bytes in the buffer so we can peek ahead -- handle case
297− where first byte of header is at the end of the buffer after the last
298− gzip segment */
299− len = s->stream.avail_in;
300− if (len < 2) {
301− if (len) s->inbuf[0] = s->stream.next_in[0];
302− errno = 0;
303− len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
304− if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
305− s->stream.avail_in += len;
306− s->stream.next_in = s->inbuf;
307− if (s->stream.avail_in < 2) {
308− s->transparent = s->stream.avail_in;
309− return;
310− }
311− }
312−
313− /* Peek ahead to check the gzip magic header */
314− if (s->stream.next_in[0] != gz_magic[0] ||
315− s->stream.next_in[1] != gz_magic[1]) {
316− s->transparent = 1;
317− return;
318− }
319− s->stream.avail_in -= 2;
320− s->stream.next_in += 2;
321−
322− /* Check the rest of the gzip header */
323− method = get_byte(s);
324− flags = get_byte(s);
325− if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
326− s->z_err = Z_DATA_ERROR;
327− return;
328− }
329−
330− /* Discard time, xflags and OS code: */
331− for (len = 0; len < 6; len++) (void)get_byte(s);
332−
333− if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
334− len = (uInt)get_byte(s);
335− len += ((uInt)get_byte(s))<<8;
336− /* len is garbage if EOF but the loop below will quit anyway */
337− while (len-- != 0 && get_byte(s) != EOF) ;
338− }
339− if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
340− while ((c = get_byte(s)) != 0 && c != EOF) ;
341− }
342− if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
343− while ((c = get_byte(s)) != 0 && c != EOF) ;
344− }
345− if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
346− for (len = 0; len < 2; len++) (void)get_byte(s);
347− }
348− s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
349−}
350−
351− /* ===========================================================================
352− * Cleanup then free the given gz_stream. Return a zlib error code.
353− Try freeing in the reverse order of allocations.
354− */
355−local int destroy (s)
356− gz_stream *s;
357−{
358− int err = Z_OK;
359−
360− if (!s) return Z_STREAM_ERROR;
361−
362− TRYFREE(s->msg);
363−
364− if (s->stream.state != NULL) {
365− if (s->mode == 'w') {
366−#ifdef NO_GZCOMPRESS
367− err = Z_STREAM_ERROR;
368−#else
369− err = deflateEnd(&(s->stream));
370−#endif
371− } else if (s->mode == 'r') {
372− err = inflateEnd(&(s->stream));
373− }
374− }
375− if (s->file != NULL && fclose(s->file)) {
376−#ifdef ESPIPE
377− if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
378−#endif
379− err = Z_ERRNO;
380− }
381− if (s->z_err < 0) err = s->z_err;
382−
383− TRYFREE(s->inbuf);
384− TRYFREE(s->outbuf);
385− TRYFREE(s->path);
386− TRYFREE(s);
387− return err;
388−}
389−
390−/* ===========================================================================
391− Reads the given number of uncompressed bytes from the compressed file.
392− gzread returns the number of bytes actually read (0 for end of file).
393−*/
394−int ZEXPORT gzread (file, buf, len)
395− gzFile file;
396− voidp buf;
397− unsigned len;
398−{
399− gz_stream *s = (gz_stream*)file;
400− Bytef *start = (Bytef*)buf; /* starting point for crc computation */
401− Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
402−
403− if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
404−
405− if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
406− if (s->z_err == Z_STREAM_END) return 0; /* EOF */
407−
408− next_out = (Byte*)buf;
409− s->stream.next_out = (Bytef*)buf;
410− s->stream.avail_out = len;
411−
412− if (s->stream.avail_out && s->back != EOF) {
413− *next_out++ = s->back;
414− s->stream.next_out++;
415− s->stream.avail_out--;
416− s->back = EOF;
417− s->out++;
418− start++;
419− if (s->last) {
420− s->z_err = Z_STREAM_END;
421− return 1;
422− }
423− }
424−
425− while (s->stream.avail_out != 0) {
426−
427− if (s->transparent) {
428− /* Copy first the lookahead bytes: */
429− uInt n = s->stream.avail_in;
430− if (n > s->stream.avail_out) n = s->stream.avail_out;
431− if (n > 0) {
432− zmemcpy(s->stream.next_out, s->stream.next_in, n);
433− next_out += n;
434− s->stream.next_out = next_out;
435− s->stream.next_in += n;
436− s->stream.avail_out -= n;
437− s->stream.avail_in -= n;
438− }
439− if (s->stream.avail_out > 0) {
440− s->stream.avail_out -=
441− (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
442− }
443− len -= s->stream.avail_out;
444− s->in += len;
445− s->out += len;
446− if (len == 0) s->z_eof = 1;
447− return (int)len;
448− }
449− if (s->stream.avail_in == 0 && !s->z_eof) {
450−
451− errno = 0;
452− s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
453− if (s->stream.avail_in == 0) {
454− s->z_eof = 1;
455− if (ferror(s->file)) {
456− s->z_err = Z_ERRNO;
457− break;
458− }
459− }
460− s->stream.next_in = s->inbuf;
461− }
462− s->in += s->stream.avail_in;
463− s->out += s->stream.avail_out;
464− s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
465− s->in -= s->stream.avail_in;
466− s->out -= s->stream.avail_out;
467−
468− if (s->z_err == Z_STREAM_END) {
469− /* Check CRC and original size */
470− s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
471− start = s->stream.next_out;
472−
473− if (getLong(s) != s->crc) {
474− s->z_err = Z_DATA_ERROR;
475− } else {
476− (void)getLong(s);
477− /* The uncompressed length returned by above getlong() may be
478− * different from s->out in case of concatenated .gz files.
479− * Check for such files:
480− */
481− check_header(s);
482− if (s->z_err == Z_OK) {
483− inflateReset(&(s->stream));
484− s->crc = crc32(0L, Z_NULL, 0);
485− }
486− }
487− }
488− if (s->z_err != Z_OK || s->z_eof) break;
489− }
490− s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
491−
492− if (len == s->stream.avail_out &&
493− (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
494− return -1;
495− return (int)(len - s->stream.avail_out);
496−}
497−
498−
499−/* ===========================================================================
500− Reads one byte from the compressed file. gzgetc returns this byte
501− or -1 in case of end of file or error.
502−*/
503−int ZEXPORT gzgetc(file)
504− gzFile file;
505−{
506− unsigned char c;
507−
508− return gzread(file, &c, 1) == 1 ? c : -1;
509−}
510−
511−
512−/* ===========================================================================
513− Push one byte back onto the stream.
514−*/
515−int ZEXPORT gzungetc(c, file)
516− int c;
517− gzFile file;
518−{
519− gz_stream *s = (gz_stream*)file;
520−
521− if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
522− s->back = c;
523− s->out--;
524− s->last = (s->z_err == Z_STREAM_END);
525− if (s->last) s->z_err = Z_OK;
526− s->z_eof = 0;
527− return c;
528−}
529−
530−
531−/* ===========================================================================
532− Reads bytes from the compressed file until len-1 characters are
533− read, or a newline character is read and transferred to buf, or an
534− end-of-file condition is encountered. The string is then terminated
535− with a null character.
536− gzgets returns buf, or Z_NULL in case of error.
537−
538− The current implementation is not optimized at all.
539−*/
540−char * ZEXPORT gzgets(file, buf, len)
541− gzFile file;
542− char *buf;
543− int len;
544−{
545− char *b = buf;
546− if (buf == Z_NULL || len <= 0) return Z_NULL;
547−
548− while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
549− *buf = '\0';
550− return b == buf && len > 0 ? Z_NULL : b;
551−}
552−
553−
554−#ifndef NO_GZCOMPRESS
555−/* ===========================================================================
556− Writes the given number of uncompressed bytes into the compressed file.
557− gzwrite returns the number of bytes actually written (0 in case of error).
558−*/
559−int ZEXPORT gzwrite (file, buf, len)
560− gzFile file;
561− voidpc buf;
562− unsigned len;
563−{
564− gz_stream *s = (gz_stream*)file;
565−
566− if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
567−
568− s->stream.next_in = (Bytef*)buf;
569− s->stream.avail_in = len;
570−
571− while (s->stream.avail_in != 0) {
572−
573− if (s->stream.avail_out == 0) {
574−
575− s->stream.next_out = s->outbuf;
576− if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
577− s->z_err = Z_ERRNO;
578− break;
579− }
580− s->stream.avail_out = Z_BUFSIZE;
581− }
582− s->in += s->stream.avail_in;
583− s->out += s->stream.avail_out;
584− s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
585− s->in -= s->stream.avail_in;
586− s->out -= s->stream.avail_out;
587− if (s->z_err != Z_OK) break;
588− }
589− s->crc = crc32(s->crc, (const Bytef *)buf, len);
590−
591− return (int)(len - s->stream.avail_in);
592−}
593−
594−
595−/* ===========================================================================
596− Converts, formats, and writes the args to the compressed file under
597− control of the format string, as in fprintf. gzprintf returns the number of
598− uncompressed bytes actually written (0 in case of error).
599−*/
600−#ifdef STDC
601−#include <stdarg.h>
602−
603−int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
604−{
605− char buf[Z_PRINTF_BUFSIZE];
606− va_list va;
607− int len;
608−
609− buf[sizeof(buf) - 1] = 0;
610− va_start(va, format);
611−#ifdef NO_vsnprintf
612−# ifdef HAS_vsprintf_void
613− (void)vsprintf(buf, format, va);
614− va_end(va);
615− for (len = 0; len < sizeof(buf); len++)
616− if (buf[len] == 0) break;
617−# else
618− len = vsprintf(buf, format, va);
619− va_end(va);
620−# endif
621−#else
622−# ifdef HAS_vsnprintf_void
623− (void)vsnprintf(buf, sizeof(buf), format, va);
624− va_end(va);
625− len = strlen(buf);
626−# else
627− len = vsnprintf(buf, sizeof(buf), format, va);
628− va_end(va);
629−# endif
630−#endif
631− if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
632− return 0;
633− return gzwrite(file, buf, (unsigned)len);
634−}
635−#else /* not ANSI C */
636−
637−int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
638− a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
639− gzFile file;
640− const char *format;
641− int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
642− a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
643−{
644− char buf[Z_PRINTF_BUFSIZE];
645− int len;
646−
647− buf[sizeof(buf) - 1] = 0;
648−#ifdef NO_snprintf
649−# ifdef HAS_sprintf_void
650− sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
651− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
652− for (len = 0; len < sizeof(buf); len++)
653− if (buf[len] == 0) break;
654−# else
655− len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
656− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
657−# endif
658−#else
659−# ifdef HAS_snprintf_void
660− snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
661− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
662− len = strlen(buf);
663−# else
664− len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
665− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
666−# endif
667−#endif
668− if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
669− return 0;
670− return gzwrite(file, buf, len);
671−}
672−#endif
673−
674−/* ===========================================================================
675− Writes c, converted to an unsigned char, into the compressed file.
676− gzputc returns the value that was written, or -1 in case of error.
677−*/
678−int ZEXPORT gzputc(file, c)
679− gzFile file;
680− int c;
681−{
682− unsigned char cc = (unsigned char) c; /* required for big endian systems */
683−
684− return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
685−}
686−
687−
688−/* ===========================================================================
689− Writes the given null-terminated string to the compressed file, excluding
690− the terminating null character.
691− gzputs returns the number of characters written, or -1 in case of error.
692−*/
693−int ZEXPORT gzputs(file, s)
694− gzFile file;
695− const char *s;
696−{
697− return gzwrite(file, (char*)s, (unsigned)strlen(s));
698−}
699−
700−
701−/* ===========================================================================
702− Flushes all pending output into the compressed file. The parameter
703− flush is as in the deflate() function.
704−*/
705−local int do_flush (file, flush)
706− gzFile file;
707− int flush;
708−{
709− uInt len;
710− int done = 0;
711− gz_stream *s = (gz_stream*)file;
712−
713− if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
714−
715− s->stream.avail_in = 0; /* should be zero already anyway */
716−
717− for (;;) {
718− len = Z_BUFSIZE - s->stream.avail_out;
719−
720− if (len != 0) {
721− if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
722− s->z_err = Z_ERRNO;
723− return Z_ERRNO;
724− }
725− s->stream.next_out = s->outbuf;
726− s->stream.avail_out = Z_BUFSIZE;
727− }
728− if (done) break;
729− s->out += s->stream.avail_out;
730− s->z_err = deflate(&(s->stream), flush);
731− s->out -= s->stream.avail_out;
732−
733− /* Ignore the second of two consecutive flushes: */
734− if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
735−
736− /* deflate has finished flushing only when it hasn't used up
737− * all the available space in the output buffer:
738− */
739− done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
740−
741− if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
742− }
743− return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
744−}
745−
746−int ZEXPORT gzflush (file, flush)
747− gzFile file;
748− int flush;
749−{
750− gz_stream *s = (gz_stream*)file;
751− int err = do_flush (file, flush);
752−
753− if (err) return err;
754− fflush(s->file);
755− return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
756−}
757−#endif /* NO_GZCOMPRESS */
758−
759−/* ===========================================================================
760− Sets the starting position for the next gzread or gzwrite on the given
761− compressed file. The offset represents a number of bytes in the
762− gzseek returns the resulting offset location as measured in bytes from
763− the beginning of the uncompressed stream, or -1 in case of error.
764− SEEK_END is not implemented, returns error.
765− In this version of the library, gzseek can be extremely slow.
766−*/
767−z_off_t ZEXPORT gzseek (file, offset, whence)
768− gzFile file;
769− z_off_t offset;
770− int whence;
771−{
772− gz_stream *s = (gz_stream*)file;
773−
774− if (s == NULL || whence == SEEK_END ||
775− s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
776− return -1L;
777− }
778−
779− if (s->mode == 'w') {
780−#ifdef NO_GZCOMPRESS
781− return -1L;
782−#else
783− if (whence == SEEK_SET) {
784− offset -= s->in;
785− }
786− if (offset < 0) return -1L;
787−
788− /* At this point, offset is the number of zero bytes to write. */
789− if (s->inbuf == Z_NULL) {
790− s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
791− if (s->inbuf == Z_NULL) return -1L;
792− zmemzero(s->inbuf, Z_BUFSIZE);
793− }
794− while (offset > 0) {
795− uInt size = Z_BUFSIZE;
796− if (offset < Z_BUFSIZE) size = (uInt)offset;
797−
798− size = gzwrite(file, s->inbuf, size);
799− if (size == 0) return -1L;
800−
801− offset -= size;
802− }
803− return s->in;
804−#endif
805− }
806− /* Rest of function is for reading only */
807−
808− /* compute absolute position */
809− if (whence == SEEK_CUR) {
810− offset += s->out;
811− }
812− if (offset < 0) return -1L;
813−
814− if (s->transparent) {
815− /* map to fseek */
816− s->back = EOF;
817− s->stream.avail_in = 0;
818− s->stream.next_in = s->inbuf;
819− if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
820−
821− s->in = s->out = offset;
822− return offset;
823− }
824−
825− /* For a negative seek, rewind and use positive seek */
826− if (offset >= s->out) {
827− offset -= s->out;
828− } else if (gzrewind(file) < 0) {
829− return -1L;
830− }
831− /* offset is now the number of bytes to skip. */
832−
833− if (offset != 0 && s->outbuf == Z_NULL) {
834− s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
835− if (s->outbuf == Z_NULL) return -1L;
836− }
837− if (offset && s->back != EOF) {
838− s->back = EOF;
839− s->out++;
840− offset--;
841− if (s->last) s->z_err = Z_STREAM_END;
842− }
843− while (offset > 0) {
844− int size = Z_BUFSIZE;
845− if (offset < Z_BUFSIZE) size = (int)offset;
846−
847− size = gzread(file, s->outbuf, (uInt)size);
848− if (size <= 0) return -1L;
849− offset -= size;
850− }
851− return s->out;
852−}
853−
854−/* ===========================================================================
855− Rewinds input file.
856−*/
857−int ZEXPORT gzrewind (file)
858− gzFile file;
859−{
860− gz_stream *s = (gz_stream*)file;
861−
862− if (s == NULL || s->mode != 'r') return -1;
863−
864− s->z_err = Z_OK;
865− s->z_eof = 0;
866− s->back = EOF;
867− s->stream.avail_in = 0;
868− s->stream.next_in = s->inbuf;
869− s->crc = crc32(0L, Z_NULL, 0);
870− if (!s->transparent) (void)inflateReset(&s->stream);
871− s->in = 0;
872− s->out = 0;
873− return fseek(s->file, s->start, SEEK_SET);
874−}
875−
876−/* ===========================================================================
877− Returns the starting position for the next gzread or gzwrite on the
878− given compressed file. This position represents a number of bytes in the
879− uncompressed data stream.
880−*/
881−z_off_t ZEXPORT gztell (file)
882− gzFile file;
883−{
884− return gzseek(file, 0L, SEEK_CUR);
885−}
886−
887−/* ===========================================================================
888− Returns 1 when EOF has previously been detected reading the given
889− input stream, otherwise zero.
890−*/
891−int ZEXPORT gzeof (file)
892− gzFile file;
893−{
894− gz_stream *s = (gz_stream*)file;
895−
896− /* With concatenated compressed files that can have embedded
897− * crc trailers, z_eof is no longer the only/best indicator of EOF
898− * on a gz_stream. Handle end-of-stream error explicitly here.
899− */
900− if (s == NULL || s->mode != 'r') return 0;
901− if (s->z_eof) return 1;
902− return s->z_err == Z_STREAM_END;
903−}
904−
905−/* ===========================================================================
906− Returns 1 if reading and doing so transparently, otherwise zero.
907−*/
908−int ZEXPORT gzdirect (file)
909− gzFile file;
910−{
911− gz_stream *s = (gz_stream*)file;
912−
913− if (s == NULL || s->mode != 'r') return 0;
914− return s->transparent;
915−}
916−
917−/* ===========================================================================
918− Outputs a long in LSB order to the given file
919−*/
920−local void putLong (file, x)
921− FILE *file;
922− uLong x;
923−{
924− int n;
925− for (n = 0; n < 4; n++) {
926− fputc((int)(x & 0xff), file);
927− x >>= 8;
928− }
929−}
930−
931−/* ===========================================================================
932− Reads a long in LSB order from the given gz_stream. Sets z_err in case
933− of error.
934−*/
935−local uLong getLong (s)
936− gz_stream *s;
937−{
938− uLong x = (uLong)get_byte(s);
939− int c;
940−
941− x += ((uLong)get_byte(s))<<8;
942− x += ((uLong)get_byte(s))<<16;
943− c = get_byte(s);
944− if (c == EOF) s->z_err = Z_DATA_ERROR;
945− x += ((uLong)c)<<24;
946− return x;
947−}
948−
949−/* ===========================================================================
950− Flushes all pending output if necessary, closes the compressed file
951− and deallocates all the (de)compression state.
952−*/
953−int ZEXPORT gzclose (file)
954− gzFile file;
955−{
956− gz_stream *s = (gz_stream*)file;
957−
958− if (s == NULL) return Z_STREAM_ERROR;
959−
960− if (s->mode == 'w') {
961−#ifdef NO_GZCOMPRESS
962− return Z_STREAM_ERROR;
963−#else
964− if (do_flush (file, Z_FINISH) != Z_OK)
965− return destroy((gz_stream*)file);
966−
967− putLong (s->file, s->crc);
968− putLong (s->file, (uLong)(s->in & 0xffffffff));
969−#endif
970− }
971− return destroy((gz_stream*)file);
972−}
973−
974−#ifdef STDC
975−# define zstrerror(errnum) strerror(errnum)
976−#else
977−# define zstrerror(errnum) ""
978−#endif
979−
980−/* ===========================================================================
981− Returns the error message for the last error which occurred on the
982− given compressed file. errnum is set to zlib error number. If an
983− error occurred in the file system and not in the compression library,
984− errnum is set to Z_ERRNO and the application may consult errno
985− to get the exact error code.
986−*/
987−const char * ZEXPORT gzerror (file, errnum)
988− gzFile file;
989− int *errnum;
990−{
991− char *m;
992− gz_stream *s = (gz_stream*)file;
993−
994− if (s == NULL) {
995− *errnum = Z_STREAM_ERROR;
996− return (const char*)ERR_MSG(Z_STREAM_ERROR);
997− }
998− *errnum = s->z_err;
999− if (*errnum == Z_OK) return (const char*)"";
1000−
1001− m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
1002−
1003− if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
1004−
1005− TRYFREE(s->msg);
1006− s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
1007− if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
1008− strcpy(s->msg, s->path);
1009− strcat(s->msg, ": ");
1010− strcat(s->msg, m);
1011− return (const char*)s->msg;
1012−}
1013−
1014−/* ===========================================================================
1015− Clear the error and end-of-file flags, and do the same for the real file.
1016−*/
1017−void ZEXPORT gzclearerr (file)
1018− gzFile file;
1019−{
1020− gz_stream *s = (gz_stream*)file;
1021−
1022− if (s == NULL) return;
1023− if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
1024− s->z_eof = 0;
1025− clearerr(s->file);
1026−}
cbits/infback.cdeleted−623
@@ -1,623 +0,0 @@
1−/* infback.c -- inflate using a call-back interface
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− This code is largely copied from inflate.c. Normally either infback.o or
8− inflate.o would be linked into an application--not both. The interface
9− with inffast.c is retained so that optimized assembler-coded versions of
10− inflate_fast() can be used with either inflate.c or infback.c.
11− */
12−
13−#include "zutil.h"
14−#include "inftrees.h"
15−#include "inflate.h"
16−#include "inffast.h"
17−
18−/* function prototypes */
19−local void fixedtables OF((struct inflate_state FAR *state));
20−
21−/*
22− strm provides memory allocation functions in zalloc and zfree, or
23− Z_NULL to use the library memory allocation functions.
24−
25− windowBits is in the range 8..15, and window is a user-supplied
26− window and output buffer that is 2**windowBits bytes.
27− */
28−int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
29−z_streamp strm;
30−int windowBits;
31−unsigned char FAR *window;
32−const char *version;
33−int stream_size;
34−{
35− struct inflate_state FAR *state;
36−
37− if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
38− stream_size != (int)(sizeof(z_stream)))
39− return Z_VERSION_ERROR;
40− if (strm == Z_NULL || window == Z_NULL ||
41− windowBits < 8 || windowBits > 15)
42− return Z_STREAM_ERROR;
43− strm->msg = Z_NULL; /* in case we return an error */
44− if (strm->zalloc == (alloc_func)0) {
45− strm->zalloc = zcalloc;
46− strm->opaque = (voidpf)0;
47− }
48− if (strm->zfree == (free_func)0) strm->zfree = zcfree;
49− state = (struct inflate_state FAR *)ZALLOC(strm, 1,
50− sizeof(struct inflate_state));
51− if (state == Z_NULL) return Z_MEM_ERROR;
52− Tracev((stderr, "inflate: allocated\n"));
53− strm->state = (struct internal_state FAR *)state;
54− state->dmax = 32768U;
55− state->wbits = windowBits;
56− state->wsize = 1U << windowBits;
57− state->window = window;
58− state->write = 0;
59− state->whave = 0;
60− return Z_OK;
61−}
62−
63−/*
64− Return state with length and distance decoding tables and index sizes set to
65− fixed code decoding. Normally this returns fixed tables from inffixed.h.
66− If BUILDFIXED is defined, then instead this routine builds the tables the
67− first time it's called, and returns those tables the first time and
68− thereafter. This reduces the size of the code by about 2K bytes, in
69− exchange for a little execution time. However, BUILDFIXED should not be
70− used for threaded applications, since the rewriting of the tables and virgin
71− may not be thread-safe.
72− */
73−local void fixedtables(state)
74−struct inflate_state FAR *state;
75−{
76−#ifdef BUILDFIXED
77− static int virgin = 1;
78− static code *lenfix, *distfix;
79− static code fixed[544];
80−
81− /* build fixed huffman tables if first call (may not be thread safe) */
82− if (virgin) {
83− unsigned sym, bits;
84− static code *next;
85−
86− /* literal/length table */
87− sym = 0;
88− while (sym < 144) state->lens[sym++] = 8;
89− while (sym < 256) state->lens[sym++] = 9;
90− while (sym < 280) state->lens[sym++] = 7;
91− while (sym < 288) state->lens[sym++] = 8;
92− next = fixed;
93− lenfix = next;
94− bits = 9;
95− inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
96−
97− /* distance table */
98− sym = 0;
99− while (sym < 32) state->lens[sym++] = 5;
100− distfix = next;
101− bits = 5;
102− inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
103−
104− /* do this just once */
105− virgin = 0;
106− }
107−#else /* !BUILDFIXED */
108−# include "inffixed.h"
109−#endif /* BUILDFIXED */
110− state->lencode = lenfix;
111− state->lenbits = 9;
112− state->distcode = distfix;
113− state->distbits = 5;
114−}
115−
116−/* Macros for inflateBack(): */
117−
118−/* Load returned state from inflate_fast() */
119−#define LOAD() \
120− do { \
121− put = strm->next_out; \
122− left = strm->avail_out; \
123− next = strm->next_in; \
124− have = strm->avail_in; \
125− hold = state->hold; \
126− bits = state->bits; \
127− } while (0)
128−
129−/* Set state from registers for inflate_fast() */
130−#define RESTORE() \
131− do { \
132− strm->next_out = put; \
133− strm->avail_out = left; \
134− strm->next_in = next; \
135− strm->avail_in = have; \
136− state->hold = hold; \
137− state->bits = bits; \
138− } while (0)
139−
140−/* Clear the input bit accumulator */
141−#define INITBITS() \
142− do { \
143− hold = 0; \
144− bits = 0; \
145− } while (0)
146−
147−/* Assure that some input is available. If input is requested, but denied,
148− then return a Z_BUF_ERROR from inflateBack(). */
149−#define PULL() \
150− do { \
151− if (have == 0) { \
152− have = in(in_desc, &next); \
153− if (have == 0) { \
154− next = Z_NULL; \
155− ret = Z_BUF_ERROR; \
156− goto inf_leave; \
157− } \
158− } \
159− } while (0)
160−
161−/* Get a byte of input into the bit accumulator, or return from inflateBack()
162− with an error if there is no input available. */
163−#define PULLBYTE() \
164− do { \
165− PULL(); \
166− have--; \
167− hold += (unsigned long)(*next++) << bits; \
168− bits += 8; \
169− } while (0)
170−
171−/* Assure that there are at least n bits in the bit accumulator. If there is
172− not enough available input to do that, then return from inflateBack() with
173− an error. */
174−#define NEEDBITS(n) \
175− do { \
176− while (bits < (unsigned)(n)) \
177− PULLBYTE(); \
178− } while (0)
179−
180−/* Return the low n bits of the bit accumulator (n < 16) */
181−#define BITS(n) \
182− ((unsigned)hold & ((1U << (n)) - 1))
183−
184−/* Remove n bits from the bit accumulator */
185−#define DROPBITS(n) \
186− do { \
187− hold >>= (n); \
188− bits -= (unsigned)(n); \
189− } while (0)
190−
191−/* Remove zero to seven bits as needed to go to a byte boundary */
192−#define BYTEBITS() \
193− do { \
194− hold >>= bits & 7; \
195− bits -= bits & 7; \
196− } while (0)
197−
198−/* Assure that some output space is available, by writing out the window
199− if it's full. If the write fails, return from inflateBack() with a
200− Z_BUF_ERROR. */
201−#define ROOM() \
202− do { \
203− if (left == 0) { \
204− put = state->window; \
205− left = state->wsize; \
206− state->whave = left; \
207− if (out(out_desc, put, left)) { \
208− ret = Z_BUF_ERROR; \
209− goto inf_leave; \
210− } \
211− } \
212− } while (0)
213−
214−/*
215− strm provides the memory allocation functions and window buffer on input,
216− and provides information on the unused input on return. For Z_DATA_ERROR
217− returns, strm will also provide an error message.
218−
219− in() and out() are the call-back input and output functions. When
220− inflateBack() needs more input, it calls in(). When inflateBack() has
221− filled the window with output, or when it completes with data in the
222− window, it calls out() to write out the data. The application must not
223− change the provided input until in() is called again or inflateBack()
224− returns. The application must not change the window/output buffer until
225− inflateBack() returns.
226−
227− in() and out() are called with a descriptor parameter provided in the
228− inflateBack() call. This parameter can be a structure that provides the
229− information required to do the read or write, as well as accumulated
230− information on the input and output such as totals and check values.
231−
232− in() should return zero on failure. out() should return non-zero on
233− failure. If either in() or out() fails, than inflateBack() returns a
234− Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
235− was in() or out() that caused in the error. Otherwise, inflateBack()
236− returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
237− error, or Z_MEM_ERROR if it could not allocate memory for the state.
238− inflateBack() can also return Z_STREAM_ERROR if the input parameters
239− are not correct, i.e. strm is Z_NULL or the state was not initialized.
240− */
241−int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
242−z_streamp strm;
243−in_func in;
244−void FAR *in_desc;
245−out_func out;
246−void FAR *out_desc;
247−{
248− struct inflate_state FAR *state;
249− unsigned char FAR *next; /* next input */
250− unsigned char FAR *put; /* next output */
251− unsigned have, left; /* available input and output */
252− unsigned long hold; /* bit buffer */
253− unsigned bits; /* bits in bit buffer */
254− unsigned copy; /* number of stored or match bytes to copy */
255− unsigned char FAR *from; /* where to copy match bytes from */
256− code this; /* current decoding table entry */
257− code last; /* parent table entry */
258− unsigned len; /* length to copy for repeats, bits to drop */
259− int ret; /* return code */
260− static const unsigned short order[19] = /* permutation of code lengths */
261− {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
262−
263− /* Check that the strm exists and that the state was initialized */
264− if (strm == Z_NULL || strm->state == Z_NULL)
265− return Z_STREAM_ERROR;
266− state = (struct inflate_state FAR *)strm->state;
267−
268− /* Reset the state */
269− strm->msg = Z_NULL;
270− state->mode = TYPE;
271− state->last = 0;
272− state->whave = 0;
273− next = strm->next_in;
274− have = next != Z_NULL ? strm->avail_in : 0;
275− hold = 0;
276− bits = 0;
277− put = state->window;
278− left = state->wsize;
279−
280− /* Inflate until end of block marked as last */
281− for (;;)
282− switch (state->mode) {
283− case TYPE:
284− /* determine and dispatch block type */
285− if (state->last) {
286− BYTEBITS();
287− state->mode = DONE;
288− break;
289− }
290− NEEDBITS(3);
291− state->last = BITS(1);
292− DROPBITS(1);
293− switch (BITS(2)) {
294− case 0: /* stored block */
295− Tracev((stderr, "inflate: stored block%s\n",
296− state->last ? " (last)" : ""));
297− state->mode = STORED;
298− break;
299− case 1: /* fixed block */
300− fixedtables(state);
301− Tracev((stderr, "inflate: fixed codes block%s\n",
302− state->last ? " (last)" : ""));
303− state->mode = LEN; /* decode codes */
304− break;
305− case 2: /* dynamic block */
306− Tracev((stderr, "inflate: dynamic codes block%s\n",
307− state->last ? " (last)" : ""));
308− state->mode = TABLE;
309− break;
310− case 3:
311− strm->msg = (char *)"invalid block type";
312− state->mode = BAD;
313− }
314− DROPBITS(2);
315− break;
316−
317− case STORED:
318− /* get and verify stored block length */
319− BYTEBITS(); /* go to byte boundary */
320− NEEDBITS(32);
321− if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
322− strm->msg = (char *)"invalid stored block lengths";
323− state->mode = BAD;
324− break;
325− }
326− state->length = (unsigned)hold & 0xffff;
327− Tracev((stderr, "inflate: stored length %u\n",
328− state->length));
329− INITBITS();
330−
331− /* copy stored block from input to output */
332− while (state->length != 0) {
333− copy = state->length;
334− PULL();
335− ROOM();
336− if (copy > have) copy = have;
337− if (copy > left) copy = left;
338− zmemcpy(put, next, copy);
339− have -= copy;
340− next += copy;
341− left -= copy;
342− put += copy;
343− state->length -= copy;
344− }
345− Tracev((stderr, "inflate: stored end\n"));
346− state->mode = TYPE;
347− break;
348−
349− case TABLE:
350− /* get dynamic table entries descriptor */
351− NEEDBITS(14);
352− state->nlen = BITS(5) + 257;
353− DROPBITS(5);
354− state->ndist = BITS(5) + 1;
355− DROPBITS(5);
356− state->ncode = BITS(4) + 4;
357− DROPBITS(4);
358−#ifndef PKZIP_BUG_WORKAROUND
359− if (state->nlen > 286 || state->ndist > 30) {
360− strm->msg = (char *)"too many length or distance symbols";
361− state->mode = BAD;
362− break;
363− }
364−#endif
365− Tracev((stderr, "inflate: table sizes ok\n"));
366−
367− /* get code length code lengths (not a typo) */
368− state->have = 0;
369− while (state->have < state->ncode) {
370− NEEDBITS(3);
371− state->lens[order[state->have++]] = (unsigned short)BITS(3);
372− DROPBITS(3);
373− }
374− while (state->have < 19)
375− state->lens[order[state->have++]] = 0;
376− state->next = state->codes;
377− state->lencode = (code const FAR *)(state->next);
378− state->lenbits = 7;
379− ret = inflate_table(CODES, state->lens, 19, &(state->next),
380− &(state->lenbits), state->work);
381− if (ret) {
382− strm->msg = (char *)"invalid code lengths set";
383− state->mode = BAD;
384− break;
385− }
386− Tracev((stderr, "inflate: code lengths ok\n"));
387−
388− /* get length and distance code code lengths */
389− state->have = 0;
390− while (state->have < state->nlen + state->ndist) {
391− for (;;) {
392− this = state->lencode[BITS(state->lenbits)];
393− if ((unsigned)(this.bits) <= bits) break;
394− PULLBYTE();
395− }
396− if (this.val < 16) {
397− NEEDBITS(this.bits);
398− DROPBITS(this.bits);
399− state->lens[state->have++] = this.val;
400− }
401− else {
402− if (this.val == 16) {
403− NEEDBITS(this.bits + 2);
404− DROPBITS(this.bits);
405− if (state->have == 0) {
406− strm->msg = (char *)"invalid bit length repeat";
407− state->mode = BAD;
408− break;
409− }
410− len = (unsigned)(state->lens[state->have - 1]);
411− copy = 3 + BITS(2);
412− DROPBITS(2);
413− }
414− else if (this.val == 17) {
415− NEEDBITS(this.bits + 3);
416− DROPBITS(this.bits);
417− len = 0;
418− copy = 3 + BITS(3);
419− DROPBITS(3);
420− }
421− else {
422− NEEDBITS(this.bits + 7);
423− DROPBITS(this.bits);
424− len = 0;
425− copy = 11 + BITS(7);
426− DROPBITS(7);
427− }
428− if (state->have + copy > state->nlen + state->ndist) {
429− strm->msg = (char *)"invalid bit length repeat";
430− state->mode = BAD;
431− break;
432− }
433− while (copy--)
434− state->lens[state->have++] = (unsigned short)len;
435− }
436− }
437−
438− /* handle error breaks in while */
439− if (state->mode == BAD) break;
440−
441− /* build code tables */
442− state->next = state->codes;
443− state->lencode = (code const FAR *)(state->next);
444− state->lenbits = 9;
445− ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
446− &(state->lenbits), state->work);
447− if (ret) {
448− strm->msg = (char *)"invalid literal/lengths set";
449− state->mode = BAD;
450− break;
451− }
452− state->distcode = (code const FAR *)(state->next);
453− state->distbits = 6;
454− ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
455− &(state->next), &(state->distbits), state->work);
456− if (ret) {
457− strm->msg = (char *)"invalid distances set";
458− state->mode = BAD;
459− break;
460− }
461− Tracev((stderr, "inflate: codes ok\n"));
462− state->mode = LEN;
463−
464− case LEN:
465− /* use inflate_fast() if we have enough input and output */
466− if (have >= 6 && left >= 258) {
467− RESTORE();
468− if (state->whave < state->wsize)
469− state->whave = state->wsize - left;
470− inflate_fast(strm, state->wsize);
471− LOAD();
472− break;
473− }
474−
475− /* get a literal, length, or end-of-block code */
476− for (;;) {
477− this = state->lencode[BITS(state->lenbits)];
478− if ((unsigned)(this.bits) <= bits) break;
479− PULLBYTE();
480− }
481− if (this.op && (this.op & 0xf0) == 0) {
482− last = this;
483− for (;;) {
484− this = state->lencode[last.val +
485− (BITS(last.bits + last.op) >> last.bits)];
486− if ((unsigned)(last.bits + this.bits) <= bits) break;
487− PULLBYTE();
488− }
489− DROPBITS(last.bits);
490− }
491− DROPBITS(this.bits);
492− state->length = (unsigned)this.val;
493−
494− /* process literal */
495− if (this.op == 0) {
496− Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
497− "inflate: literal '%c'\n" :
498− "inflate: literal 0x%02x\n", this.val));
499− ROOM();
500− *put++ = (unsigned char)(state->length);
501− left--;
502− state->mode = LEN;
503− break;
504− }
505−
506− /* process end of block */
507− if (this.op & 32) {
508− Tracevv((stderr, "inflate: end of block\n"));
509− state->mode = TYPE;
510− break;
511− }
512−
513− /* invalid code */
514− if (this.op & 64) {
515− strm->msg = (char *)"invalid literal/length code";
516− state->mode = BAD;
517− break;
518− }
519−
520− /* length code -- get extra bits, if any */
521− state->extra = (unsigned)(this.op) & 15;
522− if (state->extra != 0) {
523− NEEDBITS(state->extra);
524− state->length += BITS(state->extra);
525− DROPBITS(state->extra);
526− }
527− Tracevv((stderr, "inflate: length %u\n", state->length));
528−
529− /* get distance code */
530− for (;;) {
531− this = state->distcode[BITS(state->distbits)];
532− if ((unsigned)(this.bits) <= bits) break;
533− PULLBYTE();
534− }
535− if ((this.op & 0xf0) == 0) {
536− last = this;
537− for (;;) {
538− this = state->distcode[last.val +
539− (BITS(last.bits + last.op) >> last.bits)];
540− if ((unsigned)(last.bits + this.bits) <= bits) break;
541− PULLBYTE();
542− }
543− DROPBITS(last.bits);
544− }
545− DROPBITS(this.bits);
546− if (this.op & 64) {
547− strm->msg = (char *)"invalid distance code";
548− state->mode = BAD;
549− break;
550− }
551− state->offset = (unsigned)this.val;
552−
553− /* get distance extra bits, if any */
554− state->extra = (unsigned)(this.op) & 15;
555− if (state->extra != 0) {
556− NEEDBITS(state->extra);
557− state->offset += BITS(state->extra);
558− DROPBITS(state->extra);
559− }
560− if (state->offset > state->wsize - (state->whave < state->wsize ?
561− left : 0)) {
562− strm->msg = (char *)"invalid distance too far back";
563− state->mode = BAD;
564− break;
565− }
566− Tracevv((stderr, "inflate: distance %u\n", state->offset));
567−
568− /* copy match from window to output */
569− do {
570− ROOM();
571− copy = state->wsize - state->offset;
572− if (copy < left) {
573− from = put + copy;
574− copy = left - copy;
575− }
576− else {
577− from = put - state->offset;
578− copy = left;
579− }
580− if (copy > state->length) copy = state->length;
581− state->length -= copy;
582− left -= copy;
583− do {
584− *put++ = *from++;
585− } while (--copy);
586− } while (state->length != 0);
587− break;
588−
589− case DONE:
590− /* inflate stream terminated properly -- write leftover output */
591− ret = Z_STREAM_END;
592− if (left < state->wsize) {
593− if (out(out_desc, state->window, state->wsize - left))
594− ret = Z_BUF_ERROR;
595− }
596− goto inf_leave;
597−
598− case BAD:
599− ret = Z_DATA_ERROR;
600− goto inf_leave;
601−
602− default: /* can't happen, but makes compilers happy */
603− ret = Z_STREAM_ERROR;
604− goto inf_leave;
605− }
606−
607− /* Return unused input */
608− inf_leave:
609− strm->next_in = next;
610− strm->avail_in = have;
611− return ret;
612−}
613−
614−int ZEXPORT inflateBackEnd(strm)
615−z_streamp strm;
616−{
617− if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
618− return Z_STREAM_ERROR;
619− ZFREE(strm, strm->state);
620− strm->state = Z_NULL;
621− Tracev((stderr, "inflate: end\n"));
622− return Z_OK;
623−}
cbits/inffast.cdeleted−318
@@ -1,318 +0,0 @@
1−/* inffast.c -- fast decoding
2− * Copyright (C) 1995-2004 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−#include "zutil.h"
7−#include "inftrees.h"
8−#include "inflate.h"
9−#include "inffast.h"
10−
11−#ifndef ASMINF
12−
13−/* Allow machine dependent optimization for post-increment or pre-increment.
14− Based on testing to date,
15− Pre-increment preferred for:
16− - PowerPC G3 (Adler)
17− - MIPS R5000 (Randers-Pehrson)
18− Post-increment preferred for:
19− - none
20− No measurable difference:
21− - Pentium III (Anderson)
22− - M68060 (Nikl)
23− */
24−#ifdef POSTINC
25−# define OFF 0
26−# define PUP(a) *(a)++
27−#else
28−# define OFF 1
29−# define PUP(a) *++(a)
30−#endif
31−
32−/*
33− Decode literal, length, and distance codes and write out the resulting
34− literal and match bytes until either not enough input or output is
35− available, an end-of-block is encountered, or a data error is encountered.
36− When large enough input and output buffers are supplied to inflate(), for
37− example, a 16K input buffer and a 64K output buffer, more than 95% of the
38− inflate execution time is spent in this routine.
39−
40− Entry assumptions:
41−
42− state->mode == LEN
43− strm->avail_in >= 6
44− strm->avail_out >= 258
45− start >= strm->avail_out
46− state->bits < 8
47−
48− On return, state->mode is one of:
49−
50− LEN -- ran out of enough output space or enough available input
51− TYPE -- reached end of block code, inflate() to interpret next block
52− BAD -- error in block data
53−
54− Notes:
55−
56− - The maximum input bits used by a length/distance pair is 15 bits for the
57− length code, 5 bits for the length extra, 15 bits for the distance code,
58− and 13 bits for the distance extra. This totals 48 bits, or six bytes.
59− Therefore if strm->avail_in >= 6, then there is enough input to avoid
60− checking for available input while decoding.
61−
62− - The maximum bytes that a single length/distance pair can output is 258
63− bytes, which is the maximum length that can be coded. inflate_fast()
64− requires strm->avail_out >= 258 for each loop to avoid checking for
65− output space.
66− */
67−void inflate_fast(strm, start)
68−z_streamp strm;
69−unsigned start; /* inflate()'s starting value for strm->avail_out */
70−{
71− struct inflate_state FAR *state;
72− unsigned char FAR *in; /* local strm->next_in */
73− unsigned char FAR *last; /* while in < last, enough input available */
74− unsigned char FAR *out; /* local strm->next_out */
75− unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
76− unsigned char FAR *end; /* while out < end, enough space available */
77−#ifdef INFLATE_STRICT
78− unsigned dmax; /* maximum distance from zlib header */
79−#endif
80− unsigned wsize; /* window size or zero if not using window */
81− unsigned whave; /* valid bytes in the window */
82− unsigned write; /* window write index */
83− unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
84− unsigned long hold; /* local strm->hold */
85− unsigned bits; /* local strm->bits */
86− code const FAR *lcode; /* local strm->lencode */
87− code const FAR *dcode; /* local strm->distcode */
88− unsigned lmask; /* mask for first level of length codes */
89− unsigned dmask; /* mask for first level of distance codes */
90− code this; /* retrieved table entry */
91− unsigned op; /* code bits, operation, extra bits, or */
92− /* window position, window bytes to copy */
93− unsigned len; /* match length, unused bytes */
94− unsigned dist; /* match distance */
95− unsigned char FAR *from; /* where to copy match from */
96−
97− /* copy state to local variables */
98− state = (struct inflate_state FAR *)strm->state;
99− in = strm->next_in - OFF;
100− last = in + (strm->avail_in - 5);
101− out = strm->next_out - OFF;
102− beg = out - (start - strm->avail_out);
103− end = out + (strm->avail_out - 257);
104−#ifdef INFLATE_STRICT
105− dmax = state->dmax;
106−#endif
107− wsize = state->wsize;
108− whave = state->whave;
109− write = state->write;
110− window = state->window;
111− hold = state->hold;
112− bits = state->bits;
113− lcode = state->lencode;
114− dcode = state->distcode;
115− lmask = (1U << state->lenbits) - 1;
116− dmask = (1U << state->distbits) - 1;
117−
118− /* decode literals and length/distances until end-of-block or not enough
119− input data or output space */
120− do {
121− if (bits < 15) {
122− hold += (unsigned long)(PUP(in)) << bits;
123− bits += 8;
124− hold += (unsigned long)(PUP(in)) << bits;
125− bits += 8;
126− }
127− this = lcode[hold & lmask];
128− dolen:
129− op = (unsigned)(this.bits);
130− hold >>= op;
131− bits -= op;
132− op = (unsigned)(this.op);
133− if (op == 0) { /* literal */
134− Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
135− "inflate: literal '%c'\n" :
136− "inflate: literal 0x%02x\n", this.val));
137− PUP(out) = (unsigned char)(this.val);
138− }
139− else if (op & 16) { /* length base */
140− len = (unsigned)(this.val);
141− op &= 15; /* number of extra bits */
142− if (op) {
143− if (bits < op) {
144− hold += (unsigned long)(PUP(in)) << bits;
145− bits += 8;
146− }
147− len += (unsigned)hold & ((1U << op) - 1);
148− hold >>= op;
149− bits -= op;
150− }
151− Tracevv((stderr, "inflate: length %u\n", len));
152− if (bits < 15) {
153− hold += (unsigned long)(PUP(in)) << bits;
154− bits += 8;
155− hold += (unsigned long)(PUP(in)) << bits;
156− bits += 8;
157− }
158− this = dcode[hold & dmask];
159− dodist:
160− op = (unsigned)(this.bits);
161− hold >>= op;
162− bits -= op;
163− op = (unsigned)(this.op);
164− if (op & 16) { /* distance base */
165− dist = (unsigned)(this.val);
166− op &= 15; /* number of extra bits */
167− if (bits < op) {
168− hold += (unsigned long)(PUP(in)) << bits;
169− bits += 8;
170− if (bits < op) {
171− hold += (unsigned long)(PUP(in)) << bits;
172− bits += 8;
173− }
174− }
175− dist += (unsigned)hold & ((1U << op) - 1);
176−#ifdef INFLATE_STRICT
177− if (dist > dmax) {
178− strm->msg = (char *)"invalid distance too far back";
179− state->mode = BAD;
180− break;
181− }
182−#endif
183− hold >>= op;
184− bits -= op;
185− Tracevv((stderr, "inflate: distance %u\n", dist));
186− op = (unsigned)(out - beg); /* max distance in output */
187− if (dist > op) { /* see if copy from window */
188− op = dist - op; /* distance back in window */
189− if (op > whave) {
190− strm->msg = (char *)"invalid distance too far back";
191− state->mode = BAD;
192− break;
193− }
194− from = window - OFF;
195− if (write == 0) { /* very common case */
196− from += wsize - op;
197− if (op < len) { /* some from window */
198− len -= op;
199− do {
200− PUP(out) = PUP(from);
201− } while (--op);
202− from = out - dist; /* rest from output */
203− }
204− }
205− else if (write < op) { /* wrap around window */
206− from += wsize + write - op;
207− op -= write;
208− if (op < len) { /* some from end of window */
209− len -= op;
210− do {
211− PUP(out) = PUP(from);
212− } while (--op);
213− from = window - OFF;
214− if (write < len) { /* some from start of window */
215− op = write;
216− len -= op;
217− do {
218− PUP(out) = PUP(from);
219− } while (--op);
220− from = out - dist; /* rest from output */
221− }
222− }
223− }
224− else { /* contiguous in window */
225− from += write - op;
226− if (op < len) { /* some from window */
227− len -= op;
228− do {
229− PUP(out) = PUP(from);
230− } while (--op);
231− from = out - dist; /* rest from output */
232− }
233− }
234− while (len > 2) {
235− PUP(out) = PUP(from);
236− PUP(out) = PUP(from);
237− PUP(out) = PUP(from);
238− len -= 3;
239− }
240− if (len) {
241− PUP(out) = PUP(from);
242− if (len > 1)
243− PUP(out) = PUP(from);
244− }
245− }
246− else {
247− from = out - dist; /* copy direct from output */
248− do { /* minimum length is three */
249− PUP(out) = PUP(from);
250− PUP(out) = PUP(from);
251− PUP(out) = PUP(from);
252− len -= 3;
253− } while (len > 2);
254− if (len) {
255− PUP(out) = PUP(from);
256− if (len > 1)
257− PUP(out) = PUP(from);
258− }
259− }
260− }
261− else if ((op & 64) == 0) { /* 2nd level distance code */
262− this = dcode[this.val + (hold & ((1U << op) - 1))];
263− goto dodist;
264− }
265− else {
266− strm->msg = (char *)"invalid distance code";
267− state->mode = BAD;
268− break;
269− }
270− }
271− else if ((op & 64) == 0) { /* 2nd level length code */
272− this = lcode[this.val + (hold & ((1U << op) - 1))];
273− goto dolen;
274− }
275− else if (op & 32) { /* end-of-block */
276− Tracevv((stderr, "inflate: end of block\n"));
277− state->mode = TYPE;
278− break;
279− }
280− else {
281− strm->msg = (char *)"invalid literal/length code";
282− state->mode = BAD;
283− break;
284− }
285− } while (in < last && out < end);
286−
287− /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
288− len = bits >> 3;
289− in -= len;
290− bits -= len << 3;
291− hold &= (1U << bits) - 1;
292−
293− /* update state and return */
294− strm->next_in = in + OFF;
295− strm->next_out = out + OFF;
296− strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
297− strm->avail_out = (unsigned)(out < end ?
298− 257 + (end - out) : 257 - (out - end));
299− state->hold = hold;
300− state->bits = bits;
301− return;
302−}
303−
304−/*
305− inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
306− - Using bit fields for code structure
307− - Different op definition to avoid & for extra bits (do & for table bits)
308− - Three separate decoding do-loops for direct, window, and write == 0
309− - Special case for distance > 1 copies to do overlapped load and store copy
310− - Explicit branch predictions (based on measured branch probabilities)
311− - Deferring match copy and interspersed it with decoding subsequent codes
312− - Swapping literal/length else
313− - Swapping window/direct else
314− - Larger unrolled copy loops (three is about right)
315− - Moving len -= 3 statement into middle of loop
316− */
317−
318−#endif /* !ASMINF */
cbits/inffast.hdeleted−11
@@ -1,11 +0,0 @@
1−/* inffast.h -- header to use inffast.c
2− * Copyright (C) 1995-2003 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−void inflate_fast OF((z_streamp strm, unsigned start));
cbits/inffixed.hdeleted−94
@@ -1,94 +0,0 @@
1− /* inffixed.h -- table for decoding fixed codes
2− * Generated automatically by makefixed().
3− */
4−
5− /* WARNING: this file should *not* be used by applications. It
6− is part of the implementation of the compression library and
7− is subject to change. Applications should only use zlib.h.
8− */
9−
10− static const code lenfix[512] = {
11− {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
12− {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
13− {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
14− {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
15− {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
16− {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
17− {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
18− {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
19− {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
20− {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
21− {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
22− {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
23− {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
24− {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
25− {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
26− {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
27− {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
28− {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
29− {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
30− {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
31− {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
32− {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
33− {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
34− {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
35− {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
36− {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
37− {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
38− {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
39− {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
40− {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
41− {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
42− {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
43− {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
44− {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
45− {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
46− {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
47− {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
48− {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
49− {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
50− {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
51− {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
52− {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
53− {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
54− {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
55− {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
56− {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
57− {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
58− {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
59− {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
60− {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
61− {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
62− {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
63− {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
64− {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
65− {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
66− {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
67− {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
68− {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
69− {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
70− {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
71− {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
72− {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
73− {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
74− {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
75− {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
76− {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
77− {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
78− {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
79− {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
80− {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
81− {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
82− {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
83− {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
84− {0,9,255}
85− };
86−
87− static const code distfix[32] = {
88− {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
89− {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
90− {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
91− {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
92− {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
93− {22,5,193},{64,5,0}
94− };
cbits/inflate.cdeleted−1368
@@ -1,1368 +0,0 @@
1−/* inflate.c -- zlib decompression
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− * Change history:
8− *
9− * 1.2.beta0 24 Nov 2002
10− * - First version -- complete rewrite of inflate to simplify code, avoid
11− * creation of window when not needed, minimize use of window when it is
12− * needed, make inffast.c even faster, implement gzip decoding, and to
13− * improve code readability and style over the previous zlib inflate code
14− *
15− * 1.2.beta1 25 Nov 2002
16− * - Use pointers for available input and output checking in inffast.c
17− * - Remove input and output counters in inffast.c
18− * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19− * - Remove unnecessary second byte pull from length extra in inffast.c
20− * - Unroll direct copy to three copies per loop in inffast.c
21− *
22− * 1.2.beta2 4 Dec 2002
23− * - Change external routine names to reduce potential conflicts
24− * - Correct filename to inffixed.h for fixed tables in inflate.c
25− * - Make hbuf[] unsigned char to match parameter type in inflate.c
26− * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27− * to avoid negation problem on Alphas (64 bit) in inflate.c
28− *
29− * 1.2.beta3 22 Dec 2002
30− * - Add comments on state->bits assertion in inffast.c
31− * - Add comments on op field in inftrees.h
32− * - Fix bug in reuse of allocated window after inflateReset()
33− * - Remove bit fields--back to byte structure for speed
34− * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35− * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36− * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37− * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38− * - Use local copies of stream next and avail values, as well as local bit
39− * buffer and bit count in inflate()--for speed when inflate_fast() not used
40− *
41− * 1.2.beta4 1 Jan 2003
42− * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43− * - Move a comment on output buffer sizes from inffast.c to inflate.c
44− * - Add comments in inffast.c to introduce the inflate_fast() routine
45− * - Rearrange window copies in inflate_fast() for speed and simplification
46− * - Unroll last copy for window match in inflate_fast()
47− * - Use local copies of window variables in inflate_fast() for speed
48− * - Pull out common write == 0 case for speed in inflate_fast()
49− * - Make op and len in inflate_fast() unsigned for consistency
50− * - Add FAR to lcode and dcode declarations in inflate_fast()
51− * - Simplified bad distance check in inflate_fast()
52− * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53− * source file infback.c to provide a call-back interface to inflate for
54− * programs like gzip and unzip -- uses window as output buffer to avoid
55− * window copying
56− *
57− * 1.2.beta5 1 Jan 2003
58− * - Improved inflateBack() interface to allow the caller to provide initial
59− * input in strm.
60− * - Fixed stored blocks bug in inflateBack()
61− *
62− * 1.2.beta6 4 Jan 2003
63− * - Added comments in inffast.c on effectiveness of POSTINC
64− * - Typecasting all around to reduce compiler warnings
65− * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66− * make compilers happy
67− * - Changed type of window in inflateBackInit() to unsigned char *
68− *
69− * 1.2.beta7 27 Jan 2003
70− * - Changed many types to unsigned or unsigned short to avoid warnings
71− * - Added inflateCopy() function
72− *
73− * 1.2.0 9 Mar 2003
74− * - Changed inflateBack() interface to provide separate opaque descriptors
75− * for the in() and out() functions
76− * - Changed inflateBack() argument and in_func typedef to swap the length
77− * and buffer address return values for the input function
78− * - Check next_in and next_out for Z_NULL on entry to inflate()
79− *
80− * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81− */
82−
83−#include "zutil.h"
84−#include "inftrees.h"
85−#include "inflate.h"
86−#include "inffast.h"
87−
88−#ifdef MAKEFIXED
89−# ifndef BUILDFIXED
90−# define BUILDFIXED
91−# endif
92−#endif
93−
94−/* function prototypes */
95−local void fixedtables OF((struct inflate_state FAR *state));
96−local int updatewindow OF((z_streamp strm, unsigned out));
97−#ifdef BUILDFIXED
98− void makefixed OF((void));
99−#endif
100−local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101− unsigned len));
102−
103−int ZEXPORT inflateReset(strm)
104−z_streamp strm;
105−{
106− struct inflate_state FAR *state;
107−
108− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
109− state = (struct inflate_state FAR *)strm->state;
110− strm->total_in = strm->total_out = state->total = 0;
111− strm->msg = Z_NULL;
112− strm->adler = 1; /* to support ill-conceived Java test suite */
113− state->mode = HEAD;
114− state->last = 0;
115− state->havedict = 0;
116− state->dmax = 32768U;
117− state->head = Z_NULL;
118− state->wsize = 0;
119− state->whave = 0;
120− state->write = 0;
121− state->hold = 0;
122− state->bits = 0;
123− state->lencode = state->distcode = state->next = state->codes;
124− Tracev((stderr, "inflate: reset\n"));
125− return Z_OK;
126−}
127−
128−int ZEXPORT inflatePrime(strm, bits, value)
129−z_streamp strm;
130−int bits;
131−int value;
132−{
133− struct inflate_state FAR *state;
134−
135− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
136− state = (struct inflate_state FAR *)strm->state;
137− if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
138− value &= (1L << bits) - 1;
139− state->hold += value << state->bits;
140− state->bits += bits;
141− return Z_OK;
142−}
143−
144−int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
145−z_streamp strm;
146−int windowBits;
147−const char *version;
148−int stream_size;
149−{
150− struct inflate_state FAR *state;
151−
152− if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
153− stream_size != (int)(sizeof(z_stream)))
154− return Z_VERSION_ERROR;
155− if (strm == Z_NULL) return Z_STREAM_ERROR;
156− strm->msg = Z_NULL; /* in case we return an error */
157− if (strm->zalloc == (alloc_func)0) {
158− strm->zalloc = zcalloc;
159− strm->opaque = (voidpf)0;
160− }
161− if (strm->zfree == (free_func)0) strm->zfree = zcfree;
162− state = (struct inflate_state FAR *)
163− ZALLOC(strm, 1, sizeof(struct inflate_state));
164− if (state == Z_NULL) return Z_MEM_ERROR;
165− Tracev((stderr, "inflate: allocated\n"));
166− strm->state = (struct internal_state FAR *)state;
167− if (windowBits < 0) {
168− state->wrap = 0;
169− windowBits = -windowBits;
170− }
171− else {
172− state->wrap = (windowBits >> 4) + 1;
173−#ifdef GUNZIP
174− if (windowBits < 48) windowBits &= 15;
175−#endif
176− }
177− if (windowBits < 8 || windowBits > 15) {
178− ZFREE(strm, state);
179− strm->state = Z_NULL;
180− return Z_STREAM_ERROR;
181− }
182− state->wbits = (unsigned)windowBits;
183− state->window = Z_NULL;
184− return inflateReset(strm);
185−}
186−
187−int ZEXPORT inflateInit_(strm, version, stream_size)
188−z_streamp strm;
189−const char *version;
190−int stream_size;
191−{
192− return inflateInit2_(strm, DEF_WBITS, version, stream_size);
193−}
194−
195−/*
196− Return state with length and distance decoding tables and index sizes set to
197− fixed code decoding. Normally this returns fixed tables from inffixed.h.
198− If BUILDFIXED is defined, then instead this routine builds the tables the
199− first time it's called, and returns those tables the first time and
200− thereafter. This reduces the size of the code by about 2K bytes, in
201− exchange for a little execution time. However, BUILDFIXED should not be
202− used for threaded applications, since the rewriting of the tables and virgin
203− may not be thread-safe.
204− */
205−local void fixedtables(state)
206−struct inflate_state FAR *state;
207−{
208−#ifdef BUILDFIXED
209− static int virgin = 1;
210− static code *lenfix, *distfix;
211− static code fixed[544];
212−
213− /* build fixed huffman tables if first call (may not be thread safe) */
214− if (virgin) {
215− unsigned sym, bits;
216− static code *next;
217−
218− /* literal/length table */
219− sym = 0;
220− while (sym < 144) state->lens[sym++] = 8;
221− while (sym < 256) state->lens[sym++] = 9;
222− while (sym < 280) state->lens[sym++] = 7;
223− while (sym < 288) state->lens[sym++] = 8;
224− next = fixed;
225− lenfix = next;
226− bits = 9;
227− inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
228−
229− /* distance table */
230− sym = 0;
231− while (sym < 32) state->lens[sym++] = 5;
232− distfix = next;
233− bits = 5;
234− inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
235−
236− /* do this just once */
237− virgin = 0;
238− }
239−#else /* !BUILDFIXED */
240−# include "inffixed.h"
241−#endif /* BUILDFIXED */
242− state->lencode = lenfix;
243− state->lenbits = 9;
244− state->distcode = distfix;
245− state->distbits = 5;
246−}
247−
248−#ifdef MAKEFIXED
249−#include <stdio.h>
250−
251−/*
252− Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
253− defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
254− those tables to stdout, which would be piped to inffixed.h. A small program
255− can simply call makefixed to do this:
256−
257− void makefixed(void);
258−
259− int main(void)
260− {
261− makefixed();
262− return 0;
263− }
264−
265− Then that can be linked with zlib built with MAKEFIXED defined and run:
266−
267− a.out > inffixed.h
268− */
269−void makefixed()
270−{
271− unsigned low, size;
272− struct inflate_state state;
273−
274− fixedtables(&state);
275− puts(" /* inffixed.h -- table for decoding fixed codes");
276− puts(" * Generated automatically by makefixed().");
277− puts(" */");
278− puts("");
279− puts(" /* WARNING: this file should *not* be used by applications.");
280− puts(" It is part of the implementation of this library and is");
281− puts(" subject to change. Applications should only use zlib.h.");
282− puts(" */");
283− puts("");
284− size = 1U << 9;
285− printf(" static const code lenfix[%u] = {", size);
286− low = 0;
287− for (;;) {
288− if ((low % 7) == 0) printf("\n ");
289− printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
290− state.lencode[low].val);
291− if (++low == size) break;
292− putchar(',');
293− }
294− puts("\n };");
295− size = 1U << 5;
296− printf("\n static const code distfix[%u] = {", size);
297− low = 0;
298− for (;;) {
299− if ((low % 6) == 0) printf("\n ");
300− printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
301− state.distcode[low].val);
302− if (++low == size) break;
303− putchar(',');
304− }
305− puts("\n };");
306−}
307−#endif /* MAKEFIXED */
308−
309−/*
310− Update the window with the last wsize (normally 32K) bytes written before
311− returning. If window does not exist yet, create it. This is only called
312− when a window is already in use, or when output has been written during this
313− inflate call, but the end of the deflate stream has not been reached yet.
314− It is also called to create a window for dictionary data when a dictionary
315− is loaded.
316−
317− Providing output buffers larger than 32K to inflate() should provide a speed
318− advantage, since only the last 32K of output is copied to the sliding window
319− upon return from inflate(), and since all distances after the first 32K of
320− output will fall in the output data, making match copies simpler and faster.
321− The advantage may be dependent on the size of the processor's data caches.
322− */
323−local int updatewindow(strm, out)
324−z_streamp strm;
325−unsigned out;
326−{
327− struct inflate_state FAR *state;
328− unsigned copy, dist;
329−
330− state = (struct inflate_state FAR *)strm->state;
331−
332− /* if it hasn't been done already, allocate space for the window */
333− if (state->window == Z_NULL) {
334− state->window = (unsigned char FAR *)
335− ZALLOC(strm, 1U << state->wbits,
336− sizeof(unsigned char));
337− if (state->window == Z_NULL) return 1;
338− }
339−
340− /* if window not in use yet, initialize */
341− if (state->wsize == 0) {
342− state->wsize = 1U << state->wbits;
343− state->write = 0;
344− state->whave = 0;
345− }
346−
347− /* copy state->wsize or less output bytes into the circular window */
348− copy = out - strm->avail_out;
349− if (copy >= state->wsize) {
350− zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
351− state->write = 0;
352− state->whave = state->wsize;
353− }
354− else {
355− dist = state->wsize - state->write;
356− if (dist > copy) dist = copy;
357− zmemcpy(state->window + state->write, strm->next_out - copy, dist);
358− copy -= dist;
359− if (copy) {
360− zmemcpy(state->window, strm->next_out - copy, copy);
361− state->write = copy;
362− state->whave = state->wsize;
363− }
364− else {
365− state->write += dist;
366− if (state->write == state->wsize) state->write = 0;
367− if (state->whave < state->wsize) state->whave += dist;
368− }
369− }
370− return 0;
371−}
372−
373−/* Macros for inflate(): */
374−
375−/* check function to use adler32() for zlib or crc32() for gzip */
376−#ifdef GUNZIP
377−# define UPDATE(check, buf, len) \
378− (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
379−#else
380−# define UPDATE(check, buf, len) adler32(check, buf, len)
381−#endif
382−
383−/* check macros for header crc */
384−#ifdef GUNZIP
385−# define CRC2(check, word) \
386− do { \
387− hbuf[0] = (unsigned char)(word); \
388− hbuf[1] = (unsigned char)((word) >> 8); \
389− check = crc32(check, hbuf, 2); \
390− } while (0)
391−
392−# define CRC4(check, word) \
393− do { \
394− hbuf[0] = (unsigned char)(word); \
395− hbuf[1] = (unsigned char)((word) >> 8); \
396− hbuf[2] = (unsigned char)((word) >> 16); \
397− hbuf[3] = (unsigned char)((word) >> 24); \
398− check = crc32(check, hbuf, 4); \
399− } while (0)
400−#endif
401−
402−/* Load registers with state in inflate() for speed */
403−#define LOAD() \
404− do { \
405− put = strm->next_out; \
406− left = strm->avail_out; \
407− next = strm->next_in; \
408− have = strm->avail_in; \
409− hold = state->hold; \
410− bits = state->bits; \
411− } while (0)
412−
413−/* Restore state from registers in inflate() */
414−#define RESTORE() \
415− do { \
416− strm->next_out = put; \
417− strm->avail_out = left; \
418− strm->next_in = next; \
419− strm->avail_in = have; \
420− state->hold = hold; \
421− state->bits = bits; \
422− } while (0)
423−
424−/* Clear the input bit accumulator */
425−#define INITBITS() \
426− do { \
427− hold = 0; \
428− bits = 0; \
429− } while (0)
430−
431−/* Get a byte of input into the bit accumulator, or return from inflate()
432− if there is no input available. */
433−#define PULLBYTE() \
434− do { \
435− if (have == 0) goto inf_leave; \
436− have--; \
437− hold += (unsigned long)(*next++) << bits; \
438− bits += 8; \
439− } while (0)
440−
441−/* Assure that there are at least n bits in the bit accumulator. If there is
442− not enough available input to do that, then return from inflate(). */
443−#define NEEDBITS(n) \
444− do { \
445− while (bits < (unsigned)(n)) \
446− PULLBYTE(); \
447− } while (0)
448−
449−/* Return the low n bits of the bit accumulator (n < 16) */
450−#define BITS(n) \
451− ((unsigned)hold & ((1U << (n)) - 1))
452−
453−/* Remove n bits from the bit accumulator */
454−#define DROPBITS(n) \
455− do { \
456− hold >>= (n); \
457− bits -= (unsigned)(n); \
458− } while (0)
459−
460−/* Remove zero to seven bits as needed to go to a byte boundary */
461−#define BYTEBITS() \
462− do { \
463− hold >>= bits & 7; \
464− bits -= bits & 7; \
465− } while (0)
466−
467−/* Reverse the bytes in a 32-bit value */
468−#define REVERSE(q) \
469− ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
470− (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
471−
472−/*
473− inflate() uses a state machine to process as much input data and generate as
474− much output data as possible before returning. The state machine is
475− structured roughly as follows:
476−
477− for (;;) switch (state) {
478− ...
479− case STATEn:
480− if (not enough input data or output space to make progress)
481− return;
482− ... make progress ...
483− state = STATEm;
484− break;
485− ...
486− }
487−
488− so when inflate() is called again, the same case is attempted again, and
489− if the appropriate resources are provided, the machine proceeds to the
490− next state. The NEEDBITS() macro is usually the way the state evaluates
491− whether it can proceed or should return. NEEDBITS() does the return if
492− the requested bits are not available. The typical use of the BITS macros
493− is:
494−
495− NEEDBITS(n);
496− ... do something with BITS(n) ...
497− DROPBITS(n);
498−
499− where NEEDBITS(n) either returns from inflate() if there isn't enough
500− input left to load n bits into the accumulator, or it continues. BITS(n)
501− gives the low n bits in the accumulator. When done, DROPBITS(n) drops
502− the low n bits off the accumulator. INITBITS() clears the accumulator
503− and sets the number of available bits to zero. BYTEBITS() discards just
504− enough bits to put the accumulator on a byte boundary. After BYTEBITS()
505− and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
506−
507− NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
508− if there is no input available. The decoding of variable length codes uses
509− PULLBYTE() directly in order to pull just enough bytes to decode the next
510− code, and no more.
511−
512− Some states loop until they get enough input, making sure that enough
513− state information is maintained to continue the loop where it left off
514− if NEEDBITS() returns in the loop. For example, want, need, and keep
515− would all have to actually be part of the saved state in case NEEDBITS()
516− returns:
517−
518− case STATEw:
519− while (want < need) {
520− NEEDBITS(n);
521− keep[want++] = BITS(n);
522− DROPBITS(n);
523− }
524− state = STATEx;
525− case STATEx:
526−
527− As shown above, if the next state is also the next case, then the break
528− is omitted.
529−
530− A state may also return if there is not enough output space available to
531− complete that state. Those states are copying stored data, writing a
532− literal byte, and copying a matching string.
533−
534− When returning, a "goto inf_leave" is used to update the total counters,
535− update the check value, and determine whether any progress has been made
536− during that inflate() call in order to return the proper return code.
537− Progress is defined as a change in either strm->avail_in or strm->avail_out.
538− When there is a window, goto inf_leave will update the window with the last
539− output written. If a goto inf_leave occurs in the middle of decompression
540− and there is no window currently, goto inf_leave will create one and copy
541− output to the window for the next call of inflate().
542−
543− In this implementation, the flush parameter of inflate() only affects the
544− return code (per zlib.h). inflate() always writes as much as possible to
545− strm->next_out, given the space available and the provided input--the effect
546− documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
547− the allocation of and copying into a sliding window until necessary, which
548− provides the effect documented in zlib.h for Z_FINISH when the entire input
549− stream available. So the only thing the flush parameter actually does is:
550− when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
551− will return Z_BUF_ERROR if it has not reached the end of the stream.
552− */
553−
554−int ZEXPORT inflate(strm, flush)
555−z_streamp strm;
556−int flush;
557−{
558− struct inflate_state FAR *state;
559− unsigned char FAR *next; /* next input */
560− unsigned char FAR *put; /* next output */
561− unsigned have, left; /* available input and output */
562− unsigned long hold; /* bit buffer */
563− unsigned bits; /* bits in bit buffer */
564− unsigned in, out; /* save starting available input and output */
565− unsigned copy; /* number of stored or match bytes to copy */
566− unsigned char FAR *from; /* where to copy match bytes from */
567− code this; /* current decoding table entry */
568− code last; /* parent table entry */
569− unsigned len; /* length to copy for repeats, bits to drop */
570− int ret; /* return code */
571−#ifdef GUNZIP
572− unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
573−#endif
574− static const unsigned short order[19] = /* permutation of code lengths */
575− {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
576−
577− if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
578− (strm->next_in == Z_NULL && strm->avail_in != 0))
579− return Z_STREAM_ERROR;
580−
581− state = (struct inflate_state FAR *)strm->state;
582− if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
583− LOAD();
584− in = have;
585− out = left;
586− ret = Z_OK;
587− for (;;)
588− switch (state->mode) {
589− case HEAD:
590− if (state->wrap == 0) {
591− state->mode = TYPEDO;
592− break;
593− }
594− NEEDBITS(16);
595−#ifdef GUNZIP
596− if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
597− state->check = crc32(0L, Z_NULL, 0);
598− CRC2(state->check, hold);
599− INITBITS();
600− state->mode = FLAGS;
601− break;
602− }
603− state->flags = 0; /* expect zlib header */
604− if (state->head != Z_NULL)
605− state->head->done = -1;
606− if (!(state->wrap & 1) || /* check if zlib header allowed */
607−#else
608− if (
609−#endif
610− ((BITS(8) << 8) + (hold >> 8)) % 31) {
611− strm->msg = (char *)"incorrect header check";
612− state->mode = BAD;
613− break;
614− }
615− if (BITS(4) != Z_DEFLATED) {
616− strm->msg = (char *)"unknown compression method";
617− state->mode = BAD;
618− break;
619− }
620− DROPBITS(4);
621− len = BITS(4) + 8;
622− if (len > state->wbits) {
623− strm->msg = (char *)"invalid window size";
624− state->mode = BAD;
625− break;
626− }
627− state->dmax = 1U << len;
628− Tracev((stderr, "inflate: zlib header ok\n"));
629− strm->adler = state->check = adler32(0L, Z_NULL, 0);
630− state->mode = hold & 0x200 ? DICTID : TYPE;
631− INITBITS();
632− break;
633−#ifdef GUNZIP
634− case FLAGS:
635− NEEDBITS(16);
636− state->flags = (int)(hold);
637− if ((state->flags & 0xff) != Z_DEFLATED) {
638− strm->msg = (char *)"unknown compression method";
639− state->mode = BAD;
640− break;
641− }
642− if (state->flags & 0xe000) {
643− strm->msg = (char *)"unknown header flags set";
644− state->mode = BAD;
645− break;
646− }
647− if (state->head != Z_NULL)
648− state->head->text = (int)((hold >> 8) & 1);
649− if (state->flags & 0x0200) CRC2(state->check, hold);
650− INITBITS();
651− state->mode = TIME;
652− case TIME:
653− NEEDBITS(32);
654− if (state->head != Z_NULL)
655− state->head->time = hold;
656− if (state->flags & 0x0200) CRC4(state->check, hold);
657− INITBITS();
658− state->mode = OS;
659− case OS:
660− NEEDBITS(16);
661− if (state->head != Z_NULL) {
662− state->head->xflags = (int)(hold & 0xff);
663− state->head->os = (int)(hold >> 8);
664− }
665− if (state->flags & 0x0200) CRC2(state->check, hold);
666− INITBITS();
667− state->mode = EXLEN;
668− case EXLEN:
669− if (state->flags & 0x0400) {
670− NEEDBITS(16);
671− state->length = (unsigned)(hold);
672− if (state->head != Z_NULL)
673− state->head->extra_len = (unsigned)hold;
674− if (state->flags & 0x0200) CRC2(state->check, hold);
675− INITBITS();
676− }
677− else if (state->head != Z_NULL)
678− state->head->extra = Z_NULL;
679− state->mode = EXTRA;
680− case EXTRA:
681− if (state->flags & 0x0400) {
682− copy = state->length;
683− if (copy > have) copy = have;
684− if (copy) {
685− if (state->head != Z_NULL &&
686− state->head->extra != Z_NULL) {
687− len = state->head->extra_len - state->length;
688− zmemcpy(state->head->extra + len, next,
689− len + copy > state->head->extra_max ?
690− state->head->extra_max - len : copy);
691− }
692− if (state->flags & 0x0200)
693− state->check = crc32(state->check, next, copy);
694− have -= copy;
695− next += copy;
696− state->length -= copy;
697− }
698− if (state->length) goto inf_leave;
699− }
700− state->length = 0;
701− state->mode = NAME;
702− case NAME:
703− if (state->flags & 0x0800) {
704− if (have == 0) goto inf_leave;
705− copy = 0;
706− do {
707− len = (unsigned)(next[copy++]);
708− if (state->head != Z_NULL &&
709− state->head->name != Z_NULL &&
710− state->length < state->head->name_max)
711− state->head->name[state->length++] = len;
712− } while (len && copy < have);
713− if (state->flags & 0x0200)
714− state->check = crc32(state->check, next, copy);
715− have -= copy;
716− next += copy;
717− if (len) goto inf_leave;
718− }
719− else if (state->head != Z_NULL)
720− state->head->name = Z_NULL;
721− state->length = 0;
722− state->mode = COMMENT;
723− case COMMENT:
724− if (state->flags & 0x1000) {
725− if (have == 0) goto inf_leave;
726− copy = 0;
727− do {
728− len = (unsigned)(next[copy++]);
729− if (state->head != Z_NULL &&
730− state->head->comment != Z_NULL &&
731− state->length < state->head->comm_max)
732− state->head->comment[state->length++] = len;
733− } while (len && copy < have);
734− if (state->flags & 0x0200)
735− state->check = crc32(state->check, next, copy);
736− have -= copy;
737− next += copy;
738− if (len) goto inf_leave;
739− }
740− else if (state->head != Z_NULL)
741− state->head->comment = Z_NULL;
742− state->mode = HCRC;
743− case HCRC:
744− if (state->flags & 0x0200) {
745− NEEDBITS(16);
746− if (hold != (state->check & 0xffff)) {
747− strm->msg = (char *)"header crc mismatch";
748− state->mode = BAD;
749− break;
750− }
751− INITBITS();
752− }
753− if (state->head != Z_NULL) {
754− state->head->hcrc = (int)((state->flags >> 9) & 1);
755− state->head->done = 1;
756− }
757− strm->adler = state->check = crc32(0L, Z_NULL, 0);
758− state->mode = TYPE;
759− break;
760−#endif
761− case DICTID:
762− NEEDBITS(32);
763− strm->adler = state->check = REVERSE(hold);
764− INITBITS();
765− state->mode = DICT;
766− case DICT:
767− if (state->havedict == 0) {
768− RESTORE();
769− return Z_NEED_DICT;
770− }
771− strm->adler = state->check = adler32(0L, Z_NULL, 0);
772− state->mode = TYPE;
773− case TYPE:
774− if (flush == Z_BLOCK) goto inf_leave;
775− case TYPEDO:
776− if (state->last) {
777− BYTEBITS();
778− state->mode = CHECK;
779− break;
780− }
781− NEEDBITS(3);
782− state->last = BITS(1);
783− DROPBITS(1);
784− switch (BITS(2)) {
785− case 0: /* stored block */
786− Tracev((stderr, "inflate: stored block%s\n",
787− state->last ? " (last)" : ""));
788− state->mode = STORED;
789− break;
790− case 1: /* fixed block */
791− fixedtables(state);
792− Tracev((stderr, "inflate: fixed codes block%s\n",
793− state->last ? " (last)" : ""));
794− state->mode = LEN; /* decode codes */
795− break;
796− case 2: /* dynamic block */
797− Tracev((stderr, "inflate: dynamic codes block%s\n",
798− state->last ? " (last)" : ""));
799− state->mode = TABLE;
800− break;
801− case 3:
802− strm->msg = (char *)"invalid block type";
803− state->mode = BAD;
804− }
805− DROPBITS(2);
806− break;
807− case STORED:
808− BYTEBITS(); /* go to byte boundary */
809− NEEDBITS(32);
810− if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
811− strm->msg = (char *)"invalid stored block lengths";
812− state->mode = BAD;
813− break;
814− }
815− state->length = (unsigned)hold & 0xffff;
816− Tracev((stderr, "inflate: stored length %u\n",
817− state->length));
818− INITBITS();
819− state->mode = COPY;
820− case COPY:
821− copy = state->length;
822− if (copy) {
823− if (copy > have) copy = have;
824− if (copy > left) copy = left;
825− if (copy == 0) goto inf_leave;
826− zmemcpy(put, next, copy);
827− have -= copy;
828− next += copy;
829− left -= copy;
830− put += copy;
831− state->length -= copy;
832− break;
833− }
834− Tracev((stderr, "inflate: stored end\n"));
835− state->mode = TYPE;
836− break;
837− case TABLE:
838− NEEDBITS(14);
839− state->nlen = BITS(5) + 257;
840− DROPBITS(5);
841− state->ndist = BITS(5) + 1;
842− DROPBITS(5);
843− state->ncode = BITS(4) + 4;
844− DROPBITS(4);
845−#ifndef PKZIP_BUG_WORKAROUND
846− if (state->nlen > 286 || state->ndist > 30) {
847− strm->msg = (char *)"too many length or distance symbols";
848− state->mode = BAD;
849− break;
850− }
851−#endif
852− Tracev((stderr, "inflate: table sizes ok\n"));
853− state->have = 0;
854− state->mode = LENLENS;
855− case LENLENS:
856− while (state->have < state->ncode) {
857− NEEDBITS(3);
858− state->lens[order[state->have++]] = (unsigned short)BITS(3);
859− DROPBITS(3);
860− }
861− while (state->have < 19)
862− state->lens[order[state->have++]] = 0;
863− state->next = state->codes;
864− state->lencode = (code const FAR *)(state->next);
865− state->lenbits = 7;
866− ret = inflate_table(CODES, state->lens, 19, &(state->next),
867− &(state->lenbits), state->work);
868− if (ret) {
869− strm->msg = (char *)"invalid code lengths set";
870− state->mode = BAD;
871− break;
872− }
873− Tracev((stderr, "inflate: code lengths ok\n"));
874− state->have = 0;
875− state->mode = CODELENS;
876− case CODELENS:
877− while (state->have < state->nlen + state->ndist) {
878− for (;;) {
879− this = state->lencode[BITS(state->lenbits)];
880− if ((unsigned)(this.bits) <= bits) break;
881− PULLBYTE();
882− }
883− if (this.val < 16) {
884− NEEDBITS(this.bits);
885− DROPBITS(this.bits);
886− state->lens[state->have++] = this.val;
887− }
888− else {
889− if (this.val == 16) {
890− NEEDBITS(this.bits + 2);
891− DROPBITS(this.bits);
892− if (state->have == 0) {
893− strm->msg = (char *)"invalid bit length repeat";
894− state->mode = BAD;
895− break;
896− }
897− len = state->lens[state->have - 1];
898− copy = 3 + BITS(2);
899− DROPBITS(2);
900− }
901− else if (this.val == 17) {
902− NEEDBITS(this.bits + 3);
903− DROPBITS(this.bits);
904− len = 0;
905− copy = 3 + BITS(3);
906− DROPBITS(3);
907− }
908− else {
909− NEEDBITS(this.bits + 7);
910− DROPBITS(this.bits);
911− len = 0;
912− copy = 11 + BITS(7);
913− DROPBITS(7);
914− }
915− if (state->have + copy > state->nlen + state->ndist) {
916− strm->msg = (char *)"invalid bit length repeat";
917− state->mode = BAD;
918− break;
919− }
920− while (copy--)
921− state->lens[state->have++] = (unsigned short)len;
922− }
923− }
924−
925− /* handle error breaks in while */
926− if (state->mode == BAD) break;
927−
928− /* build code tables */
929− state->next = state->codes;
930− state->lencode = (code const FAR *)(state->next);
931− state->lenbits = 9;
932− ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
933− &(state->lenbits), state->work);
934− if (ret) {
935− strm->msg = (char *)"invalid literal/lengths set";
936− state->mode = BAD;
937− break;
938− }
939− state->distcode = (code const FAR *)(state->next);
940− state->distbits = 6;
941− ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
942− &(state->next), &(state->distbits), state->work);
943− if (ret) {
944− strm->msg = (char *)"invalid distances set";
945− state->mode = BAD;
946− break;
947− }
948− Tracev((stderr, "inflate: codes ok\n"));
949− state->mode = LEN;
950− case LEN:
951− if (have >= 6 && left >= 258) {
952− RESTORE();
953− inflate_fast(strm, out);
954− LOAD();
955− break;
956− }
957− for (;;) {
958− this = state->lencode[BITS(state->lenbits)];
959− if ((unsigned)(this.bits) <= bits) break;
960− PULLBYTE();
961− }
962− if (this.op && (this.op & 0xf0) == 0) {
963− last = this;
964− for (;;) {
965− this = state->lencode[last.val +
966− (BITS(last.bits + last.op) >> last.bits)];
967− if ((unsigned)(last.bits + this.bits) <= bits) break;
968− PULLBYTE();
969− }
970− DROPBITS(last.bits);
971− }
972− DROPBITS(this.bits);
973− state->length = (unsigned)this.val;
974− if ((int)(this.op) == 0) {
975− Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
976− "inflate: literal '%c'\n" :
977− "inflate: literal 0x%02x\n", this.val));
978− state->mode = LIT;
979− break;
980− }
981− if (this.op & 32) {
982− Tracevv((stderr, "inflate: end of block\n"));
983− state->mode = TYPE;
984− break;
985− }
986− if (this.op & 64) {
987− strm->msg = (char *)"invalid literal/length code";
988− state->mode = BAD;
989− break;
990− }
991− state->extra = (unsigned)(this.op) & 15;
992− state->mode = LENEXT;
993− case LENEXT:
994− if (state->extra) {
995− NEEDBITS(state->extra);
996− state->length += BITS(state->extra);
997− DROPBITS(state->extra);
998− }
999− Tracevv((stderr, "inflate: length %u\n", state->length));
1000− state->mode = DIST;
1001− case DIST:
1002− for (;;) {
1003− this = state->distcode[BITS(state->distbits)];
1004− if ((unsigned)(this.bits) <= bits) break;
1005− PULLBYTE();
1006− }
1007− if ((this.op & 0xf0) == 0) {
1008− last = this;
1009− for (;;) {
1010− this = state->distcode[last.val +
1011− (BITS(last.bits + last.op) >> last.bits)];
1012− if ((unsigned)(last.bits + this.bits) <= bits) break;
1013− PULLBYTE();
1014− }
1015− DROPBITS(last.bits);
1016− }
1017− DROPBITS(this.bits);
1018− if (this.op & 64) {
1019− strm->msg = (char *)"invalid distance code";
1020− state->mode = BAD;
1021− break;
1022− }
1023− state->offset = (unsigned)this.val;
1024− state->extra = (unsigned)(this.op) & 15;
1025− state->mode = DISTEXT;
1026− case DISTEXT:
1027− if (state->extra) {
1028− NEEDBITS(state->extra);
1029− state->offset += BITS(state->extra);
1030− DROPBITS(state->extra);
1031− }
1032−#ifdef INFLATE_STRICT
1033− if (state->offset > state->dmax) {
1034− strm->msg = (char *)"invalid distance too far back";
1035− state->mode = BAD;
1036− break;
1037− }
1038−#endif
1039− if (state->offset > state->whave + out - left) {
1040− strm->msg = (char *)"invalid distance too far back";
1041− state->mode = BAD;
1042− break;
1043− }
1044− Tracevv((stderr, "inflate: distance %u\n", state->offset));
1045− state->mode = MATCH;
1046− case MATCH:
1047− if (left == 0) goto inf_leave;
1048− copy = out - left;
1049− if (state->offset > copy) { /* copy from window */
1050− copy = state->offset - copy;
1051− if (copy > state->write) {
1052− copy -= state->write;
1053− from = state->window + (state->wsize - copy);
1054− }
1055− else
1056− from = state->window + (state->write - copy);
1057− if (copy > state->length) copy = state->length;
1058− }
1059− else { /* copy from output */
1060− from = put - state->offset;
1061− copy = state->length;
1062− }
1063− if (copy > left) copy = left;
1064− left -= copy;
1065− state->length -= copy;
1066− do {
1067− *put++ = *from++;
1068− } while (--copy);
1069− if (state->length == 0) state->mode = LEN;
1070− break;
1071− case LIT:
1072− if (left == 0) goto inf_leave;
1073− *put++ = (unsigned char)(state->length);
1074− left--;
1075− state->mode = LEN;
1076− break;
1077− case CHECK:
1078− if (state->wrap) {
1079− NEEDBITS(32);
1080− out -= left;
1081− strm->total_out += out;
1082− state->total += out;
1083− if (out)
1084− strm->adler = state->check =
1085− UPDATE(state->check, put - out, out);
1086− out = left;
1087− if ((
1088−#ifdef GUNZIP
1089− state->flags ? hold :
1090−#endif
1091− REVERSE(hold)) != state->check) {
1092− strm->msg = (char *)"incorrect data check";
1093− state->mode = BAD;
1094− break;
1095− }
1096− INITBITS();
1097− Tracev((stderr, "inflate: check matches trailer\n"));
1098− }
1099−#ifdef GUNZIP
1100− state->mode = LENGTH;
1101− case LENGTH:
1102− if (state->wrap && state->flags) {
1103− NEEDBITS(32);
1104− if (hold != (state->total & 0xffffffffUL)) {
1105− strm->msg = (char *)"incorrect length check";
1106− state->mode = BAD;
1107− break;
1108− }
1109− INITBITS();
1110− Tracev((stderr, "inflate: length matches trailer\n"));
1111− }
1112−#endif
1113− state->mode = DONE;
1114− case DONE:
1115− ret = Z_STREAM_END;
1116− goto inf_leave;
1117− case BAD:
1118− ret = Z_DATA_ERROR;
1119− goto inf_leave;
1120− case MEM:
1121− return Z_MEM_ERROR;
1122− case SYNC:
1123− default:
1124− return Z_STREAM_ERROR;
1125− }
1126−
1127− /*
1128− Return from inflate(), updating the total counts and the check value.
1129− If there was no progress during the inflate() call, return a buffer
1130− error. Call updatewindow() to create and/or update the window state.
1131− Note: a memory error from inflate() is non-recoverable.
1132− */
1133− inf_leave:
1134− RESTORE();
1135− if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1136− if (updatewindow(strm, out)) {
1137− state->mode = MEM;
1138− return Z_MEM_ERROR;
1139− }
1140− in -= strm->avail_in;
1141− out -= strm->avail_out;
1142− strm->total_in += in;
1143− strm->total_out += out;
1144− state->total += out;
1145− if (state->wrap && out)
1146− strm->adler = state->check =
1147− UPDATE(state->check, strm->next_out - out, out);
1148− strm->data_type = state->bits + (state->last ? 64 : 0) +
1149− (state->mode == TYPE ? 128 : 0);
1150− if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1151− ret = Z_BUF_ERROR;
1152− return ret;
1153−}
1154−
1155−int ZEXPORT inflateEnd(strm)
1156−z_streamp strm;
1157−{
1158− struct inflate_state FAR *state;
1159− if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
1160− return Z_STREAM_ERROR;
1161− state = (struct inflate_state FAR *)strm->state;
1162− if (state->window != Z_NULL) ZFREE(strm, state->window);
1163− ZFREE(strm, strm->state);
1164− strm->state = Z_NULL;
1165− Tracev((stderr, "inflate: end\n"));
1166− return Z_OK;
1167−}
1168−
1169−int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1170−z_streamp strm;
1171−const Bytef *dictionary;
1172−uInt dictLength;
1173−{
1174− struct inflate_state FAR *state;
1175− unsigned long id;
1176−
1177− /* check state */
1178− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1179− state = (struct inflate_state FAR *)strm->state;
1180− if (state->wrap != 0 && state->mode != DICT)
1181− return Z_STREAM_ERROR;
1182−
1183− /* check for correct dictionary id */
1184− if (state->mode == DICT) {
1185− id = adler32(0L, Z_NULL, 0);
1186− id = adler32(id, dictionary, dictLength);
1187− if (id != state->check)
1188− return Z_DATA_ERROR;
1189− }
1190−
1191− /* copy dictionary to window */
1192− if (updatewindow(strm, strm->avail_out)) {
1193− state->mode = MEM;
1194− return Z_MEM_ERROR;
1195− }
1196− if (dictLength > state->wsize) {
1197− zmemcpy(state->window, dictionary + dictLength - state->wsize,
1198− state->wsize);
1199− state->whave = state->wsize;
1200− }
1201− else {
1202− zmemcpy(state->window + state->wsize - dictLength, dictionary,
1203− dictLength);
1204− state->whave = dictLength;
1205− }
1206− state->havedict = 1;
1207− Tracev((stderr, "inflate: dictionary set\n"));
1208− return Z_OK;
1209−}
1210−
1211−int ZEXPORT inflateGetHeader(strm, head)
1212−z_streamp strm;
1213−gz_headerp head;
1214−{
1215− struct inflate_state FAR *state;
1216−
1217− /* check state */
1218− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1219− state = (struct inflate_state FAR *)strm->state;
1220− if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1221−
1222− /* save header structure */
1223− state->head = head;
1224− head->done = 0;
1225− return Z_OK;
1226−}
1227−
1228−/*
1229− Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1230− or when out of input. When called, *have is the number of pattern bytes
1231− found in order so far, in 0..3. On return *have is updated to the new
1232− state. If on return *have equals four, then the pattern was found and the
1233− return value is how many bytes were read including the last byte of the
1234− pattern. If *have is less than four, then the pattern has not been found
1235− yet and the return value is len. In the latter case, syncsearch() can be
1236− called again with more data and the *have state. *have is initialized to
1237− zero for the first call.
1238− */
1239−local unsigned syncsearch(have, buf, len)
1240−unsigned FAR *have;
1241−unsigned char FAR *buf;
1242−unsigned len;
1243−{
1244− unsigned got;
1245− unsigned next;
1246−
1247− got = *have;
1248− next = 0;
1249− while (next < len && got < 4) {
1250− if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1251− got++;
1252− else if (buf[next])
1253− got = 0;
1254− else
1255− got = 4 - got;
1256− next++;
1257− }
1258− *have = got;
1259− return next;
1260−}
1261−
1262−int ZEXPORT inflateSync(strm)
1263−z_streamp strm;
1264−{
1265− unsigned len; /* number of bytes to look at or looked at */
1266− unsigned long in, out; /* temporary to save total_in and total_out */
1267− unsigned char buf[4]; /* to restore bit buffer to byte string */
1268− struct inflate_state FAR *state;
1269−
1270− /* check parameters */
1271− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1272− state = (struct inflate_state FAR *)strm->state;
1273− if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1274−
1275− /* if first time, start search in bit buffer */
1276− if (state->mode != SYNC) {
1277− state->mode = SYNC;
1278− state->hold <<= state->bits & 7;
1279− state->bits -= state->bits & 7;
1280− len = 0;
1281− while (state->bits >= 8) {
1282− buf[len++] = (unsigned char)(state->hold);
1283− state->hold >>= 8;
1284− state->bits -= 8;
1285− }
1286− state->have = 0;
1287− syncsearch(&(state->have), buf, len);
1288− }
1289−
1290− /* search available input */
1291− len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1292− strm->avail_in -= len;
1293− strm->next_in += len;
1294− strm->total_in += len;
1295−
1296− /* return no joy or set up to restart inflate() on a new block */
1297− if (state->have != 4) return Z_DATA_ERROR;
1298− in = strm->total_in; out = strm->total_out;
1299− inflateReset(strm);
1300− strm->total_in = in; strm->total_out = out;
1301− state->mode = TYPE;
1302− return Z_OK;
1303−}
1304−
1305−/*
1306− Returns true if inflate is currently at the end of a block generated by
1307− Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1308− implementation to provide an additional safety check. PPP uses
1309− Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1310− block. When decompressing, PPP checks that at the end of input packet,
1311− inflate is waiting for these length bytes.
1312− */
1313−int ZEXPORT inflateSyncPoint(strm)
1314−z_streamp strm;
1315−{
1316− struct inflate_state FAR *state;
1317−
1318− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1319− state = (struct inflate_state FAR *)strm->state;
1320− return state->mode == STORED && state->bits == 0;
1321−}
1322−
1323−int ZEXPORT inflateCopy(dest, source)
1324−z_streamp dest;
1325−z_streamp source;
1326−{
1327− struct inflate_state FAR *state;
1328− struct inflate_state FAR *copy;
1329− unsigned char FAR *window;
1330− unsigned wsize;
1331−
1332− /* check input */
1333− if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
1334− source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
1335− return Z_STREAM_ERROR;
1336− state = (struct inflate_state FAR *)source->state;
1337−
1338− /* allocate space */
1339− copy = (struct inflate_state FAR *)
1340− ZALLOC(source, 1, sizeof(struct inflate_state));
1341− if (copy == Z_NULL) return Z_MEM_ERROR;
1342− window = Z_NULL;
1343− if (state->window != Z_NULL) {
1344− window = (unsigned char FAR *)
1345− ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1346− if (window == Z_NULL) {
1347− ZFREE(source, copy);
1348− return Z_MEM_ERROR;
1349− }
1350− }
1351−
1352− /* copy state */
1353− zmemcpy(dest, source, sizeof(z_stream));
1354− zmemcpy(copy, state, sizeof(struct inflate_state));
1355− if (state->lencode >= state->codes &&
1356− state->lencode <= state->codes + ENOUGH - 1) {
1357− copy->lencode = copy->codes + (state->lencode - state->codes);
1358− copy->distcode = copy->codes + (state->distcode - state->codes);
1359− }
1360− copy->next = copy->codes + (state->next - state->codes);
1361− if (window != Z_NULL) {
1362− wsize = 1U << state->wbits;
1363− zmemcpy(window, state->window, wsize);
1364− }
1365− copy->window = window;
1366− dest->state = (struct internal_state FAR *)copy;
1367− return Z_OK;
1368−}
cbits/inflate.hdeleted−115
@@ -1,115 +0,0 @@
1−/* inflate.h -- internal inflate state definition
2− * Copyright (C) 1995-2004 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* define NO_GZIP when compiling if you want to disable gzip header and
12− trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
13− the crc code when it is not needed. For shared libraries, gzip decoding
14− should be left enabled. */
15−#ifndef NO_GZIP
16−# define GUNZIP
17−#endif
18−
19−/* Possible inflate modes between inflate() calls */
20−typedef enum {
21− HEAD, /* i: waiting for magic header */
22− FLAGS, /* i: waiting for method and flags (gzip) */
23− TIME, /* i: waiting for modification time (gzip) */
24− OS, /* i: waiting for extra flags and operating system (gzip) */
25− EXLEN, /* i: waiting for extra length (gzip) */
26− EXTRA, /* i: waiting for extra bytes (gzip) */
27− NAME, /* i: waiting for end of file name (gzip) */
28− COMMENT, /* i: waiting for end of comment (gzip) */
29− HCRC, /* i: waiting for header crc (gzip) */
30− DICTID, /* i: waiting for dictionary check value */
31− DICT, /* waiting for inflateSetDictionary() call */
32− TYPE, /* i: waiting for type bits, including last-flag bit */
33− TYPEDO, /* i: same, but skip check to exit inflate on new block */
34− STORED, /* i: waiting for stored size (length and complement) */
35− COPY, /* i/o: waiting for input or output to copy stored block */
36− TABLE, /* i: waiting for dynamic block table lengths */
37− LENLENS, /* i: waiting for code length code lengths */
38− CODELENS, /* i: waiting for length/lit and distance code lengths */
39− LEN, /* i: waiting for length/lit code */
40− LENEXT, /* i: waiting for length extra bits */
41− DIST, /* i: waiting for distance code */
42− DISTEXT, /* i: waiting for distance extra bits */
43− MATCH, /* o: waiting for output space to copy string */
44− LIT, /* o: waiting for output space to write literal */
45− CHECK, /* i: waiting for 32-bit check value */
46− LENGTH, /* i: waiting for 32-bit length (gzip) */
47− DONE, /* finished check, done -- remain here until reset */
48− BAD, /* got a data error -- remain here until reset */
49− MEM, /* got an inflate() memory error -- remain here until reset */
50− SYNC /* looking for synchronization bytes to restart inflate() */
51−} inflate_mode;
52−
53−/*
54− State transitions between above modes -
55−
56− (most modes can go to the BAD or MEM mode -- not shown for clarity)
57−
58− Process header:
59− HEAD -> (gzip) or (zlib)
60− (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
61− NAME -> COMMENT -> HCRC -> TYPE
62− (zlib) -> DICTID or TYPE
63− DICTID -> DICT -> TYPE
64− Read deflate blocks:
65− TYPE -> STORED or TABLE or LEN or CHECK
66− STORED -> COPY -> TYPE
67− TABLE -> LENLENS -> CODELENS -> LEN
68− Read deflate codes:
69− LEN -> LENEXT or LIT or TYPE
70− LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
71− LIT -> LEN
72− Process trailer:
73− CHECK -> LENGTH -> DONE
74− */
75−
76−/* state maintained between inflate() calls. Approximately 7K bytes. */
77−struct inflate_state {
78− inflate_mode mode; /* current inflate mode */
79− int last; /* true if processing last block */
80− int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
81− int havedict; /* true if dictionary provided */
82− int flags; /* gzip header method and flags (0 if zlib) */
83− unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
84− unsigned long check; /* protected copy of check value */
85− unsigned long total; /* protected copy of output count */
86− gz_headerp head; /* where to save gzip header information */
87− /* sliding window */
88− unsigned wbits; /* log base 2 of requested window size */
89− unsigned wsize; /* window size or zero if not using window */
90− unsigned whave; /* valid bytes in the window */
91− unsigned write; /* window write index */
92− unsigned char FAR *window; /* allocated sliding window, if needed */
93− /* bit accumulator */
94− unsigned long hold; /* input bit accumulator */
95− unsigned bits; /* number of bits in "in" */
96− /* for string and stored block copying */
97− unsigned length; /* literal or length of data to copy */
98− unsigned offset; /* distance back to copy string from */
99− /* for table and code decoding */
100− unsigned extra; /* extra bits needed */
101− /* fixed and dynamic code tables */
102− code const FAR *lencode; /* starting table for length/literal codes */
103− code const FAR *distcode; /* starting table for distance codes */
104− unsigned lenbits; /* index bits for lencode */
105− unsigned distbits; /* index bits for distcode */
106− /* dynamic table building */
107− unsigned ncode; /* number of code length code lengths */
108− unsigned nlen; /* number of length code lengths */
109− unsigned ndist; /* number of distance code lengths */
110− unsigned have; /* number of code lengths in lens[] */
111− code FAR *next; /* next available space in codes[] */
112− unsigned short lens[320]; /* temporary storage for code lengths */
113− unsigned short work[288]; /* work area for code table building */
114− code codes[ENOUGH]; /* space for code tables */
115−};
cbits/inftrees.cdeleted−329
@@ -1,329 +0,0 @@
1−/* inftrees.c -- generate Huffman trees for efficient decoding
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−#include "zutil.h"
7−#include "inftrees.h"
8−
9−#define MAXBITS 15
10−
11−const char inflate_copyright[] =
12− " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
13−/*
14− If you use the zlib library in a product, an acknowledgment is welcome
15− in the documentation of your product. If for some reason you cannot
16− include such an acknowledgment, I would appreciate that you keep this
17− copyright string in the executable of your product.
18− */
19−
20−/*
21− Build a set of tables to decode the provided canonical Huffman code.
22− The code lengths are lens[0..codes-1]. The result starts at *table,
23− whose indices are 0..2^bits-1. work is a writable array of at least
24− lens shorts, which is used as a work area. type is the type of code
25− to be generated, CODES, LENS, or DISTS. On return, zero is success,
26− -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
27− on return points to the next available entry's address. bits is the
28− requested root table index bits, and on return it is the actual root
29− table index bits. It will differ if the request is greater than the
30− longest code or if it is less than the shortest code.
31− */
32−int inflate_table(type, lens, codes, table, bits, work)
33−codetype type;
34−unsigned short FAR *lens;
35−unsigned codes;
36−code FAR * FAR *table;
37−unsigned FAR *bits;
38−unsigned short FAR *work;
39−{
40− unsigned len; /* a code's length in bits */
41− unsigned sym; /* index of code symbols */
42− unsigned min, max; /* minimum and maximum code lengths */
43− unsigned root; /* number of index bits for root table */
44− unsigned curr; /* number of index bits for current table */
45− unsigned drop; /* code bits to drop for sub-table */
46− int left; /* number of prefix codes available */
47− unsigned used; /* code entries in table used */
48− unsigned huff; /* Huffman code */
49− unsigned incr; /* for incrementing code, index */
50− unsigned fill; /* index for replicating entries */
51− unsigned low; /* low bits for current root entry */
52− unsigned mask; /* mask for low root bits */
53− code this; /* table entry for duplication */
54− code FAR *next; /* next available space in table */
55− const unsigned short FAR *base; /* base value table to use */
56− const unsigned short FAR *extra; /* extra bits table to use */
57− int end; /* use base and extra for symbol > end */
58− unsigned short count[MAXBITS+1]; /* number of codes of each length */
59− unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
60− static const unsigned short lbase[31] = { /* Length codes 257..285 base */
61− 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
62− 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
63− static const unsigned short lext[31] = { /* Length codes 257..285 extra */
64− 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
65− 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
66− static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
67− 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
68− 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
69− 8193, 12289, 16385, 24577, 0, 0};
70− static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
71− 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
72− 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
73− 28, 28, 29, 29, 64, 64};
74−
75− /*
76− Process a set of code lengths to create a canonical Huffman code. The
77− code lengths are lens[0..codes-1]. Each length corresponds to the
78− symbols 0..codes-1. The Huffman code is generated by first sorting the
79− symbols by length from short to long, and retaining the symbol order
80− for codes with equal lengths. Then the code starts with all zero bits
81− for the first code of the shortest length, and the codes are integer
82− increments for the same length, and zeros are appended as the length
83− increases. For the deflate format, these bits are stored backwards
84− from their more natural integer increment ordering, and so when the
85− decoding tables are built in the large loop below, the integer codes
86− are incremented backwards.
87−
88− This routine assumes, but does not check, that all of the entries in
89− lens[] are in the range 0..MAXBITS. The caller must assure this.
90− 1..MAXBITS is interpreted as that code length. zero means that that
91− symbol does not occur in this code.
92−
93− The codes are sorted by computing a count of codes for each length,
94− creating from that a table of starting indices for each length in the
95− sorted table, and then entering the symbols in order in the sorted
96− table. The sorted table is work[], with that space being provided by
97− the caller.
98−
99− The length counts are used for other purposes as well, i.e. finding
100− the minimum and maximum length codes, determining if there are any
101− codes at all, checking for a valid set of lengths, and looking ahead
102− at length counts to determine sub-table sizes when building the
103− decoding tables.
104− */
105−
106− /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
107− for (len = 0; len <= MAXBITS; len++)
108− count[len] = 0;
109− for (sym = 0; sym < codes; sym++)
110− count[lens[sym]]++;
111−
112− /* bound code lengths, force root to be within code lengths */
113− root = *bits;
114− for (max = MAXBITS; max >= 1; max--)
115− if (count[max] != 0) break;
116− if (root > max) root = max;
117− if (max == 0) { /* no symbols to code at all */
118− this.op = (unsigned char)64; /* invalid code marker */
119− this.bits = (unsigned char)1;
120− this.val = (unsigned short)0;
121− *(*table)++ = this; /* make a table to force an error */
122− *(*table)++ = this;
123− *bits = 1;
124− return 0; /* no symbols, but wait for decoding to report error */
125− }
126− for (min = 1; min <= MAXBITS; min++)
127− if (count[min] != 0) break;
128− if (root < min) root = min;
129−
130− /* check for an over-subscribed or incomplete set of lengths */
131− left = 1;
132− for (len = 1; len <= MAXBITS; len++) {
133− left <<= 1;
134− left -= count[len];
135− if (left < 0) return -1; /* over-subscribed */
136− }
137− if (left > 0 && (type == CODES || max != 1))
138− return -1; /* incomplete set */
139−
140− /* generate offsets into symbol table for each length for sorting */
141− offs[1] = 0;
142− for (len = 1; len < MAXBITS; len++)
143− offs[len + 1] = offs[len] + count[len];
144−
145− /* sort symbols by length, by symbol order within each length */
146− for (sym = 0; sym < codes; sym++)
147− if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
148−
149− /*
150− Create and fill in decoding tables. In this loop, the table being
151− filled is at next and has curr index bits. The code being used is huff
152− with length len. That code is converted to an index by dropping drop
153− bits off of the bottom. For codes where len is less than drop + curr,
154− those top drop + curr - len bits are incremented through all values to
155− fill the table with replicated entries.
156−
157− root is the number of index bits for the root table. When len exceeds
158− root, sub-tables are created pointed to by the root entry with an index
159− of the low root bits of huff. This is saved in low to check for when a
160− new sub-table should be started. drop is zero when the root table is
161− being filled, and drop is root when sub-tables are being filled.
162−
163− When a new sub-table is needed, it is necessary to look ahead in the
164− code lengths to determine what size sub-table is needed. The length
165− counts are used for this, and so count[] is decremented as codes are
166− entered in the tables.
167−
168− used keeps track of how many table entries have been allocated from the
169− provided *table space. It is checked when a LENS table is being made
170− against the space in *table, ENOUGH, minus the maximum space needed by
171− the worst case distance code, MAXD. This should never happen, but the
172− sufficiency of ENOUGH has not been proven exhaustively, hence the check.
173− This assumes that when type == LENS, bits == 9.
174−
175− sym increments through all symbols, and the loop terminates when
176− all codes of length max, i.e. all codes, have been processed. This
177− routine permits incomplete codes, so another loop after this one fills
178− in the rest of the decoding tables with invalid code markers.
179− */
180−
181− /* set up for code type */
182− switch (type) {
183− case CODES:
184− base = extra = work; /* dummy value--not used */
185− end = 19;
186− break;
187− case LENS:
188− base = lbase;
189− base -= 257;
190− extra = lext;
191− extra -= 257;
192− end = 256;
193− break;
194− default: /* DISTS */
195− base = dbase;
196− extra = dext;
197− end = -1;
198− }
199−
200− /* initialize state for loop */
201− huff = 0; /* starting code */
202− sym = 0; /* starting code symbol */
203− len = min; /* starting code length */
204− next = *table; /* current table to fill in */
205− curr = root; /* current table index bits */
206− drop = 0; /* current bits to drop from code for index */
207− low = (unsigned)(-1); /* trigger new sub-table when len > root */
208− used = 1U << root; /* use root table entries */
209− mask = used - 1; /* mask for comparing low */
210−
211− /* check available table space */
212− if (type == LENS && used >= ENOUGH - MAXD)
213− return 1;
214−
215− /* process all codes and make table entries */
216− for (;;) {
217− /* create table entry */
218− this.bits = (unsigned char)(len - drop);
219− if ((int)(work[sym]) < end) {
220− this.op = (unsigned char)0;
221− this.val = work[sym];
222− }
223− else if ((int)(work[sym]) > end) {
224− this.op = (unsigned char)(extra[work[sym]]);
225− this.val = base[work[sym]];
226− }
227− else {
228− this.op = (unsigned char)(32 + 64); /* end of block */
229− this.val = 0;
230− }
231−
232− /* replicate for those indices with low len bits equal to huff */
233− incr = 1U << (len - drop);
234− fill = 1U << curr;
235− min = fill; /* save offset to next table */
236− do {
237− fill -= incr;
238− next[(huff >> drop) + fill] = this;
239− } while (fill != 0);
240−
241− /* backwards increment the len-bit code huff */
242− incr = 1U << (len - 1);
243− while (huff & incr)
244− incr >>= 1;
245− if (incr != 0) {
246− huff &= incr - 1;
247− huff += incr;
248− }
249− else
250− huff = 0;
251−
252− /* go to next symbol, update count, len */
253− sym++;
254− if (--(count[len]) == 0) {
255− if (len == max) break;
256− len = lens[work[sym]];
257− }
258−
259− /* create new sub-table if needed */
260− if (len > root && (huff & mask) != low) {
261− /* if first time, transition to sub-tables */
262− if (drop == 0)
263− drop = root;
264−
265− /* increment past last table */
266− next += min; /* here min is 1 << curr */
267−
268− /* determine length of next table */
269− curr = len - drop;
270− left = (int)(1 << curr);
271− while (curr + drop < max) {
272− left -= count[curr + drop];
273− if (left <= 0) break;
274− curr++;
275− left <<= 1;
276− }
277−
278− /* check for enough space */
279− used += 1U << curr;
280− if (type == LENS && used >= ENOUGH - MAXD)
281− return 1;
282−
283− /* point entry in root table to sub-table */
284− low = huff & mask;
285− (*table)[low].op = (unsigned char)curr;
286− (*table)[low].bits = (unsigned char)root;
287− (*table)[low].val = (unsigned short)(next - *table);
288− }
289− }
290−
291− /*
292− Fill in rest of table for incomplete codes. This loop is similar to the
293− loop above in incrementing huff for table indices. It is assumed that
294− len is equal to curr + drop, so there is no loop needed to increment
295− through high index bits. When the current sub-table is filled, the loop
296− drops back to the root table to fill in any remaining entries there.
297− */
298− this.op = (unsigned char)64; /* invalid code marker */
299− this.bits = (unsigned char)(len - drop);
300− this.val = (unsigned short)0;
301− while (huff != 0) {
302− /* when done with sub-table, drop back to root table */
303− if (drop != 0 && (huff & mask) != low) {
304− drop = 0;
305− len = root;
306− next = *table;
307− this.bits = (unsigned char)len;
308− }
309−
310− /* put invalid code marker in table */
311− next[huff >> drop] = this;
312−
313− /* backwards increment the len-bit code huff */
314− incr = 1U << (len - 1);
315− while (huff & incr)
316− incr >>= 1;
317− if (incr != 0) {
318− huff &= incr - 1;
319− huff += incr;
320− }
321− else
322− huff = 0;
323− }
324−
325− /* set return parameters */
326− *table += used;
327− *bits = root;
328− return 0;
329−}
cbits/inftrees.hdeleted−55
@@ -1,55 +0,0 @@
1−/* inftrees.h -- header to use inftrees.c
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* Structure for decoding tables. Each entry provides either the
12− information needed to do the operation requested by the code that
13− indexed that table entry, or it provides a pointer to another
14− table that indexes more bits of the code. op indicates whether
15− the entry is a pointer to another table, a literal, a length or
16− distance, an end-of-block, or an invalid code. For a table
17− pointer, the low four bits of op is the number of index bits of
18− that table. For a length or distance, the low four bits of op
19− is the number of extra bits to get after the code. bits is
20− the number of bits in this code or part of the code to drop off
21− of the bit buffer. val is the actual byte to output in the case
22− of a literal, the base length or distance, or the offset from
23− the current table to the next table. Each entry is four bytes. */
24−typedef struct {
25− unsigned char op; /* operation, extra bits, table bits */
26− unsigned char bits; /* bits in this part of the code */
27− unsigned short val; /* offset in table or code value */
28−} code;
29−
30−/* op values as set by inflate_table():
31− 00000000 - literal
32− 0000tttt - table link, tttt != 0 is the number of table index bits
33− 0001eeee - length or distance, eeee is the number of extra bits
34− 01100000 - end of block
35− 01000000 - invalid code
36− */
37−
38−/* Maximum size of dynamic tree. The maximum found in a long but non-
39− exhaustive search was 1444 code structures (852 for length/literals
40− and 592 for distances, the latter actually the result of an
41− exhaustive search). The true maximum is not known, but the value
42− below is more than safe. */
43−#define ENOUGH 2048
44−#define MAXD 592
45−
46−/* Type of code to build for inftable() */
47−typedef enum {
48− CODES,
49− LENS,
50− DISTS
51−} codetype;
52−
53−extern int inflate_table OF((codetype type, unsigned short FAR *lens,
54− unsigned codes, code FAR * FAR *table,
55− unsigned FAR *bits, unsigned short FAR *work));
cbits/trees.cdeleted−1219
@@ -1,1219 +0,0 @@
1−/* trees.c -- output deflated data using Huffman coding
2− * Copyright (C) 1995-2005 Jean-loup Gailly
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− * ALGORITHM
8− *
9− * The "deflation" process uses several Huffman trees. The more
10− * common source values are represented by shorter bit sequences.
11− *
12− * Each code tree is stored in a compressed form which is itself
13− * a Huffman encoding of the lengths of all the code strings (in
14− * ascending order by source values). The actual code strings are
15− * reconstructed from the lengths in the inflate process, as described
16− * in the deflate specification.
17− *
18− * REFERENCES
19− *
20− * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
21− * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
22− *
23− * Storer, James A.
24− * Data Compression: Methods and Theory, pp. 49-50.
25− * Computer Science Press, 1988. ISBN 0-7167-8156-5.
26− *
27− * Sedgewick, R.
28− * Algorithms, p290.
29− * Addison-Wesley, 1983. ISBN 0-201-06672-6.
30− */
31−
32−/* @(#) $Id$ */
33−
34−/* #define GEN_TREES_H */
35−
36−#include "deflate.h"
37−
38−#ifdef DEBUG
39−# include <ctype.h>
40−#endif
41−
42−/* ===========================================================================
43− * Constants
44− */
45−
46−#define MAX_BL_BITS 7
47−/* Bit length codes must not exceed MAX_BL_BITS bits */
48−
49−#define END_BLOCK 256
50−/* end of block literal code */
51−
52−#define REP_3_6 16
53−/* repeat previous bit length 3-6 times (2 bits of repeat count) */
54−
55−#define REPZ_3_10 17
56−/* repeat a zero length 3-10 times (3 bits of repeat count) */
57−
58−#define REPZ_11_138 18
59−/* repeat a zero length 11-138 times (7 bits of repeat count) */
60−
61−local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
62− = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
63−
64−local const int extra_dbits[D_CODES] /* extra bits for each distance code */
65− = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
66−
67−local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
68− = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
69−
70−local const uch bl_order[BL_CODES]
71− = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
72−/* The lengths of the bit length codes are sent in order of decreasing
73− * probability, to avoid transmitting the lengths for unused bit length codes.
74− */
75−
76−#define Buf_size (8 * 2*sizeof(char))
77−/* Number of bits used within bi_buf. (bi_buf might be implemented on
78− * more than 16 bits on some systems.)
79− */
80−
81−/* ===========================================================================
82− * Local data. These are initialized only once.
83− */
84−
85−#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
86−
87−#if defined(GEN_TREES_H) || !defined(STDC)
88−/* non ANSI compilers may not accept trees.h */
89−
90−local ct_data static_ltree[L_CODES+2];
91−/* The static literal tree. Since the bit lengths are imposed, there is no
92− * need for the L_CODES extra codes used during heap construction. However
93− * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
94− * below).
95− */
96−
97−local ct_data static_dtree[D_CODES];
98−/* The static distance tree. (Actually a trivial tree since all codes use
99− * 5 bits.)
100− */
101−
102−uch _dist_code[DIST_CODE_LEN];
103−/* Distance codes. The first 256 values correspond to the distances
104− * 3 .. 258, the last 256 values correspond to the top 8 bits of
105− * the 15 bit distances.
106− */
107−
108−uch _length_code[MAX_MATCH-MIN_MATCH+1];
109−/* length code for each normalized match length (0 == MIN_MATCH) */
110−
111−local int base_length[LENGTH_CODES];
112−/* First normalized length for each code (0 = MIN_MATCH) */
113−
114−local int base_dist[D_CODES];
115−/* First normalized distance for each code (0 = distance of 1) */
116−
117−#else
118−# include "trees.h"
119−#endif /* GEN_TREES_H */
120−
121−struct static_tree_desc_s {
122− const ct_data *static_tree; /* static tree or NULL */
123− const intf *extra_bits; /* extra bits for each code or NULL */
124− int extra_base; /* base index for extra_bits */
125− int elems; /* max number of elements in the tree */
126− int max_length; /* max bit length for the codes */
127−};
128−
129−local static_tree_desc static_l_desc =
130−{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
131−
132−local static_tree_desc static_d_desc =
133−{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
134−
135−local static_tree_desc static_bl_desc =
136−{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
137−
138−/* ===========================================================================
139− * Local (static) routines in this file.
140− */
141−
142−local void tr_static_init OF((void));
143−local void init_block OF((deflate_state *s));
144−local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
145−local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
146−local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
147−local void build_tree OF((deflate_state *s, tree_desc *desc));
148−local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
149−local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
150−local int build_bl_tree OF((deflate_state *s));
151−local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
152− int blcodes));
153−local void compress_block OF((deflate_state *s, ct_data *ltree,
154− ct_data *dtree));
155−local void set_data_type OF((deflate_state *s));
156−local unsigned bi_reverse OF((unsigned value, int length));
157−local void bi_windup OF((deflate_state *s));
158−local void bi_flush OF((deflate_state *s));
159−local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
160− int header));
161−
162−#ifdef GEN_TREES_H
163−local void gen_trees_header OF((void));
164−#endif
165−
166−#ifndef DEBUG
167−# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
168− /* Send a code of the given tree. c and tree must not have side effects */
169−
170−#else /* DEBUG */
171−# define send_code(s, c, tree) \
172− { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
173− send_bits(s, tree[c].Code, tree[c].Len); }
174−#endif
175−
176−/* ===========================================================================
177− * Output a short LSB first on the stream.
178− * IN assertion: there is enough room in pendingBuf.
179− */
180−#define put_short(s, w) { \
181− put_byte(s, (uch)((w) & 0xff)); \
182− put_byte(s, (uch)((ush)(w) >> 8)); \
183−}
184−
185−/* ===========================================================================
186− * Send a value on a given number of bits.
187− * IN assertion: length <= 16 and value fits in length bits.
188− */
189−#ifdef DEBUG
190−local void send_bits OF((deflate_state *s, int value, int length));
191−
192−local void send_bits(s, value, length)
193− deflate_state *s;
194− int value; /* value to send */
195− int length; /* number of bits */
196−{
197− Tracevv((stderr," l %2d v %4x ", length, value));
198− Assert(length > 0 && length <= 15, "invalid length");
199− s->bits_sent += (ulg)length;
200−
201− /* If not enough room in bi_buf, use (valid) bits from bi_buf and
202− * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
203− * unused bits in value.
204− */
205− if (s->bi_valid > (int)Buf_size - length) {
206− s->bi_buf |= (value << s->bi_valid);
207− put_short(s, s->bi_buf);
208− s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
209− s->bi_valid += length - Buf_size;
210− } else {
211− s->bi_buf |= value << s->bi_valid;
212− s->bi_valid += length;
213− }
214−}
215−#else /* !DEBUG */
216−
217−#define send_bits(s, value, length) \
218−{ int len = length;\
219− if (s->bi_valid > (int)Buf_size - len) {\
220− int val = value;\
221− s->bi_buf |= (val << s->bi_valid);\
222− put_short(s, s->bi_buf);\
223− s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
224− s->bi_valid += len - Buf_size;\
225− } else {\
226− s->bi_buf |= (value) << s->bi_valid;\
227− s->bi_valid += len;\
228− }\
229−}
230−#endif /* DEBUG */
231−
232−
233−/* the arguments must not have side effects */
234−
235−/* ===========================================================================
236− * Initialize the various 'constant' tables.
237− */
238−local void tr_static_init()
239−{
240−#if defined(GEN_TREES_H) || !defined(STDC)
241− static int static_init_done = 0;
242− int n; /* iterates over tree elements */
243− int bits; /* bit counter */
244− int length; /* length value */
245− int code; /* code value */
246− int dist; /* distance index */
247− ush bl_count[MAX_BITS+1];
248− /* number of codes at each bit length for an optimal tree */
249−
250− if (static_init_done) return;
251−
252− /* For some embedded targets, global variables are not initialized: */
253− static_l_desc.static_tree = static_ltree;
254− static_l_desc.extra_bits = extra_lbits;
255− static_d_desc.static_tree = static_dtree;
256− static_d_desc.extra_bits = extra_dbits;
257− static_bl_desc.extra_bits = extra_blbits;
258−
259− /* Initialize the mapping length (0..255) -> length code (0..28) */
260− length = 0;
261− for (code = 0; code < LENGTH_CODES-1; code++) {
262− base_length[code] = length;
263− for (n = 0; n < (1<<extra_lbits[code]); n++) {
264− _length_code[length++] = (uch)code;
265− }
266− }
267− Assert (length == 256, "tr_static_init: length != 256");
268− /* Note that the length 255 (match length 258) can be represented
269− * in two different ways: code 284 + 5 bits or code 285, so we
270− * overwrite length_code[255] to use the best encoding:
271− */
272− _length_code[length-1] = (uch)code;
273−
274− /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
275− dist = 0;
276− for (code = 0 ; code < 16; code++) {
277− base_dist[code] = dist;
278− for (n = 0; n < (1<<extra_dbits[code]); n++) {
279− _dist_code[dist++] = (uch)code;
280− }
281− }
282− Assert (dist == 256, "tr_static_init: dist != 256");
283− dist >>= 7; /* from now on, all distances are divided by 128 */
284− for ( ; code < D_CODES; code++) {
285− base_dist[code] = dist << 7;
286− for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
287− _dist_code[256 + dist++] = (uch)code;
288− }
289− }
290− Assert (dist == 256, "tr_static_init: 256+dist != 512");
291−
292− /* Construct the codes of the static literal tree */
293− for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
294− n = 0;
295− while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
296− while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
297− while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
298− while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
299− /* Codes 286 and 287 do not exist, but we must include them in the
300− * tree construction to get a canonical Huffman tree (longest code
301− * all ones)
302− */
303− gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
304−
305− /* The static distance tree is trivial: */
306− for (n = 0; n < D_CODES; n++) {
307− static_dtree[n].Len = 5;
308− static_dtree[n].Code = bi_reverse((unsigned)n, 5);
309− }
310− static_init_done = 1;
311−
312−# ifdef GEN_TREES_H
313− gen_trees_header();
314−# endif
315−#endif /* defined(GEN_TREES_H) || !defined(STDC) */
316−}
317−
318−/* ===========================================================================
319− * Genererate the file trees.h describing the static trees.
320− */
321−#ifdef GEN_TREES_H
322−# ifndef DEBUG
323−# include <stdio.h>
324−# endif
325−
326−# define SEPARATOR(i, last, width) \
327− ((i) == (last)? "\n};\n\n" : \
328− ((i) % (width) == (width)-1 ? ",\n" : ", "))
329−
330−void gen_trees_header()
331−{
332− FILE *header = fopen("trees.h", "w");
333− int i;
334−
335− Assert (header != NULL, "Can't open trees.h");
336− fprintf(header,
337− "/* header created automatically with -DGEN_TREES_H */\n\n");
338−
339− fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
340− for (i = 0; i < L_CODES+2; i++) {
341− fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
342− static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
343− }
344−
345− fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
346− for (i = 0; i < D_CODES; i++) {
347− fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
348− static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
349− }
350−
351− fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
352− for (i = 0; i < DIST_CODE_LEN; i++) {
353− fprintf(header, "%2u%s", _dist_code[i],
354− SEPARATOR(i, DIST_CODE_LEN-1, 20));
355− }
356−
357− fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
358− for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
359− fprintf(header, "%2u%s", _length_code[i],
360− SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
361− }
362−
363− fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
364− for (i = 0; i < LENGTH_CODES; i++) {
365− fprintf(header, "%1u%s", base_length[i],
366− SEPARATOR(i, LENGTH_CODES-1, 20));
367− }
368−
369− fprintf(header, "local const int base_dist[D_CODES] = {\n");
370− for (i = 0; i < D_CODES; i++) {
371− fprintf(header, "%5u%s", base_dist[i],
372− SEPARATOR(i, D_CODES-1, 10));
373− }
374−
375− fclose(header);
376−}
377−#endif /* GEN_TREES_H */
378−
379−/* ===========================================================================
380− * Initialize the tree data structures for a new zlib stream.
381− */
382−void _tr_init(s)
383− deflate_state *s;
384−{
385− tr_static_init();
386−
387− s->l_desc.dyn_tree = s->dyn_ltree;
388− s->l_desc.stat_desc = &static_l_desc;
389−
390− s->d_desc.dyn_tree = s->dyn_dtree;
391− s->d_desc.stat_desc = &static_d_desc;
392−
393− s->bl_desc.dyn_tree = s->bl_tree;
394− s->bl_desc.stat_desc = &static_bl_desc;
395−
396− s->bi_buf = 0;
397− s->bi_valid = 0;
398− s->last_eob_len = 8; /* enough lookahead for inflate */
399−#ifdef DEBUG
400− s->compressed_len = 0L;
401− s->bits_sent = 0L;
402−#endif
403−
404− /* Initialize the first block of the first file: */
405− init_block(s);
406−}
407−
408−/* ===========================================================================
409− * Initialize a new block.
410− */
411−local void init_block(s)
412− deflate_state *s;
413−{
414− int n; /* iterates over tree elements */
415−
416− /* Initialize the trees. */
417− for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
418− for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
419− for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
420−
421− s->dyn_ltree[END_BLOCK].Freq = 1;
422− s->opt_len = s->static_len = 0L;
423− s->last_lit = s->matches = 0;
424−}
425−
426−#define SMALLEST 1
427−/* Index within the heap array of least frequent node in the Huffman tree */
428−
429−
430−/* ===========================================================================
431− * Remove the smallest element from the heap and recreate the heap with
432− * one less element. Updates heap and heap_len.
433− */
434−#define pqremove(s, tree, top) \
435−{\
436− top = s->heap[SMALLEST]; \
437− s->heap[SMALLEST] = s->heap[s->heap_len--]; \
438− pqdownheap(s, tree, SMALLEST); \
439−}
440−
441−/* ===========================================================================
442− * Compares to subtrees, using the tree depth as tie breaker when
443− * the subtrees have equal frequency. This minimizes the worst case length.
444− */
445−#define smaller(tree, n, m, depth) \
446− (tree[n].Freq < tree[m].Freq || \
447− (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
448−
449−/* ===========================================================================
450− * Restore the heap property by moving down the tree starting at node k,
451− * exchanging a node with the smallest of its two sons if necessary, stopping
452− * when the heap property is re-established (each father smaller than its
453− * two sons).
454− */
455−local void pqdownheap(s, tree, k)
456− deflate_state *s;
457− ct_data *tree; /* the tree to restore */
458− int k; /* node to move down */
459−{
460− int v = s->heap[k];
461− int j = k << 1; /* left son of k */
462− while (j <= s->heap_len) {
463− /* Set j to the smallest of the two sons: */
464− if (j < s->heap_len &&
465− smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
466− j++;
467− }
468− /* Exit if v is smaller than both sons */
469− if (smaller(tree, v, s->heap[j], s->depth)) break;
470−
471− /* Exchange v with the smallest son */
472− s->heap[k] = s->heap[j]; k = j;
473−
474− /* And continue down the tree, setting j to the left son of k */
475− j <<= 1;
476− }
477− s->heap[k] = v;
478−}
479−
480−/* ===========================================================================
481− * Compute the optimal bit lengths for a tree and update the total bit length
482− * for the current block.
483− * IN assertion: the fields freq and dad are set, heap[heap_max] and
484− * above are the tree nodes sorted by increasing frequency.
485− * OUT assertions: the field len is set to the optimal bit length, the
486− * array bl_count contains the frequencies for each bit length.
487− * The length opt_len is updated; static_len is also updated if stree is
488− * not null.
489− */
490−local void gen_bitlen(s, desc)
491− deflate_state *s;
492− tree_desc *desc; /* the tree descriptor */
493−{
494− ct_data *tree = desc->dyn_tree;
495− int max_code = desc->max_code;
496− const ct_data *stree = desc->stat_desc->static_tree;
497− const intf *extra = desc->stat_desc->extra_bits;
498− int base = desc->stat_desc->extra_base;
499− int max_length = desc->stat_desc->max_length;
500− int h; /* heap index */
501− int n, m; /* iterate over the tree elements */
502− int bits; /* bit length */
503− int xbits; /* extra bits */
504− ush f; /* frequency */
505− int overflow = 0; /* number of elements with bit length too large */
506−
507− for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
508−
509− /* In a first pass, compute the optimal bit lengths (which may
510− * overflow in the case of the bit length tree).
511− */
512− tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
513−
514− for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
515− n = s->heap[h];
516− bits = tree[tree[n].Dad].Len + 1;
517− if (bits > max_length) bits = max_length, overflow++;
518− tree[n].Len = (ush)bits;
519− /* We overwrite tree[n].Dad which is no longer needed */
520−
521− if (n > max_code) continue; /* not a leaf node */
522−
523− s->bl_count[bits]++;
524− xbits = 0;
525− if (n >= base) xbits = extra[n-base];
526− f = tree[n].Freq;
527− s->opt_len += (ulg)f * (bits + xbits);
528− if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
529− }
530− if (overflow == 0) return;
531−
532− Trace((stderr,"\nbit length overflow\n"));
533− /* This happens for example on obj2 and pic of the Calgary corpus */
534−
535− /* Find the first bit length which could increase: */
536− do {
537− bits = max_length-1;
538− while (s->bl_count[bits] == 0) bits--;
539− s->bl_count[bits]--; /* move one leaf down the tree */
540− s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
541− s->bl_count[max_length]--;
542− /* The brother of the overflow item also moves one step up,
543− * but this does not affect bl_count[max_length]
544− */
545− overflow -= 2;
546− } while (overflow > 0);
547−
548− /* Now recompute all bit lengths, scanning in increasing frequency.
549− * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
550− * lengths instead of fixing only the wrong ones. This idea is taken
551− * from 'ar' written by Haruhiko Okumura.)
552− */
553− for (bits = max_length; bits != 0; bits--) {
554− n = s->bl_count[bits];
555− while (n != 0) {
556− m = s->heap[--h];
557− if (m > max_code) continue;
558− if ((unsigned) tree[m].Len != (unsigned) bits) {
559− Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
560− s->opt_len += ((long)bits - (long)tree[m].Len)
561− *(long)tree[m].Freq;
562− tree[m].Len = (ush)bits;
563− }
564− n--;
565− }
566− }
567−}
568−
569−/* ===========================================================================
570− * Generate the codes for a given tree and bit counts (which need not be
571− * optimal).
572− * IN assertion: the array bl_count contains the bit length statistics for
573− * the given tree and the field len is set for all tree elements.
574− * OUT assertion: the field code is set for all tree elements of non
575− * zero code length.
576− */
577−local void gen_codes (tree, max_code, bl_count)
578− ct_data *tree; /* the tree to decorate */
579− int max_code; /* largest code with non zero frequency */
580− ushf *bl_count; /* number of codes at each bit length */
581−{
582− ush next_code[MAX_BITS+1]; /* next code value for each bit length */
583− ush code = 0; /* running code value */
584− int bits; /* bit index */
585− int n; /* code index */
586−
587− /* The distribution counts are first used to generate the code values
588− * without bit reversal.
589− */
590− for (bits = 1; bits <= MAX_BITS; bits++) {
591− next_code[bits] = code = (code + bl_count[bits-1]) << 1;
592− }
593− /* Check that the bit counts in bl_count are consistent. The last code
594− * must be all ones.
595− */
596− Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
597− "inconsistent bit counts");
598− Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
599−
600− for (n = 0; n <= max_code; n++) {
601− int len = tree[n].Len;
602− if (len == 0) continue;
603− /* Now reverse the bits */
604− tree[n].Code = bi_reverse(next_code[len]++, len);
605−
606− Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
607− n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
608− }
609−}
610−
611−/* ===========================================================================
612− * Construct one Huffman tree and assigns the code bit strings and lengths.
613− * Update the total bit length for the current block.
614− * IN assertion: the field freq is set for all tree elements.
615− * OUT assertions: the fields len and code are set to the optimal bit length
616− * and corresponding code. The length opt_len is updated; static_len is
617− * also updated if stree is not null. The field max_code is set.
618− */
619−local void build_tree(s, desc)
620− deflate_state *s;
621− tree_desc *desc; /* the tree descriptor */
622−{
623− ct_data *tree = desc->dyn_tree;
624− const ct_data *stree = desc->stat_desc->static_tree;
625− int elems = desc->stat_desc->elems;
626− int n, m; /* iterate over heap elements */
627− int max_code = -1; /* largest code with non zero frequency */
628− int node; /* new node being created */
629−
630− /* Construct the initial heap, with least frequent element in
631− * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
632− * heap[0] is not used.
633− */
634− s->heap_len = 0, s->heap_max = HEAP_SIZE;
635−
636− for (n = 0; n < elems; n++) {
637− if (tree[n].Freq != 0) {
638− s->heap[++(s->heap_len)] = max_code = n;
639− s->depth[n] = 0;
640− } else {
641− tree[n].Len = 0;
642− }
643− }
644−
645− /* The pkzip format requires that at least one distance code exists,
646− * and that at least one bit should be sent even if there is only one
647− * possible code. So to avoid special checks later on we force at least
648− * two codes of non zero frequency.
649− */
650− while (s->heap_len < 2) {
651− node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
652− tree[node].Freq = 1;
653− s->depth[node] = 0;
654− s->opt_len--; if (stree) s->static_len -= stree[node].Len;
655− /* node is 0 or 1 so it does not have extra bits */
656− }
657− desc->max_code = max_code;
658−
659− /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
660− * establish sub-heaps of increasing lengths:
661− */
662− for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
663−
664− /* Construct the Huffman tree by repeatedly combining the least two
665− * frequent nodes.
666− */
667− node = elems; /* next internal node of the tree */
668− do {
669− pqremove(s, tree, n); /* n = node of least frequency */
670− m = s->heap[SMALLEST]; /* m = node of next least frequency */
671−
672− s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
673− s->heap[--(s->heap_max)] = m;
674−
675− /* Create a new node father of n and m */
676− tree[node].Freq = tree[n].Freq + tree[m].Freq;
677− s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
678− s->depth[n] : s->depth[m]) + 1);
679− tree[n].Dad = tree[m].Dad = (ush)node;
680−#ifdef DUMP_BL_TREE
681− if (tree == s->bl_tree) {
682− fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
683− node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
684− }
685−#endif
686− /* and insert the new node in the heap */
687− s->heap[SMALLEST] = node++;
688− pqdownheap(s, tree, SMALLEST);
689−
690− } while (s->heap_len >= 2);
691−
692− s->heap[--(s->heap_max)] = s->heap[SMALLEST];
693−
694− /* At this point, the fields freq and dad are set. We can now
695− * generate the bit lengths.
696− */
697− gen_bitlen(s, (tree_desc *)desc);
698−
699− /* The field len is now set, we can generate the bit codes */
700− gen_codes ((ct_data *)tree, max_code, s->bl_count);
701−}
702−
703−/* ===========================================================================
704− * Scan a literal or distance tree to determine the frequencies of the codes
705− * in the bit length tree.
706− */
707−local void scan_tree (s, tree, max_code)
708− deflate_state *s;
709− ct_data *tree; /* the tree to be scanned */
710− int max_code; /* and its largest code of non zero frequency */
711−{
712− int n; /* iterates over all tree elements */
713− int prevlen = -1; /* last emitted length */
714− int curlen; /* length of current code */
715− int nextlen = tree[0].Len; /* length of next code */
716− int count = 0; /* repeat count of the current code */
717− int max_count = 7; /* max repeat count */
718− int min_count = 4; /* min repeat count */
719−
720− if (nextlen == 0) max_count = 138, min_count = 3;
721− tree[max_code+1].Len = (ush)0xffff; /* guard */
722−
723− for (n = 0; n <= max_code; n++) {
724− curlen = nextlen; nextlen = tree[n+1].Len;
725− if (++count < max_count && curlen == nextlen) {
726− continue;
727− } else if (count < min_count) {
728− s->bl_tree[curlen].Freq += count;
729− } else if (curlen != 0) {
730− if (curlen != prevlen) s->bl_tree[curlen].Freq++;
731− s->bl_tree[REP_3_6].Freq++;
732− } else if (count <= 10) {
733− s->bl_tree[REPZ_3_10].Freq++;
734− } else {
735− s->bl_tree[REPZ_11_138].Freq++;
736− }
737− count = 0; prevlen = curlen;
738− if (nextlen == 0) {
739− max_count = 138, min_count = 3;
740− } else if (curlen == nextlen) {
741− max_count = 6, min_count = 3;
742− } else {
743− max_count = 7, min_count = 4;
744− }
745− }
746−}
747−
748−/* ===========================================================================
749− * Send a literal or distance tree in compressed form, using the codes in
750− * bl_tree.
751− */
752−local void send_tree (s, tree, max_code)
753− deflate_state *s;
754− ct_data *tree; /* the tree to be scanned */
755− int max_code; /* and its largest code of non zero frequency */
756−{
757− int n; /* iterates over all tree elements */
758− int prevlen = -1; /* last emitted length */
759− int curlen; /* length of current code */
760− int nextlen = tree[0].Len; /* length of next code */
761− int count = 0; /* repeat count of the current code */
762− int max_count = 7; /* max repeat count */
763− int min_count = 4; /* min repeat count */
764−
765− /* tree[max_code+1].Len = -1; */ /* guard already set */
766− if (nextlen == 0) max_count = 138, min_count = 3;
767−
768− for (n = 0; n <= max_code; n++) {
769− curlen = nextlen; nextlen = tree[n+1].Len;
770− if (++count < max_count && curlen == nextlen) {
771− continue;
772− } else if (count < min_count) {
773− do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
774−
775− } else if (curlen != 0) {
776− if (curlen != prevlen) {
777− send_code(s, curlen, s->bl_tree); count--;
778− }
779− Assert(count >= 3 && count <= 6, " 3_6?");
780− send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
781−
782− } else if (count <= 10) {
783− send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
784−
785− } else {
786− send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
787− }
788− count = 0; prevlen = curlen;
789− if (nextlen == 0) {
790− max_count = 138, min_count = 3;
791− } else if (curlen == nextlen) {
792− max_count = 6, min_count = 3;
793− } else {
794− max_count = 7, min_count = 4;
795− }
796− }
797−}
798−
799−/* ===========================================================================
800− * Construct the Huffman tree for the bit lengths and return the index in
801− * bl_order of the last bit length code to send.
802− */
803−local int build_bl_tree(s)
804− deflate_state *s;
805−{
806− int max_blindex; /* index of last bit length code of non zero freq */
807−
808− /* Determine the bit length frequencies for literal and distance trees */
809− scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
810− scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
811−
812− /* Build the bit length tree: */
813− build_tree(s, (tree_desc *)(&(s->bl_desc)));
814− /* opt_len now includes the length of the tree representations, except
815− * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
816− */
817−
818− /* Determine the number of bit length codes to send. The pkzip format
819− * requires that at least 4 bit length codes be sent. (appnote.txt says
820− * 3 but the actual value used is 4.)
821− */
822− for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
823− if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
824− }
825− /* Update opt_len to include the bit length tree and counts */
826− s->opt_len += 3*(max_blindex+1) + 5+5+4;
827− Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
828− s->opt_len, s->static_len));
829−
830− return max_blindex;
831−}
832−
833−/* ===========================================================================
834− * Send the header for a block using dynamic Huffman trees: the counts, the
835− * lengths of the bit length codes, the literal tree and the distance tree.
836− * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
837− */
838−local void send_all_trees(s, lcodes, dcodes, blcodes)
839− deflate_state *s;
840− int lcodes, dcodes, blcodes; /* number of codes for each tree */
841−{
842− int rank; /* index in bl_order */
843−
844− Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
845− Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
846− "too many codes");
847− Tracev((stderr, "\nbl counts: "));
848− send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
849− send_bits(s, dcodes-1, 5);
850− send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
851− for (rank = 0; rank < blcodes; rank++) {
852− Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
853− send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
854− }
855− Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
856−
857− send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
858− Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
859−
860− send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
861− Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
862−}
863−
864−/* ===========================================================================
865− * Send a stored block
866− */
867−void _tr_stored_block(s, buf, stored_len, eof)
868− deflate_state *s;
869− charf *buf; /* input block */
870− ulg stored_len; /* length of input block */
871− int eof; /* true if this is the last block for a file */
872−{
873− send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
874−#ifdef DEBUG
875− s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
876− s->compressed_len += (stored_len + 4) << 3;
877−#endif
878− copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
879−}
880−
881−/* ===========================================================================
882− * Send one empty static block to give enough lookahead for inflate.
883− * This takes 10 bits, of which 7 may remain in the bit buffer.
884− * The current inflate code requires 9 bits of lookahead. If the
885− * last two codes for the previous block (real code plus EOB) were coded
886− * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
887− * the last real code. In this case we send two empty static blocks instead
888− * of one. (There are no problems if the previous block is stored or fixed.)
889− * To simplify the code, we assume the worst case of last real code encoded
890− * on one bit only.
891− */
892−void _tr_align(s)
893− deflate_state *s;
894−{
895− send_bits(s, STATIC_TREES<<1, 3);
896− send_code(s, END_BLOCK, static_ltree);
897−#ifdef DEBUG
898− s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
899−#endif
900− bi_flush(s);
901− /* Of the 10 bits for the empty block, we have already sent
902− * (10 - bi_valid) bits. The lookahead for the last real code (before
903− * the EOB of the previous block) was thus at least one plus the length
904− * of the EOB plus what we have just sent of the empty static block.
905− */
906− if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
907− send_bits(s, STATIC_TREES<<1, 3);
908− send_code(s, END_BLOCK, static_ltree);
909−#ifdef DEBUG
910− s->compressed_len += 10L;
911−#endif
912− bi_flush(s);
913− }
914− s->last_eob_len = 7;
915−}
916−
917−/* ===========================================================================
918− * Determine the best encoding for the current block: dynamic trees, static
919− * trees or store, and output the encoded block to the zip file.
920− */
921−void _tr_flush_block(s, buf, stored_len, eof)
922− deflate_state *s;
923− charf *buf; /* input block, or NULL if too old */
924− ulg stored_len; /* length of input block */
925− int eof; /* true if this is the last block for a file */
926−{
927− ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
928− int max_blindex = 0; /* index of last bit length code of non zero freq */
929−
930− /* Build the Huffman trees unless a stored block is forced */
931− if (s->level > 0) {
932−
933− /* Check if the file is binary or text */
934− if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
935− set_data_type(s);
936−
937− /* Construct the literal and distance trees */
938− build_tree(s, (tree_desc *)(&(s->l_desc)));
939− Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
940− s->static_len));
941−
942− build_tree(s, (tree_desc *)(&(s->d_desc)));
943− Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
944− s->static_len));
945− /* At this point, opt_len and static_len are the total bit lengths of
946− * the compressed block data, excluding the tree representations.
947− */
948−
949− /* Build the bit length tree for the above two trees, and get the index
950− * in bl_order of the last bit length code to send.
951− */
952− max_blindex = build_bl_tree(s);
953−
954− /* Determine the best encoding. Compute the block lengths in bytes. */
955− opt_lenb = (s->opt_len+3+7)>>3;
956− static_lenb = (s->static_len+3+7)>>3;
957−
958− Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
959− opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
960− s->last_lit));
961−
962− if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
963−
964− } else {
965− Assert(buf != (char*)0, "lost buf");
966− opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
967− }
968−
969−#ifdef FORCE_STORED
970− if (buf != (char*)0) { /* force stored block */
971−#else
972− if (stored_len+4 <= opt_lenb && buf != (char*)0) {
973− /* 4: two words for the lengths */
974−#endif
975− /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
976− * Otherwise we can't have processed more than WSIZE input bytes since
977− * the last block flush, because compression would have been
978− * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
979− * transform a block into a stored block.
980− */
981− _tr_stored_block(s, buf, stored_len, eof);
982−
983−#ifdef FORCE_STATIC
984− } else if (static_lenb >= 0) { /* force static trees */
985−#else
986− } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
987−#endif
988− send_bits(s, (STATIC_TREES<<1)+eof, 3);
989− compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
990−#ifdef DEBUG
991− s->compressed_len += 3 + s->static_len;
992−#endif
993− } else {
994− send_bits(s, (DYN_TREES<<1)+eof, 3);
995− send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
996− max_blindex+1);
997− compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
998−#ifdef DEBUG
999− s->compressed_len += 3 + s->opt_len;
1000−#endif
1001− }
1002− Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1003− /* The above check is made mod 2^32, for files larger than 512 MB
1004− * and uLong implemented on 32 bits.
1005− */
1006− init_block(s);
1007−
1008− if (eof) {
1009− bi_windup(s);
1010−#ifdef DEBUG
1011− s->compressed_len += 7; /* align on byte boundary */
1012−#endif
1013− }
1014− Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1015− s->compressed_len-7*eof));
1016−}
1017−
1018−/* ===========================================================================
1019− * Save the match info and tally the frequency counts. Return true if
1020− * the current block must be flushed.
1021− */
1022−int _tr_tally (s, dist, lc)
1023− deflate_state *s;
1024− unsigned dist; /* distance of matched string */
1025− unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1026−{
1027− s->d_buf[s->last_lit] = (ush)dist;
1028− s->l_buf[s->last_lit++] = (uch)lc;
1029− if (dist == 0) {
1030− /* lc is the unmatched char */
1031− s->dyn_ltree[lc].Freq++;
1032− } else {
1033− s->matches++;
1034− /* Here, lc is the match length - MIN_MATCH */
1035− dist--; /* dist = match distance - 1 */
1036− Assert((ush)dist < (ush)MAX_DIST(s) &&
1037− (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1038− (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1039−
1040− s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1041− s->dyn_dtree[d_code(dist)].Freq++;
1042− }
1043−
1044−#ifdef TRUNCATE_BLOCK
1045− /* Try to guess if it is profitable to stop the current block here */
1046− if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1047− /* Compute an upper bound for the compressed length */
1048− ulg out_length = (ulg)s->last_lit*8L;
1049− ulg in_length = (ulg)((long)s->strstart - s->block_start);
1050− int dcode;
1051− for (dcode = 0; dcode < D_CODES; dcode++) {
1052− out_length += (ulg)s->dyn_dtree[dcode].Freq *
1053− (5L+extra_dbits[dcode]);
1054− }
1055− out_length >>= 3;
1056− Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1057− s->last_lit, in_length, out_length,
1058− 100L - out_length*100L/in_length));
1059− if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1060− }
1061−#endif
1062− return (s->last_lit == s->lit_bufsize-1);
1063− /* We avoid equality with lit_bufsize because of wraparound at 64K
1064− * on 16 bit machines and because stored blocks are restricted to
1065− * 64K-1 bytes.
1066− */
1067−}
1068−
1069−/* ===========================================================================
1070− * Send the block data compressed using the given Huffman trees
1071− */
1072−local void compress_block(s, ltree, dtree)
1073− deflate_state *s;
1074− ct_data *ltree; /* literal tree */
1075− ct_data *dtree; /* distance tree */
1076−{
1077− unsigned dist; /* distance of matched string */
1078− int lc; /* match length or unmatched char (if dist == 0) */
1079− unsigned lx = 0; /* running index in l_buf */
1080− unsigned code; /* the code to send */
1081− int extra; /* number of extra bits to send */
1082−
1083− if (s->last_lit != 0) do {
1084− dist = s->d_buf[lx];
1085− lc = s->l_buf[lx++];
1086− if (dist == 0) {
1087− send_code(s, lc, ltree); /* send a literal byte */
1088− Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1089− } else {
1090− /* Here, lc is the match length - MIN_MATCH */
1091− code = _length_code[lc];
1092− send_code(s, code+LITERALS+1, ltree); /* send the length code */
1093− extra = extra_lbits[code];
1094− if (extra != 0) {
1095− lc -= base_length[code];
1096− send_bits(s, lc, extra); /* send the extra length bits */
1097− }
1098− dist--; /* dist is now the match distance - 1 */
1099− code = d_code(dist);
1100− Assert (code < D_CODES, "bad d_code");
1101−
1102− send_code(s, code, dtree); /* send the distance code */
1103− extra = extra_dbits[code];
1104− if (extra != 0) {
1105− dist -= base_dist[code];
1106− send_bits(s, dist, extra); /* send the extra distance bits */
1107− }
1108− } /* literal or match pair ? */
1109−
1110− /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
1111− Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
1112− "pendingBuf overflow");
1113−
1114− } while (lx < s->last_lit);
1115−
1116− send_code(s, END_BLOCK, ltree);
1117− s->last_eob_len = ltree[END_BLOCK].Len;
1118−}
1119−
1120−/* ===========================================================================
1121− * Set the data type to BINARY or TEXT, using a crude approximation:
1122− * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
1123− * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
1124− * IN assertion: the fields Freq of dyn_ltree are set.
1125− */
1126−local void set_data_type(s)
1127− deflate_state *s;
1128−{
1129− int n;
1130−
1131− for (n = 0; n < 9; n++)
1132− if (s->dyn_ltree[n].Freq != 0)
1133− break;
1134− if (n == 9)
1135− for (n = 14; n < 32; n++)
1136− if (s->dyn_ltree[n].Freq != 0)
1137− break;
1138− s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
1139−}
1140−
1141−/* ===========================================================================
1142− * Reverse the first len bits of a code, using straightforward code (a faster
1143− * method would use a table)
1144− * IN assertion: 1 <= len <= 15
1145− */
1146−local unsigned bi_reverse(code, len)
1147− unsigned code; /* the value to invert */
1148− int len; /* its bit length */
1149−{
1150− register unsigned res = 0;
1151− do {
1152− res |= code & 1;
1153− code >>= 1, res <<= 1;
1154− } while (--len > 0);
1155− return res >> 1;
1156−}
1157−
1158−/* ===========================================================================
1159− * Flush the bit buffer, keeping at most 7 bits in it.
1160− */
1161−local void bi_flush(s)
1162− deflate_state *s;
1163−{
1164− if (s->bi_valid == 16) {
1165− put_short(s, s->bi_buf);
1166− s->bi_buf = 0;
1167− s->bi_valid = 0;
1168− } else if (s->bi_valid >= 8) {
1169− put_byte(s, (Byte)s->bi_buf);
1170− s->bi_buf >>= 8;
1171− s->bi_valid -= 8;
1172− }
1173−}
1174−
1175−/* ===========================================================================
1176− * Flush the bit buffer and align the output on a byte boundary
1177− */
1178−local void bi_windup(s)
1179− deflate_state *s;
1180−{
1181− if (s->bi_valid > 8) {
1182− put_short(s, s->bi_buf);
1183− } else if (s->bi_valid > 0) {
1184− put_byte(s, (Byte)s->bi_buf);
1185− }
1186− s->bi_buf = 0;
1187− s->bi_valid = 0;
1188−#ifdef DEBUG
1189− s->bits_sent = (s->bits_sent+7) & ~7;
1190−#endif
1191−}
1192−
1193−/* ===========================================================================
1194− * Copy a stored block, storing first the length and its
1195− * one's complement if requested.
1196− */
1197−local void copy_block(s, buf, len, header)
1198− deflate_state *s;
1199− charf *buf; /* the input data */
1200− unsigned len; /* its length */
1201− int header; /* true if block header must be written */
1202−{
1203− bi_windup(s); /* align on byte boundary */
1204− s->last_eob_len = 8; /* enough lookahead for inflate */
1205−
1206− if (header) {
1207− put_short(s, (ush)len);
1208− put_short(s, (ush)~len);
1209−#ifdef DEBUG
1210− s->bits_sent += 2*16;
1211−#endif
1212− }
1213−#ifdef DEBUG
1214− s->bits_sent += (ulg)len<<3;
1215−#endif
1216− while (len--) {
1217− put_byte(s, *buf++);
1218− }
1219−}
cbits/trees.hdeleted−128
@@ -1,128 +0,0 @@
1−/* header created automatically with -DGEN_TREES_H */
2−
3−local const ct_data static_ltree[L_CODES+2] = {
4−{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
5−{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
6−{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
7−{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
8−{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
9−{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
10−{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
11−{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
12−{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
13−{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
14−{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
15−{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
16−{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
17−{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
18−{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
19−{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
20−{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
21−{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
22−{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
23−{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
24−{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
25−{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
26−{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
27−{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
28−{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
29−{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
30−{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
31−{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
32−{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
33−{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
34−{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
35−{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
36−{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
37−{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
38−{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
39−{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
40−{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
41−{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
42−{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
43−{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
44−{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
45−{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
46−{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
47−{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
48−{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
49−{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
50−{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
51−{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
52−{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
53−{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
54−{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
55−{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
56−{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
57−{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
58−{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
59−{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
60−{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
61−{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
62−};
63−
64−local const ct_data static_dtree[D_CODES] = {
65−{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
66−{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
67−{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
68−{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
69−{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
70−{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
71−};
72−
73−const uch _dist_code[DIST_CODE_LEN] = {
74− 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
75− 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
76−10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
77−11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
78−12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
79−13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
80−13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
81−14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
82−14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
83−14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
84−15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
85−15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
86−15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
87−18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
88−23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
89−24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
90−26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
91−26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
92−27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
93−27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
94−28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
95−28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
96−28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
97−29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
98−29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
99−29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
100−};
101−
102−const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
103− 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
104−13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
105−17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
106−19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
107−21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
108−22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
109−23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
110−24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
111−25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
112−25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
113−26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
114−26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
115−27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
116−};
117−
118−local const int base_length[LENGTH_CODES] = {
119−0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
120−64, 80, 96, 112, 128, 160, 192, 224, 0
121−};
122−
123−local const int base_dist[D_CODES] = {
124− 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
125− 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
126− 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
127−};
128−
cbits/uncompr.cdeleted−61
@@ -1,61 +0,0 @@
1−/* uncompr.c -- decompress a memory buffer
2− * Copyright (C) 1995-2003 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#define ZLIB_INTERNAL
9−#include "zlib.h"
10−
11−/* ===========================================================================
12− Decompresses the source buffer into the destination buffer. sourceLen is
13− the byte length of the source buffer. Upon entry, destLen is the total
14− size of the destination buffer, which must be large enough to hold the
15− entire uncompressed data. (The size of the uncompressed data must have
16− been saved previously by the compressor and transmitted to the decompressor
17− by some mechanism outside the scope of this compression library.)
18− Upon exit, destLen is the actual size of the compressed buffer.
19− This function can be used to decompress a whole file at once if the
20− input file is mmap'ed.
21−
22− uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
23− enough memory, Z_BUF_ERROR if there was not enough room in the output
24− buffer, or Z_DATA_ERROR if the input data was corrupted.
25−*/
26−int ZEXPORT uncompress (dest, destLen, source, sourceLen)
27− Bytef *dest;
28− uLongf *destLen;
29− const Bytef *source;
30− uLong sourceLen;
31−{
32− z_stream stream;
33− int err;
34−
35− stream.next_in = (Bytef*)source;
36− stream.avail_in = (uInt)sourceLen;
37− /* Check for source > 64K on 16-bit machine: */
38− if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
39−
40− stream.next_out = dest;
41− stream.avail_out = (uInt)*destLen;
42− if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
43−
44− stream.zalloc = (alloc_func)0;
45− stream.zfree = (free_func)0;
46−
47− err = inflateInit(&stream);
48− if (err != Z_OK) return err;
49−
50− err = inflate(&stream, Z_FINISH);
51− if (err != Z_STREAM_END) {
52− inflateEnd(&stream);
53− if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
54− return Z_DATA_ERROR;
55− return err;
56− }
57− *destLen = stream.total_out;
58−
59− err = inflateEnd(&stream);
60− return err;
61−}
cbits/zconf.hdeleted−332
@@ -1,332 +0,0 @@
1−/* zconf.h -- configuration of the zlib compression library
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#ifndef ZCONF_H
9−#define ZCONF_H
10−
11−/*
12− * If you *really* need a unique prefix for all types and library functions,
13− * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
14− */
15−#ifdef Z_PREFIX
16−# define deflateInit_ z_deflateInit_
17−# define deflate z_deflate
18−# define deflateEnd z_deflateEnd
19−# define inflateInit_ z_inflateInit_
20−# define inflate z_inflate
21−# define inflateEnd z_inflateEnd
22−# define deflateInit2_ z_deflateInit2_
23−# define deflateSetDictionary z_deflateSetDictionary
24−# define deflateCopy z_deflateCopy
25−# define deflateReset z_deflateReset
26−# define deflateParams z_deflateParams
27−# define deflateBound z_deflateBound
28−# define deflatePrime z_deflatePrime
29−# define inflateInit2_ z_inflateInit2_
30−# define inflateSetDictionary z_inflateSetDictionary
31−# define inflateSync z_inflateSync
32−# define inflateSyncPoint z_inflateSyncPoint
33−# define inflateCopy z_inflateCopy
34−# define inflateReset z_inflateReset
35−# define inflateBack z_inflateBack
36−# define inflateBackEnd z_inflateBackEnd
37−# define compress z_compress
38−# define compress2 z_compress2
39−# define compressBound z_compressBound
40−# define uncompress z_uncompress
41−# define adler32 z_adler32
42−# define crc32 z_crc32
43−# define get_crc_table z_get_crc_table
44−# define zError z_zError
45−
46−# define alloc_func z_alloc_func
47−# define free_func z_free_func
48−# define in_func z_in_func
49−# define out_func z_out_func
50−# define Byte z_Byte
51−# define uInt z_uInt
52−# define uLong z_uLong
53−# define Bytef z_Bytef
54−# define charf z_charf
55−# define intf z_intf
56−# define uIntf z_uIntf
57−# define uLongf z_uLongf
58−# define voidpf z_voidpf
59−# define voidp z_voidp
60−#endif
61−
62−#if defined(__MSDOS__) && !defined(MSDOS)
63−# define MSDOS
64−#endif
65−#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
66−# define OS2
67−#endif
68−#if defined(_WINDOWS) && !defined(WINDOWS)
69−# define WINDOWS
70−#endif
71−#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
72−# ifndef WIN32
73−# define WIN32
74−# endif
75−#endif
76−#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
77−# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
78−# ifndef SYS16BIT
79−# define SYS16BIT
80−# endif
81−# endif
82−#endif
83−
84−/*
85− * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
86− * than 64k bytes at a time (needed on systems with 16-bit int).
87− */
88−#ifdef SYS16BIT
89−# define MAXSEG_64K
90−#endif
91−#ifdef MSDOS
92−# define UNALIGNED_OK
93−#endif
94−
95−#ifdef __STDC_VERSION__
96−# ifndef STDC
97−# define STDC
98−# endif
99−# if __STDC_VERSION__ >= 199901L
100−# ifndef STDC99
101−# define STDC99
102−# endif
103−# endif
104−#endif
105−#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
106−# define STDC
107−#endif
108−#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
109−# define STDC
110−#endif
111−#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
112−# define STDC
113−#endif
114−#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
115−# define STDC
116−#endif
117−
118−#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
119−# define STDC
120−#endif
121−
122−#ifndef STDC
123−# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
124−# define const /* note: need a more gentle solution here */
125−# endif
126−#endif
127−
128−/* Some Mac compilers merge all .h files incorrectly: */
129−#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
130−# define NO_DUMMY_DECL
131−#endif
132−
133−/* Maximum value for memLevel in deflateInit2 */
134−#ifndef MAX_MEM_LEVEL
135−# ifdef MAXSEG_64K
136−# define MAX_MEM_LEVEL 8
137−# else
138−# define MAX_MEM_LEVEL 9
139−# endif
140−#endif
141−
142−/* Maximum value for windowBits in deflateInit2 and inflateInit2.
143− * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
144− * created by gzip. (Files created by minigzip can still be extracted by
145− * gzip.)
146− */
147−#ifndef MAX_WBITS
148−# define MAX_WBITS 15 /* 32K LZ77 window */
149−#endif
150−
151−/* The memory requirements for deflate are (in bytes):
152− (1 << (windowBits+2)) + (1 << (memLevel+9))
153− that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
154− plus a few kilobytes for small objects. For example, if you want to reduce
155− the default memory requirements from 256K to 128K, compile with
156− make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
157− Of course this will generally degrade compression (there's no free lunch).
158−
159− The memory requirements for inflate are (in bytes) 1 << windowBits
160− that is, 32K for windowBits=15 (default value) plus a few kilobytes
161− for small objects.
162−*/
163−
164− /* Type declarations */
165−
166−#ifndef OF /* function prototypes */
167−# ifdef STDC
168−# define OF(args) args
169−# else
170−# define OF(args) ()
171−# endif
172−#endif
173−
174−/* The following definitions for FAR are needed only for MSDOS mixed
175− * model programming (small or medium model with some far allocations).
176− * This was tested only with MSC; for other MSDOS compilers you may have
177− * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
178− * just define FAR to be empty.
179− */
180−#ifdef SYS16BIT
181−# if defined(M_I86SM) || defined(M_I86MM)
182− /* MSC small or medium model */
183−# define SMALL_MEDIUM
184−# ifdef _MSC_VER
185−# define FAR _far
186−# else
187−# define FAR far
188−# endif
189−# endif
190−# if (defined(__SMALL__) || defined(__MEDIUM__))
191− /* Turbo C small or medium model */
192−# define SMALL_MEDIUM
193−# ifdef __BORLANDC__
194−# define FAR _far
195−# else
196−# define FAR far
197−# endif
198−# endif
199−#endif
200−
201−#if defined(WINDOWS) || defined(WIN32)
202− /* If building or using zlib as a DLL, define ZLIB_DLL.
203− * This is not mandatory, but it offers a little performance increase.
204− */
205−# ifdef ZLIB_DLL
206−# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
207−# ifdef ZLIB_INTERNAL
208−# define ZEXTERN extern __declspec(dllexport)
209−# else
210−# define ZEXTERN extern __declspec(dllimport)
211−# endif
212−# endif
213−# endif /* ZLIB_DLL */
214− /* If building or using zlib with the WINAPI/WINAPIV calling convention,
215− * define ZLIB_WINAPI.
216− * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
217− */
218−# ifdef ZLIB_WINAPI
219−# ifdef FAR
220−# undef FAR
221−# endif
222−# include <windows.h>
223− /* No need for _export, use ZLIB.DEF instead. */
224− /* For complete Windows compatibility, use WINAPI, not __stdcall. */
225−# define ZEXPORT WINAPI
226−# ifdef WIN32
227−# define ZEXPORTVA WINAPIV
228−# else
229−# define ZEXPORTVA FAR CDECL
230−# endif
231−# endif
232−#endif
233−
234−#if defined (__BEOS__)
235−# ifdef ZLIB_DLL
236−# ifdef ZLIB_INTERNAL
237−# define ZEXPORT __declspec(dllexport)
238−# define ZEXPORTVA __declspec(dllexport)
239−# else
240−# define ZEXPORT __declspec(dllimport)
241−# define ZEXPORTVA __declspec(dllimport)
242−# endif
243−# endif
244−#endif
245−
246−#ifndef ZEXTERN
247−# define ZEXTERN extern
248−#endif
249−#ifndef ZEXPORT
250−# define ZEXPORT
251−#endif
252−#ifndef ZEXPORTVA
253−# define ZEXPORTVA
254−#endif
255−
256−#ifndef FAR
257−# define FAR
258−#endif
259−
260−#if !defined(__MACTYPES__)
261−typedef unsigned char Byte; /* 8 bits */
262−#endif
263−typedef unsigned int uInt; /* 16 bits or more */
264−typedef unsigned long uLong; /* 32 bits or more */
265−
266−#ifdef SMALL_MEDIUM
267− /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
268−# define Bytef Byte FAR
269−#else
270− typedef Byte FAR Bytef;
271−#endif
272−typedef char FAR charf;
273−typedef int FAR intf;
274−typedef uInt FAR uIntf;
275−typedef uLong FAR uLongf;
276−
277−#ifdef STDC
278− typedef void const *voidpc;
279− typedef void FAR *voidpf;
280− typedef void *voidp;
281−#else
282− typedef Byte const *voidpc;
283− typedef Byte FAR *voidpf;
284− typedef Byte *voidp;
285−#endif
286−
287−#if 1 /* HAVE_UNISTD_H -- this line is updated by ./configure */
288−# include <sys/types.h> /* for off_t */
289−# include <unistd.h> /* for SEEK_* and off_t */
290−# ifdef VMS
291−# include <unixio.h> /* for off_t */
292−# endif
293−# define z_off_t off_t
294−#endif
295−#ifndef SEEK_SET
296−# define SEEK_SET 0 /* Seek from beginning of file. */
297−# define SEEK_CUR 1 /* Seek from current position. */
298−# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
299−#endif
300−#ifndef z_off_t
301−# define z_off_t long
302−#endif
303−
304−#if defined(__OS400__)
305−# define NO_vsnprintf
306−#endif
307−
308−#if defined(__MVS__)
309−# define NO_vsnprintf
310−# ifdef FAR
311−# undef FAR
312−# endif
313−#endif
314−
315−/* MVS linker does not support external names larger than 8 bytes */
316−#if defined(__MVS__)
317−# pragma map(deflateInit_,"DEIN")
318−# pragma map(deflateInit2_,"DEIN2")
319−# pragma map(deflateEnd,"DEEND")
320−# pragma map(deflateBound,"DEBND")
321−# pragma map(inflateInit_,"ININ")
322−# pragma map(inflateInit2_,"ININ2")
323−# pragma map(inflateEnd,"INEND")
324−# pragma map(inflateSync,"INSY")
325−# pragma map(inflateSetDictionary,"INSEDI")
326−# pragma map(compressBound,"CMBND")
327−# pragma map(inflate_table,"INTABL")
328−# pragma map(inflate_fast,"INFA")
329−# pragma map(inflate_copyright,"INCOPY")
330−#endif
331−
332−#endif /* ZCONF_H */
cbits/zlib.hdeleted−1357
@@ -1,1357 +0,0 @@
1−/* zlib.h -- interface of the 'zlib' general purpose compression library
2− version 1.2.3, July 18th, 2005
3−
4− Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
5−
6− This software is provided 'as-is', without any express or implied
7− warranty. In no event will the authors be held liable for any damages
8− arising from the use of this software.
9−
10− Permission is granted to anyone to use this software for any purpose,
11− including commercial applications, and to alter it and redistribute it
12− freely, subject to the following restrictions:
13−
14− 1. The origin of this software must not be misrepresented; you must not
15− claim that you wrote the original software. If you use this software
16− in a product, an acknowledgment in the product documentation would be
17− appreciated but is not required.
18− 2. Altered source versions must be plainly marked as such, and must not be
19− misrepresented as being the original software.
20− 3. This notice may not be removed or altered from any source distribution.
21−
22− Jean-loup Gailly Mark Adler
23− jloup@gzip.org madler@alumni.caltech.edu
24−
25−
26− The data format used by the zlib library is described by RFCs (Request for
27− Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
28− (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
29−*/
30−
31−#ifndef ZLIB_H
32−#define ZLIB_H
33−
34−#include "zconf.h"
35−
36−#ifdef __cplusplus
37−extern "C" {
38−#endif
39−
40−#define ZLIB_VERSION "1.2.3"
41−#define ZLIB_VERNUM 0x1230
42−
43−/*
44− The 'zlib' compression library provides in-memory compression and
45− decompression functions, including integrity checks of the uncompressed
46− data. This version of the library supports only one compression method
47− (deflation) but other algorithms will be added later and will have the same
48− stream interface.
49−
50− Compression can be done in a single step if the buffers are large
51− enough (for example if an input file is mmap'ed), or can be done by
52− repeated calls of the compression function. In the latter case, the
53− application must provide more input and/or consume the output
54− (providing more output space) before each call.
55−
56− The compressed data format used by default by the in-memory functions is
57− the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
58− around a deflate stream, which is itself documented in RFC 1951.
59−
60− The library also supports reading and writing files in gzip (.gz) format
61− with an interface similar to that of stdio using the functions that start
62− with "gz". The gzip format is different from the zlib format. gzip is a
63− gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
64−
65− This library can optionally read and write gzip streams in memory as well.
66−
67− The zlib format was designed to be compact and fast for use in memory
68− and on communications channels. The gzip format was designed for single-
69− file compression on file systems, has a larger header than zlib to maintain
70− directory information, and uses a different, slower check method than zlib.
71−
72− The library does not install any signal handler. The decoder checks
73− the consistency of the compressed data, so the library should never
74− crash even in case of corrupted input.
75−*/
76−
77−typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
78−typedef void (*free_func) OF((voidpf opaque, voidpf address));
79−
80−struct internal_state;
81−
82−typedef struct z_stream_s {
83− Bytef *next_in; /* next input byte */
84− uInt avail_in; /* number of bytes available at next_in */
85− uLong total_in; /* total nb of input bytes read so far */
86−
87− Bytef *next_out; /* next output byte should be put there */
88− uInt avail_out; /* remaining free space at next_out */
89− uLong total_out; /* total nb of bytes output so far */
90−
91− char *msg; /* last error message, NULL if no error */
92− struct internal_state FAR *state; /* not visible by applications */
93−
94− alloc_func zalloc; /* used to allocate the internal state */
95− free_func zfree; /* used to free the internal state */
96− voidpf opaque; /* private data object passed to zalloc and zfree */
97−
98− int data_type; /* best guess about the data type: binary or text */
99− uLong adler; /* adler32 value of the uncompressed data */
100− uLong reserved; /* reserved for future use */
101−} z_stream;
102−
103−typedef z_stream FAR *z_streamp;
104−
105−/*
106− gzip header information passed to and from zlib routines. See RFC 1952
107− for more details on the meanings of these fields.
108−*/
109−typedef struct gz_header_s {
110− int text; /* true if compressed data believed to be text */
111− uLong time; /* modification time */
112− int xflags; /* extra flags (not used when writing a gzip file) */
113− int os; /* operating system */
114− Bytef *extra; /* pointer to extra field or Z_NULL if none */
115− uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
116− uInt extra_max; /* space at extra (only when reading header) */
117− Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
118− uInt name_max; /* space at name (only when reading header) */
119− Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
120− uInt comm_max; /* space at comment (only when reading header) */
121− int hcrc; /* true if there was or will be a header crc */
122− int done; /* true when done reading gzip header (not used
123− when writing a gzip file) */
124−} gz_header;
125−
126−typedef gz_header FAR *gz_headerp;
127−
128−/*
129− The application must update next_in and avail_in when avail_in has
130− dropped to zero. It must update next_out and avail_out when avail_out
131− has dropped to zero. The application must initialize zalloc, zfree and
132− opaque before calling the init function. All other fields are set by the
133− compression library and must not be updated by the application.
134−
135− The opaque value provided by the application will be passed as the first
136− parameter for calls of zalloc and zfree. This can be useful for custom
137− memory management. The compression library attaches no meaning to the
138− opaque value.
139−
140− zalloc must return Z_NULL if there is not enough memory for the object.
141− If zlib is used in a multi-threaded application, zalloc and zfree must be
142− thread safe.
143−
144− On 16-bit systems, the functions zalloc and zfree must be able to allocate
145− exactly 65536 bytes, but will not be required to allocate more than this
146− if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
147− pointers returned by zalloc for objects of exactly 65536 bytes *must*
148− have their offset normalized to zero. The default allocation function
149− provided by this library ensures this (see zutil.c). To reduce memory
150− requirements and avoid any allocation of 64K objects, at the expense of
151− compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
152−
153− The fields total_in and total_out can be used for statistics or
154− progress reports. After compression, total_in holds the total size of
155− the uncompressed data and may be saved for use in the decompressor
156− (particularly if the decompressor wants to decompress everything in
157− a single step).
158−*/
159−
160− /* constants */
161−
162−#define Z_NO_FLUSH 0
163−#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
164−#define Z_SYNC_FLUSH 2
165−#define Z_FULL_FLUSH 3
166−#define Z_FINISH 4
167−#define Z_BLOCK 5
168−/* Allowed flush values; see deflate() and inflate() below for details */
169−
170−#define Z_OK 0
171−#define Z_STREAM_END 1
172−#define Z_NEED_DICT 2
173−#define Z_ERRNO (-1)
174−#define Z_STREAM_ERROR (-2)
175−#define Z_DATA_ERROR (-3)
176−#define Z_MEM_ERROR (-4)
177−#define Z_BUF_ERROR (-5)
178−#define Z_VERSION_ERROR (-6)
179−/* Return codes for the compression/decompression functions. Negative
180− * values are errors, positive values are used for special but normal events.
181− */
182−
183−#define Z_NO_COMPRESSION 0
184−#define Z_BEST_SPEED 1
185−#define Z_BEST_COMPRESSION 9
186−#define Z_DEFAULT_COMPRESSION (-1)
187−/* compression levels */
188−
189−#define Z_FILTERED 1
190−#define Z_HUFFMAN_ONLY 2
191−#define Z_RLE 3
192−#define Z_FIXED 4
193−#define Z_DEFAULT_STRATEGY 0
194−/* compression strategy; see deflateInit2() below for details */
195−
196−#define Z_BINARY 0
197−#define Z_TEXT 1
198−#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
199−#define Z_UNKNOWN 2
200−/* Possible values of the data_type field (though see inflate()) */
201−
202−#define Z_DEFLATED 8
203−/* The deflate compression method (the only one supported in this version) */
204−
205−#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
206−
207−#define zlib_version zlibVersion()
208−/* for compatibility with versions < 1.0.2 */
209−
210− /* basic functions */
211−
212−ZEXTERN const char * ZEXPORT zlibVersion OF((void));
213−/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
214− If the first character differs, the library code actually used is
215− not compatible with the zlib.h header file used by the application.
216− This check is automatically made by deflateInit and inflateInit.
217− */
218−
219−/*
220−ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
221−
222− Initializes the internal stream state for compression. The fields
223− zalloc, zfree and opaque must be initialized before by the caller.
224− If zalloc and zfree are set to Z_NULL, deflateInit updates them to
225− use default allocation functions.
226−
227− The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
228− 1 gives best speed, 9 gives best compression, 0 gives no compression at
229− all (the input data is simply copied a block at a time).
230− Z_DEFAULT_COMPRESSION requests a default compromise between speed and
231− compression (currently equivalent to level 6).
232−
233− deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
234− enough memory, Z_STREAM_ERROR if level is not a valid compression level,
235− Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
236− with the version assumed by the caller (ZLIB_VERSION).
237− msg is set to null if there is no error message. deflateInit does not
238− perform any compression: this will be done by deflate().
239−*/
240−
241−
242−ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
243−/*
244− deflate compresses as much data as possible, and stops when the input
245− buffer becomes empty or the output buffer becomes full. It may introduce some
246− output latency (reading input without producing any output) except when
247− forced to flush.
248−
249− The detailed semantics are as follows. deflate performs one or both of the
250− following actions:
251−
252− - Compress more input starting at next_in and update next_in and avail_in
253− accordingly. If not all input can be processed (because there is not
254− enough room in the output buffer), next_in and avail_in are updated and
255− processing will resume at this point for the next call of deflate().
256−
257− - Provide more output starting at next_out and update next_out and avail_out
258− accordingly. This action is forced if the parameter flush is non zero.
259− Forcing flush frequently degrades the compression ratio, so this parameter
260− should be set only when necessary (in interactive applications).
261− Some output may be provided even if flush is not set.
262−
263− Before the call of deflate(), the application should ensure that at least
264− one of the actions is possible, by providing more input and/or consuming
265− more output, and updating avail_in or avail_out accordingly; avail_out
266− should never be zero before the call. The application can consume the
267− compressed output when it wants, for example when the output buffer is full
268− (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
269− and with zero avail_out, it must be called again after making room in the
270− output buffer because there might be more output pending.
271−
272− Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
273− decide how much data to accumualte before producing output, in order to
274− maximize compression.
275−
276− If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
277− flushed to the output buffer and the output is aligned on a byte boundary, so
278− that the decompressor can get all input data available so far. (In particular
279− avail_in is zero after the call if enough output space has been provided
280− before the call.) Flushing may degrade compression for some compression
281− algorithms and so it should be used only when necessary.
282−
283− If flush is set to Z_FULL_FLUSH, all output is flushed as with
284− Z_SYNC_FLUSH, and the compression state is reset so that decompression can
285− restart from this point if previous compressed data has been damaged or if
286− random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
287− compression.
288−
289− If deflate returns with avail_out == 0, this function must be called again
290− with the same value of the flush parameter and more output space (updated
291− avail_out), until the flush is complete (deflate returns with non-zero
292− avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
293− avail_out is greater than six to avoid repeated flush markers due to
294− avail_out == 0 on return.
295−
296− If the parameter flush is set to Z_FINISH, pending input is processed,
297− pending output is flushed and deflate returns with Z_STREAM_END if there
298− was enough output space; if deflate returns with Z_OK, this function must be
299− called again with Z_FINISH and more output space (updated avail_out) but no
300− more input data, until it returns with Z_STREAM_END or an error. After
301− deflate has returned Z_STREAM_END, the only possible operations on the
302− stream are deflateReset or deflateEnd.
303−
304− Z_FINISH can be used immediately after deflateInit if all the compression
305− is to be done in a single step. In this case, avail_out must be at least
306− the value returned by deflateBound (see below). If deflate does not return
307− Z_STREAM_END, then it must be called again as described above.
308−
309− deflate() sets strm->adler to the adler32 checksum of all input read
310− so far (that is, total_in bytes).
311−
312− deflate() may update strm->data_type if it can make a good guess about
313− the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
314− binary. This field is only for information purposes and does not affect
315− the compression algorithm in any manner.
316−
317− deflate() returns Z_OK if some progress has been made (more input
318− processed or more output produced), Z_STREAM_END if all input has been
319− consumed and all output has been produced (only when flush is set to
320− Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
321− if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
322− (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
323− fatal, and deflate() can be called again with more input and more output
324− space to continue compressing.
325−*/
326−
327−
328−ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
329−/*
330− All dynamically allocated data structures for this stream are freed.
331− This function discards any unprocessed input and does not flush any
332− pending output.
333−
334− deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
335− stream state was inconsistent, Z_DATA_ERROR if the stream was freed
336− prematurely (some input or output was discarded). In the error case,
337− msg may be set but then points to a static string (which must not be
338− deallocated).
339−*/
340−
341−
342−/*
343−ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
344−
345− Initializes the internal stream state for decompression. The fields
346− next_in, avail_in, zalloc, zfree and opaque must be initialized before by
347− the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
348− value depends on the compression method), inflateInit determines the
349− compression method from the zlib header and allocates all data structures
350− accordingly; otherwise the allocation will be deferred to the first call of
351− inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
352− use default allocation functions.
353−
354− inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
355− memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
356− version assumed by the caller. msg is set to null if there is no error
357− message. inflateInit does not perform any decompression apart from reading
358− the zlib header if present: this will be done by inflate(). (So next_in and
359− avail_in may be modified, but next_out and avail_out are unchanged.)
360−*/
361−
362−
363−ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
364−/*
365− inflate decompresses as much data as possible, and stops when the input
366− buffer becomes empty or the output buffer becomes full. It may introduce
367− some output latency (reading input without producing any output) except when
368− forced to flush.
369−
370− The detailed semantics are as follows. inflate performs one or both of the
371− following actions:
372−
373− - Decompress more input starting at next_in and update next_in and avail_in
374− accordingly. If not all input can be processed (because there is not
375− enough room in the output buffer), next_in is updated and processing
376− will resume at this point for the next call of inflate().
377−
378− - Provide more output starting at next_out and update next_out and avail_out
379− accordingly. inflate() provides as much output as possible, until there
380− is no more input data or no more space in the output buffer (see below
381− about the flush parameter).
382−
383− Before the call of inflate(), the application should ensure that at least
384− one of the actions is possible, by providing more input and/or consuming
385− more output, and updating the next_* and avail_* values accordingly.
386− The application can consume the uncompressed output when it wants, for
387− example when the output buffer is full (avail_out == 0), or after each
388− call of inflate(). If inflate returns Z_OK and with zero avail_out, it
389− must be called again after making room in the output buffer because there
390− might be more output pending.
391−
392− The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
393− Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
394− output as possible to the output buffer. Z_BLOCK requests that inflate() stop
395− if and when it gets to the next deflate block boundary. When decoding the
396− zlib or gzip format, this will cause inflate() to return immediately after
397− the header and before the first block. When doing a raw inflate, inflate()
398− will go ahead and process the first block, and will return when it gets to
399− the end of that block, or when it runs out of data.
400−
401− The Z_BLOCK option assists in appending to or combining deflate streams.
402− Also to assist in this, on return inflate() will set strm->data_type to the
403− number of unused bits in the last byte taken from strm->next_in, plus 64
404− if inflate() is currently decoding the last block in the deflate stream,
405− plus 128 if inflate() returned immediately after decoding an end-of-block
406− code or decoding the complete header up to just before the first byte of the
407− deflate stream. The end-of-block will not be indicated until all of the
408− uncompressed data from that block has been written to strm->next_out. The
409− number of unused bits may in general be greater than seven, except when
410− bit 7 of data_type is set, in which case the number of unused bits will be
411− less than eight.
412−
413− inflate() should normally be called until it returns Z_STREAM_END or an
414− error. However if all decompression is to be performed in a single step
415− (a single call of inflate), the parameter flush should be set to
416− Z_FINISH. In this case all pending input is processed and all pending
417− output is flushed; avail_out must be large enough to hold all the
418− uncompressed data. (The size of the uncompressed data may have been saved
419− by the compressor for this purpose.) The next operation on this stream must
420− be inflateEnd to deallocate the decompression state. The use of Z_FINISH
421− is never required, but can be used to inform inflate that a faster approach
422− may be used for the single inflate() call.
423−
424− In this implementation, inflate() always flushes as much output as
425− possible to the output buffer, and always uses the faster approach on the
426− first call. So the only effect of the flush parameter in this implementation
427− is on the return value of inflate(), as noted below, or when it returns early
428− because Z_BLOCK is used.
429−
430− If a preset dictionary is needed after this call (see inflateSetDictionary
431− below), inflate sets strm->adler to the adler32 checksum of the dictionary
432− chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
433− strm->adler to the adler32 checksum of all output produced so far (that is,
434− total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
435− below. At the end of the stream, inflate() checks that its computed adler32
436− checksum is equal to that saved by the compressor and returns Z_STREAM_END
437− only if the checksum is correct.
438−
439− inflate() will decompress and check either zlib-wrapped or gzip-wrapped
440− deflate data. The header type is detected automatically. Any information
441− contained in the gzip header is not retained, so applications that need that
442− information should instead use raw inflate, see inflateInit2() below, or
443− inflateBack() and perform their own processing of the gzip header and
444− trailer.
445−
446− inflate() returns Z_OK if some progress has been made (more input processed
447− or more output produced), Z_STREAM_END if the end of the compressed data has
448− been reached and all uncompressed output has been produced, Z_NEED_DICT if a
449− preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
450− corrupted (input stream not conforming to the zlib format or incorrect check
451− value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
452− if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
453− Z_BUF_ERROR if no progress is possible or if there was not enough room in the
454− output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
455− inflate() can be called again with more input and more output space to
456− continue decompressing. If Z_DATA_ERROR is returned, the application may then
457− call inflateSync() to look for a good compression block if a partial recovery
458− of the data is desired.
459−*/
460−
461−
462−ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
463−/*
464− All dynamically allocated data structures for this stream are freed.
465− This function discards any unprocessed input and does not flush any
466− pending output.
467−
468− inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
469− was inconsistent. In the error case, msg may be set but then points to a
470− static string (which must not be deallocated).
471−*/
472−
473− /* Advanced functions */
474−
475−/*
476− The following functions are needed only in some special applications.
477−*/
478−
479−/*
480−ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
481− int level,
482− int method,
483− int windowBits,
484− int memLevel,
485− int strategy));
486−
487− This is another version of deflateInit with more compression options. The
488− fields next_in, zalloc, zfree and opaque must be initialized before by
489− the caller.
490−
491− The method parameter is the compression method. It must be Z_DEFLATED in
492− this version of the library.
493−
494− The windowBits parameter is the base two logarithm of the window size
495− (the size of the history buffer). It should be in the range 8..15 for this
496− version of the library. Larger values of this parameter result in better
497− compression at the expense of memory usage. The default value is 15 if
498− deflateInit is used instead.
499−
500− windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
501− determines the window size. deflate() will then generate raw deflate data
502− with no zlib header or trailer, and will not compute an adler32 check value.
503−
504− windowBits can also be greater than 15 for optional gzip encoding. Add
505− 16 to windowBits to write a simple gzip header and trailer around the
506− compressed data instead of a zlib wrapper. The gzip header will have no
507− file name, no extra data, no comment, no modification time (set to zero),
508− no header crc, and the operating system will be set to 255 (unknown). If a
509− gzip stream is being written, strm->adler is a crc32 instead of an adler32.
510−
511− The memLevel parameter specifies how much memory should be allocated
512− for the internal compression state. memLevel=1 uses minimum memory but
513− is slow and reduces compression ratio; memLevel=9 uses maximum memory
514− for optimal speed. The default value is 8. See zconf.h for total memory
515− usage as a function of windowBits and memLevel.
516−
517− The strategy parameter is used to tune the compression algorithm. Use the
518− value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
519− filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
520− string match), or Z_RLE to limit match distances to one (run-length
521− encoding). Filtered data consists mostly of small values with a somewhat
522− random distribution. In this case, the compression algorithm is tuned to
523− compress them better. The effect of Z_FILTERED is to force more Huffman
524− coding and less string matching; it is somewhat intermediate between
525− Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
526− Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
527− parameter only affects the compression ratio but not the correctness of the
528− compressed output even if it is not set appropriately. Z_FIXED prevents the
529− use of dynamic Huffman codes, allowing for a simpler decoder for special
530− applications.
531−
532− deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
533− memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
534− method). msg is set to null if there is no error message. deflateInit2 does
535− not perform any compression: this will be done by deflate().
536−*/
537−
538−ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
539− const Bytef *dictionary,
540− uInt dictLength));
541−/*
542− Initializes the compression dictionary from the given byte sequence
543− without producing any compressed output. This function must be called
544− immediately after deflateInit, deflateInit2 or deflateReset, before any
545− call of deflate. The compressor and decompressor must use exactly the same
546− dictionary (see inflateSetDictionary).
547−
548− The dictionary should consist of strings (byte sequences) that are likely
549− to be encountered later in the data to be compressed, with the most commonly
550− used strings preferably put towards the end of the dictionary. Using a
551− dictionary is most useful when the data to be compressed is short and can be
552− predicted with good accuracy; the data can then be compressed better than
553− with the default empty dictionary.
554−
555− Depending on the size of the compression data structures selected by
556− deflateInit or deflateInit2, a part of the dictionary may in effect be
557− discarded, for example if the dictionary is larger than the window size in
558− deflate or deflate2. Thus the strings most likely to be useful should be
559− put at the end of the dictionary, not at the front. In addition, the
560− current implementation of deflate will use at most the window size minus
561− 262 bytes of the provided dictionary.
562−
563− Upon return of this function, strm->adler is set to the adler32 value
564− of the dictionary; the decompressor may later use this value to determine
565− which dictionary has been used by the compressor. (The adler32 value
566− applies to the whole dictionary even if only a subset of the dictionary is
567− actually used by the compressor.) If a raw deflate was requested, then the
568− adler32 value is not computed and strm->adler is not set.
569−
570− deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
571− parameter is invalid (such as NULL dictionary) or the stream state is
572− inconsistent (for example if deflate has already been called for this stream
573− or if the compression method is bsort). deflateSetDictionary does not
574− perform any compression: this will be done by deflate().
575−*/
576−
577−ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
578− z_streamp source));
579−/*
580− Sets the destination stream as a complete copy of the source stream.
581−
582− This function can be useful when several compression strategies will be
583− tried, for example when there are several ways of pre-processing the input
584− data with a filter. The streams that will be discarded should then be freed
585− by calling deflateEnd. Note that deflateCopy duplicates the internal
586− compression state which can be quite large, so this strategy is slow and
587− can consume lots of memory.
588−
589− deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
590− enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
591− (such as zalloc being NULL). msg is left unchanged in both source and
592− destination.
593−*/
594−
595−ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
596−/*
597− This function is equivalent to deflateEnd followed by deflateInit,
598− but does not free and reallocate all the internal compression state.
599− The stream will keep the same compression level and any other attributes
600− that may have been set by deflateInit2.
601−
602− deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
603− stream state was inconsistent (such as zalloc or state being NULL).
604−*/
605−
606−ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
607− int level,
608− int strategy));
609−/*
610− Dynamically update the compression level and compression strategy. The
611− interpretation of level and strategy is as in deflateInit2. This can be
612− used to switch between compression and straight copy of the input data, or
613− to switch to a different kind of input data requiring a different
614− strategy. If the compression level is changed, the input available so far
615− is compressed with the old level (and may be flushed); the new level will
616− take effect only at the next call of deflate().
617−
618− Before the call of deflateParams, the stream state must be set as for
619− a call of deflate(), since the currently available input may have to
620− be compressed and flushed. In particular, strm->avail_out must be non-zero.
621−
622− deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
623− stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
624− if strm->avail_out was zero.
625−*/
626−
627−ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
628− int good_length,
629− int max_lazy,
630− int nice_length,
631− int max_chain));
632−/*
633− Fine tune deflate's internal compression parameters. This should only be
634− used by someone who understands the algorithm used by zlib's deflate for
635− searching for the best matching string, and even then only by the most
636− fanatic optimizer trying to squeeze out the last compressed bit for their
637− specific input data. Read the deflate.c source code for the meaning of the
638− max_lazy, good_length, nice_length, and max_chain parameters.
639−
640− deflateTune() can be called after deflateInit() or deflateInit2(), and
641− returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
642− */
643−
644−ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
645− uLong sourceLen));
646−/*
647− deflateBound() returns an upper bound on the compressed size after
648− deflation of sourceLen bytes. It must be called after deflateInit()
649− or deflateInit2(). This would be used to allocate an output buffer
650− for deflation in a single pass, and so would be called before deflate().
651−*/
652−
653−ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
654− int bits,
655− int value));
656−/*
657− deflatePrime() inserts bits in the deflate output stream. The intent
658− is that this function is used to start off the deflate output with the
659− bits leftover from a previous deflate stream when appending to it. As such,
660− this function can only be used for raw deflate, and must be used before the
661− first deflate() call after a deflateInit2() or deflateReset(). bits must be
662− less than or equal to 16, and that many of the least significant bits of
663− value will be inserted in the output.
664−
665− deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
666− stream state was inconsistent.
667−*/
668−
669−ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
670− gz_headerp head));
671−/*
672− deflateSetHeader() provides gzip header information for when a gzip
673− stream is requested by deflateInit2(). deflateSetHeader() may be called
674− after deflateInit2() or deflateReset() and before the first call of
675− deflate(). The text, time, os, extra field, name, and comment information
676− in the provided gz_header structure are written to the gzip header (xflag is
677− ignored -- the extra flags are set according to the compression level). The
678− caller must assure that, if not Z_NULL, name and comment are terminated with
679− a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
680− available there. If hcrc is true, a gzip header crc is included. Note that
681− the current versions of the command-line version of gzip (up through version
682− 1.3.x) do not support header crc's, and will report that it is a "multi-part
683− gzip file" and give up.
684−
685− If deflateSetHeader is not used, the default gzip header has text false,
686− the time set to zero, and os set to 255, with no extra, name, or comment
687− fields. The gzip header is returned to the default state by deflateReset().
688−
689− deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
690− stream state was inconsistent.
691−*/
692−
693−/*
694−ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
695− int windowBits));
696−
697− This is another version of inflateInit with an extra parameter. The
698− fields next_in, avail_in, zalloc, zfree and opaque must be initialized
699− before by the caller.
700−
701− The windowBits parameter is the base two logarithm of the maximum window
702− size (the size of the history buffer). It should be in the range 8..15 for
703− this version of the library. The default value is 15 if inflateInit is used
704− instead. windowBits must be greater than or equal to the windowBits value
705− provided to deflateInit2() while compressing, or it must be equal to 15 if
706− deflateInit2() was not used. If a compressed stream with a larger window
707− size is given as input, inflate() will return with the error code
708− Z_DATA_ERROR instead of trying to allocate a larger window.
709−
710− windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
711− determines the window size. inflate() will then process raw deflate data,
712− not looking for a zlib or gzip header, not generating a check value, and not
713− looking for any check values for comparison at the end of the stream. This
714− is for use with other formats that use the deflate compressed data format
715− such as zip. Those formats provide their own check values. If a custom
716− format is developed using the raw deflate format for compressed data, it is
717− recommended that a check value such as an adler32 or a crc32 be applied to
718− the uncompressed data as is done in the zlib, gzip, and zip formats. For
719− most applications, the zlib format should be used as is. Note that comments
720− above on the use in deflateInit2() applies to the magnitude of windowBits.
721−
722− windowBits can also be greater than 15 for optional gzip decoding. Add
723− 32 to windowBits to enable zlib and gzip decoding with automatic header
724− detection, or add 16 to decode only the gzip format (the zlib format will
725− return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is
726− a crc32 instead of an adler32.
727−
728− inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
729− memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
730− is set to null if there is no error message. inflateInit2 does not perform
731− any decompression apart from reading the zlib header if present: this will
732− be done by inflate(). (So next_in and avail_in may be modified, but next_out
733− and avail_out are unchanged.)
734−*/
735−
736−ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
737− const Bytef *dictionary,
738− uInt dictLength));
739−/*
740− Initializes the decompression dictionary from the given uncompressed byte
741− sequence. This function must be called immediately after a call of inflate,
742− if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
743− can be determined from the adler32 value returned by that call of inflate.
744− The compressor and decompressor must use exactly the same dictionary (see
745− deflateSetDictionary). For raw inflate, this function can be called
746− immediately after inflateInit2() or inflateReset() and before any call of
747− inflate() to set the dictionary. The application must insure that the
748− dictionary that was used for compression is provided.
749−
750− inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
751− parameter is invalid (such as NULL dictionary) or the stream state is
752− inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
753− expected one (incorrect adler32 value). inflateSetDictionary does not
754− perform any decompression: this will be done by subsequent calls of
755− inflate().
756−*/
757−
758−ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
759−/*
760− Skips invalid compressed data until a full flush point (see above the
761− description of deflate with Z_FULL_FLUSH) can be found, or until all
762− available input is skipped. No output is provided.
763−
764− inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
765− if no more input was provided, Z_DATA_ERROR if no flush point has been found,
766− or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
767− case, the application may save the current current value of total_in which
768− indicates where valid compressed data was found. In the error case, the
769− application may repeatedly call inflateSync, providing more input each time,
770− until success or end of the input data.
771−*/
772−
773−ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
774− z_streamp source));
775−/*
776− Sets the destination stream as a complete copy of the source stream.
777−
778− This function can be useful when randomly accessing a large stream. The
779− first pass through the stream can periodically record the inflate state,
780− allowing restarting inflate at those points when randomly accessing the
781− stream.
782−
783− inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
784− enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
785− (such as zalloc being NULL). msg is left unchanged in both source and
786− destination.
787−*/
788−
789−ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
790−/*
791− This function is equivalent to inflateEnd followed by inflateInit,
792− but does not free and reallocate all the internal decompression state.
793− The stream will keep attributes that may have been set by inflateInit2.
794−
795− inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
796− stream state was inconsistent (such as zalloc or state being NULL).
797−*/
798−
799−ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
800− int bits,
801− int value));
802−/*
803− This function inserts bits in the inflate input stream. The intent is
804− that this function is used to start inflating at a bit position in the
805− middle of a byte. The provided bits will be used before any bytes are used
806− from next_in. This function should only be used with raw inflate, and
807− should be used before the first inflate() call after inflateInit2() or
808− inflateReset(). bits must be less than or equal to 16, and that many of the
809− least significant bits of value will be inserted in the input.
810−
811− inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
812− stream state was inconsistent.
813−*/
814−
815−ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
816− gz_headerp head));
817−/*
818− inflateGetHeader() requests that gzip header information be stored in the
819− provided gz_header structure. inflateGetHeader() may be called after
820− inflateInit2() or inflateReset(), and before the first call of inflate().
821− As inflate() processes the gzip stream, head->done is zero until the header
822− is completed, at which time head->done is set to one. If a zlib stream is
823− being decoded, then head->done is set to -1 to indicate that there will be
824− no gzip header information forthcoming. Note that Z_BLOCK can be used to
825− force inflate() to return immediately after header processing is complete
826− and before any actual data is decompressed.
827−
828− The text, time, xflags, and os fields are filled in with the gzip header
829− contents. hcrc is set to true if there is a header CRC. (The header CRC
830− was valid if done is set to one.) If extra is not Z_NULL, then extra_max
831− contains the maximum number of bytes to write to extra. Once done is true,
832− extra_len contains the actual extra field length, and extra contains the
833− extra field, or that field truncated if extra_max is less than extra_len.
834− If name is not Z_NULL, then up to name_max characters are written there,
835− terminated with a zero unless the length is greater than name_max. If
836− comment is not Z_NULL, then up to comm_max characters are written there,
837− terminated with a zero unless the length is greater than comm_max. When
838− any of extra, name, or comment are not Z_NULL and the respective field is
839− not present in the header, then that field is set to Z_NULL to signal its
840− absence. This allows the use of deflateSetHeader() with the returned
841− structure to duplicate the header. However if those fields are set to
842− allocated memory, then the application will need to save those pointers
843− elsewhere so that they can be eventually freed.
844−
845− If inflateGetHeader is not used, then the header information is simply
846− discarded. The header is always checked for validity, including the header
847− CRC if present. inflateReset() will reset the process to discard the header
848− information. The application would need to call inflateGetHeader() again to
849− retrieve the header from the next gzip stream.
850−
851− inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
852− stream state was inconsistent.
853−*/
854−
855−/*
856−ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
857− unsigned char FAR *window));
858−
859− Initialize the internal stream state for decompression using inflateBack()
860− calls. The fields zalloc, zfree and opaque in strm must be initialized
861− before the call. If zalloc and zfree are Z_NULL, then the default library-
862− derived memory allocation routines are used. windowBits is the base two
863− logarithm of the window size, in the range 8..15. window is a caller
864− supplied buffer of that size. Except for special applications where it is
865− assured that deflate was used with small window sizes, windowBits must be 15
866− and a 32K byte window must be supplied to be able to decompress general
867− deflate streams.
868−
869− See inflateBack() for the usage of these routines.
870−
871− inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
872− the paramaters are invalid, Z_MEM_ERROR if the internal state could not
873− be allocated, or Z_VERSION_ERROR if the version of the library does not
874− match the version of the header file.
875−*/
876−
877−typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
878−typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
879−
880−ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
881− in_func in, void FAR *in_desc,
882− out_func out, void FAR *out_desc));
883−/*
884− inflateBack() does a raw inflate with a single call using a call-back
885− interface for input and output. This is more efficient than inflate() for
886− file i/o applications in that it avoids copying between the output and the
887− sliding window by simply making the window itself the output buffer. This
888− function trusts the application to not change the output buffer passed by
889− the output function, at least until inflateBack() returns.
890−
891− inflateBackInit() must be called first to allocate the internal state
892− and to initialize the state with the user-provided window buffer.
893− inflateBack() may then be used multiple times to inflate a complete, raw
894− deflate stream with each call. inflateBackEnd() is then called to free
895− the allocated state.
896−
897− A raw deflate stream is one with no zlib or gzip header or trailer.
898− This routine would normally be used in a utility that reads zip or gzip
899− files and writes out uncompressed files. The utility would decode the
900− header and process the trailer on its own, hence this routine expects
901− only the raw deflate stream to decompress. This is different from the
902− normal behavior of inflate(), which expects either a zlib or gzip header and
903− trailer around the deflate stream.
904−
905− inflateBack() uses two subroutines supplied by the caller that are then
906− called by inflateBack() for input and output. inflateBack() calls those
907− routines until it reads a complete deflate stream and writes out all of the
908− uncompressed data, or until it encounters an error. The function's
909− parameters and return types are defined above in the in_func and out_func
910− typedefs. inflateBack() will call in(in_desc, &buf) which should return the
911− number of bytes of provided input, and a pointer to that input in buf. If
912− there is no input available, in() must return zero--buf is ignored in that
913− case--and inflateBack() will return a buffer error. inflateBack() will call
914− out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
915− should return zero on success, or non-zero on failure. If out() returns
916− non-zero, inflateBack() will return with an error. Neither in() nor out()
917− are permitted to change the contents of the window provided to
918− inflateBackInit(), which is also the buffer that out() uses to write from.
919− The length written by out() will be at most the window size. Any non-zero
920− amount of input may be provided by in().
921−
922− For convenience, inflateBack() can be provided input on the first call by
923− setting strm->next_in and strm->avail_in. If that input is exhausted, then
924− in() will be called. Therefore strm->next_in must be initialized before
925− calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
926− immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
927− must also be initialized, and then if strm->avail_in is not zero, input will
928− initially be taken from strm->next_in[0 .. strm->avail_in - 1].
929−
930− The in_desc and out_desc parameters of inflateBack() is passed as the
931− first parameter of in() and out() respectively when they are called. These
932− descriptors can be optionally used to pass any information that the caller-
933− supplied in() and out() functions need to do their job.
934−
935− On return, inflateBack() will set strm->next_in and strm->avail_in to
936− pass back any unused input that was provided by the last in() call. The
937− return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
938− if in() or out() returned an error, Z_DATA_ERROR if there was a format
939− error in the deflate stream (in which case strm->msg is set to indicate the
940− nature of the error), or Z_STREAM_ERROR if the stream was not properly
941− initialized. In the case of Z_BUF_ERROR, an input or output error can be
942− distinguished using strm->next_in which will be Z_NULL only if in() returned
943− an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
944− out() returning non-zero. (in() will always be called before out(), so
945− strm->next_in is assured to be defined if out() returns non-zero.) Note
946− that inflateBack() cannot return Z_OK.
947−*/
948−
949−ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
950−/*
951− All memory allocated by inflateBackInit() is freed.
952−
953− inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
954− state was inconsistent.
955−*/
956−
957−ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
958−/* Return flags indicating compile-time options.
959−
960− Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
961− 1.0: size of uInt
962− 3.2: size of uLong
963− 5.4: size of voidpf (pointer)
964− 7.6: size of z_off_t
965−
966− Compiler, assembler, and debug options:
967− 8: DEBUG
968− 9: ASMV or ASMINF -- use ASM code
969− 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
970− 11: 0 (reserved)
971−
972− One-time table building (smaller code, but not thread-safe if true):
973− 12: BUILDFIXED -- build static block decoding tables when needed
974− 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
975− 14,15: 0 (reserved)
976−
977− Library content (indicates missing functionality):
978− 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
979− deflate code when not needed)
980− 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
981− and decode gzip streams (to avoid linking crc code)
982− 18-19: 0 (reserved)
983−
984− Operation variations (changes in library functionality):
985− 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
986− 21: FASTEST -- deflate algorithm with only one, lowest compression level
987− 22,23: 0 (reserved)
988−
989− The sprintf variant used by gzprintf (zero is best):
990− 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
991− 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
992− 26: 0 = returns value, 1 = void -- 1 means inferred string length returned
993−
994− Remainder:
995− 27-31: 0 (reserved)
996− */
997−
998−
999− /* utility functions */
1000−
1001−/*
1002− The following utility functions are implemented on top of the
1003− basic stream-oriented functions. To simplify the interface, some
1004− default options are assumed (compression level and memory usage,
1005− standard memory allocation functions). The source code of these
1006− utility functions can easily be modified if you need special options.
1007−*/
1008−
1009−ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
1010− const Bytef *source, uLong sourceLen));
1011−/*
1012− Compresses the source buffer into the destination buffer. sourceLen is
1013− the byte length of the source buffer. Upon entry, destLen is the total
1014− size of the destination buffer, which must be at least the value returned
1015− by compressBound(sourceLen). Upon exit, destLen is the actual size of the
1016− compressed buffer.
1017− This function can be used to compress a whole file at once if the
1018− input file is mmap'ed.
1019− compress returns Z_OK if success, Z_MEM_ERROR if there was not
1020− enough memory, Z_BUF_ERROR if there was not enough room in the output
1021− buffer.
1022−*/
1023−
1024−ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
1025− const Bytef *source, uLong sourceLen,
1026− int level));
1027−/*
1028− Compresses the source buffer into the destination buffer. The level
1029− parameter has the same meaning as in deflateInit. sourceLen is the byte
1030− length of the source buffer. Upon entry, destLen is the total size of the
1031− destination buffer, which must be at least the value returned by
1032− compressBound(sourceLen). Upon exit, destLen is the actual size of the
1033− compressed buffer.
1034−
1035− compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1036− memory, Z_BUF_ERROR if there was not enough room in the output buffer,
1037− Z_STREAM_ERROR if the level parameter is invalid.
1038−*/
1039−
1040−ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
1041−/*
1042− compressBound() returns an upper bound on the compressed size after
1043− compress() or compress2() on sourceLen bytes. It would be used before
1044− a compress() or compress2() call to allocate the destination buffer.
1045−*/
1046−
1047−ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
1048− const Bytef *source, uLong sourceLen));
1049−/*
1050− Decompresses the source buffer into the destination buffer. sourceLen is
1051− the byte length of the source buffer. Upon entry, destLen is the total
1052− size of the destination buffer, which must be large enough to hold the
1053− entire uncompressed data. (The size of the uncompressed data must have
1054− been saved previously by the compressor and transmitted to the decompressor
1055− by some mechanism outside the scope of this compression library.)
1056− Upon exit, destLen is the actual size of the compressed buffer.
1057− This function can be used to decompress a whole file at once if the
1058− input file is mmap'ed.
1059−
1060− uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
1061− enough memory, Z_BUF_ERROR if there was not enough room in the output
1062− buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
1063−*/
1064−
1065−
1066−typedef voidp gzFile;
1067−
1068−ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
1069−/*
1070− Opens a gzip (.gz) file for reading or writing. The mode parameter
1071− is as in fopen ("rb" or "wb") but can also include a compression level
1072− ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
1073− Huffman only compression as in "wb1h", or 'R' for run-length encoding
1074− as in "wb1R". (See the description of deflateInit2 for more information
1075− about the strategy parameter.)
1076−
1077− gzopen can be used to read a file which is not in gzip format; in this
1078− case gzread will directly read from the file without decompression.
1079−
1080− gzopen returns NULL if the file could not be opened or if there was
1081− insufficient memory to allocate the (de)compression state; errno
1082− can be checked to distinguish the two cases (if errno is zero, the
1083− zlib error is Z_MEM_ERROR). */
1084−
1085−ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
1086−/*
1087− gzdopen() associates a gzFile with the file descriptor fd. File
1088− descriptors are obtained from calls like open, dup, creat, pipe or
1089− fileno (in the file has been previously opened with fopen).
1090− The mode parameter is as in gzopen.
1091− The next call of gzclose on the returned gzFile will also close the
1092− file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
1093− descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
1094− gzdopen returns NULL if there was insufficient memory to allocate
1095− the (de)compression state.
1096−*/
1097−
1098−ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
1099−/*
1100− Dynamically update the compression level or strategy. See the description
1101− of deflateInit2 for the meaning of these parameters.
1102− gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
1103− opened for writing.
1104−*/
1105−
1106−ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
1107−/*
1108− Reads the given number of uncompressed bytes from the compressed file.
1109− If the input file was not in gzip format, gzread copies the given number
1110− of bytes into the buffer.
1111− gzread returns the number of uncompressed bytes actually read (0 for
1112− end of file, -1 for error). */
1113−
1114−ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
1115− voidpc buf, unsigned len));
1116−/*
1117− Writes the given number of uncompressed bytes into the compressed file.
1118− gzwrite returns the number of uncompressed bytes actually written
1119− (0 in case of error).
1120−*/
1121−
1122−ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
1123−/*
1124− Converts, formats, and writes the args to the compressed file under
1125− control of the format string, as in fprintf. gzprintf returns the number of
1126− uncompressed bytes actually written (0 in case of error). The number of
1127− uncompressed bytes written is limited to 4095. The caller should assure that
1128− this limit is not exceeded. If it is exceeded, then gzprintf() will return
1129− return an error (0) with nothing written. In this case, there may also be a
1130− buffer overflow with unpredictable consequences, which is possible only if
1131− zlib was compiled with the insecure functions sprintf() or vsprintf()
1132− because the secure snprintf() or vsnprintf() functions were not available.
1133−*/
1134−
1135−ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
1136−/*
1137− Writes the given null-terminated string to the compressed file, excluding
1138− the terminating null character.
1139− gzputs returns the number of characters written, or -1 in case of error.
1140−*/
1141−
1142−ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
1143−/*
1144− Reads bytes from the compressed file until len-1 characters are read, or
1145− a newline character is read and transferred to buf, or an end-of-file
1146− condition is encountered. The string is then terminated with a null
1147− character.
1148− gzgets returns buf, or Z_NULL in case of error.
1149−*/
1150−
1151−ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
1152−/*
1153− Writes c, converted to an unsigned char, into the compressed file.
1154− gzputc returns the value that was written, or -1 in case of error.
1155−*/
1156−
1157−ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
1158−/*
1159− Reads one byte from the compressed file. gzgetc returns this byte
1160− or -1 in case of end of file or error.
1161−*/
1162−
1163−ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
1164−/*
1165− Push one character back onto the stream to be read again later.
1166− Only one character of push-back is allowed. gzungetc() returns the
1167− character pushed, or -1 on failure. gzungetc() will fail if a
1168− character has been pushed but not read yet, or if c is -1. The pushed
1169− character will be discarded if the stream is repositioned with gzseek()
1170− or gzrewind().
1171−*/
1172−
1173−ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
1174−/*
1175− Flushes all pending output into the compressed file. The parameter
1176− flush is as in the deflate() function. The return value is the zlib
1177− error number (see function gzerror below). gzflush returns Z_OK if
1178− the flush parameter is Z_FINISH and all output could be flushed.
1179− gzflush should be called only when strictly necessary because it can
1180− degrade compression.
1181−*/
1182−
1183−ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
1184− z_off_t offset, int whence));
1185−/*
1186− Sets the starting position for the next gzread or gzwrite on the
1187− given compressed file. The offset represents a number of bytes in the
1188− uncompressed data stream. The whence parameter is defined as in lseek(2);
1189− the value SEEK_END is not supported.
1190− If the file is opened for reading, this function is emulated but can be
1191− extremely slow. If the file is opened for writing, only forward seeks are
1192− supported; gzseek then compresses a sequence of zeroes up to the new
1193− starting position.
1194−
1195− gzseek returns the resulting offset location as measured in bytes from
1196− the beginning of the uncompressed stream, or -1 in case of error, in
1197− particular if the file is opened for writing and the new starting position
1198− would be before the current position.
1199−*/
1200−
1201−ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
1202−/*
1203− Rewinds the given file. This function is supported only for reading.
1204−
1205− gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
1206−*/
1207−
1208−ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
1209−/*
1210− Returns the starting position for the next gzread or gzwrite on the
1211− given compressed file. This position represents a number of bytes in the
1212− uncompressed data stream.
1213−
1214− gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
1215−*/
1216−
1217−ZEXTERN int ZEXPORT gzeof OF((gzFile file));
1218−/*
1219− Returns 1 when EOF has previously been detected reading the given
1220− input stream, otherwise zero.
1221−*/
1222−
1223−ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
1224−/*
1225− Returns 1 if file is being read directly without decompression, otherwise
1226− zero.
1227−*/
1228−
1229−ZEXTERN int ZEXPORT gzclose OF((gzFile file));
1230−/*
1231− Flushes all pending output if necessary, closes the compressed file
1232− and deallocates all the (de)compression state. The return value is the zlib
1233− error number (see function gzerror below).
1234−*/
1235−
1236−ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
1237−/*
1238− Returns the error message for the last error which occurred on the
1239− given compressed file. errnum is set to zlib error number. If an
1240− error occurred in the file system and not in the compression library,
1241− errnum is set to Z_ERRNO and the application may consult errno
1242− to get the exact error code.
1243−*/
1244−
1245−ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
1246−/*
1247− Clears the error and end-of-file flags for file. This is analogous to the
1248− clearerr() function in stdio. This is useful for continuing to read a gzip
1249− file that is being written concurrently.
1250−*/
1251−
1252− /* checksum functions */
1253−
1254−/*
1255− These functions are not related to compression but are exported
1256− anyway because they might be useful in applications using the
1257− compression library.
1258−*/
1259−
1260−ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
1261−/*
1262− Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1263− return the updated checksum. If buf is NULL, this function returns
1264− the required initial value for the checksum.
1265− An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
1266− much faster. Usage example:
1267−
1268− uLong adler = adler32(0L, Z_NULL, 0);
1269−
1270− while (read_buffer(buffer, length) != EOF) {
1271− adler = adler32(adler, buffer, length);
1272− }
1273− if (adler != original_adler) error();
1274−*/
1275−
1276−ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
1277− z_off_t len2));
1278−/*
1279− Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
1280− and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
1281− each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
1282− seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
1283−*/
1284−
1285−ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
1286−/*
1287− Update a running CRC-32 with the bytes buf[0..len-1] and return the
1288− updated CRC-32. If buf is NULL, this function returns the required initial
1289− value for the for the crc. Pre- and post-conditioning (one's complement) is
1290− performed within this function so it shouldn't be done by the application.
1291− Usage example:
1292−
1293− uLong crc = crc32(0L, Z_NULL, 0);
1294−
1295− while (read_buffer(buffer, length) != EOF) {
1296− crc = crc32(crc, buffer, length);
1297− }
1298− if (crc != original_crc) error();
1299−*/
1300−
1301−ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
1302−
1303−/*
1304− Combine two CRC-32 check values into one. For two sequences of bytes,
1305− seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
1306− calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
1307− check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
1308− len2.
1309−*/
1310−
1311−
1312− /* various hacks, don't look :) */
1313−
1314−/* deflateInit and inflateInit are macros to allow checking the zlib version
1315− * and the compiler's view of z_stream:
1316− */
1317−ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
1318− const char *version, int stream_size));
1319−ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
1320− const char *version, int stream_size));
1321−ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
1322− int windowBits, int memLevel,
1323− int strategy, const char *version,
1324− int stream_size));
1325−ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
1326− const char *version, int stream_size));
1327−ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
1328− unsigned char FAR *window,
1329− const char *version,
1330− int stream_size));
1331−#define deflateInit(strm, level) \
1332− deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
1333−#define inflateInit(strm) \
1334− inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
1335−#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1336− deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1337− (strategy), ZLIB_VERSION, sizeof(z_stream))
1338−#define inflateInit2(strm, windowBits) \
1339− inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
1340−#define inflateBackInit(strm, windowBits, window) \
1341− inflateBackInit_((strm), (windowBits), (window), \
1342− ZLIB_VERSION, sizeof(z_stream))
1343−
1344−
1345−#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
1346− struct internal_state {int dummy;}; /* hack for buggy compilers */
1347−#endif
1348−
1349−ZEXTERN const char * ZEXPORT zError OF((int));
1350−ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
1351−ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
1352−
1353−#ifdef __cplusplus
1354−}
1355−#endif
1356−
1357−#endif /* ZLIB_H */
cbits/zutil.cdeleted−318
@@ -1,318 +0,0 @@
1−/* zutil.c -- target dependent utility functions for the compression library
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#include "zutil.h"
9−
10−#ifndef NO_DUMMY_DECL
11−struct internal_state {int dummy;}; /* for buggy compilers */
12−#endif
13−
14−const char * const z_errmsg[10] = {
15−"need dictionary", /* Z_NEED_DICT 2 */
16−"stream end", /* Z_STREAM_END 1 */
17−"", /* Z_OK 0 */
18−"file error", /* Z_ERRNO (-1) */
19−"stream error", /* Z_STREAM_ERROR (-2) */
20−"data error", /* Z_DATA_ERROR (-3) */
21−"insufficient memory", /* Z_MEM_ERROR (-4) */
22−"buffer error", /* Z_BUF_ERROR (-5) */
23−"incompatible version",/* Z_VERSION_ERROR (-6) */
24−""};
25−
26−
27−const char * ZEXPORT zlibVersion()
28−{
29− return ZLIB_VERSION;
30−}
31−
32−uLong ZEXPORT zlibCompileFlags()
33−{
34− uLong flags;
35−
36− flags = 0;
37− switch (sizeof(uInt)) {
38− case 2: break;
39− case 4: flags += 1; break;
40− case 8: flags += 2; break;
41− default: flags += 3;
42− }
43− switch (sizeof(uLong)) {
44− case 2: break;
45− case 4: flags += 1 << 2; break;
46− case 8: flags += 2 << 2; break;
47− default: flags += 3 << 2;
48− }
49− switch (sizeof(voidpf)) {
50− case 2: break;
51− case 4: flags += 1 << 4; break;
52− case 8: flags += 2 << 4; break;
53− default: flags += 3 << 4;
54− }
55− switch (sizeof(z_off_t)) {
56− case 2: break;
57− case 4: flags += 1 << 6; break;
58− case 8: flags += 2 << 6; break;
59− default: flags += 3 << 6;
60− }
61−#ifdef DEBUG
62− flags += 1 << 8;
63−#endif
64−#if defined(ASMV) || defined(ASMINF)
65− flags += 1 << 9;
66−#endif
67−#ifdef ZLIB_WINAPI
68− flags += 1 << 10;
69−#endif
70−#ifdef BUILDFIXED
71− flags += 1 << 12;
72−#endif
73−#ifdef DYNAMIC_CRC_TABLE
74− flags += 1 << 13;
75−#endif
76−#ifdef NO_GZCOMPRESS
77− flags += 1L << 16;
78−#endif
79−#ifdef NO_GZIP
80− flags += 1L << 17;
81−#endif
82−#ifdef PKZIP_BUG_WORKAROUND
83− flags += 1L << 20;
84−#endif
85−#ifdef FASTEST
86− flags += 1L << 21;
87−#endif
88−#ifdef STDC
89−# ifdef NO_vsnprintf
90− flags += 1L << 25;
91−# ifdef HAS_vsprintf_void
92− flags += 1L << 26;
93−# endif
94−# else
95−# ifdef HAS_vsnprintf_void
96− flags += 1L << 26;
97−# endif
98−# endif
99−#else
100− flags += 1L << 24;
101−# ifdef NO_snprintf
102− flags += 1L << 25;
103−# ifdef HAS_sprintf_void
104− flags += 1L << 26;
105−# endif
106−# else
107−# ifdef HAS_snprintf_void
108− flags += 1L << 26;
109−# endif
110−# endif
111−#endif
112− return flags;
113−}
114−
115−#ifdef DEBUG
116−
117−# ifndef verbose
118−# define verbose 0
119−# endif
120−int z_verbose = verbose;
121−
122−void z_error (m)
123− char *m;
124−{
125− fprintf(stderr, "%s\n", m);
126− exit(1);
127−}
128−#endif
129−
130−/* exported to allow conversion of error code to string for compress() and
131− * uncompress()
132− */
133−const char * ZEXPORT zError(err)
134− int err;
135−{
136− return ERR_MSG(err);
137−}
138−
139−#if defined(_WIN32_WCE)
140− /* The Microsoft C Run-Time Library for Windows CE doesn't have
141− * errno. We define it as a global variable to simplify porting.
142− * Its value is always 0 and should not be used.
143− */
144− int errno = 0;
145−#endif
146−
147−#ifndef HAVE_MEMCPY
148−
149−void zmemcpy(dest, source, len)
150− Bytef* dest;
151− const Bytef* source;
152− uInt len;
153−{
154− if (len == 0) return;
155− do {
156− *dest++ = *source++; /* ??? to be unrolled */
157− } while (--len != 0);
158−}
159−
160−int zmemcmp(s1, s2, len)
161− const Bytef* s1;
162− const Bytef* s2;
163− uInt len;
164−{
165− uInt j;
166−
167− for (j = 0; j < len; j++) {
168− if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
169− }
170− return 0;
171−}
172−
173−void zmemzero(dest, len)
174− Bytef* dest;
175− uInt len;
176−{
177− if (len == 0) return;
178− do {
179− *dest++ = 0; /* ??? to be unrolled */
180− } while (--len != 0);
181−}
182−#endif
183−
184−
185−#ifdef SYS16BIT
186−
187−#ifdef __TURBOC__
188−/* Turbo C in 16-bit mode */
189−
190−# define MY_ZCALLOC
191−
192−/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
193− * and farmalloc(64K) returns a pointer with an offset of 8, so we
194− * must fix the pointer. Warning: the pointer must be put back to its
195− * original form in order to free it, use zcfree().
196− */
197−
198−#define MAX_PTR 10
199−/* 10*64K = 640K */
200−
201−local int next_ptr = 0;
202−
203−typedef struct ptr_table_s {
204− voidpf org_ptr;
205− voidpf new_ptr;
206−} ptr_table;
207−
208−local ptr_table table[MAX_PTR];
209−/* This table is used to remember the original form of pointers
210− * to large buffers (64K). Such pointers are normalized with a zero offset.
211− * Since MSDOS is not a preemptive multitasking OS, this table is not
212− * protected from concurrent access. This hack doesn't work anyway on
213− * a protected system like OS/2. Use Microsoft C instead.
214− */
215−
216−voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
217−{
218− voidpf buf = opaque; /* just to make some compilers happy */
219− ulg bsize = (ulg)items*size;
220−
221− /* If we allocate less than 65520 bytes, we assume that farmalloc
222− * will return a usable pointer which doesn't have to be normalized.
223− */
224− if (bsize < 65520L) {
225− buf = farmalloc(bsize);
226− if (*(ush*)&buf != 0) return buf;
227− } else {
228− buf = farmalloc(bsize + 16L);
229− }
230− if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
231− table[next_ptr].org_ptr = buf;
232−
233− /* Normalize the pointer to seg:0 */
234− *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
235− *(ush*)&buf = 0;
236− table[next_ptr++].new_ptr = buf;
237− return buf;
238−}
239−
240−void zcfree (voidpf opaque, voidpf ptr)
241−{
242− int n;
243− if (*(ush*)&ptr != 0) { /* object < 64K */
244− farfree(ptr);
245− return;
246− }
247− /* Find the original pointer */
248− for (n = 0; n < next_ptr; n++) {
249− if (ptr != table[n].new_ptr) continue;
250−
251− farfree(table[n].org_ptr);
252− while (++n < next_ptr) {
253− table[n-1] = table[n];
254− }
255− next_ptr--;
256− return;
257− }
258− ptr = opaque; /* just to make some compilers happy */
259− Assert(0, "zcfree: ptr not found");
260−}
261−
262−#endif /* __TURBOC__ */
263−
264−
265−#ifdef M_I86
266−/* Microsoft C in 16-bit mode */
267−
268−# define MY_ZCALLOC
269−
270−#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
271−# define _halloc halloc
272−# define _hfree hfree
273−#endif
274−
275−voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
276−{
277− if (opaque) opaque = 0; /* to make compiler happy */
278− return _halloc((long)items, size);
279−}
280−
281−void zcfree (voidpf opaque, voidpf ptr)
282−{
283− if (opaque) opaque = 0; /* to make compiler happy */
284− _hfree(ptr);
285−}
286−
287−#endif /* M_I86 */
288−
289−#endif /* SYS16BIT */
290−
291−
292−#ifndef MY_ZCALLOC /* Any system without a special alloc function */
293−
294−#ifndef STDC
295−extern voidp malloc OF((uInt size));
296−extern voidp calloc OF((uInt items, uInt size));
297−extern void free OF((voidpf ptr));
298−#endif
299−
300−voidpf zcalloc (opaque, items, size)
301− voidpf opaque;
302− unsigned items;
303− unsigned size;
304−{
305− if (opaque) items += size - size; /* make compiler happy */
306− return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
307− (voidpf)calloc(items, size);
308−}
309−
310−void zcfree (opaque, ptr)
311− voidpf opaque;
312− voidpf ptr;
313−{
314− free(ptr);
315− if (opaque) return; /* make compiler happy */
316−}
317−
318−#endif /* MY_ZCALLOC */
cbits/zutil.hdeleted−269
@@ -1,269 +0,0 @@
1−/* zutil.h -- internal interface and configuration of the compression library
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* @(#) $Id$ */
12−
13−#ifndef ZUTIL_H
14−#define ZUTIL_H
15−
16−#define ZLIB_INTERNAL
17−#include "zlib.h"
18−
19−#ifdef STDC
20−# ifndef _WIN32_WCE
21−# include <stddef.h>
22−# endif
23−# include <string.h>
24−# include <stdlib.h>
25−#endif
26−#ifdef NO_ERRNO_H
27−# ifdef _WIN32_WCE
28− /* The Microsoft C Run-Time Library for Windows CE doesn't have
29− * errno. We define it as a global variable to simplify porting.
30− * Its value is always 0 and should not be used. We rename it to
31− * avoid conflict with other libraries that use the same workaround.
32− */
33−# define errno z_errno
34−# endif
35− extern int errno;
36−#else
37−# ifndef _WIN32_WCE
38−# include <errno.h>
39−# endif
40−#endif
41−
42−#ifndef local
43−# define local static
44−#endif
45−/* compile with -Dlocal if your debugger can't find static symbols */
46−
47−typedef unsigned char uch;
48−typedef uch FAR uchf;
49−typedef unsigned short ush;
50−typedef ush FAR ushf;
51−typedef unsigned long ulg;
52−
53−extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
54−/* (size given to avoid silly warnings with Visual C++) */
55−
56−#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
57−
58−#define ERR_RETURN(strm,err) \
59− return (strm->msg = (char*)ERR_MSG(err), (err))
60−/* To be used only when the state is known to be valid */
61−
62− /* common constants */
63−
64−#ifndef DEF_WBITS
65−# define DEF_WBITS MAX_WBITS
66−#endif
67−/* default windowBits for decompression. MAX_WBITS is for compression only */
68−
69−#if MAX_MEM_LEVEL >= 8
70−# define DEF_MEM_LEVEL 8
71−#else
72−# define DEF_MEM_LEVEL MAX_MEM_LEVEL
73−#endif
74−/* default memLevel */
75−
76−#define STORED_BLOCK 0
77−#define STATIC_TREES 1
78−#define DYN_TREES 2
79−/* The three kinds of block type */
80−
81−#define MIN_MATCH 3
82−#define MAX_MATCH 258
83−/* The minimum and maximum match lengths */
84−
85−#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
86−
87− /* target dependencies */
88−
89−#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
90−# define OS_CODE 0x00
91−# if defined(__TURBOC__) || defined(__BORLANDC__)
92−# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
93− /* Allow compilation with ANSI keywords only enabled */
94− void _Cdecl farfree( void *block );
95− void *_Cdecl farmalloc( unsigned long nbytes );
96−# else
97−# include <alloc.h>
98−# endif
99−# else /* MSC or DJGPP */
100−# include <malloc.h>
101−# endif
102−#endif
103−
104−#ifdef AMIGA
105−# define OS_CODE 0x01
106−#endif
107−
108−#if defined(VAXC) || defined(VMS)
109−# define OS_CODE 0x02
110−# define F_OPEN(name, mode) \
111− fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
112−#endif
113−
114−#if defined(ATARI) || defined(atarist)
115−# define OS_CODE 0x05
116−#endif
117−
118−#ifdef OS2
119−# define OS_CODE 0x06
120−# ifdef M_I86
121− #include <malloc.h>
122−# endif
123−#endif
124−
125−#if defined(MACOS) || defined(TARGET_OS_MAC)
126−# define OS_CODE 0x07
127−# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
128−# include <unix.h> /* for fdopen */
129−# else
130−# ifndef fdopen
131−# define fdopen(fd,mode) NULL /* No fdopen() */
132−# endif
133−# endif
134−#endif
135−
136−#ifdef TOPS20
137−# define OS_CODE 0x0a
138−#endif
139−
140−#ifdef WIN32
141−# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
142−# define OS_CODE 0x0b
143−# endif
144−#endif
145−
146−#ifdef __50SERIES /* Prime/PRIMOS */
147−# define OS_CODE 0x0f
148−#endif
149−
150−#if defined(_BEOS_) || defined(RISCOS)
151−# define fdopen(fd,mode) NULL /* No fdopen() */
152−#endif
153−
154−#if (defined(_MSC_VER) && (_MSC_VER > 600))
155−# if defined(_WIN32_WCE)
156−# define fdopen(fd,mode) NULL /* No fdopen() */
157−# ifndef _PTRDIFF_T_DEFINED
158− typedef int ptrdiff_t;
159−# define _PTRDIFF_T_DEFINED
160−# endif
161−# else
162−# define fdopen(fd,type) _fdopen(fd,type)
163−# endif
164−#endif
165−
166− /* common defaults */
167−
168−#ifndef OS_CODE
169−# define OS_CODE 0x03 /* assume Unix */
170−#endif
171−
172−#ifndef F_OPEN
173−# define F_OPEN(name, mode) fopen((name), (mode))
174−#endif
175−
176− /* functions */
177−
178−#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
179−# ifndef HAVE_VSNPRINTF
180−# define HAVE_VSNPRINTF
181−# endif
182−#endif
183−#if defined(__CYGWIN__)
184−# ifndef HAVE_VSNPRINTF
185−# define HAVE_VSNPRINTF
186−# endif
187−#endif
188−#ifndef HAVE_VSNPRINTF
189−# ifdef MSDOS
190− /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
191− but for now we just assume it doesn't. */
192−# define NO_vsnprintf
193−# endif
194−# ifdef __TURBOC__
195−# define NO_vsnprintf
196−# endif
197−# ifdef WIN32
198− /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
199−# if !defined(vsnprintf) && !defined(NO_vsnprintf)
200−# define vsnprintf _vsnprintf
201−# endif
202−# endif
203−# ifdef __SASC
204−# define NO_vsnprintf
205−# endif
206−#endif
207−#ifdef VMS
208−# define NO_vsnprintf
209−#endif
210−
211−#if defined(pyr)
212−# define NO_MEMCPY
213−#endif
214−#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
215− /* Use our own functions for small and medium model with MSC <= 5.0.
216− * You may have to use the same strategy for Borland C (untested).
217− * The __SC__ check is for Symantec.
218− */
219−# define NO_MEMCPY
220−#endif
221−#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
222−# define HAVE_MEMCPY
223−#endif
224−#ifdef HAVE_MEMCPY
225−# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
226−# define zmemcpy _fmemcpy
227−# define zmemcmp _fmemcmp
228−# define zmemzero(dest, len) _fmemset(dest, 0, len)
229−# else
230−# define zmemcpy memcpy
231−# define zmemcmp memcmp
232−# define zmemzero(dest, len) memset(dest, 0, len)
233−# endif
234−#else
235− extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
236− extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
237− extern void zmemzero OF((Bytef* dest, uInt len));
238−#endif
239−
240−/* Diagnostic functions */
241−#ifdef DEBUG
242−# include <stdio.h>
243− extern int z_verbose;
244− extern void z_error OF((char *m));
245−# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
246−# define Trace(x) {if (z_verbose>=0) fprintf x ;}
247−# define Tracev(x) {if (z_verbose>0) fprintf x ;}
248−# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
249−# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
250−# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
251−#else
252−# define Assert(cond,msg)
253−# define Trace(x)
254−# define Tracev(x)
255−# define Tracevv(x)
256−# define Tracec(c,x)
257−# define Tracecv(c,x)
258−#endif
259−
260−
261−voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
262−void zcfree OF((voidpf opaque, voidpf ptr));
263−
264−#define ZALLOC(strm, items, size) \
265− (*((strm)->zalloc))((strm)->opaque, (items), (size))
266−#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
267−#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
268−
269−#endif /* ZUTIL_H */
wai-extra.cabal+8−21
@@ -1,5 +1,5 @@
11Name: wai-extra
2−Version: 0.2.3
2+Version: 0.2.4
33Synopsis: Provides some basic WAI handlers and middleware.
44Description: The goal here is to provide common features without many dependencies.
55License: BSD3
@@ -9,11 +9,8 @@ Maintainer: michael@snoyman.com
99Homepage: http://github.com/snoyberg/wai-extra
1010Category: Web
1111Build-Type: Simple
12−Cabal-Version: >=1.2
12+Cabal-Version: >=1.6
1313Stability: Stable
14−extra-source-files: cbits/crc32.h cbits/inffast.h cbits/inflate.h
15− cbits/trees.h cbits/deflate.h cbits/inffixed.h
16− cbits/inftrees.h cbits/zutil.h
1714
1815Library
1916 Build-Depends: base >= 3 && < 5,
@@ -23,7 +20,8 @@ Library
2320 time >= 1.1.4 && < 1.3,
2421 network >= 2.2.1.5 && < 2.3,
2522 directory >= 1.0.1 && < 1.2,
26− utf8-string >= 0.3.4 && < 0.4
23+ utf8-string >= 0.3.4 && < 0.4,
24+ zlib-bindings >= 0.0 && < 0.1
2725 Exposed-modules: Network.Wai.Handler.CGI
2826 Network.Wai.Handler.SimpleServer
2927 Network.Wai.Middleware.CleanPath
@@ -33,18 +31,7 @@ Library
3331 Network.Wai.Parse
3432 Network.Wai.Handler.Helper
3533 ghc-options: -Wall
36− c-sources: c/helper.c
37− include-dirs: c
38− -- Following lines and cbits folder taken from zlib package.
39− if !os(windows)
40− -- Normally we use the the standard system zlib:
41− extra-libraries: z
42− else
43− -- However for the benefit of users of Windows (which does not have zlib
44− -- by default) we bundle a complete copy of the C sources of zlib-1.2.3
45− c-sources: cbits/adler32.c cbits/compress.c cbits/crc32.c
46− cbits/deflate.c cbits/gzio.c cbits/infback.c
47− cbits/inffast.c cbits/inflate.c cbits/inftrees.c
48− cbits/trees.c cbits/uncompr.c cbits/zutil.c
49− include-dirs: cbits
50− install-includes: zlib.h zconf.h
34+
35+source-repository head
36+ type: git
37+ location: git://github.com/snoyberg/wai-extra.git