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,
32 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
33 unsigned int olen, unsigned char *in,
36 /* memory allocations functions for zlib intialization */
37 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
41 p = OPENSSL_malloc(no * size);
43 memset(p, 0, no * size);
47 static void zlib_zfree(void *opaque, void *address)
49 OPENSSL_free(address);
53 static COMP_METHOD zlib_stateful_method = {
58 zlib_stateful_compress_block,
59 zlib_stateful_expand_block,
65 * When OpenSSL is built on Windows, we do not want to require that
66 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
67 * work. Therefore, all ZLIB routines are loaded at run time
68 * and we do not link to a .LIB file when ZLIB_SHARED is set.
70 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
72 # endif /* !(OPENSSL_SYS_WINDOWS ||
73 * OPENSSL_SYS_WIN32) */
76 # include <openssl/dso.h>
78 /* Function pointers */
79 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
80 const Bytef *source, uLong sourceLen);
81 typedef int (*inflateEnd_ft) (z_streamp strm);
82 typedef int (*inflate_ft) (z_streamp strm, int flush);
83 typedef int (*inflateInit__ft) (z_streamp strm,
84 const char *version, int stream_size);
85 typedef int (*deflateEnd_ft) (z_streamp strm);
86 typedef int (*deflate_ft) (z_streamp strm, int flush);
87 typedef int (*deflateInit__ft) (z_streamp strm, int level,
88 const char *version, int stream_size);
89 typedef const char *(*zError__ft) (int err);
90 static compress_ft p_compress = NULL;
91 static inflateEnd_ft p_inflateEnd = NULL;
92 static inflate_ft p_inflate = NULL;
93 static inflateInit__ft p_inflateInit_ = NULL;
94 static deflateEnd_ft p_deflateEnd = NULL;
95 static deflate_ft p_deflate = NULL;
96 static deflateInit__ft p_deflateInit_ = NULL;
97 static zError__ft p_zError = NULL;
99 static int zlib_loaded = 0; /* only attempt to init func pts once */
100 static DSO *zlib_dso = NULL;
102 # define compress p_compress
103 # define inflateEnd p_inflateEnd
104 # define inflate p_inflate
105 # define inflateInit_ p_inflateInit_
106 # define deflateEnd p_deflateEnd
107 # define deflate p_deflate
108 # define deflateInit_ p_deflateInit_
109 # define zError p_zError
110 # endif /* ZLIB_SHARED */
117 static int zlib_stateful_ex_idx = -1;
119 static int zlib_stateful_init(COMP_CTX *ctx)
122 struct zlib_state *state = OPENSSL_malloc(sizeof(struct zlib_state));
127 state->istream.zalloc = zlib_zalloc;
128 state->istream.zfree = zlib_zfree;
129 state->istream.opaque = Z_NULL;
130 state->istream.next_in = Z_NULL;
131 state->istream.next_out = Z_NULL;
132 state->istream.avail_in = 0;
133 state->istream.avail_out = 0;
134 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
138 state->ostream.zalloc = zlib_zalloc;
139 state->ostream.zfree = zlib_zfree;
140 state->ostream.opaque = Z_NULL;
141 state->ostream.next_in = Z_NULL;
142 state->ostream.next_out = Z_NULL;
143 state->ostream.avail_in = 0;
144 state->ostream.avail_out = 0;
145 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
146 ZLIB_VERSION, sizeof(z_stream));
150 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
151 CRYPTO_set_ex_data(&ctx->ex_data, zlib_stateful_ex_idx, state);
158 static void zlib_stateful_finish(COMP_CTX *ctx)
160 struct zlib_state *state =
161 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
162 zlib_stateful_ex_idx);
163 inflateEnd(&state->istream);
164 deflateEnd(&state->ostream);
166 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
169 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
170 unsigned int olen, unsigned char *in,
174 struct zlib_state *state =
175 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
176 zlib_stateful_ex_idx);
181 state->ostream.next_in = in;
182 state->ostream.avail_in = ilen;
183 state->ostream.next_out = out;
184 state->ostream.avail_out = olen;
186 err = deflate(&state->ostream, Z_SYNC_FLUSH);
190 fprintf(stderr, "compress(%4d)->%4d %s\n",
191 ilen, olen - state->ostream.avail_out,
192 (ilen != olen - state->ostream.avail_out) ? "zlib" : "clear");
194 return olen - state->ostream.avail_out;
197 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
198 unsigned int olen, unsigned char *in,
203 struct zlib_state *state =
204 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
205 zlib_stateful_ex_idx);
210 state->istream.next_in = in;
211 state->istream.avail_in = ilen;
212 state->istream.next_out = out;
213 state->istream.avail_out = olen;
215 err = inflate(&state->istream, Z_SYNC_FLUSH);
219 fprintf(stderr, "expand(%4d)->%4d %s\n",
220 ilen, olen - state->istream.avail_out,
221 (ilen != olen - state->istream.avail_out) ? "zlib" : "clear");
223 return olen - state->istream.avail_out;
228 COMP_METHOD *COMP_zlib(void)
230 COMP_METHOD *meth = &zlib_method_nozlib;
234 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
235 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
237 zlib_dso = DSO_load(NULL, "z", NULL, 0);
239 if (zlib_dso != NULL) {
240 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
242 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
243 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
245 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
247 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
248 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
250 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
251 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
253 if (p_compress && p_inflateEnd && p_inflate
254 && p_inflateInit_ && p_deflateEnd
255 && p_deflate && p_deflateInit_ && p_zError)
263 #if defined(ZLIB) || defined(ZLIB_SHARED)
266 * init zlib_stateful_ex_idx here so that in a multi-process
267 * application it's enough to intialize openssl before forking (idx
268 * will be inherited in all the children)
270 if (zlib_stateful_ex_idx == -1) {
271 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
272 if (zlib_stateful_ex_idx == -1)
273 zlib_stateful_ex_idx =
274 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
275 0, NULL, NULL, NULL, NULL);
276 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
277 if (zlib_stateful_ex_idx == -1)
281 meth = &zlib_stateful_method;
289 void COMP_zlib_cleanup(void)
298 /* Zlib based compression/decompression filter BIO */
301 unsigned char *ibuf; /* Input buffer */
302 int ibufsize; /* Buffer size */
303 z_stream zin; /* Input decompress context */
304 unsigned char *obuf; /* Output buffer */
305 int obufsize; /* Output buffer size */
306 unsigned char *optr; /* Position in output buffer */
307 int ocount; /* Amount of data in output buffer */
308 int odone; /* deflate EOF */
309 int comp_level; /* Compression level to use */
310 z_stream zout; /* Output compression context */
313 # define ZLIB_DEFAULT_BUFSIZE 1024
315 static int bio_zlib_new(BIO *bi);
316 static int bio_zlib_free(BIO *bi);
317 static int bio_zlib_read(BIO *b, char *out, int outl);
318 static int bio_zlib_write(BIO *b, const char *in, int inl);
319 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
320 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
322 static BIO_METHOD bio_meth_zlib = {
332 bio_zlib_callback_ctrl
335 BIO_METHOD *BIO_f_zlib(void)
337 return &bio_meth_zlib;
340 static int bio_zlib_new(BIO *bi)
346 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
350 ctx = OPENSSL_malloc(sizeof(BIO_ZLIB_CTX));
352 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
357 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
358 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
359 ctx->zin.zalloc = Z_NULL;
360 ctx->zin.zfree = Z_NULL;
361 ctx->zin.next_in = NULL;
362 ctx->zin.avail_in = 0;
363 ctx->zin.next_out = NULL;
364 ctx->zin.avail_out = 0;
365 ctx->zout.zalloc = Z_NULL;
366 ctx->zout.zfree = Z_NULL;
367 ctx->zout.next_in = NULL;
368 ctx->zout.avail_in = 0;
369 ctx->zout.next_out = NULL;
370 ctx->zout.avail_out = 0;
372 ctx->comp_level = Z_DEFAULT_COMPRESSION;
374 bi->ptr = (char *)ctx;
379 static int bio_zlib_free(BIO *bi)
384 ctx = (BIO_ZLIB_CTX *) bi->ptr;
386 /* Destroy decompress context */
387 inflateEnd(&ctx->zin);
388 OPENSSL_free(ctx->ibuf);
391 /* Destroy compress context */
392 deflateEnd(&ctx->zout);
393 OPENSSL_free(ctx->obuf);
402 static int bio_zlib_read(BIO *b, char *out, int outl)
409 ctx = (BIO_ZLIB_CTX *) b->ptr;
411 BIO_clear_retry_flags(b);
413 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
415 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
419 zin->next_in = ctx->ibuf;
423 /* Copy output data directly to supplied buffer */
424 zin->next_out = (unsigned char *)out;
425 zin->avail_out = (unsigned int)outl;
427 /* Decompress while data available */
428 while (zin->avail_in) {
429 ret = inflate(zin, 0);
430 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
431 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
432 ERR_add_error_data(2, "zlib error:", zError(ret));
435 /* If EOF or we've read everything then return */
436 if ((ret == Z_STREAM_END) || !zin->avail_out)
437 return outl - zin->avail_out;
441 * No data in input buffer try to read some in, if an error then
442 * return the total data read.
444 ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
446 /* Total data read */
447 int tot = outl - zin->avail_out;
448 BIO_copy_next_retry(b);
450 return (tot > 0) ? tot : ret;
454 zin->next_in = ctx->ibuf;
458 static int bio_zlib_write(BIO *b, const char *in, int inl)
465 ctx = (BIO_ZLIB_CTX *) b->ptr;
469 BIO_clear_retry_flags(b);
471 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
472 /* Need error here */
474 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
477 ctx->optr = ctx->obuf;
479 deflateInit(zout, ctx->comp_level);
480 zout->next_out = ctx->obuf;
481 zout->avail_out = ctx->obufsize;
483 /* Obtain input data directly from supplied buffer */
484 zout->next_in = (void *)in;
485 zout->avail_in = inl;
487 /* If data in output buffer write it first */
488 while (ctx->ocount) {
489 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
491 /* Total data written */
492 int tot = inl - zout->avail_in;
493 BIO_copy_next_retry(b);
495 return (tot > 0) ? tot : ret;
502 /* Have we consumed all supplied data? */
506 /* Compress some more */
509 ctx->optr = ctx->obuf;
510 zout->next_out = ctx->obuf;
511 zout->avail_out = ctx->obufsize;
512 /* Compress some more */
513 ret = deflate(zout, 0);
515 COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
516 ERR_add_error_data(2, "zlib error:", zError(ret));
519 ctx->ocount = ctx->obufsize - zout->avail_out;
523 static int bio_zlib_flush(BIO *b)
528 ctx = (BIO_ZLIB_CTX *) b->ptr;
529 /* If no data written or already flush show success */
530 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
533 BIO_clear_retry_flags(b);
534 /* No more input data */
535 zout->next_in = NULL;
538 /* If data in output buffer write it first */
539 while (ctx->ocount) {
540 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
542 BIO_copy_next_retry(b);
551 /* Compress some more */
554 ctx->optr = ctx->obuf;
555 zout->next_out = ctx->obuf;
556 zout->avail_out = ctx->obufsize;
557 /* Compress some more */
558 ret = deflate(zout, Z_FINISH);
559 if (ret == Z_STREAM_END)
561 else if (ret != Z_OK) {
562 COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
563 ERR_add_error_data(2, "zlib error:", zError(ret));
566 ctx->ocount = ctx->obufsize - zout->avail_out;
570 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
577 ctx = (BIO_ZLIB_CTX *) b->ptr;
587 ret = bio_zlib_flush(b);
589 ret = BIO_flush(b->next_bio);
592 case BIO_C_SET_BUFF_SIZE:
607 OPENSSL_free(ctx->ibuf);
613 OPENSSL_free(ctx->obuf);
620 case BIO_C_DO_STATE_MACHINE:
621 BIO_clear_retry_flags(b);
622 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
623 BIO_copy_next_retry(b);
627 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
635 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
639 return BIO_callback_ctrl(b->next_bio, cmd, fp);