Coverage Report

Created: 2025-12-02 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/update.c
Line
Count
Source
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
554
{
55
554
  if (updateType >= ARRAYSIZE(UPDATE_TYPE_STRINGS))
56
39
    return "UNKNOWN";
57
58
515
  return UPDATE_TYPE_STRINGS[updateType];
59
554
}
60
61
static BOOL update_recv_orders(rdpUpdate* update, wStream* s)
62
353
{
63
353
  UINT16 numberOrders = 0;
64
65
353
  WINPR_ASSERT(update);
66
67
353
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
68
1
    return FALSE;
69
70
352
  Stream_Seek_UINT16(s);               /* pad2OctetsA (2 bytes) */
71
352
  Stream_Read_UINT16(s, numberOrders); /* numberOrders (2 bytes) */
72
352
  Stream_Seek_UINT16(s);               /* pad2OctetsB (2 bytes) */
73
74
1.93k
  while (numberOrders > 0)
75
1.91k
  {
76
1.91k
    if (!update_recv_order(update, s))
77
340
    {
78
340
      WLog_ERR(TAG, "update_recv_order() failed");
79
340
      return FALSE;
80
340
    }
81
82
1.57k
    numberOrders--;
83
1.57k
  }
84
85
12
  return TRUE;
86
352
}
87
88
static BOOL update_read_bitmap_data(rdpUpdate* update, wStream* s, BITMAP_DATA* bitmapData)
89
35.3k
{
90
35.3k
  WINPR_UNUSED(update);
91
35.3k
  WINPR_ASSERT(bitmapData);
92
93
35.3k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
94
83
    return FALSE;
95
96
35.2k
  Stream_Read_UINT16(s, bitmapData->destLeft);
97
35.2k
  Stream_Read_UINT16(s, bitmapData->destTop);
98
35.2k
  Stream_Read_UINT16(s, bitmapData->destRight);
99
35.2k
  Stream_Read_UINT16(s, bitmapData->destBottom);
100
35.2k
  Stream_Read_UINT16(s, bitmapData->width);
101
35.2k
  Stream_Read_UINT16(s, bitmapData->height);
102
35.2k
  Stream_Read_UINT16(s, bitmapData->bitsPerPixel);
103
35.2k
  Stream_Read_UINT16(s, bitmapData->flags);
104
35.2k
  Stream_Read_UINT16(s, bitmapData->bitmapLength);
105
106
35.2k
  if ((bitmapData->width == 0) || (bitmapData->height == 0))
107
109
  {
108
109
    WLog_ERR(TAG, "Invalid BITMAP_DATA: width=%" PRIu16 ", height=%" PRIu16, bitmapData->width,
109
109
             bitmapData->height);
110
109
    return FALSE;
111
109
  }
112
113
35.1k
  if (bitmapData->flags & BITMAP_COMPRESSION)
114
24.9k
  {
115
24.9k
    if (!(bitmapData->flags & NO_BITMAP_COMPRESSION_HDR))
116
16.4k
    {
117
16.4k
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
118
11
        return FALSE;
119
120
16.4k
      Stream_Read_UINT16(s,
121
16.4k
                         bitmapData->cbCompFirstRowSize); /* cbCompFirstRowSize (2 bytes) */
122
16.4k
      Stream_Read_UINT16(s,
123
16.4k
                         bitmapData->cbCompMainBodySize); /* cbCompMainBodySize (2 bytes) */
124
16.4k
      Stream_Read_UINT16(s, bitmapData->cbScanWidth);     /* cbScanWidth (2 bytes) */
125
16.4k
      Stream_Read_UINT16(s,
126
16.4k
                         bitmapData->cbUncompressedSize); /* cbUncompressedSize (2 bytes) */
127
16.4k
      bitmapData->bitmapLength = bitmapData->cbCompMainBodySize;
128
16.4k
    }
129
130
24.9k
    bitmapData->compressed = TRUE;
131
24.9k
  }
132
10.2k
  else
133
10.2k
    bitmapData->compressed = FALSE;
134
135
35.1k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapData->bitmapLength))
136
76
    return FALSE;
137
138
35.0k
  if (bitmapData->bitmapLength > 0)
139
28.4k
  {
140
28.4k
    bitmapData->bitmapDataStream = malloc(bitmapData->bitmapLength);
141
142
28.4k
    if (!bitmapData->bitmapDataStream)
143
0
      return FALSE;
144
145
28.4k
    memcpy(bitmapData->bitmapDataStream, Stream_ConstPointer(s), bitmapData->bitmapLength);
146
28.4k
    Stream_Seek(s, bitmapData->bitmapLength);
147
28.4k
  }
148
149
35.0k
  return TRUE;
150
35.0k
}
151
152
static BOOL update_write_bitmap_data_header(const BITMAP_DATA* bitmapData, wStream* s)
153
11.6k
{
154
11.6k
  WINPR_ASSERT(bitmapData);
155
11.6k
  if (!Stream_EnsureRemainingCapacity(s, 18))
156
0
    return FALSE;
157
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destLeft));
158
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destTop));
159
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destRight));
160
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->destBottom));
161
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->width));
162
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->height));
163
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->bitsPerPixel));
164
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->flags));
165
11.6k
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, bitmapData->bitmapLength));
166
11.6k
  return TRUE;
167
11.6k
}
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
11.6k
{
191
11.6k
  rdp_update_internal* update = update_cast(update_pub);
192
193
11.6k
  WINPR_ASSERT(bitmapData);
194
195
11.6k
  if (!Stream_EnsureRemainingCapacity(s, 64 + bitmapData->bitmapLength))
196
0
    return FALSE;
197
198
11.6k
  if (update->common.autoCalculateBitmapData)
199
11.6k
  {
200
11.6k
    bitmapData->flags = 0;
201
11.6k
    bitmapData->cbCompFirstRowSize = 0;
202
203
11.6k
    if (bitmapData->compressed)
204
5.34k
      bitmapData->flags |= BITMAP_COMPRESSION;
205
206
11.6k
    if (update->common.context->settings->NoBitmapCompressionHeader)
207
11.6k
    {
208
11.6k
      bitmapData->flags |= NO_BITMAP_COMPRESSION_HDR;
209
11.6k
      bitmapData->cbCompMainBodySize = bitmapData->bitmapLength;
210
11.6k
    }
211
11.6k
  }
212
213
11.6k
  if (!update_write_bitmap_data_header(bitmapData, s))
214
0
    return FALSE;
215
216
11.6k
  if (bitmapData->flags & BITMAP_COMPRESSION)
217
5.34k
  {
218
5.34k
    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
5.34k
  }
224
225
11.6k
  if (!Stream_EnsureRemainingCapacity(s, bitmapData->bitmapLength))
226
0
    return FALSE;
227
11.6k
  Stream_Write(s, bitmapData->bitmapDataStream, bitmapData->bitmapLength);
228
229
11.6k
  return TRUE;
230
11.6k
}
231
232
BITMAP_UPDATE* update_read_bitmap_update(rdpUpdate* update, wStream* s)
233
402
{
234
402
  BITMAP_UPDATE* bitmapUpdate = calloc(1, sizeof(BITMAP_UPDATE));
235
402
  rdp_update_internal* up = update_cast(update);
236
237
402
  if (!bitmapUpdate)
238
0
    goto fail;
239
240
402
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
241
6
    goto fail;
242
243
396
  Stream_Read_UINT16(s, bitmapUpdate->number); /* numberRectangles (2 bytes) */
244
396
  WLog_Print(up->log, WLOG_TRACE, "BitmapUpdate: %" PRIu32 "", bitmapUpdate->number);
245
246
396
  bitmapUpdate->rectangles = (BITMAP_DATA*)calloc(bitmapUpdate->number, sizeof(BITMAP_DATA));
247
248
396
  if (!bitmapUpdate->rectangles)
249
0
    goto fail;
250
251
  /* rectangles */
252
35.4k
  for (UINT32 i = 0; i < bitmapUpdate->number; i++)
253
35.3k
  {
254
35.3k
    if (!update_read_bitmap_data(update, s, &bitmapUpdate->rectangles[i]))
255
279
      goto fail;
256
35.3k
  }
257
258
117
  return bitmapUpdate;
259
285
fail:
260
285
  WINPR_PRAGMA_DIAG_PUSH
261
285
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
262
285
  free_bitmap_update(update->context, bitmapUpdate);
263
285
  WINPR_PRAGMA_DIAG_POP
264
285
  return NULL;
265
396
}
266
267
static BOOL update_write_bitmap_update(rdpUpdate* update, wStream* s,
268
                                       const BITMAP_UPDATE* bitmapUpdate)
269
106
{
270
106
  WINPR_ASSERT(update);
271
106
  WINPR_ASSERT(bitmapUpdate);
272
273
106
  if (!Stream_EnsureRemainingCapacity(s, 32))
274
0
    return FALSE;
275
276
106
  Stream_Write_UINT16(s, UPDATE_TYPE_BITMAP); /* updateType */
277
106
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
278
106
                             uint16_t, bitmapUpdate->number)); /* numberRectangles (2 bytes) */
279
280
  /* rectangles */
281
11.7k
  for (UINT32 i = 0; i < bitmapUpdate->number; i++)
282
11.6k
  {
283
11.6k
    if (!update_write_bitmap_data(update, s, &bitmapUpdate->rectangles[i]))
284
0
      return FALSE;
285
11.6k
  }
286
287
106
  return TRUE;
