summaryrefslogtreecommitdiff
path: root/crypto/bio/bf_readbuff.c
blob: 135ccef83bf3c425729c7c2503ecd8128e69d5b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

/*
 * This is a read only BIO filter that can be used to add BIO_tell() and
 * BIO_seek() support to source/sink BIO's (such as a file BIO that uses stdin).
 * It does this by caching ALL data read from the BIO source/sink into a
 * resizable memory buffer.
 */

#include <stdio.h>
#include <errno.h>
#include "bio_local.h"
#include "internal/cryptlib.h"

#define DEFAULT_BUFFER_SIZE     4096

static int readbuffer_write(BIO *h, const char *buf, int num);
static int readbuffer_read(BIO *h, char *buf, int size);
static int readbuffer_puts(BIO *h, const char *str);
static int readbuffer_gets(BIO *h, char *str, int size);
static long readbuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int readbuffer_new(BIO *h);
static int readbuffer_free(BIO *data);
static long readbuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);

static const BIO_METHOD methods_readbuffer = {
    BIO_TYPE_BUFFER,
    "readbuffer",
    bwrite_conv,
    readbuffer_write,
    bread_conv,
    readbuffer_read,
    readbuffer_puts,
    readbuffer_gets,
    readbuffer_ctrl,
    readbuffer_new,
    readbuffer_free,
    readbuffer_callback_ctrl,
};

const BIO_METHOD *BIO_f_readbuffer(void)
{
    return &methods_readbuffer;
}

static int readbuffer_new(BIO *bi)
{
    BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));

    if (ctx == NULL)
        return 0;
    ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
    ctx->ibuf = OPENSSL_zalloc(DEFAULT_BUFFER_SIZE);
    if (ctx->ibuf == NULL) {
        OPENSSL_free(ctx);
        return 0;
    }

    bi->init = 1;
    bi->ptr = (char *)ctx;
    bi->flags = 0;
    return 1;
}

static int readbuffer_free(BIO *a)
{
    BIO_F_BUFFER_CTX *b;

    if (a == NULL)
        return 0;
    b = (BIO_F_BUFFER_CTX *)a->ptr;
    OPENSSL_free(b->ibuf);
    OPENSSL_free(a->ptr);
    a->ptr = NULL;
    a->init = 0;
    a->flags = 0;
    return 1;
}

static int readbuffer_resize(BIO_F_BUFFER_CTX *ctx, int sz)
{
    char *tmp;

    /* Figure out how many blocks are required */
    sz += (ctx->ibuf_off + DEFAULT_BUFFER_SIZE - 1);
    sz = DEFAULT_BUFFER_SIZE * (sz / DEFAULT_BUFFER_SIZE);

    /* Resize if the buffer is not big enough */
    if (sz > ctx->ibuf_size) {
        tmp = OPENSSL_realloc(ctx->ibuf, sz);
        if (tmp == NULL)
            return 0;
        ctx->ibuf = tmp;
        ctx->ibuf_size = sz;
    }
    return 1;
}

static int readbuffer_read(BIO *b, char *out, int outl)
{
    int i, num = 0;
    BIO_F_BUFFER_CTX *ctx;

    if (out == NULL || outl == 0)
        return 0;
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;

    if ((ctx == NULL) || (b->next_bio == NULL))
        return 0;
    BIO_clear_retry_flags(b);

    for (;;) {
        i = ctx->ibuf_len;
        /* If there is something in the buffer just read it. */
        if (i != 0) {
            if (i > outl)
                i = outl;
            memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
            ctx->ibuf_off += i;
            ctx->ibuf_len -= i;
            num += i;
            /* Exit if we have read the bytes required out of the buffer */
            if (outl == i)
                return num;
            outl -= i;
            out += i;
        }

        /* Only gets here if the buffer has been consumed */
        if (!readbuffer_resize(ctx, outl))
            return 0;

        /* Do some buffering by reading from the next bio */
        i = BIO_read(b->next_bio, ctx->ibuf + ctx->ibuf_off, outl);
        if (i <= 0) {
            BIO_copy_next_retry(b);
            if (i < 0)
                return ((num > 0) ? num : i);
            else
                return num; /* i == 0 */
        }
        ctx->ibuf_len = i;
    }
}

