1
0
Fork 0

Merging upstream version 1.6~pre2.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-20 20:21:13 +01:00
parent 0150d08d9a
commit fb214fca50
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
23 changed files with 679 additions and 722 deletions

View file

@ -1,3 +1,9 @@
2014-01-30 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.6-pre2 released.
* main.c (close_and_set_permissions): Behave like 'cp -p'.
* lzlib.texinfo: Renamed to lzlib.texi.
2013-10-07 Antonio Diaz Diaz <antonio@gnu.org> 2013-10-07 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.6-pre1 released. * Version 1.6-pre1 released.
@ -140,7 +146,7 @@
* Version 0.1 released. * Version 0.1 released.
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This file is a collection of facts, and thus it is not copyrightable, This file is a collection of facts, and thus it is not copyrightable,
but just in case, you have unlimited permission to copy, distribute and but just in case, you have unlimited permission to copy, distribute and

14
INSTALL
View file

@ -43,12 +43,12 @@ the main archive.
Another way Another way
----------- -----------
You can also compile lzlib into a separate directory. To do this, you You can also compile lzlib into a separate directory.
must use a version of 'make' that supports the 'VPATH' variable, such To do this, you must use a version of 'make' that supports the 'VPATH'
as GNU 'make'. 'cd' to the directory where you want the object files variable, such as GNU 'make'. 'cd' to the directory where you want the
and executables to go and run the 'configure' script. 'configure' object files and executables to go and run the 'configure' script.
automatically checks for the source code in '.', in '..' and in the 'configure' automatically checks for the source code in '.', in '..' and
directory that 'configure' is in. in the directory that 'configure' is in.
'configure' recognizes the option '--srcdir=DIR' to control where to 'configure' recognizes the option '--srcdir=DIR' to control where to
look for the sources. Usually 'configure' can determine that directory look for the sources. Usually 'configure' can determine that directory
@ -58,7 +58,7 @@ After running 'configure', you can run 'make' and 'make install' as
explained above. explained above.
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This file is free documentation: you have unlimited permission to copy, This file is free documentation: you have unlimited permission to copy,
distribute and modify it. distribute and modify it.

View file