288
106
}
289
290
PALETTE_UPDATE* update_read_palette(rdpUpdate* update, wStream* s)
291
255
{
292
255
  PALETTE_UPDATE* palette_update = calloc(1, sizeof(PALETTE_UPDATE));
293
294
255
  if (!palette_update)
295
0
    goto fail;
296
297
255
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
298
5
    goto fail;
299
300
250
  Stream_Seek_UINT16(s);                         /* pad2Octets (2 bytes) */
301
250
  Stream_Read_UINT32(s, palette_update->number); /* numberColors (4 bytes), must be set to 256 */
302
303
250
  if (palette_update->number > 256)
304
206
    palette_update->number = 256;
305
306
250
  if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, palette_update->number, 3ull))
307
186
    goto fail;
308
309
  /* paletteEntries */
310
9.70k
  for (UINT32 i = 0; i < palette_update->number; i++)
311
9.63k
  {
312
9.63k
    PALETTE_ENTRY* entry = &palette_update->entries[i];
313
9.63k
    Stream_Read_UINT8(s, entry->red);
314
9.63k
    Stream_Read_UINT8(s, entry->green);
315
9.63k
    Stream_Read_UINT8(s, entry->blue);
316
9.63k
  }
317
318
64
  return palette_update;
319
191
fail:
320
191
  WINPR_PRAGMA_DIAG_PUSH
321
191
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
322
191
  free_palette_update(update->context, palette_update);
323
191
  WINPR_PRAGMA_DIAG_POP
324
191
  return NULL;
325
250
}
326
327
static BOOL update_read_synchronize(rdpUpdate* update, wStream* s)
328
2
{
329
2
  WINPR_UNUSED(update);
330
2
  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
2
}
336
337
static BOOL update_read_play_sound(wStream* s, PLAY_SOUND_UPDATE* play_sound)
338
18.3k
{
339
18.3k
  WINPR_ASSERT(play_sound);
340
341
18.3k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
342
8.32k
    return FALSE;
343
344
10.0k
  Stream_Read_UINT32(s, play_sound->duration);  /* duration (4 bytes) */
345
10.0k
  Stream_Read_UINT32(s, play_sound->frequency); /* frequency (4 bytes) */
346
10.0k
  return TRUE;
347
18.3k
}
348
349
BOOL update_recv_play_sound(rdpUpdate* update, wStream* s)
350
18.3k
{
351
18.3k
  PLAY_SOUND_UPDATE play_sound = { 0 };
352
353
18.3k
  WINPR_ASSERT(update);
354
355
18.3k
  if (!update_read_play_sound(s, &play_sound))
356
8.32k
    return FALSE;
357
358
10.0k
  return IFCALLRESULT(FALSE, update->PlaySound, update->context, &play_sound);
359
18.3k
}
360
361
POINTER_POSITION_UPDATE* update_read_pointer_position(rdpUpdate* update, wStream* s)
362
164
{
363
164
  POINTER_POSITION_UPDATE* pointer_position = calloc(1, sizeof(POINTER_POSITION_UPDATE));
364
365
164
  WINPR_ASSERT(update);
366
367
164
  if (!pointer_position)
368
0
    goto fail;
369
370
164
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
371
66
    goto fail;
372
373
98
  Stream_Read_UINT16(s, pointer_position->xPos); /* xPos (2 bytes) */
374
98
  Stream_Read_UINT16(s, pointer_position->yPos); /* yPos (2 bytes) */
375
98
  return pointer_position;
376
66
fail:
377
66
  WINPR_PRAGMA_DIAG_PUSH
378
66
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
379
66
  free_pointer_position_update(update->context, pointer_position);
380
66
  WINPR_PRAGMA_DIAG_POP
381
66
  return NULL;
382
164
}
383
384
POINTER_SYSTEM_UPDATE* update_read_pointer_system(rdpUpdate* update, wStream* s)
385
159
{
386
159
  POINTER_SYSTEM_UPDATE* pointer_system = calloc(1, sizeof(POINTER_SYSTEM_UPDATE));
387
388
159
  WINPR_ASSERT(update);
389
390
159
  if (!pointer_system)
391
0
    goto fail;
392
393
159
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
394
5
    goto fail;
395
396
154
  Stream_Read_UINT32(s, pointer_system->type); /* systemPointerType (4 bytes) */
397
154
  return pointer_system;
398
5
fail:
399
5
  WINPR_PRAGMA_DIAG_PUSH
400
5
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
401
5
  free_pointer_system_update(update->context, pointer_system);
402
5
  WINPR_PRAGMA_DIAG_POP
403
5
  return NULL;
404
159
}
405
406
static BOOL s_update_read_pointer_color(wStream* s, POINTER_COLOR_UPDATE* pointer_color,
407
                                        BYTE xorBpp, UINT32 flags)
408
615
{
409
615
  BYTE* newMask = NULL;
410
615
  UINT32 scanlineSize = 0;
411
615
  UINT32 max = 32;
412
413
615
  WINPR_ASSERT(pointer_color);
414
415
615
  if (flags & LARGE_POINTER_FLAG_96x96)
416
611
    max = 96;
417
418
615
  if (!pointer_color)
419
0
    goto fail;
420
421
615
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 14))
422
58
    goto fail;
423
424
557
  Stream_Read_UINT16(s, pointer_color->cacheIndex); /* cacheIndex (2 bytes) */
425
557
  Stream_Read_UINT16(s, pointer_color->hotSpotX);   /* hotSpot.xPos (2 bytes) */
426
557
  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
557
  Stream_Read_UINT16(s, pointer_color->width);  /* width (2 bytes) */
437
557
  Stream_Read_UINT16(s, pointer_color->height); /* height (2 bytes) */
438
439
557
  if ((pointer_color->width > max) || (pointer_color->height > max))
440
135
    goto fail;
441
442
422
  Stream_Read_UINT16(s, pointer_color->lengthAndMask); /* lengthAndMask (2 bytes) */
443
422
  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
422
  if (pointer_color->hotSpotX >= pointer_color->width)
452
373
    pointer_color->hotSpotX = 0;
453
454
422
  if (pointer_color->hotSpotY >= pointer_color->height)
455
378
    pointer_color->hotSpotY = 0;
456
457
422
  if (pointer_color->lengthXorMask > 0)
458
192
  {
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
192
    if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer_color->lengthXorMask))
471
42
      goto fail;
472
473
150
    scanlineSize = (7 + xorBpp * pointer_color->width) / 8;
474
150
    scanlineSize = ((scanlineSize + 1) / 2) * 2;
475
476
150
    if (scanlineSize * pointer_color->height != pointer_color->lengthXorMask)
477
85
    {
478
85
      WLog_ERR(TAG,
479
85
               "invalid lengthXorMask: width=%" PRIu32 " height=%" PRIu32 ", %" PRIu32
480
85
               " instead of %" PRIu32 "",
481
85
               pointer_color->width, pointer_color->height, pointer_color->lengthXorMask,
482
85
               scanlineSize * pointer_color->height);
483
85
      goto fail;
484
85
    }
485
486
65
    newMask = realloc(pointer_color->xorMaskData, pointer_color->lengthXorMask);
487
488
65
    if (!newMask)
489
0
      goto fail;
490
491
65
    pointer_color->xorMaskData = newMask;
492
65
    Stream_Read(s, pointer_color->xorMaskData, pointer_color->lengthXorMask);
493
65
  }
494
495
295
  if (pointer_color->lengthAndMask > 0)
496
124
  {
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
124
    if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer_color->lengthAndMask))
505
39
      goto fail;
506
507
85
    scanlineSize = ((7 + pointer_color->width) / 8);
508
85
    scanlineSize = ((1 + scanlineSize) / 2) * 2;
509
510
85
    if (scanlineSize * pointer_color->height != pointer_color->lengthAndMask)
511
73
    {
512
73
      WLog_ERR(TAG, "invalid lengthAndMask: %" PRIu32 " instead of %" PRIu32 "",
513
73
               pointer_color->lengthAndMask, scanlineSize * pointer_color->height);
514
73
      goto fail;
515
73
    }
516
517
12
    newMask = realloc(pointer_color->andMaskData, pointer_color->lengthAndMask);
518
519
12
    if (!newMask)
520
0
      goto fail;
521
522
12
    pointer_color->andMaskData = newMask;
523
12
    Stream_Read(s, pointer_color->andMaskData, pointer_color->lengthAndMask);
524
12
  }
525
526
183
  if (Stream_GetRemainingLength(s) > 0)
527
156
    Stream_Seek_UINT8(s); /* pad (1 byte) */
528
529
183
  return TRUE;
530
432
fail:
531
432
  return FALSE;
