minor bugfixes
[rsync/rsync.git] / lib / zlib.c
index 474fa7c..82e82c7 100644 (file)
@@ -7,7 +7,6 @@
  *
  * Changes that have been made include:
  * - changed functions not used outside this file to "local"
- * - added minCompression parameter to deflateInit2
  * - added Z_PACKET_FLUSH (see zlib.h for details)
  * - added inflateIncomp
  *
@@ -30,6 +29,7 @@
 
 #define _Z_UTIL_H
 
+#include "../rsync.h"
 #include "zlib.h"
 
 #ifndef local
@@ -80,22 +80,8 @@ extern char *z_errmsg[]; /* indexed by 1-zlib_error */
 /* The minimum and maximum match lengths */
 
          /* functions */
-
-#if defined(KERNEL) || defined(_KERNEL)
-#  define zmemcpy(d, s, n)     bcopy((s), (d), (n))
-#  define zmemzero             bzero
-#else
-#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
-#  define HAVE_MEMCPY
-#endif
-#ifdef HAVE_MEMCPY
-#    define zmemcpy memcpy
-#    define zmemzero(dest, len) memset(dest, 0, len)
-#else
-   extern void zmemcpy  OF((Bytef* dest, Bytef* source, uInt len));
-   extern void zmemzero OF((Bytef* dest, uInt len));
-#endif
-#endif
+#define zmemcpy(d, s, n)       bcopy((s), (d), (n))
+#define zmemzero               bzero
 
 /* Diagnostic functions */
 #ifdef DEBUG_ZLIB
@@ -224,7 +210,6 @@ typedef struct deflate_state {
     int   noheader;      /* suppress zlib header and adler32 */
     Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
     Byte  method;        /* STORED (for zip only) or DEFLATED */
-    int          minCompr;      /* min size decrease for Z_FLUSH_NOSTORE */
 
                 /* used by deflate.c: */
 
@@ -468,7 +453,7 @@ local void ct_stored_type_only OF((deflate_state *s));
 
 /* From: deflate.c,v 1.8 1995/05/03 17:27:08 jloup Exp */
 
-local char zlib_copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
+char zlib_copyright[] = " deflate Copyright 1995 Jean-loup Gailly ";
 /*
   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
@@ -535,7 +520,7 @@ local void lm_init       OF((deflate_state *s));
 local int longest_match  OF((deflate_state *s, IPos cur_match));
 local void putShortMSB   OF((deflate_state *s, uInt b));
 local void flush_pending OF((z_stream *strm));
-local int read_buf       OF((z_stream *strm, charf *buf, unsigned size));
+local int zread_buf       OF((z_stream *strm, charf *buf, unsigned size));
 #ifdef ASMV
       void match_init OF((void)); /* asm code initialization */
 #endif
@@ -581,21 +566,18 @@ int deflateInit (strm, level)
     z_stream *strm;
     int level;
 {
-    return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
-                        0, 0);
+    return deflateInit2 (strm, level, DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, 0);
     /* To do: ignore strm->next_in if we use it as window */
 }
 
 /* ========================================================================= */
-int deflateInit2 (strm, level, method, windowBits, memLevel,
-                 strategy, minCompression)
+int deflateInit2 (strm, level, method, windowBits, memLevel, strategy)
     z_stream *strm;
     int  level;
     int  method;
     int  windowBits;
     int  memLevel;
     int  strategy;
