]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/comp/c_zlib.c
Add RUN_ONCE support to zlib init
[thirdparty/openssl.git] / crypto / comp / c_zlib.c
CommitLineData
62867571 1/*
3c2bdd7d 2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
9a555706 3 *
48f66f81 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
9a555706
RS
8 */
9
dfeab068
RE
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
ec577822 13#include <openssl/objects.h>
02a247e0 14#include "internal/comp.h"
bdbc9b4d 15#include <openssl/err.h>
25f2138b 16#include "crypto/cryptlib.h"
222e620b 17#include "internal/bio.h"
309c6fba 18#include "internal/thread_once.h"
706457b7 19#include "comp_local.h"
dfeab068 20
0f113f3e
MC
21COMP_METHOD *COMP_zlib(void);
22
23static COMP_METHOD zlib_method_nozlib = {
24 NID_undef,
25 "(undef)",
26 NULL,
27 NULL,
28 NULL,
29 NULL,
0f113f3e 30};
dfeab068 31
c4438dc0 32#ifndef ZLIB
0f113f3e 33# undef ZLIB_SHARED
c4438dc0 34#else
dfeab068 35
0f113f3e 36# include <zlib.h>
dfeab068 37
86a62cf1
RL
38static int zlib_stateful_init(COMP_CTX *ctx);
39static void zlib_stateful_finish(COMP_CTX *ctx);
40static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
0f113f3e
MC
41 unsigned int olen, unsigned char *in,
42 unsigned int ilen);
86a62cf1 43static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
0f113f3e
MC
44 unsigned int olen, unsigned char *in,
45 unsigned int ilen);
30e5e8ac 46
0d4fb843 47/* memory allocations functions for zlib initialisation */
0f113f3e 48static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
30e5e8ac 49{
0f113f3e 50 void *p;
30e5e8ac 51
b51bce94 52 p = OPENSSL_zalloc(no * size);
0f113f3e
MC
53 return p;
54}
30e5e8ac 55
0f113f3e 56static void zlib_zfree(void *opaque, void *address)
30e5e8ac 57{
0f113f3e 58 OPENSSL_free(address);
30e5e8ac
NL
59}
60
0f113f3e
MC
61
62static COMP_METHOD zlib_stateful_method = {
63 NID_zlib_compression,
64 LN_zlib_compression,
65 zlib_stateful_init,
66 zlib_stateful_finish,
67 zlib_stateful_compress_block,
121ee399 68 zlib_stateful_expand_block
0f113f3e
MC
69};
70
71/*
20f88b9b
RL
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
ad2695b1 75 * and we do not link to a .LIB file when ZLIB_SHARED is set.
20f88b9b 76 */
0f113f3e
MC
77# if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
78# include <windows.h>
79# endif /* !(OPENSSL_SYS_WINDOWS ||
80 * OPENSSL_SYS_WIN32) */
c4438dc0 81
0f113f3e 82# ifdef ZLIB_SHARED
921de151 83# include "internal/dso.h"
20f88b9b 84
20f88b9b 85/* Function pointers */
0f113f3e
MC
86typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
87 const Bytef *source, uLong sourceLen);
88typedef int (*inflateEnd_ft) (z_streamp strm);
89typedef int (*inflate_ft) (z_streamp strm, int flush);
90typedef int (*inflateInit__ft) (z_streamp strm,
91 const char *version, int stream_size);
92typedef int (*deflateEnd_ft) (z_streamp strm);
93typedef int (*deflate_ft) (z_streamp strm, int flush);
94typedef int (*deflateInit__ft) (z_streamp strm, int level,
95 const char *version, int stream_size);
96typedef const char *(*zError__ft) (int err);
97static compress_ft p_compress = NULL;
98static inflateEnd_ft p_inflateEnd = NULL;
99static inflate_ft p_inflate = NULL;
100static inflateInit__ft p_inflateInit_ = NULL;
101static deflateEnd_ft p_deflateEnd = NULL;
102static deflate_ft p_deflate = NULL;
103static deflateInit__ft p_deflateInit_ = NULL;
104static zError__ft p_zError = NULL;
20f88b9b 105
c4438dc0 106static DSO *zlib_dso = NULL;
20f88b9b 107
0f113f3e
MC
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 */
117
118struct zlib_state {
119 z_stream istream;
120 z_stream ostream;
121};
86a62cf1 122
86a62cf1 123static int zlib_stateful_init(COMP_CTX *ctx)
0f113f3e
MC
124{
125 int err;
64b25758 126 struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
0f113f3e
MC
127
128 if (state == NULL)
129 goto err;
130
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;
0f113f3e
MC
136 err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
137 if (err != Z_OK)
138 goto err;
139
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;
0f113f3e
MC
145 err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
146 ZLIB_VERSION, sizeof(z_stream));
147 if (err != Z_OK)
148 goto err;
149
121ee399 150 ctx->data = state;
0f113f3e 151 return 1;
86a62cf1 152 err:
b548a1f1 153 OPENSSL_free(state);
0f113f3e
MC
154 return 0;
155}
86a62cf1
RL
156
157static void zlib_stateful_finish(COMP_CTX *ctx)
0f113f3e 158{
121ee399 159 struct zlib_state *state = ctx->data;
0f113f3e
MC
160 inflateEnd(&state->istream);
161 deflateEnd(&state->ostream);
162 OPENSSL_free(state);
0f113f3e 163}
86a62cf1
RL
164
165static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
0f113f3e
MC
166 unsigned int olen, unsigned char *in,
167 unsigned int ilen)
168{
169 int err = Z_OK;
121ee399 170 struct zlib_state *state = ctx->data;
0f113f3e
MC
171
172 if (state == NULL)
173 return -1;
174
175 state->ostream.next_in = in;
176 state->ostream.avail_in = ilen;
177 state->ostream.next_out = out;
178 state->ostream.avail_out = olen;
179 if (ilen > 0)
180 err = deflate(&state->ostream, Z_SYNC_FLUSH);
181 if (err != Z_OK)
182 return -1;
0f113f3e
MC
183 return olen - state->ostream.avail_out;
184}
86a62cf1
RL
185
186static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
0f113f3e
MC
187 unsigned int olen, unsigned char *in,
188 unsigned int ilen)
189{
190 int err = Z_OK;
121ee399 191 struct zlib_state *state = ctx->data;
0f113f3e
MC
192
193 if (state == NULL)
194 return 0;
195
196 state->istream.next_in = in;
197 state->istream.avail_in = ilen;
198 state->istream.next_out = out;
199 state->istream.avail_out = olen;
200 if (ilen > 0)
201 err = inflate(&state->istream, Z_SYNC_FLUSH);
202 if (err != Z_OK)
203 return -1;
0f113f3e
MC
204 return olen - state->istream.avail_out;
205}
86a62cf1 206
309c6fba
TS
207static CRYPTO_ONCE zlib_once = CRYPTO_ONCE_STATIC_INIT;
208DEFINE_RUN_ONCE_STATIC(ossl_comp_zlib_init)
0f113f3e 209{
309c6fba 210# ifdef ZLIB_SHARED
5a5c0b95 211 /* LIBZ may be externally defined, and we should respect that value */
309c6fba
TS
212# ifndef LIBZ
213# if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
214# define LIBZ "ZLIB1"
215# elif defined(OPENSSL_SYS_VMS)
216# define LIBZ "LIBZ"
217# else
218# define LIBZ "z"
219# endif
5a5c0b95 220# endif
5a5c0b95 221
309c6fba
TS
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");
232
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();
238 return 0;
0f113f3e
MC
239 }
240 }
309c6fba
TS
241# endif
242 return 1;
243}
e984b2af 244#endif
309c6fba
TS
245
246COMP_METHOD *COMP_zlib(void)
247{
248 COMP_METHOD *meth = &zlib_method_nozlib;
249
250#ifdef ZLIB
251 if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
252 meth = &zlib_stateful_method;
20f88b9b
RL
253#endif
254
26a7d938 255 return meth;
0f113f3e 256}
20f88b9b 257
309c6fba 258/* Also called from OPENSSL_cleanup() */
f148f703 259void ossl_comp_zlib_cleanup(void)
0f113f3e 260{
8931b30d 261#ifdef ZLIB_SHARED
1b6fa9fd 262 DSO_free(zlib_dso);
8cbb1533 263 zlib_dso = NULL;
8931b30d 264#endif
0f113f3e 265}
8931b30d
DSH
266
267#ifdef ZLIB
268
269/* Zlib based compression/decompression filter BIO */
270
0f113f3e
MC
271typedef struct {
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 */
282} BIO_ZLIB_CTX;
283
284# define ZLIB_DEFAULT_BUFSIZE 1024
8931b30d
DSH
285
286static int bio_zlib_new(BIO *bi);
287static int bio_zlib_free(BIO *bi);
288static int bio_zlib_read(BIO *b, char *out, int outl);
289static int bio_zlib_write(BIO *b, const char *in, int inl);
290static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
fce78bd4 291static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
8931b30d 292
04f6b0fd 293static const BIO_METHOD bio_meth_zlib = {
0f113f3e
MC
294 BIO_TYPE_COMP,
295 "zlib",
513d76f4
MC
296 /* TODO: Convert to new style write function */
297 bwrite_conv,
0f113f3e 298 bio_zlib_write,
513d76f4
MC
299 /* TODO: Convert to new style read function */
300 bread_conv,
0f113f3e 301 bio_zlib_read,
b4ff6622
DB
302 NULL, /* bio_zlib_puts, */
303 NULL, /* bio_zlib_gets, */
0f113f3e
MC
304 bio_zlib_ctrl,
305 bio_zlib_new,
306 bio_zlib_free,
307 bio_zlib_callback_ctrl
308};
8931b30d 309
04f6b0fd 310const BIO_METHOD *BIO_f_zlib(void)
0f113f3e
MC
311{
312 return &bio_meth_zlib;
313}
8931b30d
DSH
314
315static int bio_zlib_new(BIO *bi)
0f113f3e
MC
316{
317 BIO_ZLIB_CTX *ctx;
309c6fba 318
0f113f3e 319# ifdef ZLIB_SHARED
309c6fba 320 if (!RUN_ONCE(&zlib_once, ossl_comp_zlib_init)) {
9311d0c4 321 ERR_raise(ERR_LIB_COMP, COMP_R_ZLIB_NOT_SUPPORTED);
0f113f3e
MC
322 return 0;
323 }
324# endif
64b25758 325 ctx = OPENSSL_zalloc(sizeof(*ctx));
90945fa3 326 if (ctx == NULL) {
9311d0c4 327 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
328 return 0;
329 }
0f113f3e
MC
330 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
331 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
332 ctx->zin.zalloc = Z_NULL;
333 ctx->zin.zfree = Z_NULL;
0f113f3e
MC
334 ctx->zout.zalloc = Z_NULL;
335 ctx->zout.zfree = Z_NULL;
0f113f3e 336 ctx->comp_level = Z_DEFAULT_COMPRESSION;
222e620b
MC
337 BIO_set_init(bi, 1);
338 BIO_set_data(bi, ctx);
339
0f113f3e
MC
340 return 1;
341}
8931b30d
DSH
342
343static int bio_zlib_free(BIO *bi)
0f113f3e
MC
344{
345 BIO_ZLIB_CTX *ctx;
309c6fba 346
0f113f3e
MC
347 if (!bi)
348 return 0;
222e620b 349 ctx = BIO_get_data(bi);
0f113f3e
MC
350 if (ctx->ibuf) {
351 /* Destroy decompress context */
352 inflateEnd(&ctx->zin);
353 OPENSSL_free(ctx->ibuf);
354 }
355 if (ctx->obuf) {
356 /* Destroy compress context */
357 deflateEnd(&ctx->zout);
358 OPENSSL_free(ctx->obuf);
359 }
360 OPENSSL_free(ctx);
222e620b
MC
361 BIO_set_data(bi, NULL);
362 BIO_set_init(bi, 0);
363
0f113f3e
MC
364 return 1;
365}
8931b30d
DSH
366
367static int bio_zlib_read(BIO *b, char *out, int outl)
0f113f3e
MC
368{
369 BIO_ZLIB_CTX *ctx;
370 int ret;
371 z_stream *zin;
222e620b
MC
372 BIO *next = BIO_next(b);
373
0f113f3e
MC
374 if (!out || !outl)
375 return 0;
222e620b 376 ctx = BIO_get_data(b);
0f113f3e
MC
377 zin = &ctx->zin;
378 BIO_clear_retry_flags(b);
379 if (!ctx->ibuf) {
380 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
90945fa3 381 if (ctx->ibuf == NULL) {
9311d0c4 382 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
383 return 0;
384 }
385 inflateInit(zin);
386 zin->next_in = ctx->ibuf;
387 zin->avail_in = 0;
388 }
389
390 /* Copy output data directly to supplied buffer */
391 zin->next_out = (unsigned char *)out;
392 zin->avail_out = (unsigned int)outl;
393 for (;;) {
394 /* Decompress while data available */
395 while (zin->avail_in) {
396 ret = inflate(zin, 0);
397 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
a150f8e1
RL
398 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
399 "zlib error: %s", zError(ret));
0f113f3e
MC
400 return 0;
401 }
402 /* If EOF or we've read everything then return */
403 if ((ret == Z_STREAM_END) || !zin->avail_out)
404 return outl - zin->avail_out;
405 }
406
407 /*
408 * No data in input buffer try to read some in, if an error then
409 * return the total data read.
410 */
222e620b 411 ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
0f113f3e
MC
412 if (ret <= 0) {
413 /* Total data read */
414 int tot = outl - zin->avail_out;
415 BIO_copy_next_retry(b);
416 if (ret < 0)
417 return (tot > 0) ? tot : ret;
418 return tot;
419 }
420 zin->avail_in = ret;
421 zin->next_in = ctx->ibuf;
422 }
423}
8931b30d
DSH
424
425static int bio_zlib_write(BIO *b, const char *in, int inl)
0f113f3e
MC
426{
427 BIO_ZLIB_CTX *ctx;
428 int ret;
429 z_stream *zout;
222e620b
MC
430 BIO *next = BIO_next(b);
431
0f113f3e
MC
432 if (!in || !inl)
433 return 0;
222e620b 434 ctx = BIO_get_data(b);
0f113f3e
MC
435 if (ctx->odone)
436 return 0;
437 zout = &ctx->zout;
438 BIO_clear_retry_flags(b);
439 if (!ctx->obuf) {
440 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
441 /* Need error here */
90945fa3 442 if (ctx->obuf == NULL) {
9311d0c4 443 ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
444 return 0;
445 }
446 ctx->optr = ctx->obuf;
447 ctx->ocount = 0;
448 deflateInit(zout, ctx->comp_level);
449 zout->next_out = ctx->obuf;
450 zout->avail_out = ctx->obufsize;
451 }
452 /* Obtain input data directly from supplied buffer */
453 zout->next_in = (void *)in;
454 zout->avail_in = inl;
455 for (;;) {
456 /* If data in output buffer write it first */
457 while (ctx->ocount) {
222e620b 458 ret = BIO_write(next, ctx->optr, ctx->ocount);
0f113f3e
MC
459 if (ret <= 0) {
460 /* Total data written */
461 int tot = inl - zout->avail_in;
462 BIO_copy_next_retry(b);
463 if (ret < 0)
464 return (tot > 0) ? tot : ret;
465 return tot;
466 }
467 ctx->optr += ret;
468 ctx->ocount -= ret;
469 }
470
471 /* Have we consumed all supplied data? */
472 if (!zout->avail_in)
473 return inl;
474
475 /* Compress some more */
476
477 /* Reset buffer */
478 ctx->optr = ctx->obuf;
479 zout->next_out = ctx->obuf;
480 zout->avail_out = ctx->obufsize;
481 /* Compress some more */
482 ret = deflate(zout, 0);
483 if (ret != Z_OK) {
a150f8e1
RL
484 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
485 "zlib error: %s", zError(ret));
0f113f3e
MC
486 return 0;
487 }
488 ctx->ocount = ctx->obufsize - zout->avail_out;
489 }
490}
8931b30d
DSH
491
492static int bio_zlib_flush(BIO *b)
0f113f3e
MC
493{
494 BIO_ZLIB_CTX *ctx;
495 int ret;
496 z_stream *zout;
222e620b
MC
497 BIO *next = BIO_next(b);
498
499 ctx = BIO_get_data(b);
0f113f3e
MC
500 /* If no data written or already flush show success */
501 if (!ctx->obuf || (ctx->odone && !ctx->ocount))
502 return 1;
503 zout = &ctx->zout;
504 BIO_clear_retry_flags(b);
505 /* No more input data */
506 zout->next_in = NULL;
507 zout->avail_in = 0;
508 for (;;) {
509 /* If data in output buffer write it first */
510 while (ctx->ocount) {
222e620b 511 ret = BIO_write(next, ctx->optr, ctx->ocount);
0f113f3e
MC
512 if (ret <= 0) {
513 BIO_copy_next_retry(b);
514 return ret;
515 }
516 ctx->optr += ret;
517 ctx->ocount -= ret;
518 }
519 if (ctx->odone)
520 return 1;
521
522 /* Compress some more */
523
524 /* Reset buffer */
525 ctx->optr = ctx->obuf;
526 zout->next_out = ctx->obuf;
527 zout->avail_out = ctx->obufsize;
528 /* Compress some more */
529 ret = deflate(zout, Z_FINISH);
530 if (ret == Z_STREAM_END)
531 ctx->odone = 1;
532 else if (ret != Z_OK) {
a150f8e1
RL
533 ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
534 "zlib error: %s", zError(ret));
0f113f3e
MC
535 return 0;
536 }
537 ctx->ocount = ctx->obufsize - zout->avail_out;
538 }
539}
8931b30d
DSH
540
541static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
542{
543 BIO_ZLIB_CTX *ctx;
544 int ret, *ip;
545 int ibs, obs;
222e620b
MC
546 BIO *next = BIO_next(b);
547
548 if (next == NULL)
0f113f3e 549 return 0;
222e620b 550 ctx = BIO_get_data(b);
0f113f3e
MC
551 switch (cmd) {
552
553 case BIO_CTRL_RESET:
554 ctx->ocount = 0;
555 ctx->odone = 0;
556 ret = 1;
557 break;
558
559 case BIO_CTRL_FLUSH:
560 ret = bio_zlib_flush(b);
561 if (ret > 0)
222e620b 562 ret = BIO_flush(next);
0f113f3e
MC
563 break;
564
565 case BIO_C_SET_BUFF_SIZE:
566 ibs = -1;
567 obs = -1;
568 if (ptr != NULL) {
569 ip = ptr;
570 if (*ip == 0)
571 ibs = (int)num;
572 else
573 obs = (int)num;
574 } else {
575 ibs = (int)num;
576 obs = ibs;
577 }
578
579 if (ibs != -1) {
b548a1f1
RS
580 OPENSSL_free(ctx->ibuf);
581 ctx->ibuf = NULL;
0f113f3e
MC
582 ctx->ibufsize = ibs;
583 }
584
585 if (obs != -1) {
b548a1f1
RS
586 OPENSSL_free(ctx->obuf);
587 ctx->obuf = NULL;
0f113f3e
MC
588 ctx->obufsize = obs;
589 }
590 ret = 1;
591 break;
592
593 case BIO_C_DO_STATE_MACHINE:
594 BIO_clear_retry_flags(b);
222e620b 595 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
596 BIO_copy_next_retry(b);
597 break;
598
6beb8b39
TM
599 case BIO_CTRL_WPENDING:
600 if (ctx->obuf == NULL)
601 return 0;
602
603 if (ctx->odone) {
604 ret = ctx->ocount;
605 } else {
606 ret = ctx->ocount;
607 if (ret == 0)
608 /* Unknown amount pending but we are not finished */
609 ret = 1;
610 }
611 if (ret == 0)
612 ret = BIO_ctrl(next, cmd, num, ptr);
613 break;
614
615 case BIO_CTRL_PENDING:
616 ret = ctx->zin.avail_in;
617 if (ret == 0)
618 ret = BIO_ctrl(next, cmd, num, ptr);
619 break;
620
0f113f3e 621 default:
222e620b 622 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
623 break;
624
625 }
8931b30d 626
0f113f3e
MC
627 return ret;
628}
8931b30d 629
fce78bd4 630static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e 631{
222e620b 632 BIO *next = BIO_next(b);
309c6fba 633
222e620b 634 if (next == NULL)
0f113f3e 635 return 0;
222e620b 636 return BIO_callback_ctrl(next, cmd, fp);
0f113f3e 637}
8931b30d
DSH
638
639#endif