zlib 0.91
diff --git a/ChangeLog b/ChangeLog
index 54470f2..e97948e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 		ChangeLog file for zlib
 
+Changes in 0.91 (2 May 95)
+- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h
+- Document the memory requirements in zconf.h
+- added "make install"
+- fix sync search logic in inflateSync
+- deflate(Z_FULL_FLUSH) now works even if output buffer too short
+- after inflateSync, don't scare people with just "lo world"
+- added support for DJGPP
+
 Changes in 0.9 (1 May 95)
 - don't assume that zalloc clears the allocated memory (the TurboC bug
   was Mark's bug after all :)
@@ -10,6 +19,7 @@
 - document explicitly that zalloc(64K) on MSDOS must return a normalized
   pointer (zero offset)
 - added Makefiles for Microsoft C, Turbo C, Borland C++
+- faster crc32()
 
 Changes in 0.8 (29 April 95)
 - added fast inflate (inffast.c)
diff --git a/Makefile b/Makefile
index f251670..9a48249 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,17 @@
+# Makefile for zlib
+# Copyright (C) 1995 Jean-loup Gailly.
+# For conditions of distribution and use, see copyright notice in zlib.h 
+
 CC=cc
 CFLAGS=-O
+#CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
 #CFLAGS=-g -DDEBUG
 LDFLAGS=-L. -lgz
 
 RANLIB=ranlib
 
+prefix=/usr/local
+
 OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
        zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
 
@@ -16,6 +23,14 @@
 	./example
 	echo hello world | ./minigzip | ./minigzip -d 
 
+install: libgz.a
+	-@mkdir $(prefix)/include
+	-@mkdir $(prefix)/lib
+	cp zlib.h zconf.h $(prefix)/include
+	chmod 644 $(prefix)/include/zlib.h $(prefix)/include/zconf.h
+	cp libgz.a $(prefix)/lib
+	chmod 644 $(prefix)/lib/libgz.a
+
 libgz.a: $(OBJS)
 	ar rc $@ $(OBJS)
 	$(RANLIB) $@
