2 * Copyright 1995-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
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/evp.h>
16 static int linebuffer_write(BIO *h, const char *buf, int num);
17 static int linebuffer_read(BIO *h, char *buf, int size);
18 static int linebuffer_puts(BIO *h, const char *str);
19 static int linebuffer_gets(BIO *h, char *str, int size);
20 static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
21 static int linebuffer_new(BIO *h);
22 static int linebuffer_free(BIO *data);
23 static long linebuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
25 /* A 10k maximum should be enough for most purposes */
26 #define DEFAULT_LINEBUFFER_SIZE 1024*10
30 static const BIO_METHOD methods_linebuffer = {
42 linebuffer_callback_ctrl,
45 const BIO_METHOD *BIO_f_linebuffer(void)
47 return &methods_linebuffer;
50 typedef struct bio_linebuffer_ctx_struct {
51 char *obuf; /* the output char array */
52 int obuf_size; /* how big is the output buffer */
53 int obuf_len; /* how many bytes are in it */
56 static int linebuffer_new(BIO *bi)
58 BIO_LINEBUFFER_CTX *ctx;
60 if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
61 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
64 ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
65 if (ctx->obuf == NULL) {
66 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
70 ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
74 bi->ptr = (char *)ctx;
79 static int linebuffer_free(BIO *a)
81 BIO_LINEBUFFER_CTX *b;
85 b = (BIO_LINEBUFFER_CTX *)a->ptr;
86 OPENSSL_free(b->obuf);
94 static int linebuffer_read(BIO *b, char *out, int outl)
100 if (b->next_bio == NULL)
102 ret = BIO_read(b->next_bio, out, outl);
103 BIO_clear_retry_flags(b);
104 BIO_copy_next_retry(b);
108 static int linebuffer_write(BIO *b, const char *in, int inl)
110 int i, num = 0, foundnl;
111 BIO_LINEBUFFER_CTX *ctx;
113 if ((in == NULL) || (inl <= 0))
115 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
116 if ((ctx == NULL) || (b->next_bio == NULL))
119 BIO_clear_retry_flags(b);
125 for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
133 * If a NL was found and we already have text in the save buffer,
134 * concatenate them and write
136 while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
137 && ctx->obuf_len > 0) {
138 int orig_olen = ctx->obuf_len;
140 i = ctx->obuf_size - ctx->obuf_len;
143 memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
144 ctx->obuf_len += p - in;
149 memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
156 i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
158 ctx->obuf_len = orig_olen;
159 BIO_copy_next_retry(b);
162 return ((num > 0) ? num : i);
166 if (i < ctx->obuf_len)
167 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
172 * Now that the save buffer is emptied, let's write the input buffer
173 * if a NL was found and there is anything to write.
175 if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
176 i = BIO_write(b->next_bio, in, p - in);
178 BIO_copy_next_retry(b);
180 return ((num > 0) ? num : i);
189 while (foundnl && inl > 0);
191 * We've written as much as we can. The rest of the input buffer, if
192 * any, is text that doesn't and with a NL and therefore needs to be
193 * saved for the next trip.
196 memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
197 ctx->obuf_len += inl;
203 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
206 BIO_LINEBUFFER_CTX *ctx;
212 ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
217 if (b->next_bio == NULL)
219 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
222 ret = (long)ctx->obuf_len;
224 case BIO_CTRL_WPENDING:
225 ret = (long)ctx->obuf_len;
227 if (b->next_bio == NULL)
229 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
232 case BIO_C_SET_BUFF_SIZE:
237 if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
238 p = OPENSSL_malloc((size_t)obs);
242 if (ctx->obuf != p) {
243 if (ctx->obuf_len > obs) {
246 memcpy(p, ctx->obuf, ctx->obuf_len);
247 OPENSSL_free(ctx->obuf);
249 ctx->obuf_size = obs;
252 case BIO_C_DO_STATE_MACHINE:
253 if (b->next_bio == NULL)
255 BIO_clear_retry_flags(b);
256 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
257 BIO_copy_next_retry(b);
261 if (b->next_bio == NULL)
263 if (ctx->obuf_len <= 0) {
264 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
269 BIO_clear_retry_flags(b);
270 if (ctx->obuf_len > 0) {
271 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
272 BIO_copy_next_retry(b);
275 if (r < ctx->obuf_len)
276 memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
283 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
287 if (BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
291 if (b->next_bio == NULL)
293 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
298 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
302 static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
304 if (b->next_bio == NULL)
306 return BIO_callback_ctrl(b->next_bio, cmd, fp);
309 static int linebuffer_gets(BIO *b, char *buf, int size)
311 if (b->next_bio == NULL)
313 return BIO_gets(b->next_bio, buf, size);
316 static int linebuffer_puts(BIO *b, const char *str)
318 return linebuffer_write(b, str, strlen(str));