1 /* lzo2a_9x.c -- implementation of the LZO2A-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/
45 /***********************************************************************
47 ************************************************************************/
49 #define THRESHOLD 1 /* lower limit for match length */
50 #define F 2048 /* upper limit for match length */
54 #define LZO_COMPRESS_T lzo2a_999_t
55 #define lzo_swd_t lzo2a_999_swd_t
56 #include "lzo_mchw.ch"
59 #if (LZO_CC_BORLANDC && LZO_MM_FLAT)
60 # if ((__BORLANDC__) >= 0x0450 && (__BORLANDC__) < 0x0460)
61 /* avoid internal compiler error */
67 /***********************************************************************
69 ************************************************************************/
71 #define putbyte(x) *op++ = LZO_BYTE(x)
73 #define putbits(j,x) \
74 if (k == 0) bitp = op++; \
76 if (k >= 8) { *bitp = LZO_BYTE(MASKBITS(8)); DUMPBITS(8); \
77 if (k > 0) bitp = op++; }
79 #define putbit(x) putbits(1,x)
82 /***********************************************************************
83 // this is a public function, but there is no prototype in a header file
84 ************************************************************************/
87 lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
88 lzo_bytep out, lzo_uintp out_len,
94 lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
95 lzo_bytep out, lzo_uintp out_len,
102 lzo_uint m_len, m_off;
104 LZO_COMPRESS_T * const c = &cc;
105 lzo_swd_p const swd = (lzo_swd_p) wrkmem;
108 lzo_uint32 b = 0; /* bit buffer */
109 unsigned k = 0; /* bits in bit buffer */
112 LZO_COMPILE_TIME_ASSERT(LZO2A_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)
116 c->in_end = in + in_len;
118 c->m1 = c->m2 = c->m3 = c->m4 = 0;
122 r = init_match(c,swd,NULL,0,0);
126 swd->max_chain = max_chain;
128 r = find_match(c,swd,0,0);
133 int lazy_match_min_gain = 0;
146 if (m_len < M3_MIN_LEN)
149 lazy_match_min_gain = 1;
153 if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
155 lazy_match_min_gain = 2;
159 else if (m_len >= 10)
160 lazy_match_min_gain = 1;
163 lazy_match_min_gain = 1;
170 /* try a lazy match */
171 if (lazy_match_min_gain > 0 && c->look > m_len)
173 int lit = swd->b_char;
175 r = find_match(c,swd,1,0);
180 if (m_off < 8192 && c->m_off >= 8192)
181 lazy_match_min_gain += extra1;
184 if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
186 if (!(c->m_len >= M1_MIN_LEN &&
187 c->m_len <= M1_MAX_LEN && c->m_off <= 256))
188 lazy_match_min_gain += extra2;
190 if (c->m_len >= M1_MIN_LEN &&
191 c->m_len <= M1_MAX_LEN && c->m_off <= 256)
193 lazy_match_min_gain -= 1;
196 if (lazy_match_min_gain < 1)
197 lazy_match_min_gain = 1;
199 if (c->m_len >= m_len + lazy_match_min_gain)
205 assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
207 assert(m_len >= 3 || (m_len >= 2 && m_off <= 256));
225 putbyte(swd->b_char);
227 r = find_match(c,swd,1,0);
232 assert(m_len >= M1_MIN_LEN);
237 if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256)
241 putbits(2,m_len - M1_MIN_LEN);
246 else if (m_off >= 8192)
248 unsigned len = m_len;
249 assert(m_len >= M3_MIN_LEN);
255 len -= M3_MIN_LEN - 1;
273 putbyte(((m_len - 2) << 5) | (m_off & 31));
279 lzo_uint len = m_len;
295 r = find_match(c,swd,m_len,1+ahead);
299 c->codesize = pd(op, out);
302 #if defined(LZO_EOF_CODE)
310 /* flush remaining bits */
311 assert(k < CHAR_BIT);
314 assert(b == MASKBITS(k));
315 assert(op - bitp > 1);
316 *bitp = LZO_BYTE(MASKBITS(k));
322 assert(c->textsize == in_len);
323 c->codesize = pd(op, out);
325 *out_len = pd(op, out);
327 if (c->cb && c->cb->nprogress)
328 (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);
331 printf("%ld -> %ld: %ld %ld %ld %ld %ld %ld\n",
332 (long) c->textsize, (long) c->codesize,
333 c->lit_bytes, c->m1, c->m2, c->m3, c->m4, c->lazy);
340 /***********************************************************************
342 ************************************************************************/
345 lzo2a_999_compress ( const lzo_bytep in , lzo_uint in_len,
346 lzo_bytep out, lzo_uintp out_len,
349 return lzo2a_999_compress_callback(in,in_len,out,out_len,wrkmem,
350 (lzo_callback_p) 0, 0);