@ -2,8 +2,8 @@
DISTNAME = $(pkgname)-$(pkgversion) DISTNAME = $(pkgname)-$(pkgversion)
AR = ar AR = ar
INSTALL = install INSTALL = install
INSTALL_PROGRAM = $(INSTALL) -p -m 755 INSTALL_PROGRAM = $(INSTALL) -m 755
INSTALL_DATA = $(INSTALL) -p -m 644 INSTALL_DATA = $(INSTALL) -m 644
INSTALL_DIR = $(INSTALL) -d -m 755 INSTALL_DIR = $(INSTALL) -d -m 755
LDCONFIG = /sbin/ldconfig LDCONFIG = /sbin/ldconfig
SHELL = /bin/sh SHELL = /bin/sh
@ -41,12 +41,12 @@ lzcheck : lzcheck.o lib$(libname).a
main.o : main.c main.o : main.c
$(CC) $(CPPFLAGS) $(CFLAGS) -DPROGVERSION=\"$(pkgversion)\" -c -o $@ $< $(CC) $(CPPFLAGS) $(CFLAGS) -DPROGVERSION=\"$(pkgversion)\" -c -o $@ $<
%.o : %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
lzlib_sh.o : lzlib.c lzlib_sh.o : lzlib.c
$(CC) -fpic -fPIC $(CPPFLAGS) $(CFLAGS) -c -o $@ $< $(CC) -fpic -fPIC $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
%.o : %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
lzdeps = lzlib.h lzip.h cbuffer.c decoder.h decoder.c encoder.h encoder.c lzdeps = lzlib.h lzip.h cbuffer.c decoder.h decoder.c encoder.h encoder.c
$(objs) : Makefile $(objs) : Makefile
@ -62,8 +62,8 @@ doc : info man
info : $(VPATH)/doc/$(pkgname).info info : $(VPATH)/doc/$(pkgname).info
$(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texinfo $(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texi
cd $(VPATH)/doc && makeinfo $(pkgname).texinfo cd $(VPATH)/doc && makeinfo $(pkgname).texi
man : $(VPATH)/doc/$(progname).1 man : $(VPATH)/doc/$(progname).1
@ -136,7 +136,7 @@ uninstall-man :
dist : doc dist : doc
ln -sf $(VPATH) $(DISTNAME) ln -sf $(VPATH) $(DISTNAME)
tar -cvf $(DISTNAME).tar \ tar -Hustar --owner=root --group=root -cvf $(DISTNAME).tar \
$(DISTNAME)/AUTHORS \ $(DISTNAME)/AUTHORS \
$(DISTNAME)/COPYING \ $(DISTNAME)/COPYING \
$(DISTNAME)/ChangeLog \ $(DISTNAME)/ChangeLog \
@ -147,7 +147,7 @@ dist : doc
$(DISTNAME)/configure \ $(DISTNAME)/configure \
$(DISTNAME)/doc/$(progname).1 \ $(DISTNAME)/doc/$(progname).1 \
$(DISTNAME)/doc/$(pkgname).info \ $(DISTNAME)/doc/$(pkgname).info \
$(DISTNAME)/doc/$(pkgname).texinfo \ $(DISTNAME)/doc/$(pkgname).texi \
$(DISTNAME)/testsuite/check.sh \ $(DISTNAME)/testsuite/check.sh \
$(DISTNAME)/testsuite/test.txt \ $(DISTNAME)/testsuite/test.txt \
$(DISTNAME)/testsuite/test.txt.lz \ $(DISTNAME)/testsuite/test.txt.lz \

6
NEWS
View file

@ -5,3 +5,9 @@ Improved portability to BSD systems:
The configure script now accepts the option "--disable-ldconfig". The configure script now accepts the option "--disable-ldconfig".
"make install" now ignores any errors from ldconfig. "make install" now ignores any errors from ldconfig.
Minilzip now copies file dates, permissions, and ownership like "cp -p".
(If the user ID or the group ID can't be duplicated, the file permission
bits S_ISUID and S_ISGID are cleared).
"lzlib.texinfo" has been renamed to "lzlib.texi".

8
README
View file

@ -10,9 +10,9 @@ clean, provides very safe 4 factor integrity checking, and is backed by
the recovery capabilities of lziprecover. the recovery capabilities of lziprecover.
The functions and variables forming the interface of the compression The functions and variables forming the interface of the compression
library are declared in the file lzlib.h. Usage examples of the library library are declared in the file 'lzlib.h'. Usage examples of the
are given in the files main.c and bbexample.c from the source library are given in the files 'main.c' and 'bbexample.c' from the
distribution. source distribution.
Compression/decompression is done by repeatedly calling a couple of Compression/decompression is done by repeatedly calling a couple of
read/write functions until all the data has been processed by the read/write functions until all the data has been processed by the
@ -49,7 +49,7 @@ range encoding), Igor Pavlov (for putting all the above together in
LZMA), and Julian Seward (for bzip2's CLI). LZMA), and Julian Seward (for bzip2's CLI).
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This file is free documentation: you have unlimited permission to copy, This file is free documentation: you have unlimited permission to copy,
distribute and modify it. distribute and modify it.

View file

@ -1,5 +1,5 @@
/* Buff to buff example - Test program for the lzlib library /* Buff to buff example - Test program for the lzlib library
Copyright (C) 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This program is free software: you have unlimited permission This program is free software: you have unlimited permission
to copy, distribute and modify it. to copy, distribute and modify it.

View file

@ -1,5 +1,5 @@
/* Arg_parser - POSIX/GNU command line argument parser. (C version) /* Arg_parser - POSIX/GNU command line argument parser. (C version)
Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
Antonio Diaz Diaz. Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,5 @@
/* Arg_parser - POSIX/GNU command line argument parser. (C version) /* Arg_parser - POSIX/GNU command line argument parser. (C version)
Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
Antonio Diaz Diaz. Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by

8
configure vendored
View file

@ -1,18 +1,18 @@
#! /bin/sh #! /bin/sh
# configure script for Lzlib - Compression library for lzip files # configure script for Lzlib - Compression library for lzip files
# Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
# #
# This configure script is free software: you have unlimited permission # This configure script is free software: you have unlimited permission
# to copy, distribute and modify it. # to copy, distribute and modify it.
pkgname=lzlib pkgname=lzlib
pkgversion=1.6-pre1 pkgversion=1.6-pre2
soversion=1 soversion=1
progname=minilzip progname=minilzip
progname_shared= progname_shared=
disable_ldconfig= disable_ldconfig=
libname=lz libname=lz
srctrigger=doc/${pkgname}.texinfo srctrigger=doc/${pkgname}.texi
# clear some things potentially inherited from environment. # clear some things potentially inherited from environment.
LC_ALL=C LC_ALL=C
@ -183,7 +183,7 @@ echo "LDFLAGS = ${LDFLAGS}"
rm -f Makefile rm -f Makefile
cat > Makefile << EOF cat > Makefile << EOF
# Makefile for Lzlib - Compression library for lzip files # Makefile for Lzlib - Compression library for lzip files
# Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
# This file was generated automatically by configure. Do not edit. # This file was generated automatically by configure. Do not edit.
# #
# This Makefile is free software: you have unlimited permission # This Makefile is free software: you have unlimited permission

114
decoder.c
View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -25,123 +25,120 @@
Public License. Public License.
*/ */
static bool LZd_verify_trailer( struct LZ_decoder * const decoder ) static bool LZd_verify_trailer( struct LZ_decoder * const d )
{ {
File_trailer trailer; File_trailer trailer;
const unsigned long long member_size = const unsigned long long member_size = d->rdec->member_position + Ft_size;
decoder->rdec->member_position + Ft_size;
int size = Rd_read_data( decoder->rdec, trailer, Ft_size ); int size = Rd_read_data( d->rdec, trailer, Ft_size );
if( size < Ft_size ) if( size < Ft_size )
return false; return false;
return ( decoder->rdec->code == 0 && return ( d->rdec->code == 0 &&
Ft_get_data_crc( trailer ) == LZd_crc( decoder ) && Ft_get_data_crc( trailer ) == LZd_crc( d ) &&
Ft_get_data_size( trailer ) == LZd_data_position( decoder ) && Ft_get_data_size( trailer ) == LZd_data_position( d ) &&
Ft_get_member_size( trailer ) == member_size ); Ft_get_member_size( trailer ) == member_size );
} }
/* Return value: 0 = OK, 1 = decoder error, 2 = unexpected EOF, /* Return value: 0 = OK, 1 = decoder error, 2 = unexpected EOF,
3 = trailer error, 4 = unknown marker found. */ 3 = trailer error, 4 = unknown marker found. */
static int LZd_decode_member( struct LZ_decoder * const decoder ) static int LZd_decode_member( struct LZ_decoder * const d )
{ {
struct Range_decoder * const rdec = decoder->rdec; struct Range_decoder * const rdec = d->rdec;
State * const state = &decoder->state; State * const state = &d->state;
if( decoder->member_finished ) return 0; if( d->member_finished ) return 0;
if( !Rd_try_reload( rdec, false ) ) return 0; if( !Rd_try_reload( rdec, false ) ) return 0;
if( decoder->verify_trailer_pending ) if( d->verify_trailer_pending )
{ {
if( Rd_available_bytes( rdec ) < Ft_size && !rdec->at_stream_end ) if( Rd_available_bytes( rdec ) < Ft_size && !rdec->at_stream_end )
return 0; return 0;
decoder->verify_trailer_pending = false; d->verify_trailer_pending = false;
decoder->member_finished = true; d->member_finished = true;
if( LZd_verify_trailer( decoder ) ) return 0; else return 3; if( LZd_verify_trailer( d ) ) return 0; else return 3;
} }
while( !Rd_finished( rdec ) ) while( !Rd_finished( rdec ) )
{ {
const int pos_state = LZd_data_position( decoder ) & pos_state_mask; const int pos_state = LZd_data_position( d ) & pos_state_mask;
if( !Rd_enough_available_bytes( rdec ) || if( !Rd_enough_available_bytes( rdec ) || !LZd_enough_free_bytes( d ) )
!LZd_enough_free_bytes( decoder ) )
return 0; return 0;
if( Rd_decode_bit( rdec, &decoder->bm_match[*state][pos_state] ) == 0 ) /* 1st bit */ if( Rd_decode_bit( rdec, &d->bm_match[*state][pos_state] ) == 0 ) /* 1st bit */
{ {
const uint8_t prev_byte = LZd_get_prev_byte( decoder ); const uint8_t prev_byte = LZd_get_prev_byte( d );
if( St_is_char( *state ) ) if( St_is_char( *state ) )
{ {
*state -= ( *state < 4 ) ? *state : 3; *state -= ( *state < 4 ) ? *state : 3;
LZd_put_byte( decoder, Rd_decode_tree( rdec, LZd_put_byte( d, Rd_decode_tree( rdec,
decoder->bm_literal[get_lit_state(prev_byte)], 8 ) ); d->bm_literal[get_lit_state(prev_byte)], 8 ) );
} }
else else
{ {
*state -= ( *state < 10 ) ? 3 : 6; *state -= ( *state < 10 ) ? 3 : 6;
LZd_put_byte( decoder, Rd_decode_matched( rdec, LZd_put_byte( d, Rd_decode_matched( rdec,
decoder->bm_literal[get_lit_state(prev_byte)], d->bm_literal[get_lit_state(prev_byte)],
LZd_get_byte( decoder, decoder->rep0 ) ) ); LZd_get_byte( d, d->rep0 ) ) );
} }
} }
else else
{ {
int len; int len;
if( Rd_decode_bit( rdec, &decoder->bm_rep[*state] ) != 0 ) /* 2nd bit */ if( Rd_decode_bit( rdec, &d->bm_rep[*state] ) != 0 ) /* 2nd bit */
{ {
if( Rd_decode_bit( rdec, &decoder->bm_rep0[*state] ) != 0 ) /* 3rd bit */ if( Rd_decode_bit( rdec, &d->bm_rep0[*state] ) != 0 ) /* 3rd bit */
{ {
unsigned distance; unsigned distance;
if( Rd_decode_bit( rdec, &decoder->bm_rep1[*state] ) == 0 ) /* 4th bit */ if( Rd_decode_bit( rdec, &d->bm_rep1[*state] ) == 0 ) /* 4th bit */
distance = decoder->rep1; distance = d->rep1;
else else
{ {
if( Rd_decode_bit( rdec, &decoder->bm_rep2[*state] ) == 0 ) /* 5th bit */ if( Rd_decode_bit( rdec, &d->bm_rep2[*state] ) == 0 ) /* 5th bit */
distance = decoder->rep2; distance = d->rep2;
else else
{ distance = decoder->rep3; decoder->rep3 = decoder->rep2; } { distance = d->rep3; d->rep3 = d->rep2; }
decoder->rep2 = decoder->rep1; d->rep2 = d->rep1;
} }
decoder->rep1 = decoder->rep0; d->rep1 = d->rep0;
decoder->rep0 = distance; d->rep0 = distance;
} }
else else
{ {
if( Rd_decode_bit( rdec, &decoder->bm_len[*state][pos_state] ) == 0 ) /* 4th bit */ if( Rd_decode_bit( rdec, &d->bm_len[*state][pos_state] ) == 0 ) /* 4th bit */
{ *state = St_set_short_rep( *state ); { *state = St_set_short_rep( *state );
LZd_put_byte( decoder, LZd_get_byte( decoder, decoder->rep0 ) ); continue; } LZd_put_byte( d, LZd_get_byte( d, d->rep0 ) ); continue; }
} }
*state = St_set_rep( *state ); *state = St_set_rep( *state );
len = min_match_len + Rd_decode_len( rdec, &decoder->rep_len_model, pos_state ); len = min_match_len + Rd_decode_len( rdec, &d->rep_len_model, pos_state );
} }
else else
{ {
int dis_slot; int dis_slot;
const unsigned rep0_saved = decoder->rep0; const unsigned rep0_saved = d->rep0;
len = min_match_len + Rd_decode_len( rdec, &decoder->match_len_model, pos_state ); len = min_match_len + Rd_decode_len( rdec, &d->match_len_model, pos_state );
dis_slot = Rd_decode_tree6( rdec, decoder->bm_dis_slot[get_len_state(len)] ); dis_slot = Rd_decode_tree6( rdec, d->bm_dis_slot[get_len_state(len)] );
if( dis_slot < start_dis_model ) decoder->rep0 = dis_slot; if( dis_slot < start_dis_model ) d->rep0 = dis_slot;
else else
{ {
const int direct_bits = ( dis_slot >> 1 ) - 1; const int direct_bits = ( dis_slot >> 1 ) - 1;
decoder->rep0 = ( 2 | ( dis_slot & 1 ) ) << direct_bits; d->rep0 = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
if( dis_slot < end_dis_model ) if( dis_slot < end_dis_model )
decoder->rep0 += Rd_decode_tree_reversed( rdec, d->rep0 += Rd_decode_tree_reversed( rdec,
decoder->bm_dis + decoder->rep0 - dis_slot - 1, d->bm_dis + d->rep0 - dis_slot - 1, direct_bits );
direct_bits );
else else
{ {
decoder->rep0 += Rd_decode( rdec, direct_bits - dis_align_bits ) << dis_align_bits; d->rep0 += Rd_decode( rdec, direct_bits - dis_align_bits ) << dis_align_bits;
decoder->rep0 += Rd_decode_tree_reversed4( rdec, decoder->bm_align ); d->rep0 += Rd_decode_tree_reversed4( rdec, d->bm_align );
if( decoder->rep0 == 0xFFFFFFFFU ) /* Marker found */ if( d->rep0 == 0xFFFFFFFFU ) /* Marker found */
{ {
decoder->rep0 = rep0_saved; d->rep0 = rep0_saved;
Rd_normalize( rdec ); Rd_normalize( rdec );
if( len == min_match_len ) /* End Of Stream marker */ if( len == min_match_len ) /* End Of Stream marker */
{ {
if( Rd_available_bytes( rdec ) < Ft_size && !rdec->at_stream_end ) if( Rd_available_bytes( rdec ) < Ft_size && !rdec->at_stream_end )
{ decoder->verify_trailer_pending = true; return 0; } { d->verify_trailer_pending = true; return 0; }
decoder->member_finished = true; d->member_finished = true;
if( LZd_verify_trailer( decoder ) ) return 0; else return 3; if( LZd_verify_trailer( d ) ) return 0; else return 3;
} }
if( len == min_match_len + 1 ) /* Sync Flush marker */ if( len == min_match_len + 1 ) /* Sync Flush marker */
{ {
@ -152,15 +149,12 @@ static int LZd_decode_member( struct LZ_decoder * const decoder )
} }
} }
} }
decoder->rep3 = decoder->rep2; d->rep3 = d->rep2; d->rep2 = d->rep1; d->rep1 = rep0_saved;
decoder->rep2 = decoder->rep1; decoder->rep1 = rep0_saved;
*state = St_set_match( *state ); *state = St_set_match( *state );
if( decoder->rep0 >= (unsigned)decoder->dictionary_size || if( d->rep0 >= d->dictionary_size || d->rep0 >= LZd_data_position( d ) )
( decoder->rep0 >= (unsigned)decoder->cb.put &&
!decoder->partial_data_pos ) )
return 1; return 1;
} }
LZd_copy_block( decoder, decoder->rep0, len ); LZd_copy_block( d, d->rep0, len );
} }
} }
return 2; return 2;

142
decoder.h
View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -159,6 +159,7 @@ static bool Rd_try_reload( struct Range_decoder * const rdec, const bool force )
for( i = 0; i < 5; ++i ) for( i = 0; i < 5; ++i )
rdec->code = (rdec->code << 8) | Rd_get_byte( rdec ); rdec->code = (rdec->code << 8) | Rd_get_byte( rdec );
rdec->range = 0xFFFFFFFFU; rdec->range = 0xFFFFFFFFU;
rdec->code &= rdec->range; /* make sure that first byte is discarded */
} }
return !rdec->reload_pending; return !rdec->reload_pending;
} }
@ -233,7 +234,7 @@ static inline int Rd_decode_tree6( struct Range_decoder * const rdec,
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] ); symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] ); symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] ); symbol = ( symbol << 1 ) | Rd_decode_bit( rdec, &bm[symbol] );
return symbol - (1 << 6); return symbol & 0x3F;
} }
static inline int Rd_decode_tree_reversed( struct Range_decoder * const rdec, static inline int Rd_decode_tree_reversed( struct Range_decoder * const rdec,
@ -255,9 +256,9 @@ static inline int Rd_decode_tree_reversed4( struct Range_decoder * const rdec,
Bit_model bm[] ) Bit_model bm[] )
{ {
int model = 1; int model = 1;
int symbol = 0; int symbol = Rd_decode_bit( rdec, &bm[model] );
int bit = Rd_decode_bit( rdec, &bm[model] ); int bit;
model = (model << 1) + bit; symbol |= bit; model = (model << 1) + symbol;
bit = Rd_decode_bit( rdec, &bm[model] ); bit = Rd_decode_bit( rdec, &bm[model] );
model = (model << 1) + bit; symbol |= (bit << 1); model = (model << 1) + bit; symbol |= (bit << 1);
bit = Rd_decode_bit( rdec, &bm[model] ); bit = Rd_decode_bit( rdec, &bm[model] );
@ -271,8 +272,7 @@ static inline int Rd_decode_matched( struct Range_decoder * const rdec,
{ {
Bit_model * const bm1 = bm + 0x100; Bit_model * const bm1 = bm + 0x100;
int symbol = 1; int symbol = 1;
int i; while( symbol < 0x100 )
for( i = 7; i >= 0; --i )
{ {
int match_bit, bit; int match_bit, bit;
match_byte <<= 1; match_byte <<= 1;
@ -286,7 +286,7 @@ static inline int Rd_decode_matched( struct Range_decoder * const rdec,
break; break;
} }
} }
return symbol - 0x100; return symbol & 0xFF;
} }
static inline int Rd_decode_len( struct Range_decoder * const rdec, static inline int Rd_decode_len( struct Range_decoder * const rdec,
@ -309,7 +309,8 @@ struct LZ_decoder
{ {
struct Circular_buffer cb; struct Circular_buffer cb;
unsigned long long partial_data_pos; unsigned long long partial_data_pos;
int dictionary_size; struct Range_decoder * rdec;
unsigned dictionary_size;
uint32_t crc; uint32_t crc;
bool member_finished; bool member_finished;
bool verify_trailer_pending; bool verify_trailer_pending;
@ -330,101 +331,98 @@ struct LZ_decoder
Bit_model bm_dis[modeled_distances-end_dis_model]; Bit_model bm_dis[modeled_distances-end_dis_model];
Bit_model bm_align[dis_align_size]; Bit_model bm_align[dis_align_size];
struct Range_decoder * rdec;
struct Len_model match_len_model; struct Len_model match_len_model;
struct Len_model rep_len_model; struct Len_model rep_len_model;
}; };
static inline bool LZd_enough_free_bytes( const struct LZ_decoder * const decoder ) static inline bool LZd_enough_free_bytes( const struct LZ_decoder * const d )
{ return Cb_free_bytes( &decoder->cb ) >= lzd_min_free_bytes; } { return Cb_free_bytes( &d->cb ) >= lzd_min_free_bytes; }
static inline uint8_t LZd_get_prev_byte( const struct LZ_decoder * const decoder ) static inline uint8_t LZd_get_prev_byte( const struct LZ_decoder * const d )
{ {
const int i = const int i = ( ( d->cb.put > 0 ) ? d->cb.put : d->cb.buffer_size ) - 1;
( ( decoder->cb.put > 0 ) ? decoder->cb.put : decoder->cb.buffer_size ) - 1; return d->cb.buffer[i];
return decoder->cb.buffer[i];
} }
static inline uint8_t LZd_get_byte( const struct LZ_decoder * const decoder, static inline uint8_t LZd_get_byte( const struct LZ_decoder * const d,
const int distance ) const int distance )
{ {
int i = decoder->cb.put - distance - 1; int i = d->cb.put - distance - 1;
if( i < 0 ) i += decoder->cb.buffer_size; if( i < 0 ) i += d->cb.buffer_size;
return decoder->cb.buffer[i]; return d->cb.buffer[i];
} }
static inline void LZd_put_byte( struct LZ_decoder * const decoder, static inline void LZd_put_byte( struct LZ_decoder * const d, const uint8_t b )
const uint8_t b )
{ {
CRC32_update_byte( &decoder->crc, b ); CRC32_update_byte( &d->crc, b );
decoder->cb.buffer[decoder->cb.put] = b; d->cb.buffer[d->cb.put] = b;
if( ++decoder->cb.put >= decoder->cb.buffer_size ) if( ++d->cb.put >= d->cb.buffer_size )
{ decoder->partial_data_pos += decoder->cb.put; decoder->cb.put = 0; } { d->partial_data_pos += d->cb.put; d->cb.put = 0; }
} }
static inline void LZd_copy_block( struct LZ_decoder * const decoder, static inline void LZd_copy_block( struct LZ_decoder * const d,
const int distance, int len ) const int distance, int len )
{ {
int i = decoder->cb.put - distance - 1; int i = d->cb.put - distance - 1;
if( i < 0 ) i += decoder->cb.buffer_size; if( i < 0 ) i += d->cb.buffer_size;
if( len < decoder->cb.buffer_size - max( decoder->cb.put, i ) && if( len < d->cb.buffer_size - max( d->cb.put, i ) &&
len <= abs( decoder->cb.put - i ) ) /* no wrap, no overlap */ len <= abs( d->cb.put - i ) ) /* no wrap, no overlap */
{ {
CRC32_update_buf( &decoder->crc, decoder->cb.buffer + i, len ); CRC32_update_buf( &d->crc, d->cb.buffer + i, len );
memcpy( decoder->cb.buffer + decoder->cb.put, decoder->cb.buffer + i, len ); memcpy( d->cb.buffer + d->cb.put, d->cb.buffer + i, len );
decoder->cb.put += len; d->cb.put += len;
} }
else for( ; len > 0; --len ) else for( ; len > 0; --len )
{ {
LZd_put_byte( decoder, decoder->cb.buffer[i] ); LZd_put_byte( d, d->cb.buffer[i] );
if( ++i >= decoder->cb.buffer_size ) i = 0; if( ++i >= d->cb.buffer_size ) i = 0;
} }
} }
static inline bool LZd_init( struct LZ_decoder * const decoder, static inline bool LZd_init( struct LZ_decoder * const d,
const File_header header, struct Range_decoder * const rde,
struct Range_decoder * const rde ) const int dict_size )
{ {
decoder->dictionary_size = Fh_get_dictionary_size( header ); if( !Cb_init( &d->cb, max( 65536, dict_size ) + lzd_min_free_bytes ) )
if( !Cb_init( &decoder->cb, max( 65536, decoder->dictionary_size ) + lzd_min_free_bytes ) )
return false; return false;
decoder->partial_data_pos = 0; d->partial_data_pos = 0;
decoder->crc = 0xFFFFFFFFU; d->rdec = rde;
decoder->member_finished = false; d->dictionary_size = dict_size;
decoder->verify_trailer_pending = false; d->crc = 0xFFFFFFFFU;
decoder->rep0 = 0; d->member_finished = false;
decoder->rep1 = 0; d->verify_trailer_pending = false;
decoder->rep2 = 0; d->rep0 = 0;
decoder->rep3 = 0; d->rep1 = 0;
decoder->state = 0; d->rep2 = 0;
d->rep3 = 0;
d->state = 0;
Bm_array_init( decoder->bm_literal[0], (1 << literal_context_bits) * 0x300 ); Bm_array_init( d->bm_literal[0], (1 << literal_context_bits) * 0x300 );
Bm_array_init( decoder->bm_match[0], states * pos_states ); Bm_array_init( d->bm_match[0], states * pos_states );
Bm_array_init( decoder->bm_rep, states ); Bm_array_init( d->bm_rep, states );
Bm_array_init( decoder->bm_rep0, states ); Bm_array_init( d->bm_rep0, states );
Bm_array_init( decoder->bm_rep1, states ); Bm_array_init( d->bm_rep1, states );
Bm_array_init( decoder->bm_rep2, states ); Bm_array_init( d->bm_rep2, states );
Bm_array_init( decoder->bm_len[0], states * pos_states ); Bm_array_init( d->bm_len[0], states * pos_states );
Bm_array_init( decoder->bm_dis_slot[0], len_states * (1 << dis_slot_bits) ); Bm_array_init( d->bm_dis_slot[0], len_states * (1 << dis_slot_bits) );
Bm_array_init( decoder->bm_dis, modeled_distances - end_dis_model ); Bm_array_init( d->bm_dis, modeled_distances - end_dis_model );
Bm_array_init( decoder->bm_align, dis_align_size ); Bm_array_init( d->bm_align, dis_align_size );
decoder->rdec = rde; Lm_init( &d->match_len_model );
Lm_init( &decoder->match_len_model ); Lm_init( &d->rep_len_model );
Lm_init( &decoder->rep_len_model ); d->cb.buffer[d->cb.buffer_size-1] = 0; /* prev_byte of first_byte */
decoder->cb.buffer[decoder->cb.buffer_size-1] = 0; /* prev_byte of first_byte */
return true; return true;
} }
static inline void LZd_free( struct LZ_decoder * const decoder ) static inline void LZd_free( struct LZ_decoder * const d )
{ Cb_free( &decoder->cb ); } { Cb_free( &d->cb ); }
static inline bool LZd_member_finished( const struct LZ_decoder * const decoder ) static inline bool LZd_member_finished( const struct LZ_decoder * const d )
{ return ( decoder->member_finished && !Cb_used_bytes( &decoder->cb ) ); } { return ( d->member_finished && !Cb_used_bytes( &d->cb ) ); }
static inline unsigned LZd_crc( const struct LZ_decoder * const decoder ) static inline unsigned LZd_crc( const struct LZ_decoder * const d )
{ return decoder->crc ^ 0xFFFFFFFFU; } { return d->crc ^ 0xFFFFFFFFU; }
static inline unsigned long long static inline unsigned long long
LZd_data_position( const struct LZ_decoder * const decoder ) LZd_data_position( const struct LZ_decoder * const d )
{ return decoder->partial_data_pos + decoder->cb.put; } { return d->partial_data_pos + d->cb.put; }

View file

@ -1,9 +1,8 @@
This is lzlib.info, produced by makeinfo version 4.13+ from This is lzlib.info, produced by makeinfo version 4.13+ from lzlib.texi.
lzlib.texinfo.
INFO-DIR-SECTION Data Compression INFO-DIR-SECTION Data Compression
START-INFO-DIR-ENTRY START-INFO-DIR-ENTRY
* Lzlib: (lzlib). Compression library for lzip files * Lzlib: (lzlib). Compression library for the lzip format
END-INFO-DIR-ENTRY END-INFO-DIR-ENTRY
 
@ -12,7 +11,7 @@ File: lzlib.info, Node: Top, Next: Introduction, Up: (dir)
Lzlib Manual Lzlib Manual
************ ************
This manual is for Lzlib (version 1.6-pre1, 7 October 2013). This manual is for Lzlib (version 1.6-pre2, 30 January 2014).
* Menu: * Menu:
@ -30,7 +29,7 @@ This manual is for Lzlib (version 1.6-pre1, 7 October 2013).
* Concept index:: Index of concepts * Concept index:: Index of concepts
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This manual is free documentation: you have unlimited permission to This manual is free documentation: you have unlimited permission to
copy, distribute and modify it. copy, distribute and modify it.
@ -736,19 +735,19 @@ Concept index
 
Tag Table: Tag Table:
Node: Top218 Node: Top220
Node: Introduction1326 Node: Introduction1335
Node: Library version3907 Node: Library version3916
Node: Buffering4552 Node: Buffering4561
Node: Parameter limits5675 Node: Parameter limits5684
Node: Compression functions6634 Node: Compression functions6643
Node: Decompression functions12847 Node: Decompression functions12856
Node: Error codes19008 Node: Error codes19017
Node: Error messages20947 Node: Error messages20956
Node: Data format21526 Node: Data format21535
Node: Examples24175 Node: Examples24184
Node: Problems28258 Node: Problems28267
Node: Concept index28830 Node: Concept index28839
 
End Tag Table End Tag Table

View file

@ -6,19 +6,19 @@
@finalout @finalout
@c %**end of header @c %**end of header
@set UPDATED 7 October 2013 @set UPDATED 30 January 2014
@set VERSION 1.6-pre1 @set VERSION 1.6-pre2
@dircategory Data Compression @dircategory Data Compression
@direntry @direntry
* Lzlib: (lzlib). Compression library for lzip files * Lzlib: (lzlib). Compression library for the lzip format
@end direntry @end direntry
@ifnothtml @ifnothtml
@titlepage @titlepage
@title Lzlib @title Lzlib
@subtitle Compression library for lzip files @subtitle Compression library for the lzip format
@subtitle for Lzlib version @value{VERSION}, @value{UPDATED} @subtitle for Lzlib version @value{VERSION}, @value{UPDATED}
@author by Antonio Diaz Diaz @author by Antonio Diaz Diaz
@ -50,7 +50,8 @@ This manual is for Lzlib (version @value{VERSION}, @value{UPDATED}).
@end menu @end menu
@sp 1 @sp 1
Copyright @copyright{} 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright @copyright{} 2009, 2010, 2011, 2012, 2013, 2014
Antonio Diaz Diaz.
This manual is free documentation: you have unlimited permission This manual is free documentation: you have unlimited permission
to copy, distribute and modify it. to copy, distribute and modify it.
@ -625,8 +626,9 @@ Example: 0xD3 = 2^19 - 6 * 2^15 = 512 KiB - 6 * 32 KiB = 320 KiB@*
Valid values for dictionary size range from 4 KiB to 512 MiB. Valid values for dictionary size range from 4 KiB to 512 MiB.
@item Lzma stream @item Lzma stream
The lzma stream, finished by an end of stream marker. Uses default values The lzma stream, finished by an end of stream marker. Uses default
for encoder properties. See the lzip manual for a full description.@* values for encoder properties. See the lzip manual for a full
description.@*
Lzip only uses the LZMA marker @samp{2} ("End Of Stream" marker). Lzlib Lzip only uses the LZMA marker @samp{2} ("End Of Stream" marker). Lzlib
also uses the LZMA marker @samp{3} ("Sync Flush" marker). also uses the LZMA marker @samp{3} ("Sync Flush" marker).

View file

@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.37.1. .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.37.1.
.TH MINILZIP "1" "October 2013" "Minilzip 1.6-pre1" "User Commands" .TH MINILZIP "1" "January 2014" "Minilzip 1.6-pre2" "User Commands"
.SH NAME .SH NAME
Minilzip \- reduces the size of files Minilzip \- reduces the size of files
.SH SYNOPSIS .SH SYNOPSIS
@ -82,8 +82,8 @@ Report bugs to lzip\-bug@nongnu.org
.br .br
Lzlib home page: http://www.nongnu.org/lzip/lzlib.html Lzlib home page: http://www.nongnu.org/lzip/lzlib.html
.SH COPYRIGHT .SH COPYRIGHT
Copyright \(co 2013 Antonio Diaz Diaz. Copyright \(co 2014 Antonio Diaz Diaz.
Using Lzlib 1.6\-pre1 Using Lzlib 1.6\-pre2
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
.br .br
This is free software: you are free to change and redistribute it. This is free software: you are free to change and redistribute it.

603
encoder.c

File diff suppressed because it is too large Load diff

262
encoder.h
View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -96,7 +96,7 @@ static const uint8_t dis_slots[1<<10] =
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 }; 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 };
static inline uint8_t get_slot( const uint32_t dis ) static inline uint8_t get_slot( const unsigned dis )
{ {
if( dis < (1 << 10) ) return dis_slots[dis]; if( dis < (1 << 10) ) return dis_slots[dis];
if( dis < (1 << 19) ) return dis_slots[dis>> 9] + 18; if( dis < (1 << 19) ) return dis_slots[dis>> 9] + 18;
@ -186,15 +186,15 @@ static inline int price_symbol_reversed( const Bit_model bm[], int symbol,
} }
static inline int price_matched( const Bit_model bm[], unsigned symbol, static inline int price_matched( const Bit_model bm[], int symbol,
unsigned match_byte ) int match_byte )
{ {
int price = 0; int price = 0;
unsigned mask = 0x100; int mask = 0x100;
symbol |= 0x100; symbol |= mask;
do { do {
unsigned bit, match_bit; int match_bit, bit;
match_byte <<= 1; match_byte <<= 1;
match_bit = match_byte & mask; match_bit = match_byte & mask;
symbol <<= 1; symbol <<= 1;
@ -225,17 +225,17 @@ struct Matchfinder
{ {
unsigned long long partial_data_pos; unsigned long long partial_data_pos;
uint8_t * buffer; /* input buffer */ uint8_t * buffer; /* input buffer */
int32_t * prev_positions; /* last seen position of key */ int32_t * prev_positions; /* 1 + last seen position of key. else 0 */
int32_t * prev_pos_tree; /* previous positions of key */ int32_t * prev_pos_tree; /* previous positions of key */
int match_len_limit; int match_len_limit;
int buffer_size; int buffer_size;
int dictionary_size; /* bytes to keep in buffer before pos */ int dictionary_size; /* bytes to keep in buffer before pos */
int pos; /* current pos in buffer */ int pos; /* current pos in buffer */
int cyclic_pos; /* cycles through [0, dictionary_size] */ int cyclic_pos; /* cycles through [0, dictionary_size] */
int pos_limit; /* when reached, a new block must be read */
int stream_pos; /* first byte not yet read from file */ int stream_pos; /* first byte not yet read from file */
int pos_limit; /* when reached, a new block must be read */
int cycles; int cycles;
unsigned key4_mask; int key4_mask;
int num_prev_positions; /* size of prev_positions */ int num_prev_positions; /* size of prev_positions */
bool at_stream_end; /* stream_pos shows real end of file */ bool at_stream_end; /* stream_pos shows real end of file */
bool been_flushed; bool been_flushed;
@ -262,8 +262,9 @@ static inline void Mf_free( struct Matchfinder * const mf )
free( mf->buffer ); free( mf->buffer );
} }
static inline uint8_t Mf_peek( const struct Matchfinder * const mf, const int i ) static inline uint8_t Mf_peek( const struct Matchfinder * const mf,
{ return mf->buffer[mf->pos+i]; } const int distance )
{ return mf->buffer[mf->pos-distance]; }
static inline int Mf_available_bytes( const struct Matchfinder * const mf ) static inline int Mf_available_bytes( const struct Matchfinder * const mf )
{ return mf->stream_pos - mf->pos; } { return mf->stream_pos - mf->pos; }
@ -339,7 +340,7 @@ struct Range_encoder
uint64_t low; uint64_t low;
unsigned long long partial_member_pos; unsigned long long partial_member_pos;
uint32_t range; uint32_t range;
int ff_count; unsigned ff_count;
uint8_t cache; uint8_t cache;
}; };
@ -457,14 +458,14 @@ static inline void Re_encode_tree_reversed( struct Range_encoder * const renc,
} }
static inline void Re_encode_matched( struct Range_encoder * const renc, static inline void Re_encode_matched( struct Range_encoder * const renc,
Bit_model bm[], unsigned symbol, Bit_model bm[], int symbol,
unsigned match_byte ) int match_byte )
{ {
unsigned mask = 0x100; int mask = 0x100;
symbol |= 0x100; symbol |= mask;
do { do {
unsigned bit, match_bit; int match_bit, bit;
match_byte <<= 1; match_byte <<= 1;
match_bit = match_byte & mask; match_bit = match_byte & mask;
symbol <<= 1; symbol <<= 1;
@ -484,44 +485,43 @@ struct Len_encoder
int counters[pos_states]; int counters[pos_states];
}; };
static void Lee_update_prices( struct Len_encoder * const len_encoder, static void Lee_update_prices( struct Len_encoder * const le,
const int pos_state ) const int pos_state )
{ {
int * const pps = len_encoder->prices[pos_state]; int * const pps = le->prices[pos_state];
int tmp = price0( len_encoder->lm.choice1 ); int tmp = price0( le->lm.choice1 );
int len = 0; int len = 0;
for( ; len < len_low_symbols && len < len_encoder->len_symbols; ++len ) for( ; len < len_low_symbols && len < le->len_symbols; ++len )
pps[len] = tmp + pps[len] = tmp + price_symbol( le->lm.bm_low[pos_state], len, len_low_bits );
price_symbol( len_encoder->lm.bm_low[pos_state], len, len_low_bits ); tmp = price1( le->lm.choice1 );
tmp = price1( len_encoder->lm.choice1 ); for( ; len < len_low_symbols + len_mid_symbols && len < le->len_symbols; ++len )
for( ; len < len_low_symbols + len_mid_symbols && len < len_encoder->len_symbols; ++len ) pps[len] = tmp + price0( le->lm.choice2 ) +
pps[len] = tmp + price0( len_encoder->lm.choice2 ) + price_symbol( le->lm.bm_mid[pos_state], len - len_low_symbols, len_mid_bits );
price_symbol( len_encoder->lm.bm_mid[pos_state], len - len_low_symbols, len_mid_bits ); for( ; len < le->len_symbols; ++len )
for( ; len < len_encoder->len_symbols; ++len )
/* using 4 slots per value makes "Lee_price" faster */ /* using 4 slots per value makes "Lee_price" faster */
len_encoder->prices[3][len] = len_encoder->prices[2][len] = le->prices[3][len] = le->prices[2][len] =
len_encoder->prices[1][len] = len_encoder->prices[0][len] = le->prices[1][len] = le->prices[0][len] =
tmp + price1( len_encoder->lm.choice2 ) + tmp + price1( le->lm.choice2 ) +
price_symbol( len_encoder->lm.bm_high, len - len_low_symbols - len_mid_symbols, len_high_bits ); price_symbol( le->lm.bm_high, len - len_low_symbols - len_mid_symbols, len_high_bits );
len_encoder->counters[pos_state] = len_encoder->len_symbols; le->counters[pos_state] = le->len_symbols;
} }
static void Lee_init( struct Len_encoder * const len_encoder, static void Lee_init( struct Len_encoder * const le,
const int match_len_limit ) const int match_len_limit )
{ {
int i; int i;
Lm_init( &len_encoder->lm ); Lm_init( &le->lm );
len_encoder->len_symbols = match_len_limit + 1 - min_match_len; le->len_symbols = match_len_limit + 1 - min_match_len;
for( i = 0; i < pos_states; ++i ) Lee_update_prices( len_encoder, i ); for( i = 0; i < pos_states; ++i ) Lee_update_prices( le, i );
} }
static void Lee_encode( struct Len_encoder * const len_encoder, static void Lee_encode( struct Len_encoder * const le,
struct Range_encoder * const renc, struct Range_encoder * const renc,
int symbol, const int pos_state ); int symbol, const int pos_state );
static inline int Lee_price( const struct Len_encoder * const len_encoder, static inline int Lee_price( const struct Len_encoder * const le,
const int symbol, const int pos_state ) const int symbol, const int pos_state )
{ return len_encoder->prices[pos_state][symbol - min_match_len]; } { return le->prices[pos_state][symbol - min_match_len]; }
enum { infinite_price = 0x0FFFFFFF, enum { infinite_price = 0x0FFFFFFF,
@ -534,46 +534,42 @@ struct Trial
{ {
State state; State state;
int price; /* dual use var; cumulative price, match length */ int price; /* dual use var; cumulative price, match length */
int dis; /* rep index or match distance */ int dis; /* rep index or match distance. (-1 for literal) */
int prev_index; /* index of prev trial in trials[] */ int prev_index; /* index of prev trial in trials[] */
int dis2;
int prev_index2; /* -2 trial is single step */ int prev_index2; /* -2 trial is single step */
/* -1 literal + rep0 */ /* -1 literal + rep0 */
/* >= 0 rep or match + literal + rep0 */ /* >= 0 ( rep or match ) + literal + rep0 */
int reps[num_rep_distances]; int reps[num_rep_distances];
}; };
static inline void Tr_update( struct Trial * const trial, const int pr, static inline void Tr_update( struct Trial * const trial, const int pr,
const int d, const int p_i ) const int distance, const int p_i )
{ {
if( pr < trial->price ) if( pr < trial->price )
{ {
trial->price = pr; trial->price = pr; trial->dis = distance; trial->prev_index = p_i;
trial->dis = d; trial->prev_index = p_i;
trial->prev_index2 = single_step_trial; trial->prev_index2 = single_step_trial;
} }
} }
static inline void Tr_update2( struct Trial * const trial, const int pr, static inline void Tr_update2( struct Trial * const trial, const int pr,
const int d, const int p_i ) const int p_i )
{ {
if( pr < trial->price ) if( pr < trial->price )
{ {
trial->price = pr; trial->price = pr; trial->dis = 0; trial->prev_index = p_i;
trial->dis = d; trial->prev_index = p_i;
trial->prev_index2 = dual_step_trial; trial->prev_index2 = dual_step_trial;
} }
} }
static inline void Tr_update3( struct Trial * const trial, const int pr, static inline void Tr_update3( struct Trial * const trial, const int pr,
const int d, const int p_i, const int distance, const int p_i,
const int d2, const int p_i2 ) const int p_i2 )
{ {
if( pr < trial->price ) if( pr < trial->price )
{ {
trial->price = pr; trial->price = pr; trial->dis = distance; trial->prev_index = p_i;
trial->dis = d; trial->prev_index = p_i; trial->prev_index2 = p_i2;
trial->dis2 = d2; trial->prev_index2 = p_i2;
} }
} }
@ -600,35 +596,35 @@ struct LZ_encoder
struct Len_encoder match_len_encoder; struct Len_encoder match_len_encoder;
struct Len_encoder rep_len_encoder; struct Len_encoder rep_len_encoder;
int num_dis_slots;
int rep_distances[num_rep_distances];
struct Pair pairs[max_match_len+1]; struct Pair pairs[max_match_len+1];
struct Trial trials[max_num_trials]; struct Trial trials[max_num_trials];
int reps[num_rep_distances];
int dis_slot_prices[len_states][2*max_dictionary_bits]; int dis_slot_prices[len_states][2*max_dictionary_bits];
int dis_prices[len_states][modeled_distances]; int dis_prices[len_states][modeled_distances];
int align_prices[dis_align_size]; int align_prices[dis_align_size];
int align_price_count; int align_price_count;
int num_dis_slots;
int fill_counter; int fill_counter;
State state; State state;
bool member_finished; bool member_finished;
}; };
static inline bool LZe_member_finished( const struct LZ_encoder * const encoder ) static inline bool LZe_member_finished( const struct LZ_encoder * const e )
{ {
return ( encoder->member_finished && !Cb_used_bytes( &encoder->renc.cb ) ); return ( e->member_finished && !Cb_used_bytes( &e->renc.cb ) );
} }
static inline void LZe_free( struct LZ_encoder * const encoder ) static inline void LZe_free( struct LZ_encoder * const e )
{ Re_free( &encoder->renc ); } { Re_free( &e->renc ); }
static inline unsigned LZe_crc( const struct LZ_encoder * const encoder ) static inline unsigned LZe_crc( const struct LZ_encoder * const e )
{ return encoder->crc ^ 0xFFFFFFFFU; } { return e->crc ^ 0xFFFFFFFFU; }
/* move-to-front dis in/into reps */ /* move-to-front dis in/into reps if( dis > 0 ) */
static inline void LZe_mtf_reps( const int dis, int reps[num_rep_distances] ) static inline void mtf_reps( const int dis, int reps[num_rep_distances] )
{ {
int i; int i;
if( dis >= num_rep_distances ) if( dis >= num_rep_distances )
@ -644,156 +640,146 @@ static inline void LZe_mtf_reps( const int dis, int reps[num_rep_distances] )
} }
} }
static inline int LZe_price_rep_len1( const struct LZ_encoder * const encoder, static inline int LZe_price_shortrep( const struct LZ_encoder * const e,
const State state, const int pos_state ) const State state, const int pos_state )
{ {
return price0( encoder->bm_rep0[state] ) + return price0( e->bm_rep0[state] ) + price0( e->bm_len[state][pos_state] );
price0( encoder->bm_len[state][pos_state] );
} }
static inline int LZe_price_rep( const struct LZ_encoder * const encoder, static inline int LZe_price_rep( const struct LZ_encoder * const e,
const int rep, const int rep,
const State state, const int pos_state ) const State state, const int pos_state )
{ {
int price; int price;
if( rep == 0 ) return price0( encoder->bm_rep0[state] ) + if( rep == 0 ) return price0( e->bm_rep0[state] ) +
price1( encoder->bm_len[state][pos_state] ); price1( e->bm_len[state][pos_state] );
price = price1( encoder->bm_rep0[state] ); price = price1( e->bm_rep0[state] );
if( rep == 1 ) if( rep == 1 )
price += price0( encoder->bm_rep1[state] ); price += price0( e->bm_rep1[state] );
else else
{ {
price += price1( encoder->bm_rep1[state] ); price += price1( e->bm_rep1[state] );
price += price_bit( encoder->bm_rep2[state], rep - 2 ); price += price_bit( e->bm_rep2[state], rep - 2 );
} }
return price; return price;
} }
static inline int LZe_price_rep0_len( const struct LZ_encoder * const encoder, static inline int LZe_price_rep0_len( const struct LZ_encoder * const e,
const int len, const int len,
const State state, const int pos_state ) const State state, const int pos_state )
{ {
return LZe_price_rep( encoder, 0, state, pos_state ) + return LZe_price_rep( e, 0, state, pos_state ) +
Lee_price( &encoder->rep_len_encoder, len, pos_state ); Lee_price( &e->rep_len_encoder, len, pos_state );
} }
static inline int LZe_price_dis( const struct LZ_encoder * const encoder, static inline int LZe_price_pair( const struct LZ_encoder * const e,
const int dis, const int len_state )
{
if( dis < modeled_distances )
return encoder->dis_prices[len_state][dis];
else
return encoder->dis_slot_prices[len_state][get_slot( dis )] +
encoder->align_prices[dis & (dis_align_size - 1)];
}
static inline int LZe_price_pair( const struct LZ_encoder * const encoder,
const int dis, const int len, const int dis, const int len,
const int pos_state ) const int pos_state )
{ {
return Lee_price( &encoder->match_len_encoder, len, pos_state ) + const int price = Lee_price( &e->match_len_encoder, len, pos_state );
LZe_price_dis( encoder, dis, get_len_state( len ) ); const int len_state = get_len_state( len );
if( dis < modeled_distances )
return price + e->dis_prices[len_state][dis];
else
return price + e->dis_slot_prices[len_state][get_slot( dis )] +
e->align_prices[dis & (dis_align_size - 1)];
} }
static inline int LZe_price_literal( const struct LZ_encoder * const encoder, static inline int LZe_price_literal( const struct LZ_encoder * const e,
uint8_t prev_byte, uint8_t symbol ) uint8_t prev_byte, uint8_t symbol )
{ return price_symbol( encoder->bm_literal[get_lit_state(prev_byte)], symbol, 8 ); } { return price_symbol( e->bm_literal[get_lit_state(prev_byte)], symbol, 8 ); }
static inline int LZe_price_matched( const struct LZ_encoder * const encoder, static inline int LZe_price_matched( const struct LZ_encoder * const e,
uint8_t prev_byte, uint8_t symbol, uint8_t prev_byte, uint8_t symbol,
uint8_t match_byte ) uint8_t match_byte )
{ return price_matched( encoder->bm_literal[get_lit_state(prev_byte)], { return price_matched( e->bm_literal[get_lit_state(prev_byte)], symbol,
symbol, match_byte ); } match_byte ); }
static inline void LZe_encode_literal( struct LZ_encoder * const encoder, static inline void LZe_encode_literal( struct LZ_encoder * const e,
uint8_t prev_byte, uint8_t symbol ) uint8_t prev_byte, uint8_t symbol )
{ Re_encode_tree( &encoder->renc, { Re_encode_tree( &e->renc,
encoder->bm_literal[get_lit_state(prev_byte)], symbol, 8 ); } e->bm_literal[get_lit_state(prev_byte)], symbol, 8 ); }
static inline void LZe_encode_matched( struct LZ_encoder * const encoder, static inline void LZe_encode_matched( struct LZ_encoder * const e,
uint8_t prev_byte, uint8_t symbol, uint8_t prev_byte, uint8_t symbol,
uint8_t match_byte ) uint8_t match_byte )
{ Re_encode_matched( &encoder->renc, { Re_encode_matched( &e->renc, e->bm_literal[get_lit_state(prev_byte)],
encoder->bm_literal[get_lit_state(prev_byte)],
symbol, match_byte ); } symbol, match_byte ); }
static inline void LZe_encode_pair( struct LZ_encoder * const encoder, static inline void LZe_encode_pair( struct LZ_encoder * const e,
const uint32_t dis, const int len, const unsigned dis, const int len,
const int pos_state ) const int pos_state )
{ {
const int dis_slot = get_slot( dis ); const int dis_slot = get_slot( dis );
Lee_encode( &encoder->match_len_encoder, &encoder->renc, len, pos_state ); Lee_encode( &e->match_len_encoder, &e->renc, len, pos_state );
Re_encode_tree( &encoder->renc, encoder->bm_dis_slot[get_len_state(len)], Re_encode_tree( &e->renc, e->bm_dis_slot[get_len_state(len)], dis_slot,
dis_slot, dis_slot_bits ); dis_slot_bits );
if( dis_slot >= start_dis_model ) if( dis_slot >= start_dis_model )
{ {
const int direct_bits = ( dis_slot >> 1 ) - 1; const int direct_bits = ( dis_slot >> 1 ) - 1;
const uint32_t base = ( 2 | ( dis_slot & 1 ) ) << direct_bits; const unsigned base = ( 2 | ( dis_slot & 1 ) ) << direct_bits;
const uint32_t direct_dis = dis - base; const unsigned direct_dis = dis - base;
if( dis_slot < end_dis_model ) if( dis_slot < end_dis_model )
Re_encode_tree_reversed( &encoder->renc, Re_encode_tree_reversed( &e->renc, e->bm_dis + base - dis_slot - 1,
encoder->bm_dis + base - dis_slot - 1,
direct_dis, direct_bits ); direct_dis, direct_bits );
else else
{ {
Re_encode( &encoder->renc, direct_dis >> dis_align_bits, Re_encode( &e->renc, direct_dis >> dis_align_bits,
direct_bits - dis_align_bits ); direct_bits - dis_align_bits );
Re_encode_tree_reversed( &encoder->renc, encoder->bm_align, Re_encode_tree_reversed( &e->renc, e->bm_align, direct_dis, dis_align_bits );
direct_dis, dis_align_bits ); --e->align_price_count;
--encoder->align_price_count;
} }
} }
} }
static inline int LZe_read_match_distances( struct LZ_encoder * const encoder ) static inline int LZe_read_match_distances( struct LZ_encoder * const e )
{ {
const int num_pairs = const int num_pairs = Mf_get_match_pairs( e->matchfinder, e->pairs );
Mf_get_match_pairs( encoder->matchfinder, encoder->pairs );
if( num_pairs > 0 ) if( num_pairs > 0 )
{ {
int len = encoder->pairs[num_pairs-1].len; int len = e->pairs[num_pairs-1].len;
if( len == encoder->matchfinder->match_len_limit && len < max_match_len ) if( len == e->matchfinder->match_len_limit && len < max_match_len )
{ {
len += Mf_true_match_len( encoder->matchfinder, len, len += Mf_true_match_len( e->matchfinder, len,
encoder->pairs[num_pairs-1].dis + 1, e->pairs[num_pairs-1].dis + 1,
max_match_len - len ); max_match_len - len );
encoder->pairs[num_pairs-1].len = len; e->pairs[num_pairs-1].len = len;
} }
} }
return num_pairs; return num_pairs;
} }
static inline bool LZe_move_pos( struct LZ_encoder * const encoder, int n ) static inline bool LZe_move_pos( struct LZ_encoder * const e, int n )
{ {
if( --n >= 0 && !Mf_move_pos( encoder->matchfinder ) ) return false; while( true )
while( --n >= 0 )
{ {
Mf_get_match_pairs( encoder->matchfinder, 0 ); if( !Mf_move_pos( e->matchfinder ) ) return false;
if( !Mf_move_pos( encoder->matchfinder ) ) return false; if( --n <= 0 ) break;
Mf_get_match_pairs( e->matchfinder, 0 );
} }
return true; return true;
} }
static inline void LZe_backward( struct LZ_encoder * const encoder, int cur ) static inline void LZe_backward( struct LZ_encoder * const e, int cur )
{ {
int * const dis = &encoder->trials[cur].dis; int * const dis = &e->trials[cur].dis;
while( cur > 0 ) while( cur > 0 )
{ {
const int prev_index = encoder->trials[cur].prev_index; const int prev_index = e->trials[cur].prev_index;
struct Trial * const prev_trial = &encoder->trials[prev_index]; struct Trial * const prev_trial = &e->trials[prev_index];
if( encoder->trials[cur].prev_index2 != single_step_trial ) if( e->trials[cur].prev_index2 != single_step_trial )
{ {
prev_trial->dis = -1; prev_trial->dis = -1;
prev_trial->prev_index = prev_index - 1; prev_trial->prev_index = prev_index - 1;
prev_trial->prev_index2 = single_step_trial; prev_trial->prev_index2 = single_step_trial;
if( encoder->trials[cur].prev_index2 >= 0 ) if( e->trials[cur].prev_index2 >= 0 )
{ {
struct Trial * const prev_trial2 = &encoder->trials[prev_index-1]; struct Trial * const prev_trial2 = &e->trials[prev_index-1];
prev_trial2->dis = encoder->trials[cur].dis2; prev_trial2->dis = *dis; *dis = 0;
prev_trial2->prev_index = encoder->trials[cur].prev_index2; prev_trial2->prev_index = e->trials[cur].prev_index2;
prev_trial2->prev_index2 = single_step_trial; prev_trial2->prev_index2 = single_step_trial;
} }
} }

View file

@ -1,5 +1,5 @@
/* Lzcheck - Test program for the lzlib library /* Lzcheck - Test program for the lzlib library
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This program is free software: you have unlimited permission This program is free software: you have unlimited permission
to copy, distribute and modify it. to copy, distribute and modify it.

31
lzip.h
View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -56,7 +56,7 @@ static inline State St_set_short_rep( const State st )
enum { enum {
min_dictionary_bits = 12, min_dictionary_bits = 12,
min_dictionary_size = 1 << min_dictionary_bits, min_dictionary_size = 1 << min_dictionary_bits, /* >= modeled_distances */
max_dictionary_bits = 29, max_dictionary_bits = 29,
max_dictionary_size = 1 << max_dictionary_bits, max_dictionary_size = 1 << max_dictionary_bits,
literal_context_bits = 3, literal_context_bits = 3,
@ -174,7 +174,8 @@ static inline void CRC32_update_byte( uint32_t * const crc, const uint8_t byte )
{ *crc = crc32[(*crc^byte)&0xFF] ^ ( *crc >> 8 ); } { *crc = crc32[(*crc^byte)&0xFF] ^ ( *crc >> 8 ); }
static inline void CRC32_update_buf( uint32_t * const crc, static inline void CRC32_update_buf( uint32_t * const crc,
const uint8_t * const buffer, const int size ) const uint8_t * const buffer,
const int size )
{ {
int i; int i;
for( i = 0; i < size; ++i ) for( i = 0; i < size; ++i )
@ -254,41 +255,29 @@ enum { Ft_size = 20 };
static inline unsigned Ft_get_data_crc( const File_trailer data ) static inline unsigned Ft_get_data_crc( const File_trailer data )
{ {
unsigned tmp = 0; unsigned tmp = 0;
int i; int i; for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
for( i = 3; i >= 0; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp; return tmp;
} }
static inline void Ft_set_data_crc( File_trailer data, unsigned crc ) static inline void Ft_set_data_crc( File_trailer data, unsigned crc )
{ { int i; for( i = 0; i <= 3; ++i ) { data[i] = (uint8_t)crc; crc >>= 8; } }
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 Ft_get_data_size( const File_trailer data )
{ {
unsigned long long tmp = 0; unsigned long long tmp = 0;
int i; int i; for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
for( i = 11; i >= 4; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp; return tmp;
} }
static inline void Ft_set_data_size( File_trailer data, unsigned long long sz ) static inline void Ft_set_data_size( File_trailer data, unsigned long long sz )
{ { int i; for( i = 4; i <= 11; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
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 Ft_get_member_size( const File_trailer data )
{ {
unsigned long long tmp = 0; unsigned long long tmp = 0;
int i; int i; for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
for( i = 19; i >= 12; --i ) { tmp <<= 8; tmp += data[i]; }
return tmp; return tmp;
} }
static inline void Ft_set_member_size( File_trailer data, unsigned long long sz ) static inline void Ft_set_member_size( File_trailer data, unsigned long long sz )
{ { int i; for( i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
int i;
for( i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; }
}

View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -436,7 +436,8 @@ int LZ_decompress_read( struct LZ_Decoder * const d,
return -1; return -1;
} }
d->lz_decoder = (struct LZ_decoder *)malloc( sizeof (struct LZ_decoder) ); d->lz_decoder = (struct LZ_decoder *)malloc( sizeof (struct LZ_decoder) );
if( !d->lz_decoder || !LZd_init( d->lz_decoder, d->member_header, d->rdec ) ) if( !d->lz_decoder || !LZd_init( d->lz_decoder, d->rdec,
Fh_get_dictionary_size( d->member_header ) ) )
{ /* not enough free memory */ { /* not enough free memory */
if( d->lz_decoder ) if( d->lz_decoder )
{ LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; } { LZd_free( d->lz_decoder ); free( d->lz_decoder ); d->lz_decoder = 0; }

View file

@ -1,5 +1,5 @@
/* Lzlib - Compression library for lzip files /* Lzlib - Compression library for lzip files
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This library is free software: you can redistribute it and/or modify This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -29,7 +29,7 @@
extern "C" { extern "C" {
#endif #endif
static const char * const LZ_version_string = "1.6-pre1"; static const char * const LZ_version_string = "1.6-pre2";
enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error, enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error,
LZ_sequence_error, LZ_header_error, LZ_unexpected_eof, LZ_sequence_error, LZ_header_error, LZ_unexpected_eof,

91
main.c
View file

@ -1,5 +1,5 @@
/* Minilzip - Test program for the lzlib library /* Minilzip - Test program for the lzlib library
Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -76,7 +76,7 @@ void internal_error( const char * const msg );
const char * const Program_name = "Minilzip"; const char * const Program_name = "Minilzip";
const char * const program_name = "minilzip"; const char * const program_name = "minilzip";
const char * const program_year = "2013"; const char * const program_year = "2014";
const char * invocation_name = 0; const char * invocation_name = 0;
struct { const char * from; const char * to; } const known_extensions[] = { struct { const char * from; const char * to; } const known_extensions[] = {
@ -105,10 +105,30 @@ struct Pretty_print
{ {
const char * name; const char * name;
const char * stdin_name; const char * stdin_name;
int longest_name; unsigned longest_name;
bool first_post; bool first_post;
}; };
static void Pp_init( struct Pretty_print * const pp,
const char * const filenames[], const int num_filenames )
{
unsigned stdin_name_len;
int i;
pp->name = 0;
pp->stdin_name = "(stdin)";
pp->longest_name = 0;
pp->first_post = false;
stdin_name_len = strlen( pp->stdin_name );
for( i = 0; i < num_filenames; ++i )
{
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 == 0 ) pp->longest_name = stdin_name_len;
}
static inline void Pp_set_name( struct Pretty_print * const pp, static inline void Pp_set_name( struct Pretty_print * const pp,
const char * const filename ) const char * const filename )
{ {
@ -118,11 +138,9 @@ static inline void Pp_set_name( struct Pretty_print * const pp,
pp->first_post = true; pp->first_post = true;
} }
static inline void Pp_reset( struct Pretty_print * const pp ) static inline void Pp_reset( struct Pretty_print * const pp )
{ if( pp->name && pp->name[0] ) pp->first_post = true; } { if( pp->name && pp->name[0] ) pp->first_post = true; }
static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
@ -193,14 +211,14 @@ static void show_version( void )
} }
static void show_header( struct LZ_Decoder * const decoder ) static void show_header( const unsigned dictionary_size )
{ {
const char * const prefix[8] = const char * const prefix[8] =
{ "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
enum { factor = 1024 }; enum { factor = 1024 };
const char * p = ""; const char * p = "";
const char * np = " "; const char * np = " ";
unsigned num = LZ_decompress_dictionary_size( decoder ), i; unsigned num = dictionary_size, i;
bool exact = ( num % factor == 0 ); bool exact = ( num % factor == 0 );
for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i ) for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i )
@ -304,7 +322,8 @@ static int open_instream( const char * const name, struct stat * const in_statsp
} }
else else
{ {
infd = open( name, O_RDONLY | O_BINARY ); do infd = open( name, O_RDONLY | O_BINARY );
while( infd < 0 && errno == EINTR );
if( infd < 0 ) if( infd < 0 )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
@ -388,7 +407,8 @@ static bool open_outstream( const bool force )
int flags = O_CREAT | O_WRONLY | O_BINARY; int flags = O_CREAT | O_WRONLY | O_BINARY;
if( force ) flags |= O_TRUNC; else flags |= O_EXCL; if( force ) flags |= O_TRUNC; else flags |= O_EXCL;
outfd = open( output_filename, flags, outfd_mode ); do outfd = open( output_filename, flags, outfd_mode );
while( outfd < 0 && errno == EINTR );
if( outfd < 0 && verbosity >= 0 ) if( outfd < 0 && verbosity >= 0 )
{ {
if( errno == EEXIST ) if( errno == EEXIST )
@ -441,10 +461,14 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
bool warning = false; bool warning = false;
if( in_statsp ) if( in_statsp )
{ {
const mode_t mode = in_statsp->st_mode;
/* fchown will in many cases return with EPERM, which can be safely ignored. */ /* fchown will in many cases return with EPERM, which can be safely ignored. */
if( ( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) != 0 && if( fchown( outfd, in_statsp->st_uid, in_statsp->st_gid ) == 0 )
errno != EPERM ) || { if( fchmod( outfd, mode ) != 0 ) warning = true; }
fchmod( outfd, in_statsp->st_mode ) != 0 ) warning = true; else
if( errno != EPERM ||
fchmod( outfd, mode & ~( S_ISUID | S_ISGID | S_ISVTX ) ) != 0 )
warning = true;
} }
if( close( outfd ) != 0 ) cleanup_and_fail( 1 ); if( close( outfd ) != 0 ) cleanup_and_fail( 1 );
outfd = -1; outfd = -1;
@ -515,8 +539,8 @@ static bool next_filename( void )
static int do_compress( struct LZ_Encoder * const encoder, static int do_compress( struct LZ_Encoder * const encoder,
const unsigned long long member_size, const unsigned long long member_size,
const unsigned long long volume_size, const int infd, const unsigned long long volume_size,
struct Pretty_print * const pp, const int infd, struct Pretty_print * const pp,
const struct stat * const in_statsp ) const struct stat * const in_statsp )
{ {
unsigned long long partial_volume_size = 0; unsigned long long partial_volume_size = 0;
@ -688,7 +712,8 @@ int do_decompress( struct LZ_Decoder * const decoder, const int infd,
const unsigned long long data_position = LZ_decompress_data_position( decoder ); const unsigned long long data_position = LZ_decompress_data_position( decoder );
const unsigned long long member_size = LZ_decompress_member_position( decoder ); const unsigned long long member_size = LZ_decompress_member_position( decoder );
Pp_show_msg( pp, 0 ); Pp_show_msg( pp, 0 );
if( verbosity >= 3 ) show_header( decoder ); if( verbosity >= 3 )
show_header( LZ_decompress_dictionary_size( decoder ) );
if( verbosity >= 2 && data_position > 0 && member_size > 0 ) if( verbosity >= 2 && data_position > 0 && member_size > 0 )
fprintf( stderr, "%6.3f:1, %6.3f bits/byte, %5.2f%% saved. ", fprintf( stderr, "%6.3f:1, %6.3f bits/byte, %5.2f%% saved. ",
(double)data_position / member_size, (double)data_position / member_size,
@ -714,10 +739,7 @@ int do_decompress( struct LZ_Decoder * const decoder, const int infd,
return 2; return 2;
} }
if( lz_errno == LZ_mem_error ) if( lz_errno == LZ_mem_error )
{ { Pp_show_msg( pp, "Not enough memory" ); return 1; }
Pp_show_msg( pp, "Not enough memory. Find a machine with more memory" );
return 1;
}
if( verbosity >= 0 ) if( verbosity >= 0 )
{ {
Pp_show_msg( pp, 0 ); Pp_show_msg( pp, 0 );
@ -746,10 +768,7 @@ int decompress( const int infd, struct Pretty_print * const pp,
int retval; int retval;
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ { Pp_show_msg( pp, "Not enough memory" ); retval = 1; }
Pp_show_msg( pp, "Not enough memory. Find a machine with more memory" );
retval = 1;
}
else retval = do_decompress( decoder, infd, pp, testing ); else retval = do_decompress( decoder, infd, pp, testing );
LZ_decompress_close( decoder ); LZ_decompress_close( decoder );
@ -773,27 +792,6 @@ static void set_signals( void )
} }
static void Pp_init( struct Pretty_print * const pp,
const char * const filenames[], const int num_filenames )
{
unsigned stdin_name_len;
int i;
pp->name = 0;
pp->stdin_name = "(stdin)";
pp->longest_name = 0;
pp->first_post = false;
stdin_name_len = strlen( pp->stdin_name );
for( i = 0; i < num_filenames; ++i )
{
const char * const s = filenames[i];
const int len = ( (strcmp( s, "-" ) == 0) ? stdin_name_len : strlen( s ) );
if( len > pp->longest_name ) pp->longest_name = len;
}
if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len;
}
void show_error( const char * const msg, const int errcode, const bool help ) void show_error( const char * const msg, const int errcode, const bool help )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
@ -896,7 +894,7 @@ int main( const int argc, const char * const argv[] )
internal_error( "bad library version_string" ); internal_error( "bad library version_string" );
if( !ap_init( &parser, argc, argv, options, 0 ) ) if( !ap_init( &parser, argc, argv, options, 0 ) )
{ show_error( "Memory exhausted.", 0, false ); return 1; } { show_error( "Not enough memory.", 0, false ); return 1; }
if( ap_error( &parser ) ) /* bad option */ if( ap_error( &parser ) ) /* bad option */
{ show_error( ap_error( &parser ), 0, true ); return 1; } { show_error( ap_error( &parser ), 0, true ); return 1; }
@ -907,8 +905,7 @@ int main( const int argc, const char * const argv[] )
if( !code ) break; /* no more options */ if( !code ) break; /* no more options */
switch( code ) switch( code )
{ {
case '0': case '0': case '1': case '2': case '3': case '4':
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
encoder_options = option_mapping[code-'0']; break; encoder_options = option_mapping[code-'0']; break;
case 'b': member_size = getnum( arg, 100000, max_member_size ); break; case 'b': member_size = getnum( arg, 100000, max_member_size ); break;

View file

@ -1,6 +1,6 @@
#! /bin/sh #! /bin/sh
# check script for Lzlib - A compression library for lzip files # check script for Lzlib - A compression library for lzip files
# Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Antonio Diaz Diaz.
# #
# This script is free software: you have unlimited permission # This script is free software: you have unlimited permission
# to copy, distribute and modify it. # to copy, distribute and modify it.
@ -29,13 +29,17 @@ fail=0
printf "testing lzlib-%s..." "$2" printf "testing lzlib-%s..." "$2"
"${LZIP}" -cqm4 in > /dev/null
if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi
"${LZIP}" -cqm274 in > /dev/null
if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi
"${LZIP}" -cqs-1 in > /dev/null "${LZIP}" -cqs-1 in > /dev/null
if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi
"${LZIP}" -cqs0 in > /dev/null "${LZIP}" -cqs0 in > /dev/null
if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi
"${LZIP}" -cqs4095 in > /dev/null "${LZIP}" -cqs4095 in > /dev/null
if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi
"${LZIP}" -cqm274 in > /dev/null "${LZIP}" -cqs513MiB in > /dev/null
if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi if [ $? = 1 ] ; then printf . ; else fail=1 ; printf - ; fi
"${LZIP}" -tq in "${LZIP}" -tq in
if [ $? = 2 ] ; then printf . ; else fail=1 ; printf - ; fi if [ $? = 2 ] ; then printf . ; else fail=1 ; printf - ; fi
@ -104,8 +108,16 @@ cmp in anyothername.out || fail=1
printf . printf .
cat in in > in2 || framework_failure cat in in > in2 || framework_failure
"${LZIP}" < in2 > out2 || fail=1 "${LZIP}" -o copy2 < in2 || fail=1
"${LZIP}" -d < out2 > copy2 || fail=1 "${LZIP}" -t copy2.lz || fail=1
printf .
"${LZIP}" -cd copy2.lz > copy2 || fail=1
cmp in2 copy2 || fail=1
printf .
printf "garbage" >> copy2.lz || framework_failure
printf "to be overwritten" > copy2 || framework_failure
"${LZIP}" -df copy2.lz || fail=1
cmp in2 copy2 || fail=1 cmp in2 copy2 || fail=1
printf . printf .