1
0
Fork 0

Merging upstream version 1.12.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-20 21:31:06 +01:00
parent 4f55f2a35b
commit e62d82c53f
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
40 changed files with 3224 additions and 1857 deletions

View file

@ -1,7 +1,7 @@
Lzlib was written by Antonio Diaz Diaz. Lzlib was written by Antonio Diaz Diaz.
The ideas embodied in lzlib are due to (at least) the following people: The ideas embodied in lzlib are due to (at least) the following people:
Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for the
the definition of Markov chains), G.N.N. Martin (for the definition of definition of Markov chains), G.N.N. Martin (for the definition of range
range encoding), Igor Pavlov (for putting all the above together in encoding), Igor Pavlov (for putting all the above together in LZMA), and
LZMA), and Julian Seward (for bzip2's CLI). Julian Seward (for bzip2's CLI).

26
COPYING
View file

@ -1,17 +1,17 @@
Lzlib - Compression library for the lzip format Lzlib - Compression library for the lzip format
Copyright (C) Antonio Diaz Diaz. Copyright (C) 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.

135
ChangeLog
View file

@ -1,16 +1,55 @@
2021-01-02 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.12 released.
* lzlib.h: Define LZ_API_VERSION as 1000 * major + minor. 1.12 = 1012.
This change does not affect the soversion.
* lzlib.h, lzlib.c: New function LZ_api_version.
* LZd_try_verify_trailer: Return 2 if EOF at trailer or EOS marker.
* Decompression speed has been slightly increased.
* decoder.h: Increase 'rd_min_available_bytes' from 8 to 10.
* encoder_base.c (LZeb_try_sync_flush):
Compensate for the increase in 'rd_min_available_bytes'.
* main.c (do_decompress): Fix false report about library stall.
* main.c: New option '--check-lib'.
* main.c (main): Report an error if a file name is empty.
Make '-o' behave like '-c', but writing to file instead of stdout.
Make '-c' and '-o' check whether the output is a terminal only once.
Do not open output if input is a terminal.
Replace 'decompressed', 'compressed' with 'out', 'in' in output.
Set a valid invocation_name even if argc == 0.
* lzlib.texi: Document the new way of verifying the library version.
Document that 'LZ_(de)compress_close' and 'LZ_(de)compress_errno'
can be called with a null argument.
Document that sync flush marker is not allowed in lzip files.
Document the consequences of not calling 'LZ_decompress_finish'.
Document that 'LZ_decompress_read' returns at least once per member.
Document that 'LZ_(de)compress_read' can be called with a null
buffer pointer argument.
Real code examples for common uses have been added to the tutorial.
* bbexample.c: Don't use 'LZ_(de)compress_write_size'.
* lzcheck.c: New options '-s' (sync) and '-m' (member by member).
Test member by member without 'LZ_decompress_finish'.
* ffexample.c: New file containing example functions for file-to-file
compression/decompression.
* Document extraction from tar.lz in '--help' output and man page.
* Makefile.in: 'install-bin' no longer installs the man page.
New targets 'install-bin-compress' and 'install-bin-strip-compress'.
* testsuite: Add 9 new test files.
2019-01-02 Antonio Diaz Diaz <antonio@gnu.org> 2019-01-02 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.11 released. * Version 1.11 released.
* File_* renamed to Lzip_*. * Rename File_* to Lzip_*.
* LZ_decompress_read: Don't return error until all data is read. * LZ_decompress_read: Don't return error until all data is read.
* decoder.c (LZd_decode_member): Decode truncated data until EOF. * decoder.c (LZd_decode_member): Decode truncated data until EOF.
* cbuffer.c (Cb_read_data): Allow a null buffer pointer.
* main.c: Don't allow mixing different operations (-d and -t). * main.c: Don't allow mixing different operations (-d and -t).
* main.c: Check return value of close( infd ). * main.c: Check return value of close( infd ).
* main.c: Compile on DOS with DJGPP. * main.c: Compile on DOS with DJGPP.
* lzlib.texi: Improved descriptions of '-0..-9', '-m' and '-s'. * lzlib.texi: Improve descriptions of '-0..-9', '-m', and '-s'.
Document that 'LZ_(de)compress_finish' can be called repeatedly. Document that 'LZ_(de)compress_finish' can be called repeatedly.
* configure: Accept appending to CFLAGS, 'CFLAGS+=OPTIONS'. * configure: Accept appending to CFLAGS, 'CFLAGS+=OPTIONS'.
* Makefile.in: Targets 'install-bin*' renamed to 'install-lib*'. * Makefile.in: Rename targets 'install-bin*' to 'install-lib*'.
* Makefile.in: Targets 'install-bin*' now install minilzip. * Makefile.in: Targets 'install-bin*' now install minilzip.
* INSTALL: Document use of CFLAGS+='-D __USE_MINGW_ANSI_STDIO'. * INSTALL: Document use of CFLAGS+='-D __USE_MINGW_ANSI_STDIO'.
@ -19,13 +58,13 @@
* Version 1.10 released. * Version 1.10 released.
* LZ_compress_finish now adjusts dictionary size for each member. * LZ_compress_finish now adjusts dictionary size for each member.
* lzlib.c (LZ_decompress_read): Detect corrupt header with HD=3. * lzlib.c (LZ_decompress_read): Detect corrupt header with HD=3.
* main.c: Added new option '--loose-trailing'. * main.c: New option '--loose-trailing'.
* main.c (main): Option '-S, --volume-size' now keeps input files. * main.c (main): Option '-S, --volume-size' now keeps input files.
* main.c: Replaced 'bits/byte' with inverse compression ratio. * main.c: Replace 'bits/byte' with inverse compression ratio.
* main.c: Show final diagnostic when testing multiple files. * main.c: Show final diagnostic when testing multiple files.
* main.c: Do not add a second .lz extension to the arg of -o. * main.c: Do not add a second .lz extension to the arg of -o.
* main.c: Show dictionary size at verbosity level 4 (-vvvv). * main.c: Show dictionary size at verbosity level 4 (-vvvv).
* lzlib.texi: Added chapter 'Invoking minilzip'. * lzlib.texi: New chapter 'Invoking minilzip'.
2017-04-11 Antonio Diaz Diaz <antonio@gnu.org> 2017-04-11 Antonio Diaz Diaz <antonio@gnu.org>
@ -34,14 +73,15 @@
* Compression time of options -1 to -9 has been reduced by 1%. * Compression time of options -1 to -9 has been reduced by 1%.
* Decompression time has been reduced by 3%. * Decompression time has been reduced by 3%.
* main.c: Continue testing if any input file is a terminal. * main.c: Continue testing if any input file is a terminal.
* License of the library changed to "2-clause BSD". * Change the license of the library to "2-clause BSD".
2016-05-17 Antonio Diaz Diaz <antonio@gnu.org> 2016-05-17 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.8 released. * Version 1.8 released.
* lzlib.h: Define LZ_API_VERSION to 1. * lzlib.h: Define LZ_API_VERSION to 1.
* decoder.c (LZd_verify_trailer): Removed test of final code. * lzlib.c (LZ_decompress_sync_to_member): Add skipped size to in_size.
* main.c: Added new option '-a, --trailing-error'. * decoder.c (LZd_verify_trailer): Remove test of final code.
* main.c: New option '-a, --trailing-error'.
* main.c (main): Delete '--output' file if infd is a terminal. * main.c (main): Delete '--output' file if infd is a terminal.
* main.c (main): Don't use stdin more than once. * main.c (main): Don't use stdin more than once.
* configure: Avoid warning on some shells when testing for gcc. * configure: Avoid warning on some shells when testing for gcc.
@ -52,26 +92,25 @@
2015-07-08 Antonio Diaz Diaz <antonio@gnu.org> 2015-07-08 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.7 released. * Version 1.7 released.
* Ported fast encoder and option '-0' from lzip. * Port fast encoder and option '-0' from lzip.
* If open-->write-->finish, produce same dictionary size as lzip. * If open-->write-->finish, produce same dictionary size as lzip.
* Makefile.in: Added new targets 'install*-compress'. * Makefile.in: New targets 'install*-compress'.
2014-08-27 Antonio Diaz Diaz <antonio@gnu.org> 2014-08-27 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.6 released. * Version 1.6 released.
* Compression ratio of option '-9' has been slightly increased. * Compression ratio of option '-9' has been slightly increased.
* configure: Added new option '--disable-static'. * configure: New options '--disable-static' and '--disable-ldconfig'.
* configure: Added new option '--disable-ldconfig'.
* Makefile.in: Ignore errors from ldconfig. * Makefile.in: Ignore errors from ldconfig.
* Makefile.in: Use 'CFLAGS' in every invocation of 'CC'. * Makefile.in: Use 'CFLAGS' in every invocation of 'CC'.
* main.c (close_and_set_permissions): Behave like 'cp -p'. * main.c (close_and_set_permissions): Behave like 'cp -p'.
* lzlib.texinfo: Renamed to lzlib.texi. * lzlib.texinfo: Rename to lzlib.texi.
* License changed to "GPL version 2 or later with link exception". * Change license to "GPL version 2 or later with link exception".
2013-09-15 Antonio Diaz Diaz <antonio@gnu.org> 2013-09-15 Antonio Diaz Diaz <antonio@gnu.org>
* Version 1.5 released. * Version 1.5 released.
* Removed decompression support for version 0 files. * Remove decompression support for version 0 files.
* The LZ_compress_sync_flush mechanism has been fixed (again). * The LZ_compress_sync_flush mechanism has been fixed (again).
* Minor fixes. * Minor fixes.
@ -82,12 +121,11 @@
* Compression ratio has been slightly increased. * Compression ratio has been slightly increased.
* Compression time has been reduced by 8%. * Compression time has been reduced by 8%.
* Decompression time has been reduced by 7%. * Decompression time has been reduced by 7%.
* lzlib.h: Changed 'long long' values to 'unsigned long long'. * lzlib.h: Change 'long long' values to 'unsigned long long'.
* encoder.c (Mf_init): Reduce minimum buffer size to 64KiB. * encoder.c (Mf_init): Reduce minimum buffer size to 64KiB.
* lzlib.c (LZ_decompress_read): Tell LZ_header_error from * lzlib.c (LZ_decompress_read): Tell LZ_header_error from
LZ_unexpected_eof the same way as lzip does. LZ_unexpected_eof the same way as lzip does.
* Makefile.in: Added new target 'install-as-lzip'. * Makefile.in: New targets 'install-as-lzip' and 'install-bin'.
* Makefile.in: Added new target 'install-bin'.
* main.c: Use 'setmode' instead of '_setmode' on Windows and OS/2. * main.c: Use 'setmode' instead of '_setmode' on Windows and OS/2.
* main.c: Define 'strtoull' to 'strtoul' on Windows. * main.c: Define 'strtoull' to 'strtoul' on Windows.
@ -95,7 +133,7 @@
* Version 1.3 released. * Version 1.3 released.
* Translated to C from the C++ source of lzlib 1.2. * Translated to C from the C++ source of lzlib 1.2.
* configure: 'datadir' renamed to 'datarootdir'. * configure: Rename 'datadir' to 'datarootdir'.
2011-10-25 Antonio Diaz Diaz <ant_diaz@teleline.es> 2011-10-25 Antonio Diaz Diaz <ant_diaz@teleline.es>
@ -104,10 +142,9 @@
independently of the value of 'pos_state'. This gives better independently of the value of 'pos_state'. This gives better
compression for large values of '--match-length' without being compression for large values of '--match-length' without being
slower. slower.
* encoder.h encoder.cc: Optimize pair price calculations. This * encoder.h, encoder.cc: Optimize pair price calculations, reducing
reduces compression time for large values of '--match-length' compression time for large values of '--match-length' by up to 6%.
by up to 6%. * main.cc: New option '-F, --recompress'.
* main.cc: Added new option '-F, --recompress'.
* Makefile.in: 'make install' no longer tries to run * Makefile.in: 'make install' no longer tries to run
'/sbin/ldconfig' on systems lacking it. '/sbin/ldconfig' on systems lacking it.
@ -117,7 +154,7 @@
* Compression time has been reduced by 2%. * Compression time has been reduced by 2%.
* All declarations not belonging to the API have been * All declarations not belonging to the API have been
encapsulated in the namespace 'Lzlib'. encapsulated in the namespace 'Lzlib'.
* testsuite: 'test1' renamed to 'test.txt'. Added new tests. * testsuite: Rename 'test1' to 'test.txt'. New tests.
* Match length limits set by options -1 to -9 of minilzip have * Match length limits set by options -1 to -9 of minilzip have
been changed to match those of lzip 1.11. been changed to match those of lzip 1.11.
* main.cc: Set stdin/stdout in binary mode on OS2. * main.cc: Set stdin/stdout in binary mode on OS2.
@ -127,14 +164,11 @@
2010-05-08 Antonio Diaz Diaz <ant_diaz@teleline.es> 2010-05-08 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 1.0 released. * Version 1.0 released.
* Added new function LZ_decompress_member_finished. * New functions LZ_decompress_member_version, LZ_decompress_data_crc,
* Added new function LZ_decompress_member_version. LZ_decompress_member_finished, and LZ_decompress_dictionary_size.
* Added new function LZ_decompress_dictionary_size. * Variables declared 'extern' have been encapsulated in a namespace.
* Added new function LZ_decompress_data_crc. * main.cc: Fix warning about fchown's return value being ignored.
* Variables declared 'extern' have been encapsulated in a * decoder.h: Integrate Input_buffer in Range_decoder.
namespace.
* main.cc: Fixed warning about fchown's return value being ignored.
* decoder.h: Input_buffer integrated in Range_decoder.
2010-02-10 Antonio Diaz Diaz <ant_diaz@teleline.es> 2010-02-10 Antonio Diaz Diaz <ant_diaz@teleline.es>
@ -145,28 +179,26 @@
2010-01-17 Antonio Diaz Diaz <ant_diaz@teleline.es> 2010-01-17 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 0.8 released. * Version 0.8 released.
* Added new function LZ_decompress_reset. * New functions LZ_decompress_reset, LZ_decompress_sync_to_member,
* Added new function LZ_decompress_sync_to_member. LZ_decompress_write_size, and LZ_strerror.
* Added new function LZ_decompress_write_size. * lzlib.h: API change. Replace 'enum' with functions for values of
* Added new function LZ_strerror. dictionary size limits to make interface names consistent.
* lzlib.h: API change. Replaced 'enum' with functions for values * lzlib.h: API change. Rename 'LZ_errno' to 'LZ_Errno'.
of dictionary size limits to make interface names consistent. * lzlib.h: API change. Replace 'void *' with 'struct LZ_Encoder *'
* lzlib.h: API change. 'LZ_errno' replaced with 'LZ_Errno'.
* lzlib.h: API change. Replaced 'void *' with 'struct LZ_Encoder *'
and 'struct LZ_Decoder *' to make interface type safe. and 'struct LZ_Decoder *' to make interface type safe.
* decoder.cc: Truncated member trailer is now correctly detected. * decoder.cc: A truncated member trailer is now correctly detected.
* encoder.cc: Matchfinder::reset now also clears at_stream_end_, * encoder.cc: Matchfinder::reset now also clears at_stream_end_,
allowing LZ_compress_restart_member to restart a finished stream. allowing LZ_compress_restart_member to restart a finished stream.
* lzlib.cc: Accept only query or close operations after a fatal * lzlib.cc: Accept only query or close operations after a fatal
error has occurred. error has occurred.
* Shared version of lzlib is no longer built by default. * The shared version of lzlib is no longer built by default.
* check.sh: Use 'test1' instead of 'COPYING' for testing. * check.sh: Use 'test1' instead of 'COPYING' for testing.
2009-10-20 Antonio Diaz Diaz <ant_diaz@teleline.es> 2009-10-20 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 0.7 released. * Version 0.7 released.
* Compression time has been reduced by 4%. * Compression time has been reduced by 4%.
* check.sh: Removed -9 to run in less than 256MiB of RAM. * check.sh: Remove -9 to run in less than 256MiB of RAM.
* lzcheck.cc: Read files of any size up to 2^63 bytes. * lzcheck.cc: Read files of any size up to 2^63 bytes.
2009-09-02 Antonio Diaz Diaz <ant_diaz@teleline.es> 2009-09-02 Antonio Diaz Diaz <ant_diaz@teleline.es>
@ -178,15 +210,14 @@
* Version 0.5 released. * Version 0.5 released.
* Decompression speed has been improved. * Decompression speed has been improved.
* main.cc (signal_handler): Declared as 'extern "C"'. * main.cc (signal_handler): Declare as 'extern "C"'.
2009-06-03 Antonio Diaz Diaz <ant_diaz@teleline.es> 2009-06-03 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 0.4 released. * Version 0.4 released.
* Added new function LZ_compress_sync_flush. * New functions LZ_compress_sync_flush and LZ_compress_write_size.
* Added new function LZ_compress_write_size.
* Decompression speed has been improved. * Decompression speed has been improved.
* Added chapter 'Buffering' to the manual. * lzlib.texinfo: New chapter 'Buffering'.
2009-05-03 Antonio Diaz Diaz <ant_diaz@teleline.es> 2009-05-03 Antonio Diaz Diaz <ant_diaz@teleline.es>
@ -196,16 +227,16 @@
2009-04-26 Antonio Diaz Diaz <ant_diaz@teleline.es> 2009-04-26 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 0.2 released. * Version 0.2 released.
* Fixed a segfault when decompressing trailing garbage. * Fix a segfault when decompressing trailing garbage.
* Fixed a false positive in LZ_(de)compress_finished. * Fix a false positive in LZ_(de)compress_finished.
2009-04-21 Antonio Diaz Diaz <ant_diaz@teleline.es> 2009-04-21 Antonio Diaz Diaz <ant_diaz@teleline.es>
* Version 0.1 released. * Version 0.1 released.
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 Antonio Diaz Diaz.
This file is a collection of facts, and thus it is not copyrightable, This file is a collection of facts, and thus it is not copyrightable,
but just in case, you have unlimited permission to copy, distribute and but just in case, you have unlimited permission to copy, distribute, and
modify it. modify it.

36
INSTALL
View file

@ -1,13 +1,13 @@
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 standards I use gcc 6.1.0 and 4.1.2, but the code should compile with any standards
compliant compiler. compliant compiler.
Gcc is available at http://gcc.gnu.org. Gcc is available at http://gcc.gnu.org.
The operating system must allow signal handlers read access to objects with 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 static storage duration so that the cleanup handler for Control-C can delete
the partial output file. the partial output file. (This requirement is for minilzip only).
Procedure Procedure
@ -41,27 +41,29 @@ the main archive.
documentation. (You may need to run ldconfig also). documentation. (You may need to run ldconfig also).
Or type 'make install-compress', which additionally compresses the Or type 'make install-compress', which additionally compresses the
info manual after installation. (Installing compressed docs may info manual after installation.
become the default in the future). (Installing compressed docs may become the default in the future).
You can install only the library, the info manual or the man page by You can install only the library or the info manual by typing
typing 'make install-lib', 'make install-info' or 'make install-man' 'make install-lib' or 'make install-info' respectively.
respectively.
'make install-bin' installs the minilzip program and its man page. 'make install-bin install-man' installs the program minilzip and its man
'install-bin' installs a shared minilzip if the shared library has page. 'install-bin' installs a shared minilzip if the shared library has
been configured. Else it installs a static minilzip. been configured. Else it installs a static minilzip.
'make install-as-lzip' runs 'make install-bin' and then links 'make install-bin-compress' additionally compresses the man page after
minilzip to the name 'lzip'. installation.
'make install-as-lzip' runs 'make install-bin' and then links minilzip to
the name 'lzip'.
Another way Another way
----------- -----------
You can also compile lzlib into a separate directory. You can also compile lzlib 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
@ -72,7 +74,7 @@ After running 'configure', you can run 'make' and 'make install' as
explained above. explained above.
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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

