minetest-mapper-cpp/ZlibDecompressor.cpp
Rogier 1fbf6d3962 Use cstdint instead of stdint.h to (hopefully) fix any problems caused by C/C++ mismatches
INT64_MIN was causing problems ('not declared'), because C99 requires some
C++-specific behavior, which C++11 prohibits...

Comments from clang's stdint implementation (http://clang.llvm.org/doxygen/stdint_8h_source.html):

	// C99 7.18.3 Limits of other integer types
	//
	//  Footnote 219, 220: C++ implementations should define these macros only when
	//  __STDC_LIMIT_MACROS is defined before <stdint.h> is included.
	//
	//  Footnote 222: C++ implementations should define these macros only when
	//  __STDC_CONSTANT_MACROS is defined before <stdint.h> is included.
	//
	// C++11 [cstdint.syn]p2:
	//
	//  The macros defined by <cstdint> are provided unconditionally. In particular,
	//  the symbols __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS (mentioned in
	//  footnotes 219, 220, and 222 in the C standard) play no role in C++.
	//
	// C11 removed the problematic footnotes.
2015-05-31 07:08:25 +02:00

72 lines
1.5 KiB
C++

/*
* =====================================================================
* Version: 1.0
* Created: 18.09.2012 10:20:47
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
#include <zlib.h>
#include <cstdint>
#include "ZlibDecompressor.h"
ZlibDecompressor::ZlibDecompressor(const unsigned char *data, std::size_t size):
m_data(data),
m_seekPos(0),
m_size(size)
{
}
ZlibDecompressor::~ZlibDecompressor()
{
}
void ZlibDecompressor::setSeekPos(std::size_t seekPos)
{
m_seekPos = seekPos;
}
std::size_t ZlibDecompressor::seekPos() const
{
return m_seekPos;
}
ustring ZlibDecompressor::decompress()
{
const unsigned char *data = m_data + m_seekPos;
const std::size_t size = m_size - m_seekPos;
ustring buffer;
const size_t BUFSIZE = 128 * 1024;
uint8_t temp_buffer[BUFSIZE];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = Z_NULL;
strm.avail_in = size;
if (inflateInit(&strm) != Z_OK) {
throw DecompressError(strm.msg);
}
strm.next_in = const_cast<unsigned char *>(data);
int ret = 0;
do {
strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer;
ret = inflate(&strm, Z_NO_FLUSH);
buffer += ustring(reinterpret_cast<unsigned char *>(temp_buffer), BUFSIZE - strm.avail_out);
} while (ret == Z_OK);
if (ret != Z_STREAM_END) {
throw DecompressError(strm.msg);
}
m_seekPos += strm.next_in - data;
(void)inflateEnd(&strm);
return buffer;
}