Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/cache/bitmap.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Bitmap Cache V2
4
 *
5
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <freerdp/config.h>
21
22
#include <stdio.h>
23
24
#include <winpr/crt.h>
25
#include <winpr/assert.h>
26
#include <winpr/cast.h>
27
28
#include <freerdp/freerdp.h>
29
#include <freerdp/constants.h>
30
#include <winpr/stream.h>
31
32
#include <freerdp/log.h>
33
#include <freerdp/gdi/bitmap.h>
34
35
#include "../gdi/gdi.h"
36
#include "../core/graphics.h"
37
38
#include "bitmap.h"
39
#include "cache.h"
40
41
#define TAG FREERDP_TAG("cache.bitmap")
42
43
static rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index);
44
static BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index,
45
                             rdpBitmap* bitmap);
46
47
static BOOL update_gdi_memblt(rdpContext* context, MEMBLT_ORDER* memblt)
48
0
{
49
0
  rdpBitmap* bitmap = nullptr;
50
0
  rdpCache* cache = nullptr;
51
52
0
  cache = context->cache;
53
54
0
  if (memblt->cacheId == 0xFF)
55
0
    bitmap = offscreen_cache_get(cache->offscreen, memblt->cacheIndex);
56
0
  else
57
0
    bitmap = bitmap_cache_get(cache->bitmap, (BYTE)memblt->cacheId, memblt->cacheIndex);
58
59
  /* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
60
0
  if (bitmap == nullptr)
61
0
    return TRUE;
62
63
0
  memblt->bitmap = bitmap;
64
0
  return IFCALLRESULT(TRUE, cache->bitmap->MemBlt, context, memblt);
65
0
}
66
67
static BOOL update_gdi_mem3blt(rdpContext* context, MEM3BLT_ORDER* mem3blt)
68
0
{
69
0
  rdpBitmap* bitmap = nullptr;
70
0
  rdpCache* cache = context->cache;
71
0
  rdpBrush* brush = &mem3blt->brush;
72
0
  BOOL ret = TRUE;
73
74
0
  if (mem3blt->cacheId == 0xFF)
75
0
    bitmap = offscreen_cache_get(cache->offscreen, mem3blt->cacheIndex);
76
0
  else
77
0
    bitmap = bitmap_cache_get(cache->bitmap, (BYTE)mem3blt->cacheId, mem3blt->cacheIndex);
78
79
  /* XP-SP2 servers sometimes ask for cached bitmaps they've never defined. */
80
0
  if (!bitmap)
81
0
    return TRUE;
82
83
0
  const BYTE style = WINPR_ASSERTING_INT_CAST(UINT8, brush->style);
84
85
0
  if (brush->style & CACHED_BRUSH)
86
0
  {
87
0
    brush->data = brush_cache_get(cache->brush, brush->index, &brush->bpp);
88
89
0
    if (!brush->data)
90
0
      return FALSE;
91
92
0
    brush->style = 0x03;
93
0
  }
94
95
0
  mem3blt->bitmap = bitmap;
96
0
  IFCALLRET(cache->bitmap->Mem3Blt, ret, context, mem3blt);
97
0
  brush->style = style;
98
0
  return ret;
99
0
}
100
101
static BOOL update_gdi_cache_bitmap(rdpContext* context, const CACHE_BITMAP_ORDER* cacheBitmap)
102
0
{
103
0
  rdpBitmap* bitmap = nullptr;
104
0
  rdpBitmap* prevBitmap = nullptr;
105
0
  rdpCache* cache = context->cache;
106
0
  bitmap = Bitmap_Alloc(context);
107
108
0
  if (!bitmap)
109
0
    return FALSE;
110
111
0
  if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmap->bitmapWidth),
112
0
                            WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmap->bitmapHeight)))
113
0
    goto fail;
114
115
0
  if (!bitmap->Decompress(context, bitmap, cacheBitmap->bitmapDataStream,
116
0
                          cacheBitmap->bitmapWidth, cacheBitmap->bitmapHeight,
117
0
                          cacheBitmap->bitmapBpp, cacheBitmap->bitmapLength,
118
0
                          cacheBitmap->compressed, RDP_CODEC_ID_NONE))
