This manual is for Lzlib (version 1.14, 20 January 2024).
Copyright © 2009-2024 Antonio Diaz Diaz.
This manual is free documentation: you have unlimited permission to copy, distribute, and modify it.
Lzlib is a data compression library providing in-memory LZMA compression and decompression functions, including integrity checking of the decompressed data. The compressed data format used by the library is the lzip format. Lzlib is written in C.
The lzip file format is designed for data sharing and long-term archiving, taking into account both data integrity and decoder availability:
A nice feature of the lzip format is that a corrupt byte is easier to repair the nearer it is from the beginning of the file. Therefore, with the help of lziprecover, losing an entire archive just because of a corrupt byte near the beginning is a thing of the past.
The functions and variables forming the interface of the compression library are declared in the file 'lzlib.h'. Usage examples of the library are given in the files 'bbexample.c', 'ffexample.c', and 'minilzip.c' from the source distribution.
As 'lzlib.h' can be used by C and C++ programs, it must not impose a choice of system headers on the program by including one of them. Therefore it is the responsibility of the program using lzlib to include before 'lzlib.h' some header that declares the type 'uint8_t'. There are at least four such headers in C and C++: 'stdint.h', 'cstdint', 'inttypes.h', and 'cinttypes'.
All the library functions are thread safe. The library does not install any signal handler. The decoder checks the consistency of the compressed data, so the library should never crash even in case of corrupted input.
Compression/decompression is done by repeatedly calling a couple of read/write functions until all the data have been processed by the library. This interface is safer and less error prone than the traditional zlib interface.
Compression/decompression is done when the read function is called. This means the value returned by the position functions is not updated until a read call, even if a lot of data are written. If you want the data to be compressed in advance, just call the read function with a size equal to 0.
If all the data to be compressed are written in advance, lzlib automatically adjusts the header of the compressed data to use the largest dictionary size that does not exceed neither the data size nor the limit given to 'LZ_compress_open'. This feature reduces the amount of memory needed for decompression and allows minilzip to produce identical compressed output as lzip.
Lzlib correctly decompresses a data stream which is the concatenation of two or more compressed data streams. The result is the concatenation of the corresponding decompressed data streams. Integrity testing of concatenated compressed data streams is also supported.
Lzlib is able to compress and decompress streams of unlimited size by automatically creating multimember output. The members so created are large, about 2 PiB each.
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 scheme". For example, the option -0 of lzip uses the scheme in almost the simplest way possible; issuing the longest match it can find, or a literal byte if it can't find a match. Inversely, a much more elaborated way of finding coding sequences of minimum size than the one currently used by lzip could be developed, and the resulting sequence could also be coded using the LZMA coding scheme.
Lzlib currently implements two variants of the LZMA algorithm: fast (used by option -0 of minilzip) and normal (used by all other compression levels).
The high compression of LZMA comes from combining two basic, well-proven compression ideas: sliding dictionaries (LZ77) and markov models (the thing used by every compression algorithm that uses a range encoder or similar order-0 entropy coder as its last stage) with segregation of contexts according to what the bits are used for.
The ideas embodied in lzlib are due to (at least) the following people: Abraham Lempel and Jacob Ziv (for the LZ algorithm), Andrei 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).
LANGUAGE NOTE: Uncompressed = not compressed = plain data; it may never have been compressed. Decompressed is used to refer to data which have undergone the process of decompression.
One goal of lzlib is to keep perfect backward compatibility with older versions of itself down to 1.0. Any application working with an older lzlib should work with a newer lzlib. Installing a newer lzlib should not break anything. This chapter describes the constants and functions that the application can use to discover the version of the library being used. All of them are declared in 'lzlib.h'.
This constant is defined in 'lzlib.h' and works as a version test macro. The application should check at compile time that LZ_API_VERSION is greater than or equal to the version required by the application:
#if !defined LZ_API_VERSION || LZ_API_VERSION < 1012 #error "lzlib 1.12 or newer needed." #endifBefore version 1.8, lzlib didn't define LZ_API_VERSION.
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).
NOTE: Version test macros are the library's way of announcing functionality to the application. They should not be confused with feature test macros, which allow the application to announce to the library its desire to have certain symbols and prototypes exposed.
If LZ_API_VERSION >= 1012, this function is declared in 'lzlib.h' (else it doesn't exist). It returns the LZ_API_VERSION of the library object code being used. The application should check at run time that the value returned by
LZ_api_version
is greater than or equal to the version required by the application. An application may be dynamically linked at run time with a different version of lzlib than the one it was compiled for, and this should not break the application as long as the library used provides the functionality required by the application.#if defined LZ_API_VERSION && LZ_API_VERSION >= 1012 if( LZ_api_version() < 1012 ) show_error( "lzlib 1.12 or newer needed." ); #endif
This string constant is defined in the header file 'lzlib.h' and represents the version of the library being used at compile time.
This function returns a string representing the version of the library being used at run time.
Lzlib internal functions need access to a memory chunk at least as large as the dictionary size (sliding window). For efficiency reasons, the input buffer for compression is twice or sixteen times as large as the dictionary size.
Finally, for safety reasons, lzlib uses two more internal buffers.
These are the four buffers used by lzlib, and their guaranteed minimum sizes:
These functions provide minimum and maximum values for some parameters. Current values are shown in square brackets.
Returns the base 2 logarithm of the smallest valid dictionary size [12].
Returns the smallest valid dictionary size [4 KiB].
Returns the base 2 logarithm of the largest valid dictionary size [29].
Returns the largest valid dictionary size [512 MiB].
Returns the largest valid match length limit [273].
These are the functions used to compress data. In case of error, all of them return -1 or 0, for signed and unsigned return values respectively, except 'LZ_compress_open' whose return value must be checked by calling 'LZ_compress_errno' before using it.
Initializes the internal stream state for compression and returns a pointer that can only be used as the encoder argument for the other LZ_compress functions, or a null pointer if the encoder could not be allocated.
The returned pointer must be checked by calling 'LZ_compress_errno' before using it. If 'LZ_compress_errno' does not return 'LZ_ok', the returned pointer must not be used and should be freed with 'LZ_compress_close' to avoid memory leaks.
dictionary_size sets the dictionary size to be used, in bytes. Valid values range from 4 KiB to 512 MiB. Note that dictionary sizes are quantized. If the size specified does not match one of the valid sizes, it is rounded upwards by adding up to (dictionary_size / 8) to it.
match_len_limit sets the match length limit in bytes. Valid values range from 5 to 273. Larger values usually give better compression ratios but longer compression times.
If dictionary_size is 65535 and match_len_limit is 16, the fast variant of LZMA is chosen, which produces identical compressed output as 'lzip -0'. (The dictionary size used is rounded upwards to 64 KiB).
member_size sets the member size limit in bytes. Valid values range from 4 KiB to 2 PiB. A small member size may degrade compression ratio, so use it only when needed. To produce a single-member data stream, give member_size a value larger than the amount of data to be produced. Values larger than 2 PiB are reduced to 2 PiB to prevent the uncompressed size of the member from overflowing.
Frees all dynamically allocated data structures for this stream. This function discards any unprocessed input and does not flush any pending output. After a call to 'LZ_compress_close', encoder can no longer be used as an argument to any LZ_compress function. It is safe to call 'LZ_compress_close' with a null argument.
Use this function to tell 'lzlib' that all the data for this member have already been written (with the function 'LZ_compress_write'). It is safe to call 'LZ_compress_finish' as many times as needed. After all the compressed data have been read with 'LZ_compress_read' and 'LZ_compress_member_finished' returns 1, a new member can be started with 'LZ_compress_restart_member'.
Use this function to start a new member in a multimember data stream. Call this function only after 'LZ_compress_member_finished' indicates that the current member has been fully read (with the function 'LZ_compress_read'). See member_size, for a description of member_size.
Use this function to make available to 'LZ_compress_read' all the data already written with the function 'LZ_compress_write'. First call 'LZ_compress_sync_flush'. Then call 'LZ_compress_read' until it returns 0.
This function writes at least one LZMA marker '3' ("Sync Flush" marker) to the compressed output. Note that the sync flush marker is not allowed in lzip files; it 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. See Data format.
Repeated use of 'LZ_compress_sync_flush' may degrade compression ratio, so use it only when needed. If the interval between calls to 'LZ_compress_sync_flush' is large (comparable to dictionary size), creating a multimember data stream with 'LZ_compress_restart_member' may be an alternative.
Combining multimember stream creation with flushing may be tricky. If there are more bytes available than those needed to complete member_size, 'LZ_compress_restart_member' needs to be called when 'LZ_compress_member_finished' returns 1, followed by a new call to 'LZ_compress_sync_flush'.
Reads up to size bytes from the stream pointed to by encoder, storing the results in buffer. If LZ_API_VERSION >= 1012, buffer may be a null pointer, in which case the bytes read are discarded.
Returns the number of bytes actually read. This might be less than size; for example, if there aren't that many bytes left in the stream or if more bytes have to be yet written with the function 'LZ_compress_write'. Note that reading less than size bytes is not an error.
Writes up to size bytes from buffer to the stream pointed to by encoder. Returns the number of bytes actually written. This might be less than size. Note that writing less than size bytes is not an error.
Returns the maximum number of bytes that can be immediately written through 'LZ_compress_write'. For efficiency reasons, once the input buffer is full and 'LZ_compress_write_size' returns 0, almost all the buffer must be compressed before a size greater than 0 is returned again. (This is done to minimize the amount of data that must be copied to the beginning of the buffer before new data can be accepted).
It is guaranteed that an immediate call to 'LZ_compress_write' will accept a size up to the returned number of bytes.
Returns the current error code for encoder. See Error codes. It is safe to call 'LZ_compress_errno' with a null argument, in which case it returns 'LZ_bad_argument'.
Returns 1 if all the data have been read and 'LZ_compress_close' can be safely called. Otherwise it returns 0. 'LZ_compress_finished' implies 'LZ_compress_member_finished'.
Returns 1 if the current member, in a multimember data stream, has been fully read and 'LZ_compress_restart_member' can be safely called. Otherwise it returns 0.
Returns the number of input bytes already compressed in the current member.
Returns the number of compressed bytes already produced, but perhaps not yet read, in the current member.
Returns the total number of input bytes already compressed.
Returns the total number of compressed bytes already produced, but perhaps not yet read.
These are the functions used to decompress data. In case of error, all of them return -1 or 0, for signed and unsigned return values respectively, except 'LZ_decompress_open' whose return value must be checked by calling 'LZ_decompress_errno' before using it.
Initializes the internal stream state for decompression and returns a pointer that can only be used as the decoder argument for the other LZ_decompress functions, or a null pointer if the decoder could not be allocated.
The returned pointer must be checked by calling 'LZ_decompress_errno' before using it. If 'LZ_decompress_errno' does not return 'LZ_ok', the returned pointer must not be used and should be freed with 'LZ_decompress_close' to avoid memory leaks.
Frees all dynamically allocated data structures for this stream. This function discards any unprocessed input and does not flush any pending output. After a call to 'LZ_decompress_close', decoder can no longer be used as an argument to any LZ_decompress function. It is safe to call 'LZ_decompress_close' with a null argument.
Use this function to tell 'lzlib' that all the data for this stream have already been written (with the function 'LZ_decompress_write'). It is safe to call 'LZ_decompress_finish' as many times as needed. It is not required to call 'LZ_decompress_finish' if the input stream only contains whole members, but not calling it prevents lzlib from detecting a truncated member.
Resets the internal state of decoder as it was just after opening it with the function 'LZ_decompress_open'. Data stored in the internal buffers is discarded. Position counters are set to 0.
Resets the error state of decoder and enters a search state that lasts until a new member header (or the end of the stream) is found. After a successful call to 'LZ_decompress_sync_to_member', data written with 'LZ_decompress_write' is consumed and 'LZ_decompress_read' returns 0 until a header is found.
This function is useful to discard any data preceding the first member, or to discard the rest of the current member, for example in case of a data error. If the decoder is already at the beginning of a member, this function does nothing.
Reads up to size bytes from the stream pointed to by decoder, storing the results in buffer. If LZ_API_VERSION >= 1012, buffer may be a null pointer, in which case the bytes read are discarded.
Returns the number of bytes actually read. This might be less than size; for example, if there aren't that many bytes left in the stream or if more bytes have to be yet written with the function 'LZ_decompress_write'. Note that reading less than size bytes is not an error.
'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. The increase in the value returned by 'LZ_decompress_total_in_size' can be used to tell the end of the stream from an empty member.
In case of decompression error caused by corrupt or truncated data, 'LZ_decompress_read' does not signal the error immediately to the application, but waits until all the bytes decoded have been read. This allows tools like tarlz to recover as much data as possible from each damaged member.
Writes up to size bytes from buffer to the stream pointed to by decoder. Returns the number of bytes actually written. This might be less than size. Note that writing less than size bytes is not an error.
Returns the maximum number of bytes that can be immediately written through 'LZ_decompress_write'. This number varies smoothly; each compressed byte consumed may be overwritten immediately, increasing by 1 the value returned.
It is guaranteed that an immediate call to 'LZ_decompress_write' will accept a size up to the returned number of bytes.
Returns the current error code for decoder. See Error codes. It is safe to call 'LZ_decompress_errno' with a null argument, in which case it returns 'LZ_bad_argument'.
Returns 1 if all the data have been read and 'LZ_decompress_close' can be safely called. Otherwise it returns 0. 'LZ_decompress_finished' does not imply 'LZ_decompress_member_finished'.
Returns 1 if the previous call to 'LZ_decompress_read' finished reading the current member, indicating that final values for the member are available through 'LZ_decompress_data_crc', 'LZ_decompress_data_position', and 'LZ_decompress_member_position'. Otherwise it returns 0.
Returns the version of the current member, read from the member header.
Returns the dictionary size of the current member, read from the member header.
Returns the 32 bit Cyclic Redundancy Check of the data decompressed from the current member. The value returned is valid only when 'LZ_decompress_member_finished' returns 1.
Returns the number of decompressed bytes already produced, but perhaps not yet read, in the current member.
Returns the number of input bytes already decompressed in the current member.
Returns the total number of input bytes already decompressed.
Returns the total number of decompressed bytes already produced, but perhaps not yet read.
Most library functions return -1 to indicate that they have failed. But this return value only tells you that an error has occurred. To find out what kind of error it was, you need to check the error code by calling 'LZ_(de)compress_errno'.
Library functions don't change the value returned by 'LZ_(de)compress_errno' when they succeed; thus, the value returned by 'LZ_(de)compress_errno' after a successful call is not necessarily LZ_ok, and you should not use 'LZ_(de)compress_errno' to determine whether a call failed. If the call failed, then you can examine 'LZ_(de)compress_errno'.
The error codes are defined in the header file 'lzlib.h'.
The value of this constant is 0 and is used to indicate that there is no error.
At least one of the arguments passed to the library function was invalid.
No memory available. The system cannot allocate more virtual memory because its capacity is full.
A library function was called in the wrong order. For example 'LZ_compress_restart_member' was called before 'LZ_compress_member_finished' indicates that the current member is finished.
An invalid member header (one with the wrong magic bytes) was read. If this happens at the end of the data stream it may indicate trailing data.
The end of the data stream was reached in the middle of a member.
The data stream is corrupt. If 'LZ_decompress_member_position' is 6 or less, it indicates either a format version not supported, an invalid dictionary size, a corrupt header in a multimember data stream, or trailing data too similar to a valid lzip header. Lziprecover can be used to remove conflicting trailing data from a file.
A bug was detected in the library. Please, report it. See Problems.
Returns the standard error message for a given error code. The messages are fairly short; there are no multi-line messages or embedded newlines. This function makes it easy for your program to report informative error messages about the failure of a library call.
The value of lz_errno normally comes from a call to 'LZ_(de)compress_errno'.
Minilzip is a test program for the compression library lzlib, compatible with lzip 1.4 or newer.
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 to maximize interoperability. The maximum dictionary size is 512 MiB so that any lzip file can be decompressed on 32-bit machines. Lzip provides accurate and robust 3-factor integrity checking. Lzip can compress about as fast as gzip (lzip -0) or compress most files more than bzip2 (lzip -9). 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.
The format for running minilzip is:
minilzip [options] [files]
If no file names are specified, minilzip compresses (or decompresses) from standard input to standard output. A hyphen '-' used as a file argument means standard input. It can be mixed with other files and is read just once, the first time it appears in the command line. Remember to prepend ./ to any file name beginning with a hyphen, or use '--'.
minilzip supports the following options:
-h
--help
-V
--version
-a
--trailing-error
-b
bytes--member-size=
bytes-c
--stdout
-d
--decompress
-f
--force
-F
--recompress
-k
--keep
-m
bytes--match-length=
bytes-o
file--output=
fileWhen compressing and splitting the output in volumes, file is used as
a prefix, and several files named 'file00001.lz',
'file00002.lz', etc, are created. In this case, only one input
file is allowed.
-q
--quiet
-s
bytes--dictionary-size=
bytesFor maximum compression you should use a dictionary size limit as large
as possible, but keep in mind that the decompression memory requirement
is affected at compression time by the choice of dictionary size limit.
-S
bytes--volume-size=
bytes-t
--test
-v
--verbose
-0 .. -9
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, etc, you may need to use the options --dictionary-size and --match-length directly to achieve optimal performance.
If several compression levels or -s or -m options are given, the last setting is used. For example -9 -s64MiB is equivalent to -s64MiB -m273
Level | Dictionary size (-s) | Match length limit (-m)
|
-0 | 64 KiB | 16 bytes
|
-1 | 1 MiB | 5 bytes
|
-2 | 1.5 MiB | 6 bytes
|
-3 | 2 MiB | 8 bytes
|
-4 | 3 MiB | 12 bytes
|
-5 | 4 MiB | 20 bytes
|
-6 | 8 MiB | 36 bytes
|
-7 | 16 MiB | 68 bytes
|
-8 | 24 MiB | 132 bytes
|
-9 | 32 MiB | 273 bytes
|
--fast
--best
--loose-trailing
--check-lib
Numbers given as arguments to options may be expressed in decimal, hexadecimal, or octal (using the same syntax as integer constants in C++), and may be followed by a multiplier and an optional 'B' for "byte".
Table of SI and binary prefixes (unit multipliers):
Prefix | Value | | | Prefix | Value
|
k | kilobyte (10^3 = 1000) | | | Ki | kibibyte (2^10 = 1024)
|
M | megabyte (10^6) | | | Mi | mebibyte (2^20)
|
G | gigabyte (10^9) | | | Gi | gibibyte (2^30)
|
T | terabyte (10^12) | | | Ti | tebibyte (2^40)
|
P | petabyte (10^15) | | | Pi | pebibyte (2^50)
|
E | exabyte (10^18) | | | Ei | exbibyte (2^60)
|
Z | zettabyte (10^21) | | | Zi | zebibyte (2^70)
|
Y | yottabyte (10^24) | | | Yi | yobibyte (2^80)
|
R | ronnabyte (10^27) | | | Ri | robibyte (2^90)
|
Q | quettabyte (10^30) | | | Qi | quebibyte (2^100)
|
Exit status: 0 for a normal exit, 1 for environmental problems (file not found, invalid command-line options, I/O errors, etc), 2 to indicate a corrupt or invalid input file, 3 for an internal consistency error (e.g., bug) which caused minilzip to panic.
Perfection is reached, not when there is no longer anything to add, but
when there is no longer anything to take away.
-- Antoine de Saint-Exupery
In the diagram below, a box like this:
+---+ | | <-- the vertical bars might be missing +---+
represents one byte; a box like this:
+==============+ | | +==============+
represents a variable number of bytes.
Lzip data consist of one or more independent "members" (compressed data sets). The members simply appear one after another in the data stream, with no additional information before, between, or after them. Each member can encode in compressed form up to 16 EiB - 1 byte of uncompressed data. The size of a multimember data stream is unlimited.
Each member has the following structure:
+--+--+--+--+----+----+=============+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ID string | VN | DS | LZMA stream | CRC32 | Data size | Member size | +--+--+--+--+----+----+=============+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
All multibyte values are stored in little endian order.
This chapter provides real code examples for the most common uses of the library. See these examples in context in the files 'bbexample.c' and 'ffexample.c' from the source distribution of lzlib.
Note that the interface of lzlib is symmetrical. That is, the code for normal compression and decompression is identical except because one calls LZ_compress* functions while the other calls LZ_decompress* functions.
Buffer-to-buffer single-member compression (member_size > total output).
/* Compress 'insize' bytes from 'inbuf' to 'outbuf'. Return the size of the compressed data in '*outlenp'. In case of error, or if 'outsize' is too small, return false and do 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; }
Buffer-to-buffer decompression.
/* Decompress 'insize' bytes from 'inbuf' to 'outbuf'. Return the size of the decompressed data in '*outlenp'. In case of error, or if 'outsize' is too small, return false and do 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; }
File-to-file compression using LZ_compress_write_size.
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; }
File-to-file decompression using LZ_decompress_write_size.
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; }
Example 1: Multimember compression with members of fixed size (member_size < total output).
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; }
Example 2: Multimember compression (user-restarted members). (Call LZ_compress_open with member_size > largest member).
/* Compress 'infile' to 'outfile' as a multimember stream with one member for each line of text terminated by a newline character or by EOF. Return 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; }
/* Decompress '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; } break; } len = fwrite( buffer, 1, ret, outfile ); if( len < ret ) break; if( LZ_decompress_finished( decoder ) == 1 ) return 0; } return 1; }
There are probably bugs in lzlib. There are certainly errors and omissions in this manual. If you report them, they will get fixed. If you don't, no one will ever know about them and they will remain unfixed for all eternity, if not longer.
If you find a bug in lzlib, please send electronic mail to lzip-bug@nongnu.org. Include the version number, which you can find by running 'minilzip --version' and 'minilzip -v --check-lib'.