fc0b8fa648f8bcfca73fb5b7d4c6140ad43edcda
[openssl.git] / crypto / bio / bf_buff.c
1 /*
2  * Copyright 1995-2016 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
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
24
25 static const BIO_METHOD methods_buffer = {
26     BIO_TYPE_BUFFER,
27     "buffer",
28     buffer_write,
29     /* TODO: Convert to new style read function */
30     bread_conv,
31     buffer_read,
32     buffer_puts,
33     buffer_gets,
34     buffer_ctrl,
35     buffer_new,
36     buffer_free,
37     buffer_callback_ctrl,
38 };
39
40 const BIO_METHOD *BIO_f_buffer(void)
41 {
42     return (&methods_buffer);
43 }
44
45 static int buffer_new(BIO *bi)
46 {
47     BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
48
49     if (ctx == NULL)
50         return (0);
51     ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
52     ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
53     if (ctx->ibuf == NULL) {
54         OPENSSL_free(ctx);
55         return (0);
56     }
57     ctx->obuf_size = DEFAULT_BUFFER_SIZE;
58     ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
59     if (ctx->obuf == NULL) {
60         OPENSSL_free(ctx->ibuf);
61         OPENSSL_free(ctx);
62         return (0);
63     }
64
65     bi->init = 1;
66     bi->ptr = (char *)ctx;
67     bi->flags = 0;
68     return (1);
69 }
70
71 static int buffer_free(BIO *a)
72 {
73     BIO_F_BUFFER_CTX *b;
74
75     if (a == NULL)
76         return (0);
77     b = (BIO_F_BUFFER_CTX *)a->ptr;
78     OPENSSL_free(b->ibuf);
79     OPENSSL_free(b->obuf);
80     OPENSSL_free(a->ptr);
81     a->ptr = NULL;
82     a->init = 0;
83     a->flags = 0;
84     return (1);
85 }
86
87 static int buffer_read(BIO *b, char *out, int outl)
88 {
89     int i, num = 0;
90     BIO_F_BUFFER_CTX *ctx;
91
92     if (out == NULL)
93         return (0);
94     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
95
96     if ((ctx == NULL) || (b->next_bio == NULL))
97         return (0);
98     num = 0;
99     BIO_clear_retry_flags(b);
100
101  start:
102     i = ctx->ibuf_len;
103     /* If there is stuff left over, grab it */
104     if (i != 0) {
105         if (i > outl)
106             i = outl;
107         memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
108         ctx->ibuf_off += i;
109         ctx->ibuf_len -= i;
110         num += i;
111         if (outl == i)
112             return (num);
113         outl -= i;
114         out += i;
115     }
116
117     /*
118      * We may have done a partial read. try to do more. We have nothing in
119      * the buffer. If we get an error and have read some data, just return it
120      * and let them retry to get the error again. copy direct to parent
121      * address space
122      */
123     if (outl > ctx->ibuf_size) {
124         for (;;) {
125             i = BIO_read(b->next_bio, out, outl);
126             if (i <= 0) {
127                 BIO_copy_next_retry(b);
128                 if (i < 0)
129                     return ((num > 0) ? num : i);
130                 if (i == 0)
131                     return (num);
132             }
133             num += i;
134             if (outl == i)
135                 return (num);
136             out += i;
137             outl -= i;
138         }
139     }
140     /* else */
141
142     /* we are going to be doing some buffering */
143     i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
144     if (i <= 0) {
145         BIO_copy_next_retry(b);
146         if (i < 0)
147             return ((num > 0) ? num : i);
148         if (i == 0)
149             return (num);
150     }
151     ctx->ibuf_off = 0;
152     ctx->ibuf_len = i;
153
154     /* Lets re-read using ourselves :-) */
155     goto start;
156 }
157
158 static int buffer_write(BIO *b, const char *in, int inl)
159 {
160     int i, num = 0;
161     BIO_F_BUFFER_CTX *ctx;
162
163     if ((in == NULL) || (inl <= 0))
164         return (0);
165     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
166     if ((ctx == NULL) || (b->next_bio == NULL))
167         return (0);
168
169     BIO_clear_retry_flags(b);
170  start:
171     i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
172     /* add to buffer and return */
173     if (i >= inl) {
174         memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
175         ctx->obuf_len += inl;
176         return (num + inl);
177     }
178     /* else */
179     /* stuff already in buffer, so add to it first, then flush */
180     if (ctx->obuf_len != 0) {
181         if (i > 0) {            /* lets fill it up if we can */
182             memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
183             in += i;
184             inl -= i;
185             num += i;
186             ctx->obuf_len += i;
187         }
188         /* we now have a full buffer needing flushing */
189         for (;;) {
190             i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
191                           ctx->obuf_len);
192             if (i <= 0) {
193                 BIO_copy_next_retry(b);
194
195                 if (i < 0)
196                     return ((num > 0) ? num : i);
197                 if (i == 0)
198                     return (num);
199             }
200             ctx->obuf_off += i;
201             ctx->obuf_len -= i;
202             if (ctx->obuf_len == 0)
203                 break;
204         }
205     }
206     /*
207      * we only get here if the buffer has been flushed and we still have
208      * stuff to write
209      */
210     ctx->obuf_off = 0;
211
212     /* we now have inl bytes to write */
213     while (inl >= ctx->obuf_size) {
214         i = BIO_write(b->next_bio, in, inl);
215         if (i <= 0) {
216             BIO_copy_next_retry(b);
217             if (i < 0)
218                 return ((num > 0) ? num : i);
219             if (i == 0)
220                 return (num);
221         }
222         num += i;
223         in += i;
224         inl -= i;
225         if (inl == 0)
226             return (num);
227     }
228
229     /*
230      * copy the rest into the buffer since we have only a small amount left
231      */
232     goto start;
233 }
234
235 static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
236 {
237     BIO *dbio;
238     BIO_F_BUFFER_CTX *ctx;
239     long ret = 1;
240     char *p1, *p2;
241     int r, i, *ip;
242     int ibs, obs;
243
244     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
245
246     switch (cmd) {
247     case BIO_CTRL_RESET:
248         ctx->ibuf_off = 0;
249         ctx->ibuf_len = 0;
250         ctx->obuf_off = 0;
251         ctx->obuf_len = 0;
252         if (b->next_bio == NULL)
253             return (0);
254         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
255         break;
256     case BIO_CTRL_INFO:
257         ret = (long)ctx->obuf_len;
258         break;
259     case BIO_C_GET_BUFF_NUM_LINES:
260         ret = 0;
261         p1 = ctx->ibuf;
262         for (i = 0; i < ctx->ibuf_len; i++) {
263             if (p1[ctx->ibuf_off + i] == '\n')
264                 ret++;
265         }
266         break;
267     case BIO_CTRL_WPENDING:
268         ret = (long)ctx->obuf_len;
269         if (ret == 0) {
270             if (b->next_bio == NULL)
271                 return (0);
272             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
273         }
274         break;
275     case BIO_CTRL_PENDING:
276         ret = (long)ctx->ibuf_len;
277         if (ret == 0) {
278             if (b->next_bio == NULL)
279                 return (0);
280             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
281         }
282         break;
283     case BIO_C_SET_BUFF_READ_DATA:
284         if (num > ctx->ibuf_size) {
285             p1 = OPENSSL_malloc((int)num);
286             if (p1 == NULL)
287                 goto malloc_error;
288             OPENSSL_free(ctx->ibuf);
289             ctx->ibuf = p1;
290         }
291         ctx->ibuf_off = 0;
292         ctx->ibuf_len = (int)num;
293         memcpy(ctx->ibuf, ptr, (int)num);
294         ret = 1;
295         break;
296     case BIO_C_SET_BUFF_SIZE:
297         if (ptr != NULL) {
298             ip = (int *)ptr;
299             if (*ip == 0) {
300                 ibs = (int)num;
301                 obs = ctx->obuf_size;
302             } else {            /* if (*ip == 1) */
303
304                 ibs = ctx->ibuf_size;
305                 obs = (int)num;
306             }
307         } else {
308             ibs = (int)num;
309             obs = (int)num;
310         }
311         p1 = ctx->ibuf;
312         p2 = ctx->obuf;
313         if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
314             p1 = OPENSSL_malloc((int)num);
315             if (p1 == NULL)
316                 goto malloc_error;
317         }
318         if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
319             p2 = OPENSSL_malloc((int)num);
320             if (p2 == NULL) {
321                 if (p1 != ctx->ibuf)
322                     OPENSSL_free(p1);
323                 goto malloc_error;
324             }
325         }
326         if (ctx->ibuf != p1) {
327             OPENSSL_free(ctx->ibuf);
328             ctx->ibuf = p1;
329             ctx->ibuf_off = 0;
330             ctx->ibuf_len = 0;
331             ctx->ibuf_size = ibs;
332         }
333         if (ctx->obuf != p2) {
334             OPENSSL_free(ctx->obuf);
335             ctx->obuf = p2;
336             ctx->obuf_off = 0;
337             ctx->obuf_len = 0;
338             ctx->obuf_size = obs;
339         }
340         break;
341     case BIO_C_DO_STATE_MACHINE:
342         if (b->next_bio == NULL)
343             return (0);
344         BIO_clear_retry_flags(b);
345         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
346         BIO_copy_next_retry(b);
347         break;
348
349     case BIO_CTRL_FLUSH:
350         if (b->next_bio == NULL)
351             return (0);
352         if (ctx->obuf_len <= 0) {
353             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
354             break;
355         }
356
357         for (;;) {
358             BIO_clear_retry_flags(b);
359             if (ctx->obuf_len > 0) {
360                 r = BIO_write(b->next_bio,
361                               &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
362                 BIO_copy_next_retry(b);
363                 if (r <= 0)
364                     return ((long)r);
365                 ctx->obuf_off += r;
366                 ctx->obuf_len -= r;
367             } else {
368                 ctx->obuf_len = 0;
369                 ctx->obuf_off = 0;
370                 break;
371             }
372         }
373         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
374         break;
375     case BIO_CTRL_DUP:
376         dbio = (BIO *)ptr;
377         if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
378             !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
379             ret = 0;
380         break;
381     default:
382         if (b->next_bio == NULL)
383             return (0);
384         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
385         break;
386     }
387     return (ret);
388  malloc_error:
389     BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
390     return (0);
391 }
392
393 static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
394 {
395     long ret = 1;
396
397     if (b->next_bio == NULL)
398         return (0);
399     switch (cmd) {
400     default:
401         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
402         break;
403     }
404     return (ret);
405 }
406
407 static int buffer_gets(BIO *b, char *buf, int size)
408 {
409     BIO_F_BUFFER_CTX *ctx;
410     int num = 0, i, flag;
411     char *p;
412
413     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
414     size--;                     /* reserve space for a '\0' */
415     BIO_clear_retry_flags(b);
416
417     for (;;) {
418         if (ctx->ibuf_len > 0) {
419             p = &(ctx->ibuf[ctx->ibuf_off]);
420             flag = 0;
421             for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
422                 *(buf++) = p[i];
423                 if (p[i] == '\n') {
424                     flag = 1;
425                     i++;
426                     break;
427                 }
428             }
429             num += i;
430             size -= i;
431             ctx->ibuf_len -= i;
432             ctx->ibuf_off += i;
433             if (flag || size == 0) {
434                 *buf = '\0';
435                 return (num);
436             }
437         } else {                /* read another chunk */
438
439             i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
440             if (i <= 0) {
441                 BIO_copy_next_retry(b);
442                 *buf = '\0';
443                 if (i < 0)
444                     return ((num > 0) ? num : i);
445                 if (i == 0)
446                     return (num);
447             }
448             ctx->ibuf_len = i;
449             ctx->ibuf_off = 0;
450         }
451     }
452 }
453
454 static int buffer_puts(BIO *b, const char *str)
455 {
456     return (buffer_write(b, str, strlen(str)));
457 }