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
79 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
82 #include <openssl/dso.h>
84 /* Prototypes for built in stubs */
86 static int stub_compress(Bytef *dest,uLongf *destLen,
87 const Bytef *source, uLong sourceLen);
89 static int stub_inflateEnd(z_streamp strm);
90 static int stub_inflate(z_streamp strm, int flush);
91 static int stub_inflateInit_(z_streamp strm, const char * version,
93 static int stub_deflateEnd(z_streamp strm);
94 static int stub_deflate(z_streamp strm, int flush);
95 static int stub_deflateInit_(z_streamp strm, int level,
96 const char * version, int stream_size);
98 /* Function pointers */
99 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
100 const Bytef *source, uLong sourceLen);
101 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
102 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
103 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
104 const char * version, int stream_size);
105 typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
106 typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
107 typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
108 const char * version, int stream_size);
109 static compress_ft p_compress=NULL;
110 static inflateEnd_ft p_inflateEnd=NULL;
111 static inflate_ft p_inflate=NULL;
112 static inflateInit__ft p_inflateInit_=NULL;
113 static deflateEnd_ft p_deflateEnd=NULL;
114 static deflate_ft p_deflate=NULL;
115 static deflateInit__ft p_deflateInit_=NULL;
117 static int zlib_loaded = 0; /* only attempt to init func pts once */
118 static DSO *zlib_dso = NULL;
120 #define compress stub_compress
121 #define inflateEnd stub_inflateEnd
122 #define inflate stub_inflate
123 #define inflateInit_ stub_inflateInit_
124 #define deflateEnd stub_deflateEnd
125 #define deflate stub_deflate
126 #define deflateInit_ stub_deflateInit_
127 #endif /* ZLIB_SHARED */
135 static int zlib_stateful_ex_idx = -1;
137 static void zlib_stateful_free_ex_data(void *obj, void *item,
138 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
140 struct zlib_state *state = (struct zlib_state *)item;
141 inflateEnd(&state->istream);
142 deflateEnd(&state->ostream);
146 static int zlib_stateful_init(COMP_CTX *ctx)
149 struct zlib_state *state =
150 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
155 state->istream.zalloc = Z_NULL;
156 state->istream.zfree = Z_NULL;
157 state->istream.opaque = Z_NULL;
158 state->istream.next_in = Z_NULL;
159 state->istream.next_out = Z_NULL;
160 state->istream.avail_in = 0;
161 state->istream.avail_out = 0;
162 err = inflateInit_(&state->istream,
163 ZLIB_VERSION, sizeof(z_stream));
167 state->ostream.zalloc = Z_NULL;
168 state->ostream.zfree = Z_NULL;
169 state->ostream.opaque = Z_NULL;
170 state->ostream.next_in = Z_NULL;
171 state->ostream.next_out = Z_NULL;
172 state->ostream.avail_in = 0;
173 state->ostream.avail_out = 0;
174 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
175 ZLIB_VERSION, sizeof(z_stream));
179 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
180 if (zlib_stateful_ex_idx == -1)
182 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
183 if (zlib_stateful_ex_idx == -1)
184 zlib_stateful_ex_idx =
185 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
186 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
187 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
188 if (zlib_stateful_ex_idx == -1)
191 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
194 if (state) OPENSSL_free(state);
198 static void zlib_stateful_finish(COMP_CTX *ctx)
200 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
203 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
204 unsigned int olen, unsigned char *in, unsigned int ilen)
207 struct zlib_state *state =
208 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
209 zlib_stateful_ex_idx);
214 state->ostream.next_in = in;
215 state->ostream.avail_in = ilen;
216 state->ostream.next_out = out;
217 state->ostream.avail_out = olen;
219 err = deflate(&state->ostream, Z_SYNC_FLUSH);
223 fprintf(stderr,"compress(%4d)->%4d %s\n",
224 ilen,olen - state->ostream.avail_out,
225 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
227 return olen - state->ostream.avail_out;
230 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
231 unsigned int olen, unsigned char *in, unsigned int ilen)
235 struct zlib_state *state =
236 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
237 zlib_stateful_ex_idx);
242 state->istream.next_in = in;
243 state->istream.avail_in = ilen;
244 state->istream.next_out = out;
245 state->istream.avail_out = olen;
247 err = inflate(&state->istream, Z_SYNC_FLUSH);
251 fprintf(stderr,"expand(%4d)->%4d %s\n",
252 ilen,olen - state->istream.avail_out,
253 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
255 return olen - state->istream.avail_out;
259 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
260 unsigned int olen, unsigned char *in, unsigned int ilen)
270 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
282 memcpy(&(out[1]),in,ilen);
286 fprintf(stderr,"compress(%4d)->%4d %s\n",
287 ilen,(int)l,(clear)?"clear":"zlib");
292 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
293 unsigned int olen, unsigned char *in, unsigned int ilen)
301 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
307 memcpy(out,&(in[1]),ilen-1);
311 fprintf(stderr,"expand (%4d)->%4d %s\n",
312 ilen,(int)l,in[0]?"zlib":"clear");
317 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
323 stream.next_in = (Bytef*)source;
324 stream.avail_in = (uInt)sourceLen;
325 /* Check for source > 64K on 16-bit machine: */
326 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
328 stream.next_out = dest;
329 stream.avail_out = (uInt)*destLen;
330 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
332 stream.zalloc = (alloc_func)0;
333 stream.zfree = (free_func)0;
335 err = inflateInit_(&stream,
336 ZLIB_VERSION, sizeof(z_stream));
337 if (err != Z_OK) return err;
339 err = inflate(&stream, Z_FINISH);
340 if (err != Z_STREAM_END) {
344 *destLen = stream.total_out;
346 err = inflateEnd(&stream);
353 COMP_METHOD *COMP_zlib(void)
355 COMP_METHOD *meth = &zlib_method_nozlib;
360 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
361 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
364 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
367 /* Clear the errors from the first failed
373 zlib_dso = DSO_load(NULL, "z", NULL, 0);
375 if (zlib_dso != NULL)
378 = (compress_ft) DSO_bind_func(zlib_dso,
381 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
384 = (inflate_ft) DSO_bind_func(zlib_dso,
387 = (inflateInit__ft) DSO_bind_func(zlib_dso,
390 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
393 = (deflate_ft) DSO_bind_func(zlib_dso,
396 = (deflateInit__ft) DSO_bind_func(zlib_dso,
403 #if defined(ZLIB) || defined(ZLIB_SHARED)
404 meth = &zlib_stateful_method;
412 /* Stubs for each function to be dynamicly loaded */
414 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
417 return(p_compress(dest,destLen,source,sourceLen));
424 stub_inflateEnd(z_streamp strm)
427 return(p_inflateEnd(strm));
433 stub_inflate(z_streamp strm, int flush)
436 return(p_inflate(strm,flush));
442 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
444 if ( p_inflateInit_ )
445 return(p_inflateInit_(strm,version,stream_size));
451 stub_deflateEnd(z_streamp strm)
454 return(p_deflateEnd(strm));
460 stub_deflate(z_streamp strm, int flush)
463 return(p_deflate(strm,flush));
469 stub_deflateInit_(z_streamp strm, int level,
470 const char * version, int stream_size)
472 if ( p_deflateInit_ )
473 return(p_deflateInit_(strm,level,version,stream_size));
478 #endif /* ZLIB_SHARED */