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
94296279Sjkim
95296279SjkimBIO *BIO_new_mem_buf(const void *buf, int len)
9659191Skris{
97280297Sjkim    BIO *ret;
98280297Sjkim    BUF_MEM *b;
99280297Sjkim    size_t sz;
100238405Sjkim
101280297Sjkim    if (!buf) {
102280297Sjkim        BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
103280297Sjkim        return NULL;
104280297Sjkim    }
105280297Sjkim    sz = (len < 0) ? strlen(buf) : (size_t)len;
106280297Sjkim    if (!(ret = BIO_new(BIO_s_mem())))
107280297Sjkim        return NULL;
108280297Sjkim    b = (BUF_MEM *)ret->ptr;
109296279Sjkim    /* Cast away const and trust in the MEM_RDONLY flag. */
110296279Sjkim    b->data = (void *)buf;
111280297Sjkim    b->length = sz;
112280297Sjkim    b->max = sz;
113280297Sjkim    ret->flags |= BIO_FLAGS_MEM_RDONLY;
114280297Sjkim    /* Since this is static data retrying wont help */
115280297Sjkim    ret->num = 0;
116280297Sjkim    return ret;
11759191Skris}
11859191Skris
11955714Skrisstatic int mem_new(BIO *bi)
120280297Sjkim{
121280297Sjkim    BUF_MEM *b;
12255714Skris
123280297Sjkim    if ((b = BUF_MEM_new()) == NULL)
124280297Sjkim        return (0);
125280297Sjkim    bi->shutdown = 1;
126280297Sjkim    bi->init = 1;
127280297Sjkim    bi->num = -1;
128280297Sjkim    bi->ptr = (char *)b;
129280297Sjkim    return (1);
130280297Sjkim}
13155714Skris
13255714Skrisstatic int mem_free(BIO *a)
133280297Sjkim{
134280297Sjkim    if (a == NULL)
135280297Sjkim        return (0);
136280297Sjkim    if (a->shutdown) {
137280297Sjkim        if ((a->init) && (a->ptr != NULL)) {
138280297Sjkim            BUF_MEM *b;
139280297Sjkim            b = (BUF_MEM *)a->ptr;
140280297Sjkim            if (a->flags & BIO_FLAGS_MEM_RDONLY)
141280297Sjkim                b->data = NULL;
142280297Sjkim            BUF_MEM_free(b);
143280297Sjkim            a->ptr = NULL;
144280297Sjkim        }
145280297Sjkim    }
146280297Sjkim    return (1);
147280297Sjkim}
148280297Sjkim
14955714Skrisstatic int mem_read(BIO *b, char *out, int outl)
150280297Sjkim{
151280297Sjkim    int ret = -1;
152280297Sjkim    BUF_MEM *bm;
15355714Skris
154280297Sjkim    bm = (BUF_MEM *)b->ptr;
155280297Sjkim    BIO_clear_retry_flags(b);
156280297Sjkim    ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
157280297Sjkim    if ((out != NULL) && (ret > 0)) {
158280297Sjkim        memcpy(out, bm->data, ret);
159280297Sjkim        bm->length -= ret;
160280297Sjkim        if (b->flags & BIO_FLAGS_MEM_RDONLY)
161280297Sjkim            bm->data += ret;
162280297Sjkim        else {
163280297Sjkim            memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
164280297Sjkim        }
165280297Sjkim    } else if (bm->length == 0) {
166280297Sjkim        ret = b->num;
167280297Sjkim        if (ret != 0)
168280297Sjkim            BIO_set_retry_read(b);
169280297Sjkim    }
170280297Sjkim    return (ret);
171280297Sjkim}
17255714Skris
17368651Skrisstatic int mem_write(BIO *b, const char *in, int inl)
174280297Sjkim{
175280297Sjkim    int ret = -1;
176280297Sjkim    int blen;
177280297Sjkim    BUF_MEM *bm;
17855714Skris
179280297Sjkim    bm = (BUF_MEM *)b->ptr;
180280297Sjkim    if (in == NULL) {
181280297Sjkim        BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
182280297Sjkim        goto end;
183280297Sjkim    }
18455714Skris
185280297Sjkim    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
186280297Sjkim        BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
187280297Sjkim        goto end;
188280297Sjkim    }
18959191Skris
190280297Sjkim    BIO_clear_retry_flags(b);
191337982Sjkim    if (inl == 0)
192337982Sjkim        return 0;
193280297Sjkim    blen = bm->length;
194280297Sjkim    if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
195280297Sjkim        goto end;
196280297Sjkim    memcpy(&(bm->data[blen]), in, inl);
197280297Sjkim    ret = inl;
198280297Sjkim end:
199280297Sjkim    return (ret);
200280297Sjkim}
20155714Skris
20268651Skrisstatic long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
203280297Sjkim{
204280297Sjkim    long ret = 1;
205280297Sjkim    char **pptr;
20655714Skris
207280297Sjkim    BUF_MEM *bm = (BUF_MEM *)b->ptr;
20855714Skris
209280297Sjkim    switch (cmd) {
210280297Sjkim    case BIO_CTRL_RESET:
211280297Sjkim        if (bm->data != NULL) {
212280297Sjkim            /* For read only case reset to the start again */
213280297Sjkim            if (b->flags & BIO_FLAGS_MEM_RDONLY) {
214280297Sjkim                bm->data -= bm->max - bm->length;
215280297Sjkim                bm->length = bm->max;
216280297Sjkim            } else {
217280297Sjkim                memset(bm->data, 0, bm->max);
218280297Sjkim                bm->length = 0;
219280297Sjkim            }
220280297Sjkim        }
221280297Sjkim        break;
222280297Sjkim    case BIO_CTRL_EOF:
223280297Sjkim        ret = (long)(bm->length == 0);
224280297Sjkim        break;
225280297Sjkim    case BIO_C_SET_BUF_MEM_EOF_RETURN:
226280297Sjkim        b->num = (int)num;
227280297Sjkim        break;
228280297Sjkim    case BIO_CTRL_INFO:
229280297Sjkim        ret = (long)bm->length;
230280297Sjkim        if (ptr != NULL) {
231280297Sjkim            pptr = (char **)ptr;
232280297Sjkim            *pptr = (char *)&(bm->data[0]);
233280297Sjkim        }
234280297Sjkim        break;
235280297Sjkim    case BIO_C_SET_BUF_MEM:
236280297Sjkim        mem_free(b);
237280297Sjkim        b->shutdown = (int)num;
238280297Sjkim        b->ptr = ptr;
239280297Sjkim        break;
240280297Sjkim    case BIO_C_GET_BUF_MEM_PTR:
241280297Sjkim        if (ptr != NULL) {
242280297Sjkim            pptr = (char **)ptr;
243280297Sjkim            *pptr = (char *)bm;
244280297Sjkim        }
245280297Sjkim        break;
246280297Sjkim    case BIO_CTRL_GET_CLOSE:
247280297Sjkim        ret = (long)b->shutdown;
248280297Sjkim        break;
249280297Sjkim    case BIO_CTRL_SET_CLOSE:
250280297Sjkim        b->shutdown = (int)num;
251280297Sjkim        break;
25255714Skris
253280297Sjkim    case BIO_CTRL_WPENDING:
254280297Sjkim        ret = 0L;
255280297Sjkim        break;
256280297Sjkim    case BIO_CTRL_PENDING:
257280297Sjkim        ret = (long)bm->length;
258280297Sjkim        break;
259280297Sjkim    case BIO_CTRL_DUP:
260280297Sjkim    case BIO_CTRL_FLUSH:
261280297Sjkim        ret = 1;
262280297Sjkim        break;
263280297Sjkim    case BIO_CTRL_PUSH:
264280297Sjkim    case BIO_CTRL_POP:
265280297Sjkim    default:
266280297Sjkim        ret = 0;
267280297Sjkim        break;
268280297Sjkim    }
269280297Sjkim    return (ret);
270280297Sjkim}
27155714Skris
27255714Skrisstatic int mem_gets(BIO *bp, char *buf, int size)
273280297Sjkim{
274280297Sjkim    int i, j;
275280297Sjkim    int ret = -1;
276280297Sjkim    char *p;
277280297Sjkim    BUF_MEM *bm = (BUF_MEM *)bp->ptr;
27855714Skris
279280297Sjkim    BIO_clear_retry_flags(bp);
280280297Sjkim    j = bm->length;
281280297Sjkim    if ((size - 1) < j)
282280297Sjkim        j = size - 1;
283280297Sjkim    if (j <= 0) {
284280297Sjkim        *buf = '\0';
285280297Sjkim        return 0;
286280297Sjkim    }
287280297Sjkim    p = bm->data;
288280297Sjkim    for (i = 0; i < j; i++) {
289280297Sjkim        if (p[i] == '\n') {
290280297Sjkim            i++;
291280297Sjkim            break;
292280297Sjkim        }
293280297Sjkim    }
294194206Ssimon
295280297Sjkim    /*
296280297Sjkim     * i is now the max num of bytes to copy, either j or up to
297280297Sjkim     * and including the first newline
298280297Sjkim     */
299194206Ssimon
300280297Sjkim    i = mem_read(bp, buf, i);
301280297Sjkim    if (i > 0)
302280297Sjkim        buf[i] = '\0';
303280297Sjkim    ret = i;
304280297Sjkim    return (ret);
305280297Sjkim}
30655714Skris
30768651Skrisstatic int mem_puts(BIO *bp, const char *str)
308280297Sjkim{
309280297Sjkim    int n, ret;
31055714Skris
311280297Sjkim    n = strlen(str);
312280297Sjkim    ret = mem_write(bp, str, n);
313280297Sjkim    /* memory semantics is that it will always work */
314280297Sjkim    return (ret);
315280297Sjkim}
316