/*
-Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "serialization.h"
-#include "utility.h"
+
+#include "util/serialize.h"
#ifdef _WIN32
#define ZLIB_WINAPI
#endif
/* report a zlib or i/o error */
void zerr(int ret)
{
- fputs("zerr: ", stderr);
+ dstream<<"zerr: ";
switch (ret) {
case Z_ERRNO:
if (ferror(stdin))
- fputs("error reading stdin\n", stderr);
+ dstream<<"error reading stdin"<<std::endl;
if (ferror(stdout))
- fputs("error writing stdout\n", stderr);
+ dstream<<"error writing stdout"<<std::endl;
break;
case Z_STREAM_ERROR:
- fputs("invalid compression level\n", stderr);
+ dstream<<"invalid compression level"<<std::endl;
break;
case Z_DATA_ERROR:
- fputs("invalid or incomplete deflate data\n", stderr);
+ dstream<<"invalid or incomplete deflate data"<<std::endl;
break;
case Z_MEM_ERROR:
- fputs("out of memory\n", stderr);
+ dstream<<"out of memory"<<std::endl;
break;
case Z_VERSION_ERROR:
- fputs("zlib version mismatch!\n", stderr);
+ dstream<<"zlib version mismatch!"<<std::endl;
break;
default:
- dstream<<"return value = "<<ret<<"\n";
+ dstream<<"return value = "<<ret<<std::endl;
}
}
{
z_stream z;
const s32 bufsize = 16384;
- //char input_buffer[bufsize];
char output_buffer[bufsize];
- int input_i = 0;
int status = 0;
int ret;
if(ret != Z_OK)
throw SerializationError("compressZlib: deflateInit failed");
- z.avail_in = 0;
-
+ // Point zlib to our input buffer
+ z.next_in = (Bytef*)&data[0];
+ z.avail_in = data.getSize();
+ // And get all output
for(;;)
{
- int flush = Z_NO_FLUSH;
z.next_out = (Bytef*)output_buffer;
z.avail_out = bufsize;
-
- if(z.avail_in == 0)
- {
- //z.next_in = (char*)&data[input_i];
- z.next_in = (Bytef*)&data[input_i];
- z.avail_in = data.getSize() - input_i;
- input_i += z.avail_in;
- if(input_i == (int)data.getSize())
- flush = Z_FINISH;
- }
- if(z.avail_in == 0)
- break;
- status = deflate(&z, flush);
+
+ status = deflate(&z, Z_FINISH);
if(status == Z_NEED_DICT || status == Z_DATA_ERROR
|| status == Z_MEM_ERROR)
{
int count = bufsize - z.avail_out;
if(count)
os.write(output_buffer, count);
+ // This determines zlib has given all output
+ if(status == Z_STREAM_END)
+ break;
}
deflateEnd(&z);
ret = inflateInit(&z);
if(ret != Z_OK)
- throw SerializationError("compressZlib: inflateInit failed");
+ throw SerializationError("dcompressZlib: inflateInit failed");
z.avail_in = 0;
|| status == Z_MEM_ERROR)
{
zerr(status);
- throw SerializationError("compressZlib: inflate failed");
+ throw SerializationError("decompressZlib: inflate failed");
}
int count = bufsize - z.avail_out;
//dstream<<"count="<<count<<std::endl;
{
dstream<<"unget #"<<i<<" failed"<<std::endl;
dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
- throw SerializationError("compressZlib: unget failed");
+ throw SerializationError("decompressZlib: unget failed");
}
}