menu: simplify usage for clients
[barebox-mini2440.git] / lib / zlib.c
blob699a6f2c2349b81d8a410ca53f2b45428124625f
1 /*
2 * This file is derived from various .h and .c files from the zlib-0.95
3 * distribution by Jean-loup Gailly and Mark Adler, with some additions
4 * by Paul Mackerras to aid in implementing Deflate compression and
5 * decompression for PPP packets. See zlib.h for conditions of
6 * distribution and use.
8 * Changes that have been made include:
9 * - changed functions not used outside this file to "local"
10 * - added minCompression parameter to deflateInit2
11 * - added Z_PACKET_FLUSH (see zlib.h for details)
12 * - added inflateIncomp
15 /*+++++*/
16 /* zutil.h -- internal interface and configuration of the compression library
17 * Copyright (C) 1995 Jean-loup Gailly.
18 * For conditions of distribution and use, see copyright notice in zlib.h
21 /* WARNING: this file should *not* be used by applications. It is
22 part of the implementation of the compression library and is
23 subject to change. Applications should only use zlib.h.
26 /* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */
28 #define _Z_UTIL_H
30 #include "zlib.h"
32 #ifndef local
33 # define local static
34 #endif
35 /* compile with -Dlocal if your debugger can't find static symbols */
37 #define FAR
39 typedef unsigned char uch;
40 typedef uch FAR uchf;
41 typedef unsigned short ush;
42 typedef ush FAR ushf;
43 typedef unsigned long ulg;
45 extern char *z_errmsg[]; /* indexed by 1-zlib_error */
47 #define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
48 /* To be used only when the state is known to be valid */
50 #ifndef NULL
51 #define NULL ((void *) 0)
52 #endif
54 /* common constants */
56 #define DEFLATED 8
58 #ifndef DEF_WBITS
59 # define DEF_WBITS MAX_WBITS
60 #endif
61 /* default windowBits for decompression. MAX_WBITS is for compression only */
63 #if MAX_MEM_LEVEL >= 8
64 # define DEF_MEM_LEVEL 8
65 #else
66 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
67 #endif
68 /* default memLevel */
70 #define STORED_BLOCK 0
71 #define STATIC_TREES 1
72 #define DYN_TREES 2
73 /* The three kinds of block type */
75 #define MIN_MATCH 3
76 #define MAX_MATCH 258
77 /* The minimum and maximum match lengths */
79 /* functions */
81 #include <linux/string.h>
82 #define zmemcpy memcpy
83 #define zmemzero(dest, len) memset(dest, 0, len)
85 /* Diagnostic functions */
86 #ifdef DEBUG_ZLIB
87 # include <stdio.h>
88 # ifndef verbose
89 # define verbose 0
90 # endif
91 # define Assert(cond,msg) {if(!(cond)) z_error(msg);}
92 # define Trace(x) fprintf x
93 # define Tracev(x) {if (verbose) fprintf x ;}
94 # define Tracevv(x) {if (verbose>1) fprintf x ;}
95 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
96 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
97 #else
98 # define Assert(cond,msg)
99 # define Trace(x)
100 # define Tracev(x)
101 # define Tracevv(x)
102 # define Tracec(c,x)
103 # define Tracecv(c,x)
104 #endif
107 typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
109 /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
110 /* void zcfree OF((voidpf opaque, voidpf ptr)); */
112 #define ZALLOC(strm, items, size) \
113 (*((strm)->zalloc))((strm)->opaque, (items), (size))
114 #define ZFREE(strm, addr, size) \
115 (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
116 #define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}
118 /* deflate.h -- internal compression state
119 * Copyright (C) 1995 Jean-loup Gailly
120 * For conditions of distribution and use, see copyright notice in zlib.h
123 /* WARNING: this file should *not* be used by applications. It is
124 part of the implementation of the compression library and is
125 subject to change. Applications should only use zlib.h.
128 /*+++++*/
129 /* infblock.h -- header to use infblock.c
130 * Copyright (C) 1995 Mark Adler
131 * For conditions of distribution and use, see copyright notice in zlib.h
134 /* WARNING: this file should *not* be used by applications. It is
135 part of the implementation of the compression library and is
136 subject to change. Applications should only use zlib.h.
139 struct inflate_blocks_state;
140 typedef struct inflate_blocks_state FAR inflate_blocks_statef;
142 local inflate_blocks_statef * inflate_blocks_new OF((
143 z_stream *z,
144 check_func c, /* check function */
145 uInt w)); /* window size */
147 local int inflate_blocks OF((
148 inflate_blocks_statef *,
149 z_stream *,
150 int)); /* initial return code */
152 local void inflate_blocks_reset OF((
153 inflate_blocks_statef *,
154 z_stream *,
155 uLongf *)); /* check value on output */
157 local int inflate_blocks_free OF((
158 inflate_blocks_statef *,
159 z_stream *,
160 uLongf *)); /* check value on output */
162 local int inflate_addhistory OF((
163 inflate_blocks_statef *,
164 z_stream *));
166 local int inflate_packet_flush OF((
167 inflate_blocks_statef *));
169 /*+++++*/
170 /* inftrees.h -- header to use inftrees.c
171 * Copyright (C) 1995 Mark Adler
172 * For conditions of distribution and use, see copyright notice in zlib.h
175 /* WARNING: this file should *not* be used by applications. It is
176 part of the implementation of the compression library and is
177 subject to change. Applications should only use zlib.h.
180 /* Huffman code lookup table entry--this entry is four bytes for machines
181 that have 16-bit pointers (e.g. PC's in the small or medium model). */
183 typedef struct inflate_huft_s FAR inflate_huft;
185 struct inflate_huft_s {
186 union {
187 struct {
188 Byte Exop; /* number of extra bits or operation */
189 Byte Bits; /* number of bits in this code or subcode */
190 } what;
191 uInt Nalloc; /* number of these allocated here */
192 Bytef *pad; /* pad structure to a power of 2 (4 bytes for */
193 } word; /* 16-bit, 8 bytes for 32-bit machines) */
194 union {
195 uInt Base; /* literal, length base, or distance base */
196 inflate_huft *Next; /* pointer to next level of table */
197 } more;
200 #ifdef DEBUG_ZLIB
201 local uInt inflate_hufts;
202 #endif
204 local int inflate_trees_bits OF((
205 uIntf *, /* 19 code lengths */
206 uIntf *, /* bits tree desired/actual depth */
207 inflate_huft * FAR *, /* bits tree result */
208 z_stream *)); /* for zalloc, zfree functions */
210 local int inflate_trees_dynamic OF((
211 uInt, /* number of literal/length codes */
212 uInt, /* number of distance codes */
213 uIntf *, /* that many (total) code lengths */
214 uIntf *, /* literal desired/actual bit depth */
215 uIntf *, /* distance desired/actual bit depth */
216 inflate_huft * FAR *, /* literal/length tree result */
217 inflate_huft * FAR *, /* distance tree result */
218 z_stream *)); /* for zalloc, zfree functions */
220 local int inflate_trees_fixed OF((
221 uIntf *, /* literal desired/actual bit depth */
222 uIntf *, /* distance desired/actual bit depth */
223 inflate_huft * FAR *, /* literal/length tree result */
224 inflate_huft * FAR *)); /* distance tree result */
226 local int inflate_trees_free OF((
227 inflate_huft *, /* tables to free */
228 z_stream *)); /* for zfree function */
231 /*+++++*/
232 /* infcodes.h -- header to use infcodes.c
233 * Copyright (C) 1995 Mark Adler
234 * For conditions of distribution and use, see copyright notice in zlib.h
237 /* WARNING: this file should *not* be used by applications. It is
238 part of the implementation of the compression library and is
239 subject to change. Applications should only use zlib.h.
242 struct inflate_codes_state;
243 typedef struct inflate_codes_state FAR inflate_codes_statef;
245 local inflate_codes_statef *inflate_codes_new OF((
246 uInt, uInt,
247 inflate_huft *, inflate_huft *,
248 z_stream *));
250 local int inflate_codes OF((
251 inflate_blocks_statef *,
252 z_stream *,
253 int));
255 local void inflate_codes_free OF((
256 inflate_codes_statef *,
257 z_stream *));
260 /*+++++*/
261 /* inflate.c -- zlib interface to inflate modules
262 * Copyright (C) 1995 Mark Adler
263 * For conditions of distribution and use, see copyright notice in zlib.h
266 /* inflate private state */
267 struct internal_state {
269 /* mode */
270 enum {
271 METHOD, /* waiting for method byte */
272 FLAG, /* waiting for flag byte */
273 BLOCKS, /* decompressing blocks */
274 CHECK4, /* four check bytes to go */
275 CHECK3, /* three check bytes to go */
276 CHECK2, /* two check bytes to go */
277 CHECK1, /* one check byte to go */
278 DONE, /* finished check, done */
279 BAD} /* got an error--stay here */
280 mode; /* current inflate mode */
282 /* mode dependent information */
283 union {
284 uInt method; /* if FLAGS, method byte */
285 struct {
286 uLong was; /* computed check value */
287 uLong need; /* stream check value */
288 } check; /* if CHECK, check values to compare */
289 uInt marker; /* if BAD, inflateSync's marker bytes count */
290 } sub; /* submode */
292 /* mode independent information */
293 int nowrap; /* flag for no wrapper */
294 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
295 inflate_blocks_statef
296 *blocks; /* current inflate_blocks state */
301 int inflateReset(z)
302 z_stream *z;
304 uLong c;
306 if (z == Z_NULL || z->state == Z_NULL)
307 return Z_STREAM_ERROR;
308 z->total_in = z->total_out = 0;
309 z->msg = Z_NULL;
310 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
311 inflate_blocks_reset(z->state->blocks, z, &c);
312 Trace((stderr, "inflate: reset\n"));
313 return Z_OK;
317 int inflateEnd(z)
318 z_stream *z;
320 uLong c;
322 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
323 return Z_STREAM_ERROR;
324 if (z->state->blocks != Z_NULL)
325 inflate_blocks_free(z->state->blocks, z, &c);
326 ZFREE(z, z->state, sizeof(struct internal_state));
327 z->state = Z_NULL;
328 Trace((stderr, "inflate: end\n"));
329 return Z_OK;
333 int inflateInit2(z, w)
334 z_stream *z;
335 int w;
337 /* initialize state */
338 if (z == Z_NULL)
339 return Z_STREAM_ERROR;
340 /* if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */
341 /* if (z->zfree == Z_NULL) z->zfree = zcfree; */
342 if ((z->state = (struct internal_state FAR *)
343 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
344 return Z_MEM_ERROR;
345 z->state->blocks = Z_NULL;
347 /* handle undocumented nowrap option (no zlib header or check) */
348 z->state->nowrap = 0;
349 if (w < 0)
351 w = - w;
352 z->state->nowrap = 1;
355 /* set window size */
356 if (w < 8 || w > 15)
358 inflateEnd(z);
359 return Z_STREAM_ERROR;
361 z->state->wbits = (uInt)w;
363 /* create inflate_blocks state */
364 if ((z->state->blocks =
365 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
366 == Z_NULL)
368 inflateEnd(z);
369 return Z_MEM_ERROR;
371 Trace((stderr, "inflate: allocated\n"));
373 /* reset state */
374 inflateReset(z);
375 return Z_OK;
379 int inflateInit(z)
380 z_stream *z;
382 return inflateInit2(z, DEF_WBITS);
386 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
387 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
389 int inflate(z, f)
390 z_stream *z;
391 int f;
393 int r;
394 uInt b;
396 if (z == Z_NULL || z->next_in == Z_NULL)
397 return Z_STREAM_ERROR;
398 r = Z_BUF_ERROR;
399 while (1) switch (z->state->mode)
401 case METHOD:
402 NEEDBYTE
403 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
405 z->state->mode = BAD;
406 z->msg = "unknown compression method";
407 z->state->sub.marker = 5; /* can't try inflateSync */
408 break;
410 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
412 z->state->mode = BAD;
413 z->msg = "invalid window size";
414 z->state->sub.marker = 5; /* can't try inflateSync */
415 break;
417 z->state->mode = FLAG;
418 case FLAG:
419 NEEDBYTE
420 if ((b = NEXTBYTE) & 0x20)
422 z->state->mode = BAD;
423 z->msg = "invalid reserved bit";
424 z->state->sub.marker = 5; /* can't try inflateSync */
425 break;
427 if (((z->state->sub.method << 8) + b) % 31)
429 z->state->mode = BAD;
430 z->msg = "incorrect header check";
431 z->state->sub.marker = 5; /* can't try inflateSync */
432 break;
434 Trace((stderr, "inflate: zlib header ok\n"));
435 z->state->mode = BLOCKS;
436 case BLOCKS:
437 r = inflate_blocks(z->state->blocks, z, r);
438 if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
439 r = inflate_packet_flush(z->state->blocks);
440 if (r == Z_DATA_ERROR)
442 z->state->mode = BAD;
443 z->state->sub.marker = 0; /* can try inflateSync */
444 break;
446 if (r != Z_STREAM_END)
447 return r;
448 r = Z_OK;
449 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
450 if (z->state->nowrap)
452 z->state->mode = DONE;
453 break;
455 z->state->mode = CHECK4;
456 case CHECK4:
457 NEEDBYTE
458 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
459 z->state->mode = CHECK3;
460 case CHECK3:
461 NEEDBYTE
462 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
463 z->state->mode = CHECK2;
464 case CHECK2:
465 NEEDBYTE
466 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
467 z->state->mode = CHECK1;
468 case CHECK1:
469 NEEDBYTE
470 z->state->sub.check.need += (uLong)NEXTBYTE;
472 if (z->state->sub.check.was != z->state->sub.check.need)
474 z->state->mode = BAD;
475 z->msg = "incorrect data check";
476 z->state->sub.marker = 5; /* can't try inflateSync */
477 break;
479 Trace((stderr, "inflate: zlib check ok\n"));
480 z->state->mode = DONE;
481 case DONE:
482 return Z_STREAM_END;
483 case BAD:
484 return Z_DATA_ERROR;
485 default:
486 return Z_STREAM_ERROR;
489 empty:
490 if (f != Z_PACKET_FLUSH)
491 return r;
492 z->state->mode = BAD;
493 z->state->sub.marker = 0; /* can try inflateSync */
494 return Z_DATA_ERROR;
498 * This subroutine adds the data at next_in/avail_in to the output history
499 * without performing any output. The output buffer must be "caught up";
500 * i.e. no pending output (hence s->read equals s->write), and the state must
501 * be BLOCKS (i.e. we should be willing to see the start of a series of
502 * BLOCKS). On exit, the output will also be caught up, and the checksum
503 * will have been updated if need be.
506 int inflateIncomp(z)
507 z_stream *z;
509 if (z->state->mode != BLOCKS)
510 return Z_DATA_ERROR;
511 return inflate_addhistory(z->state->blocks, z);
515 int inflateSync(z)
516 z_stream *z;
518 uInt n; /* number of bytes to look at */
519 Bytef *p; /* pointer to bytes */
520 uInt m; /* number of marker bytes found in a row */
521 uLong r, w; /* temporaries to save total_in and total_out */
523 /* set up */
524 if (z == Z_NULL || z->state == Z_NULL)
525 return Z_STREAM_ERROR;
526 if (z->state->mode != BAD)
528 z->state->mode = BAD;
529 z->state->sub.marker = 0;
531 if ((n = z->avail_in) == 0)
532 return Z_BUF_ERROR;
533 p = z->next_in;
534 m = z->state->sub.marker;
536 /* search */
537 while (n && m < 4)
539 if (*p == (Byte)(m < 2 ? 0 : 0xff))
540 m++;
541 else if (*p)
542 m = 0;
543 else
544 m = 4 - m;
545 p++, n--;
548 /* restore */
549 z->total_in += p - z->next_in;
550 z->next_in = p;
551 z->avail_in = n;
552 z->state->sub.marker = m;
554 /* return no joy or set up to restart on a new block */
555 if (m != 4)
556 return Z_DATA_ERROR;
557 r = z->total_in; w = z->total_out;
558 inflateReset(z);
559 z->total_in = r; z->total_out = w;
560 z->state->mode = BLOCKS;
561 return Z_OK;
564 #undef NEEDBYTE
565 #undef NEXTBYTE
567 /*+++++*/
568 /* infutil.h -- types and macros common to blocks and codes
569 * Copyright (C) 1995 Mark Adler
570 * For conditions of distribution and use, see copyright notice in zlib.h
573 /* WARNING: this file should *not* be used by applications. It is
574 part of the implementation of the compression library and is
575 subject to change. Applications should only use zlib.h.
578 /* inflate blocks semi-private state */
579 struct inflate_blocks_state {
581 /* mode */
582 enum {
583 TYPE, /* get type bits (3, including end bit) */
584 LENS, /* get lengths for stored */
585 STORED, /* processing stored block */
586 TABLE, /* get table lengths */
587 BTREE, /* get bit lengths tree for a dynamic block */
588 DTREE, /* get length, distance trees for a dynamic block */
589 CODES, /* processing fixed or dynamic block */
590 DRY, /* output remaining window bytes */
591 DONEB, /* finished last block, done */
592 BADB} /* got a data error--stuck here */
593 mode; /* current inflate_block mode */
595 /* mode dependent information */
596 union {
597 uInt left; /* if STORED, bytes left to copy */
598 struct {
599 uInt table; /* table lengths (14 bits) */
600 uInt index; /* index into blens (or border) */
601 uIntf *blens; /* bit lengths of codes */
602 uInt bb; /* bit length tree depth */
603 inflate_huft *tb; /* bit length decoding tree */
604 int nblens; /* # elements allocated at blens */
605 } trees; /* if DTREE, decoding info for trees */
606 struct {
607 inflate_huft *tl, *td; /* trees to free */
608 inflate_codes_statef
609 *codes;
610 } decode; /* if CODES, current state */
611 } sub; /* submode */
612 uInt last; /* true if this block is the last block */
614 /* mode independent information */
615 uInt bitk; /* bits in bit buffer */
616 uLong bitb; /* bit buffer */
617 Bytef *window; /* sliding window */
618 Bytef *end; /* one byte after sliding window */
619 Bytef *read; /* window read pointer */
620 Bytef *write; /* window write pointer */
621 check_func checkfn; /* check function */
622 uLong check; /* check on output */
627 /* defines for inflate input/output */
628 /* update pointers and return */
629 #define UPDBITS {s->bitb=b;s->bitk=k;}
630 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
631 #define UPDOUT {s->write=q;}
632 #define UPDATE {UPDBITS UPDIN UPDOUT}
633 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
634 /* get bytes and bits */
635 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
636 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
637 #define NEXTBYTE (n--,*p++)
638 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
639 #define DUMPBITS(j) {b>>=(j);k-=(j);}
640 /* output bytes */
641 #define WAVAIL (q<s->read?s->read-q-1:s->end-q)
642 #define LOADOUT {q=s->write;m=WAVAIL;}
643 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
644 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
645 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
646 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
647 /* load local pointers */
648 #define LOAD {LOADIN LOADOUT}
651 * The IBM 150 firmware munges the data right after _etext[]. This
652 * protects it. -- Cort
654 /* And'ing with mask[n] masks the lower n bits */
655 local uInt inflate_mask[] = {
656 0x0000,
657 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
658 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
661 /* copy as much as possible from the sliding window to the output area */
662 local int inflate_flush OF((
663 inflate_blocks_statef *,
664 z_stream *,
665 int));
667 /*+++++*/
668 /* inffast.h -- header to use inffast.c
669 * Copyright (C) 1995 Mark Adler
670 * For conditions of distribution and use, see copyright notice in zlib.h
673 /* WARNING: this file should *not* be used by applications. It is
674 part of the implementation of the compression library and is
675 subject to change. Applications should only use zlib.h.
678 local int inflate_fast OF((
679 uInt,
680 uInt,
681 inflate_huft *,
682 inflate_huft *,
683 inflate_blocks_statef *,
684 z_stream *));
687 /*+++++*/
688 /* infblock.c -- interpret and process block types to last block
689 * Copyright (C) 1995 Mark Adler
690 * For conditions of distribution and use, see copyright notice in zlib.h
693 /* Table for deflate from PKZIP's appnote.txt. */
694 local uInt border[] = { /* Order of the bit length code lengths */
695 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
698 Notes beyond the 1.93a appnote.txt:
700 1. Distance pointers never point before the beginning of the output
701 stream.
702 2. Distance pointers can point back across blocks, up to 32k away.
703 3. There is an implied maximum of 7 bits for the bit length table and
704 15 bits for the actual data.
705 4. If only one code exists, then it is encoded using one bit. (Zero
706 would be more efficient, but perhaps a little confusing.) If two
707 codes exist, they are coded using one bit each (0 and 1).
708 5. There is no way of sending zero distance codes--a dummy must be
709 sent if there are none. (History: a pre 2.0 version of PKZIP would
710 store blocks with no distance codes, but this was discovered to be
711 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
712 zero distance codes, which is sent as one code of zero bits in
713 length.
714 6. There are up to 286 literal/length codes. Code 256 represents the
715 end-of-block. Note however that the static length tree defines
716 288 codes just to fill out the Huffman codes. Codes 286 and 287
717 cannot be used though, since there is no length base or extra bits
718 defined for them. Similarily, there are up to 30 distance codes.
719 However, static trees define 32 codes (all 5 bits) to fill out the
720 Huffman codes, but the last two had better not show up in the data.
721 7. Unzip can check dynamic Huffman blocks for complete code sets.
722 The exception is that a single code would not be complete (see #4).
723 8. The five bits following the block type is really the number of
724 literal codes sent minus 257.
725 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
726 (1+6+6). Therefore, to output three times the length, you output
727 three codes (1+1+1), whereas to output four times the same length,
728 you only need two codes (1+3). Hmm.
729 10. In the tree reconstruction algorithm, Code = Code + Increment
730 only if BitLength(i) is not zero. (Pretty obvious.)
731 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
732 12. Note: length code 284 can represent 227-258, but length code 285
733 really is 258. The last length deserves its own, short code
734 since it gets used a lot in very redundant files. The length
735 258 is special since 258 - 3 (the min match length) is 255.
736 13. The literal/length and distance code bit lengths are read as a
737 single stream of lengths. It is possible (and advantageous) for
738 a repeat code (16, 17, or 18) to go across the boundary between
739 the two sets of lengths.
743 local void inflate_blocks_reset(s, z, c)
744 inflate_blocks_statef *s;
745 z_stream *z;
746 uLongf *c;
748 if (s->checkfn != Z_NULL)
749 *c = s->check;
750 if (s->mode == BTREE || s->mode == DTREE)
751 ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
752 if (s->mode == CODES)
754 inflate_codes_free(s->sub.decode.codes, z);
755 inflate_trees_free(s->sub.decode.td, z);
756 inflate_trees_free(s->sub.decode.tl, z);
758 s->mode = TYPE;
759 s->bitk = 0;
760 s->bitb = 0;
761 s->read = s->write = s->window;
762 if (s->checkfn != Z_NULL)
763 s->check = (*s->checkfn)(0L, Z_NULL, 0);
764 if (z->outcb != Z_NULL)
765 (*z->outcb)(Z_NULL, 0);
766 Trace((stderr, "inflate: blocks reset\n"));
770 local inflate_blocks_statef *inflate_blocks_new(z, c, w)
771 z_stream *z;
772 check_func c;
773 uInt w;
775 inflate_blocks_statef *s;
777 if ((s = (inflate_blocks_statef *)ZALLOC
778 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
779 return s;
780 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
782 ZFREE(z, s, sizeof(struct inflate_blocks_state));
783 return Z_NULL;
785 s->end = s->window + w;
786 s->checkfn = c;
787 s->mode = TYPE;
788 Trace((stderr, "inflate: blocks allocated\n"));
789 inflate_blocks_reset(s, z, &s->check);
790 return s;
794 local int inflate_blocks(s, z, r)
795 inflate_blocks_statef *s;
796 z_stream *z;
797 int r;
799 uInt t; /* temporary storage */
800 uLong b; /* bit buffer */
801 uInt k; /* bits in bit buffer */
802 Bytef *p; /* input data pointer */
803 uInt n; /* bytes available there */
804 Bytef *q; /* output window write pointer */
805 uInt m; /* bytes to end of window or read pointer */
807 /* copy input/output information to locals (UPDATE macro restores) */
808 LOAD
810 /* process input based on current state */
811 while (1) switch (s->mode)
813 case TYPE:
814 NEEDBITS(3)
815 t = (uInt)b & 7;
816 s->last = t & 1;
817 switch (t >> 1)
819 case 0: /* stored */
820 Trace((stderr, "inflate: stored block%s\n",
821 s->last ? " (last)" : ""));
822 DUMPBITS(3)
823 t = k & 7; /* go to byte boundary */
824 DUMPBITS(t)
825 s->mode = LENS; /* get length of stored block */
826 break;
827 case 1: /* fixed */
828 Trace((stderr, "inflate: fixed codes block%s\n",
829 s->last ? " (last)" : ""));
831 uInt bl, bd;
832 inflate_huft *tl, *td;
834 inflate_trees_fixed(&bl, &bd, &tl, &td);
835 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
836 if (s->sub.decode.codes == Z_NULL)
838 r = Z_MEM_ERROR;
839 LEAVE
841 s->sub.decode.tl = Z_NULL; /* don't try to free these */
842 s->sub.decode.td = Z_NULL;
844 DUMPBITS(3)
845 s->mode = CODES;
846 break;
847 case 2: /* dynamic */
848 Trace((stderr, "inflate: dynamic codes block%s\n",
849 s->last ? " (last)" : ""));
850 DUMPBITS(3)
851 s->mode = TABLE;
852 break;
853 case 3: /* illegal */
854 DUMPBITS(3)
855 s->mode = BADB;
856 z->msg = "invalid block type";
857 r = Z_DATA_ERROR;
858 LEAVE
860 break;
861 case LENS:
862 NEEDBITS(32)
863 if (((~b) >> 16) != (b & 0xffff))
865 s->mode = BADB;
866 z->msg = "invalid stored block lengths";
867 r = Z_DATA_ERROR;
868 LEAVE
870 s->sub.left = (uInt)b & 0xffff;
871 b = k = 0; /* dump bits */
872 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
873 s->mode = s->sub.left ? STORED : TYPE;
874 break;
875 case STORED:
876 if (n == 0)
877 LEAVE
878 NEEDOUT
879 t = s->sub.left;
880 if (t > n) t = n;
881 if (t > m) t = m;
882 zmemcpy(q, p, t);
883 p += t; n -= t;
884 q += t; m -= t;
885 if ((s->sub.left -= t) != 0)
886 break;
887 Tracev((stderr, "inflate: stored end, %lu total out\n",
888 z->total_out + (q >= s->read ? q - s->read :
889 (s->end - s->read) + (q - s->window))));
890 s->mode = s->last ? DRY : TYPE;
891 break;
892 case TABLE:
893 NEEDBITS(14)
894 s->sub.trees.table = t = (uInt)b & 0x3fff;
895 #ifndef PKZIP_BUG_WORKAROUND
896 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
898 s->mode = BADB;
899 z->msg = "too many length or distance symbols";
900 r = Z_DATA_ERROR;
901 LEAVE
903 #endif
904 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
905 if (t < 19)
906 t = 19;
907 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
909 r = Z_MEM_ERROR;
910 LEAVE
912 s->sub.trees.nblens = t;
913 DUMPBITS(14)
914 s->sub.trees.index = 0;
915 Tracev((stderr, "inflate: table sizes ok\n"));
916 s->mode = BTREE;
917 case BTREE:
918 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
920 NEEDBITS(3)
921 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
922 DUMPBITS(3)
924 while (s->sub.trees.index < 19)
925 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
926 s->sub.trees.bb = 7;
927 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
928 &s->sub.trees.tb, z);
929 if (t != Z_OK)
931 r = t;
932 if (r == Z_DATA_ERROR)
933 s->mode = BADB;
934 LEAVE
936 s->sub.trees.index = 0;
937 Tracev((stderr, "inflate: bits tree ok\n"));
938 s->mode = DTREE;
939 case DTREE:
940 while (t = s->sub.trees.table,
941 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
943 inflate_huft *h;
944 uInt i, j, c;
946 t = s->sub.trees.bb;
947 NEEDBITS(t)
948 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
949 t = h->word.what.Bits;
950 c = h->more.Base;
951 if (c < 16)
953 DUMPBITS(t)
954 s->sub.trees.blens[s->sub.trees.index++] = c;
956 else /* c == 16..18 */
958 i = c == 18 ? 7 : c - 14;
959 j = c == 18 ? 11 : 3;
960 NEEDBITS(t + i)
961 DUMPBITS(t)
962 j += (uInt)b & inflate_mask[i];
963 DUMPBITS(i)
964 i = s->sub.trees.index;
965 t = s->sub.trees.table;
966 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
967 (c == 16 && i < 1))
969 s->mode = BADB;
970 z->msg = "invalid bit length repeat";
971 r = Z_DATA_ERROR;
972 LEAVE
974 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
975 do {
976 s->sub.trees.blens[i++] = c;
977 } while (--j);
978 s->sub.trees.index = i;
981 inflate_trees_free(s->sub.trees.tb, z);
982 s->sub.trees.tb = Z_NULL;
984 uInt bl, bd;
985 inflate_huft *tl, *td;
986 inflate_codes_statef *c;
988 bl = 9; /* must be <= 9 for lookahead assumptions */
989 bd = 6; /* must be <= 9 for lookahead assumptions */
990 t = s->sub.trees.table;
991 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
992 s->sub.trees.blens, &bl, &bd, &tl, &td, z);
993 if (t != Z_OK)
995 if (t == (uInt)Z_DATA_ERROR)
996 s->mode = BADB;
997 r = t;
998 LEAVE
1000 Tracev((stderr, "inflate: trees ok\n"));
1001 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
1003 inflate_trees_free(td, z);
1004 inflate_trees_free(tl, z);
1005 r = Z_MEM_ERROR;
1006 LEAVE
1008 ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
1009 s->sub.decode.codes = c;
1010 s->sub.decode.tl = tl;
1011 s->sub.decode.td = td;
1013 s->mode = CODES;
1014 case CODES:
1015 UPDATE
1016 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
1017 return inflate_flush(s, z, r);
1018 r = Z_OK;
1019 inflate_codes_free(s->sub.decode.codes, z);
1020 inflate_trees_free(s->sub.decode.td, z);
1021 inflate_trees_free(s->sub.decode.tl, z);
1022 LOAD
1023 Tracev((stderr, "inflate: codes end, %lu total out\n",
1024 z->total_out + (q >= s->read ? q - s->read :
1025 (s->end - s->read) + (q - s->window))));
1026 if (!s->last)
1028 s->mode = TYPE;
1029 break;
1031 if (k > 7) /* return unused byte, if any */
1033 Assert(k < 16, "inflate_codes grabbed too many bytes")
1034 k -= 8;
1035 n++;
1036 p--; /* can always return one */
1038 s->mode = DRY;
1039 case DRY:
1040 FLUSH
1041 if (s->read != s->write)
1042 LEAVE
1043 s->mode = DONEB;
1044 case DONEB:
1045 r = Z_STREAM_END;
1046 LEAVE
1047 case BADB:
1048 r = Z_DATA_ERROR;
1049 LEAVE
1050 default:
1051 r = Z_STREAM_ERROR;
1052 LEAVE
1057 local int inflate_blocks_free(s, z, c)
1058 inflate_blocks_statef *s;
1059 z_stream *z;
1060 uLongf *c;
1062 inflate_blocks_reset(s, z, c);
1063 ZFREE(z, s->window, s->end - s->window);
1064 ZFREE(z, s, sizeof(struct inflate_blocks_state));
1065 Trace((stderr, "inflate: blocks freed\n"));
1066 return Z_OK;
1070 * This subroutine adds the data at next_in/avail_in to the output history
1071 * without performing any output. The output buffer must be "caught up";
1072 * i.e. no pending output (hence s->read equals s->write), and the state must
1073 * be BLOCKS (i.e. we should be willing to see the start of a series of
1074 * BLOCKS). On exit, the output will also be caught up, and the checksum
1075 * will have been updated if need be.
1077 local int inflate_addhistory(s, z)
1078 inflate_blocks_statef *s;
1079 z_stream *z;
1081 uLong b; /* bit buffer */ /* NOT USED HERE */
1082 uInt k; /* bits in bit buffer */ /* NOT USED HERE */
1083 uInt t; /* temporary storage */
1084 Bytef *p; /* input data pointer */
1085 uInt n; /* bytes available there */
1086 Bytef *q; /* output window write pointer */
1087 uInt m; /* bytes to end of window or read pointer */
1089 if (s->read != s->write)
1090 return Z_STREAM_ERROR;
1091 if (s->mode != TYPE)
1092 return Z_DATA_ERROR;
1094 /* we're ready to rock */
1095 LOAD
1096 /* while there is input ready, copy to output buffer, moving
1097 * pointers as needed.
1099 while (n) {
1100 t = n; /* how many to do */
1101 /* is there room until end of buffer? */
1102 if (t > m) t = m;
1103 /* update check information */
1104 if (s->checkfn != Z_NULL)
1105 s->check = (*s->checkfn)(s->check, q, t);
1106 /* output callback */
1107 if (z->outcb != Z_NULL)
1108 (*z->outcb)(q, t);
1109 zmemcpy(q, p, t);
1110 q += t;
1111 p += t;
1112 n -= t;
1113 z->total_out += t;
1114 s->read = q; /* drag read pointer forward */
1115 /* WRAP */ /* expand WRAP macro by hand to handle s->read */
1116 if (q == s->end) {
1117 s->read = q = s->window;
1118 m = WAVAIL;
1121 UPDATE
1122 return Z_OK;
1127 * At the end of a Deflate-compressed PPP packet, we expect to have seen
1128 * a `stored' block type value but not the (zero) length bytes.
1130 local int inflate_packet_flush(s)
1131 inflate_blocks_statef *s;
1133 if (s->mode != LENS)
1134 return Z_DATA_ERROR;
1135 s->mode = TYPE;
1136 return Z_OK;
1140 /*+++++*/
1141 /* inftrees.c -- generate Huffman trees for efficient decoding
1142 * Copyright (C) 1995 Mark Adler
1143 * For conditions of distribution and use, see copyright notice in zlib.h
1146 /* simplify the use of the inflate_huft type with some defines */
1147 #define base more.Base
1148 #define next more.Next
1149 #define exop word.what.Exop
1150 #define bits word.what.Bits
1153 local int huft_build OF((
1154 uIntf *, /* code lengths in bits */
1155 uInt, /* number of codes */
1156 uInt, /* number of "simple" codes */
1157 uIntf *, /* list of base values for non-simple codes */
1158 uIntf *, /* list of extra bits for non-simple codes */
1159 inflate_huft * FAR*,/* result: starting table */
1160 uIntf *, /* maximum lookup bits (returns actual) */
1161 z_stream *)); /* for zalloc function */
1163 local voidpf falloc OF((
1164 voidpf, /* opaque pointer (not used) */
1165 uInt, /* number of items */
1166 uInt)); /* size of item */
1168 local void ffree OF((
1169 voidpf q, /* opaque pointer (not used) */
1170 voidpf p, /* what to free (not used) */
1171 uInt n)); /* number of bytes (not used) */
1173 /* Tables for deflate from PKZIP's appnote.txt. */
1174 local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */
1175 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1176 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1177 /* actually lengths - 2; also see note #13 above about 258 */
1178 local uInt cplext[] = { /* Extra bits for literal codes 257..285 */
1179 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1180 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */
1181 local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */
1182 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1183 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1184 8193, 12289, 16385, 24577};
1185 local uInt cpdext[] = { /* Extra bits for distance codes */
1186 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1187 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1188 12, 12, 13, 13};
1191 Huffman code decoding is performed using a multi-level table lookup.
1192 The fastest way to decode is to simply build a lookup table whose
1193 size is determined by the longest code. However, the time it takes
1194 to build this table can also be a factor if the data being decoded
1195 is not very long. The most common codes are necessarily the
1196 shortest codes, so those codes dominate the decoding time, and hence
1197 the speed. The idea is you can have a shorter table that decodes the
1198 shorter, more probable codes, and then point to subsidiary tables for
1199 the longer codes. The time it costs to decode the longer codes is
1200 then traded against the time it takes to make longer tables.
1202 This results of this trade are in the variables lbits and dbits
1203 below. lbits is the number of bits the first level table for literal/
1204 length codes can decode in one step, and dbits is the same thing for
1205 the distance codes. Subsequent tables are also less than or equal to
1206 those sizes. These values may be adjusted either when all of the
1207 codes are shorter than that, in which case the longest code length in
1208 bits is used, or when the shortest code is *longer* than the requested
1209 table size, in which case the length of the shortest code in bits is
1210 used.
1212 There are two different values for the two tables, since they code a
1213 different number of possibilities each. The literal/length table
1214 codes 286 possible values, or in a flat code, a little over eight
1215 bits. The distance table codes 30 possible values, or a little less
1216 than five bits, flat. The optimum values for speed end up being
1217 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
1218 The optimum values may differ though from machine to machine, and
1219 possibly even between compilers. Your mileage may vary.
1223 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
1224 #define BMAX 15 /* maximum bit length of any code */
1225 #define N_MAX 288 /* maximum number of codes in any set */
1227 #ifdef DEBUG_ZLIB
1228 uInt inflate_hufts;
1229 #endif
1231 local int huft_build(b, n, s, d, e, t, m, zs)
1232 uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
1233 uInt n; /* number of codes (assumed <= N_MAX) */
1234 uInt s; /* number of simple-valued codes (0..s-1) */
1235 uIntf *d; /* list of base values for non-simple codes */
1236 uIntf *e; /* list of extra bits for non-simple codes */
1237 inflate_huft * FAR *t; /* result: starting table */
1238 uIntf *m; /* maximum lookup bits, returns actual */
1239 z_stream *zs; /* for zalloc function */
1240 /* Given a list of code lengths and a maximum table size, make a set of
1241 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
1242 if the given code set is incomplete (the tables are still built in this
1243 case), Z_DATA_ERROR if the input is invalid (all zero length codes or an
1244 over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */
1247 uInt a; /* counter for codes of length k */
1248 uInt c[BMAX+1]; /* bit length count table */
1249 uInt f; /* i repeats in table every f entries */
1250 int g; /* maximum code length */
1251 int h; /* table level */
1252 register uInt i; /* counter, current code */
1253 register uInt j; /* counter */
1254 register int k; /* number of bits in current code */
1255 int l; /* bits per table (returned in m) */
1256 register uIntf *p; /* pointer into c[], b[], or v[] */
1257 inflate_huft *q; /* points to current table */
1258 struct inflate_huft_s r; /* table entry for structure assignment */
1259 inflate_huft *u[BMAX]; /* table stack */
1260 uInt v[N_MAX]; /* values in order of bit length */
1261 register int w; /* bits before this table == (l * h) */
1262 uInt x[BMAX+1]; /* bit offsets, then code stack */
1263 uIntf *xp; /* pointer into x */
1264 int y; /* number of dummy codes added */
1265 uInt z; /* number of entries in current table */
1268 /* Generate counts for each bit length */
1269 p = c;
1270 #define C0 *p++ = 0;
1271 #define C2 C0 C0 C0 C0
1272 #define C4 C2 C2 C2 C2
1273 C4 /* clear c[]--assume BMAX+1 is 16 */
1274 p = b; i = n;
1275 do {
1276 c[*p++]++; /* assume all entries <= BMAX */
1277 } while (--i);
1278 if (c[0] == n) /* null input--all zero length codes */
1280 *t = (inflate_huft *)Z_NULL;
1281 *m = 0;
1282 return Z_OK;
1286 /* Find minimum and maximum length, bound *m by those */
1287 l = *m;
1288 for (j = 1; j <= BMAX; j++)
1289 if (c[j])
1290 break;
1291 k = j; /* minimum code length */
1292 if ((uInt)l < j)
1293 l = j;
1294 for (i = BMAX; i; i--)
1295 if (c[i])
1296 break;
1297 g = i; /* maximum code length */
1298 if ((uInt)l > i)
1299 l = i;
1300 *m = l;
1303 /* Adjust last length count to fill out codes, if needed */
1304 for (y = 1 << j; j < i; j++, y <<= 1)
1305 if ((y -= c[j]) < 0)
1306 return Z_DATA_ERROR;
1307 if ((y -= c[i]) < 0)
1308 return Z_DATA_ERROR;
1309 c[i] += y;
1312 /* Generate starting offsets into the value table for each length */
1313 x[1] = j = 0;
1314 p = c + 1; xp = x + 2;
1315 while (--i) { /* note that i == g from above */
1316 *xp++ = (j += *p++);
1320 /* Make a table of values in order of bit lengths */
1321 p = b; i = 0;
1322 do {
1323 if ((j = *p++) != 0)
1324 v[x[j]++] = i;
1325 } while (++i < n);
1328 /* Generate the Huffman codes and for each, make the table entries */
1329 x[0] = i = 0; /* first Huffman code is zero */
1330 p = v; /* grab values in bit order */
1331 h = -1; /* no tables yet--level -1 */
1332 w = -l; /* bits decoded == (l * h) */
1333 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
1334 q = (inflate_huft *)Z_NULL; /* ditto */
1335 z = 0; /* ditto */
1337 /* go through the bit lengths (k already is bits in shortest code) */
1338 for (; k <= g; k++)
1340 a = c[k];
1341 while (a--)
1343 /* here i is the Huffman code of length k bits for value *p */
1344 /* make tables up to required level */
1345 while (k > w + l)
1347 h++;
1348 w += l; /* previous table always l bits */
1350 /* compute minimum size table less than or equal to l bits */
1351 z = (z = g - w) > (uInt)l ? l : z; /* table size upper limit */
1352 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
1353 { /* too few codes for k-w bit table */
1354 f -= a + 1; /* deduct codes from patterns left */
1355 xp = c + k;
1356 if (j < z)
1357 while (++j < z) /* try smaller tables up to z bits */
1359 if ((f <<= 1) <= *++xp)
1360 break; /* enough codes to use up j bits */
1361 f -= *xp; /* else deduct codes from patterns */
1364 z = 1 << j; /* table entries for j-bit table */
1366 /* allocate and link in new table */
1367 if ((q = (inflate_huft *)ZALLOC
1368 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
1370 if (h)
1371 inflate_trees_free(u[0], zs);
1372 return Z_MEM_ERROR; /* not enough memory */
1374 q->word.Nalloc = z + 1;
1375 #ifdef DEBUG_ZLIB
1376 inflate_hufts += z + 1;
1377 #endif
1378 *t = q + 1; /* link to list for huft_free() */
1379 *(t = &(q->next)) = Z_NULL;
1380 u[h] = ++q; /* table starts after link */
1382 /* connect to last table, if there is one */
1383 if (h)
1385 x[h] = i; /* save pattern for backing up */
1386 r.bits = (Byte)l; /* bits to dump before this table */
1387 r.exop = (Byte)j; /* bits in this table */
1388 r.next = q; /* pointer to this table */
1389 j = i >> (w - l); /* (get around Turbo C bug) */
1390 u[h-1][j] = r; /* connect to last table */
1394 /* set up table entry in r */
1395 r.bits = (Byte)(k - w);
1396 if (p >= v + n)
1397 r.exop = 128 + 64; /* out of values--invalid code */
1398 else if (*p < s)
1400 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
1401 r.base = *p++; /* simple code is just the value */
1403 else
1405 r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */
1406 r.base = d[*p++ - s];
1409 /* fill code-like entries with r */
1410 f = 1 << (k - w);
1411 for (j = i >> w; j < z; j += f)
1412 q[j] = r;
1414 /* backwards increment the k-bit code i */
1415 for (j = 1 << (k - 1); i & j; j >>= 1)
1416 i ^= j;
1417 i ^= j;
1419 /* backup over finished tables */
1420 while ((i & ((1 << w) - 1)) != x[h])
1422 h--; /* don't need to update q */
1423 w -= l;
1429 /* Return Z_BUF_ERROR if we were given an incomplete table */
1430 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
1434 local int inflate_trees_bits(c, bb, tb, z)
1435 uIntf *c; /* 19 code lengths */
1436 uIntf *bb; /* bits tree desired/actual depth */
1437 inflate_huft * FAR *tb; /* bits tree result */
1438 z_stream *z; /* for zfree function */
1440 int r;
1442 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
1443 if (r == Z_DATA_ERROR)
1444 z->msg = "oversubscribed dynamic bit lengths tree";
1445 else if (r == Z_BUF_ERROR)
1447 inflate_trees_free(*tb, z);
1448 z->msg = "incomplete dynamic bit lengths tree";
1449 r = Z_DATA_ERROR;
1451 return r;
1455 local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
1456 uInt nl; /* number of literal/length codes */
1457 uInt nd; /* number of distance codes */
1458 uIntf *c; /* that many (total) code lengths */
1459 uIntf *bl; /* literal desired/actual bit depth */
1460 uIntf *bd; /* distance desired/actual bit depth */
1461 inflate_huft * FAR *tl; /* literal/length tree result */
1462 inflate_huft * FAR *td; /* distance tree result */
1463 z_stream *z; /* for zfree function */
1465 int r;
1467 /* build literal/length tree */
1468 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
1470 if (r == Z_DATA_ERROR)
1471 z->msg = "oversubscribed literal/length tree";
1472 else if (r == Z_BUF_ERROR)
1474 inflate_trees_free(*tl, z);
1475 z->msg = "incomplete literal/length tree";
1476 r = Z_DATA_ERROR;
1478 return r;
1481 /* build distance tree */
1482 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
1484 if (r == Z_DATA_ERROR)
1485 z->msg = "oversubscribed literal/length tree";
1486 else if (r == Z_BUF_ERROR) {
1487 #ifdef PKZIP_BUG_WORKAROUND
1488 r = Z_OK;
1490 #else
1491 inflate_trees_free(*td, z);
1492 z->msg = "incomplete literal/length tree";
1493 r = Z_DATA_ERROR;
1495 inflate_trees_free(*tl, z);
1496 return r;
1497 #endif
1500 /* done */
1501 return Z_OK;
1505 /* build fixed tables only once--keep them here */
1506 local int fixed_lock = 0;
1507 local int fixed_built = 0;
1508 #define FIXEDH 530 /* number of hufts used by fixed tables */
1509 local uInt fixed_left = FIXEDH;
1510 local inflate_huft fixed_mem[FIXEDH];
1511 local uInt fixed_bl;
1512 local uInt fixed_bd;
1513 local inflate_huft *fixed_tl;
1514 local inflate_huft *fixed_td;
1517 local voidpf falloc(q, n, s)
1518 voidpf q; /* opaque pointer (not used) */
1519 uInt n; /* number of items */
1520 uInt s; /* size of item */
1522 Assert(s == sizeof(inflate_huft) && n <= fixed_left,
1523 "inflate_trees falloc overflow");
1524 if (q) s++; /* to make some compilers happy */
1525 fixed_left -= n;
1526 return (voidpf)(fixed_mem + fixed_left);
1530 local void ffree(q, p, n)
1531 voidpf q;
1532 voidpf p;
1533 uInt n;
1535 Assert(0, "inflate_trees ffree called!");
1536 if (q) q = p; /* to make some compilers happy */
1540 local int inflate_trees_fixed(bl, bd, tl, td)
1541 uIntf *bl; /* literal desired/actual bit depth */
1542 uIntf *bd; /* distance desired/actual bit depth */
1543 inflate_huft * FAR *tl; /* literal/length tree result */
1544 inflate_huft * FAR *td; /* distance tree result */
1546 /* build fixed tables if not built already--lock out other instances */
1547 while (++fixed_lock > 1)
1548 fixed_lock--;
1549 if (!fixed_built)
1551 int k; /* temporary variable */
1552 unsigned c[288]; /* length list for huft_build */
1553 z_stream z; /* for falloc function */
1555 /* set up fake z_stream for memory routines */
1556 z.zalloc = falloc;
1557 z.zfree = ffree;
1558 z.opaque = Z_NULL;
1560 /* literal table */
1561 for (k = 0; k < 144; k++)
1562 c[k] = 8;
1563 for (; k < 256; k++)
1564 c[k] = 9;
1565 for (; k < 280; k++)
1566 c[k] = 7;
1567 for (; k < 288; k++)
1568 c[k] = 8;
1569 fixed_bl = 7;
1570 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
1572 /* distance table */
1573 for (k = 0; k < 30; k++)
1574 c[k] = 5;
1575 fixed_bd = 5;
1576 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
1578 /* done */
1579 fixed_built = 1;
1581 fixed_lock--;
1582 *bl = fixed_bl;
1583 *bd = fixed_bd;
1584 *tl = fixed_tl;
1585 *td = fixed_td;
1586 return Z_OK;
1590 local int inflate_trees_free(t, z)
1591 inflate_huft *t; /* table to free */
1592 z_stream *z; /* for zfree function */
1593 /* Free the malloc'ed tables built by huft_build(), which makes a linked
1594 list of the tables it made, with the links in a dummy first entry of
1595 each table. */
1597 register inflate_huft *p, *q;
1599 /* Go through linked list, freeing from the malloced (t[-1]) address. */
1600 p = t;
1601 while (p != Z_NULL)
1603 q = (--p)->next;
1604 ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
1605 p = q;
1607 return Z_OK;
1610 /*+++++*/
1611 /* infcodes.c -- process literals and length/distance pairs
1612 * Copyright (C) 1995 Mark Adler
1613 * For conditions of distribution and use, see copyright notice in zlib.h
1616 /* simplify the use of the inflate_huft type with some defines */
1617 #define base more.Base
1618 #define next more.Next
1619 #define exop word.what.Exop
1620 #define bits word.what.Bits
1622 /* inflate codes private state */
1623 struct inflate_codes_state {
1625 /* mode */
1626 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
1627 START, /* x: set up for LEN */
1628 LEN, /* i: get length/literal/eob next */
1629 LENEXT, /* i: getting length extra (have base) */
1630 DIST, /* i: get distance next */
1631 DISTEXT, /* i: getting distance extra */
1632 COPY, /* o: copying bytes in window, waiting for space */
1633 LIT, /* o: got literal, waiting for output space */
1634 WASH, /* o: got eob, possibly still output waiting */
1635 END, /* x: got eob and all data flushed */
1636 BADCODE} /* x: got error */
1637 mode; /* current inflate_codes mode */
1639 /* mode dependent information */
1640 uInt len;
1641 union {
1642 struct {
1643 inflate_huft *tree; /* pointer into tree */
1644 uInt need; /* bits needed */
1645 } code; /* if LEN or DIST, where in tree */
1646 uInt lit; /* if LIT, literal */
1647 struct {
1648 uInt get; /* bits to get for extra */
1649 uInt dist; /* distance back to copy from */
1650 } copy; /* if EXT or COPY, where and how much */
1651 } sub; /* submode */
1653 /* mode independent information */
1654 Byte lbits; /* ltree bits decoded per branch */
1655 Byte dbits; /* dtree bits decoder per branch */
1656 inflate_huft *ltree; /* literal/length/eob tree */
1657 inflate_huft *dtree; /* distance tree */
1662 local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
1663 uInt bl, bd;
1664 inflate_huft *tl, *td;
1665 z_stream *z;
1667 inflate_codes_statef *c;
1669 if ((c = (inflate_codes_statef *)
1670 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
1672 c->mode = START;
1673 c->lbits = (Byte)bl;
1674 c->dbits = (Byte)bd;
1675 c->ltree = tl;
1676 c->dtree = td;
1677 Tracev((stderr, "inflate: codes new\n"));
1679 return c;
1683 local int inflate_codes(s, z, r)
1684 inflate_blocks_statef *s;
1685 z_stream *z;
1686 int r;
1688 uInt j; /* temporary storage */
1689 inflate_huft *t; /* temporary pointer */
1690 uInt e; /* extra bits or operation */
1691 uLong b; /* bit buffer */
1692 uInt k; /* bits in bit buffer */
1693 Bytef *p; /* input data pointer */
1694 uInt n; /* bytes available there */
1695 Bytef *q; /* output window write pointer */
1696 uInt m; /* bytes to end of window or read pointer */
1697 Bytef *f; /* pointer to copy strings from */
1698 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
1700 /* copy input/output information to locals (UPDATE macro restores) */
1701 LOAD
1703 /* process input and output based on current state */
1704 while (1) switch (c->mode)
1705 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
1706 case START: /* x: set up for LEN */
1707 #ifndef SLOW
1708 if (m >= 258 && n >= 10)
1710 UPDATE
1711 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
1712 LOAD
1713 if (r != Z_OK)
1715 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
1716 break;
1719 #endif /* !SLOW */
1720 c->sub.code.need = c->lbits;
1721 c->sub.code.tree = c->ltree;
1722 c->mode = LEN;
1723 case LEN: /* i: get length/literal/eob next */
1724 j = c->sub.code.need;
1725 NEEDBITS(j)
1726 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1727 DUMPBITS(t->bits)
1728 e = (uInt)(t->exop);
1729 if (e == 0) /* literal */
1731 c->sub.lit = t->base;
1732 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
1733 "inflate: literal '%c'\n" :
1734 "inflate: literal 0x%02x\n", t->base));
1735 c->mode = LIT;
1736 break;
1738 if (e & 16) /* length */
1740 c->sub.copy.get = e & 15;
1741 c->len = t->base;
1742 c->mode = LENEXT;
1743 break;
1745 if ((e & 64) == 0) /* next table */
1747 c->sub.code.need = e;
1748 c->sub.code.tree = t->next;
1749 break;
1751 if (e & 32) /* end of block */
1753 Tracevv((stderr, "inflate: end of block\n"));
1754 c->mode = WASH;
1755 break;
1757 c->mode = BADCODE; /* invalid code */
1758 z->msg = "invalid literal/length code";
1759 r = Z_DATA_ERROR;
1760 LEAVE
1761 case LENEXT: /* i: getting length extra (have base) */
1762 j = c->sub.copy.get;
1763 NEEDBITS(j)
1764 c->len += (uInt)b & inflate_mask[j];
1765 DUMPBITS(j)
1766 c->sub.code.need = c->dbits;
1767 c->sub.code.tree = c->dtree;
1768 Tracevv((stderr, "inflate: length %u\n", c->len));
1769 c->mode = DIST;
1770 case DIST: /* i: get distance next */
1771 j = c->sub.code.need;
1772 NEEDBITS(j)
1773 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1774 DUMPBITS(t->bits)
1775 e = (uInt)(t->exop);
1776 if (e & 16) /* distance */
1778 c->sub.copy.get = e & 15;
1779 c->sub.copy.dist = t->base;
1780 c->mode = DISTEXT;
1781 break;
1783 if ((e & 64) == 0) /* next table */
1785 c->sub.code.need = e;
1786 c->sub.code.tree = t->next;
1787 break;
1789 c->mode = BADCODE; /* invalid code */
1790 z->msg = "invalid distance code";
1791 r = Z_DATA_ERROR;
1792 LEAVE
1793 case DISTEXT: /* i: getting distance extra */
1794 j = c->sub.copy.get;
1795 NEEDBITS(j)
1796 c->sub.copy.dist += (uInt)b & inflate_mask[j];
1797 DUMPBITS(j)
1798 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
1799 c->mode = COPY;
1800 case COPY: /* o: copying bytes in window, waiting for space */
1801 #ifndef __TURBOC__ /* Turbo C bug for following expression */
1802 f = (uInt)(q - s->window) < c->sub.copy.dist ?
1803 s->end - (c->sub.copy.dist - (q - s->window)) :
1804 q - c->sub.copy.dist;
1805 #else
1806 f = q - c->sub.copy.dist;
1807 if ((uInt)(q - s->window) < c->sub.copy.dist)
1808 f = s->end - (c->sub.copy.dist - (q - s->window));
1809 #endif
1810 while (c->len)
1812 NEEDOUT
1813 OUTBYTE(*f++)
1814 if (f == s->end)
1815 f = s->window;
1816 c->len--;
1818 c->mode = START;
1819 break;
1820 case LIT: /* o: got literal, waiting for output space */
1821 NEEDOUT
1822 OUTBYTE(c->sub.lit)
1823 c->mode = START;
1824 break;
1825 case WASH: /* o: got eob, possibly more output */
1826 FLUSH
1827 if (s->read != s->write)
1828 LEAVE
1829 c->mode = END;
1830 case END:
1831 r = Z_STREAM_END;
1832 LEAVE
1833 case BADCODE: /* x: got error */
1834 r = Z_DATA_ERROR;
1835 LEAVE
1836 default:
1837 r = Z_STREAM_ERROR;
1838 LEAVE
1843 local void inflate_codes_free(c, z)
1844 inflate_codes_statef *c;
1845 z_stream *z;
1847 ZFREE(z, c, sizeof(struct inflate_codes_state));
1848 Tracev((stderr, "inflate: codes free\n"));
1851 /*+++++*/
1852 /* inflate_util.c -- data and routines common to blocks and codes
1853 * Copyright (C) 1995 Mark Adler
1854 * For conditions of distribution and use, see copyright notice in zlib.h
1857 /* copy as much as possible from the sliding window to the output area */
1858 local int inflate_flush(s, z, r)
1859 inflate_blocks_statef *s;
1860 z_stream *z;
1861 int r;
1863 uInt n;
1864 Bytef *p, *q;
1866 /* local copies of source and destination pointers */
1867 p = z->next_out;
1868 q = s->read;
1870 /* compute number of bytes to copy as far as end of window */
1871 n = (uInt)((q <= s->write ? s->write : s->end) - q);
1872 if (n > z->avail_out) n = z->avail_out;
1873 if (n && r == Z_BUF_ERROR) r = Z_OK;
1875 /* update counters */
1876 z->avail_out -= n;
1877 z->total_out += n;
1879 /* update check information */
1880 if (s->checkfn != Z_NULL)
1881 s->check = (*s->checkfn)(s->check, q, n);
1883 /* output callback */
1884 if (z->outcb != Z_NULL)
1885 (*z->outcb)(q, n);
1887 /* copy as far as end of window */
1888 zmemcpy(p, q, n);
1889 p += n;
1890 q += n;
1892 /* see if more to copy at beginning of window */
1893 if (q == s->end)
1895 /* wrap pointers */
1896 q = s->window;
1897 if (s->write == s->end)
1898 s->write = s->window;
1900 /* compute bytes to copy */
1901 n = (uInt)(s->write - q);
1902 if (n > z->avail_out) n = z->avail_out;
1903 if (n && r == Z_BUF_ERROR) r = Z_OK;
1905 /* update counters */
1906 z->avail_out -= n;
1907 z->total_out += n;
1909 /* update check information */
1910 if (s->checkfn != Z_NULL)
1911 s->check = (*s->checkfn)(s->check, q, n);
1913 /* output callback */
1914 if (z->outcb != Z_NULL)
1915 (*z->outcb)(q, n);
1917 /* copy */
1918 zmemcpy(p, q, n);
1919 p += n;
1920 q += n;
1923 /* update pointers */
1924 z->next_out = p;
1925 s->read = q;
1927 /* done */
1928 return r;
1932 /*+++++*/
1933 /* inffast.c -- process literals and length/distance pairs fast
1934 * Copyright (C) 1995 Mark Adler
1935 * For conditions of distribution and use, see copyright notice in zlib.h
1938 /* simplify the use of the inflate_huft type with some defines */
1939 #define base more.Base
1940 #define next more.Next
1941 #define exop word.what.Exop
1942 #define bits word.what.Bits
1944 /* macros for bit input with no checking and for returning unused bytes */
1945 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
1946 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
1948 /* Called with number of bytes left to write in window at least 258
1949 (the maximum string length) and number of input bytes available
1950 at least ten. The ten bytes are six bytes for the longest length/
1951 distance pair plus four bytes for overloading the bit buffer. */
1953 local int inflate_fast(bl, bd, tl, td, s, z)
1954 uInt bl, bd;
1955 inflate_huft *tl, *td;
1956 inflate_blocks_statef *s;
1957 z_stream *z;
1959 inflate_huft *t; /* temporary pointer */
1960 uInt e; /* extra bits or operation */
1961 uLong b; /* bit buffer */
1962 uInt k; /* bits in bit buffer */
1963 Bytef *p; /* input data pointer */
1964 uInt n; /* bytes available there */
1965 Bytef *q; /* output window write pointer */
1966 uInt m; /* bytes to end of window or read pointer */
1967 uInt ml; /* mask for literal/length tree */
1968 uInt md; /* mask for distance tree */
1969 uInt c; /* bytes to copy */
1970 uInt d; /* distance back to copy from */
1971 Bytef *r; /* copy source pointer */
1973 /* load input, output, bit values */
1974 LOAD
1976 /* initialize masks */
1977 ml = inflate_mask[bl];
1978 md = inflate_mask[bd];
1980 /* do until not enough input or output space for fast loop */
1981 do { /* assume called with m >= 258 && n >= 10 */
1982 /* get literal/length code */
1983 GRABBITS(20) /* max bits for literal/length code */
1984 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
1986 DUMPBITS(t->bits)
1987 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
1988 "inflate: * literal '%c'\n" :
1989 "inflate: * literal 0x%02x\n", t->base));
1990 *q++ = (Byte)t->base;
1991 m--;
1992 continue;
1994 do {
1995 DUMPBITS(t->bits)
1996 if (e & 16)
1998 /* get extra bits for length */
1999 e &= 15;
2000 c = t->base + ((uInt)b & inflate_mask[e]);
2001 DUMPBITS(e)
2002 Tracevv((stderr, "inflate: * length %u\n", c));
2004 /* decode distance base of block to copy */
2005 GRABBITS(15); /* max bits for distance code */
2006 e = (t = td + ((uInt)b & md))->exop;
2007 do {
2008 DUMPBITS(t->bits)
2009 if (e & 16)
2011 /* get extra bits to add to distance base */
2012 e &= 15;
2013 GRABBITS(e) /* get extra bits (up to 13) */
2014 d = t->base + ((uInt)b & inflate_mask[e]);
2015 DUMPBITS(e)
2016 Tracevv((stderr, "inflate: * distance %u\n", d));
2018 /* do the copy */
2019 m -= c;
2020 if ((uInt)(q - s->window) >= d) /* offset before dest */
2021 { /* just copy */
2022 r = q - d;
2023 *q++ = *r++; c--; /* minimum count is three, */
2024 *q++ = *r++; c--; /* so unroll loop a little */
2026 else /* else offset after destination */
2028 e = d - (q - s->window); /* bytes from offset to end */
2029 r = s->end - e; /* pointer to offset */
2030 if (c > e) /* if source crosses, */
2032 c -= e; /* copy to end of window */
2033 do {
2034 *q++ = *r++;
2035 } while (--e);
2036 r = s->window; /* copy rest from start of window */
2039 do { /* copy all or what's left */
2040 *q++ = *r++;
2041 } while (--c);
2042 break;
2044 else if ((e & 64) == 0)
2045 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
2046 else
2048 z->msg = "invalid distance code";
2049 UNGRAB
2050 UPDATE
2051 return Z_DATA_ERROR;
2053 } while (1);
2054 break;
2056 if ((e & 64) == 0)
2058 if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
2060 DUMPBITS(t->bits)
2061 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
2062 "inflate: * literal '%c'\n" :
2063 "inflate: * literal 0x%02x\n", t->base));
2064 *q++ = (Byte)t->base;
2065 m--;
2066 break;
2069 else if (e & 32)
2071 Tracevv((stderr, "inflate: * end of block\n"));
2072 UNGRAB
2073 UPDATE
2074 return Z_STREAM_END;
2076 else
2078 z->msg = "invalid literal/length code";
2079 UNGRAB
2080 UPDATE
2081 return Z_DATA_ERROR;
2083 } while (1);
2084 } while (m >= 258 && n >= 10);
2086 /* not enough input or output--restore pointers and return */
2087 UNGRAB
2088 UPDATE
2089 return Z_OK;
2093 /*+++++*/
2094 /* zutil.c -- target dependent utility functions for the compression library
2095 * Copyright (C) 1995 Jean-loup Gailly.
2096 * For conditions of distribution and use, see copyright notice in zlib.h
2099 /* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */
2101 char *zlib_version = ZLIB_VERSION;
2103 char *z_errmsg[] = {
2104 "stream end", /* Z_STREAM_END 1 */
2105 "", /* Z_OK 0 */
2106 "file error", /* Z_ERRNO (-1) */
2107 "stream error", /* Z_STREAM_ERROR (-2) */
2108 "data error", /* Z_DATA_ERROR (-3) */
2109 "insufficient memory", /* Z_MEM_ERROR (-4) */
2110 "buffer error", /* Z_BUF_ERROR (-5) */
2111 ""};
2114 /*+++++*/
2115 /* adler32.c -- compute the Adler-32 checksum of a data stream
2116 * Copyright (C) 1995 Mark Adler
2117 * For conditions of distribution and use, see copyright notice in zlib.h
2120 /* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */
2122 #define BASE 65521L /* largest prime smaller than 65536 */
2123 #define NMAX 5552
2124 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
2126 #define DO1(buf) {s1 += *buf++; s2 += s1;}
2127 #define DO2(buf) DO1(buf); DO1(buf);
2128 #define DO4(buf) DO2(buf); DO2(buf);
2129 #define DO8(buf) DO4(buf); DO4(buf);
2130 #define DO16(buf) DO8(buf); DO8(buf);
2132 /* ========================================================================= */
2133 uLong adler32(adler, buf, len)
2134 uLong adler;
2135 Bytef *buf;
2136 uInt len;
2138 unsigned long s1 = adler & 0xffff;
2139 unsigned long s2 = (adler >> 16) & 0xffff;
2140 int k;
2142 if (buf == Z_NULL) return 1L;
2144 while (len > 0) {
2145 k = len < NMAX ? len : NMAX;
2146 len -= k;
2147 while (k >= 16) {
2148 DO16(buf);
2149 k -= 16;
2151 if (k != 0) do {
2152 DO1(buf);
2153 } while (--k);
2154 s1 %= BASE;
2155 s2 %= BASE;
2157 return (s2 << 16) | s1;