stages: 1/18-busybox: upgraded to version 1.30.0
[dragora.git] / patches / busybox / busybox-1.29.1_lzip-0.diff
blob3340b851b50ed2d9e90303e617152c4907aafd05
1 diff -urdN busybox-1.29.1.old/archival/Config.src busybox-1.29.1.new/archival/Config.src
2 --- busybox-1.29.1.old/archival/Config.src 2018-07-02 13:23:06.000000000 +0200
3 +++ busybox-1.29.1.new/archival/Config.src 2018-07-17 00:03:25.000000000 +0200
4 @@ -21,6 +21,10 @@
5 bool "Make tar, rpm, modprobe etc understand .gz data"
6 default y
8 +config FEATURE_SEAMLESS_LZ
9 + bool "Make tar, rpm, modprobe etc understand .lz data"
10 + default y
12 config FEATURE_SEAMLESS_Z
13 bool "Make tar, rpm, modprobe etc understand .Z data"
14 default n # it is ancient
15 diff -urdN busybox-1.29.1.old/archival/bbunzip.c busybox-1.29.1.new/archival/bbunzip.c
16 --- busybox-1.29.1.old/archival/bbunzip.c 2018-07-02 13:23:06.000000000 +0200
17 +++ busybox-1.29.1.new/archival/bbunzip.c 2018-07-19 12:53:03.000000000 +0200
18 @@ -194,18 +194,31 @@
20 #if ENABLE_UNCOMPRESS \
21 || ENABLE_FEATURE_BZIP2_DECOMPRESS \
22 + || ENABLE_FEATURE_LZIP_DECOMPRESS \
23 || ENABLE_UNLZMA || ENABLE_LZCAT || ENABLE_LZMA \
24 || ENABLE_UNXZ || ENABLE_XZCAT || ENABLE_XZ
25 static
26 char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
28 char *extension = strrchr(filename, '.');
29 - if (!extension || strcmp(extension + 1, expected_ext) != 0) {
31 + if (!extension)
32 + return NULL;
34 + if (strcmp(extension + 1, expected_ext) == 0) {
35 + *extension = '\0';
36 + } else if (extension[1] == 't' && strlen(expected_ext) >= 2 &&
37 + strcmp(extension + 2, expected_ext) == 0) {
38 + filename = xstrdup(filename);
39 + extension = strrchr(filename, '.');
40 + extension[2] = 'a';
41 + extension[3] = 'r';
42 + extension[4] = '\0';
43 + } else {
44 /* Mimic GNU gunzip - "real" bunzip2 tries to */
45 /* unpack file anyway, to file.out */
46 return NULL;
48 - *extension = '\0';
49 return filename;
51 #endif
52 @@ -449,6 +462,38 @@
54 #endif
57 +//config:config LUNZIP
58 +//config: bool "lunzip"
59 +//config: default y
60 +//config: select FEATURE_LZIP_DECOMPRESS
61 +//config: help
62 +//config: lunzip is used to decompress archives created by lzip.
63 +//config: You can use the '-t' option to test the integrity of
64 +//config: an archive, without decompressing it.
66 +//applet:IF_LUNZIP(APPLET(lunzip, BB_DIR_USR_BIN, BB_SUID_DROP))
67 +//kbuild:lib-$(CONFIG_LUNZIP) += bbunzip.o
69 +//usage:#define lunzip_trivial_usage
70 +//usage: "[-cft] [FILE]..."
71 +//usage:#define lunzip_full_usage "\n\n"
72 +//usage: "Decompress FILEs (or stdin)\n"
73 +//usage: "\n -c Write to stdout"
74 +//usage: "\n -f Force"
75 +//usage: "\n -k Keep input files"
76 +//usage: "\n -t Test file integrity"
77 +#if ENABLE_FEATURE_LZIP_DECOMPRESS
78 +int lunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
79 +int lunzip_main(int argc UNUSED_PARAM, char **argv)
81 + getopt32(argv, BBUNPK_OPTSTR "dt");
82 + argv += optind;
84 + return bbunpack(argv, unpack_lz_stream, make_new_name_generic, "lz");
86 +#endif
90 * Small lzma deflate implementation.
91 diff -urdN busybox-1.29.1.old/archival/dpkg.c busybox-1.29.1.new/archival/dpkg.c
92 --- busybox-1.29.1.old/archival/dpkg.c 2018-07-02 13:23:06.000000000 +0200
93 +++ busybox-1.29.1.new/archival/dpkg.c 2018-07-17 00:03:25.000000000 +0200
94 @@ -1482,6 +1482,9 @@
95 #if ENABLE_FEATURE_SEAMLESS_XZ
96 llist_add_to(&(ar_handle->accept), (char*)"control.tar.xz");
97 #endif
98 +#if ENABLE_FEATURE_SEAMLESS_LZ
99 + llist_add_to(&(ar_handle->accept), (char*)"control.tar.lz");
100 +#endif
102 /* Assign the tar handle as a subarchive of the ar handle */
103 ar_handle->dpkg__sub_archive = tar_handle;
104 @@ -1509,6 +1512,9 @@
105 #if ENABLE_FEATURE_SEAMLESS_XZ
106 llist_add_to(&(ar_handle->accept), (char*)"data.tar.xz");
107 #endif
108 +#if ENABLE_FEATURE_SEAMLESS_LZ
109 + llist_add_to(&(ar_handle->accept), (char*)"data.tar.lz");
110 +#endif
112 /* Assign the tar handle as a subarchive of the ar handle */
113 ar_handle->dpkg__sub_archive = tar_handle;
114 diff -urdN busybox-1.29.1.old/archival/dpkg_deb.c busybox-1.29.1.new/archival/dpkg_deb.c
115 --- busybox-1.29.1.old/archival/dpkg_deb.c 2018-07-02 13:23:06.000000000 +0200
116 +++ busybox-1.29.1.new/archival/dpkg_deb.c 2018-07-17 00:03:25.000000000 +0200
117 @@ -69,6 +69,10 @@
118 llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
119 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
120 #endif
121 +#if ENABLE_FEATURE_SEAMLESS_LZ
122 + llist_add_to(&ar_archive->accept, (char*)"data.tar.lz");
123 + llist_add_to(&control_tar_llist, (char*)"control.tar.lz");
124 +#endif
125 #if ENABLE_FEATURE_SEAMLESS_LZMA
126 llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
127 llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
128 diff -urdN busybox-1.29.1.old/archival/libarchive/Kbuild.src busybox-1.29.1.new/archival/libarchive/Kbuild.src
129 --- busybox-1.29.1.old/archival/libarchive/Kbuild.src 2018-07-02 13:23:06.000000000 +0200
130 +++ busybox-1.29.1.new/archival/libarchive/Kbuild.src 2018-07-19 13:03:34.000000000 +0200
131 @@ -37,6 +37,7 @@
132 get_header_tar.o \
133 get_header_tar_gz.o \
134 get_header_tar_bz2.o \
135 + get_header_tar_lz.o \
136 get_header_tar_lzma.o \
137 get_header_tar_xz.o \
139 @@ -56,6 +57,8 @@
140 # 'bzip2 -d', bunzip2 or bzcat selects FEATURE_BZIP2_DECOMPRESS
141 lib-$(CONFIG_FEATURE_BZIP2_DECOMPRESS) += open_transformer.o decompress_bunzip2.o
142 lib-$(CONFIG_FEATURE_UNZIP_BZIP2) += open_transformer.o decompress_bunzip2.o
143 +# 'lzip -d' or lunzip select FEATURE_LZIP_DECOMPRESS
144 +lib-$(CONFIG_FEATURE_LZIP_DECOMPRESS) += open_transformer.o decompress_lunzip.o
145 lib-$(CONFIG_UNLZMA) += open_transformer.o decompress_unlzma.o
146 lib-$(CONFIG_LZCAT) += open_transformer.o decompress_unlzma.o
147 lib-$(CONFIG_LZMA) += open_transformer.o decompress_unlzma.o
148 @@ -72,6 +75,7 @@
149 lib-$(CONFIG_RPM) += open_transformer.o decompress_gunzip.o get_header_cpio.o
150 lib-$(CONFIG_GZIP) += open_transformer.o
151 lib-$(CONFIG_BZIP2) += open_transformer.o
152 +lib-$(CONFIG_LZIP) += open_transformer.o
153 lib-$(CONFIG_LZOP) += open_transformer.o
154 lib-$(CONFIG_MAN) += open_transformer.o
155 lib-$(CONFIG_SETFONT) += open_transformer.o
156 @@ -87,6 +91,7 @@
157 lib-$(CONFIG_FEATURE_SEAMLESS_Z) += open_transformer.o decompress_uncompress.o
158 lib-$(CONFIG_FEATURE_SEAMLESS_GZ) += open_transformer.o decompress_gunzip.o
159 lib-$(CONFIG_FEATURE_SEAMLESS_BZ2) += open_transformer.o decompress_bunzip2.o
160 +lib-$(CONFIG_FEATURE_SEAMLESS_LZ) += open_transformer.o decompress_lunzip.o
161 lib-$(CONFIG_FEATURE_SEAMLESS_LZMA) += open_transformer.o decompress_unlzma.o
162 lib-$(CONFIG_FEATURE_SEAMLESS_XZ) += open_transformer.o decompress_unxz.o
163 lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += open_transformer.o decompress_bunzip2.o
164 diff -urdN busybox-1.29.1.old/archival/libarchive/decompress_lunzip.c busybox-1.29.1.new/archival/libarchive/decompress_lunzip.c
165 --- busybox-1.29.1.old/archival/libarchive/decompress_lunzip.c 1970-01-01 01:00:00.000000000 +0100
166 +++ busybox-1.29.1.new/archival/libarchive/decompress_lunzip.c 2018-07-18 12:26:07.000000000 +0200
167 @@ -0,0 +1,497 @@
169 + * lunzip implementation for busybox
171 + * Copyright (C) 2012-2018 Antonio Diaz Diaz.
173 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
174 + */
176 +#include "libbb.h"
177 +#include "bb_archive.h"
178 +#include "lzip.h"
180 +/* Some functions have been marked with __always_inline because xz does
181 + it, giving the impression that unxz is much faster than lunzip. */
182 +#ifndef __always_inline
183 +# ifdef __GNUC__
184 +# define __always_inline \
185 + inline __attribute__((__always_inline__))
186 +# else
187 +# define __always_inline inline
188 +# endif
189 +#endif
192 +enum { rd_buffer_size = 16384 };
194 +struct Range_decoder {
195 + unsigned long long partial_member_pos;
196 + uint8_t *buffer; /* input buffer */
197 + int pos; /* current pos in buffer */
198 + int stream_pos; /* when reached, a new block must be read */
199 + uint32_t code;
200 + uint32_t range;
201 + int infd; /* input file descriptor */
202 + bool at_stream_end;
206 +static bool Rd_read_block(struct Range_decoder * const rdec)
208 + if (!rdec->at_stream_end) {
209 + rdec->stream_pos =
210 + full_read(rdec->infd, rdec->buffer, rd_buffer_size);
211 + rdec->at_stream_end = (rdec->stream_pos < rd_buffer_size);
212 + rdec->partial_member_pos += rdec->pos;
213 + rdec->pos = 0;
215 + return rdec->pos < rdec->stream_pos;
219 +static bool Rd_init(struct Range_decoder * const rdec, const int ifd,
220 + const bool magic_skipped)
222 + rdec->partial_member_pos = (magic_skipped ? 4 : 0);
223 + rdec->buffer = (uint8_t *) malloc(rd_buffer_size);
224 + if (!rdec->buffer) return false;
225 + rdec->pos = 0;
226 + rdec->stream_pos = 0;
227 + rdec->code = 0;
228 + rdec->range = 0xFFFFFFFFU;
229 + rdec->infd = ifd;
230 + rdec->at_stream_end = false;
231 + return true;
234 +static __always_inline bool Rd_finished(struct Range_decoder * const rdec)
236 + return rdec->pos >= rdec->stream_pos && !Rd_read_block(rdec);
239 +static inline unsigned long long
240 +Rd_member_position(const struct Range_decoder * const rdec)
242 + return rdec->partial_member_pos + rdec->pos;
245 +static inline void Rd_reset_member_position(struct Range_decoder * const rdec)
247 + rdec->partial_member_pos = 0; rdec->partial_member_pos -= rdec->pos;
250 +static __always_inline uint8_t Rd_get_byte(struct Range_decoder * const rdec)
252 + /* 0xFF avoids decoder error if member is truncated at EOS marker */
253 + if (Rd_finished(rdec)) return 0xFF;
254 + return rdec->buffer[rdec->pos++];
257 +static void Rd_load(struct Range_decoder * const rdec)
259 + int i;
260 + rdec->code = 0;
261 + for (i = 0; i < 5; ++i)
262 + rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
263 + rdec->range = 0xFFFFFFFFU;
266 +static __always_inline void Rd_normalize(struct Range_decoder * const rdec)
268 + if (rdec->range <= 0x00FFFFFFU) {
269 + rdec->range <<= 8;
270 + rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
274 +static unsigned Rd_decode(struct Range_decoder * const rdec,
275 + const int num_bits)
277 + unsigned symbol = 0;
278 + int i;
279 + for (i = num_bits; i > 0; --i) {
280 + bool bit;
281 + Rd_normalize(rdec);
282 + rdec->range >>= 1;
283 + /* symbol <<= 1; */
284 + /* if(rdec->code >= rdec->range) { rdec->code -= rdec->range; symbol |= 1; } */
285 + bit = (rdec->code >= rdec->range);
286 + symbol = (symbol << 1) + bit;
287 + rdec->code -= rdec->range & (0U - bit);
289 + return symbol;
292 +static __always_inline unsigned Rd_decode_bit(struct Range_decoder * const rdec,
293 + Bit_model * const probability)
295 + uint32_t bound;
296 + Rd_normalize(rdec);
297 + bound = (rdec->range >> bit_model_total_bits) * *probability;
298 + if (rdec->code < bound) {
299 + rdec->range = bound;
300 + *probability += (bit_model_total - *probability) >> bit_model_move_bits;
301 + return 0;
302 + } else {
303 + rdec->range -= bound;
304 + rdec->code -= bound;
305 + *probability -= *probability >> bit_model_move_bits;
306 + return 1;
310 +static __always_inline unsigned Rd_decode_tree(struct Range_decoder * const rdec,
311 + Bit_model bm[], const int num_bits)
313 + unsigned symbol = 1;
314 + int i;
315 + for (i = num_bits; i > 0; --i)
316 + symbol = (symbol << 1) | Rd_decode_bit(rdec, &bm[symbol]);
317 + return symbol - (1 << num_bits);
320 +static __always_inline unsigned Rd_decode_tree_reversed(struct Range_decoder * const rdec,
321 + Bit_model bm[], const int num_bits)
323 + unsigned model = 1;
324 + unsigned symbol = 0;
325 + int i;
326 + for (i = 0; i < num_bits; ++i) {
327 + const unsigned bit = Rd_decode_bit(rdec, &bm[model]);
328 + model = (model << 1) + bit;
329 + symbol |= (bit << i);
331 + return symbol;
334 +static unsigned Rd_decode_matched(struct Range_decoder * const rdec,
335 + Bit_model bm[], unsigned match_byte)
337 + unsigned symbol = 1;
338 + unsigned mask = 0x100;
339 + while(true) {
340 + const unsigned match_bit = (match_byte <<= 1) & mask;
341 + const unsigned bit = Rd_decode_bit(rdec, &bm[symbol+match_bit+mask]);
342 + symbol = (symbol << 1) + bit;
343 + if (symbol >= 0x100) return symbol & 0xFF;
344 + mask &= ~(match_bit ^ (bit << 8)); /* if( match_bit != bit ) mask = 0; */
348 +static __always_inline unsigned Rd_decode_len(struct Range_decoder * const rdec,
349 + struct Len_model * const lm,
350 + const int pos_state)
352 + if (Rd_decode_bit(rdec, &lm->choice1) == 0)
353 + return Rd_decode_tree(rdec, lm->bm_low[pos_state], len_low_bits);
354 + if (Rd_decode_bit(rdec, &lm->choice2) == 0)
355 + return len_low_symbols +
356 + Rd_decode_tree(rdec, lm->bm_mid[pos_state], len_mid_bits);
357 + return len_low_symbols + len_mid_symbols +
358 + Rd_decode_tree(rdec, lm->bm_high, len_high_bits);
362 +struct LZ_decoder {
363 + unsigned long long partial_data_pos;
364 + struct Range_decoder *rdec;
365 + unsigned dictionary_size;
366 + uint8_t *buffer; /* output buffer */
367 + unsigned pos; /* current pos in buffer */
368 + unsigned stream_pos; /* first byte not yet written to file */
369 + uint32_t crc;
370 + int outfd; /* output file descriptor */
371 + bool pos_wrapped;
372 + bool write_error;
375 +static void LZd_flush_data(struct LZ_decoder * const d)
377 + if (d->pos > d->stream_pos) {
378 + const int size = d->pos - d->stream_pos;
379 + d->crc = crc32_block_endian0(d->crc, d->buffer + d->stream_pos,
380 + size, global_crc32_table);
381 + if (d->outfd >= 0 && full_write(d->outfd,
382 + d->buffer + d->stream_pos, size) != size)
383 + d->write_error = true;
384 + if (d->pos >= d->dictionary_size) {
385 + d->partial_data_pos += d->pos;
386 + d->pos = 0;
387 + d->pos_wrapped = true;
389 + d->stream_pos = d->pos;
393 +static __always_inline uint8_t LZd_peek_prev(const struct LZ_decoder * const d)
395 + const unsigned i = ((d->pos > 0) ? d->pos : d->dictionary_size) - 1;
396 + return d->buffer[i];
399 +static __always_inline uint8_t LZd_peek(const struct LZ_decoder * const d,
400 + const unsigned distance)
402 + unsigned i = d->pos - distance - 1;
403 + if (d->pos <= distance) i += d->dictionary_size;
404 + return d->buffer[i];
407 +static __always_inline void LZd_put_byte(struct LZ_decoder * const d,
408 + const uint8_t b)
410 + d->buffer[d->pos] = b;
411 + if (++d->pos >= d->dictionary_size) LZd_flush_data(d);
414 +static void LZd_copy_block(struct LZ_decoder * const d,
415 + const unsigned distance, unsigned len)
417 + unsigned i = d->pos - distance - 1;
418 + bool fast;
419 + if (d->pos <= distance) {
420 + i += d->dictionary_size;
421 + fast = (len <= d->dictionary_size - i && len <= i - d->pos);
423 + else
424 + fast = (len < d->dictionary_size - d->pos && len <= d->pos - i);
425 + if( fast ) { /* no wrap, no overlap */
426 + memcpy(d->buffer + d->pos, d->buffer + i, len);
427 + d->pos += len;
428 + } else for (; len > 0; --len) {
429 + d->buffer[d->pos] = d->buffer[i];
430 + if (++d->pos >= d->dictionary_size) LZd_flush_data(d);
431 + if (++i >= d->dictionary_size) i = 0;
435 +static bool LZd_init(struct LZ_decoder * const d,
436 + struct Range_decoder * const rde,
437 + const unsigned dict_size, const int ofd)
439 + d->partial_data_pos = 0;
440 + d->rdec = rde;
441 + d->dictionary_size = dict_size;
442 + d->buffer = (uint8_t *) malloc(d->dictionary_size);
443 + if (!d->buffer) return false;
444 + d->pos = 0;
445 + d->stream_pos = 0;
446 + d->crc = 0xFFFFFFFFU;
447 + d->outfd = ofd;
448 + d->pos_wrapped = false;
449 + d->write_error = false;
450 + d->buffer[d->dictionary_size - 1] = 0; /* prev_byte of first byte */
451 + return true;
454 +static inline uint32_t LZd_crc(const struct LZ_decoder * const d)
456 + return d->crc ^ 0xFFFFFFFFU;
459 +static __always_inline unsigned long long
460 +LZd_data_position(const struct LZ_decoder * const d)
462 + return d->partial_data_pos + d->pos;
466 +static bool LZd_verify_trailer(struct LZ_decoder * const d)
468 + Lzip_trailer trailer;
469 + int i = 0;
470 + while (i < Lt_size)
471 + trailer[i++] = Rd_get_byte(d->rdec);
473 + return (Lt_get_data_crc(trailer) == LZd_crc(d) &&
474 + Lt_get_data_size(trailer) == LZd_data_position(d) &&
475 + Lt_get_member_size(trailer) == Rd_member_position(d->rdec));
479 +/* Return value: -1 = write error, 0 = OK, 1 = data error. */
480 +static int LZd_decode_member(struct LZ_decoder * const d)
482 + struct Range_decoder * const rdec = d->rdec;
483 + Bit_model bm_literal[1 << literal_context_bits][0x300];
484 + Bit_model bm_match[states][pos_states];
485 + Bit_model bm_rep[states];
486 + Bit_model bm_rep0[states];
487 + Bit_model bm_rep1[states];
488 + Bit_model bm_rep2[states];
489 + Bit_model bm_len[states][pos_states];
490 + Bit_model bm_dis_slot[len_states][1 << dis_slot_bits];
491 + Bit_model bm_dis[modeled_distances-end_dis_model+1];
492 + Bit_model bm_align[dis_align_size];
493 + struct Len_model match_len_model;
494 + struct Len_model rep_len_model;
495 + unsigned rep0 = 0; /* rep[0-3] latest four distances */
496 + unsigned rep1 = 0; /* used for efficient coding of */
497 + unsigned rep2 = 0; /* repeated distances */
498 + unsigned rep3 = 0;
499 + State state = 0;
501 + Bm_array_init(bm_literal[0], (1 << literal_context_bits) * 0x300);
502 + Bm_array_init(bm_match[0], states * pos_states);
503 + Bm_array_init(bm_rep, states);
504 + Bm_array_init(bm_rep0, states);
505 + Bm_array_init(bm_rep1, states);
506 + Bm_array_init(bm_rep2, states);
507 + Bm_array_init(bm_len[0], states * pos_states);
508 + Bm_array_init(bm_dis_slot[0], len_states * (1 << dis_slot_bits));
509 + Bm_array_init(bm_dis, modeled_distances - end_dis_model + 1);
510 + Bm_array_init(bm_align, dis_align_size);
511 + Lm_init(&match_len_model);
512 + Lm_init(&rep_len_model);
514 + Rd_load(rdec);
515 + while (!Rd_finished(rdec)) {
516 + int len;
517 + const int pos_state = LZd_data_position(d) & pos_state_mask;
518 + if (Rd_decode_bit(rdec, &bm_match[state][pos_state]) == 0) {
519 + /* literal byte */
520 + Bit_model * const bm = bm_literal[get_lit_state(LZd_peek_prev(d))];
521 + if (St_is_char(state)) {
522 + state -= (state < 4) ? state : 3;
523 + LZd_put_byte(d, Rd_decode_tree(rdec, bm, 8));
524 + } else {
525 + state -= (state < 10) ? 3 : 6;
526 + LZd_put_byte(d, Rd_decode_matched(rdec, bm, LZd_peek(d, rep0)));
528 + continue;
530 + /* match or repeated match */
531 + if (Rd_decode_bit(rdec, &bm_rep[state]) != 0) {
532 + if (Rd_decode_bit(rdec, &bm_rep0[state]) == 0) {
533 + if (Rd_decode_bit(rdec, &bm_len[state][pos_state]) == 0) {
534 + state = St_set_short_rep(state);
535 + LZd_put_byte(d, LZd_peek(d, rep0));
536 + continue;
538 + } else {
539 + unsigned distance;
540 + if (Rd_decode_bit(rdec, &bm_rep1[state]) == 0)
541 + distance = rep1;
542 + else {
543 + if (Rd_decode_bit(rdec, &bm_rep2[state]) == 0)
544 + distance = rep2;
545 + else {
546 + distance = rep3;
547 + rep3 = rep2;
549 + rep2 = rep1;
551 + rep1 = rep0;
552 + rep0 = distance;
554 + state = St_set_rep(state);
555 + len = min_match_len + Rd_decode_len(rdec, &rep_len_model, pos_state);
556 + } else { /* match */
557 + unsigned distance;
558 + len = min_match_len + Rd_decode_len(rdec, &match_len_model, pos_state);
559 + distance = Rd_decode_tree(rdec, bm_dis_slot[get_len_state(len)], 6);
560 + if (distance >= start_dis_model) {
561 + const unsigned dis_slot = distance;
562 + const int direct_bits = (dis_slot >> 1) - 1;
563 + distance = (2 | (dis_slot & 1)) << direct_bits;
564 + if (dis_slot < end_dis_model)
565 + distance += Rd_decode_tree_reversed(rdec,
566 + bm_dis + (distance - dis_slot), direct_bits);
567 + else {
568 + distance +=
569 + Rd_decode(rdec, direct_bits - dis_align_bits) << dis_align_bits;
570 + distance += Rd_decode_tree_reversed(rdec, bm_align, dis_align_bits);
571 + if (distance == 0xFFFFFFFFU) { /* marker found */
572 + Rd_normalize(rdec);
573 + LZd_flush_data(d);
574 + if (d->write_error) return -1;
575 + if (len == min_match_len && /* End Of Stream marker */
576 + LZd_verify_trailer(d))
577 + return 0;
578 + if (len == min_match_len + 1) { /* Sync Flush marker */
579 + Rd_load(rdec);
580 + continue;
582 + return 1;
586 + rep3 = rep2; rep2 = rep1; rep1 = rep0; rep0 = distance;
587 + state = St_set_match(state);
588 + if (rep0 >= d->dictionary_size ||
589 + (rep0 >= d->pos && !d->pos_wrapped)) {
590 + LZd_flush_data(d);
591 + return 1;
594 + LZd_copy_block(d, rep0, len);
596 + LZd_flush_data(d);
597 + return 1;
601 +IF_DESKTOP(long long) int FAST_FUNC
602 +unpack_lz_stream(transformer_state_t *xstate)
604 + IF_DESKTOP(long long) int total = 0;
605 + struct Range_decoder rdec;
606 + bool first_member;
607 + const bool magic_skipped = (xstate->signature_skipped != 0);
609 + if (!global_crc32_table)
610 + global_crc32_new_table_le();
612 + if (!Rd_init(&rdec, xstate->src_fd, magic_skipped))
613 + return -1;
615 + for (first_member = true;; first_member = false) {
616 + int tmp = 0;
617 + Lzip_header header;
618 + struct LZ_decoder decoder;
620 + if (first_member && magic_skipped) {
621 + Lh_set_magic(header);
622 + tmp = 4;
623 + } else {
624 + Rd_reset_member_position(&rdec);
626 + while (tmp < Lh_size)
627 + header[tmp++] = Rd_get_byte(&rdec);
628 + if (Rd_finished(&rdec)) { /* End Of File */
629 + if (first_member) {
630 + bb_error_msg(bb_msg_read_error);
631 + total = -1;
633 + break;
635 + tmp = Lh_get_dictionary_size(header);
636 + if (!Lh_verify_magic(header) || tmp < min_dictionary_size ||
637 + tmp > max_dictionary_size) {
638 + if (!first_member)
639 + break; /* trailing garbage */
640 + bb_error_msg("invalid magic");
641 + total = -1;
642 + break;
645 + if (!LZd_init(&decoder, &rdec, tmp, xstate->dst_fd)) {
646 + bb_error_msg(bb_msg_memory_exhausted);
647 + total = -1;
648 + break;
650 + tmp = LZd_decode_member(&decoder);
651 + IF_DESKTOP(total += Rd_member_position(&rdec);)
652 + free(decoder.buffer);
653 + if (tmp != 0) {
654 + if (tmp < 0)
655 + bb_perror_msg(bb_msg_write_error);
656 + else
657 + bb_error_msg("corrupted data");
658 + total = -1;
659 + break;
662 + free(rdec.buffer);
663 + return total;
665 diff -urdN busybox-1.29.1.old/archival/libarchive/filter_accept_list_reassign.c busybox-1.29.1.new/archival/libarchive/filter_accept_list_reassign.c
666 --- busybox-1.29.1.old/archival/libarchive/filter_accept_list_reassign.c 2018-07-02 13:23:06.000000000 +0200
667 +++ busybox-1.29.1.new/archival/libarchive/filter_accept_list_reassign.c 2018-07-17 00:03:25.000000000 +0200
668 @@ -43,6 +43,12 @@
669 archive_handle->dpkg__action_data_subarchive = get_header_tar_bz2;
670 return EXIT_SUCCESS;
672 + if (ENABLE_FEATURE_SEAMLESS_LZ
673 + && strcmp(name_ptr, "lz") == 0
674 + ) {
675 + archive_handle->dpkg__action_data_subarchive = get_header_tar_lz;
676 + return EXIT_SUCCESS;
678 if (ENABLE_FEATURE_SEAMLESS_LZMA
679 && strcmp(name_ptr, "lzma") == 0
681 diff -urdN busybox-1.29.1.old/archival/libarchive/get_header_tar_lz.c busybox-1.29.1.new/archival/libarchive/get_header_tar_lz.c
682 --- busybox-1.29.1.old/archival/libarchive/get_header_tar_lz.c 1970-01-01 01:00:00.000000000 +0100
683 +++ busybox-1.29.1.new/archival/libarchive/get_header_tar_lz.c 2018-07-19 20:26:44.000000000 +0200
684 @@ -0,0 +1,20 @@
685 +/* vi: set sw=4 ts=4: */
687 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
688 + */
689 +#include "libbb.h"
690 +#include "bb_archive.h"
692 +char FAST_FUNC get_header_tar_lz(archive_handle_t *archive_handle)
694 + /* Can't lseek over pipes */
695 + archive_handle->seek = seek_by_read;
697 + fork_transformer_with_sig(archive_handle->src_fd, unpack_lz_stream, "lunzip");
698 + archive_handle->offset = 0;
699 + while (get_header_tar(archive_handle) == EXIT_SUCCESS)
700 + continue;
702 + /* Can only do one file at a time */
703 + return EXIT_FAILURE;
705 diff -urdN busybox-1.29.1.old/archival/libarchive/lzip.h busybox-1.29.1.new/archival/libarchive/lzip.h
706 --- busybox-1.29.1.old/archival/libarchive/lzip.h 1970-01-01 01:00:00.000000000 +0100
707 +++ busybox-1.29.1.new/archival/libarchive/lzip.h 2018-07-17 14:06:36.000000000 +0200
708 @@ -0,0 +1,249 @@
709 +/* Lzip - LZMA lossless data compressor
710 + Copyright (C) 2008-2018 Antonio Diaz Diaz.
712 + This program is free software: you can redistribute it and/or modify
713 + it under the terms of the GNU General Public License as published by
714 + the Free Software Foundation, either version 2 of the License, or
715 + (at your option) any later version.
717 + This program is distributed in the hope that it will be useful,
718 + but WITHOUT ANY WARRANTY; without even the implied warranty of
719 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
720 + GNU General Public License for more details.
722 + You should have received a copy of the GNU General Public License
723 + along with this program. If not, see <http://www.gnu.org/licenses/>.
726 +typedef int State;
728 +enum { states = 12 };
730 +static inline bool St_is_char(const State st) { return st < 7; }
732 +static inline State St_set_char(const State st)
734 + static const State next[states] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
735 + return next[st];
738 +static inline State St_set_match(const State st)
740 + return ((st < 7) ? 7 : 10);
743 +static inline State St_set_rep(const State st)
745 + return ((st < 7) ? 8 : 11);
748 +static inline State St_set_short_rep(const State st)
750 + return ((st < 7) ? 9 : 11);
754 +enum {
755 + min_dictionary_bits = 12,
756 + min_dictionary_size = 1 << min_dictionary_bits, /* >= modeled_distances */
757 + max_dictionary_bits = 29,
758 + max_dictionary_size = 1 << max_dictionary_bits,
759 + literal_context_bits = 3,
760 + pos_state_bits = 2,
761 + pos_states = 1 << pos_state_bits,
762 + pos_state_mask = pos_states - 1,
764 + len_states = 4,
765 + dis_slot_bits = 6,
766 + start_dis_model = 4,
767 + end_dis_model = 14,
768 + modeled_distances = 1 << (end_dis_model / 2), /* 128 */
769 + dis_align_bits = 4,
770 + dis_align_size = 1 << dis_align_bits,
772 + len_low_bits = 3,
773 + len_mid_bits = 3,
774 + len_high_bits = 8,
775 + len_low_symbols = 1 << len_low_bits,
776 + len_mid_symbols = 1 << len_mid_bits,
777 + len_high_symbols = 1 << len_high_bits,
778 + max_len_symbols = len_low_symbols + len_mid_symbols + len_high_symbols,
780 + min_match_len = 2, /* must be 2 */
781 + max_match_len = min_match_len + max_len_symbols - 1, /* 273 */
782 + min_match_len_limit = 5,
784 + lz_num_models =
785 + ((1 << literal_context_bits) * 0x300) +
786 + (2 * states * pos_states) +
787 + (4 * states) +
788 + (len_states * (1 << dis_slot_bits)) +
789 + (modeled_distances - end_dis_model + 1) +
790 + dis_align_size,
793 +static inline int get_len_state(const int len)
795 + return MIN(len - min_match_len, len_states - 1);
798 +static inline int get_lit_state(const uint8_t prev_byte)
800 + return (prev_byte >> (8 - literal_context_bits));
804 +enum { bit_model_move_bits = 5,
805 + bit_model_total_bits = 11,
806 + bit_model_total = 1 << bit_model_total_bits
809 +typedef int Bit_model;
811 +static inline void Bm_init(Bit_model * const probability)
813 + *probability = bit_model_total / 2;
816 +static inline void Bm_array_init(Bit_model bm[], const int size)
818 + int i;
819 + for (i = 0; i < size; ++i)
820 + Bm_init(&bm[i]);
823 +struct Len_model {
824 + Bit_model choice1;
825 + Bit_model choice2;
826 + Bit_model bm_low[pos_states][len_low_symbols];
827 + Bit_model bm_mid[pos_states][len_mid_symbols];
828 + Bit_model bm_high[len_high_symbols];
831 +static inline void Lm_init(struct Len_model * const lm)
833 + Bm_init(&lm->choice1);
834 + Bm_init(&lm->choice2);
835 + Bm_array_init(lm->bm_low[0], pos_states * len_low_symbols);
836 + Bm_array_init(lm->bm_mid[0], pos_states * len_mid_symbols);
837 + Bm_array_init(lm->bm_high, len_high_symbols);
841 +static inline int real_bits(unsigned value)
843 + int bits = 0;
844 + while(value > 0) { value >>= 1; ++bits; }
845 + return bits;
849 +static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
851 +typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */
852 + /* 4 version */
853 + /* 5 coded_dict_size */
854 +enum { Lh_size = 6 };
856 +static inline void Lh_set_magic(Lzip_header data)
858 + memcpy(data, lzip_magic, 4);
859 + data[4] = 1;
862 +static inline bool Lh_verify_magic(const Lzip_header data)
864 + return (memcmp(data, lzip_magic, 4) == 0 && data[4] == 1);
867 +static inline unsigned Lh_get_dictionary_size(const Lzip_header data)
869 + unsigned sz = (1 << (data[5] & 0x1F));
870 + if (sz > min_dictionary_size)
871 + sz -= (sz / 16) * ((data[5] >> 5) & 7);
872 + return sz;
875 +static inline bool Lh_set_dictionary_size(Lzip_header data, const unsigned sz)
877 + if (sz < min_dictionary_size || sz > max_dictionary_size) return false;
878 + data[5] = real_bits(sz - 1);
879 + if (sz > min_dictionary_size) {
880 + const unsigned base_size = 1 << data[5];
881 + const unsigned fraction = base_size / 16;
882 + unsigned i;
883 + for (i = 7; i >= 1; --i)
884 + if (base_size - (i * fraction) >= sz) {
885 + data[5] |= (i << 5);
886 + break;
889 + return true;
893 +typedef uint8_t Lzip_trailer[20];
894 + /* 0-3 CRC32 of the uncompressed data */
895 + /* 4-11 size of the uncompressed data */
896 + /* 12-19 member size including header and trailer */
897 +enum { Lt_size = 20 };
899 +static inline unsigned Lt_get_data_crc(const Lzip_trailer data)
901 + unsigned tmp = 0;
902 + int i;
903 + for (i = 3; i >= 0; --i) {
904 + tmp <<= 8;
905 + tmp += data[i];
907 + return tmp;
910 +static inline void Lt_set_data_crc(Lzip_trailer data, unsigned crc)
912 + int i;
913 + for (i = 0; i <= 3; ++i) {
914 + data[i] = (uint8_t)crc;
915 + crc >>= 8;
919 +static inline unsigned long long Lt_get_data_size(const Lzip_trailer data)
921 + unsigned long long tmp = 0;
922 + int i;
923 + for (i = 11; i >= 4; --i) {
924 + tmp <<= 8;
925 + tmp += data[i];
927 + return tmp;
930 +static inline void Lt_set_data_size(Lzip_trailer data, unsigned long long sz)
932 + int i;
933 + for (i = 4; i <= 11; ++i) {
934 + data[i] = (uint8_t)sz;
935 + sz >>= 8;
939 +static inline unsigned long long Lt_get_member_size(const Lzip_trailer data)
941 + unsigned long long tmp = 0;
942 + int i;
943 + for (i = 19; i >= 12; --i) {
944 + tmp <<= 8;
945 + tmp += data[i];
947 + return tmp;
950 +static inline void Lt_set_member_size(Lzip_trailer data, unsigned long long sz)
952 + int i;
953 + for (i = 12; i <= 19; ++i) {
954 + data[i] = (uint8_t)sz;
955 + sz >>= 8;
958 diff -urdN busybox-1.29.1.old/archival/libarchive/open_transformer.c busybox-1.29.1.new/archival/libarchive/open_transformer.c
959 --- busybox-1.29.1.old/archival/libarchive/open_transformer.c 2018-07-02 13:23:06.000000000 +0200
960 +++ busybox-1.29.1.new/archival/libarchive/open_transformer.c 2018-07-17 00:03:25.000000000 +0200
961 @@ -194,6 +194,17 @@
962 USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
963 goto found_magic;
965 + if (ENABLE_FEATURE_SEAMLESS_LZ
966 + && magic.b16[0] == LZIP_MAGIC1
967 + ) {
968 + xstate->signature_skipped = 4;
969 + xread(fd, magic.b16, sizeof(magic.b16[0]));
970 + if (magic.b16[0] == LZIP_MAGIC2) {
971 + xstate->xformer = unpack_lz_stream;
972 + USE_FOR_NOMMU(xstate->xformer_prog = "lunzip";)
973 + goto found_magic;
976 if (ENABLE_FEATURE_SEAMLESS_XZ
977 && magic.b16[0] == XZ_MAGIC1
979 @@ -210,6 +221,7 @@
980 if (fail_if_not_compressed)
981 bb_error_msg_and_die("no gzip"
982 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
983 + IF_FEATURE_SEAMLESS_LZ("/lzip")
984 IF_FEATURE_SEAMLESS_XZ("/xz")
985 " magic");
987 diff -urdN busybox-1.29.1.old/archival/lzip.c busybox-1.29.1.new/archival/lzip.c
988 --- busybox-1.29.1.old/archival/lzip.c 1970-01-01 01:00:00.000000000 +0100
989 +++ busybox-1.29.1.new/archival/lzip.c 2018-07-20 16:23:47.000000000 +0200
990 @@ -0,0 +1,1433 @@
992 + * lzip implementation for busybox
994 + * Copyright (C) 2012-2018 Antonio Diaz Diaz.
996 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
997 + */
999 +//config:config LZIP
1000 +//config: bool "lzip"
1001 +//config: default y
1002 +//config: help
1003 +//config: lzip is a lossless data compressor with a user interface similar to
1004 +//config: the one of gzip or bzip2. lzip can compress about as fast as gzip or
1005 +//config: compress most files more than bzip2 (depending on compression level).
1006 +//config: Decompression speed is intermediate between gzip and bzip2. lzip is
1007 +//config: better than gzip and bzip2 from a data recovery perspective.
1008 +//config:
1009 +//config:config FEATURE_LZIP_DECOMPRESS
1010 +//config: bool "Enable decompression"
1011 +//config: default y
1012 +//config: depends on LZIP || LUNZIP
1013 +//config: help
1014 +//config: Enable -d (--decompress) and -t (--test) options for lzip.
1015 +//config: This will be automatically selected if lunzip is enabled.
1017 +//applet:IF_LZIP(APPLET(lzip, BB_DIR_USR_BIN, BB_SUID_DROP))
1019 +//kbuild:lib-$(CONFIG_LZIP) += lzip.o
1021 +//usage:#define lzip_trivial_usage
1022 +//usage: "[-123456789c"
1023 +//usage: IF_FEATURE_LZIP_DECOMPRESS("d") "fk"
1024 +//usage: IF_FEATURE_LZIP_DECOMPRESS("t")
1025 +//usage: "] [-m MATCH_LENGTH] [-s DICT_SIZE] [FILE]..."
1026 +//usage:#define lzip_full_usage "\n\n"
1027 +//usage: "Compress FILEs (or stdin) with lzip algorithm\n"
1028 +//usage: "\n -1..9 Compression level"
1029 +//usage: "\n -c Write to stdout"
1030 +//usage: IF_FEATURE_LZIP_DECOMPRESS("\n -d Decompress")
1031 +//usage: "\n -f Force"
1032 +//usage: "\n -k Keep input files"
1033 +//usage: "\n -m Match length limit [36]"
1034 +//usage: "\n -s Dictionary size limit [8MiB]"
1035 +//usage: IF_FEATURE_LZIP_DECOMPRESS("\n -t Test compressed file integrity")
1038 +#include "libbb.h"
1039 +#include "bb_archive.h"
1040 +#include "libarchive/lzip.h"
1043 +#if CHAR_BIT != 8
1044 +#error "Environments where CHAR_BIT != 8 are not supported."
1045 +#endif
1048 +static void CRC32_update_byte(uint32_t * crc, const uint8_t byte)
1050 + *crc = global_crc32_table[(*crc ^ byte) & 0xFF] ^ (*crc >> 8);
1054 +enum { max_num_trials = 1 << 12,
1055 + price_shift_bits = 6,
1056 + price_step_bits = 2,
1057 + price_step = 1 << price_step_bits
1061 +static uint8_t * dis_slots;
1063 +static void Dis_slots_init(void)
1065 + int i, size, slot;
1066 + dis_slots = xmalloc((1 << 10) * sizeof dis_slots[0]);
1068 + for (slot = 0; slot < 4; ++slot) dis_slots[slot] = slot;
1069 + for (i = 4, size = 2, slot = 4; slot < 20; slot += 2) {
1070 + memset(&dis_slots[i], slot, size);
1071 + memset(&dis_slots[i + size], slot + 1, size);
1072 + size <<= 1;
1073 + i += size;
1077 +static uint8_t get_slot(const unsigned dis)
1079 + if (dis < (1 << 10)) return dis_slots[dis];
1080 + if (dis < (1 << 19)) return dis_slots[dis>> 9] + 18;
1081 + if (dis < (1 << 28)) return dis_slots[dis>>18] + 36;
1082 + return dis_slots[dis>>27] + 54;
1086 +static int * prob_prices;
1088 +static void Prob_prices_init(void)
1090 + int i, j;
1091 + prob_prices =
1092 + xmalloc((bit_model_total >> price_step_bits) * sizeof prob_prices[0]);
1093 + for( i = 0; i < bit_model_total >> price_step_bits; ++i ) {
1094 + unsigned val = ( i * price_step ) + ( price_step / 2 );
1095 + int bits = 0; /* base 2 logarithm of val */
1096 + for( j = 0; j < price_shift_bits; ++j ) {
1097 + val = val * val;
1098 + bits <<= 1;
1099 + while( val >= 1 << 16 ) { val >>= 1; ++bits; }
1101 + bits += 15; /* remaining bits in val */
1102 + prob_prices[i] = ( bit_model_total_bits << price_shift_bits ) - bits;
1106 +static inline int get_price(const int probability)
1108 + return prob_prices[probability >> price_step_bits];
1112 +static inline int price0(const Bit_model probability)
1114 + return get_price(probability);
1117 +static inline int price1(const Bit_model probability)
1119 + return get_price(bit_model_total - probability);
1122 +static int price_bit(const Bit_model bm, const bool bit)
1124 + return (bit ? price1(bm) : price0(bm));
1128 +static int price_symbol(const Bit_model bm[], int symbol, const int num_bits)
1130 + int price = 0;
1131 + symbol |= (1 << num_bits);
1132 + while (symbol > 1) {
1133 + const bool bit = symbol & 1;
1134 + symbol >>= 1;
1135 + price += price_bit(bm[symbol], bit);
1137 + return price;
1141 +static int price_symbol_reversed(const Bit_model bm[], int symbol,
1142 + const int num_bits)
1144 + int price = 0;
1145 + int model = 1;
1146 + int i;
1147 + for (i = num_bits; i > 0; --i) {
1148 + const bool bit = symbol & 1;
1149 + symbol >>= 1;
1150 + price += price_bit(bm[model], bit);
1151 + model = (model << 1) | bit;
1153 + return price;
1157 +static int price_matched(const Bit_model bm[], unsigned symbol,
1158 + unsigned match_byte)
1160 + int price = 0;
1161 + unsigned mask = 0x100;
1162 + symbol |= mask;
1164 + while (true) {
1165 + const unsigned match_bit = (match_byte <<= 1) & mask;
1166 + const bool bit = (symbol <<= 1) & 0x100;
1167 + price += price_bit( bm[(symbol>>9)+match_bit+mask], bit );
1168 + if (symbol >= 0x10000) return price;
1169 + mask &= ~(match_bit ^ symbol); /* if( match_bit != bit ) mask = 0; */
1174 +enum { /* bytes to keep in buffer before dictionary */
1175 + before_size = max_num_trials,
1176 + /* bytes to keep in buffer after pos */
1177 + after_size = ( 2 * max_match_len ) + 1,
1178 + num_prev_positions3 = 1 << 16,
1179 + num_prev_positions2 = 1 << 10,
1180 + num_prev_positions23 = num_prev_positions2 + num_prev_positions3
1183 +struct Matchfinder {
1184 + unsigned long long partial_data_pos;
1185 + uint8_t *buffer; /* input buffer */
1186 + int32_t *prev_positions; /* 1 + last seen position of key. else 0 */
1187 + int32_t *pos_array; /* tree of previous positions of key */
1188 + int match_len_limit;
1189 + int buffer_size;
1190 + int dictionary_size; /* bytes to keep in buffer before pos */
1191 + int pos; /* current pos in buffer */
1192 + int cyclic_pos; /* current pos in dictionary */
1193 + int stream_pos; /* first byte not yet read from file */
1194 + int pos_limit; /* when reached, a new block must be read */
1195 + int key4_mask;
1196 + int num_prev_positions; /* size of prev_positions */
1197 + int pos_array_size;
1198 + int cycles;
1199 + bool at_stream_end; /* stream_pos shows real end of file */
1202 +static bool Mf_read_block(struct Matchfinder * const mf)
1204 + if (!mf->at_stream_end && mf->stream_pos < mf->buffer_size) {
1205 + const int size = mf->buffer_size - mf->stream_pos;
1206 + const int rd = full_read(STDIN_FILENO,
1207 + mf->buffer + mf->stream_pos, size);
1208 + mf->stream_pos += rd;
1209 + if (rd < size) {
1210 + mf->at_stream_end = true;
1211 + mf->pos_limit = mf->buffer_size;
1214 + return mf->pos < mf->stream_pos;
1217 +static void Mf_normalize_pos(struct Matchfinder * const mf)
1219 + int i;
1220 + const int offset = mf->pos - before_size - mf->dictionary_size;
1221 + const int size = mf->stream_pos - offset;
1222 + if (mf->at_stream_end) return;
1223 + memmove(mf->buffer, mf->buffer + offset, size);
1224 + mf->partial_data_pos += offset;
1225 + mf->pos -= offset; /* pos = before_size + dictionary_size */
1226 + mf->stream_pos -= offset;
1227 + for (i = 0; i < mf->num_prev_positions; ++i)
1228 + mf->prev_positions[i] -= MIN(mf->prev_positions[i], offset);
1229 + for (i = 0; i < mf->pos_array_size; ++i)
1230 + mf->pos_array[i] -= MIN(mf->pos_array[i], offset);
1231 + Mf_read_block(mf);
1234 +static bool Mf_init(struct Matchfinder * const mf, const int dict_size,
1235 + const int match_len_limit)
1237 + const int buffer_size_limit = (2 * dict_size) + before_size + after_size;
1238 + unsigned size;
1239 + int i;
1241 + mf->partial_data_pos = 0;
1242 + mf->match_len_limit = match_len_limit;
1243 + mf->pos = 0;
1244 + mf->cyclic_pos = 0;
1245 + mf->stream_pos = 0;
1246 + mf->cycles = (match_len_limit < max_match_len) ?
1247 + 16 + (match_len_limit / 2) : 256;
1248 + mf->at_stream_end = false;
1250 + mf->buffer_size = MAX(65536, dict_size);
1251 + mf->buffer = (uint8_t *) malloc(mf->buffer_size);
1252 + if (!mf->buffer) return false;
1253 + if (Mf_read_block(mf) && !mf->at_stream_end &&
1254 + mf->buffer_size < buffer_size_limit) {
1255 + uint8_t *tmp;
1256 + mf->buffer_size = buffer_size_limit;
1257 + tmp = (uint8_t *) realloc(mf->buffer, mf->buffer_size);
1258 + if (!tmp) { free(mf->buffer); return false; }
1259 + mf->buffer = tmp;
1260 + Mf_read_block(mf);
1262 + if (mf->at_stream_end && mf->stream_pos < dict_size)
1263 + mf->dictionary_size = MAX(min_dictionary_size, mf->stream_pos);
1264 + else
1265 + mf->dictionary_size = dict_size;
1266 + mf->pos_limit = mf->buffer_size;
1267 + if (!mf->at_stream_end) mf->pos_limit -= after_size;
1268 + size = 1 << MAX(16, real_bits(mf->dictionary_size - 1) - 2);
1269 + if(mf->dictionary_size > 1 << 26) /* 64 MiB */
1270 + size >>= 1;
1271 + mf->key4_mask = size - 1;
1272 + size += num_prev_positions23;
1273 + mf->num_prev_positions = size;
1275 + mf->pos_array_size = 2 * (mf->dictionary_size + 1);
1276 + size += mf->pos_array_size;
1277 + if (size * sizeof mf->prev_positions[0] <= size) mf->prev_positions = 0;
1278 + else mf->prev_positions =
1279 + (int32_t *) malloc(size * sizeof mf->prev_positions[0]);
1280 + if (!mf->prev_positions) { free(mf->buffer); return false; }
1281 + mf->pos_array = mf->prev_positions + mf->num_prev_positions;
1282 + for (i = 0; i < mf->num_prev_positions; ++i) mf->prev_positions[i] = 0;
1283 + return true;
1286 +static void Mf_free(struct Matchfinder * const mf)
1288 + free(mf->prev_positions);
1289 + free(mf->buffer);
1292 +static inline uint8_t Mf_peek(const struct Matchfinder * const mf,
1293 + const int distance)
1295 + return mf->buffer[mf->pos-distance];
1298 +static inline int Mf_available_bytes(const struct Matchfinder * const mf)
1300 + return mf->stream_pos - mf->pos;
1303 +static inline unsigned long long
1304 +Mf_data_position(const struct Matchfinder * const mf)
1306 + return mf->partial_data_pos + mf->pos;
1309 +static inline bool Mf_finished(const struct Matchfinder * const mf)
1311 + return mf->at_stream_end && mf->pos >= mf->stream_pos;
1314 +static inline const uint8_t *
1315 +Mf_ptr_to_current_pos(const struct Matchfinder * const mf)
1317 + return mf->buffer + mf->pos;
1320 +static int Mf_true_match_len(const struct Matchfinder * const mf,
1321 + const int index, const int distance)
1323 + const uint8_t * const data = mf->buffer + mf->pos;
1324 + int i = index;
1325 + const int len_limit = MIN(Mf_available_bytes(mf), max_match_len);
1326 + while (i < len_limit && data[i - distance] == data[i]) ++i;
1327 + return i;
1330 +static void Mf_move_pos(struct Matchfinder * const mf)
1332 + if (++mf->cyclic_pos > mf->dictionary_size) mf->cyclic_pos = 0;
1333 + if (++mf->pos >= mf->pos_limit) Mf_normalize_pos(mf);
1336 +static int Mf_longest_match_len(struct Matchfinder * const mf,
1337 + int * const distances)
1339 + int32_t *ptr0 = mf->pos_array + (mf->cyclic_pos << 1);
1340 + int32_t *ptr1 = ptr0 + 1;
1341 + int32_t *newptr;
1342 + int len = 0, len0 = 0, len1 = 0;
1343 + int maxlen = min_match_len - 1;
1344 + const int pos1 = mf->pos + 1;
1345 + const int min_pos = (mf->pos > mf->dictionary_size) ?
1346 + mf->pos - mf->dictionary_size : 0;
1347 + const uint8_t * const data = Mf_ptr_to_current_pos(mf);
1348 + int count, key2, key3, key4, newpos1;
1349 + unsigned tmp;
1350 + int len_limit = mf->match_len_limit;
1352 + if (len_limit > Mf_available_bytes(mf)) {
1353 + len_limit = Mf_available_bytes(mf);
1354 + if (len_limit < 4) return 0;
1357 + tmp = global_crc32_table[data[0]] ^ data[1];
1358 + key2 = tmp & (num_prev_positions2 - 1);
1359 + tmp ^= ((unsigned) data[2] << 8);
1360 + key3 = num_prev_positions2 + (tmp & (num_prev_positions3 - 1));
1361 + key4 = num_prev_positions2 + num_prev_positions3 +
1362 + ((tmp ^ (global_crc32_table[data[3]] << 5)) & mf->key4_mask);
1364 + if (distances) {
1365 + int np2 = mf->prev_positions[key2];
1366 + int np3 = mf->prev_positions[key3];
1367 + if (np2 > min_pos && mf->buffer[np2-1] == data[0]) {
1368 + distances[2] = mf->pos - np2;
1369 + maxlen = 2;
1370 + } else
1371 + distances[2] = 0x7FFFFFFF;
1372 + if (np2 != np3 && np3 > min_pos && mf->buffer[np3-1] == data[0]) {
1373 + distances[3] = mf->pos - np3;
1374 + maxlen = 3;
1375 + } else
1376 + distances[3] = 0x7FFFFFFF;
1377 + distances[4] = 0x7FFFFFFF;
1380 + mf->prev_positions[key2] = pos1;
1381 + mf->prev_positions[key3] = pos1;
1382 + newpos1 = mf->prev_positions[key4];
1383 + mf->prev_positions[key4] = pos1;
1385 + for (count = mf->cycles;;) {
1386 + int delta;
1387 + if (newpos1 <= min_pos || --count < 0) {
1388 + *ptr0 = *ptr1 = 0;
1389 + break;
1391 + delta = pos1 - newpos1;
1392 + newptr = mf->pos_array +
1393 + ((mf->cyclic_pos - delta +
1394 + ((mf->cyclic_pos >= delta) ? 0 : mf->dictionary_size + 1)) << 1);
1396 + if (data[len-delta] == data[len]) {
1397 + while (++len < len_limit && data[len-delta] == data[len]) {}
1398 + if (distances && maxlen < len)
1399 + while (maxlen < len)
1400 + distances[++maxlen] = delta - 1;
1402 + if (len >= len_limit) {
1403 + *ptr0 = newptr[0];
1404 + *ptr1 = newptr[1];
1405 + break;
1408 + if (data[len-delta] < data[len]) {
1409 + *ptr0 = newpos1;
1410 + ptr0 = newptr + 1;
1411 + newpos1 = *ptr0;
1412 + len0 = len;
1413 + if (len1 < len) len = len1;
1414 + } else {
1415 + *ptr1 = newpos1;
1416 + ptr1 = newptr;
1417 + newpos1 = *ptr1;
1418 + len1 = len;
1419 + if (len0 < len) len = len0;
1422 + if (distances) {
1423 + if (distances[3] > distances[4])
1424 + distances[3] = distances[4];
1425 + if (distances[2] > distances[3])
1426 + distances[2] = distances[3];
1428 + return maxlen;
1432 +enum { re_buffer_size = 65536 };
1434 +struct Range_encoder {
1435 + uint64_t low;
1436 + unsigned long long partial_member_pos;
1437 + uint8_t *buffer; /* output buffer */
1438 + int pos; /* current pos in buffer */
1439 + uint32_t range;
1440 + unsigned ff_count;
1441 + uint8_t cache;
1442 + bool write_error;
1445 +static void Re_flush_data(struct Range_encoder * const renc)
1447 + if (renc->pos > 0) {
1448 + if (full_write(STDOUT_FILENO, renc->buffer, renc->pos) != renc->pos)
1449 + renc->write_error = true;
1450 + renc->partial_member_pos += renc->pos;
1451 + renc->pos = 0;
1455 +static void Re_put_byte(struct Range_encoder * const renc, const uint8_t b)
1457 + renc->buffer[renc->pos] = b;
1458 + if (++renc->pos >= re_buffer_size) Re_flush_data(renc);
1461 +static void Re_shift_low(struct Range_encoder * const renc)
1463 + if(renc->low >> 24 != 0xFF) {
1464 + const bool carry = (renc->low > 0xFFFFFFFFU);
1465 + Re_put_byte(renc, renc->cache + carry);
1466 + for (; renc->ff_count > 0; --renc->ff_count)
1467 + Re_put_byte(renc, 0xFF + carry);
1468 + renc->cache = renc->low >> 24;
1469 + } else
1470 + ++renc->ff_count;
1471 + renc->low = (renc->low & 0x00FFFFFFU) << 8;
1474 +static bool Re_init(struct Range_encoder * const renc)
1476 + renc->low = 0;
1477 + renc->partial_member_pos = 0;
1478 + renc->buffer = (uint8_t *) malloc(re_buffer_size);
1479 + if (!renc->buffer) return false;
1480 + renc->pos = 0;
1481 + renc->range = 0xFFFFFFFFU;
1482 + renc->ff_count = 0;
1483 + renc->cache = 0;
1484 + renc->write_error = false;
1485 + return true;
1488 +static inline void Re_free(struct Range_encoder * const renc)
1490 + free(renc->buffer);
1493 +static inline unsigned long long
1494 +Re_member_position(const struct Range_encoder * const renc)
1496 + return renc->partial_member_pos + renc->pos + renc->ff_count;
1499 +static void Re_flush(struct Range_encoder * const renc)
1501 + int i;
1502 + for (i = 0; i < 5; ++i) Re_shift_low(renc);
1505 +static void Re_encode(struct Range_encoder * const renc,
1506 + const int symbol, const int num_bits)
1508 + unsigned mask;
1509 + for (mask = 1 << ( num_bits - 1 ); mask > 0; mask >>= 1) {
1510 + renc->range >>= 1;
1511 + if (symbol &mask) renc->low += renc->range;
1512 + if (renc->range <= 0x00FFFFFFU) {
1513 + renc->range <<= 8;
1514 + Re_shift_low(renc);
1519 +static void Re_encode_bit(struct Range_encoder * const renc,
1520 + Bit_model * const probability, const bool bit)
1522 + const uint32_t bound = (renc->range >> bit_model_total_bits) * *probability;
1523 + if (!bit) {
1524 + renc->range = bound;
1525 + *probability += (bit_model_total - *probability) >> bit_model_move_bits;
1526 + } else {
1527 + renc->low += bound;
1528 + renc->range -= bound;
1529 + *probability -= *probability >> bit_model_move_bits;
1531 + if (renc->range <= 0x00FFFFFFU) {
1532 + renc->range <<= 8;
1533 + Re_shift_low(renc);
1537 +static void Re_encode_tree(struct Range_encoder * const renc,
1538 + Bit_model bm[], const int symbol,
1539 + const int num_bits)
1541 + int model = 1;
1542 + int i;
1543 + for (i = num_bits - 1; i >= 0; --i) {
1544 + const bool bit = (symbol >> i) & 1;
1545 + Re_encode_bit(renc, &bm[model], bit);
1546 + model = (model << 1) | bit;
1550 +static void Re_encode_tree_reversed(struct Range_encoder * const renc,
1551 + Bit_model bm[], int symbol, const int num_bits)
1553 + int model = 1;
1554 + int i;
1555 + for (i = num_bits; i > 0; --i) {
1556 + const bool bit = symbol & 1;
1557 + symbol >>= 1;
1558 + Re_encode_bit(renc, &bm[model], bit);
1559 + model = (model << 1) | bit;
1563 +static void Re_encode_matched(struct Range_encoder * const renc,
1564 + Bit_model bm[], unsigned symbol,
1565 + unsigned match_byte)
1567 + unsigned mask = 0x100;
1568 + symbol |= mask;
1570 + while (true) {
1571 + const unsigned match_bit = (match_byte <<= 1) & mask;
1572 + const bool bit = (symbol <<= 1) & 0x100;
1573 + Re_encode_bit( renc, &bm[(symbol>>9)+match_bit+mask], bit );
1574 + if (symbol >= 0x10000) break;
1575 + mask &= ~(match_bit ^ symbol); /* if( match_bit != bit ) mask = 0; */
1579 +static void Re_encode_len( struct Range_encoder * const renc,
1580 + struct Len_model * const lm,
1581 + int symbol, const int pos_state )
1583 + bool bit = ( ( symbol -= min_match_len ) >= len_low_symbols );
1584 + Re_encode_bit( renc, &lm->choice1, bit );
1585 + if( !bit )
1586 + Re_encode_tree( renc, lm->bm_low[pos_state], symbol, len_low_bits );
1587 + else {
1588 + bit = ((symbol -= len_low_symbols) >= len_mid_symbols );
1589 + Re_encode_bit( renc, &lm->choice2, bit );
1590 + if( !bit )
1591 + Re_encode_tree( renc, lm->bm_mid[pos_state],
1592 + symbol, len_mid_bits );
1593 + else
1594 + Re_encode_tree( renc, lm->bm_high,
1595 + symbol - len_mid_symbols, len_high_bits );
1600 +struct Len_encoder {
1601 + struct Len_model lm;
1602 + int len_symbols;
1603 + int prices[pos_states][max_len_symbols];
1604 + int counters[pos_states];
1607 +static void Lee_update_prices(struct Len_encoder * const le, const int pos_state)
1609 + int * const pps = le->prices[pos_state];
1610 + int tmp = price0(le->lm.choice1);
1611 + int len = 0;
1613 + for (; len < len_low_symbols && len < le->len_symbols; ++len)
1614 + pps[len] = tmp +
1615 + price_symbol(le->lm.bm_low[pos_state], len, len_low_bits);
1616 + tmp = price1(le->lm.choice1);
1617 + for (; len < len_low_symbols + len_mid_symbols && len < le->len_symbols; ++len)
1618 + pps[len] = tmp + price0(le->lm.choice2) +
1619 + price_symbol(le->lm.bm_mid[pos_state],
1620 + len - len_low_symbols, len_mid_bits);
1621 + for (; len < le->len_symbols; ++len)
1622 + /* using 4 slots per value makes "Lee_price" faster */
1623 + le->prices[3][len] = le->prices[2][len] =
1624 + le->prices[1][len] = le->prices[0][len] =
1625 + tmp + price1(le->lm.choice2) +
1626 + price_symbol(le->lm.bm_high,
1627 + len - len_low_symbols - len_mid_symbols,
1628 + len_high_bits);
1629 + le->counters[pos_state] = le->len_symbols;
1632 +static void Lee_init(struct Len_encoder * const le, const int len_limit)
1634 + int i;
1635 + Lm_init(&le->lm);
1636 + le->len_symbols = len_limit + 1 - min_match_len;
1637 + for (i = 0; i < pos_states; ++i) Lee_update_prices(le, i);
1640 +static void Lee_encode(struct Len_encoder * const le,
1641 + struct Range_encoder * const renc,
1642 + int symbol, const int pos_state)
1644 + Re_encode_len(renc, &le->lm, symbol, pos_state);
1645 + if (--le->counters[pos_state] <= 0)
1646 + Lee_update_prices(le, pos_state);
1649 +static int Lee_price(const struct Len_encoder * const le,
1650 + const int symbol, const int pos_state)
1652 + return le->prices[pos_state][symbol - min_match_len];
1656 +enum { infinite_price = 0x0FFFFFFF,
1657 + num_rep_distances = 4 /* must be 4 */
1660 +struct Trial {
1661 + State state;
1662 + int price; /* dual use var; cumulative price, match length */
1663 + int dis; /* rep index or match distance. (-1 for literal) */
1664 + int prev_index; /* index of prev trial in trials[] */
1665 + int reps[num_rep_distances];
1668 +static void Tr_update(struct Trial * const trial, const int pr,
1669 + const int distance, const int p_i)
1671 + if (pr < trial->price) {
1672 + trial->price = pr;
1673 + trial->dis = distance;
1674 + trial->prev_index = p_i;
1679 +struct LZ_encoder {
1680 + int longest_match_found;
1681 + uint32_t crc;
1683 + Bit_model bm_literal[1 << literal_context_bits][0x300];
1684 + Bit_model bm_match[states][pos_states];
1685 + Bit_model bm_rep[states];
1686 + Bit_model bm_rep0[states];
1687 + Bit_model bm_rep1[states];
1688 + Bit_model bm_rep2[states];
1689 + Bit_model bm_len[states][pos_states];
1690 + Bit_model bm_dis_slot[len_states][1 << dis_slot_bits];
1691 + Bit_model bm_dis[modeled_distances-end_dis_model+1];
1692 + Bit_model bm_align[dis_align_size];
1694 + struct Matchfinder *matchfinder;
1695 + struct Range_encoder renc;
1696 + struct Len_encoder match_len_encoder;
1697 + struct Len_encoder rep_len_encoder;
1699 + int match_distances[max_match_len+1];
1700 + struct Trial trials[max_num_trials];
1702 + int dis_slot_prices[len_states][2*max_dictionary_bits];
1703 + int dis_prices[len_states][modeled_distances];
1704 + int align_prices[dis_align_size];
1705 + int align_price_count;
1706 + int num_dis_slots;
1709 +static void LZe_fill_align_prices(struct LZ_encoder * const e)
1711 + int i;
1712 + for (i = 0; i < dis_align_size; ++i)
1713 + e->align_prices[i] =
1714 + price_symbol_reversed(e->bm_align, i, dis_align_bits);
1715 + e->align_price_count = dis_align_size;
1718 +static bool LZe_init(struct LZ_encoder * const e,
1719 + struct Matchfinder * const mf, const Lzip_header header)
1721 + int i;
1722 + e->longest_match_found = 0;
1723 + e->crc = 0xFFFFFFFFU;
1724 + Bm_array_init(&e->bm_literal[0][0], lz_num_models);
1725 + e->matchfinder = mf;
1726 + if (!Re_init(&e->renc)) return false;
1727 + Lee_init(&e->match_len_encoder, e->matchfinder->match_len_limit);
1728 + Lee_init(&e->rep_len_encoder, e->matchfinder->match_len_limit);
1729 + LZe_fill_align_prices(e);
1730 + e->num_dis_slots = 2 * real_bits(e->matchfinder->dictionary_size - 1);
1731 + e->trials[1].prev_index = 0;
1732 + for (i = 0; i < Lh_size; ++i)
1733 + Re_put_byte(&e->renc, header[i]);
1734 + return true;
1737 +static inline void LZe_free(struct LZ_encoder * const e)
1739 + Re_free(&e->renc);
1742 +static inline unsigned LZe_crc(const struct LZ_encoder * const e)
1744 + return e->crc ^ 0xFFFFFFFFU;
1747 + /* move-to-front dis in/into reps; do nothing if( dis4 <= 0 ) */
1748 +static void mtf_reps(const int dis4, int reps[num_rep_distances])
1750 + if (dis4 >= num_rep_distances) { /* match */
1751 + reps[3] = reps[2]; reps[2] = reps[1]; reps[1] = reps[0];
1752 + reps[0] = dis4 - num_rep_distances;
1753 + } else if (dis4 > 0) { /* repeated match */
1754 + const int distance = reps[dis4];
1755 + int i; for (i = dis4; i > 0; --i)
1756 + reps[i] = reps[i - 1];
1757 + reps[0] = distance;
1761 +static int LZe_price_shortrep(const struct LZ_encoder * const e,
1762 + const State state, const int pos_state)
1764 + return price0(e->bm_rep0[state]) + price0(e->bm_len[state][pos_state]);
1767 +static int LZe_price_rep(const struct LZ_encoder * const e, const int rep,
1768 + const State state, const int pos_state)
1770 + int price;
1771 + if (rep == 0)
1772 + return price0(e->bm_rep0[state]) +
1773 + price1(e->bm_len[state][pos_state]);
1774 + price = price1(e->bm_rep0[state]);
1775 + if (rep == 1)
1776 + price += price0(e->bm_rep1[state]);
1777 + else {
1778 + price += price1(e->bm_rep1[state]);
1779 + price += price_bit(e->bm_rep2[state], rep - 2);
1781 + return price;
1784 +static int LZe_price_dis(const struct LZ_encoder * const e,
1785 + const int dis, const int len_state)
1787 + if (dis < modeled_distances)
1788 + return e->dis_prices[len_state][dis];
1789 + else
1790 + return e->dis_slot_prices[len_state][get_slot(dis)] +
1791 + e->align_prices[dis & (dis_align_size - 1)];
1794 +static int LZe_price_pair(const struct LZ_encoder * const e,
1795 + const int dis, const int len,
1796 + const int pos_state)
1798 + if (len <= min_match_len && dis >= modeled_distances)
1799 + return infinite_price;
1800 + return Lee_price(&e->match_len_encoder, len, pos_state) +
1801 + LZe_price_dis(e, dis, get_len_state(len));
1804 +static int LZe_price_literal(const struct LZ_encoder * const e,
1805 + uint8_t prev_byte, uint8_t symbol)
1807 + return price_symbol(e->bm_literal[get_lit_state(prev_byte)], symbol, 8);
1810 +static int LZe_price_matched(const struct LZ_encoder * const e,
1811 + uint8_t prev_byte, uint8_t symbol,
1812 + uint8_t match_byte)
1814 + return price_matched(e->bm_literal[get_lit_state(prev_byte)], symbol,
1815 + match_byte);
1818 +static void LZe_encode_literal(struct LZ_encoder * const e,
1819 + uint8_t prev_byte, uint8_t symbol)
1821 + Re_encode_tree(&e->renc,
1822 + e->bm_literal[get_lit_state(prev_byte)], symbol, 8);
1825 +static void LZe_encode_matched(struct LZ_encoder * const e,
1826 + uint8_t prev_byte, uint8_t symbol,
1827 + uint8_t match_byte)
1829 + Re_encode_matched(&e->renc, e->bm_literal[get_lit_state(prev_byte)],
1830 + symbol, match_byte);
1833 +static void LZe_encode_pair(struct LZ_encoder * const e,
1834 + const unsigned dis, const int len,
1835 + const int pos_state)
1837 + const int dis_slot = get_slot(dis);
1838 + Lee_encode(&e->match_len_encoder, &e->renc, len, pos_state);
1839 + Re_encode_tree(&e->renc, e->bm_dis_slot[get_len_state(len)], dis_slot,
1840 + dis_slot_bits);
1842 + if (dis_slot >= start_dis_model) {
1843 + const int direct_bits = (dis_slot >> 1) - 1;
1844 + const unsigned base = (2 | (dis_slot & 1)) << direct_bits;
1845 + const unsigned direct_dis = dis - base;
1847 + if (dis_slot < end_dis_model)
1848 + Re_encode_tree_reversed(&e->renc,
1849 + e->bm_dis + (base - dis_slot),
1850 + direct_dis, direct_bits);
1851 + else {
1852 + Re_encode(&e->renc, direct_dis >> dis_align_bits,
1853 + direct_bits - dis_align_bits);
1854 + Re_encode_tree_reversed(&e->renc, e->bm_align,
1855 + direct_dis, dis_align_bits);
1856 + if (--e->align_price_count <= 0)
1857 + LZe_fill_align_prices(e);
1862 +static int LZe_read_match_distances(struct LZ_encoder * const e)
1864 + int len = Mf_longest_match_len(e->matchfinder, e->match_distances);
1865 + if (len == e->matchfinder->match_len_limit && len < max_match_len)
1866 + len = Mf_true_match_len(e->matchfinder, len,
1867 + e->match_distances[len] + 1);
1868 + return len;
1871 +static void LZe_move_pos(struct LZ_encoder * const e, int n)
1873 + while (true) {
1874 + Mf_move_pos(e->matchfinder);
1875 + if( --n <= 0 ) break;
1876 + Mf_longest_match_len(e->matchfinder, 0);
1880 +static void LZe_backward(struct LZ_encoder * const e, int cur)
1882 + int * const dis = &e->trials[cur].dis;
1883 + while (cur > 0) {
1884 + const int prev_index = e->trials[cur].prev_index;
1885 + struct Trial * const prev_trial = &e->trials[prev_index];
1886 + prev_trial->price = cur - prev_index; /* len */
1887 + cur = *dis;
1888 + *dis = prev_trial->dis;
1889 + prev_trial->dis = cur;
1890 + cur = prev_index;
1894 + /* End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len) */
1895 +static void LZe_full_flush(struct LZ_encoder * const e, const State state)
1897 + int i;
1898 + const int pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
1899 + Lzip_trailer trailer;
1900 + Re_encode_bit(&e->renc, &e->bm_match[state][pos_state], 1);
1901 + Re_encode_bit(&e->renc, &e->bm_rep[state], 0);
1902 + LZe_encode_pair(e, 0xFFFFFFFFU, min_match_len, pos_state);
1903 + Re_flush(&e->renc);
1904 + Lt_set_data_crc(trailer, LZe_crc(e));
1905 + Lt_set_data_size(trailer, Mf_data_position(e->matchfinder));
1906 + Lt_set_member_size(trailer, Re_member_position(&e->renc) + Lt_size);
1907 + for (i = 0; i < Lt_size; ++i)
1908 + Re_put_byte(&e->renc, trailer[i]);
1909 + Re_flush_data(&e->renc);
1913 +static void LZe_update_distance_prices(struct LZ_encoder * const e)
1915 + int dis, len_state;
1916 + for (dis = start_dis_model; dis < modeled_distances; ++dis) {
1917 + const int dis_slot = dis_slots[dis];
1918 + const int direct_bits = (dis_slot >> 1) - 1;
1919 + const int base = (2 | (dis_slot & 1)) << direct_bits;
1920 + const int price =
1921 + price_symbol_reversed(e->bm_dis + (base - dis_slot),
1922 + dis - base, direct_bits);
1923 + for (len_state = 0; len_state < len_states; ++len_state)
1924 + e->dis_prices[len_state][dis] = price;
1927 + for (len_state = 0; len_state < len_states; ++len_state) {
1928 + int * const dsp = e->dis_slot_prices[len_state];
1929 + int * const dp = e->dis_prices[len_state];
1930 + const Bit_model * const bmds = e->bm_dis_slot[len_state];
1931 + int slot = 0;
1932 + for (; slot < end_dis_model; ++slot)
1933 + dsp[slot] = price_symbol(bmds, slot, dis_slot_bits);
1934 + for (; slot < e->num_dis_slots; ++slot)
1935 + dsp[slot] = price_symbol(bmds, slot, dis_slot_bits) +
1936 + ((((slot >> 1) - 1) - dis_align_bits) << price_shift_bits);
1938 + for (dis = 0; dis < start_dis_model; ++dis)
1939 + dp[dis] = dsp[dis];
1940 + for (; dis < modeled_distances; ++dis)
1941 + dp[dis] += dsp[dis_slots[dis]];
1946 +/* Returns the number of bytes advanced (ahead).
1947 + trials[0]..trials[ahead-1] contain the steps to encode.
1948 + ( trials[0].dis == -1 && trials[0].price == 1 ) means literal.
1949 + A match/rep longer or equal than match_len_limit finishes the sequence.
1951 +static int LZe_sequence_optimizer(struct LZ_encoder * const e,
1952 + const int reps[num_rep_distances],
1953 + const State state)
1955 + int main_len, i, rep, cur = 0, num_trials;
1956 + int replens[num_rep_distances];
1957 + int rep_index = 0;
1959 + if (e->longest_match_found > 0) { /* from previous call */
1960 + main_len = e->longest_match_found;
1961 + e->longest_match_found = 0;
1962 + } else
1963 + main_len = LZe_read_match_distances(e);
1965 + for (i = 0; i < num_rep_distances; ++i) {
1966 + replens[i] = Mf_true_match_len(e->matchfinder, 0, reps[i] + 1);
1967 + if (replens[i] > replens[rep_index]) rep_index = i;
1969 + if (replens[rep_index] >= e->matchfinder->match_len_limit) {
1970 + e->trials[0].dis = rep_index;
1971 + e->trials[0].price = replens[rep_index];
1972 + LZe_move_pos(e, replens[rep_index]);
1973 + return replens[rep_index];
1976 + if (main_len >= e->matchfinder->match_len_limit) {
1977 + e->trials[0].dis =
1978 + e->match_distances[e->matchfinder->match_len_limit] +
1979 + num_rep_distances;
1980 + e->trials[0].price = main_len;
1981 + LZe_move_pos(e, main_len);
1982 + return main_len;
1986 + const int pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
1987 + const int match_price = price1(e->bm_match[state][pos_state]);
1988 + const int rep_match_price = match_price + price1(e->bm_rep[state]);
1989 + const uint8_t prev_byte = Mf_peek(e->matchfinder, 1);
1990 + const uint8_t cur_byte = Mf_peek(e->matchfinder, 0);
1991 + const uint8_t match_byte = Mf_peek(e->matchfinder, reps[0] + 1);
1993 + e->trials[1].price = price0(e->bm_match[state][pos_state]);
1994 + if (St_is_char(state))
1995 + e->trials[1].price +=
1996 + LZe_price_literal(e, prev_byte, cur_byte);
1997 + else
1998 + e->trials[1].price +=
1999 + LZe_price_matched(e, prev_byte, cur_byte, match_byte);
2000 + e->trials[1].dis = -1; /* literal */
2002 + if (match_byte == cur_byte)
2003 + Tr_update(&e->trials[1], rep_match_price +
2004 + LZe_price_shortrep(e, state, pos_state), 0, 0);
2006 + if (main_len < min_match_len) {
2007 + e->trials[0].dis = e->trials[1].dis;
2008 + e->trials[0].price = 1;
2009 + Mf_move_pos(e->matchfinder);
2010 + return 1;
2013 + e->trials[0].state = state;
2014 + for (i = 0; i < num_rep_distances; ++i)
2015 + e->trials[0].reps[i] = reps[i];
2017 + if (main_len <= replens[rep_index]) {
2018 + int len;
2020 + main_len = replens[rep_index];
2021 + for (len = min_match_len; len <= main_len; ++len)
2022 + e->trials[len].price = infinite_price;
2023 + } else {
2024 + int len;
2025 + const int normal_match_price =
2026 + match_price + price0(e->bm_rep[state]);
2027 + for (len = min_match_len; len <= main_len; ++len) {
2028 + e->trials[len].dis =
2029 + e->match_distances[len] + num_rep_distances;
2030 + e->trials[len].prev_index = 0;
2031 + e->trials[len].price = normal_match_price +
2032 + LZe_price_pair(e, e->match_distances[len],
2033 + len, pos_state);
2037 + for (rep = 0; rep < num_rep_distances; ++rep) {
2038 + const int price = rep_match_price +
2039 + LZe_price_rep(e, rep, state, pos_state);
2040 + int len;
2041 + for (len = min_match_len; len <= replens[rep]; ++len)
2042 + Tr_update(&e->trials[len], price +
2043 + Lee_price(&e->rep_len_encoder, len, pos_state),
2044 + rep, 0);
2048 + num_trials = main_len;
2050 + while (true) { /* price optimization loop */
2051 + struct Trial *cur_trial, *next_trial;
2052 + int newlen, pos_state, prev_index, len_limit;
2053 + int next_price, match_price, rep_match_price;
2054 + uint8_t prev_byte, cur_byte, match_byte;
2056 + Mf_move_pos(e->matchfinder);
2057 + if (++cur >= num_trials) { /* no more initialized trials */
2058 + LZe_backward(e, cur);
2059 + return cur;
2061 + newlen = LZe_read_match_distances(e);
2062 + if (newlen >= e->matchfinder->match_len_limit) {
2063 + e->longest_match_found = newlen;
2064 + LZe_backward(e, cur);
2065 + return cur;
2068 + /* give final values to current trial */
2069 + cur_trial = &e->trials[cur];
2070 + prev_index = cur_trial->prev_index;
2071 + cur_trial->state = e->trials[prev_index].state;
2073 + for (i = 0; i < num_rep_distances; ++i)
2074 + cur_trial->reps[i] = e->trials[prev_index].reps[i];
2076 + if (prev_index == cur - 1) {
2077 + if (cur_trial->dis == 0)
2078 + cur_trial->state = St_set_short_rep(cur_trial->state);
2079 + else
2080 + cur_trial->state = St_set_char(cur_trial->state);
2081 + } else {
2082 + if (cur_trial->dis < num_rep_distances)
2083 + cur_trial->state = St_set_rep(cur_trial->state);
2084 + else
2085 + cur_trial->state = St_set_match(cur_trial->state);
2086 + mtf_reps(cur_trial->dis, cur_trial->reps);
2089 + pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
2090 + prev_byte = Mf_peek(e->matchfinder, 1);
2091 + cur_byte = Mf_peek(e->matchfinder, 0);
2092 + match_byte = Mf_peek(e->matchfinder, cur_trial->reps[0] + 1);
2094 + next_price = cur_trial->price +
2095 + price0(e->bm_match[cur_trial->state][pos_state]);
2096 + if (St_is_char(cur_trial->state))
2097 + next_price += LZe_price_literal(e, prev_byte, cur_byte);
2098 + else
2099 + next_price += LZe_price_matched(e, prev_byte, cur_byte,
2100 + match_byte);
2101 + /* try last updates to next trial */
2102 + next_trial = &e->trials[cur + 1];
2104 + Tr_update(next_trial, next_price, -1, cur); /* literal */
2106 + match_price = cur_trial->price +
2107 + price1(e->bm_match[cur_trial->state][pos_state]);
2108 + rep_match_price = match_price + price1(e->bm_rep[cur_trial->state]);
2110 + if (match_byte == cur_byte && next_trial->dis != 0)
2111 + Tr_update(next_trial, rep_match_price +
2112 + LZe_price_shortrep(e, cur_trial->state,
2113 + pos_state), 0, cur);
2115 + len_limit = MIN(MIN(max_num_trials - 1 - cur,
2116 + Mf_available_bytes(e->matchfinder)),
2117 + e->matchfinder->match_len_limit);
2118 + if (len_limit < min_match_len) continue;
2120 + for (rep = 0; rep < num_rep_distances; ++rep) {
2121 + const int dis = cur_trial->reps[rep] + 1;
2122 + int len = 0;
2123 + const uint8_t * const data =
2124 + Mf_ptr_to_current_pos(e->matchfinder);
2125 + while (len < len_limit && data[len] == data[len - dis])
2126 + ++len;
2127 + if (len >= min_match_len) {
2128 + const int price = rep_match_price +
2129 + LZe_price_rep(e, rep, cur_trial->state, pos_state);
2130 + while (num_trials < cur + len)
2131 + e->trials[++num_trials].price = infinite_price;
2132 + for (; len >= min_match_len; --len)
2133 + Tr_update(&e->trials[cur + len], price +
2134 + Lee_price(&e->rep_len_encoder, len,
2135 + pos_state), rep, cur);
2139 + if (newlen <= len_limit &&
2140 + (newlen > min_match_len ||
2141 + (newlen == min_match_len &&
2142 + e->match_distances[min_match_len] < modeled_distances))) {
2143 + const int normal_match_price = match_price +
2144 + price0(e->bm_rep[cur_trial->state]);
2145 + int len;
2146 + int dis = e->match_distances[min_match_len];
2147 + int len_state = get_len_state(min_match_len);
2148 + int dis_price = infinite_price;
2150 + while (num_trials < cur + newlen)
2151 + e->trials[++num_trials].price = infinite_price;
2153 + if (dis < modeled_distances)
2154 + Tr_update(&e->trials[cur + min_match_len],
2155 + normal_match_price +
2156 + e->dis_prices[len_state][dis] +
2157 + Lee_price(&e->match_len_encoder,
2158 + min_match_len, pos_state),
2159 + dis + num_rep_distances, cur);
2161 + for (len = min_match_len + 1; len <= newlen; ++len) {
2162 + if (dis != e->match_distances[len] ||
2163 + len_state < len_states - 1) {
2164 + dis = e->match_distances[len];
2165 + len_state = get_len_state(len);
2166 + dis_price = LZe_price_dis(e, dis, len_state);
2168 + Tr_update(&e->trials[cur + len],
2169 + normal_match_price + dis_price +
2170 + Lee_price(&e->match_len_encoder, len, pos_state),
2171 + dis + num_rep_distances, cur);
2178 +static bool LZe_encode_member(struct LZ_encoder * const e)
2180 + const int dis_price_count =
2181 + (e->matchfinder->match_len_limit > 12) ? 512 : 2048;
2182 + int dis_price_counter = 0;
2183 + int ahead, i;
2184 + int reps[num_rep_distances];
2185 + State state = 0;
2186 + for (i = 0; i < num_rep_distances; ++i) reps[i] = 0;
2188 + if (!Mf_finished(e->matchfinder)) { /* encode first byte */
2189 + const uint8_t prev_byte = 0;
2190 + const uint8_t cur_byte = Mf_peek(e->matchfinder, 0);
2191 + Re_encode_bit(&e->renc, &e->bm_match[state][0], 0);
2192 + LZe_encode_literal(e, prev_byte, cur_byte);
2193 + CRC32_update_byte(&e->crc, cur_byte);
2194 + Mf_longest_match_len(e->matchfinder, 0);
2195 + Mf_move_pos(e->matchfinder);
2198 + while (!Mf_finished(e->matchfinder)) {
2199 + if (dis_price_counter <= 0) {
2200 + LZe_update_distance_prices(e);
2201 + dis_price_counter = dis_price_count;
2204 + ahead = LZe_sequence_optimizer(e, reps, state);
2205 + dis_price_counter -= ahead;
2207 + for (i = 0; ahead > 0;) {
2208 + const int pos_state =
2209 + (Mf_data_position(e->matchfinder) - ahead) & pos_state_mask;
2210 + const int dis = e->trials[i].dis;
2211 + const int len = e->trials[i].price;
2213 + bool bit = (dis < 0);
2214 + Re_encode_bit(&e->renc, &e->bm_match[state][pos_state], !bit);
2215 + if (bit) { /* literal byte */
2216 + const uint8_t prev_byte = Mf_peek(e->matchfinder, ahead + 1);
2217 + const uint8_t cur_byte = Mf_peek(e->matchfinder, ahead);
2218 + CRC32_update_byte(&e->crc, cur_byte);
2219 + if (St_is_char(state))
2220 + LZe_encode_literal(e, prev_byte, cur_byte);
2221 + else {
2222 + const uint8_t match_byte =
2223 + Mf_peek(e->matchfinder, ahead + reps[0] + 1);
2224 + LZe_encode_matched(e, prev_byte, cur_byte, match_byte);
2226 + state = St_set_char(state);
2227 + } else { /* match or repeated match */
2229 + e->crc = crc32_block_endian0(e->crc,
2230 + Mf_ptr_to_current_pos(e->matchfinder) - ahead,
2231 + len, global_crc32_table);
2232 + mtf_reps(dis, reps);
2233 + bit = (dis < num_rep_distances);
2234 + Re_encode_bit(&e->renc, &e->bm_rep[state], bit);
2235 + if (bit) { /* repeated match */
2236 + bit = (dis == 0);
2237 + Re_encode_bit(&e->renc, &e->bm_rep0[state], !bit);
2238 + if (bit)
2239 + Re_encode_bit(&e->renc, &e->bm_len[state][pos_state], len > 1);
2240 + else {
2241 + Re_encode_bit(&e->renc, &e->bm_rep1[state], dis > 1);
2242 + if (dis > 1)
2243 + Re_encode_bit(&e->renc, &e->bm_rep2[state], dis > 2);
2245 + if (len == 1)
2246 + state = St_set_short_rep(state);
2247 + else {
2248 + Lee_encode(&e->rep_len_encoder,
2249 + &e->renc, len, pos_state);
2250 + state = St_set_rep(state);
2252 + } else { /* match */
2253 + LZe_encode_pair(e, dis - num_rep_distances, len, pos_state);
2254 + state = St_set_match(state);
2257 + ahead -= len;
2258 + i += len;
2261 + LZe_full_flush(e, state);
2262 + return !e->renc.write_error;
2266 +struct Lzma_options {
2267 + int dictionary_size; /* 4KiB..512MiB */
2268 + int match_len_limit; /* 5..273 */
2269 +} encoder_options;
2272 +static int getnum(const char * const ptr, const int llimit, const int ulimit)
2274 + long result;
2275 + char *tail;
2276 + errno = 0;
2277 + result = strtol(ptr, &tail, 0);
2278 + if (tail == ptr || errno)
2279 + goto error;
2280 + if (tail[0]) {
2281 + int factor = (tail[1] == 'i') ? 1024 : 1000;
2282 + int exponent = 0, i;
2284 + switch (tail[0]) {
2285 + case 'M':
2286 + exponent = 2;
2287 + break;
2288 + case 'K':
2289 + if (factor == 1024) {
2290 + exponent = 1;
2291 + break;
2293 + goto error;
2294 + case 'k':
2295 + if (factor == 1000) {
2296 + exponent = 1;
2297 + break;
2299 + default:
2300 + goto error;
2302 + for (i = 0; i < exponent; ++i) {
2303 + if (LONG_MAX / factor >= labs(result))
2304 + result *= factor;
2305 + else
2306 + goto error;
2309 + if (result >= llimit && result <= ulimit)
2310 + return result;
2311 + error:
2312 + bb_error_msg_and_die("invalid number");
2316 +static int get_dict_size(const char * const arg)
2318 + char *tail;
2319 + long bits = strtol(arg, &tail, 0);
2320 + if (bits >= min_dictionary_bits &&
2321 + bits <= max_dictionary_bits && *tail == 0)
2322 + return (1 << bits);
2323 + return getnum(arg, min_dictionary_size, max_dictionary_size);
2327 +static IF_DESKTOP(long long) int FAST_FUNC pack_lzip(transformer_state_t *xstate UNUSED_PARAM)
2329 + int retval = 0;
2330 + Lzip_header header;
2331 + struct Matchfinder matchfinder;
2332 + struct LZ_encoder * encoder;
2334 + Lh_set_magic(header);
2335 + if (!Lh_set_dictionary_size(header, encoder_options.dictionary_size) ||
2336 + encoder_options.match_len_limit < min_match_len_limit ||
2337 + encoder_options.match_len_limit > max_match_len)
2338 + bb_error_msg_and_die("internal error");
2340 + if (!Mf_init(&matchfinder, Lh_get_dictionary_size(header),
2341 + encoder_options.match_len_limit)) {
2342 + bb_error_msg(bb_msg_memory_exhausted);
2343 + return -1;
2345 + Lh_set_dictionary_size(header, matchfinder.dictionary_size);
2347 + encoder = malloc(sizeof(struct LZ_encoder));
2348 + if (!encoder || !LZe_init(encoder, &matchfinder, header)) {
2349 + bb_error_msg(bb_msg_memory_exhausted);
2350 + retval = -1;
2351 + } else {
2352 + if (!LZe_encode_member(encoder)) {
2353 + bb_perror_msg(bb_msg_write_error);
2354 + retval = -1;
2356 + LZe_free(encoder);
2358 + free(encoder);
2359 + Mf_free(&matchfinder);
2360 + return retval;
2364 +int lzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2365 +#if ENABLE_FEATURE_LZIP_DECOMPRESS
2366 +int lzip_main(int argc, char **argv)
2367 +#else
2368 +int lzip_main(int argc UNUSED_PARAM, char **argv)
2369 +#endif
2371 + /* Mapping from gzip/bzip2 style 1..9 compression modes
2372 + to the corresponding LZMA compression modes. */
2373 + const struct Lzma_options option_mapping[] = {
2374 + {1 << 20, 5}, /* -0 */
2375 + {1 << 20, 5}, /* -1 */
2376 + {3 << 19, 6}, /* -2 */
2377 + {1 << 21, 8}, /* -3 */
2378 + {3 << 20, 12}, /* -4 */
2379 + {1 << 22, 20}, /* -5 */
2380 + {1 << 23, 36}, /* -6 */
2381 + {1 << 24, 68}, /* -7 */
2382 + {3 << 23, 132}, /* -8 */
2383 + {1 << 25, 273} /* -9 */
2384 + };
2385 + int i;
2386 + char *m_arg;
2387 + char *s_arg;
2388 + /* Must match BBUNPK_foo constants! */
2389 + unsigned opt = getopt32(argv, BBUNPK_OPTSTR IF_FEATURE_LZIP_DECOMPRESS("dt") "0123456789Fm:s:", &m_arg, &s_arg);
2391 +#if ENABLE_FEATURE_LZIP_DECOMPRESS /* lunzip_main may not be visible... */
2392 + if (opt & (BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST)) /* -d and/or -t */
2393 + return lunzip_main(argc, argv);
2394 +#endif
2395 + /* drop BBUNPK_OPTSTR "dt" bits */
2396 + opt >>= (BBUNPK_OPTSTRLEN IF_FEATURE_LZIP_DECOMPRESS(+ 2));
2398 + encoder_options = option_mapping[6]; /* default = "-6" */
2400 + for (i = 9; i >= 7; --i)
2401 + if (opt & (1 << i))
2402 + encoder_options = option_mapping[i];
2403 + for (i = 0; i <= 6; ++i)
2404 + if (opt & (1 << i))
2405 + encoder_options = option_mapping[i];
2406 + if (opt & (1 << 11)) /* -m */
2407 + encoder_options.match_len_limit =
2408 + getnum(m_arg, min_match_len_limit, max_match_len);
2409 + if (opt & (1 << 12)) /* -s */
2410 + encoder_options.dictionary_size = get_dict_size(s_arg);
2411 + /* end process options */
2412 + option_mask32 &= BBUNPK_OPTSTRMASK; /* retain only -cfkvq */
2414 + /* Initialize the CRC32 table */
2415 + global_crc32_new_table_le();
2416 + if (!dis_slots) {
2417 + Dis_slots_init();
2418 + Prob_prices_init();
2421 + argv += optind;
2422 + return bbunpack(argv, pack_lzip, append_ext, "lz");
2424 diff -urdN busybox-1.29.1.old/archival/tar.c busybox-1.29.1.new/archival/tar.c
2425 --- busybox-1.29.1.old/archival/tar.c 2018-07-02 13:23:06.000000000 +0200
2426 +++ busybox-1.29.1.new/archival/tar.c 2018-07-17 00:03:25.000000000 +0200
2427 @@ -43,7 +43,7 @@
2428 //config:config FEATURE_TAR_AUTODETECT
2429 //config: bool "Autodetect compressed tarballs"
2430 //config: default y
2431 -//config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
2432 +//config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZ || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
2433 //config: help
2434 //config: With this option tar can automatically detect compressed
2435 //config: tarballs. Currently it works only on files (not pipes etc).
2436 @@ -756,6 +756,7 @@
2437 //usage: IF_FEATURE_SEAMLESS_GZ("z")
2438 //usage: IF_FEATURE_SEAMLESS_XZ("J")
2439 //usage: IF_FEATURE_SEAMLESS_BZ2("j")
2440 +//usage: IF_FEATURE_SEAMLESS_LZ("y")
2441 //usage: IF_FEATURE_SEAMLESS_LZMA("a")
2442 //usage: IF_FEATURE_TAR_CREATE("h")
2443 //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
2444 @@ -795,6 +796,9 @@
2445 //usage: IF_FEATURE_SEAMLESS_BZ2(
2446 //usage: "\n -j (De)compress using bzip2"
2447 //usage: )
2448 +//usage: IF_FEATURE_SEAMLESS_LZ(
2449 +//usage: "\n -y (De)compress using lzip"
2450 +//usage: )
2451 //usage: IF_FEATURE_SEAMLESS_LZMA(
2452 //usage: "\n -a (De)compress using lzma"
2453 //usage: )
2454 @@ -832,6 +836,7 @@
2455 IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
2456 IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
2457 IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,) // 16th bit
2458 + IF_FEATURE_SEAMLESS_LZ( OPTBIT_LZIP ,)
2459 IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,)
2460 IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
2461 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
2462 @@ -859,6 +864,7 @@
2463 OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
2464 OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
2465 OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J
2466 + OPT_LZIP = IF_FEATURE_SEAMLESS_LZ( (1 << OPTBIT_LZIP )) + 0, // y
2467 OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
2468 OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
2469 OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
2470 @@ -868,7 +874,7 @@
2471 OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
2472 OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite
2474 - OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
2475 + OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_LZIP | OPT_COMPRESS),
2477 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
2478 static const char tar_longopts[] ALIGN1 =
2479 @@ -901,6 +907,9 @@
2480 # if ENABLE_FEATURE_SEAMLESS_GZ
2481 "gzip\0" No_argument "z"
2482 # endif
2483 +# if ENABLE_FEATURE_SEAMLESS_LZ
2484 + "lzip\0" No_argument "y"
2485 +# endif
2486 # if ENABLE_FEATURE_SEAMLESS_XZ
2487 "xz\0" No_argument "J"
2488 # endif
2489 @@ -1003,6 +1012,7 @@
2490 IF_FEATURE_TAR_FROM( "T:*X:*")
2491 IF_FEATURE_SEAMLESS_GZ( "z" )
2492 IF_FEATURE_SEAMLESS_XZ( "J" )
2493 + IF_FEATURE_SEAMLESS_LZ( "y" )
2494 IF_FEATURE_SEAMLESS_Z( "Z" )
2495 IF_FEATURE_TAR_NOPRESERVE_TIME("m")
2496 IF_FEATURE_TAR_LONG_OPTIONS("\xf9:") // --strip-components
2497 @@ -1052,6 +1062,7 @@
2498 showopt(OPT_EXCLUDE_FROM );
2499 showopt(OPT_GZIP );
2500 showopt(OPT_XZ );
2501 + showopt(OPT_LZIP );
2502 showopt(OPT_COMPRESS );
2503 showopt(OPT_NOPRESERVE_TIME );
2504 showopt(OPT_STRIP_COMPONENTS);
2505 @@ -1184,6 +1195,8 @@
2506 zipMode = "gzip";
2507 if (opt & OPT_BZIP2)
2508 zipMode = "bzip2";
2509 + if (opt & OPT_LZIP)
2510 + zipMode = "lzip";
2511 if (opt & OPT_LZMA)
2512 zipMode = "lzma";
2513 if (opt & OPT_XZ)
2514 @@ -1220,6 +1233,10 @@
2515 USE_FOR_MMU(IF_FEATURE_SEAMLESS_BZ2(xformer = unpack_bz2_stream;))
2516 USE_FOR_NOMMU(xformer_prog = "bunzip2";)
2518 + if (opt & OPT_LZIP) {
2519 + USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZ(xformer = unpack_lz_stream;))
2520 + USE_FOR_NOMMU(xformer_prog = "lunzip";)
2522 if (opt & OPT_LZMA) {
2523 USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZMA(xformer = unpack_lzma_stream;))
2524 USE_FOR_NOMMU(xformer_prog = "unlzma";)
2525 diff -urdN busybox-1.29.1.old/configs/TEST_nommu_defconfig busybox-1.29.1.new/configs/TEST_nommu_defconfig
2526 --- busybox-1.29.1.old/configs/TEST_nommu_defconfig 2018-07-02 13:23:06.000000000 +0200
2527 +++ busybox-1.29.1.new/configs/TEST_nommu_defconfig 2018-07-17 00:03:25.000000000 +0200
2528 @@ -107,6 +107,7 @@
2529 CONFIG_FEATURE_SEAMLESS_LZMA=y
2530 CONFIG_FEATURE_SEAMLESS_BZ2=y
2531 CONFIG_FEATURE_SEAMLESS_GZ=y
2532 +CONFIG_FEATURE_SEAMLESS_LZ=y
2533 CONFIG_FEATURE_SEAMLESS_Z=y
2534 CONFIG_AR=y
2535 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2536 @@ -120,6 +121,8 @@
2537 CONFIG_GUNZIP=y
2538 CONFIG_GZIP=y
2539 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2540 +CONFIG_LUNZIP=y
2541 +CONFIG_LZIP=y
2542 CONFIG_LZOP=y
2543 CONFIG_LZOP_COMPR_HIGH=y
2544 CONFIG_RPM2CPIO=y
2545 diff -urdN busybox-1.29.1.old/configs/TEST_noprintf_defconfig busybox-1.29.1.new/configs/TEST_noprintf_defconfig
2546 --- busybox-1.29.1.old/configs/TEST_noprintf_defconfig 2018-07-02 13:23:06.000000000 +0200
2547 +++ busybox-1.29.1.new/configs/TEST_noprintf_defconfig 2018-07-17 00:03:25.000000000 +0200
2548 @@ -118,6 +118,7 @@
2549 CONFIG_FEATURE_SEAMLESS_LZMA=y
2550 CONFIG_FEATURE_SEAMLESS_BZ2=y
2551 CONFIG_FEATURE_SEAMLESS_GZ=y
2552 +CONFIG_FEATURE_SEAMLESS_LZ=y
2553 CONFIG_FEATURE_SEAMLESS_Z=y
2554 # CONFIG_AR is not set
2555 # CONFIG_FEATURE_AR_LONG_FILENAMES is not set
2556 @@ -132,6 +133,8 @@
2557 # CONFIG_GUNZIP is not set
2558 # CONFIG_GZIP is not set
2559 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2560 +# CONFIG_LUNZIP is not set
2561 +# CONFIG_LZIP is not set
2562 # CONFIG_LZOP is not set
2563 # CONFIG_LZOP_COMPR_HIGH is not set
2564 # CONFIG_RPM2CPIO is not set
2565 diff -urdN busybox-1.29.1.old/configs/TEST_rh9_defconfig busybox-1.29.1.new/configs/TEST_rh9_defconfig
2566 --- busybox-1.29.1.old/configs/TEST_rh9_defconfig 2018-07-02 13:23:06.000000000 +0200
2567 +++ busybox-1.29.1.new/configs/TEST_rh9_defconfig 2018-07-17 00:03:25.000000000 +0200
2568 @@ -116,6 +116,7 @@
2569 CONFIG_FEATURE_SEAMLESS_LZMA=y
2570 CONFIG_FEATURE_SEAMLESS_BZ2=y
2571 CONFIG_FEATURE_SEAMLESS_GZ=y
2572 +CONFIG_FEATURE_SEAMLESS_LZ=y
2573 CONFIG_FEATURE_SEAMLESS_Z=y
2574 CONFIG_AR=y
2575 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2576 @@ -130,6 +131,8 @@
2577 CONFIG_GUNZIP=y
2578 CONFIG_GZIP=y
2579 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2580 +CONFIG_LUNZIP=y
2581 +CONFIG_LZIP=y
2582 CONFIG_LZOP=y
2583 # CONFIG_LZOP_COMPR_HIGH is not set
2584 CONFIG_RPM2CPIO=y
2585 diff -urdN busybox-1.29.1.old/configs/android2_defconfig busybox-1.29.1.new/configs/android2_defconfig
2586 --- busybox-1.29.1.old/configs/android2_defconfig 2018-07-02 13:23:06.000000000 +0200
2587 +++ busybox-1.29.1.new/configs/android2_defconfig 2018-07-17 00:03:25.000000000 +0200
2588 @@ -121,6 +121,7 @@
2589 CONFIG_FEATURE_SEAMLESS_LZMA=y
2590 CONFIG_FEATURE_SEAMLESS_BZ2=y
2591 CONFIG_FEATURE_SEAMLESS_GZ=y
2592 +CONFIG_FEATURE_SEAMLESS_LZ=y
2593 CONFIG_FEATURE_SEAMLESS_Z=y
2594 CONFIG_AR=y
2595 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2596 @@ -135,6 +136,8 @@
2597 CONFIG_GUNZIP=y
2598 CONFIG_GZIP=y
2599 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2600 +CONFIG_LUNZIP=y
2601 +CONFIG_LZIP=y
2602 CONFIG_LZOP=y
2603 CONFIG_LZOP_COMPR_HIGH=y
2604 CONFIG_RPM2CPIO=y
2605 diff -urdN busybox-1.29.1.old/configs/android_defconfig busybox-1.29.1.new/configs/android_defconfig
2606 --- busybox-1.29.1.old/configs/android_defconfig 2018-07-02 13:23:06.000000000 +0200
2607 +++ busybox-1.29.1.new/configs/android_defconfig 2018-07-17 00:03:25.000000000 +0200
2608 @@ -141,6 +141,7 @@
2609 CONFIG_FEATURE_SEAMLESS_LZMA=y
2610 CONFIG_FEATURE_SEAMLESS_BZ2=y
2611 CONFIG_FEATURE_SEAMLESS_GZ=y
2612 +CONFIG_FEATURE_SEAMLESS_LZ=y
2613 CONFIG_FEATURE_SEAMLESS_Z=y
2614 CONFIG_AR=y
2615 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2616 @@ -156,6 +157,8 @@
2617 CONFIG_GZIP=y
2618 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2619 CONFIG_GZIP_FAST=0
2620 +CONFIG_LUNZIP=y
2621 +CONFIG_LZIP=y
2622 CONFIG_LZOP=y
2623 CONFIG_LZOP_COMPR_HIGH=y
2624 CONFIG_RPM2CPIO=y
2625 diff -urdN busybox-1.29.1.old/configs/android_ndk_defconfig busybox-1.29.1.new/configs/android_ndk_defconfig
2626 --- busybox-1.29.1.old/configs/android_ndk_defconfig 2018-07-02 13:23:06.000000000 +0200
2627 +++ busybox-1.29.1.new/configs/android_ndk_defconfig 2018-07-17 00:03:25.000000000 +0200
2628 @@ -131,6 +131,7 @@
2629 CONFIG_FEATURE_SEAMLESS_LZMA=y
2630 CONFIG_FEATURE_SEAMLESS_BZ2=y
2631 CONFIG_FEATURE_SEAMLESS_GZ=y
2632 +CONFIG_FEATURE_SEAMLESS_LZ=y
2633 CONFIG_FEATURE_SEAMLESS_Z=y
2634 CONFIG_AR=y
2635 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2636 @@ -153,6 +154,8 @@
2637 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2638 CONFIG_GZIP_FAST=0
2639 # CONFIG_FEATURE_GZIP_LEVELS is not set
2640 +CONFIG_LUNZIP=y
2641 +CONFIG_LZIP=y
2642 CONFIG_LZOP=y
2643 CONFIG_LZOP_COMPR_HIGH=y
2644 CONFIG_RPM=y
2645 diff -urdN busybox-1.29.1.old/configs/cygwin_defconfig busybox-1.29.1.new/configs/cygwin_defconfig
2646 --- busybox-1.29.1.old/configs/cygwin_defconfig 2018-07-02 13:23:06.000000000 +0200
2647 +++ busybox-1.29.1.new/configs/cygwin_defconfig 2018-07-17 00:03:25.000000000 +0200
2648 @@ -121,6 +121,7 @@
2649 CONFIG_FEATURE_SEAMLESS_LZMA=y
2650 CONFIG_FEATURE_SEAMLESS_BZ2=y
2651 CONFIG_FEATURE_SEAMLESS_GZ=y
2652 +CONFIG_FEATURE_SEAMLESS_LZ=y
2653 # CONFIG_FEATURE_SEAMLESS_Z is not set
2654 # CONFIG_AR is not set
2655 # CONFIG_FEATURE_AR_LONG_FILENAMES is not set
2656 @@ -135,6 +136,8 @@
2657 CONFIG_GUNZIP=y
2658 CONFIG_GZIP=y
2659 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2660 +CONFIG_LUNZIP=y
2661 +CONFIG_LZIP=y
2662 CONFIG_LZOP=y
2663 # CONFIG_LZOP_COMPR_HIGH is not set
2664 CONFIG_RPM2CPIO=y
2665 diff -urdN busybox-1.29.1.old/configs/freebsd_defconfig busybox-1.29.1.new/configs/freebsd_defconfig
2666 --- busybox-1.29.1.old/configs/freebsd_defconfig 2018-07-02 13:23:06.000000000 +0200
2667 +++ busybox-1.29.1.new/configs/freebsd_defconfig 2018-07-17 00:03:25.000000000 +0200
2668 @@ -119,6 +119,7 @@
2669 CONFIG_FEATURE_SEAMLESS_LZMA=y
2670 CONFIG_FEATURE_SEAMLESS_BZ2=y
2671 CONFIG_FEATURE_SEAMLESS_GZ=y
2672 +CONFIG_FEATURE_SEAMLESS_LZ=y
2673 CONFIG_FEATURE_SEAMLESS_Z=y
2674 CONFIG_AR=y
2675 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2676 @@ -133,6 +134,8 @@
2677 CONFIG_GUNZIP=y
2678 CONFIG_GZIP=y
2679 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2680 +CONFIG_LUNZIP=y
2681 +CONFIG_LZIP=y
2682 CONFIG_LZOP=y
2683 # CONFIG_LZOP_COMPR_HIGH is not set
2684 CONFIG_RPM2CPIO=y
2685 diff -urdN busybox-1.29.1.old/include/bb_archive.h busybox-1.29.1.new/include/bb_archive.h
2686 --- busybox-1.29.1.old/include/bb_archive.h 2018-07-02 13:23:06.000000000 +0200
2687 +++ busybox-1.29.1.new/include/bb_archive.h 2018-07-17 00:03:25.000000000 +0200
2688 @@ -9,6 +9,8 @@
2689 COMPRESS_MAGIC = 0x1f9d,
2690 GZIP_MAGIC = 0x1f8b,
2691 BZIP2_MAGIC = 256 * 'B' + 'Z',
2692 + LZIP_MAGIC1 = 0x4C5A,
2693 + LZIP_MAGIC2 = 0x4950,
2694 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
2695 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
2696 XZ_MAGIC1 = 256 * 0xfd + '7',
2697 @@ -21,6 +23,8 @@
2698 COMPRESS_MAGIC = 0x9d1f,
2699 GZIP_MAGIC = 0x8b1f,
2700 BZIP2_MAGIC = 'B' + 'Z' * 256,
2701 + LZIP_MAGIC1 = 0x5A4C,
2702 + LZIP_MAGIC2 = 0x5049,
2703 XZ_MAGIC1 = 0xfd + '7' * 256,
2704 XZ_MAGIC2 = 'z' + ('X' + ('Z' + 0 * 256) * 256) * 256,
2705 XZ_MAGIC1a = 0xfd + ('7' + ('z' + 'X' * 256) * 256) * 256,
2706 @@ -196,6 +200,7 @@
2707 char get_header_tar_gz(archive_handle_t *archive_handle) FAST_FUNC;
2708 char get_header_tar_xz(archive_handle_t *archive_handle) FAST_FUNC;
2709 char get_header_tar_bz2(archive_handle_t *archive_handle) FAST_FUNC;
2710 +char get_header_tar_lz(archive_handle_t *archive_handle) FAST_FUNC;
2711 char get_header_tar_lzma(archive_handle_t *archive_handle) FAST_FUNC;
2712 char get_header_tar_xz(archive_handle_t *archive_handle) FAST_FUNC;
2714 @@ -251,6 +256,7 @@
2715 IF_DESKTOP(long long) int unpack_Z_stream(transformer_state_t *xstate) FAST_FUNC;
2716 IF_DESKTOP(long long) int unpack_gz_stream(transformer_state_t *xstate) FAST_FUNC;
2717 IF_DESKTOP(long long) int unpack_bz2_stream(transformer_state_t *xstate) FAST_FUNC;
2718 +IF_DESKTOP(long long) int unpack_lz_stream(transformer_state_t *xstate) FAST_FUNC;
2719 IF_DESKTOP(long long) int unpack_lzma_stream(transformer_state_t *xstate) FAST_FUNC;
2720 IF_DESKTOP(long long) int unpack_xz_stream(transformer_state_t *xstate) FAST_FUNC;
2722 diff -urdN busybox-1.29.1.old/include/libbb.h busybox-1.29.1.new/include/libbb.h
2723 --- busybox-1.29.1.old/include/libbb.h 2018-07-02 13:23:06.000000000 +0200
2724 +++ busybox-1.29.1.new/include/libbb.h 2018-07-17 00:03:25.000000000 +0200
2725 @@ -879,6 +879,7 @@
2726 #define SEAMLESS_COMPRESSION (0 \
2727 || ENABLE_FEATURE_SEAMLESS_XZ \
2728 || ENABLE_FEATURE_SEAMLESS_LZMA \
2729 + || ENABLE_FEATURE_SEAMLESS_LZ \
2730 || ENABLE_FEATURE_SEAMLESS_BZ2 \
2731 || ENABLE_FEATURE_SEAMLESS_GZ \
2732 || ENABLE_FEATURE_SEAMLESS_Z)
2733 @@ -1332,6 +1333,7 @@
2734 /* Don't need IF_xxx() guard for these */
2735 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2736 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2737 +int lunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2739 #if ENABLE_ROUTE
2740 void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
2741 diff -urdN busybox-1.29.1.old/miscutils/man.c busybox-1.29.1.new/miscutils/man.c
2742 --- busybox-1.29.1.old/miscutils/man.c 2018-07-02 13:23:06.000000000 +0200
2743 +++ busybox-1.29.1.new/miscutils/man.c 2018-07-17 00:03:25.000000000 +0200
2744 @@ -180,6 +180,11 @@
2745 if (run_pipe(filename_with_zext, man, level))
2746 return 1;
2747 #endif
2748 +#if ENABLE_FEATURE_SEAMLESS_LZ
2749 + strcpy(ext, "lz");
2750 + if (run_pipe(filename_with_zext, man, level))
2751 + return 1;
2752 +#endif
2753 #if ENABLE_FEATURE_SEAMLESS_BZ2
2754 strcpy(ext, "bz2");
2755 if (run_pipe(filename_with_zext, man, level))
2756 diff -urdN busybox-1.29.1.old/scripts/fix_ws.sh busybox-1.29.1.new/scripts/fix_ws.sh
2757 --- busybox-1.29.1.old/scripts/fix_ws.sh 2018-07-02 13:23:06.000000000 +0200
2758 +++ busybox-1.29.1.new/scripts/fix_ws.sh 2018-07-17 00:03:25.000000000 +0200
2759 @@ -19,6 +19,7 @@
2760 | while read name; do
2761 test "YES" = "${name/*.bz2/YES}" && continue
2762 test "YES" = "${name/*.gz/YES}" && continue
2763 + test "YES" = "${name/*.lz/YES}" && continue
2764 test "YES" = "${name/*.png/YES}" && continue
2765 test "YES" = "${name/*.gif/YES}" && continue
2766 test "YES" = "${name/*.jpg/YES}" && continue
2767 diff -urdN busybox-1.29.1.old/testsuite/bunzip2.tests busybox-1.29.1.new/testsuite/bunzip2.tests
2768 --- busybox-1.29.1.old/testsuite/bunzip2.tests 2018-07-02 13:23:06.000000000 +0200
2769 +++ busybox-1.29.1.new/testsuite/bunzip2.tests 2018-07-17 00:03:25.000000000 +0200
2770 @@ -9,6 +9,9 @@
2771 elif test "${0##*/}" = "bunzip2.tests"; then
2772 unpack=bunzip2
2773 ext=bz2
2774 +elif test "${0##*/}" = "lunzip.tests"; then
2775 + unpack=lunzip
2776 + ext=lz
2777 else
2778 echo "WTF? argv0='$0'"
2779 exit 1
2780 @@ -35,6 +38,13 @@
2781 $ECHO -ne "\x17\x72\x45\x38\x50\x90\x5b\xb8\xe8\xa3"
2784 +hello_lz() {
2785 +# Lzipped "HELLO\n"
2786 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x24\x11\x45\xcf\x72\xcd\x3d\x3a\xdf"
2787 +$ECHO -ne "\xff\xff\xdd\x12\x00\x00\x6e\xd7\xac\xfd\x06\x00\x00\x00\x00\x00"
2788 +$ECHO -ne "\x00\x00\x2a\x00\x00\x00\x00\x00\x00\x00"
2791 # We had bunzip2 error on this .bz2 file (fixed in rev 22521)
2792 test1_bz2()
2794 @@ -530,6 +540,12 @@
2795 expected="ok\n"
2796 prep; check "$unpack: delete src" "${bb}$unpack t2.$ext; test ! -f t2.$ext && echo ok"
2798 +expected="ok\n"
2799 +rm -f t1.*
2800 +hello_$ext > t1.t$ext
2801 +check "$unpack: replace .t$ext --> .tar" \
2802 +"${bb}$unpack t1.t$ext && test -f t1.tar && test ! -f t1.t$ext && echo ok"
2805 rm -rf testdir
2807 diff -urdN busybox-1.29.1.old/testsuite/bzcat.tests busybox-1.29.1.new/testsuite/bzcat.tests
2808 --- busybox-1.29.1.old/testsuite/bzcat.tests 2018-07-02 13:23:06.000000000 +0200
2809 +++ busybox-1.29.1.new/testsuite/bzcat.tests 2018-07-19 02:04:10.000000000 +0200
2810 @@ -30,10 +30,18 @@
2811 $ECHO -ne "\x17\x72\x45\x38\x50\x90\x5b\xb8\xe8\xa3"
2814 +hello_lz() {
2815 +# Lzipped "HELLO\n"
2816 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x24\x11\x45\xcf\x72\xcd\x3d\x3a\xdf"
2817 +$ECHO -ne "\xff\xff\xdd\x12\x00\x00\x6e\xd7\xac\xfd\x06\x00\x00\x00\x00\x00"
2818 +$ECHO -ne "\x00\x00\x2a\x00\x00\x00\x00\x00\x00\x00"
2821 test x"$CONFIG_ZCAT" = x"y" && \
2822 for ext in \
2823 `test x"$CONFIG_FEATURE_SEAMLESS_GZ" = x"y" && echo gz` \
2824 `test x"$CONFIG_FEATURE_SEAMLESS_BZ2" = x"y" && echo bz2` \
2825 + `test x"$CONFIG_FEATURE_SEAMLESS_LZ" = x"y" && echo lz` \
2826 `test x"$CONFIG_FEATURE_SEAMLESS_Z" = x"y" && echo Z`
2828 prep() {
2829 diff -urdN busybox-1.29.1.old/testsuite/lunzip.tests busybox-1.29.1.new/testsuite/lunzip.tests
2830 --- busybox-1.29.1.old/testsuite/lunzip.tests 1970-01-01 01:00:00.000000000 +0100
2831 +++ busybox-1.29.1.new/testsuite/lunzip.tests 2018-07-17 00:03:25.000000000 +0200
2832 @@ -0,0 +1,3 @@
2833 +#!/bin/sh
2835 +. ./bunzip2.tests
2836 diff -urdN busybox-1.29.1.old/testsuite/lzip.tests busybox-1.29.1.new/testsuite/lzip.tests
2837 --- busybox-1.29.1.old/testsuite/lzip.tests 1970-01-01 01:00:00.000000000 +0100
2838 +++ busybox-1.29.1.new/testsuite/lzip.tests 2018-07-17 14:06:36.000000000 +0200
2839 @@ -0,0 +1,58 @@
2840 +#!/bin/sh
2841 +# Copyright (C) 2012-2018 Antonio Diaz Diaz.
2842 +# Licensed under GPLv2 or later, see file LICENSE in this source tree.
2844 +. ./testing.sh
2846 +rm -rf lzip.testdir 2>/dev/null
2847 +mkdir lzip.testdir
2849 +# Lzipped "a"
2850 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x30\xc1\xfb\xff\xff\xff\xe0\x00\x00\
2851 +\x00\x43\xbe\xb7\xe8\x01\x00\x00\x00\x00\x00\x00\x00\x25\x00\x00\
2852 +\x00\x00\x00\x00\x00" > lzip.testdir/a.lz
2854 +# Lzipped zero-length file
2855 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x83\xff\xfb\xff\xff\xc0\x00\x00\x00\
2856 +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x00\x00\
2857 +\x00\x00\x00\x00" > lzip.testdir/zero.lz
2860 +# testing "test name" "commands" "expected result" "file input" "stdin"
2862 +testing "lzip reads from standard input" \
2863 +"busybox lzip | cmp lzip.testdir/a.lz -" "" "" "a"
2865 +testing "lzip accepts single minus" \
2866 +"busybox lzip - | cmp lzip.testdir/a.lz -" "" "" "a"
2868 +testing "lzip compresses a zero-length file" \
2869 +"> zero ; busybox lzip -c zero | cmp lzip.testdir/zero.lz -" "" "" ""
2871 +testing "lzip replaces original file" \
2872 +"$ECHO foo > lzip.testdir/foo
2873 +rm -f lzip.testdir/foo.lz 2>/dev/null
2874 +busybox lzip lzip.testdir/foo
2875 +test -f lzip.testdir/foo.lz && test ! -f lzip.testdir/foo && echo ok" \
2876 +"ok\n" "" ""
2878 +testing "lzip replaces multiple files" \
2879 +"$ECHO foo > lzip.testdir/foo
2880 +$ECHO bar > lzip.testdir/bar
2881 +rm -f lzip.testdir/foo.lz 2>/dev/null
2882 +rm -f lzip.testdir/bar.lz 2>/dev/null
2883 +busybox lzip lzip.testdir/foo lzip.testdir/bar
2884 +test -f lzip.testdir/foo.lz && test ! -f lzip.testdir/foo && echo ok1
2885 +test -f lzip.testdir/bar.lz && test ! -f lzip.testdir/bar && echo ok2" \
2886 +"ok1\nok2\n" "" ""
2888 +testing "lzip -c does not remove original file" \
2889 +"$ECHO foo > lzip.testdir/foo
2890 +busybox lzip -c lzip.testdir/foo >/dev/null
2891 +test -f lzip.testdir/foo && echo ok" "ok\n" "" ""
2894 +# Clean up
2895 +rm -rf lzip.testdir 2>/dev/null
2897 +exit $FAILCOUNT
2898 diff -urdN busybox-1.29.1.old/testsuite/tar.tests.~1~ busybox-1.29.1.new/testsuite/tar.tests.~1~
2899 --- busybox-1.29.1.old/testsuite/tar.tests.~1~ 1970-01-01 01:00:00.000000000 +0100
2900 +++ busybox-1.29.1.new/testsuite/tar.tests.~1~ 2018-07-17 00:03:25.000000000 +0200
2901 @@ -0,0 +1,368 @@
2902 +#!/bin/sh
2903 +# Copyright 2009 by Denys Vlasenko
2904 +# Licensed under GPLv2, see file LICENSE in this source tree.
2906 +. ./testing.sh
2908 +unset LANG
2909 +unset LANGUAGE
2910 +unset LC_COLLATE
2911 +unset LC_ALL
2912 +umask 022
2914 +# testing "test name" "script" "expected result" "file input" "stdin"
2916 +testing "Empty file is not a tarball" '\
2917 +tar xvf - 2>&1; echo $?
2918 +' "\
2919 +tar: short read
2921 +" \
2922 +"" ""
2923 +SKIP=
2925 +optional FEATURE_SEAMLESS_GZ GUNZIP
2926 +# In NOMMU case, "invalid magic" message comes from gunzip child process.
2927 +# Otherwise, it comes from tar.
2928 +# Need to fix output up to avoid false positive.
2929 +testing "Empty file is not a tarball.tar.gz" '\
2930 +{ tar xvzf - 2>&1; echo $?; } | grep -Fv "invalid magic"
2931 +' "\
2932 +tar: short read
2934 +" \
2935 +"" ""
2936 +SKIP=
2938 +testing "Two zeroed blocks is a ('truncated') empty tarball" '\
2939 +dd if=/dev/zero bs=512 count=2 2>/dev/null | tar xvf - 2>&1; echo $?
2940 +' "\
2942 +" \
2943 +"" ""
2944 +SKIP=
2946 +testing "Twenty zeroed blocks is an empty tarball" '\
2947 +dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $?
2948 +' "\
2950 +" \
2951 +"" ""
2952 +SKIP=
2954 +mkdir tar.tempdir && cd tar.tempdir || exit 1
2955 +# "tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input":
2956 +# GNU tar 1.26 records as hardlinks:
2957 +# input_hard2 -> input_hard1
2958 +# input_hard1 -> input_hard1 (!!!)
2959 +# input_dir/file -> input_dir/file
2960 +# input -> input
2961 +# As of 1.24.0, we don't record last two: for them, nlink==1
2962 +# and we check for "hardlink"ness only files with nlink!=1
2963 +# We also don't use "hrw-r--r--" notation for hardlinks in "tar tv" listing.
2964 +optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
2965 +testing "tar hardlinks and repeated files" '\
2966 +>input_hard1
2967 +ln input_hard1 input_hard2
2968 +mkdir input_dir
2969 +>input_dir/file
2970 +chmod -R 644 *
2971 +chmod 755 input_dir
2972 +tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
2973 +tar tvf test.tar | sed "s/.*[0-9] input/input/"
2974 +rm -rf input_dir
2975 +tar xf test.tar 2>&1
2976 +echo Ok: $?
2977 +ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
2978 +' "\
2979 +input
2980 +input_dir/
2981 +input_dir/file
2982 +input_hard1
2983 +input_hard2 -> input_hard1
2984 +input_hard1 -> input_hard1
2985 +input_dir/
2986 +input_dir/file
2987 +input
2988 +Ok: 0
2989 +-rw-r--r-- input_dir/file
2990 +drwxr-xr-x input_dir
2991 +-rw-r--r-- input_hard1
2992 +-rw-r--r-- input_hard2
2993 +" \
2994 +"" ""
2995 +SKIP=
2996 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
2998 +mkdir tar.tempdir && cd tar.tempdir || exit 1
2999 +optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
3000 +testing "tar hardlinks mode" '\
3001 +>input_hard1
3002 +chmod 741 input_hard1
3003 +ln input_hard1 input_hard2
3004 +mkdir input_dir
3005 +ln input_hard1 input_dir
3006 +ln input_hard2 input_dir
3007 +chmod 550 input_dir
3008 +# On some filesystems, input_dir/input_hard2 is returned by readdir
3009 +# BEFORE input_dir/input_hard1! Thats why we cant just "tar cf ... input_*":
3010 +tar cf test.tar input_dir/input_hard* input_hard*
3011 +tar tvf test.tar | sed "s/.*[0-9] input/input/"
3012 +chmod 770 input_dir
3013 +rm -rf input_*
3014 +tar xf test.tar 2>&1
3015 +echo Ok: $?
3016 +ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
3017 +' "\
3018 +input_dir/input_hard1
3019 +input_dir/input_hard2 -> input_dir/input_hard1
3020 +input_hard1 -> input_dir/input_hard1
3021 +input_hard2 -> input_dir/input_hard1
3022 +Ok: 0
3023 +-rwxr----x input_dir/input_hard1
3024 +-rwxr----x input_dir/input_hard2
3025 +-rwxr----x input_hard1
3026 +-rwxr----x input_hard2
3027 +" \
3028 +"" ""
3029 +SKIP=
3030 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3032 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3033 +optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
3034 +testing "tar symlinks mode" '\
3035 +>input_file
3036 +chmod 741 input_file
3037 +ln -s input_file input_soft
3038 +mkdir input_dir
3039 +ln input_file input_dir
3040 +ln input_soft input_dir
3041 +chmod 550 input_dir
3042 +tar cf test.tar input_dir/* input_[fs]*
3043 +tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
3044 +chmod 770 input_dir
3045 +rm -rf input_*
3046 +tar xf test.tar 2>&1
3047 +echo Ok: $?
3048 +ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
3049 +' "\
3050 +input_dir/input_file
3051 +input_dir/input_soft -> input_file
3052 +input_file -> input_dir/input_file
3053 +input_soft -> input_dir/input_soft
3054 +Ok: 0
3055 +-rwxr----x input_dir/input_file
3056 +lrwxrwxrwx input_file
3057 +-rwxr----x input_file
3058 +lrwxrwxrwx input_file
3059 +" \
3060 +"" ""
3061 +SKIP=
3062 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3064 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3065 +optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
3066 +testing "tar --overwrite" "\
3067 +ln input input_hard
3068 +tar cf test.tar input_hard
3069 +echo WRONG >input
3070 +# --overwrite opens 'input_hard' without unlinking,
3071 +# thus 'input_hard' still linked to 'input' and we write 'Ok' into it
3072 +tar xf test.tar --overwrite 2>&1 && cat input
3073 +" "\
3075 +" \
3076 +"Ok\n" ""
3077 +SKIP=
3078 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3080 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3081 +test x"$SKIP_KNOWN_BUGS" = x"" && {
3082 +# Needs to be run under non-root for meaningful test
3083 +optional FEATURE_TAR_CREATE
3084 +testing "tar writing into read-only dir" '\
3085 +mkdir input_dir
3086 +>input_dir/input_file
3087 +chmod 550 input_dir
3088 +tar cf test.tar input_dir
3089 +tar tvf test.tar | sed "s/.*[0-9] input/input/"
3090 +chmod 770 input_dir
3091 +rm -rf input_*
3092 +tar xf test.tar 2>&1
3093 +echo Ok: $?
3094 +ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
3095 +chmod 770 input_dir
3096 +' "\
3097 +input_dir/
3098 +input_dir/input_file
3099 +Ok: 0
3100 +-rw-r--r-- input_dir/input_file
3101 +dr-xr-x--- input_dir
3102 +" \
3103 +"" ""
3104 +SKIP=
3106 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3108 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3109 +# Had a bug where on extract autodetect first "switched off" -z
3110 +# and then failed to recognize .tgz extension
3111 +optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ GUNZIP
3112 +testing "tar extract tgz" "\
3113 +dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
3114 +tar -czf F0.tgz F0
3115 +rm F0
3116 +tar -xzvf F0.tgz && echo Ok
3117 +rm F0 || echo BAD
3118 +" "\
3121 +" \
3122 +"" ""
3123 +SKIP=
3124 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3126 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3127 +# Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
3128 +# (the uuencoded hello_world.txz contains one empty file named "hello_world")
3129 +optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
3130 +testing "tar extract txz" "\
3131 +uudecode -o input && tar tf input && echo Ok
3132 +" "\
3133 +hello_world
3135 +" \
3136 +"" "\
3137 +begin-base64 644 hello_world.txz
3138 +/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
3139 +SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
3140 +Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
3141 +AAAEWVo=
3142 +====
3144 +SKIP=
3145 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3147 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3148 +# On extract, everything up to and including last ".." component is stripped
3149 +optional FEATURE_TAR_CREATE
3150 +testing "tar strips /../ on extract" "\
3151 +rm -rf input_* test.tar 2>/dev/null
3152 +mkdir input_dir
3153 +echo Ok >input_dir/file
3154 +tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
3155 +rm -rf input_* 2>/dev/null
3156 +tar -vxf test.tar 2>&1
3157 +cat input_dir/file 2>&1
3158 +" "\
3159 +tar: removing leading './../tar.tempdir/input_dir/../' from member names
3160 +input_dir/
3161 +input_dir/file
3163 +" \
3164 +"" ""
3165 +SKIP=
3166 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3168 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3169 +# attack.tar.bz2 has symlink pointing to a system file
3170 +# followed by a regular file with the same name
3171 +# containing "root::0:0::/root:/bin/sh":
3172 +# lrwxrwxrwx root/root passwd -> /tmp/passwd
3173 +# -rw-r--r-- root/root passwd
3174 +# naive tar implementation may end up creating the symlink
3175 +# and then writing into it.
3176 +# The correct implementation unlinks target before
3177 +# creating the second file.
3178 +# We test that /tmp/passwd remains empty:
3179 +optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_BZ2
3180 +testing "tar does not extract into symlinks" "\
3181 +>>/tmp/passwd && uudecode -o input && tar xf input 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
3182 +" "\
3183 +tar: skipping unsafe symlink to '/tmp/passwd' in archive, set EXTRACT_UNSAFE_SYMLINKS=1 to extract
3185 +" \
3186 +"" "\
3187 +begin-base64 644 attack.tar.bz2
3188 +QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
3189 +po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
3190 +DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
3191 +l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
3192 +====
3194 +SKIP=
3195 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3197 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3198 +# And same with -k
3199 +optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_BZ2
3200 +testing "tar -k does not extract into symlinks" "\
3201 +>>/tmp/passwd && uudecode -o input && tar xf input -k 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
3202 +" "\
3203 +tar: skipping unsafe symlink to '/tmp/passwd' in archive, set EXTRACT_UNSAFE_SYMLINKS=1 to extract
3205 +" \
3206 +"" "\
3207 +begin-base64 644 attack.tar.bz2
3208 +QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
3209 +po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
3210 +DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
3211 +l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
3212 +====
3214 +SKIP=
3215 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3217 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3218 +optional UNICODE_SUPPORT FEATURE_TAR_GNU_EXTENSIONS FEATURE_SEAMLESS_BZ2 FEATURE_TAR_AUTODETECT
3219 +testing "Pax-encoded UTF8 names and symlinks" '\
3220 +tar xvf ../tar.utf8.tar.bz2 2>&1; echo $?
3221 +export LANG=en_US.UTF-8
3222 +ls -l etc/ssl/certs/* | sed "s:.*etc/:etc/:" | sort
3223 +unset LANG
3224 +rm -rf etc usr
3225 +' "\
3226 +etc/ssl/certs/3b2716e5.0
3227 +etc/ssl/certs/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem
3228 +tar: skipping unsafe symlink to '/usr/share/ca-certificates/mozilla/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.crt' in archive, set EXTRACT_UNSAFE_SYMLINKS=1 to extract
3229 +etc/ssl/certs/f80cc7f6.0
3230 +usr/share/ca-certificates/mozilla/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.crt
3232 +etc/ssl/certs/3b2716e5.0 -> EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem
3233 +etc/ssl/certs/f80cc7f6.0 -> EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem
3234 +" \
3235 +"" ""
3236 +SKIP=
3237 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3239 +mkdir tar.tempdir && cd tar.tempdir || exit 1
3240 +optional FEATURE_SEAMLESS_BZ2 FEATURE_TAR_AUTODETECT
3241 +testing "Symlink attack: create symlink and then write through it" '\
3242 +exec 2>&1
3243 +uudecode -o input && tar xvf input; echo $?
3244 +ls /tmp/bb_test_evilfile
3245 +ls bb_test_evilfile
3246 +ls symlink/bb_test_evilfile
3247 +' "\
3248 +anything.txt
3249 +symlink
3250 +tar: skipping unsafe symlink to '/tmp' in archive, set EXTRACT_UNSAFE_SYMLINKS=1 to extract
3251 +symlink/bb_test_evilfile
3253 +ls: /tmp/bb_test_evilfile: No such file or directory
3254 +ls: bb_test_evilfile: No such file or directory
3255 +symlink/bb_test_evilfile
3256 +" \
3257 +"" "\
3258 +begin-base64 644 tar_symlink_attack.tar.bz2
3259 +QlpoOTFBWSZTWZgs7bQAALT/hMmQAFBAAf+AEMAGJPPv32AAAIAIMAC5thlR
3260 +omAjAmCMADQT1BqNE0AEwAAjAEwElTKeo9NTR6h6gaeoA0DQNLVdwZZ5iNTk
3261 +AQwCAV6S00QFJYhrlfFkVCEDEGtgNVqYrI0uK3ggnt30gqk4e1TTQm5QIAKa
3262 +SJqzRGSFLMmOloHSAcvLiFxxRiQtQZF+qPxbo173ZDISOAoNoPN4PQPhBhKS
3263 +n8fYaKlioCTzL2oXYczyUUIP4u5IpwoSEwWdtoA=
3264 +====
3266 +SKIP=
3267 +cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
3269 +exit $FAILCOUNT