2 * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/objects.h>
14 #include "internal/comp.h"
15 #include <openssl/err.h>
16 #include "internal/cryptlib_int.h"
17 #include "internal/bio.h"
20 COMP_METHOD *COMP_zlib(void);
22 static COMP_METHOD zlib_method_nozlib = {
37 static int zlib_stateful_init(COMP_CTX *ctx);
38 static void zlib_stateful_finish(COMP_CTX *ctx);
39 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
40 unsigned int olen, unsigned char *in,
42 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
43 unsigned int olen, unsigned char *in,
46 /* memory allocations functions for zlib initialisation */
47 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
51 p = OPENSSL_zalloc(no * size);
55 static void zlib_zfree(void *opaque, void *address)
57 OPENSSL_free(address);
61 static COMP_METHOD zlib_stateful_method = {
66 zlib_stateful_compress_block,
67 zlib_stateful_expand_block
71 * When OpenSSL is built on Windows, we do not want to require that
72 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
73 * work. Therefore, all ZLIB routines are loaded at run time
74 * and we do not link to a .LIB file when ZLIB_SHARED is set.
76 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
78 # endif /* !(OPENSSL_SYS_WINDOWS ||
79 * OPENSSL_SYS_WIN32) */
82 # include "internal/dso.h"
84 /* Function pointers */
85 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
86 const Bytef *source, uLong sourceLen);
87 typedef int (*inflateEnd_ft) (z_streamp strm);
88 typedef int (*inflate_ft) (z_streamp strm, int flush);
89 typedef int (*inflateInit__ft) (z_streamp strm,
90 const char *version, int stream_size);
91 typedef int (*deflateEnd_ft) (z_streamp strm);
92 typedef int (*deflate_ft) (z_streamp strm, int flush);
93 typedef int (*deflateInit__ft) (z_streamp strm, int level,
94 const char *version, int stream_size);
95 typedef const char *(*zError__ft) (int err);
96 static compress_ft p_compress = NULL;
97 static inflateEnd_ft p_inflateEnd = NULL;
98 static inflate_ft p_inflate = NULL;
99 static inflateInit__ft p_inflateInit_ = NULL;
100 static deflateEnd_ft p_deflateEnd = NULL;
101 static deflate_ft p_deflate = NULL;
102 static deflateInit__ft p_deflateInit_ = NULL;
103 static zError__ft p_zError = NULL;
105 static int zlib_loaded = 0; /* only attempt to init func pts once */
106 static DSO *zlib_dso = NULL;
108 # define compress p_compress
109 # define inflateEnd p_inflateEnd
110 # define inflate p_inflate
111 # define inflateInit_ p_inflateInit_
112 # define deflateEnd p_deflateEnd
113 # define deflate p_deflate
114 # define deflateInit_ p_deflateInit_
115 # define zError p_zError
116 # endif /* ZLIB_SHARED */
123 static int zlib_stateful_init(COMP_CTX *ctx)
126 struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
131 state->istream.zalloc = zlib_zalloc;
132 state->istream.zfree = zlib_zfree;
133 state->istream.opaque = Z_NULL;
134 state->istream.next_in = Z_NULL;
135 state->istream.next_out = Z_NULL;
136 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
140 state->ostream.zalloc = zlib_zalloc;
141 state->ostream.zfree = zlib_zfree;
142 state->ostream.opaque = Z_NULL;
143 state->ostream.next_in = Z_NULL;
144 state->ostream.next_out = Z_NULL;
145 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
146 ZLIB_VERSION, sizeof(z_stream));
157 static void zlib_stateful_finish(COMP_CTX *ctx)
159 struct zlib_state *state = ctx->data;
160 inflateEnd(&state->istream);
161 deflateEnd(&state->ostream);
165 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
166 unsigned int olen, unsigned char *in,
170 struct zlib_state *state = ctx->data;
175 state->ostream.next_in = in;
176 state->ostream.avail_in = ilen;
177 state->ostream.next_out = out;
178 state->ostream.avail_out = olen;
180 err = deflate(&state->ostream, Z_SYNC_FLUSH);
183 return olen - state->ostream.avail_out;
186 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
187 unsigned int olen, unsigned char *in,
191 struct zlib_state *state = ctx->data;
196 state->istream.next_in = in;
197 state->istream.avail_in = ilen;
198 state->istream.next_out = out;
199 state->istream.avail_out = olen;
201 err = inflate(&state->istream, Z_SYNC_FLUSH);
204 return olen - state->istream.avail_out;
209 COMP_METHOD *COMP_zlib(void)
211 COMP_METHOD *meth = &zlib_method_nozlib;
214 /* LIBZ may be externally defined, and we should respect that value */
216 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
217 # define LIBZ "ZLIB1"
218 # elif defined(OPENSSL_SYS_VMS)
226 zlib_dso = DSO_load(NULL, LIBZ, NULL, 0);
227 if (zlib_dso != NULL) {
228 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
230 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
231 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
233 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
235 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
236 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
238 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
239 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
241 if (p_compress && p_inflateEnd && p_inflate
242 && p_inflateInit_ && p_deflateEnd
243 && p_deflate && p_deflateInit_ && p_zError)
246 if (!OPENSSL_init_crypto(OPENSSL_INIT_ZLIB, NULL)) {
247 comp_zlib_cleanup_int();
251 meth = &zlib_stateful_method;
256 meth = &zlib_stateful_method;
262 void comp_zlib_cleanup_int(void)
265 if (zlib_dso != NULL)
273 /* Zlib based compression/decompression filter BIO */
276 unsigned char *ibuf; /* Input buffer */
277 int ibufsize; /* Buffer size */
278 z_stream zin; /* Input decompress context */
279 unsigned char *obuf; /* Output buffer */
280 int obufsize; /* Output buffer size */
281 unsigned char *optr; /* Position in output buffer */
282 int ocount; /* Amount of data in output buffer */
283 int odone; /* deflate EOF */
284 int comp_level; /* Compression level to use */
285 z_stream zout; /* Output compression context */
288 # define ZLIB_DEFAULT_BUFSIZE 1024
290 static int bio_zlib_new(BIO *bi);
291 static int bio_zlib_free(BIO *bi);
292 static int bio_zlib_read(BIO *b, char *out, int outl);
293 static int bio_zlib_write(BIO *b, const char *in, int inl);
294 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
295 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
297 static const BIO_METHOD bio_meth_zlib = {
300 /* TODO: Convert to new style write function */
303 /* TODO: Convert to new style read function */
311 bio_zlib_callback_ctrl
314 const BIO_METHOD *BIO_f_zlib(void)
316 return &bio_meth_zlib;
319 static int bio_zlib_new(BIO *bi)
325 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
329 ctx = OPENSSL_zalloc(sizeof(*ctx));
331 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
334 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
335 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
336 ctx->zin.zalloc = Z_NULL;
337 ctx->zin.zfree = Z_NULL;
338 ctx->zout.zalloc = Z_NULL;
339 ctx->zout.zfree = Z_NULL;
340 ctx->comp_level = Z_DEFAULT_COMPRESSION;
342 BIO_set_data(bi, ctx);
347 static int bio_zlib_free(BIO *bi)
352 ctx = BIO_get_data(bi);
354 /* Destroy decompress context */
355 inflateEnd(&ctx->zin);
356 OPENSSL_free(ctx->ibuf);
359 /* Destroy compress context */
360 deflateEnd(&ctx->zout);
361 OPENSSL_free(ctx->obuf);
364 BIO_set_data(bi, NULL);
370 static int bio_zlib_read(BIO *b, char *out, int outl)
375 BIO *next = BIO_next(b);
379 ctx = BIO_get_data(b);
381 BIO_clear_retry_flags(b);
383 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
384 if (ctx->ibuf == NULL) {
385 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
389 zin->next_in = ctx->ibuf;
393 /* Copy output data directly to supplied buffer */
394 zin->next_out = (unsigned char *)out;
395 zin->avail_out = (unsigned int)outl;
397 /* Decompress while data available */
398 while (zin->avail_in) {
399 ret = inflate(zin, 0);
400 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
401 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
402 ERR_add_error_data(2, "zlib error:", zError(ret));
405 /* If EOF or we've read everything then return */
406 if ((ret == Z_STREAM_END) || !zin->avail_out)
407 return outl - zin->avail_out;
411 * No data in input buffer try to read some in, if an error then
412 * return the total data read.
414 ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
416 /* Total data read */
417 int tot = outl - zin->avail_out;
418 BIO_copy_next_retry(b);
420 return (tot > 0) ? tot : ret;
424 zin->next_in = ctx->ibuf;
428 static int bio_zlib_write(BIO *b, const char *in, int inl)
433 BIO *next = BIO_next(b);
437 ctx = BIO_get_data(b);
441 BIO_clear_retry_flags(b);
443 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
444 /* Need error here */
445 if (ctx->obuf == NULL) {
446 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
449 ctx->optr = ctx->obuf;
451 deflateInit(zout, ctx->comp_level);
452 zout->next_out = ctx->obuf;
453 zout->avail_out = ctx->obufsize;
455 /* Obtain input data directly from supplied buffer */
456 zout->next_in = (void *)in;
457 zout->avail_in = inl;
459 /* If data in output buffer write it first */
460 while (ctx->ocount) {
461 ret = BIO_write(next, ctx->optr, ctx->ocount);
463 /* Total data written */
464 int tot = inl - zout->avail_in;
465 BIO_copy_next_retry(b);
467 return (tot > 0) ? tot : ret;
474 /* Have we consumed all supplied data? */
478 /* Compress some more */
481 ctx->optr = ctx->obuf;
482 zout->next_out = ctx->obuf;
483 zout->avail_out = ctx->obufsize;
484 /* Compress some more */
485 ret = deflate(zout, 0);
487 COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
488 ERR_add_error_data(2, "zlib error:", zError(ret));
491 ctx->ocount = ctx->obufsize - zout->avail_out;
495 static int bio_zlib_flush(BIO *b)
500 BIO *next = BIO_next(b);
502 ctx = BIO_get_data(b);
503 /* If no data written or already flush show success */
504 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
507 BIO_clear_retry_flags(b);
508 /* No more input data */
509 zout->next_in = NULL;
512 /* If data in output buffer write it first */
513 while (ctx->ocount) {
514 ret = BIO_write(next, ctx->optr, ctx->ocount);
516 BIO_copy_next_retry(b);
525 /* Compress some more */
528 ctx->optr = ctx->obuf;
529 zout->next_out = ctx->obuf;
530 zout->avail_out = ctx->obufsize;
531 /* Compress some more */
532 ret = deflate(zout, Z_FINISH);
533 if (ret == Z_STREAM_END)
535 else if (ret != Z_OK) {
536 COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
537 ERR_add_error_data(2, "zlib error:", zError(ret));
540 ctx->ocount = ctx->obufsize - zout->avail_out;
544 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
549 BIO *next = BIO_next(b);
553 ctx = BIO_get_data(b);
563 ret = bio_zlib_flush(b);
565 ret = BIO_flush(next);
568 case BIO_C_SET_BUFF_SIZE:
583 OPENSSL_free(ctx->ibuf);
589 OPENSSL_free(ctx->obuf);
596 case BIO_C_DO_STATE_MACHINE:
597 BIO_clear_retry_flags(b);
598 ret = BIO_ctrl(next, cmd, num, ptr);
599 BIO_copy_next_retry(b);
603 ret = BIO_ctrl(next, cmd, num, ptr);
611 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
613 BIO *next = BIO_next(b);
616 return BIO_callback_ctrl(next, cmd, fp);