os/ossrv/ssl/libcrypto/src/crypto/bio/bf_buff.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/bio/bf_buff.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,511 @@
     1.4 +/* crypto/bio/bf_buff.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +#include <stdio.h>
    1.63 +#include <errno.h>
    1.64 +#include "cryptlib.h"
    1.65 +#include <openssl/bio.h>
    1.66 +
    1.67 +static int buffer_write(BIO *h, const char *buf,int num);
    1.68 +static int buffer_read(BIO *h, char *buf, int size);
    1.69 +static int buffer_puts(BIO *h, const char *str);
    1.70 +static int buffer_gets(BIO *h, char *str, int size);
    1.71 +static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
    1.72 +static int buffer_new(BIO *h);
    1.73 +static int buffer_free(BIO *data);
    1.74 +static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
    1.75 +#define DEFAULT_BUFFER_SIZE	4096
    1.76 +
    1.77 +static BIO_METHOD methods_buffer=
    1.78 +	{
    1.79 +	BIO_TYPE_BUFFER,
    1.80 +	"buffer",
    1.81 +	buffer_write,
    1.82 +	buffer_read,
    1.83 +	buffer_puts,
    1.84 +	buffer_gets,
    1.85 +	buffer_ctrl,
    1.86 +	buffer_new,
    1.87 +	buffer_free,
    1.88 +	buffer_callback_ctrl,
    1.89 +	};
    1.90 +
    1.91 +EXPORT_C BIO_METHOD *BIO_f_buffer(void)
    1.92 +	{
    1.93 +	return(&methods_buffer);
    1.94 +	}
    1.95 +
    1.96 +static int buffer_new(BIO *bi)
    1.97 +	{
    1.98 +	BIO_F_BUFFER_CTX *ctx;
    1.99 +
   1.100 +	ctx=(BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
   1.101 +	if (ctx == NULL) return(0);
   1.102 +	ctx->ibuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
   1.103 +	if (ctx->ibuf == NULL) { OPENSSL_free(ctx); return(0); }
   1.104 +	ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
   1.105 +	if (ctx->obuf == NULL) { OPENSSL_free(ctx->ibuf); OPENSSL_free(ctx); return(0); }
   1.106 +	ctx->ibuf_size=DEFAULT_BUFFER_SIZE;
   1.107 +	ctx->obuf_size=DEFAULT_BUFFER_SIZE;
   1.108 +	ctx->ibuf_len=0;
   1.109 +	ctx->ibuf_off=0;
   1.110 +	ctx->obuf_len=0;
   1.111 +	ctx->obuf_off=0;
   1.112 +
   1.113 +	bi->init=1;
   1.114 +	bi->ptr=(char *)ctx;
   1.115 +	bi->flags=0;
   1.116 +	return(1);
   1.117 +	}
   1.118 +
   1.119 +static int buffer_free(BIO *a)
   1.120 +	{
   1.121 +	BIO_F_BUFFER_CTX *b;
   1.122 +
   1.123 +	if (a == NULL) return(0);
   1.124 +	b=(BIO_F_BUFFER_CTX *)a->ptr;
   1.125 +	if (b->ibuf != NULL) OPENSSL_free(b->ibuf);
   1.126 +	if (b->obuf != NULL) OPENSSL_free(b->obuf);
   1.127 +	OPENSSL_free(a->ptr);
   1.128 +	a->ptr=NULL;
   1.129 +	a->init=0;
   1.130 +	a->flags=0;
   1.131 +	return(1);
   1.132 +	}
   1.133 +	
   1.134 +static int buffer_read(BIO *b, char *out, int outl)
   1.135 +	{
   1.136 +	int i,num=0;
   1.137 +	BIO_F_BUFFER_CTX *ctx;
   1.138 +
   1.139 +	if (out == NULL) return(0);
   1.140 +	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
   1.141 +
   1.142 +	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
   1.143 +	num=0;
   1.144 +	BIO_clear_retry_flags(b);
   1.145 +
   1.146 +start:
   1.147 +	i=ctx->ibuf_len;
   1.148 +	/* If there is stuff left over, grab it */
   1.149 +	if (i != 0)
   1.150 +		{
   1.151 +		if (i > outl) i=outl;
   1.152 +		memcpy(out,&(ctx->ibuf[ctx->ibuf_off]),i);
   1.153 +		ctx->ibuf_off+=i;
   1.154 +		ctx->ibuf_len-=i;
   1.155 +		num+=i;
   1.156 +		if (outl == i)  return(num);
   1.157 +		outl-=i;
   1.158 +		out+=i;
   1.159 +		}
   1.160 +
   1.161 +	/* We may have done a partial read. try to do more.
   1.162 +	 * We have nothing in the buffer.
   1.163 +	 * If we get an error and have read some data, just return it
   1.164 +	 * and let them retry to get the error again.
   1.165 +	 * copy direct to parent address space */
   1.166 +	if (outl > ctx->ibuf_size)
   1.167 +		{
   1.168 +		for (;;)
   1.169 +			{
   1.170 +			i=BIO_read(b->next_bio,out,outl);
   1.171 +			if (i <= 0)
   1.172 +				{
   1.173 +				BIO_copy_next_retry(b);
   1.174 +				if (i < 0) return((num > 0)?num:i);
   1.175 +				if (i == 0) return(num);
   1.176 +				}
   1.177 +			num+=i;
   1.178 +			if (outl == i) return(num);
   1.179 +			out+=i;
   1.180 +			outl-=i;
   1.181 +			}
   1.182 +		}
   1.183 +	/* else */
   1.184 +
   1.185 +	/* we are going to be doing some buffering */
   1.186 +	i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
   1.187 +	if (i <= 0)
   1.188 +		{
   1.189 +		BIO_copy_next_retry(b);
   1.190 +		if (i < 0) return((num > 0)?num:i);
   1.191 +		if (i == 0) return(num);
   1.192 +		}
   1.193 +	ctx->ibuf_off=0;
   1.194 +	ctx->ibuf_len=i;
   1.195 +
   1.196 +	/* Lets re-read using ourselves :-) */
   1.197 +	goto start;
   1.198 +	}
   1.199 +
   1.200 +static int buffer_write(BIO *b, const char *in, int inl)
   1.201 +	{
   1.202 +	int i,num=0;
   1.203 +	BIO_F_BUFFER_CTX *ctx;
   1.204 +
   1.205 +	if ((in == NULL) || (inl <= 0)) return(0);
   1.206 +	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
   1.207 +	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
   1.208 +
   1.209 +	BIO_clear_retry_flags(b);
   1.210 +start:
   1.211 +	i=ctx->obuf_size-(ctx->obuf_len+ctx->obuf_off);
   1.212 +	/* add to buffer and return */
   1.213 +	if (i >= inl)
   1.214 +		{
   1.215 +		memcpy(&(ctx->obuf[ctx->obuf_len]),in,inl);
   1.216 +		ctx->obuf_len+=inl;
   1.217 +		return(num+inl);
   1.218 +		}
   1.219 +	/* else */
   1.220 +	/* stuff already in buffer, so add to it first, then flush */
   1.221 +	if (ctx->obuf_len != 0)
   1.222 +		{
   1.223 +		if (i > 0) /* lets fill it up if we can */
   1.224 +			{
   1.225 +			memcpy(&(ctx->obuf[ctx->obuf_len]),in,i);
   1.226 +			in+=i;
   1.227 +			inl-=i;
   1.228 +			num+=i;
   1.229 +			ctx->obuf_len+=i;
   1.230 +			}
   1.231 +		/* we now have a full buffer needing flushing */
   1.232 +		for (;;)
   1.233 +			{
   1.234 +			i=BIO_write(b->next_bio,&(ctx->obuf[ctx->obuf_off]),
   1.235 +				ctx->obuf_len);
   1.236 +			if (i <= 0)
   1.237 +				{
   1.238 +				BIO_copy_next_retry(b);
   1.239 +
   1.240 +				if (i < 0) return((num > 0)?num:i);
   1.241 +				if (i == 0) return(num);
   1.242 +				}
   1.243 +			ctx->obuf_off+=i;
   1.244 +			ctx->obuf_len-=i;
   1.245 +			if (ctx->obuf_len == 0) break;
   1.246 +			}
   1.247 +		}
   1.248 +	/* we only get here if the buffer has been flushed and we
   1.249 +	 * still have stuff to write */
   1.250 +	ctx->obuf_off=0;
   1.251 +
   1.252 +	/* we now have inl bytes to write */
   1.253 +	while (inl >= ctx->obuf_size)
   1.254 +		{
   1.255 +		i=BIO_write(b->next_bio,in,inl);
   1.256 +		if (i <= 0)
   1.257 +			{
   1.258 +			BIO_copy_next_retry(b);
   1.259 +			if (i < 0) return((num > 0)?num:i);
   1.260 +			if (i == 0) return(num);
   1.261 +			}
   1.262 +		num+=i;
   1.263 +		in+=i;
   1.264 +		inl-=i;
   1.265 +		if (inl == 0) return(num);
   1.266 +		}
   1.267 +
   1.268 +	/* copy the rest into the buffer since we have only a small 
   1.269 +	 * amount left */
   1.270 +	goto start;
   1.271 +	}
   1.272 +
   1.273 +static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
   1.274 +	{
   1.275 +	BIO *dbio;
   1.276 +	BIO_F_BUFFER_CTX *ctx;
   1.277 +	long ret=1;
   1.278 +	char *p1,*p2;
   1.279 +	int r,i,*ip;
   1.280 +	int ibs,obs;
   1.281 +
   1.282 +	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
   1.283 +
   1.284 +	switch (cmd)
   1.285 +		{
   1.286 +	case BIO_CTRL_RESET:
   1.287 +		ctx->ibuf_off=0;
   1.288 +		ctx->ibuf_len=0;
   1.289 +		ctx->obuf_off=0;
   1.290 +		ctx->obuf_len=0;
   1.291 +		if (b->next_bio == NULL) return(0);
   1.292 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.293 +		break;
   1.294 +	case BIO_CTRL_INFO:
   1.295 +		ret=(long)ctx->obuf_len;
   1.296 +		break;
   1.297 +	case BIO_C_GET_BUFF_NUM_LINES:
   1.298 +		ret=0;
   1.299 +		p1=ctx->ibuf;
   1.300 +		for (i=ctx->ibuf_off; i<ctx->ibuf_len; i++)
   1.301 +			{
   1.302 +			if (p1[i] == '\n') ret++;
   1.303 +			}
   1.304 +		break;
   1.305 +	case BIO_CTRL_WPENDING:
   1.306 +		ret=(long)ctx->obuf_len;
   1.307 +		if (ret == 0)
   1.308 +			{
   1.309 +			if (b->next_bio == NULL) return(0);
   1.310 +			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.311 +			}
   1.312 +		break;
   1.313 +	case BIO_CTRL_PENDING:
   1.314 +		ret=(long)ctx->ibuf_len;
   1.315 +		if (ret == 0)
   1.316 +			{
   1.317 +			if (b->next_bio == NULL) return(0);
   1.318 +			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.319 +			}
   1.320 +		break;
   1.321 +	case BIO_C_SET_BUFF_READ_DATA:
   1.322 +		if (num > ctx->ibuf_size)
   1.323 +			{
   1.324 +			p1=OPENSSL_malloc((int)num);
   1.325 +			if (p1 == NULL) goto malloc_error;
   1.326 +			if (ctx->ibuf != NULL) OPENSSL_free(ctx->ibuf);
   1.327 +			ctx->ibuf=p1;
   1.328 +			}
   1.329 +		ctx->ibuf_off=0;
   1.330 +		ctx->ibuf_len=(int)num;
   1.331 +		memcpy(ctx->ibuf,ptr,(int)num);
   1.332 +		ret=1;
   1.333 +		break;
   1.334 +	case BIO_C_SET_BUFF_SIZE:
   1.335 +		if (ptr != NULL)
   1.336 +			{
   1.337 +			ip=(int *)ptr;
   1.338 +			if (*ip == 0)
   1.339 +				{
   1.340 +				ibs=(int)num;
   1.341 +				obs=ctx->obuf_size;
   1.342 +				}
   1.343 +			else /* if (*ip == 1) */
   1.344 +				{
   1.345 +				ibs=ctx->ibuf_size;
   1.346 +				obs=(int)num;
   1.347 +				}
   1.348 +			}
   1.349 +		else
   1.350 +			{
   1.351 +			ibs=(int)num;
   1.352 +			obs=(int)num;
   1.353 +			}
   1.354 +		p1=ctx->ibuf;
   1.355 +		p2=ctx->obuf;
   1.356 +		if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size))
   1.357 +			{
   1.358 +			p1=(char *)OPENSSL_malloc((int)num);
   1.359 +			if (p1 == NULL) goto malloc_error;
   1.360 +			}
   1.361 +		if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size))
   1.362 +			{
   1.363 +			p2=(char *)OPENSSL_malloc((int)num);
   1.364 +			if (p2 == NULL)
   1.365 +				{
   1.366 +				if (p1 != ctx->ibuf) OPENSSL_free(p1);
   1.367 +				goto malloc_error;
   1.368 +				}
   1.369 +			}
   1.370 +		if (ctx->ibuf != p1)
   1.371 +			{
   1.372 +			OPENSSL_free(ctx->ibuf);
   1.373 +			ctx->ibuf=p1;
   1.374 +			ctx->ibuf_off=0;
   1.375 +			ctx->ibuf_len=0;
   1.376 +			ctx->ibuf_size=ibs;
   1.377 +			}
   1.378 +		if (ctx->obuf != p2)
   1.379 +			{
   1.380 +			OPENSSL_free(ctx->obuf);
   1.381 +			ctx->obuf=p2;
   1.382 +			ctx->obuf_off=0;
   1.383 +			ctx->obuf_len=0;
   1.384 +			ctx->obuf_size=obs;
   1.385 +			}
   1.386 +		break;
   1.387 +	case BIO_C_DO_STATE_MACHINE:
   1.388 +		if (b->next_bio == NULL) return(0);
   1.389 +		BIO_clear_retry_flags(b);
   1.390 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.391 +		BIO_copy_next_retry(b);
   1.392 +		break;
   1.393 +
   1.394 +	case BIO_CTRL_FLUSH:
   1.395 +		if (b->next_bio == NULL) return(0);
   1.396 +		if (ctx->obuf_len <= 0)
   1.397 +			{
   1.398 +			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.399 +			break;
   1.400 +			}
   1.401 +
   1.402 +		for (;;)
   1.403 +			{
   1.404 +			BIO_clear_retry_flags(b);
   1.405 +			if (ctx->obuf_len > ctx->obuf_off)
   1.406 +				{
   1.407 +				r=BIO_write(b->next_bio,
   1.408 +					&(ctx->obuf[ctx->obuf_off]),
   1.409 +					ctx->obuf_len-ctx->obuf_off);
   1.410 +#if 0
   1.411 +fprintf(stderr,"FLUSH [%3d] %3d -> %3d\n",ctx->obuf_off,ctx->obuf_len-ctx->obuf_off,r);
   1.412 +#endif
   1.413 +				BIO_copy_next_retry(b);
   1.414 +				if (r <= 0) return((long)r);
   1.415 +				ctx->obuf_off+=r;
   1.416 +				}
   1.417 +			else
   1.418 +				{
   1.419 +				ctx->obuf_len=0;
   1.420 +				ctx->obuf_off=0;
   1.421 +				ret=1;
   1.422 +				break;
   1.423 +				}
   1.424 +			}
   1.425 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.426 +		break;
   1.427 +	case BIO_CTRL_DUP:
   1.428 +		dbio=(BIO *)ptr;
   1.429 +		if (	!BIO_set_read_buffer_size(dbio,ctx->ibuf_size) ||
   1.430 +			!BIO_set_write_buffer_size(dbio,ctx->obuf_size))
   1.431 +			ret=0;
   1.432 +		break;
   1.433 +	default:
   1.434 +		if (b->next_bio == NULL) return(0);
   1.435 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.436 +		break;
   1.437 +		}
   1.438 +	return(ret);
   1.439 +malloc_error:
   1.440 +	BIOerr(BIO_F_BUFFER_CTRL,ERR_R_MALLOC_FAILURE);
   1.441 +	return(0);
   1.442 +	}
   1.443 +
   1.444 +static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
   1.445 +	{
   1.446 +	long ret=1;
   1.447 +
   1.448 +	if (b->next_bio == NULL) return(0);
   1.449 +	switch (cmd)
   1.450 +		{
   1.451 +	default:
   1.452 +		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
   1.453 +		break;
   1.454 +		}
   1.455 +	return(ret);
   1.456 +	}
   1.457 +
   1.458 +static int buffer_gets(BIO *b, char *buf, int size)
   1.459 +	{
   1.460 +	BIO_F_BUFFER_CTX *ctx;
   1.461 +	int num=0,i,flag;
   1.462 +	char *p;
   1.463 +
   1.464 +	ctx=(BIO_F_BUFFER_CTX *)b->ptr;
   1.465 +	size--; /* reserve space for a '\0' */
   1.466 +	BIO_clear_retry_flags(b);
   1.467 +
   1.468 +	for (;;)
   1.469 +		{
   1.470 +		if (ctx->ibuf_len > 0)
   1.471 +			{
   1.472 +			p= &(ctx->ibuf[ctx->ibuf_off]);
   1.473 +			flag=0;
   1.474 +			for (i=0; (i<ctx->ibuf_len) && (i<size); i++)
   1.475 +				{
   1.476 +				*(buf++)=p[i];
   1.477 +				if (p[i] == '\n')
   1.478 +					{
   1.479 +					flag=1;
   1.480 +					i++;
   1.481 +					break;
   1.482 +					}
   1.483 +				}
   1.484 +			num+=i;
   1.485 +			size-=i;
   1.486 +			ctx->ibuf_len-=i;
   1.487 +			ctx->ibuf_off+=i;
   1.488 +			if (flag || size == 0)
   1.489 +				{
   1.490 +				*buf='\0';
   1.491 +				return(num);
   1.492 +				}
   1.493 +			}
   1.494 +		else	/* read another chunk */
   1.495 +			{
   1.496 +			i=BIO_read(b->next_bio,ctx->ibuf,ctx->ibuf_size);
   1.497 +			if (i <= 0)
   1.498 +				{
   1.499 +				BIO_copy_next_retry(b);
   1.500 +				*buf='\0';
   1.501 +				if (i < 0) return((num > 0)?num:i);
   1.502 +				if (i == 0) return(num);
   1.503 +				}
   1.504 +			ctx->ibuf_len=i;
   1.505 +			ctx->ibuf_off=0;
   1.506 +			}
   1.507 +		}
   1.508 +	}
   1.509 +
   1.510 +static int buffer_puts(BIO *b, const char *str)
   1.511 +	{
   1.512 +	return(buffer_write(b,str,strlen(str)));
   1.513 +	}
   1.514 +