Line data Source code
1 : /* crypto/bio/bf_buff.c */
2 : /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 : * All rights reserved.
4 : *
5 : * This package is an SSL implementation written
6 : * by Eric Young (eay@cryptsoft.com).
7 : * The implementation was written so as to conform with Netscapes SSL.
8 : *
9 : * This library is free for commercial and non-commercial use as long as
10 : * the following conditions are aheared to. The following conditions
11 : * apply to all code found in this distribution, be it the RC4, RSA,
12 : * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 : * included with this distribution is covered by the same copyright terms
14 : * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 : *
16 : * Copyright remains Eric Young's, and as such any Copyright notices in
17 : * the code are not to be removed.
18 : * If this package is used in a product, Eric Young should be given attribution
19 : * as the author of the parts of the library used.
20 : * This can be in the form of a textual message at program startup or
21 : * in documentation (online or textual) provided with the package.
22 : *
23 : * Redistribution and use in source and binary forms, with or without
24 : * modification, are permitted provided that the following conditions
25 : * are met:
26 : * 1. Redistributions of source code must retain the copyright
27 : * notice, this list of conditions and the following disclaimer.
28 : * 2. Redistributions in binary form must reproduce the above copyright
29 : * notice, this list of conditions and the following disclaimer in the
30 : * documentation and/or other materials provided with the distribution.
31 : * 3. All advertising materials mentioning features or use of this software
32 : * must display the following acknowledgement:
33 : * "This product includes cryptographic software written by
34 : * Eric Young (eay@cryptsoft.com)"
35 : * The word 'cryptographic' can be left out if the rouines from the library
36 : * being used are not cryptographic related :-).
37 : * 4. If you include any Windows specific code (or a derivative thereof) from
38 : * the apps directory (application code) you must include an acknowledgement:
39 : * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 : *
41 : * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 : * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 : * SUCH DAMAGE.
52 : *
53 : * The licence and distribution terms for any publically available version or
54 : * derivative of this code cannot be changed. i.e. this code cannot simply be
55 : * copied and put under another distribution licence
56 : * [including the GNU Public Licence.]
57 : */
58 :
59 : #include <stdio.h>
60 : #include <errno.h>
61 : #include "cryptlib.h"
62 : #include <openssl/bio.h>
63 :
64 : static int buffer_write(BIO *h, const char *buf, int num);
65 : static int buffer_read(BIO *h, char *buf, int size);
66 : static int buffer_puts(BIO *h, const char *str);
67 : static int buffer_gets(BIO *h, char *str, int size);
68 : static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69 : static int buffer_new(BIO *h);
70 : static int buffer_free(BIO *data);
71 : static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72 : #define DEFAULT_BUFFER_SIZE 4096
73 :
74 : static BIO_METHOD methods_buffer = {
75 : BIO_TYPE_BUFFER,
76 : "buffer",
77 : buffer_write,
78 : buffer_read,
79 : buffer_puts,
80 : buffer_gets,
81 : buffer_ctrl,
82 : buffer_new,
83 : buffer_free,
84 : buffer_callback_ctrl,
85 : };
86 :
87 746 : BIO_METHOD *BIO_f_buffer(void)
88 : {
89 746 : return (&methods_buffer);
90 : }
91 :
92 746 : static int buffer_new(BIO *bi)
93 : {
94 : BIO_F_BUFFER_CTX *ctx;
95 :
96 746 : ctx = (BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
97 746 : if (ctx == NULL)
98 : return (0);
99 746 : ctx->ibuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
100 746 : if (ctx->ibuf == NULL) {
101 0 : OPENSSL_free(ctx);
102 0 : return (0);
103 : }
104 746 : ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
105 746 : if (ctx->obuf == NULL) {
106 0 : OPENSSL_free(ctx->ibuf);
107 0 : OPENSSL_free(ctx);
108 0 : return (0);
109 : }
110 746 : ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
111 746 : ctx->obuf_size = DEFAULT_BUFFER_SIZE;
112 746 : ctx->ibuf_len = 0;
113 746 : ctx->ibuf_off = 0;
114 746 : ctx->obuf_len = 0;
115 746 : ctx->obuf_off = 0;
116 :
117 746 : bi->init = 1;
118 746 : bi->ptr = (char *)ctx;
119 746 : bi->flags = 0;
120 746 : return (1);
121 : }
122 :
123 746 : static int buffer_free(BIO *a)
124 : {
125 : BIO_F_BUFFER_CTX *b;
126 :
127 746 : if (a == NULL)
128 : return (0);
129 746 : b = (BIO_F_BUFFER_CTX *)a->ptr;
130 746 : if (b->ibuf != NULL)
131 746 : OPENSSL_free(b->ibuf);
132 746 : if (b->obuf != NULL)
133 746 : OPENSSL_free(b->obuf);
134 746 : OPENSSL_free(a->ptr);
135 746 : a->ptr = NULL;
136 746 : a->init = 0;
137 746 : a->flags = 0;
138 746 : return (1);
139 : }
140 :
141 0 : static int buffer_read(BIO *b, char *out, int outl)
142 : {
143 : int i, num = 0;
144 : BIO_F_BUFFER_CTX *ctx;
145 :
146 0 : if (out == NULL)
147 : return (0);
148 0 : ctx = (BIO_F_BUFFER_CTX *)b->ptr;
149 :
150 0 : if ((ctx == NULL) || (b->next_bio == NULL))
151 : return (0);
152 : num = 0;
153 0 : BIO_clear_retry_flags(b);
154 :
155 : start:
156 0 : i = ctx->ibuf_len;
157 : /* If there is stuff left over, grab it */
158 0 : if (i != 0) {
159 0 : if (i > outl)
160 : i = outl;
161 0 : memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
162 0 : ctx->ibuf_off += i;
163 0 : ctx->ibuf_len -= i;
164 0 : num += i;
165 0 : if (outl == i)
166 : return (num);
167 0 : outl -= i;
168 0 : out += i;
169 : }
170 :
171 : /*
172 : * We may have done a partial read. try to do more. We have nothing in
173 : * the buffer. If we get an error and have read some data, just return it
174 : * and let them retry to get the error again. copy direct to parent
175 : * address space
176 : */
177 0 : if (outl > ctx->ibuf_size) {
178 : for (;;) {
179 0 : i = BIO_read(b->next_bio, out, outl);
180 0 : if (i <= 0) {
181 0 : BIO_copy_next_retry(b);
182 0 : if (i < 0)
183 0 : return ((num > 0) ? num : i);
184 0 : if (i == 0)
185 : return (num);
186 : }
187 0 : num += i;
188 0 : if (outl == i)
189 : return (num);
190 0 : out += i;
191 0 : outl -= i;
192 0 : }
193 : }
194 : /* else */
195 :
196 : /* we are going to be doing some buffering */
197 0 : i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
198 0 : if (i <= 0) {
199 0 : BIO_copy_next_retry(b);
200 0 : if (i < 0)
201 0 : return ((num > 0) ? num : i);
202 0 : if (i == 0)
203 : return (num);
204 : }
205 0 : ctx->ibuf_off = 0;
206 0 : ctx->ibuf_len = i;
207 :
208 : /* Lets re-read using ourselves :-) */
209 0 : goto start;
210 : }
211 :
212 3703 : static int buffer_write(BIO *b, const char *in, int inl)
213 : {
214 : int i, num = 0;
215 : BIO_F_BUFFER_CTX *ctx;
216 :
217 3703 : if ((in == NULL) || (inl <= 0))
218 : return (0);
219 3703 : ctx = (BIO_F_BUFFER_CTX *)b->ptr;
220 3703 : if ((ctx == NULL) || (b->next_bio == NULL))
221 : return (0);
222 :
223 3703 : BIO_clear_retry_flags(b);
224 : start:
225 3703 : i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
226 : /* add to buffer and return */
227 3703 : if (i >= inl) {
228 3703 : memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
229 3703 : ctx->obuf_len += inl;
230 3703 : return (num + inl);
231 : }
232 : /* else */
233 : /* stuff already in buffer, so add to it first, then flush */
234 0 : if (ctx->obuf_len != 0) {
235 0 : if (i > 0) { /* lets fill it up if we can */
236 0 : memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
237 0 : in += i;
238 0 : inl -= i;
239 0 : num += i;
240 0 : ctx->obuf_len += i;
241 : }
242 : /* we now have a full buffer needing flushing */
243 : for (;;) {
244 0 : i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
245 : ctx->obuf_len);
246 0 : if (i <= 0) {
247 0 : BIO_copy_next_retry(b);
248 :
249 0 : if (i < 0)
250 0 : return ((num > 0) ? num : i);
251 0 : if (i == 0)
252 : return (num);
253 : }
254 0 : ctx->obuf_off += i;
255 0 : ctx->obuf_len -= i;
256 0 : if (ctx->obuf_len == 0)
257 : break;
258 : }
259 : }
260 : /*
261 : * we only get here if the buffer has been flushed and we still have
262 : * stuff to write
263 : */
264 0 : ctx->obuf_off = 0;
265 :
266 : /* we now have inl bytes to write */
267 0 : while (inl >= ctx->obuf_size) {
268 0 : i = BIO_write(b->next_bio, in, inl);
269 0 : if (i <= 0) {
270 0 : BIO_copy_next_retry(b);
271 0 : if (i < 0)
272 0 : return ((num > 0) ? num : i);
273 0 : if (i == 0)
274 : return (num);
275 : }
276 0 : num += i;
277 0 : in += i;
278 0 : inl -= i;
279 0 : if (inl == 0)
280 : return (num);
281 : }
282 :
283 : /*
284 : * copy the rest into the buffer since we have only a small amount left
285 : */
286 : goto start;
287 : }
288 :
289 4094 : static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
290 : {
291 : BIO *dbio;
292 : BIO_F_BUFFER_CTX *ctx;
293 : long ret = 1;
294 : char *p1, *p2;
295 : int r, i, *ip;
296 : int ibs, obs;
297 :
298 4094 : ctx = (BIO_F_BUFFER_CTX *)b->ptr;
299 :
300 4094 : switch (cmd) {
301 : case BIO_CTRL_RESET:
302 746 : ctx->ibuf_off = 0;
303 746 : ctx->ibuf_len = 0;
304 746 : ctx->obuf_off = 0;
305 746 : ctx->obuf_len = 0;
306 746 : if (b->next_bio == NULL)
307 : return (0);
308 0 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
309 0 : break;
310 : case BIO_CTRL_INFO:
311 0 : ret = (long)ctx->obuf_len;
312 0 : break;
313 : case BIO_C_GET_BUFF_NUM_LINES:
314 : ret = 0;
315 0 : p1 = ctx->ibuf;
316 0 : for (i = 0; i < ctx->ibuf_len; i++) {
317 0 : if (p1[ctx->ibuf_off + i] == '\n')
318 0 : ret++;
319 : }
320 : break;
321 : case BIO_CTRL_WPENDING:
322 0 : ret = (long)ctx->obuf_len;
323 0 : if (ret == 0) {
324 0 : if (b->next_bio == NULL)
325 : return (0);
326 0 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
327 : }
328 : break;
329 : case BIO_CTRL_PENDING:
330 0 : ret = (long)ctx->ibuf_len;
331 0 : if (ret == 0) {
332 0 : if (b->next_bio == NULL)
333 : return (0);
334 0 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
335 : }
336 : break;
337 : case BIO_C_SET_BUFF_READ_DATA:
338 0 : if (num > ctx->ibuf_size) {
339 0 : p1 = OPENSSL_malloc((int)num);
340 0 : if (p1 == NULL)
341 : goto malloc_error;
342 0 : if (ctx->ibuf != NULL)
343 0 : OPENSSL_free(ctx->ibuf);
344 0 : ctx->ibuf = p1;
345 : }
346 0 : ctx->ibuf_off = 0;
347 0 : ctx->ibuf_len = (int)num;
348 0 : memcpy(ctx->ibuf, ptr, (int)num);
349 : ret = 1;
350 0 : break;
351 : case BIO_C_SET_BUFF_SIZE:
352 746 : if (ptr != NULL) {
353 : ip = (int *)ptr;
354 746 : if (*ip == 0) {
355 746 : ibs = (int)num;
356 746 : obs = ctx->obuf_size;
357 : } else { /* if (*ip == 1) */
358 :
359 0 : ibs = ctx->ibuf_size;
360 0 : obs = (int)num;
361 : }
362 : } else {
363 0 : ibs = (int)num;
364 : obs = (int)num;
365 : }
366 746 : p1 = ctx->ibuf;
367 746 : p2 = ctx->obuf;
368 746 : if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
369 0 : p1 = (char *)OPENSSL_malloc((int)num);
370 0 : if (p1 == NULL)
371 : goto malloc_error;
372 : }
373 746 : if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
374 0 : p2 = (char *)OPENSSL_malloc((int)num);
375 0 : if (p2 == NULL) {
376 0 : if (p1 != ctx->ibuf)
377 0 : OPENSSL_free(p1);
378 : goto malloc_error;
379 : }
380 : }
381 746 : if (ctx->ibuf != p1) {
382 0 : OPENSSL_free(ctx->ibuf);
383 0 : ctx->ibuf = p1;
384 0 : ctx->ibuf_off = 0;
385 0 : ctx->ibuf_len = 0;
386 0 : ctx->ibuf_size = ibs;
387 : }
388 746 : if (ctx->obuf != p2) {
389 0 : OPENSSL_free(ctx->obuf);
390 0 : ctx->obuf = p2;
391 0 : ctx->obuf_off = 0;
392 0 : ctx->obuf_len = 0;
393 0 : ctx->obuf_size = obs;
394 : }
395 : break;
396 : case BIO_C_DO_STATE_MACHINE:
397 0 : if (b->next_bio == NULL)
398 : return (0);
399 0 : BIO_clear_retry_flags(b);
400 0 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
401 0 : BIO_copy_next_retry(b);
402 0 : break;
403 :
404 : case BIO_CTRL_FLUSH:
405 1110 : if (b->next_bio == NULL)
406 : return (0);
407 1110 : if (ctx->obuf_len <= 0) {
408 0 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
409 0 : break;
410 : }
411 :
412 : for (;;) {
413 2220 : BIO_clear_retry_flags(b);
414 2220 : if (ctx->obuf_len > 0) {
415 1110 : r = BIO_write(b->next_bio,
416 1110 : &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
417 : #if 0
418 : fprintf(stderr, "FLUSH [%3d] %3d -> %3d\n", ctx->obuf_off,
419 : ctx->obuf_len, r);
420 : #endif
421 1110 : BIO_copy_next_retry(b);
422 1110 : if (r <= 0)
423 0 : return ((long)r);
424 1110 : ctx->obuf_off += r;
425 1110 : ctx->obuf_len -= r;
426 : } else {
427 1110 : ctx->obuf_len = 0;
428 1110 : ctx->obuf_off = 0;
429 : ret = 1;
430 : break;
431 : }
432 1110 : }
433 1110 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
434 1110 : break;
435 : case BIO_CTRL_DUP:
436 : dbio = (BIO *)ptr;
437 0 : if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
438 0 : !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
439 : ret = 0;
440 : break;
441 : default:
442 1492 : if (b->next_bio == NULL)
443 : return (0);
444 1492 : ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
445 1492 : break;
446 : }
447 3348 : return (ret);
448 : malloc_error:
449 0 : BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
450 0 : return (0);
451 : }
452 :
453 0 : static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
454 : {
455 : long ret = 1;
456 :
457 0 : if (b->next_bio == NULL)
458 : return (0);
459 : switch (cmd) {
460 : default:
461 0 : ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
462 : break;
463 : }
464 0 : return (ret);
465 : }
466 :
467 0 : static int buffer_gets(BIO *b, char *buf, int size)
468 : {
469 : BIO_F_BUFFER_CTX *ctx;
470 : int num = 0, i, flag;
471 : char *p;
472 :
473 0 : ctx = (BIO_F_BUFFER_CTX *)b->ptr;
474 0 : size--; /* reserve space for a '\0' */
475 0 : BIO_clear_retry_flags(b);
476 :
477 : for (;;) {
478 0 : if (ctx->ibuf_len > 0) {
479 0 : p = &(ctx->ibuf[ctx->ibuf_off]);
480 : flag = 0;
481 0 : for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
482 0 : *(buf++) = p[i];
483 0 : if (p[i] == '\n') {
484 : flag = 1;
485 0 : i++;
486 0 : break;
487 : }
488 : }
489 0 : num += i;
490 0 : size -= i;
491 0 : ctx->ibuf_len -= i;
492 0 : ctx->ibuf_off += i;
493 0 : if (flag || size == 0) {
494 0 : *buf = '\0';
495 0 : return (num);
496 : }
497 : } else { /* read another chunk */
498 :
499 0 : i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
500 0 : if (i <= 0) {
501 0 : BIO_copy_next_retry(b);
502 0 : *buf = '\0';
503 0 : if (i < 0)
504 0 : return ((num > 0) ? num : i);
505 0 : if (i == 0)
506 : return (num);
507 : }
508 0 : ctx->ibuf_len = i;
509 0 : ctx->ibuf_off = 0;
510 : }
511 : }
512 : }
513 :
514 0 : static int buffer_puts(BIO *b, const char *str)
515 : {
516 0 : return (buffer_write(b, str, strlen(str)));
517 : }
|