@ -15,8 +15,10 @@ objs = carg_parser.o main.o
.PHONY : all install install-bin install-info install-man \ .PHONY : all install install-bin install-info install-man \
install-strip install-compress install-strip-compress \ install-strip install-compress install-strip-compress \
install-bin-strip install-info-compress install-man-compress \ install-bin-strip install-info-compress install-man-compress \
install-lib install-lib-strip uninstall-lib install-as-lzip \ install-bin-compress install-bin-strip-compress \
uninstall uninstall-bin uninstall-info uninstall-man \ install-lib install-lib-strip \
install-as-lzip \
uninstall uninstall-bin uninstall-lib uninstall-info uninstall-man \
doc info man check dist clean distclean doc info man check dist clean distclean
all : $(progname_static) $(progname_shared) all : $(progname_static) $(progname_shared)
@ -36,6 +38,9 @@ $(progname)_shared : $(objs) lib$(libname).so.$(pkgversion)
bbexample : bbexample.o lib$(libname).a bbexample : bbexample.o lib$(libname).a
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ bbexample.o lib$(libname).a $(CC) $(LDFLAGS) $(CFLAGS) -o $@ bbexample.o lib$(libname).a
ffexample : ffexample.o lib$(libname).a
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ ffexample.o lib$(libname).a
lzcheck : lzcheck.o lib$(libname).a lzcheck : lzcheck.o lib$(libname).a
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ lzcheck.o lib$(libname).a $(CC) $(LDFLAGS) $(CFLAGS) -o $@ lzcheck.o lib$(libname).a
@ -57,6 +62,7 @@ lzlib.o : Makefile $(lzdeps)
lzlib_sh.o : Makefile $(lzdeps) lzlib_sh.o : Makefile $(lzdeps)
main.o : carg_parser.h lzlib.h main.o : carg_parser.h lzlib.h
bbexample.o : Makefile lzlib.h bbexample.o : Makefile lzlib.h
ffexample.o : Makefile lzlib.h
lzcheck.o : Makefile lzlib.h lzcheck.o : Makefile lzlib.h
@ -75,15 +81,17 @@ $(VPATH)/doc/$(progname).1 : $(progname)
Makefile : $(VPATH)/configure $(VPATH)/Makefile.in Makefile : $(VPATH)/configure $(VPATH)/Makefile.in
./config.status ./config.status
check : $(progname) bbexample lzcheck check : $(progname) bbexample ffexample lzcheck
@$(VPATH)/testsuite/check.sh $(VPATH)/testsuite $(pkgversion) @$(VPATH)/testsuite/check.sh $(VPATH)/testsuite $(pkgversion)
install : install-lib install-info install : install-lib install-info
install-strip : install-lib-strip install-info install-strip : install-lib-strip install-info
install-compress : install-lib install-info-compress install-compress : install-lib install-info-compress
install-strip-compress : install-lib-strip install-info-compress install-strip-compress : install-lib-strip install-info-compress
install-bin-compress : install-bin install-man-compress
install-bin-strip-compress : install-bin-strip install-man-compress
install-bin : all install-man install-bin : all
if [ ! -d "$(DESTDIR)$(bindir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(bindir)" ; fi if [ ! -d "$(DESTDIR)$(bindir)" ] ; then $(INSTALL_DIR) "$(DESTDIR)$(bindir)" ; fi
$(INSTALL_PROGRAM) ./$(progname_lzip) "$(DESTDIR)$(bindir)/$(progname)" $(INSTALL_PROGRAM) ./$(progname_lzip) "$(DESTDIR)$(bindir)/$(progname)"
@ -119,7 +127,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
@ -139,7 +147,7 @@ install-as-lzip : install-bin
uninstall : uninstall-info uninstall-lib uninstall : uninstall-info uninstall-lib
uninstall-bin : uninstall-man uninstall-bin :
-rm -f "$(DESTDIR)$(bindir)/$(progname)" -rm -f "$(DESTDIR)$(bindir)/$(progname)"
uninstall-lib : uninstall-lib :
@ -151,7 +159,7 @@ uninstall-lib :
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"*
@ -177,16 +185,19 @@ dist : doc
$(DISTNAME)/*.c \ $(DISTNAME)/*.c \
$(DISTNAME)/testsuite/check.sh \ $(DISTNAME)/testsuite/check.sh \
$(DISTNAME)/testsuite/test.txt \ $(DISTNAME)/testsuite/test.txt \
$(DISTNAME)/testsuite/test2.txt \ $(DISTNAME)/testsuite/fox_lf \
$(DISTNAME)/testsuite/fox.lz \
$(DISTNAME)/testsuite/fox_*.lz \
$(DISTNAME)/testsuite/test_sync.lz \ $(DISTNAME)/testsuite/test_sync.lz \
$(DISTNAME)/testsuite/test.txt.lz $(DISTNAME)/testsuite/test.txt.lz \
$(DISTNAME)/testsuite/test_em.txt.lz
rm -f $(DISTNAME) rm -f $(DISTNAME)
lzip -v -9 $(DISTNAME).tar lzip -v -9 $(DISTNAME).tar
clean : clean :
-rm -f $(progname) $(objs) -rm -f $(progname) $(objs) lzlib.o lib$(libname).a
-rm -f $(progname)_shared lzlib_sh.o *.so.$(pkgversion) -rm -f $(progname)_shared lzlib_sh.o lib$(libname).so*
-rm -f bbexample bbexample.o lzcheck lzcheck.o lzlib.o *.a -rm -f bbexample bbexample.o ffexample ffexample.o lzcheck lzcheck.o
distclean : clean distclean : clean
-rm -f Makefile config.status *.tar *.tar.lz -rm -f Makefile config.status *.tar *.tar.lz

104
NEWS
View file

@ -1,30 +1,92 @@
Changes in version 1.11: Changes in version 1.12:
In case of decompression error caused by corrupt or truncated data, The value of the version test macro 'LZ_API_VERSION' is now defined as
LZ_decompress_read now does not signal the error immediately to the 1000 * major + minor. For version 1.12 it is 1012.
application, but waits until all decoded bytes have been read. This This change does not affect the soversion.
allows tools like tarlz to recover as much data as possible from damaged
members.
It is now an error to specify two different operations to minilzip in The new function 'LZ_api_version', which returns the LZ_API_VERSION of the
the command line (--decompress and --test). library object code being used, has been added to lzlib.
Minilzip now also checks for errors when closing the input file. If end of file is found at member trailer or EOS marker,
'LZ_decompress_errno' now returns 'LZ_unexpected_eof' instead of
'LZ_data_error'.
Minilzip now compiles on DOS with DJGPP. (Patch from Robert Riebisch). Decompression speed has been slightly increased.
The descriptions of '-0..-9', '-m' and '-s' in the manual have been A bug has been fixed in minilzip that falsely reported a library stall when
improved. decompressing a file with empty members.
It has been documented that it is safe to call 'LZ_(de)compress_finish' The new option '--check-lib', which compares the version of lzlib used to
as many times as needed. Examples in the manual have been simplified. compile minilzip with the version actually being used, has been added to
minilzip.
The configure script now accepts appending options to CFLAGS using the Minilzip now reports an error if a file name is empty (minilzip -t "").
syntax 'CFLAGS+=OPTIONS'.
Makefile targets 'install-bin*' have been renamed to 'install-lib*'. New Option '-o, --output' now behaves like '-c, --stdout', but sending the
'install-bin*' targets have been created that install a shared minilzip, output unconditionally to a file instead of to standard output. See the new
or a static minilzip if the shared library has not been configured. description of '-o' in the manual. This change is not backwards compatible.
Therefore commands like:
minilzip -o foo.lz - bar < foo
must now be split into:
minilzip -o foo.lz - < foo
minilzip bar
or rewritten as:
minilzip - bar < foo > foo.lz
It has been documented in INSTALL the use of When using '-c' or '-o', minilzip now checks whether the output is a
CFLAGS+='-D __USE_MINGW_ANSI_STDIO' when compiling on MinGW. terminal only once.
Minilzip now does not even open the output file if the input file is a terminal.
The words 'decompressed' and 'compressed' have been replaced with the
shorter 'out' and 'in' in the verbose output of minilzip when decompressing
or testing.
It has been documented in the manual that 'LZ_(de)compress_close' and
'LZ_(de)compress_errno' can be called with a null argument.
It has been documented in the manual that the LZMA marker '3' ("Sync Flush"
marker) is not allowed in lzip files. Marker '3' is a device for interactive
communication between applications using lzlib, but is useless and wasteful
in a file, and is excluded from the media type 'application/lzip'. The LZMA
marker '2' ("End Of Stream" marker) is the only marker allowed in lzip
files.
It has been documented in the manual that not calling 'LZ_decompress_finish'
prevents lzlib from detecting a truncated member.
It has been documented in the manual that 'LZ_decompress_read' returns at
least once per member so that 'LZ_decompress_member_finished' can be called
(and trailer data retrieved) for each member, even for empty members.
Therefore, 'LZ_decompress_read' returning 0 does not mean that the end of
the stream has been reached.
It has been documented in the manual that 'LZ_(de)compress_read' can be
called with a null buffer pointer argument.
Real code examples for the most common uses of the library have been added
to the tutorial.
'bbexample.c' has been simplified to not use 'LZ_(de)compress_write_size'.
'lzcheck' now accepts options '-s' (to check LZ_compress_sync_flush) and
'-m' (to check member by member decompression).
'lzcheck.c' now also tests member by member decompression without
intermediate calls to 'LZ_decompress_finish'.
The new file 'ffexample.c', containing example functions for file-to-file
compression/decompression, has been added to the distribution.
The commands needed to extract files from a tar.lz archive have been
documented in the output of 'minilzip --help' and in the man page.
'make install-bin' no longer installs the minilzip man page. This is to
prevent 'make install-bin install-man-compress' from installing the man page
twice before compressing it.
The new targets 'install-bin-compress' and 'install-bin-strip-compress',
which install a (stripped) minilzip and a compressed man page, have been
added to the Makefile.
9 new test files have been added to the testsuite.

114
README
View file

@ -1,65 +1,64 @@
Description Description
Lzlib is a data compression library providing in-memory LZMA compression Lzlib is a data compression library providing in-memory LZMA compression and
and decompression functions, including integrity checking of the decompression functions, including integrity checking of the decompressed
decompressed data. The compressed data format used by the library is the data. The compressed data format used by the library is the lzip format.
lzip format. Lzlib is written in C. Lzlib is written in C.
The lzip file format is designed for data sharing and long-term archiving, The lzip file format is designed for data sharing and long-term archiving,
taking into account both data integrity and decoder availability: taking into account both data integrity and decoder availability:
* The lzip format provides very safe integrity checking and some data * The lzip format provides very safe integrity checking and some data
recovery means. The lziprecover program can repair bit flip errors recovery means. The program lziprecover can repair bit flip errors
(one of the most common forms of data corruption) in lzip files, (one of the most common forms of data corruption) in lzip files, and
and provides data recovery capabilities, including error-checked provides data recovery capabilities, including error-checked merging
merging of damaged copies of a file. of damaged copies of a file.
* The lzip format is as simple as possible (but not simpler). The * The lzip format is as simple as possible (but not simpler). The lzip
lzip manual provides the source code of a simple decompressor manual provides the source code of a simple decompressor along with a
along with a detailed explanation of how it works, so that with detailed explanation of how it works, so that with the only help of the
the only help of the lzip manual it would be possible for a lzip manual it would be possible for a digital archaeologist to extract
digital archaeologist to extract the data from a lzip file long the data from a lzip file long after quantum computers eventually
after quantum computers eventually render LZMA obsolete. render LZMA obsolete.
* Additionally the lzip reference implementation is copylefted, which * Additionally the lzip reference implementation is copylefted, which
guarantees that it will remain free forever. guarantees that it will remain free forever.
A nice feature of the lzip format is that a corrupt byte is easier to A nice feature of the lzip format is that a corrupt byte is easier to repair
repair the nearer it is from the beginning of the file. Therefore, with the nearer it is from the beginning of the file. Therefore, with the help of
the help of lziprecover, losing an entire archive just because of a lziprecover, losing an entire archive just because of a corrupt byte near
corrupt byte near the beginning is a thing of the past. the beginning is a thing of the past.
The functions and variables forming the interface of the compression The functions and variables forming the interface of the compression library
library are declared in the file 'lzlib.h'. Usage examples of the are declared in the file 'lzlib.h'. Usage examples of the library are given
library are given in the files 'main.c' and 'bbexample.c' from the in the files 'bbexample.c', 'ffexample.c', and 'main.c' from the source
source distribution. distribution.
Compression/decompression is done by repeatedly calling a couple of Compression/decompression is done by repeatedly calling a couple of
read/write functions until all the data have been processed by the read/write functions until all the data have been processed by the library.
library. This interface is safer and less error prone than the This interface is safer and less error prone than the traditional zlib
traditional zlib interface. interface.
Compression/decompression is done when the read function is called. This Compression/decompression is done when the read function is called. This
means the value returned by the position functions will not be updated means the value returned by the position functions will not be updated until
until a read call, even if a lot of data are written. If you want the a read call, even if a lot of data are written. If you want the data to be
data to be compressed in advance, just call the read function with a compressed in advance, just call the read function with a size equal to 0.
size equal to 0.
If all the data to be compressed are written in advance, lzlib will If all the data to be compressed are written in advance, lzlib will
automatically adjust the header of the compressed data to use the automatically adjust the header of the compressed data to use the largest
largest dictionary size that does not exceed neither the data size nor dictionary size that does not exceed neither the data size nor the limit
the limit given to LZ_compress_open. This feature reduces the amount of given to 'LZ_compress_open'. This feature reduces the amount of memory
memory needed for decompression and allows minilzip to produce identical needed for decompression and allows minilzip to produce identical compressed
compressed output as lzip. output as lzip.
Lzlib will correctly decompress a data stream which is the concatenation Lzlib will correctly decompress a data stream which is the concatenation of
of two or more compressed data streams. The result is the concatenation two or more compressed data streams. The result is the concatenation of the
of the corresponding decompressed data streams. Integrity testing of corresponding decompressed data streams. Integrity testing of concatenated
concatenated compressed data streams is also supported. compressed data streams is also supported.
Lzlib is able to compress and decompress streams of unlimited size by Lzlib is able to compress and decompress streams of unlimited size by
automatically creating multimember output. The members so created are automatically creating multimember output. The members so created are large,
large, about 2 PiB each. about 2 PiB each.
All the library functions are thread safe. The library does not install All the library functions are thread safe. The library does not install
any signal handler. The decoder checks the consistency of the compressed any signal handler. The decoder checks the consistency of the compressed
@ -67,16 +66,15 @@ data, so the library should never crash even in case of corrupted input.
In spite of its name (Lempel-Ziv-Markov chain-Algorithm), LZMA is not a In spite of its name (Lempel-Ziv-Markov chain-Algorithm), LZMA is not a
concrete algorithm; it is more like "any algorithm using the LZMA coding concrete algorithm; it is more like "any algorithm using the LZMA coding
scheme". For example, the option '-0' of lzip uses the scheme in almost scheme". For example, the option '-0' of lzip uses the scheme in almost the
the simplest way possible; issuing the longest match it can find, or a simplest way possible; issuing the longest match it can find, or a literal
literal byte if it can't find a match. Inversely, a much more elaborated byte if it can't find a match. Inversely, a much more elaborated way of
way of finding coding sequences of minimum size than the one currently finding coding sequences of minimum size than the one currently used by lzip
used by lzip could be developed, and the resulting sequence could also could be developed, and the resulting sequence could also be coded using the
be coded using the LZMA coding scheme. LZMA coding scheme.
Lzlib currently implements two variants of the LZMA algorithm; fast Lzlib currently implements two variants of the LZMA algorithm; fast (used by
(used by option '-0' of minilzip) and normal (used by all other option '-0' of minilzip) and normal (used by all other compression levels).
compression levels).
The high compression of LZMA comes from combining two basic, well-proven The high compression of LZMA comes from combining two basic, well-proven
compression ideas: sliding dictionaries (LZ77/78) and markov models (the compression ideas: sliding dictionaries (LZ77/78) and markov models (the
@ -85,20 +83,20 @@ similar order-0 entropy coder as its last stage) with segregation of
contexts according to what the bits are used for. contexts according to what the bits are used for.
The ideas embodied in lzlib are due to (at least) the following people: The ideas embodied in lzlib are due to (at least) the following people:
Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for the
the definition of Markov chains), G.N.N. Martin (for the definition of definition of Markov chains), G.N.N. Martin (for the definition of range
range encoding), Igor Pavlov (for putting all the above together in encoding), Igor Pavlov (for putting all the above together in LZMA), and
LZMA), and Julian Seward (for bzip2's CLI). Julian Seward (for bzip2's CLI).
LANGUAGE NOTE: Uncompressed = not compressed = plain data; it may never LANGUAGE NOTE: Uncompressed = not compressed = plain data; it may never have
have been compressed. Decompressed is used to refer to data which have been compressed. Decompressed is used to refer to data which have undergone
undergone the process of decompression. the process of decompression.
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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. It has the same copyright owner and permissions that configure Makefile. It has the same copyright owner and permissions that configure

View file

@ -1,21 +1,20 @@
/* Buffer to buffer example - Test program for the lzlib library /* Buffer to buffer example - Test program for the library lzlib
Copyright (C) 2010-2019 Antonio Diaz Diaz. Copyright (C) 2010-2021 Antonio Diaz Diaz.
This program is free software: you have unlimited permission This program is free software: you have unlimited permission
to copy, distribute and modify it. to copy, distribute, and modify it.
Usage is: Usage: bbexample filename
bbexample filename
This program is an example of how buffer-to-buffer This program is an example of how buffer-to-buffer
compression/decompression can be implemented using lzlib. compression/decompression can be implemented using lzlib.
*/ */
#define _FILE_OFFSET_BITS 64
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#ifndef __cplusplus
#include <stdbool.h> #include <stdbool.h>
#endif
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -24,6 +23,10 @@
#include "lzlib.h" #include "lzlib.h"
#ifndef min
#define min(x,y) ((x) <= (y) ? (x) : (y))
#endif
/* Returns the address of a malloc'd buffer containing the file data and /* Returns the address of a malloc'd buffer containing the file data and
the file size in '*file_sizep'. the file size in '*file_sizep'.
@ -35,28 +38,26 @@ uint8_t * read_file( const char * const name, long * const file_sizep )
uint8_t * buffer, * tmp; uint8_t * buffer, * tmp;
FILE * const f = fopen( name, "rb" ); FILE * const f = fopen( name, "rb" );
if( !f ) if( !f )
{ { fprintf( stderr, "bbexample: Can't open input file '%s': %s\n",
fprintf( stderr, "bbexample: Can't open input file '%s': %s\n", name, strerror( errno ) ); return 0; }
name, strerror( errno ) );
return 0;
}
buffer = (uint8_t *)malloc( buffer_size ); buffer = (uint8_t *)malloc( buffer_size );
if( !buffer ) if( !buffer )
{ fputs( "bbexample: Not enough memory.\n", stderr ); return 0; } { fputs( "bbexample: read_file: Not enough memory.\n", stderr );
fclose( f ); return 0; }
file_size = fread( buffer, 1, buffer_size, f ); file_size = fread( buffer, 1, buffer_size, f );
while( file_size >= buffer_size ) while( file_size >= buffer_size )
{ {
if( buffer_size >= LONG_MAX ) if( buffer_size >= LONG_MAX )
{ {
fprintf( stderr, "bbexample: Input file '%s' is too large.\n", name ); fprintf( stderr, "bbexample: Input file '%s' is too large.\n", name );
free( buffer ); return 0; free( buffer ); fclose( f ); 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;
tmp = (uint8_t *)realloc( buffer, buffer_size ); tmp = (uint8_t *)realloc( buffer, buffer_size );
if( !tmp ) if( !tmp )
{ fputs( "bbexample: Not enough memory.\n", stderr ); { fputs( "bbexample: read_file: Not enough memory.\n", stderr );
free( buffer ); return 0; } free( buffer ); fclose( f ); return 0; }
buffer = tmp; buffer = tmp;
file_size += fread( buffer + file_size, 1, buffer_size - file_size, f ); file_size += fread( buffer + file_size, 1, buffer_size - file_size, f );
} }
@ -64,7 +65,7 @@ uint8_t * read_file( const char * const name, long * const file_sizep )
{ {
fprintf( stderr, "bbexample: Error reading file '%s': %s\n", fprintf( stderr, "bbexample: Error reading file '%s': %s\n",
name, strerror( errno ) ); name, strerror( errno ) );
free( buffer ); return 0; free( buffer ); fclose( f ); return 0;
} }
fclose( f ); fclose( f );
*file_sizep = file_size; *file_sizep = file_size;
@ -72,13 +73,13 @@ uint8_t * read_file( const char * const name, long * const file_sizep )
} }
/* Compresses 'size' bytes from 'data'. Returns the address of a /* Compresses 'insize' bytes from 'inbuf'.
malloc'd buffer containing the compressed data and its size in Returns the address of a malloc'd buffer containing the compressed data,
'*out_sizep'. and the size of the data in '*outlenp'.
In case of error, returns 0 and does not modify '*out_sizep'. In case of error, returns 0 and does not modify '*outlenp'.
*/ */
uint8_t * bbcompress( const uint8_t * const data, const long size, uint8_t * bbcompressl( const uint8_t * const inbuf, const long insize,
const int level, long * const out_sizep ) const int level, long * const outlenp )
{ {
struct Lzma_options struct Lzma_options
{ {
@ -100,132 +101,246 @@ uint8_t * bbcompress( const uint8_t * const data, const long size,
{ 3 << 23, 132 }, /* -8 */ { 3 << 23, 132 }, /* -8 */
{ 1 << 25, 273 } }; /* -9 */ { 1 << 25, 273 } }; /* -9 */
struct Lzma_options encoder_options; struct Lzma_options encoder_options;
const unsigned long long member_size = 0x7FFFFFFFFFFFFFFFULL; /* INT64_MAX */
struct LZ_Encoder * encoder; struct LZ_Encoder * encoder;
uint8_t * new_data; uint8_t * outbuf;
const long delta_size = ( size / 4 ) + 64; /* size may be zero */ const long delta_size = ( insize / 4 ) + 64; /* insize may be zero */
long new_data_size = delta_size; /* initial size */ long outsize = delta_size; /* initial outsize */
long new_pos = 0; long inpos = 0;
long written = 0; long outpos = 0;
bool error = false; bool error = false;
if( level < 0 || level > 9 ) return 0; if( level < 0 || level > 9 ) return 0;
encoder_options = option_mapping[level]; encoder_options = option_mapping[level];
if( encoder_options.dictionary_size > size && level != 0 ) if( encoder_options.dictionary_size > insize && level != 0 )
encoder_options.dictionary_size = size; /* saves memory */ encoder_options.dictionary_size = insize; /* saves memory */
if( encoder_options.dictionary_size < LZ_min_dictionary_size() ) if( encoder_options.dictionary_size < LZ_min_dictionary_size() )
encoder_options.dictionary_size = LZ_min_dictionary_size(); encoder_options.dictionary_size = LZ_min_dictionary_size();
encoder = LZ_compress_open( encoder_options.dictionary_size, encoder = LZ_compress_open( encoder_options.dictionary_size,
encoder_options.match_len_limit, member_size ); encoder_options.match_len_limit, INT64_MAX );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok ) outbuf = (uint8_t *)malloc( outsize );
{ LZ_compress_close( encoder ); return 0; } if( !encoder || LZ_compress_errno( encoder ) != LZ_ok || !outbuf )
{ free( outbuf ); LZ_compress_close( encoder ); return 0; }
new_data = (uint8_t *)malloc( new_data_size );
if( !new_data )
{ LZ_compress_close( encoder ); return 0; }
while( true ) while( true )
{ {
int rd; int ret = LZ_compress_write( encoder, inbuf + inpos,
if( LZ_compress_write_size( encoder ) > 0 ) min( INT_MAX, insize - inpos ) );
{ if( ret < 0 ) { error = true; break; }
if( written < size ) inpos += ret;
{ if( inpos >= insize ) LZ_compress_finish( encoder );
const int wr = LZ_compress_write( encoder, data + written, ret = LZ_compress_read( encoder, outbuf + outpos,
size - written ); min( INT_MAX, outsize - outpos ) );
if( wr < 0 ) { error = true; break; } if( ret < 0 ) { error = true; break; }
written += wr; outpos += ret;
}
if( written >= size ) LZ_compress_finish( encoder );
}
rd = LZ_compress_read( encoder, new_data + new_pos,
new_data_size - new_pos );
if( rd < 0 ) { error = true; break; }
new_pos += rd;
if( LZ_compress_finished( encoder ) == 1 ) break; if( LZ_compress_finished( encoder ) == 1 ) break;
if( new_pos >= new_data_size ) if( outpos >= outsize )
{ {
uint8_t * tmp; uint8_t * tmp;
if( new_data_size > LONG_MAX - delta_size ) { error = true; break; } if( outsize > LONG_MAX - delta_size ) { error = true; break; }
new_data_size += delta_size; outsize += delta_size;
tmp = (uint8_t *)realloc( new_data, new_data_size ); tmp = (uint8_t *)realloc( outbuf, outsize );
if( !tmp ) { error = true; break; } if( !tmp ) { error = true; break; }
new_data = tmp; outbuf = tmp;
} }
} }
if( LZ_compress_close( encoder ) < 0 ) error = true; if( LZ_compress_close( encoder ) < 0 ) error = true;
if( error ) { free( new_data ); return 0; } if( error ) { free( outbuf ); return 0; }
*out_sizep = new_pos; *outlenp = outpos;
return new_data; return outbuf;
} }
/* Decompresses 'size' bytes from 'data'. Returns the address of a /* Decompresses 'insize' bytes from 'inbuf'.
malloc'd buffer containing the decompressed data and its size in Returns the address of a malloc'd buffer containing the decompressed
'*out_sizep'. data, and the size of the data in '*outlenp'.
In case of error, returns 0 and does not modify '*out_sizep'. In case of error, returns 0 and does not modify '*outlenp'.
*/ */
uint8_t * bbdecompress( const uint8_t * const data, const long size, uint8_t * bbdecompressl( const uint8_t * const inbuf, const long insize,
long * const out_sizep ) long * const outlenp )
{ {
struct LZ_Decoder * const decoder = LZ_decompress_open(); struct LZ_Decoder * const decoder = LZ_decompress_open();
uint8_t * new_data; const long delta_size = insize; /* insize must be > zero */
const long delta_size = size; /* size must be > zero */ long outsize = delta_size; /* initial outsize */
long new_data_size = delta_size; /* initial size */ uint8_t * outbuf = (uint8_t *)malloc( outsize );
long new_pos = 0; long inpos = 0;
long written = 0; long outpos = 0;
bool error = false; bool error = false;
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok || !outbuf )
{ LZ_decompress_close( decoder ); return 0; } { free( outbuf ); LZ_decompress_close( decoder ); return 0; }
new_data = (uint8_t *)malloc( new_data_size );
if( !new_data )
{ LZ_decompress_close( decoder ); return 0; }
while( true ) while( true )
{ {
int rd; int ret = LZ_decompress_write( decoder, inbuf + inpos,
if( LZ_decompress_write_size( decoder ) > 0 ) min( INT_MAX, insize - inpos ) );
{ if( ret < 0 ) { error = true; break; }
if( written < size ) inpos += ret;
{ if( inpos >= insize ) LZ_decompress_finish( decoder );
const int wr = LZ_decompress_write( decoder, data + written, ret = LZ_decompress_read( decoder, outbuf + outpos,
size - written ); min( INT_MAX, outsize - outpos ) );
if( wr < 0 ) { error = true; break; } if( ret < 0 ) { error = true; break; }
written += wr; outpos += ret;
}
if( written >= size ) LZ_decompress_finish( decoder );
}
rd = LZ_decompress_read( decoder, new_data + new_pos,
new_data_size - new_pos );
if( rd < 0 ) { error = true; break; }
new_pos += rd;
if( LZ_decompress_finished( decoder ) == 1 ) break; if( LZ_decompress_finished( decoder ) == 1 ) break;
if( new_pos >= new_data_size ) if( outpos >= outsize )
{ {
uint8_t * tmp; uint8_t * tmp;
if( new_data_size > LONG_MAX - delta_size ) { error = true; break; } if( outsize > LONG_MAX - delta_size ) { error = true; break; }
new_data_size += delta_size; outsize += delta_size;
tmp = (uint8_t *)realloc( new_data, new_data_size ); tmp = (uint8_t *)realloc( outbuf, outsize );
if( !tmp ) { error = true; break; } if( !tmp ) { error = true; break; }
new_data = tmp; outbuf = tmp;
} }
} }
if( LZ_decompress_close( decoder ) < 0 ) error = true; if( LZ_decompress_close( decoder ) < 0 ) error = true;
if( error ) { free( new_data ); return 0; } if( error ) { free( outbuf ); return 0; }
*out_sizep = new_pos; *outlenp = outpos;
return new_data; return outbuf;
}
/* Test the whole file at all levels. */
int full_test( const uint8_t * const inbuf, const long insize )
{
int level;
for( level = 0; level <= 9; ++level )
{
long midsize = 0, outsize = 0;
uint8_t * outbuf;
uint8_t * midbuf = bbcompressl( inbuf, insize, level, &midsize );
if( !midbuf )
{ fputs( "bbexample: full_test: Not enough memory or compress error.\n",
stderr ); return 1; }
outbuf = bbdecompressl( midbuf, midsize, &outsize );
free( midbuf );
if( !outbuf )
{ fputs( "bbexample: full_test: Not enough memory or decompress error.\n",
stderr ); return 1; }
if( insize != outsize ||
( insize > 0 && memcmp( inbuf, outbuf, insize ) != 0 ) )
{ fputs( "bbexample: full_test: Decompressed data differs from original.\n",
stderr ); free( outbuf ); return 1; }
free( outbuf );
}
return 0;
}
/* Compresses 'insize' bytes from 'inbuf' to 'outbuf'.
Returns the size of the compressed data in '*outlenp'.
In case of error, or if 'outsize' is too small, returns false and does
not modify '*outlenp'.
*/
bool bbcompress( const uint8_t * const inbuf, const int insize,
const int dictionary_size, const int match_len_limit,
uint8_t * const outbuf, const int outsize,
int * const outlenp )
{
int inpos = 0, outpos = 0;
bool error = false;
struct LZ_Encoder * const encoder =
LZ_compress_open( dictionary_size, match_len_limit, INT64_MAX );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
{ LZ_compress_close( encoder ); return false; }
while( true )
{
int ret = LZ_compress_write( encoder, inbuf + inpos, insize - inpos );
if( ret < 0 ) { error = true; break; }
inpos += ret;
if( inpos >= insize ) LZ_compress_finish( encoder );
ret = LZ_compress_read( encoder, outbuf + outpos, outsize - outpos );
if( ret < 0 ) { error = true; break; }
outpos += ret;
if( LZ_compress_finished( encoder ) == 1 ) break;
if( outpos >= outsize ) { error = true; break; }
}
if( LZ_compress_close( encoder ) < 0 ) error = true;
if( error ) return false;
*outlenp = outpos;
return true;
}
/* Decompresses 'insize' bytes from 'inbuf' to 'outbuf'.
Returns the size of the decompressed data in '*outlenp'.
In case of error, or if 'outsize' is too small, returns false and does
not modify '*outlenp'.
*/
bool bbdecompress( const uint8_t * const inbuf, const int insize,
uint8_t * const outbuf, const int outsize,
int * const outlenp )
{
int inpos = 0, outpos = 0;
bool error = false;
struct LZ_Decoder * const decoder = LZ_decompress_open();
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ LZ_decompress_close( decoder ); return false; }
while( true )
{
int ret = LZ_decompress_write( decoder, inbuf + inpos, insize - inpos );
if( ret < 0 ) { error = true; break; }
inpos += ret;
if( inpos >= insize ) LZ_decompress_finish( decoder );
ret = LZ_decompress_read( decoder, outbuf + outpos, outsize - outpos );
if( ret < 0 ) { error = true; break; }
outpos += ret;
if( LZ_decompress_finished( decoder ) == 1 ) break;
if( outpos >= outsize ) { error = true; break; }
}
if( LZ_decompress_close( decoder ) < 0 ) error = true;
if( error ) return false;
*outlenp = outpos;
return true;
}
/* Test at most INT_MAX bytes from the file with buffers of fixed size. */
int fixed_test( const uint8_t * const inbuf, const int insize )
{
int dictionary_size = 65535; /* fast encoder */
int midsize = min( INT_MAX, ( insize / 8 ) * 9LL + 44 ), outsize = insize;
uint8_t * midbuf = (uint8_t *)malloc( midsize );
uint8_t * outbuf = (uint8_t *)malloc( outsize );
if( !midbuf || !outbuf )
{ fputs( "bbexample: fixed_test: Not enough memory.\n", stderr );
free( outbuf ); free( midbuf ); return 1; }
for( ; dictionary_size <= 8 << 20; dictionary_size += 8323073 )
{
int midlen, outlen;
if( !bbcompress( inbuf, insize, dictionary_size, 16, midbuf, midsize, &midlen ) )
{ fputs( "bbexample: fixed_test: Not enough memory or compress error.\n",
stderr ); free( outbuf ); free( midbuf ); return 1; }
if( !bbdecompress( midbuf, midlen, outbuf, outsize, &outlen ) )
{ fputs( "bbexample: fixed_test: Not enough memory or decompress error.\n",
stderr ); free( outbuf ); free( midbuf ); return 1; }
if( insize != outlen ||
( insize > 0 && memcmp( inbuf, outbuf, insize ) != 0 ) )
{ fputs( "bbexample: fixed_test: Decompressed data differs from original.\n",
stderr ); free( outbuf ); free( midbuf ); return 1; }
}
free( outbuf );
free( midbuf );
return 0;
} }
int main( const int argc, const char * const argv[] ) int main( const int argc, const char * const argv[] )
{ {
uint8_t * in_buffer; int retval = 0, i;
long in_size = 0; int open_failures = 0;
int level; const bool verbose = ( argc > 2 );
if( argc < 2 ) if( argc < 2 )
{ {
@ -233,38 +348,20 @@ int main( const int argc, const char * const argv[] )
return 1; return 1;
} }
in_buffer = read_file( argv[1], &in_size ); for( i = 1; i < argc && retval == 0; ++i )
if( !in_buffer ) return 1;
for( level = 0; level <= 9; ++level )
{ {
uint8_t * mid_buffer, * out_buffer; long insize;
long mid_size = 0, out_size = 0; uint8_t * const inbuf = read_file( argv[i], &insize );
if( !inbuf ) { ++open_failures; continue; }
if( verbose ) fprintf( stderr, " Testing file '%s'\n", argv[i] );
mid_buffer = bbcompress( in_buffer, in_size, level, &mid_size ); retval = full_test( inbuf, insize );
if( !mid_buffer ) if( retval == 0 ) retval = fixed_test( inbuf, min( INT_MAX, insize ) );
{ free( inbuf );
fputs( "bbexample: Not enough memory or compress error.\n", stderr );
return 1;
}
out_buffer = bbdecompress( mid_buffer, mid_size, &out_size );
if( !out_buffer )
{
fputs( "bbexample: Not enough memory or decompress error.\n", stderr );
return 1;
}
if( in_size != out_size ||
( in_size > 0 && memcmp( in_buffer, out_buffer, in_size ) != 0 ) )
{
fputs( "bbexample: Decompressed data differs from original.\n", stderr );
return 1;
}
free( out_buffer );
free( mid_buffer );
} }
free( in_buffer ); if( open_failures > 0 && verbose )
return 0; fprintf( stderr, "bbexample: warning: %d %s failed to open.\n",
open_failures, ( open_failures == 1 ) ? "file" : "files" );
if( retval == 0 && open_failures ) retval = 1;
return retval;
} }

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-2019 Antonio Diaz Diaz. Copyright (C) 2006-2021 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>

