1
0
Fork 0

Adding upstream version 0.9.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-20 16:22:03 +01:00
parent f78fedc24f
commit fefe46d70f
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
15 changed files with 230 additions and 159 deletions

View file

@ -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> 2010-01-17 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 0.8 released. * Version 0.8 released.

View file

@ -33,7 +33,6 @@ the main archive.
documentation. (You might have to run ldconfig also). documentation. (You might have to run ldconfig also).
Another way Another way
----------- -----------
You can also compile lzlib into a separate directory. To do this, you You can also compile lzlib into a separate directory. To do this, you

View file

@ -54,14 +54,15 @@ sh_lzlib.o : lzlib.cc
$(lib_objs) : Makefile lzlib.h lzip.h $(lib_objs) : Makefile lzlib.h lzip.h
$(sh_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 decoder.o : decoder.h
encoder.o : encoder.h encoder.o : encoder.h
lzlib.o : decoder.h encoder.h lzlib.o : decoder.h encoder.h
sh_decoder.o : decoder.h sh_decoder.o : decoder.h
sh_encoder.o : encoder.h sh_encoder.o : encoder.h
sh_lzlib.o : decoder.h encoder.h sh_lzlib.o : decoder.h encoder.h
arg_parser.o : Makefile arg_parser.h main.o : arg_parser.h lzlib.h
main.o : Makefile arg_parser.h lzlib.h
lzcheck.o : Makefile lzlib.h lzcheck.o : Makefile lzlib.h
@ -85,44 +86,44 @@ check : all lzcheck
@$(VPATH)/testsuite/check.sh $(VPATH)/testsuite @$(VPATH)/testsuite/check.sh $(VPATH)/testsuite
install : all install-info install : all install-info
if [ ! -d $(DESTDIR)$(includedir) ] ; then $(INSTALL_DIR) $(DESTDIR)$(includedir) ; fi if [ ! -d "$(DESTDIR)$(includedir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(includedir)" ; fi
if [ ! -d $(DESTDIR)$(libdir) ] ; then $(INSTALL_DIR) $(DESTDIR)$(libdir) ; fi if [ ! -d "$(DESTDIR)$(libdir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(libdir)" ; fi
$(INSTALL_DATA) $(VPATH)/$(libname)lib.h $(DESTDIR)$(includedir)/$(libname)lib.h $(INSTALL_DATA) $(VPATH)/$(libname)lib.h "$(DESTDIR)$(includedir)/$(libname)lib.h"
$(INSTALL_DATA) ./lib$(libname).a $(DESTDIR)$(libdir)/lib$(libname).a $(INSTALL_DATA) ./lib$(libname).a "$(DESTDIR)$(libdir)/lib$(libname).a"
if [ -n "$(progname_shared)" ] ; then \ if [ -n "$(progname_shared)" ] ; then \
$(INSTALL_PROGRAM) ./lib$(libname).so.$(pkgversion) $(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion) ; \ $(INSTALL_PROGRAM) ./lib$(libname).so.$(pkgversion) "$(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion)" ; \
if [ -e $(DESTDIR)$(libdir)/lib$(libname).so.$(soversion) ] ; then \ if [ -e "$(DESTDIR)$(libdir)/lib$(libname).so.$(soversion)" ] ; then \
run_ldconfig=no ; rm -f $(DESTDIR)$(libdir)/lib$(libname).so.$(soversion) ; \ run_ldconfig=no ; rm -f "$(DESTDIR)$(libdir)/lib$(libname).so.$(soversion)" ; \
else run_ldconfig=yes ; \ else run_ldconfig=yes ; \
fi ; \ fi ; \
cd $(DESTDIR)$(libdir) && ln -s lib$(libname).so.$(pkgversion) lib$(libname).so.$(soversion) ; \ cd "$(DESTDIR)$(libdir)" && ln -s lib$(libname).so.$(pkgversion) lib$(libname).so.$(soversion) ; \
if [ $${run_ldconfig} = yes ] ; then $(LDCONFIG) $(DESTDIR)$(libdir) ; fi ; \ if [ $${run_ldconfig} = yes ] ; then $(LDCONFIG) "$(DESTDIR)$(libdir)" ; fi ; \
fi fi
install-info : install-info :
if [ ! -d $(DESTDIR)$(infodir) ] ; then $(INSTALL_DIR) $(DESTDIR)$(infodir) ; fi if [ ! -d "$(DESTDIR)$(infodir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(infodir)" ; fi
$(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info $(DESTDIR)$(infodir)/$(pkgname).info $(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info "$(DESTDIR)$(infodir)/$(pkgname).info"
-install-info --info-dir=$(DESTDIR)$(infodir) $(DESTDIR)$(infodir)/$(pkgname).info -install-info --info-dir="$(DESTDIR)$(infodir)" $(DESTDIR)$(infodir)/$(pkgname).info
install-man : install-man :
if [ ! -d $(DESTDIR)$(mandir)/man1 ] ; then $(INSTALL_DIR) $(DESTDIR)$(mandir)/man1 ; fi if [ ! -d "$(DESTDIR)$(mandir)/man1" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1" ; fi
$(INSTALL_DATA) $(VPATH)/doc/$(progname).1 $(DESTDIR)$(mandir)/man1/$(progname).1 $(INSTALL_DATA) $(VPATH)/doc/$(progname).1 "$(DESTDIR)$(mandir)/man1/$(progname).1"
install-strip : all install-strip : all
$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install $(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install
uninstall : uninstall-info uninstall : uninstall-info
-rm -f $(DESTDIR)$(includedir)/$(libname)lib.h -rm -f "$(DESTDIR)$(includedir)/$(libname)lib.h"
-rm -f $(DESTDIR)$(libdir)/lib$(libname).a -rm -f "$(DESTDIR)$(libdir)/lib$(libname).a"
-rm -f $(DESTDIR)$(libdir)/lib$(libname).so.$(soversion) -rm -f "$(DESTDIR)$(libdir)/lib$(libname).so.$(soversion)"
-rm -f $(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion) -rm -f "$(DESTDIR)$(libdir)/lib$(libname).so.$(pkgversion)"
uninstall-info : uninstall-info :
-install-info --info-dir=$(DESTDIR)$(infodir) --remove $(DESTDIR)$(infodir)/$(pkgname).info -install-info --info-dir="$(DESTDIR)$(infodir)" --remove "$(DESTDIR)$(infodir)/$(pkgname).info"
-rm -f $(DESTDIR)$(infodir)/$(pkgname).info -rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"
uninstall-man : uninstall-man :
-rm -f $(DESTDIR)$(mandir)/man1/$(progname).1 -rm -f "$(DESTDIR)$(mandir)/man1/$(progname).1"
dist : doc dist : doc
ln -sf $(VPATH) $(DISTNAME) ln -sf $(VPATH) $(DISTNAME)

29
NEWS
View file

@ -1,28 +1,3 @@
Changes in version 0.8: Changes in version 0.9:
API changes: Compression time has been reduced by 8%.
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.

6
configure vendored
View file

@ -5,12 +5,12 @@
# This configure script is free software: you have unlimited permission # This configure script is free software: you have unlimited permission
# to copy, distribute and modify it. # to copy, distribute and modify it.
# #
# Date of this version: 2010-01-17 # Date of this version: 2010-02-10
args= args=
no_create= no_create=
pkgname=lzlib pkgname=lzlib
pkgversion=0.8 pkgversion=0.9
soversion=0 soversion=0
progname=minilzip progname=minilzip
progname_shared= progname_shared=
@ -149,7 +149,7 @@ if [ -z "${CXX}" ] ; then # Let the user override the test.
fi fi
echo echo
if [ -z ${no_create} ] ; then if [ -z "${no_create}" ] ; then
echo "creating config.status" echo "creating config.status"
rm -f config.status rm -f config.status
cat > config.status << EOF cat > config.status << EOF

View file

@ -44,6 +44,7 @@ const CRC32 crc32;
// Returns the number of bytes copied. // Returns the number of bytes copied.
int Circular_buffer::read_data( uint8_t * const out_buffer, const int out_size ) throw() int Circular_buffer::read_data( uint8_t * const out_buffer, const int out_size ) throw()
{ {
if( out_size < 0 ) return 0;
int size = 0; int size = 0;
if( get > put ) 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. // Returns the number of bytes copied.
int Circular_buffer::write_data( const uint8_t * const in_buffer, const int in_size ) throw() 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; int size = 0;
if( put >= get ) if( put >= get )
{ {

View file

@ -12,7 +12,7 @@ File: lzlib.info, Node: Top, Next: Introduction, Up: (dir)
Lzlib Manual 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: * Menu:
@ -676,17 +676,17 @@ Concept Index
 
Tag Table: Tag Table:
Node: Top219 Node: Top219
Node: Introduction1157 Node: Introduction1158
Node: Library Version2932 Node: Library Version2933
Node: Buffering3577 Node: Buffering3578
Node: Parameter Limits4697 Node: Parameter Limits4698
Node: Compression Functions5654 Node: Compression Functions5655
Node: Decompression Functions11700 Node: Decompression Functions11701
Node: Error Codes16762 Node: Error Codes16763
Node: Error Messages18701 Node: Error Messages18702
Node: Data Format19280 Node: Data Format19281
Node: Examples21250 Node: Examples21251
Node: Problems24826 Node: Problems24827
Node: Concept Index25398 Node: Concept Index25399
 
End Tag Table End Tag Table

View file

@ -5,8 +5,8 @@
@finalout @finalout
@c %**end of header @c %**end of header
@set UPDATED 17 January 2010 @set UPDATED 10 February 2010
@set VERSION 0.8 @set VERSION 0.9
@dircategory Data Compression @dircategory Data Compression
@direntry @direntry

View file

@ -44,7 +44,7 @@ const Prob_prices prob_prices;
int Matchfinder::write_data( const uint8_t * const in_buffer, const int in_size ) throw() 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 ); const int size = std::min( buffer_size - stream_pos, in_size );
if( size > 0 ) if( size > 0 )
{ {
@ -68,7 +68,8 @@ Matchfinder::Matchfinder( const int dict_size, const int len_limit )
pos_limit( buffer_size - after_size ), pos_limit( buffer_size - after_size ),
match_len_limit_( len_limit ), match_len_limit_( len_limit ),
prev_positions( new( std::nothrow ) int32_t[num_prev_positions] ), 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_]; prev_pos_tree = new( std::nothrow ) int32_t[2*dictionary_size_];
if( !buffer || !prev_positions || !prev_pos_tree ) if( !buffer || !prev_positions || !prev_pos_tree )
@ -91,6 +92,7 @@ void Matchfinder::reset() throw()
pos = 0; pos = 0;
cyclic_pos = 0; cyclic_pos = 0;
at_stream_end_ = false; at_stream_end_ = false;
been_flushed = false;
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] = -1;
} }
@ -126,6 +128,7 @@ int Matchfinder::longest_match_len( int * const distances ) throw()
int len_limit = match_len_limit_; int len_limit = match_len_limit_;
if( len_limit > available_bytes() ) if( len_limit > available_bytes() )
{ {
been_flushed = true;
len_limit = available_bytes(); len_limit = available_bytes();
if( len_limit < 4 ) if( len_limit < 4 )
{ prev_pos_tree[idx0] = prev_pos_tree[idx1] = -1; return 0; } { 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]; int newpos = prev_positions[key4];
prev_positions[key4] = pos; prev_positions[key4] = pos;
int len = 0, len0 = 0, len1 = 0;
for( int count = 16 + ( match_len_limit_ / 2 ); ; ) for( int count = 16 + ( match_len_limit_ / 2 ); ; )
{ {
if( newpos < min_pos || --count < 0 ) if( newpos < min_pos || --count < 0 )
{ prev_pos_tree[idx0] = prev_pos_tree[idx1] = -1; break; } { prev_pos_tree[idx0] = prev_pos_tree[idx1] = -1; break; }
const uint8_t * const newdata = buffer + newpos; const uint8_t * const newdata = buffer + newpos;
int len = 0; if( been_flushed ) len = 0;
while( len < len_limit && newdata[len] == data[len] ) ++len; while( len < len_limit && newdata[len] == data[len] ) ++len;
const int delta = pos - newpos; const int delta = pos - newpos;
@ -181,12 +186,14 @@ int Matchfinder::longest_match_len( int * const distances ) throw()
prev_pos_tree[idx0] = newpos; prev_pos_tree[idx0] = newpos;
idx0 = newidx + 1; idx0 = newidx + 1;
newpos = prev_pos_tree[idx0]; newpos = prev_pos_tree[idx0];
len0 = len; if( len1 < len ) len = len1;
} }
else else
{ {
prev_pos_tree[idx1] = newpos; prev_pos_tree[idx1] = newpos;
idx1 = newidx; idx1 = newidx;
newpos = prev_pos_tree[idx1]; newpos = prev_pos_tree[idx1];
len1 = len; if( len0 < len ) len = len0;
} }
} }
else else
@ -481,7 +488,7 @@ bool LZ_encoder::sync_flush()
bool LZ_encoder::full_flush() bool LZ_encoder::full_flush()
{ {
if( member_finished_ || 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; return false;
const int pos_state = ( matchfinder.data_position() ) & pos_state_mask; const int pos_state = ( matchfinder.data_position() ) & pos_state_mask;
range_encoder.encode_bit( bm_match[state()][pos_state], 1 ); 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, LZ_encoder::LZ_encoder( Matchfinder & mf, const File_header & header,
const long long member_size ) 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 ), longest_match_found( 0 ),
crc_( 0xFFFFFFFF ), crc_( 0xFFFFFFFF ),
matchfinder( mf ), matchfinder( mf ),

View file

@ -172,6 +172,7 @@ class Matchfinder
int32_t * const prev_positions; // last seen position of key int32_t * const prev_positions; // last seen position of key
int32_t * prev_pos_tree; int32_t * prev_pos_tree;
bool at_stream_end_; // stream_pos shows real end of file bool at_stream_end_; // stream_pos shows real end of file
bool been_flushed;
public: public:
Matchfinder( const int dict_size, const int len_limit ); Matchfinder( const int dict_size, const int len_limit );
@ -575,8 +576,8 @@ class LZ_encoder
{ {
const int prev_index = trials[cur].prev_index; const int prev_index = trials[cur].prev_index;
Trial & prev_trial = trials[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; cur = prev_index;
} }
} }

View file

@ -82,7 +82,64 @@ int main( const int argc, const char * argv[] )
} }
int retval = 0; 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 ); const int read_size = std::fread( in_buffer, 1, buffer_size / 2, file );
if( read_size <= 0 ) break; // end of file if( read_size <= 0 ) break; // end of file

2
lzip.h
View file

@ -207,7 +207,7 @@ struct File_trailer
uint8_t member_size_[8]; // member size including header and trailer uint8_t member_size_[8]; // member size including header and trailer
static int size( const int version ) 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() uint32_t data_crc() const throw()
{ {

View file

@ -391,7 +391,7 @@ int LZ_decompress_read( struct LZ_Decoder * const decoder,
} }
if( !d.lz_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; if( !d.ibuf->at_stream_end() || d.ibuf->finished() ) return 0;
d.ibuf->purge(); // remove trailing garbage d.ibuf->purge(); // remove trailing garbage

View file

@ -29,7 +29,7 @@
extern "C" { extern "C" {
#endif #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, enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error,
LZ_sequence_error, LZ_header_error, LZ_unexpected_eof, LZ_sequence_error, LZ_header_error, LZ_unexpected_eof,

175
main.cc
View file

@ -37,7 +37,6 @@
#include <unistd.h> #include <unistd.h>
#include <utime.h> #include <utime.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include "arg_parser.h" #include "arg_parser.h"
#include "lzlib.h" #include "lzlib.h"
@ -54,8 +53,8 @@
void show_error( const char * msg, const int errcode = 0, const bool help = false ) throw(); void show_error( const char * msg, const int errcode = 0, const bool help = false ) throw();
void internal_error( const char * msg ); void internal_error( const char * msg );
int readblock( const int fd, char * buf, const int size ) throw(); int readblock( const int fd, uint8_t * buf, const int size ) throw();
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();
namespace { namespace {
@ -65,6 +64,12 @@ const char * const Program_name = "Minilzip";
const char * const program_name = "minilzip"; const char * const program_name = "minilzip";
const char * const program_year = "2010"; 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[] = { struct { const char * from; const char * to; } const known_extensions[] = {
{ ".lz", "" }, { ".lz", "" },
{ ".tlz", ".tar" }, { ".tlz", ".tar" },
@ -79,7 +84,7 @@ struct lzma_options
enum Mode { m_compress = 0, m_decompress, m_test }; enum Mode { m_compress = 0, m_decompress, m_test };
std::string output_filename; std::string output_filename;
int outhandle = -1; int outfd = -1;
int verbosity = 0; int verbosity = 0;
bool delete_output_on_interrupt = false; 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 Mode program_mode, const int eindex,
const bool force, const bool to_stdout ) throw() const bool force, const bool to_stdout ) throw()
{ {
int inhandle = -1; int infd = -1;
if( program_mode == m_compress && !force && eindex >= 0 ) if( program_mode == m_compress && !force && eindex >= 0 )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
@ -255,8 +260,8 @@ int open_instream( const std::string & name, struct stat * in_statsp,
} }
else else
{ {
inhandle = open( name.c_str(), O_RDONLY ); infd = open( name.c_str(), O_RDONLY | o_binary );
if( inhandle < 0 ) if( infd < 0 )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
std::fprintf( stderr, "%s: Can't open input file `%s': %s.\n", 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 else
{ {
const int i = fstat( inhandle, in_statsp ); const int i = fstat( infd, in_statsp );
const mode_t & mode = in_statsp->st_mode; const mode_t & mode = in_statsp->st_mode;
if( i < 0 || !( S_ISREG( mode ) || ( to_stdout && if( i < 0 || !( S_ISREG( mode ) || ( to_stdout &&
( S_ISFIFO( mode ) || S_ISSOCK( mode ) || ( 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", std::fprintf( stderr, "%s: input file `%s' is not a regular file%s.\n",
program_name, name.c_str(), program_name, name.c_str(),
to_stdout ? "" : " and `--stdout' was not specified" ); to_stdout ? "" : " and `--stdout' was not specified" );
close( inhandle ); close( infd );
inhandle = -1; 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() bool open_outstream( const bool force ) throw()
{ {
if( force ) if( force )
outhandle = open( output_filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY, outfd = open( output_filename.c_str(),
O_CREAT | O_TRUNC | O_WRONLY | o_binary,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
else outhandle = open( output_filename.c_str(), O_CREAT | O_EXCL | O_WRONLY, else outfd = open( output_filename.c_str(),
O_CREAT | O_EXCL | O_WRONLY | o_binary,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
if( outhandle < 0 ) if( outfd < 0 )
{ {
if( errno == EEXIST ) outhandle = -2; else outhandle = -1; if( errno == EEXIST ) outfd = -2; else outfd = -1;
if( verbosity >= 0 ) if( verbosity >= 0 )
{ {
if( outhandle == -2 ) if( outfd == -2 )
std::fprintf( stderr, "%s: Output file %s already exists, skipping.\n", std::fprintf( stderr, "%s: Output file %s already exists, skipping.\n",
program_name, output_filename.c_str() ); program_name, output_filename.c_str() );
else else
@ -330,19 +337,19 @@ bool open_outstream( const bool force ) throw()
program_name, output_filename.c_str(), std::strerror( errno ) ); 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 ); show_error( "I won't write compressed data to a terminal.", 0, true );
return false; return false;
} }
if( ( program_mode == m_decompress || program_mode == m_test ) && 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 ); show_error( "I won't read compressed data from a terminal.", 0, true );
return false; return false;
@ -358,7 +365,7 @@ void cleanup_and_fail( const int retval ) throw()
if( verbosity >= 0 ) if( verbosity >= 0 )
std::fprintf( stderr, "%s: Deleting output file `%s', if it exists.\n", std::fprintf( stderr, "%s: Deleting output file `%s', if it exists.\n",
program_name, output_filename.c_str() ); 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 ) if( std::remove( output_filename.c_str() ) != 0 )
show_error( "WARNING: deletion of output file (apparently) failed." ); 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; bool error = false;
if( in_statsp ) if( in_statsp )
{ {
if( fchmod( outhandle, in_statsp->st_mode ) != 0 ) error = true; if( fchmod( outfd, in_statsp->st_mode ) != 0 ) error = true;
else (void)fchown( outhandle, in_statsp->st_uid, in_statsp->st_gid ); 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. // 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 ); else cleanup_and_fail( 1 );
delete_output_on_interrupt = false; delete_output_on_interrupt = false;
if( !in_statsp ) return; if( !in_statsp ) return;
@ -409,28 +416,29 @@ bool next_filename()
int do_compress( LZ_Encoder * const encoder, const long long member_size, 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 ) const Pretty_print & pp, const struct stat * const in_statsp )
{ {
long long partial_volume_size = 0; long long partial_volume_size = 0;
const int out_buffer_size = 65536, in_buffer_size = 8 * out_buffer_size; const int buffer_size = 65536;
uint8_t in_buffer[in_buffer_size], out_buffer[out_buffer_size]; uint8_t buffer[buffer_size];
if( verbosity >= 1 ) pp();
while( true ) while( true )
{ {
int in_size = std::min( LZ_compress_write_size( encoder ), in_buffer_size ); int in_size = 0;
if( in_size > 0 ) while( LZ_compress_write_size( encoder ) > 0 )
{ {
const int max_in_size = in_size; const int size = std::min( LZ_compress_write_size( encoder ),
in_size = readblock( inhandle, (char *)in_buffer, max_in_size ); buffer_size );
if( in_size != max_in_size && errno ) const int rd = readblock( infd, buffer, size );
if( rd != size && errno )
{ pp(); show_error( "read error", errno ); return 1; } { pp(); show_error( "read error", errno ); return 1; }
if( in_size == 0 ) LZ_compress_finish( encoder ); if( rd > 0 && rd != LZ_compress_write( encoder, buffer, rd ) )
else if( in_size != LZ_compress_write( encoder, in_buffer, in_size ) )
internal_error( "library error (LZ_compress_write)" ); 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 ); // std::fprintf( stderr, "%6d in_size, %5d out_size.\n", in_size, out_size );
if( out_size < 0 ) 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 ) 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 ) if( wr != out_size )
{ pp(); show_error( "write error", errno ); return 1; } { 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, 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 ) const Pretty_print & pp, const struct stat * const in_statsp )
{ {
LZ_Encoder * const encoder = 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( !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" ); pp( "not enough memory. Try a smaller dictionary size" );
else else
internal_error( "invalid argument to encoder" ); internal_error( "invalid argument to encoder" );
retval = 1; retval = 1;
} }
else retval = do_compress( encoder, member_size, volume_size, else retval = do_compress( encoder, member_size, volume_size,
inhandle, pp, in_statsp ); infd, pp, in_statsp );
LZ_compress_close( encoder ); LZ_compress_close( encoder );
return retval; 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 Pretty_print & pp, const bool testing )
{ {
const int in_buffer_size = 65536, out_buffer_size = 8 * in_buffer_size; const int buffer_size = 65536;
uint8_t in_buffer[in_buffer_size], out_buffer[out_buffer_size]; uint8_t buffer[buffer_size];
if( verbosity >= 1 ) pp();
while( true ) 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 ) if( in_size > 0 )
{ {
const int max_in_size = in_size; 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 ) if( in_size != max_in_size && errno )
{ pp(); show_error( "read error", errno ); return 1; } { pp(); show_error( "read error", errno ); return 1; }
if( in_size == 0 ) LZ_decompress_finish( decoder ); if( in_size > 0 && in_size != LZ_decompress_write( decoder, buffer, in_size ) )
else if( in_size != LZ_decompress_write( decoder, in_buffer, in_size ) )
internal_error( "library error (LZ_decompress_write)" ); 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 ); // std::fprintf( stderr, "%5d in_size, %6d out_size.\n", in_size, out_size );
if( out_size < 0 ) if( out_size < 0 )
{ {
@ -569,16 +591,14 @@ int do_decompress( LZ_Decoder * const decoder, const int inhandle,
LZ_strerror( LZ_decompress_errno( decoder ) ) ); LZ_strerror( LZ_decompress_errno( decoder ) ) );
return 1; 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( LZ_decompress_finished( decoder ) == 1 ) break;
if( in_size == 0 && out_size == 0 ) if( in_size == 0 && out_size == 0 )
internal_error( "library error (LZ_decompress_read)" ); 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( verbosity >= 1 )
{ if( testing ) std::fprintf( stderr, "ok\n" ); { if( testing ) std::fprintf( stderr, "ok\n" );
else std::fprintf( stderr, "done\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 ) const bool testing )
{ {
LZ_Decoder * const decoder = LZ_decompress_open(); 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" ); pp( "not enough memory. Find a machine with more memory" );
retval = 1; retval = 1;
} }
else retval = do_decompress( decoder, inhandle, pp, testing ); else retval = do_decompress( decoder, infd, pp, testing );
LZ_decompress_close( decoder ); LZ_decompress_close( decoder );
return retval; return retval;
@ -665,7 +685,7 @@ void internal_error( const char * msg )
// Returns the number of bytes really read. // Returns the number of bytes really read.
// If (returned value < size) and (errno == 0), means EOF was reached. // 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; int rest = size;
errno = 0; errno = 0;
@ -684,7 +704,7 @@ int readblock( const int fd, char * buf, const int size ) throw()
// Returns the number of bytes really written. // Returns the number of bytes really written.
// If (returned value < size), it is always an error. // 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; int rest = size;
errno = 0; errno = 0;
@ -717,7 +737,7 @@ int main( const int argc, const char * argv[] )
lzma_options encoder_options = option_mapping[5]; // default = "-6" lzma_options encoder_options = option_mapping[5]; // default = "-6"
long long member_size = LLONG_MAX; long long member_size = LLONG_MAX;
long long volume_size = LLONG_MAX; long long volume_size = LLONG_MAX;
int inhandle = -1; int infd = -1;
Mode program_mode = m_compress; Mode program_mode = m_compress;
bool force = false; bool force = false;
bool keep_input_files = 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.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 ); Pretty_print pp( filenames );
if( program_mode == m_test ) if( program_mode == m_test )
outhandle = -1; outfd = -1;
int retval = 0; int retval = 0;
for( unsigned int i = 0; i < filenames.size(); ++i ) 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] == "-" ) if( !filenames[i].size() || filenames[i] == "-" )
{ {
input_filename.clear(); input_filename.clear();
inhandle = STDIN_FILENO; infd = STDIN_FILENO;
if( program_mode != m_test ) if( program_mode != m_test )
{ {
if( to_stdout || !default_output_filename.size() ) if( to_stdout || !default_output_filename.size() )
outhandle = STDOUT_FILENO; outfd = STDOUT_FILENO;
else else
{ {
if( program_mode == m_compress ) if( program_mode == m_compress )
@ -832,8 +854,8 @@ int main( const int argc, const char * argv[] )
else output_filename = default_output_filename; else output_filename = default_output_filename;
if( !open_outstream( force ) ) if( !open_outstream( force ) )
{ {
if( outhandle == -1 && retval < 1 ) retval = 1; if( outfd == -1 && retval < 1 ) retval = 1;
close( inhandle ); inhandle = -1; close( infd ); infd = -1;
continue; continue;
} }
} }
@ -843,12 +865,12 @@ int main( const int argc, const char * argv[] )
{ {
input_filename = filenames[i]; input_filename = filenames[i];
const int eindex = extension_index( input_filename ); const int eindex = extension_index( input_filename );
inhandle = open_instream( input_filename, &in_stats, program_mode, infd = open_instream( input_filename, &in_stats, program_mode,
eindex, force, to_stdout ); eindex, force, to_stdout );
if( inhandle < 0 ) { if( retval < 1 ) retval = 1; continue; } if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; }
if( program_mode != m_test ) if( program_mode != m_test )
{ {
if( to_stdout ) outhandle = STDOUT_FILENO; if( to_stdout ) outfd = STDOUT_FILENO;
else else
{ {
if( program_mode == m_compress ) if( program_mode == m_compress )
@ -856,26 +878,27 @@ int main( const int argc, const char * argv[] )
else set_d_outname( input_filename, eindex ); else set_d_outname( input_filename, eindex );
if( !open_outstream( force ) ) if( !open_outstream( force ) )
{ {
if( outhandle == -1 && retval < 1 ) retval = 1; if( outfd == -1 && retval < 1 ) retval = 1;
close( inhandle ); inhandle = -1; close( infd ); infd = -1;
continue; 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 ) if( output_filename.size() && !to_stdout && program_mode != m_test )
delete_output_on_interrupt = true; delete_output_on_interrupt = true;
const struct stat * const in_statsp = input_filename.size() ? &in_stats : 0; const struct stat * const in_statsp = input_filename.size() ? &in_stats : 0;
pp.set_name( input_filename ); pp.set_name( input_filename );
if( verbosity >= 1 ) pp();
int tmp = 0; int tmp = 0;
if( program_mode == m_compress ) 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 ); pp, in_statsp );
else else
tmp = decompress( inhandle, pp, program_mode == m_test ); tmp = decompress( infd, pp, program_mode == m_test );
if( tmp > retval ) retval = tmp; if( tmp > retval ) retval = tmp;
if( tmp && program_mode != m_test ) cleanup_and_fail( retval ); 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 ); close_and_set_permissions( in_statsp );
if( input_filename.size() ) if( input_filename.size() )
{ {
close( inhandle ); inhandle = -1; close( infd ); infd = -1;
if( !keep_input_files && !to_stdout && program_mode != m_test ) if( !keep_input_files && !to_stdout && program_mode != m_test )
std::remove( input_filename.c_str() ); std::remove( input_filename.c_str() );
} }
} }
if( outhandle >= 0 && close( outhandle ) != 0 ) if( outfd >= 0 && close( outfd ) != 0 )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
std::fprintf( stderr, "%s: Can't close stdout: %s.\n", std::fprintf( stderr, "%s: Can't close stdout: %s.\n",