Quellcode durchsuchen

Add compressors

Daniele Bartolini vor 13 Jahren
Ursprung
Commit
6961574991

+ 1 - 0
CMakeLists.txt

@@ -16,6 +16,7 @@ set (INCLUDES
 	${CMAKE_SOURCE_DIR}/src/core/containers
 	${CMAKE_SOURCE_DIR}/src/core/math
 	${CMAKE_SOURCE_DIR}/src/core/mem
+	${CMAKE_SOURCE_DIR}/src/core/compressors
 	${CMAKE_SOURCE_DIR}/src/core/streams
 	${CMAKE_SOURCE_DIR}/src/os
 	${CMAKE_SOURCE_DIR}/src/os/linux

+ 11 - 0
src/CMakeLists.txt

@@ -158,6 +158,15 @@ set (MEM_HEADERS
 	core/mem/MallocAllocator.h
 )
 
+set (COMPRESSORS_SRC
+	core/compressors/ZipCompressor.cpp
+)
+
+set (COMPRESSORS_HEADERS
+	core/compressors/Compressor.h
+	core/compressors/ZipCompressor.h
+)
+
 set (INPUT_SRC
 	input/EventDispatcher.cpp
 	input/InputManager.cpp
@@ -251,6 +260,8 @@ set (SOURCES
 	${STREAMS_HEADERS}
 	${MEM_SRC}
 	${MEM_HEADERS}
+	${COMPRESSORS_SRC}
+	${COMPRESSORS_HEADERS}
 	${INPUT_SRC}
 	${INPUT_HEADERS}
 	${LOADERS_SRC}

+ 35 - 0
src/core/compressors/Compressor.h

@@ -0,0 +1,35 @@
+#pragma once
+
+#include "Types.h"
+#include "Allocator.h"
+
+namespace crown
+{
+
+class Compressor
+{
+public:
+						Compressor(Allocator& allocator);
+	virtual 			~Compressor();	
+
+	virtual uint8_t* 	compress(void* data, size_t in_size, size_t& out_size) = 0;
+	virtual uint8_t* 	decompress(const void* data, size_t in_size, size_t& out_size) = 0;
+
+protected:
+	
+	Allocator* 			m_allocator;
+};
+
+inline Compressor::Compressor(Allocator& allocator) :
+	m_allocator(&allocator)
+{
+
+}
+
+inline Compressor::~Compressor()
+{
+	
+}
+
+
+}

+ 100 - 0
src/core/compressors/ZipCompressor.cpp

@@ -0,0 +1,100 @@
+#include <cstring>
+
+#include "zlib.h"
+#include "MathUtils.h"
+#include "ZipCompressor.h"
+
+namespace crown
+{
+
+ZipCompressor::ZipCompressor(Allocator& allocator) :
+	Compressor(allocator),
+	m_data_list(allocator)
+{
+	
+}
+
+ZipCompressor::~ZipCompressor()
+{
+
+}
+
+uint8_t* ZipCompressor::compress(void* data, size_t in_size, size_t& out_size)
+{
+	assert(data != NULL);
+	assert(in_size > 0);
+
+	m_data_list.clear();
+//	uint8_t* out_data = (uint8_t*)m_allocator->allocate(sizeof(uint8_t) * in_size);
+//	uint8_t* last_byte = out_data;
+
+	// memory's chunk max dimension
+	const size_t CHUNK_SIZE = 16384;
+	// incoming buffer
+	uint8_t in[CHUNK_SIZE];
+	// outgoing buffer
+	uint8_t out[CHUNK_SIZE];
+
+	int32_t ret;
+	int32_t flush;
+	unsigned have;
+	z_stream strm;
+
+	strm.zalloc = Z_NULL;
+	strm.zfree = Z_NULL;
+	strm.opaque = Z_NULL;
+	ret = deflateInit(&strm, 6);
+
+	assert(ret == Z_OK);
+
+	size_t bytes_read = 0;
+
+	do
+	{
+		size_t this_step_bytes = math::min(CHUNK_SIZE, in_size - bytes_read);
+		memcpy(in, (uint8_t*)data, sizeof(uint8_t) * this_step_bytes);
+//		read_data_block(in, this_step_bytes); 
+
+		strm.avail_in = this_step_bytes;
+		strm.next_in = in;
+
+		flush = (in_size - bytes_read) <= CHUNK_SIZE ? Z_FINISH : Z_NO_FLUSH;
+
+		do
+		{
+			strm.avail_out = CHUNK_SIZE;
+			strm.next_out = out;
+
+			ret = deflate(&strm, flush);    /* no bad return value */
+			assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+
+			have = CHUNK_SIZE - strm.avail_out;
+			if (have > 0)
+			{
+//				memcpy(last_byte, out, sizeof(uint8_t) * have);
+				m_data_list.push(out, have);			
+//				last_byte += sizeof(uint8_t) * have;	
+				//stream->write_data_block(out, have);
+			}
+		} while (strm.avail_out == 0);
+		assert(strm.avail_in == 0);     /* all input will be used */
+
+		bytes_read += this_step_bytes;
+		/* done when last data in file processed */
+	} while (flush != Z_FINISH);
+	assert(ret == Z_STREAM_END);        /* stream will be complete */
+
+	/* clean up and return */
+	(void)deflateEnd(&strm);
+
+	out_size = strm.total_out;
+
+	return m_data_list.begin(); 
+}
+
+uint8_t* ZipCompressor::decompress(const void* data, size_t in_size, size_t& out_size)
+{
+
+}
+
+}

+ 24 - 0
src/core/compressors/ZipCompressor.h

@@ -0,0 +1,24 @@
+#pragma once
+
+#include "Compressor.h"
+#include "List.h"
+
+namespace crown
+{
+
+class ZipCompressor : Compressor
+{
+public:
+
+				ZipCompressor(Allocator& allocator);
+				~ZipCompressor();
+
+	uint8_t*	compress(void* data, size_t in_size, size_t& out_size);
+	uint8_t* 	decompress(const void* data, size_t in_size, size_t& out_size);
+
+private:
+	
+	List<uint8_t> m_data_list;
+};
+
+} // namespace crown