Coverage Report

Created: 2026-05-11 07:01

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
  /* initialize the overallocated extra slot for old RDP servers that send
400
   * cacheId == maxCells; use a minimal allocation since no protocol-negotiated
401
   * capacity exists for this slot */
402
0
  {
403
0
    BITMAP_V2_CELL* extra = &bitmapCache->cells[bitmapCache->maxCells];
404
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
405
0
    extra->entries = (rdpBitmap**)calloc(1, sizeof(rdpBitmap*));
406
407
0
    if (!extra->entries)
408
0
      goto fail;
409
0
    extra->number = 0;
410
0
  }
411
412
0
  return bitmapCache;
413
0
fail:
414
0
  WINPR_PRAGMA_DIAG_PUSH
415
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
416
0
  bitmap_cache_free(bitmapCache);
417
0
  WINPR_PRAGMA_DIAG_POP
418
0
  return nullptr;
419
0
}
420
421
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
422
0
{
423
0
  if (!bitmapCache)
424
0
    return;
425
426
0
  bitmap_cache_save_persistent(bitmapCache);
427
428
0
  if (bitmapCache->cells)
429
0
  {
430
    /* iterate through maxCells + 1 to also free the overallocated extra slot */
431
0
    for (UINT32 i = 0; i <= bitmapCache->maxCells; i++)
432
0
    {
433
0
      UINT32 j = 0;
434
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
435
436
0
      if (!cell->entries)
437
0
        continue;
438
439
0
      for (j = 0; j < cell->number + 1; j++)
440
0
      {
441
0
        rdpBitmap* bitmap = cell->entries[j];
442
0
        Bitmap_Free(bitmapCache->context, bitmap);
443
0
      }
444
445
0
      free((void*)cell->entries);
446
0
    }
447
448
0
    free(bitmapCache->cells);
449
0
  }
450
451
0
  persistent_cache_free(bitmapCache->persistent);
452
453
0
  free(bitmapCache);
454
0
}
455
456
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
457
397
{
458
397
  if (!data)
459
5
    return;
460
461
2.31M
  for (size_t x = 0; x < count; x++)
462
2.31M
    free(data[x].bitmapDataStream);
463
464
392
  free(data);
465
392
}
466
467
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
468
0
{
469
0
  BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
470
471
0
  if (!dst)
472
0
    goto fail;
473
474
0
  for (size_t x = 0; x < count; x++)
475
0
  {
476
0
    dst[x] = data[x];
477
478
0
    if (data[x].bitmapLength > 0)
479
0
    {
480
0
      dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
481
482
0
      if (!dst[x].bitmapDataStream)
483
0
        goto fail;
484
485
0
      memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
486
0
    }
487
0
  }
488
489
0
  return dst;
490
0
fail:
491
0
  free_bitmap_data(dst, count);
492
0
  return nullptr;
493
0
}
494
495
void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
496
                        WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
497
397
{
498
397
  if (!pointer)
499
0
    return;
500
501
397
  free_bitmap_data(pointer->rectangles, pointer->number);
502
397
  free(pointer);
503
397
}
504
505
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
506
0
{
507
0
  BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
508
509
0
  if (!dst || !pointer)
510
0
    goto fail;
511
512
0
  *dst = *pointer;
513
0
  dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
514
515
0
  if (!dst->rectangles)
516
0
    goto fail;
517
518
0
  return dst;
519
0
fail:
520
0
  WINPR_PRAGMA_DIAG_PUSH
521
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
522
0
  free_bitmap_update(context, dst);
523
0
  WINPR_PRAGMA_DIAG_POP
524
0
  return nullptr;
525
0
}
526
527
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
528
0
{
529
0
  CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
530
531
0
  if (!dst || !order)
532
0
    goto fail;
533
534
0
  *dst = *order;
535
536
0
  if (order->bitmapLength > 0)
537
0
  {
538
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
539
540
0
    if (!dst->bitmapDataStream)
541
0
      goto fail;
542
543
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
544
0
  }
545
546
0
  return dst;
547
0
fail:
548
0
  WINPR_PRAGMA_DIAG_PUSH
549
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
550
0
  free_cache_bitmap_order(context, dst);
551
0
  WINPR_PRAGMA_DIAG_POP
552
0
  return nullptr;
553
0
}
554
555
void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
556
0
{
557
0
  if (order)
558
0
    free(order->bitmapDataStream);
559
560
0
  free(order);
561
0
}
562
563
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
564
                                                  const CACHE_BITMAP_V2_ORDER* order)
565
0
{
566
0
  CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
567
568
0
  if (!dst || !order)
569
0
    goto fail;
570
571
0
  *dst = *order;
572
573
0
  if (order->bitmapLength > 0)
574
0
  {
575
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
576
577
0
    if (!dst->bitmapDataStream)
578
0
      goto fail;
579
580
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
581
0
  }
582
583
0
  return dst;
584
0
fail:
585
0
  WINPR_PRAGMA_DIAG_PUSH
586
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
587
0
  free_cache_bitmap_v2_order(context, dst);
588
0
  WINPR_PRAGMA_DIAG_POP
589
0
  return nullptr;
590
0
}
591
592
void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
593
                                WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
594
0
{
595
0
  if (order)
596
0
    free(order->bitmapDataStream);
597
598
0
  free(order);
599
0
}
600
601
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
602
                                                  const CACHE_BITMAP_V3_ORDER* order)
603
0
{
604
0
  CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
605
606
0
  if (!dst || !order)
607
0
    goto fail;
608
609
0
  *dst = *order;
610
611
0
  if (order->bitmapData.length > 0)
612
0
  {
613
0
    dst->bitmapData.data = malloc(order->bitmapData.length);
614
615
0
    if (!dst->bitmapData.data)
616
0
      goto fail;
617
618
0
    memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
619
0
  }
620
621
0
  return dst;
622
0
fail:
623
0
  WINPR_PRAGMA_DIAG_PUSH
624
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
625
0
  free_cache_bitmap_v3_order(context, dst);
626
0
  WINPR_PRAGMA_DIAG_POP
627
0
  return nullptr;
628
0
}
629
630
void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
631
0
{
632
0
  if (order)
633
0
    free(order->bitmapData.data);
634
635
0
  free(order);
636
0
}