--- sourceforge.net/trunk/rdesktop/orders.c 2003/05/30 22:04:25 382 +++ sourceforge.net/trunk/rdesktop/orders.c 2005/04/23 22:36:42 893 @@ -1,7 +1,7 @@ /* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. RDP order processing - Copyright (C) Matthew Chapman 1999-2002 + Copyright (C) Matthew Chapman 1999-2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,9 +21,9 @@ #include "rdesktop.h" #include "orders.h" -extern uint8 *next_packet; +extern uint8 *g_next_packet; static RDP_ORDER_STATE g_order_state; -extern BOOL use_rdp5; +extern BOOL g_use_rdp5; /* Read field indicating which parameters are present */ static void @@ -70,6 +70,24 @@ } } +/* Parse a delta co-ordinate in polyline/polygon order form */ +static int +parse_delta(uint8 * buffer, int *offset) +{ + int value = buffer[(*offset)++]; + int two_byte = value & 0x80; + + if (value & 0x40) /* sign bit */ + value |= ~0x3f; + else + value &= 0x3f; + + if (two_byte) + value = (value << 8) | buffer[(*offset)++]; + + return value; +} + /* Read a colour entry */ static void rdp_in_colour(STREAM s, uint32 * colour) @@ -269,7 +287,7 @@ rdp_parse_pen(s, &os->pen, present >> 7); - 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", os->opcode, os->startx, os->starty, os->endx, os->endy, os->pen.colour)); if (os->opcode < 0x01 || os->opcode > 0x10) @@ -462,33 +480,178 @@ bitmap, os->srcx, os->srcy, &os->brush, os->bgcolour, os->fgcolour); } -/* Parse a delta co-ordinate in polyline order form */ -static int -parse_delta(uint8 * buffer, int *offset) +/* Process a polygon order */ +static void +process_polygon(STREAM s, POLYGON_ORDER * os, uint32 present, BOOL delta) { - int value = buffer[(*offset)++]; - int two_byte = value & 0x80; + int index, data, next; + uint8 flags = 0; + POINT *points; - if (value & 0x40) /* sign bit */ - value |= ~0x3f; + if (present & 0x01) + rdp_in_coord(s, &os->x, delta); + + if (present & 0x02) + rdp_in_coord(s, &os->y, delta); + + if (present & 0x04) + in_uint8(s, os->opcode); + + if (present & 0x08) + in_uint8(s, os->fillmode); + + if (present & 0x10) + rdp_in_colour(s, &os->fgcolour); + + if (present & 0x20) + in_uint8(s, os->npoints); + + if (present & 0x40) + { + in_uint8(s, os->datasize); + in_uint8a(s, os->data, os->datasize); + } + + DEBUG(("POLYGON(x=%d,y=%d,op=0x%x,fm=%d,fg=0x%x,n=%d,sz=%d)\n", + os->x, os->y, os->opcode, os->fillmode, os->fgcolour, os->npoints, os->datasize)); + + DEBUG(("Data: ")); + + for (index = 0; index < os->datasize; index++) + DEBUG(("%02x ", os->data[index])); + + DEBUG(("\n")); + + if (os->opcode < 0x01 || os->opcode > 0x10) + { + error("bad ROP2 0x%x\n", os->opcode); + return; + } + + points = (POINT *) xmalloc((os->npoints + 1) * sizeof(POINT)); + memset(points, 0, (os->npoints + 1) * sizeof(POINT)); + + points[0].x = os->x; + points[0].y = os->y; + + index = 0; + data = ((os->npoints - 1) / 4) + 1; + for (next = 1; (next <= os->npoints) && (next < 256) && (data < os->datasize); next++) + { + if ((next - 1) % 4 == 0) + flags = os->data[index++]; + + if (~flags & 0x80) + points[next].x = parse_delta(os->data, &data); + + if (~flags & 0x40) + points[next].y = parse_delta(os->data, &data); + + flags <<= 2; + } + + if (next - 1 == os->npoints) + ui_polygon(os->opcode - 1, os->fillmode, points, os->npoints + 1, NULL, 0, + os->fgcolour); else - value &= 0x3f; + error("polygon parse error\n"); - if (two_byte) - value = (value << 8) | buffer[(*offset)++]; + xfree(points); +} - return value; +/* Process a polygon2 order */ +static void +process_polygon2(STREAM s, POLYGON2_ORDER * os, uint32 present, BOOL delta) +{ + int index, data, next; + uint8 flags = 0; + POINT *points; + + if (present & 0x0001) + rdp_in_coord(s, &os->x, delta); + + if (present & 0x0002) + rdp_in_coord(s, &os->y, delta); + + if (present & 0x0004) + in_uint8(s, os->opcode); + + if (present & 0x0008) + in_uint8(s, os->fillmode); + + if (present & 0x0010) + rdp_in_colour(s, &os->bgcolour); + + if (present & 0x0020) + rdp_in_colour(s, &os->fgcolour); + + rdp_parse_brush(s, &os->brush, present >> 6); + + if (present & 0x0800) + in_uint8(s, os->npoints); + + if (present & 0x1000) + { + in_uint8(s, os->datasize); + in_uint8a(s, os->data, os->datasize); + } + + DEBUG(("POLYGON2(x=%d,y=%d,op=0x%x,fm=%d,bs=%d,bg=0x%x,fg=0x%x,n=%d,sz=%d)\n", + os->x, os->y, os->opcode, os->fillmode, os->brush.style, os->bgcolour, os->fgcolour, + os->npoints, os->datasize)); + + DEBUG(("Data: ")); + + for (index = 0; index < os->datasize; index++) + DEBUG(("%02x ", os->data[index])); + + DEBUG(("\n")); + + if (os->opcode < 0x01 || os->opcode > 0x10) + { + error("bad ROP2 0x%x\n", os->opcode); + return; + } + + points = (POINT *) xmalloc((os->npoints + 1) * sizeof(POINT)); + memset(points, 0, (os->npoints + 1) * sizeof(POINT)); + + points[0].x = os->x; + points[0].y = os->y; + + index = 0; + data = ((os->npoints - 1) / 4) + 1; + for (next = 1; (next <= os->npoints) && (next < 256) && (data < os->datasize); next++) + { + if ((next - 1) % 4 == 0) + flags = os->data[index++]; + + if (~flags & 0x80) + points[next].x = parse_delta(os->data, &data); + + if (~flags & 0x40) + points[next].y = parse_delta(os->data, &data); + + flags <<= 2; + } + + if (next - 1 == os->npoints) + ui_polygon(os->opcode - 1, os->fillmode, points, os->npoints + 1, + &os->brush, os->bgcolour, os->fgcolour); + else + error("polygon2 parse error\n"); + + xfree(points); } /* Process a polyline order */ static void process_polyline(STREAM s, POLYLINE_ORDER * os, uint32 present, BOOL delta) { - int index, line, data; - int x, y, xfrom, yfrom; + int index, next, data; uint8 flags = 0; PEN pen; - uint8 opcode; + POINT *points; if (present & 0x01) rdp_in_coord(s, &os->x, delta); @@ -527,35 +690,106 @@ return; } - opcode = os->opcode - 1; - x = os->x; - y = os->y; + points = (POINT *) xmalloc((os->lines + 1) * sizeof(POINT)); + memset(points, 0, (os->lines + 1) * sizeof(POINT)); + + points[0].x = os->x; + points[0].y = os->y; pen.style = pen.width = 0; pen.colour = os->fgcolour; index = 0; data = ((os->lines - 1) / 4) + 1; - for (line = 0; (line < os->lines) && (data < os->datasize); line++) + for (next = 1; (next <= os->lines) && (data < os->datasize); next++) { - xfrom = x; - yfrom = y; - - if (line % 4 == 0) + if ((next - 1) % 4 == 0) flags = os->data[index++]; - if ((flags & 0xc0) == 0) - flags |= 0xc0; /* none = both */ - - if (flags & 0x40) - x += parse_delta(os->data, &data); - - if (flags & 0x80) - y += parse_delta(os->data, &data); + if (~flags & 0x80) + points[next].x = parse_delta(os->data, &data); - ui_line(opcode, xfrom, yfrom, x, y, &pen); + if (~flags & 0x40) + points[next].y = parse_delta(os->data, &data); flags <<= 2; } + + if (next - 1 == os->lines) + ui_polyline(os->opcode - 1, points, os->lines + 1, &pen); + else + error("polyline parse error\n"); + + xfree(points); +} + +/* Process an ellipse order */ +static void +process_ellipse(STREAM s, ELLIPSE_ORDER * os, uint32 present, BOOL delta) +{ + if (present & 0x01) + rdp_in_coord(s, &os->left, delta); + + if (present & 0x02) + rdp_in_coord(s, &os->top, delta); + + if (present & 0x04) + rdp_in_coord(s, &os->right, delta); + + if (present & 0x08) + rdp_in_coord(s, &os->bottom, delta); + + if (present & 0x10) + in_uint8(s, os->opcode); + + if (present & 0x20) + in_uint8(s, os->fillmode); + + if (present & 0x40) + rdp_in_colour(s, &os->fgcolour); + + DEBUG(("ELLIPSE(l=%d,t=%d,r=%d,b=%d,op=0x%x,fm=%d,fg=0x%x)\n", os->left, os->top, + os->right, os->bottom, os->opcode, os->fillmode, os->fgcolour)); + + ui_ellipse(os->opcode - 1, os->fillmode, os->left, os->top, os->right - os->left, + os->bottom - os->top, NULL, 0, os->fgcolour); +} + +/* Process an ellipse2 order */ +static void +process_ellipse2(STREAM s, ELLIPSE2_ORDER * os, uint32 present, BOOL delta) +{ + if (present & 0x0001) + rdp_in_coord(s, &os->left, delta); + + if (present & 0x0002) + rdp_in_coord(s, &os->top, delta); + + if (present & 0x0004) + rdp_in_coord(s, &os->right, delta); + + if (present & 0x0008) + rdp_in_coord(s, &os->bottom, delta); + + if (present & 0x0010) + in_uint8(s, os->opcode); + + if (present & 0x0020) + in_uint8(s, os->fillmode); + + if (present & 0x0040) + rdp_in_colour(s, &os->bgcolour); + + if (present & 0x0080) + rdp_in_colour(s, &os->fgcolour); + + rdp_parse_brush(s, &os->brush, present >> 8); + + DEBUG(("ELLIPSE2(l=%d,t=%d,r=%d,b=%d,op=0x%x,fm=%d,bs=%d,bg=0x%x,fg=0x%x)\n", + os->left, os->top, os->right, os->bottom, os->opcode, os->fillmode, os->brush.style, + os->bgcolour, os->fgcolour)); + + ui_ellipse(os->opcode - 1, os->fillmode, os->left, os->top, os->right - os->left, + os->bottom - os->top, &os->brush, os->bgcolour, os->fgcolour); } /* Process a text order */ @@ -571,7 +805,7 @@ in_uint8(s, os->flags); if (present & 0x000004) - in_uint8(s, os->unknown); + in_uint8(s, os->opcode); if (present & 0x000008) in_uint8(s, os->mixmode); @@ -606,9 +840,7 @@ if (present & 0x002000) in_uint16_le(s, os->boxbottom); - if (present & 0x004000) /* fix for connecting to a server that */ - in_uint8s(s, 10); /* was disconnected with mstsc.exe */ - /* 0x008000, 0x020000, and 0x040000 are present too ??? */ + rdp_parse_brush(s, &os->brush, present >> 14); if (present & 0x080000) in_uint16_le(s, os->x); @@ -622,7 +854,7 @@ in_uint8a(s, os->text, os->length); } - DEBUG(("TEXT2(x=%d,y=%d,cl=%d,ct=%d,cr=%d,cb=%d,bl=%d,bt=%d,bb=%d,br=%d,fg=0x%x,bg=0x%x,font=%d,fl=0x%x,mix=%d,unk=0x%x,n=%d)\n", os->x, os->y, os->clipleft, os->cliptop, os->clipright, os->clipbottom, os->boxleft, os->boxtop, os->boxright, os->boxbottom, os->fgcolour, os->bgcolour, os->font, os->flags, os->mixmode, os->unknown, os->length)); + DEBUG(("TEXT2(x=%d,y=%d,cl=%d,ct=%d,cr=%d,cb=%d,bl=%d,bt=%d,br=%d,bb=%d,bs=%d,bg=0x%x,fg=0x%x,font=%d,fl=0x%x,op=0x%x,mix=%d,n=%d)\n", os->x, os->y, os->clipleft, os->cliptop, os->clipright, os->clipbottom, os->boxleft, os->boxtop, os->boxright, os->boxbottom, os->brush.style, os->bgcolour, os->fgcolour, os->font, os->flags, os->opcode, os->mixmode, os->length)); DEBUG(("Text: ")); @@ -631,13 +863,11 @@ DEBUG(("\n")); - ui_draw_text(os->font, os->flags, os->mixmode, os->x, os->y, - os->clipleft, os->cliptop, - os->clipright - os->clipleft, - os->clipbottom - os->cliptop, - os->boxleft, os->boxtop, - os->boxright - os->boxleft, - os->boxbottom - os->boxtop, os->bgcolour, os->fgcolour, os->text, os->length); + ui_draw_text(os->font, os->flags, os->opcode - 1, os->mixmode, os->x, os->y, + os->clipleft, os->cliptop, os->clipright - os->clipleft, + os->clipbottom - os->cliptop, os->boxleft, os->boxtop, + os->boxright - os->boxleft, os->boxbottom - os->boxtop, + &os->brush, os->bgcolour, os->fgcolour, os->text, os->length); } /* Process a raw bitmap cache order */ @@ -661,7 +891,7 @@ in_uint8p(s, data, bufsize); DEBUG(("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx)); - inverted = (uint8*)xmalloc(width * height * Bpp); + inverted = (uint8 *) xmalloc(width * height * Bpp); for (y = 0; y < height; y++) { memcpy(&inverted[(height - y - 1) * (width * Bpp)], &data[y * (width * Bpp)], @@ -684,7 +914,7 @@ uint16 bufsize, pad2, row_size, final_size; uint8 pad1; - pad2 = row_size = final_size = 0xffff; /* Shut the compiler up */ + pad2 = row_size = final_size = 0xffff; /* Shut the compiler up */ in_uint8(s, cache_id); in_uint8(s, pad1); /* pad */ @@ -695,26 +925,26 @@ in_uint16_le(s, bufsize); /* bufsize */ in_uint16_le(s, cache_idx); - if (!use_rdp5) { + if (g_use_rdp5) + { + size = bufsize; + } + else + { /* Begin compressedBitmapData */ in_uint16_le(s, pad2); /* pad */ in_uint16_le(s, size); - // in_uint8s(s, 4); /* row_size, final_size */ + /* in_uint8s(s, 4); *//* row_size, final_size */ in_uint16_le(s, row_size); in_uint16_le(s, final_size); - } else { - size = bufsize; } in_uint8p(s, data, size); - 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)); + 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)); - bmpdata = (uint8*)xmalloc(width * height * Bpp); + bmpdata = (uint8 *) xmalloc(width * height * Bpp); if (bitmap_decompress(bmpdata, width, height, data, size, Bpp)) { @@ -729,6 +959,86 @@ xfree(bmpdata); } +/* Process a bitmap cache v2 order */ +static void +process_bmpcache2(STREAM s, uint16 flags, BOOL compressed) +{ + HBITMAP bitmap; + int y; + uint8 cache_id, cache_idx_low, width, height, Bpp; + uint16 cache_idx, bufsize; + uint8 *data, *bmpdata, *bitmap_id; + + bitmap_id = NULL; /* prevent compiler warning */ + cache_id = flags & ID_MASK; + Bpp = ((flags & MODE_MASK) >> MODE_SHIFT) - 2; + + if (flags & PERSIST) + { + in_uint8p(s, bitmap_id, 8); + } + + if (flags & SQUARE) + { + in_uint8(s, width); + height = width; + } + else + { + in_uint8(s, width); + in_uint8(s, height); + } + + in_uint16_be(s, bufsize); + bufsize &= BUFSIZE_MASK; + in_uint8(s, cache_idx); + + if (cache_idx & LONG_FORMAT) + { + in_uint8(s, cache_idx_low); + cache_idx = ((cache_idx ^ LONG_FORMAT) << 8) + cache_idx_low; + } + + in_uint8p(s, data, bufsize); + + DEBUG(("BMPCACHE2(compr=%d,flags=%x,cx=%d,cy=%d,id=%d,idx=%d,Bpp=%d,bs=%d)\n", + compressed, flags, width, height, cache_id, cache_idx, Bpp, bufsize)); + + bmpdata = (uint8 *) xmalloc(width * height * Bpp); + + if (compressed) + { + if (!bitmap_decompress(bmpdata, width, height, data, bufsize, Bpp)) + { + DEBUG(("Failed to decompress bitmap data\n")); + xfree(bmpdata); + return; + } + } + else + { + for (y = 0; y < height; y++) + memcpy(&bmpdata[(height - y - 1) * (width * Bpp)], + &data[y * (width * Bpp)], width * Bpp); + } + + bitmap = ui_create_bitmap(width, height, bmpdata); + + if (bitmap) + { + cache_put_bitmap(cache_id, cache_idx, bitmap); + if (flags & PERSIST) + pstcache_save_bitmap(cache_id, cache_idx, bitmap_id, width, height, + width * height * Bpp, bmpdata); + } + else + { + DEBUG(("process_bmpcache2: ui_create_bitmap failed\n")); + } + + xfree(bmpdata); +} + /* Process a colourmap cache order */ static void process_colcache(STREAM s) @@ -742,7 +1052,7 @@ in_uint8(s, cache_id); in_uint16_le(s, map.ncolours); - map.colours = (COLOURENTRY*)xmalloc(3 * map.ncolours); + map.colours = (COLOURENTRY *) xmalloc(sizeof(COLOURENTRY) * map.ncolours); for (i = 0; i < map.ncolours; i++) { @@ -756,7 +1066,9 @@ DEBUG(("COLCACHE(id=%d,n=%d)\n", cache_id, map.ncolours)); hmap = ui_create_colourmap(&map); - ui_set_colourmap(hmap); + + if (cache_id) + ui_set_colourmap(hmap); xfree(map.colours); } @@ -796,15 +1108,19 @@ static void process_secondary_order(STREAM s) { + /* The length isn't calculated correctly by the server. + * For very compact orders the length becomes negative + * so a signed integer must be used. */ uint16 length; + uint16 flags; uint8 type; uint8 *next_order; in_uint16_le(s, length); - in_uint8s(s, 2); /* flags */ + in_uint16_le(s, flags); /* used by bmpcache2 */ in_uint8(s, type); - next_order = s->p + length + 7; + next_order = s->p + (sint16) length + 7; switch (type) { @@ -824,6 +1140,14 @@ process_fontcache(s); break; + case RDP_ORDER_RAW_BMPCACHE2: + process_bmpcache2(s, flags, False); /* uncompressed */ + break; + + case RDP_ORDER_BMPCACHE2: + process_bmpcache2(s, flags, True); /* compressed */ + break; + default: unimpl("secondary order %d\n", type); } @@ -872,6 +1196,8 @@ case RDP_ORDER_PATBLT: case RDP_ORDER_MEMBLT: case RDP_ORDER_LINE: + case RDP_ORDER_POLYGON2: + case RDP_ORDER_ELLIPSE2: size = 2; break; @@ -929,10 +1255,26 @@ process_triblt(s, &os->triblt, present, delta); break; + case RDP_ORDER_POLYGON: + process_polygon(s, &os->polygon, present, delta); + break; + + case RDP_ORDER_POLYGON2: + process_polygon2(s, &os->polygon2, present, delta); + break; + case RDP_ORDER_POLYLINE: process_polyline(s, &os->polyline, present, delta); break; + case RDP_ORDER_ELLIPSE: + process_ellipse(s, &os->ellipse, present, delta); + break; + + case RDP_ORDER_ELLIPSE2: + process_ellipse2(s, &os->ellipse2, present, delta); + break; + case RDP_ORDER_TEXT2: process_text2(s, &os->text2, present, delta); break; @@ -948,9 +1290,12 @@ processed++; } +#if 0 + /* not true when RDP_COMPRESSION is set */ + if (s->p != g_next_packet) + error("%d bytes remaining\n", (int) (g_next_packet - s->p)); +#endif - if (s->p != next_packet) - error("%d bytes remaining\n", (int) (next_packet - s->p)); } /* Reset order state */