2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
#include "inflate.h"
|
|
|
|
|
|
|
|
#define CHUNK 1024
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int inflateFile( FILE *source, FILE *dest )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
int ret;
|
|
|
|
unsigned have;
|
|
|
|
z_stream strm;
|
|
|
|
unsigned char in[CHUNK];
|
|
|
|
unsigned char out[CHUNK];
|
|
|
|
|
|
|
|
/* allocate inflate state */
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
strm.avail_in = 0;
|
|
|
|
strm.next_in = Z_NULL;
|
2010-09-19 01:16:05 +02:00
|
|
|
ret = inflateInit( &strm );
|
|
|
|
if ( ret != Z_OK )
|
2009-10-01 01:10:58 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* decompress until deflate stream ends or end of file */
|
2010-09-19 01:16:05 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
strm.avail_in = fread( in, 1, CHUNK, source );
|
|
|
|
if ( ferror( source ) )
|
|
|
|
{
|
|
|
|
( void )inflateEnd( &strm );
|
2009-10-01 01:10:58 +02:00
|
|
|
return Z_ERRNO;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( strm.avail_in == 0 )
|
2009-10-01 01:10:58 +02:00
|
|
|
break;
|
|
|
|
strm.next_in = in;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-10-01 01:10:58 +02:00
|
|
|
/* run inflate() on input until output buffer not full */
|
2010-09-19 01:16:05 +02:00
|
|
|
do
|
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
strm.avail_out = CHUNK;
|
|
|
|
strm.next_out = out;
|
2010-09-19 01:16:05 +02:00
|
|
|
ret = inflate( &strm, Z_NO_FLUSH );
|
|
|
|
switch ( ret )
|
|
|
|
{
|
|
|
|
case Z_NEED_DICT:
|
|
|
|
( void )inflateEnd( &strm );
|
|
|
|
return -20;
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
( void )inflateEnd( &strm );
|
|
|
|
return -21;
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
( void )inflateEnd( &strm );
|
|
|
|
return -22;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
have = CHUNK - strm.avail_out;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( fwrite( out, 1, have, dest ) != have || ferror( dest ) )
|
|
|
|
{
|
|
|
|
( void )inflateEnd( &strm );
|
2009-10-01 01:10:58 +02:00
|
|
|
return Z_ERRNO;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
while ( strm.avail_out == 0 );
|
2009-10-01 01:10:58 +02:00
|
|
|
/* done when inflate() says it's done */
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
while ( ret != Z_STREAM_END );
|
2009-10-01 01:10:58 +02:00
|
|
|
/* clean up and return */
|
2010-09-19 01:16:05 +02:00
|
|
|
( void )inflateEnd( &strm );
|
2009-10-01 01:10:58 +02:00
|
|
|
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
|
|
|
}
|