4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
7 COMP_METHOD *COMP_zlib(void );
9 static COMP_METHOD zlib_method_nozlib={
25 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
26 unsigned int olen, unsigned char *in, unsigned int ilen);
27 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
28 unsigned int olen, unsigned char *in, unsigned int ilen);
30 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
33 static COMP_METHOD zlib_method={
44 * When OpenSSL is built on Windows, we do not want to require that
45 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
46 * work. Therefore, all ZLIB routines are loaded at run time
47 * and we do not link to a .LIB file.
49 #if defined(WINDOWS) || defined(WIN32)
52 # define Z_CALLCONV _stcall
56 #endif /* !(WINDOWS || WIN32) */
59 #include <openssl/dso.h>
61 /* Prototypes for built in stubs */
62 static int stub_compress(Bytef *dest,uLongf *destLen,
63 const Bytef *source, uLong sourceLen);
64 static int stub_inflateEnd(z_streamp strm);
65 static int stub_inflate(z_streamp strm, int flush);
66 static int stub_inflateInit_(z_streamp strm, const char * version,
69 /* Function pointers */
70 typedef int Z_CALLCONV (*compress_ft)(Bytef *dest,uLongf *destLen,
71 const Bytef *source, uLong sourceLen);
72 typedef int Z_CALLCONV (*inflateEnd_ft)(z_streamp strm);
73 typedef int Z_CALLCONV (*inflate_ft)(z_streamp strm, int flush);
74 typedef int Z_CALLCONV (*inflateInit__ft)(z_streamp strm,
75 const char * version, int stream_size);
76 static compress_ft p_compress=NULL;
77 static inflateEnd_ft p_inflateEnd=NULL;
78 static inflate_ft p_inflate=NULL;
79 static inflateInit__ft p_inflateInit_=NULL;
81 static int zlib_loaded = 0; /* only attempt to init func pts once */
82 static DSO *zlib_dso = NULL;
84 #define compress stub_compress
85 #define inflateEnd stub_inflateEnd
86 #define inflate stub_inflate
87 #define inflateInit_ stub_inflateInit_
88 #endif /* ZLIB_SHARED */
90 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
91 unsigned int olen, unsigned char *in, unsigned int ilen)
101 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
113 memcpy(&(out[1]),in,ilen);
117 fprintf(stderr,"compress(%4d)->%4d %s\n",
118 ilen,(int)l,(clear)?"clear":"zlib");
123 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
124 unsigned int olen, unsigned char *in, unsigned int ilen)
132 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
138 memcpy(out,&(in[1]),ilen-1);
142 fprintf(stderr,"expand (%4d)->%4d %s\n",
143 ilen,(int)l,in[0]?"zlib":"clear");
148 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
154 stream.next_in = (Bytef*)source;
155 stream.avail_in = (uInt)sourceLen;
156 /* Check for source > 64K on 16-bit machine: */
157 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
159 stream.next_out = dest;
160 stream.avail_out = (uInt)*destLen;
161 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
163 stream.zalloc = (alloc_func)0;
164 stream.zfree = (free_func)0;
166 err = inflateInit(&stream);
167 if (err != Z_OK) return err;
169 err = inflate(&stream, Z_FINISH);
170 if (err != Z_STREAM_END) {
174 *destLen = stream.total_out;
176 err = inflateEnd(&stream);
182 COMP_METHOD *COMP_zlib(void)
184 COMP_METHOD *meth = &zlib_method_nozlib;
189 #if defined(WINDOWS) || defined(WIN32)
190 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
192 zlib_dso = DSO_load(NULL, "z", NULL, 0);
194 if (zlib_dso != NULL)
197 = (compress_ft) DSO_bind_func(zlib_dso,
200 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
203 = (inflate_ft) DSO_bind_func(zlib_dso,
206 = (inflateInit__ft) DSO_bind_func(zlib_dso,
221 /* Stubs for each function to be dynamicly loaded */
223 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
226 return(p_compress(dest,destLen,source,sourceLen));
232 stub_inflateEnd(z_streamp strm)
235 return(p_inflateEnd(strm));
241 stub_inflate(z_streamp strm, int flush)
244 return(p_inflate(strm,flush));
250 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
252 if ( p_inflateInit_ )
253 return(p_inflateInit_(strm,version,stream_size));
258 #endif /* ZLIB_SHARED */