1c3fb4252SPedro F. Giffuni /*- 2c3fb4252SPedro F. Giffuni * Copyright (c) 2002-2004 Jan Dubiec <jdx@slackware.pl> 3c3fb4252SPedro F. Giffuni * Copyright (c) 2007 Alexander Motin <mav@freebsd.org> 4c3fb4252SPedro F. Giffuni * All rights reserved. 5c3fb4252SPedro F. Giffuni * 6c3fb4252SPedro F. Giffuni * Redistribution and use in source and binary forms, with or without 7c3fb4252SPedro F. Giffuni * modification, are permitted provided that the following conditions 8c3fb4252SPedro F. Giffuni * are met: 9c3fb4252SPedro F. Giffuni * 1. Redistributions of source code must retain the above copyright 10c3fb4252SPedro F. Giffuni * notice unmodified, this list of conditions, and the following 11c3fb4252SPedro F. Giffuni * disclaimer. 12c3fb4252SPedro F. Giffuni * 2. Redistributions in binary form must reproduce the above copyright 13c3fb4252SPedro F. Giffuni * notice, this list of conditions and the following disclaimer in the 14c3fb4252SPedro F. Giffuni * documentation and/or other materials provided with the distribution. 15c3fb4252SPedro F. Giffuni * 16c3fb4252SPedro F. Giffuni * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17c3fb4252SPedro F. Giffuni * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18c3fb4252SPedro F. Giffuni * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19c3fb4252SPedro F. Giffuni * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20c3fb4252SPedro F. Giffuni * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21c3fb4252SPedro F. Giffuni * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22c3fb4252SPedro F. Giffuni * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23c3fb4252SPedro F. Giffuni * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24c3fb4252SPedro F. Giffuni * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25c3fb4252SPedro F. Giffuni * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26c3fb4252SPedro F. Giffuni * SUCH DAMAGE. 27c3fb4252SPedro F. Giffuni * 28c3fb4252SPedro F. Giffuni * $FreeBSD$ 29c3fb4252SPedro F. Giffuni */ 30c3fb4252SPedro F. Giffuni 31c3fb4252SPedro F. Giffuni /* 32c3fb4252SPedro F. Giffuni * MPPC decompression library. 33c3fb4252SPedro F. Giffuni * Version 1.0 34c3fb4252SPedro F. Giffuni * 35c3fb4252SPedro F. Giffuni * Note that Hi/Fn (later acquired by Exar Corporation) held US patents 36c3fb4252SPedro F. Giffuni * on some implementation-critical aspects of MPPC compression. 37c3fb4252SPedro F. Giffuni * These patents lapsed due to non-payment of fees in 2007 and by 2015 38c3fb4252SPedro F. Giffuni * expired altogether. 39c3fb4252SPedro F. Giffuni */ 40c3fb4252SPedro F. Giffuni 41c3fb4252SPedro F. Giffuni #include <sys/param.h> 42c3fb4252SPedro F. Giffuni #include <sys/systm.h> 43c3fb4252SPedro F. Giffuni 44c3fb4252SPedro F. Giffuni #include <net/mppc.h> 45c3fb4252SPedro F. Giffuni 46c3fb4252SPedro F. Giffuni #define MPPE_HIST_LEN 8192 47c3fb4252SPedro F. Giffuni 48c3fb4252SPedro F. Giffuni #define HASH(x) (((40543*(((((x)[0]<<4)^(x)[1])<<4)^(x)[2]))>>4) & 0x1fff) 49c3fb4252SPedro F. Giffuni 50c3fb4252SPedro F. Giffuni struct MPPC_comp_state { 51c3fb4252SPedro F. Giffuni uint8_t hist[2*MPPE_HIST_LEN]; 52c3fb4252SPedro F. Giffuni uint16_t histptr; 53c3fb4252SPedro F. Giffuni uint16_t hash[MPPE_HIST_LEN]; 54c3fb4252SPedro F. Giffuni }; 55c3fb4252SPedro F. Giffuni 56c3fb4252SPedro F. Giffuni /* Inserts 1 to 8 bits into the output buffer. */ 57c3fb4252SPedro F. Giffuni static void __inline 58c3fb4252SPedro F. Giffuni putbits8(uint8_t *buf, uint32_t val, const uint32_t n, uint32_t *i, uint32_t *l) 59c3fb4252SPedro F. Giffuni { 60c3fb4252SPedro F. Giffuni buf += *i; 61c3fb4252SPedro F. Giffuni if (*l >= n) { 62c3fb4252SPedro F. Giffuni *l = (*l) - n; 63c3fb4252SPedro F. Giffuni val <<= *l; 64c3fb4252SPedro F. Giffuni *buf = *buf | (val & 0xff); 65c3fb4252SPedro F. Giffuni if (*l == 0) { 66c3fb4252SPedro F. Giffuni *l = 8; 67c3fb4252SPedro F. Giffuni (*i)++; 68c3fb4252SPedro F. Giffuni *(++buf) = 0; 69c3fb4252SPedro F. Giffuni } 70c3fb4252SPedro F. Giffuni } else { 71c3fb4252SPedro F. Giffuni (*i)++; 72c3fb4252SPedro F. Giffuni *l = 8 - n + (*l); 73c3fb4252SPedro F. Giffuni val <<= *l; 74c3fb4252SPedro F. Giffuni *buf = *buf | ((val >> 8) & 0xff); 75c3fb4252SPedro F. Giffuni *(++buf) = val & 0xff; 76c3fb4252SPedro F. Giffuni } 77c3fb4252SPedro F. Giffuni } 78c3fb4252SPedro F. Giffuni 79c3fb4252SPedro F. Giffuni /* Inserts 9 to 16 bits into the output buffer. */ 80c3fb4252SPedro F. Giffuni static void __inline 81c3fb4252SPedro F. Giffuni putbits16(uint8_t *buf, uint32_t val, const uint32_t n, uint32_t *i, uint32_t *l) 82c3fb4252SPedro F. Giffuni { 83c3fb4252SPedro F. Giffuni buf += *i; 84c3fb4252SPedro F. Giffuni if (*l >= n - 8) { 85c3fb4252SPedro F. Giffuni (*i)++; 86c3fb4252SPedro F. Giffuni *l = 8 - n + (*l); 87c3fb4252SPedro F. Giffuni val <<= *l; 88c3fb4252SPedro F. Giffuni *buf = *buf | ((val >> 8) & 0xff); 89c3fb4252SPedro F. Giffuni *(++buf) = val & 0xff; 90c3fb4252SPedro F. Giffuni if (*l == 0) { 91c3fb4252SPedro F. Giffuni *l = 8; 92c3fb4252SPedro F. Giffuni (*i)++; 93c3fb4252SPedro F. Giffuni *(++buf) = 0; 94c3fb4252SPedro F. Giffuni } 95c3fb4252SPedro F. Giffuni } else { 96c3fb4252SPedro F. Giffuni (*i)++; (*i)++; 97c3fb4252SPedro F. Giffuni *l = 16 - n + (*l); 98c3fb4252SPedro F. Giffuni val <<= *l; 99c3fb4252SPedro F. Giffuni *buf = *buf | ((val >> 16) & 0xff); 100c3fb4252SPedro F. Giffuni *(++buf) = (val >> 8) & 0xff; 101c3fb4252SPedro F. Giffuni *(++buf) = val & 0xff; 102c3fb4252SPedro F. Giffuni } 103c3fb4252SPedro F. Giffuni } 104c3fb4252SPedro F. Giffuni 105c3fb4252SPedro F. Giffuni /* Inserts 17 to 24 bits into the output buffer. */ 106c3fb4252SPedro F. Giffuni static void __inline 107c3fb4252SPedro F. Giffuni putbits24(uint8_t *buf, uint32_t val, const uint32_t n, uint32_t *i, uint32_t *l) 108c3fb4252SPedro F. Giffuni { 109c3fb4252SPedro F. Giffuni buf += *i; 110c3fb4252SPedro F. Giffuni if (*l >= n - 16) { 111c3fb4252SPedro F. Giffuni (*i)++; (*i)++; 112c3fb4252SPedro F. Giffuni *l = 16 - n + (*l); 113c3fb4252SPedro F. Giffuni val <<= *l; 114c3fb4252SPedro F. Giffuni *buf = *buf | ((val >> 16) & 0xff); 115c3fb4252SPedro F. Giffuni *(++buf) = (val >> 8) & 0xff; 116c3fb4252SPedro F. Giffuni *(++buf) = val & 0xff; 117c3fb4252SPedro F. Giffuni if (*l == 0) { 118c3fb4252SPedro F. Giffuni *l = 8; 119c3fb4252SPedro F. Giffuni (*i)++; 120c3fb4252SPedro F. Giffuni *(++buf) = 0; 121c3fb4252SPedro F. Giffuni } 122c3fb4252SPedro F. Giffuni } else { 123c3fb4252SPedro F. Giffuni (*i)++; (*i)++; (*i)++; 124c3fb4252SPedro F. Giffuni *l = 24 - n + (*l); 125c3fb4252SPedro F. Giffuni val <<= *l; 126c3fb4252SPedro F. Giffuni *buf = *buf | ((val >> 24) & 0xff); 127c3fb4252SPedro F. Giffuni *(++buf) = (val >> 16) & 0xff; 128c3fb4252SPedro F. Giffuni *(++buf) = (val >> 8) & 0xff; 129c3fb4252SPedro F. Giffuni *(++buf) = val & 0xff; 130c3fb4252SPedro F. Giffuni } 131c3fb4252SPedro F. Giffuni } 132c3fb4252SPedro F. Giffuni 133c3fb4252SPedro F. Giffuni size_t MPPC_SizeOfCompressionHistory(void) 134c3fb4252SPedro F. Giffuni { 135c3fb4252SPedro F. Giffuni return (sizeof(struct MPPC_comp_state)); 136c3fb4252SPedro F. Giffuni } 137c3fb4252SPedro F. Giffuni 138c3fb4252SPedro F. Giffuni void MPPC_InitCompressionHistory(char *history) 139c3fb4252SPedro F. Giffuni { 140c3fb4252SPedro F. Giffuni struct MPPC_comp_state *state = (struct MPPC_comp_state*)history; 141c3fb4252SPedro F. Giffuni 142c3fb4252SPedro F. Giffuni bzero(history, sizeof(struct MPPC_comp_state)); 143c3fb4252SPedro F. Giffuni state->histptr = MPPE_HIST_LEN; 144c3fb4252SPedro F. Giffuni } 145c3fb4252SPedro F. Giffuni 146c3fb4252SPedro F. Giffuni int MPPC_Compress(u_char **src, u_char **dst, u_long *srcCnt, u_long *dstCnt, char *history, int flags, int undef) 147c3fb4252SPedro F. Giffuni { 148c3fb4252SPedro F. Giffuni struct MPPC_comp_state *state = (struct MPPC_comp_state*)history; 149c3fb4252SPedro F. Giffuni uint32_t olen, off, len, idx, i, l; 150c3fb4252SPedro F. Giffuni uint8_t *hist, *sbuf, *p, *q, *r, *s; 151c3fb4252SPedro F. Giffuni int rtn = MPPC_OK; 152c3fb4252SPedro F. Giffuni 153c3fb4252SPedro F. Giffuni /* 154c3fb4252SPedro F. Giffuni * At this point, to avoid possible buffer overflow caused by packet 155c3fb4252SPedro F. Giffuni * expansion during/after compression, we should make sure we have 156c3fb4252SPedro F. Giffuni * space for the worst case. 157c3fb4252SPedro F. Giffuni 158c3fb4252SPedro F. Giffuni * Maximum MPPC packet expansion is 12.5%. This is the worst case when 159c3fb4252SPedro F. Giffuni * all octets in the input buffer are >= 0x80 and we cannot find any 160c3fb4252SPedro F. Giffuni * repeated tokens. 161c3fb4252SPedro F. Giffuni */ 162c3fb4252SPedro F. Giffuni if (*dstCnt < (*srcCnt * 9 / 8 + 2)) { 163c3fb4252SPedro F. Giffuni rtn &= ~MPPC_OK; 164c3fb4252SPedro F. Giffuni return (rtn); 165c3fb4252SPedro F. Giffuni } 166c3fb4252SPedro F. Giffuni 167c3fb4252SPedro F. Giffuni /* We can't compress more then MPPE_HIST_LEN bytes in a call. */ 168c3fb4252SPedro F. Giffuni if (*srcCnt > MPPE_HIST_LEN) { 169c3fb4252SPedro F. Giffuni rtn &= ~MPPC_OK; 170c3fb4252SPedro F. Giffuni return (rtn); 171c3fb4252SPedro F. Giffuni } 172c3fb4252SPedro F. Giffuni 173c3fb4252SPedro F. Giffuni hist = state->hist + MPPE_HIST_LEN; 174c3fb4252SPedro F. Giffuni /* check if there is enough room at the end of the history */ 175c3fb4252SPedro F. Giffuni if (state->histptr + *srcCnt >= 2*MPPE_HIST_LEN) { 176c3fb4252SPedro F. Giffuni rtn |= MPPC_RESTART_HISTORY; 177c3fb4252SPedro F. Giffuni state->histptr = MPPE_HIST_LEN; 178c3fb4252SPedro F. Giffuni memcpy(state->hist, hist, MPPE_HIST_LEN); 179c3fb4252SPedro F. Giffuni } 180c3fb4252SPedro F. Giffuni /* Add packet to the history. */ 181c3fb4252SPedro F. Giffuni sbuf = state->hist + state->histptr; 182c3fb4252SPedro F. Giffuni memcpy(sbuf, *src, *srcCnt); 183c3fb4252SPedro F. Giffuni state->histptr += *srcCnt; 184c3fb4252SPedro F. Giffuni 185c3fb4252SPedro F. Giffuni /* compress data */ 186c3fb4252SPedro F. Giffuni r = sbuf + *srcCnt; 187c3fb4252SPedro F. Giffuni **dst = olen = i = 0; 188c3fb4252SPedro F. Giffuni l = 8; 189c3fb4252SPedro F. Giffuni while (i < *srcCnt - 2) { 190c3fb4252SPedro F. Giffuni s = q = sbuf + i; 191c3fb4252SPedro F. Giffuni 192c3fb4252SPedro F. Giffuni /* Prognose matching position using hash function. */ 193c3fb4252SPedro F. Giffuni idx = HASH(s); 194c3fb4252SPedro F. Giffuni p = hist + state->hash[idx]; 195c3fb4252SPedro F. Giffuni state->hash[idx] = (uint16_t) (s - hist); 196c3fb4252SPedro F. Giffuni if (p > s) /* It was before MPPC_RESTART_HISTORY. */ 197c3fb4252SPedro F. Giffuni p -= MPPE_HIST_LEN; /* Try previous history buffer. */ 198c3fb4252SPedro F. Giffuni off = s - p; 199c3fb4252SPedro F. Giffuni 200c3fb4252SPedro F. Giffuni /* Check our prognosis. */ 201c3fb4252SPedro F. Giffuni if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ || 202c3fb4252SPedro F. Giffuni *p++ != *s++ || *p++ != *s++) { 203c3fb4252SPedro F. Giffuni /* No match found; encode literal byte. */ 204c3fb4252SPedro F. Giffuni if ((*src)[i] < 0x80) { /* literal byte < 0x80 */ 205c3fb4252SPedro F. Giffuni putbits8(*dst, (uint32_t) (*src)[i], 8, &olen, &l); 206c3fb4252SPedro F. Giffuni } else { /* literal byte >= 0x80 */ 207c3fb4252SPedro F. Giffuni putbits16(*dst, (uint32_t) (0x100|((*src)[i]&0x7f)), 9, 208c3fb4252SPedro F. Giffuni &olen, &l); 209c3fb4252SPedro F. Giffuni } 210c3fb4252SPedro F. Giffuni ++i; 211c3fb4252SPedro F. Giffuni continue; 212c3fb4252SPedro F. Giffuni } 213c3fb4252SPedro F. Giffuni 214c3fb4252SPedro F. Giffuni /* Find length of the matching fragment */ 215c3fb4252SPedro F. Giffuni #if defined(__amd64__) || defined(__i386__) 216c3fb4252SPedro F. Giffuni /* Optimization for CPUs without strict data aligning requirements */ 217c3fb4252SPedro F. Giffuni while ((*((uint32_t*)p) == *((uint32_t*)s)) && (s < (r - 3))) { 218c3fb4252SPedro F. Giffuni p+=4; 219c3fb4252SPedro F. Giffuni s+=4; 220c3fb4252SPedro F. Giffuni } 221c3fb4252SPedro F. Giffuni #endif 222c3fb4252SPedro F. Giffuni while((*p++ == *s++) && (s <= r)); 223c3fb4252SPedro F. Giffuni len = s - q - 1; 224c3fb4252SPedro F. Giffuni i += len; 225c3fb4252SPedro F. Giffuni 226c3fb4252SPedro F. Giffuni /* At least 3 character match found; code data. */ 227c3fb4252SPedro F. Giffuni /* Encode offset. */ 228c3fb4252SPedro F. Giffuni if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */ 229c3fb4252SPedro F. Giffuni putbits16(*dst, 0x3c0|off, 10, &olen, &l); 230c3fb4252SPedro F. Giffuni } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */ 231c3fb4252SPedro F. Giffuni putbits16(*dst, 0xe00|(off-64), 12, &olen, &l); 232c3fb4252SPedro F. Giffuni } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */ 233c3fb4252SPedro F. Giffuni putbits16(*dst, 0xc000|(off-320), 16, &olen, &l); 234c3fb4252SPedro F. Giffuni } else { /* NOTREACHED */ 235*c79cee71SKyle Evans __assert_unreachable(); 236c3fb4252SPedro F. Giffuni rtn &= ~MPPC_OK; 237a4bf2e2dSPedro F. Giffuni return (rtn); 238c3fb4252SPedro F. Giffuni } 239c3fb4252SPedro F. Giffuni 240c3fb4252SPedro F. Giffuni /* Encode length of match. */ 241c3fb4252SPedro F. Giffuni if (len < 4) { /* length = 3 */ 242c3fb4252SPedro F. Giffuni putbits8(*dst, 0, 1, &olen, &l); 243c3fb4252SPedro F. Giffuni } else if (len < 8) { /* 4 <= length < 8 */ 244c3fb4252SPedro F. Giffuni putbits8(*dst, 0x08|(len&0x03), 4, &olen, &l); 245c3fb4252SPedro F. Giffuni } else if (len < 16) { /* 8 <= length < 16 */ 246c3fb4252SPedro F. Giffuni putbits8(*dst, 0x30|(len&0x07), 6, &olen, &l); 247c3fb4252SPedro F. Giffuni } else if (len < 32) { /* 16 <= length < 32 */ 248c3fb4252SPedro F. Giffuni putbits8(*dst, 0xe0|(len&0x0f), 8, &olen, &l); 249c3fb4252SPedro F. Giffuni } else if (len < 64) { /* 32 <= length < 64 */ 250c3fb4252SPedro F. Giffuni putbits16(*dst, 0x3c0|(len&0x1f), 10, &olen, &l); 251c3fb4252SPedro F. Giffuni } else if (len < 128) { /* 64 <= length < 128 */ 252c3fb4252SPedro F. Giffuni putbits16(*dst, 0xf80|(len&0x3f), 12, &olen, &l); 253c3fb4252SPedro F. Giffuni } else if (len < 256) { /* 128 <= length < 256 */ 254c3fb4252SPedro F. Giffuni putbits16(*dst, 0x3f00|(len&0x7f), 14, &olen, &l); 255c3fb4252SPedro F. Giffuni } else if (len < 512) { /* 256 <= length < 512 */ 256c3fb4252SPedro F. Giffuni putbits16(*dst, 0xfe00|(len&0xff), 16, &olen, &l); 257c3fb4252SPedro F. Giffuni } else if (len < 1024) { /* 512 <= length < 1024 */ 258c3fb4252SPedro F. Giffuni putbits24(*dst, 0x3fc00|(len&0x1ff), 18, &olen, &l); 259c3fb4252SPedro F. Giffuni } else if (len < 2048) { /* 1024 <= length < 2048 */ 260c3fb4252SPedro F. Giffuni putbits24(*dst, 0xff800|(len&0x3ff), 20, &olen, &l); 261c3fb4252SPedro F. Giffuni } else if (len < 4096) { /* 2048 <= length < 4096 */ 262c3fb4252SPedro F. Giffuni putbits24(*dst, 0x3ff000|(len&0x7ff), 22, &olen, &l); 263c3fb4252SPedro F. Giffuni } else if (len < 8192) { /* 4096 <= length < 8192 */ 264c3fb4252SPedro F. Giffuni putbits24(*dst, 0xffe000|(len&0xfff), 24, &olen, &l); 265c3fb4252SPedro F. Giffuni } else { /* NOTREACHED */ 266c3fb4252SPedro F. Giffuni rtn &= ~MPPC_OK; 267c3fb4252SPedro F. Giffuni return (rtn); 268c3fb4252SPedro F. Giffuni } 269c3fb4252SPedro F. Giffuni } 270c3fb4252SPedro F. Giffuni 271c3fb4252SPedro F. Giffuni /* Add remaining octets to the output. */ 272c3fb4252SPedro F. Giffuni while(*srcCnt - i > 0) { 273c3fb4252SPedro F. Giffuni if ((*src)[i] < 0x80) { /* literal byte < 0x80 */ 274c3fb4252SPedro F. Giffuni putbits8(*dst, (uint32_t) (*src)[i++], 8, &olen, &l); 275c3fb4252SPedro F. Giffuni } else { /* literal byte >= 0x80 */ 276c3fb4252SPedro F. Giffuni putbits16(*dst, (uint32_t) (0x100|((*src)[i++]&0x7f)), 9, &olen, 277c3fb4252SPedro F. Giffuni &l); 278c3fb4252SPedro F. Giffuni } 279c3fb4252SPedro F. Giffuni } 280c3fb4252SPedro F. Giffuni 281c3fb4252SPedro F. Giffuni /* Reset unused bits of the last output octet. */ 282c3fb4252SPedro F. Giffuni if ((l != 0) && (l != 8)) { 283c3fb4252SPedro F. Giffuni putbits8(*dst, 0, l, &olen, &l); 284c3fb4252SPedro F. Giffuni } 285c3fb4252SPedro F. Giffuni 286c3fb4252SPedro F. Giffuni /* If result is bigger then original, set flag and flush history. */ 287c3fb4252SPedro F. Giffuni if ((*srcCnt < olen) || ((flags & MPPC_SAVE_HISTORY) == 0)) { 288c3fb4252SPedro F. Giffuni if (*srcCnt < olen) 289c3fb4252SPedro F. Giffuni rtn |= MPPC_EXPANDED; 290c3fb4252SPedro F. Giffuni bzero(history, sizeof(struct MPPC_comp_state)); 291c3fb4252SPedro F. Giffuni state->histptr = MPPE_HIST_LEN; 292c3fb4252SPedro F. Giffuni } 293c3fb4252SPedro F. Giffuni 294c3fb4252SPedro F. Giffuni *src += *srcCnt; 295c3fb4252SPedro F. Giffuni *srcCnt = 0; 296c3fb4252SPedro F. Giffuni *dst += olen; 297c3fb4252SPedro F. Giffuni *dstCnt -= olen; 298c3fb4252SPedro F. Giffuni 299c3fb4252SPedro F. Giffuni return (rtn); 300c3fb4252SPedro F. Giffuni } 301