Coverage Report

Created: 2026-09-14 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
    BITMAP_V2_CELL* cell = &bitmapCache->cells[i];
426
0
    UINT32 nr = info->numEntries;
427
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
428
0
    cell->entries = (rdpBitmap**)calloc((nr + 1), sizeof(rdpBitmap*));
429
430
0
    if (!cell->entries)
431
0
      return FALSE;
432
0
    cell->number = nr;
433
0
  }
434
435
  /* initialize the overallocated extra slot for old RDP servers that send
436
   * cacheId == maxCells; use a minimal allocation since no protocol-negotiated
437
   * capacity exists for this slot */
438
0
  {
439
0
    BITMAP_V2_CELL* extra = &bitmapCache->cells[bitmapCache->maxCells];
440
    /* allocate an extra entry for BITMAP_CACHE_WAITING_LIST_INDEX */
441
0
    extra->entries = (rdpBitmap**)calloc(1, sizeof(rdpBitmap*));
442
443
0
    if (!extra->entries)
444
0
      return FALSE;
445
0
    extra->number = 0;
446
0
  }
447
448
0
  return TRUE;
449
0
}
450
451
rdpBitmapCache* bitmap_cache_new(rdpContext* context)
452
0
{
453
0
  WINPR_ASSERT(context);
454
455
0
  rdpBitmapCache* bitmapCache = (rdpBitmapCache*)calloc(1, sizeof(rdpBitmapCache));
456
457
0
  if (!bitmapCache)
458
0
    return nullptr;
459
460
0
  bitmapCache->context = context;
461
462
0
  if (!bitmap_cache_resize(bitmapCache))
463
0
    goto fail;
464
465
0
  return bitmapCache;
466
0
fail:
467
0
  WINPR_PRAGMA_DIAG_PUSH
468
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
469
0
  bitmap_cache_free(bitmapCache);
470
0
  WINPR_PRAGMA_DIAG_POP
471
0
  return nullptr;
472
0
}
473
474
void bitmap_cache_free(rdpBitmapCache* bitmapCache)
475
0
{
476
0
  if (!bitmapCache)
477
0
    return;
478
479
0
  bitmap_cache_save_persistent(bitmapCache);
480
481
0
  bitmap_cache_cell_free(bitmapCache);
482
483
0
  persistent_cache_free(bitmapCache->persistent);
484
485
0
  free(bitmapCache);
486
0
}
487
488
static void free_bitmap_data(BITMAP_DATA* data, size_t count)
489
0
{
490
0
  if (!data)
491
0
    return;
492
493
0
  for (size_t x = 0; x < count; x++)
494
0
    free(data[x].bitmapDataStream);
495
496
0
  free(data);
497
0
}
498
499
static BITMAP_DATA* copy_bitmap_data(const BITMAP_DATA* data, size_t count)
500
0
{
501
0
  BITMAP_DATA* dst = (BITMAP_DATA*)calloc(count, sizeof(BITMAP_DATA));
502
503
0
  if (!dst)
504
0
    goto fail;
505
506
0
  for (size_t x = 0; x < count; x++)
507
0
  {
508
0
    dst[x] = data[x];
509
510
0
    if (data[x].bitmapLength > 0)
511
0
    {
512
0
      dst[x].bitmapDataStream = malloc(data[x].bitmapLength);
513
514
0
      if (!dst[x].bitmapDataStream)
515
0
        goto fail;
516
517
0
      memcpy(dst[x].bitmapDataStream, data[x].bitmapDataStream, data[x].bitmapLength);
518
0
    }
519
0
  }
520
521
0
  return dst;
522
0
fail:
523
0
  free_bitmap_data(dst, count);
524
0
  return nullptr;
525
0
}
526
527
void free_bitmap_update(WINPR_ATTR_UNUSED rdpContext* context,
528
                        WINPR_ATTR_UNUSED BITMAP_UPDATE* pointer)
