Index: cpukit/libnetworking/net/zlib.c
===================================================================
--- cpukit/libnetworking/net/zlib.c	(revision 0819f4c3bc1b10cea48c3181447e1168a60e2660)
+++ cpukit/libnetworking/net/zlib.c	(revision 8281e06c0df7d57d781ef5322d98ca9976c21a85)
@@ -11,6 +11,8 @@
  * - allow strm->next_out to be NULL, meaning discard the output
  *
- * $Id$
- */
+ * $FreeBSD: src/sys/net/zlib.c,v 1.20 2005/09/11 16:13:02 rodrigc Exp $
+ */
+
+/* $Id$ */
 
 /* 
@@ -25,5 +27,5 @@
 #define MY_ZCALLOC
 
-#if defined(__FreeBSD__) && (defined(KERNEL) || defined(_KERNEL))
+#if defined(__FreeBSD__) && defined(_KERNEL)
 #define inflate	inflate_ppp	/* FreeBSD already has an inflate :-( */
 #endif
@@ -31,5 +33,6 @@
 
 /* +++ zutil.h */
-/* zutil.h -- internal interface and configuration of the compression library
+/*-
+ * zutil.h -- internal interface and configuration of the compression library
  * Copyright (C) 1995-1996 Jean-loup Gailly.
  * For conditions of distribution and use, see copyright notice in zlib.h
@@ -46,16 +49,21 @@
 #define _Z_UTIL_H
 
+#ifdef _KERNEL
+#include <net/zlib.h>
+#else
 #include "zlib.h"
-
-#if defined(KERNEL) || defined(_KERNEL)
+#endif
+
+#ifdef _KERNEL
 /* Assume this is a *BSD or SVR4 kernel */
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/systm.h>
-#undef u
+#include <sys/param.h>
+#include <sys/kernel.h>
+#ifndef __rtems__
+#include <sys/module.h>
+#endif
 #  define HAVE_MEMCPY
-#  define memcpy(d, s, n)	bcopy((s), (d), (n))
-#  define memset(d, v, n)	bzero((d), (n))
-#  define memcmp		bcmp
 
 #else
@@ -78,5 +86,5 @@
 #endif
 #endif /* __KERNEL__ */
-#endif /* _KERNEL || KERNEL */
+#endif /* _KERNEL */
 
 #ifndef local
@@ -91,5 +99,5 @@
 typedef unsigned long  ulg;
 
-extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
+static const char *z_errmsg[10]; /* indexed by 2-zlib_error */
 /* (size given to avoid silly warnings with Visual C++) */
 
@@ -97,5 +105,5 @@
 
 #define ERR_RETURN(strm,err) \
-  return (strm->msg = (char*)ERR_MSG(err), (err))
+  return (strm->msg = (const char*)ERR_MSG(err), (err))
 /* To be used only when the state is known to be valid */
 
@@ -774,5 +782,5 @@
     }
     if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
-        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
+        windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
 	strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
         return Z_STREAM_ERROR;
