1 /* implementation of the LZO1X decompression algorithm
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996..2008 Markus Franz Xaver Johannes Oberhumer
8 Markus F.X.J. Oberhumer <markus@oberhumer.com>
9 http://www.oberhumer.com/opensource/lzo/
11 The LZO library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 The LZO library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with the LZO library; see the file COPYING.
23 If not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 /***********************************************************************
30 // decompress a block of data.
31 ************************************************************************/
32 /* safe decompression with overrun testing */
33 int lzo1x_decompress_safe(const uint8_t* in, unsigned in_len,
34 uint8_t* out, unsigned* out_len /*, void* wrkmem */)
37 register const uint8_t* ip;
39 #if defined(COPY_DICT)
41 const uint8_t* dict_end;
43 register const uint8_t* m_pos = NULL; /* possibly not needed */
45 const uint8_t* const ip_end = in + in_len;
46 #if defined(HAVE_ANY_OP)
47 uint8_t* const op_end = out + *out_len;
50 unsigned last_m_off = 0;
53 // LZO_UNUSED(wrkmem);
55 #if defined(COPY_DICT)
57 if (dict_len > M4_MAX_OFFSET) {
58 dict += dict_len - M4_MAX_OFFSET;
59 dict_len = M4_MAX_OFFSET;
61 dict_end = dict + dict_len;
66 #endif /* COPY_DICT */
77 assert(t > 0); NEED_OP(t); NEED_IP(t+1);
78 do *op++ = *ip++; while (--t > 0);
79 goto first_literal_run;
82 while (TEST_IP && TEST_OP) {
101 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
102 # if !defined(LZO_UNALIGNED_OK_4)
103 if (PTR_ALIGNED2_4(op, ip))
118 do *op++ = *ip++; while (--t > 0);
120 do *op++ = *ip++; while (--t > 0);
124 # if !defined(LZO_UNALIGNED_OK_4)
128 #if !defined(LZO_UNALIGNED_OK_4)
133 do *op++ = *ip++; while (--t > 0);
141 #if defined(COPY_DICT)
143 m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
146 m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
149 t = 3; COPY_DICT(t,m_off)
150 #else /* !COPY_DICT */
152 t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
156 m_pos = op - (1 + M2_MAX_OFFSET);
160 TEST_LB(m_pos); NEED_OP(3);
164 #endif /* COPY_DICT */
170 if (t >= 64) { /* a M2 match */
171 #if defined(COPY_DICT)
173 m_off = 1 + ((t >> 2) & 7) + (*ip++ << 3);
176 m_off = 1 + ((t >> 2) & 3) + (*ip++ << 2);
183 m_off = 1 + (m_off << 6) + (*ip++ >> 2);
188 #else /* !COPY_DICT */
191 m_pos -= (t >> 2) & 7;
196 m_pos -= (t >> 2) & 3;
201 unsigned off = t & 0x1f;
204 assert(last_m_off > 0);
207 off = 1 + (off << 6) + (*ip++ >> 2);
214 TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
216 #endif /* COPY_DICT */
218 else if (t >= 32) { /* a M3 match */
230 #if defined(COPY_DICT)
232 m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
235 m_off = 1 + (ip[0] >> 2) + (ip[1] << 6);
237 #else /* !COPY_DICT */
240 unsigned off = 1 + (ip[0] << 6) + (ip[1] >> 2);
244 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
246 m_pos -= (* (const lzo_ushortp) ip) >> 2;
249 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
251 #endif /* COPY_DICT */
254 else if (t >= 16) { /* a M4 match */
255 #if defined(COPY_DICT)
256 m_off = (t & 8) << 11;
257 #else /* !COPY_DICT */
259 m_pos -= (t & 8) << 11;
260 #endif /* COPY_DICT */
272 #if defined(COPY_DICT)
274 m_off += (ip[0] << 6) + (ip[1] >> 2);
276 m_off += (ip[0] >> 2) + (ip[1] << 6);
285 #else /* !COPY_DICT */
287 m_pos -= (ip[0] << 6) + (ip[1] >> 2);
288 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
289 m_pos -= (* (const lzo_ushortp) ip) >> 2;
291 m_pos -= (ip[0] >> 2) + (ip[1] << 6);
298 last_m_off = pd((const uint8_t*)op, m_pos);
300 #endif /* COPY_DICT */
302 else { /* a M1 match */
303 #if defined(COPY_DICT)
305 m_off = 1 + (t << 6) + (*ip++ >> 2);
308 m_off = 1 + (t >> 2) + (*ip++ << 2);
311 t = 2; COPY_DICT(t,m_off)
312 #else /* !COPY_DICT */
314 t = 1 + (t << 6) + (*ip++ >> 2);
322 TEST_LB(m_pos); NEED_OP(2);
325 #endif /* COPY_DICT */
330 #if defined(COPY_DICT)
333 t += 3-1; COPY_DICT(t,m_off)
335 #else /* !COPY_DICT */
337 TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
338 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
339 # if !defined(LZO_UNALIGNED_OK_4)
340 if (t >= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op,m_pos)) {
341 assert((op - m_pos) >= 4); /* both pointers are aligned */
343 if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4) {
346 op += 4; m_pos += 4; t -= 4 - (3 - 1);
349 op += 4; m_pos += 4; t -= 4;
352 do *op++ = *m_pos++; while (--t > 0);
358 *op++ = *m_pos++; *op++ = *m_pos++;
359 do *op++ = *m_pos++; while (--t > 0);
362 #endif /* COPY_DICT */
380 do *op++ = *ip++; while (--t > 0);
390 } while (TEST_IP && TEST_OP);
393 //#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
394 /* no EOF code was found */
395 *out_len = pd(op, out);
396 return LZO_E_EOF_NOT_FOUND;
401 *out_len = pd(op, out);
402 return (ip == ip_end ? LZO_E_OK :
403 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
405 //#if defined(HAVE_NEED_IP)
407 *out_len = pd(op, out);
408 return LZO_E_INPUT_OVERRUN;
411 //#if defined(HAVE_NEED_OP)
413 *out_len = pd(op, out);
414 return LZO_E_OUTPUT_OVERRUN;
417 //#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
419 *out_len = pd(op, out);
420 return LZO_E_LOOKBEHIND_OVERRUN;