src/utility/tinflate.c
int tinflate_partial(const void *compressed_data, long compressed_size,
                     void *output_buffer, long output_size,
                     unsigned long *size_ret, unsigned long *crc_ret,
                     void *state_buffer, long state_buffer_size)
{
    /* state: Decompression state buffer, cast to the structured type. */
    DecompressionState *state = (DecompressionState *)state_buffer;

    /*-------------------------------------------------------------------*/

    /**** Check parameter validity. ****/

    if (compressed_data == NULL
     || compressed_size < 0
     || output_size < 0
     || (output_size > 0 && output_buffer == NULL)
     || state_buffer == NULL
     || state_buffer_size < (long)sizeof(DecompressionState)) {
        return -1;
    }

    /**** Insert data pointers into decompression state block. ****/

    state->in_ptr   = (const unsigned char *)compressed_data;
    state->in_top   = state->in_ptr + compressed_size;
    state->out_base = output_buffer;
    state->out_size = output_size;

    /**** If this is the first call, check for a zlib header, then ****
     **** initialize the processing state.                         ****/

    if (state->state == INITIAL || state->state == PARTIAL_ZLIB_HEADER) {
        /*
         * A zlib header is a big-endian 16-bit integer, composed of the
         * following fields:
         *     0xF000: Window size (log2(maximum_distance), 8..15) minus 8
         *     0x0F00: Compression method (always 8)
         *     0x00C0: Compression level
         *     0x0020: Custom dictionary flag
         *     0x001F: Check bits (set so the header is a multiple of 31)
         */
        unsigned int zlib_header;
        if (compressed_size == 0) {
            /* We didn't get any data at all, so there's no change in state. */
            return 1;
        }
        if (state->state == INITIAL && compressed_size == 1) {
            /* Save the single byte in the state block for next time. */
            state->first_byte = state->in_ptr[0];
            state->state = PARTIAL_ZLIB_HEADER;
            return 1;
        }
        if (state->state == PARTIAL_ZLIB_HEADER) {
            zlib_header = state->first_byte<<8 | state->in_ptr[0];
        } else {
            zlib_header = state->in_ptr[0]<<8 | state->in_ptr[1];
        }
        if ((zlib_header & 0x8F00) == 0x0800 && zlib_header % 31 == 0) {
            if (zlib_header & 0x0020) {
                /* This library does not support custom dictionaries. */
                return -1;
            }
            state->in_ptr += (state->state == PARTIAL_ZLIB_HEADER ? 1 : 2);
        } else if (state->state == PARTIAL_ZLIB_HEADER) {
            /* Return the first byte to the bitstream so we can process it
             * as part of the compressed data. */
            state->bit_accum = state->first_byte;
            state->num_bits = 8;
        }
        state->state = HEADER;
    }

    /**** Decompress blocks until either the end of the compressed ****
     **** data is reached, an error occurs, or a block with the    ****
     **** "final" bit is set.  ****/

    do {
        int res = tinflate_block(state);
        if (res != 0) {
            /* This will catch the out-of-data case, so we don't need to
             * check for out-of-data separately. */
            return res;
        }
        /* Ensure that the total output size has not rolled over to a
         * negative value; if it has, return an error. */
        if ((long)state->out_ofs < 0) {
            return -1;
        }
        /* Check for end-of-stream.  (Note that this flag will be set at
         * the beginning of processing the final block, but since we
         * already checked for end-of-block, we do not need to do so again
         * here.) */
    } while (!state->final);

    /**** Return the total decompressed size and CRC (if requested). ****/

    if (size_ret) {
        *size_ret = state->out_ofs;
    }
    if (crc_ret) {
        *crc_ret = state->crc;
    }
    return 0;
}
/**
 * tinflate_partial:  Decompress a portion of a stream of data compressed
 * with the "deflate" algorithm.
 *
 * Parameters:
 *       compressed_data: Pointer to the portion of the compressed data to
 *                           process.
 *       compressed_size: Number of bytes of compressed data.
 *         output_buffer: Pointer to the buffer to receive uncompressed data.
 *           output_size: Size of the output buffer, in bytes.
 *              size_ret: Pointer to variable to receive the size of the
 *                           uncompressed data, or NULL if the size is not
 *                           needed.
 *               crc_ret: Pointer to variable to receive the CRC-32 of the
 *                           uncompressed data, or NULL if the CRC is not
 *                           needed.
 *          state_buffer: Pointer to a buffer to hold state information,
 *                           which must be zeroed before the first call for
 *                           the stream.
 *     state_buffer_size: Size of the state buffer, in bytes.  Must be no
 *                           less than the value returned by
 *                           tinflate_state_size().
 * Return value:
 *     Zero when the data has been completely decompressed; an unspecified
 *     positive value if the end of the input data is reached before
 *     decompression is complete; or an unspecified negative value if an
 *     error occurs.  (A full output buffer is not considered an error.)
 * Notes:
 *     The returned CRC value is only valid after the entire stream of data
 *     has been decompressed, and only when the decompressed data fits
 *     within the output buffer, i.e. when the value stored in *size_ret is
 *     no greater than the value of output_size.
 */