119
0
    goto fail;
120
121
0
  if (!bitmap->New(context, bitmap))
122
0
    goto fail;
123
124
0
  prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex);
125
0
  Bitmap_Free(context, prevBitmap);
126
0
  if (!bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap))
127
0
    goto fail;
128
0
  return TRUE;
129
130
0
fail:
131
0
  Bitmap_Free(context, bitmap);
132
0
  return FALSE;
133
0
}
134
135
static BOOL update_gdi_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
136
137
0
{
138
0
  rdpBitmap* prevBitmap = nullptr;
139
0
  rdpCache* cache = context->cache;
140
0
  rdpSettings* settings = context->settings;
141
0
  rdpBitmap* bitmap = Bitmap_Alloc(context);
142
143
0
  if (!bitmap)
144
0
    return FALSE;
145
146
0
  const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
147
0
  bitmap->key64 = ((UINT64)cacheBitmapV2->key1 | (((UINT64)cacheBitmapV2->key2) << 32));
148
149
0
  if (!cacheBitmapV2->bitmapBpp)
150
0
    cacheBitmapV2->bitmapBpp = ColorDepth;
151
152
0
  if ((ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16))
153
0
    cacheBitmapV2->bitmapBpp = ColorDepth;
154
155
0
  if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapWidth),
156
0
                            WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapHeight)))
157
0
    goto fail;
158
159
0
  if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream,
160
0
                          cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight,
161
0
                          cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength,
162
0
                          cacheBitmapV2->compressed, RDP_CODEC_ID_NONE))
163
0
    goto fail;
164
165
0
  prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex);
166
167
0
  if (!bitmap->New(context, bitmap))
168
0
    goto fail;
169
170
0
  Bitmap_Free(context, prevBitmap);
171
0
  if (!bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex, bitmap))
172
0
    goto fail;
173
0
  return TRUE;
174
175
0
fail:
176
0
  Bitmap_Free(context, bitmap);
177
0
  return FALSE;
178
0
}
179
180
static BOOL update_gdi_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
181
0
{
182
0
  rdpBitmap* bitmap = nullptr;
183
0
  rdpBitmap* prevBitmap = nullptr;
184
0
  BOOL compressed = TRUE;
185
0
  rdpCache* cache = context->cache;
186
0
  rdpSettings* settings = context->settings;
187
0
  BITMAP_DATA_EX* bitmapData = &cacheBitmapV3->bitmapData;
188
0
  bitmap = Bitmap_Alloc(context);
189
190
0
  if (!bitmap)
191
0
    return FALSE;
192
193
0
  const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
194
0
  bitmap->key64 = ((UINT64)cacheBitmapV3->key1 | (((UINT64)cacheBitmapV3->key2) << 32));
195
196
0
  if (!cacheBitmapV3->bpp)
197
0
    cacheBitmapV3->bpp = ColorDepth;
198
199
0
  compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE);
200
201
0
  if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->width),
202
0
                            WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->height)))
203
0
    goto fail;
204
205
0
  if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width,
206
0
                          bitmapData->height, bitmapData->bpp, bitmapData->length, compressed,
207
0
                          bitmapData->codecID))
208
0
    goto fail;
209
210
0
  if (!bitmap->New(context, bitmap))
211
0
    goto fail;
212
213
0
  prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex);
214
0
  Bitmap_Free(context, prevBitmap);
215
0
  if (!bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex, bitmap))
216
0
    goto fail;
217
0
  return TRUE;
218
219
0
fail:
220
0
  Bitmap_Free(context, bitmap);
221
0
  return FALSE;
