Coverage Report

Created: 2026-03-04 06:17

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
  return bitmap_cache_put(cache->bitmap, cacheBitmap->cacheId, cacheBitmap->cacheIndex, bitmap);
127
128
0
fail:
129
0
  Bitmap_Free(context, bitmap);
130
0
  return FALSE;
131
0
}
132
133
static BOOL update_gdi_cache_bitmap_v2(rdpContext* context, CACHE_BITMAP_V2_ORDER* cacheBitmapV2)
134
135
0
{
136
0
  rdpBitmap* prevBitmap = nullptr;
137
0
  rdpCache* cache = context->cache;
138
0
  rdpSettings* settings = context->settings;
139
0
  rdpBitmap* bitmap = Bitmap_Alloc(context);
140
141
0
  if (!bitmap)
142
0
    return FALSE;
143
144
0
  const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
145
0
  bitmap->key64 = ((UINT64)cacheBitmapV2->key1 | (((UINT64)cacheBitmapV2->key2) << 32));
146
147
0
  if (!cacheBitmapV2->bitmapBpp)
148
0
    cacheBitmapV2->bitmapBpp = ColorDepth;
149
150
0
  if ((ColorDepth == 15) && (cacheBitmapV2->bitmapBpp == 16))
151
0
    cacheBitmapV2->bitmapBpp = ColorDepth;
152
153
0
  if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapWidth),
154
0
                            WINPR_ASSERTING_INT_CAST(UINT16, cacheBitmapV2->bitmapHeight)))
155
0
    goto fail;
156
157
0
  if (!bitmap->Decompress(context, bitmap, cacheBitmapV2->bitmapDataStream,
158
0
                          cacheBitmapV2->bitmapWidth, cacheBitmapV2->bitmapHeight,
159
0
                          cacheBitmapV2->bitmapBpp, cacheBitmapV2->bitmapLength,
160
0
                          cacheBitmapV2->compressed, RDP_CODEC_ID_NONE))
161
0
    goto fail;
162
163
0
  prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex);
164
165
0
  if (!bitmap->New(context, bitmap))
166
0
    goto fail;
167
168
0
  Bitmap_Free(context, prevBitmap);
169
0
  return bitmap_cache_put(cache->bitmap, cacheBitmapV2->cacheId, cacheBitmapV2->cacheIndex,
170
0
                          bitmap);
171
172
0
fail:
173
0
  Bitmap_Free(context, bitmap);
174
0
  return FALSE;
175
0
}
176
177
static BOOL update_gdi_cache_bitmap_v3(rdpContext* context, CACHE_BITMAP_V3_ORDER* cacheBitmapV3)
178
0
{
179
0
  rdpBitmap* bitmap = nullptr;
180
0
  rdpBitmap* prevBitmap = nullptr;
181
0
  BOOL compressed = TRUE;
182
0
  rdpCache* cache = context->cache;
183
0
  rdpSettings* settings = context->settings;
184
0
  BITMAP_DATA_EX* bitmapData = &cacheBitmapV3->bitmapData;
185
0
  bitmap = Bitmap_Alloc(context);
186
187
0
  if (!bitmap)
188
0
    return FALSE;
189
190
0
  const UINT32 ColorDepth = freerdp_settings_get_uint32(settings, FreeRDP_ColorDepth);
191
0
  bitmap->key64 = ((UINT64)cacheBitmapV3->key1 | (((UINT64)cacheBitmapV3->key2) << 32));
192
193
0
  if (!cacheBitmapV3->bpp)
194
0
    cacheBitmapV3->bpp = ColorDepth;
195
196
0
  compressed = (bitmapData->codecID != RDP_CODEC_ID_NONE);
197
198
0
  if (!Bitmap_SetDimensions(bitmap, WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->width),
199
0
                            WINPR_ASSERTING_INT_CAST(UINT16, bitmapData->height)))
200
0
    goto fail;
201
202
0
  if (!bitmap->Decompress(context, bitmap, bitmapData->data, bitmapData->width,
203
0
                          bitmapData->height, bitmapData->bpp, bitmapData->length, compressed,
204
0
                          bitmapData->codecID))
