diff options
Diffstat (limited to '')
-rw-r--r-- | decoder.c | 47 |
1 files changed, 44 insertions, 3 deletions
@@ -23,6 +23,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "clzip.h" #include "decoder.h" @@ -30,6 +31,46 @@ CRC32 crc32; + +/* Returns the number of bytes really read. + If (returned value < size) and (errno == 0), means EOF was reached. +*/ +int readblock( const int fd, uint8_t * const buf, const int size ) + { + int rest = size; + while( true ) + { + int n; + errno = 0; + if( rest <= 0 ) break; + n = read( fd, buf + size - rest, rest ); + if( n > 0 ) rest -= n; + else if( n == 0 ) break; + else if( errno != EINTR && errno != EAGAIN ) break; + } + return ( rest > 0 ) ? size - rest : size; + } + + +/* Returns the number of bytes really written. + If (returned value < size), it is always an error. +*/ +int writeblock( const int fd, const uint8_t * const buf, const int size ) + { + int rest = size; + while( true ) + { + int n; + errno = 0; + if( rest <= 0 ) break; + n = write( fd, buf + size - rest, rest ); + if( n > 0 ) rest -= n; + else if( errno && errno != EINTR && errno != EAGAIN ) break; + } + return ( rest > 0 ) ? size - rest : size; + } + + bool Rd_read_block( struct Range_decoder * const rdec ) { if( !rdec->at_stream_end ) @@ -53,7 +94,7 @@ void LZd_flush_data( struct LZ_decoder * const decoder ) CRC32_update_buf( &decoder->crc_, decoder->buffer + decoder->stream_pos, size ); if( decoder->outfd >= 0 && writeblock( decoder->outfd, decoder->buffer + decoder->stream_pos, size ) != size ) - { show_error( "write error", errno, false ); cleanup_and_fail( 1 ); } + { show_error( "Write error", errno, false ); cleanup_and_fail( 1 ); } if( decoder->pos >= decoder->buffer_size ) { decoder->partial_data_pos += decoder->pos; decoder->pos = 0; } decoder->stream_pos = decoder->pos; @@ -124,12 +165,12 @@ bool LZd_verify_trailer( struct LZ_decoder * const decoder, Ft_get_member_size( trailer ), member_size, member_size ); } } - if( !error && pp->verbosity >= 4 && LZd_data_position( decoder ) > 0 && member_size > 0 ) + if( !error && pp->verbosity >= 3 && LZd_data_position( decoder ) > 0 && member_size > 0 ) fprintf( stderr, "%6.3f:1, %6.3f bits/byte, %5.2f%% saved. ", (double)LZd_data_position( decoder ) / member_size, ( 8.0 * member_size ) / LZd_data_position( decoder ), 100.0 * ( 1.0 - ( (double)member_size / LZd_data_position( decoder ) ) ) ); - if( !error && pp->verbosity >= 3 ) + if( !error && pp->verbosity >= 4 ) fprintf( stderr, "data CRC %08X, data size %9lld, member size %8lld. ", (unsigned int)Ft_get_data_crc( trailer ), Ft_get_data_size( trailer ), Ft_get_member_size( trailer ) ); |