4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
7 COMP_METHOD *COMP_zlib(void );
9 static COMP_METHOD zlib_method_nozlib={
26 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
27 unsigned int olen, unsigned char *in, unsigned int ilen);
28 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
29 unsigned int olen, unsigned char *in, unsigned int ilen);
31 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
34 static COMP_METHOD zlib_method={
46 * When OpenSSL is built on Windows, we do not want to require that
47 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
48 * work. Therefore, all ZLIB routines are loaded at run time
49 * and we do not link to a .LIB file.
51 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
54 # define Z_CALLCONV _stdcall
58 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
61 #include <openssl/dso.h>
63 /* Prototypes for built in stubs */
64 static int stub_compress(Bytef *dest,uLongf *destLen,
65 const Bytef *source, uLong sourceLen);
66 static int stub_inflateEnd(z_streamp strm);
67 static int stub_inflate(z_streamp strm, int flush);
68 static int stub_inflateInit_(z_streamp strm, const char * version,
71 /* Function pointers */
72 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
73 const Bytef *source, uLong sourceLen);
74 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
75 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
76 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
77 const char * version, int stream_size);
78 static compress_ft p_compress=NULL;
79 static inflateEnd_ft p_inflateEnd=NULL;
80 static inflate_ft p_inflate=NULL;
81 static inflateInit__ft p_inflateInit_=NULL;
83 static int zlib_loaded = 0; /* only attempt to init func pts once */
84 static DSO *zlib_dso = NULL;
86 #define compress stub_compress
87 #define inflateEnd stub_inflateEnd
88 #define inflate stub_inflate
89 #define inflateInit_ stub_inflateInit_
90 #endif /* ZLIB_SHARED */
92 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
93 unsigned int olen, unsigned char *in, unsigned int ilen)
103 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
115 memcpy(&(out[1]),in,ilen);
119 fprintf(stderr,"compress(%4d)->%4d %s\n",
120 ilen,(int)l,(clear)?"clear":"zlib");
125 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
126 unsigned int olen, unsigned char *in, unsigned int ilen)
134 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
140 memcpy(out,&(in[1]),ilen-1);
144 fprintf(stderr,"expand (%4d)->%4d %s\n",
145 ilen,(int)l,in[0]?"zlib":"clear");
150 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
156 stream.next_in = (Bytef*)source;
157 stream.avail_in = (uInt)sourceLen;
158 /* Check for source > 64K on 16-bit machine: */
159 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
161 stream.next_out = dest;
162 stream.avail_out = (uInt)*destLen;
163 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
165 stream.zalloc = (alloc_func)0;
166 stream.zfree = (free_func)0;
168 err = inflateInit(&stream);
169 if (err != Z_OK) return err;
171 err = inflate(&stream, Z_FINISH);
172 if (err != Z_STREAM_END) {
176 *destLen = stream.total_out;
178 err = inflateEnd(&stream);
184 COMP_METHOD *COMP_zlib(void)
186 COMP_METHOD *meth = &zlib_method_nozlib;
191 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
192 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
194 zlib_dso = DSO_load(NULL, "z", NULL, 0);
196 if (zlib_dso != NULL)
199 = (compress_ft) DSO_bind_func(zlib_dso,
202 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
205 = (inflate_ft) DSO_bind_func(zlib_dso,
208 = (inflateInit__ft) DSO_bind_func(zlib_dso,
223 /* Stubs for each function to be dynamicly loaded */
225 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
228 return(p_compress(dest,destLen,source,sourceLen));
234 stub_inflateEnd(z_streamp strm)
237 return(p_inflateEnd(strm));
243 stub_inflate(z_streamp strm, int flush)
246 return(p_inflate(strm,flush));
252 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
254 if ( p_inflateInit_ )
255 return(p_inflateInit_(strm,version,stream_size));
260 #endif /* ZLIB_SHARED */