Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/cache/bitmap.c
Line
Count
Source (jump to first uncovered line)
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 = NULL;
50
0
  rdpCache* cache = NULL;
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 == NULL)
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 = NULL;
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 = NULL;
104
0
  rdpBitmap* prevBitmap = NULL;
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 = NULL;
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 = NULL;
180
0
  rdpBitmap* prevBitmap = NULL;
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 = NULL;
223
224
0
  if (id >= bitmapCache->maxCells)
225
0
  {
226
0
    WLog_ERR(TAG, "get invalid bitmap cell id: %" PRIu32 "", id);
227
0
    return NULL;
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 NULL;
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 = NULL;
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 = { 0 };
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 = NULL;
361
0
  rdpBitmapCache* bitmapCache = NULL;
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 NULL;
372
373
0
  const UINT32 BitmapCacheV2NumCells =
374
0
      freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
375
0
  bitmapCache->context = context;
376
0
  bitmapCache->cells = (BITMAP_V2_CELL*)calloc(BitmapCacheV2NumCells, sizeof(BITMAP_V2_CELL));
377
378
0
  if (!bitmapCache->cells)
379
0
    goto fail;
380
0
  bitmapCache->maxCells = BitmapCacheV2NumCells;
381
382
0
  for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
383
0
  {
384
0
    const BITMAP_CACHE_V2_CELL_INFO* info =
385
0
        freerdp_settings_get_pointer_array(settings, FreeRDP_BitmapCacheV2CellInfo, i);
386
0
    BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
387
0
    UINT32 nr = info->numEntries;
388
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
389
0
    cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
390
391
0
    if (!cell->entries)
392
0
      goto fail;
393
0
    cell->number = nr;
394
0
  }
395
396
0
  return bitmapCache;
397
0
fail:
398
0
  WINPR_PRAGMA_DIAG_PUSH
399
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
400
0
  bitmap_cache_free(bitmapCache);
401
0
  WINPR_PRAGMA_DIAG_POP
402
0
  return NULL;
403
0
}
404
405
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
406
0
{
407
0
  if (!bitmapCache)
408
0
    return;
409
410
0
  bitmap_cache_save_persistent(bitmapCache);
411
412
0
  if (bitmapCache->cells)
413
0
  {
414
0
    for (UINT32 i = 0; i < bitmapCache->maxCells; i++)
415
0
    {
416
0
      UINT32 j = 0;
417
0
      BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
418
419
0
      if (!cell->entries)
420
0
        continue;
421
422
0
      for (j = 0; j < cell->number + 1; j++)
423
0
      {
424
0
        rdpBitmap* bitmap = cell->entries[j];
425
0
        Bitmap_Free(bitmapCache->context, bitmap);
426
0
      }
427
428
0
      free((void*)cell->entries);
429
0
    }
430
431
0
    free(bitmapCache->cells);
432
0
  }
433
434
0
  persistent_cache_free(bitmapCache->persistent);
435
436
0
  free(bitmapCache);
437
0
}
438
439
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
440
0
{
441
0
  if (!data)
442
0
    return;
443
444
0
  for (size_t x = 0; x < count; x++)
445
0
    free(data[x].bitmapDataStream);
446
447
0
  free(data);
448
0
}
449
450
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
451
0
{
452
0
  BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
453
454
0
  if (!dst)
455
0
    goto fail;
456
457
0
  for (size_t x = 0; x < count; x++)
458
0
  {
459
0
    dst[x] = data[x];
460
461
0
    if (data[x].bitmapLength > 0)
462
0
    {
463
0
      dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
464
465
0
      if (!dst[x].bitmapDataStream)
466
0
        goto fail;
467
468
0
      memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
469
0
    }
470
0
  }
471
472
0
  return dst;
473
0
fail:
474
0
  free_bitmap_data(dst, count);
475
0
  return NULL;
476
0
}
477
478
void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
479
                        WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
480
0
{
481
0
  if (!pointer)
482
0
    return;
483
484
0
  free_bitmap_data(pointer->rectangles, pointer->number);
485
0
  free(pointer);
486
0
}
487
488
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
489
0
{
490
0
  BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
491
492
0
  if (!dst || !pointer)
493
0
    goto fail;
494
495
0
  *dst = *pointer;
496
0
  dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
497
498
0
  if (!dst->rectangles)
499
0
    goto fail;
500
501
0
  return dst;
502
0
fail:
503
0
  WINPR_PRAGMA_DIAG_PUSH
504
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
505
0
  free_bitmap_update(context, dst);
506
0
  WINPR_PRAGMA_DIAG_POP
507
0
  return NULL;
508
0
}
509
510
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
511
0
{
512
0
  CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
513
514
0
  if (!dst || !order)
515
0
    goto fail;
516
517
0
  *dst = *order;
518
519
0
  if (order->bitmapLength > 0)
520
0
  {
521
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
522
523
0
    if (!dst->bitmapDataStream)
524
0
      goto fail;
525
526
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
527
0
  }
528
529
0
  return dst;
530
0
fail:
531
0
  WINPR_PRAGMA_DIAG_PUSH
532
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
533
0
  free_cache_bitmap_order(context, dst);
534
0
  WINPR_PRAGMA_DIAG_POP
535
0
  return NULL;
536
0
}
537
538
void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
539
0
{
540
0
  if (order)
541
0
    free(order->bitmapDataStream);
542
543
0
  free(order);
544
0
}
545
546
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
547
                                                  const CACHE_BITMAP_V2_ORDER* order)
548
0
{
549
0
  CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
550
551
0
  if (!dst || !order)
552
0
    goto fail;
553
554
0
  *dst = *order;
555
556
0
  if (order->bitmapLength > 0)
557
0
  {
558
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
559
560
0
    if (!dst->bitmapDataStream)
561
0
      goto fail;
562
563
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
564
0
  }
565
566
0
  return dst;
567
0
fail:
568
0
  WINPR_PRAGMA_DIAG_PUSH
569
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
570
0
  free_cache_bitmap_v2_order(context, dst);
571
0
  WINPR_PRAGMA_DIAG_POP
572
0
  return NULL;
573
0
}
574
575
void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
576
                                WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
577
0
{
578
0
  if (order)
579
0
    free(order->bitmapDataStream);
580
581
0
  free(order);
582
0
}
583
584
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
585
                                                  const CACHE_BITMAP_V3_ORDER* order)
586
0
{
587
0
  CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
588
589
0
  if (!dst || !order)
590
0
    goto fail;
591
592
0
  *dst = *order;
593
594
0
  if (order->bitmapData.length > 0)
595
0
  {
596
0
    dst->bitmapData.data = malloc(order->bitmapData.length);
597
598
0
    if (!dst->bitmapData.data)
599
0
      goto fail;
600
601
0
    memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
602
0
  }
603
604
0
  return dst;
605
0
fail:
606
0
  WINPR_PRAGMA_DIAG_PUSH
607
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
608
0
  free_cache_bitmap_v3_order(context, dst);
609
0
  WINPR_PRAGMA_DIAG_POP
610
0
  return NULL;
611
0
}
612
613
void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
614
0
{
615
0
  if (order)
616
0
    free(order->bitmapData.data);
617
618
0
  free(order);
619
0
}