bsd_comp.c 29 KB

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