diff options
Diffstat (limited to '')
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | INSTALL | 3 | ||||
-rw-r--r-- | Makefile.in | 12 | ||||
-rw-r--r-- | NEWS | 16 | ||||
-rw-r--r-- | README | 61 | ||||
-rwxr-xr-x | configure | 10 | ||||
-rw-r--r-- | decoder.cc | 15 | ||||
-rw-r--r-- | decoder.h | 46 | ||||
-rw-r--r-- | doc/lzip.1 | 4 | ||||
-rw-r--r-- | doc/lzip.info | 312 | ||||
-rw-r--r-- | doc/lzip.texi (renamed from doc/lzip.texinfo) | 100 | ||||
-rw-r--r-- | encoder.cc | 181 | ||||
-rw-r--r-- | encoder.h | 244 | ||||
-rw-r--r-- | fast_encoder.cc | 119 | ||||
-rw-r--r-- | fast_encoder.h | 40 | ||||
-rw-r--r-- | lzip.h | 15 | ||||
-rw-r--r-- | main.cc | 69 | ||||
-rwxr-xr-x | testsuite/check.sh | 15 |
18 files changed, 670 insertions, 607 deletions
@@ -1,3 +1,11 @@ +2014-01-11 Antonio Diaz Diaz <antonio@gnu.org> + + * Version 1.16-pre1 released. + * main.cc (close_and_set_permissions): Behave like 'cp -p'. + * Compression time has been reduced by 4%. + * Minor improvements. + * lzip.texinfo: Renamed to lzip.texi. + 2013-09-20 Antonio Diaz Diaz <antonio@gnu.org> * Version 1.15 released. @@ -115,7 +123,7 @@ * Version 1.8 released. * Compression time has been reduced by 4%. * Lzdiff and lzgrep have been moved to the new package zutils. - * Fixed warnings on sistems where uint32_t != unsigned int. + * Fixed warnings on systems where uint32_t != unsigned int. 2009-06-25 Antonio Diaz Diaz <ant_diaz@teleline.es> @@ -209,7 +217,7 @@ * Version 0.4 released. * Code cleanup for global variable 'verbosity'. * Regained the compression ratio of 0.2 with 5% faster speed. - * Fixed compilation on sistems where size_t != unsigned int. + * Fixed compilation on systems where size_t != unsigned int. 2008-09-15 Antonio Diaz Diaz <ant_diaz@teleline.es> @@ -230,7 +238,8 @@ * Version 0.1 released. -Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. +Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 +Antonio Diaz Diaz. This file is a collection of facts, and thus it is not copyrightable, but just in case, you have unlimited permission to copy, distribute and @@ -54,7 +54,8 @@ After running 'configure', you can run 'make' and 'make install' as explained above. -Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. +Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 +Antonio Diaz Diaz. This file is free documentation: you have unlimited permission to copy, distribute and modify it. diff --git a/Makefile.in b/Makefile.in index c8f0ad9..66aad84 100644 --- a/Makefile.in +++ b/Makefile.in @@ -1,8 +1,8 @@ DISTNAME = $(pkgname)-$(pkgversion) INSTALL = install -INSTALL_PROGRAM = $(INSTALL) -p -m 755 -INSTALL_DATA = $(INSTALL) -p -m 644 +INSTALL_PROGRAM = $(INSTALL) -m 755 +INSTALL_DATA = $(INSTALL) -m 644 INSTALL_DIR = $(INSTALL) -d -m 755 SHELL = /bin/sh @@ -39,8 +39,8 @@ doc : info man info : $(VPATH)/doc/$(pkgname).info -$(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texinfo - cd $(VPATH)/doc && makeinfo $(pkgname).texinfo +$(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texi + cd $(VPATH)/doc && makeinfo $(pkgname).texi man : $(VPATH)/doc/$(progname).1 @@ -85,7 +85,7 @@ uninstall-man : dist : doc ln -sf $(VPATH) $(DISTNAME) - tar -cvf $(DISTNAME).tar \ + tar -Hustar --owner=root --group=root -cvf $(DISTNAME).tar \ $(DISTNAME)/AUTHORS \ $(DISTNAME)/COPYING \ $(DISTNAME)/ChangeLog \ @@ -96,7 +96,7 @@ dist : doc $(DISTNAME)/configure \ $(DISTNAME)/doc/$(progname).1 \ $(DISTNAME)/doc/$(pkgname).info \ - $(DISTNAME)/doc/$(pkgname).texinfo \ + $(DISTNAME)/doc/$(pkgname).texi \ $(DISTNAME)/testsuite/check.sh \ $(DISTNAME)/testsuite/test.txt \ $(DISTNAME)/testsuite/test.txt.lz \ @@ -1,13 +1,11 @@ -Changes in version 1.15: +Changes in version 1.16: -Lzip now shows the progress of compression at verbosity level 2 (-vv). +Copying of file dates, permissions, and ownership now behaves like "cp -p". +(If the user ID or the group ID can't be duplicated, the file permission +bits S_ISUID and S_ISGID are cleared). -Decompressing and testing no more show file version. +Compression time has been reduced by 4%. -Option "-n, --threads" is now accepted and ignored for compatibility -with plzip. +Some minor improvements have been made. -The configure script now accepts options with a separate argument. - -The chapter "Stream format" and the appendix "Reference source code" -have been added to the manual. +"lzip.texinfo" has been renamed to "lzip.texi". @@ -1,23 +1,28 @@ Description Lzip is a lossless data compressor with a user interface similar to the -one of gzip or bzip2. Lzip decompresses almost as fast as gzip and -compresses more than bzip2, which makes it well suited for software -distribution and data archiving. Lzip is a clean implementation of the -LZMA algorithm. +one of gzip or bzip2. Lzip is about as fast as gzip, compresses most +files more than bzip2, and is better than both from a data recovery +perspective. Lzip is a clean implementation of the LZMA algorithm. The lzip file format is designed for long-term data archiving and -provides very safe integrity checking. The member trailer stores the -32-bit CRC of the original data, the size of the original data and the -size of the member. These values, together with the value remaining in -the range decoder and the end-of-stream marker, provide a 4 factor -integrity checking which guarantees that the decompressed version of the -data is identical to the original. This guards against corruption of the -compressed data, and against undetected bugs in lzip (hopefully very -unlikely). The chances of data corruption going undetected are -microscopic. Be aware, though, that the check occurs upon decompression, -so it can only tell you that something is wrong. It can't help you -recover the original uncompressed data. +provides very safe integrity checking. It is as simple as possible (but +not simpler), so that with the only help of the lzip manual it would be +possible for a digital archaeologist to extract the data from a lzip +file long after quantum computers eventually render LZMA obsolete. +Additionally lzip is copylefted, which guarantees that it will remain +free forever. + +The member trailer stores the 32-bit CRC of the original data, the size +of the original data and the size of the member. These values, together +with the value remaining in the range decoder and the end-of-stream +marker, provide a 4 factor integrity checking which guarantees that the +decompressed version of the data is identical to the original. This +guards against corruption of the compressed data, and against undetected +bugs in lzip (hopefully very unlikely). The chances of data corruption +going undetected are microscopic. Be aware, though, that the check +occurs upon decompression, so it can only tell you that something is +wrong. It can't help you recover the original uncompressed data. If you ever need to recover data from a damaged lzip file, try the lziprecover program. Lziprecover makes lzip files resistant to bit-flip @@ -29,12 +34,23 @@ Lzip uses the same well-defined exit status values used by bzip2, which makes it safer than compressors returning ambiguous warning values (like gzip) when it is used as a back end for tar or zutils. -Lzip replaces every file given in the command line with a compressed -version of itself, with the name "original_name.lz". Each compressed -file has the same modification date, permissions, and, when possible, -ownership as the corresponding original, so that these properties can be -correctly restored at decompression time. Lzip is able to read from some -types of non regular files if the "--stdout" option is specified. +When compressing, lzip replaces every file given in the command line +with a compressed version of itself, with the name "original_name.lz". +When decompressing, lzip attempts to guess the name for the decompressed +file from that of the compressed file as follows: + +filename.lz becomes filename +filename.tlz becomes filename.tar +anyothername becomes anyothername.out + +(De)compressing a file is much like copying or moving it; therefore lzip +preserves the access and modification dates, permissions, and, when +possible, ownership of the file just as "cp -p" does. (If the user ID or +the group ID can't be duplicated, the file permission bits S_ISUID and +S_ISGID are cleared). + +Lzip is able to read from some types of non regular files if the +"--stdout" option is specified. If no file names are specified, lzip compresses (or decompresses) from standard input to standard output. In this case, lzip will decline to @@ -76,7 +92,8 @@ range encoding), Igor Pavlov (for putting all the above together in LZMA), and Julian Seward (for bzip2's CLI). -Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. +Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 +Antonio Diaz Diaz. This file is free documentation: you have unlimited permission to copy, distribute and modify it. @@ -1,14 +1,15 @@ #! /bin/sh # configure script for Lzip - LZMA lossless data compressor -# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. +# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 +# Antonio Diaz Diaz. # # This configure script is free software: you have unlimited permission # to copy, distribute and modify it. pkgname=lzip -pkgversion=1.15 +pkgversion=1.16-pre1 progname=lzip -srctrigger=doc/${pkgname}.texinfo +srctrigger=doc/${pkgname}.texi # clear some things potentially inherited from environment. LC_ALL=C @@ -165,7 +166,8 @@ echo "LDFLAGS = ${LDFLAGS}" rm -f Makefile cat > Makefile << EOF # Makefile for Lzip - LZMA lossless data compressor -# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. +# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 +# Antonio Diaz Diaz. # This file was generated automatically by configure. Do not edit. # # This Makefile is free software: you have unlimited permission @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -63,7 +64,7 @@ int readblock( const int fd, uint8_t * const buf, const int size ) const int n = read( fd, buf + size - rest, rest ); if( n > 0 ) rest -= n; else if( n == 0 ) break; // EOF - else if( errno != EINTR && errno != EAGAIN ) break; + else if( errno != EINTR ) break; errno = 0; } return size - rest; @@ -81,7 +82,7 @@ int writeblock( const int fd, const uint8_t * const buf, const int size ) { const int n = write( fd, buf + size - rest, rest ); if( n > 0 ) rest -= n; - else if( n < 0 && errno != EINTR && errno != EAGAIN ) break; + else if( n < 0 && errno != EINTR ) break; errno = 0; } return size - rest; @@ -108,8 +109,7 @@ void LZ_decoder::flush_data() { const int size = pos - stream_pos; crc32.update_buf( crc_, buffer + stream_pos, size ); - if( outfd >= 0 && - writeblock( outfd, buffer + stream_pos, size ) != size ) + if( outfd >= 0 && writeblock( outfd, buffer + stream_pos, size ) != size ) throw Error( "Write error" ); if( pos >= buffer_size ) { partial_data_pos += pos; pos = 0; } stream_pos = pos; @@ -219,7 +219,7 @@ int LZ_decoder::decode_member( const Pretty_print & pp ) if( state.is_char() ) { state.set_char1(); - put_byte( rdec.decode_tree( bm_literal[get_lit_state(prev_byte)], 8 ) ); + put_byte( rdec.decode_tree8( bm_literal[get_lit_state(prev_byte)] ) ); } else { @@ -298,8 +298,7 @@ int LZ_decoder::decode_member( const Pretty_print & pp ) } rep3 = rep2; rep2 = rep1; rep1 = rep0_saved; state.set_match(); - if( rep0 >= (unsigned)dictionary_size || - ( rep0 >= (unsigned)pos && !partial_data_pos ) ) + if( rep0 >= dictionary_size || rep0 >= data_position() ) { flush_data(); return 1; } } copy_block( rep0, len ); @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -76,6 +77,7 @@ public: code = 0; for( int i = 0; i < 5; ++i ) code = (code << 8) | get_byte(); range = 0xFFFFFFFFU; + code &= range; // make sure that first byte is discarded } void normalize() @@ -120,12 +122,13 @@ public: } } - int decode_tree( Bit_model bm[], const int num_bits ) + int decode_tree3( Bit_model bm[] ) { int symbol = 1; - for( int i = num_bits; i > 0; --i ) - symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); - return symbol - (1 << num_bits); + symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); + symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); + symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); + return symbol & 7; } int decode_tree6( Bit_model bm[] ) @@ -137,7 +140,15 @@ public: symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); - return symbol - (1 << 6); + return symbol & 0x3F; + } + + int decode_tree8( Bit_model bm[] ) + { + int symbol = 1; + while( symbol < 0x100 ) + symbol = ( symbol << 1 ) | decode_bit( bm[symbol] ); + return symbol & 0xFF; } int decode_tree_reversed( Bit_model bm[], const int num_bits ) @@ -156,10 +167,9 @@ public: int decode_tree_reversed4( Bit_model bm[] ) { int model = 1; - int symbol = 0; + int symbol = decode_bit( bm[model] ); + model = (model << 1) + symbol; int bit = decode_bit( bm[model] ); - model = (model << 1) + bit; symbol |= bit; - bit = decode_bit( bm[model] ); model = (model << 1) + bit; symbol |= (bit << 1); bit = decode_bit( bm[model] ); model = (model << 1) + bit; symbol |= (bit << 2); @@ -171,7 +181,7 @@ public: { Bit_model * const bm1 = bm + 0x100; int symbol = 1; - for( int i = 7; i >= 0; --i ) + while( symbol < 0x100 ) { match_byte <<= 1; const int match_bit = match_byte & 0x100; @@ -184,18 +194,16 @@ public: break; } } - return symbol - 0x100; + return symbol & 0xFF; } int decode_len( Len_model & lm, const int pos_state ) { if( decode_bit( lm.choice1 ) == 0 ) - return decode_tree( lm.bm_low[pos_state], len_low_bits ); + return decode_tree3( lm.bm_low[pos_state] ); if( decode_bit( lm.choice2 ) == 0 ) - return len_low_symbols + - decode_tree( lm.bm_mid[pos_state], len_mid_bits ); - return len_low_symbols + len_mid_symbols + - decode_tree( lm.bm_high, len_high_bits ); + return len_low_symbols + decode_tree3( lm.bm_mid[pos_state] ); + return len_low_symbols + len_mid_symbols + decode_tree8( lm.bm_high ); } }; @@ -204,7 +212,7 @@ class LZ_decoder { unsigned long long partial_data_pos; Range_decoder & rdec; - const int dictionary_size; + const unsigned dictionary_size; const int buffer_size; uint8_t * const buffer; // output buffer int pos; // current pos in buffer @@ -261,14 +269,14 @@ public: partial_data_pos( 0 ), rdec( rde ), dictionary_size( header.dictionary_size() ), - buffer_size( std::max( 65536, dictionary_size ) ), + buffer_size( std::max( 65536U, dictionary_size ) ), buffer( new uint8_t[buffer_size] ), pos( 0 ), stream_pos( 0 ), crc_( 0xFFFFFFFFU ), outfd( ofd ), member_version( header.version() ) - { buffer[buffer_size-1] = 0; } // prev_byte of first_byte + { buffer[buffer_size-1] = 0; } // prev_byte of first_byte ~LZ_decoder() { delete[] buffer; } @@ -1,5 +1,5 @@ .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.37.1. -.TH LZIP "1" "September 2013" "Lzip 1.15" "User Commands" +.TH LZIP "1" "January 2014" "Lzip 1.16-pre1" "User Commands" .SH NAME Lzip \- reduces the size of files .SH SYNOPSIS @@ -82,7 +82,7 @@ Report bugs to lzip\-bug@nongnu.org .br Lzip home page: http://www.nongnu.org/lzip/lzip.html .SH COPYRIGHT -Copyright \(co 2013 Antonio Diaz Diaz. +Copyright \(co 2014 Antonio Diaz Diaz. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> .br This is free software: you are free to change and redistribute it. diff --git a/doc/lzip.info b/doc/lzip.info index a1a2cbe..609da4f 100644 --- a/doc/lzip.info +++ b/doc/lzip.info @@ -1,4 +1,4 @@ -This is lzip.info, produced by makeinfo version 4.13 from lzip.texinfo. +This is lzip.info, produced by makeinfo version 4.13+ from lzip.texi. INFO-DIR-SECTION Data Compression START-INFO-DIR-ENTRY @@ -11,7 +11,7 @@ File: lzip.info, Node: Top, Next: Introduction, Up: (dir) Lzip Manual *********** -This manual is for Lzip (version 1.15, 20 September 2013). +This manual is for Lzip (version 1.16-pre1, 11 January 2014). * Menu: @@ -26,7 +26,8 @@ This manual is for Lzip (version 1.15, 20 September 2013). * Concept index:: Index of concepts - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz +Diaz. This manual is free documentation: you have unlimited permission to copy, distribute and modify it. @@ -38,23 +39,29 @@ File: lzip.info, Node: Introduction, Next: Algorithm, Prev: Top, Up: Top ************** Lzip is a lossless data compressor with a user interface similar to the -one of gzip or bzip2. Lzip decompresses almost as fast as gzip and -compresses more than bzip2, which makes it well suited for software -distribution and data archiving. Lzip is a clean implementation of the -LZMA algorithm. +one of gzip or bzip2. Lzip is about as fast as gzip, compresses most +files more than bzip2, and is better than both from a data recovery +perspective. Lzip is a clean implementation of the LZMA algorithm. The lzip file format is designed for long-term data archiving and -provides very safe integrity checking. The member trailer stores the -32-bit CRC of the original data, the size of the original data and the -size of the member. These values, together with the value remaining in -the range decoder and the end-of-stream marker, provide a 4 factor -integrity checking which guarantees that the decompressed version of the -data is identical to the original. This guards against corruption of the -compressed data, and against undetected bugs in lzip (hopefully very -unlikely). The chances of data corruption going undetected are -microscopic. Be aware, though, that the check occurs upon decompression, -so it can only tell you that something is wrong. It can't help you -recover the original uncompressed data. +provides very safe integrity checking. It is as simple as possible (but +not simpler), so that with the only help of the lzip manual it would be +possible for a digital archaeologist to extract the data from a lzip +file long after quantum computers eventually render LZMA obsolete. +Additionally lzip is copylefted, which guarantees that it will remain +free forever. + + The member trailer stores the 32-bit CRC of the original data, the +size of the original data and the size of the member. These values, +together with the value remaining in the range decoder and the +end-of-stream marker, provide a 4 factor integrity checking which +guarantees that the decompressed version of the data is identical to +the original. This guards against corruption of the compressed data, +and against undetected bugs in lzip (hopefully very unlikely). The +chances of data corruption going undetected are microscopic. Be aware, +though, that the check occurs upon decompression, so it can only tell +you that something is wrong. It can't help you recover the original +uncompressed data. If you ever need to recover data from a damaged lzip file, try the lziprecover program. Lziprecover makes lzip files resistant to bit-flip @@ -66,12 +73,23 @@ of a file. which makes it safer than compressors returning ambiguous warning values (like gzip) when it is used as a back end for tar or zutils. - Lzip replaces every file given in the command line with a compressed -version of itself, with the name "original_name.lz". Each compressed -file has the same modification date, permissions, and, when possible, -ownership as the corresponding original, so that these properties can be -correctly restored at decompression time. Lzip is able to read from some -types of non regular files if the `--stdout' option is specified. + When compressing, lzip replaces every file given in the command line +with a compressed version of itself, with the name "original_name.lz". +When decompressing, lzip attempts to guess the name for the decompressed +file from that of the compressed file as follows: + +filename.lz becomes filename +filename.tlz becomes filename.tar +anyothername becomes anyothername.out + + (De)compressing a file is much like copying or moving it; therefore +lzip preserves the access and modification dates, permissions, and, when +possible, ownership of the file just as "cp -p" does. (If the user ID or +the group ID can't be duplicated, the file permission bits S_ISUID and +S_ISGID are cleared). + + Lzip is able to read from some types of non regular files if the +'--stdout' option is specified. If no file names are specified, lzip compresses (or decompresses) from standard input to standard output. In this case, lzip will decline @@ -96,22 +114,15 @@ large, about 64 PiB each. The amount of memory required for compression is about 1 or 2 times the dictionary size limit (1 if input file size is less than dictionary size limit, else 2) plus 9 times the dictionary size really used. The -option `-0' is special and only requires about 1.5 MiB at most. The -amount of memory required for decompression is only a few tens of KiB -larger than the dictionary size really used. +option '-0' is special and only requires about 1.5 MiB at most. The +amount of memory required for decompression is about 46 kB larger than +the dictionary size really used. Lzip will automatically use the smallest possible dictionary size without exceeding the given limit. Keep in mind that the decompression memory requirement is affected at compression time by the choice of dictionary size limit. - When decompressing, lzip attempts to guess the name for the -decompressed file from that of the compressed file as follows: - -filename.lz becomes filename -filename.tlz becomes filename.tar -anyothername becomes anyothername.out - File: lzip.info, Node: Algorithm, Next: Invoking lzip, Prev: Introduction, Up: Top @@ -157,7 +168,7 @@ price represents the number of output bits produced. 6) The range encoder encodes the sequence produced by the main encoder and sends the produced bytes to the output stream. - 7) Go back to step 3 until the input data is finished or until the + 7) Go back to step 3 until the input data are finished or until the member or volume size limits are reached. 8) The range encoder is flushed. @@ -185,67 +196,67 @@ The format for running lzip is: Lzip supports the following options: -`-h' -`--help' +'-h' +'--help' Print an informative help message describing the options and exit. -`-V' -`--version' +'-V' +'--version' Print the version number of lzip on the standard output and exit. -`-b BYTES' -`--member-size=BYTES' +'-b BYTES' +'--member-size=BYTES' Set the member size limit to BYTES. A small member size may degrade compression ratio, so use it only when needed. Valid values range from 100 kB to 64 PiB. Defaults to 64 PiB. -`-c' -`--stdout' +'-c' +'--stdout' Compress or decompress to standard output. Needed when reading from a named pipe (fifo) or from a device. Use it to recover as much of the uncompressed data as possible when decompressing a corrupt file. -`-d' -`--decompress' +'-d' +'--decompress' Decompress. -`-f' -`--force' +'-f' +'--force' Force overwrite of output files. -`-F' -`--recompress' - Force recompression of files whose name already has the `.lz' or - `.tlz' suffix. +'-F' +'--recompress' + Force recompression of files whose name already has the '.lz' or + '.tlz' suffix. -`-k' -`--keep' +'-k' +'--keep' Keep (don't delete) input files during compression or decompression. -`-m BYTES' -`--match-length=BYTES' +'-m BYTES' +'--match-length=BYTES' Set the match length limit in bytes. After a match this long is found, the search is finished. Valid values range from 5 to 273. Larger values usually give better compression ratios but longer compression times. -`-o FILE' -`--output=FILE' - When reading from standard input and `--stdout' has not been - specified, use `FILE' as the virtual name of the uncompressed - file. This produces a file named `FILE' when decompressing, a file - named `FILE.lz' when compressing, and several files named - `FILE00001.lz', `FILE00002.lz', etc, when compressing and +'-o FILE' +'--output=FILE' + When reading from standard input and '--stdout' has not been + specified, use 'FILE' as the virtual name of the uncompressed + file. This produces a file named 'FILE' when decompressing, a file + named 'FILE.lz' when compressing, and several files named + 'FILE00001.lz', 'FILE00002.lz', etc, when compressing and splitting the output in volumes. -`-q' -`--quiet' +'-q' +'--quiet' Quiet operation. Suppress all messages. -`-s BYTES' -`--dictionary-size=BYTES' +'-s BYTES' +'--dictionary-size=BYTES' Set the dictionary size limit in bytes. Valid values range from 4 KiB to 512 MiB. Lzip will use the smallest possible dictionary size for each member without exceeding this limit. Note that @@ -258,43 +269,43 @@ The format for running lzip is: requirement is affected at compression time by the choice of dictionary size limit. -`-S BYTES' -`--volume-size=BYTES' +'-S BYTES' +'--volume-size=BYTES' Split the compressed output into several volume files with names - `original_name00001.lz', `original_name00002.lz', etc, and set the + 'original_name00001.lz', 'original_name00002.lz', etc, and set the volume size limit to BYTES. Each volume is a complete, maybe multi-member, lzip file. A small volume size may degrade compression ratio, so use it only when needed. Valid values range from 100 kB to 4 EiB. -`-t' -`--test' +'-t' +'--test' Check integrity of the specified file(s), but don't decompress them. This really performs a trial decompression and throws away - the result. Use it together with `-v' to see information about + the result. Use it together with '-v' to see information about the file. -`-v' -`--verbose' +'-v' +'--verbose' Verbose mode. When compressing, show the compression ratio for each file - processed. A second `-v' shows the progress of compression. + processed. A second '-v' shows the progress of compression. When decompressing or testing, further -v's (up to 4) increase the verbosity level, showing status, compression ratio, dictionary size, trailer contents (CRC, data size, member size), and up to 6 bytes of trailing garbage (if any). -`-0 .. -9' +'-0 .. -9' Set the compression parameters (dictionary size and match length - limit) as shown in the table below. Note that `-9' can be much - slower than `-0'. These options have no effect when decompressing. + limit) as shown in the table below. Note that '-9' can be much + slower than '-0'. These options have no effect when decompressing. The bidimensional parameter space of LZMA can't be mapped to a linear scale optimal for all files. If your files are large, very - repetitive, etc, you may need to use the `--match-length' and - `--dictionary-size' options directly to achieve optimal - performance. For example, `-9m64' usually compresses executables - more (and faster) than `-9'. + repetitive, etc, you may need to use the '--match-length' and + '--dictionary-size' options directly to achieve optimal + performance. For example, '-9m64' usually compresses executables + more (and faster) than '-9'. Level Dictionary size Match length limit -0 64 KiB 16 bytes @@ -308,13 +319,13 @@ The format for running lzip is: -8 24 MiB 132 bytes -9 32 MiB 273 bytes -`--fast' -`--best' +'--fast' +'--best' Aliases for GNU gzip compatibility. Numbers given as arguments to options may be followed by a multiplier -and an optional `B' for "byte". +and an optional 'B' for "byte". Table of SI and binary prefixes (unit multipliers): @@ -369,15 +380,15 @@ additional information before, between, or after them. All multibyte values are stored in little endian order. -`ID string' +'ID string' A four byte string, identifying the lzip format, with the value "LZIP" (0x4C, 0x5A, 0x49, 0x50). -`VN (version number, 1 byte)' +'VN (version number, 1 byte)' Just in case something needs to be modified in the future. 1 for now. -`DS (coded dictionary size, 1 byte)' +'DS (coded dictionary size, 1 byte)' Lzip divides the distance between any two powers of 2 into 8 equally spaced intervals, named "wedges". The dictionary size is calculated by taking a power of 2 (the base size) and substracting @@ -389,18 +400,18 @@ additional information before, between, or after them. Example: 0xD3 = 2^19 - 6 * 2^15 = 512 KiB - 6 * 32 KiB = 320 KiB Valid values for dictionary size range from 4 KiB to 512 MiB. -`Lzma stream' +'Lzma stream' The lzma stream, finished by an end of stream marker. Uses default - values for encoder properties. See the chapter `Stream format' + values for encoder properties. See the chapter 'Stream format' (*note Stream format::) for a complete description. -`CRC32 (4 bytes)' +'CRC32 (4 bytes)' CRC of the uncompressed original data. -`Data size (8 bytes)' +'Data size (8 bytes)' Size of the uncompressed original data. -`Member size (8 bytes)' +'Member size (8 bytes)' Total size of the member, including header and trailer. This field acts as a distributed index, allows the verification of stream integrity, and facilitates safe recovery of undamaged members from @@ -415,9 +426,9 @@ File: lzip.info, Node: Stream format, Next: Examples, Prev: File format, Up: The LZMA algorithm has three parameters, called "special LZMA properties", to adjust it for some kinds of binary data. These -parameters are; `literal_context_bits' (with a default value of 3), -`literal_pos_state_bits' (with a default value of 0), and -`pos_state_bits' (with a default value of 2). As a general purpose +parameters are; 'literal_context_bits' (with a default value of 3), +'literal_pos_state_bits' (with a default value of 0), and +'pos_state_bits' (with a default value of 2). As a general purpose compressor, lzip only uses the default values for these parameters. Lzip also finishes the LZMA stream with an "End Of Stream" marker @@ -473,7 +484,7 @@ Bit sequence Name Description In the following tables, multi-bit sequences are coded in normal order, from MSB to LSB, except where noted otherwise. - Lengths (the `len' in the table above) are coded as follows: + Lengths (the 'len' in the table above) are coded as follows: Bit sequence Description -------------------------------------------------------------------------- @@ -485,13 +496,13 @@ Bit sequence Description The coding of distances is a little more complicated. LZMA divides the interval between any two powers of 2 into 2 halves, named slots. As possible distances range from 0 to (2^32 - 1), there are 64 slots (0 to -63). The slot number is context-coded in 6 bits. `direct_bits' are the +63). The slot number is context-coded in 6 bits. 'direct_bits' are the remaining bits (from 0 to 30) needed to form a complete distance, and are calculated as (slot >> 1) - 1. If a distance needs 6 or more direct_bits, the last 4 bits are coded separately. The last piece (direct_bits for distances 4 to 127 or the last 4 bits for distances >= 128) is context-coded in reverse order (from LSB to MSB). For distances ->= 128, the `direct_bits - 4' part is coded with fixed 0.5 probability. +>= 128, the 'direct_bits - 4' part is coded with fixed 0.5 probability. Bit sequence Description -------------------------------------------------------------------------- @@ -503,40 +514,40 @@ slot + (direct_bits - 4) + 4 bits distances from 128 to 2^32 - 1 5.2 The coding contexts ======================= -These contexts (`Bit_model' in the source), are integers or arrays of +These contexts ('Bit_model' in the source), are integers or arrays of integers representing the probability of the corresponding bit being 0. The indices used in these arrays are: -`state' - A state machine (`State' in the source) with 12 states (0 to 11), +'state' + A state machine ('State' in the source) with 12 states (0 to 11), coding the latest 2 to 4 types of sequences processed. The initial state is 0. -`pos_state' +'pos_state' Value of the 2 least significant bits of the current position in the decoded data. -`literal_state' +'literal_state' Value of the 3 most significant bits of the latest byte decoded. -`len_state' +'len_state' Coded value of length (length - 2), with a maximum of 3. The resulting value is in the range 0 to 3. - In the following table, `!literal' is any sequence except a literal -byte. `rep' is any one of `rep0', `rep1', `rep2' or `rep3'. The types + In the following table, '!literal' is any sequence except a literal +byte. 'rep' is any one of 'rep0', 'rep1', 'rep2' or 'rep3'. The types of previous sequences corresponding to each state are: State Types of previous sequences ----------------------------------------------- +-------------------------------------------------------- 0 literal, literal, literal 1 match, literal, literal -2 (rep or shortrep), literal, literal +2 rep or (!literal, shortrep), literal, literal 3 literal, shortrep, literal, literal 4 match, literal -5 (rep or shortrep), literal +5 rep or (!literal, shortrep), literal 6 literal, shortrep, literal 7 literal, match 8 literal, rep @@ -567,9 +578,9 @@ bm_align reverse bit tree for distances >= 128, after fixed probability bits - There are two separate sets of contexts for lengths (`Len_model' in + There are two separate sets of contexts for lengths ('Len_model' in the source). One for normal matches, the other for repeated matches. The -contexts in each Len_model are (see `decode_len' in the source): +contexts in each Len_model are (see 'decode_len' in the source): Name Indices Used when --------------------------------------------------------------------------- @@ -580,33 +591,33 @@ bm_mid pos_state, bit tree after sequence 10 bm_high bit tree after sequence 11 - The context array `bm_literal' is special. In principle it acts as a -normal bit tree context, the one selected by `literal_state'. But if + The context array 'bm_literal' is special. In principle it acts as a +normal bit tree context, the one selected by 'literal_state'. But if the previous decoded byte was not a literal, two other bit tree -contexts are used depending on the value of each bit in `match_byte' +contexts are used depending on the value of each bit in 'match_byte' (the byte at the latest used distance), until a bit is decoded that is -different from its corresponding bit in `match_byte'. After the first +different from its corresponding bit in 'match_byte'. After the first difference is found, the rest of the byte is decoded using the normal -bit tree context. (See `decode_matched' in the source). +bit tree context. (See 'decode_matched' in the source). 5.3 The range decoder ===================== The LZMA stream is consumed one byte at a time by the range decoder. -(See `normalize' in the source). Every byte consumed produces a +(See 'normalize' in the source). Every byte consumed produces a variable number of decoded bits, depending on how well these bits agree -with their context. (See `decode_bit' in the source). +with their context. (See 'decode_bit' in the source). The range decoder state consists of two unsigned 32-bit variables, -`range' (representing the most significant part of the range size not -yet decoded), and `code' (representing the current point within -`range'). `range' is initialized to (2^32 - 1), and `code' is +'range' (representing the most significant part of the range size not +yet decoded), and 'code' (representing the current point within +'range'). 'range' is initialized to (2^32 - 1), and 'code' is initialized to 0. The range encoder produces a first 0 byte that must be ignored by the range decoder. This is done by shifting 5 bytes in the initialization of -`code' instead of 4. (See the `Range_decoder' constructor in the +'code' instead of 4. (See the 'Range_decoder' constructor in the source). @@ -615,7 +626,7 @@ source). After decoding the member header and obtaining the dictionary size, the range decoder is initialized and then the LZMA decoder enters a loop -(See `decode_member' in the source) where it invokes the range decoder +(See 'decode_member' in the source) where it invokes the range decoder with the appropiate contexts to decode the different coding sequences (matches, repeated matches, and literal bytes), until the "End Of Stream" marker is decoded. @@ -628,49 +639,49 @@ File: lzip.info, Node: Examples, Next: Problems, Prev: Stream format, Up: To WARNING! Even if lzip is bug-free, other causes may result in a corrupt compressed file (bugs in the system libraries, memory errors, etc). -Therefore, if the data you are going to compress is important, give the -`--keep' option to lzip and do not remove the original file until you +Therefore, if the data you are going to compress are important, give the +'--keep' option to lzip and do not remove the original file until you verify the compressed file with a command like -`lzip -cd file.lz | cmp file -'. +'lzip -cd file.lz | cmp file -'. -Example 1: Replace a regular file with its compressed version `file.lz' +Example 1: Replace a regular file with its compressed version 'file.lz' and show the compression ratio. lzip -v file -Example 2: Like example 1 but the created `file.lz' is multi-member +Example 2: Like example 1 but the created 'file.lz' is multi-member with a member size of 1 MiB. The compression ratio is not shown. lzip -b 1MiB file Example 3: Restore a regular file from its compressed version -`file.lz'. If the operation is successful, `file.lz' is removed. +'file.lz'. If the operation is successful, 'file.lz' is removed. lzip -d file.lz -Example 4: Verify the integrity of the compressed file `file.lz' and +Example 4: Verify the integrity of the compressed file 'file.lz' and show status. lzip -tv file.lz Example 5: Compress a whole floppy in /dev/fd0 and send the output to -`file.lz'. +'file.lz'. lzip -c /dev/fd0 > file.lz -Example 6: Decompress `file.lz' partially until 10 KiB of decompressed +Example 6: Decompress 'file.lz' partially until 10 KiB of decompressed data are produced. lzip -cd file.lz | dd bs=1024 count=10 -Example 7: Decompress `file.lz' partially from decompressed byte 10000 +Example 7: Decompress 'file.lz' partially from decompressed byte 10000 to decompressed byte 15000 (5000 bytes are produced). lzip -cd file.lz | dd bs=1000 skip=10 count=5 @@ -706,7 +717,7 @@ for all eternity, if not longer. If you find a bug in lzip, please send electronic mail to <lzip-bug@nongnu.org>. Include the version number, which you can find -by running `lzip --version'. +by running 'lzip --version'. File: lzip.info, Node: Reference source code, Next: Concept index, Prev: Problems, Up: Top @@ -715,7 +726,7 @@ Appendix A Reference source code ******************************** /* Lzd - Educational decompressor for lzip files - Copyright (C) 2013 Antonio Diaz Diaz. + Copyright (C) 2013, 2014 Antonio Diaz Diaz. This program is free software: you have unlimited permission to copy, distribute and modify it. @@ -925,7 +936,7 @@ public: break; } } - return symbol - 0x100; + return symbol & 0xFF; } int decode_len( Len_model & lm, const int pos_state ) @@ -1092,7 +1103,7 @@ bool LZ_decoder::decode_member() // Returns false if error } } state.set_match(); - if( rep0 >= dictionary_size || ( rep0 >= pos && !partial_data_pos ) ) + if( rep0 >= dictionary_size || rep0 >= data_position() ) return false; } for( int i = 0; i < len; ++i ) @@ -1114,7 +1125,7 @@ int main( const int argc, const char * const argv[] ) "It is not safe to use lzd for any real work.\n" "\nUsage: %s < file.lz > file\n", argv[0] ); std::printf( "Lzd decompresses from standard input to standard output.\n" - "\nCopyright (C) 2013 Antonio Diaz Diaz.\n" + "\nCopyright (C) 2014 Antonio Diaz Diaz.\n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n" "Report bugs to lzip-bug@nongnu.org\n" @@ -1125,8 +1136,7 @@ int main( const int argc, const char * const argv[] ) for( bool first_member = true; ; first_member = false ) { File_header header; - for( int i = 0; i < 6; ++i ) - header[i] = std::getc( stdin ); + for( int i = 0; i < 6; ++i ) header[i] = std::getc( stdin ); if( std::feof( stdin ) || std::memcmp( header, "LZIP", 4 ) != 0 ) { if( first_member ) @@ -1191,16 +1201,16 @@ Concept index Tag Table: -Node: Top210 -Node: Introduction1052 -Node: Algorithm5031 -Node: Invoking lzip7524 -Node: File format13202 -Node: Stream format15750 -Node: Examples25150 -Node: Problems27106 -Node: Reference source code27636 -Node: Concept index41139 +Node: Top208 +Node: Introduction1059 +Node: Algorithm5466 +Node: Invoking lzip7960 +Node: File format13638 +Node: Stream format16186 +Node: Examples25616 +Node: Problems27573 +Node: Reference source code28103 +Node: Concept index41592 End Tag Table diff --git a/doc/lzip.texinfo b/doc/lzip.texi index cfc9138..957af34 100644 --- a/doc/lzip.texinfo +++ b/doc/lzip.texi @@ -6,8 +6,8 @@ @finalout @c %**end of header -@set UPDATED 20 September 2013 -@set VERSION 1.15 +@set UPDATED 11 January 2014 +@set VERSION 1.16-pre1 @dircategory Data Compression @direntry @@ -47,7 +47,7 @@ This manual is for Lzip (version @value{VERSION}, @value{UPDATED}). @end menu @sp 1 -Copyright @copyright{} 2008, 2009, 2010, 2011, 2012, 2013 +Copyright @copyright{} 2008, 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz. This manual is free documentation: you have unlimited permission @@ -59,23 +59,28 @@ to copy, distribute and modify it. @cindex introduction Lzip is a lossless data compressor with a user interface similar to the -one of gzip or bzip2. Lzip decompresses almost as fast as gzip and -compresses more than bzip2, which makes it well suited for software -distribution and data archiving. Lzip is a clean implementation of the -LZMA algorithm. +one of gzip or bzip2. Lzip is about as fast as gzip, compresses most +files more than bzip2, and is better than both from a data recovery +perspective. Lzip is a clean implementation of the LZMA algorithm. The lzip file format is designed for long-term data archiving and -provides very safe integrity checking. The member trailer stores the -32-bit CRC of the original data, the size of the original data and the -size of the member. These values, together with the value remaining in -the range decoder and the end-of-stream marker, provide a 4 factor -integrity checking which guarantees that the decompressed version of the -data is identical to the original. This guards against corruption of the -compressed data, and against undetected bugs in lzip (hopefully very -unlikely). The chances of data corruption going undetected are -microscopic. Be aware, though, that the check occurs upon decompression, -so it can only tell you that something is wrong. It can't help you -recover the original uncompressed data. +provides very safe integrity checking. It is as simple as possible (but +not simpler), so that with the only help of the lzip manual it would be +possible for a digital archaeologist to extract the data from a lzip +file long after quantum computers eventually render LZMA obsolete. +Additionally lzip is copylefted, which guarantees that it will remain +free forever. + +The member trailer stores the 32-bit CRC of the original data, the size +of the original data and the size of the member. These values, together +with the value remaining in the range decoder and the end-of-stream +marker, provide a 4 factor integrity checking which guarantees that the +decompressed version of the data is identical to the original. This +guards against corruption of the compressed data, and against undetected +bugs in lzip (hopefully very unlikely). The chances of data corruption +going undetected are microscopic. Be aware, though, that the check +occurs upon decompression, so it can only tell you that something is +wrong. It can't help you recover the original uncompressed data. If you ever need to recover data from a damaged lzip file, try the lziprecover program. Lziprecover makes lzip files resistant to bit-flip @@ -87,12 +92,25 @@ Lzip uses the same well-defined exit status values used by bzip2, which makes it safer than compressors returning ambiguous warning values (like gzip) when it is used as a back end for tar or zutils. -Lzip replaces every file given in the command line with a compressed -version of itself, with the name "original_name.lz". Each compressed -file has the same modification date, permissions, and, when possible, -ownership as the corresponding original, so that these properties can be -correctly restored at decompression time. Lzip is able to read from some -types of non regular files if the @samp{--stdout} option is specified. +When compressing, lzip replaces every file given in the command line +with a compressed version of itself, with the name "original_name.lz". +When decompressing, lzip attempts to guess the name for the decompressed +file from that of the compressed file as follows: + +@multitable {anyothername} {becomes} {anyothername.out} +@item filename.lz @tab becomes @tab filename +@item filename.tlz @tab becomes @tab filename.tar +@item anyothername @tab becomes @tab anyothername.out +@end multitable + +(De)compressing a file is much like copying or moving it; therefore lzip +preserves the access and modification dates, permissions, and, when +possible, ownership of the file just as "cp -p" does. (If the user ID or +the group ID can't be duplicated, the file permission bits S_ISUID and +S_ISGID are cleared). + +Lzip is able to read from some types of non regular files if the +@samp{--stdout} option is specified. If no file names are specified, lzip compresses (or decompresses) from standard input to standard output. In this case, lzip will decline to @@ -118,23 +136,14 @@ The amount of memory required for compression is about 1 or 2 times the dictionary size limit (1 if input file size is less than dictionary size limit, else 2) plus 9 times the dictionary size really used. The option @samp{-0} is special and only requires about 1.5 MiB at most. The amount -of memory required for decompression is only a few tens of KiB larger -than the dictionary size really used. +of memory required for decompression is about 46 kB larger than the +dictionary size really used. Lzip will automatically use the smallest possible dictionary size without exceeding the given limit. Keep in mind that the decompression memory requirement is affected at compression time by the choice of dictionary size limit. -When decompressing, lzip attempts to guess the name for the decompressed -file from that of the compressed file as follows: - -@multitable {anyothername} {becomes} {anyothername.out} -@item filename.lz @tab becomes @tab filename -@item filename.tlz @tab becomes @tab filename.tar -@item anyothername @tab becomes @tab anyothername.out -@end multitable - @node Algorithm @chapter Algorithm @@ -179,7 +188,7 @@ price represents the number of output bits produced. 6) The range encoder encodes the sequence produced by the main encoder and sends the produced bytes to the output stream. -7) Go back to step 3 until the input data is finished or until the +7) Go back to step 3 until the input data are finished or until the member or volume size limits are reached. 8) The range encoder is flushed. @@ -566,14 +575,14 @@ byte. @samp{rep} is any one of @samp{rep0}, @samp{rep1}, @samp{rep2} or @samp{rep3}. The types of previous sequences corresponding to each state are: -@multitable {State} {literal, shortrep, literal, literal} +@multitable {State} {rep or (!literal, shortrep), literal, literal} @headitem State @tab Types of previous sequences @item 0 @tab literal, literal, literal @item 1 @tab match, literal, literal -@item 2 @tab (rep or shortrep), literal, literal +@item 2 @tab rep or (!literal, shortrep), literal, literal @item 3 @tab literal, shortrep, literal, literal @item 4 @tab match, literal -@item 5 @tab (rep or shortrep), literal +@item 5 @tab rep or (!literal, shortrep), literal @item 6 @tab literal, shortrep, literal @item 7 @tab literal, match @item 8 @tab literal, rep @@ -667,7 +676,7 @@ Of Stream" marker is decoded. WARNING! Even if lzip is bug-free, other causes may result in a corrupt compressed file (bugs in the system libraries, memory errors, etc). -Therefore, if the data you are going to compress is important, give the +Therefore, if the data you are going to compress are important, give the @samp{--keep} option to lzip and do not remove the original file until you verify the compressed file with a command like @w{@samp{lzip -cd file.lz | cmp file -}}. @@ -785,7 +794,7 @@ find by running @w{@samp{lzip --version}}. @verbatim /* Lzd - Educational decompressor for lzip files - Copyright (C) 2013 Antonio Diaz Diaz. + Copyright (C) 2013, 2014 Antonio Diaz Diaz. This program is free software: you have unlimited permission to copy, distribute and modify it. @@ -995,7 +1004,7 @@ public: break; } } - return symbol - 0x100; + return symbol & 0xFF; } int decode_len( Len_model & lm, const int pos_state ) @@ -1162,7 +1171,7 @@ bool LZ_decoder::decode_member() // Returns false if error } } state.set_match(); - if( rep0 >= dictionary_size || ( rep0 >= pos && !partial_data_pos ) ) + if( rep0 >= dictionary_size || rep0 >= data_position() ) return false; } for( int i = 0; i < len; ++i ) @@ -1184,7 +1193,7 @@ int main( const int argc, const char * const argv[] ) "It is not safe to use lzd for any real work.\n" "\nUsage: %s < file.lz > file\n", argv[0] ); std::printf( "Lzd decompresses from standard input to standard output.\n" - "\nCopyright (C) 2013 Antonio Diaz Diaz.\n" + "\nCopyright (C) 2014 Antonio Diaz Diaz.\n" "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n" "Report bugs to lzip-bug@nongnu.org\n" @@ -1195,8 +1204,7 @@ int main( const int argc, const char * const argv[] ) for( bool first_member = true; ; first_member = false ) { File_header header; - for( int i = 0; i < 6; ++i ) - header[i] = std::getc( stdin ); + for( int i = 0; i < 6; ++i ) header[i] = std::getc( stdin ); if( std::feof( stdin ) || std::memcmp( header, "LZIP", 4 ) != 0 ) { if( first_member ) @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -60,9 +61,9 @@ void Matchfinder_base::normalize_pos() pos -= offset; stream_pos -= offset; for( int i = 0; i < num_prev_positions; ++i ) - if( prev_positions[i] >= 0 ) prev_positions[i] -= offset; + prev_positions[i] -= std::min( prev_positions[i], offset ); for( int i = 0; i < pos_array_size; ++i ) - if( pos_array[i] >= 0 ) pos_array[i] -= offset; + pos_array[i] -= std::min( pos_array[i], offset ); read_block(); } } @@ -70,12 +71,11 @@ void Matchfinder_base::normalize_pos() Matchfinder_base::Matchfinder_base( const int before, const int dict_size, const int after_size, const int dict_factor, - const int match_len_limit, const int num_prev_positions23, + const int num_prev_positions23, const int pos_array_factor, const int ifd ) : partial_data_pos( 0 ), before_size( before ), - match_len_limit_( match_len_limit ), pos( 0 ), cyclic_pos( 0 ), stream_pos( 0 ), @@ -113,19 +113,19 @@ Matchfinder_base::Matchfinder_base( const int before, const int dict_size, prev_positions = new( std::nothrow ) int32_t[size]; if( !prev_positions ) { std::free( buffer ); throw std::bad_alloc(); } pos_array = prev_positions + num_prev_positions; - for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = -1; + for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = 0; } void Matchfinder_base::reset() { - const int size = stream_pos - pos; - if( size > 0 ) std::memmove( buffer, buffer + pos, size ); + if( stream_pos > pos ) + std::memmove( buffer, buffer + pos, stream_pos - pos ); partial_data_pos = 0; stream_pos -= pos; pos = 0; cyclic_pos = 0; - for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = -1; + for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = 0; read_block(); } @@ -139,14 +139,15 @@ int Matchfinder::get_match_pairs( struct Pair * pairs ) if( len_limit < 4 ) return 0; } - int maxlen = min_match_len - 1; + int maxlen = 0; int num_pairs = 0; + const int pos1 = pos + 1; const int min_pos = ( pos > dictionary_size_ ) ? pos - dictionary_size_ : 0; const uint8_t * const data = buffer + pos; unsigned tmp = crc32[data[0]] ^ data[1]; const int key2 = tmp & ( num_prev_positions2 - 1 ); - tmp ^= (uint32_t)data[2] << 8; + tmp ^= (unsigned)data[2] << 8; const int key3 = num_prev_positions2 + ( tmp & ( num_prev_positions3 - 1 ) ); const int key4 = num_prev_positions2 + num_prev_positions3 + ( ( tmp ^ ( crc32[data[3]] << 5 ) ) & key4_mask ); @@ -155,34 +156,34 @@ int Matchfinder::get_match_pairs( struct Pair * pairs ) { int np2 = prev_positions[key2]; int np3 = prev_positions[key3]; - if( np2 >= min_pos && buffer[np2] == data[0] ) + if( np2 > min_pos && buffer[np2-1] == data[0] ) { - pairs[0].dis = pos - np2 - 1; + pairs[0].dis = pos - np2; pairs[0].len = maxlen = 2; num_pairs = 1; } - if( np2 != np3 && np3 >= min_pos && buffer[np3] == data[0] ) + if( np2 != np3 && np3 > min_pos && buffer[np3-1] == data[0] ) { maxlen = 3; - pairs[num_pairs].dis = pos - np3 - 1; - ++num_pairs; np2 = np3; + pairs[num_pairs].dis = pos - np2; + ++num_pairs; } if( num_pairs > 0 ) { - const int delta = pos - np2; + const int delta = pos1 - np2; while( maxlen < len_limit && data[maxlen-delta] == data[maxlen] ) ++maxlen; pairs[num_pairs-1].len = maxlen; - if( maxlen >= len_limit ) pairs = 0; + if( maxlen >= len_limit ) pairs = 0; // done. now just skip } if( maxlen < 3 ) maxlen = 3; } - prev_positions[key2] = pos; - prev_positions[key3] = pos; + prev_positions[key2] = pos1; + prev_positions[key3] = pos1; int newpos = prev_positions[key4]; - prev_positions[key4] = pos; + prev_positions[key4] = pos1; int32_t * ptr0 = pos_array + ( cyclic_pos << 1 ); int32_t * ptr1 = ptr0 + 1; @@ -190,9 +191,9 @@ int Matchfinder::get_match_pairs( struct Pair * pairs ) for( int count = cycles; ; ) { - if( newpos < min_pos || --count < 0 ) { *ptr0 = *ptr1 = -1; break; } + if( newpos <= min_pos || --count < 0 ) { *ptr0 = *ptr1 = 0; break; } - const int delta = pos - newpos; + const int delta = pos1 - newpos; int32_t * const newptr = pos_array + ( ( cyclic_pos - delta + ( ( cyclic_pos >= delta ) ? 0 : dictionary_size_ + 1 ) ) << 1 ); @@ -251,7 +252,7 @@ void Len_encoder::encode( Range_encoder & renc, int symbol, if( symbol < len_low_symbols ) { renc.encode_bit( choice1, 0 ); - renc.encode_tree( bm_low[pos_state], symbol, len_low_bits ); + renc.encode_tree3( bm_low[pos_state], symbol ); } else { @@ -259,14 +260,12 @@ void Len_encoder::encode( Range_encoder & renc, int symbol, if( symbol < len_low_symbols + len_mid_symbols ) { renc.encode_bit( choice2, 0 ); - renc.encode_tree( bm_mid[pos_state], symbol - len_low_symbols, - len_mid_bits ); + renc.encode_tree3( bm_mid[pos_state], symbol - len_low_symbols ); } else { renc.encode_bit( choice2, 1 ); - renc.encode_tree( bm_high, symbol - len_low_symbols - len_mid_symbols, - len_high_bits ); + renc.encode_tree8( bm_high, symbol - len_low_symbols - len_mid_symbols ); } } if( --counters[pos_state] <= 0 ) update_prices( pos_state ); @@ -318,10 +317,10 @@ void LZ_encoder::fill_distance_prices() int * const dsp = dis_slot_prices[len_state]; const Bit_model * const bmds = bm_dis_slot[len_state]; int slot = 0; - for( ; slot < end_dis_model && slot < num_dis_slots; ++slot ) - dsp[slot] = price_symbol( bmds, slot, dis_slot_bits ); + for( ; slot < end_dis_model; ++slot ) + dsp[slot] = price_symbol6( bmds, slot ); for( ; slot < num_dis_slots; ++slot ) - dsp[slot] = price_symbol( bmds, slot, dis_slot_bits ) + + dsp[slot] = price_symbol6( bmds, slot ) + (((( slot >> 1 ) - 1 ) - dis_align_bits ) << price_shift_bits ); int * const dp = dis_prices[len_state]; @@ -336,7 +335,7 @@ void LZ_encoder::fill_distance_prices() /* Return value == number of bytes advanced (ahead). trials[0]..trials[ahead-1] contain the steps to encode. - ( trials[0].dis == -1 && trials[0].price == 1 ) means literal. + ( trials[0].dis == -1 ) means literal. */ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], const State state ) @@ -376,12 +375,12 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], } const int pos_state = matchfinder.data_position() & pos_state_mask; - const uint8_t prev_byte = matchfinder[-1]; + const uint8_t prev_byte = matchfinder[1]; const uint8_t cur_byte = matchfinder[0]; - const uint8_t match_byte = matchfinder[-reps[0]-1]; + const uint8_t match_byte = matchfinder[reps[0]+1]; trials[0].state = state; - trials[1].dis = -1; + trials[1].dis = -1; // literal trials[1].price = price0( bm_match[state()][pos_state] ); if( state.is_char() ) trials[1].price += price_literal( prev_byte, cur_byte ); @@ -392,7 +391,7 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], const int rep_match_price = match_price + price1( bm_rep[state()] ); if( match_byte == cur_byte ) - trials[1].update( rep_match_price + price_rep_len1( state, pos_state ), 0, 0 ); + trials[1].update( rep_match_price + price_shortrep( state, pos_state ), 0, 0 ); num_trials = std::max( main_len, replens[rep_index] ); @@ -457,60 +456,50 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], // give final values to current trial Trial & cur_trial = trials[cur]; + State cur_state; + { + int dis = cur_trial.dis; int prev_index = cur_trial.prev_index; const int prev_index2 = cur_trial.prev_index2; - State cur_state; - if( prev_index2 != single_step_trial ) + if( prev_index2 == single_step_trial ) { - --prev_index; - if( prev_index2 >= 0 ) + cur_state = trials[prev_index].state; + if( prev_index + 1 == cur ) // len == 1 { - cur_state = trials[prev_index2].state; - if( cur_trial.dis2 < num_rep_distances ) - cur_state.set_rep(); - else - cur_state.set_match(); + if( dis == 0 ) cur_state.set_short_rep(); + else cur_state.set_char(); // literal } - else - cur_state = trials[prev_index].state; - cur_state.set_char(); + else if( dis < num_rep_distances ) cur_state.set_rep(); + else cur_state.set_match(); } - else - cur_state = trials[prev_index].state; - - if( prev_index == cur - 1 ) + else if( prev_index2 == dual_step_trial ) // dis == 0 { - if( cur_trial.dis == 0 ) cur_state.set_short_rep(); - else cur_state.set_char(); - for( int i = 0; i < num_rep_distances; ++i ) - cur_trial.reps[i] = trials[prev_index].reps[i]; + --prev_index; + cur_state = trials[prev_index].state; + cur_state.set_char(); + cur_state.set_rep(); } - else + else // if( prev_index2 >= 0 ) { - int dis; - if( prev_index2 >= 0 ) - { - dis = cur_trial.dis2; - prev_index = prev_index2; - cur_state.set_rep(); - } - else - { - dis = cur_trial.dis; - if( dis < num_rep_distances ) cur_state.set_rep(); - else cur_state.set_match(); - } - for( int i = 0; i < num_rep_distances; ++i ) - cur_trial.reps[i] = trials[prev_index].reps[i]; - mtf_reps( dis, cur_trial.reps ); + prev_index = prev_index2; + cur_state = trials[prev_index].state; + if( dis < num_rep_distances ) cur_state.set_rep(); + else cur_state.set_match(); + cur_state.set_char(); + cur_state.set_rep(); } cur_trial.state = cur_state; + for( int i = 0; i < num_rep_distances; ++i ) + cur_trial.reps[i] = trials[prev_index].reps[i]; + mtf_reps( dis, cur_trial.reps ); + } const int pos_state = matchfinder.data_position() & pos_state_mask; - const uint8_t prev_byte = matchfinder[-1]; + const uint8_t prev_byte = matchfinder[1]; const uint8_t cur_byte = matchfinder[0]; - const uint8_t match_byte = matchfinder[-cur_trial.reps[0]-1]; + const uint8_t match_byte = matchfinder[cur_trial.reps[0]+1]; + matchfinder.move_pos(); int next_price = cur_trial.price + price0( bm_match[cur_state()][pos_state] ); @@ -518,26 +507,25 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], next_price += price_literal( prev_byte, cur_byte ); else next_price += price_matched( prev_byte, cur_byte, match_byte ); - matchfinder.move_pos(); // try last updates to next trial Trial & next_trial = trials[cur+1]; - next_trial.update( next_price, -1, cur ); + next_trial.update( next_price, -1, cur ); // literal const int match_price = cur_trial.price + price1( bm_match[cur_state()][pos_state] ); const int rep_match_price = match_price + price1( bm_rep[cur_state()] ); - if( match_byte == cur_byte && next_trial.dis != 0 ) + if( match_byte == cur_byte && next_trial.dis != 0 && + next_trial.prev_index2 == single_step_trial ) { const int price = rep_match_price + - price_rep_len1( cur_state, pos_state ); + price_shortrep( cur_state, pos_state ); if( price <= next_trial.price ) { next_trial.price = price; next_trial.dis = 0; next_trial.prev_index = cur; - next_trial.prev_index2 = single_step_trial; } } @@ -567,7 +555,7 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], price_rep0_len( len, state2, pos_state2 ); while( num_trials < cur + 1 + len ) trials[++num_trials].price = infinite_price; - trials[cur+1+len].update2( price, 0, cur + 1 ); + trials[cur+1+len].update2( price, cur + 1 ); } } @@ -580,7 +568,7 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], int len; const int dis = cur_trial.reps[rep] + 1; - if( data[-dis] != data[0] || data[1-dis] != data[1] ) continue; + if( data[0-dis] != data[0] || data[1-dis] != data[1] ) continue; for( len = min_match_len; len < len_limit; ++len ) if( data[len-dis] != data[len] ) break; while( num_trials < cur + len ) @@ -612,7 +600,7 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], price_rep0_len( len2, state2, pos_state2 ); while( num_trials < cur + len + 1 + len2 ) trials[++num_trials].price = infinite_price; - trials[cur+len+1+len2].update3( price, 0, cur + len + 1, rep, cur ); + trials[cur+len+1+len2].update3( price, rep, cur + len + 1, cur ); } // try matches @@ -657,8 +645,8 @@ int LZ_encoder::sequence_optimizer( const int reps[num_rep_distances], while( num_trials < cur + len + 1 + len2 ) trials[++num_trials].price = infinite_price; - trials[cur+len+1+len2].update3( price, 0, cur + len + 1, - dis + num_rep_distances, cur ); + trials[cur+len+1+len2].update3( price, dis + num_rep_distances, + cur + len + 1, cur ); } if( ++i >= num_pairs ) break; dis = pairs[i].dis; @@ -675,9 +663,9 @@ bool LZ_encoder::encode_member( const unsigned long long member_size ) member_size - File_trailer::size() - max_marker_size; const int fill_count = ( matchfinder.match_len_limit() > 12 ) ? 128 : 512; int fill_counter = 0; - int rep_distances[num_rep_distances]; + int reps[num_rep_distances]; State state; - for( int i = 0; i < num_rep_distances; ++i ) rep_distances[i] = 0; + for( int i = 0; i < num_rep_distances; ++i ) reps[i] = 0; if( matchfinder.data_position() != 0 || renc.member_position() != File_header::size ) @@ -703,28 +691,28 @@ bool LZ_encoder::encode_member( const unsigned long long member_size ) if( align_price_count <= 0 ) fill_align_prices(); } - int ahead = sequence_optimizer( rep_distances, state ); + int ahead = sequence_optimizer( reps, state ); if( ahead <= 0 ) return false; // can't happen - for( int i = 0; ; ) + for( int i = 0; ahead > 0; ) { const int pos_state = ( matchfinder.data_position() - ahead ) & pos_state_mask; const int dis = trials[i].dis; const int len = trials[i].price; - bool bit = ( dis < 0 && len == 1 ); + bool bit = ( dis < 0 ); renc.encode_bit( bm_match[state()][pos_state], !bit ); if( bit ) // literal byte { - const uint8_t prev_byte = matchfinder[-ahead-1]; - const uint8_t cur_byte = matchfinder[-ahead]; + const uint8_t prev_byte = matchfinder[ahead+1]; + const uint8_t cur_byte = matchfinder[ahead]; crc32.update_byte( crc_, cur_byte ); if( state.is_char() ) encode_literal( prev_byte, cur_byte ); else { - const uint8_t match_byte = matchfinder[-ahead-rep_distances[0]-1]; + const uint8_t match_byte = matchfinder[ahead+reps[0]+1]; encode_matched( prev_byte, cur_byte, match_byte ); } state.set_char(); @@ -732,10 +720,10 @@ bool LZ_encoder::encode_member( const unsigned long long member_size ) else // match or repeated match { crc32.update_buf( crc_, matchfinder.ptr_to_current_pos() - ahead, len ); - mtf_reps( dis, rep_distances ); + mtf_reps( dis, reps ); bit = ( dis < num_rep_distances ); renc.encode_bit( bm_rep[state()], bit ); - if( bit ) + if( bit ) // repeated match { bit = ( dis == 0 ); renc.encode_bit( bm_rep0[state()], !bit ); @@ -754,7 +742,7 @@ bool LZ_encoder::encode_member( const unsigned long long member_size ) state.set_rep(); } } - else + else // match { encode_pair( dis - num_rep_distances, len, pos_state ); if( get_slot( dis - num_rep_distances ) >= end_dis_model ) @@ -770,7 +758,6 @@ bool LZ_encoder::encode_member( const unsigned long long member_size ) full_flush( matchfinder.data_position(), state ); return true; } - if( ahead <= 0 ) break; } } full_flush( matchfinder.data_position(), state ); @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -42,7 +43,7 @@ public: extern Dis_slots dis_slots; -inline uint8_t get_slot( const uint32_t dis ) +inline uint8_t get_slot( const unsigned dis ) { if( dis < (1 << 10) ) return dis_slots[dis]; if( dis < (1 << 19) ) return dis_slots[dis>> 9] + 18; @@ -91,17 +92,41 @@ inline int price_bit( const Bit_model bm, const int bit ) { if( bit ) return price1( bm ); else return price0( bm ); } -inline int price_symbol( const Bit_model bm[], int symbol, const int num_bits ) +inline int price_symbol3( const Bit_model bm[], int symbol ) { - int price = 0; - symbol |= ( 1 << num_bits ); - while( symbol > 1 ) - { - const int bit = symbol & 1; - symbol >>= 1; - price += price_bit( bm[symbol], bit ); - } - return price; + int bit = symbol & 1; + symbol |= 8; symbol >>= 1; + int price = price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + return price + price_bit( bm[1], symbol & 1 ); + } + + +inline int price_symbol6( const Bit_model bm[], int symbol ) + { + int bit = symbol & 1; + symbol |= 64; symbol >>= 1; + int price = price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + return price + price_bit( bm[1], symbol & 1 ); + } + + +inline int price_symbol8( const Bit_model bm[], int symbol ) + { + int bit = symbol & 1; + symbol |= 0x100; symbol >>= 1; + int price = price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + bit = symbol & 1; symbol >>= 1; price += price_bit( bm[symbol], bit ); + return price + price_bit( bm[1], symbol & 1 ); } @@ -121,18 +146,17 @@ inline int price_symbol_reversed( const Bit_model bm[], int symbol, } -inline int price_matched( const Bit_model bm[], unsigned symbol, - unsigned match_byte ) +inline int price_matched( const Bit_model bm[], int symbol, int match_byte ) { int price = 0; - unsigned mask = 0x100; - symbol |= 0x100; + int mask = 0x100; + symbol |= mask; do { match_byte <<= 1; - const unsigned match_bit = match_byte & mask; + const int match_bit = match_byte & mask; symbol <<= 1; - const unsigned bit = symbol & 0x100; + const int bit = symbol & 0x100; price += price_bit( bm[match_bit+(symbol>>9)+mask], bit ); mask &= ~(match_byte ^ symbol); // if( match_bit != bit ) mask = 0; } @@ -152,17 +176,16 @@ class Matchfinder_base protected: unsigned long long partial_data_pos; uint8_t * buffer; // input buffer - int32_t * prev_positions; // last seen position of key + int32_t * prev_positions; // 1 + last seen position of key. else 0 int32_t * pos_array; // may be tree or chain const int before_size; // bytes to keep in buffer before dictionary - const int match_len_limit_; int buffer_size; int dictionary_size_; // bytes to keep in buffer before pos int pos; // current pos in buffer int cyclic_pos; // cycles through [0, dictionary_size] - int pos_limit; // when reached, a new block must be read int stream_pos; // first byte not yet read from file - unsigned key4_mask; + int pos_limit; // when reached, a new block must be read + int key4_mask; int num_prev_positions; // size of prev_positions int pos_array_size; const int infd; // input file descriptor @@ -170,19 +193,19 @@ protected: Matchfinder_base( const int before, const int dict_size, const int after_size, const int dict_factor, - const int match_len_limit, const int num_prev_positions23, + const int num_prev_positions23, const int pos_array_factor, const int ifd ); ~Matchfinder_base() { delete[] prev_positions; std::free( buffer ); } public: - uint8_t operator[]( const int i ) const { return buffer[pos+i]; } + uint8_t operator[]( const int distance ) const + { return buffer[pos-distance]; } int available_bytes() const { return stream_pos - pos; } unsigned long long data_position() const { return partial_data_pos + pos; } int dictionary_size() const { return dictionary_size_; } bool finished() const { return at_stream_end && pos >= stream_pos; } - int match_len_limit() const { return match_len_limit_; } const uint8_t * ptr_to_current_pos() const { return buffer + pos; } int true_match_len( const int index, const int distance, int len_limit ) const @@ -224,14 +247,15 @@ class Matchfinder : public Matchfinder_base pos_array_factor = 2 }; const int cycles; + const int match_len_limit_; public: - Matchfinder( const int dict_size, const int match_len_limit, const int ifd ) + Matchfinder( const int dict_size, const int len_limit, const int ifd ) : Matchfinder_base( before_size, dict_size, after_size, dict_factor, - match_len_limit, num_prev_positions23, pos_array_factor, ifd ), - cycles( ( match_len_limit < max_match_len ) ? - 16 + ( match_len_limit / 2 ) : 256 ) + num_prev_positions23, pos_array_factor, ifd ), + cycles( ( len_limit < max_match_len ) ? 16 + ( len_limit / 2 ) : 256 ), + match_len_limit_( len_limit ) {} bool dec_pos( const int ahead ) @@ -243,6 +267,7 @@ public: return true; } + int match_len_limit() const { return match_len_limit_; } int get_match_pairs( struct Pair * pairs = 0 ); }; @@ -255,7 +280,7 @@ class Range_encoder uint8_t * const buffer; // output buffer int pos; // current pos in buffer uint32_t range; - int ff_count; + unsigned ff_count; const int outfd; // output file descriptor uint8_t cache; @@ -329,17 +354,42 @@ public: if( range <= 0x00FFFFFFU ) { range <<= 8; shift_low(); } } - void encode_tree( Bit_model bm[], const int symbol, const int num_bits ) + void encode_tree3( Bit_model bm[], const int symbol ) { - int mask = ( 1 << ( num_bits - 1 ) ); int model = 1; - for( int i = num_bits; i > 0; --i, mask >>= 1 ) - { + int bit = ( symbol >> 2 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + bit = ( symbol >> 1 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + encode_bit( bm[model], symbol & 1 ); + } + + void encode_tree6( Bit_model bm[], const int symbol ) + { + int model = 1; + int bit = ( symbol >> 5 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + bit = ( symbol >> 4 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + bit = ( symbol >> 3 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + bit = ( symbol >> 2 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + bit = ( symbol >> 1 ) & 1; + encode_bit( bm[model], bit ); model = ( model << 1 ) | bit; + encode_bit( bm[model], symbol & 1 ); + } + + void encode_tree8( Bit_model bm[], const int symbol ) + { + int model = 1; + int mask = ( 1 << 7 ); + do { const int bit = ( symbol & mask ); encode_bit( bm[model], bit ); - model <<= 1; - if( bit ) model |= 1; + model <<= 1; if( bit ) ++model; } + while( mask >>= 1 ); } void encode_tree_reversed( Bit_model bm[], int symbol, const int num_bits ) @@ -354,16 +404,16 @@ public: } } - void encode_matched( Bit_model bm[], unsigned symbol, unsigned match_byte ) + void encode_matched( Bit_model bm[], int symbol, int match_byte ) { - unsigned mask = 0x100; - symbol |= 0x100; + int mask = 0x100; + symbol |= mask; do { match_byte <<= 1; - const unsigned match_bit = match_byte & mask; + const int match_bit = match_byte & mask; symbol <<= 1; - const unsigned bit = symbol & 0x100; + const int bit = symbol & 0x100; encode_bit( bm[match_bit+(symbol>>9)+mask], bit ); mask &= ~(match_byte ^ symbol); // if( match_bit != bit ) mask = 0; } @@ -384,17 +434,16 @@ class Len_encoder : public Len_model int tmp = price0( choice1 ); int len = 0; for( ; len < len_low_symbols && len < len_symbols; ++len ) - pps[len] = tmp + - price_symbol( bm_low[pos_state], len, len_low_bits ); + pps[len] = tmp + price_symbol3( bm_low[pos_state], len ); tmp = price1( choice1 ); for( ; len < len_low_symbols + len_mid_symbols && len < len_symbols; ++len ) pps[len] = tmp + price0( choice2 ) + - price_symbol( bm_mid[pos_state], len - len_low_symbols, len_mid_bits ); + price_symbol3( bm_mid[pos_state], len - len_low_symbols ); for( ; len < len_symbols; ++len ) // using 4 slots per value makes "price" faster prices[3][len] = prices[2][len] = prices[1][len] = prices[0][len] = tmp + price1( choice2 ) + - price_symbol( bm_high, len - len_low_symbols - len_mid_symbols, len_high_bits ); + price_symbol8( bm_high, len - len_low_symbols - len_mid_symbols ); counters[pos_state] = len_symbols; } @@ -435,41 +484,22 @@ protected: Len_encoder match_len_encoder; Len_encoder rep_len_encoder; - const int num_dis_slots; - unsigned crc() const { return crc_ ^ 0xFFFFFFFFU; } - LZ_encoder_base( const File_header & header, const int dictionary_size, - const int match_len_limit, const int outfd ) + LZ_encoder_base( const File_header & header, const int match_len_limit, + const int outfd ) : crc_( 0xFFFFFFFFU ), renc( outfd ), match_len_encoder( match_len_limit ), - rep_len_encoder( match_len_limit ), - num_dis_slots( 2 * real_bits( dictionary_size - 1 ) ) + rep_len_encoder( match_len_limit ) { for( int i = 0; i < File_header::size; ++i ) renc.put_byte( header.data[i] ); } - // move-to-front dis in/into reps - void mtf_reps( const int dis, int reps[num_rep_distances] ) - { - if( dis >= num_rep_distances ) - { - for( int i = num_rep_distances - 1; i > 0; --i ) reps[i] = reps[i-1]; - reps[0] = dis - num_rep_distances; - } - else if( dis > 0 ) - { - const int distance = reps[dis]; - for( int i = dis; i > 0; --i ) reps[i] = reps[i-1]; - reps[0] = distance; - } - } - int price_literal( const uint8_t prev_byte, const uint8_t symbol ) const - { return price_symbol( bm_literal[get_lit_state(prev_byte)], symbol, 8 ); } + { return price_symbol8( bm_literal[get_lit_state(prev_byte)], symbol ); } int price_matched( const uint8_t prev_byte, const uint8_t symbol, const uint8_t match_byte ) const @@ -477,24 +507,24 @@ protected: symbol, match_byte ); } void encode_literal( const uint8_t prev_byte, const uint8_t symbol ) - { renc.encode_tree( bm_literal[get_lit_state(prev_byte)], symbol, 8 ); } + { renc.encode_tree8( bm_literal[get_lit_state(prev_byte)], symbol ); } void encode_matched( const uint8_t prev_byte, const uint8_t symbol, const uint8_t match_byte ) { renc.encode_matched( bm_literal[get_lit_state(prev_byte)], symbol, match_byte ); } - void encode_pair( const uint32_t dis, const int len, const int pos_state ) + void encode_pair( const unsigned dis, const int len, const int pos_state ) { match_len_encoder.encode( renc, len, pos_state ); const int dis_slot = get_slot( dis ); - renc.encode_tree( bm_dis_slot[get_len_state(len)], dis_slot, dis_slot_bits ); + renc.encode_tree6( bm_dis_slot[get_len_state(len)], dis_slot ); if( dis_slot >= start_dis_model ) { const int direct_bits = ( dis_slot >> 1 ) - 1; - const uint32_t base = ( 2 | ( dis_slot & 1 ) ) << direct_bits; - const uint32_t direct_dis = dis - base; + const unsigned base = ( 2 | ( dis_slot & 1 ) ) << direct_bits; + const unsigned direct_dis = dis - base; if( dis_slot < end_dis_model ) renc.encode_tree_reversed( bm_dis + base - dis_slot - 1, direct_dis, @@ -524,34 +554,32 @@ class LZ_encoder : public LZ_encoder_base { State state; int price; // dual use var; cumulative price, match length - int dis; // rep index or match distance + int dis; // rep index or match distance. (-1 for literal) int prev_index; // index of prev trial in trials[] - int dis2; int prev_index2; // -2 trial is single step // -1 literal + rep0 - // >= 0 rep or match + literal + rep0 + // >= 0 ( rep or match ) + literal + rep0 int reps[num_rep_distances]; - void update( const int pr, const int d, const int p_i ) + void update( const int pr, const int distance, const int p_i ) { if( pr < price ) - { price = pr; dis = d; prev_index = p_i; + { price = pr; dis = distance; prev_index = p_i; prev_index2 = single_step_trial; } } - void update2( const int pr, const int d, const int p_i ) + void update2( const int pr, const int p_i ) { if( pr < price ) - { price = pr; dis = d; prev_index = p_i; + { price = pr; dis = 0; prev_index = p_i; prev_index2 = dual_step_trial; } } - void update3( const int pr, const int d, const int p_i, - const int d2, const int p_i2 ) + void update3( const int pr, const int distance, const int p_i, + const int p_i2 ) { if( pr < price ) - { price = pr; dis = d; prev_index = p_i; - dis2 = d2; prev_index2 = p_i2; } + { price = pr; dis = distance; prev_index = p_i; prev_index2 = p_i2; } } }; @@ -560,15 +588,32 @@ class LZ_encoder : public LZ_encoder_base struct Pair pairs[max_match_len+1]; Trial trials[max_num_trials]; - int dis_slot_prices[len_states][2*max_dictionary_bits]; int dis_prices[len_states][modeled_distances]; + int dis_slot_prices[len_states][2*max_dictionary_bits]; int align_prices[dis_align_size]; int align_price_count; + const int num_dis_slots; void fill_align_prices(); void fill_distance_prices(); - int price_rep_len1( const State state, const int pos_state ) const + // move-to-front dis in/into reps if( dis > 0 ) + static void mtf_reps( const int dis, int reps[num_rep_distances] ) + { + if( dis >= num_rep_distances ) + { + for( int i = num_rep_distances - 1; i > 0; --i ) reps[i] = reps[i-1]; + reps[0] = dis - num_rep_distances; + } + else if( dis > 0 ) + { + const int distance = reps[dis]; + for( int i = dis; i > 0; --i ) reps[i] = reps[i-1]; + reps[0] = distance; + } + } + + int price_shortrep( const State state, const int pos_state ) const { return price0( bm_rep0[state()] ) + price0( bm_len[state()][pos_state] ); } @@ -594,21 +639,17 @@ class LZ_encoder : public LZ_encoder_base rep_len_encoder.price( len, pos_state ); } - int price_dis( const int dis, const int len_state ) const + int price_pair( const int dis, const int len, const int pos_state ) const { + const int price = match_len_encoder.price( len, pos_state ); + const int len_state = get_len_state( len ); if( dis < modeled_distances ) - return dis_prices[len_state][dis]; + return price + dis_prices[len_state][dis]; else - return dis_slot_prices[len_state][get_slot( dis )] + + return price + dis_slot_prices[len_state][get_slot( dis )] + align_prices[dis & (dis_align_size - 1)]; } - int price_pair( const int dis, const int len, const int pos_state ) const - { - return match_len_encoder.price( len, pos_state ) + - price_dis( dis, get_len_state( len ) ); - } - int read_match_distances() { const int num_pairs = matchfinder.get_match_pairs( pairs ); @@ -627,11 +668,11 @@ class LZ_encoder : public LZ_encoder_base void move_pos( int n ) { - if( --n >= 0 ) matchfinder.move_pos(); - while( --n >= 0 ) + while( true ) { - matchfinder.get_match_pairs(); matchfinder.move_pos(); + if( --n <= 0 ) break; + matchfinder.get_match_pairs(); } } @@ -651,7 +692,7 @@ class LZ_encoder : public LZ_encoder_base if( trials[cur].prev_index2 >= 0 ) { Trial & prev_trial2 = trials[prev_index-1]; - prev_trial2.dis = trials[cur].dis2; + prev_trial2.dis = dis; dis = 0; prev_trial2.prev_index = trials[cur].prev_index2; prev_trial2.prev_index2 = single_step_trial; } @@ -668,10 +709,11 @@ class LZ_encoder : public LZ_encoder_base public: LZ_encoder( Matchfinder & mf, const File_header & header, const int outfd ) : - LZ_encoder_base( header, mf.dictionary_size(), mf.match_len_limit(), outfd ), + LZ_encoder_base( header, mf.match_len_limit(), outfd ), matchfinder( mf ), pending_num_pairs( 0 ), - align_price_count( 0 ) + align_price_count( 0 ), + num_dis_slots( 2 * real_bits( mf.dictionary_size() - 1 ) ) {} bool encode_member( const unsigned long long member_size ); diff --git a/fast_encoder.cc b/fast_encoder.cc index dfbb429..f87fdec 100644 --- a/fast_encoder.cc +++ b/fast_encoder.cc @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,37 +33,35 @@ int Fmatchfinder::longest_match_len( int * const distance ) { - int len_limit = match_len_limit_; - if( len_limit > available_bytes() ) - { - len_limit = available_bytes(); - if( len_limit < 4 ) return 0; - } + if( len_limit > available_bytes() ) return 0; - key4 = ( ( key4 << 4 ) ^ buffer[pos+3] ) & key4_mask; + const uint8_t * const data = buffer + pos; + key4 = ( ( key4 << 4 ) ^ data[3] ) & key4_mask; + const int pos1 = pos + 1; int newpos = prev_positions[key4]; - prev_positions[key4] = pos; + prev_positions[key4] = pos1; int32_t * ptr0 = pos_array + cyclic_pos; int maxlen = 0; for( int count = 4; ; ) { - const int delta = pos - newpos; - if( delta > dictionary_size_ || newpos < 0 || --count < 0 ) - { *ptr0 = -1; break; } - int len = 0; - if( buffer[maxlen+newpos] == buffer[maxlen+pos] ) - while( len < len_limit && buffer[len+newpos] == buffer[len+pos] ) ++len; - - if( maxlen < len ) { maxlen = len; *distance = delta - 1; } - + if( --count < 0 || newpos <= 0 ) { *ptr0 = 0; break; } + const int delta = pos1 - newpos; + if( delta > dictionary_size_ ) { *ptr0 = 0; break; } int32_t * const newptr = pos_array + ( cyclic_pos - delta + ( ( cyclic_pos >= delta ) ? 0 : dictionary_size_ + 1 ) ); - if( len < len_limit ) + if( data[maxlen-delta] == data[maxlen] ) + { + int len = 0; + while( len < len_limit && data[len-delta] == data[len] ) ++len; + if( maxlen < len ) { maxlen = len; *distance = delta - 1; } + } + + if( maxlen < len_limit ) { *ptr0 = newpos; ptr0 = newptr; @@ -71,54 +70,19 @@ int Fmatchfinder::longest_match_len( int * const distance ) else { *ptr0 = *newptr; + maxlen += true_match_len( maxlen, *distance + 1, max_match_len - maxlen ); break; } } - if( maxlen == match_len_limit_ && maxlen < max_match_len ) - maxlen += true_match_len( maxlen, *distance + 1, max_match_len - maxlen ); return maxlen; } -void Fmatchfinder::longest_match_len( int n ) - { - while( --n >= 0 ) - { - int len_limit = match_len_limit_ - 1; // len_limit - 1 - if( len_limit >= available_bytes() ) - { - len_limit = available_bytes() - 1; - if( len_limit < 3 ) { move_pos(); continue; } - } - - key4 = ( ( key4 << 4 ) ^ buffer[pos+3] ) & key4_mask; - - const int newpos = prev_positions[key4]; - prev_positions[key4] = pos; - - int32_t * const ptr0 = pos_array + cyclic_pos; - - if( pos - newpos > dictionary_size_ || newpos < 0 ) - *ptr0 = -1; - else if( buffer[len_limit+newpos] != buffer[len_limit+pos] || - std::memcmp( buffer + newpos, buffer + pos, len_limit ) != 0 ) - *ptr0 = newpos; - else - { - int idx = cyclic_pos - pos + newpos; - if( idx < 0 ) idx += dictionary_size_ + 1; - *ptr0 = pos_array[idx]; - } - move_pos(); - } - } - - bool FLZ_encoder::encode_member( const unsigned long long member_size ) { const unsigned long long member_size_limit = member_size - File_trailer::size() - max_marker_size; - int dis = 0; + int rep = 0; int reps[num_rep_distances]; State state; for( int i = 0; i < num_rep_distances; ++i ) reps[i] = 0; @@ -134,7 +98,7 @@ bool FLZ_encoder::encode_member( const unsigned long long member_size ) renc.encode_bit( bm_match[state()][0], 0 ); encode_literal( prev_byte, cur_byte ); crc32.update_byte( crc_, cur_byte ); - fmatchfinder.longest_match_len( 1 ); + fmatchfinder.update_and_move( 1 ); } while( !fmatchfinder.finished() && @@ -149,52 +113,51 @@ bool FLZ_encoder::encode_member( const unsigned long long member_size ) { const int tlen = fmatchfinder.true_match_len( 0, reps[i] + 1, max_match_len ); - if( tlen > len ) { len = tlen; dis = i; } + if( tlen > len ) { len = tlen; rep = i; } } - if( len > min_match_len && len + 4 > main_len ) + if( len > min_match_len && len + 3 > main_len ) { crc32.update_buf( crc_, fmatchfinder.ptr_to_current_pos(), len ); renc.encode_bit( bm_match[state()][pos_state], 1 ); renc.encode_bit( bm_rep[state()], 1 ); - const bool bit = ( dis == 0 ); - renc.encode_bit( bm_rep0[state()], !bit ); - if( bit ) + renc.encode_bit( bm_rep0[state()], rep != 0 ); + if( rep == 0 ) renc.encode_bit( bm_len[state()][pos_state], 1 ); else { - const int distance = reps[dis]; - for( int i = dis; i > 0; --i ) reps[i] = reps[i-1]; + renc.encode_bit( bm_rep1[state()], rep > 1 ); + if( rep > 1 ) + renc.encode_bit( bm_rep2[state()], rep > 2 ); + const int distance = reps[rep]; + for( int i = rep; i > 0; --i ) reps[i] = reps[i-1]; reps[0] = distance; - renc.encode_bit( bm_rep1[state()], dis > 1 ); - if( dis > 1 ) - renc.encode_bit( bm_rep2[state()], dis > 2 ); } - rep_len_encoder.encode( renc, len, pos_state ); state.set_rep(); - move_pos( len ); + rep_len_encoder.encode( renc, len, pos_state ); + fmatchfinder.move_pos(); + fmatchfinder.update_and_move( len - 1 ); continue; } - if( main_len > min_match_len || - ( main_len == min_match_len && match_distance < modeled_distances ) ) + if( main_len > min_match_len ) { crc32.update_buf( crc_, fmatchfinder.ptr_to_current_pos(), main_len ); - dis = match_distance; renc.encode_bit( bm_match[state()][pos_state], 1 ); renc.encode_bit( bm_rep[state()], 0 ); - encode_pair( dis, main_len, pos_state ); state.set_match(); - move_pos( main_len ); for( int i = num_rep_distances - 1; i > 0; --i ) reps[i] = reps[i-1]; - reps[0] = dis; + reps[0] = match_distance; + encode_pair( match_distance, main_len, pos_state ); + fmatchfinder.move_pos(); + fmatchfinder.update_and_move( main_len - 1 ); continue; } - const uint8_t prev_byte = fmatchfinder[-1]; + const uint8_t prev_byte = fmatchfinder[1]; const uint8_t cur_byte = fmatchfinder[0]; - const uint8_t match_byte = fmatchfinder[-reps[0]-1]; - crc32.update_byte( crc_, cur_byte ); + const uint8_t match_byte = fmatchfinder[reps[0]+1]; fmatchfinder.move_pos(); + crc32.update_byte( crc_, cur_byte ); if( match_byte == cur_byte ) { diff --git a/fast_encoder.h b/fast_encoder.h index 3f8125f..486f03f 100644 --- a/fast_encoder.h +++ b/fast_encoder.h @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,23 +23,40 @@ class Fmatchfinder : public Matchfinder_base // bytes to keep in buffer after pos after_size = max_match_len, dict_factor = 16, - len_limit = 16, num_prev_positions23 = 0, pos_array_factor = 1 }; int key4; // key made from latest 4 bytes + void reset_key4() + { + key4 = 0; + for( int i = 0; i < 3 && i < available_bytes(); ++i ) + key4 = ( key4 << 4 ) ^ buffer[i]; + } + public: explicit Fmatchfinder( const int ifd ) : Matchfinder_base( before_size, dict_size, after_size, dict_factor, - len_limit, num_prev_positions23, pos_array_factor, ifd ), - key4( 0 ) - {} + num_prev_positions23, pos_array_factor, ifd ) + { reset_key4(); } - void reset() { Matchfinder_base::reset(); key4 = 0; } + enum { len_limit = 16 }; + void reset() { Matchfinder_base::reset(); reset_key4(); } int longest_match_len( int * const distance ); - void longest_match_len( int n ); + + void update_and_move( int n ) + { + while( --n >= 0 ) + { + key4 = ( ( key4 << 4 ) ^ buffer[pos+3] ) & key4_mask; + const int newpos = prev_positions[key4]; + prev_positions[key4] = pos + 1; + pos_array[cyclic_pos] = newpos; + move_pos(); + } + } }; @@ -46,16 +64,10 @@ class FLZ_encoder : public LZ_encoder_base { Fmatchfinder & fmatchfinder; - void move_pos( int n ) - { - if( --n >= 0 ) fmatchfinder.move_pos(); - fmatchfinder.longest_match_len( n ); - } - public: FLZ_encoder( Fmatchfinder & mf, const File_header & header, const int outfd ) : - LZ_encoder_base( header, mf.dictionary_size(), mf.match_len_limit(), outfd ), + LZ_encoder_base( header, mf.len_limit, outfd ), fmatchfinder( mf ) {} @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,7 +41,7 @@ public: enum { min_dictionary_bits = 12, - min_dictionary_size = 1 << min_dictionary_bits, + min_dictionary_size = 1 << min_dictionary_bits, // >= modeled_distances max_dictionary_bits = 29, max_dictionary_size = 1 << max_dictionary_bits, literal_context_bits = 3, @@ -240,9 +241,7 @@ struct File_trailer } void data_size( unsigned long long sz ) - { - for( int i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } - } + { for( int i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } } unsigned long long member_size() const { @@ -252,9 +251,7 @@ struct File_trailer } void member_size( unsigned long long sz ) - { - for( int i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } - } + { for( int i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } } }; @@ -278,4 +275,4 @@ class Matchfinder_base; void show_progress( const unsigned long long partial_size = 0, const Matchfinder_base * const m = 0, const Pretty_print * const p = 0, - const struct stat * const in_statsp = 0 ); + const unsigned long long cfile_size = 0 ); @@ -1,5 +1,6 @@ /* Lzip - LZMA lossless data compressor - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + Antonio Diaz Diaz. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -59,6 +60,10 @@ #include "encoder.h" #include "fast_encoder.h" +#ifndef O_BINARY +#define O_BINARY 0 +#endif + #if CHAR_BIT != 8 #error "Environments where CHAR_BIT != 8 are not supported." #endif @@ -68,15 +73,9 @@ namespace { const char * const Program_name = "Lzip"; const char * const program_name = "lzip"; -const char * const program_year = "2013"; +const char * const program_year = "2014"; const char * invocation_name = 0; -#ifdef O_BINARY -const int o_binary = O_BINARY; -#else -const int o_binary = 0; -#endif - struct { const char * from; const char * to; } const known_extensions[] = { { ".lz", "" }, { ".tlz", ".tar" }, @@ -260,7 +259,8 @@ int open_instream( const char * const name, struct stat * const in_statsp, } else { - infd = open( name, O_RDONLY | o_binary ); + do infd = open( name, O_RDONLY | O_BINARY ); + while( infd < 0 && errno == EINTR ); if( infd < 0 ) { if( verbosity >= 0 ) @@ -320,10 +320,11 @@ void set_d_outname( const std::string & name, const int i ) bool open_outstream( const bool force ) { - int flags = O_CREAT | O_WRONLY | o_binary; + int flags = O_CREAT | O_WRONLY | O_BINARY; if( force ) flags |= O_TRUNC; else flags |= O_EXCL; - outfd = open( output_filename.c_str(), flags, outfd_mode ); + do outfd = open( output_filename.c_str(), flags, outfd_mode ); + while( outfd < 0 && errno == EINTR ); if( outfd < 0 && verbosity >= 0 ) { if( errno == EEXIST ) @@ -376,10 +377,14 @@ void close_and_set_permissions( const struct stat * const in_statsp ) bool warning = false; if( in_statsp ) { + const mode_t mode = in_statsp->st_mode; // fchown will in many cases return with EPERM, which can be safely ignored. - if( ( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) != 0 && - errno != EPERM ) || - fchmod( outfd, in_statsp->st_mode ) != 0 ) warning = true; + if( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) == 0 ) + { if( fchmod( outfd, mode ) != 0 ) warning = true; } + else + if( errno != EPERM || + fchmod( outfd, mode & ~( S_ISUID | S_ISGID | S_ISVTX ) ) != 0 ) + warning = true; } if( close( outfd ) != 0 ) cleanup_and_fail( 1 ); outfd = -1; @@ -414,6 +419,8 @@ int compress( const unsigned long long member_size, const Lzma_options & encoder_options, const int infd, const Pretty_print & pp, const struct stat * const in_statsp ) { + const unsigned long long cfile_size = + (in_statsp && S_ISREG( in_statsp->st_mode )) ? in_statsp->st_size / 100 : 0; int retval = 0; File_header header; header.set_magic(); @@ -435,7 +442,8 @@ int compress( const unsigned long long member_size, LZ_encoder encoder( matchfinder, header, outfd ); const unsigned long long size = ( volume_size > 0 ) ? std::min( member_size, volume_size - partial_volume_size ) : member_size; - show_progress( in_size, &matchfinder, &pp, in_statsp ); // init + if( verbosity >= 2 ) + show_progress( in_size, &matchfinder, &pp, cfile_size ); // init if( !encoder.encode_member( size ) ) { pp( "Encoder error" ); retval = 1; break; } in_size += matchfinder.data_position(); @@ -487,6 +495,8 @@ int fcompress( const unsigned long long member_size, const unsigned long long volume_size, const int infd, const Pretty_print & pp, const struct stat * const in_statsp ) { + const unsigned long long cfile_size = + (in_statsp && S_ISREG( in_statsp->st_mode )) ? in_statsp->st_size / 100 : 0; int retval = 0; File_header header; header.set_magic(); @@ -503,7 +513,8 @@ int fcompress( const unsigned long long member_size, FLZ_encoder encoder( fmatchfinder, header, outfd ); const unsigned long long size = ( volume_size > 0 ) ? std::min( member_size, volume_size - partial_volume_size ) : member_size; - show_progress( in_size, &fmatchfinder, &pp, in_statsp ); // init + if( verbosity >= 2 ) + show_progress( in_size, &fmatchfinder, &pp, cfile_size ); // init if( !encoder.encode_member( size ) ) { pp( "Encoder error" ); retval = 1; break; } in_size += fmatchfinder.data_position(); @@ -651,11 +662,7 @@ int decompress( const int infd, const Pretty_print & pp, const bool testing ) { std::fprintf( stderr, testing ? "ok\n" : "done\n" ); pp.reset(); } } } - catch( std::bad_alloc ) - { - pp( "Not enough memory. Find a machine with more memory" ); - retval = 1; - } + catch( std::bad_alloc ) { pp( "Not enough memory" ); retval = 1; } catch( Error e ) { pp(); show_error( e.msg, errno ); retval = 1; } if( verbosity == 1 && retval == 0 ) std::fprintf( stderr, testing ? "ok\n" : "done\n" ); @@ -712,25 +719,20 @@ void internal_error( const char * const msg ) void show_progress( const unsigned long long partial_size, const Matchfinder_base * const m, const Pretty_print * const p, - const struct stat * const in_statsp ) + const unsigned long long cfile_size ) { - static unsigned long long cfile_size = 0; // file_size / 100 + static unsigned long long csize = 0; // file_size / 100 static unsigned long long psize = 0; static const Matchfinder_base * mb = 0; static const Pretty_print * pp = 0; if( m ) // initialize static vars - { - psize = partial_size; mb = m; pp = p; - cfile_size = ( in_statsp && S_ISREG( in_statsp->st_mode ) ) ? - in_statsp->st_size / 100 : 0; - return; - } + { csize = cfile_size; psize = partial_size; mb = m; pp = p; } if( mb && pp ) { const unsigned long long pos = psize + mb->data_position(); - if( cfile_size > 0 ) - std::fprintf( stderr, "%4llu%%", pos / cfile_size ); + if( csize > 0 ) + std::fprintf( stderr, "%4llu%%", pos / csize ); std::fprintf( stderr, " %.1f MB\r", pos / 1000000.0 ); pp->reset(); (*pp)(); // restore cursor position } @@ -812,10 +814,9 @@ int main( const int argc, const char * const argv[] ) const char * const arg = parser.argument( argind ).c_str(); switch( code ) { - case '0': zero = true; break; - case '1': case '2': case '3': case '4': + case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - zero = false; + zero = ( code == '0' ); encoder_options = option_mapping[code-'0']; break; case 'b': member_size = getnum( arg, 100000, max_member_size ); break; case 'c': to_stdout = true; break; diff --git a/testsuite/check.sh b/testsuite/check.sh index 8d6bb79..a8d0bf1 100755 --- a/testsuite/check.sh +++ b/testsuite/check.sh @@ -1,6 +1,7 @@ #! /bin/sh # check script for Lzip - LZMA lossless data compressor -# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. +# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 +# Antonio Diaz Diaz. # # This script is free software: you have unlimited permission # to copy, distribute and modify it. @@ -97,8 +98,16 @@ cmp in anyothername.out || fail=1 printf . cat in in > in2 || framework_failure -"${LZIP}" < in2 > out2 || fail=1 -"${LZIP}" -d < out2 > copy2 || fail=1 +"${LZIP}" -o copy2 < in2 || fail=1 +"${LZIP}" -t copy2.lz || fail=1 +printf . +"${LZIP}" -cd copy2.lz > copy2 || fail=1 +cmp in2 copy2 || fail=1 +printf . + +printf "garbage" >> copy2.lz || framework_failure +printf "to be overwritten" > copy2 || framework_failure +"${LZIP}" -df copy2.lz || fail=1 cmp in2 copy2 || fail=1 printf . |