532
295
}
533
534
POINTER_COLOR_UPDATE* update_read_pointer_color(rdpUpdate* update, wStream* s, BYTE xorBpp)
535
385
{
536
385
  POINTER_COLOR_UPDATE* pointer_color = calloc(1, sizeof(POINTER_COLOR_UPDATE));
537
538
385
  WINPR_ASSERT(update);
539
540
385
  if (!pointer_color)
541
0
    goto fail;
542
543
385
  if (!s_update_read_pointer_color(s, pointer_color, xorBpp,
544
385
                                   update->context->settings->LargePointerFlag))
545
231
    goto fail;
546
547
154
  return pointer_color;
548
231
fail:
549
231
  WINPR_PRAGMA_DIAG_PUSH
550
231
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
551
231
  free_pointer_color_update(update->context, pointer_color);
552
231
  WINPR_PRAGMA_DIAG_POP
553
231
  return NULL;
554
385
}
555
556
static BOOL s_update_read_pointer_large(wStream* s, POINTER_LARGE_UPDATE* pointer)
557
730
{
558
730
  BYTE* newMask = NULL;
559
730
  UINT32 scanlineSize = 0;
560
561
730
  if (!pointer)
562
0
    goto fail;
563
564
730
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
565
52
    goto fail;
566
567
678
  Stream_Read_UINT16(s, pointer->xorBpp);
568
678
  Stream_Read_UINT16(s, pointer->cacheIndex); /* cacheIndex (2 bytes) */
569
678
  Stream_Read_UINT16(s, pointer->hotSpotX);   /* hotSpot.xPos (2 bytes) */
570
678
  Stream_Read_UINT16(s, pointer->hotSpotY);   /* hotSpot.yPos (2 bytes) */
571
572
678
  Stream_Read_UINT16(s, pointer->width);  /* width (2 bytes) */
573
678
  Stream_Read_UINT16(s, pointer->height); /* height (2 bytes) */
574
575
678
  if ((pointer->width > 384) || (pointer->height > 384))
576
61
    goto fail;
577
578
617
  Stream_Read_UINT32(s, pointer->lengthAndMask); /* lengthAndMask (4 bytes) */
579
617
  Stream_Read_UINT32(s, pointer->lengthXorMask); /* lengthXorMask (4 bytes) */
580
581
617
  if (pointer->hotSpotX >= pointer->width)
582
494
    pointer->hotSpotX = 0;
583
584
617
  if (pointer->hotSpotY >= pointer->height)
585
532
    pointer->hotSpotY = 0;
586
587
617
  if (pointer->lengthXorMask > 0)
588
294
  {
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
294
    if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer->lengthXorMask))
601
145
      goto fail;
602
603
149
    scanlineSize = (7 + pointer->xorBpp * pointer->width) / 8;
604
149
    scanlineSize = ((scanlineSize + 1) / 2) * 2;
605
606
149
    if (scanlineSize * pointer->height != pointer->lengthXorMask)
607
81
    {
608
81
      WLog_ERR(TAG,
609
81
               "invalid lengthXorMask: width=%" PRIu32 " height=%" PRIu32 ", %" PRIu32
610
81
               " instead of %" PRIu32 "",
611
81
               pointer->width, pointer->height, pointer->lengthXorMask,
612
81
               scanlineSize * pointer->height);
613
81
      goto fail;
614
81
    }
615
616
68
    newMask = realloc(pointer->xorMaskData, pointer->lengthXorMask);
617
618
68
    if (!newMask)
619
0
      goto fail;
620
621
68
    pointer->xorMaskData = newMask;
622
68
    Stream_Read(s, pointer->xorMaskData, pointer->lengthXorMask);
623
68
  }
624
625
391
  if (pointer->lengthAndMask > 0)
626
207
  {
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
207
    if (!Stream_CheckAndLogRequiredLength(TAG, s, pointer->lengthAndMask))
635
105
      goto fail;
636
637
102
    scanlineSize = ((7 + pointer->width) / 8);
638
102
    scanlineSize = ((1 + scanlineSize) / 2) * 2;
639
640
102
    if (scanlineSize * pointer->height != pointer->lengthAndMask)
641
37
    {
642
37
      WLog_ERR(TAG, "invalid lengthAndMask: %" PRIu32 " instead of %" PRIu32 "",
643
37
               pointer->lengthAndMask, scanlineSize * pointer->height);
644
37
      goto fail;
645
37
    }
646
647
65
    newMask = realloc(pointer->andMaskData, pointer->lengthAndMask);
648
649
65
    if (!newMask)
650
0
      goto fail;
651
652
65
    pointer->andMaskData = newMask;
653
65
    Stream_Read(s, pointer->andMaskData, pointer->lengthAndMask);
654
65
  }
655
656
249
  if (Stream_GetRemainingLength(s) > 0)
657
238
    Stream_Seek_UINT8(s); /* pad (1 byte) */
658
659
249
  return TRUE;
660
481
fail:
661
481
  return FALSE;
662
391
}
663
664
POINTER_LARGE_UPDATE* update_read_pointer_large(rdpUpdate* update, wStream* s)
665
730
{
666
730
  POINTER_LARGE_UPDATE* pointer = calloc(1, sizeof(POINTER_LARGE_UPDATE));
667
668
730
  WINPR_ASSERT(update);
669
670
730
  if (!pointer)
671
0
    goto fail;
672
673
730
  if (!s_update_read_pointer_large(s, pointer))
674
481
    goto fail;
675
676
249
  return pointer;
677
481
fail:
678
481
  WINPR_PRAGMA_DIAG_PUSH
679
481
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
680
481
  free_pointer_large_update(update->context, pointer);
681
481
  WINPR_PRAGMA_DIAG_POP
682
481
  return NULL;
683
730
}
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
29
    goto fail;
696
697
348
  Stream_Read_UINT16(s, pointer_new->xorBpp); /* xorBpp (2 bytes) */
698
699
348
  if ((pointer_new->xorBpp < 1) || (pointer_new->xorBpp > 32))
700
118
  {
701
118
    WLog_ERR(TAG, "invalid xorBpp %" PRIu32 "", pointer_new->xorBpp);
702
118
    goto fail;
703
118
  }
704
705
230
  WINPR_ASSERT(pointer_new->xorBpp <= UINT8_MAX);
706
230
  if (!s_update_read_pointer_color(
707
230
          s, &pointer_new->colorPtrAttr, (UINT8)pointer_new->xorBpp,
708
230
          update->context->settings->LargePointerFlag)) /* colorPtrAttr */
709
201
    goto fail;
710
711
29
  return pointer_new;
712
348
fail:
713
348
  WINPR_PRAGMA_DIAG_PUSH
714
348
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
715
348
  free_pointer_new_update(update->context, pointer_new);
716
348
  WINPR_PRAGMA_DIAG_POP
717
348
  return NULL;
718
230
}
719
720
POINTER_CACHED_UPDATE* update_read_pointer_cached(rdpUpdate* update, wStream* s)
721
225
{
722
225
  POINTER_CACHED_UPDATE* pointer = calloc(1, sizeof(POINTER_CACHED_UPDATE));
723
724
225
  WINPR_ASSERT(update);
725
726
225
  if (!pointer)
727
0
    goto fail;
728
729
225
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
730
146
    goto fail;
731
732
79
  Stream_Read_UINT16(s, pointer->cacheIndex); /* cacheIndex (2 bytes) */
733
79
  return pointer;
734
146
fail:
735
146
  WINPR_PRAGMA_DIAG_PUSH
736
146
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
737
146
  free_pointer_cached_update(update->context, pointer);
738
146
  WINPR_PRAGMA_DIAG_POP
739
146
  return NULL;
740
225
}
741
742
BOOL update_recv_pointer(rdpUpdate* update, wStream* s)
743
18.3k
{
744
18.3k
  BOOL rc = FALSE;
745
18.3k
  UINT16 messageType = 0;
746
747
18.3k
  WINPR_ASSERT(update);
748
749
18.3k
  rdpContext* context = update->context;
750
18.3k
  rdpPointerUpdate* pointer = update->pointer;
751
752
18.3k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2 + 2))
753
8.16k
    return FALSE;
754
755
10.1k
  Stream_Read_UINT16(s, messageType); /* messageType (2 bytes) */
756
10.1k
  Stream_Seek_UINT16(s);              /* pad2Octets (2 bytes) */
757
758
10.1k
  switch (messageType)
759
10.1k
  {
760
27
    case PTR_MSG_TYPE_POSITION:
761
27
    {
762
27
      POINTER_POSITION_UPDATE* pointer_position = update_read_pointer_position(update, s);
763
764
27
      if (pointer_position)
765
17
      {
766
17
        rc = IFCALLRESULT(FALSE, pointer->PointerPosition, context, pointer_position);
767
17
        free_pointer_position_update(context, pointer_position);
768
17
      }
769
27
    }
770
27
    break;
771
772
159
    case PTR_MSG_TYPE_SYSTEM:
773
159
    {
774
159
      POINTER_SYSTEM_UPDATE* pointer_system = update_read_pointer_system(update, s);
775
776
159
      if (pointer_system)
777
154
      {
778
154
        rc = IFCALLRESULT(FALSE, pointer->PointerSystem, context, pointer_system);
779
154
        free_pointer_system_update(context, pointer_system);
780
154
      }
781
159
    }
782
159
    break;
783
784
92
    case PTR_MSG_TYPE_COLOR:
785
92
    {
786
92
      POINTER_COLOR_UPDATE* pointer_color = update_read_pointer_color(update, s, 24);
787
788
92
      if (pointer_color)
789
29
      {
790
29
        rc = IFCALLRESULT(FALSE, pointer->PointerColor, context, pointer_color);
791
29
        free_pointer_color_update(context, pointer_color);
792
29
      }
793
92
    }
794
92
    break;
795
796
207
    case PTR_MSG_TYPE_POINTER_LARGE:
797
207
    {
798
207
      POINTER_LARGE_UPDATE* pointer_large = update_read_pointer_large(update, s);
799
800
207
      if (pointer_large)
801
115
      {
802
115
        rc = IFCALLRESULT(FALSE, pointer->PointerLarge, context, pointer_large);
803
115
        free_pointer_large_update(context, pointer_large);
804
115
      }
805
207
    }
806
207
    break;
807
808
275
    case PTR_MSG_TYPE_POINTER:
809
275
    {
810
275
      POINTER_NEW_UPDATE* pointer_new = update_read_pointer_new(update, s);
811
812
275
      if (pointer_new)
813
12
      {
814
12
        rc = IFCALLRESULT(FALSE, pointer->PointerNew, context, pointer_new);
815
12
        free_pointer_new_update(context, pointer_new);
816
12
      }
817
275
    }
818
275
    break;
819
820
36
    case PTR_MSG_TYPE_CACHED:
821
36
    {
822
36
      POINTER_CACHED_UPDATE* pointer_cached = update_read_pointer_cached(update, s);
823
824
36
      if (pointer_cached)
825
32
      {
826
32
        rc = IFCALLRESULT(FALSE, pointer->PointerCached, context, pointer_cached);
827
32
        free_pointer_cached_update(context, pointer_cached);
828
32
      }
829
36
    }
830
36
    break;
831
832
9.39k
    default:
833
9.39k
      break;
834
10.1k
  }
