bss_bio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Special method for a BIO where the other endpoint is also a BIO of this
  11. * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
  12. * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
  13. * library with I/O interfaces for which no specific BIO method is available.
  14. * See ssl/ssltest.c for some hints on how this can be used.
  15. */
  16. #include "e_os.h"
  17. #include <assert.h>
  18. #include <limits.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "bio_local.h"
  22. #include <openssl/err.h>
  23. #include <openssl/crypto.h>
  24. static int bio_new(BIO *bio);
  25. static int bio_free(BIO *bio);
  26. static int bio_read(BIO *bio, char *buf, int size);
  27. static int bio_write(BIO *bio, const char *buf, int num);
  28. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
  29. static int bio_puts(BIO *bio, const char *str);
  30. static int bio_make_pair(BIO *bio1, BIO *bio2);
  31. static void bio_destroy_pair(BIO *bio);
  32. static const BIO_METHOD methods_biop = {
  33. BIO_TYPE_BIO,
  34. "BIO pair",
  35. /* TODO: Convert to new style write function */
  36. bwrite_conv,
  37. bio_write,
  38. /* TODO: Convert to new style read function */
  39. bread_conv,
  40. bio_read,
  41. bio_puts,
  42. NULL /* no bio_gets */ ,
  43. bio_ctrl,
  44. bio_new,
  45. bio_free,
  46. NULL /* no bio_callback_ctrl */
  47. };
  48. const BIO_METHOD *BIO_s_bio(void)
  49. {
  50. return &methods_biop;
  51. }
  52. struct bio_bio_st {
  53. BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
  54. * peer->ptr is also a bio_bio_st, and its
  55. * "peer" member points back to us. peer !=
  56. * NULL iff init != 0 in the BIO. */
  57. /* This is for what we write (i.e. reading uses peer's struct): */
  58. int closed; /* valid iff peer != NULL */
  59. size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
  60. size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
  61. size_t size;
  62. char *buf; /* "size" elements (if != NULL) */
  63. size_t request; /* valid iff peer != NULL; 0 if len != 0,
  64. * otherwise set by peer to number of bytes
  65. * it (unsuccessfully) tried to read, never
  66. * more than buffer space (size-len)
  67. * warrants. */
  68. };
  69. static int bio_new(BIO *bio)
  70. {
  71. struct bio_bio_st *b = OPENSSL_zalloc(sizeof(*b));
  72. if (b == NULL)
  73. return 0;
  74. /* enough for one TLS record (just a default) */
  75. b->size = 17 * 1024;
  76. bio->ptr = b;
  77. return 1;
  78. }
  79. static int bio_free(BIO *bio)
  80. {
  81. struct bio_bio_st *b;
  82. if (bio == NULL)
  83. return 0;
  84. b = bio->ptr;
  85. assert(b != NULL);
  86. if (b->peer)
  87. bio_destroy_pair(bio);
  88. OPENSSL_free(b->buf);
  89. OPENSSL_free(b);
  90. return 1;
  91. }
  92. static int bio_read(BIO *bio, char *buf, int size_)
  93. {
  94. size_t size = size_;
  95. size_t rest;
  96. struct bio_bio_st *b, *peer_b;
  97. BIO_clear_retry_flags(bio);
  98. if (!bio->init)
  99. return 0;
  100. b = bio->ptr;
  101. assert(b != NULL);
  102. assert(b->peer != NULL);
  103. peer_b = b->peer->ptr;
  104. assert(peer_b != NULL);
  105. assert(peer_b->buf != NULL);
  106. peer_b->request = 0; /* will be set in "retry_read" situation */
  107. if (buf == NULL || size == 0)
  108. return 0;
  109. if (peer_b->len == 0) {
  110. if (peer_b->closed)
  111. return 0; /* writer has closed, and no data is left */
  112. else {
  113. BIO_set_retry_read(bio); /* buffer is empty */
  114. if (size <= peer_b->size)
  115. peer_b->request = size;
  116. else
  117. /*
  118. * don't ask for more than the peer can deliver in one write
  119. */
  120. peer_b->request = peer_b->size;
  121. return -1;
  122. }
  123. }
  124. /* we can read */
  125. if (peer_b->len < size)
  126. size = peer_b->len;
  127. /* now read "size" bytes */
  128. rest = size;
  129. assert(rest > 0);
  130. do { /* one or two iterations */
  131. size_t chunk;
  132. assert(rest <= peer_b->len);
  133. if (peer_b->offset + rest <= peer_b->size)
  134. chunk = rest;
  135. else
  136. /* wrap around ring buffer */
  137. chunk = peer_b->size - peer_b->offset;
  138. assert(peer_b->offset + chunk <= peer_b->size);
  139. memcpy(buf, peer_b->buf + peer_b->offset, chunk);
  140. peer_b->len -= chunk;
  141. if (peer_b->len) {
  142. peer_b->offset += chunk;
  143. assert(peer_b->offset <= peer_b->size);
  144. if (peer_b->offset == peer_b->size)
  145. peer_b->offset = 0;
  146. buf += chunk;
  147. } else {
  148. /* buffer now empty, no need to advance "buf" */
  149. assert(chunk == rest);
  150. peer_b->offset = 0;
  151. }
  152. rest -= chunk;
  153. }
  154. while (rest);
  155. return size;
  156. }
  157. /*-
  158. * non-copying interface: provide pointer to available data in buffer
  159. * bio_nread0: return number of available bytes
  160. * bio_nread: also advance index
  161. * (example usage: bio_nread0(), read from buffer, bio_nread()
  162. * or just bio_nread(), read from buffer)
  163. */
  164. /*
  165. * WARNING: The non-copying interface is largely untested as of yet and may
  166. * contain bugs.
  167. */
  168. static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
  169. {
  170. struct bio_bio_st *b, *peer_b;
  171. ossl_ssize_t num;
  172. BIO_clear_retry_flags(bio);
  173. if (!bio->init)
  174. return 0;
  175. b = bio->ptr;
  176. assert(b != NULL);
  177. assert(b->peer != NULL);
  178. peer_b = b->peer->ptr;
  179. assert(peer_b != NULL);
  180. assert(peer_b->buf != NULL);
  181. peer_b->request = 0;
  182. if (peer_b->len == 0) {
  183. char dummy;
  184. /* avoid code duplication -- nothing available for reading */
  185. return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
  186. }
  187. num = peer_b->len;
  188. if (peer_b->size < peer_b->offset + num)
  189. /* no ring buffer wrap-around for non-copying interface */
  190. num = peer_b->size - peer_b->offset;
  191. assert(num > 0);
  192. if (buf != NULL)
  193. *buf = peer_b->buf + peer_b->offset;
  194. return num;
  195. }
  196. static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
  197. {
  198. struct bio_bio_st *b, *peer_b;
  199. ossl_ssize_t num, available;
  200. if (num_ > OSSL_SSIZE_MAX)
  201. num = OSSL_SSIZE_MAX;
  202. else
  203. num = (ossl_ssize_t) num_;
  204. available = bio_nread0(bio, buf);
  205. if (num > available)
  206. num = available;
  207. if (num <= 0)
  208. return num;
  209. b = bio->ptr;
  210. peer_b = b->peer->ptr;
  211. peer_b->len -= num;
  212. if (peer_b->len) {
  213. peer_b->offset += num;
  214. assert(peer_b->offset <= peer_b->size);
  215. if (peer_b->offset == peer_b->size)
  216. peer_b->offset = 0;
  217. } else
  218. peer_b->offset = 0;
  219. return num;
  220. }
  221. static int bio_write(BIO *bio, const char *buf, int num_)
  222. {
  223. size_t num = num_;
  224. size_t rest;
  225. struct bio_bio_st *b;
  226. BIO_clear_retry_flags(bio);
  227. if (!bio->init || buf == NULL || num == 0)
  228. return 0;
  229. b = bio->ptr;
  230. assert(b != NULL);
  231. assert(b->peer != NULL);
  232. assert(b->buf != NULL);
  233. b->request = 0;
  234. if (b->closed) {
  235. /* we already closed */
  236. BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
  237. return -1;
  238. }
  239. assert(b->len <= b->size);
  240. if (b->len == b->size) {
  241. BIO_set_retry_write(bio); /* buffer is full */
  242. return -1;
  243. }
  244. /* we can write */
  245. if (num > b->size - b->len)
  246. num = b->size - b->len;
  247. /* now write "num" bytes */
  248. rest = num;
  249. assert(rest > 0);
  250. do { /* one or two iterations */
  251. size_t write_offset;
  252. size_t chunk;
  253. assert(b->len + rest <= b->size);
  254. write_offset = b->offset + b->len;
  255. if (write_offset >= b->size)
  256. write_offset -= b->size;
  257. /* b->buf[write_offset] is the first byte we can write to. */
  258. if (write_offset + rest <= b->size)
  259. chunk = rest;
  260. else
  261. /* wrap around ring buffer */
  262. chunk = b->size - write_offset;
  263. memcpy(b->buf + write_offset, buf, chunk);
  264. b->len += chunk;
  265. assert(b->len <= b->size);
  266. rest -= chunk;
  267. buf += chunk;
  268. }
  269. while (rest);
  270. return num;
  271. }
  272. /*-
  273. * non-copying interface: provide pointer to region to write to
  274. * bio_nwrite0: check how much space is available
  275. * bio_nwrite: also increase length
  276. * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
  277. * or just bio_nwrite(), write to buffer)
  278. */
  279. static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
  280. {
  281. struct bio_bio_st *b;
  282. size_t num;
  283. size_t write_offset;
  284. BIO_clear_retry_flags(bio);
  285. if (!bio->init)
  286. return 0;
  287. b = bio->ptr;
  288. assert(b != NULL);
  289. assert(b->peer != NULL);
  290. assert(b->buf != NULL);
  291. b->request = 0;
  292. if (b->closed) {
  293. BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
  294. return -1;
  295. }
  296. assert(b->len <= b->size);
  297. if (b->len == b->size) {
  298. BIO_set_retry_write(bio);
  299. return -1;
  300. }
  301. num = b->size - b->len;
  302. write_offset = b->offset + b->len;
  303. if (write_offset >= b->size)
  304. write_offset -= b->size;
  305. if (write_offset + num > b->size)
  306. /*
  307. * no ring buffer wrap-around for non-copying interface (to fulfil
  308. * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
  309. * to be called twice)
  310. */
  311. num = b->size - write_offset;
  312. if (buf != NULL)
  313. *buf = b->buf + write_offset;
  314. assert(write_offset + num <= b->size);
  315. return num;
  316. }
  317. static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
  318. {
  319. struct bio_bio_st *b;
  320. ossl_ssize_t num, space;
  321. if (num_ > OSSL_SSIZE_MAX)
  322. num = OSSL_SSIZE_MAX;
  323. else
  324. num = (ossl_ssize_t) num_;
  325. space = bio_nwrite0(bio, buf);
  326. if (num > space)
  327. num = space;
  328. if (num <= 0)
  329. return num;
  330. b = bio->ptr;
  331. assert(b != NULL);
  332. b->len += num;
  333. assert(b->len <= b->size);
  334. return num;
  335. }
  336. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
  337. {
  338. long ret;
  339. struct bio_bio_st *b = bio->ptr;
  340. assert(b != NULL);
  341. switch (cmd) {
  342. /* specific CTRL codes */
  343. case BIO_C_SET_WRITE_BUF_SIZE:
  344. if (b->peer) {
  345. BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
  346. ret = 0;
  347. } else if (num == 0) {
  348. BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
  349. ret = 0;
  350. } else {
  351. size_t new_size = num;
  352. if (b->size != new_size) {
  353. OPENSSL_free(b->buf);
  354. b->buf = NULL;
  355. b->size = new_size;
  356. }
  357. ret = 1;
  358. }
  359. break;
  360. case BIO_C_GET_WRITE_BUF_SIZE:
  361. ret = (long)b->size;
  362. break;
  363. case BIO_C_MAKE_BIO_PAIR:
  364. {
  365. BIO *other_bio = ptr;
  366. if (bio_make_pair(bio, other_bio))
  367. ret = 1;
  368. else
  369. ret = 0;
  370. }
  371. break;
  372. case BIO_C_DESTROY_BIO_PAIR:
  373. /*
  374. * Affects both BIOs in the pair -- call just once! Or let
  375. * BIO_free(bio1); BIO_free(bio2); do the job.
  376. */
  377. bio_destroy_pair(bio);
  378. ret = 1;
  379. break;
  380. case BIO_C_GET_WRITE_GUARANTEE:
  381. /*
  382. * How many bytes can the caller feed to the next write without
  383. * having to keep any?
  384. */
  385. if (b->peer == NULL || b->closed)
  386. ret = 0;
  387. else
  388. ret = (long)b->size - b->len;
  389. break;
  390. case BIO_C_GET_READ_REQUEST:
  391. /*
  392. * If the peer unsuccessfully tried to read, how many bytes were
  393. * requested? (As with BIO_CTRL_PENDING, that number can usually be
  394. * treated as boolean.)
  395. */
  396. ret = (long)b->request;
  397. break;
  398. case BIO_C_RESET_READ_REQUEST:
  399. /*
  400. * Reset request. (Can be useful after read attempts at the other
  401. * side that are meant to be non-blocking, e.g. when probing SSL_read
  402. * to see if any data is available.)
  403. */
  404. b->request = 0;
  405. ret = 1;
  406. break;
  407. case BIO_C_SHUTDOWN_WR:
  408. /* similar to shutdown(..., SHUT_WR) */
  409. b->closed = 1;
  410. ret = 1;
  411. break;
  412. case BIO_C_NREAD0:
  413. /* prepare for non-copying read */
  414. ret = (long)bio_nread0(bio, ptr);
  415. break;
  416. case BIO_C_NREAD:
  417. /* non-copying read */
  418. ret = (long)bio_nread(bio, ptr, (size_t)num);
  419. break;
  420. case BIO_C_NWRITE0:
  421. /* prepare for non-copying write */
  422. ret = (long)bio_nwrite0(bio, ptr);
  423. break;
  424. case BIO_C_NWRITE:
  425. /* non-copying write */
  426. ret = (long)bio_nwrite(bio, ptr, (size_t)num);
  427. break;
  428. /* standard CTRL codes follow */
  429. case BIO_CTRL_RESET:
  430. if (b->buf != NULL) {
  431. b->len = 0;
  432. b->offset = 0;
  433. }
  434. ret = 0;
  435. break;
  436. case BIO_CTRL_GET_CLOSE:
  437. ret = bio->shutdown;
  438. break;
  439. case BIO_CTRL_SET_CLOSE:
  440. bio->shutdown = (int)num;
  441. ret = 1;
  442. break;
  443. case BIO_CTRL_PENDING:
  444. if (b->peer != NULL) {
  445. struct bio_bio_st *peer_b = b->peer->ptr;
  446. ret = (long)peer_b->len;
  447. } else
  448. ret = 0;
  449. break;
  450. case BIO_CTRL_WPENDING:
  451. if (b->buf != NULL)
  452. ret = (long)b->len;
  453. else
  454. ret = 0;
  455. break;
  456. case BIO_CTRL_DUP:
  457. /* See BIO_dup_chain for circumstances we have to expect. */
  458. {
  459. BIO *other_bio = ptr;
  460. struct bio_bio_st *other_b;
  461. assert(other_bio != NULL);
  462. other_b = other_bio->ptr;
  463. assert(other_b != NULL);
  464. assert(other_b->buf == NULL); /* other_bio is always fresh */
  465. other_b->size = b->size;
  466. }
  467. ret = 1;
  468. break;
  469. case BIO_CTRL_FLUSH:
  470. ret = 1;
  471. break;
  472. case BIO_CTRL_EOF:
  473. if (b->peer != NULL) {
  474. struct bio_bio_st *peer_b = b->peer->ptr;
  475. if (peer_b->len == 0 && peer_b->closed)
  476. ret = 1;
  477. else
  478. ret = 0;
  479. } else {
  480. ret = 1;
  481. }
  482. break;
  483. default:
  484. ret = 0;
  485. }
  486. return ret;
  487. }
  488. static int bio_puts(BIO *bio, const char *str)
  489. {
  490. return bio_write(bio, str, strlen(str));
  491. }
  492. static int bio_make_pair(BIO *bio1, BIO *bio2)
  493. {
  494. struct bio_bio_st *b1, *b2;
  495. assert(bio1 != NULL);
  496. assert(bio2 != NULL);
  497. b1 = bio1->ptr;
  498. b2 = bio2->ptr;
  499. if (b1->peer != NULL || b2->peer != NULL) {
  500. BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
  501. return 0;
  502. }
  503. if (b1->buf == NULL) {
  504. b1->buf = OPENSSL_malloc(b1->size);
  505. if (b1->buf == NULL) {
  506. BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
  507. return 0;
  508. }
  509. b1->len = 0;
  510. b1->offset = 0;
  511. }
  512. if (b2->buf == NULL) {
  513. b2->buf = OPENSSL_malloc(b2->size);
  514. if (b2->buf == NULL) {
  515. BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
  516. return 0;
  517. }
  518. b2->len = 0;
  519. b2->offset = 0;
  520. }
  521. b1->peer = bio2;
  522. b1->closed = 0;
  523. b1->request = 0;
  524. b2->peer = bio1;
  525. b2->closed = 0;
  526. b2->request = 0;
  527. bio1->init = 1;
  528. bio2->init = 1;
  529. return 1;
  530. }
  531. static void bio_destroy_pair(BIO *bio)
  532. {
  533. struct bio_bio_st *b = bio->ptr;
  534. if (b != NULL) {
  535. BIO *peer_bio = b->peer;
  536. if (peer_bio != NULL) {
  537. struct bio_bio_st *peer_b = peer_bio->ptr;
  538. assert(peer_b != NULL);
  539. assert(peer_b->peer == bio);
  540. peer_b->peer = NULL;
  541. peer_bio->init = 0;
  542. assert(peer_b->buf != NULL);
  543. peer_b->len = 0;
  544. peer_b->offset = 0;
  545. b->peer = NULL;
  546. bio->init = 0;
  547. assert(b->buf != NULL);
  548. b->len = 0;
  549. b->offset = 0;
  550. }
  551. }
  552. }
  553. /* Exported convenience functions */
  554. int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
  555. BIO **bio2_p, size_t writebuf2)
  556. {
  557. BIO *bio1 = NULL, *bio2 = NULL;
  558. long r;
  559. int ret = 0;
  560. bio1 = BIO_new(BIO_s_bio());
  561. if (bio1 == NULL)
  562. goto err;
  563. bio2 = BIO_new(BIO_s_bio());
  564. if (bio2 == NULL)
  565. goto err;
  566. if (writebuf1) {
  567. r = BIO_set_write_buf_size(bio1, writebuf1);
  568. if (!r)
  569. goto err;
  570. }
  571. if (writebuf2) {
  572. r = BIO_set_write_buf_size(bio2, writebuf2);
  573. if (!r)
  574. goto err;
  575. }
  576. r = BIO_make_bio_pair(bio1, bio2);
  577. if (!r)
  578. goto err;
  579. ret = 1;
  580. err:
  581. if (ret == 0) {
  582. BIO_free(bio1);
  583. bio1 = NULL;
  584. BIO_free(bio2);
  585. bio2 = NULL;
  586. }
  587. *bio1_p = bio1;
  588. *bio2_p = bio2;
  589. return ret;
  590. }
  591. size_t BIO_ctrl_get_write_guarantee(BIO *bio)
  592. {
  593. return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
  594. }
  595. size_t BIO_ctrl_get_read_request(BIO *bio)
  596. {
  597. return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
  598. }
  599. int BIO_ctrl_reset_read_request(BIO *bio)
  600. {
  601. return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
  602. }
  603. /*
  604. * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
  605. * (conceivably some other BIOs could allow non-copying reads and writes
  606. * too.)
  607. */
  608. int BIO_nread0(BIO *bio, char **buf)
  609. {
  610. long ret;
  611. if (!bio->init) {
  612. BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
  613. return -2;
  614. }
  615. ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
  616. if (ret > INT_MAX)
  617. return INT_MAX;
  618. else
  619. return (int)ret;
  620. }
  621. int BIO_nread(BIO *bio, char **buf, int num)
  622. {
  623. int ret;
  624. if (!bio->init) {
  625. BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
  626. return -2;
  627. }
  628. ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
  629. if (ret > 0)
  630. bio->num_read += ret;
  631. return ret;
  632. }
  633. int BIO_nwrite0(BIO *bio, char **buf)
  634. {
  635. long ret;
  636. if (!bio->init) {
  637. BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
  638. return -2;
  639. }
  640. ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
  641. if (ret > INT_MAX)
  642. return INT_MAX;
  643. else
  644. return (int)ret;
  645. }
  646. int BIO_nwrite(BIO *bio, char **buf, int num)
  647. {
  648. int ret;
  649. if (!bio->init) {
  650. BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
  651. return -2;
  652. }
  653. ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
  654. if (ret > 0)
  655. bio->num_write += ret;
  656. return ret;
  657. }