1 |
/* |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
rdesktop: A Remote Desktop Protocol client. |
3 |
RDP order processing |
RDP order processing |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
Copyright (C) Matthew Chapman 1999-2002 |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
21 |
#include "rdesktop.h" |
#include "rdesktop.h" |
22 |
#include "orders.h" |
#include "orders.h" |
23 |
|
|
24 |
extern uint8 *next_packet; |
extern uint8 *g_next_packet; |
25 |
static RDP_ORDER_STATE order_state; |
static RDP_ORDER_STATE g_order_state; |
26 |
|
extern BOOL g_use_rdp5; |
27 |
|
|
28 |
/* Read field indicating which parameters are present */ |
/* Read field indicating which parameters are present */ |
29 |
static void |
static void |
55 |
|
|
56 |
/* Read a co-ordinate (16-bit, or 8-bit delta) */ |
/* Read a co-ordinate (16-bit, or 8-bit delta) */ |
57 |
static void |
static void |
58 |
rdp_in_coord(STREAM s, uint16 * coord, BOOL delta) |
rdp_in_coord(STREAM s, sint16 * coord, BOOL delta) |
59 |
{ |
{ |
60 |
uint8 change; |
sint8 change; |
61 |
|
|
62 |
if (delta) |
if (delta) |
63 |
{ |
{ |
64 |
in_uint8(s, change); |
in_uint8(s, change); |
65 |
*coord += (signed char) change; |
*coord += change; |
66 |
} |
} |
67 |
else |
else |
68 |
{ |
{ |
72 |
|
|
73 |
/* Read a colour entry */ |
/* Read a colour entry */ |
74 |
static void |
static void |
75 |
rdp_in_colour(STREAM s, uint8 * colour) |
rdp_in_colour(STREAM s, uint32 * colour) |
76 |
{ |
{ |
77 |
in_uint8(s, *colour); |
uint32 i; |
78 |
s->p += 2; |
in_uint8(s, i); |
79 |
|
*colour = i; |
80 |
|
in_uint8(s, i); |
81 |
|
*colour |= i << 8; |
82 |
|
in_uint8(s, i); |
83 |
|
*colour |= i << 16; |
84 |
} |
} |
85 |
|
|
86 |
/* Parse bounds information */ |
/* Parse bounds information */ |
269 |
|
|
270 |
rdp_parse_pen(s, &os->pen, present >> 7); |
rdp_parse_pen(s, &os->pen, present >> 7); |
271 |
|
|
272 |
DEBUG(("LINE(op=0x%x,sx=%d,sy=%d,dx=%d,dx=%d,fg=0x%x)\n", |
DEBUG(("LINE(op=0x%x,sx=%d,sy=%d,dx=%d,dy=%d,fg=0x%x)\n", |
273 |
os->opcode, os->startx, os->starty, os->endx, os->endy, os->pen.colour)); |
os->opcode, os->startx, os->starty, os->endx, os->endy, os->pen.colour)); |
274 |
|
|
275 |
if (os->opcode < 0x01 || os->opcode > 0x10) |
if (os->opcode < 0x01 || os->opcode > 0x10) |
285 |
static void |
static void |
286 |
process_rect(STREAM s, RECT_ORDER * os, uint32 present, BOOL delta) |
process_rect(STREAM s, RECT_ORDER * os, uint32 present, BOOL delta) |
287 |
{ |
{ |
288 |
|
uint32 i; |
289 |
if (present & 0x01) |
if (present & 0x01) |
290 |
rdp_in_coord(s, &os->x, delta); |
rdp_in_coord(s, &os->x, delta); |
291 |
|
|
299 |
rdp_in_coord(s, &os->cy, delta); |
rdp_in_coord(s, &os->cy, delta); |
300 |
|
|
301 |
if (present & 0x10) |
if (present & 0x10) |
302 |
in_uint8(s, os->colour); |
{ |
303 |
|
in_uint8(s, i); |
304 |
|
os->colour = (os->colour & 0xffffff00) | i; |
305 |
|
} |
306 |
|
|
307 |
|
if (present & 0x20) |
308 |
|
{ |
309 |
|
in_uint8(s, i); |
310 |
|
os->colour = (os->colour & 0xffff00ff) | (i << 8); |
311 |
|
} |
312 |
|
|
313 |
|
if (present & 0x40) |
314 |
|
{ |
315 |
|
in_uint8(s, i); |
316 |
|
os->colour = (os->colour & 0xff00ffff) | (i << 16); |
317 |
|
} |
318 |
|
|
319 |
DEBUG(("RECT(x=%d,y=%d,cx=%d,cy=%d,fg=0x%x)\n", os->x, os->y, os->cx, os->cy, os->colour)); |
DEBUG(("RECT(x=%d,y=%d,cx=%d,cy=%d,fg=0x%x)\n", os->x, os->y, os->cx, os->cy, os->colour)); |
320 |
|
|
606 |
if (present & 0x002000) |
if (present & 0x002000) |
607 |
in_uint16_le(s, os->boxbottom); |
in_uint16_le(s, os->boxbottom); |
608 |
|
|
609 |
|
if (present & 0x004000) /* fix for connecting to a server that */ |
610 |
|
in_uint8s(s, 10); /* was disconnected with mstsc.exe */ |
611 |
|
/* 0x008000, 0x020000, and 0x040000 are present too ??? */ |
612 |
|
|
613 |
if (present & 0x080000) |
if (present & 0x080000) |
614 |
in_uint16_le(s, os->x); |
in_uint16_le(s, os->x); |
615 |
|
|
646 |
{ |
{ |
647 |
HBITMAP bitmap; |
HBITMAP bitmap; |
648 |
uint16 cache_idx, bufsize; |
uint16 cache_idx, bufsize; |
649 |
uint8 cache_id, width, height, bpp; |
uint8 cache_id, width, height, bpp, Bpp; |
650 |
uint8 *data, *inverted; |
uint8 *data, *inverted; |
651 |
int y; |
int y; |
652 |
|
|
655 |
in_uint8(s, width); |
in_uint8(s, width); |
656 |
in_uint8(s, height); |
in_uint8(s, height); |
657 |
in_uint8(s, bpp); |
in_uint8(s, bpp); |
658 |
|
Bpp = (bpp + 7) / 8; |
659 |
in_uint16_le(s, bufsize); |
in_uint16_le(s, bufsize); |
660 |
in_uint16_le(s, cache_idx); |
in_uint16_le(s, cache_idx); |
661 |
in_uint8p(s, data, bufsize); |
in_uint8p(s, data, bufsize); |
662 |
|
|
663 |
DEBUG(("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx)); |
DEBUG(("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx)); |
664 |
inverted = xmalloc(width * height); |
inverted = (uint8 *) xmalloc(width * height * Bpp); |
665 |
for (y = 0; y < height; y++) |
for (y = 0; y < height; y++) |
666 |
{ |
{ |
667 |
memcpy(&inverted[(height - y - 1) * width], &data[y * width], width); |
memcpy(&inverted[(height - y - 1) * (width * Bpp)], &data[y * (width * Bpp)], |
668 |
|
width * Bpp); |
669 |
} |
} |
670 |
|
|
671 |
bitmap = ui_create_bitmap(width, height, inverted); |
bitmap = ui_create_bitmap(width, height, inverted); |
672 |
xfree(inverted); |
xfree(inverted); |
673 |
cache_put_bitmap(cache_id, cache_idx, bitmap); |
cache_put_bitmap(cache_id, cache_idx, bitmap, 0); |
674 |
} |
} |
675 |
|
|
676 |
/* Process a bitmap cache order */ |
/* Process a bitmap cache order */ |
679 |
{ |
{ |
680 |
HBITMAP bitmap; |
HBITMAP bitmap; |
681 |
uint16 cache_idx, size; |
uint16 cache_idx, size; |
682 |
uint8 cache_id, width, height, bpp; |
uint8 cache_id, width, height, bpp, Bpp; |
683 |
uint8 *data, *bmpdata; |
uint8 *data, *bmpdata; |
684 |
|
uint16 bufsize, pad2, row_size, final_size; |
685 |
|
uint8 pad1; |
686 |
|
|
687 |
|
pad2 = row_size = final_size = 0xffff; /* Shut the compiler up */ |
688 |
|
|
689 |
in_uint8(s, cache_id); |
in_uint8(s, cache_id); |
690 |
in_uint8s(s, 1); /* pad */ |
in_uint8(s, pad1); /* pad */ |
691 |
in_uint8(s, width); |
in_uint8(s, width); |
692 |
in_uint8(s, height); |
in_uint8(s, height); |
693 |
in_uint8(s, bpp); |
in_uint8(s, bpp); |
694 |
in_uint8s(s, 2); /* bufsize */ |
Bpp = (bpp + 7) / 8; |
695 |
|
in_uint16_le(s, bufsize); /* bufsize */ |
696 |
in_uint16_le(s, cache_idx); |
in_uint16_le(s, cache_idx); |
697 |
in_uint8s(s, 2); /* pad */ |
|
698 |
in_uint16_le(s, size); |
if (g_use_rdp5) |
699 |
in_uint8s(s, 4); /* row_size, final_size */ |
{ |
700 |
|
size = bufsize; |
701 |
|
} |
702 |
|
else |
703 |
|
{ |
704 |
|
|
705 |
|
/* Begin compressedBitmapData */ |
706 |
|
in_uint16_le(s, pad2); /* pad */ |
707 |
|
in_uint16_le(s, size); |
708 |
|
/* in_uint8s(s, 4); *//* row_size, final_size */ |
709 |
|
in_uint16_le(s, row_size); |
710 |
|
in_uint16_le(s, final_size); |
711 |
|
|
712 |
|
} |
713 |
in_uint8p(s, data, size); |
in_uint8p(s, data, size); |
714 |
|
|
715 |
DEBUG(("BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx)); |
DEBUG(("BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d,bpp=%d,size=%d,pad1=%d,bufsize=%d,pad2=%d,rs=%d,fs=%d)\n", width, height, cache_id, cache_idx, bpp, size, pad1, bufsize, pad2, row_size, final_size)); |
716 |
|
|
717 |
bmpdata = xmalloc(width * height); |
bmpdata = (uint8 *) xmalloc(width * height * Bpp); |
718 |
|
|
719 |
if (bitmap_decompress(bmpdata, width, height, data, size)) |
if (bitmap_decompress(bmpdata, width, height, data, size, Bpp)) |
720 |
{ |
{ |
721 |
bitmap = ui_create_bitmap(width, height, bmpdata); |
bitmap = ui_create_bitmap(width, height, bmpdata); |
722 |
cache_put_bitmap(cache_id, cache_idx, bitmap); |
cache_put_bitmap(cache_id, cache_idx, bitmap, 0); |
723 |
|
} |
724 |
|
else |
725 |
|
{ |
726 |
|
DEBUG(("Failed to decompress bitmap data\n")); |
727 |
|
} |
728 |
|
|
729 |
|
xfree(bmpdata); |
730 |
|
} |
731 |
|
|
732 |
|
/* Process a bitmap cache v2 order */ |
733 |
|
static void |
734 |
|
process_bmpcache2(STREAM s, uint16 flags, BOOL compressed) |
735 |
|
{ |
736 |
|
HBITMAP bitmap; |
737 |
|
int y; |
738 |
|
uint8 cache_id, cache_idx_low, width, height, Bpp; |
739 |
|
uint16 cache_idx, bufsize; |
740 |
|
uint8 *data, *bmpdata, *bitmap_id; |
741 |
|
|
742 |
|
bitmap_id = NULL; /* prevent compiler warning */ |
743 |
|
cache_id = flags & ID_MASK; |
744 |
|
Bpp = ((flags & MODE_MASK) >> MODE_SHIFT) - 2; |
745 |
|
|
746 |
|
if (flags & PERSIST) |
747 |
|
{ |
748 |
|
in_uint8p(s, bitmap_id, 8); |
749 |
|
} |
750 |
|
|
751 |
|
if (flags & SQUARE) |
752 |
|
{ |
753 |
|
in_uint8(s, width); |
754 |
|
height = width; |
755 |
|
} |
756 |
|
else |
757 |
|
{ |
758 |
|
in_uint8(s, width); |
759 |
|
in_uint8(s, height); |
760 |
|
} |
761 |
|
|
762 |
|
in_uint16_be(s, bufsize); |
763 |
|
bufsize &= BUFSIZE_MASK; |
764 |
|
in_uint8(s, cache_idx); |
765 |
|
|
766 |
|
if (cache_idx & LONG_FORMAT) |
767 |
|
{ |
768 |
|
in_uint8(s, cache_idx_low); |
769 |
|
cache_idx = ((cache_idx ^ LONG_FORMAT) << 8) + cache_idx_low; |
770 |
|
} |
771 |
|
|
772 |
|
in_uint8p(s, data, bufsize); |
773 |
|
|
774 |
|
DEBUG(("BMPCACHE2(compr=%d,flags=%x,cx=%d,cy=%d,id=%d,idx=%d,Bpp=%d,bs=%d)\n", |
775 |
|
compressed, flags, width, height, cache_id, cache_idx, Bpp, bufsize)); |
776 |
|
|
777 |
|
bmpdata = (uint8 *) xmalloc(width * height * Bpp); |
778 |
|
|
779 |
|
if (compressed) |
780 |
|
{ |
781 |
|
if (!bitmap_decompress(bmpdata, width, height, data, bufsize, Bpp)) |
782 |
|
{ |
783 |
|
DEBUG(("Failed to decompress bitmap data\n")); |
784 |
|
xfree(bmpdata); |
785 |
|
return; |
786 |
|
} |
787 |
|
} |
788 |
|
else |
789 |
|
{ |
790 |
|
for (y = 0; y < height; y++) |
791 |
|
memcpy(&bmpdata[(height - y - 1) * (width * Bpp)], |
792 |
|
&data[y * (width * Bpp)], width * Bpp); |
793 |
|
} |
794 |
|
|
795 |
|
bitmap = ui_create_bitmap(width, height, bmpdata); |
796 |
|
|
797 |
|
if (bitmap) |
798 |
|
{ |
799 |
|
cache_put_bitmap(cache_id, cache_idx, bitmap, 0); |
800 |
|
if (flags & PERSIST) |
801 |
|
pstcache_put_bitmap(cache_id, cache_idx, bitmap_id, width, height, |
802 |
|
width * height * Bpp, bmpdata); |
803 |
|
} |
804 |
|
else |
805 |
|
{ |
806 |
|
DEBUG(("process_bmpcache2: ui_create_bitmap failed\n")); |
807 |
} |
} |
808 |
|
|
809 |
xfree(bmpdata); |
xfree(bmpdata); |
822 |
in_uint8(s, cache_id); |
in_uint8(s, cache_id); |
823 |
in_uint16_le(s, map.ncolours); |
in_uint16_le(s, map.ncolours); |
824 |
|
|
825 |
map.colours = xmalloc(3 * map.ncolours); |
map.colours = (COLOURENTRY *) xmalloc(sizeof(COLOURENTRY) * map.ncolours); |
826 |
|
|
827 |
for (i = 0; i < map.ncolours; i++) |
for (i = 0; i < map.ncolours; i++) |
828 |
{ |
{ |
876 |
static void |
static void |
877 |
process_secondary_order(STREAM s) |
process_secondary_order(STREAM s) |
878 |
{ |
{ |
879 |
|
/* The length isn't calculated correctly by the server. |
880 |
|
* For very compact orders the length becomes negative |
881 |
|
* so a signed integer must be used. */ |
882 |
uint16 length; |
uint16 length; |
883 |
|
uint16 flags; |
884 |
uint8 type; |
uint8 type; |
885 |
uint8 *next_order; |
uint8 *next_order; |
886 |
|
|
887 |
in_uint16_le(s, length); |
in_uint16_le(s, length); |
888 |
in_uint8s(s, 2); /* flags */ |
in_uint16_le(s, flags); /* used by bmpcache2 */ |
889 |
in_uint8(s, type); |
in_uint8(s, type); |
890 |
|
|
891 |
next_order = s->p + length + 7; |
next_order = s->p + (sint16)length + 7; |
892 |
|
|
893 |
switch (type) |
switch (type) |
894 |
{ |
{ |
908 |
process_fontcache(s); |
process_fontcache(s); |
909 |
break; |
break; |
910 |
|
|
911 |
|
case RDP_ORDER_RAW_BMPCACHE2: |
912 |
|
process_bmpcache2(s, flags, False); /* uncompressed */ |
913 |
|
break; |
914 |
|
|
915 |
|
case RDP_ORDER_BMPCACHE2: |
916 |
|
process_bmpcache2(s, flags, True); /* compressed */ |
917 |
|
break; |
918 |
|
|
919 |
default: |
default: |
920 |
unimpl("secondary order %d\n", type); |
unimpl("secondary order %d\n", type); |
921 |
} |
} |
925 |
|
|
926 |
/* Process an order PDU */ |
/* Process an order PDU */ |
927 |
void |
void |
928 |
process_orders(STREAM s) |
process_orders(STREAM s, uint16 num_orders) |
929 |
{ |
{ |
930 |
RDP_ORDER_STATE *os = &order_state; |
RDP_ORDER_STATE *os = &g_order_state; |
931 |
uint32 present; |
uint32 present; |
|
uint16 num_orders; |
|
932 |
uint8 order_flags; |
uint8 order_flags; |
933 |
int size, processed = 0; |
int size, processed = 0; |
934 |
BOOL delta; |
BOOL delta; |
935 |
|
|
|
in_uint8s(s, 2); /* pad */ |
|
|
in_uint16_le(s, num_orders); |
|
|
in_uint8s(s, 2); /* pad */ |
|
|
|
|
936 |
while (processed < num_orders) |
while (processed < num_orders) |
937 |
{ |
{ |
938 |
in_uint8(s, order_flags); |
in_uint8(s, order_flags); |
1040 |
|
|
1041 |
processed++; |
processed++; |
1042 |
} |
} |
1043 |
|
#if 0 |
1044 |
|
/* not true when RDP_COMPRESSION is set */ |
1045 |
|
if (s->p != g_next_packet) |
1046 |
|
error("%d bytes remaining\n", (int) (g_next_packet - s->p)); |
1047 |
|
#endif |
1048 |
|
|
|
if (s->p != next_packet) |
|
|
error("%d bytes remaining\n", (int) (next_packet - s->p)); |
|
1049 |
} |
} |
1050 |
|
|
1051 |
/* Reset order state */ |
/* Reset order state */ |
1052 |
void |
void |
1053 |
reset_order_state() |
reset_order_state(void) |
1054 |
{ |
{ |
1055 |
memset(&order_state, 0, sizeof(order_state)); |
memset(&g_order_state, 0, sizeof(g_order_state)); |
1056 |
order_state.order_type = RDP_ORDER_PATBLT; |
g_order_state.order_type = RDP_ORDER_PATBLT; |
1057 |
} |
} |