static int ssl3_read_n(SSL *s, int n, int max, int extend)
	{
	int i,off,newb;
	if (!extend)
		{
		if (s->s3->rbuf.left == 0)
			s->s3->rbuf.offset = 0;
		s->packet = s->s3->rbuf.buf + s->s3->rbuf.offset;
		s->packet_length = 0;
		}
	if (s->s3->rbuf.left >= (int)n)
		{
		s->packet_length+=n;
		s->s3->rbuf.left-=n;
		s->s3->rbuf.offset+=n;
		return(n);
		}
	if (!s->read_ahead)
		max=n;
	{
		int max_max = SSL3_RT_MAX_PACKET_SIZE - s->packet_length;
		if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
			max_max += SSL3_RT_MAX_EXTRA;
		if (max > max_max)
			max = max_max;
	}
	if (n > max)
		{
		SSLerr(SSL_F_SSL3_READ_N,SSL_R_INTERNAL_ERROR);
		return -1;
		}
	off = s->packet_length;
	newb = s->s3->rbuf.left;
	if (s->packet != s->s3->rbuf.buf)
		{
		memmove(s->s3->rbuf.buf, s->packet, off+newb);
		s->packet = s->s3->rbuf.buf;
		}
	while (newb < n)
		{
		clear_sys_error();
		if (s->rbio != NULL)
			{
			s->rwstate=SSL_READING;
			i=BIO_read(s->rbio,	&(s->s3->rbuf.buf[off+newb]), max-newb);
			}
		else
			{
			SSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET);
			i = -1;
			}
		if (i <= 0)
			{
			s->s3->rbuf.left = newb;
			return(i);
			}
		newb+=i;
		}
	s->s3->rbuf.offset = off + n;
	s->s3->rbuf.left = newb - n;
	s->packet_length += n;
	s->rwstate=SSL_NOTHING;
	return(n);
	}
