mppcc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*-
  2. * Copyright (c) 2002-2004 Jan Dubiec <jdx@slackware.pl>
  3. * Copyright (c) 2007 Alexander Motin <mav@freebsd.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice unmodified, this list of conditions, and the following
  11. * disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * MPPC decompression library.
  30. * Version 1.0
  31. *
  32. * Note that Hi/Fn (later acquired by Exar Corporation) held US patents
  33. * on some implementation-critical aspects of MPPC compression.
  34. * These patents lapsed due to non-payment of fees in 2007 and by 2015
  35. * expired altogether.
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <net/mppc.h>
  40. #define MPPE_HIST_LEN 8192
  41. #define HASH(x) (((40543*(((((x)[0]<<4)^(x)[1])<<4)^(x)[2]))>>4) & 0x1fff)
  42. struct MPPC_comp_state {
  43. uint8_t hist[2*MPPE_HIST_LEN];
  44. uint16_t histptr;
  45. uint16_t hash[MPPE_HIST_LEN];
  46. };
  47. /* Inserts 1 to 8 bits into the output buffer. */
  48. static void __inline
  49. putbits8(uint8_t *buf, uint32_t val, const uint32_t n, uint32_t *i, uint32_t *l)
  50. {
  51. buf += *i;
  52. if (*l >= n) {
  53. *l = (*l) - n;
  54. val <<= *l;
  55. *buf = *buf | (val & 0xff);
  56. if (*l == 0) {
  57. *l = 8;
  58. (*i)++;
  59. *(++buf) = 0;
  60. }
  61. } else {
  62. (*i)++;
  63. *l = 8 - n + (*l);
  64. val <<= *l;
  65. *buf = *buf | ((val >> 8) & 0xff);
  66. *(++buf) = val & 0xff;
  67. }
  68. }
  69. /* Inserts 9 to 16 bits into the output buffer. */
  70. static void __inline
  71. putbits16(uint8_t *buf, uint32_t val, const uint32_t n, uint32_t *i, uint32_t *l)
  72. {
  73. buf += *i;
  74. if (*l >= n - 8) {
  75. (*i)++;
  76. *l = 8 - n + (*l);
  77. val <<= *l;
  78. *buf = *buf | ((val >> 8) & 0xff);
  79. *(++buf) = val & 0xff;
  80. if (*l == 0) {
  81. *l = 8;
  82. (*i)++;
  83. *(++buf) = 0;
  84. }
  85. } else {
  86. (*i)++; (*i)++;
  87. *l = 16 - n + (*l);
  88. val <<= *l;
  89. *buf = *buf | ((val >> 16) & 0xff);
  90. *(++buf) = (val >> 8) & 0xff;
  91. *(++buf) = val & 0xff;
  92. }
  93. }
  94. /* Inserts 17 to 24 bits into the output buffer. */
  95. static void __inline
  96. putbits24(uint8_t *buf, uint32_t val, const uint32_t n, uint32_t *i, uint32_t *l)
  97. {
  98. buf += *i;
  99. if (*l >= n - 16) {
  100. (*i)++; (*i)++;
  101. *l = 16 - n + (*l);
  102. val <<= *l;
  103. *buf = *buf | ((val >> 16) & 0xff);
  104. *(++buf) = (val >> 8) & 0xff;
  105. *(++buf) = val & 0xff;
  106. if (*l == 0) {
  107. *l = 8;
  108. (*i)++;
  109. *(++buf) = 0;
  110. }
  111. } else {
  112. (*i)++; (*i)++; (*i)++;
  113. *l = 24 - n + (*l);
  114. val <<= *l;
  115. *buf = *buf | ((val >> 24) & 0xff);
  116. *(++buf) = (val >> 16) & 0xff;
  117. *(++buf) = (val >> 8) & 0xff;
  118. *(++buf) = val & 0xff;
  119. }
  120. }
  121. size_t MPPC_SizeOfCompressionHistory(void)
  122. {
  123. return (sizeof(struct MPPC_comp_state));
  124. }
  125. void MPPC_InitCompressionHistory(char *history)
  126. {
  127. struct MPPC_comp_state *state = (struct MPPC_comp_state*)history;
  128. bzero(history, sizeof(struct MPPC_comp_state));
  129. state->histptr = MPPE_HIST_LEN;
  130. }
  131. int MPPC_Compress(u_char **src, u_char **dst, u_long *srcCnt, u_long *dstCnt, char *history, int flags, int undef)
  132. {
  133. struct MPPC_comp_state *state = (struct MPPC_comp_state*)history;
  134. uint32_t olen, off, len, idx, i, l;
  135. uint8_t *hist, *sbuf, *p, *q, *r, *s;
  136. int rtn = MPPC_OK;
  137. /*
  138. * At this point, to avoid possible buffer overflow caused by packet
  139. * expansion during/after compression, we should make sure we have
  140. * space for the worst case.
  141. * Maximum MPPC packet expansion is 12.5%. This is the worst case when
  142. * all octets in the input buffer are >= 0x80 and we cannot find any
  143. * repeated tokens.
  144. */
  145. if (*dstCnt < (*srcCnt * 9 / 8 + 2)) {
  146. rtn &= ~MPPC_OK;
  147. return (rtn);
  148. }
  149. /* We can't compress more then MPPE_HIST_LEN bytes in a call. */
  150. if (*srcCnt > MPPE_HIST_LEN) {
  151. rtn &= ~MPPC_OK;
  152. return (rtn);
  153. }
  154. hist = state->hist + MPPE_HIST_LEN;
  155. /* check if there is enough room at the end of the history */
  156. if (state->histptr + *srcCnt >= 2*MPPE_HIST_LEN) {
  157. rtn |= MPPC_RESTART_HISTORY;
  158. state->histptr = MPPE_HIST_LEN;
  159. memcpy(state->hist, hist, MPPE_HIST_LEN);
  160. }
  161. /* Add packet to the history. */
  162. sbuf = state->hist + state->histptr;
  163. memcpy(sbuf, *src, *srcCnt);
  164. state->histptr += *srcCnt;
  165. /* compress data */
  166. r = sbuf + *srcCnt;
  167. **dst = olen = i = 0;
  168. l = 8;
  169. while (i < *srcCnt - 2) {
  170. s = q = sbuf + i;
  171. /* Prognose matching position using hash function. */
  172. idx = HASH(s);
  173. p = hist + state->hash[idx];
  174. state->hash[idx] = (uint16_t) (s - hist);
  175. if (p > s) /* It was before MPPC_RESTART_HISTORY. */
  176. p -= MPPE_HIST_LEN; /* Try previous history buffer. */
  177. off = s - p;
  178. /* Check our prognosis. */
  179. if (off > MPPE_HIST_LEN - 1 || off < 1 || *p++ != *s++ ||
  180. *p++ != *s++ || *p++ != *s++) {
  181. /* No match found; encode literal byte. */
  182. if ((*src)[i] < 0x80) { /* literal byte < 0x80 */
  183. putbits8(*dst, (uint32_t) (*src)[i], 8, &olen, &l);
  184. } else { /* literal byte >= 0x80 */
  185. putbits16(*dst, (uint32_t) (0x100|((*src)[i]&0x7f)), 9,
  186. &olen, &l);
  187. }
  188. ++i;
  189. continue;
  190. }
  191. /* Find length of the matching fragment */
  192. #if defined(__amd64__) || defined(__i386__)
  193. /* Optimization for CPUs without strict data aligning requirements */
  194. while ((*((uint32_t*)p) == *((uint32_t*)s)) && (s < (r - 3))) {
  195. p+=4;
  196. s+=4;
  197. }
  198. #endif
  199. while((*p++ == *s++) && (s <= r));
  200. len = s - q - 1;
  201. i += len;
  202. /* At least 3 character match found; code data. */
  203. /* Encode offset. */
  204. if (off < 64) { /* 10-bit offset; 0 <= offset < 64 */
  205. putbits16(*dst, 0x3c0|off, 10, &olen, &l);
  206. } else if (off < 320) { /* 12-bit offset; 64 <= offset < 320 */
  207. putbits16(*dst, 0xe00|(off-64), 12, &olen, &l);
  208. } else if (off < 8192) { /* 16-bit offset; 320 <= offset < 8192 */
  209. putbits16(*dst, 0xc000|(off-320), 16, &olen, &l);
  210. } else { /* NOTREACHED */
  211. __assert_unreachable();
  212. rtn &= ~MPPC_OK;
  213. return (rtn);
  214. }
  215. /* Encode length of match. */
  216. if (len < 4) { /* length = 3 */
  217. putbits8(*dst, 0, 1, &olen, &l);
  218. } else if (len < 8) { /* 4 <= length < 8 */
  219. putbits8(*dst, 0x08|(len&0x03), 4, &olen, &l);
  220. } else if (len < 16) { /* 8 <= length < 16 */
  221. putbits8(*dst, 0x30|(len&0x07), 6, &olen, &l);
  222. } else if (len < 32) { /* 16 <= length < 32 */
  223. putbits8(*dst, 0xe0|(len&0x0f), 8, &olen, &l);
  224. } else if (len < 64) { /* 32 <= length < 64 */
  225. putbits16(*dst, 0x3c0|(len&0x1f), 10, &olen, &l);
  226. } else if (len < 128) { /* 64 <= length < 128 */
  227. putbits16(*dst, 0xf80|(len&0x3f), 12, &olen, &l);
  228. } else if (len < 256) { /* 128 <= length < 256 */
  229. putbits16(*dst, 0x3f00|(len&0x7f), 14, &olen, &l);
  230. } else if (len < 512) { /* 256 <= length < 512 */
  231. putbits16(*dst, 0xfe00|(len&0xff), 16, &olen, &l);
  232. } else if (len < 1024) { /* 512 <= length < 1024 */
  233. putbits24(*dst, 0x3fc00|(len&0x1ff), 18, &olen, &l);
  234. } else if (len < 2048) { /* 1024 <= length < 2048 */
  235. putbits24(*dst, 0xff800|(len&0x3ff), 20, &olen, &l);
  236. } else if (len < 4096) { /* 2048 <= length < 4096 */
  237. putbits24(*dst, 0x3ff000|(len&0x7ff), 22, &olen, &l);
  238. } else if (len < 8192) { /* 4096 <= length < 8192 */
  239. putbits24(*dst, 0xffe000|(len&0xfff), 24, &olen, &l);
  240. } else { /* NOTREACHED */
  241. rtn &= ~MPPC_OK;
  242. return (rtn);
  243. }
  244. }
  245. /* Add remaining octets to the output. */
  246. while(*srcCnt - i > 0) {
  247. if ((*src)[i] < 0x80) { /* literal byte < 0x80 */
  248. putbits8(*dst, (uint32_t) (*src)[i++], 8, &olen, &l);
  249. } else { /* literal byte >= 0x80 */
  250. putbits16(*dst, (uint32_t) (0x100|((*src)[i++]&0x7f)), 9, &olen,
  251. &l);
  252. }
  253. }
  254. /* Reset unused bits of the last output octet. */
  255. if ((l != 0) && (l != 8)) {
  256. putbits8(*dst, 0, l, &olen, &l);
  257. }
  258. /* If result is bigger then original, set flag and flush history. */
  259. if ((*srcCnt < olen) || ((flags & MPPC_SAVE_HISTORY) == 0)) {
  260. if (*srcCnt < olen)
  261. rtn |= MPPC_EXPANDED;
  262. bzero(history, sizeof(struct MPPC_comp_state));
  263. state->histptr = MPPE_HIST_LEN;
  264. }
  265. *src += *srcCnt;
  266. *srcCnt = 0;
  267. *dst += olen;
  268. *dstCnt -= olen;
  269. return (rtn);
  270. }