1 | /* -*- c-basic-offset: 8 -*-
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | Persistent Bitmap Cache routines
|
---|
4 | Copyright (C) Jeroen Meijer 2004-2007
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
24 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
25 | * a choice of GPL license versions is made available with the language indicating
|
---|
26 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the GPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "rdesktop.h"
|
---|
31 |
|
---|
32 | #define MAX_CELL_SIZE 0x1000 /* pixels */
|
---|
33 |
|
---|
34 | #define IS_PERSISTENT(id) (id < 8 && g_pstcache_fd[id] > 0)
|
---|
35 |
|
---|
36 | extern int g_server_depth;
|
---|
37 | extern RD_BOOL g_bitmap_cache;
|
---|
38 | extern RD_BOOL g_bitmap_cache_persist_enable;
|
---|
39 | extern RD_BOOL g_bitmap_cache_precache;
|
---|
40 |
|
---|
41 | int g_pstcache_fd[8];
|
---|
42 | int g_pstcache_Bpp;
|
---|
43 | RD_BOOL g_pstcache_enumerated = False;
|
---|
44 | uint8 zero_key[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
---|
45 |
|
---|
46 |
|
---|
47 | /* Update mru stamp/index for a bitmap */
|
---|
48 | void
|
---|
49 | pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp)
|
---|
50 | {
|
---|
51 | int fd;
|
---|
52 |
|
---|
53 | if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
|
---|
54 | return;
|
---|
55 |
|
---|
56 | fd = g_pstcache_fd[cache_id];
|
---|
57 | rd_lseek_file(fd, 12 + cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
---|
58 | rd_write_file(fd, &stamp, sizeof(stamp));
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* Load a bitmap from the persistent cache */
|
---|
62 | RD_BOOL
|
---|
63 | pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx)
|
---|
64 | {
|
---|
65 | uint8 *celldata;
|
---|
66 | int fd;
|
---|
67 | CELLHEADER cellhdr;
|
---|
68 | RD_HBITMAP bitmap;
|
---|
69 |
|
---|
70 | if (!g_bitmap_cache_persist_enable)
|
---|
71 | return False;
|
---|
72 |
|
---|
73 | if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
|
---|
74 | return False;
|
---|
75 |
|
---|
76 | fd = g_pstcache_fd[cache_id];
|
---|
77 | rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
---|
78 | rd_read_file(fd, &cellhdr, sizeof(CELLHEADER));
|
---|
79 | celldata = (uint8 *) xmalloc(cellhdr.length);
|
---|
80 | rd_read_file(fd, celldata, cellhdr.length);
|
---|
81 |
|
---|
82 | bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata);
|
---|
83 | DEBUG(("Load bitmap from disk: id=%d, idx=%d, bmp=0x%x)\n", cache_id, cache_idx, bitmap));
|
---|
84 | cache_put_bitmap(cache_id, cache_idx, bitmap);
|
---|
85 |
|
---|
86 | xfree(celldata);
|
---|
87 | return True;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /* Store a bitmap in the persistent cache */
|
---|
91 | RD_BOOL
|
---|
92 | pstcache_save_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * key,
|
---|
93 | uint8 width, uint8 height, uint16 length, uint8 * data)
|
---|
94 | {
|
---|
95 | int fd;
|
---|
96 | CELLHEADER cellhdr;
|
---|
97 |
|
---|
98 | if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
|
---|
99 | return False;
|
---|
100 |
|
---|
101 | memcpy(cellhdr.key, key, sizeof(HASH_KEY));
|
---|
102 | cellhdr.width = width;
|
---|
103 | cellhdr.height = height;
|
---|
104 | cellhdr.length = length;
|
---|
105 | cellhdr.stamp = 0;
|
---|
106 |
|
---|
107 | fd = g_pstcache_fd[cache_id];
|
---|
108 | rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
---|
109 | rd_write_file(fd, &cellhdr, sizeof(CELLHEADER));
|
---|
110 | rd_write_file(fd, data, length);
|
---|
111 |
|
---|
112 | return True;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* List the bitmap keys from the persistent cache file */
|
---|
116 | int
|
---|
117 | pstcache_enumerate(uint8 id, HASH_KEY * keylist)
|
---|
118 | {
|
---|
119 | int fd, n;
|
---|
120 | uint16 idx;
|
---|
121 | sint16 mru_idx[0xa00];
|
---|
122 | uint32 mru_stamp[0xa00];
|
---|
123 | CELLHEADER cellhdr;
|
---|
124 |
|
---|
125 | if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(id)))
|
---|
126 | return 0;
|
---|
127 |
|
---|
128 | /* The server disconnects if the bitmap cache content is sent more than once */
|
---|
129 | if (g_pstcache_enumerated)
|
---|
130 | return 0;
|
---|
131 |
|
---|
132 | DEBUG_RDP5(("Persistent bitmap cache enumeration... "));
|
---|
133 | for (idx = 0; idx < BMPCACHE2_NUM_PSTCELLS; idx++)
|
---|
134 | {
|
---|
135 | fd = g_pstcache_fd[id];
|
---|
136 | rd_lseek_file(fd, idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
---|
137 | if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0)
|
---|
138 | break;
|
---|
139 |
|
---|
140 | if (memcmp(cellhdr.key, zero_key, sizeof(HASH_KEY)) != 0)
|
---|
141 | {
|
---|
142 | memcpy(keylist[idx], cellhdr.key, sizeof(HASH_KEY));
|
---|
143 |
|
---|
144 | /* Pre-cache (not possible for 8 bit colour depth cause it needs a colourmap) */
|
---|
145 | if (g_bitmap_cache_precache && cellhdr.stamp && g_server_depth > 8)
|
---|
146 | pstcache_load_bitmap(id, idx);
|
---|
147 |
|
---|
148 | /* Sort by stamp */
|
---|
149 | for (n = idx; n > 0 && cellhdr.stamp < mru_stamp[n - 1]; n--)
|
---|
150 | {
|
---|
151 | mru_idx[n] = mru_idx[n - 1];
|
---|
152 | mru_stamp[n] = mru_stamp[n - 1];
|
---|
153 | }
|
---|
154 |
|
---|
155 | mru_idx[n] = idx;
|
---|
156 | mru_stamp[n] = cellhdr.stamp;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | break;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | DEBUG_RDP5(("%d cached bitmaps.\n", idx));
|
---|
165 |
|
---|
166 | cache_rebuild_bmpcache_linked_list(id, mru_idx, idx);
|
---|
167 | g_pstcache_enumerated = True;
|
---|
168 | return idx;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* initialise the persistent bitmap cache */
|
---|
172 | RD_BOOL
|
---|
173 | pstcache_init(uint8 cache_id)
|
---|
174 | {
|
---|
175 | int fd;
|
---|
176 | char filename[256];
|
---|
177 |
|
---|
178 | if (g_pstcache_enumerated)
|
---|
179 | return True;
|
---|
180 |
|
---|
181 | g_pstcache_fd[cache_id] = 0;
|
---|
182 |
|
---|
183 | if (!(g_bitmap_cache && g_bitmap_cache_persist_enable))
|
---|
184 | return False;
|
---|
185 |
|
---|
186 | if (!rd_pstcache_mkdir())
|
---|
187 | {
|
---|
188 | DEBUG(("failed to get/make cache directory!\n"));
|
---|
189 | return False;
|
---|
190 | }
|
---|
191 |
|
---|
192 | g_pstcache_Bpp = (g_server_depth + 7) / 8;
|
---|
193 | sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp);
|
---|
194 | DEBUG(("persistent bitmap cache file: %s\n", filename));
|
---|
195 |
|
---|
196 | fd = rd_open_file(filename);
|
---|
197 | if (fd == -1)
|
---|
198 | return False;
|
---|
199 |
|
---|
200 | if (!rd_lock_file(fd, 0, 0))
|
---|
201 | {
|
---|
202 | warning("Persistent bitmap caching is disabled. (The file is already in use)\n");
|
---|
203 | rd_close_file(fd);
|
---|
204 | return False;
|
---|
205 | }
|
---|
206 |
|
---|
207 | g_pstcache_fd[cache_id] = fd;
|
---|
208 | return True;
|
---|
209 | }
|
---|