Fixed problem with multiple load-unload of comp zlib
[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 };
114
115 /*
116  * When OpenSSL is built on Windows, we do not want to require that
117  * the ZLIB.DLL be available in order for the OpenSSL DLLs to
118  * work.  Therefore, all ZLIB routines are loaded at run time
119  * and we do not link to a .LIB file when ZLIB_SHARED is set.
120  */
121 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
122 #  include <windows.h>
123 # endif                         /* !(OPENSSL_SYS_WINDOWS ||
124                                  * OPENSSL_SYS_WIN32) */
125
126 # ifdef ZLIB_SHARED
127 #  include <openssl/dso.h>
128
129 /* Function pointers */
130 typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
131                             const Bytef *source, uLong sourceLen);
132 typedef int (*inflateEnd_ft) (z_streamp strm);
133 typedef int (*inflate_ft) (z_streamp strm, int flush);
134 typedef int (*inflateInit__ft) (z_streamp strm,
135                                 const char *version, int stream_size);
136 typedef int (*deflateEnd_ft) (z_streamp strm);
137 typedef int (*deflate_ft) (z_streamp strm, int flush);
138 typedef int (*deflateInit__ft) (z_streamp strm, int level,
139                                 const char *version, int stream_size);
140 typedef const char *(*zError__ft) (int err);
141 static compress_ft p_compress = NULL;
142 static inflateEnd_ft p_inflateEnd = NULL;
143 static inflate_ft p_inflate = NULL;
144 static inflateInit__ft p_inflateInit_ = NULL;
145 static deflateEnd_ft p_deflateEnd = NULL;
146 static deflate_ft p_deflate = NULL;
147 static deflateInit__ft p_deflateInit_ = NULL;
148 static zError__ft p_zError = NULL;
149
150 static int zlib_loaded = 0;     /* only attempt to init func pts once */
151 static DSO *zlib_dso = NULL;
152
153 #  define compress                p_compress
154 #  define inflateEnd              p_inflateEnd
155 #  define inflate                 p_inflate
156 #  define inflateInit_            p_inflateInit_
157 #  define deflateEnd              p_deflateEnd
158 #  define deflate                 p_deflate
159 #  define deflateInit_            p_deflateInit_
160 #  define zError                  p_zError
161 # endif                         /* ZLIB_SHARED */
162
163 struct zlib_state {
164     z_stream istream;
165     z_stream ostream;
166 };
167
168 static int zlib_stateful_init(COMP_CTX *ctx)
169 {
170     int err;
171     struct zlib_state *state = OPENSSL_malloc(sizeof(*state));
172
173     if (state == NULL)
174         goto err;
175
176     state->istream.zalloc = zlib_zalloc;
177     state->istream.zfree = zlib_zfree;
178     state->istream.opaque = Z_NULL;
179     state->istream.next_in = Z_NULL;
180     state->istream.next_out = Z_NULL;
181     state->istream.avail_in = 0;
182     state->istream.avail_out = 0;
183     err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
184     if (err != Z_OK)
185         goto err;
186
187     state->ostream.zalloc = zlib_zalloc;
188     state->ostream.zfree = zlib_zfree;
189     state->ostream.opaque = Z_NULL;
190     state->ostream.next_in = Z_NULL;
191     state->ostream.next_out = Z_NULL;
192     state->ostream.avail_in = 0;
193     state->ostream.avail_out = 0;
194     err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
195                        ZLIB_VERSION, sizeof(z_stream));
196     if (err != Z_OK)
197         goto err;
198
199     ctx->data = state;
200     return 1;
201  err:
202     OPENSSL_free(state);
203     return 0;
204 }
205
206 static void zlib_stateful_finish(COMP_CTX *ctx)
207 {
208     struct zlib_state *state = ctx->data;
209     inflateEnd(&state->istream);
210     deflateEnd(&state->ostream);
211     OPENSSL_free(state);
212 }
213
214 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
215                                         unsigned int olen, unsigned char *in,
216                                         unsigned int ilen)
217 {
218     int err = Z_OK;
219     struct zlib_state *state = ctx->data;
220
221     if (state == NULL)
222         return -1;
223
224     state->ostream.next_in = in;
225     state->ostream.avail_in = ilen;
226     state->ostream.next_out = out;
227     state->ostream.avail_out = olen;
228     if (ilen > 0)
229         err = deflate(&state->ostream, Z_SYNC_FLUSH);
230     if (err != Z_OK)
231         return -1;
232 # ifdef DEBUG_ZLIB
233     fprintf(stderr, "compress(%4d)->%4d %s\n",
234             ilen, olen - state->ostream.avail_out,
235             (ilen != olen - state->ostream.avail_out) ? "zlib" : "clear");
236 # endif
237     return olen - state->ostream.avail_out;
238 }
239
240 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
241                                       unsigned int olen, unsigned char *in,
242                                       unsigned int ilen)
243 {
244     int err = Z_OK;
245     struct zlib_state *state = ctx->data;
246
247     if (state == NULL)
248         return 0;
249
250     state->istream.next_in = in;
251     state->istream.avail_in = ilen;
252     state->istream.next_out = out;
253     state->istream.avail_out = olen;
254     if (ilen > 0)
255         err = inflate(&state->istream, Z_SYNC_FLUSH);
256     if (err != Z_OK)
257         return -1;
258 # ifdef DEBUG_ZLIB
259     fprintf(stderr, "expand(%4d)->%4d %s\n",
260             ilen, olen - state->istream.avail_out,
261             (ilen != olen - state->istream.avail_out) ? "zlib" : "clear");
262 # endif
263     return olen - state->istream.avail_out;
264 }
265
266 #endif
267
268 COMP_METHOD *COMP_zlib(void)
269 {
270     COMP_METHOD *meth = &zlib_method_nozlib;
271
272 #ifdef ZLIB_SHARED
273     if (!zlib_loaded) {
274 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
275         zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
276 # else
277         zlib_dso = DSO_load(NULL, "z", NULL, 0);
278 # endif
279         if (zlib_dso != NULL) {
280             p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
281             p_inflateEnd
282                 = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
283             p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
284             p_inflateInit_
285                 = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
286             p_deflateEnd
287                 = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
288             p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
289             p_deflateInit_
290                 = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
291             p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
292
293             if (p_compress && p_inflateEnd && p_inflate
294                 && p_inflateInit_ && p_deflateEnd
295                 && p_deflate && p_deflateInit_ && p_zError)
296                 zlib_loaded++;
297             if (zlib_loaded)
298                 meth = &zlib_stateful_method;
299         }
300     }
301 #endif
302 #if defined(ZLIB)
303     meth = &zlib_stateful_method;
304 #endif
305
306     return (meth);
307 }
308
309 void COMP_zlib_cleanup(void)
310 {
311 #ifdef ZLIB_SHARED
312     if (zlib_dso != NULL)
313         DSO_free(zlib_dso);
314     zlib_dso = NULL;
315 #endif
316 }
317
318 #ifdef ZLIB
319
320 /* Zlib based compression/decompression filter BIO */
321
322 typedef struct {
323     unsigned char *ibuf;        /* Input buffer */
324     int ibufsize;               /* Buffer size */
325     z_stream zin;               /* Input decompress context */
326     unsigned char *obuf;        /* Output buffer */
327     int obufsize;               /* Output buffer size */
328     unsigned char *optr;        /* Position in output buffer */
329     int ocount;                 /* Amount of data in output buffer */
330     int odone;                  /* deflate EOF */
331     int comp_level;             /* Compression level to use */
332     z_stream zout;              /* Output compression context */
333 } BIO_ZLIB_CTX;
334
335 # define ZLIB_DEFAULT_BUFSIZE 1024
336
337 static int bio_zlib_new(BIO *bi);
338 static int bio_zlib_free(BIO *bi);
339 static int bio_zlib_read(BIO *b, char *out, int outl);
340 static int bio_zlib_write(BIO *b, const char *in, int inl);
341 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
342 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
343
344 static BIO_METHOD bio_meth_zlib = {
345     BIO_TYPE_COMP,
346     "zlib",
347     bio_zlib_write,
348     bio_zlib_read,
349     NULL,
350     NULL,
351     bio_zlib_ctrl,
352     bio_zlib_new,
353     bio_zlib_free,
354     bio_zlib_callback_ctrl
355 };
356
357 BIO_METHOD *BIO_f_zlib(void)
358 {
359     return &bio_meth_zlib;
360 }
361
362 static int bio_zlib_new(BIO *bi)
363 {
364     BIO_ZLIB_CTX *ctx;
365 # ifdef ZLIB_SHARED
366     (void)COMP_zlib();
367     if (!zlib_loaded) {
368         COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
369         return 0;
370     }
371 # endif
372     ctx = OPENSSL_malloc(sizeof(*ctx));
373     if (!ctx) {
374         COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
375         return 0;
376     }
377     ctx->ibuf = NULL;
378     ctx->obuf = NULL;
379     ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
380     ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
381     ctx->zin.zalloc = Z_NULL;
382     ctx->zin.zfree = Z_NULL;
383     ctx->zin.next_in = NULL;
384     ctx->zin.avail_in = 0;
385     ctx->zin.next_out = NULL;
386     ctx->zin.avail_out = 0;
387     ctx->zout.zalloc = Z_NULL;
388     ctx->zout.zfree = Z_NULL;
389     ctx->zout.next_in = NULL;
390     ctx->zout.avail_in = 0;
391     ctx->zout.next_out = NULL;
392     ctx->zout.avail_out = 0;
393     ctx->odone = 0;
394     ctx->comp_level = Z_DEFAULT_COMPRESSION;
395     bi->init = 1;
396     bi->ptr = (char *)ctx;
397     bi->flags = 0;
398     return 1;
399 }
400
401 static int bio_zlib_free(BIO *bi)
402 {
403     BIO_ZLIB_CTX *ctx;
404     if (!bi)
405         return 0;
406     ctx = (BIO_ZLIB_CTX *) bi->ptr;
407     if (ctx->ibuf) {
408         /* Destroy decompress context */
409         inflateEnd(&ctx->zin);
410         OPENSSL_free(ctx->ibuf);
411     }
412     if (ctx->obuf) {
413         /* Destroy compress context */
414         deflateEnd(&ctx->zout);
415         OPENSSL_free(ctx->obuf);
416     }
417     OPENSSL_free(ctx);
418     bi->ptr = NULL;
419     bi->init = 0;
420     bi->flags = 0;
421     return 1;
422 }
423
424 static int bio_zlib_read(BIO *b, char *out, int outl)
425 {
426     BIO_ZLIB_CTX *ctx;
427     int ret;
428     z_stream *zin;
429     if (!out || !outl)
430         return 0;
431     ctx = (BIO_ZLIB_CTX *) b->ptr;
432     zin = &ctx->zin;
433     BIO_clear_retry_flags(b);
434     if (!ctx->ibuf) {
435         ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
436         if (!ctx->ibuf) {
437             COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
438             return 0;
439         }
440         inflateInit(zin);
441         zin->next_in = ctx->ibuf;
442         zin->avail_in = 0;
443     }
444
445     /* Copy output data directly to supplied buffer */
446     zin->next_out = (unsigned char *)out;
447     zin->avail_out = (unsigned int)outl;
448     for (;;) {
449         /* Decompress while data available */
450         while (zin->avail_in) {
451             ret = inflate(zin, 0);
452             if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
453                 COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR);
454                 ERR_add_error_data(2, "zlib error:", zError(ret));
455                 return 0;
456             }
457             /* If EOF or we've read everything then return */
458             if ((ret == Z_STREAM_END) || !zin->avail_out)
459                 return outl - zin->avail_out;
460         }
461
462         /*
463          * No data in input buffer try to read some in, if an error then
464          * return the total data read.
465          */
466         ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
467         if (ret <= 0) {
468             /* Total data read */
469             int tot = outl - zin->avail_out;
470             BIO_copy_next_retry(b);
471             if (ret < 0)
472                 return (tot > 0) ? tot : ret;
473             return tot;
474         }
475         zin->avail_in = ret;
476         zin->next_in = ctx->ibuf;
477     }
478 }
479
480 static int bio_zlib_write(BIO *b, const char *in, int inl)
481 {
482     BIO_ZLIB_CTX *ctx;
483     int ret;
484     z_stream *zout;
485     if (!in || !inl)
486         return 0;
487     ctx = (BIO_ZLIB_CTX *) b->ptr;
488     if (ctx->odone)
489         return 0;
490     zout = &ctx->zout;
491     BIO_clear_retry_flags(b);
492     if (!ctx->obuf) {
493         ctx->obuf = OPENSSL_malloc(ctx->obufsize);
494         /* Need error here */
495         if (!ctx->obuf) {
496             COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
497             return 0;
498         }
499         ctx->optr = ctx->obuf;
500         ctx->ocount = 0;
501         deflateInit(zout, ctx->comp_level);
502         zout->next_out = ctx->obuf;
503         zout->avail_out = ctx->obufsize;
504     }
505     /* Obtain input data directly from supplied buffer */
506     zout->next_in = (void *)in;
507     zout->avail_in = inl;
508     for (;;) {
509         /* If data in output buffer write it first */
510         while (ctx->ocount) {
511             ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
512             if (ret <= 0) {
513                 /* Total data written */
514                 int tot = inl - zout->avail_in;
515                 BIO_copy_next_retry(b);
516                 if (ret < 0)
517                     return (tot > 0) ? tot : ret;
518                 return tot;
519             }
520             ctx->optr += ret;
521             ctx->ocount -= ret;
522         }
523
524         /* Have we consumed all supplied data? */
525         if (!zout->avail_in)
526             return inl;
527
528         /* Compress some more */
529
530         /* Reset buffer */
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, 0);
536         if (ret != Z_OK) {
537             COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR);
538             ERR_add_error_data(2, "zlib error:", zError(ret));
539             return 0;
540         }
541         ctx->ocount = ctx->obufsize - zout->avail_out;
542     }
543 }
544
545 static int bio_zlib_flush(BIO *b)
546 {
547     BIO_ZLIB_CTX *ctx;
548     int ret;
549     z_stream *zout;
550     ctx = (BIO_ZLIB_CTX *) b->ptr;
551     /* If no data written or already flush show success */
552     if (!ctx->obuf || (ctx->odone && !ctx->ocount))
553         return 1;
554     zout = &ctx->zout;
555     BIO_clear_retry_flags(b);
556     /* No more input data */
557     zout->next_in = NULL;
558     zout->avail_in = 0;
559     for (;;) {
560         /* If data in output buffer write it first */
561         while (ctx->ocount) {
562             ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
563             if (ret <= 0) {
564                 BIO_copy_next_retry(b);
565                 return ret;
566             }
567             ctx->optr += ret;
568             ctx->ocount -= ret;
569         }
570         if (ctx->odone)
571             return 1;
572
573         /* Compress some more */
574
575         /* Reset buffer */
576         ctx->optr = ctx->obuf;
577         zout->next_out = ctx->obuf;
578         zout->avail_out = ctx->obufsize;
579         /* Compress some more */
580         ret = deflate(zout, Z_FINISH);
581         if (ret == Z_STREAM_END)
582             ctx->odone = 1;
583         else if (ret != Z_OK) {
584             COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR);
585             ERR_add_error_data(2, "zlib error:", zError(ret));
586             return 0;
587         }
588         ctx->ocount = ctx->obufsize - zout->avail_out;
589     }
590 }
591
592 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
593 {
594     BIO_ZLIB_CTX *ctx;
595     int ret, *ip;
596     int ibs, obs;
597     if (!b->next_bio)
598         return 0;
599     ctx = (BIO_ZLIB_CTX *) b->ptr;
600     switch (cmd) {
601
602     case BIO_CTRL_RESET:
603         ctx->ocount = 0;
604         ctx->odone = 0;
605         ret = 1;
606         break;
607
608     case BIO_CTRL_FLUSH:
609         ret = bio_zlib_flush(b);
610         if (ret > 0)
611             ret = BIO_flush(b->next_bio);
612         break;
613
614     case BIO_C_SET_BUFF_SIZE:
615         ibs = -1;
616         obs = -1;
617         if (ptr != NULL) {
618             ip = ptr;
619             if (*ip == 0)
620                 ibs = (int)num;
621             else
622                 obs = (int)num;
623         } else {
624             ibs = (int)num;
625             obs = ibs;
626         }
627
628         if (ibs != -1) {
629             OPENSSL_free(ctx->ibuf);
630             ctx->ibuf = NULL;
631             ctx->ibufsize = ibs;
632         }
633
634         if (obs != -1) {
635             OPENSSL_free(ctx->obuf);
636             ctx->obuf = NULL;
637             ctx->obufsize = obs;
638         }
639         ret = 1;
640         break;
641
642     case BIO_C_DO_STATE_MACHINE:
643         BIO_clear_retry_flags(b);
644         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
645         BIO_copy_next_retry(b);
646         break;
647
648     default:
649         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
650         break;
651
652     }
653
654     return ret;
655 }
656
657 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
658 {
659     if (!b->next_bio)
660         return 0;
661     return BIO_callback_ctrl(b->next_bio, cmd, fp);
662 }
663
664 #endif