libfreerdp-core: hook up mppc compressor to fastpath packets

This commit is contained in:
Jay Sorg 2012-03-20 10:03:36 -07:00
parent bd55533220
commit 7ecd7c5fdb
6 changed files with 358 additions and 292 deletions

View File

@ -461,6 +461,16 @@ int add_mppc_enc_suite(void)
return 0;
}
int get_random(int in_bytes)
{
int rv;
rv = rand() % in_bytes;
if (rv < 128)
rv = 128;
return rv;
}
#if DEBUG_MPPC_ENC_TEST
#define DLOG(_args) printf _args
#else
@ -498,7 +508,9 @@ void test_mppc_enc(void)
rdp.mppc->history_ptr = rdp.mppc->history_buf;
/* setup encoder for RDP 5.0 */
CU_ASSERT((enc = rdp_mppc_enc_new(PROTO_RDP_50)) != NULL);
CU_ASSERT((enc = mppc_enc_new(PROTO_RDP_50)) != NULL);
srand(time(0));
/* open image file with pixel data */
fd = open("nature.bmp", O_RDONLY);
@ -510,7 +522,7 @@ void test_mppc_enc(void)
gettimeofday(&start_time, NULL);
/* compress block, decompress it then compare with original data */
while ((bytes_read = read(fd, buf, BUF_SIZE)) > 0)
while ((bytes_read = read(fd, buf, get_random(BUF_SIZE))) > 0)
{
block_num++;
total += bytes_read;
@ -577,6 +589,6 @@ void test_mppc_enc(void)
close(fd);
}
rdp_mppc_enc_free(enc);
mppc_enc_free(enc);
free(rdp.mppc->history_buf);
}

View File

@ -27,8 +27,8 @@
#include "orders.h"
#include "update.h"
#include "surface.h"
#include "fastpath.h"
#include "mppc_enc.h"
/**
* Fast-Path packet format is defined in [MS-RDPBCGR] 2.2.9.1.2, which revises
@ -607,10 +607,17 @@ boolean fastpath_send_update_pdu(rdpFastPath* fastpath, uint8 updateCode, STREAM
{
rdpRdp* rdp;
uint8* bm;
uint8* ptr;
uint8* ptr_to_crypt;
uint8* ptr_sig;
int fragment;
int sec_bytes;
uint16 length;
int try_comp;
int comp_flags;
int header_bytes;
int cflags;
int pdu_data_bytes;
int dlen;
int bytes_to_crypt;
boolean result;
uint16 pduLength;
uint16 maxLength;
@ -618,63 +625,107 @@ boolean fastpath_send_update_pdu(rdpFastPath* fastpath, uint8 updateCode, STREAM
uint8 fragmentation;
uint8 header;
STREAM* update;
STREAM* comp_update;
STREAM* ls;
result = true;
rdp = fastpath->rdp;
sec_bytes = fastpath_get_sec_bytes(rdp);
maxLength = FASTPATH_MAX_PACKET_SIZE - 6 - sec_bytes;
totalLength = stream_get_length(s) - 6 - sec_bytes;
stream_set_pos(s, 0);
update = stream_new(0);
try_comp = rdp->settings->compression;
comp_flags = 0;
comp_update = stream_new(0);
for (fragment = 0; totalLength > 0; fragment++)
{
length = MIN(maxLength, totalLength);
totalLength -= length;
pduLength = length + 6 + sec_bytes;
ls = s;
dlen = MIN(maxLength, totalLength);
cflags = 0;
header_bytes = 6 + sec_bytes;
pdu_data_bytes = dlen;
if (try_comp)
{
if (compress_rdp(rdp->mppc_enc, ls->p + header_bytes, dlen))
{
if (rdp->mppc_enc->flags & PACKET_COMPRESSED)
{
cflags = rdp->mppc_enc->flags;
pdu_data_bytes = rdp->mppc_enc->bytes_in_opb;
comp_flags = FASTPATH_OUTPUT_COMPRESSION_USED;
header_bytes = 7 + sec_bytes;
bm = (uint8*) (rdp->mppc_enc->outputBuffer - header_bytes);
stream_attach(comp_update, bm, pdu_data_bytes + header_bytes);
ls = comp_update;
}
}
else
printf("fastpath_send_update_pdu: mppc_encode failed\n");
}
totalLength -= dlen;
pduLength = pdu_data_bytes + header_bytes;
if (totalLength == 0)
fragmentation = (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
else
fragmentation = (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
stream_get_mark(s, bm);
stream_get_mark(ls, bm);
header = 0;
if (sec_bytes > 0)
header |= (FASTPATH_OUTPUT_ENCRYPTED << 6);
stream_write_uint8(s, header); /* fpOutputHeader (1 byte) */
stream_write_uint8(s, 0x80 | (pduLength >> 8)); /* length1 */
stream_write_uint8(s, pduLength & 0xFF); /* length2 */
stream_write_uint8(ls, header); /* fpOutputHeader (1 byte) */
stream_write_uint8(ls, 0x80 | (pduLength >> 8)); /* length1 */
stream_write_uint8(ls, pduLength & 0xFF); /* length2 */
if (sec_bytes > 0)
stream_seek(s, sec_bytes);
fastpath_write_update_header(s, updateCode, fragmentation, 0);
stream_write_uint16(s, length);
stream_seek(ls, sec_bytes);
fastpath_write_update_header(ls, updateCode, fragmentation, comp_flags);
/* extra byte if compressed */
if (ls == comp_update)
{
stream_write_uint8(ls, cflags);
bytes_to_crypt = pdu_data_bytes + 4;
}
else
bytes_to_crypt = pdu_data_bytes + 3;
stream_write_uint16(ls, pdu_data_bytes);
stream_attach(update, bm, pduLength);
stream_seek(update, pduLength);
if (sec_bytes > 0)
{
ptr = bm + 3 + sec_bytes;
/* does this work ? */
ptr_to_crypt = bm + 3 + sec_bytes;
ptr_sig = bm + 3;
if (rdp->sec_flags & SEC_SECURE_CHECKSUM)
security_salted_mac_signature(rdp, ptr, length + 3, true, bm + 3);
security_salted_mac_signature(rdp, ptr_to_crypt, bytes_to_crypt, true, ptr_sig);
else
security_mac_signature(rdp, ptr, length + 3, bm + 3);
security_encrypt(ptr, length + 3, rdp);
security_mac_signature(rdp, ptr_to_crypt, bytes_to_crypt, ptr_sig);
security_encrypt(ptr_to_crypt, bytes_to_crypt, rdp);
}
if (transport_write(fastpath->rdp->transport, update) < 0)
{
stream_detach(update);
result = false;
break;
}
stream_detach(update);
/* Reserve 6 + sec_bytes bytes for the next fragment header, if any. */
stream_seek(s, length - 6 - sec_bytes);
stream_seek(s, dlen - header_bytes);
}
stream_detach(update);
stream_detach(comp_update);
stream_free(update);
stream_free(comp_update);
return result;
}

