2
* FreeRDP: A Remote Desktop Protocol Client
5
* Copyright 2011 Vic Lee
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
23
#include <freerdp/api.h>
24
#include <freerdp/utils/stream.h>
33
* Fast-Path packet format is defined in [MS-RDPBCGR] 2.2.9.1.2, which revises
34
* server output packets from the first byte with the goal of improving
37
* Slow-Path packet always starts with TPKT header, which has the first
38
* byte 0x03, while Fast-Path packet starts with 2 zero bits in the first
39
* two less significant bits of the first byte.
42
#define FASTPATH_MAX_PACKET_SIZE 0x3FFF
45
* Read a Fast-Path packet header.\n
47
* @param encryptionFlags
51
uint16 fastpath_read_header(rdpFastPath* fastpath, STREAM* s)
57
stream_read_uint8(s, header);
61
fastpath->encryptionFlags = (header & 0xC0) >> 6;
62
fastpath->numberEvents = (header & 0x3C) >> 2;
65
stream_read_uint8(s, length); /* length1 */
66
/* If most significant bit is not set, length2 is not presented. */
71
stream_read_uint8(s, t);
78
INLINE void fastpath_read_update_header(STREAM* s, uint8* updateCode, uint8* fragmentation, uint8* compression)
82
stream_read_uint8(s, updateHeader);
83
*updateCode = updateHeader & 0x0F;
84
*fragmentation = (updateHeader >> 4) & 0x03;
85
*compression = (updateHeader >> 6) & 0x03;
88
INLINE void fastpath_write_update_header(STREAM* s, uint8 updateCode, uint8 fragmentation, uint8 compression)
90
uint8 updateHeader = 0;
92
updateHeader |= updateCode & 0x0F;
93
updateHeader |= (fragmentation & 0x03) << 4;
94
updateHeader |= (compression & 0x03) << 6;
95
stream_write_uint8(s, updateHeader);
98
uint16 fastpath_read_header_rdp(rdpFastPath* fastpath, STREAM* s)
106
stream_read_uint8(s, header);
108
if (fastpath != NULL)
110
fastpath->encryptionFlags = (header & 0xC0) >> 6;
111
fastpath->numberEvents = (header & 0x3C) >> 2;
114
stream_read_uint8(s, length); /* length1 */
115
/* If most significant bit is not set, length2 is not presented. */
121
stream_read_uint8(s, t);
128
static void fastpath_recv_orders(rdpFastPath* fastpath, STREAM* s)
130
rdpUpdate* update = fastpath->rdp->update;
133
stream_read_uint16(s, numberOrders); /* numberOrders (2 bytes) */
135
while (numberOrders > 0)
137
update_recv_order(update, s);
142
static void fastpath_recv_update_common(rdpFastPath* fastpath, STREAM* s)
145
rdpUpdate* update = fastpath->rdp->update;
146
rdpContext* context = update->context;
148
stream_read_uint16(s, updateType); /* updateType (2 bytes) */
152
case UPDATE_TYPE_BITMAP:
153
update_read_bitmap(update, s, &update->bitmap_update);
154
IFCALL(update->BitmapUpdate, context, &update->bitmap_update);
157
case UPDATE_TYPE_PALETTE:
158
update_read_palette(update, s, &update->palette_update);
159
IFCALL(update->Palette, context, &update->palette_update);
164
static void fastpath_recv_update_synchronize(rdpFastPath* fastpath, STREAM* s)
166
stream_seek_uint16(s); /* size (2 bytes), must be set to zero */
169
static void fastpath_recv_update(rdpFastPath* fastpath, uint8 updateCode, uint32 size, STREAM* s)
171
rdpUpdate* update = fastpath->rdp->update;
172
rdpContext* context = fastpath->rdp->update->context;
173
rdpPointerUpdate* pointer = update->pointer;
177
case FASTPATH_UPDATETYPE_ORDERS:
178
fastpath_recv_orders(fastpath, s);
181
case FASTPATH_UPDATETYPE_BITMAP:
182
case FASTPATH_UPDATETYPE_PALETTE:
183
fastpath_recv_update_common(fastpath, s);
186
case FASTPATH_UPDATETYPE_SYNCHRONIZE:
187
fastpath_recv_update_synchronize(fastpath, s);
188
IFCALL(update->Synchronize, context);
191
case FASTPATH_UPDATETYPE_SURFCMDS:
192
update_recv_surfcmds(update, size, s);
195
case FASTPATH_UPDATETYPE_PTR_NULL:
196
pointer->pointer_system.type = SYSPTR_NULL;
197
IFCALL(pointer->PointerSystem, context, &pointer->pointer_system);
200
case FASTPATH_UPDATETYPE_PTR_DEFAULT:
201
update->pointer->pointer_system.type = SYSPTR_DEFAULT;
202
IFCALL(pointer->PointerSystem, context, &pointer->pointer_system);
205
case FASTPATH_UPDATETYPE_PTR_POSITION:
206
update_read_pointer_position(s, &pointer->pointer_position);
207
IFCALL(pointer->PointerPosition, context, &pointer->pointer_position);
210
case FASTPATH_UPDATETYPE_COLOR:
211
update_read_pointer_color(s, &pointer->pointer_color);
212
IFCALL(pointer->PointerColor, context, &pointer->pointer_color);
215
case FASTPATH_UPDATETYPE_CACHED:
216
update_read_pointer_cached(s, &pointer->pointer_cached);
217
IFCALL(pointer->PointerCached, context, &pointer->pointer_cached);
220
case FASTPATH_UPDATETYPE_POINTER:
221
update_read_pointer_new(s, &pointer->pointer_new);
222
IFCALL(pointer->PointerNew, context, &pointer->pointer_new);
226
DEBUG_WARN("unknown updateCode 0x%X", updateCode);
231
static void fastpath_recv_update_data(rdpFastPath* fastpath, STREAM* s)
239
uint8 compressionFlags;
240
STREAM* update_stream;
248
fastpath_read_update_header(s, &updateCode, &fragmentation, &compression);
250
if (compression == FASTPATH_OUTPUT_COMPRESSION_USED)
251
stream_read_uint8(s, compressionFlags);
253
compressionFlags = 0;
255
stream_read_uint16(s, size);
256
next_pos = stream_get_pos(s) + size;
259
if (compressionFlags & PACKET_COMPRESSED)
261
if (decompress_rdp(rdp, s->p, size, compressionFlags, &roff, &rlen))
263
comp_stream = stream_new(0);
264
comp_stream->data = rdp->mppc->history_buf + roff;
265
comp_stream->p = comp_stream->data;
266
comp_stream->size = rlen;
267
size = comp_stream->size;
271
printf("decompress_rdp() failed\n");
272
stream_seek(s, size);
276
update_stream = NULL;
277
if (fragmentation == FASTPATH_FRAGMENT_SINGLE)
280
update_stream = comp_stream;
284
if (fragmentation == FASTPATH_FRAGMENT_FIRST)
285
stream_set_pos(fastpath->updateData, 0);
287
stream_check_size(fastpath->updateData, size);
288
stream_copy(fastpath->updateData, comp_stream, size);
290
if (fragmentation == FASTPATH_FRAGMENT_LAST)
292
update_stream = fastpath->updateData;
293
totalSize = stream_get_length(update_stream);
294
stream_set_pos(update_stream, 0);
299
fastpath_recv_update(fastpath, updateCode, totalSize, update_stream);
301
stream_set_pos(s, next_pos);
303
if (comp_stream != s)
307
boolean fastpath_recv_updates(rdpFastPath* fastpath, STREAM* s)
309
rdpUpdate* update = fastpath->rdp->update;
311
IFCALL(update->BeginPaint, update->context);
313
while (stream_get_left(s) >= 3)
315
fastpath_recv_update_data(fastpath, s);
318
IFCALL(update->EndPaint, update->context);
323
static boolean fastpath_read_input_event_header(STREAM* s, uint8* eventFlags, uint8* eventCode)
327
if (stream_get_left(s) < 1)
330
stream_read_uint8(s, eventHeader); /* eventHeader (1 byte) */
332
*eventFlags = (eventHeader & 0x1F);
333
*eventCode = (eventHeader >> 5);
338
static boolean fastpath_recv_input_event_scancode(rdpFastPath* fastpath, STREAM* s, uint8 eventFlags)
343
if (stream_get_left(s) < 1)
346
stream_read_uint8(s, code); /* keyCode (1 byte) */
349
if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
350
flags |= KBD_FLAGS_RELEASE;
352
flags |= KBD_FLAGS_DOWN;
354
if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
355
flags |= KBD_FLAGS_EXTENDED;
357
IFCALL(fastpath->rdp->input->KeyboardEvent, fastpath->rdp->input, flags, code);
362
static boolean fastpath_recv_input_event_mouse(rdpFastPath* fastpath, STREAM* s, uint8 eventFlags)
368
if (stream_get_left(s) < 6)
371
stream_read_uint16(s, pointerFlags); /* pointerFlags (2 bytes) */
372
stream_read_uint16(s, xPos); /* xPos (2 bytes) */
373
stream_read_uint16(s, yPos); /* yPos (2 bytes) */
375
IFCALL(fastpath->rdp->input->MouseEvent, fastpath->rdp->input, pointerFlags, xPos, yPos);
380
static boolean fastpath_recv_input_event_mousex(rdpFastPath* fastpath, STREAM* s, uint8 eventFlags)
386
if (stream_get_left(s) < 6)
389
stream_read_uint16(s, pointerFlags); /* pointerFlags (2 bytes) */
390
stream_read_uint16(s, xPos); /* xPos (2 bytes) */
391
stream_read_uint16(s, yPos); /* yPos (2 bytes) */
393
IFCALL(fastpath->rdp->input->ExtendedMouseEvent, fastpath->rdp->input, pointerFlags, xPos, yPos);
398
static boolean fastpath_recv_input_event_sync(rdpFastPath* fastpath, STREAM* s, uint8 eventFlags)
400
IFCALL(fastpath->rdp->input->SynchronizeEvent, fastpath->rdp->input, eventFlags);
405
static boolean fastpath_recv_input_event_unicode(rdpFastPath* fastpath, STREAM* s, uint8 eventFlags)
409
if (stream_get_left(s) < 2)
412
stream_read_uint16(s, unicodeCode); /* unicodeCode (2 bytes) */
414
IFCALL(fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input, unicodeCode);
419
static boolean fastpath_recv_input_event(rdpFastPath* fastpath, STREAM* s)
424
if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
429
case FASTPATH_INPUT_EVENT_SCANCODE:
430
if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
434
case FASTPATH_INPUT_EVENT_MOUSE:
435
if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
439
case FASTPATH_INPUT_EVENT_MOUSEX:
440
if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
444
case FASTPATH_INPUT_EVENT_SYNC:
445
if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
449
case FASTPATH_INPUT_EVENT_UNICODE:
450
if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
455
printf("Unknown eventCode %d\n", eventCode);
462
boolean fastpath_recv_inputs(rdpFastPath* fastpath, STREAM* s)
466
if (fastpath->numberEvents == 0)
469
* If numberEvents is not provided in fpInputHeader, it will be provided
470
* as one additional byte here.
473
if (stream_get_left(s) < 1)
476
stream_read_uint8(s, fastpath->numberEvents); /* eventHeader (1 byte) */
479
for (i = 0; i < fastpath->numberEvents; i++)
481
if (!fastpath_recv_input_event(fastpath, s))
488
STREAM* fastpath_input_pdu_init(rdpFastPath* fastpath, uint8 eventFlags, uint8 eventCode)
491
s = transport_send_stream_init(fastpath->rdp->transport, 127);
492
stream_seek(s, 2); /* fpInputHeader and length1 */
493
/* length2 is not necessary since input PDU should not exceed 127 bytes */
494
stream_write_uint8(s, eventFlags | (eventCode << 5)); /* eventHeader (1 byte) */
498
boolean fastpath_send_input_pdu(rdpFastPath* fastpath, STREAM* s)
502
length = stream_get_length(s);
505
printf("Maximum FastPath PDU length is 127\n");
509
stream_set_pos(s, 0);
510
stream_write_uint8(s, (1 << 2));
511
stream_write_uint8(s, length);
513
stream_set_pos(s, length);
514
if (transport_write(fastpath->rdp->transport, s) < 0)
520
STREAM* fastpath_update_pdu_init(rdpFastPath* fastpath)
523
s = transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
524
stream_seek(s, 3); /* fpOutputHeader, length1 and length2 */
525
stream_seek(s, 3); /* updateHeader, size */
529
boolean fastpath_send_update_pdu(rdpFastPath* fastpath, uint8 updateCode, STREAM* s)
543
maxLength = FASTPATH_MAX_PACKET_SIZE - 6;
544
totalLength = stream_get_length(s) - 6;
545
stream_set_pos(s, 0);
546
update = stream_new(0);
548
for (fragment = 0; totalLength > 0; fragment++)
550
length = MIN(maxLength, totalLength);
551
totalLength -= length;
552
pduLength = length + 6;
554
if (totalLength == 0)
555
fragmentation = (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
557
fragmentation = (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
559
stream_get_mark(s, bm);
560
stream_write_uint8(s, 0); /* fpOutputHeader (1 byte) */
561
stream_write_uint8(s, 0x80 | (pduLength >> 8)); /* length1 */
562
stream_write_uint8(s, pduLength & 0xFF); /* length2 */
563
fastpath_write_update_header(s, updateCode, fragmentation, 0);
564
stream_write_uint16(s, length);
566
stream_attach(update, bm, pduLength);
567
stream_seek(update, pduLength);
568
if (transport_write(fastpath->rdp->transport, update) < 0)
570
stream_detach(update);
574
stream_detach(update);
576
/* Reserve 6 bytes for the next fragment header, if any. */
577
stream_seek(s, length - 6);
585
rdpFastPath* fastpath_new(rdpRdp* rdp)
587
rdpFastPath* fastpath;
589
fastpath = xnew(rdpFastPath);
591
fastpath->updateData = stream_new(4096);
596
void fastpath_free(rdpFastPath* fastpath)
598
stream_free(fastpath->updateData);