tests/zdeflate.c
/* Decompress from file source to file dest until stream ends or EOF.
   inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
   allocated for processing, Z_DATA_ERROR if the deflate data is
   invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
   the version of the library linked do not match, or Z_ERRNO if there
   is an error reading or writing the files. */
Decompress from file source to file dest until stream ends or EOF.
inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_DATA_ERROR if the deflate data is
invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
the version of the library linked do not match, or Z_ERRNO if there
is an error reading or writing the files.
Decompress from file source to file dest until stream ends or EOF . inf () returns Z_OK on success Z_MEM_ERROR if memory could not be allocated for processing Z_DATA_ERROR if the deflate data is invalid or incomplete Z_VERSION_ERROR if the version of zlib . h and the version of the library linked do not match or Z_ERRNO if there is an error reading or writing the files .
int inf(FILE *source, FILE *dest, int isZlib, char* dictionary, int dictSize)
{
    int ret;
    unsigned have;
    z_stream strm;
    unsigned char in[CHUNK];
    unsigned char out[CHUNK];
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.avail_in = 0;
    strm.next_in = Z_NULL;
    ret = inflateInit2(&strm, isZlib?15:-15);
    if (ret != Z_OK)
        return ret;
    if (dictionary && !isZlib)
        inflateSetDictionary(&strm, dictionary, dictSize);
    do {
        strm.avail_in = fread(in, 1, CHUNK, source);
        if (ferror(source)) {
            (void)inflateEnd(&strm);
            return Z_ERRNO;
        }
        if (strm.avail_in == 0)
            break;
        strm.next_in = in;
        int set_dictionary_zlib = 0;
        do {
            set_dictionary_zlib = 0 ;
            strm.avail_out = CHUNK;
            strm.next_out = out;
            ret = inflate(&strm, Z_NO_FLUSH);
            assert(ret != Z_STREAM_ERROR);  
            switch (ret) {
            case Z_NEED_DICT:
                if (!dictionary)
                    ret = Z_DATA_ERROR;     
                else {
                    ret = inflateSetDictionary(&strm, dictionary, dictSize);
                    if (ret == Z_OK) {
                        set_dictionary_zlib = 1;
                        break;
                    } 
                }
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                (void)inflateEnd(&strm);
                return ret;
            }
            if (!set_dictionary_zlib) {
                have = CHUNK - strm.avail_out;
                if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
                    (void)inflateEnd(&strm);
                    return Z_ERRNO;
                }
            }
        } while (strm.avail_out == 0 || set_dictionary_zlib);
    } while (ret != Z_STREAM_END);
    (void)inflateEnd(&strm);
    if (ret == Z_STREAM_END) {
        int fPos = ftell(source);
        fseek(source, 0, SEEK_END);
        int unprocessed = ftell(source)-fPos+strm.avail_in;
        if (unprocessed > 0) {
            fprintf(stderr, "%d", unprocessed);
        }
        return Z_OK;
    }
    else
        return Z_DATA_ERROR;
}
int inf ( FILE * source , FILE * dest , int isZlib , char * dictionary , int dictSize ) { int ret ; unsigned have ; z_stream strm ; unsigned char in [ CHUNK ] ; unsigned char out [ CHUNK ] ; strm . zalloc = Z_NULL ; strm . zfree = Z_NULL ; strm . opaque = Z_NULL ; strm . avail_in = 0 ; strm . next_in = Z_NULL ; ret = inflateInit2 ( & strm , isZlib ? 15 : -15 ) ; if ( ret != Z_OK ) return ret ; if ( dictionary && ! isZlib ) inflateSetDictionary ( & strm , dictionary , dictSize ) ; do { strm . avail_in = fread ( in , 1 , CHUNK , source ) ; if ( ferror ( source ) ) { ( void ) inflateEnd ( & strm ) ; return Z_ERRNO ; } if ( strm . avail_in == 0 ) break ; strm . next_in = in ; int set_dictionary_zlib = 0 ; do { set_dictionary_zlib = 0 ; strm . avail_out = CHUNK ; strm . next_out = out ; ret = inflate ( & strm , Z_NO_FLUSH ) ; assert ( ret != Z_STREAM_ERROR ) ; switch ( ret ) { case Z_NEED_DICT : if ( ! dictionary ) ret = Z_DATA_ERROR ; else { ret = inflateSetDictionary ( & strm , dictionary , dictSize ) ; if ( ret == Z_OK ) { set_dictionary_zlib = 1 ; break ; } } case Z_DATA_ERROR : case Z_MEM_ERROR : ( void ) inflateEnd ( & strm ) ; return ret ; } if ( ! set_dictionary_zlib ) { have = CHUNK - strm . avail_out ; if ( fwrite ( out , 1 , have , dest ) != have || ferror ( dest ) ) { ( void ) inflateEnd ( & strm ) ; return Z_ERRNO ; } } } while ( strm . avail_out == 0 || set_dictionary_zlib ) ; } while ( ret != Z_STREAM_END ) ; ( void ) inflateEnd ( & strm ) ; if ( ret == Z_STREAM_END ) { int fPos = ftell ( source ) ; fseek ( source , 0 , SEEK_END ) ; int unprocessed = ftell ( source ) - fPos + strm . avail_in ; if ( unprocessed > 0 ) { fprintf ( stderr , " " , unprocessed ) ; } return Z_OK ; } else return Z_DATA_ERROR ; }
Decompress from file source to file dest until stream ends or EOF.
Decompress from file source to file dest until stream ends or EOF .
/* allocate inflate state */ /* decompress until deflate stream ends or end of file */ /* run inflate() on input until output buffer not full */ /* state not clobbered */ /* and fall through */ // LibDeflate: Bad problem practice /* done when inflate() says it's done */ /* clean up and return */