Remove /* foo.c */ comments
[openssl.git] / crypto / bio / bss_bio.c
1 /* ====================================================================
2  * Copyright (c) 1998-2003 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 /*
56  * Special method for a BIO where the other endpoint is also a BIO of this
57  * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
58  * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
59  * library with I/O interfaces for which no specific BIO method is available.
60  * See ssl/ssltest.c for some hints on how this can be used.
61  */
62
63 /* BIO_DEBUG implies BIO_PAIR_DEBUG */
64 #ifdef BIO_DEBUG
65 # ifndef BIO_PAIR_DEBUG
66 #  define BIO_PAIR_DEBUG
67 # endif
68 #endif
69
70 /* disable assert() unless BIO_PAIR_DEBUG has been defined */
71 #ifndef BIO_PAIR_DEBUG
72 # ifndef NDEBUG
73 #  define NDEBUG
74 # endif
75 #endif
76
77 #include <assert.h>
78 #include <limits.h>
79 #include <stdlib.h>
80 #include <string.h>
81
82 #include <openssl/bio.h>
83 #include <openssl/err.h>
84 #include <openssl/crypto.h>
85
86 #include "e_os.h"
87
88 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
89 #if defined(OPENSSL_SYS_VXWORKS)
90 # undef SSIZE_MAX
91 #endif
92 #ifndef SSIZE_MAX
93 # define SSIZE_MAX INT_MAX
94 #endif
95
96 static int bio_new(BIO *bio);
97 static int bio_free(BIO *bio);
98 static int bio_read(BIO *bio, char *buf, int size);
99 static int bio_write(BIO *bio, const char *buf, int num);
100 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
101 static int bio_puts(BIO *bio, const char *str);
102
103 static int bio_make_pair(BIO *bio1, BIO *bio2);
104 static void bio_destroy_pair(BIO *bio);
105
106 static BIO_METHOD methods_biop = {
107     BIO_TYPE_BIO,
108     "BIO pair",
109     bio_write,
110     bio_read,
111     bio_puts,
112     NULL /* no bio_gets */ ,
113     bio_ctrl,
114     bio_new,
115     bio_free,
116     NULL                        /* no bio_callback_ctrl */
117 };
118
119 BIO_METHOD *BIO_s_bio(void)
120 {
121     return &methods_biop;
122 }
123
124 struct bio_bio_st {
125     BIO *peer;                  /* NULL if buf == NULL. If peer != NULL, then
126                                  * peer->ptr is also a bio_bio_st, and its
127                                  * "peer" member points back to us. peer !=
128                                  * NULL iff init != 0 in the BIO. */
129     /* This is for what we write (i.e. reading uses peer's struct): */
130     int closed;                 /* valid iff peer != NULL */
131     size_t len;                 /* valid iff buf != NULL; 0 if peer == NULL */
132     size_t offset;              /* valid iff buf != NULL; 0 if len == 0 */
133     size_t size;
134     char *buf;                  /* "size" elements (if != NULL) */
135     size_t request;             /* valid iff peer != NULL; 0 if len != 0,
136                                  * otherwise set by peer to number of bytes
137                                  * it (unsuccessfully) tried to read, never
138                                  * more than buffer space (size-len)
139                                  * warrants. */
140 };
141
142 static int bio_new(BIO *bio)
143 {
144     struct bio_bio_st *b;
145
146     b = OPENSSL_malloc(sizeof(*b));
147     if (b == NULL)
148         return 0;
149
150     b->peer = NULL;
151     /* enough for one TLS record (just a default) */
152     b->size = 17 * 1024;
153     b->buf = NULL;
154
155     bio->ptr = b;
156     return 1;
157 }
158
159 static int bio_free(BIO *bio)
160 {
161     struct bio_bio_st *b;
162
163     if (bio == NULL)
164         return 0;
165     b = bio->ptr;
166
167     assert(b != NULL);
168
169     if (b->peer)
170         bio_destroy_pair(bio);
171
172     OPENSSL_free(b->buf);
173     OPENSSL_free(b);
174
175     return 1;
176 }
177
178 static int bio_read(BIO *bio, char *buf, int size_)
179 {
180     size_t size = size_;
181     size_t rest;
182     struct bio_bio_st *b, *peer_b;
183
184     BIO_clear_retry_flags(bio);
185
186     if (!bio->init)
187         return 0;
188
189     b = bio->ptr;
190     assert(b != NULL);
191     assert(b->peer != NULL);
192     peer_b = b->peer->ptr;
193     assert(peer_b != NULL);
194     assert(peer_b->buf != NULL);
195
196     peer_b->request = 0;        /* will be set in "retry_read" situation */
197
198     if (buf == NULL || size == 0)
199         return 0;
200
201     if (peer_b->len == 0) {
202         if (peer_b->closed)
203             return 0;           /* writer has closed, and no data is left */
204         else {
205             BIO_set_retry_read(bio); /* buffer is empty */
206             if (size <= peer_b->size)
207                 peer_b->request = size;
208             else
209                 /*
210                  * don't ask for more than the peer can deliver in one write
211                  */
212                 peer_b->request = peer_b->size;
213             return -1;
214         }
215     }
216
217     /* we can read */
218     if (peer_b->len < size)
219         size = peer_b->len;
220
221     /* now read "size" bytes */
222
223     rest = size;
224
225     assert(rest > 0);
226     do {                        /* one or two iterations */
227         size_t chunk;
228
229         assert(rest <= peer_b->len);
230         if (peer_b->offset + rest <= peer_b->size)
231             chunk = rest;
232         else
233             /* wrap around ring buffer */
234             chunk = peer_b->size - peer_b->offset;
235         assert(peer_b->offset + chunk <= peer_b->size);
236
237         memcpy(buf, peer_b->buf + peer_b->offset, chunk);
238
239         peer_b->len -= chunk;
240         if (peer_b->len) {
241             peer_b->offset += chunk;
242             assert(peer_b->offset <= peer_b->size);
243             if (peer_b->offset == peer_b->size)
244                 peer_b->offset = 0;
245             buf += chunk;
246         } else {
247             /* buffer now empty, no need to advance "buf" */
248             assert(chunk == rest);
249             peer_b->offset = 0;
250         }
251         rest -= chunk;
252     }
253     while (rest);
254
255     return size;
256 }
257
258 /*-
259  * non-copying interface: provide pointer to available data in buffer
260  *    bio_nread0:  return number of available bytes
261  *    bio_nread:   also advance index
262  * (example usage:  bio_nread0(), read from buffer, bio_nread()
263  *  or just         bio_nread(), read from buffer)
264  */
265 /*
266  * WARNING: The non-copying interface is largely untested as of yet and may
267  * contain bugs.
268  */
269 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
270 {
271     struct bio_bio_st *b, *peer_b;
272     ossl_ssize_t num;
273
274     BIO_clear_retry_flags(bio);
275
276     if (!bio->init)
277         return 0;
278
279     b = bio->ptr;
280     assert(b != NULL);
281     assert(b->peer != NULL);
282     peer_b = b->peer->ptr;
283     assert(peer_b != NULL);
284     assert(peer_b->buf != NULL);
285
286     peer_b->request = 0;
287
288     if (peer_b->len == 0) {
289         char dummy;
290
291         /* avoid code duplication -- nothing available for reading */
292         return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
293     }
294
295     num = peer_b->len;
296     if (peer_b->size < peer_b->offset + num)
297         /* no ring buffer wrap-around for non-copying interface */
298         num = peer_b->size - peer_b->offset;
299     assert(num > 0);
300
301     if (buf != NULL)
302         *buf = peer_b->buf + peer_b->offset;
303     return num;
304 }
305
306 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
307 {
308     struct bio_bio_st *b, *peer_b;
309     ossl_ssize_t num, available;
310
311     if (num_ > SSIZE_MAX)
312         num = SSIZE_MAX;
313     else
314         num = (ossl_ssize_t) num_;
315
316     available = bio_nread0(bio, buf);
317     if (num > available)
318         num = available;
319     if (num <= 0)
320         return num;
321
322     b = bio->ptr;
323     peer_b = b->peer->ptr;
324
325     peer_b->len -= num;
326     if (peer_b->len) {
327         peer_b->offset += num;
328         assert(peer_b->offset <= peer_b->size);
329         if (peer_b->offset == peer_b->size)
330             peer_b->offset = 0;
331     } else
332         peer_b->offset = 0;
333
334     return num;
335 }
336
337 static int bio_write(BIO *bio, const char *buf, int num_)
338 {
339     size_t num = num_;
340     size_t rest;
341     struct bio_bio_st *b;
342
343     BIO_clear_retry_flags(bio);
344
345     if (!bio->init || buf == NULL || num == 0)
346         return 0;
347
348     b = bio->ptr;
349     assert(b != NULL);
350     assert(b->peer != NULL);
351     assert(b->buf != NULL);
352
353     b->request = 0;
354     if (b->closed) {
355         /* we already closed */
356         BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
357         return -1;
358     }
359
360     assert(b->len <= b->size);
361
362     if (b->len == b->size) {
363         BIO_set_retry_write(bio); /* buffer is full */
364         return -1;
365     }
366
367     /* we can write */
368     if (num > b->size - b->len)
369         num = b->size - b->len;
370
371     /* now write "num" bytes */
372
373     rest = num;
374
375     assert(rest > 0);
376     do {                        /* one or two iterations */
377         size_t write_offset;
378         size_t chunk;
379
380         assert(b->len + rest <= b->size);
381
382         write_offset = b->offset + b->len;
383         if (write_offset >= b->size)
384             write_offset -= b->size;
385         /* b->buf[write_offset] is the first byte we can write to. */
386
387         if (write_offset + rest <= b->size)
388             chunk = rest;
389         else
390             /* wrap around ring buffer */
391             chunk = b->size - write_offset;
392
393         memcpy(b->buf + write_offset, buf, chunk);
394
395         b->len += chunk;
396
397         assert(b->len <= b->size);
398
399         rest -= chunk;
400         buf += chunk;
401     }
402     while (rest);
403
404     return num;
405 }
406
407 /*-
408  * non-copying interface: provide pointer to region to write to
409  *   bio_nwrite0:  check how much space is available
410  *   bio_nwrite:   also increase length
411  * (example usage:  bio_nwrite0(), write to buffer, bio_nwrite()
412  *  or just         bio_nwrite(), write to buffer)
413  */
414 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
415 {
416     struct bio_bio_st *b;
417     size_t num;
418     size_t write_offset;
419
420     BIO_clear_retry_flags(bio);
421
422     if (!bio->init)
423         return 0;
424
425     b = bio->ptr;
426     assert(b != NULL);
427     assert(b->peer != NULL);
428     assert(b->buf != NULL);
429
430     b->request = 0;
431     if (b->closed) {
432         BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
433         return -1;
434     }
435
436     assert(b->len <= b->size);
437
438     if (b->len == b->size) {
439         BIO_set_retry_write(bio);
440         return -1;
441     }
442
443     num = b->size - b->len;
444     write_offset = b->offset + b->len;
445     if (write_offset >= b->size)
446         write_offset -= b->size;
447     if (write_offset + num > b->size)
448         /*
449          * no ring buffer wrap-around for non-copying interface (to fulfil
450          * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
451          * to be called twice)
452          */
453         num = b->size - write_offset;
454
455     if (buf != NULL)
456         *buf = b->buf + write_offset;
457     assert(write_offset + num <= b->size);
458
459     return num;
460 }
461
462 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
463 {
464     struct bio_bio_st *b;
465     ossl_ssize_t num, space;
466
467     if (num_ > SSIZE_MAX)
468         num = SSIZE_MAX;
469     else
470         num = (ossl_ssize_t) num_;
471
472     space = bio_nwrite0(bio, buf);
473     if (num > space)
474         num = space;
475     if (num <= 0)
476         return num;
477     b = bio->ptr;
478     assert(b != NULL);
479     b->len += num;
480     assert(b->len <= b->size);
481
482     return num;
483 }
484
485 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
486 {
487     long ret;
488     struct bio_bio_st *b = bio->ptr;
489
490     assert(b != NULL);
491
492     switch (cmd) {
493         /* specific CTRL codes */
494
495     case BIO_C_SET_WRITE_BUF_SIZE:
496         if (b->peer) {
497             BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
498             ret = 0;
499         } else if (num == 0) {
500             BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
501             ret = 0;
502         } else {
503             size_t new_size = num;
504
505             if (b->size != new_size) {
506                 OPENSSL_free(b->buf);
507                 b->buf = NULL;
508                 b->size = new_size;
509             }
510             ret = 1;
511         }
512         break;
513
514     case BIO_C_GET_WRITE_BUF_SIZE:
515         ret = (long)b->size;
516         break;
517
518     case BIO_C_MAKE_BIO_PAIR:
519         {
520             BIO *other_bio = ptr;
521
522             if (bio_make_pair(bio, other_bio))
523                 ret = 1;
524             else
525                 ret = 0;
526         }
527         break;
528
529     case BIO_C_DESTROY_BIO_PAIR:
530         /*
531          * Affects both BIOs in the pair -- call just once! Or let
532          * BIO_free(bio1); BIO_free(bio2); do the job.
533          */
534         bio_destroy_pair(bio);
535         ret = 1;
536         break;
537
538     case BIO_C_GET_WRITE_GUARANTEE:
539         /*
540          * How many bytes can the caller feed to the next write without
541          * having to keep any?
542          */
543         if (b->peer == NULL || b->closed)
544             ret = 0;
545         else
546             ret = (long)b->size - b->len;
547         break;
548
549     case BIO_C_GET_READ_REQUEST:
550         /*
551          * If the peer unsuccessfully tried to read, how many bytes were
552          * requested? (As with BIO_CTRL_PENDING, that number can usually be
553          * treated as boolean.)
554          */
555         ret = (long)b->request;
556         break;
557
558     case BIO_C_RESET_READ_REQUEST:
559         /*
560          * Reset request.  (Can be useful after read attempts at the other
561          * side that are meant to be non-blocking, e.g. when probing SSL_read
562          * to see if any data is available.)
563          */
564         b->request = 0;
565         ret = 1;
566         break;
567
568     case BIO_C_SHUTDOWN_WR:
569         /* similar to shutdown(..., SHUT_WR) */
570         b->closed = 1;
571         ret = 1;
572         break;
573
574     case BIO_C_NREAD0:
575         /* prepare for non-copying read */
576         ret = (long)bio_nread0(bio, ptr);
577         break;
578
579     case BIO_C_NREAD:
580         /* non-copying read */
581         ret = (long)bio_nread(bio, ptr, (size_t)num);
582         break;
583
584     case BIO_C_NWRITE0:
585         /* prepare for non-copying write */
586         ret = (long)bio_nwrite0(bio, ptr);
587         break;
588
589     case BIO_C_NWRITE:
590         /* non-copying write */
591         ret = (long)bio_nwrite(bio, ptr, (size_t)num);
592         break;
593
594         /* standard CTRL codes follow */
595
596     case BIO_CTRL_RESET:
597         if (b->buf != NULL) {
598             b->len = 0;
599             b->offset = 0;
600         }
601         ret = 0;
602         break;
603
604     case BIO_CTRL_GET_CLOSE:
605         ret = bio->shutdown;
606         break;
607
608     case BIO_CTRL_SET_CLOSE:
609         bio->shutdown = (int)num;
610         ret = 1;
611         break;
612
613     case BIO_CTRL_PENDING:
614         if (b->peer != NULL) {
615             struct bio_bio_st *peer_b = b->peer->ptr;
616
617             ret = (long)peer_b->len;
618         } else
619             ret = 0;
620         break;
621
622     case BIO_CTRL_WPENDING:
623         if (b->buf != NULL)
624             ret = (long)b->len;
625         else
626             ret = 0;
627         break;
628
629     case BIO_CTRL_DUP:
630         /* See BIO_dup_chain for circumstances we have to expect. */
631         {
632             BIO *other_bio = ptr;
633             struct bio_bio_st *other_b;
634
635             assert(other_bio != NULL);
636             other_b = other_bio->ptr;
637             assert(other_b != NULL);
638
639             assert(other_b->buf == NULL); /* other_bio is always fresh */
640
641             other_b->size = b->size;
642         }
643
644         ret = 1;
645         break;
646
647     case BIO_CTRL_FLUSH:
648         ret = 1;
649         break;
650
651     case BIO_CTRL_EOF:
652         {
653             BIO *other_bio = ptr;
654
655             if (other_bio) {
656                 struct bio_bio_st *other_b = other_bio->ptr;
657
658                 assert(other_b != NULL);
659                 ret = other_b->len == 0 && other_b->closed;
660             } else
661                 ret = 1;
662         }
663         break;
664
665     default:
666         ret = 0;
667     }
668     return ret;
669 }
670
671 static int bio_puts(BIO *bio, const char *str)
672 {
673     return bio_write(bio, str, strlen(str));
674 }
675
676 static int bio_make_pair(BIO *bio1, BIO *bio2)
677 {
678     struct bio_bio_st *b1, *b2;
679
680     assert(bio1 != NULL);
681     assert(bio2 != NULL);
682
683     b1 = bio1->ptr;
684     b2 = bio2->ptr;
685
686     if (b1->peer != NULL || b2->peer != NULL) {
687         BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
688         return 0;
689     }
690
691     if (b1->buf == NULL) {
692         b1->buf = OPENSSL_malloc(b1->size);
693         if (b1->buf == NULL) {
694             BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
695             return 0;
696         }
697         b1->len = 0;
698         b1->offset = 0;
699     }
700
701     if (b2->buf == NULL) {
702         b2->buf = OPENSSL_malloc(b2->size);
703         if (b2->buf == NULL) {
704             BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
705             return 0;
706         }
707         b2->len = 0;
708         b2->offset = 0;
709     }
710
711     b1->peer = bio2;
712     b1->closed = 0;
713     b1->request = 0;
714     b2->peer = bio1;
715     b2->closed = 0;
716     b2->request = 0;
717
718     bio1->init = 1;
719     bio2->init = 1;
720
721     return 1;
722 }
723
724 static void bio_destroy_pair(BIO *bio)
725 {
726     struct bio_bio_st *b = bio->ptr;
727
728     if (b != NULL) {
729         BIO *peer_bio = b->peer;
730
731         if (peer_bio != NULL) {
732             struct bio_bio_st *peer_b = peer_bio->ptr;
733
734             assert(peer_b != NULL);
735             assert(peer_b->peer == bio);
736
737             peer_b->peer = NULL;
738             peer_bio->init = 0;
739             assert(peer_b->buf != NULL);
740             peer_b->len = 0;
741             peer_b->offset = 0;
742
743             b->peer = NULL;
744             bio->init = 0;
745             assert(b->buf != NULL);
746             b->len = 0;
747             b->offset = 0;
748         }
749     }
750 }
751
752 /* Exported convenience functions */
753 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
754                      BIO **bio2_p, size_t writebuf2)
755 {
756     BIO *bio1 = NULL, *bio2 = NULL;
757     long r;
758     int ret = 0;
759
760     bio1 = BIO_new(BIO_s_bio());
761     if (bio1 == NULL)
762         goto err;
763     bio2 = BIO_new(BIO_s_bio());
764     if (bio2 == NULL)
765         goto err;
766
767     if (writebuf1) {
768         r = BIO_set_write_buf_size(bio1, writebuf1);
769         if (!r)
770             goto err;
771     }
772     if (writebuf2) {
773         r = BIO_set_write_buf_size(bio2, writebuf2);
774         if (!r)
775             goto err;
776     }
777
778     r = BIO_make_bio_pair(bio1, bio2);
779     if (!r)
780         goto err;
781     ret = 1;
782
783  err:
784     if (ret == 0) {
785         BIO_free(bio1);
786         bio1 = NULL;
787         BIO_free(bio2);
788         bio2 = NULL;
789     }
790
791     *bio1_p = bio1;
792     *bio2_p = bio2;
793     return ret;
794 }
795
796 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
797 {
798     return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
799 }
800
801 size_t BIO_ctrl_get_read_request(BIO *bio)
802 {
803     return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
804 }
805
806 int BIO_ctrl_reset_read_request(BIO *bio)
807 {
808     return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
809 }
810
811 /*
812  * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
813  * (conceivably some other BIOs could allow non-copying reads and writes
814  * too.)
815  */
816 int BIO_nread0(BIO *bio, char **buf)
817 {
818     long ret;
819
820     if (!bio->init) {
821         BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
822         return -2;
823     }
824
825     ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
826     if (ret > INT_MAX)
827         return INT_MAX;
828     else
829         return (int)ret;
830 }
831
832 int BIO_nread(BIO *bio, char **buf, int num)
833 {
834     int ret;
835
836     if (!bio->init) {
837         BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
838         return -2;
839     }
840
841     ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
842     if (ret > 0)
843         bio->num_read += ret;
844     return ret;
845 }
846
847 int BIO_nwrite0(BIO *bio, char **buf)
848 {
849     long ret;
850
851     if (!bio->init) {
852         BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
853         return -2;
854     }
855
856     ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
857     if (ret > INT_MAX)
858         return INT_MAX;
859     else
860         return (int)ret;
861 }
862
863 int BIO_nwrite(BIO *bio, char **buf, int num)
864 {
865     int ret;
866
867     if (!bio->init) {
868         BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
869         return -2;
870     }
871
872     ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
873     if (ret > 0)
874         bio->num_write += ret;
875     return ret;
876 }