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) 2011 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7 Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
23 The LZO library is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2 of
26 the License, or (at your option) any later version.
28 The LZO library is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with the LZO library; see the file COPYING.
35 If not, write to the Free Software Foundation, Inc.,
36 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 Markus F.X.J. Oberhumer
39 <markus@oberhumer.com>
40 http://www.oberhumer.com/opensource/lzo/
48 /***********************************************************************
50 ************************************************************************/
52 #define SWD_THRESHOLD 1 /* lower limit for match length */
53 #define SWD_F 2048 /* upper limit for match length */
57 #define LZO_COMPRESS_T lzo2a_999_t
58 #define lzo_swd_t lzo2a_999_swd_t
59 #include "lzo_mchw.ch"
62 #if (LZO_CC_BORLANDC && LZO_MM_FLAT)
63 # if ((__BORLANDC__) >= 0x0450 && (__BORLANDC__) < 0x0460)
64 /* avoid internal compiler error */
70 /***********************************************************************
72 ************************************************************************/
74 #define putbyte(x) *op++ = LZO_BYTE(x)
76 #define putbits(j,x) \
77 if (k == 0) bitp = op++; \
79 if (k >= 8) { *bitp = LZO_BYTE(MASKBITS(8)); DUMPBITS(8); \
80 if (k > 0) bitp = op++; }
82 #define putbit(x) putbits(1,x)
85 /***********************************************************************
86 // this is a public function, but there is no prototype in a header file
87 ************************************************************************/
90 lzo2a_999_compress_callback ( const lzo_bytep in
, lzo_uint in_len
,
91 lzo_bytep out
, lzo_uintp out_len
,
97 lzo2a_999_compress_callback ( const lzo_bytep in
, lzo_uint in_len
,
98 lzo_bytep out
, lzo_uintp out_len
,
105 lzo_uint m_len
, m_off
;
107 LZO_COMPRESS_T
* const c
= &cc
;
108 lzo_swd_p
const swd
= (lzo_swd_p
) wrkmem
;
111 lzo_uint32 b
= 0; /* bit buffer */
112 unsigned k
= 0; /* bits in bit buffer */
115 LZO_COMPILE_TIME_ASSERT(LZO2A_999_MEM_COMPRESS
>= SIZEOF_LZO_SWD_T
)
119 c
->in_end
= in
+ in_len
;
121 c
->m1
= c
->m2
= c
->m3
= c
->m4
= 0;
125 r
= init_match(c
,swd
,NULL
,0,0);
129 swd
->max_chain
= max_chain
;
131 r
= find_match(c
,swd
,0,0);
136 lzo_uint lazy_match_min_gain
= 0;
149 if (m_len
< M3_MIN_LEN
)
152 lazy_match_min_gain
= 1;
156 if (m_len
>= M1_MIN_LEN
&& m_len
<= M1_MAX_LEN
&& m_off
<= 256)
158 lazy_match_min_gain
= 2;
164 else if (m_len
>= 10)
165 lazy_match_min_gain
= 1;
168 lazy_match_min_gain
= 1;
177 /* try a lazy match */
178 if (lazy_match_min_gain
> 0 && c
->look
> m_len
)
180 unsigned char lit
= LZO_BYTE(swd
->b_char
);
182 r
= find_match(c
,swd
,1,0);
183 assert(r
== 0); LZO_UNUSED(r
);
187 if (m_off
< 8192 && c
->m_off
>= 8192)
188 lazy_match_min_gain
+= extra1
;
191 if (m_len
>= M1_MIN_LEN
&& m_len
<= M1_MAX_LEN
&& m_off
<= 256)
193 if (!(c
->m_len
>= M1_MIN_LEN
&&
194 c
->m_len
<= M1_MAX_LEN
&& c
->m_off
<= 256))
195 lazy_match_min_gain
+= extra2
;
197 if (c
->m_len
>= M1_MIN_LEN
&&
198 c
->m_len
<= M1_MAX_LEN
&& c
->m_off
<= 256)
200 lazy_match_min_gain
-= 1;
203 if ((lzo_int
) lazy_match_min_gain
< 1)
204 lazy_match_min_gain
= 1;
206 if (c
->m_len
>= m_len
+ lazy_match_min_gain
)
212 assert(lzo_memcmp(c
->ip
- c
->look
, c
->ip
- c
->look
- m_off
,
214 assert(m_len
>= 3 || (m_len
>= 2 && m_off
<= 256));
232 putbyte(swd
->b_char
);
234 r
= find_match(c
,swd
,1,0);
235 assert(r
== 0); LZO_UNUSED(r
);
239 assert(m_len
>= M1_MIN_LEN
);
241 assert(m_off
<= SWD_N
);
244 if (m_len
>= M1_MIN_LEN
&& m_len
<= M1_MAX_LEN
&& m_off
<= 256)
248 putbits(2,m_len
- M1_MIN_LEN
);
253 else if (m_off
>= 8192)
255 unsigned len
= m_len
;
256 assert(m_len
>= M3_MIN_LEN
);
262 len
-= M3_MIN_LEN
- 1;
280 putbyte(((m_len
- 2) << 5) | (m_off
& 31));
286 lzo_uint len
= m_len
;
302 r
= find_match(c
,swd
,m_len
,1+ahead
);
303 assert(r
== 0); LZO_UNUSED(r
);
306 c
->codesize
= pd(op
, out
);
309 #if defined(LZO_EOF_CODE)
317 /* flush remaining bits */
318 assert(k
< CHAR_BIT
);
321 assert(b
== MASKBITS(k
));
322 assert(op
- bitp
> 1);
323 *bitp
= LZO_BYTE(MASKBITS(k
));
329 assert(c
->textsize
== in_len
);
330 c
->codesize
= pd(op
, out
);
332 *out_len
= pd(op
, out
);
334 if (c
->cb
&& c
->cb
->nprogress
)
335 (*c
->cb
->nprogress
)(c
->cb
, c
->textsize
, c
->codesize
, 0);
338 printf("%ld -> %ld: %ld %ld %ld %ld %ld %ld\n",
339 (long) c
->textsize
, (long) c
->codesize
,
340 c
->lit_bytes
, c
->m1
, c
->m2
, c
->m3
, c
->m4
, c
->lazy
);
347 /***********************************************************************
349 ************************************************************************/
352 lzo2a_999_compress ( const lzo_bytep in
, lzo_uint in_len
,
353 lzo_bytep out
, lzo_uintp out_len
,
356 return lzo2a_999_compress_callback(in
,in_len
,out
,out_len
,wrkmem
,
357 (lzo_callback_p
) 0, 0);