OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [isdn/] [isdn_bsdcomp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * BSD compression module
3
 *
4
 * Patched version for ISDN syncPPP written 1997/1998 by Michael Hipp
5
 * The whole module is now SKB based.
6
 *
7
 */
8
 
9
/*
10
 * Update: The Berkeley copyright was changed, and the change
11
 * is retroactive to all "true" BSD software (ie everything
12
 * from UCB as opposed to other peoples code that just carried
13
 * the same license). The new copyright doesn't clash with the
14
 * GPL, so the module-only restriction has been removed..
15
 */
16
 
17
/*
18
 * Original copyright notice:
19
 *
20
 * Copyright (c) 1985, 1986 The Regents of the University of California.
21
 * All rights reserved.
22
 *
23
 * This code is derived from software contributed to Berkeley by
24
 * James A. Woods, derived from original work by Spencer Thomas
25
 * and Joseph Orost.
26
 *
27
 * Redistribution and use in source and binary forms, with or without
28
 * modification, are permitted provided that the following conditions
29
 * are met:
30
 * 1. Redistributions of source code must retain the above copyright
31
 *    notice, this list of conditions and the following disclaimer.
32
 * 2. Redistributions in binary form must reproduce the above copyright
33
 *    notice, this list of conditions and the following disclaimer in the
34
 *    documentation and/or other materials provided with the distribution.
35
 * 3. All advertising materials mentioning features or use of this software
36
 *    must display the following acknowledgement:
37
 *      This product includes software developed by the University of
38
 *      California, Berkeley and its contributors.
39
 * 4. Neither the name of the University nor the names of its contributors
40
 *    may be used to endorse or promote products derived from this software
41
 *    without specific prior written permission.
42
 *
43
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53
 * SUCH DAMAGE.
54
 */
55
 
56
#include <linux/module.h>
57
#include <linux/init.h>
58
#include <linux/kernel.h>
59
#include <linux/sched.h>
60
#include <linux/types.h>
61
#include <linux/fcntl.h>
62
#include <linux/interrupt.h>
63
#include <linux/ptrace.h>
64
#include <linux/ioport.h>
65
#include <linux/in.h>
66
#include <linux/slab.h>
67
#include <linux/tty.h>
68
#include <linux/errno.h>
69
#include <linux/string.h>       /* used in new tty drivers */
70
#include <linux/signal.h>       /* used in new tty drivers */
71
 
72
#include <asm/system.h>
73
#include <asm/bitops.h>
74
#include <asm/segment.h>
75
#include <asm/byteorder.h>
76
#include <asm/types.h>
77
 
78
#include <linux/if.h>
79
 
80
#include <linux/if_ether.h>
81
#include <linux/netdevice.h>
82
#include <linux/skbuff.h>
83
#include <linux/inet.h>
84
#include <linux/ioctl.h>
85
#include <linux/vmalloc.h>
86
 
87
#include <linux/ppp_defs.h>
88
 
89
#include <linux/isdn.h>
90
#include <linux/isdn_ppp.h>
91
#include <linux/ip.h>
92
#include <linux/tcp.h>
93
#include <linux/if_arp.h>
94
#include <linux/ppp-comp.h>
95
 
96
#include "isdn_ppp.h"
97
 
98
MODULE_DESCRIPTION("ISDN4Linux: BSD Compression for PPP over ISDN");
99
MODULE_LICENSE("Dual BSD/GPL");
100
 
101
#define BSD_VERSION(x)  ((x) >> 5)
102
#define BSD_NBITS(x)    ((x) & 0x1F)
103
 
104
#define BSD_CURRENT_VERSION     1
105
 
106
#define DEBUG 1
107
 
108
/*
109
 * A dictionary for doing BSD compress.
110
 */
111
 
112
struct bsd_dict {
113
        u32 fcode;
114
        u16 codem1;             /* output of hash table -1 */
115
        u16 cptr;               /* map code to hash table entry */
116
};
117
 
118
struct bsd_db {
119
        int            totlen;          /* length of this structure */
120
        unsigned int   hsize;           /* size of the hash table */
121
        unsigned char  hshift;          /* used in hash function */
122
        unsigned char  n_bits;          /* current bits/code */
123
        unsigned char  maxbits;         /* maximum bits/code */
124
        unsigned char  debug;           /* non-zero if debug desired */
125
        unsigned char  unit;            /* ppp unit number */
126
        u16 seqno;                      /* sequence # of next packet */
127
        unsigned int   mru;             /* size of receive (decompress) bufr */
128
        unsigned int   maxmaxcode;      /* largest valid code */
129
        unsigned int   max_ent;         /* largest code in use */
130
        unsigned int   in_count;        /* uncompressed bytes, aged */
131
        unsigned int   bytes_out;       /* compressed bytes, aged */
132
        unsigned int   ratio;           /* recent compression ratio */
133
        unsigned int   checkpoint;      /* when to next check the ratio */
134
        unsigned int   clear_count;     /* times dictionary cleared */
135
        unsigned int   incomp_count;    /* incompressible packets */
136
        unsigned int   incomp_bytes;    /* incompressible bytes */
137
        unsigned int   uncomp_count;    /* uncompressed packets */
138
        unsigned int   uncomp_bytes;    /* uncompressed bytes */
139
        unsigned int   comp_count;      /* compressed packets */
140
        unsigned int   comp_bytes;      /* compressed bytes */
141
        unsigned short  *lens;          /* array of lengths of codes */
142
        struct bsd_dict *dict;          /* dictionary */
143
        int xmit;
144
};
145
 
146
#define BSD_OVHD        2               /* BSD compress overhead/packet */
147
#define MIN_BSD_BITS    9
148
#define BSD_INIT_BITS   MIN_BSD_BITS
149
#define MAX_BSD_BITS    15
150
 
151
/*
152
 * the next two codes should not be changed lightly, as they must not
153
 * lie within the contiguous general code space.
154
 */
155
#define CLEAR   256                     /* table clear output code */
156
#define FIRST   257                     /* first free entry */
157
#define LAST    255
158
 
159
#define MAXCODE(b)      ((1 << (b)) - 1)
160
#define BADCODEM1       MAXCODE(MAX_BSD_BITS);
161
 
162
#define BSD_HASH(prefix,suffix,hshift) ((((unsigned long)(suffix))<<(hshift)) \
163
                                         ^ (unsigned long)(prefix))
