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