Coverage Report

Created: 2026-09-17 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/comp/c_zlib.c
Line
Count
Source
1
/*
2
 * Copyright 1998-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <string.h>
13
#include <openssl/objects.h>
14
#include "internal/e_os.h"
15
#include "internal/comp.h"
16
#include <openssl/err.h>
17
#include "crypto/cryptlib.h"
18
#include "internal/bio.h"
19
#include "internal/thread_once.h"
20
#include "comp_local.h"
21
22
COMP_METHOD *COMP_zlib(void);
23
24
#ifdef OPENSSL_NO_ZLIB
25
#undef ZLIB_SHARED
26
#else
27
28
#include <zlib.h>
29
30
static int zlib_stateful_init(COMP_CTX *ctx);
31
static void zlib_stateful_finish(COMP_CTX *ctx);
32
static ossl_ssize_t zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
33
    size_t olen, unsigned char *in,
34
    size_t ilen);
35
static ossl_ssize_t zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
36
    size_t olen, unsigned char *in,
37
    size_t ilen);
38
39
/* memory allocations functions for zlib initialisation */
40
static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
41
{
42
    void *p;
43
44
    p = OPENSSL_calloc(no, size);
45
    return p;
46
}
47
48
static void zlib_zfree(void *opaque, void *address)
49
{
50
    OPENSSL_free(address);
51
}
52
53
static COMP_METHOD zlib_stateful_method = {
54
    NID_zlib_compression,
55
    LN_zlib_compression,
56
    zlib_stateful_init,
57
    zlib_stateful_finish,
58
    zlib_stateful_compress_block,
59
    zlib_stateful_expand_block
60
};
61
62
/*
63
 * When OpenSSL is built on Windows, we do not want to require that
64
 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
65
 * work.  Therefore, all ZLIB routines are loaded at run time
66
 * and we do not link to a .LIB file when ZLIB_SHARED is set.
67
 */
68
69
#ifdef ZLIB_SHARED
70
#include "internal/dso.h"
71
72
/* Function pointers */
73
typedef int (*compress_ft)(Bytef *dest, uLongf *destLen,
74
    const Bytef *source, uLong sourceLen);
75
typedef int (*uncompress_ft)(Bytef *dest, uLongf *destLen,
76
    const Bytef *source, uLong sourceLen);
77
typedef int (*inflateEnd_ft)(z_streamp strm);
78
typedef int (*inflate_ft)(z_streamp strm, int flush);
79
typedef int (*inflateInit__ft)(z_streamp strm,
80
    const char *version, int stream_size);
81
typedef int (*deflateEnd_ft)(z_streamp strm);
82
typedef int (*deflate_ft)(z_streamp strm, int flush);
83
typedef int (*deflateInit__ft)(z_streamp strm, int level,
84
    const char *version, int stream_size);
85
typedef const char *(*zError__ft)(int err);
86
static compress_ft p_compress = NULL;
87
static uncompress_ft p_uncompress = NULL;
88
static inflateEnd_ft p_inflateEnd = NULL;
89
static inflate_ft p_inflate = NULL;
90
static inflateInit__ft p_inflateInit_ = NULL;
91
static deflateEnd_ft p_deflateEnd = NULL;
92
static deflate_ft p_deflate = NULL;
93
static deflateInit__ft p_deflateInit_ = NULL;
94
static zError__ft p_zError = NULL;
95
96
static DSO *zlib_dso = NULL;
97
98
#define compress p_compress
99
#define uncompress p_uncompress
100
#define inflateEnd p_inflateEnd
101
#define inflate p_inflate
102
#define inflateInit_ p_inflateInit_
103
#define deflateEnd p_deflateEnd
104
#define deflate p_deflate
105
#define deflateInit_ p_deflateInit_
106
#define zError p_zError
107
#endif /* ZLIB_SHARED */
108
109
struct zlib_state {
110
    z_stream istream;
111
    z_stream ostream;
112
};
113
114
static int zlib_stateful_init(COMP_CTX *ctx)
115
{
116
    int err;
117
    struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
118
119
    if (state == NULL)
120
        goto err;
121
122
    state->istream.zalloc = zlib_zalloc;
123
    state->istream.zfree = zlib_zfree;
124
    state->istream.opaque = Z_NULL;
125
    state->istream.next_in = Z_NULL;
126
    state->istream.next_out = Z_NULL;
127
    err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
128
    if (err != Z_OK)
129
        goto err;
130
131
    state->ostream.zalloc = zlib_zalloc;
132
    state->ostream.zfree = zlib_zfree;
133
    state->ostream.opaque = Z_NULL;
134
    state->ostream.next_in = Z_NULL;
135
    state->ostream.next_out = Z_NULL;
136
    err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
137
        ZLIB_VERSION, sizeof(z_stream));
138
    if (err != Z_OK)
139
        goto err;
140
141
    ctx->data = state;
142
    return 1;
143
err:
144
    OPENSSL_free(state);
145
    return 0;
146
}
147
148
static void zlib_stateful_finish(COMP_CTX *ctx)
149
{
150
    struct zlib_state *state = ctx->data;
151
    inflateEnd(&state->istream);
152
    deflateEnd(&state->ostream);
153
    OPENSSL_free(state);
154
}
155
156
static ossl_ssize_t zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
157
    size_t olen, unsigned char *in,
158
    size_t ilen)
159
{
160
    int err = Z_OK;
161
    struct zlib_state *state = ctx->data;
162
163
    if (state == NULL)
164
        return -1;
165
166
    state->ostream.next_in = in;
167
    state->ostream.avail_in = ilen;
168
    state->ostream.next_out = out;
169
    state->ostream.avail_out = olen;
170
    if (ilen > 0)
171
        err = deflate(&state->ostream, Z_SYNC_FLUSH);
172
    if (err != Z_OK)
173
        return -1;
174
    if (state->ostream.avail_out > olen)
175
        return -1;
176
    return (ossl_ssize_t)(olen - state->ostream.avail_out);
177
}
178
179
static ossl_ssize_t zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
180
    size_t olen, unsigned char *in,
181
    size_t ilen)
182
{
183
    int err = Z_OK;
184
    struct zlib_state *state = ctx->data;
185
186
    if (state == NULL)
187
        return 0;
188
189
    state->istream.next_in = in;
190
    state->istream.avail_in = ilen;
191
    state->istream.next_out = out;
192
    state->istream.avail_out = olen;
193
    if (ilen > 0)
194
        err = inflate(&state->istream, Z_SYNC_FLUSH);
195
    if (err != Z_OK)
196
        return -1;
197
    if (state->istream.avail_out > olen)
198
        return -1;
199
    return (ossl_ssize_t)(olen - state->istream.avail_out);
200
}
201
202
/* ONESHOT COMPRESSION/DECOMPRESSION */
203
204
static int zlib_oneshot_init(COMP_CTX *ctx)
205
{
206
    return 1;
207
}
208
209
static void zlib_oneshot_finish(COMP_CTX *ctx)
210
{
211
}
212
213
static ossl_ssize_t zlib_oneshot_compress_block(COMP_CTX *ctx, unsigned char *out,
214
    size_t olen, unsigned char *in,
215
    size_t ilen)
216
{
217
    uLongf out_size;
218
219
    if (ilen == 0)
220
        return 0;
221
222
    /* zlib's uLongf defined as unsigned long FAR */
223
    if (olen > ULONG_MAX)
224
        return -1;
225
    out_size = (uLongf)olen;
226
227
    if (compress(out, &out_size, in, ilen) != Z_OK)
228
        return -1;
229
230
    if (out_size > OSSL_SSIZE_MAX)
231
        return -1;
232
    return (ossl_ssize_t)out_size;
233
}
234
235
static ossl_ssize_t zlib_oneshot_expand_block(COMP_CTX *ctx, unsigned char *out,
236
    size_t olen, unsigned char *in,
237
    size_t ilen)
