--- sourceforge.net/trunk/rdesktop/orders.c 2002/07/18 16:38:31 64 +++ sourceforge.net/trunk/rdesktop/orders.c 2004/04/27 12:55:33 683 @@ -1,7 +1,7 @@ -/* +/* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. RDP order processing - Copyright (C) Matthew Chapman 1999-2001 + Copyright (C) Matthew Chapman 1999-2002 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,8 +21,9 @@ #include "rdesktop.h" #include "orders.h" -extern uint8 *next_packet; -static RDP_ORDER_STATE order_state; +extern uint8 *g_next_packet; +static RDP_ORDER_STATE g_order_state; +extern BOOL g_use_rdp5; /* Read field indicating which parameters are present */ static void @@ -54,14 +55,14 @@ /* Read a co-ordinate (16-bit, or 8-bit delta) */ static void -rdp_in_coord(STREAM s, uint16 * coord, BOOL delta) +rdp_in_coord(STREAM s, sint16 * coord, BOOL delta) { - uint8 change; + sint8 change; if (delta) { in_uint8(s, change); - *coord += (signed char) change; + *coord += change; } else { @@ -71,10 +72,15 @@ /* Read a colour entry */ static void -rdp_in_colour(STREAM s, uint8 * colour) +rdp_in_colour(STREAM s, uint32 * colour) { - in_uint8(s, *colour); - s->p += 2; + uint32 i; + in_uint8(s, i); + *colour = i; + in_uint8(s, i); + *colour |= i << 8; + in_uint8(s, i); + *colour |= i << 16; } /* Parse bounds information */ @@ -198,7 +204,8 @@ rdp_parse_brush(s, &os->brush, present >> 7); - DEBUG(("PATBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,bs=%d,bg=0x%x,fg=0x%x)\n", os->opcode, os->x, os->y, os->cx, os->cy, os->brush.style, os->bgcolour, os->fgcolour)); + DEBUG(("PATBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,bs=%d,bg=0x%x,fg=0x%x)\n", os->opcode, os->x, + os->y, os->cx, os->cy, os->brush.style, os->bgcolour, os->fgcolour)); ui_patblt(ROP2_P(os->opcode), os->x, os->y, os->cx, os->cy, &os->brush, os->bgcolour, os->fgcolour); @@ -232,8 +239,7 @@ DEBUG(("SCREENBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,srcx=%d,srcy=%d)\n", os->opcode, os->x, os->y, os->cx, os->cy, os->srcx, os->srcy)); - ui_screenblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, - os->srcx, os->srcy); + ui_screenblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, os->srcx, os->srcy); } /* Process a line order */ @@ -264,8 +270,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", - 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)); if (os->opcode < 0x01 || os->opcode > 0x10) { @@ -273,14 +278,14 @@ return; } - ui_line(os->opcode - 1, os->startx, os->starty, - os->endx, os->endy, &os->pen); + ui_line(os->opcode - 1, os->startx, os->starty, os->endx, os->endy, &os->pen); } /* Process an opaque rectangle order */ static void process_rect(STREAM s, RECT_ORDER * os, uint32 present, BOOL delta) { + uint32 i; if (present & 0x01) rdp_in_coord(s, &os->x, delta); @@ -294,10 +299,24 @@ rdp_in_coord(s, &os->cy, delta); if (present & 0x10) - in_uint8(s, os->colour); + { + in_uint8(s, i); + os->colour = (os->colour & 0xffffff00) | i; + } + + if (present & 0x20) + { + in_uint8(s, i); + os->colour = (os->colour & 0xffff00ff) | (i << 8); + } + + if (present & 0x40) + { + in_uint8(s, i); + os->colour = (os->colour & 0xff00ffff) | (i << 16); + } - 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)); ui_rect(os->x, os->y, os->cx, os->cy, os->colour); } @@ -327,8 +346,7 @@ in_uint8(s, os->action); DEBUG(("DESKSAVE(l=%d,t=%d,r=%d,b=%d,off=%d,op=%d)\n", - os->left, os->top, os->right, os->bottom, os->offset, - os->action)); + os->left, os->top, os->right, os->bottom, os->offset, os->action)); width = os->right - os->left + 1; height = os->bottom - os->top + 1; @@ -336,8 +354,7 @@ if (os->action == 0) ui_desktop_save(os->offset, os->left, os->top, width, height); else - ui_desktop_restore(os->offset, os->left, os->top, width, - height); + ui_desktop_restore(os->offset, os->left, os->top, width, height); } /* Process a memory blt order */ @@ -377,15 +394,13 @@ in_uint16_le(s, os->cache_idx); DEBUG(("MEMBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,id=%d,idx=%d)\n", - os->opcode, os->x, os->y, os->cx, os->cy, os->cache_id, - os->cache_idx)); + os->opcode, os->x, os->y, os->cx, os->cy, os->cache_id, os->cache_idx)); bitmap = cache_get_bitmap(os->cache_id, os->cache_idx); if (bitmap == NULL) return; - ui_memblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, - bitmap, os->srcx, os->srcy); + ui_memblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, bitmap, os->srcx, os->srcy); } /* Process a 3-way blt order */ @@ -435,15 +450,16 @@ if (present & 0x010000) in_uint16_le(s, os->unknown); - DEBUG(("TRIBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,id=%d,idx=%d,bs=%d,bg=0x%x,fg=0x%x)\n", os->opcode, os->x, os->y, os->cx, os->cy, os->cache_id, os->cache_idx, os->brush.style, os->bgcolour, os->fgcolour)); + DEBUG(("TRIBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,id=%d,idx=%d,bs=%d,bg=0x%x,fg=0x%x)\n", + os->opcode, os->x, os->y, os->cx, os->cy, os->cache_id, os->cache_idx, + os->brush.style, os->bgcolour, os->fgcolour)); bitmap = cache_get_bitmap(os->cache_id, os->cache_idx); if (bitmap == NULL) return; ui_triblt(os->opcode, os->x, os->y, os->cx, os->cy, - bitmap, os->srcx, os->srcy, - &os->brush, os->bgcolour, os->fgcolour); + bitmap, os->srcx, os->srcy, &os->brush, os->bgcolour, os->fgcolour); } /* Parse a delta co-ordinate in polyline order form */ @@ -481,7 +497,7 @@ rdp_in_coord(s, &os->y, delta); if (present & 0x04) - in_uint8(s, os->flags); + in_uint8(s, os->opcode); if (present & 0x10) rdp_in_colour(s, &os->fgcolour); @@ -494,14 +510,9 @@ in_uint8(s, os->datasize); in_uint8a(s, os->data, os->datasize); } - if (os->flags & 1) - opcode = ROP2_COPY; - else - opcode = ROP2_NXOR; - DEBUG(("POLYLINE(x=%d,y=%d,fl=0x%x,fg=0x%x,n=%d,sz=%d)\n", - os->x, os->y, os->flags, os->fgcolour, os->lines, - os->datasize)); + DEBUG(("POLYLINE(x=%d,y=%d,op=0x%x,fg=0x%x,n=%d,sz=%d)\n", + os->x, os->y, os->opcode, os->fgcolour, os->lines, os->datasize)); DEBUG(("Data: ")); @@ -510,6 +521,13 @@ DEBUG(("\n")); + if (os->opcode < 0x01 || os->opcode > 0x10) + { + error("bad ROP2 0x%x\n", os->opcode); + return; + } + + opcode = os->opcode - 1; x = os->x; y = os->y; pen.style = pen.width = 0; @@ -588,6 +606,10 @@ 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 ??? */ + if (present & 0x080000) in_uint16_le(s, os->x); @@ -615,8 +637,7 @@ os->clipbottom - os->cliptop, os->boxleft, os->boxtop, os->boxright - os->boxleft, - os->boxbottom - os->boxtop, - os->bgcolour, os->fgcolour, os->text, os->length); + os->boxbottom - os->boxtop, os->bgcolour, os->fgcolour, os->text, os->length); } /* Process a raw bitmap cache order */ @@ -625,7 +646,7 @@ { HBITMAP bitmap; uint16 cache_idx, bufsize; - uint8 cache_id, width, height, bpp; + uint8 cache_id, width, height, bpp, Bpp; uint8 *data, *inverted; int y; @@ -634,17 +655,17 @@ in_uint8(s, width); in_uint8(s, height); in_uint8(s, bpp); + Bpp = (bpp + 7) / 8; in_uint16_le(s, bufsize); in_uint16_le(s, cache_idx); in_uint8p(s, data, bufsize); - DEBUG(("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", - width, height, cache_id, cache_idx)); - inverted = xmalloc(width * height); + DEBUG(("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx)); + inverted = (uint8 *) xmalloc(width * height * Bpp); for (y = 0; y < height; y++) { - memcpy(&inverted[(height - y - 1) * width], &data[y * width], - width); + memcpy(&inverted[(height - y - 1) * (width * Bpp)], &data[y * (width * Bpp)], + width * Bpp); } bitmap = ui_create_bitmap(width, height, inverted); @@ -658,31 +679,52 @@ { HBITMAP bitmap; uint16 cache_idx, size; - uint8 cache_id, width, height, bpp; + uint8 cache_id, width, height, bpp, Bpp; uint8 *data, *bmpdata; + uint16 bufsize, pad2, row_size, final_size; + uint8 pad1; + + pad2 = row_size = final_size = 0xffff; /* Shut the compiler up */ in_uint8(s, cache_id); - in_uint8s(s, 1); /* pad */ + in_uint8(s, pad1); /* pad */ in_uint8(s, width); in_uint8(s, height); in_uint8(s, bpp); - in_uint8s(s, 2); /* bufsize */ + Bpp = (bpp + 7) / 8; + in_uint16_le(s, bufsize); /* bufsize */ in_uint16_le(s, cache_idx); - in_uint8s(s, 2); /* pad */ - in_uint16_le(s, size); - in_uint8s(s, 4); /* row_size, final_size */ + + if (!g_use_rdp5) + { + + /* Begin compressedBitmapData */ + in_uint16_le(s, pad2); /* pad */ + in_uint16_le(s, 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)\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)); - bmpdata = xmalloc(width * height); + bmpdata = (uint8 *) xmalloc(width * height * Bpp); - if (bitmap_decompress(bmpdata, width, height, data, size)) + if (bitmap_decompress(bmpdata, width, height, data, size, Bpp)) { bitmap = ui_create_bitmap(width, height, bmpdata); cache_put_bitmap(cache_id, cache_idx, bitmap); } + else + { + DEBUG(("Failed to decompress bitmap data\n")); + } xfree(bmpdata); } @@ -700,7 +742,7 @@ in_uint8(s, cache_id); in_uint16_le(s, map.ncolours); - map.colours = xmalloc(3 * map.ncolours); + map.colours = (COLOURENTRY *) xmalloc(sizeof(COLOURENTRY) * map.ncolours); for (i = 0; i < map.ncolours; i++) { @@ -746,8 +788,7 @@ in_uint8p(s, data, datasize); bitmap = ui_create_glyph(width, height, data); - cache_put_font(font, character, offset, baseline, - width, height, bitmap); + cache_put_font(font, character, offset, baseline, width, height, bitmap); } } @@ -792,19 +833,14 @@ /* Process an order PDU */ void -process_orders(STREAM s) +process_orders(STREAM s, uint16 num_orders) { - RDP_ORDER_STATE *os = &order_state; + RDP_ORDER_STATE *os = &g_order_state; uint32 present; - uint16 num_orders; uint8 order_flags; int size, processed = 0; BOOL delta; - in_uint8s(s, 2); /* pad */ - in_uint16_le(s, num_orders); - in_uint8s(s, 2); /* pad */ - while (processed < num_orders) { in_uint8(s, order_flags); @@ -854,8 +890,7 @@ os->bounds.top, os->bounds.right - os->bounds.left + 1, - os->bounds.bottom - - os->bounds.top + 1); + os->bounds.bottom - os->bounds.top + 1); } delta = order_flags & RDP_ORDER_DELTA; @@ -863,53 +898,43 @@ switch (os->order_type) { case RDP_ORDER_DESTBLT: - process_destblt(s, &os->destblt, - present, delta); + process_destblt(s, &os->destblt, present, delta); break; case RDP_ORDER_PATBLT: - process_patblt(s, &os->patblt, - present, delta); + process_patblt(s, &os->patblt, present, delta); break; case RDP_ORDER_SCREENBLT: - process_screenblt(s, &os->screenblt, - present, delta); + process_screenblt(s, &os->screenblt, present, delta); break; case RDP_ORDER_LINE: - process_line(s, &os->line, - present, delta); + process_line(s, &os->line, present, delta); break; case RDP_ORDER_RECT: - process_rect(s, &os->rect, - present, delta); + process_rect(s, &os->rect, present, delta); break; case RDP_ORDER_DESKSAVE: - process_desksave(s, &os->desksave, - present, delta); + process_desksave(s, &os->desksave, present, delta); break; case RDP_ORDER_MEMBLT: - process_memblt(s, &os->memblt, - present, delta); + process_memblt(s, &os->memblt, present, delta); break; case RDP_ORDER_TRIBLT: - process_triblt(s, &os->triblt, - present, delta); + process_triblt(s, &os->triblt, present, delta); break; case RDP_ORDER_POLYLINE: - process_polyline(s, &os->polyline, - present, delta); + process_polyline(s, &os->polyline, present, delta); break; case RDP_ORDER_TEXT2: - process_text2(s, &os->text2, - present, delta); + process_text2(s, &os->text2, present, delta); break; default: @@ -923,15 +948,18 @@ 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 */ void -reset_order_state() +reset_order_state(void) { - memset(&order_state, 0, sizeof(order_state)); - order_state.order_type = RDP_ORDER_PATBLT; + memset(&g_order_state, 0, sizeof(g_order_state)); + g_order_state.order_type = RDP_ORDER_PATBLT; }