RT3841: memset() cipher_data when allocated
[openssl.git] / crypto / comp / c_zlib.c
1 /* ====================================================================
2  * Copyright (c) 1999-2015 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@OpenSSL.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This product includes cryptographic software written by Eric Young
50  * (eay@cryptsoft.com).  This product includes software written by Tim
51  * Hudson (tjh@cryptsoft.com).
52  *
53  */
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <openssl/objects.h>
59 #include <openssl/comp.h>
60 #include <openssl/err.h>
61 #include "comp_lcl.h"
62
63 COMP_METHOD *COMP_zlib(void);
64
65 static COMP_METHOD zlib_method_nozlib = {
66     NID_undef,
67     "(undef)",
68     NULL,
69     NULL,
70     NULL,
71     NULL,
72 };
73
74 #ifndef ZLIB
75 # undef ZLIB_SHARED
76 #else
77
78 # include <zlib.h>
79
80 static int zlib_stateful_init(COMP_CTX *ctx);
81 static void zlib_stateful_finish(COMP_CTX *ctx);
82 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
83                                         unsigned int olen, unsigned char *in,
84                                         unsigned int ilen);
85 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
86                                       unsigned int olen, unsigned char *in,
87                                       unsigned int ilen);
88
89 /* memory allocations functions for zlib intialization */
90 static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
91 {
92     void *p;
93
94     p = OPENSSL_malloc(no * size);
95     if (p)
96         memset(p, 0, no * size);
97     return p;
98 }
99
100 static void zlib_zfree(void *opaque, void *address)
101 {
102     OPENSSL_free(address);
103 }
104
105
106 static COMP_METHOD zlib_stateful_method = {
107     NID_zlib_compression,
108     LN_zlib_compression,
109     zlib_stateful_init,
110     zlib_stateful_finish,
111     zlib_stateful_compress_block,
112     zlib_stateful_expand_block,
113     NULL,
114     NULL,
115 };
116
117 /*
118  * When OpenSSL is built on Windows, we do not want to require that
119  * the ZLIB.DLL be available in order for the OpenSSL DLLs to
120  * work.  Therefore, all ZLIB routines are loaded at run time
121  * and we do not link to a .LIB file when ZLIB_SHARED is set.
122  */
123 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
124 #  include <windows.h>
125 # endif                         /* !(OPENSSL_SYS_WINDOWS ||
126                                  * OPENSSL_SYS_WIN32) */
127
128 # ifdef ZLIB_SHARED
129 #  include <openssl/dso.h>
130
131 /* Function pointers */
132 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
133                             const Bytef *source, uLong sourceLen);
134 typedef int (*inflateEnd_ft) (z_streamp strm);
135 typedef int (*inflate_ft) (z_streamp strm, int flush);
136 typedef int (*inflateInit__ft) (z_streamp strm,
137                                 const char *version, int stream_size);
138 typedef int (*deflateEnd_ft) (z_streamp strm);
139 typedef int (*deflate_ft) (z_streamp strm, int flush);
140 typedef int (*deflateInit__ft) (z_streamp strm, int level,
141                                 const char *version, int stream_size);
142 typedef const char *(*zError__ft) (int err);
143 static compress_ft p_compress = NULL;
144 static inflateEnd_ft p_inflateEnd = NULL;
145 static inflate_ft p_inflate = NULL;
146 static inflateInit__ft p_inflateInit_ = NULL;
147 static deflateEnd_ft p_deflateEnd = NULL;
148 static deflate_ft p_deflate = NULL;
149 static deflateInit__ft p_deflateInit_ = NULL;
150 static zError__ft p_zError = NULL;
151
152 static int zlib_loaded = 0;     /* only attempt to init func pts once */
153 static DSO *zlib_dso = NULL;
154
155 #  define compress                p_compress
156 #  define inflateEnd              p_inflateEnd
157 #  define inflate                 p_inflate
158 #  define inflateInit_            p_inflateInit_
159 #  define deflateEnd              p_deflateEnd
160 #  define deflate                 p_deflate
161 #  define deflateInit_            p_deflateInit_
162 #  define zError                  p_zError
163 # endif                         /* ZLIB_SHARED */
164
165 struct zlib_state {
166     z_stream istream;
167     z_stream ostream;
168 };
169
170 static int zlib_stateful_ex_idx = -1;
171
172 static int zlib_stateful_init(COMP_CTX *ctx)
173 {
174     int err;
175     struct zlib_state *state = OPENSSL_malloc(sizeof(*state));
176
177     if (state == NULL)
178         goto err;
179
180     state->istream.zalloc = zlib_zalloc;
181     state->istream.zfree = zlib_zfree;
182     state->istream.opaque = Z_NULL;
183     state->istream.next_in = Z_NULL;
184     state->istream.next_out = Z_NULL;
185     state->istream.avail_in = 0;
186     state->istream.avail_out = 0;
187     err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
188     if (err != Z_OK)
189         goto err;
190
191     state->ostream.zalloc = zlib_zalloc;
192     state->ostream.zfree = zlib_zfree;
193     state->ostream.opaque = Z_NULL;
194     state->ostream.next_in = Z_NULL;
195     state->ostream.next_out = Z_NULL;
196     state->ostream.avail_in = 0;
197     state->ostream.avail_out = 0;
198     err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
199                        ZLIB_VERSION, sizeof(z_stream));
200     if (err != Z_OK)
201         goto err;
202
203     CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
204     CRYPTO_set_ex_data(&ctx->ex_data, zlib_stateful_ex_idx, state);
205     return 1;
206  err:
207     OPENSSL_free(state);
208     return 0;
209 }
210
211 static void zlib_stateful_finish(COMP_CTX *ctx)
212 {
213     struct zlib_state *state =
214         (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
215                                                 zlib_stateful_ex_idx);
216     inflateEnd(&state->istream);
217     deflateEnd(&state->ostream);
218     OPENSSL_free(state);
219     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP, ctx, &ctx->ex_data);
220 }
221
222 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
223                                         unsigned int olen, unsigned char *in,
224                                         unsigned int ilen)
225 {
226     int err = Z_OK;
227     struct zlib_state *state =
228         (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
229                                                 zlib_stateful_ex_idx);
230
231     if (state == NULL)
232         return -1;
233
234     state->ostream.next_in = in;
235     state->ostream.avail_in = ilen;
236     state->ostream.next_out = out;
237     state->ostream.avail_out = olen;
238     if (ilen > 0)
239         err = deflate(&state->ostream, Z_SYNC_FLUSH);
240     if (err != Z_OK)
241         return -1;
242 # ifdef DEBUG_ZLIB
243     fprintf(stderr, "compress(%4d)->%4d %s\n",
244             ilen, olen - state->ostream.avail_out,
245             (ilen != olen - state->ostream.avail_out) ? "zlib" : "clear");
246 # endif
247     return olen - state->ostream.avail_out;
248 }
249
250 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
251                                       unsigned int olen, unsigned char *in,
252                                       unsigned int ilen)
253 {
254     int err = Z_OK;
255
256     struct zlib_state *state =
257         (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
258                                                 zlib_stateful_ex_idx);
259
260     if (state == NULL)
261         return 0;
262
263     state->istream.next_in = in;
264     state->istream.avail_in = ilen;
265     state->istream.next_out = out;
266     state->istream.avail_out = olen;
267     if (ilen > 0)
268         err = inflate(&state->istream, Z_SYNC_FLUSH);
269     if (err != Z_OK)
270         return -1;
271 # ifdef DEBUG_ZLIB
272     fprintf(stderr, "expand(%4d)->%4d %s\n",
273             ilen, olen - state->istream.avail_out,
274             (ilen != olen - state->istream.avail_out) ? "zlib" : "clear");
275 # endif
276     return olen - state->istream.avail_out;
277 }
278
279 #endif
280
281 COMP_METHOD *COMP_zlib(void)
282 {
283     COMP_METHOD *meth = &zlib_method_nozlib;
284
285 #ifdef ZLIB_SHARED
286     if (!zlib_loaded) {
287 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
288         zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
289 # else
290         zlib_dso = DSO_load(NULL, "z", NULL, 0);
291 # endif
292         if (zlib_dso != NULL) {
293             p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
294             p_inflateEnd
295                 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
296             p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
297             p_inflateInit_
298                 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
299             p_deflateEnd
300                 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
301             p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
302             p_deflateInit_
303                 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
304             p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
305
306             if (p_compress && p_inflateEnd && p_inflate
307                 && p_inflateInit_ && p_deflateEnd
308                 && p_deflate && p_deflateInit_ && p_zError)
309                 zlib_loaded++;
310         }
311     }
312 #endif
313 #ifdef ZLIB_SHARED
314     if (zlib_loaded)
315 #endif
316 #if defined(ZLIB) || defined(ZLIB_SHARED)
317     {
318         /*
319          * init zlib_stateful_ex_idx here so that in a multi-process
320          * application it's enough to intialize openssl before forking (idx
321          * will be inherited in all the children)
322          */
323         if (zlib_stateful_ex_idx == -1) {
324             CRYPTO_w_lock(CRYPTO_LOCK_COMP);
325             if (zlib_stateful_ex_idx == -1)
326                 zlib_stateful_ex_idx =
327                     CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
328                                             0, NULL, NULL, NULL, NULL);
329             CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
330             if (zlib_stateful_ex_idx == -1)
331                 goto err;
332         }
333
334         meth = &zlib_stateful_method;
335     }
336  err:
337 #endif
338
339     return (meth);
340 }
341
342 void COMP_zlib_cleanup(void)
343 {
344 #ifdef ZLIB_SHARED
345     DSO_free(zlib_dso);
346 #endif
347 }
348
349 #ifdef ZLIB
350
351 /* Zlib based compression/decompression filter BIO */
352
353 typedef struct {
354     unsigned char *ibuf;        /* Input buffer */
355     int ibufsize;               /* Buffer size */
356     z_stream zin;               /* Input decompress context */
357     unsigned char *obuf;        /* Output buffer */
358     int obufsize;               /* Output buffer size */
359     unsigned char *optr;        /* Position in output buffer */
360     int ocount;                 /* Amount of data in output buffer */
361     int odone;                  /* deflate EOF */
362     int comp_level;             /* Compression level to use */
363     z_stream zout;              /* Output compression context */
364 } BIO_ZLIB_CTX;
365
366 # define ZLIB_DEFAULT_BUFSIZE 1024
367
368 static int bio_zlib_new(BIO *bi);
369 static int bio_zlib_free(BIO *bi);
370 static int bio_zlib_read(BIO *b, char *out, int outl);
371 static int bio_zlib_write(BIO *b, const char *in, int inl);
372 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
373 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
374
375 static BIO_METHOD bio_meth_zlib = {
376     BIO_TYPE_COMP,
377     "zlib",
378     bio_zlib_write,
379     bio_zlib_read,
380     NULL,
381     NULL,
382     bio_zlib_ctrl,
383     bio_zlib_new,
384     bio_zlib_free,
385     bio_zlib_callback_ctrl
386 };
387
388 BIO_METHOD *BIO_f_zlib(void)
389 {
390     return &bio_meth_zlib;
391 }
392
393 static int bio_zlib_new(BIO *bi)
394 {
395     BIO_ZLIB_CTX *ctx;
396 # ifdef ZLIB_SHARED
397     (void)COMP_zlib();
398     if (!zlib_loaded) {
399         COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
400         return 0;
401     }
402 # endif
403     ctx = OPENSSL_malloc(sizeof(*ctx));
404     if (!ctx) {
405         COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
406         return 0;
407     }
408     ctx->ibuf = NULL;
409     ctx->obuf = NULL;
410     ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
411     ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
412     ctx->zin.zalloc = Z_NULL;
413     ctx->zin.zfree = Z_NULL;
414     ctx->zin.next_in = NULL;
415     ctx->zin.avail_in = 0;
416     ctx->zin.next_out = NULL;
417     ctx->zin.avail_out = 0;
418     ctx->zout.zalloc = Z_NULL;
419     ctx->zout.zfree = Z_NULL;
420     ctx->zout.next_in = NULL;
421     ctx->zout.avail_in = 0;
422     ctx->zout.next_out = NULL;
423     ctx->zout.avail_out = 0;
424     ctx->odone = 0;
425     ctx->comp_level = Z_DEFAULT_COMPRESSION;
426     bi->init = 1;
427     bi->ptr = (char *)ctx;
428     bi->flags = 0;
429     return 1;
430 }
431
432 static int bio_zlib_free(BIO *bi)
433 {
434     BIO_ZLIB_CTX *ctx;
435     if (!bi)
436         return 0;
437     ctx = (BIO_ZLIB_CTX *) bi->ptr;
438     if (ctx->ibuf) {
439         /* Destroy decompress context */
440         inflateEnd(&ctx->zin);
441         OPENSSL_free(ctx->ibuf);
442     }
443     if (ctx->obuf) {
444         /* Destroy compress context */
445         deflateEnd(&ctx->zout);
446         OPENSSL_free(ctx->obuf);
447     }
448     OPENSSL_free(ctx);
449     bi->ptr = NULL;
450     bi->init = 0;
451     bi->flags = 0;
452     return 1;
453 }
454
455 static int bio_zlib_read(BIO *b, char *out, int outl)
456 {
457     BIO_ZLIB_CTX *ctx;
458     int ret;
459     z_stream *zin;
460     if (!out || !outl)
461         return 0;
462     ctx = (BIO_ZLIB_CTX *) b->ptr;
463     zin = &ctx->zin;
464     BIO_clear_retry_flags(b);
465     if (!ctx->ibuf) {
466         ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
467         if (!ctx->ibuf) {
468             COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
469             return 0;
470         }
471         inflateInit(zin);
472         zin->next_in = ctx->ibuf;
473         zin->avail_in = 0;
474     }
475
476     /* Copy output data directly to supplied buffer */
477     zin->next_out = (unsigned char *)out;
478     zin->avail_out = (unsigned int)outl;
479     for (;;) {
480         /* Decompress while data available */
481         while (zin->avail_in) {
482             ret = inflate(zin, 0);
483             if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
484                 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
485                 ERR_add_error_data(2, "zlib error:", zError(ret));
486                 return 0;
487             }
488             /* If EOF or we've read everything then return */
489             if ((ret == Z_STREAM_END) || !zin->avail_out)
490                 return outl - zin->avail_out;
491         }
492
493         /*
494          * No data in input buffer try to read some in, if an error then
495          * return the total data read.
496          */
497         ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
498         if (ret <= 0) {
499             /* Total data read */
500             int tot = outl - zin->avail_out;
501             BIO_copy_next_retry(b);
502             if (ret < 0)
503                 return (tot > 0) ? tot : ret;
504             return tot;
505         }
506         zin->avail_in = ret;
507         zin->next_in = ctx->ibuf;
508     }
509 }
510
511 static int bio_zlib_write(BIO *b, const char *in, int inl)
512 {
513     BIO_ZLIB_CTX *ctx;
514     int ret;
515     z_stream *zout;
516     if (!in || !inl)
517         return 0;
518     ctx = (BIO_ZLIB_CTX *) b->ptr;
519     if (ctx->odone)
520         return 0;
521     zout = &ctx->zout;
522     BIO_clear_retry_flags(b);
523     if (!ctx->obuf) {
524         ctx->obuf = OPENSSL_malloc(ctx->obufsize);
525         /* Need error here */
526         if (!ctx->obuf) {
527             COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
528             return 0;
529         }
530         ctx->optr = ctx->obuf;
531         ctx->ocount = 0;
532         deflateInit(zout, ctx->comp_level);
533         zout->next_out = ctx->obuf;
534         zout->avail_out = ctx->obufsize;
535     }
536     /* Obtain input data directly from supplied buffer */
537     zout->next_in = (void *)in;
538     zout->avail_in = inl;
539     for (;;) {
540         /* If data in output buffer write it first */
541         while (ctx->ocount) {
542             ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
543             if (ret <= 0) {
544                 /* Total data written */
545                 int tot = inl - zout->avail_in;
546                 BIO_copy_next_retry(b);
547                 if (ret < 0)
548                     return (tot > 0) ? tot : ret;
549                 return tot;
550             }
551             ctx->optr += ret;
552             ctx->ocount -= ret;
553         }
554
555         /* Have we consumed all supplied data? */
556         if (!zout->avail_in)
557             return inl;
558
559         /* Compress some more */
560
561         /* Reset buffer */
562         ctx->optr = ctx->obuf;
563         zout->next_out = ctx->obuf;
564         zout->avail_out = ctx->obufsize;
565         /* Compress some more */
566         ret = deflate(zout, 0);
567         if (ret != Z_OK) {
568             COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
569             ERR_add_error_data(2, "zlib error:", zError(ret));
570             return 0;
571         }
572         ctx->ocount = ctx->obufsize - zout->avail_out;
573     }
574 }
575
576 static int bio_zlib_flush(BIO *b)
577 {
578     BIO_ZLIB_CTX *ctx;
579     int ret;
580     z_stream *zout;
581     ctx = (BIO_ZLIB_CTX *) b->ptr;
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(b->next_bio, 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             COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
616             ERR_add_error_data(2, "zlib error:", 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     if (!b->next_bio)
629         return 0;
630     ctx = (BIO_ZLIB_CTX *) b->ptr;
631     switch (cmd) {
632
633     case BIO_CTRL_RESET:
634         ctx->ocount = 0;
635         ctx->odone = 0;
636         ret = 1;
637         break;
638
639     case BIO_CTRL_FLUSH:
640         ret = bio_zlib_flush(b);
641         if (ret > 0)
642             ret = BIO_flush(b->next_bio);
643         break;
644
645     case BIO_C_SET_BUFF_SIZE:
646         ibs = -1;
647         obs = -1;
648         if (ptr != NULL) {
649             ip = ptr;
650             if (*ip == 0)
651                 ibs = (int)num;
652             else
653                 obs = (int)num;
654         } else {
655             ibs = (int)num;
656             obs = ibs;
657         }
658
659         if (ibs != -1) {
660             OPENSSL_free(ctx->ibuf);
661             ctx->ibuf = NULL;
662             ctx->ibufsize = ibs;
663         }
664
665         if (obs != -1) {
666             OPENSSL_free(ctx->obuf);
667             ctx->obuf = NULL;
668             ctx->obufsize = obs;
669         }
670         ret = 1;
671         break;
672
673     case BIO_C_DO_STATE_MACHINE:
674         BIO_clear_retry_flags(b);
675         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
676         BIO_copy_next_retry(b);
677         break;
678
679     default:
680         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
681         break;
682
683     }
684
685     return ret;
686 }
687
688 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
689 {
690     if (!b->next_bio)
691         return 0;
692     return BIO_callback_ctrl(b->next_bio, cmd, fp);
693 }
694
695 #endif