@@ -805,5 +813,5 @@
     if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
         s->pending_buf == Z_NULL) {
-        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
+        strm->msg = (const char*)ERR_MSG(Z_MEM_ERROR);
         deflateEnd (strm);
         return Z_MEM_ERROR;
@@ -1984,5 +1992,5 @@
 
 #ifndef DEBUG_ZLIB
-#  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
+#  define send_code(s, c, tree) send_bits(s, tree[(c)].Code, tree[(c)].Len)
    /* Send a code of the given tree. c and tree must not have side effects */
 
@@ -2042,20 +2050,20 @@
 
 #define send_bits(s, value, length) \
-{ int len = length;\
-  if (s->bi_valid > (int)Buf_size - len) {\
-    int val = value;\
-    s->bi_buf |= (val << s->bi_valid);\
-    put_short(s, s->bi_buf);\
-    s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
-    s->bi_valid += len - Buf_size;\
+{ int len = (length);\
+  if ((s)->bi_valid > (int)Buf_size - len) {\
+    int val = (value);\
+    (s)->bi_buf |= (val << (s)->bi_valid);\
+    put_short((s), (s)->bi_buf);\
+    (s)->bi_buf = (ush)val >> (Buf_size - (s)->bi_valid);\
+    (s)->bi_valid += len - Buf_size;\
   } else {\
-    s->bi_buf |= (value) << s->bi_valid;\
-    s->bi_valid += len;\
+    (s)->bi_buf |= (value) << (s)->bi_valid;\
+    (s)->bi_valid += len;\
   }\
 }
 #endif /* DEBUG_ZLIB */
 
-
 #define MAX(a,b) (a >= b ? a : b)
+
 /* the arguments must not have side effects */
 
@@ -3862,8 +3870,9 @@
       if (t != Z_OK)
       {
+        r = t;
+        if (r == Z_DATA_ERROR) {
         ZFREE(z, s->sub.trees.blens);
-        r = t;
-        if (r == Z_DATA_ERROR)
           s->mode = BADB;
+        }
         LEAVE
       }
@@ -3930,9 +3939,10 @@
         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
                                   s->sub.trees.blens, &bl, &bd, &tl, &td, z);
-        ZFREE(z, s->sub.trees.blens);
         if (t != Z_OK)
         {
-          if (t == (uInt)Z_DATA_ERROR)
+          if (t == (uInt)Z_DATA_ERROR) {
+            ZFREE(z, s->sub.trees.blens);
             s->mode = BADB;
+          }
           r = t;
           LEAVE
@@ -3947,4 +3957,9 @@
           LEAVE
         }
+	/*
+	 * this ZFREE must occur *BEFORE* we mess with sub.decode, because
+	 * sub.trees is union'd with sub.decode.
+	 */
+        ZFREE(z, s->sub.trees.blens);
         s->sub.decode.codes = c;
         s->sub.decode.tl = tl;
@@ -5129,5 +5144,5 @@
 #endif
 
-const char *z_errmsg[10] = {
+static const char *z_errmsg[10] = {
 "need dictionary",     /* Z_NEED_DICT       2  */
 "stream end",          /* Z_STREAM_END      1  */
@@ -5339,8 +5354,8 @@
 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
 
-#define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
-#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
-#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
-#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
+#define DO1(buf,i)  {s1 += buf[(i)]; s2 += s1;}
+#define DO2(buf,i)  DO1(buf,i); DO1(buf,(i)+1);
+#define DO4(buf,i)  DO2(buf,i); DO2(buf,(i)+2);
+#define DO8(buf,i)  DO4(buf,i); DO4(buf,(i)+4);
 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
 
Index: cpukit/libnetworking/net/zlib.h
===================================================================
--- cpukit/libnetworking/net/zlib.h	(revision 0819f4c3bc1b10cea48c3181447e1168a60e2660)
+++ cpukit/libnetworking/net/zlib.h	(revision 8281e06c0df7d57d781ef5322d98ca9976c21a85)
@@ -1,3 +1,3 @@
-/*	$Id$	*/
+/* $FreeBSD: src/sys/net/zlib.h,v 1.10 2005/01/07 01:45:35 imp Exp $	*/
 
 /*
@@ -17,5 +17,6 @@
 
 /* +++ zlib.h */
-/* zlib.h -- interface of the 'zlib' general purpose compression library
+/*-
+  zlib.h -- interface of the 'zlib' general purpose compression library
   version 1.0.4, Jul 24th, 1996.
 
@@ -40,6 +41,6 @@
   Jean-loup Gailly        Mark Adler
   gzip@prep.ai.mit.edu    madler@alumni.caltech.edu
-
-
+*/
+/*
   The data format used by the zlib library is described by RFCs (Request for
   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
@@ -282,5 +283,5 @@
     uLong    total_out; /* total nb of bytes output so far */
 
-    char     *msg;      /* last error message, NULL if no error */
+    const char     *msg; /* last error message, NULL if no error */
     struct internal_state FAR *state; /* not visible by applications */
 
@@ -508,4 +509,7 @@
 */
 
+#if defined(__FreeBSD__) && defined(_KERNEL)
+#define inflate       inflate_ppp     /* FreeBSD already has an inflate :-( */
+#endif
 
 extern int EXPORT inflate OF((z_streamp strm, int flush));
@@ -926,5 +930,5 @@
      Returns the error message for the last error which occurred on the
    given compressed file. errnum is set to zlib error number. If an
-   error occurred in the file system and not in the compression library,
+   error occurred in the filesystem and not in the compression library,
    errnum is set to Z_ERRNO and the application may consult errno
    to get the exact error code.
