1
0
Fork 0

Compare commits

..

6 commits

Author SHA1 Message Date
4a3010ccc6
Adding upstream version 0.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-02-17 22:53:36 +01:00
7559b1f0d1
Adding upstream version 0.8.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-02-17 22:53:05 +01:00
f6627a3f1e
Adding upstream version 0.7.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-02-17 22:51:50 +01:00
a8181c85d2
Adding upstream version 0.6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-02-17 22:51:32 +01:00
7d45b3fa5f
Adding upstream version 0.5.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-02-17 22:51:18 +01:00
de3f7de10d
Adding upstream version 0.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-02-17 22:49:34 +01:00
31 changed files with 1243 additions and 891 deletions

View file

@ -1,8 +1,7 @@
GNU GENERAL PUBLIC LICENSE GNU GENERAL PUBLIC LICENSE
Version 2, June 1991 Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc., Copyright (C) 1989, 1991 Free Software Foundation, Inc. <http://fsf.org/>
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed. of this license document, but changing it is not allowed.

View file

@ -1,13 +1,70 @@
2025-01-06 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.9 released.
* main.c (decompress): Return 2 if empty member in multimember file.
(Pp_free): New function.
* lzip_decompress.c (Rd_load): Check first byte of the LZMA stream.
* check.sh: Use 'cp' instead of 'cat'.
2024-01-18 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.8 released.
* main.c: Reformat file diagnostics as 'PROGRAM: FILE: MESSAGE'.
(getnum): Show option name and valid range if error.
(show_option_error): New function showing argument and option name.
(main): Make -o preserve date/mode/owner if 1 input file.
* lzip.h: Rename verify_* to check_*.
* configure, Makefile.in: New variable 'MAKEINFO'.
* INSTALL: Document use of CFLAGS+='--std=c99 -D_XOPEN_SOURCE=500'.
2021-01-01 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.7 released.
* main.c (main): Do not open output if input is a terminal.
2020-06-24 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.6 released.
* Make '-o' behave like '-c', but writing to file instead of stdout.
* in_place.c: Multimember decompression should now work for any file
that fits in memory and is smaller than (LONG_MAX - extra_bytes).
* README: Add an analysis of the in-place decompression.
2020-04-27 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.5 released.
* lzip_decompress module updated to version 5.4.18.
* main.c (main): Report an error if a file name is empty.
* Replace 'decompressed', 'compressed' with 'out', 'in' in output.
* Decompression speed has been slightly increased.
* Fix a compilation error with GCC 10. (Reported by Daniel Baumann).
* main.c: Set a valid invocation_name even if argc == 0.
* Document extraction from tar.lz in '--help' output and man page.
* main.c: Compile on DOS with DJGPP.
* configure: Accept appending to CFLAGS; 'CFLAGS+=OPTIONS'.
* testsuite: Add 9 new test files.
2018-09-18 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.4 released.
* lzip_decompress module updated to version 4.18.1-2.
* lzip.c: Rename to lzip_decompress.c.
* lzip_decompress.c (LZd_init): Fix a warning on 32 bit systems.
* in_place.c (set_file_sizes): Skip trailing zeros efficiently.
* main.c (main): Check return value of close( infd ).
* INSTALL: Document use of '-D __USE_MINGW_ANSI_STDIO'.
2018-07-10 Antonio Diaz Diaz <antonio@gnu.org> 2018-07-10 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.3 released. * Version 0.3 released.
* lzip_decompress module updated to version 4.14.40-3.
* lzip.c: Use a precalculated CRC table as lzlib does. * lzip.c: Use a precalculated CRC table as lzlib does.
2018-07-09 Antonio Diaz Diaz <antonio@gnu.org> 2018-07-09 Antonio Diaz Diaz <antonio@gnu.org>
* Version 0.2 released. * Version 0.2 released.
* lzip_decompress module updated to version 4.14.40-2. * lzip_decompress module updated to version 4.14.40-2.
* Improved corrupt header detection to HD=3. * Improve corrupt header detection to HD=3.
* --in-place now works with both --decompress and --test. * --in-place now works with both --decompress and --test.
* main.c: Show final diagnostic when testing multiple files. * main.c: Show final diagnostic when testing multiple files.
@ -18,8 +75,7 @@
* Tests the code shipped in linux patches before june 2018. * Tests the code shipped in linux patches before june 2018.
Copyright (C) 2016-2018 Antonio Diaz Diaz. Copyright (C) 2016-2025 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
but just in case, you have unlimited permission to copy, distribute and in case, you have unlimited permission to copy, distribute, and modify it.
modify it.

50
INSTALL
View file