Decompress a portion of a stream of data compressed
with the "deflate" algorithm.

Pointer to the portion of the compressed data to
process.
compressed_size: Number of bytes of compressed data.
output_buffer: Pointer to the buffer to receive uncompressed data.
output_size: Size of the output buffer, in bytes.
size_ret: Pointer to variable to receive the size of the
uncompressed data, or NULL if the size is not
needed.
crc_ret: Pointer to variable to receive the CRC-32 of the
uncompressed data, or NULL if the CRC is not
needed.
state_buffer: Pointer to a buffer to hold state information,
which must be zeroed before the first call for
the stream.
state_buffer_size: Size of the state buffer, in bytes.  Must be no
less than the value returned by
tinflate_state_size().
Return value:
Zero when the data has been completely decompressed; an unspecified
positive value if the end of the input data is reached before
decompression is complete; or an unspecified negative value if an
error occurs.  (A full output buffer is not considered an error.)
Notes:
The returned CRC value is only valid after the entire stream of data
has been decompressed, and only when the decompressed data fits
within the output buffer, i.e. when the value stored in *size_ret is
no greater than the value of output_size.
Decompress a portion of a stream of data compressed with the " deflate " algorithm . Pointer to the portion of the compressed data to process . compressed_size : Number of bytes of compressed data . output_buffer : Pointer to the buffer to receive uncompressed data . output_size : Size of the output buffer in bytes . size_ret : Pointer to variable to receive the size of the uncompressed data or NULL if the size is not needed . crc_ret : Pointer to variable to receive the CRC - 32 of the uncompressed data or NULL if the CRC is not needed . state_buffer : Pointer to a buffer to hold state information which must be zeroed before the first call for the stream . state_buffer_size : Size of the state buffer in bytes . Must be no less than the value returned by tinflate_state_size () . Return value : Zero when the data has been completely decompressed ; an unspecified positive value if the end of the input data is reached before decompression is complete ; or an unspecified negative value if an error occurs . ( A full output buffer is not considered an error . ) Notes : The returned CRC value is only valid after the entire stream of data has been decompressed and only when the decompressed data fits within the output buffer i . e . when the value stored in * size_ret is no greater than the value of output_size .
int tinflate_partial(const void *compressed_data, long compressed_size,
                     void *output_buffer, long output_size,
                     unsigned long *size_ret, unsigned long *crc_ret,
                     void *state_buffer, long state_buffer_size)
{
    DecompressionState *state = (DecompressionState *)state_buffer;
    if (compressed_data == NULL
     || compressed_size < 0
     || output_size < 0
     || (output_size > 0 && output_buffer == NULL)
     || state_buffer == NULL
     || state_buffer_size < (long)sizeof(DecompressionState)) {
        return -1;
    }
    state->in_ptr   = (const unsigned char *)compressed_data;
    state->in_top   = state->in_ptr + compressed_size;
    state->out_base = output_buffer;
    state->out_size = output_size;
    if (state->state == INITIAL || state->state == PARTIAL_ZLIB_HEADER) {
        unsigned int zlib_header;
        if (compressed_size == 0) {
            return 1;
        }
        if (state->state == INITIAL && compressed_size == 1) {
            state->first_byte = state->in_ptr[0];
            state->state = PARTIAL_ZLIB_HEADER;
            return 1;
        }
        if (state->state == PARTIAL_ZLIB_HEADER) {
            zlib_header = state->first_byte<<8 | state->in_ptr[0];
        } else {
            zlib_header = state->in_ptr[0]<<8 | state->in_ptr[1];
        }
        if ((zlib_header & 0x8F00) == 0x0800 && zlib_header % 31 == 0) {
            if (zlib_header & 0x0020) {
                return -1;
            }
            state->in_ptr += (state->state == PARTIAL_ZLIB_HEADER ? 1 : 2);
        } else if (state->state == PARTIAL_ZLIB_HEADER) {
            state->bit_accum = state->first_byte;
            state->num_bits = 8;
        }
        state->state = HEADER;
    }
    do {
        int res = tinflate_block(state);
        if (res != 0) {
            return res;
        }
        if ((long)state->out_ofs < 0) {
            return -1;
        }
    } while (!state->final);
    if (size_ret) {
        *size_ret = state->out_ofs;
    }
    if (crc_ret) {
        *crc_ret = state->crc;
    }
    return 0;
}
int tinflate_partial ( const void * compressed_data , long compressed_size , void * output_buffer , long output_size , unsigned long * size_ret , unsigned long * crc_ret , void * state_buffer , long state_buffer_size ) { DecompressionState * state = ( DecompressionState * ) state_buffer ; if ( compressed_data == NULL || compressed_size < 0 || output_size < 0 || ( output_size > 0 && output_buffer == NULL ) || state_buffer == NULL || state_buffer_size < ( long ) sizeof ( DecompressionState ) ) { return -1 ; } state -> in_ptr = ( const unsigned char * ) compressed_data ; state -> in_top = state -> in_ptr + compressed_size ; state -> out_base = output_buffer ; state -> out_size = output_size ; if ( state -> state == INITIAL || state -> state == PARTIAL_ZLIB_HEADER ) { unsigned int zlib_header ; if ( compressed_size == 0 ) { return 1 ; } if ( state -> state == INITIAL && compressed_size == 1 ) { state -> first_byte = state -> in_ptr [ 0 ] ; state -> state = PARTIAL_ZLIB_HEADER ; return 1 ; } if ( state -> state == PARTIAL_ZLIB_HEADER ) { zlib_header = state -> first_byte << 8 | state -> in_ptr [ 0 ] ; } else { zlib_header = state -> in_ptr [ 0 ] << 8 | state -> in_ptr [ 1 ] ; } if ( ( zlib_header & 0x8F00 ) == 0x0800 && zlib_header % 31 == 0 ) { if ( zlib_header & 0x0020 ) { return -1 ; } state -> in_ptr += ( state -> state == PARTIAL_ZLIB_HEADER ? 1 : 2 ) ; } else if ( state -> state == PARTIAL_ZLIB_HEADER ) { state -> bit_accum = state -> first_byte ; state -> num_bits = 8 ; } state -> state = HEADER ; } do { int res = tinflate_block ( state ) ; if ( res != 0 ) { return res ; } if ( ( long ) state -> out_ofs < 0 ) { return -1 ; } } while ( ! state -> final ) ; if ( size_ret ) { * size_ret = state -> out_ofs ; } if ( crc_ret ) { * crc_ret = state -> crc ; } return 0 ; }
tinflate_partial:  Decompress a portion of a stream of data compressed
 with the "deflate" algorithm.