View File

@ -378,7 +378,7 @@ do \
* @return struct rdp_mppc_enc* or nil on failure
*/
struct rdp_mppc_enc* rdp_mppc_enc_new(int protocol_type)
struct rdp_mppc_enc* mppc_enc_new(int protocol_type)
{
struct rdp_mppc_enc* enc;
@ -423,7 +423,7 @@ struct rdp_mppc_enc* rdp_mppc_enc_new(int protocol_type)
* @param enc struct to be deinited
*/
void rdp_mppc_enc_free(struct rdp_mppc_enc* enc)
void mppc_enc_free(struct rdp_mppc_enc* enc)
{
if (enc == NULL)
return;
@ -506,6 +506,7 @@ boolean compress_rdp_5(struct rdp_mppc_enc* enc, uint8* srcData, int len)
uint32 x;
uint8 data;
uint16 data16;
uint32 historyOffset;
opb_index = 0;
bits_left = 8;
@ -527,9 +528,11 @@ boolean compress_rdp_5(struct rdp_mppc_enc* enc, uint8* srcData, int len)
enc->flagsHold |= PACKET_AT_FRONT;
}
historyOffset = enc->historyOffset;
/* add / append new data to historyBuffer */
memcpy(&enc->historyBuffer[enc->historyOffset], srcData, len);
historyPointer = &enc->historyBuffer[enc->historyOffset];
memcpy(&(enc->historyBuffer[enc->historyOffset]), srcData, len);
historyPointer = &(enc->historyBuffer[enc->historyOffset]);
/* if we are at start of history buffer, do not attempt to compress */
/* first 2 bytes,because minimum LoM is 3 */
@ -556,7 +559,7 @@ boolean compress_rdp_5(struct rdp_mppc_enc* enc, uint8* srcData, int len)
enc->historyOffset += len;
/* point to last byte in new data */
hptr_end = &enc->historyBuffer[enc->historyOffset - 1];
hptr_end = &(enc->historyBuffer[enc->historyOffset - 1]);
while (historyPointer <= hptr_end)
{
@ -566,7 +569,9 @@ boolean compress_rdp_5(struct rdp_mppc_enc* enc, uint8* srcData, int len)
cptr1 = hbuf_start;
lom = 0;
saved_lom = 0;
keep_looking:
while (1)
{
/* look for first byte match */
while ((cptr1 <= index_ptr) && (*cptr1 != *historyPointer))
{
@ -575,12 +580,7 @@ keep_looking:
if (cptr1 > index_ptr)
{
/* no match found */
if (saved_lom)
{
goto insert_tuple;
}
goto insert_literal;
break;
}
/* got a one byte match - continue looking */
@ -600,23 +600,22 @@ keep_looking:
{
/* false alarm, keep looking */
lom = 0;
goto keep_looking;
}
/* we got a match */
if (lom < saved_lom)
else
{
/* too small, ignore this match */
goto keep_looking;
}
else if (lom >= saved_lom)
if (lom >= saved_lom)
{
/* this is a longer match, or a match closer to historyPointer */
saved_lom = lom;
copy_offset = (historyPointer - cptr1) + 1;
goto keep_looking;
}
insert_tuple:
}
}
if (saved_lom)
{
/* insert_tuple */
DLOG(("<%d: %ld,%d> ", (int) (historyPointer - enc->historyBuffer),
(long) copy_offset, saved_lom));
lom = saved_lom;
@ -824,8 +823,11 @@ insert_tuple:
data16 = lom - 32768;
insert_15_bits(data16);
}
goto check_len;
insert_literal:
}
else
{
/* insert_literal */
/* data not found in historyBuffer; encode and place data in output buffer */
data = *(historyPointer++);
DLOG(("%.2x ", (unsigned char) data));
@ -841,17 +843,22 @@ insert_literal:
/* insert literal */
insert_8_bits(data);
}
check_len:
}
/* if bits_left == 8, opb_index has already been incremented */
if ((bits_left == 8) && (opb_index > len))
{
/* compressed data longer than uncompressed data */
goto give_up;
/* give up */
enc->historyOffset = historyOffset;
return true;
}
else if (opb_index + 1 > len)
{
/* compressed data longer than uncompressed data */
goto give_up;
/* give up */
enc->historyOffset = historyOffset;
return true;
}
} /* end while (historyPointer < hptr_end) */
@ -863,7 +870,9 @@ check_len:
if (opb_index > len)
{
goto give_up;
/* give up */
enc->historyOffset = historyOffset;
return true;
}
enc->flags |= PACKET_COMPRESSED;
enc->bytes_in_opb = opb_index;
@ -871,16 +880,6 @@ check_len:
enc->flags |= enc->flagsHold;
enc->flagsHold = 0;
DLOG(("\n"));
return true;
give_up:
enc->flagsHold |= PACKET_FLUSHED;
enc->bytes_in_opb = 0;
memset(enc->historyBuffer, 0, enc->buf_len);
memcpy(enc->historyBuffer, srcData, len);
enc->historyOffset = len;
DLOG(("\n"));
return true;
}