diff --git a/Makefile.bak b/Makefile.bak
deleted file mode 100644
index bfe1b74..0000000
--- a/Makefile.bak
+++ /dev/null
@@ -1,59 +0,0 @@
-CC=gcc
-CFLAGS=-O2
-#CFLAGS=-g -DDEBUG
-LDFLAGS=-L. -lgz
-
-RANLIB=ranlib
-
-OBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \
-       zutil.o inflate.o infblock.o inftrees.o infcodes.o infutil.o inffast.o
-
-TEST_OBJS = example.o minigzip.o inftest.o
-
-all: example minigzip inftest
-
-test: all
-	./example
-	echo hello world | ./minigzip | ./minigzip -d 
-
-libgz.a: $(OBJS)
-	ar rc $@ $(OBJS)
-	$(RANLIB) $@
-
-example: example.o libgz.a
-	$(CC) $(CFLAGS) -o $@ example.o $(LDFLAGS)
-
-minigzip: minigzip.o libgz.a
-	$(CC) $(CFLAGS) -o $@ minigzip.o $(LDFLAGS)
-
-inftest: inftest.o libgz.a
-	$(CC) $(CFLAGS) -o $@ inftest.o $(LDFLAGS)
-
-clean:
-	rm -f *.o example minigzip inftest libgz.a foo.gz
-
-zip:
-	zip -ul9 zlib README ChangeLog Makefile *.[ch]
-
-tgz:
-	cd ..; tar cfz zlib/zlib.tgz zlib/README zlib/ChangeLog zlib/Makefile \
-		zlib/*.[ch]
-
-# DO NOT DELETE THIS LINE -- make depend depends on it.
-
-adler32.o: zutil.h zlib.h zconf.h
-compress.o: zlib.h zconf.h
-crc32.o: zutil.h zlib.h zconf.h
-deflate.o: deflate.h zutil.h zlib.h zconf.h
-example.o: zlib.h zconf.h
-gzio.o: zutil.h zlib.h zconf.h
-infblock.o: zutil.h zlib.h zconf.h infblock.h inftrees.h infcodes.h infutil.h
-infcodes.o: zutil.h zlib.h zconf.h inftrees.h infutil.h infcodes.h
-inflate.o: zutil.h zlib.h zconf.h infblock.h
-inftest.o: zutil.h zlib.h zconf.h
-inftrees.o: zutil.h zlib.h zconf.h inftrees.h
-infutil.o: zutil.h zlib.h zconf.h inftrees.h infutil.h
-minigzip.o: zlib.h zconf.h
-trees.o: deflate.h zutil.h zlib.h zconf.h
-uncompr.o: zlib.h zconf.h
-zutil.o: zutil.h zlib.h zconf.h
diff --git a/Makefile.bor b/Makefile.bor
index 877ed62..732ea5d 100644
--- a/Makefile.bor
+++ b/Makefile.bor
@@ -4,7 +4,10 @@
 # To use, do "make -fmakefile.bor"
 
 # WARNING: the small model is supported but only for small values of
-# MAX_WBITS and MAX_MEM_LEVEL
+# MAX_WBITS and MAX_MEM_LEVEL. If you wish to reduce the memory
+# requirements (default 256K for big objects plus a few K), you can add
+# to CFLAGS below: -DMAX_MEM_LEVEL=7 -DMAX_WBITS=14
+# See zconf.h for details about the memory requirements.
 
 # ------------- Turbo C++, Borland C++ -------------
 MODEL=-ml
diff --git a/Makefile.msc b/Makefile.msc
index 375d21c..d6899d8 100644
--- a/Makefile.msc
+++ b/Makefile.msc
@@ -4,7 +4,10 @@
 # To use, do "make makefile.msc"
 
 # WARNING: the small model is supported but only for small values of
-# MAX_WBITS and MAX_MEM_LEVEL
+# MAX_WBITS and MAX_MEM_LEVEL. If you wish to reduce the memory
+# requirements (default 256K for big objects plus a few K), you can add
+# to CFLAGS below: -DMAX_MEM_LEVEL=7 -DMAX_WBITS=14
+# See zconf.h for details about the memory requirements.
 
 # ------------- Microsoft C 5.1 and later -------------
 MODEL=-AL
diff --git a/Makefile.tc b/Makefile.tc
index 51075f7..e173a55 100644
--- a/Makefile.tc
+++ b/Makefile.tc
@@ -4,7 +4,10 @@
 # To use, do "make -fmakefile.tc"
 
 # WARNING: the small model is supported but only for small values of
-# MAX_WBITS and MAX_MEM_LEVEL
+# MAX_WBITS and MAX_MEM_LEVEL. If you wish to reduce the memory
+# requirements (default 256K for big objects plus a few K), you can add
+# to CFLAGS below: -DMAX_MEM_LEVEL=7 -DMAX_WBITS=14
+# See zconf.h for details about the memory requirements.
 
 # ------------- Turbo C 2.0 -------------
 MODEL=-ml
diff --git a/README b/README
index 0c718f1..caeccb9 100644
--- a/README
+++ b/README
@@ -1,25 +1,31 @@
-zlib 0.9 is a beta version of a general purpose compression library.
+zlib 0.91 is a beta version of a general purpose compression library.
 
 The data format used by the zlib library is described in the
-file zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available
+files zlib-3.1.doc, deflate-1.1.doc and gzip-4.1.doc, available
 in ftp.uu.net:/pub/archiving/zip/doc.
 
 All functions of the compression library are documented in the file
 zlib.h. A usage example of the library is given in the file example.c
 which also tests that the library is working correctly.
-To compile all files and run the test program, just type: make test
 
-The changes made in version 0.9 are documented in the file ChangeLog.
-The main changes since 0.8 are:
-- don't assume that zalloc clears the allocated memory
-- let again gzread copy uncompressed data unchanged (was working in 0.71)
-- deflate(Z_FULL_FLUSH), inflateReset and inflateSync are now fully implemented
+To compile all files and run the test program, just type: make test
+(For MSDOS, use one of the special makefiles such as Makefile.msc.)
+To install the zlib library (libgz.a) in /usr/local/lib, type: make install
+To install in a different directory, use for example: make install prefix=$HOME
+This will install in $HOME/lib instead of /usr/local/lib.
+
+The changes made in version 0.91 are documented in the file ChangeLog.
+The main changes since 0.9 are:
+- Default MEM_LEVEL is 8 (not 9 for Unix) as documented in zlib.h
+- Document the memory requirements in zconf.h
+- added "make install"
+- added support for DJGPP
 
 On MSDOS, this version works in both large and small model. However
-small model compression works only for small values of MEM_LEVEL and
-WBITS (see zutil.h). Small model decompression should work up to WBITS=15.
-This version of zlib does not support small or medium model with far
-allocation of big objects.
+small model compression works only for small values of MAX_MEM_LEVEL
+and MAX_WBITS (see zconf.h). Small model decompression should work up
+to MAX_WBITS=15.  This version of zlib does not support small or
+medium model with far allocation of big objects.
 
 
   Copyright (C) 1995 Jean-loup Gailly and Mark Adler
diff --git a/deflate.c b/deflate.c
index 75db3d0..b65ba25 100644
--- a/deflate.c
+++ b/deflate.c
@@ -47,7 +47,7 @@
  *
  */
 