529
0
{
530
0
  if (!pointer)
531
0
    return;
532
533
0
  free_bitmap_data(pointer->rectangles, pointer->number);
534
0
  free(pointer);
535
0
}
536
537
BITMAP_UPDATE* copy_bitmap_update(rdpContext* context, const BITMAP_UPDATE* pointer)
538
0
{
539
0
  BITMAP_UPDATE* dst = calloc(1, sizeof(BITMAP_UPDATE));
540
541
0
  if (!dst || !pointer)
542
0
    goto fail;
543
544
0
  *dst = *pointer;
545
0
  dst->rectangles = copy_bitmap_data(pointer->rectangles, pointer->number);
546
547
0
  if (!dst->rectangles)
548
0
    goto fail;
549
550
0
  return dst;
551
0
fail:
552
0
  WINPR_PRAGMA_DIAG_PUSH
553
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
554
0
  free_bitmap_update(context, dst);
555
0
  WINPR_PRAGMA_DIAG_POP
556
0
  return nullptr;
557
0
}
558
559
CACHE_BITMAP_ORDER* copy_cache_bitmap_order(rdpContext* context, const CACHE_BITMAP_ORDER* order)
560
0
{
561
0
  CACHE_BITMAP_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_ORDER));
562
563
0
  if (!dst || !order)
564
0
    goto fail;
565
566
0
  *dst = *order;
567
568
0
  if (order->bitmapLength > 0)
569
0
  {
570
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
571
572
0
    if (!dst->bitmapDataStream)
573
0
      goto fail;
574
575
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
576
0
  }
577
578
0
  return dst;
579
0
fail:
580
0
  WINPR_PRAGMA_DIAG_PUSH
581
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
582
0
  free_cache_bitmap_order(context, dst);
583
0
  WINPR_PRAGMA_DIAG_POP
584
0
  return nullptr;
585
0
}
586
587
void free_cache_bitmap_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_ORDER* order)
588
0
{
589
0
  if (order)
590
0
    free(order->bitmapDataStream);
591
592
0
  free(order);
593
0
}
594
595
CACHE_BITMAP_V2_ORDER* copy_cache_bitmap_v2_order(rdpContext* context,
596
                                                  const CACHE_BITMAP_V2_ORDER* order)
597
0
{
598
0
  CACHE_BITMAP_V2_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V2_ORDER));
599
600
0
  if (!dst || !order)
601
0
    goto fail;
602
603
0
  *dst = *order;
604
605
0
  if (order->bitmapLength > 0)
606
0
  {
607
0
    dst->bitmapDataStream = malloc(order->bitmapLength);
608
609
0
    if (!dst->bitmapDataStream)
610
0
      goto fail;
611
612
0
    memcpy(dst->bitmapDataStream, order->bitmapDataStream, order->bitmapLength);
613
0
  }
614
615
0
  return dst;
616
0
fail:
617
0
  WINPR_PRAGMA_DIAG_PUSH
618
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
619
0
  free_cache_bitmap_v2_order(context, dst);
620
0
  WINPR_PRAGMA_DIAG_POP
621
0
  return nullptr;
622
0
}
623
624
void free_cache_bitmap_v2_order(WINPR_ATTR_UNUSED rdpContext* context,
625
                                WINPR_ATTR_UNUSED CACHE_BITMAP_V2_ORDER* order)
626
0
{
627
0
  if (order)
628
0
    free(order->bitmapDataStream);
629
630
0
  free(order);
631
0
}
632
633
CACHE_BITMAP_V3_ORDER* copy_cache_bitmap_v3_order(rdpContext* context,
634
                                                  const CACHE_BITMAP_V3_ORDER* order)
635
0
{
636
0
  CACHE_BITMAP_V3_ORDER* dst = calloc(1, sizeof(CACHE_BITMAP_V3_ORDER));
637
638
0
  if (!dst || !order)
639
0
    goto fail;
640
641
0
  *dst = *order;
642
643
0
  if (order->bitmapData.length > 0)
644
0
  {
645
0
    dst->bitmapData.data = malloc(order->bitmapData.length);
646
647
0
    if (!dst->bitmapData.data)
648
0
      goto fail;
649
650
0
    memcpy(dst->bitmapData.data, order->bitmapData.data, order->bitmapData.length);
651
0
  }
652
653
0
  return dst;
654
0
fail:
655
0
  WINPR_PRAGMA_DIAG_PUSH
656
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
657
0
  free_cache_bitmap_v3_order(context, dst);
658
0
  WINPR_PRAGMA_DIAG_POP
659
0
  return nullptr;
660
0
}
661
662
void free_cache_bitmap_v3_order(WINPR_ATTR_UNUSED rdpContext* context, CACHE_BITMAP_V3_ORDER* order)
663
0
{
664
0
  if (order)
665
0
    free(order->bitmapData.data);
666
667
0
  free(order);
668
0
}