Coverage Report

Created: 2026-07-16 07:09

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
  rdpContext* context = bitmapCache->context;
298
0
  rdpSettings* settings = context->settings;
299
300
0
  const UINT32 version = freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheVersion);
301
302
0
  if (version != 2)
303
0
    return 0; /* persistent bitmap cache already saved in egfx channel */
304
305
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
306
0
    return 0;
307
308
0
  const char* BitmapCachePersistFile =
309
0
      freerdp_settings_get_string(settings, FreeRDP_BitmapCachePersistFile);
310
0
  if (!BitmapCachePersistFile)
311
0
    return 0;
312
313
0
  rdpPersistentCache* persistent = persistent_cache_new();
314
315
0
  if (!persistent)
316
0
    return -1;
317
318
0
  int status = persistent_cache_open(persistent, BitmapCachePersistFile, TRUE, version);
319
320
0
  if (status < 1)
321
0
    goto end;
322
323
0
  if (bitmapCache->cells)
324
0
  {
325
0
    for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
326
0
    {
327
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
328
0
      for (UINT32 j = 0; j < cell->number + 1 && cell->entries; j++)
329
0
      {
330
0
        PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
331
0
        rdpBitmap* bitmap = cell->entries[j];
332
333
0
        if (!bitmap || !bitmap->key64)
334
0
          continue;
335
336
0
        cacheEntry.key64 = bitmap->key64;
337
338
0
        cacheEntry.width = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->width);
339
0
        cacheEntry.height = WINPR_ASSERTING_INT_CAST(UINT16, bitmap->height);
340
0
        const UINT64 size = 4ULL * bitmap->width * bitmap->height;
341
0
        if (size > UINT32_MAX)
342
0
          continue;
343
0
        cacheEntry.size = (UINT32)size;
344
0
        cacheEntry.flags = 0;
345
0
        cacheEntry.data = bitmap->data;
346
347
0
        if (persistent_cache_write_entry(persistent, &cacheEntry) < 1)
348
0
        {
349
0
          status = -1;
350
0
          goto end;
351
0
        }
352
0
      }
353
0
    }
354
0
  }
355
356
0
  status = 1;
357
358
0
end:
359
0
  persistent_cache_free(persistent);
360
0
  return status;
361
0
}
362
363
rdpBitmapCache* bitmap_cache_new(rdpContext* context)
364
0
{
365
0
  rdpSettings* settings = nullptr;
366
0
  rdpBitmapCache* bitmapCache = nullptr;
367
368
0
  WINPR_ASSERT(context);
369
370
0
  settings = context->settings;
371
0
  WINPR_ASSERT(settings);
372
373
0
  bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
374
375
0
  if (!bitmapCache)
376
0
    return nullptr;
377
378
0
  const UINT32 BitmapCacheV2NumCells =
379
0
      freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
380
0
  bitmapCache->context = context;
381
382
  /* overallocate by 1. older RDP servers do send a off by 1 cache index. */
383
0
  bitmapCache->cells =
384
0
      (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells + 1ull, sizeof(BITMAP_V2_CELL));
385
386
0
  if (!bitmapCache->cells)
387
0
    goto fail;
388
0
  bitmapCache->maxCells = BitmapCacheV2NumCells;
389
390
0
  for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
391
0
  {
392
0
    const BITMAP_CACHE_V2_CELL_INFO* info =
393
0
        freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
394
0
    BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
395
0
    UINT32 nr = info->numEntries;
396
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
397
0
    cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
398
399
0
    if (!cell->entries)
400
0
      goto fail;
401
0
    cell->number = nr;
402
0
  }
403
404
  /* initialize the overallocated extra slot for old RDP servers that send
405
   * cacheId == maxCells; use a minimal allocation since no protocol-negotiated
406
   * capacity exists for this slot */
407
0
  {
408
0
    BITMAP_V2_CELL* extra = &bitmapCache->cells[bitmapCache->maxCells];
409
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
410
0
    extra->entries = (rdpBitmap**)calloc(1, sizeof(rdpBitmap*));
411
412
0
    if (!extra->entries)
413
0
      goto fail;
414
0
    extra->number = 0;
415
0
  }
416
417
0
  return bitmapCache;
418
0
fail:
419
0
  WINPR_PRAGMA_DIAG_PUSH
420
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
421
0
  bitmap_cache_free(bitmapCache);
422
0
  WINPR_PRAGMA_DIAG_POP
423
0
  return nullptr;
424
0
}
425
426
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
427
0
{
428
0
  if (!bitmapCache)
429
0
    return;
430
431
0
  bitmap_cache_save_persistent(bitmapCache);
432
433
0
  if (bitmapCache->cells)
434
0
  {
435
    /* iterate through maxCells + 1 to also free the overallocated extra slot */
436
0
    for (UINT32 i = 0; i <= bitmapCache->maxCells; i++)
437
0
    {
438
0
      UINT32 j = 0;
439
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
440
441
0
      if (!cell->entries)
442
0
        continue;
443
444
0
      for (j = 0; j < cell->number + 1; j++)
445
0
      {
446
0
        rdpBitmap* bitmap = cell->entries[j];
447
0
        Bitmap_Free(bitmapCache->context, bitmap);
448
0
      }
449
450
0
      free((void*)cell->entries);
451
0
    }
452
453
0
    free(bitmapCache->cells);
454
0
  }
455
456
0
  persistent_cache_free(bitmapCache->persistent);
457
458
0
  free(bitmapCache);
459
0
}
460
461
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
462
0
{
463
0
  if (!data)
464
0
    return;
465
466
0
  for (size_t x = 0; x < count; x++)
467
0
    free(data[x].bitmapDataStream);
468
469
0
  free(data);
470
0
}
471
472
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
473
0
{
474
0
  BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
475
476
0
  if (!dst)
477
0
    goto fail;
478
479
0
  for (size_t x = 0; x < count; x++)
480
0
  {
481
0
    dst[x] = data[x];
482
483
0
    if (data[x].bitmapLength > 0)
484
0
    {
485
0
      dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
486
487
0
      if (!dst[x].bitmapDataStream)
488
0
        goto fail;
489
490
0
      memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
491
0
    }
492
0
  }
493
494
0
  return dst;
495
0
fail:
496
0
  free_bitmap_data(dst, count);
497
0
  return nullptr;
498
0
}
499
500
void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
501
                        WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