-    int  minCompression;
 {
     deflate_state *s;
     int noheader = 0;
@@ -655,7 +637,6 @@ int deflateInit2 (strm, level, method, windowBits, memLevel,
     s->level = level;
     s->strategy = strategy;
     s->method = (Byte)method;
-    s->minCompr = minCompression;
     s->blocks_in_packet = 0;
 
     return deflateReset(strm);
@@ -874,7 +855,7 @@ int deflateEnd (strm)
  * Read a new buffer from the current input stream, update the adler32
  * and total number of bytes read.
  */
-local int read_buf(strm, buf, size)
+local int zread_buf(strm, buf, size)
     z_stream *strm;
     charf *buf;
     unsigned size;
@@ -1179,7 +1160,7 @@ local void fill_window(s)
          */
         Assert(more >= 2, "more < 2");
 
-        n = read_buf(s->strm, (charf *)s->window + s->strstart + s->lookahead,
+        n = zread_buf(s->strm, (charf *)s->window + s->strstart + s->lookahead,
                      more);
         s->lookahead += n;
 
@@ -1671,7 +1652,6 @@ local void send_bits(s, value, length)
 #endif /* DEBUG_ZLIB */
 
 
-#define MAX(a,b) (a >= b ? a : b)
 /* the arguments must not have side effects */
 
 /* ===========================================================================
@@ -2351,17 +2331,6 @@ local ulg ct_flush_block(s, buf, stored_len, flush)
     } else
 #endif /* STORED_FILE_OK */
 
-    /* For Z_PACKET_FLUSH, if we don't achieve the required minimum
-     * compression, and this block contains all the data since the last
-     * time we used Z_PACKET_FLUSH, then just omit this block completely
-     * from the output.
-     */
-    if (flush == Z_PACKET_FLUSH && s->blocks_in_packet == 1
-       && opt_lenb > stored_len - s->minCompr) {
-       s->blocks_in_packet = 0;
-       /* output nothing */
-    } else
-
 #ifdef FORCE_STORED
     if (buf != (char*)0) /* force stored block */
 #else
@@ -2756,7 +2725,7 @@ struct internal_state {
       CHECK2,   /* two check bytes to go */
       CHECK1,   /* one check byte to go */
       DONE,     /* finished check, done */
-      BAD}      /* got an error--stay here */
+      ZBAD}      /* got an error--stay here */
     mode;               /* current inflate mode */
 
   /* mode dependent information */
@@ -2766,7 +2735,7 @@ struct internal_state {
       uLong was;                /* computed check value */
       uLong need;               /* stream check value */
     } check;            /* if CHECK, check values to compare */
-    uInt marker;        /* if BAD, inflateSync's marker bytes count */
+    uInt marker;        /* if ZBAD, inflateSync's marker bytes count */
   } sub;        /* submode */
 
   /* mode independent information */
@@ -2882,14 +2851,14 @@ int f;
       NEEDBYTE
       if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED)
       {
-        z->state->mode = BAD;
+        z->state->mode = ZBAD;
         z->msg = "unknown compression method";
         z->state->sub.marker = 5;       /* can't try inflateSync */
         break;
       }
       if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
       {
-        z->state->mode = BAD;
+        z->state->mode = ZBAD;
         z->msg = "invalid window size";
         z->state->sub.marker = 5;       /* can't try inflateSync */
         break;
@@ -2899,14 +2868,14 @@ int f;
       NEEDBYTE
       if ((b = NEXTBYTE) & 0x20)
       {
-        z->state->mode = BAD;
+        z->state->mode = ZBAD;
         z->msg = "invalid reserved bit";
         z->state->sub.marker = 5;       /* can't try inflateSync */
         break;
       }
       if (((z->state->sub.method << 8) + b) % 31)
       {
-        z->state->mode = BAD;
+        z->state->mode = ZBAD;
         z->msg = "incorrect header check";
         z->state->sub.marker = 5;       /* can't try inflateSync */
         break;
@@ -2919,7 +2888,7 @@ int f;
          r = inflate_packet_flush(z->state->blocks);
       if (r == Z_DATA_ERROR)
       {
-        z->state->mode = BAD;
+        z->state->mode = ZBAD;
         z->state->sub.marker = 0;       /* can try inflateSync */
         break;
       }
@@ -2951,7 +2920,7 @@ int f;
 
       if (z->state->sub.check.was != z->state->sub.check.need)
       {
-        z->state->mode = BAD;
+        z->state->mode = ZBAD;
         z->msg = "incorrect data check";
         z->state->sub.marker = 5;       /* can't try inflateSync */
         break;
@@ -2960,7 +2929,7 @@ int f;
       z->state->mode = DONE;
     case DONE:
       return Z_STREAM_END;
-    case BAD:
+    case ZBAD:
       return Z_DATA_ERROR;
     default:
       return Z_STREAM_ERROR;
@@ -2969,7 +2938,7 @@ int f;
  empty:
   if (f != Z_PACKET_FLUSH)
     return r;
-  z->state->mode = BAD;
+  z->state->mode = ZBAD;
   z->state->sub.marker = 0;       /* can try inflateSync */
   return Z_DATA_ERROR;
 }
@@ -3003,9 +2972,9 @@ z_stream *z;
   /* set up */
   if (z == Z_NULL || z->state == Z_NULL)
     return Z_STREAM_ERROR;
-  if (z->state->mode != BAD)
+  if (z->state->mode != ZBAD)
   {
-    z->state->mode = BAD;
+    z->state->mode = ZBAD;
     z->state->sub.marker = 0;
   }
   if ((n = z->avail_in) == 0)
@@ -3974,7 +3943,9 @@ z_stream *z;            /* for zfree function */
 
 
 /* build fixed tables only once--keep them here */
-local int fixed_lock = 0;
+#ifdef MULTI_THREADED
+local volatile int fixed_lock = 0;
+#endif
 local int fixed_built = 0;
 #define FIXEDH 530      /* number of hufts used by fixed tables */
 local uInt fixed_left = FIXEDH;
@@ -4015,8 +3986,10 @@ inflate_huft * FAR *tl;  /* literal/length tree result */
 inflate_huft * FAR *td;  /* distance tree result */
 {
   /* build fixed tables if not built already--lock out other instances */
+#ifdef MULTI_THREADED
   while (++fixed_lock > 1)
     fixed_lock--;
+#endif
   if (!fixed_built)
   {
     int k;              /* temporary variable */
@@ -4049,7 +4022,9 @@ inflate_huft * FAR *td;  /* distance tree result */
     /* done */
     fixed_built = 1;
   }
+#ifdef MULTI_THREADED
   fixed_lock--;
+#endif
   *bl = fixed_bl;
   *bd = fixed_bd;
   *tl = fixed_tl;