blob: a5fc01941f56f2ac9babbc65cb81cda8d0847dd2 [file] [log] [blame]
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001/*
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 Vlasenko60cb48c2013-01-14 15:57:44 +010017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Denis Vlasenko052ad9a2009-04-29 12:01:51 +000018 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 Vlasenkof6beef62013-11-14 11:39:00 +010028//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 Vlasenkoac216872013-11-14 11:38:18 +010043//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 Vlasenko66620fa2013-11-14 09:53:52 +010046//kbuild:lib-$(CONFIG_LZOP) += lzop.o
47
Pere Orga1f4447b2011-03-27 22:40:30 +020048//usage:#define lzop_trivial_usage
49//usage: "[-cfvd123456789CF] [FILE]..."
50//usage:#define lzop_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020051//usage: " -1..9 Compression level"
Pere Orga1f4447b2011-03-27 22:40:30 +020052//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 Vlasenko66426762011-06-05 03:58:28 +020068//usage: " -c Write to stdout"
Pere Orga1f4447b2011-03-27 22:40:30 +020069//usage: "\n -f Force"
70//usage: "\n -v Verbose"
71//usage: "\n -F Don't store or verify checksum"
72
Denis Vlasenko052ad9a2009-04-29 12:01:51 +000073#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020074#include "bb_archive.h"
Denis Vlasenko052ad9a2009-04-29 12:01:51 +000075#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/**********************************************************************/
92static 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
101static 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 Vlasenkoa7bb3c12009-10-08 12:28:08 +0200123static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000124 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 Vlasenko60cb48c2013-01-14 15:57:44 +0100139// LZO_UNUSED(wrkmem);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000140
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 Vlasenko6b9f1632010-01-28 02:24:24 +0100214 else
215 if (nl == 0
216 && ip[0] < 16
217 && ip[0] != 0
218 && (lit + 2 + ip[0] < 16)
219 ) {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000220 t = *ip++;
221 /* remove short run */
222 *litp &= ~3;
223 /* copy over the 2 literals that replace the match */
Denys Vlasenkod6f50002011-12-15 12:39:25 +0100224 copy2(ip-3+1, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000225 /* 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 Vlasenkod6f50002011-12-15 12:39:25 +0100234 *op++ = *m_pos++;
235 *op++ = *m_pos++;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000236 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 Vlasenkod6f50002011-12-15 12:39:25 +0100264 copy3(ip-1-2, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000265 /* 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 Vlasenkod6f50002011-12-15 12:39:25 +0100313 copy3(ip-3, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000314 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 Vlasenkod6f50002011-12-15 12:39:25 +0100324 copy3(ip-4+1, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000325 /* 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 Vlasenko60cb48c2013-01-14 15:57:44 +0100369// 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 Vlasenko052ad9a2009-04-29 12:01:51 +0000371 *out_len = pd(op, out);
372 return (ip == ip_end ? LZO_E_OK :
Denys Vlasenko69675782013-01-14 01:34:48 +0100373 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000374}
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/**********************************************************************/
385enum {
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
416typedef struct chksum_t {
417 uint32_t f_adler32;
418 uint32_t f_crc32;
419} chksum_t;
420
421typedef 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
441struct globals {
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200442 /*const uint32_t *lzo_crc32_table;*/
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000443 chksum_t chksum_in;
444 chksum_t chksum_out;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100445} FIX_ALIASING;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000446#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 Vlasenkob7c9fb22011-02-03 00:05:48 +0100450// SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000451//} 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 Vlasenko2fe5fed2013-03-01 08:25:45 +0100459#define OPTION_STRING "cfvqdt123456789CF"
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000460
Mike Frysinger93b51812013-03-03 00:48:53 -0500461/* Note: must be kept in sync with archival/bbunzip.c */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000462enum {
463 OPT_STDOUT = (1 << 0),
464 OPT_FORCE = (1 << 1),
465 OPT_VERBOSE = (1 << 2),
Denys Vlasenko2fe5fed2013-03-01 08:25:45 +0100466 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 Vlasenko052ad9a2009-04-29 12:01:51 +0000480};
481
482/**********************************************************************/
483// adler32 checksum
484// adapted from free code by Mark Adler <madler@alumni.caltech.edu>
485// see http://www.zlib.org/
486/**********************************************************************/
487static FAST_FUNC uint32_t
488lzo_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
516static FAST_FUNC uint32_t
517lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
518{
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200519 //if (buf == NULL) - impossible
520 // return 0;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000521
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200522 return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000523}
524
525/**********************************************************************/
526static void init_chksum(chksum_t *ct)
527{
528 ct->f_adler32 = ADLER32_INIT_VALUE;
529 ct->f_crc32 = CRC32_INIT_VALUE;
530}
531
532static 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
541static 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/**********************************************************************/
547static uint32_t read32(void)
548{
549 uint32_t v;
550 xread(0, &v, 4);
551 return ntohl(v);
552}
553
554static void write32(uint32_t v)
555{
556 v = htonl(v);
557 xwrite(1, &v, 4);
558}
559
560static 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
566static 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
572static int f_read8(void)
573{
574 uint8_t v;
575 f_read(&v, 1);
576 return v;
577}
578
579static void f_write8(uint8_t v)
580{
581 f_write(&v, 1);
582}
583
584static unsigned f_read16(void)
585{
586 uint16_t v;
587 f_read(&v, 2);
588 return ntohs(v);
589}
590
591static void f_write16(uint16_t v)
592{
593 v = htons(v);
594 f_write(&v, 2);
595}
596
597static uint32_t f_read32(void)
598{
599 uint32_t v;
600 f_read(&v, 4);
601 return ntohl(v);
602}
603
604static void f_write32(uint32_t v)
605{
606 v = htonl(v);
607 f_write(&v, 4);
608}
609
610/**********************************************************************/
611static 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 Vlasenko48403252015-10-28 15:33:19 +0100643static NOINLINE int lzo_compress(const header_t *h)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000644{
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 Vlasenko052ad9a2009-04-29 12:01:51 +0000653 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 Vlasenko9ce642f2010-10-27 15:26:45 +0200720 write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000721 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 Vlasenko48403252015-10-28 15:33:19 +0100734 return 1;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000735}
736
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200737static 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 Vlasenko052ad9a2009-04-29 12:01:51 +0000742{
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200743 /* 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 Vlasenko052ad9a2009-04-29 12:01:51 +0000747 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 Vlasenko48403252015-10-28 15:33:19 +0100755static NOINLINE int lzo_decompress(const header_t *h)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000756{
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 Vlasenko052ad9a2009-04-29 12:01:51 +0000763 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 Vlasenkoccb88a62010-05-27 02:22:54 +0200783 bb_error_msg_and_die("corrupted data");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000784
785 /* read compressed block size */
786 src_len = read32();
787 if (src_len <= 0 || src_len > dst_len)
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200788 bb_error_msg_and_die("corrupted data");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000789
790 if (dst_len > block_size) {
791 if (b2) {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000792 free(b2);
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200793 b2 = NULL;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000794 }
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 Vlasenko8ae6e9b2010-09-04 19:46:52 +0200825 lzo_check(ADLER32_INIT_VALUE,
826 b1, src_len,
827 lzo_adler32, c_adler32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000828 if (h->flags & F_CRC32_C)
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200829 lzo_check(CRC32_INIT_VALUE,
830 b1, src_len,
831 lzo_crc32, c_crc32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000832 }
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 Vlasenkoccb88a62010-05-27 02:22:54 +0200841 bb_error_msg_and_die("corrupted data");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000842 }
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 Vlasenko8ae6e9b2010-09-04 19:46:52 +0200852 lzo_check(ADLER32_INIT_VALUE,
853 dst, dst_len,
854 lzo_adler32, d_adler32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000855 if (h->flags & F_CRC32_D)
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200856 lzo_check(CRC32_INIT_VALUE,
857 dst, dst_len,
858 lzo_crc32, d_crc32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000859 }
860
861 /* write uncompressed block data */
862 xwrite(1, dst, dst_len);
863 }
864
865 free(b2);
Denys Vlasenko48403252015-10-28 15:33:19 +0100866 return 1;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000867}
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*/
898static 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. */
903static void check_magic(void)
904{
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200905 unsigned char magic[sizeof(lzop_magic)];
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000906 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/**********************************************************************/
914static 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
940static 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 Vlasenko9038d6f2009-07-15 20:02:19 +0200997
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000998 /* 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
1012static 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/**********************************************************************/
1025static 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 Vlasenko48403252015-10-28 15:33:19 +01001051static int do_lzo_compress(void)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001052{
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 Vlasenko48403252015-10-28 15:33:19 +01001079static int do_lzo_decompress(void)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001080{
1081 header_t header;
Denys Vlasenko9038d6f2009-07-15 20:02:19 +02001082
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001083 check_magic();
1084 p_header(&header);
1085 return lzo_decompress(&header);
1086}
1087
Denys Vlasenko39a04f72010-05-31 14:18:57 +02001088static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001089{
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 Vlasenkoe7800f32014-12-07 00:42:49 +01001100static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001101{
1102 if (option_mask32 & OPT_DECOMPRESS)
1103 return do_lzo_decompress();
1104 return do_lzo_compress();
1105}
1106
1107int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1108int 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 Vlasenko2fe5fed2013-03-01 08:25:45 +01001116 if (applet_name[4] == 'o')
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001117 option_mask32 |= OPT_DECOMPRESS;
1118
Denys Vlasenko9ce642f2010-10-27 15:26:45 +02001119 global_crc32_table = crc32_filltable(NULL, 0);
Denys Vlasenko39a04f72010-05-31 14:18:57 +02001120 return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001121}