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
) {
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 */
229 #if defined(COPY_DICT)
231 m_off
= 1 + (ip
[0] << 6) + (ip
[1] >> 2);
234 m_off
= 1 + (ip
[0] >> 2) + (ip
[1] << 6);
236 #else /* !COPY_DICT */
239 unsigned off
= 1 + (ip
[0] << 6) + (ip
[1] >> 2);
243 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
245 m_pos
-= (* (const lzo_ushortp
) ip
) >> 2;
248 m_pos
-= (ip
[0] >> 2) + (ip
[1] << 6);
250 #endif /* COPY_DICT */
253 else if (t
>= 16) { /* a M4 match */
254 #if defined(COPY_DICT)
255 m_off
= (t
& 8) << 11;
256 #else /* !COPY_DICT */
258 m_pos
-= (t
& 8) << 11;
259 #endif /* COPY_DICT */
270 #if defined(COPY_DICT)
272 m_off
+= (ip
[0] << 6) + (ip
[1] >> 2);
274 m_off
+= (ip
[0] >> 2) + (ip
[1] << 6);
283 #else /* !COPY_DICT */
285 m_pos
-= (ip
[0] << 6) + (ip
[1] >> 2);
286 #elif defined(LZO_UNALIGNED_OK_2) && defined(LZO_ABI_LITTLE_ENDIAN)
287 m_pos
-= (* (const lzo_ushortp
) ip
) >> 2;
289 m_pos
-= (ip
[0] >> 2) + (ip
[1] << 6);
296 last_m_off
= pd((const uint8_t*)op
, m_pos
);
298 #endif /* COPY_DICT */
300 else { /* a M1 match */
301 #if defined(COPY_DICT)
303 m_off
= 1 + (t
<< 6) + (*ip
++ >> 2);
306 m_off
= 1 + (t
>> 2) + (*ip
++ << 2);
309 t
= 2; COPY_DICT(t
,m_off
)
310 #else /* !COPY_DICT */
312 t
= 1 + (t
<< 6) + (*ip
++ >> 2);
320 TEST_LB(m_pos
); NEED_OP(2);
323 #endif /* COPY_DICT */
328 #if defined(COPY_DICT)
331 t
+= 3-1; COPY_DICT(t
,m_off
)
333 #else /* !COPY_DICT */
335 TEST_LB(m_pos
); assert(t
> 0); NEED_OP(t
+3-1);
336 #if defined(LZO_UNALIGNED_OK_4) || defined(LZO_ALIGNED_OK_4)
337 # if !defined(LZO_UNALIGNED_OK_4)
338 if (t
>= 2 * 4 - (3 - 1) && PTR_ALIGNED2_4(op
,m_pos
)) {
339 assert((op
- m_pos
) >= 4); /* both pointers are aligned */
341 if (t
>= 2 * 4 - (3 - 1) && (op
- m_pos
) >= 4) {
344 op
+= 4; m_pos
+= 4; t
-= 4 - (3 - 1);
347 op
+= 4; m_pos
+= 4; t
-= 4;
350 do *op
++ = *m_pos
++; while (--t
> 0);
356 *op
++ = *m_pos
++; *op
++ = *m_pos
++;
357 do *op
++ = *m_pos
++; while (--t
> 0);
360 #endif /* COPY_DICT */
378 do *op
++ = *ip
++; while (--t
> 0);
388 } while (TEST_IP
&& TEST_OP
);
391 //#if defined(HAVE_TEST_IP) || defined(HAVE_TEST_OP)
392 /* no EOF code was found */
393 *out_len
= pd(op
, out
);
394 return LZO_E_EOF_NOT_FOUND
;
399 *out_len
= pd(op
, out
);
400 return (ip
== ip_end
? LZO_E_OK
:
401 (ip
< ip_end
? LZO_E_INPUT_NOT_CONSUMED
: LZO_E_INPUT_OVERRUN
));
403 //#if defined(HAVE_NEED_IP)
405 *out_len
= pd(op
, out
);
406 return LZO_E_INPUT_OVERRUN
;
409 //#if defined(HAVE_NEED_OP)
411 *out_len
= pd(op
, out
);
412 return LZO_E_OUTPUT_OVERRUN
;
415 //#if defined(LZO_TEST_OVERRUN_LOOKBEHIND)
417 *out_len
= pd(op
, out
);
418 return LZO_E_LOOKBEHIND_OVERRUN
;