164
#define BSD_KEY(prefix,suffix)          ((((unsigned long)(suffix)) << 16) \
165
                                         + (unsigned long)(prefix))
166
 
167
#define CHECK_GAP       10000           /* Ratio check interval */
168
 
169
#define RATIO_SCALE_LOG 8
170
#define RATIO_SCALE     (1<<RATIO_SCALE_LOG)
171
#define RATIO_MAX       (0x7fffffff>>RATIO_SCALE_LOG)
172
 
173
/*
174
 * clear the dictionary
175
 */
176
 
177
static void bsd_clear(struct bsd_db *db)
178
{
179
        db->clear_count++;
180
        db->max_ent      = FIRST-1;
181
        db->n_bits       = BSD_INIT_BITS;
182
        db->bytes_out    = 0;
183
        db->in_count     = 0;
184
        db->incomp_count = 0;
185
        db->ratio            = 0;
186
        db->checkpoint   = CHECK_GAP;
187
}
188
 
189
/*
190
 * If the dictionary is full, then see if it is time to reset it.
191
 *
192
 * Compute the compression ratio using fixed-point arithmetic
193
 * with 8 fractional bits.
194
 *
195
 * Since we have an infinite stream instead of a single file,
196
 * watch only the local compression ratio.
197
 *
198
 * Since both peers must reset the dictionary at the same time even in
199
 * the absence of CLEAR codes (while packets are incompressible), they
200
 * must compute the same ratio.
201
 */
202
static int bsd_check (struct bsd_db *db)        /* 1=output CLEAR */
203
{
204
    unsigned int new_ratio;
205
 
206
    if (db->in_count >= db->checkpoint)
207
      {
208
        /* age the ratio by limiting the size of the counts */
209
        if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX)
210
          {
211
            db->in_count  -= (db->in_count  >> 2);
212
            db->bytes_out -= (db->bytes_out >> 2);
213
          }
214
 
215
        db->checkpoint = db->in_count + CHECK_GAP;
216
 
217
        if (db->max_ent >= db->maxmaxcode)
218
          {
219
            /* Reset the dictionary only if the ratio is worse,
220
             * or if it looks as if it has been poisoned
221
             * by incompressible data.
222
             *
223
             * This does not overflow, because
224
             *  db->in_count <= RATIO_MAX.
225
             */
226
 
227
            new_ratio = db->in_count << RATIO_SCALE_LOG;
228
            if (db->bytes_out != 0)
229
              {
230
                new_ratio /= db->bytes_out;
231
              }
232
 
233
            if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE)
234
              {
235
                bsd_clear (db);
236
                return 1;
237
              }
238
            db->ratio = new_ratio;
239
          }