238
{
239
    uLongf out_size;
240
241
    if (ilen == 0)
242
        return 0;
243
244
    /* zlib's uLongf defined as unsigned long FAR */
245
    if (olen > ULONG_MAX)
246
        return -1;
247
    out_size = (uLongf)olen;
248
249
    if (uncompress(out, &out_size, in, ilen) != Z_OK)
250
        return -1;
251
252
    if (out_size > OSSL_SSIZE_MAX)
253
        return -1;
254
    return (ossl_ssize_t)out_size;
255
}
256
257
static COMP_METHOD zlib_oneshot_method = {
258
    NID_zlib_compression,
259
    LN_zlib_compression,
260
    zlib_oneshot_init,
261
    zlib_oneshot_finish,
262
    zlib_oneshot_compress_block,
263
    zlib_oneshot_expand_block
264
};
265
266
static CRYPTO_ONCE zlib_once = CRYPTO_ONCE_STATIC_INIT;
267
DEFINE_RUN_ONCE_STATIC(ossl_comp_zlib_init)
268
{
269
#ifdef ZLIB_SHARED
270
    /* LIBZ may be externally defined, and we should respect that value */
271
#ifndef LIBZ
272
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
273
#define LIBZ "ZLIB1"
274
#elif defined(OPENSSL_SYS_VMS)
275
#define LIBZ "LIBZ"
276
#else
277
#define LIBZ "z"
278
#endif
279
#endif
280
281
    ERR_set_mark();
282
    zlib_dso = DSO_load(NULL, LIBZ, NULL, 0);
283
    if (zlib_dso != NULL) {
284
        p_compress = (compress_ft)DSO_bind_func(zlib_dso, "compress");
285
        p_uncompress = (uncompress_ft)DSO_bind_func(zlib_dso, "uncompress");
286
        p_inflateEnd = (inflateEnd_ft)DSO_bind_func(zlib_dso, "inflateEnd");
287
        p_inflate = (inflate_ft)DSO_bind_func(zlib_dso, "inflate");
288
        p_inflateInit_ = (inflateInit__ft)DSO_bind_func(zlib_dso, "inflateInit_");
289
        p_deflateEnd = (deflateEnd_ft)DSO_bind_func(zlib_dso, "deflateEnd");
290
        p_deflate = (deflate_ft)DSO_bind_func(zlib_dso, "deflate");
291
        p_deflateInit_ = (deflateInit__ft)DSO_bind_func(zlib_dso, "deflateInit_");
292
        p_zError = (zError__ft)DSO_bind_func(zlib_dso, "zError");
293
    }
294
295
    if (p_compress == NULL || p_uncompress == NULL || p_inflateEnd == NULL
296
        || p_inflate == NULL || p_inflateInit_ == NULL
297
        || p_deflateEnd == NULL || p_deflate == NULL
298
        || p_deflateInit_ == NULL || p_zError == NULL) {
299
        ERR_clear_last_mark();
300
        ossl_comp_zlib_cleanup();
301
        return 0;
302
    }
303
    /* Do not leave errors behind on success. */
304
    ERR_pop_to_mark();
305
#endif
306
    return 1;
307
}
308
#endif
309
310
COMP_METHOD *COMP_zlib(void)
311
16
{
312
16
    COMP_METHOD *meth = NULL;
313
314
#ifndef OPENSSL_NO_ZLIB
315
    if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
316
        meth = &zlib_stateful_method;
317
#endif
318
319
16
    return meth;
320
16
}
321
322
COMP_METHOD *COMP_zlib_oneshot(void)
323
0
{
324
0
    COMP_METHOD *meth = NULL;
325
326
#ifndef OPENSSL_NO_ZLIB
327
    if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
328
        meth = &zlib_oneshot_method;
329
#endif
330
331
0
    return meth;
332
0
}
333
334
/* Also called from OPENSSL_cleanup() */
335
void ossl_comp_zlib_cleanup(void)
336
0
{
337
#ifdef ZLIB_SHARED
338
    DSO_free(zlib_dso);
339
    zlib_dso = NULL;
340
#endif
341
0
}
342
343
#ifndef OPENSSL_NO_ZLIB
344
345
/* Zlib based compression/decompression filter BIO */
346
347
typedef struct {
348
    unsigned char *ibuf; /* Input buffer */
349
    int ibufsize; /* Buffer size */
350
    z_stream zin; /* Input decompress context */
351
    unsigned char *obuf; /* Output buffer */
352
    int obufsize; /* Output buffer size */
353
    unsigned char *optr; /* Position in output buffer */
354
    int ocount; /* Amount of data in output buffer */
355
    int odone; /* deflate EOF */
356
    int comp_level; /* Compression level to use */
357
    z_stream zout; /* Output compression context */
358
} BIO_ZLIB_CTX;
359
360
#define ZLIB_DEFAULT_BUFSIZE 1024
361
362
static int bio_zlib_new(BIO *bi);
363
static int bio_zlib_free(BIO *bi);
364
static int bio_zlib_read(BIO *b, char *out, int outl);
365
static int bio_zlib_write(BIO *b, const char *in, int inl);
366
static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
367
static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
368
369
static const BIO_METHOD bio_meth_zlib = {
370
    BIO_TYPE_COMP,
371
    "zlib",
372
    bwrite_conv,
373
    bio_zlib_write,
374
    bread_conv,
375
    bio_zlib_read,
376
    NULL, /* bio_zlib_puts, */
377
    NULL, /* bio_zlib_gets, */
378
    bio_zlib_ctrl,
379
    bio_zlib_new,
380
    bio_zlib_free,
381
    bio_zlib_callback_ctrl
382
};
383
#endif
384
385
const BIO_METHOD *BIO_f_zlib(void)
386
0
{
387
#ifndef OPENSSL_NO_ZLIB
388
    if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
389
        return &bio_meth_zlib;
390
#endif
391
    return NULL;
392
0
}
393
394
#ifndef OPENSSL_NO_ZLIB
395
static int bio_zlib_new(BIO *bi)
396
{
397
    BIO_ZLIB_CTX *ctx;
398
399
#ifdef ZLIB_SHARED
400
    if (!RUN_ONCE(&zlib_once, ossl_comp_zlib_init)) {
401
        ERR_raise(ERR_LIB_COMP, COMP_R_ZLIB_NOT_SUPPORTED);
402
        return 0;
403
    }
404
#endif
405
    ctx = OPENSSL_zalloc(sizeof(*ctx));
406
    if (ctx == NULL)
407
        return 0;
408
    ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
409
    ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
410
    ctx->zin.zalloc = Z_NULL;
411
    ctx->zin.zfree = Z_NULL;
412
    ctx->zout.zalloc = Z_NULL;
413
    ctx->zout.zfree = Z_NULL;
414
    ctx->comp_level = Z_DEFAULT_COMPRESSION;
415
    BIO_set_init(bi, 1);
416
    BIO_set_data(bi, ctx);
417
418
    return 1;
419
}
420
421
static int bio_zlib_free(BIO *bi)
422
{
423
    BIO_ZLIB_CTX *ctx;
424
425
    if (!bi)
426
        return 0;
427
    ctx = BIO_get_data(bi);
428
    if (ctx->ibuf) {
429
        /* Destroy decompress context */
430
        inflateEnd(&ctx->zin);
431
        OPENSSL_free(ctx->ibuf);
432
    }
433
    if (ctx->obuf) {
434
        /* Destroy compress context */
435
        deflateEnd(&ctx->zout);
436
        OPENSSL_free(ctx->obuf);
437
    }
438
    OPENSSL_free(ctx);
439
    BIO_set_data(bi, NULL);
440
    BIO_set_init(bi, 0);
441
442
    return 1;
443
}
444
445
static int bio_zlib_read(BIO *b, char *out, int outl)
446
{
447
    BIO_ZLIB_CTX *ctx;
448
    int ret;
449
    z_stream *zin;
450
    BIO *next = BIO_next(b);
451
452
    if (!out || !outl)
453
        return 0;
454
    ctx = BIO_get_data(b);
455
    zin = &ctx->zin;
456
    BIO_clear_retry_flags(b);
457
    if (!ctx->ibuf) {
458
        ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
459
        if (ctx->ibuf == NULL)
460
            return 0;
461
        if ((ret = inflateInit(zin)) != Z_OK) {
462
            ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
463
                "zlib error: %s", zError(ret));
464
            return 0;
465
        }
466
        zin->next_in = ctx->ibuf;
467
        zin->avail_in = 0;
468
    }
