int ssl3_read_n(SSL *s, int n, int max, int extend)
	{
	int i,len,left,align=0;
	unsigned char *pkt;
	SSL3_BUFFER *rb;
	if (n <= 0) return n;
	rb    = &(s->s3->rbuf);
	left  = rb->left;
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
	align = (int)rb->buf + SSL3_RT_HEADER_LENGTH;
	align = (-align)&(SSL3_ALIGN_PAYLOAD-1);
#endif
	if (!extend)
		{
		if (left == 0)
			rb->offset = align;
		else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH)
			{
			pkt = rb->buf + rb->offset;
			if (pkt[0] == SSL3_RT_APPLICATION_DATA
			    && (pkt[3]<<8|pkt[4]) >= 128)
				{
				memmove (rb->buf+align,pkt,left);
				rb->offset = align;
				}
			}
		s->packet = rb->buf + rb->offset;
		s->packet_length = 0;
		}
	if ( SSL_version(s) == DTLS1_VERSION &&
		extend)
		{
		if ( left > 0 && n > left)
			n = left;
		}
	if (left >= n)
		{
		s->packet_length+=n;
		rb->left=left-n;
		rb->offset+=n;
		return(n);
		}
	len = s->packet_length;
	pkt = rb->buf+align;
	if (s->packet != pkt)
		{
		memmove(pkt, s->packet, len+left);
		s->packet = pkt;
		rb->offset = len + align;
		}
	max = rb->len - rb->offset;
	if (n > max)
		{
		SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR);
		return -1;
		}
	if (!s->read_ahead)
		max=n;
	while (left < n)
		{
		clear_sys_error();
		if (s->rbio != NULL)
			{
			s->rwstate=SSL_READING;
			i=BIO_read(s->rbio,pkt+len+left, max-left);
			}
		else
			{
			SSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET);
			i = -1;
			}
		if (i <= 0)
			{
			rb->left = left;
			return(i);
			}
		left+=i;
		}
	rb->offset += n;
	rb->left = left - n;
	s->packet_length += n;
	s->rwstate=SSL_NOTHING;
	return(n);
	}
['int dtls1_get_record(SSL *s)\n\t{\n\tint ssl_major,ssl_minor,al;\n\tint i,n;\n\tSSL3_RECORD *rr;\n\tSSL_SESSION *sess;\n\tunsigned char *p;\n\tshort version;\n\tDTLS1_BITMAP *bitmap;\n\tunsigned int is_next_epoch;\n\trr= &(s->s3->rrec);\n\tsess=s->session;\n\tif ( ! dtls1_process_buffered_records(s))\n            return 0;\n\tif (dtls1_get_processed_record(s))\n\t\treturn 1;\nagain:\n\tif (\t(s->rstate != SSL_ST_READ_BODY) ||\n\t\t(s->packet_length < DTLS1_RT_HEADER_LENGTH))\n\t\t{\n\t\tn=ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);\n\t\tif (n <= 0) return(n);\n\t\tOPENSSL_assert(s->packet_length == DTLS1_RT_HEADER_LENGTH);\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->epoch);\n\t\tmemcpy(&(s->s3->read_sequence[2]), p, 6);\n\t\tp+=6;\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_DTLS1_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 & 0xff00) != (DTLS1_VERSION & 0xff00))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_DTLS1_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-DTLS1_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\tif ( n != i)\n\t\t\t{\n\t\t\ts->packet_length = 0;\n\t\t\tgoto again;\n\t\t\t}\n\t\t}\n\ts->rstate=SSL_ST_READ_HEADER;\n\tbitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n\tif ( bitmap == NULL)\n\t\t{\n\t\ts->packet_length = 0;\n\t\tgoto again;\n\t\t}\n\tif ( ! dtls1_record_replay_check(s, bitmap))\n\t\t{\n\t\ts->packet_length=0;\n\t\tgoto again;\n\t\t}\n\tif (rr->length == 0) goto again;\n\tif (is_next_epoch)\n\t\t{\n\t\tdtls1_record_bitmap_update(s, bitmap);\n\t\tdtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num);\n\t\ts->packet_length = 0;\n\t\tgoto again;\n\t\t}\n\tif ( ! dtls1_process_record(s))\n\t\treturn(0);\n\tdtls1_clear_timeouts(s);\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(0);\n\t}', 'int ssl3_read_n(SSL *s, int n, int max, int extend)\n\t{\n\tint i,len,left,align=0;\n\tunsigned char *pkt;\n\tSSL3_BUFFER *rb;\n\tif (n <= 0) return n;\n\trb    = &(s->s3->rbuf);\n\tleft  = rb->left;\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0\n\talign = (int)rb->buf + SSL3_RT_HEADER_LENGTH;\n\talign = (-align)&(SSL3_ALIGN_PAYLOAD-1);\n#endif\n\tif (!extend)\n\t\t{\n\t\tif (left == 0)\n\t\t\trb->offset = align;\n\t\telse if (align != 0 && left >= SSL3_RT_HEADER_LENGTH)\n\t\t\t{\n\t\t\tpkt = rb->buf + rb->offset;\n\t\t\tif (pkt[0] == SSL3_RT_APPLICATION_DATA\n\t\t\t    && (pkt[3]<<8|pkt[4]) >= 128)\n\t\t\t\t{\n\t\t\t\tmemmove (rb->buf+align,pkt,left);\n\t\t\t\trb->offset = align;\n\t\t\t\t}\n\t\t\t}\n\t\ts->packet = rb->buf + rb->offset;\n\t\ts->packet_length = 0;\n\t\t}\n\tif ( SSL_version(s) == DTLS1_VERSION &&\n\t\textend)\n\t\t{\n\t\tif ( left > 0 && n > left)\n\t\t\tn = left;\n\t\t}\n\tif (left >= n)\n\t\t{\n\t\ts->packet_length+=n;\n\t\trb->left=left-n;\n\t\trb->offset+=n;\n\t\treturn(n);\n\t\t}\n\tlen = s->packet_length;\n\tpkt = rb->buf+align;\n\tif (s->packet != pkt)\n\t\t{\n\t\tmemmove(pkt, s->packet, len+left);\n\t\ts->packet = pkt;\n\t\trb->offset = len + align;\n\t\t}\n\tmax = rb->len - rb->offset;\n\tif (n > max)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR);\n\t\treturn -1;\n\t\t}\n\tif (!s->read_ahead)\n\t\tmax=n;\n\twhile (left < 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,pkt+len+left, max-left);\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\trb->left = left;\n\t\t\treturn(i);\n\t\t\t}\n\t\tleft+=i;\n\t\t}\n\trb->offset += n;\n\trb->left = left - n;\n\ts->packet_length += n;\n\ts->rwstate=SSL_NOTHING;\n\treturn(n);\n\t}']