1 /* lzo1b_cm.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/
41 /* WARNING: this file should *not* be used by applications. It is
42 part of the implementation of the library and is subject
48 /***********************************************************************
50 ************************************************************************/
54 /* we already matched M2_MIN_LEN bytes,
55 * m_pos also already advanced M2_MIN_LEN bytes */
59 /* try to match another M2_MAX_LEN + 1 - M2_MIN_LEN bytes
60 * to see if we get more than a M2 match */
61 #define M2_OR_M3 (MATCH_M2)
63 #else /* (DD_BITS == 0) */
65 /* we already matched m_len bytes */
66 assert(m_len >= M2_MIN_LEN);
70 #define M2_OR_M3 (m_len <= M2_MAX_LEN)
72 #endif /* (DD_BITS == 0) */
78 /* we've found a M2 or M3 match */
81 /* 2a) compute match parameters */
83 assert(pd(ip,m_pos) == m_off);
84 --ip; /* ran one too far, point back to non-match */
88 /* 2a2) verify match parameters */
89 assert(m_len >= M2_MIN_LEN);
90 assert(m_len <= M2_MAX_LEN);
91 assert(m_len <= M3_MAX_LEN);
93 assert(m_off >= M2_MIN_OFFSET);
94 assert(m_off >= M3_MIN_OFFSET);
95 assert(m_off <= M3_MAX_OFFSET);
96 assert(ii-m_off == m_pos_sav);
97 assert(lzo_memcmp(m_pos_sav,ii,m_len) == 0);
99 /* 2b) code the match */
100 #if (_M2_MAX_OFFSET != _M3_MAX_OFFSET)
101 if (m_off <= M2_MAX_OFFSET)
104 assert(m_off <= M2_MAX_OFFSET);
106 m_off -= M2_MIN_OFFSET;
107 /* code match len + low offset bits */
108 *op++ = LZO_BYTE(((m_len - (M2_MIN_LEN - 2)) << M2O_BITS) |
110 /* code high offset bits */
111 *op++ = LZO_BYTE(m_off >> M2O_BITS);
112 LZO_STATS(lzo_stats->m2_matches++);
113 LZO_STATS(lzo_stats->m2_match[m_len]++);
114 #if (_M2_MAX_OFFSET != _M3_MAX_OFFSET)
118 #if defined(LZO_HAVE_R1)
119 #if (M3_MIN_LEN == M2_MIN_LEN)
120 r1 = ip_end; /* invalidate R1 pointer */
123 assert(m_len >= M3_MIN_LEN);
124 m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
126 *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
127 /* code low offset bits */
128 *op++ = LZO_BYTE(m_off & M3O_MASK);
129 /* code high offset bits */
130 *op++ = LZO_BYTE(m_off >> M3O_BITS);
131 LZO_STATS(lzo_stats->m3_matches++);
132 LZO_STATS(lzo_stats->m3_match[m_len]++);
133 #if defined(LZO_HAVE_M3)
134 m3 = op; /* set M3 pointer */
137 #endif /* (_M2_MAX_OFFSET != _M3_MAX_OFFSET) */
147 /* 2c) Insert phrases (beginning with ii+1) into the dictionary. */
149 #if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
150 /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
155 UPDATE_D(dict,drun,dv,ii,in);
157 dict[ DINDEX(dv,ii) ] = DENTRY(ii,in);
176 /* we've found a M3 or M4 match - see how far we can still go */
177 assert(ip <= in_end);
178 assert(lzo_memcmp(m_pos_sav,ii,(lzo_uint)(ip-ii)) == 0);
180 /* 2a) compute match parameters */
181 #if !defined(MATCH_IP_END)
182 assert(ii == ip - (M2_MAX_LEN + 1));
184 assert(m_len == (lzo_uint)(ip-ii));
186 assert(m_pos == m_pos_sav + m_len);
191 while (ip < end && *m_pos == *ip)
193 assert(ip <= in_end);
196 assert(pd(ip,m_pos) == m_off);
199 /* 2a2) verify match parameters */
200 assert(m_len >= M3_MIN_LEN);
202 assert(m_off >= M3_MIN_OFFSET);
203 assert(m_off >= M4_MIN_OFFSET);
204 assert(m_off <= M3_MAX_OFFSET);
205 assert(m_off <= M4_MAX_OFFSET);
206 assert(ii-m_off == m_pos_sav);
207 assert(lzo_memcmp(m_pos_sav,ii,m_len) == 0);
209 /* 2b) code the match */
210 if (m_len <= M3_MAX_LEN)
213 *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
214 LZO_STATS(lzo_stats->m3_matches++);
215 LZO_STATS(lzo_stats->m3_match[m_len]++);
219 assert(m_len >= M4_MIN_LEN);
220 /* code M4 match len flag */
223 m_len -= M4_MIN_LEN - 1;
230 *op++ = LZO_BYTE(m_len);
231 LZO_STATS(lzo_stats->m4_matches++);
234 m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
235 /* code low offset bits */
236 *op++ = LZO_BYTE(m_off & M3O_MASK);
237 /* code high offset bits */
238 *op++ = LZO_BYTE(m_off >> M3O_BITS);
240 #if defined(LZO_HAVE_M3)
241 m3 = op; /* set M3 pointer */
252 /* 2c) Insert phrases (beginning with ii+1) into the dictionary. */
254 /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
255 /* This is not recommended because it can be slow. */
260 UPDATE_D(dict,drun,dv,ii,in);
262 dict[ DINDEX(dv,ii) ] = DENTRY(ii,in);
270 SI DI DI DI DI DI DI DI DI XI
272 SI DI DI DI DI DI DI DI XI
274 SI DI DI DI DI DI DI XI
288 /* ii now points to the start of the next literal run */