3 Copyright (C) 2013 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"
26 /* report a zlib or i/o error */
33 dstream<<"error reading stdin"<<std::endl;
35 dstream<<"error writing stdout"<<std::endl;
38 dstream<<"invalid compression level"<<std::endl;
41 dstream<<"invalid or incomplete deflate data"<<std::endl;
44 dstream<<"out of memory"<<std::endl;
47 dstream<<"zlib version mismatch!"<<std::endl;
50 dstream<<"return value = "<<ret<<std::endl;
54 void compressZlib(const u8 *data, size_t data_size, std::ostream &os, int level)
57 const s32 bufsize = 16384;
58 char output_buffer[bufsize];
66 ret = deflateInit(&z, level);
68 throw SerializationError("compressZlib: deflateInit failed");
70 // Point zlib to our input buffer
71 z.next_in = (Bytef*)&data[0];
72 z.avail_in = data_size;
76 z.next_out = (Bytef*)output_buffer;
77 z.avail_out = bufsize;
79 status = deflate(&z, Z_FINISH);
80 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
81 || status == Z_MEM_ERROR)
84 throw SerializationError("compressZlib: deflate failed");
86 int count = bufsize - z.avail_out;
88 os.write(output_buffer, count);
89 // This determines zlib has given all output
90 if(status == Z_STREAM_END)
97 void compressZlib(const std::string &data, std::ostream &os, int level)
99 compressZlib((u8*)data.c_str(), data.size(), os, level);
102 void decompressZlib(std::istream &is, std::ostream &os)
105 const s32 bufsize = 16384;
106 char input_buffer[bufsize];
107 char output_buffer[bufsize];
111 int input_buffer_len = 0;
117 ret = inflateInit(&z);
119 throw SerializationError("dcompressZlib: inflateInit failed");
123 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
127 z.next_out = (Bytef*)output_buffer;
128 z.avail_out = bufsize;
132 z.next_in = (Bytef*)input_buffer;
133 is.read(input_buffer, bufsize);
134 input_buffer_len = is.gcount();
135 z.avail_in = input_buffer_len;
136 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
140 //dstream<<"z.avail_in == 0"<<std::endl;
144 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
145 status = inflate(&z, Z_NO_FLUSH);
146 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
147 bytes_read += is.gcount() - z.avail_in;
148 //dstream<<"bytes_read="<<bytes_read<<std::endl;
150 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
151 || status == Z_MEM_ERROR)
154 throw SerializationError("decompressZlib: inflate failed");
156 int count = bufsize - z.avail_out;
157 //dstream<<"count="<<count<<std::endl;
159 os.write(output_buffer, count);
160 if(status == Z_STREAM_END)
162 //dstream<<"Z_STREAM_END"<<std::endl;
164 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
165 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
166 // Unget all the data that inflate didn't take
167 is.clear(); // Just in case EOF is set
168 for(u32 i=0; i < z.avail_in; i++)
171 if(is.fail() || is.bad())
173 dstream<<"unget #"<<i<<" failed"<<std::endl;
174 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
175 throw SerializationError("decompressZlib: unget failed");
186 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version)
190 compressZlib(*data ,data.getSize(), os);
194 if(data.getSize() == 0)
197 // Write length (u32)
200 writeU32(tmp, data.getSize());
201 os.write((char*)tmp, 4);
203 // We will be writing 8-bit pairs of more_count and byte
205 u8 current_byte = data[0];
206 for(u32 i=1; i<data.getSize(); i++)
209 data[i] != current_byte
213 // write count and byte
214 os.write((char*)&more_count, 1);
215 os.write((char*)¤t_byte, 1);
217 current_byte = data[i];
224 // write count and byte
225 os.write((char*)&more_count, 1);
226 os.write((char*)¤t_byte, 1);
229 void decompress(std::istream &is, std::ostream &os, u8 version)
233 decompressZlib(is, os);
240 is.read((char*)tmp, 4);
241 u32 len = readU32(tmp);
243 // We will be reading 8-bit pairs of more_count and byte
250 is.read((char*)&more_count, 1);
252 is.read((char*)&byte, 1);
255 throw SerializationError("decompress: stream ended halfway");
257 for(s32 i=0; i<(u16)more_count+1; i++)
258 os.write((char*)&byte, 1);
260 count += (u16)more_count+1;