summaryrefslogtreecommitdiffstats
path: root/decoder.c
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-06 13:15:12 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-06 13:15:12 +0000
commitaf0ac2217d1005ad32420a31414182b14ed54b4f (patch)
tree6387958c45b0614c2d02c0cfdecc1222771403aa /decoder.c
parentAdding upstream version 1.0. (diff)
downloadlunzip-af0ac2217d1005ad32420a31414182b14ed54b4f.tar.xz
lunzip-af0ac2217d1005ad32420a31414182b14ed54b4f.zip
Adding upstream version 1.1.upstream/1.1
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r--decoder.c82
1 files changed, 59 insertions, 23 deletions
diff --git a/decoder.c b/decoder.c
index e59b2ad..d09a143 100644
--- a/decoder.c
+++ b/decoder.c
@@ -1,5 +1,5 @@
/* Lunzip - Decompressor for lzip files
- Copyright (C) 2010, 2011 Antonio Diaz Diaz.
+ Copyright (C) 2010, 2011, 2012 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
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "lunzip.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.
+*/
+static 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.
+*/
+static 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( n < 0 && 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 )
@@ -50,10 +91,10 @@ void LZd_flush_data( struct LZ_decoder * const decoder )
const int size = decoder->pos - decoder->stream_pos;
if( size > 0 )
{
- CRC32_update_buf( &decoder->crc_, decoder->buffer + decoder->stream_pos, size );
+ 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;
@@ -64,31 +105,26 @@ void LZd_flush_data( struct LZ_decoder * const decoder )
bool LZd_verify_trailer( struct LZ_decoder * const decoder,
struct Pretty_print * const pp )
{
- int i;
File_trailer trailer;
const int trailer_size = Ft_versioned_size( decoder->member_version );
const long long member_size =
Rd_member_position( decoder->range_decoder ) + trailer_size;
bool error = false;
- for( i = 0; i < trailer_size && !error; ++i )
+ int size = Rd_read_data( decoder->range_decoder, trailer, trailer_size );
+ if( size < trailer_size )
{
- if( !Rd_finished( decoder->range_decoder ) )
- trailer[i] = Rd_get_byte( decoder->range_decoder );
- else
+ error = true;
+ if( pp->verbosity >= 0 )
{
- error = true;
- if( pp->verbosity >= 0 )
- {
- Pp_show_msg( pp, 0 );
- fprintf( stderr, "Trailer truncated at trailer position %d;"
- " some checks may fail.\n", i );
- }
- for( ; i < trailer_size; ++i ) trailer[i] = 0;
+ Pp_show_msg( pp, 0 );
+ fprintf( stderr, "Trailer truncated at trailer position %d;"
+ " some checks may fail.\n", size );
}
+ while( size < trailer_size ) trailer[size++] = 0;
}
if( decoder->member_version == 0 ) Ft_set_member_size( trailer, member_size );
- if( !Rd_code_is_zero( decoder->range_decoder ) )
+ if( decoder->range_decoder->code != 0 )
{
error = true;
Pp_show_msg( pp, "Range decoder final code is not zero" );
@@ -124,12 +160,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 ) );
@@ -149,11 +185,9 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
State state = 0;
Rd_load( decoder->range_decoder );
- while( true )
+ while( !Rd_finished( decoder->range_decoder ) )
{
const int pos_state = LZd_data_position( decoder ) & pos_state_mask;
- if( Rd_finished( decoder->range_decoder ) )
- { LZd_flush_data( decoder ); return 2; }
if( Rd_decode_bit( decoder->range_decoder, &decoder->bm_match[state][pos_state] ) == 0 )
{
const uint8_t prev_byte = LZd_get_prev_byte( decoder );
@@ -230,7 +264,7 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
if( pp->verbosity >= 0 )
{
Pp_show_msg( pp, 0 );
- fprintf( stderr, "Unsupported marker code `%d'.\n", len );
+ fprintf( stderr, "Unsupported marker code '%d'.\n", len );
}
return 4;
}
@@ -245,4 +279,6 @@ int LZd_decode_member( struct LZ_decoder * const decoder,
LZd_copy_block( decoder, rep0, len );
}
}
+ LZd_flush_data( decoder );
+ return 2;
}