2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include "internal/cryptlib.h"
15 static int buffer_write(BIO *h, const char *buf, int num);
16 static int buffer_read(BIO *h, char *buf, int size);
17 static int buffer_puts(BIO *h, const char *str);
18 static int buffer_gets(BIO *h, char *str, int size);
19 static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20 static int buffer_new(BIO *h);
21 static int buffer_free(BIO *data);
22 static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
23 #define DEFAULT_BUFFER_SIZE 4096
25 static const BIO_METHOD methods_buffer = {
38 const BIO_METHOD *BIO_f_buffer(void)
40 return (&methods_buffer);
43 static int buffer_new(BIO *bi)
45 BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
49 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
50 ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
51 if (ctx->ibuf == NULL) {
55 ctx->obuf_size = DEFAULT_BUFFER_SIZE;
56 ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
57 if (ctx->obuf == NULL) {
58 OPENSSL_free(ctx->ibuf);
64 bi->ptr = (char *)ctx;
69 static int buffer_free(BIO *a)
75 b = (BIO_F_BUFFER_CTX *)a->ptr;
76 OPENSSL_free(b->ibuf);
77 OPENSSL_free(b->obuf);
85 static int buffer_read(BIO *b, char *out, int outl)
88 BIO_F_BUFFER_CTX *ctx;
92 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
94 if ((ctx == NULL) || (b->next_bio == NULL))
97 BIO_clear_retry_flags(b);
101 /* If there is stuff left over, grab it */
105 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
116 * We may have done a partial read. try to do more. We have nothing in
117 * the buffer. If we get an error and have read some data, just return it
118 * and let them retry to get the error again. copy direct to parent
121 if (outl > ctx->ibuf_size) {
123 i = BIO_read(b->next_bio, out, outl);
125 BIO_copy_next_retry(b);
127 return ((num > 0) ? num : i);
140 /* we are going to be doing some buffering */
141 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
143 BIO_copy_next_retry(b);
145 return ((num > 0) ? num : i);
152 /* Lets re-read using ourselves :-) */
156 static int buffer_write(BIO *b, const char *in, int inl)
159 BIO_F_BUFFER_CTX *ctx;
161 if ((in == NULL) || (inl <= 0))
163 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
164 if ((ctx == NULL) || (b->next_bio == NULL))
167 BIO_clear_retry_flags(b);
169 i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
170 /* add to buffer and return */
172 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
173 ctx->obuf_len += inl;
177 /* stuff already in buffer, so add to it first, then flush */
178 if (ctx->obuf_len != 0) {
179 if (i > 0) { /* lets fill it up if we can */
180 memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
186 /* we now have a full buffer needing flushing */
188 i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
191 BIO_copy_next_retry(b);
194 return ((num > 0) ? num : i);
200 if (ctx->obuf_len == 0)
205 * we only get here if the buffer has been flushed and we still have
210 /* we now have inl bytes to write */
211 while (inl >= ctx->obuf_size) {
212 i = BIO_write(b->next_bio, in, inl);
214 BIO_copy_next_retry(b);
216 return ((num > 0) ? num : i);
228 * copy the rest into the buffer since we have only a small amount left
233 static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
236 BIO_F_BUFFER_CTX *ctx;
242 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
250 if (b->next_bio == NULL)
252 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
255 ret = (long)ctx->obuf_len;
257 case BIO_C_GET_BUFF_NUM_LINES:
260 for (i = 0; i < ctx->ibuf_len; i++) {
261 if (p1[ctx->ibuf_off + i] == '\n')
265 case BIO_CTRL_WPENDING:
266 ret = (long)ctx->obuf_len;
268 if (b->next_bio == NULL)
270 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
273 case BIO_CTRL_PENDING:
274 ret = (long)ctx->ibuf_len;
276 if (b->next_bio == NULL)
278 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
281 case BIO_C_SET_BUFF_READ_DATA:
282 if (num > ctx->ibuf_size) {
283 p1 = OPENSSL_malloc((int)num);
286 OPENSSL_free(ctx->ibuf);
290 ctx->ibuf_len = (int)num;
291 memcpy(ctx->ibuf, ptr, (int)num);
294 case BIO_C_SET_BUFF_SIZE:
299 obs = ctx->obuf_size;
300 } else { /* if (*ip == 1) */
302 ibs = ctx->ibuf_size;
311 if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
312 p1 = OPENSSL_malloc((int)num);
316 if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
317 p2 = OPENSSL_malloc((int)num);
324 if (ctx->ibuf != p1) {
325 OPENSSL_free(ctx->ibuf);
329 ctx->ibuf_size = ibs;
331 if (ctx->obuf != p2) {
332 OPENSSL_free(ctx->obuf);
336 ctx->obuf_size = obs;
339 case BIO_C_DO_STATE_MACHINE:
340 if (b->next_bio == NULL)
342 BIO_clear_retry_flags(b);
343 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
344 BIO_copy_next_retry(b);
348 if (b->next_bio == NULL)
350 if (ctx->obuf_len <= 0) {
351 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
356 BIO_clear_retry_flags(b);
357 if (ctx->obuf_len > 0) {
358 r = BIO_write(b->next_bio,
359 &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
360 BIO_copy_next_retry(b);
371 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
375 if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
376 !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
380 if (b->next_bio == NULL)
382 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
387 BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
391 static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
395 if (b->next_bio == NULL)
399 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
405 static int buffer_gets(BIO *b, char *buf, int size)
407 BIO_F_BUFFER_CTX *ctx;
408 int num = 0, i, flag;
411 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
412 size--; /* reserve space for a '\0' */
413 BIO_clear_retry_flags(b);
416 if (ctx->ibuf_len > 0) {
417 p = &(ctx->ibuf[ctx->ibuf_off]);
419 for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
431 if (flag || size == 0) {
435 } else { /* read another chunk */
437 i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
439 BIO_copy_next_retry(b);
442 return ((num > 0) ? num : i);
452 static int buffer_puts(BIO *b, const char *str)
454 return (buffer_write(b, str, strlen(str)));