Coverage Report

Created: 2025-08-29 06:49

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