1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/* Lzip - LZMA lossless data compressor
Copyright (C) 2008-2016 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
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <stdint.h>
#include "lzip.h"
#include "encoder_base.h"
Dis_slots dis_slots;
Prob_prices prob_prices;
bool Matchfinder_base::read_block()
{
if( !at_stream_end && stream_pos < buffer_size )
{
const int size = buffer_size - stream_pos;
const int rd = readblock( infd, buffer + stream_pos, size );
stream_pos += rd;
if( rd != size && errno ) throw Error( "Read error" );
if( rd < size ) { at_stream_end = true; pos_limit = buffer_size; }
}
return pos < stream_pos;
}
void Matchfinder_base::normalize_pos()
{
if( pos > stream_pos )
internal_error( "pos > stream_pos in Matchfinder_base::normalize_pos." );
if( !at_stream_end )
{
const int offset = pos - dictionary_size - before_size;
const int size = stream_pos - offset;
std::memmove( buffer, buffer + offset, size );
partial_data_pos += offset;
pos -= offset;
stream_pos -= offset;
for( int i = 0; i < num_prev_positions; ++i )
prev_positions[i] -= std::min( prev_positions[i], offset );
for( int i = 0; i < pos_array_size; ++i )
pos_array[i] -= std::min( pos_array[i], offset );
read_block();
}
}
Matchfinder_base::Matchfinder_base( const int before, const int dict_size,
const int after_size, const int dict_factor,
const int num_prev_positions23,
const int pos_array_factor, const int ifd )
:
partial_data_pos( 0 ),
before_size( before ),
pos( 0 ),
cyclic_pos( 0 ),
stream_pos( 0 ),
infd( ifd ),
at_stream_end( false )
{
const int buffer_size_limit =
( dict_factor * dict_size ) + before_size + after_size;
buffer_size = std::max( 65536, dict_size );
buffer = (uint8_t *)std::malloc( buffer_size );
if( !buffer ) throw std::bad_alloc();
if( read_block() && !at_stream_end && buffer_size < buffer_size_limit )
{
buffer_size = buffer_size_limit;
uint8_t * const tmp = (uint8_t *)std::realloc( buffer, buffer_size );
if( !tmp ) { std::free( buffer ); throw std::bad_alloc(); }
buffer = tmp;
read_block();
}
if( at_stream_end && stream_pos < dict_size )
dictionary_size = std::max( (int)min_dictionary_size, stream_pos );
else
dictionary_size = dict_size;
pos_limit = buffer_size;
if( !at_stream_end ) pos_limit -= after_size;
unsigned size = 1 << std::max( 16, real_bits( dictionary_size - 1 ) - 2 );
if( dictionary_size > 1 << 26 ) // 64 MiB
size >>= 1;
key4_mask = size - 1;
size += num_prev_positions23;
num_prev_positions = size;
pos_array_size = pos_array_factor * ( dictionary_size + 1 );
size += pos_array_size;
prev_positions = new( std::nothrow ) int32_t[size];
if( !prev_positions ) { std::free( buffer ); throw std::bad_alloc(); }
pos_array = prev_positions + num_prev_positions;
for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = 0;
}
void Matchfinder_base::reset()
{
if( stream_pos > pos )
std::memmove( buffer, buffer + pos, stream_pos - pos );
partial_data_pos = 0;
stream_pos -= pos;
pos = 0;
cyclic_pos = 0;
for( int i = 0; i < num_prev_positions; ++i ) prev_positions[i] = 0;
read_block();
}
void Range_encoder::flush_data()
{
if( pos > 0 )
{
if( outfd >= 0 && writeblock( outfd, buffer, pos ) != pos )
throw Error( "Write error" );
partial_member_pos += pos;
pos = 0;
show_progress();
}
}
// End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len)
void LZ_encoder_base::full_flush( const State state )
{
const int pos_state = data_position() & pos_state_mask;
renc.encode_bit( bm_match[state()][pos_state], 1 );
renc.encode_bit( bm_rep[state()], 0 );
encode_pair( 0xFFFFFFFFU, min_match_len, pos_state );
renc.flush();
File_trailer trailer;
trailer.data_crc( crc() );
trailer.data_size( data_position() );
trailer.member_size( renc.member_position() + File_trailer::size );
for( int i = 0; i < File_trailer::size; ++i )
renc.put_byte( trailer.data[i] );
renc.flush_data();
}
void LZ_encoder_base::reset()
{
Matchfinder_base::reset();
crc_ = 0xFFFFFFFFU;
bm_literal[0][0].reset( ( 1 << literal_context_bits ) * 0x300 );
bm_match[0][0].reset( State::states * pos_states );
bm_rep[0].reset( State::states );
bm_rep0[0].reset( State::states );
bm_rep1[0].reset( State::states );
bm_rep2[0].reset( State::states );
bm_len[0][0].reset( State::states * pos_states );
bm_dis_slot[0][0].reset( len_states * (1 << dis_slot_bits ) );
bm_dis[0].reset( modeled_distances - end_dis_model );
bm_align[0].reset( dis_align_size );
match_len_model.reset();
rep_len_model.reset();
renc.reset();
}
|