View file

@ -1,44 +1,44 @@
/* Arg_parser - POSIX/GNU command line argument parser. (C version) /* Arg_parser - POSIX/GNU command line argument parser. (C version)
Copyright (C) 2006-2019 Antonio Diaz Diaz. Copyright (C) 2006-2021 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 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 option. long-only 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 for optional option arguments is '-<short_option><argument>'
(without whitespace), or '--<long_option>=<argument>'. (without whitespace), or '--<long_option>=<argument>'.
*/ */
#ifdef __cplusplus #ifdef __cplusplus
@ -79,11 +79,11 @@ void ap_free( struct Arg_parser * const ap );
const char * ap_error( const struct Arg_parser * const ap ); const char * ap_error( const struct 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 struct 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 struct Arg_parser * const ap, const int i );
const char * ap_argument( const struct Arg_parser * const ap, const int i ); const char * ap_argument( const struct Arg_parser * const ap, const int i );

View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
struct Circular_buffer struct Circular_buffer

25
configure vendored
View file

@ -1,12 +1,12 @@
#! /bin/sh #! /bin/sh
# configure script for Lzlib - Compression library for the lzip format # configure script for Lzlib - Compression library for the lzip format
# Copyright (C) 2009-2019 Antonio Diaz Diaz. # Copyright (C) 2009-2021 Antonio Diaz Diaz.
# #
# This configure script is free software: you have unlimited permission # This configure script is free software: you have unlimited permission
# to copy, distribute and modify it. # to copy, distribute, and modify it.
pkgname=lzlib pkgname=lzlib
pkgversion=1.11 pkgversion=1.12
soversion=1 soversion=1
progname=minilzip progname=minilzip
progname_static=${progname} progname_static=${progname}
@ -34,11 +34,7 @@ CFLAGS='-Wall -W -O2'
LDFLAGS= LDFLAGS=
# 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=
@ -50,11 +46,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
@ -151,7 +148,7 @@ if [ -z "${srcdir}" ] ; then
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
@ -174,7 +171,7 @@ 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
@ -198,11 +195,11 @@ echo "LDFLAGS = ${LDFLAGS}"
rm -f Makefile rm -f Makefile
cat > Makefile << EOF cat > Makefile << EOF
# Makefile for Lzlib - Compression library for the lzip format # Makefile for Lzlib - Compression library for the lzip format
# Copyright (C) 2009-2019 Antonio Diaz Diaz. # Copyright (C) 2009-2021 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}

View file