835
836
10.1k
  return rc;
837
10.1k
}
838
839
BOOL update_recv(rdpUpdate* update, wStream* s)
840
570
{
841
570
  BOOL rc = FALSE;
842
570
  UINT16 updateType = 0;
843
570
  rdp_update_internal* up = update_cast(update);
844
570
  rdpContext* context = update->context;
845
846
570
  WINPR_ASSERT(context);
847
848
570
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
849
3
    return FALSE;
850
851
567
  Stream_Read_UINT16(s, updateType); /* updateType (2 bytes) */
852
567
  WLog_Print(up->log, WLOG_TRACE, "%s Update Data PDU", update_type_to_string(updateType));
853
854
567
  if (!update_begin_paint(update))
855
0
    goto fail;
856
857
567
  switch (updateType)
858
567
  {
859
353
    case UPDATE_TYPE_ORDERS:
860
353
      rc = update_recv_orders(update, s);
861
353
      break;
862
863
127
    case UPDATE_TYPE_BITMAP:
864
127
    {
865
127
      BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s);
866
867
127
      if (!bitmap_update)
868
124
      {
869
124
        WLog_ERR(TAG, "UPDATE_TYPE_BITMAP - update_read_bitmap_update() failed");
870
124
        goto fail;
871
124
      }
872
873
3
      rc = IFCALLRESULT(FALSE, update->BitmapUpdate, context, bitmap_update);
874
3
      free_bitmap_update(context, bitmap_update);
875
3
    }
876
0
    break;
877
878
46
    case UPDATE_TYPE_PALETTE:
879
46
    {
880
46
      PALETTE_UPDATE* palette_update = update_read_palette(update, s);
881
882
46
      if (!palette_update)
883
13
      {
884
13
        WLog_ERR(TAG, "UPDATE_TYPE_PALETTE - update_read_palette() failed");
885
13
        goto fail;
886
13
      }
887
888
33
      rc = IFCALLRESULT(FALSE, update->Palette, context, palette_update);
889
33
      free_palette_update(context, palette_update);
890
33
    }
891
0
    break;
892
893
2
    case UPDATE_TYPE_SYNCHRONIZE:
894
2
      if (!update_read_synchronize(update, s))
895
1
        goto fail;
896
1
      rc = IFCALLRESULT(TRUE, update->Synchronize, context);
897
1
      break;
898
899
39
    default:
900
39
      break;
901
567
  }
902
903
567
fail:
904
905
567
  if (!update_end_paint(update))
906
0
    rc = FALSE;
907
908
567
  if (!rc)
909
554
  {
910
554
    WLog_ERR(TAG, "UPDATE_TYPE %s [%" PRIu16 "] failed", update_type_to_string(updateType),
911
554
             updateType);
912
554
    return FALSE;
913
554
  }
914
915
13
  return TRUE;
916
567
}
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.90k
{
1017
9.90k
  wStream* s = NULL;
1018
9.90k
  WINPR_ASSERT(context);
1019
9.90k
  rdp_update_internal* update = update_cast(context->update);
1020
1021
9.90k
  if (update->us)
1022
0
  {
1023
0
    if (!update_end_paint(&update->common))
1024
0
      return FALSE;
1025
0
  }
1026
1027
9.90k
  WINPR_ASSERT(context->rdp);
1028
9.90k
  s = fastpath_update_pdu_init_new(context->rdp->fastpath);
1029
1030
9.90k
  if (!s)
1031
0
    return FALSE;
1032
1033
9.90k
  Stream_SealLength(s);
1034
9.90k
  Stream_GetLength(s, update->offsetOrders);
1035
9.90k
  Stream_Seek(s, 2); /* numberOrders (2 bytes) */
1036
9.90k
  update->combineUpdates = TRUE;
1037
9.90k
  update->numberOrders = 0;
1038
9.90k
  update->us = s;
1039
9.90k
  return TRUE;
1040
9.90k
}
1041
1042
static BOOL s_update_end_paint(rdpContext* context)
1043
9.29k
{
1044
9.29k
  wStream* s = NULL;
1045
9.29k
  WINPR_ASSERT(context);
1046
9.29k
  rdp_update_internal* update = update_cast(context->update);
1047
1048
9.29k
  if (!update->us)
1049
0
    return FALSE;
1050
1051
9.29k
  s = update->us;
1052
9.29k
  Stream_SealLength(s);
1053
9.29k
  Stream_SetPosition(s, update->offsetOrders);
1054
9.29k
  Stream_Write_UINT16(s, update->numberOrders); /* numberOrders (2 bytes) */
1055
9.29k
  Stream_SetPosition(s, Stream_Length(s));
1056
1057
9.29k
  if (update->numberOrders > 0)
1058
1.38k
  {
1059
1.38k
    WLog_DBG(TAG, "sending %" PRIu16 " orders", update->numberOrders);
1060
1.38k
    fastpath_send_update_pdu(context->rdp->fastpath, FASTPATH_UPDATETYPE_ORDERS, s, FALSE);
1061
1.38k
  }
1062
1063
9.29k
  update->combineUpdates = FALSE;
1064
9.29k
  update->numberOrders = 0;
1065
9.29k
  update->offsetOrders = 0;
1066
9.29k
  update->us = NULL;
1067
9.29k
  Stream_Free(s, TRUE);
1068
9.29k
  return TRUE;
1069
9.29k
}
1070
1071
static BOOL update_flush(rdpContext* context)
1072
789
{
1073
789
  rdp_update_internal* update = NULL;
1074
1075
789
  WINPR_ASSERT(context);
1076
789
  update = update_cast(context->update);
1077
1078
789
  if (update->numberOrders > 0)
1079
560
  {
1080
560
    if (!update_end_paint(&update->common))
1081
0
      return FALSE;
1082
1083
560
    if (!update_begin_paint(&update->common))
1084
0
      return FALSE;
1085
560
  }
1086
789
  return TRUE;
1087
789
}
1088
1089
static BOOL update_force_flush(rdpContext* context)
1090
231
{
1091
231
  return update_flush(context);
1092
231
}
1093
1094
static BOOL update_check_flush(rdpContext* context, size_t size)
1095
802k
{
1096
802k
  WINPR_ASSERT(context);
1097
802k
  rdp_update_internal* update = update_cast(context->update);
1098
1099
802k
  wStream* s = update->us;
1100
1101
802k
  if (!s)
1102
612
  {
1103
612
    if (!update_begin_paint(&update->common))
1104
0
      return FALSE;
1105
612
    s = update->us;
1106
612
  }
1107
1108
802k
  if (Stream_GetPosition(s) + size + 64 >= FASTPATH_MAX_PACKET_SIZE)
1109
558
  {
1110
    // Too big for the current packet. Flush first
1111
558
    if (!update_flush(context))
1112
0
      return FALSE;
1113
558
  }
1114
1115
802k
  return TRUE;
1116
802k
}
1117
1118
static BOOL update_set_bounds(rdpContext* context, const rdpBounds* bounds)
1119
793k
{
1120
793k
  rdp_update_internal* update = NULL;
1121
1122
793k
  WINPR_ASSERT(context);
1123
1124
793k
  update = update_cast(context->update);
1125
1126
793k
  CopyMemory(&update->previousBounds, &update->currentBounds, sizeof(rdpBounds));
1127
1128
793k
  if (!bounds)
1129
396k
    ZeroMemory(&update->currentBounds, sizeof(rdpBounds));
1130
396k
  else
1131
396k
    CopyMemory(&update->currentBounds, bounds, sizeof(rdpBounds));
1132
1133
793k
  return TRUE;
1134
793k
}
1135
1136
static BOOL update_bounds_is_null(rdpBounds* bounds)
1137
802k
{
1138
802k
  WINPR_ASSERT(bounds);
1139
802k
  if ((bounds->left == 0) && (bounds->top == 0) && (bounds->right == 0) && (bounds->bottom == 0))
1140
507k
    return TRUE;
1141
1142
294k
  return FALSE;
1143
802k
}
1144
1145
static BOOL update_bounds_equals(rdpBounds* bounds1, rdpBounds* bounds2)
1146
294k
{
1147
294k
  WINPR_ASSERT(bounds1);
1148
294k
  WINPR_ASSERT(bounds2);
1149
1150
294k
  if ((bounds1->left == bounds2->left) && (bounds1->top == bounds2->top) &&
1151
74.9k
      (bounds1->right == bounds2->right) && (bounds1->bottom == bounds2->bottom))
1152
1
    return TRUE;
1153
1154
294k
  return FALSE;
1155
294k
}
1156
1157
static size_t update_prepare_bounds(rdpContext* context, ORDER_INFO* orderInfo)
1158
802k
{
1159
802k
  size_t length = 0;
1160
802k
  rdp_update_internal* update = NULL;
1161
1162
802k
  WINPR_ASSERT(context);
1163
802k
  WINPR_ASSERT(orderInfo);
1164
1165
802k
  update = update_cast(context->update);
1166
1167
802k
  orderInfo->boundsFlags = 0;
1168
1169
802k
  if (update_bounds_is_null(&update->currentBounds))
1170
507k
    return 0;
1171
1172
294k
  orderInfo->controlFlags |= ORDER_BOUNDS;
1173
1174
294k
  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
294k
  else
1180
294k
  {
1181
294k
    length += 1;
1182
1183
294k
    if (update->previousBounds.left != update->currentBounds.left)
1184
186k
    {
1185
186k
      orderInfo->bounds.left = update->currentBounds.left;
1186
186k
      orderInfo->boundsFlags |= BOUND_LEFT;
1187
186k
      length += 2;
1188
186k
    }
1189
1190
294k
    if (update->previousBounds.top != update->currentBounds.top)
1191
174k
    {
1192
174k
      orderInfo->bounds.top = update->currentBounds.top;
1193
174k
      orderInfo->boundsFlags |= BOUND_TOP;
1194
174k
      length += 2;
1195
174k
    }
1196
1197
294k
    if (update->previousBounds.right != update->currentBounds.right)
1198
201k
    {
1199
201k
      orderInfo->bounds.right = update->currentBounds.right;
1200
201k
      orderInfo->boundsFlags |= BOUND_RIGHT;
1201
201k
      length += 2;
1202
201k
    }
1203
1204
294k
    if (update->previousBounds.bottom != update->currentBounds.bottom)
1205
184k
    {
1206
184k
      orderInfo->bounds.bottom = update->currentBounds.bottom;
1207
184k
      orderInfo->boundsFlags |= BOUND_BOTTOM;
1208
184k
      length += 2;
1209
184k
    }
1210
294k
  }
1211
1212
294k
  return length;
1213
294k
}
1214
1215
static size_t update_prepare_order_info(rdpContext* context, ORDER_INFO* orderInfo,
1216
                                        UINT32 orderType)
