FreeRDP
Loading...
Searching...
No Matches
update.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/print.h>
26#include <winpr/synch.h>
27#include <winpr/thread.h>
28#include <winpr/collections.h>
29#include <winpr/assert.h>
30#include <winpr/cast.h>
31
32#include "settings.h"
33#include "update.h"
34#include "surface.h"
35#include "message.h"
36#include "info.h"
37#include "window.h"
38
39#include <freerdp/log.h>
40#include <freerdp/peer.h>
41#include <freerdp/codec/bitmap.h>
42
43#include "../cache/pointer.h"
44#include "../cache/palette.h"
45#include "../cache/bitmap.h"
46
47#define TAG FREERDP_TAG("core.update")
48
49#define FORCE_ASYNC_UPDATE_OFF
50
51#define RDP_STATS_COUNT sizeof(rdp_stats) / sizeof(uint64_t)
52#define bufferlen 64
53
54static INIT_ONCE stats_names_once = INIT_ONCE_STATIC_INIT;
55static char stats_names[RDP_STATS_COUNT][bufferlen];
56
57static BOOL CALLBACK stats_names_generate(WINPR_ATTR_UNUSED PINIT_ONCE InitOnce,
58 WINPR_ATTR_UNUSED PVOID Parameter,
59 WINPR_ATTR_UNUSED PVOID* Context)
60{
61 for (size_t index = 0; index < RDP_STATS_COUNT; index++)
62 {
63 char* buffer = stats_names[index];
64
65 const rdp_stats stats = WINPR_C_ARRAY_INIT;
66 size_t limit = ARRAYSIZE(stats.primary);
67 size_t offset = 0;
68 if (index < limit)
69 {
70 char obuffer[64] = WINPR_C_ARRAY_INIT;
71 const char* str = primary_order_string(WINPR_ASSERTING_INT_CAST(UINT32, index), obuffer,
72 sizeof(obuffer));
73 if (!str)
74 return FALSE;
75 (void)_snprintf(buffer, bufferlen, "RDP_STATS_ORDER_PRIMARY %s", str);
76 WINPR_ASSERT(strnlen(buffer, 2) > 0);
77 continue;
78 }
79
80 offset = limit;
81 limit += ARRAYSIZE(stats.secondary);
82 if (index < limit)
83 {
84 char obuffer[64] = WINPR_C_ARRAY_INIT;
85 const char* str = secondary_order_string(
86 WINPR_ASSERTING_INT_CAST(UINT32, index - offset), obuffer, sizeof(obuffer));
87 if (!str)
88 return FALSE;
89 (void)_snprintf(buffer, bufferlen, "RDP_STATS_ORDER_SECONDARY %s", str);
90 WINPR_ASSERT(strnlen(buffer, 2) > 0);
91 continue;
92 }
93
94 offset = limit;
95 limit += ARRAYSIZE(stats.altsec);
96 if (index < limit)
97 {
98 char obuffer[64] = WINPR_C_ARRAY_INIT;
99 const char* str = altsec_order_string(WINPR_ASSERTING_INT_CAST(BYTE, index - offset),
100 obuffer, sizeof(obuffer));
101 if (!str)
102 return FALSE;
103 (void)_snprintf(buffer, bufferlen, "RDP_STATS_ORDER_ALTSEC %s", str);
104 WINPR_ASSERT(strnlen(buffer, 2) > 0);
105 continue;
106 }
107
108 offset = limit;
109 limit += ARRAYSIZE(stats.base);
110 if (index < limit)
111 {
112#define EVCASE(x) \
113 case x: \
114 (void)_snprintf(buffer, bufferlen, "%s", #x); \
115 break
116
117 switch (index - offset)
118 {
119 EVCASE(RDP_STATS_SURFACE_BITS);
120 EVCASE(RDP_STATS_SURFACE_BITS_NSC);
121 EVCASE(RDP_STATS_SURFACE_BITS_RFX);
122 EVCASE(RDP_STATS_SURFACE_BITS_RFX_IMAGE);
123 EVCASE(RDP_STATS_SURFACE_BITS_NONE);
124 EVCASE(RDP_STATS_SURFACE_BITS_UNKNOWN);
125 EVCASE(RDP_STATS_BEGIN_PAINT);
126 EVCASE(RDP_STATS_END_PAINT);
127 EVCASE(RDP_STATS_SET_BOUNDS);
128 EVCASE(RDP_STATS_SYNC);
129 EVCASE(RDP_STATS_RESIZE);
130 EVCASE(RDP_STATS_BITMAP_UPDATE);
131 EVCASE(RDP_STATS_PALETTE);
132 EVCASE(RDP_STATS_REFRESH_RECT);
133 EVCASE(RDP_STATS_SUPPRESS_OUTPUT);
134 EVCASE(RDP_STATS_SURFACE_COMMAND);
135 EVCASE(RDP_STATS_SURFACE_FRAME_MARKER);
136 EVCASE(RDP_STATS_SURFACE_FRAME_ACK);
137 EVCASE(RDP_STATS_POINTER_SYSTEM);
138 EVCASE(RDP_STATS_POINTER_DEFAULT);
139 EVCASE(RDP_STATS_POINTER_POSITION);
140 EVCASE(RDP_STATS_POINTER_COLOR);
141 EVCASE(RDP_STATS_POINTER_CACHED);
142 EVCASE(RDP_STATS_POINTER_NEW);
143 EVCASE(RDP_STATS_POINTER_LARGE);
144 default:
145 (void)_snprintf(buffer, bufferlen, "RDP_STATS_UNUSED");
146 break;
147 }
148#undef EVCASE
149 }
150 else
151 (void)_snprintf(buffer, bufferlen, "RDP_STATS_UNUSED");
152 }
153
154 return TRUE;
155}
156static const char* const UPDATE_TYPE_STRINGS[] = { "Orders", "Bitmap", "Palette", "Synchronize" };
157
158static const char* update_type_to_string(UINT16 updateType)
159{
160 if (updateType >= ARRAYSIZE(UPDATE_TYPE_STRINGS))
161 return "UNKNOWN";
162
163 return UPDATE_TYPE_STRINGS[updateType];
164}
165
166static BOOL update_recv_orders(rdpUpdate* update, wStream* s)
167{
168 UINT16 numberOrders = 0;
169
170 WINPR_ASSERT(update);
171
172 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
173 return FALSE;
174
175 Stream_Seek_UINT16(s); /* pad2OctetsA (2 bytes) */
176 Stream_Read_UINT16(s, numberOrders); /* numberOrders (2 bytes) */
177 Stream_Seek_UINT16(s); /* pad2OctetsB (2 bytes) */
178
179 while (numberOrders > 0)
180 {
181 if (!update_recv_order(update, s))
182 {
183 WLog_ERR(TAG, "update_recv_order() failed");
184 return FALSE;
185 }
186
187 numberOrders--;
188 }
189
190 return TRUE;
191}
192
193static BOOL update_read_bitmap_data(rdpUpdate* update, wStream* s, BITMAP_DATA* bitmapData)
194{
195 WINPR_UNUSED(update);
196 WINPR_ASSERT(bitmapData);
197
198 if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
199 return FALSE;
200
201 Stream_Read_UINT16(s, bitmapData->destLeft);
202 Stream_Read_UINT16(s, bitmapData->destTop);
203 Stream_Read_UINT16(s, bitmapData->destRight);
204 Stream_Read_UINT16(s, bitmapData->destBottom);
205 Stream_Read_UINT16(s, bitmapData->width);
206 Stream_Read_UINT16(s, bitmapData->height);
207 Stream_Read_UINT16(s, bitmapData->bitsPerPixel);
208 Stream_Read_UINT16(s, bitmapData->flags);
209 Stream_Read_UINT16(s, bitmapData->bitmapLength);
210
211 if ((bitmapData->width == 0) || (bitmapData->height == 0))
212 {
213 WLog_ERR(TAG, "Invalid BITMAP_DATA: width=%" PRIu16 ", height=%" PRIu16, bitmapData->width,
214 bitmapData->height);
215 return FALSE;
216 }
217
218 if (bitmapData->flags & BITMAP_COMPRESSION)
219 {
220 if (!(bitmapData->flags & NO_BITMAP_COMPRESSION_HDR))
221 {
222 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
223 return FALSE;
224
225 Stream_Read_UINT16(s,
226 bitmapData->cbCompFirstRowSize); /* cbCompFirstRowSize (2 bytes) */
227 Stream_Read_UINT16(s,
228 bitmapData->cbCompMainBodySize); /* cbCompMainBodySize (2 bytes) */
229 Stream_Read_UINT16(s, bitmapData->cbScanWidth); /* cbScanWidth (2 bytes) */
230 Stream_Read_UINT16(s,
231 bitmapData->cbUncompressedSize); /* cbUncompressedSize (2 bytes) */
232 bitmapData->bitmapLength = bitmapData->cbCompMainBodySize;
233 }
234
235 bitmapData->compressed = TRUE;
236 }
237 else
238 bitmapData->compressed = FALSE;
239
240 if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapData->bitmapLength))
241 return FALSE;
242
243 if (bitmapData->bitmapLength > 0)
244 {
245 bitmapData->bitmapDataStream = malloc(bitmapData->bitmapLength);
246
247 if (!bitmapData->bitmapDataStream)
248 return FALSE;
249
250 memcpy(bitmapData->bitmapDataStream, Stream_ConstPointer(s), bitmapData->bitmapLength);
251 Stream_Seek(s, bitmapData->bitmapLength);
252 }
253
254 return TRUE;
255}
256
257static BOOL update_write_bitmap_data_header(const BITMAP_DATA* bitmapData, wStream* s)
258{
259 WINPR_ASSERT(bitmapData);
260 if (!Stream_EnsureRemainingCapacity(s, 18))
261 return FALSE;
262 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destLeft));
263 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destTop));
264 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destRight));
265 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destBottom));
266 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->width));
267 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->height));
268 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->bitsPerPixel));
269 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->flags));
270 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->bitmapLength));
271 return TRUE;
272}
273
274static BOOL update_write_bitmap_data_no_comp_header(const BITMAP_DATA* bitmapData, wStream* s)
275{
276 WINPR_ASSERT(bitmapData);
277 if (!Stream_EnsureRemainingCapacity(s, 8))
278 return FALSE;
279
280 Stream_Write_UINT16(
281 s, WINPR_ASSERTING_INT_CAST(
282 uint16_t, bitmapData->cbCompFirstRowSize)); /* cbCompFirstRowSize (2 bytes) */
283 Stream_Write_UINT16(
284 s, WINPR_ASSERTING_INT_CAST(
285 uint16_t, bitmapData->cbCompMainBodySize)); /* cbCompMainBodySize (2 bytes) */
286 Stream_Write_UINT16(
287 s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->cbScanWidth)); /* cbScanWidth (2 bytes) */
288 Stream_Write_UINT16(
289 s, WINPR_ASSERTING_INT_CAST(
290 uint16_t, bitmapData->cbUncompressedSize)); /* cbUncompressedSize (2 bytes) */
291 return TRUE;
292}
293
294static BOOL update_write_bitmap_data(rdpUpdate* update_pub, wStream* s, BITMAP_DATA* bitmapData)
295{
296 rdp_update_internal* update = update_cast(update_pub);
297
298 WINPR_ASSERT(bitmapData);
299
300 if (!Stream_EnsureRemainingCapacity(s, 64 + bitmapData->bitmapLength))
301 return FALSE;
302
303 if (update->common.autoCalculateBitmapData)
304 {
305 bitmapData->flags = 0;
306 bitmapData->cbCompFirstRowSize = 0;
307
308 if (bitmapData->compressed)
309 bitmapData->flags |= BITMAP_COMPRESSION;
310
311 if (update->common.context->settings->NoBitmapCompressionHeader)
312 {
313 bitmapData->flags |= NO_BITMAP_COMPRESSION_HDR;
314 bitmapData->cbCompMainBodySize = bitmapData->bitmapLength;
315 }
316 }
317
318 if (!update_write_bitmap_data_header(bitmapData, s))
319 return FALSE;
320
321 if (bitmapData->flags & BITMAP_COMPRESSION)
322 {
323 if ((bitmapData->flags & NO_BITMAP_COMPRESSION_HDR) == 0)
324 {
325 if (!update_write_bitmap_data_no_comp_header(bitmapData, s))
326 return FALSE;
327 }
328 }
329
330 if (!Stream_EnsureRemainingCapacity(s, bitmapData->bitmapLength))
331 return FALSE;
332 Stream_Write(s, bitmapData->bitmapDataStream, bitmapData->bitmapLength);
333
334 return TRUE;
335}
336
337BITMAP_UPDATE* update_read_bitmap_update(rdpUpdate* update, wStream* s)
338{
339 BITMAP_UPDATE* bitmapUpdate = calloc(1, sizeof(BITMAP_UPDATE));
340 rdp_update_internal* up = update_cast(update);
341
342 if (!bitmapUpdate)
343 goto fail;
344
345 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
346 goto fail;
347
348 Stream_Read_UINT16(s, bitmapUpdate->number); /* numberRectangles (2 bytes) */
349 WLog_Print(up->log, WLOG_TRACE, "BitmapUpdate: %" PRIu32 "", bitmapUpdate->number);
350
351 bitmapUpdate->rectangles = (BITMAP_DATA*)calloc(bitmapUpdate->number, sizeof(BITMAP_DATA));
352
353 if (!bitmapUpdate->rectangles)
354 goto fail;
355
356 /* rectangles */
357 for (UINT32 i = 0; i < bitmapUpdate->number; i++)
358 {
359 if (!update_read_bitmap_data(update, s, &bitmapUpdate->rectangles[i]))
360 goto fail;
361 }
362
363 return bitmapUpdate;
364fail:
365 WINPR_PRAGMA_DIAG_PUSH
366 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
367 free_bitmap_update(update->context, bitmapUpdate);
368 WINPR_PRAGMA_DIAG_POP
369 return nullptr;
370}
371
372static BOOL update_write_bitmap_update(rdpUpdate* update, wStream* s,
373 const BITMAP_UPDATE* bitmapUpdate)
374{
375 WINPR_ASSERT(update);
376 WINPR_ASSERT(bitmapUpdate);
377
378 if (!Stream_EnsureRemainingCapacity(s, 32))
379 return FALSE;
380
381 Stream_Write_UINT16(s, UPDATE_TYPE_BITMAP); /* updateType */
382 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
383 uint16_t, bitmapUpdate->number)); /* numberRectangles (2 bytes) */
384
385 /* rectangles */
386 for (UINT32 i = 0; i < bitmapUpdate->number; i++)
387 {
388 if (!update_write_bitmap_data(update, s, &bitmapUpdate->rectangles[i]))
389 return FALSE;
390 }
391
392 return TRUE;
393}
394
395PALETTE_UPDATE* update_read_palette(rdpUpdate* update, wStream* s)
396{
397 PALETTE_UPDATE* palette_update = calloc(1, sizeof(PALETTE_UPDATE));
398
399 if (!palette_update)
400 goto fail;
401
402 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
403 goto fail;
404
405 Stream_Seek_UINT16(s); /* pad2Octets (2 bytes) */
406 Stream_Read_UINT32(s, palette_update->number); /* numberColors (4 bytes), must be set to 256 */
407
408 if (palette_update->number > 256)
409 palette_update->number = 256;
410
411 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, palette_update->number, 3ull))
412 goto fail;
413
414 /* paletteEntries */
415 for (UINT32 i = 0; i < palette_update->number; i++)
416 {
417 PALETTE_ENTRY* entry = &palette_update->entries[i];
418 Stream_Read_UINT8(s, entry->red);
419 Stream_Read_UINT8(s, entry->green);
420 Stream_Read_UINT8(s, entry->blue);
421 }
422
423 return palette_update;
424fail:
425 WINPR_PRAGMA_DIAG_PUSH
426 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
427 free_palette_update(update->context, palette_update);
428 WINPR_PRAGMA_DIAG_POP
429 return nullptr;
430}
431
432static BOOL update_read_synchronize(rdpUpdate* update, wStream* s)
433{
434 WINPR_UNUSED(update);
435 return Stream_SafeSeek(s, 2); /* pad2Octets (2 bytes) */
440}
441
442static BOOL update_read_play_sound(wStream* s, PLAY_SOUND_UPDATE* play_sound)
443{
444 WINPR_ASSERT(play_sound);
445
446 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
447 return FALSE;
448
449 Stream_Read_UINT32(s, play_sound->duration); /* duration (4 bytes) */
450 Stream_Read_UINT32(s, play_sound->frequency); /* frequency (4 bytes) */
451 return TRUE;
452}
453
454BOOL update_recv_play_sound(rdpUpdate* update, wStream* s)
455{
456 PLAY_SOUND_UPDATE play_sound = WINPR_C_ARRAY_INIT;
457
458 WINPR_ASSERT(update);
459
460 if (!update_read_play_sound(s, &play_sound))
461 return FALSE;
462
463 return IFCALLRESULT(TRUE, update->PlaySound, update->context, &play_sound);
464}
465
466POINTER_POSITION_UPDATE* update_read_pointer_position(rdpUpdate* update, wStream* s)
467{
468 POINTER_POSITION_UPDATE* pointer_position = calloc(1, sizeof(POINTER_POSITION_UPDATE));
469
470 WINPR_ASSERT(update);
471
472 if (!pointer_position)
473 goto fail;
474
475 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
476 goto fail;
477
478 Stream_Read_UINT16(s, pointer_position->xPos); /* xPos (2 bytes) */
479 Stream_Read_UINT16(s, pointer_position->yPos); /* yPos (2 bytes) */
480 return pointer_position;
481fail:
482 WINPR_PRAGMA_DIAG_PUSH
483 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
484 free_pointer_position_update(update->context, pointer_position);
485 WINPR_PRAGMA_DIAG_POP
486 return nullptr;
487}
488
489POINTER_SYSTEM_UPDATE* update_read_pointer_system(rdpUpdate* update, wStream* s)
490{
491 POINTER_SYSTEM_UPDATE* pointer_system = calloc(1, sizeof(POINTER_SYSTEM_UPDATE));
492
493 WINPR_ASSERT(update);
494
495 if (!pointer_system)
496 goto fail;
497
498 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
499 goto fail;
500
501 Stream_Read_UINT32(s, pointer_system->type); /* systemPointerType (4 bytes) */
502 return pointer_system;
503fail:
504 WINPR_PRAGMA_DIAG_PUSH
505 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
506 free_pointer_system_update(update->context, pointer_system);
507 WINPR_PRAGMA_DIAG_POP
508 return nullptr;
509}
510
511static BOOL s_update_read_pointer_color(wStream* s, POINTER_COLOR_UPDATE* pointer_color,
512 BYTE xorBpp, UINT32 flags)
513{
514 BYTE* newMask = nullptr;
515 UINT32 scanlineSize = 0;
516 UINT32 max = 32;
517
518 WINPR_ASSERT(pointer_color);
519
520 if (flags & LARGE_POINTER_FLAG_96x96)
521 max = 96;
522
523 if (!pointer_color)
524 goto fail;
525
526 if (!Stream_CheckAndLogRequiredLength(TAG, s, 14))
527 goto fail;
528
529 Stream_Read_UINT16(s, pointer_color->cacheIndex); /* cacheIndex (2 bytes) */
530 Stream_Read_UINT16(s, pointer_color->hotSpotX); /* hotSpot.xPos (2 bytes) */
531 Stream_Read_UINT16(s, pointer_color->hotSpotY); /* hotSpot.yPos (2 bytes) */
541 Stream_Read_UINT16(s, pointer_color->width); /* width (2 bytes) */
542 Stream_Read_UINT16(s, pointer_color->height); /* height (2 bytes) */
543
544 if ((pointer_color->width > max) || (pointer_color->height > max))
545 goto fail;
546
547 Stream_Read_UINT16(s, pointer_color->lengthAndMask); /* lengthAndMask (2 bytes) */
548 Stream_Read_UINT16(s, pointer_color->lengthXorMask); /* lengthXorMask (2 bytes) */
549
556 if (pointer_color->hotSpotX >= pointer_color->width)
557 pointer_color->hotSpotX = 0;
558
559 if (pointer_color->hotSpotY >= pointer_color->height)
560 pointer_color->hotSpotY = 0;
561
562 if (pointer_color->lengthXorMask > 0)
563 {
575 if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer_color->lengthXorMask))
576 goto fail;
577
578 scanlineSize = (7 + xorBpp * pointer_color->width) / 8;
579 scanlineSize = ((scanlineSize + 1) / 2) * 2;
580
581 if (scanlineSize * pointer_color->height != pointer_color->lengthXorMask)
582 {
583 WLog_ERR(TAG,
584 "invalid lengthXorMask: width=%" PRIu32 " height=%" PRIu32 ", %" PRIu32
585 " instead of %" PRIu32 "",
586 pointer_color->width, pointer_color->height, pointer_color->lengthXorMask,
587 scanlineSize * pointer_color->height);
588 goto fail;
589 }
590
591 newMask = realloc(pointer_color->xorMaskData, pointer_color->lengthXorMask);
592
593 if (!newMask)
594 goto fail;
595
596 pointer_color->xorMaskData = newMask;
597 Stream_Read(s, pointer_color->xorMaskData, pointer_color->lengthXorMask);
598 }
599
600 if (pointer_color->lengthAndMask > 0)
601 {
609 if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer_color->lengthAndMask))
610 goto fail;
611
612 scanlineSize = ((7 + pointer_color->width) / 8);
613 scanlineSize = ((1 + scanlineSize) / 2) * 2;
614
615 if (scanlineSize * pointer_color->height != pointer_color->lengthAndMask)
616 {
617 WLog_ERR(TAG, "invalid lengthAndMask: %" PRIu32 " instead of %" PRIu32 "",
618 pointer_color->lengthAndMask, scanlineSize * pointer_color->height);
619 goto fail;
620 }
621
622 newMask = realloc(pointer_color->andMaskData, pointer_color->lengthAndMask);
623
624 if (!newMask)
625 goto fail;
626
627 pointer_color->andMaskData = newMask;
628 Stream_Read(s, pointer_color->andMaskData, pointer_color->lengthAndMask);
629 }
630
631 if (Stream_GetRemainingLength(s) > 0)
632 Stream_Seek_UINT8(s); /* pad (1 byte) */
633
634 return TRUE;
635fail:
636 return FALSE;
637}
638
639POINTER_COLOR_UPDATE* update_read_pointer_color(rdpUpdate* update, wStream* s, BYTE xorBpp)
640{
641 POINTER_COLOR_UPDATE* pointer_color = calloc(1, sizeof(POINTER_COLOR_UPDATE));
642
643 WINPR_ASSERT(update);
644
645 if (!pointer_color)
646 goto fail;
647
648 if (!s_update_read_pointer_color(s, pointer_color, xorBpp,
649 update->context->settings->LargePointerFlag))
650 goto fail;
651
652 return pointer_color;
653fail:
654 WINPR_PRAGMA_DIAG_PUSH
655 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
656 free_pointer_color_update(update->context, pointer_color);
657 WINPR_PRAGMA_DIAG_POP
658 return nullptr;
659}
660
661static BOOL s_update_read_pointer_large(wStream* s, POINTER_LARGE_UPDATE* pointer)
662{
663 BYTE* newMask = nullptr;
664 UINT32 scanlineSize = 0;
665
666 if (!pointer)
667 goto fail;
668
669 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
670 goto fail;
671
672 Stream_Read_UINT16(s, pointer->xorBpp);
673 Stream_Read_UINT16(s, pointer->cacheIndex); /* cacheIndex (2 bytes) */
674 Stream_Read_UINT16(s, pointer->hotSpotX); /* hotSpot.xPos (2 bytes) */
675 Stream_Read_UINT16(s, pointer->hotSpotY); /* hotSpot.yPos (2 bytes) */
676
677 Stream_Read_UINT16(s, pointer->width); /* width (2 bytes) */
678 Stream_Read_UINT16(s, pointer->height); /* height (2 bytes) */
679
680 if ((pointer->width > 384) || (pointer->height > 384))
681 goto fail;
682
683 Stream_Read_UINT32(s, pointer->lengthAndMask); /* lengthAndMask (4 bytes) */
684 Stream_Read_UINT32(s, pointer->lengthXorMask); /* lengthXorMask (4 bytes) */
685
686 if (pointer->hotSpotX >= pointer->width)
687 pointer->hotSpotX = 0;
688
689 if (pointer->hotSpotY >= pointer->height)
690 pointer->hotSpotY = 0;
691
692 if (pointer->lengthXorMask > 0)
693 {
705 if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer->lengthXorMask))
706 goto fail;
707
708 scanlineSize = (7 + pointer->xorBpp * pointer->width) / 8;
709 scanlineSize = ((scanlineSize + 1) / 2) * 2;
710
711 if (scanlineSize * pointer->height != pointer->lengthXorMask)
712 {
713 WLog_ERR(TAG,
714 "invalid lengthXorMask: width=%" PRIu32 " height=%" PRIu32 ", %" PRIu32
715 " instead of %" PRIu32 "",
716 pointer->width, pointer->height, pointer->lengthXorMask,
717 scanlineSize * pointer->height);
718 goto fail;
719 }
720
721 newMask = realloc(pointer->xorMaskData, pointer->lengthXorMask);
722
723 if (!newMask)
724 goto fail;
725
726 pointer->xorMaskData = newMask;
727 Stream_Read(s, pointer->xorMaskData, pointer->lengthXorMask);
728 }
729
730 if (pointer->lengthAndMask > 0)
731 {
739 if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer->lengthAndMask))
740 goto fail;
741
742 scanlineSize = ((7 + pointer->width) / 8);
743 scanlineSize = ((1 + scanlineSize) / 2) * 2;
744
745 if (scanlineSize * pointer->height != pointer->lengthAndMask)
746 {
747 WLog_ERR(TAG, "invalid lengthAndMask: %" PRIu32 " instead of %" PRIu32 "",
748 pointer->lengthAndMask, scanlineSize * pointer->height);
749 goto fail;
750 }
751
752 newMask = realloc(pointer->andMaskData, pointer->lengthAndMask);
753
754 if (!newMask)
755 goto fail;
756
757 pointer->andMaskData = newMask;
758 Stream_Read(s, pointer->andMaskData, pointer->lengthAndMask);
759 }
760
761 if (Stream_GetRemainingLength(s) > 0)
762 Stream_Seek_UINT8(s); /* pad (1 byte) */
763
764 return TRUE;
765fail:
766 return FALSE;
767}
768
769POINTER_LARGE_UPDATE* update_read_pointer_large(rdpUpdate* update, wStream* s)
770{
771 POINTER_LARGE_UPDATE* pointer = calloc(1, sizeof(POINTER_LARGE_UPDATE));
772
773 WINPR_ASSERT(update);
774
775 if (!pointer)
776 goto fail;
777
778 if (!s_update_read_pointer_large(s, pointer))
779 goto fail;
780
781 return pointer;
782fail:
783 WINPR_PRAGMA_DIAG_PUSH
784 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
785 free_pointer_large_update(update->context, pointer);
786 WINPR_PRAGMA_DIAG_POP
787 return nullptr;
788}
789
790POINTER_NEW_UPDATE* update_read_pointer_new(rdpUpdate* update, wStream* s)
791{
792 POINTER_NEW_UPDATE* pointer_new = calloc(1, sizeof(POINTER_NEW_UPDATE));
793
794 WINPR_ASSERT(update);
795
796 if (!pointer_new)
797 goto fail;
798
799 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
800 goto fail;
801
802 Stream_Read_UINT16(s, pointer_new->xorBpp); /* xorBpp (2 bytes) */
803
804 if ((pointer_new->xorBpp < 1) || (pointer_new->xorBpp > 32))
805 {
806 WLog_ERR(TAG, "invalid xorBpp %" PRIu32 "", pointer_new->xorBpp);
807 goto fail;
808 }
809
810 WINPR_ASSERT(pointer_new->xorBpp <= UINT8_MAX);
811 if (!s_update_read_pointer_color(
812 s, &pointer_new->colorPtrAttr, (UINT8)pointer_new->xorBpp,
813 update->context->settings->LargePointerFlag)) /* colorPtrAttr */
814 goto fail;
815
816 return pointer_new;
817fail:
818 WINPR_PRAGMA_DIAG_PUSH
819 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
820 free_pointer_new_update(update->context, pointer_new);
821 WINPR_PRAGMA_DIAG_POP
822 return nullptr;
823}
824
825POINTER_CACHED_UPDATE* update_read_pointer_cached(rdpUpdate* update, wStream* s)
826{
827 POINTER_CACHED_UPDATE* pointer = calloc(1, sizeof(POINTER_CACHED_UPDATE));
828
829 WINPR_ASSERT(update);
830
831 if (!pointer)
832 goto fail;
833
834 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
835 goto fail;
836
837 Stream_Read_UINT16(s, pointer->cacheIndex); /* cacheIndex (2 bytes) */
838 return pointer;
839fail:
840 WINPR_PRAGMA_DIAG_PUSH
841 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
842 free_pointer_cached_update(update->context, pointer);
843 WINPR_PRAGMA_DIAG_POP
844 return nullptr;
845}
846
847BOOL update_recv_pointer(rdpUpdate* update, wStream* s)
848{
849 BOOL rc = FALSE;
850 UINT16 messageType = 0;
851
852 WINPR_ASSERT(update);
853
854 rdpContext* context = update->context;
855 rdpPointerUpdate* pointer = update->pointer;
856
857 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2 + 2))
858 return FALSE;
859
860 Stream_Read_UINT16(s, messageType); /* messageType (2 bytes) */
861 Stream_Seek_UINT16(s); /* pad2Octets (2 bytes) */
862
863 switch (messageType)
864 {
865 case PTR_MSG_TYPE_POSITION:
866 {
867 POINTER_POSITION_UPDATE* pointer_position = update_read_pointer_position(update, s);
868
869 if (pointer_position)
870 {
871 rc = IFCALLRESULT(FALSE, pointer->PointerPosition, context, pointer_position);
872 free_pointer_position_update(context, pointer_position);
873 }
874 }
875 break;
876
877 case PTR_MSG_TYPE_SYSTEM:
878 {
879 POINTER_SYSTEM_UPDATE* pointer_system = update_read_pointer_system(update, s);
880
881 if (pointer_system)
882 {
883 rc = IFCALLRESULT(FALSE, pointer->PointerSystem, context, pointer_system);
884 free_pointer_system_update(context, pointer_system);
885 }
886 }
887 break;
888
889 case PTR_MSG_TYPE_COLOR:
890 {
891 POINTER_COLOR_UPDATE* pointer_color = update_read_pointer_color(update, s, 24);
892
893 if (pointer_color)
894 {
895 rc = IFCALLRESULT(FALSE, pointer->PointerColor, context, pointer_color);
896 free_pointer_color_update(context, pointer_color);
897 }
898 }
899 break;
900
901 case PTR_MSG_TYPE_POINTER_LARGE:
902 {
903 POINTER_LARGE_UPDATE* pointer_large = update_read_pointer_large(update, s);
904
905 if (pointer_large)
906 {
907 rc = IFCALLRESULT(FALSE, pointer->PointerLarge, context, pointer_large);
908 free_pointer_large_update(context, pointer_large);
909 }
910 }
911 break;
912
913 case PTR_MSG_TYPE_POINTER:
914 {
915 POINTER_NEW_UPDATE* pointer_new = update_read_pointer_new(update, s);
916
917 if (pointer_new)
918 {
919 rc = IFCALLRESULT(FALSE, pointer->PointerNew, context, pointer_new);
920 free_pointer_new_update(context, pointer_new);
921 }
922 }
923 break;
924
925 case PTR_MSG_TYPE_CACHED:
926 {
927 POINTER_CACHED_UPDATE* pointer_cached = update_read_pointer_cached(update, s);
928
929 if (pointer_cached)
930 {
931 rc = IFCALLRESULT(FALSE, pointer->PointerCached, context, pointer_cached);
932 free_pointer_cached_update(context, pointer_cached);
933 }
934 }
935 break;
936
937 default:
938 break;
939 }
940
941 return rc;
942}
943
944BOOL update_recv(rdpUpdate* update, wStream* s)
945{
946 BOOL rc = FALSE;
947 UINT16 updateType = 0;
948 rdp_update_internal* up = update_cast(update);
949 rdpContext* context = update->context;
950
951 WINPR_ASSERT(context);
952
953 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
954 return FALSE;
955
956 Stream_Read_UINT16(s, updateType); /* updateType (2 bytes) */
957 WLog_Print(up->log, WLOG_TRACE, "%s Update Data PDU", update_type_to_string(updateType));
958
959 if (!update_begin_paint(update))
960 goto fail;
961
962 switch (updateType)
963 {
964 case UPDATE_TYPE_ORDERS:
965 rc = update_recv_orders(update, s);
966 break;
967
968 case UPDATE_TYPE_BITMAP:
969 {
970 BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s);
971
972 if (!bitmap_update)
973 {
974 WLog_ERR(TAG, "UPDATE_TYPE_BITMAP - update_read_bitmap_update() failed");
975 goto fail;
976 }
977
978 up->stats.base[RDP_STATS_BITMAP_UPDATE]++;
979 rc = IFCALLRESULT(FALSE, update->BitmapUpdate, context, bitmap_update);
980 free_bitmap_update(context, bitmap_update);
981 }
982 break;
983
984 case UPDATE_TYPE_PALETTE:
985 {
986 PALETTE_UPDATE* palette_update = update_read_palette(update, s);
987
988 if (!palette_update)
989 {
990 WLog_ERR(TAG, "UPDATE_TYPE_PALETTE - update_read_palette() failed");
991 goto fail;
992 }
993
994 up->stats.base[RDP_STATS_PALETTE]++;
995 rc = IFCALLRESULT(FALSE, update->Palette, context, palette_update);
996 free_palette_update(context, palette_update);
997 }
998 break;
999
1000 case UPDATE_TYPE_SYNCHRONIZE:
1001 if (!update_read_synchronize(update, s))
1002 goto fail;
1003 up->stats.base[RDP_STATS_SYNC]++;
1004 rc = IFCALLRESULT(TRUE, update->Synchronize, context);
1005 break;
1006
1007 default:
1008 break;
1009 }
1010
1011fail:
1012
1013 if (!update_end_paint(update))
1014 rc = FALSE;
1015
1016 if (!rc)
1017 {
1018 WLog_ERR(TAG, "UPDATE_TYPE %s [%" PRIu16 "] failed", update_type_to_string(updateType),
1019 updateType);
1020 return FALSE;
1021 }
1022
1023 return TRUE;
1024}
1025
1026void update_reset_state(rdpUpdate* update)
1027{
1028 rdp_update_internal* up = update_cast(update);
1029 rdp_primary_update_internal* primary = primary_update_cast(update->primary);
1030
1031 WINPR_ASSERT(primary);
1032
1033 ZeroMemory(&primary->order_info, sizeof(ORDER_INFO));
1034 ZeroMemory(&primary->dstblt, sizeof(DSTBLT_ORDER));
1035 ZeroMemory(&primary->patblt, sizeof(PATBLT_ORDER));
1036 ZeroMemory(&primary->scrblt, sizeof(SCRBLT_ORDER));
1037 ZeroMemory(&primary->opaque_rect, sizeof(OPAQUE_RECT_ORDER));
1038 ZeroMemory(&primary->draw_nine_grid, sizeof(DRAW_NINE_GRID_ORDER));
1039 ZeroMemory(&primary->multi_dstblt, sizeof(MULTI_DSTBLT_ORDER));
1040 ZeroMemory(&primary->multi_patblt, sizeof(MULTI_PATBLT_ORDER));
1041 ZeroMemory(&primary->multi_scrblt, sizeof(MULTI_SCRBLT_ORDER));
1042 ZeroMemory(&primary->multi_opaque_rect, sizeof(MULTI_OPAQUE_RECT_ORDER));
1043 ZeroMemory(&primary->multi_draw_nine_grid, sizeof(MULTI_DRAW_NINE_GRID_ORDER));
1044 ZeroMemory(&primary->line_to, sizeof(LINE_TO_ORDER));
1045
1046 free(primary->polyline.points);
1047 ZeroMemory(&primary->polyline, sizeof(POLYLINE_ORDER));
1048
1049 ZeroMemory(&primary->memblt, sizeof(MEMBLT_ORDER));
1050 ZeroMemory(&primary->mem3blt, sizeof(MEM3BLT_ORDER));
1051 ZeroMemory(&primary->save_bitmap, sizeof(SAVE_BITMAP_ORDER));
1052 ZeroMemory(&primary->glyph_index, sizeof(GLYPH_INDEX_ORDER));
1053 ZeroMemory(&primary->fast_index, sizeof(FAST_INDEX_ORDER));
1054
1055 free(primary->fast_glyph.glyphData.aj);
1056 ZeroMemory(&primary->fast_glyph, sizeof(FAST_GLYPH_ORDER));
1057
1058 free(primary->polygon_sc.points);
1059 ZeroMemory(&primary->polygon_sc, sizeof(POLYGON_SC_ORDER));
1060
1061 free(primary->polygon_cb.points);
1062 ZeroMemory(&primary->polygon_cb, sizeof(POLYGON_CB_ORDER));
1063
1064 ZeroMemory(&primary->ellipse_sc, sizeof(ELLIPSE_SC_ORDER));
1065 ZeroMemory(&primary->ellipse_cb, sizeof(ELLIPSE_CB_ORDER));
1066 primary->order_info.orderType = ORDER_TYPE_PATBLT;
1067
1068 if (!up->initialState)
1069 {
1070 rdp_altsec_update_internal* altsec = altsec_update_cast(update->altsec);
1071 WINPR_ASSERT(altsec);
1072
1073 altsec->switch_surface.bitmapId = SCREEN_BITMAP_SURFACE;
1074 if (altsec->common.SwitchSurface)
1075 {
1076 if (!altsec->common.SwitchSurface(update->context, &(altsec->switch_surface)))
1077 WLog_Print(up->log, WLOG_WARN, "altsec->common.SwitchSurface failed");
1078 }
1079 }
1080}
1081
1082BOOL update_post_connect(rdpUpdate* update)
1083{
1084 rdp_update_internal* up = update_cast(update);
1085 rdp_altsec_update_internal* altsec = altsec_update_cast(update->altsec);
1086
1087 WINPR_ASSERT(update->context);
1088 WINPR_ASSERT(update->context->settings);
1089 up->asynchronous = update->context->settings->AsyncUpdate;
1090
1091 if (up->asynchronous)
1092 {
1093#if defined(FORCE_ASYNC_UPDATE_OFF)
1094 WLog_WARN(TAG, "AsyncUpdate requested, but forced deactivated");
1095 WLog_WARN(TAG, "see https://github.com/FreeRDP/FreeRDP/issues/10153 for details");
1096#else
1097 if (!(up->proxy = update_message_proxy_new(update)))
1098 return FALSE;
1099#endif
1100 }
1101
1102 altsec->switch_surface.bitmapId = SCREEN_BITMAP_SURFACE;
1103 const BOOL rc = IFCALLRESULT(TRUE, update->altsec->SwitchSurface, update->context,
1104 &(altsec->switch_surface));
1105 up->initialState = FALSE;
1106 return rc;
1107}
1108
1109void update_post_disconnect(rdpUpdate* update)
1110{
1111 rdp_update_internal* up = update_cast(update);
1112
1113 WINPR_ASSERT(update->context);
1114 WINPR_ASSERT(update->context->settings);
1115
1116 up->asynchronous = update->context->settings->AsyncUpdate;
1117
1118 if (up->asynchronous)
1119 {
1120#if !defined(FORCE_ASYNC_UPDATE_OFF)
1121 update_message_proxy_free(up->proxy);
1122#endif
1123 }
1124
1125 up->initialState = TRUE;
1126}
1127
1128static BOOL s_update_begin_paint(rdpContext* context)
1129{
1130 wStream* s = nullptr;
1131 WINPR_ASSERT(context);
1132 rdp_update_internal* update = update_cast(context->update);
1133
1134 if (update->us)
1135 {
1136 if (!update_end_paint(&update->common))
1137 return FALSE;
1138 }
1139
1140 WINPR_ASSERT(context->rdp);
1141 s = fastpath_update_pdu_init_new(context->rdp->fastpath);
1142
1143 if (!s)
1144 return FALSE;
1145
1146 Stream_SealLength(s);
1147 Stream_GetLength(s, update->offsetOrders);
1148 Stream_Seek(s, 2); /* numberOrders (2 bytes) */
1149 update->combineUpdates = TRUE;
1150 update->numberOrders = 0;
1151 update->us = s;
1152 return TRUE;
1153}
1154
1155static BOOL s_update_end_paint(rdpContext* context)
1156{
1157 BOOL rc = FALSE;
1158
1159 WINPR_ASSERT(context);
1160 rdp_update_internal* update = update_cast(context->update);
1161
1162 if (!update->us)
1163 return FALSE;
1164
1165 wStream* s = update->us;
1166 update->us = nullptr;
1167
1168 Stream_SealLength(s);
1169 if (!Stream_SetPosition(s, update->offsetOrders))
1170 goto fail;
1171 Stream_Write_UINT16(s, update->numberOrders); /* numberOrders (2 bytes) */
1172 if (!Stream_SetPosition(s, Stream_Length(s)))
1173 goto fail;
1174
1175 if (update->numberOrders > 0)
1176 {
1177 WLog_DBG(TAG, "sending %" PRIu16 " orders", update->numberOrders);
1178 if (!fastpath_send_update_pdu(context->rdp->fastpath, FASTPATH_UPDATETYPE_ORDERS, s, FALSE))
1179 goto fail;
1180 }
1181
1182 update->combineUpdates = FALSE;
1183 update->numberOrders = 0;
1184 update->offsetOrders = 0;
1185
1186 rc = TRUE;
1187fail:
1188 Stream_Free(s, TRUE);
1189 return rc;
1190}
1191
1192static BOOL update_flush(rdpContext* context)
1193{
1194 rdp_update_internal* update = nullptr;
1195
1196 WINPR_ASSERT(context);
1197 update = update_cast(context->update);
1198
1199 if (update->numberOrders > 0)
1200 {
1201 if (!update_end_paint(&update->common))
1202 return FALSE;
1203
1204 if (!update_begin_paint(&update->common))
1205 return FALSE;
1206 }
1207 return TRUE;
1208}
1209
1210static BOOL update_force_flush(rdpContext* context)
1211{
1212 return update_flush(context);
1213}
1214
1215static BOOL update_check_flush(rdpContext* context, size_t size)
1216{
1217 WINPR_ASSERT(context);
1218 rdp_update_internal* update = update_cast(context->update);
1219
1220 wStream* s = update->us;
1221
1222 if (!s)
1223 {
1224 if (!update_begin_paint(&update->common))
1225 return FALSE;
1226 s = update->us;
1227 }
1228
1229 if (Stream_GetPosition(s) + size + 64 >= FASTPATH_MAX_PACKET_SIZE)
1230 {
1231 // Too big for the current packet. Flush first
1232 if (!update_flush(context))
1233 return FALSE;
1234 }
1235
1236 return TRUE;
1237}
1238
1239static BOOL update_set_bounds(rdpContext* context, const rdpBounds* bounds)
1240{
1241 rdp_update_internal* update = nullptr;
1242
1243 WINPR_ASSERT(context);
1244
1245 update = update_cast(context->update);
1246
1247 CopyMemory(&update->previousBounds, &update->currentBounds, sizeof(rdpBounds));
1248
1249 if (!bounds)
1250 ZeroMemory(&update->currentBounds, sizeof(rdpBounds));
1251 else
1252 CopyMemory(&update->currentBounds, bounds, sizeof(rdpBounds));
1253
1254 return TRUE;
1255}
1256
1257static BOOL update_bounds_is_null(rdpBounds* bounds)
1258{
1259 WINPR_ASSERT(bounds);
1260 return ((bounds->left == 0) && (bounds->top == 0) && (bounds->right == 0) &&
1261 (bounds->bottom == 0));
1262}
1263
1264static BOOL update_bounds_equals(rdpBounds* bounds1, rdpBounds* bounds2)
1265{
1266 WINPR_ASSERT(bounds1);
1267 WINPR_ASSERT(bounds2);
1268
1269 return ((bounds1->left == bounds2->left) && (bounds1->top == bounds2->top) &&
1270 (bounds1->right == bounds2->right) && (bounds1->bottom == bounds2->bottom));
1271}
1272
1273static size_t update_prepare_bounds(rdpContext* context, ORDER_INFO* orderInfo)
1274{
1275 size_t length = 0;
1276 rdp_update_internal* update = nullptr;
1277
1278 WINPR_ASSERT(context);
1279 WINPR_ASSERT(orderInfo);
1280
1281 update = update_cast(context->update);
1282
1283 orderInfo->boundsFlags = 0;
1284
1285 if (update_bounds_is_null(&update->currentBounds))
1286 return 0;
1287
1288 orderInfo->controlFlags |= ORDER_BOUNDS;
1289
1290 if (update_bounds_equals(&update->previousBounds, &update->currentBounds))
1291 {
1292 orderInfo->controlFlags |= ORDER_ZERO_BOUNDS_DELTAS;
1293 return 0;
1294 }
1295 else
1296 {
1297 length += 1;
1298
1299 if (update->previousBounds.left != update->currentBounds.left)
1300 {
1301 orderInfo->bounds.left = update->currentBounds.left;
1302 orderInfo->boundsFlags |= BOUND_LEFT;
1303 length += 2;
1304 }
1305
1306 if (update->previousBounds.top != update->currentBounds.top)
1307 {
1308 orderInfo->bounds.top = update->currentBounds.top;
1309 orderInfo->boundsFlags |= BOUND_TOP;
1310 length += 2;
1311 }
1312
1313 if (update->previousBounds.right != update->currentBounds.right)
1314 {
1315 orderInfo->bounds.right = update->currentBounds.right;
1316 orderInfo->boundsFlags |= BOUND_RIGHT;
1317 length += 2;
1318 }
1319
1320 if (update->previousBounds.bottom != update->currentBounds.bottom)
1321 {
1322 orderInfo->bounds.bottom = update->currentBounds.bottom;
1323 orderInfo->boundsFlags |= BOUND_BOTTOM;
1324 length += 2;
1325 }
1326 }
1327
1328 return length;
1329}
1330
1331static size_t update_prepare_order_info(rdpContext* context, ORDER_INFO* orderInfo,
1332 UINT32 orderType)
1333{
1334 WINPR_ASSERT(context);
1335 WINPR_ASSERT(orderInfo);
1336
1337 orderInfo->fieldFlags = 0;
1338 orderInfo->orderType = orderType;
1339 orderInfo->controlFlags = ORDER_STANDARD;
1340 orderInfo->controlFlags |= ORDER_TYPE_CHANGE;
1341 size_t length = 2;
1342 length += get_primary_drawing_order_field_bytes(orderInfo->orderType, nullptr);
1343 length += update_prepare_bounds(context, orderInfo);
1344 return length;
1345}
1346
1347WINPR_ATTR_NODISCARD
1348static int update_write_order_info(rdpContext* context, wStream* s, const ORDER_INFO* orderInfo,
1349 size_t offset)
1350{
1351 WINPR_UNUSED(context);
1352 WINPR_ASSERT(orderInfo);
1353 WINPR_ASSERT(orderInfo->controlFlags <= UINT8_MAX);
1354
1355 const size_t position = Stream_GetPosition(s);
1356 const UINT8 controlFlags = (UINT8)orderInfo->controlFlags;
1357
1358 if (!Stream_SetPosition(s, offset))
1359 return -1;
1360
1361 Stream_Write_UINT8(s, controlFlags); /* controlFlags (1 byte) */
1362
1363 if (orderInfo->controlFlags & ORDER_TYPE_CHANGE)
1364 Stream_Write_UINT8(
1365 s, WINPR_ASSERTING_INT_CAST(uint8_t, orderInfo->orderType)); /* orderType (1 byte) */
1366
1367 if (!update_write_field_flags(
1368 s, orderInfo->fieldFlags, controlFlags,
1369 get_primary_drawing_order_field_bytes(orderInfo->orderType, nullptr)))
1370 return -1;
1371 if (!update_write_bounds(s, orderInfo))
1372 return -1;
1373 if (!Stream_SetPosition(s, position))
1374 return -1;
1375 return 0;
1376}
1377
1378static void update_write_refresh_rect(wStream* s, BYTE count, const RECTANGLE_16* areas)
1379{
1380 WINPR_ASSERT(s);
1381 WINPR_ASSERT(areas || (count == 0));
1382
1383 Stream_Write_UINT8(s, count); /* numberOfAreas (1 byte) */
1384 Stream_Seek(s, 3); /* pad3Octets (3 bytes) */
1385
1386 for (BYTE i = 0; i < count; i++)
1387 {
1388 Stream_Write_UINT16(s, areas[i].left); /* left (2 bytes) */
1389 Stream_Write_UINT16(s, areas[i].top); /* top (2 bytes) */
1390 Stream_Write_UINT16(s, areas[i].right); /* right (2 bytes) */
1391 Stream_Write_UINT16(s, areas[i].bottom); /* bottom (2 bytes) */
1392 }
1393}
1394
1395static BOOL update_send_refresh_rect(rdpContext* context, BYTE count, const RECTANGLE_16* areas)
1396{
1397 WINPR_ASSERT(context);
1398 rdpRdp* rdp = context->rdp;
1399
1400 WINPR_ASSERT(rdp->settings);
1401 if (rdp->settings->RefreshRect)
1402 {
1403 UINT16 sec_flags = 0;
1404 wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
1405
1406 if (!s)
1407 return FALSE;
1408
1409 update_write_refresh_rect(s, count, areas);
1410 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_REFRESH_RECT, rdp->mcs->userId, sec_flags);
1411 }
1412
1413 return TRUE;
1414}
1415
1416static void update_write_suppress_output(wStream* s, BYTE allow, const RECTANGLE_16* area)
1417{
1418 WINPR_ASSERT(s);
1419
1420 Stream_Write_UINT8(s, allow); /* allowDisplayUpdates (1 byte) */
1421 /* Use zeros for padding (like mstsc) for compatibility with legacy servers */
1422 Stream_Zero(s, 3); /* pad3Octets (3 bytes) */
1423
1424 if (allow > 0)
1425 {
1426 WINPR_ASSERT(area);
1427 Stream_Write_UINT16(s, area->left); /* left (2 bytes) */
1428 Stream_Write_UINT16(s, area->top); /* top (2 bytes) */
1429 Stream_Write_UINT16(s, area->right); /* right (2 bytes) */
1430 Stream_Write_UINT16(s, area->bottom); /* bottom (2 bytes) */
1431 }
1432}
1433
1434static BOOL update_send_suppress_output(rdpContext* context, BYTE allow, const RECTANGLE_16* area)
1435{
1436 WINPR_ASSERT(context);
1437 rdpRdp* rdp = context->rdp;
1438
1439 WINPR_ASSERT(rdp);
1440 WINPR_ASSERT(rdp->settings);
1441 if (rdp->settings->SuppressOutput)
1442 {
1443 UINT16 sec_flags = 0;
1444 wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
1445
1446 if (!s)
1447 return FALSE;
1448
1449 update_write_suppress_output(s, allow, area);
1450 WINPR_ASSERT(rdp->mcs);
1451 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SUPPRESS_OUTPUT, rdp->mcs->userId,
1452 sec_flags);
1453 }
1454
1455 return TRUE;
1456}
1457
1458static BOOL update_send_surface_command(rdpContext* context, wStream* s)
1459{
1460 wStream* update = nullptr;
1461 WINPR_ASSERT(context);
1462 rdpRdp* rdp = context->rdp;
1463 BOOL ret = 0;
1464
1465 WINPR_ASSERT(rdp);
1466 update = fastpath_update_pdu_init(rdp->fastpath);
1467
1468 if (!update)
1469 return FALSE;
1470
1471 if (!Stream_EnsureRemainingCapacity(update, Stream_GetPosition(s)))
1472 {
1473 ret = FALSE;
1474 goto out;
1475 }
1476
1477 Stream_Write(update, Stream_Buffer(s), Stream_GetPosition(s));
1478 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, update, FALSE);
1479out:
1480 Stream_Release(update);
1481 return ret;
1482}
1483
1484static BOOL update_send_surface_bits(rdpContext* context,
1485 const SURFACE_BITS_COMMAND* surfaceBitsCommand)
1486{
1487 wStream* s = nullptr;
1488 WINPR_ASSERT(context);
1489 rdpRdp* rdp = context->rdp;
1490 BOOL ret = FALSE;
1491
1492 WINPR_ASSERT(surfaceBitsCommand);
1493 WINPR_ASSERT(rdp);
1494
1495 if (!update_force_flush(context))
1496 return FALSE;
1497 s = fastpath_update_pdu_init(rdp->fastpath);
1498
1499 if (!s)
1500 return FALSE;
1501
1502 if (!update_write_surfcmd_surface_bits(s, surfaceBitsCommand))
1503 goto out_fail;
1504
1505 if (!fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s,
1506 surfaceBitsCommand->skipCompression))
1507 goto out_fail;
1508
1509 ret = update_force_flush(context);
1510out_fail:
1511 Stream_Release(s);
1512 return ret;
1513}
1514
1515static BOOL update_send_surface_frame_marker(rdpContext* context,
1516 const SURFACE_FRAME_MARKER* surfaceFrameMarker)
1517{
1518 wStream* s = nullptr;
1519 WINPR_ASSERT(context);
1520 rdpRdp* rdp = context->rdp;
1521 BOOL ret = FALSE;
1522 if (!update_force_flush(context))
1523 return FALSE;
1524
1525 WINPR_ASSERT(rdp);
1526 s = fastpath_update_pdu_init(rdp->fastpath);
1527
1528 if (!s)
1529 return FALSE;
1530
1531 WINPR_ASSERT(surfaceFrameMarker->frameAction <= UINT16_MAX);
1532 if (!update_write_surfcmd_frame_marker(s, (UINT16)surfaceFrameMarker->frameAction,
1533 surfaceFrameMarker->frameId) ||
1534 !fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s, FALSE))
1535 goto out_fail;
1536
1537 ret = update_force_flush(context);
1538out_fail:
1539 Stream_Release(s);
1540 return ret;
1541}
1542
1543static BOOL update_send_surface_frame_bits(rdpContext* context, const SURFACE_BITS_COMMAND* cmd,
1544 BOOL first, BOOL last, UINT32 frameId)
1545{
1546 wStream* s = nullptr;
1547
1548 WINPR_ASSERT(context);
1549 rdpRdp* rdp = context->rdp;
1550 BOOL ret = FALSE;
1551
1552 if (!update_force_flush(context))
1553 return FALSE;
1554
1555 WINPR_ASSERT(rdp);
1556 s = fastpath_update_pdu_init(rdp->fastpath);
1557
1558 if (!s)
1559 return FALSE;
1560
1561 if (first)
1562 {
1563 if (!update_write_surfcmd_frame_marker(s, SURFACECMD_FRAMEACTION_BEGIN, frameId))
1564 goto out_fail;
1565 }
1566
1567 if (!update_write_surfcmd_surface_bits(s, cmd))
1568 goto out_fail;
1569
1570 if (last)
1571 {
1572 if (!update_write_surfcmd_frame_marker(s, SURFACECMD_FRAMEACTION_END, frameId))
1573 goto out_fail;
1574 }
1575
1576 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s,
1577 cmd->skipCompression);
1578 if (!ret)
1579 goto out_fail;
1580
1581 ret = update_force_flush(context);
1582out_fail:
1583 Stream_Release(s);
1584 return ret;
1585}
1586
1587static BOOL update_send_frame_acknowledge(rdpContext* context, UINT32 frameId)
1588{
1589 WINPR_ASSERT(context);
1590 rdpRdp* rdp = context->rdp;
1591
1592 WINPR_ASSERT(rdp);
1593 WINPR_ASSERT(rdp->settings);
1594 WINPR_ASSERT(rdp->settings->ReceivedCapabilities);
1595 WINPR_ASSERT(rdp->settings->ReceivedCapabilitiesSize > CAPSET_TYPE_FRAME_ACKNOWLEDGE);
1596 if (rdp->settings->ReceivedCapabilities[CAPSET_TYPE_FRAME_ACKNOWLEDGE])
1597 {
1598 UINT16 sec_flags = 0;
1599 wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
1600
1601 if (!s)
1602 return FALSE;
1603
1604 Stream_Write_UINT32(s, frameId);
1605 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_FRAME_ACKNOWLEDGE, rdp->mcs->userId,
1606 sec_flags);
1607 }
1608
1609 return TRUE;
1610}
1611
1612static BOOL update_send_synchronize(rdpContext* context)
1613{
1614 wStream* s = nullptr;
1615 WINPR_ASSERT(context);
1616 rdpRdp* rdp = context->rdp;
1617 BOOL ret = 0;
1618
1619 WINPR_ASSERT(rdp);
1620 s = fastpath_update_pdu_init(rdp->fastpath);
1621
1622 if (!s)
1623 return FALSE;
1624
1625 Stream_Zero(s, 2); /* pad2Octets (2 bytes) */
1626 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SYNCHRONIZE, s, FALSE);
1627 Stream_Release(s);
1628 return ret;
1629}
1630
1631static BOOL update_send_desktop_resize(rdpContext* context)
1632{
1633 WINPR_ASSERT(context);
1634 return rdp_server_reactivate(context->rdp);
1635}
1636
1637static BOOL update_send_bitmap_update(rdpContext* context, const BITMAP_UPDATE* bitmapUpdate)
1638{
1639 wStream* s = nullptr;
1640 WINPR_ASSERT(context);
1641 rdpRdp* rdp = context->rdp;
1642 rdpUpdate* update = context->update;
1643 BOOL ret = TRUE;
1644
1645 if (!update_force_flush(context))
1646 return FALSE;
1647
1648 WINPR_ASSERT(rdp);
1649 s = fastpath_update_pdu_init(rdp->fastpath);
1650
1651 if (!s)
1652 return FALSE;
1653
1654 if (!update_write_bitmap_update(update, s, bitmapUpdate) ||
1655 !fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_BITMAP, s,
1656 bitmapUpdate->skipCompression))
1657 {
1658 ret = FALSE;
1659 goto out_fail;
1660 }
1661
1662 ret = update_force_flush(context);
1663
1664out_fail:
1665 Stream_Release(s);
1666 return ret;
1667}
1668
1669static BOOL update_send_play_sound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
1670{
1671 UINT16 sec_flags = 0;
1672 wStream* s = nullptr;
1673 WINPR_ASSERT(context);
1674 rdpRdp* rdp = context->rdp;
1675
1676 WINPR_ASSERT(rdp);
1677 WINPR_ASSERT(rdp->settings);
1678 WINPR_ASSERT(play_sound);
1679 WINPR_ASSERT(rdp->settings->ReceivedCapabilities);
1680 WINPR_ASSERT(rdp->settings->ReceivedCapabilitiesSize > CAPSET_TYPE_SOUND);
1681 if (!rdp->settings->ReceivedCapabilities[CAPSET_TYPE_SOUND])
1682 {
1683 return TRUE;
1684 }
1685
1686 s = rdp_data_pdu_init(rdp, &sec_flags);
1687
1688 if (!s)
1689 return FALSE;
1690
1691 Stream_Write_UINT32(s, play_sound->duration);
1692 Stream_Write_UINT32(s, play_sound->frequency);
1693 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_PLAY_SOUND, rdp->mcs->userId, sec_flags);
1694}
1695
1700static BOOL update_send_dstblt(rdpContext* context, const DSTBLT_ORDER* dstblt)
1701{
1702 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1703
1704 WINPR_ASSERT(context);
1705 WINPR_ASSERT(dstblt);
1706
1707 rdp_update_internal* update = update_cast(context->update);
1708
1709 const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_DSTBLT);
1710 const size_t inf = update_approximate_dstblt_order(&orderInfo, dstblt);
1711 if (!update_check_flush(context, headerLength + inf))
1712 return FALSE;
1713
1714 wStream* s = update->us;
1715
1716 if (!s)
1717 return FALSE;
1718
1719 const size_t offset = Stream_GetPosition(s);
1720
1721 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1722 return FALSE;
1723
1724 Stream_Seek(s, headerLength);
1725
1726 if (!update_write_dstblt_order(s, &orderInfo, dstblt))
1727 return FALSE;
1728
1729 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1730 return FALSE;
1731 update->numberOrders++;
1732 return TRUE;
1733}
1734
1735static BOOL update_send_patblt(rdpContext* context, PATBLT_ORDER* patblt)
1736{
1737 size_t offset = 0;
1738 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1739
1740 WINPR_ASSERT(context);
1741 WINPR_ASSERT(patblt);
1742 rdp_update_internal* update = update_cast(context->update);
1743
1744 const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_PATBLT);
1745 if (!update_check_flush(context,
1746 headerLength + update_approximate_patblt_order(&orderInfo, patblt)))
1747 return FALSE;
1748
1749 wStream* s = update->us;
1750
1751 if (!s)
1752 return FALSE;
1753
1754 offset = Stream_GetPosition(s);
1755
1756 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1757 return FALSE;
1758
1759 Stream_Seek(s, headerLength);
1760 if (!update_write_patblt_order(s, &orderInfo, patblt))
1761 return FALSE;
1762 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1763 return FALSE;
1764 update->numberOrders++;
1765 return TRUE;
1766}
1767
1768static BOOL update_send_scrblt(rdpContext* context, const SCRBLT_ORDER* scrblt)
1769{
1770 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1771
1772 WINPR_ASSERT(context);
1773 WINPR_ASSERT(scrblt);
1774 rdp_update_internal* update = update_cast(context->update);
1775
1776 const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_SCRBLT);
1777 const size_t inf = update_approximate_scrblt_order(&orderInfo, scrblt);
1778 if (!update_check_flush(context, headerLength + inf))
1779 return FALSE;
1780
1781 wStream* s = update->us;
1782
1783 if (!s)
1784 return TRUE;
1785
1786 const size_t offset = Stream_GetPosition(s);
1787
1788 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1789 return FALSE;
1790
1791 Stream_Seek(s, headerLength);
1792 if (!update_write_scrblt_order(s, &orderInfo, scrblt))
1793 return FALSE;
1794 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1795 return FALSE;
1796 update->numberOrders++;
1797 return TRUE;
1798}
1799
1800static BOOL update_send_opaque_rect(rdpContext* context, const OPAQUE_RECT_ORDER* opaque_rect)
1801{
1802 size_t offset = 0;
1803 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1804
1805 WINPR_ASSERT(context);
1806 WINPR_ASSERT(opaque_rect);
1807 rdp_update_internal* update = update_cast(context->update);
1808
1809 const size_t headerLength =
1810 update_prepare_order_info(context, &orderInfo, ORDER_TYPE_OPAQUE_RECT);
1811 if (!update_check_flush(
1812 context, headerLength + update_approximate_opaque_rect_order(&orderInfo, opaque_rect)))
1813 return FALSE;
1814
1815 wStream* s = update->us;
1816
1817 if (!s)
1818 return FALSE;
1819
1820 offset = Stream_GetPosition(s);
1821
1822 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1823 return FALSE;
1824
1825 Stream_Seek(s, headerLength);
1826 if (!update_write_opaque_rect_order(s, &orderInfo, opaque_rect))
1827 return FALSE;
1828 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1829 return FALSE;
1830 update->numberOrders++;
1831 return TRUE;
1832}
1833
1834static BOOL update_send_line_to(rdpContext* context, const LINE_TO_ORDER* line_to)
1835{
1836 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1837
1838 WINPR_ASSERT(context);
1839 WINPR_ASSERT(line_to);
1840 rdp_update_internal* update = update_cast(context->update);
1841 const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_LINE_TO);
1842 const size_t inf = update_approximate_line_to_order(&orderInfo, line_to);
1843 if (!update_check_flush(context, headerLength + inf))
1844 return FALSE;
1845
1846 wStream* s = update->us;
1847
1848 if (!s)
1849 return FALSE;
1850
1851 const size_t offset = Stream_GetPosition(s);
1852
1853 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1854 return FALSE;
1855
1856 Stream_Seek(s, headerLength);
1857 if (!update_write_line_to_order(s, &orderInfo, line_to))
1858 return FALSE;
1859 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1860 return FALSE;
1861 update->numberOrders++;
1862 return TRUE;
1863}
1864
1865static BOOL update_send_memblt(rdpContext* context, MEMBLT_ORDER* memblt)
1866{
1867 size_t offset = 0;
1868 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1869
1870 WINPR_ASSERT(context);
1871 WINPR_ASSERT(memblt);
1872 rdp_update_internal* update = update_cast(context->update);
1873 const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_MEMBLT);
1874 if (!update_check_flush(context,
1875 headerLength + update_approximate_memblt_order(&orderInfo, memblt)))
1876 return FALSE;
1877
1878 wStream* s = update->us;
1879
1880 if (!s)
1881 return FALSE;
1882
1883 offset = Stream_GetPosition(s);
1884
1885 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1886 return FALSE;
1887
1888 Stream_Seek(s, headerLength);
1889 if (!update_write_memblt_order(s, &orderInfo, memblt))
1890 return FALSE;
1891 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1892 return FALSE;
1893 update->numberOrders++;
1894 return TRUE;
1895}
1896
1897static BOOL update_send_glyph_index(rdpContext* context, GLYPH_INDEX_ORDER* glyph_index)
1898{
1899 ORDER_INFO orderInfo = WINPR_C_ARRAY_INIT;
1900
1901 WINPR_ASSERT(context);
1902 WINPR_ASSERT(glyph_index);
1903 rdp_update_internal* update = update_cast(context->update);
1904
1905 const size_t headerLength =
1906 update_prepare_order_info(context, &orderInfo, ORDER_TYPE_GLYPH_INDEX);
1907 const size_t inf = update_approximate_glyph_index_order(&orderInfo, glyph_index);
1908 if (!update_check_flush(context, headerLength + inf))
1909 return FALSE;
1910
1911 wStream* s = update->us;
1912
1913 if (!s)
1914 return FALSE;
1915
1916 const size_t offset = Stream_GetPosition(s);
1917
1918 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1919 return FALSE;
1920
1921 Stream_Seek(s, headerLength);
1922 if (!update_write_glyph_index_order(s, &orderInfo, glyph_index))
1923 return FALSE;
1924 if (update_write_order_info(context, s, &orderInfo, offset) < 0)
1925 return FALSE;
1926 update->numberOrders++;
1927 return TRUE;
1928}
1929
1930/*
1931 * Secondary Drawing Orders
1932 */
1933
1934static BOOL update_send_cache_bitmap(rdpContext* context, const CACHE_BITMAP_ORDER* cache_bitmap)
1935{
1936 const size_t headerLength = 6;
1937 UINT16 extraFlags = 0;
1938
1939 WINPR_ASSERT(context);
1940 WINPR_ASSERT(cache_bitmap);
1941 rdp_update_internal* update = update_cast(context->update);
1942
1943 const BYTE orderType = cache_bitmap->compressed ? ORDER_TYPE_CACHE_BITMAP_COMPRESSED
1944 : ORDER_TYPE_BITMAP_UNCOMPRESSED;
1945 const size_t inf =
1946 update_approximate_cache_bitmap_order(cache_bitmap, cache_bitmap->compressed, &extraFlags);
1947 if (!update_check_flush(context, headerLength + inf))
1948 return FALSE;
1949
1950 wStream* s = update->us;
1951
1952 if (!s)
1953 return FALSE;
1954
1955 const size_t bm = Stream_GetPosition(s);
1956
1957 if (!Stream_EnsureRemainingCapacity(s, headerLength))
1958 return FALSE;
1959
1960 Stream_Seek(s, headerLength);
1961
1962 if (!update_write_cache_bitmap_order(s, cache_bitmap, cache_bitmap->compressed, &extraFlags))
1963 return FALSE;
1964
1965 const size_t em = Stream_GetPosition(s);
1966 WINPR_ASSERT(em >= bm + 13);
1967 const size_t orderLength = (em - bm) - 13;
1968 WINPR_ASSERT(orderLength <= UINT16_MAX);
1969
1970 if (!Stream_SetPosition(s, bm))
1971 return FALSE;
1972 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
1973 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
1974 Stream_Write_UINT16(s, extraFlags); /* extraFlags (2 bytes) */
1975 Stream_Write_UINT8(s, orderType); /* orderType (1 byte) */
1976 update->numberOrders++;
1977 return Stream_SetPosition(s, em);
1978}
1979
1980static BOOL update_send_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cache_bitmap_v2)
1981{
1982 const size_t headerLength = 6;
1983 UINT16 extraFlags = 0;
1984
1985 WINPR_ASSERT(context);
1986 WINPR_ASSERT(cache_bitmap_v2);
1987 rdp_update_internal* update = update_cast(context->update);
1988
1989 const BYTE orderType = cache_bitmap_v2->compressed ? ORDER_TYPE_BITMAP_COMPRESSED_V2
1990 : ORDER_TYPE_BITMAP_UNCOMPRESSED_V2;
1991
1992 if (context->settings->NoBitmapCompressionHeader)
1993 cache_bitmap_v2->flags |= CBR2_NO_BITMAP_COMPRESSION_HDR;
1994
1995 if (!update_check_flush(
1996 context, headerLength + update_approximate_cache_bitmap_v2_order(
1997 cache_bitmap_v2, cache_bitmap_v2->compressed, &extraFlags)))
1998 return FALSE;
1999
2000 wStream* s = update->us;
2001
2002 if (!s)
2003 return FALSE;
2004
2005 const size_t bm = Stream_GetPosition(s);
2006
2007 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2008 return FALSE;
2009
2010 Stream_Seek(s, headerLength);
2011
2012 if (!update_write_cache_bitmap_v2_order(s, cache_bitmap_v2, cache_bitmap_v2->compressed,
2013 &extraFlags))
2014 return FALSE;
2015
2016 const size_t em = Stream_GetPosition(s);
2017 WINPR_ASSERT(em >= bm + 13);
2018 const size_t orderLength = (em - bm) - 13;
2019 WINPR_ASSERT(orderLength <= UINT16_MAX);
2020
2021 if (!Stream_SetPosition(s, bm))
2022 return FALSE;
2023 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2024 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
2025 Stream_Write_UINT16(s, extraFlags); /* extraFlags (2 bytes) */
2026 Stream_Write_UINT8(s, orderType); /* orderType (1 byte) */
2027 update->numberOrders++;
2028 return Stream_SetPosition(s, em);
2029}
2030
2031static BOOL update_send_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cache_bitmap_v3)
2032{
2033 const size_t headerLength = 6;
2034 UINT16 extraFlags = 0;
2035
2036 WINPR_ASSERT(context);
2037 WINPR_ASSERT(cache_bitmap_v3);
2038 rdp_update_internal* update = update_cast(context->update);
2039
2040 const BYTE orderType = ORDER_TYPE_BITMAP_COMPRESSED_V3;
2041 if (!update_check_flush(context, headerLength + update_approximate_cache_bitmap_v3_order(
2042 cache_bitmap_v3, &extraFlags)))
2043 return FALSE;
2044
2045 wStream* s = update->us;
2046
2047 if (!s)
2048 return FALSE;
2049
2050 const size_t bm = Stream_GetPosition(s);
2051
2052 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2053 return FALSE;
2054
2055 Stream_Seek(s, headerLength);
2056
2057 if (!update_write_cache_bitmap_v3_order(s, cache_bitmap_v3, &extraFlags))
2058 return FALSE;
2059
2060 const size_t em = Stream_GetPosition(s);
2061 WINPR_ASSERT(em >= bm + 13);
2062 const size_t orderLength = (em - bm) - 13;
2063 WINPR_ASSERT(orderLength <= UINT16_MAX);
2064
2065 if (!Stream_SetPosition(s, bm))
2066 return FALSE;
2067 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2068 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
2069 Stream_Write_UINT16(s, extraFlags); /* extraFlags (2 bytes) */
2070 Stream_Write_UINT8(s, orderType); /* orderType (1 byte) */
2071 update->numberOrders++;
2072 return Stream_SetPosition(s, em);
2073}
2074
2075static BOOL update_send_cache_color_table(rdpContext* context,
2076 const CACHE_COLOR_TABLE_ORDER* cache_color_table)
2077{
2078 UINT16 flags = 0;
2079 size_t headerLength = 6;
2080
2081 WINPR_ASSERT(context);
2082 WINPR_ASSERT(cache_color_table);
2083 rdp_update_internal* update = update_cast(context->update);
2084
2085 const size_t inf = update_approximate_cache_color_table_order(cache_color_table, &flags);
2086 if (!update_check_flush(context, headerLength + inf))
2087 return FALSE;
2088
2089 wStream* s = update->us;
2090
2091 if (!s)
2092 return FALSE;
2093
2094 const size_t bm = Stream_GetPosition(s);
2095
2096 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2097 return FALSE;
2098
2099 Stream_Seek(s, headerLength);
2100
2101 if (!update_write_cache_color_table_order(s, cache_color_table, &flags))
2102 return FALSE;
2103
2104 const size_t em = Stream_GetPosition(s);
2105 WINPR_ASSERT(em >= bm + 13);
2106 const size_t orderLength = (em - bm) - 13;
2107 WINPR_ASSERT(orderLength <= UINT16_MAX);
2108 if (!Stream_SetPosition(s, bm))
2109 return FALSE;
2110 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2111 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
2112 Stream_Write_UINT16(s, flags); /* extraFlags (2 bytes) */
2113 Stream_Write_UINT8(s, ORDER_TYPE_CACHE_COLOR_TABLE); /* orderType (1 byte) */
2114 update->numberOrders++;
2115 return Stream_SetPosition(s, em);
2116}
2117
2118static BOOL update_send_cache_glyph(rdpContext* context, const CACHE_GLYPH_ORDER* cache_glyph)
2119{
2120 UINT16 flags = 0;
2121 const size_t headerLength = 6;
2122
2123 WINPR_ASSERT(context);
2124 WINPR_ASSERT(cache_glyph);
2125 rdp_update_internal* update = update_cast(context->update);
2126
2127 const size_t inf = update_approximate_cache_glyph_order(cache_glyph, &flags);
2128 if (!update_check_flush(context, headerLength + inf))
2129 return FALSE;
2130
2131 wStream* s = update->us;
2132
2133 if (!s)
2134 return FALSE;
2135
2136 const size_t bm = Stream_GetPosition(s);
2137
2138 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2139 return FALSE;
2140
2141 Stream_Seek(s, headerLength);
2142
2143 if (!update_write_cache_glyph_order(s, cache_glyph, &flags))
2144 return FALSE;
2145
2146 const size_t em = Stream_GetPosition(s);
2147 WINPR_ASSERT(em >= bm + 13);
2148 const size_t orderLength = (em - bm) - 13;
2149 WINPR_ASSERT(orderLength <= UINT16_MAX);
2150 if (!Stream_SetPosition(s, bm))
2151 return FALSE;
2152 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2153 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
2154 Stream_Write_UINT16(s, flags); /* extraFlags (2 bytes) */
2155 Stream_Write_UINT8(s, ORDER_TYPE_CACHE_GLYPH); /* orderType (1 byte) */
2156 update->numberOrders++;
2157 return Stream_SetPosition(s, em);
2158}
2159
2160static BOOL update_send_cache_glyph_v2(rdpContext* context,
2161 const CACHE_GLYPH_V2_ORDER* cache_glyph_v2)
2162{
2163 UINT16 flags = 0;
2164 const size_t headerLength = 6;
2165
2166 WINPR_ASSERT(context);
2167 WINPR_ASSERT(cache_glyph_v2);
2168 rdp_update_internal* update = update_cast(context->update);
2169
2170 const size_t inf = update_approximate_cache_glyph_v2_order(cache_glyph_v2, &flags);
2171 if (!update_check_flush(context, headerLength + inf))
2172 return FALSE;
2173
2174 wStream* s = update->us;
2175
2176 if (!s)
2177 return FALSE;
2178
2179 const size_t bm = Stream_GetPosition(s);
2180
2181 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2182 return FALSE;
2183
2184 Stream_Seek(s, headerLength);
2185
2186 if (!update_write_cache_glyph_v2_order(s, cache_glyph_v2, &flags))
2187 return FALSE;
2188
2189 const size_t em = Stream_GetPosition(s);
2190 WINPR_ASSERT(em >= bm + 13);
2191 const size_t orderLength = (em - bm) - 13;
2192 WINPR_ASSERT(orderLength <= UINT16_MAX);
2193 if (!Stream_SetPosition(s, bm))
2194 return FALSE;
2195 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2196 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
2197 Stream_Write_UINT16(s, flags); /* extraFlags (2 bytes) */
2198 Stream_Write_UINT8(s, ORDER_TYPE_CACHE_GLYPH); /* orderType (1 byte) */
2199 update->numberOrders++;
2200 return Stream_SetPosition(s, em);
2201}
2202
2203static BOOL update_send_cache_brush(rdpContext* context, const CACHE_BRUSH_ORDER* cache_brush)
2204{
2205 UINT16 flags = 0;
2206 const size_t headerLength = 6;
2207
2208 WINPR_ASSERT(context);
2209 WINPR_ASSERT(cache_brush);
2210 rdp_update_internal* update = update_cast(context->update);
2211
2212 const size_t inf = update_approximate_cache_brush_order(cache_brush, &flags);
2213 if (!update_check_flush(context, headerLength + inf))
2214 return FALSE;
2215
2216 wStream* s = update->us;
2217
2218 if (!s)
2219 return FALSE;
2220
2221 const size_t bm = Stream_GetPosition(s);
2222
2223 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2224 return FALSE;
2225
2226 Stream_Seek(s, headerLength);
2227
2228 if (!update_write_cache_brush_order(s, cache_brush, &flags))
2229 return FALSE;
2230
2231 const size_t em = Stream_GetPosition(s);
2232 if (em <= bm + 13)
2233 return FALSE;
2234
2235 const size_t orderLength = (em - bm) - 13;
2236 WINPR_ASSERT(orderLength <= UINT16_MAX);
2237 if (!Stream_SetPosition(s, bm))
2238 return FALSE;
2239 Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2240 Stream_Write_UINT16(s, (UINT16)orderLength); /* orderLength (2 bytes) */
2241 Stream_Write_UINT16(s, flags); /* extraFlags (2 bytes) */
2242 Stream_Write_UINT8(s, ORDER_TYPE_CACHE_BRUSH); /* orderType (1 byte) */
2243 update->numberOrders++;
2244 return Stream_SetPosition(s, em);
2245}
2246
2251static BOOL update_send_create_offscreen_bitmap_order(
2252 rdpContext* context, const CREATE_OFFSCREEN_BITMAP_ORDER* create_offscreen_bitmap)
2253{
2254 WINPR_ASSERT(context);
2255 WINPR_ASSERT(create_offscreen_bitmap);
2256 rdp_update_internal* update = update_cast(context->update);
2257
2258 const size_t headerLength = 1;
2259 const size_t orderType = ORDER_TYPE_CREATE_OFFSCREEN_BITMAP;
2260 const size_t controlFlags = ORDER_SECONDARY | (orderType << 2);
2261 const size_t inf = update_approximate_create_offscreen_bitmap_order(create_offscreen_bitmap);
2262 if (!update_check_flush(context, headerLength + inf))
2263 return FALSE;
2264
2265 wStream* s = update->us;
2266
2267 if (!s)
2268 return FALSE;
2269
2270 const size_t bm = Stream_GetPosition(s);
2271
2272 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2273 return FALSE;
2274
2275 Stream_Seek(s, headerLength);
2276
2277 if (!update_write_create_offscreen_bitmap_order(s, create_offscreen_bitmap))
2278 return FALSE;
2279
2280 const size_t em = Stream_GetPosition(s);
2281 if (!Stream_SetPosition(s, bm))
2282 return FALSE;
2283 Stream_Write_UINT8(s,
2284 WINPR_ASSERTING_INT_CAST(uint8_t, controlFlags)); /* controlFlags (1 byte) */
2285 update->numberOrders++;
2286 return Stream_SetPosition(s, em);
2287}
2288
2289static BOOL update_send_switch_surface_order(rdpContext* context,
2290 const SWITCH_SURFACE_ORDER* switch_surface)
2291{
2292 WINPR_ASSERT(context);
2293 WINPR_ASSERT(switch_surface);
2294 rdp_update_internal* update = update_cast(context->update);
2295
2296 const size_t headerLength = 1;
2297 const size_t orderType = ORDER_TYPE_SWITCH_SURFACE;
2298 const size_t controlFlags = ORDER_SECONDARY | (orderType << 2);
2299 const size_t inf = update_approximate_switch_surface_order(switch_surface);
2300 if (!update_check_flush(context, headerLength + inf))
2301 return FALSE;
2302
2303 wStream* s = update->us;
2304
2305 if (!s)
2306 return FALSE;
2307
2308 const size_t bm = Stream_GetPosition(s);
2309
2310 if (!Stream_EnsureRemainingCapacity(s, headerLength))
2311 return FALSE;
2312
2313 Stream_Seek(s, headerLength);
2314
2315 if (!update_write_switch_surface_order(s, switch_surface))
2316 return FALSE;
2317
2318 const size_t em = Stream_GetPosition(s);
2319 if (!Stream_SetPosition(s, bm))
2320 return FALSE;
2321 Stream_Write_UINT8(s,
2322 WINPR_ASSERTING_INT_CAST(uint8_t, controlFlags)); /* controlFlags (1 byte) */
2323 update->numberOrders++;
2324 return Stream_SetPosition(s, em);
2325}
2326
2327static BOOL update_send_pointer_system(rdpContext* context,
2328 const POINTER_SYSTEM_UPDATE* pointer_system)
2329{
2330 wStream* s = nullptr;
2331 BYTE updateCode = 0;
2332
2333 WINPR_ASSERT(context);
2334 rdpRdp* rdp = context->rdp;
2335 BOOL ret = 0;
2336
2337 WINPR_ASSERT(rdp);
2338 s = fastpath_update_pdu_init(rdp->fastpath);
2339
2340 if (!s)
2341 return FALSE;
2342
2343 if (pointer_system->type == SYSPTR_NULL)
2344 updateCode = FASTPATH_UPDATETYPE_PTR_NULL;
2345 else
2346 updateCode = FASTPATH_UPDATETYPE_PTR_DEFAULT;
2347
2348 ret = fastpath_send_update_pdu(rdp->fastpath, updateCode, s, FALSE);
2349 Stream_Release(s);
2350 return ret;
2351}
2352
2353static BOOL update_send_pointer_position(rdpContext* context,
2354 const POINTER_POSITION_UPDATE* pointerPosition)
2355{
2356 wStream* s = nullptr;
2357 WINPR_ASSERT(context);
2358 rdpRdp* rdp = context->rdp;
2359 BOOL ret = FALSE;
2360
2361 WINPR_ASSERT(rdp);
2362 s = fastpath_update_pdu_init(rdp->fastpath);
2363
2364 if (!s)
2365 return FALSE;
2366
2367 if (!Stream_EnsureRemainingCapacity(s, 16))
2368 goto out_fail;
2369
2370 Stream_Write_UINT16(
2371 s, WINPR_ASSERTING_INT_CAST(uint16_t, pointerPosition->xPos)); /* xPos (2 bytes) */
2372 Stream_Write_UINT16(
2373 s, WINPR_ASSERTING_INT_CAST(uint16_t, pointerPosition->yPos)); /* yPos (2 bytes) */
2374 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_PTR_POSITION, s, FALSE);
2375out_fail:
2376 Stream_Release(s);
2377 return ret;
2378}
2379
2380static BOOL update_write_pointer_color(wStream* s, const POINTER_COLOR_UPDATE* pointer_color)
2381{
2382 WINPR_ASSERT(pointer_color);
2383 if (!Stream_EnsureRemainingCapacity(s, 32 + pointer_color->lengthAndMask +
2384 pointer_color->lengthXorMask))
2385 return FALSE;
2386
2387 Stream_Write_UINT16(s, pointer_color->cacheIndex);
2388 Stream_Write_UINT16(s, pointer_color->hotSpotX);
2389 Stream_Write_UINT16(s, pointer_color->hotSpotY);
2390 Stream_Write_UINT16(s, pointer_color->width);
2391 Stream_Write_UINT16(s, pointer_color->height);
2392 Stream_Write_UINT16(s, pointer_color->lengthAndMask);
2393 Stream_Write_UINT16(s, pointer_color->lengthXorMask);
2394
2395 if (pointer_color->lengthXorMask > 0)
2396 Stream_Write(s, pointer_color->xorMaskData, pointer_color->lengthXorMask);
2397
2398 if (pointer_color->lengthAndMask > 0)
2399 Stream_Write(s, pointer_color->andMaskData, pointer_color->lengthAndMask);
2400
2401 Stream_Write_UINT8(s, 0); /* pad (1 byte) */
2402 return TRUE;
2403}
2404
2405static BOOL update_send_pointer_color(rdpContext* context,
2406 const POINTER_COLOR_UPDATE* pointer_color)
2407{
2408 wStream* s = nullptr;
2409
2410 WINPR_ASSERT(context);
2411 rdpRdp* rdp = context->rdp;
2412 BOOL ret = FALSE;
2413
2414 WINPR_ASSERT(rdp);
2415 WINPR_ASSERT(pointer_color);
2416 s = fastpath_update_pdu_init(rdp->fastpath);
2417
2418 if (!s)
2419 return FALSE;
2420
2421 if (!update_write_pointer_color(s, pointer_color))
2422 goto out_fail;
2423
2424 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_COLOR, s, FALSE);
2425out_fail:
2426 Stream_Release(s);
2427 return ret;
2428}
2429
2430static BOOL update_write_pointer_large(wStream* s, const POINTER_LARGE_UPDATE* pointer)
2431{
2432 WINPR_ASSERT(pointer);
2433
2434 if (!Stream_EnsureRemainingCapacity(s, 32 + pointer->lengthAndMask + pointer->lengthXorMask))
2435 return FALSE;
2436
2437 Stream_Write_UINT16(s, pointer->xorBpp);
2438 Stream_Write_UINT16(s, pointer->cacheIndex);
2439 Stream_Write_UINT16(s, pointer->hotSpotX);
2440 Stream_Write_UINT16(s, pointer->hotSpotY);
2441 Stream_Write_UINT16(s, pointer->width);
2442 Stream_Write_UINT16(s, pointer->height);
2443 Stream_Write_UINT32(s, pointer->lengthAndMask);
2444 Stream_Write_UINT32(s, pointer->lengthXorMask);
2445 Stream_Write(s, pointer->xorMaskData, pointer->lengthXorMask);
2446 Stream_Write(s, pointer->andMaskData, pointer->lengthAndMask);
2447 Stream_Write_UINT8(s, 0); /* pad (1 byte) */
2448 return TRUE;
2449}
2450
2451static BOOL update_send_pointer_large(rdpContext* context, const POINTER_LARGE_UPDATE* pointer)
2452{
2453 wStream* s = nullptr;
2454 WINPR_ASSERT(context);
2455 rdpRdp* rdp = context->rdp;
2456 BOOL ret = FALSE;
2457
2458 WINPR_ASSERT(rdp);
2459 WINPR_ASSERT(pointer);
2460 s = fastpath_update_pdu_init(rdp->fastpath);
2461
2462 if (!s)
2463 return FALSE;
2464
2465 if (!update_write_pointer_large(s, pointer))
2466 goto out_fail;
2467
2468 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_LARGE_POINTER, s, FALSE);
2469out_fail:
2470 Stream_Release(s);
2471 return ret;
2472}
2473
2474static BOOL update_send_pointer_new(rdpContext* context, const POINTER_NEW_UPDATE* pointer_new)
2475{
2476 wStream* s = nullptr;
2477
2478 WINPR_ASSERT(context);
2479 rdpRdp* rdp = context->rdp;
2480 BOOL ret = FALSE;
2481
2482 WINPR_ASSERT(rdp);
2483 WINPR_ASSERT(pointer_new);
2484 s = fastpath_update_pdu_init(rdp->fastpath);
2485
2486 if (!s)
2487 return FALSE;
2488
2489 if (!Stream_EnsureRemainingCapacity(s, 16))
2490 goto out_fail;
2491
2492 Stream_Write_UINT16(
2493 s, WINPR_ASSERTING_INT_CAST(uint16_t, pointer_new->xorBpp)); /* xorBpp (2 bytes) */
2494 update_write_pointer_color(s, &pointer_new->colorPtrAttr);
2495 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_POINTER, s, FALSE);
2496out_fail:
2497 Stream_Release(s);
2498 return ret;
2499}
2500
2501static BOOL update_send_pointer_cached(rdpContext* context,
2502 const POINTER_CACHED_UPDATE* pointer_cached)
2503{
2504 wStream* s = nullptr;
2505
2506 WINPR_ASSERT(context);
2507 rdpRdp* rdp = context->rdp;
2508 BOOL ret = 0;
2509
2510 WINPR_ASSERT(rdp);
2511 WINPR_ASSERT(pointer_cached);
2512 s = fastpath_update_pdu_init(rdp->fastpath);
2513
2514 if (!s)
2515 return FALSE;
2516
2517 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
2518 uint16_t, pointer_cached->cacheIndex)); /* cacheIndex (2 bytes) */
2519 ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_CACHED, s, FALSE);
2520 Stream_Release(s);
2521 return ret;
2522}
2523
2524BOOL update_read_refresh_rect(rdpUpdate* update, wStream* s)
2525{
2526 BYTE numberOfAreas = 0;
2527 RECTANGLE_16 areas[256] = WINPR_C_ARRAY_INIT;
2528 rdp_update_internal* up = update_cast(update);
2529
2530 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2531 return FALSE;
2532
2533 Stream_Read_UINT8(s, numberOfAreas);
2534 Stream_Seek(s, 3); /* pad3Octects */
2535
2536 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, numberOfAreas, 8ull))
2537 return FALSE;
2538
2539 for (BYTE index = 0; index < numberOfAreas; index++)
2540 {
2541 RECTANGLE_16* area = &areas[index];
2542
2543 Stream_Read_UINT16(s, area->left);
2544 Stream_Read_UINT16(s, area->top);
2545 Stream_Read_UINT16(s, area->right);
2546 Stream_Read_UINT16(s, area->bottom);
2547 }
2548
2549 WINPR_ASSERT(update->context);
2550 WINPR_ASSERT(update->context->settings);
2551 if (update->context->settings->RefreshRect)
2552 IFCALL(update->RefreshRect, update->context, numberOfAreas, areas);
2553 else
2554 WLog_Print(up->log, WLOG_WARN, "ignoring refresh rect request from client");
2555
2556 return TRUE;
2557}
2558
2559BOOL update_read_suppress_output(rdpUpdate* update, wStream* s)
2560{
2561 rdp_update_internal* up = update_cast(update);
2562 RECTANGLE_16* prect = nullptr;
2563 RECTANGLE_16 rect = WINPR_C_ARRAY_INIT;
2564 BYTE allowDisplayUpdates = 0;
2565
2566 WINPR_ASSERT(up);
2567 WINPR_ASSERT(s);
2568
2569 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2570 return FALSE;
2571
2572 Stream_Read_UINT8(s, allowDisplayUpdates);
2573 Stream_Seek(s, 3); /* pad3Octects */
2574
2575 if (allowDisplayUpdates > 0)
2576 {
2577 if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(RECTANGLE_16)))
2578 return FALSE;
2579
2580 Stream_Read_UINT16(s, rect.left);
2581 Stream_Read_UINT16(s, rect.top);
2582 Stream_Read_UINT16(s, rect.right);
2583 Stream_Read_UINT16(s, rect.bottom);
2584
2585 prect = &rect;
2586 }
2587
2588 WINPR_ASSERT(update->context);
2589 WINPR_ASSERT(update->context->settings);
2590 if (update->context->settings->SuppressOutput)
2591 IFCALL(update->SuppressOutput, update->context, allowDisplayUpdates, prect);
2592 else
2593 WLog_Print(up->log, WLOG_WARN, "ignoring suppress output request from client");
2594
2595 return TRUE;
2596}
2597
2598static BOOL update_send_set_keyboard_indicators(rdpContext* context, UINT16 led_flags)
2599{
2600 UINT16 sec_flags = 0;
2601 wStream* s = nullptr;
2602
2603 WINPR_ASSERT(context);
2604 rdpRdp* rdp = context->rdp;
2605 s = rdp_data_pdu_init(rdp, &sec_flags);
2606
2607 if (!s)
2608 return FALSE;
2609
2610 Stream_Write_UINT16(s, 0); /* unitId should be 0 according to MS-RDPBCGR 2.2.8.2.1.1 */
2611 Stream_Write_UINT16(s, led_flags); /* ledFlags (2 bytes) */
2612
2613 WINPR_ASSERT(rdp->mcs);
2614 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS, rdp->mcs->userId,
2615 sec_flags);
2616}
2617
2618static BOOL update_send_set_keyboard_ime_status(rdpContext* context, UINT16 imeId, UINT32 imeState,
2619 UINT32 imeConvMode)
2620{
2621 UINT16 sec_flags = 0;
2622 wStream* s = nullptr;
2623
2624 WINPR_ASSERT(context);
2625 rdpRdp* rdp = context->rdp;
2626 s = rdp_data_pdu_init(rdp, &sec_flags);
2627
2628 if (!s)
2629 return FALSE;
2630
2631 /* unitId should be 0 according to MS-RDPBCGR 2.2.8.2.2.1 */
2632 Stream_Write_UINT16(s, imeId);
2633 Stream_Write_UINT32(s, imeState);
2634 Stream_Write_UINT32(s, imeConvMode);
2635
2636 WINPR_ASSERT(rdp->mcs);
2637 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS, rdp->mcs->userId,
2638 sec_flags);
2639}
2640
2641static UINT16 update_calculate_new_or_existing_window(const WINDOW_ORDER_INFO* orderInfo,
2642 const WINDOW_STATE_ORDER* stateOrder)
2643{
2644 size_t orderSize = 11;
2645
2646 WINPR_ASSERT(orderInfo);
2647 WINPR_ASSERT(stateOrder);
2648
2649 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_OWNER) != 0)
2650 orderSize += 4;
2651
2652 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_STYLE) != 0)
2653 orderSize += 8;
2654
2655 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_SHOW) != 0)
2656 orderSize += 1;
2657
2658 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_TITLE) != 0)
2659 orderSize += 2 + stateOrder->titleInfo.length;
2660
2661 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_CLIENT_AREA_OFFSET) != 0)
2662 orderSize += 8;
2663
2664 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_CLIENT_AREA_SIZE) != 0)
2665 orderSize += 8;
2666
2667 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_RESIZE_MARGIN_X) != 0)
2668 orderSize += 8;
2669
2670 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_RESIZE_MARGIN_Y) != 0)
2671 orderSize += 8;
2672
2673 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_RP_CONTENT) != 0)
2674 orderSize += 1;
2675
2676 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_ROOT_PARENT) != 0)
2677 orderSize += 4;
2678
2679 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_WND_OFFSET) != 0)
2680 orderSize += 8;
2681
2682 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_WND_CLIENT_DELTA) != 0)
2683 orderSize += 8;
2684
2685 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_WND_SIZE) != 0)
2686 orderSize += 8;
2687
2688 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_WND_RECTS) != 0)
2689 {
2690 const size_t len = 2ULL + stateOrder->numWindowRects * sizeof(RECTANGLE_16);
2691 orderSize += len;
2692 }
2693
2694 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_VIS_OFFSET) != 0)
2695 orderSize += 8;
2696
2697 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_VISIBILITY) != 0)
2698 {
2699
2700 const size_t len = 2ULL + stateOrder->numVisibilityRects * sizeof(RECTANGLE_16);
2701 orderSize += len;
2702 }
2703
2704 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_OVERLAY_DESCRIPTION) != 0)
2705 orderSize += 2 + stateOrder->OverlayDescription.length;
2706
2707 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_TASKBAR_BUTTON) != 0)
2708 orderSize += 1;
2709
2710 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_ENFORCE_SERVER_ZORDER) != 0)
2711 orderSize += 1;
2712
2713 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_APPBAR_STATE) != 0)
2714 orderSize += 1;
2715
2716 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_APPBAR_EDGE) != 0)
2717 orderSize += 1;
2718
2719 return WINPR_ASSERTING_INT_CAST(uint16_t, orderSize);
2720}
2721
2722static BOOL update_write_order_field_flags(UINT32 fieldFlags, const WINDOW_STATE_ORDER* stateOrder,
2723 wStream* s)
2724{
2725 WINPR_ASSERT(stateOrder);
2726
2727 if ((fieldFlags & WINDOW_ORDER_FIELD_OWNER) != 0)
2728 Stream_Write_UINT32(s, stateOrder->ownerWindowId);
2729
2730 if ((fieldFlags & WINDOW_ORDER_FIELD_STYLE) != 0)
2731 {
2732 Stream_Write_UINT32(s, stateOrder->style);
2733 Stream_Write_UINT32(s, stateOrder->extendedStyle);
2734 }
2735
2736 if ((fieldFlags & WINDOW_ORDER_FIELD_SHOW) != 0)
2737 {
2738 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, stateOrder->showState));
2739 }
2740
2741 if ((fieldFlags & WINDOW_ORDER_FIELD_TITLE) != 0)
2742 {
2743 Stream_Write_UINT16(s, stateOrder->titleInfo.length);
2744 Stream_Write(s, stateOrder->titleInfo.string, stateOrder->titleInfo.length);
2745 }
2746
2747 if ((fieldFlags & WINDOW_ORDER_FIELD_CLIENT_AREA_OFFSET) != 0)
2748 {
2749 Stream_Write_INT32(s, stateOrder->clientOffsetX);
2750 Stream_Write_INT32(s, stateOrder->clientOffsetY);
2751 }
2752
2753 if ((fieldFlags & WINDOW_ORDER_FIELD_CLIENT_AREA_SIZE) != 0)
2754 {
2755 Stream_Write_UINT32(s, stateOrder->clientAreaWidth);
2756 Stream_Write_UINT32(s, stateOrder->clientAreaHeight);
2757 }
2758
2759 if ((fieldFlags & WINDOW_ORDER_FIELD_RESIZE_MARGIN_X) != 0)
2760 {
2761 Stream_Write_UINT32(s, stateOrder->resizeMarginLeft);
2762 Stream_Write_UINT32(s, stateOrder->resizeMarginRight);
2763 }
2764
2765 if ((fieldFlags & WINDOW_ORDER_FIELD_RESIZE_MARGIN_Y) != 0)
2766 {
2767 Stream_Write_UINT32(s, stateOrder->resizeMarginTop);
2768 Stream_Write_UINT32(s, stateOrder->resizeMarginBottom);
2769 }
2770
2771 if ((fieldFlags & WINDOW_ORDER_FIELD_RP_CONTENT) != 0)
2772 {
2773 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, stateOrder->RPContent));
2774 }
2775
2776 if ((fieldFlags & WINDOW_ORDER_FIELD_ROOT_PARENT) != 0)
2777 {
2778 Stream_Write_UINT32(s, stateOrder->rootParentHandle);
2779 }
2780
2781 if ((fieldFlags & WINDOW_ORDER_FIELD_WND_OFFSET) != 0)
2782 {
2783 Stream_Write_INT32(s, stateOrder->windowOffsetX);
2784 Stream_Write_INT32(s, stateOrder->windowOffsetY);
2785 }
2786
2787 if ((fieldFlags & WINDOW_ORDER_FIELD_WND_CLIENT_DELTA) != 0)
2788 {
2789 Stream_Write_INT32(s, stateOrder->windowClientDeltaX);
2790 Stream_Write_INT32(s, stateOrder->windowClientDeltaY);
2791 }
2792
2793 if ((fieldFlags & WINDOW_ORDER_FIELD_WND_SIZE) != 0)
2794 {
2795 Stream_Write_UINT32(s, stateOrder->windowWidth);
2796 Stream_Write_UINT32(s, stateOrder->windowHeight);
2797 }
2798
2799 if ((fieldFlags & WINDOW_ORDER_FIELD_WND_RECTS) != 0)
2800 {
2801 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, stateOrder->numWindowRects));
2802 Stream_Write(s, stateOrder->windowRects, stateOrder->numWindowRects * sizeof(RECTANGLE_16));
2803 }
2804
2805 if ((fieldFlags & WINDOW_ORDER_FIELD_VIS_OFFSET) != 0)
2806 {
2807 Stream_Write_INT32(s, stateOrder->visibleOffsetX);
2808 Stream_Write_INT32(s, stateOrder->visibleOffsetY);
2809 }
2810
2811 if ((fieldFlags & WINDOW_ORDER_FIELD_VISIBILITY) != 0)
2812 {
2813 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, stateOrder->numVisibilityRects));
2814 Stream_Write(s, stateOrder->visibilityRects,
2815 stateOrder->numVisibilityRects * sizeof(RECTANGLE_16));
2816 }
2817
2818 if ((fieldFlags & WINDOW_ORDER_FIELD_OVERLAY_DESCRIPTION) != 0)
2819 {
2820 Stream_Write_UINT16(s, stateOrder->OverlayDescription.length);
2821 Stream_Write(s, stateOrder->OverlayDescription.string,
2822 stateOrder->OverlayDescription.length);
2823 }
2824
2825 if ((fieldFlags & WINDOW_ORDER_FIELD_TASKBAR_BUTTON) != 0)
2826 {
2827 Stream_Write_UINT8(s, stateOrder->TaskbarButton);
2828 }
2829
2830 if ((fieldFlags & WINDOW_ORDER_FIELD_ENFORCE_SERVER_ZORDER) != 0)
2831 {
2832 Stream_Write_UINT8(s, stateOrder->EnforceServerZOrder);
2833 }
2834
2835 if ((fieldFlags & WINDOW_ORDER_FIELD_APPBAR_STATE) != 0)
2836 {
2837 Stream_Write_UINT8(s, stateOrder->AppBarState);
2838 }
2839
2840 if ((fieldFlags & WINDOW_ORDER_FIELD_APPBAR_EDGE) != 0)
2841 {
2842 Stream_Write_UINT8(s, stateOrder->AppBarEdge);
2843 }
2844
2845 return TRUE;
2846}
2847
2848static BOOL update_send_new_or_existing_window(rdpContext* context,
2849 const WINDOW_ORDER_INFO* orderInfo,
2850 const WINDOW_STATE_ORDER* stateOrder)
2851{
2852 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
2853 UINT16 orderSize = update_calculate_new_or_existing_window(orderInfo, stateOrder);
2854
2855 WINPR_ASSERT(context);
2856 WINPR_ASSERT(orderInfo);
2857 WINPR_ASSERT(stateOrder);
2858
2859 rdp_update_internal* update = update_cast(context->update);
2860
2861 if (!update_check_flush(context, orderSize))
2862 return FALSE;
2863
2864 wStream* s = update->us;
2865
2866 if (!s)
2867 return FALSE;
2868
2869 if (!Stream_EnsureRemainingCapacity(s, orderSize))
2870 return FALSE;
2871
2872 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
2873 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
2874 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
2875 Stream_Write_UINT32(s, orderInfo->windowId); /* WindowID (4 bytes) */
2876
2877 if (!update_write_order_field_flags(orderInfo->fieldFlags, stateOrder, s))
2878 return FALSE;
2879
2880 update->numberOrders++;
2881 return TRUE;
2882}
2883
2884static BOOL update_send_window_create(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
2885 const WINDOW_STATE_ORDER* stateOrder)
2886{
2887 return update_send_new_or_existing_window(context, orderInfo, stateOrder);
2888}
2889
2890static BOOL update_send_window_update(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
2891 const WINDOW_STATE_ORDER* stateOrder)
2892{
2893 return update_send_new_or_existing_window(context, orderInfo, stateOrder);
2894}
2895
2896static UINT16
2897update_calculate_window_icon_order(WINPR_ATTR_UNUSED const WINDOW_ORDER_INFO* orderInfo,
2898 const WINDOW_ICON_ORDER* iconOrder)
2899{
2900 UINT16 orderSize = 23;
2901
2902 WINPR_ASSERT(iconOrder);
2903 ICON_INFO* iconInfo = iconOrder->iconInfo;
2904 WINPR_ASSERT(iconInfo);
2905
2906 orderSize += iconInfo->cbBitsColor + iconInfo->cbBitsMask;
2907
2908 if (iconInfo->bpp <= 8)
2909 orderSize += 2 + iconInfo->cbColorTable;
2910
2911 return orderSize;
2912}
2913
2914static BOOL update_send_window_icon(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
2915 const WINDOW_ICON_ORDER* iconOrder)
2916{
2917 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
2918
2919 WINPR_ASSERT(iconOrder);
2920 ICON_INFO* iconInfo = iconOrder->iconInfo;
2921 UINT16 orderSize = update_calculate_window_icon_order(orderInfo, iconOrder);
2922
2923 WINPR_ASSERT(context);
2924 WINPR_ASSERT(orderInfo);
2925 WINPR_ASSERT(iconInfo);
2926
2927 rdp_update_internal* update = update_cast(context->update);
2928
2929 if (!update_check_flush(context, orderSize))
2930 return FALSE;
2931
2932 wStream* s = update->us;
2933
2934 if (!s || !iconInfo)
2935 return FALSE;
2936
2937 if (!Stream_EnsureRemainingCapacity(s, orderSize))
2938 return FALSE;
2939
2940 /* Write Hdr */
2941 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
2942 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
2943 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
2944 Stream_Write_UINT32(s, orderInfo->windowId); /* WindowID (4 bytes) */
2945 /* Write body */
2946 Stream_Write_UINT16(
2947 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->cacheEntry)); /* CacheEntry (2 bytes) */
2948 Stream_Write_UINT8(s,
2949 WINPR_ASSERTING_INT_CAST(uint8_t, iconInfo->cacheId)); /* CacheId (1 byte) */
2950 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, iconInfo->bpp)); /* Bpp (1 byte) */
2951 Stream_Write_UINT16(s,
2952 WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->width)); /* Width (2 bytes) */
2953 Stream_Write_UINT16(
2954 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->height)); /* Height (2 bytes) */
2955
2956 if (iconInfo->bpp <= 8)
2957 {
2958 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
2959 uint16_t, iconInfo->cbColorTable)); /* CbColorTable (2 bytes) */
2960 }
2961
2962 Stream_Write_UINT16(
2963 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->cbBitsMask)); /* CbBitsMask (2 bytes) */
2964 Stream_Write_UINT16(
2965 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->cbBitsColor)); /* CbBitsColor (2 bytes) */
2966 Stream_Write(s, iconInfo->bitsMask, iconInfo->cbBitsMask); /* BitsMask (variable) */
2967
2968 if (iconInfo->bpp <= 8)
2969 {
2970 Stream_Write(s, iconInfo->colorTable, iconInfo->cbColorTable); /* ColorTable (variable) */
2971 }
2972
2973 Stream_Write(s, iconInfo->bitsColor, iconInfo->cbBitsColor); /* BitsColor (variable) */
2974
2975 update->numberOrders++;
2976 return TRUE;
2977}
2978
2979static BOOL update_send_window_cached_icon(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
2980 const WINDOW_CACHED_ICON_ORDER* cachedIconOrder)
2981{
2982 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
2983 UINT16 orderSize = 14;
2984
2985 WINPR_ASSERT(cachedIconOrder);
2986 const CACHED_ICON_INFO* cachedIcon = &cachedIconOrder->cachedIcon;
2987
2988 WINPR_ASSERT(context);
2989 WINPR_ASSERT(orderInfo);
2990 WINPR_ASSERT(cachedIcon);
2991
2992 rdp_update_internal* update = update_cast(context->update);
2993
2994 if (!update_check_flush(context, orderSize))
2995 return FALSE;
2996
2997 wStream* s = update->us;
2998 if (!s)
2999 return FALSE;
3000
3001 if (!Stream_EnsureRemainingCapacity(s, orderSize))
3002 return FALSE;
3003
3004 /* Write Hdr */
3005 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
3006 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
3007 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
3008 Stream_Write_UINT32(s, orderInfo->windowId); /* WindowID (4 bytes) */
3009 /* Write body */
3010 Stream_Write_UINT16(
3011 s, WINPR_ASSERTING_INT_CAST(uint16_t, cachedIcon->cacheEntry)); /* CacheEntry (2 bytes) */
3012 Stream_Write_UINT8(
3013 s, WINPR_ASSERTING_INT_CAST(uint8_t, cachedIcon->cacheId)); /* CacheId (1 byte) */
3014 update->numberOrders++;
3015 return TRUE;
3016}
3017
3018static BOOL update_send_window_delete(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo)
3019{
3020 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
3021 UINT16 orderSize = 11;
3022
3023 WINPR_ASSERT(context);
3024 WINPR_ASSERT(orderInfo);
3025 rdp_update_internal* update = update_cast(context->update);
3026
3027 if (!update_check_flush(context, orderSize))
3028 return FALSE;
3029
3030 wStream* s = update->us;
3031
3032 if (!s)
3033 return FALSE;
3034
3035 if (!Stream_EnsureRemainingCapacity(s, orderSize))
3036 return FALSE;
3037
3038 /* Write Hdr */
3039 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
3040 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
3041 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
3042 Stream_Write_UINT32(s, orderInfo->windowId); /* WindowID (4 bytes) */
3043 update->numberOrders++;
3044 return TRUE;
3045}
3046
3047static UINT16 update_calculate_new_or_existing_notification_icons_order(
3048 const WINDOW_ORDER_INFO* orderInfo, const NOTIFY_ICON_STATE_ORDER* iconStateOrder)
3049{
3050 UINT16 orderSize = 15;
3051
3052 WINPR_ASSERT(orderInfo);
3053 WINPR_ASSERT(iconStateOrder);
3054
3055 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_VERSION) != 0)
3056 orderSize += 4;
3057
3058 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_TIP) != 0)
3059 {
3060 orderSize += 2 + iconStateOrder->toolTip.length;
3061 }
3062
3063 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_INFO_TIP) != 0)
3064 {
3065 NOTIFY_ICON_INFOTIP infoTip = iconStateOrder->infoTip;
3066 orderSize += 12 + infoTip.text.length + infoTip.title.length;
3067 }
3068
3069 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_STATE) != 0)
3070 {
3071 orderSize += 4;
3072 }
3073
3074 if ((orderInfo->fieldFlags & WINDOW_ORDER_ICON) != 0)
3075 {
3076 ICON_INFO iconInfo = iconStateOrder->icon;
3077 orderSize += 12;
3078
3079 if (iconInfo.bpp <= 8)
3080 orderSize += 2 + iconInfo.cbColorTable;
3081
3082 orderSize += iconInfo.cbBitsMask + iconInfo.cbBitsColor;
3083 }
3084 else if ((orderInfo->fieldFlags & WINDOW_ORDER_CACHED_ICON) != 0)
3085 {
3086 orderSize += 3;
3087 }
3088
3089 return orderSize;
3090}
3091
3092static BOOL update_send_new_or_existing_order_icon(const ICON_INFO* iconInfo, wStream* s)
3093{
3094 WINPR_ASSERT(iconInfo);
3095
3096 if (!Stream_EnsureRemainingCapacity(s, 8))
3097 return FALSE;
3098
3099 Stream_Write_UINT16(
3100 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->cacheEntry)); /* CacheEntry (2 bytes) */
3101 Stream_Write_UINT8(s,
3102 WINPR_ASSERTING_INT_CAST(uint8_t, iconInfo->cacheId)); /* CacheId (1 byte) */
3103 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, iconInfo->bpp)); /* Bpp (1 byte) */
3104 Stream_Write_UINT16(s,
3105 WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->width)); /* Width (2 bytes) */
3106 Stream_Write_UINT16(
3107 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->height)); /* Height (2 bytes) */
3108
3109 if (iconInfo->bpp <= 8)
3110 {
3111 if (!Stream_EnsureRemainingCapacity(s, 2))
3112 return FALSE;
3113 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
3114 uint16_t, iconInfo->cbColorTable)); /* CbColorTable (2 bytes) */
3115 }
3116
3117 if (!Stream_EnsureRemainingCapacity(s, 4ULL + iconInfo->cbBitsMask))
3118 return FALSE;
3119 Stream_Write_UINT16(
3120 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->cbBitsMask)); /* CbBitsMask (2 bytes) */
3121 Stream_Write_UINT16(
3122 s, WINPR_ASSERTING_INT_CAST(uint16_t, iconInfo->cbBitsColor)); /* CbBitsColor (2 bytes) */
3123 Stream_Write(s, iconInfo->bitsMask, iconInfo->cbBitsMask); /* BitsMask (variable) */
3124
3125 if (iconInfo->bpp <= 8)
3126 {
3127 if (!Stream_EnsureRemainingCapacity(s, iconInfo->cbColorTable))
3128 return FALSE;
3129 Stream_Write(s, iconInfo->colorTable, iconInfo->cbColorTable); /* ColorTable (variable) */
3130 }
3131
3132 if (!Stream_EnsureRemainingCapacity(s, iconInfo->cbBitsColor))
3133 return FALSE;
3134 Stream_Write(s, iconInfo->bitsColor, iconInfo->cbBitsColor); /* BitsColor (variable) */
3135 return TRUE;
3136}
3137
3138static BOOL
3139update_send_new_or_existing_notification_icons(rdpContext* context,
3140 const WINDOW_ORDER_INFO* orderInfo,
3141 const NOTIFY_ICON_STATE_ORDER* iconStateOrder)
3142{
3143 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
3144 BOOL versionFieldPresent = FALSE;
3145 const UINT16 orderSize =
3146 update_calculate_new_or_existing_notification_icons_order(orderInfo, iconStateOrder);
3147
3148 WINPR_ASSERT(context);
3149 WINPR_ASSERT(orderInfo);
3150 WINPR_ASSERT(iconStateOrder);
3151 rdp_update_internal* update = update_cast(context->update);
3152
3153 if (!update_check_flush(context, orderSize))
3154 return FALSE;
3155
3156 wStream* s = update->us;
3157 if (!s)
3158 return FALSE;
3159
3160 if (!Stream_EnsureRemainingCapacity(s, orderSize))
3161 return FALSE;
3162
3163 /* Write Hdr */
3164 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
3165 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
3166 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
3167 Stream_Write_UINT32(s, orderInfo->windowId); /* WindowID (4 bytes) */
3168 Stream_Write_UINT32(s, orderInfo->notifyIconId); /* NotifyIconId (4 bytes) */
3169
3170 /* Write body */
3171 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_VERSION) != 0)
3172 {
3173 versionFieldPresent = TRUE;
3174 Stream_Write_UINT32(s, iconStateOrder->version);
3175 }
3176
3177 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_TIP) != 0)
3178 {
3179 Stream_Write_UINT16(s, iconStateOrder->toolTip.length);
3180 Stream_Write(s, iconStateOrder->toolTip.string, iconStateOrder->toolTip.length);
3181 }
3182
3183 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_INFO_TIP) != 0)
3184 {
3185 NOTIFY_ICON_INFOTIP infoTip = iconStateOrder->infoTip;
3186
3187 /* info tip should not be sent when version is 0 */
3188 if (versionFieldPresent && iconStateOrder->version == 0)
3189 return FALSE;
3190
3191 Stream_Write_UINT32(s, infoTip.timeout); /* Timeout (4 bytes) */
3192 Stream_Write_UINT32(s, infoTip.flags); /* InfoFlags (4 bytes) */
3193 Stream_Write_UINT16(s, infoTip.text.length); /* InfoTipText (variable) */
3194 Stream_Write(s, infoTip.text.string, infoTip.text.length);
3195 Stream_Write_UINT16(s, infoTip.title.length); /* Title (variable) */
3196 Stream_Write(s, infoTip.title.string, infoTip.title.length);
3197 }
3198
3199 if ((orderInfo->fieldFlags & WINDOW_ORDER_FIELD_NOTIFY_STATE) != 0)
3200 {
3201 /* notify state should not be sent when version is 0 */
3202 if (versionFieldPresent && iconStateOrder->version == 0)
3203 return FALSE;
3204
3205 Stream_Write_UINT32(s, iconStateOrder->state);
3206 }
3207
3208 if ((orderInfo->fieldFlags & WINDOW_ORDER_ICON) != 0)
3209 {
3210 const ICON_INFO* iconInfo = &iconStateOrder->icon;
3211
3212 if (!update_send_new_or_existing_order_icon(iconInfo, s))
3213 return FALSE;
3214 }
3215 else if ((orderInfo->fieldFlags & WINDOW_ORDER_CACHED_ICON) != 0)
3216 {
3217 const CACHED_ICON_INFO cachedIcon = iconStateOrder->cachedIcon;
3218 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
3219 uint16_t, cachedIcon.cacheEntry)); /* CacheEntry (2 bytes) */
3220 Stream_Write_UINT8(
3221 s, WINPR_ASSERTING_INT_CAST(uint8_t, cachedIcon.cacheId)); /* CacheId (1 byte) */
3222 }
3223
3224 update->numberOrders++;
3225 return TRUE;
3226}
3227
3228static BOOL update_send_notify_icon_create(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
3229 const NOTIFY_ICON_STATE_ORDER* iconStateOrder)
3230{
3231 return update_send_new_or_existing_notification_icons(context, orderInfo, iconStateOrder);
3232}
3233
3234static BOOL update_send_notify_icon_update(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
3235 const NOTIFY_ICON_STATE_ORDER* iconStateOrder)
3236{
3237 return update_send_new_or_existing_notification_icons(context, orderInfo, iconStateOrder);
3238}
3239
3240static BOOL update_send_notify_icon_delete(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo)
3241{
3242 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
3243 UINT16 orderSize = 15;
3244
3245 WINPR_ASSERT(context);
3246 WINPR_ASSERT(orderInfo);
3247 rdp_update_internal* update = update_cast(context->update);
3248
3249 if (!update_check_flush(context, orderSize))
3250 return FALSE;
3251
3252 wStream* s = update->us;
3253
3254 if (!s)
3255 return FALSE;
3256
3257 /* Write Hdr */
3258 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
3259 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
3260 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
3261 Stream_Write_UINT32(s, orderInfo->windowId); /* WindowID (4 bytes) */
3262 Stream_Write_UINT32(s, orderInfo->notifyIconId); /* NotifyIconId (4 bytes) */
3263 update->numberOrders++;
3264 return TRUE;
3265}
3266
3267static UINT16 update_calculate_monitored_desktop(const WINDOW_ORDER_INFO* orderInfo,
3268 const MONITORED_DESKTOP_ORDER* monitoredDesktop)
3269{
3270 UINT16 orderSize = 7;
3271
3272 WINPR_ASSERT(orderInfo);
3273 WINPR_ASSERT(monitoredDesktop);
3274
3275 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)
3276 {
3277 orderSize += 4;
3278 }
3279
3280 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
3281 {
3282 orderSize += 1 + (4 * monitoredDesktop->numWindowIds);
3283 }
3284
3285 return orderSize;
3286}
3287
3288static BOOL update_send_monitored_desktop(rdpContext* context, const WINDOW_ORDER_INFO* orderInfo,
3289 const MONITORED_DESKTOP_ORDER* monitoredDesktop)
3290{
3291 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
3292 UINT16 orderSize = update_calculate_monitored_desktop(orderInfo, monitoredDesktop);
3293
3294 WINPR_ASSERT(context);
3295 WINPR_ASSERT(orderInfo);
3296 WINPR_ASSERT(monitoredDesktop);
3297
3298 rdp_update_internal* update = update_cast(context->update);
3299
3300 if (!update_check_flush(context, orderSize))
3301 return FALSE;
3302
3303 wStream* s = update->us;
3304
3305 if (!s)
3306 return FALSE;
3307
3308 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
3309 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
3310 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
3311
3312 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ACTIVE_WND)
3313 {
3314 Stream_Write_UINT32(s, monitoredDesktop->activeWindowId); /* activeWindowId (4 bytes) */
3315 }
3316
3317 if (orderInfo->fieldFlags & WINDOW_ORDER_FIELD_DESKTOP_ZORDER)
3318 {
3319 Stream_Write_UINT8(
3320 s, WINPR_ASSERTING_INT_CAST(
3321 uint8_t, monitoredDesktop->numWindowIds)); /* numWindowIds (1 byte) */
3322
3323 /* windowIds */
3324 for (UINT32 i = 0; i < monitoredDesktop->numWindowIds; i++)
3325 {
3326 Stream_Write_UINT32(s,
3327 WINPR_ASSERTING_INT_CAST(uint32_t, monitoredDesktop->windowIds[i]));
3328 }
3329 }
3330
3331 update->numberOrders++;
3332 return TRUE;
3333}
3334
3335static BOOL update_send_non_monitored_desktop(rdpContext* context,
3336 const WINDOW_ORDER_INFO* orderInfo)
3337{
3338 BYTE controlFlags = ORDER_SECONDARY | (ORDER_TYPE_WINDOW << 2);
3339 UINT16 orderSize = 7;
3340
3341 WINPR_ASSERT(context);
3342 WINPR_ASSERT(orderInfo);
3343 rdp_update_internal* update = update_cast(context->update);
3344
3345 if (!update_check_flush(context, orderSize))
3346 return FALSE;
3347
3348 wStream* s = update->us;
3349
3350 if (!s)
3351 return FALSE;
3352
3353 Stream_Write_UINT8(s, controlFlags); /* Header (1 byte) */
3354 Stream_Write_UINT16(s, orderSize); /* OrderSize (2 bytes) */
3355 Stream_Write_UINT32(s, orderInfo->fieldFlags); /* FieldsPresentFlags (4 bytes) */
3356 update->numberOrders++;
3357 return TRUE;
3358}
3359
3360void update_register_server_callbacks(rdpUpdate* update)
3361{
3362 WINPR_ASSERT(update);
3363
3364 update->BeginPaint = s_update_begin_paint;
3365 update->EndPaint = s_update_end_paint;
3366 update->SetBounds = update_set_bounds;
3367 update->Synchronize = update_send_synchronize;
3368 update->DesktopResize = update_send_desktop_resize;
3369 update->BitmapUpdate = update_send_bitmap_update;
3370 update->SurfaceBits = update_send_surface_bits;
3371 update->SurfaceFrameMarker = update_send_surface_frame_marker;
3372 update->SurfaceCommand = update_send_surface_command;
3373 update->SurfaceFrameBits = update_send_surface_frame_bits;
3374 update->PlaySound = update_send_play_sound;
3375 update->SetKeyboardIndicators = update_send_set_keyboard_indicators;
3376 update->SetKeyboardImeStatus = update_send_set_keyboard_ime_status;
3377 update->SaveSessionInfo = rdp_send_save_session_info;
3378 update->ServerStatusInfo = rdp_send_server_status_info;
3379 update->primary->DstBlt = update_send_dstblt;
3380 update->primary->PatBlt = update_send_patblt;
3381 update->primary->ScrBlt = update_send_scrblt;
3382 update->primary->OpaqueRect = update_send_opaque_rect;
3383 update->primary->LineTo = update_send_line_to;
3384 update->primary->MemBlt = update_send_memblt;
3385 update->primary->GlyphIndex = update_send_glyph_index;
3386 update->secondary->CacheBitmap = update_send_cache_bitmap;
3387 update->secondary->CacheBitmapV2 = update_send_cache_bitmap_v2;
3388 update->secondary->CacheBitmapV3 = update_send_cache_bitmap_v3;
3389 update->secondary->CacheColorTable = update_send_cache_color_table;
3390 update->secondary->CacheGlyph = update_send_cache_glyph;
3391 update->secondary->CacheGlyphV2 = update_send_cache_glyph_v2;
3392 update->secondary->CacheBrush = update_send_cache_brush;
3393 update->altsec->CreateOffscreenBitmap = update_send_create_offscreen_bitmap_order;
3394 update->altsec->SwitchSurface = update_send_switch_surface_order;
3395 update->pointer->PointerSystem = update_send_pointer_system;
3396 update->pointer->PointerPosition = update_send_pointer_position;
3397 update->pointer->PointerColor = update_send_pointer_color;
3398 update->pointer->PointerLarge = update_send_pointer_large;
3399 update->pointer->PointerNew = update_send_pointer_new;
3400 update->pointer->PointerCached = update_send_pointer_cached;
3401 update->window->WindowCreate = update_send_window_create;
3402 update->window->WindowUpdate = update_send_window_update;
3403 update->window->WindowIcon = update_send_window_icon;
3404 update->window->WindowCachedIcon = update_send_window_cached_icon;
3405 update->window->WindowDelete = update_send_window_delete;
3406 update->window->NotifyIconCreate = update_send_notify_icon_create;
3407 update->window->NotifyIconUpdate = update_send_notify_icon_update;
3408 update->window->NotifyIconDelete = update_send_notify_icon_delete;
3409 update->window->MonitoredDesktop = update_send_monitored_desktop;
3410 update->window->NonMonitoredDesktop = update_send_non_monitored_desktop;
3411}
3412
3413void update_register_client_callbacks(rdpUpdate* update)
3414{
3415 WINPR_ASSERT(update);
3416
3417 update->RefreshRect = update_send_refresh_rect;
3418 update->SuppressOutput = update_send_suppress_output;
3419 update->SurfaceFrameAcknowledge = update_send_frame_acknowledge;
3420}
3421
3422int update_process_messages(rdpUpdate* update)
3423{
3424 return update_message_queue_process_pending_messages(update);
3425}
3426
3427static void update_free_queued_message(void* obj)
3428{
3429 wMessage* msg = (wMessage*)obj;
3430 update_message_queue_free_message(msg);
3431}
3432
3433void update_free_window_state(WINDOW_STATE_ORDER* window_state)
3434{
3435 if (!window_state)
3436 return;
3437
3438 free(window_state->OverlayDescription.string);
3439 free(window_state->titleInfo.string);
3440 free(window_state->windowRects);
3441 free(window_state->visibilityRects);
3442 memset(window_state, 0, sizeof(WINDOW_STATE_ORDER));
3443}
3444
3445rdpUpdate* update_new(rdpRdp* rdp)
3446{
3447 const wObject cb = { nullptr, nullptr, nullptr, update_free_queued_message, nullptr };
3448
3449 WINPR_ASSERT(rdp);
3450 WINPR_ASSERT(rdp->context);
3451
3452 rdp_update_internal* update = (rdp_update_internal*)calloc(1, sizeof(rdp_update_internal));
3453
3454 if (!update)
3455 return nullptr;
3456
3457 update->common.context = rdp->context;
3458 update->log = WLog_Get("com.freerdp.core.update");
3459 InitializeCriticalSection(&(update->mux));
3460 update->common.pointer = (rdpPointerUpdate*)calloc(1, sizeof(rdpPointerUpdate));
3461
3462 if (!update->common.pointer)
3463 goto fail;
3464
3465 {
3468
3469 if (!primary)
3470 goto fail;
3471 update->common.primary = &primary->common;
3472 }
3473
3474 {
3477
3478 if (!secondary)
3479 goto fail;
3480 update->common.secondary = &secondary->common;
3481 }
3482
3483 {
3486
3487 if (!altsec)
3488 goto fail;
3489
3490 update->common.altsec = &altsec->common;
3491
3492 update->common.window = (rdpWindowUpdate*)calloc(1, sizeof(rdpWindowUpdate));
3493
3494 if (!update->common.window)
3495 goto fail;
3496
3497 {
3498 OFFSCREEN_DELETE_LIST* deleteList = &(altsec->create_offscreen_bitmap.deleteList);
3499 deleteList->sIndices = 64;
3500 deleteList->indices = calloc(deleteList->sIndices, 2);
3501
3502 if (!deleteList->indices)
3503 goto fail;
3504
3505 deleteList->cIndices = 0;
3506 }
3507 }
3508
3509 update->common.SuppressOutput = update_send_suppress_output;
3510 update->initialState = TRUE;
3511 update->common.autoCalculateBitmapData = TRUE;
3512 update->queue = MessageQueue_New(&cb);
3513
3514 if (!update->queue)
3515 goto fail;
3516
3517 return &update->common;
3518fail:
3519 WINPR_PRAGMA_DIAG_PUSH
3520 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
3521 update_free(&update->common);
3522 WINPR_PRAGMA_DIAG_POP
3523 return nullptr;
3524}
3525
3526void update_free(rdpUpdate* update)
3527{
3528 if (update != nullptr)
3529 {
3530 rdp_update_internal* up = update_cast(update);
3531 rdp_altsec_update_internal* altsec = altsec_update_cast(update->altsec);
3532 OFFSCREEN_DELETE_LIST* deleteList = &(altsec->create_offscreen_bitmap.deleteList);
3533
3534 if (deleteList)
3535 free(deleteList->indices);
3536
3537 free(update->pointer);
3538
3539 if (update->primary)
3540 {
3541 rdp_primary_update_internal* primary = primary_update_cast(update->primary);
3542
3543 free(primary->polygon_cb.points);
3544 free(primary->polyline.points);
3545 free(primary->polygon_sc.points);
3546 free(primary->fast_glyph.glyphData.aj);
3547 free(primary);
3548 }
3549
3550 free(update->secondary);
3551 free(altsec);
3552
3553 if (update->window)
3554 free(update->window);
3555
3556 MessageQueue_Free(up->queue);
3557 DeleteCriticalSection(&up->mux);
3558
3559 if (up->us)
3560 Stream_Free(up->us, TRUE);
3561 free(update);
3562 }
3563}
3564
3565void rdp_update_lock(rdpUpdate* update)
3566{
3567 rdp_update_internal* up = update_cast(update);
3568 EnterCriticalSection(&up->mux);
3569}
3570
3571void rdp_update_unlock(rdpUpdate* update)
3572{
3573 rdp_update_internal* up = update_cast(update);
3574 LeaveCriticalSection(&up->mux);
3575}
3576
3577BOOL update_begin_paint(rdpUpdate* update)
3578{
3579 rdp_update_internal* up = update_cast(update);
3580 WINPR_ASSERT(update);
3581 rdp_update_lock(update);
3582
3583 up->withinBeginEndPaint = TRUE;
3584
3585 WINPR_ASSERT(update->context);
3586
3587 up->stats.base[RDP_STATS_BEGIN_PAINT]++;
3588 BOOL rc = IFCALLRESULT(TRUE, update->BeginPaint, update->context);
3589 if (!rc)
3590 WLog_WARN(TAG, "BeginPaint call failed");
3591
3592 /* Reset the invalid regions, we start a new frame here. */
3593 rdpGdi* gdi = update->context->gdi;
3594 if (!gdi)
3595 return rc;
3596
3597 if (gdi->hdc && gdi->primary && gdi->primary->hdc)
3598 {
3599 HGDI_WND hwnd = gdi->primary->hdc->hwnd;
3600 WINPR_ASSERT(hwnd);
3601 WINPR_ASSERT(hwnd->invalid);
3602
3603 hwnd->invalid->null = TRUE;
3604 hwnd->ninvalid = 0;
3605 }
3606
3607 return rc;
3608}
3609
3610BOOL update_end_paint(rdpUpdate* update)
3611{
3612 rdp_update_internal* up = update_cast(update);
3613 BOOL rc = TRUE;
3614
3615 WINPR_ASSERT(update);
3616 up->stats.base[RDP_STATS_END_PAINT]++;
3617
3618 IFCALLRET(update->EndPaint, rc, update->context);
3619 if (!rc)
3620 WLog_WARN(TAG, "EndPaint call failed");
3621
3622 if (!up->withinBeginEndPaint)
3623 return rc;
3624 up->withinBeginEndPaint = FALSE;
3625
3626 rdp_update_unlock(update);
3627 return rc;
3628}
3629
3630uint64_t rdp_stats_value_for_index(rdpUpdate* context, size_t index)
3631{
3632 rdp_update_internal* up = update_cast(context);
3633 WINPR_ASSERT(up);
3634
3635 size_t limit = ARRAYSIZE(up->stats.primary);
3636 size_t offset = 0;
3637 if (index < limit)
3638 return up->stats.primary[index];
3639
3640 offset = limit;
3641 limit += ARRAYSIZE(up->stats.secondary);
3642 if (index < limit)
3643 return up->stats.secondary[index - offset];
3644
3645 offset = limit;
3646 limit += ARRAYSIZE(up->stats.altsec);
3647 if (index < limit)
3648 return up->stats.altsec[index - offset];
3649
3650 offset = limit;
3651 limit += ARRAYSIZE(up->stats.base);
3652 if (index < limit)
3653 return up->stats.base[index - offset];
3654
3655 return 0;
3656}
3657
3658const char* rdp_stats_name_for_index(size_t index)
3659{
3660 if (!InitOnceExecuteOnce(&stats_names_once, stats_names_generate, nullptr, nullptr))
3661 return "RDP_STATS_UNUSED";
3662 if (index < rdp_stats_max_index())
3663 return stats_names[index];
3664 return "RDP_STATS_UNUSED";
3665}
3666
3667size_t rdp_stats_max_index(void)
3668{
3669 return RDP_STATS_COUNT;
3670}
3671
3672void update_dump_stats(rdpUpdate* update)
3673{
3674 rdp_update_internal* up = update_cast(update);
3675 WINPR_ASSERT(up);
3676
3677 wLog* log = up->log;
3678 const DWORD level = WLOG_TRACE;
3679 if (!WLog_IsLevelActive(log, level))
3680 return;
3681
3682 WLog_Print(log, level, "RdpCodecStats");
3683 for (size_t x = 0; x < rdp_stats_max_index(); x++)
3684 {
3685 const char* name = rdp_stats_name_for_index(x);
3686 const uint64_t val = rdp_stats_value_for_index(update, x);
3687 WINPR_ASSERT(name && strnlen(name, 2) > 0);
3688 const bool unknown = strstr(name, " UNKNOWN") != nullptr;
3689 const bool unused = strstr(name, "UNUSED") != nullptr;
3690 const bool sunused = strcmp("RDP_STATS_UNUSED", name) == 0;
3691 if ((val != 0) || (!unknown && !sunused && !unused))
3692 WLog_Print(log, level, "%s: %" PRIu64, name, val);
3693 }
3694}
Definition types.h:82
This struct contains function pointer to initialize/free objects.
Definition collections.h:52