240
      }
241
    return 0;
242
}
243
 
244
/*
245
 * Return statistics.
246
 */
247
 
248
static void bsd_stats (void *state, struct compstat *stats)
249
{
250
        struct bsd_db *db = (struct bsd_db *) state;
251
 
252
        stats->unc_bytes    = db->uncomp_bytes;
253
        stats->unc_packets  = db->uncomp_count;
254
        stats->comp_bytes   = db->comp_bytes;
255
        stats->comp_packets = db->comp_count;
256
        stats->inc_bytes    = db->incomp_bytes;
257
        stats->inc_packets  = db->incomp_count;
258
        stats->in_count     = db->in_count;
259
        stats->bytes_out    = db->bytes_out;
260
}
261
 
262
/*
263
 * Reset state, as on a CCP ResetReq.
264
 */
265
static void bsd_reset (void *state,unsigned char code, unsigned char id,
266
                        unsigned char *data, unsigned len,
267
                        struct isdn_ppp_resetparams *rsparm)
268
{
269
        struct bsd_db *db = (struct bsd_db *) state;
270
 
271
        bsd_clear(db);
272
        db->seqno       = 0;
273
        db->clear_count = 0;
274
}
275
 
276
/*
277
 * Release the compression structure
278
 */
279
static void bsd_free (void *state)
280
{
281
        struct bsd_db *db = (struct bsd_db *) state;
282
 
283
        if (db) {
284
                /*
285
                 * Release the dictionary
286
                 */
287
                if (db->dict) {
288
                        vfree (db->dict);
289
                        db->dict = NULL;
290
                }
291
 
292
                /*
293
                 * Release the string buffer
294
                 */
295
                if (db->lens) {
296
                        vfree (db->lens);
297
                        db->lens = NULL;
298
                }
299
 
300
                /*
301
                 * Finally release the structure itself.
302
                 */
303
                kfree (db);
304
                MOD_DEC_USE_COUNT;
305
        }
306
}
307
 
308
 
309
/*
310
 * Allocate space for a (de) compressor.
311
 */
312
static void *bsd_alloc (struct isdn_ppp_comp_data *data)
313
{
314
        int bits;
315
        unsigned int hsize, hshift, maxmaxcode;
316
        struct bsd_db *db;
317
        int decomp;
318
 
319
        static unsigned int htab[][2] = {
320
                { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } ,
321
                { 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 }
322
        };
323
 
324
        if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
325
                || BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
326
                return NULL;
327
 
328
        bits = BSD_NBITS(data->options[0]);
329
 
330
        if(bits < 9 || bits > 15)
331
                return NULL;
332
 
333
        hsize = htab[bits-9][0];
334
        hshift = htab[bits-9][1];
335
 
336
        /*
337
         * Allocate the main control structure for this instance.
338
         */
339
        maxmaxcode = MAXCODE(bits);
340
        db = (struct bsd_db *) kmalloc (sizeof (struct bsd_db),GFP_KERNEL);
341
        if (!db)
342
                return NULL;
343
 
344
        memset (db, 0, sizeof(struct bsd_db));
345
 
346
        db->xmit = data->flags & IPPP_COMP_FLAG_XMIT;
347
        decomp = db->xmit ? 0 : 1;
348
 
349
        /*
350
         * Allocate space for the dictionary. This may be more than one page in
351
         * length.
352
         */
353
        db->dict = (struct bsd_dict *) vmalloc (hsize * sizeof (struct bsd_dict));
354
        if (!db->dict) {
355
                bsd_free (db);
356
                return NULL;
357
        }
358
 
359
        MOD_INC_USE_COUNT;
360
 
361
        /*
362
         * If this is the compression buffer then there is no length data.
363
         * For decompression, the length information is needed as well.
364
         */
365
        if (!decomp)
366
                db->lens = NULL;
367
        else {
368
                db->lens = (unsigned short *) vmalloc ((maxmaxcode + 1) *
369
                        sizeof (db->lens[0]));
370
                if (!db->lens) {
371
                        bsd_free (db); /* calls MOD_DEC_USE_COUNT; */
372
                        return (NULL);
373
                }
374
        }
375
 
376
        /*
377
         * Initialize the data information for the compression code
378
         */
379
        db->totlen     = sizeof (struct bsd_db) + (sizeof (struct bsd_dict) * hsize);
380
        db->hsize      = hsize;
381
        db->hshift     = hshift;
382
        db->maxmaxcode = maxmaxcode;
383
        db->maxbits    = bits;
384
 
385
        return (void *) db;
386
}
387
 