@ -1,27 +1,27 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
static int LZd_try_verify_trailer( struct LZ_decoder * const d ) static int LZd_try_verify_trailer( struct LZ_decoder * const d )
{ {
Lzip_trailer trailer; Lzip_trailer trailer;
if( Rd_available_bytes( d->rdec ) < Lt_size && !d->rdec->at_stream_end ) if( Rd_available_bytes( d->rdec ) < Lt_size )
return 0; { if( !d->rdec->at_stream_end ) return 0; else return 2; }
d->verify_trailer_pending = false; d->verify_trailer_pending = false;
d->member_finished = true; d->member_finished = true;
@ -40,10 +40,10 @@ static int LZd_decode_member( struct LZ_decoder * const d )
{ {
struct Range_decoder * const rdec = d->rdec; struct Range_decoder * const rdec = d->rdec;
State * const state = &d->state; State * const state = &d->state;
/* unsigned old_mpos = d->rdec->member_position; */ /* unsigned old_mpos = rdec->member_position; */
if( d->member_finished ) return 0; if( d->member_finished ) return 0;
if( !Rd_try_reload( rdec, false ) ) if( !Rd_try_reload( rdec ) )
{ if( !rdec->at_stream_end ) return 0; else return 2; } { if( !rdec->at_stream_end ) return 0; else return 2; }
if( d->verify_trailer_pending ) return LZd_try_verify_trailer( d ); if( d->verify_trailer_pending ) return LZd_try_verify_trailer( d );
@ -51,7 +51,7 @@ static int LZd_decode_member( struct LZ_decoder * const d )
{ {
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;
/* const unsigned mpos = d->rdec->member_position; /* const unsigned mpos = rdec->member_position;
if( mpos - old_mpos > rd_min_available_bytes ) return 5; if( mpos - old_mpos > rd_min_available_bytes ) return 5;
old_mpos = mpos; */ old_mpos = mpos; */
if( !Rd_enough_available_bytes( rdec ) ) /* check unexpected EOF */ if( !Rd_enough_available_bytes( rdec ) ) /* check unexpected EOF */
@ -123,6 +123,9 @@ static int LZd_decode_member( struct LZ_decoder * const d )
if( distance == 0xFFFFFFFFU ) /* marker found */ if( distance == 0xFFFFFFFFU ) /* marker found */
{ {
Rd_normalize( rdec ); Rd_normalize( rdec );
/* const unsigned mpos = rdec->member_position;
if( mpos - old_mpos > rd_min_available_bytes ) return 5;
old_mpos = mpos; */
if( len == min_match_len ) /* End Of Stream marker */ if( len == min_match_len ) /* End Of Stream marker */
{ {
d->verify_trailer_pending = true; d->verify_trailer_pending = true;
@ -130,7 +133,8 @@ static int LZd_decode_member( struct LZ_decoder * const d )
} }
if( len == min_match_len + 1 ) /* Sync Flush marker */ if( len == min_match_len + 1 ) /* Sync Flush marker */
{ {
if( Rd_try_reload( rdec, true ) ) { /*old_mpos += 5;*/ continue; } rdec->reload_pending = true;
if( Rd_try_reload( rdec ) ) continue;
else { if( !rdec->at_stream_end ) return 0; else break; } else { if( !rdec->at_stream_end ) return 0; else break; }
} }
return 4; return 4;

View file

@ -1,23 +1,23 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
enum { rd_min_available_bytes = 8 }; enum { rd_min_available_bytes = 10 };
struct Range_decoder struct Range_decoder
{ {
@ -133,9 +133,8 @@ static inline bool Rd_unread_data( struct Range_decoder * const rdec,
return true; return true;
} }
static bool Rd_try_reload( struct Range_decoder * const rdec, const bool force ) static bool Rd_try_reload( struct Range_decoder * const rdec )
{ {
if( force ) rdec->reload_pending = true;
if( rdec->reload_pending && Rd_available_bytes( rdec ) >= 5 ) if( rdec->reload_pending && Rd_available_bytes( rdec ) >= 5 )
{ {
int i; int i;
@ -168,7 +167,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;
@ -182,15 +181,15 @@ static inline unsigned Rd_decode_bit( struct Range_decoder * const rdec,
bound = ( rdec->range >> bit_model_total_bits ) * *probability; bound = ( rdec->range >> bit_model_total_bits ) * *probability;
if( rdec->code < bound ) if( rdec->code < bound )
{ {
rdec->range = bound;
*probability += (bit_model_total - *probability) >> bit_model_move_bits; *probability += (bit_model_total - *probability) >> bit_model_move_bits;
rdec->range = bound;
return 0; return 0;
} }
else else
{ {
rdec->range -= bound;
rdec->code -= bound;
*probability -= *probability >> bit_model_move_bits; *probability -= *probability >> bit_model_move_bits;
rdec->code -= bound;
rdec->range -= bound;
return 1; return 1;
} }
} }
@ -198,8 +197,7 @@ 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;
@ -208,8 +206,7 @@ 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] );
@ -238,7 +235,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;
@ -248,12 +245,9 @@ 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; symbol += Rd_decode_bit( rdec, &bm[2+symbol] ) << 1;
unsigned bit = Rd_decode_bit( rdec, &bm[model] ); symbol += Rd_decode_bit( rdec, &bm[4+symbol] ) << 2;
model = ( model << 1 ) + bit; symbol |= ( bit << 1 ); symbol += Rd_decode_bit( rdec, &bm[8+symbol] ) << 3;
bit = Rd_decode_bit( rdec, &bm[model] );
model = ( model << 1 ) + bit; symbol |= ( bit << 2 );
symbol |= ( Rd_decode_bit( rdec, &bm[model] ) << 3 );
return symbol; return symbol;
} }
@ -266,7 +260,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 ) return symbol & 0xFF; if( 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; */
} }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,23 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.46.1. .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.47.16.
.TH MINILZIP "1" "January 2019" "minilzip 1.11" "User Commands" .TH MINILZIP "1" "January 2021" "minilzip 1.12" "User Commands"
.SH NAME .SH NAME
minilzip \- reduces the size of files minilzip \- reduces the size of files
.SH SYNOPSIS .SH SYNOPSIS
.B minilzip .B minilzip
[\fI\,options\/\fR] [\fI\,files\/\fR] [\fI\,options\/\fR] [\fI\,files\/\fR]
.SH DESCRIPTION .SH DESCRIPTION
Minilzip is a test program for the lzlib compression library, fully Minilzip is a test program for the compression library lzlib, fully
compatible with lzip 1.4 or newer. compatible with lzip 1.4 or newer.
.PP
Lzip is a lossless data compressor with a user interface similar to the one
of gzip or bzip2. Lzip uses a simplified form of the 'Lempel\-Ziv\-Markov
chain\-Algorithm' (LZMA) stream format, chosen to maximize safety and
interoperability. Lzip can compress about as fast as gzip (lzip \fB\-0\fR) or
compress most files more than bzip2 (lzip \fB\-9\fR). Decompression speed is
intermediate between gzip and bzip2. Lzip is better than gzip and bzip2 from
a data recovery perspective. Lzip has been designed, written, and tested
with great care to replace gzip and bzip2 as the standard general\-purpose
compressed format for unix\-like systems.
.SH OPTIONS .SH OPTIONS
.TP .TP
\fB\-h\fR, \fB\-\-help\fR \fB\-h\fR, \fB\-\-help\fR
@ -41,7 +51,7 @@ keep (don't delete) input files
set match length limit in bytes [36] set match length limit in bytes [36]
.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
@ -69,6 +79,9 @@ alias for \fB\-9\fR
.TP .TP
\fB\-\-loose\-trailing\fR \fB\-\-loose\-trailing\fR
allow trailing data seeming corrupt header allow trailing data seeming corrupt header
.TP
\fB\-\-check\-lib\fR
compare version of lzlib.h with liblz.{a,so}
.PP .PP
If no file names are given, or if a file is '\-', minilzip compresses or If no file names are given, or if a file is '\-', minilzip compresses or
decompresses from standard input to standard output. decompresses from standard input to standard output.
@ -79,20 +92,29 @@ to 2^29 bytes.
.PP .PP
The bidimensional parameter space of LZMA can't be mapped to a linear The bidimensional parameter space of LZMA can't be mapped to a linear
scale optimal for all files. If your files are large, very repetitive, scale optimal for all files. If your files are large, very repetitive,
etc, you may need to use the \fB\-\-dictionary\-size\fR and \fB\-\-match\-length\fR etc, you may need to use the options \fB\-\-dictionary\-size\fR and \fB\-\-match\-length\fR
options directly to achieve optimal performance. directly to achieve optimal performance.
.PP
To extract all the files from archive 'foo.tar.lz', use the commands
\&'tar \fB\-xf\fR foo.tar.lz' or 'minilzip \fB\-cd\fR foo.tar.lz | tar \fB\-xf\fR \-'.
.PP .PP
Exit status: 0 for a normal exit, 1 for environmental problems (file Exit status: 0 for a normal exit, 1 for environmental problems (file
not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or
invalid input file, 3 for an internal consistency error (eg, bug) which invalid input file, 3 for an internal consistency error (eg, bug) which
caused minilzip to panic. caused minilzip to panic.
.PP
The ideas embodied in lzlib are due to (at least) the following people:
Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for the
definition of Markov chains), G.N.N. Martin (for the definition of range
encoding), Igor Pavlov (for putting all the above together in LZMA), and
Julian Seward (for bzip2's CLI).
.SH "REPORTING BUGS" .SH "REPORTING BUGS"
Report bugs to lzip\-bug@nongnu.org Report bugs to lzip\-bug@nongnu.org
.br .br
Lzlib home page: http://www.nongnu.org/lzip/lzlib.html Lzlib home page: http://www.nongnu.org/lzip/lzlib.html
.SH COPYRIGHT .SH COPYRIGHT
Copyright \(co 2019 Antonio Diaz Diaz. Copyright \(co 2021 Antonio Diaz Diaz.
Using lzlib 1.11 Using lzlib 1.12
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,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs ) static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs )
@ -23,7 +23,7 @@ static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs
int32_t * ptr1 = ptr0 + 1; int32_t * ptr1 = ptr0 + 1;
int32_t * newptr; int32_t * newptr;
int len = 0, len0 = 0, len1 = 0; int len = 0, len0 = 0, len1 = 0;
int maxlen = 0; int maxlen = 3; /* only used if pairs != 0 */
int num_pairs = 0; int num_pairs = 0;
const int pos1 = e->eb.mb.pos + 1; const int pos1 = e->eb.mb.pos + 1;
const int min_pos = ( e->eb.mb.pos > e->eb.mb.dictionary_size ) ? const int min_pos = ( e->eb.mb.pos > e->eb.mb.dictionary_size ) ?
@ -49,8 +49,8 @@ static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs
if( pairs ) if( pairs )
{ {
int np2 = e->eb.mb.prev_positions[key2]; const int np2 = e->eb.mb.prev_positions[key2];
int np3 = e->eb.mb.prev_positions[key3]; const int np3 = e->eb.mb.prev_positions[key3];
if( np2 > min_pos && e->eb.mb.buffer[np2-1] == data[0] ) if( np2 > min_pos && e->eb.mb.buffer[np2-1] == data[0] )
{ {
pairs[0].dis = e->eb.mb.pos - np2; pairs[0].dis = e->eb.mb.pos - np2;
@ -60,19 +60,17 @@ static int LZe_get_match_pairs( struct LZ_encoder * const e, struct Pair * pairs
if( np2 != np3 && np3 > min_pos && e->eb.mb.buffer[np3-1] == data[0] ) if( np2 != np3 && np3 > min_pos && e->eb.mb.buffer[np3-1] == data[0] )
{ {
maxlen = 3; maxlen = 3;
np2 = np3; pairs[num_pairs++].dis = e->eb.mb.pos - np3;
pairs[num_pairs].dis = e->eb.mb.pos - np2;
++num_pairs;
} }
if( num_pairs > 0 ) if( num_pairs > 0 )
{ {
const int delta = pos1 - np2; const int delta = pairs[num_pairs-1].dis + 1;
while( maxlen < len_limit && data[maxlen-delta] == data[maxlen] ) while( maxlen < len_limit && data[maxlen-delta] == data[maxlen] )
++maxlen; ++maxlen;
pairs[num_pairs-1].len = maxlen; pairs[num_pairs-1].len = maxlen;
if( maxlen < 3 ) maxlen = 3;
if( maxlen >= len_limit ) pairs = 0; /* done. now just skip */ if( maxlen >= len_limit ) pairs = 0; /* done. now just skip */
} }
if( maxlen < 3 ) maxlen = 3;
} }
e->eb.mb.prev_positions[key2] = pos1; e->eb.mb.prev_positions[key2] = pos1;

View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
struct Len_prices struct Len_prices

View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
static bool Mb_normalize_pos( struct Matchfinder_base * const mb ) static bool Mb_normalize_pos( struct Matchfinder_base * const mb )
@ -58,7 +58,7 @@ static bool Mb_init( struct Matchfinder_base * const mb, const int before_size,
mb->stream_pos = 0; mb->stream_pos = 0;
mb->num_prev_positions23 = num_prev_positions23; mb->num_prev_positions23 = num_prev_positions23;
mb->at_stream_end = false; mb->at_stream_end = false;
mb->flushing = false; mb->sync_flush_pending = false;
mb->buffer_size = max( 65536, buffer_size_limit ); mb->buffer_size = max( 65536, buffer_size_limit );
mb->buffer = (uint8_t *)malloc( mb->buffer_size ); mb->buffer = (uint8_t *)malloc( mb->buffer_size );
@ -69,7 +69,7 @@ static bool Mb_init( struct Matchfinder_base * const mb, const int before_size,
size = 1 << max( 16, real_bits( mb->dictionary_size - 1 ) - 2 ); size = 1 << max( 16, real_bits( mb->dictionary_size - 1 ) - 2 );
if( mb->dictionary_size > 1 << 26 ) /* 64 MiB */ if( mb->dictionary_size > 1 << 26 ) /* 64 MiB */
size >>= 1; size >>= 1;
mb->key4_mask = size - 1; mb->key4_mask = size - 1; /* increases with dictionary size */
size += num_prev_positions23; size += num_prev_positions23;
mb->num_prev_positions = size; mb->num_prev_positions = size;
@ -118,7 +118,7 @@ static void Mb_reset( struct Matchfinder_base * const mb )
mb->pos = 0; mb->pos = 0;
mb->cyclic_pos = 0; mb->cyclic_pos = 0;
mb->at_stream_end = false; mb->at_stream_end = false;
mb->flushing = false; mb->sync_flush_pending = false;
mb->dictionary_size = mb->saved_dictionary_size; mb->dictionary_size = mb->saved_dictionary_size;
Mb_adjust_array( mb ); Mb_adjust_array( mb );
mb->pos_limit = mb->buffer_size - mb->after_size; mb->pos_limit = mb->buffer_size - mb->after_size;
@ -126,7 +126,7 @@ static void Mb_reset( struct Matchfinder_base * const mb )
} }
/* End Of Stream mark => (dis == 0xFFFFFFFFU, len == min_match_len) */ /* End Of Stream marker => (dis == 0xFFFFFFFFU, len == min_match_len) */
static void LZeb_try_full_flush( struct LZ_encoder_base * const eb ) static void LZeb_try_full_flush( struct LZ_encoder_base * const eb )
{ {
int i; int i;
@ -149,33 +149,36 @@ static void LZeb_try_full_flush( struct LZ_encoder_base * const eb )
} }
/* Sync Flush mark => (dis == 0xFFFFFFFFU, len == min_match_len + 1) */ /* Sync Flush marker => (dis == 0xFFFFFFFFU, len == min_match_len + 1) */
static bool LZeb_sync_flush( struct LZ_encoder_base * const eb ) static void LZeb_try_sync_flush( struct LZ_encoder_base * const eb )
{ {
int i;
const int pos_state = Mb_data_position( &eb->mb ) & pos_state_mask; const int pos_state = Mb_data_position( &eb->mb ) & pos_state_mask;
const State state = eb->state; const State state = eb->state;
const unsigned min_size = eb->renc.ff_count + max_marker_size;
if( eb->member_finished || if( eb->member_finished ||
Cb_free_bytes( &eb->renc.cb ) < (2 * max_marker_size) + eb->renc.ff_count ) Cb_free_bytes( &eb->renc.cb ) < min_size + max_marker_size ) return;
return false; eb->mb.sync_flush_pending = false;
for( i = 0; i < 2; ++i ) /* 2 consecutive markers guarantee decoding */ const unsigned long long old_mpos = Re_member_position( &eb->renc );
{ do { /* size of markers must be >= rd_min_available_bytes + 5 */
Re_encode_bit( &eb->renc, &eb->bm_match[state][pos_state], 1 ); Re_encode_bit( &eb->renc, &eb->bm_match[state][pos_state], 1 );
Re_encode_bit( &eb->renc, &eb->bm_rep[state], 0 ); Re_encode_bit( &eb->renc, &eb->bm_rep[state], 0 );
LZeb_encode_pair( eb, 0xFFFFFFFFU, min_match_len + 1, pos_state ); LZeb_encode_pair( eb, 0xFFFFFFFFU, min_match_len + 1, pos_state );
Re_flush( &eb->renc ); Re_flush( &eb->renc );
} }
return true; while( Re_member_position( &eb->renc ) - old_mpos < min_size );
} }
static void LZeb_reset( struct LZ_encoder_base * const eb, static void LZeb_reset( struct LZ_encoder_base * const eb,
const unsigned long long member_size ) const unsigned long long member_size )
{ {
const unsigned long long min_member_size = min_dictionary_size;
const unsigned long long max_member_size = 0x0008000000000000ULL; /* 2 PiB */
int i; int i;
Mb_reset( &eb->mb ); Mb_reset( &eb->mb );
eb->member_size_limit = eb->member_size_limit =
min( member_size, 0x0008000000000000ULL ) - Lt_size - max_marker_size; min( max( min_member_size, member_size ), max_member_size ) -
Lt_size - max_marker_size;
eb->crc = 0xFFFFFFFFU; eb->crc = 0xFFFFFFFFU;
Bm_array_init( eb->bm_literal[0], (1 << literal_context_bits) * 0x300 ); Bm_array_init( eb->bm_literal[0], (1 << literal_context_bits) * 0x300 );
Bm_array_init( eb->bm_match[0], states * pos_states ); Bm_array_init( eb->bm_match[0], states * pos_states );

View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
enum { price_shift_bits = 6, enum { price_shift_bits = 6,
@ -197,7 +197,7 @@ static inline int price_symbol_reversed( const Bit_model bm[], int symbol,
const bool bit = symbol & 1; const bool bit = symbol & 1;
symbol >>= 1; symbol >>= 1;
price += price_bit( bm[model], bit ); price += price_bit( bm[model], bit );
model = ( model << 1 ) | bit; model <<= 1; model |= bit;
} }
return price; return price;
} }
@ -240,7 +240,7 @@ struct Matchfinder_base
int pos_array_size; int pos_array_size;
int saved_dictionary_size; /* dictionary_size restored by Mb_reset */ int saved_dictionary_size; /* dictionary_size restored by Mb_reset */
bool at_stream_end; /* stream_pos shows real end of file */ bool at_stream_end; /* stream_pos shows real end of file */
bool flushing; bool sync_flush_pending;
}; };
static bool Mb_normalize_pos( struct Matchfinder_base * const mb ); static bool Mb_normalize_pos( struct Matchfinder_base * const mb );
@ -265,19 +265,20 @@ Mb_data_position( const struct Matchfinder_base * const mb )
{ return mb->partial_data_pos + mb->pos; } { return mb->partial_data_pos + mb->pos; }
static inline void Mb_finish( struct Matchfinder_base * const mb ) static inline void Mb_finish( struct Matchfinder_base * const mb )
{ mb->at_stream_end = true; mb->flushing = false; } { mb->at_stream_end = true; mb->sync_flush_pending = false; }
static inline bool Mb_data_finished( const struct Matchfinder_base * const mb ) static inline bool Mb_data_finished( const struct Matchfinder_base * const mb )
{ return mb->at_stream_end && !mb->flushing && mb->pos >= mb->stream_pos; } { return mb->at_stream_end && mb->pos >= mb->stream_pos; }
static inline bool Mb_flushing_or_end( const struct Matchfinder_base * const mb ) static inline bool Mb_flushing_or_end( const struct Matchfinder_base * const mb )
{ return mb->at_stream_end || mb->flushing; } { return mb->at_stream_end || mb->sync_flush_pending; }
static inline int Mb_free_bytes( const struct Matchfinder_base * const mb ) static inline int Mb_free_bytes( const struct Matchfinder_base * const mb )
{ if( Mb_flushing_or_end( mb ) ) return 0; { if( Mb_flushing_or_end( mb ) ) return 0;
return mb->buffer_size - mb->stream_pos; } return mb->buffer_size - mb->stream_pos; }
static inline bool Mb_enough_available_bytes( const struct Matchfinder_base * const mb ) static inline bool
Mb_enough_available_bytes( const struct Matchfinder_base * const mb )
{ return ( mb->pos + mb->after_size <= mb->stream_pos || { return ( mb->pos + mb->after_size <= mb->stream_pos ||
( Mb_flushing_or_end( mb ) && mb->pos < mb->stream_pos ) ); } ( Mb_flushing_or_end( mb ) && mb->pos < mb->stream_pos ) ); }
@ -426,28 +427,30 @@ static inline void Re_encode_bit( struct Range_encoder * const renc,
static inline void Re_encode_tree3( struct Range_encoder * const renc, static inline void Re_encode_tree3( struct Range_encoder * const renc,
Bit_model bm[], const int symbol ) Bit_model bm[], const int symbol )
{ {
int model = 1; int model;
bool bit = ( symbol >> 2 ) & 1; bool bit = ( symbol >> 2 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[1], bit );
model = 2 | bit;
bit = ( symbol >> 1 ) & 1; bit = ( symbol >> 1 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit;
Re_encode_bit( renc, &bm[model], symbol & 1 ); Re_encode_bit( renc, &bm[model], symbol & 1 );
} }
static inline void Re_encode_tree6( struct Range_encoder * const renc, static inline void Re_encode_tree6( struct Range_encoder * const renc,
Bit_model bm[], const unsigned symbol ) Bit_model bm[], const unsigned symbol )
{ {
int model = 1; int model;
bool bit = ( symbol >> 5 ) & 1; bool bit = ( symbol >> 5 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[1], bit );
model = 2 | bit;
bit = ( symbol >> 4 ) & 1; bit = ( symbol >> 4 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit;
bit = ( symbol >> 3 ) & 1; bit = ( symbol >> 3 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit;
bit = ( symbol >> 2 ) & 1; bit = ( symbol >> 2 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit;
bit = ( symbol >> 1 ) & 1; bit = ( symbol >> 1 ) & 1;
Re_encode_bit( renc, &bm[model], bit ); model = ( model << 1 ) | bit; Re_encode_bit( renc, &bm[model], bit ); model <<= 1; model |= bit;
Re_encode_bit( renc, &bm[model], symbol & 1 ); Re_encode_bit( renc, &bm[model], symbol & 1 );
} }
@ -460,7 +463,7 @@ static inline void Re_encode_tree8( struct Range_encoder * const renc,
{ {
const bool bit = ( symbol >> i ) & 1; const bool bit = ( symbol >> i ) & 1;
Re_encode_bit( renc, &bm[model], bit ); Re_encode_bit( renc, &bm[model], bit );
model = ( model << 1 ) | bit; model <<= 1; model |= bit;
} }
} }
@ -474,7 +477,7 @@ static inline void Re_encode_tree_reversed( struct Range_encoder * const renc,
const bool bit = symbol & 1; const bool bit = symbol & 1;
symbol >>= 1; symbol >>= 1;
Re_encode_bit( renc, &bm[model], bit ); Re_encode_bit( renc, &bm[model], bit );
model = ( model << 1 ) | bit; model <<= 1; model |= bit;
} }
} }
@ -561,7 +564,7 @@ static inline bool LZeb_init( struct LZ_encoder_base * const eb,
} }
static inline bool LZeb_member_finished( const struct LZ_encoder_base * const eb ) static inline bool LZeb_member_finished( const struct LZ_encoder_base * const eb )
{ return ( eb->member_finished && !Cb_used_bytes( &eb->renc.cb ) ); } { return ( eb->member_finished && Cb_empty( &eb->renc.cb ) ); }
static inline void LZeb_free( struct LZ_encoder_base * const eb ) static inline void LZeb_free( struct LZ_encoder_base * const eb )
{ Re_free( &eb->renc ); Mb_free( &eb->mb ); } { Re_free( &eb->renc ); Mb_free( &eb->mb ); }

View file

@ -1,23 +1,23 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
int FLZe_longest_match_len( struct FLZ_encoder * const fe, int * const distance ) static int FLZe_longest_match_len( struct FLZ_encoder * const fe, int * const distance )
{ {
enum { len_limit = 16 }; enum { len_limit = 16 };
const uint8_t * const data = Mb_ptr_to_current_pos( &fe->eb.mb ); const uint8_t * const data = Mb_ptr_to_current_pos( &fe->eb.mb );
@ -59,7 +59,7 @@ int FLZe_longest_match_len( struct FLZ_encoder * const fe, int * const distance
} }
bool FLZe_encode_member( struct FLZ_encoder * const fe ) static bool FLZe_encode_member( struct FLZ_encoder * const fe )
{ {
int rep = 0, i; int rep = 0, i;
State * const state = &fe->eb.state; State * const state = &fe->eb.state;
@ -86,7 +86,7 @@ bool FLZe_encode_member( struct FLZ_encoder * const fe )
while( !Mb_data_finished( &fe->eb.mb ) && while( !Mb_data_finished( &fe->eb.mb ) &&
Re_member_position( &fe->eb.renc ) < fe->eb.member_size_limit ) Re_member_position( &fe->eb.renc ) < fe->eb.member_size_limit )
{ {
int match_distance; int match_distance = 0; /* avoid warning from gcc 6.1.0 */
int main_len, pos_state; int main_len, pos_state;
int len = 0; int len = 0;
if( !Mb_enough_available_bytes( &fe->eb.mb ) || if( !Mb_enough_available_bytes( &fe->eb.mb ) ||

View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
struct FLZ_encoder struct FLZ_encoder
@ -31,21 +31,19 @@ static inline void FLZe_reset_key4( struct FLZ_encoder * const fe )
fe->key4 = ( fe->key4 << 4 ) ^ fe->eb.mb.buffer[i]; fe->key4 = ( fe->key4 << 4 ) ^ fe->eb.mb.buffer[i];
} }
int FLZe_longest_match_len( struct FLZ_encoder * const fe, int * const distance );
static inline bool FLZe_update_and_move( struct FLZ_encoder * const fe, int n ) static inline bool FLZe_update_and_move( struct FLZ_encoder * const fe, int n )
{ {
struct Matchfinder_base * const mb = &fe->eb.mb;
while( --n >= 0 ) while( --n >= 0 )
{ {
if( Mb_available_bytes( &fe->eb.mb ) >= 4 ) if( Mb_available_bytes( mb ) >= 4 )
{ {
fe->key4 = ( ( fe->key4 << 4 ) ^ fe->eb.mb.buffer[fe->eb.mb.pos+3] ) & fe->key4 = ( ( fe->key4 << 4 ) ^ mb->buffer[mb->pos+3] ) & mb->key4_mask;
fe->eb.mb.key4_mask; mb->pos_array[mb->cyclic_pos] = mb->prev_positions[fe->key4];
fe->eb.mb.pos_array[fe->eb.mb.cyclic_pos] = fe->eb.mb.prev_positions[fe->key4]; mb->prev_positions[fe->key4] = mb->pos + 1;
fe->eb.mb.prev_positions[fe->key4] = fe->eb.mb.pos + 1;
} }
else fe->eb.mb.pos_array[fe->eb.mb.cyclic_pos] = 0; else mb->pos_array[mb->cyclic_pos] = 0;
if( !Mb_move_pos( &fe->eb.mb ) ) return false; if( !Mb_move_pos( mb ) ) return false;
} }
return true; return true;
} }

300
ffexample.c Normal file
View file

@ -0,0 +1,300 @@
/* File to file example - Test program for the library lzlib
Copyright (C) 2010-2021 Antonio Diaz Diaz.
This program is free software: you have unlimited permission
to copy, distribute, and modify it.
Try 'ffexample -h' for usage information.
This program is an example of how file-to-file
compression/decompression can be implemented using lzlib.
*/
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__)
#include <fcntl.h>
#include <io.h>
#endif
#include "lzlib.h"
#ifndef min
#define min(x,y) ((x) <= (y) ? (x) : (y))
#endif
static void show_help( void )
{
printf( "ffexample is an example program showing how file-to-file (de)compression can\n"
"be implemented using lzlib. The content of infile is compressed,\n"
"decompressed, or both, and then written to outfile.\n"
"\nUsage: ffexample operation [infile [outfile]]\n" );
printf( "\nOperation:\n"
" -h display this help and exit\n"
" -c compress infile to outfile\n"
" -d decompress infile to outfile\n"
" -b both (compress then decompress) infile to outfile\n"
" -m compress (multimember) infile to outfile\n"
" -l compress (1 member per line) infile to outfile\n"
" -r decompress with resync if data error or leading garbage\n"
"\nIf infile or outfile are omitted, or are specified as '-', standard input or\n"
"standard output are used in their place respectively.\n"
"\nReport bugs to lzip-bug@nongnu.org\n"
"Lzlib home page: http://www.nongnu.org/lzip/lzlib.html\n" );
}
int ffcompress( struct LZ_Encoder * const encoder,
FILE * const infile, FILE * const outfile )
{
enum { buffer_size = 16384 };
uint8_t buffer[buffer_size];
while( true )
{
int len, ret;
int size = min( buffer_size, LZ_compress_write_size( encoder ) );
if( size > 0 )
{
len = fread( buffer, 1, size, infile );
ret = LZ_compress_write( encoder, buffer, len );
if( ret < 0 || ferror( infile ) ) break;
if( feof( infile ) ) LZ_compress_finish( encoder );
}
ret = LZ_compress_read( encoder, buffer, buffer_size );
if( ret < 0 ) break;
len = fwrite( buffer, 1, ret, outfile );
if( len < ret ) break;
if( LZ_compress_finished( encoder ) == 1 ) return 0;
}
return 1;
}
int ffdecompress( struct LZ_Decoder * const decoder,
FILE * const infile, FILE * const outfile )
{
enum { buffer_size = 16384 };
uint8_t buffer[buffer_size];
while( true )
{
int len, ret;
int size = min( buffer_size, LZ_decompress_write_size( decoder ) );
if( size > 0 )
{
len = fread( buffer, 1, size, infile );
ret = LZ_decompress_write( decoder, buffer, len );
if( ret < 0 || ferror( infile ) ) break;
if( feof( infile ) ) LZ_decompress_finish( decoder );
}
ret = LZ_decompress_read( decoder, buffer, buffer_size );
if( ret < 0 ) break;
len = fwrite( buffer, 1, ret, outfile );
if( len < ret ) break;
if( LZ_decompress_finished( decoder ) == 1 ) return 0;
}
return 1;
}
int ffboth( struct LZ_Encoder * const encoder,
struct LZ_Decoder * const decoder,
FILE * const infile, FILE * const outfile )
{
enum { buffer_size = 16384 };
uint8_t buffer[buffer_size];
while( true )
{
int len, ret;
int size = min( buffer_size, LZ_compress_write_size( encoder ) );
if( size > 0 )
{
len = fread( buffer, 1, size, infile );
ret = LZ_compress_write( encoder, buffer, len );
if( ret < 0 || ferror( infile ) ) break;
if( feof( infile ) ) LZ_compress_finish( encoder );
}
size = min( buffer_size, LZ_decompress_write_size( decoder ) );
if( size > 0 )
{
ret = LZ_compress_read( encoder, buffer, size );
if( ret < 0 ) break;
ret = LZ_decompress_write( decoder, buffer, ret );
if( ret < 0 ) break;
if( LZ_compress_finished( encoder ) == 1 )
LZ_decompress_finish( decoder );
}
ret = LZ_decompress_read( decoder, buffer, buffer_size );
if( ret < 0 ) break;
len = fwrite( buffer, 1, ret, outfile );
if( len < ret ) break;
if( LZ_decompress_finished( decoder ) == 1 ) return 0;
}
return 1;
}
int ffmmcompress( FILE * const infile, FILE * const outfile )
{
enum { buffer_size = 16384, member_size = 4096 };
uint8_t buffer[buffer_size];
bool done = false;
struct LZ_Encoder * const encoder =
LZ_compress_open( 65535, 16, member_size );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
{ fputs( "ffexample: Not enough memory.\n", stderr );
LZ_compress_close( encoder ); return 1; }
while( true )
{
int len, ret;
int size = min( buffer_size, LZ_compress_write_size( encoder ) );
if( size > 0 )
{
len = fread( buffer, 1, size, infile );
ret = LZ_compress_write( encoder, buffer, len );
if( ret < 0 || ferror( infile ) ) break;
if( feof( infile ) ) LZ_compress_finish( encoder );
}
ret = LZ_compress_read( encoder, buffer, buffer_size );
if( ret < 0 ) break;
len = fwrite( buffer, 1, ret, outfile );
if( len < ret ) break;
if( LZ_compress_member_finished( encoder ) == 1 )
{
if( LZ_compress_finished( encoder ) == 1 ) { done = true; break; }
if( LZ_compress_restart_member( encoder, member_size ) < 0 ) break;
}
}
if( LZ_compress_close( encoder ) < 0 ) done = false;
return done;
}
/* Compresses 'infile' to 'outfile' as a multimember stream with one member
for each line of text terminated by a newline character or by EOF.
Returns 0 if success, 1 if error.
*/
int fflfcompress( struct LZ_Encoder * const encoder,
FILE * const infile, FILE * const outfile )
{
enum { buffer_size = 16384 };
uint8_t buffer[buffer_size];
while( true )
{
int len, ret;
int size = min( buffer_size, LZ_compress_write_size( encoder ) );
if( size > 0 )
{
for( len = 0; len < size; )
{
int ch = getc( infile );
if( ch == EOF || ( buffer[len++] = ch ) == '\n' ) break;
}
/* avoid writing an empty member to outfile */
if( len == 0 && LZ_compress_data_position( encoder ) == 0 ) return 0;
ret = LZ_compress_write( encoder, buffer, len );
if( ret < 0 || ferror( infile ) ) break;
if( feof( infile ) || buffer[len-1] == '\n' )
LZ_compress_finish( encoder );
}
ret = LZ_compress_read( encoder, buffer, buffer_size );
if( ret < 0 ) break;
len = fwrite( buffer, 1, ret, outfile );
if( len < ret ) break;
if( LZ_compress_member_finished( encoder ) == 1 )
{
if( feof( infile ) && LZ_compress_finished( encoder ) == 1 ) return 0;
if( LZ_compress_restart_member( encoder, INT64_MAX ) < 0 ) break;
}
}
return 1;
}
/* Decompresses 'infile' to 'outfile' with automatic resynchronization to
next member in case of data error, including the automatic removal of
leading garbage.
*/
int ffrsdecompress( struct LZ_Decoder * const decoder,
FILE * const infile, FILE * const outfile )
{
enum { buffer_size = 16384 };
uint8_t buffer[buffer_size];
while( true )
{
int len, ret;
int size = min( buffer_size, LZ_decompress_write_size( decoder ) );
if( size > 0 )
{
len = fread( buffer, 1, size, infile );
ret = LZ_decompress_write( decoder, buffer, len );
if( ret < 0 || ferror( infile ) ) break;
if( feof( infile ) ) LZ_decompress_finish( decoder );
}
ret = LZ_decompress_read( decoder, buffer, buffer_size );
if( ret < 0 )
{
if( LZ_decompress_errno( decoder ) == LZ_header_error ||
LZ_decompress_errno( decoder ) == LZ_data_error )
{ LZ_decompress_sync_to_member( decoder ); continue; }
else break;
}
len = fwrite( buffer, 1, ret, outfile );
if( len < ret ) break;
if( LZ_decompress_finished( decoder ) == 1 ) return 0;
}
return 1;
}
int main( const int argc, const char * const argv[] )
{
#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__)
setmode( STDIN_FILENO, O_BINARY );
setmode( STDOUT_FILENO, O_BINARY );
#endif
struct LZ_Encoder * const encoder = LZ_compress_open( 65535, 16, INT64_MAX );
struct LZ_Decoder * const decoder = LZ_decompress_open();
FILE * const infile = ( argc >= 3 && strcmp( argv[2], "-" ) != 0 ) ?
fopen( argv[2], "rb" ) : stdin;
FILE * const outfile = ( argc >= 4 && strcmp( argv[3], "-" ) != 0 ) ?
fopen( argv[3], "wb" ) : stdout;
int retval;
if( argc < 2 || argc > 4 || strlen( argv[1] ) != 2 || argv[1][0] != '-' )
{ show_help(); return 1; }
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok ||
!decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ fputs( "ffexample: Not enough memory.\n", stderr );
LZ_compress_close( encoder ); LZ_decompress_close( decoder ); return 1; }
if( !infile )
{ fprintf( stderr, "ffexample: Can't open input file '%s': %s\n",
argv[2], strerror( errno ) ); return 1; }
if( !outfile )
{ fprintf( stderr, "ffexample: Can't open output file '%s': %s\n",
argv[3], strerror( errno ) ); return 1; }
switch( argv[1][1] )
{
case 'c': retval = ffcompress( encoder, infile, outfile ); break;
case 'd': retval = ffdecompress( decoder, infile, outfile ); break;
case 'b': retval = ffboth( encoder, decoder, infile, outfile ); break;
case 'm': retval = ffmmcompress( infile, outfile ); break;
case 'l': retval = fflfcompress( encoder, infile, outfile ); break;
case 'r': retval = ffrsdecompress( decoder, infile, outfile ); break;
default: show_help(); return ( argv[1][1] != 'h' );
}
if( LZ_decompress_close( decoder ) < 0 || LZ_compress_close( encoder ) < 0 ||
fclose( outfile ) != 0 || fclose( infile ) != 0 ) retval = 1;
return retval;
}

394
lzcheck.c
View file

@ -1,15 +1,14 @@
/* Lzcheck - Test program for the lzlib library /* Lzcheck - Test program for the library lzlib
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 Antonio Diaz Diaz.
This program is free software: you have unlimited permission This program is free software: you have unlimited permission
to copy, distribute and modify it. to copy, distribute, and modify it.
Usage is: Usage: lzcheck [-m|-s] filename.txt...
lzcheck filename.txt...
This program reads each specified text file and then compresses it, This program reads each text file specified and then compresses it,
line by line, to test the flushing mechanism and the member line by line, to test the flushing mechanism and the member
restart/reset/sync functions. restart/reset/sync functions.
*/ */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
@ -21,21 +20,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h>
#include "lzlib.h" #include "lzlib.h"
#ifndef min
#define min(x,y) ((x) <= (y) ? (x) : (y))
#endif
const unsigned long long member_size = INT64_MAX;
enum { buffer_size = 32768 }; enum { buffer_size = 32768 };
uint8_t in_buffer[buffer_size]; uint8_t in_buffer[buffer_size];
uint8_t mid_buffer[buffer_size]; uint8_t mid_buffer[buffer_size];
uint8_t out_buffer[buffer_size]; uint8_t out_buffer[buffer_size];
void show_line( const uint8_t * const buffer, const int size ) static void show_line( const uint8_t * const buffer, const int size )
{ {
int i; int i;
for( i = 0; i < size; ++i ) for( i = 0; i < size; ++i )
@ -44,177 +41,281 @@ void show_line( const uint8_t * const buffer, const int size )
} }
int lzcheck( FILE * const file, const int dictionary_size ) static struct LZ_Encoder * xopen_encoder( const int dictionary_size )
{ {
const int match_len_limit = 16; const int match_len_limit = 16;
const unsigned long long member_size = 0x7FFFFFFFFFFFFFFFULL; /* INT64_MAX */ struct LZ_Encoder * const encoder =
struct LZ_Encoder * encoder; LZ_compress_open( dictionary_size, match_len_limit, member_size );
struct LZ_Decoder * decoder;
int retval = 0;
encoder = LZ_compress_open( dictionary_size, match_len_limit, member_size );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok ) if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
{ {
const bool mem_error = ( LZ_compress_errno( encoder ) == LZ_mem_error ); const bool bad_arg =
encoder && ( LZ_compress_errno( encoder ) == LZ_bad_argument );
LZ_compress_close( encoder ); LZ_compress_close( encoder );
if( mem_error ) if( bad_arg )
{ {
fputs( "lzcheck: Not enough memory.\n", stderr ); fputs( "lzcheck: internal error: Invalid argument to encoder.\n", stderr );
return 1; exit( 3 );
} }
fputs( "lzcheck: internal error: Invalid argument to encoder.\n", stderr ); fputs( "lzcheck: Not enough memory.\n", stderr );
return 3; exit( 1 );
} }
return encoder;
}
decoder = LZ_decompress_open(); static struct LZ_Decoder * xopen_decoder( void )
{
struct LZ_Decoder * const decoder = LZ_decompress_open();
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ {
LZ_decompress_close( decoder ); LZ_decompress_close( decoder );
fputs( "lzcheck: Not enough memory.\n", stderr ); fputs( "lzcheck: Not enough memory.\n", stderr );
return 1; exit( 1 );
}
return decoder;
}
static void xclose_encoder( struct LZ_Encoder * const encoder,
const bool finish )
{
if( finish )
{
unsigned long long size = 0;
LZ_compress_finish( encoder );
while( true )
{
const int rd = LZ_compress_read( encoder, mid_buffer, buffer_size );
if( rd < 0 )
{
fprintf( stderr, "lzcheck: xclose: LZ_compress_read error: %s\n",
LZ_strerror( LZ_compress_errno( encoder ) ) );
exit( 3 );
}
size += rd;
if( LZ_compress_finished( encoder ) == 1 ) break;
}
if( size > 0 )
{
fprintf( stderr, "lzcheck: %lld bytes remain in encoder.\n", size );
exit( 3 );
}
}
if( LZ_compress_close( encoder ) < 0 ) exit( 1 );
}
static void xclose_decoder( struct LZ_Decoder * const decoder,
const bool finish )
{
if( finish )
{
unsigned long long size = 0;
LZ_decompress_finish( decoder );
while( true )
{
const int rd = LZ_decompress_read( decoder, out_buffer, buffer_size );
if( rd < 0 )
{
fprintf( stderr, "lzcheck: xclose: LZ_decompress_read error: %s\n",
LZ_strerror( LZ_decompress_errno( decoder ) ) );
exit( 3 );
}
size += rd;
if( LZ_decompress_finished( decoder ) == 1 ) break;
}
if( size > 0 )
{
fprintf( stderr, "lzcheck: %lld bytes remain in decoder.\n", size );
exit( 3 );
}
}
if( LZ_decompress_close( decoder ) < 0 ) exit( 1 );
}
/* Returns the next (usually newline-terminated) chunk of data from file.
The size returned in *sizep is always <= buffer_size.
If sizep is a null pointer, rewinds the file, resets state, and returns.
If file is at EOF, returns an empty line. */
static const uint8_t * next_line( FILE * const file, int * const sizep )
{
static int l = 0;
static int read_size = 0;
int r;
if( !sizep ) { rewind( file ); l = read_size = 0; return in_buffer; }
if( l >= read_size )
{
l = 0; read_size = fread( in_buffer, 1, buffer_size, file );
if( l >= read_size ) { *sizep = 0; return in_buffer; } /* end of file */
} }
while( retval <= 1 ) for( r = l + 1; r < read_size && in_buffer[r-1] != '\n'; ++r );
*sizep = r - l; l = r;
return in_buffer + l - *sizep;
}
static int check_sync_flush( FILE * const file, const int dictionary_size )
{
struct LZ_Encoder * const encoder = xopen_encoder( dictionary_size );
struct LZ_Decoder * const decoder = xopen_decoder();
int retval = 0;
while( retval <= 1 ) /* test LZ_compress_sync_flush */
{ {
int l, r; int in_size, mid_size, out_size;
const int read_size = fread( in_buffer, 1, buffer_size, file ); int line_size;
if( read_size <= 0 ) break; /* end of file */ const uint8_t * const line_buf = next_line( file, &line_size );
if( line_size <= 0 ) break; /* end of file */
for( l = 0, r = 1; r <= read_size; l = r, ++r ) in_size = LZ_compress_write( encoder, line_buf, line_size );
{ if( in_size < line_size )
int in_size, mid_size, out_size; fprintf( stderr, "lzcheck: sync: LZ_compress_write only accepted %d of %d bytes\n",
while( r < read_size && in_buffer[r-1] != '\n' ) ++r; in_size, line_size );
in_size = LZ_compress_write( encoder, in_buffer + l, r - l ); LZ_compress_sync_flush( encoder );
if( in_size < r - l ) r = l + in_size; if( line_buf[0] & 1 ) /* read all data at once or byte by byte */
LZ_compress_sync_flush( encoder );
mid_size = LZ_compress_read( encoder, mid_buffer, buffer_size ); mid_size = LZ_compress_read( encoder, mid_buffer, buffer_size );
if( mid_size < 0 ) else for( mid_size = 0; mid_size < buffer_size; )
{ {
fprintf( stderr, "lzcheck: LZ_compress_read error: %s\n", const int rd = LZ_compress_read( encoder, mid_buffer + mid_size, 1 );
LZ_strerror( LZ_compress_errno( encoder ) ) ); if( rd > 0 ) mid_size += rd;
retval = 3; break; else { if( rd < 0 ) { mid_size = -1; } break; }
} }
LZ_decompress_write( decoder, mid_buffer, mid_size ); if( mid_size < 0 )
out_size = LZ_decompress_read( decoder, out_buffer, buffer_size ); {
if( out_size < 0 ) fprintf( stderr, "lzcheck: LZ_compress_read error: %s\n",
{ LZ_strerror( LZ_compress_errno( encoder ) ) );
fprintf( stderr, "lzcheck: LZ_decompress_read error: %s\n", retval = 3; break;
LZ_strerror( LZ_decompress_errno( decoder ) ) ); }
retval = 3; break; LZ_decompress_write( decoder, mid_buffer, mid_size );
} out_size = LZ_decompress_read( decoder, out_buffer, buffer_size );
if( out_size < 0 )
{
fprintf( stderr, "lzcheck: LZ_decompress_read error: %s\n",
LZ_strerror( LZ_decompress_errno( decoder ) ) );
retval = 3; break;
}
if( out_size != in_size || memcmp( in_buffer + l, out_buffer, out_size ) ) if( out_size != in_size || memcmp( line_buf, out_buffer, out_size ) )
{ {
fprintf( stderr, "lzcheck: Sync error at pos %d in_size = %d, " fprintf( stderr, "lzcheck: LZ_compress_sync_flush error: "
"out_size = %d\n", "in_size = %d, out_size = %d\n", in_size, out_size );
l, in_size, out_size ); show_line( line_buf, in_size );
show_line( in_buffer + l, in_size ); show_line( out_buffer, out_size );
show_line( out_buffer, out_size ); retval = 1;
retval = 1;
}
} }
} }
if( retval <= 1 ) if( retval <= 1 )
{ {
rewind( file ); int rd = 0;
if( LZ_compress_finish( encoder ) < 0 || if( LZ_compress_finish( encoder ) < 0 ||
LZ_compress_finish( encoder ) < 0 || ( rd = LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 )
LZ_decompress_write( decoder, mid_buffer,
LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 ||
LZ_decompress_read( decoder, out_buffer, buffer_size ) != 0 ||
LZ_compress_finish( encoder ) < 0 ||
LZ_compress_restart_member( encoder, member_size ) < 0 )
{ {
fprintf( stderr, "lzcheck: Can't finish member: %s\n", fprintf( stderr, "lzcheck: Can't drain encoder: %s\n",
LZ_strerror( LZ_decompress_errno( decoder ) ) ); LZ_strerror( LZ_compress_errno( encoder ) ) );
retval = 3; retval = 3;
} }
LZ_decompress_write( decoder, mid_buffer, rd );
} }
while( retval <= 1 ) xclose_decoder( decoder, retval == 0 );
{ xclose_encoder( encoder, retval == 0 );
int l, r, size; return retval;
const int read_size = fread( in_buffer, 1, buffer_size / 2, file ); }
if( read_size <= 0 ) break; /* end of file */
for( l = 0, r = 1; r <= read_size; l = r, ++r )
/* Test member by member decompression without calling LZ_decompress_finish,
inserting leading garbage before some members, and resetting the
decompressor sometimes. Test that the increase in total_in_size when
syncing to member is equal to the size of the leading garbage skipped.
*/
static int check_members( FILE * const file, const int dictionary_size )
{
struct LZ_Encoder * const encoder = xopen_encoder( dictionary_size );
struct LZ_Decoder * const decoder = xopen_decoder();
int retval = 0;
while( retval <= 1 ) /* test LZ_compress_restart_member */
{
unsigned long long garbage_begin = 0; /* avoid warning from gcc 3.3.6 */
int leading_garbage, in_size, mid_size, out_size;
int line_size;
const uint8_t * const line_buf = next_line( file, &line_size );
if( line_size <= 0 && /* end of file, write at least 1 member */
LZ_decompress_total_in_size( decoder ) != 0 ) break;
if( LZ_compress_finished( encoder ) == 1 )
{ {
int leading_garbage, in_size, mid_size, out_size; if( LZ_compress_restart_member( encoder, member_size ) < 0 )
while( r < read_size && in_buffer[r-1] != '\n' ) ++r;
leading_garbage = (l == 0) ? min( r, read_size ) / 2 : 0;
in_size = LZ_compress_write( encoder, in_buffer + l, r - l );
if( in_size < r - l ) r = l + in_size;
LZ_compress_sync_flush( encoder );
if( leading_garbage )
memset( mid_buffer, in_buffer[0], leading_garbage );
mid_size = LZ_compress_read( encoder, mid_buffer + leading_garbage,
buffer_size - leading_garbage );
if( mid_size < 0 )
{ {
fprintf( stderr, "lzcheck: LZ_compress_read error: %s\n", fprintf( stderr, "lzcheck: Can't restart member: %s\n",
LZ_strerror( LZ_compress_errno( encoder ) ) ); LZ_strerror( LZ_compress_errno( encoder ) ) );
retval = 3; break; retval = 3; break;
} }
LZ_decompress_write( decoder, mid_buffer, mid_size + leading_garbage ); if( line_size >= 2 && line_buf[1] == 'h' )
out_size = LZ_decompress_read( decoder, out_buffer, buffer_size ); LZ_decompress_reset( decoder );
if( out_size < 0 ) }
in_size = LZ_compress_write( encoder, line_buf, line_size );
if( in_size < line_size )
fprintf( stderr, "lzcheck: member: LZ_compress_write only accepted %d of %d bytes\n",
in_size, line_size );
LZ_compress_finish( encoder );
if( line_size * 3 < buffer_size && line_buf[0] == 't' )
{ leading_garbage = line_size;
memset( mid_buffer, in_buffer[0], leading_garbage );
garbage_begin = LZ_decompress_total_in_size( decoder ); }
else leading_garbage = 0;
mid_size = LZ_compress_read( encoder, mid_buffer + leading_garbage,
buffer_size - leading_garbage );
if( mid_size < 0 )
{
fprintf( stderr, "lzcheck: member: LZ_compress_read error: %s\n",
LZ_strerror( LZ_compress_errno( encoder ) ) );
retval = 3; break;
}
LZ_decompress_write( decoder, mid_buffer, leading_garbage + mid_size );
out_size = LZ_decompress_read( decoder, out_buffer, buffer_size );
if( out_size < 0 )
{
if( leading_garbage &&
( LZ_decompress_errno( decoder ) == LZ_header_error ||
LZ_decompress_errno( decoder ) == LZ_data_error ) )
{ {
if( LZ_decompress_errno( decoder ) == LZ_header_error || LZ_decompress_sync_to_member( decoder ); /* skip leading garbage */
LZ_decompress_errno( decoder ) == LZ_data_error ) const unsigned long long garbage_end =
LZ_decompress_total_in_size( decoder );
if( garbage_end - garbage_begin != (unsigned)leading_garbage )
{ {
LZ_decompress_sync_to_member( decoder ); /* remove leading garbage */ fprintf( stderr, "lzcheck: member: LZ_decompress_sync_to_member error:\n"
out_size = LZ_decompress_read( decoder, out_buffer, buffer_size ); " garbage_begin = %llu garbage_end = %llu "
} "difference = %llu expected = %d\n", garbage_begin,
if( out_size < 0 ) garbage_end, garbage_end - garbage_begin, leading_garbage );
{
fprintf( stderr, "lzcheck: LZ_decompress_read error: %s\n",
LZ_strerror( LZ_decompress_errno( decoder ) ) );
retval = 3; break; retval = 3; break;
} }
out_size = LZ_decompress_read( decoder, out_buffer, buffer_size );
} }
if( out_size < 0 )
if( out_size != in_size || memcmp( in_buffer + l, out_buffer, out_size ) )
{ {
fprintf( stderr, "lzcheck: Sync error at pos %d in_size = %d, " fprintf( stderr, "lzcheck: member: LZ_decompress_read error: %s\n",
"out_size = %d, leading garbage = %d\n", LZ_strerror( LZ_decompress_errno( decoder ) ) );
l, in_size, out_size, leading_garbage ); retval = 3; break;
show_line( in_buffer + l, in_size );
show_line( out_buffer, out_size );
retval = 1;
} }
} }
if( retval >= 3 ) break;
if( LZ_compress_finish( encoder ) < 0 || if( out_size != in_size || memcmp( line_buf, out_buffer, out_size ) )
LZ_decompress_write( decoder, mid_buffer,
LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 ||
LZ_decompress_read( decoder, out_buffer, buffer_size ) != 0 ||
LZ_decompress_reset( decoder ) < 0 ||
LZ_compress_restart_member( encoder, member_size ) < 0 )
{ {
fprintf( stderr, "lzcheck: Can't restart member: %s\n", fprintf( stderr, "lzcheck: LZ_compress_restart_member error: "
LZ_strerror( LZ_decompress_errno( decoder ) ) ); "in_size = %d, out_size = %d\n", in_size, out_size );
retval = 3; break; show_line( line_buf, in_size );
} show_line( out_buffer, out_size );
retval = 1;
size = min( 100, read_size );
if( LZ_compress_write( encoder, in_buffer, size ) != size ||
LZ_compress_finish( encoder ) < 0 ||
LZ_decompress_write( decoder, mid_buffer,
LZ_compress_read( encoder, mid_buffer, buffer_size ) ) < 0 ||
LZ_decompress_read( decoder, out_buffer, 0 ) != 0 ||
LZ_decompress_sync_to_member( decoder ) < 0 ||
LZ_compress_restart_member( encoder, member_size ) < 0 )
{
fprintf( stderr, "lzcheck: Can't seek to next member: %s\n",
LZ_strerror( LZ_decompress_errno( decoder ) ) );
retval = 3; break;
} }
} }
LZ_decompress_close( decoder ); xclose_decoder( decoder, retval == 0 );
LZ_compress_close( encoder ); xclose_encoder( encoder, retval == 0 );
return retval; return retval;
} }
@ -223,7 +324,11 @@ int main( const int argc, const char * const argv[] )
{ {
int retval = 0, i; int retval = 0, i;
int open_failures = 0; int open_failures = 0;
const bool verbose = ( argc > 2 ); const char opt = ( argc > 2 &&
( strcmp( argv[1], "-m" ) == 0 || strcmp( argv[1], "-s" ) == 0 ) ) ?
argv[1][1] : 0;
const int first = opt ? 2 : 1;
const bool verbose = ( opt != 0 || argc > first + 1 );
if( argc < 2 ) if( argc < 2 )
{ {
@ -231,8 +336,10 @@ int main( const int argc, const char * const argv[] )
return 1; return 1;
} }
for( i = 1; i < argc && retval == 0; ++ i ) for( i = first; i < argc && retval == 0; ++i )
{ {
struct stat st;
if( stat( argv[i], &st ) != 0 || !S_ISREG( st.st_mode ) ) continue;
FILE * file = fopen( argv[i], "rb" ); FILE * file = fopen( argv[i], "rb" );
if( !file ) if( !file )
{ {
@ -241,9 +348,14 @@ int main( const int argc, const char * const argv[] )
} }
if( verbose ) fprintf( stderr, " Testing file '%s'\n", argv[i] ); if( verbose ) fprintf( stderr, " Testing file '%s'\n", argv[i] );
retval = lzcheck( file, 65535 ); /* 65535,16 chooses fast encoder */ /* 65535,16 chooses fast encoder */
if( retval == 0 ) if( opt != 'm' ) retval = check_sync_flush( file, 65535 );
{ rewind( file ); retval = lzcheck( file, 1 << 20 ); } if( retval == 0 && opt != 'm' )
{ next_line( file, 0 ); retval = check_sync_flush( file, 1 << 20 ); }
if( retval == 0 && opt != 's' )
{ next_line( file, 0 ); retval = check_members( file, 65535 ); }
if( retval == 0 && opt != 's' )
{ next_line( file, 0 ); retval = check_members( file, 1 << 20 ); }
fclose( file ); fclose( file );
} }
if( open_failures > 0 && verbose ) if( open_failures > 0 && verbose )

35
lzip.h
View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
#ifndef max #ifndef max
@ -83,7 +83,7 @@ static inline int get_len_state( const int len )
{ return min( len - min_match_len, len_states - 1 ); } { return min( len - min_match_len, len_states - 1 ); }
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 ); }
enum { bit_model_move_bits = 5, enum { bit_model_move_bits = 5,
@ -197,7 +197,7 @@ static const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; /* "LZIP" */
typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */ typedef uint8_t Lzip_header[6]; /* 0-3 magic bytes */
/* 4 version */ /* 4 version */
/* 5 coded_dict_size */ /* 5 coded dictionary size */
enum { Lh_size = 6 }; enum { Lh_size = 6 };
static inline void Lh_set_magic( Lzip_header data ) static inline void Lh_set_magic( Lzip_header data )
@ -255,9 +255,8 @@ static inline bool Lh_set_dictionary_size( Lzip_header data, const unsigned sz )
static inline bool Lh_verify( const Lzip_header data ) static inline bool Lh_verify( const Lzip_header data )
{ {
if( Lh_verify_magic( data ) && Lh_verify_version( data ) ) return Lh_verify_magic( data ) && Lh_verify_version( data ) &&
return isvalid_ds( Lh_get_dictionary_size( data ) ); isvalid_ds( Lh_get_dictionary_size( data ) );
return false;
} }

64
lzlib.c
View file

@ -1,20 +1,20 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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 <stdbool.h> #include <stdbool.h>
@ -105,11 +105,12 @@ static bool verify_decoder( struct LZ_Decoder * const d )
} }
/*------------------------- Misc Functions -------------------------*/ /* ------------------------- Misc Functions ------------------------- */
int LZ_api_version( void ) { return LZ_API_VERSION; }
const char * LZ_version( void ) { return LZ_version_string; } const char * LZ_version( void ) { return LZ_version_string; }
const char * LZ_strerror( const enum LZ_Errno lz_errno ) const char * LZ_strerror( const enum LZ_Errno lz_errno )
{ {
switch( lz_errno ) switch( lz_errno )
@ -135,7 +136,7 @@ int LZ_min_match_len_limit( void ) { return min_match_len_limit; }
int LZ_max_match_len_limit( void ) { return max_match_len; } int LZ_max_match_len_limit( void ) { return max_match_len; }
/*---------------------- Compression Functions ----------------------*/ /* --------------------- Compression Functions --------------------- */
struct LZ_Encoder * LZ_compress_open( const int dictionary_size, struct LZ_Encoder * LZ_compress_open( const int dictionary_size,
const int match_len_limit, const int match_len_limit,
@ -226,8 +227,8 @@ int LZ_compress_restart_member( struct LZ_Encoder * const e,
int LZ_compress_sync_flush( struct LZ_Encoder * const e ) int LZ_compress_sync_flush( struct LZ_Encoder * const e )
{ {
if( !verify_encoder( e ) || e->fatal ) return -1; if( !verify_encoder( e ) || e->fatal ) return -1;
if( !Mb_flushing_or_end( &e->lz_encoder_base->mb ) ) if( !e->lz_encoder_base->mb.at_stream_end )
e->lz_encoder_base->mb.flushing = true; e->lz_encoder_base->mb.sync_flush_pending = true;
return 0; return 0;
} }
@ -235,24 +236,22 @@ int LZ_compress_sync_flush( struct LZ_Encoder * const e )
int LZ_compress_read( struct LZ_Encoder * const e, int LZ_compress_read( struct LZ_Encoder * const e,
uint8_t * const buffer, const int size ) uint8_t * const buffer, const int size )
{ {
int out_size = 0;
if( !verify_encoder( e ) || e->fatal ) return -1; if( !verify_encoder( e ) || e->fatal ) return -1;
if( size < 0 ) return 0; if( size < 0 ) return 0;
do {
{ struct LZ_encoder_base * const eb = e->lz_encoder_base;
int out_size = Re_read_data( &eb->renc, buffer, size );
/* minimize number of calls to encode_member */
if( out_size < size || size == 0 )
{
if( ( e->flz_encoder && !FLZe_encode_member( e->flz_encoder ) ) || if( ( e->flz_encoder && !FLZe_encode_member( e->flz_encoder ) ) ||
( e->lz_encoder && !LZe_encode_member( e->lz_encoder ) ) ) ( e->lz_encoder && !LZe_encode_member( e->lz_encoder ) ) )
{ e->lz_errno = LZ_library_error; e->fatal = true; return -1; } { e->lz_errno = LZ_library_error; e->fatal = true; return -1; }
if( e->lz_encoder_base->mb.flushing && if( eb->mb.sync_flush_pending && Mb_available_bytes( &eb->mb ) <= 0 )
Mb_available_bytes( &e->lz_encoder_base->mb ) <= 0 && LZeb_try_sync_flush( eb );
LZeb_sync_flush( e->lz_encoder_base ) ) out_size += Re_read_data( &eb->renc, buffer + out_size, size - out_size );
e->lz_encoder_base->mb.flushing = false;
out_size += Re_read_data( &e->lz_encoder_base->renc,
buffer + out_size, size - out_size );
} }
while( e->lz_encoder_base->mb.flushing && out_size < size && return out_size; }
Mb_enough_available_bytes( &e->lz_encoder_base->mb ) &&
Re_enough_free_bytes( &e->lz_encoder_base->renc ) );
return out_size;
} }
@ -321,7 +320,7 @@ unsigned long long LZ_compress_total_out_size( struct LZ_Encoder * const e )
} }
/*--------------------- Decompression Functions ---------------------*/ /* -------------------- Decompression Functions -------------------- */
struct LZ_Decoder * LZ_decompress_open( void ) struct LZ_Decoder * LZ_decompress_open( void )
{ {
@ -401,10 +400,11 @@ int LZ_decompress_read( struct LZ_Decoder * const d,
{ {
int result; int result;
if( !verify_decoder( d ) ) return -1; if( !verify_decoder( d ) ) return -1;
if( size < 0 ) return 0;
if( d->fatal ) /* don't return error until pending bytes are read */ if( d->fatal ) /* don't return error until pending bytes are read */
{ if( d->lz_decoder && !Cb_empty( &d->lz_decoder->cb ) ) goto get_data; { if( d->lz_decoder && !Cb_empty( &d->lz_decoder->cb ) ) goto get_data;
return -1; } return -1; }
if( d->seeking || size < 0 ) return 0; if( d->seeking ) return 0;
if( d->lz_decoder && LZd_member_finished( d->lz_decoder ) ) if( d->lz_decoder && LZd_member_finished( d->lz_decoder ) )
{ {

38
lzlib.h
View file

@ -1,35 +1,39 @@
/* Lzlib - Compression library for the lzip format /* Lzlib - Compression library for the lzip format
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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.
*/ */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define LZ_API_VERSION 1 /* LZ_API_VERSION was first defined in lzlib 1.8 to 1.
Since lzlib 1.12, LZ_API_VERSION is defined as (major * 1000 + minor). */
static const char * const LZ_version_string = "1.11"; #define LZ_API_VERSION 1012
static const char * const LZ_version_string = "1.12";
enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error, enum LZ_Errno { LZ_ok = 0, LZ_bad_argument, LZ_mem_error,
LZ_sequence_error, LZ_header_error, LZ_unexpected_eof, LZ_sequence_error, LZ_header_error, LZ_unexpected_eof,
LZ_data_error, LZ_library_error }; LZ_data_error, LZ_library_error };
int LZ_api_version( void ); /* new in 1.12 */
const char * LZ_version( void ); const char * LZ_version( void );
const char * LZ_strerror( const enum LZ_Errno lz_errno ); const char * LZ_strerror( const enum LZ_Errno lz_errno );
@ -41,7 +45,7 @@ int LZ_min_match_len_limit( void );
int LZ_max_match_len_limit( void ); int LZ_max_match_len_limit( void );
/*---------------------- Compression Functions ----------------------*/ /* --------------------- Compression Functions --------------------- */
struct LZ_Encoder; struct LZ_Encoder;
@ -71,7 +75,7 @@ unsigned long long LZ_compress_total_in_size( struct LZ_Encoder * const encoder
unsigned long long LZ_compress_total_out_size( struct LZ_Encoder * const encoder ); unsigned long long LZ_compress_total_out_size( struct LZ_Encoder * const encoder );
/*--------------------- Decompression Functions ---------------------*/ /* -------------------- Decompression Functions -------------------- */
struct LZ_Decoder; struct LZ_Decoder;

422
main.c
View file

@ -1,24 +1,24 @@
/* Minilzip - Test program for the lzlib library /* Minilzip - Test program for the library lzlib
Copyright (C) 2009-2019 Antonio Diaz Diaz. Copyright (C) 2009-2021 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 flags, I/O errors, etc), 2 to indicate a
corrupt or invalid input file, 3 for an internal consistency error corrupt or invalid input file, 3 for an internal consistency error
(eg, bug) which caused minilzip to panic. (eg, bug) which caused minilzip to panic.
*/ */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
@ -74,19 +74,21 @@
#define min(x,y) ((x) <= (y) ? (x) : (y)) #define min(x,y) ((x) <= (y) ? (x) : (y))
#endif #endif
void cleanup_and_fail( const int retval ); static void cleanup_and_fail( const int retval );
void show_error( const char * const msg, const int errcode, const bool help ); static void show_error( const char * const msg, const int errcode,
void show_file_error( const char * const filename, const char * const msg, const bool help );
const int errcode ); static void show_file_error( const char * const filename,
void internal_error( const char * const msg ); const char * const msg, const int errcode );
static void internal_error( const char * const msg );
static const char * const mem_msg = "Not enough memory.";
int verbosity = 0; int verbosity = 0;
const char * const program_name = "minilzip"; static const char * const program_name = "minilzip";
const char * const program_year = "2019"; static const char * const program_year = "2021";
const char * invocation_name = 0; static const char * invocation_name = "minilzip"; /* default value */
const struct { const char * from; const char * to; } known_extensions[] = { static const struct { const char * from; const char * to; } known_extensions[] = {
{ ".lz", "" }, { ".lz", "" },
{ ".tlz", ".tar" }, { ".tlz", ".tar" },
{ 0, 0 } }; { 0, 0 } };
@ -101,15 +103,24 @@ enum Mode { m_compress, m_decompress, m_test };
/* Variables used in signal handler context. /* Variables used in signal handler context.
They are not declared volatile because the handler never returns. */ They are not declared volatile because the handler never returns. */
char * output_filename = 0; static char * output_filename = 0;
int outfd = -1; static int outfd = -1;
bool delete_output_on_interrupt = false; static bool delete_output_on_interrupt = false;
static void show_help( void ) static void show_help( void )
{ {
printf( "Minilzip is a test program for the lzlib compression library, fully\n" printf( "Minilzip is a test program for the compression library lzlib, fully\n"
"compatible with lzip 1.4 or newer.\n" "compatible with lzip 1.4 or newer.\n"
"\nLzip is a lossless data compressor with a user interface similar to the one\n"
"of gzip or bzip2. Lzip uses a simplified form of the 'Lempel-Ziv-Markov\n"
"chain-Algorithm' (LZMA) stream format, chosen to maximize safety and\n"
"interoperability. Lzip can compress about as fast as gzip (lzip -0) or\n"
"compress most files more than bzip2 (lzip -9). Decompression speed is\n"
"intermediate between gzip and bzip2. Lzip is better than gzip and bzip2 from\n"
"a data recovery perspective. Lzip has been designed, written, and tested\n"
"with great care to replace gzip and bzip2 as the standard general-purpose\n"
"compressed format for unix-like systems.\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"
@ -122,7 +133,7 @@ static void show_help( void )
" -F, --recompress force re-compression of compressed files\n" " -F, --recompress force re-compression of compressed files\n"
" -k, --keep keep (don't delete) input files\n" " -k, --keep keep (don't delete) input files\n"
" -m, --match-length=<bytes> set match length limit in bytes [36]\n" " -m, --match-length=<bytes> set match length limit in bytes [36]\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"
" -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8 MiB]\n" " -s, --dictionary-size=<bytes> set dictionary size limit in bytes [8 MiB]\n"
" -S, --volume-size=<bytes> set volume size limit in bytes\n" " -S, --volume-size=<bytes> set volume size limit in bytes\n"
@ -132,7 +143,8 @@ static void show_help( void )
" --fast alias for -0\n" " --fast alias for -0\n"
" --best alias for -9\n" " --best alias for -9\n"
" --loose-trailing allow trailing data seeming corrupt header\n" " --loose-trailing allow trailing data seeming corrupt header\n"
"If no file names are given, or if a file is '-', minilzip compresses or\n" " --check-lib compare version of lzlib.h with liblz.{a,so}\n"
"\nIf no file names are given, or if a file is '-', minilzip compresses or\n"
"decompresses from standard input to standard output.\n" "decompresses 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"
@ -140,12 +152,19 @@ static void show_help( void )
"to 2^29 bytes.\n" "to 2^29 bytes.\n"
"\nThe bidimensional parameter space of LZMA can't be mapped to a linear\n" "\nThe bidimensional parameter space of LZMA can't be mapped to a linear\n"
"scale optimal for all files. If your files are large, very repetitive,\n" "scale optimal for all files. If your files are large, very repetitive,\n"
"etc, you may need to use the --dictionary-size and --match-length\n" "etc, you may need to use the options --dictionary-size and --match-length\n"
"options directly to achieve optimal performance.\n" "directly to achieve optimal performance.\n"
"\nTo extract all the files from archive 'foo.tar.lz', use the commands\n"
"'tar -xf foo.tar.lz' or 'minilzip -cd foo.tar.lz | tar -xf -'.\n"
"\nExit status: 0 for a normal exit, 1 for environmental problems (file\n" "\nExit status: 0 for a normal exit, 1 for environmental problems (file\n"
"not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n" "not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or\n"
"invalid input file, 3 for an internal consistency error (eg, bug) which\n" "invalid input file, 3 for an internal consistency error (eg, bug) which\n"
"caused minilzip to panic.\n" "caused minilzip to panic.\n"
"\nThe ideas embodied in lzlib are due to (at least) the following people:\n"
"Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrey Markov (for the\n"
"definition of Markov chains), G.N.N. Martin (for the definition of range\n"
"encoding), Igor Pavlov (for putting all the above together in LZMA), and\n"
"Julian Seward (for bzip2's CLI).\n"
"\nReport bugs to lzip-bug@nongnu.org\n" "\nReport bugs to lzip-bug@nongnu.org\n"
"Lzlib home page: http://www.nongnu.org/lzip/lzlib.html\n" ); "Lzlib home page: http://www.nongnu.org/lzip/lzlib.html\n" );
} }
@ -162,16 +181,43 @@ static void show_version( void )
} }
int check_lib()
{
bool warning = false;
if( strcmp( LZ_version_string, LZ_version() ) != 0 )
{ warning = true;
if( verbosity >= 0 )
printf( "warning: LZ_version_string != LZ_version() (%s vs %s)\n",
LZ_version_string, LZ_version() ); }
#if defined LZ_API_VERSION && LZ_API_VERSION >= 1012
if( LZ_API_VERSION != LZ_api_version() )
{ warning = true;
if( verbosity >= 0 )
printf( "warning: LZ_API_VERSION != LZ_api_version() (%u vs %u)\n",
LZ_API_VERSION, LZ_api_version() ); }
#endif
if( verbosity >= 1 )
{
printf( "Using lzlib %s\n", LZ_version() );
#if !defined LZ_API_VERSION
fputs( "LZ_API_VERSION is not defined.\n", stdout );
#elif LZ_API_VERSION >= 1012
printf( "Using LZ_API_VERSION = %u\n", LZ_api_version() );
#else
printf( "Compiled with LZ_API_VERSION = %u. "
"Using an unknown LZ_API_VERSION\n", LZ_API_VERSION );
#endif
}
return warning;
}
/* assure at least a minimum size for buffer 'buf' */ /* assure at least a minimum size for buffer 'buf' */
static 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;
} }
@ -186,8 +232,7 @@ struct Pretty_print
}; };
static void Pp_init( struct Pretty_print * const pp, static void Pp_init( struct Pretty_print * const pp,
const char * const filenames[], const char * const filenames[], const int num_filenames )
const int num_filenames )
{ {
unsigned stdin_name_len; unsigned stdin_name_len;
int i; int i;
@ -208,8 +253,8 @@ static void Pp_init( struct Pretty_print * const pp,
if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len; if( pp->longest_name == 0 ) pp->longest_name = stdin_name_len;
} }
static inline void Pp_set_name( struct Pretty_print * const pp, static void Pp_set_name( struct Pretty_print * const pp,
const char * const filename ) const char * const filename )
{ {
unsigned name_len, padded_name_len, i = 0; unsigned name_len, padded_name_len, i = 0;
@ -227,7 +272,7 @@ static inline void Pp_set_name( struct Pretty_print * const pp,
pp->first_post = true; pp->first_post = true;
} }
static inline void Pp_reset( struct Pretty_print * const pp ) static void Pp_reset( struct Pretty_print * const pp )
{ if( pp->name && pp->name[0] ) pp->first_post = true; } { if( pp->name && pp->name[0] ) pp->first_post = true; }
static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg ) static void Pp_show_msg( struct Pretty_print * const pp, const char * const msg )
@ -258,7 +303,7 @@ static void show_header( const unsigned dictionary_size )
int i; for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i ) int i; for( i = 0; i < 8 && ( num > 9999 || ( exact && num >= factor ) ); ++i )
{ num /= factor; if( num % factor != 0 ) exact = false; { num /= factor; if( num % factor != 0 ) exact = false;
p = prefix[i]; np = ""; } p = prefix[i]; np = ""; }
fprintf( stderr, "dictionary %s%4u %sB, ", np, num, p ); fprintf( stderr, "dict %s%4u %sB, ", np, num, p );
} }
@ -321,7 +366,7 @@ static int get_dict_size( const char * const arg )
const long bits = strtol( arg, &tail, 0 ); const long bits = strtol( arg, &tail, 0 );
if( bits >= LZ_min_dictionary_bits() && if( bits >= LZ_min_dictionary_bits() &&
bits <= LZ_max_dictionary_bits() && *tail == 0 ) bits <= LZ_max_dictionary_bits() && *tail == 0 )
return ( 1 << bits ); return 1 << bits;
dictionary_size = getnum( arg, LZ_min_dictionary_size(), dictionary_size = getnum( arg, LZ_min_dictionary_size(),
LZ_max_dictionary_size() ); LZ_max_dictionary_size() );
if( dictionary_size == 65535 ) ++dictionary_size; /* no fast encoder */ if( dictionary_size == 65535 ) ++dictionary_size; /* no fast encoder */
@ -329,7 +374,7 @@ static int get_dict_size( const char * const arg )
} }
void set_mode( enum Mode * const program_modep, const enum Mode new_mode ) static void set_mode( enum Mode * const program_modep, const enum Mode new_mode )
{ {
if( *program_modep != m_compress && *program_modep != new_mode ) if( *program_modep != m_compress && *program_modep != new_mode )
{ {
@ -363,7 +408,7 @@ static void set_c_outname( const char * const name, const bool force_ext,
strlen( known_extensions[0].from ) + 1 ); strlen( known_extensions[0].from ) + 1 );
strcpy( output_filename, name ); strcpy( output_filename, name );
if( multifile ) strcat( output_filename, "00001" ); if( multifile ) strcat( output_filename, "00001" );
if( force_ext || multifile || extension_index( output_filename ) < 0 ) if( force_ext || multifile )
strcat( output_filename, known_extensions[0].from ); strcat( output_filename, known_extensions[0].from );
} }
@ -395,7 +440,7 @@ static void set_d_outname( const char * const name, const int eindex )
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 enum Mode program_mode, const int eindex, const enum Mode program_mode, const int eindex,
const bool recompress, const bool to_stdout ) const bool one_to_one, const bool recompress )
{ {
int infd = -1; int infd = -1;
if( program_mode == m_compress && !recompress && eindex >= 0 ) if( program_mode == m_compress && !recompress && eindex >= 0 )
@ -416,14 +461,12 @@ static int open_instream( const char * const name, struct stat * const in_statsp
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 ) ) );
const bool no_ofile = ( to_stdout || program_mode == m_test ); if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || one_to_one ) ) )
if( i != 0 || ( !S_ISREG( mode ) && ( !can_read || !no_ofile ) ) )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
fprintf( stderr, "%s: Input file '%s' is not a regular file%s.\n", fprintf( stderr, "%s: Input file '%s' 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;
} }
@ -433,11 +476,11 @@ 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_CREAT | O_WRONLY | O_BINARY; int flags = O_CREAT | O_WRONLY | O_BINARY;
if( force ) flags |= O_TRUNC; else flags |= O_EXCL; if( force ) flags |= O_TRUNC; else flags |= O_EXCL;
@ -456,25 +499,6 @@ static bool open_outstream( const bool force, const bool from_stdin )
} }
static bool check_tty( const char * const input_filename, const int infd,
const enum Mode program_mode )
{
if( program_mode == m_compress && isatty( outfd ) )
{
show_error( "I won't write compressed data to a terminal.", 0, true );
return false;
}
if( ( program_mode == m_decompress || program_mode == m_test ) &&
isatty( infd ) )
{
show_file_error( input_filename,
"I won't read compressed data from a terminal.", 0 );
return false;
}
return true;
}
static void set_signals( void (*action)(int) ) static void set_signals( void (*action)(int) )
{ {
signal( SIGHUP, action ); signal( SIGHUP, action );
@ -483,7 +507,7 @@ static void set_signals( void (*action)(int) )
} }
void cleanup_and_fail( const int retval ) static void cleanup_and_fail( const int retval )
{ {
set_signals( SIG_IGN ); /* ignore signals */ set_signals( SIG_IGN ); /* ignore signals */
if( delete_output_on_interrupt ) if( delete_output_on_interrupt )
@ -500,7 +524,7 @@ void cleanup_and_fail( const int retval )
} }
void signal_handler( int sig ) static void signal_handler( int sig )
{ {
if( sig ) {} /* keep compiler happy */ if( sig ) {} /* keep compiler happy */
show_error( "Control-C or similar caught, quitting.", 0, false ); show_error( "Control-C or similar caught, quitting.", 0, false );
@ -508,7 +532,35 @@ void signal_handler( int sig )
} }
/* Set permissions, owner and times. */ static inline void set_retval( int * retval, const int new_val )
{ if( *retval < new_val ) *retval = new_val; }
static bool check_tty_in( const char * const input_filename, const int infd,
const enum Mode program_mode, int * const retval )
{
if( ( program_mode == m_decompress || program_mode == m_test ) &&
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, 1 );
if( program_mode != m_test ) cleanup_and_fail( *retval );
return false; }
return true;
}
static bool check_tty_out( const enum Mode program_mode )
{
if( program_mode == m_compress && isatty( outfd ) )
{ show_file_error( output_filename[0] ?
output_filename : "(stdout)",
"I won't write compressed data to a terminal.", 0 );
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;
@ -602,7 +654,7 @@ static int do_compress( struct LZ_Encoder * const encoder,
{ {
unsigned long long partial_volume_size = 0; unsigned long long partial_volume_size = 0;
enum { buffer_size = 65536 }; enum { buffer_size = 65536 };
uint8_t buffer[buffer_size]; uint8_t buffer[buffer_size]; /* read/write buffer */
if( verbosity >= 1 ) Pp_show_msg( pp, 0 ); if( verbosity >= 1 ) Pp_show_msg( pp, 0 );
while( true ) while( true )
@ -658,7 +710,7 @@ static int do_compress( struct LZ_Encoder * const encoder,
close_and_set_permissions( in_statsp ); close_and_set_permissions( in_statsp );
if( !next_filename() ) if( !next_filename() )
{ Pp_show_msg( pp, "Too many volume files." ); return 1; } { Pp_show_msg( pp, "Too many volume files." ); return 1; }
if( !open_outstream( true, !in_statsp ) ) return 1; if( !open_outstream( true, in_statsp ) ) return 1;
} }
} }
size = min( member_size, volume_size - partial_volume_size ); size = min( member_size, volume_size - partial_volume_size );
@ -726,7 +778,8 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd,
const bool loose_trailing, const bool testing ) const bool loose_trailing, const bool testing )
{ {
enum { buffer_size = 65536 }; enum { buffer_size = 65536 };
uint8_t buffer[buffer_size]; uint8_t buffer[buffer_size]; /* read/write buffer */
unsigned long long total_in = 0; /* to detect library stall */
bool first_member; bool first_member;
for( first_member = true; ; ) for( first_member = true; ; )
@ -777,17 +830,16 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd,
if( verbosity >= 4 ) if( verbosity >= 4 )
show_header( LZ_decompress_dictionary_size( decoder ) ); show_header( LZ_decompress_dictionary_size( decoder ) );
if( data_size == 0 || member_size == 0 ) if( data_size == 0 || member_size == 0 )
fputs( "no data compressed. ", stderr ); fputs( "no data compressed. ", stderr );
else else
fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ", fprintf( stderr, "%6.3f:1, %5.2f%% ratio, %5.2f%% saved. ",
(double)data_size / member_size, (double)data_size / member_size,
( 100.0 * member_size ) / data_size, ( 100.0 * member_size ) / data_size,
100.0 - ( ( 100.0 * member_size ) / data_size ) ); 100.0 - ( ( 100.0 * member_size ) / data_size ) );
if( verbosity >= 4 ) if( verbosity >= 4 )
fprintf( stderr, "CRC %08X, ", LZ_decompress_data_crc( decoder ) ); fprintf( stderr, "CRC %08X, ", LZ_decompress_data_crc( decoder ) );
if( verbosity >= 3 ) if( verbosity >= 3 )
fprintf( stderr, "decompressed %9llu, compressed %8llu. ", fprintf( stderr, "%9llu out, %8llu in. ", data_size, member_size );
data_size, member_size );
fputs( testing ? "ok\n" : "done\n", stderr ); Pp_reset( pp ); fputs( testing ? "ok\n" : "done\n", stderr ); Pp_reset( pp );
} }
} }
@ -840,8 +892,7 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd,
else break; /* trailing data */ else break; /* trailing data */
return 2; return 2;
} }
if( lz_errno == LZ_mem_error ) if( lz_errno == LZ_mem_error ) { Pp_show_msg( pp, mem_msg ); return 1; }
{ Pp_show_msg( pp, "Not enough memory." ); return 1; }
if( verbosity >= 0 ) if( verbosity >= 0 )
{ {
Pp_show_msg( pp, 0 ); Pp_show_msg( pp, 0 );
@ -853,7 +904,11 @@ static int do_decompress( struct LZ_Decoder * const decoder, const int infd,
} }
if( LZ_decompress_finished( decoder ) == 1 ) break; if( LZ_decompress_finished( decoder ) == 1 ) break;
if( in_size == 0 && out_size == 0 ) if( in_size == 0 && out_size == 0 )
internal_error( "library error (stalled)." ); {
const unsigned long long size = LZ_decompress_total_in_size( decoder );
if( total_in == size ) internal_error( "library error (stalled)." );
total_in = size;
}
} }
if( verbosity == 1 ) fputs( testing ? "ok\n" : "done\n", stderr ); if( verbosity == 1 ) fputs( testing ? "ok\n" : "done\n", stderr );
return 0; return 0;
@ -868,7 +923,7 @@ static int decompress( const int infd, struct Pretty_print * const pp,
int retval; int retval;
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ Pp_show_msg( pp, "Not enough memory." ); retval = 1; } { Pp_show_msg( pp, mem_msg ); retval = 1; }
else retval = do_decompress( decoder, infd, pp, ignore_trailing, else retval = do_decompress( decoder, infd, pp, ignore_trailing,
loose_trailing, testing ); loose_trailing, testing );
LZ_decompress_close( decoder ); LZ_decompress_close( decoder );
@ -876,7 +931,8 @@ static int decompress( const int infd, struct Pretty_print * const pp,
} }
void show_error( const char * const msg, const int errcode, const bool help ) static 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] )
@ -889,8 +945,8 @@ void show_error( const char * const msg, const int errcode, const bool help )
} }
void show_file_error( const char * const filename, const char * const msg, static void show_file_error( const char * const filename,
const int errcode ) const char * const msg, const int errcode )
{ {
if( verbosity >= 0 ) if( verbosity >= 0 )
fprintf( stderr, "%s: %s: %s%s%s\n", program_name, filename, msg, fprintf( stderr, "%s: %s: %s%s%s\n", program_name, filename, msg,
@ -899,7 +955,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 );
@ -924,12 +980,14 @@ int main( const int argc, const char * const argv[] )
{ 3 << 23, 132 }, /* -8 */ { 3 << 23, 132 }, /* -8 */
{ 1 << 25, 273 } }; /* -9 */ { 1 << 25, 273 } }; /* -9 */
struct Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */ struct Lzma_options encoder_options = option_mapping[6]; /* default = "-6" */
const unsigned long long max_member_size = 0x0008000000000000ULL; const unsigned long long max_member_size = 0x0008000000000000ULL; /* 2 PiB */
const unsigned long long max_volume_size = 0x4000000000000000ULL; const unsigned long long max_volume_size = 0x4000000000000000ULL; /* 4 EiB */
unsigned long long member_size = max_member_size; unsigned long long member_size = max_member_size;
unsigned long long volume_size = 0; unsigned long long volume_size = 0;
const char * default_output_filename = ""; const char * default_output_filename = "";
const char ** filenames = 0; static struct Arg_parser parser; /* static because valgrind complains */
static struct Pretty_print pp; /* and memory management in C sucks */
static const char ** filenames = 0;
int num_filenames = 0; int num_filenames = 0;
enum Mode program_mode = m_compress; enum Mode program_mode = m_compress;
int argind = 0; int argind = 0;
@ -944,20 +1002,19 @@ int main( const int argc, const char * const argv[] )
bool recompress = false; bool recompress = false;
bool stdin_used = false; bool stdin_used = false;
bool to_stdout = false; bool to_stdout = false;
struct Pretty_print pp;
enum { opt_lt = 256 }; enum { opt_chk = 256, opt_lt };
const struct ap_Option options[] = const struct ap_Option options[] =
{ {
{ '0', "fast", ap_no }, { '0', "fast", ap_no },
{ '1', 0, ap_no }, { '1', 0, ap_no },
{ '2', 0, ap_no }, { '2', 0, ap_no },
{ '3', 0, ap_no }, { '3', 0, ap_no },
{ '4', 0, ap_no }, { '4', 0, ap_no },
{ '5', 0, ap_no }, { '5', 0, ap_no },
{ '6', 0, ap_no }, { '6', 0, ap_no },
{ '7', 0, ap_no }, { '7', 0, ap_no },
{ '8', 0, ap_no }, { '8', 0, ap_no },
{ '9', "best", ap_no }, { '9', "best", ap_no },
{ 'a', "trailing-error", ap_no }, { 'a', "trailing-error", ap_no },
{ 'b', "member-size", ap_yes }, { 'b', "member-size", ap_yes },
@ -976,20 +1033,14 @@ 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_chk, "check-lib", ap_no },
{ opt_lt, "loose-trailing", ap_no }, { opt_lt, "loose-trailing", ap_no },
{ 0 , 0, ap_no } }; { 0, 0, ap_no } };
struct Arg_parser parser; if( argc > 0 ) invocation_name = argv[0];
invocation_name = argv[0];
if( LZ_version()[0] != LZ_version_string[0] )
internal_error( "bad library version." );
if( strcmp( PROGVERSION, LZ_version_string ) != 0 )
internal_error( "bad library version_string." );
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; }
@ -1015,7 +1066,8 @@ int main( const int argc, const char * const argv[] )
getnum( arg, LZ_min_match_len_limit(), getnum( arg, LZ_min_match_len_limit(),
LZ_max_match_len_limit() ); break; LZ_max_match_len_limit() ); break;
case 'n': break; case 'n': break;
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 's': encoder_options.dictionary_size = get_dict_size( arg ); case 's': encoder_options.dictionary_size = get_dict_size( arg );
break; break;
@ -1023,11 +1075,27 @@ int main( const int argc, const char * const argv[] )
case 't': set_mode( &program_mode, m_test ); break; case 't': set_mode( &program_mode, m_test ); 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_chk: return check_lib();
case opt_lt: loose_trailing = true; break; case opt_lt: loose_trailing = true; break;
default : internal_error( "uncaught option." ); default : internal_error( "uncaught option." );
} }
} /* end process options */ } /* end process options */
if( strcmp( PROGVERSION, LZ_version_string ) != 0 )
internal_error( "wrong PROGVERSION." );
#if !defined LZ_API_VERSION || LZ_API_VERSION < 1012
#error "lzlib 1.12 or newer needed."
#else
if( LZ_api_version() < 1012 ) /* minilzip passes null to LZ_decompress_read */
{ show_error( "lzlib 1.12 or newer needed. Try --check-lib.", 0, false );
return 1; }
if( LZ_api_version() != LZ_API_VERSION ) show_error(
"warning: wrong library API version. Try --check-lib.", 0, false );
else
#endif
if( strcmp( LZ_version_string, LZ_version() ) != 0 ) show_error(
"warning: wrong library version_string. Try --check-lib.", 0, false );
#if defined(__MSVCRT__) || defined(__OS2__) || defined(__DJGPP__) #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 );
@ -1043,16 +1111,31 @@ int main( const int argc, const char * const argv[] )
if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true; if( strcmp( filenames[i], "-" ) != 0 ) filenames_given = true;
} }
if( program_mode == m_test ) if( program_mode == m_compress )
outfd = -1; {
if( volume_size > 0 && !to_stdout && default_output_filename[0] &&
num_filenames > 1 )
{ show_error( "Only can compress one file when using '-o' and '-S'.",
0, true ); return 1; }
}
else volume_size = 0;
if( program_mode == m_test ) to_stdout = false; /* apply overrides */
if( program_mode == m_test || to_stdout ) default_output_filename = "";
if( !to_stdout && program_mode != m_test && output_filename = resize_buffer( output_filename, 1 );
( filenames_given || default_output_filename[0] ) ) output_filename[0] = 0;
if( to_stdout && program_mode != m_test ) /* check tty only once */
{ outfd = STDOUT_FILENO; if( !check_tty_out( program_mode ) ) return 1; }
else outfd = -1;
const bool to_file = !to_stdout && program_mode != m_test &&
default_output_filename[0];
if( !to_stdout && program_mode != m_test && ( filenames_given || to_file ) )
set_signals( signal_handler ); set_signals( signal_handler );
Pp_init( &pp, filenames, num_filenames ); Pp_init( &pp, filenames, num_filenames );
output_filename = resize_buffer( output_filename, 1 ); const bool one_to_one = !to_stdout && program_mode != m_test && !to_file;
for( i = 0; i < num_filenames; ++i ) for( i = 0; i < num_filenames; ++i )
{ {
const char * input_filename = ""; const char * input_filename = "";
@ -1060,68 +1143,48 @@ int main( const int argc, const char * const argv[] )
int tmp; int tmp;
struct stat in_stats; struct stat in_stats;
const struct stat * in_statsp; 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( program_mode != m_test ) if( !check_tty_in( pp.name, infd, program_mode, &retval ) ) continue;
{ if( one_to_one ) { outfd = STDOUT_FILENO; output_filename[0] = 0; }
if( to_stdout || !default_output_filename[0] )
outfd = STDOUT_FILENO;
else
{
if( program_mode == m_compress )
set_c_outname( default_output_filename, false, volume_size > 0 );
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 );
continue;
}
}
}
} }
else else
{ {
const int eindex = extension_index( input_filename = filenames[i] ); const int eindex = extension_index( input_filename = filenames[i] );
infd = open_instream( input_filename, &in_stats, program_mode, infd = open_instream( input_filename, &in_stats, program_mode,
eindex, recompress, to_stdout ); eindex, one_to_one, recompress );
if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; } if( infd < 0 ) { set_retval( &retval, 1 ); continue; }
if( program_mode != m_test ) if( !check_tty_in( pp.name, infd, program_mode, &retval ) ) continue;
if( one_to_one ) /* open outfd after verifying infd */
{ {
if( to_stdout ) outfd = STDOUT_FILENO; if( program_mode == m_compress )
else set_c_outname( input_filename, true, volume_size > 0 );
{ else set_d_outname( input_filename, eindex );
if( program_mode == m_compress ) if( !open_outstream( force, true ) )
set_c_outname( input_filename, true, volume_size > 0 ); { close( infd ); set_retval( &retval, 1 ); continue; }
else set_d_outname( input_filename, eindex );
if( !open_outstream( force, false ) )
{
if( retval < 1 ) retval = 1;
close( infd );
continue;
}
}
} }
} }
Pp_set_name( &pp, input_filename ); if( one_to_one && !check_tty_out( program_mode ) )
if( !check_tty( pp.name, infd, program_mode ) ) { set_retval( &retval, 1 ); return retval; } /* don't delete a tty */
if( to_file && outfd < 0 ) /* open outfd after verifying infd */
{ {
if( retval < 1 ) retval = 1; if( program_mode == m_compress ) set_c_outname( default_output_filename,
if( program_mode == m_test ) { close( infd ); continue; } false, volume_size > 0 );
cleanup_and_fail( retval ); else
{ output_filename = resize_buffer( output_filename,
strlen( default_output_filename ) + 1 );
strcpy( output_filename, default_output_filename ); }
if( !open_outstream( force, false ) || !check_tty_out( program_mode ) )
return 1; /* check tty only once and don't try to delete a tty */
} }
in_statsp = input_filename[0] ? &in_stats : 0; in_statsp = ( input_filename[0] && one_to_one ) ? &in_stats : 0;
if( program_mode == m_compress ) if( program_mode == m_compress )
tmp = compress( member_size, volume_size, infd, &encoder_options, &pp, tmp = compress( member_size, volume_size, infd, &encoder_options, &pp,
in_statsp ); in_statsp );
@ -1129,29 +1192,24 @@ int main( const int argc, const char * const argv[] )
tmp = decompress( infd, &pp, ignore_trailing, tmp = decompress( infd, &pp, ignore_trailing,
loose_trailing, program_mode == m_test ); loose_trailing, program_mode == m_test );
if( close( infd ) != 0 ) if( close( infd ) != 0 )
{ { show_file_error( pp.name, "Error closing input file", errno );
show_error( input_filename[0] ? "Error closing input file" : set_retval( &tmp, 1 ); }
"Error closing stdin", errno, false ); set_retval( &retval, tmp );
if( tmp < 1 ) tmp = 1;
}
if( tmp > retval ) retval = tmp;
if( tmp ) if( tmp )
{ if( program_mode != m_test ) cleanup_and_fail( retval ); { if( program_mode != m_test ) 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 &&
{ ( program_mode != m_compress || volume_size == 0 ) )
if( !keep_input_files && !to_stdout && program_mode != m_test && remove( input_filename );
( program_mode != m_compress || volume_size == 0 ) )
remove( input_filename );
}
} }
if( outfd >= 0 && close( outfd ) != 0 ) if( delete_output_on_interrupt ) close_and_set_permissions( 0 ); /* -o */
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",