1217
802k
{
1218
802k
  WINPR_ASSERT(context);
1219
802k
  WINPR_ASSERT(orderInfo);
1220
1221
802k
  orderInfo->fieldFlags = 0;
1222
802k
  orderInfo->orderType = orderType;
1223
802k
  orderInfo->controlFlags = ORDER_STANDARD;
1224
802k
  orderInfo->controlFlags |= ORDER_TYPE_CHANGE;
1225
802k
  size_t length = 2;
1226
802k
  length += get_primary_drawing_order_field_bytes(orderInfo->orderType, NULL);
1227
802k
  length += update_prepare_bounds(context, orderInfo);
1228
802k
  return length;
1229
802k
}
1230
1231
static int update_write_order_info(rdpContext* context, wStream* s, const ORDER_INFO* orderInfo,
1232
                                   size_t offset)
1233
802k
{
1234
802k
  WINPR_UNUSED(context);
1235
802k
  WINPR_ASSERT(orderInfo);
1236
802k
  WINPR_ASSERT(orderInfo->controlFlags <= UINT8_MAX);
1237
1238
802k
  const size_t position = Stream_GetPosition(s);
1239
802k
  const UINT8 controlFlags = (UINT8)orderInfo->controlFlags;
1240
1241
802k
  Stream_SetPosition(s, offset);
1242
802k
  Stream_Write_UINT8(s, controlFlags); /* controlFlags (1 byte) */
1243
1244
802k
  if (orderInfo->controlFlags & ORDER_TYPE_CHANGE)
1245
802k
    Stream_Write_UINT8(
1246
802k
        s, WINPR_ASSERTING_INT_CAST(uint8_t, orderInfo->orderType)); /* orderType (1 byte) */
1247
1248
802k
  if (!update_write_field_flags(
1249
802k
          s, orderInfo->fieldFlags, controlFlags,
1250
802k
          get_primary_drawing_order_field_bytes(orderInfo->orderType, NULL)))
1251
0
    return -1;
1252
802k
  if (!update_write_bounds(s, orderInfo))
1253
177k
    return -1;
1254
624k
  Stream_SetPosition(s, position);
1255
624k
  return 0;
1256
802k
}
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
77
{
1367
77
  wStream* s = NULL;
1368
77
  WINPR_ASSERT(context);
1369
77
  rdpRdp* rdp = context->rdp;
1370
77
  BOOL ret = FALSE;
1371
1372
77
  WINPR_ASSERT(surfaceBitsCommand);
1373
77
  WINPR_ASSERT(rdp);
1374
1375
77
  if (!update_force_flush(context))
1376
0
    return FALSE;
1377
77
  s = fastpath_update_pdu_init(rdp->fastpath);
1378
1379
77
  if (!s)
1380
0
    return FALSE;
1381
1382
77
  if (!update_write_surfcmd_surface_bits(s, surfaceBitsCommand))
1383
0
    goto out_fail;
1384
1385
77
  if (!fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s,
1386
77
                                surfaceBitsCommand->skipCompression))
1387
77
    goto out_fail;
1388
1389
0
  ret = update_force_flush(context);
1390
77
out_fail:
1391
77
  Stream_Release(s);
1392
77
  return ret;
1393
0
}
1394
1395
static BOOL update_send_surface_frame_marker(rdpContext* context,
1396
                                             const SURFACE_FRAME_MARKER* surfaceFrameMarker)
1397
48
{
1398
48
  wStream* s = NULL;
1399
48
  WINPR_ASSERT(context);
1400
48
  rdpRdp* rdp = context->rdp;
1401
48
  BOOL ret = FALSE;
1402
48
  if (!update_force_flush(context))
1403
0
    return FALSE;
1404
1405
48
  WINPR_ASSERT(rdp);
1406
48
  s = fastpath_update_pdu_init(rdp->fastpath);
1407
1408
48
  if (!s)
1409
0
    return FALSE;
1410
1411
48
  WINPR_ASSERT(surfaceFrameMarker->frameAction <= UINT16_MAX);
1412
48
  if (!update_write_surfcmd_frame_marker(s, (UINT16)surfaceFrameMarker->frameAction,
1413
48
                                         surfaceFrameMarker->frameId) ||
1414
48
      !fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SURFCMDS, s, FALSE))
1415
48
    goto out_fail;
1416
1417
0
  ret = update_force_flush(context);
1418
48
out_fail:
1419
48
  Stream_Release(s);
1420
48
  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.87k
{
1494
1.87k
  wStream* s = NULL;
1495
1.87k
  WINPR_ASSERT(context);
1496
1.87k
  rdpRdp* rdp = context->rdp;
1497
1.87k
  BOOL ret = 0;
1498
1499
1.87k
  WINPR_ASSERT(rdp);
1500
1.87k
  s = fastpath_update_pdu_init(rdp->fastpath);
1501
1502
1.87k
  if (!s)
1503
0
    return FALSE;
1504
1505
1.87k
  Stream_Zero(s, 2); /* pad2Octets (2 bytes) */
1506
1.87k
  ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_SYNCHRONIZE, s, FALSE);
1507
1.87k
  Stream_Release(s);
1508
1.87k
  return ret;
1509
1.87k
}
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
106
{
1519
106
  wStream* s = NULL;
1520
106
  WINPR_ASSERT(context);
1521
106
  rdpRdp* rdp = context->rdp;
1522
106
  rdpUpdate* update = context->update;
1523
106
  BOOL ret = TRUE;
1524
1525
106
  if (!update_force_flush(context))
1526
0
    return FALSE;
1527
1528
106
  WINPR_ASSERT(rdp);
1529
106
  s = fastpath_update_pdu_init(rdp->fastpath);
1530
1531
106
  if (!s)
1532
0
    return FALSE;
1533
1534
106
  if (!update_write_bitmap_update(update, s, bitmapUpdate) ||
1535
106
      !fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_BITMAP, s,
1536
106
                                bitmapUpdate->skipCompression))
1537
106
  {
1538
106
    ret = FALSE;
1539
106
    goto out_fail;
1540
106
  }
1541
1542
0
  ret = update_force_flush(context);
1543
1544
106
out_fail:
1545
106
  Stream_Release(s);
1546
106
  return ret;
1547
0
}
1548
1549
static BOOL update_send_play_sound(rdpContext* context, const PLAY_SOUND_UPDATE* play_sound)
1550
4.85k
{
1551
4.85k
  UINT16 sec_flags = 0;
1552
4.85k
  wStream* s = NULL;
1553
4.85k
  WINPR_ASSERT(context);
1554
4.85k
  rdpRdp* rdp = context->rdp;
1555
1556
4.85k
  WINPR_ASSERT(rdp);
1557
4.85k
  WINPR_ASSERT(rdp->settings);
1558
4.85k
  WINPR_ASSERT(play_sound);
1559
4.85k
  WINPR_ASSERT(rdp->settings->ReceivedCapabilities);
1560
4.85k
  WINPR_ASSERT(rdp->settings->ReceivedCapabilitiesSize > CAPSET_TYPE_SOUND);
1561
4.85k
  if (!rdp->settings->ReceivedCapabilities[CAPSET_TYPE_SOUND])
1562
4.85k
  {
1563
4.85k
    return TRUE;
1564
4.85k
  }
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
494k
{
1582
494k
  ORDER_INFO orderInfo = { 0 };
1583
1584
494k
  WINPR_ASSERT(context);
1585
494k
  WINPR_ASSERT(dstblt);
1586
1587
494k
  rdp_update_internal* update = update_cast(context->update);
1588
1589
494k
  const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_DSTBLT);
1590
494k
  const size_t inf = update_approximate_dstblt_order(&orderInfo, dstblt);
1591
494k
  if (!update_check_flush(context, headerLength + inf))
1592
0
    return FALSE;
1593
1594
494k
  wStream* s = update->us;
1595
1596
494k
  if (!s)
1597
0
    return FALSE;
1598
1599
494k
  const size_t offset = Stream_GetPosition(s);
1600
1601
494k
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
1602
0
    return FALSE;
1603
1604
494k
  Stream_Seek(s, headerLength);
1605
1606
494k
  if (!update_write_dstblt_order(s, &orderInfo, dstblt))
1607
48
    return FALSE;
1608
1609
494k
  update_write_order_info(context, s, &orderInfo, offset);
1610
494k
  update->numberOrders++;
1611
494k
  return TRUE;
1612
494k
}
1613
1614
static BOOL update_send_patblt(rdpContext* context, PATBLT_ORDER* patblt)
1615
102k
{
1616
102k
  size_t offset = 0;
1617
102k
  ORDER_INFO orderInfo = { 0 };
1618
1619
102k
  WINPR_ASSERT(context);
1620
102k
  WINPR_ASSERT(patblt);
1621
102k
  rdp_update_internal* update = update_cast(context->update);
1622
1623
102k
  const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_PATBLT);
