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"
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 output_buffer[bufsize];
68 ret = deflateInit(&z, -1);
70 throw SerializationError("compressZlib: deflateInit failed");
72 // Point zlib to our input buffer
73 z.next_in = (Bytef*)&data[0];
74 z.avail_in = data.getSize();
78 z.next_out = (Bytef*)output_buffer;
79 z.avail_out = bufsize;
81 status = deflate(&z, Z_FINISH);
82 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
83 || status == Z_MEM_ERROR)
86 throw SerializationError("compressZlib: deflate failed");
88 int count = bufsize - z.avail_out;
90 os.write(output_buffer, count);
91 // This determines zlib has given all output
92 if(status == Z_STREAM_END)
100 void compressZlib(const std::string &data, std::ostream &os)
102 SharedBuffer<u8> databuf((u8*)data.c_str(), data.size());
103 compressZlib(databuf, os);
106 void decompressZlib(std::istream &is, std::ostream &os)
109 const s32 bufsize = 16384;
110 char input_buffer[bufsize];
111 char output_buffer[bufsize];
115 int input_buffer_len = 0;
121 ret = inflateInit(&z);
123 throw SerializationError("dcompressZlib: inflateInit failed");
127 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
131 z.next_out = (Bytef*)output_buffer;
132 z.avail_out = bufsize;
136 z.next_in = (Bytef*)input_buffer;
137 input_buffer_len = is.readsome(input_buffer, bufsize);
138 z.avail_in = input_buffer_len;
139 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
143 //dstream<<"z.avail_in == 0"<<std::endl;
147 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
148 status = inflate(&z, Z_NO_FLUSH);
149 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
150 bytes_read += is.gcount() - z.avail_in;
151 //dstream<<"bytes_read="<<bytes_read<<std::endl;
153 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
154 || status == Z_MEM_ERROR)
157 throw SerializationError("decompressZlib: inflate failed");
159 int count = bufsize - z.avail_out;
160 //dstream<<"count="<<count<<std::endl;
162 os.write(output_buffer, count);
163 if(status == Z_STREAM_END)
165 //dstream<<"Z_STREAM_END"<<std::endl;
167 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
168 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
169 // Unget all the data that inflate didn't take
170 for(u32 i=0; i < z.avail_in; i++)
173 if(is.fail() || is.bad())
175 dstream<<"unget #"<<i<<" failed"<<std::endl;
176 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
177 throw SerializationError("decompressZlib: unget failed");
188 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
192 compressZlib(data, os);
196 if(data.getSize() == 0)
199 // Write length (u32)
202 writeU32(tmp, data.getSize());
203 os.write((char*)tmp, 4);
205 // We will be writing 8-bit pairs of more_count and byte
207 u8 current_byte = data[0];
208 for(u32 i=1; i<data.getSize(); i++)
211 data[i] != current_byte
215 // write count and byte
216 os.write((char*)&more_count, 1);
217 os.write((char*)¤t_byte, 1);
219 current_byte = data[i];
226 // write count and byte
227 os.write((char*)&more_count, 1);
228 os.write((char*)¤t_byte, 1);
231 void decompress(std::istream &is, std::ostream &os, u8 version)
235 decompressZlib(is, os);
242 is.read((char*)tmp, 4);
243 u32 len = readU32(tmp);
245 // We will be reading 8-bit pairs of more_count and byte
252 is.read((char*)&more_count, 1);
254 is.read((char*)&byte, 1);
257 throw SerializationError("decompress: stream ended halfway");
259 for(s32 i=0; i<(u16)more_count+1; i++)
260 os.write((char*)&byte, 1);
262 count += (u16)more_count+1;