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