GPAK  1.0.0
A general-purpose archive library

◆ _gpak_decompressor_inflate()

GPAK_API uint32_t _gpak_decompressor_inflate ( gpak_t _pak,
FILE *  _infile,
FILE *  _outfile,
size_t  _read_size 
)
Brief Description:\n Decompresses the input file using the Inflate algorithm.

This function decompresses the input file using the Inflate algorithm and writes the decompressed data to the output file.

Parameters
_pakA pointer to the gpak_t.
_infileA pointer to the input FILE.
_outfileA pointer to the output FILE.
_read_sizeThe number of bytes to read from the input file.
Returns
The number of bytes written to the output file.

Definition at line 162 of file gpak_compressors.c.

163 {
164  z_stream strm;
165  strm.zalloc = Z_NULL;
166  strm.zfree = Z_NULL;
167  strm.opaque = Z_NULL;
168 
169  char* _bufferIn = (char*)malloc(_DEFAULT_BLOCK_SIZE);
170  char* _bufferOut = (char*)malloc(_DEFAULT_BLOCK_SIZE);
171 
172  int ret = inflateInit(&strm);
173  if (ret != Z_OK)
174  return _gpak_make_error(_pak, GPAK_ERROR_INFLATE_INIT);
175 
176  uint32_t _crc32 = crc32(0L, Z_NULL, 0);
177 
178  unsigned have;
179  size_t total_read = 0ull;
180 
181  do
182  {
183  size_t bytes_to_read = _read_size - total_read < _DEFAULT_BLOCK_SIZE ? _read_size - total_read : _DEFAULT_BLOCK_SIZE;
184  strm.avail_in = _freadb(_bufferIn, 1, bytes_to_read, _infile);
185  total_read += strm.avail_in;
186  _gpak_pass_progress(_pak, total_read, _read_size, GPAK_STAGE_DECOMPRESSION);
187 
188  strm.next_in = _bufferIn;
189 
190  do
191  {
192  strm.avail_out = _DEFAULT_BLOCK_SIZE;
193  strm.next_out = _bufferOut;
194  ret = inflate(&strm, Z_NO_FLUSH);
195  assert(ret != Z_STREAM_ERROR);
196 
197  switch (ret) {
198  case Z_NEED_DICT:
199  case Z_DATA_ERROR:
200  case Z_MEM_ERROR:
201  goto end;
202  }
203 
204  have = _DEFAULT_BLOCK_SIZE - strm.avail_out;
205 
206  _crc32 = crc32(_crc32, _bufferOut, have);
207 
208  if (_fwriteb(_bufferOut, 1, have, _outfile) != have || ferror(_outfile))
209  {
210  _gpak_make_error(_pak, GPAK_ERROR_INFLATE_FAILED);
211  goto end;
212  }
213 
214  } while (strm.avail_out == 0);
215 
216  } while (ret != Z_STREAM_END);
217 
218 end:
219  free(_bufferIn);
220  free(_bufferOut);
221  (void)inflateEnd(&strm);
222 
223  return _crc32;
224 }
@ GPAK_ERROR_INFLATE_FAILED
Definition: gpak_data.h:169
@ GPAK_ERROR_INFLATE_INIT
Definition: gpak_data.h:168
@ GPAK_STAGE_DECOMPRESSION
Definition: gpak_data.h:227

Referenced by gpak_fopen().