Coverage Report

Created: 2026-10-28 06:28

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