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