a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* -*- c-basic-offset: 8 -*-
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rdesktop: A Remote Desktop Protocol client.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Protocol services - RDP decompression
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
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 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 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/>.
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * the General Public License version 2 (GPLv2) at this time for any software where
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * a choice of GPL license versions is made available with the language indicating
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * that GPLv2 or any later version may be used, or where a choice of which version
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * of the GPL is applied is otherwise unspecified.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* mppc decompression */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Contacts: */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* hifn contact mentioned in the faq is */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Robert Friend rfriend at hifn dot com */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* if you have questions regarding MPPC */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* our contact is */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Guus Dhaeze GDhaeze at hifn dot com */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Licensing: */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* decompression is alright as long as we */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* don't compress data */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Algorithm: */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* as the rfc states the algorithm seems to */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* be LZ77 with a sliding buffer */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* that is empty at init. */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* the algorithm is called LZS and is */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* patented for another couple of years. */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* more information is available in */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* http://www.ietf.org/ietf/IPR/hifn-ipr-draft-friend-tls-lzs-compression.txt */
a180a41bba1d50822df23fff0099e90b86638b89vboxsyncmppc_expand(uint8 * data, uint32 clen, uint8 ctype, uint32 * roff, uint32 * rlen)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync dict[next_offset++] = (((uint32) walker) >> ((uint32) 24));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* fetch next 8-bits */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* literal decoding */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync dict[next_offset++] = (uint8) (walker >> 24 | 0x80);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* decode offset */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* length pair */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* offset decoding where offset len is:
a180a41bba1d50822df23fff0099e90b86638b89vboxsync -63: 11111 followed by the lower 6 bits of the value
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 64-319: 11110 followed by the lower 8 bits of the value ( value - 64 )
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 320-2367: 1110 followed by lower 11 bits of the value ( value - 320 )
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 2368-65535: 110 followed by lower 16 bits of the value ( value - 2368 )
a180a41bba1d50822df23fff0099e90b86638b89vboxsync match_off = (((uint32) walker) >> ((uint32) 24)) + 64;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync match_off = (((uint32) walker) >> ((uint32) 21)) + 320;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync default: /* 2368 - 65535 */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync match_off = (((uint32) walker) >> ((uint32) 16)) + 2368;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* offset decoding where offset len is:
a180a41bba1d50822df23fff0099e90b86638b89vboxsync -63: 1111 followed by the lower 6 bits of the value
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 64-319: 1110 followed by the lower 8 bits of the value ( value - 64 )
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 320-8191: 110 followed by the lower 13 bits of the value ( value - 320 )
a180a41bba1d50822df23fff0099e90b86638b89vboxsync match_off = (((uint32) walker) >> ((uint32) 24)) + 64;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync default: /* 320 - 8191 */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* decode length of match */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync { /* special case - length of 3 is in bit 0 */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* this is how it works len of:
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 4-7: 10 followed by 2 bits of the value
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 8-15: 110 followed by 3 bits of the value
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 16-31: 1110 followed by 4 bits of the value
a180a41bba1d50822df23fff0099e90b86638b89vboxsync 32-63: .... and so forth
a180a41bba1d50822df23fff0099e90b86638b89vboxsync i.e. 4097 is encoded as: 111111111110 000000000001
a180a41bba1d50822df23fff0099e90b86638b89vboxsync meaning 4096 + 1...
a180a41bba1d50822df23fff0099e90b86638b89vboxsync match_bits = big ? 14 : 11; /* 11 or 14 bits of value at most */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync ((walker >> (32 - match_bits)) & (~(-1 << match_bits))) | (1 <<
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* memory areas can overlap - meaning we can't use memXXX functions */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync k = (next_offset - match_off) & (big ? 65535 : 8191);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync while (--match_len != 0);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* store history offset */