469
470
    /* Copy output data directly to supplied buffer */
471
    zin->next_out = (unsigned char *)out;
472
    zin->avail_out = (unsigned int)outl;
473
    for (;;) {
474
        /* Decompress while data available */
475
        while (zin->avail_in) {
476
            ret = inflate(zin, 0);
477
            if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
478
                ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
479
                    "zlib error: %s", zError(ret));
480
                return 0;
481
            }
482
            /* If EOF or we've read everything then return */
483
            if ((ret == Z_STREAM_END) || !zin->avail_out)
484
                return outl - zin->avail_out;
485
        }
486
487
        /*
488
         * No data in input buffer try to read some in, if an error then
489
         * return the total data read.
490
         */
491
        ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
492
        if (ret <= 0) {
493
            /* Total data read */
494
            int tot = outl - zin->avail_out;
495
            BIO_copy_next_retry(b);
496
            if (ret < 0)
497
                return (tot > 0) ? tot : ret;
498
            return tot;
499
        }
500
        zin->avail_in = ret;
501
        zin->next_in = ctx->ibuf;
502
    }
503
}
504
505
static int bio_zlib_write(BIO *b, const char *in, int inl)
506
{
507
    BIO_ZLIB_CTX *ctx;
508
    int ret;
509
    z_stream *zout;
510
    BIO *next = BIO_next(b);
511
512
    if (!in || !inl)
513
        return 0;
514
    ctx = BIO_get_data(b);
515
    if (ctx->odone)
516
        return 0;
517
    zout = &ctx->zout;
518
    BIO_clear_retry_flags(b);
519
    if (!ctx->obuf) {
520
        ctx->obuf = OPENSSL_malloc(ctx->obufsize);
521
        /* Need error here */
522
        if (ctx->obuf == NULL)
523
            return 0;
524
        ctx->optr = ctx->obuf;
525
        ctx->ocount = 0;
526
        if ((ret = deflateInit(zout, ctx->comp_level)) != Z_OK) {
527
            ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
528
                "zlib error: %s", zError(ret));
529
            return 0;
530
        }
531
        zout->next_out = ctx->obuf;
532
        zout->avail_out = ctx->obufsize;
533
    }
