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