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_stateful_init(COMP_CTX *ctx);
28 static void zlib_stateful_finish(COMP_CTX *ctx);
29 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
30 unsigned int olen, unsigned char *in, unsigned int ilen);
31 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
32 unsigned int olen, unsigned char *in, unsigned int ilen);
35 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
36 unsigned int olen, unsigned char *in, unsigned int ilen);
37 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
38 unsigned int olen, unsigned char *in, unsigned int ilen);
40 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
43 static COMP_METHOD zlib_stateless_method={
55 static COMP_METHOD zlib_stateful_method={
60 zlib_stateful_compress_block,
61 zlib_stateful_expand_block,
67 * When OpenSSL is built on Windows, we do not want to require that
68 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
69 * work. Therefore, all ZLIB routines are loaded at run time
70 * and we do not link to a .LIB file.
72 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
75 # define Z_CALLCONV _stdcall
81 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
84 #include <openssl/dso.h>
86 /* Prototypes for built in stubs */
88 static int stub_compress(Bytef *dest,uLongf *destLen,
89 const Bytef *source, uLong sourceLen);
91 static int stub_inflateEnd(z_streamp strm);
92 static int stub_inflate(z_streamp strm, int flush);
93 static int stub_inflateInit_(z_streamp strm, const char * version,
95 static int stub_deflateEnd(z_streamp strm);
96 static int stub_deflate(z_streamp strm, int flush);
97 static int stub_deflateInit_(z_streamp strm, int level,
98 const char * version, int stream_size);
100 /* Function pointers */
101 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
102 const Bytef *source, uLong sourceLen);
103 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
104 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
105 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
106 const char * version, int stream_size);
107 typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
108 typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
109 typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
110 const char * version, int stream_size);
111 static compress_ft p_compress=NULL;
112 static inflateEnd_ft p_inflateEnd=NULL;
113 static inflate_ft p_inflate=NULL;
114 static inflateInit__ft p_inflateInit_=NULL;
115 static deflateEnd_ft p_deflateEnd=NULL;
116 static deflate_ft p_deflate=NULL;
117 static deflateInit__ft p_deflateInit_=NULL;
119 static int zlib_loaded = 0; /* only attempt to init func pts once */
120 static DSO *zlib_dso = NULL;
122 #define compress stub_compress
123 #define inflateEnd stub_inflateEnd
124 #define inflate stub_inflate
125 #define inflateInit_ stub_inflateInit_
126 #define deflateEnd stub_deflateEnd
127 #define deflate stub_deflate
128 #define deflateInit_ stub_deflateInit_
129 #endif /* ZLIB_SHARED */
137 static int zlib_stateful_ex_idx = -1;
139 static void zlib_stateful_free_ex_data(void *obj, void *item,
140 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
142 struct zlib_state *state = (struct zlib_state *)item;
143 inflateEnd(&state->istream);
144 deflateEnd(&state->ostream);
148 static int zlib_stateful_init(COMP_CTX *ctx)
151 struct zlib_state *state =
152 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
157 state->istream.zalloc = Z_NULL;
158 state->istream.zfree = Z_NULL;
159 state->istream.opaque = Z_NULL;
160 state->istream.next_in = Z_NULL;
161 state->istream.next_out = Z_NULL;
162 state->istream.avail_in = 0;
163 state->istream.avail_out = 0;
164 err = inflateInit_(&state->istream,
165 ZLIB_VERSION, sizeof(z_stream));
169 state->ostream.zalloc = Z_NULL;
170 state->ostream.zfree = Z_NULL;
171 state->ostream.opaque = Z_NULL;
172 state->ostream.next_in = Z_NULL;
173 state->ostream.next_out = Z_NULL;
174 state->ostream.avail_in = 0;
175 state->ostream.avail_out = 0;
176 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
177 ZLIB_VERSION, sizeof(z_stream));
181 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
182 if (zlib_stateful_ex_idx == -1)
184 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
185 if (zlib_stateful_ex_idx == -1)
186 zlib_stateful_ex_idx =
187 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
188 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
189 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
190 if (zlib_stateful_ex_idx == -1)
193 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
196 if (state) OPENSSL_free(state);
200 static void zlib_stateful_finish(COMP_CTX *ctx)
202 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
205 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
206 unsigned int olen, unsigned char *in, unsigned int ilen)
209 struct zlib_state *state =
210 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
211 zlib_stateful_ex_idx);
216 state->ostream.next_in = in;
217 state->ostream.avail_in = ilen;
218 state->ostream.next_out = out;
219 state->ostream.avail_out = olen;
221 err = deflate(&state->ostream, Z_SYNC_FLUSH);
225 fprintf(stderr,"compress(%4d)->%4d %s\n",
226 ilen,olen - state->ostream.avail_out,
227 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
229 return olen - state->ostream.avail_out;
232 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
233 unsigned int olen, unsigned char *in, unsigned int ilen)
237 struct zlib_state *state =
238 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
239 zlib_stateful_ex_idx);
244 state->istream.next_in = in;
245 state->istream.avail_in = ilen;
246 state->istream.next_out = out;
247 state->istream.avail_out = olen;
249 err = inflate(&state->istream, Z_SYNC_FLUSH);
253 fprintf(stderr,"expand(%4d)->%4d %s\n",
254 ilen,olen - state->istream.avail_out,
255 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
257 return olen - state->istream.avail_out;
261 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
262 unsigned int olen, unsigned char *in, unsigned int ilen)
272 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
284 memcpy(&(out[1]),in,ilen);
288 fprintf(stderr,"compress(%4d)->%4d %s\n",
289 ilen,(int)l,(clear)?"clear":"zlib");
294 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
295 unsigned int olen, unsigned char *in, unsigned int ilen)
303 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
309 memcpy(out,&(in[1]),ilen-1);
313 fprintf(stderr,"expand (%4d)->%4d %s\n",
314 ilen,(int)l,in[0]?"zlib":"clear");
319 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
325 stream.next_in = (Bytef*)source;
326 stream.avail_in = (uInt)sourceLen;
327 /* Check for source > 64K on 16-bit machine: */
328 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
330 stream.next_out = dest;
331 stream.avail_out = (uInt)*destLen;
332 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
334 stream.zalloc = (alloc_func)0;
335 stream.zfree = (free_func)0;
337 err = inflateInit_(&stream,
338 ZLIB_VERSION, sizeof(z_stream));
339 if (err != Z_OK) return err;
341 err = inflate(&stream, Z_FINISH);
342 if (err != Z_STREAM_END) {
346 *destLen = stream.total_out;
348 err = inflateEnd(&stream);
355 COMP_METHOD *COMP_zlib(void)
357 COMP_METHOD *meth = &zlib_method_nozlib;
362 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
363 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
366 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
369 /* Clear the errors from the first failed
375 zlib_dso = DSO_load(NULL, "z", NULL, 0);
377 if (zlib_dso != NULL)
380 = (compress_ft) DSO_bind_func(zlib_dso,
383 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
386 = (inflate_ft) DSO_bind_func(zlib_dso,
389 = (inflateInit__ft) DSO_bind_func(zlib_dso,
392 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
395 = (deflate_ft) DSO_bind_func(zlib_dso,
398 = (deflateInit__ft) DSO_bind_func(zlib_dso,
405 #if defined(ZLIB) || defined(ZLIB_SHARED)
406 meth = &zlib_stateful_method;
414 /* Stubs for each function to be dynamicly loaded */
416 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
419 return(p_compress(dest,destLen,source,sourceLen));
426 stub_inflateEnd(z_streamp strm)
429 return(p_inflateEnd(strm));
435 stub_inflate(z_streamp strm, int flush)
438 return(p_inflate(strm,flush));
444 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
446 if ( p_inflateInit_ )
447 return(p_inflateInit_(strm,version,stream_size));
453 stub_deflateEnd(z_streamp strm)
456 return(p_deflateEnd(strm));
462 stub_deflate(z_streamp strm, int flush)
465 return(p_deflate(strm,flush));
471 stub_deflateInit_(z_streamp strm, int level,
472 const char * version, int stream_size)
474 if ( p_deflateInit_ )
475 return(p_deflateInit_(strm,level,version,stream_size));
480 #endif /* ZLIB_SHARED */