uncompr.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(dest, destLen, source, sourceLen)
  24. Bytef *dest;
  25. uLongf *destLen;
  26. const Bytef *source;
  27. uLong *sourceLen;
  28. {
  29. z_stream stream;
  30. int err;
  31. const uInt max = (uInt)-1;
  32. uLong len, left;
  33. /* for detection of incomplete stream when *destLen == 0 */
  34. Byte buf[1];
  35. len = *sourceLen;
  36. if (*destLen) {
  37. left = *destLen;
  38. *destLen = 0;
  39. } else {
  40. left = 1;
  41. dest = buf;
  42. }
  43. stream.next_in = (z_const Bytef *)source;
  44. stream.avail_in = 0;
  45. stream.zalloc = (alloc_func)0;
  46. stream.zfree = (free_func)0;
  47. stream.opaque = (voidpf)0;
  48. err = inflateInit(&stream);
  49. if (err != Z_OK)
  50. return err;
  51. stream.next_out = dest;
  52. stream.avail_out = 0;
  53. do {
  54. if (stream.avail_out == 0) {
  55. stream.avail_out = left > (uLong)max ? max : (uInt)left;
  56. left -= stream.avail_out;
  57. }
  58. if (stream.avail_in == 0) {
  59. stream.avail_in = len > (uLong)max ? max : (uInt)len;
  60. len -= stream.avail_in;
  61. }
  62. err = inflate(&stream, Z_NO_FLUSH);
  63. } while (err == Z_OK);
  64. *sourceLen -= len + stream.avail_in;
  65. if (dest != buf)
  66. *destLen = stream.total_out;
  67. else if (stream.total_out && err == Z_BUF_ERROR)
  68. left = 1;
  69. inflateEnd(&stream);
  70. return err == Z_STREAM_END ? Z_OK :
  71. err == Z_NEED_DICT ? Z_DATA_ERROR :
  72. err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
  73. err;
  74. }
  75. int ZEXPORT uncompress(dest, destLen, source, sourceLen)
  76. Bytef *dest;
  77. uLongf *destLen;
  78. const Bytef *source;
  79. uLong sourceLen;
  80. {
  81. return uncompress2(dest, destLen, source, &sourceLen);
  82. }