2025-02-23 19:26:17 +01:00
|
|
|
/* Pdlzip - LZMA lossless data compressor
|
2025-02-23 19:27:49 +01:00
|
|
|
Copyright (C) 2010-2023 Antonio Diaz Diaz.
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:26:17 +01:00
|
|
|
This program is free software. Redistribution and use in source and
|
|
|
|
binary forms, with or without modification, are permitted provided
|
|
|
|
that the following conditions are met:
|
2025-02-23 19:20:27 +01:00
|
|
|
|
2025-02-23 19:26:17 +01:00
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions, and the following disclaimer.
|
2025-02-23 19:20:27 +01:00
|
|
|
|
2025-02-23 19:26:17 +01:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions, and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:26:17 +01:00
|
|
|
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.
|
2025-02-23 19:07:50 +01:00
|
|
|
*/
|
|
|
|
|
2025-02-23 19:12:39 +01:00
|
|
|
#ifndef max
|
|
|
|
#define max(x,y) ((x) >= (y) ? (x) : (y))
|
|
|
|
#endif
|
2025-02-23 19:23:58 +01:00
|
|
|
|
2025-02-23 19:16:24 +01:00
|
|
|
typedef int State;
|
|
|
|
|
2025-02-23 19:07:50 +01:00
|
|
|
enum {
|
|
|
|
min_dictionary_bits = 12,
|
2025-02-23 19:26:17 +01:00
|
|
|
min_dictionary_size = 1 << min_dictionary_bits, /* >= modeled_distances */
|
2025-02-23 19:23:00 +01:00
|
|
|
max_dictionary_bits = 29,
|
2025-02-23 19:07:50 +01:00
|
|
|
max_dictionary_size = 1 << max_dictionary_bits,
|
2025-02-23 19:23:00 +01:00
|
|
|
max_dictionary_bits_c = 27, /* kDicLogSizeMaxCompress */
|
|
|
|
max_dictionary_size_c = 1 << max_dictionary_bits_c,
|
2025-02-23 19:07:50 +01:00
|
|
|
literal_context_bits = 3,
|
2025-02-23 19:21:47 +01:00
|
|
|
literal_pos_state_bits = 0, /* not used */
|
2025-02-23 19:07:50 +01:00
|
|
|
pos_state_bits = 2,
|
|
|
|
|
|
|
|
len_low_bits = 3,
|
|
|
|
len_mid_bits = 3,
|
|
|
|
len_high_bits = 8,
|
|
|
|
len_low_symbols = 1 << len_low_bits,
|
|
|
|
len_mid_symbols = 1 << len_mid_bits,
|
|
|
|
len_high_symbols = 1 << len_high_bits,
|
|
|
|
max_len_symbols = len_low_symbols + len_mid_symbols + len_high_symbols,
|
|
|
|
|
2025-02-23 19:12:39 +01:00
|
|
|
min_match_len = 2, /* must be 2 */
|
|
|
|
max_match_len = min_match_len + max_len_symbols - 1, /* 273 */
|
2025-02-23 19:07:50 +01:00
|
|
|
min_match_len_limit = 5 };
|
|
|
|
|
2025-02-23 19:16:24 +01:00
|
|
|
|
2025-02-23 19:12:39 +01:00
|
|
|
typedef uint32_t CRC32[256]; /* Table of CRCs of all 8-bit messages. */
|
2025-02-23 19:07:50 +01:00
|
|
|
|
|
|
|
extern CRC32 crc32;
|
|
|
|
|
2025-02-23 19:16:24 +01:00
|
|
|
static inline void CRC32_init( void )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:16:24 +01:00
|
|
|
unsigned n;
|
2025-02-23 19:12:39 +01:00
|
|
|
for( n = 0; n < 256; ++n )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:16:24 +01:00
|
|
|
unsigned c = n;
|
2025-02-23 19:12:39 +01:00
|
|
|
int k;
|
|
|
|
for( k = 0; k < 8; ++k )
|
2025-02-23 19:10:43 +01:00
|
|
|
{ if( c & 1 ) c = 0xEDB88320U ^ ( c >> 1 ); else c >>= 1; }
|
2025-02-23 19:07:50 +01:00
|
|
|
crc32[n] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-23 19:26:50 +01:00
|
|
|
/* about as fast as it is possible without messing with endianness */
|
2025-02-23 19:16:24 +01:00
|
|
|
static inline void CRC32_update_buf( uint32_t * const crc,
|
2025-02-23 19:20:27 +01:00
|
|
|
const uint8_t * const buffer,
|
|
|
|
const int size )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:12:39 +01:00
|
|
|
int i;
|
2025-02-23 19:23:00 +01:00
|
|
|
uint32_t c = *crc;
|
2025-02-23 19:12:39 +01:00
|
|
|
for( i = 0; i < size; ++i )
|
2025-02-23 19:23:00 +01:00
|
|
|
c = crc32[(c^buffer[i])&0xFF] ^ ( c >> 8 );
|
|
|
|
*crc = c;
|
2025-02-23 19:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-23 19:21:47 +01:00
|
|
|
static inline bool isvalid_ds( const unsigned dictionary_size )
|
2025-02-23 19:27:49 +01:00
|
|
|
{ return dictionary_size >= min_dictionary_size &&
|
|
|
|
dictionary_size <= max_dictionary_size; }
|
2025-02-23 19:21:47 +01:00
|
|
|
|
|
|
|
|
2025-02-23 19:16:24 +01:00
|
|
|
static inline int real_bits( unsigned value )
|
2025-02-23 19:14:00 +01:00
|
|
|
{
|
2025-02-23 19:16:24 +01:00
|
|
|
int bits = 0;
|
|
|
|
while( value > 0 ) { value >>= 1; ++bits; }
|
2025-02-23 19:14:00 +01:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
|
2025-02-23 19:12:39 +01:00
|
|
|
|
2025-02-23 19:27:49 +01:00
|
|
|
enum { Lh_size = 6 };
|
|
|
|
typedef uint8_t Lzip_header[Lh_size]; /* 0-3 magic bytes */
|
2025-02-23 19:12:39 +01:00
|
|
|
/* 4 version */
|
2025-02-23 19:26:17 +01:00
|
|
|
/* 5 coded dictionary size */
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline void Lh_set_magic( Lzip_header data )
|
|
|
|
{ memcpy( data, lzip_magic, 4 ); data[4] = 1; }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:27:49 +01:00
|
|
|
static inline bool Lh_check_magic( const Lzip_header data )
|
|
|
|
{ return memcmp( data, lzip_magic, 4 ) == 0; }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:23:58 +01:00
|
|
|
/* detect (truncated) header */
|
2025-02-23 19:27:49 +01:00
|
|
|
static inline bool Lh_check_prefix( const Lzip_header data, const int sz )
|
2025-02-23 19:21:47 +01:00
|
|
|
{
|
2025-02-23 19:23:58 +01:00
|
|
|
int i; for( i = 0; i < sz && i < 4; ++i )
|
2025-02-23 19:24:41 +01:00
|
|
|
if( data[i] != lzip_magic[i] ) return false;
|
2025-02-23 19:27:49 +01:00
|
|
|
return sz > 0;
|
2025-02-23 19:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* detect corrupt header */
|
2025-02-23 19:27:49 +01:00
|
|
|
static inline bool Lh_check_corrupt( const Lzip_header data )
|
2025-02-23 19:23:58 +01:00
|
|
|
{
|
|
|
|
int matches = 0;
|
|
|
|
int i; for( i = 0; i < 4; ++i )
|
2025-02-23 19:24:41 +01:00
|
|
|
if( data[i] == lzip_magic[i] ) ++matches;
|
2025-02-23 19:27:49 +01:00
|
|
|
return matches > 1 && matches < 4;
|
2025-02-23 19:21:47 +01:00
|
|
|
}
|
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline uint8_t Lh_version( const Lzip_header data )
|
2025-02-23 19:14:00 +01:00
|
|
|
{ return data[4]; }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:27:49 +01:00
|
|
|
static inline bool Lh_check_version( const Lzip_header data )
|
|
|
|
{ return data[4] == 1; }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline unsigned Lh_get_dictionary_size( const Lzip_header data )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:27:49 +01:00
|
|
|
unsigned sz = 1 << ( data[5] & 0x1F );
|
2025-02-23 19:16:24 +01:00
|
|
|
if( sz > min_dictionary_size )
|
|
|
|
sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 7 );
|
2025-02-23 19:10:43 +01:00
|
|
|
return sz;
|
2025-02-23 19:07:50 +01:00
|
|
|
}
|
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline bool Lh_set_dictionary_size( Lzip_header data, const unsigned sz )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:21:47 +01:00
|
|
|
if( !isvalid_ds( sz ) ) return false;
|
|
|
|
data[5] = real_bits( sz - 1 );
|
|
|
|
if( sz > min_dictionary_size )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:21:47 +01:00
|
|
|
const unsigned base_size = 1 << data[5];
|
|
|
|
const unsigned fraction = base_size / 16;
|
2025-02-23 19:23:00 +01:00
|
|
|
unsigned i;
|
2025-02-23 19:21:47 +01:00
|
|
|
for( i = 7; i >= 1; --i )
|
|
|
|
if( base_size - ( i * fraction ) >= sz )
|
2025-02-23 19:27:49 +01:00
|
|
|
{ data[5] |= i << 5; break; }
|
2025-02-23 19:07:50 +01:00
|
|
|
}
|
2025-02-23 19:21:47 +01:00
|
|
|
return true;
|
2025-02-23 19:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-23 19:27:49 +01:00
|
|
|
enum { Lt_size = 20 };
|
|
|
|
typedef uint8_t Lzip_trailer[Lt_size];
|
2025-02-23 19:12:39 +01:00
|
|
|
/* 0-3 CRC32 of the uncompressed data */
|
|
|
|
/* 4-11 size of the uncompressed data */
|
|
|
|
/* 12-19 member size including header and trailer */
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline unsigned Lt_get_data_crc( const Lzip_trailer data )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:16:24 +01:00
|
|
|
unsigned tmp = 0;
|
2025-02-23 19:20:27 +01:00
|
|
|
int i; for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
|
2025-02-23 19:07:50 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline void Lt_set_data_crc( Lzip_trailer data, unsigned crc )
|
2025-02-23 19:20:27 +01:00
|
|
|
{ int i; for( i = 0; i <= 3; ++i ) { data[i] = (uint8_t)crc; crc >>= 8; } }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline unsigned long long Lt_get_data_size( const Lzip_trailer data )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:16:24 +01:00
|
|
|
unsigned long long tmp = 0;
|
2025-02-23 19:20:27 +01:00
|
|
|
int i; for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
|
2025-02-23 19:07:50 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline void Lt_set_data_size( Lzip_trailer data, unsigned long long sz )
|
2025-02-23 19:20:27 +01:00
|
|
|
{ int i; for( i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline unsigned long long Lt_get_member_size( const Lzip_trailer data )
|
2025-02-23 19:07:50 +01:00
|
|
|
{
|
2025-02-23 19:16:24 +01:00
|
|
|
unsigned long long tmp = 0;
|
2025-02-23 19:20:27 +01:00
|
|
|
int i; for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
|
2025-02-23 19:07:50 +01:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2025-02-23 19:24:41 +01:00
|
|
|
static inline void Lt_set_member_size( Lzip_trailer data, unsigned long long sz )
|
2025-02-23 19:20:27 +01:00
|
|
|
{ int i; for( i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
|
2025-02-23 19:07:50 +01:00
|
|
|
|
|
|
|
|
2025-02-23 19:26:17 +01:00
|
|
|
static inline void set_retval( int * retval, const int new_val )
|
|
|
|
{ if( *retval < new_val ) *retval = new_val; }
|
|
|
|
|
2025-02-23 19:23:00 +01:00
|
|
|
static const char * const trailing_msg = "Trailing data not allowed.";
|
2025-02-23 19:26:17 +01:00
|
|
|
static const char * const mem_msg = "Not enough memory.";
|
2025-02-23 19:23:00 +01:00
|
|
|
|
2025-02-23 19:20:27 +01:00
|
|
|
/* defined in main.c */
|
2025-02-23 19:26:17 +01:00
|
|
|
extern int verbosity;
|
2025-02-23 19:16:24 +01:00
|
|
|
int readblock( const int fd, uint8_t * const buf, const int size );
|
|
|
|
int writeblock( const int fd, const uint8_t * const buf, const int size );
|
|
|
|
|
|
|
|
#define SZ_OK 0
|
|
|
|
|
|
|
|
#define SZ_ERROR_READ 8
|
|
|
|
#define SZ_ERROR_WRITE 9
|