Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-core / orders.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Drawing Orders
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include "window.h"
21 #include <freerdp/api.h>
22 #include <freerdp/graphics.h>
23 #include <freerdp/codec/bitmap.h>
24
25 #include "orders.h"
26
27 #ifdef WITH_DEBUG_ORDERS
28
29 static const char* const PRIMARY_DRAWING_ORDER_STRINGS[] =
30 {
31         "DstBlt",
32         "PatBlt",
33         "ScrBlt",
34         "", "", "", "",
35         "DrawNineGrid",
36         "MultiDrawNineGrid",
37         "LineTo",
38         "OpaqueRect",
39         "SaveBitmap",
40         "",
41         "MemBlt",
42         "Mem3Blt",
43         "MultiDstBlt",
44         "MultiPatBlt",
45         "MultiScrBlt",
46         "MultiOpaqueRect",
47         "FastIndex",
48         "PolygonSC",
49         "PolygonCB",
50         "Polyline",
51         "",
52         "FastGlyph",
53         "EllipseSC",
54         "EllipseCB",
55         "GlyphIndex"
56 };
57
58 #define PRIMARY_DRAWING_ORDER_COUNT     (sizeof(PRIMARY_DRAWING_ORDER_STRINGS) / sizeof(PRIMARY_DRAWING_ORDER_STRINGS[0]))
59
60 static const char* const SECONDARY_DRAWING_ORDER_STRINGS[] =
61 {
62         "Cache Bitmap",
63         "Cache Color Table",
64         "Cache Bitmap (Compressed)",
65         "Cache Glyph",
66         "Cache Bitmap V2",
67         "Cache Bitmap V2 (Compressed)",
68         "",
69         "Cache Brush",
70         "Cache Bitmap V3"
71 };
72
73 #define SECONDARY_DRAWING_ORDER_COUNT   (sizeof(SECONDARY_DRAWING_ORDER_STRINGS) / sizeof(SECONDARY_DRAWING_ORDER_STRINGS[0]))
74
75 static const char* const ALTSEC_DRAWING_ORDER_STRINGS[] =
76 {
77         "Switch Surface",
78         "Create Offscreen Bitmap",
79         "Stream Bitmap First",
80         "Stream Bitmap Next",
81         "Create NineGrid Bitmap",
82         "Draw GDI+ First",
83         "Draw GDI+ Next",
84         "Draw GDI+ End",
85         "Draw GDI+ Cache First",
86         "Draw GDI+ Cache Next",
87         "Draw GDI+ Cache End",
88         "Windowing",
89         "Desktop Composition",
90         "Frame Marker"
91 };
92
93 #define ALTSEC_DRAWING_ORDER_COUNT      (sizeof(ALTSEC_DRAWING_ORDER_STRINGS) / sizeof(ALTSEC_DRAWING_ORDER_STRINGS[0]))
94
95 #endif /* WITH_DEBUG_ORDERS */
96
97 static const uint8 PRIMARY_DRAWING_ORDER_FIELD_BYTES[] =
98 {
99         DSTBLT_ORDER_FIELD_BYTES,
100         PATBLT_ORDER_FIELD_BYTES,
101         SCRBLT_ORDER_FIELD_BYTES,
102         0, 0, 0, 0,
103         DRAW_NINE_GRID_ORDER_FIELD_BYTES,
104         MULTI_DRAW_NINE_GRID_ORDER_FIELD_BYTES,
105         LINE_TO_ORDER_FIELD_BYTES,
106         OPAQUE_RECT_ORDER_FIELD_BYTES,
107         SAVE_BITMAP_ORDER_FIELD_BYTES,
108         0,
109         MEMBLT_ORDER_FIELD_BYTES,
110         MEM3BLT_ORDER_FIELD_BYTES,
111         MULTI_DSTBLT_ORDER_FIELD_BYTES,
112         MULTI_PATBLT_ORDER_FIELD_BYTES,
113         MULTI_SCRBLT_ORDER_FIELD_BYTES,
114         MULTI_OPAQUE_RECT_ORDER_FIELD_BYTES,
115         FAST_INDEX_ORDER_FIELD_BYTES,
116         POLYGON_SC_ORDER_FIELD_BYTES,
117         POLYGON_CB_ORDER_FIELD_BYTES,
118         POLYLINE_ORDER_FIELD_BYTES,
119         0,
120         FAST_GLYPH_ORDER_FIELD_BYTES,
121         ELLIPSE_SC_ORDER_FIELD_BYTES,
122         ELLIPSE_CB_ORDER_FIELD_BYTES,
123         GLYPH_INDEX_ORDER_FIELD_BYTES
124 };
125
126 static const uint8 CBR2_BPP[] =
127 {
128                 0, 0, 0, 8, 16, 24, 32
129 };
130
131 static const uint8 CBR23_BPP[] =
132 {
133                 0, 0, 0, 8, 16, 24, 32
134 };
135
136 static const uint8 BMF_BPP[] =
137 {
138                 0, 1, 0, 8, 16, 24, 32
139 };
140
141 INLINE void update_read_coord(STREAM* s, sint32* coord, boolean delta)
142 {
143         sint8 lsi8;
144         sint16 lsi16;
145
146         if (delta)
147         {
148                 stream_read_uint8(s, lsi8);
149                 *coord += lsi8;
150         }
151         else
152         {
153                 stream_read_uint16(s, lsi16);
154                 *coord = lsi16;
155         }
156 }
157
158 INLINE void update_read_color(STREAM* s, uint32* color)
159 {
160         uint8 byte;
161
162         stream_read_uint8(s, byte);
163         *color = byte;
164         stream_read_uint8(s, byte);
165         *color |= (byte << 8);
166         stream_read_uint8(s, byte);
167         *color |= (byte << 16);
168 }
169
170 INLINE void update_read_colorref(STREAM* s, uint32* color)
171 {
172         uint8 byte;
173
174         stream_read_uint8(s, byte);
175         *color = byte;
176         stream_read_uint8(s, byte);
177         *color |= (byte << 8);
178         stream_read_uint8(s, byte);
179         *color |= (byte << 16);
180         stream_seek_uint8(s);
181 }
182
183 INLINE void update_read_color_quad(STREAM* s, uint32* color)
184 {
185         uint8 byte;
186
187         stream_read_uint8(s, byte);
188         *color = (byte << 16);
189         stream_read_uint8(s, byte);
190         *color |= (byte << 8);
191         stream_read_uint8(s, byte);
192         *color |= byte;
193         stream_seek_uint8(s);
194 }
195
196 INLINE void update_read_2byte_unsigned(STREAM* s, uint32* value)
197 {
198         uint8 byte;
199
200         stream_read_uint8(s, byte);
201
202         if (byte & 0x80)
203         {
204                 *value = (byte & 0x7F) << 8;
205                 stream_read_uint8(s, byte);
206                 *value |= byte;
207         }
208         else
209         {
210                 *value = (byte & 0x7F);
211         }
212 }
213
214 INLINE void update_read_2byte_signed(STREAM* s, sint32* value)
215 {
216         uint8 byte;
217         boolean negative;
218
219         stream_read_uint8(s, byte);
220
221         negative = (byte & 0x40) ? true : false;
222
223         *value = (byte & 0x3F);
224
225         if (byte & 0x80)
226         {
227                 stream_read_uint8(s, byte);
228                 *value = (*value << 8) | byte;
229         }
230
231         if (negative)
232                 *value *= -1;
233 }
234
235 INLINE void update_read_4byte_unsigned(STREAM* s, uint32* value)
236 {
237         uint8 byte;
238         uint8 count;
239
240         stream_read_uint8(s, byte);
241
242         count = (byte & 0xC0) >> 6;
243
244         switch (count)
245         {
246                 case 0:
247                         *value = (byte & 0x3F);
248                         break;
249
250                 case 1:
251                         *value = (byte & 0x3F) << 8;
252                         stream_read_uint8(s, byte);
253                         *value |= byte;
254                         break;
255
256                 case 2:
257                         *value = (byte & 0x3F) << 16;
258                         stream_read_uint8(s, byte);
259                         *value |= (byte << 8);
260                         stream_read_uint8(s, byte);
261                         *value |= byte;
262                         break;
263
264                 case 3:
265                         *value = (byte & 0x3F) << 24;
266                         stream_read_uint8(s, byte);
267                         *value |= (byte << 16);
268                         stream_read_uint8(s, byte);
269                         *value |= (byte << 8);
270                         stream_read_uint8(s, byte);
271                         *value |= byte;
272                         break;
273
274                 default:
275                         break;
276         }
277 }
278
279 INLINE void update_read_delta(STREAM* s, sint32* value)
280 {
281         uint8 byte;
282
283         stream_read_uint8(s, byte);
284
285         if (byte & 0x40)
286                 *value = (byte | ~0x3F);
287         else
288                 *value = (byte & 0x3F);
289
290         if (byte & 0x80)
291         {
292                 stream_read_uint8(s, byte);
293                 *value = (*value << 8) | byte;
294         }
295 }
296
297 INLINE void update_read_glyph_delta(STREAM* s, uint16* value)
298 {
299         uint8 byte;
300
301         stream_read_uint8(s, byte);
302
303         if (byte == 0x80)
304                 stream_read_uint16(s, *value);
305         else
306                 *value = (byte & 0x3F);
307 }
308
309 INLINE void update_seek_glyph_delta(STREAM* s)
310 {
311         uint8 byte;
312
313         stream_read_uint8(s, byte);
314
315         if (byte & 0x80)
316                 stream_seek_uint8(s);
317 }
318
319 INLINE void update_read_brush(STREAM* s, rdpBrush* brush, uint8 fieldFlags)
320 {
321         if (fieldFlags & ORDER_FIELD_01)
322                 stream_read_uint8(s, brush->x);
323
324         if (fieldFlags & ORDER_FIELD_02)
325                 stream_read_uint8(s, brush->y);
326
327         if (fieldFlags & ORDER_FIELD_03)
328                 stream_read_uint8(s, brush->style);
329
330         if (fieldFlags & ORDER_FIELD_04)
331                 stream_read_uint8(s, brush->hatch);
332
333         if (brush->style & CACHED_BRUSH)
334         {
335                 brush->index = brush->hatch;
336
337                 brush->bpp = BMF_BPP[brush->style & 0x0F];
338
339                 if (brush->bpp == 0)
340                         brush->bpp = 1;
341         }
342
343         if (fieldFlags & ORDER_FIELD_05)
344         {
345                 brush->data = (uint8*) brush->p8x8;
346                 stream_read_uint8(s, brush->data[7]);
347                 stream_read_uint8(s, brush->data[6]);
348                 stream_read_uint8(s, brush->data[5]);
349                 stream_read_uint8(s, brush->data[4]);
350                 stream_read_uint8(s, brush->data[3]);
351                 stream_read_uint8(s, brush->data[2]);
352                 stream_read_uint8(s, brush->data[1]);
353                 brush->data[0] = brush->hatch;
354         }
355 }
356
357 INLINE void update_read_delta_rects(STREAM* s, DELTA_RECT* rectangles, int number)
358 {
359         int i;
360         uint8 flags = 0;
361         uint8* zeroBits;
362         int zeroBitsSize;
363
364         if (number > 45)
365                 number = 45;
366
367         zeroBitsSize = ((number + 1) / 2);
368
369         stream_get_mark(s, zeroBits);
370         stream_seek(s, zeroBitsSize);
371
372         memset(rectangles, 0, sizeof(DELTA_RECT) * (number + 1));
373
374         for (i = 1; i < number + 1; i++)
375         {
376                 if ((i - 1) % 2 == 0)
377                         flags = zeroBits[(i - 1) / 2];
378
379                 if (~flags & 0x80)
380                         update_read_delta(s, &rectangles[i].left);
381
382                 if (~flags & 0x40)
383                         update_read_delta(s, &rectangles[i].top);
384
385                 if (~flags & 0x20)
386                         update_read_delta(s, &rectangles[i].width);
387                 else
388                         rectangles[i].width = rectangles[i - 1].width;
389
390                 if (~flags & 0x10)
391                         update_read_delta(s, &rectangles[i].height);
392                 else
393                         rectangles[i].height = rectangles[i - 1].height;
394
395                 rectangles[i].left = rectangles[i].left + rectangles[i - 1].left;
396                 rectangles[i].top = rectangles[i].top + rectangles[i - 1].top;
397
398                 flags <<= 4;
399         }
400 }
401
402 INLINE void update_read_delta_points(STREAM* s, DELTA_POINT* points, int number, sint16 x, sint16 y)
403 {
404         int i;
405         uint8 flags = 0;
406         uint8* zeroBits;
407         int zeroBitsSize;
408
409         zeroBitsSize = ((number + 3) / 4);
410
411         stream_get_mark(s, zeroBits);
412         stream_seek(s, zeroBitsSize);
413
414         memset(points, 0, sizeof(DELTA_POINT) * number);
415
416         for (i = 0; i < number; i++)
417         {
418                 if (i % 4 == 0)
419                         flags = zeroBits[i / 4];
420
421                 if (~flags & 0x80)
422                         update_read_delta(s, &points[i].x);
423
424                 if (~flags & 0x40)
425                         update_read_delta(s, &points[i].y);
426
427                 flags <<= 2;
428         }
429 }
430
431 /* Primary Drawing Orders */
432
433 void update_read_dstblt_order(STREAM* s, ORDER_INFO* orderInfo, DSTBLT_ORDER* dstblt)
434 {
435         if (orderInfo->fieldFlags & ORDER_FIELD_01)
436                 update_read_coord(s, &dstblt->nLeftRect, orderInfo->deltaCoordinates);
437
438         if (orderInfo->fieldFlags & ORDER_FIELD_02)
439                 update_read_coord(s, &dstblt->nTopRect, orderInfo->deltaCoordinates);
440
441         if (orderInfo->fieldFlags & ORDER_FIELD_03)
442                 update_read_coord(s, &dstblt->nWidth, orderInfo->deltaCoordinates);
443
444         if (orderInfo->fieldFlags & ORDER_FIELD_04)
445                 update_read_coord(s, &dstblt->nHeight, orderInfo->deltaCoordinates);
446
447         if (orderInfo->fieldFlags & ORDER_FIELD_05)
448                 stream_read_uint8(s, dstblt->bRop);
449 }
450
451 void update_read_patblt_order(STREAM* s, ORDER_INFO* orderInfo, PATBLT_ORDER* patblt)
452 {
453         if (orderInfo->fieldFlags & ORDER_FIELD_01)
454                 update_read_coord(s, &patblt->nLeftRect, orderInfo->deltaCoordinates);
455
456         if (orderInfo->fieldFlags & ORDER_FIELD_02)
457                 update_read_coord(s, &patblt->nTopRect, orderInfo->deltaCoordinates);
458
459         if (orderInfo->fieldFlags & ORDER_FIELD_03)
460                 update_read_coord(s, &patblt->nWidth, orderInfo->deltaCoordinates);
461
462         if (orderInfo->fieldFlags & ORDER_FIELD_04)
463                 update_read_coord(s, &patblt->nHeight, orderInfo->deltaCoordinates);
464
465         if (orderInfo->fieldFlags & ORDER_FIELD_05)
466                 stream_read_uint8(s, patblt->bRop);
467
468         if (orderInfo->fieldFlags & ORDER_FIELD_06)
469                 update_read_color(s, &patblt->backColor);
470
471         if (orderInfo->fieldFlags & ORDER_FIELD_07)
472                 update_read_color(s, &patblt->foreColor);
473
474         update_read_brush(s, &patblt->brush, orderInfo->fieldFlags >> 7);
475 }
476
477 void update_read_scrblt_order(STREAM* s, ORDER_INFO* orderInfo, SCRBLT_ORDER* scrblt)
478 {
479         if (orderInfo->fieldFlags & ORDER_FIELD_01)
480                 update_read_coord(s, &scrblt->nLeftRect, orderInfo->deltaCoordinates);
481
482         if (orderInfo->fieldFlags & ORDER_FIELD_02)
483                 update_read_coord(s, &scrblt->nTopRect, orderInfo->deltaCoordinates);
484
485         if (orderInfo->fieldFlags & ORDER_FIELD_03)
486                 update_read_coord(s, &scrblt->nWidth, orderInfo->deltaCoordinates);
487
488         if (orderInfo->fieldFlags & ORDER_FIELD_04)
489                 update_read_coord(s, &scrblt->nHeight, orderInfo->deltaCoordinates);
490
491         if (orderInfo->fieldFlags & ORDER_FIELD_05)
492                 stream_read_uint8(s, scrblt->bRop);
493
494         if (orderInfo->fieldFlags & ORDER_FIELD_06)
495                 update_read_coord(s, &scrblt->nXSrc, orderInfo->deltaCoordinates);
496
497         if (orderInfo->fieldFlags & ORDER_FIELD_07)
498                 update_read_coord(s, &scrblt->nYSrc, orderInfo->deltaCoordinates);
499 }
500
501 void update_read_opaque_rect_order(STREAM* s, ORDER_INFO* orderInfo, OPAQUE_RECT_ORDER* opaque_rect)
502 {
503         uint8 byte;
504
505         if (orderInfo->fieldFlags & ORDER_FIELD_01)
506                 update_read_coord(s, &opaque_rect->nLeftRect, orderInfo->deltaCoordinates);
507
508         if (orderInfo->fieldFlags & ORDER_FIELD_02)
509                 update_read_coord(s, &opaque_rect->nTopRect, orderInfo->deltaCoordinates);
510
511         if (orderInfo->fieldFlags & ORDER_FIELD_03)
512                 update_read_coord(s, &opaque_rect->nWidth, orderInfo->deltaCoordinates);
513
514         if (orderInfo->fieldFlags & ORDER_FIELD_04)
515                 update_read_coord(s, &opaque_rect->nHeight, orderInfo->deltaCoordinates);
516
517         if (orderInfo->fieldFlags & ORDER_FIELD_05)
518         {
519                 stream_read_uint8(s, byte);
520                 opaque_rect->color = (opaque_rect->color & 0xFFFFFF00) | byte;
521         }
522
523         if (orderInfo->fieldFlags & ORDER_FIELD_06)
524         {
525                 stream_read_uint8(s, byte);
526                 opaque_rect->color = (opaque_rect->color & 0xFFFF00FF) | (byte << 8);
527         }
528
529         if (orderInfo->fieldFlags & ORDER_FIELD_07)
530         {
531                 stream_read_uint8(s, byte);
532                 opaque_rect->color = (opaque_rect->color & 0xFF00FFFF) | (byte << 16);
533         }
534 }
535
536 void update_read_draw_nine_grid_order(STREAM* s, ORDER_INFO* orderInfo, DRAW_NINE_GRID_ORDER* draw_nine_grid)
537 {
538         if (orderInfo->fieldFlags & ORDER_FIELD_01)
539                 update_read_coord(s, &draw_nine_grid->srcLeft, orderInfo->deltaCoordinates);
540
541         if (orderInfo->fieldFlags & ORDER_FIELD_02)
542                 update_read_coord(s, &draw_nine_grid->srcTop, orderInfo->deltaCoordinates);
543
544         if (orderInfo->fieldFlags & ORDER_FIELD_03)
545                 update_read_coord(s, &draw_nine_grid->srcRight, orderInfo->deltaCoordinates);
546
547         if (orderInfo->fieldFlags & ORDER_FIELD_04)
548                 update_read_coord(s, &draw_nine_grid->srcBottom, orderInfo->deltaCoordinates);
549
550         if (orderInfo->fieldFlags & ORDER_FIELD_05)
551                 stream_read_uint16(s, draw_nine_grid->bitmapId);
552 }
553
554 void update_read_multi_dstblt_order(STREAM* s, ORDER_INFO* orderInfo, MULTI_DSTBLT_ORDER* multi_dstblt)
555 {
556         if (orderInfo->fieldFlags & ORDER_FIELD_01)
557                 update_read_coord(s, &multi_dstblt->nLeftRect, orderInfo->deltaCoordinates);
558
559         if (orderInfo->fieldFlags & ORDER_FIELD_02)
560                 update_read_coord(s, &multi_dstblt->nTopRect, orderInfo->deltaCoordinates);
561
562         if (orderInfo->fieldFlags & ORDER_FIELD_03)
563                 update_read_coord(s, &multi_dstblt->nWidth, orderInfo->deltaCoordinates);
564
565         if (orderInfo->fieldFlags & ORDER_FIELD_04)
566                 update_read_coord(s, &multi_dstblt->nHeight, orderInfo->deltaCoordinates);
567
568         if (orderInfo->fieldFlags & ORDER_FIELD_05)
569                 stream_read_uint8(s, multi_dstblt->bRop);
570
571         if (orderInfo->fieldFlags & ORDER_FIELD_06)
572                 stream_read_uint8(s, multi_dstblt->numRectangles);
573
574         if (orderInfo->fieldFlags & ORDER_FIELD_07)
575         {
576                 stream_read_uint16(s, multi_dstblt->cbData);
577                 update_read_delta_rects(s, multi_dstblt->rectangles, multi_dstblt->numRectangles);
578         }
579 }
580
581 void update_read_multi_patblt_order(STREAM* s, ORDER_INFO* orderInfo, MULTI_PATBLT_ORDER* multi_patblt)
582 {
583         if (orderInfo->fieldFlags & ORDER_FIELD_01)
584                 update_read_coord(s, &multi_patblt->nLeftRect, orderInfo->deltaCoordinates);
585
586         if (orderInfo->fieldFlags & ORDER_FIELD_02)
587                 update_read_coord(s, &multi_patblt->nTopRect, orderInfo->deltaCoordinates);
588
589         if (orderInfo->fieldFlags & ORDER_FIELD_03)
590                 update_read_coord(s, &multi_patblt->nWidth, orderInfo->deltaCoordinates);
591
592         if (orderInfo->fieldFlags & ORDER_FIELD_04)
593                 update_read_coord(s, &multi_patblt->nHeight, orderInfo->deltaCoordinates);
594
595         if (orderInfo->fieldFlags & ORDER_FIELD_05)
596                 stream_read_uint8(s, multi_patblt->bRop);
597
598         if (orderInfo->fieldFlags & ORDER_FIELD_06)
599                 update_read_color(s, &multi_patblt->backColor);
600
601         if (orderInfo->fieldFlags & ORDER_FIELD_07)
602                 update_read_color(s, &multi_patblt->foreColor);
603
604         update_read_brush(s, &multi_patblt->brush, orderInfo->fieldFlags >> 7);
605
606         if (orderInfo->fieldFlags & ORDER_FIELD_13)
607                 stream_read_uint8(s, multi_patblt->numRectangles);
608
609         if (orderInfo->fieldFlags & ORDER_FIELD_14)
610         {
611                 stream_read_uint16(s, multi_patblt->cbData);
612                 update_read_delta_rects(s, multi_patblt->rectangles, multi_patblt->numRectangles);
613         }
614 }
615
616 void update_read_multi_scrblt_order(STREAM* s, ORDER_INFO* orderInfo, MULTI_SCRBLT_ORDER* multi_scrblt)
617 {
618         if (orderInfo->fieldFlags & ORDER_FIELD_01)
619                 update_read_coord(s, &multi_scrblt->nLeftRect, orderInfo->deltaCoordinates);
620
621         if (orderInfo->fieldFlags & ORDER_FIELD_02)
622                 update_read_coord(s, &multi_scrblt->nTopRect, orderInfo->deltaCoordinates);
623
624         if (orderInfo->fieldFlags & ORDER_FIELD_03)
625                 update_read_coord(s, &multi_scrblt->nWidth, orderInfo->deltaCoordinates);
626
627         if (orderInfo->fieldFlags & ORDER_FIELD_04)
628                 update_read_coord(s, &multi_scrblt->nHeight, orderInfo->deltaCoordinates);
629
630         if (orderInfo->fieldFlags & ORDER_FIELD_05)
631                 stream_read_uint8(s, multi_scrblt->bRop);
632
633         if (orderInfo->fieldFlags & ORDER_FIELD_06)
634                 update_read_coord(s, &multi_scrblt->nXSrc, orderInfo->deltaCoordinates);
635
636         if (orderInfo->fieldFlags & ORDER_FIELD_07)
637                 update_read_coord(s, &multi_scrblt->nYSrc, orderInfo->deltaCoordinates);
638
639         if (orderInfo->fieldFlags & ORDER_FIELD_08)
640                 stream_read_uint8(s, multi_scrblt->numRectangles);
641
642         if (orderInfo->fieldFlags & ORDER_FIELD_09)
643         {
644                 stream_read_uint16(s, multi_scrblt->cbData);
645                 update_read_delta_rects(s, multi_scrblt->rectangles, multi_scrblt->numRectangles);
646         }
647 }
648
649 void update_read_multi_opaque_rect_order(STREAM* s, ORDER_INFO* orderInfo, MULTI_OPAQUE_RECT_ORDER* multi_opaque_rect)
650 {
651         uint8 byte;
652
653         if (orderInfo->fieldFlags & ORDER_FIELD_01)
654                 update_read_coord(s, &multi_opaque_rect->nLeftRect, orderInfo->deltaCoordinates);
655
656         if (orderInfo->fieldFlags & ORDER_FIELD_02)
657                 update_read_coord(s, &multi_opaque_rect->nTopRect, orderInfo->deltaCoordinates);
658
659         if (orderInfo->fieldFlags & ORDER_FIELD_03)
660                 update_read_coord(s, &multi_opaque_rect->nWidth, orderInfo->deltaCoordinates);
661
662         if (orderInfo->fieldFlags & ORDER_FIELD_04)
663                 update_read_coord(s, &multi_opaque_rect->nHeight, orderInfo->deltaCoordinates);
664
665         if (orderInfo->fieldFlags & ORDER_FIELD_05)
666         {
667                 stream_read_uint8(s, byte);
668                 multi_opaque_rect->color = (multi_opaque_rect->color & 0xFFFFFF00) | byte;
669         }
670
671         if (orderInfo->fieldFlags & ORDER_FIELD_06)
672         {
673                 stream_read_uint8(s, byte);
674                 multi_opaque_rect->color = (multi_opaque_rect->color & 0xFFFF00FF) | (byte << 8);
675         }
676
677         if (orderInfo->fieldFlags & ORDER_FIELD_07)
678         {
679                 stream_read_uint8(s, byte);
680                 multi_opaque_rect->color = (multi_opaque_rect->color & 0xFF00FFFF) | (byte << 16);
681         }
682
683         if (orderInfo->fieldFlags & ORDER_FIELD_08)
684                 stream_read_uint8(s, multi_opaque_rect->numRectangles);
685
686         if (orderInfo->fieldFlags & ORDER_FIELD_09)
687         {
688                 stream_read_uint16(s, multi_opaque_rect->cbData);
689                 update_read_delta_rects(s, multi_opaque_rect->rectangles, multi_opaque_rect->numRectangles);
690         }
691 }
692
693 void update_read_multi_draw_nine_grid_order(STREAM* s, ORDER_INFO* orderInfo, MULTI_DRAW_NINE_GRID_ORDER* multi_draw_nine_grid)
694 {
695         if (orderInfo->fieldFlags & ORDER_FIELD_01)
696                 update_read_coord(s, &multi_draw_nine_grid->srcLeft, orderInfo->deltaCoordinates);
697
698         if (orderInfo->fieldFlags & ORDER_FIELD_02)
699                 update_read_coord(s, &multi_draw_nine_grid->srcTop, orderInfo->deltaCoordinates);
700
701         if (orderInfo->fieldFlags & ORDER_FIELD_03)
702                 update_read_coord(s, &multi_draw_nine_grid->srcRight, orderInfo->deltaCoordinates);
703
704         if (orderInfo->fieldFlags & ORDER_FIELD_04)
705                 update_read_coord(s, &multi_draw_nine_grid->srcBottom, orderInfo->deltaCoordinates);
706
707         if (orderInfo->fieldFlags & ORDER_FIELD_05)
708                 stream_read_uint16(s, multi_draw_nine_grid->bitmapId);
709
710         if (orderInfo->fieldFlags & ORDER_FIELD_06)
711                 stream_read_uint8(s, multi_draw_nine_grid->nDeltaEntries);
712
713         if (orderInfo->fieldFlags & ORDER_FIELD_07)
714         {
715                 stream_read_uint16(s, multi_draw_nine_grid->cbData);
716                 stream_seek(s, multi_draw_nine_grid->cbData);
717         }
718 }
719
720 void update_read_line_to_order(STREAM* s, ORDER_INFO* orderInfo, LINE_TO_ORDER* line_to)
721 {
722         if (orderInfo->fieldFlags & ORDER_FIELD_01)
723                 stream_read_uint16(s, line_to->backMode);
724
725         if (orderInfo->fieldFlags & ORDER_FIELD_02)
726                 update_read_coord(s, &line_to->nXStart, orderInfo->deltaCoordinates);
727
728         if (orderInfo->fieldFlags & ORDER_FIELD_03)
729                 update_read_coord(s, &line_to->nYStart, orderInfo->deltaCoordinates);
730
731         if (orderInfo->fieldFlags & ORDER_FIELD_04)
732                 update_read_coord(s, &line_to->nXEnd, orderInfo->deltaCoordinates);
733
734         if (orderInfo->fieldFlags & ORDER_FIELD_05)
735                 update_read_coord(s, &line_to->nYEnd, orderInfo->deltaCoordinates);
736
737         if (orderInfo->fieldFlags & ORDER_FIELD_06)
738                 update_read_color(s, &line_to->backColor);
739
740         if (orderInfo->fieldFlags & ORDER_FIELD_07)
741                 stream_read_uint8(s, line_to->bRop2);
742
743         if (orderInfo->fieldFlags & ORDER_FIELD_08)
744                 stream_read_uint8(s, line_to->penStyle);
745
746         if (orderInfo->fieldFlags & ORDER_FIELD_09)
747                 stream_read_uint8(s, line_to->penWidth);
748
749         if (orderInfo->fieldFlags & ORDER_FIELD_10)
750                 update_read_color(s, &line_to->penColor);
751 }
752
753 void update_read_polyline_order(STREAM* s, ORDER_INFO* orderInfo, POLYLINE_ORDER* polyline)
754 {
755         if (orderInfo->fieldFlags & ORDER_FIELD_01)
756                 update_read_coord(s, &polyline->xStart, orderInfo->deltaCoordinates);
757
758         if (orderInfo->fieldFlags & ORDER_FIELD_02)
759                 update_read_coord(s, &polyline->yStart, orderInfo->deltaCoordinates);
760
761         if (orderInfo->fieldFlags & ORDER_FIELD_03)
762                 stream_read_uint8(s, polyline->bRop2);
763
764         if (orderInfo->fieldFlags & ORDER_FIELD_04)
765                 stream_seek_uint16(s);
766
767         if (orderInfo->fieldFlags & ORDER_FIELD_05)
768                 update_read_color(s, &polyline->penColor);
769
770         if (orderInfo->fieldFlags & ORDER_FIELD_06)
771                 stream_read_uint8(s, polyline->numPoints);
772
773         if (orderInfo->fieldFlags & ORDER_FIELD_07)
774         {
775                 stream_read_uint8(s, polyline->cbData);
776
777                 if (polyline->points == NULL)
778                         polyline->points = (DELTA_POINT*) xmalloc(sizeof(DELTA_POINT) * polyline->numPoints);
779                 else
780                         polyline->points = (DELTA_POINT*) xrealloc(polyline->points, sizeof(DELTA_POINT) * polyline->numPoints);
781
782                 update_read_delta_points(s, polyline->points, polyline->numPoints, polyline->xStart, polyline->yStart);
783         }
784 }
785
786 void update_read_memblt_order(STREAM* s, ORDER_INFO* orderInfo, MEMBLT_ORDER* memblt)
787 {
788         if (orderInfo->fieldFlags & ORDER_FIELD_01)
789                 stream_read_uint16(s, memblt->cacheId);
790
791         if (orderInfo->fieldFlags & ORDER_FIELD_02)
792                 update_read_coord(s, &memblt->nLeftRect, orderInfo->deltaCoordinates);
793
794         if (orderInfo->fieldFlags & ORDER_FIELD_03)
795                 update_read_coord(s, &memblt->nTopRect, orderInfo->deltaCoordinates);
796
797         if (orderInfo->fieldFlags & ORDER_FIELD_04)
798                 update_read_coord(s, &memblt->nWidth, orderInfo->deltaCoordinates);
799
800         if (orderInfo->fieldFlags & ORDER_FIELD_05)
801                 update_read_coord(s, &memblt->nHeight, orderInfo->deltaCoordinates);
802
803         if (orderInfo->fieldFlags & ORDER_FIELD_06)
804                 stream_read_uint8(s, memblt->bRop);
805
806         if (orderInfo->fieldFlags & ORDER_FIELD_07)
807                 update_read_coord(s, &memblt->nXSrc, orderInfo->deltaCoordinates);
808
809         if (orderInfo->fieldFlags & ORDER_FIELD_08)
810                 update_read_coord(s, &memblt->nYSrc, orderInfo->deltaCoordinates);
811
812         if (orderInfo->fieldFlags & ORDER_FIELD_09)
813                 stream_read_uint16(s, memblt->cacheIndex);
814
815         memblt->colorIndex = (memblt->cacheId >> 8);
816         memblt->cacheId = (memblt->cacheId & 0xFF);
817 }
818
819 void update_read_mem3blt_order(STREAM* s, ORDER_INFO* orderInfo, MEM3BLT_ORDER* mem3blt)
820 {
821         if (orderInfo->fieldFlags & ORDER_FIELD_01)
822                 stream_read_uint16(s, mem3blt->cacheId);
823
824         if (orderInfo->fieldFlags & ORDER_FIELD_02)
825                 update_read_coord(s, &mem3blt->nLeftRect, orderInfo->deltaCoordinates);
826
827         if (orderInfo->fieldFlags & ORDER_FIELD_03)
828                 update_read_coord(s, &mem3blt->nTopRect, orderInfo->deltaCoordinates);
829
830         if (orderInfo->fieldFlags & ORDER_FIELD_04)
831                 update_read_coord(s, &mem3blt->nWidth, orderInfo->deltaCoordinates);
832
833         if (orderInfo->fieldFlags & ORDER_FIELD_05)
834                 update_read_coord(s, &mem3blt->nHeight, orderInfo->deltaCoordinates);
835
836         if (orderInfo->fieldFlags & ORDER_FIELD_06)
837                 stream_read_uint8(s, mem3blt->bRop);
838
839         if (orderInfo->fieldFlags & ORDER_FIELD_07)
840                 update_read_coord(s, &mem3blt->nXSrc, orderInfo->deltaCoordinates);
841
842         if (orderInfo->fieldFlags & ORDER_FIELD_08)
843                 update_read_coord(s, &mem3blt->nYSrc, orderInfo->deltaCoordinates);
844
845         if (orderInfo->fieldFlags & ORDER_FIELD_09)
846                 update_read_color(s, &mem3blt->backColor);
847
848         if (orderInfo->fieldFlags & ORDER_FIELD_10)
849                 update_read_color(s, &mem3blt->foreColor);
850
851         update_read_brush(s, &mem3blt->brush, orderInfo->fieldFlags >> 10);
852
853         if (orderInfo->fieldFlags & ORDER_FIELD_16)
854                 stream_read_uint16(s, mem3blt->cacheIndex);
855
856         mem3blt->colorIndex = (mem3blt->cacheId >> 8);
857         mem3blt->cacheId = (mem3blt->cacheId & 0xFF);
858 }
859
860 void update_read_save_bitmap_order(STREAM* s, ORDER_INFO* orderInfo, SAVE_BITMAP_ORDER* save_bitmap)
861 {
862         if (orderInfo->fieldFlags & ORDER_FIELD_01)
863                 stream_read_uint32(s, save_bitmap->savedBitmapPosition);
864
865         if (orderInfo->fieldFlags & ORDER_FIELD_02)
866                 update_read_coord(s, &save_bitmap->nLeftRect, orderInfo->deltaCoordinates);
867
868         if (orderInfo->fieldFlags & ORDER_FIELD_03)
869                 update_read_coord(s, &save_bitmap->nTopRect, orderInfo->deltaCoordinates);
870
871         if (orderInfo->fieldFlags & ORDER_FIELD_04)
872                 update_read_coord(s, &save_bitmap->nRightRect, orderInfo->deltaCoordinates);
873
874         if (orderInfo->fieldFlags & ORDER_FIELD_05)
875                 update_read_coord(s, &save_bitmap->nBottomRect, orderInfo->deltaCoordinates);
876
877         if (orderInfo->fieldFlags & ORDER_FIELD_06)
878                 stream_read_uint8(s, save_bitmap->operation);
879 }
880
881 void update_read_glyph_index_order(STREAM* s, ORDER_INFO* orderInfo, GLYPH_INDEX_ORDER* glyph_index)
882 {
883         if (orderInfo->fieldFlags & ORDER_FIELD_01)
884                 stream_read_uint8(s, glyph_index->cacheId);
885
886         if (orderInfo->fieldFlags & ORDER_FIELD_02)
887                 stream_read_uint8(s, glyph_index->flAccel);
888
889         if (orderInfo->fieldFlags & ORDER_FIELD_03)
890                 stream_read_uint8(s, glyph_index->ulCharInc);
891
892         if (orderInfo->fieldFlags & ORDER_FIELD_04)
893                 stream_read_uint8(s, glyph_index->fOpRedundant);
894
895         if (orderInfo->fieldFlags & ORDER_FIELD_05)
896                 update_read_color(s, &glyph_index->backColor);
897
898         if (orderInfo->fieldFlags & ORDER_FIELD_06)
899                 update_read_color(s, &glyph_index->foreColor);
900
901         if (orderInfo->fieldFlags & ORDER_FIELD_07)
902                 stream_read_uint16(s, glyph_index->bkLeft);
903
904         if (orderInfo->fieldFlags & ORDER_FIELD_08)
905                 stream_read_uint16(s, glyph_index->bkTop);
906
907         if (orderInfo->fieldFlags & ORDER_FIELD_09)
908                 stream_read_uint16(s, glyph_index->bkRight);
909
910         if (orderInfo->fieldFlags & ORDER_FIELD_10)
911                 stream_read_uint16(s, glyph_index->bkBottom);
912
913         if (orderInfo->fieldFlags & ORDER_FIELD_11)
914                 stream_read_uint16(s, glyph_index->opLeft);
915
916         if (orderInfo->fieldFlags & ORDER_FIELD_12)
917                 stream_read_uint16(s, glyph_index->opTop);
918
919         if (orderInfo->fieldFlags & ORDER_FIELD_13)
920                 stream_read_uint16(s, glyph_index->opRight);
921
922         if (orderInfo->fieldFlags & ORDER_FIELD_14)
923                 stream_read_uint16(s, glyph_index->opBottom);
924
925         update_read_brush(s, &glyph_index->brush, orderInfo->fieldFlags >> 14);
926
927         if (orderInfo->fieldFlags & ORDER_FIELD_20)
928                 stream_read_uint16(s, glyph_index->x);
929
930         if (orderInfo->fieldFlags & ORDER_FIELD_21)
931                 stream_read_uint16(s, glyph_index->y);
932
933         if (orderInfo->fieldFlags & ORDER_FIELD_22)
934         {
935                 stream_read_uint8(s, glyph_index->cbData);
936                 memcpy(glyph_index->data, s->p, glyph_index->cbData);
937                 stream_seek(s, glyph_index->cbData);
938         }
939 }
940
941 void update_read_fast_index_order(STREAM* s, ORDER_INFO* orderInfo, FAST_INDEX_ORDER* fast_index)
942 {
943         if (orderInfo->fieldFlags & ORDER_FIELD_01)
944                 stream_read_uint8(s, fast_index->cacheId);
945
946         if (orderInfo->fieldFlags & ORDER_FIELD_02)
947         {
948                 stream_read_uint8(s, fast_index->ulCharInc);
949                 stream_read_uint8(s, fast_index->flAccel);
950         }
951
952         if (orderInfo->fieldFlags & ORDER_FIELD_03)
953                 update_read_color(s, &fast_index->backColor);
954
955         if (orderInfo->fieldFlags & ORDER_FIELD_04)
956                 update_read_color(s, &fast_index->foreColor);
957
958         if (orderInfo->fieldFlags & ORDER_FIELD_05)
959                 update_read_coord(s, &fast_index->bkLeft, orderInfo->deltaCoordinates);
960
961         if (orderInfo->fieldFlags & ORDER_FIELD_06)
962                 update_read_coord(s, &fast_index->bkTop, orderInfo->deltaCoordinates);
963
964         if (orderInfo->fieldFlags & ORDER_FIELD_07)
965                 update_read_coord(s, &fast_index->bkRight, orderInfo->deltaCoordinates);
966
967         if (orderInfo->fieldFlags & ORDER_FIELD_08)
968                 update_read_coord(s, &fast_index->bkBottom, orderInfo->deltaCoordinates);
969
970         if (orderInfo->fieldFlags & ORDER_FIELD_09)
971                 update_read_coord(s, &fast_index->opLeft, orderInfo->deltaCoordinates);
972
973         if (orderInfo->fieldFlags & ORDER_FIELD_10)
974                 update_read_coord(s, &fast_index->opTop, orderInfo->deltaCoordinates);
975
976         if (orderInfo->fieldFlags & ORDER_FIELD_11)
977                 update_read_coord(s, &fast_index->opRight, orderInfo->deltaCoordinates);
978
979         if (orderInfo->fieldFlags & ORDER_FIELD_12)
980                 update_read_coord(s, &fast_index->opBottom, orderInfo->deltaCoordinates);
981
982         if (orderInfo->fieldFlags & ORDER_FIELD_13)
983                 update_read_coord(s, &fast_index->x, orderInfo->deltaCoordinates);
984
985         if (orderInfo->fieldFlags & ORDER_FIELD_14)
986                 update_read_coord(s, &fast_index->y, orderInfo->deltaCoordinates);
987
988         if (orderInfo->fieldFlags & ORDER_FIELD_15)
989         {
990                 stream_read_uint8(s, fast_index->cbData);
991                 memcpy(fast_index->data, s->p, fast_index->cbData);
992                 stream_seek(s, fast_index->cbData);
993         }
994 }
995
996 void update_read_fast_glyph_order(STREAM* s, ORDER_INFO* orderInfo, FAST_GLYPH_ORDER* fast_glyph)
997 {
998         GLYPH_DATA_V2* glyph;
999         uint8* phold;
1000
1001         if (orderInfo->fieldFlags & ORDER_FIELD_01)
1002                 stream_read_uint8(s, fast_glyph->cacheId);
1003
1004         if (orderInfo->fieldFlags & ORDER_FIELD_02)
1005         {
1006                 stream_read_uint8(s, fast_glyph->ulCharInc);
1007                 stream_read_uint8(s, fast_glyph->flAccel);
1008         }
1009
1010         if (orderInfo->fieldFlags & ORDER_FIELD_03)
1011                 update_read_color(s, &fast_glyph->backColor);
1012
1013         if (orderInfo->fieldFlags & ORDER_FIELD_04)
1014                 update_read_color(s, &fast_glyph->foreColor);
1015
1016         if (orderInfo->fieldFlags & ORDER_FIELD_05)
1017                 update_read_coord(s, &fast_glyph->bkLeft, orderInfo->deltaCoordinates);
1018
1019         if (orderInfo->fieldFlags & ORDER_FIELD_06)
1020                 update_read_coord(s, &fast_glyph->bkTop, orderInfo->deltaCoordinates);
1021
1022         if (orderInfo->fieldFlags & ORDER_FIELD_07)
1023                 update_read_coord(s, &fast_glyph->bkRight, orderInfo->deltaCoordinates);
1024
1025         if (orderInfo->fieldFlags & ORDER_FIELD_08)
1026                 update_read_coord(s, &fast_glyph->bkBottom, orderInfo->deltaCoordinates);
1027
1028         if (orderInfo->fieldFlags & ORDER_FIELD_09)
1029                 update_read_coord(s, &fast_glyph->opLeft, orderInfo->deltaCoordinates);
1030
1031         if (orderInfo->fieldFlags & ORDER_FIELD_10)
1032                 update_read_coord(s, &fast_glyph->opTop, orderInfo->deltaCoordinates);
1033
1034         if (orderInfo->fieldFlags & ORDER_FIELD_11)
1035                 update_read_coord(s, &fast_glyph->opRight, orderInfo->deltaCoordinates);
1036
1037         if (orderInfo->fieldFlags & ORDER_FIELD_12)
1038                 update_read_coord(s, &fast_glyph->opBottom, orderInfo->deltaCoordinates);
1039
1040         if (orderInfo->fieldFlags & ORDER_FIELD_13)
1041                 update_read_coord(s, &fast_glyph->x, orderInfo->deltaCoordinates);
1042
1043         if (orderInfo->fieldFlags & ORDER_FIELD_14)
1044                 update_read_coord(s, &fast_glyph->y, orderInfo->deltaCoordinates);
1045
1046         if (orderInfo->fieldFlags & ORDER_FIELD_15)
1047         {
1048                 stream_read_uint8(s, fast_glyph->cbData);
1049                 memcpy(fast_glyph->data, s->p, fast_glyph->cbData);
1050                 phold = s->p;
1051                 stream_seek(s, 1);
1052                 if ((fast_glyph->cbData > 1) && (fast_glyph->glyph_data == NULL))
1053                 {
1054                         /* parse optional glyph data */
1055                         glyph = (GLYPH_DATA_V2*) xmalloc(sizeof(GLYPH_DATA_V2));
1056                         glyph->cacheIndex = fast_glyph->data[0];
1057                         update_read_2byte_signed(s, &glyph->x);
1058                         update_read_2byte_signed(s, &glyph->y);
1059                         update_read_2byte_unsigned(s, &glyph->cx);
1060                         update_read_2byte_unsigned(s, &glyph->cy);
1061                         glyph->cb = ((glyph->cx + 7) / 8) * glyph->cy;
1062                         glyph->cb += ((glyph->cb % 4) > 0) ? 4 - (glyph->cb % 4) : 0;
1063                         glyph->aj = (uint8*) xmalloc(glyph->cb);
1064                         stream_read(s, glyph->aj, glyph->cb);
1065                         fast_glyph->glyph_data = glyph;
1066                 }
1067                 s->p = phold + fast_glyph->cbData;
1068         }
1069 }
1070
1071 void update_read_polygon_sc_order(STREAM* s, ORDER_INFO* orderInfo, POLYGON_SC_ORDER* polygon_sc)
1072 {
1073         if (orderInfo->fieldFlags & ORDER_FIELD_01)
1074                 update_read_coord(s, &polygon_sc->xStart, orderInfo->deltaCoordinates);
1075
1076         if (orderInfo->fieldFlags & ORDER_FIELD_02)
1077                 update_read_coord(s, &polygon_sc->yStart, orderInfo->deltaCoordinates);
1078
1079         if (orderInfo->fieldFlags & ORDER_FIELD_03)
1080                 stream_read_uint8(s, polygon_sc->bRop2);
1081
1082         if (orderInfo->fieldFlags & ORDER_FIELD_04)
1083                 stream_read_uint8(s, polygon_sc->fillMode);
1084
1085         if (orderInfo->fieldFlags & ORDER_FIELD_05)
1086                 update_read_color(s, &polygon_sc->brushColor);
1087
1088         if (orderInfo->fieldFlags & ORDER_FIELD_06)
1089                 stream_read_uint8(s, polygon_sc->nDeltaEntries);
1090
1091         if (orderInfo->fieldFlags & ORDER_FIELD_07)
1092         {
1093                 stream_read_uint8(s, polygon_sc->cbData);
1094                 stream_seek(s, polygon_sc->cbData);
1095         }
1096 }
1097
1098 void update_read_polygon_cb_order(STREAM* s, ORDER_INFO* orderInfo, POLYGON_CB_ORDER* polygon_cb)
1099 {
1100         if (orderInfo->fieldFlags & ORDER_FIELD_01)
1101                 update_read_coord(s, &polygon_cb->xStart, orderInfo->deltaCoordinates);
1102
1103         if (orderInfo->fieldFlags & ORDER_FIELD_02)
1104                 update_read_coord(s, &polygon_cb->yStart, orderInfo->deltaCoordinates);
1105
1106         if (orderInfo->fieldFlags & ORDER_FIELD_03)
1107                 stream_read_uint8(s, polygon_cb->bRop2);
1108
1109         if (orderInfo->fieldFlags & ORDER_FIELD_04)
1110                 stream_read_uint8(s, polygon_cb->fillMode);
1111
1112         if (orderInfo->fieldFlags & ORDER_FIELD_05)
1113                 update_read_color(s, &polygon_cb->backColor);
1114
1115         if (orderInfo->fieldFlags & ORDER_FIELD_06)
1116                 update_read_color(s, &polygon_cb->foreColor);
1117
1118         update_read_brush(s, &polygon_cb->brush, orderInfo->fieldFlags >> 6);
1119
1120         if (orderInfo->fieldFlags & ORDER_FIELD_12)
1121                 stream_read_uint8(s, polygon_cb->nDeltaEntries);
1122
1123         if (orderInfo->fieldFlags & ORDER_FIELD_13)
1124         {
1125                 stream_read_uint8(s, polygon_cb->cbData);
1126                 stream_seek(s, polygon_cb->cbData);
1127         }
1128 }
1129
1130 void update_read_ellipse_sc_order(STREAM* s, ORDER_INFO* orderInfo, ELLIPSE_SC_ORDER* ellipse_sc)
1131 {
1132         if (orderInfo->fieldFlags & ORDER_FIELD_01)
1133                 update_read_coord(s, &ellipse_sc->leftRect, orderInfo->deltaCoordinates);
1134
1135         if (orderInfo->fieldFlags & ORDER_FIELD_02)
1136                 update_read_coord(s, &ellipse_sc->topRect, orderInfo->deltaCoordinates);
1137
1138         if (orderInfo->fieldFlags & ORDER_FIELD_03)
1139                 update_read_coord(s, &ellipse_sc->rightRect, orderInfo->deltaCoordinates);
1140
1141         if (orderInfo->fieldFlags & ORDER_FIELD_04)
1142                 update_read_coord(s, &ellipse_sc->bottomRect, orderInfo->deltaCoordinates);
1143
1144         if (orderInfo->fieldFlags & ORDER_FIELD_05)
1145                 stream_read_uint8(s, ellipse_sc->bRop2);
1146
1147         if (orderInfo->fieldFlags & ORDER_FIELD_06)
1148                 stream_read_uint8(s, ellipse_sc->fillMode);
1149
1150         if (orderInfo->fieldFlags & ORDER_FIELD_07)
1151                 update_read_color(s, &ellipse_sc->color);
1152 }
1153
1154 void update_read_ellipse_cb_order(STREAM* s, ORDER_INFO* orderInfo, ELLIPSE_CB_ORDER* ellipse_cb)
1155 {
1156         if (orderInfo->fieldFlags & ORDER_FIELD_01)
1157                 update_read_coord(s, &ellipse_cb->leftRect, orderInfo->deltaCoordinates);
1158
1159         if (orderInfo->fieldFlags & ORDER_FIELD_02)
1160                 update_read_coord(s, &ellipse_cb->topRect, orderInfo->deltaCoordinates);
1161
1162         if (orderInfo->fieldFlags & ORDER_FIELD_03)
1163                 update_read_coord(s, &ellipse_cb->rightRect, orderInfo->deltaCoordinates);
1164
1165         if (orderInfo->fieldFlags & ORDER_FIELD_04)
1166                 update_read_coord(s, &ellipse_cb->bottomRect, orderInfo->deltaCoordinates);
1167
1168         if (orderInfo->fieldFlags & ORDER_FIELD_05)
1169                 stream_read_uint8(s, ellipse_cb->bRop2);
1170
1171         if (orderInfo->fieldFlags & ORDER_FIELD_06)
1172                 stream_read_uint8(s, ellipse_cb->fillMode);
1173
1174         if (orderInfo->fieldFlags & ORDER_FIELD_07)
1175                 update_read_color(s, &ellipse_cb->backColor);
1176
1177         if (orderInfo->fieldFlags & ORDER_FIELD_08)
1178                 update_read_color(s, &ellipse_cb->foreColor);
1179
1180         update_read_brush(s, &ellipse_cb->brush, orderInfo->fieldFlags >> 8);
1181 }
1182
1183 /* Secondary Drawing Orders */
1184
1185 void update_read_cache_bitmap_order(STREAM* s, CACHE_BITMAP_ORDER* cache_bitmap_order, boolean compressed, uint16 flags)
1186 {
1187         stream_read_uint8(s, cache_bitmap_order->cacheId); /* cacheId (1 byte) */
1188         stream_seek_uint8(s); /* pad1Octet (1 byte) */
1189         stream_read_uint8(s, cache_bitmap_order->bitmapWidth); /* bitmapWidth (1 byte) */
1190         stream_read_uint8(s, cache_bitmap_order->bitmapHeight); /* bitmapHeight (1 byte) */
1191         stream_read_uint8(s, cache_bitmap_order->bitmapBpp); /* bitmapBpp (1 byte) */
1192         stream_read_uint16(s, cache_bitmap_order->bitmapLength); /* bitmapLength (2 bytes) */
1193         stream_read_uint16(s, cache_bitmap_order->cacheIndex); /* cacheIndex (2 bytes) */
1194
1195         if (compressed)
1196         {
1197                 if ((flags & NO_BITMAP_COMPRESSION_HDR) == 0)
1198                 {
1199                         uint8* bitmapComprHdr = (uint8*) &(cache_bitmap_order->bitmapComprHdr);
1200                         stream_read(s, bitmapComprHdr, 8); /* bitmapComprHdr (8 bytes) */
1201                         cache_bitmap_order->bitmapLength -= 8;
1202                 }
1203                 stream_get_mark(s, cache_bitmap_order->bitmapDataStream);
1204                 stream_seek(s, cache_bitmap_order->bitmapLength);
1205         }
1206         else
1207         {
1208                 stream_get_mark(s, cache_bitmap_order->bitmapDataStream);
1209                 stream_seek(s, cache_bitmap_order->bitmapLength); /* bitmapDataStream */
1210         }
1211         cache_bitmap_order->compressed = compressed;
1212 }
1213
1214 void update_read_cache_bitmap_v2_order(STREAM* s, CACHE_BITMAP_V2_ORDER* cache_bitmap_v2_order, boolean compressed, uint16 flags)
1215 {
1216         uint8 bitsPerPixelId;
1217
1218         cache_bitmap_v2_order->cacheId = flags & 0x0003;
1219         cache_bitmap_v2_order->flags = (flags & 0xFF80) >> 7;
1220
1221         bitsPerPixelId = (flags & 0x0078) >> 3;
1222         cache_bitmap_v2_order->bitmapBpp = CBR2_BPP[bitsPerPixelId];
1223
1224         if (cache_bitmap_v2_order->flags & CBR2_PERSISTENT_KEY_PRESENT)
1225         {
1226                 stream_read_uint32(s, cache_bitmap_v2_order->key1); /* key1 (4 bytes) */
1227                 stream_read_uint32(s, cache_bitmap_v2_order->key2); /* key2 (4 bytes) */
1228         }
1229
1230         if (cache_bitmap_v2_order->flags & CBR2_HEIGHT_SAME_AS_WIDTH)
1231         {
1232                 update_read_2byte_unsigned(s, &cache_bitmap_v2_order->bitmapWidth); /* bitmapWidth */
1233                 cache_bitmap_v2_order->bitmapHeight = cache_bitmap_v2_order->bitmapWidth;
1234         }
1235         else
1236         {
1237                 update_read_2byte_unsigned(s, &cache_bitmap_v2_order->bitmapWidth); /* bitmapWidth */
1238                 update_read_2byte_unsigned(s, &cache_bitmap_v2_order->bitmapHeight); /* bitmapHeight */
1239         }
1240
1241         update_read_4byte_unsigned(s, &cache_bitmap_v2_order->bitmapLength); /* bitmapLength */
1242         update_read_2byte_unsigned(s, &cache_bitmap_v2_order->cacheIndex); /* cacheIndex */
1243
1244         if (cache_bitmap_v2_order->flags & CBR2_DO_NOT_CACHE)
1245                 cache_bitmap_v2_order->cacheIndex = BITMAP_CACHE_WAITING_LIST_INDEX;
1246
1247         if (compressed)
1248         {
1249                 if (!(cache_bitmap_v2_order->flags & CBR2_NO_BITMAP_COMPRESSION_HDR))
1250                 {
1251                         stream_read_uint16(s, cache_bitmap_v2_order->cbCompFirstRowSize); /* cbCompFirstRowSize (2 bytes) */
1252                         stream_read_uint16(s, cache_bitmap_v2_order->cbCompMainBodySize); /* cbCompMainBodySize (2 bytes) */
1253                         stream_read_uint16(s, cache_bitmap_v2_order->cbScanWidth); /* cbScanWidth (2 bytes) */
1254                         stream_read_uint16(s, cache_bitmap_v2_order->cbUncompressedSize); /* cbUncompressedSize (2 bytes) */
1255                         cache_bitmap_v2_order->bitmapLength = cache_bitmap_v2_order->cbCompMainBodySize;
1256                 }
1257
1258                 stream_get_mark(s, cache_bitmap_v2_order->bitmapDataStream);
1259                 stream_seek(s, cache_bitmap_v2_order->bitmapLength);
1260         }
1261         else
1262         {
1263                 stream_get_mark(s, cache_bitmap_v2_order->bitmapDataStream);
1264                 stream_seek(s, cache_bitmap_v2_order->bitmapLength);
1265         }
1266         cache_bitmap_v2_order->compressed = compressed;
1267 }
1268
1269 void update_read_cache_bitmap_v3_order(STREAM* s, CACHE_BITMAP_V3_ORDER* cache_bitmap_v3_order, boolean compressed, uint16 flags)
1270 {
1271         uint8 bitsPerPixelId;
1272         BITMAP_DATA_EX* bitmapData;
1273
1274         cache_bitmap_v3_order->cacheId = flags & 0x00000003;
1275         cache_bitmap_v3_order->flags = (flags & 0x0000FF80) >> 7;
1276
1277         bitsPerPixelId = (flags & 0x00000078) >> 3;
1278         cache_bitmap_v3_order->bpp = CBR23_BPP[bitsPerPixelId];
1279
1280         stream_read_uint16(s, cache_bitmap_v3_order->cacheIndex); /* cacheIndex (2 bytes) */
1281         stream_read_uint32(s, cache_bitmap_v3_order->key1); /* key1 (4 bytes) */
1282         stream_read_uint32(s, cache_bitmap_v3_order->key2); /* key2 (4 bytes) */
1283
1284         bitmapData = &cache_bitmap_v3_order->bitmapData;
1285
1286         stream_read_uint8(s, bitmapData->bpp);
1287         stream_seek_uint8(s); /* reserved1 (1 byte) */
1288         stream_seek_uint8(s); /* reserved2 (1 byte) */
1289         stream_read_uint8(s, bitmapData->codecID); /* codecID (1 byte) */
1290         stream_read_uint16(s, bitmapData->width); /* width (2 bytes) */
1291         stream_read_uint16(s, bitmapData->height); /* height (2 bytes) */
1292         stream_read_uint32(s, bitmapData->length); /* length (4 bytes) */
1293
1294         if (bitmapData->data == NULL)
1295                 bitmapData->data = (uint8*) xmalloc(bitmapData->length);
1296         else
1297                 bitmapData->data = (uint8*) xrealloc(bitmapData->data, bitmapData->length);
1298
1299         stream_read(s, bitmapData->data, bitmapData->length);
1300 }
1301
1302 void update_read_cache_color_table_order(STREAM* s, CACHE_COLOR_TABLE_ORDER* cache_color_table_order, uint16 flags)
1303 {
1304         int i;
1305         uint32* colorTable;
1306
1307         stream_read_uint8(s, cache_color_table_order->cacheIndex); /* cacheIndex (1 byte) */
1308         stream_read_uint8(s, cache_color_table_order->numberColors); /* numberColors (2 bytes) */
1309
1310         colorTable = cache_color_table_order->colorTable;
1311
1312         if (colorTable == NULL)
1313                 colorTable = (uint32*) xmalloc(cache_color_table_order->numberColors * 4);
1314         else
1315                 colorTable = (uint32*) xrealloc(colorTable, cache_color_table_order->numberColors * 4);
1316
1317         for (i = 0; i < (int) cache_color_table_order->numberColors; i++)
1318         {
1319                 update_read_color_quad(s, &colorTable[i]);
1320         }
1321
1322         cache_color_table_order->colorTable = colorTable;
1323 }
1324
1325 void update_read_cache_glyph_order(STREAM* s, CACHE_GLYPH_ORDER* cache_glyph_order, uint16 flags)
1326 {
1327         int i;
1328         sint16 lsi16;
1329         GLYPH_DATA* glyph;
1330
1331         stream_read_uint8(s, cache_glyph_order->cacheId); /* cacheId (1 byte) */
1332         stream_read_uint8(s, cache_glyph_order->cGlyphs); /* cGlyphs (1 byte) */
1333
1334         for (i = 0; i < (int) cache_glyph_order->cGlyphs; i++)
1335         {
1336                 if (cache_glyph_order->glyphData[i] == NULL)
1337                 {
1338                         cache_glyph_order->glyphData[i] = (GLYPH_DATA*) xmalloc(sizeof(GLYPH_DATA));
1339                 }
1340                 glyph = cache_glyph_order->glyphData[i];
1341
1342                 stream_read_uint16(s, glyph->cacheIndex);
1343                 stream_read_uint16(s, lsi16);
1344                 glyph->x = lsi16;
1345                 stream_read_uint16(s, lsi16);
1346                 glyph->y = lsi16;
1347                 stream_read_uint16(s, glyph->cx);
1348                 stream_read_uint16(s, glyph->cy);
1349
1350                 glyph->cb = ((glyph->cx + 7) / 8) * glyph->cy;
1351                 glyph->cb += ((glyph->cb % 4) > 0) ? 4 - (glyph->cb % 4) : 0;
1352
1353                 glyph->aj = (uint8*) xmalloc(glyph->cb);
1354
1355                 stream_read(s, glyph->aj, glyph->cb);
1356         }
1357
1358         if (flags & CG_GLYPH_UNICODE_PRESENT)
1359                 stream_seek(s, cache_glyph_order->cGlyphs * 2);
1360 }
1361
1362 void update_read_cache_glyph_v2_order(STREAM* s, CACHE_GLYPH_V2_ORDER* cache_glyph_v2_order, uint16 flags)
1363 {
1364         int i;
1365         GLYPH_DATA_V2* glyph;
1366
1367         cache_glyph_v2_order->cacheId = (flags & 0x000F);
1368         cache_glyph_v2_order->flags = (flags & 0x00F0) >> 4;
1369         cache_glyph_v2_order->cGlyphs = (flags & 0xFF00) >> 8;
1370
1371         for (i = 0; i < (int) cache_glyph_v2_order->cGlyphs; i++)
1372         {
1373                 if (cache_glyph_v2_order->glyphData[i] == NULL)
1374                 {
1375                         cache_glyph_v2_order->glyphData[i] = (GLYPH_DATA_V2*) xmalloc(sizeof(GLYPH_DATA_V2));
1376                 }
1377                 glyph = cache_glyph_v2_order->glyphData[i];
1378
1379                 stream_read_uint8(s, glyph->cacheIndex);
1380                 update_read_2byte_signed(s, &glyph->x);
1381                 update_read_2byte_signed(s, &glyph->y);
1382                 update_read_2byte_unsigned(s, &glyph->cx);
1383                 update_read_2byte_unsigned(s, &glyph->cy);
1384
1385                 glyph->cb = ((glyph->cx + 7) / 8) * glyph->cy;
1386                 glyph->cb += ((glyph->cb % 4) > 0) ? 4 - (glyph->cb % 4) : 0;
1387
1388                 glyph->aj = (uint8*) xmalloc(glyph->cb);
1389
1390                 stream_read(s, glyph->aj, glyph->cb);
1391         }
1392
1393         if (flags & CG_GLYPH_UNICODE_PRESENT)
1394                 stream_seek(s, cache_glyph_v2_order->cGlyphs * 2);
1395 }
1396
1397 void update_decompress_brush(STREAM* s, uint8* output, uint8 bpp)
1398 {
1399         int index;
1400         int x, y, k;
1401         uint8 byte = 0;
1402         uint8* palette;
1403         int bytesPerPixel;
1404
1405         palette = s->p + 16;
1406         bytesPerPixel = ((bpp + 1) / 8);
1407
1408         for (y = 7; y >= 0; y--)
1409         {
1410                 for (x = 0; x < 8; x++)
1411                 {
1412                         if ((x % 4) == 0)
1413                                 stream_read_uint8(s, byte);
1414
1415                         index = ((byte >> ((3 - (x % 4)) * 2)) & 0x03);
1416
1417                         for (k = 0; k < bytesPerPixel; k++)
1418                         {
1419                                 output[((y * 8 + x) * bytesPerPixel) + k] = palette[(index * bytesPerPixel) + k];
1420                         }
1421                 }
1422         }
1423 }
1424
1425 void update_read_cache_brush_order(STREAM* s, CACHE_BRUSH_ORDER* cache_brush_order, uint16 flags)
1426 {
1427         int i;
1428         int size;
1429         uint8 iBitmapFormat;
1430         boolean compressed = false;
1431
1432         stream_read_uint8(s, cache_brush_order->index); /* cacheEntry (1 byte) */
1433
1434         stream_read_uint8(s, iBitmapFormat); /* iBitmapFormat (1 byte) */
1435         cache_brush_order->bpp = BMF_BPP[iBitmapFormat];
1436
1437         stream_read_uint8(s, cache_brush_order->cx); /* cx (1 byte) */
1438         stream_read_uint8(s, cache_brush_order->cy); /* cy (1 byte) */
1439         stream_read_uint8(s, cache_brush_order->style); /* style (1 byte) */
1440         stream_read_uint8(s, cache_brush_order->length); /* iBytes (1 byte) */
1441
1442         if ((cache_brush_order->cx == 8) && (cache_brush_order->cy == 8))
1443         {
1444                 size = (cache_brush_order->bpp == 1) ? 8 : 8 * 8 * cache_brush_order->bpp;
1445
1446                 cache_brush_order->data = (uint8*) xmalloc(size);
1447
1448                 if (cache_brush_order->bpp == 1)
1449                 {
1450                         if (cache_brush_order->length != 8)
1451                         {
1452                                 printf("incompatible 1bpp brush of length:%d\n", cache_brush_order->length);
1453                                 return;
1454                         }
1455
1456                         /* rows are encoded in reverse order */
1457
1458                         for (i = 7; i >= 0; i--)
1459                         {
1460                                 stream_read_uint8(s, cache_brush_order->data[i]);
1461                         }
1462                 }
1463                 else
1464                 {
1465                         if ((iBitmapFormat == BMF_8BPP) && (cache_brush_order->length == 20))
1466                                 compressed = true;
1467                         else if ((iBitmapFormat == BMF_16BPP) && (cache_brush_order->length == 24))
1468                                 compressed = true;
1469                         else if ((iBitmapFormat == BMF_32BPP) && (cache_brush_order->length == 32))
1470                                 compressed = true;
1471
1472                         if (compressed != false)
1473                         {
1474                                 /* compressed brush */
1475                                 update_decompress_brush(s, cache_brush_order->data, cache_brush_order->bpp);
1476                         }
1477                         else
1478                         {
1479                                 /* uncompressed brush */
1480                                 int scanline = (cache_brush_order->bpp / 8) * 8;
1481
1482                                 for (i = 7; i >= 0; i--)
1483                                 {
1484                                         stream_read(s, &cache_brush_order->data[i * scanline], scanline);
1485                                 }
1486                         }
1487                 }
1488         }
1489 }
1490
1491 /* Alternate Secondary Drawing Orders */
1492
1493 void update_read_create_offscreen_bitmap_order(STREAM* s, CREATE_OFFSCREEN_BITMAP_ORDER* create_offscreen_bitmap)
1494 {
1495         uint16 flags;
1496         boolean deleteListPresent;
1497         OFFSCREEN_DELETE_LIST* deleteList;
1498
1499         stream_read_uint16(s, flags); /* flags (2 bytes) */
1500         create_offscreen_bitmap->id = flags & 0x7FFF;
1501         deleteListPresent = (flags & 0x8000) ? true : false;
1502
1503         stream_read_uint16(s, create_offscreen_bitmap->cx); /* cx (2 bytes) */
1504         stream_read_uint16(s, create_offscreen_bitmap->cy); /* cy (2 bytes) */
1505
1506         deleteList = &(create_offscreen_bitmap->deleteList);
1507         if (deleteListPresent)
1508         {
1509                 int i;
1510
1511                 stream_read_uint16(s, deleteList->cIndices);
1512
1513                 if (deleteList->cIndices > deleteList->sIndices)
1514                 {
1515                         deleteList->sIndices = deleteList->cIndices;
1516                         deleteList->indices = xrealloc(deleteList->indices, deleteList->sIndices * 2);
1517                 }
1518
1519                 for (i = 0; i < (int) deleteList->cIndices; i++)
1520                 {
1521                         stream_read_uint16(s, deleteList->indices[i]);
1522                 }
1523         }
1524         else
1525         {
1526                 deleteList->cIndices = 0;
1527         }
1528 }
1529
1530 void update_read_switch_surface_order(STREAM* s, SWITCH_SURFACE_ORDER* switch_surface)
1531 {
1532         stream_read_uint16(s, switch_surface->bitmapId); /* bitmapId (2 bytes) */
1533 }
1534
1535 void update_read_create_nine_grid_bitmap_order(STREAM* s, CREATE_NINE_GRID_BITMAP_ORDER* create_nine_grid_bitmap)
1536 {
1537         NINE_GRID_BITMAP_INFO* nineGridInfo;
1538
1539         stream_read_uint8(s, create_nine_grid_bitmap->bitmapBpp); /* bitmapBpp (1 byte) */
1540         stream_read_uint16(s, create_nine_grid_bitmap->bitmapId); /* bitmapId (2 bytes) */
1541
1542         nineGridInfo = &(create_nine_grid_bitmap->nineGridInfo);
1543         stream_read_uint32(s, nineGridInfo->flFlags); /* flFlags (4 bytes) */
1544         stream_read_uint16(s, nineGridInfo->ulLeftWidth); /* ulLeftWidth (2 bytes) */
1545         stream_read_uint16(s, nineGridInfo->ulRightWidth); /* ulRightWidth (2 bytes) */
1546         stream_read_uint16(s, nineGridInfo->ulTopHeight); /* ulTopHeight (2 bytes) */
1547         stream_read_uint16(s, nineGridInfo->ulBottomHeight); /* ulBottomHeight (2 bytes) */
1548         update_read_colorref(s, &nineGridInfo->crTransparent); /* crTransparent (4 bytes) */
1549 }
1550
1551 void update_read_frame_marker_order(STREAM* s, FRAME_MARKER_ORDER* frame_marker)
1552 {
1553         stream_read_uint32(s, frame_marker->action); /* action (4 bytes) */
1554 }
1555
1556 void update_read_stream_bitmap_first_order(STREAM* s, STREAM_BITMAP_FIRST_ORDER* stream_bitmap_first)
1557 {
1558         stream_read_uint8(s, stream_bitmap_first->bitmapFlags); /* bitmapFlags (1 byte) */
1559         stream_read_uint8(s, stream_bitmap_first->bitmapBpp); /* bitmapBpp (1 byte) */
1560         stream_read_uint16(s, stream_bitmap_first->bitmapType); /* bitmapType (2 bytes) */
1561         stream_read_uint16(s, stream_bitmap_first->bitmapWidth); /* bitmapWidth (2 bytes) */
1562         stream_read_uint16(s, stream_bitmap_first->bitmapHeight); /* bitmapHeigth (2 bytes) */
1563
1564         if (stream_bitmap_first->bitmapFlags & STREAM_BITMAP_V2)
1565                 stream_read_uint32(s, stream_bitmap_first->bitmapSize); /* bitmapSize (4 bytes) */
1566         else
1567                 stream_read_uint16(s, stream_bitmap_first->bitmapSize); /* bitmapSize (2 bytes) */
1568
1569         stream_read_uint16(s, stream_bitmap_first->bitmapBlockSize); /* bitmapBlockSize (2 bytes) */
1570         stream_seek(s, stream_bitmap_first->bitmapBlockSize); /* bitmapBlock */
1571 }
1572
1573 void update_read_stream_bitmap_next_order(STREAM* s, STREAM_BITMAP_FIRST_ORDER* stream_bitmap_next)
1574 {
1575         stream_read_uint8(s, stream_bitmap_next->bitmapFlags); /* bitmapFlags (1 byte) */
1576         stream_read_uint16(s, stream_bitmap_next->bitmapType); /* bitmapType (2 bytes) */
1577         stream_read_uint16(s, stream_bitmap_next->bitmapBlockSize); /* bitmapBlockSize (2 bytes) */
1578         stream_seek(s, stream_bitmap_next->bitmapBlockSize); /* bitmapBlock */
1579 }
1580
1581 void update_read_draw_gdiplus_first_order(STREAM* s, DRAW_GDIPLUS_FIRST_ORDER* draw_gdiplus_first)
1582 {
1583         stream_seek_uint8(s); /* pad1Octet (1 byte) */
1584         stream_read_uint16(s, draw_gdiplus_first->cbSize); /* cbSize (2 bytes) */
1585         stream_read_uint32(s, draw_gdiplus_first->cbTotalSize); /* cbTotalSize (4 bytes) */
1586         stream_read_uint32(s, draw_gdiplus_first->cbTotalEmfSize); /* cbTotalEmfSize (4 bytes) */
1587         stream_seek(s, draw_gdiplus_first->cbSize); /* emfRecords */
1588 }
1589
1590 void update_read_draw_gdiplus_next_order(STREAM* s, DRAW_GDIPLUS_NEXT_ORDER* draw_gdiplus_next)
1591 {
1592         stream_seek_uint8(s); /* pad1Octet (1 byte) */
1593         stream_read_uint16(s, draw_gdiplus_next->cbSize); /* cbSize (2 bytes) */
1594         stream_seek(s, draw_gdiplus_next->cbSize); /* emfRecords */
1595 }
1596
1597 void update_read_draw_gdiplus_end_order(STREAM* s, DRAW_GDIPLUS_END_ORDER* draw_gdiplus_end)
1598 {
1599         stream_seek_uint8(s); /* pad1Octet (1 byte) */
1600         stream_read_uint16(s, draw_gdiplus_end->cbSize); /* cbSize (2 bytes) */
1601         stream_read_uint32(s, draw_gdiplus_end->cbTotalSize); /* cbTotalSize (4 bytes) */
1602         stream_read_uint32(s, draw_gdiplus_end->cbTotalEmfSize); /* cbTotalEmfSize (4 bytes) */
1603         stream_seek(s, draw_gdiplus_end->cbSize); /* emfRecords */
1604 }
1605
1606 void update_read_draw_gdiplus_cache_first_order(STREAM* s, DRAW_GDIPLUS_CACHE_FIRST_ORDER* draw_gdiplus_cache_first)
1607 {
1608         stream_read_uint8(s, draw_gdiplus_cache_first->flags); /* flags (1 byte) */
1609         stream_read_uint16(s, draw_gdiplus_cache_first->cacheType); /* cacheType (2 bytes) */
1610         stream_read_uint16(s, draw_gdiplus_cache_first->cacheIndex); /* cacheIndex (2 bytes) */
1611         stream_read_uint16(s, draw_gdiplus_cache_first->cbSize); /* cbSize (2 bytes) */
1612         stream_read_uint32(s, draw_gdiplus_cache_first->cbTotalSize); /* cbTotalSize (4 bytes) */
1613         stream_seek(s, draw_gdiplus_cache_first->cbSize); /* emfRecords */
1614 }
1615
1616 void update_read_draw_gdiplus_cache_next_order(STREAM* s, DRAW_GDIPLUS_CACHE_NEXT_ORDER* draw_gdiplus_cache_next)
1617 {
1618         stream_read_uint8(s, draw_gdiplus_cache_next->flags); /* flags (1 byte) */
1619         stream_read_uint16(s, draw_gdiplus_cache_next->cacheType); /* cacheType (2 bytes) */
1620         stream_read_uint16(s, draw_gdiplus_cache_next->cacheIndex); /* cacheIndex (2 bytes) */
1621         stream_read_uint16(s, draw_gdiplus_cache_next->cbSize); /* cbSize (2 bytes) */
1622         stream_seek(s, draw_gdiplus_cache_next->cbSize); /* emfRecords */
1623 }
1624
1625 void update_read_draw_gdiplus_cache_end_order(STREAM* s, DRAW_GDIPLUS_CACHE_END_ORDER* draw_gdiplus_cache_end)
1626 {
1627         stream_read_uint8(s, draw_gdiplus_cache_end->flags); /* flags (1 byte) */
1628         stream_read_uint16(s, draw_gdiplus_cache_end->cacheType); /* cacheType (2 bytes) */
1629         stream_read_uint16(s, draw_gdiplus_cache_end->cacheIndex); /* cacheIndex (2 bytes) */
1630         stream_read_uint16(s, draw_gdiplus_cache_end->cbSize); /* cbSize (2 bytes) */
1631         stream_read_uint32(s, draw_gdiplus_cache_end->cbTotalSize); /* cbTotalSize (4 bytes) */
1632         stream_seek(s, draw_gdiplus_cache_end->cbSize); /* emfRecords */
1633 }
1634
1635 void update_read_field_flags(STREAM* s, uint32* fieldFlags, uint8 flags, uint8 fieldBytes)
1636 {
1637         int i;
1638         uint8 byte;
1639
1640         if (flags & ORDER_ZERO_FIELD_BYTE_BIT0)
1641                 fieldBytes--;
1642
1643         if (flags & ORDER_ZERO_FIELD_BYTE_BIT1)
1644         {
1645                 if (fieldBytes > 1)
1646                         fieldBytes -= 2;
1647                 else
1648                         fieldBytes = 0;
1649         }
1650
1651         *fieldFlags = 0;
1652         for (i = 0; i < fieldBytes; i++)
1653         {
1654                 stream_read_uint8(s, byte);
1655                 *fieldFlags |= byte << (i * 8);
1656         }
1657 }
1658
1659 void update_read_bounds(STREAM* s, rdpBounds* bounds)
1660 {
1661         uint8 flags;
1662
1663         stream_read_uint8(s, flags); /* field flags */
1664
1665         if (flags & BOUND_LEFT)
1666                 update_read_coord(s, &bounds->left, false);
1667         else if (flags & BOUND_DELTA_LEFT)
1668                 update_read_coord(s, &bounds->left, true);
1669
1670         if (flags & BOUND_TOP)
1671                 update_read_coord(s, &bounds->top, false);
1672         else if (flags & BOUND_DELTA_TOP)
1673                 update_read_coord(s, &bounds->top, true);
1674
1675         if (flags & BOUND_RIGHT)
1676                 update_read_coord(s, &bounds->right, false);
1677         else if (flags & BOUND_DELTA_RIGHT)
1678                 update_read_coord(s, &bounds->right, true);
1679
1680         if (flags & BOUND_BOTTOM)
1681                 update_read_coord(s, &bounds->bottom, false);
1682         else if (flags & BOUND_DELTA_BOTTOM)
1683                 update_read_coord(s, &bounds->bottom, true);
1684 }
1685
1686 void update_recv_primary_order(rdpUpdate* update, STREAM* s, uint8 flags)
1687 {
1688         ORDER_INFO* orderInfo;
1689         rdpContext* context = update->context;
1690         rdpPrimaryUpdate* primary = update->primary;
1691
1692         orderInfo = &(primary->order_info);
1693
1694         if (flags & ORDER_TYPE_CHANGE)
1695                 stream_read_uint8(s, orderInfo->orderType); /* orderType (1 byte) */
1696
1697         update_read_field_flags(s, &(orderInfo->fieldFlags), flags,
1698                         PRIMARY_DRAWING_ORDER_FIELD_BYTES[orderInfo->orderType]);
1699
1700         if (flags & ORDER_BOUNDS)
1701         {
1702                 if (!(flags & ORDER_ZERO_BOUNDS_DELTAS))
1703                         update_read_bounds(s, &orderInfo->bounds);
1704
1705                 IFCALL(update->SetBounds, context, &orderInfo->bounds);
1706         }
1707
1708         orderInfo->deltaCoordinates = (flags & ORDER_DELTA_COORDINATES) ? true : false;
1709
1710 #ifdef WITH_DEBUG_ORDERS
1711         if (orderInfo->orderType < PRIMARY_DRAWING_ORDER_COUNT)
1712                 printf("%s Primary Drawing Order (0x%02X)\n", PRIMARY_DRAWING_ORDER_STRINGS[orderInfo->orderType], orderInfo->orderType);
1713         else
1714                 printf("Unknown Primary Drawing Order (0x%02X)\n", orderInfo->orderType);
1715 #endif
1716
1717         switch (orderInfo->orderType)
1718         {
1719                 case ORDER_TYPE_DSTBLT:
1720                         update_read_dstblt_order(s, orderInfo, &(primary->dstblt));
1721                         IFCALL(primary->DstBlt, context, &primary->dstblt);
1722                         break;
1723
1724                 case ORDER_TYPE_PATBLT:
1725                         update_read_patblt_order(s, orderInfo, &(primary->patblt));
1726                         IFCALL(primary->PatBlt, context, &primary->patblt);
1727                         break;
1728
1729                 case ORDER_TYPE_SCRBLT:
1730                         update_read_scrblt_order(s, orderInfo, &(primary->scrblt));
1731                         IFCALL(primary->ScrBlt, context, &primary->scrblt);
1732                         break;
1733
1734                 case ORDER_TYPE_OPAQUE_RECT:
1735                         update_read_opaque_rect_order(s, orderInfo, &(primary->opaque_rect));
1736                         IFCALL(primary->OpaqueRect, context, &primary->opaque_rect);
1737                         break;
1738
1739                 case ORDER_TYPE_DRAW_NINE_GRID:
1740                         update_read_draw_nine_grid_order(s, orderInfo, &(primary->draw_nine_grid));
1741                         IFCALL(primary->DrawNineGrid, context, &primary->draw_nine_grid);
1742                         break;
1743
1744                 case ORDER_TYPE_MULTI_DSTBLT:
1745                         update_read_multi_dstblt_order(s, orderInfo, &(primary->multi_dstblt));
1746                         IFCALL(primary->MultiDstBlt, context, &primary->multi_dstblt);
1747                         break;
1748
1749                 case ORDER_TYPE_MULTI_PATBLT:
1750                         update_read_multi_patblt_order(s, orderInfo, &(primary->multi_patblt));
1751                         IFCALL(primary->MultiPatBlt, context, &primary->multi_patblt);
1752                         break;
1753
1754                 case ORDER_TYPE_MULTI_SCRBLT:
1755                         update_read_multi_scrblt_order(s, orderInfo, &(primary->multi_scrblt));
1756                         IFCALL(primary->MultiScrBlt, context, &primary->multi_scrblt);
1757                         break;
1758
1759                 case ORDER_TYPE_MULTI_OPAQUE_RECT:
1760                         update_read_multi_opaque_rect_order(s, orderInfo, &(primary->multi_opaque_rect));
1761                         IFCALL(primary->MultiOpaqueRect, context, &primary->multi_opaque_rect);
1762                         break;
1763
1764                 case ORDER_TYPE_MULTI_DRAW_NINE_GRID:
1765                         update_read_multi_draw_nine_grid_order(s, orderInfo, &(primary->multi_draw_nine_grid));
1766                         IFCALL(primary->MultiDrawNineGrid, context, &primary->multi_draw_nine_grid);
1767                         break;
1768
1769                 case ORDER_TYPE_LINE_TO:
1770                         update_read_line_to_order(s, orderInfo, &(primary->line_to));
1771                         IFCALL(primary->LineTo, context, &primary->line_to);
1772                         break;
1773
1774                 case ORDER_TYPE_POLYLINE:
1775                         update_read_polyline_order(s, orderInfo, &(primary->polyline));
1776                         IFCALL(primary->Polyline, context, &primary->polyline);
1777                         break;
1778
1779                 case ORDER_TYPE_MEMBLT:
1780                         update_read_memblt_order(s, orderInfo, &(primary->memblt));
1781                         IFCALL(primary->MemBlt, context, &primary->memblt);
1782                         break;
1783
1784                 case ORDER_TYPE_MEM3BLT:
1785                         update_read_mem3blt_order(s, orderInfo, &(primary->mem3blt));
1786                         IFCALL(primary->Mem3Blt, context, &primary->mem3blt);
1787                         break;
1788
1789                 case ORDER_TYPE_SAVE_BITMAP:
1790                         update_read_save_bitmap_order(s, orderInfo, &(primary->save_bitmap));
1791                         IFCALL(primary->SaveBitmap, context, &primary->save_bitmap);
1792                         break;
1793
1794                 case ORDER_TYPE_GLYPH_INDEX:
1795                         update_read_glyph_index_order(s, orderInfo, &(primary->glyph_index));
1796                         IFCALL(primary->GlyphIndex, context, &primary->glyph_index);
1797                         break;
1798
1799                 case ORDER_TYPE_FAST_INDEX:
1800                         update_read_fast_index_order(s, orderInfo, &(primary->fast_index));
1801                         IFCALL(primary->FastIndex, context, &primary->fast_index);
1802                         break;
1803
1804                 case ORDER_TYPE_FAST_GLYPH:
1805                         update_read_fast_glyph_order(s, orderInfo, &(primary->fast_glyph));
1806                         IFCALL(primary->FastGlyph, context, &primary->fast_glyph);
1807                         break;
1808
1809                 case ORDER_TYPE_POLYGON_SC:
1810                         update_read_polygon_sc_order(s, orderInfo, &(primary->polygon_sc));
1811                         IFCALL(primary->PolygonSC, context, &primary->polygon_sc);
1812                         break;
1813
1814                 case ORDER_TYPE_POLYGON_CB:
1815                         update_read_polygon_cb_order(s, orderInfo, &(primary->polygon_cb));
1816                         IFCALL(primary->PolygonCB, context, &primary->polygon_cb);
1817                         break;
1818
1819                 case ORDER_TYPE_ELLIPSE_SC:
1820                         update_read_ellipse_sc_order(s, orderInfo, &(primary->ellipse_sc));
1821                         IFCALL(primary->EllipseSC, context, &primary->ellipse_sc);
1822                         break;
1823
1824                 case ORDER_TYPE_ELLIPSE_CB:
1825                         update_read_ellipse_cb_order(s, orderInfo, &(primary->ellipse_cb));
1826                         IFCALL(primary->EllipseCB, context, &primary->ellipse_cb);
1827                         break;
1828
1829                 default:
1830                         break;
1831         }
1832
1833         if (flags & ORDER_BOUNDS)
1834         {
1835                 IFCALL(update->SetBounds, context, NULL);
1836         }
1837 }
1838
1839 void update_recv_secondary_order(rdpUpdate* update, STREAM* s, uint8 flags)
1840 {
1841         uint8* next;
1842         uint8 orderType;
1843         uint16 extraFlags;
1844         uint16 orderLength;
1845         rdpContext* context = update->context;
1846         rdpSecondaryUpdate* secondary = update->secondary;
1847
1848         stream_read_uint16(s, orderLength); /* orderLength (2 bytes) */
1849         stream_read_uint16(s, extraFlags); /* extraFlags (2 bytes) */
1850         stream_read_uint8(s, orderType); /* orderType (1 byte) */
1851
1852         next = s->p + ((sint16) orderLength) + 7;
1853
1854 #ifdef WITH_DEBUG_ORDERS
1855         if (orderType < SECONDARY_DRAWING_ORDER_COUNT)
1856                 printf("%s Secondary Drawing Order (0x%02X)\n", SECONDARY_DRAWING_ORDER_STRINGS[orderType], orderType);
1857         else
1858                 printf("Unknown Secondary Drawing Order (0x%02X)\n", orderType);
1859 #endif
1860
1861         switch (orderType)
1862         {
1863                 case ORDER_TYPE_BITMAP_UNCOMPRESSED:
1864                         update_read_cache_bitmap_order(s, &(secondary->cache_bitmap_order), false, extraFlags);
1865                         IFCALL(secondary->CacheBitmap, context, &(secondary->cache_bitmap_order));
1866                         break;
1867
1868                 case ORDER_TYPE_CACHE_BITMAP_COMPRESSED:
1869                         update_read_cache_bitmap_order(s, &(secondary->cache_bitmap_order), true, extraFlags);
1870                         IFCALL(secondary->CacheBitmap, context, &(secondary->cache_bitmap_order));
1871                         break;
1872
1873                 case ORDER_TYPE_BITMAP_UNCOMPRESSED_V2:
1874                         update_read_cache_bitmap_v2_order(s, &(secondary->cache_bitmap_v2_order), false, extraFlags);
1875                         IFCALL(secondary->CacheBitmapV2, context, &(secondary->cache_bitmap_v2_order));
1876                         break;
1877
1878                 case ORDER_TYPE_BITMAP_COMPRESSED_V2:
1879                         update_read_cache_bitmap_v2_order(s, &(secondary->cache_bitmap_v2_order), true, extraFlags);
1880                         IFCALL(secondary->CacheBitmapV2, context, &(secondary->cache_bitmap_v2_order));
1881                         break;
1882
1883                 case ORDER_TYPE_BITMAP_COMPRESSED_V3:
1884                         update_read_cache_bitmap_v3_order(s, &(secondary->cache_bitmap_v3_order), true, extraFlags);
1885                         IFCALL(secondary->CacheBitmapV3, context, &(secondary->cache_bitmap_v3_order));
1886                         break;
1887
1888                 case ORDER_TYPE_CACHE_COLOR_TABLE:
1889                         update_read_cache_color_table_order(s, &(secondary->cache_color_table_order), extraFlags);
1890                         IFCALL(secondary->CacheColorTable, context, &(secondary->cache_color_table_order));
1891                         break;
1892
1893                 case ORDER_TYPE_CACHE_GLYPH:
1894                         if (secondary->glyph_v2)
1895                         {
1896                                 update_read_cache_glyph_v2_order(s, &(secondary->cache_glyph_v2_order), extraFlags);
1897                                 IFCALL(secondary->CacheGlyphV2, context, &(secondary->cache_glyph_v2_order));
1898                         }
1899                         else
1900                         {
1901                                 update_read_cache_glyph_order(s, &(secondary->cache_glyph_order), extraFlags);
1902                                 IFCALL(secondary->CacheGlyph, context, &(secondary->cache_glyph_order));
1903                         }
1904                         break;
1905
1906                 case ORDER_TYPE_CACHE_BRUSH:
1907                         update_read_cache_brush_order(s, &(secondary->cache_brush_order), extraFlags);
1908                         IFCALL(secondary->CacheBrush, context, &(secondary->cache_brush_order));
1909                         break;
1910
1911                 default:
1912                         break;
1913         }
1914
1915         s->p = next;
1916 }
1917
1918 void update_recv_altsec_order(rdpUpdate* update, STREAM* s, uint8 flags)
1919 {
1920         uint8 orderType;
1921         rdpContext* context = update->context;
1922         rdpAltSecUpdate* altsec = update->altsec;
1923
1924         orderType = flags >>= 2; /* orderType is in higher 6 bits of flags field */
1925
1926 #ifdef WITH_DEBUG_ORDERS
1927         if (orderType < ALTSEC_DRAWING_ORDER_COUNT)
1928                 printf("%s Alternate Secondary Drawing Order (0x%02X)\n", ALTSEC_DRAWING_ORDER_STRINGS[orderType], orderType);
1929         else
1930                 printf("Unknown Alternate Secondary Drawing Order: 0x%02X\n", orderType);
1931 #endif
1932
1933         switch (orderType)
1934         {
1935                 case ORDER_TYPE_CREATE_OFFSCREEN_BITMAP:
1936                         update_read_create_offscreen_bitmap_order(s, &(altsec->create_offscreen_bitmap));
1937                         IFCALL(altsec->CreateOffscreenBitmap, context, &(altsec->create_offscreen_bitmap));
1938                         break;
1939
1940                 case ORDER_TYPE_SWITCH_SURFACE:
1941                         update_read_switch_surface_order(s, &(altsec->switch_surface));
1942                         IFCALL(altsec->SwitchSurface, context, &(altsec->switch_surface));
1943                         break;
1944
1945                 case ORDER_TYPE_CREATE_NINE_GRID_BITMAP:
1946                         update_read_create_nine_grid_bitmap_order(s, &(altsec->create_nine_grid_bitmap));
1947                         IFCALL(altsec->CreateNineGridBitmap, context, &(altsec->create_nine_grid_bitmap));
1948                         break;
1949
1950                 case ORDER_TYPE_FRAME_MARKER:
1951                         update_read_frame_marker_order(s, &(altsec->frame_marker));
1952                         IFCALL(altsec->FrameMarker, context, &(altsec->frame_marker));
1953                         break;
1954
1955                 case ORDER_TYPE_STREAM_BITMAP_FIRST:
1956                         update_read_stream_bitmap_first_order(s, &(altsec->stream_bitmap_first));
1957                         IFCALL(altsec->StreamBitmapFirst, context, &(altsec->stream_bitmap_first));
1958                         break;
1959
1960                 case ORDER_TYPE_STREAM_BITMAP_NEXT:
1961                         update_read_stream_bitmap_next_order(s, &(altsec->stream_bitmap_next));
1962                         IFCALL(altsec->StreamBitmapNext, context, &(altsec->stream_bitmap_next));
1963                         break;
1964
1965                 case ORDER_TYPE_GDIPLUS_FIRST:
1966                         update_read_draw_gdiplus_first_order(s, &(altsec->draw_gdiplus_first));
1967                         IFCALL(altsec->DrawGdiPlusFirst, context, &(altsec->draw_gdiplus_first));
1968                         break;
1969
1970                 case ORDER_TYPE_GDIPLUS_NEXT:
1971                         update_read_draw_gdiplus_next_order(s, &(altsec->draw_gdiplus_next));
1972                         IFCALL(altsec->DrawGdiPlusNext, context, &(altsec->draw_gdiplus_next));
1973                         break;
1974
1975                 case ORDER_TYPE_GDIPLUS_END:
1976                         update_read_draw_gdiplus_end_order(s, &(altsec->draw_gdiplus_end));
1977                         IFCALL(altsec->DrawGdiPlusEnd, context, &(altsec->draw_gdiplus_end));
1978                         break;
1979
1980                 case ORDER_TYPE_GDIPLUS_CACHE_FIRST:
1981                         update_read_draw_gdiplus_cache_first_order(s, &(altsec->draw_gdiplus_cache_first));
1982                         IFCALL(altsec->DrawGdiPlusCacheFirst, context, &(altsec->draw_gdiplus_cache_first));
1983                         break;
1984
1985                 case ORDER_TYPE_GDIPLUS_CACHE_NEXT:
1986                         update_read_draw_gdiplus_cache_next_order(s, &(altsec->draw_gdiplus_cache_next));
1987                         IFCALL(altsec->DrawGdiPlusCacheNext, context, &(altsec->draw_gdiplus_cache_next));
1988                         break;
1989
1990                 case ORDER_TYPE_GDIPLUS_CACHE_END:
1991                         update_read_draw_gdiplus_cache_end_order(s, &(altsec->draw_gdiplus_cache_end));
1992                         IFCALL(altsec->DrawGdiPlusCacheEnd, context, &(altsec->draw_gdiplus_cache_end));
1993                         break;
1994
1995                 case ORDER_TYPE_WINDOW:
1996                         update_recv_altsec_window_order(update, s);
1997                         break;
1998
1999                 case ORDER_TYPE_COMPDESK_FIRST:
2000                         break;
2001
2002                 default:
2003                         break;
2004         }
2005 }
2006
2007 void update_recv_order(rdpUpdate* update, STREAM* s)
2008 {
2009         uint8 controlFlags;
2010
2011         stream_read_uint8(s, controlFlags); /* controlFlags (1 byte) */
2012
2013         if (!(controlFlags & ORDER_STANDARD))
2014                 update_recv_altsec_order(update, s, controlFlags);
2015         else if (controlFlags & ORDER_SECONDARY)
2016                 update_recv_secondary_order(update, s, controlFlags);
2017         else
2018                 update_recv_primary_order(update, s, controlFlags);
2019 }
2020