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 */
34 dstream<<"error reading stdin"<<std::endl;
36 dstream<<"error writing stdout"<<std::endl;
39 dstream<<"invalid compression level"<<std::endl;
42 dstream<<"invalid or incomplete deflate data"<<std::endl;
45 dstream<<"out of memory"<<std::endl;
48 dstream<<"zlib version mismatch!"<<std::endl;
51 dstream<<"return value = "<<ret<<std::endl;
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 compressZlib(const std::string &data, std::ostream &os)
110 SharedBuffer<u8> databuf((u8*)data.c_str(), data.size());
111 compressZlib(databuf, os);
114 void decompressZlib(std::istream &is, std::ostream &os)
117 const s32 bufsize = 16384;
118 char input_buffer[bufsize];
119 char output_buffer[bufsize];
123 int input_buffer_len = 0;
129 ret = inflateInit(&z);
131 throw SerializationError("dcompressZlib: inflateInit failed");
135 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
139 z.next_out = (Bytef*)output_buffer;
140 z.avail_out = bufsize;
144 z.next_in = (Bytef*)input_buffer;
145 input_buffer_len = is.readsome(input_buffer, bufsize);
146 z.avail_in = input_buffer_len;
147 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
151 //dstream<<"z.avail_in == 0"<<std::endl;
155 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
156 status = inflate(&z, Z_NO_FLUSH);
157 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
158 bytes_read += is.gcount() - z.avail_in;
159 //dstream<<"bytes_read="<<bytes_read<<std::endl;
161 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
162 || status == Z_MEM_ERROR)
165 throw SerializationError("decompressZlib: inflate failed");
167 int count = bufsize - z.avail_out;
168 //dstream<<"count="<<count<<std::endl;
170 os.write(output_buffer, count);
171 if(status == Z_STREAM_END)
173 //dstream<<"Z_STREAM_END"<<std::endl;
175 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
176 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
177 // Unget all the data that inflate didn't take
178 for(u32 i=0; i < z.avail_in; i++)
181 if(is.fail() || is.bad())
183 dstream<<"unget #"<<i<<" failed"<<std::endl;
184 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
185 throw SerializationError("decompressZlib: unget failed");
196 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
200 compressZlib(data, os);
204 if(data.getSize() == 0)
207 // Write length (u32)
210 writeU32(tmp, data.getSize());
211 os.write((char*)tmp, 4);
213 // We will be writing 8-bit pairs of more_count and byte
215 u8 current_byte = data[0];
216 for(u32 i=1; i<data.getSize(); i++)
219 data[i] != current_byte
223 // write count and byte
224 os.write((char*)&more_count, 1);
225 os.write((char*)¤t_byte, 1);
227 current_byte = data[i];
234 // write count and byte
235 os.write((char*)&more_count, 1);
236 os.write((char*)¤t_byte, 1);
239 void decompress(std::istream &is, std::ostream &os, u8 version)
243 decompressZlib(is, os);
250 is.read((char*)tmp, 4);
251 u32 len = readU32(tmp);
253 // We will be reading 8-bit pairs of more_count and byte
260 is.read((char*)&more_count, 1);
262 is.read((char*)&byte, 1);
265 throw SerializationError("decompress: stream ended halfway");
267 for(s32 i=0; i<(u16)more_count+1; i++)
268 os.write((char*)&byte, 1);
270 count += (u16)more_count+1;