bss_mem.c revision 280297
155714Skris/* crypto/bio/bss_mem.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280297Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280297Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280297Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280297Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280297Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <errno.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/bio.h>
6355714Skris
6468651Skrisstatic int mem_write(BIO *h, const char *buf, int num);
6568651Skrisstatic int mem_read(BIO *h, char *buf, int size);
6668651Skrisstatic int mem_puts(BIO *h, const char *str);
6768651Skrisstatic int mem_gets(BIO *h, char *str, int size);
6868651Skrisstatic long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
6955714Skrisstatic int mem_new(BIO *h);
7055714Skrisstatic int mem_free(BIO *data);
71280297Sjkimstatic BIO_METHOD mem_method = {
72280297Sjkim    BIO_TYPE_MEM,
73280297Sjkim    "memory buffer",
74280297Sjkim    mem_write,
75280297Sjkim    mem_read,
76280297Sjkim    mem_puts,
77280297Sjkim    mem_gets,
78280297Sjkim    mem_ctrl,
79280297Sjkim    mem_new,
80280297Sjkim    mem_free,
81280297Sjkim    NULL,
82280297Sjkim};
8355714Skris
84280297Sjkim/*
85280297Sjkim * bio->num is used to hold the value to return on 'empty', if it is 0,
86280297Sjkim * should_retry is not set
87280297Sjkim */
8855714Skris
8955714SkrisBIO_METHOD *BIO_s_mem(void)
90280297Sjkim{
91280297Sjkim    return (&mem_method);
92280297Sjkim}
9355714Skris
9459191SkrisBIO *BIO_new_mem_buf(void *buf, int len)
9559191Skris{
96280297Sjkim    BIO *ret;
97280297Sjkim    BUF_MEM *b;
98280297Sjkim    size_t sz;
99238405Sjkim
100280297Sjkim    if (!buf) {
101280297Sjkim        BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
102280297Sjkim        return NULL;
103280297Sjkim    }
104280297Sjkim    sz = (len < 0) ? strlen(buf) : (size_t)len;
105280297Sjkim    if (!(ret = BIO_new(BIO_s_mem())))
106280297Sjkim        return NULL;
107280297Sjkim    b = (BUF_MEM *)ret->ptr;
108280297Sjkim    b->data = buf;
109280297Sjkim    b->length = sz;
110280297Sjkim    b->max = sz;
111280297Sjkim    ret->flags |= BIO_FLAGS_MEM_RDONLY;
112280297Sjkim    /* Since this is static data retrying wont help */
113280297Sjkim    ret->num = 0;
114280297Sjkim    return ret;
11559191Skris}
11659191Skris
11755714Skrisstatic int mem_new(BIO *bi)
118280297Sjkim{
119280297Sjkim    BUF_MEM *b;
12055714Skris
121280297Sjkim    if ((b = BUF_MEM_new()) == NULL)
122280297Sjkim        return (0);
123280297Sjkim    bi->shutdown = 1;
124280297Sjkim    bi->init = 1;
125280297Sjkim    bi->num = -1;
126280297Sjkim    bi->ptr = (char *)b;
127280297Sjkim    return (1);
128280297Sjkim}
12955714Skris
13055714Skrisstatic int mem_free(BIO *a)
131280297Sjkim{
132280297Sjkim    if (a == NULL)
133280297Sjkim        return (0);
134280297Sjkim    if (a->shutdown) {
135280297Sjkim        if ((a->init) && (a->ptr != NULL)) {
136280297Sjkim            BUF_MEM *b;
137280297Sjkim            b = (BUF_MEM *)a->ptr;
138280297Sjkim            if (a->flags & BIO_FLAGS_MEM_RDONLY)
139280297Sjkim                b->data = NULL;
140280297Sjkim            BUF_MEM_free(b);
141280297Sjkim            a->ptr = NULL;
142280297Sjkim        }
143280297Sjkim    }
144280297Sjkim    return (1);
145280297Sjkim}
146280297Sjkim
14755714Skrisstatic int mem_read(BIO *b, char *out, int outl)
148280297Sjkim{
149280297Sjkim    int ret = -1;
150280297Sjkim    BUF_MEM *bm;
15155714Skris
152280297Sjkim    bm = (BUF_MEM *)b->ptr;
153280297Sjkim    BIO_clear_retry_flags(b);
154280297Sjkim    ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
155280297Sjkim    if ((out != NULL) && (ret > 0)) {
156280297Sjkim        memcpy(out, bm->data, ret);
157280297Sjkim        bm->length -= ret;
158280297Sjkim        if (b->flags & BIO_FLAGS_MEM_RDONLY)
159280297Sjkim            bm->data += ret;
160280297Sjkim        else {
161280297Sjkim            memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
162280297Sjkim        }
163280297Sjkim    } else if (bm->length == 0) {
164280297Sjkim        ret = b->num;
165280297Sjkim        if (ret != 0)
166280297Sjkim            BIO_set_retry_read(b);
167280297Sjkim    }
168280297Sjkim    return (ret);
169280297Sjkim}
17055714Skris
17168651Skrisstatic int mem_write(BIO *b, const char *in, int inl)
172280297Sjkim{
173280297Sjkim    int ret = -1;
174280297Sjkim    int blen;
175280297Sjkim    BUF_MEM *bm;
17655714Skris
177280297Sjkim    bm = (BUF_MEM *)b->ptr;
178280297Sjkim    if (in == NULL) {
179280297Sjkim        BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
180280297Sjkim        goto end;
181280297Sjkim    }
18255714Skris
183280297Sjkim    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
184280297Sjkim        BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
185280297Sjkim        goto end;
186280297Sjkim    }
18759191Skris
188280297Sjkim    BIO_clear_retry_flags(b);
189280297Sjkim    blen = bm->length;
190280297Sjkim    if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
191280297Sjkim        goto end;
192280297Sjkim    memcpy(&(bm->data[blen]), in, inl);
193280297Sjkim    ret = inl;
194280297Sjkim end:
195280297Sjkim    return (ret);
196280297Sjkim}
19755714Skris
19868651Skrisstatic long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
199280297Sjkim{
200280297Sjkim    long ret = 1;
201280297Sjkim    char **pptr;
20255714Skris
203280297Sjkim    BUF_MEM *bm = (BUF_MEM *)b->ptr;
20455714Skris
205280297Sjkim    switch (cmd) {
206280297Sjkim    case BIO_CTRL_RESET:
207280297Sjkim        if (bm->data != NULL) {
208280297Sjkim            /* For read only case reset to the start again */
209280297Sjkim            if (b->flags & BIO_FLAGS_MEM_RDONLY) {
210280297Sjkim                bm->data -= bm->max - bm->length;
211280297Sjkim                bm->length = bm->max;
212280297Sjkim            } else {
213280297Sjkim                memset(bm->data, 0, bm->max);
214280297Sjkim                bm->length = 0;
215280297Sjkim            }
216280297Sjkim        }
217280297Sjkim        break;
218280297Sjkim    case BIO_CTRL_EOF:
219280297Sjkim        ret = (long)(bm->length == 0);
220280297Sjkim        break;
221280297Sjkim    case BIO_C_SET_BUF_MEM_EOF_RETURN:
222280297Sjkim        b->num = (int)num;
223280297Sjkim        break;
224280297Sjkim    case BIO_CTRL_INFO:
225280297Sjkim        ret = (long)bm->length;
226280297Sjkim        if (ptr != NULL) {
227280297Sjkim            pptr = (char **)ptr;
228280297Sjkim            *pptr = (char *)&(bm->data[0]);
229280297Sjkim        }
230280297Sjkim        break;
231280297Sjkim    case BIO_C_SET_BUF_MEM:
232280297Sjkim        mem_free(b);
233280297Sjkim        b->shutdown = (int)num;
234280297Sjkim        b->ptr = ptr;
235280297Sjkim        break;
236280297Sjkim    case BIO_C_GET_BUF_MEM_PTR:
237280297Sjkim        if (ptr != NULL) {
238280297Sjkim            pptr = (char **)ptr;
239280297Sjkim            *pptr = (char *)bm;
240280297Sjkim        }
241280297Sjkim        break;
242280297Sjkim    case BIO_CTRL_GET_CLOSE:
243280297Sjkim        ret = (long)b->shutdown;
244280297Sjkim        break;
245280297Sjkim    case BIO_CTRL_SET_CLOSE:
246280297Sjkim        b->shutdown = (int)num;
247280297Sjkim        break;
24855714Skris
249280297Sjkim    case BIO_CTRL_WPENDING:
250280297Sjkim        ret = 0L;
251280297Sjkim        break;
252280297Sjkim    case BIO_CTRL_PENDING:
253280297Sjkim        ret = (long)bm->length;
254280297Sjkim        break;
255280297Sjkim    case BIO_CTRL_DUP:
256280297Sjkim    case BIO_CTRL_FLUSH:
257280297Sjkim        ret = 1;
258280297Sjkim        break;
259280297Sjkim    case BIO_CTRL_PUSH:
260280297Sjkim    case BIO_CTRL_POP:
261280297Sjkim    default:
262280297Sjkim        ret = 0;
263280297Sjkim        break;
264280297Sjkim    }
265280297Sjkim    return (ret);
266280297Sjkim}
26755714Skris
26855714Skrisstatic int mem_gets(BIO *bp, char *buf, int size)
269280297Sjkim{
270280297Sjkim    int i, j;
271280297Sjkim    int ret = -1;
272280297Sjkim    char *p;
273280297Sjkim    BUF_MEM *bm = (BUF_MEM *)bp->ptr;
27455714Skris
275280297Sjkim    BIO_clear_retry_flags(bp);
276280297Sjkim    j = bm->length;
277280297Sjkim    if ((size - 1) < j)
278280297Sjkim        j = size - 1;
279280297Sjkim    if (j <= 0) {
280280297Sjkim        *buf = '\0';
281280297Sjkim        return 0;
282280297Sjkim    }
283280297Sjkim    p = bm->data;
284280297Sjkim    for (i = 0; i < j; i++) {
285280297Sjkim        if (p[i] == '\n') {
286280297Sjkim            i++;
287280297Sjkim            break;
288280297Sjkim        }
289280297Sjkim    }
290194206Ssimon
291280297Sjkim    /*
292280297Sjkim     * i is now the max num of bytes to copy, either j or up to
293280297Sjkim     * and including the first newline
294280297Sjkim     */
295194206Ssimon
296280297Sjkim    i = mem_read(bp, buf, i);
297280297Sjkim    if (i > 0)
298280297Sjkim        buf[i] = '\0';
299280297Sjkim    ret = i;
300280297Sjkim    return (ret);
301280297Sjkim}
30255714Skris
30368651Skrisstatic int mem_puts(BIO *bp, const char *str)
304280297Sjkim{
305280297Sjkim    int n, ret;
30655714Skris
307280297Sjkim    n = strlen(str);
308280297Sjkim    ret = mem_write(bp, str, n);
309280297Sjkim    /* memory semantics is that it will always work */
310280297Sjkim    return (ret);
311280297Sjkim}
312