parent
08eb5fdae6
commit
c20adcd743
@ -1,469 +0,0 @@
|
||||
/* -*- c-basic-offset: 8 -*-
|
||||
rdesktop: A Remote Desktop Protocol client.
|
||||
Protocol services - Multipoint Communications Service
|
||||
Copyright (C) Matthew Chapman 1999-2005
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "rdesktop.h"
|
||||
|
||||
uint16 g_mcs_userid;
|
||||
extern VCHANNEL g_channels[];
|
||||
extern unsigned int g_num_channels;
|
||||
|
||||
/* Parse an ASN.1 BER header */
|
||||
static BOOL
|
||||
ber_parse_header(STREAM s, int tagval, int *length)
|
||||
{
|
||||
int tag, len;
|
||||
|
||||
if (tagval > 0xff)
|
||||
{
|
||||
in_uint16_be(s, tag);
|
||||
}
|
||||
else
|
||||
{
|
||||
in_uint8(s, tag)}
|
||||
|
||||
if (tag != tagval)
|
||||
{
|
||||
error("expected tag %d, got %d\n", tagval, tag);
|
||||
return False;
|
||||
}
|
||||
|
||||
in_uint8(s, len);
|
||||
|
||||
if (len & 0x80)
|
||||
{
|
||||
len &= ~0x80;
|
||||
*length = 0;
|
||||
while (len--)
|
||||
next_be(s, *length);
|
||||
}
|
||||
else
|
||||
*length = len;
|
||||
|
||||
return s_check(s);
|
||||
}
|
||||
|
||||
/* Output an ASN.1 BER header */
|
||||
static void
|
||||
ber_out_header(STREAM s, int tagval, int length)
|
||||
{
|
||||
if (tagval > 0xff)
|
||||
{
|
||||
out_uint16_be(s, tagval);
|
||||
}
|
||||
else
|
||||
{
|
||||
out_uint8(s, tagval);
|
||||
}
|
||||
|
||||
if (length >= 0x80)
|
||||
{
|
||||
out_uint8(s, 0x82);
|
||||
out_uint16_be(s, length);
|
||||
}
|
||||
else
|
||||
out_uint8(s, length);
|
||||
}
|
||||
|
||||
/* Output an ASN.1 BER integer */
|
||||
static void
|
||||
ber_out_integer(STREAM s, int value)
|
||||
{
|
||||
ber_out_header(s, BER_TAG_INTEGER, 2);
|
||||
out_uint16_be(s, value);
|
||||
}
|
||||
|
||||
/* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
|
||||
static void
|
||||
mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
|
||||
{
|
||||
ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
|
||||
ber_out_integer(s, max_channels);
|
||||
ber_out_integer(s, max_users);
|
||||
ber_out_integer(s, max_tokens);
|
||||
ber_out_integer(s, 1); /* num_priorities */
|
||||
ber_out_integer(s, 0); /* min_throughput */
|
||||
ber_out_integer(s, 1); /* max_height */
|
||||
ber_out_integer(s, max_pdusize);
|
||||
ber_out_integer(s, 2); /* ver_protocol */
|
||||
}
|
||||
|
||||
/* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
|
||||
static BOOL
|
||||
mcs_parse_domain_params(STREAM s)
|
||||
{
|
||||
int length;
|
||||
|
||||
ber_parse_header(s, MCS_TAG_DOMAIN_PARAMS, &length);
|
||||
in_uint8s(s, length);
|
||||
|
||||
return s_check(s);
|
||||
}
|
||||
|
||||
/* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
|
||||
static void
|
||||
mcs_send_connect_initial(STREAM mcs_data)
|
||||
{
|
||||
int datalen = mcs_data->end - mcs_data->data;
|
||||
int length = 9 + 3 * 34 + 4 + datalen;
|
||||
STREAM s;
|
||||
|
||||
s = iso_init(length + 5);
|
||||
|
||||
ber_out_header(s, MCS_CONNECT_INITIAL, length);
|
||||
ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* calling domain */
|
||||
out_uint8(s, 1);
|
||||
ber_out_header(s, BER_TAG_OCTET_STRING, 1); /* called domain */
|
||||
out_uint8(s, 1);
|
||||
|
||||
ber_out_header(s, BER_TAG_BOOLEAN, 1);
|
||||
out_uint8(s, 0xff); /* upward flag */
|
||||
|
||||
mcs_out_domain_params(s, 34, 2, 0, 0xffff); /* target params */
|
||||
mcs_out_domain_params(s, 1, 1, 1, 0x420); /* min params */
|
||||
mcs_out_domain_params(s, 0xffff, 0xfc17, 0xffff, 0xffff); /* max params */
|
||||
|
||||
ber_out_header(s, BER_TAG_OCTET_STRING, datalen);
|
||||
out_uint8p(s, mcs_data->data, datalen);
|
||||
|
||||
s_mark_end(s);
|
||||
iso_send(s);
|
||||
}
|
||||
|
||||
/* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
|
||||
static BOOL
|
||||
mcs_recv_connect_response(STREAM mcs_data)
|
||||
{
|
||||
uint8 result;
|
||||
int length;
|
||||
STREAM s;
|
||||
|
||||
s = iso_recv(NULL);
|
||||
if (s == NULL)
|
||||
return False;
|
||||
|
||||
ber_parse_header(s, MCS_CONNECT_RESPONSE, &length);
|
||||
|
||||
ber_parse_header(s, BER_TAG_RESULT, &length);
|
||||
in_uint8(s, result);
|
||||
if (result != 0)
|
||||
{
|
||||
error("MCS connect: %d\n", result);
|
||||
return False;
|
||||
}
|
||||
|
||||
ber_parse_header(s, BER_TAG_INTEGER, &length);
|
||||
in_uint8s(s, length); /* connect id */
|
||||
mcs_parse_domain_params(s);
|
||||
|
||||
ber_parse_header(s, BER_TAG_OCTET_STRING, &length);
|
||||
|
||||
sec_process_mcs_data(s);
|
||||
/*
|
||||
if (length > mcs_data->size)
|
||||
{
|
||||
error("MCS data length %d, expected %d\n", length,
|
||||
mcs_data->size);
|
||||
length = mcs_data->size;
|
||||
}
|
||||
|
||||
in_uint8a(s, mcs_data->data, length);
|
||||
mcs_data->p = mcs_data->data;
|
||||
mcs_data->end = mcs_data->data + length;
|
||||
*/
|
||||
return s_check_end(s);
|
||||
}
|
||||
|
||||
/* Send an EDrq message (ASN.1 PER) */
|
||||
static void
|
||||
mcs_send_edrq(void)
|
||||
{
|
||||
STREAM s;
|
||||
|
||||
s = iso_init(5);
|
||||
|
||||
out_uint8(s, (MCS_EDRQ << 2));
|
||||
out_uint16_be(s, 1); /* height */
|
||||
out_uint16_be(s, 1); /* interval */
|
||||
|
||||
s_mark_end(s);
|
||||
iso_send(s);
|
||||
}
|
||||
|
||||
/* Send an AUrq message (ASN.1 PER) */
|
||||
static void
|
||||
mcs_send_aurq(void)
|
||||
{
|
||||
STREAM s;
|
||||
|
||||
s = iso_init(1);
|
||||
|
||||
out_uint8(s, (MCS_AURQ << 2));
|
||||
|
||||
s_mark_end(s);
|
||||
iso_send(s);
|
||||
}
|
||||
|
||||
/* Expect a AUcf message (ASN.1 PER) */
|
||||
static BOOL
|
||||
mcs_recv_aucf(uint16 * mcs_userid)
|
||||
{
|
||||
uint8 opcode, result;
|
||||
STREAM s;
|
||||
|
||||
s = iso_recv(NULL);
|
||||
if (s == NULL)
|
||||
return False;
|
||||
|
||||
in_uint8(s, opcode);
|
||||
if ((opcode >> 2) != MCS_AUCF)
|
||||
{
|
||||
error("expected AUcf, got %d\n", opcode);
|
||||
return False;
|
||||
}
|
||||
|
||||
in_uint8(s, result);
|
||||
if (result != 0)
|
||||
{
|
||||
error("AUrq: %d\n", result);
|
||||
return False;
|
||||
}
|
||||
|
||||
if (opcode & 2)
|
||||
in_uint16_be(s, *mcs_userid);
|
||||
|
||||
return s_check_end(s);
|
||||
}
|
||||
|
||||
/* Send a CJrq message (ASN.1 PER) */
|
||||
static void
|
||||
mcs_send_cjrq(uint16 chanid)
|
||||
{
|
||||
STREAM s;
|
||||
|
||||
DEBUG_RDP5(("Sending CJRQ for channel #%d\n", chanid));
|
||||
|
||||
s = iso_init(5);
|
||||
|
||||
out_uint8(s, (MCS_CJRQ << 2));
|
||||
out_uint16_be(s, g_mcs_userid);
|
||||
out_uint16_be(s, chanid);
|
||||
|
||||
s_mark_end(s);
|
||||
iso_send(s);
|
||||
}
|
||||
|
||||
/* Expect a CJcf message (ASN.1 PER) */
|
||||
static BOOL
|
||||
mcs_recv_cjcf(void)
|
||||
{
|
||||
uint8 opcode, result;
|
||||
STREAM s;
|
||||
|
||||
s = iso_recv(NULL);
|
||||
if (s == NULL)
|
||||
return False;
|
||||
|
||||
in_uint8(s, opcode);
|
||||
if ((opcode >> 2) != MCS_CJCF)
|
||||
{
|
||||
error("expected CJcf, got %d\n", opcode);
|
||||
return False;
|
||||
}
|
||||
|
||||
in_uint8(s, result);
|
||||
if (result != 0)
|
||||
{
|
||||
error("CJrq: %d\n", result);
|
||||
return False;
|
||||
}
|
||||
|
||||
in_uint8s(s, 4); /* mcs_userid, req_chanid */
|
||||
if (opcode & 2)
|
||||
in_uint8s(s, 2); /* join_chanid */
|
||||
|
||||
return s_check_end(s);
|
||||
}
|
||||
|
||||
/* Initialise an MCS transport data packet */
|
||||
STREAM
|
||||
mcs_init(int length)
|
||||
{
|
||||
STREAM s;
|
||||
|
||||
s = iso_init(length + 8);
|
||||
s_push_layer(s, mcs_hdr, 8);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Send an MCS transport data packet to a specific channel */
|
||||
void
|
||||
mcs_send_to_channel(STREAM s, uint16 channel)
|
||||
{
|
||||
uint16 length;
|
||||
|
||||
s_pop_layer(s, mcs_hdr);
|
||||
length = s->end - s->p - 8;
|
||||
length |= 0x8000;
|
||||
|
||||
out_uint8(s, (MCS_SDRQ << 2));
|
||||
out_uint16_be(s, g_mcs_userid);
|
||||
out_uint16_be(s, channel);
|
||||
out_uint8(s, 0x70); /* flags */
|
||||
out_uint16_be(s, length);
|
||||
|
||||
iso_send(s);
|
||||
}
|
||||
|
||||
/* Send an MCS transport data packet to the global channel */
|
||||
void
|
||||
mcs_send(STREAM s)
|
||||
{
|
||||
mcs_send_to_channel(s, MCS_GLOBAL_CHANNEL);
|
||||
}
|
||||
|
||||
/* Receive an MCS transport data packet */
|
||||
STREAM
|
||||
mcs_recv(uint16 * channel, uint8 * rdpver)
|
||||
{
|
||||
uint8 opcode, appid, length;
|
||||
STREAM s;
|
||||
|
||||
s = iso_recv(rdpver);
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
if (rdpver != NULL)
|
||||
if (*rdpver != 3)
|
||||
return s;
|
||||
in_uint8(s, opcode);
|
||||
appid = opcode >> 2;
|
||||
if (appid != MCS_SDIN)
|
||||
{
|
||||
if (appid != MCS_DPUM)
|
||||
{
|
||||
error("expected data, got %d\n", opcode);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
in_uint8s(s, 2); /* userid */
|
||||
in_uint16_be(s, *channel);
|
||||
in_uint8s(s, 1); /* flags */
|
||||
in_uint8(s, length);
|
||||
if (length & 0x80)
|
||||
in_uint8s(s, 1); /* second byte of length */
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Establish a connection up to the MCS layer */
|
||||
BOOL
|
||||
mcs_connect(char *server, STREAM mcs_data, char *username)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (!iso_connect(server, username))
|
||||
return False;
|
||||
|
||||
mcs_send_connect_initial(mcs_data);
|
||||
if (!mcs_recv_connect_response(mcs_data))
|
||||
goto error;
|
||||
|
||||
mcs_send_edrq();
|
||||
|
||||
mcs_send_aurq();
|
||||
if (!mcs_recv_aucf(&g_mcs_userid))
|
||||
goto error;
|
||||
|
||||
mcs_send_cjrq((uint16) (g_mcs_userid + MCS_USERCHANNEL_BASE));
|
||||
|
||||
if (!mcs_recv_cjcf())
|
||||
goto error;
|
||||
|
||||
mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
|
||||
if (!mcs_recv_cjcf())
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < g_num_channels; i++)
|
||||
{
|
||||
mcs_send_cjrq(g_channels[i].mcs_id);
|
||||
if (!mcs_recv_cjcf())
|
||||
goto error;
|
||||
}
|
||||
return True;
|
||||
|
||||
error:
|
||||
iso_disconnect();
|
||||
return False;
|
||||
}
|
||||
|
||||
/* Establish a connection up to the MCS layer */
|
||||
BOOL
|
||||
mcs_reconnect(char *server, STREAM mcs_data)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
if (!iso_reconnect(server))
|
||||
return False;
|
||||
|
||||
mcs_send_connect_initial(mcs_data);
|
||||
if (!mcs_recv_connect_response(mcs_data))
|
||||
goto error;
|
||||
|
||||
mcs_send_edrq();
|
||||
|
||||
mcs_send_aurq();
|
||||
if (!mcs_recv_aucf(&g_mcs_userid))
|
||||
goto error;
|
||||
|
||||
mcs_send_cjrq((uint16) (g_mcs_userid + MCS_USERCHANNEL_BASE));
|
||||
|
||||
if (!mcs_recv_cjcf())
|
||||
goto error;
|
||||
|
||||
mcs_send_cjrq(MCS_GLOBAL_CHANNEL);
|
||||
if (!mcs_recv_cjcf())
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < g_num_channels; i++)
|
||||
{
|
||||
mcs_send_cjrq(g_channels[i].mcs_id);
|
||||
if (!mcs_recv_cjcf())
|
||||
goto error;
|
||||
}
|
||||
return True;
|
||||
|
||||
error:
|
||||
iso_disconnect();
|
||||
return False;
|
||||
}
|
||||
|
||||
/* Disconnect from the MCS layer */
|
||||
void
|
||||
mcs_disconnect(void)
|
||||
{
|
||||
iso_disconnect();
|
||||
}
|
||||
|
||||
/* reset the state of the mcs layer */
|
||||
void
|
||||
mcs_reset_state(void)
|
||||
{
|
||||
g_mcs_userid = 0;
|
||||
iso_reset_state();
|
||||
}
|
@ -1,380 +0,0 @@
|
||||
/* -*- c-basic-offset: 8 -*-
|
||||
rdesktop: A Remote Desktop Protocol client.
|
||||
Protocol services - RDP decompression
|
||||
Copyright (C) Matthew Chapman 1999-2005
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "rdesktop.h"
|
||||
|
||||
/* mppc decompression */
|
||||
/* http://www.faqs.org/rfcs/rfc2118.html */
|
||||
|
||||
/* Contacts: */
|
||||
|
||||
/* hifn contact mentioned in the faq is */
|
||||
/* Robert Friend rfriend at hifn dot com */
|
||||
|
||||
/* if you have questions regarding MPPC */
|
||||
/* our contact is */
|
||||
/* Guus Dhaeze GDhaeze at hifn dot com */
|
||||
|
||||
/* Licensing: */
|
||||
|
||||
/* decompression is alright as long as we */
|
||||
/* don't compress data */
|
||||
|
||||
/* Algorithm: */
|
||||
|
||||
/* as the rfc states the algorithm seems to */
|
||||
/* be LZ77 with a sliding buffer */
|
||||
/* that is empty at init. */
|
||||
|
||||
/* the algorithm is called LZS and is */
|
||||
/* patented for another couple of years. */
|
||||
|
||||
/* more information is available in */
|
||||
/* http://www.ietf.org/ietf/IPR/hifn-ipr-draft-friend-tls-lzs-compression.txt */
|
||||
|
||||
|
||||
RDPCOMP g_mppc_dict;
|
||||
|
||||
int
|
||||
mppc_expand(uint8 * data, uint32 clen, uint8 ctype, uint32 * roff, uint32 * rlen)
|
||||
{
|
||||
int k, walker_len = 0, walker;
|
||||
uint32 i = 0;
|
||||
int next_offset, match_off;
|
||||
int match_len;
|
||||
int old_offset, match_bits;
|
||||
BOOL big = ctype & RDP_MPPC_BIG ? True : False;
|
||||
|
||||
uint8 *dict = g_mppc_dict.hist;
|
||||
|
||||
if ((ctype & RDP_MPPC_COMPRESSED) == 0)
|
||||
{
|
||||
*roff = 0;
|
||||
*rlen = clen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((ctype & RDP_MPPC_RESET) != 0)
|
||||
{
|
||||
g_mppc_dict.roff = 0;
|
||||
}
|
||||
|
||||
if ((ctype & RDP_MPPC_FLUSH) != 0)
|
||||
{
|
||||
memset(dict, 0, RDP_MPPC_DICT_SIZE);
|
||||
g_mppc_dict.roff = 0;
|
||||
}
|
||||
|
||||
*roff = 0;
|
||||
*rlen = 0;
|
||||
|
||||
walker = g_mppc_dict.roff;
|
||||
|
||||
next_offset = walker;
|
||||
old_offset = next_offset;
|
||||
*roff = old_offset;
|
||||
if (clen == 0)
|
||||
return 0;
|
||||
clen += i;
|
||||
|
||||
do
|
||||
{
|
||||
if (walker_len == 0)
|
||||
{
|
||||
if (i >= clen)
|
||||
break;
|
||||
walker = data[i++] << 24;
|
||||
walker_len = 8;
|
||||
}
|
||||
if (walker >= 0)
|
||||
{
|
||||
if (walker_len < 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
{
|
||||
if (walker != 0)
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
walker_len += 8;
|
||||
}
|
||||
if (next_offset >= RDP_MPPC_DICT_SIZE)
|
||||
return -1;
|
||||
dict[next_offset++] = (((uint32) walker) >> ((uint32) 24));
|
||||
walker <<= 8;
|
||||
walker_len -= 8;
|
||||
continue;
|
||||
}
|
||||
walker <<= 1;
|
||||
/* fetch next 8-bits */
|
||||
if (--walker_len == 0)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker = data[i++] << 24;
|
||||
walker_len = 8;
|
||||
}
|
||||
/* literal decoding */
|
||||
if (walker >= 0)
|
||||
{
|
||||
if (walker_len < 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
walker_len += 8;
|
||||
}
|
||||
if (next_offset >= RDP_MPPC_DICT_SIZE)
|
||||
return -1;
|
||||
dict[next_offset++] = (uint8) (walker >> 24 | 0x80);
|
||||
walker <<= 8;
|
||||
walker_len -= 8;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* decode offset */
|
||||
/* length pair */
|
||||
walker <<= 1;
|
||||
if (--walker_len < (big ? 3 : 2))
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
walker_len += 8;
|
||||
}
|
||||
|
||||
if (big)
|
||||
{
|
||||
/* offset decoding where offset len is:
|
||||
-63: 11111 followed by the lower 6 bits of the value
|
||||
64-319: 11110 followed by the lower 8 bits of the value ( value - 64 )
|
||||
320-2367: 1110 followed by lower 11 bits of the value ( value - 320 )
|
||||
2368-65535: 110 followed by lower 16 bits of the value ( value - 2368 )
|
||||
*/
|
||||
switch (((uint32) walker) >> ((uint32) 29))
|
||||
{
|
||||
case 7: /* - 63 */
|
||||
for (; walker_len < 9; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
walker <<= 3;
|
||||
match_off = ((uint32) walker) >> ((uint32) 26);
|
||||
walker <<= 6;
|
||||
walker_len -= 9;
|
||||
break;
|
||||
|
||||
case 6: /* 64 - 319 */
|
||||
for (; walker_len < 11; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
|
||||
walker <<= 3;
|
||||
match_off = (((uint32) walker) >> ((uint32) 24)) + 64;
|
||||
walker <<= 8;
|
||||
walker_len -= 11;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
case 4: /* 320 - 2367 */
|
||||
for (; walker_len < 13; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
|
||||
walker <<= 2;
|
||||
match_off = (((uint32) walker) >> ((uint32) 21)) + 320;
|
||||
walker <<= 11;
|
||||
walker_len -= 13;
|
||||
break;
|
||||
|
||||
default: /* 2368 - 65535 */
|
||||
for (; walker_len < 17; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
|
||||
walker <<= 1;
|
||||
match_off = (((uint32) walker) >> ((uint32) 16)) + 2368;
|
||||
walker <<= 16;
|
||||
walker_len -= 17;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* offset decoding where offset len is:
|
||||
-63: 1111 followed by the lower 6 bits of the value
|
||||
64-319: 1110 followed by the lower 8 bits of the value ( value - 64 )
|
||||
320-8191: 110 followed by the lower 13 bits of the value ( value - 320 )
|
||||
*/
|
||||
switch (((uint32) walker) >> ((uint32) 30))
|
||||
{
|
||||
case 3: /* - 63 */
|
||||
if (walker_len < 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
walker_len += 8;
|
||||
}
|
||||
walker <<= 2;
|
||||
match_off = ((uint32) walker) >> ((uint32) 26);
|
||||
walker <<= 6;
|
||||
walker_len -= 8;
|
||||
break;
|
||||
|
||||
case 2: /* 64 - 319 */
|
||||
for (; walker_len < 10; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
|
||||
walker <<= 2;
|
||||
match_off = (((uint32) walker) >> ((uint32) 24)) + 64;
|
||||
walker <<= 8;
|
||||
walker_len -= 10;
|
||||
break;
|
||||
|
||||
default: /* 320 - 8191 */
|
||||
for (; walker_len < 14; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
|
||||
match_off = (walker >> 18) + 320;
|
||||
walker <<= 14;
|
||||
walker_len -= 14;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (walker_len == 0)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker = data[i++] << 24;
|
||||
walker_len = 8;
|
||||
}
|
||||
|
||||
/* decode length of match */
|
||||
match_len = 0;
|
||||
if (walker >= 0)
|
||||
{ /* special case - length of 3 is in bit 0 */
|
||||
match_len = 3;
|
||||
walker <<= 1;
|
||||
walker_len--;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* this is how it works len of:
|
||||
4-7: 10 followed by 2 bits of the value
|
||||
8-15: 110 followed by 3 bits of the value
|
||||
16-31: 1110 followed by 4 bits of the value
|
||||
32-63: .... and so forth
|
||||
64-127:
|
||||
128-255:
|
||||
256-511:
|
||||
512-1023:
|
||||
1024-2047:
|
||||
2048-4095:
|
||||
4096-8191:
|
||||
|
||||
i.e. 4097 is encoded as: 111111111110 000000000001
|
||||
meaning 4096 + 1...
|
||||
*/
|
||||
match_bits = big ? 14 : 11; /* 11 or 14 bits of value at most */
|
||||
do
|
||||
{
|
||||
walker <<= 1;
|
||||
if (--walker_len == 0)
|
||||
{
|
||||
if (i >= clen)
|
||||
return -1;
|
||||
walker = data[i++] << 24;
|
||||
walker_len = 8;
|
||||
}
|
||||
if (walker >= 0)
|
||||
break;
|
||||
if (--match_bits == 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
while (1);
|
||||
match_len = (big ? 16 : 13) - match_bits;
|
||||
walker <<= 1;
|
||||
if (--walker_len < match_len)
|
||||
{
|
||||
for (; walker_len < match_len; walker_len += 8)
|
||||
{
|
||||
if (i >= clen)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
walker |= (data[i++] & 0xff) << (24 - walker_len);
|
||||
}
|
||||
}
|
||||
|
||||
match_bits = match_len;
|
||||
match_len =
|
||||
((walker >> (32 - match_bits)) & (~(-1 << match_bits))) | (1 <<
|
||||
match_bits);
|
||||
walker <<= match_bits;
|
||||
walker_len -= match_bits;
|
||||
}
|
||||
if (next_offset + match_len >= RDP_MPPC_DICT_SIZE)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
/* memory areas can overlap - meaning we can't use memXXX functions */
|
||||
k = (next_offset - match_off) & (big ? 65535 : 8191);
|
||||
do
|
||||
{
|
||||
dict[next_offset++] = dict[k++];
|
||||
}
|
||||
while (--match_len != 0);
|
||||
}
|
||||
while (1);
|
||||
|
||||
/* store history offset */
|
||||
g_mppc_dict.roff = next_offset;
|
||||
|
||||
*roff = old_offset;
|
||||
*rlen = next_offset - old_offset;
|
||||
|
||||
return 0;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,200 +0,0 @@
|
||||
/* -*- c-basic-offset: 8 -*-
|
||||
rdesktop: A Remote Desktop Protocol client.
|
||||
Persistent Bitmap Cache routines
|
||||
Copyright (C) Jeroen Meijer 2004-2005
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include "rdesktop.h"
|
||||
|
||||
#define MAX_CELL_SIZE 0x1000 /* pixels */
|
||||
|
||||
#define IS_PERSISTENT(id) (id < 8 && g_pstcache_fd[id] > 0)
|
||||
|
||||
extern int g_server_depth;
|
||||
extern BOOL g_bitmap_cache;
|
||||
extern BOOL g_bitmap_cache_persist_enable;
|
||||
extern BOOL g_bitmap_cache_precache;
|
||||
|
||||
int g_pstcache_fd[8];
|
||||
int g_pstcache_Bpp;
|
||||
BOOL g_pstcache_enumerated = False;
|
||||
uint8 zero_key[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
|
||||
/* Update mru stamp/index for a bitmap */
|
||||
void
|
||||
pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
|
||||
return;
|
||||
|
||||
fd = g_pstcache_fd[cache_id];
|
||||
rd_lseek_file(fd, 12 + cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
||||
rd_write_file(fd, &stamp, sizeof(stamp));
|
||||
}
|
||||
|
||||
/* Load a bitmap from the persistent cache */
|
||||
BOOL
|
||||
pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx)
|
||||
{
|
||||
uint8 *celldata;
|
||||
int fd;
|
||||
CELLHEADER cellhdr;
|
||||
RD_HBITMAP bitmap;
|
||||
|
||||
if (!g_bitmap_cache_persist_enable)
|
||||
return False;
|
||||
|
||||
if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
|
||||
return False;
|
||||
|
||||
fd = g_pstcache_fd[cache_id];
|
||||
rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
||||
rd_read_file(fd, &cellhdr, sizeof(CELLHEADER));
|
||||
celldata = (uint8 *) xmalloc(cellhdr.length);
|
||||
rd_read_file(fd, celldata, cellhdr.length);
|
||||
|
||||
bitmap = ui_create_bitmap(cellhdr.width, cellhdr.height, celldata);
|
||||
DEBUG(("Load bitmap from disk: id=%d, idx=%d, bmp=0x%x)\n", cache_id, cache_idx, bitmap));
|
||||
cache_put_bitmap(cache_id, cache_idx, bitmap);
|
||||
|
||||
xfree(celldata);
|
||||
return True;
|
||||
}
|
||||
|
||||
/* Store a bitmap in the persistent cache */
|
||||
BOOL
|
||||
pstcache_save_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * key,
|
||||
uint8 width, uint8 height, uint16 length, uint8 * data)
|
||||
{
|
||||
int fd;
|
||||
CELLHEADER cellhdr;
|
||||
|
||||
if (!IS_PERSISTENT(cache_id) || cache_idx >= BMPCACHE2_NUM_PSTCELLS)
|
||||
return False;
|
||||
|
||||
memcpy(cellhdr.key, key, sizeof(HASH_KEY));
|
||||
cellhdr.width = width;
|
||||
cellhdr.height = height;
|
||||
cellhdr.length = length;
|
||||
cellhdr.stamp = 0;
|
||||
|
||||
fd = g_pstcache_fd[cache_id];
|
||||
rd_lseek_file(fd, cache_idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
||||
rd_write_file(fd, &cellhdr, sizeof(CELLHEADER));
|
||||
rd_write_file(fd, data, length);
|
||||
|
||||
return True;
|
||||
}
|
||||
|
||||
/* List the bitmap keys from the persistent cache file */
|
||||
int
|
||||
pstcache_enumerate(uint8 id, HASH_KEY * keylist)
|
||||
{
|
||||
int fd, n;
|
||||
uint16 idx;
|
||||
sint16 mru_idx[0xa00];
|
||||
uint32 mru_stamp[0xa00];
|
||||
CELLHEADER cellhdr;
|
||||
|
||||
if (!(g_bitmap_cache && g_bitmap_cache_persist_enable && IS_PERSISTENT(id)))
|
||||
return 0;
|
||||
|
||||
/* The server disconnects if the bitmap cache content is sent more than once */
|
||||
if (g_pstcache_enumerated)
|
||||
return 0;
|
||||
|
||||
DEBUG_RDP5(("Persistent bitmap cache enumeration... "));
|
||||
for (idx = 0; idx < BMPCACHE2_NUM_PSTCELLS; idx++)
|
||||
{
|
||||
fd = g_pstcache_fd[id];
|
||||
rd_lseek_file(fd, idx * (g_pstcache_Bpp * MAX_CELL_SIZE + sizeof(CELLHEADER)));
|
||||
if (rd_read_file(fd, &cellhdr, sizeof(CELLHEADER)) <= 0)
|
||||
break;
|
||||
|
||||
if (memcmp(cellhdr.key, zero_key, sizeof(HASH_KEY)) != 0)
|
||||
{
|
||||
memcpy(keylist[idx], cellhdr.key, sizeof(HASH_KEY));
|
||||
|
||||
/* Pre-cache (not possible for 8 bit colour depth cause it needs a colourmap) */
|
||||
if (g_bitmap_cache_precache && cellhdr.stamp && g_server_depth > 8)
|
||||
pstcache_load_bitmap(id, idx);
|
||||
|
||||
/* Sort by stamp */
|
||||
for (n = idx; n > 0 && cellhdr.stamp < mru_stamp[n - 1]; n--)
|
||||
{
|
||||
mru_idx[n] = mru_idx[n - 1];
|
||||
mru_stamp[n] = mru_stamp[n - 1];
|
||||
}
|
||||
|
||||
mru_idx[n] = idx;
|
||||
mru_stamp[n] = cellhdr.stamp;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
DEBUG_RDP5(("%d cached bitmaps.\n", idx));
|
||||
|
||||
cache_rebuild_bmpcache_linked_list(id, mru_idx, idx);
|
||||
g_pstcache_enumerated = True;
|
||||
return idx;
|
||||
}
|
||||
|
||||
/* initialise the persistent bitmap cache */
|
||||
BOOL
|
||||
pstcache_init(uint8 cache_id)
|
||||
{
|
||||
int fd;
|
||||
char filename[256];
|
||||
|
||||
if (g_pstcache_enumerated)
|
||||
return True;
|
||||
|
||||
g_pstcache_fd[cache_id] = 0;
|
||||
|
||||
if (!(g_bitmap_cache && g_bitmap_cache_persist_enable))
|
||||
return False;
|
||||
|
||||
if (!rd_pstcache_mkdir())
|
||||
{
|
||||
DEBUG(("failed to get/make cache directory!\n"));
|
||||
return False;
|
||||
}
|
||||
|
||||
g_pstcache_Bpp = (g_server_depth + 7) / 8;
|
||||
sprintf(filename, "cache/pstcache_%d_%d", cache_id, g_pstcache_Bpp);
|
||||
DEBUG(("persistent bitmap cache file: %s\n", filename));
|
||||
|
||||
fd = rd_open_file(filename);
|
||||
if (fd == -1)
|
||||
return False;
|
||||
|
||||
if (!rd_lock_file(fd, 0, 0))
|
||||
{
|
||||
warning("Persistent bitmap caching is disabled. (The file is already in use)\n");
|
||||
rd_close_file(fd);
|
||||
return False;
|
||||
}
|
||||
|
||||
g_pstcache_fd[cache_id] = fd;
|
||||
return True;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue