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