Coverage Report

Created: 2026-07-16 07:14

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