222
0
}
223
224
rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index)
225
0
{
226
0
  rdpBitmap* bitmap = nullptr;
227
228
0
  if (id >= bitmapCache->maxCells)
229
0
  {
230
0
    WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
231
0
    return nullptr;
232
0
  }
233
234
0
  if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
235
0
  {
236
0
    index = bitmapCache->cells[id].number;
237
0
  }
238
0
  else if (index > bitmapCache->cells[id].number)
239
0
  {
240
0
    WLog_ERR(TAG, "get invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
241
0
    return nullptr;
242
0
  }
243
244
0
  bitmap = bitmapCache->cells[id].entries[index];
245
0
  return bitmap;
246
0
}
247
248
BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, rdpBitmap* bitmap)
249
0
{
250
0
  WINPR_ASSERT(bitmapCache);
251
0
  if (id > bitmapCache->maxCells)
252
0
  {
253
0
    WLog_ERR(TAG, "put invalid bitmap cell id: %" PRIu32 "", id);
254
0
    return FALSE;
255
0
  }
256
257
0
  if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
258
0
  {
259
0
    index = bitmapCache->cells[id].number;
260
0
  }
261
0
  else if (index > bitmapCache->cells[id].number)
262
0
  {
263
0
    WLog_ERR(TAG, "put invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
264
0
    return FALSE;
265
0
  }
266
267
0
  bitmapCache->cells[id].entries[index] = bitmap;
268
0
  return TRUE;
269
0
}
270
271
void bitmap_cache_register_callbacks(rdpUpdate* update)
272
0
{
273
0
  rdpCache* cache = nullptr;
274
275
0
  WINPR_ASSERT(update);
276
0
  WINPR_ASSERT(update->context);
277
0
  WINPR_ASSERT(update->context->cache);
278
279
0
  cache = update->context->cache;
280
0
  WINPR_ASSERT(cache);
281
282
0
  if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
283
0
  {
284
0
    cache->bitmap->MemBlt = update->primary->MemBlt;
285
0
    cache->bitmap->Mem3Blt = update->primary->Mem3Blt;
286
0
    update->primary->MemBlt = update_gdi_memblt;
287
0
    update->primary->Mem3Blt = update_gdi_mem3blt;
288
0
    update->secondary->CacheBitmap = update_gdi_cache_bitmap;
289
0
    update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2;
290
0
    update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3;
291
0
    update->BitmapUpdate = gdi_bitmap_update;
292
0
  }
293
0
}
294
295
static int bitmap_cache_save_persistent(rdpBitmapCache* bitmapCache)
296
0
{
297
0
  WINPR_ASSERT(bitmapCache);
298
299
0
  rdpContext* context = bitmapCache->context;
300
0
  rdpSettings* settings = context->settings;
301
302
0
  const UINT32 version = freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheVersion);
303
304
0
  if (version != 2)
305
0
    return 0; /* persistent bitmap cache already saved in egfx channel */
306
307
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
308
0
    return 0;
309
310
0
  const char* BitmapCachePersistFile =
311
0
      freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
312
0
  if (!BitmapCachePersistFile)
313
0
    return 0;
314
315
0
  rdpPersistentCache* persistent = persistent_cache_new();
316
317
0
  if (!persistent)
318
0
    return -1;
319
320
0
  int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version);
321
322
0
  if (status < 1)
323
0
    goto end;
324
325
0
  if (bitmapCache->cells)
326
0
  {
327
0
    for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
328
0
    {
329
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
330
0
      for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++)
331
0
      {
332
0
        PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
333
0
        rdpBitmap* bitmap = cell->entries[j];
334
335
0
        if (!bitmap || !bitmap->key64)
336
0
          continue;
337
338
0
        cacheEntry.key64 = bitmap->key64;
339
340
0
        cacheEntry.width = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width);
341
0
        cacheEntry.height = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height);
342
0
        const UINT64 size = 4ULL * bitmap->width * bitmap->height;
343
0
        if (size > UINT32_MAX)
344
0
          continue;
345
0
        cacheEntry.size = (UINT32)size;
346
0
        cacheEntry.flags = 0;
347
0
        cacheEntry.data = bitmap->data;
348
349
0
        if (persistent_cache_write_entry(persistent, &cacheEntry) < 1)
350
0
        {
351
0
          status = -1;
352
0
          goto end;
353
0
        }
354
0
      }
355
0
    }
356
0
  }
357
358
0
  status = 1;
359
360
0
end:
361
0
  persistent_cache_free(persistent);
362
0
  return status;
