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