388
/*
389
 * Initialize the database.
390
 */
391
static int bsd_init (void *state, struct isdn_ppp_comp_data *data, int unit, int debug)
392
{
393
        struct bsd_db *db = state;
394
        int indx;
395
        int decomp;
396
 
397
        if(!state || !data) {
398
                printk(KERN_ERR "isdn_bsd_init: [%d] ERR, state %lx data %lx\n",unit,(long)state,(long)data);
399
                return 0;
400
        }
401
 
402
        decomp = db->xmit ? 0 : 1;
403
 
404
        if (data->optlen != 1 || data->num != CI_BSD_COMPRESS
405
                || (BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION)
406
                || (BSD_NBITS(data->options[0]) != db->maxbits)
407
                || (decomp && db->lens == NULL)) {
408
                printk(KERN_ERR "isdn_bsd: %d %d %d %d %lx\n",data->optlen,data->num,data->options[0],decomp,(unsigned long)db->lens);
409
                return 0;
410
        }
411
 
412
        if (decomp)
413
                for(indx=LAST;indx>=0;indx--)
414
                        db->lens[indx] = 1;
415
 
416
        indx = db->hsize;
417
        while (indx-- != 0) {
418
                db->dict[indx].codem1 = BADCODEM1;
419
                db->dict[indx].cptr   = 0;
420
        }
421
 
422
        db->unit = unit;
423
        db->mru  = 0;
424
 
425
        db->debug = 1;
426
 
427
        bsd_reset(db,0,0,NULL,0,NULL);
428
 
429
        return 1;
430
}
431
 
432
/*
433
 * Obtain pointers to the various structures in the compression tables
434
 */
435
 
436
#define dict_ptrx(p,idx) &(p->dict[idx])
437
#define lens_ptrx(p,idx) &(p->lens[idx])
438
 
439
#ifdef DEBUG
440
static unsigned short *lens_ptr(struct bsd_db *db, int idx)
441
{
442
        if ((unsigned int) idx > (unsigned int) db->maxmaxcode) {
443
                printk (KERN_DEBUG "<9>ppp: lens_ptr(%d) > max\n", idx);
444
                idx = 0;
445
        }
446
        return lens_ptrx (db, idx);
447
}
448
 
449
static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx)
450
{
451
        if ((unsigned int) idx >= (unsigned int) db->hsize) {
452
                printk (KERN_DEBUG "<9>ppp: dict_ptr(%d) > max\n", idx);
453
                idx = 0;
454
        }
455
        return dict_ptrx (db, idx);
456
}
457
 
458
#else
459
#define lens_ptr(db,idx) lens_ptrx(db,idx)
460
#define dict_ptr(db,idx) dict_ptrx(db,idx)
461
#endif
462
 
463
/*
464
 * compress a packet
465
 */
