uncompr.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* uncompr.c -- decompress a memory buffer
  2. * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler
  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 size
  11. of the destination buffer, which must be large enough to hold the entire
  12. uncompressed data. (The size of the uncompressed data must have been saved
  13. previously by the compressor and transmitted to the decompressor by some
  14. mechanism outside the scope of this compression library.) Upon exit,
  15. *destLen is the size of the decompressed data and *sourceLen is the number
  16. of source bytes consumed. Upon return, source + *sourceLen points to the
  17. first unused input byte.
  18. uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough
  19. memory, Z_BUF_ERROR if there was not enough room in the output buffer, or
  20. Z_DATA_ERROR if the input data was corrupted, including if the input data is
  21. an incomplete zlib stream.
  22. */
  23. int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
  24. uLong *sourceLen) {
  25. z_stream stream;
  26. int err;
  27. const uInt max = (uInt)-1;
  28. uLong len, left;
  29. Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
  30. len = *sourceLen;
  31. if (*destLen) {
  32. left = *destLen;
  33. *destLen = 0;
  34. }
  35. else {
  36. left = 1;
  37. dest = buf;
  38. }
  39. stream.next_in = (z_const Bytef *)source;
  40. stream.avail_in = 0;
  41. stream.zalloc = (alloc_func)0;
  42. stream.zfree = (free_func)0;
  43. stream.opaque = (voidpf)0;
  44. err = inflateInit(&stream);
  45. if (err != Z_OK) return err;
  46. stream.next_out = dest;
  47. stream.avail_out = 0;
  48. do {
  49. if (stream.avail_out == 0) {
  50. stream.avail_out = left > (uLong)max ? max : (uInt)left;
  51. left -= stream.avail_out;
  52. }
  53. if (stream.avail_in == 0) {
  54. stream.avail_in = len > (uLong)max ? max : (uInt)len;
  55. len -= stream.avail_in;
  56. }
  57. err = inflate(&stream, Z_NO_FLUSH);
  58. } while (err == Z_OK);
  59. *sourceLen -= len + stream.avail_in;
  60. if (dest != buf)
  61. *destLen = stream.total_out;
  62. else if (stream.total_out && err == Z_BUF_ERROR)
  63. left = 1;
  64. inflateEnd(&stream);
  65. return err == Z_STREAM_END ? Z_OK :
  66. err == Z_NEED_DICT ? Z_DATA_ERROR :
  67. err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
  68. err;
  69. }
  70. int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
  71. uLong sourceLen) {
  72. return uncompress2(dest, destLen, source, &sourceLen);
  73. }