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