4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
6 #include <openssl/err.h>
8 COMP_METHOD *COMP_zlib(void );
10 static COMP_METHOD zlib_method_nozlib={
27 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
28 unsigned int olen, unsigned char *in, unsigned int ilen);
29 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
30 unsigned int olen, unsigned char *in, unsigned int ilen);
32 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
35 static COMP_METHOD zlib_method={
47 * When OpenSSL is built on Windows, we do not want to require that
48 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
49 * work. Therefore, all ZLIB routines are loaded at run time
50 * and we do not link to a .LIB file.
52 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
55 # define Z_CALLCONV _stdcall
59 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
62 #include <openssl/dso.h>
64 /* Prototypes for built in stubs */
65 static int stub_compress(Bytef *dest,uLongf *destLen,
66 const Bytef *source, uLong sourceLen);
67 static int stub_inflateEnd(z_streamp strm);
68 static int stub_inflate(z_streamp strm, int flush);
69 static int stub_inflateInit_(z_streamp strm, const char * version,
72 /* Function pointers */
73 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
74 const Bytef *source, uLong sourceLen);
75 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
76 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
77 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
78 const char * version, int stream_size);
79 static compress_ft p_compress=NULL;
80 static inflateEnd_ft p_inflateEnd=NULL;
81 static inflate_ft p_inflate=NULL;
82 static inflateInit__ft p_inflateInit_=NULL;
84 static int zlib_loaded = 0; /* only attempt to init func pts once */
85 static DSO *zlib_dso = NULL;
87 #define compress stub_compress
88 #define inflateEnd stub_inflateEnd
89 #define inflate stub_inflate
90 #define inflateInit_ stub_inflateInit_
91 #endif /* ZLIB_SHARED */
93 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
94 unsigned int olen, unsigned char *in, unsigned int ilen)
104 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
116 memcpy(&(out[1]),in,ilen);
120 fprintf(stderr,"compress(%4d)->%4d %s\n",
121 ilen,(int)l,(clear)?"clear":"zlib");
126 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
127 unsigned int olen, unsigned char *in, unsigned int ilen)
135 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
141 memcpy(out,&(in[1]),ilen-1);
145 fprintf(stderr,"expand (%4d)->%4d %s\n",
146 ilen,(int)l,in[0]?"zlib":"clear");
151 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
157 stream.next_in = (Bytef*)source;
158 stream.avail_in = (uInt)sourceLen;
159 /* Check for source > 64K on 16-bit machine: */
160 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
162 stream.next_out = dest;
163 stream.avail_out = (uInt)*destLen;
164 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
166 stream.zalloc = (alloc_func)0;
167 stream.zfree = (free_func)0;
169 err = inflateInit(&stream);
170 if (err != Z_OK) return err;
172 err = inflate(&stream, Z_FINISH);
173 if (err != Z_STREAM_END) {
177 *destLen = stream.total_out;
179 err = inflateEnd(&stream);
185 COMP_METHOD *COMP_zlib(void)
187 COMP_METHOD *meth = &zlib_method_nozlib;
192 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
193 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
196 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
199 /* Clear the errors from the first failed
205 zlib_dso = DSO_load(NULL, "z", NULL, 0);
207 if (zlib_dso != NULL)
210 = (compress_ft) DSO_bind_func(zlib_dso,
213 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
216 = (inflate_ft) DSO_bind_func(zlib_dso,
219 = (inflateInit__ft) DSO_bind_func(zlib_dso,
226 #if defined(ZLIB) || defined(ZLIB_SHARED)
234 /* Stubs for each function to be dynamicly loaded */
236 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
239 return(p_compress(dest,destLen,source,sourceLen));
245 stub_inflateEnd(z_streamp strm)
248 return(p_inflateEnd(strm));
254 stub_inflate(z_streamp strm, int flush)
257 return(p_inflate(strm,flush));
263 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
265 if ( p_inflateInit_ )
266 return(p_inflateInit_(strm,version,stream_size));
271 #endif /* ZLIB_SHARED */