1 /* lzo1a_cm.ch -- implementation of the LZO1A 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/
44 /* WARNING: this file should *not* be used by applications. It is
45 part of the implementation of the library and is subject
51 /***********************************************************************
52 // code the match in LZO1 compatible format
53 ************************************************************************/
55 #define THRESHOLD (M2_MIN_LEN - 1)
56 #define MSIZE LZO_SIZE(M2L_BITS)
59 /***********************************************************************
61 ************************************************************************/
65 /* we already matched M2_MIN_LEN bytes,
66 * m_pos also already advanced M2_MIN_LEN bytes */
70 /* try to match another M2_MAX_LEN + 1 - M2_MIN_LEN bytes
71 * to see if we get more than a M2 match */
72 #define M2_OR_M3 (MATCH_M2)
74 #else /* (DD_BITS == 0) */
76 /* we already matched m_len bytes */
77 assert(m_len >= M2_MIN_LEN);
81 #define M2_OR_M3 (m_len <= M2_MAX_LEN)
83 #endif /* (DD_BITS == 0) */
88 /* we've found a short match */
91 /* 2a) compute match parameters */
93 assert(pd(ip,m_pos) == m_off);
94 --ip; /* ran one too far, point back to non-match */
97 assert(m_len >= M2_MIN_LEN);
98 assert(m_len <= M2_MAX_LEN);
100 assert(m_off >= M2_MIN_OFFSET);
101 assert(m_off <= M2_MAX_OFFSET);
102 assert(ii-m_off == m_pos_sav);
103 assert(lzo_memcmp(m_pos_sav,ii,m_len) == 0);
105 /* 2b) code the match */
106 m_off -= M2_MIN_OFFSET;
107 /* code short match len + low offset bits */
108 *op++ = LZO_BYTE(((m_len - THRESHOLD) << M2O_BITS) |
110 /* code high offset bits */
111 *op++ = LZO_BYTE(m_off >> M2O_BITS);
121 /* 2c) Insert phrases (beginning with ii+1) into the dictionary. */
123 #if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
124 /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
129 UPDATE_D(dict,drun,dv,ii,in);
131 dict[ DINDEX(dv,ii) ] = DENTRY(ii,in);
150 /* we've found a long match - see how far we can still go */
153 assert(ip <= in_end);
154 assert(ii == ip - (M2_MAX_LEN + 1));
155 assert(lzo_memcmp(m_pos_sav,ii,(lzo_uint)(ip-ii)) == 0);
158 assert(m_len == (lzo_uint)(ip-ii));
160 assert(m_pos == m_pos_sav + m_len);
163 if (pd(in_end,ip) <= (M3_MAX_LEN - M3_MIN_LEN))
167 end = ip + (M3_MAX_LEN - M3_MIN_LEN);
168 assert(end < in_end);
171 while (ip < end && *m_pos == *ip)
173 assert(ip <= in_end);
175 /* 2a) compute match parameters */
177 assert(m_len >= M3_MIN_LEN);
178 assert(m_len <= M3_MAX_LEN);
180 assert(m_off >= M3_MIN_OFFSET);
181 assert(m_off <= M3_MAX_OFFSET);
182 assert(ii-m_off == m_pos_sav);
183 assert(lzo_memcmp(m_pos_sav,ii,m_len) == 0);
184 assert(pd(ip,m_pos) == m_off);
186 /* 2b) code the match */
187 m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
188 /* code long match flag + low offset bits */
189 *op++ = LZO_BYTE(((MSIZE - 1) << M3O_BITS) | (m_off & M3O_MASK));
190 /* code high offset bits */
191 *op++ = LZO_BYTE(m_off >> M3O_BITS);
193 *op++ = LZO_BYTE(m_len - M3_MIN_LEN);
203 /* 2c) Insert phrases (beginning with ii+1) into the dictionary. */
205 /* Insert the whole match (ii+1)..(ip-1) into dictionary. */
206 /* This is not recommended because it can be slow. */
211 UPDATE_D(dict,drun,dv,ii,in);
213 dict[ DINDEX(dv,ii) ] = DENTRY(ii,in);
221 SI DI DI DI DI DI DI DI DI XI
223 SI DI DI DI DI DI DI DI XI
225 SI DI DI DI DI DI DI XI
239 /* ii now points to the start of the next literal run */