lzo1x_9x.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* lzo1x_9x.c -- implementation of the LZO1X-999 compression algorithm
  2. This file is part of the LZO real-time data compression library.
  3. Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
  4. Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
  5. Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
  6. Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
  7. Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
  8. Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
  9. Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
  10. Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
  11. Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
  12. Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
  13. Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
  14. Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
  15. Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  16. All Rights Reserved.
  17. The LZO library is free software; you can redistribute it and/or
  18. modify it under the terms of the GNU General Public License as
  19. published by the Free Software Foundation; either version 2 of
  20. the License, or (at your option) any later version.
  21. The LZO library is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25. You should have received a copy of the GNU General Public License
  26. along with the LZO library; see the file COPYING.
  27. If not, write to the Free Software Foundation, Inc.,
  28. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  29. Markus F.X.J. Oberhumer
  30. <markus@oberhumer.com>
  31. http://www.oberhumer.com/opensource/lzo/
  32. */
  33. #include "libbb.h"
  34. /* The following is probably only safe on Intel-compatible processors ... */
  35. #define LZO_UNALIGNED_OK_2
  36. #define LZO_UNALIGNED_OK_4
  37. #include "liblzo.h"
  38. #define LZO_MAX(a,b) ((a) >= (b) ? (a) : (b))
  39. #define LZO_MIN(a,b) ((a) <= (b) ? (a) : (b))
  40. #define LZO_MAX3(a,b,c) ((a) >= (b) ? LZO_MAX(a,c) : LZO_MAX(b,c))
  41. /***********************************************************************
  42. //
  43. ************************************************************************/
  44. #define SWD_N M4_MAX_OFFSET /* size of ring buffer */
  45. #define SWD_F 2048 /* upper limit for match length */
  46. #define SWD_BEST_OFF (LZO_MAX3(M2_MAX_LEN, M3_MAX_LEN, M4_MAX_LEN) + 1)
  47. typedef struct {
  48. int init;
  49. unsigned look; /* bytes in lookahead buffer */
  50. unsigned m_len;
  51. unsigned m_off;
  52. const uint8_t *bp;
  53. const uint8_t *ip;
  54. const uint8_t *in;
  55. const uint8_t *in_end;
  56. uint8_t *out;
  57. unsigned r1_lit;
  58. } lzo1x_999_t;
  59. #define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
  60. /* lzo_swd.c -- sliding window dictionary */
  61. /***********************************************************************
  62. //
  63. ************************************************************************/
  64. #define SWD_UINT_MAX USHRT_MAX
  65. #ifndef SWD_HSIZE
  66. # define SWD_HSIZE 16384
  67. #endif
  68. #ifndef SWD_MAX_CHAIN
  69. # define SWD_MAX_CHAIN 2048
  70. #endif
  71. #define HEAD3(b, p) \
  72. ( ((0x9f5f * ((((b[p]<<5)^b[p+1])<<5) ^ b[p+2])) >> 5) & (SWD_HSIZE-1) )
  73. #if defined(LZO_UNALIGNED_OK_2)
  74. # define HEAD2(b,p) (* (uint16_t *) &(b[p]))
  75. #else
  76. # define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
  77. #endif
  78. #define NIL2 SWD_UINT_MAX
  79. typedef struct lzo_swd {
  80. /* public - "built-in" */
  81. /* public - configuration */
  82. unsigned max_chain;
  83. int use_best_off;
  84. /* public - output */
  85. unsigned m_len;
  86. unsigned m_off;
  87. unsigned look;
  88. int b_char;
  89. #if defined(SWD_BEST_OFF)
  90. unsigned best_off[SWD_BEST_OFF];
  91. #endif
  92. /* semi public */
  93. lzo1x_999_t *c;
  94. unsigned m_pos;
  95. #if defined(SWD_BEST_OFF)
  96. unsigned best_pos[SWD_BEST_OFF];
  97. #endif
  98. /* private */
  99. unsigned ip; /* input pointer (lookahead) */
  100. unsigned bp; /* buffer pointer */
  101. unsigned rp; /* remove pointer */
  102. unsigned node_count;
  103. unsigned first_rp;
  104. uint8_t b[SWD_N + SWD_F];
  105. uint8_t b_wrap[SWD_F]; /* must follow b */
  106. uint16_t head3[SWD_HSIZE];
  107. uint16_t succ3[SWD_N + SWD_F];
  108. uint16_t best3[SWD_N + SWD_F];
  109. uint16_t llen3[SWD_HSIZE];
  110. #ifdef HEAD2
  111. uint16_t head2[65536L];
  112. #endif
  113. } lzo_swd_t, *lzo_swd_p;
  114. #define SIZEOF_LZO_SWD_T (sizeof(lzo_swd_t))
  115. /* Access macro for head3.
  116. * head3[key] may be uninitialized, but then its value will never be used.
  117. */
  118. #define s_get_head3(s,key) s->head3[key]
  119. /***********************************************************************
  120. //
  121. ************************************************************************/
  122. #define B_SIZE (SWD_N + SWD_F)
  123. static int swd_init(lzo_swd_p s)
  124. {
  125. /* defaults */
  126. s->node_count = SWD_N;
  127. memset(s->llen3, 0, sizeof(s->llen3[0]) * (unsigned)SWD_HSIZE);
  128. #ifdef HEAD2
  129. memset(s->head2, 0xff, sizeof(s->head2[0]) * 65536L);
  130. assert(s->head2[0] == NIL2);
  131. #endif
  132. s->ip = 0;
  133. s->bp = s->ip;
  134. s->first_rp = s->ip;
  135. assert(s->ip + SWD_F <= B_SIZE);
  136. s->look = (unsigned) (s->c->in_end - s->c->ip);
  137. if (s->look > 0) {
  138. if (s->look > SWD_F)
  139. s->look = SWD_F;
  140. memcpy(&s->b[s->ip],s->c->ip,s->look);
  141. s->c->ip += s->look;
  142. s->ip += s->look;
  143. }
  144. if (s->ip == B_SIZE)
  145. s->ip = 0;
  146. s->rp = s->first_rp;
  147. if (s->rp >= s->node_count)
  148. s->rp -= s->node_count;
  149. else
  150. s->rp += B_SIZE - s->node_count;
  151. return LZO_E_OK;
  152. }
  153. #define swd_pos2off(s,pos) \
  154. (s->bp > (pos) ? s->bp - (pos) : B_SIZE - ((pos) - s->bp))
  155. /***********************************************************************
  156. //
  157. ************************************************************************/
  158. static void swd_getbyte(lzo_swd_p s)
  159. {
  160. int c;
  161. if ((c = getbyte(*(s->c))) < 0) {
  162. if (s->look > 0)
  163. --s->look;
  164. } else {
  165. s->b[s->ip] = c;
  166. if (s->ip < SWD_F)
  167. s->b_wrap[s->ip] = c;
  168. }
  169. if (++s->ip == B_SIZE)
  170. s->ip = 0;
  171. if (++s->bp == B_SIZE)
  172. s->bp = 0;
  173. if (++s->rp == B_SIZE)
  174. s->rp = 0;
  175. }
  176. /***********************************************************************
  177. // remove node from lists
  178. ************************************************************************/
  179. static void swd_remove_node(lzo_swd_p s, unsigned node)
  180. {
  181. if (s->node_count == 0) {
  182. unsigned key;
  183. key = HEAD3(s->b,node);
  184. assert(s->llen3[key] > 0);
  185. --s->llen3[key];
  186. #ifdef HEAD2
  187. key = HEAD2(s->b,node);
  188. assert(s->head2[key] != NIL2);
  189. if ((unsigned) s->head2[key] == node)
  190. s->head2[key] = NIL2;
  191. #endif
  192. } else
  193. --s->node_count;
  194. }
  195. /***********************************************************************
  196. //
  197. ************************************************************************/
  198. static void swd_accept(lzo_swd_p s, unsigned n)
  199. {
  200. assert(n <= s->look);
  201. while (n--) {
  202. unsigned key;
  203. swd_remove_node(s,s->rp);
  204. /* add bp into HEAD3 */
  205. key = HEAD3(s->b,s->bp);
  206. s->succ3[s->bp] = s_get_head3(s,key);
  207. s->head3[key] = s->bp;
  208. s->best3[s->bp] = SWD_F + 1;
  209. s->llen3[key]++;
  210. assert(s->llen3[key] <= SWD_N);
  211. #ifdef HEAD2
  212. /* add bp into HEAD2 */
  213. key = HEAD2(s->b,s->bp);
  214. s->head2[key] = s->bp;
  215. #endif
  216. swd_getbyte(s);
  217. }
  218. }
  219. /***********************************************************************
  220. //
  221. ************************************************************************/
  222. static void swd_search(lzo_swd_p s, unsigned node, unsigned cnt)
  223. {
  224. const uint8_t *p1;
  225. const uint8_t *p2;
  226. const uint8_t *px;
  227. unsigned m_len = s->m_len;
  228. const uint8_t *b = s->b;
  229. const uint8_t *bp = s->b + s->bp;
  230. const uint8_t *bx = s->b + s->bp + s->look;
  231. unsigned char scan_end1;
  232. assert(s->m_len > 0);
  233. scan_end1 = bp[m_len - 1];
  234. for ( ; cnt-- > 0; node = s->succ3[node]) {
  235. p1 = bp;
  236. p2 = b + node;
  237. px = bx;
  238. assert(m_len < s->look);
  239. if (p2[m_len - 1] == scan_end1 &&
  240. p2[m_len] == p1[m_len] &&
  241. p2[0] == p1[0] &&
  242. p2[1] == p1[1]) {
  243. unsigned i;
  244. assert(lzo_memcmp(bp,&b[node],3) == 0);
  245. p1 += 2; p2 += 2;
  246. do {} while (++p1 < px && *p1 == *++p2);
  247. i = p1-bp;
  248. assert(lzo_memcmp(bp,&b[node],i) == 0);
  249. #if defined(SWD_BEST_OFF)
  250. if (i < SWD_BEST_OFF) {
  251. if (s->best_pos[i] == 0)
  252. s->best_pos[i] = node + 1;
  253. }
  254. #endif
  255. if (i > m_len) {
  256. s->m_len = m_len = i;
  257. s->m_pos = node;
  258. if (m_len == s->look)
  259. return;
  260. if (m_len >= SWD_F)
  261. return;
  262. if (m_len > (unsigned) s->best3[node])
  263. return;
  264. scan_end1 = bp[m_len - 1];
  265. }
  266. }
  267. }
  268. }
  269. /***********************************************************************
  270. //
  271. ************************************************************************/
  272. #ifdef HEAD2
  273. static int swd_search2(lzo_swd_p s)
  274. {
  275. unsigned key;
  276. assert(s->look >= 2);
  277. assert(s->m_len > 0);
  278. key = s->head2[ HEAD2(s->b,s->bp) ];
  279. if (key == NIL2)
  280. return 0;
  281. assert(lzo_memcmp(&s->b[s->bp],&s->b[key],2) == 0);
  282. #if defined(SWD_BEST_OFF)
  283. if (s->best_pos[2] == 0)
  284. s->best_pos[2] = key + 1;
  285. #endif
  286. if (s->m_len < 2) {
  287. s->m_len = 2;
  288. s->m_pos = key;
  289. }
  290. return 1;
  291. }
  292. #endif
  293. /***********************************************************************
  294. //
  295. ************************************************************************/
  296. static void swd_findbest(lzo_swd_p s)
  297. {
  298. unsigned key;
  299. unsigned cnt, node;
  300. unsigned len;
  301. assert(s->m_len > 0);
  302. /* get current head, add bp into HEAD3 */
  303. key = HEAD3(s->b,s->bp);
  304. node = s->succ3[s->bp] = s_get_head3(s,key);
  305. cnt = s->llen3[key]++;
  306. assert(s->llen3[key] <= SWD_N + SWD_F);
  307. if (cnt > s->max_chain)
  308. cnt = s->max_chain;
  309. s->head3[key] = s->bp;
  310. s->b_char = s->b[s->bp];
  311. len = s->m_len;
  312. if (s->m_len >= s->look) {
  313. if (s->look == 0)
  314. s->b_char = -1;
  315. s->m_off = 0;
  316. s->best3[s->bp] = SWD_F + 1;
  317. } else {
  318. #ifdef HEAD2
  319. if (swd_search2(s))
  320. #endif
  321. if (s->look >= 3)
  322. swd_search(s,node,cnt);
  323. if (s->m_len > len)
  324. s->m_off = swd_pos2off(s,s->m_pos);
  325. s->best3[s->bp] = s->m_len;
  326. #if defined(SWD_BEST_OFF)
  327. if (s->use_best_off) {
  328. int i;
  329. for (i = 2; i < SWD_BEST_OFF; i++)
  330. if (s->best_pos[i] > 0)
  331. s->best_off[i] = swd_pos2off(s,s->best_pos[i]-1);
  332. else
  333. s->best_off[i] = 0;
  334. }
  335. #endif
  336. }
  337. swd_remove_node(s,s->rp);
  338. #ifdef HEAD2
  339. /* add bp into HEAD2 */
  340. key = HEAD2(s->b,s->bp);
  341. s->head2[key] = s->bp;
  342. #endif
  343. }
  344. #undef HEAD3
  345. #undef HEAD2
  346. #undef s_get_head3
  347. /***********************************************************************
  348. //
  349. ************************************************************************/
  350. static int init_match(lzo1x_999_t *c, lzo_swd_p s, uint32_t use_best_off)
  351. {
  352. int r;
  353. assert(!c->init);
  354. c->init = 1;
  355. s->c = c;
  356. r = swd_init(s);
  357. if (r != 0)
  358. return r;
  359. s->use_best_off = use_best_off;
  360. return r;
  361. }
  362. /***********************************************************************
  363. //
  364. ************************************************************************/
  365. static int find_match(lzo1x_999_t *c, lzo_swd_p s,
  366. unsigned this_len, unsigned skip)
  367. {
  368. assert(c->init);
  369. if (skip > 0) {
  370. assert(this_len >= skip);
  371. swd_accept(s, this_len - skip);
  372. } else {
  373. assert(this_len <= 1);
  374. }
  375. s->m_len = 1;
  376. s->m_len = 1;
  377. #ifdef SWD_BEST_OFF
  378. if (s->use_best_off)
  379. memset(s->best_pos,0,sizeof(s->best_pos));
  380. #endif
  381. swd_findbest(s);
  382. c->m_len = s->m_len;
  383. c->m_off = s->m_off;
  384. swd_getbyte(s);
  385. if (s->b_char < 0) {
  386. c->look = 0;
  387. c->m_len = 0;
  388. } else {
  389. c->look = s->look + 1;
  390. }
  391. c->bp = c->ip - c->look;
  392. return LZO_E_OK;
  393. }
  394. /* this is a public functions, but there is no prototype in a header file */
  395. static int lzo1x_999_compress_internal(const uint8_t *in , unsigned in_len,
  396. uint8_t *out, unsigned *out_len,
  397. void *wrkmem,
  398. unsigned good_length,
  399. unsigned max_lazy,
  400. unsigned max_chain,
  401. uint32_t use_best_off);
  402. /***********************************************************************
  403. //
  404. ************************************************************************/
  405. static uint8_t *code_match(lzo1x_999_t *c,
  406. uint8_t *op, unsigned m_len, unsigned m_off)
  407. {
  408. assert(op > c->out);
  409. if (m_len == 2) {
  410. assert(m_off <= M1_MAX_OFFSET);
  411. assert(c->r1_lit > 0); assert(c->r1_lit < 4);
  412. m_off -= 1;
  413. *op++ = M1_MARKER | ((m_off & 3) << 2);
  414. *op++ = m_off >> 2;
  415. } else if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET) {
  416. assert(m_len >= 3);
  417. m_off -= 1;
  418. *op++ = ((m_len - 1) << 5) | ((m_off & 7) << 2);
  419. *op++ = m_off >> 3;
  420. assert(op[-2] >= M2_MARKER);
  421. } else if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && c->r1_lit >= 4) {
  422. assert(m_len == 3);
  423. assert(m_off > M2_MAX_OFFSET);
  424. m_off -= 1 + M2_MAX_OFFSET;
  425. *op++ = M1_MARKER | ((m_off & 3) << 2);
  426. *op++ = m_off >> 2;
  427. } else if (m_off <= M3_MAX_OFFSET) {
  428. assert(m_len >= 3);
  429. m_off -= 1;
  430. if (m_len <= M3_MAX_LEN)
  431. *op++ = M3_MARKER | (m_len - 2);
  432. else {
  433. m_len -= M3_MAX_LEN;
  434. *op++ = M3_MARKER | 0;
  435. while (m_len > 255)
  436. {
  437. m_len -= 255;
  438. *op++ = 0;
  439. }
  440. assert(m_len > 0);
  441. *op++ = m_len;
  442. }
  443. *op++ = m_off << 2;
  444. *op++ = m_off >> 6;
  445. } else {
  446. unsigned k;
  447. assert(m_len >= 3);
  448. assert(m_off > 0x4000); assert(m_off <= 0xbfff);
  449. m_off -= 0x4000;
  450. k = (m_off & 0x4000) >> 11;
  451. if (m_len <= M4_MAX_LEN)
  452. *op++ = M4_MARKER | k | (m_len - 2);
  453. else {
  454. m_len -= M4_MAX_LEN;
  455. *op++ = M4_MARKER | k | 0;
  456. while (m_len > 255)
  457. {
  458. m_len -= 255;
  459. *op++ = 0;
  460. }
  461. assert(m_len > 0);
  462. *op++ = m_len;
  463. }
  464. *op++ = m_off << 2;
  465. *op++ = m_off >> 6;
  466. }
  467. return op;
  468. }
  469. static uint8_t *STORE_RUN(lzo1x_999_t *c, uint8_t *op,
  470. const uint8_t *ii, unsigned t)
  471. {
  472. if (op == c->out && t <= 238) {
  473. *op++ = 17 + t;
  474. } else if (t <= 3) {
  475. op[-2] |= t;
  476. } else if (t <= 18) {
  477. *op++ = t - 3;
  478. } else {
  479. unsigned tt = t - 18;
  480. *op++ = 0;
  481. while (tt > 255) {
  482. tt -= 255;
  483. *op++ = 0;
  484. }
  485. assert(tt > 0);
  486. *op++ = tt;
  487. }
  488. do *op++ = *ii++; while (--t > 0);
  489. return op;
  490. }
  491. static uint8_t *code_run(lzo1x_999_t *c, uint8_t *op, const uint8_t *ii,
  492. unsigned lit)
  493. {
  494. if (lit > 0) {
  495. assert(m_len >= 2);
  496. op = STORE_RUN(c,op,ii,lit);
  497. } else {
  498. assert(m_len >= 3);
  499. }
  500. c->r1_lit = lit;
  501. return op;
  502. }
  503. /***********************************************************************
  504. //
  505. ************************************************************************/
  506. static int len_of_coded_match(unsigned m_len, unsigned m_off, unsigned lit)
  507. {
  508. int n = 4;
  509. if (m_len < 2)
  510. return -1;
  511. if (m_len == 2)
  512. return (m_off <= M1_MAX_OFFSET && lit > 0 && lit < 4) ? 2 : -1;
  513. if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
  514. return 2;
  515. if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
  516. return 2;
  517. if (m_off <= M3_MAX_OFFSET) {
  518. if (m_len <= M3_MAX_LEN)
  519. return 3;
  520. m_len -= M3_MAX_LEN;
  521. } else if (m_off <= M4_MAX_OFFSET) {
  522. if (m_len <= M4_MAX_LEN)
  523. return 3;
  524. m_len -= M4_MAX_LEN;
  525. } else
  526. return -1;
  527. while (m_len > 255) {
  528. m_len -= 255;
  529. n++;
  530. }
  531. return n;
  532. }
  533. static int min_gain(unsigned ahead, unsigned lit1,
  534. unsigned lit2, int l1, int l2, int l3)
  535. {
  536. int lazy_match_min_gain = 0;
  537. assert (ahead >= 1);
  538. lazy_match_min_gain += ahead;
  539. if (lit1 <= 3)
  540. lazy_match_min_gain += (lit2 <= 3) ? 0 : 2;
  541. else if (lit1 <= 18)
  542. lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
  543. lazy_match_min_gain += (l2 - l1) * 2;
  544. if (l3 > 0)
  545. lazy_match_min_gain -= (ahead - l3) * 2;
  546. if (lazy_match_min_gain < 0)
  547. lazy_match_min_gain = 0;
  548. return lazy_match_min_gain;
  549. }
  550. /***********************************************************************
  551. //
  552. ************************************************************************/
  553. #if defined(SWD_BEST_OFF)
  554. static void better_match(const lzo_swd_p swd,
  555. unsigned *m_len, unsigned *m_off)
  556. {
  557. if (*m_len <= M2_MIN_LEN)
  558. return;
  559. if (*m_off <= M2_MAX_OFFSET)
  560. return;
  561. /* M3/M4 -> M2 */
  562. if (*m_off > M2_MAX_OFFSET &&
  563. *m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
  564. swd->best_off[*m_len-1] && swd->best_off[*m_len-1] <= M2_MAX_OFFSET)
  565. {
  566. *m_len = *m_len - 1;
  567. *m_off = swd->best_off[*m_len];
  568. return;
  569. }
  570. /* M4 -> M2 */
  571. if (*m_off > M3_MAX_OFFSET &&
  572. *m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
  573. swd->best_off[*m_len-2] && swd->best_off[*m_len-2] <= M2_MAX_OFFSET)
  574. {
  575. *m_len = *m_len - 2;
  576. *m_off = swd->best_off[*m_len];
  577. return;
  578. }
  579. /* M4 -> M3 */
  580. if (*m_off > M3_MAX_OFFSET &&
  581. *m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
  582. swd->best_off[*m_len-1] && swd->best_off[*m_len-1] <= M3_MAX_OFFSET)
  583. {
  584. *m_len = *m_len - 1;
  585. *m_off = swd->best_off[*m_len];
  586. }
  587. }
  588. #endif
  589. /***********************************************************************
  590. //
  591. ************************************************************************/
  592. static int lzo1x_999_compress_internal(const uint8_t *in, unsigned in_len,
  593. uint8_t *out, unsigned *out_len,
  594. void *wrkmem,
  595. unsigned good_length,
  596. unsigned max_lazy,
  597. unsigned max_chain,
  598. uint32_t use_best_off)
  599. {
  600. uint8_t *op;
  601. const uint8_t *ii;
  602. unsigned lit;
  603. unsigned m_len, m_off;
  604. lzo1x_999_t cc;
  605. lzo1x_999_t * const c = &cc;
  606. lzo_swd_p const swd = (lzo_swd_p) wrkmem;
  607. int r;
  608. c->init = 0;
  609. c->ip = c->in = in;
  610. c->in_end = in + in_len;
  611. c->out = out;
  612. op = out;
  613. ii = c->ip; /* point to start of literal run */
  614. lit = 0;
  615. c->r1_lit = 0;
  616. r = init_match(c, swd, use_best_off);
  617. if (r != 0)
  618. return r;
  619. swd->max_chain = max_chain;
  620. r = find_match(c, swd, 0, 0);
  621. if (r != 0)
  622. return r;
  623. while (c->look > 0) {
  624. unsigned ahead;
  625. unsigned max_ahead;
  626. int l1, l2, l3;
  627. m_len = c->m_len;
  628. m_off = c->m_off;
  629. assert(c->bp == c->ip - c->look);
  630. assert(c->bp >= in);
  631. if (lit == 0)
  632. ii = c->bp;
  633. assert(ii + lit == c->bp);
  634. assert(swd->b_char == *(c->bp));
  635. if ( m_len < 2 ||
  636. (m_len == 2 && (m_off > M1_MAX_OFFSET || lit == 0 || lit >= 4)) ||
  637. /* Do not accept this match for compressed-data compatibility
  638. * with LZO v1.01 and before
  639. * [ might be a problem for decompress() and optimize() ]
  640. */
  641. (m_len == 2 && op == out) ||
  642. (op == out && lit == 0))
  643. {
  644. /* a literal */
  645. m_len = 0;
  646. }
  647. else if (m_len == M2_MIN_LEN) {
  648. /* compression ratio improves if we code a literal in some cases */
  649. if (m_off > MX_MAX_OFFSET && lit >= 4)
  650. m_len = 0;
  651. }
  652. if (m_len == 0) {
  653. /* a literal */
  654. lit++;
  655. swd->max_chain = max_chain;
  656. r = find_match(c,swd,1,0);
  657. assert(r == 0);
  658. continue;
  659. }
  660. /* a match */
  661. #if defined(SWD_BEST_OFF)
  662. if (swd->use_best_off)
  663. better_match(swd,&m_len,&m_off);
  664. #endif
  665. /* shall we try a lazy match ? */
  666. ahead = 0;
  667. if (m_len >= max_lazy) {
  668. /* no */
  669. l1 = 0;
  670. max_ahead = 0;
  671. } else {
  672. /* yes, try a lazy match */
  673. l1 = len_of_coded_match(m_len,m_off,lit);
  674. assert(l1 > 0);
  675. max_ahead = LZO_MIN(2, (unsigned)l1 - 1);
  676. }
  677. while (ahead < max_ahead && c->look > m_len) {
  678. int lazy_match_min_gain;
  679. if (m_len >= good_length)
  680. swd->max_chain = max_chain >> 2;
  681. else
  682. swd->max_chain = max_chain;
  683. r = find_match(c,swd,1,0);
  684. ahead++;
  685. assert(r == 0);
  686. assert(c->look > 0);
  687. assert(ii + lit + ahead == c->bp);
  688. if (c->m_len < m_len)
  689. continue;
  690. if (c->m_len == m_len && c->m_off >= m_off)
  691. continue;
  692. #if defined(SWD_BEST_OFF)
  693. if (swd->use_best_off)
  694. better_match(swd,&c->m_len,&c->m_off);
  695. #endif
  696. l2 = len_of_coded_match(c->m_len,c->m_off,lit+ahead);
  697. if (l2 < 0)
  698. continue;
  699. /* compressed-data compatibility [see above] */
  700. l3 = (op == out) ? -1 : len_of_coded_match(ahead,m_off,lit);
  701. lazy_match_min_gain = min_gain(ahead,lit,lit+ahead,l1,l2,l3);
  702. if (c->m_len >= m_len + lazy_match_min_gain) {
  703. if (l3 > 0) {
  704. /* code previous run */
  705. op = code_run(c,op,ii,lit);
  706. lit = 0;
  707. /* code shortened match */
  708. op = code_match(c,op,ahead,m_off);
  709. } else {
  710. lit += ahead;
  711. assert(ii + lit == c->bp);
  712. }
  713. goto lazy_match_done;
  714. }
  715. }
  716. assert(ii + lit + ahead == c->bp);
  717. /* 1 - code run */
  718. op = code_run(c,op,ii,lit);
  719. lit = 0;
  720. /* 2 - code match */
  721. op = code_match(c,op,m_len,m_off);
  722. swd->max_chain = max_chain;
  723. r = find_match(c,swd,m_len,1+ahead);
  724. assert(r == 0);
  725. lazy_match_done: ;
  726. }
  727. /* store final run */
  728. if (lit > 0)
  729. op = STORE_RUN(c,op,ii,lit);
  730. #if defined(LZO_EOF_CODE)
  731. *op++ = M4_MARKER | 1;
  732. *op++ = 0;
  733. *op++ = 0;
  734. #endif
  735. *out_len = op - out;
  736. return LZO_E_OK;
  737. }
  738. /***********************************************************************
  739. //
  740. ************************************************************************/
  741. int lzo1x_999_compress_level(const uint8_t *in, unsigned in_len,
  742. uint8_t *out, unsigned *out_len,
  743. void *wrkmem,
  744. int compression_level)
  745. {
  746. static const struct {
  747. uint16_t good_length;
  748. uint16_t max_lazy;
  749. uint16_t max_chain;
  750. uint16_t use_best_off;
  751. } c[3] = {
  752. { 8, 32, 256, 0 },
  753. { 32, 128, 2048, 1 },
  754. { SWD_F, SWD_F, 4096, 1 } /* max. compression */
  755. };
  756. if (compression_level < 7 || compression_level > 9)
  757. return LZO_E_ERROR;
  758. compression_level -= 7;
  759. return lzo1x_999_compress_internal(in, in_len, out, out_len, wrkmem,
  760. c[compression_level].good_length,
  761. c[compression_level].max_lazy,
  762. c[compression_level].max_chain,
  763. c[compression_level].use_best_off);
  764. }