1 /* lzo1f_9x.c -- implementation of the LZO1F-999 compression algorithm
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
7 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
8 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
9 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
10 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
11 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
12 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
13 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
14 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
15 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
16 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
17 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
20 The LZO library is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation; either version 2 of
23 the License, or (at your option) any later version.
25 The LZO library is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with the LZO library; see the file COPYING.
32 If not, write to the Free Software Foundation, Inc.,
33 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 Markus F.X.J. Oberhumer
36 <markus@oberhumer.com>
37 http://www.oberhumer.com/opensource/lzo/
44 /***********************************************************************
46 ************************************************************************/
48 #define N 16383 /* size of ring buffer */
49 #define THRESHOLD 2 /* lower limit for match length */
50 #define F 2048 /* upper limit for match length */
54 #define LZO_COMPRESS_T lzo1f_999_t
55 #define lzo_swd_t lzo1f_999_swd_t
56 #include "lzo_mchw.ch"
60 /***********************************************************************
62 ************************************************************************/
65 code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
67 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
70 *op++ = LZO_BYTE(((m_len - 2) << 5) | ((m_off & 7) << 2));
71 *op++ = LZO_BYTE(m_off >> 3);
74 else if (m_len == M2_MIN_LEN && m_off <= 2 * M2_MAX_OFFSET &&
77 assert(m_off > M2_MAX_OFFSET);
78 m_off -= 1 + M2_MAX_OFFSET;
79 *op++ = LZO_BYTE(((m_off & 7) << 2));
80 *op++ = LZO_BYTE(m_off >> 3);
85 if (m_len <= M3_MAX_LEN)
86 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
90 *op++ = M3_MARKER | 0;
97 *op++ = LZO_BYTE(m_len);
99 *op++ = LZO_BYTE((m_off & 63) << 2);
100 *op++ = LZO_BYTE(m_off >> 6);
109 STORE_RUN ( lzo_bytep op, const lzo_bytep ii, lzo_uint t, lzo_bytep out )
111 if (t < 4 && op > out)
112 op[-2] |= LZO_BYTE(t);
117 lzo_uint tt = t - 31;
126 *op++ = LZO_BYTE(tt);
128 do *op++ = *ii++; while (--t > 0);
134 /***********************************************************************
135 // this is a public function, but there is no prototype in a header file
136 ************************************************************************/
139 lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
140 lzo_bytep out, lzo_uintp out_len,
143 lzo_uint max_chain );
146 lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
147 lzo_bytep out, lzo_uintp out_len,
155 lzo_uint m_len, m_off;
157 LZO_COMPRESS_T * const c = &cc;
158 lzo_swd_p const swd = (lzo_swd_p) wrkmem;
162 LZO_COMPILE_TIME_ASSERT(LZO1F_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
166 c->in_end = in + in_len;
168 c->r1_r = c->m2_m = c->m3_m = 0;
171 ii = c->ip; /* point to start of literal run */
173 c->r1_lit = c->r1_m_len = 0;
175 r = init_match(c,swd,NULL,0,0);
179 swd->max_chain = max_chain;
181 r = find_match(c,swd,0,0);
186 int lazy_match_min_gain = -1;
192 assert(c->ip - c->look >= in);
194 ii = c->ip - c->look;
195 assert(ii + lit == c->ip - c->look);
196 assert(swd->b_char == *(c->ip - c->look));
198 if ((m_len < M2_MIN_LEN) ||
199 (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET))
205 assert(c->ip - c->look - m_off >= in);
206 assert(c->ip - c->look - m_off + m_len < c->ip);
207 assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
211 lazy_match_min_gain = 1;
213 lazy_match_min_gain = 3;
215 lazy_match_min_gain = 3;
217 lazy_match_min_gain = 1;
220 /* try a lazy match */
221 if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len)
223 r = find_match(c,swd,1,0);
227 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
228 c->m_off > M2_MAX_OFFSET)
230 lazy_match_min_gain += 1;
232 else if (c->m_len <= M2_MAX_LEN &&
233 c->m_off <= M2_MAX_OFFSET &&
234 m_off > M2_MAX_OFFSET)
236 if (lazy_match_min_gain > 0)
237 lazy_match_min_gain -= 1;
239 else if (m_len == M2_MIN_LEN && c->m_len == M2_MIN_LEN &&
240 c->m_off <= 2 * M2_MAX_OFFSET &&
241 m_off > M2_MAX_OFFSET)
243 if (lazy_match_min_gain > 0)
244 lazy_match_min_gain -= 1;
247 if (c->m_len >= m_len + lazy_match_min_gain)
253 assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
257 assert(ii + lit == c->ip - c->look);
263 assert(ii + lit + 1 == c->ip - c->look);
267 assert(ii + lit + ahead == c->ip - c->look);
274 r = find_match(c,swd,1,0);
282 op = STORE_RUN(op,ii,lit,out);
288 c->r1_lit = c->r1_m_len = 0;
291 op = code_match(c,op,m_len,m_off);
292 r = find_match(c,swd,m_len,1+ahead);
296 c->codesize = pd(op, out);
300 /* store final run */
302 op = STORE_RUN(op,ii,lit,out);
304 #if defined(LZO_EOF_CODE)
305 *op++ = M3_MARKER | 1;
310 c->codesize = pd(op, out);
311 assert(c->textsize == in_len);
313 *out_len = pd(op, out);
315 if (c->cb && c->cb->nprogress)
316 (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
319 printf("%ld %ld -> %ld: %ld %ld %ld %ld\n",
320 (long) c->textsize, (long)in_len, (long) c->codesize,
321 c->r1_r, c->m2_m, c->m3_m, c->lazy);
328 /***********************************************************************
330 ************************************************************************/
333 lzo1f_999_compress ( const lzo_bytep in , lzo_uint in_len,
334 lzo_bytep out, lzo_uintp out_len,
337 return lzo1f_999_compress_callback(in,in_len,out,out_len,wrkmem,
338 (lzo_callback_p) 0, 0);