@ -1,9 +1,14 @@
Requirements Requirements
------------ ------------
You will need a C compiler. You will need a C99 compiler. (gcc 3.3.6 or newer is recommended).
I use gcc 5.3.0 and 4.1.2, but the code should compile with any I use gcc 6.1.0 and 3.3.6, but the code should compile with any standards
standards compliant compiler. compliant compiler.
Gcc is available at http://gcc.gnu.org. Gcc is available at http://gcc.gnu.org
Lzip is available at http://www.nongnu.org/lzip/lzip.html
The operating system must allow signal handlers read access to objects with
static storage duration so that the cleanup handler for Control-C can delete
the partial output file.
Procedure Procedure
@ -14,8 +19,8 @@ Procedure
or or
lzip -cd xlunzip[version].tar.lz | tar -xf - lzip -cd xlunzip[version].tar.lz | tar -xf -
This creates the directory ./xlunzip[version] containing the source from This creates the directory ./xlunzip[version] containing the source code
the main archive. extracted from the archive.
2. Change to xlunzip directory and run configure. 2. Change to xlunzip directory and run configure.
(Try 'configure --help' for usage instructions). (Try 'configure --help' for usage instructions).
@ -23,6 +28,14 @@ the main archive.
cd xlunzip[version] cd xlunzip[version]
./configure ./configure
If you choose a C standard, enable the POSIX features explicitly:
./configure CFLAGS+='--std=c99 -D_XOPEN_SOURCE=500'
If you are compiling on MinGW, use:
./configure CFLAGS+='-D __USE_MINGW_ANSI_STDIO'
3. Run make. 3. Run make.
make make
@ -30,14 +43,15 @@ the main archive.
4. Optionally, type 'make check' to run the tests that come with xlunzip. 4. Optionally, type 'make check' to run the tests that come with xlunzip.
5. Type 'make install' to install the program and any data files and 5. Type 'make install' to install the program and any data files and
documentation. documentation. You need root privileges to install into a prefix owned
by root.
Or type 'make install-compress', which additionally compresses the Or type 'make install-compress', which additionally compresses the
man page after installation. (Installing compressed docs may become man page after installation.
the default in the future). (Installing compressed docs may become the default in the future).
You can install only the program or the man page by typing 'make You can install only the program or the man page by typing
install-bin' or 'make install-man' respectively. 'make install-bin' or 'make install-man' respectively.
Instead of 'make install', you can type 'make install-as-lzip' to Instead of 'make install', you can type 'make install-as-lzip' to
install the program and any data files and documentation, and link install the program and any data files and documentation, and link
@ -47,21 +61,21 @@ the main archive.
Another way Another way
----------- -----------
You can also compile xlunzip into a separate directory. You can also compile xlunzip into a separate directory.
To do this, you must use a version of 'make' that supports the 'VPATH' To do this, you must use a version of 'make' that supports the variable
variable, such as GNU 'make'. 'cd' to the directory where you want the 'VPATH', such as GNU 'make'. 'cd' to the directory where you want the
object files and executables to go and run the 'configure' script. object files and executables to go and run the 'configure' script.
'configure' automatically checks for the source code in '.', in '..' and 'configure' automatically checks for the source code in '.', in '..', and
in the 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
look for the sources. Usually 'configure' can determine that directory for the source code. Usually 'configure' can determine that directory
automatically. automatically.
After running 'configure', you can run 'make' and 'make install' as After running 'configure', you can run 'make' and 'make install' as
explained above. explained above.
Copyright (C) 2016-2018 Antonio Diaz Diaz. Copyright (C) 2016-2025 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,12 +2,12 @@
DISTNAME = $(pkgname)-$(pkgversion) DISTNAME = $(pkgname)-$(pkgversion)
INSTALL = install INSTALL = install
INSTALL_PROGRAM = $(INSTALL) -m 755 INSTALL_PROGRAM = $(INSTALL) -m 755
INSTALL_DATA = $(INSTALL) -m 644
INSTALL_DIR = $(INSTALL) -d -m 755 INSTALL_DIR = $(INSTALL) -d -m 755
INSTALL_DATA = $(INSTALL) -m 644
SHELL = /bin/sh SHELL = /bin/sh
CAN_RUN_INSTALLINFO = $(SHELL) -c "install-info --version" > /dev/null 2>&1 CAN_RUN_INSTALLINFO = $(SHELL) -c "install-info --version" > /dev/null 2>&1
objs = carg_parser.o decompress_lunzip.o in_place.o lzip.o main.o objs = carg_parser.o decompress_lunzip.o in_place.o lzip_decompress.o main.o
.PHONY : all install install-bin install-info install-man \ .PHONY : all install install-bin install-info install-man \
@ -20,7 +20,7 @@ objs = carg_parser.o decompress_lunzip.o in_place.o lzip.o main.o
all : $(progname) all : $(progname)
$(progname) : $(objs) $(progname) : $(objs)
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(objs) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(objs)
main.o : main.c main.o : main.c
$(CC) $(CPPFLAGS) $(CFLAGS) -DPROGVERSION=\"$(pkgversion)\" -c -o $@ $< $(CC) $(CPPFLAGS) $(CFLAGS) -DPROGVERSION=\"$(pkgversion)\" -c -o $@ $<
@ -28,25 +28,29 @@ main.o : main.c
%.o : %.c %.o : %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
# prevent 'make' from trying to remake source files
$(VPATH)/configure $(VPATH)/Makefile.in $(VPATH)/doc/$(pkgname).texi : ;
MAKEFLAGS += -r
.SUFFIXES :
$(objs) : Makefile $(objs) : Makefile
carg_parser.o : carg_parser.h carg_parser.o : carg_parser.h
decompress_lunzip.o : linux_lzip.h linux_lunzip.h linux_mm.h lzip.c decompress_lunzip.o : linux_lzip.h linux_lunzip.h linux_mm.h lzip_decompress.c
in_place.o : linux_lunzip.h lzip.h in_place.o : linux_lunzip.h lzip.h
lzip.o : linux_lzip.h linux_mm.h lzip_decompress.o : linux_module.h linux_lzip.h linux_mm.h
main.o : carg_parser.h linux_lzip.h linux_lunzip.h lzip.h main.o : carg_parser.h linux_lzip.h linux_lunzip.h lzip.h
doc : man doc : man
info : $(VPATH)/doc/$(pkgname).info info : $(VPATH)/doc/$(pkgname).info
$(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texi $(VPATH)/doc/$(pkgname).info : $(VPATH)/doc/$(pkgname).texi
cd $(VPATH)/doc && makeinfo $(pkgname).texi cd $(VPATH)/doc && $(MAKEINFO) $(pkgname).texi
man : $(VPATH)/doc/$(progname).1 man : $(VPATH)/doc/$(progname).1
$(VPATH)/doc/$(progname).1 : $(progname) $(VPATH)/doc/$(progname).1 : $(progname)
help2man -n 'test tool for the lunzip linux module' -o $@ --no-info ./$(progname) help2man -n 'test tool for the lzip_decompress linux module' -o $@ --no-info ./$(progname)
Makefile : $(VPATH)/configure $(VPATH)/Makefile.in Makefile : $(VPATH)/configure $(VPATH)/Makefile.in
./config.status ./config.status
@ -71,7 +75,7 @@ install-info :
-rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"* -rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"*
$(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info "$(DESTDIR)$(infodir)/$(pkgname).info" $(INSTALL_DATA) $(VPATH)/doc/$(pkgname).info "$(DESTDIR)$(infodir)/$(pkgname).info"
-if $(CAN_RUN_INSTALLINFO) ; then \ -if $(CAN_RUN_INSTALLINFO) ; then \
install-info --info-dir="$(DESTDIR)$(infodir)" "$(DESTDIR)$(infodir)/$(pkgname).info" ; \ install-info --info-dir="$(DESTDIR)$(infodir)" "$(DESTDIR)$(infodir)/$(pkgname).info" ; \
fi fi
install-info-compress : install-info install-info-compress : install-info
@ -96,7 +100,7 @@ uninstall-bin :
uninstall-info : uninstall-info :
-if $(CAN_RUN_INSTALLINFO) ; then \ -if $(CAN_RUN_INSTALLINFO) ; then \
install-info --info-dir="$(DESTDIR)$(infodir)" --remove "$(DESTDIR)$(infodir)/$(pkgname).info" ; \ install-info --info-dir="$(DESTDIR)$(infodir)" --remove "$(DESTDIR)$(infodir)/$(pkgname).info" ; \
fi fi
-rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"* -rm -f "$(DESTDIR)$(infodir)/$(pkgname).info"*
@ -119,8 +123,10 @@ dist : doc
$(DISTNAME)/*.c \ $(DISTNAME)/*.c \
$(DISTNAME)/testsuite/check.sh \ $(DISTNAME)/testsuite/check.sh \
$(DISTNAME)/testsuite/test.txt \ $(DISTNAME)/testsuite/test.txt \
$(DISTNAME)/testsuite/test.txt.lz \ $(DISTNAME)/testsuite/em.lz \
$(DISTNAME)/testsuite/zero.lz $(DISTNAME)/testsuite/fox.lz \
$(DISTNAME)/testsuite/fox_*.lz \
$(DISTNAME)/testsuite/test.txt.lz
rm -f $(DISTNAME) rm -f $(DISTNAME)
lzip -v -9 $(DISTNAME).tar lzip -v -9 $(DISTNAME).tar

14
NEWS
View file

@ -1,11 +1,7 @@
Changes in version 0.3: Changes in version 0.9:
lzip_decompress module updated to version 4.14.40-3. xlunzip now exits with error status 2 if any empty member is found in a
multimember file.
Use a precalculated CRC table as lzlib does. xlunzip now exits with error status 2 if the first byte of the LZMA stream
is not 0.
Improved corrupt header detection to HD=3.
'--in-place' now works with both '--decompress' and '--test'.
Show final diagnostic when testing multiple files.

120
README
View file

@ -1,47 +1,113 @@
See the file INSTALL for compilation and installation instructions.
Description Description
Xlunzip is a test tool for the lzip decompression code of my lzip patch Xlunzip is a test tool for the lzip decompression code of my lzip patch for
for linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress linux
linux module as a backend. Xlunzip tests the module for stream, module as a backend. Xlunzip tests the module for stream, buffer-to-buffer,
buffer-to-buffer and mixed decompression modes, including in-place and mixed decompression modes, including in-place decompression (using the
decompression (using the same buffer for input and output). You can use same buffer for input and output). You can use xlunzip to check that the
xlunzip to verify that the module produces correct results when module produces correct results when decompressing single member files,
decompressing single member files, multimember files, or the multimember files, or the concatenation of two or more compressed files.
concatenation of two or more compressed files. Xlunzip can be used with Xlunzip can be used with unzcrash to test the robustness of the module to
unzcrash to test the robustness of the module to the decompression of the decompression of corrupted data.
corrupted data.
Note that the in-place decompression of concatenated files can't be The distributed index feature of the lzip format allows xlunzip to
guaranteed to work because an arbitrarily low compression ratio of the decompress concatenated files in place. This can't be guaranteed to work
last part of the data can be achieved by appending enough empty with formats like gzip or bzip2 because they can't detect whether a high
compressed members to a file. compression ratio in the first members of the multimember data is being
masked by a low compression ratio in the last members.
The xlunzip tarball contains a copy of the lzip_decompress module and can be
compiled and tested without downloading or applying the patch to the kernel.
The xlunzip tarball contains a copy of the lzip_decompress module and
can be compiled and tested without downloading or applying the patch to
the kernel.
My lzip patch for linux can be found at My lzip patch for linux can be found at
http://download.savannah.gnu.org/releases/lzip/kernel/ http://download.savannah.gnu.org/releases/lzip/kernel/
Lzip related components in the kernel Lzip related components in the kernel
===================================== =====================================
The lzip_decompress module in lib/lzip.c provides a versatile lzip The lzip_decompress module in lib/lzip_decompress.c provides a versatile
decompression function able to do buffer to buffer decompression or lzip decompression function able to do buffer-to-buffer decompression or
stream decompression with fill and flush callback functions. The usage stream decompression with fill and flush callback functions. The usage of
of the function is documented in include/linux/lzip.h. the function is documented in include/linux/lzip.h.
For decompressing the kernel image, initramfs, and initrd, there is a For decompressing the kernel image, initramfs, and initrd, there is a
wrapper function in lib/decompress_lunzip.c providing the same common wrapper function in lib/decompress_lunzip.c providing the same common
interface as the other decompress_*.c files, which is defined in interface as the other decompress_*.c files, which is defined in
include/linux/decompress/generic.h. include/linux/decompress/generic.h.
Analysis of the in-place decompression
======================================
Copyright (C) 2016-2018 Antonio Diaz Diaz. In order to decompress the kernel in place (using the same buffer for input
and output), the compressed data is placed at the end of the buffer used to
hold the decompressed data. The buffer must be large enough to contain after
the decompressed data extra space for a marker, a trailer, the maximum
possible data expansion, and (if the compressed data consists of more than
one member) N-1 empty members.
|------ compressed data ------|
V V
|----------------|-------------------|---------|
^ ^ extra
|-------- decompressed data ---------|
The input pointer initially points to the beginning of the compressed data
and the output pointer initially points to the beginning of the buffer.
Decompressing compressible data reduces the distance between the pointers,
while decompressing uncompressible data increases the distance. The extra
space must be large enough that the output pointer does not overrun the
input pointer even if all the overlap between compressed and decompressed
data is uncompressible. The worst case is very compressible data followed by
uncompressible data because in this case the output pointer increases faster
when the input pointer is smaller.
| * <-- input pointer (*)
| * , <-- output pointer (,)
| * , '
| x ' <-- overrun (x)
memory | * ,'
address | * ,'
|* ,'
| ,'
| ,'
|,'
'--------------------------
time
All we need to know to calculate the minimum required extra space is:
The maximum expansion ratio.
The size of the last part of a member required to check integrity.
For multimember data, the overhead per member. (36 bytes for lzip).
The maximum expansion ratio of LZMA data is of about 1.4%. Rounding this up
to 1/64 (1.5625%) and adding 36 bytes per input member, the extra space
required to decompress lzip data in place is:
extra_bytes = ( compressed_size >> 6 ) + members * 36
Using the compressed size to calculate the extra_bytes (as in the formula
above) may slightly overestimate the amount of space required in the worst
case (maximum expansion). But calculating the extra_bytes from the
uncompressed size (as does linux currently) is wrong (and inefficient for
high compression ratios). The formula used in arch/x86/boot/header.S:
extra_bytes = ( uncompressed_size >> 8 ) + 131072
fails to decompress 1 MB of zeros followed by 8 MB of random data, wastes
memory for compression ratios larger than 4:1, and does not even consider
multimember data.
Xlunzip uses Arg_parser for command-line argument parsing:
http://www.nongnu.org/arg-parser/arg_parser.html
Copyright (C) 2016-2025 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.
The file Makefile.in is a data file used by configure to produce the The file Makefile.in is a data file used by configure to produce the Makefile.
Makefile. It has the same copyright owner and permissions that configure It has the same copyright owner and permissions that configure itself.
itself.

View file

@ -1,20 +1,20 @@
/* Arg_parser - POSIX/GNU command line argument parser. (C version) /* Arg_parser - POSIX/GNU command-line argument parser. (C version)
Copyright (C) 2006-2018 Antonio Diaz Diaz. Copyright (C) 2006-2025 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided binary forms, with or without modification, are permitted provided
that the following conditions are met: that the following conditions are met:
1. Redistributions of source code must retain the above copyright 1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer. notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/ */
#include <stdlib.h> #include <stdlib.h>
@ -32,28 +32,46 @@ static void * ap_resize_buffer( void * buf, const int min_size )
} }
static char push_back_record( struct Arg_parser * const ap, static char push_back_record( Arg_parser * const ap, const int code,
const int code, const char * const argument ) const char * const long_name,
const char * const argument )
{ {
const int len = strlen( argument ); ap_Record * p;
struct ap_Record * p;
void * tmp = ap_resize_buffer( ap->data, void * tmp = ap_resize_buffer( ap->data,
( ap->data_size + 1 ) * sizeof (struct ap_Record) ); ( ap->data_size + 1 ) * sizeof (ap_Record) );
if( !tmp ) return 0; if( !tmp ) return 0;
ap->data = (struct ap_Record *)tmp; ap->data = (ap_Record *)tmp;
p = &(ap->data[ap->data_size]); p = &(ap->data[ap->data_size]);
p->code = code; p->code = code;
p->argument = 0; if( long_name )
tmp = ap_resize_buffer( p->argument, len + 1 ); {
if( !tmp ) return 0; const int len = strlen( long_name );
p->argument = (char *)tmp; p->parsed_name = (char *)malloc( len + 2 + 1 );
strncpy( p->argument, argument, len + 1 ); if( !p->parsed_name ) return 0;
p->parsed_name[0] = p->parsed_name[1] = '-';
strncpy( p->parsed_name + 2, long_name, len + 1 );
}
else if( code > 0 && code < 256 )
{
p->parsed_name = (char *)malloc( 2 + 1 );
if( !p->parsed_name ) return 0;
p->parsed_name[0] = '-'; p->parsed_name[1] = code; p->parsed_name[2] = 0;
}
else p->parsed_name = 0;
if( argument )
{
const int len = strlen( argument );
p->argument = (char *)malloc( len + 1 );
if( !p->argument ) { free( p->parsed_name ); return 0; }
strncpy( p->argument, argument, len + 1 );
}
else p->argument = 0;
++ap->data_size; ++ap->data_size;
return 1; return 1;
} }
static char add_error( struct Arg_parser * const ap, const char * const msg ) static char add_error( Arg_parser * const ap, const char * const msg )
{ {
const int len = strlen( msg ); const int len = strlen( msg );
void * tmp = ap_resize_buffer( ap->error, ap->error_size + len + 1 ); void * tmp = ap_resize_buffer( ap->error, ap->error_size + len + 1 );
@ -65,19 +83,20 @@ static char add_error( struct Arg_parser * const ap, const char * const msg )
} }
static void free_data( struct Arg_parser * const ap ) static void free_data( Arg_parser * const ap )
{ {
int i; int i;
for( i = 0; i < ap->data_size; ++i ) free( ap->data[i].argument ); for( i = 0; i < ap->data_size; ++i )
{ free( ap->data[i].argument ); free( ap->data[i].parsed_name ); }
if( ap->data ) { free( ap->data ); ap->data = 0; } if( ap->data ) { free( ap->data ); ap->data = 0; }
ap->data_size = 0; ap->data_size = 0;
} }
static char parse_long_option( struct Arg_parser * const ap, /* Return 0 only if out of memory. */
static char parse_long_option( Arg_parser * const ap,
const char * const opt, const char * const arg, const char * const opt, const char * const arg,
const struct ap_Option options[], const ap_Option options[], int * const argindp )
int * const argindp )
{ {
unsigned len; unsigned len;
int index = -1, i; int index = -1, i;
@ -87,9 +106,10 @@ static char parse_long_option( struct Arg_parser * const ap,
/* Test all long options for either exact match or abbreviated matches. */ /* Test all long options for either exact match or abbreviated matches. */
for( i = 0; options[i].code != 0; ++i ) for( i = 0; options[i].code != 0; ++i )
if( options[i].name && strncmp( options[i].name, &opt[2], len ) == 0 ) if( options[i].long_name &&
strncmp( options[i].long_name, &opt[2], len ) == 0 )
{ {
if( strlen( options[i].name ) == len ) /* Exact match found */ if( strlen( options[i].long_name ) == len ) /* Exact match found */
{ index = i; exact = 1; break; } { index = i; exact = 1; break; }
else if( index < 0 ) index = i; /* First nonexact match found */ else if( index < 0 ) index = i; /* First nonexact match found */
else if( options[index].code != options[i].code || else if( options[index].code != options[i].code ||
@ -117,52 +137,55 @@ static char parse_long_option( struct Arg_parser * const ap,
{ {
if( options[index].has_arg == ap_no ) if( options[index].has_arg == ap_no )
{ {
add_error( ap, "option '--" ); add_error( ap, options[index].name ); add_error( ap, "option '--" ); add_error( ap, options[index].long_name );
add_error( ap, "' doesn't allow an argument" ); add_error( ap, "' doesn't allow an argument" );
return 1; return 1;
} }
if( options[index].has_arg == ap_yes && !opt[len+3] ) if( options[index].has_arg == ap_yes && !opt[len+3] )
{ {
add_error( ap, "option '--" ); add_error( ap, options[index].name ); add_error( ap, "option '--" ); add_error( ap, options[index].long_name );
add_error( ap, "' requires an argument" ); add_error( ap, "' requires an argument" );
return 1; return 1;
} }
return push_back_record( ap, options[index].code, &opt[len+3] ); return push_back_record( ap, options[index].code, options[index].long_name,
&opt[len+3] ); /* argument may be empty */
} }
if( options[index].has_arg == ap_yes ) if( options[index].has_arg == ap_yes || options[index].has_arg == ap_yme )
{ {
if( !arg || !arg[0] ) if( !arg || ( options[index].has_arg == ap_yes && !arg[0] ) )
{ {
add_error( ap, "option '--" ); add_error( ap, options[index].name ); add_error( ap, "option '--" ); add_error( ap, options[index].long_name );
add_error( ap, "' requires an argument" ); add_error( ap, "' requires an argument" );
return 1; return 1;
} }
++*argindp; ++*argindp;
return push_back_record( ap, options[index].code, arg ); return push_back_record( ap, options[index].code, options[index].long_name,
arg ); /* argument may be empty */
} }
return push_back_record( ap, options[index].code, "" ); return push_back_record( ap, options[index].code,
options[index].long_name, 0 );
} }
static char parse_short_option( struct Arg_parser * const ap, /* Return 0 only if out of memory. */
static char parse_short_option( Arg_parser * const ap,
const char * const opt, const char * const arg, const char * const opt, const char * const arg,
const struct ap_Option options[], const ap_Option options[], int * const argindp )
int * const argindp )
{ {
int cind = 1; /* character index in opt */ int cind = 1; /* character index in opt */
while( cind > 0 ) while( cind > 0 )
{ {
int index = -1, i; int index = -1, i;
const unsigned char code = opt[cind]; const unsigned char c = opt[cind];
char code_str[2]; char code_str[2];
code_str[0] = code; code_str[1] = 0; code_str[0] = c; code_str[1] = 0;
if( code != 0 ) if( c != 0 )
for( i = 0; options[i].code; ++i ) for( i = 0; options[i].code; ++i )
if( code == options[i].code ) if( c == options[i].code )
{ index = i; break; } { index = i; break; }
if( index < 0 ) if( index < 0 )
@ -176,34 +199,34 @@ static char parse_short_option( struct Arg_parser * const ap,
if( options[index].has_arg != ap_no && cind > 0 && opt[cind] ) if( options[index].has_arg != ap_no && cind > 0 && opt[cind] )
{ {
if( !push_back_record( ap, code, &opt[cind] ) ) return 0; if( !push_back_record( ap, c, 0, &opt[cind] ) ) return 0;
++*argindp; cind = 0; ++*argindp; cind = 0;
} }
else if( options[index].has_arg == ap_yes ) else if( options[index].has_arg == ap_yes || options[index].has_arg == ap_yme )
{ {
if( !arg || !arg[0] ) if( !arg || ( options[index].has_arg == ap_yes && !arg[0] ) )
{ {
add_error( ap, "option requires an argument -- '" ); add_error( ap, "option requires an argument -- '" );
add_error( ap, code_str ); add_error( ap, "'" ); add_error( ap, code_str ); add_error( ap, "'" );
return 1; return 1;
} }
++*argindp; cind = 0; ++*argindp; cind = 0; /* argument may be empty */
if( !push_back_record( ap, code, arg ) ) return 0; if( !push_back_record( ap, c, 0, arg ) ) return 0;
} }
else if( !push_back_record( ap, code, "" ) ) return 0; else if( !push_back_record( ap, c, 0, 0 ) ) return 0;
} }
return 1; return 1;
} }
char ap_init( struct Arg_parser * const ap, char ap_init( Arg_parser * const ap,
const int argc, const char * const argv[], const int argc, const char * const argv[],
const struct ap_Option options[], const char in_order ) const ap_Option options[], const char in_order )
{ {
const char ** non_options = 0; /* skipped non-options */ const char ** non_options = 0; /* skipped non-options */
int non_options_size = 0; /* number of skipped non-options */ int non_options_size = 0; /* number of skipped non-options */
int argind = 1; /* index in argv */ int argind = 1; /* index in argv */
int i; char done = 0; /* false until success */
ap->data = 0; ap->data = 0;
ap->error = 0; ap->error = 0;
@ -223,20 +246,20 @@ char ap_init( struct Arg_parser * const ap,
if( ch2 == '-' ) if( ch2 == '-' )
{ {
if( !argv[argind][2] ) { ++argind; break; } /* we found "--" */ if( !argv[argind][2] ) { ++argind; break; } /* we found "--" */
else if( !parse_long_option( ap, opt, arg, options, &argind ) ) return 0; else if( !parse_long_option( ap, opt, arg, options, &argind ) ) goto out;
} }
else if( !parse_short_option( ap, opt, arg, options, &argind ) ) return 0; else if( !parse_short_option( ap, opt, arg, options, &argind ) ) goto out;
if( ap->error ) break; if( ap->error ) break;
} }
else else
{ {
if( in_order ) if( in_order )
{ if( !push_back_record( ap, 0, argv[argind++] ) ) return 0; } { if( !push_back_record( ap, 0, 0, argv[argind++] ) ) goto out; }
else else
{ {
void * tmp = ap_resize_buffer( non_options, void * tmp = ap_resize_buffer( non_options,
( non_options_size + 1 ) * sizeof *non_options ); ( non_options_size + 1 ) * sizeof *non_options );
if( !tmp ) return 0; if( !tmp ) goto out;
non_options = (const char **)tmp; non_options = (const char **)tmp;
non_options[non_options_size++] = argv[argind++]; non_options[non_options_size++] = argv[argind++];
} }
@ -245,17 +268,19 @@ char ap_init( struct Arg_parser * const ap,
if( ap->error ) free_data( ap ); if( ap->error ) free_data( ap );
else else
{ {
int i;
for( i = 0; i < non_options_size; ++i ) for( i = 0; i < non_options_size; ++i )
if( !push_back_record( ap, 0, non_options[i] ) ) return 0; if( !push_back_record( ap, 0, 0, non_options[i] ) ) goto out;
while( argind < argc ) while( argind < argc )
if( !push_back_record( ap, 0, argv[argind++] ) ) return 0; if( !push_back_record( ap, 0, 0, argv[argind++] ) ) goto out;
} }
if( non_options ) free( non_options ); done = 1;
return 1; out: if( non_options ) free( non_options );
return done;
} }
void ap_free( struct Arg_parser * const ap ) void ap_free( Arg_parser * const ap )
{ {
free_data( ap ); free_data( ap );
if( ap->error ) { free( ap->error ); ap->error = 0; } if( ap->error ) { free( ap->error ); ap->error = 0; }
@ -263,23 +288,26 @@ void ap_free( struct Arg_parser * const ap )
} }
const char * ap_error( const struct Arg_parser * const ap ) const char * ap_error( const Arg_parser * const ap ) { return ap->error; }
{ return ap->error; }
int ap_arguments( const Arg_parser * const ap ) { return ap->data_size; }
int ap_arguments( const struct Arg_parser * const ap ) int ap_code( const Arg_parser * const ap, const int i )
{ return ap->data_size; }
int ap_code( const struct Arg_parser * const ap, const int i )
{ {
if( i >= 0 && i < ap_arguments( ap ) ) return ap->data[i].code; if( i < 0 || i >= ap_arguments( ap ) ) return 0;
else return 0; return ap->data[i].code;
} }
const char * ap_argument( const struct Arg_parser * const ap, const int i ) const char * ap_parsed_name( const Arg_parser * const ap, const int i )
{ {
if( i >= 0 && i < ap_arguments( ap ) ) return ap->data[i].argument; if( i < 0 || i >= ap_arguments( ap ) || !ap->data[i].parsed_name ) return "";
else return ""; return ap->data[i].parsed_name;
}
const char * ap_argument( const Arg_parser * const ap, const int i )
{
if( i < 0 || i >= ap_arguments( ap ) || !ap->data[i].argument ) return "";
return ap->data[i].argument;
} }

View file

@ -1,92 +1,101 @@
/* Arg_parser - POSIX/GNU command line argument parser. (C version) /* Arg_parser - POSIX/GNU command-line argument parser. (C version)
Copyright (C) 2006-2018 Antonio Diaz Diaz. Copyright (C) 2006-2025 Antonio Diaz Diaz.
This library is free software. Redistribution and use in source and This library is free software. Redistribution and use in source and
binary forms, with or without modification, are permitted provided binary forms, with or without modification, are permitted provided
that the following conditions are met: that the following conditions are met:
1. Redistributions of source code must retain the above copyright 1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer. notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/ */
/* Arg_parser reads the arguments in 'argv' and creates a number of /* Arg_parser reads the arguments in 'argv' and creates a number of
option codes, option arguments and non-option arguments. option codes, option arguments, and non-option arguments.
In case of error, 'ap_error' returns a non-null pointer to an error In case of error, 'ap_error' returns a non-null pointer to an error
message. message.
'options' is an array of 'struct ap_Option' terminated by an element 'options' is an array of 'struct ap_Option' terminated by an element
containing a code which is zero. A null name means a short-only containing a code which is zero. A null long_name means a short-only
option. A code value outside the unsigned char range means a option. A code value outside the unsigned char range means a long-only
long-only option. option.
Arg_parser normally makes it appear as if all the option arguments Arg_parser normally makes it appear as if all the option arguments
were specified before all the non-option arguments for the purposes were specified before all the non-option arguments for the purposes
of parsing, even if the user of your program intermixed option and of parsing, even if the user of your program intermixed option and
non-option arguments. If you want the arguments in the exact order non-option arguments. If you want the arguments in the exact order
the user typed them, call 'ap_init' with 'in_order' = true. the user typed them, call 'ap_init' with 'in_order' = true.
The argument '--' terminates all options; any following arguments are The argument '--' terminates all options; any following arguments are
treated as non-option arguments, even if they begin with a hyphen. treated as non-option arguments, even if they begin with a hyphen.
The syntax for optional option arguments is '-<short_option><argument>' The syntax of options with an optional argument is
(without whitespace), or '--<long_option>=<argument>'. '-<short_option><argument>' (without whitespace), or
'--<long_option>=<argument>'.
The syntax of options with an empty argument is '-<short_option> ""',
'--<long_option> ""', or '--<long_option>=""'.
*/ */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
enum ap_Has_arg { ap_no, ap_yes, ap_maybe }; /* ap_yme = yes but maybe empty */
typedef enum ap_Has_arg { ap_no, ap_yes, ap_maybe, ap_yme } ap_Has_arg;
struct ap_Option typedef struct ap_Option
{ {
int code; /* Short option letter or code ( code != 0 ) */ int code; /* Short option letter or code ( code != 0 ) */
const char * name; /* Long option name (maybe null) */ const char * long_name; /* Long option name (maybe null) */
enum ap_Has_arg has_arg; ap_Has_arg has_arg;
}; } ap_Option;
struct ap_Record typedef struct ap_Record
{ {
int code; int code;
char * parsed_name;
char * argument; char * argument;
}; } ap_Record;
struct Arg_parser typedef struct Arg_parser
{ {
struct ap_Record * data; ap_Record * data;
char * error; char * error;
int data_size; int data_size;
int error_size; int error_size;
}; } Arg_parser;
char ap_init( struct Arg_parser * const ap, char ap_init( Arg_parser * const ap,
const int argc, const char * const argv[], const int argc, const char * const argv[],
const struct ap_Option options[], const char in_order ); const ap_Option options[], const char in_order );
void ap_free( struct Arg_parser * const ap ); void ap_free( Arg_parser * const ap );
const char * ap_error( const struct Arg_parser * const ap ); const char * ap_error( const Arg_parser * const ap );
/* The number of arguments parsed (may be different from argc) */ /* The number of arguments parsed. May be different from argc. */
int ap_arguments( const struct Arg_parser * const ap ); int ap_arguments( const Arg_parser * const ap );
/* If ap_code( i ) is 0, ap_argument( i ) is a non-option. /* If ap_code( i ) is 0, ap_argument( i ) is a non-option.
Else ap_argument( i ) is the option's argument (or empty). */ Else ap_argument( i ) is the option's argument (or empty). */
int ap_code( const struct Arg_parser * const ap, const int i ); int ap_code( const Arg_parser * const ap, const int i );
const char * ap_argument( const struct Arg_parser * const ap, const int i ); /* Full name of the option parsed (short or long). */
const char * ap_parsed_name( const Arg_parser * const ap, const int i );
const char * ap_argument( const Arg_parser * const ap, const int i );
#ifdef __cplusplus #ifdef __cplusplus
} }

60
configure vendored
View file

@ -1,12 +1,12 @@
#! /bin/sh #! /bin/sh
# configure script for Xlunzip - Test tool for the lunzip linux module # configure script for Xlunzip - Test tool for the lzip_decompress linux module
# Copyright (C) 2016-2018 Antonio Diaz Diaz. # Copyright (C) 2016-2025 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=xlunzip pkgname=xlunzip
pkgversion=0.3 pkgversion=0.9
progname=xlunzip progname=xlunzip
srctrigger=doc/${progname}.1 srctrigger=doc/${progname}.1
@ -24,13 +24,10 @@ CC=gcc
CPPFLAGS= CPPFLAGS=
CFLAGS='-Wall -W -O2' CFLAGS='-Wall -W -O2'
LDFLAGS= LDFLAGS=
MAKEINFO=makeinfo
# checking whether we are using GNU C. # checking whether we are using GNU C.
/bin/sh -c "${CC} --version" > /dev/null 2>&1 || /bin/sh -c "${CC} --version" > /dev/null 2>&1 || { CC=cc ; CFLAGS=-O2 ; }
{
CC=cc
CFLAGS=-O2
}
# Loop over all args # Loop over all args
args= args=
@ -42,11 +39,12 @@ while [ $# != 0 ] ; do
shift shift
# Add the argument quoted to args # Add the argument quoted to args
args="${args} \"${option}\"" if [ -z "${args}" ] ; then args="\"${option}\""
else args="${args} \"${option}\"" ; fi
# Split out the argument for options that take them # Split out the argument for options that take them
case ${option} in case ${option} in
*=*) optarg=`echo ${option} | sed -e 's,^[^=]*=,,;s,/$,,'` ;; *=*) optarg=`echo "${option}" | sed -e 's,^[^=]*=,,;s,/$,,'` ;;
esac esac
# Process the options # Process the options
@ -60,7 +58,7 @@ while [ $# != 0 ] ; do
echo "Options and variables: [defaults in brackets]" echo "Options and variables: [defaults in brackets]"
echo " -h, --help display this help and exit" echo " -h, --help display this help and exit"
echo " -V, --version output version information and exit" echo " -V, --version output version information and exit"
echo " --srcdir=DIR find the sources in DIR [. or ..]" echo " --srcdir=DIR find the source code in DIR [. or ..]"
echo " --prefix=DIR install into DIR [${prefix}]" echo " --prefix=DIR install into DIR [${prefix}]"
echo " --exec-prefix=DIR base directory for arch-dependent files [${exec_prefix}]" echo " --exec-prefix=DIR base directory for arch-dependent files [${exec_prefix}]"
echo " --bindir=DIR user executables directory [${bindir}]" echo " --bindir=DIR user executables directory [${bindir}]"
@ -68,9 +66,11 @@ while [ $# != 0 ] ; do
echo " --infodir=DIR info files directory [${infodir}]" echo " --infodir=DIR info files directory [${infodir}]"
echo " --mandir=DIR man pages directory [${mandir}]" echo " --mandir=DIR man pages directory [${mandir}]"
echo " CC=COMPILER C compiler to use [${CC}]" echo " CC=COMPILER C compiler to use [${CC}]"
echo " CPPFLAGS=OPTIONS command line options for the preprocessor [${CPPFLAGS}]" echo " CPPFLAGS=OPTIONS command-line options for the preprocessor [${CPPFLAGS}]"
echo " CFLAGS=OPTIONS command line options for the C compiler [${CFLAGS}]" echo " CFLAGS=OPTIONS command-line options for the C compiler [${CFLAGS}]"
echo " LDFLAGS=OPTIONS command line options for the linker [${LDFLAGS}]" echo " CFLAGS+=OPTIONS append options to the current value of CFLAGS"
echo " LDFLAGS=OPTIONS command-line options for the linker [${LDFLAGS}]"
echo " MAKEINFO=NAME makeinfo program to use [${MAKEINFO}]"
echo echo
exit 0 ;; exit 0 ;;
--version | -V) --version | -V)
@ -93,10 +93,12 @@ while [ $# != 0 ] ; do
--mandir=*) mandir=${optarg} ;; --mandir=*) mandir=${optarg} ;;
--no-create) no_create=yes ;; --no-create) no_create=yes ;;
CC=*) CC=${optarg} ;; CC=*) CC=${optarg} ;;
CPPFLAGS=*) CPPFLAGS=${optarg} ;; CPPFLAGS=*) CPPFLAGS=${optarg} ;;
CFLAGS=*) CFLAGS=${optarg} ;; CFLAGS=*) CFLAGS=${optarg} ;;
LDFLAGS=*) LDFLAGS=${optarg} ;; CFLAGS+=*) CFLAGS="${CFLAGS} ${optarg}" ;;
LDFLAGS=*) LDFLAGS=${optarg} ;;
MAKEINFO=*) MAKEINFO=${optarg} ;;
--*) --*)
echo "configure: WARNING: unrecognized option: '${option}'" 1>&2 ;; echo "configure: WARNING: unrecognized option: '${option}'" 1>&2 ;;
@ -107,7 +109,7 @@ while [ $# != 0 ] ; do
exit 1 ;; exit 1 ;;
esac esac
# Check if the option took a separate argument # Check whether the option took a separate argument
if [ "${arg2}" = yes ] ; then if [ "${arg2}" = yes ] ; then
if [ $# != 0 ] ; then args="${args} \"$1\"" ; shift if [ $# != 0 ] ; then args="${args} \"$1\"" ; shift
else echo "configure: Missing argument to '${option}'" 1>&2 else echo "configure: Missing argument to '${option}'" 1>&2
@ -116,19 +118,19 @@ while [ $# != 0 ] ; do
fi fi
done done
# Find the source files, if location was not specified. # Find the source code, if location was not specified.
srcdirtext= srcdirtext=
if [ -z "${srcdir}" ] ; then if [ -z "${srcdir}" ] ; then
srcdirtext="or . or .." ; srcdir=. srcdirtext="or . or .." ; srcdir=.
if [ ! -r "${srcdir}/${srctrigger}" ] ; then srcdir=.. ; fi if [ ! -r "${srcdir}/${srctrigger}" ] ; then srcdir=.. ; fi
if [ ! -r "${srcdir}/${srctrigger}" ] ; then if [ ! -r "${srcdir}/${srctrigger}" ] ; then
## the sed command below emulates the dirname command ## the sed command below emulates the dirname command
srcdir=`echo $0 | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` srcdir=`echo "$0" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
fi fi
fi fi
if [ ! -r "${srcdir}/${srctrigger}" ] ; then if [ ! -r "${srcdir}/${srctrigger}" ] ; then
echo "configure: Can't find sources in ${srcdir} ${srcdirtext}" 1>&2 echo "configure: Can't find source code in ${srcdir} ${srcdirtext}" 1>&2
echo "configure: (At least ${srctrigger} is missing)." 1>&2 echo "configure: (At least ${srctrigger} is missing)." 1>&2
exit 1 exit 1
fi fi
@ -146,9 +148,9 @@ if [ -z "${no_create}" ] ; then
# Run this file to recreate the current configuration. # Run this file to recreate the current configuration.
# #
# 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.
exec /bin/sh $0 ${args} --no-create exec /bin/sh "$0" ${args} --no-create
EOF EOF
chmod +x config.status chmod +x config.status
fi fi
@ -165,14 +167,15 @@ echo "CC = ${CC}"
echo "CPPFLAGS = ${CPPFLAGS}" echo "CPPFLAGS = ${CPPFLAGS}"
echo "CFLAGS = ${CFLAGS}" echo "CFLAGS = ${CFLAGS}"
echo "LDFLAGS = ${LDFLAGS}" echo "LDFLAGS = ${LDFLAGS}"
echo "MAKEINFO = ${MAKEINFO}"
rm -f Makefile rm -f Makefile
cat > Makefile << EOF cat > Makefile << EOF
# Makefile for Xlunzip - Test tool for the lunzip linux module # Makefile for Xlunzip - Test tool for the lzip_decompress linux module
# Copyright (C) 2016-2018 Antonio Diaz Diaz. # Copyright (C) 2016-2025 Antonio Diaz Diaz.
# This file was generated automatically by configure. Don't edit. # This file was generated automatically by configure. Don't edit.
# #
# This Makefile is free software: you have unlimited permission # This Makefile is free software: you have unlimited permission
# to copy, distribute and modify it. # to copy, distribute, and modify it.
pkgname = ${pkgname} pkgname = ${pkgname}
pkgversion = ${pkgversion} pkgversion = ${pkgversion}
@ -188,6 +191,7 @@ CC = ${CC}
CPPFLAGS = ${CPPFLAGS} CPPFLAGS = ${CPPFLAGS}
CFLAGS = ${CFLAGS} CFLAGS = ${CFLAGS}
LDFLAGS = ${LDFLAGS} LDFLAGS = ${LDFLAGS}
MAKEINFO = ${MAKEINFO}
EOF EOF
cat "${srcdir}/Makefile.in" >> Makefile cat "${srcdir}/Makefile.in" >> Makefile

View file

@ -1,14 +1,14 @@
/* /*
* Wrapper for decompressing LZIP-compressed kernel, initramfs, and initrd * Wrapper for decompressing LZIP-compressed kernel, initramfs, and initrd
* *
* Copyright (C) 2016-2018 Antonio Diaz Diaz. * Copyright (C) 2016-2025 Antonio Diaz Diaz.
* *
* Licensed under GPLv2 or later, see file LICENSE in this source tree. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/ */
#ifdef STATIC #ifdef STATIC
#define PREBOOT #define PREBOOT
#include "lzip.c" #include "lzip_decompress.c"
#else #else
#include "linux_lzip.h" #include "linux_lzip.h"
#include "linux_lunzip.h" #include "linux_lunzip.h"
@ -62,16 +62,18 @@ STATIC int INIT __lunzip(unsigned char *inbuf, long in_len,
case LZIP_BAD_CRC: case LZIP_BAD_CRC:
error("CRC mismatch in LZIP-compressed data."); error("CRC mismatch in LZIP-compressed data.");
break; break;
default: case LZIP_EMPTY_MEMBER:
error("Bug in the LZIP decompressor."); error("Empty member in LZIP multimember data.");
break;
default: error("Bug in the LZIP decompressor.");
} }
return retval; return retval;
} }
#ifndef PREBOOT #ifndef PREBOOT
/* decompress_fn (see linux/decompress/generic.h) should have an out_size /* decompress_fn (see include/linux/decompress/generic.h) should have an
* argument to prevent overrunning outbuf in case of corruption of the * out_size argument to prevent overflowing outbuf in case of corruption
* compressed data. * of the compressed data.
*/ */
STATIC int INIT lunzip(unsigned char *inbuf, long in_len, STATIC int INIT lunzip(unsigned char *inbuf, long in_len,
long (*fill)(void*, unsigned long), long (*fill)(void*, unsigned long),

View file

@ -1,26 +1,26 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.46.1. .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.49.2.
.TH XLUNZIP "1" "July 2018" "xlunzip 0.3" "User Commands" .TH XLUNZIP "1" "January 2025" "xlunzip 0.9" "User Commands"
.SH NAME .SH NAME
xlunzip \- test tool for the lunzip linux module xlunzip \- test tool for the lzip_decompress linux module
.SH SYNOPSIS .SH SYNOPSIS
.B xlunzip .B xlunzip
[\fI\,options\/\fR] [\fI\,files\/\fR] [\fI\,options\/\fR] [\fI\,files\/\fR]
.SH DESCRIPTION .SH DESCRIPTION
Xlunzip is a test tool for the lzip decompression code of my lzip patch Xlunzip is a test tool for the lzip decompression code of my lzip patch for
for linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress linux
linux module as a backend. Xlunzip tests the module for stream, module as a backend. Xlunzip tests the module for stream, buffer\-to\-buffer,
buffer\-to\-buffer and mixed decompression modes, including in\-place and mixed decompression modes, including in\-place decompression (using the
decompression (using the same buffer for input and output). You can use same buffer for input and output). You can use xlunzip to check that the
xlunzip to verify that the module produces correct results when module produces correct results when decompressing single member files,
decompressing single member files, multimember files, or the multimember files, or the concatenation of two or more compressed files.
concatenation of two or more compressed files. Xlunzip can be used with Xlunzip can be used with unzcrash to test the robustness of the module to
unzcrash to test the robustness of the module to the decompression of the decompression of corrupted data.
corrupted data.
.PP .PP
Note that the in\-place decompression of concatenated files can't be The distributed index feature of the lzip format allows xlunzip to
guaranteed to work because an arbitrarily low compression ratio of the decompress concatenated files in place. This can't be guaranteed to work
last part of the data can be achieved by appending enough empty with formats like gzip or bzip2 because they can't detect whether a high
compressed members to a file. compression ratio in the first members of the multimember data is being
masked by a low compression ratio in the last members.
.SH OPTIONS .SH OPTIONS
.TP .TP
\fB\-h\fR, \fB\-\-help\fR \fB\-h\fR, \fB\-\-help\fR
@ -45,7 +45,7 @@ decompress or test using only one buffer
keep (don't delete) input files keep (don't delete) input files
.TP .TP
\fB\-o\fR, \fB\-\-output=\fR<file> \fB\-o\fR, \fB\-\-output=\fR<file>
if reading standard input, write to <file> write to <file>, keep input files
.TP .TP
\fB\-q\fR, \fB\-\-quiet\fR \fB\-q\fR, \fB\-\-quiet\fR
suppress all messages suppress all messages
@ -74,16 +74,19 @@ from standard input to standard output.
Numbers may be followed by a multiplier: k = kB = 10^3 = 1000, Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,
Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc... Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...
.PP .PP
Exit status: 0 for a normal exit, 1 for environmental problems (file To extract all the files from archive 'foo.tar.lz', use the commands
not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or \&'tar \fB\-xf\fR foo.tar.lz' or 'xlunzip \fB\-cd\fR foo.tar.lz | tar \fB\-xf\fR \-'.
invalid input file, 3 for an internal consistency error (eg, bug) which .PP
caused xlunzip to panic. Exit status: 0 for a normal exit, 1 for environmental problems
(file not found, invalid command\-line options, I/O errors, etc), 2 to
indicate a corrupt or invalid input file, 3 for an internal consistency
error (e.g., bug) which caused xlunzip to panic.
.SH "REPORTING BUGS" .SH "REPORTING BUGS"
Report bugs to lzip\-bug@nongnu.org Report bugs to lzip\-bug@nongnu.org
.br .br
Xlunzip home page: http://www.nongnu.org/lzip/xlunzip.html Xlunzip home page: http://www.nongnu.org/lzip/xlunzip.html
.SH COPYRIGHT .SH COPYRIGHT
Copyright \(co 2018 Antonio Diaz Diaz. Copyright \(co 2025 Antonio Diaz Diaz.
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html> License GPLv2+: GNU GPL version 2 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.

View file

@ -1,18 +1,18 @@
/* Xlunzip - Test tool for the lunzip linux module /* Xlunzip - Test tool for the lzip_decompress linux module
Copyright (C) 2016-2018 Antonio Diaz Diaz. Copyright (C) 2016-2025 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
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
@ -30,10 +30,10 @@
#include "lzip.h" #include "lzip.h"
/* Returns the number of bytes really read. /* Return the number of bytes really read.
If (returned value < size) and (errno == 0), means EOF was reached. If (value returned < size) and (errno == 0), means EOF was reached.
*/ */
long readblock( const int fd, uint8_t * const buf, const long size ) static long readblock( const int fd, uint8_t * const buf, const long size )
{ {
long sz = 0; long sz = 0;
errno = 0; errno = 0;
@ -49,36 +49,33 @@ long readblock( const int fd, uint8_t * const buf, const long size )
} }
/* Returns the address of a malloc'd buffer containing the file data and /* Return the address of a malloc'd buffer containing the file data and
the buffer and file sizes in '*buffer_sizep' and '*file_sizep'. the buffer and file sizes in '*buffer_sizep' and '*file_sizep'.
In case of error, returns 0 and does not modify '*size'. In case of error, return 0 and do not modify '*sizep'.
*/ */
uint8_t * read_file( const int infd, long * const buffer_sizep, static uint8_t * read_file( const int infd, long * const buffer_sizep,
long * const file_sizep, struct Pretty_print * const pp ) long * const file_sizep, const char * const filename )
{ {
long buffer_size = 1 << 20; long buffer_size = 1 << 20;
uint8_t * buffer = (uint8_t *)malloc( buffer_size ); uint8_t * buffer = (uint8_t *)malloc( buffer_size );
if( !buffer ) if( !buffer ) { show_file_error( filename, mem_msg, 0 ); return 0; }
{ show_file_error( pp->name, "Not enough memory.", 0 ); return 0; }
long file_size = readblock( infd, buffer, buffer_size ); long file_size = readblock( infd, buffer, buffer_size );
while( file_size >= buffer_size && !errno ) while( file_size >= buffer_size && !errno )
{ {
if( buffer_size >= LONG_MAX ) if( buffer_size >= LONG_MAX )
{ show_file_error( pp->name, "File is too large.", 0 ); free( buffer ); { show_file_error( filename, "Input file is too large.", 0 );
return 0; } free( buffer ); return 0; }
buffer_size = ( buffer_size <= LONG_MAX / 2 ) ? 2 * buffer_size : LONG_MAX; buffer_size = ( buffer_size <= LONG_MAX / 2 ) ? 2 * buffer_size : LONG_MAX;
uint8_t * const tmp = (uint8_t *)realloc( buffer, buffer_size ); uint8_t * const tmp = (uint8_t *)realloc( buffer, buffer_size );
if( !tmp ) if( !tmp )
{ show_file_error( pp->name, "Not enough memory.", 0 ); free( buffer ); { show_file_error( filename, mem_msg, 0 ); free( buffer ); return 0; }
return 0; }
buffer = tmp; buffer = tmp;
file_size += readblock( infd, buffer + file_size, buffer_size - file_size ); file_size += readblock( infd, buffer + file_size, buffer_size - file_size );
} }
if( errno ) if( errno )
{ show_file_error( pp->name, "Error reading file", errno ); free( buffer ); { show_file_error( filename, "Error reading file", errno ); free( buffer );
return 0; } return 0; }
close( infd );
*buffer_sizep = buffer_size; *buffer_sizep = buffer_size;
*file_sizep = file_size; *file_sizep = file_size;
return buffer; return buffer;
@ -87,104 +84,114 @@ uint8_t * read_file( const int infd, long * const buffer_sizep,
struct File_sizes struct File_sizes
{ {
long long csize; unsigned long long csize; /* compressed size */
long long dsize; unsigned long long dsize; /* decompressed size */
long trailing; unsigned long tsize; /* trailing data size */
long members;
}; };
const char * set_file_sizes( struct File_sizes * const file_sizes, static const char * set_file_sizes( struct File_sizes * const file_sizes,
const uint8_t * const buffer, const long file_size ) const uint8_t * const buffer,
const unsigned long file_size )
{ {
if( file_size < min_member_size ) return "Input file is too short."; if( file_size <= Lh_size ) return "File ends unexpectedly at member header.";
const Lzip_header * header = (Lzip_header *)buffer; if( file_size < min_member_size ) return "Input file is truncated.";
if( !Lh_verify_magic( *header ) ) const Lzip_header * header = (const Lzip_header *)buffer;
if( !Lh_check_magic( *header ) )
return "Bad magic number (file not in lzip format)."; return "Bad magic number (file not in lzip format).";
if( !Lh_verify_version( *header ) ) if( !Lh_check_version( *header ) )
return "Version of lzip member format not supported."; return "Version of lzip member format not supported.";
file_sizes->csize = file_sizes->dsize = file_sizes->trailing = 0; file_sizes->csize = file_sizes->dsize = file_sizes->tsize = 0;
long long pos = file_size; /* always points to a header or to EOF */ file_sizes->members = 0;
unsigned long pos = file_size; /* always points to a header or to EOF */
while( pos >= min_member_size ) while( pos >= min_member_size )
{ {
const Lzip_trailer * const trailer = const Lzip_trailer * const trailer =
(Lzip_trailer *)( buffer + pos - Lt_size ); (const Lzip_trailer *)( buffer + pos - Lt_size );
const long long member_size = Lt_get_member_size( *trailer ); const unsigned long long member_size = Lt_get_member_size( *trailer );
if( member_size < min_member_size || member_size > pos ) if( member_size < min_member_size || member_size > pos )
{ {
if( file_sizes->csize == 0 ) { --pos; continue; } /* maybe trailing data */ if( file_sizes->csize == 0 ) /* maybe trailing data */
{
if( member_size == 0 ) /* skip trailing zeros */
while( pos > Lt_size && buffer[pos-8] == 0 ) --pos;
else --pos;
continue;
}
return "Member size in trailer is corrupt."; return "Member size in trailer is corrupt.";
} }
header = (Lzip_header *)( buffer + pos - member_size ); header = (const Lzip_header *)( buffer + pos - member_size );
if( !Lh_verify_magic( *header ) || !Lh_verify_version( *header ) ) if( !Lh_check_magic( *header ) || !Lh_check_version( *header ) )
{ {
if( file_sizes->csize == 0 ) { --pos; continue; } /* maybe trailing data */ if( file_sizes->csize == 0 ) { --pos; continue; } /* maybe trailing data */
return "Bad member header inside file."; return "Bad member header inside file.";
} }
if( file_sizes->csize == 0 && file_size - pos > 0 ) if( file_sizes->csize == 0 && file_size - pos > 0 )
{ {
file_sizes->trailing = file_size - pos; file_sizes->tsize = file_size - pos;
header = (Lzip_header *)( buffer + pos ); header = (const Lzip_header *)( buffer + pos );
if( file_size - pos > Lh_size && if( file_size - pos > Lh_size &&
Lh_verify_magic( *header ) && Lh_verify_version( *header ) ) Lh_check_magic( *header ) && Lh_check_version( *header ) )
return "Last member in input file is truncated or corrupt."; return "Last member in input file is truncated or corrupt.";
} }
pos -= member_size; pos -= member_size;
file_sizes->csize += member_size; file_sizes->csize += member_size;
file_sizes->dsize += Lt_get_data_size( *trailer ); file_sizes->dsize += Lt_get_data_size( *trailer );
++file_sizes->members;
} }
if( pos != 0 || file_sizes->csize == 0 ) return "Can't get file sizes."; if( pos != 0 || file_sizes->csize == 0 ) return "Can't get file sizes.";
if( file_sizes->csize + file_sizes->trailing != file_size ) if( file_sizes->csize + file_sizes->tsize != file_size )
return "Error getting file sizes."; return "Error getting file sizes.";
if( file_sizes->csize > LONG_MAX ) return "File is larger than LONG_MAX.";
if( file_sizes->dsize > LONG_MAX ) return "Data is larger than LONG_MAX.";
return 0; return 0;
} }
const char * global_name; static const char * global_name; /* copy of filename for 'error' */
static void error(char *x) { show_file_error( global_name, x, 0 ); } static void error(char *x) { show_file_error( global_name, x, 0 ); }
/* /*
* Load the compressed file at the end of the buffer used to hold the * Load the compressed file at the end of the buffer used to hold the
* decompressed data. Verify that the in-place decompression does not * decompressed data. Check that the in-place decompression does not
* overwrite the compressed data. * overwrite the compressed data. The buffer must be large enough to contain
* after the decompressed data extra space for a marker, a trailer, the
* maximum possible data expansion, and (if multimember) N-1 empty members.
* *
* |----- compressed data ------| * |------ compressed data ------|
* V V * V V
* |---------------|-------------------|--------| * |----------------|-------------------|---------|
* ^ ^ * ^ ^ extra
* |------- decompressed data ---------| * |-------- decompressed data ---------|
*/ */
int decompress_in_place( const int infd, struct Pretty_print * const pp, int decompress_in_place( const int infd, Pretty_print * const pp,
const bool testing ) const bool testing )
{ {
long buffer_size = 0, file_size = 0; long buffer_size = 0, file_size = 0;
uint8_t * buffer = read_file( infd, &buffer_size, &file_size, pp ); uint8_t * buffer = read_file( infd, &buffer_size, &file_size, pp->name );
if( !buffer ) return 1; if( !buffer ) return 1;
struct File_sizes file_sizes; struct File_sizes file_sizes;
const char * emsg = set_file_sizes( &file_sizes, buffer, file_size ); const char * emsg = set_file_sizes( &file_sizes, buffer, file_size );
if( emsg ) { show_file_error( pp->name, emsg, 0 ); return 2; } if( emsg ) { show_file_error( pp->name, emsg, 0 ); return 2; }
const long long csize = file_sizes.csize; const long csize = file_sizes.csize;
const long long dsize = file_sizes.dsize; const long dsize = file_sizes.dsize;
/* const long trailing = file_sizes.trailing; */ /* const long extra_bytes = ( dsize >> 8 ) + 65536; wrong linux formula */
if( csize <= 0 || csize > LONG_MAX ) const long extra_bytes = ( csize >> 6 ) + file_sizes.members * min_member_size;
{ show_file_error( pp->name, "File is larger than LONG_MAX.", 0 ); const long long target_buffer_size = max( dsize, csize ) + extra_bytes;
return 2; } if( target_buffer_size > LONG_MAX )
if( dsize < 0 || dsize > LONG_MAX ) { show_file_error( pp->name, "Buffer is larger than LONG_MAX.", 0 );
{ show_file_error( pp->name, "Data is larger than LONG_MAX.", 0 ); return 1; }
return 2; } if( buffer_size < target_buffer_size ) /* avoid realloc if big enough */
/* ( (csize-36+63) >> 6 ) + 36 never failed with single member */
const long rextra = ( csize >> 5 ) + 72;
if( buffer_size < dsize + rextra ) /* avoid realloc if big enough */
{ {
buffer_size = dsize + rextra; buffer = (uint8_t *)realloc( buffer, target_buffer_size );
buffer = (uint8_t *)realloc( buffer, buffer_size ); if( !buffer ) { show_file_error( pp->name, mem_msg, 0 ); return 1; }
if( !buffer )
{ show_file_error( pp->name, "Not enough memory.", 0 ); return 1; }
} }
else buffer_size = max( dsize + rextra, csize ); buffer_size = target_buffer_size;
const long cbegin = buffer_size - csize; const long cbegin = buffer_size - csize; /* overwrite trailing data */
if( cbegin > 0 ) memmove( buffer + cbegin, buffer, csize ); if( cbegin > 0 ) memmove( buffer + cbegin, buffer, csize );
long in_pos, out_pos; long in_pos, out_pos;
@ -200,21 +207,6 @@ int decompress_in_place( const int infd, struct Pretty_print * const pp,
} }
free( buffer ); free( buffer );
if( retval ) return retval; if( retval ) return retval;
if( verbosity >= 1 ) Pp_show_msg( pp, 0 ); show_results( pp, in_pos, out_pos, testing );
if( verbosity >= 2 )
{
if( out_pos <= 0 || in_pos <= 0 )
fputs( "no data compressed. ", stderr );
else
fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ",
(double)out_pos / in_pos,
( 100.0 * in_pos ) / out_pos,
100.0 - ( ( 100.0 * in_pos ) / out_pos ) );
if( verbosity >= 3 )
fprintf( stderr, "decompressed %9lu, compressed %8lu. ",
out_pos, in_pos );
}
if( verbosity >= 1 )
fputs( testing ? "ok\n" : "done\n", stderr );
return 0; return 0;
} }

View file

@ -9,10 +9,15 @@ int lunzip(unsigned char *inbuf, long in_len,
long *in_posp, long *in_posp,
void (*error)(char *x)); void (*error)(char *x));
/* This internal function is required because the decompress_fn above
* (see include/linux/decompress/generic.h) should have an out_size
* argument to prevent overflowing outbuf in case of corruption of the
* compressed data.
*/
int __lunzip(unsigned char *inbuf, long in_len, int __lunzip(unsigned char *inbuf, long in_len,
long (*fill)(void*, unsigned long), long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long), long (*flush)(void*, unsigned long),
unsigned char *outbuf, long out_size, unsigned char *outbuf, long out_size,
long *in_posp, long *out_posp, long *in_posp, long *out_posp,
void (*error)(char *x)); void (*error)(char *x));
#endif #endif

View file

@ -4,7 +4,7 @@
/* /*
* LZIP decompressor * LZIP decompressor
* *
* Copyright (C) 2016-2018 Antonio Diaz Diaz. * Copyright (C) 2016-2025 Antonio Diaz Diaz.
*/ */
/* Return values (< 0 = Error) */ /* Return values (< 0 = Error) */
@ -20,7 +20,8 @@ enum {
LZIP_WRITE_ERROR = -9, LZIP_WRITE_ERROR = -9,
LZIP_BAD_DATA = -10, LZIP_BAD_DATA = -10,
LZIP_DATA_EOF = -11, LZIP_DATA_EOF = -11,
LZIP_BAD_CRC = -12 LZIP_BAD_CRC = -12,
LZIP_EMPTY_MEMBER = -13
}; };
int lzip_decompress(unsigned char *inbuf, long in_len, int lzip_decompress(unsigned char *inbuf, long in_len,

8
linux_module.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef _LINUX_MODULE_H
#define _LINUX_MODULE_H
#define MODULE_LICENSE(_license)
#define MODULE_AUTHOR(_author)
#define MODULE_DESCRIPTION(_description)
#endif /* _LINUX_MODULE_H */

144
lzip.h
View file

@ -1,18 +1,18 @@
/* Xlunzip - Test tool for the lunzip linux module /* Xlunzip - Test tool for the lzip_decompress linux module
Copyright (C) 2016-2018 Antonio Diaz Diaz. Copyright (C) 2016-2025 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
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef max #ifndef max
@ -22,129 +22,43 @@
#define min(x,y) ((x) <= (y) ? (x) : (y)) #define min(x,y) ((x) <= (y) ? (x) : (y))
#endif #endif
void * resize_buffer( void * buf, const unsigned min_size );
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,
min_member_size = 36 }; min_member_size = 36 };
/* defined in main.c */ typedef struct Pretty_print
extern int verbosity;
struct Pretty_print
{ {
const char * name; const char * name;
char * padded_name; char * padded_name;
const char * stdin_name; const char * stdin_name;
unsigned longest_name; unsigned longest_name;
bool first_post; bool first_post;
}; } Pretty_print;
static inline 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->padded_name = 0;
pp->stdin_name = "(stdin)";
pp->longest_name = 0;
pp->first_post = false;
if( verbosity <= 0 ) return;
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,
const char * const filename )
{
unsigned name_len, padded_name_len, i = 0;
if( filename && filename[0] && strcmp( filename, "-" ) != 0 )
pp->name = filename;
else pp->name = pp->stdin_name;
name_len = strlen( pp->name );
padded_name_len = max( name_len, pp->longest_name ) + 4;
pp->padded_name = resize_buffer( pp->padded_name, padded_name_len + 1 );
while( i < 2 ) pp->padded_name[i++] = ' ';
while( i < name_len + 2 ) { pp->padded_name[i] = pp->name[i-2]; ++i; }
pp->padded_name[i++] = ':';
while( i < padded_name_len ) pp->padded_name[i++] = ' ';
pp->padded_name[i] = 0;
pp->first_post = true;
}
static inline void Pp_reset( struct Pretty_print * const pp )
{ if( pp->name && pp->name[0] ) pp->first_post = true; }
void Pp_show_msg( struct Pretty_print * const pp, const char * const msg );
static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */ static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */
/* 4 version */
/* 5 coded_dict_size */
enum { Lh_size = 6 }; enum { Lh_size = 6 };
typedef uint8_t Lzip_header[Lh_size]; /* 0-3 magic bytes */
/* 4 version */
/* 5 coded dictionary size */
static inline bool Lh_verify_magic( const Lzip_header data ) static inline bool Lh_check_magic( const Lzip_header data )
{ return ( memcmp( data, lzip_magic, 4 ) == 0 ); } { return memcmp( data, lzip_magic, 4 ) == 0; }
/* detect (truncated) header */ static inline bool Lh_check_version( const Lzip_header data )
static inline bool Lh_verify_prefix( const Lzip_header data, const int sz ) { return data[4] == 1; }
{
int i; for( i = 0; i < sz && i < 4; ++i )
if( data[i] != lzip_magic[i] ) return false;
return ( sz > 0 );
}
/* detect corrupt header */
static inline bool Lh_verify_corrupt( const Lzip_header data )
{
int matches = 0;
int i; for( i = 0; i < 4; ++i )
if( data[i] == lzip_magic[i] ) ++matches;
return ( matches > 1 && matches < 4 );
}
static inline uint8_t Lh_version( const Lzip_header data )
{ return data[4]; }
static inline bool Lh_verify_version( const Lzip_header data )
{ return ( data[4] == 1 ); }
static inline unsigned Lh_get_dictionary_size( const Lzip_header data )
{
unsigned sz = ( 1 << ( data[5] & 0x1F ) );
if( sz > min_dictionary_size )
sz -= ( sz / 16 ) * ( ( data[5] >> 5 ) & 7 );
return sz;
}
typedef uint8_t Lzip_trailer[20]; enum { Lt_size = 20 };
typedef uint8_t Lzip_trailer[Lt_size];
/* 0-3 CRC32 of the uncompressed data */ /* 0-3 CRC32 of the uncompressed data */
/* 4-11 size of the uncompressed data */ /* 4-11 size of the uncompressed data */
/* 12-19 member size including header and trailer */ /* 12-19 member size including header and trailer */
enum { Lt_size = 20 };
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 unsigned long long Lt_get_data_size( const Lzip_trailer data ) static inline unsigned long long Lt_get_data_size( const Lzip_trailer data )
{ {
@ -161,13 +75,19 @@ static inline unsigned long long Lt_get_member_size( const Lzip_trailer data )
} }
static inline void set_retval( int * retval, const int new_val )
{ if( *retval < new_val ) *retval = new_val; }
static const char * const mem_msg = "Not enough memory.";
/* defined in in_place.c */ /* defined in in_place.c */
int decompress_in_place( const int infd, struct Pretty_print * const pp, int decompress_in_place( const int infd, Pretty_print * const pp,
const bool testing ); const bool testing );
/* defined in main.c */ /* defined in main.c */
int convert_retval( const int retval ); int convert_retval( const int retval );
long flush( void * buf, unsigned long size ); long flush( void * buf, unsigned long size );
void show_error( const char * const msg, const int errcode, const bool help ); void show_results( Pretty_print * const pp, const long in_pos,
const long out_pos, const bool testing );
void show_file_error( const char * const filename, const char * const msg, void show_file_error( const char * const filename, const char * const msg,
const int errcode ); const int errcode );

View file

@ -1,17 +1,18 @@
/* /*
* LZIP decompressor * LZIP decompressor
* *
* Copyright (C) 2016-2018 Antonio Diaz Diaz. * Copyright (C) 2016-2025 Antonio Diaz Diaz.
* *
* Licensed under GPLv2 or later, see file LICENSE in this source tree. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/ */
#include "linux_module.h"
#include "linux_lzip.h" #include "linux_lzip.h"
#include "linux_mm.h" #include "linux_mm.h"
/* /*
* STATIC_RW_DATA is used in the pre-boot environment on some architectures. * STATIC_RW_DATA is used in the pre-boot environment on some architectures.
* See <linux/decompress/mm.h> for details. * See include/linux/decompress/mm.h for details.
*/ */
#ifndef STATIC_RW_DATA #ifndef STATIC_RW_DATA
#define STATIC_RW_DATA static #define STATIC_RW_DATA static
@ -31,23 +32,23 @@ static inline State St_set_char(const State st)
static inline State St_set_match(const State st) static inline State St_set_match(const State st)
{ {
return ((st < 7) ? 7 : 10); return (st < 7) ? 7 : 10;
} }
static inline State St_set_rep(const State st) static inline State St_set_rep(const State st)
{ {
return ((st < 7) ? 8 : 11); return (st < 7) ? 8 : 11;
} }
static inline State St_set_short_rep(const State st) static inline State St_set_shortrep(const State st)
{ {
return ((st < 7) ? 9 : 11); return (st < 7) ? 9 : 11;
} }
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,
@ -83,7 +84,7 @@ static inline int get_len_state(const int len)
static inline int get_lit_state(const uint8_t prev_byte) static inline int get_lit_state(const uint8_t prev_byte)
{ {
return (prev_byte >> (8 - literal_context_bits)); return prev_byte >> (8 - literal_context_bits);
} }
@ -188,12 +189,12 @@ static inline void CRC32_update_buf(uint32_t * const crc,
STATIC_RW_DATA const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */ STATIC_RW_DATA const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */
/* 4 version */
/* 5 coded_dict_size */
enum { Lh_size = 6 }; enum { Lh_size = 6 };
typedef uint8_t Lzip_header[Lh_size]; /* 0-3 magic bytes */
/* 4 version */
/* 5 coded dictionary size */
static inline bool Lh_verify_magic(const Lzip_header data) static inline bool Lh_check_magic(const Lzip_header data)
{ {
int i; int i;
@ -204,34 +205,34 @@ static inline bool Lh_verify_magic(const Lzip_header data)
} }
/* detect (truncated) header */ /* detect (truncated) header */
static inline bool Lh_verify_prefix(const Lzip_header data, const int sz) static inline bool Lh_check_prefix(const Lzip_header data, const int sz)
{ {
int i; int i;
for (i = 0; i < sz && i < 4; ++i) for (i = 0; i < sz && i < 4; ++i)
if (data[i] != lzip_magic[i]) if (data[i] != lzip_magic[i])
return false; return false;
return (sz > 0); return sz > 0;
} }
/* detect corrupt header */ /* detect corrupt header */
static inline bool Lh_verify_corrupt(const Lzip_header data) static inline bool Lh_check_corrupt(const Lzip_header data)
{ {
int matches = 0; int matches = 0;
int i; int i;
for (i = 0; i < 4; ++i) for (i = 0; i < 4; ++i)
if (data[i] == lzip_magic[i]) if (data[i] == lzip_magic[i])
++matches; ++matches;
return (matches > 1 && matches < 4); return matches > 1 && matches < 4;
} }
static inline bool Lh_verify_version(const Lzip_header data) static inline bool Lh_check_version(const Lzip_header data)
{ {
return (data[4] == 1); return data[4] == 1;
} }
static inline unsigned Lh_get_dictionary_size(const Lzip_header data) static inline unsigned Lh_get_dictionary_size(const Lzip_header data)
{ {
unsigned sz = (1 << (data[5] & 0x1F)); unsigned sz = 1 << (data[5] & 0x1F);
if (sz > min_dictionary_size) if (sz > min_dictionary_size)
sz -= (sz / 16) * ((data[5] >> 5) & 7); sz -= (sz / 16) * ((data[5] >> 5) & 7);
@ -239,11 +240,11 @@ static inline unsigned Lh_get_dictionary_size(const Lzip_header data)
} }
typedef uint8_t Lzip_trailer[20]; enum { Lt_size = 20 };
typedef uint8_t Lzip_trailer[Lt_size];
/* 0-3 CRC32 of the uncompressed data */ /* 0-3 CRC32 of the uncompressed data */
/* 4-11 size of the uncompressed data */ /* 4-11 size of the uncompressed data */
/* 12-19 member size including header and trailer */ /* 12-19 member size including header and trailer */
enum { Lt_size = 20 };
static inline unsigned Lt_get_data_crc(const Lzip_trailer data) static inline unsigned Lt_get_data_crc(const Lzip_trailer data)
{ {
@ -358,14 +359,17 @@ static inline uint8_t Rd_get_byte(struct Range_decoder * const rdec)
return rdec->buffer[rdec->pos++]; return rdec->buffer[rdec->pos++];
} }
static inline void Rd_load(struct Range_decoder * const rdec) static inline bool Rd_load(struct Range_decoder * const rdec)
{ {
int i; int i;
rdec->code = 0; rdec->code = 0;
for (i = 0; i < 5; ++i)
rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
rdec->range = 0xFFFFFFFFU; rdec->range = 0xFFFFFFFFU;
/* check first byte of the LZMA stream */
if (Rd_get_byte(rdec) != 0)
return false;
for (i = 0; i < 4; ++i) rdec->code = (rdec->code << 8) | Rd_get_byte(rdec);
return true;
} }
static inline void Rd_normalize(struct Range_decoder * const rdec) static inline void Rd_normalize(struct Range_decoder * const rdec)
@ -390,7 +394,7 @@ static inline unsigned Rd_decode(struct Range_decoder * const rdec,
/* symbol <<= 1; */ /* symbol <<= 1; */
/* if(rdec->code >= rdec->range) { rdec->code -= rdec->range; symbol |= 1; } */ /* if(rdec->code >= rdec->range) { rdec->code -= rdec->range; symbol |= 1; } */
bit = (rdec->code >= rdec->range); bit = (rdec->code >= rdec->range);
symbol = (symbol << 1) + bit; symbol <<= 1; symbol += bit;
rdec->code -= rdec->range & (0U - bit); rdec->code -= rdec->range & (0U - bit);
} }
return symbol; return symbol;
@ -418,9 +422,8 @@ static inline unsigned Rd_decode_bit(struct Range_decoder * const rdec,
static inline unsigned Rd_decode_tree3(struct Range_decoder * const rdec, static inline unsigned Rd_decode_tree3(struct Range_decoder * const rdec,
Bit_model bm[]) Bit_model bm[])
{ {
unsigned symbol = 1; unsigned symbol = 2 | Rd_decode_bit(rdec, &bm[1]);
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 & 7; return symbol & 7;
@ -429,14 +432,13 @@ static inline unsigned Rd_decode_tree3(struct Range_decoder * const rdec,
static inline unsigned Rd_decode_tree6(struct Range_decoder * const rdec, static inline unsigned Rd_decode_tree6(struct Range_decoder * const rdec,
Bit_model bm[]) Bit_model bm[])
{ {
unsigned symbol = 1; unsigned symbol = 2 | Rd_decode_bit(rdec, &bm[1]);
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]);
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 & 0x3F; return symbol & 0x3F;
} }
@ -462,7 +464,7 @@ Rd_decode_tree_reversed(struct Range_decoder * const rdec,
for (i = 0; i < num_bits; ++i) { for (i = 0; i < num_bits; ++i) {
const unsigned bit = Rd_decode_bit(rdec, &bm[model]); const unsigned bit = Rd_decode_bit(rdec, &bm[model]);
model = (model << 1) + bit; model <<= 1; model += bit;
symbol |= (bit << i); symbol |= (bit << i);
} }
return symbol; return symbol;
@ -472,13 +474,10 @@ static inline unsigned
Rd_decode_tree_reversed4(struct Range_decoder * const rdec, Bit_model bm[]) Rd_decode_tree_reversed4(struct Range_decoder * const rdec, Bit_model bm[])
{ {
unsigned symbol = Rd_decode_bit(rdec, &bm[1]); unsigned symbol = Rd_decode_bit(rdec, &bm[1]);
unsigned model = 2 + symbol;
unsigned bit = Rd_decode_bit(rdec, &bm[model]);
model = (model << 1) + bit; symbol |= (bit << 1); symbol += Rd_decode_bit(rdec, &bm[2+symbol]) << 1;
bit = Rd_decode_bit(rdec, &bm[model]); symbol += Rd_decode_bit(rdec, &bm[4+symbol]) << 2;
model = (model << 1) + bit; symbol |= (bit << 2); symbol += Rd_decode_bit(rdec, &bm[8+symbol]) << 3;
symbol |= (Rd_decode_bit(rdec, &bm[model]) << 3);
return symbol; return symbol;
} }
@ -492,7 +491,7 @@ static inline unsigned Rd_decode_matched(struct Range_decoder * const rdec,
const unsigned match_bit = (match_byte <<= 1) & mask; const unsigned match_bit = (match_byte <<= 1) & mask;
const unsigned bit = Rd_decode_bit(rdec, &bm[symbol+match_bit+mask]); const unsigned bit = Rd_decode_bit(rdec, &bm[symbol+match_bit+mask]);
symbol = (symbol << 1) + bit; symbol <<= 1; symbol += bit;
if (symbol > 0xFF) if (symbol > 0xFF)
return symbol & 0xFF; return symbol & 0xFF;
mask &= ~(match_bit ^ (bit << 8)); /* if( match_bit != bit ) mask = 0; */ mask &= ~(match_bit ^ (bit << 8)); /* if( match_bit != bit ) mask = 0; */
@ -504,11 +503,12 @@ static inline unsigned Rd_decode_len(struct Range_decoder * const rdec,
const int pos_state) const int pos_state)
{ {
if (Rd_decode_bit(rdec, &lm->choice1) == 0) if (Rd_decode_bit(rdec, &lm->choice1) == 0)
return Rd_decode_tree3(rdec, lm->bm_low[pos_state]); return min_match_len +
Rd_decode_tree3(rdec, lm->bm_low[pos_state]);
if (Rd_decode_bit(rdec, &lm->choice2) == 0) if (Rd_decode_bit(rdec, &lm->choice2) == 0)
return len_low_symbols + return min_match_len + len_low_symbols +
Rd_decode_tree3(rdec, lm->bm_mid[pos_state]); Rd_decode_tree3(rdec, lm->bm_mid[pos_state]);
return len_low_symbols + len_mid_symbols + return min_match_len + len_low_symbols + len_mid_symbols +
Rd_decode_tree8(rdec, lm->bm_high); Rd_decode_tree8(rdec, lm->bm_high);
} }
@ -637,7 +637,7 @@ static inline bool LZd_init(struct LZ_decoder * const d,
Lm_init(&d->rep_len_model); Lm_init(&d->rep_len_model);
d->buffer_given = (outbuf && out_size > 0); d->buffer_given = (outbuf && out_size > 0);
d->buffer_size = d->buffer_given ? out_size : dict_size; d->buffer_size = d->buffer_given ? (unsigned long)out_size : dict_size;
d->dictionary_size = min_t(unsigned long, d->buffer_size, dict_size); d->dictionary_size = min_t(unsigned long, d->buffer_size, dict_size);
d->buffer = d->buffer_given ? outbuf : large_malloc(d->buffer_size); d->buffer = d->buffer_given ? outbuf : large_malloc(d->buffer_size);
if (!d->buffer) if (!d->buffer)
@ -672,7 +672,7 @@ LZd_data_position(const struct LZ_decoder * const d)
} }
static bool LZd_verify_trailer(struct LZ_decoder * const d) static bool LZd_check_trailer(struct LZ_decoder * const d)
{ {
Lzip_trailer trailer; Lzip_trailer trailer;
int i = 0; int i = 0;
@ -680,13 +680,13 @@ static bool LZd_verify_trailer(struct LZ_decoder * const d)
while (i < Lt_size) while (i < Lt_size)
trailer[i++] = Rd_get_byte(d->rdec); trailer[i++] = Rd_get_byte(d->rdec);
return (Lt_get_data_crc(trailer) == LZd_crc(d) && return Lt_get_data_crc(trailer) == LZd_crc(d) &&
Lt_get_data_size(trailer) == LZd_data_position(d) && Lt_get_data_size(trailer) == LZd_data_position(d) &&
Lt_get_member_size(trailer) == Rd_member_position(d->rdec)); Lt_get_member_size(trailer) == Rd_member_position(d->rdec);
} }
/* Return value: 0 = OK, < 0 = error (see <linux/lzip.h>). */ /* Return value: 0 = OK, < 0 = error (see include/linux/lzip.h). */
static int LZd_decode_member(struct LZ_decoder * const d) static int LZd_decode_member(struct LZ_decoder * const d)
{ {
struct Range_decoder * const rdec = d->rdec; struct Range_decoder * const rdec = d->rdec;
@ -696,7 +696,8 @@ static int LZd_decode_member(struct LZ_decoder * const d)
unsigned rep3 = 0; unsigned rep3 = 0;
State state = 0; State state = 0;
Rd_load(rdec); if (!Rd_load(rdec))
return LZIP_BAD_DATA;
while (!Rd_finished(rdec)) { while (!Rd_finished(rdec)) {
int len; int len;
const int pos_state = LZd_data_position(d) & pos_state_mask; const int pos_state = LZd_data_position(d) & pos_state_mask;
@ -718,7 +719,7 @@ static int LZd_decode_member(struct LZ_decoder * const d)
if (Rd_decode_bit(rdec, &d->bm_rep[state]) != 0) { if (Rd_decode_bit(rdec, &d->bm_rep[state]) != 0) {
if (Rd_decode_bit(rdec, &d->bm_rep0[state]) == 0) { if (Rd_decode_bit(rdec, &d->bm_rep0[state]) == 0) {
if (Rd_decode_bit(rdec, &d->bm_len[state][pos_state]) == 0) { if (Rd_decode_bit(rdec, &d->bm_len[state][pos_state]) == 0) {
state = St_set_short_rep(state); state = St_set_shortrep(state);
LZd_put_byte(d, LZd_peek(d, rep0)); LZd_put_byte(d, LZd_peek(d, rep0));
continue; continue;
} }
@ -740,44 +741,37 @@ static int LZd_decode_member(struct LZ_decoder * const d)
rep0 = distance; rep0 = distance;
} }
state = St_set_rep(state); state = St_set_rep(state);
len = min_match_len + Rd_decode_len(rdec, &d->rep_len_model, pos_state); len = Rd_decode_len(rdec, &d->rep_len_model, pos_state);
} else { /* match */ } else { /* match */
unsigned distance; rep3 = rep2; rep2 = rep1; rep1 = rep0;
len = Rd_decode_len(rdec, &d->match_len_model, pos_state);
len = min_match_len + Rd_decode_len(rdec, &d->match_len_model, pos_state); rep0 = Rd_decode_tree6(rdec, d->bm_dis_slot[get_len_state(len)]);
distance = Rd_decode_tree6(rdec, d->bm_dis_slot[get_len_state(len)]); if (rep0 >= start_dis_model) {
if (distance >= start_dis_model) { const unsigned dis_slot = rep0;
const unsigned dis_slot = distance;
const int direct_bits = (dis_slot >> 1) - 1; const int direct_bits = (dis_slot >> 1) - 1;
distance = (2 | (dis_slot & 1)) << direct_bits; rep0 = (2 | (dis_slot & 1)) << direct_bits;
if (dis_slot < end_dis_model) if (dis_slot < end_dis_model)
distance += Rd_decode_tree_reversed(rdec, rep0 += Rd_decode_tree_reversed(rdec,
d->bm_dis + (distance - dis_slot), direct_bits); d->bm_dis + (rep0 - dis_slot), direct_bits);
else { else {
distance += rep0 += Rd_decode(rdec, direct_bits - dis_align_bits) << dis_align_bits;
Rd_decode(rdec, direct_bits - dis_align_bits) << dis_align_bits; rep0 += Rd_decode_tree_reversed4(rdec, d->bm_align);
distance += Rd_decode_tree_reversed4(rdec, d->bm_align); if (rep0 == 0xFFFFFFFFU) { /* marker found */
if (distance == 0xFFFFFFFFU) { /* marker found */
Rd_normalize(rdec); Rd_normalize(rdec);
LZd_flush_data(d); LZd_flush_data(d);
if (d->write_error) if (d->write_error)
return LZIP_WRITE_ERROR; return LZIP_WRITE_ERROR;
if (len == min_match_len) { /* End Of Stream marker */ if (len == min_match_len) { /* End Of Stream marker */
if (LZd_verify_trailer(d)) if (LZd_check_trailer(d))
return 0; return 0;
else else
return LZIP_BAD_CRC; return LZIP_BAD_CRC;
} }
if (len == min_match_len + 1) { /* Sync Flush marker */
Rd_load(rdec);
continue;
}
return LZIP_BAD_DATA; /* unknown marker */ return LZIP_BAD_DATA; /* unknown marker */
} }
} }
} }
rep3 = rep2; rep2 = rep1; rep1 = rep0; rep0 = distance;
state = St_set_match(state); state = St_set_match(state);
if (rep0 >= d->dictionary_size || if (rep0 >= d->dictionary_size ||
(rep0 >= d->pos && !d->pos_wrapped)) { (rep0 >= d->pos && !d->pos_wrapped)) {
@ -803,6 +797,7 @@ int lzip_decompress(unsigned char *inbuf, long in_len,
struct LZ_decoder *decoder = 0; struct LZ_decoder *decoder = 0;
int retval = 0; int retval = 0;
bool first_member; bool first_member;
bool empty = false, multi = false;
if (in_posp) if (in_posp)
*in_posp = 0; *in_posp = 0;
@ -824,18 +819,18 @@ int lzip_decompress(unsigned char *inbuf, long in_len,
if (Rd_finished(&rdec)) { /* End Of File */ if (Rd_finished(&rdec)) { /* End Of File */
if (first_member) if (first_member)
retval = LZIP_HEADER1_EOF; retval = LZIP_HEADER1_EOF;
else if (Lh_verify_prefix(header, size)) else if (Lh_check_prefix(header, size))
retval = LZIP_HEADER2_EOF; retval = LZIP_HEADER2_EOF;
break; break;
} }
if (!Lh_verify_magic(header)) { if (!Lh_check_magic(header)) {
if (first_member) if (first_member)
retval = LZIP_BAD_MAGIC1; retval = LZIP_BAD_MAGIC1;
else if (Lh_verify_corrupt(header)) else if (Lh_check_corrupt(header))
retval = LZIP_BAD_MAGIC2; retval = LZIP_BAD_MAGIC2;
break; break;
} }
if (!Lh_verify_version(header)) { if (!Lh_check_version(header)) {
retval = LZIP_BAD_VERSION; retval = LZIP_BAD_VERSION;
break; break;
} }
@ -856,7 +851,9 @@ int lzip_decompress(unsigned char *inbuf, long in_len,
retval = LZd_decode_member(decoder); retval = LZd_decode_member(decoder);
if (in_posp) if (in_posp)
*in_posp += Rd_member_position(&rdec); *in_posp += Rd_member_position(&rdec);
multi = !first_member;
data_pos = LZd_data_position(decoder); data_pos = LZd_data_position(decoder);
if (data_pos == 0) empty = true;
if (outptr) if (outptr)
outptr += data_pos; outptr += data_pos;
if (out_posp) if (out_posp)
@ -870,12 +867,14 @@ int lzip_decompress(unsigned char *inbuf, long in_len,
if (decoder) if (decoder)
free(decoder); free(decoder);
Rd_free(&rdec); Rd_free(&rdec);
if (empty && multi && retval == 0)
retval = LZIP_EMPTY_MEMBER;
return retval; return retval;
} }
#ifndef STATIC #ifndef STATIC
EXPORT_SYMBOL_GPL(lzip_decompress); EXPORT_SYMBOL_GPL(lzip_decompress);
#endif
MODULE_DESCRIPTION("LZIP Decompressor"); MODULE_DESCRIPTION("LZIP Decompressor");
MODULE_AUTHOR("Antonio Diaz Diaz <antonio@gnu.org>"); MODULE_AUTHOR("Antonio Diaz Diaz <antonio@gnu.org>");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
#endif

589
main.c
View file

@ -1,24 +1,24 @@
/* Xlunzip - Test tool for the lunzip linux module /* Xlunzip - Test tool for the lzip_decompress linux module
Copyright (C) 2016-2018 Antonio Diaz Diaz. Copyright (C) 2016-2025 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
the Free Software Foundation, either version 2 of the License, or the Free Software Foundation, either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/* /*
Exit status: 0 for a normal exit, 1 for environmental problems Exit status: 0 for a normal exit, 1 for environmental problems
(file not found, invalid flags, I/O errors, etc), 2 to indicate a (file not found, invalid command-line options, I/O errors, etc), 2 to
corrupt or invalid input file, 3 for an internal consistency error indicate a corrupt or invalid input file, 3 for an internal consistency
(eg, bug) which caused xlunzip to panic. error (e.g., bug) which caused xlunzip to panic.
*/ */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
@ -26,18 +26,19 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h> /* CHAR_BIT, SSIZE_MAX */
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h> /* SIZE_MAX */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <utime.h> #include <utime.h>
#include <sys/stat.h> #include <sys/stat.h>
#if defined(__MSVCRT__) #if defined __MSVCRT__ || defined __OS2__ || defined __DJGPP__
#include <io.h> #include <io.h>
#if defined __MSVCRT__
#define fchmod(x,y) 0 #define fchmod(x,y) 0
#define fchown(x,y,z) 0 #define fchown(x,y,z) 0
#define SIGHUP SIGTERM #define SIGHUP SIGTERM
@ -49,8 +50,10 @@
#define S_IWOTH 0 #define S_IWOTH 0
#endif #endif
#endif #endif
#if defined(__OS2__) #if defined __DJGPP__
#include <io.h> #define S_ISSOCK(x) 0
#define S_ISVTX 0
#endif
#endif #endif
#include "carg_parser.h" #include "carg_parser.h"
@ -66,41 +69,48 @@
#error "Environments where CHAR_BIT != 8 are not supported." #error "Environments where CHAR_BIT != 8 are not supported."
#endif #endif
int verbosity = 0; #if ( defined SIZE_MAX && SIZE_MAX < ULONG_MAX ) || \
void cleanup_and_fail( const int retval ); ( defined SSIZE_MAX && SSIZE_MAX < LONG_MAX )
#error "Environments where 'size_t' is narrower than 'long' are not supported."
#endif
const char * const Program_name = "Xlunzip"; static int verbosity = 0;
const char * const program_name = "xlunzip"; static void cleanup_and_fail( const int retval );
const char * const program_year = "2018"; static void show_error( const char * const msg, const int errcode,
const char * invocation_name = 0; const bool help );
const struct { const char * from; const char * to; } known_extensions[] = { static const char * const program_name = "xlunzip";
static const char * const program_year = "2025";
static const char * invocation_name = "xlunzip"; /* default value */
static const struct { const char * from; const char * to; } known_extensions[] = {
{ ".lz", "" }, { ".lz", "" },
{ ".tlz", ".tar" }, { ".tlz", ".tar" },
{ 0, 0 } }; { 0, 0 } };
char * output_filename = 0; /* Variables used in signal handler context.
int infd = -1; /* needed by the fill function */ They are not declared volatile because the handler never returns. */
int outfd = -1; static char * output_filename = 0;
bool delete_output_on_interrupt = false; static int outfd = -1;
static bool delete_output_on_interrupt = false;
static void show_help( void ) static void show_help( void )
{ {
printf( "Xlunzip is a test tool for the lzip decompression code of my lzip patch\n" printf( "Xlunzip is a test tool for the lzip decompression code of my lzip patch for\n"
"for linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress\n" "linux. Xlunzip is similar to lunzip, but it uses the lzip_decompress linux\n"
"linux module as a backend. Xlunzip tests the module for stream,\n" "module as a backend. Xlunzip tests the module for stream, buffer-to-buffer,\n"
"buffer-to-buffer and mixed decompression modes, including in-place\n" "and mixed decompression modes, including in-place decompression (using the\n"
"decompression (using the same buffer for input and output). You can use\n" "same buffer for input and output). You can use xlunzip to check that the\n"
"xlunzip to verify that the module produces correct results when\n" "module produces correct results when decompressing single member files,\n"
"decompressing single member files, multimember files, or the\n" "multimember files, or the concatenation of two or more compressed files.\n"
"concatenation of two or more compressed files. Xlunzip can be used with\n" "Xlunzip can be used with unzcrash to test the robustness of the module to\n"
"unzcrash to test the robustness of the module to the decompression of\n" "the decompression of corrupted data.\n"
"corrupted data.\n" "\nThe distributed index feature of the lzip format allows xlunzip to\n"
"\nNote that the in-place decompression of concatenated files can't be\n" "decompress concatenated files in place. This can't be guaranteed to work\n"
"guaranteed to work because an arbitrarily low compression ratio of the\n" "with formats like gzip or bzip2 because they can't detect whether a high\n"
"last part of the data can be achieved by appending enough empty\n" "compression ratio in the first members of the multimember data is being\n"
"compressed members to a file.\n" "masked by a low compression ratio in the last members.\n"
"\nUsage: %s [options] [files]\n", invocation_name ); "\nUsage: %s [options] [files]\n", invocation_name );
printf( "\nOptions:\n" printf( "\nOptions:\n"
" -h, --help display this help and exit\n" " -h, --help display this help and exit\n"
@ -110,7 +120,7 @@ static void show_help( void )
" -f, --force overwrite existing output files\n" " -f, --force overwrite existing output files\n"
" -I, --in-place decompress or test using only one buffer\n" " -I, --in-place decompress or test using only one buffer\n"
" -k, --keep keep (don't delete) input files\n" " -k, --keep keep (don't delete) input files\n"
" -o, --output=<file> if reading standard input, write to <file>\n" " -o, --output=<file> write to <file>, keep input files\n"
" -q, --quiet suppress all messages\n" " -q, --quiet suppress all messages\n"
" -t, --test test compressed file integrity\n" " -t, --test test compressed file integrity\n"
" -v, --verbose be verbose (a 2nd -v gives more)\n" " -v, --verbose be verbose (a 2nd -v gives more)\n"
@ -119,14 +129,16 @@ static void show_help( void )
" --outsize[=<size>] pre-allocate outbuf [default 512 MiB]\n" " --outsize[=<size>] pre-allocate outbuf [default 512 MiB]\n"
" --nofill do not pass a fill function; requires --insize\n" " --nofill do not pass a fill function; requires --insize\n"
" --noflush do not pass a flush function; requires --outsize\n" " --noflush do not pass a flush function; requires --outsize\n"
"If no file names are given, or if a file is '-', xlunzip decompresses\n" "\nIf no file names are given, or if a file is '-', xlunzip decompresses\n"
"from standard input to standard output.\n" "from standard input to standard output.\n"
"Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n" "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
"Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n" "Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n"
"\nExit status: 0 for a normal exit, 1 for environmental problems (file\n" "\nTo extract all the files from archive 'foo.tar.lz', use the commands\n"
"not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n" "'tar -xf foo.tar.lz' or 'xlunzip -cd foo.tar.lz | tar -xf -'.\n"
"invalid input file, 3 for an internal consistency error (eg, bug) which\n" "\nExit status: 0 for a normal exit, 1 for environmental problems\n"
"caused xlunzip to panic.\n" "(file not found, invalid command-line options, I/O errors, etc), 2 to\n"
"indicate a corrupt or invalid input file, 3 for an internal consistency\n"
"error (e.g., bug) which caused xlunzip to panic.\n"
"\nReport bugs to lzip-bug@nongnu.org\n" "\nReport bugs to lzip-bug@nongnu.org\n"
"Xlunzip home page: http://www.nongnu.org/lzip/xlunzip.html\n" ); "Xlunzip home page: http://www.nongnu.org/lzip/xlunzip.html\n" );
} }
@ -143,55 +155,135 @@ static void show_version( void )
/* assure at least a minimum size for buffer 'buf' */ /* assure at least a minimum size for buffer 'buf' */
void * resize_buffer( void * buf, const unsigned min_size ) static void * resize_buffer( void * buf, const unsigned min_size )
{ {
if( buf ) buf = realloc( buf, min_size ); if( buf ) buf = realloc( buf, min_size );
else buf = malloc( min_size ); else buf = malloc( min_size );
if( !buf ) if( !buf ) { show_error( mem_msg, 0, false ); cleanup_and_fail( 1 ); }
{
show_error( "Not enough memory.", 0, false );
cleanup_and_fail( 1 );
}
return buf; return buf;
} }
void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) static void Pp_init( Pretty_print * const pp,
const char * const filenames[], const int num_filenames )
{ {
if( verbosity >= 0 ) pp->name = 0;
pp->padded_name = 0;
pp->stdin_name = "(stdin)";
pp->longest_name = 0;
pp->first_post = false;
if( verbosity <= 0 ) return;
const unsigned stdin_name_len = strlen( pp->stdin_name );
int i;
for( i = 0; i < num_filenames; ++i )
{ {
if( pp->first_post ) const char * const s = filenames[i];
{ const unsigned len = (strcmp( s, "-" ) == 0) ? stdin_name_len : strlen( s );
pp->first_post = false; if( pp->longest_name < len ) pp->longest_name = len;
fputs( pp->padded_name, stderr );
if( !msg ) fflush( stderr );
}
if( msg ) fprintf( stderr, "%s\n", msg );
} }
if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len;
}
void Pp_free( Pretty_print * const pp )
{ if( pp->padded_name ) { free( pp->padded_name ); pp->padded_name = 0; } }
static void Pp_set_name( Pretty_print * const pp, const char * const filename )
{
unsigned name_len, padded_name_len, i = 0;
if( filename && filename[0] && strcmp( filename, "-" ) != 0 )
pp->name = filename;
else pp->name = pp->stdin_name;
name_len = strlen( pp->name );
padded_name_len = max( name_len, pp->longest_name ) + 4;
pp->padded_name = resize_buffer( pp->padded_name, padded_name_len + 1 );
while( i < 2 ) pp->padded_name[i++] = ' ';
while( i < name_len + 2 ) { pp->padded_name[i] = pp->name[i-2]; ++i; }
pp->padded_name[i++] = ':';
while( i < padded_name_len ) pp->padded_name[i++] = ' ';
pp->padded_name[i] = 0;
pp->first_post = true;
}
static void Pp_show_msg( Pretty_print * const pp, const char * const msg )
{
if( verbosity < 0 ) return;
if( pp->first_post )
{
pp->first_post = false;
fputs( pp->padded_name, stderr );
if( !msg ) fflush( stderr );
}
if( msg ) fprintf( stderr, "%s\n", msg );
} }
static unsigned long getnum( const char * const ptr, /* separate numbers of 5 or more digits in groups of 3 digits using '_' */
static const char * format_num3( unsigned long long num )
{
enum { buffers = 8, bufsize = 4 * sizeof num, n = 10 };
const char * const si_prefix = "kMGTPEZYRQ";
const char * const binary_prefix = "KMGTPEZYRQ";
static char buffer[buffers][bufsize]; /* circle of static buffers for printf */
static int current = 0;
int i;
char * const buf = buffer[current++]; current %= buffers;
char * p = buf + bufsize - 1; /* fill the buffer backwards */
*p = 0; /* terminator */
if( num > 9999 )
{
char prefix = 0; /* try binary first, then si */
for( i = 0; i < n && num != 0 && num % 1024 == 0; ++i )
{ num /= 1024; prefix = binary_prefix[i]; }
if( prefix ) *(--p) = 'i';
else
for( i = 0; i < n && num != 0 && num % 1000 == 0; ++i )
{ num /= 1000; prefix = si_prefix[i]; }
if( prefix ) *(--p) = prefix;
}
const bool split = num >= 10000;
for( i = 0; ; )
{
*(--p) = num % 10 + '0'; num /= 10; if( num == 0 ) break;
if( split && ++i >= 3 ) { i = 0; *(--p) = '_'; }
}
return p;
}
void show_option_error( const char * const arg, const char * const msg,
const char * const option_name )
{
if( verbosity >= 0 )
fprintf( stderr, "%s: '%s': %s option '%s'.\n",
program_name, arg, msg, option_name );
}
/* Recognized formats: <num>k, <num>Ki, <num>[MGTPEZYRQ][i] */
static unsigned long getnum( const char * const arg,
const char * const option_name,
const unsigned long llimit, const unsigned long llimit,
const unsigned long ulimit ) const unsigned long ulimit )
{ {
unsigned long result;
char * tail; char * tail;
errno = 0; errno = 0;
result = strtoul( ptr, &tail, 0 ); unsigned long result = strtoul( arg, &tail, 0 );
if( tail == ptr ) if( tail == arg )
{ { show_option_error( arg, "Bad or missing numerical argument in",
show_error( "Bad or missing numerical argument.", 0, true ); option_name ); exit( 1 ); }
exit( 1 );
}
if( !errno && tail[0] ) if( !errno && tail[0] )
{ {
const unsigned factor = ( tail[1] == 'i' ) ? 1024 : 1000; const unsigned factor = (tail[1] == 'i') ? 1024 : 1000;
int exponent = 0; /* 0 = bad multiplier */ int exponent = 0; /* 0 = bad multiplier */
int i; int i;
switch( tail[0] ) switch( tail[0] )
{ {
case 'Q': exponent = 10; break;
case 'R': exponent = 9; break;
case 'Y': exponent = 8; break; case 'Y': exponent = 8; break;
case 'Z': exponent = 7; break; case 'Z': exponent = 7; break;
case 'E': exponent = 6; break; case 'E': exponent = 6; break;
@ -203,10 +295,8 @@ static unsigned long getnum( const char * const ptr,
case 'k': if( factor == 1000 ) exponent = 1; break; case 'k': if( factor == 1000 ) exponent = 1; break;
} }
if( exponent <= 0 ) if( exponent <= 0 )
{ { show_option_error( arg, "Bad multiplier in numerical argument of",
show_error( "Bad multiplier in numerical argument.", 0, true ); option_name ); exit( 1 ); }
exit( 1 );
}
for( i = 0; i < exponent; ++i ) for( i = 0; i < exponent; ++i )
{ {
if( ulimit / factor >= result ) result *= factor; if( ulimit / factor >= result ) result *= factor;
@ -216,7 +306,10 @@ static unsigned long getnum( const char * const ptr,
if( !errno && ( result < llimit || result > ulimit ) ) errno = ERANGE; if( !errno && ( result < llimit || result > ulimit ) ) errno = ERANGE;
if( errno ) if( errno )
{ {
show_error( "Numerical argument out of limits.", 0, false ); if( verbosity >= 0 )
fprintf( stderr, "%s: '%s': Value out of limits [%s,%s] in "
"option '%s'.\n", program_name, arg, format_num3( llimit ),
format_num3( ulimit ), option_name );
exit( 1 ); exit( 1 );
} }
return result; return result;
@ -259,13 +352,13 @@ static void set_d_outname( const char * const name, const int eindex )
strcpy( output_filename, name ); strcpy( output_filename, name );
strcat( output_filename, ".out" ); strcat( output_filename, ".out" );
if( verbosity >= 1 ) if( verbosity >= 1 )
fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n", fprintf( stderr, "%s: %s: Can't guess original name -- using '%s'\n",
program_name, name, output_filename ); program_name, name, output_filename );
} }
static int open_instream( const char * const name, struct stat * const in_statsp, static int open_instream( const char * const name, struct stat * const in_statsp,
const bool no_ofile ) const bool one_to_one )
{ {
int infd = open( name, O_RDONLY | O_BINARY ); int infd = open( name, O_RDONLY | O_BINARY );
if( infd < 0 ) if( infd < 0 )
@ -274,16 +367,15 @@ static int open_instream( const char * const name, struct stat * const in_statsp
{ {
const int i = fstat( infd, in_statsp ); const int i = fstat( infd, in_statsp );
const mode_t mode = in_statsp->st_mode; const mode_t mode = in_statsp->st_mode;
const bool can_read = ( i == 0 && const bool can_read = i == 0 &&
( S_ISBLK( mode ) || S_ISCHR( mode ) || ( S_ISBLK( mode ) || S_ISCHR( mode ) ||
S_ISFIFO( mode ) || S_ISSOCK( mode ) ) ); S_ISFIFO( mode ) || S_ISSOCK( mode ) );
if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) ) if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", fprintf( stderr, "%s: %s: Input file is not a regular file%s.\n",
program_name, name, program_name, name, ( can_read && one_to_one ) ?
( can_read && !no_ofile ) ? ",\n and neither '-c' nor '-o' were specified" : "" );
",\n and '--stdout' was not specified" : "" );
close( infd ); close( infd );
infd = -1; infd = -1;
} }
@ -292,53 +384,77 @@ static int open_instream( const char * const name, struct stat * const in_statsp
} }
static bool open_outstream( const bool force, const bool from_stdin ) static bool open_outstream( const bool force, const bool protect )
{ {
const mode_t usr_rw = S_IRUSR | S_IWUSR; const mode_t usr_rw = S_IRUSR | S_IWUSR;
const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; const mode_t all_rw = usr_rw | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
const mode_t outfd_mode = from_stdin ? all_rw : usr_rw; const mode_t outfd_mode = protect ? usr_rw : all_rw;
int flags = O_APPEND | O_CREAT | O_RDWR | O_BINARY; int flags = O_APPEND | O_CREAT | O_RDWR | 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 ); outfd = open( output_filename, flags, outfd_mode );
if( outfd >= 0 ) delete_output_on_interrupt = true; if( outfd >= 0 ) delete_output_on_interrupt = true;
else if( verbosity >= 0 ) else if( errno == EEXIST )
{ show_file_error( output_filename,
if( errno == EEXIST ) "Output file already exists, skipping.", 0 );
fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n", else
program_name, output_filename ); show_file_error( output_filename, "Can't create output file", errno );
else return outfd >= 0;
fprintf( stderr, "%s: Can't create output file '%s': %s\n",
program_name, output_filename, strerror( errno ) );
}
return ( outfd >= 0 );
} }
void cleanup_and_fail( const int retval ) static void set_signals( void (*action)(int) )
{ {
signal( SIGHUP, action );
signal( SIGINT, action );
signal( SIGTERM, action );
}
static void cleanup_and_fail( const int retval )
{
set_signals( SIG_IGN ); /* ignore signals */
if( delete_output_on_interrupt ) if( delete_output_on_interrupt )
{ {
delete_output_on_interrupt = false; delete_output_on_interrupt = false;
if( verbosity >= 0 ) show_file_error( output_filename, "Deleting output file, if it exists.", 0 );
fprintf( stderr, "%s: Deleting output file '%s', if it exists.\n",
program_name, output_filename );
if( outfd >= 0 ) { close( outfd ); outfd = -1; } if( outfd >= 0 ) { close( outfd ); outfd = -1; }
if( remove( output_filename ) != 0 && errno != ENOENT ) if( remove( output_filename ) != 0 && errno != ENOENT )
show_error( "WARNING: deletion of output file (apparently) failed.", 0, false ); show_error( "warning: deletion of output file failed", errno, false );
} }
exit( retval ); exit( retval );
} }
/* Set permissions, owner and times. */ static void signal_handler( int sig )
{
if( sig ) {} /* keep compiler happy */
show_error( "Control-C or similar caught, quitting.", 0, false );
cleanup_and_fail( 1 );
}
static bool check_tty_in( const char * const input_filename, const int infd,
const bool testing, int * const retval )
{
if( isatty( infd ) ) /* for example /dev/tty */
{ show_file_error( input_filename,
"I won't read compressed data from a terminal.", 0 );
close( infd ); set_retval( retval, 2 );
if( !testing ) cleanup_and_fail( *retval );
return false; }
return true;
}
/* Set permissions, owner, and times. */
static void close_and_set_permissions( const struct stat * const in_statsp ) 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; const mode_t mode = in_statsp->st_mode;
/* fchown will in many cases return with EPERM, which can be safely ignored. */ /* fchown in many cases returns 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 )
{ if( fchmod( outfd, mode ) != 0 ) warning = true; } { if( fchmod( outfd, mode ) != 0 ) warning = true; }
else else
@ -347,10 +463,8 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
warning = true; warning = true;
} }
if( close( outfd ) != 0 ) if( close( outfd ) != 0 )
{ { show_file_error( output_filename, "Error closing output file", errno );
show_error( "Error closing output file", errno, false ); cleanup_and_fail( 1 ); }
cleanup_and_fail( 1 );
}
outfd = -1; outfd = -1;
delete_output_on_interrupt = false; delete_output_on_interrupt = false;
if( in_statsp ) if( in_statsp )
@ -361,7 +475,8 @@ static void close_and_set_permissions( const struct stat * const in_statsp )
if( utime( output_filename, &t ) != 0 ) warning = true; if( utime( output_filename, &t ) != 0 ) warning = true;
} }
if( warning && verbosity >= 1 ) if( warning && verbosity >= 1 )
show_error( "Can't change output file attributes.", 0, false ); show_file_error( output_filename,
"warning: can't change output file attributes", errno );
} }
@ -369,10 +484,10 @@ int convert_retval( const int retval )
{ {
switch( retval ) switch( retval )
{ {
case 0: return 0; case 0: return 0;
case LZIP_OOM_INBUF: case LZIP_OOM_INBUF:
case LZIP_OOM_OUTBUF: case LZIP_OOM_OUTBUF:
case LZIP_WRITE_ERROR: return 1; case LZIP_WRITE_ERROR: return 1;
case LZIP_HEADER1_EOF: case LZIP_HEADER1_EOF:
case LZIP_HEADER2_EOF: case LZIP_HEADER2_EOF:
case LZIP_BAD_MAGIC1: case LZIP_BAD_MAGIC1:
@ -381,19 +496,22 @@ int convert_retval( const int retval )
case LZIP_BAD_DICT_SIZE: case LZIP_BAD_DICT_SIZE:
case LZIP_BAD_DATA: case LZIP_BAD_DATA:
case LZIP_DATA_EOF: case LZIP_DATA_EOF:
case LZIP_BAD_CRC: return 2; case LZIP_BAD_CRC:
default: return 3; case LZIP_EMPTY_MEMBER: return 2;
default: return 3;
} }
} }
static int global_infd = -1; /* needed by the fill function */
static long fill( void * buf, unsigned long size ) static long fill( void * buf, unsigned long size )
{ {
unsigned long sz = 0; unsigned long sz = 0;
errno = 0; errno = 0;
while( sz < size ) while( sz < size )
{ {
const int n = read( infd, (uint8_t *)buf + sz, min( 1UL << 20, size - sz ) ); const int n = read( global_infd, (uint8_t *)buf + sz, min( 1UL << 20, size - sz ) );
if( n > 0 ) sz += n; if( n > 0 ) sz += n;
else if( n == 0 ) break; /* EOF */ else if( n == 0 ) break; /* EOF */
else if( errno != EINTR ) break; else if( errno != EINTR ) break;
@ -404,7 +522,7 @@ static long fill( void * buf, unsigned long size )
long flush( void * buf, unsigned long size ) long flush( void * buf, unsigned long size )
{ {
unsigned long sz = ( outfd >= 0 ) ? 0 : size; unsigned long sz = (outfd >= 0) ? 0 : size;
errno = 0; errno = 0;
while( sz < size ) while( sz < size )
{ {
@ -416,13 +534,13 @@ long flush( void * buf, unsigned long size )
return sz; return sz;
} }
const char * global_name; static const char * global_name; /* copy of filename for 'error' */
static void error(char *x) { show_file_error( global_name, x, 0 ); } static void error(char *x) { show_file_error( global_name, x, 0 ); }
static int decompress( struct Pretty_print * const pp, const long cl_insize, static int decompress( const int infd, Pretty_print * const pp,
const long cl_outsize, const bool nofill, const long cl_insize, const long cl_outsize,
const bool noflush, const bool testing ) const bool nofill, const bool noflush, const bool testing )
{ {
long in_len = cl_insize; long in_len = cl_insize;
uint8_t * const inbuf = (in_len > 0) ? malloc( in_len ) : 0; uint8_t * const inbuf = (in_len > 0) ? malloc( in_len ) : 0;
@ -431,19 +549,22 @@ static int decompress( struct Pretty_print * const pp, const long cl_insize,
long in_pos, out_pos; long in_pos, out_pos;
int retval; int retval;
if( ( in_len > 0 && !inbuf ) || ( out_size > 0 && !outbuf ) ) if( ( in_len > 0 && !inbuf ) || ( out_size > 0 && !outbuf ) )
{ show_error( "Not enough memory.", 0, false ); return 1; } { show_error( mem_msg, 0, false ); return 1; }
global_infd = infd;
if( inbuf ) if( inbuf )
{ {
const long len = fill( inbuf, in_len ); const long len = fill( inbuf, in_len );
if( len < in_len ) if( len < in_len )
{ if( errno ) { show_file_error( pp->name, "Read error", errno ); return 1; } { if( errno ) { show_file_error( pp->name, "Read error", errno );
global_infd = -1; return 1; }
in_len = len; } in_len = len; }
} }
global_name = pp->name; global_name = pp->name;
retval = convert_retval( __lunzip( inbuf, in_len, nofill ? 0 : fill, retval = convert_retval( __lunzip( inbuf, in_len, nofill ? 0 : fill,
noflush ? 0 : flush, outbuf, out_size, noflush ? 0 : flush, outbuf, out_size,
&in_pos, &out_pos, error ) ); &in_pos, &out_pos, error ) );
global_infd = -1;
if( retval ) return retval; if( retval ) return retval;
if( outbuf && noflush ) if( outbuf && noflush )
{ {
@ -451,43 +572,33 @@ static int decompress( struct Pretty_print * const pp, const long cl_insize,
if( len < out_pos ) if( len < out_pos )
{ show_file_error( pp->name, "Write error", errno ); return 1; } { show_file_error( pp->name, "Write error", errno ); return 1; }
} }
if( verbosity >= 1 ) Pp_show_msg( pp, 0 ); show_results( pp, in_pos, out_pos, testing );
if( verbosity >= 2 )
{
if( out_pos <= 0 || in_pos <= 0 )
fputs( "no data compressed. ", stderr );
else
fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ",
(double)out_pos / in_pos,
( 100.0 * in_pos ) / out_pos,
100.0 - ( ( 100.0 * in_pos ) / out_pos ) );
if( verbosity >= 3 )
fprintf( stderr, "decompressed %9lu, compressed %8lu. ",
out_pos, in_pos );
}
if( verbosity >= 1 )
fputs( testing ? "ok\n" : "done\n", stderr );
return 0; return 0;
} }
void signal_handler( int sig ) void show_results( Pretty_print * const pp, const long in_pos,
const long out_pos, const bool testing )
{ {
if( sig ) {} /* keep compiler happy */ if( verbosity >= 1 ) Pp_show_msg( pp, 0 );
show_error( "Control-C or similar caught, quitting.", 0, false ); if( verbosity >= 2 )
cleanup_and_fail( 1 ); {
if( out_pos <= 0 || in_pos <= 0 )
fputs( "no data compressed. ", stderr );
else
fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ",
(double)out_pos / in_pos,
( 100.0 * in_pos ) / out_pos,
100.0 - ( ( 100.0 * in_pos ) / out_pos ) );
if( verbosity >= 3 )
fprintf( stderr, "%9lu out, %8lu in. ", out_pos, in_pos );
}
if( verbosity >= 1 ) fputs( testing ? "ok\n" : "done\n", stderr );
} }
static void set_signals( void ) static void show_error( const char * const msg, const int errcode,
{ const bool help )
signal( SIGHUP, signal_handler );
signal( SIGINT, signal_handler );
signal( SIGTERM, signal_handler );
}
void show_error( const char * const msg, const int errcode, const bool help )
{ {
if( verbosity < 0 ) return; if( verbosity < 0 ) return;
if( msg && msg[0] ) if( msg && msg[0] )
@ -510,7 +621,7 @@ void show_file_error( const char * const filename, const char * const msg,
} }
void internal_error( const char * const msg ) static void internal_error( const char * const msg )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
fprintf( stderr, "%s: internal error: %s\n", program_name, msg ); fprintf( stderr, "%s: internal error: %s\n", program_name, msg );
@ -521,27 +632,19 @@ void internal_error( const char * const msg )
int main( const int argc, const char * const argv[] ) int main( const int argc, const char * const argv[] )
{ {
const char * default_output_filename = ""; const char * default_output_filename = "";
const char ** filenames = 0;
long cl_insize = 0; long cl_insize = 0;
long cl_outsize = 0; long cl_outsize = 0;
int num_filenames = 0;
int argind = 0;
int failed_tests = 0;
int retval = 0;
int i;
bool filenames_given = false;
bool force = false; bool force = false;
bool in_place = false; bool in_place = false;
bool keep_input_files = false; bool keep_input_files = false;
bool nofill = false; bool nofill = false;
bool noflush = false; bool noflush = false;
bool stdin_used = false;
bool testing = false; bool testing = false;
bool to_stdout = false; bool to_stdout = false;
struct Pretty_print pp; if( argc > 0 ) invocation_name = argv[0];
enum { opt_insize = 256, opt_outsize, opt_nofill, opt_noflush }; enum { opt_insize = 256, opt_outsize, opt_nofill, opt_noflush };
const struct ap_Option options[] = const ap_Option options[] =
{ {
{ 'c', "stdout", ap_no }, { 'c', "stdout", ap_no },
{ 'd', "decompress", ap_no }, { 'd', "decompress", ap_no },
@ -555,25 +658,26 @@ int main( const int argc, const char * const argv[] )
{ 't', "test", ap_no }, { 't', "test", ap_no },
{ 'v', "verbose", ap_no }, { 'v', "verbose", ap_no },
{ 'V', "version", ap_no }, { 'V', "version", ap_no },
{ opt_insize, "insize", ap_maybe }, { opt_insize, "insize", ap_maybe },
{ opt_outsize, "outsize", ap_maybe }, { opt_outsize, "outsize", ap_maybe },
{ opt_nofill, "nofill", ap_no }, { opt_nofill, "nofill", ap_no },
{ opt_noflush, "noflush", ap_no }, { opt_noflush, "noflush", ap_no },
{ 0 , 0, ap_no } }; { 0, 0, ap_no } };
struct Arg_parser parser;
invocation_name = argv[0];
/* static because valgrind complains and memory management in C sucks */
static Arg_parser parser;
if( !ap_init( &parser, argc, argv, options, 0 ) ) if( !ap_init( &parser, argc, argv, options, 0 ) )
{ show_error( "Not enough memory.", 0, false ); return 1; } { show_error( mem_msg, 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; }
int argind = 0;
for( ; argind < ap_arguments( &parser ); ++argind ) for( ; argind < ap_arguments( &parser ); ++argind )
{ {
const int code = ap_code( &parser, argind ); const int code = ap_code( &parser, argind );
const char * const arg = ap_argument( &parser, argind );
if( !code ) break; /* no more options */ if( !code ) break; /* no more options */
const char * const pn = ap_parsed_name( &parser, argind );
const char * const arg = ap_argument( &parser, argind );
switch( code ) switch( code )
{ {
case 'c': to_stdout = true; break; case 'c': to_stdout = true; break;
@ -582,138 +686,131 @@ int main( const int argc, const char * const argv[] )
case 'h': show_help(); return 0; case 'h': show_help(); return 0;
case 'I': in_place = true; break; case 'I': in_place = true; break;
case 'k': keep_input_files = true; break; case 'k': keep_input_files = true; break;
case 'n': break; case 'n': break; /* ignored */
case 'o': default_output_filename = arg; break; case 'o': if( strcmp( arg, "-" ) == 0 ) to_stdout = true;
else { default_output_filename = arg; } break;
case 'q': verbosity = -1; break; case 'q': verbosity = -1; break;
case 't': testing = true; break; case 't': testing = true; break;
case 'v': if( verbosity < 4 ) ++verbosity; break; case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0; case 'V': show_version(); return 0;
case opt_insize: case opt_insize:
cl_insize = arg[0] ? getnum( arg, 1, LONG_MAX ) : 16384; break; cl_insize = arg[0] ? getnum( arg, pn, 1, LONG_MAX ) : 16384; break;
case opt_outsize: cl_outsize = arg[0] ? case opt_outsize: cl_outsize = arg[0] ?
getnum( arg, min_dictionary_size, LONG_MAX ) : max_dictionary_size; getnum( arg, pn, min_dictionary_size, LONG_MAX ) : max_dictionary_size;
break; break;
case opt_nofill: nofill = true; break; case opt_nofill: nofill = true; break;
case opt_noflush: noflush = true; break; case opt_noflush: noflush = true; break;
default : internal_error( "uncaught option." ); default: internal_error( "uncaught option." );
} }
} /* end process options */ } /* end process options */
#if defined(__MSVCRT__) || defined(__OS2__) #if defined __MSVCRT__ || defined __OS2__ || defined __DJGPP__
setmode( STDIN_FILENO, O_BINARY ); setmode( STDIN_FILENO, O_BINARY );
setmode( STDOUT_FILENO, O_BINARY ); setmode( STDOUT_FILENO, O_BINARY );
#endif #endif
num_filenames = max( 1, ap_arguments( &parser ) - argind ); static const char ** filenames = 0;
int num_filenames = max( 1, ap_arguments( &parser ) - argind );
filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] ); filenames = resize_buffer( filenames, num_filenames * sizeof filenames[0] );
filenames[0] = "-"; filenames[0] = "-";
int i;
bool filenames_given = false;
for( i = 0; argind + i < ap_arguments( &parser ); ++i ) for( i = 0; argind + i < ap_arguments( &parser ); ++i )
{ {
filenames[i] = ap_argument( &parser, argind + i ); filenames[i] = ap_argument( &parser, argind + i );
if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true; if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true;
} }
if( testing ) if( testing ) to_stdout = false; /* apply overrides */
outfd = -1; if( testing || to_stdout ) default_output_filename = "";
if( !to_stdout && !testing &&
( filenames_given || default_output_filename[0] ) )
set_signals();
Pp_init( &pp, filenames, num_filenames );
output_filename = resize_buffer( output_filename, 1 ); output_filename = resize_buffer( output_filename, 1 );
output_filename[0] = 0;
if( to_stdout && !testing ) outfd = STDOUT_FILENO;
else outfd = -1;
const bool to_file = !to_stdout && !testing && default_output_filename[0];
if( !to_stdout && !testing && ( filenames_given || to_file ) )
set_signals( signal_handler );
static Pretty_print pp;
Pp_init( &pp, filenames, num_filenames );
int failed_tests = 0;
int retval = 0;
const bool one_to_one = !to_stdout && !testing && !to_file;
bool stdin_used = false;
struct stat in_stats;
for( i = 0; i < num_filenames; ++i ) for( i = 0; i < num_filenames; ++i )
{ {
const char * input_filename = ""; const char * input_filename = "";
int tmp; int infd;
struct stat in_stats;
const struct stat * in_statsp;
output_filename[0] = 0;
if( !filenames[i][0] || strcmp( filenames[i], "-" ) == 0 ) Pp_set_name( &pp, filenames[i] );
if( strcmp( filenames[i], "-" ) == 0 )
{ {
if( stdin_used ) continue; else stdin_used = true; if( stdin_used ) continue; else stdin_used = true;
infd = STDIN_FILENO; infd = STDIN_FILENO;
if( !testing ) if( !check_tty_in( pp.name, infd, testing, &retval ) ) continue;
{ if( one_to_one ) { outfd = STDOUT_FILENO; output_filename[0] = 0; }
if( to_stdout || !default_output_filename[0] )
outfd = STDOUT_FILENO;
else
{
output_filename = resize_buffer( output_filename,
strlen( default_output_filename ) + 1 );
strcpy( output_filename, default_output_filename );
if( !open_outstream( force, true ) )
{
if( retval < 1 ) retval = 1;
close( infd ); infd = -1;
continue;
}
}
}
} }
else else
{ {
input_filename = filenames[i]; input_filename = filenames[i];
infd = open_instream( input_filename, &in_stats, to_stdout || testing ); infd = open_instream( input_filename, &in_stats, one_to_one );
if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; } if( infd < 0 ) { set_retval( &retval, 1 ); continue; }
if( !testing ) if( !check_tty_in( pp.name, infd, testing, &retval ) ) continue;
if( one_to_one ) /* open outfd after checking infd */
{ {
if( to_stdout ) outfd = STDOUT_FILENO; set_d_outname( input_filename, extension_index( input_filename ) );
else if( !open_outstream( force, true ) )
{ { close( infd ); set_retval( &retval, 1 ); continue; }
set_d_outname( input_filename, extension_index( input_filename ) );
if( !open_outstream( force, false ) )
{
if( retval < 1 ) retval = 1;
close( infd ); infd = -1;
continue;
}
}
} }
} }
Pp_set_name( &pp, input_filename ); if( to_file && outfd < 0 ) /* open outfd after checking infd */
if( isatty( infd ) )
{ {
show_file_error( pp.name, output_filename = resize_buffer( output_filename,
"I won't read compressed data from a terminal.", 0 ); strlen( default_output_filename ) + 1 );
if( retval < 1 ) retval = 1; strcpy( output_filename, default_output_filename );
if( testing ) { close( infd ); infd = -1; continue; } if( !open_outstream( force, false ) ) return 1;
cleanup_and_fail( retval );
} }
in_statsp = input_filename[0] ? &in_stats : 0; const struct stat * const in_statsp =
( input_filename[0] && one_to_one ) ? &in_stats : 0;
int tmp;
if( in_place ) if( in_place )
tmp = decompress_in_place( infd, &pp, testing ); tmp = decompress_in_place( infd, &pp, testing );
else else
tmp = decompress( &pp, cl_insize, cl_outsize, nofill, noflush, testing ); tmp = decompress( infd, &pp, cl_insize, cl_outsize, nofill, noflush, testing );
if( tmp > retval ) retval = tmp; if( close( infd ) != 0 )
{ show_file_error( pp.name, "Error closing input file", errno );
set_retval( &tmp, 1 ); }
set_retval( &retval, tmp );
if( tmp ) if( tmp )
{ if( !testing ) cleanup_and_fail( retval ); { if( !testing ) cleanup_and_fail( retval );
else ++failed_tests; } else ++failed_tests; }
if( delete_output_on_interrupt ) if( delete_output_on_interrupt && one_to_one )
close_and_set_permissions( in_statsp ); close_and_set_permissions( in_statsp );
if( input_filename[0] ) if( input_filename[0] && !keep_input_files && one_to_one )
{ remove( input_filename );
close( infd ); infd = -1;
if( !keep_input_files && !to_stdout && !testing )
remove( input_filename );
}
} }
if( outfd >= 0 && close( outfd ) != 0 ) if( delete_output_on_interrupt ) /* -o */
close_and_set_permissions( ( retval == 0 && !stdin_used &&
filenames_given && num_filenames == 1 ) ? &in_stats : 0 );
else if( outfd >= 0 && close( outfd ) != 0 ) /* -c */
{ {
show_error( "Error closing stdout", errno, false ); show_error( "Error closing stdout", errno, false );
if( retval < 1 ) retval = 1; set_retval( &retval, 1 );
} }
if( failed_tests > 0 && verbosity >= 1 && num_filenames > 1 ) if( failed_tests > 0 && verbosity >= 1 && num_filenames > 1 )
fprintf( stderr, "%s: warning: %d %s failed the test.\n", fprintf( stderr, "%s: warning: %d %s failed the test.\n",
program_name, failed_tests, program_name, failed_tests,
( failed_tests == 1 ) ? "file" : "files" ); ( failed_tests == 1 ) ? "file" : "files" );
free( output_filename ); free( output_filename );
Pp_free( &pp );
free( filenames ); free( filenames );
ap_free( &parser ); ap_free( &parser );
return retval; return retval;

View file

@ -1,9 +1,9 @@
#! /bin/sh #! /bin/sh
# check script for Xlunzip - Test tool for the lunzip linux module # check script for Xlunzip - Test tool for the lzip_decompress linux module
# Copyright (C) 2016-2018 Antonio Diaz Diaz. # Copyright (C) 2016-2025 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.
LC_ALL=C LC_ALL=C
export LC_ALL export LC_ALL
@ -28,9 +28,11 @@ if [ -d tmp ] ; then rm -rf tmp ; fi
mkdir tmp mkdir tmp
cd "${objdir}"/tmp || framework_failure cd "${objdir}"/tmp || framework_failure
cat "${testdir}"/test.txt > in || framework_failure cp "${testdir}"/test.txt in || framework_failure
in_lz="${testdir}"/test.txt.lz in_lz="${testdir}"/test.txt.lz
zero_lz="${testdir}"/zero.lz em_lz="${testdir}"/em.lz
fox_lz="${testdir}"/fox.lz
fnz_lz="${testdir}"/fox_nz.lz
fail=0 fail=0
test_failed() { fail=1 ; printf " $1" ; [ -z "$2" ] || printf "($2)" ; } test_failed() { fail=1 ; printf " $1" ; [ -z "$2" ] || printf "($2)" ; }
@ -46,8 +48,15 @@ printf "testing xlunzip-%s..." "$2"
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
"${LZIP}" -dq -o in < "${in_lz}" "${LZIP}" -dq -o in < "${in_lz}"
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
"${LZIP}" -dq -o in "${in_lz}"
[ $? = 1 ] || test_failed $LINENO
"${LZIP}" -dq -o out nx_file.lz
[ $? = 1 ] || test_failed $LINENO
[ ! -e out ] || test_failed $LINENO
# these are for code coverage # these are for code coverage
"${LZIP}" -t -- nx_file 2> /dev/null "${LZIP}" -t -- nx_file.lz 2> /dev/null
[ $? = 1 ] || test_failed $LINENO
"${LZIP}" -t "" < /dev/null 2> /dev/null
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
"${LZIP}" --help > /dev/null || test_failed $LINENO "${LZIP}" --help > /dev/null || test_failed $LINENO
"${LZIP}" -n1 -V > /dev/null || test_failed $LINENO "${LZIP}" -n1 -V > /dev/null || test_failed $LINENO
@ -72,212 +81,352 @@ printf "LZIP\001+.............................." | "${LZIP}" -t 2> /dev/null
printf "\ntesting decompression..." printf "\ntesting decompression..."
"${LZIP}" -t "${in_lz}" || test_failed $LINENO "${LZIP}" -t "${in_lz}" || test_failed $LINENO
"${LZIP}" -cd "${in_lz}" > copy || test_failed $LINENO "${LZIP}" -d "${in_lz}" -o out || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in out || test_failed $LINENO
"${LZIP}" -cd "${in_lz}" > out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -d "${in_lz}" -o - > out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -d < "${in_lz}" > out || test_failed $LINENO
cmp in out || test_failed $LINENO
rm -f out || framework_failure
rm -f copy || framework_failure cp "${in_lz}" out.lz || framework_failure
cat "${in_lz}" > copy.lz || framework_failure "${LZIP}" -dk out.lz || test_failed $LINENO
"${LZIP}" -dk copy.lz || test_failed $LINENO cmp in out || test_failed $LINENO
cmp in copy || test_failed $LINENO rm -f out || framework_failure
printf "to be overwritten" > copy || framework_failure "${LZIP}" -cd "${fox_lz}" > fox || test_failed $LINENO
"${LZIP}" -d copy.lz 2> /dev/null cp fox copy || framework_failure
cp "${in_lz}" copy.lz || framework_failure
"${LZIP}" -d copy.lz out.lz 2> /dev/null # skip copy, decompress out
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
[ ! -e out.lz ] || test_failed $LINENO
cmp fox copy || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -df copy.lz || test_failed $LINENO "${LZIP}" -df copy.lz || test_failed $LINENO
[ ! -e copy.lz ] || test_failed $LINENO [ ! -e copy.lz ] || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
rm -f copy out || framework_failure
printf "to be overwritten" > copy || framework_failure printf "to be overwritten" > out || framework_failure
"${LZIP}" -df -o copy < "${in_lz}" || test_failed $LINENO "${LZIP}" -df -o out < "${in_lz}" || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in out || test_failed $LINENO
"${LZIP}" -d -o ./- "${in_lz}" || test_failed $LINENO
cmp in ./- || test_failed $LINENO
rm -f ./- || framework_failure
"${LZIP}" -d -o ./- < "${in_lz}" || test_failed $LINENO
cmp in ./- || test_failed $LINENO
rm -f ./- || framework_failure
rm -f copy || framework_failure cp "${in_lz}" anyothername || framework_failure
cat "${in_lz}" > anyothername || framework_failure "${LZIP}" -dv - anyothername - < "${in_lz}" > out 2> /dev/null ||
"${LZIP}" -dv --output copy - anyothername - < "${in_lz}" 2> /dev/null ||
test_failed $LINENO test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in out || test_failed $LINENO
cmp in anyothername.out || test_failed $LINENO cmp in anyothername.out || test_failed $LINENO
rm -f copy anyothername.out || framework_failure rm -f anyothername.out || framework_failure
"${LZIP}" -tq in "${in_lz}" "${LZIP}" -tq in "${in_lz}"
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
"${LZIP}" -tq nx_file.lz "${in_lz}" "${LZIP}" -tq nx_file.lz "${in_lz}"
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
"${LZIP}" -cdq in "${in_lz}" > copy "${LZIP}" -cdq in "${in_lz}" > out
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
cat copy in | cmp in - || test_failed $LINENO cat out in | cmp in - || test_failed $LINENO # out must be empty
"${LZIP}" -cdq nx_file.lz "${in_lz}" > copy "${LZIP}" -cdq nx_file.lz "${in_lz}" > out # skip nx_file, decompress in
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in out || test_failed $LINENO
rm -f copy || framework_failure rm -f out || framework_failure
cat "${in_lz}" > copy.lz || framework_failure cp "${in_lz}" out.lz || framework_failure
"${LZIP}" -dq in copy.lz "${LZIP}" -dq in out.lz
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
[ -e copy.lz ] || test_failed $LINENO [ -e out.lz ] || test_failed $LINENO
[ ! -e copy ] || test_failed $LINENO [ ! -e out ] || test_failed $LINENO
[ ! -e in.out ] || test_failed $LINENO [ ! -e in.out ] || test_failed $LINENO
"${LZIP}" -dq nx_file.lz copy.lz "${LZIP}" -dq nx_file.lz out.lz
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
[ ! -e copy.lz ] || test_failed $LINENO [ ! -e out.lz ] || test_failed $LINENO
[ ! -e nx_file ] || test_failed $LINENO [ ! -e nx_file ] || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in out || test_failed $LINENO
rm -f out || framework_failure
cat in in > in2 || framework_failure cat in in > in2 || framework_failure
cat "${in_lz}" "${in_lz}" > in2.lz || framework_failure "${LZIP}" -t "${in_lz}" "${in_lz}" || test_failed $LINENO
"${LZIP}" -t in2.lz || test_failed $LINENO "${LZIP}" -cd "${in_lz}" "${in_lz}" -o out > out2 || test_failed $LINENO
"${LZIP}" -cd in2.lz > copy2 || test_failed $LINENO [ ! -e out ] || test_failed $LINENO # override -o
cmp in2 copy2 || test_failed $LINENO cmp in2 out2 || test_failed $LINENO
rm -f out2 || framework_failure
"${LZIP}" -d "${in_lz}" "${in_lz}" -o out2 || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
rm -f out2 || framework_failure
cat in2.lz > copy2.lz || framework_failure cat "${in_lz}" "${in_lz}" > out2.lz || framework_failure
printf "\ngarbage" >> copy2.lz || framework_failure lines=`"${LZIP}" -tvv out2.lz 2>&1 | wc -l` || test_failed $LINENO
printf "to be overwritten" > copy2 || framework_failure [ "${lines}" -eq 1 ] || test_failed $LINENO "${lines}"
"${LZIP}" -df copy2.lz || test_failed $LINENO
cmp in2 copy2 || test_failed $LINENO printf "\ngarbage" >> out2.lz || framework_failure
"${LZIP}" -tvvvv out2.lz 2> /dev/null || test_failed $LINENO
printf "to be overwritten" > out2 || framework_failure
"${LZIP}" -df out2.lz || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
touch empty || framework_failure
cp "${em_lz}" em.lz || framework_failure
"${LZIP}" -dk em.lz || test_failed $LINENO
cmp empty em || test_failed $LINENO
printf "\ntesting bad input..." printf "\ntesting bad input..."
cat em.lz em.lz | "${LZIP}" -tq
[ $? = 2 ] || test_failed $LINENO
cat em.lz em.lz | "${LZIP}" -dq > em
[ $? = 2 ] || test_failed $LINENO
cmp empty em || test_failed $LINENO
cat em.lz "${in_lz}" | "${LZIP}" -tq
[ $? = 2 ] || test_failed $LINENO
cat em.lz "${in_lz}" | "${LZIP}" -dq > out
[ $? = 2 ] || test_failed $LINENO
cmp in out || test_failed $LINENO
cat "${in_lz}" em.lz | "${LZIP}" -tq
[ $? = 2 ] || test_failed $LINENO
cat "${in_lz}" em.lz | "${LZIP}" -dq > out
[ $? = 2 ] || test_failed $LINENO
cmp in out || test_failed $LINENO
cat em.lz em.lz > ee.lz || framework_failure
"${LZIP}" -tq < ee.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -dq < ee.lz > em
[ $? = 2 ] || test_failed $LINENO
cmp empty em || test_failed $LINENO
"${LZIP}" -tq ee.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -dq ee.lz
[ $? = 2 ] || test_failed $LINENO
[ ! -e ee ] || test_failed $LINENO
"${LZIP}" -cdq ee.lz > em
[ $? = 2 ] || test_failed $LINENO
cmp empty em || test_failed $LINENO
rm -f em || framework_failure
cat "${in_lz}" em.lz "${in_lz}" > inein.lz || framework_failure
"${LZIP}" -tq < inein.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -dq < inein.lz > out2
[ $? = 2 ] || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
"${LZIP}" -tq inein.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -dq inein.lz
[ $? = 2 ] || test_failed $LINENO
[ ! -e inein ] || test_failed $LINENO
"${LZIP}" -cdq inein.lz > out2
[ $? = 2 ] || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
rm -f out2 inein.lz em.lz || framework_failure
headers='LZIp LZiP LZip LzIP LzIp LziP lZIP lZIp lZiP lzIP' headers='LZIp LZiP LZip LzIP LzIp LziP lZIP lZIp lZiP lzIP'
body='\001\014\000\203\377\373\377\377\300\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000$\000\000\000\000\000\000\000' body='\001\014\000\000\101\376\367\377\377\340\000\200\000\215\357\002\322\001\000\000\000\000\000\000\000\045\000\000\000\000\000\000\000'
cat "${in_lz}" > int.lz cp "${in_lz}" int.lz || framework_failure
printf "LZIP${body}" >> int.lz printf "LZIP${body}" >> int.lz || framework_failure
if "${LZIP}" -tq int.lz ; then if "${LZIP}" -t int.lz ; then
for header in ${headers} ; do for header in ${headers} ; do
printf "${header}${body}" > int.lz # first member printf "${header}${body}" > int.lz || framework_failure
"${LZIP}" -tq int.lz "${LZIP}" -tq int.lz # first member
[ $? = 2 ] || test_failed $LINENO ${header} [ $? = 2 ] || test_failed $LINENO ${header}
cat "${in_lz}" > int.lz "${LZIP}" -tq < int.lz
printf "${header}${body}" >> int.lz # trailing data [ $? = 2 ] || test_failed $LINENO ${header}
"${LZIP}" -tq int.lz "${LZIP}" -cdq int.lz > /dev/null
[ $? = 2 ] || test_failed $LINENO ${header}
cp "${in_lz}" int.lz || framework_failure
printf "${header}${body}" >> int.lz || framework_failure
"${LZIP}" -tq int.lz # trailing data
[ $? = 2 ] || test_failed $LINENO ${header}
"${LZIP}" -tq < int.lz
[ $? = 2 ] || test_failed $LINENO ${header}
"${LZIP}" -cdq int.lz > /dev/null
[ $? = 2 ] || test_failed $LINENO ${header} [ $? = 2 ] || test_failed $LINENO ${header}
done done
else else
printf "\nwarning: skipping header test: 'printf' does not work on your system." printf "warning: skipping header test: 'printf' does not work on your system."
fi fi
rm -f int.lz || framework_failure rm -f int.lz || framework_failure
"${LZIP}" -tq "${fnz_lz}"
[ $? = 2 ] || test_failed $LINENO
for i in fox_v2.lz fox_s11.lz fox_de20.lz \
fox_bcrc.lz fox_crc0.lz fox_das46.lz fox_mes81.lz ; do
"${LZIP}" -tq "${testdir}"/$i
[ $? = 2 ] || test_failed $LINENO $i
done
for i in fox_bcrc.lz fox_crc0.lz fox_das46.lz fox_mes81.lz ; do
"${LZIP}" -cdq "${testdir}"/$i > out
[ $? = 2 ] || test_failed $LINENO $i
cmp fox out || test_failed $LINENO $i
done
rm -f fox || framework_failure
cat "${in_lz}" "${in_lz}" > in2.lz || framework_failure
cat "${in_lz}" "${in_lz}" "${in_lz}" > in3.lz || framework_failure cat "${in_lz}" "${in_lz}" "${in_lz}" > in3.lz || framework_failure
if dd if=in3.lz of=trunc.lz bs=14752 count=1 2> /dev/null && if dd if=in3.lz of=trunc.lz bs=14682 count=1 2> /dev/null &&
[ -e trunc.lz ] && cmp in2.lz trunc.lz > /dev/null 2>&1 ; then [ -e trunc.lz ] && cmp in2.lz trunc.lz ; then
for i in 6 20 14734 14753 14754 14755 14756 14757 14758 ; do for i in 6 20 14664 14683 14684 14685 14686 14687 14688 ; do
dd if=in3.lz of=trunc.lz bs=$i count=1 2> /dev/null dd if=in3.lz of=trunc.lz bs=$i count=1 2> /dev/null
"${LZIP}" -tq trunc.lz "${LZIP}" -tq trunc.lz
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
"${LZIP}" -tq < trunc.lz "${LZIP}" -tq < trunc.lz
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
"${LZIP}" -cdq trunc.lz > out "${LZIP}" -cdq trunc.lz > /dev/null
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
"${LZIP}" -dq < trunc.lz > out "${LZIP}" -dq < trunc.lz > /dev/null
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
done done
else else
printf "\nwarning: skipping truncation test: 'dd' does not work on your system." printf "warning: skipping truncation test: 'dd' does not work on your system."
fi fi
rm -f trunc.lz out || framework_failure rm -f trunc.lz || framework_failure
cat "${in_lz}" > ingin.lz || framework_failure cp "${in_lz}" ingin.lz || framework_failure
printf "g" >> ingin.lz || framework_failure printf "g" >> ingin.lz || framework_failure
cat "${in_lz}" >> ingin.lz || framework_failure cat "${in_lz}" >> ingin.lz || framework_failure
"${LZIP}" -t ingin.lz || test_failed $LINENO "${LZIP}" -t ingin.lz || test_failed $LINENO
"${LZIP}" -cd ingin.lz > copy || test_failed $LINENO
cmp in copy || test_failed $LINENO
"${LZIP}" -t < ingin.lz || test_failed $LINENO "${LZIP}" -t < ingin.lz || test_failed $LINENO
"${LZIP}" -d < ingin.lz > copy || test_failed $LINENO "${LZIP}" -dk ingin.lz || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in ingin || test_failed $LINENO
"${LZIP}" -cd ingin.lz > out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -d < ingin.lz > out || test_failed $LINENO
cmp in out || test_failed $LINENO
printf "\ntesting linux options..." printf "\ntesting linux options..."
for i in "" =1 =2 =4 =16 =64 =256 =1024 =4096 =16384 =65536 ; do for i in "" =1 =2 =4 =16 =64 =256 =1024 =4096 =16384 =65536 ; do
"${LZIP}" -t --insize$i "${in_lz}" || test_failed $LINENO $i "${LZIP}" -t --insize$i "${in_lz}" || test_failed $LINENO $i
"${LZIP}" -cd --insize$i "${in_lz}" > copy || test_failed $LINENO $i "${LZIP}" -cd --insize$i "${in_lz}" > out || test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in out || test_failed $LINENO $i
done done
for i in =1 =2 =4 =16 =64 =256 =1024 =4096 ; do for i in =1 =2 =4 =16 =64 =256 =1024 =4096 ; do
"${LZIP}" -tq --insize$i --nofill "${in_lz}" "${LZIP}" -tq --insize$i --nofill "${in_lz}"
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
"${LZIP}" -cdq --insize$i --nofill "${in_lz}" > copy "${LZIP}" -cdq --insize$i --nofill "${in_lz}" > out
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
cmp -s in copy && test_failed $LINENO $i cmp -s in out && test_failed $LINENO $i
done done
for i in "" =16384 =65536 ; do for i in "" =16384 =65536 ; do
"${LZIP}" -t --insize$i --nofill "${in_lz}" || test_failed $LINENO $i "${LZIP}" -t --insize$i --nofill "${in_lz}" || test_failed $LINENO $i
"${LZIP}" -cd --insize$i --nofill "${in_lz}" > copy || "${LZIP}" -cd --insize$i --nofill "${in_lz}" > out ||
test_failed $LINENO $i test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in out || test_failed $LINENO $i
done done
for i in =18KiB =65536 =262144 ; do for i in =18KiB =65536 =262144 ; do
"${LZIP}" -t --outsize$i "${in_lz}" || test_failed $LINENO $i "${LZIP}" -t --outsize$i "${in_lz}" || test_failed $LINENO $i
"${LZIP}" -cd --outsize$i "${in_lz}" > copy || "${LZIP}" -cd --outsize$i "${in_lz}" > out ||
test_failed $LINENO $i test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in out || test_failed $LINENO $i
done done
for i in =36388 =65536 =262144 ; do for i in =36301 =65536 =262144 ; do
"${LZIP}" -t --outsize$i --noflush "${in_lz}" || "${LZIP}" -t --outsize$i --noflush "${in_lz}" ||
test_failed $LINENO $i test_failed $LINENO $i
"${LZIP}" -cd --outsize$i --noflush "${in_lz}" > copy || "${LZIP}" -cd --outsize$i --noflush "${in_lz}" > out ||
test_failed $LINENO $i test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in out || test_failed $LINENO $i
done done
for i in =36387 ; do for i in =36300 ; do
"${LZIP}" -tq --outsize$i --noflush "${in_lz}" "${LZIP}" -tq --outsize$i --noflush "${in_lz}"
[ $? = 1 ] || test_failed $LINENO $i [ $? = 1 ] || test_failed $LINENO $i
"${LZIP}" -cdq --outsize$i --noflush "${in_lz}" > copy "${LZIP}" -cdq --outsize$i --noflush "${in_lz}" > out
[ $? = 1 ] || test_failed $LINENO $i [ $? = 1 ] || test_failed $LINENO $i
cmp -s in copy && test_failed $LINENO $i cmp -s in out && test_failed $LINENO $i
done done
for i in =4096 =16384 ; do for i in =4096 =16384 ; do
"${LZIP}" -tq --outsize$i --noflush "${in_lz}" "${LZIP}" -tq --outsize$i --noflush "${in_lz}"
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
"${LZIP}" -cdq --outsize$i --noflush "${in_lz}" > copy "${LZIP}" -cdq --outsize$i --noflush "${in_lz}" > out
[ $? = 2 ] || test_failed $LINENO $i [ $? = 2 ] || test_failed $LINENO $i
cmp -s in copy && test_failed $LINENO $i cmp -s in out && test_failed $LINENO $i
done done
"${LZIP}" -t --insize --outsize=36388 --nofill --noflush "${in_lz}" || "${LZIP}" -t --insize --outsize=36388 --nofill --noflush "${in_lz}" ||
test_failed $LINENO test_failed $LINENO
"${LZIP}" -cd --insize --outsize=36388 --nofill --noflush "${in_lz}" > copy || "${LZIP}" -cd --insize --outsize=36388 --nofill --noflush "${in_lz}" > out ||
test_failed $LINENO test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in out || test_failed $LINENO
"${LZIP}" -tq --nofill "${in_lz}" "${LZIP}" -tq --nofill "${in_lz}"
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
"${LZIP}" -cdq --nofill "${in_lz}" > copy "${LZIP}" -cdq --nofill "${in_lz}" > out
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
cmp -s in copy && test_failed $LINENO cmp -s in out && test_failed $LINENO
"${LZIP}" -tq --noflush "${in_lz}" "${LZIP}" -tq --noflush "${in_lz}"
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
"${LZIP}" -cdq --noflush "${in_lz}" > copy "${LZIP}" -cdq --noflush "${in_lz}" > out
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
cmp -s in copy && test_failed $LINENO cmp -s in out && test_failed $LINENO
# decompress in place
rm -f out out2 || framework_failure
"${LZIP}" -t --in-place "${in_lz}" || test_failed $LINENO "${LZIP}" -t --in-place "${in_lz}" || test_failed $LINENO
"${LZIP}" -cd --in-place "${in_lz}" > copy || test_failed $LINENO "${LZIP}" -t --in-place < "${in_lz}" || test_failed $LINENO
cmp in copy || test_failed $LINENO "${LZIP}" -d --in-place "${in_lz}" -o out || test_failed $LINENO
cmp in out || test_failed $LINENO
rm -f out || framework_failure
"${LZIP}" -d --in-place < "${in_lz}" -o out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -cd --in-place "${in_lz}" > out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -cd --in-place < "${in_lz}" > out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${LZIP}" -t --in-place "${in_lz}" "${in_lz}" || test_failed $LINENO
"${LZIP}" -d --in-place "${in_lz}" "${in_lz}" -o out2 || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
"${LZIP}" -cd --in-place "${in_lz}" "${in_lz}" > out2 || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
# decompress multimember in place
cat in in in > in3 || framework_failure cat in in in > in3 || framework_failure
for i in in2 in3 ; do for i in in2 in3 ; do
"${LZIP}" -t --in-place $i.lz || test_failed $LINENO "${LZIP}" -t --in-place $i.lz || test_failed $LINENO
"${LZIP}" -cd --in-place $i.lz > copy || test_failed $LINENO "${LZIP}" -t --in-place < $i.lz || test_failed $LINENO
cmp $i copy || test_failed $LINENO rm -f out || framework_failure
"${LZIP}" -d --in-place $i.lz -o out || test_failed $LINENO
cmp $i out || test_failed $LINENO
rm -f out || framework_failure
"${LZIP}" -d --in-place < $i.lz -o out || test_failed $LINENO
cmp $i out || test_failed $LINENO
"${LZIP}" -cd --in-place $i.lz > out || test_failed $LINENO
cmp $i out || test_failed $LINENO
"${LZIP}" -cd --in-place < $i.lz > out || test_failed $LINENO
cmp $i out || test_failed $LINENO
done done
"${LZIP}" -tq --in-place ingin.lz "${LZIP}" -tq --in-place ingin.lz
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
rm -f out2 ingin.lz in2 in2.lz in3 in3.lz || framework_failure
cat "${in_lz}" "${zero_lz}" "${zero_lz}" "${zero_lz}" "${zero_lz}" \ cat "${in_lz}" "${em_lz}" > ine.lz || framework_failure
"${zero_lz}" "${zero_lz}" "${zero_lz}" > inz.lz || framework_failure "${LZIP}" -tq --in-place ine.lz
"${LZIP}" -t --in-place inz.lz || test_failed $LINENO
"${LZIP}" -cd --in-place inz.lz > copy || test_failed $LINENO
cmp in copy || test_failed $LINENO
cat "${zero_lz}" >> inz.lz || framework_failure
"${LZIP}" -tq --in-place inz.lz
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
"${LZIP}" -cdq --in-place inz.lz > copy "${LZIP}" -cdq --in-place ine.lz > out
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
cmp -s in copy && test_failed $LINENO cmp empty out || test_failed $LINENO
rm -f copy ingin.lz in2 in2.lz in3 in3.lz || framework_failure cat "${em_lz}" "${in_lz}" > ine.lz || framework_failure
"${LZIP}" -tq --in-place ine.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -cdq --in-place ine.lz > out
[ $? = 2 ] || test_failed $LINENO
cmp empty out || test_failed $LINENO
rm -f ine.lz empty || framework_failure
# decompress with trailing data in place
cat "${in_lz}" in in in in > int.lz || framework_failure
"${LZIP}" -t --in-place int.lz || test_failed $LINENO
"${LZIP}" -cd --in-place int.lz > out || test_failed $LINENO
cmp in out || test_failed $LINENO
rm -f out int.lz || framework_failure
echo echo
if [ ${fail} = 0 ] ; then if [ ${fail} = 0 ] ; then

BIN
testsuite/fox.lz Normal file

Binary file not shown.

BIN
testsuite/fox_bcrc.lz Normal file

Binary file not shown.

BIN
testsuite/fox_crc0.lz Normal file

Binary file not shown.

BIN
testsuite/fox_das46.lz Normal file

Binary file not shown.

BIN
testsuite/fox_de20.lz Normal file

Binary file not shown.

BIN
testsuite/fox_mes81.lz Normal file

Binary file not shown.

BIN
testsuite/fox_nz.lz Normal file

Binary file not shown.

BIN
testsuite/fox_s11.lz Normal file

Binary file not shown.

BIN
testsuite/fox_v2.lz Normal file

Binary file not shown.

View file

@ -1,8 +1,7 @@
GNU GENERAL PUBLIC LICENSE GNU GENERAL PUBLIC LICENSE
Version 2, June 1991 Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc., Copyright (C) 1989, 1991 Free Software Foundation, Inc. <http://fsf.org/>
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed. of this license document, but changing it is not allowed.
@ -339,8 +338,7 @@ Public License instead of this License.
GNU GENERAL PUBLIC LICENSE GNU GENERAL PUBLIC LICENSE
Version 2, June 1991 Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc., Copyright (C) 1989, 1991 Free Software Foundation, Inc. <http://fsf.org/>
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed. of this license document, but changing it is not allowed.

Binary file not shown.