1624
102k
  if (!update_check_flush(context,
1625
102k
                          headerLength + update_approximate_patblt_order(&orderInfo, patblt)))
1626
0
    return FALSE;
1627
1628
102k
  wStream* s = update->us;
1629
1630
102k
  if (!s)
1631
0
    return FALSE;
1632
1633
102k
  offset = Stream_GetPosition(s);
1634
1635
102k
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
1636
0
    return FALSE;
1637
1638
102k
  Stream_Seek(s, headerLength);
1639
102k
  update_write_patblt_order(s, &orderInfo, patblt);
1640
102k
  update_write_order_info(context, s, &orderInfo, offset);
1641
102k
  update->numberOrders++;
1642
102k
  return TRUE;
1643
102k
}
1644
1645
static BOOL update_send_scrblt(rdpContext* context, const SCRBLT_ORDER* scrblt)
1646
104k
{
1647
104k
  ORDER_INFO orderInfo = { 0 };
1648
1649
104k
  WINPR_ASSERT(context);
1650
104k
  WINPR_ASSERT(scrblt);
1651
104k
  rdp_update_internal* update = update_cast(context->update);
1652
1653
104k
  const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_SCRBLT);
1654
104k
  const size_t inf = update_approximate_scrblt_order(&orderInfo, scrblt);
1655
104k
  if (!update_check_flush(context, headerLength + inf))
1656
0
    return FALSE;
1657
1658
104k
  wStream* s = update->us;
1659
1660
104k
  if (!s)
1661
0
    return TRUE;
1662
1663
104k
  const size_t offset = Stream_GetPosition(s);
1664
1665
104k
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
1666
0
    return FALSE;
1667
1668
104k
  Stream_Seek(s, headerLength);
1669
104k
  update_write_scrblt_order(s, &orderInfo, scrblt);
1670
104k
  update_write_order_info(context, s, &orderInfo, offset);
1671
104k
  update->numberOrders++;
1672
104k
  return TRUE;
1673
104k
}
1674
1675
static BOOL update_send_opaque_rect(rdpContext* context, const OPAQUE_RECT_ORDER* opaque_rect)
1676
43.5k
{
1677
43.5k
  size_t offset = 0;
1678
43.5k
  ORDER_INFO orderInfo = { 0 };
1679
1680
43.5k
  WINPR_ASSERT(context);
1681
43.5k
  WINPR_ASSERT(opaque_rect);
1682
43.5k
  rdp_update_internal* update = update_cast(context->update);
1683
1684
43.5k
  const size_t headerLength =
1685
43.5k
      update_prepare_order_info(context, &orderInfo, ORDER_TYPE_OPAQUE_RECT);
1686
43.5k
  if (!update_check_flush(
1687
43.5k
          context, headerLength + update_approximate_opaque_rect_order(&orderInfo, opaque_rect)))
1688
0
    return FALSE;
1689
1690
43.5k
  wStream* s = update->us;
1691
1692
43.5k
  if (!s)
1693
0
    return FALSE;
1694
1695
43.5k
  offset = Stream_GetPosition(s);
1696
1697
43.5k
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
1698
0
    return FALSE;
1699
1700
43.5k
  Stream_Seek(s, headerLength);
1701
43.5k
  update_write_opaque_rect_order(s, &orderInfo, opaque_rect);
1702
43.5k
  update_write_order_info(context, s, &orderInfo, offset);
1703
43.5k
  update->numberOrders++;
1704
43.5k
  return TRUE;
1705
43.5k
}
1706
1707
static BOOL update_send_line_to(rdpContext* context, const LINE_TO_ORDER* line_to)
1708
58.5k
{
1709
58.5k
  ORDER_INFO orderInfo = { 0 };
1710
1711
58.5k
  WINPR_ASSERT(context);
1712
58.5k
  WINPR_ASSERT(line_to);
1713
58.5k
  rdp_update_internal* update = update_cast(context->update);
1714
58.5k
  const size_t headerLength = update_prepare_order_info(context, &orderInfo, ORDER_TYPE_LINE_TO);
1715
58.5k
  const size_t inf = update_approximate_line_to_order(&orderInfo, line_to);
1716
58.5k
  if (!update_check_flush(context, headerLength + inf))
1717
0
    return FALSE;
1718
1719
58.5k
  wStream* s = update->us;
1720
1721
58.5k
  if (!s)
1722
0
    return FALSE;
1723
1724
58.5k
  const size_t offset = Stream_GetPosition(s);
1725
1726
58.5k
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
1727
0
    return FALSE;
1728
1729
58.5k
  Stream_Seek(s, headerLength);
1730
58.5k
  update_write_line_to_order(s, &orderInfo, line_to);
1731
58.5k
  update_write_order_info(context, s, &orderInfo, offset);
1732
58.5k
  update->numberOrders++;
1733
58.5k
  return TRUE;
1734
58.5k
}
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
356
{
2072
356
  UINT16 flags = 0;
2073
356
  const size_t headerLength = 6;
2074
2075
356
  WINPR_ASSERT(context);
2076
356
  WINPR_ASSERT(cache_brush);
2077
356
  rdp_update_internal* update = update_cast(context->update);
2078
2079
356
  const size_t inf = update_approximate_cache_brush_order(cache_brush, &flags);
2080
356
  if (!update_check_flush(context, headerLength + inf))
2081
0
    return FALSE;
2082
2083
356
  wStream* s = update->us;
2084
2085
356
  if (!s)
2086
0
    return FALSE;
2087
2088
356
  const size_t bm = Stream_GetPosition(s);
2089
2090
356
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
2091
0
    return FALSE;
2092
2093
356
  Stream_Seek(s, headerLength);
2094
2095
356
  if (!update_write_cache_brush_order(s, cache_brush, &flags))
2096
13
    return FALSE;
2097
2098
343
  const size_t em = Stream_GetPosition(s);
2099
343
  if (em <= bm + 13)
2100
44
    return FALSE;
2101
2102
299
  const size_t orderLength = (em - bm) - 13;
2103
299
  WINPR_ASSERT(orderLength <= UINT16_MAX);
2104
299
  Stream_SetPosition(s, bm);
2105
299
  Stream_Write_UINT8(s, ORDER_STANDARD | ORDER_SECONDARY); /* controlFlags (1 byte) */
2106
299
  Stream_Write_UINT16(s, (UINT16)orderLength);             /* orderLength (2 bytes) */
2107
299
  Stream_Write_UINT16(s, flags);                           /* extraFlags (2 bytes) */
2108
299
  Stream_Write_UINT8(s, ORDER_TYPE_CACHE_BRUSH);           /* orderType (1 byte) */
2109
299
  Stream_SetPosition(s, em);
2110
299
  update->numberOrders++;
2111
299
  return TRUE;
2112
299
}
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
{
2121
  WINPR_ASSERT(context);
2122
  WINPR_ASSERT(create_offscreen_bitmap);
2123
  rdp_update_internal* update = update_cast(context->update);
2124
2125
  const size_t headerLength = 1;
2126
  const size_t orderType = ORDER_TYPE_CREATE_OFFSCREEN_BITMAP;
2127
  const size_t controlFlags = ORDER_SECONDARY | (orderType << 2);
2128
  const size_t inf = update_approximate_create_offscreen_bitmap_order(create_offscreen_bitmap);
2129
  if (!update_check_flush(context, headerLength + inf))
2130
    return FALSE;
2131
2132
  wStream* s = update->us;
2133
2134
  if (!s)
2135
    return FALSE;
2136
2137
  const size_t bm = Stream_GetPosition(s);
2138
2139
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
2140
    return FALSE;
2141
2142
  Stream_Seek(s, headerLength);
2143
2144
  if (!update_write_create_offscreen_bitmap_order(s, create_offscreen_bitmap))
2145
    return FALSE;
2146
2147
  const size_t em = Stream_GetPosition(s);
2148
  Stream_SetPosition(s, bm);
2149
  Stream_Write_UINT8(s,
2150
                     WINPR_ASSERTING_INT_CAST(uint8_t, controlFlags)); /* controlFlags (1 byte) */
2151
  Stream_SetPosition(s, em);
2152
  update->numberOrders++;
2153
  return TRUE;
2154
}
2155
2156
static BOOL update_send_switch_surface_order(rdpContext* context,
2157
                                             const SWITCH_SURFACE_ORDER* switch_surface)
