diff options
Diffstat (limited to '')
-rw-r--r-- | lzip.h | 146 |
1 files changed, 0 insertions, 146 deletions
@@ -1,146 +0,0 @@ -/* Tarlz - Archiver with multimember lzip compression - Copyright (C) 2013-2019 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/>. -*/ - -#ifndef LZ_API_VERSION -#define LZ_API_VERSION 1 -#endif - -enum { - min_dictionary_bits = 12, - min_dictionary_size = 1 << min_dictionary_bits, - max_dictionary_bits = 29, - max_dictionary_size = 1 << max_dictionary_bits, - min_member_size = 36 }; - - -class CRC32 - { - uint32_t data[256]; // Table of CRCs of all 8-bit messages. - -public: - CRC32() - { - for( unsigned n = 0; n < 256; ++n ) - { - unsigned c = n; - for( int k = 0; k < 8; ++k ) - { if( c & 1 ) c = 0xEDB88320U ^ ( c >> 1 ); else c >>= 1; } - data[n] = c; - } - } - - void update_byte( uint32_t & crc, const uint8_t byte ) const - { crc = data[(crc^byte)&0xFF] ^ ( crc >> 8 ); } - }; - - -inline bool isvalid_ds( const unsigned dictionary_size ) - { return ( dictionary_size >= min_dictionary_size && - dictionary_size <= max_dictionary_size ); } - - -const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; // "LZIP" - -struct Lzip_header - { - uint8_t data[6]; // 0-3 magic bytes - // 4 version - // 5 coded_dict_size - enum { size = 6 }; - - bool verify_magic() const - { return ( std::memcmp( data, lzip_magic, 4 ) == 0 ); } - - bool verify_prefix( const int sz ) const // detect (truncated) header - { - for( int i = 0; i < sz && i < 4; ++i ) - if( data[i] != lzip_magic[i] ) return false; - return ( sz > 0 ); - } - bool verify_corrupt() const // detect corrupt header - { - int matches = 0; - for( int i = 0; i < 4; ++i ) - if( data[i] == lzip_magic[i] ) ++matches; - return ( matches > 1 && matches < 4 ); - } - - uint8_t version() const { return data[4]; } - bool verify_version() const { return ( data[4] == 1 ); } - - unsigned dictionary_size() const - { - unsigned sz = ( 1 << ( data[5] & 0x1F ) ); - if( sz > min_dictionary_size ) - sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 7 ); - return sz; - } - }; - - -struct Lzip_trailer - { - uint8_t data[20]; // 0-3 CRC32 of the uncompressed data - // 4-11 size of the uncompressed data - // 12-19 member size including header and trailer - enum { size = 20 }; - - unsigned data_crc() const - { - unsigned tmp = 0; - for( int i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; } - return tmp; - } - - unsigned long long data_size() const - { - unsigned long long tmp = 0; - for( int i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; } - return tmp; - } - - unsigned long long member_size() const - { - unsigned long long tmp = 0; - for( int i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; } - return tmp; - } - - bool verify_consistency() const // check internal consistency - { - const unsigned crc = data_crc(); - const unsigned long long dsize = data_size(); - if( ( crc == 0 ) != ( dsize == 0 ) ) return false; - const unsigned long long msize = member_size(); - if( msize < min_member_size ) return false; - const unsigned long long mlimit = ( 9 * dsize + 7 ) / 8 + min_member_size; - if( mlimit > dsize && msize > mlimit ) return false; - const unsigned long long dlimit = 7090 * ( msize - 26 ) - 1; - if( dlimit > msize && dsize > dlimit ) return false; - return true; - } - }; - - -const char * const bad_magic_msg = "Bad magic number (file not in lzip format)."; -const char * const bad_dict_msg = "Invalid dictionary size in member header."; -const char * const corrupt_mm_msg = "Corrupt header in multimember file."; -const char * const trailing_msg = "Trailing data not allowed."; - -// defined in extract.cc -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 ); |