Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

  1. /* compress.c -- compress a memory buffer
  2. * Copyright (C) 1995-2002 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* @(#) $Id$ */
  6. #include "zlib.h"
  7. /* ===========================================================================
  8. Compresses the source buffer into the destination buffer. The level
  9. parameter has the same meaning as in deflateInit. sourceLen is the byte
  10. length of the source buffer. Upon entry, destLen is the total size of the
  11. destination buffer, which must be at least 0.1% larger than sourceLen plus
  12. 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  13. compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  14. memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  15. Z_STREAM_ERROR if the level parameter is invalid.
  16. */
  17. int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
  18. Bytef *dest;
  19. uLongf *destLen;
  20. const Bytef *source;
  21. uLong sourceLen;
  22. int level;
  23. {
  24. z_stream stream;
  25. int err;
  26. stream.next_in = (Bytef*)source;
  27. stream.avail_in = (uInt)sourceLen;
  28. #ifdef MAXSEG_64K
  29. /* Check for source > 64K on 16-bit machine: */
  30. if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
  31. #endif
  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. stream.opaque = (voidpf)0;
  38. err = deflateInit(&stream, level);
  39. if (err != Z_OK) return err;
  40. err = deflate(&stream, Z_FINISH);
  41. if (err != Z_STREAM_END) {
  42. deflateEnd(&stream);
  43. return err == Z_OK ? Z_BUF_ERROR : err;
  44. }
  45. *destLen = stream.total_out;
  46. err = deflateEnd(&stream);
  47. return err;
  48. }
  49. /* ===========================================================================
  50. */
  51. int ZEXPORT compress (dest, destLen, source, sourceLen)
  52. Bytef *dest;
  53. uLongf *destLen;
  54. const Bytef *source;
  55. uLong sourceLen;
  56. {
  57. return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
  58. }