['static int ssl3_get_record(SSL *s)\n\t{\n\tint ssl_major,ssl_minor,al;\n\tint n,i,ret= -1;\n\tSSL3_RECORD *rr;\n\tSSL_SESSION *sess;\n\tunsigned char *p;\n\tunsigned char md[EVP_MAX_MD_SIZE];\n\tshort version;\n\tunsigned int mac_size;\n\tint clear=0,extra;\n\trr= &(s->s3->rrec);\n\tsess=s->session;\n\tif (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)\n\t\textra=SSL3_RT_MAX_EXTRA;\n\telse\n\t\textra=0;\nagain:\n\tif (\t(s->rstate != SSL_ST_READ_BODY) ||\n\t\t(s->packet_length < SSL3_RT_HEADER_LENGTH))\n\t\t{\n\t\tn=ssl3_read_n(s,SSL3_RT_HEADER_LENGTH,\n\t\t\tSSL3_RT_MAX_PACKET_SIZE,0);\n\t\tif (n <= 0) return(n);\n\t\ts->rstate=SSL_ST_READ_BODY;\n\t\tp=s->packet;\n\t\trr->type= *(p++);\n\t\tssl_major= *(p++);\n\t\tssl_minor= *(p++);\n\t\tversion=(ssl_major<<8)|ssl_minor;\n\t\tn2s(p,rr->length);\n\t\tif (s->first_packet)\n\t\t\t{\n\t\t\ts->first_packet=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (version != s->version)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\t\ts->version=version;\n\t\t\t\tal=SSL_AD_PROTOCOL_VERSION;\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((version>>8) != SSL3_VERSION_MAJOR)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (rr->length >\n\t\t\t(unsigned int)SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (rr->length > (s->packet_length-SSL3_RT_HEADER_LENGTH))\n\t\t{\n\t\ti=rr->length;\n\t\tn=ssl3_read_n(s,i,i,1);\n\t\tif (n <= 0) return(n);\n\t\t}\n\ts->rstate=SSL_ST_READ_HEADER;\n\trr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]);\n\tif (rr->length > (unsigned int)SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)\n\t\t{\n\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n\trr->data=rr->input;\n\tif (!s->method->ssl3_enc->enc(s,0))\n\t\t{\n\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\tgoto f_err;\n\t\t}\n#ifdef TLS_DEBUG\nprintf("dec %d\\n",rr->length);\n{ unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?\' \':\'\\n\'); }\nprintf("\\n");\n#endif\n\tif (\t(sess == NULL) ||\n\t\t(s->enc_read_ctx == NULL) ||\n\t\t(s->read_hash == NULL))\n\t\tclear=1;\n\tif (!clear)\n\t\t{\n\t\tmac_size=EVP_MD_size(s->read_hash);\n\t\tif (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (rr->length < mac_size)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\trr->length-=mac_size;\n\t\ti=s->method->ssl3_enc->mac(s,md,0);\n\t\tif (memcmp(md,&(rr->data[rr->length]),mac_size) != 0)\n\t\t\t{\n\t\t\tal=SSL_AD_BAD_RECORD_MAC;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_MAC_DECODE);\n\t\t\tret= -1;\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (s->expand != NULL)\n\t\t{\n\t\tif (rr->length >\n\t\t\t(unsigned int)SSL3_RT_MAX_COMPRESSED_LENGTH+extra)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!do_uncompress(s))\n\t\t\t{\n\t\t\tal=SSL_AD_DECOMPRESSION_FAILURE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (rr->length > (unsigned int)SSL3_RT_MAX_PLAIN_LENGTH+extra)\n\t\t{\n\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n\trr->off=0;\n\ts->packet_length=0;\n\tif (rr->length == 0) goto again;\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(ret);\n\t}', 'static int ssl3_read_n(SSL *s, int n, int max, int extend)\n\t{\n\tint i,off,newb;\n\tif (!extend)\n\t\t{\n\t\tif (s->s3->rbuf.left == 0)\n\t\t\ts->s3->rbuf.offset = 0;\n\t\ts->packet = s->s3->rbuf.buf + s->s3->rbuf.offset;\n\t\ts->packet_length = 0;\n\t\t}\n\tif (s->s3->rbuf.left >= (int)n)\n\t\t{\n\t\ts->packet_length+=n;\n\t\ts->s3->rbuf.left-=n;\n\t\ts->s3->rbuf.offset+=n;\n\t\treturn(n);\n\t\t}\n\tif (!s->read_ahead)\n\t\tmax=n;\n\t{\n\t\tint max_max = SSL3_RT_MAX_PACKET_SIZE - s->packet_length;\n\t\tif (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)\n\t\t\tmax_max += SSL3_RT_MAX_EXTRA;\n\t\tif (max > max_max)\n\t\t\tmax = max_max;\n\t}\n\tif (n > max)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_READ_N,SSL_R_INTERNAL_ERROR);\n\t\treturn -1;\n\t\t}\n\toff = s->packet_length;\n\tnewb = s->s3->rbuf.left;\n\tif (s->packet != s->s3->rbuf.buf)\n\t\t{\n\t\tmemmove(s->s3->rbuf.buf, s->packet, off+newb);\n\t\ts->packet = s->s3->rbuf.buf;\n\t\t}\n\twhile (newb < n)\n\t\t{\n\t\tclear_sys_error();\n\t\tif (s->rbio != NULL)\n\t\t\t{\n\t\t\ts->rwstate=SSL_READING;\n\t\t\ti=BIO_read(s->rbio,\t&(s->s3->rbuf.buf[off+newb]), max-newb);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET);\n\t\t\ti = -1;\n\t\t\t}\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\ts->s3->rbuf.left = newb;\n\t\t\treturn(i);\n\t\t\t}\n\t\tnewb+=i;\n\t\t}\n\ts->s3->rbuf.offset = off + n;\n\ts->s3->rbuf.left = newb - n;\n\ts->packet_length += n;\n\ts->rwstate=SSL_NOTHING;\n\treturn(n);\n\t}']