Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 1 | /* |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 2 | * LZO1X Decompressor from LZO |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 3 | * |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 4 | * Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com> |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 5 | * |
| 6 | * The full LZO package can be found at: |
| 7 | * http://www.oberhumer.com/opensource/lzo/ |
| 8 | * |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 9 | * Changed for Linux kernel use by: |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 10 | * Nitin Gupta <nitingupta910@gmail.com> |
| 11 | * Richard Purdie <rpurdie@openedhand.com> |
| 12 | */ |
| 13 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 14 | #ifndef STATIC |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 17 | #endif |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 18 | #include <asm/unaligned.h> |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 19 | #include <linux/lzo.h> |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 20 | #include "lzodefs.h" |
| 21 | |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 22 | #define HAVE_IP(t, x) \ |
| 23 | (((size_t)(ip_end - ip) >= (size_t)(t + x)) && \ |
| 24 | (((t + x) >= t) && ((t + x) >= x))) |
| 25 | |
| 26 | #define HAVE_OP(t, x) \ |
| 27 | (((size_t)(op_end - op) >= (size_t)(t + x)) && \ |
| 28 | (((t + x) >= t) && ((t + x) >= x))) |
| 29 | |
| 30 | #define NEED_IP(t, x) \ |
| 31 | do { \ |
| 32 | if (!HAVE_IP(t, x)) \ |
| 33 | goto input_overrun; \ |
| 34 | } while (0) |
| 35 | |
| 36 | #define NEED_OP(t, x) \ |
| 37 | do { \ |
| 38 | if (!HAVE_OP(t, x)) \ |
| 39 | goto output_overrun; \ |
| 40 | } while (0) |
| 41 | |
| 42 | #define TEST_LB(m_pos) \ |
| 43 | do { \ |
| 44 | if ((m_pos) < out) \ |
| 45 | goto lookbehind_overrun; \ |
| 46 | } while (0) |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 47 | |
| 48 | int lzo1x_decompress_safe(const unsigned char *in, size_t in_len, |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 49 | unsigned char *out, size_t *out_len) |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 50 | { |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 51 | unsigned char *op; |
| 52 | const unsigned char *ip; |
| 53 | size_t t, next; |
| 54 | size_t state = 0; |
| 55 | const unsigned char *m_pos; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 56 | const unsigned char * const ip_end = in + in_len; |
| 57 | unsigned char * const op_end = out + *out_len; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 58 | |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 59 | op = out; |
| 60 | ip = in; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 61 | |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 62 | if (unlikely(in_len < 3)) |
| 63 | goto input_overrun; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 64 | if (*ip > 17) { |
| 65 | t = *ip++ - 17; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 66 | if (t < 4) { |
| 67 | next = t; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 68 | goto match_next; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 69 | } |
| 70 | goto copy_literal_run; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 73 | for (;;) { |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 74 | t = *ip++; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 75 | if (t < 16) { |
| 76 | if (likely(state == 0)) { |
| 77 | if (unlikely(t == 0)) { |
| 78 | while (unlikely(*ip == 0)) { |
| 79 | t += 255; |
| 80 | ip++; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 81 | NEED_IP(1, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 82 | } |
| 83 | t += 15 + *ip++; |
| 84 | } |
| 85 | t += 3; |
| 86 | copy_literal_run: |
| 87 | #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 88 | if (likely(HAVE_IP(t, 15) && HAVE_OP(t, 15))) { |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 89 | const unsigned char *ie = ip + t; |
| 90 | unsigned char *oe = op + t; |
| 91 | do { |
| 92 | COPY8(op, ip); |
| 93 | op += 8; |
| 94 | ip += 8; |
| 95 | COPY8(op, ip); |
| 96 | op += 8; |
| 97 | ip += 8; |
| 98 | } while (ip < ie); |
| 99 | ip = ie; |
| 100 | op = oe; |
| 101 | } else |
| 102 | #endif |
| 103 | { |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 104 | NEED_OP(t, 0); |
| 105 | NEED_IP(t, 3); |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 106 | do { |
| 107 | *op++ = *ip++; |
| 108 | } while (--t > 0); |
| 109 | } |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 110 | state = 4; |
| 111 | continue; |
| 112 | } else if (state != 4) { |
| 113 | next = t & 3; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 114 | m_pos = op - 1; |
| 115 | m_pos -= t >> 2; |
| 116 | m_pos -= *ip++ << 2; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 117 | TEST_LB(m_pos); |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 118 | NEED_OP(2, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 119 | op[0] = m_pos[0]; |
| 120 | op[1] = m_pos[1]; |
| 121 | op += 2; |
| 122 | goto match_next; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 123 | } else { |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 124 | next = t & 3; |
| 125 | m_pos = op - (1 + M2_MAX_OFFSET); |
| 126 | m_pos -= t >> 2; |
| 127 | m_pos -= *ip++ << 2; |
| 128 | t = 3; |
| 129 | } |
| 130 | } else if (t >= 64) { |
| 131 | next = t & 3; |
| 132 | m_pos = op - 1; |
| 133 | m_pos -= (t >> 2) & 7; |
| 134 | m_pos -= *ip++ << 3; |
| 135 | t = (t >> 5) - 1 + (3 - 1); |
| 136 | } else if (t >= 32) { |
| 137 | t = (t & 31) + (3 - 1); |
| 138 | if (unlikely(t == 2)) { |
| 139 | while (unlikely(*ip == 0)) { |
| 140 | t += 255; |
| 141 | ip++; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 142 | NEED_IP(1, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 143 | } |
| 144 | t += 31 + *ip++; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 145 | NEED_IP(2, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 146 | } |
| 147 | m_pos = op - 1; |
| 148 | next = get_unaligned_le16(ip); |
| 149 | ip += 2; |
| 150 | m_pos -= next >> 2; |
| 151 | next &= 3; |
| 152 | } else { |
| 153 | m_pos = op; |
| 154 | m_pos -= (t & 8) << 11; |
| 155 | t = (t & 7) + (3 - 1); |
| 156 | if (unlikely(t == 2)) { |
| 157 | while (unlikely(*ip == 0)) { |
| 158 | t += 255; |
| 159 | ip++; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 160 | NEED_IP(1, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 161 | } |
| 162 | t += 7 + *ip++; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 163 | NEED_IP(2, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 164 | } |
| 165 | next = get_unaligned_le16(ip); |
| 166 | ip += 2; |
| 167 | m_pos -= next >> 2; |
| 168 | next &= 3; |
| 169 | if (m_pos == op) |
| 170 | goto eof_found; |
| 171 | m_pos -= 0x4000; |
| 172 | } |
| 173 | TEST_LB(m_pos); |
| 174 | #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
| 175 | if (op - m_pos >= 8) { |
| 176 | unsigned char *oe = op + t; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 177 | if (likely(HAVE_OP(t, 15))) { |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 178 | do { |
| 179 | COPY8(op, m_pos); |
| 180 | op += 8; |
| 181 | m_pos += 8; |
| 182 | COPY8(op, m_pos); |
| 183 | op += 8; |
| 184 | m_pos += 8; |
| 185 | } while (op < oe); |
| 186 | op = oe; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 187 | if (HAVE_IP(6, 0)) { |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 188 | state = next; |
| 189 | COPY4(op, ip); |
| 190 | op += next; |
| 191 | ip += next; |
| 192 | continue; |
| 193 | } |
| 194 | } else { |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 195 | NEED_OP(t, 0); |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 196 | do { |
| 197 | *op++ = *m_pos++; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 198 | } while (op < oe); |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 199 | } |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 200 | } else |
| 201 | #endif |
| 202 | { |
| 203 | unsigned char *oe = op + t; |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 204 | NEED_OP(t, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 205 | op[0] = m_pos[0]; |
| 206 | op[1] = m_pos[1]; |
| 207 | op += 2; |
| 208 | m_pos += 2; |
| 209 | do { |
| 210 | *op++ = *m_pos++; |
| 211 | } while (op < oe); |
| 212 | } |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 213 | match_next: |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 214 | state = next; |
| 215 | t = next; |
| 216 | #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 217 | if (likely(HAVE_IP(6, 0) && HAVE_OP(4, 0))) { |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 218 | COPY4(op, ip); |
| 219 | op += t; |
| 220 | ip += t; |
| 221 | } else |
| 222 | #endif |
| 223 | { |
Greg Kroah-Hartman | 206a81c | 2014-06-20 22:00:53 -0700 | [diff] [blame] | 224 | NEED_IP(t, 3); |
| 225 | NEED_OP(t, 0); |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 226 | while (t > 0) { |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 227 | *op++ = *ip++; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 228 | t--; |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 229 | } |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 230 | } |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 233 | eof_found: |
| 234 | *out_len = op - out; |
Markus F.X.J. Oberhumer | 8b975bd | 2012-08-13 17:25:44 +0200 | [diff] [blame] | 235 | return (t != 3 ? LZO_E_ERROR : |
| 236 | ip == ip_end ? LZO_E_OK : |
| 237 | ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN); |
| 238 | |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 239 | input_overrun: |
| 240 | *out_len = op - out; |
| 241 | return LZO_E_INPUT_OVERRUN; |
| 242 | |
| 243 | output_overrun: |
| 244 | *out_len = op - out; |
| 245 | return LZO_E_OUTPUT_OVERRUN; |
| 246 | |
| 247 | lookbehind_overrun: |
| 248 | *out_len = op - out; |
| 249 | return LZO_E_LOOKBEHIND_OVERRUN; |
| 250 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 251 | #ifndef STATIC |
Richard Purdie | 64c70b1 | 2007-07-10 17:22:24 -0700 | [diff] [blame] | 252 | EXPORT_SYMBOL_GPL(lzo1x_decompress_safe); |
| 253 | |
| 254 | MODULE_LICENSE("GPL"); |
| 255 | MODULE_DESCRIPTION("LZO1X Decompressor"); |
| 256 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 257 | #endif |