Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of the lzop file compressor. |
| 3 | |
| 4 | Copyright (C) 1996..2003 Markus Franz Xaver Johannes Oberhumer |
| 5 | All Rights Reserved. |
| 6 | |
| 7 | Markus F.X.J. Oberhumer <markus@oberhumer.com> |
| 8 | http://www.oberhumer.com/opensource/lzop/ |
| 9 | |
| 10 | lzop and the LZO library are free software; you can redistribute them |
| 11 | and/or modify them under the terms of the GNU General Public License as |
| 12 | published by the Free Software Foundation; either version 2 of |
| 13 | the License, or (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; see the file COPYING. |
| 22 | If not, write to the Free Software Foundation, Inc., |
| 23 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | |
| 25 | "Minimalized" for busybox by Alain Knaff |
| 26 | */ |
| 27 | |
Denys Vlasenko | f6beef6 | 2013-11-14 11:39:00 +0100 | [diff] [blame] | 28 | //config:config LZOP |
| 29 | //config: bool "lzop" |
| 30 | //config: default y |
| 31 | //config: help |
| 32 | //config: Lzop compression/decompresion. |
| 33 | //config: |
| 34 | //config:config LZOP_COMPR_HIGH |
| 35 | //config: bool "lzop compression levels 7,8,9 (not very useful)" |
| 36 | //config: default n |
| 37 | //config: depends on LZOP |
| 38 | //config: help |
| 39 | //config: High levels (7,8,9) of lzop compression. These levels |
| 40 | //config: are actually slower than gzip at equivalent compression ratios |
| 41 | //config: and take up 3.2K of code. |
| 42 | |
Denys Vlasenko | ac21687 | 2013-11-14 11:38:18 +0100 | [diff] [blame] | 43 | //applet:IF_LZOP(APPLET(lzop, BB_DIR_BIN, BB_SUID_DROP)) |
| 44 | //applet:IF_LZOP(APPLET_ODDNAME(lzopcat, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, lzopcat)) |
| 45 | //applet:IF_LZOP(APPLET_ODDNAME(unlzop, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, unlzop)) |
Denys Vlasenko | 66620fa | 2013-11-14 09:53:52 +0100 | [diff] [blame] | 46 | //kbuild:lib-$(CONFIG_LZOP) += lzop.o |
| 47 | |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 48 | //usage:#define lzop_trivial_usage |
| 49 | //usage: "[-cfvd123456789CF] [FILE]..." |
| 50 | //usage:#define lzop_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 51 | //usage: " -1..9 Compression level" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 52 | //usage: "\n -d Decompress" |
| 53 | //usage: "\n -c Write to stdout" |
| 54 | //usage: "\n -f Force" |
| 55 | //usage: "\n -v Verbose" |
| 56 | //usage: "\n -F Don't store or verify checksum" |
| 57 | //usage: "\n -C Also write checksum of compressed block" |
| 58 | //usage: |
| 59 | //usage:#define lzopcat_trivial_usage |
| 60 | //usage: "[-vCF] [FILE]..." |
| 61 | //usage:#define lzopcat_full_usage "\n\n" |
| 62 | //usage: " -v Verbose" |
| 63 | //usage: "\n -F Don't store or verify checksum" |
| 64 | //usage: |
| 65 | //usage:#define unlzop_trivial_usage |
| 66 | //usage: "[-cfvCF] [FILE]..." |
| 67 | //usage:#define unlzop_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 68 | //usage: " -c Write to stdout" |
Pere Orga | 1f4447b | 2011-03-27 22:40:30 +0200 | [diff] [blame] | 69 | //usage: "\n -f Force" |
| 70 | //usage: "\n -v Verbose" |
| 71 | //usage: "\n -F Don't store or verify checksum" |
| 72 | |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 73 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 74 | #include "bb_archive.h" |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 75 | #include "liblzo_interface.h" |
| 76 | |
| 77 | /* lzo-2.03/src/lzo_ptr.h */ |
| 78 | #define pd(a,b) ((unsigned)((a)-(b))) |
| 79 | |
| 80 | #define lzo_version() LZO_VERSION |
| 81 | #define lzo_sizeof_dict_t (sizeof(uint8_t*)) |
| 82 | |
| 83 | /* lzo-2.03/include/lzo/lzo1x.h */ |
| 84 | #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t) |
| 85 | #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t) |
| 86 | #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short)) |
| 87 | |
| 88 | /* lzo-2.03/src/lzo1x_oo.c */ |
| 89 | #define NO_LIT UINT_MAX |
| 90 | |
| 91 | /**********************************************************************/ |
| 92 | static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off) |
| 93 | { |
| 94 | ip[0] = m_pos[0]; |
| 95 | if (off == 1) |
| 96 | ip[1] = m_pos[0]; |
| 97 | else |
| 98 | ip[1] = m_pos[1]; |
| 99 | } |
| 100 | |
| 101 | static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off) |
| 102 | { |
| 103 | ip[0] = m_pos[0]; |
| 104 | if (off == 1) { |
| 105 | ip[2] = ip[1] = m_pos[0]; |
| 106 | } |
| 107 | else if (off == 2) { |
| 108 | ip[1] = m_pos[1]; |
| 109 | ip[2] = m_pos[0]; |
| 110 | } |
| 111 | else { |
| 112 | ip[1] = m_pos[1]; |
| 113 | ip[2] = m_pos[2]; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /**********************************************************************/ |
| 118 | // optimize a block of data. |
| 119 | /**********************************************************************/ |
| 120 | #define TEST_IP (ip < ip_end) |
| 121 | #define TEST_OP (op <= op_end) |
| 122 | |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 123 | static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len, |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 124 | uint8_t *out, unsigned *out_len, |
| 125 | void* wrkmem UNUSED_PARAM) |
| 126 | { |
| 127 | uint8_t* op; |
| 128 | uint8_t* ip; |
| 129 | unsigned t; |
| 130 | uint8_t* m_pos; |
| 131 | uint8_t* const ip_end = in + in_len; |
| 132 | uint8_t* const op_end = out + *out_len; |
| 133 | uint8_t* litp = NULL; |
| 134 | unsigned lit = 0; |
| 135 | unsigned next_lit = NO_LIT; |
| 136 | unsigned nl; |
| 137 | unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0; |
| 138 | |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 139 | // LZO_UNUSED(wrkmem); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 140 | |
| 141 | *out_len = 0; |
| 142 | |
| 143 | op = out; |
| 144 | ip = in; |
| 145 | |
| 146 | if (*ip > 17) { |
| 147 | t = *ip++ - 17; |
| 148 | if (t < 4) |
| 149 | goto match_next; |
| 150 | goto first_literal_run; |
| 151 | } |
| 152 | |
| 153 | while (TEST_IP && TEST_OP) { |
| 154 | t = *ip++; |
| 155 | if (t >= 16) |
| 156 | goto match; |
| 157 | /* a literal run */ |
| 158 | litp = ip - 1; |
| 159 | if (t == 0) { |
| 160 | t = 15; |
| 161 | while (*ip == 0) |
| 162 | t += 255, ip++; |
| 163 | t += *ip++; |
| 164 | } |
| 165 | lit = t + 3; |
| 166 | /* copy literals */ |
| 167 | copy_literal_run: |
| 168 | *op++ = *ip++; |
| 169 | *op++ = *ip++; |
| 170 | *op++ = *ip++; |
| 171 | first_literal_run: |
| 172 | do *op++ = *ip++; while (--t > 0); |
| 173 | |
| 174 | t = *ip++; |
| 175 | |
| 176 | if (t >= 16) |
| 177 | goto match; |
| 178 | #if defined(LZO1X) |
| 179 | m_pos = op - 1 - 0x800; |
| 180 | #elif defined(LZO1Y) |
| 181 | m_pos = op - 1 - 0x400; |
| 182 | #endif |
| 183 | m_pos -= t >> 2; |
| 184 | m_pos -= *ip++ << 2; |
| 185 | *op++ = *m_pos++; |
| 186 | *op++ = *m_pos++; |
| 187 | *op++ = *m_pos++; |
| 188 | lit = 0; |
| 189 | goto match_done; |
| 190 | |
| 191 | |
| 192 | /* handle matches */ |
| 193 | do { |
| 194 | if (t < 16) { /* a M1 match */ |
| 195 | m_pos = op - 1; |
| 196 | m_pos -= t >> 2; |
| 197 | m_pos -= *ip++ << 2; |
| 198 | |
| 199 | if (litp == NULL) |
| 200 | goto copy_m1; |
| 201 | |
| 202 | nl = ip[-2] & 3; |
| 203 | /* test if a match follows */ |
| 204 | if (nl == 0 && lit == 1 && ip[0] >= 16) { |
| 205 | next_lit = nl; |
| 206 | /* adjust length of previous short run */ |
| 207 | lit += 2; |
| 208 | *litp = (unsigned char)((*litp & ~3) | lit); |
| 209 | /* copy over the 2 literals that replace the match */ |
| 210 | copy2(ip-2, m_pos, pd(op, m_pos)); |
| 211 | o_m1_a++; |
| 212 | } |
| 213 | /* test if a literal run follows */ |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 214 | else |
| 215 | if (nl == 0 |
| 216 | && ip[0] < 16 |
| 217 | && ip[0] != 0 |
| 218 | && (lit + 2 + ip[0] < 16) |
| 219 | ) { |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 220 | t = *ip++; |
| 221 | /* remove short run */ |
| 222 | *litp &= ~3; |
| 223 | /* copy over the 2 literals that replace the match */ |
Denys Vlasenko | d6f5000 | 2011-12-15 12:39:25 +0100 | [diff] [blame] | 224 | copy2(ip-3+1, m_pos, pd(op, m_pos)); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 225 | /* move literals 1 byte ahead */ |
| 226 | litp += 2; |
| 227 | if (lit > 0) |
| 228 | memmove(litp+1, litp, lit); |
| 229 | /* insert new length of long literal run */ |
| 230 | lit += 2 + t + 3; |
| 231 | *litp = (unsigned char)(lit - 3); |
| 232 | |
| 233 | o_m1_b++; |
Denys Vlasenko | d6f5000 | 2011-12-15 12:39:25 +0100 | [diff] [blame] | 234 | *op++ = *m_pos++; |
| 235 | *op++ = *m_pos++; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 236 | goto copy_literal_run; |
| 237 | } |
| 238 | copy_m1: |
| 239 | *op++ = *m_pos++; |
| 240 | *op++ = *m_pos++; |
| 241 | } else { |
| 242 | match: |
| 243 | if (t >= 64) { /* a M2 match */ |
| 244 | m_pos = op - 1; |
| 245 | #if defined(LZO1X) |
| 246 | m_pos -= (t >> 2) & 7; |
| 247 | m_pos -= *ip++ << 3; |
| 248 | t = (t >> 5) - 1; |
| 249 | #elif defined(LZO1Y) |
| 250 | m_pos -= (t >> 2) & 3; |
| 251 | m_pos -= *ip++ << 2; |
| 252 | t = (t >> 4) - 3; |
| 253 | #endif |
| 254 | if (litp == NULL) |
| 255 | goto copy_m; |
| 256 | |
| 257 | nl = ip[-2] & 3; |
| 258 | /* test if in beetween two long literal runs */ |
| 259 | if (t == 1 && lit > 3 && nl == 0 |
| 260 | && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16) |
| 261 | ) { |
| 262 | t = *ip++; |
| 263 | /* copy over the 3 literals that replace the match */ |
Denys Vlasenko | d6f5000 | 2011-12-15 12:39:25 +0100 | [diff] [blame] | 264 | copy3(ip-1-2, m_pos, pd(op, m_pos)); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 265 | /* set new length of previous literal run */ |
| 266 | lit += 3 + t + 3; |
| 267 | *litp = (unsigned char)(lit - 3); |
| 268 | o_m2++; |
| 269 | *op++ = *m_pos++; |
| 270 | *op++ = *m_pos++; |
| 271 | *op++ = *m_pos++; |
| 272 | goto copy_literal_run; |
| 273 | } |
| 274 | } else { |
| 275 | if (t >= 32) { /* a M3 match */ |
| 276 | t &= 31; |
| 277 | if (t == 0) { |
| 278 | t = 31; |
| 279 | while (*ip == 0) |
| 280 | t += 255, ip++; |
| 281 | t += *ip++; |
| 282 | } |
| 283 | m_pos = op - 1; |
| 284 | m_pos -= *ip++ >> 2; |
| 285 | m_pos -= *ip++ << 6; |
| 286 | } else { /* a M4 match */ |
| 287 | m_pos = op; |
| 288 | m_pos -= (t & 8) << 11; |
| 289 | t &= 7; |
| 290 | if (t == 0) { |
| 291 | t = 7; |
| 292 | while (*ip == 0) |
| 293 | t += 255, ip++; |
| 294 | t += *ip++; |
| 295 | } |
| 296 | m_pos -= *ip++ >> 2; |
| 297 | m_pos -= *ip++ << 6; |
| 298 | if (m_pos == op) |
| 299 | goto eof_found; |
| 300 | m_pos -= 0x4000; |
| 301 | } |
| 302 | if (litp == NULL) |
| 303 | goto copy_m; |
| 304 | |
| 305 | nl = ip[-2] & 3; |
| 306 | /* test if in beetween two matches */ |
| 307 | if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) { |
| 308 | next_lit = nl; |
| 309 | /* make a previous short run */ |
| 310 | lit += 3; |
| 311 | *litp = (unsigned char)((*litp & ~3) | lit); |
| 312 | /* copy over the 3 literals that replace the match */ |
Denys Vlasenko | d6f5000 | 2011-12-15 12:39:25 +0100 | [diff] [blame] | 313 | copy3(ip-3, m_pos, pd(op, m_pos)); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 314 | o_m3_a++; |
| 315 | } |
| 316 | /* test if a literal run follows */ |
| 317 | else if (t == 1 && lit <= 3 && nl == 0 |
| 318 | && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16) |
| 319 | ) { |
| 320 | t = *ip++; |
| 321 | /* remove short run */ |
| 322 | *litp &= ~3; |
| 323 | /* copy over the 3 literals that replace the match */ |
Denys Vlasenko | d6f5000 | 2011-12-15 12:39:25 +0100 | [diff] [blame] | 324 | copy3(ip-4+1, m_pos, pd(op, m_pos)); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 325 | /* move literals 1 byte ahead */ |
| 326 | litp += 2; |
| 327 | if (lit > 0) |
| 328 | memmove(litp+1,litp,lit); |
| 329 | /* insert new length of long literal run */ |
| 330 | lit += 3 + t + 3; |
| 331 | *litp = (unsigned char)(lit - 3); |
| 332 | |
| 333 | o_m3_b++; |
| 334 | *op++ = *m_pos++; |
| 335 | *op++ = *m_pos++; |
| 336 | *op++ = *m_pos++; |
| 337 | goto copy_literal_run; |
| 338 | } |
| 339 | } |
| 340 | copy_m: |
| 341 | *op++ = *m_pos++; |
| 342 | *op++ = *m_pos++; |
| 343 | do *op++ = *m_pos++; while (--t > 0); |
| 344 | } |
| 345 | |
| 346 | match_done: |
| 347 | if (next_lit == NO_LIT) { |
| 348 | t = ip[-2] & 3; |
| 349 | lit = t; |
| 350 | litp = ip - 2; |
| 351 | } |
| 352 | else |
| 353 | t = next_lit; |
| 354 | next_lit = NO_LIT; |
| 355 | if (t == 0) |
| 356 | break; |
| 357 | /* copy literals */ |
| 358 | match_next: |
| 359 | do *op++ = *ip++; while (--t > 0); |
| 360 | t = *ip++; |
| 361 | } while (TEST_IP && TEST_OP); |
| 362 | } |
| 363 | |
| 364 | /* no EOF code was found */ |
| 365 | *out_len = pd(op, out); |
| 366 | return LZO_E_EOF_NOT_FOUND; |
| 367 | |
| 368 | eof_found: |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 369 | // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2); |
| 370 | // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 371 | *out_len = pd(op, out); |
| 372 | return (ip == ip_end ? LZO_E_OK : |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 373 | (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN)); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /**********************************************************************/ |
| 377 | #define F_OS F_OS_UNIX |
| 378 | #define F_CS F_CS_NATIVE |
| 379 | |
| 380 | /**********************************************************************/ |
| 381 | #define ADLER32_INIT_VALUE 1 |
| 382 | #define CRC32_INIT_VALUE 0 |
| 383 | |
| 384 | /**********************************************************************/ |
| 385 | enum { |
| 386 | M_LZO1X_1 = 1, |
| 387 | M_LZO1X_1_15 = 2, |
| 388 | M_LZO1X_999 = 3, |
| 389 | }; |
| 390 | |
| 391 | /**********************************************************************/ |
| 392 | /* header flags */ |
| 393 | #define F_ADLER32_D 0x00000001L |
| 394 | #define F_ADLER32_C 0x00000002L |
| 395 | #define F_H_EXTRA_FIELD 0x00000040L |
| 396 | #define F_H_GMTDIFF 0x00000080L |
| 397 | #define F_CRC32_D 0x00000100L |
| 398 | #define F_CRC32_C 0x00000200L |
| 399 | #define F_H_FILTER 0x00000800L |
| 400 | #define F_H_CRC32 0x00001000L |
| 401 | #define F_MASK 0x00003FFFL |
| 402 | |
| 403 | /* operating system & file system that created the file [mostly unused] */ |
| 404 | #define F_OS_UNIX 0x03000000L |
| 405 | #define F_OS_SHIFT 24 |
| 406 | #define F_OS_MASK 0xff000000L |
| 407 | |
| 408 | /* character set for file name encoding [mostly unused] */ |
| 409 | #define F_CS_NATIVE 0x00000000L |
| 410 | #define F_CS_SHIFT 20 |
| 411 | #define F_CS_MASK 0x00f00000L |
| 412 | |
| 413 | /* these bits must be zero */ |
| 414 | #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL) |
| 415 | |
| 416 | typedef struct chksum_t { |
| 417 | uint32_t f_adler32; |
| 418 | uint32_t f_crc32; |
| 419 | } chksum_t; |
| 420 | |
| 421 | typedef struct header_t { |
| 422 | unsigned version; |
| 423 | unsigned lib_version; |
| 424 | unsigned version_needed_to_extract; |
| 425 | uint32_t flags; |
| 426 | uint32_t mode; |
| 427 | uint32_t mtime; |
| 428 | uint32_t gmtdiff; |
| 429 | uint32_t header_checksum; |
| 430 | |
| 431 | uint32_t extra_field_len; |
| 432 | uint32_t extra_field_checksum; |
| 433 | |
| 434 | unsigned char method; |
| 435 | unsigned char level; |
| 436 | |
| 437 | /* info */ |
| 438 | char name[255+1]; |
| 439 | } header_t; |
| 440 | |
| 441 | struct globals { |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 442 | /*const uint32_t *lzo_crc32_table;*/ |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 443 | chksum_t chksum_in; |
| 444 | chksum_t chksum_out; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 445 | } FIX_ALIASING; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 446 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 447 | #define INIT_G() do { } while (0) |
| 448 | //#define G (*ptr_to_globals) |
| 449 | //#define INIT_G() do { |
Denys Vlasenko | b7c9fb2 | 2011-02-03 00:05:48 +0100 | [diff] [blame] | 450 | // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 451 | //} while (0) |
| 452 | |
| 453 | |
| 454 | /**********************************************************************/ |
| 455 | #define LZOP_VERSION 0x1010 |
| 456 | //#define LZOP_VERSION_STRING "1.01" |
| 457 | //#define LZOP_VERSION_DATE "Apr 27th 2003" |
| 458 | |
Denys Vlasenko | 2fe5fed | 2013-03-01 08:25:45 +0100 | [diff] [blame] | 459 | #define OPTION_STRING "cfvqdt123456789CF" |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 460 | |
Mike Frysinger | 93b5181 | 2013-03-03 00:48:53 -0500 | [diff] [blame] | 461 | /* Note: must be kept in sync with archival/bbunzip.c */ |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 462 | enum { |
| 463 | OPT_STDOUT = (1 << 0), |
| 464 | OPT_FORCE = (1 << 1), |
| 465 | OPT_VERBOSE = (1 << 2), |
Denys Vlasenko | 2fe5fed | 2013-03-01 08:25:45 +0100 | [diff] [blame] | 466 | OPT_QUIET = (1 << 3), |
| 467 | OPT_DECOMPRESS = (1 << 4), |
| 468 | OPT_TEST = (1 << 5), |
| 469 | OPT_1 = (1 << 6), |
| 470 | OPT_2 = (1 << 7), |
| 471 | OPT_3 = (1 << 8), |
| 472 | OPT_4 = (1 << 9), |
| 473 | OPT_5 = (1 << 10), |
| 474 | OPT_6 = (1 << 11), |
| 475 | OPT_789 = (7 << 12), |
| 476 | OPT_7 = (1 << 13), |
| 477 | OPT_8 = (1 << 14), |
| 478 | OPT_C = (1 << 15), |
| 479 | OPT_F = (1 << 16), |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | /**********************************************************************/ |
| 483 | // adler32 checksum |
| 484 | // adapted from free code by Mark Adler <madler@alumni.caltech.edu> |
| 485 | // see http://www.zlib.org/ |
| 486 | /**********************************************************************/ |
| 487 | static FAST_FUNC uint32_t |
| 488 | lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len) |
| 489 | { |
| 490 | enum { |
| 491 | LZO_BASE = 65521, /* largest prime smaller than 65536 */ |
| 492 | /* NMAX is the largest n such that |
| 493 | * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
| 494 | LZO_NMAX = 5552, |
| 495 | }; |
| 496 | uint32_t s1 = adler & 0xffff; |
| 497 | uint32_t s2 = (adler >> 16) & 0xffff; |
| 498 | unsigned k; |
| 499 | |
| 500 | if (buf == NULL) |
| 501 | return 1; |
| 502 | |
| 503 | while (len > 0) { |
| 504 | k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX; |
| 505 | len -= k; |
| 506 | if (k != 0) do { |
| 507 | s1 += *buf++; |
| 508 | s2 += s1; |
| 509 | } while (--k > 0); |
| 510 | s1 %= LZO_BASE; |
| 511 | s2 %= LZO_BASE; |
| 512 | } |
| 513 | return (s2 << 16) | s1; |
| 514 | } |
| 515 | |
| 516 | static FAST_FUNC uint32_t |
| 517 | lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len) |
| 518 | { |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 519 | //if (buf == NULL) - impossible |
| 520 | // return 0; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 521 | |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 522 | return ~crc32_block_endian0(~c, buf, len, global_crc32_table); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /**********************************************************************/ |
| 526 | static void init_chksum(chksum_t *ct) |
| 527 | { |
| 528 | ct->f_adler32 = ADLER32_INIT_VALUE; |
| 529 | ct->f_crc32 = CRC32_INIT_VALUE; |
| 530 | } |
| 531 | |
| 532 | static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt) |
| 533 | { |
| 534 | /* We need to handle the two checksums at once, because at the |
| 535 | * beginning of the header, we don't know yet which one we'll |
| 536 | * eventually need */ |
| 537 | ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt); |
| 538 | ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt); |
| 539 | } |
| 540 | |
| 541 | static uint32_t chksum_getresult(chksum_t *ct, const header_t *h) |
| 542 | { |
| 543 | return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32; |
| 544 | } |
| 545 | |
| 546 | /**********************************************************************/ |
| 547 | static uint32_t read32(void) |
| 548 | { |
| 549 | uint32_t v; |
| 550 | xread(0, &v, 4); |
| 551 | return ntohl(v); |
| 552 | } |
| 553 | |
| 554 | static void write32(uint32_t v) |
| 555 | { |
| 556 | v = htonl(v); |
| 557 | xwrite(1, &v, 4); |
| 558 | } |
| 559 | |
| 560 | static void f_write(const void* buf, int cnt) |
| 561 | { |
| 562 | xwrite(1, buf, cnt); |
| 563 | add_bytes_to_chksum(&G.chksum_out, buf, cnt); |
| 564 | } |
| 565 | |
| 566 | static void f_read(void* buf, int cnt) |
| 567 | { |
| 568 | xread(0, buf, cnt); |
| 569 | add_bytes_to_chksum(&G.chksum_in, buf, cnt); |
| 570 | } |
| 571 | |
| 572 | static int f_read8(void) |
| 573 | { |
| 574 | uint8_t v; |
| 575 | f_read(&v, 1); |
| 576 | return v; |
| 577 | } |
| 578 | |
| 579 | static void f_write8(uint8_t v) |
| 580 | { |
| 581 | f_write(&v, 1); |
| 582 | } |
| 583 | |
| 584 | static unsigned f_read16(void) |
| 585 | { |
| 586 | uint16_t v; |
| 587 | f_read(&v, 2); |
| 588 | return ntohs(v); |
| 589 | } |
| 590 | |
| 591 | static void f_write16(uint16_t v) |
| 592 | { |
| 593 | v = htons(v); |
| 594 | f_write(&v, 2); |
| 595 | } |
| 596 | |
| 597 | static uint32_t f_read32(void) |
| 598 | { |
| 599 | uint32_t v; |
| 600 | f_read(&v, 4); |
| 601 | return ntohl(v); |
| 602 | } |
| 603 | |
| 604 | static void f_write32(uint32_t v) |
| 605 | { |
| 606 | v = htonl(v); |
| 607 | f_write(&v, 4); |
| 608 | } |
| 609 | |
| 610 | /**********************************************************************/ |
| 611 | static int lzo_get_method(header_t *h) |
| 612 | { |
| 613 | /* check method */ |
| 614 | if (h->method == M_LZO1X_1) { |
| 615 | if (h->level == 0) |
| 616 | h->level = 3; |
| 617 | } else if (h->method == M_LZO1X_1_15) { |
| 618 | if (h->level == 0) |
| 619 | h->level = 1; |
| 620 | } else if (h->method == M_LZO1X_999) { |
| 621 | if (h->level == 0) |
| 622 | h->level = 9; |
| 623 | } else |
| 624 | return -1; /* not a LZO method */ |
| 625 | |
| 626 | /* check compression level */ |
| 627 | if (h->level < 1 || h->level > 9) |
| 628 | return 15; |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /**********************************************************************/ |
| 634 | #define LZO_BLOCK_SIZE (256 * 1024l) |
| 635 | #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */ |
| 636 | |
| 637 | /* LZO may expand uncompressible data by a small amount */ |
| 638 | #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3) |
| 639 | |
| 640 | /**********************************************************************/ |
| 641 | // compress a file |
| 642 | /**********************************************************************/ |
Denys Vlasenko | 4840325 | 2015-10-28 15:33:19 +0100 | [diff] [blame] | 643 | static NOINLINE int lzo_compress(const header_t *h) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 644 | { |
| 645 | unsigned block_size = LZO_BLOCK_SIZE; |
| 646 | int r = 0; /* LZO_E_OK */ |
| 647 | uint8_t *const b1 = xzalloc(block_size); |
| 648 | uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size)); |
| 649 | unsigned src_len = 0, dst_len = 0; |
| 650 | uint32_t d_adler32 = ADLER32_INIT_VALUE; |
| 651 | uint32_t d_crc32 = CRC32_INIT_VALUE; |
| 652 | int l; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 653 | uint8_t *wrk_mem = NULL; |
| 654 | |
| 655 | if (h->method == M_LZO1X_1) |
| 656 | wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS); |
| 657 | else if (h->method == M_LZO1X_1_15) |
| 658 | wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS); |
| 659 | else if (h->method == M_LZO1X_999) |
| 660 | wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS); |
| 661 | |
| 662 | for (;;) { |
| 663 | /* read a block */ |
| 664 | l = full_read(0, b1, block_size); |
| 665 | src_len = (l > 0 ? l : 0); |
| 666 | |
| 667 | /* write uncompressed block size */ |
| 668 | write32(src_len); |
| 669 | |
| 670 | /* exit if last block */ |
| 671 | if (src_len == 0) |
| 672 | break; |
| 673 | |
| 674 | /* compute checksum of uncompressed block */ |
| 675 | if (h->flags & F_ADLER32_D) |
| 676 | d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len); |
| 677 | if (h->flags & F_CRC32_D) |
| 678 | d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len); |
| 679 | |
| 680 | /* compress */ |
| 681 | if (h->method == M_LZO1X_1) |
| 682 | r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem); |
| 683 | else if (h->method == M_LZO1X_1_15) |
| 684 | r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem); |
| 685 | #if ENABLE_LZOP_COMPR_HIGH |
| 686 | else if (h->method == M_LZO1X_999) |
| 687 | r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len, |
| 688 | wrk_mem, h->level); |
| 689 | #endif |
| 690 | else |
| 691 | bb_error_msg_and_die("internal error"); |
| 692 | |
| 693 | if (r != 0) /* not LZO_E_OK */ |
| 694 | bb_error_msg_and_die("internal error - compression failed"); |
| 695 | |
| 696 | /* write compressed block size */ |
| 697 | if (dst_len < src_len) { |
| 698 | /* optimize */ |
| 699 | if (h->method == M_LZO1X_999) { |
| 700 | unsigned new_len = src_len; |
| 701 | r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL); |
| 702 | if (r != 0 /*LZO_E_OK*/ || new_len != src_len) |
| 703 | bb_error_msg_and_die("internal error - optimization failed"); |
| 704 | } |
| 705 | write32(dst_len); |
| 706 | } else { |
| 707 | /* data actually expanded => store data uncompressed */ |
| 708 | write32(src_len); |
| 709 | } |
| 710 | |
| 711 | /* write checksum of uncompressed block */ |
| 712 | if (h->flags & F_ADLER32_D) |
| 713 | write32(d_adler32); |
| 714 | if (h->flags & F_CRC32_D) |
| 715 | write32(d_crc32); |
| 716 | |
| 717 | if (dst_len < src_len) { |
| 718 | /* write checksum of compressed block */ |
| 719 | if (h->flags & F_ADLER32_C) |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 720 | write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len)); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 721 | if (h->flags & F_CRC32_C) |
| 722 | write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len)); |
| 723 | /* write compressed block data */ |
| 724 | xwrite(1, b2, dst_len); |
| 725 | } else { |
| 726 | /* write uncompressed block data */ |
| 727 | xwrite(1, b1, src_len); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | free(wrk_mem); |
| 732 | free(b1); |
| 733 | free(b2); |
Denys Vlasenko | 4840325 | 2015-10-28 15:33:19 +0100 | [diff] [blame] | 734 | return 1; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 737 | static FAST_FUNC void lzo_check( |
| 738 | uint32_t init, |
| 739 | uint8_t* buf, unsigned len, |
| 740 | uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned), |
| 741 | uint32_t ref) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 742 | { |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 743 | /* This function, by having the same order of parameters |
| 744 | * as fn, and by being marked FAST_FUNC (same as fn), |
| 745 | * saves a dozen bytes of code. |
| 746 | */ |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 747 | uint32_t c = fn(init, buf, len); |
| 748 | if (c != ref) |
| 749 | bb_error_msg_and_die("checksum error"); |
| 750 | } |
| 751 | |
| 752 | /**********************************************************************/ |
| 753 | // decompress a file |
| 754 | /**********************************************************************/ |
Denys Vlasenko | 4840325 | 2015-10-28 15:33:19 +0100 | [diff] [blame] | 755 | static NOINLINE int lzo_decompress(const header_t *h) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 756 | { |
| 757 | unsigned block_size = LZO_BLOCK_SIZE; |
| 758 | int r; |
| 759 | uint32_t src_len, dst_len; |
| 760 | uint32_t c_adler32 = ADLER32_INIT_VALUE; |
| 761 | uint32_t d_adler32 = ADLER32_INIT_VALUE; |
| 762 | uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 763 | uint8_t *b1; |
| 764 | uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size); |
| 765 | uint8_t *b2 = NULL; |
| 766 | |
| 767 | for (;;) { |
| 768 | uint8_t *dst; |
| 769 | |
| 770 | /* read uncompressed block size */ |
| 771 | dst_len = read32(); |
| 772 | |
| 773 | /* exit if last block */ |
| 774 | if (dst_len == 0) |
| 775 | break; |
| 776 | |
| 777 | /* error if split file */ |
| 778 | if (dst_len == 0xffffffffL) |
| 779 | /* should not happen - not yet implemented */ |
| 780 | bb_error_msg_and_die("this file is a split lzop file"); |
| 781 | |
| 782 | if (dst_len > MAX_BLOCK_SIZE) |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 783 | bb_error_msg_and_die("corrupted data"); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 784 | |
| 785 | /* read compressed block size */ |
| 786 | src_len = read32(); |
| 787 | if (src_len <= 0 || src_len > dst_len) |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 788 | bb_error_msg_and_die("corrupted data"); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 789 | |
| 790 | if (dst_len > block_size) { |
| 791 | if (b2) { |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 792 | free(b2); |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 793 | b2 = NULL; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 794 | } |
| 795 | block_size = dst_len; |
| 796 | mcs_block_size = MAX_COMPRESSED_SIZE(block_size); |
| 797 | } |
| 798 | |
| 799 | /* read checksum of uncompressed block */ |
| 800 | if (h->flags & F_ADLER32_D) |
| 801 | d_adler32 = read32(); |
| 802 | if (h->flags & F_CRC32_D) |
| 803 | d_crc32 = read32(); |
| 804 | |
| 805 | /* read checksum of compressed block */ |
| 806 | if (src_len < dst_len) { |
| 807 | if (h->flags & F_ADLER32_C) |
| 808 | c_adler32 = read32(); |
| 809 | if (h->flags & F_CRC32_C) |
| 810 | c_crc32 = read32(); |
| 811 | } |
| 812 | |
| 813 | if (b2 == NULL) |
| 814 | b2 = xzalloc(mcs_block_size); |
| 815 | /* read the block into the end of our buffer */ |
| 816 | b1 = b2 + mcs_block_size - src_len; |
| 817 | xread(0, b1, src_len); |
| 818 | |
| 819 | if (src_len < dst_len) { |
| 820 | unsigned d = dst_len; |
| 821 | |
| 822 | if (!(option_mask32 & OPT_F)) { |
| 823 | /* verify checksum of compressed block */ |
| 824 | if (h->flags & F_ADLER32_C) |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 825 | lzo_check(ADLER32_INIT_VALUE, |
| 826 | b1, src_len, |
| 827 | lzo_adler32, c_adler32); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 828 | if (h->flags & F_CRC32_C) |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 829 | lzo_check(CRC32_INIT_VALUE, |
| 830 | b1, src_len, |
| 831 | lzo_crc32, c_crc32); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* decompress */ |
| 835 | // if (option_mask32 & OPT_F) |
| 836 | // r = lzo1x_decompress(b1, src_len, b2, &d, NULL); |
| 837 | // else |
| 838 | r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL); |
| 839 | |
| 840 | if (r != 0 /*LZO_E_OK*/ || dst_len != d) { |
Denys Vlasenko | ccb88a6 | 2010-05-27 02:22:54 +0200 | [diff] [blame] | 841 | bb_error_msg_and_die("corrupted data"); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 842 | } |
| 843 | dst = b2; |
| 844 | } else { |
| 845 | /* "stored" block => no decompression */ |
| 846 | dst = b1; |
| 847 | } |
| 848 | |
| 849 | if (!(option_mask32 & OPT_F)) { |
| 850 | /* verify checksum of uncompressed block */ |
| 851 | if (h->flags & F_ADLER32_D) |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 852 | lzo_check(ADLER32_INIT_VALUE, |
| 853 | dst, dst_len, |
| 854 | lzo_adler32, d_adler32); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 855 | if (h->flags & F_CRC32_D) |
Denys Vlasenko | 8ae6e9b | 2010-09-04 19:46:52 +0200 | [diff] [blame] | 856 | lzo_check(CRC32_INIT_VALUE, |
| 857 | dst, dst_len, |
| 858 | lzo_crc32, d_crc32); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* write uncompressed block data */ |
| 862 | xwrite(1, dst, dst_len); |
| 863 | } |
| 864 | |
| 865 | free(b2); |
Denys Vlasenko | 4840325 | 2015-10-28 15:33:19 +0100 | [diff] [blame] | 866 | return 1; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /**********************************************************************/ |
| 870 | // lzop file signature (shamelessly borrowed from PNG) |
| 871 | /**********************************************************************/ |
| 872 | /* |
| 873 | * The first nine bytes of a lzop file always contain the following values: |
| 874 | * |
| 875 | * 0 1 2 3 4 5 6 7 8 |
| 876 | * --- --- --- --- --- --- --- --- --- |
| 877 | * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a |
| 878 | * (decimal) 137 76 90 79 0 13 10 26 10 |
| 879 | * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n |
| 880 | */ |
| 881 | |
| 882 | /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd): |
| 883 | * Only slight differences in header: |
| 884 | * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02 |
| 885 | * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02 |
| 886 | * ^^^^^ ^^^^^ |
| 887 | * version lib_version |
| 888 | * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00 |
| 889 | * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00 |
| 890 | * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ |
| 891 | * flags mode mtime |
| 892 | * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d |
| 893 | * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d |
| 894 | * ^^^^^^^^^^^ |
| 895 | * chksum_out |
| 896 | * The rest is identical. |
| 897 | */ |
| 898 | static const unsigned char lzop_magic[9] = { |
| 899 | 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a |
| 900 | }; |
| 901 | |
| 902 | /* This coding is derived from Alexander Lehmann's pngcheck code. */ |
| 903 | static void check_magic(void) |
| 904 | { |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 905 | unsigned char magic[sizeof(lzop_magic)]; |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 906 | xread(0, magic, sizeof(magic)); |
| 907 | if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0) |
| 908 | bb_error_msg_and_die("bad magic number"); |
| 909 | } |
| 910 | |
| 911 | /**********************************************************************/ |
| 912 | // lzop file header |
| 913 | /**********************************************************************/ |
| 914 | static void write_header(const header_t *h) |
| 915 | { |
| 916 | int l; |
| 917 | |
| 918 | xwrite(1, lzop_magic, sizeof(lzop_magic)); |
| 919 | |
| 920 | init_chksum(&G.chksum_out); |
| 921 | |
| 922 | f_write16(h->version); |
| 923 | f_write16(h->lib_version); |
| 924 | f_write16(h->version_needed_to_extract); |
| 925 | f_write8(h->method); |
| 926 | f_write8(h->level); |
| 927 | f_write32(h->flags); |
| 928 | f_write32(h->mode); |
| 929 | f_write32(h->mtime); |
| 930 | f_write32(h->gmtdiff); |
| 931 | |
| 932 | l = (int) strlen(h->name); |
| 933 | f_write8(l); |
| 934 | if (l) |
| 935 | f_write(h->name, l); |
| 936 | |
| 937 | f_write32(chksum_getresult(&G.chksum_out, h)); |
| 938 | } |
| 939 | |
| 940 | static int read_header(header_t *h) |
| 941 | { |
| 942 | int r; |
| 943 | int l; |
| 944 | uint32_t checksum; |
| 945 | |
| 946 | memset(h, 0, sizeof(*h)); |
| 947 | h->version_needed_to_extract = 0x0900; /* first lzop version */ |
| 948 | h->level = 0; |
| 949 | |
| 950 | init_chksum(&G.chksum_in); |
| 951 | |
| 952 | h->version = f_read16(); |
| 953 | if (h->version < 0x0900) |
| 954 | return 3; |
| 955 | h->lib_version = f_read16(); |
| 956 | if (h->version >= 0x0940) { |
| 957 | h->version_needed_to_extract = f_read16(); |
| 958 | if (h->version_needed_to_extract > LZOP_VERSION) |
| 959 | return 16; |
| 960 | if (h->version_needed_to_extract < 0x0900) |
| 961 | return 3; |
| 962 | } |
| 963 | h->method = f_read8(); |
| 964 | if (h->version >= 0x0940) |
| 965 | h->level = f_read8(); |
| 966 | h->flags = f_read32(); |
| 967 | if (h->flags & F_H_FILTER) |
| 968 | return 16; /* filter not supported */ |
| 969 | h->mode = f_read32(); |
| 970 | h->mtime = f_read32(); |
| 971 | if (h->version >= 0x0940) |
| 972 | h->gmtdiff = f_read32(); |
| 973 | |
| 974 | l = f_read8(); |
| 975 | if (l > 0) |
| 976 | f_read(h->name, l); |
| 977 | h->name[l] = 0; |
| 978 | |
| 979 | checksum = chksum_getresult(&G.chksum_in, h); |
| 980 | h->header_checksum = f_read32(); |
| 981 | if (h->header_checksum != checksum) |
| 982 | return 2; |
| 983 | |
| 984 | if (h->method <= 0) |
| 985 | return 14; |
| 986 | r = lzo_get_method(h); |
| 987 | if (r != 0) |
| 988 | return r; |
| 989 | |
| 990 | /* check reserved flags */ |
| 991 | if (h->flags & F_RESERVED) |
| 992 | return -13; |
| 993 | |
| 994 | /* skip extra field [not used yet] */ |
| 995 | if (h->flags & F_H_EXTRA_FIELD) { |
| 996 | uint32_t k; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 997 | |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 998 | /* note: the checksum also covers the length */ |
| 999 | init_chksum(&G.chksum_in); |
| 1000 | h->extra_field_len = f_read32(); |
| 1001 | for (k = 0; k < h->extra_field_len; k++) |
| 1002 | f_read8(); |
| 1003 | checksum = chksum_getresult(&G.chksum_in, h); |
| 1004 | h->extra_field_checksum = f_read32(); |
| 1005 | if (h->extra_field_checksum != checksum) |
| 1006 | return 3; |
| 1007 | } |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static void p_header(header_t *h) |
| 1013 | { |
| 1014 | int r; |
| 1015 | |
| 1016 | r = read_header(h); |
| 1017 | if (r == 0) |
| 1018 | return; |
| 1019 | bb_error_msg_and_die("header_error %d", r); |
| 1020 | } |
| 1021 | |
| 1022 | /**********************************************************************/ |
| 1023 | // compress |
| 1024 | /**********************************************************************/ |
| 1025 | static void lzo_set_method(header_t *h) |
| 1026 | { |
| 1027 | int level = 1; |
| 1028 | |
| 1029 | if (option_mask32 & OPT_1) { |
| 1030 | h->method = M_LZO1X_1_15; |
| 1031 | } else if (option_mask32 & OPT_789) { |
| 1032 | #if ENABLE_LZOP_COMPR_HIGH |
| 1033 | h->method = M_LZO1X_999; |
| 1034 | if (option_mask32 & OPT_7) |
| 1035 | level = 7; |
| 1036 | else if (option_mask32 & OPT_8) |
| 1037 | level = 8; |
| 1038 | else |
| 1039 | level = 9; |
| 1040 | #else |
| 1041 | bb_error_msg_and_die("high compression not compiled in"); |
| 1042 | #endif |
| 1043 | } else { /* levels 2..6 or none (defaults to level 3) */ |
| 1044 | h->method = M_LZO1X_1; |
| 1045 | level = 5; /* levels 2-6 are actually the same */ |
| 1046 | } |
| 1047 | |
| 1048 | h->level = level; |
| 1049 | } |
| 1050 | |
Denys Vlasenko | 4840325 | 2015-10-28 15:33:19 +0100 | [diff] [blame] | 1051 | static int do_lzo_compress(void) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1052 | { |
| 1053 | header_t header; |
| 1054 | |
| 1055 | #define h (&header) |
| 1056 | memset(h, 0, sizeof(*h)); |
| 1057 | |
| 1058 | lzo_set_method(h); |
| 1059 | |
| 1060 | h->version = (LZOP_VERSION & 0xffff); |
| 1061 | h->version_needed_to_extract = 0x0940; |
| 1062 | h->lib_version = lzo_version() & 0xffff; |
| 1063 | |
| 1064 | h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK); |
| 1065 | |
| 1066 | if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) { |
| 1067 | h->flags |= F_ADLER32_D; |
| 1068 | if (option_mask32 & OPT_C) |
| 1069 | h->flags |= F_ADLER32_C; |
| 1070 | } |
| 1071 | write_header(h); |
| 1072 | return lzo_compress(h); |
| 1073 | #undef h |
| 1074 | } |
| 1075 | |
| 1076 | /**********************************************************************/ |
| 1077 | // decompress |
| 1078 | /**********************************************************************/ |
Denys Vlasenko | 4840325 | 2015-10-28 15:33:19 +0100 | [diff] [blame] | 1079 | static int do_lzo_decompress(void) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1080 | { |
| 1081 | header_t header; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 1082 | |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1083 | check_magic(); |
| 1084 | p_header(&header); |
| 1085 | return lzo_decompress(&header); |
| 1086 | } |
| 1087 | |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 1088 | static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1089 | { |
| 1090 | if (option_mask32 & OPT_DECOMPRESS) { |
| 1091 | char *extension = strrchr(filename, '.'); |
| 1092 | if (!extension || strcmp(extension + 1, "lzo") != 0) |
| 1093 | return xasprintf("%s.out", filename); |
| 1094 | *extension = '\0'; |
| 1095 | return filename; |
| 1096 | } |
| 1097 | return xasprintf("%s.lzo", filename); |
| 1098 | } |
| 1099 | |
Denys Vlasenko | e7800f3 | 2014-12-07 00:42:49 +0100 | [diff] [blame] | 1100 | static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM) |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1101 | { |
| 1102 | if (option_mask32 & OPT_DECOMPRESS) |
| 1103 | return do_lzo_decompress(); |
| 1104 | return do_lzo_compress(); |
| 1105 | } |
| 1106 | |
| 1107 | int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 1108 | int lzop_main(int argc UNUSED_PARAM, char **argv) |
| 1109 | { |
| 1110 | getopt32(argv, OPTION_STRING); |
| 1111 | argv += optind; |
| 1112 | /* lzopcat? */ |
| 1113 | if (applet_name[4] == 'c') |
| 1114 | option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS); |
| 1115 | /* unlzop? */ |
Denys Vlasenko | 2fe5fed | 2013-03-01 08:25:45 +0100 | [diff] [blame] | 1116 | if (applet_name[4] == 'o') |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1117 | option_mask32 |= OPT_DECOMPRESS; |
| 1118 | |
Denys Vlasenko | 9ce642f | 2010-10-27 15:26:45 +0200 | [diff] [blame] | 1119 | global_crc32_table = crc32_filltable(NULL, 0); |
Denys Vlasenko | 39a04f7 | 2010-05-31 14:18:57 +0200 | [diff] [blame] | 1120 | return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL); |
Denis Vlasenko | 052ad9a | 2009-04-29 12:01:51 +0000 | [diff] [blame] | 1121 | } |