ppp_deflate.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  ==FILEVERSION 980319==
  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. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/init.h>
  37. #include <linux/ppp_defs.h>
  38. #include <linux/ppp-comp.h>
  39. #include <linux/zlib.h>
  40. /*
  41.  * State for a Deflate (de)compressor.
  42.  */
  43. struct ppp_deflate_state {
  44.     int seqno;
  45.     int w_size;
  46.     int unit;
  47.     int mru;
  48.     int debug;
  49.     z_stream strm;
  50.     struct compstat stats;
  51. };
  52. #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
  53. static void *z_comp_alloc __P((unsigned char *options, int opt_len));
  54. static void *z_decomp_alloc __P((unsigned char *options, int opt_len));
  55. static void z_comp_free __P((void *state));
  56. static void z_decomp_free __P((void *state));
  57. static int z_comp_init __P((void *state, unsigned char *options,
  58.  int opt_len,
  59.  int unit, int hdrlen, int debug));
  60. static int z_decomp_init __P((void *state, unsigned char *options,
  61.    int opt_len,
  62.    int unit, int hdrlen, int mru, int debug));
  63. static int z_compress __P((void *state, unsigned char *rptr,
  64. unsigned char *obuf,
  65. int isize, int osize));
  66. static void z_incomp __P((void *state, unsigned char *ibuf, int icnt));
  67. static int z_decompress __P((void *state, unsigned char *ibuf,
  68. int isize, unsigned char *obuf, int osize));
  69. static void z_comp_reset __P((void *state));
  70. static void z_decomp_reset __P((void *state));
  71. static void z_comp_stats __P((void *state, struct compstat *stats));
  72. static void
  73. z_comp_free(arg)
  74.     void *arg;
  75. {
  76. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  77. if (state) {
  78. zlib_deflateEnd(&state->strm);
  79. if (state->strm.workspace)
  80. vfree(state->strm.workspace);
  81. kfree(state);
  82. MOD_DEC_USE_COUNT;
  83. }
  84. }
  85. /*
  86.  * Allocate space for a compressor.
  87.  */
  88. static void *
  89. z_comp_alloc(options, opt_len)
  90.     unsigned char *options;
  91.     int opt_len;
  92. {
  93. struct ppp_deflate_state *state;
  94. int w_size;
  95. if (opt_len != CILEN_DEFLATE
  96.     || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  97.     || options[1] != CILEN_DEFLATE
  98.     || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  99.     || options[3] != DEFLATE_CHK_SEQUENCE)
  100. return NULL;
  101. w_size = DEFLATE_SIZE(options[2]);
  102. if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
  103. return NULL;
  104. state = (struct ppp_deflate_state *) kmalloc(sizeof(*state),
  105.      GFP_KERNEL);
  106. if (state == NULL)
  107. return NULL;
  108. MOD_INC_USE_COUNT;
  109. memset (state, 0, sizeof (struct ppp_deflate_state));
  110. state->strm.next_in   = NULL;
  111. state->w_size         = w_size;
  112. state->strm.workspace = vmalloc(zlib_deflate_workspacesize());
  113. if (state->strm.workspace == NULL)
  114. goto out_free;
  115. if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
  116.  DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
  117.     != Z_OK)
  118. goto out_free;
  119. return (void *) state;
  120. out_free:
  121. z_comp_free(state);
  122. return NULL;
  123. }
  124. static int
  125. z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
  126.     void *arg;
  127.     unsigned char *options;
  128.     int opt_len, unit, hdrlen, debug;
  129. {
  130. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  131. if (opt_len < CILEN_DEFLATE
  132.     || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  133.     || options[1] != CILEN_DEFLATE
  134.     || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  135.     || DEFLATE_SIZE(options[2]) != state->w_size
  136.     || options[3] != DEFLATE_CHK_SEQUENCE)
  137. return 0;
  138. state->seqno = 0;
  139. state->unit  = unit;
  140. state->debug = debug;
  141. zlib_deflateReset(&state->strm);
  142. return 1;
  143. }
  144. static void
  145. z_comp_reset(arg)
  146.     void *arg;
  147. {
  148. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  149. state->seqno = 0;
  150. zlib_deflateReset(&state->strm);
  151. }
  152. int
  153. z_compress(arg, rptr, obuf, isize, osize)
  154.     void *arg;
  155.     unsigned char *rptr; /* uncompressed packet (in) */
  156.     unsigned char *obuf; /* compressed packet (out) */
  157.     int isize, osize;
  158. {
  159. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  160. int r, proto, off, olen, oavail;
  161. unsigned char *wptr;
  162. /*
  163.  * Check that the protocol is in the range we handle.
  164.  */
  165. proto = PPP_PROTOCOL(rptr);
  166. if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
  167. return 0;
  168. /* Don't generate compressed packets which are larger than
  169.    the uncompressed packet. */
  170. if (osize > isize)
  171. osize = isize;
  172. wptr = obuf;
  173. /*
  174.  * Copy over the PPP header and store the 2-byte sequence number.
  175.  */
  176. wptr[0] = PPP_ADDRESS(rptr);
  177. wptr[1] = PPP_CONTROL(rptr);
  178. wptr[2] = PPP_COMP >> 8;
  179. wptr[3] = PPP_COMP;
  180. wptr += PPP_HDRLEN;
  181. wptr[0] = state->seqno >> 8;
  182. wptr[1] = state->seqno;
  183. wptr += DEFLATE_OVHD;
  184. olen = PPP_HDRLEN + DEFLATE_OVHD;
  185. state->strm.next_out = wptr;
  186. state->strm.avail_out = oavail = osize - olen;
  187. ++state->seqno;
  188. off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
  189. rptr += off;
  190. state->strm.next_in = rptr;
  191. state->strm.avail_in = (isize - off);
  192. for (;;) {
  193. r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
  194. if (r != Z_OK) {
  195. if (state->debug)
  196. printk(KERN_ERR
  197.        "z_compress: deflate returned %dn", r);
  198. break;
  199. }
  200. if (state->strm.avail_out == 0) {
  201. olen += oavail;
  202. state->strm.next_out = NULL;
  203. state->strm.avail_out = oavail = 1000000;
  204. } else {
  205. break; /* all done */
  206. }
  207. }
  208. olen += oavail - state->strm.avail_out;
  209. /*
  210.  * See if we managed to reduce the size of the packet.
  211.  */
  212. if (olen < isize) {
  213. state->stats.comp_bytes += olen;
  214. state->stats.comp_packets++;
  215. } else {
  216. state->stats.inc_bytes += isize;
  217. state->stats.inc_packets++;
  218. olen = 0;
  219. }
  220. state->stats.unc_bytes += isize;
  221. state->stats.unc_packets++;
  222. return olen;
  223. }
  224. static void
  225. z_comp_stats(arg, stats)
  226.     void *arg;
  227.     struct compstat *stats;
  228. {
  229. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  230. *stats = state->stats;
  231. }
  232. static void
  233. z_decomp_free(arg)
  234.     void *arg;
  235. {
  236. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  237. if (state) {
  238. zlib_inflateEnd(&state->strm);
  239. if (state->strm.workspace)
  240. kfree(state->strm.workspace);
  241. kfree(state);
  242. MOD_DEC_USE_COUNT;
  243. }
  244. }
  245. /*
  246.  * Allocate space for a decompressor.
  247.  */
  248. static void *
  249. z_decomp_alloc(options, opt_len)
  250.     unsigned char *options;
  251.     int opt_len;
  252. {
  253. struct ppp_deflate_state *state;
  254. int w_size;
  255. if (opt_len != CILEN_DEFLATE
  256.     || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  257.     || options[1] != CILEN_DEFLATE
  258.     || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  259.     || options[3] != DEFLATE_CHK_SEQUENCE)
  260. return NULL;
  261. w_size = DEFLATE_SIZE(options[2]);
  262. if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
  263. return NULL;
  264. state = (struct ppp_deflate_state *) kmalloc(sizeof(*state), GFP_KERNEL);
  265. if (state == NULL)
  266. return NULL;
  267. MOD_INC_USE_COUNT;
  268. memset (state, 0, sizeof (struct ppp_deflate_state));
  269. state->w_size         = w_size;
  270. state->strm.next_out  = NULL;
  271. state->strm.workspace = kmalloc(zlib_inflate_workspacesize(),
  272. GFP_KERNEL);
  273. if (state->strm.workspace == NULL)
  274. goto out_free;
  275. if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
  276. goto out_free;
  277. return (void *) state;
  278. out_free:
  279. z_decomp_free(state);
  280. return NULL;
  281. }
  282. static int
  283. z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
  284.     void *arg;
  285.     unsigned char *options;
  286.     int opt_len, unit, hdrlen, mru, debug;
  287. {
  288. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  289. if (opt_len < CILEN_DEFLATE
  290.     || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  291.     || options[1] != CILEN_DEFLATE
  292.     || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  293.     || DEFLATE_SIZE(options[2]) != state->w_size
  294.     || options[3] != DEFLATE_CHK_SEQUENCE)
  295. return 0;
  296. state->seqno = 0;
  297. state->unit  = unit;
  298. state->debug = debug;
  299. state->mru   = mru;
  300. zlib_inflateReset(&state->strm);
  301. return 1;
  302. }
  303. static void
  304. z_decomp_reset(arg)
  305.     void *arg;
  306. {
  307. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  308. state->seqno = 0;
  309. zlib_inflateReset(&state->strm);
  310. }
  311. /*
  312.  * Decompress a Deflate-compressed packet.
  313.  *
  314.  * Because of patent problems, we return DECOMP_ERROR for errors
  315.  * found by inspecting the input data and for system problems, but
  316.  * DECOMP_FATALERROR for any errors which could possibly be said to
  317.  * be being detected "after" decompression.  For DECOMP_ERROR,
  318.  * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
  319.  * infringing a patent of Motorola's if we do, so we take CCP down
  320.  * instead.
  321.  *
  322.  * Given that the frame has the correct sequence number and a good FCS,
  323.  * errors such as invalid codes in the input most likely indicate a
  324.  * bug, so we return DECOMP_FATALERROR for them in order to turn off
  325.  * compression, even though they are detected by inspecting the input.
  326.  */
  327. int
  328. z_decompress(arg, ibuf, isize, obuf, osize)
  329.     void *arg;
  330.     unsigned char *ibuf;
  331.     int isize;
  332.     unsigned char *obuf;
  333.     int osize;
  334. {
  335. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  336. int olen, seq, r;
  337. int decode_proto, overflow;
  338. unsigned char overflow_buf[1];
  339. if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
  340. if (state->debug)
  341. printk(KERN_DEBUG "z_decompress%d: short pkt (%d)n",
  342.        state->unit, isize);
  343. return DECOMP_ERROR;
  344. }
  345. /* Check the sequence number. */
  346. seq = (ibuf[PPP_HDRLEN] << 8) + ibuf[PPP_HDRLEN+1];
  347. if (seq != state->seqno) {
  348. if (state->debug)
  349. printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %dn",
  350.        state->unit, seq, state->seqno);
  351. return DECOMP_ERROR;
  352. }
  353. ++state->seqno;
  354. /*
  355.  * Fill in the first part of the PPP header.  The protocol field
  356.  * comes from the decompressed data.
  357.  */
  358. obuf[0] = PPP_ADDRESS(ibuf);
  359. obuf[1] = PPP_CONTROL(ibuf);
  360. obuf[2] = 0;
  361. /*
  362.  * Set up to call inflate.  We set avail_out to 1 initially so we can
  363.  * look at the first byte of the output and decide whether we have
  364.  * a 1-byte or 2-byte protocol field.
  365.  */
  366. state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
  367. state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
  368. state->strm.next_out = obuf + 3;
  369. state->strm.avail_out = 1;
  370. decode_proto = 1;
  371. overflow = 0;
  372. /*
  373.  * Call inflate, supplying more input or output as needed.
  374.  */
  375. for (;;) {
  376. r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
  377. if (r != Z_OK) {
  378. if (state->debug)
  379. printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)n",
  380.        state->unit, r, (state->strm.msg? state->strm.msg: ""));
  381. return DECOMP_FATALERROR;
  382. }
  383. if (state->strm.avail_out != 0)
  384. break; /* all done */
  385. if (decode_proto) {
  386. state->strm.avail_out = osize - PPP_HDRLEN;
  387. if ((obuf[3] & 1) == 0) {
  388. /* 2-byte protocol field */
  389. obuf[2] = obuf[3];
  390. --state->strm.next_out;
  391. ++state->strm.avail_out;
  392. }
  393. decode_proto = 0;
  394. } else if (!overflow) {
  395. /*
  396.  * We've filled up the output buffer; the only way to
  397.  * find out whether inflate has any more characters
  398.  * left is to give it another byte of output space.
  399.  */
  400. state->strm.next_out = overflow_buf;
  401. state->strm.avail_out = 1;
  402. overflow = 1;
  403. } else {
  404. if (state->debug)
  405. printk(KERN_DEBUG "z_decompress%d: ran out of mrun",
  406.        state->unit);
  407. return DECOMP_FATALERROR;
  408. }
  409. }
  410. if (decode_proto) {
  411. if (state->debug)
  412. printk(KERN_DEBUG "z_decompress%d: didn't get proton",
  413.        state->unit);
  414. return DECOMP_ERROR;
  415. }
  416. olen = osize + overflow - state->strm.avail_out;
  417. state->stats.unc_bytes += olen;
  418. state->stats.unc_packets++;
  419. state->stats.comp_bytes += isize;
  420. state->stats.comp_packets++;
  421. return olen;
  422. }
  423. /*
  424.  * Incompressible data has arrived - add it to the history.
  425.  */
  426. static void
  427. z_incomp(arg, ibuf, icnt)
  428.     void *arg;
  429.     unsigned char *ibuf;
  430.     int icnt;
  431. {
  432. struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
  433. int proto, r;
  434. /*
  435.  * Check that the protocol is one we handle.
  436.  */
  437. proto = PPP_PROTOCOL(ibuf);
  438. if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
  439. return;
  440. ++state->seqno;
  441. /*
  442.  * We start at the either the 1st or 2nd byte of the protocol field,
  443.  * depending on whether the protocol value is compressible.
  444.  */
  445. state->strm.next_in = ibuf + 3;
  446. state->strm.avail_in = icnt - 3;
  447. if (proto > 0xff) {
  448. --state->strm.next_in;
  449. ++state->strm.avail_in;
  450. }
  451. r = zlib_inflateIncomp(&state->strm);
  452. if (r != Z_OK) {
  453. /* gak! */
  454. if (state->debug) {
  455. printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)n",
  456.        state->unit, r, (state->strm.msg? state->strm.msg: ""));
  457. }
  458. return;
  459. }
  460. /*
  461.  * Update stats.
  462.  */
  463. state->stats.inc_bytes += icnt;
  464. state->stats.inc_packets++;
  465. state->stats.unc_bytes += icnt;
  466. state->stats.unc_packets++;
  467. }
  468. /*************************************************************
  469.  * Module interface table
  470.  *************************************************************/
  471. /* These are in ppp_generic.c */
  472. extern int  ppp_register_compressor   (struct compressor *cp);
  473. extern void ppp_unregister_compressor (struct compressor *cp);
  474. /*
  475.  * Procedures exported to if_ppp.c.
  476.  */
  477. struct compressor ppp_deflate = {
  478. CI_DEFLATE, /* compress_proto */
  479. z_comp_alloc, /* comp_alloc */
  480. z_comp_free, /* comp_free */
  481. z_comp_init, /* comp_init */
  482. z_comp_reset, /* comp_reset */
  483. z_compress, /* compress */
  484. z_comp_stats, /* comp_stat */
  485. z_decomp_alloc, /* decomp_alloc */
  486. z_decomp_free, /* decomp_free */
  487. z_decomp_init, /* decomp_init */
  488. z_decomp_reset, /* decomp_reset */
  489. z_decompress, /* decompress */
  490. z_incomp, /* incomp */
  491. z_comp_stats, /* decomp_stat */
  492. };
  493. struct compressor ppp_deflate_draft = {
  494. CI_DEFLATE_DRAFT, /* compress_proto */
  495. z_comp_alloc, /* comp_alloc */
  496. z_comp_free, /* comp_free */
  497. z_comp_init, /* comp_init */
  498. z_comp_reset, /* comp_reset */
  499. z_compress, /* compress */
  500. z_comp_stats, /* comp_stat */
  501. z_decomp_alloc, /* decomp_alloc */
  502. z_decomp_free, /* decomp_free */
  503. z_decomp_init, /* decomp_init */
  504. z_decomp_reset, /* decomp_reset */
  505. z_decompress, /* decompress */
  506. z_incomp, /* incomp */
  507. z_comp_stats, /* decomp_stat */
  508. };
  509. int __init deflate_init(void)
  510. {  
  511.         int answer = ppp_register_compressor(&ppp_deflate);
  512.         if (answer == 0)
  513.                 printk(KERN_INFO
  514.        "PPP Deflate Compression module registeredn");
  515. ppp_register_compressor(&ppp_deflate_draft);
  516.         return answer;
  517. }
  518.      
  519. void __exit deflate_cleanup(void)
  520. {
  521. ppp_unregister_compressor(&ppp_deflate);
  522. ppp_unregister_compressor(&ppp_deflate_draft);
  523. }
  524. module_init(deflate_init);
  525. module_exit(deflate_cleanup);
  526. MODULE_LICENSE("Dual BSD/GPL");