diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-06 11:33:36 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2015-11-06 11:33:36 +0000 |
commit | 8b42f93e9b42a93caddaa897a237acb5acbf3231 (patch) | |
tree | 0385d43238b978b7be02fb442fb64fadc92ad7dc /decoder.h | |
parent | Adding upstream version 1.2. (diff) | |
download | clzip-8b42f93e9b42a93caddaa897a237acb5acbf3231.tar.xz clzip-8b42f93e9b42a93caddaa897a237acb5acbf3231.zip |
Adding upstream version 1.3.upstream/1.3
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'decoder.h')
-rw-r--r-- | decoder.h | 83 |
1 files changed, 44 insertions, 39 deletions
@@ -1,5 +1,5 @@ /* Clzip - Data compressor based on the LZMA algorithm - 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 @@ -31,33 +31,27 @@ struct Range_decoder bool Rd_read_block( struct Range_decoder * const rdec ); -static inline void Rd_init( struct Range_decoder * const rdec, const int ifd ) +static inline bool Rd_init( struct Range_decoder * const rdec, const int ifd ) { rdec->partial_member_pos = 0; rdec->buffer = (uint8_t *)malloc( rd_buffer_size ); - if( !rdec->buffer ) - { - show_error( "Not enough memory. Find a machine with more memory.", 0, false ); - cleanup_and_fail( 1 ); - } + if( !rdec->buffer ) return false; rdec->pos = 0; rdec->stream_pos = 0; rdec->code = 0; rdec->range = 0xFFFFFFFFU; rdec->infd = ifd; rdec->at_stream_end = false; + return true; } static inline void Rd_free( struct Range_decoder * const rdec ) - { free( rdec->buffer ); rdec->buffer = 0; } - -static inline bool Rd_code_is_zero( struct Range_decoder * const rdec ) - { return ( rdec->code == 0 ); } + { free( rdec->buffer ); } static inline bool Rd_finished( struct Range_decoder * const rdec ) { return rdec->pos >= rdec->stream_pos && !Rd_read_block( rdec ); } -static inline long long Rd_member_position( struct Range_decoder * const rdec ) +static inline long long Rd_member_position( const struct Range_decoder * const rdec ) { return rdec->partial_member_pos + rdec->pos; } static inline void Rd_reset_member_position( struct Range_decoder * const rdec ) @@ -69,6 +63,20 @@ static inline uint8_t Rd_get_byte( struct Range_decoder * const rdec ) return rdec->buffer[rdec->pos++]; } +static inline int Rd_read_data( struct Range_decoder * const rdec, + uint8_t * const outbuf, const int size ) + { + int rest = size; + while( rest > 0 && !Rd_finished( rdec ) ) + { + const int rd = min( rest, rdec->stream_pos - rdec->pos ); + memcpy( outbuf + size - rest, rdec->buffer + rdec->pos, rd ); + rdec->pos += rd; + rest -= rd; + } + return ( rest > 0 ) ? size - rest : size; + } + static inline void Rd_load( struct Range_decoder * const rdec ) { int i; @@ -223,27 +231,27 @@ struct Literal_decoder Bit_model bm_literal[1<<literal_context_bits][0x300]; }; -static inline void Lid_init( struct Literal_decoder * const literal_decoder ) +static inline void Lid_init( struct Literal_decoder * const lidec ) { int i, j; for( i = 0; i < 1<<literal_context_bits; ++i ) for( j = 0; j < 0x300; ++j ) - Bm_init( &literal_decoder->bm_literal[i][j] ); + Bm_init( &lidec->bm_literal[i][j] ); } -static inline int Lid_state( const int prev_byte ) +static inline int Lid_state( const uint8_t prev_byte ) { return ( prev_byte >> ( 8 - literal_context_bits ) ); } -static inline uint8_t Lid_decode( struct Literal_decoder * const literal_decoder, +static inline uint8_t Lid_decode( struct Literal_decoder * const lidec, struct Range_decoder * const rdec, const uint8_t prev_byte ) - { return Rd_decode_tree( rdec, literal_decoder->bm_literal[Lid_state(prev_byte)], 8 ); } + { return Rd_decode_tree( rdec, lidec->bm_literal[Lid_state(prev_byte)], 8 ); } -static inline uint8_t Lid_decode_matched( struct Literal_decoder * const literal_decoder, +static inline uint8_t Lid_decode_matched( struct Literal_decoder * const lidec, struct Range_decoder * const rdec, const uint8_t prev_byte, const uint8_t match_byte ) - { return Rd_decode_matched( rdec, literal_decoder->bm_literal[Lid_state(prev_byte)], match_byte ); } + { return Rd_decode_matched( rdec, lidec->bm_literal[Lid_state(prev_byte)], match_byte ); } struct LZ_decoder @@ -254,7 +262,7 @@ struct LZ_decoder uint8_t * buffer; /* output buffer */ int pos; /* current pos in buffer */ int stream_pos; /* first byte not yet written to file */ - uint32_t crc_; + uint32_t crc; int outfd; /* output file descriptor */ int member_version; @@ -279,14 +287,14 @@ void LZd_flush_data( struct LZ_decoder * const decoder ); bool LZd_verify_trailer( struct LZ_decoder * const decoder, struct Pretty_print * const pp ); -static inline uint8_t LZd_get_prev_byte( struct LZ_decoder * const decoder ) - { - const int i = - ( ( decoder->pos > 0 ) ? decoder->pos : decoder->buffer_size ) - 1; - return decoder->buffer[i]; - } +static inline uint8_t LZd_get_prev_byte( const struct LZ_decoder * const decoder ) + { + const int i = + ( ( decoder->pos > 0 ) ? decoder->pos : decoder->buffer_size ) - 1; + return decoder->buffer[i]; + } -static inline uint8_t LZd_get_byte( struct LZ_decoder * const decoder, +static inline uint8_t LZd_get_byte( const struct LZ_decoder * const decoder, const int distance ) { int i = decoder->pos - distance - 1; @@ -312,7 +320,7 @@ static inline void LZd_copy_block( struct LZ_decoder * const decoder, memcpy( decoder->buffer + decoder->pos, decoder->buffer + i, len ); decoder->pos += len; } - else for( ; len > 0 ; --len ) + else for( ; len > 0; --len ) { decoder->buffer[decoder->pos] = decoder->buffer[i]; if( ++decoder->pos >= decoder->buffer_size ) LZd_flush_data( decoder ); @@ -320,7 +328,7 @@ static inline void LZd_copy_block( struct LZ_decoder * const decoder, } } -static inline void LZd_init( struct LZ_decoder * const decoder, +static inline bool LZd_init( struct LZ_decoder * const decoder, const File_header header, struct Range_decoder * const rdec, const int ofd ) { @@ -329,14 +337,10 @@ static inline void LZd_init( struct LZ_decoder * const decoder, decoder->dictionary_size = Fh_get_dictionary_size( header ); decoder->buffer_size = max( 65536, decoder->dictionary_size ); decoder->buffer = (uint8_t *)malloc( decoder->buffer_size ); - if( !decoder->buffer ) - { - show_error( "Not enough memory. Find a machine with more memory.", 0, false ); - cleanup_and_fail( 1 ); - } + if( !decoder->buffer ) return false; decoder->pos = 0; decoder->stream_pos = 0; - decoder->crc_ = 0xFFFFFFFFU; + decoder->crc = 0xFFFFFFFFU; decoder->outfd = ofd; decoder->member_version = Fh_version( header ); @@ -365,15 +369,16 @@ static inline void LZd_init( struct LZ_decoder * const decoder, Led_init( &decoder->rep_match_len_decoder ); Lid_init( &decoder->literal_decoder ); decoder->buffer[decoder->buffer_size-1] = 0; /* prev_byte of first_byte */ + return true; } static inline void LZd_free( struct LZ_decoder * const decoder ) - { free( decoder->buffer ); decoder->buffer = 0; } + { free( decoder->buffer ); } -static inline uint32_t LZd_crc( struct LZ_decoder * const decoder ) - { return decoder->crc_ ^ 0xFFFFFFFFU; } +static inline uint32_t LZd_crc( const struct LZ_decoder * const decoder ) + { return decoder->crc ^ 0xFFFFFFFFU; } -static inline long long LZd_data_position( struct LZ_decoder * const decoder ) +static inline long long LZd_data_position( const struct LZ_decoder * const decoder ) { return decoder->partial_data_pos + decoder->pos; } int LZd_decode_member( struct LZ_decoder * const decoder, |