shithub: drawterm-fdroid

Download patch

ref: cdb9a6e131c162f8c6f29b4fb131bb78b9ea7928
parent: 43866763e399e223a6e9b6f16e0c09bb16be19fa
author: cinap_lenrek <[email protected]>
date: Tue Apr 4 15:02:25 EDT 2017

tlshand: simplify tlsReadN() (from 9front)

--- a/libsec/tlshand.c
+++ b/libsec/tlshand.c
@@ -99,7 +99,7 @@
 	HandshakeHash	handhash;
 	Finished	finished;
 
-	uchar *sendp, *recvp, *recvw;
+	uchar *sendp;
 	uchar buf[1<<16];
 } TlsConnection;
 
@@ -690,7 +690,6 @@
 	c->trace = trace;
 	c->version = ProtocolVersion;
 	c->sendp = c->buf;
-	c->recvp = c->recvw = &c->buf[sizeof(c->buf)];
 
 	memset(&m, 0, sizeof(m));
 	if(!msgRecv(c, &m)){
@@ -995,7 +994,6 @@
 	c->trace = trace;
 	c->cert = nil;
 	c->sendp = c->buf;
-	c->recvp = c->recvw = &c->buf[sizeof(c->buf)];
 
 	c->version = ProtocolVersion;
 	tlsSecInitc(c->sec, c->version);
@@ -1263,7 +1261,7 @@
 	int n, i;
 
 	p = c->sendp;
-	e = c->recvp;
+	e = &c->buf[sizeof(c->buf)];
 	if(c->trace)
 		c->trace("send %s", msgPrint((char*)p, e - p, m));
 
@@ -1439,28 +1437,17 @@
 static uchar*
 tlsReadN(TlsConnection *c, int n)
 {
-	uchar *p, *e;
+	uchar *p, *w, *e;
 
-	p = c->recvp;
-	if(n <= c->recvw - p){
-		c->recvp += n;
-		return p;
-	}
 	e = &c->buf[sizeof(c->buf)];
-	c->recvp = e - n;
-	if(c->recvp < c->sendp || n > sizeof(c->buf)){
+	p = e - n;
+	if(n > sizeof(c->buf) || p < c->sendp){
 		tlsError(c, EDecodeError, "handshake message too long %d", n);
 		return nil;
 	}
-	memmove(c->recvp, p, c->recvw - p);
-	c->recvw -= p - c->recvp;
-	p = c->recvp;
-	c->recvp += n;
-	while(c->recvw < c->recvp){
-		if((n = read(c->hand, c->recvw, e - c->recvw)) <= 0)
+	for(w = p; w < e; w += n)
+		if((n = read(c->hand, w, e - w)) <= 0)
 			return nil;
-		c->recvw += n;
-	}
 	return p;
 }
 
@@ -1803,7 +1790,8 @@
 		goto Short;
 Ok:
 	if(c->trace)
-		c->trace("recv %s", msgPrint((char*)c->sendp, c->recvp - c->sendp, m));
+		c->trace("recv %s", msgPrint((char*)c->sendp, 
+			&c->buf[sizeof(c->buf)] - c->sendp, m));
 	return 1;
 Short:
 	tlsError(c, EDecodeError, "handshake message (%d) has invalid length", type);