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 /* memory allocations functions for zlib intialization */
36 static void* zlib_zalloc(void* opaque, unsigned int no, unsigned int size)
40 p=OPENSSL_malloc(no*size);
42 memset(p, 0, no*size);
47 static void zlib_zfree(void* opaque, void* address)
49 OPENSSL_free(address);
53 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
54 unsigned int olen, unsigned char *in, unsigned int ilen);
55 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
56 unsigned int olen, unsigned char *in, unsigned int ilen);
58 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
61 static COMP_METHOD zlib_stateless_method={
73 static COMP_METHOD zlib_stateful_method={
78 zlib_stateful_compress_block,
79 zlib_stateful_expand_block,
85 * When OpenSSL is built on Windows, we do not want to require that
86 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
87 * work. Therefore, all ZLIB routines are loaded at run time
88 * and we do not link to a .LIB file when ZLIB_SHARED is set.
90 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
92 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
95 #include <openssl/dso.h>
97 /* Function pointers */
98 typedef int (*compress_ft)(Bytef *dest,uLongf *destLen,
99 const Bytef *source, uLong sourceLen);
100 typedef int (*inflateEnd_ft)(z_streamp strm);
101 typedef int (*inflate_ft)(z_streamp strm, int flush);
102 typedef int (*inflateInit__ft)(z_streamp strm,
103 const char * version, int stream_size);
104 typedef int (*deflateEnd_ft)(z_streamp strm);
105 typedef int (*deflate_ft)(z_streamp strm, int flush);
106 typedef int (*deflateInit__ft)(z_streamp strm, int level,
107 const char * version, int stream_size);
108 static compress_ft p_compress=NULL;
109 static inflateEnd_ft p_inflateEnd=NULL;
110 static inflate_ft p_inflate=NULL;
111 static inflateInit__ft p_inflateInit_=NULL;
112 static deflateEnd_ft p_deflateEnd=NULL;
113 static deflate_ft p_deflate=NULL;
114 static deflateInit__ft p_deflateInit_=NULL;
116 static int zlib_loaded = 0; /* only attempt to init func pts once */
117 static DSO *zlib_dso = NULL;
119 #define compress p_compress
120 #define inflateEnd p_inflateEnd
121 #define inflate p_inflate
122 #define inflateInit_ p_inflateInit_
123 #define deflateEnd p_deflateEnd
124 #define deflate p_deflate
125 #define deflateInit_ p_deflateInit_
126 #endif /* ZLIB_SHARED */
134 static int zlib_stateful_ex_idx = -1;
136 static void zlib_stateful_free_ex_data(void *obj, void *item,
137 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
139 struct zlib_state *state = (struct zlib_state *)item;
140 inflateEnd(&state->istream);
141 deflateEnd(&state->ostream);
145 static int zlib_stateful_init(COMP_CTX *ctx)
148 struct zlib_state *state =
149 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
154 state->istream.zalloc = zlib_zalloc;
155 state->istream.zfree = zlib_zfree;
156 state->istream.opaque = Z_NULL;
157 state->istream.next_in = Z_NULL;
158 state->istream.next_out = Z_NULL;
159 state->istream.avail_in = 0;
160 state->istream.avail_out = 0;
161 err = inflateInit_(&state->istream,
162 ZLIB_VERSION, sizeof(z_stream));
166 state->ostream.zalloc = zlib_zalloc;
167 state->ostream.zfree = zlib_zfree;
168 state->ostream.opaque = Z_NULL;
169 state->ostream.next_in = Z_NULL;
170 state->ostream.next_out = Z_NULL;
171 state->ostream.avail_in = 0;
172 state->ostream.avail_out = 0;
173 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
174 ZLIB_VERSION, sizeof(z_stream));
178 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
179 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
182 if (state) OPENSSL_free(state);
186 static void zlib_stateful_finish(COMP_CTX *ctx)
188 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
191 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
192 unsigned int olen, unsigned char *in, unsigned int ilen)
195 struct zlib_state *state =
196 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
197 zlib_stateful_ex_idx);
202 state->ostream.next_in = in;
203 state->ostream.avail_in = ilen;
204 state->ostream.next_out = out;
205 state->ostream.avail_out = olen;
207 err = deflate(&state->ostream, Z_SYNC_FLUSH);
211 fprintf(stderr,"compress(%4d)->%4d %s\n",
212 ilen,olen - state->ostream.avail_out,
213 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
215 return olen - state->ostream.avail_out;
218 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
219 unsigned int olen, unsigned char *in, unsigned int ilen)
223 struct zlib_state *state =
224 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
225 zlib_stateful_ex_idx);
230 state->istream.next_in = in;
231 state->istream.avail_in = ilen;
232 state->istream.next_out = out;
233 state->istream.avail_out = olen;
235 err = inflate(&state->istream, Z_SYNC_FLUSH);
239 fprintf(stderr,"expand(%4d)->%4d %s\n",
240 ilen,olen - state->istream.avail_out,
241 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
243 return olen - state->istream.avail_out;
247 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
248 unsigned int olen, unsigned char *in, unsigned int ilen)
258 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
270 memcpy(&(out[1]),in,ilen);
274 fprintf(stderr,"compress(%4d)->%4d %s\n",
275 ilen,(int)l,(clear)?"clear":"zlib");
280 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
281 unsigned int olen, unsigned char *in, unsigned int ilen)
289 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
295 memcpy(out,&(in[1]),ilen-1);
299 fprintf(stderr,"expand (%4d)->%4d %s\n",
300 ilen,(int)l,in[0]?"zlib":"clear");
305 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
311 stream.next_in = (Bytef*)source;
312 stream.avail_in = (uInt)sourceLen;
313 /* Check for source > 64K on 16-bit machine: */
314 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
316 stream.next_out = dest;
317 stream.avail_out = (uInt)*destLen;
318 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
320 stream.zalloc = (alloc_func)0;
321 stream.zfree = (free_func)0;
323 err = inflateInit_(&stream,
324 ZLIB_VERSION, sizeof(z_stream));
325 if (err != Z_OK) return err;
327 err = inflate(&stream, Z_FINISH);
328 if (err != Z_STREAM_END) {
332 *destLen = stream.total_out;
334 err = inflateEnd(&stream);
341 COMP_METHOD *COMP_zlib(void)
343 COMP_METHOD *meth = &zlib_method_nozlib;
348 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
349 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
351 zlib_dso = DSO_load(NULL, "z", NULL, 0);
353 if (zlib_dso != NULL)
356 = (compress_ft) DSO_bind_func(zlib_dso,
359 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
362 = (inflate_ft) DSO_bind_func(zlib_dso,
365 = (inflateInit__ft) DSO_bind_func(zlib_dso,
368 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
371 = (deflate_ft) DSO_bind_func(zlib_dso,
374 = (deflateInit__ft) DSO_bind_func(zlib_dso,
377 if (p_compress && p_inflateEnd && p_inflate
378 && p_inflateInit_ && p_deflateEnd
379 && p_deflate && p_deflateInit_)
388 #if defined(ZLIB) || defined(ZLIB_SHARED)
390 /* init zlib_stateful_ex_idx here so that in a multi-process
391 * application it's enough to intialize openssl before forking
392 * (idx will be inherited in all the children) */
393 if (zlib_stateful_ex_idx == -1)
395 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
396 if (zlib_stateful_ex_idx == -1)
397 zlib_stateful_ex_idx =
398 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
399 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
400 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
401 if (zlib_stateful_ex_idx == -1)
405 meth = &zlib_stateful_method;