A gzip (RFC1951 + RFC1952) compression and decompression library
Oskar Schirmer and felix winkelmann
Provides functions to read and write compressed data using the RFC 1951 (DEFLATE) and RFC 1952 (gzip) compression standards. This extension includes code from Oskar Schirmers excellent z3lib library.
RFC1952 is popularly used in the gzip file format and in compression of replies from web servers. RFC1951 (DEFLATE) is the underlying compression technique which is also used by PKZIP. NOTE: RFC1952 is essentially a file wrapper around a RFC1951 stream. Attempting to use the RFC1952 interfaces on RFC1951 data or vice versa will result in errors. Errors occurring in inflation/deflation procedures will result in a composite condition of the kinds exn and z3.
- z3:encode-init #!key buffer buffer-size tellwhen thrmin thrmax initialgrant preferlongerprocedure
Initializes RFC1951 compression of data into a memory buffer. Various keyword arguments can be supplied to control compression:
- The target buffer for the compressed data.
- maximal size of the compressed data.
- Frequency for code size estimation (0 for none, i.e. full block usage).
- thrmin, thrmax
- Threshold for block close decision.
- For threshold comparison, all but the first slice are asumed to be incremented by this value to roughly compensate RFC1951 block dynamic table size.
- When non-zero, the compressor will try to find a longer match at n+1 and prefer it over a previous match at n.
Returns a z3 handle.
Encode data (a string) into a memory buffer using RFC1951 compression and returns the number of bytes written. If less data has been commpressed than given in the call, invoke z3:encode repeatedly with the remaining data. Returns #f when finished. Each time some compressed data is" available, the one-argument procedure RECEIVER is called with a string containing a chunk of compressed data.
Initialize an in-memory RFC1951 decompression and return a z3 handle for it.
- z3:decode Z3HANDLE RECEIVER BUFFER #!optional LENGTHprocedure
Decode the RFC1951 compressed data in BUFFER (a string) and return the number of bytes decompressed.
- z3:handle? Xprocedure
Returns #t if X is a z3 handle or #f otherwise.
- z3:encode-buffer STRING #!optional START ENDprocedure
Encodes the data in STRING using RFC1951 encoding with optional start- and end-positions. Returns a freshly allocated string that contains the compressed data.
- z3:encode-buffer-to-blob STRING #!optional START ENDprocedure
Like z3:encode-buffer, but returns a freshly-allocated blob containing the compressed data.
- z3:decode-buffer STRING #!optional START ENDprocedure
Decodes the RFC1951 compressed data in STRING and returns the uncompressed data. Optional start- and end-position may be given. STRING may be a string or a blob.
- z3:encode-file FILENO #!key level filename comment ostype extraprocedure
Open a RFC1952 compressed "gzip" file (specified by the file-descriptor in FILENO) for encoded data. The keyword arguments have the following meaning:
- Desired compression-level in the range 1 .. 9, or 0 for selecting the default compression level
- The name of the file to compress, will be included in the gzip-file header
- An arbitrary comment
- Operating system indicator byte (defaults to -1)
- Extra data to be encoded in the header"
Returns a z3 file-handle.
- z3:write-encoded Z3FHANDLE DATA #!optional LENGTHprocedure
Writes DATA (a string) into a RFC1952 (gzip) compressed file, optionally limited in length. Before a file has been encoded completely, this procedure must be called once more with DATA being #f to indicate that the compression process is finished.
- z3:decode-file FILENOprocedure
Returns a z3 file-handle for reading the gzip file designated by the file-descriptor FILENO.
- z3:read-decoded Z3FHANDLE #!optional LENGTHprocedure
Reads a chunk of decoded data from a gzip compressed file. The length of the chunk can be given as an optional argument and defaults to 4096 bytes. Returns a string or the end-of-file object.
- z3:file-handle? Xprocedure
Returns #t if X is a z3 file handle or #f otherwise.
- z3:file-handle-fileno Z3FHANDLEprocedure
Returns the file-descriptor associated with a z3 file-handle.
- z3:open-compressed-input-file FILENAMEprocedure
Opens a RFC1952 compressed "gzip" input file and returns a port that automatically decompresses the data as it is read.
- z3:open-compressed-output-file FILENAME #!key level comment ostype extraprocedure
Creates a RFC1952 compressed "gzip" file and returns an output-port. The keyword arguments have the same meaning as for the z3:encode-file procedure.
- Ported to CHICKEN 5
- Removed optional destination argument to z3:encode-buffer, fixed case when compression exceeds temporary destination buffer
- use bind instead of easyffi [Ivan Raikov]
- add z3:encode-buffer-to-blob, extend z3:decode-buffer to accept blob input [Jim Ursetto]
- fixed dependencies and converted documentation to wiki format
- Fix eof bug in port interface
- Fix minor bug in error handling and clarify documentation (trac tickets 81, 82)
- ported to CHICKEN 4
- Should compile now on Windows with mingw
- Fixed bug in z3:encode-buffer [reported by Mario Domenech Goulart]
- Added whole-buffer encoding/decoding
- Fixed setup script and meta file [Thanks to Peter Busser]
- Fixed bug in z3lib code, related to 64-bit architectures
- Fixed bug in z3:encode
- Initial release
Copyright (c) 2005-2007 Oskar Schirmer. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.