2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * This is a read only BIO filter that can be used to add BIO_tell() and
12 * BIO_seek() support to source/sink BIO's (such as a file BIO that uses stdin).
13 * It does this by caching ALL data read from the BIO source/sink into a
14 * resizable memory buffer.
19 #include "bio_local.h"
20 #include "internal/cryptlib.h"
22 #define DEFAULT_BUFFER_SIZE 4096
24 static int readbuffer_write(BIO *h, const char *buf, int num);
25 static int readbuffer_read(BIO *h, char *buf, int size);
26 static int readbuffer_puts(BIO *h, const char *str);
27 static int readbuffer_gets(BIO *h, char *str, int size);
28 static long readbuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
29 static int readbuffer_new(BIO *h);
30 static int readbuffer_free(BIO *data);
31 static long readbuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
33 static const BIO_METHOD methods_readbuffer = {
45 readbuffer_callback_ctrl,
48 const BIO_METHOD *BIO_f_readbuffer(void)
50 return &methods_readbuffer;
53 static int readbuffer_new(BIO *bi)
55 BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
59 ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
60 ctx->ibuf = OPENSSL_zalloc(DEFAULT_BUFFER_SIZE);
61 if (ctx->ibuf == NULL) {
67 bi->ptr = (char *)ctx;
72 static int readbuffer_free(BIO *a)
78 b = (BIO_F_BUFFER_CTX *)a->ptr;
79 OPENSSL_free(b->ibuf);
87 static int readbuffer_resize(BIO_F_BUFFER_CTX *ctx, int sz)
91 /* Figure out how many blocks are required */
92 sz += (ctx->ibuf_off + DEFAULT_BUFFER_SIZE - 1);
93 sz = DEFAULT_BUFFER_SIZE * (sz / DEFAULT_BUFFER_SIZE);
95 /* Resize if the buffer is not big enough */
96 if (sz > ctx->ibuf_size) {
97 tmp = OPENSSL_realloc(ctx->ibuf, sz);
106 static int readbuffer_read(BIO *b, char *out, int outl)
109 BIO_F_BUFFER_CTX *ctx;
111 if (out == NULL || outl == 0)
113 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
115 if ((ctx == NULL) || (b->next_bio == NULL))
117 BIO_clear_retry_flags(b);
121 /* If there is something in the buffer just read it. */
125 memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
129 /* Exit if we have read the bytes required out of the buffer */
136 /* Only gets here if the buffer has been consumed */
137 if (!readbuffer_resize(ctx, outl))
140 /* Do some buffering by reading from the next bio */
141 i = BIO_read(b->next_bio, ctx->ibuf + ctx->ibuf_off, outl);
143 BIO_copy_next_retry(b);
145 return ((num > 0) ? num : i);
147 return num; /* i == 0 */
153 static int readbuffer_write(BIO *b, const char *in, int inl)
157 static int readbuffer_puts(BIO *b, const char *str)
162 static long readbuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
164 BIO_F_BUFFER_CTX *ctx;
167 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
171 if (ctx->ibuf_len > 0)
173 if (b->next_bio == NULL)
175 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
178 case BIO_C_FILE_SEEK:
180 sz = ctx->ibuf_off + ctx->ibuf_len;
181 /* Assume it can only seek backwards */
182 if (num < 0 || num > sz)
185 ctx->ibuf_len = sz - num;
188 case BIO_C_FILE_TELL:
190 ret = (long)ctx->ibuf_off;
192 case BIO_CTRL_PENDING:
193 ret = (long)ctx->ibuf_len;
195 if (b->next_bio == NULL)
197 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
211 static long readbuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
213 if (b->next_bio == NULL)
215 return BIO_callback_ctrl(b->next_bio, cmd, fp);
218 static int readbuffer_gets(BIO *b, char *buf, int size)
220 BIO_F_BUFFER_CTX *ctx;
221 int num = 0, num_chars, found_newline;
227 --size; /* the passed in size includes the terminator - so remove it here */
228 ctx = (BIO_F_BUFFER_CTX *)b->ptr;
229 BIO_clear_retry_flags(b);
231 /* If data is already buffered then use this first */
232 if (ctx->ibuf_len > 0) {
233 p = ctx->ibuf + ctx->ibuf_off;
236 (num_chars < ctx->ibuf_len) && (num_chars < size);
238 *buf++ = p[num_chars];
239 if (p[num_chars] == '\n') {
247 ctx->ibuf_len -= num_chars;
248 ctx->ibuf_off += num_chars;
249 if (found_newline || size == 0) {
255 * If there is no buffered data left then read any remaining data from the
259 /* Resize if we have to */
260 if (!readbuffer_resize(ctx, 1 + size))
263 * Read more data from the next bio using BIO_read_ex:
264 * Note we cannot use BIO_gets() here as it does not work on a
265 * binary stream that contains 0x00. (Since strlen() will stop at
266 * any 0x00 not at the last read '\n' in a FILE bio).
267 * Also note that some applications open and close the file bio
268 * multiple times and need to read the next available block when using
269 * stdin - so we need to READ one byte at a time!
271 p = ctx->ibuf + ctx->ibuf_off;
272 for (i = 0; i < size; ++i) {
273 j = BIO_read(b->next_bio, p, 1);
275 BIO_copy_next_retry(b);
277 return num > 0 ? num : j;