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 "internal/comp.h"
60 #include <openssl/err.h>
61 #include "internal/cryptlib_int.h"
62 #include "internal/bio.h"
65 COMP_METHOD *COMP_zlib(void);
67 static COMP_METHOD zlib_method_nozlib = {
82 static int zlib_stateful_init(COMP_CTX *ctx);
83 static void zlib_stateful_finish(COMP_CTX *ctx);
84 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
85 unsigned int olen, unsigned char *in,
87 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
88 unsigned int olen, unsigned char *in,
91 /* memory allocations functions for zlib initialisation */
92 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
96 p = OPENSSL_zalloc(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
116 * When OpenSSL is built on Windows, we do not want to require that
117 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
118 * work. Therefore, all ZLIB routines are loaded at run time
119 * and we do not link to a .LIB file when ZLIB_SHARED is set.
121 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
122 # include <windows.h>
123 # endif /* !(OPENSSL_SYS_WINDOWS ||
124 * OPENSSL_SYS_WIN32) */
127 # include "internal/dso.h"
129 /* Function pointers */
130 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
131 const Bytef *source, uLong sourceLen);
132 typedef int (*inflateEnd_ft) (z_streamp strm);
133 typedef int (*inflate_ft) (z_streamp strm, int flush);
134 typedef int (*inflateInit__ft) (z_streamp strm,
135 const char *version, int stream_size);
136 typedef int (*deflateEnd_ft) (z_streamp strm);
137 typedef int (*deflate_ft) (z_streamp strm, int flush);
138 typedef int (*deflateInit__ft) (z_streamp strm, int level,
139 const char *version, int stream_size);
140 typedef const char *(*zError__ft) (int err);
141 static compress_ft p_compress = NULL;
142 static inflateEnd_ft p_inflateEnd = NULL;
143 static inflate_ft p_inflate = NULL;
144 static inflateInit__ft p_inflateInit_ = NULL;
145 static deflateEnd_ft p_deflateEnd = NULL;
146 static deflate_ft p_deflate = NULL;
147 static deflateInit__ft p_deflateInit_ = NULL;
148 static zError__ft p_zError = NULL;
150 static int zlib_loaded = 0; /* only attempt to init func pts once */
151 static DSO *zlib_dso = NULL;
153 # define compress p_compress
154 # define inflateEnd p_inflateEnd
155 # define inflate p_inflate
156 # define inflateInit_ p_inflateInit_
157 # define deflateEnd p_deflateEnd
158 # define deflate p_deflate
159 # define deflateInit_ p_deflateInit_
160 # define zError p_zError
161 # endif /* ZLIB_SHARED */
168 static int zlib_stateful_init(COMP_CTX *ctx)
171 struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
176 state->istream.zalloc = zlib_zalloc;
177 state->istream.zfree = zlib_zfree;
178 state->istream.opaque = Z_NULL;
179 state->istream.next_in = Z_NULL;
180 state->istream.next_out = Z_NULL;
181 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
185 state->ostream.zalloc = zlib_zalloc;
186 state->ostream.zfree = zlib_zfree;
187 state->ostream.opaque = Z_NULL;
188 state->ostream.next_in = Z_NULL;
189 state->ostream.next_out = Z_NULL;
190 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
191 ZLIB_VERSION, sizeof(z_stream));
202 static void zlib_stateful_finish(COMP_CTX *ctx)
204 struct zlib_state *state = ctx->data;
205 inflateEnd(&state->istream);
206 deflateEnd(&state->ostream);
210 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
211 unsigned int olen, unsigned char *in,
215 struct zlib_state *state = ctx->data;
220 state->ostream.next_in = in;
221 state->ostream.avail_in = ilen;
222 state->ostream.next_out = out;
223 state->ostream.avail_out = olen;
225 err = deflate(&state->ostream, Z_SYNC_FLUSH);
228 return olen - state->ostream.avail_out;
231 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
232 unsigned int olen, unsigned char *in,
236 struct zlib_state *state = ctx->data;
241 state->istream.next_in = in;
242 state->istream.avail_in = ilen;
243 state->istream.next_out = out;
244 state->istream.avail_out = olen;
246 err = inflate(&state->istream, Z_SYNC_FLUSH);
249 return olen - state->istream.avail_out;
254 COMP_METHOD *COMP_zlib(void)
256 COMP_METHOD *meth = &zlib_method_nozlib;
260 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
261 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
263 zlib_dso = DSO_load(NULL, "z", NULL, 0);
265 if (zlib_dso != NULL) {
266 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
268 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
269 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
271 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
273 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
274 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
276 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
277 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
279 if (p_compress && p_inflateEnd && p_inflate
280 && p_inflateInit_ && p_deflateEnd
281 && p_deflate && p_deflateInit_ && p_zError)
284 if (!OPENSSL_init_crypto(OPENSSL_INIT_ZLIB, NULL)) {
285 int_comp_zlib_cleanup();
289 meth = &zlib_stateful_method;
294 meth = &zlib_stateful_method;
300 void int_comp_zlib_cleanup(void)
303 if (zlib_dso != NULL)
311 /* Zlib based compression/decompression filter BIO */
314 unsigned char *ibuf; /* Input buffer */
315 int ibufsize; /* Buffer size */
316 z_stream zin; /* Input decompress context */
317 unsigned char *obuf; /* Output buffer */
318 int obufsize; /* Output buffer size */
319 unsigned char *optr; /* Position in output buffer */
320 int ocount; /* Amount of data in output buffer */
321 int odone; /* deflate EOF */
322 int comp_level; /* Compression level to use */
323 z_stream zout; /* Output compression context */
326 # define ZLIB_DEFAULT_BUFSIZE 1024
328 static int bio_zlib_new(BIO *bi);
329 static int bio_zlib_free(BIO *bi);
330 static int bio_zlib_read(BIO *b, char *out, int outl);
331 static int bio_zlib_write(BIO *b, const char *in, int inl);
332 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
333 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
335 static const BIO_METHOD bio_meth_zlib = {
345 bio_zlib_callback_ctrl
348 const BIO_METHOD *BIO_f_zlib(void)
350 return &bio_meth_zlib;
353 static int bio_zlib_new(BIO *bi)
359 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
363 ctx = OPENSSL_zalloc(sizeof(*ctx));
365 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
368 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
369 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
370 ctx->zin.zalloc = Z_NULL;
371 ctx->zin.zfree = Z_NULL;
372 ctx->zout.zalloc = Z_NULL;
373 ctx->zout.zfree = Z_NULL;
374 ctx->comp_level = Z_DEFAULT_COMPRESSION;
376 BIO_set_data(bi, ctx);
381 static int bio_zlib_free(BIO *bi)
386 ctx = BIO_get_data(bi);
388 /* Destroy decompress context */
389 inflateEnd(&ctx->zin);
390 OPENSSL_free(ctx->ibuf);
393 /* Destroy compress context */
394 deflateEnd(&ctx->zout);
395 OPENSSL_free(ctx->obuf);
398 BIO_set_data(bi, NULL);
404 static int bio_zlib_read(BIO *b, char *out, int outl)
409 BIO *next = BIO_next(b);
413 ctx = BIO_get_data(b);
415 BIO_clear_retry_flags(b);
417 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
418 if (ctx->ibuf == NULL) {
419 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
423 zin->next_in = ctx->ibuf;
427 /* Copy output data directly to supplied buffer */
428 zin->next_out = (unsigned char *)out;
429 zin->avail_out = (unsigned int)outl;
431 /* Decompress while data available */
432 while (zin->avail_in) {
433 ret = inflate(zin, 0);
434 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
435 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
436 ERR_add_error_data(2, "zlib error:", zError(ret));
439 /* If EOF or we've read everything then return */
440 if ((ret == Z_STREAM_END) || !zin->avail_out)
441 return outl - zin->avail_out;
445 * No data in input buffer try to read some in, if an error then
446 * return the total data read.
448 ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
450 /* Total data read */
451 int tot = outl - zin->avail_out;
452 BIO_copy_next_retry(b);
454 return (tot > 0) ? tot : ret;
458 zin->next_in = ctx->ibuf;
462 static int bio_zlib_write(BIO *b, const char *in, int inl)
467 BIO *next = BIO_next(b);
471 ctx = BIO_get_data(b);
475 BIO_clear_retry_flags(b);
477 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
478 /* Need error here */
479 if (ctx->obuf == NULL) {
480 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
483 ctx->optr = ctx->obuf;
485 deflateInit(zout, ctx->comp_level);
486 zout->next_out = ctx->obuf;
487 zout->avail_out = ctx->obufsize;
489 /* Obtain input data directly from supplied buffer */
490 zout->next_in = (void *)in;
491 zout->avail_in = inl;
493 /* If data in output buffer write it first */
494 while (ctx->ocount) {
495 ret = BIO_write(next, ctx->optr, ctx->ocount);
497 /* Total data written */
498 int tot = inl - zout->avail_in;
499 BIO_copy_next_retry(b);
501 return (tot > 0) ? tot : ret;
508 /* Have we consumed all supplied data? */
512 /* Compress some more */
515 ctx->optr = ctx->obuf;
516 zout->next_out = ctx->obuf;
517 zout->avail_out = ctx->obufsize;
518 /* Compress some more */
519 ret = deflate(zout, 0);
521 COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
522 ERR_add_error_data(2, "zlib error:", zError(ret));
525 ctx->ocount = ctx->obufsize - zout->avail_out;
529 static int bio_zlib_flush(BIO *b)
534 BIO *next = BIO_next(b);
536 ctx = BIO_get_data(b);
537 /* If no data written or already flush show success */
538 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
541 BIO_clear_retry_flags(b);
542 /* No more input data */
543 zout->next_in = NULL;
546 /* If data in output buffer write it first */
547 while (ctx->ocount) {
548 ret = BIO_write(next, ctx->optr, ctx->ocount);
550 BIO_copy_next_retry(b);
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, Z_FINISH);
567 if (ret == Z_STREAM_END)
569 else if (ret != Z_OK) {
570 COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
571 ERR_add_error_data(2, "zlib error:", zError(ret));
574 ctx->ocount = ctx->obufsize - zout->avail_out;
578 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
583 BIO *next = BIO_next(b);
587 ctx = BIO_get_data(b);
597 ret = bio_zlib_flush(b);
599 ret = BIO_flush(next);
602 case BIO_C_SET_BUFF_SIZE:
617 OPENSSL_free(ctx->ibuf);
623 OPENSSL_free(ctx->obuf);
630 case BIO_C_DO_STATE_MACHINE:
631 BIO_clear_retry_flags(b);
632 ret = BIO_ctrl(next, cmd, num, ptr);
633 BIO_copy_next_retry(b);
637 ret = BIO_ctrl(next, cmd, num, ptr);
645 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
647 BIO *next = BIO_next(b);
650 return BIO_callback_ctrl(next, cmd, fp);