Name

deflateSetDictionary — Initializes the compression dictionary associated with stream using the dictlen bytes referenced by dictionary.


Library

Libz.lib


Synopsis

#include <zlib.h>
int deflateSetDictionary(z_streamp stream, const Bytef * dictionary, uInt dictlen);


Return Value

On success, deflateSetDictionary() shall return Z_OK. Otherwise it shall return Z_STREAM_ERROR to indicate an error.


Detailed Description

The deflateSetDictionary() function shall initialize the compression dictionary associated with stream using the dictlen bytes referenced by dictionary.

The implementation may silently use a subset of the provided dictionary if the dictionary cannot fit in the current window associated with stream (see deflateInit2_()). The application should ensure that the dictionary is sorted such that the most commonly used strings occur at the end of the dictionary.

If the dictionary is successfully set, the Adler32 checksum of the entire provided dictionary shall be stored in the adler member of stream. This value may be used by the decompression system to select the correct dictionary. The compression and decompression systems must use the same dictionary.

stream shall reference an initialized compression stream, with total_in zero (i.e. no data has been compressed since the stream was initialized).


Examples

To initialize the compression dictionary associated with stream using the dictlen bytes referenced by dictionary:

#include <stdio.h>
#include <zlib.h>

void DeflateSetDictionary( )
{    
    Byte *compr, *uncompr;
    uLong comprLen = 20*sizeof(int);
    uLong uncomprLen = comprLen;
    const char dictionary[] = "hello";                    
    compr    = (Byte*)calloc((uInt)comprLen, 1);
    uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
    const char hello[] = "hello, hello!";
    compress(compr, &comprLen, (const Bytef*)hello, len);
    z_stream c_stream; /* compression stream */
    int err;
    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
    c_stream.opaque = (voidpf)0;

    err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
    err = deflateSetDictionary(&c_stream, (const Bytef*)dictionary, sizeof(dictionary));
    dictId = c_stream.adler;
    c_stream.next_out = compr;
    c_stream.avail_out = (uInt)comprLen;

    c_stream.next_in = (Bytef*)hello;
    c_stream.avail_in = (uInt)strlen(hello)+1;

    err = deflate(&c_stream, Z_FINISH);
    if (err != Z_STREAM_END)
    printf("error");
    deflateEnd(&c_stream);
    free(compr);
    free(uncompr);
}

Errors

On error, deflateSetDictionary() shall return a value as described below: 

Z_STREAM_ERROR

The state in stream is inconsistent, or stream was NULL.


Feedback

For additional information or queries on this page send feedback

© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top