TclZlib(3tcl)
hey bestie πβ¨ welcome to TclZlib(3tcl) β that's the Tcl Library Procedures page for compression and decompression functions. no cap, this is literally the whole manual, and we're gonna go through every single detail together, because we don't skip. grab a snack π and let's go.
β¨ NAME β¨
okay so here's the whole squad, no cap: Tcl_ZlibAdler32, Tcl_ZlibCRC32, Tcl_ZlibDeflate, Tcl_ZlibInflate, Tcl_ZlibStreamChecksum, Tcl_ZlibStreamClose, Tcl_ZlibStreamEof, Tcl_ZlibStreamGet, Tcl_ZlibStreamGetCommandName, Tcl_ZlibStreamInit, Tcl_ZlibStreamPut β all together they're the compression and decompression functions. π
π SYNOPSIS π
this is the exact code, bestie, don't touch a character:
#include <tcl.h>
int
Tcl_ZlibDeflate(interp, format, dataObj, level, dictObj)
int
Tcl_ZlibInflate(interp, format, dataObj, bufferSize, dictObj)
unsigned
Tcl_ZlibCRC32(initValue, bytes, length)
unsigned
Tcl_ZlibAdler32(initValue, bytes, length)
int
Tcl_ZlibStreamInit(interp, mode, format, level, dictObj, zshandlePtr)
Tcl_Obj *
Tcl_ZlibStreamGetCommandName(zshandle)
int
Tcl_ZlibStreamEof(zshandle)
int
Tcl_ZlibStreamClose(zshandle)
int
Tcl_ZlibStreamReset(zshandle)
int
Tcl_ZlibStreamChecksum(zshandle)
int
Tcl_ZlibStreamPut(zshandle, dataObj, flush)
int
Tcl_ZlibStreamGet(zshandle, dataObj, count)
Tcl_ZlibStreamSetCompressionDictionary(zshandle, compDict)
π ARGUMENTS π
okay, now the arguments, and there are a lot. every single one matters, so we're going through them one by one. π«Ά
Tcl_Interp *interp(in) β the interpreter to store resulting compressed or uncompressed data in. also where any error messages are written. forTcl_ZlibStreamInit, this can beNULLto create a stream that is not bound to a command. πint format(in) β what format of compressed data to work with. must be one ofTCL_ZLIB_FORMAT_ZLIBfor zlib-format data,TCL_ZLIB_FORMAT_GZIPfor gzip-format data, orTCL_ZLIB_FORMAT_RAWfor raw compressed data. in addition, for decompression only,TCL_ZLIB_FORMAT_AUTOmay also be chosen which can automatically detect whether the compressed data was in zlib or gzip format. β¨Tcl_Obj *dataObj(in/out) β a byte-array value containing the data to be compressed or decompressed, or to which the data extracted from the stream is appended when passed toTcl_ZlibStreamGet. πint level(in) β what level of compression to use. should be a number from0to9or one of the following:TCL_ZLIB_COMPRESS_NONEfor no compression,TCL_ZLIB_COMPRESS_FASTfor fast but inefficient compression,TCL_ZLIB_COMPRESS_BESTfor slow but maximal compression, orTCL_ZLIB_COMPRESS_DEFAULTfor the level recommended by the zlib library. π₯Tcl_Obj *dictObj(in/out) β a dictionary that contains, or which will be updated to contain, a description of the gzip header associated with the compressed data. only useful when the format isTCL_ZLIB_FORMAT_GZIPorTCL_ZLIB_FORMAT_AUTO. if aNULLis passed, a default header will be used on compression and the header will be ignored (apart from integrity checks) on decompression. see the sectionGZIP OPTIONS DICTIONARYfor details about the contents of this dictionary. πunsigned initValue(in) β the initial value for the checksum algorithm. βTcl_Size bufferSize(in) β a hint as to what size of buffer is to be used to receive the data. use0to use a geric guess based on the input data. πunsigned char *bytes(in) β an array of bytes to run the checksum algorithm over, orNULLto get the recommended initial value for the checksum algorithm. πΈTcl_Size length(in) β the number of bytes in the array. π¦int mode(in) β what mode to operate the stream in. should be eitherTCL_ZLIB_STREAM_DEFLATEfor a compressing stream orTCL_ZLIB_STREAM_INFLATEfor a decompressing stream. πTcl_ZlibStream *zshandlePtr(out) β a pointer to a variable in which to write the abstract token for the stream upon successful creation. β¨Tcl_ZlibStream zshandle(in) β the abstract token for the stream to operate on. πint flush(in) β whether and how to flush the stream after writing the data to it. must be one of:TCL_ZLIB_NO_FLUSHif no flushing is to be done,TCL_ZLIB_FLUSHif the currently compressed data must be made available for access usingTcl_ZlibStreamGet,TCL_ZLIB_FULLFLUSHif the stream must be put into a state where the decompressor can recover from on corruption, orTCL_ZLIB_FINALIZEto ensure that the stream is finished and that any trailer demanded by the format is written. π«ΆTcl_Size count(in) β the maximum number of bytes to get from the stream, or-1to get all remaining bytes from the stream's buffers. π₯Tcl_Obj *compDict(in) β a byte array value that is the compression dictionary to use with the stream. note that this is not a Tcl dictionary, and it is recommended that this only ever be used with streams that were created with their format set toTCL_ZLIB_FORMAT_ZLIBbecause the other formats have no mechanism to indicate whether a compression dictionary was present other than to fail on decompression. π
β¨ DESCRIPTION β¨
okay bestie, so these functions are literally the whole interface from the Tcl library over to the Zlib library by Jean-loup Gailly and Mark Adler. π
Tcl_ZlibDeflate and Tcl_ZlibInflate respectively compress and decompress the data sitting inside the dataObj argument, according to the format argument and β for compression β the level argument. The dictionary in the dictObj parameter is how you pass along extra header info about the compressed data when the compression format supports it. rn, that dictionary is only used when the format parameter is TCL_ZLIB_FORMAT_GZIP or TCL_ZLIB_FORMAT_AUTO. For the deets on what goes inside the dictionary, peep the GZIP OPTIONS DICTIONARY section below. π
Upon success, both functions leave the resulting compressed or decompressed data in a byte-array value that is the Tcl interpreterβs result. The returned value is a standard Tcl result code. periodt. β¨
Tcl_ZlibAdler32 and Tcl_ZlibCRC32 compute checksums on arrays of bytes, returning the computed checksum. Checksums are computed incrementally, which means you can process data one block at a time β but that means you have to maintain the current checksum and pass it in as the initValue parameter. To get the initial value for that, use NULL for the bytes parameter instead of a pointer to the array of bytes you want to compute the checksum over. So the typical single-data-block usage looks exactly like this:
checksum = Tcl_ZlibCRC32(Tcl_ZlibCRC32(0,NULL,0), data, length);
ngl, the Adler-32 algorithm is not a real checksum β itβs more like a related type of hash that works best on longer data. π
π ZLIB STREAMS π
Tcl_ZlibStreamInit creates a compressing or decompressing stream that is linked to a Tcl command, according to its arguments. It gives you an abstract token for the stream and returns a normal Tcl result code. Tcl_ZlibStreamGetCommandName returns the name of that command given the stream token, or NULL if the stream has no command. Streams are not designed to be thread-safe, bestie β each stream should only ever be used from the thread that created it. π§΅π«
When youβre working with gzip streams, a dictionary (fields are exactly as given in the GZIP OPTIONS DICTIONARY section below) can be given via the dictObj parameter. On compression, that lets you control the generated headers, and on decompression it lets you discover the existing headers. Note that the dictionary will be written to on decompression once sufficient data has been read to have a complete header. That means the dictionary must be an unshared value in that case. A blank value created with Tcl_NewObj is suggested. π
Once a stream has been constructed, Tcl_ZlibStreamPut is used to add data to the stream, and Tcl_ZlibStreamGet is used to retrieve data from the stream after processing. Both return normal Tcl result codes and leave an error message in the result of the interpreter that the stream is registered with in the error case β if such a registration has been performed. With Tcl_ZlibStreamPut, the data buffer value passed to it should not be modified afterwards. With Tcl_ZlibStreamGet, the data buffer value passed to it will have the data bytes appended to it. Internally to the stream, data is kept compressed so as to minimize the cost of buffer space. β¨
Tcl_ZlibStreamChecksum returns the checksum computed over the uncompressed data according to the format, and Tcl_ZlibStreamEof returns a boolean value indicating whether the end of the uncompressed data has been reached.
Tcl_ZlibStreamSetCompressionDictionary is used to control the compression dictionary used with the stream. A compression dictionary is an array of bytes (such as might be created with Tcl_NewByteArrayObj) that is used to initialize the compression engine rather than leaving it to create it on the fly from the data being compressed. Setting a compression dictionary allows for more efficient compression in the case where the start of the data is highly regular, but it does require both the compressor and the decompressor to agree on the value to use. π₯
Compression dictionaries are only fully supported for zlib-format data. On compression, they must be set before any data is sent in with Tcl_ZlibStreamPut. On decompression, they should be set when Tcl_ZlibStreamGet produces an error with its -errorcode set to βZLIB NEED_DICT codeβ; the code will be the Adler-32 checksum (see Tcl_ZlibAdler32) of the compression dictionary sought. (Note that this is only true for zlib-format streams; gzip streams ignore compression dictionaries as the format specification doesnβt permit them, and raw streams just produce a data error if the compression dictionary is missing or incorrect.)
If you wish to clear a stream and reuse it for a new compression or decompression action, Tcl_ZlibStreamReset will do this and return a normal Tcl result code to indicate whether it was successful. If the stream is registered with an interpreter, an error message will be left in the interpreter result when this function returns TCL_ERROR. Finally, Tcl_ZlibStreamClose will clean up the stream and delete the associated command: using Tcl_DeleteCommand on the streamβs command is equivalent β when such a command exists. π
π GZIP OPTIONS DICTIONARY π
The dictObj parameter to Tcl_ZlibDeflate, Tcl_ZlibInflate and Tcl_ZlibStreamInit is used to pass a dictionary of options about that is used to describe the gzip header in the compressed data. When creating compressed data, the dictionary is read; when unpacking compressed data, the dictionary is written β in which case the dictObj parameter must refer to an unshared dictionary value. π«Ά
The following fields in the dictionary value are understood. All other fields are ignored. No field is required when creating a gzip-format stream.
commentβ This holds the comment field of the header, if present. If absent, no comment was supplied (on decompression) or will be created (on compression).crcβ A boolean value describing whether a CRC of the header is computed. Note that the gzip program does not use or allow a CRC on the header.filenameβ The name of the file that held the uncompressed data. This should not contain any directory separators, and should be sanitized before use on decompression with filetail.osβ The operating system type code field from the header (if not the βunknownβ value). See RFC 1952 for the meaning of these codes. On compression, if this is absent then the field will be set to the βunknownβ value.sizeβ The size of the uncompressed data. This is ignored on compression; the size of the data compressed depends on how much data is supplied to the compression engine.timeβ The time field from the header if non-zero, expected to be the time that the file named by thefilenamefield was modified. Suitable for use withclock format. On creation, the right value to use is that fromclock secondsorfile mtime.typeβ The type of the uncompressed data (eitherbinaryortext) if known.
π§Ύ REFERENCE COUNT MANAGEMENT π§Ύ
Tcl_ZlibDeflate and Tcl_ZlibInflate take a value with arbitrary reference count for their dataObj and dictObj arguments (the latter often being NULL instead), and set the interpreter result with their output value (or an error). The existing interpreter result should not be passed as any argument value unless an additional reference is held.
Tcl_ZlibStreamInit takes a value with arbitrary reference count for its dictObj argument; it only reads from it. The existing interpreter result should not be passed unless an additional reference is held.
Tcl_ZlibStreamGetCommandName returns a zero reference count value, much like Tcl_NewObj.
The dataObj argument to Tcl_ZlibStreamPut is a value with arbitrary reference count; it is only ever read from.
The dataObj argument to Tcl_ZlibStreamGet is an unshared value (see Tcl_IsShared) that will be updated by the function.
The compDict argument to Tcl_ZlibStreamSetCompressionDictionary, if non-NULL, may be duplicated or may have its reference count incremented. Using a zero reference count value is not recommended.
π PORTABILITY NOTES π
These functions will fail gracefully if Tcl is not linked with the zlib library. π¦
π SEE ALSO π
Tcl_NewByteArrayObj(3tcl), zlib(3tcl)
π KEYWORDS π
compress, decompress, deflate, gzip, inflate
this iconique page was brought to you by Tcl 8.6 and the manual page TclZlib(3tcl) β stay compressed, bestie ππ¦β¨
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2008 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms