]> git.lizzy.rs Git - zlib.git/commitdiff
Add deflateGetDictionary() function.
authorMark Adler <madler@alumni.caltech.edu>
Sat, 31 Dec 2016 00:29:56 +0000 (16:29 -0800)
committerMark Adler <madler@alumni.caltech.edu>
Sat, 31 Dec 2016 00:29:56 +0000 (16:29 -0800)
Per request, but its utility is likely to be very limited. See the
comments in zlib.h.

deflate.c
zlib.h

index 47d55af345a676c52edf09c2242a7627beefa469..cf4c056edec8008602055c71aa42691c82c40fd5 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -440,6 +440,27 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
     return Z_OK;
 }
 
+/* ========================================================================= */
+int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
+    z_streamp strm;
+    Bytef *dictionary;
+    uInt  *dictLength;
+{
+    deflate_state *s;
+
+    if (deflateStateCheck(strm))
+        return Z_STREAM_ERROR;
+    s = strm->state;
+    uInt len = s->strstart + s->lookahead;
+    if (len > s->w_size)
+        len = s->w_size;
+    if (dictionary != Z_NULL && len)
+        zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
+    if (dictLength != Z_NULL)
+        *dictLength = len;
+    return Z_OK;
+}
+
 /* ========================================================================= */
 int ZEXPORT deflateResetKeep (strm)
     z_streamp strm;
diff --git a/zlib.h b/zlib.h
index 053e6e2e3de9b308cb661ffd17ffde2fe0018f31..6f4a049042bccd3036ce1dae01d1ad7eb610f1f1 100644 (file)
--- a/zlib.h
+++ b/zlib.h
@@ -651,6 +651,28 @@ ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
    not perform any compression: this will be done by deflate().
 */
 
+ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm,
+                                             Bytef *dictionary,
+                                             uInt  *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 OF((z_streamp dest,
                                     z_streamp source));
 /*