534
    /* Obtain input data directly from supplied buffer */
535
    zout->next_in = (void *)in;
536
    zout->avail_in = inl;
537
    for (;;) {
538
        /* If data in output buffer write it first */
539
        while (ctx->ocount) {
540
            ret = BIO_write(next, ctx->optr, ctx->ocount);
541
            if (ret <= 0) {
542
                /* Total data written */
543
                int tot = inl - zout->avail_in;
544
                BIO_copy_next_retry(b);
545
                if (ret < 0)
546
                    return (tot > 0) ? tot : ret;
547
                return tot;
548
            }
549
            ctx->optr += ret;
550
            ctx->ocount -= ret;
551
        }
552
553
        /* Have we consumed all supplied data? */
554
        if (!zout->avail_in)
555
            return inl;
556
557
        /* Compress some more */
558
559
        /* Reset buffer */
560
        ctx->optr = ctx->obuf;
561
        zout->next_out = ctx->obuf;
562
        zout->avail_out = ctx->obufsize;
563
        /* Compress some more */
564
        ret = deflate(zout, 0);
565
        if (ret != Z_OK) {
566
            ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
567
                "zlib error: %s", zError(ret));
568
            return 0;
569
        }
570
        ctx->ocount = ctx->obufsize - zout->avail_out;
571
    }
