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) 1996-2014 Markus Franz Xaver Johannes Oberhumer
8 The LZO library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 The LZO library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the LZO library; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
30 #if 1 && defined(DO_COMPRESS) && !defined(do_compress)
31 /* choose a unique name to better help PGO optimizations */
32 # define do_compress LZO_PP_ECONCAT2(DO_COMPRESS,_core)
36 /***********************************************************************
37 // compress a block of data.
38 ************************************************************************/
40 static __lzo_noinline lzo_uint
41 do_compress ( const lzo_bytep in , lzo_uint in_len,
42 lzo_bytep out, lzo_uintp out_len,
43 lzo_uint ti, lzo_voidp wrkmem)
47 const lzo_bytep const in_end = in + in_len;
48 const lzo_bytep const ip_end = in + in_len - 20;
50 lzo_dict_p const dict = (lzo_dict_p) wrkmem;
56 ip += ti < 4 ? 4 - ti : 0;
59 const lzo_bytep m_pos;
60 #if !(LZO_DETERMINISTIC)
61 LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
65 if __lzo_unlikely(ip >= ip_end)
68 GINDEX(m_pos,m_off,dict,dindex,in);
69 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
72 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
76 GINDEX(m_pos,m_off,dict,dindex,in);
77 if (LZO_CHECK_MPOS_NON_DET(m_pos,m_off,in,ip,M4_MAX_OFFSET))
79 if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
84 #if (LZO_OPT_UNALIGNED32)
85 if (UA_GET_NE32(m_pos) != UA_GET_NE32(ip))
87 if (m_pos[0] != ip[0] || m_pos[1] != ip[1] || m_pos[2] != ip[2] || m_pos[3] != ip[3])
92 UPDATE_I(dict,0,dindex,ip,in);
93 ip += 1 + ((ip - ii) >> 5);
97 UPDATE_I(dict,0,dindex,ip,in);
105 ip += 1 + ((ip - ii) >> 5);
107 if __lzo_unlikely(ip >= ip_end)
109 dv = UA_GET_LE32(ip);
110 dindex = DINDEX(dv,ip);
111 GINDEX(m_off,m_pos,in+dict,dindex,in);
112 UPDATE_I(dict,0,dindex,ip,in);
113 if __lzo_unlikely(dv != UA_GET_LE32(m_pos))
122 lzo_uint t = pd(ip,ii);
127 op[-2] = LZO_BYTE(op[-2] | t);
128 #if (LZO_OPT_UNALIGNED32)
132 { do *op++ = *ii++; while (--t > 0); }
135 #if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
138 *op++ = LZO_BYTE(t - 3);
140 UA_COPY8(op+8, ii+8);
147 *op++ = LZO_BYTE(t - 3);
150 lzo_uint tt = t - 18;
152 while __lzo_unlikely(tt > 255)
159 *op++ = LZO_BYTE(tt);
161 #if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
164 UA_COPY8(op+8, ii+8);
165 op += 16; ii += 16; t -= 16;
166 } while (t >= 16); if (t > 0)
168 { do *op++ = *ii++; while (--t > 0); }
174 #if (LZO_OPT_UNALIGNED64)
176 v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
177 if __lzo_unlikely(v == 0) {
180 v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
181 if __lzo_unlikely(ip + m_len >= ip_end)
185 #if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz64)
186 m_len += lzo_bitops_ctlz64(v) / CHAR_BIT;
187 #elif (LZO_ABI_BIG_ENDIAN)
188 if ((v >> (64 - CHAR_BIT)) == 0) do {
191 } while ((v >> (64 - CHAR_BIT)) == 0);
192 #elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz64)
193 m_len += lzo_bitops_cttz64(v) / CHAR_BIT;
194 #elif (LZO_ABI_LITTLE_ENDIAN)
195 if ((v & UCHAR_MAX) == 0) do {
198 } while ((v & UCHAR_MAX) == 0);
200 if (ip[m_len] == m_pos[m_len]) do {
202 } while (ip[m_len] == m_pos[m_len]);
204 #elif (LZO_OPT_UNALIGNED32)
206 v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
207 if __lzo_unlikely(v == 0) {
210 v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
214 v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
215 if __lzo_unlikely(ip + m_len >= ip_end)
219 #if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz32)
220 m_len += lzo_bitops_ctlz32(v) / CHAR_BIT;
221 #elif (LZO_ABI_BIG_ENDIAN)
222 if ((v >> (32 - CHAR_BIT)) == 0) do {
225 } while ((v >> (32 - CHAR_BIT)) == 0);
226 #elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz32)
227 m_len += lzo_bitops_cttz32(v) / CHAR_BIT;
228 #elif (LZO_ABI_LITTLE_ENDIAN)
229 if ((v & UCHAR_MAX) == 0) do {
232 } while ((v & UCHAR_MAX) == 0);
234 if (ip[m_len] == m_pos[m_len]) do {
236 } while (ip[m_len] == m_pos[m_len]);
239 if __lzo_unlikely(ip[m_len] == m_pos[m_len]) {
242 if (ip[m_len] != m_pos[m_len])
245 if (ip[m_len] != m_pos[m_len])
248 if (ip[m_len] != m_pos[m_len])
251 if (ip[m_len] != m_pos[m_len])
254 if (ip[m_len] != m_pos[m_len])
257 if (ip[m_len] != m_pos[m_len])
260 if (ip[m_len] != m_pos[m_len])
263 if __lzo_unlikely(ip + m_len >= ip_end)
265 } while (ip[m_len] == m_pos[m_len]);
270 m_off = pd(ip,m_pos);
273 if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
277 *op++ = LZO_BYTE(((m_len - 1) << 5) | ((m_off & 7) << 2));
278 *op++ = LZO_BYTE(m_off >> 3);
280 *op++ = LZO_BYTE(((m_len + 1) << 4) | ((m_off & 3) << 2));
281 *op++ = LZO_BYTE(m_off >> 2);
284 else if (m_off <= M3_MAX_OFFSET)
287 if (m_len <= M3_MAX_LEN)
288 *op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
292 *op++ = M3_MARKER | 0;
293 while __lzo_unlikely(m_len > 255)
299 *op++ = LZO_BYTE(m_len);
301 *op++ = LZO_BYTE(m_off << 2);
302 *op++ = LZO_BYTE(m_off >> 6);
307 if (m_len <= M4_MAX_LEN)
308 *op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8) | (m_len - 2));
312 *op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8));
313 while __lzo_unlikely(m_len > 255)
319 *op++ = LZO_BYTE(m_len);
321 *op++ = LZO_BYTE(m_off << 2);
322 *op++ = LZO_BYTE(m_off >> 6);
327 *out_len = pd(op, out);
328 return pd(in_end,ii-ti);
332 /***********************************************************************
333 // public entry point
334 ************************************************************************/
337 DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
338 lzo_bytep out, lzo_uintp out_len,
341 const lzo_bytep ip = in;
349 lzo_uintptr_t ll_end;
350 #if 0 || (LZO_DETERMINISTIC)
351 ll = LZO_MIN(ll, 49152);
353 ll_end = (lzo_uintptr_t)ip + ll;
354 if ((ll_end + ((t + ll) >> 5)) <= ll_end || (const lzo_bytep)(ll_end + ((t + ll) >> 5)) <= ip + ll)
356 #if (LZO_DETERMINISTIC)
357 lzo_memset(wrkmem, 0, ((lzo_uint)1 << D_BITS) * sizeof(lzo_dict_t));
359 t = do_compress(ip,ll,op,out_len,t,wrkmem);
368 const lzo_bytep ii = in + in_len - t;
370 if (op == out && t <= 238)
371 *op++ = LZO_BYTE(17 + t);
373 op[-2] = LZO_BYTE(op[-2] | t);
375 *op++ = LZO_BYTE(t - 3);
378 lzo_uint tt = t - 18;
388 *op++ = LZO_BYTE(tt);
394 *op++ = M4_MARKER | 1;
398 *out_len = pd(op, out);