]> git.lizzy.rs Git - zlib.git/blobdiff - inflate.c
zlib 1.2.0.5
[zlib.git] / inflate.c
index 0fd158d8f71dc79e17ac61873e5cff3004f7a2ee..287efdaff8610363925ee03deef37c5289aa9130 100644 (file)
--- a/inflate.c
+++ b/inflate.c
@@ -97,7 +97,7 @@ local int updatewindow OF((z_streamp strm, unsigned out));
 #ifdef BUILDFIXED
    void makefixed OF((void));
 #endif
-local unsigned syncsearch OF((unsigned *have, unsigned char FAR *buf,
+local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
                               unsigned len));
 
 int ZEXPORT inflateReset(strm)
@@ -113,6 +113,7 @@ z_streamp strm;
     state->last = 0;
     state->havedict = 0;
     state->wsize = 0;
+    state->whave = 0;
     state->hold = 0;
     state->bits = 0;
     state->lencode = state->distcode = state->next = state->codes;
@@ -133,11 +134,11 @@ int stream_size;
         return Z_VERSION_ERROR;
     if (strm == Z_NULL) return Z_STREAM_ERROR;
     strm->msg = Z_NULL;                 /* in case we return an error */
-    if (strm->zalloc == Z_NULL) {
+    if (strm->zalloc == (alloc_func)0) {
         strm->zalloc = zcalloc;
         strm->opaque = (voidpf)0;
     }
-    if (strm->zfree == Z_NULL) strm->zfree = zcfree;
+    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;
@@ -150,7 +151,7 @@ int stream_size;
     else {
         state->wrap = (windowBits >> 4) + 1;
 #ifdef GUNZIP
-        windowBits &= 15;
+        if (windowBits < 48) windowBits &= 15;
 #endif
     }
     if (windowBits < 8 || windowBits > 15) {
@@ -320,6 +321,7 @@ unsigned out;
     if (state->wsize == 0) {
         state->wsize = 1U << state->wbits;
         state->write = 0;
+        state->whave = 0;
     }
 
     /* copy state->wsize or less output bytes into the circular window */
@@ -327,6 +329,7 @@ unsigned out;
     if (copy >= state->wsize) {
         zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
         state->write = 0;
+        state->whave = state->wsize;
     }
     else {
         dist = state->wsize - state->write;
@@ -336,10 +339,12 @@ unsigned out;
         if (copy) {
             zmemcpy(state->window, strm->next_out - copy, copy);
             state->write = copy;
+            state->whave = state->wsize;
         }
         else {
             state->write += dist;
             if (state->write == state->wsize) state->write = 0;
+            if (state->whave < state->wsize) state->whave += dist;
         }
     }
     return 0;
@@ -531,13 +536,14 @@ z_streamp strm;
 int flush;
 {
     struct inflate_state FAR *state;
-    unsigned char *next, *put;  /* next input and output */
+    unsigned char FAR *next;    /* next input */
+    unsigned char FAR *put;     /* next output */
     unsigned have, left;        /* available input and output */
     unsigned long hold;         /* bit buffer */
     unsigned bits;              /* bits in bit buffer */
     unsigned in, out;           /* save starting available input and output */
     unsigned copy;              /* number of stored or match bytes to copy */
-    unsigned char *from;        /* where to copy match bytes from */
+    unsigned char FAR *from;    /* where to copy match bytes from */
     code this;                  /* current decoding table entry */
     code last;                  /* parent table entry */
     unsigned len;               /* length to copy for repeats, bits to drop */
@@ -553,6 +559,7 @@ int flush;
         return Z_STREAM_ERROR;
 
     state = (struct inflate_state FAR *)strm->state;
+    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
     LOAD();
     in = have;
     out = left;
@@ -703,6 +710,8 @@ int flush;
             strm->adler = state->check = adler32(0L, Z_NULL, 0);
             state->mode = TYPE;
         case TYPE:
+            if (flush == Z_BLOCK) goto inf_leave;
+        case TYPEDO:
             if (state->last) {
                 BYTEBITS();
                 state->mode = CHECK;
@@ -956,8 +965,7 @@ int flush;
                 state->offset += BITS(state->extra);
                 DROPBITS(state->extra);
             }
-            if (state->offset > (state->wsize ? state->wsize :
-                                                out - left)) {
+            if (state->offset > state->whave + out - left) {
                 strm->msg = (char *)"invalid distance too far back";
                 state->mode = BAD;
                 break;
@@ -1066,6 +1074,8 @@ int flush;
     if (state->wrap && out)
         strm->adler = state->check =
             UPDATE(state->check, strm->next_out - out, out);
+    strm->data_type = state->bits + (state->last ? 8 : 0) +
+                      (state->mode == TYPE ? 16 : 0);
     if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
         ret = Z_BUF_ERROR;
     return ret;
@@ -1075,7 +1085,7 @@ int ZEXPORT inflateEnd(strm)
 z_streamp strm;
 {
     struct inflate_state FAR *state;
-    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == Z_NULL)
+    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
         return Z_STREAM_ERROR;
     state = (struct inflate_state FAR *)strm->state;
     if (state->window != Z_NULL) ZFREE(strm, state->window);
@@ -1108,12 +1118,16 @@ uInt dictLength;
         state->mode = MEM;
         return Z_MEM_ERROR;
     }
-    if (dictLength > state->wsize)
+    if (dictLength > state->wsize) {
         zmemcpy(state->window, dictionary + dictLength - state->wsize,
                 state->wsize);
-    else
+        state->whave = state->wsize;
+    }
+    else {
         zmemcpy(state->window + state->wsize - dictLength, dictionary,
                 dictLength);
+        state->whave = dictLength;
+    }
     state->havedict = 1;
     Tracev((stderr, "inflate:   dictionary set\n"));
     return Z_OK;
@@ -1131,7 +1145,7 @@ uInt dictLength;
    zero for the first call.
  */
 local unsigned syncsearch(have, buf, len)
-unsigned *have;
+unsigned FAR *have;
 unsigned char FAR *buf;
 unsigned len;
 {
@@ -1224,7 +1238,7 @@ z_streamp source;
 
     /* check input */
     if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
-        source->zalloc == Z_NULL || source->zfree == Z_NULL)
+        source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
         return Z_STREAM_ERROR;
     state = (struct inflate_state FAR *)source->state;