2158
{
2159
  WINPR_ASSERT(context);
2160
  WINPR_ASSERT(switch_surface);
2161
  rdp_update_internal* update = update_cast(context->update);
2162
2163
  const size_t headerLength = 1;
2164
  const size_t orderType = ORDER_TYPE_SWITCH_SURFACE;
2165
  const size_t controlFlags = ORDER_SECONDARY | (orderType << 2);
2166
  const size_t inf = update_approximate_switch_surface_order(switch_surface);
2167
  if (!update_check_flush(context, headerLength + inf))
2168
    return FALSE;
2169
2170
  wStream* s = update->us;
2171
2172
  if (!s)
2173
    return FALSE;
2174
2175
  const size_t bm = Stream_GetPosition(s);
2176
2177
  if (!Stream_EnsureRemainingCapacity(s, headerLength))
2178
    return FALSE;
2179
2180
  Stream_Seek(s, headerLength);
2181
2182
  if (!update_write_switch_surface_order(s, switch_surface))
2183
    return FALSE;
2184
2185
  const size_t em = Stream_GetPosition(s);
2186
  Stream_SetPosition(s, bm);
2187
  Stream_Write_UINT8(s,
2188
                     WINPR_ASSERTING_INT_CAST(uint8_t, controlFlags)); /* controlFlags (1 byte) */
2189
  Stream_SetPosition(s, em);
2190
  update->numberOrders++;
2191
  return TRUE;
2192
}
2193
2194
static BOOL update_send_pointer_system(rdpContext* context,
2195
                                       const POINTER_SYSTEM_UPDATE* pointer_system)
2196
102
{
2197
102
  wStream* s = NULL;
2198
102
  BYTE updateCode = 0;
2199
2200
102
  WINPR_ASSERT(context);
2201
102
  rdpRdp* rdp = context->rdp;
2202
102
  BOOL ret = 0;
2203
2204
102
  WINPR_ASSERT(rdp);
2205
102
  s = fastpath_update_pdu_init(rdp->fastpath);
2206
2207
102
  if (!s)
2208
0
    return FALSE;
2209
2210
102
  if (pointer_system->type == SYSPTR_NULL)
2211
28
    updateCode = FASTPATH_UPDATETYPE_PTR_NULL;
2212
74
  else
2213
74
    updateCode = FASTPATH_UPDATETYPE_PTR_DEFAULT;
2214
2215
102
  ret = fastpath_send_update_pdu(rdp->fastpath, updateCode, s, FALSE);
2216
102
  Stream_Release(s);
2217
102
  return ret;
2218
102
}
2219
2220
static BOOL update_send_pointer_position(rdpContext* context,
2221
                                         const POINTER_POSITION_UPDATE* pointerPosition)
2222
70
{
2223
70
  wStream* s = NULL;
2224
70
  WINPR_ASSERT(context);
2225
70
  rdpRdp* rdp = context->rdp;
2226
70
  BOOL ret = FALSE;
2227
2228
70
  WINPR_ASSERT(rdp);
2229
70
  s = fastpath_update_pdu_init(rdp->fastpath);
2230
2231
70
  if (!s)
2232
0
    return FALSE;
2233
2234
70
  if (!Stream_EnsureRemainingCapacity(s, 16))
2235
0
    goto out_fail;
2236
2237
70
  Stream_Write_UINT16(
2238
70
      s, WINPR_ASSERTING_INT_CAST(uint16_t, pointerPosition->xPos)); /* xPos (2 bytes) */
2239
70
  Stream_Write_UINT16(
2240
70
      s, WINPR_ASSERTING_INT_CAST(uint16_t, pointerPosition->yPos)); /* yPos (2 bytes) */
2241
70
  ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_PTR_POSITION, s, FALSE);
2242
70
out_fail:
2243
70
  Stream_Release(s);
2244
70
  return ret;
2245
70
}
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
8
    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
90
{
2275
90
  wStream* s = NULL;
2276
2277
90
  WINPR_ASSERT(context);
2278
90
  rdpRdp* rdp = context->rdp;
2279
90
  BOOL ret = FALSE;
2280
2281
90
  WINPR_ASSERT(rdp);
2282
90
  WINPR_ASSERT(pointer_color);
2283
90
  s = fastpath_update_pdu_init(rdp->fastpath);
2284
2285
90
  if (!s)
2286
0
    return FALSE;
2287
2288
90
  if (!update_write_pointer_color(s, pointer_color))
2289
0
    goto out_fail;
2290
2291
90
  ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_COLOR, s, FALSE);
2292
90
out_fail:
2293
90
  Stream_Release(s);
2294
90
  return ret;
2295
90
}
2296
2297
static BOOL update_write_pointer_large(wStream* s, const POINTER_LARGE_UPDATE* pointer)
2298
130
{
2299
130
  WINPR_ASSERT(pointer);
2300
2301
130
  if (!Stream_EnsureRemainingCapacity(s, 32 + pointer->lengthAndMask + pointer->lengthXorMask))
2302
0
    return FALSE;
2303
2304
130
  Stream_Write_UINT16(s, pointer->xorBpp);
2305
130
  Stream_Write_UINT16(s, pointer->cacheIndex);
2306
130
  Stream_Write_UINT16(s, pointer->hotSpotX);
2307
130
  Stream_Write_UINT16(s, pointer->hotSpotY);
2308
130
  Stream_Write_UINT16(s, pointer->width);
2309
130
  Stream_Write_UINT16(s, pointer->height);
2310
130
  Stream_Write_UINT32(s, pointer->lengthAndMask);
2311
130
  Stream_Write_UINT32(s, pointer->lengthXorMask);
2312
130
  Stream_Write(s, pointer->xorMaskData, pointer->lengthXorMask);
2313
130
  Stream_Write(s, pointer->andMaskData, pointer->lengthAndMask);
2314
130
  Stream_Write_UINT8(s, 0); /* pad (1 byte) */
2315
130
  return TRUE;
2316
130
}
2317
2318
static BOOL update_send_pointer_large(rdpContext* context, const POINTER_LARGE_UPDATE* pointer)
2319
130
{
2320
130
  wStream* s = NULL;
2321
130
  WINPR_ASSERT(context);
2322
130
  rdpRdp* rdp = context->rdp;
2323
130
  BOOL ret = FALSE;
2324
2325
130
  WINPR_ASSERT(rdp);
2326
130
  WINPR_ASSERT(pointer);
2327
130
  s = fastpath_update_pdu_init(rdp->fastpath);
2328
2329
130
  if (!s)
2330
0
    return FALSE;
2331
2332
130
  if (!update_write_pointer_large(s, pointer))
2333
0
    goto out_fail;
2334
2335
130
  ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_LARGE_POINTER, s, FALSE);
2336
130
out_fail:
2337
130
  Stream_Release(s);
2338
130
  return ret;
2339
130
}
2340
2341
static BOOL update_send_pointer_new(rdpContext* context, const POINTER_NEW_UPDATE* pointer_new)
2342
13
{
2343
13
  wStream* s = NULL;
2344
2345
13
  WINPR_ASSERT(context);
2346
13
  rdpRdp* rdp = context->rdp;
2347
13
  BOOL ret = FALSE;
2348
2349
13
  WINPR_ASSERT(rdp);
2350
13
  WINPR_ASSERT(pointer_new);
2351
13
  s = fastpath_update_pdu_init(rdp->fastpath);
2352
2353
13
  if (!s)
2354
0
    return FALSE;
2355
2356
13
  if (!Stream_EnsureRemainingCapacity(s, 16))
2357
0
    goto out_fail;
2358
2359
13
  Stream_Write_UINT16(
2360
13
      s, WINPR_ASSERTING_INT_CAST(uint16_t, pointer_new->xorBpp)); /* xorBpp (2 bytes) */
2361
13
  update_write_pointer_color(s, &pointer_new->colorPtrAttr);
2362
13
  ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_POINTER, s, FALSE);
2363
13
out_fail:
2364
13
  Stream_Release(s);
2365
13
  return ret;
2366
13
}
2367
2368
static BOOL update_send_pointer_cached(rdpContext* context,
2369
                                       const POINTER_CACHED_UPDATE* pointer_cached)
2370
42
{
2371
42
  wStream* s = NULL;
2372
2373
42
  WINPR_ASSERT(context);
2374
42
  rdpRdp* rdp = context->rdp;
2375
42
  BOOL ret = 0;
2376
2377
42
  WINPR_ASSERT(rdp);
2378
42
  WINPR_ASSERT(pointer_cached);
2379
42
  s = fastpath_update_pdu_init(rdp->fastpath);
2380
2381
42
  if (!s)
2382
0
    return FALSE;
2383
2384
42
  Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(
2385
42
                             uint16_t, pointer_cached->cacheIndex)); /* cacheIndex (2 bytes) */
2386
42
  ret = fastpath_send_update_pdu(rdp->fastpath, FASTPATH_UPDATETYPE_CACHED, s, FALSE);
2387
42
  Stream_Release(s);
2388
42
  return ret;
