diff options
author | Andrei Karas <akaras@inbox.ru> | 2011-01-02 01:48:38 +0200 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2011-01-02 02:41:24 +0200 |
commit | 3eeae12c498d1a4dbe969462d2ba841f77ee3ccb (patch) | |
tree | ff8eab35e732bc0749fc11677c8873a7b3a58704 /tools/tmxcopy/zlibutils.cpp | |
download | manaverse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.tar.gz manaverse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.tar.bz2 manaverse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.tar.xz manaverse-3eeae12c498d1a4dbe969462d2ba841f77ee3ccb.zip |
Initial commit.
This code based on mana client http://www.gitorious.org/mana/mana
and my private repository.
Diffstat (limited to 'tools/tmxcopy/zlibutils.cpp')
-rw-r--r-- | tools/tmxcopy/zlibutils.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
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 <stdlib.h> +#include <iostream> +#include <cassert> +#include <zlib.h> + +/** + * 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); +} +*/ |