1 /* crypto/bio/bf_buff.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
62 #include <openssl/bio.h>
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
74 static BIO_METHOD methods_buffer = {
87 BIO_METHOD *BIO_f_buffer(void)
89 return (&methods_buffer);
92 static int buffer_new(BIO *bi)
94 BIO_F_BUFFER_CTX *ctx;
96 ctx = (BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
99 ctx->ibuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
100 if (ctx->ibuf == NULL) {
104 ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
105 if (ctx->obuf == NULL) {
106 OPENSSL_free(ctx->ibuf);
110 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
111 ctx->obuf_size = DEFAULT_BUFFER_SIZE;
118 bi->ptr = (char *)ctx;
123 static int buffer_free(BIO *a)
129 b = (BIO_F_BUFFER_CTX *)a->ptr;
131 OPENSSL_free(b->ibuf);
133 OPENSSL_free(b->obuf);
134 OPENSSL_free(a->ptr);
141 static int buffer_read(BIO *b, char *out, int outl)
144 BIO_F_BUFFER_CTX *ctx;
148 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
150 if ((ctx == NULL) || (b->next_bio == NULL))
153 BIO_clear_retry_flags(b);
157 /* If there is stuff left over, grab it */
161 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
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
177 if (outl > ctx->ibuf_size) {
179 i = BIO_read(b->next_bio, out, outl);
181 BIO_copy_next_retry(b);
183 return ((num > 0) ? num : i);
196 /* we are going to be doing some buffering */
197 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
199 BIO_copy_next_retry(b);
201 return ((num > 0) ? num : i);
208 /* Lets re-read using ourselves :-) */
212 static int buffer_write(BIO *b, const char *in, int inl)
215 BIO_F_BUFFER_CTX *ctx;
217 if ((in == NULL) || (inl <= 0))
219 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
220 if ((ctx == NULL) || (b->next_bio == NULL))
223 BIO_clear_retry_flags(b);
225 i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
226 /* add to buffer and return */
228 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
229 ctx->obuf_len += inl;
233 /* stuff already in buffer, so add to it first, then flush */
234 if (ctx->obuf_len != 0) {
235 if (i > 0) { /* lets fill it up if we can */
236 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
242 /* we now have a full buffer needing flushing */
244 i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
247 BIO_copy_next_retry(b);
250 return ((num > 0) ? num : i);
256 if (ctx->obuf_len == 0)
261 * we only get here if the buffer has been flushed and we still have
266 /* we now have inl bytes to write */
267 while (inl >= ctx->obuf_size) {
268 i = BIO_write(b->next_bio, in, inl);
270 BIO_copy_next_retry(b);
272 return ((num > 0) ? num : i);
284 * copy the rest into the buffer since we have only a small amount left
289 static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
292 BIO_F_BUFFER_CTX *ctx;
298 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
306 if (b->next_bio == NULL)
308 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
311 ret = (long)ctx->obuf_len;
313 case BIO_C_GET_BUFF_NUM_LINES:
316 for (i = 0; i < ctx->ibuf_len; i++) {
317 if (p1[ctx->ibuf_off + i] == '\n')
321 case BIO_CTRL_WPENDING:
322 ret = (long)ctx->obuf_len;
324 if (b->next_bio == NULL)
326 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
329 case BIO_CTRL_PENDING:
330 ret = (long)ctx->ibuf_len;
332 if (b->next_bio == NULL)
334 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
337 case BIO_C_SET_BUFF_READ_DATA:
338 if (num > ctx->ibuf_size) {
339 p1 = OPENSSL_malloc((int)num);
342 if (ctx->ibuf != NULL)
343 OPENSSL_free(ctx->ibuf);
347 ctx->ibuf_len = (int)num;
348 memcpy(ctx->ibuf, ptr, (int)num);
351 case BIO_C_SET_BUFF_SIZE:
356 obs = ctx->obuf_size;
357 } else { /* if (*ip == 1) */
359 ibs = ctx->ibuf_size;
368 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
369 p1 = (char *)OPENSSL_malloc((int)num);
373 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
374 p2 = (char *)OPENSSL_malloc((int)num);
381 if (ctx->ibuf != p1) {
382 OPENSSL_free(ctx->ibuf);
386 ctx->ibuf_size = ibs;
388 if (ctx->obuf != p2) {
389 OPENSSL_free(ctx->obuf);
393 ctx->obuf_size = obs;
396 case BIO_C_DO_STATE_MACHINE:
397 if (b->next_bio == NULL)
399 BIO_clear_retry_flags(b);
400 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
401 BIO_copy_next_retry(b);
405 if (b->next_bio == NULL)
407 if (ctx->obuf_len <= 0) {
408 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
413 BIO_clear_retry_flags(b);
414 if (ctx->obuf_len > 0) {
415 r = BIO_write(b->next_bio,
416 &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
418 fprintf(stderr, "FLUSH [%3d] %3d -> %3d\n", ctx->obuf_off,
421 BIO_copy_next_retry(b);
433 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
437 if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
438 !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
442 if (b->next_bio == NULL)
444 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
449 BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
453 static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
457 if (b->next_bio == NULL)
461 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
467 static int buffer_gets(BIO *b, char *buf, int size)
469 BIO_F_BUFFER_CTX *ctx;
470 int num = 0, i, flag;
473 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
474 size--; /* reserve space for a '\0' */
475 BIO_clear_retry_flags(b);
478 if (ctx->ibuf_len > 0) {
479 p = &(ctx->ibuf[ctx->ibuf_off]);
481 for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
493 if (flag || size == 0) {
497 } else { /* read another chunk */
499 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
501 BIO_copy_next_retry(b);
504 return ((num > 0) ? num : i);
514 static int buffer_puts(BIO *b, const char *str)
516 return (buffer_write(b, str, strlen(str)));