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
,
35 void* wrkmem UNUSED_PARAM
)
38 register const uint8_t* ip
;
40 #if defined(COPY_DICT)
42 const uint8_t* dict_end
;
44 register const uint8_t* m_pos
= NULL
; /* possibly not needed */
46 const uint8_t* const ip_end
= in
+ in_len
;
47 #if defined(HAVE_ANY_OP)
48 uint8_t* const op_end
= out
+ *out_len
;
51 unsigned last_m_off
= 0;
54 // LZO_UNUSED(wrkmem);
56 #if defined(COPY_DICT)
58 if (dict_len
> M4_MAX_OFFSET
) {
59 dict
+= dict_len
- M4_MAX_OFFSET
;
60 dict_len
= M4_MAX_OFFSET
;
62 dict_end
= dict
+ dict_len
;
67 #endif /* COPY_DICT */
78 assert(t
> 0); NEED_OP(t
); NEED_IP(t
+1);
79 do *op
++ = *ip
++; while (--t
> 0);
80 goto first_literal_run
;
83 while (TEST_IP
&& TEST_OP
) {
102 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
103 # if !defined(LZO_UNALIGNED_OK_4)
104 if (PTR_ALIGNED2_4(op
, ip
))
119 do *op
++ = *ip
++; while (--t
> 0);
121 do *op
++ = *ip
++; while (--t
> 0);
125 # if !defined(LZO_UNALIGNED_OK_4)
129 #if !defined(LZO_UNALIGNED_OK_4)
134 do *op
++ = *ip
++; while (--t
> 0);
142 #if defined(COPY_DICT)
144 m_off
= (1 + M2_MAX_OFFSET
) + (t
<< 6) + (*ip
++ >> 2);
147 m_off
= (1 + M2_MAX_OFFSET
) + (t
>> 2) + (*ip
++ << 2);
150 t
= 3; COPY_DICT(t
,m_off
)
151 #else /* !COPY_DICT */
153 t
= (1 + M2_MAX_OFFSET
) + (t
<< 6) + (*ip
++ >> 2);
157 m_pos
= op
- (1 + M2_MAX_OFFSET
);
161 TEST_LB(m_pos
); NEED_OP(3);
165 #endif /* COPY_DICT */
171 if (t
>= 64) { /* a M2 match */
172 #if defined(COPY_DICT)
174 m_off
= 1 + ((t
>> 2) & 7) + (*ip
++ << 3);
177 m_off
= 1 + ((t
>> 2) & 3) + (*ip
++ << 2);
184 m_off
= 1 + (m_off
<< 6) + (*ip
++ >> 2);
189 #else /* !COPY_DICT */
192 m_pos
-= (t
>> 2) & 7;
197 m_pos
-= (t
>> 2) & 3;
202 unsigned off
= t
& 0x1f;
205 assert(last_m_off
> 0);
208 off
= 1 + (off
<< 6) + (*ip
++ >> 2);
215 TEST_LB(m_pos
); assert(t
> 0); NEED_OP(t
+3-1);
217 #endif /* COPY_DICT */
219 else if (t
>= 32) { /* a M3 match */
231 #if defined(COPY_DICT)
233 m_off
= 1 + (ip
[0] << 6) + (ip
[1] >> 2);
236 m_off
= 1 + (ip
[0] >> 2) + (ip
[1] << 6);
238 #else /* !COPY_DICT */
241 unsigned off
= 1 + (ip
[0] << 6) + (ip
[1] >> 2);
245 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
247 m_pos
-= (* (const lzo_ushortp
) ip
) >> 2;
250 m_pos
-= (ip
[0] >> 2) + (ip
[1] << 6);
252 #endif /* COPY_DICT */
255 else if (t
>= 16) { /* a M4 match */
256 #if defined(COPY_DICT)
257 m_off
= (t
& 8) << 11;
258 #else /* !COPY_DICT */
260 m_pos
-= (t
& 8) << 11;
261 #endif /* COPY_DICT */
273 #if defined(COPY_DICT)
275 m_off
+= (ip
[0] << 6) + (ip
[1] >> 2);
277 m_off
+= (ip
[0] >> 2) + (ip
[1] << 6);
286 #else /* !COPY_DICT */
288 m_pos
-= (ip
[0] << 6) + (ip
[1] >> 2);
289 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
290 m_pos
-= (* (const lzo_ushortp
) ip
) >> 2;
292 m_pos
-= (ip
[0] >> 2) + (ip
[1] << 6);
299 last_m_off
= pd((const uint8_t*)op
, m_pos
);
301 #endif /* COPY_DICT */
303 else { /* a M1 match */
304 #if defined(COPY_DICT)
306 m_off
= 1 + (t
<< 6) + (*ip
++ >> 2);
309 m_off
= 1 + (t
>> 2) + (*ip
++ << 2);
312 t
= 2; COPY_DICT(t
,m_off
)
313 #else /* !COPY_DICT */
315 t
= 1 + (t
<< 6) + (*ip
++ >> 2);
323 TEST_LB(m_pos
); NEED_OP(2);
326 #endif /* COPY_DICT */
331 #if defined(COPY_DICT)
334 t
+= 3-1; COPY_DICT(t
,m_off
)
336 #else /* !COPY_DICT */
338 TEST_LB(m_pos
); assert(t
> 0); NEED_OP(t
+3-1);
339 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
340 # if !defined(LZO_UNALIGNED_OK_4)
341 if (t
>= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op
,m_pos
)) {
342 assert((op
- m_pos
) >= 4); /* both pointers are aligned */
344 if (t
>= 2 * 4 - (3 - 1) && (op
- m_pos
) >= 4) {
347 op
+= 4; m_pos
+= 4; t
-= 4 - (3 - 1);
350 op
+= 4; m_pos
+= 4; t
-= 4;
353 do *op
++ = *m_pos
++; while (--t
> 0);
359 *op
++ = *m_pos
++; *op
++ = *m_pos
++;
360 do *op
++ = *m_pos
++; while (--t
> 0);
363 #endif /* COPY_DICT */
381 do *op
++ = *ip
++; while (--t
> 0);
391 } while (TEST_IP
&& TEST_OP
);
394 //#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
395 /* no EOF code was found */
396 *out_len
= pd(op
, out
);
397 return LZO_E_EOF_NOT_FOUND
;
402 *out_len
= pd(op
, out
);
403 return (ip
== ip_end
? LZO_E_OK
:
404 (ip
< ip_end
? LZO_E_INPUT_NOT_CONSUMED
: LZO_E_INPUT_OVERRUN
));
406 //#if defined(HAVE_NEED_IP)
408 *out_len
= pd(op
, out
);
409 return LZO_E_INPUT_OVERRUN
;
412 //#if defined(HAVE_NEED_OP)
414 *out_len
= pd(op
, out
);
415 return LZO_E_OUTPUT_OVERRUN
;
418 //#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
420 *out_len
= pd(op
, out
);
421 return LZO_E_LOOKBEHIND_OVERRUN
;