diff options
Diffstat (limited to '')
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | INSTALL | 1 | ||||
-rw-r--r-- | Makefile.in | 47 | ||||
-rw-r--r-- | NEWS | 29 | ||||
-rwxr-xr-x | configure | 6 | ||||
-rw-r--r-- | decoder.cc | 2 | ||||
-rw-r--r-- | doc/lzlib.info | 26 | ||||
-rw-r--r-- | doc/lzlib.texinfo | 4 | ||||
-rw-r--r-- | encoder.cc | 17 | ||||
-rw-r--r-- | encoder.h | 5 | ||||
-rw-r--r-- | lzcheck.cc | 59 | ||||
-rw-r--r-- | lzip.h | 2 | ||||
-rw-r--r-- | lzlib.cc | 2 | ||||
-rw-r--r-- | lzlib.h | 2 | ||||
-rw-r--r-- | main.cc | 181 |
15 files changed, 230 insertions, 159 deletions
@@ -1,3 +1,9 @@ +2010-02-10 Antonio Diaz Diaz <ant_diaz@teleline.es> + + * Version 0.9 released. + * Compression time has been reduced by 8%. + * main.cc: New constant "o_binary". + 2010-01-17 Antonio Diaz Diaz <ant_diaz@teleline.es> * Version 0.8 released. @@ -33,7 +33,6 @@ the main archive. documentation. (You might have to run ldconfig also). - Another way ----------- You can also compile lzlib into a separate directory. To do this, you diff --git a/Makefile.in b/Makefile.in index c06404d..a68698a 100644 --- a/Makefile.in +++ b/Makefile.in @@ -54,14 +54,15 @@ sh_lzlib.o : lzlib.cc $(lib_objs) : Makefile lzlib.h lzip.h $(sh_lib_objs) : Makefile lzlib.h lzip.h +$(objs) : Makefile +arg_parser.o : arg_parser.h decoder.o : decoder.h encoder.o : encoder.h lzlib.o : decoder.h encoder.h sh_decoder.o : decoder.h sh_encoder.o : encoder.h sh_lzlib.o : decoder.h encoder.h -arg_parser.o : Makefile arg_parser.h -main.o : Makefile arg_parser.h lzlib.h +main.o : arg_parser.h lzlib.h lzcheck.o : Makefile lzlib.h @@ -85,44 +86,44 @@ check : all lzcheck @$(VPATH)/testsuite/check.sh $(VPATH)/testsuite install : all install-info - if [ ! -d $(DESTDIR)$(includedir) ] ; then $(INSTALL_DIR) $(DESTDIR)$(includedir) ; fi - if [ ! -d $(DESTDIR)$(libdir) ] ; then $(INSTALL_DIR) $(DESTDIR)$(libdir) ; fi - $(INSTALL_DATA) $(VPATH)/$(libname)lib.h $(DESTDIR)$(includedir)/$(libname)lib.h - $(INSTALL_DATA) ./lib$(libname).a $(DESTDIR)$(libdir)/lib$(libname).a + if [ ! -d "$(DESTDIR)$(includedir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(includedir)" ; fi + if [ ! -d "$(DESTDIR)$(libdir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(libdir)" ; fi + $(INSTALL_DATA) $(VPATH)/$(libname)lib.h "$(DESTDIR)$(includedir)/$(libname)lib.h" + $(INSTALL_DATA) ./lib$(libname).a "$(DESTDIR)$(libdir)/lib$(libname).a" if [ -n "$(progname_shared)" ] ; then \ - $(INSTALL_PROGRAM) ./lib$(libname).so.$(pkgversion) $(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion) ; \ - if [ -e $(DESTDIR)$(libdir)/lib$(libname).so.$(soversion) ] ; then \ - run_ldconfig=no ; rm -f $(DESTDIR)$(libdir)/lib$(libname).so.$(soversion) ; \ + $(INSTALL_PROGRAM) ./lib$(libname).so.$(pkgversion) "$(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion)" ; \ + if [ -e "$(DESTDIR)$(libdir)/lib$(libname).so.$(soversion)" ] ; then \ + run_ldconfig=no ; rm -f "$(DESTDIR)$(libdir)/lib$(libname).so.$(soversion)" ; \ else run_ldconfig=yes ; \ fi ; \ - cd $(DESTDIR)$(libdir) && ln -s lib$(libname).so.$(pkgversion) lib$(libname).so.$(soversion) ; \ - if [ $${run_ldconfig} = yes ] ; then $(LDCONFIG) $(DESTDIR)$(libdir) ; fi ; \ + cd "$(DESTDIR)$(libdir)" && ln -s lib$(libname).so.$(pkgversion) lib$(libname).so.$(soversion) ; \ + if [ $${run_ldconfig} = yes ] ; then $(LDCONFIG) "$(DESTDIR)$(libdir)" ; fi ; \ fi install-info : - if [ ! -d $(DESTDIR)$(infodir) ] ; then $(INSTALL_DIR) $(DESTDIR)$(infodir) ; fi - $(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info $(DESTDIR)$(infodir)/$(pkgname).info - -install-info --info-dir=$(DESTDIR)$(infodir) $(DESTDIR)$(infodir)/$(pkgname).info + if [ ! -d "$(DESTDIR)$(infodir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(infodir)" ; fi + $(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info "$(DESTDIR)$(infodir)/$(pkgname).info" + -install-info --info-dir="$(DESTDIR)$(infodir)" $(DESTDIR)$(infodir)/$(pkgname).info install-man : - if [ ! -d $(DESTDIR)$(mandir)/man1 ] ; then $(INSTALL_DIR) $(DESTDIR)$(mandir)/man1 ; fi - $(INSTALL_DATA) $(VPATH)/doc/$(progname).1 $(DESTDIR)$(mandir)/man1/$(progname).1 + if [ ! -d "$(DESTDIR)$(mandir)/man1" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1" ; fi + $(INSTALL_DATA) $(VPATH)/doc/$(progname).1 "$(DESTDIR)$(mandir)/man1/$(progname).1" install-strip : all $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install uninstall : uninstall-info - -rm -f $(DESTDIR)$(includedir)/$(libname)lib.h - -rm -f $(DESTDIR)$(libdir)/lib$(libname).a - -rm -f $(DESTDIR)$(libdir)/lib$(libname).so.$(soversion) - -rm -f $(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion) + -rm -f "$(DESTDIR)$(includedir)/$(libname)lib.h" + -rm -f "$(DESTDIR)$(libdir)/lib$(libname).a" + -rm -f "$(DESTDIR)$(libdir)/lib$(libname).so.$(soversion)" + -rm -f "$(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion)" uninstall-info : - -install-info --info-dir=$(DESTDIR)$(infodir) --remove $(DESTDIR)$(infodir)/$(pkgname).info - -rm -f $(DESTDIR)$(infodir)/$(pkgname).info + -install-info --info-dir="$(DESTDIR)$(infodir)" --remove "$(DESTDIR)$(infodir)/$(pkgname).info" + -rm -f "$(DESTDIR)$(infodir)/$(pkgname).info" uninstall-man : - -rm -f $(DESTDIR)$(mandir)/man1/$(progname).1 + -rm -f "$(DESTDIR)$(mandir)/man1/$(progname).1" dist : doc ln -sf $(VPATH) $(DISTNAME) @@ -1,28 +1,3 @@ -Changes in version 0.8: +Changes in version 0.9: -API changes: - New functions: - LZ_decompress_reset - LZ_decompress_sync_to_member - LZ_decompress_write_size - LZ_strerror - - Names and types for values of dictionary size limits have been changed - to make interface names consistent. - - "LZ_errno" has been replaced with "LZ_Errno". - - "void *" has been replaced with "struct LZ_Encoder *" and - "struct LZ_Decoder *" to make interface type safe. - -A truncated member trailer is now correctly detected. - -LZ_compress_restart_member is now able to restart a finished stream. - -Lzlib now only accepts query or close operations after a fatal error has -occurred. - -Shared version of lzlib is no more built by default. Use -"configure --enable-shared" to build it. - -The "COPYING" file is no more used for testing decompression. +Compression time has been reduced by 8%. @@ -5,12 +5,12 @@ # This configure script is free software: you have unlimited permission # to copy, distribute and modify it. # -# Date of this version: 2010-01-17 +# Date of this version: 2010-02-10 args= no_create= pkgname=lzlib -pkgversion=0.8 +pkgversion=0.9 soversion=0 progname=minilzip progname_shared= @@ -149,7 +149,7 @@ if [ -z "${CXX}" ] ; then # Let the user override the test. fi echo -if [ -z ${no_create} ] ; then +if [ -z "${no_create}" ] ; then echo "creating config.status" rm -f config.status cat > config.status << EOF @@ -44,6 +44,7 @@ const CRC32 crc32; // Returns the number of bytes copied. int Circular_buffer::read_data( uint8_t * const out_buffer, const int out_size ) throw() { + if( out_size < 0 ) return 0; int size = 0; if( get > put ) { @@ -73,6 +74,7 @@ int Circular_buffer::read_data( uint8_t * const out_buffer, const int out_size ) // Returns the number of bytes copied. int Circular_buffer::write_data( const uint8_t * const in_buffer, const int in_size ) throw() { + if( in_size < 0 ) return 0; int size = 0; if( put >= get ) { diff --git a/doc/lzlib.info b/doc/lzlib.info index 9437d8c..9a516a9 100644 --- a/doc/lzlib.info +++ b/doc/lzlib.info @@ -12,7 +12,7 @@ File: lzlib.info, Node: Top, Next: Introduction, Up: (dir) Lzlib Manual ************ -This manual is for Lzlib (version 0.8, 17 January 2010). +This manual is for Lzlib (version 0.9, 10 February 2010). * Menu: @@ -676,17 +676,17 @@ Concept Index Tag Table: Node: Top219 -Node: Introduction1157 -Node: Library Version2932 -Node: Buffering3577 -Node: Parameter Limits4697 -Node: Compression Functions5654 -Node: Decompression Functions11700 -Node: Error Codes16762 -Node: Error Messages18701 -Node: Data Format19280 -Node: Examples21250 -Node: Problems24826 -Node: Concept Index25398 +Node: Introduction1158 +Node: Library Version2933 +Node: Buffering3578 +Node: Parameter Limits4698 +Node: Compression Functions5655 +Node: Decompression Functions11701 +Node: Error Codes16763 +Node: Error Messages18702 +Node: Data Format19281 +Node: Examples21251 +Node: Problems24827 +Node: Concept Index25399 End Tag Table diff --git a/doc/lzlib.texinfo b/doc/lzlib.texinfo index ae22e44..8163502 100644 --- a/doc/lzlib.texinfo +++ b/doc/lzlib.texinfo @@ -5,8 +5,8 @@ @finalout @c %**end of header -@set UPDATED 17 January 2010 -@set VERSION 0.8 +@set UPDATED 10 February 2010 +@set VERSION 0.9 @dircategory Data Compression @direntry @@ -44,7 +44,7 @@ const Prob_prices prob_prices; int Matchfinder::write_data( const uint8_t * const in_buffer, const int in_size ) throw() { - if( at_stream_end_ ) return 0; + if( at_stream_end_ || in_size < 0 ) return 0; const int size = std::min( buffer_size - stream_pos, in_size ); if( size > 0 ) { @@ -68,7 +68,8 @@ Matchfinder::Matchfinder( const int dict_size, const int len_limit ) pos_limit( buffer_size - after_size ), match_len_limit_( len_limit ), prev_positions( new( std::nothrow ) int32_t[num_prev_positions] ), - at_stream_end_( false ) + at_stream_end_( false ), + been_flushed( false ) { prev_pos_tree = new( std::nothrow ) int32_t[2*dictionary_size_]; if( !buffer || !prev_positions || !prev_pos_tree ) @@ -91,6 +92,7 @@ void Matchfinder::reset() throw() pos = 0; cyclic_pos = 0; at_stream_end_ = false; + been_flushed = false; for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = -1; } @@ -126,6 +128,7 @@ int Matchfinder::longest_match_len( int * const distances ) throw() int len_limit = match_len_limit_; if( len_limit > available_bytes() ) { + been_flushed = true; len_limit = available_bytes(); if( len_limit < 4 ) { prev_pos_tree[idx0] = prev_pos_tree[idx1] = -1; return 0; } @@ -160,12 +163,14 @@ int Matchfinder::longest_match_len( int * const distances ) throw() int newpos = prev_positions[key4]; prev_positions[key4] = pos; + int len = 0, len0 = 0, len1 = 0; + for( int count = 16 + ( match_len_limit_ / 2 ); ; ) { if( newpos < min_pos || --count < 0 ) { prev_pos_tree[idx0] = prev_pos_tree[idx1] = -1; break; } const uint8_t * const newdata = buffer + newpos; - int len = 0; + if( been_flushed ) len = 0; while( len < len_limit && newdata[len] == data[len] ) ++len; const int delta = pos - newpos; @@ -181,12 +186,14 @@ int Matchfinder::longest_match_len( int * const distances ) throw() prev_pos_tree[idx0] = newpos; idx0 = newidx + 1; newpos = prev_pos_tree[idx0]; + len0 = len; if( len1 < len ) len = len1; } else { prev_pos_tree[idx1] = newpos; idx1 = newidx; newpos = prev_pos_tree[idx1]; + len1 = len; if( len0 < len ) len = len0; } } else @@ -481,7 +488,7 @@ bool LZ_encoder::sync_flush() bool LZ_encoder::full_flush() { if( member_finished_ || - range_encoder.free_bytes() < (int)sizeof( File_trailer ) + max_marker_size ) + range_encoder.free_bytes() < (int)sizeof (File_trailer) + max_marker_size ) return false; const int pos_state = ( matchfinder.data_position() ) & pos_state_mask; range_encoder.encode_bit( bm_match[state()][pos_state], 1 ); @@ -501,7 +508,7 @@ bool LZ_encoder::full_flush() LZ_encoder::LZ_encoder( Matchfinder & mf, const File_header & header, const long long member_size ) : - member_size_limit( member_size - sizeof( File_trailer ) - max_marker_size ), + member_size_limit( member_size - sizeof (File_trailer) - max_marker_size ), longest_match_found( 0 ), crc_( 0xFFFFFFFF ), matchfinder( mf ), @@ -172,6 +172,7 @@ class Matchfinder int32_t * const prev_positions; // last seen position of key int32_t * prev_pos_tree; bool at_stream_end_; // stream_pos shows real end of file + bool been_flushed; public: Matchfinder( const int dict_size, const int len_limit ); @@ -575,8 +576,8 @@ class LZ_encoder { const int prev_index = trials[cur].prev_index; Trial & prev_trial = trials[prev_index]; - std::swap( dis, prev_trial.dis ); - prev_trial.price = cur - prev_index; // len + prev_trial.price = cur - prev_index; // len + cur = dis; dis = prev_trial.dis; prev_trial.dis = cur; cur = prev_index; } } @@ -82,7 +82,64 @@ int main( const int argc, const char * argv[] ) } int retval = 0; - while( retval <= 2 ) + while( retval <= 1 ) + { + const int read_size = std::fread( in_buffer, 1, buffer_size, file ); + if( read_size <= 0 ) break; // end of file + + for( int l = 0, r = 1; r <= read_size; l = r, ++r ) + { + while( r < read_size && in_buffer[r-1] != '\n' ) ++r; + const int in_size = LZ_compress_write( encoder, in_buffer + l, r - l ); + if( in_size < r - l ) r = l + in_size; + LZ_compress_sync_flush( encoder ); + const int mid_size = LZ_compress_read( encoder, mid_buffer, buffer_size ); + if( mid_size < 0 ) + { + std::fprintf( stderr, "LZ_compress_read error: %s.\n", + LZ_strerror( LZ_compress_errno( encoder ) ) ); + retval = 3; break; + } + LZ_decompress_write( decoder, mid_buffer, mid_size ); + const int out_size = LZ_decompress_read( decoder, out_buffer, buffer_size ); + if( out_size < 0 ) + { + std::fprintf( stderr, "LZ_decompress_read error: %s.\n", + LZ_strerror( LZ_decompress_errno( decoder ) ) ); + retval = 3; break; + } + + if( out_size != in_size || std::memcmp( in_buffer + l, out_buffer, out_size ) ) + { + std::fprintf( stderr, "sync error at pos %d. in_size = %d, out_size = %d\n", + l, in_size, out_size ); + for( int i = 0; i < in_size; ++i ) + std::fputc( in_buffer[l+i], stderr ); + if( in_buffer[l+in_size-1] != '\n' ) + std::fputc( '\n', stderr ); + for( int i = 0; i < out_size; ++i ) + std::fputc( out_buffer[i], stderr ); + std::fputc( '\n', stderr ); + retval = 1; + } + } + } + + if( retval <= 1 ) + { + std::rewind( file ); + if( LZ_compress_finish( encoder ) < 0 || + LZ_decompress_write( decoder, mid_buffer, LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 || + LZ_decompress_read( decoder, out_buffer, buffer_size ) != 0 || + LZ_compress_restart_member( encoder, member_size ) < 0 ) + { + std::fprintf( stderr, "can't finish member: %s.\n", + LZ_strerror( LZ_decompress_errno( decoder ) ) ); + retval = 3; + } + } + + while( retval <= 1 ) { const int read_size = std::fread( in_buffer, 1, buffer_size / 2, file ); if( read_size <= 0 ) break; // end of file @@ -207,7 +207,7 @@ struct File_trailer uint8_t member_size_[8]; // member size including header and trailer static int size( const int version ) - { return sizeof( File_trailer ) - ( ( version >= 1 ) ? 0 : 8 ); } + { return sizeof (File_trailer) - ( ( version >= 1 ) ? 0 : 8 ); } uint32_t data_crc() const throw() { @@ -391,7 +391,7 @@ int LZ_decompress_read( struct LZ_Decoder * const decoder, } if( !d.lz_decoder ) { - if( d.ibuf->used_bytes() < 5 + (int)sizeof( File_header ) ) + if( d.ibuf->used_bytes() < 5 + (int)sizeof (File_header) ) { if( !d.ibuf->at_stream_end() || d.ibuf->finished() ) return 0; d.ibuf->purge(); // remove trailing garbage @@ -29,7 +29,7 @@ extern "C" { #endif -const char * const LZ_version_string = "0.8"; +const char * const LZ_version_string = "0.9"; enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error, LZ_sequence_error, LZ_header_error, LZ_unexpected_eof, @@ -37,7 +37,6 @@ #include <unistd.h> #include <utime.h> #include <sys/stat.h> -#include <sys/time.h> #include "arg_parser.h" #include "lzlib.h" @@ -54,8 +53,8 @@ void show_error( const char * msg, const int errcode = 0, const bool help = false ) throw(); void internal_error( const char * msg ); -int readblock( const int fd, char * buf, const int size ) throw(); -int writeblock( const int fd, const char * buf, const int size ) throw(); +int readblock( const int fd, uint8_t * buf, const int size ) throw(); +int writeblock( const int fd, const uint8_t * buf, const int size ) throw(); namespace { @@ -65,6 +64,12 @@ const char * const Program_name = "Minilzip"; const char * const program_name = "minilzip"; const char * const program_year = "2010"; +#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" }, @@ -79,7 +84,7 @@ struct lzma_options enum Mode { m_compress = 0, m_decompress, m_test }; std::string output_filename; -int outhandle = -1; +int outfd = -1; int verbosity = 0; bool delete_output_on_interrupt = false; @@ -245,7 +250,7 @@ int open_instream( const std::string & name, struct stat * in_statsp, const Mode program_mode, const int eindex, const bool force, const bool to_stdout ) throw() { - int inhandle = -1; + int infd = -1; if( program_mode == m_compress && !force && eindex >= 0 ) { if( verbosity >= 0 ) @@ -255,8 +260,8 @@ int open_instream( const std::string & name, struct stat * in_statsp, } else { - inhandle = open( name.c_str(), O_RDONLY ); - if( inhandle < 0 ) + infd = open( name.c_str(), O_RDONLY | o_binary ); + if( infd < 0 ) { if( verbosity >= 0 ) std::fprintf( stderr, "%s: Can't open input file `%s': %s.\n", @@ -264,7 +269,7 @@ int open_instream( const std::string & name, struct stat * in_statsp, } else { - const int i = fstat( inhandle, in_statsp ); + const int i = fstat( infd, in_statsp ); const mode_t & mode = in_statsp->st_mode; if( i < 0 || !( S_ISREG( mode ) || ( to_stdout && ( S_ISFIFO( mode ) || S_ISSOCK( mode ) || @@ -274,12 +279,12 @@ int open_instream( const std::string & name, struct stat * in_statsp, std::fprintf( stderr, "%s: input file `%s' is not a regular file%s.\n", program_name, name.c_str(), to_stdout ? "" : " and `--stdout' was not specified" ); - close( inhandle ); - inhandle = -1; + close( infd ); + infd = -1; } } } - return inhandle; + return infd; } @@ -313,16 +318,18 @@ void set_d_outname( const std::string & name, const int i ) throw() bool open_outstream( const bool force ) throw() { if( force ) - outhandle = open( output_filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); - else outhandle = open( output_filename.c_str(), O_CREAT | O_EXCL | O_WRONLY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); - if( outhandle < 0 ) + outfd = open( output_filename.c_str(), + O_CREAT | O_TRUNC | O_WRONLY | o_binary, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); + else outfd = open( output_filename.c_str(), + O_CREAT | O_EXCL | O_WRONLY | o_binary, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); + if( outfd < 0 ) { - if( errno == EEXIST ) outhandle = -2; else outhandle = -1; + if( errno == EEXIST ) outfd = -2; else outfd = -1; if( verbosity >= 0 ) { - if( outhandle == -2 ) + if( outfd == -2 ) std::fprintf( stderr, "%s: Output file %s already exists, skipping.\n", program_name, output_filename.c_str() ); else @@ -330,19 +337,19 @@ bool open_outstream( const bool force ) throw() program_name, output_filename.c_str(), std::strerror( errno ) ); } } - return ( outhandle >= 0 ); + return ( outfd >= 0 ); } -bool check_tty( const int inhandle, const Mode program_mode ) throw() +bool check_tty( const int infd, const Mode program_mode ) throw() { - if( program_mode == m_compress && isatty( outhandle ) ) + if( program_mode == m_compress && isatty( outfd ) ) { show_error( "I won't write compressed data to a terminal.", 0, true ); return false; } if( ( program_mode == m_decompress || program_mode == m_test ) && - isatty( inhandle ) ) + isatty( infd ) ) { show_error( "I won't read compressed data from a terminal.", 0, true ); return false; @@ -358,7 +365,7 @@ void cleanup_and_fail( const int retval ) throw() if( verbosity >= 0 ) std::fprintf( stderr, "%s: Deleting output file `%s', if it exists.\n", program_name, output_filename.c_str() ); - if( outhandle >= 0 ) { close( outhandle ); outhandle = -1; } + if( outfd >= 0 ) { close( outfd ); outfd = -1; } if( std::remove( output_filename.c_str() ) != 0 ) show_error( "WARNING: deletion of output file (apparently) failed." ); } @@ -372,11 +379,11 @@ void close_and_set_permissions( const struct stat * const in_statsp ) bool error = false; if( in_statsp ) { - if( fchmod( outhandle, in_statsp->st_mode ) != 0 ) error = true; - else (void)fchown( outhandle, in_statsp->st_uid, in_statsp->st_gid ); + if( fchmod( outfd, in_statsp->st_mode ) != 0 ) error = true; + else (void)fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ); // fchown will in many cases return with EPERM, which can be safely ignored. } - if( close( outhandle ) == 0 ) outhandle = -1; + if( close( outfd ) == 0 ) outfd = -1; else cleanup_and_fail( 1 ); delete_output_on_interrupt = false; if( !in_statsp ) return; @@ -409,28 +416,29 @@ bool next_filename() int do_compress( LZ_Encoder * const encoder, const long long member_size, - const long long volume_size, const int inhandle, + const long long volume_size, const int infd, const Pretty_print & pp, const struct stat * const in_statsp ) { long long partial_volume_size = 0; - const int out_buffer_size = 65536, in_buffer_size = 8 * out_buffer_size; - uint8_t in_buffer[in_buffer_size], out_buffer[out_buffer_size]; + const int buffer_size = 65536; + uint8_t buffer[buffer_size]; - if( verbosity >= 1 ) pp(); while( true ) { - int in_size = std::min( LZ_compress_write_size( encoder ), in_buffer_size ); - if( in_size > 0 ) + int in_size = 0; + while( LZ_compress_write_size( encoder ) > 0 ) { - const int max_in_size = in_size; - in_size = readblock( inhandle, (char *)in_buffer, max_in_size ); - if( in_size != max_in_size && errno ) + const int size = std::min( LZ_compress_write_size( encoder ), + buffer_size ); + const int rd = readblock( infd, buffer, size ); + if( rd != size && errno ) { pp(); show_error( "read error", errno ); return 1; } - if( in_size == 0 ) LZ_compress_finish( encoder ); - else if( in_size != LZ_compress_write( encoder, in_buffer, in_size ) ) + if( rd > 0 && rd != LZ_compress_write( encoder, buffer, rd ) ) internal_error( "library error (LZ_compress_write)" ); + if( rd < size ) LZ_compress_finish( encoder ); + in_size += rd; } - int out_size = LZ_compress_read( encoder, out_buffer, out_buffer_size ); + const int out_size = LZ_compress_read( encoder, buffer, buffer_size ); // std::fprintf( stderr, "%6d in_size, %5d out_size.\n", in_size, out_size ); if( out_size < 0 ) { @@ -442,7 +450,7 @@ int do_compress( LZ_Encoder * const encoder, const long long member_size, } else if( out_size > 0 ) { - const int wr = writeblock( outhandle, (char *)out_buffer, out_size ); + const int wr = writeblock( outfd, buffer, out_size ); if( wr != out_size ) { pp(); show_error( "write error", errno ); return 1; } } @@ -495,7 +503,7 @@ int do_compress( LZ_Encoder * const encoder, const long long member_size, int compress( const long long member_size, const long long volume_size, - const lzma_options & encoder_options, const int inhandle, + const lzma_options & encoder_options, const int infd, const Pretty_print & pp, const struct stat * const in_statsp ) { LZ_Encoder * const encoder = @@ -506,40 +514,54 @@ int compress( const long long member_size, const long long volume_size, if( !encoder || LZ_compress_errno( encoder ) != LZ_ok ) { - if( LZ_compress_errno( encoder ) == LZ_mem_error ) + if( !encoder || LZ_compress_errno( encoder ) == LZ_mem_error ) pp( "not enough memory. Try a smaller dictionary size" ); else internal_error( "invalid argument to encoder" ); retval = 1; } else retval = do_compress( encoder, member_size, volume_size, - inhandle, pp, in_statsp ); + infd, pp, in_statsp ); LZ_compress_close( encoder ); return retval; } -int do_decompress( LZ_Decoder * const decoder, const int inhandle, +int do_decompress( LZ_Decoder * const decoder, const int infd, const Pretty_print & pp, const bool testing ) { - const int in_buffer_size = 65536, out_buffer_size = 8 * in_buffer_size; - uint8_t in_buffer[in_buffer_size], out_buffer[out_buffer_size]; + const int buffer_size = 65536; + uint8_t buffer[buffer_size]; - if( verbosity >= 1 ) pp(); while( true ) { - int in_size = std::min( LZ_decompress_write_size( decoder ), in_buffer_size ); + int in_size = std::min( LZ_decompress_write_size( decoder ), buffer_size ); if( in_size > 0 ) { const int max_in_size = in_size; - in_size = readblock( inhandle, (char *)in_buffer, max_in_size ); + in_size = readblock( infd, buffer, max_in_size ); if( in_size != max_in_size && errno ) { pp(); show_error( "read error", errno ); return 1; } - if( in_size == 0 ) LZ_decompress_finish( decoder ); - else if( in_size != LZ_decompress_write( decoder, in_buffer, in_size ) ) + if( in_size > 0 && in_size != LZ_decompress_write( decoder, buffer, in_size ) ) internal_error( "library error (LZ_decompress_write)" ); + if( in_size < max_in_size ) LZ_decompress_finish( decoder ); + } + int out_size = 0; + while( true ) + { + const int rd = LZ_decompress_read( decoder, buffer, buffer_size ); + if( rd > 0 ) + { + out_size += rd; + if( outfd >= 0 ) + { + const int wr = writeblock( outfd, buffer, rd ); + if( wr != rd ) + { pp(); show_error( "write error", errno ); return 1; } + } + } + else { if( rd < 0 ) out_size = rd; break; } } - int out_size = LZ_decompress_read( decoder, out_buffer, out_buffer_size ); // std::fprintf( stderr, "%5d in_size, %6d out_size.\n", in_size, out_size ); if( out_size < 0 ) { @@ -569,16 +591,14 @@ int do_decompress( LZ_Decoder * const decoder, const int inhandle, LZ_strerror( LZ_decompress_errno( decoder ) ) ); return 1; } - else if( out_size > 0 && outhandle >= 0 ) - { - const int wr = writeblock( outhandle, (char *)out_buffer, out_size ); - if( wr != out_size ) - { pp(); show_error( "write error", errno ); return 1; } - } if( LZ_decompress_finished( decoder ) == 1 ) break; if( in_size == 0 && out_size == 0 ) internal_error( "library error (LZ_decompress_read)" ); } + if( verbosity >= 2 ) + std::fprintf( stderr, "decompressed size %9lld, size %9lld. ", + LZ_decompress_total_out_size( decoder ), + LZ_decompress_total_in_size( decoder ) ); if( verbosity >= 1 ) { if( testing ) std::fprintf( stderr, "ok\n" ); else std::fprintf( stderr, "done\n" ); } @@ -586,7 +606,7 @@ int do_decompress( LZ_Decoder * const decoder, const int inhandle, } -int decompress( const int inhandle, const Pretty_print & pp, +int decompress( const int infd, const Pretty_print & pp, const bool testing ) { LZ_Decoder * const decoder = LZ_decompress_open(); @@ -597,7 +617,7 @@ int decompress( const int inhandle, const Pretty_print & pp, pp( "not enough memory. Find a machine with more memory" ); retval = 1; } - else retval = do_decompress( decoder, inhandle, pp, testing ); + else retval = do_decompress( decoder, infd, pp, testing ); LZ_decompress_close( decoder ); return retval; @@ -665,7 +685,7 @@ void internal_error( const char * msg ) // Returns the number of bytes really read. // If (returned value < size) and (errno == 0), means EOF was reached. // -int readblock( const int fd, char * buf, const int size ) throw() +int readblock( const int fd, uint8_t * buf, const int size ) throw() { int rest = size; errno = 0; @@ -684,7 +704,7 @@ int readblock( const int fd, char * buf, const int size ) throw() // Returns the number of bytes really written. // If (returned value < size), it is always an error. // -int writeblock( const int fd, const char * buf, const int size ) throw() +int writeblock( const int fd, const uint8_t * buf, const int size ) throw() { int rest = size; errno = 0; @@ -717,7 +737,7 @@ int main( const int argc, const char * argv[] ) lzma_options encoder_options = option_mapping[5]; // default = "-6" long long member_size = LLONG_MAX; long long volume_size = LLONG_MAX; - int inhandle = -1; + int infd = -1; Mode program_mode = m_compress; bool force = false; bool keep_input_files = false; @@ -805,11 +825,13 @@ int main( const int argc, const char * argv[] ) } if( filenames.empty() ) filenames.push_back("-"); - if( filenames_given ) set_signals(); + if( !to_stdout && program_mode != m_test && + ( filenames_given || default_output_filename.size() ) ) + set_signals(); Pretty_print pp( filenames ); if( program_mode == m_test ) - outhandle = -1; + outfd = -1; int retval = 0; for( unsigned int i = 0; i < filenames.size(); ++i ) @@ -820,11 +842,11 @@ int main( const int argc, const char * argv[] ) if( !filenames[i].size() || filenames[i] == "-" ) { input_filename.clear(); - inhandle = STDIN_FILENO; + infd = STDIN_FILENO; if( program_mode != m_test ) { if( to_stdout || !default_output_filename.size() ) - outhandle = STDOUT_FILENO; + outfd = STDOUT_FILENO; else { if( program_mode == m_compress ) @@ -832,8 +854,8 @@ int main( const int argc, const char * argv[] ) else output_filename = default_output_filename; if( !open_outstream( force ) ) { - if( outhandle == -1 && retval < 1 ) retval = 1; - close( inhandle ); inhandle = -1; + if( outfd == -1 && retval < 1 ) retval = 1; + close( infd ); infd = -1; continue; } } @@ -843,12 +865,12 @@ int main( const int argc, const char * argv[] ) { input_filename = filenames[i]; const int eindex = extension_index( input_filename ); - inhandle = open_instream( input_filename, &in_stats, program_mode, - eindex, force, to_stdout ); - if( inhandle < 0 ) { if( retval < 1 ) retval = 1; continue; } + infd = open_instream( input_filename, &in_stats, program_mode, + eindex, force, to_stdout ); + if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; } if( program_mode != m_test ) { - if( to_stdout ) outhandle = STDOUT_FILENO; + if( to_stdout ) outfd = STDOUT_FILENO; else { if( program_mode == m_compress ) @@ -856,26 +878,27 @@ int main( const int argc, const char * argv[] ) else set_d_outname( input_filename, eindex ); if( !open_outstream( force ) ) { - if( outhandle == -1 && retval < 1 ) retval = 1; - close( inhandle ); inhandle = -1; + if( outfd == -1 && retval < 1 ) retval = 1; + close( infd ); infd = -1; continue; } } } } - if( !check_tty( inhandle, program_mode ) ) return 1; + if( !check_tty( infd, program_mode ) ) return 1; if( output_filename.size() && !to_stdout && program_mode != m_test ) delete_output_on_interrupt = true; const struct stat * const in_statsp = input_filename.size() ? &in_stats : 0; pp.set_name( input_filename ); + if( verbosity >= 1 ) pp(); int tmp = 0; if( program_mode == m_compress ) - tmp = compress( member_size, volume_size, encoder_options, inhandle, + tmp = compress( member_size, volume_size, encoder_options, infd, pp, in_statsp ); else - tmp = decompress( inhandle, pp, program_mode == m_test ); + tmp = decompress( infd, pp, program_mode == m_test ); if( tmp > retval ) retval = tmp; if( tmp && program_mode != m_test ) cleanup_and_fail( retval ); @@ -883,12 +906,12 @@ int main( const int argc, const char * argv[] ) close_and_set_permissions( in_statsp ); if( input_filename.size() ) { - close( inhandle ); inhandle = -1; + close( infd ); infd = -1; if( !keep_input_files && !to_stdout && program_mode != m_test ) std::remove( input_filename.c_str() ); } } - if( outhandle >= 0 && close( outhandle ) != 0 ) + if( outfd >= 0 && close( outfd ) != 0 ) { if( verbosity >= 0 ) std::fprintf( stderr, "%s: Can't close stdout: %s.\n", |