From 7384ffe731a3e78058d84b3d78005e325635ee29 Mon Sep 17 00:00:00 2001
From: Karsten Rink <karsten.rink@ufz.de>
Date: Mon, 26 Nov 2012 12:40:29 +0100
Subject: [PATCH] added compress + uncompress methods for bytestreams using
 zlib

---
 CMakeLists.txt                           |  1 +
 FileIO/RapidXmlIO/zLibDataCompressor.cpp | 64 ++++++++++++++++++++++++
 FileIO/RapidXmlIO/zLibDataCompressor.h   | 39 +++++++++++++++
 3 files changed, 104 insertions(+)
 create mode 100644 FileIO/RapidXmlIO/zLibDataCompressor.cpp
 create mode 100644 FileIO/RapidXmlIO/zLibDataCompressor.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 31767af1e6a..e706b3a45b5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -90,6 +90,7 @@ ENDIF() #OGS_PACKAGING
 ADD_SUBDIRECTORY( ThirdParty )
 INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/ThirdParty )
 INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/ThirdParty/quickcheck )
+INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR}/ThirdParty/zlib )
 
 ADD_SUBDIRECTORY( BaseLib )
 ADD_SUBDIRECTORY( FemLib )
diff --git a/FileIO/RapidXmlIO/zLibDataCompressor.cpp b/FileIO/RapidXmlIO/zLibDataCompressor.cpp
new file mode 100644
index 00000000000..154634ec5e7
--- /dev/null
+++ b/FileIO/RapidXmlIO/zLibDataCompressor.cpp
@@ -0,0 +1,64 @@
+/**
+ * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.net)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.net/LICENSE.txt
+ *
+ * \file zLibDataCompressor.cpp
+ *
+ * Created on 2012-11-26 by Karsten Rink
+ * Based on the vtkZLibDataCompressor-class in VTK 5.6
+ */
+
+#include "zLibDataCompressor.h"
+#include <cstddef>
+#include <iostream>
+#include "zlib/zlib.h"
+
+
+unsigned long zLibDataCompressor::CompressBuffer(const unsigned char* uncompressedData,
+                                      unsigned long uncompressedSize,
+                                      unsigned char* compressedData,
+                                      unsigned long compressionSpace)
+{
+	int CompressionLevel = Z_DEFAULT_COMPRESSION;
+	unsigned long compressedSize = compressionSpace;
+	Bytef* cd = reinterpret_cast<Bytef*>(compressedData);
+	const Bytef* ud = reinterpret_cast<const Bytef*>(uncompressedData);
+
+	// Call zlib's compress function.
+	if(compress2(cd, &compressedSize, ud, uncompressedSize, CompressionLevel) != Z_OK)
+	{
+		std::cout << "Zlib error while compressing data." << std::endl;
+		return 0;
+	}
+
+	return compressedSize;
+}
+
+unsigned long zLibDataCompressor::UncompressBuffer(const unsigned char* compressedData,
+                                        unsigned long compressedSize,
+                                        unsigned char* uncompressedData,
+                                        unsigned long uncompressedSize)
+{
+	unsigned long decSize = uncompressedSize;
+	Bytef* ud = reinterpret_cast<Bytef*>(uncompressedData);
+	const Bytef* cd = reinterpret_cast<const Bytef*>(compressedData);
+
+	// Call zlib's uncompress function.
+	if(uncompress(ud, &decSize, cd, compressedSize) != Z_OK)
+	{
+		std::cout << "Zlib error while uncompressing data." << std::endl;
+		return 0;
+	}
+
+	// Make sure the output size matched that expected.
+	if(decSize != uncompressedSize)
+	{
+		std::cout << "Decompression produced incorrect size. Expected " 
+			      << uncompressedSize << " and got " << decSize << std::endl;
+		return 0;
+	}
+
+	return decSize;
+}
diff --git a/FileIO/RapidXmlIO/zLibDataCompressor.h b/FileIO/RapidXmlIO/zLibDataCompressor.h
new file mode 100644
index 00000000000..e6cc033bc91
--- /dev/null
+++ b/FileIO/RapidXmlIO/zLibDataCompressor.h
@@ -0,0 +1,39 @@
+/**
+ * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.net)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.net/LICENSE.txt
+ *
+ * \file zLibDataCompressor.h
+ *
+ * Created on 2012-11-26 by Karsten Rink
+ * Based on the vtkZLibDataCompressor-class in VTK 5.6
+ */
+
+#ifndef ZLIBDATACOMPRESSOR_H
+#define ZLIBDATACOMPRESSOR_H
+
+
+
+class zLibDataCompressor
+{
+public:
+	unsigned long GetMaximumCompressionSpace(unsigned long size);
+
+	// Compression method required by vtkDataCompressor.
+	static unsigned long CompressBuffer(const unsigned char* uncompressedData,
+	                             unsigned long uncompressedSize,
+	                             unsigned char* compressedData,
+	                             unsigned long compressionSpace);
+
+	// Decompression method required by vtkDataCompressor.
+	static unsigned long UncompressBuffer(const unsigned char* compressedData,
+                                   unsigned long compressedSize,
+                                   unsigned char* uncompressedData,
+                                   unsigned long uncompressedSize);
+private:
+
+};
+
+#endif //ZLIBDATACOMPRESSOR_H
+
-- 
GitLab