4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
7 COMP_METHOD *COMP_zlib(void );
9 static COMP_METHOD zlib_method_nozlib={
23 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
24 unsigned int olen, unsigned char *in, unsigned int ilen);
25 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
26 unsigned int olen, unsigned char *in, unsigned int ilen);
28 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
31 static COMP_METHOD zlib_method={
42 * When OpenSSL is built on Windows, we do not want to require that
43 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
44 * work. Therefore, all ZLIB routines are loaded at run time
45 * and we do not link to a .LIB file.
47 #if defined(WINDOWS) || defined(WIN32)
50 /* Prototypes for built in stubs */
51 int stub_compress(Bytef *dest,uLongf *destLen,
52 const Bytef *source, uLong sourceLen);
53 int stub_inflateEnd(z_streamp strm);
54 int stub_inflate(z_streamp strm, int flush);
55 int stub_inflateInit_(z_streamp strm, const char * version, int stream_size);
57 /* Function pointers */
58 typedef int (_stdcall *compress_ft)(Bytef *dest,uLongf *destLen,
59 const Bytef *source, uLong sourceLen);
60 typedef int (_stdcall *inflateEnd_ft)(z_streamp strm);
61 typedef int (_stdcall *inflate_ft)(z_streamp strm, int flush);
62 typedef int (_stdcall *inflateInit__ft)(z_streamp strm, const char * version,
64 static compress_ft p_compress=NULL;
65 static inflateEnd_ft p_inflateEnd=NULL;
66 static inflate_ft p_inflate=NULL;
67 static inflate_Init__ft p_inflateInit_=NULL;
69 static int zlib_loaded = 0; /* only attempt to init func pts once */
71 #define compress stub_compress
72 #define inflateEnd stub_inflateEnd
73 #define inflate stub_inflate
74 #define inflateInit_ stub_inflateInit_
75 #endif /* WINDOWS || WIN32 */
77 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
78 unsigned int olen, unsigned char *in, unsigned int ilen)
88 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
100 memcpy(&(out[1]),in,ilen);
104 fprintf(stderr,"compress(%4d)->%4d %s\n",
105 ilen,(int)l,(clear)?"clear":"zlib");
110 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
111 unsigned int olen, unsigned char *in, unsigned int ilen)
119 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
125 memcpy(out,&(in[1]),ilen-1);
129 fprintf(stderr,"expand (%4d)->%4d %s\n",
130 ilen,(int)l,in[0]?"zlib":"clear");
135 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
141 stream.next_in = (Bytef*)source;
142 stream.avail_in = (uInt)sourceLen;
143 /* Check for source > 64K on 16-bit machine: */
144 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
146 stream.next_out = dest;
147 stream.avail_out = (uInt)*destLen;
148 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
150 stream.zalloc = (alloc_func)0;
151 stream.zfree = (free_func)0;
153 err = inflateInit(&stream);
154 if (err != Z_OK) return err;
156 err = inflate(&stream, Z_FINISH);
157 if (err != Z_STREAM_END) {
161 *destLen = stream.total_out;
163 err = inflateEnd(&stream);
169 COMP_METHOD *COMP_zlib(void)
171 COMP_METHOD *meth = &zlib_method_nozlib;
174 #if defined(WINDOWS) || defined(WIN32)
177 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
181 = (compress_ft) DSO_bind_func(dso, "compress");
182 (FARPROC) p_inflateEnd
183 = (inflateEnd_ft) DSO_bind_func(dso, "inflateEnd");
185 = (inflate_ft) DSO_bind_func(dso, "inflate");
186 (FARPROC) p_inflateInit_
187 = (inflateInit__ft) DSO_bind_func(dso, "inflateInit_");
202 #if defined(WINDOWS) || defined(WIN32)
203 /* Stubs for each function to be dynamicly loaded */
205 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
208 return(p_compress(dest,destLen,source,sourceLen));
214 stub_inflateEnd(z_streamp strm)
217 return(p_inflateEnd(strm));
223 stub_inflate(z_streamp strm, int flush)
226 return(p_inflate(strm,flush));
232 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
234 if ( p_inflateInit_ )
235 return(p_inflateInit_(strm,version,stream_size));
240 #endif /* WINDOWS || WIN32 */