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