xref: /openbmc/linux/drivers/net/ppp/bsd_comp.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1224cf5adSJeff Kirsher /*
2224cf5adSJeff Kirsher  * Update: The Berkeley copyright was changed, and the change
3224cf5adSJeff Kirsher  * is retroactive to all "true" BSD software (ie everything
4224cf5adSJeff Kirsher  * from UCB as opposed to other peoples code that just carried
5224cf5adSJeff Kirsher  * the same license). The new copyright doesn't clash with the
6224cf5adSJeff Kirsher  * GPL, so the module-only restriction has been removed..
7224cf5adSJeff Kirsher  */
8224cf5adSJeff Kirsher 
9224cf5adSJeff Kirsher /* Because this code is derived from the 4.3BSD compress source:
10224cf5adSJeff Kirsher  *
11224cf5adSJeff Kirsher  * Copyright (c) 1985, 1986 The Regents of the University of California.
12224cf5adSJeff Kirsher  * All rights reserved.
13224cf5adSJeff Kirsher  *
14224cf5adSJeff Kirsher  * This code is derived from software contributed to Berkeley by
15224cf5adSJeff Kirsher  * James A. Woods, derived from original work by Spencer Thomas
16224cf5adSJeff Kirsher  * and Joseph Orost.
17224cf5adSJeff Kirsher  *
18224cf5adSJeff Kirsher  * Redistribution and use in source and binary forms, with or without
19224cf5adSJeff Kirsher  * modification, are permitted provided that the following conditions
20224cf5adSJeff Kirsher  * are met:
21224cf5adSJeff Kirsher  * 1. Redistributions of source code must retain the above copyright
22224cf5adSJeff Kirsher  *    notice, this list of conditions and the following disclaimer.
23224cf5adSJeff Kirsher  * 2. Redistributions in binary form must reproduce the above copyright
24224cf5adSJeff Kirsher  *    notice, this list of conditions and the following disclaimer in the
25224cf5adSJeff Kirsher  *    documentation and/or other materials provided with the distribution.
26224cf5adSJeff Kirsher  * 3. All advertising materials mentioning features or use of this software
27224cf5adSJeff Kirsher  *    must display the following acknowledgement:
28224cf5adSJeff Kirsher  *	This product includes software developed by the University of
29224cf5adSJeff Kirsher  *	California, Berkeley and its contributors.
30224cf5adSJeff Kirsher  * 4. Neither the name of the University nor the names of its contributors
31224cf5adSJeff Kirsher  *    may be used to endorse or promote products derived from this software
32224cf5adSJeff Kirsher  *    without specific prior written permission.
33224cf5adSJeff Kirsher  *
34224cf5adSJeff Kirsher  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35224cf5adSJeff Kirsher  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36224cf5adSJeff Kirsher  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37224cf5adSJeff Kirsher  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38224cf5adSJeff Kirsher  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39224cf5adSJeff Kirsher  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40224cf5adSJeff Kirsher  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41224cf5adSJeff Kirsher  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42224cf5adSJeff Kirsher  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43224cf5adSJeff Kirsher  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44224cf5adSJeff Kirsher  * SUCH DAMAGE.
45224cf5adSJeff Kirsher  */
46224cf5adSJeff Kirsher 
47224cf5adSJeff Kirsher /*
48224cf5adSJeff Kirsher  * This version is for use with contiguous buffers on Linux-derived systems.
49224cf5adSJeff Kirsher  *
50224cf5adSJeff Kirsher  *  ==FILEVERSION 20000226==
51224cf5adSJeff Kirsher  *
52224cf5adSJeff Kirsher  *  NOTE TO MAINTAINERS:
53224cf5adSJeff Kirsher  *     If you modify this file at all, please set the number above to the
54224cf5adSJeff Kirsher  *     date of the modification as YYMMDD (year month day).
55224cf5adSJeff Kirsher  *     bsd_comp.c is shipped with a PPP distribution as well as with
56224cf5adSJeff Kirsher  *     the kernel; if everyone increases the FILEVERSION number above,
57224cf5adSJeff Kirsher  *     then scripts can do the right thing when deciding whether to
58224cf5adSJeff Kirsher  *     install a new bsd_comp.c file. Don't change the format of that
59224cf5adSJeff Kirsher  *     line otherwise, so the installation script can recognize it.
60224cf5adSJeff Kirsher  *
61224cf5adSJeff Kirsher  * From: bsd_comp.c,v 1.3 1994/12/08 01:59:58 paulus Exp
62224cf5adSJeff Kirsher  */
63224cf5adSJeff Kirsher 
64224cf5adSJeff Kirsher #include <linux/module.h>
65224cf5adSJeff Kirsher #include <linux/init.h>
66224cf5adSJeff Kirsher #include <linux/slab.h>
67224cf5adSJeff Kirsher #include <linux/vmalloc.h>
68224cf5adSJeff Kirsher #include <linux/string.h>
69224cf5adSJeff Kirsher 
70224cf5adSJeff Kirsher #include <linux/ppp_defs.h>
71224cf5adSJeff Kirsher 
72224cf5adSJeff Kirsher #undef   PACKETPTR
73224cf5adSJeff Kirsher #define  PACKETPTR 1
74224cf5adSJeff Kirsher #include <linux/ppp-comp.h>
75224cf5adSJeff Kirsher #undef   PACKETPTR
76224cf5adSJeff Kirsher 
77224cf5adSJeff Kirsher #include <asm/byteorder.h>
78224cf5adSJeff Kirsher 
79224cf5adSJeff Kirsher /*
80224cf5adSJeff Kirsher  * PPP "BSD compress" compression
81224cf5adSJeff Kirsher  *  The differences between this compression and the classic BSD LZW
82224cf5adSJeff Kirsher  *  source are obvious from the requirement that the classic code worked
83224cf5adSJeff Kirsher  *  with files while this handles arbitrarily long streams that
84224cf5adSJeff Kirsher  *  are broken into packets.  They are:
85224cf5adSJeff Kirsher  *
86224cf5adSJeff Kirsher  *	When the code size expands, a block of junk is not emitted by
87224cf5adSJeff Kirsher  *	    the compressor and not expected by the decompressor.
88224cf5adSJeff Kirsher  *
89224cf5adSJeff Kirsher  *	New codes are not necessarily assigned every time an old
90224cf5adSJeff Kirsher  *	    code is output by the compressor.  This is because a packet
91224cf5adSJeff Kirsher  *	    end forces a code to be emitted, but does not imply that a
92224cf5adSJeff Kirsher  *	    new sequence has been seen.
93224cf5adSJeff Kirsher  *
94224cf5adSJeff Kirsher  *	The compression ratio is checked at the first end of a packet
95224cf5adSJeff Kirsher  *	    after the appropriate gap.	Besides simplifying and speeding
96224cf5adSJeff Kirsher  *	    things up, this makes it more likely that the transmitter
97224cf5adSJeff Kirsher  *	    and receiver will agree when the dictionary is cleared when
98224cf5adSJeff Kirsher  *	    compression is not going well.
99224cf5adSJeff Kirsher  */
100224cf5adSJeff Kirsher 
101224cf5adSJeff Kirsher /*
102224cf5adSJeff Kirsher  * Macros to extract protocol version and number of bits
103224cf5adSJeff Kirsher  * from the third byte of the BSD Compress CCP configuration option.
104224cf5adSJeff Kirsher  */
105224cf5adSJeff Kirsher 
106224cf5adSJeff Kirsher #define BSD_VERSION(x)	((x) >> 5)
107224cf5adSJeff Kirsher #define BSD_NBITS(x)	((x) & 0x1F)
108224cf5adSJeff Kirsher 
109224cf5adSJeff Kirsher #define BSD_CURRENT_VERSION	1
110224cf5adSJeff Kirsher 
111224cf5adSJeff Kirsher /*
112224cf5adSJeff Kirsher  * A dictionary for doing BSD compress.
113224cf5adSJeff Kirsher  */
114224cf5adSJeff Kirsher 
115224cf5adSJeff Kirsher struct bsd_dict {
116224cf5adSJeff Kirsher     union {				/* hash value */
117224cf5adSJeff Kirsher 	unsigned long	fcode;
118224cf5adSJeff Kirsher 	struct {
119224cf5adSJeff Kirsher #if defined(__LITTLE_ENDIAN)		/* Little endian order */
120224cf5adSJeff Kirsher 	    unsigned short	prefix;	/* preceding code */
121224cf5adSJeff Kirsher 	    unsigned char	suffix; /* last character of new code */
122224cf5adSJeff Kirsher 	    unsigned char	pad;
123224cf5adSJeff Kirsher #elif defined(__BIG_ENDIAN)		/* Big endian order */
124224cf5adSJeff Kirsher 	    unsigned char	pad;
125224cf5adSJeff Kirsher 	    unsigned char	suffix; /* last character of new code */
126224cf5adSJeff Kirsher 	    unsigned short	prefix; /* preceding code */
127224cf5adSJeff Kirsher #else
128224cf5adSJeff Kirsher #error Endianness not defined...
129224cf5adSJeff Kirsher #endif
130224cf5adSJeff Kirsher 	} hs;
131224cf5adSJeff Kirsher     } f;
132224cf5adSJeff Kirsher     unsigned short codem1;		/* output of hash table -1 */
133224cf5adSJeff Kirsher     unsigned short cptr;		/* map code to hash table entry */
134224cf5adSJeff Kirsher };
135224cf5adSJeff Kirsher 
136224cf5adSJeff Kirsher struct bsd_db {
137224cf5adSJeff Kirsher     int	    totlen;			/* length of this structure */
138224cf5adSJeff Kirsher     unsigned int   hsize;		/* size of the hash table */
139224cf5adSJeff Kirsher     unsigned char  hshift;		/* used in hash function */
140224cf5adSJeff Kirsher     unsigned char  n_bits;		/* current bits/code */
141224cf5adSJeff Kirsher     unsigned char  maxbits;		/* maximum bits/code */
142224cf5adSJeff Kirsher     unsigned char  debug;		/* non-zero if debug desired */
143224cf5adSJeff Kirsher     unsigned char  unit;		/* ppp unit number */
144224cf5adSJeff Kirsher     unsigned short seqno;		/* sequence # of next packet */
145224cf5adSJeff Kirsher     unsigned int   mru;			/* size of receive (decompress) bufr */
146224cf5adSJeff Kirsher     unsigned int   maxmaxcode;		/* largest valid code */
147224cf5adSJeff Kirsher     unsigned int   max_ent;		/* largest code in use */
148224cf5adSJeff Kirsher     unsigned int   in_count;		/* uncompressed bytes, aged */
149224cf5adSJeff Kirsher     unsigned int   bytes_out;		/* compressed bytes, aged */
150224cf5adSJeff Kirsher     unsigned int   ratio;		/* recent compression ratio */
151224cf5adSJeff Kirsher     unsigned int   checkpoint;		/* when to next check the ratio */
152224cf5adSJeff Kirsher     unsigned int   clear_count;		/* times dictionary cleared */
153224cf5adSJeff Kirsher     unsigned int   incomp_count;	/* incompressible packets */
154224cf5adSJeff Kirsher     unsigned int   incomp_bytes;	/* incompressible bytes */
155224cf5adSJeff Kirsher     unsigned int   uncomp_count;	/* uncompressed packets */
156224cf5adSJeff Kirsher     unsigned int   uncomp_bytes;	/* uncompressed bytes */
157224cf5adSJeff Kirsher     unsigned int   comp_count;		/* compressed packets */
158224cf5adSJeff Kirsher     unsigned int   comp_bytes;		/* compressed bytes */
159224cf5adSJeff Kirsher     unsigned short  *lens;		/* array of lengths of codes */
160224cf5adSJeff Kirsher     struct bsd_dict *dict;		/* dictionary */
161224cf5adSJeff Kirsher };
162224cf5adSJeff Kirsher 
163224cf5adSJeff Kirsher #define BSD_OVHD	2		/* BSD compress overhead/packet */
164224cf5adSJeff Kirsher #define MIN_BSD_BITS	9
165224cf5adSJeff Kirsher #define BSD_INIT_BITS	MIN_BSD_BITS
166224cf5adSJeff Kirsher #define MAX_BSD_BITS	15
167224cf5adSJeff Kirsher 
168224cf5adSJeff Kirsher static void	bsd_free (void *state);
169224cf5adSJeff Kirsher static void	*bsd_alloc(unsigned char *options, int opt_len, int decomp);
170224cf5adSJeff Kirsher static void	*bsd_comp_alloc (unsigned char *options, int opt_len);
171224cf5adSJeff Kirsher static void	*bsd_decomp_alloc (unsigned char *options, int opt_len);
172224cf5adSJeff Kirsher 
173224cf5adSJeff Kirsher static int	bsd_init        (void *db, unsigned char *options,
174224cf5adSJeff Kirsher 			         int opt_len, int unit, int debug, int decomp);
175224cf5adSJeff Kirsher static int	bsd_comp_init   (void *state, unsigned char *options,
176224cf5adSJeff Kirsher 			         int opt_len, int unit, int opthdr, int debug);
177224cf5adSJeff Kirsher static int	bsd_decomp_init (void *state, unsigned char *options,
178224cf5adSJeff Kirsher 				 int opt_len, int unit, int opthdr, int mru,
179224cf5adSJeff Kirsher 				 int debug);
180224cf5adSJeff Kirsher 
181224cf5adSJeff Kirsher static void	bsd_reset (void *state);
182224cf5adSJeff Kirsher static void	bsd_comp_stats (void *state, struct compstat *stats);
183224cf5adSJeff Kirsher 
184224cf5adSJeff Kirsher static int	bsd_compress (void *state, unsigned char *rptr,
185224cf5adSJeff Kirsher 			      unsigned char *obuf, int isize, int osize);
186224cf5adSJeff Kirsher static void	bsd_incomp (void *state, unsigned char *ibuf, int icnt);
187224cf5adSJeff Kirsher 
188224cf5adSJeff Kirsher static int	bsd_decompress (void *state, unsigned char *ibuf, int isize,
189224cf5adSJeff Kirsher 				unsigned char *obuf, int osize);
190224cf5adSJeff Kirsher 
191224cf5adSJeff Kirsher /* These are in ppp_generic.c */
192224cf5adSJeff Kirsher extern int  ppp_register_compressor   (struct compressor *cp);
193224cf5adSJeff Kirsher extern void ppp_unregister_compressor (struct compressor *cp);
194224cf5adSJeff Kirsher 
195224cf5adSJeff Kirsher /*
196224cf5adSJeff Kirsher  * the next two codes should not be changed lightly, as they must not
197224cf5adSJeff Kirsher  * lie within the contiguous general code space.
198224cf5adSJeff Kirsher  */
199224cf5adSJeff Kirsher #define CLEAR	256			/* table clear output code */
200224cf5adSJeff Kirsher #define FIRST	257			/* first free entry */
201224cf5adSJeff Kirsher #define LAST	255
202224cf5adSJeff Kirsher 
203224cf5adSJeff Kirsher #define MAXCODE(b)	((1 << (b)) - 1)
204224cf5adSJeff Kirsher #define BADCODEM1	MAXCODE(MAX_BSD_BITS)
205224cf5adSJeff Kirsher 
206224cf5adSJeff Kirsher #define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
207224cf5adSJeff Kirsher 					 ^ (unsigned long)(prefix))
208224cf5adSJeff Kirsher #define BSD_KEY(prefix,suffix)		((((unsigned long)(suffix)) << 16) \
209224cf5adSJeff Kirsher 					 + (unsigned long)(prefix))
210224cf5adSJeff Kirsher 
211224cf5adSJeff Kirsher #define CHECK_GAP	10000		/* Ratio check interval */
212224cf5adSJeff Kirsher 
213224cf5adSJeff Kirsher #define RATIO_SCALE_LOG	8
214224cf5adSJeff Kirsher #define RATIO_SCALE	(1<<RATIO_SCALE_LOG)
215224cf5adSJeff Kirsher #define RATIO_MAX	(0x7fffffff>>RATIO_SCALE_LOG)
216224cf5adSJeff Kirsher 
217224cf5adSJeff Kirsher /*
218224cf5adSJeff Kirsher  * clear the dictionary
219224cf5adSJeff Kirsher  */
220224cf5adSJeff Kirsher 
221224cf5adSJeff Kirsher static void
bsd_clear(struct bsd_db * db)222224cf5adSJeff Kirsher bsd_clear(struct bsd_db *db)
223224cf5adSJeff Kirsher {
224224cf5adSJeff Kirsher     db->clear_count++;
225224cf5adSJeff Kirsher     db->max_ent      = FIRST-1;
226224cf5adSJeff Kirsher     db->n_bits       = BSD_INIT_BITS;
227224cf5adSJeff Kirsher     db->bytes_out    = 0;
228224cf5adSJeff Kirsher     db->in_count     = 0;
229224cf5adSJeff Kirsher     db->ratio	     = 0;
230224cf5adSJeff Kirsher     db->checkpoint   = CHECK_GAP;
231224cf5adSJeff Kirsher }
232224cf5adSJeff Kirsher 
233224cf5adSJeff Kirsher /*
234224cf5adSJeff Kirsher  * If the dictionary is full, then see if it is time to reset it.
235224cf5adSJeff Kirsher  *
236224cf5adSJeff Kirsher  * Compute the compression ratio using fixed-point arithmetic
237224cf5adSJeff Kirsher  * with 8 fractional bits.
238224cf5adSJeff Kirsher  *
239224cf5adSJeff Kirsher  * Since we have an infinite stream instead of a single file,
240224cf5adSJeff Kirsher  * watch only the local compression ratio.
241224cf5adSJeff Kirsher  *
242224cf5adSJeff Kirsher  * Since both peers must reset the dictionary at the same time even in
243224cf5adSJeff Kirsher  * the absence of CLEAR codes (while packets are incompressible), they
244224cf5adSJeff Kirsher  * must compute the same ratio.
245224cf5adSJeff Kirsher  */
246224cf5adSJeff Kirsher 
bsd_check(struct bsd_db * db)247224cf5adSJeff Kirsher static int bsd_check (struct bsd_db *db)	/* 1=output CLEAR */
248224cf5adSJeff Kirsher   {
249224cf5adSJeff Kirsher     unsigned int new_ratio;
250224cf5adSJeff Kirsher 
251224cf5adSJeff Kirsher     if (db->in_count >= db->checkpoint)
252224cf5adSJeff Kirsher       {
253224cf5adSJeff Kirsher 	/* age the ratio by limiting the size of the counts */
254224cf5adSJeff Kirsher 	if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX)
255224cf5adSJeff Kirsher 	  {
256224cf5adSJeff Kirsher 	    db->in_count  -= (db->in_count  >> 2);
257224cf5adSJeff Kirsher 	    db->bytes_out -= (db->bytes_out >> 2);
258224cf5adSJeff Kirsher 	  }
259224cf5adSJeff Kirsher 
260224cf5adSJeff Kirsher 	db->checkpoint = db->in_count + CHECK_GAP;
261224cf5adSJeff Kirsher 
262224cf5adSJeff Kirsher 	if (db->max_ent >= db->maxmaxcode)
263224cf5adSJeff Kirsher 	  {
264224cf5adSJeff Kirsher 	    /* Reset the dictionary only if the ratio is worse,
265224cf5adSJeff Kirsher 	     * or if it looks as if it has been poisoned
266224cf5adSJeff Kirsher 	     * by incompressible data.
267224cf5adSJeff Kirsher 	     *
268224cf5adSJeff Kirsher 	     * This does not overflow, because
269224cf5adSJeff Kirsher 	     *	db->in_count <= RATIO_MAX.
270224cf5adSJeff Kirsher 	     */
271224cf5adSJeff Kirsher 
272224cf5adSJeff Kirsher 	    new_ratio = db->in_count << RATIO_SCALE_LOG;
273224cf5adSJeff Kirsher 	    if (db->bytes_out != 0)
274224cf5adSJeff Kirsher 	      {
275224cf5adSJeff Kirsher 		new_ratio /= db->bytes_out;
276224cf5adSJeff Kirsher 	      }
277224cf5adSJeff Kirsher 
278224cf5adSJeff Kirsher 	    if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
279224cf5adSJeff Kirsher 	      {
280224cf5adSJeff Kirsher 		bsd_clear (db);
281224cf5adSJeff Kirsher 		return 1;
282224cf5adSJeff Kirsher 	      }
283224cf5adSJeff Kirsher 	    db->ratio = new_ratio;
284224cf5adSJeff Kirsher 	  }
285224cf5adSJeff Kirsher       }
286224cf5adSJeff Kirsher     return 0;
287224cf5adSJeff Kirsher   }
288224cf5adSJeff Kirsher 
289224cf5adSJeff Kirsher /*
290224cf5adSJeff Kirsher  * Return statistics.
291224cf5adSJeff Kirsher  */
292224cf5adSJeff Kirsher 
bsd_comp_stats(void * state,struct compstat * stats)293224cf5adSJeff Kirsher static void bsd_comp_stats (void *state, struct compstat *stats)
294224cf5adSJeff Kirsher   {
295224cf5adSJeff Kirsher     struct bsd_db *db = (struct bsd_db *) state;
296224cf5adSJeff Kirsher 
297224cf5adSJeff Kirsher     stats->unc_bytes    = db->uncomp_bytes;
298224cf5adSJeff Kirsher     stats->unc_packets  = db->uncomp_count;
299224cf5adSJeff Kirsher     stats->comp_bytes   = db->comp_bytes;
300224cf5adSJeff Kirsher     stats->comp_packets = db->comp_count;
301224cf5adSJeff Kirsher     stats->inc_bytes    = db->incomp_bytes;
302224cf5adSJeff Kirsher     stats->inc_packets  = db->incomp_count;
303224cf5adSJeff Kirsher     stats->in_count     = db->in_count;
304224cf5adSJeff Kirsher     stats->bytes_out    = db->bytes_out;
305224cf5adSJeff Kirsher   }
306224cf5adSJeff Kirsher 
307224cf5adSJeff Kirsher /*
308224cf5adSJeff Kirsher  * Reset state, as on a CCP ResetReq.
309224cf5adSJeff Kirsher  */
310224cf5adSJeff Kirsher 
bsd_reset(void * state)311224cf5adSJeff Kirsher static void bsd_reset (void *state)
312224cf5adSJeff Kirsher   {
313224cf5adSJeff Kirsher     struct bsd_db *db = (struct bsd_db *) state;
314224cf5adSJeff Kirsher 
315224cf5adSJeff Kirsher     bsd_clear(db);
316224cf5adSJeff Kirsher 
317224cf5adSJeff Kirsher     db->seqno       = 0;
318224cf5adSJeff Kirsher     db->clear_count = 0;
319224cf5adSJeff Kirsher   }
320224cf5adSJeff Kirsher 
321224cf5adSJeff Kirsher /*
322224cf5adSJeff Kirsher  * Release the compression structure
323224cf5adSJeff Kirsher  */
324224cf5adSJeff Kirsher 
bsd_free(void * state)325224cf5adSJeff Kirsher static void bsd_free (void *state)
326224cf5adSJeff Kirsher {
327224cf5adSJeff Kirsher 	struct bsd_db *db = state;
328224cf5adSJeff Kirsher 
329224cf5adSJeff Kirsher 	if (!db)
330224cf5adSJeff Kirsher 		return;
331224cf5adSJeff Kirsher 
332224cf5adSJeff Kirsher /*
333224cf5adSJeff Kirsher  * Release the dictionary
334224cf5adSJeff Kirsher  */
335224cf5adSJeff Kirsher 	vfree(db->dict);
336224cf5adSJeff Kirsher 	db->dict = NULL;
337224cf5adSJeff Kirsher /*
338224cf5adSJeff Kirsher  * Release the string buffer
339224cf5adSJeff Kirsher  */
340224cf5adSJeff Kirsher 	vfree(db->lens);
341224cf5adSJeff Kirsher 	db->lens = NULL;
342224cf5adSJeff Kirsher /*
343224cf5adSJeff Kirsher  * Finally release the structure itself.
344224cf5adSJeff Kirsher  */
345224cf5adSJeff Kirsher 	kfree(db);
346224cf5adSJeff Kirsher }
347224cf5adSJeff Kirsher 
348224cf5adSJeff Kirsher /*
349224cf5adSJeff Kirsher  * Allocate space for a (de) compressor.
350224cf5adSJeff Kirsher  */
351224cf5adSJeff Kirsher 
bsd_alloc(unsigned char * options,int opt_len,int decomp)352224cf5adSJeff Kirsher static void *bsd_alloc (unsigned char *options, int opt_len, int decomp)
353224cf5adSJeff Kirsher   {
354224cf5adSJeff Kirsher     int bits;
355224cf5adSJeff Kirsher     unsigned int hsize, hshift, maxmaxcode;
356224cf5adSJeff Kirsher     struct bsd_db *db;
357224cf5adSJeff Kirsher 
358224cf5adSJeff Kirsher     if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
359224cf5adSJeff Kirsher 	|| BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
360224cf5adSJeff Kirsher       {
361224cf5adSJeff Kirsher 	return NULL;
362224cf5adSJeff Kirsher       }
363224cf5adSJeff Kirsher 
364224cf5adSJeff Kirsher     bits = BSD_NBITS(options[2]);
365224cf5adSJeff Kirsher 
366224cf5adSJeff Kirsher     switch (bits)
367224cf5adSJeff Kirsher       {
368224cf5adSJeff Kirsher     case 9:			/* needs 82152 for both directions */
369224cf5adSJeff Kirsher     case 10:			/* needs 84144 */
370224cf5adSJeff Kirsher     case 11:			/* needs 88240 */
371224cf5adSJeff Kirsher     case 12:			/* needs 96432 */
372224cf5adSJeff Kirsher 	hsize = 5003;
373224cf5adSJeff Kirsher 	hshift = 4;
374224cf5adSJeff Kirsher 	break;
375224cf5adSJeff Kirsher     case 13:			/* needs 176784 */
376224cf5adSJeff Kirsher 	hsize = 9001;
377224cf5adSJeff Kirsher 	hshift = 5;
378224cf5adSJeff Kirsher 	break;
379224cf5adSJeff Kirsher     case 14:			/* needs 353744 */
380224cf5adSJeff Kirsher 	hsize = 18013;
381224cf5adSJeff Kirsher 	hshift = 6;
382224cf5adSJeff Kirsher 	break;
383224cf5adSJeff Kirsher     case 15:			/* needs 691440 */
384224cf5adSJeff Kirsher 	hsize = 35023;
385224cf5adSJeff Kirsher 	hshift = 7;
386224cf5adSJeff Kirsher 	break;
387224cf5adSJeff Kirsher     case 16:			/* needs 1366160--far too much, */
388224cf5adSJeff Kirsher 	/* hsize = 69001; */	/* and 69001 is too big for cptr */
389224cf5adSJeff Kirsher 	/* hshift = 8; */	/* in struct bsd_db */
390224cf5adSJeff Kirsher 	/* break; */
391224cf5adSJeff Kirsher     default:
392224cf5adSJeff Kirsher 	return NULL;
393224cf5adSJeff Kirsher       }
394224cf5adSJeff Kirsher /*
395224cf5adSJeff Kirsher  * Allocate the main control structure for this instance.
396224cf5adSJeff Kirsher  */
397224cf5adSJeff Kirsher     maxmaxcode = MAXCODE(bits);
398224cf5adSJeff Kirsher     db         = kzalloc(sizeof (struct bsd_db),
399224cf5adSJeff Kirsher 					    GFP_KERNEL);
400224cf5adSJeff Kirsher     if (!db)
401224cf5adSJeff Kirsher       {
402224cf5adSJeff Kirsher 	return NULL;
403224cf5adSJeff Kirsher       }
404224cf5adSJeff Kirsher 
405224cf5adSJeff Kirsher /*
406224cf5adSJeff Kirsher  * Allocate space for the dictionary. This may be more than one page in
407224cf5adSJeff Kirsher  * length.
408224cf5adSJeff Kirsher  */
409*42bc47b3SKees Cook     db->dict = vmalloc(array_size(hsize, sizeof(struct bsd_dict)));
410224cf5adSJeff Kirsher     if (!db->dict)
411224cf5adSJeff Kirsher       {
412224cf5adSJeff Kirsher 	bsd_free (db);
413224cf5adSJeff Kirsher 	return NULL;
414224cf5adSJeff Kirsher       }
415224cf5adSJeff Kirsher 
416224cf5adSJeff Kirsher /*
417224cf5adSJeff Kirsher  * If this is the compression buffer then there is no length data.
418224cf5adSJeff Kirsher  */
419224cf5adSJeff Kirsher     if (!decomp)
420224cf5adSJeff Kirsher       {
421224cf5adSJeff Kirsher 	db->lens = NULL;
422224cf5adSJeff Kirsher       }
423224cf5adSJeff Kirsher /*
424224cf5adSJeff Kirsher  * For decompression, the length information is needed as well.
425224cf5adSJeff Kirsher  */
426224cf5adSJeff Kirsher     else
427224cf5adSJeff Kirsher       {
428*42bc47b3SKees Cook         db->lens = vmalloc(array_size(sizeof(db->lens[0]), (maxmaxcode + 1)));
429224cf5adSJeff Kirsher 	if (!db->lens)
430224cf5adSJeff Kirsher 	  {
431224cf5adSJeff Kirsher 	    bsd_free (db);
432224cf5adSJeff Kirsher 	    return NULL;
433224cf5adSJeff Kirsher 	  }
434224cf5adSJeff Kirsher       }
435224cf5adSJeff Kirsher /*
436224cf5adSJeff Kirsher  * Initialize the data information for the compression code
437224cf5adSJeff Kirsher  */
438224cf5adSJeff Kirsher     db->totlen     = sizeof (struct bsd_db)   +
439224cf5adSJeff Kirsher 		    (sizeof (struct bsd_dict) * hsize);
440224cf5adSJeff Kirsher 
441224cf5adSJeff Kirsher     db->hsize      = hsize;
442224cf5adSJeff Kirsher     db->hshift     = hshift;
443224cf5adSJeff Kirsher     db->maxmaxcode = maxmaxcode;
444224cf5adSJeff Kirsher     db->maxbits    = bits;
445224cf5adSJeff Kirsher 
446224cf5adSJeff Kirsher     return (void *) db;
447224cf5adSJeff Kirsher   }
448224cf5adSJeff Kirsher 
bsd_comp_alloc(unsigned char * options,int opt_len)449224cf5adSJeff Kirsher static void *bsd_comp_alloc (unsigned char *options, int opt_len)
450224cf5adSJeff Kirsher   {
451224cf5adSJeff Kirsher     return bsd_alloc (options, opt_len, 0);
452224cf5adSJeff Kirsher   }
453224cf5adSJeff Kirsher 
bsd_decomp_alloc(unsigned char * options,int opt_len)454224cf5adSJeff Kirsher static void *bsd_decomp_alloc (unsigned char *options, int opt_len)
455224cf5adSJeff Kirsher   {
456224cf5adSJeff Kirsher     return bsd_alloc (options, opt_len, 1);
457224cf5adSJeff Kirsher   }
458224cf5adSJeff Kirsher 
459224cf5adSJeff Kirsher /*
460224cf5adSJeff Kirsher  * Initialize the database.
461224cf5adSJeff Kirsher  */
462224cf5adSJeff Kirsher 
bsd_init(void * state,unsigned char * options,int opt_len,int unit,int debug,int decomp)463224cf5adSJeff Kirsher static int bsd_init (void *state, unsigned char *options,
464224cf5adSJeff Kirsher 		     int opt_len, int unit, int debug, int decomp)
465224cf5adSJeff Kirsher   {
466224cf5adSJeff Kirsher     struct bsd_db *db = state;
467224cf5adSJeff Kirsher     int indx;
468224cf5adSJeff Kirsher 
469224cf5adSJeff Kirsher     if ((opt_len != 3) || (options[0] != CI_BSD_COMPRESS) || (options[1] != 3)
470224cf5adSJeff Kirsher 	|| (BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
471224cf5adSJeff Kirsher 	|| (BSD_NBITS(options[2]) != db->maxbits)
472224cf5adSJeff Kirsher 	|| (decomp && db->lens == NULL))
473224cf5adSJeff Kirsher       {
474224cf5adSJeff Kirsher 	return 0;
475224cf5adSJeff Kirsher       }
476224cf5adSJeff Kirsher 
477224cf5adSJeff Kirsher     if (decomp)
478224cf5adSJeff Kirsher       {
479224cf5adSJeff Kirsher 	indx = LAST;
480224cf5adSJeff Kirsher 	do
481224cf5adSJeff Kirsher 	  {
482224cf5adSJeff Kirsher 	    db->lens[indx] = 1;
483224cf5adSJeff Kirsher 	  }
484224cf5adSJeff Kirsher 	while (indx-- > 0);
485224cf5adSJeff Kirsher       }
486224cf5adSJeff Kirsher 
487224cf5adSJeff Kirsher     indx = db->hsize;
488224cf5adSJeff Kirsher     while (indx-- != 0)
489224cf5adSJeff Kirsher       {
490224cf5adSJeff Kirsher 	db->dict[indx].codem1 = BADCODEM1;
491224cf5adSJeff Kirsher 	db->dict[indx].cptr   = 0;
492224cf5adSJeff Kirsher       }
493224cf5adSJeff Kirsher 
494224cf5adSJeff Kirsher     db->unit = unit;
495224cf5adSJeff Kirsher     db->mru  = 0;
496224cf5adSJeff Kirsher #ifndef DEBUG
497224cf5adSJeff Kirsher     if (debug)
498224cf5adSJeff Kirsher #endif
499224cf5adSJeff Kirsher       db->debug = 1;
500224cf5adSJeff Kirsher 
501224cf5adSJeff Kirsher     bsd_reset(db);
502224cf5adSJeff Kirsher 
503224cf5adSJeff Kirsher     return 1;
504224cf5adSJeff Kirsher   }
505224cf5adSJeff Kirsher 
bsd_comp_init(void * state,unsigned char * options,int opt_len,int unit,int opthdr,int debug)506224cf5adSJeff Kirsher static int bsd_comp_init (void *state, unsigned char *options,
507224cf5adSJeff Kirsher 			  int opt_len, int unit, int opthdr, int debug)
508224cf5adSJeff Kirsher   {
509224cf5adSJeff Kirsher     return bsd_init (state, options, opt_len, unit, debug, 0);
510224cf5adSJeff Kirsher   }
511224cf5adSJeff Kirsher 
bsd_decomp_init(void * state,unsigned char * options,int opt_len,int unit,int opthdr,int mru,int debug)512224cf5adSJeff Kirsher static int bsd_decomp_init (void *state, unsigned char *options,
513224cf5adSJeff Kirsher 			    int opt_len, int unit, int opthdr, int mru,
514224cf5adSJeff Kirsher 			    int debug)
515224cf5adSJeff Kirsher   {
516224cf5adSJeff Kirsher     return bsd_init (state, options, opt_len, unit, debug, 1);
517224cf5adSJeff Kirsher   }
518224cf5adSJeff Kirsher 
519224cf5adSJeff Kirsher /*
520224cf5adSJeff Kirsher  * Obtain pointers to the various structures in the compression tables
521224cf5adSJeff Kirsher  */
522224cf5adSJeff Kirsher 
523224cf5adSJeff Kirsher #define dict_ptrx(p,idx) &(p->dict[idx])
524224cf5adSJeff Kirsher #define lens_ptrx(p,idx) &(p->lens[idx])
525224cf5adSJeff Kirsher 
526224cf5adSJeff Kirsher #ifdef DEBUG
lens_ptr(struct bsd_db * db,int idx)527224cf5adSJeff Kirsher static unsigned short *lens_ptr(struct bsd_db *db, int idx)
528224cf5adSJeff Kirsher   {
529224cf5adSJeff Kirsher     if ((unsigned int) idx > (unsigned int) db->maxmaxcode)
530224cf5adSJeff Kirsher       {
531224cf5adSJeff Kirsher 	printk ("<9>ppp: lens_ptr(%d) > max\n", idx);
532224cf5adSJeff Kirsher 	idx = 0;
533224cf5adSJeff Kirsher       }
534224cf5adSJeff Kirsher     return lens_ptrx (db, idx);
535224cf5adSJeff Kirsher   }
536224cf5adSJeff Kirsher 
dict_ptr(struct bsd_db * db,int idx)537224cf5adSJeff Kirsher static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx)
538224cf5adSJeff Kirsher   {
539224cf5adSJeff Kirsher     if ((unsigned int) idx >= (unsigned int) db->hsize)
540224cf5adSJeff Kirsher       {
541224cf5adSJeff Kirsher 	printk ("<9>ppp: dict_ptr(%d) > max\n", idx);
542224cf5adSJeff Kirsher 	idx = 0;
543224cf5adSJeff Kirsher       }
544224cf5adSJeff Kirsher     return dict_ptrx (db, idx);
545224cf5adSJeff Kirsher   }
546224cf5adSJeff Kirsher 
547224cf5adSJeff Kirsher #else
548224cf5adSJeff Kirsher #define lens_ptr(db,idx) lens_ptrx(db,idx)
549224cf5adSJeff Kirsher #define dict_ptr(db,idx) dict_ptrx(db,idx)
550224cf5adSJeff Kirsher #endif
551224cf5adSJeff Kirsher 
552224cf5adSJeff Kirsher /*
553224cf5adSJeff Kirsher  * compress a packet
554224cf5adSJeff Kirsher  *
555224cf5adSJeff Kirsher  *	The result of this function is the size of the compressed
556224cf5adSJeff Kirsher  *	packet. A zero is returned if the packet was not compressed
557224cf5adSJeff Kirsher  *	for some reason, such as the size being larger than uncompressed.
558224cf5adSJeff Kirsher  *
559224cf5adSJeff Kirsher  *	One change from the BSD compress command is that when the
560224cf5adSJeff Kirsher  *	code size expands, we do not output a bunch of padding.
561224cf5adSJeff Kirsher  */
562224cf5adSJeff Kirsher 
bsd_compress(void * state,unsigned char * rptr,unsigned char * obuf,int isize,int osize)563224cf5adSJeff Kirsher static int bsd_compress (void *state, unsigned char *rptr, unsigned char *obuf,
564224cf5adSJeff Kirsher 			 int isize, int osize)
565224cf5adSJeff Kirsher   {
566224cf5adSJeff Kirsher     struct bsd_db *db;
567224cf5adSJeff Kirsher     int hshift;
568224cf5adSJeff Kirsher     unsigned int max_ent;
569224cf5adSJeff Kirsher     unsigned int n_bits;
570224cf5adSJeff Kirsher     unsigned int bitno;
571224cf5adSJeff Kirsher     unsigned long accm;
572224cf5adSJeff Kirsher     int ent;
573224cf5adSJeff Kirsher     unsigned long fcode;
574224cf5adSJeff Kirsher     struct bsd_dict *dictp;
575224cf5adSJeff Kirsher     unsigned char c;
576224cf5adSJeff Kirsher     int hval;
577224cf5adSJeff Kirsher     int disp;
578224cf5adSJeff Kirsher     int ilen;
579224cf5adSJeff Kirsher     int mxcode;
580224cf5adSJeff Kirsher     unsigned char *wptr;
581224cf5adSJeff Kirsher     int olen;
582224cf5adSJeff Kirsher 
583224cf5adSJeff Kirsher #define PUTBYTE(v)			\
584224cf5adSJeff Kirsher   {					\
585224cf5adSJeff Kirsher     ++olen;				\
586224cf5adSJeff Kirsher     if (wptr)				\
587224cf5adSJeff Kirsher       {					\
588224cf5adSJeff Kirsher 	*wptr++ = (unsigned char) (v);	\
589224cf5adSJeff Kirsher 	if (olen >= osize)		\
590224cf5adSJeff Kirsher 	  {				\
591224cf5adSJeff Kirsher 	    wptr = NULL;		\
592224cf5adSJeff Kirsher 	  }				\
593224cf5adSJeff Kirsher       }					\
594224cf5adSJeff Kirsher   }
595224cf5adSJeff Kirsher 
596224cf5adSJeff Kirsher #define OUTPUT(ent)			\
597224cf5adSJeff Kirsher   {					\
598224cf5adSJeff Kirsher     bitno -= n_bits;			\
599224cf5adSJeff Kirsher     accm |= ((ent) << bitno);		\
600224cf5adSJeff Kirsher     do					\
601224cf5adSJeff Kirsher       {					\
602224cf5adSJeff Kirsher 	PUTBYTE(accm >> 24);		\
603224cf5adSJeff Kirsher 	accm <<= 8;			\
604224cf5adSJeff Kirsher 	bitno += 8;			\
605224cf5adSJeff Kirsher       }					\
606224cf5adSJeff Kirsher     while (bitno <= 24);		\
607224cf5adSJeff Kirsher   }
608224cf5adSJeff Kirsher 
609224cf5adSJeff Kirsher   /*
610224cf5adSJeff Kirsher    * If the protocol is not in the range we're interested in,
611224cf5adSJeff Kirsher    * just return without compressing the packet.  If it is,
612224cf5adSJeff Kirsher    * the protocol becomes the first byte to compress.
613224cf5adSJeff Kirsher    */
614224cf5adSJeff Kirsher 
615224cf5adSJeff Kirsher     ent = PPP_PROTOCOL(rptr);
616224cf5adSJeff Kirsher     if (ent < 0x21 || ent > 0xf9)
617224cf5adSJeff Kirsher       {
618224cf5adSJeff Kirsher 	return 0;
619224cf5adSJeff Kirsher       }
620224cf5adSJeff Kirsher 
621224cf5adSJeff Kirsher     db      = (struct bsd_db *) state;
622224cf5adSJeff Kirsher     hshift  = db->hshift;
623224cf5adSJeff Kirsher     max_ent = db->max_ent;
624224cf5adSJeff Kirsher     n_bits  = db->n_bits;
625224cf5adSJeff Kirsher     bitno   = 32;
626224cf5adSJeff Kirsher     accm    = 0;
627224cf5adSJeff Kirsher     mxcode  = MAXCODE (n_bits);
628224cf5adSJeff Kirsher 
629224cf5adSJeff Kirsher     /* Initialize the output pointers */
630224cf5adSJeff Kirsher     wptr  = obuf;
631224cf5adSJeff Kirsher     olen  = PPP_HDRLEN + BSD_OVHD;
632224cf5adSJeff Kirsher 
633224cf5adSJeff Kirsher     if (osize > isize)
634224cf5adSJeff Kirsher       {
635224cf5adSJeff Kirsher 	osize = isize;
636224cf5adSJeff Kirsher       }
637224cf5adSJeff Kirsher 
638224cf5adSJeff Kirsher     /* This is the PPP header information */
639224cf5adSJeff Kirsher     if (wptr)
640224cf5adSJeff Kirsher       {
641224cf5adSJeff Kirsher 	*wptr++ = PPP_ADDRESS(rptr);
642224cf5adSJeff Kirsher 	*wptr++ = PPP_CONTROL(rptr);
643224cf5adSJeff Kirsher 	*wptr++ = 0;
644224cf5adSJeff Kirsher 	*wptr++ = PPP_COMP;
645224cf5adSJeff Kirsher 	*wptr++ = db->seqno >> 8;
646224cf5adSJeff Kirsher 	*wptr++ = db->seqno;
647224cf5adSJeff Kirsher       }
648224cf5adSJeff Kirsher 
649224cf5adSJeff Kirsher     /* Skip the input header */
650224cf5adSJeff Kirsher     rptr  += PPP_HDRLEN;
651224cf5adSJeff Kirsher     isize -= PPP_HDRLEN;
652224cf5adSJeff Kirsher     ilen   = ++isize;	/* Low byte of protocol is counted as input */
653224cf5adSJeff Kirsher 
654224cf5adSJeff Kirsher     while (--ilen > 0)
655224cf5adSJeff Kirsher       {
656224cf5adSJeff Kirsher 	c     = *rptr++;
657224cf5adSJeff Kirsher 	fcode = BSD_KEY  (ent, c);
658224cf5adSJeff Kirsher 	hval  = BSD_HASH (ent, c, hshift);
659224cf5adSJeff Kirsher 	dictp = dict_ptr (db, hval);
660224cf5adSJeff Kirsher 
661224cf5adSJeff Kirsher 	/* Validate and then check the entry. */
662224cf5adSJeff Kirsher 	if (dictp->codem1 >= max_ent)
663224cf5adSJeff Kirsher 	  {
664224cf5adSJeff Kirsher 	    goto nomatch;
665224cf5adSJeff Kirsher 	  }
666224cf5adSJeff Kirsher 
667224cf5adSJeff Kirsher 	if (dictp->f.fcode == fcode)
668224cf5adSJeff Kirsher 	  {
669224cf5adSJeff Kirsher 	    ent = dictp->codem1 + 1;
670224cf5adSJeff Kirsher 	    continue;	/* found (prefix,suffix) */
671224cf5adSJeff Kirsher 	  }
672224cf5adSJeff Kirsher 
673224cf5adSJeff Kirsher 	/* continue probing until a match or invalid entry */
674224cf5adSJeff Kirsher 	disp = (hval == 0) ? 1 : hval;
675224cf5adSJeff Kirsher 
676224cf5adSJeff Kirsher 	do
677224cf5adSJeff Kirsher 	  {
678224cf5adSJeff Kirsher 	    hval += disp;
679224cf5adSJeff Kirsher 	    if (hval >= db->hsize)
680224cf5adSJeff Kirsher 	      {
681224cf5adSJeff Kirsher 		hval -= db->hsize;
682224cf5adSJeff Kirsher 	      }
683224cf5adSJeff Kirsher 	    dictp = dict_ptr (db, hval);
684224cf5adSJeff Kirsher 	    if (dictp->codem1 >= max_ent)
685224cf5adSJeff Kirsher 	      {
686224cf5adSJeff Kirsher 		goto nomatch;
687224cf5adSJeff Kirsher 	      }
688224cf5adSJeff Kirsher 	  }
689224cf5adSJeff Kirsher 	while (dictp->f.fcode != fcode);
690224cf5adSJeff Kirsher 
691224cf5adSJeff Kirsher 	ent = dictp->codem1 + 1;	/* finally found (prefix,suffix) */
692224cf5adSJeff Kirsher 	continue;
693224cf5adSJeff Kirsher 
694224cf5adSJeff Kirsher nomatch:
695224cf5adSJeff Kirsher 	OUTPUT(ent);		/* output the prefix */
696224cf5adSJeff Kirsher 
697224cf5adSJeff Kirsher 	/* code -> hashtable */
698224cf5adSJeff Kirsher 	if (max_ent < db->maxmaxcode)
699224cf5adSJeff Kirsher 	  {
700224cf5adSJeff Kirsher 	    struct bsd_dict *dictp2;
701224cf5adSJeff Kirsher 	    struct bsd_dict *dictp3;
702224cf5adSJeff Kirsher 	    int    indx;
703224cf5adSJeff Kirsher 
704224cf5adSJeff Kirsher 	    /* expand code size if needed */
705224cf5adSJeff Kirsher 	    if (max_ent >= mxcode)
706224cf5adSJeff Kirsher 	      {
707224cf5adSJeff Kirsher 		db->n_bits = ++n_bits;
708224cf5adSJeff Kirsher 		mxcode     = MAXCODE (n_bits);
709224cf5adSJeff Kirsher 	      }
710224cf5adSJeff Kirsher 
711224cf5adSJeff Kirsher 	    /* Invalidate old hash table entry using
712224cf5adSJeff Kirsher 	     * this code, and then take it over.
713224cf5adSJeff Kirsher 	     */
714224cf5adSJeff Kirsher 
715224cf5adSJeff Kirsher 	    dictp2 = dict_ptr (db, max_ent + 1);
716224cf5adSJeff Kirsher 	    indx   = dictp2->cptr;
717224cf5adSJeff Kirsher 	    dictp3 = dict_ptr (db, indx);
718224cf5adSJeff Kirsher 
719224cf5adSJeff Kirsher 	    if (dictp3->codem1 == max_ent)
720224cf5adSJeff Kirsher 	      {
721224cf5adSJeff Kirsher 		dictp3->codem1 = BADCODEM1;
722224cf5adSJeff Kirsher 	      }
723224cf5adSJeff Kirsher 
724224cf5adSJeff Kirsher 	    dictp2->cptr   = hval;
725224cf5adSJeff Kirsher 	    dictp->codem1  = max_ent;
726224cf5adSJeff Kirsher 	    dictp->f.fcode = fcode;
727224cf5adSJeff Kirsher 	    db->max_ent    = ++max_ent;
728224cf5adSJeff Kirsher 
729224cf5adSJeff Kirsher 	    if (db->lens)
730224cf5adSJeff Kirsher 	      {
731224cf5adSJeff Kirsher 		unsigned short *len1 = lens_ptr (db, max_ent);
732224cf5adSJeff Kirsher 		unsigned short *len2 = lens_ptr (db, ent);
733224cf5adSJeff Kirsher 		*len1 = *len2 + 1;
734224cf5adSJeff Kirsher 	      }
735224cf5adSJeff Kirsher 	  }
736224cf5adSJeff Kirsher 	ent = c;
737224cf5adSJeff Kirsher       }
738224cf5adSJeff Kirsher 
739224cf5adSJeff Kirsher     OUTPUT(ent);		/* output the last code */
740224cf5adSJeff Kirsher 
741224cf5adSJeff Kirsher     db->bytes_out    += olen - PPP_HDRLEN - BSD_OVHD;
742224cf5adSJeff Kirsher     db->uncomp_bytes += isize;
743224cf5adSJeff Kirsher     db->in_count     += isize;
744224cf5adSJeff Kirsher     ++db->uncomp_count;
745224cf5adSJeff Kirsher     ++db->seqno;
746224cf5adSJeff Kirsher 
747224cf5adSJeff Kirsher     if (bitno < 32)
748224cf5adSJeff Kirsher       {
749224cf5adSJeff Kirsher 	++db->bytes_out; /* must be set before calling bsd_check */
750224cf5adSJeff Kirsher       }
751224cf5adSJeff Kirsher 
752224cf5adSJeff Kirsher     /*
753224cf5adSJeff Kirsher      * Generate the clear command if needed
754224cf5adSJeff Kirsher      */
755224cf5adSJeff Kirsher 
756224cf5adSJeff Kirsher     if (bsd_check(db))
757224cf5adSJeff Kirsher       {
758224cf5adSJeff Kirsher 	OUTPUT (CLEAR);
759224cf5adSJeff Kirsher       }
760224cf5adSJeff Kirsher 
761224cf5adSJeff Kirsher     /*
762224cf5adSJeff Kirsher      * Pad dribble bits of last code with ones.
763224cf5adSJeff Kirsher      * Do not emit a completely useless byte of ones.
764224cf5adSJeff Kirsher      */
765224cf5adSJeff Kirsher 
766224cf5adSJeff Kirsher     if (bitno != 32)
767224cf5adSJeff Kirsher       {
768224cf5adSJeff Kirsher 	PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
769224cf5adSJeff Kirsher       }
770224cf5adSJeff Kirsher 
771224cf5adSJeff Kirsher     /*
772224cf5adSJeff Kirsher      * Increase code size if we would have without the packet
773224cf5adSJeff Kirsher      * boundary because the decompressor will do so.
774224cf5adSJeff Kirsher      */
775224cf5adSJeff Kirsher 
776224cf5adSJeff Kirsher     if (max_ent >= mxcode && max_ent < db->maxmaxcode)
777224cf5adSJeff Kirsher       {
778224cf5adSJeff Kirsher 	db->n_bits++;
779224cf5adSJeff Kirsher       }
780224cf5adSJeff Kirsher 
781224cf5adSJeff Kirsher     /* If output length is too large then this is an incomplete frame. */
782224cf5adSJeff Kirsher     if (wptr == NULL)
783224cf5adSJeff Kirsher       {
784224cf5adSJeff Kirsher 	++db->incomp_count;
785224cf5adSJeff Kirsher 	db->incomp_bytes += isize;
786224cf5adSJeff Kirsher 	olen              = 0;
787224cf5adSJeff Kirsher       }
788224cf5adSJeff Kirsher     else /* Count the number of compressed frames */
789224cf5adSJeff Kirsher       {
790224cf5adSJeff Kirsher 	++db->comp_count;
791224cf5adSJeff Kirsher 	db->comp_bytes += olen;
792224cf5adSJeff Kirsher       }
793224cf5adSJeff Kirsher 
794224cf5adSJeff Kirsher     /* Return the resulting output length */
795224cf5adSJeff Kirsher     return olen;
796224cf5adSJeff Kirsher #undef OUTPUT
797224cf5adSJeff Kirsher #undef PUTBYTE
798224cf5adSJeff Kirsher   }
799224cf5adSJeff Kirsher 
800224cf5adSJeff Kirsher /*
801224cf5adSJeff Kirsher  * Update the "BSD Compress" dictionary on the receiver for
802224cf5adSJeff Kirsher  * incompressible data by pretending to compress the incoming data.
803224cf5adSJeff Kirsher  */
804224cf5adSJeff Kirsher 
bsd_incomp(void * state,unsigned char * ibuf,int icnt)805224cf5adSJeff Kirsher static void bsd_incomp (void *state, unsigned char *ibuf, int icnt)
806224cf5adSJeff Kirsher   {
807224cf5adSJeff Kirsher     (void) bsd_compress (state, ibuf, (char *) 0, icnt, 0);
808224cf5adSJeff Kirsher   }
809224cf5adSJeff Kirsher 
810224cf5adSJeff Kirsher /*
811224cf5adSJeff Kirsher  * Decompress "BSD Compress".
812224cf5adSJeff Kirsher  *
813224cf5adSJeff Kirsher  * Because of patent problems, we return DECOMP_ERROR for errors
814224cf5adSJeff Kirsher  * found by inspecting the input data and for system problems, but
815224cf5adSJeff Kirsher  * DECOMP_FATALERROR for any errors which could possibly be said to
816224cf5adSJeff Kirsher  * be being detected "after" decompression.  For DECOMP_ERROR,
817224cf5adSJeff Kirsher  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
818224cf5adSJeff Kirsher  * infringing a patent of Motorola's if we do, so we take CCP down
819224cf5adSJeff Kirsher  * instead.
820224cf5adSJeff Kirsher  *
821224cf5adSJeff Kirsher  * Given that the frame has the correct sequence number and a good FCS,
822224cf5adSJeff Kirsher  * errors such as invalid codes in the input most likely indicate a
823224cf5adSJeff Kirsher  * bug, so we return DECOMP_FATALERROR for them in order to turn off
824224cf5adSJeff Kirsher  * compression, even though they are detected by inspecting the input.
825224cf5adSJeff Kirsher  */
826224cf5adSJeff Kirsher 
bsd_decompress(void * state,unsigned char * ibuf,int isize,unsigned char * obuf,int osize)827224cf5adSJeff Kirsher static int bsd_decompress (void *state, unsigned char *ibuf, int isize,
828224cf5adSJeff Kirsher 			   unsigned char *obuf, int osize)
829224cf5adSJeff Kirsher   {
830224cf5adSJeff Kirsher     struct bsd_db *db;
831224cf5adSJeff Kirsher     unsigned int max_ent;
832224cf5adSJeff Kirsher     unsigned long accm;
833224cf5adSJeff Kirsher     unsigned int bitno;		/* 1st valid bit in accm */
834224cf5adSJeff Kirsher     unsigned int n_bits;
835224cf5adSJeff Kirsher     unsigned int tgtbitno;	/* bitno when we have a code */
836224cf5adSJeff Kirsher     struct bsd_dict *dictp;
837224cf5adSJeff Kirsher     int explen;
838224cf5adSJeff Kirsher     int seq;
839224cf5adSJeff Kirsher     unsigned int incode;
840224cf5adSJeff Kirsher     unsigned int oldcode;
841224cf5adSJeff Kirsher     unsigned int finchar;
842224cf5adSJeff Kirsher     unsigned char *p;
843224cf5adSJeff Kirsher     unsigned char *wptr;
844224cf5adSJeff Kirsher     int adrs;
845224cf5adSJeff Kirsher     int ctrl;
846224cf5adSJeff Kirsher     int ilen;
847224cf5adSJeff Kirsher     int codelen;
848224cf5adSJeff Kirsher     int extra;
849224cf5adSJeff Kirsher 
850224cf5adSJeff Kirsher     db       = (struct bsd_db *) state;
851224cf5adSJeff Kirsher     max_ent  = db->max_ent;
852224cf5adSJeff Kirsher     accm     = 0;
853224cf5adSJeff Kirsher     bitno    = 32;		/* 1st valid bit in accm */
854224cf5adSJeff Kirsher     n_bits   = db->n_bits;
855224cf5adSJeff Kirsher     tgtbitno = 32 - n_bits;	/* bitno when we have a code */
856224cf5adSJeff Kirsher 
857224cf5adSJeff Kirsher     /*
858224cf5adSJeff Kirsher      * Save the address/control from the PPP header
859224cf5adSJeff Kirsher      * and then get the sequence number.
860224cf5adSJeff Kirsher      */
861224cf5adSJeff Kirsher 
862224cf5adSJeff Kirsher     adrs  = PPP_ADDRESS (ibuf);
863224cf5adSJeff Kirsher     ctrl  = PPP_CONTROL (ibuf);
864224cf5adSJeff Kirsher 
865224cf5adSJeff Kirsher     seq   = (ibuf[4] << 8) + ibuf[5];
866224cf5adSJeff Kirsher 
867224cf5adSJeff Kirsher     ibuf += (PPP_HDRLEN + 2);
868224cf5adSJeff Kirsher     ilen  = isize - (PPP_HDRLEN + 2);
869224cf5adSJeff Kirsher 
870224cf5adSJeff Kirsher     /*
871224cf5adSJeff Kirsher      * Check the sequence number and give up if it differs from
872224cf5adSJeff Kirsher      * the value we're expecting.
873224cf5adSJeff Kirsher      */
874224cf5adSJeff Kirsher 
875224cf5adSJeff Kirsher     if (seq != db->seqno)
876224cf5adSJeff Kirsher       {
877224cf5adSJeff Kirsher 	if (db->debug)
878224cf5adSJeff Kirsher 	  {
879224cf5adSJeff Kirsher 	    printk("bsd_decomp%d: bad sequence # %d, expected %d\n",
880224cf5adSJeff Kirsher 		   db->unit, seq, db->seqno - 1);
881224cf5adSJeff Kirsher 	  }
882224cf5adSJeff Kirsher 	return DECOMP_ERROR;
883224cf5adSJeff Kirsher       }
884224cf5adSJeff Kirsher 
885224cf5adSJeff Kirsher     ++db->seqno;
886224cf5adSJeff Kirsher     db->bytes_out += ilen;
887224cf5adSJeff Kirsher 
888224cf5adSJeff Kirsher     /*
889224cf5adSJeff Kirsher      * Fill in the ppp header, but not the last byte of the protocol
890224cf5adSJeff Kirsher      * (that comes from the decompressed data).
891224cf5adSJeff Kirsher      */
892224cf5adSJeff Kirsher 
893224cf5adSJeff Kirsher     wptr    = obuf;
894224cf5adSJeff Kirsher     *wptr++ = adrs;
895224cf5adSJeff Kirsher     *wptr++ = ctrl;
896224cf5adSJeff Kirsher     *wptr++ = 0;
897224cf5adSJeff Kirsher 
898224cf5adSJeff Kirsher     oldcode = CLEAR;
899224cf5adSJeff Kirsher     explen  = 3;
900224cf5adSJeff Kirsher 
901224cf5adSJeff Kirsher     /*
902224cf5adSJeff Kirsher      * Keep the checkpoint correctly so that incompressible packets
903224cf5adSJeff Kirsher      * clear the dictionary at the proper times.
904224cf5adSJeff Kirsher      */
905224cf5adSJeff Kirsher 
906224cf5adSJeff Kirsher     for (;;)
907224cf5adSJeff Kirsher       {
908224cf5adSJeff Kirsher 	if (ilen-- <= 0)
909224cf5adSJeff Kirsher 	  {
910224cf5adSJeff Kirsher 	    db->in_count += (explen - 3); /* don't count the header */
911224cf5adSJeff Kirsher 	    break;
912224cf5adSJeff Kirsher 	  }
913224cf5adSJeff Kirsher 
914224cf5adSJeff Kirsher 	/*
915224cf5adSJeff Kirsher 	 * Accumulate bytes until we have a complete code.
916224cf5adSJeff Kirsher 	 * Then get the next code, relying on the 32-bit,
917224cf5adSJeff Kirsher 	 * unsigned accm to mask the result.
918224cf5adSJeff Kirsher 	 */
919224cf5adSJeff Kirsher 
920224cf5adSJeff Kirsher 	bitno -= 8;
921224cf5adSJeff Kirsher 	accm  |= *ibuf++ << bitno;
922224cf5adSJeff Kirsher 	if (tgtbitno < bitno)
923224cf5adSJeff Kirsher 	  {
924224cf5adSJeff Kirsher 	    continue;
925224cf5adSJeff Kirsher 	  }
926224cf5adSJeff Kirsher 
927224cf5adSJeff Kirsher 	incode = accm >> tgtbitno;
928224cf5adSJeff Kirsher 	accm <<= n_bits;
929224cf5adSJeff Kirsher 	bitno += n_bits;
930224cf5adSJeff Kirsher 
931224cf5adSJeff Kirsher 	/*
932224cf5adSJeff Kirsher 	 * The dictionary must only be cleared at the end of a packet.
933224cf5adSJeff Kirsher 	 */
934224cf5adSJeff Kirsher 
935224cf5adSJeff Kirsher 	if (incode == CLEAR)
936224cf5adSJeff Kirsher 	  {
937224cf5adSJeff Kirsher 	    if (ilen > 0)
938224cf5adSJeff Kirsher 	      {
939224cf5adSJeff Kirsher 		if (db->debug)
940224cf5adSJeff Kirsher 		  {
941224cf5adSJeff Kirsher 		    printk("bsd_decomp%d: bad CLEAR\n", db->unit);
942224cf5adSJeff Kirsher 		  }
943224cf5adSJeff Kirsher 		return DECOMP_FATALERROR;	/* probably a bug */
944224cf5adSJeff Kirsher 	      }
945224cf5adSJeff Kirsher 
946224cf5adSJeff Kirsher 	    bsd_clear(db);
947224cf5adSJeff Kirsher 	    break;
948224cf5adSJeff Kirsher 	  }
949224cf5adSJeff Kirsher 
950224cf5adSJeff Kirsher 	if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
951224cf5adSJeff Kirsher 	    || (incode > max_ent && oldcode == CLEAR))
952224cf5adSJeff Kirsher 	  {
953224cf5adSJeff Kirsher 	    if (db->debug)
954224cf5adSJeff Kirsher 	      {
955224cf5adSJeff Kirsher 		printk("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
956224cf5adSJeff Kirsher 		       db->unit, incode, oldcode);
957224cf5adSJeff Kirsher 		printk("max_ent=0x%x explen=%d seqno=%d\n",
958224cf5adSJeff Kirsher 		       max_ent, explen, db->seqno);
959224cf5adSJeff Kirsher 	      }
960224cf5adSJeff Kirsher 	    return DECOMP_FATALERROR;	/* probably a bug */
961224cf5adSJeff Kirsher 	  }
962224cf5adSJeff Kirsher 
963224cf5adSJeff Kirsher 	/* Special case for KwKwK string. */
964224cf5adSJeff Kirsher 	if (incode > max_ent)
965224cf5adSJeff Kirsher 	  {
966224cf5adSJeff Kirsher 	    finchar = oldcode;
967224cf5adSJeff Kirsher 	    extra   = 1;
968224cf5adSJeff Kirsher 	  }
969224cf5adSJeff Kirsher 	else
970224cf5adSJeff Kirsher 	  {
971224cf5adSJeff Kirsher 	    finchar = incode;
972224cf5adSJeff Kirsher 	    extra   = 0;
973224cf5adSJeff Kirsher 	  }
974224cf5adSJeff Kirsher 
975224cf5adSJeff Kirsher 	codelen = *(lens_ptr (db, finchar));
976224cf5adSJeff Kirsher 	explen += codelen + extra;
977224cf5adSJeff Kirsher 	if (explen > osize)
978224cf5adSJeff Kirsher 	  {
979224cf5adSJeff Kirsher 	    if (db->debug)
980224cf5adSJeff Kirsher 	      {
981224cf5adSJeff Kirsher 		printk("bsd_decomp%d: ran out of mru\n", db->unit);
982224cf5adSJeff Kirsher #ifdef DEBUG
983224cf5adSJeff Kirsher 		printk("  len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
984224cf5adSJeff Kirsher 		       ilen, finchar, codelen, explen);
985224cf5adSJeff Kirsher #endif
986224cf5adSJeff Kirsher 	      }
987224cf5adSJeff Kirsher 	    return DECOMP_FATALERROR;
988224cf5adSJeff Kirsher 	  }
989224cf5adSJeff Kirsher 
990224cf5adSJeff Kirsher 	/*
991224cf5adSJeff Kirsher 	 * Decode this code and install it in the decompressed buffer.
992224cf5adSJeff Kirsher 	 */
993224cf5adSJeff Kirsher 
994224cf5adSJeff Kirsher 	wptr += codelen;
995224cf5adSJeff Kirsher 	p     = wptr;
996224cf5adSJeff Kirsher 	while (finchar > LAST)
997224cf5adSJeff Kirsher 	  {
998224cf5adSJeff Kirsher 	    struct bsd_dict *dictp2 = dict_ptr (db, finchar);
999224cf5adSJeff Kirsher 
1000224cf5adSJeff Kirsher 	    dictp = dict_ptr (db, dictp2->cptr);
1001224cf5adSJeff Kirsher #ifdef DEBUG
1002224cf5adSJeff Kirsher 	    if (--codelen <= 0 || dictp->codem1 != finchar-1)
1003224cf5adSJeff Kirsher 	      {
1004224cf5adSJeff Kirsher 		if (codelen <= 0)
1005224cf5adSJeff Kirsher 		  {
1006224cf5adSJeff Kirsher 		    printk("bsd_decomp%d: fell off end of chain ", db->unit);
1007224cf5adSJeff Kirsher 		    printk("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1008224cf5adSJeff Kirsher 			   incode, finchar, dictp2->cptr, max_ent);
1009224cf5adSJeff Kirsher 		  }
1010224cf5adSJeff Kirsher 		else
1011224cf5adSJeff Kirsher 		  {
1012224cf5adSJeff Kirsher 		    if (dictp->codem1 != finchar-1)
1013224cf5adSJeff Kirsher 		      {
1014224cf5adSJeff Kirsher 			printk("bsd_decomp%d: bad code chain 0x%x "
1015224cf5adSJeff Kirsher 			       "finchar=0x%x ",
1016224cf5adSJeff Kirsher 			       db->unit, incode, finchar);
1017224cf5adSJeff Kirsher 
1018224cf5adSJeff Kirsher 			printk("oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1019224cf5adSJeff Kirsher 			       oldcode, dictp2->cptr, dictp->codem1);
1020224cf5adSJeff Kirsher 		      }
1021224cf5adSJeff Kirsher 		  }
1022224cf5adSJeff Kirsher 		return DECOMP_FATALERROR;
1023224cf5adSJeff Kirsher 	      }
1024224cf5adSJeff Kirsher #endif
1025224cf5adSJeff Kirsher 	    *--p    = dictp->f.hs.suffix;
1026224cf5adSJeff Kirsher 	    finchar = dictp->f.hs.prefix;
1027224cf5adSJeff Kirsher 	  }
1028224cf5adSJeff Kirsher 	*--p = finchar;
1029224cf5adSJeff Kirsher 
1030224cf5adSJeff Kirsher #ifdef DEBUG
1031224cf5adSJeff Kirsher 	if (--codelen != 0)
1032224cf5adSJeff Kirsher 	  {
1033224cf5adSJeff Kirsher 	    printk("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1034224cf5adSJeff Kirsher 		   db->unit, codelen, incode, max_ent);
1035224cf5adSJeff Kirsher 	  }
1036224cf5adSJeff Kirsher #endif
1037224cf5adSJeff Kirsher 
1038224cf5adSJeff Kirsher 	if (extra)		/* the KwKwK case again */
1039224cf5adSJeff Kirsher 	  {
1040224cf5adSJeff Kirsher 	    *wptr++ = finchar;
1041224cf5adSJeff Kirsher 	  }
1042224cf5adSJeff Kirsher 
1043224cf5adSJeff Kirsher 	/*
1044224cf5adSJeff Kirsher 	 * If not first code in a packet, and
1045224cf5adSJeff Kirsher 	 * if not out of code space, then allocate a new code.
1046224cf5adSJeff Kirsher 	 *
1047224cf5adSJeff Kirsher 	 * Keep the hash table correct so it can be used
1048224cf5adSJeff Kirsher 	 * with uncompressed packets.
1049224cf5adSJeff Kirsher 	 */
1050224cf5adSJeff Kirsher 
1051224cf5adSJeff Kirsher 	if (oldcode != CLEAR && max_ent < db->maxmaxcode)
1052224cf5adSJeff Kirsher 	  {
1053224cf5adSJeff Kirsher 	    struct bsd_dict *dictp2, *dictp3;
1054224cf5adSJeff Kirsher 	    unsigned short  *lens1,  *lens2;
1055224cf5adSJeff Kirsher 	    unsigned long fcode;
1056224cf5adSJeff Kirsher 	    int hval, disp, indx;
1057224cf5adSJeff Kirsher 
1058224cf5adSJeff Kirsher 	    fcode = BSD_KEY(oldcode,finchar);
1059224cf5adSJeff Kirsher 	    hval  = BSD_HASH(oldcode,finchar,db->hshift);
1060224cf5adSJeff Kirsher 	    dictp = dict_ptr (db, hval);
1061224cf5adSJeff Kirsher 
1062224cf5adSJeff Kirsher 	    /* look for a free hash table entry */
1063224cf5adSJeff Kirsher 	    if (dictp->codem1 < max_ent)
1064224cf5adSJeff Kirsher 	      {
1065224cf5adSJeff Kirsher 		disp = (hval == 0) ? 1 : hval;
1066224cf5adSJeff Kirsher 		do
1067224cf5adSJeff Kirsher 		  {
1068224cf5adSJeff Kirsher 		    hval += disp;
1069224cf5adSJeff Kirsher 		    if (hval >= db->hsize)
1070224cf5adSJeff Kirsher 		      {
1071224cf5adSJeff Kirsher 			hval -= db->hsize;
1072224cf5adSJeff Kirsher 		      }
1073224cf5adSJeff Kirsher 		    dictp = dict_ptr (db, hval);
1074224cf5adSJeff Kirsher 		  }
1075224cf5adSJeff Kirsher 		while (dictp->codem1 < max_ent);
1076224cf5adSJeff Kirsher 	      }
1077224cf5adSJeff Kirsher 
1078224cf5adSJeff Kirsher 	    /*
1079224cf5adSJeff Kirsher 	     * Invalidate previous hash table entry
1080224cf5adSJeff Kirsher 	     * assigned this code, and then take it over
1081224cf5adSJeff Kirsher 	     */
1082224cf5adSJeff Kirsher 
1083224cf5adSJeff Kirsher 	    dictp2 = dict_ptr (db, max_ent + 1);
1084224cf5adSJeff Kirsher 	    indx   = dictp2->cptr;
1085224cf5adSJeff Kirsher 	    dictp3 = dict_ptr (db, indx);
1086224cf5adSJeff Kirsher 
1087224cf5adSJeff Kirsher 	    if (dictp3->codem1 == max_ent)
1088224cf5adSJeff Kirsher 	      {
1089224cf5adSJeff Kirsher 		dictp3->codem1 = BADCODEM1;
1090224cf5adSJeff Kirsher 	      }
1091224cf5adSJeff Kirsher 
1092224cf5adSJeff Kirsher 	    dictp2->cptr   = hval;
1093224cf5adSJeff Kirsher 	    dictp->codem1  = max_ent;
1094224cf5adSJeff Kirsher 	    dictp->f.fcode = fcode;
1095224cf5adSJeff Kirsher 	    db->max_ent    = ++max_ent;
1096224cf5adSJeff Kirsher 
1097224cf5adSJeff Kirsher 	    /* Update the length of this string. */
1098224cf5adSJeff Kirsher 	    lens1  = lens_ptr (db, max_ent);
1099224cf5adSJeff Kirsher 	    lens2  = lens_ptr (db, oldcode);
1100224cf5adSJeff Kirsher 	    *lens1 = *lens2 + 1;
1101224cf5adSJeff Kirsher 
1102224cf5adSJeff Kirsher 	    /* Expand code size if needed. */
1103224cf5adSJeff Kirsher 	    if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
1104224cf5adSJeff Kirsher 	      {
1105224cf5adSJeff Kirsher 		db->n_bits = ++n_bits;
1106224cf5adSJeff Kirsher 		tgtbitno   = 32-n_bits;
1107224cf5adSJeff Kirsher 	      }
1108224cf5adSJeff Kirsher 	  }
1109224cf5adSJeff Kirsher 	oldcode = incode;
1110224cf5adSJeff Kirsher       }
1111224cf5adSJeff Kirsher 
1112224cf5adSJeff Kirsher     ++db->comp_count;
1113224cf5adSJeff Kirsher     ++db->uncomp_count;
1114224cf5adSJeff Kirsher     db->comp_bytes   += isize - BSD_OVHD - PPP_HDRLEN;
1115224cf5adSJeff Kirsher     db->uncomp_bytes += explen;
1116224cf5adSJeff Kirsher 
1117224cf5adSJeff Kirsher     if (bsd_check(db))
1118224cf5adSJeff Kirsher       {
1119224cf5adSJeff Kirsher 	if (db->debug)
1120224cf5adSJeff Kirsher 	  {
1121224cf5adSJeff Kirsher 	    printk("bsd_decomp%d: peer should have cleared dictionary on %d\n",
1122224cf5adSJeff Kirsher 		   db->unit, db->seqno - 1);
1123224cf5adSJeff Kirsher 	  }
1124224cf5adSJeff Kirsher       }
1125224cf5adSJeff Kirsher     return explen;
1126224cf5adSJeff Kirsher   }
1127224cf5adSJeff Kirsher 
1128224cf5adSJeff Kirsher /*************************************************************
1129224cf5adSJeff Kirsher  * Table of addresses for the BSD compression module
1130224cf5adSJeff Kirsher  *************************************************************/
1131224cf5adSJeff Kirsher 
1132224cf5adSJeff Kirsher static struct compressor ppp_bsd_compress = {
1133224cf5adSJeff Kirsher 	.compress_proto =	CI_BSD_COMPRESS,
1134224cf5adSJeff Kirsher 	.comp_alloc =		bsd_comp_alloc,
1135224cf5adSJeff Kirsher 	.comp_free =		bsd_free,
1136224cf5adSJeff Kirsher 	.comp_init =		bsd_comp_init,
1137224cf5adSJeff Kirsher 	.comp_reset =		bsd_reset,
1138224cf5adSJeff Kirsher 	.compress =		bsd_compress,
1139224cf5adSJeff Kirsher 	.comp_stat =		bsd_comp_stats,
1140224cf5adSJeff Kirsher 	.decomp_alloc =		bsd_decomp_alloc,
1141224cf5adSJeff Kirsher 	.decomp_free =		bsd_free,
1142224cf5adSJeff Kirsher 	.decomp_init =		bsd_decomp_init,
1143224cf5adSJeff Kirsher 	.decomp_reset =		bsd_reset,
1144224cf5adSJeff Kirsher 	.decompress =		bsd_decompress,
1145224cf5adSJeff Kirsher 	.incomp =		bsd_incomp,
1146224cf5adSJeff Kirsher 	.decomp_stat =		bsd_comp_stats,
1147224cf5adSJeff Kirsher 	.owner =		THIS_MODULE
1148224cf5adSJeff Kirsher };
1149224cf5adSJeff Kirsher 
1150224cf5adSJeff Kirsher /*************************************************************
1151224cf5adSJeff Kirsher  * Module support routines
1152224cf5adSJeff Kirsher  *************************************************************/
1153224cf5adSJeff Kirsher 
bsdcomp_init(void)1154224cf5adSJeff Kirsher static int __init bsdcomp_init(void)
1155224cf5adSJeff Kirsher {
1156224cf5adSJeff Kirsher 	int answer = ppp_register_compressor(&ppp_bsd_compress);
1157224cf5adSJeff Kirsher 	if (answer == 0)
1158224cf5adSJeff Kirsher 		printk(KERN_INFO "PPP BSD Compression module registered\n");
1159224cf5adSJeff Kirsher 	return answer;
1160224cf5adSJeff Kirsher }
1161224cf5adSJeff Kirsher 
bsdcomp_cleanup(void)1162224cf5adSJeff Kirsher static void __exit bsdcomp_cleanup(void)
1163224cf5adSJeff Kirsher {
1164224cf5adSJeff Kirsher 	ppp_unregister_compressor(&ppp_bsd_compress);
1165224cf5adSJeff Kirsher }
1166224cf5adSJeff Kirsher 
1167224cf5adSJeff Kirsher module_init(bsdcomp_init);
1168224cf5adSJeff Kirsher module_exit(bsdcomp_cleanup);
1169224cf5adSJeff Kirsher MODULE_LICENSE("Dual BSD/GPL");
1170224cf5adSJeff Kirsher MODULE_ALIAS("ppp-compress-" __stringify(CI_BSD_COMPRESS));
1171