From 3eeae12c498d1a4dbe969462d2ba841f77ee3ccb Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 2 Jan 2011 01:48:38 +0200 Subject: Initial commit. This code based on mana client http://www.gitorious.org/mana/mana and my private repository. --- tools/tmxcopy/zlibutils.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tools/tmxcopy/zlibutils.cpp (limited to 'tools/tmxcopy/zlibutils.cpp') diff --git a/tools/tmxcopy/zlibutils.cpp b/tools/tmxcopy/zlibutils.cpp new file mode 100644 index 000000000..39c19aeef --- /dev/null +++ b/tools/tmxcopy/zlibutils.cpp @@ -0,0 +1,122 @@ + +#include +#include +#include +#include + +/** + * Inflates either zlib or gzip deflated memory. The inflated memory is + * expected to be freed by the caller. + */ +int +inflateMemory(unsigned char *in, unsigned int inLength, + unsigned char *&out, unsigned int &outLength) +{ + int bufferSize = 256 * 1024; + int ret; + z_stream strm; + + out = (unsigned char*) malloc(bufferSize); + + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.next_in = in; + strm.avail_in = inLength; + strm.next_out = out; + strm.avail_out = bufferSize; + + ret = inflateInit2(&strm, 15 + 32); + + if (ret != Z_OK) + return ret; + + do + { + if (strm.next_out == NULL) + { + inflateEnd(&strm); + return Z_MEM_ERROR; + } + + ret = inflate(&strm, Z_NO_FLUSH); + assert(ret != Z_STREAM_ERROR); + + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void) inflateEnd(&strm); + return ret; + } + + if (ret != Z_STREAM_END) + { + out = (unsigned char*) realloc(out, bufferSize * 2); + + if (out == NULL) + { + inflateEnd(&strm); + return Z_MEM_ERROR; + } + + strm.next_out = out + bufferSize; + strm.avail_out = bufferSize; + bufferSize *= 2; + } + } + while (ret != Z_STREAM_END); + assert(strm.avail_in == 0); + + outLength = bufferSize - strm.avail_out; + (void) inflateEnd(&strm); + return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; +} + +int +inflateMemory(unsigned char *in, unsigned int inLength, + unsigned char *&out) +{ + unsigned int outLength = 0; + int ret = inflateMemory(in, inLength, out, outLength); + + if (ret != Z_OK || out == NULL) + { + if (ret == Z_MEM_ERROR) + { + std::cerr<<"Error: Out of memory while decompressing map data!"; + } + else if (ret == Z_VERSION_ERROR) + { + std::cerr<<"Error: Incompatible zlib version!"; + } + else if (ret == Z_DATA_ERROR) + { + std::cerr<<"Error: Incorrect zlib compressed data!"; + } + else + { + std::cerr<<"Error: Unknown error while decompressing map data!"; + } + + free(out); + out = NULL; + outLength = 0; + } + + return outLength; +} + +/* +int +compressMemory(unsigned char *in, unsigned int inLength, + unsigned char *&out, unsigned int &outLength) +{ + uLongf fOutLen = outLength; + int ret = compress((Bytef*)out, &fOutLen, (Bytef*)in, inLength); + outLength = fOutLen; + + assert (ret == Z_OK); +} +*/ -- cgit v1.2.3-60-g2f50