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