1 /* lzo1x_c.ch -- implementation of the LZO1[XY]-1 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 /***********************************************************************
43 // compress a block of data.
44 ************************************************************************/
46 static __lzo_noinline lzo_uint
47 do_compress ( const lzo_bytep in , lzo_uint in_len,
48 lzo_bytep out, lzo_uintp out_len,
51 register const lzo_bytep ip;
53 const lzo_bytep const in_end = in + in_len;
54 const lzo_bytep const ip_end = in + in_len - M2_MAX_LEN - 5;
56 lzo_dict_p const dict = (lzo_dict_p) wrkmem;
65 register const lzo_bytep m_pos;
71 GINDEX(m_pos,m_off,dict,dindex,in);
72 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
75 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
79 GINDEX(m_pos,m_off,dict,dindex,in);
80 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
82 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
88 #if 1 && defined(LZO_UNALIGNED_OK_2)
89 if (* (const lzo_ushortp) m_pos != * (const lzo_ushortp) ip)
91 if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
97 if __lzo_likely(m_pos[2] == ip[2])
100 if (m_off <= M2_MAX_OFFSET)
104 if (lit == 3) /* better compression, but slower */
106 assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
107 *op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
110 if (m_pos[3] == ip[3])
116 /* still need a better way for finding M1 matches */
120 if (m_off <= M1_MAX_OFFSET && lit > 0 && lit <= 3)
122 if (m_off <= M1_MAX_OFFSET && lit == 3)
128 assert(op - 2 > out); op[-2] |= LZO_BYTE(t);
129 do *op++ = *ii++; while (--t > 0);
132 *op++ = LZO_BYTE(M1_MARKER | ((m_off & 3) << 2));
133 *op++ = LZO_BYTE(m_off >> 2);
144 UPDATE_I(dict,0,dindex,ip,in);
146 if __lzo_unlikely(ip >= ip_end)
153 UPDATE_I(dict,0,dindex,ip,in);
154 /* store current literal run */
157 register lzo_uint t = pd(ip,ii);
161 assert(op - 2 > out);
162 op[-2] |= LZO_BYTE(t);
165 *op++ = LZO_BYTE(t - 3);
168 register lzo_uint tt = t - 18;
177 *op++ = LZO_BYTE(tt);
179 do *op++ = *ii++; while (--t > 0);
185 if (m_pos[3] != *ip++ || m_pos[4] != *ip++ || m_pos[5] != *ip++ ||
186 m_pos[6] != *ip++ || m_pos[7] != *ip++ || m_pos[8] != *ip++
188 || m_pos[ 9] != *ip++ || m_pos[10] != *ip++ || m_pos[11] != *ip++
189 || m_pos[12] != *ip++ || m_pos[13] != *ip++ || m_pos[14] != *ip++
195 assert(m_len >= 3); assert(m_len <= M2_MAX_LEN);
197 if (m_off <= M2_MAX_OFFSET)
201 *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
202 *op++ = LZO_BYTE(m_off >> 3);
204 *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
205 *op++ = LZO_BYTE(m_off >> 2);
208 else if (m_off <= M3_MAX_OFFSET)
211 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
218 assert(m_off > 0); assert(m_off <= 0x7fff);
219 *op++ = LZO_BYTE(M4_MARKER |
220 ((m_off & 0x4000) >> 11) | (m_len - 2));
230 const lzo_bytep end = in_end;
231 const lzo_bytep m = m_pos + M2_MAX_LEN + 1;
232 while (ip < end && *m == *ip)
236 assert(m_len > M2_MAX_LEN);
238 if (m_off <= M3_MAX_OFFSET)
242 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
246 *op++ = M3_MARKER | 0;
256 assert(m_off > 0); assert(m_off <= 0x7fff);
257 if (m_len <= M4_MAX_LEN)
258 *op++ = LZO_BYTE(M4_MARKER |
259 ((m_off & 0x4000) >> 11) | (m_len - 2));
263 *op++ = LZO_BYTE(M4_MARKER | ((m_off & 0x4000) >> 11));
271 *op++ = LZO_BYTE(m_len);
276 *op++ = LZO_BYTE((m_off & 63) << 2);
277 *op++ = LZO_BYTE(m_off >> 6);
284 if __lzo_unlikely(ip >= ip_end)
288 *out_len = pd(op, out);
289 return pd(in_end,ii);
293 /***********************************************************************
294 // public entry point
295 ************************************************************************/
298 DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
299 lzo_bytep out, lzo_uintp out_len,
305 if __lzo_unlikely(in_len <= M2_MAX_LEN + 5)
309 t = do_compress(in,in_len,op,out_len,wrkmem);
315 const lzo_bytep ii = in + in_len - t;
317 if (op == out && t <= 238)
318 *op++ = LZO_BYTE(17 + t);
320 op[-2] |= LZO_BYTE(t);
322 *op++ = LZO_BYTE(t - 3);
325 lzo_uint tt = t - 18;
334 *op++ = LZO_BYTE(tt);
336 do *op++ = *ii++; while (--t > 0);
339 *op++ = M4_MARKER | 1;
343 *out_len = pd(op, out);