1 /* ====================================================================
2 * Copyright (c) 1999-2015 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
58 #include <openssl/objects.h>
59 #include <openssl/comp.h>
60 #include <openssl/err.h>
63 COMP_METHOD *COMP_zlib(void);
65 static COMP_METHOD zlib_method_nozlib = {
80 static int zlib_stateful_init(COMP_CTX *ctx);
81 static void zlib_stateful_finish(COMP_CTX *ctx);
82 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
83 unsigned int olen, unsigned char *in,
85 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
86 unsigned int olen, unsigned char *in,
89 /* memory allocations functions for zlib intialization */
90 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
94 p = OPENSSL_malloc(no * size);
96 memset(p, 0, no * size);
100 static void zlib_zfree(void *opaque, void *address)
102 OPENSSL_free(address);
106 static COMP_METHOD zlib_stateful_method = {
107 NID_zlib_compression,
110 zlib_stateful_finish,
111 zlib_stateful_compress_block,
112 zlib_stateful_expand_block,
118 * When OpenSSL is built on Windows, we do not want to require that
119 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
120 * work. Therefore, all ZLIB routines are loaded at run time
121 * and we do not link to a .LIB file when ZLIB_SHARED is set.
123 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
124 # include <windows.h>
125 # endif /* !(OPENSSL_SYS_WINDOWS ||
126 * OPENSSL_SYS_WIN32) */
129 # include <openssl/dso.h>
131 /* Function pointers */
132 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
133 const Bytef *source, uLong sourceLen);
134 typedef int (*inflateEnd_ft) (z_streamp strm);
135 typedef int (*inflate_ft) (z_streamp strm, int flush);
136 typedef int (*inflateInit__ft) (z_streamp strm,
137 const char *version, int stream_size);
138 typedef int (*deflateEnd_ft) (z_streamp strm);
139 typedef int (*deflate_ft) (z_streamp strm, int flush);
140 typedef int (*deflateInit__ft) (z_streamp strm, int level,
141 const char *version, int stream_size);
142 typedef const char *(*zError__ft) (int err);
143 static compress_ft p_compress = NULL;
144 static inflateEnd_ft p_inflateEnd = NULL;
145 static inflate_ft p_inflate = NULL;
146 static inflateInit__ft p_inflateInit_ = NULL;
147 static deflateEnd_ft p_deflateEnd = NULL;
148 static deflate_ft p_deflate = NULL;
149 static deflateInit__ft p_deflateInit_ = NULL;
150 static zError__ft p_zError = NULL;
152 static int zlib_loaded = 0; /* only attempt to init func pts once */
153 static DSO *zlib_dso = NULL;
155 # define compress p_compress
156 # define inflateEnd p_inflateEnd
157 # define inflate p_inflate
158 # define inflateInit_ p_inflateInit_
159 # define deflateEnd p_deflateEnd
160 # define deflate p_deflate
161 # define deflateInit_ p_deflateInit_
162 # define zError p_zError
163 # endif /* ZLIB_SHARED */
170 static int zlib_stateful_ex_idx = -1;
172 static int zlib_stateful_init(COMP_CTX *ctx)
175 struct zlib_state *state = OPENSSL_malloc(sizeof(*state));
180 state->istream.zalloc = zlib_zalloc;
181 state->istream.zfree = zlib_zfree;
182 state->istream.opaque = Z_NULL;
183 state->istream.next_in = Z_NULL;
184 state->istream.next_out = Z_NULL;
185 state->istream.avail_in = 0;
186 state->istream.avail_out = 0;
187 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
191 state->ostream.zalloc = zlib_zalloc;
192 state->ostream.zfree = zlib_zfree;
193 state->ostream.opaque = Z_NULL;
194 state->ostream.next_in = Z_NULL;
195 state->ostream.next_out = Z_NULL;
196 state->ostream.avail_in = 0;
197 state->ostream.avail_out = 0;
198 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
199 ZLIB_VERSION, sizeof(z_stream));
203 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
204 CRYPTO_set_ex_data(&ctx->ex_data, zlib_stateful_ex_idx, state);
211 static void zlib_stateful_finish(COMP_CTX *ctx)
213 struct zlib_state *state =
214 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
215 zlib_stateful_ex_idx);
216 inflateEnd(&state->istream);
217 deflateEnd(&state->ostream);
219 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
222 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
223 unsigned int olen, unsigned char *in,
227 struct zlib_state *state =
228 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
229 zlib_stateful_ex_idx);
234 state->ostream.next_in = in;
235 state->ostream.avail_in = ilen;
236 state->ostream.next_out = out;
237 state->ostream.avail_out = olen;
239 err = deflate(&state->ostream, Z_SYNC_FLUSH);
243 fprintf(stderr, "compress(%4d)->%4d %s\n",
244 ilen, olen - state->ostream.avail_out,
245 (ilen != olen - state->ostream.avail_out) ? "zlib" : "clear");
247 return olen - state->ostream.avail_out;
250 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
251 unsigned int olen, unsigned char *in,
256 struct zlib_state *state =
257 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
258 zlib_stateful_ex_idx);
263 state->istream.next_in = in;
264 state->istream.avail_in = ilen;
265 state->istream.next_out = out;
266 state->istream.avail_out = olen;
268 err = inflate(&state->istream, Z_SYNC_FLUSH);
272 fprintf(stderr, "expand(%4d)->%4d %s\n",
273 ilen, olen - state->istream.avail_out,
274 (ilen != olen - state->istream.avail_out) ? "zlib" : "clear");
276 return olen - state->istream.avail_out;
281 COMP_METHOD *COMP_zlib(void)
283 COMP_METHOD *meth = &zlib_method_nozlib;
287 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
288 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
290 zlib_dso = DSO_load(NULL, "z", NULL, 0);
292 if (zlib_dso != NULL) {
293 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
295 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
296 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
298 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
300 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
301 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
303 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
304 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
306 if (p_compress && p_inflateEnd && p_inflate
307 && p_inflateInit_ && p_deflateEnd
308 && p_deflate && p_deflateInit_ && p_zError)
316 #if defined(ZLIB) || defined(ZLIB_SHARED)
319 * init zlib_stateful_ex_idx here so that in a multi-process
320 * application it's enough to intialize openssl before forking (idx
321 * will be inherited in all the children)
323 if (zlib_stateful_ex_idx == -1) {
324 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
325 if (zlib_stateful_ex_idx == -1)
326 zlib_stateful_ex_idx =
327 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
328 0, NULL, NULL, NULL, NULL);
329 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
330 if (zlib_stateful_ex_idx == -1)
334 meth = &zlib_stateful_method;
342 void COMP_zlib_cleanup(void)
351 /* Zlib based compression/decompression filter BIO */
354 unsigned char *ibuf; /* Input buffer */
355 int ibufsize; /* Buffer size */
356 z_stream zin; /* Input decompress context */
357 unsigned char *obuf; /* Output buffer */
358 int obufsize; /* Output buffer size */
359 unsigned char *optr; /* Position in output buffer */
360 int ocount; /* Amount of data in output buffer */
361 int odone; /* deflate EOF */
362 int comp_level; /* Compression level to use */
363 z_stream zout; /* Output compression context */
366 # define ZLIB_DEFAULT_BUFSIZE 1024
368 static int bio_zlib_new(BIO *bi);
369 static int bio_zlib_free(BIO *bi);
370 static int bio_zlib_read(BIO *b, char *out, int outl);
371 static int bio_zlib_write(BIO *b, const char *in, int inl);
372 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
373 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
375 static BIO_METHOD bio_meth_zlib = {
385 bio_zlib_callback_ctrl
388 BIO_METHOD *BIO_f_zlib(void)
390 return &bio_meth_zlib;
393 static int bio_zlib_new(BIO *bi)
399 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
403 ctx = OPENSSL_malloc(sizeof(*ctx));
405 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
410 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
411 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
412 ctx->zin.zalloc = Z_NULL;
413 ctx->zin.zfree = Z_NULL;
414 ctx->zin.next_in = NULL;
415 ctx->zin.avail_in = 0;
416 ctx->zin.next_out = NULL;
417 ctx->zin.avail_out = 0;
418 ctx->zout.zalloc = Z_NULL;
419 ctx->zout.zfree = Z_NULL;
420 ctx->zout.next_in = NULL;
421 ctx->zout.avail_in = 0;
422 ctx->zout.next_out = NULL;
423 ctx->zout.avail_out = 0;
425 ctx->comp_level = Z_DEFAULT_COMPRESSION;
427 bi->ptr = (char *)ctx;
432 static int bio_zlib_free(BIO *bi)
437 ctx = (BIO_ZLIB_CTX *) bi->ptr;
439 /* Destroy decompress context */
440 inflateEnd(&ctx->zin);
441 OPENSSL_free(ctx->ibuf);
444 /* Destroy compress context */
445 deflateEnd(&ctx->zout);
446 OPENSSL_free(ctx->obuf);
455 static int bio_zlib_read(BIO *b, char *out, int outl)
462 ctx = (BIO_ZLIB_CTX *) b->ptr;
464 BIO_clear_retry_flags(b);
466 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
468 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
472 zin->next_in = ctx->ibuf;
476 /* Copy output data directly to supplied buffer */
477 zin->next_out = (unsigned char *)out;
478 zin->avail_out = (unsigned int)outl;
480 /* Decompress while data available */
481 while (zin->avail_in) {
482 ret = inflate(zin, 0);
483 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
484 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
485 ERR_add_error_data(2, "zlib error:", zError(ret));
488 /* If EOF or we've read everything then return */
489 if ((ret == Z_STREAM_END) || !zin->avail_out)
490 return outl - zin->avail_out;
494 * No data in input buffer try to read some in, if an error then
495 * return the total data read.
497 ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
499 /* Total data read */
500 int tot = outl - zin->avail_out;
501 BIO_copy_next_retry(b);
503 return (tot > 0) ? tot : ret;
507 zin->next_in = ctx->ibuf;
511 static int bio_zlib_write(BIO *b, const char *in, int inl)
518 ctx = (BIO_ZLIB_CTX *) b->ptr;
522 BIO_clear_retry_flags(b);
524 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
525 /* Need error here */
527 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
530 ctx->optr = ctx->obuf;
532 deflateInit(zout, ctx->comp_level);
533 zout->next_out = ctx->obuf;
534 zout->avail_out = ctx->obufsize;
536 /* Obtain input data directly from supplied buffer */
537 zout->next_in = (void *)in;
538 zout->avail_in = inl;
540 /* If data in output buffer write it first */
541 while (ctx->ocount) {
542 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
544 /* Total data written */
545 int tot = inl - zout->avail_in;
546 BIO_copy_next_retry(b);
548 return (tot > 0) ? tot : ret;
555 /* Have we consumed all supplied data? */
559 /* Compress some more */
562 ctx->optr = ctx->obuf;
563 zout->next_out = ctx->obuf;
564 zout->avail_out = ctx->obufsize;
565 /* Compress some more */
566 ret = deflate(zout, 0);
568 COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
569 ERR_add_error_data(2, "zlib error:", zError(ret));
572 ctx->ocount = ctx->obufsize - zout->avail_out;
576 static int bio_zlib_flush(BIO *b)
581 ctx = (BIO_ZLIB_CTX *) b->ptr;
582 /* If no data written or already flush show success */
583 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
586 BIO_clear_retry_flags(b);
587 /* No more input data */
588 zout->next_in = NULL;
591 /* If data in output buffer write it first */
592 while (ctx->ocount) {
593 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
595 BIO_copy_next_retry(b);
604 /* Compress some more */
607 ctx->optr = ctx->obuf;
608 zout->next_out = ctx->obuf;
609 zout->avail_out = ctx->obufsize;
610 /* Compress some more */
611 ret = deflate(zout, Z_FINISH);
612 if (ret == Z_STREAM_END)
614 else if (ret != Z_OK) {
615 COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
616 ERR_add_error_data(2, "zlib error:", zError(ret));
619 ctx->ocount = ctx->obufsize - zout->avail_out;
623 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
630 ctx = (BIO_ZLIB_CTX *) b->ptr;
640 ret = bio_zlib_flush(b);
642 ret = BIO_flush(b->next_bio);
645 case BIO_C_SET_BUFF_SIZE:
660 OPENSSL_free(ctx->ibuf);
666 OPENSSL_free(ctx->obuf);
673 case BIO_C_DO_STATE_MACHINE:
674 BIO_clear_retry_flags(b);
675 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
676 BIO_copy_next_retry(b);
680 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
688 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
692 return BIO_callback_ctrl(b->next_bio, cmd, fp);