Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * LZO decompressor for the Linux kernel. Code borrowed from the lzo |
| 3 | * implementation by Markus Franz Xaver Johannes Oberhumer. |
| 4 | * |
| 5 | * Linux kernel adaptation: |
| 6 | * Copyright (C) 2009 |
| 7 | * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com> |
| 8 | * |
| 9 | * Original code: |
| 10 | * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer |
| 11 | * All Rights Reserved. |
| 12 | * |
| 13 | * lzop and the LZO library are free software; you can redistribute them |
| 14 | * and/or modify them under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 2 of |
| 16 | * the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; see the file COPYING. |
| 25 | * If not, write to the Free Software Foundation, Inc., |
| 26 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | * |
| 28 | * Markus F.X.J. Oberhumer |
| 29 | * <markus@oberhumer.com> |
| 30 | * http://www.oberhumer.com/opensource/lzop/ |
| 31 | */ |
| 32 | |
| 33 | #ifdef STATIC |
| 34 | #include "lzo/lzo1x_decompress.c" |
| 35 | #else |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 36 | #include <linux/decompress/unlzo.h> |
| 37 | #endif |
| 38 | |
| 39 | #include <linux/types.h> |
| 40 | #include <linux/lzo.h> |
| 41 | #include <linux/decompress/mm.h> |
| 42 | |
| 43 | #include <linux/compiler.h> |
| 44 | #include <asm/unaligned.h> |
| 45 | |
| 46 | static const unsigned char lzop_magic[] = { |
| 47 | 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a }; |
| 48 | |
| 49 | #define LZO_BLOCK_SIZE (256*1024l) |
| 50 | #define HEADER_HAS_FILTER 0x00000800L |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 51 | #define HEADER_SIZE_MIN (9 + 7 + 4 + 8 + 1 + 4) |
| 52 | #define HEADER_SIZE_MAX (9 + 7 + 1 + 8 + 8 + 4 + 1 + 255 + 4) |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 53 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 54 | STATIC inline int INIT parse_header(u8 *input, int *skip, int in_len) |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 55 | { |
| 56 | int l; |
| 57 | u8 *parse = input; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 58 | u8 *end = input + in_len; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 59 | u8 level = 0; |
| 60 | u16 version; |
| 61 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 62 | /* |
| 63 | * Check that there's enough input to possibly have a valid header. |
| 64 | * Then it is possible to parse several fields until the minimum |
| 65 | * size may have been used. |
| 66 | */ |
| 67 | if (in_len < HEADER_SIZE_MIN) |
| 68 | return 0; |
| 69 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 70 | /* read magic: 9 first bits */ |
| 71 | for (l = 0; l < 9; l++) { |
| 72 | if (*parse++ != lzop_magic[l]) |
| 73 | return 0; |
| 74 | } |
| 75 | /* get version (2bytes), skip library version (2), |
| 76 | * 'need to be extracted' version (2) and |
| 77 | * method (1) */ |
| 78 | version = get_unaligned_be16(parse); |
| 79 | parse += 7; |
| 80 | if (version >= 0x0940) |
| 81 | level = *parse++; |
| 82 | if (get_unaligned_be32(parse) & HEADER_HAS_FILTER) |
| 83 | parse += 8; /* flags + filter info */ |
| 84 | else |
| 85 | parse += 4; /* flags */ |
| 86 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 87 | /* |
| 88 | * At least mode, mtime_low, filename length, and checksum must |
| 89 | * be left to be parsed. If also mtime_high is present, it's OK |
| 90 | * because the next input buffer check is after reading the |
| 91 | * filename length. |
| 92 | */ |
| 93 | if (end - parse < 8 + 1 + 4) |
| 94 | return 0; |
| 95 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 96 | /* skip mode and mtime_low */ |
| 97 | parse += 8; |
| 98 | if (version >= 0x0940) |
| 99 | parse += 4; /* skip mtime_high */ |
| 100 | |
| 101 | l = *parse++; |
| 102 | /* don't care about the file name, and skip checksum */ |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 103 | if (end - parse < l + 4) |
| 104 | return 0; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 105 | parse += l + 4; |
| 106 | |
| 107 | *skip = parse - input; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | STATIC inline int INIT unlzo(u8 *input, int in_len, |
| 112 | int (*fill) (void *, unsigned int), |
| 113 | int (*flush) (void *, unsigned int), |
| 114 | u8 *output, int *posp, |
Lasse Collin | 93685ad2 | 2011-01-12 17:01:14 -0800 | [diff] [blame] | 115 | void (*error) (char *x)) |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 116 | { |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 117 | u8 r = 0; |
| 118 | int skip = 0; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 119 | u32 src_len, dst_len; |
| 120 | size_t tmp; |
| 121 | u8 *in_buf, *in_buf_save, *out_buf; |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 122 | int ret = -1; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 123 | |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 124 | if (output) { |
| 125 | out_buf = output; |
| 126 | } else if (!flush) { |
| 127 | error("NULL output pointer and no flush function provided"); |
| 128 | goto exit; |
| 129 | } else { |
| 130 | out_buf = malloc(LZO_BLOCK_SIZE); |
| 131 | if (!out_buf) { |
| 132 | error("Could not allocate output buffer"); |
| 133 | goto exit; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (input && fill) { |
| 138 | error("Both input pointer and fill function provided, don't know what to do"); |
| 139 | goto exit_1; |
| 140 | } else if (input) { |
| 141 | in_buf = input; |
| 142 | } else if (!fill || !posp) { |
| 143 | error("NULL input pointer and missing position pointer or fill function"); |
| 144 | goto exit_1; |
| 145 | } else { |
| 146 | in_buf = malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE)); |
| 147 | if (!in_buf) { |
| 148 | error("Could not allocate input buffer"); |
| 149 | goto exit_1; |
| 150 | } |
| 151 | } |
| 152 | in_buf_save = in_buf; |
| 153 | |
| 154 | if (posp) |
| 155 | *posp = 0; |
| 156 | |
| 157 | if (fill) |
| 158 | fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE)); |
| 159 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 160 | if (!parse_header(input, &skip, in_len)) { |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 161 | error("invalid header"); |
| 162 | goto exit_2; |
| 163 | } |
| 164 | in_buf += skip; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 165 | in_len -= skip; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 166 | |
| 167 | if (posp) |
| 168 | *posp = skip; |
| 169 | |
| 170 | for (;;) { |
| 171 | /* read uncompressed block size */ |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 172 | if (in_len < 4) { |
| 173 | error("file corrupted"); |
| 174 | goto exit_2; |
| 175 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 176 | dst_len = get_unaligned_be32(in_buf); |
| 177 | in_buf += 4; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 178 | in_len -= 4; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 179 | |
| 180 | /* exit if last block */ |
| 181 | if (dst_len == 0) { |
| 182 | if (posp) |
| 183 | *posp += 4; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | if (dst_len > LZO_BLOCK_SIZE) { |
| 188 | error("dest len longer than block size"); |
| 189 | goto exit_2; |
| 190 | } |
| 191 | |
| 192 | /* read compressed block size, and skip block checksum info */ |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 193 | if (in_len < 8) { |
| 194 | error("file corrupted"); |
| 195 | goto exit_2; |
| 196 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 197 | src_len = get_unaligned_be32(in_buf); |
| 198 | in_buf += 8; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 199 | in_len -= 8; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 200 | |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 201 | if (src_len <= 0 || src_len > dst_len || src_len > in_len) { |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 202 | error("file corrupted"); |
| 203 | goto exit_2; |
| 204 | } |
| 205 | |
| 206 | /* decompress */ |
| 207 | tmp = dst_len; |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 208 | |
| 209 | /* When the input data is not compressed at all, |
| 210 | * lzo1x_decompress_safe will fail, so call memcpy() |
| 211 | * instead */ |
| 212 | if (unlikely(dst_len == src_len)) |
| 213 | memcpy(out_buf, in_buf, src_len); |
| 214 | else { |
| 215 | r = lzo1x_decompress_safe((u8 *) in_buf, src_len, |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 216 | out_buf, &tmp); |
| 217 | |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 218 | if (r != LZO_E_OK || dst_len != tmp) { |
| 219 | error("Compressed data violation"); |
| 220 | goto exit_2; |
| 221 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Lasse Collin | 8f9b54a | 2011-01-12 17:01:20 -0800 | [diff] [blame] | 224 | if (flush && flush(out_buf, dst_len) != dst_len) |
| 225 | goto exit_2; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 226 | if (output) |
| 227 | out_buf += dst_len; |
| 228 | if (posp) |
| 229 | *posp += src_len + 12; |
| 230 | if (fill) { |
| 231 | in_buf = in_buf_save; |
| 232 | fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE)); |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 233 | } else { |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 234 | in_buf += src_len; |
Lasse Collin | 5a3f81a | 2011-01-12 17:01:21 -0800 | [diff] [blame^] | 235 | in_len -= src_len; |
| 236 | } |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 239 | ret = 0; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 240 | exit_2: |
| 241 | if (!input) |
| 242 | free(in_buf); |
| 243 | exit_1: |
| 244 | if (!output) |
| 245 | free(out_buf); |
| 246 | exit: |
Albin Tonnerre | ccdb400 | 2010-04-23 13:17:58 -0400 | [diff] [blame] | 247 | return ret; |
Albin Tonnerre | 7dd65fe | 2010-01-08 14:42:42 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | #define decompress unlzo |