205
0
    goto fail;
206
207
0
  if (!bitmap->New(context, bitmap))
208
0
    goto fail;
209
210
0
  prevBitmap = bitmap_cache_get(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex);
211
0
  Bitmap_Free(context, prevBitmap);
212
0
  return bitmap_cache_put(cache->bitmap, cacheBitmapV3->cacheId, cacheBitmapV3->cacheIndex,
213
0
                          bitmap);
214
215
0
fail:
216
0
  Bitmap_Free(context, bitmap);
217
0
  return FALSE;
218
0
}
219
220
rdpBitmap* bitmap_cache_get(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index)
221
0
{
222
0
  rdpBitmap* bitmap = nullptr;
223
224
0
  if (id >= bitmapCache->maxCells)
225
0
  {
226
0
    WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
227
0
    return nullptr;
228
0
  }
229
230
0
  if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
231
0
  {
232
0
    index = bitmapCache->cells[id].number;
233
0
  }
234
0
  else if (index > bitmapCache->cells[id].number)
235
0
  {
236
0
    WLog_ERR(TAG, "get invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
237
0
    return nullptr;
238
0
  }
239
240
0
  bitmap = bitmapCache->cells[id].entries[index];
241
0
  return bitmap;
242
0
}
243
244
BOOL bitmap_cache_put(rdpBitmapCache* bitmapCache, UINT32 id, UINT32 index, rdpBitmap* bitmap)
245
0
{
246
0
  if (id > bitmapCache->maxCells)
247
0
  {
248
0
    WLog_ERR(TAG, "put invalid bitmap cell id: %" PRIu32 "", id);
249
0
    return FALSE;
250
0
  }
251
252
0
  if (index == BITMAP_CACHE_WAITING_LIST_INDEX)
253
0
  {
254
0
    index = bitmapCache->cells[id].number;
255
0
  }
256
0
  else if (index > bitmapCache->cells[id].number)
257
0
  {
258
0
    WLog_ERR(TAG, "put invalid bitmap index %" PRIu32 " in cell id: %" PRIu32 "", index, id);
259
0
    return FALSE;
260
0
  }
261
262
0
  bitmapCache->cells[id].entries[index] = bitmap;
263
0
  return TRUE;
264
0
}
265
266
void bitmap_cache_register_callbacks(rdpUpdate* update)
267
0
{
268
0
  rdpCache* cache = nullptr;
269
270
0
  WINPR_ASSERT(update);
271
0
  WINPR_ASSERT(update->context);
272
0
  WINPR_ASSERT(update->context->cache);
273
274
0
  cache = update->context->cache;
275
0
  WINPR_ASSERT(cache);
276
277
0
  if (!freerdp_settings_get_bool(update->context->settings, FreeRDP_DeactivateClientDecoding))
278
0
  {
279
0
    cache->bitmap->MemBlt = update->primary->MemBlt;
280
0
    cache->bitmap->Mem3Blt = update->primary->Mem3Blt;
281
0
    update->primary->MemBlt = update_gdi_memblt;
282
0
    update->primary->Mem3Blt = update_gdi_mem3blt;
283
0
    update->secondary->CacheBitmap = update_gdi_cache_bitmap;
284
0
    update->secondary->CacheBitmapV2 = update_gdi_cache_bitmap_v2;
285
0
    update->secondary->CacheBitmapV3 = update_gdi_cache_bitmap_v3;
286
0
    update->BitmapUpdate = gdi_bitmap_update;
287
0
  }
288
0
}
289
290
static int bitmap_cache_save_persistent(rdpBitmapCache* bitmapCache)
291
0
{
292
0
  rdpContext* context = bitmapCache->context;
293
0
  rdpSettings* settings = context->settings;
294
295
0
  const UINT32 version = freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheVersion);
296
297
0
  if (version != 2)
298
0
    return 0; /* persistent bitmap cache already saved in egfx channel */
299
300
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
301
0
    return 0;
302
303
0
  const char* BitmapCachePersistFile =
304
0
      freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
305
0
  if (!BitmapCachePersistFile)
306
0
    return 0;
307
308
0
  rdpPersistentCache* persistent = persistent_cache_new();
309
310
0
  if (!persistent)
311
0
    return -1;
312
313
0
  int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version);
