| 1 |
'\" |
| 2 |
'\" Copyright (c) 2008 Donal K. Fellows |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH TclZlib 3 8.6 Tcl "Tcl Library Procedures" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
Tcl_ZlibAdler32, Tcl_ZlibCRC32, Tcl_ZlibDeflate, Tcl_ZlibInflate, Tcl_ZlibStreamChecksum, Tcl_ZlibStreamClose, Tcl_ZlibStreamEof, Tcl_ZlibStreamGet, Tcl_ZlibStreamGetCommandName, Tcl_ZlibStreamInit, Tcl_ZlibStreamPut \- compression and decompression functions |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
#include <tcl.h> |
| 16 |
.sp |
| 17 |
int |
| 18 |
\fBTcl_ZlibDeflate\fR(\fIinterp, format, dataObj, level, dictObj\fR) |
| 19 |
.sp |
| 20 |
int |
| 21 |
\fBTcl_ZlibInflate\fR(\fIinterp, format, dataObj, bufferSize, dictObj\fR) |
| 22 |
.sp |
| 23 |
unsigned int |
| 24 |
\fBTcl_ZlibCRC32\fR(\fIinitValue, bytes, length\fR) |
| 25 |
.sp |
| 26 |
unsigned int |
| 27 |
\fBTcl_ZlibAdler32\fR(\fIinitValue, bytes, length\fR) |
| 28 |
.sp |
| 29 |
int |
| 30 |
\fBTcl_ZlibStreamInit\fR(\fIinterp, mode, format, level, dictObj, zshandlePtr\fR) |
| 31 |
.sp |
| 32 |
Tcl_Obj * |
| 33 |
\fBTcl_ZlibStreamGetCommandName\fR(\fIzshandle\fR) |
| 34 |
.sp |
| 35 |
int |
| 36 |
\fBTcl_ZlibStreamEof\fR(\fIzshandle\fR) |
| 37 |
.sp |
| 38 |
int |
| 39 |
\fBTcl_ZlibStreamClose\fR(\fIzshandle\fR) |
| 40 |
.sp |
| 41 |
int |
| 42 |
\fBTcl_ZlibStreamReset\fR(\fIzshandle\fR) |
| 43 |
.sp |
| 44 |
int |
| 45 |
\fBTcl_ZlibStreamChecksum\fR(\fIzshandle\fR) |
| 46 |
.sp |
| 47 |
int |
| 48 |
\fBTcl_ZlibStreamPut\fR(\fIzshandle, dataObj, flush\fR) |
| 49 |
.sp |
| 50 |
int |
| 51 |
\fBTcl_ZlibStreamGet\fR(\fIzshandle, dataObj, count\fR) |
| 52 |
.sp |
| 53 |
\fBTcl_ZlibStreamSetCompressionDictionary\fR(\fIzshandle, compDict\fR) |
| 54 |
.fi |
| 55 |
.SH ARGUMENTS |
| 56 |
.AS Tcl_ZlibStream zshandle in |
| 57 |
.AP Tcl_Interp *interp in |
| 58 |
The interpreter to store resulting compressed or uncompressed data in. Also |
| 59 |
where any error messages are written. For \fBTcl_ZlibStreamInit\fR, this can |
| 60 |
be NULL to create a stream that is not bound to a command. |
| 61 |
.AP int format in |
| 62 |
What format of compressed data to work with. Must be one of |
| 63 |
\fBTCL_ZLIB_FORMAT_ZLIB\fR for zlib-format data, \fBTCL_ZLIB_FORMAT_GZIP\fR |
| 64 |
for gzip-format data, or \fBTCL_ZLIB_FORMAT_RAW\fR for raw compressed data. In |
| 65 |
addition, for decompression only, \fBTCL_ZLIB_FORMAT_AUTO\fR may also be |
| 66 |
chosen which can automatically detect whether the compressed data was in zlib |
| 67 |
or gzip format. |
| 68 |
.AP Tcl_Obj *dataObj in/out |
| 69 |
A byte-array value containing the data to be compressed or decompressed, or |
| 70 |
to which the data extracted from the stream is appended when passed to |
| 71 |
\fBTcl_ZlibStreamGet\fR. |
| 72 |
.AP int level in |
| 73 |
What level of compression to use. Should be a number from 0 to 9 or one of the |
| 74 |
following: \fBTCL_ZLIB_COMPRESS_NONE\fR for no compression, |
| 75 |
\fBTCL_ZLIB_COMPRESS_FAST\fR for fast but inefficient compression, |
| 76 |
\fBTCL_ZLIB_COMPRESS_BEST\fR for slow but maximal compression, or |
| 77 |
\fBTCL_ZLIB_COMPRESS_DEFAULT\fR for the level recommended by the zlib library. |
| 78 |
.AP Tcl_Obj *dictObj in/out |
| 79 |
A dictionary that contains, or which will be updated to contain, a description |
| 80 |
of the gzip header associated with the compressed data. Only useful when the |
| 81 |
\fIformat\fR is \fBTCL_ZLIB_FORMAT_GZIP\fR or \fBTCL_ZLIB_FORMAT_AUTO\fR. If |
| 82 |
a NULL is passed, a default header will be used on compression and the header |
| 83 |
will be ignored (apart from integrity checks) on decompression. See the |
| 84 |
section \fBGZIP OPTIONS DICTIONARY\fR for details about the contents of this |
| 85 |
dictionary. |
| 86 |
.AP "unsigned int" initValue in |
| 87 |
The initial value for the checksum algorithm. |
| 88 |
.AP "int" bufferSize in |
| 89 |
A hint as to what size of buffer is to be used to receive the data. |
| 90 |
Use 0 to use a geric guess based on the input data. |
| 91 |
.AP "unsigned char" *bytes in |
| 92 |
An array of bytes to run the checksum algorithm over, or NULL to get the |
| 93 |
recommended initial value for the checksum algorithm. |
| 94 |
.AP int length in |
| 95 |
The number of bytes in the array. |
| 96 |
.AP int mode in |
| 97 |
What mode to operate the stream in. Should be either |
| 98 |
\fBTCL_ZLIB_STREAM_DEFLATE\fR for a compressing stream or |
| 99 |
\fBTCL_ZLIB_STREAM_INFLATE\fR for a decompressing stream. |
| 100 |
.AP Tcl_ZlibStream *zshandlePtr out |
| 101 |
A pointer to a variable in which to write the abstract token for the stream |
| 102 |
upon successful creation. |
| 103 |
.AP Tcl_ZlibStream zshandle in |
| 104 |
The abstract token for the stream to operate on. |
| 105 |
.AP int flush in |
| 106 |
Whether and how to flush the stream after writing the data to it. Must be one |
| 107 |
of: \fBTCL_ZLIB_NO_FLUSH\fR if no flushing is to be done, \fBTCL_ZLIB_FLUSH\fR |
| 108 |
if the currently compressed data must be made available for access using |
| 109 |
\fBTcl_ZlibStreamGet\fR, \fBTCL_ZLIB_FULLFLUSH\fR if the stream must be put |
| 110 |
into a state where the decompressor can recover from on corruption, or |
| 111 |
\fBTCL_ZLIB_FINALIZE\fR to ensure that the stream is finished and that any |
| 112 |
trailer demanded by the format is written. |
| 113 |
.AP int count in |
| 114 |
The maximum number of bytes to get from the stream, or -1 to get all remaining |
| 115 |
bytes from the stream's buffers. |
| 116 |
.AP Tcl_Obj *compDict in |
| 117 |
A byte array value that is the compression dictionary to use with the stream. |
| 118 |
Note that this is \fInot a Tcl dictionary\fR, and it is recommended that this |
| 119 |
only ever be used with streams that were created with their \fIformat\fR set |
| 120 |
to \fBTCL_ZLIB_FORMAT_ZLIB\fR because the other formats have no mechanism to |
| 121 |
indicate whether a compression dictionary was present other than to fail on |
| 122 |
decompression. |
| 123 |
.BE |
| 124 |
.SH DESCRIPTION |
| 125 |
These functions form the interface from the Tcl library to the Zlib |
| 126 |
library by Jean-loup Gailly and Mark Adler. |
| 127 |
.PP |
| 128 |
\fBTcl_ZlibDeflate\fR and \fBTcl_ZlibInflate\fR respectively compress and |
| 129 |
decompress the data contained in the \fIdataObj\fR argument, according to the |
| 130 |
\fIformat\fR and, for compression, \fIlevel\fR arguments. The dictionary in |
| 131 |
the \fIdictObj\fR parameter is used to convey additional header information |
| 132 |
about the compressed data when the compression format supports it; currently, |
| 133 |
the dictionary is only used when the \fIformat\fR parameter is |
| 134 |
\fBTCL_ZLIB_FORMAT_GZIP\fR or \fBTCL_ZLIB_FORMAT_AUTO\fR. For details of the |
| 135 |
contents of the dictionary, see the \fBGZIP OPTIONS DICTIONARY\fR section |
| 136 |
below. Upon success, both functions leave the resulting compressed or |
| 137 |
decompressed data in a byte-array value that is the Tcl interpreter's result; |
| 138 |
the returned value is a standard Tcl result code. |
| 139 |
.PP |
| 140 |
\fBTcl_ZlibAdler32\fR and \fBTcl_ZlibCRC32\fR compute checksums on arrays of |
| 141 |
bytes, returning the computed checksum. Checksums are computed incrementally, |
| 142 |
allowing data to be processed one block at a time, but this requires the |
| 143 |
caller to maintain the current checksum and pass it in as the \fIinitValue\fR |
| 144 |
parameter; the initial value to use for this can be obtained by using NULL for |
| 145 |
the \fIbytes\fR parameter instead of a pointer to the array of bytes to |
| 146 |
compute the checksum over. Thus, typical usage in the single data block case |
| 147 |
is like this: |
| 148 |
.PP |
| 149 |
.CS |
| 150 |
checksum = \fBTcl_ZlibCRC32\fR(\fBTcl_ZlibCRC32\fR(0,NULL,0), data, length); |
| 151 |
.CE |
| 152 |
.PP |
| 153 |
Note that the Adler-32 algorithm is not a real checksum, but instead is a |
| 154 |
related type of hash that works best on longer data. |
| 155 |
.SS "ZLIB STREAMS" |
| 156 |
.PP |
| 157 |
\fBTcl_ZlibStreamInit\fR creates a compressing or decompressing stream that is |
| 158 |
linked to a Tcl command, according to its arguments, and provides an abstract |
| 159 |
token for the stream and returns a normal Tcl result code; |
| 160 |
\fBTcl_ZlibStreamGetCommandName\fR returns the name of that command given the |
| 161 |
stream token, or NULL if the stream has no command. Streams are not designed |
| 162 |
to be thread-safe; each stream should only ever be used from the thread that |
| 163 |
created it. When working with gzip streams, a dictionary (fields as given in |
| 164 |
the \fBGZIP OPTIONS DICTIONARY\fR section below) can be given via the |
| 165 |
\fIdictObj\fR parameter that on compression allows control over the generated |
| 166 |
headers, and on decompression allows discovery of the existing headers. Note |
| 167 |
that the dictionary will be written to on decompression once sufficient data |
| 168 |
has been read to have a complete header. This means that the dictionary must |
| 169 |
be an unshared value in that case; a blank value created with |
| 170 |
\fBTcl_NewObj\fR is suggested. |
| 171 |
.PP |
| 172 |
Once a stream has been constructed, \fBTcl_ZlibStreamPut\fR is used to add |
| 173 |
data to the stream and \fBTcl_ZlibStreamGet\fR is used to retrieve data from |
| 174 |
the stream after processing. Both return normal Tcl result codes and leave an |
| 175 |
error message in the result of the interpreter that the stream is registered |
| 176 |
with in the error case (if such a registration has been performed). With |
| 177 |
\fBTcl_ZlibStreamPut\fR, the data buffer value passed to it should not be |
| 178 |
modified afterwards. With \fBTcl_ZlibStreamGet\fR, the data buffer value |
| 179 |
passed to it will have the data bytes appended to it. Internally to the |
| 180 |
stream, data is kept compressed so as to minimize the cost of buffer space. |
| 181 |
.PP |
| 182 |
\fBTcl_ZlibStreamChecksum\fR returns the checksum computed over the |
| 183 |
uncompressed data according to the format, and \fBTcl_ZlibStreamEof\fR returns |
| 184 |
a boolean value indicating whether the end of the uncompressed data has been |
| 185 |
reached. |
| 186 |
.PP |
| 187 |
\fBTcl_ZlibStreamSetCompressionDictionary\fR is used to control the |
| 188 |
compression dictionary used with the stream, a compression dictionary being an |
| 189 |
array of bytes (such as might be created with \fBTcl_NewByteArrayObj\fR) that |
| 190 |
is used to initialize the compression engine rather than leaving it to create |
| 191 |
it on the fly from the data being compressed. Setting a compression dictionary |
| 192 |
allows for more efficient compression in the case where the start of the data |
| 193 |
is highly regular, but it does require both the compressor and the |
| 194 |
decompressor to agreee on the value to use. Compression dictionaries are only |
| 195 |
fully supported for zlib-format data; on compression, they must be set before |
| 196 |
any data is sent in with \fBTcl_ZlibStreamPut\fR, and on decompression they |
| 197 |
should be set when \fBTcl_ZlibStreamGet\fR produces an \fBerror\fR with its |
| 198 |
\fB\-errorcode\fR set to |
| 199 |
.QW "\fBZLIB NEED_DICT\fI code\fR" ; |
| 200 |
the \fIcode\fR will be the Adler-32 checksum (see \fBTcl_ZlibAdler32\fR) of |
| 201 |
the compression dictionary sought. (Note that this is only true for |
| 202 |
zlib-format streams; gzip streams ignore compression dictionaries as the |
| 203 |
format specification doesn't permit them, and raw streams just produce a data |
| 204 |
error if the compression dictionary is missing or incorrect.) |
| 205 |
.PP |
| 206 |
If you wish to clear a stream and reuse it for a new compression or |
| 207 |
decompression action, \fBTcl_ZlibStreamReset\fR will do this and return a |
| 208 |
normal Tcl result code to indicate whether it was successful; if the stream is |
| 209 |
registered with an interpreter, an error message will be left in the |
| 210 |
interpreter result when this function returns TCL_ERROR. |
| 211 |
Finally, \fBTcl_ZlibStreamClose\fR will clean up the stream and delete the |
| 212 |
associated command: using \fBTcl_DeleteCommand\fR on the stream's command is |
| 213 |
equivalent (when such a command exists). |
| 214 |
.SH "GZIP OPTIONS DICTIONARY" |
| 215 |
.PP |
| 216 |
The \fIdictObj\fR parameter to \fBTcl_ZlibDeflate\fR, \fBTcl_ZlibInflate\fR |
| 217 |
and \fBTcl_ZlibStreamInit\fR is used to pass a dictionary of options about |
| 218 |
that is used to describe the gzip header in the compressed data. When creating |
| 219 |
compressed data, the dictionary is read and when unpacking compressed data the |
| 220 |
dictionary is written (in which case the \fIdictObj\fR parameter must refer to |
| 221 |
an unshared dictionary value). |
| 222 |
.PP |
| 223 |
The following fields in the dictionary value are understood. All other fields |
| 224 |
are ignored. No field is required when creating a gzip-format stream. |
| 225 |
.TP |
| 226 |
\fBcomment\fR |
| 227 |
. |
| 228 |
This holds the comment field of the header, if present. If absent, no comment |
| 229 |
was supplied (on decompression) or will be created (on compression). |
| 230 |
.TP |
| 231 |
\fBcrc\fR |
| 232 |
. |
| 233 |
A boolean value describing whether a CRC of the header is computed. Note that |
| 234 |
the \fBgzip\fR program does \fInot\fR use or allow a CRC on the header. |
| 235 |
.TP |
| 236 |
\fBfilename\fR |
| 237 |
. |
| 238 |
The name of the file that held the uncompressed data. This should not contain |
| 239 |
any directory separators, and should be sanitized before use on decompression |
| 240 |
with \fBfile tail\fR. |
| 241 |
.TP |
| 242 |
\fBos\fR |
| 243 |
. |
| 244 |
The operating system type code field from the header (if not the |
| 245 |
.QW unknown |
| 246 |
value). See RFC 1952 for the meaning of these codes. On compression, if this |
| 247 |
is absent then the field will be set to the |
| 248 |
.QW unknown |
| 249 |
value. |
| 250 |
.TP |
| 251 |
\fBsize\fR |
| 252 |
. |
| 253 |
The size of the uncompressed data. This is ignored on compression; the size |
| 254 |
of the data compressed depends on how much data is supplied to the |
| 255 |
compression engine. |
| 256 |
.TP |
| 257 |
\fBtime\fR |
| 258 |
. |
| 259 |
The time field from the header if non-zero, expected to be the time that the |
| 260 |
file named by the \fBfilename\fR field was modified. Suitable for use with |
| 261 |
\fBclock format\fR. On creation, the right value to use is that from |
| 262 |
\fBclock seconds\fR or \fBfile mtime\fR. |
| 263 |
.TP |
| 264 |
\fBtype\fR |
| 265 |
. |
| 266 |
The type of the uncompressed data (either \fBbinary\fR or \fBtext\fR) if |
| 267 |
known. |
| 268 |
.SH "PORTABILITY NOTES" |
| 269 |
These functions will fail gracefully if Tcl is not linked with the zlib |
| 270 |
library. |
| 271 |
.SH "SEE ALSO" |
| 272 |
Tcl_NewByteArrayObj(3), zlib(n) |
| 273 |
'\"Tcl_StackChannel(3) |
| 274 |
.SH "KEYWORDS" |
| 275 |
compress, decompress, deflate, gzip, inflate |
| 276 |
'\" Local Variables: |
| 277 |
'\" mode: nroff |
| 278 |
'\" fill-column: 78 |
| 279 |
'\" End: |