static int readbuffer_write(BIO *b, const char *in, int inl)
{
    return 0;
}
static int readbuffer_puts(BIO *b, const char *str)
{
    return 0;
}

static long readbuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
{
    BIO_F_BUFFER_CTX *ctx;
    long ret = 1, sz;

    ctx = (BIO_F_BUFFER_CTX *)b->ptr;

    switch (cmd) {
    case BIO_CTRL_EOF:
        if (ctx->ibuf_len > 0)
            return 0;
        if (b->next_bio == NULL)
            return 1;
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        break;

    case BIO_C_FILE_SEEK:
    case BIO_CTRL_RESET:
        sz = ctx->ibuf_off + ctx->ibuf_len;
        /* Assume it can only seek backwards */
        if (num < 0 || num > sz)
            return 0;
        ctx->ibuf_off = num;
        ctx->ibuf_len = sz - num;
        break;

    case BIO_C_FILE_TELL:
    case BIO_CTRL_INFO:
        ret = (long)ctx->ibuf_off;
        break;
    case BIO_CTRL_PENDING:
        ret = (long)ctx->ibuf_len;
        if (ret == 0) {
            if (b->next_bio == NULL)
                return 0;
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
        }
        break;
    case BIO_CTRL_DUP:
    case BIO_CTRL_FLUSH:
        ret = 1;
        break;
    default:
        ret = 0;
        break;
    }
    return ret;
}

static long readbuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
{
    if (b->next_bio == NULL)
        return 0;
    return BIO_callback_ctrl(b->next_bio, cmd, fp);
}

static int readbuffer_gets(BIO *b, char *buf, int size)
{
    BIO_F_BUFFER_CTX *ctx;
    int num = 0, num_chars, found_newline;
    char *p;
    int i, j;

    if (size == 0)
        return 0;
    --size; /* the passed in size includes the terminator - so remove it here */
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
    BIO_clear_retry_flags(b);

    /* If data is already buffered then use this first */
    if (ctx->ibuf_len > 0) {
        p = ctx->ibuf + ctx->ibuf_off;
        found_newline = 0;
        for (num_chars = 0;
             (num_chars < ctx->ibuf_len) && (num_chars < size);
             num_chars++) {
            *buf++ = p[num_chars];
            if (p[num_chars] == '\n') {
                found_newline = 1;
                num_chars++;
                break;
            }
        }
        num += num_chars;
        size -= num_chars;
        ctx->ibuf_len -= num_chars;
        ctx->ibuf_off += num_chars;
        if (found_newline || size == 0) {
            *buf = '\0';
            return num;
        }
    }
    /*
     * If there is no buffered data left then read any remaining data from the
     * next bio.
     */

     /* Resize if we have to */
     if (!readbuffer_resize(ctx, 1 + size))
         return 0;
     /*
      * Read more data from the next bio using BIO_read_ex:
      * Note we cannot use BIO_gets() here as it does not work on a
      * binary stream that contains 0x00. (Since strlen() will stop at
      * any 0x00 not at the last read '\n' in a FILE bio).
      * Also note that some applications open and close the file bio
      * multiple times and need to read the next available block when using
      * stdin - so we need to READ one byte at a time!
      */
     p = ctx->ibuf + ctx->ibuf_off;
     for (i = 0; i < size; ++i) {
         j = BIO_read(b->next_bio, p, 1);
         if (j <= 0) {
             BIO_copy_next_retry(b);
             *buf = '\0';
             return num > 0 ? num : j;
         }
         *buf++ = *p;
         num++;
         ctx->ibuf_off++;
         if (*p == '\n')
             break;
         ++p;
     }
     *buf = '\0';
     return num;
}