Add ossl_siv symbols
[openssl.git] / crypto / bio / bf_readbuff.c
1 /*
2  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*
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.
15  */
16
17 #include <stdio.h>
18 #include <errno.h>
19 #include "bio_local.h"
20 #include "internal/cryptlib.h"
21
22 #define DEFAULT_BUFFER_SIZE     4096
23
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);
32
33 static const BIO_METHOD methods_readbuffer = {
34     BIO_TYPE_BUFFER,
35     "readbuffer",
36     bwrite_conv,
37     readbuffer_write,
38     bread_conv,
39     readbuffer_read,
40     readbuffer_puts,
41     readbuffer_gets,
42     readbuffer_ctrl,
43     readbuffer_new,
44     readbuffer_free,
45     readbuffer_callback_ctrl,
46 };
47
48 const BIO_METHOD *BIO_f_readbuffer(void)
49 {
50     return &methods_readbuffer;
51 }
52
53 static int readbuffer_new(BIO *bi)
54 {
55     BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
56
57     if (ctx == NULL)
58         return 0;
59     ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
60     ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
61     if (ctx->ibuf == NULL) {
62         OPENSSL_free(ctx);
63         return 0;
64     }
65
66     bi->init = 1;
67     bi->ptr = (char *)ctx;
68     bi->flags = 0;
69     return 1;
70 }
71
72 static int readbuffer_free(BIO *a)
73 {
74     BIO_F_BUFFER_CTX *b;
75
76     if (a == NULL)
77         return 0;
78     b = (BIO_F_BUFFER_CTX *)a->ptr;
79     OPENSSL_free(b->ibuf);
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 readbuffer_resize(BIO_F_BUFFER_CTX *ctx, int sz)
88 {
89     char *tmp;
90
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);
94
95     /* Resize if the buffer is not big enough */
96     if (sz > ctx->ibuf_size) {
97         tmp = OPENSSL_realloc(ctx->ibuf, sz);
98         if (tmp == NULL)
99             return 0;
100         ctx->ibuf = tmp;
101         ctx->ibuf_size = sz;
102     }
103     return 1;
104 }
105
106 static int readbuffer_read(BIO *b, char *out, int outl)
107 {
108     int i, num = 0;
109     BIO_F_BUFFER_CTX *ctx;
110
111     if (out == NULL || outl == 0)
112         return 0;
113     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
114
115     if ((ctx == NULL) || (b->next_bio == NULL))
116         return 0;
117     BIO_clear_retry_flags(b);
118
119     for (;;) {
120         i = ctx->ibuf_len;
121         /* If there is something in the buffer just read it. */
122         if (i != 0) {
123             if (i > outl)
124                 i = outl;
125             memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
126             ctx->ibuf_off += i;
127             ctx->ibuf_len -= i;
128             num += i;
129             /* Exit if we have read the bytes required out of the buffer */
130             if (outl == i)
131                 return num;
132             outl -= i;
133             out += i;
134         }
135
136         /* Only gets here if the buffer has been consumed */
137         if (!readbuffer_resize(ctx, outl))
138             return 0;
139
140         /* Do some buffering by reading from the next bio */
141         i = BIO_read(b->next_bio, ctx->ibuf + ctx->ibuf_off, outl);
142         if (i <= 0) {
143             BIO_copy_next_retry(b);
144             if (i < 0)
145                 return ((num > 0) ? num : i);
146             else
147                 return num; /* i == 0 */
148         }
149         ctx->ibuf_len = i;
150     }
151 }
152
153 static int readbuffer_write(BIO *b, const char *in, int inl)
154 {
155     return 0;
156 }
157 static int readbuffer_puts(BIO *b, const char *str)
158 {
159     return 0;
160 }
161
162 static long readbuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
163 {
164     BIO_F_BUFFER_CTX *ctx;
165     long ret = 1, sz;
166
167     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
168
169     switch (cmd) {
170     case BIO_CTRL_EOF:
171         if (ctx->ibuf_len > 0)
172             return 0;
173         if (b->next_bio == NULL)
174             return 1;
175         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
176         break;
177
178     case BIO_C_FILE_SEEK:
179     case BIO_CTRL_RESET:
180         sz = ctx->ibuf_off + ctx->ibuf_len;
181         /* Assume it can only seek backwards */
182         if (num < 0 || num > sz)
183             return 0;
184         ctx->ibuf_off = num;
185         ctx->ibuf_len = sz - num;
186         break;
187
188     case BIO_C_FILE_TELL:
189     case BIO_CTRL_INFO:
190         ret = (long)ctx->ibuf_off;
191         break;
192     case BIO_CTRL_PENDING:
193         ret = (long)ctx->ibuf_len;
194         if (ret == 0) {
195             if (b->next_bio == NULL)
196                 return 0;
197             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
198         }
199         break;
200     case BIO_CTRL_DUP:
201     case BIO_CTRL_FLUSH:
202         ret = 1;
203         break;
204     default:
205         ret = 0;
206         break;
207     }
208     return ret;
209 }
210
211 static long readbuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
212 {
213     if (b->next_bio == NULL)
214         return 0;
215     return BIO_callback_ctrl(b->next_bio, cmd, fp);
216 }
217
218 static int readbuffer_gets(BIO *b, char *buf, int size)
219 {
220     BIO_F_BUFFER_CTX *ctx;
221     int num = 0, num_chars, found_newline;
222     char *p;
223
224     if (size == 0)
225         return 0;
226     --size; /* the passed in size includes the terminator - so remove it here */
227     ctx = (BIO_F_BUFFER_CTX *)b->ptr;
228     BIO_clear_retry_flags(b);
229
230     for (;;) {
231         if (ctx->ibuf_len > 0) {
232             p = &(ctx->ibuf[ctx->ibuf_off]);
233             found_newline = 0;
234             for (num_chars = 0;
235                  (num_chars < ctx->ibuf_len) && (num_chars < size);
236                  num_chars++) {
237                 *(buf++) = p[num_chars];
238                 if (p[num_chars] == '\n') {
239                     found_newline = 1;
240                     num_chars++;
241                     break;
242                 }
243             }
244             num += num_chars;
245             size -= num_chars;
246             ctx->ibuf_len -= num_chars;
247             ctx->ibuf_off += num_chars;
248             if (found_newline || size == 0) {
249                 *buf = '\0';
250                 return num;
251             }
252         } else {
253             /* read another line and resize if we have to */
254             if (!readbuffer_resize(ctx, size))
255                 return 0;
256
257             /* Read another line from the next bio using BIO_gets */
258             num_chars = BIO_gets(b->next_bio, ctx->ibuf + ctx->ibuf_off,
259                                  1 + size);
260             if (num_chars <= 0) {
261                 BIO_copy_next_retry(b);
262                 *buf = '\0';
263                 return num > 0 ? num : num_chars;
264             }
265             ctx->ibuf_len = num_chars;
266         }
267     }
268 }