compress.c 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* compress.c -- compress a memory buffer
  2. * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zbuild.h"
  6. #include "zutil.h"
  7. /* ===========================================================================
  8. * Architecture-specific hooks.
  9. */
  10. #ifdef S390_DFLTCC_DEFLATE
  11. # include "arch/s390/dfltcc_common.h"
  12. #else
  13. /* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings.
  14. * Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */
  15. # define DEFLATE_BOUND_COMPLEN(source_len) 0
  16. #endif
  17. /* ===========================================================================
  18. Compresses the source buffer into the destination buffer. The level
  19. parameter has the same meaning as in deflateInit. sourceLen is the byte
  20. length of the source buffer. Upon entry, destLen is the total size of the
  21. destination buffer, which must be at least 0.1% larger than sourceLen plus
  22. 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  23. compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  24. memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  25. Z_STREAM_ERROR if the level parameter is invalid.
  26. */
  27. int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source,
  28. z_uintmax_t sourceLen, int level) {
  29. PREFIX3(stream) stream;
  30. int err;
  31. const unsigned int max = (unsigned int)-1;
  32. z_size_t left;
  33. left = *destLen;
  34. *destLen = 0;
  35. stream.zalloc = NULL;
  36. stream.zfree = NULL;
  37. stream.opaque = NULL;
  38. err = PREFIX(deflateInit)(&stream, level);
  39. if (err != Z_OK)
  40. return err;
  41. stream.next_out = dest;
  42. stream.avail_out = 0;
  43. stream.next_in = (z_const unsigned char *)source;
  44. stream.avail_in = 0;
  45. do {
  46. if (stream.avail_out == 0) {
  47. stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left;
  48. left -= stream.avail_out;
  49. }
  50. if (stream.avail_in == 0) {
  51. stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen;
  52. sourceLen -= stream.avail_in;
  53. }
  54. err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
  55. } while (err == Z_OK);
  56. *destLen = stream.total_out;
  57. PREFIX(deflateEnd)(&stream);
  58. return err == Z_STREAM_END ? Z_OK : err;
  59. }
  60. /* ===========================================================================
  61. */
  62. int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_uintmax_t *destLen, const unsigned char *source, z_uintmax_t sourceLen) {
  63. return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
  64. }
  65. /* ===========================================================================
  66. If the default memLevel or windowBits for deflateInit() is changed, then
  67. this function needs to be updated.
  68. */
  69. z_uintmax_t Z_EXPORT PREFIX(compressBound)(z_uintmax_t sourceLen) {
  70. z_uintmax_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);
  71. if (complen > 0)
  72. /* Architecture-specific code provided an upper bound. */
  73. return complen + ZLIB_WRAPLEN;
  74. #ifndef NO_QUICK_STRATEGY
  75. return sourceLen /* The source size itself */
  76. + (sourceLen == 0 ? 1 : 0) /* Always at least one byte for any input */
  77. + (sourceLen < 9 ? 1 : 0) /* One extra byte for lengths less than 9 */
  78. + DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */
  79. + DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */
  80. + ZLIB_WRAPLEN; /* zlib wrapper */
  81. #else
  82. return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN;
  83. #endif
  84. }