summaryrefslogtreecommitdiffstats
path: root/pdlzip.h
diff options
context:
space:
mode:
Diffstat (limited to 'pdlzip.h')
-rw-r--r--pdlzip.h79
1 files changed, 40 insertions, 39 deletions
diff --git a/pdlzip.h b/pdlzip.h
index 077ea9b..a54ef76 100644
--- a/pdlzip.h
+++ b/pdlzip.h
@@ -1,5 +1,5 @@
-/* Pdlzip - A data compressor based on the LZMA algorithm
- Copyright (C) 2010 Antonio Diaz Diaz.
+/* Pdlzip - Data compressor based on the LZMA algorithm
+ Copyright (C) 2010, 2011 Antonio Diaz Diaz.
This program is free software: you have unlimited permission
to copy, distribute and modify it.
@@ -69,6 +69,15 @@ static inline void CRC32_update_buf( uint32_t * crc, const uint8_t * const buffe
}
+static inline int real_bits( const int value )
+ {
+ int bits = 0, i, mask;
+ for( i = 1, mask = 1; mask > 0; ++i, mask <<= 1 )
+ if( value & mask ) bits = i;
+ return bits;
+ }
+
+
static const uint8_t magic_string[4] = { 'L', 'Z', 'I', 'P' };
typedef uint8_t File_header[6]; /* 0-3 magic bytes */
@@ -76,52 +85,44 @@ typedef uint8_t File_header[6]; /* 0-3 magic bytes */
/* 5 coded_dict_size */
enum { Fh_size = 6 };
-static inline void Fh_set_magic( File_header header )
+static inline void Fh_set_magic( File_header data )
{
- memcpy( header, magic_string, 4 );
- header[4] = 1;
+ memcpy( data, magic_string, 4 );
+ data[4] = 1;
}
-static inline bool Fh_verify_magic( const File_header header )
+static inline bool Fh_verify_magic( const File_header data )
{
- return ( memcmp( header, magic_string, 4 ) == 0 );
+ return ( memcmp( data, magic_string, 4 ) == 0 );
}
-static inline uint8_t Fh_version( const File_header header )
- { return header[4]; }
+static inline uint8_t Fh_version( const File_header data )
+ { return data[4]; }
-static inline bool Fh_verify_version( const File_header header )
- { return ( header[4] <= 1 ); }
-
-static inline int Fh_real_bits( const int value )
- {
- int bits = 0, i, mask;
- for( i = 1, mask = 1; mask > 0; ++i, mask <<= 1 )
- if( value & mask ) bits = i;
- return bits;
- }
+static inline bool Fh_verify_version( const File_header data )
+ { return ( data[4] <= 1 ); }
-static inline int Fh_get_dictionary_size( const File_header header )
+static inline int Fh_get_dictionary_size( const File_header data )
{
- int sz = ( 1 << ( header[5] & 0x1F ) );
+ int sz = ( 1 << ( data[5] & 0x1F ) );
if( sz > min_dictionary_size && sz <= max_dictionary_size )
- sz -= ( sz / 16 ) * ( ( header[5] >> 5 ) & 0x07 );
+ sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 0x07 );
return sz;
}
-static inline bool Fh_set_dictionary_size( File_header header, const int sz )
+static inline bool Fh_set_dictionary_size( File_header data, const int sz )
{
if( sz >= min_dictionary_size && sz <= max_dictionary_size )
{
- header[5] = Fh_real_bits( sz - 1 );
+ data[5] = real_bits( sz - 1 );
if( sz > min_dictionary_size )
{
- const int base_size = 1 << header[5];
+ const int base_size = 1 << data[5];
const int wedge = base_size / 16;
int i;
for( i = 7; i >= 1; --i )
if( base_size - ( i * wedge ) >= sz )
- { header[5] |= ( i << 5 ); break; }
+ { data[5] |= ( i << 5 ); break; }
}
return true;
}
@@ -139,51 +140,51 @@ enum { Ft_size = 20 };
static inline int Ft_versioned_size( const int version )
{ return ( ( version >= 1 ) ? 20 : 12 ); }
-static inline uint32_t Ft_get_data_crc( const File_trailer trailer )
+static inline uint32_t Ft_get_data_crc( const File_trailer data )
{
uint32_t tmp = 0;
int i;
- for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += trailer[i]; }
+ for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
-static inline void Ft_set_data_crc( File_trailer trailer, uint32_t crc )
+static inline void Ft_set_data_crc( File_trailer data, uint32_t crc )
{
int i;
- for( i = 0; i <= 3; ++i ) { trailer[i] = (uint8_t)crc; crc >>= 8; }
+ for( i = 0; i <= 3; ++i ) { data[i] = (uint8_t)crc; crc >>= 8; }
}
-static inline long long Ft_get_data_size( const File_trailer trailer )
+static inline long long Ft_get_data_size( const File_trailer data )
{
long long tmp = 0;
int i;
- for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += trailer[i]; }
+ for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
-static inline void Ft_set_data_size( File_trailer trailer, long long sz )
+static inline void Ft_set_data_size( File_trailer data, long long sz )
{
int i;
- for( i = 4; i <= 11; ++i ) { trailer[i] = (uint8_t)sz; sz >>= 8; }
+ for( i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; }
}
-static inline long long Ft_get_member_size( const File_trailer trailer )
+static inline long long Ft_get_member_size( const File_trailer data )
{
long long tmp = 0;
int i;
- for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += trailer[i]; }
+ for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
-static inline void Ft_set_member_size( File_trailer trailer, long long sz )
+static inline void Ft_set_member_size( File_trailer data, long long sz )
{
int i;
- for( i = 12; i <= 19; ++i ) { trailer[i] = (uint8_t)sz; sz >>= 8; }
+ for( i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; }
}
+/* defined in main.c */
extern int verbosity;
-void cleanup_and_fail( const int retval );
void show_error( const char * const msg, const int errcode, const bool help );
void internal_error( const char * const msg );