j=(sw_num > (long)sizeof(sbuf))? sizeof(sbuf):(int)sw_num;
j = (sw_num > (long)sizeof(sbuf)) ? (int)sizeof(sbuf) : (int)sw_num;
int doit(SSL *s_ssl, SSL *c_ssl, long count)	{	MS_STATIC char cbuf[1024*8],sbuf[1024*8];	long cw_num=count,cr_num=count;	long sw_num=count,sr_num=count;	int ret=1;	BIO *c_to_s=NULL;	BIO *s_to_c=NULL;	BIO *c_bio=NULL;	BIO *s_bio=NULL;	int c_r,c_w,s_r,s_w;	int c_want,s_want;	int i,j;	int done=0;	int c_write,s_write;	int do_server=0,do_client=0;	memset(cbuf,0,sizeof(cbuf));	memset(sbuf,0,sizeof(sbuf));	c_to_s=BIO_new(BIO_s_mem());	s_to_c=BIO_new(BIO_s_mem());	if ((s_to_c == NULL) || (c_to_s == NULL))		{		ERR_print_errors(bio_err);		goto err;		}	c_bio=BIO_new(BIO_f_ssl());	s_bio=BIO_new(BIO_f_ssl());	if ((c_bio == NULL) || (s_bio == NULL))		{		ERR_print_errors(bio_err);		goto err;		}	SSL_set_connect_state(c_ssl);	SSL_set_bio(c_ssl,s_to_c,c_to_s);	BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);	SSL_set_accept_state(s_ssl);	SSL_set_bio(s_ssl,c_to_s,s_to_c);	BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);	c_r=0; s_r=1;	c_w=1; s_w=0;	c_want=W_WRITE;	s_want=0;	c_write=1,s_write=0;	/* We can always do writes */	for (;;)		{		do_server=0;		do_client=0;		i=(int)BIO_pending(s_bio);		if ((i && s_r) || s_w) do_server=1;		i=(int)BIO_pending(c_bio);		if ((i && c_r) || c_w) do_client=1;		if (do_server && debug)			{			if (SSL_in_init(s_ssl))				printf("server waiting in SSL_accept - %s\n",					SSL_state_string_long(s_ssl));/*			else if (s_write)				printf("server:SSL_write()\n");			else				printf("server:SSL_read()\n"); */			}		if (do_client && debug)			{			if (SSL_in_init(c_ssl))				printf("client waiting in SSL_connect - %s\n",					SSL_state_string_long(c_ssl));/*			else if (c_write)				printf("client:SSL_write()\n");			else				printf("client:SSL_read()\n"); */			}		if (!do_client && !do_server)			{			fprintf(stdout,"ERROR IN STARTUP\n");			ERR_print_errors(bio_err);			break;			}		if (do_client && !(done & C_DONE))			{			if (c_write)				{				j=(cw_num > (long)sizeof(cbuf))					?sizeof(cbuf):(int)cw_num;				i=BIO_write(c_bio,cbuf,j);				if (i < 0)					{					c_r=0;					c_w=0;					if (BIO_should_retry(c_bio))						{						if (BIO_should_read(c_bio))							c_r=1;						if (BIO_should_write(c_bio))							c_w=1;						}					else						{						fprintf(stderr,"ERROR in CLIENT\n");						ERR_print_errors(bio_err);						goto err;						}					}				else if (i == 0)					{					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");					goto err;					}				else					{					if (debug)						printf("client wrote %d\n",i);					/* ok */					s_r=1;					c_write=0;					cw_num-=i;					}				}			else				{				i=BIO_read(c_bio,cbuf,sizeof(cbuf));				if (i < 0)					{					c_r=0;					c_w=0;					if (BIO_should_retry(c_bio))						{						if (BIO_should_read(c_bio))							c_r=1;						if (BIO_should_write(c_bio))							c_w=1;						}					else						{						fprintf(stderr,"ERROR in CLIENT\n");						ERR_print_errors(bio_err);						goto err;						}					}				else if (i == 0)					{					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");					goto err;					}				else					{					if (debug)						printf("client read %d\n",i);					cr_num-=i;					if (sw_num > 0)						{						s_write=1;						s_w=1;						}					if (cr_num <= 0)						{						s_write=1;						s_w=1;						done=S_DONE|C_DONE;						}					}				}			}		if (do_server && !(done & S_DONE))			{			if (!s_write)				{				i=BIO_read(s_bio,sbuf,sizeof(cbuf));				if (i < 0)					{					s_r=0;					s_w=0;					if (BIO_should_retry(s_bio))						{						if (BIO_should_read(s_bio))							s_r=1;						if (BIO_should_write(s_bio))							s_w=1;						}					else						{						fprintf(stderr,"ERROR in SERVER\n");						ERR_print_errors(bio_err);						goto err;						}					}				else if (i == 0)					{					ERR_print_errors(bio_err);					fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");					goto err;					}				else					{					if (debug)						printf("server read %d\n",i);					sr_num-=i;					if (cw_num > 0)						{						c_write=1;						c_w=1;						}					if (sr_num <= 0)						{						s_write=1;						s_w=1;						c_write=0;						}					}				}			else				{				j=(sw_num > (long)sizeof(sbuf))?					sizeof(sbuf):(int)sw_num;				i=BIO_write(s_bio,sbuf,j);				if (i < 0)					{					s_r=0;					s_w=0;					if (BIO_should_retry(s_bio))						{						if (BIO_should_read(s_bio))							s_r=1;						if (BIO_should_write(s_bio))							s_w=1;						}					else						{						fprintf(stderr,"ERROR in SERVER\n");						ERR_print_errors(bio_err);						goto err;						}					}				else if (i == 0)					{					ERR_print_errors(bio_err);					fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");					goto err;					}				else					{					if (debug)						printf("server wrote %d\n",i);					sw_num-=i;					s_write=0;					c_r=1;					if (sw_num <= 0)						done|=S_DONE;					}				}			}		if ((done & S_DONE) && (done & C_DONE)) break;		}	if (verbose)		print_details(c_ssl, "DONE: ");	ret=0;err:	/* We have to set the BIO's to NULL otherwise they will be	 * OPENSSL_free()ed twice.  Once when th s_ssl is SSL_free()ed and	 * again when c_ssl is SSL_free()ed.	 * This is a hack required because s_ssl and c_ssl are sharing the same	 * BIO structure and SSL_set_bio() and SSL_free() automatically	 * BIO_free non NULL entries.	 * You should not normally do this or be required to do this */	if (s_ssl != NULL)		{		s_ssl->rbio=NULL;		s_ssl->wbio=NULL;		}	if (c_ssl != NULL)		{		c_ssl->rbio=NULL;		c_ssl->wbio=NULL;		}	if (c_to_s != NULL) BIO_free(c_to_s);	if (s_to_c != NULL) BIO_free(s_to_c);	if (c_bio != NULL) BIO_free_all(c_bio);	if (s_bio != NULL) BIO_free_all(s_bio);	return(ret);	}