1 // SPDX-License-Identifier: GPL-2.0-only
3 * LZO1X Decompressor from LZO
5 * Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
7 * The full LZO package can be found at:
8 * http://www.oberhumer.com/opensource/lzo/
10 * Changed for Linux kernel use by:
11 * Nitin Gupta <nitingupta910@gmail.com>
12 * Richard Purdie <rpurdie@openedhand.com>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
19 #include <asm/unaligned.h>
20 #include <linux/lzo.h>
23 #define HAVE_IP(x) ((size_t)(ip_end - ip) >= (size_t)(x))
24 #define HAVE_OP(x) ((size_t)(op_end - op) >= (size_t)(x))
25 #define NEED_IP(x) if (!HAVE_IP(x)) goto input_overrun
26 #define NEED_OP(x) if (!HAVE_OP(x)) goto output_overrun
27 #define TEST_LB(m_pos) if ((m_pos) < out) goto lookbehind_overrun
29 /* This MAX_255_COUNT is the maximum number of times we can add 255 to a base
30 * count without overflowing an integer. The multiply will overflow when
31 * multiplying 255 by more than MAXINT/255. The sum will overflow earlier
32 * depending on the base count. Since the base count is taken from a u8
33 * and a few bits, it is safe to assume that it will always be lower than
34 * or equal to 2*255, thus we can always prevent any overflow by accepting
35 * two less 255 steps. See Documentation/staging/lzo.rst for more information.
37 #define MAX_255_COUNT ((((size_t)~0) / 255) - 2)
39 int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
40 unsigned char *out, size_t *out_len)
43 const unsigned char *ip;
46 const unsigned char *m_pos;
47 const unsigned char * const ip_end = in + in_len;
48 unsigned char * const op_end = out + *out_len;
50 unsigned char bitstream_version;
55 if (unlikely(in_len < 3))
58 if (likely(in_len >= 5) && likely(*ip == 17)) {
59 bitstream_version = ip[1];
62 bitstream_version = 0;
71 goto copy_literal_run;
77 if (likely(state == 0)) {
78 if (unlikely(t == 0)) {
80 const unsigned char *ip_last = ip;
82 while (unlikely(*ip == 0)) {
86 offset = ip - ip_last;
87 if (unlikely(offset > MAX_255_COUNT))
90 offset = (offset << 8) - offset;
91 t += offset + 15 + *ip++;
95 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
96 if (likely(HAVE_IP(t + 15) && HAVE_OP(t + 15))) {
97 const unsigned char *ie = ip + t;
98 unsigned char *oe = op + t;
120 } else if (state != 4) {
133 m_pos = op - (1 + M2_MAX_OFFSET);
138 } else if (t >= 64) {
141 m_pos -= (t >> 2) & 7;
143 t = (t >> 5) - 1 + (3 - 1);
144 } else if (t >= 32) {
145 t = (t & 31) + (3 - 1);
146 if (unlikely(t == 2)) {
148 const unsigned char *ip_last = ip;
150 while (unlikely(*ip == 0)) {
154 offset = ip - ip_last;
155 if (unlikely(offset > MAX_255_COUNT))
158 offset = (offset << 8) - offset;
159 t += offset + 31 + *ip++;
163 next = get_unaligned_le16(ip);
169 next = get_unaligned_le16(ip);
170 if (((next & 0xfffc) == 0xfffc) &&
171 ((t & 0xf8) == 0x18) &&
172 likely(bitstream_version)) {
176 t += MIN_ZERO_RUN_LENGTH;
185 m_pos -= (t & 8) << 11;
186 t = (t & 7) + (3 - 1);
187 if (unlikely(t == 2)) {
189 const unsigned char *ip_last = ip;
191 while (unlikely(*ip == 0)) {
195 offset = ip - ip_last;
196 if (unlikely(offset > MAX_255_COUNT))
199 offset = (offset << 8) - offset;
200 t += offset + 7 + *ip++;
202 next = get_unaligned_le16(ip);
213 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
214 if (op - m_pos >= 8) {
215 unsigned char *oe = op + t;
216 if (likely(HAVE_OP(t + 15))) {
242 unsigned char *oe = op + t;
255 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
256 if (likely(HAVE_IP(6) && HAVE_OP(4))) {
274 return (t != 3 ? LZO_E_ERROR :
275 ip == ip_end ? LZO_E_OK :
276 ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN);
280 return LZO_E_INPUT_OVERRUN;
284 return LZO_E_OUTPUT_OVERRUN;
288 return LZO_E_LOOKBEHIND_OVERRUN;
291 EXPORT_SYMBOL_GPL(lzo1x_decompress_safe);
293 MODULE_LICENSE("GPL");
294 MODULE_DESCRIPTION("LZO1X Decompressor");