bf_buff.c revision 55714
1/* crypto/bio/bf_buff.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@cryptsoft.com)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <errno.h>
61#include "cryptlib.h"
62#include <openssl/bio.h>
63#include <openssl/evp.h>
64
65static int buffer_write(BIO *h,char *buf,int num);
66static int buffer_read(BIO *h,char *buf,int size);
67static int buffer_puts(BIO *h,char *str);
68static int buffer_gets(BIO *h,char *str,int size);
69static long buffer_ctrl(BIO *h,int cmd,long arg1,char *arg2);
70static int buffer_new(BIO *h);
71static int buffer_free(BIO *data);
72#define DEFAULT_BUFFER_SIZE	1024
73
74static BIO_METHOD methods_buffer=
75	{
76	BIO_TYPE_BUFFER,
77	"buffer",
78	buffer_write,
79	buffer_read,
80	buffer_puts,
81	buffer_gets,
82	buffer_ctrl,
83	buffer_new,
84	buffer_free,
85	};
86
87BIO_METHOD *BIO_f_buffer(void)
88	{
89	return(&methods_buffer);
90	}
91
92static int buffer_new(BIO *bi)
93	{
94	BIO_F_BUFFER_CTX *ctx;
95
96	ctx=(BIO_F_BUFFER_CTX *)Malloc(sizeof(BIO_F_BUFFER_CTX));
97	if (ctx == NULL) return(0);
98	ctx->ibuf=(char *)Malloc(DEFAULT_BUFFER_SIZE);
99	if (ctx->ibuf == NULL) { Free(ctx); return(0); }
100	ctx->obuf=(char *)Malloc(DEFAULT_BUFFER_SIZE);
101	if (ctx->obuf == NULL) { Free(ctx->ibuf); Free(ctx); return(0); }
102	ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
103	ctx->obuf_size=DEFAULT_BUFFER_SIZE;
104	ctx->ibuf_len=0;
105	ctx->ibuf_off=0;
106	ctx->obuf_len=0;
107	ctx->obuf_off=0;
108
109	bi->init=1;
110	bi->ptr=(char *)ctx;
111	bi->flags=0;
112	return(1);
113	}
114
115static int buffer_free(BIO *a)
116	{
117	BIO_F_BUFFER_CTX *b;
118
119	if (a == NULL) return(0);
120	b=(BIO_F_BUFFER_CTX *)a->ptr;
121	if (b->ibuf != NULL) Free(b->ibuf);
122	if (b->obuf != NULL) Free(b->obuf);
123	Free(a->ptr);
124	a->ptr=NULL;
125	a->init=0;
126	a->flags=0;
127	return(1);
128	}
129
130static int buffer_read(BIO *b, char *out, int outl)
131	{
132	int i,num=0;
133	BIO_F_BUFFER_CTX *ctx;
134
135	if (out == NULL) return(0);
136	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
137
138	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
139	num=0;
140	BIO_clear_retry_flags(b);
141
142start:
143	i=ctx->ibuf_len;
144	/* If there is stuff left over, grab it */
145	if (i != 0)
146		{
147		if (i > outl) i=outl;
148		memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i);
149		ctx->ibuf_off+=i;
150		ctx->ibuf_len-=i;
151		num+=i;
152		if (outl == i)  return(num);
153		outl-=i;
154		out+=i;
155		}
156
157	/* We may have done a partial read. try to do more.
158	 * We have nothing in the buffer.
159	 * If we get an error and have read some data, just return it
160	 * and let them retry to get the error again.
161	 * copy direct to parent address space */
162	if (outl > ctx->ibuf_size)
163		{
164		for (;;)
165			{
166			i=BIO_read(b->next_bio,out,outl);
167			if (i <= 0)
168				{
169				BIO_copy_next_retry(b);
170				if (i < 0) return((num > 0)?num:i);
171				if (i == 0) return(num);
172				}
173			num+=i;
174			if (outl == i) return(num);
175			out+=i;
176			outl-=i;
177			}
178		}
179	/* else */
180
181	/* we are going to be doing some buffering */
182	i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
183	if (i <= 0)
184		{
185		BIO_copy_next_retry(b);
186		if (i < 0) return((num > 0)?num:i);
187		if (i == 0) return(num);
188		}
189	ctx->ibuf_off=0;
190	ctx->ibuf_len=i;
191
192	/* Lets re-read using ourselves :-) */
193	goto start;
194	}
195
196static int buffer_write(BIO *b, char *in, int inl)
197	{
198	int i,num=0;
199	BIO_F_BUFFER_CTX *ctx;
200
201	if ((in == NULL) || (inl <= 0)) return(0);
202	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
203	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
204
205	BIO_clear_retry_flags(b);
206start:
207	i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off);
208	/* add to buffer and return */
209	if (i >= inl)
210		{
211		memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl);
212		ctx->obuf_len+=inl;
213		return(num+inl);
214		}
215	/* else */
216	/* stuff already in buffer, so add to it first, then flush */
217	if (ctx->obuf_len != 0)
218		{
219		if (i > 0) /* lets fill it up if we can */
220			{
221			memcpy(&(ctx->obuf[ctx->obuf_len]),in,i);
222			in+=i;
223			inl-=i;
224			num+=i;
225			ctx->obuf_len+=i;
226			}
227		/* we now have a full buffer needing flushing */
228		for (;;)
229			{
230			i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]),
231				ctx->obuf_len);
232			if (i <= 0)
233				{
234				BIO_copy_next_retry(b);
235
236				if (i < 0) return((num > 0)?num:i);
237				if (i == 0) return(num);
238				}
239			ctx->obuf_off+=i;
240			ctx->obuf_len-=i;
241			if (ctx->obuf_len == 0) break;
242			}
243		}
244	/* we only get here if the buffer has been flushed and we
245	 * still have stuff to write */
246	ctx->obuf_off=0;
247
248	/* we now have inl bytes to write */
249	while (inl >= ctx->obuf_size)
250		{
251		i=BIO_write(b->next_bio,in,inl);
252		if (i <= 0)
253			{
254			BIO_copy_next_retry(b);
255			if (i < 0) return((num > 0)?num:i);
256			if (i == 0) return(num);
257			}
258		num+=i;
259		in+=i;
260		inl-=i;
261		if (inl == 0) return(num);
262		}
263
264	/* copy the rest into the buffer since we have only a small
265	 * amount left */
266	goto start;
267	}
268
269static long buffer_ctrl(BIO *b, int cmd, long num, char *ptr)
270	{
271	BIO *dbio;
272	BIO_F_BUFFER_CTX *ctx;
273	long ret=1;
274	char *p1,*p2;
275	int r,i,*ip;
276	int ibs,obs;
277
278	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
279
280	switch (cmd)
281		{
282	case BIO_CTRL_RESET:
283		ctx->ibuf_off=0;
284		ctx->ibuf_len=0;
285		ctx->obuf_off=0;
286		ctx->obuf_len=0;
287		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
288		break;
289	case BIO_CTRL_INFO:
290		ret=(long)ctx->obuf_len;
291		break;
292	case BIO_C_GET_BUFF_NUM_LINES:
293		ret=0;
294		p1=ctx->ibuf;
295		for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++)
296			{
297			if (p1[i] == '\n') ret++;
298			}
299		break;
300	case BIO_CTRL_WPENDING:
301		ret=(long)ctx->obuf_len;
302		if (ret == 0)
303			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
304		break;
305	case BIO_CTRL_PENDING:
306		ret=(long)ctx->ibuf_len;
307		if (ret == 0)
308			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
309		break;
310	case BIO_C_SET_BUFF_READ_DATA:
311		if (num > ctx->ibuf_size)
312			{
313			p1=Malloc((int)num);
314			if (p1 == NULL) goto malloc_error;
315			if (ctx->ibuf != NULL) Free(ctx->ibuf);
316			ctx->ibuf=p1;
317			}
318		ctx->ibuf_off=0;
319		ctx->ibuf_len=(int)num;
320		memcpy(ctx->ibuf,ptr,(int)num);
321		ret=1;
322		break;
323	case BIO_C_SET_BUFF_SIZE:
324		if (ptr != NULL)
325			{
326			ip=(int *)ptr;
327			if (*ip == 0)
328				{
329				ibs=(int)num;
330				obs=ctx->obuf_size;
331				}
332			else /* if (*ip == 1) */
333				{
334				ibs=ctx->ibuf_size;
335				obs=(int)num;
336				}
337			}
338		else
339			{
340			ibs=(int)num;
341			obs=(int)num;
342			}
343		p1=ctx->ibuf;
344		p2=ctx->obuf;
345		if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
346			{
347			p1=(char *)Malloc((int)num);
348			if (p1 == NULL) goto malloc_error;
349			}
350		if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
351			{
352			p2=(char *)Malloc((int)num);
353			if (p2 == NULL)
354				{
355				if (p1 != ctx->ibuf) Free(p1);
356				goto malloc_error;
357				}
358			}
359		if (ctx->ibuf != p1)
360			{
361			Free(ctx->ibuf);
362			ctx->ibuf=p1;
363			ctx->ibuf_off=0;
364			ctx->ibuf_len=0;
365			ctx->ibuf_size=ibs;
366			}
367		if (ctx->obuf != p2)
368			{
369			Free(ctx->obuf);
370			ctx->obuf=p2;
371			ctx->obuf_off=0;
372			ctx->obuf_len=0;
373			ctx->obuf_size=obs;
374			}
375		break;
376	case BIO_C_DO_STATE_MACHINE:
377		BIO_clear_retry_flags(b);
378		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
379		BIO_copy_next_retry(b);
380		break;
381
382	case BIO_CTRL_FLUSH:
383		if (ctx->obuf_len <= 0)
384			{
385			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
386			break;
387			}
388
389		for (;;)
390			{
391			BIO_clear_retry_flags(b);
392			if (ctx->obuf_len > ctx->obuf_off)
393				{
394				r=BIO_write(b->next_bio,
395					&(ctx->obuf[ctx->obuf_off]),
396					ctx->obuf_len-ctx->obuf_off);
397#if 0
398fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r);
399#endif
400				BIO_copy_next_retry(b);
401				if (r <= 0) return((long)r);
402				ctx->obuf_off+=r;
403				}
404			else
405				{
406				ctx->obuf_len=0;
407				ctx->obuf_off=0;
408				ret=1;
409				break;
410				}
411			}
412		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
413		break;
414	case BIO_CTRL_DUP:
415		dbio=(BIO *)ptr;
416		if (	!BIO_set_read_buffer_size(dbio,ctx->ibuf_size) ||
417			!BIO_set_write_buffer_size(dbio,ctx->obuf_size))
418			ret=0;
419		break;
420	default:
421		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
422		break;
423		}
424	return(ret);
425malloc_error:
426	BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
427	return(0);
428	}
429
430static int buffer_gets(BIO *b, char *buf, int size)
431	{
432	BIO_F_BUFFER_CTX *ctx;
433	int num=0,i,flag;
434	char *p;
435
436	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
437	size--; /* reserve space for a '\0' */
438	BIO_clear_retry_flags(b);
439
440	for (;;)
441		{
442		if (ctx->ibuf_len > 0)
443			{
444			p= &(ctx->ibuf[ctx->ibuf_off]);
445			flag=0;
446			for (i=0; (i<ctx->ibuf_len) && (i<size); i++)
447				{
448				*(buf++)=p[i];
449				if (p[i] == '\n')
450					{
451					flag=1;
452					i++;
453					break;
454					}
455				}
456			num+=i;
457			size-=i;
458			ctx->ibuf_len-=i;
459			ctx->ibuf_off+=i;
460			if ((flag) || (i == size))
461				{
462				*buf='\0';
463				return(num);
464				}
465			}
466		else	/* read another chunk */
467			{
468			i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
469			if (i <= 0)
470				{
471				BIO_copy_next_retry(b);
472				if (i < 0) return((num > 0)?num:i);
473				if (i == 0) return(num);
474				}
475			ctx->ibuf_len=i;
476			ctx->ibuf_off=0;
477			}
478		}
479	}
480
481static int buffer_puts(BIO *b, char *str)
482	{
483	return(BIO_write(b,str,strlen(str)));
484	}
485
486