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 =
123 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
128 state->istream.zalloc = zlib_zalloc;
129 state->istream.zfree = zlib_zfree;
130 state->istream.opaque = Z_NULL;
131 state->istream.next_in = Z_NULL;
132 state->istream.next_out = Z_NULL;
133 state->istream.avail_in = 0;
134 state->istream.avail_out = 0;
135 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
139 state->ostream.zalloc = zlib_zalloc;
140 state->ostream.zfree = zlib_zfree;
141 state->ostream.opaque = Z_NULL;
142 state->ostream.next_in = Z_NULL;
143 state->ostream.next_out = Z_NULL;
144 state->ostream.avail_in = 0;
145 state->ostream.avail_out = 0;
146 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
147 ZLIB_VERSION, sizeof(z_stream));
151 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
152 CRYPTO_set_ex_data(&ctx->ex_data, zlib_stateful_ex_idx, state);
160 static void zlib_stateful_finish(COMP_CTX *ctx)
162 struct zlib_state *state =
163 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
164 zlib_stateful_ex_idx);
165 inflateEnd(&state->istream);
166 deflateEnd(&state->ostream);
168 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
171 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
172 unsigned int olen, unsigned char *in,
176 struct zlib_state *state =
177 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
178 zlib_stateful_ex_idx);
183 state->ostream.next_in = in;
184 state->ostream.avail_in = ilen;
185 state->ostream.next_out = out;
186 state->ostream.avail_out = olen;
188 err = deflate(&state->ostream, Z_SYNC_FLUSH);
192 fprintf(stderr, "compress(%4d)->%4d %s\n",
193 ilen, olen - state->ostream.avail_out,
194 (ilen != olen - state->ostream.avail_out) ? "zlib" : "clear");
196 return olen - state->ostream.avail_out;
199 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
200 unsigned int olen, unsigned char *in,
205 struct zlib_state *state =
206 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
207 zlib_stateful_ex_idx);
212 state->istream.next_in = in;
213 state->istream.avail_in = ilen;
214 state->istream.next_out = out;
215 state->istream.avail_out = olen;
217 err = inflate(&state->istream, Z_SYNC_FLUSH);
221 fprintf(stderr, "expand(%4d)->%4d %s\n",
222 ilen, olen - state->istream.avail_out,
223 (ilen != olen - state->istream.avail_out) ? "zlib" : "clear");
225 return olen - state->istream.avail_out;
230 COMP_METHOD *COMP_zlib(void)
232 COMP_METHOD *meth = &zlib_method_nozlib;
236 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
237 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
239 zlib_dso = DSO_load(NULL, "z", NULL, 0);
241 if (zlib_dso != NULL) {
242 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
244 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
245 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
247 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
249 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
250 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
252 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
253 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
255 if (p_compress && p_inflateEnd && p_inflate
256 && p_inflateInit_ && p_deflateEnd
257 && p_deflate && p_deflateInit_ && p_zError)
265 #if defined(ZLIB) || defined(ZLIB_SHARED)
268 * init zlib_stateful_ex_idx here so that in a multi-process
269 * application it's enough to intialize openssl before forking (idx
270 * will be inherited in all the children)
272 if (zlib_stateful_ex_idx == -1) {
273 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
274 if (zlib_stateful_ex_idx == -1)
275 zlib_stateful_ex_idx =
276 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
277 0, NULL, NULL, NULL, NULL);
278 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
279 if (zlib_stateful_ex_idx == -1)
283 meth = &zlib_stateful_method;
291 void COMP_zlib_cleanup(void)
301 /* Zlib based compression/decompression filter BIO */
304 unsigned char *ibuf; /* Input buffer */
305 int ibufsize; /* Buffer size */
306 z_stream zin; /* Input decompress context */
307 unsigned char *obuf; /* Output buffer */
308 int obufsize; /* Output buffer size */
309 unsigned char *optr; /* Position in output buffer */
310 int ocount; /* Amount of data in output buffer */
311 int odone; /* deflate EOF */
312 int comp_level; /* Compression level to use */
313 z_stream zout; /* Output compression context */
316 # define ZLIB_DEFAULT_BUFSIZE 1024
318 static int bio_zlib_new(BIO *bi);
319 static int bio_zlib_free(BIO *bi);
320 static int bio_zlib_read(BIO *b, char *out, int outl);
321 static int bio_zlib_write(BIO *b, const char *in, int inl);
322 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
323 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
325 static BIO_METHOD bio_meth_zlib = {
335 bio_zlib_callback_ctrl
338 BIO_METHOD *BIO_f_zlib(void)
340 return &bio_meth_zlib;
343 static int bio_zlib_new(BIO *bi)
349 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
353 ctx = OPENSSL_malloc(sizeof(BIO_ZLIB_CTX));
355 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
360 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
361 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
362 ctx->zin.zalloc = Z_NULL;
363 ctx->zin.zfree = Z_NULL;
364 ctx->zin.next_in = NULL;
365 ctx->zin.avail_in = 0;
366 ctx->zin.next_out = NULL;
367 ctx->zin.avail_out = 0;
368 ctx->zout.zalloc = Z_NULL;
369 ctx->zout.zfree = Z_NULL;
370 ctx->zout.next_in = NULL;
371 ctx->zout.avail_in = 0;
372 ctx->zout.next_out = NULL;
373 ctx->zout.avail_out = 0;
375 ctx->comp_level = Z_DEFAULT_COMPRESSION;
377 bi->ptr = (char *)ctx;
382 static int bio_zlib_free(BIO *bi)
387 ctx = (BIO_ZLIB_CTX *) bi->ptr;
389 /* Destroy decompress context */
390 inflateEnd(&ctx->zin);
391 OPENSSL_free(ctx->ibuf);
394 /* Destroy compress context */
395 deflateEnd(&ctx->zout);
396 OPENSSL_free(ctx->obuf);
405 static int bio_zlib_read(BIO *b, char *out, int outl)
412 ctx = (BIO_ZLIB_CTX *) b->ptr;
414 BIO_clear_retry_flags(b);
416 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
418 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
422 zin->next_in = ctx->ibuf;
426 /* Copy output data directly to supplied buffer */
427 zin->next_out = (unsigned char *)out;
428 zin->avail_out = (unsigned int)outl;
430 /* Decompress while data available */
431 while (zin->avail_in) {
432 ret = inflate(zin, 0);
433 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
434 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
435 ERR_add_error_data(2, "zlib error:", zError(ret));
438 /* If EOF or we've read everything then return */
439 if ((ret == Z_STREAM_END) || !zin->avail_out)
440 return outl - zin->avail_out;
444 * No data in input buffer try to read some in, if an error then
445 * return the total data read.
447 ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
449 /* Total data read */
450 int tot = outl - zin->avail_out;
451 BIO_copy_next_retry(b);
453 return (tot > 0) ? tot : ret;
457 zin->next_in = ctx->ibuf;
461 static int bio_zlib_write(BIO *b, const char *in, int inl)
468 ctx = (BIO_ZLIB_CTX *) b->ptr;
472 BIO_clear_retry_flags(b);
474 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
475 /* Need error here */
477 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
480 ctx->optr = ctx->obuf;
482 deflateInit(zout, ctx->comp_level);
483 zout->next_out = ctx->obuf;
484 zout->avail_out = ctx->obufsize;
486 /* Obtain input data directly from supplied buffer */
487 zout->next_in = (void *)in;
488 zout->avail_in = inl;
490 /* If data in output buffer write it first */
491 while (ctx->ocount) {
492 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
494 /* Total data written */
495 int tot = inl - zout->avail_in;
496 BIO_copy_next_retry(b);
498 return (tot > 0) ? tot : ret;
505 /* Have we consumed all supplied data? */
509 /* Compress some more */
512 ctx->optr = ctx->obuf;
513 zout->next_out = ctx->obuf;
514 zout->avail_out = ctx->obufsize;
515 /* Compress some more */
516 ret = deflate(zout, 0);
518 COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
519 ERR_add_error_data(2, "zlib error:", zError(ret));
522 ctx->ocount = ctx->obufsize - zout->avail_out;
526 static int bio_zlib_flush(BIO *b)
531 ctx = (BIO_ZLIB_CTX *) b->ptr;
532 /* If no data written or already flush show success */
533 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
536 BIO_clear_retry_flags(b);
537 /* No more input data */
538 zout->next_in = NULL;
541 /* If data in output buffer write it first */
542 while (ctx->ocount) {
543 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
545 BIO_copy_next_retry(b);
554 /* Compress some more */
557 ctx->optr = ctx->obuf;
558 zout->next_out = ctx->obuf;
559 zout->avail_out = ctx->obufsize;
560 /* Compress some more */
561 ret = deflate(zout, Z_FINISH);
562 if (ret == Z_STREAM_END)
564 else if (ret != Z_OK) {
565 COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
566 ERR_add_error_data(2, "zlib error:", zError(ret));
569 ctx->ocount = ctx->obufsize - zout->avail_out;
573 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
580 ctx = (BIO_ZLIB_CTX *) b->ptr;
590 ret = bio_zlib_flush(b);
592 ret = BIO_flush(b->next_bio);
595 case BIO_C_SET_BUFF_SIZE:
611 OPENSSL_free(ctx->ibuf);
619 OPENSSL_free(ctx->obuf);
627 case BIO_C_DO_STATE_MACHINE:
628 BIO_clear_retry_flags(b);
629 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
630 BIO_copy_next_retry(b);
634 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
642 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
646 return BIO_callback_ctrl(b->next_bio, cmd, fp);