You are looking at historical revision 26429 of this page. It may differ significantly from its current revision.
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.
Raw data interface - RFC1951[procedure] (z3:encode-init #!key buffer buffer-size tellwhen thrmin thrmax initialgrant preferlonger)
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.[procedure] z3:encode Z3HANDLE RECEIVER DATA [LENGTH])
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.[procedure] (z3:decode-init)
Initialize an in-memory RFC1951 decompression and return a z3 handle for it.[procedure] (z3:decode Z3HANDLE RECEIVER BUFFER [LENGTH])
Decode the RFC1951 compressed data in BUFFER (a string) and return the number of bytes decompressed.[procedure] (z3:handle? X)
Returns #t if X is a z3 handle or #f otherwise.
Buffer interface - RFC1951[procedure] (z3:encode-buffer STRING [START [END [DESTINATION]]])
Encodes the data in STRING using RFC1951 encoding with optional start- and end-positions. If DESTINATION is given, then the compressed data will be stored in that argument (which should be a string of sufficient size) and z3:encode-buffer returns the length of the compressed data. If no destination is given then a freshly allocated string is returned that contains the compressed data.[procedure] (z3:encode-buffer-to-blob STRING [START [END]])
Like z3:encode-buffer, but returns a freshly-allocated blob containing the compressed data.[procedure] (z3:decode-buffer STRING [START [END]])
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.
File-system interface - RFC1952[procedure] (z3:encode-file FILENO #!key level filename comment ostype extra)
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.[procedure] (z3:write-encoded Z3FHANDLE DATA [LENGTH])
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.[procedure] (z3:decode-file FILENO)
Returns a z3 file-handle for reading the gzip file designated by the file-descriptor FILENO.[procedure] (z3:read-decoded Z3FHANDLE [LENGTH])
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.[procedure] (z3:file-handle? X)
Returns #t if X is a z3 file handle or #f otherwise.[procedure] (z3:file-handle-fileno Z3FHANDLE)
Returns the file-descriptor associated with a z3 file-handle.
Port interface - RFC1952[procedure] (z3:open-compressed-input-file FILENAME)
Opens a RFC1952 compressed "gzip" input file and returns a port that automatically decompresses the data as it is read.[procedure] (z3:open-compressed-output-file FILENAME #!key level comment ostype extra)
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.
- 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.