1 /* lzo1x_d.ch -- implementation of the LZO1X decompression 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/
47 /***********************************************************************
48 // decompress a block of data.
49 ************************************************************************/
51 #if defined(DO_DECOMPRESS)
53 DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
54 lzo_bytep out, lzo_uintp out_len,
58 register lzo_bytep op;
59 register const lzo_bytep ip;
61 #if defined(COPY_DICT)
63 const lzo_bytep dict_end;
65 register const lzo_bytep m_pos;
68 const lzo_bytep const ip_end = in + in_len;
69 #if defined(HAVE_ANY_OP)
70 lzo_bytep const op_end = out + *out_len;
73 lzo_uint last_m_off = 0;
78 #if defined(COPY_DICT)
81 if (dict_len > M4_MAX_OFFSET)
83 dict += dict_len - M4_MAX_OFFSET;
84 dict_len = M4_MAX_OFFSET;
86 dict_end = dict + dict_len;
93 #endif /* COPY_DICT */
105 assert(t > 0); NEED_OP(t); NEED_IP(t+1);
106 do *op++ = *ip++; while (--t > 0);
107 goto first_literal_run;
110 while (TEST_IP && TEST_OP)
128 assert(t > 0); NEED_OP(t+3); NEED_IP(t+4);
129 #if defined(LZO_UNALIGNED_OK_8) && defined(LZO_UNALIGNED_OK_4)
134 op += 8; ip += 8; t -= 8;
139 op += 4; ip += 4; t -= 4;
144 if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
146 #elif defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
147 #if !defined(LZO_UNALIGNED_OK_4)
148 if (PTR_ALIGNED2_4(op,ip))
159 op += 4; ip += 4; t -= 4;
161 if (t > 0) do *op++ = *ip++; while (--t > 0);
164 do *op++ = *ip++; while (--t > 0);
166 #if !defined(LZO_UNALIGNED_OK_4)
171 #if !defined(LZO_UNALIGNED_OK_4) && !defined(LZO_UNALIGNED_OK_8)
173 *op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
174 do *op++ = *ip++; while (--t > 0);
185 #if defined(COPY_DICT)
187 m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
190 m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
193 t = 3; COPY_DICT(t,m_off)
194 #else /* !COPY_DICT */
196 t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
200 m_pos = op - (1 + M2_MAX_OFFSET);
204 TEST_LB(m_pos); NEED_OP(3);
205 *op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
206 #endif /* COPY_DICT */
213 if (t >= 64) /* a M2 match */
215 #if defined(COPY_DICT)
217 m_off = 1 + ((t >> 2) & 7) + (*ip++ << 3);
220 m_off = 1 + ((t >> 2) & 3) + (*ip++ << 2);
228 m_off = 1 + (m_off << 6) + (*ip++ >> 2);
233 #else /* !COPY_DICT */
236 m_pos -= (t >> 2) & 7;
241 m_pos -= (t >> 2) & 3;
246 lzo_uint off = t & 0x1f;
250 assert(last_m_off > 0);
255 off = 1 + (off << 6) + (*ip++ >> 2);
262 TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
264 #endif /* COPY_DICT */
266 else if (t >= 32) /* a M3 match */
280 #if defined(COPY_DICT)
282 m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
285 m_off = 1 + (ip[0] >> 2) + (ip[1] << 6);
287 #else /* !COPY_DICT */
290 lzo_uint off = 1 + (ip[0] << 6) + (ip[1] >> 2);
294 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
296 m_pos -= UA_GET16(ip) >> 2;
299 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
301 #endif /* COPY_DICT */
304 else if (t >= 16) /* a M4 match */
306 #if defined(COPY_DICT)
307 m_off = (t & 8) << 11;
308 #else /* !COPY_DICT */
310 m_pos -= (t & 8) << 11;
311 #endif /* COPY_DICT */
324 #if defined(COPY_DICT)
326 m_off += (ip[0] << 6) + (ip[1] >> 2);
328 m_off += (ip[0] >> 2) + (ip[1] << 6);
337 #else /* !COPY_DICT */
339 m_pos -= (ip[0] << 6) + (ip[1] >> 2);
340 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
341 m_pos -= UA_GET16(ip) >> 2;
343 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
350 last_m_off = pd((const lzo_bytep)op, m_pos);
352 #endif /* COPY_DICT */
354 else /* a M1 match */
356 #if defined(COPY_DICT)
358 m_off = 1 + (t << 6) + (*ip++ >> 2);
361 m_off = 1 + (t >> 2) + (*ip++ << 2);
364 t = 2; COPY_DICT(t,m_off)
365 #else /* !COPY_DICT */
367 t = 1 + (t << 6) + (*ip++ >> 2);
375 TEST_LB(m_pos); NEED_OP(2);
376 *op++ = *m_pos++; *op++ = *m_pos;
377 #endif /* COPY_DICT */
382 #if defined(COPY_DICT)
385 t += 3-1; COPY_DICT(t,m_off)
387 #else /* !COPY_DICT */
389 TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
390 #if defined(LZO_UNALIGNED_OK_8) && defined(LZO_UNALIGNED_OK_4)
397 op += 8; m_pos += 8; t -= 8;
402 op += 4; m_pos += 4; t -= 4;
407 if (t > 1) { *op++ = m_pos[1]; if (t > 2) { *op++ = m_pos[2]; } }
411 #elif defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
412 #if !defined(LZO_UNALIGNED_OK_4)
413 if (t >= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op,m_pos))
415 assert((op - m_pos) >= 4); /* both pointers are aligned */
417 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
421 op += 4; m_pos += 4; t -= 4 - (3 - 1);
424 op += 4; m_pos += 4; t -= 4;
426 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
432 *op++ = *m_pos++; *op++ = *m_pos++;
433 do *op++ = *m_pos++; while (--t > 0);
436 #endif /* COPY_DICT */
449 assert(t > 0); assert(t < 4); NEED_OP(t); NEED_IP(t+1);
451 do *op++ = *ip++; while (--t > 0);
454 if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
457 } while (TEST_IP && TEST_OP);
460 #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
461 /* no EOF code was found */
462 *out_len = pd(op, out);
463 return LZO_E_EOF_NOT_FOUND;
468 *out_len = pd(op, out);
469 return (ip == ip_end ? LZO_E_OK :
470 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
473 #if defined(HAVE_NEED_IP)
475 *out_len = pd(op, out);
476 return LZO_E_INPUT_OVERRUN;
479 #if defined(HAVE_NEED_OP)
481 *out_len = pd(op, out);
482 return LZO_E_OUTPUT_OVERRUN;
485 #if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
487 *out_len = pd(op, out);
488 return LZO_E_LOOKBEHIND_OVERRUN;