314
315
0
  if (status < 1)
316
0
    goto end;
317
318
0
  if (bitmapCache->cells)
319
0
  {
320
0
    for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
321
0
    {
322
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
323
0
      for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++)
324
0
      {
325
0
        PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
326
0
        rdpBitmap* bitmap = cell->entries[j];
327
328
0
        if (!bitmap || !bitmap->key64)
329
0
          continue;
330
331
0
        cacheEntry.key64 = bitmap->key64;
332
333
0
        cacheEntry.width = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width);
334
0
        cacheEntry.height = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height);
335
0
        const UINT64 size = 4ULL * bitmap->width * bitmap->height;
336
0
        if (size > UINT32_MAX)
337
0
          continue;
338
0
        cacheEntry.size = (UINT32)size;
339
0
        cacheEntry.flags = 0;
340
0
        cacheEntry.data = bitmap->data;
341
342
0
        if (persistent_cache_write_entry(persistent, &cacheEntry) < 1)
343
0
        {
344
0
          status = -1;
345
0
          goto end;
346
0
        }
347
0
      }
348
0
    }
349
0
  }
350
351
0
  status = 1;
352
353
0
end:
354
0
  persistent_cache_free(persistent);
355
0
  return status;
356
0
}
357
358
rdpBitmapCache* bitmap_cache_new(rdpContext* context)
359
0
{
360
0
  rdpSettings* settings = nullptr;
361
0
  rdpBitmapCache* bitmapCache = nullptr;
362
363
0
  WINPR_ASSERT(context);
364
365
0
  settings = context->settings;
366
0
  WINPR_ASSERT(settings);
367
368
0
  bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
369
370
0
  if (!bitmapCache)
371
0
    return nullptr;
372
373
0
  const UINT32 BitmapCacheV2NumCells =
374
0
      freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
375
0
  bitmapCache->context = context;
376
377
  /* overallocate by 1. older RDP servers do send a off by 1 cache index. */
378
0
  bitmapCache->cells =
379
0
      (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells + 1ull, sizeof(BITMAP_V2_CELL));
380
381
0
  if (!bitmapCache->cells)
382
0
    goto fail;
383
0
  bitmapCache->maxCells = BitmapCacheV2NumCells;
384
385
0
  for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
386
0
  {
387
0
    const BITMAP_CACHE_V2_CELL_INFO* info =
388
0
        freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
389
0
    BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
390
0
    UINT32 nr = info->numEntries;
391
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
392
0
    cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
393
394
0
    if (!cell->entries)
395
0
      goto fail;
396
0
    cell->number = nr;
397
0
  }
398
399
0
  return bitmapCache;
400
0
fail:
401
0
  WINPR_PRAGMA_DIAG_PUSH
402
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
403
0
  bitmap_cache_free(bitmapCache);
404
0
  WINPR_PRAGMA_DIAG_POP
405
0
  return nullptr;
406
0
}
407
408
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
409
0
{
410
0
  if (!bitmapCache)
411
0
    return;
412
413
0
  bitmap_cache_save_persistent(bitmapCache);
414
415
0
  if (bitmapCache->cells)
416
0
  {
417
0
    for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
418
0
    {
419
0
      UINT32 j = 0;
420
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
421
422
0
      if (!cell->entries)
423
0
        continue;
424
425
0
      for (j = 0; j < cell->number + 1; j++)
426
0
      {
427
0
        rdpBitmap* bitmap = cell->entries[j];
428
0
        Bitmap_Free(bitmapCache->context, bitmap);
429
0
      }
430
431
0
      free((void*)cell->entries);
432
0
    }
433
434
0
    free(bitmapCache->cells);
435
0
  }
436
437
0
  persistent_cache_free(bitmapCache->persistent);
438
439
0
  free(bitmapCache);
440
0
}
441
442
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
443
331
{
444
331
  if (!data)
445
5
    return;
446
447
2.57M
  for (size_t x = 0; x < count; x++)
448
2.57M
    free(data[x].bitmapDataStream);
449
450
326
  free(data);
451
326
}
452
453
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
454
0
{
455
0
  BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
456
457
0
  if (!dst)
458
0
    goto fail;
459
460
0
  for (size_t x = 0; x < count; x++)
461
0
  {
462
0
    dst[x] = data[x];
463
464
0
    if (data[x].bitmapLength > 0)
465
0
    {
466
0
      dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
467
468
0
      if (!dst[x].bitmapDataStream)
469
0
        goto fail;
470
471
0
      memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
472
0
    }
473
0
  }
474
475
0
  return dst;
476
0
fail:
477
0
  free_bitmap_data(dst, count);
478
0
  return nullptr;
479
0
}
480
481
void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
482
                        WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
