bss_mem.c revision 59191
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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
6455714Skrisstatic int mem_write(BIO *h,char *buf,int num);
6555714Skrisstatic int mem_read(BIO *h,char *buf,int size);
6655714Skrisstatic int mem_puts(BIO *h,char *str);
6755714Skrisstatic int mem_gets(BIO *h,char *str,int size);
6855714Skrisstatic long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2);
6955714Skrisstatic int mem_new(BIO *h);
7055714Skrisstatic int mem_free(BIO *data);
7155714Skrisstatic BIO_METHOD mem_method=
7255714Skris	{
7355714Skris	BIO_TYPE_MEM,
7455714Skris	"memory buffer",
7555714Skris	mem_write,
7655714Skris	mem_read,
7755714Skris	mem_puts,
7855714Skris	mem_gets,
7955714Skris	mem_ctrl,
8055714Skris	mem_new,
8155714Skris	mem_free,
8259191Skris	NULL,
8355714Skris	};
8455714Skris
8555714Skris/* bio->num is used to hold the value to return on 'empty', if it is
8655714Skris * 0, should_retry is not set */
8755714Skris
8855714SkrisBIO_METHOD *BIO_s_mem(void)
8955714Skris	{
9055714Skris	return(&mem_method);
9155714Skris	}
9255714Skris
9359191SkrisBIO *BIO_new_mem_buf(void *buf, int len)
9459191Skris{
9559191Skris	BIO *ret;
9659191Skris	BUF_MEM *b;
9759191Skris	if (!buf) {
9859191Skris		BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER);
9959191Skris		return NULL;
10059191Skris	}
10159191Skris	if(len == -1) len = strlen(buf);
10259191Skris	if(!(ret = BIO_new(BIO_s_mem())) ) return NULL;
10359191Skris	b = (BUF_MEM *)ret->ptr;
10459191Skris	b->data = buf;
10559191Skris	b->length = len;
10659191Skris	b->max = len;
10759191Skris	ret->flags |= BIO_FLAGS_MEM_RDONLY;
10859191Skris	/* Since this is static data retrying wont help */
10959191Skris	ret->num = 0;
11059191Skris	return ret;
11159191Skris}
11259191Skris
11355714Skrisstatic int mem_new(BIO *bi)
11455714Skris	{
11555714Skris	BUF_MEM *b;
11655714Skris
11755714Skris	if ((b=BUF_MEM_new()) == NULL)
11855714Skris		return(0);
11955714Skris	bi->shutdown=1;
12055714Skris	bi->init=1;
12155714Skris	bi->num= -1;
12255714Skris	bi->ptr=(char *)b;
12355714Skris	return(1);
12455714Skris	}
12555714Skris
12655714Skrisstatic int mem_free(BIO *a)
12755714Skris	{
12855714Skris	if (a == NULL) return(0);
12955714Skris	if (a->shutdown)
13055714Skris		{
13155714Skris		if ((a->init) && (a->ptr != NULL))
13255714Skris			{
13359191Skris			BUF_MEM *b;
13459191Skris			b = (BUF_MEM *)a->ptr;
13559191Skris			if(a->flags & BIO_FLAGS_MEM_RDONLY) b->data = NULL;
13659191Skris			BUF_MEM_free(b);
13755714Skris			a->ptr=NULL;
13855714Skris			}
13955714Skris		}
14055714Skris	return(1);
14155714Skris	}
14255714Skris
14355714Skrisstatic int mem_read(BIO *b, char *out, int outl)
14455714Skris	{
14555714Skris	int ret= -1;
14655714Skris	BUF_MEM *bm;
14755714Skris	int i;
14855714Skris	char *from,*to;
14955714Skris
15055714Skris	bm=(BUF_MEM *)b->ptr;
15155714Skris	BIO_clear_retry_flags(b);
15255714Skris	ret=(outl > bm->length)?bm->length:outl;
15359191Skris	if ((out != NULL) && (ret > 0)) {
15455714Skris		memcpy(out,bm->data,ret);
15555714Skris		bm->length-=ret;
15655714Skris		/* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */
15759191Skris		if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret;
15859191Skris		else {
15959191Skris			from=(char *)&(bm->data[ret]);
16059191Skris			to=(char *)&(bm->data[0]);
16159191Skris			for (i=0; i<bm->length; i++)
16259191Skris				to[i]=from[i];
16355714Skris		}
16459191Skris	} else if (bm->length == 0)
16555714Skris		{
16655714Skris		if (b->num != 0)
16755714Skris			BIO_set_retry_read(b);
16855714Skris		ret= b->num;
16955714Skris		}
17055714Skris	return(ret);
17155714Skris	}
17255714Skris
17355714Skrisstatic int mem_write(BIO *b, char *in, int inl)
17455714Skris	{
17555714Skris	int ret= -1;
17655714Skris	int blen;
17755714Skris	BUF_MEM *bm;
17855714Skris
17955714Skris	bm=(BUF_MEM *)b->ptr;
18055714Skris	if (in == NULL)
18155714Skris		{
18255714Skris		BIOerr(BIO_F_MEM_WRITE,BIO_R_NULL_PARAMETER);
18355714Skris		goto end;
18455714Skris		}
18555714Skris
18659191Skris	if(b->flags & BIO_FLAGS_MEM_RDONLY) {
18759191Skris		BIOerr(BIO_F_MEM_WRITE,BIO_R_WRITE_TO_READ_ONLY_BIO);
18859191Skris		goto end;
18959191Skris	}
19059191Skris
19155714Skris	BIO_clear_retry_flags(b);
19255714Skris	blen=bm->length;
19355714Skris	if (BUF_MEM_grow(bm,blen+inl) != (blen+inl))
19455714Skris		goto end;
19555714Skris	memcpy(&(bm->data[blen]),in,inl);
19655714Skris	ret=inl;
19755714Skrisend:
19855714Skris	return(ret);
19955714Skris	}
20055714Skris
20155714Skrisstatic long mem_ctrl(BIO *b, int cmd, long num, char *ptr)
20255714Skris	{
20355714Skris	long ret=1;
20455714Skris	char **pptr;
20555714Skris
20655714Skris	BUF_MEM *bm=(BUF_MEM *)b->ptr;
20755714Skris
20855714Skris	switch (cmd)
20955714Skris		{
21055714Skris	case BIO_CTRL_RESET:
21159191Skris		if (bm->data != NULL) {
21259191Skris			/* For read only case reset to the start again */
21359191Skris			if(b->flags & BIO_FLAGS_MEM_RDONLY)
21459191Skris					bm->data -= bm->max - bm->length;
21559191Skris			else {
21659191Skris				memset(bm->data,0,bm->max);
21759191Skris				bm->length=0;
21859191Skris			}
21959191Skris		}
22055714Skris		break;
22155714Skris	case BIO_CTRL_EOF:
22255714Skris		ret=(long)(bm->length == 0);
22355714Skris		break;
22455714Skris	case BIO_C_SET_BUF_MEM_EOF_RETURN:
22555714Skris		b->num=(int)num;
22655714Skris		break;
22755714Skris	case BIO_CTRL_INFO:
22855714Skris		ret=(long)bm->length;
22955714Skris		if (ptr != NULL)
23055714Skris			{
23155714Skris			pptr=(char **)ptr;
23255714Skris			*pptr=(char *)&(bm->data[0]);
23355714Skris			}
23455714Skris		break;
23555714Skris	case BIO_C_SET_BUF_MEM:
23655714Skris		mem_free(b);
23755714Skris		b->shutdown=(int)num;
23855714Skris		b->ptr=ptr;
23955714Skris		break;
24055714Skris	case BIO_C_GET_BUF_MEM_PTR:
24155714Skris		if (ptr != NULL)
24255714Skris			{
24355714Skris			pptr=(char **)ptr;
24455714Skris			*pptr=(char *)bm;
24555714Skris			}
24655714Skris		break;
24755714Skris	case BIO_CTRL_GET_CLOSE:
24855714Skris		ret=(long)b->shutdown;
24955714Skris		break;
25055714Skris	case BIO_CTRL_SET_CLOSE:
25155714Skris		b->shutdown=(int)num;
25255714Skris		break;
25355714Skris
25455714Skris	case BIO_CTRL_WPENDING:
25555714Skris		ret=0L;
25655714Skris		break;
25755714Skris	case BIO_CTRL_PENDING:
25855714Skris		ret=(long)bm->length;
25955714Skris		break;
26055714Skris	case BIO_CTRL_DUP:
26155714Skris	case BIO_CTRL_FLUSH:
26255714Skris		ret=1;
26355714Skris		break;
26455714Skris	case BIO_CTRL_PUSH:
26555714Skris	case BIO_CTRL_POP:
26655714Skris	default:
26755714Skris		ret=0;
26855714Skris		break;
26955714Skris		}
27055714Skris	return(ret);
27155714Skris	}
27255714Skris
27355714Skrisstatic int mem_gets(BIO *bp, char *buf, int size)
27455714Skris	{
27555714Skris	int i,j;
27655714Skris	int ret= -1;
27755714Skris	char *p;
27855714Skris	BUF_MEM *bm=(BUF_MEM *)bp->ptr;
27955714Skris
28055714Skris	BIO_clear_retry_flags(bp);
28155714Skris	j=bm->length;
28255714Skris	if (j <= 0) return(0);
28355714Skris	p=bm->data;
28455714Skris	for (i=0; i<j; i++)
28555714Skris		{
28655714Skris		if (p[i] == '\n') break;
28755714Skris		}
28855714Skris	if (i == j)
28955714Skris		{
29055714Skris		BIO_set_retry_read(bp);
29155714Skris		/* return(-1);  change the semantics 0.6.6a */
29255714Skris		}
29355714Skris	else
29455714Skris		i++;
29555714Skris	/* i is the max to copy */
29655714Skris	if ((size-1) < i) i=size-1;
29755714Skris	i=mem_read(bp,buf,i);
29855714Skris	if (i > 0) buf[i]='\0';
29955714Skris	ret=i;
30055714Skris	return(ret);
30155714Skris	}
30255714Skris
30355714Skrisstatic int mem_puts(BIO *bp, char *str)
30455714Skris	{
30555714Skris	int n,ret;
30655714Skris
30755714Skris	n=strlen(str);
30855714Skris	ret=mem_write(bp,str,n);
30955714Skris	/* memory semantics is that it will always work */
31055714Skris	return(ret);
31155714Skris	}
31255714Skris
313