502
0
{
503
0
  if (!pointer)
504
0
    return;
505
506
0
  free_bitmap_data(pointer->rectangles, pointer->number);
507
0
  free(pointer);
508
0
}
509
510
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
511
0
{
512
0
  BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
513
514
0
  if (!dst || !pointer)
515
0
    goto fail;
516
517
0
  *dst = *pointer;
518
0
  dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
519
520
0
  if (!dst->rectangles)
521
0
    goto fail;
522
523
0
  return dst;
524
0
fail:
525
0
  WINPR_PRAGMA_DIAG_PUSH
526
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
527
0
  free_bitmap_update(context, dst);
528
0
  WINPR_PRAGMA_DIAG_POP
529
0
  return nullptr;
530
0
}
531
532
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
533
0
{
534
0
  CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
535
536
0
  if (!dst || !order)
537
0
    goto fail;
538
539
0
  *dst = *order;
540
541
0
  if (order->bitmapLength > 0)
542
0
  {
543
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
544
545
0
    if (!dst->bitmapDataStream)
546
0
      goto fail;
547
548
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
549
0
  }
550
551
0
  return dst;
552
0
fail:
553
0
  WINPR_PRAGMA_DIAG_PUSH
554
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
555
0
  free_cache_bitmap_order(context, dst);
556
0
  WINPR_PRAGMA_DIAG_POP
557
0
  return nullptr;
558
0
}
559
560
void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
561
0
{
562
0
  if (order)
563
0
    free(order->bitmapDataStream);
564
565
0
  free(order);
566
0
}
567
568
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
569
                                                  const CACHE_BITMAP_V2_ORDER* order)
570
0
{
571
0
  CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
572
573
0
  if (!dst || !order)
574
0
    goto fail;
575
576
0
  *dst = *order;
577
578
0
  if (order->bitmapLength > 0)
579
0
  {
580
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
581
582
0
    if (!dst->bitmapDataStream)
583
0
      goto fail;
584
585
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
586
0
  }
587
588
0
  return dst;
589
0
fail:
590
0
  WINPR_PRAGMA_DIAG_PUSH
591
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
592
0
  free_cache_bitmap_v2_order(context, dst);
593
0
  WINPR_PRAGMA_DIAG_POP
594
0
  return nullptr;
595
0
}
596
597
void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
598
                                WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
599
0
{
600
0
  if (order)
601
0
    free(order->bitmapDataStream);
602
603
0
  free(order);
604
0
}
605
606
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
607
                                                  const CACHE_BITMAP_V3_ORDER* order)
608
0
{
609
0
  CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
610
611
0
  if (!dst || !order)
612
0
    goto fail;
613
614
0
  *dst = *order;
615
616
0
  if (order->bitmapData.length > 0)
617
0
  {
618
0
    dst->bitmapData.data = malloc(order->bitmapData.length);
619
620
0
    if (!dst->bitmapData.data)
621
0
      goto fail;
622
623
0
    memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
624
0
  }
625
626
0
  return dst;
627
0
fail:
628
0
  WINPR_PRAGMA_DIAG_PUSH
629
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
630
0
  free_cache_bitmap_v3_order(context, dst);
631
0
  WINPR_PRAGMA_DIAG_POP
632
0
  return nullptr;
633
0
}
634
635
void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
636
0
{
637
0
  if (order)
638
0
    free(order->bitmapData.data);
639
640
0
  free(order);
641
0
}