From: Hans Kristian Rosbach Date: Mon, 6 Feb 2017 13:40:29 +0000 (+0100) Subject: Add deflateGetDictionary() function. X-Git-Tag: 1.9.9-b1~714 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e13c989b2fe5e8fea707f16411720d82b107e41;p=thirdparty%2Fzlib-ng.git Add deflateGetDictionary() function. Per request, but its utility is likely to be very limited. See the comments in zlib.h. Based on upstream ee7d7b5dda25c111e61e19ac7b476c26aa6f3020 --- diff --git a/deflate.c b/deflate.c index e28a9a806..90415d35a 100644 --- a/deflate.c +++ b/deflate.c @@ -415,6 +415,23 @@ int ZEXPORT deflateSetDictionary(z_stream *strm, const unsigned char *dictionary return Z_OK; } +/* ========================================================================= */ +int ZEXPORT deflateGetDictionary (z_stream *strm, unsigned char *dictionary, unsigned int *dictLength) { + deflate_state *s; + + if (deflateStateCheck(strm)) + return Z_STREAM_ERROR; + s = strm->state; + unsigned int len = s->strstart + s->lookahead; + if (len > s->w_size) + len = s->w_size; + if (dictionary != NULL && len) + memcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); + if (dictLength != NULL) + *dictLength = len; + return Z_OK; +} + /* ========================================================================= */ int ZEXPORT deflateResetKeep(z_stream *strm) { deflate_state *s; diff --git a/zlib.h b/zlib.h index ff7441415..1cec7a5d9 100644 --- a/zlib.h +++ b/zlib.h @@ -648,6 +648,26 @@ ZEXTERN int ZEXPORT deflateSetDictionary(z_stream *strm, not perform any compression: this will be done by deflate(). */ +ZEXTERN int ZEXPORT deflateGetDictionary (z_stream *strm, unsigned char *dictionary, unsigned int *dictLength) { +/* + Returns the sliding dictionary being maintained by deflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If deflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + deflateGetDictionary() may return a length less than the window size, even + when more than the window size in input has been provided. It may return up + to 258 bytes less in that case, due to how zlib's implementation of deflate + manages the sliding window and lookahead for matches, where matches can be + up to 258 bytes long. If the application needs the last window-size bytes of + input, then that would need to be saved by the application outside of zlib. + + deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + ZEXTERN int ZEXPORT deflateCopy(z_stream *dest, z_stream *source); /* Sets the destination stream as a complete copy of the source stream.