2389
42
}
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.73k
{
3229
8.73k
  WINPR_ASSERT(update);
3230
3231
8.73k
  update->BeginPaint = s_update_begin_paint;
3232
8.73k
  update->EndPaint = s_update_end_paint;
3233
8.73k
  update->SetBounds = update_set_bounds;
3234
8.73k
  update->Synchronize = update_send_synchronize;
3235
8.73k
  update->DesktopResize = update_send_desktop_resize;
3236
8.73k
  update->BitmapUpdate = update_send_bitmap_update;
3237
8.73k
  update->SurfaceBits = update_send_surface_bits;
3238
8.73k
  update->SurfaceFrameMarker = update_send_surface_frame_marker;
3239
8.73k
  update->SurfaceCommand = update_send_surface_command;
3240
8.73k
  update->SurfaceFrameBits = update_send_surface_frame_bits;
3241
8.73k
  update->PlaySound = update_send_play_sound;
3242
8.73k
  update->SetKeyboardIndicators = update_send_set_keyboard_indicators;
3243
8.73k
  update->SetKeyboardImeStatus = update_send_set_keyboard_ime_status;
3244
8.73k
  update->SaveSessionInfo = rdp_send_save_session_info;
3245
8.73k
  update->ServerStatusInfo = rdp_send_server_status_info;
3246
8.73k
  update->primary->DstBlt = update_send_dstblt;
3247
8.73k
  update->primary->PatBlt = update_send_patblt;
3248
8.73k
  update->primary->ScrBlt = update_send_scrblt;
3249
8.73k
  update->primary->OpaqueRect = update_send_opaque_rect;
3250
8.73k
  update->primary->LineTo = update_send_line_to;
3251
8.73k
  update->primary->MemBlt = update_send_memblt;
3252
8.73k
  update->primary->GlyphIndex = update_send_glyph_index;
3253
8.73k
  update->secondary->CacheBitmap = update_send_cache_bitmap;
3254
8.73k
  update->secondary->CacheBitmapV2 = update_send_cache_bitmap_v2;
3255
8.73k
  update->secondary->CacheBitmapV3 = update_send_cache_bitmap_v3;
3256
8.73k
  update->secondary->CacheColorTable = update_send_cache_color_table;
3257
8.73k
  update->secondary->CacheGlyph = update_send_cache_glyph;
3258
8.73k
  update->secondary->CacheGlyphV2 = update_send_cache_glyph_v2;
3259
8.73k
  update->secondary->CacheBrush = update_send_cache_brush;
3260
8.73k
  update->altsec->CreateOffscreenBitmap = update_send_create_offscreen_bitmap_order;
3261
8.73k
  update->altsec->SwitchSurface = update_send_switch_surface_order;
3262
8.73k
  update->pointer->PointerSystem = update_send_pointer_system;
3263
8.73k
  update->pointer->PointerPosition = update_send_pointer_position;
3264
8.73k
  update->pointer->PointerColor = update_send_pointer_color;
3265
8.73k
  update->pointer->PointerLarge = update_send_pointer_large;
3266
8.73k
  update->pointer->PointerNew = update_send_pointer_new;
3267
8.73k
  update->pointer->PointerCached = update_send_pointer_cached;
3268
8.73k
  update->window->WindowCreate = update_send_window_create;
3269
8.73k
  update->window->WindowUpdate = update_send_window_update;
3270
8.73k
  update->window->WindowIcon = update_send_window_icon;
3271
8.73k
  update->window->WindowCachedIcon = update_send_window_cached_icon;
3272
8.73k
  update->window->WindowDelete = update_send_window_delete;
3273
8.73k
  update->window->NotifyIconCreate = update_send_notify_icon_create;
3274
8.73k
  update->window->NotifyIconUpdate = update_send_notify_icon_update;
3275
8.73k
  update->window->NotifyIconDelete = update_send_notify_icon_delete;
3276
8.73k
  update->window->MonitoredDesktop = update_send_monitored_desktop;
3277
8.73k
  update->window->NonMonitoredDesktop = update_send_non_monitored_desktop;
3278
8.73k
}
3279
3280
void update_register_client_callbacks(rdpUpdate* update)
3281
9.60k
{
3282
9.60k
  WINPR_ASSERT(update);
3283
3284
9.60k
  update->RefreshRect = update_send_refresh_rect;
3285
9.60k
  update->SuppressOutput = update_send_suppress_output;
3286
9.60k
  update->SurfaceFrameAcknowledge = update_send_frame_acknowledge;
3287
9.60k
}
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
18.3k
{
3314
18.3k
  const wObject cb = { NULL, NULL, NULL, update_free_queued_message, NULL };
3315
3316
18.3k
  WINPR_ASSERT(rdp);
3317
18.3k
  WINPR_ASSERT(rdp->context);
3318
3319
18.3k
  rdp_update_internal* update = (rdp_update_internal*)calloc(1, sizeof(rdp_update_internal));
3320
3321
18.3k
  if (!update)
3322
0
    return NULL;
3323
3324
18.3k
  update->common.context = rdp->context;
3325
18.3k
  update->log = WLog_Get("com.freerdp.core.update");
3326
18.3k
  InitializeCriticalSection(&(update->mux));
3327
18.3k
  update->common.pointer = (rdpPointerUpdate*)calloc(1, sizeof(rdpPointerUpdate));
3328
3329
18.3k
  if (!update->common.pointer)
3330
0
    goto fail;
3331
3332
18.3k
  rdp_primary_update_internal* primary =
3333
18.3k
      (rdp_primary_update_internal*)calloc(1, sizeof(rdp_primary_update_internal));
3334
3335
18.3k
  if (!primary)
3336
0
    goto fail;
3337
18.3k
  update->common.primary = &primary->common;
3338
3339
18.3k
  rdp_secondary_update_internal* secondary =
3340
18.3k
      (rdp_secondary_update_internal*)calloc(1, sizeof(rdp_secondary_update_internal));
3341
3342
18.3k
  if (!secondary)
3343
0
    goto fail;
3344
18.3k
  update->common.secondary = &secondary->common;
3345
3346
18.3k
  rdp_altsec_update_internal* altsec =
3347
18.3k
      (rdp_altsec_update_internal*)calloc(1, sizeof(rdp_altsec_update_internal));
3348
3349
18.3k
  if (!altsec)
3350
0
    goto fail;
3351
3352
18.3k
  update->common.altsec = &altsec->common;
3353
18.3k
  update->common.window = (rdpWindowUpdate*)calloc(1, sizeof(rdpWindowUpdate));
3354
3355
18.3k
  if (!update->common.window)
3356
0
    goto fail;
3357
3358
18.3k
  OFFSCREEN_DELETE_LIST* deleteList = &(altsec->create_offscreen_bitmap.deleteList);
3359
18.3k
  deleteList->sIndices = 64;
3360
18.3k
  deleteList->indices = calloc(deleteList->sIndices, 2);
3361
3362
18.3k
  if (!deleteList->indices)
3363
0
    goto fail;
3364
3365
18.3k
  deleteList->cIndices = 0;
3366
18.3k
  update->common.SuppressOutput = update_send_suppress_output;
3367
18.3k
  update->initialState = TRUE;
3368
18.3k
  update->common.autoCalculateBitmapData = TRUE;
3369
18.3k
  update->queue = MessageQueue_New(&cb);
3370
3371
18.3k
  if (!update->queue)
3372
0
    goto fail;
3373
3374
18.3k
  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
18.3k
}
3382
3383
void update_free(rdpUpdate* update)
3384
18.3k
{
3385
18.3k
  if (update != NULL)
3386
18.3k
  {
3387
18.3k
    rdp_update_internal* up = update_cast(update);
3388
18.3k
    rdp_altsec_update_internal* altsec = altsec_update_cast(update->altsec);
3389
18.3k
    OFFSCREEN_DELETE_LIST* deleteList = &(altsec->create_offscreen_bitmap.deleteList);
3390
3391
18.3k
    if (deleteList)
3392
18.3k
      free(deleteList->indices);
3393
3394
18.3k
    free(update->pointer);
3395
3396
18.3k
    if (update->primary)
3397
18.3k
    {
3398
18.3k
      rdp_primary_update_internal* primary = primary_update_cast(update->primary);
3399
3400
18.3k
      free(primary->polygon_cb.points);
3401
18.3k
      free(primary->polyline.points);
3402
18.3k
      free(primary->polygon_sc.points);
3403
18.3k
      free(primary->fast_glyph.glyphData.aj);
3404
18.3k
      free(primary);
3405
18.3k
    }
3406
3407
18.3k
    free(update->secondary);
3408
18.3k
    free(altsec);
3409
3410
18.3k
    if (update->window)
3411
18.3k
      free(update->window);
3412
3413
18.3k
    MessageQueue_Free(up->queue);
3414
18.3k
    DeleteCriticalSection(&up->mux);
3415
3416
18.3k
    if (up->us)
3417
612
      Stream_Free(up->us, TRUE);
3418
18.3k
    free(update);
3419
18.3k
  }
3420
18.3k
}
3421
3422
void rdp_update_lock(rdpUpdate* update)
3423
20.0k
{
3424
20.0k
  rdp_update_internal* up = update_cast(update);
3425
20.0k
  EnterCriticalSection(&up->mux);
3426
20.0k
}
3427
3428
void rdp_update_unlock(rdpUpdate* update)
3429
19.4k
{
3430
19.4k
  rdp_update_internal* up = update_cast(update);
3431
19.4k
  LeaveCriticalSection(&up->mux);
3432
19.4k
}
3433
3434
BOOL update_begin_paint(rdpUpdate* update)
3435
20.0k
{
3436
20.0k
  rdp_update_internal* up = update_cast(update);
3437
20.0k
  WINPR_ASSERT(update);
3438
20.0k
  rdp_update_lock(update);
3439
3440
20.0k
  up->withinBeginEndPaint = TRUE;
3441
3442
20.0k
  WINPR_ASSERT(update->context);
3443
3444
20.0k
  BOOL rc = IFCALLRESULT(TRUE, update->BeginPaint, update->context);
3445
20.0k
  if (!rc)
3446
0
    WLog_WARN(TAG, "BeginPaint call failed");
3447
3448
  /* Reset the invalid regions, we start a new frame here. */
3449
20.0k
  rdpGdi* gdi = update->context->gdi;
3450
20.0k
  if (!gdi)
3451
20.0k
    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
19.4k
{
3468
19.4k
  BOOL rc = TRUE;
3469
3470
19.4k
  WINPR_ASSERT(update);
3471
19.4k
  IFCALLRET(update->EndPaint, rc, update->context);
3472
19.4k
  if (!rc)
3473
0
    WLog_WARN(TAG, "EndPaint call failed");
3474
3475
19.4k
  rdp_update_internal* up = update_cast(update);
3476
3477
19.4k
  if (!up->withinBeginEndPaint)
3478
0
    return rc;
3479
19.4k
  up->withinBeginEndPaint = FALSE;
3480
3481
19.4k
  rdp_update_unlock(update);
3482
19.4k
  return rc;
3483
19.4k
}