34 Bytef* outputBuffer = NULL;
35 kkuint32 outputBufferSize = sourceLen * 2;
37 outputBuffer =
new Bytef[outputBufferSize];
39 uchar* compressedBuff = NULL;
40 compressedBuffLen = 0;
69 ret = deflateInit (&strm, Z_DEFAULT_COMPRESSION);
73 compressedBuffLen = 0;
84 strm.avail_in = sourceLen;
85 strm.next_in = (Bytef*)source;
92 strm.avail_out = outputBufferSize;
93 strm.next_out = outputBuffer;
113 ret = deflate (&strm, Z_FINISH);
114 if (ret == Z_STREAM_ERROR)
116 cerr <<
"Compressor::CreateCompressedBuffer ***ERROR** deflate returned error[" << ret <<
"]." << std::endl;
117 compressedBuffLen = 0;
125 kkint32 have = outputBufferSize - strm.avail_out;
127 if (compressedBuff == NULL)
129 compressedBuff =
new uchar[have];
130 compressedBuffLen = have;
131 memcpy (compressedBuff, outputBuffer, have);
135 kkuint32 newCompressedBuffLen = compressedBuffLen + have;
136 uchar* newCompressedBuff =
new uchar[newCompressedBuffLen];
137 memcpy (newCompressedBuff, compressedBuff, compressedBuffLen);
138 memcpy (newCompressedBuff + compressedBuffLen, outputBuffer, have);
139 delete[] compressedBuff;
140 compressedBuff = newCompressedBuff;
141 compressedBuffLen = newCompressedBuffLen;
144 }
while (strm.avail_out == 0);
147 if (strm.avail_in != 0)
150 cerr <<
"Compressor::CreateCompressedBuffer - The input buffer was not fully processed." << std::endl;
151 delete[] compressedBuff; compressedBuff = NULL;
152 delete[] outputBuffer; outputBuffer = NULL;
160 (
void)deflateEnd (&strm);
162 delete[] outputBuffer; outputBuffer = NULL;
164 return compressedBuff;
183 if (compressedBuff == NULL)
189 uchar* unCompressedBuff = NULL;
191 Bytef* outBuffer = NULL;
198 strm.zalloc = Z_NULL;
200 strm.opaque = Z_NULL;
202 strm.next_in = Z_NULL;
204 ret = inflateInit (&strm);
207 cerr <<
"Compressor::Decompress ***ERROR*** zlib function call 'inflateInit' failed." << std::endl;
214 outBufferLen = compressedBuffLen * 4;
215 outBuffer =
new Bytef[outBufferLen];
217 strm.avail_in = compressedBuffLen;
218 strm.next_in = (Bytef*)compressedBuff;
224 strm.avail_out = outBufferLen;
225 strm.next_out = outBuffer;
227 ret = inflate (&strm, Z_NO_FLUSH);
228 if (ret == Z_STREAM_ERROR)
230 cerr <<
"Compressor::Decompress ***ERROR*** zlib function call 'inflate' failed." << std::endl;
231 delete[] outBuffer; outBuffer = NULL;
232 delete[] unCompressedBuff; unCompressedBuff = NULL;
240 case Z_NEED_DICT: ret = Z_DATA_ERROR;
244 cerr <<
"Compressor::Decompress ***ERROR*** zlib function call 'inflate' failed." << std::endl;
245 (
void)inflateEnd (&strm);
246 delete[] outBuffer; outBuffer = NULL;
247 delete[] unCompressedBuff; unCompressedBuff = NULL;
254 have = outBufferLen - strm.avail_out;
255 if (unCompressedBuff == NULL)
257 unCompressedBuff =
new uchar[have];
258 unCompressedLen = have;
259 memcpy (unCompressedBuff, outBuffer, have);
263 kkint32 newUnCompressedLen = unCompressedLen + have;
264 uchar* newUnCompressedBuff =
new uchar[newUnCompressedLen];
265 memcpy (newUnCompressedBuff, unCompressedBuff, unCompressedLen);
266 memcpy (newUnCompressedBuff + unCompressedLen, outBuffer, have);
267 delete[] unCompressedBuff;
268 unCompressedBuff = newUnCompressedBuff;
269 unCompressedLen = newUnCompressedLen;
270 newUnCompressedBuff = NULL;
273 while (strm.avail_out == 0);
276 (
void)inflateEnd(&strm);
283 return unCompressedBuff;
296 uchar*& unCompressedBuff,
302 if (compressedBuff == NULL)
305 unCompressedBuffLen = 0;
313 strm.zalloc = Z_NULL;
315 strm.opaque = Z_NULL;
317 strm.next_in = Z_NULL;
319 ret = inflateInit (&strm);
322 cerr <<
"Compressor::Decompress ***ERROR*** zlib function call 'inflateInit' failed." << std::endl;
323 unCompressedBuffLen = 0;
328 if (unCompressedBuff == NULL)
330 unCompressedBuffSize = compressedBuffLen * 4;
331 unCompressedBuff =
new uchar[unCompressedBuffSize];
334 strm.avail_in = compressedBuffLen;
335 strm.next_in = (Bytef*)compressedBuff;
341 kkuint32 unCompressedBuffLeft = unCompressedBuffSize - unCompressedBuffLen;
342 if ((strm.avail_in * 1.2) > unCompressedBuffLeft)
344 kkuint32 increaseBy = (
kkuint32)((strm.avail_in * 1.2) - unCompressedBuffLeft);
345 increaseBy = Max (increaseBy, (kkuint32)10240);
347 kkuint32 newUncompressedBuffSize = unCompressedBuffSize + increaseBy;
348 uchar* newUnCompressedBuff =
new uchar[newUncompressedBuffSize];
349 memcpy (newUnCompressedBuff, unCompressedBuff, unCompressedBuffSize);
350 delete unCompressedBuff;
351 unCompressedBuff = newUnCompressedBuff;
352 newUnCompressedBuff = NULL;
353 unCompressedBuffSize = newUncompressedBuffSize;
356 strm.avail_out = unCompressedBuffSize - unCompressedBuffLen;
357 strm.next_out = (Bytef*)unCompressedBuff + unCompressedBuffLen;
359 ret = inflate (&strm, Z_NO_FLUSH);
360 if (ret == Z_STREAM_ERROR)
362 cerr <<
"Compressor::Decompress ***ERROR*** zlib function call 'inflate' failed." << std::endl;
363 unCompressedBuffLen = 0;
369 case Z_NEED_DICT: ret = Z_DATA_ERROR;
373 cerr <<
"Compressor::Decompress ***ERROR*** zlib function call 'inflate' failed." << std::endl;
374 (
void)inflateEnd (&strm);
375 unCompressedBuffLen = 0;
380 unCompressedBuffLen = strm.total_out;
382 while (strm.avail_in > 0);
385 (
void)inflateEnd(&strm);
Simple class that will compress and decompress specified buffers using the routines provided in zlib...
unsigned __int32 kkuint32
static void * Decompress(const void *compressedBuff, kkuint32 compressedBuffLen, kkuint32 &unCompressedLen)
static void * CreateCompressedBuffer(void *source, kkuint32 sourceLen, kkuint32 &compressedBuffLen)
static void Decompress(const void *compressedBuff, kkuint32 compressedBuffLen, uchar *&unCompressedBuff, kkuint32 &unCompressedBuffSize, kkuint32 &unCompressedBuffLen)
unsigned char uchar
Unsigned character.
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.