Coverage Report

Created: 2026-11-10 06:45

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