1 /* lzo1b_d.ch -- implementation of the LZO1B decompression algorithm
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996-2015 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/
32 /***********************************************************************
33 // decompress a block of data.
34 ************************************************************************/
37 DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
38 lzo_bytep out, lzo_uintp out_len,
44 const lzo_bytep m_pos;
46 const lzo_bytep const ip_end = in + in_len;
47 #if defined(HAVE_ANY_OP)
48 lzo_bytep const op_end = out + *out_len;
56 while (TEST_IP_AND_TEST_OP)
58 t = *ip++; /* get marker */
60 if (t < R0MIN) /* a literal run */
62 if (t == 0) /* a R0 literal run */
66 if (t >= R0FAST - R0MIN) /* a long R0 run */
74 t = 256u << ((unsigned) t);
76 /* help the optimizer */
78 do tt <<= 1; while (--t > 0);
83 NEED_IP(t); NEED_OP(t);
84 #if 1 && (LZO_OPT_UNALIGNED32)
96 t += R0MIN; /* a short R0 run */
99 NEED_IP(t); NEED_OP(t);
100 /* copy literal run */
101 #if 1 && (LZO_OPT_UNALIGNED32)
106 op += 4; ip += 4; t -= 4;
108 if (t > 0) do *op++ = *ip++; while (--t > 0);
116 do *op++ = *ip++; while (--t > 0);
123 /* after a literal a match must follow */
124 while (TEST_IP_AND_TEST_OP)
126 t = *ip++; /* get R1 marker */
130 NEED_IP(2); NEED_OP(M2_MIN_LEN + 1);
132 /* R1 match - a M2_MIN_LEN match + 1 byte literal */
133 assert((t & M2O_MASK) == t);
134 m_pos = op - M2_MIN_OFFSET;
135 m_pos -= t | (((lzo_uint) *ip++) << M2O_BITS);
136 assert(m_pos >= out); assert(m_pos < op);
142 #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
149 if (t >= M2_MARKER) /* a M2 match */
151 /* get match offset */
153 m_pos = op - M2_MIN_OFFSET;
154 m_pos -= (t & M2O_MASK) | (((lzo_uint) *ip++) << M2O_BITS);
155 assert(m_pos >= out); assert(m_pos < op);
159 t = (t >> M2O_BITS) - 1;
160 NEED_OP(t + M2_MIN_LEN - 1);
162 MEMCPY_DS(op,m_pos,t);
164 else /* a M3 or M4 match */
168 if (t == 0) /* a M4 match */
178 t += (M4_MIN_LEN - M3_MIN_LEN) + *ip++;
181 /* get match offset */
183 m_pos = op - (M3_MIN_OFFSET - M3_EOF_OFFSET);
184 m_pos -= *ip++ & M3O_MASK;
185 m_pos -= (lzo_uint)(*ip++) << M3O_BITS;
186 #if defined(LZO_EOF_CODE)
192 assert(m_pos >= out); assert(m_pos < op);
193 TEST_LB(m_pos); NEED_OP(t + M3_MIN_LEN - 1);
194 #if (LZO_OPT_UNALIGNED32)
195 if (t >= 2 * 4 - (M3_MIN_LEN - 1) && (op - m_pos) >= 4)
198 op += 4; m_pos += 4; t -= 4 - (M3_MIN_LEN - 1);
201 op += 4; m_pos += 4; t -= 4;
203 if (t > 0) do *op++ = *m_pos++; while (--t > 0);
209 MEMCPY_DS(op,m_pos,t);
214 t = ip[-2] >> M3O_BITS;
217 NEED_IP(t); NEED_OP(t);
220 #elif (M3O_BITS == 7)
221 /* optimized version */
222 if (ip[-2] & (1 << M3O_BITS))
224 NEED_IP(1); NEED_OP(1);
233 #if defined(LZO_EOF_CODE)
234 #if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
235 /* no EOF code was found */
236 *out_len = pd(op, out);
237 return LZO_E_EOF_NOT_FOUND;
243 *out_len = pd(op, out);
244 return (ip == ip_end ? LZO_E_OK :
245 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
248 #if defined(HAVE_NEED_IP)
250 *out_len = pd(op, out);
251 return LZO_E_INPUT_OVERRUN;
254 #if defined(HAVE_NEED_OP)
256 *out_len = pd(op, out);
257 return LZO_E_OUTPUT_OVERRUN;
260 #if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
262 *out_len = pd(op, out);
263 return LZO_E_LOOKBEHIND_OVERRUN;
268 /* vim:set ts=4 sw=4 et: */