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