3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "serialization.h"
27 /* report a zlib or i/o error */
30 fputs("zerr: ", stderr);
34 fputs("error reading stdin\n", stderr);
36 fputs("error writing stdout\n", stderr);
39 fputs("invalid compression level\n", stderr);
42 fputs("invalid or incomplete deflate data\n", stderr);
45 fputs("out of memory\n", stderr);
48 fputs("zlib version mismatch!\n", stderr);
51 dstream<<"return value = "<<ret<<"\n";
55 void compressZlib(SharedBuffer<u8> data, std::ostream &os)
58 const s32 bufsize = 16384;
59 //char input_buffer[bufsize];
60 char output_buffer[bufsize];
69 ret = deflateInit(&z, -1);
71 throw SerializationError("compressZlib: deflateInit failed");
77 int flush = Z_NO_FLUSH;
78 z.next_out = (Bytef*)output_buffer;
79 z.avail_out = bufsize;
83 //z.next_in = (char*)&data[input_i];
84 z.next_in = (Bytef*)&data[input_i];
85 z.avail_in = data.getSize() - input_i;
86 input_i += z.avail_in;
87 if(input_i == (int)data.getSize())
92 status = deflate(&z, flush);
93 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
94 || status == Z_MEM_ERROR)
97 throw SerializationError("compressZlib: deflate failed");
99 int count = bufsize - z.avail_out;
101 os.write(output_buffer, count);
108 void decompressZlib(std::istream &is, std::ostream &os)
111 const s32 bufsize = 16384;
112 char input_buffer[bufsize];
113 char output_buffer[bufsize];
117 int input_buffer_len = 0;
123 ret = inflateInit(&z);
125 throw SerializationError("compressZlib: inflateInit failed");
129 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
133 z.next_out = (Bytef*)output_buffer;
134 z.avail_out = bufsize;
138 z.next_in = (Bytef*)input_buffer;
139 input_buffer_len = is.readsome(input_buffer, bufsize);
140 z.avail_in = input_buffer_len;
141 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
145 //dstream<<"z.avail_in == 0"<<std::endl;
149 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
150 status = inflate(&z, Z_NO_FLUSH);
151 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
152 bytes_read += is.gcount() - z.avail_in;
153 //dstream<<"bytes_read="<<bytes_read<<std::endl;
155 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
156 || status == Z_MEM_ERROR)
159 throw SerializationError("compressZlib: inflate failed");
161 int count = bufsize - z.avail_out;
162 //dstream<<"count="<<count<<std::endl;
164 os.write(output_buffer, count);
165 if(status == Z_STREAM_END)
167 //dstream<<"Z_STREAM_END"<<std::endl;
169 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
170 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
171 // Unget all the data that inflate didn't take
172 for(u32 i=0; i < z.avail_in; i++)
175 if(is.fail() || is.bad())
177 dstream<<"unget #"<<i<<" failed"<<std::endl;
178 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
179 throw SerializationError("compressZlib: unget failed");
190 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
194 compressZlib(data, os);
198 if(data.getSize() == 0)
201 // Write length (u32)
204 writeU32(tmp, data.getSize());
205 os.write((char*)tmp, 4);
207 // We will be writing 8-bit pairs of more_count and byte
209 u8 current_byte = data[0];
210 for(u32 i=1; i<data.getSize(); i++)
213 data[i] != current_byte
217 // write count and byte
218 os.write((char*)&more_count, 1);
219 os.write((char*)¤t_byte, 1);
221 current_byte = data[i];
228 // write count and byte
229 os.write((char*)&more_count, 1);
230 os.write((char*)¤t_byte, 1);
233 void decompress(std::istream &is, std::ostream &os, u8 version)
237 decompressZlib(is, os);
244 is.read((char*)tmp, 4);
245 u32 len = readU32(tmp);
247 // We will be reading 8-bit pairs of more_count and byte
254 is.read((char*)&more_count, 1);
256 is.read((char*)&byte, 1);
259 throw SerializationError("decompress: stream ended halfway");
261 for(s32 i=0; i<(u16)more_count+1; i++)
262 os.write((char*)&byte, 1);
264 count += (u16)more_count+1;