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, size_t limit)
105 const s32 bufsize = 16384;
106 char input_buffer[bufsize];
107 char output_buffer[bufsize];
111 int bytes_written = 0;
112 int input_buffer_len = 0;
118 ret = inflateInit(&z);
120 throw SerializationError("dcompressZlib: inflateInit failed");
124 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
128 int output_size = bufsize;
129 z.next_out = (Bytef*)output_buffer;
130 z.avail_out = output_size;
133 int limit_remaining = limit - bytes_written;
134 if (limit_remaining <= 0) {
135 // we're aborting ahead of time - throw an error?
138 if (limit_remaining < output_size) {
139 z.avail_out = output_size = limit_remaining;
145 z.next_in = (Bytef*)input_buffer;
146 is.read(input_buffer, bufsize);
147 input_buffer_len = is.gcount();
148 z.avail_in = input_buffer_len;
149 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
153 //dstream<<"z.avail_in == 0"<<std::endl;
157 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
158 status = inflate(&z, Z_NO_FLUSH);
159 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
160 bytes_read += is.gcount() - z.avail_in;
161 //dstream<<"bytes_read="<<bytes_read<<std::endl;
163 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
164 || status == Z_MEM_ERROR)
167 throw SerializationError("decompressZlib: inflate failed");
169 int count = output_size - z.avail_out;
170 //dstream<<"count="<<count<<std::endl;
172 os.write(output_buffer, count);
173 bytes_written += count;
174 if(status == Z_STREAM_END)
176 //dstream<<"Z_STREAM_END"<<std::endl;
178 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
179 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
180 // Unget all the data that inflate didn't take
181 is.clear(); // Just in case EOF is set
182 for(u32 i=0; i < z.avail_in; i++)
185 if(is.fail() || is.bad())
187 dstream<<"unget #"<<i<<" failed"<<std::endl;
188 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
189 throw SerializationError("decompressZlib: unget failed");
200 void compress(const SharedBuffer<u8> &data, std::ostream &os, u8 version)
204 compressZlib(*data ,data.getSize(), os);
208 if(data.getSize() == 0)
211 // Write length (u32)
214 writeU32(tmp, data.getSize());
215 os.write((char*)tmp, 4);
217 // We will be writing 8-bit pairs of more_count and byte
219 u8 current_byte = data[0];
220 for(u32 i=1; i<data.getSize(); i++)
223 data[i] != current_byte
227 // write count and byte
228 os.write((char*)&more_count, 1);
229 os.write((char*)¤t_byte, 1);
231 current_byte = data[i];
238 // write count and byte
239 os.write((char*)&more_count, 1);
240 os.write((char*)¤t_byte, 1);
243 void decompress(std::istream &is, std::ostream &os, u8 version)
247 decompressZlib(is, os);
254 is.read((char*)tmp, 4);
255 u32 len = readU32(tmp);
257 // We will be reading 8-bit pairs of more_count and byte
264 is.read((char*)&more_count, 1);
266 is.read((char*)&byte, 1);
269 throw SerializationError("decompress: stream ended halfway");
271 for(s32 i=0; i<(u16)more_count+1; i++)
272 os.write((char*)&byte, 1);
274 count += (u16)more_count+1;