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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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"
22 #include "util/serialize.h"
28 /* report a zlib or i/o error */
35 dstream<<"error reading stdin"<<std::endl;
37 dstream<<"error writing stdout"<<std::endl;
40 dstream<<"invalid compression level"<<std::endl;
43 dstream<<"invalid or incomplete deflate data"<<std::endl;
46 dstream<<"out of memory"<<std::endl;
49 dstream<<"zlib version mismatch!"<<std::endl;
52 dstream<<"return value = "<<ret<<std::endl;
56 void compressZlib(SharedBuffer<u8> data, std::ostream &os)
59 const s32 bufsize = 16384;
60 //char input_buffer[bufsize];
61 char output_buffer[bufsize];
70 ret = deflateInit(&z, -1);
72 throw SerializationError("compressZlib: deflateInit failed");
78 int flush = Z_NO_FLUSH;
79 z.next_out = (Bytef*)output_buffer;
80 z.avail_out = bufsize;
84 //z.next_in = (char*)&data[input_i];
85 z.next_in = (Bytef*)&data[input_i];
86 z.avail_in = data.getSize() - input_i;
87 input_i += z.avail_in;
88 if(input_i == (int)data.getSize())
93 status = deflate(&z, flush);
94 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
95 || status == Z_MEM_ERROR)
98 throw SerializationError("compressZlib: deflate failed");
100 int count = bufsize - z.avail_out;
102 os.write(output_buffer, count);
109 void compressZlib(const std::string &data, std::ostream &os)
111 SharedBuffer<u8> databuf((u8*)data.c_str(), data.size());
112 compressZlib(databuf, os);
115 void decompressZlib(std::istream &is, std::ostream &os)
118 const s32 bufsize = 16384;
119 char input_buffer[bufsize];
120 char output_buffer[bufsize];
124 int input_buffer_len = 0;
130 ret = inflateInit(&z);
132 throw SerializationError("dcompressZlib: inflateInit failed");
136 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
140 z.next_out = (Bytef*)output_buffer;
141 z.avail_out = bufsize;
145 z.next_in = (Bytef*)input_buffer;
146 input_buffer_len = is.readsome(input_buffer, bufsize);
147 z.avail_in = input_buffer_len;
148 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
152 //dstream<<"z.avail_in == 0"<<std::endl;
156 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
157 status = inflate(&z, Z_NO_FLUSH);
158 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
159 bytes_read += is.gcount() - z.avail_in;
160 //dstream<<"bytes_read="<<bytes_read<<std::endl;
162 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
163 || status == Z_MEM_ERROR)
166 throw SerializationError("decompressZlib: inflate failed");
168 int count = bufsize - z.avail_out;
169 //dstream<<"count="<<count<<std::endl;
171 os.write(output_buffer, count);
172 if(status == Z_STREAM_END)
174 //dstream<<"Z_STREAM_END"<<std::endl;
176 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
177 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
178 // Unget all the data that inflate didn't take
179 for(u32 i=0; i < z.avail_in; i++)
182 if(is.fail() || is.bad())
184 dstream<<"unget #"<<i<<" failed"<<std::endl;
185 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
186 throw SerializationError("decompressZlib: unget failed");
197 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
201 compressZlib(data, os);
205 if(data.getSize() == 0)
208 // Write length (u32)
211 writeU32(tmp, data.getSize());
212 os.write((char*)tmp, 4);
214 // We will be writing 8-bit pairs of more_count and byte
216 u8 current_byte = data[0];
217 for(u32 i=1; i<data.getSize(); i++)
220 data[i] != current_byte
224 // write count and byte
225 os.write((char*)&more_count, 1);
226 os.write((char*)¤t_byte, 1);
228 current_byte = data[i];
235 // write count and byte
236 os.write((char*)&more_count, 1);
237 os.write((char*)¤t_byte, 1);
240 void decompress(std::istream &is, std::ostream &os, u8 version)
244 decompressZlib(is, os);
251 is.read((char*)tmp, 4);
252 u32 len = readU32(tmp);
254 // We will be reading 8-bit pairs of more_count and byte
261 is.read((char*)&more_count, 1);
263 is.read((char*)&byte, 1);
266 throw SerializationError("decompress: stream ended halfway");
268 for(s32 i=0; i<(u16)more_count+1; i++)
269 os.write((char*)&byte, 1);
271 count += (u16)more_count+1;