View File

@ -44,7 +44,7 @@ struct rdp_mppc_enc
boolean compress_rdp(struct rdp_mppc_enc* enc, uint8* srcData, int len);
boolean compress_rdp_4(struct rdp_mppc_enc* enc, uint8* srcData, int len);
boolean compress_rdp_5(struct rdp_mppc_enc* enc, uint8* srcData, int len);
struct rdp_mppc_enc* rdp_mppc_enc_new(int protocol_type);
void rdp_mppc_enc_free(struct rdp_mppc_enc* enc);
struct rdp_mppc_enc* mppc_enc_new(int protocol_type);
void mppc_enc_free(struct rdp_mppc_enc* enc);
#endif

View File

@ -21,6 +21,7 @@
#include "info.h"
#include "redirection.h"
#include "mppc_enc.h"
#include <freerdp/crypto/per.h>
@ -922,6 +923,7 @@ rdpRdp* rdp_new(freerdp* instance)
rdp->mcs = mcs_new(rdp->transport);
rdp->redirection = redirection_new();
rdp->mppc = mppc_new(rdp);
rdp->mppc_enc = mppc_enc_new(PROTO_RDP_50);
}
return rdp;
@ -952,6 +954,7 @@ void rdp_free(rdpRdp* rdp)
mcs_free(rdp->mcs);
redirection_free(rdp->redirection);
mppc_free(rdp);
mppc_enc_free(rdp->mppc_enc);
xfree(rdp);
}
}

View File

@ -135,6 +135,7 @@ struct rdp_rdp
struct rdp_transport* transport;
struct rdp_extension* extension;
struct rdp_mppc* mppc;
struct rdp_mppc_enc* mppc_enc;
struct crypto_rc4_struct* rc4_decrypt_key;
int decrypt_use_count;
int decrypt_checksum_use_count;