-/* $Id: deflate.c,v 1.6 1995/05/01 17:23:57 jloup Exp $ */
+/* $Id: deflate.c,v 1.7 1995/05/02 13:28:18 jloup Exp $ */
 
 #include "deflate.h"
 
@@ -165,7 +165,7 @@
     z_stream *strm;
     int level;
 {
-    return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, MAX_MEM_LEVEL, 0);
+    return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0);
     /* To do: ignore strm->next_in if we use it as window */
 }
 
@@ -344,22 +344,23 @@
      */
     if (strm->avail_in != 0 ||
 	(flush == Z_FINISH && strm->state->status != FINISH_STATE)) {
+	int quit;
 	
 	if (flush == Z_FINISH) {
 	    strm->state->status = FINISH_STATE;
 	}
         if (strm->state->level <= 3) {
-	    if (deflate_fast(strm->state, flush)) return Z_OK;
+	    quit = deflate_fast(strm->state, flush);
 	} else {
-	    if (deflate_slow(strm->state, flush)) return Z_OK;
+	    quit = deflate_slow(strm->state, flush);
 	}
-	/* ??? remember Z_FULL_FLUSH if we didn't have enough space */
 	if (flush == Z_FULL_FLUSH) {
 	    ct_stored_block(strm->state, (char*)0, 0L, 0); /* special marker */
 	    flush_pending(strm);
 	    CLEAR_HASH(strm->state);             /* forget history */
 	    if (strm->avail_out == 0) return Z_OK;
 	}
+	if (quit) return Z_OK;
     }
     Assert(strm->avail_out > 0, "bug2");
 
diff --git a/example.c b/example.c
index c1bac1e..5c34733 100644
--- a/example.c
+++ b/example.c
@@ -3,7 +3,7 @@
  * For conditions of distribution and use, see copyright notice in zlib.h 
  */
 
-/* $Id: example.c,v 1.7 1995/05/01 16:57:22 jloup Exp $ */
+/* $Id: example.c,v 1.8 1995/05/02 15:52:32 jloup Exp $ */
 
 #include <stdio.h>
 #include "zlib.h"
@@ -12,7 +12,9 @@
 #  include <string.h>
 #endif
 
+#ifndef __GO32__
 extern void exit  __P((int));
+#endif
 
 #define BUFLEN 4096
 
@@ -253,7 +255,7 @@
     err = inflateEnd(&d_stream);
     CHECK_ERR(err, "inflateEnd");
 
-    printf("after inflateSync(): %s\n", uncompr);
+    printf("after inflateSync(): hel%s\n", uncompr);
 }
 
 /* ===========================================================================
diff --git a/gzio.c b/gzio.c
index 1f5e130..7a3d05a 100644
--- a/gzio.c
+++ b/gzio.c
@@ -3,7 +3,7 @@
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
-/* $Id: gzio.c,v 1.6 1995/04/30 19:52:21 jloup Exp $ */
+/* $Id: gzio.c,v 1.7 1995/05/02 12:22:08 jloup Exp $ */
 
 #include <stdio.h>
 