363
0
}
364
365
static void bitmap_cache_cell_free(rdpBitmapCache* bitmapCache)
366
0
{
367
0
  WINPR_ASSERT(bitmapCache);
368
0
  if (!bitmapCache->cells)
369
0
    return;
370
371
  /* iterate through maxCells + 1 to also free the overallocated extra slot */
372
0
  for (UINT32 i = 0; i <= bitmapCache->maxCells; i++)
373
0
  {
374
0
    UINT32 j = 0;
375
0
    BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
376
377
0
    if (!cell->entries)
378
0
      continue;
379
380
0
    for (j = 0; j < cell->number + 1; j++)
381
0
    {
382
0
      rdpBitmap* bitmap = cell->entries[j];
383
0
      Bitmap_Free(bitmapCache->context, bitmap);
384
0
    }
385
386
0
    free((void*)cell->entries);
387
0
  }
388
389
0
  free(bitmapCache->cells);
390
391
0
  bitmapCache->cells = nullptr;
392
0
}
393
394
BOOL bitmap_cache_resize(rdpBitmapCache* bitmapCache)
395
0
{
396
0
  if (!bitmapCache)
397
0
    return FALSE;
398
399
0
  rdpContext* context = bitmapCache->context;
400
0
  WINPR_ASSERT(context);
401
402
0
  rdpSettings* settings = context->settings;
403
0
  WINPR_ASSERT(settings);
404
405
0
  const UINT32 BitmapCacheV2NumCells =
406
0
      freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
407
408
0
  if (BitmapCacheV2NumCells == bitmapCache->maxCells)
409
0
    return TRUE;
410
411
0
  bitmap_cache_cell_free(bitmapCache);
412
413
  /* overallocate by 1. older RDP servers do send a off by 1 cache index. */
414
0
  bitmapCache->cells =
415
0
      (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells + 1ull, sizeof(BITMAP_V2_CELL));
416
417
0
  if (!bitmapCache->cells)
418
0
    return FALSE;
419
0
  bitmapCache->maxCells = BitmapCacheV2NumCells;
420
421
0
  for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
422
0
  {
423
0
    const BITMAP_CACHE_V2_CELL_INFO* info =
424
0
        freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
425
0
    if (!info)
426
0
      return FALSE;
427
428
0
    BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
429
0
    UINT32 nr = info->numEntries;
430
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
431
0
    cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
432
433
0
    if (!cell->entries)
434
0
      return FALSE;
435
0
    cell->number = nr;
436
0
  }
437
438
  /* initialize the overallocated extra slot for old RDP servers that send
439
   * cacheId == maxCells; use a minimal allocation since no protocol-negotiated
440
   * capacity exists for this slot */
441
0
  {
442
0
    BITMAP_V2_CELL* extra = &bitmapCache->cells[bitmapCache->maxCells];
443
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
444
0
    extra->entries = (rdpBitmap**)calloc(1, sizeof(rdpBitmap*));
445
446
0
    if (!extra->entries)
447
0
      return FALSE;
448
0
    extra->number = 0;
449
0
  }
450
451
0
  return TRUE;
452
0
}
453
454
rdpBitmapCache* bitmap_cache_new(rdpContext* context)
455
0
{
456
0
  WINPR_ASSERT(context);
457
458
0
  rdpBitmapCache* bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
459
460
0
  if (!bitmapCache)
461
0
    return nullptr;
462
463
0
  bitmapCache->context = context;
464
465
0
  if (!bitmap_cache_resize(bitmapCache))
466
0
    goto fail;
467
468
0
  return bitmapCache;
469
0
fail:
470
0
  WINPR_PRAGMA_DIAG_PUSH
471
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
472
0
  bitmap_cache_free(bitmapCache);
473
0
  WINPR_PRAGMA_DIAG_POP
474
0
  return nullptr;
475
0
}
476
477
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
478
0
{
479
0
  if (!bitmapCache)
480
0
    return;
481
482
0
  bitmap_cache_save_persistent(bitmapCache);
483
484
0
  bitmap_cache_cell_free(bitmapCache);
485
486
0
  persistent_cache_free(bitmapCache->persistent);
487
488
0
  free(bitmapCache);
489
0
}
490
491
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
492
364
{
493
364
  if (!data)
494
4
    return;
495
496
2.56M
  for (size_t x = 0; x < count; x++)
497
2.56M
    free(data[x].bitmapDataStream);
498
499
360
  free(data);
500
360
}
501
502
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
503
0
{
504
0
  BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
505
506
0
  if (!dst)
507
0
    goto fail;
508
509
0
  for (size_t x = 0; x < count; x++)
510
0
  {
511
0
    dst[x] = data[x];
512
513
0
    if (data[x].bitmapLength > 0)
514
0
    {
515
0
      dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
516
517
0
      if (!dst[x].bitmapDataStream)
518
0
        goto fail;
519
520
0
      memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
521
0
    }
522
0
  }
523
524
0
  return dst;
525
0
fail:
526
0
  free_bitmap_data(dst, count);
527
0
  return nullptr;
528
0
}
529
530
void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
531
                        WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