466
static int bsd_compress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,int proto)
467
{
468
        struct bsd_db *db;
469
        int hshift;
470
        unsigned int max_ent;
471
        unsigned int n_bits;
472
        unsigned int bitno;
473
        unsigned long accm;
474
        int ent;
475
        unsigned long fcode;
476
        struct bsd_dict *dictp;
477
        unsigned char c;
478
        int hval,disp,ilen,mxcode;
479
        unsigned char *rptr = skb_in->data;
480
        int isize = skb_in->len;
481
 
482
#define OUTPUT(ent)                     \
483
  {                                     \
484
    bitno -= n_bits;                    \
485
    accm |= ((ent) << bitno);           \
486
    do  {                               \
487
        if(skb_out && skb_tailroom(skb_out) > 0)         \
488
                *(skb_put(skb_out,1)) = (unsigned char) (accm>>24); \
489
        accm <<= 8;                     \
490
        bitno += 8;                     \
491
    } while (bitno <= 24);              \
492
  }
493
 
494
        /*
495
         * If the protocol is not in the range we're interested in,
496
         * just return without compressing the packet.  If it is,
497
         * the protocol becomes the first byte to compress.
498
         */
499
        printk(KERN_DEBUG "bsd_compress called with %x\n",proto);
500
 
501
        ent = proto;
502
        if (proto < 0x21 || proto > 0xf9 || !(proto & 0x1) )
503
                return 0;
504
 
505
        db      = (struct bsd_db *) state;
506
        hshift  = db->hshift;
507
        max_ent = db->max_ent;
508
        n_bits  = db->n_bits;
509
        bitno   = 32;
510
        accm    = 0;
511
        mxcode  = MAXCODE (n_bits);
512
 
513
        /* This is the PPP header information */
514
        if(skb_out && skb_tailroom(skb_out) >= 2) {
515
                char *v = skb_put(skb_out,2);
516
                /* we only push our own data on the header,
517
                  AC,PC and protos is pushed by caller  */
518
                v[0] = db->seqno >> 8;
519
                v[1] = db->seqno;
520
        }
521
 
522
        ilen   = ++isize; /* This is off by one, but that is what is in draft! */
523
 
524
        while (--ilen > 0) {
525
                c     = *rptr++;
526
                fcode = BSD_KEY  (ent, c);
527
                hval  = BSD_HASH (ent, c, hshift);
528
                dictp = dict_ptr (db, hval);
529
 
530
                /* Validate and then check the entry. */
531
                if (dictp->codem1 >= max_ent)
532
                        goto nomatch;
533
 
534
                if (dictp->fcode == fcode) {
535
                        ent = dictp->codem1 + 1;
536
                        continue;       /* found (prefix,suffix) */
537
                }
538
 
539
                /* continue probing until a match or invalid entry */
540
                disp = (hval == 0) ? 1 : hval;
541
 
542
                do {
543
                        hval += disp;
544
                        if (hval >= db->hsize)
545
                                hval -= db->hsize;
546
                        dictp = dict_ptr (db, hval);
547
                        if (dictp->codem1 >= max_ent)
548
                                goto nomatch;
549
                } while (dictp->fcode != fcode);
550
 
551
                ent = dictp->codem1 + 1;        /* finally found (prefix,suffix) */
552
                continue;
553
 
554
nomatch:
555
                OUTPUT(ent);            /* output the prefix */
556
 
557
                /* code -> hashtable */
558
                if (max_ent < db->maxmaxcode) {
559
                        struct bsd_dict *dictp2;
560
                        struct bsd_dict *dictp3;
561
                        int indx;
562
 
563
                        /* expand code size if needed */
564
                        if (max_ent >= mxcode) {
565
                                db->n_bits = ++n_bits;
566
                                mxcode = MAXCODE (n_bits);
567
                        }
568
 
569
                        /*
570
                         * Invalidate old hash table entry using
571
                         * this code, and then take it over.
572
                         */
573
                        dictp2 = dict_ptr (db, max_ent + 1);
574
                        indx   = dictp2->cptr;
575
                        dictp3 = dict_ptr (db, indx);
576
 
577
                        if (dictp3->codem1 == max_ent)
578
                                dictp3->codem1 = BADCODEM1;
579
 
580
                        dictp2->cptr   = hval;
581
                        dictp->codem1  = max_ent;
582
                        dictp->fcode = fcode;
583
                        db->max_ent    = ++max_ent;
584
 
585
                        if (db->lens) {
586
                                unsigned short *len1 = lens_ptr (db, max_ent);
587
                                unsigned short *len2 = lens_ptr (db, ent);
588
                                *len1 = *len2 + 1;
589
                        }
590
                }
591
                ent = c;
592
        }
593
 
594
        OUTPUT(ent);            /* output the last code */
595
 
596
        if(skb_out)
597
                db->bytes_out    += skb_out->len; /* Do not count bytes from here */
598
        db->uncomp_bytes += isize;
599
        db->in_count     += isize;
600
        ++db->uncomp_count;
601
        ++db->seqno;
602
 
603
        if (bitno < 32)
604
                ++db->bytes_out; /* must be set before calling bsd_check */
605
 
606
        /*
607
         * Generate the clear command if needed
608
         */
609
 
610
        if (bsd_check(db))
611
                OUTPUT (CLEAR);
612
 
613
        /*
614
         * Pad dribble bits of last code with ones.
615
         * Do not emit a completely useless byte of ones.
616
         */
617
        if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0)