@@ -128,7 +128,7 @@
     
     if (s->mode == 'w') {
 	err = deflateInit2(&(s->stream), Z_DEFAULT_COMPRESSION,
-			   DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, 0);
+			   DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
 	/* windowBits is passed < 0 to suppress zlib header */
 
 	s->stream.next_out = s->outbuf = ALLOC(Z_BUFSIZE);
@@ -221,7 +221,7 @@
     char *mode;
 {
     char name[20];
-    sprintf(name, "_fd:%d_", fd); /* for debugging */
+    sprintf(name, "<fd:%d>", fd); /* for debugging */
 
     return gz_open (name, mode, fd);
 }
diff --git a/inflate.c b/inflate.c
index bed4e54..0b0ee87 100644
--- a/inflate.c
+++ b/inflate.c
@@ -145,7 +145,7 @@
   {
     case METHOD:
       NEEDBYTE
-      if (((z->state->sub.method = NEXTBYTE) & 0xf != DEFLATED))
+      if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
       {
         z->state->mode = BAD;
 	z->msg = "unknown compression method";
@@ -243,7 +243,10 @@
   if (z == Z_NULL || z->state == Z_NULL)
     return Z_STREAM_ERROR;
   if (z->state->mode != BAD)
+  {
+    z->state->mode = BAD;
     z->state->sub.marker = 0;
+  }
   if ((n = z->avail_in) == 0)
     return Z_BUF_ERROR;
   p = z->next_in;
@@ -252,10 +255,12 @@
   /* search */
   while (n && m < 4)
   {
-    if (*p == (m < 2 ? 0 : 0xff))
+    if (*p == (Byte)(m < 2 ? 0 : 0xff))
       m++;
-    else if (*p || m > 2)
+    else if (*p)
       m = 0;
+    else
+      m = 4 - m;
     p++, n--;
   }
 
diff --git a/minigzip.c b/minigzip.c
index d3d2fe9..8f58080 100644
--- a/minigzip.c
+++ b/minigzip.c
@@ -13,12 +13,14 @@
  * or in pipe mode.
  */
 
-/* $Id: minigzip.c,v 1.3 1995/04/29 14:27:21 jloup Exp $ */
+/* $Id: minigzip.c,v 1.4 1995/05/02 15:54:22 jloup Exp $ */
 
 #include <stdio.h>
 #include "zlib.h"
 
+#ifndef __GO32__
 extern void exit  __P((int));
+#endif
 extern int unlink __P((const char *));
 
 #ifdef STDC
diff --git a/zconf.h b/zconf.h
index 4e05a3e..1abb0ea 100644
--- a/zconf.h
+++ b/zconf.h
@@ -3,7 +3,7 @@
  * For conditions of distribution and use, see copyright notice in zlib.h 
  */
 
-/* $Id: zconf.h,v 1.10 1995/04/30 19:27:14 jloup Exp $ */
+/* $Id: zconf.h,v 1.11 1995/05/02 13:07:21 jloup Exp $ */
 
 #ifndef _ZCONF_H
 #define _ZCONF_H
@@ -32,6 +32,7 @@
 #  define STDC
 #endif
 
+/* Maximum value for memLevel in deflateInit2 */
 #ifndef MAX_MEM_LEVEL
 #  ifdef MAXSEG_64K
 #    define MAX_MEM_LEVEL 8
@@ -40,10 +41,24 @@
 #  endif
 #endif
 
+/* Maximum value for windowBits in deflateInit2 and inflateInit2 */
 #ifndef MAX_WBITS
 #  define MAX_WBITS   15 /* 32K LZ77 window */
 #endif
 
+/* The memory requirements for deflate are (in bytes):
+            1 << (windowBits+2)   +  1 << (memLevel+9)
+ that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
+ plus a few kilobytes for small objects. For example, if you want to reduce
+ the default memory requirements from 256K to 128K, compile with
+     make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
+ Of course this will generally degrade compression (there's no free lunch).
+
+   The memory requirements for inflate are (in bytes) 1 << windowBits
+ that is, 32K for windowBits=15 (default value) plus a few kilobytes
+ for small objects.
+*/
+
 			/* Type declarations */
 
 #ifndef __P /* function prototypes */
diff --git a/zlib.h b/zlib.h
index e64ab91..1712209 100644
--- a/zlib.h
+++ b/zlib.h
@@ -1,5 +1,5 @@
 /* zlib.h -- interface of the 'zlib' general purpose compression library
-  version 0.9 April 30th, 1995.
+  version 0.91 May 2nd, 1995.
 
   Copyright (C) 1995 Jean-loup Gailly and Mark Adler
 
@@ -28,7 +28,7 @@
 
 #include "zconf.h"
 
-#define ZLIB_VERSION "0.9"
+#define ZLIB_VERSION "0.91"
 
 /* 
      The 'zlib' compression library provides in-memory compression and
@@ -335,7 +335,8 @@
     The memLevel parameter specifies how much memory should be allocated
    for the internal compression state. memLevel=1 uses minimum memory but
    is slow and reduces compression ratio; memLevel=9 uses maximum memory
-   for optimal speed. The default value is 8.
+   for optimal speed. The default value is 8. See zconf.h for total memory
+   usage as a function of windowBits and memLevel.
 
      The strategy parameter is used to tune the compression algorithm. Use
    the value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data
diff --git a/zutil.c b/zutil.c
index 4ceb89f..508ad62 100644
--- a/zutil.c
+++ b/zutil.c
@@ -3,13 +3,15 @@
  * For conditions of distribution and use, see copyright notice in zlib.h 
  */
 
-/* $Id: zutil.c,v 1.6 1995/04/29 14:54:02 jloup Exp $ */
+/* $Id: zutil.c,v 1.7 1995/05/02 15:54:47 jloup Exp $ */
 
 #include <stdio.h>
 
 #include "zutil.h"
 
+#ifndef __GO32__
 extern void exit __P((int));
+#endif
 
 char *zlib_version = ZLIB_VERSION;
 
@@ -55,8 +57,9 @@
 }
 #endif
 
-#if defined(MSDOS) && !defined(__SMALL__) && !defined(M_I86SM)
-#  ifdef __TURBOC__
+#if defined(__TURBOC__) && !defined(__SMALL__)
+
+#  define MY_ZCALLOC
 
 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  * and farmalloc(64K) returns a pointer with an offset of 8, so we
@@ -124,8 +127,11 @@
     ptr = opaque; /* just to make some compilers happy */
     z_error("zcfree: ptr not found");
 }
+#endif /* __TURBOC__ */
 
-#  else /* MSC */
+#if defined(M_I86CM) || defined(M_I86LM) /* MSC compact or large model */
+
+#  define MY_ZCALLOC
 
 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
 #  define _halloc  halloc
@@ -144,12 +150,15 @@
     _hfree(ptr);
 }
 
