2
({ __typeof__ (a) _a = (a); \
3
__typeof__ (b) _b = (b); \
7
({ __typeof__ (a) _a = (a); \
8
__typeof__ (b) _b = (b); \
11
/* Lzip - Data compressor based on the LZMA algorithm
12
Copyright (C) 2008, 2009, 2010, 2011 Antonio Diaz Diaz.
14
This program is free software: you can redistribute it and/or modify
15
it under the terms of the GNU General Public License as published by
16
the Free Software Foundation, either version 3 of the License, or
17
(at your option) any later version.
19
This program is distributed in the hope that it will be useful,
20
but WITHOUT ANY WARRANTY; without even the implied warranty of
21
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
GNU General Public License for more details.
24
You should have received a copy of the GNU General Public License
25
along with this program. If not, see <http://www.gnu.org/licenses/>.
35
unsigned char operator()() const { return st; }
36
bool is_char() const { return st < 7; }
40
static const unsigned char next[states] =
41
{ 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
47
static const unsigned char next[states] =
48
{ 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10 };
54
static const unsigned char next[states] =
55
{ 8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11 };
61
static const unsigned char next[states] =
62
{ 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11 };
69
min_dictionary_bits = 12,
70
min_dictionary_size = 1 << min_dictionary_bits,
71
max_dictionary_bits = 29,
72
max_dictionary_size = 1 << max_dictionary_bits,
73
literal_context_bits = 3,
75
pos_states = 1 << pos_state_bits,
76
pos_state_mask = pos_states - 1,
81
modeled_distances = 1 << (end_dis_model / 2),
83
dis_align_size = 1 << dis_align_bits,
88
len_low_symbols = 1 << len_low_bits,
89
len_mid_symbols = 1 << len_mid_bits,
90
len_high_symbols = 1 << len_high_bits,
91
max_len_symbols = len_low_symbols + len_mid_symbols + len_high_symbols,
93
min_match_len = 2, // must be 2
94
max_match_len = min_match_len + max_len_symbols - 1, // 273
95
min_match_len_limit = 5,
99
inline int get_dis_state( int len )
101
len -= min_match_len;
102
if( len >= max_dis_states ) len = max_dis_states - 1;
107
enum { bit_model_move_bits = 5,
108
bit_model_total_bits = 11,
109
bit_model_total = 1 << bit_model_total_bits };
113
unsigned int probability;
114
Bit_model() : probability( bit_model_total / 2 ) {}
120
uint32_t data[256]; // Table of CRCs of all 8-bit messages.
125
for( unsigned int n = 0; n < 256; ++n )
128
for( int k = 0; k < 8; ++k )
129
{ if( c & 1 ) c = 0xEDB88320U ^ ( c >> 1 ); else c >>= 1; }
134
uint32_t operator[]( const uint8_t byte ) const { return data[byte]; }
135
void update( uint32_t & crc, const uint8_t byte ) const
136
{ crc = data[(crc^byte)&0xFF] ^ ( crc >> 8 ); }
137
void update( uint32_t & crc, const uint8_t * const buffer, const int size ) const
139
for( int i = 0; i < size; ++i )
140
crc = data[(crc^buffer[i])&0xFF] ^ ( crc >> 8 );
144
extern const CRC32 crc32;
147
inline int real_bits( const int value )
150
for( int i = 1, mask = 1; mask > 0; ++i, mask <<= 1 )
151
if( value & mask ) bits = i;
155
const uint8_t magic_string[4] = { 'L', 'Z', 'I', 'P' };
159
uint8_t data[6]; // 0-3 magic bytes
165
{ memcpy( data, magic_string, 4 ); data[4] = 1; }
167
bool verify_magic() const
168
{ return ( memcmp( data, magic_string, 4 ) == 0 ); }
170
uint8_t version() const { return data[4]; }
171
bool verify_version() const { return ( data[4] <= 1 ); }
173
int dictionary_size() const
175
int sz = ( 1 << ( data[5] & 0x1F ) );
176
if( sz > min_dictionary_size && sz <= max_dictionary_size )
177
sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 0x07 );
181
bool dictionary_size( const int sz )
183
if( sz >= min_dictionary_size && sz <= max_dictionary_size )
185
data[5] = real_bits( sz - 1 );
186
if( sz > min_dictionary_size )
188
const int base_size = 1 << data[5];
189
const int wedge = base_size / 16;
190
for( int i = 7; i >= 1; --i )
191
if( base_size - ( i * wedge ) >= sz )
192
{ data[5] |= ( i << 5 ); break; }
203
uint8_t data[20]; // 0-3 CRC32 of the uncompressed data
204
// 4-11 size of the uncompressed data
205
// 12-19 member size including header and trailer
207
static int size( const int version = 1 )
208
{ return ( ( version >= 1 ) ? 20 : 12 ); }
210
uint32_t data_crc() const
213
for( int i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
217
void data_crc( uint32_t crc )
218
{ for( int i = 0; i <= 3; ++i ) { data[i] = (uint8_t)crc; crc >>= 8; } }
220
long long data_size() const
223
for( int i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
227
void data_size( long long sz )
229
for( int i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; }
232
long long member_size() const
235
for( int i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
239
void member_size( long long sz )
241
for( int i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; }
248
const char * const msg;
249
Error( const char * const s ) : msg( s ) {}
253
// defined in main.cc lziprecover.cc
254
void show_error( const char * const msg, const int errcode = 0,
255
const bool help = false );
256
void internal_error( const char * const msg );
258
// defined in decoder.cc
259
int readblock( const int fd, uint8_t * const buf, const int size );
260
int writeblock( const int fd, const uint8_t * const buf, const int size );
263
extern void pp(const char *p=NULL);