2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "crypto/cryptlib.h"
17 #include "internal/bio.h"
18 #include "internal/thread_once.h"
19 #include "comp_local.h"
21 COMP_METHOD *COMP_zlib(void);
23 static COMP_METHOD zlib_method_nozlib = {
38 static int zlib_stateful_init(COMP_CTX *ctx);
39 static void zlib_stateful_finish(COMP_CTX *ctx);
40 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
41 unsigned int olen, unsigned char *in,
43 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
44 unsigned int olen, unsigned char *in,
47 /* memory allocations functions for zlib initialisation */
48 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
52 p = OPENSSL_zalloc(no * size);
56 static void zlib_zfree(void *opaque, void *address)
58 OPENSSL_free(address);
62 static COMP_METHOD zlib_stateful_method = {
67 zlib_stateful_compress_block,
68 zlib_stateful_expand_block
72 * When OpenSSL is built on Windows, we do not want to require that
73 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
74 * work. Therefore, all ZLIB routines are loaded at run time
75 * and we do not link to a .LIB file when ZLIB_SHARED is set.
77 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
79 # endif /* !(OPENSSL_SYS_WINDOWS ||
80 * OPENSSL_SYS_WIN32) */
83 # include "internal/dso.h"
85 /* Function pointers */
86 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
87 const Bytef *source, uLong sourceLen);
88 typedef int (*inflateEnd_ft) (z_streamp strm);
89 typedef int (*inflate_ft) (z_streamp strm, int flush);
90 typedef int (*inflateInit__ft) (z_streamp strm,
91 const char *version, int stream_size);
92 typedef int (*deflateEnd_ft) (z_streamp strm);
93 typedef int (*deflate_ft) (z_streamp strm, int flush);
94 typedef int (*deflateInit__ft) (z_streamp strm, int level,
95 const char *version, int stream_size);
96 typedef const char *(*zError__ft) (int err);
97 static compress_ft p_compress = NULL;
98 static inflateEnd_ft p_inflateEnd = NULL;
99 static inflate_ft p_inflate = NULL;
100 static inflateInit__ft p_inflateInit_ = NULL;
101 static deflateEnd_ft p_deflateEnd = NULL;
102 static deflate_ft p_deflate = NULL;
103 static deflateInit__ft p_deflateInit_ = NULL;
104 static zError__ft p_zError = NULL;
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;
207 static CRYPTO_ONCE zlib_once = CRYPTO_ONCE_STATIC_INIT;
208 DEFINE_RUN_ONCE_STATIC(ossl_comp_zlib_init)
211 /* LIBZ may be externally defined, and we should respect that value */
213 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
214 # define LIBZ "ZLIB1"
215 # elif defined(OPENSSL_SYS_VMS)
222 zlib_dso = DSO_load(NULL, LIBZ, NULL, 0);
223 if (zlib_dso != NULL) {
224 p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
225 p_inflateEnd = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
226 p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
227 p_inflateInit_ = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
228 p_deflateEnd = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
229 p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
230 p_deflateInit_ = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
231 p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
233 if (p_compress == NULL || p_inflateEnd == NULL
234 || p_inflate == NULL || p_inflateInit_ == NULL
235 || p_deflateEnd == NULL || p_deflate == NULL
236 || p_deflateInit_ == NULL || p_zError == NULL) {
237 ossl_comp_zlib_cleanup();
246 COMP_METHOD *COMP_zlib(void)
248 COMP_METHOD *meth = &zlib_method_nozlib;
251 if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
252 meth = &zlib_stateful_method;
258 /* Also called from OPENSSL_cleanup() */
259 void ossl_comp_zlib_cleanup(void)
269 /* Zlib based compression/decompression filter BIO */
272 unsigned char *ibuf; /* Input buffer */
273 int ibufsize; /* Buffer size */
274 z_stream zin; /* Input decompress context */
275 unsigned char *obuf; /* Output buffer */
276 int obufsize; /* Output buffer size */
277 unsigned char *optr; /* Position in output buffer */
278 int ocount; /* Amount of data in output buffer */
279 int odone; /* deflate EOF */
280 int comp_level; /* Compression level to use */
281 z_stream zout; /* Output compression context */
284 # define ZLIB_DEFAULT_BUFSIZE 1024
286 static int bio_zlib_new(BIO *bi);
287 static int bio_zlib_free(BIO *bi);
288 static int bio_zlib_read(BIO *b, char *out, int outl);
289 static int bio_zlib_write(BIO *b, const char *in, int inl);
290 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
291 static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
293 static const BIO_METHOD bio_meth_zlib = {
300 NULL, /* bio_zlib_puts, */
301 NULL, /* bio_zlib_gets, */
305 bio_zlib_callback_ctrl
308 const BIO_METHOD *BIO_f_zlib(void)
310 return &bio_meth_zlib;
313 static int bio_zlib_new(BIO *bi)
318 if (!RUN_ONCE(&zlib_once, ossl_comp_zlib_init)) {
319 ERR_raise(ERR_LIB_COMP, COMP_R_ZLIB_NOT_SUPPORTED);
323 ctx = OPENSSL_zalloc(sizeof(*ctx));
325 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
328 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
329 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
330 ctx->zin.zalloc = Z_NULL;
331 ctx->zin.zfree = Z_NULL;
332 ctx->zout.zalloc = Z_NULL;
333 ctx->zout.zfree = Z_NULL;
334 ctx->comp_level = Z_DEFAULT_COMPRESSION;
336 BIO_set_data(bi, ctx);
341 static int bio_zlib_free(BIO *bi)
347 ctx = BIO_get_data(bi);
349 /* Destroy decompress context */
350 inflateEnd(&ctx->zin);
351 OPENSSL_free(ctx->ibuf);
354 /* Destroy compress context */
355 deflateEnd(&ctx->zout);
356 OPENSSL_free(ctx->obuf);
359 BIO_set_data(bi, NULL);
365 static int bio_zlib_read(BIO *b, char *out, int outl)
370 BIO *next = BIO_next(b);
374 ctx = BIO_get_data(b);
376 BIO_clear_retry_flags(b);
378 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
379 if (ctx->ibuf == NULL) {
380 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
383 if ((ret = inflateInit(zin)) != Z_OK) {
384 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
385 "zlib error: %s", zError(ret));
388 zin->next_in = ctx->ibuf;
392 /* Copy output data directly to supplied buffer */
393 zin->next_out = (unsigned char *)out;
394 zin->avail_out = (unsigned int)outl;
396 /* Decompress while data available */
397 while (zin->avail_in) {
398 ret = inflate(zin, 0);
399 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
400 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
401 "zlib error: %s", zError(ret));
404 /* If EOF or we've read everything then return */
405 if ((ret == Z_STREAM_END) || !zin->avail_out)
406 return outl - zin->avail_out;
410 * No data in input buffer try to read some in, if an error then
411 * return the total data read.
413 ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
415 /* Total data read */
416 int tot = outl - zin->avail_out;
417 BIO_copy_next_retry(b);
419 return (tot > 0) ? tot : ret;
423 zin->next_in = ctx->ibuf;
427 static int bio_zlib_write(BIO *b, const char *in, int inl)
432 BIO *next = BIO_next(b);
436 ctx = BIO_get_data(b);
440 BIO_clear_retry_flags(b);
442 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
443 /* Need error here */
444 if (ctx->obuf == NULL) {
445 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
448 ctx->optr = ctx->obuf;
450 if ((ret = deflateInit(zout, ctx->comp_level)) != Z_OK) {
451 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
452 "zlib error: %s", zError(ret));
455 zout->next_out = ctx->obuf;
456 zout->avail_out = ctx->obufsize;
458 /* Obtain input data directly from supplied buffer */
459 zout->next_in = (void *)in;
460 zout->avail_in = inl;
462 /* If data in output buffer write it first */
463 while (ctx->ocount) {
464 ret = BIO_write(next, ctx->optr, ctx->ocount);
466 /* Total data written */
467 int tot = inl - zout->avail_in;
468 BIO_copy_next_retry(b);
470 return (tot > 0) ? tot : ret;
477 /* Have we consumed all supplied data? */
481 /* Compress some more */
484 ctx->optr = ctx->obuf;
485 zout->next_out = ctx->obuf;
486 zout->avail_out = ctx->obufsize;
487 /* Compress some more */
488 ret = deflate(zout, 0);
490 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
491 "zlib error: %s", zError(ret));
494 ctx->ocount = ctx->obufsize - zout->avail_out;
498 static int bio_zlib_flush(BIO *b)
503 BIO *next = BIO_next(b);
505 ctx = BIO_get_data(b);
506 /* If no data written or already flush show success */
507 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
510 BIO_clear_retry_flags(b);
511 /* No more input data */
512 zout->next_in = NULL;
515 /* If data in output buffer write it first */
516 while (ctx->ocount) {
517 ret = BIO_write(next, ctx->optr, ctx->ocount);
519 BIO_copy_next_retry(b);
528 /* Compress some more */
531 ctx->optr = ctx->obuf;
532 zout->next_out = ctx->obuf;
533 zout->avail_out = ctx->obufsize;
534 /* Compress some more */
535 ret = deflate(zout, Z_FINISH);
536 if (ret == Z_STREAM_END)
538 else if (ret != Z_OK) {
539 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
540 "zlib error: %s", zError(ret));
543 ctx->ocount = ctx->obufsize - zout->avail_out;
547 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
552 BIO *next = BIO_next(b);
556 ctx = BIO_get_data(b);
566 ret = bio_zlib_flush(b);
568 ret = BIO_flush(next);
571 case BIO_C_SET_BUFF_SIZE:
586 OPENSSL_free(ctx->ibuf);
592 OPENSSL_free(ctx->obuf);
599 case BIO_C_DO_STATE_MACHINE:
600 BIO_clear_retry_flags(b);
601 ret = BIO_ctrl(next, cmd, num, ptr);
602 BIO_copy_next_retry(b);
605 case BIO_CTRL_WPENDING:
606 if (ctx->obuf == NULL)
614 /* Unknown amount pending but we are not finished */
618 ret = BIO_ctrl(next, cmd, num, ptr);
621 case BIO_CTRL_PENDING:
622 ret = ctx->zin.avail_in;
624 ret = BIO_ctrl(next, cmd, num, ptr);
628 ret = BIO_ctrl(next, cmd, num, ptr);
636 static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
638 BIO *next = BIO_next(b);
642 return BIO_callback_ctrl(next, cmd, fp);