Skip to content

Commit ee7d7b5

Browse files
committed
Add deflateGetDictionary() function.
Per request, but its utility is likely to be very limited. See the comments in zlib.h.
1 parent feafcfa commit ee7d7b5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

deflate.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,27 @@ int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
440440
return Z_OK;
441441
}
442442

443+
/* ========================================================================= */
444+
int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength)
445+
z_streamp strm;
446+
Bytef *dictionary;
447+
uInt *dictLength;
448+
{
449+
deflate_state *s;
450+
451+
if (deflateStateCheck(strm))
452+
return Z_STREAM_ERROR;
453+
s = strm->state;
454+
uInt len = s->strstart + s->lookahead;
455+
if (len > s->w_size)
456+
len = s->w_size;
457+
if (dictionary != Z_NULL && len)
458+
zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
459+
if (dictLength != Z_NULL)
460+
*dictLength = len;
461+
return Z_OK;
462+
}
463+
443464
/* ========================================================================= */
444465
int ZEXPORT deflateResetKeep (strm)
445466
z_streamp strm;

zlib.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,28 @@ ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
651651
not perform any compression: this will be done by deflate().
652652
*/
653653

654+
ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm,
655+
Bytef *dictionary,
656+
uInt *dictLength));
657+
/*
658+
Returns the sliding dictionary being maintained by deflate. dictLength is
659+
set to the number of bytes in the dictionary, and that many bytes are copied
660+
to dictionary. dictionary must have enough space, where 32768 bytes is
661+
always enough. If deflateGetDictionary() is called with dictionary equal to
662+
Z_NULL, then only the dictionary length is returned, and nothing is copied.
663+
Similary, if dictLength is Z_NULL, then it is not set.
664+
665+
deflateGetDictionary() may return a length less than the window size, even
666+
when more than the window size in input has been provided. It may return up
667+
to 258 bytes less in that case, due to how zlib's implementation of deflate
668+
manages the sliding window and lookahead for matches, where matches can be
669+
up to 258 bytes long. If the application needs the last window-size bytes of
670+
input, then that would need to be saved by the application outside of zlib.
671+
672+
deflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
673+
stream state is inconsistent.
674+
*/
675+
654676
ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
655677
z_streamp source));
656678
/*

0 commit comments

Comments
 (0)