pstcache.c revision a180a41bba1d50822df23fff0099e90b86638b89
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* -*- c-basic-offset: 8 -*-
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rdesktop: A Remote Desktop Protocol client.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Persistent Bitmap Cache routines
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright (C) Jeroen Meijer <jeroen@oldambt7.com> 2004-2008
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync This program is free software: you can redistribute it and/or modify
a180a41bba1d50822df23fff0099e90b86638b89vboxsync it under the terms of the GNU General Public License as published by
a180a41bba1d50822df23fff0099e90b86638b89vboxsync the Free Software Foundation, either version 3 of the License, or
a180a41bba1d50822df23fff0099e90b86638b89vboxsync (at your option) any later version.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync This program is distributed in the hope that it will be useful,
a180a41bba1d50822df23fff0099e90b86638b89vboxsync but WITHOUT ANY WARRANTY; without even the implied warranty of
a180a41bba1d50822df23fff0099e90b86638b89vboxsync MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a180a41bba1d50822df23fff0099e90b86638b89vboxsync GNU General Public License for more details.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync You should have received a copy of the GNU General Public License
a180a41bba1d50822df23fff0099e90b86638b89vboxsync along with this program. If not, see <http://www.gnu.org/licenses/>.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync*/
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#include "rdesktop.h"
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define MAX_CELL_SIZE 0x1000 /* pixels */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define IS_PERSISTENT(id) (id < 8 && g_pstcache_fd[id] > 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncextern int g_server_depth;
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncextern RD_BOOL g_bitmap_cache;
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncextern RD_BOOL g_bitmap_cache_persist_enable;
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncextern RD_BOOL g_bitmap_cache_precache;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncint g_pstcache_fd[8];
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncint g_pstcache_Bpp;
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncRD_BOOL g_pstcache_enumerated = False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncuint8 zero_key[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Update mru stamp/index for a bitmap */
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncvoid
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncpstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync{
a180a41bba1d50822df23fff0099e90b86638b89vboxsync int fd;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync fd = g_pstcache_fd[cache_id];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_lseek_file(fd, 12 + cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_write_file(fd, &stamp, sizeof(stamp));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync}
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Load a bitmap from the persistent cache */
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncRD_BOOL
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncpstcache_load_bitmap(uint8 cache_id, uint16 cache_idx)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync{
a180a41bba1d50822df23fff0099e90b86638b89vboxsync uint8 *celldata;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync int fd;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync CELLHEADER cellhdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync RD_HBITMAP bitmap;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!g_bitmap_cache_persist_enable)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync fd = g_pstcache_fd[cache_id];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_read_file(fd, &cellhdr, sizeof(CELLHEADER));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync celldata = (uint8 *) xmalloc(cellhdr.length);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_read_file(fd, celldata, cellhdr.length);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync DEBUG(("Load bitmap from disk: id=%d, idx=%d, bmp=%p)\n", cache_id, cache_idx, bitmap));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cache_put_bitmap(cache_id, cache_idx, bitmap);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync xfree(celldata);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return True;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync}
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Store a bitmap in the persistent cache */
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncRD_BOOL
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncpstcache_save_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * key,
a180a41bba1d50822df23fff0099e90b86638b89vboxsync uint8 width, uint8 height, uint16 length, uint8 * data)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync{
a180a41bba1d50822df23fff0099e90b86638b89vboxsync int fd;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync CELLHEADER cellhdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync memcpy(cellhdr.key, key, sizeof(HASH_KEY));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cellhdr.width = width;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cellhdr.height = height;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cellhdr.length = length;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cellhdr.stamp = 0;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync fd = g_pstcache_fd[cache_id];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_write_file(fd, &cellhdr, sizeof(CELLHEADER));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_write_file(fd, data, length);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return True;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync}
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* List the bitmap keys from the persistent cache file */
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncint
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncpstcache_enumerate(uint8 id, HASH_KEY * keylist)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync{
a180a41bba1d50822df23fff0099e90b86638b89vboxsync int fd, n;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync uint16 idx;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync sint16 mru_idx[0xa00];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync uint32 mru_stamp[0xa00];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync CELLHEADER cellhdr;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(id)))
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return 0;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* The server disconnects if the bitmap cache content is sent more than once */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (g_pstcache_enumerated)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return 0;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync DEBUG_RDP5(("Persistent bitmap cache enumeration... "));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync for (idx = 0; idx < BMPCACHE2_NUM_PSTCELLS; idx++)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync {
a180a41bba1d50822df23fff0099e90b86638b89vboxsync fd = g_pstcache_fd[id];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_lseek_file(fd, idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync break;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (memcmp(cellhdr.key, zero_key, sizeof(HASH_KEY)) != 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync {
a180a41bba1d50822df23fff0099e90b86638b89vboxsync memcpy(keylist[idx], cellhdr.key, sizeof(HASH_KEY));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* Pre-cache (not possible for 8 bit colour depth cause it needs a colourmap) */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (g_bitmap_cache_precache && cellhdr.stamp && g_server_depth > 8)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync pstcache_load_bitmap(id, idx);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* Sort by stamp */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync for (n = idx; n > 0 && cellhdr.stamp < mru_stamp[n - 1]; n--)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync {
a180a41bba1d50822df23fff0099e90b86638b89vboxsync mru_idx[n] = mru_idx[n - 1];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync mru_stamp[n] = mru_stamp[n - 1];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync mru_idx[n] = idx;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync mru_stamp[n] = cellhdr.stamp;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync else
a180a41bba1d50822df23fff0099e90b86638b89vboxsync {
a180a41bba1d50822df23fff0099e90b86638b89vboxsync break;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync DEBUG_RDP5(("%d cached bitmaps.\n", idx));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cache_rebuild_bmpcache_linked_list(id, mru_idx, idx);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync g_pstcache_enumerated = True;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return idx;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync}
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* initialise the persistent bitmap cache */
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncRD_BOOL
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncpstcache_init(uint8 cache_id)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync{
a180a41bba1d50822df23fff0099e90b86638b89vboxsync int fd;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync char filename[256];
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (g_pstcache_enumerated)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return True;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync g_pstcache_fd[cache_id] = 0;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!(g_bitmap_cache && g_bitmap_cache_persist_enable))
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!rd_pstcache_mkdir())
a180a41bba1d50822df23fff0099e90b86638b89vboxsync {
a180a41bba1d50822df23fff0099e90b86638b89vboxsync DEBUG(("failed to get/make cache directory!\n"));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync g_pstcache_Bpp = (g_server_depth + 7) / 8;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync DEBUG(("persistent bitmap cache file: %s\n", filename));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync fd = rd_open_file(filename);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (fd == -1)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (!rd_lock_file(fd, 0, 0))
a180a41bba1d50822df23fff0099e90b86638b89vboxsync {
a180a41bba1d50822df23fff0099e90b86638b89vboxsync warning("Persistent bitmap caching is disabled. (The file is already in use)\n");
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rd_close_file(fd);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return False;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync }
a180a41bba1d50822df23fff0099e90b86638b89vboxsync
a180a41bba1d50822df23fff0099e90b86638b89vboxsync g_pstcache_fd[cache_id] = fd;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return True;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync}