summaryrefslogtreecommitdiffstats
path: root/fast_encoder.cc
diff options
context:
space:
mode:
Diffstat (limited to 'fast_encoder.cc')
-rw-r--r--fast_encoder.cc67
1 files changed, 33 insertions, 34 deletions
diff --git a/fast_encoder.cc b/fast_encoder.cc
index 9642e54..1ecd169 100644
--- a/fast_encoder.cc
+++ b/fast_encoder.cc
@@ -1,5 +1,5 @@
/* Lzip - LZMA lossless data compressor
- Copyright (C) 2008-2014 Antonio Diaz Diaz.
+ Copyright (C) 2008-2015 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
@@ -26,15 +26,16 @@
#include <stdint.h>
#include "lzip.h"
-#include "encoder.h"
+#include "encoder_base.h"
#include "fast_encoder.h"
-int Fmatchfinder::longest_match_len( int * const distance )
+int FLZ_encoder::longest_match_len( int * const distance )
{
+ enum { len_limit = 16 };
if( len_limit > available_bytes() ) return 0;
- const uint8_t * const data = buffer + pos;
+ const uint8_t * const data = ptr_to_current_pos();
key4 = ( ( key4 << 4 ) ^ data[3] ) & key4_mask;
const int pos1 = pos + 1;
@@ -48,10 +49,10 @@ int Fmatchfinder::longest_match_len( int * const distance )
{
if( --count < 0 || newpos <= 0 ) { *ptr0 = 0; break; }
const int delta = pos1 - newpos;
- if( delta > dictionary_size_ ) { *ptr0 = 0; break; }
+ if( delta > dictionary_size ) { *ptr0 = 0; break; }
int32_t * const newptr = pos_array +
( cyclic_pos - delta +
- ( ( cyclic_pos >= delta ) ? 0 : dictionary_size_ + 1 ) );
+ ( ( cyclic_pos >= delta ) ? 0 : dictionary_size + 1 ) );
if( data[maxlen-delta] == data[maxlen] )
{
@@ -86,37 +87,35 @@ bool FLZ_encoder::encode_member( const unsigned long long member_size )
State state;
for( int i = 0; i < num_rep_distances; ++i ) reps[i] = 0;
- if( fmatchfinder.data_position() != 0 ||
- renc.member_position() != File_header::size )
- return false; // can be called only once
+ if( data_position() != 0 || renc.member_position() != File_header::size )
+ return false; /* can be called only once */
- if( !fmatchfinder.finished() ) // encode first byte
+ if( !data_finished() ) // encode first byte
{
const uint8_t prev_byte = 0;
- const uint8_t cur_byte = fmatchfinder[0];
+ const uint8_t cur_byte = peek( 0 );
renc.encode_bit( bm_match[state()][0], 0 );
encode_literal( prev_byte, cur_byte );
crc32.update_byte( crc_, cur_byte );
- fmatchfinder.update_and_move( 1 );
+ reset_key4();
+ update_and_move( 1 );
}
- while( !fmatchfinder.finished() &&
- renc.member_position() < member_size_limit )
+ while( !data_finished() && renc.member_position() < member_size_limit )
{
int match_distance;
- const int main_len = fmatchfinder.longest_match_len( &match_distance );
- const int pos_state = fmatchfinder.data_position() & pos_state_mask;
+ const int main_len = longest_match_len( &match_distance );
+ const int pos_state = data_position() & pos_state_mask;
int len = 0;
for( int i = 0; i < num_rep_distances; ++i )
{
- const int tlen =
- fmatchfinder.true_match_len( 0, reps[i] + 1, max_match_len );
+ const int tlen = true_match_len( 0, reps[i] + 1, max_match_len );
if( tlen > len ) { len = tlen; rep = i; }
}
if( len > min_match_len && len + 3 > main_len )
{
- crc32.update_buf( crc_, fmatchfinder.ptr_to_current_pos(), len );
+ crc32.update_buf( crc_, ptr_to_current_pos(), len );
renc.encode_bit( bm_match[state()][pos_state], 1 );
renc.encode_bit( bm_rep[state()], 1 );
renc.encode_bit( bm_rep0[state()], rep != 0 );
@@ -133,42 +132,42 @@ bool FLZ_encoder::encode_member( const unsigned long long member_size )
}
state.set_rep();
renc.encode_len( rep_len_model, len, pos_state );
- fmatchfinder.move_pos();
- fmatchfinder.update_and_move( len - 1 );
+ move_pos();
+ update_and_move( len - 1 );
continue;
}
if( main_len > min_match_len )
{
- crc32.update_buf( crc_, fmatchfinder.ptr_to_current_pos(), main_len );
+ crc32.update_buf( crc_, ptr_to_current_pos(), main_len );
renc.encode_bit( bm_match[state()][pos_state], 1 );
renc.encode_bit( bm_rep[state()], 0 );
state.set_match();
for( int i = num_rep_distances - 1; i > 0; --i ) reps[i] = reps[i-1];
reps[0] = match_distance;
encode_pair( match_distance, main_len, pos_state );
- fmatchfinder.move_pos();
- fmatchfinder.update_and_move( main_len - 1 );
+ move_pos();
+ update_and_move( main_len - 1 );
continue;
}
- const uint8_t prev_byte = fmatchfinder[1];
- const uint8_t cur_byte = fmatchfinder[0];
- const uint8_t match_byte = fmatchfinder[reps[0]+1];
- fmatchfinder.move_pos();
+ const uint8_t prev_byte = peek( 1 );
+ const uint8_t cur_byte = peek( 0 );
+ const uint8_t match_byte = peek( reps[0] + 1 );
+ move_pos();
crc32.update_byte( crc_, cur_byte );
if( match_byte == cur_byte )
{
+ const int short_rep_price = price1( bm_match[state()][pos_state] ) +
+ price1( bm_rep[state()] ) +
+ price0( bm_rep0[state()] ) +
+ price0( bm_len[state()][pos_state] );
int price = price0( bm_match[state()][pos_state] );
if( state.is_char() )
price += price_literal( prev_byte, cur_byte );
else
price += price_matched( prev_byte, cur_byte, match_byte );
- const int short_rep_price = price1( bm_match[state()][pos_state] ) +
- price1( bm_rep[state()] ) +
- price0( bm_rep0[state()] ) +
- price0( bm_len[state()][pos_state] );
if( short_rep_price < price )
{
renc.encode_bit( bm_match[state()][pos_state], 1 );
@@ -180,7 +179,7 @@ bool FLZ_encoder::encode_member( const unsigned long long member_size )
}
}
- // literal byte
+ /* literal byte */
renc.encode_bit( bm_match[state()][pos_state], 0 );
if( state.is_char() )
encode_literal( prev_byte, cur_byte );
@@ -189,6 +188,6 @@ bool FLZ_encoder::encode_member( const unsigned long long member_size )
state.set_char();
}
- full_flush( fmatchfinder.data_position(), state );
+ full_flush( state );
return true;
}