Moved some repeated "read all" code into function.
@@ -75,57 +75,61 @@ static BOOL rdg_write_packet(rdpRdg* rdg, wStream* sPacket)
return TRUE;
}
-static wStream* rdg_receive_packet(rdpRdg* rdg)
+static BOOL rdg_read_all(rdpTls* tls, BYTE* buffer, int size)
{
int status;
- wStream* s;
- RdgPacketHeader* packet;
- UINT32 readCount = 0;
- s = Stream_New(NULL, 1024);
+ int readCount = 0;
- if (!s)
- return NULL;
-
- packet = (RdgPacketHeader*) Stream_Buffer(s);
-
- while (readCount < sizeof(RdgPacketHeader))
+ while (readCount < size)
{
- status = BIO_read(rdg->tlsOut->bio, Stream_Pointer(s), sizeof(RdgPacketHeader) - readCount);
+ status = BIO_read(tls->bio, buffer, size - readCount);
- if (status < 0)
+ if (status <= 0)
{
+ if (!BIO_should_retry(tls->bio))
+ return FALSE;
+
continue;
}
readCount += status;
- Stream_Seek(s, readCount);
+ }
+ return TRUE;
}
- if (Stream_Capacity(s) < packet->packetLength)
+static wStream* rdg_receive_packet(rdpRdg* rdg)
{
- if (!Stream_EnsureCapacity(s, packet->packetLength))
+ wStream* s;
+ size_t packetLength;
+
+ s = Stream_New(NULL, 1024);
+
+ if (!s)
+ return NULL;
+
+ if (!rdg_read_all(rdg->tlsOut, Stream_Buffer(s), sizeof(RdgPacketHeader)))
{
Stream_Free(s, TRUE);
return NULL;
}
- packet = (RdgPacketHeader*) Stream_Buffer(s);
- }
+ packetLength = ((RdgPacketHeader*)Stream_Buffer(s))->packetLength;
- while (readCount < packet->packetLength)
+ if (!Stream_EnsureCapacity(s, packetLength))
{
- status = BIO_read(rdg->tlsOut->bio, Stream_Pointer(s), packet->packetLength - readCount);
+ Stream_Free(s, TRUE);
+ return NULL;
+ }
- if (status < 0)
+ if (!rdg_read_all(rdg->tlsOut, Stream_Buffer(s) + sizeof(RdgPacketHeader),
+ packetLength - sizeof(RdgPacketHeader)))
{
- continue;
+ Stream_Free(s, TRUE);
+ return NULL;
}
- readCount += status;
- Stream_Seek(s, readCount);
- }
+ Stream_SetLength(s, packetLength);
- Stream_SealLength(s);
return s;
}
