File indexing completed on 2025-05-11 08:23:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #define _Z_UTIL_H
0029
0030 #include "zlib.h"
0031
0032 #ifndef local
0033 # define local static
0034 #endif
0035
0036
0037 #define FAR
0038
0039 typedef unsigned char uch;
0040 typedef uch FAR uchf;
0041 typedef unsigned short ush;
0042 typedef ush FAR ushf;
0043 typedef unsigned long ulg;
0044
0045 extern char *z_errmsg[];
0046
0047 #define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err)
0048
0049
0050 #ifndef NULL
0051 #define NULL ((void *) 0)
0052 #endif
0053
0054
0055
0056 #define DEFLATED 8
0057
0058 #ifndef DEF_WBITS
0059 # define DEF_WBITS MAX_WBITS
0060 #endif
0061
0062
0063 #if MAX_MEM_LEVEL >= 8
0064 # define DEF_MEM_LEVEL 8
0065 #else
0066 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
0067 #endif
0068
0069
0070 #define STORED_BLOCK 0
0071 #define STATIC_TREES 1
0072 #define DYN_TREES 2
0073
0074
0075 #define MIN_MATCH 3
0076 #define MAX_MATCH 258
0077
0078
0079
0080
0081 #include <string.h>
0082 #define zmemcpy memcpy
0083 #define zmemzero(dest, len) memset(dest, 0, len)
0084
0085
0086 #ifdef DEBUG_ZLIB
0087 # include <stdio.h>
0088 # ifndef verbose
0089 # define verbose 0
0090 # endif
0091 # define Assert(cond, msg) {if(!(cond)) Trace(msg);}
0092 # define Trace(x) printk(x)
0093 # define Tracev(x) {if (verbose) printk x ;}
0094 # define Tracevv(x) {if (verbose>1) printk x ;}
0095 # define Tracec(c,x) {if (verbose && (c)) printk x ;}
0096 # define Tracecv(c,x) {if (verbose>1 && (c)) printk x ;}
0097 #else
0098 # define Assert(cond,msg)
0099 # define Trace(x)
0100 # define Tracev(x)
0101 # define Tracevv(x)
0102 # define Tracec(c,x)
0103 # define Tracecv(c,x)
0104 #endif
0105
0106 typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len));
0107
0108
0109
0110
0111 #define ZALLOC(strm, items, size) \
0112 (*((strm)->zalloc))((strm)->opaque, (items), (size))
0113 #define ZFREE(strm, addr, size) \
0114 (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size))
0115 #define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);}
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 struct inflate_blocks_state;
0139 typedef struct inflate_blocks_state FAR inflate_blocks_statef;
0140
0141 local inflate_blocks_statef * inflate_blocks_new OF((
0142 z_stream *z,
0143 check_func c,
0144 uInt w));
0145
0146 local int inflate_blocks OF((
0147 inflate_blocks_statef *,
0148 z_stream *,
0149 int));
0150
0151 local void inflate_blocks_reset OF((
0152 inflate_blocks_statef *,
0153 z_stream *,
0154 uLongf *));
0155
0156 local int inflate_blocks_free OF((
0157 inflate_blocks_statef *,
0158 z_stream *,
0159 uLongf *));
0160
0161 local int inflate_addhistory OF((
0162 inflate_blocks_statef *,
0163 z_stream *));
0164
0165 local int inflate_packet_flush OF((
0166 inflate_blocks_statef *));
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 typedef struct inflate_huft_s FAR inflate_huft;
0183
0184 struct inflate_huft_s {
0185 union {
0186 struct {
0187 Byte Exop;
0188 Byte Bits;
0189 } what;
0190 uInt Nalloc;
0191 Bytef *pad;
0192 } word;
0193 union {
0194 uInt Base;
0195 inflate_huft *Next;
0196 } more;
0197 };
0198
0199 #ifdef DEBUG_ZLIB
0200 local uInt inflate_hufts;
0201 #endif
0202
0203 local int inflate_trees_bits OF((
0204 uIntf *,
0205 uIntf *,
0206 inflate_huft * FAR *,
0207 z_stream *));
0208
0209 local int inflate_trees_dynamic OF((
0210 uInt,
0211 uInt,
0212 uIntf *,
0213 uIntf *,
0214 uIntf *,
0215 inflate_huft * FAR *,
0216 inflate_huft * FAR *,
0217 z_stream *));
0218
0219 local int inflate_trees_fixed OF((
0220 uIntf *,
0221 uIntf *,
0222 inflate_huft * FAR *,
0223 inflate_huft * FAR *));
0224
0225 local int inflate_trees_free OF((
0226 inflate_huft *,
0227 z_stream *));
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240 struct inflate_codes_state;
0241 typedef struct inflate_codes_state FAR inflate_codes_statef;
0242
0243 local inflate_codes_statef *inflate_codes_new OF((
0244 uInt, uInt,
0245 inflate_huft *, inflate_huft *,
0246 z_stream *));
0247
0248 local int inflate_codes OF((
0249 inflate_blocks_statef *,
0250 z_stream *,
0251 int));
0252
0253 local void inflate_codes_free OF((
0254 inflate_codes_statef *,
0255 z_stream *));
0256
0257
0258
0259
0260
0261
0262
0263
0264 struct internal_state {
0265
0266
0267 enum {
0268 METHOD,
0269 FLAG,
0270 BLOCKS,
0271 CHECK4,
0272 CHECK3,
0273 CHECK2,
0274 CHECK1,
0275 DONE,
0276 BAD}
0277 mode;
0278
0279
0280 union {
0281 uInt method;
0282 struct {
0283 uLong was;
0284 uLong need;
0285 } check;
0286 uInt marker;
0287 } sub;
0288
0289
0290 int nowrap;
0291 uInt wbits;
0292 inflate_blocks_statef
0293 *blocks;
0294
0295 };
0296
0297 int inflateReset(z)
0298 z_stream *z;
0299 {
0300 uLong c;
0301
0302 if (z == Z_NULL || z->state == Z_NULL)
0303 return Z_STREAM_ERROR;
0304 z->total_in = z->total_out = 0;
0305 z->msg = Z_NULL;
0306 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
0307 inflate_blocks_reset(z->state->blocks, z, &c);
0308 Trace("inflate: reset\n");
0309 return Z_OK;
0310 }
0311
0312 int inflateEnd(z)
0313 z_stream *z;
0314 {
0315 uLong c;
0316
0317 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
0318 return Z_STREAM_ERROR;
0319 if (z->state->blocks != Z_NULL)
0320 inflate_blocks_free(z->state->blocks, z, &c);
0321 ZFREE(z, z->state, sizeof(struct internal_state));
0322 z->state = Z_NULL;
0323 Trace("inflate: end\n");
0324 return Z_OK;
0325 }
0326
0327 int inflateInit2(z, w)
0328 z_stream *z;
0329 int w;
0330 {
0331
0332 if (z == Z_NULL)
0333 return Z_STREAM_ERROR;
0334
0335
0336 if ((z->state = (struct internal_state FAR *)
0337 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
0338 return Z_MEM_ERROR;
0339 z->state->blocks = Z_NULL;
0340
0341
0342 z->state->nowrap = 0;
0343 if (w < 0)
0344 {
0345 w = - w;
0346 z->state->nowrap = 1;
0347 }
0348
0349
0350 if (w < 8 || w > 15)
0351 {
0352 inflateEnd(z);
0353 return Z_STREAM_ERROR;
0354 }
0355 z->state->wbits = (uInt)w;
0356
0357
0358 if ((z->state->blocks =
0359 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w))
0360 == Z_NULL)
0361 {
0362 inflateEnd(z);
0363 return Z_MEM_ERROR;
0364 }
0365 Trace("inflate: allocated\n");
0366
0367
0368 inflateReset(z);
0369 return Z_OK;
0370 }
0371
0372 int inflateInit(z)
0373 z_stream *z;
0374 {
0375 return inflateInit2(z, DEF_WBITS);
0376 }
0377
0378 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
0379 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
0380
0381 int inflate(z, f)
0382 z_stream *z;
0383 int f;
0384 {
0385 int r;
0386 uInt b;
0387
0388 if (z == Z_NULL || z->next_in == Z_NULL)
0389 return Z_STREAM_ERROR;
0390 r = Z_BUF_ERROR;
0391 while (1) switch (z->state->mode)
0392 {
0393 case METHOD:
0394 NEEDBYTE
0395 if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
0396 {
0397 z->state->mode = BAD;
0398 z->msg = "unknown compression method";
0399 z->state->sub.marker = 5;
0400 break;
0401 }
0402 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
0403 {
0404 z->state->mode = BAD;
0405 z->msg = "invalid window size";
0406 z->state->sub.marker = 5;
0407 break;
0408 }
0409 z->state->mode = FLAG;
0410 case FLAG:
0411 NEEDBYTE
0412 if ((b = NEXTBYTE) & 0x20)
0413 {
0414 z->state->mode = BAD;
0415 z->msg = "invalid reserved bit";
0416 z->state->sub.marker = 5;
0417 break;
0418 }
0419 if (((z->state->sub.method << 8) + b) % 31)
0420 {
0421 z->state->mode = BAD;
0422 z->msg = "incorrect header check";
0423 z->state->sub.marker = 5;
0424 break;
0425 }
0426 Trace("inflate: zlib header ok\n");
0427 z->state->mode = BLOCKS;
0428 case BLOCKS:
0429 r = inflate_blocks(z->state->blocks, z, r);
0430 if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
0431 r = inflate_packet_flush(z->state->blocks);
0432 if (r == Z_DATA_ERROR)
0433 {
0434 z->state->mode = BAD;
0435 z->state->sub.marker = 0;
0436 break;
0437 }
0438 if (r != Z_STREAM_END)
0439 return r;
0440 r = Z_OK;
0441 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
0442 if (z->state->nowrap)
0443 {
0444 z->state->mode = DONE;
0445 break;
0446 }
0447 z->state->mode = CHECK4;
0448 case CHECK4:
0449 NEEDBYTE
0450 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
0451 z->state->mode = CHECK3;
0452 case CHECK3:
0453 NEEDBYTE
0454 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
0455 z->state->mode = CHECK2;
0456 case CHECK2:
0457 NEEDBYTE
0458 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
0459 z->state->mode = CHECK1;
0460 case CHECK1:
0461 NEEDBYTE
0462 z->state->sub.check.need += (uLong)NEXTBYTE;
0463
0464 if (z->state->sub.check.was != z->state->sub.check.need)
0465 {
0466 z->state->mode = BAD;
0467 z->msg = "incorrect data check";
0468 z->state->sub.marker = 5;
0469 break;
0470 }
0471 Trace( "inflate: zlib check ok\n");
0472 z->state->mode = DONE;
0473 case DONE:
0474 return Z_STREAM_END;
0475 case BAD:
0476 return Z_DATA_ERROR;
0477 default:
0478 return Z_STREAM_ERROR;
0479 }
0480
0481 empty:
0482 if (f != Z_PACKET_FLUSH)
0483 return r;
0484 z->state->mode = BAD;
0485 z->state->sub.marker = 0;
0486 return Z_DATA_ERROR;
0487 }
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498 int inflateIncomp(z)
0499 z_stream *z;
0500 {
0501 if (z->state->mode != BLOCKS)
0502 return Z_DATA_ERROR;
0503 return inflate_addhistory(z->state->blocks, z);
0504 }
0505
0506 int inflateSync(z)
0507 z_stream *z;
0508 {
0509 uInt n;
0510 Bytef *p;
0511 uInt m;
0512 uLong r, w;
0513
0514
0515 if (z == Z_NULL || z->state == Z_NULL)
0516 return Z_STREAM_ERROR;
0517 if (z->state->mode != BAD)
0518 {
0519 z->state->mode = BAD;
0520 z->state->sub.marker = 0;
0521 }
0522 if ((n = z->avail_in) == 0)
0523 return Z_BUF_ERROR;
0524 p = z->next_in;
0525 m = z->state->sub.marker;
0526
0527
0528 while (n && m < 4)
0529 {
0530 if (*p == (Byte)(m < 2 ? 0 : 0xff))
0531 m++;
0532 else if (*p)
0533 m = 0;
0534 else
0535 m = 4 - m;
0536 p++, n--;
0537 }
0538
0539
0540 z->total_in += p - z->next_in;
0541 z->next_in = p;
0542 z->avail_in = n;
0543 z->state->sub.marker = m;
0544
0545
0546 if (m != 4)
0547 return Z_DATA_ERROR;
0548 r = z->total_in; w = z->total_out;
0549 inflateReset(z);
0550 z->total_in = r; z->total_out = w;
0551 z->state->mode = BLOCKS;
0552 return Z_OK;
0553 }
0554
0555 #undef NEEDBYTE
0556 #undef NEXTBYTE
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570 struct inflate_blocks_state {
0571
0572
0573 enum {
0574 TYPE,
0575 LENS,
0576 STORED,
0577 TABLE,
0578 BTREE,
0579 DTREE,
0580 CODES,
0581 DRY,
0582 DONEB,
0583 BADB}
0584 mode;
0585
0586
0587 union {
0588 uInt left;
0589 struct {
0590 uInt table;
0591 uInt index;
0592 uIntf *blens;
0593 uInt bb;
0594 inflate_huft *tb;
0595 int nblens;
0596 } trees;
0597 struct {
0598 inflate_huft *tl, *td;
0599 inflate_codes_statef
0600 *codes;
0601 } decode;
0602 } sub;
0603 uInt last;
0604
0605
0606 uInt bitk;
0607 uLong bitb;
0608 Bytef *window;
0609 Bytef *end;
0610 Bytef *read;
0611 Bytef *write;
0612 check_func checkfn;
0613 uLong check;
0614
0615 };
0616
0617
0618
0619 #define UPDBITS {s->bitb=b;s->bitk=k;}
0620 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
0621 #define UPDOUT {s->write=q;}
0622 #define UPDATE {UPDBITS UPDIN UPDOUT}
0623 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
0624
0625 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
0626 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
0627 #define NEXTBYTE (n--,*p++)
0628 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
0629 #define DUMPBITS(j) {b>>=(j);k-=(j);}
0630
0631 #define WAVAIL (q<s->read?s->read-q-1:s->end-q)
0632 #define LOADOUT {q=s->write;m=WAVAIL;}
0633 #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}}
0634 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
0635 #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
0636 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
0637
0638 #define LOAD {LOADIN LOADOUT}
0639
0640
0641 local uInt inflate_mask[] = {
0642 0x0000,
0643 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
0644 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
0645 };
0646
0647
0648 local int inflate_flush OF((
0649 inflate_blocks_statef *,
0650 z_stream *,
0651 int));
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664 local int inflate_fast OF((
0665 uInt,
0666 uInt,
0667 inflate_huft *,
0668 inflate_huft *,
0669 inflate_blocks_statef *,
0670 z_stream *));
0671
0672
0673
0674
0675
0676
0677
0678
0679 local uInt border[] = {
0680 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727 local void inflate_blocks_reset(s, z, c)
0728 inflate_blocks_statef *s;
0729 z_stream *z;
0730 uLongf *c;
0731 {
0732 if (s->checkfn != Z_NULL)
0733 *c = s->check;
0734 if (s->mode == BTREE || s->mode == DTREE)
0735 ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
0736 if (s->mode == CODES)
0737 {
0738 inflate_codes_free(s->sub.decode.codes, z);
0739 inflate_trees_free(s->sub.decode.td, z);
0740 inflate_trees_free(s->sub.decode.tl, z);
0741 }
0742 s->mode = TYPE;
0743 s->bitk = 0;
0744 s->bitb = 0;
0745 s->read = s->write = s->window;
0746 if (s->checkfn != Z_NULL)
0747 s->check = (*s->checkfn)(0L, Z_NULL, 0);
0748 Trace("inflate: blocks reset\n");
0749 }
0750
0751 local inflate_blocks_statef *inflate_blocks_new(z, c, w)
0752 z_stream *z;
0753 check_func c;
0754 uInt w;
0755 {
0756 inflate_blocks_statef *s;
0757
0758 if ((s = (inflate_blocks_statef *)ZALLOC
0759 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
0760 return s;
0761 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
0762 {
0763 ZFREE(z, s, sizeof(struct inflate_blocks_state));
0764 return Z_NULL;
0765 }
0766 s->end = s->window + w;
0767 s->checkfn = c;
0768 s->mode = TYPE;
0769 Trace("inflate: blocks allocated\n");
0770 inflate_blocks_reset(s, z, &s->check);
0771 return s;
0772 }
0773
0774 local int inflate_blocks(s, z, r)
0775 inflate_blocks_statef *s;
0776 z_stream *z;
0777 int r;
0778 {
0779 uInt t;
0780 uLong b;
0781 uInt k;
0782 Bytef *p;
0783 uInt n;
0784 Bytef *q;
0785 uInt m;
0786
0787
0788 LOAD
0789
0790
0791 while (1) switch (s->mode)
0792 {
0793 case TYPE:
0794 NEEDBITS(3)
0795 t = (uInt)b & 7;
0796 s->last = t & 1;
0797 switch (t >> 1)
0798 {
0799 case 0:
0800 Trace(("inflate: stored block%s\n",
0801 s->last ? " (last)" : ""));
0802 DUMPBITS(3)
0803 t = k & 7;
0804 DUMPBITS(t)
0805 s->mode = LENS;
0806 break;
0807 case 1:
0808 Trace(( "inflate: fixed codes block%s\n",
0809 s->last ? " (last)" : ""));
0810 {
0811 uInt bl, bd;
0812 inflate_huft *tl, *td;
0813
0814 inflate_trees_fixed(&bl, &bd, &tl, &td);
0815 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
0816 if (s->sub.decode.codes == Z_NULL)
0817 {
0818 r = Z_MEM_ERROR;
0819 LEAVE
0820 }
0821 s->sub.decode.tl = Z_NULL;
0822 s->sub.decode.td = Z_NULL;
0823 }
0824 DUMPBITS(3)
0825 s->mode = CODES;
0826 break;
0827 case 2:
0828 Trace(( "inflate: dynamic codes block%s\n",
0829 s->last ? " (last)" : ""));
0830 DUMPBITS(3)
0831 s->mode = TABLE;
0832 break;
0833 case 3:
0834 DUMPBITS(3)
0835 s->mode = BADB;
0836 z->msg = "invalid block type";
0837 r = Z_DATA_ERROR;
0838 LEAVE
0839 }
0840 break;
0841 case LENS:
0842 NEEDBITS(32)
0843 if (((~b) >> 16) != (b & 0xffff))
0844 {
0845 s->mode = BADB;
0846 z->msg = "invalid stored block lengths";
0847 r = Z_DATA_ERROR;
0848 LEAVE
0849 }
0850 s->sub.left = (uInt)b & 0xffff;
0851 b = k = 0;
0852 Tracev(( "inflate: stored length %u\n", s->sub.left));
0853 s->mode = s->sub.left ? STORED : TYPE;
0854 break;
0855 case STORED:
0856 if (n == 0)
0857 LEAVE
0858 NEEDOUT
0859 t = s->sub.left;
0860 if (t > n) t = n;
0861 if (t > m) t = m;
0862 zmemcpy(q, p, t);
0863 p += t; n -= t;
0864 q += t; m -= t;
0865 if ((s->sub.left -= t) != 0)
0866 break;
0867 Tracev(( "inflate: stored end, %lu total out\n",
0868 z->total_out + (q >= s->read ? q - s->read :
0869 (s->end - s->read) + (q - s->window))));
0870 s->mode = s->last ? DRY : TYPE;
0871 break;
0872 case TABLE:
0873 NEEDBITS(14)
0874 s->sub.trees.table = t = (uInt)b & 0x3fff;
0875 #ifndef PKZIP_BUG_WORKAROUND
0876 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
0877 {
0878 s->mode = BADB;
0879 z->msg = "too many length or distance symbols";
0880 r = Z_DATA_ERROR;
0881 LEAVE
0882 }
0883 #endif
0884 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
0885 if (t < 19)
0886 t = 19;
0887 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
0888 {
0889 r = Z_MEM_ERROR;
0890 LEAVE
0891 }
0892 s->sub.trees.nblens = t;
0893 DUMPBITS(14)
0894 s->sub.trees.index = 0;
0895 Tracev(( "inflate: table sizes ok\n"));
0896 s->mode = BTREE;
0897 case BTREE:
0898 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
0899 {
0900 NEEDBITS(3)
0901 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
0902 DUMPBITS(3)
0903 }
0904 while (s->sub.trees.index < 19)
0905 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
0906 s->sub.trees.bb = 7;
0907 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
0908 &s->sub.trees.tb, z);
0909 if (t != Z_OK)
0910 {
0911 r = t;
0912 if (r == Z_DATA_ERROR)
0913 s->mode = BADB;
0914 LEAVE
0915 }
0916 s->sub.trees.index = 0;
0917 Tracev(( "inflate: bits tree ok\n"));
0918 s->mode = DTREE;
0919 case DTREE:
0920 while (t = s->sub.trees.table,
0921 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
0922 {
0923 inflate_huft *h;
0924 uInt i, j, c;
0925
0926 t = s->sub.trees.bb;
0927 NEEDBITS(t)
0928 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
0929 t = h->word.what.Bits;
0930 c = h->more.Base;
0931 if (c < 16)
0932 {
0933 DUMPBITS(t)
0934 s->sub.trees.blens[s->sub.trees.index++] = c;
0935 }
0936 else
0937 {
0938 i = c == 18 ? 7 : c - 14;
0939 j = c == 18 ? 11 : 3;
0940 NEEDBITS(t + i)
0941 DUMPBITS(t)
0942 j += (uInt)b & inflate_mask[i];
0943 DUMPBITS(i)
0944 i = s->sub.trees.index;
0945 t = s->sub.trees.table;
0946 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
0947 (c == 16 && i < 1))
0948 {
0949 s->mode = BADB;
0950 z->msg = "invalid bit length repeat";
0951 r = Z_DATA_ERROR;
0952 LEAVE
0953 }
0954 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
0955 do {
0956 s->sub.trees.blens[i++] = c;
0957 } while (--j);
0958 s->sub.trees.index = i;
0959 }
0960 }
0961 inflate_trees_free(s->sub.trees.tb, z);
0962 s->sub.trees.tb = Z_NULL;
0963 {
0964 uInt bl, bd;
0965 inflate_huft *tl, *td;
0966 inflate_codes_statef *c;
0967
0968 bl = 9;
0969 bd = 6;
0970 t = s->sub.trees.table;
0971 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
0972 s->sub.trees.blens, &bl, &bd, &tl, &td, z);
0973 if (t != Z_OK)
0974 {
0975 if (t == (uInt)Z_DATA_ERROR)
0976 s->mode = BADB;
0977 r = t;
0978 LEAVE
0979 }
0980 Tracev(( "inflate: trees ok\n"));
0981 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
0982 {
0983 inflate_trees_free(td, z);
0984 inflate_trees_free(tl, z);
0985 r = Z_MEM_ERROR;
0986 LEAVE
0987 }
0988 ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt));
0989 s->sub.decode.codes = c;
0990 s->sub.decode.tl = tl;
0991 s->sub.decode.td = td;
0992 }
0993 s->mode = CODES;
0994 case CODES:
0995 UPDATE
0996 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
0997 return inflate_flush(s, z, r);
0998 r = Z_OK;
0999 inflate_codes_free(s->sub.decode.codes, z);
1000 inflate_trees_free(s->sub.decode.td, z);
1001 inflate_trees_free(s->sub.decode.tl, z);
1002 LOAD
1003 Tracev(( "inflate: codes end, %lu total out\n",
1004 z->total_out + (q >= s->read ? q - s->read :
1005 (s->end - s->read) + (q - s->window))));
1006 if (!s->last)
1007 {
1008 s->mode = TYPE;
1009 break;
1010 }
1011 if (k > 7)
1012 {
1013 Assert(k < 16, "inflate_codes grabbed too many bytes")
1014 k -= 8;
1015 n++;
1016 p--;
1017 }
1018 s->mode = DRY;
1019 case DRY:
1020 FLUSH
1021 if (s->read != s->write)
1022 LEAVE
1023 s->mode = DONEB;
1024 case DONEB:
1025 r = Z_STREAM_END;
1026 LEAVE
1027 case BADB:
1028 r = Z_DATA_ERROR;
1029 LEAVE
1030 default:
1031 r = Z_STREAM_ERROR;
1032 LEAVE
1033 }
1034 }
1035
1036 local int inflate_blocks_free(s, z, c)
1037 inflate_blocks_statef *s;
1038 z_stream *z;
1039 uLongf *c;
1040 {
1041 inflate_blocks_reset(s, z, c);
1042 ZFREE(z, s->window, s->end - s->window);
1043 ZFREE(z, s, sizeof(struct inflate_blocks_state));
1044 Trace(( "inflate: blocks freed\n"));
1045 return Z_OK;
1046 }
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 local int inflate_addhistory(s, z)
1057 inflate_blocks_statef *s;
1058 z_stream *z;
1059 {
1060 uLong b;
1061 uInt k;
1062 uInt t;
1063 Bytef *p;
1064 uInt n;
1065 Bytef *q;
1066 uInt m;
1067
1068 if (s->read != s->write)
1069 return Z_STREAM_ERROR;
1070 if (s->mode != TYPE)
1071 return Z_DATA_ERROR;
1072
1073
1074 LOAD
1075
1076
1077
1078 while (n) {
1079 t = n;
1080
1081 if (t > m) t = m;
1082
1083 if (s->checkfn != Z_NULL)
1084 s->check = (*s->checkfn)(s->check, q, t);
1085 zmemcpy(q, p, t);
1086 q += t;
1087 p += t;
1088 n -= t;
1089 z->total_out += t;
1090 s->read = q;
1091
1092 if (q == s->end) {
1093 s->read = q = s->window;
1094 m = WAVAIL;
1095 }
1096 }
1097 UPDATE
1098 return Z_OK;
1099 }
1100
1101
1102
1103
1104
1105 local int inflate_packet_flush(s)
1106 inflate_blocks_statef *s;
1107 {
1108 if (s->mode != LENS)
1109 return Z_DATA_ERROR;
1110 s->mode = TYPE;
1111 return Z_OK;
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121 #define base more.Base
1122 #define next more.Next
1123 #define exop word.what.Exop
1124 #define bits word.what.Bits
1125
1126 local int huft_build OF((
1127 uIntf *,
1128 uInt,
1129 uInt,
1130 uIntf *,
1131 uIntf *,
1132 inflate_huft * FAR*,
1133 uIntf *,
1134 z_stream *));
1135
1136 local voidpf falloc OF((
1137 voidpf,
1138 uInt,
1139 uInt));
1140
1141 local void ffree OF((
1142 voidpf q,
1143 voidpf p,
1144 uInt n));
1145
1146
1147 local uInt cplens[] = {
1148 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1149 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1150
1151 local uInt cplext[] = {
1152 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1153 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192};
1154 local uInt cpdist[] = {
1155 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1156 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1157 8193, 12289, 16385, 24577};
1158 local uInt cpdext[] = {
1159 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1160 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1161 12, 12, 13, 13};
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196 #define BMAX 15
1197 #define N_MAX 288
1198
1199 #ifdef DEBUG_ZLIB
1200 uInt inflate_hufts;
1201 #endif
1202
1203 local int huft_build(b, n, s, d, e, t, m, zs)
1204 uIntf *b;
1205 uInt n;
1206 uInt s;
1207 uIntf *d;
1208 uIntf *e;
1209 inflate_huft * FAR *t;
1210 uIntf *m;
1211 z_stream *zs;
1212
1213
1214
1215
1216
1217 {
1218
1219 uInt a;
1220 uInt c[BMAX+1];
1221 uInt f;
1222 int g;
1223 int h;
1224 register uInt i;
1225 register uInt j;
1226 register int k;
1227 int l;
1228 register uIntf *p;
1229 inflate_huft *q;
1230 struct inflate_huft_s r;
1231 inflate_huft *u[BMAX];
1232 uInt v[N_MAX];
1233 register int w;
1234 uInt x[BMAX+1];
1235 uIntf *xp;
1236 int y;
1237 uInt z;
1238
1239
1240 p = c;
1241 #define C0 *p++ = 0;
1242 #define C2 C0 C0 C0 C0
1243 #define C4 C2 C2 C2 C2
1244 C4
1245 p = b; i = n;
1246 do {
1247 c[*p++]++;
1248 } while (--i);
1249 if (c[0] == n)
1250 {
1251 *t = (inflate_huft *)Z_NULL;
1252 *m = 0;
1253 return Z_OK;
1254 }
1255
1256
1257 l = *m;
1258 for (j = 1; j <= BMAX; j++)
1259 if (c[j])
1260 break;
1261 k = j;
1262 if ((uInt)l < j)
1263 l = j;
1264 for (i = BMAX; i; i--)
1265 if (c[i])
1266 break;
1267 g = i;
1268 if ((uInt)l > i)
1269 l = i;
1270 *m = l;
1271
1272
1273 for (y = 1 << j; j < i; j++, y <<= 1)
1274 if ((y -= c[j]) < 0)
1275 return Z_DATA_ERROR;
1276 if ((y -= c[i]) < 0)
1277 return Z_DATA_ERROR;
1278 c[i] += y;
1279
1280
1281 x[1] = j = 0;
1282 p = c + 1; xp = x + 2;
1283 while (--i) {
1284 *xp++ = (j += *p++);
1285 }
1286
1287
1288 p = b; i = 0;
1289 do {
1290 if ((j = *p++) != 0)
1291 v[x[j]++] = i;
1292 } while (++i < n);
1293
1294
1295 x[0] = i = 0;
1296 p = v;
1297 h = -1;
1298 w = -l;
1299 u[0] = (inflate_huft *)Z_NULL;
1300 q = (inflate_huft *)Z_NULL;
1301 z = 0;
1302
1303
1304 for (; k <= g; k++)
1305 {
1306 a = c[k];
1307 while (a--)
1308 {
1309
1310
1311 while (k > w + l)
1312 {
1313 h++;
1314 w += l;
1315
1316
1317 z = (z = g - w) > (uInt)l ? l : z;
1318 if ((f = 1 << (j = k - w)) > a + 1)
1319 {
1320 f -= a + 1;
1321 xp = c + k;
1322 if (j < z)
1323 while (++j < z)
1324 {
1325 if ((f <<= 1) <= *++xp)
1326 break;
1327 f -= *xp;
1328 }
1329 }
1330 z = 1 << j;
1331
1332
1333 if ((q = (inflate_huft *)ZALLOC
1334 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
1335 {
1336 if (h)
1337 inflate_trees_free(u[0], zs);
1338 return Z_MEM_ERROR;
1339 }
1340 q->word.Nalloc = z + 1;
1341 #ifdef DEBUG_ZLIB
1342 inflate_hufts += z + 1;
1343 #endif
1344 *t = q + 1;
1345 *(t = &(q->next)) = Z_NULL;
1346 u[h] = ++q;
1347
1348
1349 if (h)
1350 {
1351 x[h] = i;
1352 r.bits = (Byte)l;
1353 r.exop = (Byte)j;
1354 r.next = q;
1355 j = i >> (w - l);
1356 u[h-1][j] = r;
1357 }
1358 }
1359
1360
1361 r.bits = (Byte)(k - w);
1362 if (p >= v + n)
1363 r.exop = 128 + 64;
1364 else if (*p < s)
1365 {
1366 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);
1367 r.base = *p++;
1368 }
1369 else
1370 {
1371 r.exop = (Byte)e[*p - s] + 16 + 64;
1372 r.base = d[*p++ - s];
1373 }
1374
1375
1376 f = 1 << (k - w);
1377 for (j = i >> w; j < z; j += f)
1378 q[j] = r;
1379
1380
1381 for (j = 1 << (k - 1); i & j; j >>= 1)
1382 i ^= j;
1383 i ^= j;
1384
1385
1386 while ((i & ((1 << w) - 1)) != x[h])
1387 {
1388 h--;
1389 w -= l;
1390 }
1391 }
1392 }
1393
1394
1395 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
1396 }
1397
1398 local int inflate_trees_bits(c, bb, tb, z)
1399 uIntf *c;
1400 uIntf *bb;
1401 inflate_huft * FAR *tb;
1402 z_stream *z;
1403 {
1404 int r;
1405
1406 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
1407 if (r == Z_DATA_ERROR)
1408 z->msg = "oversubscribed dynamic bit lengths tree";
1409 else if (r == Z_BUF_ERROR)
1410 {
1411 inflate_trees_free(*tb, z);
1412 z->msg = "incomplete dynamic bit lengths tree";
1413 r = Z_DATA_ERROR;
1414 }
1415 return r;
1416 }
1417
1418 local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
1419 uInt nl;
1420 uInt nd;
1421 uIntf *c;
1422 uIntf *bl;
1423 uIntf *bd;
1424 inflate_huft * FAR *tl;
1425 inflate_huft * FAR *td;
1426 z_stream *z;
1427 {
1428 int r;
1429
1430
1431 if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK)
1432 {
1433 if (r == Z_DATA_ERROR)
1434 z->msg = "oversubscribed literal/length tree";
1435 else if (r == Z_BUF_ERROR)
1436 {
1437 inflate_trees_free(*tl, z);
1438 z->msg = "incomplete literal/length tree";
1439 r = Z_DATA_ERROR;
1440 }
1441 return r;
1442 }
1443
1444
1445 if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK)
1446 {
1447 if (r == Z_DATA_ERROR)
1448 z->msg = "oversubscribed literal/length tree";
1449 else if (r == Z_BUF_ERROR) {
1450 #ifdef PKZIP_BUG_WORKAROUND
1451 r = Z_OK;
1452 }
1453 #else
1454 inflate_trees_free(*td, z);
1455 z->msg = "incomplete literal/length tree";
1456 r = Z_DATA_ERROR;
1457 }
1458 inflate_trees_free(*tl, z);
1459 return r;
1460 #endif
1461 }
1462
1463
1464 return Z_OK;
1465 }
1466
1467
1468 local int fixed_lock = 0;
1469 local int fixed_built = 0;
1470 #define FIXEDH 530
1471 local uInt fixed_left = FIXEDH;
1472 local inflate_huft fixed_mem[FIXEDH];
1473 local uInt fixed_bl;
1474 local uInt fixed_bd;
1475 local inflate_huft *fixed_tl;
1476 local inflate_huft *fixed_td;
1477
1478 local voidpf falloc(q, n, s)
1479 voidpf q;
1480 uInt n;
1481 uInt s;
1482 {
1483 Assert(s == sizeof(inflate_huft) && n <= fixed_left,
1484 "inflate_trees falloc overflow");
1485 if (q) s++;
1486 fixed_left -= n;
1487 return (voidpf)(fixed_mem + fixed_left);
1488 }
1489
1490 local void ffree(q, p, n)
1491 voidpf q;
1492 voidpf p;
1493 uInt n;
1494 {
1495 Assert(0, "inflate_trees ffree called!");
1496 if (q) q = p;
1497 }
1498
1499 local int inflate_trees_fixed(bl, bd, tl, td)
1500 uIntf *bl;
1501 uIntf *bd;
1502 inflate_huft * FAR *tl;
1503 inflate_huft * FAR *td;
1504 {
1505
1506 while (++fixed_lock > 1)
1507 fixed_lock--;
1508 if (!fixed_built)
1509 {
1510 int k;
1511 unsigned c[288];
1512 z_stream z;
1513
1514
1515 z.zalloc = falloc;
1516 z.zfree = ffree;
1517 z.opaque = Z_NULL;
1518
1519
1520 for (k = 0; k < 144; k++)
1521 c[k] = 8;
1522 for (; k < 256; k++)
1523 c[k] = 9;
1524 for (; k < 280; k++)
1525 c[k] = 7;
1526 for (; k < 288; k++)
1527 c[k] = 8;
1528 fixed_bl = 7;
1529 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
1530
1531
1532 for (k = 0; k < 30; k++)
1533 c[k] = 5;
1534 fixed_bd = 5;
1535 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
1536
1537
1538 fixed_built = 1;
1539 }
1540 fixed_lock--;
1541 *bl = fixed_bl;
1542 *bd = fixed_bd;
1543 *tl = fixed_tl;
1544 *td = fixed_td;
1545 return Z_OK;
1546 }
1547
1548 local int inflate_trees_free(t, z)
1549 inflate_huft *t;
1550 z_stream *z;
1551
1552
1553
1554 {
1555 register inflate_huft *p, *q;
1556
1557
1558 p = t;
1559 while (p != Z_NULL)
1560 {
1561 q = (--p)->next;
1562 ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft));
1563 p = q;
1564 }
1565 return Z_OK;
1566 }
1567
1568
1569
1570
1571
1572
1573
1574
1575 #define base more.Base
1576 #define next more.Next
1577 #define exop word.what.Exop
1578 #define bits word.what.Bits
1579
1580
1581 struct inflate_codes_state {
1582
1583
1584 enum {
1585 START,
1586 LEN,
1587 LENEXT,
1588 DIST,
1589 DISTEXT,
1590 COPY,
1591 LIT,
1592 WASH,
1593 END,
1594 BADCODE}
1595 mode;
1596
1597
1598 uInt len;
1599 union {
1600 struct {
1601 inflate_huft *tree;
1602 uInt need;
1603 } code;
1604 uInt lit;
1605 struct {
1606 uInt get;
1607 uInt dist;
1608 } copy;
1609 } sub;
1610
1611
1612 Byte lbits;
1613 Byte dbits;
1614 inflate_huft *ltree;
1615 inflate_huft *dtree;
1616
1617 };
1618
1619 local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
1620 uInt bl, bd;
1621 inflate_huft *tl, *td;
1622 z_stream *z;
1623 {
1624 inflate_codes_statef *c;
1625
1626 if ((c = (inflate_codes_statef *)
1627 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
1628 {
1629 c->mode = START;
1630 c->lbits = (Byte)bl;
1631 c->dbits = (Byte)bd;
1632 c->ltree = tl;
1633 c->dtree = td;
1634 Tracev(( "inflate: codes new\n"));
1635 }
1636 return c;
1637 }
1638
1639 local int inflate_codes(s, z, r)
1640 inflate_blocks_statef *s;
1641 z_stream *z;
1642 int r;
1643 {
1644 uInt j;
1645 inflate_huft *t;
1646 uInt e;
1647 uLong b;
1648 uInt k;
1649 Bytef *p;
1650 uInt n;
1651 Bytef *q;
1652 uInt m;
1653 Bytef *f;
1654 inflate_codes_statef *c = s->sub.decode.codes;
1655
1656
1657 LOAD
1658
1659
1660 while (1) switch (c->mode)
1661 {
1662 case START:
1663 #ifndef SLOW
1664 if (m >= 258 && n >= 10)
1665 {
1666 UPDATE
1667 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
1668 LOAD
1669 if (r != Z_OK)
1670 {
1671 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
1672 break;
1673 }
1674 }
1675 #endif
1676 c->sub.code.need = c->lbits;
1677 c->sub.code.tree = c->ltree;
1678 c->mode = LEN;
1679 case LEN:
1680 j = c->sub.code.need;
1681 NEEDBITS(j)
1682 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1683 DUMPBITS(t->bits)
1684 e = (uInt)(t->exop);
1685 if (e == 0)
1686 {
1687 c->sub.lit = t->base;
1688 Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
1689 "inflate: literal '%c'\n" :
1690 "inflate: literal 0x%02x\n", t->base));
1691 c->mode = LIT;
1692 break;
1693 }
1694 if (e & 16)
1695 {
1696 c->sub.copy.get = e & 15;
1697 c->len = t->base;
1698 c->mode = LENEXT;
1699 break;
1700 }
1701 if ((e & 64) == 0)
1702 {
1703 c->sub.code.need = e;
1704 c->sub.code.tree = t->next;
1705 break;
1706 }
1707 if (e & 32)
1708 {
1709 Tracevv(( "inflate: end of block\n"));
1710 c->mode = WASH;
1711 break;
1712 }
1713 c->mode = BADCODE;
1714 z->msg = "invalid literal/length code";
1715 r = Z_DATA_ERROR;
1716 LEAVE
1717 case LENEXT:
1718 j = c->sub.copy.get;
1719 NEEDBITS(j)
1720 c->len += (uInt)b & inflate_mask[j];
1721 DUMPBITS(j)
1722 c->sub.code.need = c->dbits;
1723 c->sub.code.tree = c->dtree;
1724 Tracevv(( "inflate: length %u\n", c->len));
1725 c->mode = DIST;
1726 case DIST:
1727 j = c->sub.code.need;
1728 NEEDBITS(j)
1729 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
1730 DUMPBITS(t->bits)
1731 e = (uInt)(t->exop);
1732 if (e & 16)
1733 {
1734 c->sub.copy.get = e & 15;
1735 c->sub.copy.dist = t->base;
1736 c->mode = DISTEXT;
1737 break;
1738 }
1739 if ((e & 64) == 0)
1740 {
1741 c->sub.code.need = e;
1742 c->sub.code.tree = t->next;
1743 break;
1744 }
1745 c->mode = BADCODE;
1746 z->msg = "invalid distance code";
1747 r = Z_DATA_ERROR;
1748 LEAVE
1749 case DISTEXT:
1750 j = c->sub.copy.get;
1751 NEEDBITS(j)
1752 c->sub.copy.dist += (uInt)b & inflate_mask[j];
1753 DUMPBITS(j)
1754 Tracevv(( "inflate: distance %u\n", c->sub.copy.dist));
1755 c->mode = COPY;
1756 case COPY:
1757 #ifndef __TURBOC__
1758 f = (uInt)(q - s->window) < c->sub.copy.dist ?
1759 s->end - (c->sub.copy.dist - (q - s->window)) :
1760 q - c->sub.copy.dist;
1761 #else
1762 f = q - c->sub.copy.dist;
1763 if ((uInt)(q - s->window) < c->sub.copy.dist)
1764 f = s->end - (c->sub.copy.dist - (q - s->window));
1765 #endif
1766 while (c->len)
1767 {
1768 NEEDOUT
1769 OUTBYTE(*f++)
1770 if (f == s->end)
1771 f = s->window;
1772 c->len--;
1773 }
1774 c->mode = START;
1775 break;
1776 case LIT:
1777 NEEDOUT
1778 OUTBYTE(c->sub.lit)
1779 c->mode = START;
1780 break;
1781 case WASH:
1782 FLUSH
1783 if (s->read != s->write)
1784 LEAVE
1785 c->mode = END;
1786 case END:
1787 r = Z_STREAM_END;
1788 LEAVE
1789 case BADCODE:
1790 r = Z_DATA_ERROR;
1791 LEAVE
1792 default:
1793 r = Z_STREAM_ERROR;
1794 LEAVE
1795 }
1796 }
1797
1798 local void inflate_codes_free(c, z)
1799 inflate_codes_statef *c;
1800 z_stream *z;
1801 {
1802 ZFREE(z, c, sizeof(struct inflate_codes_state));
1803 Tracev(( "inflate: codes free\n"));
1804 }
1805
1806
1807
1808
1809
1810
1811
1812
1813 local int inflate_flush(s, z, r)
1814 inflate_blocks_statef *s;
1815 z_stream *z;
1816 int r;
1817 {
1818 uInt n;
1819 Bytef *p, *q;
1820
1821
1822 p = z->next_out;
1823 q = s->read;
1824
1825
1826 n = (uInt)((q <= s->write ? s->write : s->end) - q);
1827 if (n > z->avail_out) n = z->avail_out;
1828 if (n && r == Z_BUF_ERROR) r = Z_OK;
1829
1830
1831 z->avail_out -= n;
1832 z->total_out += n;
1833
1834
1835 if (s->checkfn != Z_NULL)
1836 s->check = (*s->checkfn)(s->check, q, n);
1837
1838
1839 zmemcpy(p, q, n);
1840 p += n;
1841 q += n;
1842
1843
1844 if (q == s->end)
1845 {
1846
1847 q = s->window;
1848 if (s->write == s->end)
1849 s->write = s->window;
1850
1851
1852 n = (uInt)(s->write - q);
1853 if (n > z->avail_out) n = z->avail_out;
1854 if (n && r == Z_BUF_ERROR) r = Z_OK;
1855
1856
1857 z->avail_out -= n;
1858 z->total_out += n;
1859
1860
1861 if (s->checkfn != Z_NULL)
1862 s->check = (*s->checkfn)(s->check, q, n);
1863
1864
1865 zmemcpy(p, q, n);
1866 p += n;
1867 q += n;
1868 }
1869
1870
1871 z->next_out = p;
1872 s->read = q;
1873
1874
1875 return r;
1876 }
1877
1878
1879
1880
1881
1882
1883
1884
1885 #define base more.Base
1886 #define next more.Next
1887 #define exop word.what.Exop
1888 #define bits word.what.Bits
1889
1890
1891 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
1892 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
1893
1894
1895
1896
1897
1898
1899 local int inflate_fast(bl, bd, tl, td, s, z)
1900 uInt bl, bd;
1901 inflate_huft *tl, *td;
1902 inflate_blocks_statef *s;
1903 z_stream *z;
1904 {
1905 inflate_huft *t;
1906 uInt e;
1907 uLong b;
1908 uInt k;
1909 Bytef *p;
1910 uInt n;
1911 Bytef *q;
1912 uInt m;
1913 uInt ml;
1914 uInt md;
1915 uInt c;
1916 uInt d;
1917 Bytef *r;
1918
1919
1920 LOAD
1921
1922
1923 ml = inflate_mask[bl];
1924 md = inflate_mask[bd];
1925
1926
1927 do {
1928
1929 GRABBITS(20)
1930 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
1931 {
1932 DUMPBITS(t->bits)
1933 Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
1934 "inflate: * literal '%c'\n" :
1935 "inflate: * literal 0x%02x\n", t->base));
1936 *q++ = (Byte)t->base;
1937 m--;
1938 continue;
1939 }
1940 do {
1941 DUMPBITS(t->bits)
1942 if (e & 16)
1943 {
1944
1945 e &= 15;
1946 c = t->base + ((uInt)b & inflate_mask[e]);
1947 DUMPBITS(e)
1948 Tracevv(( "inflate: * length %u\n", c));
1949
1950
1951 GRABBITS(15);
1952 e = (t = td + ((uInt)b & md))->exop;
1953 do {
1954 DUMPBITS(t->bits)
1955 if (e & 16)
1956 {
1957
1958 e &= 15;
1959 GRABBITS(e)
1960 d = t->base + ((uInt)b & inflate_mask[e]);
1961 DUMPBITS(e)
1962 Tracevv(( "inflate: * distance %u\n", d));
1963
1964
1965 m -= c;
1966 if ((uInt)(q - s->window) >= d)
1967 {
1968 r = q - d;
1969 *q++ = *r++; c--;
1970 *q++ = *r++; c--;
1971 }
1972 else
1973 {
1974 e = d - (q - s->window);
1975 r = s->end - e;
1976 if (c > e)
1977 {
1978 c -= e;
1979 do {
1980 *q++ = *r++;
1981 } while (--e);
1982 r = s->window;
1983 }
1984 }
1985 do {
1986 *q++ = *r++;
1987 } while (--c);
1988 break;
1989 }
1990 else if ((e & 64) == 0)
1991 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
1992 else
1993 {
1994 z->msg = "invalid distance code";
1995 UNGRAB
1996 UPDATE
1997 return Z_DATA_ERROR;
1998 }
1999 } while (1);
2000 break;
2001 }
2002 if ((e & 64) == 0)
2003 {
2004 if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
2005 {
2006 DUMPBITS(t->bits)
2007 Tracevv(( t->base >= 0x20 && t->base < 0x7f ?
2008 "inflate: * literal '%c'\n" :
2009 "inflate: * literal 0x%02x\n", t->base));
2010 *q++ = (Byte)t->base;
2011 m--;
2012 break;
2013 }
2014 }
2015 else if (e & 32)
2016 {
2017 Tracevv(( "inflate: * end of block\n"));
2018 UNGRAB
2019 UPDATE
2020 return Z_STREAM_END;
2021 }
2022 else
2023 {
2024 z->msg = "invalid literal/length code";
2025 UNGRAB
2026 UPDATE
2027 return Z_DATA_ERROR;
2028 }
2029 } while (1);
2030 } while (m >= 258 && n >= 10);
2031
2032
2033 UNGRAB
2034 UPDATE
2035 return Z_OK;
2036 }
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046 char *zlib_version = ZLIB_VERSION;
2047
2048 char *z_errmsg[] = {
2049 "stream end",
2050 "",
2051 "file error",
2052 "stream error",
2053 "data error",
2054 "insufficient memory",
2055 "buffer error",
2056 ""};
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066 #define BASE 65521L
2067 #define NMAX 5552
2068
2069
2070 #define DO1(buf) {s1 += *buf++; s2 += s1;}
2071 #define DO2(buf) DO1(buf); DO1(buf);
2072 #define DO4(buf) DO2(buf); DO2(buf);
2073 #define DO8(buf) DO4(buf); DO4(buf);
2074 #define DO16(buf) DO8(buf); DO8(buf);
2075
2076
2077 uLong adler32(adler, buf, len)
2078 uLong adler;
2079 Bytef *buf;
2080 uInt len;
2081 {
2082 unsigned long s1 = adler & 0xffff;
2083 unsigned long s2 = (adler >> 16) & 0xffff;
2084 int k;
2085
2086 if (buf == Z_NULL) return 1L;
2087
2088 while (len > 0) {
2089 k = len < NMAX ? len : NMAX;
2090 len -= k;
2091 while (k >= 16) {
2092 DO16(buf);
2093 k -= 16;
2094 }
2095 if (k != 0) do {
2096 DO1(buf);
2097 } while (--k);
2098 s1 %= BASE;
2099 s2 %= BASE;
2100 }
2101 return (s2 << 16) | s1;
2102 }