-#  endif /* __TURBOC__ ? */
+#endif /* defined(M_I86CM) || defined(M_I86LM) */
 
-#else /* !MSDOS */
 
+#ifndef MY_ZCALLOC /* Any system without a special alloc function */
+
+#ifndef __GO32__
 extern voidp calloc __P((uInt items, uInt size));
 extern void  free   __P((voidp ptr));
+#endif
 
 voidp zcalloc (opaque, items, size)
     voidp opaque;
@@ -166,4 +175,4 @@
     free(ptr);
 }
 
-#endif /* MSDOS */
+#endif /* MY_ZCALLOC */
diff --git a/zutil.h b/zutil.h
index a4b00a0..503a445 100644
--- a/zutil.h
+++ b/zutil.h
@@ -8,7 +8,7 @@
    subject to change. Applications should only use zlib.h.
  */
 
-/* $Id: zutil.h,v 1.7 1995/04/30 10:55:33 jloup Exp $ */
+/* $Id: zutil.h,v 1.8 1995/05/02 15:44:46 jloup Exp $ */
 
 #ifndef _Z_UTIL_H
 #define _Z_UTIL_H
@@ -17,6 +17,7 @@
 
 #ifdef MSDOS
 #   include <stddef.h>
+#   include <errno.h>
 #else
     extern int errno;
 #endif
@@ -43,7 +44,14 @@
 #define DEFLATED   8
 
 #define DEF_WBITS 15
-/* default WBITS for decompression. MAX_WBITS is useful for compression only */
+/* default windowBits for decompression. MAX_WBITS is for compression only */
+
+#if MAX_MEM_LEVEL >= 8
+#  define DEF_MEM_LEVEL 8
+#else
+#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
+#endif
+/* default memLevel */
 
 #define STORED_BLOCK 0
 #define STATIC_TREES 1