618
                *(skb_put(skb_out,1)) = (unsigned char) ((accm | (0xff << (bitno-8))) >> 24);
619
 
620
        /*
621
         * Increase code size if we would have without the packet
622
         * boundary because the decompressor will do so.
623
         */
624
        if (max_ent >= mxcode && max_ent < db->maxmaxcode)
625
                db->n_bits++;
626
 
627
        /* If output length is too large then this is an incompressible frame. */
628
        if (!skb_out || (skb_out && skb_out->len >= skb_in->len) ) {
629
                ++db->incomp_count;
630
                db->incomp_bytes += isize;
631
                return 0;
632
        }
633
 
634
        /* Count the number of compressed frames */
635
        ++db->comp_count;
636
        db->comp_bytes += skb_out->len;
637
        return skb_out->len;
638
 
639
#undef OUTPUT
640
}
641
 
642
/*
643
 * Update the "BSD Compress" dictionary on the receiver for
644
 * incompressible data by pretending to compress the incoming data.
645
 */
646
static void bsd_incomp (void *state, struct sk_buff *skb_in,int proto)
647
{
648
        bsd_compress (state, skb_in, NULL, proto);
649
}
650
 
651
/*
652
 * Decompress "BSD Compress".
653
 */
654
static int bsd_decompress (void *state, struct sk_buff *skb_in, struct sk_buff *skb_out,
655
                           struct isdn_ppp_resetparams *rsparm)
