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"
24 /* report a zlib or i/o error */
27 fputs("zerr: ", stderr);
31 fputs("error reading stdin\n", stderr);
33 fputs("error writing stdout\n", stderr);
36 fputs("invalid compression level\n", stderr);
39 fputs("invalid or incomplete deflate data\n", stderr);
42 fputs("out of memory\n", stderr);
45 fputs("zlib version mismatch!\n", stderr);
48 dstream<<"return value = "<<ret<<"\n";
52 void compressZlib(SharedBuffer<u8> data, std::ostream &os)
55 const s32 bufsize = 16384;
56 //char input_buffer[bufsize];
57 char output_buffer[bufsize];
66 ret = deflateInit(&z, -1);
68 throw SerializationError("compressZlib: deflateInit failed");
74 int flush = Z_NO_FLUSH;
75 z.next_out = (Bytef*)output_buffer;
76 z.avail_out = bufsize;
80 //z.next_in = (char*)&data[input_i];
81 z.next_in = (Bytef*)&data[input_i];
82 z.avail_in = data.getSize() - input_i;
83 input_i += z.avail_in;
84 if(input_i == (int)data.getSize())
89 status = deflate(&z, flush);
90 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
91 || status == Z_MEM_ERROR)
94 throw SerializationError("compressZlib: deflate failed");
96 int count = bufsize - z.avail_out;
98 os.write(output_buffer, count);
105 void decompressZlib(std::istream &is, std::ostream &os)
108 const s32 bufsize = 16384;
109 char input_buffer[bufsize];
110 char output_buffer[bufsize];
114 int input_buffer_len = 0;
120 ret = inflateInit(&z);
122 throw SerializationError("compressZlib: inflateInit failed");
126 //dstream<<"initial fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
130 z.next_out = (Bytef*)output_buffer;
131 z.avail_out = bufsize;
135 z.next_in = (Bytef*)input_buffer;
136 input_buffer_len = is.readsome(input_buffer, bufsize);
137 z.avail_in = input_buffer_len;
138 //dstream<<"read fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
142 //dstream<<"z.avail_in == 0"<<std::endl;
146 //dstream<<"1 z.avail_in="<<z.avail_in<<std::endl;
147 status = inflate(&z, Z_NO_FLUSH);
148 //dstream<<"2 z.avail_in="<<z.avail_in<<std::endl;
149 bytes_read += is.gcount() - z.avail_in;
150 //dstream<<"bytes_read="<<bytes_read<<std::endl;
152 if(status == Z_NEED_DICT || status == Z_DATA_ERROR
153 || status == Z_MEM_ERROR)
156 throw SerializationError("compressZlib: inflate failed");
158 int count = bufsize - z.avail_out;
159 //dstream<<"count="<<count<<std::endl;
161 os.write(output_buffer, count);
162 if(status == Z_STREAM_END)
164 //dstream<<"Z_STREAM_END"<<std::endl;
166 //dstream<<"z.avail_in="<<z.avail_in<<std::endl;
167 //dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
168 // Unget all the data that inflate didn't take
169 for(u32 i=0; i < z.avail_in; i++)
172 if(is.fail() || is.bad())
174 dstream<<"unget #"<<i<<" failed"<<std::endl;
175 dstream<<"fail="<<is.fail()<<" bad="<<is.bad()<<std::endl;
176 throw SerializationError("compressZlib: unget failed");
187 void compress(SharedBuffer<u8> data, std::ostream &os, u8 version)
191 compressZlib(data, os);
195 if(data.getSize() == 0)
198 // Write length (u32)
201 writeU32(tmp, data.getSize());
202 os.write((char*)tmp, 4);
204 // We will be writing 8-bit pairs of more_count and byte
206 u8 current_byte = data[0];
207 for(u32 i=1; i<data.getSize(); i++)
210 data[i] != current_byte
214 // write count and byte
215 os.write((char*)&more_count, 1);
216 os.write((char*)¤t_byte, 1);
218 current_byte = data[i];
225 // write count and byte
226 os.write((char*)&more_count, 1);
227 os.write((char*)¤t_byte, 1);
230 void decompress(std::istream &is, std::ostream &os, u8 version)
234 decompressZlib(is, os);
241 is.read((char*)tmp, 4);
242 u32 len = readU32(tmp);
244 // We will be reading 8-bit pairs of more_count and byte
251 is.read((char*)&more_count, 1);
253 is.read((char*)&byte, 1);
256 throw SerializationError("decompress: stream ended halfway");
258 for(s32 i=0; i<(u16)more_count+1; i++)
259 os.write((char*)&byte, 1);
261 count += (u16)more_count+1;