blob: 74df8ff03160bb2b56734a4d294ff396f2cbde95 [file] [log] [blame]
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001/*
2 This file is part of the lzop file compressor.
3
4 Copyright (C) 1996..2003 Markus Franz Xaver Johannes Oberhumer
5 All Rights Reserved.
6
7 Markus F.X.J. Oberhumer <markus@oberhumer.com>
8 http://www.oberhumer.com/opensource/lzop/
9
10 lzop and the LZO library are free software; you can redistribute them
11 and/or modify them under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Denis Vlasenko052ad9a2009-04-29 12:01:51 +000018 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*/
Denys Vlasenkof6beef62013-11-14 11:39:00 +010027//config:config LZOP
Denys Vlasenkob097a842018-12-28 03:20:17 +010028//config: bool "lzop (12 kb)"
Denys Vlasenkof6beef62013-11-14 11:39:00 +010029//config: default y
30//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020031//config: Lzop compression/decompresion.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010032//config:
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010033//config:config UNLZOP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020034//config: bool "unlzop (13 kb)"
Denys Vlasenko1c013fa2017-06-15 18:07:04 +020035//config: default n # INCOMPAT: upstream lzop does not provide such tool
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010036//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020037//config: Lzop decompresion.
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010038//config:
39//config:config LZOPCAT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020040//config: bool "lzopcat (13 kb)"
Denys Vlasenko1c013fa2017-06-15 18:07:04 +020041//config: default n # INCOMPAT: upstream lzop does not provide such tool
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010042//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020043//config: Alias to "lzop -dc".
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010044//config:
Denys Vlasenkof6beef62013-11-14 11:39:00 +010045//config:config LZOP_COMPR_HIGH
46//config: bool "lzop compression levels 7,8,9 (not very useful)"
47//config: default n
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010048//config: depends on LZOP || UNLZOP || LZOPCAT
Denys Vlasenkof6beef62013-11-14 11:39:00 +010049//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020050//config: High levels (7,8,9) of lzop compression. These levels
51//config: are actually slower than gzip at equivalent compression ratios
52//config: and take up 3.2K of code.
Denys Vlasenkof6beef62013-11-14 11:39:00 +010053
Denys Vlasenkoac216872013-11-14 11:38:18 +010054//applet:IF_LZOP(APPLET(lzop, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +010055// APPLET_ODDNAME:name main location suid_type help
56//applet:IF_UNLZOP( APPLET_ODDNAME(unlzop, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, unlzop))
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010057//applet:IF_LZOPCAT(APPLET_ODDNAME(lzopcat, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, lzopcat))
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020058
Denys Vlasenko66620fa2013-11-14 09:53:52 +010059//kbuild:lib-$(CONFIG_LZOP) += lzop.o
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010060//kbuild:lib-$(CONFIG_UNLZOP) += lzop.o
61//kbuild:lib-$(CONFIG_LZOPCAT) += lzop.o
Denys Vlasenko66620fa2013-11-14 09:53:52 +010062
Pere Orga1f4447b2011-03-27 22:40:30 +020063//usage:#define lzop_trivial_usage
Denys Vlasenko1c013fa2017-06-15 18:07:04 +020064//usage: "[-cfUvd123456789CF] [FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +020065//usage:#define lzop_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020066//usage: " -1..9 Compression level"
Pere Orga1f4447b2011-03-27 22:40:30 +020067//usage: "\n -d Decompress"
68//usage: "\n -c Write to stdout"
69//usage: "\n -f Force"
Denys Vlasenko1c013fa2017-06-15 18:07:04 +020070//usage: "\n -U Delete input files"
71///////: "\n -k Keep input files" (default, so why bother documenting?)
Pere Orga1f4447b2011-03-27 22:40:30 +020072//usage: "\n -v Verbose"
73//usage: "\n -F Don't store or verify checksum"
74//usage: "\n -C Also write checksum of compressed block"
75//usage:
76//usage:#define lzopcat_trivial_usage
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010077//usage: "[-vF] [FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +020078//usage:#define lzopcat_full_usage "\n\n"
79//usage: " -v Verbose"
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010080//usage: "\n -F Don't verify checksum"
Pere Orga1f4447b2011-03-27 22:40:30 +020081//usage:
82//usage:#define unlzop_trivial_usage
Denys Vlasenko97058d02018-02-01 01:03:50 +010083//usage: "[-cfUvF] [FILE]..."
Pere Orga1f4447b2011-03-27 22:40:30 +020084//usage:#define unlzop_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020085//usage: " -c Write to stdout"
Pere Orga1f4447b2011-03-27 22:40:30 +020086//usage: "\n -f Force"
Denys Vlasenko1c013fa2017-06-15 18:07:04 +020087//usage: "\n -U Delete input files"
88///////: "\n -k Keep input files" (default, so why bother documenting?)
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020089//usage: "\n -t Test integrity"
Pere Orga1f4447b2011-03-27 22:40:30 +020090//usage: "\n -v Verbose"
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +010091//usage: "\n -F Don't verify checksum"
Pere Orga1f4447b2011-03-27 22:40:30 +020092
Denis Vlasenko052ad9a2009-04-29 12:01:51 +000093#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020094#include "common_bufsiz.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020095#include "bb_archive.h"
Denis Vlasenko052ad9a2009-04-29 12:01:51 +000096#include "liblzo_interface.h"
97
98/* lzo-2.03/src/lzo_ptr.h */
99#define pd(a,b) ((unsigned)((a)-(b)))
100
101#define lzo_version() LZO_VERSION
102#define lzo_sizeof_dict_t (sizeof(uint8_t*))
103
104/* lzo-2.03/include/lzo/lzo1x.h */
105#define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
106#define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
107#define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
108
109/* lzo-2.03/src/lzo1x_oo.c */
110#define NO_LIT UINT_MAX
111
112/**********************************************************************/
113static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
114{
115 ip[0] = m_pos[0];
116 if (off == 1)
117 ip[1] = m_pos[0];
118 else
119 ip[1] = m_pos[1];
120}
121
122static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
123{
124 ip[0] = m_pos[0];
125 if (off == 1) {
126 ip[2] = ip[1] = m_pos[0];
127 }
128 else if (off == 2) {
129 ip[1] = m_pos[1];
130 ip[2] = m_pos[0];
131 }
132 else {
133 ip[1] = m_pos[1];
134 ip[2] = m_pos[2];
135 }
136}
137
138/**********************************************************************/
139// optimize a block of data.
140/**********************************************************************/
141#define TEST_IP (ip < ip_end)
142#define TEST_OP (op <= op_end)
143
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200144static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
Denys Vlasenko99ac1752018-02-01 01:41:31 +0100145 uint8_t *out, unsigned *out_len /*, void* wrkmem */)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000146{
147 uint8_t* op;
148 uint8_t* ip;
149 unsigned t;
150 uint8_t* m_pos;
151 uint8_t* const ip_end = in + in_len;
152 uint8_t* const op_end = out + *out_len;
153 uint8_t* litp = NULL;
154 unsigned lit = 0;
155 unsigned next_lit = NO_LIT;
156 unsigned nl;
157 unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
158
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100159// LZO_UNUSED(wrkmem);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000160
161 *out_len = 0;
162
163 op = out;
164 ip = in;
165
166 if (*ip > 17) {
167 t = *ip++ - 17;
168 if (t < 4)
169 goto match_next;
170 goto first_literal_run;
171 }
172
173 while (TEST_IP && TEST_OP) {
174 t = *ip++;
175 if (t >= 16)
176 goto match;
177 /* a literal run */
178 litp = ip - 1;
179 if (t == 0) {
180 t = 15;
181 while (*ip == 0)
182 t += 255, ip++;
183 t += *ip++;
184 }
185 lit = t + 3;
186 /* copy literals */
187 copy_literal_run:
188 *op++ = *ip++;
189 *op++ = *ip++;
190 *op++ = *ip++;
191 first_literal_run:
192 do *op++ = *ip++; while (--t > 0);
193
194 t = *ip++;
195
196 if (t >= 16)
197 goto match;
198#if defined(LZO1X)
199 m_pos = op - 1 - 0x800;
200#elif defined(LZO1Y)
201 m_pos = op - 1 - 0x400;
202#endif
203 m_pos -= t >> 2;
204 m_pos -= *ip++ << 2;
205 *op++ = *m_pos++;
206 *op++ = *m_pos++;
207 *op++ = *m_pos++;
208 lit = 0;
209 goto match_done;
210
211
212 /* handle matches */
213 do {
214 if (t < 16) { /* a M1 match */
215 m_pos = op - 1;
216 m_pos -= t >> 2;
217 m_pos -= *ip++ << 2;
218
219 if (litp == NULL)
220 goto copy_m1;
221
222 nl = ip[-2] & 3;
223 /* test if a match follows */
224 if (nl == 0 && lit == 1 && ip[0] >= 16) {
225 next_lit = nl;
226 /* adjust length of previous short run */
227 lit += 2;
228 *litp = (unsigned char)((*litp & ~3) | lit);
229 /* copy over the 2 literals that replace the match */
230 copy2(ip-2, m_pos, pd(op, m_pos));
231 o_m1_a++;
232 }
233 /* test if a literal run follows */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100234 else
235 if (nl == 0
236 && ip[0] < 16
237 && ip[0] != 0
238 && (lit + 2 + ip[0] < 16)
239 ) {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000240 t = *ip++;
241 /* remove short run */
242 *litp &= ~3;
243 /* copy over the 2 literals that replace the match */
Denys Vlasenkod6f50002011-12-15 12:39:25 +0100244 copy2(ip-3+1, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000245 /* move literals 1 byte ahead */
246 litp += 2;
247 if (lit > 0)
248 memmove(litp+1, litp, lit);
249 /* insert new length of long literal run */
250 lit += 2 + t + 3;
251 *litp = (unsigned char)(lit - 3);
252
253 o_m1_b++;
Denys Vlasenkod6f50002011-12-15 12:39:25 +0100254 *op++ = *m_pos++;
255 *op++ = *m_pos++;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000256 goto copy_literal_run;
257 }
258 copy_m1:
259 *op++ = *m_pos++;
260 *op++ = *m_pos++;
261 } else {
262 match:
263 if (t >= 64) { /* a M2 match */
264 m_pos = op - 1;
265#if defined(LZO1X)
266 m_pos -= (t >> 2) & 7;
267 m_pos -= *ip++ << 3;
268 t = (t >> 5) - 1;
269#elif defined(LZO1Y)
270 m_pos -= (t >> 2) & 3;
271 m_pos -= *ip++ << 2;
272 t = (t >> 4) - 3;
273#endif
274 if (litp == NULL)
275 goto copy_m;
276
277 nl = ip[-2] & 3;
278 /* test if in beetween two long literal runs */
279 if (t == 1 && lit > 3 && nl == 0
280 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
281 ) {
282 t = *ip++;
283 /* copy over the 3 literals that replace the match */
Denys Vlasenkod6f50002011-12-15 12:39:25 +0100284 copy3(ip-1-2, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000285 /* set new length of previous literal run */
286 lit += 3 + t + 3;
287 *litp = (unsigned char)(lit - 3);
288 o_m2++;
289 *op++ = *m_pos++;
290 *op++ = *m_pos++;
291 *op++ = *m_pos++;
292 goto copy_literal_run;
293 }
294 } else {
295 if (t >= 32) { /* a M3 match */
296 t &= 31;
297 if (t == 0) {
298 t = 31;
299 while (*ip == 0)
300 t += 255, ip++;
301 t += *ip++;
302 }
303 m_pos = op - 1;
304 m_pos -= *ip++ >> 2;
305 m_pos -= *ip++ << 6;
306 } else { /* a M4 match */
307 m_pos = op;
308 m_pos -= (t & 8) << 11;
309 t &= 7;
310 if (t == 0) {
311 t = 7;
312 while (*ip == 0)
313 t += 255, ip++;
314 t += *ip++;
315 }
316 m_pos -= *ip++ >> 2;
317 m_pos -= *ip++ << 6;
318 if (m_pos == op)
319 goto eof_found;
320 m_pos -= 0x4000;
321 }
322 if (litp == NULL)
323 goto copy_m;
324
325 nl = ip[-2] & 3;
326 /* test if in beetween two matches */
327 if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) {
328 next_lit = nl;
329 /* make a previous short run */
330 lit += 3;
331 *litp = (unsigned char)((*litp & ~3) | lit);
332 /* copy over the 3 literals that replace the match */
Denys Vlasenkod6f50002011-12-15 12:39:25 +0100333 copy3(ip-3, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000334 o_m3_a++;
335 }
336 /* test if a literal run follows */
337 else if (t == 1 && lit <= 3 && nl == 0
338 && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
339 ) {
340 t = *ip++;
341 /* remove short run */
342 *litp &= ~3;
343 /* copy over the 3 literals that replace the match */
Denys Vlasenkod6f50002011-12-15 12:39:25 +0100344 copy3(ip-4+1, m_pos, pd(op, m_pos));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000345 /* move literals 1 byte ahead */
346 litp += 2;
347 if (lit > 0)
348 memmove(litp+1,litp,lit);
349 /* insert new length of long literal run */
350 lit += 3 + t + 3;
351 *litp = (unsigned char)(lit - 3);
352
353 o_m3_b++;
354 *op++ = *m_pos++;
355 *op++ = *m_pos++;
356 *op++ = *m_pos++;
357 goto copy_literal_run;
358 }
359 }
360 copy_m:
361 *op++ = *m_pos++;
362 *op++ = *m_pos++;
363 do *op++ = *m_pos++; while (--t > 0);
364 }
365
366 match_done:
367 if (next_lit == NO_LIT) {
368 t = ip[-2] & 3;
369 lit = t;
370 litp = ip - 2;
371 }
372 else
373 t = next_lit;
374 next_lit = NO_LIT;
375 if (t == 0)
376 break;
377 /* copy literals */
378 match_next:
379 do *op++ = *ip++; while (--t > 0);
380 t = *ip++;
381 } while (TEST_IP && TEST_OP);
382 }
383
384 /* no EOF code was found */
385 *out_len = pd(op, out);
386 return LZO_E_EOF_NOT_FOUND;
387
388 eof_found:
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100389// LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
390// LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000391 *out_len = pd(op, out);
392 return (ip == ip_end ? LZO_E_OK :
Denys Vlasenko69675782013-01-14 01:34:48 +0100393 (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000394}
395
396/**********************************************************************/
397#define F_OS F_OS_UNIX
398#define F_CS F_CS_NATIVE
399
400/**********************************************************************/
401#define ADLER32_INIT_VALUE 1
402#define CRC32_INIT_VALUE 0
403
404/**********************************************************************/
405enum {
406 M_LZO1X_1 = 1,
407 M_LZO1X_1_15 = 2,
408 M_LZO1X_999 = 3,
409};
410
411/**********************************************************************/
412/* header flags */
413#define F_ADLER32_D 0x00000001L
414#define F_ADLER32_C 0x00000002L
415#define F_H_EXTRA_FIELD 0x00000040L
416#define F_H_GMTDIFF 0x00000080L
417#define F_CRC32_D 0x00000100L
418#define F_CRC32_C 0x00000200L
419#define F_H_FILTER 0x00000800L
420#define F_H_CRC32 0x00001000L
421#define F_MASK 0x00003FFFL
422
423/* operating system & file system that created the file [mostly unused] */
424#define F_OS_UNIX 0x03000000L
425#define F_OS_SHIFT 24
426#define F_OS_MASK 0xff000000L
427
428/* character set for file name encoding [mostly unused] */
429#define F_CS_NATIVE 0x00000000L
430#define F_CS_SHIFT 20
431#define F_CS_MASK 0x00f00000L
432
433/* these bits must be zero */
434#define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
435
436typedef struct chksum_t {
437 uint32_t f_adler32;
438 uint32_t f_crc32;
439} chksum_t;
440
441typedef struct header_t {
Denys Vlasenko434f9592018-04-08 13:32:47 +0200442 /* used to have auxiliary fields here */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000443
Denys Vlasenko434f9592018-04-08 13:32:47 +0200444 /* Starting from here, the layout and endianness
445 * are exactly in on-disk format.
446 */
447 uint16_t version_be16;
448 uint16_t lib_version_be16;
449 uint16_t version_needed_to_extract_be16;
450 uint8_t method;
451 uint8_t level;
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200452 uint32_t flags32; /* be32 on disk, but we keep this field in native order */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200453 uint32_t mode_be32;
454 uint32_t mtime_be32;
455 uint32_t gmtdiff_be32;
456 char len_and_name[1+255+1];
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000457} header_t;
458
459struct globals {
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200460 /*const uint32_t *lzo_crc32_table;*/
Denys Vlasenko434f9592018-04-08 13:32:47 +0200461 chksum_t chksum;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100462} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200463#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000464//#define G (*ptr_to_globals)
Denys Vlasenko434f9592018-04-08 13:32:47 +0200465#define INIT_G() do { \
466 setup_common_bufsiz(); \
467 /*SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));*/ \
468} while (0)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000469
470
471/**********************************************************************/
472#define LZOP_VERSION 0x1010
473//#define LZOP_VERSION_STRING "1.01"
474//#define LZOP_VERSION_DATE "Apr 27th 2003"
475
Denys Vlasenko1c013fa2017-06-15 18:07:04 +0200476// lzop wants to be weird:
477// unlike all other compressosrs, its -k "keep" option is the default,
478// and -U is used to delete the source. We will invert the bit after getopt().
479#define OPTION_STRING "cfUvqdt123456789CFk"
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000480
Mike Frysinger93b51812013-03-03 00:48:53 -0500481/* Note: must be kept in sync with archival/bbunzip.c */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000482enum {
483 OPT_STDOUT = (1 << 0),
484 OPT_FORCE = (1 << 1),
Denys Vlasenko1c013fa2017-06-15 18:07:04 +0200485 OPT_KEEP = (1 << 2),
486 OPT_VERBOSE = (1 << 3),
487 OPT_QUIET = (1 << 4),
488 OPT_DECOMPRESS = (1 << 5),
489 OPT_TEST = (1 << 6),
490 OPT_1 = (1 << 7),
491 OPT_2 = (1 << 8),
492 OPT_3 = (1 << 9),
493 OPT_4 = (1 << 10),
494 OPT_5 = (1 << 11),
495 OPT_6 = (1 << 12),
Denys Vlasenko2fe5fed2013-03-01 08:25:45 +0100496 OPT_7 = (1 << 13),
497 OPT_8 = (1 << 14),
Denys Vlasenko1c013fa2017-06-15 18:07:04 +0200498 OPT_9 = (1 << 15),
499 OPT_C = (1 << 16),
500 OPT_F = (1 << 17),
501 OPT_k = (1 << 18),
502 OPT_789 = OPT_7 | OPT_8 | OPT_9
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000503};
504
505/**********************************************************************/
506// adler32 checksum
507// adapted from free code by Mark Adler <madler@alumni.caltech.edu>
508// see http://www.zlib.org/
509/**********************************************************************/
510static FAST_FUNC uint32_t
511lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
512{
513 enum {
514 LZO_BASE = 65521, /* largest prime smaller than 65536 */
515 /* NMAX is the largest n such that
516 * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
517 LZO_NMAX = 5552,
518 };
519 uint32_t s1 = adler & 0xffff;
520 uint32_t s2 = (adler >> 16) & 0xffff;
521 unsigned k;
522
523 if (buf == NULL)
524 return 1;
525
526 while (len > 0) {
527 k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
528 len -= k;
529 if (k != 0) do {
530 s1 += *buf++;
531 s2 += s1;
532 } while (--k > 0);
533 s1 %= LZO_BASE;
534 s2 %= LZO_BASE;
535 }
536 return (s2 << 16) | s1;
537}
538
539static FAST_FUNC uint32_t
540lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
541{
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200542 //if (buf == NULL) - impossible
543 // return 0;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000544
Denys Vlasenko9ce642f2010-10-27 15:26:45 +0200545 return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000546}
547
548/**********************************************************************/
Denys Vlasenko434f9592018-04-08 13:32:47 +0200549static void init_chksum(void)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000550{
Denys Vlasenko434f9592018-04-08 13:32:47 +0200551 G.chksum.f_adler32 = ADLER32_INIT_VALUE;
552 G.chksum.f_crc32 = CRC32_INIT_VALUE;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000553}
554
Denys Vlasenko434f9592018-04-08 13:32:47 +0200555static void add_bytes_to_chksum(const void* buf, int cnt)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000556{
557 /* We need to handle the two checksums at once, because at the
558 * beginning of the header, we don't know yet which one we'll
559 * eventually need */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200560 G.chksum.f_adler32 = lzo_adler32(G.chksum.f_adler32, (const uint8_t*)buf, cnt);
561 G.chksum.f_crc32 = lzo_crc32(G.chksum.f_crc32, (const uint8_t*)buf, cnt);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000562}
563
Denys Vlasenko434f9592018-04-08 13:32:47 +0200564static uint32_t chksum_getresult(uint32_t h_flags32)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000565{
Denys Vlasenko434f9592018-04-08 13:32:47 +0200566 return (h_flags32 & F_H_CRC32) ? G.chksum.f_crc32 : G.chksum.f_adler32;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000567}
568
569/**********************************************************************/
570static uint32_t read32(void)
571{
572 uint32_t v;
573 xread(0, &v, 4);
574 return ntohl(v);
575}
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000576static void f_read(void* buf, int cnt)
577{
578 xread(0, buf, cnt);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200579 add_bytes_to_chksum(buf, cnt);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000580}
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200581//static int f_read8(void)
582//{
583// uint8_t v;
584// f_read(&v, 1);
585// return v;
586//}
587//static unsigned f_read16(void)
588//{
589// uint16_t v;
590// f_read(&v, 2);
591// return ntohs(v);
592//}
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000593static uint32_t f_read32(void)
594{
595 uint32_t v;
596 f_read(&v, 4);
597 return ntohl(v);
598}
599
Denys Vlasenko434f9592018-04-08 13:32:47 +0200600static void write32(uint32_t v)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000601{
602 v = htonl(v);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200603 xwrite(1, &v, 4);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000604}
Denys Vlasenko434f9592018-04-08 13:32:47 +0200605static void f_write(const void* buf, int cnt)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000606{
Denys Vlasenko434f9592018-04-08 13:32:47 +0200607 xwrite(1, buf, cnt);
608 add_bytes_to_chksum(buf, cnt);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000609}
Denys Vlasenko434f9592018-04-08 13:32:47 +0200610//static void f_write8(uint8_t v)
611//{
612// f_write(&v, 1);
613//}
614//static void f_write16(uint16_t v)
615//{
616// v = htons(v);
617// f_write(&v, 2);
618//}
619//static void f_write32(uint32_t v)
620//{
621// v = htonl(v);
622// f_write(&v, 4);
623//}
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000624
625/**********************************************************************/
626#define LZO_BLOCK_SIZE (256 * 1024l)
627#define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
628
629/* LZO may expand uncompressible data by a small amount */
630#define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
631
632/**********************************************************************/
633// compress a file
634/**********************************************************************/
Denys Vlasenko48403252015-10-28 15:33:19 +0100635static NOINLINE int lzo_compress(const header_t *h)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000636{
637 unsigned block_size = LZO_BLOCK_SIZE;
638 int r = 0; /* LZO_E_OK */
639 uint8_t *const b1 = xzalloc(block_size);
640 uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000641 uint32_t d_adler32 = ADLER32_INIT_VALUE;
642 uint32_t d_crc32 = CRC32_INIT_VALUE;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000643 uint8_t *wrk_mem = NULL;
644
Denys Vlasenko87a08e42018-04-08 16:44:45 +0200645 /* Only these methods are possible, see lzo_set_method():
646 * -1: M_LZO1X_1_15
647 * -2..6: M_LZO1X_1
648 * -7..9: M_LZO1X_999 if ENABLE_LZOP_COMPR_HIGH
649 */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000650 if (h->method == M_LZO1X_1)
651 wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
Denys Vlasenko87a08e42018-04-08 16:44:45 +0200652 else /* check only if it's not the only possibility */
653 IF_LZOP_COMPR_HIGH(if (h->method == M_LZO1X_1_15))
654 wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
655#if ENABLE_LZOP_COMPR_HIGH
656 else /* must be h->method == M_LZO1X_999 */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000657 wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
Denys Vlasenko87a08e42018-04-08 16:44:45 +0200658#endif
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000659
660 for (;;) {
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200661 unsigned src_len, dst_len;
662 int l;
663 uint32_t wordbuf[6];
664 uint32_t *wordptr = wordbuf;
665
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000666 /* read a block */
667 l = full_read(0, b1, block_size);
668 src_len = (l > 0 ? l : 0);
669
670 /* write uncompressed block size */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000671 /* exit if last block */
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200672 if (src_len == 0) {
673 write32(0);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000674 break;
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200675 }
676 *wordptr++ = htonl(src_len);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000677
678 /* compute checksum of uncompressed block */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200679 if (h->flags32 & F_ADLER32_D)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000680 d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200681 if (h->flags32 & F_CRC32_D)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000682 d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
683
684 /* compress */
685 if (h->method == M_LZO1X_1)
686 r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
Denys Vlasenko87a08e42018-04-08 16:44:45 +0200687 else IF_LZOP_COMPR_HIGH(if (h->method == M_LZO1X_1_15))
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000688 r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
689#if ENABLE_LZOP_COMPR_HIGH
Denys Vlasenko87a08e42018-04-08 16:44:45 +0200690 else /* must be h->method == M_LZO1X_999 */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000691 r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
692 wrk_mem, h->level);
693#endif
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000694 if (r != 0) /* not LZO_E_OK */
Denys Vlasenko24ef5c62018-04-08 14:00:03 +0200695 bb_error_msg_and_die("%s: %s", "internal error", "compression");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000696
697 /* write compressed block size */
698 if (dst_len < src_len) {
699 /* optimize */
700 if (h->method == M_LZO1X_999) {
701 unsigned new_len = src_len;
Denys Vlasenko99ac1752018-02-01 01:41:31 +0100702 r = lzo1x_optimize(b2, dst_len, b1, &new_len /*, NULL*/);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000703 if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
Denys Vlasenko24ef5c62018-04-08 14:00:03 +0200704 bb_error_msg_and_die("%s: %s", "internal error", "optimization");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000705 }
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200706 *wordptr++ = htonl(dst_len);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000707 } else {
708 /* data actually expanded => store data uncompressed */
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200709 *wordptr++ = htonl(src_len);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000710 }
711
712 /* write checksum of uncompressed block */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200713 if (h->flags32 & F_ADLER32_D)
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200714 *wordptr++ = htonl(d_adler32);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200715 if (h->flags32 & F_CRC32_D)
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200716 *wordptr++ = htonl(d_crc32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000717
718 if (dst_len < src_len) {
719 /* write checksum of compressed block */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200720 if (h->flags32 & F_ADLER32_C)
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200721 *wordptr++ = htonl(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
Denys Vlasenko434f9592018-04-08 13:32:47 +0200722 if (h->flags32 & F_CRC32_C)
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200723 *wordptr++ = htonl(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
724 }
725 xwrite(1, wordbuf, ((char*)wordptr) - ((char*)wordbuf));
726 if (dst_len < src_len) {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000727 /* write compressed block data */
728 xwrite(1, b2, dst_len);
729 } else {
730 /* write uncompressed block data */
731 xwrite(1, b1, src_len);
732 }
Denys Vlasenko3d4f6882018-04-08 16:31:02 +0200733 // /* if full_read() was nevertheless "short", it was EOF */
734 // if (src_len < block_size)
735 // break;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000736 }
737
738 free(wrk_mem);
739 free(b1);
740 free(b2);
Denys Vlasenko48403252015-10-28 15:33:19 +0100741 return 1;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000742}
743
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200744static FAST_FUNC void lzo_check(
745 uint32_t init,
746 uint8_t* buf, unsigned len,
747 uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
748 uint32_t ref)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000749{
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200750 /* This function, by having the same order of parameters
751 * as fn, and by being marked FAST_FUNC (same as fn),
752 * saves a dozen bytes of code.
753 */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000754 uint32_t c = fn(init, buf, len);
755 if (c != ref)
James Byrne69374872019-07-02 11:35:03 +0200756 bb_simple_error_msg_and_die("checksum error");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000757}
758
759/**********************************************************************/
760// decompress a file
761/**********************************************************************/
Denys Vlasenko434f9592018-04-08 13:32:47 +0200762// used to have "const header_t *h" parameter, but since it uses
763// only flags32 field, changed to receive only that.
764static NOINLINE int lzo_decompress(uint32_t h_flags32)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000765{
766 unsigned block_size = LZO_BLOCK_SIZE;
767 int r;
768 uint32_t src_len, dst_len;
769 uint32_t c_adler32 = ADLER32_INIT_VALUE;
770 uint32_t d_adler32 = ADLER32_INIT_VALUE;
771 uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000772 uint8_t *b1;
773 uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
774 uint8_t *b2 = NULL;
775
776 for (;;) {
777 uint8_t *dst;
778
779 /* read uncompressed block size */
780 dst_len = read32();
781
782 /* exit if last block */
783 if (dst_len == 0)
784 break;
785
786 /* error if split file */
787 if (dst_len == 0xffffffffL)
788 /* should not happen - not yet implemented */
James Byrne69374872019-07-02 11:35:03 +0200789 bb_simple_error_msg_and_die("this file is a split lzop file");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000790
791 if (dst_len > MAX_BLOCK_SIZE)
James Byrne69374872019-07-02 11:35:03 +0200792 bb_simple_error_msg_and_die("corrupted data");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000793
794 /* read compressed block size */
795 src_len = read32();
796 if (src_len <= 0 || src_len > dst_len)
James Byrne69374872019-07-02 11:35:03 +0200797 bb_simple_error_msg_and_die("corrupted data");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000798
799 if (dst_len > block_size) {
800 if (b2) {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000801 free(b2);
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200802 b2 = NULL;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000803 }
804 block_size = dst_len;
805 mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
806 }
807
808 /* read checksum of uncompressed block */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200809 if (h_flags32 & F_ADLER32_D)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000810 d_adler32 = read32();
Denys Vlasenko434f9592018-04-08 13:32:47 +0200811 if (h_flags32 & F_CRC32_D)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000812 d_crc32 = read32();
813
814 /* read checksum of compressed block */
815 if (src_len < dst_len) {
Denys Vlasenko434f9592018-04-08 13:32:47 +0200816 if (h_flags32 & F_ADLER32_C)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000817 c_adler32 = read32();
Denys Vlasenko434f9592018-04-08 13:32:47 +0200818 if (h_flags32 & F_CRC32_C)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000819 c_crc32 = read32();
820 }
821
822 if (b2 == NULL)
823 b2 = xzalloc(mcs_block_size);
824 /* read the block into the end of our buffer */
825 b1 = b2 + mcs_block_size - src_len;
826 xread(0, b1, src_len);
827
828 if (src_len < dst_len) {
829 unsigned d = dst_len;
830
831 if (!(option_mask32 & OPT_F)) {
832 /* verify checksum of compressed block */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200833 if (h_flags32 & F_ADLER32_C)
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200834 lzo_check(ADLER32_INIT_VALUE,
835 b1, src_len,
836 lzo_adler32, c_adler32);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200837 if (h_flags32 & F_CRC32_C)
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200838 lzo_check(CRC32_INIT_VALUE,
839 b1, src_len,
840 lzo_crc32, c_crc32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000841 }
842
843 /* decompress */
844// if (option_mask32 & OPT_F)
Denys Vlasenko99ac1752018-02-01 01:41:31 +0100845// r = lzo1x_decompress(b1, src_len, b2, &d /*, NULL*/);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000846// else
Denys Vlasenko99ac1752018-02-01 01:41:31 +0100847 r = lzo1x_decompress_safe(b1, src_len, b2, &d /*, NULL*/);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000848
849 if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
James Byrne69374872019-07-02 11:35:03 +0200850 bb_simple_error_msg_and_die("corrupted data");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000851 }
852 dst = b2;
853 } else {
854 /* "stored" block => no decompression */
855 dst = b1;
856 }
857
858 if (!(option_mask32 & OPT_F)) {
859 /* verify checksum of uncompressed block */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200860 if (h_flags32 & F_ADLER32_D)
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200861 lzo_check(ADLER32_INIT_VALUE,
862 dst, dst_len,
863 lzo_adler32, d_adler32);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200864 if (h_flags32 & F_CRC32_D)
Denys Vlasenko8ae6e9b2010-09-04 19:46:52 +0200865 lzo_check(CRC32_INIT_VALUE,
866 dst, dst_len,
867 lzo_crc32, d_crc32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000868 }
869
870 /* write uncompressed block data */
871 xwrite(1, dst, dst_len);
872 }
873
874 free(b2);
Denys Vlasenko48403252015-10-28 15:33:19 +0100875 return 1;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000876}
877
878/**********************************************************************/
879// lzop file signature (shamelessly borrowed from PNG)
880/**********************************************************************/
881/*
882 * The first nine bytes of a lzop file always contain the following values:
883 *
884 * 0 1 2 3 4 5 6 7 8
885 * --- --- --- --- --- --- --- --- ---
886 * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
887 * (decimal) 137 76 90 79 0 13 10 26 10
888 * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
889 */
890
891/* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
892 * Only slight differences in header:
893 * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
894 * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
895 * ^^^^^ ^^^^^
896 * version lib_version
897 * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
898 * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
899 * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
900 * flags mode mtime
901 * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
902 * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
903 * ^^^^^^^^^^^
Denys Vlasenko434f9592018-04-08 13:32:47 +0200904 * chksum
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000905 * The rest is identical.
906*/
Denys Vlasenko3e134eb2016-04-22 18:09:21 +0200907static const unsigned char lzop_magic[9] ALIGN1 = {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000908 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
909};
910
911/* This coding is derived from Alexander Lehmann's pngcheck code. */
912static void check_magic(void)
913{
Denys Vlasenko9038d6f2009-07-15 20:02:19 +0200914 unsigned char magic[sizeof(lzop_magic)];
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000915 xread(0, magic, sizeof(magic));
916 if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
James Byrne69374872019-07-02 11:35:03 +0200917 bb_simple_error_msg_and_die("bad magic number");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000918}
919
920/**********************************************************************/
921// lzop file header
922/**********************************************************************/
Denys Vlasenko434f9592018-04-08 13:32:47 +0200923static void write_header(header_t *h)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000924{
Denys Vlasenko434f9592018-04-08 13:32:47 +0200925 char *end;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000926
927 xwrite(1, lzop_magic, sizeof(lzop_magic));
928
Denys Vlasenko434f9592018-04-08 13:32:47 +0200929 init_chksum();
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000930
Denys Vlasenko434f9592018-04-08 13:32:47 +0200931 /* Our caller leaves name zero-filled, so len == 0 */
932 end = h->len_and_name+1 + 0; /* 0 is strlen(h->len_and_name+1) */
933 /* Store length byte */
934 /*h->len_and_name[0] = end - (h->len_and_name+1); - zero already */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000935
Denys Vlasenko434f9592018-04-08 13:32:47 +0200936 f_write(&h->version_be16, end - (char*)&h->version_be16);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000937
Denys Vlasenko434f9592018-04-08 13:32:47 +0200938 h->flags32 = htonl(h->flags32); /* native endianness for lzo_compress() */
939
Denys Vlasenko15684bf2018-04-08 13:45:04 +0200940 write32(chksum_getresult(h->flags32));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000941}
942
943static int read_header(header_t *h)
944{
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000945 int l;
946 uint32_t checksum;
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200947 /* As it stands now, only h->flags32 is used by our caller.
948 * Therefore we don't store many fields in h->FIELD.
949 */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200950 unsigned h_version;
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200951 unsigned h_version_needed_to_extract;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000952
Denys Vlasenko434f9592018-04-08 13:32:47 +0200953 init_chksum();
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000954
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200955 /* We don't support versions < 0.94, since 0.94
956 * came only 2 months after 0.90:
957 * 0.90 (10 Aug 1997): First public release of lzop
958 * 0.94 (15 Oct 1997): Header format change
Denys Vlasenko434f9592018-04-08 13:32:47 +0200959 */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000960
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200961 /* Read up to and including name length byte */
962 f_read(&h->version_be16, ((char*)&h->len_and_name[1]) - ((char*)&h->version_be16));
963
964 h_version = htons(h->version_be16);
965 if (h_version < 0x0940)
966 return 3;
967 h_version_needed_to_extract = htons(h->version_needed_to_extract_be16);
968 if (h_version_needed_to_extract > LZOP_VERSION)
969 return 16;
970 if (h_version_needed_to_extract < 0x0940)
971 return 3;
972
973 if (h->method <= 0)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000974 return 14;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000975
Denys Vlasenko434f9592018-04-08 13:32:47 +0200976 /* former lzo_get_method(h): */
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200977 if (h->method == M_LZO1X_1) {
978 if (h->level == 0)
979 h->level = 3;
980 } else if (h->method == M_LZO1X_1_15) {
981 if (h->level == 0)
982 h->level = 1;
983 } else if (h->method == M_LZO1X_999) {
984 if (h->level == 0)
985 h->level = 9;
Denys Vlasenko434f9592018-04-08 13:32:47 +0200986 } else
987 return -1; /* not a LZO method */
988 /* check compression level */
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200989 if (h->level < 1 || h->level > 9)
Denys Vlasenko434f9592018-04-08 13:32:47 +0200990 return 15;
991
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200992 h->flags32 = ntohl(h->flags32);
Denys Vlasenko434f9592018-04-08 13:32:47 +0200993 if (h->flags32 & F_H_FILTER)
994 return 16; /* filter not supported */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000995 /* check reserved flags */
Denys Vlasenko434f9592018-04-08 13:32:47 +0200996 if (h->flags32 & F_RESERVED)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +0000997 return -13;
998
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +0200999 l = h->len_and_name[0];
Denys Vlasenko434f9592018-04-08 13:32:47 +02001000 if (l > 0)
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +02001001 /* UNUSED */ f_read(h->len_and_name+1, l);
1002 /* UNUSED h->len_and_name[1+l] = 0; */
Denys Vlasenko434f9592018-04-08 13:32:47 +02001003
1004 checksum = chksum_getresult(h->flags32);
Denys Vlasenko15684bf2018-04-08 13:45:04 +02001005 if (read32() != checksum)
Denys Vlasenko434f9592018-04-08 13:32:47 +02001006 return 2;
1007
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001008 /* skip extra field [not used yet] */
Denys Vlasenko434f9592018-04-08 13:32:47 +02001009 if (h->flags32 & F_H_EXTRA_FIELD) {
1010 uint32_t extra_field_len;
1011 uint32_t extra_field_checksum;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001012 uint32_t k;
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +02001013 char dummy;
Denys Vlasenko9038d6f2009-07-15 20:02:19 +02001014
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001015 /* note: the checksum also covers the length */
Denys Vlasenko434f9592018-04-08 13:32:47 +02001016 init_chksum();
1017 extra_field_len = f_read32();
1018 for (k = 0; k < extra_field_len; k++)
Denys Vlasenko3a7d16d2018-04-08 13:35:28 +02001019 f_read(&dummy, 1);
Denys Vlasenko434f9592018-04-08 13:32:47 +02001020 checksum = chksum_getresult(h->flags32);
Denys Vlasenko15684bf2018-04-08 13:45:04 +02001021 extra_field_checksum = read32();
Denys Vlasenko434f9592018-04-08 13:32:47 +02001022 if (extra_field_checksum != checksum)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001023 return 3;
1024 }
1025
1026 return 0;
1027}
1028
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001029/**********************************************************************/
1030// compress
1031/**********************************************************************/
1032static void lzo_set_method(header_t *h)
1033{
Denys Vlasenko87a08e42018-04-08 16:44:45 +02001034 smallint level;
1035
1036 /* levels 2..6 or none (defaults to level 3) */
1037 h->method = M_LZO1X_1;
1038 level = 5; /* levels 2-6 are actually the same */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001039
1040 if (option_mask32 & OPT_1) {
1041 h->method = M_LZO1X_1_15;
Denys Vlasenko87a08e42018-04-08 16:44:45 +02001042 level = 1;
1043 }
1044 if (option_mask32 & OPT_789) {
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001045#if ENABLE_LZOP_COMPR_HIGH
1046 h->method = M_LZO1X_999;
Denys Vlasenko87a08e42018-04-08 16:44:45 +02001047 level = 9;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001048 if (option_mask32 & OPT_7)
1049 level = 7;
1050 else if (option_mask32 & OPT_8)
1051 level = 8;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001052#else
James Byrne69374872019-07-02 11:35:03 +02001053 bb_simple_error_msg_and_die("high compression not compiled in");
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001054#endif
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001055 }
1056
1057 h->level = level;
1058}
1059
Denys Vlasenko48403252015-10-28 15:33:19 +01001060static int do_lzo_compress(void)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001061{
1062 header_t header;
1063
1064#define h (&header)
1065 memset(h, 0, sizeof(*h));
1066
1067 lzo_set_method(h);
1068
Denys Vlasenko434f9592018-04-08 13:32:47 +02001069 h->version_be16 = htons(LZOP_VERSION & 0xffff);
1070 h->version_needed_to_extract_be16 = htons(0x0940);
1071 h->lib_version_be16 = htons(lzo_version() & 0xffff);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001072
Denys Vlasenko434f9592018-04-08 13:32:47 +02001073 h->flags32 = htonl((F_OS & F_OS_MASK) | (F_CS & F_CS_MASK));
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001074
1075 if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
Denys Vlasenko434f9592018-04-08 13:32:47 +02001076 h->flags32 |= htonl(F_ADLER32_D);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001077 if (option_mask32 & OPT_C)
Denys Vlasenko434f9592018-04-08 13:32:47 +02001078 h->flags32 |= htonl(F_ADLER32_C);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001079 }
Denys Vlasenko434f9592018-04-08 13:32:47 +02001080
1081 /* write_header() also converts h->flags32 to native endianness */
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001082 write_header(h);
Denys Vlasenko434f9592018-04-08 13:32:47 +02001083
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001084 return lzo_compress(h);
1085#undef h
1086}
1087
1088/**********************************************************************/
1089// decompress
1090/**********************************************************************/
Denys Vlasenko48403252015-10-28 15:33:19 +01001091static int do_lzo_decompress(void)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001092{
Denys Vlasenko434f9592018-04-08 13:32:47 +02001093 int r;
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001094 header_t header;
Denys Vlasenko9038d6f2009-07-15 20:02:19 +02001095
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001096 check_magic();
Denys Vlasenko434f9592018-04-08 13:32:47 +02001097 r = read_header(&header);
1098 if (r != 0)
1099 bb_error_msg_and_die("header_error %d", r);
1100 return lzo_decompress(header.flags32);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001101}
1102
Denys Vlasenko39a04f72010-05-31 14:18:57 +02001103static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001104{
1105 if (option_mask32 & OPT_DECOMPRESS) {
1106 char *extension = strrchr(filename, '.');
1107 if (!extension || strcmp(extension + 1, "lzo") != 0)
1108 return xasprintf("%s.out", filename);
1109 *extension = '\0';
1110 return filename;
1111 }
1112 return xasprintf("%s.lzo", filename);
1113}
1114
Denys Vlasenkoe7800f32014-12-07 00:42:49 +01001115static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001116{
1117 if (option_mask32 & OPT_DECOMPRESS)
1118 return do_lzo_decompress();
1119 return do_lzo_compress();
1120}
1121
1122int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1123int lzop_main(int argc UNUSED_PARAM, char **argv)
1124{
Denys Vlasenko434f9592018-04-08 13:32:47 +02001125 INIT_G();
1126
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001127 getopt32(argv, OPTION_STRING);
1128 argv += optind;
Denys Vlasenko1c013fa2017-06-15 18:07:04 +02001129 /* -U is "anti -k", invert bit for bbunpack(): */
1130 option_mask32 ^= OPT_KEEP;
1131 /* -k disables -U (if any): */
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001132 /* opt_complementary "k-U"? - nope, only handles -Uk, not -kU */
Denys Vlasenko1c013fa2017-06-15 18:07:04 +02001133 if (option_mask32 & OPT_k)
1134 option_mask32 |= OPT_KEEP;
1135
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001136 /* lzopcat? */
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +01001137 if (ENABLE_LZOPCAT && applet_name[4] == 'c')
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001138 option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
1139 /* unlzop? */
Denys Vlasenkoeb3fdc82016-11-16 15:45:05 +01001140 if (ENABLE_UNLZOP && applet_name[4] == 'o')
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001141 option_mask32 |= OPT_DECOMPRESS;
1142
Denys Vlasenkoddacb032018-02-01 10:56:19 +01001143 global_crc32_new_table_le();
Denys Vlasenko39a04f72010-05-31 14:18:57 +02001144 return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);
Denis Vlasenko052ad9a2009-04-29 12:01:51 +00001145}