[NtUser]
[reactos.git] / reactos / base / applications / mstsc / orders.c
1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 RDP order processing
4 Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
5
6 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
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "precomp.h"
21 #include "orders.h"
22
23 extern uint8 *g_next_packet;
24 static RDP_ORDER_STATE g_order_state;
25 extern RDP_VERSION g_rdp_version;
26
27 /* Read field indicating which parameters are present */
28 static void
29 rdp_in_present(STREAM s, uint32 * present, uint8 flags, int size)
30 {
31 uint8 bits;
32 int i;
33
34 if (flags & RDP_ORDER_SMALL)
35 {
36 size--;
37 }
38
39 if (flags & RDP_ORDER_TINY)
40 {
41 if (size < 2)
42 size = 0;
43 else
44 size -= 2;
45 }
46
47 *present = 0;
48 for (i = 0; i < size; i++)
49 {
50 in_uint8(s, bits);
51 *present |= bits << (i * 8);
52 }
53 }
54
55 /* Read a co-ordinate (16-bit, or 8-bit delta) */
56 static void
57 rdp_in_coord(STREAM s, sint16 * coord, RD_BOOL delta)
58 {
59 sint8 change;
60
61 if (delta)
62 {
63 in_uint8(s, change);
64 *coord += change;
65 }
66 else
67 {
68 in_uint16_le(s, *coord);
69 }
70 }
71
72 /* Parse a delta co-ordinate in polyline/polygon order form */
73 static int
74 parse_delta(uint8 * buffer, int *offset)
75 {
76 int value = buffer[(*offset)++];
77 int two_byte = value & 0x80;
78
79 if (value & 0x40) /* sign bit */
80 value |= ~0x3f;
81 else
82 value &= 0x3f;
83
84 if (two_byte)
85 value = (value << 8) | buffer[(*offset)++];
86
87 return value;
88 }
89
90 /* Read a colour entry */
91 static void
92 rdp_in_colour(STREAM s, uint32 * colour)
93 {
94 uint32 i;
95 in_uint8(s, i);
96 *colour = i;
97 in_uint8(s, i);
98 *colour |= i << 8;
99 in_uint8(s, i);
100 *colour |= i << 16;
101 }
102
103 /* Parse bounds information */
104 static RD_BOOL
105 rdp_parse_bounds(STREAM s, BOUNDS * bounds)
106 {
107 uint8 present;
108
109 in_uint8(s, present);
110
111 if (present & 1)
112 rdp_in_coord(s, &bounds->left, False);
113 else if (present & 16)
114 rdp_in_coord(s, &bounds->left, True);
115
116 if (present & 2)
117 rdp_in_coord(s, &bounds->top, False);
118 else if (present & 32)
119 rdp_in_coord(s, &bounds->top, True);
120
121 if (present & 4)
122 rdp_in_coord(s, &bounds->right, False);
123 else if (present & 64)
124 rdp_in_coord(s, &bounds->right, True);
125
126 if (present & 8)
127 rdp_in_coord(s, &bounds->bottom, False);
128 else if (present & 128)
129 rdp_in_coord(s, &bounds->bottom, True);
130
131 return s_check(s);
132 }
133
134 /* Parse a pen */
135 static RD_BOOL
136 rdp_parse_pen(STREAM s, PEN * pen, uint32 present)
137 {
138 if (present & 1)
139 in_uint8(s, pen->style);
140
141 if (present & 2)
142 in_uint8(s, pen->width);
143
144 if (present & 4)
145 rdp_in_colour(s, &pen->colour);
146
147 return s_check(s);
148 }
149
150 static void
151 setup_brush(BRUSH * out_brush, BRUSH * in_brush)
152 {
153 BRUSHDATA *brush_data;
154 uint8 cache_idx;
155 uint8 colour_code;
156
157 memcpy(out_brush, in_brush, sizeof(BRUSH));
158 if (out_brush->style & 0x80)
159 {
160 colour_code = out_brush->style & 0x0f;
161 cache_idx = out_brush->pattern[0];
162 brush_data = cache_get_brush_data(colour_code, cache_idx);
163 if ((brush_data == NULL) || (brush_data->data == NULL))
164 {
165 error("error getting brush data, style %x\n", out_brush->style);
166 out_brush->bd = NULL;
167 memset(out_brush->pattern, 0, 8);
168 }
169 else
170 {
171 out_brush->bd = brush_data;
172 }
173 out_brush->style = 3;
174 }
175 }
176
177 /* Parse a brush */
178 static RD_BOOL
179 rdp_parse_brush(STREAM s, BRUSH * brush, uint32 present)
180 {
181 if (present & 1)
182 in_uint8(s, brush->xorigin);
183
184 if (present & 2)
185 in_uint8(s, brush->yorigin);
186
187 if (present & 4)
188 in_uint8(s, brush->style);
189
190 if (present & 8)
191 in_uint8(s, brush->pattern[0]);
192
193 if (present & 16)
194 in_uint8a(s, &brush->pattern[1], 7);
195
196 return s_check(s);
197 }
198
199 /* Process a destination blt order */
200 static void
201 process_destblt(STREAM s, DESTBLT_ORDER * os, uint32 present, RD_BOOL delta)
202 {
203 if (present & 0x01)
204 rdp_in_coord(s, &os->x, delta);
205
206 if (present & 0x02)
207 rdp_in_coord(s, &os->y, delta);
208
209 if (present & 0x04)
210 rdp_in_coord(s, &os->cx, delta);
211
212 if (present & 0x08)
213 rdp_in_coord(s, &os->cy, delta);
214
215 if (present & 0x10)
216 in_uint8(s, os->opcode);
217
218 DEBUG(("DESTBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d)\n",
219 os->opcode, os->x, os->y, os->cx, os->cy));
220
221 ui_destblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy);
222 }
223
224 /* Process a pattern blt order */
225 static void
226 process_patblt(STREAM s, PATBLT_ORDER * os, uint32 present, RD_BOOL delta)
227 {
228 BRUSH brush;
229
230 if (present & 0x0001)
231 rdp_in_coord(s, &os->x, delta);
232
233 if (present & 0x0002)
234 rdp_in_coord(s, &os->y, delta);
235
236 if (present & 0x0004)
237 rdp_in_coord(s, &os->cx, delta);
238
239 if (present & 0x0008)
240 rdp_in_coord(s, &os->cy, delta);
241
242 if (present & 0x0010)
243 in_uint8(s, os->opcode);
244
245 if (present & 0x0020)
246 rdp_in_colour(s, &os->bgcolour);
247
248 if (present & 0x0040)
249 rdp_in_colour(s, &os->fgcolour);
250
251 rdp_parse_brush(s, &os->brush, present >> 7);
252
253 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,
254 os->y, os->cx, os->cy, os->brush.style, os->bgcolour, os->fgcolour));
255
256 setup_brush(&brush, &os->brush);
257
258 ui_patblt(ROP2_P(os->opcode), os->x, os->y, os->cx, os->cy,
259 &brush, os->bgcolour, os->fgcolour);
260 }
261
262 /* Process a screen blt order */
263 static void
264 process_screenblt(STREAM s, SCREENBLT_ORDER * os, uint32 present, RD_BOOL delta)
265 {
266 if (present & 0x0001)
267 rdp_in_coord(s, &os->x, delta);
268
269 if (present & 0x0002)
270 rdp_in_coord(s, &os->y, delta);
271
272 if (present & 0x0004)
273 rdp_in_coord(s, &os->cx, delta);
274
275 if (present & 0x0008)
276 rdp_in_coord(s, &os->cy, delta);
277
278 if (present & 0x0010)
279 in_uint8(s, os->opcode);
280
281 if (present & 0x0020)
282 rdp_in_coord(s, &os->srcx, delta);
283
284 if (present & 0x0040)
285 rdp_in_coord(s, &os->srcy, delta);
286
287 DEBUG(("SCREENBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,srcx=%d,srcy=%d)\n",
288 os->opcode, os->x, os->y, os->cx, os->cy, os->srcx, os->srcy));
289
290 ui_screenblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, os->srcx, os->srcy);
291 }
292
293 /* Process a line order */
294 static void
295 process_line(STREAM s, LINE_ORDER * os, uint32 present, RD_BOOL delta)
296 {
297 if (present & 0x0001)
298 in_uint16_le(s, os->mixmode);
299
300 if (present & 0x0002)
301 rdp_in_coord(s, &os->startx, delta);
302
303 if (present & 0x0004)
304 rdp_in_coord(s, &os->starty, delta);
305
306 if (present & 0x0008)
307 rdp_in_coord(s, &os->endx, delta);
308
309 if (present & 0x0010)
310 rdp_in_coord(s, &os->endy, delta);
311
312 if (present & 0x0020)
313 rdp_in_colour(s, &os->bgcolour);
314
315 if (present & 0x0040)
316 in_uint8(s, os->opcode);
317
318 rdp_parse_pen(s, &os->pen, present >> 7);
319
320 DEBUG(("LINE(op=0x%x,sx=%d,sy=%d,dx=%d,dy=%d,fg=0x%x)\n",
321 os->opcode, os->startx, os->starty, os->endx, os->endy, os->pen.colour));
322
323 if (os->opcode < 0x01 || os->opcode > 0x10)
324 {
325 error("bad ROP2 0x%x\n", os->opcode);
326 return;
327 }
328
329 ui_line(os->opcode - 1, os->startx, os->starty, os->endx, os->endy, &os->pen);
330 }
331
332 /* Process an opaque rectangle order */
333 static void
334 process_rect(STREAM s, RECT_ORDER * os, uint32 present, RD_BOOL delta)
335 {
336 uint32 i;
337 if (present & 0x01)
338 rdp_in_coord(s, &os->x, delta);
339
340 if (present & 0x02)
341 rdp_in_coord(s, &os->y, delta);
342
343 if (present & 0x04)
344 rdp_in_coord(s, &os->cx, delta);
345
346 if (present & 0x08)
347 rdp_in_coord(s, &os->cy, delta);
348
349 if (present & 0x10)
350 {
351 in_uint8(s, i);
352 os->colour = (os->colour & 0xffffff00) | i;
353 }
354
355 if (present & 0x20)
356 {
357 in_uint8(s, i);
358 os->colour = (os->colour & 0xffff00ff) | (i << 8);
359 }
360
361 if (present & 0x40)
362 {
363 in_uint8(s, i);
364 os->colour = (os->colour & 0xff00ffff) | (i << 16);
365 }
366
367 DEBUG(("RECT(x=%d,y=%d,cx=%d,cy=%d,fg=0x%x)\n", os->x, os->y, os->cx, os->cy, os->colour));
368
369 ui_rect(os->x, os->y, os->cx, os->cy, os->colour);
370 }
371
372 /* Process a desktop save order */
373 static void
374 process_desksave(STREAM s, DESKSAVE_ORDER * os, uint32 present, RD_BOOL delta)
375 {
376 int width, height;
377
378 if (present & 0x01)
379 in_uint32_le(s, os->offset);
380
381 if (present & 0x02)
382 rdp_in_coord(s, &os->left, delta);
383
384 if (present & 0x04)
385 rdp_in_coord(s, &os->top, delta);
386
387 if (present & 0x08)
388 rdp_in_coord(s, &os->right, delta);
389
390 if (present & 0x10)
391 rdp_in_coord(s, &os->bottom, delta);
392
393 if (present & 0x20)
394 in_uint8(s, os->action);
395
396 DEBUG(("DESKSAVE(l=%d,t=%d,r=%d,b=%d,off=%d,op=%d)\n",
397 os->left, os->top, os->right, os->bottom, os->offset, os->action));
398
399 width = os->right - os->left + 1;
400 height = os->bottom - os->top + 1;
401
402 if (os->action == 0)
403 ui_desktop_save(os->offset, os->left, os->top, width, height);
404 else
405 ui_desktop_restore(os->offset, os->left, os->top, width, height);
406 }
407
408 /* Process a memory blt order */
409 static void
410 process_memblt(STREAM s, MEMBLT_ORDER * os, uint32 present, RD_BOOL delta)
411 {
412 RD_HBITMAP bitmap;
413
414 if (present & 0x0001)
415 {
416 in_uint8(s, os->cache_id);
417 in_uint8(s, os->colour_table);
418 }
419
420 if (present & 0x0002)
421 rdp_in_coord(s, &os->x, delta);
422
423 if (present & 0x0004)
424 rdp_in_coord(s, &os->y, delta);
425
426 if (present & 0x0008)
427 rdp_in_coord(s, &os->cx, delta);
428
429 if (present & 0x0010)
430 rdp_in_coord(s, &os->cy, delta);
431
432 if (present & 0x0020)
433 in_uint8(s, os->opcode);
434
435 if (present & 0x0040)
436 rdp_in_coord(s, &os->srcx, delta);
437
438 if (present & 0x0080)
439 rdp_in_coord(s, &os->srcy, delta);
440
441 if (present & 0x0100)
442 in_uint16_le(s, os->cache_idx);
443
444 DEBUG(("MEMBLT(op=0x%x,x=%d,y=%d,cx=%d,cy=%d,id=%d,idx=%d)\n",
445 os->opcode, os->x, os->y, os->cx, os->cy, os->cache_id, os->cache_idx));
446
447 bitmap = cache_get_bitmap(os->cache_id, os->cache_idx);
448 if (bitmap == NULL)
449 return;
450
451 ui_memblt(ROP2_S(os->opcode), os->x, os->y, os->cx, os->cy, bitmap, os->srcx, os->srcy);
452 }
453
454 /* Process a 3-way blt order */
455 static void
456 process_triblt(STREAM s, TRIBLT_ORDER * os, uint32 present, RD_BOOL delta)
457 {
458 RD_HBITMAP bitmap;
459 BRUSH brush;
460
461 if (present & 0x000001)
462 {
463 in_uint8(s, os->cache_id);
464 in_uint8(s, os->colour_table);
465 }
466
467 if (present & 0x000002)
468 rdp_in_coord(s, &os->x, delta);
469
470 if (present & 0x000004)
471 rdp_in_coord(s, &os->y, delta);
472
473 if (present & 0x000008)
474 rdp_in_coord(s, &os->cx, delta);
475
476 if (present & 0x000010)
477 rdp_in_coord(s, &os->cy, delta);
478
479 if (present & 0x000020)
480 in_uint8(s, os->opcode);
481
482 if (present & 0x000040)
483 rdp_in_coord(s, &os->srcx, delta);
484
485 if (present & 0x000080)
486 rdp_in_coord(s, &os->srcy, delta);
487
488 if (present & 0x000100)
489 rdp_in_colour(s, &os->bgcolour);
490
491 if (present & 0x000200)
492 rdp_in_colour(s, &os->fgcolour);
493
494 rdp_parse_brush(s, &os->brush, present >> 10);
495
496 if (present & 0x008000)
497 in_uint16_le(s, os->cache_idx);
498
499 if (present & 0x010000)
500 in_uint16_le(s, os->unknown);
501
502 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",
503 os->opcode, os->x, os->y, os->cx, os->cy, os->cache_id, os->cache_idx,
504 os->brush.style, os->bgcolour, os->fgcolour));
505
506 bitmap = cache_get_bitmap(os->cache_id, os->cache_idx);
507 if (bitmap == NULL)
508 return;
509
510 setup_brush(&brush, &os->brush);
511
512 ui_triblt(os->opcode, os->x, os->y, os->cx, os->cy,
513 bitmap, os->srcx, os->srcy, &brush, os->bgcolour, os->fgcolour);
514 }
515
516 /* Process a polygon order */
517 static void
518 process_polygon(STREAM s, POLYGON_ORDER * os, uint32 present, RD_BOOL delta)
519 {
520 int index, data, next;
521 uint8 flags = 0;
522 RD_POINT *points;
523
524 if (present & 0x01)
525 rdp_in_coord(s, &os->x, delta);
526
527 if (present & 0x02)
528 rdp_in_coord(s, &os->y, delta);
529
530 if (present & 0x04)
531 in_uint8(s, os->opcode);
532
533 if (present & 0x08)
534 in_uint8(s, os->fillmode);
535
536 if (present & 0x10)
537 rdp_in_colour(s, &os->fgcolour);
538
539 if (present & 0x20)
540 in_uint8(s, os->npoints);
541
542 if (present & 0x40)
543 {
544 in_uint8(s, os->datasize);
545 in_uint8a(s, os->data, os->datasize);
546 }
547
548 DEBUG(("POLYGON(x=%d,y=%d,op=0x%x,fm=%d,fg=0x%x,n=%d,sz=%d)\n",
549 os->x, os->y, os->opcode, os->fillmode, os->fgcolour, os->npoints, os->datasize));
550
551 DEBUG(("Data: "));
552
553 for (index = 0; index < os->datasize; index++)
554 DEBUG(("%02x ", os->data[index]));
555
556 DEBUG(("\n"));
557
558 if (os->opcode < 0x01 || os->opcode > 0x10)
559 {
560 error("bad ROP2 0x%x\n", os->opcode);
561 return;
562 }
563
564 points = (RD_POINT *) xmalloc((os->npoints + 1) * sizeof(RD_POINT));
565 memset(points, 0, (os->npoints + 1) * sizeof(RD_POINT));
566
567 points[0].x = os->x;
568 points[0].y = os->y;
569
570 index = 0;
571 data = ((os->npoints - 1) / 4) + 1;
572 for (next = 1; (next <= os->npoints) && (next < 256) && (data < os->datasize); next++)
573 {
574 if ((next - 1) % 4 == 0)
575 flags = os->data[index++];
576
577 if (~flags & 0x80)
578 points[next].x = parse_delta(os->data, &data);
579
580 if (~flags & 0x40)
581 points[next].y = parse_delta(os->data, &data);
582
583 flags <<= 2;
584 }
585
586 if (next - 1 == os->npoints)
587 ui_polygon(os->opcode - 1, os->fillmode, points, os->npoints + 1, NULL, 0,
588 os->fgcolour);
589 else
590 error("polygon parse error\n");
591
592 xfree(points);
593 }
594
595 /* Process a polygon2 order */
596 static void
597 process_polygon2(STREAM s, POLYGON2_ORDER * os, uint32 present, RD_BOOL delta)
598 {
599 int index, data, next;
600 uint8 flags = 0;
601 RD_POINT *points;
602 BRUSH brush;
603
604 if (present & 0x0001)
605 rdp_in_coord(s, &os->x, delta);
606
607 if (present & 0x0002)
608 rdp_in_coord(s, &os->y, delta);
609
610 if (present & 0x0004)
611 in_uint8(s, os->opcode);
612
613 if (present & 0x0008)
614 in_uint8(s, os->fillmode);
615
616 if (present & 0x0010)
617 rdp_in_colour(s, &os->bgcolour);
618
619 if (present & 0x0020)
620 rdp_in_colour(s, &os->fgcolour);
621
622 rdp_parse_brush(s, &os->brush, present >> 6);
623
624 if (present & 0x0800)
625 in_uint8(s, os->npoints);
626
627 if (present & 0x1000)
628 {
629 in_uint8(s, os->datasize);
630 in_uint8a(s, os->data, os->datasize);
631 }
632
633 DEBUG(("POLYGON2(x=%d,y=%d,op=0x%x,fm=%d,bs=%d,bg=0x%x,fg=0x%x,n=%d,sz=%d)\n",
634 os->x, os->y, os->opcode, os->fillmode, os->brush.style, os->bgcolour, os->fgcolour,
635 os->npoints, os->datasize));
636
637 DEBUG(("Data: "));
638
639 for (index = 0; index < os->datasize; index++)
640 DEBUG(("%02x ", os->data[index]));
641
642 DEBUG(("\n"));
643
644 if (os->opcode < 0x01 || os->opcode > 0x10)
645 {
646 error("bad ROP2 0x%x\n", os->opcode);
647 return;
648 }
649
650 setup_brush(&brush, &os->brush);
651
652 points = (RD_POINT *) xmalloc((os->npoints + 1) * sizeof(RD_POINT));
653 memset(points, 0, (os->npoints + 1) * sizeof(RD_POINT));
654
655 points[0].x = os->x;
656 points[0].y = os->y;
657
658 index = 0;
659 data = ((os->npoints - 1) / 4) + 1;
660 for (next = 1; (next <= os->npoints) && (next < 256) && (data < os->datasize); next++)
661 {
662 if ((next - 1) % 4 == 0)
663 flags = os->data[index++];
664
665 if (~flags & 0x80)
666 points[next].x = parse_delta(os->data, &data);
667
668 if (~flags & 0x40)
669 points[next].y = parse_delta(os->data, &data);
670
671 flags <<= 2;
672 }
673
674 if (next - 1 == os->npoints)
675 ui_polygon(os->opcode - 1, os->fillmode, points, os->npoints + 1,
676 &brush, os->bgcolour, os->fgcolour);
677 else
678 error("polygon2 parse error\n");
679
680 xfree(points);
681 }
682
683 /* Process a polyline order */
684 static void
685 process_polyline(STREAM s, POLYLINE_ORDER * os, uint32 present, RD_BOOL delta)
686 {
687 int index, next, data;
688 uint8 flags = 0;
689 PEN pen;
690 RD_POINT *points;
691
692 if (present & 0x01)
693 rdp_in_coord(s, &os->x, delta);
694
695 if (present & 0x02)
696 rdp_in_coord(s, &os->y, delta);
697
698 if (present & 0x04)
699 in_uint8(s, os->opcode);
700
701 if (present & 0x10)
702 rdp_in_colour(s, &os->fgcolour);
703
704 if (present & 0x20)
705 in_uint8(s, os->lines);
706
707 if (present & 0x40)
708 {
709 in_uint8(s, os->datasize);
710 in_uint8a(s, os->data, os->datasize);
711 }
712
713 DEBUG(("POLYLINE(x=%d,y=%d,op=0x%x,fg=0x%x,n=%d,sz=%d)\n",
714 os->x, os->y, os->opcode, os->fgcolour, os->lines, os->datasize));
715
716 DEBUG(("Data: "));
717
718 for (index = 0; index < os->datasize; index++)
719 DEBUG(("%02x ", os->data[index]));
720
721 DEBUG(("\n"));
722
723 if (os->opcode < 0x01 || os->opcode > 0x10)
724 {
725 error("bad ROP2 0x%x\n", os->opcode);
726 return;
727 }
728
729 points = (RD_POINT *) xmalloc((os->lines + 1) * sizeof(RD_POINT));
730 memset(points, 0, (os->lines + 1) * sizeof(RD_POINT));
731
732 points[0].x = os->x;
733 points[0].y = os->y;
734 pen.style = pen.width = 0;
735 pen.colour = os->fgcolour;
736
737 index = 0;
738 data = ((os->lines - 1) / 4) + 1;
739 for (next = 1; (next <= os->lines) && (data < os->datasize); next++)
740 {
741 if ((next - 1) % 4 == 0)
742 flags = os->data[index++];
743
744 if (~flags & 0x80)
745 points[next].x = parse_delta(os->data, &data);
746
747 if (~flags & 0x40)
748 points[next].y = parse_delta(os->data, &data);
749
750 flags <<= 2;
751 }
752
753 if (next - 1 == os->lines)
754 ui_polyline(os->opcode - 1, points, os->lines + 1, &pen);
755 else
756 error("polyline parse error\n");
757
758 xfree(points);
759 }
760
761 /* Process an ellipse order */
762 static void
763 process_ellipse(STREAM s, ELLIPSE_ORDER * os, uint32 present, RD_BOOL delta)
764 {
765 if (present & 0x01)
766 rdp_in_coord(s, &os->left, delta);
767
768 if (present & 0x02)
769 rdp_in_coord(s, &os->top, delta);
770
771 if (present & 0x04)
772 rdp_in_coord(s, &os->right, delta);
773
774 if (present & 0x08)
775 rdp_in_coord(s, &os->bottom, delta);
776
777 if (present & 0x10)
778 in_uint8(s, os->opcode);
779
780 if (present & 0x20)
781 in_uint8(s, os->fillmode);
782
783 if (present & 0x40)
784 rdp_in_colour(s, &os->fgcolour);
785
786 DEBUG(("ELLIPSE(l=%d,t=%d,r=%d,b=%d,op=0x%x,fm=%d,fg=0x%x)\n", os->left, os->top,
787 os->right, os->bottom, os->opcode, os->fillmode, os->fgcolour));
788
789 ui_ellipse(os->opcode - 1, os->fillmode, os->left, os->top, os->right - os->left,
790 os->bottom - os->top, NULL, 0, os->fgcolour);
791 }
792
793 /* Process an ellipse2 order */
794 static void
795 process_ellipse2(STREAM s, ELLIPSE2_ORDER * os, uint32 present, RD_BOOL delta)
796 {
797 BRUSH brush;
798
799 if (present & 0x0001)
800 rdp_in_coord(s, &os->left, delta);
801
802 if (present & 0x0002)
803 rdp_in_coord(s, &os->top, delta);
804
805 if (present & 0x0004)
806 rdp_in_coord(s, &os->right, delta);
807
808 if (present & 0x0008)
809 rdp_in_coord(s, &os->bottom, delta);
810
811 if (present & 0x0010)
812 in_uint8(s, os->opcode);
813
814 if (present & 0x0020)
815 in_uint8(s, os->fillmode);
816
817 if (present & 0x0040)
818 rdp_in_colour(s, &os->bgcolour);
819
820 if (present & 0x0080)
821 rdp_in_colour(s, &os->fgcolour);
822
823 rdp_parse_brush(s, &os->brush, present >> 8);
824
825 DEBUG(("ELLIPSE2(l=%d,t=%d,r=%d,b=%d,op=0x%x,fm=%d,bs=%d,bg=0x%x,fg=0x%x)\n",
826 os->left, os->top, os->right, os->bottom, os->opcode, os->fillmode, os->brush.style,
827 os->bgcolour, os->fgcolour));
828
829 setup_brush(&brush, &os->brush);
830
831 ui_ellipse(os->opcode - 1, os->fillmode, os->left, os->top, os->right - os->left,
832 os->bottom - os->top, &brush, os->bgcolour, os->fgcolour);
833 }
834
835 /* Process a text order */
836 static void
837 process_text2(STREAM s, TEXT2_ORDER * os, uint32 present, RD_BOOL delta)
838 {
839 int i;
840 BRUSH brush;
841
842 if (present & 0x000001)
843 in_uint8(s, os->font);
844
845 if (present & 0x000002)
846 in_uint8(s, os->flags);
847
848 if (present & 0x000004)
849 in_uint8(s, os->opcode);
850
851 if (present & 0x000008)
852 in_uint8(s, os->mixmode);
853
854 if (present & 0x000010)
855 rdp_in_colour(s, &os->fgcolour);
856
857 if (present & 0x000020)
858 rdp_in_colour(s, &os->bgcolour);
859
860 if (present & 0x000040)
861 in_uint16_le(s, os->clipleft);
862
863 if (present & 0x000080)
864 in_uint16_le(s, os->cliptop);
865
866 if (present & 0x000100)
867 in_uint16_le(s, os->clipright);
868
869 if (present & 0x000200)
870 in_uint16_le(s, os->clipbottom);
871
872 if (present & 0x000400)
873 in_uint16_le(s, os->boxleft);
874
875 if (present & 0x000800)
876 in_uint16_le(s, os->boxtop);
877
878 if (present & 0x001000)
879 in_uint16_le(s, os->boxright);
880
881 if (present & 0x002000)
882 in_uint16_le(s, os->boxbottom);
883
884 rdp_parse_brush(s, &os->brush, present >> 14);
885
886 if (present & 0x080000)
887 in_uint16_le(s, os->x);
888
889 if (present & 0x100000)
890 in_uint16_le(s, os->y);
891
892 if (present & 0x200000)
893 {
894 in_uint8(s, os->length);
895 in_uint8a(s, os->text, os->length);
896 }
897
898 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));
899
900 DEBUG(("Text: "));
901
902 for (i = 0; i < os->length; i++)
903 DEBUG(("%02x ", os->text[i]));
904
905 DEBUG(("\n"));
906
907 setup_brush(&brush, &os->brush);
908
909 ui_draw_text(os->font, os->flags, os->opcode - 1, os->mixmode, os->x, os->y,
910 os->clipleft, os->cliptop, os->clipright - os->clipleft,
911 os->clipbottom - os->cliptop, os->boxleft, os->boxtop,
912 os->boxright - os->boxleft, os->boxbottom - os->boxtop,
913 &brush, os->bgcolour, os->fgcolour, os->text, os->length);
914 }
915
916 /* Process a raw bitmap cache order */
917 static void
918 process_raw_bmpcache(STREAM s)
919 {
920 RD_HBITMAP bitmap;
921 uint16 cache_idx, bufsize;
922 uint8 cache_id, width, height, bpp, Bpp;
923 uint8 *data, *inverted;
924 int y;
925
926 in_uint8(s, cache_id);
927 in_uint8s(s, 1); /* pad */
928 in_uint8(s, width);
929 in_uint8(s, height);
930 in_uint8(s, bpp);
931 Bpp = (bpp + 7) / 8;
932 in_uint16_le(s, bufsize);
933 in_uint16_le(s, cache_idx);
934 in_uint8p(s, data, bufsize);
935
936 DEBUG(("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n", width, height, cache_id, cache_idx));
937 inverted = (uint8 *) xmalloc(width * height * Bpp);
938 for (y = 0; y < height; y++)
939 {
940 memcpy(&inverted[(height - y - 1) * (width * Bpp)], &data[y * (width * Bpp)],
941 width * Bpp);
942 }
943
944 bitmap = ui_create_bitmap(width, height, inverted);
945 xfree(inverted);
946 cache_put_bitmap(cache_id, cache_idx, bitmap);
947 }
948
949 /* Process a bitmap cache order */
950 static void
951 process_bmpcache(STREAM s)
952 {
953 RD_HBITMAP bitmap;
954 uint16 cache_idx, size;
955 uint8 cache_id, width, height, bpp, Bpp;
956 uint8 *data, *bmpdata;
957 uint16 bufsize, pad2, row_size, final_size;
958 uint8 pad1;
959
960 pad2 = row_size = final_size = 0xffff; /* Shut the compiler up */
961
962 in_uint8(s, cache_id);
963 in_uint8(s, pad1); /* pad */
964 in_uint8(s, width);
965 in_uint8(s, height);
966 in_uint8(s, bpp);
967 Bpp = (bpp + 7) / 8;
968 in_uint16_le(s, bufsize); /* bufsize */
969 in_uint16_le(s, cache_idx);
970
971 if (g_rdp_version >= RDP_V5)
972 {
973 size = bufsize;
974 }
975 else
976 {
977
978 /* Begin compressedBitmapData */
979 in_uint16_le(s, pad2); /* pad */
980 in_uint16_le(s, size);
981 /* in_uint8s(s, 4); *//* row_size, final_size */
982 in_uint16_le(s, row_size);
983 in_uint16_le(s, final_size);
984
985 }
986 in_uint8p(s, data, size);
987
988 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));
989
990 bmpdata = (uint8 *) xmalloc(width * height * Bpp);
991
992 if (bitmap_decompress(bmpdata, width, height, data, size, Bpp))
993 {
994 bitmap = ui_create_bitmap(width, height, bmpdata);
995 cache_put_bitmap(cache_id, cache_idx, bitmap);
996 }
997 else
998 {
999 DEBUG(("Failed to decompress bitmap data\n"));
1000 }
1001 if (pad1 || pad2) {}
1002
1003 xfree(bmpdata);
1004 }
1005
1006 /* Process a bitmap cache v2 order */
1007 static void
1008 process_bmpcache2(STREAM s, uint16 flags, RD_BOOL compressed)
1009 {
1010 RD_HBITMAP bitmap;
1011 int y;
1012 uint8 cache_id, cache_idx_low, width, height, Bpp;
1013 uint16 cache_idx, bufsize;
1014 uint8 *data, *bmpdata, *bitmap_id;
1015
1016 bitmap_id = NULL; /* prevent compiler warning */
1017 cache_id = flags & ID_MASK;
1018 Bpp = ((flags & MODE_MASK) >> MODE_SHIFT) - 2;
1019
1020 if (flags & PERSIST)
1021 {
1022 in_uint8p(s, bitmap_id, 8);
1023 }
1024
1025 if (flags & SQUARE)
1026 {
1027 in_uint8(s, width);
1028 height = width;
1029 }
1030 else
1031 {
1032 in_uint8(s, width);
1033 in_uint8(s, height);
1034 }
1035
1036 in_uint16_be(s, bufsize);
1037 bufsize &= BUFSIZE_MASK;
1038 in_uint8(s, cache_idx);
1039
1040 if (cache_idx & LONG_FORMAT)
1041 {
1042 in_uint8(s, cache_idx_low);
1043 cache_idx = ((cache_idx ^ LONG_FORMAT) << 8) + cache_idx_low;
1044 }
1045
1046 in_uint8p(s, data, bufsize);
1047
1048 DEBUG(("BMPCACHE2(compr=%d,flags=%x,cx=%d,cy=%d,id=%d,idx=%d,Bpp=%d,bs=%d)\n",
1049 compressed, flags, width, height, cache_id, cache_idx, Bpp, bufsize));
1050
1051 bmpdata = (uint8 *) xmalloc(width * height * Bpp);
1052
1053 if (compressed)
1054 {
1055 if (!bitmap_decompress(bmpdata, width, height, data, bufsize, Bpp))
1056 {
1057 DEBUG(("Failed to decompress bitmap data\n"));
1058 xfree(bmpdata);
1059 return;
1060 }
1061 }
1062 else
1063 {
1064 for (y = 0; y < height; y++)
1065 memcpy(&bmpdata[(height - y - 1) * (width * Bpp)],
1066 &data[y * (width * Bpp)], width * Bpp);
1067 }
1068
1069 bitmap = ui_create_bitmap(width, height, bmpdata);
1070
1071 if (bitmap)
1072 {
1073 cache_put_bitmap(cache_id, cache_idx, bitmap);
1074 if (flags & PERSIST)
1075 pstcache_save_bitmap(cache_id, cache_idx, bitmap_id, width, height,
1076 width * height * Bpp, bmpdata);
1077 }
1078 else
1079 {
1080 DEBUG(("process_bmpcache2: ui_create_bitmap failed\n"));
1081 }
1082
1083 xfree(bmpdata);
1084 }
1085
1086 /* Process a colourmap cache order */
1087 static void
1088 process_colcache(STREAM s)
1089 {
1090 COLOURENTRY *entry;
1091 COLOURMAP map;
1092 RD_HCOLOURMAP hmap;
1093 uint8 cache_id;
1094 int i;
1095
1096 in_uint8(s, cache_id);
1097 in_uint16_le(s, map.ncolours);
1098
1099 map.colours = (COLOURENTRY *) xmalloc(sizeof(COLOURENTRY) * map.ncolours);
1100
1101 for (i = 0; i < map.ncolours; i++)
1102 {
1103 entry = &map.colours[i];
1104 in_uint8(s, entry->blue);
1105 in_uint8(s, entry->green);
1106 in_uint8(s, entry->red);
1107 in_uint8s(s, 1); /* pad */
1108 }
1109
1110 DEBUG(("COLCACHE(id=%d,n=%d)\n", cache_id, map.ncolours));
1111
1112 hmap = ui_create_colourmap(&map);
1113
1114 if (cache_id)
1115 ui_set_colourmap(hmap);
1116
1117 xfree(map.colours);
1118 }
1119
1120 /* Process a font cache order */
1121 static void
1122 process_fontcache(STREAM s)
1123 {
1124 RD_HGLYPH bitmap;
1125 uint8 font, nglyphs;
1126 uint16 character, offset, baseline, width, height;
1127 int i, datasize;
1128 uint8 *data;
1129
1130 in_uint8(s, font);
1131 in_uint8(s, nglyphs);
1132
1133 DEBUG(("FONTCACHE(font=%d,n=%d)\n", font, nglyphs));
1134
1135 for (i = 0; i < nglyphs; i++)
1136 {
1137 in_uint16_le(s, character);
1138 in_uint16_le(s, offset);
1139 in_uint16_le(s, baseline);
1140 in_uint16_le(s, width);
1141 in_uint16_le(s, height);
1142
1143 datasize = (height * ((width + 7) / 8) + 3) & ~3;
1144 in_uint8p(s, data, datasize);
1145
1146 bitmap = ui_create_glyph(width, height, data);
1147 cache_put_font(font, character, offset, baseline, width, height, bitmap);
1148 }
1149 }
1150
1151 static void
1152 process_compressed_8x8_brush_data(uint8 * in, uint8 * out, int Bpp)
1153 {
1154 int x, y, pal_index, in_index, shift, do2, i;
1155 uint8 *pal;
1156
1157 in_index = 0;
1158 pal = in + 16;
1159 /* read it bottom up */
1160 for (y = 7; y >= 0; y--)
1161 {
1162 /* 2 bytes per row */
1163 x = 0;
1164 for (do2 = 0; do2 < 2; do2++)
1165 {
1166 /* 4 pixels per byte */
1167 shift = 6;
1168 while (shift >= 0)
1169 {
1170 pal_index = (in[in_index] >> shift) & 3;
1171 /* size of palette entries depends on Bpp */
1172 for (i = 0; i < Bpp; i++)
1173 {
1174 out[(y * 8 + x) * Bpp + i] = pal[pal_index * Bpp + i];
1175 }
1176 x++;
1177 shift -= 2;
1178 }
1179 in_index++;
1180 }
1181 }
1182 }
1183
1184 /* Process a brush cache order */
1185 static void
1186 process_brushcache(STREAM s, uint16 flags)
1187 {
1188 BRUSHDATA brush_data;
1189 uint8 cache_idx, colour_code, width, height, size, type;
1190 uint8 *comp_brush;
1191 int index;
1192 int Bpp;
1193
1194 in_uint8(s, cache_idx);
1195 in_uint8(s, colour_code);
1196 in_uint8(s, width);
1197 in_uint8(s, height);
1198 in_uint8(s, type); /* type, 0x8x = cached */
1199 in_uint8(s, size);
1200
1201 DEBUG(("BRUSHCACHE(idx=%d,wd=%d,ht=%d,sz=%d)\n", cache_idx, width, height, size));
1202
1203 if ((width == 8) && (height == 8))
1204 {
1205 if (colour_code == 1)
1206 {
1207 brush_data.colour_code = 1;
1208 brush_data.data_size = 8;
1209 brush_data.data = xmalloc(8);
1210 if (size == 8)
1211 {
1212 /* read it bottom up */
1213 for (index = 7; index >= 0; index--)
1214 {
1215 in_uint8(s, brush_data.data[index]);
1216 }
1217 }
1218 else
1219 {
1220 warning("incompatible brush, colour_code %d size %d\n", colour_code,
1221 size);
1222 }
1223 cache_put_brush_data(1, cache_idx, &brush_data);
1224 }
1225 else if ((colour_code >= 3) && (colour_code <= 6))
1226 {
1227 Bpp = colour_code - 2;
1228 brush_data.colour_code = colour_code;
1229 brush_data.data_size = 8 * 8 * Bpp;
1230 brush_data.data = xmalloc(8 * 8 * Bpp);
1231 if (size == 16 + 4 * Bpp)
1232 {
1233 in_uint8p(s, comp_brush, 16 + 4 * Bpp);
1234 process_compressed_8x8_brush_data(comp_brush, brush_data.data, Bpp);
1235 }
1236 else
1237 {
1238 in_uint8a(s, brush_data.data, 8 * 8 * Bpp);
1239 }
1240 cache_put_brush_data(colour_code, cache_idx, &brush_data);
1241 }
1242 else
1243 {
1244 warning("incompatible brush, colour_code %d size %d\n", colour_code, size);
1245 }
1246 }
1247 else
1248 {
1249 warning("incompatible brush, width height %d %d\n", width, height);
1250 }
1251 if (type) {}
1252 }
1253
1254 /* Process a secondary order */
1255 static void
1256 process_secondary_order(STREAM s)
1257 {
1258 /* The length isn't calculated correctly by the server.
1259 * For very compact orders the length becomes negative
1260 * so a signed integer must be used. */
1261 uint16 length;
1262 uint16 flags;
1263 uint8 type;
1264 uint8 *next_order;
1265
1266 in_uint16_le(s, length);
1267 in_uint16_le(s, flags); /* used by bmpcache2 */
1268 in_uint8(s, type);
1269
1270 next_order = s->p + (sint16) length + 7;
1271
1272 switch (type)
1273 {
1274 case RDP_ORDER_RAW_BMPCACHE:
1275 process_raw_bmpcache(s);
1276 break;
1277
1278 case RDP_ORDER_COLCACHE:
1279 process_colcache(s);
1280 break;
1281
1282 case RDP_ORDER_BMPCACHE:
1283 process_bmpcache(s);
1284 break;
1285
1286 case RDP_ORDER_FONTCACHE:
1287 process_fontcache(s);
1288 break;
1289
1290 case RDP_ORDER_RAW_BMPCACHE2:
1291 process_bmpcache2(s, flags, False); /* uncompressed */
1292 break;
1293
1294 case RDP_ORDER_BMPCACHE2:
1295 process_bmpcache2(s, flags, True); /* compressed */
1296 break;
1297
1298 case RDP_ORDER_BRUSHCACHE:
1299 process_brushcache(s, flags);
1300 break;
1301
1302 default:
1303 unimpl("secondary order %d\n", type);
1304 }
1305
1306 s->p = next_order;
1307 }
1308
1309 /* Process an order PDU */
1310 void
1311 process_orders(STREAM s, uint16 num_orders)
1312 {
1313 RDP_ORDER_STATE *os = &g_order_state;
1314 uint32 present;
1315 uint8 order_flags;
1316 int size, processed = 0;
1317 RD_BOOL delta;
1318
1319 while (processed < num_orders)
1320 {
1321 in_uint8(s, order_flags);
1322
1323 if (!(order_flags & RDP_ORDER_STANDARD))
1324 {
1325 error("order parsing failed\n");
1326 break;
1327 }
1328
1329 if (order_flags & RDP_ORDER_SECONDARY)
1330 {
1331 process_secondary_order(s);
1332 }
1333 else
1334 {
1335 if (order_flags & RDP_ORDER_CHANGE)
1336 {
1337 in_uint8(s, os->order_type);
1338 }
1339
1340 switch (os->order_type)
1341 {
1342 case RDP_ORDER_TRIBLT:
1343 case RDP_ORDER_TEXT2:
1344 size = 3;
1345 break;
1346
1347 case RDP_ORDER_PATBLT:
1348 case RDP_ORDER_MEMBLT:
1349 case RDP_ORDER_LINE:
1350 case RDP_ORDER_POLYGON2:
1351 case RDP_ORDER_ELLIPSE2:
1352 size = 2;
1353 break;
1354
1355 default:
1356 size = 1;
1357 }
1358
1359 rdp_in_present(s, &present, order_flags, size);
1360
1361 if (order_flags & RDP_ORDER_BOUNDS)
1362 {
1363 if (!(order_flags & RDP_ORDER_LASTBOUNDS))
1364 rdp_parse_bounds(s, &os->bounds);
1365
1366 ui_set_clip(os->bounds.left,
1367 os->bounds.top,
1368 os->bounds.right -
1369 os->bounds.left + 1,
1370 os->bounds.bottom - os->bounds.top + 1);
1371 }
1372
1373 delta = order_flags & RDP_ORDER_DELTA;
1374
1375 switch (os->order_type)
1376 {
1377 case RDP_ORDER_DESTBLT:
1378 process_destblt(s, &os->destblt, present, delta);
1379 break;
1380
1381 case RDP_ORDER_PATBLT:
1382 process_patblt(s, &os->patblt, present, delta);
1383 break;
1384
1385 case RDP_ORDER_SCREENBLT:
1386 process_screenblt(s, &os->screenblt, present, delta);
1387 break;
1388
1389 case RDP_ORDER_LINE:
1390 process_line(s, &os->line, present, delta);
1391 break;
1392
1393 case RDP_ORDER_RECT:
1394 process_rect(s, &os->rect, present, delta);
1395 break;
1396
1397 case RDP_ORDER_DESKSAVE:
1398 process_desksave(s, &os->desksave, present, delta);
1399 break;
1400
1401 case RDP_ORDER_MEMBLT:
1402 process_memblt(s, &os->memblt, present, delta);
1403 break;
1404
1405 case RDP_ORDER_TRIBLT:
1406 process_triblt(s, &os->triblt, present, delta);
1407 break;
1408
1409 case RDP_ORDER_POLYGON:
1410 process_polygon(s, &os->polygon, present, delta);
1411 break;
1412
1413 case RDP_ORDER_POLYGON2:
1414 process_polygon2(s, &os->polygon2, present, delta);
1415 break;
1416
1417 case RDP_ORDER_POLYLINE:
1418 process_polyline(s, &os->polyline, present, delta);
1419 break;
1420
1421 case RDP_ORDER_ELLIPSE:
1422 process_ellipse(s, &os->ellipse, present, delta);
1423 break;
1424
1425 case RDP_ORDER_ELLIPSE2:
1426 process_ellipse2(s, &os->ellipse2, present, delta);
1427 break;
1428
1429 case RDP_ORDER_TEXT2:
1430 process_text2(s, &os->text2, present, delta);
1431 break;
1432
1433 default:
1434 unimpl("order %d\n", os->order_type);
1435 return;
1436 }
1437
1438 if (order_flags & RDP_ORDER_BOUNDS)
1439 ui_reset_clip();
1440 }
1441
1442 processed++;
1443 }
1444 #if 0
1445 /* not true when RDP_COMPRESSION is set */
1446 if (s->p != g_next_packet)
1447 error("%d bytes remaining\n", (int) (g_next_packet - s->p));
1448 #endif
1449
1450 }
1451
1452 /* Reset order state */
1453 void
1454 reset_order_state(void)
1455 {
1456 memset(&g_order_state, 0, sizeof(g_order_state));
1457 g_order_state.order_type = RDP_ORDER_PATBLT;
1458 }