1
0

uncompr.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* uncompr.c -- decompress a memory buffer
  2. * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* @(#) $Id$ */
  6. #define ZLIB_INTERNAL
  7. #include "zlib.h"
  8. /* ===========================================================================
  9. Decompresses the source buffer into the destination buffer. sourceLen is
  10. the byte length of the source buffer. Upon entry, destLen is the total
  11. size of the destination buffer, which must be large enough to hold the
  12. entire uncompressed data. (The size of the uncompressed data must have
  13. been saved previously by the compressor and transmitted to the decompressor
  14. by some mechanism outside the scope of this compression library.)
  15. Upon exit, destLen is the actual size of the compressed buffer.
  16. uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  17. enough memory, Z_BUF_ERROR if there was not enough room in the output
  18. buffer, or Z_DATA_ERROR if the input data was corrupted.
  19. */
  20. int ZEXPORT uncompress (dest, destLen, source, sourceLen)
  21. Bytef *dest;
  22. uLongf *destLen;
  23. const Bytef *source;
  24. uLong sourceLen;
  25. {
  26. z_stream stream;
  27. int err;
  28. stream.next_in = (z_const Bytef *)source;
  29. stream.avail_in = (uInt)sourceLen;
  30. /* Check for source > 64K on 16-bit machine: */
  31. if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  32. stream.next_out = dest;
  33. stream.avail_out = (uInt)*destLen;
  34. if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
  35. stream.zalloc = (alloc_func)0;
  36. stream.zfree = (free_func)0;
  37. err = inflateInit(&stream);
  38. if (err != Z_OK) return err;
  39. err = inflate(&stream, Z_FINISH);
  40. if (err != Z_STREAM_END) {
  41. inflateEnd(&stream);
  42. if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
  43. return Z_DATA_ERROR;
  44. return err;
  45. }
  46. *destLen = stream.total_out;
  47. err = inflateEnd(&stream);
  48. return err;
  49. }