Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/cache/palette.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Palette (Color Table) Cache
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
26
#include <freerdp/log.h>
27
28
#include "palette.h"
29
#include "cache.h"
30
31
#define TAG FREERDP_TAG("cache.palette")
32
33
static void palette_cache_put(rdpPaletteCache* palette, UINT32 index, void* entry);
34
35
static BOOL update_gdi_cache_color_table(rdpContext* context,
36
                                         const CACHE_COLOR_TABLE_ORDER* cacheColorTable)
37
0
{
38
0
  UINT32* colorTable = NULL;
39
0
  rdpCache* cache = context->cache;
40
0
  colorTable = (UINT32*)malloc(sizeof(UINT32) * 256);
41
42
0
  if (!colorTable)
43
0
    return FALSE;
44
45
0
  CopyMemory(colorTable, cacheColorTable->colorTable, sizeof(UINT32) * 256);
46
0
  palette_cache_put(cache->palette, cacheColorTable->cacheIndex, (void*)colorTable);
47
0
  return TRUE;
48
0
}
49
50
void palette_cache_put(rdpPaletteCache* paletteCache, UINT32 index, void* entry)
51
0
{
52
0
  if (index >= paletteCache->maxEntries)
53
0
  {
54
0
    WLog_ERR(TAG, "invalid color table index: 0x%08" PRIX32 "", index);
55
0
    free(entry);
56
0
    return;
57
0
  }
58
59
0
  free(paletteCache->entries[index].entry);
60
0
  paletteCache->entries[index].entry = entry;
61
0
}
62
63
void palette_cache_register_callbacks(rdpUpdate* update)
64
0
{
65
0
  WINPR_ASSERT(update);
66
0
  WINPR_ASSERT(update->secondary);
67
0
  update->secondary->CacheColorTable = update_gdi_cache_color_table;
68
0
}
69
70
rdpPaletteCache* palette_cache_new(rdpContext* context)
71
0
{
72
0
  rdpPaletteCache* paletteCache = NULL;
73
74
0
  WINPR_ASSERT(context);
75
76
0
  paletteCache = (rdpPaletteCache*)calloc(1, sizeof(rdpPaletteCache));
77
78
0
  if (paletteCache)
79
0
  {
80
0
    paletteCache->context = context;
81
0
    paletteCache->maxEntries = 6;
82
0
    paletteCache->entries =
83
0
        (PALETTE_TABLE_ENTRY*)calloc(paletteCache->maxEntries, sizeof(PALETTE_TABLE_ENTRY));
84
0
  }
85
86
0
  return paletteCache;
87
0
}
88
89
void palette_cache_free(rdpPaletteCache* paletteCache)
90
0
{
91
0
  if (paletteCache)
92
0
  {
93
0
    for (UINT32 i = 0; i < paletteCache->maxEntries; i++)
94
0
      free(paletteCache->entries[i].entry);
95
96
0
    free(paletteCache->entries);
97
0
    free(paletteCache);
98
0
  }
99
0
}
100
101
void free_palette_update(WINPR_ATTR_UNUSED rdpContext* context, PALETTE_UPDATE* pointer)
102
0
{
103
0
  free(pointer);
104
0
}
105
106
PALETTE_UPDATE* copy_palette_update(rdpContext* context, const PALETTE_UPDATE* pointer)
107
0
{
108
0
  PALETTE_UPDATE* dst = calloc(1, sizeof(PALETTE_UPDATE));
109
110
0
  if (!dst || !pointer)
111
0
    goto fail;
112
113
0
  *dst = *pointer;
114
0
  return dst;
115
0
fail:
116
0
  WINPR_PRAGMA_DIAG_PUSH
117
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
118
0
  free_palette_update(context, dst);
119
0
  WINPR_PRAGMA_DIAG_POP
120
0
  return NULL;
121
0
}