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