572
}
573
574
static int bio_zlib_flush(BIO *b)
575
{
576
    BIO_ZLIB_CTX *ctx;
577
    int ret;
578
    z_stream *zout;
579
    BIO *next = BIO_next(b);
580
581
    ctx = BIO_get_data(b);
582
    /* If no data written or already flush show success */
583
    if (!ctx->obuf || (ctx->odone && !ctx->ocount))
584
        return 1;
585
    zout = &ctx->zout;
586
    BIO_clear_retry_flags(b);
587
    /* No more input data */
588
    zout->next_in = NULL;
589
    zout->avail_in = 0;
590
    for (;;) {
591
        /* If data in output buffer write it first */
592
        while (ctx->ocount) {
593
            ret = BIO_write(next, ctx->optr, ctx->ocount);
594
            if (ret <= 0) {
595
                BIO_copy_next_retry(b);
596
                return ret;
597
            }
598
            ctx->optr += ret;
599
            ctx->ocount -= ret;
600
        }
601
        if (ctx->odone)
602
            return 1;
603
604
        /* Compress some more */
605
606
        /* Reset buffer */
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)
613
            ctx->odone = 1;
614
        else if (ret != Z_OK) {
615
            ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
616
                "zlib error: %s", zError(ret));
617
            return 0;
618
        }
619
        ctx->ocount = ctx->obufsize - zout->avail_out;
620
    }
621
}
622
623
static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
624
{
625
    BIO_ZLIB_CTX *ctx;
626
    int ret, *ip;
627
    int ibs, obs;
628
    BIO *next = BIO_next(b);
629
630
    if (next == NULL)
631
        return 0;
632
    ctx = BIO_get_data(b);
633
    switch (cmd) {
634
635
    case BIO_CTRL_RESET:
636
        ctx->ocount = 0;
637
        ctx->odone = 0;
638
        ret = 1;
639
        break;
640
641
    case BIO_CTRL_FLUSH:
642
        ret = bio_zlib_flush(b);
643
        if (ret > 0) {
644
            ret = BIO_flush(next);
645
            BIO_copy_next_retry(b);
646
        }
647
        break;
648
649
    case BIO_C_SET_BUFF_SIZE:
650
        ibs = -1;
651
        obs = -1;
652
        if (ptr != NULL) {
653
            ip = ptr;
654
            if (*ip == 0)
655
                ibs = (int)num;
656
            else
657
                obs = (int)num;
658
        } else {
659
            ibs = (int)num;
660
            obs = ibs;
661
        }
662
663
        if (ibs != -1) {
664
            OPENSSL_free(ctx->ibuf);
665
            ctx->ibuf = NULL;
666
            ctx->ibufsize = ibs;
667
        }
668
669
        if (obs != -1) {
670
            OPENSSL_free(ctx->obuf);
671
            ctx->obuf = NULL;
672
            ctx->obufsize = obs;
673
        }
674
        ret = 1;
675
        break;
676
677
    case BIO_C_DO_STATE_MACHINE:
678
        BIO_clear_retry_flags(b);
679
        ret = BIO_ctrl(next, cmd, num, ptr);
680
        BIO_copy_next_retry(b);
681
        break;
682
683
    case BIO_CTRL_WPENDING:
684
        if (ctx->obuf == NULL)
685
            return 0;
686
687
        if (ctx->odone) {
688
            ret = ctx->ocount;
689
        } else {
690
            ret = ctx->ocount;
691
            if (ret == 0)
692
                /* Unknown amount pending but we are not finished */
693
                ret = 1;
694
        }
695
        if (ret == 0)
696
            ret = BIO_ctrl(next, cmd, num, ptr);
697
        break;
698
699
    case BIO_CTRL_PENDING:
700
        ret = ctx->zin.avail_in;
701
        if (ret == 0)
702
            ret = BIO_ctrl(next, cmd, num, ptr);
703
        break;
704
705
    default:
706
        ret = BIO_ctrl(next, cmd, num, ptr);
707
        break;
708
    }
709
710
    return ret;
711
}
712
713
static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
714
{
715
    BIO *next = BIO_next(b);
716
717
    if (next == NULL)
718
        return 0;
719
    return BIO_callback_ctrl(next, cmd, fp);
720
}
721
722
#endif