mirror of https://github.com/madler/zlib.git
parent
4b5a43a219
commit
f81ba93d4a
37 changed files with 1235 additions and 551 deletions
@ -0,0 +1,605 @@ |
||||
/* infback9.c -- inflate deflate64 data using a call-back interface
|
||||
* Copyright (C) 1995-2003 Mark Adler |
||||
* For conditions of distribution and use, see copyright notice in zlib.h |
||||
*/ |
||||
|
||||
#include "zutil.h" |
||||
#include "infback9.h" |
||||
#include "inftree9.h" |
||||
#include "inflate9.h" |
||||
|
||||
#define WSIZE 65536UL |
||||
|
||||
/*
|
||||
strm provides memory allocation functions in zalloc and zfree, or |
||||
Z_NULL to use the library memory allocation functions. |
||||
|
||||
window is a user-supplied window and output buffer that is 64K bytes. |
||||
*/ |
||||
int ZEXPORT inflateBack9Init_(strm, window, version, stream_size) |
||||
z_stream FAR *strm; |
||||
unsigned char FAR *window; |
||||
const char *version; |
||||
int stream_size; |
||||
{ |
||||
struct inflate_state FAR *state; |
||||
|
||||
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || |
||||
stream_size != (int)(sizeof(z_stream))) |
||||
return Z_VERSION_ERROR; |
||||
if (strm == Z_NULL || window == Z_NULL) |
||||
return Z_STREAM_ERROR; |
||||
strm->msg = Z_NULL; /* in case we return an error */ |
||||
if (strm->zalloc == (alloc_func)0) { |
||||
strm->zalloc = zcalloc; |
||||
strm->opaque = (voidpf)0; |
||||
} |
||||
if (strm->zfree == (free_func)0) strm->zfree = zcfree; |
||||
state = (struct inflate_state FAR *)ZALLOC(strm, 1, |
||||
sizeof(struct inflate_state)); |
||||
if (state == Z_NULL) return Z_MEM_ERROR; |
||||
Tracev((stderr, "inflate: allocated\n")); |
||||
strm->state = (voidpf)state; |
||||
state->window = window; |
||||
return Z_OK; |
||||
} |
||||
|
||||
/*
|
||||
Build and output length and distance decoding tables for fixed code |
||||
decoding. |
||||
*/ |
||||
#ifdef MAKEFIXED |
||||
#include <stdio.h> |
||||
|
||||
void makefixed9(void) |
||||
{ |
||||
unsigned sym, bits, low, size; |
||||
code *next, *lenfix, *distfix; |
||||
struct inflate_state state; |
||||
code fixed[544]; |
||||
|
||||
/* literal/length table */ |
||||
sym = 0; |
||||
while (sym < 144) state.lens[sym++] = 8; |
||||
while (sym < 256) state.lens[sym++] = 9; |
||||
while (sym < 280) state.lens[sym++] = 7; |
||||
while (sym < 288) state.lens[sym++] = 8; |
||||
next = fixed; |
||||
lenfix = next; |
||||
bits = 9; |
||||
inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work); |
||||
|
||||
/* distance table */ |
||||
sym = 0; |
||||
while (sym < 32) state.lens[sym++] = 5; |
||||
distfix = next; |
||||
bits = 5; |
||||
inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work); |
||||
|
||||
/* write tables */ |
||||
puts(" /* inffix9.h -- table for decoding deflate64 fixed codes"); |
||||
puts(" * Generated automatically by makefixed9()."); |
||||
puts(" */"); |
||||
puts(""); |
||||
puts(" /* WARNING: this file should *not* be used by applications."); |
||||
puts(" It is part of the implementation of this library and is"); |
||||
puts(" subject to change. Applications should only use zlib.h."); |
||||
puts(" */"); |
||||
puts(""); |
||||
size = 1U << 9; |
||||
printf(" static const code lenfix[%u] = {", size); |
||||
low = 0; |
||||
for (;;) { |
||||
if ((low % 6) == 0) printf("\n "); |
||||
printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits, |
||||
lenfix[low].val); |
||||
if (++low == size) break; |
||||
putchar(','); |
||||
} |
||||
puts("\n };"); |
||||
size = 1U << 5; |
||||
printf("\n static const code distfix[%u] = {", size); |
||||
low = 0; |
||||
for (;;) { |
||||
if ((low % 5) == 0) printf("\n "); |
||||
printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits, |
||||
distfix[low].val); |
||||
if (++low == size) break; |
||||
putchar(','); |
||||
} |
||||
puts("\n };"); |
||||
} |
||||
#endif /* MAKEFIXED */ |
||||
|
||||
/* Macros for inflateBack(): */ |
||||
|
||||
/* Clear the input bit accumulator */ |
||||
#define INITBITS() \ |
||||
do { \
|
||||
hold = 0; \
|
||||
bits = 0; \
|
||||
} while (0) |
||||
|
||||
/* Assure that some input is available. If input is requested, but denied,
|
||||
then return a Z_BUF_ERROR from inflateBack(). */ |
||||
#define PULL() \ |
||||
do { \
|
||||
if (have == 0) { \
|
||||
have = in(in_desc, &next); \
|
||||
if (have == 0) { \
|
||||
next = Z_NULL; \
|
||||
ret = Z_BUF_ERROR; \
|
||||
goto inf_leave; \
|
||||
} \
|
||||
} \
|
||||
} while (0) |
||||
|
||||
/* Get a byte of input into the bit accumulator, or return from inflateBack()
|
||||
with an error if there is no input available. */ |
||||
#define PULLBYTE() \ |
||||
do { \
|
||||
PULL(); \
|
||||
have--; \
|
||||
hold += (unsigned long)(*next++) << bits; \
|
||||
bits += 8; \
|
||||
} while (0) |
||||
|
||||
/* Assure that there are at least n bits in the bit accumulator. If there is
|
||||
not enough available input to do that, then return from inflateBack() with |
||||
an error. */ |
||||
#define NEEDBITS(n) \ |
||||
do { \
|
||||
while (bits < (unsigned)(n)) \
|
||||
PULLBYTE(); \
|
||||
} while (0) |
||||
|
||||
/* Return the low n bits of the bit accumulator (n <= 16) */ |
||||
#define BITS(n) \ |
||||
((unsigned)hold & ((1U << (n)) - 1)) |
||||
|
||||
/* Remove n bits from the bit accumulator */ |
||||
#define DROPBITS(n) \ |
||||
do { \
|
||||
hold >>= (n); \
|
||||
bits -= (unsigned)(n); \
|
||||
} while (0) |
||||
|
||||
/* Remove zero to seven bits as needed to go to a byte boundary */ |
||||
#define BYTEBITS() \ |
||||
do { \
|
||||
hold >>= bits & 7; \
|
||||
bits -= bits & 7; \
|
||||
} while (0) |
||||
|
||||
/* Assure that some output space is available, by writing out the window
|
||||
if it's full. If the write fails, return from inflateBack() with a |
||||
Z_BUF_ERROR. */ |
||||
#define ROOM() \ |
||||
do { \
|
||||
if (left == 0) { \
|
||||
put = window; \
|
||||
left = WSIZE; \
|
||||
wrap = 1; \
|
||||
if (out(out_desc, put, (unsigned)left)) { \
|
||||
ret = Z_BUF_ERROR; \
|
||||
goto inf_leave; \
|
||||
} \
|
||||
} \
|
||||
} while (0) |
||||
|
||||
/*
|
||||
strm provides the memory allocation functions and window buffer on input, |
||||
and provides information on the unused input on return. For Z_DATA_ERROR |
||||
returns, strm will also provide an error message. |
||||
|
||||
in() and out() are the call-back input and output functions. When |
||||
inflateBack() needs more input, it calls in(). When inflateBack() has |
||||
filled the window with output, or when it completes with data in the |
||||
window, it calls out() to write out the data. The application must not |
||||
change the provided input until in() is called again or inflateBack() |
||||
returns. The application must not change the window/output buffer until |
||||
inflateBack() returns. |
||||
|
||||
in() and out() are called with a descriptor parameter provided in the |
||||
inflateBack() call. This parameter can be a structure that provides the |
||||
information required to do the read or write, as well as accumulated |
||||
information on the input and output such as totals and check values. |
||||
|
||||
in() should return zero on failure. out() should return non-zero on |
||||
failure. If either in() or out() fails, than inflateBack() returns a |
||||
Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it |
||||
was in() or out() that caused in the error. Otherwise, inflateBack() |
||||
returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format |
||||
error, or Z_MEM_ERROR if it could not allocate memory for the state. |
||||
inflateBack() can also return Z_STREAM_ERROR if the input parameters |
||||
are not correct, i.e. strm is Z_NULL or the state was not initialized. |
||||
*/ |
||||
int ZEXPORT inflateBack9(strm, in, in_desc, out, out_desc) |
||||
z_stream FAR *strm; |
||||
in_func in; |
||||
void FAR *in_desc; |
||||
out_func out; |
||||
void FAR *out_desc; |
||||
{ |
||||
struct inflate_state FAR *state; |
||||
unsigned char FAR *next; /* next input */ |
||||
unsigned char FAR *put; /* next output */ |
||||
unsigned have; /* available input */ |
||||
unsigned long left; /* available output */ |
||||
inflate_mode mode; /* current inflate mode */ |
||||
int lastblock; /* true if processing last block */ |
||||
int wrap; /* true if the window has wrapped */ |
||||
unsigned long write; /* window write index */ |
||||
unsigned char FAR *window; /* allocated sliding window, if needed */ |
||||
unsigned long hold; /* bit buffer */ |
||||
unsigned bits; /* bits in bit buffer */ |
||||
unsigned extra; /* extra bits needed */ |
||||
unsigned long length; /* literal or length of data to copy */ |
||||
unsigned long offset; /* distance back to copy string from */ |
||||
unsigned long copy; /* number of stored or match bytes to copy */ |
||||
unsigned char FAR *from; /* where to copy match bytes from */ |
||||
code const FAR *lencode; /* starting table for length/literal codes */ |
||||
code const FAR *distcode; /* starting table for distance codes */ |
||||
unsigned lenbits; /* index bits for lencode */ |
||||
unsigned distbits; /* index bits for distcode */ |
||||
code this; /* current decoding table entry */ |
||||
code last; /* parent table entry */ |
||||
unsigned len; /* length to copy for repeats, bits to drop */ |
||||
int ret; /* return code */ |
||||
static const unsigned short order[19] = /* permutation of code lengths */ |
||||
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
||||
#include "inffix9.h" |
||||
|
||||
/* Check that the strm exists and that the state was initialized */ |
||||
if (strm == Z_NULL || strm->state == Z_NULL) |
||||
return Z_STREAM_ERROR; |
||||
state = (struct inflate_state FAR *)strm->state; |
||||
|
||||
/* Reset the state */ |
||||
strm->msg = Z_NULL; |
||||
mode = TYPE; |
||||
lastblock = 0; |
||||
write = 0; |
||||
wrap = 0; |
||||
window = state->window; |
||||
next = strm->next_in; |
||||
have = next != Z_NULL ? strm->avail_in : 0; |
||||
hold = 0; |
||||
bits = 0; |
||||
put = window; |
||||
left = WSIZE; |
||||
lencode = Z_NULL; |
||||
distcode = Z_NULL; |
||||
|
||||
/* Inflate until end of block marked as last */ |
||||
for (;;) |
||||
switch (mode) { |
||||
case TYPE: |
||||
/* determine and dispatch block type */ |
||||
if (lastblock) { |
||||
BYTEBITS(); |
||||
mode = DONE; |
||||
break; |
||||
} |
||||
NEEDBITS(3); |
||||
lastblock = BITS(1); |
||||
DROPBITS(1); |
||||
switch (BITS(2)) { |
||||
case 0: /* stored block */ |
||||
Tracev((stderr, "inflate: stored block%s\n", |
||||
lastblock ? " (last)" : "")); |
||||
mode = STORED; |
||||
break; |
||||
case 1: /* fixed block */ |
||||
lencode = lenfix; |
||||
lenbits = 9; |
||||
distcode = distfix; |
||||
distbits = 5; |
||||
Tracev((stderr, "inflate: fixed codes block%s\n", |
||||
lastblock ? " (last)" : "")); |
||||
mode = LEN; /* decode codes */ |
||||
break; |
||||
case 2: /* dynamic block */ |
||||
Tracev((stderr, "inflate: dynamic codes block%s\n", |
||||
lastblock ? " (last)" : "")); |
||||
mode = TABLE; |
||||
break; |
||||
case 3: |
||||
strm->msg = (char *)"invalid block type"; |
||||
mode = BAD; |
||||
} |
||||
DROPBITS(2); |
||||
break; |
||||
|
||||
case STORED: |
||||
/* get and verify stored block length */ |
||||
BYTEBITS(); /* go to byte boundary */ |
||||
NEEDBITS(32); |
||||
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { |
||||
strm->msg = (char *)"invalid stored block lengths"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
length = (unsigned)hold & 0xffff; |
||||
Tracev((stderr, "inflate: stored length %u\n", |
||||
length)); |
||||
INITBITS(); |
||||
|
||||
/* copy stored block from input to output */ |
||||
while (length != 0) { |
||||
copy = length; |
||||
PULL(); |
||||
ROOM(); |
||||
if (copy > have) copy = have; |
||||
if (copy > left) copy = left; |
||||
zmemcpy(put, next, copy); |
||||
have -= copy; |
||||
next += copy; |
||||
left -= copy; |
||||
put += copy; |
||||
length -= copy; |
||||
} |
||||
Tracev((stderr, "inflate: stored end\n")); |
||||
mode = TYPE; |
||||
break; |
||||
|
||||
case TABLE: |
||||
/* get dynamic table entries descriptor */ |
||||
NEEDBITS(14); |
||||
state->nlen = BITS(5) + 257; |
||||
DROPBITS(5); |
||||
state->ndist = BITS(5) + 1; |
||||
DROPBITS(5); |
||||
state->ncode = BITS(4) + 4; |
||||
DROPBITS(4); |
||||
if (state->nlen > 286) { |
||||
strm->msg = (char *)"too many length symbols"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
Tracev((stderr, "inflate: table sizes ok\n")); |
||||
|
||||
/* get code length code lengths (not a typo) */ |
||||
state->have = 0; |
||||
while (state->have < state->ncode) { |
||||
NEEDBITS(3); |
||||
state->lens[order[state->have++]] = (unsigned short)BITS(3); |
||||
DROPBITS(3); |
||||
} |
||||
while (state->have < 19) |
||||
state->lens[order[state->have++]] = 0; |
||||
state->next = state->codes; |
||||
lencode = (code const FAR *)(state->next); |
||||
lenbits = 7; |
||||
ret = inflate_table9(CODES, state->lens, 19, &(state->next), |
||||
&(lenbits), state->work); |
||||
if (ret) { |
||||
strm->msg = (char *)"invalid code lengths set"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
Tracev((stderr, "inflate: code lengths ok\n")); |
||||
|
||||
/* get length and distance code code lengths */ |
||||
state->have = 0; |
||||
while (state->have < state->nlen + state->ndist) { |
||||
for (;;) { |
||||
this = lencode[BITS(lenbits)]; |
||||
if ((unsigned)(this.bits) <= bits) break; |
||||
PULLBYTE(); |
||||
} |
||||
if (this.val < 16) { |
||||
NEEDBITS(this.bits); |
||||
DROPBITS(this.bits); |
||||
state->lens[state->have++] = this.val; |
||||
} |
||||
else { |
||||
if (this.val == 16) { |
||||
NEEDBITS(this.bits + 2); |
||||
DROPBITS(this.bits); |
||||
if (state->have == 0) { |
||||
strm->msg = (char *)"invalid bit length repeat"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
len = (unsigned)(state->lens[state->have - 1]); |
||||
copy = 3 + BITS(2); |
||||
DROPBITS(2); |
||||
} |
||||
else if (this.val == 17) { |
||||
NEEDBITS(this.bits + 3); |
||||
DROPBITS(this.bits); |
||||
len = 0; |
||||
copy = 3 + BITS(3); |
||||
DROPBITS(3); |
||||
} |
||||
else { |
||||
NEEDBITS(this.bits + 7); |
||||
DROPBITS(this.bits); |
||||
len = 0; |
||||
copy = 11 + BITS(7); |
||||
DROPBITS(7); |
||||
} |
||||
if (state->have + copy > state->nlen + state->ndist) { |
||||
strm->msg = (char *)"invalid bit length repeat"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
while (copy--) |
||||
state->lens[state->have++] = (unsigned short)len; |
||||
} |
||||
} |
||||
|
||||
/* build code tables */ |
||||
state->next = state->codes; |
||||
lencode = (code const FAR *)(state->next); |
||||
lenbits = 9; |
||||
ret = inflate_table9(LENS, state->lens, state->nlen, |
||||
&(state->next), &(lenbits), state->work); |
||||
if (ret) { |
||||
strm->msg = (char *)"invalid literal/lengths set"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
distcode = (code const FAR *)(state->next); |
||||
distbits = 6; |
||||
ret = inflate_table9(DISTS, state->lens + state->nlen, |
||||
state->ndist, &(state->next), &(distbits), |
||||
state->work); |
||||
if (ret) { |
||||
strm->msg = (char *)"invalid distances set"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
Tracev((stderr, "inflate: codes ok\n")); |
||||
mode = LEN; |
||||
|
||||
case LEN: |
||||
/* get a literal, length, or end-of-block code */ |
||||
for (;;) { |
||||
this = lencode[BITS(lenbits)]; |
||||
if ((unsigned)(this.bits) <= bits) break; |
||||
PULLBYTE(); |
||||
} |
||||
if (this.op && (this.op & 0xf0) == 0) { |
||||
last = this; |
||||
for (;;) { |
||||
this = lencode[last.val + |
||||
(BITS(last.bits + last.op) >> last.bits)]; |
||||
if ((unsigned)(last.bits + this.bits) <= bits) break; |
||||
PULLBYTE(); |
||||
} |
||||
DROPBITS(last.bits); |
||||
} |
||||
DROPBITS(this.bits); |
||||
length = (unsigned)this.val; |
||||
|
||||
/* process literal */ |
||||
if (this.op == 0) { |
||||
Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? |
||||
"inflate: literal '%c'\n" : |
||||
"inflate: literal 0x%02x\n", this.val)); |
||||
ROOM(); |
||||
*put++ = (unsigned char)(length); |
||||
left--; |
||||
mode = LEN; |
||||
break; |
||||
} |
||||
|
||||
/* process end of block */ |
||||
if (this.op & 32) { |
||||
Tracevv((stderr, "inflate: end of block\n")); |
||||
mode = TYPE; |
||||
break; |
||||
} |
||||
|
||||
/* invalid code */ |
||||
if (this.op & 64) { |
||||
strm->msg = (char *)"invalid literal/length code"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
|
||||
/* length code -- get extra bits, if any */ |
||||
extra = (unsigned)(this.op) & 31; |
||||
if (extra != 0) { |
||||
NEEDBITS(extra); |
||||
length += BITS(extra); |
||||
DROPBITS(extra); |
||||
} |
||||
Tracevv((stderr, "inflate: length %u\n", length)); |
||||
|
||||
/* get distance code */ |
||||
for (;;) { |
||||
this = distcode[BITS(distbits)]; |
||||
if ((unsigned)(this.bits) <= bits) break; |
||||
PULLBYTE(); |
||||
} |
||||
if ((this.op & 0xf0) == 0) { |
||||
last = this; |
||||
for (;;) { |
||||
this = distcode[last.val + |
||||
(BITS(last.bits + last.op) >> last.bits)]; |
||||
if ((unsigned)(last.bits + this.bits) <= bits) break; |
||||
PULLBYTE(); |
||||
} |
||||
DROPBITS(last.bits); |
||||
} |
||||
DROPBITS(this.bits); |
||||
if (this.op & 64) { |
||||
strm->msg = (char *)"invalid distance code"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
offset = (unsigned)this.val; |
||||
|
||||
/* get distance extra bits, if any */ |
||||
extra = (unsigned)(this.op) & 15; |
||||
if (extra != 0) { |
||||
NEEDBITS(extra); |
||||
offset += BITS(extra); |
||||
DROPBITS(extra); |
||||
} |
||||
if (offset > WSIZE - (wrap ? 0: left)) { |
||||
strm->msg = (char *)"invalid distance too far back"; |
||||
mode = BAD; |
||||
break; |
||||
} |
||||
Tracevv((stderr, "inflate: distance %u\n", offset)); |
||||
|
||||
/* copy match from window to output */ |
||||
do { |
||||
ROOM(); |
||||
copy = WSIZE - offset; |
||||
if (copy < left) { |
||||
from = put + copy; |
||||
copy = left - copy; |
||||
} |
||||
else { |
||||
from = put - offset; |
||||
copy = left; |
||||
} |
||||
if (copy > length) copy = length; |
||||
length -= copy; |
||||
left -= copy; |
||||
do { |
||||
*put++ = *from++; |
||||
} while (--copy); |
||||
} while (length != 0); |
||||
break; |
||||
|
||||
case DONE: |
||||
/* inflate stream terminated properly -- write leftover output */ |
||||
ret = Z_STREAM_END; |
||||
if (left < WSIZE) { |
||||
if (out(out_desc, window, (unsigned)(WSIZE - left))) |
||||
ret = Z_BUF_ERROR; |
||||
} |
||||
goto inf_leave; |
||||
|
||||
case BAD: |
||||
ret = Z_DATA_ERROR; |
||||
goto inf_leave; |
||||
|
||||
default: /* can't happen, but makes compilers happy */ |
||||
ret = Z_STREAM_ERROR; |
||||
goto inf_leave; |
||||
} |
||||
|
||||
/* Return unused input */ |
||||
inf_leave: |
||||
strm->next_in = next; |
||||
strm->avail_in = have; |
||||
return ret; |
||||
} |
||||
|
||||
int ZEXPORT inflateBack9End(strm) |
||||
z_stream FAR *strm; |
||||
{ |
||||
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) |
||||
return Z_STREAM_ERROR; |
||||
ZFREE(strm, strm->state); |
||||
strm->state = Z_NULL; |
||||
Tracev((stderr, "inflate: end\n")); |
||||
return Z_OK; |
||||
} |
@ -1,427 +0,0 @@ |
||||
*** infback.c Mon Aug 11 16:48:06 2003
|
||||
--- infback9.c Mon Sep 8 21:22:46 2003
|
||||
***************
|
||||
*** 1,19 ****
|
||||
! /* infback.c -- inflate using a call-back interface
|
||||
* Copyright (C) 1995-2003 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
|
||||
- /*
|
||||
- This code is largely copied from inflate.c. Normally either infback.o or
|
||||
- inflate.o would be linked into an application--not both. The interface
|
||||
- with inffast.c is retained so that optimized assembler-coded versions of
|
||||
- inflate_fast() can be used with either inflate.c or infback.c.
|
||||
- */
|
||||
-
|
||||
#include "zutil.h"
|
||||
! #include "inftrees.h"
|
||||
#include "inflate.h"
|
||||
- #include "inffast.h"
|
||||
|
||||
/* function prototypes */
|
||||
local void fixedtables OF((struct inflate_state FAR *state));
|
||||
--- 1,12 ----
|
||||
! /* infback9.c -- inflate deflate64 data using a call-back interface
|
||||
* Copyright (C) 1995-2003 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
|
||||
#include "zutil.h"
|
||||
! #include "infback9.h"
|
||||
! #include "inftree9.h"
|
||||
#include "inflate.h"
|
||||
|
||||
/* function prototypes */
|
||||
local void fixedtables OF((struct inflate_state FAR *state));
|
||||
***************
|
||||
*** 22,33 ****
|
||||
strm provides memory allocation functions in zalloc and zfree, or
|
||||
Z_NULL to use the library memory allocation functions.
|
||||
|
||||
! windowBits is in the range 8..15, and window is a user-supplied
|
||||
! window and output buffer that is 2**windowBits bytes.
|
||||
*/
|
||||
! int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
|
||||
z_stream FAR *strm;
|
||||
- int windowBits;
|
||||
unsigned char FAR *window;
|
||||
const char *version;
|
||||
int stream_size;
|
||||
--- 15,24 ----
|
||||
strm provides memory allocation functions in zalloc and zfree, or
|
||||
Z_NULL to use the library memory allocation functions.
|
||||
|
||||
! window is a user-supplied window and output buffer that is 64K bytes.
|
||||
*/
|
||||
! int ZEXPORT inflateBack9Init_(strm, window, version, stream_size)
|
||||
z_stream FAR *strm;
|
||||
unsigned char FAR *window;
|
||||
const char *version;
|
||||
int stream_size;
|
||||
***************
|
||||
*** 37,44 ****
|
||||
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
|
||||
stream_size != (int)(sizeof(z_stream)))
|
||||
return Z_VERSION_ERROR;
|
||||
! if (strm == Z_NULL || window == Z_NULL ||
|
||||
! windowBits < 8 || windowBits > 15)
|
||||
return Z_STREAM_ERROR;
|
||||
strm->msg = Z_NULL; /* in case we return an error */
|
||||
if (strm->zalloc == (alloc_func)0) {
|
||||
--- 28,34 ----
|
||||
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
|
||||
stream_size != (int)(sizeof(z_stream)))
|
||||
return Z_VERSION_ERROR;
|
||||
! if (strm == Z_NULL || window == Z_NULL)
|
||||
return Z_STREAM_ERROR;
|
||||
strm->msg = Z_NULL; /* in case we return an error */
|
||||
if (strm->zalloc == (alloc_func)0) {
|
||||
***************
|
||||
*** 51,58 ****
|
||||
if (state == Z_NULL) return Z_MEM_ERROR;
|
||||
Tracev((stderr, "inflate: allocated\n"));
|
||||
strm->state = (voidpf)state;
|
||||
! state->wbits = windowBits;
|
||||
! state->wsize = 1U << windowBits;
|
||||
state->window = window;
|
||||
state->write = 0;
|
||||
state->whave = 0;
|
||||
--- 41,48 ----
|
||||
if (state == Z_NULL) return Z_MEM_ERROR;
|
||||
Tracev((stderr, "inflate: allocated\n"));
|
||||
strm->state = (voidpf)state;
|
||||
! state->wbits = 16;
|
||||
! state->wsize = 1U << 16;
|
||||
state->window = window;
|
||||
state->write = 0;
|
||||
state->whave = 0;
|
||||
***************
|
||||
*** 91,110 ****
|
||||
next = fixed;
|
||||
lenfix = next;
|
||||
bits = 9;
|
||||
! inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
|
||||
|
||||
/* distance table */
|
||||
sym = 0;
|
||||
while (sym < 32) state->lens[sym++] = 5;
|
||||
distfix = next;
|
||||
bits = 5;
|
||||
! inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
|
||||
|
||||
/* do this just once */
|
||||
virgin = 0;
|
||||
}
|
||||
#else /* !BUILDFIXED */
|
||||
! # include "inffixed.h"
|
||||
#endif /* BUILDFIXED */
|
||||
state->lencode = lenfix;
|
||||
state->lenbits = 9;
|
||||
--- 81,100 ----
|
||||
next = fixed;
|
||||
lenfix = next;
|
||||
bits = 9;
|
||||
! inflate_table9(LENS, state->lens, 288, &(next), &(bits), state->work);
|
||||
|
||||
/* distance table */
|
||||
sym = 0;
|
||||
while (sym < 32) state->lens[sym++] = 5;
|
||||
distfix = next;
|
||||
bits = 5;
|
||||
! inflate_table9(DISTS, state->lens, 32, &(next), &(bits), state->work);
|
||||
|
||||
/* do this just once */
|
||||
virgin = 0;
|
||||
}
|
||||
#else /* !BUILDFIXED */
|
||||
! # include "inffix9.h"
|
||||
#endif /* BUILDFIXED */
|
||||
state->lencode = lenfix;
|
||||
state->lenbits = 9;
|
||||
***************
|
||||
*** 114,141 ****
|
||||
|
||||
/* Macros for inflateBack(): */
|
||||
|
||||
- /* Load returned state from inflate_fast() */
|
||||
- #define LOAD() \
|
||||
- do { \
|
||||
- put = strm->next_out; \
|
||||
- left = strm->avail_out; \
|
||||
- next = strm->next_in; \
|
||||
- have = strm->avail_in; \
|
||||
- hold = state->hold; \
|
||||
- bits = state->bits; \
|
||||
- } while (0)
|
||||
-
|
||||
- /* Set state from registers for inflate_fast() */
|
||||
- #define RESTORE() \
|
||||
- do { \
|
||||
- strm->next_out = put; \
|
||||
- strm->avail_out = left; \
|
||||
- strm->next_in = next; \
|
||||
- strm->avail_in = have; \
|
||||
- state->hold = hold; \
|
||||
- state->bits = bits; \
|
||||
- } while (0)
|
||||
-
|
||||
/* Clear the input bit accumulator */
|
||||
#define INITBITS() \
|
||||
do { \
|
||||
--- 104,109 ----
|
||||
***************
|
||||
*** 237,243 ****
|
||||
inflateBack() can also return Z_STREAM_ERROR if the input parameters
|
||||
are not correct, i.e. strm is Z_NULL or the state was not initialized.
|
||||
*/
|
||||
! int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
|
||||
z_stream FAR *strm;
|
||||
in_func in;
|
||||
void FAR *in_desc;
|
||||
--- 205,211 ----
|
||||
inflateBack() can also return Z_STREAM_ERROR if the input parameters
|
||||
are not correct, i.e. strm is Z_NULL or the state was not initialized.
|
||||
*/
|
||||
! int ZEXPORT inflateBack9(strm, in, in_desc, out, out_desc)
|
||||
z_stream FAR *strm;
|
||||
in_func in;
|
||||
void FAR *in_desc;
|
||||
***************
|
||||
*** 354,366 ****
|
||||
DROPBITS(5);
|
||||
state->ncode = BITS(4) + 4;
|
||||
DROPBITS(4);
|
||||
! #ifndef PKZIP_BUG_WORKAROUND
|
||||
! if (state->nlen > 286 || state->ndist > 30) {
|
||||
! strm->msg = (char *)"too many length or distance symbols";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
- #endif
|
||||
Tracev((stderr, "inflate: table sizes ok\n"));
|
||||
|
||||
/* get code length code lengths (not a typo) */
|
||||
--- 322,332 ----
|
||||
DROPBITS(5);
|
||||
state->ncode = BITS(4) + 4;
|
||||
DROPBITS(4);
|
||||
! if (state->nlen > 286) {
|
||||
! strm->msg = (char *)"too many length symbols";
|
||||
state->mode = BAD;
|
||||
break;
|
||||
}
|
||||
Tracev((stderr, "inflate: table sizes ok\n"));
|
||||
|
||||
/* get code length code lengths (not a typo) */
|
||||
***************
|
||||
*** 375,381 ****
|
||||
state->next = state->codes;
|
||||
state->lencode = (code const FAR *)(state->next);
|
||||
state->lenbits = 7;
|
||||
! ret = inflate_table(CODES, state->lens, 19, &(state->next),
|
||||
&(state->lenbits), state->work);
|
||||
if (ret) {
|
||||
strm->msg = (char *)"invalid code lengths set";
|
||||
--- 341,347 ----
|
||||
state->next = state->codes;
|
||||
state->lencode = (code const FAR *)(state->next);
|
||||
state->lenbits = 7;
|
||||
! ret = inflate_table9(CODES, state->lens, 19, &(state->next),
|
||||
&(state->lenbits), state->work);
|
||||
if (ret) {
|
||||
strm->msg = (char *)"invalid code lengths set";
|
||||
***************
|
||||
*** 438,445 ****
|
||||
state->next = state->codes;
|
||||
state->lencode = (code const FAR *)(state->next);
|
||||
state->lenbits = 9;
|
||||
! ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
|
||||
! &(state->lenbits), state->work);
|
||||
if (ret) {
|
||||
strm->msg = (char *)"invalid literal/lengths set";
|
||||
state->mode = BAD;
|
||||
--- 404,411 ----
|
||||
state->next = state->codes;
|
||||
state->lencode = (code const FAR *)(state->next);
|
||||
state->lenbits = 9;
|
||||
! ret = inflate_table9(LENS, state->lens, state->nlen,
|
||||
! &(state->next), &(state->lenbits), state->work);
|
||||
if (ret) {
|
||||
strm->msg = (char *)"invalid literal/lengths set";
|
||||
state->mode = BAD;
|
||||
***************
|
||||
*** 447,454 ****
|
||||
}
|
||||
state->distcode = (code const FAR *)(state->next);
|
||||
state->distbits = 6;
|
||||
! ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
|
||||
! &(state->next), &(state->distbits), state->work);
|
||||
if (ret) {
|
||||
strm->msg = (char *)"invalid distances set";
|
||||
state->mode = BAD;
|
||||
--- 413,421 ----
|
||||
}
|
||||
state->distcode = (code const FAR *)(state->next);
|
||||
state->distbits = 6;
|
||||
! ret = inflate_table9(DISTS, state->lens + state->nlen,
|
||||
! state->ndist, &(state->next), &(state->distbits),
|
||||
! state->work);
|
||||
if (ret) {
|
||||
strm->msg = (char *)"invalid distances set";
|
||||
state->mode = BAD;
|
||||
***************
|
||||
*** 458,473 ****
|
||||
state->mode = LEN;
|
||||
|
||||
case LEN:
|
||||
- /* use inflate_fast() if we have enough input and output */
|
||||
- if (have >= 6 && left >= 258) {
|
||||
- RESTORE();
|
||||
- if (state->whave < state->wsize)
|
||||
- state->whave = state->wsize - left;
|
||||
- inflate_fast(strm, state->wsize);
|
||||
- LOAD();
|
||||
- break;
|
||||
- }
|
||||
-
|
||||
/* get a literal, length, or end-of-block code */
|
||||
for (;;) {
|
||||
this = state->lencode[BITS(state->lenbits)];
|
||||
--- 425,430 ----
|
||||
***************
|
||||
*** 607,613 ****
|
||||
return ret;
|
||||
}
|
||||
|
||||
! int ZEXPORT inflateBackEnd(strm)
|
||||
z_stream FAR *strm;
|
||||
{
|
||||
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
|
||||
--- 564,570 ----
|
||||
return ret;
|
||||
}
|
||||
|
||||
! int ZEXPORT inflateBack9End(strm)
|
||||
z_stream FAR *strm;
|
||||
{
|
||||
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
|
||||
*** inftrees.c Sun Sep 7 10:59:10 2003
|
||||
--- inftree9.c Mon Sep 8 20:54:36 2003
|
||||
***************
|
||||
*** 1,15 ****
|
||||
! /* inftrees.c -- generate Huffman trees for efficient decoding
|
||||
* Copyright (C) 1995-2003 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
|
||||
#include "zutil.h"
|
||||
! #include "inftrees.h"
|
||||
|
||||
#define MAXBITS 15
|
||||
|
||||
! const char inflate_copyright[] =
|
||||
! " inflate 1.2.0.5 Copyright 1995-2003 Mark Adler ";
|
||||
/*
|
||||
If you use the zlib library in a product, an acknowledgment is welcome
|
||||
in the documentation of your product. If for some reason you cannot
|
||||
--- 1,15 ----
|
||||
! /* inftree9.c -- generate Huffman trees for efficient decoding
|
||||
* Copyright (C) 1995-2003 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
|
||||
#include "zutil.h"
|
||||
! #include "inftree9.h"
|
||||
|
||||
#define MAXBITS 15
|
||||
|
||||
! const char inflate9_copyright[] =
|
||||
! " inflate9 1.2.0.5 Copyright 1995-2003 Mark Adler ";
|
||||
/*
|
||||
If you use the zlib library in a product, an acknowledgment is welcome
|
||||
in the documentation of your product. If for some reason you cannot
|
||||
***************
|
||||
*** 29,35 ****
|
||||
table index bits. It will differ if the request is greater than the
|
||||
longest code or if it is less than the shortest code.
|
||||
*/
|
||||
! int inflate_table(type, lens, codes, table, bits, work)
|
||||
codetype type;
|
||||
unsigned short FAR *lens;
|
||||
unsigned codes;
|
||||
--- 29,35 ----
|
||||
table index bits. It will differ if the request is greater than the
|
||||
longest code or if it is less than the shortest code.
|
||||
*/
|
||||
! int inflate_table9(type, lens, codes, table, bits, work)
|
||||
codetype type;
|
||||
unsigned short FAR *lens;
|
||||
unsigned codes;
|
||||
***************
|
||||
*** 59,76 ****
|
||||
unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
|
||||
static const unsigned short lbase[31] = { /* Length codes 257..285 base */
|
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
||||
! 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
|
||||
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
|
||||
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
|
||||
! 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 192, 78};
|
||||
! static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
|
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
||||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
||||
! 8193, 12289, 16385, 24577, 0, 0};
|
||||
! static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
|
||||
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
|
||||
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
|
||||
! 28, 28, 29, 29, 64, 64};
|
||||
|
||||
/*
|
||||
Process a set of code lengths to create a canonical Huffman code. The
|
||||
--- 59,76 ----
|
||||
unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
|
||||
static const unsigned short lbase[31] = { /* Length codes 257..285 base */
|
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
||||
! 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 3, 0, 0};
|
||||
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
|
||||
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
|
||||
! 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 32, 192, 78};
|
||||
! static const unsigned short dbase[32] = { /* Distance codes 0..31 base */
|
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
||||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
||||
! 8193, 12289, 16385, 24577, 32769, 49153};
|
||||
! static const unsigned short dext[32] = { /* Distance codes 0..31 extra */
|
||||
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
|
||||
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
|
||||
! 28, 28, 29, 29, 30, 30};
|
||||
|
||||
/*
|
||||
Process a set of code lengths to create a canonical Huffman code. The
|
||||
*** inftrees.h Sun Aug 10 15:15:50 2003
|
||||
--- inftree9.h Mon Sep 8 20:54:51 2003
|
||||
***************
|
||||
*** 1,4 ****
|
||||
! /* inftrees.h -- header to use inftrees.c
|
||||
* Copyright (C) 1995-2003 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
--- 1,4 ----
|
||||
! /* inftree9.h -- header to use inftree9.c
|
||||
* Copyright (C) 1995-2003 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
***************
|
||||
*** 50,55 ****
|
||||
DISTS
|
||||
} codetype;
|
||||
|
||||
! extern int inflate_table OF((codetype type, unsigned short FAR *lens,
|
||||
unsigned codes, code FAR * FAR *table,
|
||||
unsigned FAR *bits, unsigned short FAR *work));
|
||||
--- 50,55 ----
|
||||
DISTS
|
||||
} codetype;
|
||||
|
||||
! extern int inflate_table9 OF((codetype type, unsigned short FAR *lens,
|
||||
unsigned codes, code FAR * FAR *table,
|
||||
unsigned FAR *bits, unsigned short FAR *work));
|
@ -0,0 +1,47 @@ |
||||
/* inflate9.h -- internal inflate state definition
|
||||
* Copyright (C) 1995-2003 Mark Adler |
||||
* For conditions of distribution and use, see copyright notice in zlib.h |
||||
*/ |
||||
|
||||
/* WARNING: this file should *not* be used by applications. It is
|
||||
part of the implementation of the compression library and is |
||||
subject to change. Applications should only use zlib.h. |
||||
*/ |
||||
|
||||
/* Possible inflate modes between inflate() calls */ |
||||
typedef enum { |
||||
TYPE, /* i: waiting for type bits, including last-flag bit */ |
||||
STORED, /* i: waiting for stored size (length and complement) */ |
||||
TABLE, /* i: waiting for dynamic block table lengths */ |
||||
LEN, /* i: waiting for length/lit code */ |
||||
DONE, /* finished check, done -- remain here until reset */ |
||||
BAD /* got a data error -- remain here until reset */ |
||||
} inflate_mode; |
||||
|
||||
/*
|
||||
State transitions between above modes - |
||||
|
||||
(most modes can go to the BAD mode -- not shown for clarity) |
||||
|
||||
Read deflate blocks: |
||||
TYPE -> STORED or TABLE or LEN or DONE |
||||
STORED -> TYPE |
||||
TABLE -> LENLENS -> CODELENS -> LEN |
||||
Read deflate codes: |
||||
LEN -> LEN or TYPE |
||||
*/ |
||||
|
||||
/* state maintained between inflate() calls. Approximately 7K bytes. */ |
||||
struct inflate_state { |
||||
/* sliding window */ |
||||
unsigned char FAR *window; /* allocated sliding window, if needed */ |
||||
/* dynamic table building */ |
||||
unsigned ncode; /* number of code length code lengths */ |
||||
unsigned nlen; /* number of length code lengths */ |
||||
unsigned ndist; /* number of distance code lengths */ |
||||
unsigned have; /* number of code lengths in lens[] */ |
||||
code FAR *next; /* next available space in codes[] */ |
||||
unsigned short lens[320]; /* temporary storage for code lengths */ |
||||
unsigned short work[288]; /* work area for code table building */ |
||||
code codes[ENOUGH]; /* space for code tables */ |
||||
}; |
@ -0,0 +1,323 @@ |
||||
/* inftree9.c -- generate Huffman trees for efficient decoding
|
||||
* Copyright (C) 1995-2003 Mark Adler |
||||
* For conditions of distribution and use, see copyright notice in zlib.h |
||||
*/ |
||||
|
||||
#include "zutil.h" |
||||
#include "inftree9.h" |
||||
|
||||
#define MAXBITS 15 |
||||
|
||||
const char inflate9_copyright[] = |
||||
" inflate9 1.2.0.6 Copyright 1995-2003 Mark Adler "; |
||||
/*
|
||||
If you use the zlib library in a product, an acknowledgment is welcome |
||||
in the documentation of your product. If for some reason you cannot |
||||
include such an acknowledgment, I would appreciate that you keep this |
||||
copyright string in the executable of your product. |
||||
*/ |
||||
|
||||
/*
|
||||
Build a set of tables to decode the provided canonical Huffman code. |
||||
The code lengths are lens[0..codes-1]. The result starts at *table, |
||||
whose indices are 0..2^bits-1. work is a writable array of at least |
||||
lens shorts, which is used as a work area. type is the type of code |
||||
to be generated, CODES, LENS, or DISTS. On return, zero is success, |
||||
-1 is an invalid code, and +1 means that ENOUGH isn't enough. table |
||||
on return points to the next available entry's address. bits is the |
||||
requested root table index bits, and on return it is the actual root |
||||
table index bits. It will differ if the request is greater than the |
||||
longest code or if it is less than the shortest code. |
||||
*/ |
||||
int inflate_table9(type, lens, codes, table, bits, work) |
||||
codetype type; |
||||
unsigned short FAR *lens; |
||||
unsigned codes; |
||||
code FAR * FAR *table; |
||||
unsigned FAR *bits; |
||||
unsigned short FAR *work; |
||||
{ |
||||
unsigned len; /* a code's length in bits */ |
||||
unsigned sym; /* index of code symbols */ |
||||
unsigned min, max; /* minimum and maximum code lengths */ |
||||
unsigned root; /* number of index bits for root table */ |
||||
unsigned curr; /* number of index bits for current table */ |
||||
unsigned drop; /* code bits to drop for sub-table */ |
||||
int left; /* number of prefix codes available */ |
||||
unsigned used; /* code entries in table used */ |
||||
unsigned huff; /* Huffman code */ |
||||
unsigned incr; /* for incrementing code, index */ |
||||
unsigned fill; /* index for replicating entries */ |
||||
unsigned low; /* low bits for current root entry */ |
||||
unsigned mask; /* mask for low root bits */ |
||||
code this; /* table entry for duplication */ |
||||
code FAR *next; /* next available space in table */ |
||||
const unsigned short FAR *base; /* base value table to use */ |
||||
const unsigned short FAR *extra; /* extra bits table to use */ |
||||
int end; /* use base and extra for symbol > end */ |
||||
unsigned short count[MAXBITS+1]; /* number of codes of each length */ |
||||
unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ |
||||
static const unsigned short lbase[31] = { /* Length codes 257..285 base */ |
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, |
||||
19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, |
||||
131, 163, 195, 227, 3, 0, 0}; |
||||
static const unsigned short lext[31] = { /* Length codes 257..285 extra */ |
||||
128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, |
||||
130, 130, 130, 130, 131, 131, 131, 131, 132, 132, 132, 132, |
||||
133, 133, 133, 133, 144, 65, 77}; |
||||
static const unsigned short dbase[32] = { /* Distance codes 0..31 base */ |
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, |
||||
65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, |
||||
4097, 6145, 8193, 12289, 16385, 24577, 32769, 49153}; |
||||
static const unsigned short dext[32] = { /* Distance codes 0..31 extra */ |
||||
128, 128, 128, 128, 129, 129, 130, 130, 131, 131, 132, 132, |
||||
133, 133, 134, 134, 135, 135, 136, 136, 137, 137, 138, 138, |
||||
139, 139, 140, 140, 141, 141, 142, 142}; |
||||
|
||||
/*
|
||||
Process a set of code lengths to create a canonical Huffman code. The |
||||
code lengths are lens[0..codes-1]. Each length corresponds to the |
||||
symbols 0..codes-1. The Huffman code is generated by first sorting the |
||||
symbols by length from short to long, and retaining the symbol order |
||||
for codes with equal lengths. Then the code starts with all zero bits |
||||
for the first code of the shortest length, and the codes are integer |
||||
increments for the same length, and zeros are appended as the length |
||||
increases. For the deflate format, these bits are stored backwards |
||||
from their more natural integer increment ordering, and so when the |
||||
decoding tables are built in the large loop below, the integer codes |
||||
are incremented backwards. |
||||
|
||||
This routine assumes, but does not check, that all of the entries in |
||||
lens[] are in the range 0..MAXBITS. The caller must assure this. |
||||
1..MAXBITS is interpreted as that code length. zero means that that |
||||
symbol does not occur in this code. |
||||
|
||||
The codes are sorted by computing a count of codes for each length, |
||||
creating from that a table of starting indices for each length in the |
||||
sorted table, and then entering the symbols in order in the sorted |
||||
table. The sorted table is work[], with that space being provided by |
||||
the caller. |
||||
|
||||
The length counts are used for other purposes as well, i.e. finding |
||||
the minimum and maximum length codes, determining if there are any |
||||
codes at all, checking for a valid set of lengths, and looking ahead |
||||
at length counts to determine sub-table sizes when building the |
||||
decoding tables. |
||||
*/ |
||||
|
||||
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ |
||||
for (len = 0; len <= MAXBITS; len++) |
||||
count[len] = 0; |
||||
for (sym = 0; sym < codes; sym++) |
||||
count[lens[sym]]++; |
||||
|
||||
/* bound code lengths, force root to be within code lengths */ |
||||
root = *bits; |
||||
for (max = MAXBITS; max >= 1; max--) |
||||
if (count[max] != 0) break; |
||||
if (root > max) root = max; |
||||
if (max == 0) return -1; /* no codes! */ |
||||
for (min = 1; min <= MAXBITS; min++) |
||||
if (count[min] != 0) break; |
||||
if (root < min) root = min; |
||||
|
||||
/* check for an over-subscribed or incomplete set of lengths */ |
||||
left = 1; |
||||
for (len = 1; len <= MAXBITS; len++) { |
||||
left <<= 1; |
||||
left -= count[len]; |
||||
if (left < 0) return -1; /* over-subscribed */ |
||||
} |
||||
if (left > 0 && (type == CODES || (codes - count[0] != 1))) |
||||
return -1; /* incomplete set */ |
||||
|
||||
/* generate offsets into symbol table for each length for sorting */ |
||||
offs[1] = 0; |
||||
for (len = 1; len < MAXBITS; len++) |
||||
offs[len + 1] = offs[len] + count[len]; |
||||
|
||||
/* sort symbols by length, by symbol order within each length */ |
||||
for (sym = 0; sym < codes; sym++) |
||||
if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; |
||||
|
||||
/*
|
||||
Create and fill in decoding tables. In this loop, the table being |
||||
filled is at next and has curr index bits. The code being used is huff |
||||
with length len. That code is converted to an index by dropping drop |
||||
bits off of the bottom. For codes where len is less than drop + curr, |
||||
those top drop + curr - len bits are incremented through all values to |
||||
fill the table with replicated entries. |
||||
|
||||
root is the number of index bits for the root table. When len exceeds |
||||
root, sub-tables are created pointed to by the root entry with an index |
||||
of the low root bits of huff. This is saved in low to check for when a |
||||
new sub-table should be started. drop is zero when the root table is |
||||
being filled, and drop is root when sub-tables are being filled. |
||||
|
||||
When a new sub-table is needed, it is necessary to look ahead in the |
||||
code lengths to determine what size sub-table is needed. The length |
||||
counts are used for this, and so count[] is decremented as codes are |
||||
entered in the tables. |
||||
|
||||
used keeps track of how many table entries have been allocated from the |
||||
provided *table space. It is checked when a LENS table is being made |
||||
against the space in *table, ENOUGH, minus the maximum space needed by |
||||
the worst case distance code, MAXD. This should never happen, but the |
||||
sufficiency of ENOUGH has not been proven exhaustively, hence the check. |
||||
This assumes that when type == LENS, bits == 9. |
||||
|
||||
sym increments through all symbols, and the loop terminates when |
||||
all codes of length max, i.e. all codes, have been processed. This |
||||
routine permits incomplete codes, so another loop after this one fills |
||||
in the rest of the decoding tables with invalid code markers. |
||||
*/ |
||||
|
||||
/* set up for code type */ |
||||
switch (type) { |
||||
case CODES: |
||||
base = extra = work; /* dummy value--not used */ |
||||
end = 19; |
||||
break; |
||||
case LENS: |
||||
base = lbase; |
||||
base -= 257; |
||||
extra = lext; |
||||
extra -= 257; |
||||
end = 256; |
||||
break; |
||||
default: /* DISTS */ |
||||
base = dbase; |
||||
extra = dext; |
||||
end = -1; |
||||
} |
||||
|
||||
/* initialize state for loop */ |
||||
huff = 0; /* starting code */ |
||||
sym = 0; /* starting code symbol */ |
||||
len = min; /* starting code length */ |
||||
next = *table; /* current table to fill in */ |
||||
curr = root; /* current table index bits */ |
||||
drop = 0; /* current bits to drop from code for index */ |
||||
low = (unsigned)(-1); /* trigger new sub-table when len > root */ |
||||
used = 1U << root; /* use root table entries */ |
||||
mask = used - 1; /* mask for comparing low */ |
||||
|
||||
/* check available table space */ |
||||
if (type == LENS && used >= ENOUGH - MAXD) |
||||
return 1; |
||||
|
||||
/* process all codes and make table entries */ |
||||
for (;;) { |
||||
/* create table entry */ |
||||
this.bits = (unsigned char)(len - drop); |
||||
if ((int)(work[sym]) < end) { |
||||
this.op = (unsigned char)0; |
||||
this.val = work[sym]; |
||||
} |
||||
else if ((int)(work[sym]) > end) { |
||||
this.op = (unsigned char)(extra[work[sym]]); |
||||
this.val = base[work[sym]]; |
||||
} |
||||
else { |
||||
this.op = (unsigned char)(32 + 64); /* end of block */ |
||||
this.val = 0; |
||||
} |
||||
|
||||
/* replicate for those indices with low len bits equal to huff */ |
||||
incr = 1U << (len - drop); |
||||
fill = 1U << curr; |
||||
do { |
||||
fill -= incr; |
||||
next[(huff >> drop) + fill] = this; |
||||
} while (fill != 0); |
||||
|
||||
/* backwards increment the len-bit code huff */ |
||||
incr = 1U << (len - 1); |
||||
while (huff & incr) |
||||
incr >>= 1; |
||||
if (incr != 0) { |
||||
huff &= incr - 1; |
||||
huff += incr; |
||||
} |
||||
else |
||||
huff = 0; |
||||
|
||||
/* go to next symbol, update count, len */ |
||||
sym++; |
||||
if (--(count[len]) == 0) { |
||||
if (len == max) break; |
||||
len = lens[work[sym]]; |
||||
} |
||||
|
||||
/* create new sub-table if needed */ |
||||
if (len > root && (huff & mask) != low) { |
||||
/* if first time, transition to sub-tables */ |
||||
if (drop == 0) |
||||
drop = root; |
||||
|
||||
/* increment past last table */ |
||||
next += 1U << curr; |
||||
|
||||
/* determine length of next table */ |
||||
curr = len - drop; |
||||
left = (int)(1 << curr); |
||||
while (curr + drop < max) { |
||||
left -= count[curr + drop]; |
||||
if (left <= 0) break; |
||||
curr++; |
||||
left <<= 1; |
||||
} |
||||
|
||||
/* check for enough space */ |
||||
used += 1U << curr; |
||||
if (type == LENS && used >= ENOUGH - MAXD) |
||||
return 1; |
||||
|
||||
/* point entry in root table to sub-table */ |
||||
low = huff & mask; |
||||
(*table)[low].op = (unsigned char)curr; |
||||
(*table)[low].bits = (unsigned char)root; |
||||
(*table)[low].val = (unsigned short)(next - *table); |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
Fill in rest of table for incomplete codes. This loop is similar to the |
||||
loop above in incrementing huff for table indices. It is assumed that |
||||
len is equal to curr + drop, so there is no loop needed to increment |
||||
through high index bits. When the current sub-table is filled, the loop |
||||
drops back to the root table to fill in any remaining entries there. |
||||
*/ |
||||
this.op = (unsigned char)64; /* invalid code marker */ |
||||
this.bits = (unsigned char)(len - drop); |
||||
this.val = (unsigned short)0; |
||||
while (huff != 0) { |
||||
/* when done with sub-table, drop back to root table */ |
||||
if (drop != 0 && (huff & mask) != low) { |
||||
drop = 0; |
||||
len = root; |
||||
next = *table; |
||||
curr = root; |
||||
this.bits = (unsigned char)len; |
||||
} |
||||
|
||||
/* put invalid code marker in table */ |
||||
next[huff >> drop] = this; |
||||
|
||||
/* backwards increment the len-bit code huff */ |
||||
incr = 1U << (len - 1); |
||||
while (huff & incr) |
||||
incr >>= 1; |
||||
if (incr != 0) { |
||||
huff &= incr - 1; |
||||
huff += incr; |
||||
} |
||||
else |
||||
huff = 0; |
||||
} |
||||
|
||||
/* set return parameters */ |
||||
*table += used; |
||||
*bits = root; |
||||
return 0; |
||||
} |
@ -0,0 +1,55 @@ |
||||
/* inftree9.h -- header to use inftree9.c
|
||||
* Copyright (C) 1995-2003 Mark Adler |
||||
* For conditions of distribution and use, see copyright notice in zlib.h |
||||
*/ |
||||
|
||||
/* WARNING: this file should *not* be used by applications. It is
|
||||
part of the implementation of the compression library and is |
||||
subject to change. Applications should only use zlib.h. |
||||
*/ |
||||
|
||||
/* Structure for decoding tables. Each entry provides either the
|
||||
information needed to do the operation requested by the code that |
||||
indexed that table entry, or it provides a pointer to another |
||||
table that indexes more bits of the code. op indicates whether |
||||
the entry is a pointer to another table, a literal, a length or |
||||
distance, an end-of-block, or an invalid code. For a table |
||||
pointer, the low four bits of op is the number of index bits of |
||||
that table. For a length or distance, the low four bits of op |
||||
is the number of extra bits to get after the code. bits is |
||||
the number of bits in this code or part of the code to drop off |
||||
of the bit buffer. val is the actual byte to output in the case |
||||
of a literal, the base length or distance, or the offset from |
||||
the current table to the next table. Each entry is four bytes. */ |
||||
typedef struct { |
||||
unsigned char op; /* operation, extra bits, table bits */ |
||||
unsigned char bits; /* bits in this part of the code */ |
||||
unsigned short val; /* offset in table or code value */ |
||||
} code; |
||||
|
||||
/* op values as set by inflate_table():
|
||||
00000000 - literal |
||||
0000tttt - table link, tttt != 0 is the number of table index bits |
||||
100eeeee - length or distance, eeee is the number of extra bits |
||||
01100000 - end of block |
||||
01000000 - invalid code |
||||
*/ |
||||
|
||||
/* Maximum size of dynamic tree. The maximum found in a long but non-
|
||||
exhaustive search was 1004 code structures (850 for length/literals |
||||
and 154 for distances, the latter actually the result of an |
||||
exhaustive search). The true maximum is not known, but the value |
||||
below is more than safe. */ |
||||
#define ENOUGH 1440 |
||||
#define MAXD 154 |
||||
|
||||
/* Type of code to build for inftable() */ |
||||
typedef enum { |
||||
CODES, |
||||
LENS, |
||||
DISTS |
||||
} codetype; |
||||
|
||||
extern int inflate_table9 OF((codetype type, unsigned short FAR *lens, |
||||
unsigned codes, code FAR * FAR *table, |
||||
unsigned FAR *bits, unsigned short FAR *work)); |
Loading…
Reference in new issue