summaryrefslogtreecommitdiffstats
path: root/lzip.h
diff options
context:
space:
mode:
Diffstat (limited to 'lzip.h')
-rw-r--r--lzip.h80
1 files changed, 38 insertions, 42 deletions
diff --git a/lzip.h b/lzip.h
index c0ef9e8..e0bdae9 100644
--- a/lzip.h
+++ b/lzip.h
@@ -1,20 +1,20 @@
-/* Lzlib - Compression library for the lzip format
- Copyright (C) 2009-2019 Antonio Diaz Diaz.
+/* Lzlib - Compression library for the lzip format
+ Copyright (C) 2009-2024 Antonio Diaz Diaz.
- This library is free software. Redistribution and use in source and
- binary forms, with or without modification, are permitted provided
- that the following conditions are met:
+ This library is free software. Redistribution and use in source and
+ binary forms, with or without modification, are permitted provided
+ that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
- 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.
+ 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.
- This library 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.
+ This library 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.
*/
#ifndef max
@@ -35,17 +35,13 @@ static inline State St_set_char( const State st )
static const State next[states] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
return next[st];
}
-
static inline State St_set_char_rep() { return 8; }
-
static inline State St_set_match( const State st )
- { return ( ( st < 7 ) ? 7 : 10 ); }
-
+ { return ( st < 7 ) ? 7 : 10; }
static inline State St_set_rep( const State st )
- { return ( ( st < 7 ) ? 8 : 11 ); }
-
+ { return ( st < 7 ) ? 8 : 11; }
static inline State St_set_short_rep( const State st )
- { return ( ( st < 7 ) ? 9 : 11 ); }
+ { return ( st < 7 ) ? 9 : 11; }
enum {
@@ -83,7 +79,7 @@ static inline int get_len_state( const int len )
{ return min( len - min_match_len, len_states - 1 ); }
static inline int get_lit_state( const uint8_t prev_byte )
- { return ( prev_byte >> ( 8 - literal_context_bits ) ); }
+ { return prev_byte >> ( 8 - literal_context_bits ); }
enum { bit_model_move_bits = 5,
@@ -168,6 +164,7 @@ static const uint32_t crc32[256] =
static inline void CRC32_update_byte( uint32_t * const crc, const uint8_t byte )
{ *crc = crc32[(*crc^byte)&0xFF] ^ ( *crc >> 8 ); }
+/* about as fast as it is possible without messing with endianness */
static inline void CRC32_update_buf( uint32_t * const crc,
const uint8_t * const buffer,
const int size )
@@ -181,8 +178,8 @@ static inline void CRC32_update_buf( uint32_t * const crc,
static inline bool isvalid_ds( const unsigned dictionary_size )
- { return ( dictionary_size >= min_dictionary_size &&
- dictionary_size <= max_dictionary_size ); }
+ { return dictionary_size >= min_dictionary_size &&
+ dictionary_size <= max_dictionary_size; }
static inline int real_bits( unsigned value )
@@ -195,43 +192,43 @@ static inline int real_bits( unsigned value )
static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
-typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */
- /* 4 version */
- /* 5 coded_dict_size */
enum { Lh_size = 6 };
+typedef uint8_t Lzip_header[Lh_size]; /* 0-3 magic bytes */
+ /* 4 version */
+ /* 5 coded dictionary size */
static inline void Lh_set_magic( Lzip_header data )
{ memcpy( data, lzip_magic, 4 ); data[4] = 1; }
-static inline bool Lh_verify_magic( const Lzip_header data )
- { return ( memcmp( data, lzip_magic, 4 ) == 0 ); }
+static inline bool Lh_check_magic( const Lzip_header data )
+ { return memcmp( data, lzip_magic, 4 ) == 0; }
/* detect (truncated) header */
-static inline bool Lh_verify_prefix( const Lzip_header data, const int sz )
+static inline bool Lh_check_prefix( const Lzip_header data, const int sz )
{
int i; for( i = 0; i < sz && i < 4; ++i )
if( data[i] != lzip_magic[i] ) return false;
- return ( sz > 0 );
+ return sz > 0;
}
/* detect corrupt header */
-static inline bool Lh_verify_corrupt( const Lzip_header data )
+static inline bool Lh_check_corrupt( const Lzip_header data )
{
int matches = 0;
int i; for( i = 0; i < 4; ++i )
if( data[i] == lzip_magic[i] ) ++matches;
- return ( matches > 1 && matches < 4 );
+ return matches > 1 && matches < 4;
}
static inline uint8_t Lh_version( const Lzip_header data )
{ return data[4]; }
-static inline bool Lh_verify_version( const Lzip_header data )
- { return ( data[4] == 1 ); }
+static inline bool Lh_check_version( const Lzip_header data )
+ { return data[4] == 1; }
static inline unsigned Lh_get_dictionary_size( const Lzip_header data )
{
- unsigned sz = ( 1 << ( data[5] & 0x1F ) );
+ unsigned sz = 1 << ( data[5] & 0x1F );
if( sz > min_dictionary_size )
sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 7 );
return sz;
@@ -248,24 +245,23 @@ static inline bool Lh_set_dictionary_size( Lzip_header data, const unsigned sz )
unsigned i;
for( i = 7; i >= 1; --i )
if( base_size - ( i * fraction ) >= sz )
- { data[5] |= ( i << 5 ); break; }
+ { data[5] |= i << 5; break; }
}
return true;
}
-static inline bool Lh_verify( const Lzip_header data )
+static inline bool Lh_check( const Lzip_header data )
{
- if( Lh_verify_magic( data ) && Lh_verify_version( data ) )
- return isvalid_ds( Lh_get_dictionary_size( data ) );
- return false;
+ return Lh_check_magic( data ) && Lh_check_version( data ) &&
+ isvalid_ds( Lh_get_dictionary_size( data ) );
}
-typedef uint8_t Lzip_trailer[20];
+enum { Lt_size = 20 };
+typedef uint8_t Lzip_trailer[Lt_size];
/* 0-3 CRC32 of the uncompressed data */
/* 4-11 size of the uncompressed data */
/* 12-19 member size including header and trailer */
-enum { Lt_size = 20 };
static inline unsigned Lt_get_data_crc( const Lzip_trailer data )
{