deflate.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * ppp_deflate.c - interface the zlib procedures for Deflate compression
  3. * and decompression (as used by gzip) to the PPP code.
  4. *
  5. * Copyright (c) 1994 Paul Mackerras. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The name(s) of the authors of this software must not be used to
  20. * endorse or promote products derived from this software without
  21. * prior written permission.
  22. *
  23. * 4. Redistributions of any form whatsoever must retain the following
  24. * acknowledgment:
  25. * "This product includes software developed by Paul Mackerras
  26. * <paulus@samba.org>".
  27. *
  28. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  29. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  30. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  31. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  32. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  33. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  34. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  35. *
  36. * $Id: deflate.c,v 1.5 2004/01/17 05:47:55 carlsonj Exp $
  37. */
  38. #include <sys/types.h>
  39. #include <stdio.h>
  40. #include <stddef.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "ppp_defs.h"
  44. #include "ppp-comp.h"
  45. #include "zlib.h"
  46. #if DO_DEFLATE
  47. #define DEFLATE_DEBUG 1
  48. /*
  49. * State for a Deflate (de)compressor.
  50. */
  51. struct deflate_state {
  52. int seqno;
  53. int w_size;
  54. int unit;
  55. int hdrlen;
  56. int mru;
  57. int debug;
  58. z_stream strm;
  59. struct compstat stats;
  60. };
  61. #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
  62. static void *z_alloc __P((void *, u_int items, u_int size));
  63. static void z_free __P((void *, void *ptr, u_int nb));
  64. static void *z_decomp_alloc __P((u_char *options, int opt_len));
  65. static void z_decomp_free __P((void *state));
  66. static int z_decomp_init __P((void *state, u_char *options, int opt_len,
  67. int unit, int hdrlen, int mru, int debug));
  68. static void z_incomp __P((void *state, u_char *dmsg, int len));
  69. static int z_decompress __P((void *state, u_char *cmp, int inlen,
  70. u_char *dmp, int *outlenp));
  71. static void z_decomp_reset __P((void *state));
  72. static void z_comp_stats __P((void *state, struct compstat *stats));
  73. /*
  74. * Procedures exported to if_ppp.c.
  75. */
  76. struct compressor ppp_deflate = {
  77. CI_DEFLATE, /* compress_proto */
  78. z_decomp_alloc, /* decomp_alloc */
  79. z_decomp_free, /* decomp_free */
  80. z_decomp_init, /* decomp_init */
  81. z_decomp_reset, /* decomp_reset */
  82. z_decompress, /* decompress */
  83. z_incomp, /* incomp */
  84. z_comp_stats, /* decomp_stat */
  85. };
  86. /*
  87. * Space allocation and freeing routines for use by zlib routines.
  88. */
  89. static void *
  90. z_alloc(notused, items, size)
  91. void *notused;
  92. u_int items, size;
  93. {
  94. return malloc(items * size);
  95. }
  96. static void
  97. z_free(notused, ptr, nbytes)
  98. void *notused;
  99. void *ptr;
  100. u_int nbytes;
  101. {
  102. free(ptr);
  103. }
  104. static void
  105. z_comp_stats(arg, stats)
  106. void *arg;
  107. struct compstat *stats;
  108. {
  109. struct deflate_state *state = (struct deflate_state *) arg;
  110. u_int out;
  111. *stats = state->stats;
  112. stats->ratio = stats->unc_bytes;
  113. out = stats->comp_bytes + stats->unc_bytes;
  114. if (stats->ratio <= 0x7ffffff)
  115. stats->ratio <<= 8;
  116. else
  117. out >>= 8;
  118. if (out != 0)
  119. stats->ratio /= out;
  120. }
  121. /*
  122. * Allocate space for a decompressor.
  123. */
  124. static void *
  125. z_decomp_alloc(options, opt_len)
  126. u_char *options;
  127. int opt_len;
  128. {
  129. struct deflate_state *state;
  130. int w_size;
  131. if (opt_len != CILEN_DEFLATE || options[0] != CI_DEFLATE
  132. || options[1] != CILEN_DEFLATE
  133. || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  134. || options[3] != DEFLATE_CHK_SEQUENCE)
  135. return NULL;
  136. w_size = DEFLATE_SIZE(options[2]);
  137. if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
  138. return NULL;
  139. state = (struct deflate_state *) malloc(sizeof(*state));
  140. if (state == NULL)
  141. return NULL;
  142. state->strm.next_out = NULL;
  143. state->strm.zalloc = (alloc_func) z_alloc;
  144. state->strm.zfree = (free_func) z_free;
  145. if (inflateInit2(&state->strm, -w_size) != Z_OK) {
  146. free(state);
  147. return NULL;
  148. }
  149. state->w_size = w_size;
  150. memset(&state->stats, 0, sizeof(state->stats));
  151. return (void *) state;
  152. }
  153. static void
  154. z_decomp_free(arg)
  155. void *arg;
  156. {
  157. struct deflate_state *state = (struct deflate_state *) arg;
  158. inflateEnd(&state->strm);
  159. free(state);
  160. }
  161. static int
  162. z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
  163. void *arg;
  164. u_char *options;
  165. int opt_len, unit, hdrlen, mru, debug;
  166. {
  167. struct deflate_state *state = (struct deflate_state *) arg;
  168. if (opt_len < CILEN_DEFLATE || options[0] != CI_DEFLATE
  169. || options[1] != CILEN_DEFLATE
  170. || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
  171. || DEFLATE_SIZE(options[2]) != state->w_size
  172. || options[3] != DEFLATE_CHK_SEQUENCE)
  173. return 0;
  174. state->seqno = 0;
  175. state->unit = unit;
  176. state->hdrlen = hdrlen;
  177. state->debug = debug;
  178. state->mru = mru;
  179. inflateReset(&state->strm);
  180. return 1;
  181. }
  182. static void
  183. z_decomp_reset(arg)
  184. void *arg;
  185. {
  186. struct deflate_state *state = (struct deflate_state *) arg;
  187. state->seqno = 0;
  188. inflateReset(&state->strm);
  189. }
  190. /*
  191. * Decompress a Deflate-compressed packet.
  192. *
  193. * Because of patent problems, we return DECOMP_ERROR for errors
  194. * found by inspecting the input data and for system problems, but
  195. * DECOMP_FATALERROR for any errors which could possibly be said to
  196. * be being detected "after" decompression. For DECOMP_ERROR,
  197. * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
  198. * infringing a patent of Motorola's if we do, so we take CCP down
  199. * instead.
  200. *
  201. * Given that the frame has the correct sequence number and a good FCS,
  202. * errors such as invalid codes in the input most likely indicate a
  203. * bug, so we return DECOMP_FATALERROR for them in order to turn off
  204. * compression, even though they are detected by inspecting the input.
  205. */
  206. static int
  207. z_decompress(arg, mi, inlen, mo, outlenp)
  208. void *arg;
  209. u_char *mi, *mo;
  210. int inlen, *outlenp;
  211. {
  212. struct deflate_state *state = (struct deflate_state *) arg;
  213. u_char *rptr, *wptr;
  214. int rlen, olen;
  215. int seq, r;
  216. rptr = mi;
  217. if (*rptr == 0)
  218. ++rptr;
  219. ++rptr;
  220. /* Check the sequence number. */
  221. seq = (rptr[0] << 8) + rptr[1];
  222. rptr += 2;
  223. if (seq != state->seqno) {
  224. #if !DEFLATE_DEBUG
  225. if (state->debug)
  226. #endif
  227. printf("z_decompress%d: bad seq # %d, expected %d\n",
  228. state->unit, seq, state->seqno);
  229. return DECOMP_ERROR;
  230. }
  231. ++state->seqno;
  232. /*
  233. * Set up to call inflate.
  234. */
  235. wptr = mo;
  236. state->strm.next_in = rptr;
  237. state->strm.avail_in = mi + inlen - rptr;
  238. rlen = state->strm.avail_in + PPP_HDRLEN + DEFLATE_OVHD;
  239. state->strm.next_out = wptr;
  240. state->strm.avail_out = state->mru + 2;
  241. r = inflate(&state->strm, Z_PACKET_FLUSH);
  242. if (r != Z_OK) {
  243. #if !DEFLATE_DEBUG
  244. if (state->debug)
  245. #endif
  246. printf("z_decompress%d: inflate returned %d (%s)\n",
  247. state->unit, r, (state->strm.msg? state->strm.msg: ""));
  248. return DECOMP_FATALERROR;
  249. }
  250. olen = state->mru + 2 - state->strm.avail_out;
  251. *outlenp = olen;
  252. if ((wptr[0] & 1) != 0)
  253. ++olen; /* for suppressed protocol high byte */
  254. olen += 2; /* for address, control */
  255. #if DEFLATE_DEBUG
  256. if (olen > state->mru + PPP_HDRLEN)
  257. printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
  258. state->unit, olen, state->mru + PPP_HDRLEN);
  259. #endif
  260. state->stats.unc_bytes += olen;
  261. state->stats.unc_packets++;
  262. state->stats.comp_bytes += rlen;
  263. state->stats.comp_packets++;
  264. return DECOMP_OK;
  265. }
  266. /*
  267. * Incompressible data has arrived - add it to the history.
  268. */
  269. static void
  270. z_incomp(arg, mi, mlen)
  271. void *arg;
  272. u_char *mi;
  273. int mlen;
  274. {
  275. struct deflate_state *state = (struct deflate_state *) arg;
  276. u_char *rptr;
  277. int rlen, proto, r;
  278. /*
  279. * Check that the protocol is one we handle.
  280. */
  281. rptr = mi;
  282. proto = rptr[0];
  283. if ((proto & 1) == 0)
  284. proto = (proto << 8) + rptr[1];
  285. if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
  286. return;
  287. ++state->seqno;
  288. if (rptr[0] == 0)
  289. ++rptr;
  290. rlen = mi + mlen - rptr;
  291. state->strm.next_in = rptr;
  292. state->strm.avail_in = rlen;
  293. r = inflateIncomp(&state->strm);
  294. if (r != Z_OK) {
  295. /* gak! */
  296. #if !DEFLATE_DEBUG
  297. if (state->debug)
  298. #endif
  299. printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
  300. state->unit, r, (state->strm.msg? state->strm.msg: ""));
  301. return;
  302. }
  303. /*
  304. * Update stats.
  305. */
  306. if (proto <= 0xff)
  307. ++rlen;
  308. rlen += 2;
  309. state->stats.inc_bytes += rlen;
  310. state->stats.inc_packets++;
  311. state->stats.unc_bytes += rlen;
  312. state->stats.unc_packets++;
  313. }
  314. #endif /* DO_DEFLATE */