deflate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * ppp_deflate.c - interface the zlib procedures for Deflate compression
  3. * and decompression (as used by gzip) to the PPP code.
  4. * This version is for use with STREAMS under SunOS 4.x, Solaris 2,
  5. * SVR4, OSF/1 and AIX 4.x.
  6. *
  7. * Copyright (c) 1994 Paul Mackerras. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. The name(s) of the authors of this software must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission.
  24. *
  25. * 4. Redistributions of any form whatsoever must retain the following
  26. * acknowledgment:
  27. * "This product includes software developed by Paul Mackerras
  28. * <paulus@samba.org>".
  29. *
  30. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  31. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  32. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  33. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  34. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  35. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  36. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  37. *
  38. * $Id: deflate.c,v 1.12 2004/01/17 05:47:55 carlsonj Exp $
  39. */
  40. #ifdef AIX4
  41. #include <net/net_globals.h>
  42. #endif
  43. #include <sys/param.h>
  44. #include <sys/types.h>
  45. #include <sys/stream.h>
  46. #include <net/ppp_defs.h>
  47. #include "ppp_mod.h"
  48. #define PACKETPTR mblk_t *
  49. #include <net/ppp-comp.h>
  50. #ifdef __osf__
  51. #include "zlib.h"
  52. #else
  53. #include "../common/zlib.h"
  54. #endif
  55. #ifdef SOL2
  56. #include <sys/sunddi.h>
  57. #endif
  58. #if DO_DEFLATE
  59. #define DEFLATE_DEBUG 1
  60. /*
  61. * State for a Deflate (de)compressor.
  62. */
  63. struct deflate_state {
  64. int seqno;
  65. int w_size;
  66. int unit;
  67. int hdrlen;
  68. int mru;
  69. int debug;
  70. z_stream strm;
  71. struct compstat stats;
  72. };
  73. #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
  74. static void *z_alloc __P((void *, u_int items, u_int size));
  75. static void *z_alloc_init __P((void *, u_int items, u_int size));
  76. static void z_free __P((void *, void *ptr));
  77. static void *z_comp_alloc __P((u_char *options, int opt_len));
  78. static void *z_decomp_alloc __P((u_char *options, int opt_len));
  79. static void z_comp_free __P((void *state));
  80. static void z_decomp_free __P((void *state));
  81. static int z_comp_init __P((void *state, u_char *options, int opt_len,
  82. int unit, int hdrlen, int debug));
  83. static int z_decomp_init __P((void *state, u_char *options, int opt_len,
  84. int unit, int hdrlen, int mru, int debug));
  85. static int z_compress __P((void *state, mblk_t **mret,
  86. mblk_t *mp, int slen, int maxolen));
  87. static void z_incomp __P((void *state, mblk_t *dmsg));
  88. static int z_decompress __P((void *state, mblk_t *cmp,
  89. mblk_t **dmpp));
  90. static void z_comp_reset __P((void *state));
  91. static void z_decomp_reset __P((void *state));
  92. static void z_comp_stats __P((void *state, struct compstat *stats));
  93. /*
  94. * Procedures exported to ppp_comp.c.
  95. */
  96. struct compressor ppp_deflate = {
  97. CI_DEFLATE, /* compress_proto */
  98. z_comp_alloc, /* comp_alloc */
  99. z_comp_free, /* comp_free */
  100. z_comp_init, /* comp_init */
  101. z_comp_reset, /* comp_reset */
  102. z_compress, /* compress */
  103. z_comp_stats, /* comp_stat */
  104. z_decomp_alloc, /* decomp_alloc */
  105. z_decomp_free, /* decomp_free */
  106. z_decomp_init, /* decomp_init */
  107. z_decomp_reset, /* decomp_reset */
  108. z_decompress, /* decompress */
  109. z_incomp, /* incomp */
  110. z_comp_stats, /* decomp_stat */
  111. };
  112. struct compressor ppp_deflate_draft = {
  113. CI_DEFLATE_DRAFT, /* compress_proto */
  114. z_comp_alloc, /* comp_alloc */
  115. z_comp_free, /* comp_free */
  116. z_comp_init, /* comp_init */
  117. z_comp_reset, /* comp_reset */
  118. z_compress, /* compress */
  119. z_comp_stats, /* comp_stat */
  120. z_decomp_alloc, /* decomp_alloc */
  121. z_decomp_free, /* decomp_free */
  122. z_decomp_init, /* decomp_init */
  123. z_decomp_reset, /* decomp_reset */
  124. z_decompress, /* decompress */
  125. z_incomp, /* incomp */
  126. z_comp_stats, /* decomp_stat */
  127. };
  128. #define DECOMP_CHUNK 512
  129. /*
  130. * Space allocation and freeing routines for use by zlib routines.
  131. */
  132. struct zchunk {
  133. u_int size;
  134. u_int guard;
  135. };
  136. #define GUARD_MAGIC 0x77a6011a
  137. static void *
  138. z_alloc_init(notused, items, size)
  139. void *notused;
  140. u_int items, size;
  141. {
  142. struct zchunk *z;
  143. size = items * size + sizeof(struct zchunk);
  144. #ifdef __osf__
  145. z = (struct zchunk *) ALLOC_SLEEP(size);
  146. #else
  147. z = (struct zchunk *) ALLOC_NOSLEEP(size);
  148. #endif
  149. z->size = size;
  150. z->guard = GUARD_MAGIC;
  151. return (void *) (z + 1);
  152. }
  153. static void *
  154. z_alloc(notused, items, size)
  155. void *notused;
  156. u_int items, size;
  157. {
  158. struct zchunk *z;
  159. size = items * size + sizeof(struct zchunk);
  160. z = (struct zchunk *) ALLOC_NOSLEEP(size);
  161. z->size = size;
  162. z->guard = GUARD_MAGIC;
  163. return (void *) (z + 1);
  164. }
  165. static void
  166. z_free(notused, ptr)
  167. void *notused;
  168. void *ptr;
  169. {
  170. struct zchunk *z = ((struct zchunk *) ptr) - 1;
  171. if (z->guard != GUARD_MAGIC) {
  172. printf("ppp: z_free of corrupted chunk at %x (%x, %x)\n",
  173. z, z->size, z->guard);
  174. return;
  175. }
  176. FREE(z, z->size);
  177. }
  178. /*
  179. * Allocate space for a compressor.
  180. */
  181. static void *
  182. z_comp_alloc(options, opt_len)
  183. u_char *options;
  184. int opt_len;
  185. {
  186. struct deflate_state *state;
  187. int w_size;
  188. if (opt_len != CILEN_DEFLATE
  189. || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  190. || options[1] != CILEN_DEFLATE
  191. || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  192. || options[3] != DEFLATE_CHK_SEQUENCE)
  193. return NULL;
  194. w_size = DEFLATE_SIZE(options[2]);
  195. /*
  196. * N.B. the 9 below should be DEFLATE_MIN_SIZE (8), but using
  197. * 8 will cause kernel crashes because of a bug in zlib.
  198. */
  199. if (w_size < 9 || w_size > DEFLATE_MAX_SIZE)
  200. return NULL;
  201. #ifdef __osf__
  202. state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
  203. #else
  204. state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
  205. #endif
  206. if (state == NULL)
  207. return NULL;
  208. state->strm.next_in = NULL;
  209. state->strm.zalloc = (alloc_func) z_alloc_init;
  210. state->strm.zfree = (free_func) z_free;
  211. if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
  212. -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  213. FREE(state, sizeof(*state));
  214. return NULL;
  215. }
  216. state->strm.zalloc = (alloc_func) z_alloc;
  217. state->w_size = w_size;
  218. bzero(&state->stats, sizeof(state->stats));
  219. return (void *) state;
  220. }
  221. static void
  222. z_comp_free(arg)
  223. void *arg;
  224. {
  225. struct deflate_state *state = (struct deflate_state *) arg;
  226. deflateEnd(&state->strm);
  227. FREE(state, sizeof(*state));
  228. }
  229. static int
  230. z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
  231. void *arg;
  232. u_char *options;
  233. int opt_len, unit, hdrlen, debug;
  234. {
  235. struct deflate_state *state = (struct deflate_state *) arg;
  236. if (opt_len < CILEN_DEFLATE
  237. || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  238. || options[1] != CILEN_DEFLATE
  239. || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  240. || DEFLATE_SIZE(options[2]) != state->w_size
  241. || options[3] != DEFLATE_CHK_SEQUENCE)
  242. return 0;
  243. state->seqno = 0;
  244. state->unit = unit;
  245. state->hdrlen = hdrlen;
  246. state->debug = debug;
  247. deflateReset(&state->strm);
  248. return 1;
  249. }
  250. static void
  251. z_comp_reset(arg)
  252. void *arg;
  253. {
  254. struct deflate_state *state = (struct deflate_state *) arg;
  255. state->seqno = 0;
  256. deflateReset(&state->strm);
  257. }
  258. static int
  259. z_compress(arg, mret, mp, orig_len, maxolen)
  260. void *arg;
  261. mblk_t **mret; /* compressed packet (out) */
  262. mblk_t *mp; /* uncompressed packet (in) */
  263. int orig_len, maxolen;
  264. {
  265. struct deflate_state *state = (struct deflate_state *) arg;
  266. u_char *rptr, *wptr;
  267. int proto, olen, wspace, r, flush;
  268. mblk_t *m;
  269. /*
  270. * Check that the protocol is in the range we handle.
  271. */
  272. *mret = NULL;
  273. rptr = mp->b_rptr;
  274. if (rptr + PPP_HDRLEN > mp->b_wptr) {
  275. if (!pullupmsg(mp, PPP_HDRLEN))
  276. return 0;
  277. rptr = mp->b_rptr;
  278. }
  279. proto = PPP_PROTOCOL(rptr);
  280. if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
  281. return orig_len;
  282. /* Allocate one mblk initially. */
  283. if (maxolen > orig_len)
  284. maxolen = orig_len;
  285. if (maxolen <= PPP_HDRLEN + 2) {
  286. wspace = 0;
  287. m = NULL;
  288. } else {
  289. wspace = maxolen + state->hdrlen;
  290. if (wspace > 4096)
  291. wspace = 4096;
  292. m = allocb(wspace, BPRI_MED);
  293. }
  294. if (m != NULL) {
  295. *mret = m;
  296. if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
  297. m->b_rptr += state->hdrlen;
  298. m->b_wptr = m->b_rptr;
  299. wspace -= state->hdrlen;
  300. }
  301. wptr = m->b_wptr;
  302. /*
  303. * Copy over the PPP header and store the 2-byte sequence number.
  304. */
  305. wptr[0] = PPP_ADDRESS(rptr);
  306. wptr[1] = PPP_CONTROL(rptr);
  307. wptr[2] = PPP_COMP >> 8;
  308. wptr[3] = PPP_COMP;
  309. wptr += PPP_HDRLEN;
  310. wptr[0] = state->seqno >> 8;
  311. wptr[1] = state->seqno;
  312. wptr += 2;
  313. state->strm.next_out = wptr;
  314. state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
  315. } else {
  316. state->strm.next_out = NULL;
  317. state->strm.avail_out = 1000000;
  318. }
  319. ++state->seqno;
  320. rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
  321. state->strm.next_in = rptr;
  322. state->strm.avail_in = mp->b_wptr - rptr;
  323. mp = mp->b_cont;
  324. flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
  325. olen = 0;
  326. for (;;) {
  327. r = deflate(&state->strm, flush);
  328. if (r != Z_OK) {
  329. printf("z_compress: deflate returned %d (%s)\n",
  330. r, (state->strm.msg? state->strm.msg: ""));
  331. break;
  332. }
  333. if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
  334. break; /* all done */
  335. if (state->strm.avail_in == 0 && mp != NULL) {
  336. state->strm.next_in = mp->b_rptr;
  337. state->strm.avail_in = mp->b_wptr - mp->b_rptr;
  338. mp = mp->b_cont;
  339. if (mp == NULL)
  340. flush = Z_PACKET_FLUSH;
  341. }
  342. if (state->strm.avail_out == 0) {
  343. if (m != NULL) {
  344. m->b_wptr += wspace;
  345. olen += wspace;
  346. wspace = maxolen - olen;
  347. if (wspace <= 0) {
  348. wspace = 0;
  349. m->b_cont = NULL;
  350. } else {
  351. if (wspace < 32)
  352. wspace = 32;
  353. else if (wspace > 4096)
  354. wspace = 4096;
  355. m->b_cont = allocb(wspace, BPRI_MED);
  356. }
  357. m = m->b_cont;
  358. if (m != NULL) {
  359. state->strm.next_out = m->b_wptr;
  360. state->strm.avail_out = wspace;
  361. }
  362. }
  363. if (m == NULL) {
  364. state->strm.next_out = NULL;
  365. state->strm.avail_out = 1000000;
  366. }
  367. }
  368. }
  369. if (m != NULL) {
  370. m->b_wptr += wspace - state->strm.avail_out;
  371. olen += wspace - state->strm.avail_out;
  372. }
  373. /*
  374. * See if we managed to reduce the size of the packet.
  375. */
  376. if (olen < orig_len && m != NULL) {
  377. state->stats.comp_bytes += olen;
  378. state->stats.comp_packets++;
  379. } else {
  380. if (*mret != NULL) {
  381. freemsg(*mret);
  382. *mret = NULL;
  383. }
  384. state->stats.inc_bytes += orig_len;
  385. state->stats.inc_packets++;
  386. olen = orig_len;
  387. }
  388. state->stats.unc_bytes += orig_len;
  389. state->stats.unc_packets++;
  390. return olen;
  391. }
  392. static void
  393. z_comp_stats(arg, stats)
  394. void *arg;
  395. struct compstat *stats;
  396. {
  397. struct deflate_state *state = (struct deflate_state *) arg;
  398. u_int out;
  399. *stats = state->stats;
  400. stats->ratio = stats->unc_bytes;
  401. out = stats->comp_bytes + stats->unc_bytes;
  402. if (stats->ratio <= 0x7ffffff)
  403. stats->ratio <<= 8;
  404. else
  405. out >>= 8;
  406. if (out != 0)
  407. stats->ratio /= out;
  408. }
  409. /*
  410. * Allocate space for a decompressor.
  411. */
  412. static void *
  413. z_decomp_alloc(options, opt_len)
  414. u_char *options;
  415. int opt_len;
  416. {
  417. struct deflate_state *state;
  418. int w_size;
  419. if (opt_len != CILEN_DEFLATE
  420. || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  421. || options[1] != CILEN_DEFLATE
  422. || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  423. || options[3] != DEFLATE_CHK_SEQUENCE)
  424. return NULL;
  425. w_size = DEFLATE_SIZE(options[2]);
  426. /*
  427. * N.B. the 9 below should be DEFLATE_MIN_SIZE (8), but using
  428. * 8 will cause kernel crashes because of a bug in zlib.
  429. */
  430. if (w_size < 9 || w_size > DEFLATE_MAX_SIZE)
  431. return NULL;
  432. #ifdef __osf__
  433. state = (struct deflate_state *) ALLOC_SLEEP(sizeof(*state));
  434. #else
  435. state = (struct deflate_state *) ALLOC_NOSLEEP(sizeof(*state));
  436. #endif
  437. if (state == NULL)
  438. return NULL;
  439. state->strm.next_out = NULL;
  440. state->strm.zalloc = (alloc_func) z_alloc_init;
  441. state->strm.zfree = (free_func) z_free;
  442. if (inflateInit2(&state->strm, -w_size) != Z_OK) {
  443. FREE(state, sizeof(*state));
  444. return NULL;
  445. }
  446. state->strm.zalloc = (alloc_func) z_alloc;
  447. state->w_size = w_size;
  448. bzero(&state->stats, sizeof(state->stats));
  449. return (void *) state;
  450. }
  451. static void
  452. z_decomp_free(arg)
  453. void *arg;
  454. {
  455. struct deflate_state *state = (struct deflate_state *) arg;
  456. inflateEnd(&state->strm);
  457. FREE(state, sizeof(*state));
  458. }
  459. static int
  460. z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
  461. void *arg;
  462. u_char *options;
  463. int opt_len, unit, hdrlen, mru, debug;
  464. {
  465. struct deflate_state *state = (struct deflate_state *) arg;
  466. if (opt_len < CILEN_DEFLATE
  467. || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
  468. || options[1] != CILEN_DEFLATE
  469. || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  470. || DEFLATE_SIZE(options[2]) != state->w_size
  471. || options[3] != DEFLATE_CHK_SEQUENCE)
  472. return 0;
  473. state->seqno = 0;
  474. state->unit = unit;
  475. state->hdrlen = hdrlen;
  476. state->debug = debug;
  477. state->mru = mru;
  478. inflateReset(&state->strm);
  479. return 1;
  480. }
  481. static void
  482. z_decomp_reset(arg)
  483. void *arg;
  484. {
  485. struct deflate_state *state = (struct deflate_state *) arg;
  486. state->seqno = 0;
  487. inflateReset(&state->strm);
  488. }
  489. /*
  490. * Decompress a Deflate-compressed packet.
  491. *
  492. * Because of patent problems, we return DECOMP_ERROR for errors
  493. * found by inspecting the input data and for system problems, but
  494. * DECOMP_FATALERROR for any errors which could possibly be said to
  495. * be being detected "after" decompression. For DECOMP_ERROR,
  496. * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
  497. * infringing a patent of Motorola's if we do, so we take CCP down
  498. * instead.
  499. *
  500. * Given that the frame has the correct sequence number and a good FCS,
  501. * errors such as invalid codes in the input most likely indicate a
  502. * bug, so we return DECOMP_FATALERROR for them in order to turn off
  503. * compression, even though they are detected by inspecting the input.
  504. */
  505. static int
  506. z_decompress(arg, mi, mop)
  507. void *arg;
  508. mblk_t *mi, **mop;
  509. {
  510. struct deflate_state *state = (struct deflate_state *) arg;
  511. mblk_t *mo, *mo_head;
  512. u_char *rptr, *wptr;
  513. int rlen, olen, ospace;
  514. int seq, i, flush, r, decode_proto;
  515. u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
  516. *mop = NULL;
  517. rptr = mi->b_rptr;
  518. for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
  519. while (rptr >= mi->b_wptr) {
  520. mi = mi->b_cont;
  521. if (mi == NULL)
  522. return DECOMP_ERROR;
  523. rptr = mi->b_rptr;
  524. }
  525. hdr[i] = *rptr++;
  526. }
  527. /* Check the sequence number. */
  528. seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
  529. if (seq != state->seqno) {
  530. #if !DEFLATE_DEBUG
  531. if (state->debug)
  532. #endif
  533. printf("z_decompress%d: bad seq # %d, expected %d\n",
  534. state->unit, seq, state->seqno);
  535. return DECOMP_ERROR;
  536. }
  537. ++state->seqno;
  538. /* Allocate an output message block. */
  539. mo = allocb(DECOMP_CHUNK + state->hdrlen, BPRI_MED);
  540. if (mo == NULL)
  541. return DECOMP_ERROR;
  542. mo_head = mo;
  543. mo->b_cont = NULL;
  544. mo->b_rptr += state->hdrlen;
  545. mo->b_wptr = wptr = mo->b_rptr;
  546. ospace = DECOMP_CHUNK;
  547. olen = 0;
  548. /*
  549. * Fill in the first part of the PPP header. The protocol field
  550. * comes from the decompressed data.
  551. */
  552. wptr[0] = PPP_ADDRESS(hdr);
  553. wptr[1] = PPP_CONTROL(hdr);
  554. wptr[2] = 0;
  555. /*
  556. * Set up to call inflate. We set avail_out to 1 initially so we can
  557. * look at the first byte of the output and decide whether we have
  558. * a 1-byte or 2-byte protocol field.
  559. */
  560. state->strm.next_in = rptr;
  561. state->strm.avail_in = mi->b_wptr - rptr;
  562. mi = mi->b_cont;
  563. flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
  564. rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
  565. state->strm.next_out = wptr + 3;
  566. state->strm.avail_out = 1;
  567. decode_proto = 1;
  568. /*
  569. * Call inflate, supplying more input or output as needed.
  570. */
  571. for (;;) {
  572. r = inflate(&state->strm, flush);
  573. if (r != Z_OK) {
  574. #if !DEFLATE_DEBUG
  575. if (state->debug)
  576. #endif
  577. printf("z_decompress%d: inflate returned %d (%s)\n",
  578. state->unit, r, (state->strm.msg? state->strm.msg: ""));
  579. freemsg(mo_head);
  580. return DECOMP_FATALERROR;
  581. }
  582. if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
  583. break; /* all done */
  584. if (state->strm.avail_in == 0 && mi != NULL) {
  585. state->strm.next_in = mi->b_rptr;
  586. state->strm.avail_in = mi->b_wptr - mi->b_rptr;
  587. rlen += state->strm.avail_in;
  588. mi = mi->b_cont;
  589. if (mi == NULL)
  590. flush = Z_PACKET_FLUSH;
  591. }
  592. if (state->strm.avail_out == 0) {
  593. if (decode_proto) {
  594. state->strm.avail_out = ospace - PPP_HDRLEN;
  595. if ((wptr[3] & 1) == 0) {
  596. /* 2-byte protocol field */
  597. wptr[2] = wptr[3];
  598. --state->strm.next_out;
  599. ++state->strm.avail_out;
  600. }
  601. decode_proto = 0;
  602. } else {
  603. mo->b_wptr += ospace;
  604. olen += ospace;
  605. mo->b_cont = allocb(DECOMP_CHUNK, BPRI_MED);
  606. mo = mo->b_cont;
  607. if (mo == NULL) {
  608. freemsg(mo_head);
  609. return DECOMP_ERROR;
  610. }
  611. state->strm.next_out = mo->b_rptr;
  612. state->strm.avail_out = ospace = DECOMP_CHUNK;
  613. }
  614. }
  615. }
  616. if (decode_proto) {
  617. freemsg(mo_head);
  618. return DECOMP_ERROR;
  619. }
  620. mo->b_wptr += ospace - state->strm.avail_out;
  621. olen += ospace - state->strm.avail_out;
  622. #if DEFLATE_DEBUG
  623. if (olen > state->mru + PPP_HDRLEN)
  624. printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
  625. state->unit, olen, state->mru + PPP_HDRLEN);
  626. #endif
  627. state->stats.unc_bytes += olen;
  628. state->stats.unc_packets++;
  629. state->stats.comp_bytes += rlen;
  630. state->stats.comp_packets++;
  631. *mop = mo_head;
  632. return DECOMP_OK;
  633. }
  634. /*
  635. * Incompressible data has arrived - add it to the history.
  636. */
  637. static void
  638. z_incomp(arg, mi)
  639. void *arg;
  640. mblk_t *mi;
  641. {
  642. struct deflate_state *state = (struct deflate_state *) arg;
  643. u_char *rptr;
  644. int rlen, proto, r;
  645. /*
  646. * Check that the protocol is one we handle.
  647. */
  648. rptr = mi->b_rptr;
  649. if (rptr + PPP_HDRLEN > mi->b_wptr) {
  650. if (!pullupmsg(mi, PPP_HDRLEN))
  651. return;
  652. rptr = mi->b_rptr;
  653. }
  654. proto = PPP_PROTOCOL(rptr);
  655. if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
  656. return;
  657. ++state->seqno;
  658. /*
  659. * Iterate through the message blocks, adding the characters in them
  660. * to the decompressor's history. For the first block, we start
  661. * at the either the 1st or 2nd byte of the protocol field,
  662. * depending on whether the protocol value is compressible.
  663. */
  664. rlen = mi->b_wptr - mi->b_rptr;
  665. state->strm.next_in = rptr + 3;
  666. state->strm.avail_in = rlen - 3;
  667. if (proto > 0xff) {
  668. --state->strm.next_in;
  669. ++state->strm.avail_in;
  670. }
  671. for (;;) {
  672. r = inflateIncomp(&state->strm);
  673. if (r != Z_OK) {
  674. /* gak! */
  675. #if !DEFLATE_DEBUG
  676. if (state->debug)
  677. #endif
  678. printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
  679. state->unit, r, (state->strm.msg? state->strm.msg: ""));
  680. return;
  681. }
  682. mi = mi->b_cont;
  683. if (mi == NULL)
  684. break;
  685. state->strm.next_in = mi->b_rptr;
  686. state->strm.avail_in = mi->b_wptr - mi->b_rptr;
  687. rlen += state->strm.avail_in;
  688. }
  689. /*
  690. * Update stats.
  691. */
  692. state->stats.inc_bytes += rlen;
  693. state->stats.inc_packets++;
  694. state->stats.unc_bytes += rlen;
  695. state->stats.unc_packets++;
  696. }
  697. #endif /* DO_DEFLATE */