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