View file

@ -1,9 +1,9 @@
#! /bin/sh #! /bin/sh
# check script for Lzlib - Compression library for the lzip format # check script for Lzlib - Compression library for the lzip format
# Copyright (C) 2009-2019 Antonio Diaz Diaz. # Copyright (C) 2009-2021 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
@ -11,6 +11,7 @@ objdir=`pwd`
testdir=`cd "$1" ; pwd` testdir=`cd "$1" ; pwd`
LZIP="${objdir}"/minilzip LZIP="${objdir}"/minilzip
BBEXAMPLE="${objdir}"/bbexample BBEXAMPLE="${objdir}"/bbexample
FFEXAMPLE="${objdir}"/ffexample
LZCHECK="${objdir}"/lzcheck LZCHECK="${objdir}"/lzcheck
framework_failure() { echo "failure in testing framework" ; exit 1 ; } framework_failure() { echo "failure in testing framework" ; exit 1 ; }
@ -32,10 +33,13 @@ cd "${objdir}"/tmp || framework_failure
cat "${testdir}"/test.txt > in || framework_failure cat "${testdir}"/test.txt > in || framework_failure
in_lz="${testdir}"/test.txt.lz in_lz="${testdir}"/test.txt.lz
test2="${testdir}"/test2.txt in_em="${testdir}"/test_em.txt.lz
fox_lf="${testdir}"/fox_lf
fox_lz="${testdir}"/fox.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)" ; }
"${LZIP}" --check-lib # just print warning
printf "testing lzlib-%s..." "$2" printf "testing lzlib-%s..." "$2"
"${LZIP}" -fkqm4 in "${LZIP}" -fkqm4 in
@ -59,10 +63,22 @@ done
[ $? = 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
"${LZIP}" -q -o out.lz nx_file
[ $? = 1 ] || test_failed $LINENO
[ ! -e out.lz ] || test_failed $LINENO
"${LZIP}" -qf -S100k -o out in in
[ $? = 1 ] || test_failed $LINENO
# these are for code coverage # these are for code coverage
"${LZIP}" -cdt "${in_lz}" > out 2> /dev/null "${LZIP}" -cdt "${in_lz}" > out 2> /dev/null
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
"${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
@ -86,15 +102,22 @@ printf "LZIP\001+.............................." | "${LZIP}" -t 2> /dev/null
printf "\ntesting decompression..." printf "\ntesting decompression..."
"${LZIP}" -t "${in_lz}" || test_failed $LINENO for i in "${in_lz}" "${in_em}" "${testdir}"/test_sync.lz ; do
"${LZIP}" -cd "${in_lz}" > copy || test_failed $LINENO "${LZIP}" -t "$i" || test_failed $LINENO "$i"
cmp in copy || test_failed $LINENO "${LZIP}" -d "$i" -o copy || test_failed $LINENO "$i"
cmp in copy || test_failed $LINENO "$i"
"${LZIP}" -cd "$i" > copy || test_failed $LINENO "$i"
cmp in copy || test_failed $LINENO "$i"
"${LZIP}" -d "$i" -o - > copy || test_failed $LINENO "$i"
cmp in copy || test_failed $LINENO "$i"
"${LZIP}" -d < "$i" > copy || test_failed $LINENO "$i"
cmp in copy || test_failed $LINENO "$i"
rm -f copy || framework_failure
done
"${LZIP}" -t "${testdir}"/test_sync.lz || test_failed $LINENO lines=$("${LZIP}" -tvv "${in_em}" 2>&1 | wc -l) || test_failed $LINENO
"${LZIP}" -cd "${testdir}"/test_sync.lz > copy || test_failed $LINENO [ "${lines}" -eq 8 ] || test_failed $LINENO "${lines}"
cmp in copy || test_failed $LINENO
rm -f copy || framework_failure
cat "${in_lz}" > copy.lz || framework_failure cat "${in_lz}" > copy.lz || framework_failure
"${LZIP}" -dk copy.lz || test_failed $LINENO "${LZIP}" -dk copy.lz || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
@ -112,12 +135,20 @@ cat "${in_lz}" > copy.lz || framework_failure
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
printf "to be overwritten" > copy || framework_failure printf "to be overwritten" > copy || framework_failure
"${LZIP}" -d -o copy < "${in_lz}" 2> /dev/null
[ $? = 1 ] || test_failed $LINENO
"${LZIP}" -df -o copy < "${in_lz}" || test_failed $LINENO "${LZIP}" -df -o copy < "${in_lz}" || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
rm -f out copy || framework_failure
"${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 cat "${in_lz}" > anyothername || framework_failure
"${LZIP}" -s16 < in > anyothername || test_failed $LINENO "${LZIP}" -dv - anyothername - < "${in_lz}" > copy 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 copy || test_failed $LINENO
cmp in anyothername.out || test_failed $LINENO cmp in anyothername.out || test_failed $LINENO
@ -152,19 +183,18 @@ done
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
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 > copy2 || 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 copy2 || test_failed $LINENO
rm -f copy2 || framework_failure
"${LZIP}" -s16 --output=copy2.lz < in2 || test_failed $LINENO "${LZIP}" -d "${in_lz}" "${in_lz}" -o copy2 || test_failed $LINENO
"${LZIP}" -t copy2.lz || test_failed $LINENO
"${LZIP}" -cd copy2.lz > copy2 || test_failed $LINENO
cmp in2 copy2 || test_failed $LINENO cmp in2 copy2 || test_failed $LINENO
rm -f copy2 || framework_failure
cat "${in_lz}" "${in_lz}" > copy2.lz || framework_failure
printf "\ngarbage" >> copy2.lz || framework_failure printf "\ngarbage" >> copy2.lz || framework_failure
"${LZIP}" -tvvvv copy2.lz 2> /dev/null || test_failed $LINENO "${LZIP}" -tvvvv copy2.lz 2> /dev/null || test_failed $LINENO
rm -f copy2 || framework_failure
"${LZIP}" -atq copy2.lz "${LZIP}" -atq copy2.lz
[ $? = 2 ] || test_failed $LINENO [ $? = 2 ] || test_failed $LINENO
"${LZIP}" -atq < copy2.lz "${LZIP}" -atq < copy2.lz
@ -178,68 +208,91 @@ rm -f copy2 || framework_failure
printf "to be overwritten" > copy2 || framework_failure printf "to be overwritten" > copy2 || framework_failure
"${LZIP}" -df copy2.lz || test_failed $LINENO "${LZIP}" -df copy2.lz || test_failed $LINENO
cmp in2 copy2 || test_failed $LINENO cmp in2 copy2 || test_failed $LINENO
rm -f in2 copy2 || framework_failure rm -f copy2 || framework_failure
printf "\ntesting compression..." printf "\ntesting compression..."
"${LZIP}" -c -0 in in in -S100k -o out3.lz > copy2.lz || test_failed $LINENO
[ ! -e out3.lz ] || test_failed $LINENO # override -o and -S
"${LZIP}" -0f in in --output=copy2.lz || test_failed $LINENO
"${LZIP}" -d copy2.lz -o out2 || test_failed $LINENO
cmp in2 out2 || test_failed $LINENO
rm -f out2 copy2.lz || framework_failure
"${LZIP}" -cf "${in_lz}" > out 2> /dev/null # /dev/null is a tty on OS/2 "${LZIP}" -cf "${in_lz}" > out 2> /dev/null # /dev/null is a tty on OS/2
[ $? = 1 ] || test_failed $LINENO [ $? = 1 ] || test_failed $LINENO
"${LZIP}" -cFvvm36 -s16 "${in_lz}" > out 2> /dev/null || test_failed $LINENO "${LZIP}" -Fvvm36 -o - -s16 "${in_lz}" > out 2> /dev/null || test_failed $LINENO
"${LZIP}" -cd out | "${LZIP}" -d > copy || test_failed $LINENO "${LZIP}" -cd out | "${LZIP}" -d > copy || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
"${LZIP}" -0 -o ./- in || test_failed $LINENO
"${LZIP}" -cd ./- | cmp in - || test_failed $LINENO
rm -f ./- || framework_failure
"${LZIP}" -0 -o ./- < in || test_failed $LINENO # don't add .lz
[ ! -e ./-.lz ] || test_failed $LINENO
"${LZIP}" -cd ./- | cmp in - || test_failed $LINENO
rm -f ./- || framework_failure
for i in s4Ki 0 1 2 3 4 5 6 7 8 9 ; do for i in s4Ki 0 1 2 3 4 5 6 7 8 9 ; do
"${LZIP}" -k -$i -s16 in || test_failed $LINENO $i "${LZIP}" -k -$i -s16 in || test_failed $LINENO $i
mv -f in.lz copy.lz || test_failed $LINENO $i mv -f in.lz copy.lz || test_failed $LINENO $i
printf "garbage" >> copy.lz || framework_failure printf "garbage" >> copy.lz || framework_failure
"${LZIP}" -df copy.lz || test_failed $LINENO $i "${LZIP}" -df copy.lz || test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in copy || test_failed $LINENO $i
done
for i in s4Ki 0 1 2 3 4 5 6 7 8 9 ; do "${LZIP}" -$i -s16 in -c > out || test_failed $LINENO $i
"${LZIP}" -c -$i -s16 in > out || test_failed $LINENO $i "${LZIP}" -$i -s16 in -o o_out || test_failed $LINENO $i # don't add .lz
[ ! -e o_out.lz ] || test_failed $LINENO
cmp out o_out || test_failed $LINENO $i
rm -f o_out || framework_failure
printf "g" >> out || framework_failure printf "g" >> out || framework_failure
"${LZIP}" -cd out > copy || test_failed $LINENO $i "${LZIP}" -cd out > copy || test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in copy || test_failed $LINENO $i
done
for i in s4Ki 0 1 2 3 4 5 6 7 8 9 ; do
"${LZIP}" -$i -s16 < in > out || test_failed $LINENO $i "${LZIP}" -$i -s16 < in > out || test_failed $LINENO $i
"${LZIP}" -d < out > copy || test_failed $LINENO $i "${LZIP}" -d < out > copy || test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in copy || test_failed $LINENO $i
done
for i in s4Ki 0 1 2 3 4 5 6 7 8 9 ; do rm -f out.lz || framework_failure
printf "to be overwritten" > out || framework_failure # don't add .lz
"${LZIP}" -f -$i -s16 -o out < in || test_failed $LINENO $i "${LZIP}" -f -$i -s16 -o out < in || test_failed $LINENO $i
"${LZIP}" -df -o copy < out.lz || test_failed $LINENO $i [ ! -e out.lz ] || test_failed $LINENO
"${LZIP}" -df -o copy < out || test_failed $LINENO $i
cmp in copy || test_failed $LINENO $i cmp in copy || test_failed $LINENO $i
done done
rm -f out.lz || framework_failure rm -f out out.lz || framework_failure
cat in in in in in in in in > in8 || framework_failure cat in in in in in in in in > in8 || framework_failure
"${LZIP}" -1s12 -S100k in8 || test_failed $LINENO "${LZIP}" -1s12 -S100k in8 || test_failed $LINENO
"${LZIP}" -t in800001.lz in800002.lz || test_failed $LINENO "${LZIP}" -t in800001.lz in800002.lz || test_failed $LINENO
"${LZIP}" -cd in800001.lz in800002.lz | cmp in8 - || test_failed $LINENO "${LZIP}" -cd in800001.lz in800002.lz | cmp in8 - || test_failed $LINENO
[ ! -e in800003.lz ] || test_failed $LINENO
rm -f in800001.lz in800002.lz || framework_failure rm -f in800001.lz in800002.lz || framework_failure
"${LZIP}" -1s12 -S100k -o out.lz < in8 || test_failed $LINENO "${LZIP}" -1s12 -S100k -o out.lz in8 || test_failed $LINENO
# ignore -S
"${LZIP}" -d out.lz00001.lz out.lz00002.lz -S100k -o out || test_failed $LINENO
cmp in8 out || test_failed $LINENO
"${LZIP}" -t out.lz00001.lz out.lz00002.lz || test_failed $LINENO "${LZIP}" -t out.lz00001.lz out.lz00002.lz || test_failed $LINENO
"${LZIP}" -cd out.lz00001.lz out.lz00002.lz | cmp in8 - || test_failed $LINENO [ ! -e out.lz00003.lz ] || test_failed $LINENO
rm -f out.lz00001.lz out.lz00002.lz || framework_failure rm -f out out.lz00001.lz out.lz00002.lz || framework_failure
"${LZIP}" -1ks4Ki -b100000 in8 || test_failed $LINENO "${LZIP}" -1ks4Ki -b100000 in8 || test_failed $LINENO
"${LZIP}" -t in8.lz || test_failed $LINENO "${LZIP}" -t in8.lz || test_failed $LINENO
"${LZIP}" -cd in8.lz | cmp in8 - || test_failed $LINENO "${LZIP}" -cd in8.lz -o out | cmp in8 - || test_failed $LINENO # override -o
rm -f in8 || framework_failure [ ! -e out ] || test_failed $LINENO
"${LZIP}" -0 -S100k -o out < in8.lz || test_failed $LINENO "${LZIP}" -0 -S100k -o out < in8.lz || test_failed $LINENO
"${LZIP}" -t out00001.lz out00002.lz || test_failed $LINENO "${LZIP}" -t out00001.lz out00002.lz || test_failed $LINENO
"${LZIP}" -cd out00001.lz out00002.lz | cmp in8.lz - || test_failed $LINENO "${LZIP}" -cd out00001.lz out00002.lz | cmp in8.lz - || test_failed $LINENO
[ ! -e out00003.lz ] || test_failed $LINENO
rm -f out00001.lz || framework_failure rm -f out00001.lz || framework_failure
"${LZIP}" -1 -S100k -o out < in8.lz || test_failed $LINENO "${LZIP}" -1 -S100k -o out < in8.lz || test_failed $LINENO
"${LZIP}" -t out00001.lz out00002.lz || test_failed $LINENO "${LZIP}" -t out00001.lz out00002.lz || test_failed $LINENO
"${LZIP}" -cd out00001.lz out00002.lz | cmp in8.lz - || test_failed $LINENO "${LZIP}" -cd out00001.lz out00002.lz | cmp in8.lz - || test_failed $LINENO
[ ! -e out00003.lz ] || test_failed $LINENO
rm -f out00001.lz out00002.lz || framework_failure rm -f out00001.lz out00002.lz || framework_failure
"${LZIP}" -0 -F -S100k in8.lz || test_failed $LINENO "${LZIP}" -0 -F -S100k in8.lz || test_failed $LINENO
"${LZIP}" -t in8.lz00001.lz in8.lz00002.lz || test_failed $LINENO "${LZIP}" -t in8.lz00001.lz in8.lz00002.lz || test_failed $LINENO
"${LZIP}" -cd in8.lz00001.lz in8.lz00002.lz | cmp in8.lz - || test_failed $LINENO "${LZIP}" -cd in8.lz00001.lz in8.lz00002.lz | cmp in8.lz - || test_failed $LINENO
[ ! -e in8.lz00003.lz ] || test_failed $LINENO
rm -f in8.lz00001.lz in8.lz00002.lz || framework_failure rm -f in8.lz00001.lz in8.lz00002.lz || framework_failure
"${LZIP}" -0kF -b100k in8.lz || test_failed $LINENO "${LZIP}" -0kF -b100k in8.lz || test_failed $LINENO
"${LZIP}" -t in8.lz.lz || test_failed $LINENO "${LZIP}" -t in8.lz.lz || test_failed $LINENO
@ -247,12 +300,33 @@ rm -f in8.lz00001.lz in8.lz00002.lz || framework_failure
rm -f in8.lz in8.lz.lz || framework_failure rm -f in8.lz in8.lz.lz || framework_failure
"${BBEXAMPLE}" in || test_failed $LINENO "${BBEXAMPLE}" in || test_failed $LINENO
"${BBEXAMPLE}" out || test_failed $LINENO "${BBEXAMPLE}" "${in_lz}" || test_failed $LINENO
"${BBEXAMPLE}" ${test2} || test_failed $LINENO "${BBEXAMPLE}" "${fox_lf}" || test_failed $LINENO
"${FFEXAMPLE}" -h > /dev/null || test_failed $LINENO
"${FFEXAMPLE}" > /dev/null && test_failed $LINENO
rm -f out || framework_failure
"${FFEXAMPLE}" -b in out || test_failed $LINENO
cmp in out || test_failed $LINENO
"${FFEXAMPLE}" -b in | cmp in - || test_failed $LINENO
"${FFEXAMPLE}" -b in8 | cmp in8 - || test_failed $LINENO
"${FFEXAMPLE}" -b "${fox_lf}" | cmp "${fox_lf}" - || test_failed $LINENO
"${FFEXAMPLE}" -d "${in_lz}" - | cmp in - || test_failed $LINENO
"${FFEXAMPLE}" -d "${in_em}" - | cmp in - || test_failed $LINENO
"${FFEXAMPLE}" -c in | "${FFEXAMPLE}" -d | cmp in - || test_failed $LINENO
"${FFEXAMPLE}" -m in | "${FFEXAMPLE}" -d | cmp in - || test_failed $LINENO
"${FFEXAMPLE}" -l in | "${FFEXAMPLE}" -d | cmp in - || test_failed $LINENO
cat "${fox_lf}" "${in_lz}" | "${FFEXAMPLE}" -r | cmp in - || test_failed $LINENO
cat in8 "${in_lz}" | "${FFEXAMPLE}" -r | cmp in - || test_failed $LINENO
cat "${in_lz}" "${fox_lf}" "${in_lz}" | "${FFEXAMPLE}" -r - | cmp in2 - ||
test_failed $LINENO
cat "${in_lz}" in8 "${in_lz}" | "${FFEXAMPLE}" -r - - | cmp in2 - ||
test_failed $LINENO
"${LZCHECK}" in || test_failed $LINENO "${LZCHECK}" in || test_failed $LINENO
"${LZCHECK}" out || test_failed $LINENO "${LZCHECK}" "${in_lz}" || test_failed $LINENO
"${LZCHECK}" ${test2} || test_failed $LINENO "${LZCHECK}" "${fox_lf}" || test_failed $LINENO
rm -f in8 || framework_failure
printf "\ntesting bad input..." printf "\ntesting bad input..."
@ -301,6 +375,21 @@ else
fi fi
rm -f int.lz || framework_failure rm -f int.lz || framework_failure
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
"${LZIP}" -cd "${fox_lz}" > fox || test_failed $LINENO
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 out || 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=14752 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 > /dev/null 2>&1 ; then
@ -323,13 +412,23 @@ rm -f in2.lz in3.lz trunc.lz out || framework_failure
cat "${in_lz}" > ingin.lz || framework_failure cat "${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}" -atq ingin.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -atq < ingin.lz
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -acdq ingin.lz > out
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -adq < ingin.lz > out
[ $? = 2 ] || test_failed $LINENO
"${LZIP}" -t ingin.lz || test_failed $LINENO "${LZIP}" -t ingin.lz || test_failed $LINENO
"${LZIP}" -t < ingin.lz || test_failed $LINENO
"${LZIP}" -cd ingin.lz > copy || test_failed $LINENO "${LZIP}" -cd ingin.lz > copy || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
"${LZIP}" -t < ingin.lz || test_failed $LINENO
"${LZIP}" -d < ingin.lz > copy || test_failed $LINENO "${LZIP}" -d < ingin.lz > copy || test_failed $LINENO
cmp in copy || test_failed $LINENO cmp in copy || test_failed $LINENO
rm -f copy ingin.lz || framework_failure "${FFEXAMPLE}" -d ingin.lz | cmp in - || test_failed $LINENO
"${FFEXAMPLE}" -r ingin.lz | cmp in2 - || test_failed $LINENO
rm -f copy ingin.lz in2 out || 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_s11.lz Normal file

Binary file not shown.

BIN
testsuite/fox_v2.lz Normal file

Binary file not shown.

BIN
testsuite/test_em.txt.lz Normal file

Binary file not shown.