A few bug fixes for Windows.
[openssl.git] / crypto / comp / c_zlib.c
index 35ab0c63dc73fe35446a063c22186b0210c3c7dd..c2ff6c701120b80521d8bf41a313ffa02c17dd09 100644 (file)
@@ -1,16 +1,15 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "objects.h"
-#include "comp.h"
+#include <openssl/objects.h>
+#include <openssl/comp.h>
 
 COMP_METHOD *COMP_zlib(void );
 
-#ifndef ZLIB
-
-static COMP_METHOD zlib_method={
+static COMP_METHOD zlib_method_nozlib={
        NID_undef,
-       "(null)",
+       "(undef)",
+       NULL,
        NULL,
        NULL,
        NULL,
@@ -18,6 +17,8 @@ static COMP_METHOD zlib_method={
        NULL,
        };
 
+#ifndef ZLIB
+#undef ZLIB_SHARED
 #else
 
 #include <zlib.h>
@@ -38,14 +39,58 @@ static COMP_METHOD zlib_method={
        zlib_compress_block,
        zlib_expand_block,
        NULL,
+       NULL,
        };
 
-static int zlib_compress_block(ctx,out,olen,in,ilen)
-COMP_CTX *ctx;
-unsigned char *out;
-unsigned int olen;
-unsigned char *in;
-unsigned int ilen;
+/* 
+ * When OpenSSL is built on Windows, we do not want to require that
+ * the ZLIB.DLL be available in order for the OpenSSL DLLs to
+ * work.  Therefore, all ZLIB routines are loaded at run time
+ * and we do not link to a .LIB file.
+ */
+#if defined(WINDOWS) || defined(WIN32)
+# include <windows.h>
+
+# define Z_CALLCONV _stdcall
+# define ZLIB_SHARED
+#else
+# define Z_CALLCONV
+#endif /* !(WINDOWS || WIN32) */
+
+#ifdef ZLIB_SHARED
+#include <openssl/dso.h>
+
+/* Prototypes for built in stubs */
+static int stub_compress(Bytef *dest,uLongf *destLen,
+       const Bytef *source, uLong sourceLen);
+static int stub_inflateEnd(z_streamp strm);
+static int stub_inflate(z_streamp strm, int flush);
+static int stub_inflateInit_(z_streamp strm, const char * version,
+       int stream_size);
+
+/* Function pointers */
+typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
+       const Bytef *source, uLong sourceLen);
+typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
+typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
+typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
+       const char * version, int stream_size);
+static compress_ft     p_compress=NULL;
+static inflateEnd_ft   p_inflateEnd=NULL;
+static inflate_ft      p_inflate=NULL;
+static inflateInit__ft p_inflateInit_=NULL;
+
+static int zlib_loaded = 0;     /* only attempt to init func pts once */
+static DSO *zlib_dso = NULL;
+
+#define compress                stub_compress
+#define inflateEnd              stub_inflateEnd
+#define inflate                 stub_inflate
+#define inflateInit_            stub_inflateInit_
+#endif /* ZLIB_SHARED */
+
+static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
+            unsigned int olen, unsigned char *in, unsigned int ilen)
        {
        unsigned long l;
        int i;
@@ -70,16 +115,15 @@ unsigned int ilen;
                memcpy(&(out[1]),in,ilen);
                l=ilen+1;
                }
-fprintf(stderr,"compress(%4d)->%4d %s\n",ilen,(int)l,(clear)?"clear":"zlib");
+#ifdef DEBUG_ZLIB
+       fprintf(stderr,"compress(%4d)->%4d %s\n",
+               ilen,(int)l,(clear)?"clear":"zlib");
+#endif
        return((int)l);
        }
 
-static int zlib_expand_block(ctx,out,olen,in,ilen)
-COMP_CTX *ctx;
-unsigned char *out;
-unsigned int olen;
-unsigned char *in;
-unsigned int ilen;
+static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
+            unsigned int olen, unsigned char *in, unsigned int ilen)
        {
        unsigned long l;
        int i;
@@ -96,15 +140,15 @@ unsigned int ilen;
                memcpy(out,&(in[1]),ilen-1);
                l=ilen-1;
                }
-        fprintf(stderr,"expand  (%4d)->%4d %s\n",ilen,(int)l,in[0]?"zlib":"clear");
+#ifdef DEBUG_ZLIB
+        fprintf(stderr,"expand  (%4d)->%4d %s\n",
+               ilen,(int)l,in[0]?"zlib":"clear");
+#endif
        return((int)l);
        }
 
-static int zz_uncompress (dest, destLen, source, sourceLen)
-    Bytef *dest;
-    uLongf *destLen;
-    const Bytef *source;
-    uLong sourceLen;
+static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
+            uLong sourceLen)
 {
     z_stream stream;
     int err;
@@ -137,8 +181,80 @@ static int zz_uncompress (dest, destLen, source, sourceLen)
 
 #endif
 
-COMP_METHOD *COMP_zlib()
+COMP_METHOD *COMP_zlib(void)
+       {
+       COMP_METHOD *meth = &zlib_method_nozlib;
+
+#ifdef ZLIB_SHARED
+       if (!zlib_loaded)
+               {
+#if defined(WINDOWS) || defined(WIN32)
+               zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
+#else
+               zlib_dso = DSO_load(NULL, "z", NULL, 0);
+#endif
+               if (zlib_dso != NULL)
+                       {
+                       p_compress
+                               = (compress_ft) DSO_bind_func(zlib_dso,
+                                       "compress");
+                       p_inflateEnd
+                               = (inflateEnd_ft) DSO_bind_func(zlib_dso,
+                                       "inflateEnd");
+                       p_inflate
+                               = (inflate_ft) DSO_bind_func(zlib_dso,
+                                       "inflate");
+                       p_inflateInit_
+                               = (inflateInit__ft) DSO_bind_func(zlib_dso,
+                                       "inflateInit_");
+                       zlib_loaded++;
+                       meth = &zlib_method;
+                       }
+               }
+
+#elif defined(ZLIB)
+       meth = &zlib_method;
+#endif
+
+       return(meth);
+       }
+
+#ifdef ZLIB_SHARED
+/* Stubs for each function to be dynamicly loaded */
+static int 
+stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
+       {
+       if (p_compress)
+               return(p_compress(dest,destLen,source,sourceLen));
+       else
+               return(Z_MEM_ERROR);
+       }
+
+static int
+stub_inflateEnd(z_streamp strm)
+       {
+       if ( p_inflateEnd )
+               return(p_inflateEnd(strm));
+       else
+               return(Z_MEM_ERROR);
+       }
+
+static int
+stub_inflate(z_streamp strm, int flush)
        {
-       return(&zlib_method);
+       if ( p_inflate )
+               return(p_inflate(strm,flush));
+       else
+               return(Z_MEM_ERROR);
+       }
+
+static int
+stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
+       {
+       if ( p_inflateInit_ )
+               return(p_inflateInit_(strm,version,stream_size));
+       else
+               return(Z_MEM_ERROR);
        }
 
+#endif /* ZLIB_SHARED */