--- sourceforge.net/trunk/rdesktop/orders.c 2000/08/15 10:23:24 10 +++ sourceforge.net/trunk/rdesktop/orders.c 2001/06/20 13:54:48 28 @@ -21,11 +21,12 @@ #include "rdesktop.h" #include "orders.h" -extern unsigned char *next_packet; +extern uint8 *next_packet; static RDP_ORDER_STATE order_state; /* Read field indicating which parameters are present */ -static void rdp_in_present(STREAM s, uint32 *present, uint8 flags, int size) +static void +rdp_in_present(STREAM s, uint32 *present, uint8 flags, int size) { uint8 bits; int i; @@ -52,14 +53,15 @@ } /* Read a co-ordinate (16-bit, or 8-bit delta) */ -static void rdp_in_coord(STREAM s, uint16 *coord, BOOL delta) +static void +rdp_in_coord(STREAM s, uint16 *coord, BOOL delta) { uint8 change; if (delta) { in_uint8(s, change); - *coord += (char)change; + *coord += (char) change; } else { @@ -68,14 +70,16 @@ } /* Read a colour entry */ -static void rdp_in_colour(STREAM s, uint8 *colour) +static void +rdp_in_colour(STREAM s, uint8 *colour) { in_uint8(s, *colour); s->p += 2; } /* Parse bounds information */ -static BOOL rdp_parse_bounds(STREAM s, BOUNDS *bounds) +static BOOL +rdp_parse_bounds(STREAM s, BOUNDS *bounds) { uint8 present; @@ -105,7 +109,8 @@ } /* Parse a pen */ -static BOOL rdp_parse_pen(STREAM s, PEN *pen, uint32 present) +static BOOL +rdp_parse_pen(STREAM s, PEN *pen, uint32 present) { if (present & 1) in_uint8(s, pen->style); @@ -120,7 +125,8 @@ } /* Parse a brush */ -static BOOL rdp_parse_brush(STREAM s, BRUSH *brush, uint32 present) +static BOOL +rdp_parse_brush(STREAM s, BRUSH *brush, uint32 present) { if (present & 1) in_uint8(s, brush->xorigin); @@ -141,8 +147,8 @@ } /* Process a destination blt order */ -static void process_destblt(STREAM s, DESTBLT_ORDER *os, - uint32 present, BOOL delta) +static void +process_destblt(STREAM s, DESTBLT_ORDER *os, uint32 present, BOOL delta) { if (present & 0x01) rdp_in_coord(s, &os->x, delta); @@ -166,8 +172,8 @@ } /* Process a pattern blt order */ -static void process_patblt(STREAM s, PATBLT_ORDER *os, - uint32 present, BOOL delta) +static void +process_patblt(STREAM s, PATBLT_ORDER *os, uint32 present, BOOL delta) { if (present & 0x0001) rdp_in_coord(s, &os->x, delta); @@ -197,12 +203,12 @@ 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); + &os->brush, os->bgcolour, os->fgcolour); } /* Process a screen blt order */ -static void process_screenblt(STREAM s, SCREENBLT_ORDER *os, - uint32 present, BOOL delta) +static void +process_screenblt(STREAM s, SCREENBLT_ORDER *os, uint32 present, BOOL delta) { if (present & 0x0001) rdp_in_coord(s, &os->x, delta); @@ -229,11 +235,12 @@ 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); + os->srcx, os->srcy); } /* Process a line order */ -static void process_line(STREAM s, LINE_ORDER *os, uint32 present, BOOL delta) +static void +process_line(STREAM s, LINE_ORDER *os, uint32 present, BOOL delta) { if (present & 0x0001) in_uint16_le(s, os->mixmode); @@ -268,12 +275,13 @@ 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) +static void +process_rect(STREAM s, RECT_ORDER *os, uint32 present, BOOL delta) { if (present & 0x01) rdp_in_coord(s, &os->x, delta); @@ -297,8 +305,8 @@ } /* Process a desktop save order */ -static void process_desksave(STREAM s, DESKSAVE_ORDER *os, - uint32 present, BOOL delta) +static void +process_desksave(STREAM s, DESKSAVE_ORDER *os, uint32 present, BOOL delta) { int width, height; @@ -330,12 +338,13 @@ 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 */ -static void process_memblt(STREAM s, MEMBLT_ORDER *os, - uint32 present, BOOL delta) +static void +process_memblt(STREAM s, MEMBLT_ORDER *os, uint32 present, BOOL delta) { HBITMAP bitmap; @@ -378,12 +387,12 @@ return; ui_memblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, - bitmap, os->srcx, os->srcy); + bitmap, os->srcx, os->srcy); } /* Process a 3-way blt order */ -static void process_triblt(STREAM s, TRIBLT_ORDER *os, - uint32 present, BOOL delta) +static void +process_triblt(STREAM s, TRIBLT_ORDER *os, uint32 present, BOOL delta) { HBITMAP bitmap; @@ -428,21 +437,111 @@ 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 */ +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; +} + +/* 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; + uint8 flags = 0; + PEN pen; + + 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->flags); + + if (present & 0x10) + rdp_in_colour(s, &os->fgcolour); + + if (present & 0x20) + in_uint8(s, os->lines); + + if (present & 0x40) + { + in_uint8(s, os->datasize); + in_uint8a(s, os->data, os->datasize); + } + + 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("Data: "); + + for (index = 0; index < os->datasize; index++) + DEBUG("%02x ", os->data[index]); + + DEBUG("\n"); + + x = os->x; + 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++) + { + xfrom = x; + yfrom = y; + + if (line % 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); + + ui_line(ROP2_NXOR, xfrom, yfrom, x, y, &pen); + + flags <<= 2; + } } /* Process a text order */ -static void process_text2(STREAM s, TEXT2_ORDER *os, uint32 present, BOOL delta) +static void +process_text2(STREAM s, TEXT2_ORDER *os, uint32 present, BOOL delta) { DATABLOB *entry; int i; @@ -501,11 +600,12 @@ 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,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("Text: "); @@ -515,39 +615,45 @@ DEBUG("\n"); /* Process special cache strings */ - if ((os->length == 2) && (os->text[0] == 0xfe)) + if ((os->length >= 2) && (os->text[0] == 0xfe)) { entry = cache_get_text(os->text[1]); if (entry == NULL) return; - + memcpy(os->text, entry->data, entry->size); os->length = entry->size; } - else if ((os->length >= 3) && (os->text[os->length-3] == 0xff)) + else if ((os->length >= 3) && (os->text[os->length - 3] == 0xff)) { os->length -= 3; - cache_put_text(os->text[os->length+1], os->text, os->length); + cache_put_text(os->text[os->length + 1], os->text, + os->length); } - ui_draw_text(os->font, os->flags, os->mixmode, - os->x, os->y, 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->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); } /* Process a raw bitmap cache order */ -static void process_raw_bmpcache(STREAM s) +static void +process_raw_bmpcache(STREAM s) { HBITMAP bitmap; uint16 cache_idx, bufsize; uint8 cache_id, width, height, bpp; - uint8 *data; + uint8 *data, *inverted; + int y; in_uint8(s, cache_id); - in_uint8s(s, 1); /* pad */ + in_uint8s(s, 1); /* pad */ in_uint8(s, width); in_uint8(s, height); in_uint8(s, bpp); @@ -557,13 +663,21 @@ DEBUG("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx); + inverted = xmalloc(width * height); + for (y = 0; y < height; y++) + { + memcpy(&inverted[(height - y - 1) * width], &data[y * width], + width); + } - bitmap = ui_create_bitmap(width, height, data); + bitmap = ui_create_bitmap(width, height, inverted); + xfree(inverted); cache_put_bitmap(cache_id, cache_idx, bitmap); } /* Process a bitmap cache order */ -static void process_bmpcache(STREAM s) +static void +process_bmpcache(STREAM s) { HBITMAP bitmap; uint16 cache_idx, size; @@ -571,15 +685,15 @@ uint8 *data, *bmpdata; in_uint8(s, cache_id); - in_uint8s(s, 1); /* pad */ + in_uint8s(s, 1); /* pad */ in_uint8(s, width); in_uint8(s, height); in_uint8(s, bpp); - in_uint8s(s, 2); /* bufsize */ + in_uint8s(s, 2); /* bufsize */ in_uint16_le(s, cache_idx); - in_uint8s(s, 2); /* pad */ + in_uint8s(s, 2); /* pad */ in_uint16_le(s, size); - in_uint8s(s, 4); /* row_size, final_size */ + in_uint8s(s, 4); /* row_size, final_size */ in_uint8p(s, data, size); DEBUG("BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", @@ -597,7 +711,8 @@ } /* Process a colourmap cache order */ -static void process_colcache(STREAM s) +static void +process_colcache(STREAM s) { COLOURENTRY *entry; COLOURMAP map; @@ -616,7 +731,7 @@ in_uint8(s, entry->blue); in_uint8(s, entry->green); in_uint8(s, entry->red); - in_uint8s(s, 1); /* pad */ + in_uint8s(s, 1); /* pad */ } DEBUG("COLCACHE(id=%d,n=%d)\n", cache_id, map.ncolours); @@ -628,13 +743,14 @@ } /* Process a font cache order */ -static void process_fontcache(STREAM s) +static void +process_fontcache(STREAM s) { HGLYPH bitmap; uint8 font, nglyphs; - uint16 character, baseline, width, height; - uint8 *data, *rev_data, in, out; - int i, j, datasize; + uint16 character, offset, baseline, width, height; + int i, datasize; + uint8 *data; in_uint8(s, font); in_uint8(s, nglyphs); @@ -644,7 +760,7 @@ for (i = 0; i < nglyphs; i++) { in_uint16_le(s, character); - in_uint8s(s, 2); /* unknown */ + in_uint16_le(s, offset); in_uint16_le(s, baseline); in_uint16_le(s, width); in_uint16_le(s, height); @@ -652,40 +768,22 @@ datasize = (height * ((width + 7) / 8) + 3) & ~3; in_uint8p(s, data, datasize); - /* Need to reverse bit order */ - rev_data = xmalloc(datasize); - - for (j = 0; j < datasize; j++) - { - in = data[j]; - out = 0; - if (in & 1) out |= 128; - if (in & 2) out |= 64; - if (in & 4) out |= 32; - if (in & 8) out |= 16; - if (in & 16) out |= 8; - if (in & 32) out |= 4; - if (in & 64) out |= 2; - if (in & 128) out |= 1; - rev_data[j] = out; - } - - bitmap = ui_create_glyph(width, height, rev_data); - xfree(rev_data); - - cache_put_font(font, character, baseline, width, height, bitmap); + bitmap = ui_create_glyph(width, height, data); + cache_put_font(font, character, offset, baseline, + width, height, bitmap); } } /* Process a secondary order */ -static void process_secondary_order(STREAM s) +static void +process_secondary_order(STREAM s) { uint16 length; uint8 type; uint8 *next_order; in_uint16_le(s, length); - in_uint8s(s, 2); /* flags */ + in_uint8s(s, 2); /* flags */ in_uint8(s, type); next_order = s->p + length + 7; @@ -716,7 +814,8 @@ } /* Process an order PDU */ -void process_orders(STREAM s) +void +process_orders(STREAM s) { RDP_ORDER_STATE *os = &order_state; uint32 present; @@ -725,9 +824,9 @@ int size, processed = 0; BOOL delta; - in_uint8s(s, 2); /* pad */ + in_uint8s(s, 2); /* pad */ in_uint16_le(s, num_orders); - in_uint8s(s, 2); /* pad */ + in_uint8s(s, 2); /* pad */ while (processed < num_orders) { @@ -775,9 +874,11 @@ rdp_parse_bounds(s, &os->bounds); ui_set_clip(os->bounds.left, - os->bounds.top, - os->bounds.right - os->bounds.left + 1, - os->bounds.bottom - os->bounds.top + 1); + os->bounds.top, + os->bounds.right - + os->bounds.left + 1, + os->bounds.bottom - + os->bounds.top + 1); } delta = order_flags & RDP_ORDER_DELTA; @@ -801,7 +902,7 @@ case RDP_ORDER_LINE: process_line(s, &os->line, - present, delta); + present, delta); break; case RDP_ORDER_RECT: @@ -824,6 +925,11 @@ present, delta); break; + case RDP_ORDER_POLYLINE: + process_polyline(s, &os->polyline, + present, delta); + break; + case RDP_ORDER_TEXT2: process_text2(s, &os->text2, present, delta); @@ -842,12 +948,13 @@ } if (s->p != next_packet) - WARN("%d bytes remaining\n", next_packet - s->p); + WARN("%d bytes remaining\n", (int) (next_packet - s->p)); } /* Reset order state */ -void reset_order_state() +void +reset_order_state() { memset(&order_state, 0, sizeof(order_state)); + order_state.order_type = RDP_ORDER_PATBLT; } -