532
364
{
533
364
  if (!pointer)
534
0
    return;
535
536
364
  free_bitmap_data(pointer->rectangles, pointer->number);
537
364
  free(pointer);
538
364
}
539
540
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
541
0
{
542
0
  BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
543
544
0
  if (!dst || !pointer)
545
0
    goto fail;
546
547
0
  *dst = *pointer;
548
0
  dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
549
550
0
  if (!dst->rectangles)
551
0
    goto fail;
552
553
0
  return dst;
554
0
fail:
555
0
  WINPR_PRAGMA_DIAG_PUSH
556
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
557
0
  free_bitmap_update(context, dst);
558
0
  WINPR_PRAGMA_DIAG_POP
559
0
  return nullptr;
560
0
}
561
562
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
563
0
{
564
0
  CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
565
566
0
  if (!dst || !order)
567
0
    goto fail;
568
569
0
  *dst = *order;
570
571
0
  if (order->bitmapLength > 0)
572
0
  {
573
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
574
575
0
    if (!dst->bitmapDataStream)
576
0
      goto fail;
577
578
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
579
0
  }
580
581
0
  return dst;
582
0
fail:
583
0
  WINPR_PRAGMA_DIAG_PUSH
584
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
585
0
  free_cache_bitmap_order(context, dst);
586
0
  WINPR_PRAGMA_DIAG_POP
587
0
  return nullptr;
588
0
}
589
590
void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
591
0
{
592
0
  if (order)
593
0
    free(order->bitmapDataStream);
594
595
0
  free(order);
596
0
}
597
598
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
599
                                                  const CACHE_BITMAP_V2_ORDER* order)
600
0
{
601
0
  CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
602
603
0
  if (!dst || !order)
604
0
    goto fail;
605
606
0
  *dst = *order;
607
608
0
  if (order->bitmapLength > 0)
609
0
  {
610
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
611
612
0
    if (!dst->bitmapDataStream)
613
0
      goto fail;
614
615
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
616
0
  }
617
618
0
  return dst;
619
0
fail:
620
0
  WINPR_PRAGMA_DIAG_PUSH
621
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
622
0
  free_cache_bitmap_v2_order(context, dst);
623
0
  WINPR_PRAGMA_DIAG_POP
624
0
  return nullptr;
625
0
}
626
627
void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
628
                                WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
629
0
{
630
0
  if (order)
631
0
    free(order->bitmapDataStream);
632
633
0
  free(order);
634
0
}
635
636
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
637
                                                  const CACHE_BITMAP_V3_ORDER* order)
638
0
{
639
0
  CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
640
641
0
  if (!dst || !order)
642
0
    goto fail;
643
644
0
  *dst = *order;
645
646
0
  if (order->bitmapData.length > 0)
647
0
  {
648
0
    dst->bitmapData.data = malloc(order->bitmapData.length);
649
650
0
    if (!dst->bitmapData.data)
651
0
      goto fail;
652
653
0
    memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
654
0
  }
655
656
0
  return dst;
657
0
fail:
658
0
  WINPR_PRAGMA_DIAG_PUSH
659
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
660
0
  free_cache_bitmap_v3_order(context, dst);
661
0
  WINPR_PRAGMA_DIAG_POP
662
0
  return nullptr;
663
0
}
664
665
void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
666
0
{
667
0
  if (order)
668
0
    free(order->bitmapData.data);
669
670
0
  free(order);
671
0
}