summaryrefslogtreecommitdiffstats
path: root/lzip.h
diff options
context:
space:
mode:
Diffstat (limited to 'lzip.h')
-rw-r--r--lzip.h70
1 files changed, 43 insertions, 27 deletions
diff --git a/lzip.h b/lzip.h
index 41784da..b2f04ef 100644
--- a/lzip.h
+++ b/lzip.h
@@ -1,5 +1,5 @@
/* Clzip - LZMA lossless data compressor
- Copyright (C) 2010-2018 Antonio Diaz Diaz.
+ Copyright (C) 2010-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
@@ -36,6 +36,8 @@ static inline State St_set_char( const State st )
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 ); }
@@ -119,7 +121,7 @@ static inline void Lm_init( struct Len_model * const lm )
/* defined in main.c */
extern int verbosity;
-struct Pretty_print
+struct Pretty_print /* requires global var 'int verbosity' */
{
const char * name;
char * padded_name;
@@ -146,7 +148,7 @@ static inline void Pp_init( struct Pretty_print * const pp,
{
const char * const s = filenames[i];
const unsigned len = (strcmp( s, "-" ) == 0) ? stdin_name_len : strlen( s );
- if( len > pp->longest_name ) pp->longest_name = len;
+ if( pp->longest_name < len ) pp->longest_name = len;
}
if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len;
}
@@ -220,43 +222,43 @@ static inline int real_bits( unsigned value )
}
-static const uint8_t magic_string[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
+static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
-typedef uint8_t File_header[6]; /* 0-3 magic bytes */
+typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */
/* 4 version */
/* 5 coded_dict_size */
-enum { Fh_size = 6 };
+enum { Lh_size = 6 };
-static inline void Fh_set_magic( File_header data )
- { memcpy( data, magic_string, 4 ); data[4] = 1; }
+static inline void Lh_set_magic( Lzip_header data )
+ { memcpy( data, lzip_magic, 4 ); data[4] = 1; }
-static inline bool Fh_verify_magic( const File_header data )
- { return ( memcmp( data, magic_string, 4 ) == 0 ); }
+static inline bool Lh_verify_magic( const Lzip_header data )
+ { return ( memcmp( data, lzip_magic, 4 ) == 0 ); }
/* detect (truncated) header */
-static inline bool Fh_verify_prefix( const File_header data, const int sz )
+static inline bool Lh_verify_prefix( const Lzip_header data, const int sz )
{
int i; for( i = 0; i < sz && i < 4; ++i )
- if( data[i] != magic_string[i] ) return false;
+ if( data[i] != lzip_magic[i] ) return false;
return ( sz > 0 );
}
/* detect corrupt header */
-static inline bool Fh_verify_corrupt( const File_header data )
+static inline bool Lh_verify_corrupt( const Lzip_header data )
{
int matches = 0;
int i; for( i = 0; i < 4; ++i )
- if( data[i] == magic_string[i] ) ++matches;
+ if( data[i] == lzip_magic[i] ) ++matches;
return ( matches > 1 && matches < 4 );
}
-static inline uint8_t Fh_version( const File_header data )
+static inline uint8_t Lh_version( const Lzip_header data )
{ return data[4]; }
-static inline bool Fh_verify_version( const File_header data )
+static inline bool Lh_verify_version( const Lzip_header data )
{ return ( data[4] == 1 ); }
-static inline unsigned Fh_get_dictionary_size( const File_header data )
+static inline unsigned Lh_get_dictionary_size( const Lzip_header data )
{
unsigned sz = ( 1 << ( data[5] & 0x1F ) );
if( sz > min_dictionary_size )
@@ -264,7 +266,7 @@ static inline unsigned Fh_get_dictionary_size( const File_header data )
return sz;
}
-static inline bool Fh_set_dictionary_size( File_header data, const unsigned sz )
+static inline bool Lh_set_dictionary_size( Lzip_header data, const unsigned sz )
{
if( !isvalid_ds( sz ) ) return false;
data[5] = real_bits( sz - 1 );
@@ -281,43 +283,57 @@ static inline bool Fh_set_dictionary_size( File_header data, const unsigned sz )
}
-typedef uint8_t File_trailer[20];
+typedef uint8_t Lzip_trailer[20];
/* 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 };
-enum { Ft_size = 20 };
-
-static inline unsigned Ft_get_data_crc( const File_trailer data )
+static inline unsigned Lt_get_data_crc( const Lzip_trailer data )
{
unsigned tmp = 0;
int i; for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
-static inline void Ft_set_data_crc( File_trailer data, unsigned crc )
+static inline void Lt_set_data_crc( Lzip_trailer data, unsigned crc )
{ int i; for( i = 0; i <= 3; ++i ) { data[i] = (uint8_t)crc; crc >>= 8; } }
-static inline unsigned long long Ft_get_data_size( const File_trailer data )
+static inline unsigned long long Lt_get_data_size( const Lzip_trailer data )
{
unsigned long long tmp = 0;
int i; for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
-static inline void Ft_set_data_size( File_trailer data, unsigned long long sz )
+static inline void Lt_set_data_size( Lzip_trailer data, unsigned long long sz )
{ int i; for( i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
-static inline unsigned long long Ft_get_member_size( const File_trailer data )
+static inline unsigned long long Lt_get_member_size( const Lzip_trailer data )
{
unsigned long long tmp = 0;
int i; for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp;
}
-static inline void Ft_set_member_size( File_trailer data, unsigned long long sz )
+static inline void Lt_set_member_size( Lzip_trailer data, unsigned long long sz )
{ int i; for( i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
+/* check internal consistency */
+static inline bool Lt_verify_consistency( const Lzip_trailer data )
+ {
+ const unsigned crc = Lt_get_data_crc( data );
+ const unsigned long long dsize = Lt_get_data_size( data );
+ const unsigned long long msize = Lt_get_member_size( data );
+ const unsigned long long mlimit = ( 9 * dsize + 7 ) / 8 + min_member_size;
+ const unsigned long long dlimit = 7090 * ( msize - 26 ) - 1;
+ if( ( crc == 0 ) != ( dsize == 0 ) ) return false;
+ if( msize < min_member_size ) return false;
+ if( mlimit > dsize && msize > mlimit ) return false;
+ if( dlimit > msize && dsize > dlimit ) return false;
+ return true;
+ }
+
static const char * const bad_magic_msg = "Bad magic number (file not in lzip format).";
static const char * const bad_dict_msg = "Invalid dictionary size in member header.";