1 |
|
/* -*- c-basic-offset: 8 -*- |
2 |
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
Protocol services - RDP decompression |
4 |
|
Copyright (C) Matthew Chapman 1999-2004 |
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 |
#include <stdio.h> |
#include <stdio.h> |
22 |
#include <string.h> |
#include <string.h> |
23 |
|
|
24 |
#include "rdesktop.h" |
#include "rdesktop.h" |
25 |
|
|
26 |
/* mppc-like??? decompression */ |
/* mppc decompression */ |
27 |
/* http://www.faqs.org/rfcs/rfc2118.html */ |
/* http://www.faqs.org/rfcs/rfc2118.html */ |
28 |
|
|
29 |
/* TODO: research the below statements */ |
/* Contacts: */ |
30 |
|
|
31 |
|
/* hifn contact mentioned in the faq is */ |
32 |
|
/* Robert Friend rfriend at hifn dot com */ |
33 |
|
|
34 |
|
/* if you have questions regarding MPPC */ |
35 |
|
/* our contact is */ |
36 |
|
/* Guus Dhaeze GDhaeze at hifn dot com */ |
37 |
|
|
38 |
|
/* Licensing: */ |
39 |
|
|
40 |
|
/* decompression is alright as long as we */ |
41 |
|
/* don't compress data */ |
42 |
|
|
43 |
/* there exists one or more patents */ |
/* Algorithm: */ |
|
/* related to compression algorithms */ |
|
44 |
|
|
45 |
/* since we are only decompressing I */ |
/* as the rfc states the algorithm seems to */ |
46 |
/* think the end-user is safe. */ |
/* be LZ77 with a sliding buffer */ |
47 |
|
/* that is empty at init. */ |
48 |
|
|
49 |
/* even if that isn't true, aren't you */ |
/* the algorithm is called LZS and is */ |
50 |
/* already paying royalties */ |
/* patented for another couple of years. */ |
|
/* through the CAL licenses? */ |
|
51 |
|
|
52 |
/* the dictionary is empty when init. like */ |
/* more information is available in */ |
53 |
/* LZ78, which is not patented */ |
/* http://www.ietf.org/ietf/IPR/hifn-ipr-draft-friend-tls-lzs-compression.txt */ |
54 |
|
|
55 |
|
|
56 |
RDPCOMP mppc_dict; |
RDPCOMP g_mppc_dict; |
57 |
|
|
58 |
int |
int |
59 |
mppc_expand(uint8 * data, uint32 clen, uint8 ctype, uint32 * roff, uint32 * rlen) |
mppc_expand(uint8 * data, uint32 clen, uint8 ctype, uint32 * roff, uint32 * rlen) |
64 |
int match_len; |
int match_len; |
65 |
int old_offset, match_bits; |
int old_offset, match_bits; |
66 |
|
|
67 |
signed char *dict = &(mppc_dict.hist); |
uint8 *dict = g_mppc_dict.hist; |
68 |
|
|
69 |
if ((ctype & RDP_MPPC_COMPRESSED) == 0) |
if ((ctype & RDP_MPPC_COMPRESSED) == 0) |
70 |
{ |
{ |
75 |
|
|
76 |
if ((ctype & RDP_MPPC_RESET) != 0) |
if ((ctype & RDP_MPPC_RESET) != 0) |
77 |
{ |
{ |
78 |
mppc_dict.roff = 0; |
g_mppc_dict.roff = 0; |
79 |
} |
} |
80 |
|
|
81 |
if ((ctype & RDP_MPPC_FLUSH) != 0) |
if ((ctype & RDP_MPPC_FLUSH) != 0) |
82 |
{ |
{ |
83 |
memset(dict, 0, RDP_MPPC_DICT_SIZE); |
memset(dict, 0, RDP_MPPC_DICT_SIZE); |
84 |
mppc_dict.roff = 0; |
g_mppc_dict.roff = 0; |
85 |
} |
} |
86 |
|
|
87 |
*roff = 0; |
*roff = 0; |
88 |
*rlen = 0; |
*rlen = 0; |
89 |
|
|
90 |
walker = mppc_dict.roff; |
walker = g_mppc_dict.roff; |
91 |
|
|
92 |
next_offset = walker; |
next_offset = walker; |
93 |
old_offset = next_offset; |
old_offset = next_offset; |
279 |
|
|
280 |
match_bits = match_len; |
match_bits = match_len; |
281 |
match_len = |
match_len = |
282 |
walker >> 32 - match_bits & ~(-1 << match_bits) | 1 << match_bits; |
((walker >> (32 - match_bits)) & (~(-1 << match_bits))) | (1 << match_bits); |
283 |
walker <<= match_bits; |
walker <<= match_bits; |
284 |
walker_len -= match_bits; |
walker_len -= match_bits; |
285 |
} |
} |
288 |
return -1; |
return -1; |
289 |
} |
} |
290 |
/* memory areas can overlap - meaning we can't use memXXX functions */ |
/* memory areas can overlap - meaning we can't use memXXX functions */ |
291 |
k = next_offset - match_off & (RDP_MPPC_DICT_SIZE - 1); |
k = (next_offset - match_off) & (RDP_MPPC_DICT_SIZE - 1); |
292 |
do |
do |
293 |
{ |
{ |
294 |
dict[next_offset++] = dict[k++]; |
dict[next_offset++] = dict[k++]; |
298 |
while (1); |
while (1); |
299 |
|
|
300 |
/* store history offset */ |
/* store history offset */ |
301 |
mppc_dict.roff = next_offset; |
g_mppc_dict.roff = next_offset; |
302 |
|
|
303 |
*roff = old_offset; |
*roff = old_offset; |
304 |
*rlen = next_offset - old_offset; |
*rlen = next_offset - old_offset; |