1 /* lzo1b_c.ch -- implementation of the LZO1B 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/
42 /***********************************************************************
44 ************************************************************************/
46 #if !defined(LZO_HAVE_R1) && !defined(LZO_NO_R1)
50 #if !defined(LZO_HAVE_M3) && !defined(LZO_NO_M3)
60 #define DI ++ii; DVAL_NEXT(dv,ii); UPDATE_D(dict,drun,dv,ii,in); MI
61 #define XI assert(ii < ip); ii = ip; DVAL_FIRST(dv,(ip));
63 #define DI ++ii; DINDEX1(dindex,ii); UPDATE_I(dict,0,dindex,ii,in); MI
64 #define XI assert(ii < ip); ii = ip;
68 /***********************************************************************
69 // compress a block of data.
71 // I really apologize for this spaghetti code.
72 ************************************************************************/
75 do_compress ( const lzo_bytep in , lzo_uint in_len,
76 lzo_bytep out, lzo_uintp out_len,
79 register const lzo_bytep ip;
81 #if defined(__LZO_HASH_INCREMENTAL)
87 const lzo_bytep const in_end = in + in_len;
88 const lzo_bytep const ip_end = in + in_len - MIN_LOOKAHEAD;
90 #if defined(LZO_HAVE_R1)
91 const lzo_bytep r1 = ip_end; /* pointer for R1 match (none yet) */
93 #if defined(LZO_HAVE_M3)
94 lzo_bytep m3 = out + 1; /* pointer after last m3/m4 match */
97 lzo_dict_p const dict = (lzo_dict_p) wrkmem;
100 #if defined(LZO_COLLECT_STATS)
101 lzo_stats->r_bits = R_BITS;
102 lzo_stats->m3o_bits = M3O_BITS;
103 lzo_stats->dd_bits = DD_BITS;
104 lzo_stats->clevel = CLEVEL;
105 lzo_stats->d_bits = D_BITS;
106 lzo_stats->min_lookahead = MIN_LOOKAHEAD;
107 lzo_stats->max_lookbehind = MAX_LOOKBEHIND;
108 lzo_stats->compress_id = LZO_CPP_MACRO_EXPAND(COMPRESS_ID);
111 /* init dictionary */
112 #if defined(LZO_DETERMINISTIC)
113 BZERO8_PTR(wrkmem,sizeof(lzo_dict_t),D_SIZE);
119 ii = ip; /* point to start of current literal run */
124 UPDATE_D(dict,drun,dv,ip,in);
134 const lzo_bytep m_pos;
136 const lzo_bytep m_pos_sav = NULL;
145 /***********************************************************************
146 // search for a match
147 ************************************************************************/
149 #if !defined(LZO_SEARCH_MATCH_INCLUDE_FILE)
150 # define LZO_SEARCH_MATCH_INCLUDE_FILE "lzo1b_sm.ch"
153 #include LZO_SEARCH_MATCH_INCLUDE_FILE
156 #if !defined(LZO_TEST_MATCH_INCLUDE_FILE)
157 # define LZO_TEST_MATCH_INCLUDE_FILE "lzo1b_tm.ch"
160 #include LZO_TEST_MATCH_INCLUDE_FILE
164 /***********************************************************************
166 ************************************************************************/
172 UPDATE_I(dict,0,dindex,ip,in);
183 /***********************************************************************
185 ************************************************************************/
189 UPDATE_I(dict,0,dindex,ip,in);
191 /* we have found a match of at least M2_MIN_LEN */
194 #if !defined(LZO_CODE_RUN_INCLUDE_FILE)
195 # define LZO_CODE_RUN_INCLUDE_FILE "lzo1b_cr.ch"
198 #include LZO_CODE_RUN_INCLUDE_FILE
201 /* ii now points to the start of the current match */
205 /***********************************************************************
207 ************************************************************************/
209 #if !defined(LZO_CODE_MATCH_INCLUDE_FILE)
210 # define LZO_CODE_MATCH_INCLUDE_FILE "lzo1b_cm.ch"
213 #include LZO_CODE_MATCH_INCLUDE_FILE
216 /* ii now points to the start of the next literal run */
222 /***********************************************************************
224 ************************************************************************/
226 assert(ip <= in_end);
228 #if defined(LZO_COLLECT_STATS)
233 for (i = 0; i < D_SIZE; i++)
236 if (BOUNDS_CHECKING_OFF_IN_EXPR(p == NULL || p < in || p > in_end))
237 lzo_stats->unused_dict_entries++;
239 lzo_stats->unused_dict_entries_percent =
240 100.0 * lzo_stats->unused_dict_entries / D_SIZE;
245 #if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
246 /* return if op == out to indicate that we
247 * couldn't compress and didn't copy anything.
252 return LZO_E_NOT_COMPRESSIBLE;
256 /* store the final literal run */
257 if (pd(in_end,ii) > 0)
259 lzo_uint t = pd(in_end,ii);
260 op = STORE_RUN(op,ii,t);
263 *out_len = pd(op, out);
264 return LZO_E_OK; /* compression went ok */