2
0

uncompr.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (
  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. Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */
  34. len = *sourceLen;
  35. if (*destLen) {
  36. left = *destLen;
  37. *destLen = 0;
  38. }
  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) return err;
  50. stream.next_out = dest;
  51. stream.avail_out = 0;
  52. do {
  53. if (stream.avail_out == 0) {
  54. stream.avail_out = left > (uLong)max ? max : (uInt)left;
  55. left -= stream.avail_out;
  56. }
  57. if (stream.avail_in == 0) {
  58. stream.avail_in = len > (uLong)max ? max : (uInt)len;
  59. len -= stream.avail_in;
  60. }
  61. err = inflate(&stream, Z_NO_FLUSH);
  62. } while (err == Z_OK);
  63. *sourceLen -= len + stream.avail_in;
  64. if (dest != buf)
  65. *destLen = stream.total_out;
  66. else if (stream.total_out && err == Z_BUF_ERROR)
  67. left = 1;
  68. inflateEnd(&stream);
  69. return err == Z_STREAM_END ? Z_OK :
  70. err == Z_NEED_DICT ? Z_DATA_ERROR :
  71. err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR :
  72. err;
  73. }
  74. int ZEXPORT uncompress (
  75. Bytef *dest,
  76. uLongf *destLen,
  77. const Bytef *source,
  78. uLong sourceLen)
  79. {
  80. return uncompress2(dest, destLen, source, &sourceLen);
  81. }