tinflate_partial : Decompress a portion of a stream of data compressed with the " deflate " algorithm .
/* state: Decompression state buffer, cast to the structured type. */ /*-------------------------------------------------------------------*/ /**** Check parameter validity. ****/ /**** Insert data pointers into decompression state block. ****/ /**** If this is the first call, check for a zlib header, then ****
     **** initialize the processing state.                         ****/ /*
         * A zlib header is a big-endian 16-bit integer, composed of the
         * following fields:
         *     0xF000: Window size (log2(maximum_distance), 8..15) minus 8
         *     0x0F00: Compression method (always 8)
         *     0x00C0: Compression level
         *     0x0020: Custom dictionary flag
         *     0x001F: Check bits (set so the header is a multiple of 31)
         */ /* We didn't get any data at all, so there's no change in state. */ /* Save the single byte in the state block for next time. */ /* This library does not support custom dictionaries. */ /* Return the first byte to the bitstream so we can process it
             * as part of the compressed data. */ /**** Decompress blocks until either the end of the compressed ****
     **** data is reached, an error occurs, or a block with the    ****
     **** "final" bit is set.  ****/ /* This will catch the out-of-data case, so we don't need to
             * check for out-of-data separately. */ /* Ensure that the total output size has not rolled over to a
         * negative value; if it has, return an error. */ /* Check for end-of-stream.  (Note that this flag will be set at
         * the beginning of processing the final block, but since we
         * already checked for end-of-block, we do not need to do so again
         * here.) */ /**** Return the total decompressed size and CRC (if requested). ****/