1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
60 #include "internal/cryptlib.h"
61 #include <openssl/bio.h>
63 static int buffer_write(BIO *h, const char *buf, int num);
64 static int buffer_read(BIO *h, char *buf, int size);
65 static int buffer_puts(BIO *h, const char *str);
66 static int buffer_gets(BIO *h, char *str, int size);
67 static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
68 static int buffer_new(BIO *h);
69 static int buffer_free(BIO *data);
70 static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
71 #define DEFAULT_BUFFER_SIZE 4096
73 static BIO_METHOD methods_buffer = {
86 BIO_METHOD *BIO_f_buffer(void)
88 return (&methods_buffer);
91 static int buffer_new(BIO *bi)
93 BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
97 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
98 ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
99 if (ctx->ibuf == NULL) {
103 ctx->obuf_size = DEFAULT_BUFFER_SIZE;
104 ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
105 if (ctx->obuf == NULL) {
106 OPENSSL_free(ctx->ibuf);
112 bi->ptr = (char *)ctx;
117 static int buffer_free(BIO *a)
123 b = (BIO_F_BUFFER_CTX *)a->ptr;
124 OPENSSL_free(b->ibuf);
125 OPENSSL_free(b->obuf);
126 OPENSSL_free(a->ptr);
133 static int buffer_read(BIO *b, char *out, int outl)
136 BIO_F_BUFFER_CTX *ctx;
140 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
142 if ((ctx == NULL) || (b->next_bio == NULL))
145 BIO_clear_retry_flags(b);
149 /* If there is stuff left over, grab it */
153 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
164 * We may have done a partial read. try to do more. We have nothing in
165 * the buffer. If we get an error and have read some data, just return it
166 * and let them retry to get the error again. copy direct to parent
169 if (outl > ctx->ibuf_size) {
171 i = BIO_read(b->next_bio, out, outl);
173 BIO_copy_next_retry(b);
175 return ((num > 0) ? num : i);
188 /* we are going to be doing some buffering */
189 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
191 BIO_copy_next_retry(b);
193 return ((num > 0) ? num : i);
200 /* Lets re-read using ourselves :-) */
204 static int buffer_write(BIO *b, const char *in, int inl)
207 BIO_F_BUFFER_CTX *ctx;
209 if ((in == NULL) || (inl <= 0))
211 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
212 if ((ctx == NULL) || (b->next_bio == NULL))
215 BIO_clear_retry_flags(b);
217 i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
218 /* add to buffer and return */
220 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
221 ctx->obuf_len += inl;
225 /* stuff already in buffer, so add to it first, then flush */
226 if (ctx->obuf_len != 0) {
227 if (i > 0) { /* lets fill it up if we can */
228 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
234 /* we now have a full buffer needing flushing */
236 i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
239 BIO_copy_next_retry(b);
242 return ((num > 0) ? num : i);
248 if (ctx->obuf_len == 0)
253 * we only get here if the buffer has been flushed and we still have
258 /* we now have inl bytes to write */
259 while (inl >= ctx->obuf_size) {
260 i = BIO_write(b->next_bio, in, inl);
262 BIO_copy_next_retry(b);
264 return ((num > 0) ? num : i);
276 * copy the rest into the buffer since we have only a small amount left
281 static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
284 BIO_F_BUFFER_CTX *ctx;
290 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
298 if (b->next_bio == NULL)
300 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
303 ret = (long)ctx->obuf_len;
305 case BIO_C_GET_BUFF_NUM_LINES:
308 for (i = 0; i < ctx->ibuf_len; i++) {
309 if (p1[ctx->ibuf_off + i] == '\n')
313 case BIO_CTRL_WPENDING:
314 ret = (long)ctx->obuf_len;
316 if (b->next_bio == NULL)
318 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
321 case BIO_CTRL_PENDING:
322 ret = (long)ctx->ibuf_len;
324 if (b->next_bio == NULL)
326 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
329 case BIO_C_SET_BUFF_READ_DATA:
330 if (num > ctx->ibuf_size) {
331 p1 = OPENSSL_malloc((int)num);
334 OPENSSL_free(ctx->ibuf);
338 ctx->ibuf_len = (int)num;
339 memcpy(ctx->ibuf, ptr, (int)num);
342 case BIO_C_SET_BUFF_SIZE:
347 obs = ctx->obuf_size;
348 } else { /* if (*ip == 1) */
350 ibs = ctx->ibuf_size;
359 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
360 p1 = OPENSSL_malloc((int)num);
364 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
365 p2 = OPENSSL_malloc((int)num);
372 if (ctx->ibuf != p1) {
373 OPENSSL_free(ctx->ibuf);
377 ctx->ibuf_size = ibs;
379 if (ctx->obuf != p2) {
380 OPENSSL_free(ctx->obuf);
384 ctx->obuf_size = obs;
387 case BIO_C_DO_STATE_MACHINE:
388 if (b->next_bio == NULL)
390 BIO_clear_retry_flags(b);
391 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
392 BIO_copy_next_retry(b);
396 if (b->next_bio == NULL)
398 if (ctx->obuf_len <= 0) {
399 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
404 BIO_clear_retry_flags(b);
405 if (ctx->obuf_len > 0) {
406 r = BIO_write(b->next_bio,
407 &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
408 BIO_copy_next_retry(b);
420 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
424 if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
425 !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
429 if (b->next_bio == NULL)
431 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
436 BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
440 static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
444 if (b->next_bio == NULL)
448 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
454 static int buffer_gets(BIO *b, char *buf, int size)
456 BIO_F_BUFFER_CTX *ctx;
457 int num = 0, i, flag;
460 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
461 size--; /* reserve space for a '\0' */
462 BIO_clear_retry_flags(b);
465 if (ctx->ibuf_len > 0) {
466 p = &(ctx->ibuf[ctx->ibuf_off]);
468 for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
480 if (flag || size == 0) {
484 } else { /* read another chunk */
486 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
488 BIO_copy_next_retry(b);
491 return ((num > 0) ? num : i);
501 static int buffer_puts(BIO *b, const char *str)
503 return (buffer_write(b, str, strlen(str)));