increase the version number of Dragora
[dragora.git] / patches / busybox / busybox-1.27.0_lzip-0.patch
blobafb7d868a223c6bca0cad5d032e73e96a5a49abe
1 diff -urdN busybox-1.27.0.old/archival/Config.src busybox-1.27.0.new/archival/Config.src
2 --- busybox-1.27.0.old/archival/Config.src 2017-01-10 16:31:40.000000000 +0100
3 +++ busybox-1.27.0.new/archival/Config.src 2017-07-07 01:00:19.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.27.0.old/archival/bbunzip.c busybox-1.27.0.new/archival/bbunzip.c
16 --- busybox-1.27.0.old/archival/bbunzip.c 2017-07-01 22:51:12.000000000 +0200
17 +++ busybox-1.27.0.new/archival/bbunzip.c 2017-07-07 16:17:12.000000000 +0200
18 @@ -202,18 +202,31 @@
20 #if ENABLE_UNCOMPRESS \
21 || ENABLE_FEATURE_BZIP2_DECOMPRESS \
22 + || ENABLE_LUNZIP \
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 @@ -456,6 +469,37 @@
54 #endif
57 +//config:config LUNZIP
58 +//config: bool "lunzip"
59 +//config: default y
60 +//config: help
61 +//config: lunzip is used to decompress archives created by lzip.
62 +//config: You can use the '-t' option to test the integrity of an archive,
63 +//config: without decompressing it.
65 +//applet:IF_LUNZIP(APPLET(lunzip, BB_DIR_USR_BIN, BB_SUID_DROP))
66 +//kbuild:lib-$(CONFIG_LUNZIP) += bbunzip.o
68 +//usage:#define lunzip_trivial_usage
69 +//usage: "[-cft] [FILE]..."
70 +//usage:#define lunzip_full_usage "\n\n"
71 +//usage: "Decompress FILEs (or stdin)\n"
72 +//usage: "\n -c Write to stdout"
73 +//usage: "\n -f Force"
74 +//usage: "\n -k Keep input files"
75 +//usage: "\n -t Test file integrity"
76 +#if ENABLE_LUNZIP
77 +int lunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 +int lunzip_main(int argc UNUSED_PARAM, char **argv)
80 + getopt32(argv, "cfkvqdt");
81 + argv += optind;
83 + return bbunpack(argv, unpack_lz_stream, make_new_name_generic, "lz");
85 +#endif
89 * Small lzma deflate implementation.
90 diff -urdN busybox-1.27.0.old/archival/dpkg.c busybox-1.27.0.new/archival/dpkg.c
91 --- busybox-1.27.0.old/archival/dpkg.c 2017-07-01 22:51:12.000000000 +0200
92 +++ busybox-1.27.0.new/archival/dpkg.c 2017-07-07 00:24:37.000000000 +0200
93 @@ -1482,6 +1482,9 @@
94 #if ENABLE_FEATURE_SEAMLESS_XZ
95 llist_add_to(&(ar_handle->accept), (char*)"control.tar.xz");
96 #endif
97 +#if ENABLE_FEATURE_SEAMLESS_LZ
98 + llist_add_to(&(ar_handle->accept), (char*)"control.tar.lz");
99 +#endif
101 /* Assign the tar handle as a subarchive of the ar handle */
102 ar_handle->dpkg__sub_archive = tar_handle;
103 @@ -1509,6 +1512,9 @@
104 #if ENABLE_FEATURE_SEAMLESS_XZ
105 llist_add_to(&(ar_handle->accept), (char*)"data.tar.xz");
106 #endif
107 +#if ENABLE_FEATURE_SEAMLESS_LZ
108 + llist_add_to(&(ar_handle->accept), (char*)"data.tar.lz");
109 +#endif
111 /* Assign the tar handle as a subarchive of the ar handle */
112 ar_handle->dpkg__sub_archive = tar_handle;
113 diff -urdN busybox-1.27.0.old/archival/dpkg_deb.c busybox-1.27.0.new/archival/dpkg_deb.c
114 --- busybox-1.27.0.old/archival/dpkg_deb.c 2016-12-23 13:52:46.000000000 +0100
115 +++ busybox-1.27.0.new/archival/dpkg_deb.c 2017-07-07 00:24:37.000000000 +0200
116 @@ -70,6 +70,10 @@
117 llist_add_to(&ar_archive->accept, (char*)"data.tar.bz2");
118 llist_add_to(&control_tar_llist, (char*)"control.tar.bz2");
119 #endif
120 +#if ENABLE_FEATURE_SEAMLESS_LZ
121 + llist_add_to(&ar_archive->accept, (char*)"data.tar.lz");
122 + llist_add_to(&control_tar_llist, (char*)"control.tar.lz");
123 +#endif
124 #if ENABLE_FEATURE_SEAMLESS_LZMA
125 llist_add_to(&ar_archive->accept, (char*)"data.tar.lzma");
126 llist_add_to(&control_tar_llist, (char*)"control.tar.lzma");
127 diff -urdN busybox-1.27.0.old/archival/libarchive/Kbuild.src busybox-1.27.0.new/archival/libarchive/Kbuild.src
128 --- busybox-1.27.0.old/archival/libarchive/Kbuild.src 2017-04-05 18:27:36.000000000 +0200
129 +++ busybox-1.27.0.new/archival/libarchive/Kbuild.src 2017-07-07 01:00:19.000000000 +0200
130 @@ -35,6 +35,7 @@
131 get_header_tar.o \
132 get_header_tar_gz.o \
133 get_header_tar_bz2.o \
134 + get_header_tar_lz.o \
135 get_header_tar_lzma.o \
136 get_header_tar_xz.o \
138 @@ -54,6 +55,7 @@
139 # 'bzip2 -d', bunzip2 or bzcat selects FEATURE_BZIP2_DECOMPRESS
140 lib-$(CONFIG_FEATURE_BZIP2_DECOMPRESS) += open_transformer.o decompress_bunzip2.o
141 lib-$(CONFIG_FEATURE_UNZIP_BZIP2) += open_transformer.o decompress_bunzip2.o
142 +lib-$(CONFIG_LUNZIP) += open_transformer.o decompress_lunzip.o
143 lib-$(CONFIG_UNLZMA) += open_transformer.o decompress_unlzma.o
144 lib-$(CONFIG_LZCAT) += open_transformer.o decompress_unlzma.o
145 lib-$(CONFIG_LZMA) += open_transformer.o decompress_unlzma.o
146 @@ -70,6 +72,7 @@
147 lib-$(CONFIG_RPM) += open_transformer.o decompress_gunzip.o get_header_cpio.o
148 lib-$(CONFIG_GZIP) += open_transformer.o
149 lib-$(CONFIG_BZIP2) += open_transformer.o
150 +lib-$(CONFIG_LZIP) += open_transformer.o
151 lib-$(CONFIG_LZOP) += open_transformer.o
152 lib-$(CONFIG_MAN) += open_transformer.o
153 lib-$(CONFIG_SETFONT) += open_transformer.o
154 @@ -85,6 +88,7 @@
155 lib-$(CONFIG_FEATURE_SEAMLESS_Z) += open_transformer.o decompress_uncompress.o
156 lib-$(CONFIG_FEATURE_SEAMLESS_GZ) += open_transformer.o decompress_gunzip.o
157 lib-$(CONFIG_FEATURE_SEAMLESS_BZ2) += open_transformer.o decompress_bunzip2.o
158 +lib-$(CONFIG_FEATURE_SEAMLESS_LZ) += open_transformer.o decompress_lunzip.o
159 lib-$(CONFIG_FEATURE_SEAMLESS_LZMA) += open_transformer.o decompress_unlzma.o
160 lib-$(CONFIG_FEATURE_SEAMLESS_XZ) += open_transformer.o decompress_unxz.o
161 lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += open_transformer.o decompress_bunzip2.o
162 diff -urdN busybox-1.27.0.old/archival/libarchive/decompress_lunzip.c busybox-1.27.0.new/archival/libarchive/decompress_lunzip.c
163 --- busybox-1.27.0.old/archival/libarchive/decompress_lunzip.c 1970-01-01 01:00:00.000000000 +0100
164 +++ busybox-1.27.0.new/archival/libarchive/decompress_lunzip.c 2017-07-07 00:24:37.000000000 +0200
165 @@ -0,0 +1,502 @@
167 + * lunzip implementation for busybox
169 + * Copyright (C) 2012-2016 Antonio Diaz Diaz.
171 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
172 + */
174 +#include "libbb.h"
175 +#include "bb_archive.h"
176 +#include "lzip.h"
178 +/* Some functions have been marked with __always_inline because xz does
179 + it, giving the impression that unxz is much faster than lunzip. */
180 +#ifndef __always_inline
181 +# ifdef __GNUC__
182 +# define __always_inline \
183 + inline __attribute__((__always_inline__))
184 +# else
185 +# define __always_inline inline
186 +# endif
187 +#endif
190 +enum { rd_buffer_size = 16384 };
192 +struct Range_decoder {
193 + unsigned long long partial_member_pos;
194 + uint8_t *buffer; /* input buffer */
195 + int pos; /* current pos in buffer */
196 + int stream_pos; /* when reached, a new block must be read */
197 + uint32_t code;
198 + uint32_t range;
199 + int infd; /* input file descriptor */
200 + bool at_stream_end;
204 +static bool Rd_read_block(struct Range_decoder *const rdec)
206 + if (!rdec->at_stream_end) {
207 + rdec->stream_pos =
208 + full_read(rdec->infd, rdec->buffer, rd_buffer_size);
209 + rdec->at_stream_end = (rdec->stream_pos < rd_buffer_size);
210 + rdec->partial_member_pos += rdec->pos;
211 + rdec->pos = 0;
213 + return rdec->pos < rdec->stream_pos;
217 +static bool Rd_init(struct Range_decoder *const rdec, const int ifd,
218 + const bool magic_skipped)
220 + rdec->partial_member_pos = (magic_skipped ? 4 : 0);
221 + rdec->buffer = (uint8_t *) malloc(rd_buffer_size);
222 + if (!rdec->buffer) return false;
223 + rdec->pos = 0;
224 + rdec->stream_pos = 0;
225 + rdec->code = 0;
226 + rdec->range = 0xFFFFFFFFU;
227 + rdec->infd = ifd;
228 + rdec->at_stream_end = false;
229 + return true;
232 +static __always_inline bool Rd_finished(struct Range_decoder *const rdec)
234 + return rdec->pos >= rdec->stream_pos && !Rd_read_block(rdec);
237 +static inline unsigned long long
238 +Rd_member_position(const struct Range_decoder *const rdec)
240 + return rdec->partial_member_pos + rdec->pos;
243 +static inline void Rd_reset_member_position(struct Range_decoder *const rdec)
245 + rdec->partial_member_pos = 0; rdec->partial_member_pos -= rdec->pos;
248 +static __always_inline uint8_t Rd_get_byte(struct Range_decoder *const rdec)
250 + /* 0xFF avoids decoder error if member is truncated at EOS marker */
251 + if (Rd_finished(rdec)) return 0xFF;
252 + return rdec->buffer[rdec->pos++];
255 +static void Rd_load(struct Range_decoder *const rdec)
257 + int i;
258 + rdec->code = 0;
259 + for (i = 0; i < 5; ++i)
260 + rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
261 + rdec->range = 0xFFFFFFFFU;
264 +static __always_inline void Rd_normalize(struct Range_decoder *const rdec)
266 + if (rdec->range <= 0x00FFFFFFU) {
267 + rdec->range <<= 8;
268 + rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
272 +static uint32_t Rd_decode(struct Range_decoder *const rdec,
273 + const uint32_t num_bits)
275 + uint32_t symbol = 0;
276 + uint32_t i;
277 + for (i = num_bits; i > 0; --i) {
278 + uint32_t mask;
279 + Rd_normalize(rdec);
280 + rdec->range >>= 1;
281 + /* symbol <<= 1; */
282 + /* if(rdec->code >= rdec->range) { rdec->code -= rdec->range; symbol |= 1; } */
283 + mask = 0U - (rdec->code < rdec->range);
284 + rdec->code -= rdec->range;
285 + rdec->code += rdec->range & mask;
286 + symbol = (symbol << 1) + (mask + 1);
288 + return symbol;
291 +static __always_inline uint32_t Rd_decode_bit(struct Range_decoder *const rdec,
292 + Bit_model * const probability)
294 + uint32_t bound;
295 + Rd_normalize(rdec);
296 + bound = (rdec->range >> bit_model_total_bits) * *probability;
297 + if (rdec->code < bound) {
298 + rdec->range = bound;
299 + *probability += (bit_model_total - *probability) >> bit_model_move_bits;
300 + return 0;
301 + } else {
302 + rdec->range -= bound;
303 + rdec->code -= bound;
304 + *probability -= *probability >> bit_model_move_bits;
305 + return 1;
309 +static __always_inline uint32_t Rd_decode_tree(struct Range_decoder *const rdec,
310 + Bit_model bm[], const uint32_t num_bits)
312 + uint32_t symbol = 1;
313 + uint32_t i;
314 + for (i = num_bits; i > 0; --i)
315 + symbol = (symbol << 1) | Rd_decode_bit(rdec, &bm[symbol]);
316 + return symbol - (1 << num_bits);
319 +static __always_inline uint32_t Rd_decode_tree_reversed(struct Range_decoder *const rdec,
320 + Bit_model bm[], const uint32_t num_bits)
322 + uint32_t model = 1;
323 + uint32_t symbol = 0;
324 + uint32_t i;
325 + for (i = 0; i < num_bits; ++i) {
326 + const uint32_t bit = Rd_decode_bit(rdec, &bm[model]);
327 + model = ( model << 1 ) + bit;
328 + symbol |= ( bit << i );
330 + return symbol;
333 +static uint32_t Rd_decode_matched(struct Range_decoder *const rdec,
334 + Bit_model bm[], uint32_t match_byte)
336 + uint32_t symbol = 1;
337 + uint32_t mask = 0x100;
338 + while(true) {
339 + const uint32_t match_bit = (match_byte <<= 1) & mask;
340 + const uint32_t bit = Rd_decode_bit(rdec, &bm[symbol+match_bit+mask]);
341 + symbol = (symbol << 1) + bit;
342 + if (symbol >= 0x100) return symbol & 0xFF;
343 + mask &= ~(match_bit ^ (bit << 8)); /* if( match_bit != bit ) mask = 0; */
347 +static __always_inline uint32_t Rd_decode_len(struct Range_decoder *const rdec,
348 + struct Len_model * const lm,
349 + const int pos_state)
351 + if (Rd_decode_bit(rdec, &lm->choice1) == 0)
352 + return Rd_decode_tree(rdec, lm->bm_low[pos_state], len_low_bits);
353 + if (Rd_decode_bit(rdec, &lm->choice2) == 0)
354 + return len_low_symbols +
355 + Rd_decode_tree(rdec, lm->bm_mid[pos_state], len_mid_bits);
356 + return len_low_symbols + len_mid_symbols +
357 + Rd_decode_tree(rdec, lm->bm_high, len_high_bits);
361 +struct LZ_decoder {
362 + unsigned long long partial_data_pos;
363 + struct Range_decoder *rdec;
364 + uint32_t dictionary_size;
365 + uint8_t *buffer; /* output buffer */
366 + uint32_t pos; /* current pos in buffer */
367 + uint32_t stream_pos; /* first byte not yet written to file */
368 + uint32_t crc;
369 + int outfd; /* output file descriptor */
370 + bool pos_wrapped;
371 + bool write_error;
374 +static void LZd_flush_data(struct LZ_decoder *const d)
376 + if (d->pos > d->stream_pos) {
377 + const int size = d->pos - d->stream_pos;
378 + d->crc = crc32_block_endian0(d->crc, d->buffer + d->stream_pos,
379 + size, global_crc32_table);
380 + if (d->outfd >= 0 && full_write(d->outfd,
381 + d->buffer + d->stream_pos, size) != size)
382 + d->write_error = true;
383 + if (d->pos >= d->dictionary_size) {
384 + d->partial_data_pos += d->pos;
385 + d->pos = 0;
386 + d->pos_wrapped = true;
388 + d->stream_pos = d->pos;
392 +static __always_inline uint8_t LZd_peek_prev(const struct LZ_decoder *const d)
394 + const uint32_t i = ((d->pos > 0) ? d->pos : d->dictionary_size) - 1;
395 + return d->buffer[i];
398 +static __always_inline uint8_t LZd_peek(const struct LZ_decoder *const d,
399 + const uint32_t distance)
401 + uint32_t i = d->pos - distance - 1;
402 + if (d->pos <= distance) i += d->dictionary_size;
403 + return d->buffer[i];
406 +static __always_inline void LZd_put_byte(struct LZ_decoder *const d,
407 + const uint8_t b)
409 + d->buffer[d->pos] = b;
410 + if (++d->pos >= d->dictionary_size) LZd_flush_data(d);
413 +static void LZd_copy_block(struct LZ_decoder *const d,
414 + const uint32_t distance, uint32_t len)
416 + uint32_t i = d->pos - distance - 1;
417 + bool fast;
418 + if (d->pos <= distance) {
419 + i += d->dictionary_size;
420 + fast = (len <= d->dictionary_size - i && len <= i - d->pos);
422 + else
423 + fast = (len < d->dictionary_size - d->pos && len <= d->pos - i);
424 + if( fast ) { /* no wrap, no overlap */
425 + memcpy(d->buffer + d->pos, d->buffer + i, len);
426 + d->pos += len;
427 + } else for (; len > 0; --len) {
428 + d->buffer[d->pos] = d->buffer[i];
429 + if (++d->pos >= d->dictionary_size) LZd_flush_data(d);
430 + if (++i >= d->dictionary_size) i = 0;
434 +static bool LZd_init(struct LZ_decoder *const d,
435 + struct Range_decoder *const rde,
436 + const uint32_t dict_size, const int ofd)
438 + d->partial_data_pos = 0;
439 + d->rdec = rde;
440 + d->dictionary_size = dict_size;
441 + d->buffer = (uint8_t *) malloc(d->dictionary_size);
442 + if (!d->buffer) return false;
443 + d->pos = 0;
444 + d->stream_pos = 0;
445 + d->crc = 0xFFFFFFFFU;
446 + d->outfd = ofd;
447 + d->pos_wrapped = false;
448 + d->write_error = false;
449 + d->buffer[d->dictionary_size - 1] = 0; /* prev_byte of first byte */
450 + return true;
453 +static inline uint32_t LZd_crc(const struct LZ_decoder *const d)
455 + return d->crc ^ 0xFFFFFFFFU;
458 +static __always_inline unsigned long long
459 +LZd_data_position(const struct LZ_decoder *const d)
461 + return d->partial_data_pos + d->pos;
465 +static bool LZd_verify_trailer(struct LZ_decoder *const d)
467 + File_trailer trailer;
468 + int i = 0;
469 + while (i < Ft_size)
470 + trailer[i++] = Rd_get_byte(d->rdec);
472 + return (Ft_get_data_crc(trailer) == LZd_crc(d) &&
473 + Ft_get_data_size(trailer) == LZd_data_position(d) &&
474 + Ft_get_member_size(trailer) == Rd_member_position(d->rdec));
478 +/* Return value: -1 = write error, 0 = OK, 1 = data error. */
479 +static int LZd_decode_member(struct LZ_decoder *const d)
481 + struct Range_decoder * const rdec = d->rdec;
482 + Bit_model bm_literal[1 << literal_context_bits][0x300];
483 + Bit_model bm_match[states][pos_states];
484 + Bit_model bm_rep[states];
485 + Bit_model bm_rep0[states];
486 + Bit_model bm_rep1[states];
487 + Bit_model bm_rep2[states];
488 + Bit_model bm_len[states][pos_states];
489 + Bit_model bm_dis_slot[len_states][1 << dis_slot_bits];
490 + Bit_model bm_dis[modeled_distances-end_dis_model];
491 + Bit_model bm_align[dis_align_size];
492 + struct Len_model match_len_model;
493 + struct Len_model rep_len_model;
494 + uint32_t rep0 = 0; /* rep[0-3] latest four distances */
495 + uint32_t rep1 = 0; /* used for efficient coding of */
496 + uint32_t rep2 = 0; /* repeated distances */
497 + uint32_t rep3 = 0;
498 + State state = 0;
500 + Bm_array_init( bm_literal[0], (1 << literal_context_bits) * 0x300 );
501 + Bm_array_init( bm_match[0], states * pos_states );
502 + Bm_array_init( bm_rep, states );
503 + Bm_array_init( bm_rep0, states );
504 + Bm_array_init( bm_rep1, states );
505 + Bm_array_init( bm_rep2, states );
506 + Bm_array_init( bm_len[0], states * pos_states );
507 + Bm_array_init( bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
508 + Bm_array_init( bm_dis, modeled_distances - end_dis_model );
509 + Bm_array_init( bm_align, dis_align_size );
510 + Lm_init(&match_len_model);
511 + Lm_init(&rep_len_model);
513 + Rd_load(rdec);
514 + while (!Rd_finished(rdec)) {
515 + const int pos_state = LZd_data_position(d) & pos_state_mask;
516 + if (Rd_decode_bit(rdec, &bm_match[state][pos_state]) == 0) {
517 + const uint8_t prev_byte = LZd_peek_prev(d);
518 + if (St_is_char(state)) {
519 + state -= (state < 4) ? state : 3;
520 + LZd_put_byte(d, Rd_decode_tree(rdec,
521 + bm_literal[get_lit_state(prev_byte)], 8));
522 + } else {
523 + state -= (state < 10) ? 3 : 6;
524 + LZd_put_byte(d, Rd_decode_matched(rdec,
525 + bm_literal[get_lit_state(prev_byte)],
526 + LZd_peek(d, rep0)));
528 + } else {
529 + uint32_t len;
530 + if (Rd_decode_bit(rdec, &bm_rep[state]) != 0) {
531 + if (Rd_decode_bit(rdec, &bm_rep0[state]) != 0) {
532 + uint32_t distance;
533 + if (Rd_decode_bit(rdec, &bm_rep1[state]) == 0)
534 + distance = rep1;
535 + else {
536 + if (Rd_decode_bit(rdec, &bm_rep2[state]) == 0)
537 + distance = rep2;
538 + else {
539 + distance = rep3;
540 + rep3 = rep2;
542 + rep2 = rep1;
544 + rep1 = rep0;
545 + rep0 = distance;
546 + } else {
547 + if (Rd_decode_bit(rdec, &bm_len[state][pos_state]) == 0) {
548 + state = St_set_short_rep(state);
549 + LZd_put_byte(d, LZd_peek(d, rep0));
550 + continue;
553 + state = St_set_rep(state);
554 + len = min_match_len + Rd_decode_len(rdec, &rep_len_model, pos_state);
555 + } else {
556 + const uint32_t rep0_saved = rep0;
557 + uint32_t dis_slot;
558 + len = min_match_len + Rd_decode_len(rdec, &match_len_model, pos_state);
559 + dis_slot = Rd_decode_tree(rdec, bm_dis_slot[get_len_state(len)], 6);
560 + if (dis_slot < start_dis_model) rep0 = dis_slot;
561 + else {
562 + const uint32_t direct_bits = (dis_slot >> 1) - 1;
563 + rep0 = (2 | (dis_slot & 1)) << direct_bits;
564 + if (dis_slot < end_dis_model)
565 + rep0 += Rd_decode_tree_reversed(rdec,
566 + bm_dis + rep0 - dis_slot - 1, direct_bits);
567 + else {
568 + rep0 += Rd_decode(rdec, direct_bits - dis_align_bits) << dis_align_bits;
569 + rep0 += Rd_decode_tree_reversed(rdec, bm_align, dis_align_bits);
570 + if (rep0 == 0xFFFFFFFFU) { /* marker found */
571 + rep0 = rep0_saved;
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;
587 + rep2 = rep1;
588 + rep1 = rep0_saved;
589 + state = St_set_match(state);
590 + if (rep0 >= d->dictionary_size ||
591 + (rep0 >= d->pos && !d->pos_wrapped)) {
592 + LZd_flush_data(d);
593 + return 1;
596 + LZd_copy_block(d, rep0, len);
599 + LZd_flush_data(d);
600 + return 1;
604 +IF_DESKTOP(long long) int FAST_FUNC
605 +unpack_lz_stream(transformer_state_t *xstate)
607 + IF_DESKTOP(long long) int total = 0;
608 + struct Range_decoder rdec;
609 + bool first_member;
610 + const bool magic_skipped = (xstate->signature_skipped != 0);
612 + if (!global_crc32_table)
613 + global_crc32_table = crc32_filltable(NULL, 0);
615 + if (!Rd_init(&rdec, xstate->src_fd, magic_skipped))
616 + return -1;
618 + for (first_member = true;; first_member = false) {
619 + int tmp = 0;
620 + File_header header;
621 + struct LZ_decoder decoder;
623 + if (first_member && magic_skipped) {
624 + Fh_set_magic(header);
625 + tmp = 4;
626 + } else {
627 + Rd_reset_member_position(&rdec);
629 + while (tmp < Fh_size)
630 + header[tmp++] = Rd_get_byte(&rdec);
631 + if (Rd_finished(&rdec)) { /* End Of File */
632 + if (first_member) {
633 + bb_error_msg(bb_msg_read_error);
634 + total = -1;
636 + break;
638 + tmp = Fh_get_dictionary_size(header);
639 + if (!Fh_verify_magic(header) || tmp < min_dictionary_size ||
640 + tmp > max_dictionary_size) {
641 + if (!first_member)
642 + break; /* trailing garbage */
643 + bb_error_msg("invalid magic");
644 + total = -1;
645 + break;
648 + if (!LZd_init(&decoder, &rdec, tmp, xstate->dst_fd)) {
649 + bb_error_msg(bb_msg_memory_exhausted);
650 + total = -1;
651 + break;
653 + tmp = LZd_decode_member(&decoder);
654 + IF_DESKTOP(total += Rd_member_position(&rdec);)
655 + free(decoder.buffer);
656 + if (tmp != 0) {
657 + if (tmp < 0)
658 + bb_perror_msg(bb_msg_write_error);
659 + else
660 + bb_error_msg("corrupted data");
661 + total = -1;
662 + break;
665 + free(rdec.buffer);
666 + return total;
668 diff -urdN busybox-1.27.0.old/archival/libarchive/filter_accept_list_reassign.c busybox-1.27.0.new/archival/libarchive/filter_accept_list_reassign.c
669 --- busybox-1.27.0.old/archival/libarchive/filter_accept_list_reassign.c 2016-11-27 03:55:16.000000000 +0100
670 +++ busybox-1.27.0.new/archival/libarchive/filter_accept_list_reassign.c 2017-07-07 00:24:37.000000000 +0200
671 @@ -44,6 +44,12 @@
672 archive_handle->dpkg__action_data_subarchive = get_header_tar_bz2;
673 return EXIT_SUCCESS;
675 + if (ENABLE_FEATURE_SEAMLESS_LZ
676 + && strcmp(name_ptr, "lz") == 0
677 + ) {
678 + archive_handle->dpkg__action_data_subarchive = get_header_tar_lz;
679 + return EXIT_SUCCESS;
681 if (ENABLE_FEATURE_SEAMLESS_LZMA
682 && strcmp(name_ptr, "lzma") == 0
684 diff -urdN busybox-1.27.0.old/archival/libarchive/get_header_tar_lz.c busybox-1.27.0.new/archival/libarchive/get_header_tar_lz.c
685 --- busybox-1.27.0.old/archival/libarchive/get_header_tar_lz.c 1970-01-01 01:00:00.000000000 +0100
686 +++ busybox-1.27.0.new/archival/libarchive/get_header_tar_lz.c 2017-07-07 00:24:37.000000000 +0200
687 @@ -0,0 +1,21 @@
688 +/* vi: set sw=4 ts=4: */
690 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
691 + */
693 +#include "libbb.h"
694 +#include "bb_archive.h"
696 +char FAST_FUNC get_header_tar_lz(archive_handle_t *archive_handle)
698 + /* Can't lseek over pipes */
699 + archive_handle->seek = seek_by_read;
701 + fork_transformer_with_sig(archive_handle->src_fd, unpack_lz_stream, "lunzip");
702 + archive_handle->offset = 0;
703 + while (get_header_tar(archive_handle) == EXIT_SUCCESS)
704 + continue;
706 + /* Can only do one file at a time */
707 + return EXIT_FAILURE;
709 diff -urdN busybox-1.27.0.old/archival/libarchive/lzip.h busybox-1.27.0.new/archival/libarchive/lzip.h
710 --- busybox-1.27.0.old/archival/libarchive/lzip.h 1970-01-01 01:00:00.000000000 +0100
711 +++ busybox-1.27.0.new/archival/libarchive/lzip.h 2017-07-07 00:24:37.000000000 +0200
712 @@ -0,0 +1,250 @@
713 +/* Lzip - LZMA lossless data compressor
714 + Copyright (C) 2008-2016 Antonio Diaz Diaz.
716 + This program is free software: you can redistribute it and/or modify
717 + it under the terms of the GNU General Public License as published by
718 + the Free Software Foundation, either version 2 of the License, or
719 + (at your option) any later version.
721 + This program is distributed in the hope that it will be useful,
722 + but WITHOUT ANY WARRANTY; without even the implied warranty of
723 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
724 + GNU General Public License for more details.
726 + You should have received a copy of the GNU General Public License
727 + along with this program. If not, see <http://www.gnu.org/licenses/>.
730 +typedef int State;
732 +enum { states = 12 };
734 +static inline bool St_is_char(const State st) { return st < 7; }
736 +static inline State St_set_char(const State st)
738 + static const State next[states] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
739 + return next[st];
742 +static inline State St_set_match(const State st)
744 + return ((st < 7) ? 7 : 10);
747 +static inline State St_set_rep(const State st)
749 + return ((st < 7) ? 8 : 11);
752 +static inline State St_set_short_rep(const State st)
754 + return ((st < 7) ? 9 : 11);
758 +enum {
759 + min_dictionary_bits = 12,
760 + min_dictionary_size = 1 << min_dictionary_bits, /* >= modeled_distances */
761 + max_dictionary_bits = 29,
762 + max_dictionary_size = 1 << max_dictionary_bits,
763 + literal_context_bits = 3,
764 + pos_state_bits = 2,
765 + pos_states = 1 << pos_state_bits,
766 + pos_state_mask = pos_states - 1,
768 + len_states = 4,
769 + dis_slot_bits = 6,
770 + start_dis_model = 4,
771 + end_dis_model = 14,
772 + modeled_distances = 1 << (end_dis_model / 2), /* 128 */
773 + dis_align_bits = 4,
774 + dis_align_size = 1 << dis_align_bits,
776 + len_low_bits = 3,
777 + len_mid_bits = 3,
778 + len_high_bits = 8,
779 + len_low_symbols = 1 << len_low_bits,
780 + len_mid_symbols = 1 << len_mid_bits,
781 + len_high_symbols = 1 << len_high_bits,
782 + max_len_symbols = len_low_symbols + len_mid_symbols + len_high_symbols,
784 + min_match_len = 2, /* must be 2 */
785 + max_match_len = min_match_len + max_len_symbols - 1, /* 273 */
786 + min_match_len_limit = 5,
788 + lz_num_models =
789 + ((1 << literal_context_bits) * 0x300) +
790 + (2 * states * pos_states) +
791 + (4 * states) +
792 + (len_states * (1 << dis_slot_bits)) +
793 + (modeled_distances - end_dis_model) +
794 + dis_align_size,
797 +static inline int get_len_state(const int len)
799 + return MIN(len - min_match_len, len_states - 1);
802 +static inline int get_lit_state(const uint8_t prev_byte)
804 + return (prev_byte >> (8 - literal_context_bits));
808 +enum { bit_model_move_bits = 5,
809 + bit_model_total_bits = 11,
810 + bit_model_total = 1 << bit_model_total_bits
813 +typedef int Bit_model;
815 +static inline void Bm_init(Bit_model * const probability)
817 + *probability = bit_model_total / 2;
820 +static inline void Bm_array_init(Bit_model * const p, const int size)
822 + int i = 0;
823 + while (i < size)
824 + p[i++] = bit_model_total / 2;
827 +struct Len_model {
828 + Bit_model choice1;
829 + Bit_model choice2;
830 + Bit_model bm_low[pos_states][len_low_symbols];
831 + Bit_model bm_mid[pos_states][len_mid_symbols];
832 + Bit_model bm_high[len_high_symbols];
835 +static inline void Lm_init(struct Len_model * const lm)
837 + Bm_init(&lm->choice1);
838 + Bm_init(&lm->choice2);
839 + Bm_array_init(lm->bm_low[0], pos_states * len_low_symbols);
840 + Bm_array_init(lm->bm_mid[0], pos_states * len_mid_symbols);
841 + Bm_array_init(lm->bm_high, len_high_symbols);
845 +static inline int real_bits(unsigned value)
847 + int bits = 0;
848 + while(value > 0) { value >>= 1; ++bits; }
849 + return bits;
853 +static const uint8_t magic_string[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
855 +typedef uint8_t File_header[6]; /* 0-3 magic bytes */
856 + /* 4 version */
857 + /* 5 coded_dict_size */
858 +enum { Fh_size = 6 };
860 +static inline void Fh_set_magic(File_header data)
862 + memcpy(data, magic_string, 4);
863 + data[4] = 1;
866 +static inline bool Fh_verify_magic(const File_header data)
868 + return (memcmp(data, magic_string, 4) == 0 && data[4] == 1);
871 +static inline unsigned Fh_get_dictionary_size(const File_header data)
873 + unsigned sz = (1 << (data[5] & 0x1F));
874 + if (sz > min_dictionary_size)
875 + sz -= (sz / 16) * ((data[5] >> 5) & 7);
876 + return sz;
879 +static inline bool Fh_set_dictionary_size(File_header data, const unsigned sz)
881 + if (sz < min_dictionary_size || sz > max_dictionary_size) return false;
882 + data[5] = real_bits(sz - 1);
883 + if (sz > min_dictionary_size) {
884 + const unsigned base_size = 1 << data[5];
885 + const unsigned fraction = base_size / 16;
886 + unsigned i;
887 + for (i = 7; i >= 1; --i)
888 + if (base_size - (i * fraction) >= sz) {
889 + data[5] |= (i << 5);
890 + break;
893 + return true;
897 +typedef uint8_t File_trailer[20];
898 + /* 0-3 CRC32 of the uncompressed data */
899 + /* 4-11 size of the uncompressed data */
900 + /* 12-19 member size including header and trailer */
902 +enum { Ft_size = 20 };
904 +static inline unsigned Ft_get_data_crc(const File_trailer data)
906 + unsigned tmp = 0;
907 + int i;
908 + for (i = 3; i >= 0; --i) {
909 + tmp <<= 8;
910 + tmp += data[i];
912 + return tmp;
915 +static inline void Ft_set_data_crc(File_trailer data, unsigned crc)
917 + int i;
918 + for (i = 0; i <= 3; ++i) {
919 + data[i] = (uint8_t)crc;
920 + crc >>= 8;
924 +static inline unsigned long long Ft_get_data_size(const File_trailer data)
926 + unsigned long long tmp = 0;
927 + int i;
928 + for (i = 11; i >= 4; --i) {
929 + tmp <<= 8;
930 + tmp += data[i];
932 + return tmp;
935 +static inline void Ft_set_data_size(File_trailer data, unsigned long long sz)
937 + int i;
938 + for (i = 4; i <= 11; ++i) {
939 + data[i] = (uint8_t)sz;
940 + sz >>= 8;
944 +static inline unsigned long long Ft_get_member_size(const File_trailer data)
946 + unsigned long long tmp = 0;
947 + int i;
948 + for (i = 19; i >= 12; --i) {
949 + tmp <<= 8;
950 + tmp += data[i];
952 + return tmp;
955 +static inline void Ft_set_member_size(File_trailer data, unsigned long long sz)
957 + int i;
958 + for (i = 12; i <= 19; ++i) {
959 + data[i] = (uint8_t)sz;
960 + sz >>= 8;
963 diff -urdN busybox-1.27.0.old/archival/libarchive/open_transformer.c busybox-1.27.0.new/archival/libarchive/open_transformer.c
964 --- busybox-1.27.0.old/archival/libarchive/open_transformer.c 2016-11-27 03:55:16.000000000 +0100
965 +++ busybox-1.27.0.new/archival/libarchive/open_transformer.c 2017-07-07 00:24:37.000000000 +0200
966 @@ -195,6 +195,17 @@
967 USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
968 goto found_magic;
970 + if (ENABLE_FEATURE_SEAMLESS_LZ
971 + && magic.b16[0] == LZIP_MAGIC1
972 + ) {
973 + xstate->signature_skipped = 4;
974 + xread(fd, magic.b16, sizeof(magic.b16[0]));
975 + if (magic.b16[0] == LZIP_MAGIC2) {
976 + xstate->xformer = unpack_lz_stream;
977 + USE_FOR_NOMMU(xstate->xformer_prog = "lunzip";)
978 + goto found_magic;
981 if (ENABLE_FEATURE_SEAMLESS_XZ
982 && magic.b16[0] == XZ_MAGIC1
984 @@ -211,6 +222,7 @@
985 if (fail_if_not_compressed)
986 bb_error_msg_and_die("no gzip"
987 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
988 + IF_FEATURE_SEAMLESS_LZ("/lzip")
989 IF_FEATURE_SEAMLESS_XZ("/xz")
990 " magic");
992 diff -urdN busybox-1.27.0.old/archival/lzip.c busybox-1.27.0.new/archival/lzip.c
993 --- busybox-1.27.0.old/archival/lzip.c 1970-01-01 01:00:00.000000000 +0100
994 +++ busybox-1.27.0.new/archival/lzip.c 2017-07-07 16:49:00.000000000 +0200
995 @@ -0,0 +1,1434 @@
997 + * lzip implementation for busybox
999 + * Copyright (C) 2012-2016 Antonio Diaz Diaz.
1001 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
1002 + */
1004 +//config:config LZIP
1005 +//config: bool "lzip"
1006 +//config: default y
1007 +//config: help
1008 +//config: lzip is a lossless data compressor with a user interface similar to
1009 +//config: the one of gzip or bzip2. lzip can compress about as fast as gzip or
1010 +//config: compress most files more than bzip2 (depending on compression level).
1011 +//config: Decompression speed is intermediate between gzip and bzip2. lzip is
1012 +//config: better than gzip and bzip2 from a data recovery perspective.
1014 +//applet:IF_LZIP(APPLET(lzip, BB_DIR_USR_BIN, BB_SUID_DROP))
1015 +//kbuild:lib-$(CONFIG_LZIP) += lzip.o bbunzip.o
1017 +//usage:#define lzip_trivial_usage
1018 +//usage: "[-123456789c"
1019 +//usage: IF_LUNZIP("d") "fk"
1020 +//usage: IF_LUNZIP("t")
1021 +//usage: "] [-m MATCH_LENGTH] [-s DICT_SIZE] [FILE]..."
1022 +//usage:#define lzip_full_usage "\n\n"
1023 +//usage: "Compress FILEs (or stdin) with lzip algorithm\n"
1024 +//usage: "\n -1..9 Compression level"
1025 +//usage: "\n -c Write to stdout"
1026 +//usage: IF_LUNZIP("\n -d Decompress")
1027 +//usage: "\n -f Force"
1028 +//usage: "\n -k Keep input files"
1029 +//usage: "\n -m Match length limit [36]"
1030 +//usage: "\n -s Dictionary size limit [8MiB]"
1031 +//usage: IF_LUNZIP("\n -t Test compressed file integrity")
1034 +#include "libbb.h"
1035 +#include "bb_archive.h"
1036 +#include "libarchive/lzip.h"
1039 +#if CHAR_BIT != 8
1040 +#error "Environments where CHAR_BIT != 8 are not supported."
1041 +#endif
1044 +static void CRC32_update_byte(uint32_t * crc, const uint8_t byte)
1046 + *crc = global_crc32_table[(*crc ^ byte) & 0xFF] ^ (*crc >> 8);
1050 +enum { max_num_trials = 1 << 12,
1051 + price_shift_bits = 6
1055 +static uint8_t * dis_slots;
1057 +static void Dis_slots_init(void)
1059 + int i, size, slot;
1060 + dis_slots = xmalloc((1 << 10) * sizeof dis_slots[0]);
1062 + for (slot = 0; slot < 4; ++slot) dis_slots[slot] = slot;
1063 + for (i = 4, size = 2, slot = 4; slot < 20; slot += 2) {
1064 + memset(&dis_slots[i], slot, size);
1065 + memset(&dis_slots[i + size], slot + 1, size);
1066 + size <<= 1;
1067 + i += size;
1071 +static uint8_t get_slot(const unsigned dis)
1073 + if (dis < (1 << 10)) return dis_slots[dis];
1074 + if (dis < (1 << 19)) return dis_slots[dis>> 9] + 18;
1075 + if (dis < (1 << 28)) return dis_slots[dis>>18] + 36;
1076 + return dis_slots[dis>>27] + 54;
1080 +static int * prob_prices;
1082 +static void Prob_prices_init(void)
1084 + const int num_bits = (bit_model_total_bits - 2);
1085 + int i, j = 1, end = 2;
1086 + prob_prices = xmalloc((bit_model_total >> 2) * sizeof prob_prices[0]);
1088 + prob_prices[0] = bit_model_total_bits << price_shift_bits;
1089 + for (i = num_bits - 1; i >= 0; --i, end <<= 1) {
1090 + for (; j < end; ++j)
1091 + prob_prices[j] = (i << price_shift_bits) +
1092 + (((end - j) << price_shift_bits) >> (num_bits - i - 1));
1096 +static inline int get_price(const int probability)
1098 + return prob_prices[probability >> 2];
1102 +static inline int price0(const Bit_model probability)
1104 + return get_price(probability);
1107 +static inline int price1(const Bit_model probability)
1109 + return get_price(bit_model_total - probability);
1112 +static int price_bit(const Bit_model bm, const int bit)
1114 + if (bit) return price1(bm);
1115 + else return price0(bm);
1119 +static int price_symbol(const Bit_model bm[], int symbol,
1120 + const int num_bits)
1122 + int price = 0;
1123 + symbol |= (1 << num_bits);
1124 + while (symbol > 1) {
1125 + const int bit = symbol & 1;
1126 + symbol >>= 1;
1127 + price += price_bit(bm[symbol], bit);
1129 + return price;
1133 +static int price_symbol_reversed(const Bit_model bm[], int symbol,
1134 + const int num_bits)
1136 + int price = 0;
1137 + int model = 1;
1138 + int i;
1139 + for (i = num_bits; i > 0; --i) {
1140 + const int bit = symbol & 1;
1141 + price += price_bit(bm[model], bit);
1142 + model = (model << 1) | bit;
1143 + symbol >>= 1;
1145 + return price;
1149 +static int price_matched(const Bit_model bm[], int symbol, int match_byte)
1151 + int price = 0;
1152 + int mask = 0x100;
1153 + symbol |= mask;
1155 + do {
1156 + int match_bit, bit;
1157 + match_byte <<= 1;
1158 + match_bit = match_byte & mask;
1159 + symbol <<= 1;
1160 + bit = symbol & 0x100;
1161 + price += price_bit( bm[match_bit+(symbol>>9)+mask], bit );
1162 + mask &= ~(match_byte ^ symbol); /* if( match_bit != bit ) mask = 0; */
1164 + while( symbol < 0x10000 );
1165 + return price;
1169 +enum { /* bytes to keep in buffer before dictionary */
1170 + before_size = max_num_trials + 1,
1171 + /* bytes to keep in buffer after pos */
1172 + after_size = max_match_len,
1173 + num_prev_positions4 = 1 << 20,
1174 + num_prev_positions3 = 1 << 18,
1175 + num_prev_positions2 = 1 << 16,
1176 + num_prev_positions = num_prev_positions4 + num_prev_positions3 +
1177 + num_prev_positions2
1180 +struct Matchfinder {
1181 + unsigned long long partial_data_pos;
1182 + uint8_t *buffer; /* input buffer */
1183 + int32_t *prev_positions; /* last seen position of key */
1184 + int32_t *prev_pos_tree; /* previous positions of key */
1185 + int match_len_limit;
1186 + int buffer_size;
1187 + int dictionary_size; /* bytes to keep in buffer before pos */
1188 + int pos; /* current pos in buffer */
1189 + int cyclic_pos; /* current pos in dictionary */
1190 + int stream_pos; /* first byte not yet read from file */
1191 + int pos_limit; /* when reached, a new block must be read */
1192 + int cycles;
1193 + bool at_stream_end; /* stream_pos shows real end of file */
1196 +static bool Mf_read_block(struct Matchfinder *const mf)
1198 + if (!mf->at_stream_end && mf->stream_pos < mf->buffer_size) {
1199 + const int size = mf->buffer_size - mf->stream_pos;
1200 + const int rd = full_read(STDIN_FILENO,
1201 + mf->buffer + mf->stream_pos, size);
1202 + mf->stream_pos += rd;
1203 + if (rd < size) {
1204 + mf->at_stream_end = true;
1205 + mf->pos_limit = mf->buffer_size;
1208 + return mf->pos < mf->stream_pos;
1211 +static void Mf_normalize_pos(struct Matchfinder *const mf)
1213 + if (!mf->at_stream_end) {
1214 + int i;
1215 + const int offset = mf->pos - mf->dictionary_size - before_size;
1216 + const int size = mf->stream_pos - offset;
1217 + memmove(mf->buffer, mf->buffer + offset, size);
1218 + mf->partial_data_pos += offset;
1219 + mf->pos -= offset;
1220 + mf->stream_pos -= offset;
1221 + for (i = 0; i < num_prev_positions; ++i)
1222 + if (mf->prev_positions[i] >= 0)
1223 + mf->prev_positions[i] -= offset;
1224 + for (i = 0; i < 2 * mf->dictionary_size; ++i)
1225 + if (mf->prev_pos_tree[i] >= 0)
1226 + mf->prev_pos_tree[i] -= offset;
1227 + Mf_read_block(mf);
1231 +static bool Mf_init(struct Matchfinder *const mf, const int dict_size,
1232 + const int match_len_limit)
1234 + const int buffer_size_limit = (2 * dict_size) + before_size + after_size;
1235 + int i;
1237 + mf->partial_data_pos = 0;
1238 + mf->match_len_limit = match_len_limit;
1239 + mf->prev_positions = (int32_t *) malloc(num_prev_positions * sizeof(int32_t));
1240 + if (!mf->prev_positions) return false;
1241 + mf->pos = 0;
1242 + mf->cyclic_pos = 0;
1243 + mf->stream_pos = 0;
1244 + mf->cycles = (match_len_limit < max_match_len) ?
1245 + 16 + (match_len_limit / 2) : 256;
1246 + mf->at_stream_end = false;
1248 + for (i = 0; i < num_prev_positions; ++i)
1249 + mf->prev_positions[i] = -1;
1250 + mf->buffer_size = MAX(65536, dict_size);
1251 + mf->buffer = (uint8_t *) malloc(mf->buffer_size);
1252 + if (!mf->buffer) {
1253 + free(mf->prev_positions);
1254 + return false;
1256 + if (Mf_read_block(mf) && !mf->at_stream_end &&
1257 + mf->buffer_size < buffer_size_limit) {
1258 + uint8_t *tmp;
1259 + mf->buffer_size = buffer_size_limit;
1260 + tmp = (uint8_t *) realloc(mf->buffer, mf->buffer_size);
1261 + if (!tmp) {
1262 + free(mf->buffer);
1263 + free(mf->prev_positions);
1264 + return false;
1266 + mf->buffer = tmp;
1267 + Mf_read_block(mf);
1269 + if (mf->at_stream_end && mf->stream_pos < dict_size)
1270 + mf->dictionary_size = MAX(min_dictionary_size, mf->stream_pos);
1271 + else
1272 + mf->dictionary_size = dict_size;
1273 + mf->pos_limit = mf->buffer_size;
1274 + if (!mf->at_stream_end) mf->pos_limit -= after_size;
1275 + mf->prev_pos_tree =
1276 + (int32_t *) malloc(2 * mf->dictionary_size * sizeof(int32_t));
1277 + if (!mf->prev_pos_tree) {
1278 + free(mf->buffer);
1279 + free(mf->prev_positions);
1280 + return false;
1282 + return true;
1285 +static void Mf_free(struct Matchfinder *const mf)
1287 + free(mf->prev_pos_tree);
1288 + free(mf->buffer);
1289 + free(mf->prev_positions);
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, int len_limit)
1323 + const uint8_t *const data = mf->buffer + mf->pos + index;
1324 + int i = 0;
1325 + if (index + len_limit > Mf_available_bytes(mf))
1326 + len_limit = Mf_available_bytes(mf) - index;
1327 + while (i < len_limit && data[i - distance] == data[i]) ++i;
1328 + return i;
1331 +static void Mf_move_pos(struct Matchfinder *const mf)
1333 + if (++mf->cyclic_pos >= mf->dictionary_size) mf->cyclic_pos = 0;
1334 + if (++mf->pos >= mf->pos_limit) Mf_normalize_pos(mf);
1337 +static int Mf_longest_match_len(struct Matchfinder *const mf,
1338 + int *const distances)
1340 + int32_t *ptr0 = mf->prev_pos_tree + (mf->cyclic_pos << 1);
1341 + int32_t *ptr1 = ptr0 + 1;
1342 + int32_t *newptr;
1343 + const uint8_t *newdata;
1344 + int len = 0, len0 = 0, len1 = 0;
1345 + int maxlen = min_match_len - 1;
1346 + const int min_pos = (mf->pos >= mf->dictionary_size) ?
1347 + (mf->pos - mf->dictionary_size + 1) : 0;
1348 + const uint8_t *const data = mf->buffer + mf->pos;
1349 + int count, delta, key2, key3, key4, newpos, 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 + key2 = num_prev_positions4 + num_prev_positions3 +
1358 + (((int) data[0] << 8) | data[1]);
1359 + tmp = global_crc32_table[data[0]] ^ data[1] ^ ((uint32_t) data[2] << 8);
1360 + key3 = num_prev_positions4 + (int) (tmp & (num_prev_positions3 - 1));
1361 + key4 = (int) ((tmp ^ (global_crc32_table[data[3]] << 5)) &
1362 + (num_prev_positions4 - 1));
1364 + if (distances) {
1365 + int np = mf->prev_positions[key2];
1366 + if (np >= min_pos) {
1367 + distances[2] = mf->pos - np - 1;
1368 + maxlen = 2;
1369 + } else
1370 + distances[2] = 0x7FFFFFFF;
1371 + np = mf->prev_positions[key3];
1372 + if (np >= min_pos && mf->buffer[np] == data[0]) {
1373 + distances[3] = mf->pos - np - 1;
1374 + maxlen = 3;
1375 + } else
1376 + distances[3] = 0x7FFFFFFF;
1377 + distances[4] = 0x7FFFFFFF;
1380 + mf->prev_positions[key2] = mf->pos;
1381 + mf->prev_positions[key3] = mf->pos;
1382 + newpos = mf->prev_positions[key4];
1383 + mf->prev_positions[key4] = mf->pos;
1385 + for (count = mf->cycles;;) {
1386 + if (newpos < min_pos || --count < 0) {
1387 + *ptr0 = *ptr1 = -1;
1388 + break;
1390 + newdata = mf->buffer + newpos;
1391 + while (len < len_limit && newdata[len] == data[len]) ++len;
1393 + delta = mf->pos - newpos;
1394 + if (distances)
1395 + while (maxlen < len)
1396 + distances[++maxlen] = delta - 1;
1398 + newptr = mf->prev_pos_tree +
1399 + ((mf->cyclic_pos - delta +
1400 + ((mf->cyclic_pos >= delta) ? 0 : mf->dictionary_size)) << 1);
1402 + if (len < len_limit) {
1403 + if (newdata[len] < data[len]) {
1404 + *ptr0 = newpos;
1405 + ptr0 = newptr + 1;
1406 + newpos = *ptr0;
1407 + len0 = len;
1408 + if (len1 < len) len = len1;
1409 + } else {
1410 + *ptr1 = newpos;
1411 + ptr1 = newptr;
1412 + newpos = *ptr1;
1413 + len1 = len;
1414 + if (len0 < len) len = len0;
1416 + } else {
1417 + *ptr0 = newptr[0];
1418 + *ptr1 = newptr[1];
1419 + break;
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 = 16384 };
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 + const bool carry = (renc->low > 0xFFFFFFFFU);
1464 + if (carry || renc->low < 0xFF000000U) {
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 + int i;
1509 + for (i = num_bits - 1; i >= 0; --i) {
1510 + renc->range >>= 1;
1511 + if ((symbol >> i) & 1) 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 int 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 mask = (1 << (num_bits - 1));
1542 + int model = 1;
1543 + int i;
1544 + for (i = num_bits; i > 0; --i, mask >>= 1) {
1545 + const int bit = (symbol & mask);
1546 + Re_encode_bit(renc, &bm[model], bit);
1547 + model <<= 1;
1548 + if (bit) model |= 1;
1552 +static void Re_encode_tree_reversed(struct Range_encoder *const renc,
1553 + Bit_model bm[], int symbol, const int num_bits)
1555 + int model = 1;
1556 + int i;
1557 + for (i = num_bits; i > 0; --i) {
1558 + const int bit = symbol & 1;
1559 + Re_encode_bit(renc, &bm[model], bit);
1560 + model = (model << 1) | bit;
1561 + symbol >>= 1;
1565 +static void Re_encode_matched(struct Range_encoder *const renc,
1566 + Bit_model bm[], int symbol, int match_byte)
1568 + int mask = 0x100;
1569 + symbol |= mask;
1571 + do {
1572 + int match_bit, bit;
1573 + match_byte <<= 1;
1574 + match_bit = match_byte & mask;
1575 + symbol <<= 1;
1576 + bit = symbol & 0x100;
1577 + Re_encode_bit( renc, &bm[match_bit+(symbol>>9)+mask], bit );
1578 + mask &= ~(match_byte ^ symbol); /* if( match_bit != bit ) mask = 0; */
1580 + while( symbol < 0x10000 );
1583 +static void Re_encode_len( struct Range_encoder * const renc,
1584 + struct Len_model * const lm,
1585 + int symbol, const int pos_state )
1587 + bool bit = ( ( symbol -= min_match_len ) >= len_low_symbols );
1588 + Re_encode_bit( renc, &lm->choice1, bit );
1589 + if( !bit )
1590 + Re_encode_tree( renc, lm->bm_low[pos_state], symbol, len_low_bits );
1591 + else {
1592 + bit = ( symbol >= len_low_symbols + len_mid_symbols );
1593 + Re_encode_bit( renc, &lm->choice2, bit );
1594 + if( !bit )
1595 + Re_encode_tree( renc, lm->bm_mid[pos_state],
1596 + symbol - len_low_symbols, len_mid_bits );
1597 + else
1598 + Re_encode_tree( renc, lm->bm_high,
1599 + symbol - len_low_symbols - len_mid_symbols, len_high_bits );
1604 +struct Len_encoder {
1605 + struct Len_model lm;
1606 + int len_symbols;
1607 + int prices[pos_states][max_len_symbols];
1608 + int counters[pos_states];
1611 +static void Lee_update_prices(struct Len_encoder *const le, const int pos_state)
1613 + int *const pps = le->prices[pos_state];
1614 + int tmp = price0(le->lm.choice1);
1615 + int len = 0;
1617 + for (; len < len_low_symbols && len < le->len_symbols; ++len)
1618 + pps[len] = tmp +
1619 + price_symbol(le->lm.bm_low[pos_state], len, len_low_bits);
1620 + tmp = price1(le->lm.choice1);
1621 + for (; len < len_low_symbols + len_mid_symbols && len < le->len_symbols; ++len)
1622 + pps[len] = tmp + price0(le->lm.choice2) +
1623 + price_symbol(le->lm.bm_mid[pos_state],
1624 + len - len_low_symbols, len_mid_bits);
1625 + for (; len < le->len_symbols; ++len)
1626 + /* using 4 slots per value makes "Lee_price" faster */
1627 + le->prices[3][len] = le->prices[2][len] =
1628 + le->prices[1][len] = le->prices[0][len] =
1629 + tmp + price1(le->lm.choice2) +
1630 + price_symbol(le->lm.bm_high,
1631 + len - len_low_symbols - len_mid_symbols,
1632 + len_high_bits);
1633 + le->counters[pos_state] = le->len_symbols;
1636 +static void Lee_init(struct Len_encoder *const le, const int len_limit)
1638 + int i;
1639 + Lm_init(&le->lm);
1640 + le->len_symbols = len_limit + 1 - min_match_len;
1641 + for (i = 0; i < pos_states; ++i) Lee_update_prices(le, i);
1644 +static void Lee_encode(struct Len_encoder *const le,
1645 + struct Range_encoder *const renc,
1646 + int symbol, const int pos_state)
1648 + Re_encode_len(renc, &le->lm, symbol, pos_state);
1649 + if (--le->counters[pos_state] <= 0)
1650 + Lee_update_prices(le, pos_state);
1653 +static int Lee_price(const struct Len_encoder *const le,
1654 + const int symbol, const int pos_state)
1656 + return le->prices[pos_state][symbol - min_match_len];
1660 +enum { infinite_price = 0x0FFFFFFF,
1661 + num_rep_distances = 4 /* must be 4 */
1664 +struct Trial {
1665 + State state;
1666 + int price; /* dual use var; cumulative price, match length */
1667 + int dis; /* rep index or match distance. (-1 for literal) */
1668 + int prev_index; /* index of prev trial in trials[] */
1669 + int reps[num_rep_distances];
1672 +static void Tr_update(struct Trial *const trial, const int pr,
1673 + const int distance, const int p_i)
1675 + if (pr < trial->price) {
1676 + trial->price = pr;
1677 + trial->dis = distance;
1678 + trial->prev_index = p_i;
1683 +struct LZ_encoder {
1684 + int longest_match_found;
1685 + uint32_t crc;
1687 + Bit_model bm_literal[1<<literal_context_bits][0x300];
1688 + Bit_model bm_match[states][pos_states];
1689 + Bit_model bm_rep[states];
1690 + Bit_model bm_rep0[states];
1691 + Bit_model bm_rep1[states];
1692 + Bit_model bm_rep2[states];
1693 + Bit_model bm_len[states][pos_states];
1694 + Bit_model bm_dis_slot[len_states][1<<dis_slot_bits];
1695 + Bit_model bm_dis[modeled_distances-end_dis_model];
1696 + Bit_model bm_align[dis_align_size];
1698 + struct Matchfinder *matchfinder;
1699 + struct Range_encoder renc;
1700 + struct Len_encoder match_len_encoder;
1701 + struct Len_encoder rep_len_encoder;
1703 + int match_distances[max_match_len+1];
1704 + struct Trial trials[max_num_trials];
1706 + int dis_slot_prices[len_states][2*max_dictionary_bits];
1707 + int dis_prices[len_states][modeled_distances];
1708 + int align_prices[dis_align_size];
1709 + int align_price_count;
1710 + int num_dis_slots;
1713 +static void LZe_fill_align_prices(struct LZ_encoder *const e)
1715 + int i;
1716 + for (i = 0; i < dis_align_size; ++i)
1717 + e->align_prices[i] =
1718 + price_symbol_reversed(e->bm_align, i, dis_align_bits);
1719 + e->align_price_count = dis_align_size;
1722 +static bool LZe_init(struct LZ_encoder *const e,
1723 + struct Matchfinder *const mf, const File_header header)
1725 + int i;
1726 + e->longest_match_found = 0;
1727 + e->crc = 0xFFFFFFFFU;
1728 + Bm_array_init(&e->bm_literal[0][0], lz_num_models);
1729 + e->matchfinder = mf;
1730 + if (!Re_init(&e->renc)) return false;
1731 + Lee_init(&e->match_len_encoder, e->matchfinder->match_len_limit);
1732 + Lee_init(&e->rep_len_encoder, e->matchfinder->match_len_limit);
1733 + LZe_fill_align_prices(e);
1734 + e->num_dis_slots = 2 * real_bits(e->matchfinder->dictionary_size - 1);
1735 + for (i = 0; i < Fh_size; ++i)
1736 + Re_put_byte(&e->renc, header[i]);
1737 + return true;
1740 +static inline void LZe_free(struct LZ_encoder *const e)
1742 + Re_free(&e->renc);
1745 +static inline unsigned LZe_crc(const struct LZ_encoder *const e)
1747 + return e->crc ^ 0xFFFFFFFFU;
1750 + /* move-to-front dis in/into reps if( dis > 0 ) */
1751 +static void mtf_reps(const int dis, int reps[num_rep_distances])
1753 + int i;
1754 + if (dis >= num_rep_distances) {
1755 + for (i = num_rep_distances - 1; i > 0; --i)
1756 + reps[i] = reps[i - 1];
1757 + reps[0] = dis - num_rep_distances;
1758 + } else if (dis > 0) {
1759 + const int distance = reps[dis];
1760 + for (i = dis; i > 0; --i)
1761 + reps[i] = reps[i - 1];
1762 + reps[0] = distance;
1766 +static int LZe_price_shortrep(const struct LZ_encoder *const e,
1767 + const State state, const int pos_state)
1769 + return price0(e->bm_rep0[state]) + price0(e->bm_len[state][pos_state]);
1772 +static int LZe_price_rep(const struct LZ_encoder *const e, const int rep,
1773 + const State state, const int pos_state)
1775 + int price;
1776 + if (rep == 0)
1777 + return price0(e->bm_rep0[state]) +
1778 + price1(e->bm_len[state][pos_state]);
1779 + price = price1(e->bm_rep0[state]);
1780 + if (rep == 1)
1781 + price += price0(e->bm_rep1[state]);
1782 + else {
1783 + price += price1(e->bm_rep1[state]);
1784 + price += price_bit(e->bm_rep2[state], rep - 2);
1786 + return price;
1789 +static int LZe_price_dis(const struct LZ_encoder *const e,
1790 + const int dis, const int len_state)
1792 + if (dis < modeled_distances)
1793 + return e->dis_prices[len_state][dis];
1794 + else
1795 + return e->dis_slot_prices[len_state][get_slot(dis)] +
1796 + e->align_prices[dis & (dis_align_size - 1)];
1799 +static int LZe_price_pair(const struct LZ_encoder *const e,
1800 + const int dis, const int len,
1801 + const int pos_state)
1803 + if (len <= min_match_len && dis >= modeled_distances)
1804 + return infinite_price;
1805 + return Lee_price(&e->match_len_encoder, len, pos_state) +
1806 + LZe_price_dis(e, dis, get_len_state(len));
1809 +static int LZe_price_literal(const struct LZ_encoder *const e,
1810 + uint8_t prev_byte, uint8_t symbol)
1812 + return price_symbol(e->bm_literal[get_lit_state(prev_byte)], symbol, 8);
1815 +static int LZe_price_matched(const struct LZ_encoder *const e,
1816 + uint8_t prev_byte, uint8_t symbol,
1817 + uint8_t match_byte)
1819 + return price_matched(e->bm_literal[get_lit_state(prev_byte)], symbol,
1820 + match_byte);
1823 +static void LZe_encode_literal(struct LZ_encoder *const e,
1824 + uint8_t prev_byte, uint8_t symbol)
1826 + Re_encode_tree(&e->renc,
1827 + e->bm_literal[get_lit_state(prev_byte)], symbol, 8);
1830 +static void LZe_encode_matched(struct LZ_encoder *const e,
1831 + uint8_t prev_byte, uint8_t symbol,
1832 + uint8_t match_byte)
1834 + Re_encode_matched(&e->renc, e->bm_literal[get_lit_state(prev_byte)],
1835 + symbol, match_byte);
1838 +static void LZe_encode_pair(struct LZ_encoder *const e,
1839 + const unsigned dis, const int len,
1840 + const int pos_state)
1842 + const int dis_slot = get_slot(dis);
1843 + Lee_encode(&e->match_len_encoder, &e->renc, len, pos_state);
1844 + Re_encode_tree(&e->renc, e->bm_dis_slot[get_len_state(len)], dis_slot,
1845 + dis_slot_bits);
1847 + if (dis_slot >= start_dis_model) {
1848 + const int direct_bits = (dis_slot >> 1) - 1;
1849 + const unsigned base = (2 | (dis_slot & 1)) << direct_bits;
1850 + const unsigned direct_dis = dis - base;
1852 + if (dis_slot < end_dis_model)
1853 + Re_encode_tree_reversed(&e->renc,
1854 + e->bm_dis + base - dis_slot - 1,
1855 + direct_dis, direct_bits);
1856 + else {
1857 + Re_encode(&e->renc, direct_dis >> dis_align_bits,
1858 + direct_bits - dis_align_bits);
1859 + Re_encode_tree_reversed(&e->renc, e->bm_align,
1860 + direct_dis, dis_align_bits);
1861 + if (--e->align_price_count <= 0)
1862 + LZe_fill_align_prices(e);
1867 +static int LZe_read_match_distances(struct LZ_encoder *const e)
1869 + int len = Mf_longest_match_len(e->matchfinder, e->match_distances);
1870 + if (len == e->matchfinder->match_len_limit && len < max_match_len)
1871 + len += Mf_true_match_len(e->matchfinder, len,
1872 + e->match_distances[len] + 1,
1873 + max_match_len - len);
1874 + return len;
1877 +static void LZe_move_pos(struct LZ_encoder *const e, int n)
1879 + while (true) {
1880 + Mf_move_pos(e->matchfinder);
1881 + if( --n <= 0 ) break;
1882 + Mf_longest_match_len(e->matchfinder, 0);
1886 +static void LZe_backward(struct LZ_encoder *const e, int cur)
1888 + int *const dis = &e->trials[cur].dis;
1889 + while (cur > 0) {
1890 + const int prev_index = e->trials[cur].prev_index;
1891 + struct Trial *const prev_trial = &e->trials[prev_index];
1892 + prev_trial->price = cur - prev_index; /* len */
1893 + cur = *dis;
1894 + *dis = prev_trial->dis;
1895 + prev_trial->dis = cur;
1896 + cur = prev_index;
1900 + /* End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len) */
1901 +static void LZe_full_flush(struct LZ_encoder *const e, const State state)
1903 + int i;
1904 + const int pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
1905 + File_trailer trailer;
1906 + Re_encode_bit(&e->renc, &e->bm_match[state][pos_state], 1);
1907 + Re_encode_bit(&e->renc, &e->bm_rep[state], 0);
1908 + LZe_encode_pair(e, 0xFFFFFFFFU, min_match_len, pos_state);
1909 + Re_flush(&e->renc);
1910 + Ft_set_data_crc(trailer, LZe_crc(e));
1911 + Ft_set_data_size(trailer, Mf_data_position(e->matchfinder));
1912 + Ft_set_member_size(trailer, Re_member_position(&e->renc) + Ft_size);
1913 + for (i = 0; i < Ft_size; ++i)
1914 + Re_put_byte(&e->renc, trailer[i]);
1915 + Re_flush_data(&e->renc);
1919 +static void LZe_update_distance_prices(struct LZ_encoder *const e)
1921 + int dis, len_state;
1922 + for (dis = start_dis_model; dis < modeled_distances; ++dis) {
1923 + const int dis_slot = dis_slots[dis];
1924 + const int direct_bits = (dis_slot >> 1) - 1;
1925 + const int base = (2 | (dis_slot & 1)) << direct_bits;
1926 + const int price =
1927 + price_symbol_reversed(e->bm_dis + base - dis_slot - 1,
1928 + dis - base, direct_bits);
1929 + for (len_state = 0; len_state < len_states; ++len_state)
1930 + e->dis_prices[len_state][dis] = price;
1933 + for (len_state = 0; len_state < len_states; ++len_state) {
1934 + int *const dsp = e->dis_slot_prices[len_state];
1935 + int *const dp = e->dis_prices[len_state];
1936 + const Bit_model *const bmds = e->bm_dis_slot[len_state];
1937 + int slot = 0;
1938 + for (; slot < end_dis_model; ++slot)
1939 + dsp[slot] = price_symbol(bmds, slot, dis_slot_bits);
1940 + for (; slot < e->num_dis_slots; ++slot)
1941 + dsp[slot] = price_symbol(bmds, slot, dis_slot_bits) +
1942 + ((((slot >> 1) - 1) - dis_align_bits) << price_shift_bits);
1944 + for (dis = 0; dis < start_dis_model; ++dis)
1945 + dp[dis] = dsp[dis];
1946 + for (; dis < modeled_distances; ++dis)
1947 + dp[dis] += dsp[dis_slots[dis]];
1952 +/* Returns the number of bytes advanced (ahead).
1953 + trials[0]..trials[ahead-1] contain the steps to encode.
1954 + ( trials[0].dis == -1 && trials[0].price == 1 ) means literal.
1955 + A match/rep longer or equal than match_len_limit finishes the sequence.
1957 +static int LZe_sequence_optimizer(struct LZ_encoder *const e,
1958 + const int reps[num_rep_distances],
1959 + const State state)
1961 + int main_len, i, rep, cur = 0, num_trials;
1962 + int replens[num_rep_distances];
1963 + int rep_index = 0;
1965 + if (e->longest_match_found > 0) { /* from previous call */
1966 + main_len = e->longest_match_found;
1967 + e->longest_match_found = 0;
1968 + } else
1969 + main_len = LZe_read_match_distances(e);
1971 + for (i = 0; i < num_rep_distances; ++i) {
1972 + replens[i] = Mf_true_match_len(e->matchfinder, 0, reps[i] + 1,
1973 + max_match_len);
1974 + if (replens[i] > replens[rep_index]) rep_index = i;
1976 + if (replens[rep_index] >= e->matchfinder->match_len_limit) {
1977 + e->trials[0].dis = rep_index;
1978 + e->trials[0].price = replens[rep_index];
1979 + LZe_move_pos(e, replens[rep_index]);
1980 + return replens[rep_index];
1983 + if (main_len >= e->matchfinder->match_len_limit) {
1984 + e->trials[0].dis =
1985 + e->match_distances[e->matchfinder->match_len_limit] +
1986 + num_rep_distances;
1987 + e->trials[0].price = main_len;
1988 + LZe_move_pos(e, main_len);
1989 + return main_len;
1993 + const int pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
1994 + const int match_price = price1(e->bm_match[state][pos_state]);
1995 + const int rep_match_price = match_price + price1(e->bm_rep[state]);
1996 + const uint8_t prev_byte = Mf_peek(e->matchfinder, 1);
1997 + const uint8_t cur_byte = Mf_peek(e->matchfinder, 0);
1998 + const uint8_t match_byte = Mf_peek(e->matchfinder, reps[0] + 1);
2000 + e->trials[0].state = state;
2001 + for (i = 0; i < num_rep_distances; ++i)
2002 + e->trials[0].reps[i] = reps[i];
2003 + e->trials[1].dis = -1; /* literal */
2004 + e->trials[1].prev_index = 0;
2005 + e->trials[1].price = price0(e->bm_match[state][pos_state]);
2006 + if (St_is_char(state))
2007 + e->trials[1].price +=
2008 + LZe_price_literal(e, prev_byte, cur_byte);
2009 + else
2010 + e->trials[1].price +=
2011 + LZe_price_matched(e, prev_byte, cur_byte, match_byte);
2013 + if (match_byte == cur_byte)
2014 + Tr_update(&e->trials[1], rep_match_price +
2015 + LZe_price_shortrep(e, state, pos_state), 0, 0);
2017 + if (main_len < min_match_len) {
2018 + e->trials[0].dis = e->trials[1].dis;
2019 + e->trials[0].price = 1;
2020 + Mf_move_pos(e->matchfinder);
2021 + return 1;
2024 + if (main_len <= replens[rep_index]) {
2025 + int len;
2027 + main_len = replens[rep_index];
2028 + for (len = min_match_len; len <= main_len; ++len)
2029 + e->trials[len].price = infinite_price;
2030 + } else {
2031 + int len;
2032 + const int normal_match_price =
2033 + match_price + price0(e->bm_rep[state]);
2034 + for (len = min_match_len; len <= main_len; ++len) {
2035 + e->trials[len].dis =
2036 + e->match_distances[len] + num_rep_distances;
2037 + e->trials[len].prev_index = 0;
2038 + e->trials[len].price = normal_match_price +
2039 + LZe_price_pair(e, e->match_distances[len],
2040 + len, pos_state);
2044 + for (rep = 0; rep < num_rep_distances; ++rep) {
2045 + const int price = rep_match_price +
2046 + LZe_price_rep(e, rep, state, pos_state);
2047 + int len;
2048 + for (len = min_match_len; len <= replens[rep]; ++len)
2049 + Tr_update(&e->trials[len], price +
2050 + Lee_price(&e->rep_len_encoder, len, pos_state),
2051 + rep, 0);
2055 + num_trials = main_len;
2057 + while (true) { /* price optimization loop */
2058 + struct Trial *cur_trial, *next_trial;
2059 + int newlen, pos_state, prev_index, len_limit;
2060 + int next_price, match_price, rep_match_price;
2061 + uint8_t prev_byte, cur_byte, match_byte;
2063 + Mf_move_pos(e->matchfinder);
2064 + if (++cur >= num_trials) { /* no more initialized trials */
2065 + LZe_backward(e, cur);
2066 + return cur;
2068 + newlen = LZe_read_match_distances(e);
2069 + if (newlen >= e->matchfinder->match_len_limit) {
2070 + e->longest_match_found = newlen;
2071 + LZe_backward(e, cur);
2072 + return cur;
2075 + /* give final values to current trial */
2076 + cur_trial = &e->trials[cur];
2077 + prev_index = cur_trial->prev_index;
2078 + cur_trial->state = e->trials[prev_index].state;
2080 + for (i = 0; i < num_rep_distances; ++i)
2081 + cur_trial->reps[i] = e->trials[prev_index].reps[i];
2083 + if (prev_index == cur - 1) {
2084 + if (cur_trial->dis == 0)
2085 + cur_trial->state = St_set_short_rep(cur_trial->state);
2086 + else
2087 + cur_trial->state = St_set_char(cur_trial->state);
2088 + } else {
2089 + if (cur_trial->dis < num_rep_distances)
2090 + cur_trial->state = St_set_rep(cur_trial->state);
2091 + else
2092 + cur_trial->state = St_set_match(cur_trial->state);
2093 + mtf_reps(cur_trial->dis, cur_trial->reps);
2096 + pos_state = Mf_data_position(e->matchfinder) & pos_state_mask;
2097 + prev_byte = Mf_peek(e->matchfinder, 1);
2098 + cur_byte = Mf_peek(e->matchfinder, 0);
2099 + match_byte = Mf_peek(e->matchfinder, cur_trial->reps[0] + 1);
2101 + next_price = cur_trial->price +
2102 + price0(e->bm_match[cur_trial->state][pos_state]);
2103 + if (St_is_char(cur_trial->state))
2104 + next_price += LZe_price_literal(e, prev_byte, cur_byte);
2105 + else
2106 + next_price += LZe_price_matched(e, prev_byte, cur_byte,
2107 + match_byte);
2108 + /* try last updates to next trial */
2109 + next_trial = &e->trials[cur + 1];
2111 + Tr_update(next_trial, next_price, -1, cur); /* literal */
2113 + match_price = cur_trial->price +
2114 + price1(e->bm_match[cur_trial->state][pos_state]);
2115 + rep_match_price = match_price + price1(e->bm_rep[cur_trial->state]);
2117 + if (match_byte == cur_byte && next_trial->dis != 0)
2118 + Tr_update(next_trial, rep_match_price +
2119 + LZe_price_shortrep(e, cur_trial->state,
2120 + pos_state), 0, cur);
2122 + len_limit = MIN(MIN(max_num_trials - 1 - cur,
2123 + Mf_available_bytes(e->matchfinder)),
2124 + e->matchfinder->match_len_limit);
2125 + if (len_limit < min_match_len) continue;
2127 + for (rep = 0; rep < num_rep_distances; ++rep) {
2128 + const int dis = cur_trial->reps[rep] + 1;
2129 + int len = 0;
2130 + const uint8_t *const data =
2131 + Mf_ptr_to_current_pos(e->matchfinder);
2132 + while (len < len_limit && data[len] == data[len - dis])
2133 + ++len;
2134 + if (len >= min_match_len) {
2135 + const int price = rep_match_price +
2136 + LZe_price_rep(e, rep, cur_trial->state, pos_state);
2137 + while (num_trials < cur + len)
2138 + e->trials[++num_trials].price = infinite_price;
2139 + for (; len >= min_match_len; --len)
2140 + Tr_update(&e->trials[cur + len], price +
2141 + Lee_price(&e->rep_len_encoder, len,
2142 + pos_state), rep, cur);
2146 + if (newlen <= len_limit &&
2147 + (newlen > min_match_len ||
2148 + (newlen == min_match_len &&
2149 + e->match_distances[min_match_len] < modeled_distances))) {
2150 + const int normal_match_price = match_price +
2151 + price0(e->bm_rep[cur_trial->state]);
2152 + int len;
2153 + int dis = e->match_distances[min_match_len];
2154 + int len_state = get_len_state(min_match_len);
2155 + int dis_price = infinite_price;
2157 + while (num_trials < cur + newlen)
2158 + e->trials[++num_trials].price = infinite_price;
2160 + if (dis < modeled_distances)
2161 + Tr_update(&e->trials[cur + min_match_len],
2162 + normal_match_price +
2163 + e->dis_prices[len_state][dis] +
2164 + Lee_price(&e->match_len_encoder,
2165 + min_match_len, pos_state),
2166 + dis + num_rep_distances, cur);
2168 + for (len = min_match_len + 1; len <= newlen; ++len) {
2169 + if (dis != e->match_distances[len] ||
2170 + len_state < len_states - 1) {
2171 + dis = e->match_distances[len];
2172 + len_state = get_len_state(len);
2173 + dis_price = LZe_price_dis(e, dis, len_state);
2175 + Tr_update(&e->trials[cur + len],
2176 + normal_match_price + dis_price +
2177 + Lee_price(&e->match_len_encoder, len, pos_state),
2178 + dis + num_rep_distances, cur);
2185 +static bool LZe_encode_member(struct LZ_encoder *const e)
2187 + const int dis_price_count =
2188 + (e->matchfinder->match_len_limit > 12) ? 512 : 2048;
2189 + int dis_price_counter = 0;
2190 + int ahead, i;
2191 + int reps[num_rep_distances];
2192 + State state = 0;
2193 + for (i = 0; i < num_rep_distances; ++i) reps[i] = 0;
2195 + if (!Mf_finished(e->matchfinder)) { /* encode first byte */
2196 + const uint8_t prev_byte = 0;
2197 + const uint8_t cur_byte = Mf_peek(e->matchfinder, 0);
2198 + Re_encode_bit(&e->renc, &e->bm_match[state][0], 0);
2199 + LZe_encode_literal(e, prev_byte, cur_byte);
2200 + CRC32_update_byte(&e->crc, cur_byte);
2201 + Mf_longest_match_len(e->matchfinder, 0);
2202 + Mf_move_pos(e->matchfinder);
2205 + while (!Mf_finished(e->matchfinder)) {
2206 + if (dis_price_counter <= 0) {
2207 + LZe_update_distance_prices(e);
2208 + dis_price_counter = dis_price_count;
2211 + ahead = LZe_sequence_optimizer(e, reps, state);
2212 + dis_price_counter -= ahead;
2214 + for (i = 0; ahead > 0;) {
2215 + const int pos_state =
2216 + (Mf_data_position(e->matchfinder) - ahead) & pos_state_mask;
2217 + const int dis = e->trials[i].dis;
2218 + const int len = e->trials[i].price;
2220 + bool bit = (dis < 0 && len == 1);
2221 + Re_encode_bit(&e->renc, &e->bm_match[state][pos_state], !bit);
2222 + if (bit) { /* literal byte */
2223 + const uint8_t prev_byte = Mf_peek(e->matchfinder, ahead + 1);
2224 + const uint8_t cur_byte = Mf_peek(e->matchfinder, ahead);
2225 + CRC32_update_byte(&e->crc, cur_byte);
2226 + if (St_is_char(state))
2227 + LZe_encode_literal(e, prev_byte, cur_byte);
2228 + else {
2229 + const uint8_t match_byte =
2230 + Mf_peek(e->matchfinder, ahead + reps[0] + 1);
2231 + LZe_encode_matched(e, prev_byte, cur_byte, match_byte);
2233 + state = St_set_char(state);
2234 + } else { /* match or repeated match */
2236 + e->crc = crc32_block_endian0(e->crc,
2237 + Mf_ptr_to_current_pos(e->matchfinder) - ahead,
2238 + len, global_crc32_table);
2239 + mtf_reps(dis, reps);
2240 + bit = (dis < num_rep_distances);
2241 + Re_encode_bit(&e->renc, &e->bm_rep[state], bit);
2242 + if (bit) { /* repeated match */
2243 + bit = (dis == 0);
2244 + Re_encode_bit(&e->renc, &e->bm_rep0[state], !bit);
2245 + if (bit)
2246 + Re_encode_bit(&e->renc, &e->bm_len[state][pos_state], len > 1);
2247 + else {
2248 + Re_encode_bit(&e->renc, &e->bm_rep1[state], dis > 1);
2249 + if (dis > 1)
2250 + Re_encode_bit(&e->renc, &e->bm_rep2[state], dis > 2);
2252 + if (len == 1)
2253 + state = St_set_short_rep(state);
2254 + else {
2255 + Lee_encode(&e->rep_len_encoder,
2256 + &e->renc, len, pos_state);
2257 + state = St_set_rep(state);
2259 + } else { /* match */
2260 + LZe_encode_pair(e, dis - num_rep_distances, len, pos_state);
2261 + state = St_set_match(state);
2264 + ahead -= len;
2265 + i += len;
2268 + LZe_full_flush(e, state);
2269 + return !e->renc.write_error;
2273 +struct Lzma_options {
2274 + int dictionary_size; /* 4KiB..512MiB */
2275 + int match_len_limit; /* 5..273 */
2276 +} encoder_options;
2279 +static int getnum(const char *const ptr, const int llimit, const int ulimit)
2281 + long result;
2282 + char *tail;
2283 + errno = 0;
2284 + result = strtol(ptr, &tail, 0);
2285 + if (tail == ptr || errno)
2286 + goto error;
2287 + if (tail[0]) {
2288 + int factor = (tail[1] == 'i') ? 1024 : 1000;
2289 + int exponent = 0, i;
2291 + switch (tail[0]) {
2292 + case 'M':
2293 + exponent = 2;
2294 + break;
2295 + case 'K':
2296 + if (factor == 1024) {
2297 + exponent = 1;
2298 + break;
2300 + goto error;
2301 + case 'k':
2302 + if (factor == 1000) {
2303 + exponent = 1;
2304 + break;
2306 + default:
2307 + goto error;
2309 + for (i = 0; i < exponent; ++i) {
2310 + if (LONG_MAX / factor >= labs(result))
2311 + result *= factor;
2312 + else
2313 + goto error;
2316 + if (result >= llimit && result <= ulimit)
2317 + return result;
2318 + error:
2319 + bb_error_msg_and_die("invalid number");
2323 +static int get_dict_size(const char *const arg)
2325 + char *tail;
2326 + long bits = strtol(arg, &tail, 0);
2327 + if (bits >= min_dictionary_bits &&
2328 + bits <= max_dictionary_bits && *tail == 0)
2329 + return (1 << bits);
2330 + return getnum(arg, min_dictionary_size, max_dictionary_size);
2334 +static IF_DESKTOP(long long) int FAST_FUNC pack_lzip(transformer_state_t *xstate UNUSED_PARAM)
2336 + int retval = 0;
2337 + File_header header;
2338 + struct Matchfinder matchfinder;
2339 + struct LZ_encoder * encoder;
2341 + Fh_set_magic(header);
2342 + if (!Fh_set_dictionary_size(header, encoder_options.dictionary_size) ||
2343 + encoder_options.match_len_limit < min_match_len_limit ||
2344 + encoder_options.match_len_limit > max_match_len)
2345 + bb_error_msg_and_die("internal error");
2347 + if (!Mf_init(&matchfinder, Fh_get_dictionary_size(header),
2348 + encoder_options.match_len_limit)) {
2349 + bb_error_msg(bb_msg_memory_exhausted);
2350 + return -1;
2352 + Fh_set_dictionary_size(header, matchfinder.dictionary_size);
2354 + encoder = malloc(sizeof(struct LZ_encoder));
2355 + if (!encoder || !LZe_init(encoder, &matchfinder, header)) {
2356 + bb_error_msg(bb_msg_memory_exhausted);
2357 + retval = -1;
2358 + } else {
2359 + if (!LZe_encode_member(encoder)) {
2360 + bb_perror_msg(bb_msg_write_error);
2361 + retval = -1;
2363 + LZe_free(encoder);
2365 + free(encoder);
2366 + Mf_free(&matchfinder);
2367 + return retval;
2371 +int lzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2372 +int lzip_main(int argc UNUSED_PARAM, char **argv)
2374 + /* Mapping from gzip/bzip2 style 1..9 compression modes
2375 + to the corresponding LZMA compression modes. */
2376 + const struct Lzma_options option_mapping[] = {
2377 + {1 << 20, 5}, /* -0 */
2378 + {1 << 20, 5}, /* -1 */
2379 + {3 << 19, 6}, /* -2 */
2380 + {1 << 21, 8}, /* -3 */
2381 + {3 << 20, 12}, /* -4 */
2382 + {1 << 22, 20}, /* -5 */
2383 + {1 << 23, 36}, /* -6 */
2384 + {1 << 24, 68}, /* -7 */
2385 + {3 << 23, 132}, /* -8 */
2386 + {1 << 25, 273} /* -9 */
2387 + };
2388 + int i;
2389 + char *m_arg;
2390 + char *s_arg;
2391 + /* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
2392 + uint32_t flags = getopt32(argv, "cfkvqdt0123456789Fm:s:", &m_arg, &s_arg);
2394 + if (flags & 0x60) { // -d and/or -t
2395 +#if ENABLE_LUNZIP /* lunzip_main may not be visible... */
2396 + return lunzip_main(argc, argv);
2397 +#else
2398 + bb_error_msg("decompression is disabled");
2399 + return 1;
2400 +#endif
2402 + flags >>= 7; /* drop "cfkvqdt" bits */
2404 + encoder_options = option_mapping[6]; /* default = "-6" */
2406 + for (i = 9; i >= 7; --i)
2407 + if (flags & (1 << i))
2408 + encoder_options = option_mapping[i];
2409 + for (i = 0; i <= 6; ++i)
2410 + if (flags & (1 << i))
2411 + encoder_options = option_mapping[i];
2412 + if (flags & (1 << 11)) /* -m */
2413 + encoder_options.match_len_limit =
2414 + getnum(m_arg, min_match_len_limit, max_match_len);
2415 + if (flags & (1 << 12)) /* -s */
2416 + encoder_options.dictionary_size = get_dict_size(s_arg);
2417 + /* end process options */
2419 + argv += optind;
2421 + if (!global_crc32_table)
2422 + global_crc32_table = crc32_filltable(NULL, 0);
2423 + if (!dis_slots) {
2424 + Dis_slots_init();
2425 + Prob_prices_init();
2428 + return bbunpack(argv, pack_lzip, append_ext, "lz");
2430 diff -urdN busybox-1.27.0.old/archival/tar.c busybox-1.27.0.new/archival/tar.c
2431 --- busybox-1.27.0.old/archival/tar.c 2017-07-01 22:51:12.000000000 +0200
2432 +++ busybox-1.27.0.new/archival/tar.c 2017-07-07 01:00:19.000000000 +0200
2433 @@ -62,7 +62,7 @@
2434 //config:config FEATURE_TAR_AUTODETECT
2435 //config: bool "Autodetect compressed tarballs"
2436 //config: default y
2437 -//config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
2438 +//config: depends on TAR && (FEATURE_SEAMLESS_Z || FEATURE_SEAMLESS_GZ || FEATURE_SEAMLESS_BZ2 || FEATURE_SEAMLESS_LZ || FEATURE_SEAMLESS_LZMA || FEATURE_SEAMLESS_XZ)
2439 //config: help
2440 //config: With this option tar can automatically detect compressed
2441 //config: tarballs. Currently it works only on files (not pipes etc).
2442 @@ -771,6 +771,7 @@
2443 //usage: IF_FEATURE_SEAMLESS_GZ("z")
2444 //usage: IF_FEATURE_SEAMLESS_XZ("J")
2445 //usage: IF_FEATURE_SEAMLESS_BZ2("j")
2446 +//usage: IF_FEATURE_SEAMLESS_LZ("y")
2447 //usage: IF_FEATURE_SEAMLESS_LZMA("a")
2448 //usage: IF_FEATURE_TAR_CREATE("h")
2449 //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
2450 @@ -802,6 +803,9 @@
2451 //usage: IF_FEATURE_SEAMLESS_BZ2(
2452 //usage: "\n j (De)compress using bzip2"
2453 //usage: )
2454 +//usage: IF_FEATURE_SEAMLESS_LZ(
2455 +//usage: "\n y (De)compress using lzip"
2456 +//usage: )
2457 //usage: IF_FEATURE_SEAMLESS_LZMA(
2458 //usage: "\n a (De)compress using lzma"
2459 //usage: )
2460 @@ -846,6 +850,7 @@
2461 IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
2462 IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
2463 IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,) // 16th bit
2464 + IF_FEATURE_SEAMLESS_LZ( OPTBIT_LZIP ,)
2465 IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,)
2466 IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
2467 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
2468 @@ -873,6 +878,7 @@
2469 OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
2470 OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
2471 OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J
2472 + OPT_LZIP = IF_FEATURE_SEAMLESS_LZ( (1 << OPTBIT_LZIP )) + 0, // y
2473 OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
2474 OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
2475 OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
2476 @@ -882,7 +888,7 @@
2477 OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
2478 OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite
2480 - OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
2481 + OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_LZIP | OPT_COMPRESS),
2483 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
2484 static const char tar_longopts[] ALIGN1 =
2485 @@ -915,6 +921,9 @@
2486 # if ENABLE_FEATURE_SEAMLESS_GZ
2487 "gzip\0" No_argument "z"
2488 # endif
2489 +# if ENABLE_FEATURE_SEAMLESS_LZ
2490 + "lzip\0" No_argument "y"
2491 +# endif
2492 # if ENABLE_FEATURE_SEAMLESS_XZ
2493 "xz\0" No_argument "J"
2494 # endif
2495 @@ -1026,6 +1035,7 @@
2496 IF_FEATURE_TAR_FROM( "T:*X:*")
2497 IF_FEATURE_SEAMLESS_GZ( "z" )
2498 IF_FEATURE_SEAMLESS_XZ( "J" )
2499 + IF_FEATURE_SEAMLESS_LZ( "y" )
2500 IF_FEATURE_SEAMLESS_Z( "Z" )
2501 IF_FEATURE_TAR_NOPRESERVE_TIME("m")
2502 IF_FEATURE_TAR_LONG_OPTIONS("\xf9:") // --strip-components
2503 @@ -1063,6 +1073,7 @@
2504 showopt(OPT_EXCLUDE_FROM );
2505 showopt(OPT_GZIP );
2506 showopt(OPT_XZ );
2507 + showopt(OPT_LZIP );
2508 showopt(OPT_COMPRESS );
2509 showopt(OPT_NOPRESERVE_TIME );
2510 showopt(OPT_STRIP_COMPONENTS);
2511 @@ -1198,6 +1209,8 @@
2512 zipMode = "gzip";
2513 if (opt & OPT_BZIP2)
2514 zipMode = "bzip2";
2515 + if (opt & OPT_LZIP)
2516 + zipMode = "lzip";
2517 if (opt & OPT_LZMA)
2518 zipMode = "lzma";
2519 if (opt & OPT_XZ)
2520 @@ -1228,6 +1241,10 @@
2521 USE_FOR_MMU(IF_FEATURE_SEAMLESS_BZ2(xformer = unpack_bz2_stream;))
2522 USE_FOR_NOMMU(xformer_prog = "bunzip2";)
2524 + if (opt & OPT_LZIP) {
2525 + USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZ(xformer = unpack_lz_stream;))
2526 + USE_FOR_NOMMU(xformer_prog = "lunzip";)
2528 if (opt & OPT_LZMA) {
2529 USE_FOR_MMU(IF_FEATURE_SEAMLESS_LZMA(xformer = unpack_lzma_stream;))
2530 USE_FOR_NOMMU(xformer_prog = "unlzma";)
2531 diff -urdN busybox-1.27.0.old/configs/TEST_nommu_defconfig busybox-1.27.0.new/configs/TEST_nommu_defconfig
2532 --- busybox-1.27.0.old/configs/TEST_nommu_defconfig 2017-01-30 21:57:30.000000000 +0100
2533 +++ busybox-1.27.0.new/configs/TEST_nommu_defconfig 2017-07-07 00:24:37.000000000 +0200
2534 @@ -108,6 +108,7 @@
2535 CONFIG_FEATURE_SEAMLESS_LZMA=y
2536 CONFIG_FEATURE_SEAMLESS_BZ2=y
2537 CONFIG_FEATURE_SEAMLESS_GZ=y
2538 +CONFIG_FEATURE_SEAMLESS_LZ=y
2539 CONFIG_FEATURE_SEAMLESS_Z=y
2540 CONFIG_AR=y
2541 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2542 @@ -121,6 +122,8 @@
2543 CONFIG_GUNZIP=y
2544 CONFIG_GZIP=y
2545 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2546 +CONFIG_LUNZIP=y
2547 +CONFIG_LZIP=y
2548 CONFIG_LZOP=y
2549 CONFIG_LZOP_COMPR_HIGH=y
2550 CONFIG_RPM2CPIO=y
2551 diff -urdN busybox-1.27.0.old/configs/TEST_noprintf_defconfig busybox-1.27.0.new/configs/TEST_noprintf_defconfig
2552 --- busybox-1.27.0.old/configs/TEST_noprintf_defconfig 2017-01-15 00:18:53.000000000 +0100
2553 +++ busybox-1.27.0.new/configs/TEST_noprintf_defconfig 2017-07-07 00:24:37.000000000 +0200
2554 @@ -119,6 +119,7 @@
2555 CONFIG_FEATURE_SEAMLESS_LZMA=y
2556 CONFIG_FEATURE_SEAMLESS_BZ2=y
2557 CONFIG_FEATURE_SEAMLESS_GZ=y
2558 +CONFIG_FEATURE_SEAMLESS_LZ=y
2559 CONFIG_FEATURE_SEAMLESS_Z=y
2560 # CONFIG_AR is not set
2561 # CONFIG_FEATURE_AR_LONG_FILENAMES is not set
2562 @@ -133,6 +134,8 @@
2563 # CONFIG_GUNZIP is not set
2564 # CONFIG_GZIP is not set
2565 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2566 +# CONFIG_LUNZIP is not set
2567 +# CONFIG_LZIP is not set
2568 # CONFIG_LZOP is not set
2569 # CONFIG_LZOP_COMPR_HIGH is not set
2570 # CONFIG_RPM2CPIO is not set
2571 diff -urdN busybox-1.27.0.old/configs/TEST_rh9_defconfig busybox-1.27.0.new/configs/TEST_rh9_defconfig
2572 --- busybox-1.27.0.old/configs/TEST_rh9_defconfig 2017-01-30 21:57:30.000000000 +0100
2573 +++ busybox-1.27.0.new/configs/TEST_rh9_defconfig 2017-07-07 00:24:37.000000000 +0200
2574 @@ -117,6 +117,7 @@
2575 CONFIG_FEATURE_SEAMLESS_LZMA=y
2576 CONFIG_FEATURE_SEAMLESS_BZ2=y
2577 CONFIG_FEATURE_SEAMLESS_GZ=y
2578 +CONFIG_FEATURE_SEAMLESS_LZ=y
2579 CONFIG_FEATURE_SEAMLESS_Z=y
2580 CONFIG_AR=y
2581 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2582 @@ -131,6 +132,8 @@
2583 CONFIG_GUNZIP=y
2584 CONFIG_GZIP=y
2585 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2586 +CONFIG_LUNZIP=y
2587 +CONFIG_LZIP=y
2588 CONFIG_LZOP=y
2589 # CONFIG_LZOP_COMPR_HIGH is not set
2590 CONFIG_RPM2CPIO=y
2591 diff -urdN busybox-1.27.0.old/configs/android2_defconfig busybox-1.27.0.new/configs/android2_defconfig
2592 --- busybox-1.27.0.old/configs/android2_defconfig 2017-01-30 21:57:30.000000000 +0100
2593 +++ busybox-1.27.0.new/configs/android2_defconfig 2017-07-07 00:24:37.000000000 +0200
2594 @@ -122,6 +122,7 @@
2595 CONFIG_FEATURE_SEAMLESS_LZMA=y
2596 CONFIG_FEATURE_SEAMLESS_BZ2=y
2597 CONFIG_FEATURE_SEAMLESS_GZ=y
2598 +CONFIG_FEATURE_SEAMLESS_LZ=y
2599 CONFIG_FEATURE_SEAMLESS_Z=y
2600 CONFIG_AR=y
2601 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2602 @@ -136,6 +137,8 @@
2603 CONFIG_GUNZIP=y
2604 CONFIG_GZIP=y
2605 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2606 +CONFIG_LUNZIP=y
2607 +CONFIG_LZIP=y
2608 CONFIG_LZOP=y
2609 CONFIG_LZOP_COMPR_HIGH=y
2610 CONFIG_RPM2CPIO=y
2611 diff -urdN busybox-1.27.0.old/configs/android_defconfig busybox-1.27.0.new/configs/android_defconfig
2612 --- busybox-1.27.0.old/configs/android_defconfig 2017-01-30 21:57:30.000000000 +0100
2613 +++ busybox-1.27.0.new/configs/android_defconfig 2017-07-07 00:24:37.000000000 +0200
2614 @@ -142,6 +142,7 @@
2615 CONFIG_FEATURE_SEAMLESS_LZMA=y
2616 CONFIG_FEATURE_SEAMLESS_BZ2=y
2617 CONFIG_FEATURE_SEAMLESS_GZ=y
2618 +CONFIG_FEATURE_SEAMLESS_LZ=y
2619 CONFIG_FEATURE_SEAMLESS_Z=y
2620 CONFIG_AR=y
2621 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2622 @@ -157,6 +158,8 @@
2623 CONFIG_GZIP=y
2624 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2625 CONFIG_GZIP_FAST=0
2626 +CONFIG_LUNZIP=y
2627 +CONFIG_LZIP=y
2628 CONFIG_LZOP=y
2629 CONFIG_LZOP_COMPR_HIGH=y
2630 CONFIG_RPM2CPIO=y
2631 diff -urdN busybox-1.27.0.old/configs/android_ndk_defconfig busybox-1.27.0.new/configs/android_ndk_defconfig
2632 --- busybox-1.27.0.old/configs/android_ndk_defconfig 2017-01-30 21:57:30.000000000 +0100
2633 +++ busybox-1.27.0.new/configs/android_ndk_defconfig 2017-07-07 00:24:37.000000000 +0200
2634 @@ -132,6 +132,7 @@
2635 CONFIG_FEATURE_SEAMLESS_LZMA=y
2636 CONFIG_FEATURE_SEAMLESS_BZ2=y
2637 CONFIG_FEATURE_SEAMLESS_GZ=y
2638 +CONFIG_FEATURE_SEAMLESS_LZ=y
2639 CONFIG_FEATURE_SEAMLESS_Z=y
2640 CONFIG_AR=y
2641 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2642 @@ -154,6 +155,8 @@
2643 # CONFIG_FEATURE_GZIP_LONG_OPTIONS is not set
2644 CONFIG_GZIP_FAST=0
2645 # CONFIG_FEATURE_GZIP_LEVELS is not set
2646 +CONFIG_LUNZIP=y
2647 +CONFIG_LZIP=y
2648 CONFIG_LZOP=y
2649 CONFIG_LZOP_COMPR_HIGH=y
2650 CONFIG_RPM=y
2651 diff -urdN busybox-1.27.0.old/configs/cygwin_defconfig busybox-1.27.0.new/configs/cygwin_defconfig
2652 --- busybox-1.27.0.old/configs/cygwin_defconfig 2017-01-15 00:18:53.000000000 +0100
2653 +++ busybox-1.27.0.new/configs/cygwin_defconfig 2017-07-07 00:24:37.000000000 +0200
2654 @@ -122,6 +122,7 @@
2655 CONFIG_FEATURE_SEAMLESS_LZMA=y
2656 CONFIG_FEATURE_SEAMLESS_BZ2=y
2657 CONFIG_FEATURE_SEAMLESS_GZ=y
2658 +CONFIG_FEATURE_SEAMLESS_LZ=y
2659 # CONFIG_FEATURE_SEAMLESS_Z is not set
2660 # CONFIG_AR is not set
2661 # CONFIG_FEATURE_AR_LONG_FILENAMES is not set
2662 @@ -136,6 +137,8 @@
2663 CONFIG_GUNZIP=y
2664 CONFIG_GZIP=y
2665 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2666 +CONFIG_LUNZIP=y
2667 +CONFIG_LZIP=y
2668 CONFIG_LZOP=y
2669 # CONFIG_LZOP_COMPR_HIGH is not set
2670 CONFIG_RPM2CPIO=y
2671 diff -urdN busybox-1.27.0.old/configs/freebsd_defconfig busybox-1.27.0.new/configs/freebsd_defconfig
2672 --- busybox-1.27.0.old/configs/freebsd_defconfig 2017-01-15 00:18:53.000000000 +0100
2673 +++ busybox-1.27.0.new/configs/freebsd_defconfig 2017-07-07 00:24:37.000000000 +0200
2674 @@ -120,6 +120,7 @@
2675 CONFIG_FEATURE_SEAMLESS_LZMA=y
2676 CONFIG_FEATURE_SEAMLESS_BZ2=y
2677 CONFIG_FEATURE_SEAMLESS_GZ=y
2678 +CONFIG_FEATURE_SEAMLESS_LZ=y
2679 CONFIG_FEATURE_SEAMLESS_Z=y
2680 CONFIG_AR=y
2681 CONFIG_FEATURE_AR_LONG_FILENAMES=y
2682 @@ -134,6 +135,8 @@
2683 CONFIG_GUNZIP=y
2684 CONFIG_GZIP=y
2685 CONFIG_FEATURE_GZIP_LONG_OPTIONS=y
2686 +CONFIG_LUNZIP=y
2687 +CONFIG_LZIP=y
2688 CONFIG_LZOP=y
2689 # CONFIG_LZOP_COMPR_HIGH is not set
2690 CONFIG_RPM2CPIO=y
2691 diff -urdN busybox-1.27.0.old/include/bb_archive.h busybox-1.27.0.new/include/bb_archive.h
2692 --- busybox-1.27.0.old/include/bb_archive.h 2016-11-27 03:55:17.000000000 +0100
2693 +++ busybox-1.27.0.new/include/bb_archive.h 2017-07-07 00:24:37.000000000 +0200
2694 @@ -9,6 +9,8 @@
2695 COMPRESS_MAGIC = 0x1f9d,
2696 GZIP_MAGIC = 0x1f8b,
2697 BZIP2_MAGIC = 256 * 'B' + 'Z',
2698 + LZIP_MAGIC1 = 0x4C5A,
2699 + LZIP_MAGIC2 = 0x4950,
2700 /* .xz signature: 0xfd, '7', 'z', 'X', 'Z', 0x00 */
2701 /* More info at: http://tukaani.org/xz/xz-file-format.txt */
2702 XZ_MAGIC1 = 256 * 0xfd + '7',
2703 @@ -21,6 +23,8 @@
2704 COMPRESS_MAGIC = 0x9d1f,
2705 GZIP_MAGIC = 0x8b1f,
2706 BZIP2_MAGIC = 'B' + 'Z' * 256,
2707 + LZIP_MAGIC1 = 0x5A4C,
2708 + LZIP_MAGIC2 = 0x5049,
2709 XZ_MAGIC1 = 0xfd + '7' * 256,
2710 XZ_MAGIC2 = 'z' + ('X' + ('Z' + 0 * 256) * 256) * 256,
2711 XZ_MAGIC1a = 0xfd + ('7' + ('z' + 'X' * 256) * 256) * 256,
2712 @@ -189,6 +193,7 @@
2713 char get_header_tar(archive_handle_t *archive_handle) FAST_FUNC;
2714 char get_header_tar_gz(archive_handle_t *archive_handle) FAST_FUNC;
2715 char get_header_tar_bz2(archive_handle_t *archive_handle) FAST_FUNC;
2716 +char get_header_tar_lz(archive_handle_t *archive_handle) FAST_FUNC;
2717 char get_header_tar_lzma(archive_handle_t *archive_handle) FAST_FUNC;
2718 char get_header_tar_xz(archive_handle_t *archive_handle) FAST_FUNC;
2720 @@ -239,6 +244,7 @@
2721 IF_DESKTOP(long long) int unpack_Z_stream(transformer_state_t *xstate) FAST_FUNC;
2722 IF_DESKTOP(long long) int unpack_gz_stream(transformer_state_t *xstate) FAST_FUNC;
2723 IF_DESKTOP(long long) int unpack_bz2_stream(transformer_state_t *xstate) FAST_FUNC;
2724 +IF_DESKTOP(long long) int unpack_lz_stream(transformer_state_t *xstate) FAST_FUNC;
2725 IF_DESKTOP(long long) int unpack_lzma_stream(transformer_state_t *xstate) FAST_FUNC;
2726 IF_DESKTOP(long long) int unpack_xz_stream(transformer_state_t *xstate) FAST_FUNC;
2728 diff -urdN busybox-1.27.0.old/include/libbb.h busybox-1.27.0.new/include/libbb.h
2729 --- busybox-1.27.0.old/include/libbb.h 2017-07-01 22:51:12.000000000 +0200
2730 +++ busybox-1.27.0.new/include/libbb.h 2017-07-07 00:24:37.000000000 +0200
2731 @@ -858,12 +858,13 @@
2732 #define SEAMLESS_COMPRESSION (0 \
2733 || ENABLE_FEATURE_SEAMLESS_XZ \
2734 || ENABLE_FEATURE_SEAMLESS_LZMA \
2735 + || ENABLE_FEATURE_SEAMLESS_LZ \
2736 || ENABLE_FEATURE_SEAMLESS_BZ2 \
2737 || ENABLE_FEATURE_SEAMLESS_GZ \
2738 || ENABLE_FEATURE_SEAMLESS_Z)
2740 #if SEAMLESS_COMPRESSION
2741 -/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
2742 +/* Autodetects gzip/bzip2/lzip formats. fd may be in the middle of the file! */
2743 extern int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
2744 /* Autodetects .gz etc */
2745 extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
2746 @@ -1288,6 +1289,7 @@
2747 /* Don't need IF_xxx() guard for these */
2748 int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2749 int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2750 +int lunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2752 #if ENABLE_ROUTE
2753 void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
2754 diff -urdN busybox-1.27.0.old/miscutils/man.c busybox-1.27.0.new/miscutils/man.c
2755 --- busybox-1.27.0.old/miscutils/man.c 2016-11-27 03:55:17.000000000 +0100
2756 +++ busybox-1.27.0.new/miscutils/man.c 2017-07-07 00:24:37.000000000 +0200
2757 @@ -180,6 +180,11 @@
2758 if (run_pipe(filename_with_zext, man, level))
2759 return 1;
2760 #endif
2761 +#if ENABLE_FEATURE_SEAMLESS_LZ
2762 + strcpy(ext, "lz");
2763 + if (run_pipe(filename_with_zext, man, level))
2764 + return 1;
2765 +#endif
2766 #if ENABLE_FEATURE_SEAMLESS_BZ2
2767 strcpy(ext, "bz2");
2768 if (run_pipe(filename_with_zext, man, level))
2769 diff -urdN busybox-1.27.0.old/scripts/fix_ws.sh busybox-1.27.0.new/scripts/fix_ws.sh
2770 --- busybox-1.27.0.old/scripts/fix_ws.sh 2016-11-27 03:55:17.000000000 +0100
2771 +++ busybox-1.27.0.new/scripts/fix_ws.sh 2017-07-07 00:24:37.000000000 +0200
2772 @@ -19,6 +19,7 @@
2773 | while read name; do
2774 test "YES" = "${name/*.bz2/YES}" && continue
2775 test "YES" = "${name/*.gz/YES}" && continue
2776 + test "YES" = "${name/*.lz/YES}" && continue
2777 test "YES" = "${name/*.png/YES}" && continue
2778 test "YES" = "${name/*.gif/YES}" && continue
2779 test "YES" = "${name/*.jpg/YES}" && continue
2780 diff -urdN busybox-1.27.0.old/testsuite/bunzip2.tests busybox-1.27.0.new/testsuite/bunzip2.tests
2781 --- busybox-1.27.0.old/testsuite/bunzip2.tests 2016-11-27 03:55:17.000000000 +0100
2782 +++ busybox-1.27.0.new/testsuite/bunzip2.tests 2017-07-07 00:24:37.000000000 +0200
2783 @@ -9,6 +9,9 @@
2784 elif test "${0##*/}" = "bunzip2.tests"; then
2785 unpack=bunzip2
2786 ext=bz2
2787 +elif test "${0##*/}" = "lunzip.tests"; then
2788 + unpack=lunzip
2789 + ext=lz
2790 else
2791 echo "WTF? argv0='$0'"
2792 exit 1
2793 @@ -35,6 +38,13 @@
2794 $ECHO -ne "\x17\x72\x45\x38\x50\x90\x5b\xb8\xe8\xa3"
2797 +hello_lz() {
2798 +# Lzipped "HELLO\n"
2799 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x24\x11\x45\xcf\x72\xcd\x3d\x3a\xdf"
2800 +$ECHO -ne "\xff\xff\xdd\x12\x00\x00\x6e\xd7\xac\xfd\x06\x00\x00\x00\x00\x00"
2801 +$ECHO -ne "\x00\x00\x2a\x00\x00\x00\x00\x00\x00\x00"
2804 # We had bunzip2 error on this .bz2 file (fixed in rev 22521)
2805 test1_bz2()
2807 @@ -530,6 +540,12 @@
2808 expected="ok\n"
2809 prep; check "$unpack: delete src" "${bb}$unpack t2.$ext; test ! -f t2.$ext && echo ok"
2811 +expected="ok\n"
2812 +rm -f t1.*
2813 +hello_$ext > t1.t$ext
2814 +check "$unpack: replace .t$ext --> .tar" \
2815 +"${bb}$unpack t1.t$ext && test -f t1.tar && test ! -f t1.t$ext && echo ok"
2818 rm -rf testdir
2820 diff -urdN busybox-1.27.0.old/testsuite/lunzip.tests busybox-1.27.0.new/testsuite/lunzip.tests
2821 --- busybox-1.27.0.old/testsuite/lunzip.tests 1970-01-01 01:00:00.000000000 +0100
2822 +++ busybox-1.27.0.new/testsuite/lunzip.tests 2017-07-07 00:24:37.000000000 +0200
2823 @@ -0,0 +1,3 @@
2824 +#!/bin/sh
2826 +. ./bunzip2.tests
2827 diff -urdN busybox-1.27.0.old/testsuite/lzip.tests busybox-1.27.0.new/testsuite/lzip.tests
2828 --- busybox-1.27.0.old/testsuite/lzip.tests 1970-01-01 01:00:00.000000000 +0100
2829 +++ busybox-1.27.0.new/testsuite/lzip.tests 2017-07-07 00:24:38.000000000 +0200
2830 @@ -0,0 +1,58 @@
2831 +#!/bin/sh
2832 +# Copyright (C) 2012-2016 Antonio Diaz Diaz.
2833 +# Licensed under GPLv2 or later, see file LICENSE in this source tree.
2835 +. ./testing.sh
2837 +rm -rf lzip.testdir 2>/dev/null
2838 +mkdir lzip.testdir
2840 +# Lzipped "a"
2841 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x30\xc1\xfb\xff\xff\xff\xe0\x00\x00\
2842 +\x00\x43\xbe\xb7\xe8\x01\x00\x00\x00\x00\x00\x00\x00\x25\x00\x00\
2843 +\x00\x00\x00\x00\x00" > lzip.testdir/a.lz
2845 +# Lzipped zero-length file
2846 +$ECHO -ne "\x4c\x5a\x49\x50\x01\x0c\x00\x83\xff\xfb\xff\xff\xc0\x00\x00\x00\
2847 +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\x00\x00\x00\
2848 +\x00\x00\x00\x00" > lzip.testdir/zero.lz
2851 +# testing "test name" "commands" "expected result" "file input" "stdin"
2853 +testing "lzip reads from standard input" \
2854 +"busybox lzip | cmp lzip.testdir/a.lz -" "" "" "a"
2856 +testing "lzip accepts single minus" \
2857 +"busybox lzip - | cmp lzip.testdir/a.lz -" "" "" "a"
2859 +testing "lzip compresses a zero-length file" \
2860 +"> zero ; busybox lzip -c zero | cmp lzip.testdir/zero.lz -" "" "" ""
2862 +testing "lzip replaces original file" \
2863 +"$ECHO foo > lzip.testdir/foo
2864 +rm -f lzip.testdir/foo.lz 2>/dev/null
2865 +busybox lzip lzip.testdir/foo
2866 +test -f lzip.testdir/foo.lz && test ! -f lzip.testdir/foo && echo ok" \
2867 +"ok\n" "" ""
2869 +testing "lzip replaces multiple files" \
2870 +"$ECHO foo > lzip.testdir/foo
2871 +$ECHO bar > lzip.testdir/bar
2872 +rm -f lzip.testdir/foo.lz 2>/dev/null
2873 +rm -f lzip.testdir/bar.lz 2>/dev/null
2874 +busybox lzip lzip.testdir/foo lzip.testdir/bar
2875 +test -f lzip.testdir/foo.lz && test ! -f lzip.testdir/foo && echo ok1
2876 +test -f lzip.testdir/bar.lz && test ! -f lzip.testdir/bar && echo ok2" \
2877 +"ok1\nok2\n" "" ""
2879 +testing "lzip -c does not remove original file" \
2880 +"$ECHO foo > lzip.testdir/foo
2881 +busybox lzip -c lzip.testdir/foo >/dev/null
2882 +test -f lzip.testdir/foo && echo ok" "ok\n" "" ""
2885 +# Clean up
2886 +rm -rf lzip.testdir 2>/dev/null
2888 +exit $FAILCOUNT
2889 diff -urdN busybox-1.27.0.old/testsuite/tar.tests busybox-1.27.0.new/testsuite/tar.tests
2890 --- busybox-1.27.0.old/testsuite/tar.tests 2016-12-12 19:16:43.000000000 +0100
2891 +++ busybox-1.27.0.new/testsuite/tar.tests 2017-07-07 00:24:38.000000000 +0200
2892 @@ -218,6 +218,25 @@
2893 "" ""
2894 SKIP=
2896 +# lzipped tar archive with a file named ustar containing "ustar\n"
2897 +optional FEATURE_SEAMLESS_LZ
2898 +testing "tar extract tlz" "\
2899 +tar -xyvf - && echo Ok
2900 +rm ustar || echo BAD
2901 +" "\
2902 +ustar
2904 +" \
2905 +"" "\
2906 +\x4c\x5a\x49\x50\x01\x0c\x00\x3a\x9c\xca\xdd\xdf\xd4\xd1\x20\x5c\
2907 +\xd8\x09\x8b\x76\xed\xd4\xe3\x53\xc0\x91\x33\x1e\x8c\x70\xfe\x9a\
2908 +\xb3\xd9\xab\xbf\x33\xd0\x4e\x42\x90\xee\xa0\xad\xa7\xa3\x8c\xa9\
2909 +\xfb\x94\xce\xf9\xdc\x54\x68\x12\x1d\x1b\x42\x36\x10\x5a\xa9\x15\
2910 +\x59\x4c\x46\xae\x50\xa5\xe7\xe2\x8d\xc0\xf5\x4b\xa3\xfa\xb7\x15\
2911 +\x6e\x33\x92\xc0\x93\xff\xff\xf7\x6b\x7b\x00\x2e\xb1\xb8\xc0\x00\
2912 +\x08\x00\x00\x00\x00\x00\x00\x6f\x00\x00\x00\x00\x00\x00\x00"
2913 +SKIP=
2915 # Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
2916 # (the uuencoded hello_world.txz contains one empty file named "hello_world")
2917 optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