summaryrefslogtreecommitdiffstats
path: root/lzlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lzlib.c')
-rw-r--r--lzlib.c142
1 files changed, 71 insertions, 71 deletions
diff --git a/lzlib.c b/lzlib.c
index f9f7d2f..4105205 100644
--- a/lzlib.c
+++ b/lzlib.c
@@ -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.
*/
#include <stdbool.h>
@@ -86,7 +86,7 @@ static void LZ_Decoder_init( struct LZ_Decoder * const d )
}
-static bool verify_encoder( struct LZ_Encoder * const e )
+static bool check_encoder( struct LZ_Encoder * const e )
{
if( !e ) return false;
if( !e->lz_encoder_base || ( !e->lz_encoder && !e->flz_encoder ) ||
@@ -96,7 +96,7 @@ static bool verify_encoder( struct LZ_Encoder * const e )
}
-static bool verify_decoder( struct LZ_Decoder * const d )
+static bool check_decoder( struct LZ_Decoder * const d )
{
if( !d ) return false;
if( !d->rdec )
@@ -105,10 +105,11 @@ static bool verify_decoder( struct LZ_Decoder * const d )
}
-/*------------------------- Misc Functions -------------------------*/
+/* ------------------------- Misc Functions ------------------------- */
-const char * LZ_version( void ) { return LZ_version_string; }
+int LZ_api_version( void ) { return LZ_API_VERSION; }
+const char * LZ_version( void ) { return LZ_version_string; }
const char * LZ_strerror( const enum LZ_Errno lz_errno )
{
@@ -135,7 +136,7 @@ int LZ_min_match_len_limit( void ) { return min_match_len_limit; }
int LZ_max_match_len_limit( void ) { return max_match_len; }
-/*---------------------- Compression Functions ----------------------*/
+/* --------------------- Compression Functions --------------------- */
struct LZ_Encoder * LZ_compress_open( const int dictionary_size,
const int match_len_limit,
@@ -188,7 +189,7 @@ int LZ_compress_close( struct LZ_Encoder * const e )
int LZ_compress_finish( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) || e->fatal ) return -1;
+ if( !check_encoder( e ) || e->fatal ) return -1;
Mb_finish( &e->lz_encoder_base->mb );
/* if (open --> write --> finish) use same dictionary size as lzip. */
/* this does not save any memory. */
@@ -207,7 +208,7 @@ int LZ_compress_finish( struct LZ_Encoder * const e )
int LZ_compress_restart_member( struct LZ_Encoder * const e,
const unsigned long long member_size )
{
- if( !verify_encoder( e ) || e->fatal ) return -1;
+ if( !check_encoder( e ) || e->fatal ) return -1;
if( !LZeb_member_finished( e->lz_encoder_base ) )
{ e->lz_errno = LZ_sequence_error; return -1; }
if( member_size < min_dictionary_size )
@@ -225,9 +226,9 @@ int LZ_compress_restart_member( struct LZ_Encoder * const e,
int LZ_compress_sync_flush( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) || e->fatal ) return -1;
- if( !Mb_flushing_or_end( &e->lz_encoder_base->mb ) )
- e->lz_encoder_base->mb.flushing = true;
+ if( !check_encoder( e ) || e->fatal ) return -1;
+ if( !e->lz_encoder_base->mb.at_stream_end )
+ e->lz_encoder_base->mb.sync_flush_pending = true;
return 0;
}
@@ -235,38 +236,36 @@ int LZ_compress_sync_flush( struct LZ_Encoder * const e )
int LZ_compress_read( struct LZ_Encoder * const e,
uint8_t * const buffer, const int size )
{
- int out_size = 0;
- if( !verify_encoder( e ) || e->fatal ) return -1;
+ if( !check_encoder( e ) || e->fatal ) return -1;
if( size < 0 ) return 0;
- do {
+
+ { struct LZ_encoder_base * const eb = e->lz_encoder_base;
+ int out_size = Re_read_data( &eb->renc, buffer, size );
+ /* minimize number of calls to encode_member */
+ if( out_size < size || size == 0 )
+ {
if( ( e->flz_encoder && !FLZe_encode_member( e->flz_encoder ) ) ||
( e->lz_encoder && !LZe_encode_member( e->lz_encoder ) ) )
{ e->lz_errno = LZ_library_error; e->fatal = true; return -1; }
- if( e->lz_encoder_base->mb.flushing &&
- Mb_available_bytes( &e->lz_encoder_base->mb ) <= 0 &&
- LZeb_sync_flush( e->lz_encoder_base ) )
- e->lz_encoder_base->mb.flushing = false;
- out_size += Re_read_data( &e->lz_encoder_base->renc,
- buffer + out_size, size - out_size );
+ if( eb->mb.sync_flush_pending && Mb_available_bytes( &eb->mb ) <= 0 )
+ LZeb_try_sync_flush( eb );
+ out_size += Re_read_data( &eb->renc, buffer + out_size, size - out_size );
}
- while( e->lz_encoder_base->mb.flushing && out_size < size &&
- Mb_enough_available_bytes( &e->lz_encoder_base->mb ) &&
- Re_enough_free_bytes( &e->lz_encoder_base->renc ) );
- return out_size;
+ return out_size; }
}
int LZ_compress_write( struct LZ_Encoder * const e,
const uint8_t * const buffer, const int size )
{
- if( !verify_encoder( e ) || e->fatal ) return -1;
+ if( !check_encoder( e ) || e->fatal ) return -1;
return Mb_write_data( &e->lz_encoder_base->mb, buffer, size );
}
int LZ_compress_write_size( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) || e->fatal ) return -1;
+ if( !check_encoder( e ) || e->fatal ) return -1;
return Mb_free_bytes( &e->lz_encoder_base->mb );
}
@@ -280,48 +279,48 @@ enum LZ_Errno LZ_compress_errno( struct LZ_Encoder * const e )
int LZ_compress_finished( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) ) return -1;
- return ( Mb_data_finished( &e->lz_encoder_base->mb ) &&
- LZeb_member_finished( e->lz_encoder_base ) );
+ if( !check_encoder( e ) ) return -1;
+ return Mb_data_finished( &e->lz_encoder_base->mb ) &&
+ LZeb_member_finished( e->lz_encoder_base );
}
int LZ_compress_member_finished( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) ) return -1;
+ if( !check_encoder( e ) ) return -1;
return LZeb_member_finished( e->lz_encoder_base );
}
unsigned long long LZ_compress_data_position( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) ) return 0;
+ if( !check_encoder( e ) ) return 0;
return Mb_data_position( &e->lz_encoder_base->mb );
}
unsigned long long LZ_compress_member_position( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) ) return 0;
+ if( !check_encoder( e ) ) return 0;
return Re_member_position( &e->lz_encoder_base->renc );
}
unsigned long long LZ_compress_total_in_size( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) ) return 0;
+ if( !check_encoder( e ) ) return 0;
return e->partial_in_size + Mb_data_position( &e->lz_encoder_base->mb );
}
unsigned long long LZ_compress_total_out_size( struct LZ_Encoder * const e )
{
- if( !verify_encoder( e ) ) return 0;
+ if( !check_encoder( e ) ) return 0;
return e->partial_out_size + Re_member_position( &e->lz_encoder_base->renc );
}
-/*--------------------- Decompression Functions ---------------------*/
+/* -------------------- Decompression Functions -------------------- */
struct LZ_Decoder * LZ_decompress_open( void )
{
@@ -353,7 +352,7 @@ int LZ_decompress_close( struct LZ_Decoder * const d )
int LZ_decompress_finish( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) || d->fatal ) return -1;
+ if( !check_decoder( d ) || d->fatal ) return -1;
if( d->seeking )
{ d->seeking = false; d->partial_in_size += Rd_purge( d->rdec ); }
else Rd_finish( d->rdec );
@@ -363,7 +362,7 @@ int LZ_decompress_finish( struct LZ_Decoder * const d )
int LZ_decompress_reset( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) ) return -1;
+ if( !check_decoder( d ) ) return -1;
if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; }
d->partial_in_size = 0;
@@ -380,7 +379,7 @@ int LZ_decompress_reset( struct LZ_Decoder * const d )
int LZ_decompress_sync_to_member( struct LZ_Decoder * const d )
{
unsigned skipped = 0;
- if( !verify_decoder( d ) ) return -1;
+ if( !check_decoder( d ) ) return -1;
if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; }
if( Rd_find_header( d->rdec, &skipped ) ) d->seeking = false;
@@ -400,11 +399,12 @@ int LZ_decompress_read( struct LZ_Decoder * const d,
uint8_t * const buffer, const int size )
{
int result;
- if( !verify_decoder( d ) ) return -1;
+ if( !check_decoder( d ) ) return -1;
+ if( size < 0 ) return 0;
if( d->fatal ) /* don't return error until pending bytes are read */
{ if( d->lz_decoder && !Cb_empty( &d->lz_decoder->cb ) ) goto get_data;
return -1; }
- if( d->seeking || size < 0 ) return 0;
+ if( d->seeking ) return 0;
if( d->lz_decoder && LZd_member_finished( d->lz_decoder ) )
{
@@ -422,20 +422,20 @@ int LZ_decompress_read( struct LZ_Decoder * const d,
rd = Rd_read_data( d->rdec, d->member_header, Lh_size );
if( rd < Lh_size || Rd_finished( d->rdec ) ) /* End Of File */
{
- if( rd <= 0 || Lh_verify_prefix( d->member_header, rd ) )
+ if( rd <= 0 || Lh_check_prefix( d->member_header, rd ) )
d->lz_errno = LZ_unexpected_eof;
else
d->lz_errno = LZ_header_error;
d->fatal = true;
return -1;
}
- if( !Lh_verify_magic( d->member_header ) )
+ if( !Lh_check_magic( d->member_header ) )
{
/* unreading the header prevents sync_to_member from skipping a member
if leading garbage is shorter than a full header; "lgLZIP\x01\x0C" */
if( Rd_unread_data( d->rdec, rd ) )
{
- if( d->first_header || !Lh_verify_corrupt( d->member_header ) )
+ if( d->first_header || !Lh_check_corrupt( d->member_header ) )
d->lz_errno = LZ_header_error;
else
d->lz_errno = LZ_data_error; /* corrupt header */
@@ -445,12 +445,12 @@ int LZ_decompress_read( struct LZ_Decoder * const d,
d->fatal = true;
return -1;
}
- if( !Lh_verify_version( d->member_header ) ||
+ if( !Lh_check_version( d->member_header ) ||
!isvalid_ds( Lh_get_dictionary_size( d->member_header ) ) )
{
/* Skip a possible "LZIP" leading garbage; "LZIPLZIP\x01\x0C".
Leave member_pos pointing to the first error. */
- if( Rd_unread_data( d->rdec, 1 + !Lh_verify_version( d->member_header ) ) )
+ if( Rd_unread_data( d->rdec, 1 + !Lh_check_version( d->member_header ) ) )
d->lz_errno = LZ_data_error; /* bad version or bad dict size */
else
d->lz_errno = LZ_library_error;
@@ -500,7 +500,7 @@ int LZ_decompress_write( struct LZ_Decoder * const d,
const uint8_t * const buffer, const int size )
{
int result;
- if( !verify_decoder( d ) || d->fatal ) return -1;
+ if( !check_decoder( d ) || d->fatal ) return -1;
if( size < 0 ) return 0;
result = Rd_write_data( d->rdec, buffer, size );
@@ -521,7 +521,7 @@ int LZ_decompress_write( struct LZ_Decoder * const d,
int LZ_decompress_write_size( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) || d->fatal ) return -1;
+ if( !check_decoder( d ) || d->fatal ) return -1;
return Rd_free_bytes( d->rdec );
}
@@ -535,36 +535,36 @@ enum LZ_Errno LZ_decompress_errno( struct LZ_Decoder * const d )
int LZ_decompress_finished( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) || d->fatal ) return -1;
- return ( Rd_finished( d->rdec ) &&
- ( !d->lz_decoder || LZd_member_finished( d->lz_decoder ) ) );
+ if( !check_decoder( d ) || d->fatal ) return -1;
+ return Rd_finished( d->rdec ) &&
+ ( !d->lz_decoder || LZd_member_finished( d->lz_decoder ) );
}
int LZ_decompress_member_finished( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) || d->fatal ) return -1;
- return ( d->lz_decoder && LZd_member_finished( d->lz_decoder ) );
+ if( !check_decoder( d ) || d->fatal ) return -1;
+ return d->lz_decoder && LZd_member_finished( d->lz_decoder );
}
int LZ_decompress_member_version( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) ) return -1;
+ if( !check_decoder( d ) ) return -1;
return Lh_version( d->member_header );
}
int LZ_decompress_dictionary_size( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) ) return -1;
+ if( !check_decoder( d ) ) return -1;
return Lh_get_dictionary_size( d->member_header );
}
unsigned LZ_decompress_data_crc( struct LZ_Decoder * const d )
{
- if( verify_decoder( d ) && d->lz_decoder )
+ if( check_decoder( d ) && d->lz_decoder )
return LZd_crc( d->lz_decoder );
return 0;
}
@@ -572,7 +572,7 @@ unsigned LZ_decompress_data_crc( struct LZ_Decoder * const d )
unsigned long long LZ_decompress_data_position( struct LZ_Decoder * const d )
{
- if( verify_decoder( d ) && d->lz_decoder )
+ if( check_decoder( d ) && d->lz_decoder )
return LZd_data_position( d->lz_decoder );
return 0;
}
@@ -580,21 +580,21 @@ unsigned long long LZ_decompress_data_position( struct LZ_Decoder * const d )
unsigned long long LZ_decompress_member_position( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) ) return 0;
+ if( !check_decoder( d ) ) return 0;
return d->rdec->member_position;
}
unsigned long long LZ_decompress_total_in_size( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) ) return 0;
+ if( !check_decoder( d ) ) return 0;
return d->partial_in_size + d->rdec->member_position;
}
unsigned long long LZ_decompress_total_out_size( struct LZ_Decoder * const d )
{
- if( !verify_decoder( d ) ) return 0;
+ if( !check_decoder( d ) ) return 0;
if( d->lz_decoder )
return d->partial_out_size + LZd_data_position( d->lz_decoder );
return d->partial_out_size;