1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Update: The Berkeley copyright was changed, and the change
3*4882a593Smuzhiyun * is retroactive to all "true" BSD software (ie everything
4*4882a593Smuzhiyun * from UCB as opposed to other peoples code that just carried
5*4882a593Smuzhiyun * the same license). The new copyright doesn't clash with the
6*4882a593Smuzhiyun * GPL, so the module-only restriction has been removed..
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /* Because this code is derived from the 4.3BSD compress source:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Copyright (c) 1985, 1986 The Regents of the University of California.
12*4882a593Smuzhiyun * All rights reserved.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * This code is derived from software contributed to Berkeley by
15*4882a593Smuzhiyun * James A. Woods, derived from original work by Spencer Thomas
16*4882a593Smuzhiyun * and Joseph Orost.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
19*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
20*4882a593Smuzhiyun * are met:
21*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright
22*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
23*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright
24*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
25*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
26*4882a593Smuzhiyun * 3. All advertising materials mentioning features or use of this software
27*4882a593Smuzhiyun * must display the following acknowledgement:
28*4882a593Smuzhiyun * This product includes software developed by the University of
29*4882a593Smuzhiyun * California, Berkeley and its contributors.
30*4882a593Smuzhiyun * 4. Neither the name of the University nor the names of its contributors
31*4882a593Smuzhiyun * may be used to endorse or promote products derived from this software
32*4882a593Smuzhiyun * without specific prior written permission.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35*4882a593Smuzhiyun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36*4882a593Smuzhiyun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37*4882a593Smuzhiyun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38*4882a593Smuzhiyun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39*4882a593Smuzhiyun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40*4882a593Smuzhiyun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41*4882a593Smuzhiyun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42*4882a593Smuzhiyun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43*4882a593Smuzhiyun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44*4882a593Smuzhiyun * SUCH DAMAGE.
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * This version is for use with contiguous buffers on Linux-derived systems.
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * ==FILEVERSION 20000226==
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * NOTE TO MAINTAINERS:
53*4882a593Smuzhiyun * If you modify this file at all, please set the number above to the
54*4882a593Smuzhiyun * date of the modification as YYMMDD (year month day).
55*4882a593Smuzhiyun * bsd_comp.c is shipped with a PPP distribution as well as with
56*4882a593Smuzhiyun * the kernel; if everyone increases the FILEVERSION number above,
57*4882a593Smuzhiyun * then scripts can do the right thing when deciding whether to
58*4882a593Smuzhiyun * install a new bsd_comp.c file. Don't change the format of that
59*4882a593Smuzhiyun * line otherwise, so the installation script can recognize it.
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * From: bsd_comp.c,v 1.3 1994/12/08 01:59:58 paulus Exp
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #include <linux/module.h>
65*4882a593Smuzhiyun #include <linux/init.h>
66*4882a593Smuzhiyun #include <linux/slab.h>
67*4882a593Smuzhiyun #include <linux/vmalloc.h>
68*4882a593Smuzhiyun #include <linux/string.h>
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #include <linux/ppp_defs.h>
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun #undef PACKETPTR
73*4882a593Smuzhiyun #define PACKETPTR 1
74*4882a593Smuzhiyun #include <linux/ppp-comp.h>
75*4882a593Smuzhiyun #undef PACKETPTR
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #include <asm/byteorder.h>
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * PPP "BSD compress" compression
81*4882a593Smuzhiyun * The differences between this compression and the classic BSD LZW
82*4882a593Smuzhiyun * source are obvious from the requirement that the classic code worked
83*4882a593Smuzhiyun * with files while this handles arbitrarily long streams that
84*4882a593Smuzhiyun * are broken into packets. They are:
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * When the code size expands, a block of junk is not emitted by
87*4882a593Smuzhiyun * the compressor and not expected by the decompressor.
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * New codes are not necessarily assigned every time an old
90*4882a593Smuzhiyun * code is output by the compressor. This is because a packet
91*4882a593Smuzhiyun * end forces a code to be emitted, but does not imply that a
92*4882a593Smuzhiyun * new sequence has been seen.
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * The compression ratio is checked at the first end of a packet
95*4882a593Smuzhiyun * after the appropriate gap. Besides simplifying and speeding
96*4882a593Smuzhiyun * things up, this makes it more likely that the transmitter
97*4882a593Smuzhiyun * and receiver will agree when the dictionary is cleared when
98*4882a593Smuzhiyun * compression is not going well.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * Macros to extract protocol version and number of bits
103*4882a593Smuzhiyun * from the third byte of the BSD Compress CCP configuration option.
104*4882a593Smuzhiyun */
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun #define BSD_VERSION(x) ((x) >> 5)
107*4882a593Smuzhiyun #define BSD_NBITS(x) ((x) & 0x1F)
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun #define BSD_CURRENT_VERSION 1
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * A dictionary for doing BSD compress.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun struct bsd_dict {
116*4882a593Smuzhiyun union { /* hash value */
117*4882a593Smuzhiyun unsigned long fcode;
118*4882a593Smuzhiyun struct {
119*4882a593Smuzhiyun #if defined(__LITTLE_ENDIAN) /* Little endian order */
120*4882a593Smuzhiyun unsigned short prefix; /* preceding code */
121*4882a593Smuzhiyun unsigned char suffix; /* last character of new code */
122*4882a593Smuzhiyun unsigned char pad;
123*4882a593Smuzhiyun #elif defined(__BIG_ENDIAN) /* Big endian order */
124*4882a593Smuzhiyun unsigned char pad;
125*4882a593Smuzhiyun unsigned char suffix; /* last character of new code */
126*4882a593Smuzhiyun unsigned short prefix; /* preceding code */
127*4882a593Smuzhiyun #else
128*4882a593Smuzhiyun #error Endianness not defined...
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun } hs;
131*4882a593Smuzhiyun } f;
132*4882a593Smuzhiyun unsigned short codem1; /* output of hash table -1 */
133*4882a593Smuzhiyun unsigned short cptr; /* map code to hash table entry */
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun struct bsd_db {
137*4882a593Smuzhiyun int totlen; /* length of this structure */
138*4882a593Smuzhiyun unsigned int hsize; /* size of the hash table */
139*4882a593Smuzhiyun unsigned char hshift; /* used in hash function */
140*4882a593Smuzhiyun unsigned char n_bits; /* current bits/code */
141*4882a593Smuzhiyun unsigned char maxbits; /* maximum bits/code */
142*4882a593Smuzhiyun unsigned char debug; /* non-zero if debug desired */
143*4882a593Smuzhiyun unsigned char unit; /* ppp unit number */
144*4882a593Smuzhiyun unsigned short seqno; /* sequence # of next packet */
145*4882a593Smuzhiyun unsigned int mru; /* size of receive (decompress) bufr */
146*4882a593Smuzhiyun unsigned int maxmaxcode; /* largest valid code */
147*4882a593Smuzhiyun unsigned int max_ent; /* largest code in use */
148*4882a593Smuzhiyun unsigned int in_count; /* uncompressed bytes, aged */
149*4882a593Smuzhiyun unsigned int bytes_out; /* compressed bytes, aged */
150*4882a593Smuzhiyun unsigned int ratio; /* recent compression ratio */
151*4882a593Smuzhiyun unsigned int checkpoint; /* when to next check the ratio */
152*4882a593Smuzhiyun unsigned int clear_count; /* times dictionary cleared */
153*4882a593Smuzhiyun unsigned int incomp_count; /* incompressible packets */
154*4882a593Smuzhiyun unsigned int incomp_bytes; /* incompressible bytes */
155*4882a593Smuzhiyun unsigned int uncomp_count; /* uncompressed packets */
156*4882a593Smuzhiyun unsigned int uncomp_bytes; /* uncompressed bytes */
157*4882a593Smuzhiyun unsigned int comp_count; /* compressed packets */
158*4882a593Smuzhiyun unsigned int comp_bytes; /* compressed bytes */
159*4882a593Smuzhiyun unsigned short *lens; /* array of lengths of codes */
160*4882a593Smuzhiyun struct bsd_dict *dict; /* dictionary */
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun #define BSD_OVHD 2 /* BSD compress overhead/packet */
164*4882a593Smuzhiyun #define MIN_BSD_BITS 9
165*4882a593Smuzhiyun #define BSD_INIT_BITS MIN_BSD_BITS
166*4882a593Smuzhiyun #define MAX_BSD_BITS 15
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun static void bsd_free (void *state);
169*4882a593Smuzhiyun static void *bsd_alloc(unsigned char *options, int opt_len, int decomp);
170*4882a593Smuzhiyun static void *bsd_comp_alloc (unsigned char *options, int opt_len);
171*4882a593Smuzhiyun static void *bsd_decomp_alloc (unsigned char *options, int opt_len);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun static int bsd_init (void *db, unsigned char *options,
174*4882a593Smuzhiyun int opt_len, int unit, int debug, int decomp);
175*4882a593Smuzhiyun static int bsd_comp_init (void *state, unsigned char *options,
176*4882a593Smuzhiyun int opt_len, int unit, int opthdr, int debug);
177*4882a593Smuzhiyun static int bsd_decomp_init (void *state, unsigned char *options,
178*4882a593Smuzhiyun int opt_len, int unit, int opthdr, int mru,
179*4882a593Smuzhiyun int debug);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun static void bsd_reset (void *state);
182*4882a593Smuzhiyun static void bsd_comp_stats (void *state, struct compstat *stats);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun static int bsd_compress (void *state, unsigned char *rptr,
185*4882a593Smuzhiyun unsigned char *obuf, int isize, int osize);
186*4882a593Smuzhiyun static void bsd_incomp (void *state, unsigned char *ibuf, int icnt);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun static int bsd_decompress (void *state, unsigned char *ibuf, int isize,
189*4882a593Smuzhiyun unsigned char *obuf, int osize);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* These are in ppp_generic.c */
192*4882a593Smuzhiyun extern int ppp_register_compressor (struct compressor *cp);
193*4882a593Smuzhiyun extern void ppp_unregister_compressor (struct compressor *cp);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * the next two codes should not be changed lightly, as they must not
197*4882a593Smuzhiyun * lie within the contiguous general code space.
198*4882a593Smuzhiyun */
199*4882a593Smuzhiyun #define CLEAR 256 /* table clear output code */
200*4882a593Smuzhiyun #define FIRST 257 /* first free entry */
201*4882a593Smuzhiyun #define LAST 255
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun #define MAXCODE(b) ((1 << (b)) - 1)
204*4882a593Smuzhiyun #define BADCODEM1 MAXCODE(MAX_BSD_BITS)
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun #define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
207*4882a593Smuzhiyun ^ (unsigned long)(prefix))
208*4882a593Smuzhiyun #define BSD_KEY(prefix,suffix) ((((unsigned long)(suffix)) << 16) \
209*4882a593Smuzhiyun + (unsigned long)(prefix))
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun #define CHECK_GAP 10000 /* Ratio check interval */
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun #define RATIO_SCALE_LOG 8
214*4882a593Smuzhiyun #define RATIO_SCALE (1<<RATIO_SCALE_LOG)
215*4882a593Smuzhiyun #define RATIO_MAX (0x7fffffff>>RATIO_SCALE_LOG)
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun * clear the dictionary
219*4882a593Smuzhiyun */
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun static void
bsd_clear(struct bsd_db * db)222*4882a593Smuzhiyun bsd_clear(struct bsd_db *db)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun db->clear_count++;
225*4882a593Smuzhiyun db->max_ent = FIRST-1;
226*4882a593Smuzhiyun db->n_bits = BSD_INIT_BITS;
227*4882a593Smuzhiyun db->bytes_out = 0;
228*4882a593Smuzhiyun db->in_count = 0;
229*4882a593Smuzhiyun db->ratio = 0;
230*4882a593Smuzhiyun db->checkpoint = CHECK_GAP;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /*
234*4882a593Smuzhiyun * If the dictionary is full, then see if it is time to reset it.
235*4882a593Smuzhiyun *
236*4882a593Smuzhiyun * Compute the compression ratio using fixed-point arithmetic
237*4882a593Smuzhiyun * with 8 fractional bits.
238*4882a593Smuzhiyun *
239*4882a593Smuzhiyun * Since we have an infinite stream instead of a single file,
240*4882a593Smuzhiyun * watch only the local compression ratio.
241*4882a593Smuzhiyun *
242*4882a593Smuzhiyun * Since both peers must reset the dictionary at the same time even in
243*4882a593Smuzhiyun * the absence of CLEAR codes (while packets are incompressible), they
244*4882a593Smuzhiyun * must compute the same ratio.
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun
bsd_check(struct bsd_db * db)247*4882a593Smuzhiyun static int bsd_check (struct bsd_db *db) /* 1=output CLEAR */
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun unsigned int new_ratio;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (db->in_count >= db->checkpoint)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun /* age the ratio by limiting the size of the counts */
254*4882a593Smuzhiyun if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun db->in_count -= (db->in_count >> 2);
257*4882a593Smuzhiyun db->bytes_out -= (db->bytes_out >> 2);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun db->checkpoint = db->in_count + CHECK_GAP;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (db->max_ent >= db->maxmaxcode)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun /* Reset the dictionary only if the ratio is worse,
265*4882a593Smuzhiyun * or if it looks as if it has been poisoned
266*4882a593Smuzhiyun * by incompressible data.
267*4882a593Smuzhiyun *
268*4882a593Smuzhiyun * This does not overflow, because
269*4882a593Smuzhiyun * db->in_count <= RATIO_MAX.
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun new_ratio = db->in_count << RATIO_SCALE_LOG;
273*4882a593Smuzhiyun if (db->bytes_out != 0)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun new_ratio /= db->bytes_out;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun bsd_clear (db);
281*4882a593Smuzhiyun return 1;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun db->ratio = new_ratio;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun return 0;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /*
290*4882a593Smuzhiyun * Return statistics.
291*4882a593Smuzhiyun */
292*4882a593Smuzhiyun
bsd_comp_stats(void * state,struct compstat * stats)293*4882a593Smuzhiyun static void bsd_comp_stats (void *state, struct compstat *stats)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun struct bsd_db *db = (struct bsd_db *) state;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun stats->unc_bytes = db->uncomp_bytes;
298*4882a593Smuzhiyun stats->unc_packets = db->uncomp_count;
299*4882a593Smuzhiyun stats->comp_bytes = db->comp_bytes;
300*4882a593Smuzhiyun stats->comp_packets = db->comp_count;
301*4882a593Smuzhiyun stats->inc_bytes = db->incomp_bytes;
302*4882a593Smuzhiyun stats->inc_packets = db->incomp_count;
303*4882a593Smuzhiyun stats->in_count = db->in_count;
304*4882a593Smuzhiyun stats->bytes_out = db->bytes_out;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun /*
308*4882a593Smuzhiyun * Reset state, as on a CCP ResetReq.
309*4882a593Smuzhiyun */
310*4882a593Smuzhiyun
bsd_reset(void * state)311*4882a593Smuzhiyun static void bsd_reset (void *state)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun struct bsd_db *db = (struct bsd_db *) state;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun bsd_clear(db);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun db->seqno = 0;
318*4882a593Smuzhiyun db->clear_count = 0;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun * Release the compression structure
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun
bsd_free(void * state)325*4882a593Smuzhiyun static void bsd_free (void *state)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct bsd_db *db = state;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (!db)
330*4882a593Smuzhiyun return;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /*
333*4882a593Smuzhiyun * Release the dictionary
334*4882a593Smuzhiyun */
335*4882a593Smuzhiyun vfree(db->dict);
336*4882a593Smuzhiyun db->dict = NULL;
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun * Release the string buffer
339*4882a593Smuzhiyun */
340*4882a593Smuzhiyun vfree(db->lens);
341*4882a593Smuzhiyun db->lens = NULL;
342*4882a593Smuzhiyun /*
343*4882a593Smuzhiyun * Finally release the structure itself.
344*4882a593Smuzhiyun */
345*4882a593Smuzhiyun kfree(db);
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /*
349*4882a593Smuzhiyun * Allocate space for a (de) compressor.
350*4882a593Smuzhiyun */
351*4882a593Smuzhiyun
bsd_alloc(unsigned char * options,int opt_len,int decomp)352*4882a593Smuzhiyun static void *bsd_alloc (unsigned char *options, int opt_len, int decomp)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun int bits;
355*4882a593Smuzhiyun unsigned int hsize, hshift, maxmaxcode;
356*4882a593Smuzhiyun struct bsd_db *db;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun if (opt_len != 3 || options[0] != CI_BSD_COMPRESS || options[1] != 3
359*4882a593Smuzhiyun || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun return NULL;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun bits = BSD_NBITS(options[2]);
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun switch (bits)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun case 9: /* needs 82152 for both directions */
369*4882a593Smuzhiyun case 10: /* needs 84144 */
370*4882a593Smuzhiyun case 11: /* needs 88240 */
371*4882a593Smuzhiyun case 12: /* needs 96432 */
372*4882a593Smuzhiyun hsize = 5003;
373*4882a593Smuzhiyun hshift = 4;
374*4882a593Smuzhiyun break;
375*4882a593Smuzhiyun case 13: /* needs 176784 */
376*4882a593Smuzhiyun hsize = 9001;
377*4882a593Smuzhiyun hshift = 5;
378*4882a593Smuzhiyun break;
379*4882a593Smuzhiyun case 14: /* needs 353744 */
380*4882a593Smuzhiyun hsize = 18013;
381*4882a593Smuzhiyun hshift = 6;
382*4882a593Smuzhiyun break;
383*4882a593Smuzhiyun case 15: /* needs 691440 */
384*4882a593Smuzhiyun hsize = 35023;
385*4882a593Smuzhiyun hshift = 7;
386*4882a593Smuzhiyun break;
387*4882a593Smuzhiyun case 16: /* needs 1366160--far too much, */
388*4882a593Smuzhiyun /* hsize = 69001; */ /* and 69001 is too big for cptr */
389*4882a593Smuzhiyun /* hshift = 8; */ /* in struct bsd_db */
390*4882a593Smuzhiyun /* break; */
391*4882a593Smuzhiyun default:
392*4882a593Smuzhiyun return NULL;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun * Allocate the main control structure for this instance.
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun maxmaxcode = MAXCODE(bits);
398*4882a593Smuzhiyun db = kzalloc(sizeof (struct bsd_db),
399*4882a593Smuzhiyun GFP_KERNEL);
400*4882a593Smuzhiyun if (!db)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun return NULL;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /*
406*4882a593Smuzhiyun * Allocate space for the dictionary. This may be more than one page in
407*4882a593Smuzhiyun * length.
408*4882a593Smuzhiyun */
409*4882a593Smuzhiyun db->dict = vmalloc(array_size(hsize, sizeof(struct bsd_dict)));
410*4882a593Smuzhiyun if (!db->dict)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun bsd_free (db);
413*4882a593Smuzhiyun return NULL;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun /*
417*4882a593Smuzhiyun * If this is the compression buffer then there is no length data.
418*4882a593Smuzhiyun */
419*4882a593Smuzhiyun if (!decomp)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun db->lens = NULL;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun /*
424*4882a593Smuzhiyun * For decompression, the length information is needed as well.
425*4882a593Smuzhiyun */
426*4882a593Smuzhiyun else
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun db->lens = vmalloc(array_size(sizeof(db->lens[0]), (maxmaxcode + 1)));
429*4882a593Smuzhiyun if (!db->lens)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun bsd_free (db);
432*4882a593Smuzhiyun return NULL;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun /*
436*4882a593Smuzhiyun * Initialize the data information for the compression code
437*4882a593Smuzhiyun */
438*4882a593Smuzhiyun db->totlen = sizeof (struct bsd_db) +
439*4882a593Smuzhiyun (sizeof (struct bsd_dict) * hsize);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun db->hsize = hsize;
442*4882a593Smuzhiyun db->hshift = hshift;
443*4882a593Smuzhiyun db->maxmaxcode = maxmaxcode;
444*4882a593Smuzhiyun db->maxbits = bits;
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun return (void *) db;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
bsd_comp_alloc(unsigned char * options,int opt_len)449*4882a593Smuzhiyun static void *bsd_comp_alloc (unsigned char *options, int opt_len)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun return bsd_alloc (options, opt_len, 0);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
bsd_decomp_alloc(unsigned char * options,int opt_len)454*4882a593Smuzhiyun static void *bsd_decomp_alloc (unsigned char *options, int opt_len)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun return bsd_alloc (options, opt_len, 1);
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun /*
460*4882a593Smuzhiyun * Initialize the database.
461*4882a593Smuzhiyun */
462*4882a593Smuzhiyun
bsd_init(void * state,unsigned char * options,int opt_len,int unit,int debug,int decomp)463*4882a593Smuzhiyun static int bsd_init (void *state, unsigned char *options,
464*4882a593Smuzhiyun int opt_len, int unit, int debug, int decomp)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun struct bsd_db *db = state;
467*4882a593Smuzhiyun int indx;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun if ((opt_len != 3) || (options[0] != CI_BSD_COMPRESS) || (options[1] != 3)
470*4882a593Smuzhiyun || (BSD_VERSION(options[2]) != BSD_CURRENT_VERSION)
471*4882a593Smuzhiyun || (BSD_NBITS(options[2]) != db->maxbits)
472*4882a593Smuzhiyun || (decomp && db->lens == NULL))
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun return 0;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun if (decomp)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun indx = LAST;
480*4882a593Smuzhiyun do
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun db->lens[indx] = 1;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun while (indx-- > 0);
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun indx = db->hsize;
488*4882a593Smuzhiyun while (indx-- != 0)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun db->dict[indx].codem1 = BADCODEM1;
491*4882a593Smuzhiyun db->dict[indx].cptr = 0;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun db->unit = unit;
495*4882a593Smuzhiyun db->mru = 0;
496*4882a593Smuzhiyun #ifndef DEBUG
497*4882a593Smuzhiyun if (debug)
498*4882a593Smuzhiyun #endif
499*4882a593Smuzhiyun db->debug = 1;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun bsd_reset(db);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun return 1;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun
bsd_comp_init(void * state,unsigned char * options,int opt_len,int unit,int opthdr,int debug)506*4882a593Smuzhiyun static int bsd_comp_init (void *state, unsigned char *options,
507*4882a593Smuzhiyun int opt_len, int unit, int opthdr, int debug)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun return bsd_init (state, options, opt_len, unit, debug, 0);
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
bsd_decomp_init(void * state,unsigned char * options,int opt_len,int unit,int opthdr,int mru,int debug)512*4882a593Smuzhiyun static int bsd_decomp_init (void *state, unsigned char *options,
513*4882a593Smuzhiyun int opt_len, int unit, int opthdr, int mru,
514*4882a593Smuzhiyun int debug)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun return bsd_init (state, options, opt_len, unit, debug, 1);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun * Obtain pointers to the various structures in the compression tables
521*4882a593Smuzhiyun */
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun #define dict_ptrx(p,idx) &(p->dict[idx])
524*4882a593Smuzhiyun #define lens_ptrx(p,idx) &(p->lens[idx])
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun #ifdef DEBUG
lens_ptr(struct bsd_db * db,int idx)527*4882a593Smuzhiyun static unsigned short *lens_ptr(struct bsd_db *db, int idx)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun if ((unsigned int) idx > (unsigned int) db->maxmaxcode)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun printk ("<9>ppp: lens_ptr(%d) > max\n", idx);
532*4882a593Smuzhiyun idx = 0;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun return lens_ptrx (db, idx);
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
dict_ptr(struct bsd_db * db,int idx)537*4882a593Smuzhiyun static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun if ((unsigned int) idx >= (unsigned int) db->hsize)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun printk ("<9>ppp: dict_ptr(%d) > max\n", idx);
542*4882a593Smuzhiyun idx = 0;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun return dict_ptrx (db, idx);
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun #else
548*4882a593Smuzhiyun #define lens_ptr(db,idx) lens_ptrx(db,idx)
549*4882a593Smuzhiyun #define dict_ptr(db,idx) dict_ptrx(db,idx)
550*4882a593Smuzhiyun #endif
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /*
553*4882a593Smuzhiyun * compress a packet
554*4882a593Smuzhiyun *
555*4882a593Smuzhiyun * The result of this function is the size of the compressed
556*4882a593Smuzhiyun * packet. A zero is returned if the packet was not compressed
557*4882a593Smuzhiyun * for some reason, such as the size being larger than uncompressed.
558*4882a593Smuzhiyun *
559*4882a593Smuzhiyun * One change from the BSD compress command is that when the
560*4882a593Smuzhiyun * code size expands, we do not output a bunch of padding.
561*4882a593Smuzhiyun */
562*4882a593Smuzhiyun
bsd_compress(void * state,unsigned char * rptr,unsigned char * obuf,int isize,int osize)563*4882a593Smuzhiyun static int bsd_compress (void *state, unsigned char *rptr, unsigned char *obuf,
564*4882a593Smuzhiyun int isize, int osize)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun struct bsd_db *db;
567*4882a593Smuzhiyun int hshift;
568*4882a593Smuzhiyun unsigned int max_ent;
569*4882a593Smuzhiyun unsigned int n_bits;
570*4882a593Smuzhiyun unsigned int bitno;
571*4882a593Smuzhiyun unsigned long accm;
572*4882a593Smuzhiyun int ent;
573*4882a593Smuzhiyun unsigned long fcode;
574*4882a593Smuzhiyun struct bsd_dict *dictp;
575*4882a593Smuzhiyun unsigned char c;
576*4882a593Smuzhiyun int hval;
577*4882a593Smuzhiyun int disp;
578*4882a593Smuzhiyun int ilen;
579*4882a593Smuzhiyun int mxcode;
580*4882a593Smuzhiyun unsigned char *wptr;
581*4882a593Smuzhiyun int olen;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun #define PUTBYTE(v) \
584*4882a593Smuzhiyun { \
585*4882a593Smuzhiyun ++olen; \
586*4882a593Smuzhiyun if (wptr) \
587*4882a593Smuzhiyun { \
588*4882a593Smuzhiyun *wptr++ = (unsigned char) (v); \
589*4882a593Smuzhiyun if (olen >= osize) \
590*4882a593Smuzhiyun { \
591*4882a593Smuzhiyun wptr = NULL; \
592*4882a593Smuzhiyun } \
593*4882a593Smuzhiyun } \
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun #define OUTPUT(ent) \
597*4882a593Smuzhiyun { \
598*4882a593Smuzhiyun bitno -= n_bits; \
599*4882a593Smuzhiyun accm |= ((ent) << bitno); \
600*4882a593Smuzhiyun do \
601*4882a593Smuzhiyun { \
602*4882a593Smuzhiyun PUTBYTE(accm >> 24); \
603*4882a593Smuzhiyun accm <<= 8; \
604*4882a593Smuzhiyun bitno += 8; \
605*4882a593Smuzhiyun } \
606*4882a593Smuzhiyun while (bitno <= 24); \
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /*
610*4882a593Smuzhiyun * If the protocol is not in the range we're interested in,
611*4882a593Smuzhiyun * just return without compressing the packet. If it is,
612*4882a593Smuzhiyun * the protocol becomes the first byte to compress.
613*4882a593Smuzhiyun */
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun ent = PPP_PROTOCOL(rptr);
616*4882a593Smuzhiyun if (ent < 0x21 || ent > 0xf9)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun return 0;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun db = (struct bsd_db *) state;
622*4882a593Smuzhiyun hshift = db->hshift;
623*4882a593Smuzhiyun max_ent = db->max_ent;
624*4882a593Smuzhiyun n_bits = db->n_bits;
625*4882a593Smuzhiyun bitno = 32;
626*4882a593Smuzhiyun accm = 0;
627*4882a593Smuzhiyun mxcode = MAXCODE (n_bits);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun /* Initialize the output pointers */
630*4882a593Smuzhiyun wptr = obuf;
631*4882a593Smuzhiyun olen = PPP_HDRLEN + BSD_OVHD;
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun if (osize > isize)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun osize = isize;
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /* This is the PPP header information */
639*4882a593Smuzhiyun if (wptr)
640*4882a593Smuzhiyun {
641*4882a593Smuzhiyun *wptr++ = PPP_ADDRESS(rptr);
642*4882a593Smuzhiyun *wptr++ = PPP_CONTROL(rptr);
643*4882a593Smuzhiyun *wptr++ = 0;
644*4882a593Smuzhiyun *wptr++ = PPP_COMP;
645*4882a593Smuzhiyun *wptr++ = db->seqno >> 8;
646*4882a593Smuzhiyun *wptr++ = db->seqno;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* Skip the input header */
650*4882a593Smuzhiyun rptr += PPP_HDRLEN;
651*4882a593Smuzhiyun isize -= PPP_HDRLEN;
652*4882a593Smuzhiyun ilen = ++isize; /* Low byte of protocol is counted as input */
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun while (--ilen > 0)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun c = *rptr++;
657*4882a593Smuzhiyun fcode = BSD_KEY (ent, c);
658*4882a593Smuzhiyun hval = BSD_HASH (ent, c, hshift);
659*4882a593Smuzhiyun dictp = dict_ptr (db, hval);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /* Validate and then check the entry. */
662*4882a593Smuzhiyun if (dictp->codem1 >= max_ent)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun goto nomatch;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun if (dictp->f.fcode == fcode)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun ent = dictp->codem1 + 1;
670*4882a593Smuzhiyun continue; /* found (prefix,suffix) */
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun /* continue probing until a match or invalid entry */
674*4882a593Smuzhiyun disp = (hval == 0) ? 1 : hval;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun do
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun hval += disp;
679*4882a593Smuzhiyun if (hval >= db->hsize)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun hval -= db->hsize;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun dictp = dict_ptr (db, hval);
684*4882a593Smuzhiyun if (dictp->codem1 >= max_ent)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun goto nomatch;
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun while (dictp->f.fcode != fcode);
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */
692*4882a593Smuzhiyun continue;
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun nomatch:
695*4882a593Smuzhiyun OUTPUT(ent); /* output the prefix */
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /* code -> hashtable */
698*4882a593Smuzhiyun if (max_ent < db->maxmaxcode)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun struct bsd_dict *dictp2;
701*4882a593Smuzhiyun struct bsd_dict *dictp3;
702*4882a593Smuzhiyun int indx;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun /* expand code size if needed */
705*4882a593Smuzhiyun if (max_ent >= mxcode)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun db->n_bits = ++n_bits;
708*4882a593Smuzhiyun mxcode = MAXCODE (n_bits);
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun /* Invalidate old hash table entry using
712*4882a593Smuzhiyun * this code, and then take it over.
713*4882a593Smuzhiyun */
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun dictp2 = dict_ptr (db, max_ent + 1);
716*4882a593Smuzhiyun indx = dictp2->cptr;
717*4882a593Smuzhiyun dictp3 = dict_ptr (db, indx);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun if (dictp3->codem1 == max_ent)
720*4882a593Smuzhiyun {
721*4882a593Smuzhiyun dictp3->codem1 = BADCODEM1;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun dictp2->cptr = hval;
725*4882a593Smuzhiyun dictp->codem1 = max_ent;
726*4882a593Smuzhiyun dictp->f.fcode = fcode;
727*4882a593Smuzhiyun db->max_ent = ++max_ent;
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun if (db->lens)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun unsigned short *len1 = lens_ptr (db, max_ent);
732*4882a593Smuzhiyun unsigned short *len2 = lens_ptr (db, ent);
733*4882a593Smuzhiyun *len1 = *len2 + 1;
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun ent = c;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun OUTPUT(ent); /* output the last code */
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun db->bytes_out += olen - PPP_HDRLEN - BSD_OVHD;
742*4882a593Smuzhiyun db->uncomp_bytes += isize;
743*4882a593Smuzhiyun db->in_count += isize;
744*4882a593Smuzhiyun ++db->uncomp_count;
745*4882a593Smuzhiyun ++db->seqno;
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun if (bitno < 32)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun ++db->bytes_out; /* must be set before calling bsd_check */
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun /*
753*4882a593Smuzhiyun * Generate the clear command if needed
754*4882a593Smuzhiyun */
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun if (bsd_check(db))
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun OUTPUT (CLEAR);
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /*
762*4882a593Smuzhiyun * Pad dribble bits of last code with ones.
763*4882a593Smuzhiyun * Do not emit a completely useless byte of ones.
764*4882a593Smuzhiyun */
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun if (bitno != 32)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun PUTBYTE((accm | (0xff << (bitno-8))) >> 24);
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun /*
772*4882a593Smuzhiyun * Increase code size if we would have without the packet
773*4882a593Smuzhiyun * boundary because the decompressor will do so.
774*4882a593Smuzhiyun */
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun if (max_ent >= mxcode && max_ent < db->maxmaxcode)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun db->n_bits++;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun /* If output length is too large then this is an incomplete frame. */
782*4882a593Smuzhiyun if (wptr == NULL)
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun ++db->incomp_count;
785*4882a593Smuzhiyun db->incomp_bytes += isize;
786*4882a593Smuzhiyun olen = 0;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun else /* Count the number of compressed frames */
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun ++db->comp_count;
791*4882a593Smuzhiyun db->comp_bytes += olen;
792*4882a593Smuzhiyun }
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /* Return the resulting output length */
795*4882a593Smuzhiyun return olen;
796*4882a593Smuzhiyun #undef OUTPUT
797*4882a593Smuzhiyun #undef PUTBYTE
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun * Update the "BSD Compress" dictionary on the receiver for
802*4882a593Smuzhiyun * incompressible data by pretending to compress the incoming data.
803*4882a593Smuzhiyun */
804*4882a593Smuzhiyun
bsd_incomp(void * state,unsigned char * ibuf,int icnt)805*4882a593Smuzhiyun static void bsd_incomp (void *state, unsigned char *ibuf, int icnt)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun (void) bsd_compress (state, ibuf, (char *) 0, icnt, 0);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun /*
811*4882a593Smuzhiyun * Decompress "BSD Compress".
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * Because of patent problems, we return DECOMP_ERROR for errors
814*4882a593Smuzhiyun * found by inspecting the input data and for system problems, but
815*4882a593Smuzhiyun * DECOMP_FATALERROR for any errors which could possibly be said to
816*4882a593Smuzhiyun * be being detected "after" decompression. For DECOMP_ERROR,
817*4882a593Smuzhiyun * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
818*4882a593Smuzhiyun * infringing a patent of Motorola's if we do, so we take CCP down
819*4882a593Smuzhiyun * instead.
820*4882a593Smuzhiyun *
821*4882a593Smuzhiyun * Given that the frame has the correct sequence number and a good FCS,
822*4882a593Smuzhiyun * errors such as invalid codes in the input most likely indicate a
823*4882a593Smuzhiyun * bug, so we return DECOMP_FATALERROR for them in order to turn off
824*4882a593Smuzhiyun * compression, even though they are detected by inspecting the input.
825*4882a593Smuzhiyun */
826*4882a593Smuzhiyun
bsd_decompress(void * state,unsigned char * ibuf,int isize,unsigned char * obuf,int osize)827*4882a593Smuzhiyun static int bsd_decompress (void *state, unsigned char *ibuf, int isize,
828*4882a593Smuzhiyun unsigned char *obuf, int osize)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun struct bsd_db *db;
831*4882a593Smuzhiyun unsigned int max_ent;
832*4882a593Smuzhiyun unsigned long accm;
833*4882a593Smuzhiyun unsigned int bitno; /* 1st valid bit in accm */
834*4882a593Smuzhiyun unsigned int n_bits;
835*4882a593Smuzhiyun unsigned int tgtbitno; /* bitno when we have a code */
836*4882a593Smuzhiyun struct bsd_dict *dictp;
837*4882a593Smuzhiyun int explen;
838*4882a593Smuzhiyun int seq;
839*4882a593Smuzhiyun unsigned int incode;
840*4882a593Smuzhiyun unsigned int oldcode;
841*4882a593Smuzhiyun unsigned int finchar;
842*4882a593Smuzhiyun unsigned char *p;
843*4882a593Smuzhiyun unsigned char *wptr;
844*4882a593Smuzhiyun int adrs;
845*4882a593Smuzhiyun int ctrl;
846*4882a593Smuzhiyun int ilen;
847*4882a593Smuzhiyun int codelen;
848*4882a593Smuzhiyun int extra;
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun db = (struct bsd_db *) state;
851*4882a593Smuzhiyun max_ent = db->max_ent;
852*4882a593Smuzhiyun accm = 0;
853*4882a593Smuzhiyun bitno = 32; /* 1st valid bit in accm */
854*4882a593Smuzhiyun n_bits = db->n_bits;
855*4882a593Smuzhiyun tgtbitno = 32 - n_bits; /* bitno when we have a code */
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun /*
858*4882a593Smuzhiyun * Save the address/control from the PPP header
859*4882a593Smuzhiyun * and then get the sequence number.
860*4882a593Smuzhiyun */
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun adrs = PPP_ADDRESS (ibuf);
863*4882a593Smuzhiyun ctrl = PPP_CONTROL (ibuf);
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun seq = (ibuf[4] << 8) + ibuf[5];
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun ibuf += (PPP_HDRLEN + 2);
868*4882a593Smuzhiyun ilen = isize - (PPP_HDRLEN + 2);
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun /*
871*4882a593Smuzhiyun * Check the sequence number and give up if it differs from
872*4882a593Smuzhiyun * the value we're expecting.
873*4882a593Smuzhiyun */
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun if (seq != db->seqno)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun if (db->debug)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun printk("bsd_decomp%d: bad sequence # %d, expected %d\n",
880*4882a593Smuzhiyun db->unit, seq, db->seqno - 1);
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun return DECOMP_ERROR;
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun ++db->seqno;
886*4882a593Smuzhiyun db->bytes_out += ilen;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun * Fill in the ppp header, but not the last byte of the protocol
890*4882a593Smuzhiyun * (that comes from the decompressed data).
891*4882a593Smuzhiyun */
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun wptr = obuf;
894*4882a593Smuzhiyun *wptr++ = adrs;
895*4882a593Smuzhiyun *wptr++ = ctrl;
896*4882a593Smuzhiyun *wptr++ = 0;
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun oldcode = CLEAR;
899*4882a593Smuzhiyun explen = 3;
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun /*
902*4882a593Smuzhiyun * Keep the checkpoint correctly so that incompressible packets
903*4882a593Smuzhiyun * clear the dictionary at the proper times.
904*4882a593Smuzhiyun */
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun for (;;)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun if (ilen-- <= 0)
909*4882a593Smuzhiyun {
910*4882a593Smuzhiyun db->in_count += (explen - 3); /* don't count the header */
911*4882a593Smuzhiyun break;
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun /*
915*4882a593Smuzhiyun * Accumulate bytes until we have a complete code.
916*4882a593Smuzhiyun * Then get the next code, relying on the 32-bit,
917*4882a593Smuzhiyun * unsigned accm to mask the result.
918*4882a593Smuzhiyun */
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun bitno -= 8;
921*4882a593Smuzhiyun accm |= *ibuf++ << bitno;
922*4882a593Smuzhiyun if (tgtbitno < bitno)
923*4882a593Smuzhiyun {
924*4882a593Smuzhiyun continue;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun incode = accm >> tgtbitno;
928*4882a593Smuzhiyun accm <<= n_bits;
929*4882a593Smuzhiyun bitno += n_bits;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun /*
932*4882a593Smuzhiyun * The dictionary must only be cleared at the end of a packet.
933*4882a593Smuzhiyun */
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun if (incode == CLEAR)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun if (ilen > 0)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun if (db->debug)
940*4882a593Smuzhiyun {
941*4882a593Smuzhiyun printk("bsd_decomp%d: bad CLEAR\n", db->unit);
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun return DECOMP_FATALERROR; /* probably a bug */
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun bsd_clear(db);
947*4882a593Smuzhiyun break;
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
951*4882a593Smuzhiyun || (incode > max_ent && oldcode == CLEAR))
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun if (db->debug)
954*4882a593Smuzhiyun {
955*4882a593Smuzhiyun printk("bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
956*4882a593Smuzhiyun db->unit, incode, oldcode);
957*4882a593Smuzhiyun printk("max_ent=0x%x explen=%d seqno=%d\n",
958*4882a593Smuzhiyun max_ent, explen, db->seqno);
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun return DECOMP_FATALERROR; /* probably a bug */
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun /* Special case for KwKwK string. */
964*4882a593Smuzhiyun if (incode > max_ent)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun finchar = oldcode;
967*4882a593Smuzhiyun extra = 1;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun else
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun finchar = incode;
972*4882a593Smuzhiyun extra = 0;
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun codelen = *(lens_ptr (db, finchar));
976*4882a593Smuzhiyun explen += codelen + extra;
977*4882a593Smuzhiyun if (explen > osize)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun if (db->debug)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun printk("bsd_decomp%d: ran out of mru\n", db->unit);
982*4882a593Smuzhiyun #ifdef DEBUG
983*4882a593Smuzhiyun printk(" len=%d, finchar=0x%x, codelen=%d, explen=%d\n",
984*4882a593Smuzhiyun ilen, finchar, codelen, explen);
985*4882a593Smuzhiyun #endif
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun return DECOMP_FATALERROR;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun /*
991*4882a593Smuzhiyun * Decode this code and install it in the decompressed buffer.
992*4882a593Smuzhiyun */
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun wptr += codelen;
995*4882a593Smuzhiyun p = wptr;
996*4882a593Smuzhiyun while (finchar > LAST)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun struct bsd_dict *dictp2 = dict_ptr (db, finchar);
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun dictp = dict_ptr (db, dictp2->cptr);
1001*4882a593Smuzhiyun #ifdef DEBUG
1002*4882a593Smuzhiyun if (--codelen <= 0 || dictp->codem1 != finchar-1)
1003*4882a593Smuzhiyun {
1004*4882a593Smuzhiyun if (codelen <= 0)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun printk("bsd_decomp%d: fell off end of chain ", db->unit);
1007*4882a593Smuzhiyun printk("0x%x at 0x%x by 0x%x, max_ent=0x%x\n",
1008*4882a593Smuzhiyun incode, finchar, dictp2->cptr, max_ent);
1009*4882a593Smuzhiyun }
1010*4882a593Smuzhiyun else
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun if (dictp->codem1 != finchar-1)
1013*4882a593Smuzhiyun {
1014*4882a593Smuzhiyun printk("bsd_decomp%d: bad code chain 0x%x "
1015*4882a593Smuzhiyun "finchar=0x%x ",
1016*4882a593Smuzhiyun db->unit, incode, finchar);
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun printk("oldcode=0x%x cptr=0x%x codem1=0x%x\n",
1019*4882a593Smuzhiyun oldcode, dictp2->cptr, dictp->codem1);
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun return DECOMP_FATALERROR;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun #endif
1025*4882a593Smuzhiyun *--p = dictp->f.hs.suffix;
1026*4882a593Smuzhiyun finchar = dictp->f.hs.prefix;
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun *--p = finchar;
1029*4882a593Smuzhiyun
1030*4882a593Smuzhiyun #ifdef DEBUG
1031*4882a593Smuzhiyun if (--codelen != 0)
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun printk("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n",
1034*4882a593Smuzhiyun db->unit, codelen, incode, max_ent);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun #endif
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun if (extra) /* the KwKwK case again */
1039*4882a593Smuzhiyun {
1040*4882a593Smuzhiyun *wptr++ = finchar;
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun /*
1044*4882a593Smuzhiyun * If not first code in a packet, and
1045*4882a593Smuzhiyun * if not out of code space, then allocate a new code.
1046*4882a593Smuzhiyun *
1047*4882a593Smuzhiyun * Keep the hash table correct so it can be used
1048*4882a593Smuzhiyun * with uncompressed packets.
1049*4882a593Smuzhiyun */
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun if (oldcode != CLEAR && max_ent < db->maxmaxcode)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun struct bsd_dict *dictp2, *dictp3;
1054*4882a593Smuzhiyun unsigned short *lens1, *lens2;
1055*4882a593Smuzhiyun unsigned long fcode;
1056*4882a593Smuzhiyun int hval, disp, indx;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun fcode = BSD_KEY(oldcode,finchar);
1059*4882a593Smuzhiyun hval = BSD_HASH(oldcode,finchar,db->hshift);
1060*4882a593Smuzhiyun dictp = dict_ptr (db, hval);
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun /* look for a free hash table entry */
1063*4882a593Smuzhiyun if (dictp->codem1 < max_ent)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun disp = (hval == 0) ? 1 : hval;
1066*4882a593Smuzhiyun do
1067*4882a593Smuzhiyun {
1068*4882a593Smuzhiyun hval += disp;
1069*4882a593Smuzhiyun if (hval >= db->hsize)
1070*4882a593Smuzhiyun {
1071*4882a593Smuzhiyun hval -= db->hsize;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun dictp = dict_ptr (db, hval);
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun while (dictp->codem1 < max_ent);
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun /*
1079*4882a593Smuzhiyun * Invalidate previous hash table entry
1080*4882a593Smuzhiyun * assigned this code, and then take it over
1081*4882a593Smuzhiyun */
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun dictp2 = dict_ptr (db, max_ent + 1);
1084*4882a593Smuzhiyun indx = dictp2->cptr;
1085*4882a593Smuzhiyun dictp3 = dict_ptr (db, indx);
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun if (dictp3->codem1 == max_ent)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun dictp3->codem1 = BADCODEM1;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun dictp2->cptr = hval;
1093*4882a593Smuzhiyun dictp->codem1 = max_ent;
1094*4882a593Smuzhiyun dictp->f.fcode = fcode;
1095*4882a593Smuzhiyun db->max_ent = ++max_ent;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /* Update the length of this string. */
1098*4882a593Smuzhiyun lens1 = lens_ptr (db, max_ent);
1099*4882a593Smuzhiyun lens2 = lens_ptr (db, oldcode);
1100*4882a593Smuzhiyun *lens1 = *lens2 + 1;
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun /* Expand code size if needed. */
1103*4882a593Smuzhiyun if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode)
1104*4882a593Smuzhiyun {
1105*4882a593Smuzhiyun db->n_bits = ++n_bits;
1106*4882a593Smuzhiyun tgtbitno = 32-n_bits;
1107*4882a593Smuzhiyun }
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun oldcode = incode;
1110*4882a593Smuzhiyun }
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun ++db->comp_count;
1113*4882a593Smuzhiyun ++db->uncomp_count;
1114*4882a593Smuzhiyun db->comp_bytes += isize - BSD_OVHD - PPP_HDRLEN;
1115*4882a593Smuzhiyun db->uncomp_bytes += explen;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun if (bsd_check(db))
1118*4882a593Smuzhiyun {
1119*4882a593Smuzhiyun if (db->debug)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun printk("bsd_decomp%d: peer should have cleared dictionary on %d\n",
1122*4882a593Smuzhiyun db->unit, db->seqno - 1);
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun return explen;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun /*************************************************************
1129*4882a593Smuzhiyun * Table of addresses for the BSD compression module
1130*4882a593Smuzhiyun *************************************************************/
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun static struct compressor ppp_bsd_compress = {
1133*4882a593Smuzhiyun .compress_proto = CI_BSD_COMPRESS,
1134*4882a593Smuzhiyun .comp_alloc = bsd_comp_alloc,
1135*4882a593Smuzhiyun .comp_free = bsd_free,
1136*4882a593Smuzhiyun .comp_init = bsd_comp_init,
1137*4882a593Smuzhiyun .comp_reset = bsd_reset,
1138*4882a593Smuzhiyun .compress = bsd_compress,
1139*4882a593Smuzhiyun .comp_stat = bsd_comp_stats,
1140*4882a593Smuzhiyun .decomp_alloc = bsd_decomp_alloc,
1141*4882a593Smuzhiyun .decomp_free = bsd_free,
1142*4882a593Smuzhiyun .decomp_init = bsd_decomp_init,
1143*4882a593Smuzhiyun .decomp_reset = bsd_reset,
1144*4882a593Smuzhiyun .decompress = bsd_decompress,
1145*4882a593Smuzhiyun .incomp = bsd_incomp,
1146*4882a593Smuzhiyun .decomp_stat = bsd_comp_stats,
1147*4882a593Smuzhiyun .owner = THIS_MODULE
1148*4882a593Smuzhiyun };
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /*************************************************************
1151*4882a593Smuzhiyun * Module support routines
1152*4882a593Smuzhiyun *************************************************************/
1153*4882a593Smuzhiyun
bsdcomp_init(void)1154*4882a593Smuzhiyun static int __init bsdcomp_init(void)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun int answer = ppp_register_compressor(&ppp_bsd_compress);
1157*4882a593Smuzhiyun if (answer == 0)
1158*4882a593Smuzhiyun printk(KERN_INFO "PPP BSD Compression module registered\n");
1159*4882a593Smuzhiyun return answer;
1160*4882a593Smuzhiyun }
1161*4882a593Smuzhiyun
bsdcomp_cleanup(void)1162*4882a593Smuzhiyun static void __exit bsdcomp_cleanup(void)
1163*4882a593Smuzhiyun {
1164*4882a593Smuzhiyun ppp_unregister_compressor(&ppp_bsd_compress);
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun module_init(bsdcomp_init);
1168*4882a593Smuzhiyun module_exit(bsdcomp_cleanup);
1169*4882a593Smuzhiyun MODULE_LICENSE("Dual BSD/GPL");
1170*4882a593Smuzhiyun MODULE_ALIAS("ppp-compress-" __stringify(CI_BSD_COMPRESS));
1171