Update copyright year
[openssl.git] / crypto / bio / bf_lbuf.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "bio_lcl.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/evp.h>
15
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);
24
25 /* A 10k maximum should be enough for most purposes */
26 #define DEFAULT_LINEBUFFER_SIZE 1024*10
27
28 /* #define DEBUG */
29
30 static const BIO_METHOD methods_linebuffer = {
31     BIO_TYPE_LINEBUFFER,
32     "linebuffer",
33     linebuffer_write,
34     linebuffer_read,
35     linebuffer_puts,
36     linebuffer_gets,
37     linebuffer_ctrl,
38     linebuffer_new,
39     linebuffer_free,
40     linebuffer_callback_ctrl,
41 };
42
43 const BIO_METHOD *BIO_f_linebuffer(void)
44 {
45     return (&methods_linebuffer);
46 }
47
48 typedef struct bio_linebuffer_ctx_struct {
49     char *obuf;                 /* the output char array */
50     int obuf_size;              /* how big is the output buffer */
51     int obuf_len;               /* how many bytes are in it */
52 } BIO_LINEBUFFER_CTX;
53
54 static int linebuffer_new(BIO *bi)
55 {
56     BIO_LINEBUFFER_CTX *ctx;
57
58     ctx = OPENSSL_malloc(sizeof(*ctx));
59     if (ctx == NULL)
60         return (0);
61     ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
62     if (ctx->obuf == NULL) {
63         OPENSSL_free(ctx);
64         return (0);
65     }
66     ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
67     ctx->obuf_len = 0;
68
69     bi->init = 1;
70     bi->ptr = (char *)ctx;
71     bi->flags = 0;
72     return (1);
73 }
74
75 static int linebuffer_free(BIO *a)
76 {
77     BIO_LINEBUFFER_CTX *b;
78
79     if (a == NULL)
80         return (0);
81     b = (BIO_LINEBUFFER_CTX *)a->ptr;
82     OPENSSL_free(b->obuf);
83     OPENSSL_free(a->ptr);
84     a->ptr = NULL;
85     a->init = 0;
86     a->flags = 0;
87     return (1);
88 }
89
90 static int linebuffer_read(BIO *b, char *out, int outl)
91 {
92     int ret = 0;
93
94     if (out == NULL)
95         return (0);
96     if (b->next_bio == NULL)
97         return (0);
98     ret = BIO_read(b->next_bio, out, outl);
99     BIO_clear_retry_flags(b);
100     BIO_copy_next_retry(b);
101     return (ret);
102 }
103
104 static int linebuffer_write(BIO *b, const char *in, int inl)
105 {
106     int i, num = 0, foundnl;
107     BIO_LINEBUFFER_CTX *ctx;
108
109     if ((in == NULL) || (inl <= 0))
110         return (0);
111     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
112     if ((ctx == NULL) || (b->next_bio == NULL))
113         return (0);
114
115     BIO_clear_retry_flags(b);
116
117     do {
118         const char *p;
119         char c;
120
121         for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
122         if (c == '\n') {
123             p++;
124             foundnl = 1;
125         } else
126             foundnl = 0;
127
128         /*
129          * If a NL was found and we already have text in the save buffer,
130          * concatenate them and write
131          */
132         while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
133                && ctx->obuf_len > 0) {
134             int orig_olen = ctx->obuf_len;
135
136             i = ctx->obuf_size - ctx->obuf_len;
137             if (p - in > 0) {
138                 if (i >= p - in) {
139                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
140                     ctx->obuf_len += p - in;
141                     inl -= p - in;
142                     num += p - in;
143                     in = p;
144                 } else {
145                     memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
146                     ctx->obuf_len += i;
147                     inl -= i;
148                     in += i;
149                     num += i;
150                 }
151             }
152             i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
153             if (i <= 0) {
154                 ctx->obuf_len = orig_olen;
155                 BIO_copy_next_retry(b);
156
157                 if (i < 0)
158                     return ((num > 0) ? num : i);
159                 if (i == 0)
160                     return (num);
161             }
162             if (i < ctx->obuf_len)
163                 memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
164             ctx->obuf_len -= i;
165         }
166
167         /*
168          * Now that the save buffer is emptied, let's write the input buffer
169          * if a NL was found and there is anything to write.
170          */
171         if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
172             i = BIO_write(b->next_bio, in, p - in);
173             if (i <= 0) {
174                 BIO_copy_next_retry(b);
175                 if (i < 0)
176                     return ((num > 0) ? num : i);
177                 if (i == 0)
178                     return (num);
179             }
180             num += i;
181             in += i;
182             inl -= i;
183         }
184     }
185     while (foundnl && inl > 0);
186     /*
187      * We've written as much as we can.  The rest of the input buffer, if
188      * any, is text that doesn't and with a NL and therefore needs to be
189      * saved for the next trip.
190      */
191     if (inl > 0) {
192         memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
193         ctx->obuf_len += inl;
194         num += inl;
195     }
196     return num;
197 }
198
199 static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
200 {
201     BIO *dbio;
202     BIO_LINEBUFFER_CTX *ctx;
203     long ret = 1;
204     char *p;
205     int r;
206     int obs;
207
208     ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
209
210     switch (cmd) {
211     case BIO_CTRL_RESET:
212         ctx->obuf_len = 0;
213         if (b->next_bio == NULL)
214             return (0);
215         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
216         break;
217     case BIO_CTRL_INFO:
218         ret = (long)ctx->obuf_len;
219         break;
220     case BIO_CTRL_WPENDING:
221         ret = (long)ctx->obuf_len;
222         if (ret == 0) {
223             if (b->next_bio == NULL)
224                 return (0);
225             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
226         }
227         break;
228     case BIO_C_SET_BUFF_SIZE:
229         obs = (int)num;
230         p = ctx->obuf;
231         if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
232             p = OPENSSL_malloc((int)num);
233             if (p == NULL)
234                 goto malloc_error;
235         }
236         if (ctx->obuf != p) {
237             if (ctx->obuf_len > obs) {
238                 ctx->obuf_len = obs;
239             }
240             memcpy(p, ctx->obuf, ctx->obuf_len);
241             OPENSSL_free(ctx->obuf);
242             ctx->obuf = p;
243             ctx->obuf_size = obs;
244         }
245         break;
246     case BIO_C_DO_STATE_MACHINE:
247         if (b->next_bio == NULL)
248             return (0);
249         BIO_clear_retry_flags(b);
250         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
251         BIO_copy_next_retry(b);
252         break;
253
254     case BIO_CTRL_FLUSH:
255         if (b->next_bio == NULL)
256             return (0);
257         if (ctx->obuf_len <= 0) {
258             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
259             break;
260         }
261
262         for (;;) {
263             BIO_clear_retry_flags(b);
264             if (ctx->obuf_len > 0) {
265                 r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
266                 BIO_copy_next_retry(b);
267                 if (r <= 0)
268                     return ((long)r);
269                 if (r < ctx->obuf_len)
270                     memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
271                 ctx->obuf_len -= r;
272             } else {
273                 ctx->obuf_len = 0;
274                 break;
275             }
276         }
277         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
278         break;
279     case BIO_CTRL_DUP:
280         dbio = (BIO *)ptr;
281         if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
282             ret = 0;
283         break;
284     default:
285         if (b->next_bio == NULL)
286             return (0);
287         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
288         break;
289     }
290     return (ret);
291  malloc_error:
292     BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
293     return (0);
294 }
295
296 static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
297 {
298     long ret = 1;
299
300     if (b->next_bio == NULL)
301         return (0);
302     switch (cmd) {
303     default:
304         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
305         break;
306     }
307     return (ret);
308 }
309
310 static int linebuffer_gets(BIO *b, char *buf, int size)
311 {
312     if (b->next_bio == NULL)
313         return (0);
314     return (BIO_gets(b->next_bio, buf, size));
315 }
316
317 static int linebuffer_puts(BIO *b, const char *str)
318 {
319     return (linebuffer_write(b, str, strlen(str)));
320 }