656
{
657
        struct bsd_db *db;
658
        unsigned int max_ent;
659
        unsigned long accm;
660
        unsigned int bitno;             /* 1st valid bit in accm */
661
        unsigned int n_bits;
662
        unsigned int tgtbitno;  /* bitno when we have a code */
663
        struct bsd_dict *dictp;
664
        int seq;
665
        unsigned int incode;
666
        unsigned int oldcode;
667
        unsigned int finchar;
668
        unsigned char *p,*ibuf;
669
        int ilen;
670
        int codelen;
671
        int extra;
672
 
673
        db       = (struct bsd_db *) state;
674
        max_ent  = db->max_ent;
675
        accm     = 0;
676
        bitno    = 32;          /* 1st valid bit in accm */
677
        n_bits   = db->n_bits;
678
        tgtbitno = 32 - n_bits; /* bitno when we have a code */
679
 
680
        printk(KERN_DEBUG "bsd_decompress called\n");
681
 
682
        if(!skb_in || !skb_out) {
683
                printk(KERN_ERR "bsd_decompress called with NULL parameter\n");
684
                return DECOMP_ERROR;
685
        }
686
 
687
        /*
688
         * Get the sequence number.
689
         */
690
        if( (p = skb_pull(skb_in,2)) == NULL) {
691
                return DECOMP_ERROR;
692
        }
693
        p-=2;
694
        seq   = (p[0] << 8) + p[1];
695
        ilen  = skb_in->len;
696
        ibuf = skb_in->data;
697
 
698
        /*
699
         * Check the sequence number and give up if it differs from
700
         * the value we're expecting.
701
         */
702
        if (seq != db->seqno) {
703
                if (db->debug) {
704
                        printk(KERN_DEBUG "bsd_decomp%d: bad sequence # %d, expected %d\n",
705
                                db->unit, seq, db->seqno - 1);
706
                }
707
                return DECOMP_ERROR;
708
        }
709
 
710
        ++db->seqno;
711
        db->bytes_out += ilen;
712
 
713
        if(skb_tailroom(skb_out) > 0)
714
                *(skb_put(skb_out,1)) = 0;
715
        else
716
                return DECOMP_ERR_NOMEM;
717
 
718
        oldcode = CLEAR;
719
 
720
        /*
721
         * Keep the checkpoint correctly so that incompressible packets
722
         * clear the dictionary at the proper times.
723
         */
724
 
725
        for (;;) {
726
                if (ilen-- <= 0) {
727
                        db->in_count += (skb_out->len - 1); /* don't count the header */
728
                        break;
729
                }
730
 
731
                /*
732
                 * Accumulate bytes until we have a complete code.
733
                 * Then get the next code, relying on the 32-bit,
734
                 * unsigned accm to mask the result.
735
                 */
736
 
737
                bitno -= 8;
738
                accm  |= *ibuf++ << bitno;
739
                if (tgtbitno < bitno)
740
                        continue;
741
 
742
                incode = accm >> tgtbitno;
743
                accm <<= n_bits;
744
                bitno += n_bits;
745
 
746
                /*
747
                 * The dictionary must only be cleared at the end of a packet.
748
                 */
749
 
750
                if (incode == CLEAR) {
751
                        if (ilen > 0) {
752
                                if (db->debug)
753
                                        printk(KERN_DEBUG "bsd_decomp%d: bad CLEAR\n", db->unit);
754
                                return DECOMP_FATALERROR;       /* probably a bug */
755
                        }
756
                        bsd_clear(db);
757
                        break;
758
                }
759
 
760
                if ((incode > max_ent + 2) || (incode > db->maxmaxcode)
761
                        || (incode > max_ent && oldcode == CLEAR)) {
762
                        if (db->debug) {
763
                                printk(KERN_DEBUG "bsd_decomp%d: bad code 0x%x oldcode=0x%x ",
764
                                        db->unit, incode, oldcode);
765
                                printk(KERN_DEBUG "max_ent=0x%x skb->Len=%d seqno=%d\n",
766
                                        max_ent, skb_out->len, db->seqno);
767
                        }
768
                        return DECOMP_FATALERROR;       /* probably a bug */
769
                }
770
 
771
                /* Special case for KwKwK string. */
772
                if (incode > max_ent) {
773
                        finchar = oldcode;
774
                        extra   = 1;
775
                } else {
776
                        finchar = incode;
777
                        extra   = 0;
778
                }
779
 
780
                codelen = *(lens_ptr (db, finchar));
781
                if( skb_tailroom(skb_out) < codelen + extra) {
782
                        if (db->debug) {
783
                                printk(KERN_DEBUG "bsd_decomp%d: ran out of mru\n", db->unit);
784
#ifdef DEBUG
785
                                printk(KERN_DEBUG "  len=%d, finchar=0x%x, codelen=%d,skblen=%d\n",
786
                                        ilen, finchar, codelen, skb_out->len);
787
#endif
788
                        }
789
                        return DECOMP_FATALERROR;
790
                }
791
 
792
                /*
793
                 * Decode this code and install it in the decompressed buffer.
794
                 */
795
 
796
                p     = skb_put(skb_out,codelen);
797
                p += codelen;
798
                while (finchar > LAST) {
799
                        struct bsd_dict *dictp2 = dict_ptr (db, finchar);
800
 
801
                        dictp = dict_ptr (db, dictp2->cptr);
802
 
803
#ifdef DEBUG
804
                        if (--codelen <= 0 || dictp->codem1 != finchar-1) {
805
                                if (codelen <= 0) {
806
                                        printk(KERN_ERR "bsd_decomp%d: fell off end of chain ", db->unit);
807
                                        printk(KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->cptr, max_ent);
808
                                } else {
809
                                        if (dictp->codem1 != finchar-1) {
810
                                                printk(KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ",db->unit, incode, finchar);
811
                                                printk(KERN_ERR "oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, dictp2->cptr, dictp->codem1);
812
                                        }
813
                                }
814
                                return DECOMP_FATALERROR;
815
                        }
816
#endif
817
 
818
                        {
819
                                u32 fcode = dictp->fcode;
820
                                *--p    = (fcode >> 16) & 0xff;
821
                                finchar = fcode & 0xffff;
822
                        }
823
                }
824
                *--p = finchar;
825
 
826
#ifdef DEBUG
827
                if (--codelen != 0)
828
                        printk(KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->unit, codelen, incode, max_ent);
829
#endif
830
 
831
                if (extra)              /* the KwKwK case again */
832
                        *(skb_put(skb_out,1)) = finchar;
833
 
834
                /*
835
                 * If not first code in a packet, and
836
                 * if not out of code space, then allocate a new code.
837
                 *
838
                 * Keep the hash table correct so it can be used
839
                 * with uncompressed packets.
840
                 */
841
                if (oldcode != CLEAR && max_ent < db->maxmaxcode) {
842
                        struct bsd_dict *dictp2, *dictp3;
843
                        u16  *lens1,  *lens2;
844
                        unsigned long fcode;
845
                        int hval, disp, indx;
846
 
847
                        fcode = BSD_KEY(oldcode,finchar);
848
                        hval  = BSD_HASH(oldcode,finchar,db->hshift);
849
                        dictp = dict_ptr (db, hval);
850
 
851
                        /* look for a free hash table entry */
852
                        if (dictp->codem1 < max_ent) {
853
                                disp = (hval == 0) ? 1 : hval;
854
                                do {
855
                                        hval += disp;
856
                                        if (hval >= db->hsize)
857
                                                hval -= db->hsize;
858
                                        dictp = dict_ptr (db, hval);
859
                                } while (dictp->codem1 < max_ent);
860
                        }
861
 
862
                        /*
863
                         * Invalidate previous hash table entry
864
                         * assigned this code, and then take it over
865
                         */
866
 
867
                        dictp2 = dict_ptr (db, max_ent + 1);
868
                        indx   = dictp2->cptr;
869
                        dictp3 = dict_ptr (db, indx);
870
 
871
                        if (dictp3->codem1 == max_ent)
872
                                dictp3->codem1 = BADCODEM1;
873
 
874
                        dictp2->cptr   = hval;
875
                        dictp->codem1  = max_ent;
876
                        dictp->fcode = fcode;
877
                        db->max_ent    = ++max_ent;
878
 
879
                        /* Update the length of this string. */
880
                        lens1  = lens_ptr (db, max_ent);
881
                        lens2  = lens_ptr (db, oldcode);
882
                        *lens1 = *lens2 + 1;
883
 
884
                        /* Expand code size if needed. */
885
                        if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) {
886
                                db->n_bits = ++n_bits;
887
                                tgtbitno   = 32-n_bits;
888
                        }
889
                }
890
                oldcode = incode;
891
        }
892
 
893
        ++db->comp_count;
894
        ++db->uncomp_count;
895
        db->comp_bytes   += skb_in->len - BSD_OVHD;
896
        db->uncomp_bytes += skb_out->len;
897
 
898
        if (bsd_check(db)) {
899
                if (db->debug)
900
                        printk(KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n",
901
                                db->unit, db->seqno - 1);
902
        }
903
        return skb_out->len;
904
}
905
 
906
/*************************************************************
907
 * Table of addresses for the BSD compression module
908
 *************************************************************/
909
 
910
static struct isdn_ppp_compressor ippp_bsd_compress = {
911
        NULL,NULL,              /* prev,next: overwritten by isdn_ppp */
912
        CI_BSD_COMPRESS,        /* compress_proto */
913
        bsd_alloc,              /* alloc */
914
        bsd_free,               /* free */
915
        bsd_init,               /* init */
916
        bsd_reset,              /* reset */
917
        bsd_compress,           /* compress */
918
        bsd_decompress,         /* decompress */
919
        bsd_incomp,             /* incomp */
920
        bsd_stats               /* comp_stat */
921
};
922
 
923
/*************************************************************
924
 * Module support routines
925
 *************************************************************/
926
 
927
static int __init isdn_bsdcomp_init(void)
928
{
929
        int answer = isdn_ppp_register_compressor (&ippp_bsd_compress);
930
        if (answer == 0)
931
                printk (KERN_INFO "PPP BSD Compression module registered\n");
932
        return answer;
933
}
934
 
935
static void __exit isdn_bsdcomp_exit(void)
936
{
937
        isdn_ppp_unregister_compressor (&ippp_bsd_compress);
938
}
939
 
940
module_init(isdn_bsdcomp_init);
941
module_exit(isdn_bsdcomp_exit);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.