+
+#ifdef FEATURE_COMPRESSION
+/*********************************************************************
+ *
+ * Function : compress_buffer
+ *
+ * Description : Compresses the content of a buffer with zlib's deflate
+ * Allocates a new buffer for the result, free'ing it is
+ * up to the caller.
+ *
+ * Parameters :
+ * 1 : buffer = buffer whose content should be compressed
+ * 2 : buffer_length = length of the buffer
+ * 3 : compression_level = compression level for compress2()
+ *
+ * Returns : NULL on error, otherwise a pointer to the compressed
+ * content of the input buffer.
+ *
+ *********************************************************************/
+char *compress_buffer(char *buffer, size_t *buffer_length, int compression_level)
+{
+ char *compressed_buffer;
+ uLongf new_length;
+ assert(-1 <= compression_level && compression_level <= 9);
+
+ /* Let zlib figure out the maximum length of the compressed data */
+ new_length = compressBound((uLongf)*buffer_length);
+
+ compressed_buffer = malloc(new_length);
+ if (NULL == compressed_buffer)
+ {
+ log_error(LOG_LEVEL_FATAL,
+ "Out of memory allocation compression buffer.");
+ }
+
+ if (Z_OK != compress2((Bytef *)compressed_buffer, &new_length,
+ (Bytef *)buffer, *buffer_length, compression_level))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "compress2() failed. Buffer size: %d, compression level: %d.",
+ new_length, compression_level);
+ freez(compressed_buffer);
+ return NULL;
+ }
+
+ log_error(LOG_LEVEL_RE_FILTER,
+ "Compressed content from %d to %d bytes. Compression level: %d",
+ *buffer_length, new_length, compression_level);
+
+ *buffer_length = (size_t)new_length;
+
+ return compressed_buffer;
+
+}
+#endif
+
+