483
331
{
484
331
  if (!pointer)
485
0
    return;
486
487
331
  free_bitmap_data(pointer->rectangles, pointer->number);
488
331
  free(pointer);
489
331
}
490
491
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
492
0
{
493
0
  BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
494
495
0
  if (!dst || !pointer)
496
0
    goto fail;
497
498
0
  *dst = *pointer;
499
0
  dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
500
501
0
  if (!dst->rectangles)
502
0
    goto fail;
503
504
0
  return dst;
505
0
fail:
506
0
  WINPR_PRAGMA_DIAG_PUSH
507
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
508
0
  free_bitmap_update(context, dst);
509
0
  WINPR_PRAGMA_DIAG_POP
510
0
  return nullptr;
511
0
}
512
513
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
514
0
{
515
0
  CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
516
517
0
  if (!dst || !order)
518
0
    goto fail;
519
520
0
  *dst = *order;
521
522
0
  if (order->bitmapLength > 0)
523
0
  {
524
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
525
526
0
    if (!dst->bitmapDataStream)
527
0
      goto fail;
528
529
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
530
0
  }
531
532
0
  return dst;
533
0
fail:
534
0
  WINPR_PRAGMA_DIAG_PUSH
535
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
536
0
  free_cache_bitmap_order(context, dst);
537
0
  WINPR_PRAGMA_DIAG_POP
538
0
  return nullptr;
539
0
}
540
541
void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
542
0
{
543
0
  if (order)
544
0
    free(order->bitmapDataStream);
545
546
0
  free(order);
547
0
}
548
549
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
550
                                                  const CACHE_BITMAP_V2_ORDER* order)
551
0
{
552
0
  CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
553
554
0
  if (!dst || !order)
555
0
    goto fail;
556
557
0
  *dst = *order;
558
559
0
  if (order->bitmapLength > 0)
560
0
  {
561
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
562
563
0
    if (!dst->bitmapDataStream)
564
0
      goto fail;
565
566
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
567
0
  }
568
569
0
  return dst;
570
0
fail:
571
0
  WINPR_PRAGMA_DIAG_PUSH
572
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
573
0
  free_cache_bitmap_v2_order(context, dst);
574
0
  WINPR_PRAGMA_DIAG_POP
575
0
  return nullptr;
576
0
}
577
578
void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
579
                                WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
580
0
{
581
0
  if (order)
582
0
    free(order->bitmapDataStream);
583
584
0
  free(order);
585
0
}
586
587
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
588
                                                  const CACHE_BITMAP_V3_ORDER* order)
589
0
{
590
0
  CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
591
592
0
  if (!dst || !order)
593
0
    goto fail;
594
595
0
  *dst = *order;
596
597
0
  if (order->bitmapData.length > 0)
598
0
  {
599
0
    dst->bitmapData.data = malloc(order->bitmapData.length);
600
601
0
    if (!dst->bitmapData.data)
602
0
      goto fail;
603
604
0
    memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
605
0
  }
606
607
0
  return dst;
608
0
fail:
609
0
  WINPR_PRAGMA_DIAG_PUSH
610
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
611
0
  free_cache_bitmap_v3_order(context, dst);
612
0
  WINPR_PRAGMA_DIAG_POP
613
0
  return nullptr;
614
0
}
615
616
void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
617
0
{
618
0
  if (order)
619
0
    free(order->bitmapData.data);
620
621
0
  free(order);
622
0
}