Hello all,

I noticed that lzip isn't particularly friendly to Windows. :)

Attached patch, based on lzip-1.6, makes it work properly on Windows, binary console IO works too. I hope to get this patch into mainline lzip.

Make check reports tests completed successfully.

Comments?
diff --git a/decoder.cc b/decoder.cc
index f5cb101..a417e24 100644
--- a/decoder.cc
+++ b/decoder.cc
@@ -106,7 +106,7 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp ) 
const
       {
       if( trailer.data_size() >= 0 )
         { pp();
-          std::fprintf( stderr, "data size mismatch; trailer says %lld, data 
size is %lld.\n",
+          std::fprintf( stderr, "data size mismatch; trailer says %"LZIPLL"d, 
data size is %"LZIPLL"d.\n",
                         trailer.data_size(), data_position() ); }
       else pp( "member trailer is corrupt" );
       }
@@ -118,13 +118,13 @@ bool LZ_decoder::verify_trailer( const Pretty_print & pp 
) const
       {
       if( trailer.member_size() >= 0 )
         { pp();
-          std::fprintf( stderr, "member size mismatch; trailer says %lld, 
member size is %lld.\n",
+          std::fprintf( stderr, "member size mismatch; trailer says 
%"LZIPLL"d, member size is %"LZIPLL"d.\n",
                         trailer.member_size(), member_position() ); }
       else pp( "member trailer is corrupt" );
       }
     }
   if( !error && verbosity >= 3 )
-    std::fprintf( stderr, "data crc %08X, data size %8lld, member size %8lld.  
",
+    std::fprintf( stderr, "data crc %08X, data size %8"LZIPLL"d, member size 
%8"LZIPLL"d.  ",
                   (unsigned int)trailer.data_crc(), trailer.data_size(),
                   trailer.member_size() );
   return !error;
diff --git a/lzip.h b/lzip.h
index 0a2293d..ee1bfd5 100644
--- a/lzip.h
+++ b/lzip.h
@@ -15,6 +15,21 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef _WIN32
+#define LZIPLL "I64"
+#define S_ISSOCK(x) 0
+/* We don't care about permissions */
+#define S_IRGRP _S_IREAD
+#define S_IROTH _S_IREAD
+#define S_IRGRP _S_IREAD
+#define S_IROTH _S_IREAD
+/* Unimplemented Functions */
+#define fchmod(x,y) 0
+#define fchown(x,y,z) 0
+#else
+#define LZIPLL "ll"
+#endif
+
 class State
   {
   unsigned char st;
diff --git a/lziprecover.cc b/lziprecover.cc
index deed53c..baa074f 100644
--- a/lziprecover.cc
+++ b/lziprecover.cc
@@ -75,7 +75,7 @@ void show_version() throw()
 
 int open_instream( const std::string & input_filename ) throw()
   {
-  int ides = open( input_filename.c_str(), O_RDONLY );
+  int ides = open( input_filename.c_str(), O_RDONLY | O_BINARY);
   if( ides < 0 )
     {
     if( verbosity >= 0 )
@@ -101,7 +101,7 @@ int open_instream( const std::string & input_filename ) 
throw()
 
 int open_outstream( const std::string & output_filename ) throw()
   {
-  int odes = open( output_filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 
S_IRUSR | S_IWUSR );
+  int odes = open( output_filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY| 
O_BINARY, S_IRUSR | S_IWUSR );
   if( odes < 0 )
     {
     if( verbosity >= 0 )
@@ -279,6 +279,10 @@ int readblock( const int fd, char * buf, const int size ) 
throw()
   {
   int rest = size;
   errno = 0;
+  #ifdef _WIN32
+  /*set IO to binary mode */
+  int prevmode = _setmode(  fd, _O_BINARY);
+  #endif
   while( rest > 0 )
     {
     errno = 0;
@@ -287,6 +291,10 @@ int readblock( const int fd, char * buf, const int size ) 
throw()
     else if( n == 0 ) break;
     else if( errno != EINTR && errno != EAGAIN ) break;
     }
+  #ifdef _WIN32
+  /*Restore IO to default mode */
+  _setmode(  fd, prevmode);
+  #endif
   return ( rest > 0 ) ? size - rest : size;
   }
 
@@ -298,6 +306,10 @@ int writeblock( const int fd, const char * buf, const int 
size ) throw()
   {
   int rest = size;
   errno = 0;
+  #ifdef _WIN32
+  /*set IO to binary mode */
+  int prevmode = _setmode(  fd, _O_BINARY);
+  #endif
   while( rest > 0 )
     {
     errno = 0;
@@ -305,6 +317,10 @@ int writeblock( const int fd, const char * buf, const int 
size ) throw()
     if( n > 0 ) rest -= n;
     else if( errno && errno != EINTR && errno != EAGAIN ) break;
     }
+  #ifdef _WIN32
+  /*Restore IO to default mode */
+  _setmode(  fd, prevmode);
+  #endif
   return ( rest > 0 ) ? size - rest : size;
   }
 
diff --git a/main.cc b/main.cc
index 41fde78..1a9af04 100644
--- a/main.cc
+++ b/main.cc
@@ -140,7 +140,7 @@ const char * format_num( long long num, long long limit = 
9999,
   for( int i = 0; i < 8 && ( llabs( num ) > limit ||
        ( llabs( num ) >= factor && num % factor == 0 ) ); ++i )
     { num /= factor; p = prefix[i]; }
-  snprintf( buf, sizeof buf, "%lld %s", num, p );
+  snprintf( buf, sizeof buf, "%"LZIPLL"d %s", num, p );
   return buf;
   }
 
@@ -240,7 +240,7 @@ int open_instream( const std::string & name, struct stat * 
in_statsp,
     }
   else
     {
-    inhandle = open( name.c_str(), O_RDONLY );
+    inhandle = open( name.c_str(), O_RDONLY | O_BINARY);
     if( inhandle < 0 )
       {
       if( verbosity >= 0 )
@@ -298,9 +298,9 @@ 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,
+    outhandle = open( output_filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY | 
O_BINARY,
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
-  else outhandle = open( output_filename.c_str(), O_CREAT | O_EXCL | O_WRONLY,
+  else outhandle = open( output_filename.c_str(), O_CREAT | O_EXCL | O_WRONLY| 
O_BINARY,
                          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
   if( outhandle < 0 )
     {
@@ -446,7 +446,7 @@ int compress( const long long member_size, const long long 
volume_size,
         std::fprintf( stderr, "no data compressed.\n" );
       else
         std::fprintf( stderr, "%6.3f:1, %6.3f bits/byte, "
-                              "%5.2f%% saved, %lld in, %lld out.\n",
+                              "%5.2f%% saved, %"LZIPLL"d in, %"LZIPLL"d 
out.\n",
                       (double)in_size / out_size,
                       ( 8.0 * out_size ) / in_size,
                       100.0 * ( 1.0 - ( (double)out_size / in_size ) ),
@@ -518,10 +518,10 @@ int decompress( const int inhandle, const Pretty_print & 
pp,
           {
           pp();
           if( result == 2 )
-            std::fprintf( stderr, "file ends unexpectedly at pos %lld\n",
+            std::fprintf( stderr, "file ends unexpectedly at pos %"LZIPLL"d\n",
                           partial_file_pos );
           else
-            std::fprintf( stderr, "decoder error at pos %lld\n",
+            std::fprintf( stderr, "decoder error at pos %"LZIPLL"d\n",
                           partial_file_pos );
           }
         return 2;
@@ -551,7 +551,9 @@ extern "C" void signal_handler( int ) throw()
 void set_signals() throw()
   {
   signal( SIGTERM, signal_handler );
+  #ifndef _WIN32 /* Windows doesn't have SIGHUP */
   signal( SIGHUP, signal_handler );
+  #endif
   signal( SIGINT, signal_handler );
   }
 
@@ -601,7 +603,6 @@ void internal_error( const char * msg )
   std::exit( 3 );
   }
 
-
 // Returns the number of bytes really read.
 // If (returned value < size) and (errno == 0), means EOF was reached.
 //
@@ -609,6 +610,10 @@ int readblock( const int fd, char * buf, const int size ) 
throw()
   {
   int rest = size;
   errno = 0;
+  #ifdef _WIN32
+  /*set IO to binary mode */
+  int prevmode = _setmode(  fd, _O_BINARY);
+  #endif
   while( rest > 0 )
     {
     errno = 0;
@@ -617,6 +622,10 @@ int readblock( const int fd, char * buf, const int size ) 
throw()
     else if( n == 0 ) break;
     else if( errno != EINTR && errno != EAGAIN ) break;
     }
+  #ifdef _WIN32
+  /*Restore IO to default mode */
+  _setmode(  fd, prevmode);
+  #endif
   return ( rest > 0 ) ? size - rest : size;
   }
 
@@ -628,6 +637,10 @@ int writeblock( const int fd, const char * buf, const int 
size ) throw()
   {
   int rest = size;
   errno = 0;
+  #ifdef _WIN32
+  /*set IO to binary mode */
+  int prevmode = _setmode(  fd, _O_BINARY);
+  #endif
   while( rest > 0 )
     {
     errno = 0;
@@ -635,6 +648,10 @@ int writeblock( const int fd, const char * buf, const int 
size ) throw()
     if( n > 0 ) rest -= n;
     else if( errno && errno != EINTR && errno != EAGAIN ) break;
     }
+  #ifdef _WIN32
+  /*Restore IO to default mode */
+  _setmode(  fd, prevmode);
+  #endif
   return ( rest > 0 ) ? size - rest : size;
   }
 
_______________________________________________
Lzip-bug mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/lzip-bug

Reply via email to