os/ossrv/ssl/libcrypto/src/crypto/bio/bio_lib.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/bio_lib.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,603 @@
     1.4 +/* crypto/bio/bio_lib.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 <openssl/crypto.h>
    1.65 +#include "cryptlib.h"
    1.66 +#include <openssl/bio.h>
    1.67 +#include <openssl/stack.h>
    1.68 +
    1.69 +EXPORT_C BIO *BIO_new(BIO_METHOD *method)
    1.70 +	{
    1.71 +	BIO *ret=NULL;
    1.72 +
    1.73 +	ret=(BIO *)OPENSSL_malloc(sizeof(BIO));
    1.74 +	if (ret == NULL)
    1.75 +		{
    1.76 +		BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);
    1.77 +		return(NULL);
    1.78 +		}
    1.79 +#ifdef SYMBIAN
    1.80 +   memset(ret,0,sizeof(BIO));
    1.81 +#endif		
    1.82 +	if (!BIO_set(ret,method))
    1.83 +		{
    1.84 +		OPENSSL_free(ret);
    1.85 +		ret=NULL;
    1.86 +		}
    1.87 +	return(ret);
    1.88 +	}
    1.89 +
    1.90 +EXPORT_C int BIO_set(BIO *bio, BIO_METHOD *method)
    1.91 +	{
    1.92 +	bio->method=method;
    1.93 +	bio->callback=NULL;
    1.94 +	bio->cb_arg=NULL;
    1.95 +	bio->init=0;
    1.96 +	bio->shutdown=1;
    1.97 +	bio->flags=0;
    1.98 +	bio->retry_reason=0;
    1.99 +	bio->num=0;
   1.100 +	bio->ptr=NULL;
   1.101 +	bio->prev_bio=NULL;
   1.102 +	bio->next_bio=NULL;
   1.103 +	bio->references=1;
   1.104 +	bio->num_read=0L;
   1.105 +	bio->num_write=0L;
   1.106 +	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
   1.107 +	if (method->create != NULL)
   1.108 +		if (!method->create(bio))
   1.109 +			{
   1.110 +			CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
   1.111 +					&bio->ex_data);
   1.112 +			return(0);
   1.113 +			}
   1.114 +	return(1);
   1.115 +	}
   1.116 +
   1.117 +EXPORT_C int BIO_free(BIO *a)
   1.118 +	{
   1.119 +	int ret=0,i;
   1.120 +
   1.121 +	if (a == NULL) return(0);
   1.122 +
   1.123 +	i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO);
   1.124 +#ifdef REF_PRINT
   1.125 +	REF_PRINT("BIO",a);
   1.126 +#endif
   1.127 +	if (i > 0) return(1);
   1.128 +#ifdef REF_CHECK
   1.129 +	if (i < 0)
   1.130 +		{
   1.131 +		fprintf(stderr,"BIO_free, bad reference count\n");
   1.132 +		abort();
   1.133 +		}
   1.134 +#endif
   1.135 +	if ((a->callback != NULL) &&
   1.136 +		((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
   1.137 +			return(i);
   1.138 +
   1.139 +	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
   1.140 +
   1.141 +	if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
   1.142 +	ret=a->method->destroy(a);
   1.143 +	OPENSSL_free(a);
   1.144 +	return(1);
   1.145 +	}
   1.146 +
   1.147 +EXPORT_C void BIO_vfree(BIO *a)
   1.148 +    { BIO_free(a); }
   1.149 +
   1.150 +EXPORT_C void BIO_clear_flags(BIO *b, int flags)
   1.151 +	{
   1.152 +	b->flags &= ~flags;
   1.153 +	}
   1.154 +
   1.155 +EXPORT_C int	BIO_test_flags(const BIO *b, int flags)
   1.156 +	{
   1.157 +	return (b->flags & flags);
   1.158 +	}
   1.159 +
   1.160 +EXPORT_C void	BIO_set_flags(BIO *b, int flags)
   1.161 +	{
   1.162 +	b->flags |= flags;
   1.163 +	}
   1.164 +
   1.165 +EXPORT_C long (*BIO_get_callback(const BIO *b))(struct bio_st *,int,const char *,int, long,long)
   1.166 +	{
   1.167 +	return b->callback;
   1.168 +	}
   1.169 +
   1.170 +EXPORT_C void BIO_set_callback(BIO *b, long (*cb)(struct bio_st *,int,const char *,int, long,long))
   1.171 +	{
   1.172 +	b->callback = cb;
   1.173 +	}
   1.174 +
   1.175 +EXPORT_C void BIO_set_callback_arg(BIO *b, char *arg)
   1.176 +	{
   1.177 +	b->cb_arg = arg;
   1.178 +	}
   1.179 +
   1.180 +EXPORT_C char * BIO_get_callback_arg(const BIO *b)
   1.181 +	{
   1.182 +	return b->cb_arg;
   1.183 +	}
   1.184 +
   1.185 +EXPORT_C const char * BIO_method_name(const BIO *b)
   1.186 +	{
   1.187 +	return b->method->name;
   1.188 +	}
   1.189 +
   1.190 +EXPORT_C int BIO_method_type(const BIO *b)
   1.191 +	{
   1.192 +	return b->method->type;
   1.193 +	}
   1.194 +EXPORT_C int BIO_read(BIO *b, void *out, int outl)
   1.195 +	{
   1.196 +	int i;
   1.197 +	long (*cb)(BIO *,int,const char *,int,long,long);
   1.198 +
   1.199 +	if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL))
   1.200 +		{
   1.201 +		BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD);
   1.202 +		return(-2);
   1.203 +		}
   1.204 +
   1.205 +	cb=b->callback;
   1.206 +	if ((cb != NULL) &&
   1.207 +		((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0))
   1.208 +			return(i);
   1.209 +
   1.210 +	if (!b->init)
   1.211 +		{
   1.212 +		BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED);
   1.213 +		return(-2);
   1.214 +		}
   1.215 +
   1.216 +	i=b->method->bread(b,out,outl);
   1.217 +
   1.218 +	if (i > 0) b->num_read+=(unsigned long)i;
   1.219 +
   1.220 +	if (cb != NULL)
   1.221 +		i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl,
   1.222 +			0L,(long)i);
   1.223 +	return(i);
   1.224 +	}
   1.225 +
   1.226 +EXPORT_C int BIO_write(BIO *b, const void *in, int inl)
   1.227 +	{
   1.228 +	int i;
   1.229 +	long (*cb)(BIO *,int,const char *,int,long,long);
   1.230 +
   1.231 +	if (b == NULL)
   1.232 +		return(0);
   1.233 +
   1.234 +	cb=b->callback;
   1.235 +	if ((b->method == NULL) || (b->method->bwrite == NULL))
   1.236 +		{
   1.237 +		BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD);
   1.238 +		return(-2);
   1.239 +		}
   1.240 +
   1.241 +	if ((cb != NULL) &&
   1.242 +		((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0))
   1.243 +			return(i);
   1.244 +
   1.245 +	if (!b->init)
   1.246 +		{
   1.247 +		BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED);
   1.248 +		return(-2);
   1.249 +		}
   1.250 +
   1.251 +	i=b->method->bwrite(b,in,inl);
   1.252 +
   1.253 +	if (i > 0) b->num_write+=(unsigned long)i;
   1.254 +
   1.255 +	if (cb != NULL)
   1.256 +		i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl,
   1.257 +			0L,(long)i);
   1.258 +	return(i);
   1.259 +	}
   1.260 +
   1.261 +EXPORT_C int BIO_puts(BIO *b, const char *in)
   1.262 +	{
   1.263 +	int i;
   1.264 +	long (*cb)(BIO *,int,const char *,int,long,long);
   1.265 +
   1.266 +	if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL))
   1.267 +		{
   1.268 +		BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD);
   1.269 +		return(-2);
   1.270 +		}
   1.271 +
   1.272 +	cb=b->callback;
   1.273 +
   1.274 +	if ((cb != NULL) &&
   1.275 +		((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0))
   1.276 +			return(i);
   1.277 +
   1.278 +	if (!b->init)
   1.279 +		{
   1.280 +		BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);
   1.281 +		return(-2);
   1.282 +		}
   1.283 +
   1.284 +	i=b->method->bputs(b,in);
   1.285 +
   1.286 +	if (i > 0) b->num_write+=(unsigned long)i;
   1.287 +
   1.288 +	if (cb != NULL)
   1.289 +		i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,
   1.290 +			0L,(long)i);
   1.291 +	return(i);
   1.292 +	}
   1.293 +
   1.294 +EXPORT_C int BIO_gets(BIO *b, char *in, int inl)
   1.295 +	{
   1.296 +	int i;
   1.297 +	long (*cb)(BIO *,int,const char *,int,long,long);
   1.298 +
   1.299 +	if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL))
   1.300 +		{
   1.301 +		BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD);
   1.302 +		return(-2);
   1.303 +		}
   1.304 +
   1.305 +	cb=b->callback;
   1.306 +
   1.307 +	if ((cb != NULL) &&
   1.308 +		((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0))
   1.309 +			return(i);
   1.310 +
   1.311 +	if (!b->init)
   1.312 +		{
   1.313 +		BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED);
   1.314 +		return(-2);
   1.315 +		}
   1.316 +
   1.317 +	i=b->method->bgets(b,in,inl);
   1.318 +
   1.319 +	if (cb != NULL)
   1.320 +		i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl,
   1.321 +			0L,(long)i);
   1.322 +	return(i);
   1.323 +	}
   1.324 +
   1.325 +EXPORT_C int BIO_indent(BIO *b,int indent,int max)
   1.326 +	{
   1.327 +	if(indent < 0)
   1.328 +		indent=0;
   1.329 +	if(indent > max)
   1.330 +		indent=max;
   1.331 +	while(indent--)
   1.332 +		if(BIO_puts(b," ") != 1)
   1.333 +			return 0;
   1.334 +	return 1;
   1.335 +	}
   1.336 +
   1.337 +EXPORT_C long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
   1.338 +	{
   1.339 +	int i;
   1.340 +
   1.341 +	i=iarg;
   1.342 +	return(BIO_ctrl(b,cmd,larg,(char *)&i));
   1.343 +	}
   1.344 +
   1.345 +EXPORT_C char *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
   1.346 +	{
   1.347 +	char *p=NULL;
   1.348 +
   1.349 +	if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0)
   1.350 +		return(NULL);
   1.351 +	else
   1.352 +		return(p);
   1.353 +	}
   1.354 +
   1.355 +EXPORT_C long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
   1.356 +	{
   1.357 +	long ret;
   1.358 +	long (*cb)(BIO *,int,const char *,int,long,long);
   1.359 +
   1.360 +	if (b == NULL) return(0);
   1.361 +
   1.362 +	if ((b->method == NULL) || (b->method->ctrl == NULL))
   1.363 +		{
   1.364 +		BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
   1.365 +		return(-2);
   1.366 +		}
   1.367 +
   1.368 +	cb=b->callback;
   1.369 +
   1.370 +	if ((cb != NULL) &&
   1.371 +		((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0))
   1.372 +		return(ret);
   1.373 +
   1.374 +	ret=b->method->ctrl(b,cmd,larg,parg);
   1.375 +
   1.376 +	if (cb != NULL)
   1.377 +		ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd,
   1.378 +			larg,ret);
   1.379 +	return(ret);
   1.380 +	}
   1.381 +
   1.382 +EXPORT_C long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long))
   1.383 +	{
   1.384 +	long ret;
   1.385 +	long (*cb)(BIO *,int,const char *,int,long,long);
   1.386 +
   1.387 +	if (b == NULL) return(0);
   1.388 +
   1.389 +	if ((b->method == NULL) || (b->method->callback_ctrl == NULL))
   1.390 +		{
   1.391 +		BIOerr(BIO_F_BIO_CALLBACK_CTRL,BIO_R_UNSUPPORTED_METHOD);
   1.392 +		return(-2);
   1.393 +		}
   1.394 +
   1.395 +	cb=b->callback;
   1.396 +
   1.397 +	if ((cb != NULL) &&
   1.398 +		((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0))
   1.399 +		return(ret);
   1.400 +
   1.401 +	ret=b->method->callback_ctrl(b,cmd,fp);
   1.402 +
   1.403 +	if (cb != NULL)
   1.404 +		ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd,
   1.405 +			0,ret);
   1.406 +	return(ret);
   1.407 +	}
   1.408 +
   1.409 +/* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
   1.410 + * do; but those macros have inappropriate return type, and for interfacing
   1.411 + * from other programming languages, C macros aren't much of a help anyway. */
   1.412 +EXPORT_C size_t BIO_ctrl_pending(BIO *bio)
   1.413 +	{
   1.414 +	return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
   1.415 +	}
   1.416 +
   1.417 +EXPORT_C size_t BIO_ctrl_wpending(BIO *bio)
   1.418 +	{
   1.419 +	return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
   1.420 +	}
   1.421 +
   1.422 +
   1.423 +/* put the 'bio' on the end of b's list of operators */
   1.424 +EXPORT_C BIO *BIO_push(BIO *b, BIO *bio)
   1.425 +	{
   1.426 +	BIO *lb;
   1.427 +
   1.428 +	if (b == NULL) return(bio);
   1.429 +	lb=b;
   1.430 +	while (lb->next_bio != NULL)
   1.431 +		lb=lb->next_bio;
   1.432 +	lb->next_bio=bio;
   1.433 +	if (bio != NULL)
   1.434 +		bio->prev_bio=lb;
   1.435 +	/* called to do internal processing */
   1.436 +	BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL);
   1.437 +	return(b);
   1.438 +	}
   1.439 +
   1.440 +/* Remove the first and return the rest */
   1.441 +EXPORT_C BIO *BIO_pop(BIO *b)
   1.442 +	{
   1.443 +	BIO *ret;
   1.444 +
   1.445 +	if (b == NULL) return(NULL);
   1.446 +	ret=b->next_bio;
   1.447 +
   1.448 +	BIO_ctrl(b,BIO_CTRL_POP,0,NULL);
   1.449 +
   1.450 +	if (b->prev_bio != NULL)
   1.451 +		b->prev_bio->next_bio=b->next_bio;
   1.452 +	if (b->next_bio != NULL)
   1.453 +		b->next_bio->prev_bio=b->prev_bio;
   1.454 +
   1.455 +	b->next_bio=NULL;
   1.456 +	b->prev_bio=NULL;
   1.457 +	return(ret);
   1.458 +	}
   1.459 +
   1.460 +EXPORT_C BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
   1.461 +	{
   1.462 +	BIO *b,*last;
   1.463 +
   1.464 +	b=last=bio;
   1.465 +	for (;;)
   1.466 +		{
   1.467 +		if (!BIO_should_retry(b)) break;
   1.468 +		last=b;
   1.469 +		b=b->next_bio;
   1.470 +		if (b == NULL) break;
   1.471 +		}
   1.472 +	if (reason != NULL) *reason=last->retry_reason;
   1.473 +	return(last);
   1.474 +	}
   1.475 +
   1.476 +EXPORT_C int BIO_get_retry_reason(BIO *bio)
   1.477 +	{
   1.478 +	return(bio->retry_reason);
   1.479 +	}
   1.480 +
   1.481 +EXPORT_C BIO *BIO_find_type(BIO *bio, int type)
   1.482 +	{
   1.483 +	int mt,mask;
   1.484 +
   1.485 +	if(!bio) return NULL;
   1.486 +	mask=type&0xff;
   1.487 +	do	{
   1.488 +		if (bio->method != NULL)
   1.489 +			{
   1.490 +			mt=bio->method->type;
   1.491 +
   1.492 +			if (!mask)
   1.493 +				{
   1.494 +				if (mt & type) return(bio);
   1.495 +				}
   1.496 +			else if (mt == type)
   1.497 +				return(bio);
   1.498 +			}
   1.499 +		bio=bio->next_bio;
   1.500 +		} while (bio != NULL);
   1.501 +	return(NULL);
   1.502 +	}
   1.503 +
   1.504 +EXPORT_C BIO *BIO_next(BIO *b)
   1.505 +	{
   1.506 +	if(!b) return NULL;
   1.507 +	return b->next_bio;
   1.508 +	}
   1.509 +
   1.510 +EXPORT_C void BIO_free_all(BIO *bio)
   1.511 +	{
   1.512 +	BIO *b;
   1.513 +	int ref;
   1.514 +
   1.515 +	while (bio != NULL)
   1.516 +		{
   1.517 +		b=bio;
   1.518 +		ref=b->references;
   1.519 +		bio=bio->next_bio;
   1.520 +		BIO_free(b);
   1.521 +		/* Since ref count > 1, don't free anyone else. */
   1.522 +		if (ref > 1) break;
   1.523 +		}
   1.524 +	}
   1.525 +
   1.526 +EXPORT_C BIO *BIO_dup_chain(BIO *in)
   1.527 +	{
   1.528 +	BIO *ret=NULL,*eoc=NULL,*bio,*new;
   1.529 +
   1.530 +	for (bio=in; bio != NULL; bio=bio->next_bio)
   1.531 +		{
   1.532 +		if ((new=BIO_new(bio->method)) == NULL) goto err;
   1.533 +		new->callback=bio->callback;
   1.534 +		new->cb_arg=bio->cb_arg;
   1.535 +		new->init=bio->init;
   1.536 +		new->shutdown=bio->shutdown;
   1.537 +		new->flags=bio->flags;
   1.538 +
   1.539 +		/* This will let SSL_s_sock() work with stdin/stdout */
   1.540 +		new->num=bio->num;
   1.541 +
   1.542 +		if (!BIO_dup_state(bio,(char *)new))
   1.543 +			{
   1.544 +			BIO_free(new);
   1.545 +			goto err;
   1.546 +			}
   1.547 +
   1.548 +		/* copy app data */
   1.549 +		if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data,
   1.550 +					&bio->ex_data))
   1.551 +			goto err;
   1.552 +
   1.553 +		if (ret == NULL)
   1.554 +			{
   1.555 +			eoc=new;
   1.556 +			ret=eoc;
   1.557 +			}
   1.558 +		else
   1.559 +			{
   1.560 +			BIO_push(eoc,new);
   1.561 +			eoc=new;
   1.562 +			}
   1.563 +		}
   1.564 +	return(ret);
   1.565 +err:
   1.566 +	if (ret != NULL)
   1.567 +		BIO_free(ret);
   1.568 +	return(NULL);	
   1.569 +	}
   1.570 +
   1.571 +EXPORT_C void BIO_copy_next_retry(BIO *b)
   1.572 +	{
   1.573 +	BIO_set_flags(b,BIO_get_retry_flags(b->next_bio));
   1.574 +	b->retry_reason=b->next_bio->retry_reason;
   1.575 +	}
   1.576 +
   1.577 +EXPORT_C int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
   1.578 +	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
   1.579 +	{
   1.580 +	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
   1.581 +				new_func, dup_func, free_func);
   1.582 +	}
   1.583 +
   1.584 +EXPORT_C int BIO_set_ex_data(BIO *bio, int idx, void *data)
   1.585 +	{
   1.586 +	return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
   1.587 +	}
   1.588 +
   1.589 +EXPORT_C void *BIO_get_ex_data(BIO *bio, int idx)
   1.590 +	{
   1.591 +	return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
   1.592 +	}
   1.593 +
   1.594 +EXPORT_C unsigned long BIO_number_read(BIO *bio)
   1.595 +{
   1.596 +	if(bio) return bio->num_read;
   1.597 +	return 0;
   1.598 +}
   1.599 +
   1.600 +EXPORT_C unsigned long BIO_number_written(BIO *bio)
   1.601 +{
   1.602 +	if(bio) return bio->num_write;
   1.603 +	return 0;
   1.604 +}
   1.605 +
   1.606 +IMPLEMENT_STACK_OF(BIO)