]> git.ozlabs.org Git - ppp.git/blob - linux/ppp_deflate.c
*** empty log message ***
[ppp.git] / linux / ppp_deflate.c
1 /*
2  *  ==FILEVERSION 960926==
3  *
4  * ppp_deflate.c - interface the zlib procedures for Deflate compression
5  * and decompression (as used by gzip) to the PPP code.
6  * This version is for use with Linux kernel 1.3.X.
7  *
8  * Copyright (c) 1994 The Australian National University.
9  * All rights reserved.
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation is hereby granted, provided that the above copyright
13  * notice appears in all copies.  This software is provided without any
14  * warranty, express or implied. The Australian National University
15  * makes no representations about the suitability of this software for
16  * any purpose.
17  *
18  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
19  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
20  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21  * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
22  * OF SUCH DAMAGE.
23  *
24  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
27  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
28  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29  * OR MODIFICATIONS.
30  *
31  * From: deflate.c,v 1.1 1996/01/18 03:17:48 paulus Exp
32  */
33
34 #include <linux/module.h>
35
36 #include <endian.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
42 #include <linux/ptrace.h>
43 #include <linux/ioport.h>
44 #include <linux/in.h>
45 #include <linux/malloc.h>
46 #include <linux/tty.h>
47 #include <linux/errno.h>
48 #include <linux/sched.h>        /* to get the struct task_struct */
49 #include <linux/string.h>       /* used in new tty drivers */
50 #include <linux/signal.h>       /* used in new tty drivers */
51
52 #include <asm/system.h>
53 #include <asm/bitops.h>
54 #include <asm/segment.h>
55
56 #include <net/if.h>
57
58 #include <linux/if_ether.h>
59 #include <linux/netdevice.h>
60 #include <linux/skbuff.h>
61 #include <linux/inet.h>
62 #include <linux/ioctl.h>
63
64 #include <linux/ppp_defs.h>
65
66 #ifdef NEW_SKBUFF
67 #include <linux/netprotocol.h>
68 #endif
69
70 #include <netinet/ip.h>
71 #include <netinet/tcp.h>
72 #include <net/if_arp.h>
73 #include <linux/ppp-comp.h>
74
75 #include "zlib.c"
76
77 #define USEMEMPOOL      0       /* use kmalloc, not memPool routines */
78
79 /*
80  * State for a Deflate (de)compressor.
81  */
82 struct ppp_deflate_state {
83     int         seqno;
84     int         w_size;
85     int         unit;
86     int         mru;
87     int         debug;
88     int         in_alloc;       /* set when we're in [de]comp_alloc */
89     z_stream    strm;
90     struct compstat stats;
91 };
92
93 #define DEFLATE_OVHD    2               /* Deflate overhead/packet */
94
95 static void     *zalloc __P((void *, unsigned int items, unsigned int size));
96 static void     zfree __P((void *, void *ptr, unsigned int nb));
97 static void     *z_comp_alloc __P((unsigned char *options, int opt_len));
98 static void     *z_decomp_alloc __P((unsigned char *options, int opt_len));
99 static void     z_comp_free __P((void *state));
100 static void     z_decomp_free __P((void *state));
101 static int      z_comp_init __P((void *state, unsigned char *options,
102                                  int opt_len,
103                                  int unit, int hdrlen, int debug));
104 static int      z_decomp_init __P((void *state, unsigned char *options,
105                                    int opt_len,
106                                    int unit, int hdrlen, int mru, int debug));
107 static int      z_compress __P((void *state, unsigned char *rptr,
108                                 unsigned char *obuf,
109                                 int isize, int osize));
110 static void     z_incomp __P((void *state, unsigned char *ibuf, int icnt));
111 static int      z_decompress __P((void *state, unsigned char *ibuf,
112                                 int isize, unsigned char *obuf, int osize));
113 static void     z_comp_reset __P((void *state));
114 static void     z_decomp_reset __P((void *state));
115 static void     z_comp_stats __P((void *state, struct compstat *stats));
116
117 #if USEMEMPOOL
118 /*
119  * This is a small set of memory allocation routines.  I created them so
120  * that all memory allocation from the kernel takes place at the
121  * z_(de)comp_alloc and z_(de)comp_free routines.  This eliminates worry
122  * about calling valloc() from within an interrupt.
123  *
124  * The free list is a single linked list sorted by memory address.
125  * The zfree() function re-combines any segments it can.
126  */
127 typedef struct memchunk {
128     unsigned int m_size;
129     struct memchunk *m_next;
130 } MemChunk;
131
132 typedef struct {
133     void *memHead;
134     MemChunk *freePool;
135 } MemPool;
136
137 static int     memPoolAlloc __P((void *arg, unsigned int size));
138 static void    memPoolFree __P((void *arg));
139
140 static int
141 memPoolAlloc(arg, size)
142 void *arg;
143 unsigned int size;
144 {
145     MemPool **memPool = arg;
146     MemChunk *freePool;
147
148     if ((*memPool = kmalloc(sizeof(MemPool), GFP_KERNEL)) == NULL) {
149         printk(KERN_DEBUG "Unable to allocate Memory Head\n");
150         return 0;
151     }
152
153     if (((*memPool)->memHead = (void *)vmalloc(size)) == NULL) {
154         printk(KERN_DEBUG "Unable to allocate Memory Pool\n");
155         kfree(*memPool);
156         return 0;
157     }
158     freePool = (*memPool)->freePool = (*memPool)->memHead;
159     freePool->m_size = size;
160     freePool->m_next = 0;
161
162     return 1;
163 }
164
165 static void
166 memPoolFree(arg)
167 void *arg;
168 {
169     MemPool **memPool = arg;
170
171     if (*memPool) {
172         vfree((*memPool)->memHead);
173         kfree(*memPool);
174         *memPool = NULL;
175     }
176 }
177
178 #ifdef POOLDGB
179 static void    showFreeList __P((MemChunk *));
180
181 static void
182 showFreeList(freePool)
183 MemChunk *freePool;
184 {
185     MemChunk *node;
186     
187     for (node = freePool; node; node = node->m_next)
188         printk(KERN_DEBUG "{%x,%d}->", node, node->m_size);
189     printk(KERN_DEBUG "\n");
190 }
191 #endif
192 #endif /* USEMEMPOOL */
193
194 /*
195  * Space allocation and freeing routines for use by zlib routines.
196  */
197 void
198 zfree(arg, ptr, nbytes)
199     void *arg;
200     void *ptr;
201     unsigned int nbytes;
202 {
203 #if !USEMEMPOOL
204     kfree(ptr);
205 #else
206     MemPool *memPool = (MemPool *)arg;
207     MemChunk *mprev = 0, *node;
208     MemChunk *new = (void *)(((unsigned char *)ptr) - sizeof(MemChunk));
209
210     if (!memPool->freePool) {
211         new->m_next = 0;
212         memPool->freePool = new;
213     } else {
214         /*
215          * Find where this new chunk fits in the free list.
216          */
217         for (node = memPool->freePool; node && new > node; node = node->m_next)
218             mprev = node;
219         /*
220          * Re-combine with the following free chunk if possible.
221          */
222         if ((((unsigned char *)new) + new->m_size) == (unsigned char *)node) {
223             new->m_size += node->m_size;
224             new->m_next = node->m_next;
225             if (mprev) {
226                 if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)new) {
227                     mprev->m_size += new->m_size;
228                     mprev->m_next = new->m_next;
229                 } else
230                     mprev->m_next = new;
231             } else
232                 memPool->freePool = new;
233         /*
234          * Re-combine with the previous free chunk if possible.
235          */
236         } else if (mprev && (((unsigned char *)mprev) + mprev->m_size) ==
237           (unsigned char *)new) {
238             mprev->m_size += new->m_size;
239             if ((((unsigned char *)mprev) + mprev->m_size) == (unsigned char *)node) {
240                 mprev->m_size += node->m_size;
241                 mprev->m_next = node->m_next;
242             } else
243                 mprev->m_next = node;
244         /*
245          * No luck re-combining, just insert the new chunk into the list.
246          */
247         } else {
248             if (mprev)
249                 mprev->m_next = new;
250             else
251                 memPool->freePool = new;
252             new->m_next = node;
253         }
254     }
255 #endif /* USEMEMPOOL */
256 }
257
258 void *
259 zalloc(arg, items, size)
260     void *arg;
261     unsigned int items, size;
262 {
263 #if !USEMEMPOOL
264     struct ppp_deflate_state *state = arg;
265
266     return kmalloc(items * size, state->in_alloc? GFP_KERNEL: GFP_ATOMIC);
267 #else
268     MemPool *memPool = (MemPool *)arg;
269     MemChunk *mprev = 0, *node;
270
271     size *= items;
272     size += sizeof(MemChunk);
273     if (size & 0x3)
274         size = (size + 7) & ~3; 
275     for (node = memPool->freePool; node; node = node->m_next) {
276         if (size == node->m_size) {
277             if (mprev)
278                 mprev->m_next = node->m_next;
279             else
280                 memPool->freePool = node->m_next;
281             return (void *)(((unsigned char *)node)+sizeof(MemChunk));
282         } else if (node->m_size > (size + sizeof(MemChunk) + 7)) {
283             MemChunk *new;
284
285             new = (void *)(((unsigned char *)node) + size);
286             new->m_size = node->m_size - size;
287             new->m_next = node->m_next;
288             if (mprev)
289                 mprev->m_next = new;
290             else
291                 memPool->freePool = new;
292
293             node->m_size = size;
294
295             return (void *)(((unsigned char *)node)+sizeof(MemChunk));
296             break;
297         }
298         mprev = node;
299     }
300     printk(KERN_DEBUG
301            "zalloc(%d)... Out of memory in Pool!\n", size - sizeof(MemChunk)); 
302     return NULL;
303 #endif /* USEMEMPOOL */
304 }
305
306 static void
307 z_comp_free(arg)
308     void *arg;
309 {
310     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
311
312     if (state) {
313             deflateEnd(&state->strm);
314 #if USEMEMPOOL
315             memPoolFree(&state->strm.opaque);
316 #endif
317             kfree(state);
318             MOD_DEC_USE_COUNT;
319     }
320 }
321
322 /*
323  * Allocate space for a compressor.
324  */
325 static void *
326 z_comp_alloc(options, opt_len)
327     unsigned char *options;
328     int opt_len;
329 {
330     struct ppp_deflate_state *state;
331     int w_size;
332
333     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
334         || options[1] != CILEN_DEFLATE
335         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
336         || options[3] != DEFLATE_CHK_SEQUENCE)
337         return NULL;
338     w_size = DEFLATE_SIZE(options[2]);
339     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
340         return NULL;
341
342     state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
343     if (state == NULL)
344         return NULL;
345
346     MOD_INC_USE_COUNT;
347     memset (state, 0, sizeof (struct ppp_deflate_state));
348     state->strm.next_in = NULL;
349     state->strm.zalloc  = zalloc;
350     state->strm.zfree   = zfree;
351     state->w_size       = w_size;
352     state->in_alloc = 1;
353
354 #if USEMEMPOOL
355     if (!memPoolAlloc(&state->strm.opaque, 0x50000)) {
356         z_comp_free(state);
357         return NULL;
358     }
359 #else
360     state->strm.opaque = state;
361 #endif
362
363     if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
364                      -w_size, 8, Z_DEFAULT_STRATEGY, DEFLATE_OVHD+2) != Z_OK) {
365         z_comp_free(state);
366         return NULL;
367     }
368     state->in_alloc = 0;
369     return (void *) state;
370 }
371
372 static int
373 z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
374     void *arg;
375     unsigned char *options;
376     int opt_len, unit, hdrlen, debug;
377 {
378     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
379
380     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
381         || options[1] != CILEN_DEFLATE
382         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
383         || DEFLATE_SIZE(options[2]) != state->w_size
384         || options[3] != DEFLATE_CHK_SEQUENCE)
385         return 0;
386
387     state->seqno = 0;
388     state->unit  = unit;
389     state->debug = debug;
390
391     deflateReset(&state->strm);
392
393     return 1;
394 }
395
396 static void
397 z_comp_reset(arg)
398     void *arg;
399 {
400     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
401
402     state->seqno = 0;
403     deflateReset(&state->strm);
404 }
405
406 int
407 z_compress(arg, rptr, obuf, isize, osize)
408     void *arg;
409     unsigned char *rptr;        /* uncompressed packet (in) */
410     unsigned char *obuf;        /* compressed packet (out) */
411     int isize, osize;
412 {
413     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
414     int r, proto, off, olen;
415     unsigned char *wptr;
416
417     /*
418      * Check that the protocol is in the range we handle.
419      */
420     proto = PPP_PROTOCOL(rptr);
421     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
422         return 0;
423
424     /* Don't generate compressed packets which are larger than
425        the uncompressed packet. */
426     if (osize > isize)
427         osize = isize;
428
429     wptr = obuf;
430
431     /*
432      * Copy over the PPP header and store the 2-byte sequence number.
433      */
434     wptr[0] = PPP_ADDRESS(rptr);
435     wptr[1] = PPP_CONTROL(rptr);
436     wptr[2] = PPP_COMP >> 8;
437     wptr[3] = PPP_COMP;
438     wptr += PPP_HDRLEN;
439     wptr[0] = state->seqno >> 8;
440     wptr[1] = state->seqno;
441     wptr += 2;
442     state->strm.next_out = wptr;
443     state->strm.avail_out = osize - (PPP_HDRLEN + 2);
444     ++state->seqno;
445
446     off = (proto > 0xff) ? 2 : 3;       /* skip 1st proto byte if 0 */
447     rptr += off;
448     state->strm.next_in = rptr;
449     state->strm.avail_in = (isize - off);
450
451     olen = 0;
452     for (;;) {
453         r = deflate(&state->strm, Z_PACKET_FLUSH);
454         if (r != Z_OK) {
455             if (state->debug)
456                 printk(KERN_DEBUG "z_compress: deflate returned %d (%s)\n",
457                        r, (state->strm.msg? state->strm.msg: ""));
458             break;
459         }
460         if (state->strm.avail_out == 0) {
461             olen += osize;
462             state->strm.next_out = NULL;
463             state->strm.avail_out = 1000000;
464         } else {
465             break;              /* all done */
466         }
467     }
468     if (olen < osize)
469         olen += osize - state->strm.avail_out;
470
471     /*
472      * See if we managed to reduce the size of the packet.
473      * If the compressor just gave us a single zero byte, it means
474      * the packet was incompressible.
475      */
476     if (olen < isize && !(olen == PPP_HDRLEN + 3 && *wptr == 0)) {
477         state->stats.comp_bytes += olen;
478         state->stats.comp_packets++;
479     } else {
480         state->stats.inc_bytes += isize;
481         state->stats.inc_packets++;
482         olen = 0;
483     }
484     state->stats.unc_bytes += isize;
485     state->stats.unc_packets++;
486
487     return olen;
488 }
489
490 static void
491 z_comp_stats(arg, stats)
492     void *arg;
493     struct compstat *stats;
494 {
495     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
496
497     *stats = state->stats;
498 }
499
500 static void
501 z_decomp_free(arg)
502     void *arg;
503 {
504     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
505
506     if (state) {
507             inflateEnd(&state->strm);
508 #if USEMEMPOOL
509             memPoolFree(&state->strm.opaque);
510 #endif
511             kfree(state);
512             MOD_DEC_USE_COUNT;
513     }
514 }
515
516 /*
517  * Allocate space for a decompressor.
518  */
519 static void *
520 z_decomp_alloc(options, opt_len)
521     unsigned char *options;
522     int opt_len;
523 {
524     struct ppp_deflate_state *state;
525     int w_size;
526
527     if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
528         || options[1] != CILEN_DEFLATE
529         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
530         || options[3] != DEFLATE_CHK_SEQUENCE)
531         return NULL;
532     w_size = DEFLATE_SIZE(options[2]);
533     if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
534         return NULL;
535
536     state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
537     if (state == NULL)
538         return NULL;
539
540     MOD_INC_USE_COUNT;
541     memset (state, 0, sizeof (struct ppp_deflate_state));
542     state->w_size        = w_size;
543     state->strm.next_out = NULL;
544     state->strm.zalloc   = zalloc;
545     state->strm.zfree    = zfree;
546     state->in_alloc = 1;
547
548 #if USEMEMPOOL
549     if (!memPoolAlloc(&state->strm.opaque, 0x10000)) {
550         z_decomp_free(state);
551         return NULL;
552     }
553 #else
554     state->strm.opaque = state;
555 #endif
556
557     if (inflateInit2(&state->strm, -w_size) != Z_OK) {
558         z_decomp_free(state);
559         return NULL;
560     }
561     state->in_alloc = 0;
562     return (void *) state;
563 }
564
565 static int
566 z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
567     void *arg;
568     unsigned char *options;
569     int opt_len, unit, hdrlen, mru, debug;
570 {
571     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
572
573     if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
574         || options[1] != CILEN_DEFLATE
575         || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
576         || DEFLATE_SIZE(options[2]) != state->w_size
577         || options[3] != DEFLATE_CHK_SEQUENCE)
578         return 0;
579
580     state->seqno = 0;
581     state->unit  = unit;
582     state->debug = debug;
583     state->mru   = mru;
584
585     inflateReset(&state->strm);
586
587     return 1;
588 }
589
590 static void
591 z_decomp_reset(arg)
592     void *arg;
593 {
594     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
595
596     state->seqno = 0;
597     inflateReset(&state->strm);
598 }
599
600 /*
601  * Decompress a Deflate-compressed packet.
602  *
603  * Because of patent problems, we return DECOMP_ERROR for errors
604  * found by inspecting the input data and for system problems, but
605  * DECOMP_FATALERROR for any errors which could possibly be said to
606  * be being detected "after" decompression.  For DECOMP_ERROR,
607  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
608  * infringing a patent of Motorola's if we do, so we take CCP down
609  * instead.
610  *
611  * Given that the frame has the correct sequence number and a good FCS,
612  * errors such as invalid codes in the input most likely indicate a
613  * bug, so we return DECOMP_FATALERROR for them in order to turn off
614  * compression, even though they are detected by inspecting the input.
615  */
616 int
617 z_decompress(arg, ibuf, isize, obuf, osize)
618     void *arg;
619     unsigned char *ibuf;
620     int isize;
621     unsigned char *obuf;
622     int osize;
623 {
624     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
625     int olen, seq, r;
626     int decode_proto, overflow;
627     unsigned char overflow_buf[1];
628
629     if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
630         if (state->debug)
631             printk(KERN_DEBUG "z_decompress%d: short packet (len=%d)\n",
632                    state->unit, isize);
633         return DECOMP_ERROR;
634     }
635
636     /* Check the sequence number. */
637     seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
638     if (seq != state->seqno) {
639         if (state->debug)
640             printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
641                    state->unit, seq, state->seqno);
642         return DECOMP_ERROR;
643     }
644     ++state->seqno;
645
646     /*
647      * Fill in the first part of the PPP header.  The protocol field
648      * comes from the decompressed data.
649      */
650     obuf[0] = PPP_ADDRESS(ibuf);
651     obuf[1] = PPP_CONTROL(ibuf);
652     obuf[2] = 0;
653
654     /*
655      * Set up to call inflate.  We set avail_out to 1 initially so we can
656      * look at the first byte of the output and decide whether we have
657      * a 1-byte or 2-byte protocol field.
658      */
659     state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
660     state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
661     state->strm.next_out = obuf + 3;
662     state->strm.avail_out = 1;
663     decode_proto = 1;
664     overflow = 0;
665
666     /*
667      * Call inflate, supplying more input or output as needed.
668      */
669     for (;;) {
670         r = inflate(&state->strm, Z_PACKET_FLUSH);
671         if (r != Z_OK) {
672             if (state->debug)
673                 printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
674                        state->unit, r, (state->strm.msg? state->strm.msg: ""));
675             return DECOMP_FATALERROR;
676         }
677         if (state->strm.avail_out != 0)
678             break;              /* all done */
679         if (decode_proto) {
680             state->strm.avail_out = osize - PPP_HDRLEN;
681             if ((obuf[3] & 1) == 0) {
682                 /* 2-byte protocol field */
683                 obuf[2] = obuf[3];
684                 --state->strm.next_out;
685                 ++state->strm.avail_out;
686             }
687             decode_proto = 0;
688         } else if (!overflow) {
689             /*
690              * We've filled up the output buffer; the only way to
691              * find out whether inflate has any more characters left
692              * is to give it another byte of output space.
693              */
694             state->strm.next_out = overflow_buf;
695             state->strm.avail_out = 1;
696             overflow = 1;
697         } else {
698             if (state->debug)
699                 printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
700                        state->unit);
701             return DECOMP_FATALERROR;
702         }
703     }
704
705     if (decode_proto)
706         return DECOMP_ERROR;
707
708     olen = osize - state->strm.avail_out;
709     state->stats.unc_bytes += olen;
710     state->stats.unc_packets++;
711     state->stats.comp_bytes += isize;
712     state->stats.comp_packets++;
713
714     return olen;
715 }
716
717 /*
718  * Incompressible data has arrived - add it to the history.
719  */
720 static void
721 z_incomp(arg, ibuf, icnt)
722     void *arg;
723     unsigned char *ibuf;
724     int icnt;
725 {
726     struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
727     int proto, r;
728
729     /*
730      * Check that the protocol is one we handle.
731      */
732     proto = PPP_PROTOCOL(ibuf);
733     if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
734         return;
735
736     ++state->seqno;
737
738     /*
739      * Iterate through the message blocks, adding the characters in them
740      * to the decompressor's history.  For the first block, we start
741      * at the either the 1st or 2nd byte of the protocol field,
742      * depending on whether the protocol value is compressible.
743      */
744     state->strm.next_in = ibuf + 3;
745     state->strm.avail_in = icnt - 3;
746     if (proto > 0xff) {
747         --state->strm.next_in;
748         ++state->strm.avail_in;
749     }
750
751     r = inflateIncomp(&state->strm);
752     if (r != Z_OK) {
753         /* gak! */
754         if (state->debug) {
755             printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
756                    state->unit, r, (state->strm.msg? state->strm.msg: ""));
757         }
758         return;
759     }
760
761     /*
762      * Update stats.
763      */
764     state->stats.inc_bytes += icnt;
765     state->stats.inc_packets++;
766     state->stats.unc_bytes += icnt;
767     state->stats.unc_packets++;
768 }
769
770 /*************************************************************
771  * Module interface table
772  *************************************************************/
773
774 /* These are in ppp.c */
775 extern int  ppp_register_compressor   (struct compressor *cp);
776 extern void ppp_unregister_compressor (struct compressor *cp);
777
778 /*
779  * Procedures exported to if_ppp.c.
780  */
781 struct compressor ppp_deflate = {
782     CI_DEFLATE,                 /* compress_proto */
783     z_comp_alloc,               /* comp_alloc */
784     z_comp_free,                /* comp_free */
785     z_comp_init,                /* comp_init */
786     z_comp_reset,               /* comp_reset */
787     z_compress,                 /* compress */
788     z_comp_stats,               /* comp_stat */
789     z_decomp_alloc,             /* decomp_alloc */
790     z_decomp_free,              /* decomp_free */
791     z_decomp_init,              /* decomp_init */
792     z_decomp_reset,             /* decomp_reset */
793     z_decompress,               /* decompress */
794     z_incomp,                   /* incomp */
795     z_comp_stats,               /* decomp_stat */
796 };
797
798 #ifdef MODULE
799 /*************************************************************
800  * Module support routines
801  *************************************************************/
802
803 int
804 init_module(void)
805 {  
806         int answer = ppp_register_compressor (&ppp_deflate);
807         if (answer == 0)
808                 printk (KERN_INFO
809                         "PPP Deflate Compression module registered\n");
810         return answer;
811 }
812      
813 void
814 cleanup_module(void)
815 {
816         if (MOD_IN_USE)
817                 printk (KERN_INFO
818                         "Deflate Compression module busy, remove delayed\n");
819         else
820                 ppp_unregister_compressor (&ppp_deflate);
821 }
822 #endif